Organizing Python objects for retrieval


Organizing Python objects for retrieval



I have a Club class and a Player Class. The player class has an attribute Fav.clubs which will have unique club values. So the user is supposed to enter various club names. Based on the club names I must retrieve those club objects and establish the relationship that this particular player has this Fav.clubs.

The attribute Fav.clubs in Player class should store the names of Club. Now what I have to do is, take input from user about Fav.clubs (a list). After that traverse each element in the list and access the string name to find the corresponding club object and then store that object instance in Player class.




Read more numbers in a line in Python

1:



How to format contour lines from Matplotlib
Store all clubs in a dictionary called all_clubs.


Where can I find a high quality music library for python that plays OGG, ACC and/or MP3 which isn't pygame?
The key should be the club-name and the value the club object itself.


python, wrapping class returning the average of the wrapped members
Then you can do all_clubs[clubname] to retrieve the club object for a given name..
My method is being recognized within my own program. Newbie mistake probably
The player might have an attribute club_names which is the list of the unique names you described and a property clubs which might look like this:.
Will Python 3 ever catch on?
class Player(object):     # ... 


Issue reading packets from a pcap file. dpkt module. What gives?
@property def clubs(self): result = [] for name in self.club_names: result.append(all_clubs.get(name)) return result
Alternatively, it might also be a good idea to use a ORM tool like sqlalchemy and a simple file-based or in-memory sqlite database.


How could I check if a number is a perfect square?
Then you have the power of SQL and a extremely good relational mapping.

But if you are new to Python, I wouldn't use something like that, because sqlalchemy is quite a complex topic and the mapping uses some python magic in the background, which you might not understand at the beginning.

Therefore, I would suggest the first method..


2:


Use a dict to store a map between strings and instances of Club:.
clubmap = {   'basketball': Club("Basketball club"),   'chess': Club("Chess club"), }  someplayer.joinclub(clubmap['basketball']) 



96 out of 100 based on 56 user ratings 606 reviews

@