Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On 28/08/18 02:53, boB Stepp wrote: > Wouldn't a single JSON file be wasteful? If I used this program for a > couple of years or so and habitually played a lot of solitaire, that > would be a lot of stuff to load into RAM when on any given solitaire > session I might only play one to three kinds of solitaire. It shouldn't be any more wasteful than if you used separate files. Either you have to load up every file you ever saved or you have some criteria that prevents you loading all of them. The same with a JSON file. Either you load every record in the file or you have some criteria that lets you load a subset. You might want to eventually have some kind of archive function that offloads old games to a separate file, just to keep initial load times down but that's probably not an issue in the short term. > perhaps I am misunderstanding JSON's capabilities as I only have a > cursory knowledge of it from considering it for other projects. Think of it like a file based dictionary. You only instantiate the objects you need. You load objects based on the key. How you identify which objects you need at startup is the clever bit. You haven't told us enough about your workflow for us to suggest a solution there. But the same issue applies whether you use separate files per game or session or a single JSON file. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On Mon, Aug 27, 2018 at 3:44 AM Alan Gauld via Tutor wrote: > > On 27/08/18 04:58, boB Stepp wrote: > >> Maybe JSON for that? Or even a shelve database? > > > > I plan to keep this simple. I will use a ".cfg" file to store game > > configuration information and a ".csv" file to store the actual > > records of hands played. But I will have to be careful how I generate > > the base filenames to avoid duplicates and potential nasty > > user-generated names. Though this project is only meant for my use > > > If you go with a single JSON file or shelve you have > no worries about name clashes. JSON is specifically > designed to store multiple complex object records. > And it retains the readability of CSV (unlike shelve). Wouldn't a single JSON file be wasteful? If I used this program for a couple of years or so and habitually played a lot of solitaire, that would be a lot of stuff to load into RAM when on any given solitaire session I might only play one to three kinds of solitaire. But perhaps I am misunderstanding JSON's capabilities as I only have a cursory knowledge of it from considering it for other projects. OTOH, even if I loaded into RAM all games I might have ever played I doubt I would stress out my RAM capacity, so perhaps this is a non-issue for this type of program on any modern computer. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On 27/08/18 04:58, boB Stepp wrote: > So you are saying do something like: > > class SolitaireGame(): > def __init__(self, name): > self.name = name > > def describe_self(self): > print("This game of solitaire is called", self.name, ".") > > game_objects = {} > def make_new_game_object(name): > global game_objects > game_objects[name[ = SolitaireGame(name) > > make_new_game_object('Chinese Solitaire') > make_new_game_object('Ace Mastery') > make_new_game_object('King Mastery') Yes, although I'd miss out the function and just populate the dict directly. A single line function doesn't really help much unless its a very complicated line or one that might change regularly (eg storing directly in a database or file rather than in memory). game_objects[name] = SolitaireGame(name) If you are concerned about duplicates just add a guard when you read the name: while True: name = input("Name: ") if name in game_objects: print ("duplicate name, try again") else: break Maybe put that in a function... >> Maybe JSON for that? Or even a shelve database? > > I plan to keep this simple. I will use a ".cfg" file to store game > configuration information and a ".csv" file to store the actual > records of hands played. But I will have to be careful how I generate > the base filenames to avoid duplicates and potential nasty > user-generated names. Though this project is only meant for my use If you go with a single JSON file or shelve you have no worries about name clashes. JSON is specifically designed to store multiple complex object records. And it retains the readability of CSV (unlike shelve). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
since your data is somewhat heirarchical - you have records for each game, which can contain from zero to many score records - please consider looking at json or yaml over csv for your persistent storage. they're no harder to program for in Python, and in addition to representing the style of your data better, are more readable as the fields are named. more bulky certainly. On August 26, 2018 9:58:34 PM MDT, boB Stepp wrote: >On Sun, Aug 26, 2018 at 6:10 PM Alan Gauld via Tutor >wrote: >> >> On 26/08/18 23:38, boB Stepp wrote: >> >> > class SolitaireGame(): >> > def __init__(self, name): >> > self.name = name >> >> > Say I go with the aforementioned game with 13 separate scores to >keep >> > track of. The names of these games might be "Two_Mastery", >> > "Three_Mastery", ... , "Ace_Mastery". In principle I want 13 >objects >> > with each one keeping track of each of the above games. Then I >might >> > want to switch to "Spider_Solitaire", keep track of its score, then >go >> > to something else, then back to Mastery, etc. How on earth am I to >> > generate unique identifiers for each of these SolitaireGame objects >in >> > a rational way, not knowing in advance moment to moment what type >of >> > solitaire game I might be playing? >> >> A dictionary of objects keyed by name? > >So you are saying do something like: > >class SolitaireGame(): >def __init__(self, name): >self.name = name > >def describe_self(self): >print("This game of solitaire is called", self.name, ".") > >game_objects = {} >def make_new_game_object(name): >global game_objects >game_objects[name[ = SolitaireGame(name) > >make_new_game_object('Chinese Solitaire') >make_new_game_object('Ace Mastery') >make_new_game_object('King Mastery') >make_new_game_object('Spider') > >If I run the above in the interactive interpreter: >3.6.6: game_objects >{'Chinese Solitaire': <__main__.SolitaireGame object at >0x7f3991d5e400>, 'Ace Mastery': <__main__.SolitaireGame object at >0x7f3991d5e470>, 'King Mastery': <__main__.SolitaireGame object at >0x7f3991d5e438>, 'Spider': <__main__.SolitaireGame object at >0x7f3991d5e4e0>} >3.6.6: game_objects['Spider'].describe_self() >This game of solitaire is called Spider. > >This would seem to work, though I would have to be very careful to not >allow the user to create a new game with the same name (Now a key.) >which would overwrite an already existing game object. > >> If using a GUI add the names to a drop down or listbox >> to ease later selection. > >Ultimately I would add a GUI interface. > >> Does that work for you? > >If what I wrote above describes what you intend, then yes. > >> > between them at will. Of course the intent is to persistently >store >> > these objects on disk upon program closure. >> >> Maybe JSON for that? Or even a shelve database? > >I plan to keep this simple. I will use a ".cfg" file to store game >configuration information and a ".csv" file to store the actual >records of hands played. But I will have to be careful how I generate >the base filenames to avoid duplicates and potential nasty >user-generated names. Though this project is only meant for my use >... > > > >-- >boB >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Sent from my Android device with K-9 Mail. Please excuse my brevity. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On Sun, Aug 26, 2018 at 6:10 PM Alan Gauld via Tutor wrote: > > On 26/08/18 23:38, boB Stepp wrote: > > > class SolitaireGame(): > > def __init__(self, name): > > self.name = name > > > Say I go with the aforementioned game with 13 separate scores to keep > > track of. The names of these games might be "Two_Mastery", > > "Three_Mastery", ... , "Ace_Mastery". In principle I want 13 objects > > with each one keeping track of each of the above games. Then I might > > want to switch to "Spider_Solitaire", keep track of its score, then go > > to something else, then back to Mastery, etc. How on earth am I to > > generate unique identifiers for each of these SolitaireGame objects in > > a rational way, not knowing in advance moment to moment what type of > > solitaire game I might be playing? > > A dictionary of objects keyed by name? So you are saying do something like: class SolitaireGame(): def __init__(self, name): self.name = name def describe_self(self): print("This game of solitaire is called", self.name, ".") game_objects = {} def make_new_game_object(name): global game_objects game_objects[name[ = SolitaireGame(name) make_new_game_object('Chinese Solitaire') make_new_game_object('Ace Mastery') make_new_game_object('King Mastery') make_new_game_object('Spider') If I run the above in the interactive interpreter: 3.6.6: game_objects {'Chinese Solitaire': <__main__.SolitaireGame object at 0x7f3991d5e400>, 'Ace Mastery': <__main__.SolitaireGame object at 0x7f3991d5e470>, 'King Mastery': <__main__.SolitaireGame object at 0x7f3991d5e438>, 'Spider': <__main__.SolitaireGame object at 0x7f3991d5e4e0>} 3.6.6: game_objects['Spider'].describe_self() This game of solitaire is called Spider. This would seem to work, though I would have to be very careful to not allow the user to create a new game with the same name (Now a key.) which would overwrite an already existing game object. > If using a GUI add the names to a drop down or listbox > to ease later selection. Ultimately I would add a GUI interface. > Does that work for you? If what I wrote above describes what you intend, then yes. > > between them at will. Of course the intent is to persistently store > > these objects on disk upon program closure. > > Maybe JSON for that? Or even a shelve database? I plan to keep this simple. I will use a ".cfg" file to store game configuration information and a ".csv" file to store the actual records of hands played. But I will have to be careful how I generate the base filenames to avoid duplicates and potential nasty user-generated names. Though this project is only meant for my use ... -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On Sun, Aug 26, 2018 at 7:48 PM Steven D'Aprano wrote: > > On Sun, Aug 26, 2018 at 05:38:52PM -0500, boB Stepp wrote: > > > I feel that I may be missing something truly obvious. I am pondering > > the design of a solitaire scorekeeper program. It is just meant to be > > an electronic scorekeeper for hands of solitaire that I plan with a > > "real" deck of cards, instead of a computer game. I might want to > > play and track a variety of solitaire games. And I might want to play > > one game for a bit and then switch to another. > > > What you say is a little ambiguous. When you say solitaire, do you mean > the specific card game known as "Solitaire", or do you mean the generic > term for dozens of different solitaire-type single person card games? > > When you describe changing games, do you mean changing from (say) > "Grandfather's Clock" to "Spider" to "Solitaire", or do you mean > start a new game by reshuffling the cards and dealing out a new hand? There are probably hundreds if not thousands of games that generically fit under the description solitaire. I am only interested in ones that I might play that will have a score result for each hand played with all such scores added together to form a cumulative score for a particular game. > It might help if you explain how you currently track these games, on > paper. The example that I partially gave in my original posting was a game I was told was called "Mastery" when I was a child. In it you count out a pile of 13 cards that is placed to the player's left, face up. Then to the right of this "pile" are placed 4 cards in a row, which is the area where you can play red on black, black on red in descending sequence. Finally you place a single card face up above all of this. In many games, this is where aces are placed and you eventually build up stacks of each suit until you, if fortunate, exhaust the entire deck in this area, where you would have four suit stacks going "A, 2, 3, ... , J, Q, K" in that order. In Mastery, the starting card can be any of the 13 cards. Say it was a 5. Then you would try to get all of the cards into segregated suit stacks where the bottom-most card was the 5 of each suit. So the sequence in a perfectly played game would be "5, 6, 7, ... , J, Q, K, A, 2, 3, 4" In the end any cards in the left-most pile count 2 points against you, while every card in the top-most up to 4 stacks count 1 point for you. So the worst score possible on a hand would be "-26", while the best score possible would be "+52". I hope that this example is clearer than mud! The adult who taught me this particular game when I was a kid kept a spiral bound notebook which he divided into 13 sections, one for each rank of card (A, 2, 3, ... , J, Q, K). Any one of these ranks might start the top-most piles of a particular hand. So for each hand played he would write down the new cumulative score for that section of his notebook. But there are other types of solitaire that I play that might be scored differently. I want my program to work with any such game where a given played hand can have a worst score, a best score, or any integer in between, where the cumulative score over all hands played of that particular game type would reflect the current state of the game. The way the scorekeeper program would work as I currently envisage it would be the player opens an existing game from disk, starts a new game or switches to a game already open. If it is a new game the player will be asked for the minimum possible score per hand, the maximum possible score per hand, and a name for that particular solitaire game he/she wishes to keep track of the cumulative scores for. As for the persistent storage I will have two files per game, a ".cfg" file storing the min and max possible scores (Later if I add features there might be more things in this file.) and a ".csv" file which will retain in played order the date played, time played, and score for the hand played. I figure cumulative scores can be calculated on the fly from this information. Later on if I like what I've done I might add the ability to do various statistical analyses of the hands played, such as average score per hand, number of instances of a particular score or scores, etc. But right now I'm stuck on how to identify each active object with a valid Python identifier. Alan's suggestion of a dictionary of objects sounds like a possibility, but I have no idea if that is the "best " way to do what I am trying to do. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On Sun, Aug 26, 2018 at 05:38:52PM -0500, boB Stepp wrote: > I feel that I may be missing something truly obvious. I am pondering > the design of a solitaire scorekeeper program. It is just meant to be > an electronic scorekeeper for hands of solitaire that I plan with a > "real" deck of cards, instead of a computer game. I might want to > play and track a variety of solitaire games. And I might want to play > one game for a bit and then switch to another. What you say is a little ambiguous. When you say solitaire, do you mean the specific card game known as "Solitaire", or do you mean the generic term for dozens of different solitaire-type single person card games? When you describe changing games, do you mean changing from (say) "Grandfather's Clock" to "Spider" to "Solitaire", or do you mean start a new game by reshuffling the cards and dealing out a new hand? It might help if you explain how you currently track these games, on paper. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to have unique identifiers for multiple object instances of a given class?
On 26/08/18 23:38, boB Stepp wrote: > class SolitaireGame(): > def __init__(self, name): > self.name = name > Say I go with the aforementioned game with 13 separate scores to keep > track of. The names of these games might be "Two_Mastery", > "Three_Mastery", ... , "Ace_Mastery". In principle I want 13 objects > with each one keeping track of each of the above games. Then I might > want to switch to "Spider_Solitaire", keep track of its score, then go > to something else, then back to Mastery, etc. How on earth am I to > generate unique identifiers for each of these SolitaireGame objects in > a rational way, not knowing in advance moment to moment what type of > solitaire game I might be playing? A dictionary of objects keyed by name? If using a GUI add the names to a drop down or listbox to ease later selection. Does that work for you? > between them at will. Of course the intent is to persistently store > these objects on disk upon program closure. Maybe JSON for that? Or even a shelve database? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor