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] Hi This is Michael Munn and I’m interested of the Python programming language.
see also https://wiki.python.org/moin/PythonEditors On August 26, 2018 5:19:20 PM MDT, Alan Gauld via Tutor wrote: >On 26/08/18 18:42, Michael Munn wrote: > >> I’m using Python 3.6 and I heard a friend of mine told me that He >write his >> code using a word processer called Note pad plus some thing like that >to >> code. > >I assume you are on Windows OS? >In which case you probably mean Notepad++ (like >in the C++ programming language) > >It's a well known programmer's editor for Windows. > >Note that it is not a Word Processor, it has >no support for formatting text (fonts, justification >etc) it is just a powerful text editor, which is >what you need for programming. Most importantly >it saves in plain text not in a binary or XML/HTML >format. > >> Any idea where to get this word Processer? > >A web search for Notepad++ should find it. >However Python comes with a useful programming >environment called Idle which you can use instead. >It is Python specific whereas Notepad++ caters >for many languages but if you are starting out >in Python Idle is a fair choice and its already >installed! > >I don't use Windows for Python work but last >time I looked it was called something like >Python GUI in the Windows start menu. > >> PS This question might sounds Stupid to you all. > >Not at all, choice of programming toolset is >a very personal opinion and you will likely >get several suggestions. > >> I ask you not to laugh at me if I post question like this in the >future. >We won't laugh, its why we are here. > >When posting questions always try to be as specific >as possible, post any code you have and the full >text of any error messages. Also post in plain text >if possibly, email servers tends to mess up code >otherwise. > >A reminder of your OS and Python version is also useful. > >Have fun, > >-- >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 -- 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] Hi This is Michael Munn and I’m interested of the Python programming language.
On 26/08/18 18:42, Michael Munn wrote: > I’m using Python 3.6 and I heard a friend of mine told me that He write his > code using a word processer called Note pad plus some thing like that to > code. I assume you are on Windows OS? In which case you probably mean Notepad++ (like in the C++ programming language) It's a well known programmer's editor for Windows. Note that it is not a Word Processor, it has no support for formatting text (fonts, justification etc) it is just a powerful text editor, which is what you need for programming. Most importantly it saves in plain text not in a binary or XML/HTML format. > Any idea where to get this word Processer? A web search for Notepad++ should find it. However Python comes with a useful programming environment called Idle which you can use instead. It is Python specific whereas Notepad++ caters for many languages but if you are starting out in Python Idle is a fair choice and its already installed! I don't use Windows for Python work but last time I looked it was called something like Python GUI in the Windows start menu. > PS This question might sounds Stupid to you all. Not at all, choice of programming toolset is a very personal opinion and you will likely get several suggestions. > I ask you not to laugh at me if I post question like this in the future. We won't laugh, its why we are here. When posting questions always try to be as specific as possible, post any code you have and the full text of any error messages. Also post in plain text if possibly, email servers tends to mess up code otherwise. A reminder of your OS and Python version is also useful. Have fun, -- 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 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
[Tutor] Hi This is Michael Munn and I’m interested of the Python programming language.
Hi All, This is Michael and I have a question about resources on starting to code python. I’m using Python 3.6 and I heard a friend of mine told me that He write his code using a word processer called Note pad plus some thing like that to code. Any idea where to get this word Processer? Please respond if you all get any chance. Best Regards Michael Munn PS This question might sounds Stupid to you all. I just want you all to know that I’m a beginner Pythonist. And I know Nothing about coding at all. I ask you not to laugh at me if I post question like this in the future. Thanks and have a nice day. Best REGARDS michael Munn ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to have unique identifiers for multiple object instances of a given class?
Python 3.6.6, Linux Mint 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. One version of solitaire I might play has thirteen possible separately scored versions, depending on which of thirteen cards gets turned up in the beginning (2, 3, 4, ... , J, Q, K, A). So each hand played needs to be scored under its own card. I would need to be able to switch back and forth between all thirteen at need to enter a score for a hand. So no matter what solitaire game I am playing it seems that it would boil down to: 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? I am *not* wanting to discard one object prior to creating a new one for a new game. I would like to have all such objects to peacefully coexist and be able to switch between them at will. Of course the intent is to persistently store these objects on disk upon program closure. TIA! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor