Re: [Tutor] Tutor Digest, Vol 118, Issue 53
e_time = endTime-startTime > > > > > > print type(startTime) > > > print type(endTime) > > > print type(exe_time) > > > > > > print "startTime: ", startTime > > > print "endTime:", endTime > > > print "exe_time: ", exe_time #how to format this to "D Days, HH: MM: > SS" > > ? > > > #exe_time: 0:00:05.156000 > > > #desired 0 Days, 0h: 00:m: 05s > > > > > print str(exe_time).split('.')[0] > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20131211/143a5ed5/attachment-0001.html > > > > -- > > Message: 4 > Date: Wed, 11 Dec 2013 14:12:52 +0100 > From: spir > To: tutor@python.org > Subject: Re: [Tutor] recursive function example > Message-ID: <52a864d4.9060...@gmail.com> > Content-Type: text/plain; charset=UTF-8; format=flowed > > On 12/11/2013 09:50 AM, Alan Gauld wrote: > > Remember that each time mult() is called it creates > > its own mini-world of variables independent of the > > previous calls. > > That, is a key point. > > Denis > > > -- > > Message: 5 > Date: Wed, 11 Dec 2013 13:37:11 + > From: Mark Lawrence > To: tutor@python.org > Subject: Re: [Tutor] formatting datetime.timedelta to "HH:MM:SS" > Message-ID: > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 11/12/2013 13:12, Jignesh Sutar wrote: > > print str(exe_time).split('.')[0] > > Sorry, I guess my question was why I can't use something similar to > > below on exe_time (of type datetime.timedelta)? Rather than doing string > > manipulation on decimals or colons to extract the same. > > > > now = datetime.now() > > print now.hour > > print now.minute > > print now.year > > > > Old style > > print('%02d:%02d:%04d' % (now.hour, now.minute, now.year)) > > New style > > print('{}:{}:{}'.format(now.hour, now.minute, now.year)) > > Sorry I can never remember the formatting types to go between {} so look > for them around here > http://docs.python.org/3/library/string.html#formatstrings > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > > > -- > > Subject: Digest Footer > > ___ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > > > -- > > End of Tutor Digest, Vol 118, Issue 53 > ** > -- Keith Winston Director, Earth Sun Energy Systems 301-980-6325 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Superb exemplar
On Thu, Dec 12, 2013 at 5:00 AM, wrote: > That was a superb example of how not to post, don't change the subject > line and send the whole digest instead of the part that you're replying > to, no thanks. > You are certainly welcome. I blame gmail. Seriously, though, sorry. I was castigated privately, as well, but nothing beats a good public harangue. Also seriously, this list is very helpful. Thanks for everyone's contributions. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 118, Issue 62
> > Message: 6 > Date: Thu, 12 Dec 2013 23:10:31 -0500 > From: Sky blaze > To: tutor@python.org > Subject: [Tutor] Coding for a Secret Message in a Game > > it'd be amusing to have the message change after the player types something > other than "start" at least 10 times. I've attempted numerous times to code > this, but all of them have failed. Could you help me with the coding? It > should look something like this in the end: > > while start != True: #Infinite loop that doesn't end until "start" is typed > if start_prompt == "start": > start = True #Continues from the title screen > else: > #This is where I'm stuck. I can loop it so it always returns the > command message when > #"start" isn't typed, but changing the message upon having that > occur at least 10 times is > #what's giving me trouble > Probably smarter people than I will have better ideas, but if you make your else an elif and use an expression something like *** counter = 0 # somewhere before the while elif counter < 10: counter += 1 print("type start") else print("just do it") that should (roughly) do it, unless I'm misunderstanding. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 118, Issue 75
On Sun, Dec 15, 2013 at 10:40 AM, wrote: > Are you saying that it can't do list comprehensions, recursive functions > and floating point arithmetic correctly? > My understanding is that the answer here is essentially yes: that quantum computing requires a different approach to the problem, and can be shockingly powerful in some kind of flexible pattern-matching kind of stuff: for example, they talk about "training" one to do image classification. I think they are not programmed in a similar way at all. As long as we're OT on interesting alternative computing, Jeff Hawkins has some AMAZING things going on: read his book "On Intelligence", read papers published online, and/or watch some of the Youtube vids (I think they actually might have the most recent info): again, it's a deeply different way of thinking about computing (analogous perhaps to functional vs. OOP), as near as I can tell, though in this case I think perhaps you could do it in Python... just not efficiently. I think with the quantum stuff you actually can't do the same thing on a binary computer. http://www.youtube.com/watch?v=1_eT5bsS4bQ -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Saving files in Python, IDE's & editors
On Tue, Dec 17, 2013 at 7:26 PM, wrote: > What else do I need to do to make this version of Python an actually > usable programming environment? > > Chris Acreman > Chris, I'm also a noob, but I would recommend you install/use an IDE, such as IDLE which comes free with all (I think) Python installs. An Integrated Development Environment will help with formatting & debugging, but the way I like to use IDLE is open up a window on the right side of my screen with the file I'm working on, and whenever I want to run it I save (ctrl-S, or menu) and run (F5, or menu), and then watch it go in the other window. Very efficient. There are quite a few other IDE's, free and not, but I don't really see the value for a beginner (but then, I'm just a beginner!). You didn't mention what operating system (or even what version of Python) you are using, this will likely influence the choices others offer. It is completely possible to do everything without an IDE, though AFAIK most people end up using IDEs or editors that can be set up to recognize (and color-code, etc) programming: VIM and EMACs are big favorites. I can't imagine the learning curve of the latter is worth it at first, if I correctly surmise your relatively noobiness based on the question... IDLE is simple, you already have it installed probably (a little more work if you are on linux), and it's got a GUI interface with drop-down menus and all that good stuff. Hopefully I didn't just start a flame war... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Pedantry
On Thu, Dec 19, 2013 at 6:00 AM, wrote: > Correction: no practical way to discourage pedants from correcting anyone > has been found yet. Your statement has no effect (at best). > Correction: small positive effects might occur, but complete elimination of pedantry is unlikely. Negative effects are somewhat probable. I only wish I had something on the terminal comments... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 118, Issue 95
On Thu, Dec 19, 2013 at 5:58 PM, wrote: > > So... is there any way to go back up to the previous line and correct the > > syntax? As it is now we are starting the entire file over. It's really > > cumbersome. Is it supposed to be this way? > I notice "Python for Kids" uses Python 3.2, and (p. 9) IDLE, the Integrated Development Environment that comes with Python. But it doesn't sound like YOU'RE using Idle: Look for an icon/"button" that says Idle, instead of Python. Try to make sure that it says something about Python 3.x, since 2.x is different in a few respects (in case you installed the wrong one). If you use Idle, you'll need to open a new window (from within IDLE) in which to create/edit files, and it will prompt you to save them before you can run them (which will automatically happen in the other window): all of this can be done from the menu bar of the "new" window. It will be sort of obvious once you start. It appears to be an interesting book, one of the reviewers is 15 y.o... but it's not light, I will be surprised if a 5 y.o. gets through it. They suggest 10 yo and up. But kids can often rise to challenges, good luck! -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] menu-libre broken?
I did this sequence of commands: sudo add-apt-repository ppa:menulibre-dev/devel sudo apt-get update sudo apt-get install menulibre But at the end of the update I got: W: Failed to fetch http://ppa.launchpad.net/menulibre-dev/devel/ubuntu/dists/saucy/main/source/Sources 404 Not Found W: Failed to fetch http://ppa.launchpad.net/menulibre-dev/devel/ubuntu/dists/saucy/main/binary-amd64/Packages 404 Not Found W: Failed to fetch http://ppa.launchpad.net/menulibre-dev/devel/ubuntu/dists/saucy/main/binary-i386/Packages 404 Not Found which led understandably, following the third command, to: Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package menulibre Sooo: I think I understand that something went wrong (the package was successfully added to my repository, I think, but then not successfully updated, and therefore couldn't be installed, or something like that). But I'm pretty clueless about what to do about it. Which is to say, help! Meanwhile however, I did find that some programs I'd installed (Python, Idle) were added to my menu when I rebooted my machine, after I'd noted they were not automatically added when I installed them with the software manager. I unreasonably blamed it all on Whisker Menu... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] class variables
I am a little confused about class variables: I feel like I've repeatedly seen statements like this: There is only one copy of the class variable and when any one object makes a change to a class variable, that change will be seen by all the other instances. Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy But when I test, I see some interesting things: first (and this is consistent with above) the class variables are created when the class is defined, and can be used even without any instances of the class being created. Second, initially confusing but maybe I understand... there are pointers to the class variables associated with every instance of the object, but if I assign THOSE variables new values, it crerates new, "local"/instance variables. So: Class.pi == 3.14 # defined/set in the class def instance.pi == 3.14 # initially instance.pi = 4 # oops, changed it Class.pi == 3.14 # still Class.pi = "rhubarb" # oops, there I go again instance.pi == 4 # still Sorry if I'm beating this to a pulp, I think I've got it... I'm just confused because the way they are described feels a little confusing, but maybe that's because I'm not taking into account how easy it is to create local variables... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Linux vs. Python
On Fri, Dec 20, 2013 at 6:00 AM, wrote: > This looks more like an Ubuntu issue than a Python one? > Did you mean to send it to the tutor list? > Oops. Sorry -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 118, Issue 99
On Sat, Dec 21, 2013 at 2:14 AM, wrote: > I don't like the terms "class variable" and "instance variable". In the > Python community, these are usually called class and instance attributes > rather than variables or members. > Hey Steven, that was a very generous explanation. Thanks! Very clear. I was floundering over the simple name/concept of attibute, and it had undermined my reading of other material. Your examples were extremely helpful. I think I understood everything you said (after a second reading). I keep hearing about how Python creates namespaces which I think are dictionaries, I'm going to have to look into that further to understand how some of this fits together. I think that's where Python is going when you're talking about looking up attributes (and it would include methods too, unless they're still functions... maybe they're methods for instances and functions for classes? Ok, I don't get that part yet). Anyway, thanks again -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] The Charms of Gmail
On Sat, Dec 21, 2013 at 6:00 AM, wrote: > I'm unsure as to what the subject line has in common with class and > instance variables, would you care to explain it please. > I'm sorry Mark, I'm stuck with using gmail where I have to remember to delete the (essentially invisible) included text of the entire digest I'm responding to, and change the (entirely invisible) subject line. It -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Global namespace/dictionary
On Sat, Dec 21, 2013 at 12:56 PM, wrote: > py> x = 23 > py> d = globals() > py> d['x'] > 23 > py> d['x'] = 42 > py> x > 42 > Well this is sort of fascinating, but a bit confusing: I was playing with this, and it seems like the global dictionary includes not just the name but the entire contents of every dictionary within it... that seems implausibly redundant, so maybe that's just something about how exploring a dictionary functions recursively, or something? Maybe it's correct to say that any dictionaries within a namespace are stored in that namespace, though that doesn't really sound right. >>> d = globals() >>> fubar = {1: 'spam', 'eggs': 2} >>> d {'__name__': '__main__', '__builtins__': , '__doc__': None, '__loader__': , 'fubar': {1: 'spam', 'eggs': 2}, 'd': {...}, '__package__': None} And yes, I did change up my string/int order in key/value positions: I'm fascinated by the flexibility of the data structure. I suspect the answer to my question lies in exactly this facet. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] print in py3
I've been playing with afterhoursprogramming python tutorial, and I was going to write a question about myExample = {'someItem': 2, 'otherItem': 20} for a in myExample: print (a, myExample[a]) print (a) returning ('someItem', 2) someItem ('otherItem', 20) otherItem Which is to say, why would the output formatting be completely different (parens, quotes) between the two print statements, but then when I run it in Python 3.3, it's the more reasonable otherItem 20 otherItem someItem 2 someItem Which I'm much happier with. I assume this just reflects changes in the print command default formatting: is there some reasonably efficient way to see where/when/how this changed? I guess it would be buried in a PEP somewhere? -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Stats packages in Mint 16
I want to play with some stats, but I am having trouble installing numpy on mint 16 Petra/Saucy. Is there some way to do it, or some alternative, or do I not know what I'm talking about (largely true in this case)? -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Chutes & Ladders
I've put together my first small program. It's a simulation of the game Chutes & Ladders. It plays the game and amasses an array of ([multicount] [gamecount]) size, and then crunches simple stats on the average moves, chutes, and ladders for all games in each high-level (multi) pass. Hopefully the code is clear. I don't think I really thought out the OOP element properly... I was thinking of accomodating a multiplayer version in the future, but that would require a significant rewrite. Perhaps Games should be a separate class, I still don't really have OOP down (I'm learning Python, OOP, and Linux simultaneously). There's no interaction between players in the game, so there's not really any reason to do a multiplayer version: I was just using this to familiarize myself with basic Python and maybe some stats. I'd be interested in ALL feedback: aesthetic, functional, design, whatever. I would have used numpy but I couldn't get it installed... I noticed, belatedly, that the math module has arrays, I didn't look to see if they would have made sense to use, I think I remember hearing something about them being inefficient or something. Anyway, thanks. #Chutes & Ladders Simulation 1.0 import random # Landing on a chute (key) causes one to slide down to the corresponding value. chutes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98:78} # Landing on a ladder (key) causes one to slide up to the corresponding value. ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80:100} class Player: """Player class for Chutes & Ladders.""" def __init__(self): self.reset() def reset(self): self.position = 0 self.movecount = 0 self.numchutes = 0 self.numladders = 0 def move(self): """Single move, with chutes, ladders, & out of bounds""" roll = random.randint(1,6) self.movecount += 1 self.position += roll if self.position in chutes.keys(): self.position = chutes.get(self.position) self.numchutes += 1 elif self.position in ladders.keys(): self.position = ladders.get(self.position) self.numladders += 1 elif self.position > 100: # move doesn't count, have to land exactly self.position -= roll def game(self): """Single game""" self.reset() while self.position < 100: self.move() def gameset(self, reps): """A set of games, returning associated stats array""" setstats = [] for i in range(reps): self.game() stat = [i, self.movecount, self.numchutes, self.numladders] setstats.append(stat) return setstats def multisets(self, multi, reps): """A set of game sets, adding another dim to the stats array""" multistats = [] for i in range(multi): set1 = p1.gameset(reps) multistats.append(set1) return multistats p1 = Player() gamecount = 1000 multicount = 10 games = p1.multisets(multicount, gamecount) print("Avg moves Avg chutes Avg ladders") for i in range(multicount): tmulti = games[i] summoves, sumchutes, sumladders = 0, 0, 0 for j in range(gamecount): tgset = tmulti[j] summoves += tgset[1] sumchutes += tgset[2] sumladders += tgset[3] print(str(summoves/gamecount).rjust(9), \ str(sumchutes/gamecount).rjust(12), \ str(sumladders/gamecount).rjust(13)) Sample output is Avg moves Avg chutes Avg ladders 38.9074.192 3.368 38.644.173 3.276 39.5844.259 3.355 39.2544.243 3.411 40.434.399 3.378 39.634.195 3.305 38.5044.046 3.301 39.9174.265 3.281 39.6784.317 3.335 39.5854.229 3.326 Thanks for any suggestions or thoughts. I know this is a very simple program, but I'm very pleased that, once I began to sort out the basics, it fell together pretty readily: I really like Python, though there's a lot to learn. FYI, I recently played C & L with a 4 y.o. friend, it is not otherwise my game of choice ;-) -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] The Charms of Gmail
On Sun, Dec 22, 2013 at 3:55 AM, wrote: > I'm no expert, but would a (semi-)decent email client help? > I do email on a lot of different computers, but I am going to look into options. Meanwhile, I'm going to redouble my efforts to be conscientious. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Stuck on Error
On Sun, Dec 22, 2013 at 3:55 AM, wrote: > import random > > for i in range(1): > RN1 = random.randint(1,75) > As noted before, these "for i in range(1)" statements are pointless: iteration over a range of 1 is no iteration at all. This is exactly equivalent to simply saying RN1 = random.randint(1,75) One thing you might want to do is carefully document your code: add a bunch of lines, preceded by the pound sign # (to make them into comments), to explain what you're trying to do in the next section. This might help you clarify your thinking, and it will definitely help others understand your intention. Like this: # Collect a set of lottery results RN1 = random.randint(1, 75) RN2 = random.randint(1, 75) etc. You could do this entire piece with a list comprehension in one line, but I'm only mentioning it b/c I just learned them. My crude first effort would look like this: RN = [] # create the array RN [RN.append(random.randint(1, 75)) for i in range(5)] # populate the array Also, the fact that you have an identical set of assignments twice makes one wonder if it's time for a function... Mostly though, add comments! -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Stats packages in Mint 16
On Sun, Dec 22, 2013 at 10:09 AM, wrote: > What did you try? I don't have Mint 16, but I'd expect that > > $ sudo apt-get install python3-numpy > > will work. > Well how about that. I looked all over the web, all over scipy, and somehow this was never suggested. I'm still learning about repositories and such. Anyway, looks like it worked, don't have time to test it right now. Thanks! -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] print in py3
On Sun, Dec 22, 2013 at 6:00 AM, wrote: > But in Python 2, the parentheses aren't part of the function call, > because print isn't a function. So what do the brackets do? They are > used for *grouping* terms together. > > In the first line, the brackets group variable a, comma, myExample[a] > together. What does the comma do? It creates a tuple. > Sorry my question was rambling and imprecise. Thanks, I never would have guessed the tuple issue, though it makes perfect sense the second you say it. The other part of my question was: how did you find that PEP? I started looking, and it seemed like I could have taken hours, even though I sort of knew what I was looking for. You must have had a reasonably efficient search strategy/tool. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Global namespace/dictionary
On Sun, Dec 22, 2013 at 6:00 AM, wrote: > They have to live somewhere. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Stats packages in Mint 16
On Sun, Dec 22, 2013 at 6:00 AM, wrote: > As an alternative to numpy, or possibly as well as numpy, you might like > to try the statistics library which will appear in Python 3.4. As the > author of that library, I would be very, very grateful for bug reports > or other feedback. > I now remember reading this before, I will try it, thanks. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Global namespace/dictionary
On Sun, Dec 22, 2013 at 6:00 AM, wrote: > They have to live somewhere. Don't we all. Thanks, this helped clarify, I'll experiment more... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] The Charms of Gmail
On Sun, Dec 22, 2013 at 6:00 AM, wrote: > The problem is that you're responding to the digest. Change your > subscription to receive individual messages. Create a filter that > automatically puts the tutor list emails under a particular label > OMG, so obvious. I actually had to reply to several messages in recent digests, and I utterly resented my clunky technique the second I saw you'd mentioned this. Thanks. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The Charms of Gmail
The way I was doing this before, I had to retype the subject line, and I think that broke threading. Hopefully it will be fixed now that I ditched digests. I don't know what you mean, nik, about (including layer 8!)? [research] Never mind, I get it. But I can't get layer 8 buy-in, too many disparate interests & inattentive participants (me, myself, AND I, after all). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Chutes & Ladders
On Sun, Dec 22, 2013 at 3:04 PM, wrote: > To sum it up: I like what you have, my hints are all about very minor > points > :) > Peter, that's a bunch of great suggestions, I knew there were a lot of places to streamline, make more readable, and probably make faster. Thank you. I find that if I run it [1] [10] times, it takes about 20s on my machine, so I'm eventually going to take a little time seeing how fast I can get it, I think... maybe I'll learn decorators so I can time it/parts of it? I'm really grateful for yours and everyone's help. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Chutes & Ladders
On Sun, Dec 22, 2013 at 3:04 PM, wrote: > > games = [p1.gameset(gamecount) for _ in range(multicount)] So Peter gave me a list of suggesttions, all of which I've incorporated and gotten running, and it's been instructive. But in my haste I included the line above, cut & pasted from his suggestion, without understanding what the underscore is doing in there. I think I understand that at the prompt of the interpreter the underscore returns the last value returned... but I can't really figure out what it's doing here. Also: this statement worked fine: for tmulti in games: print("{moves:9.2f} {chutes:12.2f} {ladders:13.2f}".format( moves=mean(tgset[1] for tgset in tmulti), chutes=mean(tgset[2] for tgset in tmulti), ladders=mean(tgset[3] for tgset in tmulti) )) Which is sort of awesome to me, but in my efforts to riff on it I've been stumped: if I want to further process the arrays/lists my list comprehensions are generating, I sort of can't, since they're gone: that is, if I wanted to use the entire len(tmulti) of them to determine grand total averages and variances, I can't. And if my array is large, I woudn't want to iterate over it over and over to do each of these steps, I think. Anyway, I'm just goofing on all this, for the learning value, but I'll appreciate any thoughts. Thanks. I'm also trying to speed up the program at this point: the way I've set it up now it builds a list of lists (of lists) of all the stats of all the games of all the gamesets of all the multisets. I've visually streamlined it some, without any effect on performance I can detect. Would using arrays, or tuples, or something else be likely to be faster? It's interesting that running it on my 8 y.o. Core 2 Duo and my 2 y.o. Core I5 result in virtually exactly the same speed. Weird. Obviously, it's just doing simple math, pretty much -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: How to post: Was Re: The Charms of Gmail
Yikes, as the progenitor of the antecedent of this screed... I'm sorry! I have found, by and large, this to be a very helpful list, there's no question it has kept me moving forward at times that I might not have in python. Lots of generosity and knowledge. And when someone suggested a way of doing gmail that should resolve some problems,I was happy to do it, since my tedious manual efforts weren't very successful. Anyway, I'd suggest people could consider being a little gentler with each other, but mostly for the umpteenth time I'll appreciate the help I've gotten here. It's a great list. Okay this is funny... I'm responding on my phone, and I can't tell if it's including anything I can't see;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator next()
Also: it works fine (in it's original form, before I changed the print statements) in 2.7, so I'm reinforced in my thinking that it's a 3.3 issue (well, a changed syntax issue). Finally: this isn't really a Python issue I don't think, but when I cut & paste code from the web page above (in Firefox in Linux 16 XFCE) and paste it into the IDLE New File window, it cleans out all the white space, which is slightly suboptimal for Python! Any suggestions? On Fri, Dec 27, 2013 at 1:14 PM, Keith Winston wrote: > I am beginning to think about decorators, generators, and the like. I'm > starting from zero. I've read a few PEPs, and looked at some code, and > maybe things are starting to sink in, though I don't really have enough > framework to hang it all on. It'll come. Anyway, I was trying to run some > timing code I swiped off the web, but ran into a problem with the .next() > function, which I THINK relates to the changes between how generators are > implemented between pre-3.3 and post, but I can't really tell. Here's the > code, stolen without apology from here: > http://enja.org/2011/03/09/a-python-function-timing-decorator/ > > import time > > class Timing(object): > def __init__(self): > self.timings = {} > self.col = self.__collector() > self.col.next() #coroutine syntax > > def __collector(self): > while True: > (name, t) = (yield) #coroutine syntax > if name in self.timings: > self.timings[name]["timings"] += [t] > self.timings[name]["count"] += 1 > self.timings[name]["total"] += t > else: > self.timings[name] = {} #if this entry doesn't exist yet > self.timings[name]["timings"] = [t] > self.timings[name]["count"] = 1 > self.timings[name]["total"] = t > > def __call__(self, func): > """Turn the object into a decorator""" > def wrapper(*arg, **kwargs): > t1 = time.time() #start time > res = func(*arg, **kwargs) #call the originating function > t2 = time.time() #stop time > t = (t2-t1)*1000.0 #time in milliseconds > data = (func.__name__, t) > self.col.send(data) #collect the data > return res > return wrapper > > def __str__(self): > s = "Timings:\n" > print(dir(self)) > for key in self.timings.keys(): > s += "%s | " % key > ts = self.timings[key]["timings"] > count = self.timings[key]["count"] > total = self.timings[key]["total"] > s += "average: %s | total: %s | count: %s\n" % (total / count, > total, count) > return "%s" % s > > > if __name__ == "__main__": > > timings = Timing() > > @timings > def add(x,y): > for i in range(1): > c = x + y > return c > > @timings > def multiply(x,y): > for i in range(1): > c = x * y > return c > > for i in range(100): > add(3.,4.) > multiply(3., 4.) > > print(timings) > > > > And here's the error message: > > Traceback (most recent call last): > File "/home/keithwins/Dropbox/Python/timer1.py", line 50, in > timings = Timing() > File "/home/keithwins/Dropbox/Python/timer1.py", line 7, in __init__ > self.col.next() #coroutine syntax > AttributeError: 'generator' object has no attribute 'next' > -- > Keith > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Generator next()
I am beginning to think about decorators, generators, and the like. I'm starting from zero. I've read a few PEPs, and looked at some code, and maybe things are starting to sink in, though I don't really have enough framework to hang it all on. It'll come. Anyway, I was trying to run some timing code I swiped off the web, but ran into a problem with the .next() function, which I THINK relates to the changes between how generators are implemented between pre-3.3 and post, but I can't really tell. Here's the code, stolen without apology from here: http://enja.org/2011/03/09/a-python-function-timing-decorator/ import time class Timing(object): def __init__(self): self.timings = {} self.col = self.__collector() self.col.next() #coroutine syntax def __collector(self): while True: (name, t) = (yield) #coroutine syntax if name in self.timings: self.timings[name]["timings"] += [t] self.timings[name]["count"] += 1 self.timings[name]["total"] += t else: self.timings[name] = {} #if this entry doesn't exist yet self.timings[name]["timings"] = [t] self.timings[name]["count"] = 1 self.timings[name]["total"] = t def __call__(self, func): """Turn the object into a decorator""" def wrapper(*arg, **kwargs): t1 = time.time() #start time res = func(*arg, **kwargs) #call the originating function t2 = time.time() #stop time t = (t2-t1)*1000.0 #time in milliseconds data = (func.__name__, t) self.col.send(data) #collect the data return res return wrapper def __str__(self): s = "Timings:\n" print(dir(self)) for key in self.timings.keys(): s += "%s | " % key ts = self.timings[key]["timings"] count = self.timings[key]["count"] total = self.timings[key]["total"] s += "average: %s | total: %s | count: %s\n" % (total / count, total, count) return "%s" % s if __name__ == "__main__": timings = Timing() @timings def add(x,y): for i in range(1): c = x + y return c @timings def multiply(x,y): for i in range(1): c = x * y return c for i in range(100): add(3.,4.) multiply(3., 4.) print(timings) And here's the error message: Traceback (most recent call last): File "/home/keithwins/Dropbox/Python/timer1.py", line 50, in timings = Timing() File "/home/keithwins/Dropbox/Python/timer1.py", line 7, in __init__ self.col.next() #coroutine syntax AttributeError: 'generator' object has no attribute 'next' -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator next()
On Sat, Dec 28, 2013 at 4:45 AM, Steven D'Aprano wrote: > That's my idea of an instrumented function :-) > > Feel free to ask for a explanation of how it works. > Hey Steve or anyone: That seems like a very clean timer function, and while I have it working I only understand it caricaturistically... Sooo... I don't really get the inner thing, I tried to look it up, but I don't think I found the right thing, just references to nested functions. I'd like to understand what I'm looking at better, but can't figure out what question to ask... Also: in the timer function, it has a series of attribute assignments to 0/None after the inner function definition... from the behaviour, I assume those are applied once, the first time the timer function is called wrapping a new method/function, and then not again, but that doesn't really make sense. Again, I don't really have a well-formed question, maybe that's clear enough? The functools module seems really interesting. In the previous timer function that I was using, it defined a timer class, and then I had to instantiate it before I could use it, and then it saved a list of timing results. I think in yours, it adds attributes to each instance of a function/method, in which the relevant timings are stored. I guess that's just an observation, I've played with it a bit and it's interesting. You said you didn't like the prior implementation, I'd be interested in anything further you have to say about why: specifically, you called it a coroutine, and a "decorator factory" (tell me you made that up). Any more on that? Sorry to be so vague, I will be continuing my efforts to understand it all even if you (and everyone) are too busy to reply. Thanks for all the help as always. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator next()
Wow Steven, this is great.I'll be brief I'm on a phone > def f(x): > print("Inside the outer function x =", x) > def g(y): # a function nested inside another function > print("Inside the inner function x =", x) > print("Inside the inner function y =", y) > return x + y > return f(23) Actually as I was forming my question I sorted out what's going on. Your examples and descriptions are very clear and exceedingly helpful, you could write a useful book. Thanks for all the help. Now on to your next two posts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator next()
This is all really quite awesome, though I'm sure it'll be a while before these are really available tools for me. I think I get(a bit more than) the basic concept. Thanks! > is a short-cut for this: > > def spam(n): > return "spam"*n > > spam = decorator(spam) > > > This may be a lot to digest in one sitting. Factory functions, > decorators, closures, they're all fantastically useful and powerful > features of Python, but they can be a bit tricky for people to wrap > their brains around. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generator next()
Hah,I must understand,I read it that way! > > Oops, sorry a typo crept into this. That last line ought to be > "return g(23)". Sorry for any confusion. > > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] same python script now running much slower
It seems likely that mentioning what version of Python you're running it on might help in trouble-shooting... if you can run it on a subsection of your data, get it down to a workable amount of time (as in, minutes) and then put timers on the various sections to try to see what's taking so long. My suspicion, almost wholly free of distortion by facts, is that you are running it on a different version of Python, either an upgrade or downgrade was done and something about the script doesn't like that. Is it a terribly long script? On Mon, Dec 30, 2013 at 5:37 PM, William Ray Wing wrote: > On Dec 30, 2013, at 1:37 PM, "Protas, Meredith" > wrote: > > Hi, > > I'm very new to python so I'm sorry about such a basic question. > > I am using a python script generated by another person. I have used this > script multiple times before and it takes around 24 hours to run. > Recently, I have tried to run the script again (the same exact command > lines) and it is much much slower. I have tried on two different computers > with the same result. I used top to see if there were any suspicious > functions that were happening but there seems to not be. I also ran > another python script I used before and that went at the same speed as > before so the problem seems unique to the first python script. > > Does anyone have any idea why it is so much slower now than it used to be > (just around a month ago). > > Thanks for your help! > > Meredith > > > Meredith, This is just a slight expansion on the note you received from > Alan. Is there any chance that the script now is paging itself to death? > That is, if you are reading a huge amount of data into a structure in > memory, and if it no longer fits in available physical memory (either > because the amount of data to be read has grown or the number of other > processes that are occupying memory have grown), then that data structure > may have gone virtual and the OS may be swapping it out to disk. That > would dramatically increase the amount of elapsed wall time the program > takes to run. > > If you can tell us more about what the program actually is doing or > calculating, we might be able to offer more help. > > -Bill > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] lists of lists: more Chutes & Ladders!
I resolved a problem I was having with lists, but I don't understand how! I caught my code inadvertently resetting/zeroing two lists TWICE at the invocation of the game method, and it was leading to all the (gamechutes & gameladders) lists returned by that method being zeroed out except the final time the method is called. That is: the game method below is iterated iter times (this happens outside the method), and every time gamechutes and gameladders (which should be lists of all the chutes and ladders landed on during the game) were returned empty, except for the last time, in which case they were correct. I can see that doing the multiple zeroing is pointless, but I can't understand why it would have any effect on the returned values. Note that self.reset() is called in __init__, so the lists exist before this method is ever called, if I understand properly. def game(self, iter): """Single game""" self.gamechutes[:] = [] #when I take out these two slice assignments, self.gameladders[:] = [] # then gamechutes & gameladders work properly self.gamechutes = [] # these were actually in a call to self.reset() self.gameladders = [] # other stuff in reset() while self.position < 100: gamecandl = self.move() if gamecandl[0] != 0: self.gamechutes.append(gamecandl[0]) if gamecandl[1] != 0: self.gameladders.append(gamecandl[1]) return [iter, self.movecount, self.numchutes, self.numladders, self.gamechutes,self.gameladders] I'm happy to share the rest of the code if you want it, though I'm pretty sure the problem lies here. If it's not obvious, I'm setting myself up to analyse chute & ladder frequency: how often, in a sequence of games, one hits specific chutes & ladders, and related stats. As always, any comments on style or substance are appreciated. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists of lists: more Chutes & Ladders!
Never mind, I figured out that the slice assignment is emptying the previous lists, before the .reset() statements are creating new lists that I then populate and pass on. It makes sense. On Tue, Dec 31, 2013 at 12:59 AM, Keith Winston wrote: > I resolved a problem I was having with lists, but I don't understand how! > I caught my code inadvertently resetting/zeroing two lists TWICE at the > invocation of the game method, and it was leading to all the (gamechutes & > gameladders) lists returned by that method being zeroed out except the > final time the method is called. That is: the game method below is iterated > iter times (this happens outside the method), and every time gamechutes and > gameladders (which should be lists of all the chutes and ladders landed on > during the game) were returned empty, except for the last time, in which > case they were correct. I can see that doing the multiple zeroing is > pointless, but I can't understand why it would have any effect on the > returned values. Note that self.reset() is called in __init__, so the lists > exist before this method is ever called, if I understand properly. > > def game(self, iter): > """Single game""" > > self.gamechutes[:] = [] #when I take out these two slice > assignments, > self.gameladders[:] = [] # then gamechutes & gameladders work > properly > > self.gamechutes = [] # these were actually in a call to > self.reset() > self.gameladders = [] > > # other stuff in reset() > > while self.position < 100: > gamecandl = self.move() > if gamecandl[0] != 0: > self.gamechutes.append(gamecandl[0]) > if gamecandl[1] != 0: > self.gameladders.append(gamecandl[1]) > return [iter, self.movecount, self.numchutes, self.numladders, > self.gamechutes,self.gameladders] > > I'm happy to share the rest of the code if you want it, though I'm pretty > sure the problem lies here. If it's not obvious, I'm setting myself up to > analyse chute & ladder frequency: how often, in a sequence of games, one > hits specific chutes & ladders, and related stats. > > As always, any comments on style or substance are appreciated. > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists of lists: more Chutes & Ladders!
Thanks Denis, I found out about the iter builtin last night, a few hours after I'd coded/posted that. Oops. Thanks for your other comments, I am clearer now about the distinction of creating a new, empty list vs. clearing the same list out, and the subsequent implications on other symbols bound to the same list (is that the right language?). Not to beat a dead horse: you mention the name of the "game" method: in my code, "game" plays a game of Chutes & Ladders (does a series of moves until the game is over), compiles the statistics from said game, and passes those, as a list of ints & lists, to be gathered into a list of lists at the next level ("games" is the list of lists, composed of many "game" lists). I should absolutely document it better, but does that still not seem like a good name to you? Thanks for your feedback. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ValueError: could not convert string to float: '13,2'
Playing with this, a list comprehension is perfect: fnumlist = [float(num.replace(",", ".")) for num in snumlist] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ValueError: could not convert string to float: '13,2'
Hi PierreD, I think if you iterate over your strings with something like this, it will do what you want, if I understand correctly (snum is your string number, like "123,321"): fnum = float(snum.replace(",", ".") keith: rank beginner, take everything with a grain of salt! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Shelve & immutable objects
I'm working my way slowly through Programming Python by Mark Lutz, and as an example of data persistence, he uses this example: -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shelve & immutable objects
So sorry, I hit return: here's the example: import shelve db = shelve.open('class-shelve') sue = db['sue'] sue.giveRaise(.25) db['sue'] = sue tom = db['tom'] tom.giveRaise(.20) db['tom'] = tom db.close() Is it possible to dispense with the assignment/reassignment and just use (open shelve) db['sue'].giveRaise(.25) db['sue'].giveRaise(.25) (close shelve) or is the assignment (or bounding?) necessary to unshelve/reshelve the items... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shelve & immutable objects
Thanks Danny, I don't understand the re-persisted part, but I'll look into it. I realized I hadn't done enough homework to justify a question right after I sent the first half of that one! Happy New Year! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shelve & immutable objects
Thanks for all this Eryksun (and Mark!), but... I don't understand why you brought gdbm in? Is it something underlying shelve, or a better approach, or something else? That last part really puts me in a pickle, and I don't understand why. Separately, I'm also curious about how to process big files. For example, I was trying to play 100 million games of chutes & ladders, and I crashed my machine, I believe: the game results, including 4 ints & 2 short lists of ints per game, are gathered into a list, so it can become a pretty big list. I need to do stats and other analyses on it in the end (okay, I really don't NEED to play 100 million games of chutes & ladders, but as long as I have...): I suppose I could break it into manageable (maybe 1 million games each), but that will make some of the stats either clunky or wrong (I haven't really attacked that part yet). And since I'm not REALLY ready to ask this question, I'll tack it on to the end... I'm also beginning to think about how to speed it up: I'm imagining my two options are going to be to code some sections in a faster language (i.e. C), or maybe to introduce multi-threading since I'm working on a multicore machine generally (core I7), and I'm doing a lot of iterations of the same thing with no important order... seems like a good candidate. Now, I'm probably pretty far from that piece (in my learning process), but this is moving along pretty well so I'm open to suggestions about how to proceed. I've started switching up my code a fair bit to try to make it more OOP, though I'm still rough on that part. K ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] What's in a name?
I've got the beginner's version of a question I think Denis asked recently... If I'm iterating a variable through a series of list names, for future processing, I would like to print the name of the list the variable is set to in a given moment... i.e. for i in [alist, blist, clist] i[3] = "okey dokey " print(i[3], i.name) # this is definitely not the way to do it... output: okey dokey alist okey dokey blist okey dokey clist Is there any way to do that? I'm thinking there may not be, since the variable is actually bound to the content of the list, not the other name... which would be good, in the sense that I'm beginning to understand, but bad in the sense that I need to rethink a piece (or two) of code. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
Hmm, maybe I stumbled upon at least one approach, turning the problem around. Make it something like: for i in ["alist", "blist", "clist"] i[3] = "okey dokey " print(eval(i)[3], i) Of course I've been staring at this for a while, but as soon as I post I find a way... this is my first use of eval(), so it took me a while to stumble upon it. If there are more elegant solutions, I'm all ears. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: What's in a name?
Shoot: I sent this response directly to Mark, without even trimming. Here it is to the list... Hi Mark: sorry for unclarity. I am probably going to make a hash of explaining this, but here goes: I want to iterate a variable across a list of objects, and print both the outputs (wrong word) of said objects, and the name of the objects. Those objects might be lists, or functions, as examples. As a non-iterative example, something like this: a = "max" print(eval(a)(3,4), a) # output: 4 max That's the only way I can figure out how to make it work. Here's an actual code snippet, watch for stype: for func in ["mean", "max", "min", "variance", "stdev"]: print("{moves:9.2f} {chutes:12.2f} {ladders:13.2f} {stype}".format( moves=eval(func)(tgset[1] for tgset in garray), chutes=eval(func)(tgset[2] for tgset in garray), ladders=eval(func)(tgset[3] for tgset in garray), stype=func )) Output: 4.67 0.21 0.79 mean 28.00 1.00 1.00 max 1.00 0.00 0.00 min 23.69 0.17 0.17 variance 4.87 0.41 0.41 stdev I appreciate the point about eval being dangerous, though the second line in your reference does say "if you accept strings to evaluate from untrusted input". Still, I can appreciate how eval() could go off the rails. Is there another way I can do what I want? Sorry for not testing the code I posted earlier. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
Danny: I appreciate your point, but these are just for little code loops, nothing I need to hold on to, like the snippet above: I'm just trying to wrap a few things into one loop, which gives me flexibility about expanding/contracting the stats, for example, that I print (by changing the range list for "func"). Here's another code example, from my recent masterpiece: def print_candl_info(garray): game_array, chute_nums, ladder_nums = {}, chutes, ladders list_index = 3 for i in chute_nums.keys(): chute_nums[i] = 0 # zero all values for i in ladder_nums.keys(): ladder_nums[i] = 0 for clkeys in ["chute_nums", "ladder_nums"]: # increment candl values list_index += 1 # horrible kluge: must be 4 for chutes, 5 for ladders below for corl in eval(clkeys): for game in garray: if corl in game[list_index]: eval(clkeys)[corl] += 1 print("total ", clkeys, "= ", sum(list(eval(clkeys).values( For the purposes of answering this question, ignore the horrible kluge. Though if you have any suggestions on that point, I'm still all ears. Since I couldn't figure out the right way to do that, I left it as more or less the most unforgivable kluge I could find, so I won't be able to sleep until I improve on it. Suggestions are welcome. If it's not obvious, it relies on the fact that the clkeys iterates precisely twice. It might be that the answer lies in rearranging my data structure... I'm about to post the entire program again for any comments, once I clean it up just a BIT more, so if it's not obvious don't worry about it. game = [int, int, int, int, [], []] # game[4] = chutes, game[5] = ladders ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: What's in a name?
Mark wrote: You enjoy making life difficult for yourself :) You've assigned strings to the name func, just assign the functions themselves? Like. > > for func in max, min: > print(func.__name__, func(range(5))) > > Output. > > max 4 > min 0 > > I wouldn't say I enjoy making life difficult for myself, but it is one of my strengths ;) That would work, I think, for the function example I gave you, but the example I gave in response to Danny used the same trick on lists: that is, I want to iterate through a bunch of lists, subsequently printing both list members (via indexing, for example) and the name of the list I'm on. I might even want to do the same of a dict, or some random data structure. Unless it's just really a bad idea to code like this. But certainly when the .__name__ attribute is available, that makes more sense. I'll change that part. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: What's in a name?
I spoke about iterating through a bunch of lists in my last post, but in fact I'm iterating through a bunch of dicts in the example I gave. Sorry. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Fwd: What's in a name?
OMG, another one to Mark and not the list. I'll see if there's something I can adjust in email... On Fri, Jan 3, 2014 at 2:23 AM, Mark Lawrence wrote: > > lista = list(range(5)) > listb = list(reversed(range(5))) > for alist in lista, listb: > print(alist.__class__.__name__, alist) > > list [0, 1, 2, 3, 4] > list [4, 3, 2, 1, 0] > > > Thank you Mark for your unreasonable patience. But the output I'd like to see from you example would be: lista [0, 1, 2, 3, 4] listb [4, 3, 2, 1, 0] Which I think is not possible since the list names are lost by the time the print statement is executing. Unless, of course, I'm wrong. I need the instance name, I guess, not the object name, of an object that includes no __name__ method (I'm stretching on this description) -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: What's in a name?
And to beat that poor horse in the same example, my current way of doing that would be: for alist in "lista", "listb": print(alist, eval(alist)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
Below you will find the full current version of my Chutes or Snakes & Ladders game. I tried to reorganize it into a more OOP structure, and I think I started succeeding, and then something might have gone wrong. There is also something somewhat seriously wrong with it: if I run it as __main__, it works fine, but if I run it after I've loaded it (by candl(100) at the prompt, for example), it is way off: something isn't being reset, and I'm sure it'd be obvious to someone, and I'm going to look back at it, but I can't figure it out yet. Anyway, thanks for all the help so far, I'll probably add persistence, maybe trivial GUI, and definitely more stats/analysis (well, almost definitely)... """ Chutes & Ladders Simulation Simulates a number of games of Chutes & Ladders (Snakes & Ladders). Chutes & Ladders are separate dictionaries to allow tracking of separate stats. Gathers the results into a list of lists of individual game results in the form (per game) of [game_no, moves, len(chutes), len(ladders), [chutes], [ladders]] There is some info redundancy in the list with the len members: [chutes] and [ladders] are lists of the actual chutes and ladders encountered in each game (named by key) """ import random from timer2 import timer from statistics import * # from whence comes mean, variance, stdev # Landing on a chute causes one to slide down to the corresponding value. chutes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98:78} # Landing on a ladder (key) causes one to climb up to the corresponding value. ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80:100} class games: """Game class for Chutes & Ladders.""" def __init__(self): self.reset() def reset(self): self.move_count = 0 self.num_chutes = 0 self.num_ladders = 0 self.chutes_list = [] self.ladders_list = [] #@timer def move(self): """Single move, with chutes, ladders, & out of bounds. Primary action is to move self.position, but returns a list that consists of either the chute or ladder if landed on, if either """ roll = random.randint(1,6) tchutes = 0 tladders = 0 self.move_count += 1 self.position += roll if self.position in chutes: tchutes = self.position self.position = chutes[self.position] self.num_chutes += 1 elif self.position in ladders: tladders = self.position self.position = ladders[self.position] self.num_ladders += 1 elif self.position > 100: # move doesn't count, have to land exactly self.position -= roll return [tchutes, tladders] # only one will be != 0 #@timer def play_game(self, step): """Single game""" self.position = 0 self.reset() while self.position < 100: gamecandl = self.move() # [chute, ladder] or [0, 0] if gamecandl[0] != 0: # populate chutes list self.chutes_list.append(gamecandl[0]) if gamecandl[1] != 0: # populate ladders list self.ladders_list.append(gamecandl[1]) return [step, self.move_count, self.num_chutes, self.num_ladders, self.chutes_list, self.ladders_list] #@timer def play_gameset(self, gamecount1): """A set of games, returning associated stats array""" return [self.play_game(i) for i in range(gamecount1)] def candl(gamecount2): """ play a mess of games, return the results array """ gname = games() game_array = gname.play_gameset(gamecount2) print_gameset_stats(game_array) print_candl_info(game_array) #print(game_array) def print_gameset_stats(garray): print("Total number of games: ", len(garray)) print(" MovesChutesLadders") for func in [mean, max, min, variance, stdev]: print("{moves:9.2f} {chutes:12.2f} {ladders:13.2f} {stype}".format( moves=func(tgset[1] for tgset in garray), chutes=func(tgset[2] for tgset in garray), ladders=func(tgset[3] for tgset in garray), stype=func.__name__ )) def timing_report(): print("game.total, count = ", p1.game.total, p1.game.count) print("move.total, count = ", p1.move.total, p1.move.count) def print_candl_info(garray): """ Create two dictionaries with the same keys as chutes & ladders, but with the total number of times each c or l is traversed (in the entire gameset) as the values. Then, print them. """ chute_nums, ladder_nums = chutes, ladders summarize_game("chutes", chutes, 4, garray) summarize_game("ladders", ladders, 5, garray) def summarize_game(game_name, game_nums, game_index, garray): for i in game_nums.keys(): game_nums[i] = 0 for corl in game_nums: for game in garray: i
Re: [Tutor] What's in a name?
On Fri, Jan 3, 2014 at 2:42 AM, Danny Yoo wrote: > > I hope you don't take offense. But I actually do not understand > print_candl_info(). I thought I did! But then I realized I was > deluding myself. I don't not understand it after all, and that's > after staring at it for more than about thirty minutes. > > I dare you to try to offend me, while offering helpful comments! Okay, I take that back. Anyway, no I don't. > Take that a "canary in the coalmine" kind of comment. eval() can > encourages habits that hurt program readability in a deep way. > > Fair enough. > where we first unroll the original's outer loop out so that there are > no eval()s anywhere. This hurts program length a little, but we can > deal with that. Do you agree so far that the program above preserves > the meaning of what you had before? > > Ha ha, you must understand, because you recreated the original form of the code. First, realize that this is all a learning exercise for me, I am not actually obsessed by Chutes & Ladders. So I was trying to figure out if I could do this thing of iterating over a list of (functions, lists, dictionaries) and print both some output related to the object, and the objects name. But I think you now understand that. In this _particular_ case, I've got that little list_index kluge, which is probably good, since it sort of forces me to try to solve a more general case (or get other people to!... er, thanks!) > > If we can assume that the rewritten code "means" the same thing, then > we can eliminate the duplication by doing something like this: > > > def print_candl_info(garray): > game_array, chute_nums, ladder_nums = {}, chutes, ladders > for i in chute_nums.keys(): chute_nums[i] = 0 > for i in ladder_nums.keys(): ladder_nums[i] = 0 > > summarize_game("chutes", chute_nums, 4, garray) > summarize_game("ladders", ladder_nums, 5, garray) > > I see what you did there. > Hmmm. I don't understand why chute_nums is assigned to chutes, nor > ladder_nums to ladders, and I don't see game_array being used here > yet. We can absorb that: > > > Yes, game_array was left over, sorry. But chutes and ladders are dictionaries that should remain unchanged, for the purpose of future games. chute_nums = chutes only b/c that was an easy way to fill in all the keys: I then zero out the values, and increment the values for every time any given chute or ladder is traversed during a game (this information is in game[4] and game[5], each of which are lists of the chutes or ladders traversed in each given game). I hope that's clear. So I think you can't lose that, or you'd have to fill in those keys differently (which is how I did it at first, but this approach seemed cleaner). > def print_candl_info(garray): > summarize_game("chutes", chutes, 4, garray) > summarize_game("ladders", ladders, 5, garray) > > def summarize_game(game_name, game_nums, game_index, garray): > for i in game_nums.keys(): game_nums[i] = 0 > > for corl in game_nums: > for game in garray: > if corl in game[game_index]: > game_nums[corl] += 1 > print("total ", game_name, "= ", sum(game_nums.values())) > > > This is definitely an improvement, IMO. Summarize_game is _almost_ a really good name, but it isn't really summarizing a single game: it's iterating over all the games in garray (the array of games), and adding up all the particular chutes and ladders occurrences, if I haven't already made that clear... It's sneaky like that. I'm tempted to say summarize_candl, but candl is getting boring... I'll leave it for now. > As a side note: the code above has more latitude than the original > because it can print out a friendlier game name. That is, for > example, you can do this: > > ## > summarize_game(" Chutes! ", chutes, 4, garray) > summarize_game(" Ladders! ", ladders, 5, garray) > ## > Yep, true that. Thanks Danny, this was instructive. Very kind of you. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
On Fri, Jan 3, 2014 at 4:50 AM, Danny Yoo wrote: > Quick thing. Please look at the following small program: > > # > d1 = {1 : 'one'} > d2 = d1 > d2[2] = 'two' > print(d1) > print(d2) > # > > Predict what this prints out. Write your prediction on a piece of paper. > > Then run the program. Does it match what you wrote? > > If not, start asking questions. If so, then I'm puzzled. :P > > > Thanks Danny: I understand this issue when I'm looking right at it, but it can still slip past me with a moments inattention... as soon as you pointed it out, I bruised my forehead by slapping it so hard. Thanks. Actually, it's funnier than that: I now notice that the code I swiped from you passes the original arrays, not even the "copies" which aren't copies. That's what I meant to do: make a copy when I wrote chute_nums = chutes. So I should have passed chute_nums to summarize_game, but it still wouldn't work (because it's not a copy). More below. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
On Fri, Jan 3, 2014 at 9:03 AM, spir wrote: > On 01/03/2014 12:41 PM, Alan Gauld wrote: > >> >> return [step, self.move_count, self.num_chutes, >>> self.num_ladders, self.chutes_list, self.ladders_list] >>> >> >> In OOP you rarely have to return attributes. And since step >> is passed in as an argument the caller already knows [it]. So I >> >> don't think you really need to return anything here. >> > Alas, it's uglier than all that, and probably inherently non-OOP organized. The calling method DOES know the step variable (attribute? it's not part of a class, is it still an attribute?), since it's using it to iterate through a list: but that variable is gone by the time the play_game returns, and yet I want to ensconce it in the data structure to keep track of the different games... And so I pass it in to save it to the data structure. It's ugly, and probably not the right way to do that (post script: I think I figured this out below). Calling method: return [self.play_game(i) for i in range(gamecount1)] def play_game(self, step): # various stuff happens, but step is just passed through to be stored in the structure that the calling method is contributing to return [step, self.move_count, self.num_chutes, self.num_ladders, self.chutes_list, self.ladders_list] > Maybe the intent here (except from the case of step) is to conceptually > isolate another part of code from the current object (here a 'game'), or > from this method (play_game). So that don't they interact in a hidden way > (that one cannot guess from the outside, without reading code in detail). > In other words, to make code _transparent_, which is indeed a common advice > (and one I approve). > > In this, to have the caller explicitely, obviously read information about > the 'game' object, one could just: > return self > > However, this makes no sense if the caller the cirrent method, play_game, > is another method of the 'game' ;-). In this case, indeed return nothing. > > (All this, if I correctly understand and interpret.) > > Denis Well, I sort of mangle OOP still. Here's the issue: I have this play_game method of Game, which returns/fills in the "results" of a game in the form of a list of ints & lists. What I though I want is to play lots of games, gather all those game "results" into a big array (just another list really, of course), and then do stats/analsis on that array. But I think my thinking is broken here: I am trying to create a list of game results, whereas I should be creating a list of games (with their corresponding attributes)... And that should happen altogether outside the Game glass. I imagine I should have another class for that, say GameArray... but that's just a first thought on the next reorganization... So I guess my Game class should have an attribute like game_number, corresponding to step variable above, and GameArray should do something like... game1 = Game() # instance of Game for i in range(gamecount): game_array[i] = game1.play_game(i) (inside play_game: self.game_number = i) And I guess GameArray can hold all the stats/analysis/printing functions, since that's the object they operate on... This sounds like the right direction here, but I don't completely trust my own thinking yet on this stuff. In retrospect, my need for a reset method probably indicated I was going the wrong direction, and probably popped up as soon as I tried to deal with a multitude of games within the Game class itself. thanks as always! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
On Fri, Jan 3, 2014 at 6:41 AM, Alan Gauld wrote: > This is off topic but a couple of points about the OOP stuff... > > thanks Alan, this was helpful. > "If I'm iterating a variable through a series of list names, > for future processing, I would like to print the name of the > list the variable is set to in a given moment... " > > Remember that variables in Python are just names. > So we can rewrite your statement as > > "If I'm iterating a name through a series of list names, for future > processing, I would like to print the name of the list the name is set to > in a given moment... " > > And since the list names are just strings we can write: > > "If I'm iterating a name through a list of strings, for future processing, > I would like to print the name in a given moment... > > So the answer to your question is just to print the string. > The real challenge, as we have discovered, is how to access > the list that is named by the string. And the usual way to > map strings to objects is via a dictionary not by using eval(). > > Here's the thing: I think most of my "problems" go away with better design. But in case I'm wrong, what I was trying to do, which I would think could sometimes be helpful, is printing the name of an object along with it's output/results... I don't think there's any way to do the opposite of eval(), that is, create a string from an object name. Or am I just missing something here (I THINK the right way to do this is add, if necessary, and __name__ method, or at least a .name attribute, to the class... though now that I think about it I think that still doesn't solve the problem, it names the class not the instance. Do you really need the num_xxx variables? > Using len(xxx_list) is more reliable that depending on your > code to maintain the values. It may be that you need them > for a performance optimisation but given that games are > not normally CPU bound that seems unlikely. > > It was something between performance optimization and historical artifact. I'm pretty sure the effect of it's optimization was to slow the program down. It/they are redundant. > > return [tchutes, tladders] # only one will be != 0 >> > > I don't understand the tchutes/tladders stuff? They hold the > target position or zero (the return comment is wrong BTW since both > could be zero). Why not just have a single variable called target > or somesuch? Also if you must return two values its probably better to use > a tuple rather than a list. > > Oh god, you are airing all my dirty laundry. On each move, the position might be a chute, or a ladder, or neither. After I use that position to determine if it's a chute or ladder, it seems like it makes sense to store that info explicitly, to avoid another dictionary lookup. Probably another optimization mistake. So if the first returned list entry has a number in it, it's a chute, and add it to the list of chutes this game traversed, and same with the second entry and ladder... I suspect I should do this entirely differently. > > Shouldn't the self.position assignment be part of reset()? > > It used to be, but then I realized it's not a state I need to save: I meant to change it to a local variable. > > while self.position < 100: >> gamecandl = self.move() # [chute, ladder] or [0, 0] >> if gamecandl[0] != 0: # populate chutes list >> self.chutes_list.append(gamecandl[0]) >> if gamecandl[1] != 0: # populate ladders list >> self.ladders_list.append(gamecandl[1]) >> > > Why don't you do this update of the lists in the move() code. > It's where it logically happens and saves passing back the > list/tuple and then having to test it here. This just adds > extra work. > > Hmm. That seems like a good point. I think it all shifts when I properly implement the Game() class. Next draft. > > return [step, self.move_count, self.num_chutes, >> self.num_ladders, self.chutes_list, self.ladders_list] >> > > In OOP you rarely have to return attributes. And since step > is passed in as an argument the caller already knows. So I > don't think you really need to return anything here. Yes, reading this is what helped me see the direction of the next rewrite. Thanks. > > OK, I see now, you are storing the current state values > after each game. Personally I'd probably create another > method called (get_stats() or similar and have play() return > success/failure. > > o success or failure. You're edging me towards exception handling. Then your line above would be: > > return [self.get_stats() for i in range(gamecount) if self.play()] > > It keeps the purpose of the methods explicit. play() plays the game, > get_stats() returns the data. > > But there is another more OOP approach. > Create a game instance for each iteration. > Then collect the instances which then hold the data. > No need for all the extra arrays, return values, etc. > It's by using and passing
[Tutor] python, speed, game programming
-- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Shoot, sorry for the empty message. Here's what I was going to say: I am gearing up for the next project (yeah, an eventual end to Chutes & Ladders!). It is a typing tutor, I am inclined to use it to learn Dvorak but I would expect it easily adapted to QWERTY or anything else. The basic idea is build a database of the key and response time of every keystroke, with simultaneous background processing going on, based on a customizable set of parameters to select the test text in more or less real time. So for example, I might wish to adjust the interval at which I revisit old material, to be sure I "get" it without becoming bored by it: we could call this the repetition interval, and it might well be a function that increases with time, though it might also be keyed to the speed of recall... all responses are to be saved long-term for subsequent analysis. My concern is with speed. This will have to keep up with (somewhat arbitrarily) fast typing, while doing background processing, with a GUI of course. This illustrates why I was concerned about the fact that my Chutes & Ladders game seems to run at the same speed on my 8 y.o. Core 2 Duo and my 2 y.o. Core I7 with 3-4x as much memory. This WILL require a faster machine and a more significant share of it's resources, if I develop it in the direction I am thinking. I hope Python is a good choice here, though I suppose some modules or classes or parts will have to be recoded into something faster... Any thoughts on this will be appreciated, including how to structure it in such a manner that it might be more portably applied to future versions in entirely different knowledge realms (music/midi input, language/vocabulary/audio output, etc). -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Just to be clear: this is equal parts learning Python project, prototype tutorial software project, OOP practice, and the beginning of a more general inquiry into learning styles/paradigms/parameters... I'm (slowly) writing some of these ideas up in a separate doc. On Fri, Jan 3, 2014 at 4:53 PM, Keith Winston wrote: > Shoot, sorry for the empty message. Here's what I was going to say: > > I am gearing up for the next project (yeah, an eventual end to Chutes & > Ladders!). It is a typing tutor, I am inclined to use it to learn Dvorak > but I would expect it easily adapted to QWERTY or anything else. > > The basic idea is build a database of the key and response time of every > keystroke, with simultaneous background processing going on, based on a > customizable set of parameters to select the test text in more or less real > time. So for example, I might wish to adjust the interval at which I > revisit old material, to be sure I "get" it without becoming bored by it: > we could call this the repetition interval, and it might well be a function > that increases with time, though it might also be keyed to the speed of > recall... all responses are to be saved long-term for subsequent analysis. > > My concern is with speed. This will have to keep up with (somewhat > arbitrarily) fast typing, while doing background processing, with a GUI of > course. This illustrates why I was concerned about the fact that my Chutes > & Ladders game seems to run at the same speed on my 8 y.o. Core 2 Duo and > my 2 y.o. Core I7 with 3-4x as much memory. This WILL require a faster > machine and a more significant share of it's resources, if I develop it in > the direction I am thinking. > > I hope Python is a good choice here, though I suppose some modules or > classes or parts will have to be recoded into something faster... Any > thoughts on this will be appreciated, including how to structure it in such > a manner that it might be more portably applied to future versions in > entirely different knowledge realms (music/midi input, > language/vocabulary/audio output, etc). > > -- > Keith > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Okay, thanks, I'll probably be proceeding over the next few weeks, as I finish up my Chutes & Ladders project... I think I have to do a bit more code/manual reading in proportion to my coding for a bit, also. Thanks for the insights. K On Fri, Jan 3, 2014 at 6:12 PM, spir wrote: > On 01/03/2014 10:53 PM, Keith Winston wrote: > >> My concern is with speed. This will have to keep up with (somewhat >> arbitrarily) fast typing, while doing background processing, with a GUI of >> course. >> > > I wouldn't even bother. Try & see, you may be surprised. There are several > factors at play: > * The core processing in dealing with user input (read, decode, store, > queue key strokes, etc) will be done by low-level (OS) routines written in > C, with only thin Python wrappers (which essentially translate from/to > python data types). > * Similar point for GUI and/or other user interface layer (less so if > you'd use a pure python GUI framework, but in general they're also just > wrappers over C code). > * The ratio between such low-level processing and actual processing code > written and executed in python for your application logic is certainly high > (as is usually the case), unless there is much realtime computation you did > not speak about. > Anyway, just try and see. > > This is similar to how & why python is reputed to be good (read: fast) at > "number crunching": they are standard libs for that just link to low-level > C routines which do about all computation, behind the stage, and very few > application logic remains written *and actually executed* in python (search > "numpy"). > > Denis > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
Thanks Walter, I think I've got the lay of the land roughly, but my grasp is still light and imperfect. I'm pretty confident I shouldn't be doing anything like what you're describing for the level of coding I'm doing, but it's interesting to see the approach. Keith On Fri, Jan 3, 2014 at 6:56 PM, Walter Prins wrote: > Hi, > > The code in my last post got line wrapped which will break if directly > tried out. To avoid possible confusion I paste a version below with > shorter lines which should avoid the problem: > > > import gc, itertools > > def names_of(obj): > """Try to find the names associated to a given object.""" > #Find dict objects from the garbage collector: > refs_dicts = (ref for ref in gc.get_referrers(obj) > if isinstance(ref, dict)) > #Get a single chained generator for all the iteritems() iterators > #in all the dicts > keyvalue_pairs = itertools.chain.from_iterable( > refd.iteritems() for refd in refs_dicts) > #Return a list of keys (names) where the value (obj) is the one > #we're looking for: > return [k for k, v in keyvalue_pairs if v is obj] > > x = [] > y = x > z = [x] > > print names_of(z[0]) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Just to be clear, what I'm asking this typing tutor to do is vastly more than normal, albeit still not seemingly very much. In most programs, they give you a sentence or paragraph to type, and then time how long it takes. I'm talking about timing every keypress, and modifying the text stream based on that. The thing that put me on edge was noticing that my simple Chutes & Ladders game doesn't go ANY faster on a machine that benchmarks perhaps 1000 times faster than another... Keith On Fri, Jan 3, 2014 at 8:17 PM, Alan Gauld wrote: > On 03/01/14 21:53, Keith Winston wrote: > > Ladders!). It is a typing tutor, I am inclined to use it to learn Dvorak >> but I would expect it easily adapted to QWERTY or anything else. >> ... >> >> >> My concern is with speed. This will have to keep up with (somewhat >> arbitrarily) fast typing, >> > > Lets see. The speed record for touch typing is around 150 wpm with average > word being about 5 chars, so a speed of about 750 cpm > or 12.5cps That's about 80ms between letters. > > Python on a modern PC can probably execute around 100k lines > of code(*) per second or 100 per millisecond. That's 8k lines > executed between each keypress for the worlds fastest typist. > > I used to use a typing tutor that was written in old GW Basic > on the original IBM PC (speed 4.7MHz) and it had no problem > analyzing my stats (albeit at a modest 40-50 wpm). > > I'd worry about speed after you find you need to. > > (*)Caveat: I haven't tried any kind of objective test and > of course some Python 'lines' are equal to many > lines of simpler languages - think list comprehensions. > But in practice I still don't think you will have a > big problem. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Truth in advertising: I just realized a Core I7 only benchmarks about 10x faster than a Core 2 Duo, using Passmark. Wow, something like 6 years newer and only 10 times? Anyway, I'd STILL expect to see some of that in the program performance, though maybe once I get it ironed out it will be a little sleeker... Keith On Fri, Jan 3, 2014 at 8:38 PM, Keith Winston wrote: > Just to be clear, what I'm asking this typing tutor to do is vastly more > than normal, albeit still not seemingly very much. In most programs, they > give you a sentence or paragraph to type, and then time how long it takes. > I'm talking about timing every keypress, and modifying the text stream > based on that. The thing that put me on edge was noticing that my simple > Chutes & Ladders game doesn't go ANY faster on a machine that benchmarks > perhaps 1000 times faster than another... > > Keith > > > On Fri, Jan 3, 2014 at 8:17 PM, Alan Gauld wrote: > >> On 03/01/14 21:53, Keith Winston wrote: >> >> Ladders!). It is a typing tutor, I am inclined to use it to learn Dvorak >>> but I would expect it easily adapted to QWERTY or anything else. >>> ... >>> >>> >>> My concern is with speed. This will have to keep up with (somewhat >>> arbitrarily) fast typing, >>> >> >> Lets see. The speed record for touch typing is around 150 wpm with >> average word being about 5 chars, so a speed of about 750 cpm >> or 12.5cps That's about 80ms between letters. >> >> Python on a modern PC can probably execute around 100k lines >> of code(*) per second or 100 per millisecond. That's 8k lines >> executed between each keypress for the worlds fastest typist. >> >> I used to use a typing tutor that was written in old GW Basic >> on the original IBM PC (speed 4.7MHz) and it had no problem >> analyzing my stats (albeit at a modest 40-50 wpm). >> >> I'd worry about speed after you find you need to. >> >> (*)Caveat: I haven't tried any kind of objective test and >> of course some Python 'lines' are equal to many >> lines of simpler languages - think list comprehensions. >> But in practice I still don't think you will have a >> big problem. >> >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> http://www.flickr.com/photos/alangauldphotos >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Keith > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] simple arg problem
I'm trying to rewrite/reshuffle my Chutes & Ladders code, which I generally find more confusing than writing anew. Anyway, I've got a method that seems like it calls for one argument but... def populate(self, gamecount1): """populate candl_array with a set of games""" (method of a new class CandL_Array, which contains a list called candl_array) Every time I run it with -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple arg problem
gmail is driving me crazy. Anyway, every time I run it with: if __name__ == "__main__": tarray = CandL_Array tarray.populate(100) I get an error Traceback (most recent call last): File "/home/keithwins/Dropbox/Python/CandL8.py", line 127, in tarray.populate(100) TypeError: populate() missing 1 required positional argument: 'gamecount1' >>> Which seems to say it wants another argument, but I am in the habit of not counting the self argument... I thought I understood that. I'm sure it's something obvious, but I've been staring at it for hours. This is going to be embarrassing... On Fri, Jan 3, 2014 at 9:53 PM, Keith Winston wrote: > I'm trying to rewrite/reshuffle my Chutes & Ladders code, which I > generally find more confusing than writing anew. > > Anyway, I've got a method that seems like it calls for one argument but... > > def populate(self, gamecount1): > """populate candl_array with a set of games""" > > (method of a new class CandL_Array, which contains a list called > candl_array) > > Every time I run it with > > > -- > Keith > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple arg problem
Ah, more briefly: parens. Wow, okay then. Thanks. On Fri, Jan 3, 2014 at 10:14 PM, eryksun wrote: > On Fri, Jan 3, 2014 at 9:56 PM, Keith Winston wrote: > > > > if __name__ == "__main__": > > tarray = CandL_Array > > tarray.populate(100) > > > > I get an error > > > > Traceback (most recent call last): > > File "/home/keithwins/Dropbox/Python/CandL8.py", line 127, in > > tarray.populate(100) > > TypeError: populate() missing 1 required positional argument: > 'gamecount1' > >>>> > > > > Which seems to say it wants another argument, but I am in the habit of > not > > counting the self argument... I thought I understood that. I'm sure it's > > something obvious, but I've been staring at it for hours. This is going > to > > be embarrassing... > > You've assigned the class to `tarray` instead of assigning an instance > of the class. Forgetting to call() is a common mistake. > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
On Fri, Jan 3, 2014 at 11:14 PM, Mark Lawrence wrote: > On 03/01/2014 21:41, Keith Winston wrote: > >> -- >> Keith >> >> > Frankly I think you're lining up to jump fences when you're actually > riding on the flat :) > Fair enough, but I am thinking of the next project as a long-term dabbling: hopefully my abilities will rise to my concept... I guess I'm about 3 weeks into Python (and OOP) so far, of course my ability to focus on it will depend on work and other pressures, but I'm amazed at how accessible this language is. Anyway, thanks to you and everyone who've helped me as I crawl on the flat... this group has been way more than invaluable. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] More or less final Chutes & Ladders
Here is what I think will be about the final version of C and L. I rearranged it quite a bit (into 2 classes), fixed a bug or two, and generally cleaned it up a bit. I haven't really polished it, but hopefully it will be less difficult to read... which is to say, if anyone wants to go through it AGAIN (at your leisure) I would appreciate comments on style, etc. Probably the biggest change is that I added a result() method to the ChutesAndLadders class, that returns a list of the pertinent attributes of each game (to be compiled into the array of games results that CandL_Array class is all about). Anyway, I sort of need to get off this since I need to do a lot more reading on this language, though I may be back with early drafts of my typing tutor before long... we'll see how that goes. -- Keith """ Chutes & Ladders Simulation Simulates a number of games of Chutes & Ladders (Snakes & Ladders). Chutes & Ladders are separate dictionaries to allow tracking of separate stats. Gathers the results into a list of lists of individual game results in the form (per game) of [game_no, moves, [chutes], [ladders]] """ import random from timer2 import timer from statistics import * # from whence comes mean, variance, stdev # Landing on a chute causes one to slide down to the corresponding value. chutes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98:78} # Landing on a ladder (key) causes one to climb up to the corresponding value. ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80:100} class ChutesAndLadders: """Game class for Chutes & Ladders.""" def __init__(self): self.reset() def reset(self): self.position = 0 self.game_number = 0 self.move_count = 0 self.chutes_list = [] self.ladders_list = [] def results(self): return [self.game_number, self.move_count, self.chutes_list, self.ladders_list] #@timer def move(self): """Single move, with chutes, ladders, & out of bounds. Primary action is to move self.position, but returns a list that consists of either the chute or ladder if landed on, if either """ roll = random.randint(1,6) self.move_count += 1 self.position += roll if self.position in chutes: self.chutes_list.append(self.position) self.position = chutes[self.position] elif self.position in ladders: self.ladders_list.append(self.position) self.position = ladders[self.position] elif self.position > 100: # move doesn't count, have to land exactly self.position -= roll return #@timer def play(self, game_no): """Single game""" self.reset() self.game_number = game_no while self.position < 100: self.move() return class CandL_Array: """ Create & analyze an array of CandL games """ candl_array = [] def __init__(self): self.candl_array = [] #@timer def populate(self, gamecount1): """populate candl_array with a set of games""" tgame = ChutesAndLadders() for i in range(gamecount1): tgame.play(i) self.candl_array.append(tgame.results()) def print_stuff(self): self.print_gameset_stats(self.candl_array) self.print_candl_info(self.candl_array) def print_gameset_stats(self, garray): print("Total number of games: ", len(garray)) print(" MovesChutesLadders") for func in [mean, max, min, stdev]: print("{moves:9.2f} {chutes:12.2f} {ladders:13.2f} {fname}".format( moves=func(tgset[1] for tgset in garray), chutes=func(len(tgset[2]) for tgset in garray), ladders=func(len(tgset[3]) for tgset in garray), fname=func.__name__ )) def print_candl_info(self, garray): """ Create two dictionaries with the same keys as chutes & ladders, but with the total number of times each c or l is traversed (in the entire gameset) as the values. Then, print them. """ self.chute_nums, self.ladder_nums = dict(chutes), dict(ladders) self.summarize_game("chutes", self.chute_nums, 2, garray) self.summarize_game("ladders", self.ladder_nums, 3, garray) def summarize_game(self, game_name, game_nums, game_index, garray): tgame_nums = game_nums for i in tgame_nums.keys(): game_nums[i] = 0 for corl in tgame_nums: for game in garray: tgame_nums[corl] += game[game_index].count(corl) print("total ", game_name, "= ", sum(tgame_nums.values())) """ def timing_report(): print("game.total, count = ", p1.game.total, p1.game.count) print("move.total, count = ", p1.move.total, p1.move.count) """ def run_CandL(gamecount): tarray = CandL_Array
Re: [Tutor] simple arg problem
Hi Steven, tarray = CandL_Array(100) Yes, that's definitely better. I'll make that change. Thanks, makes sense. I'd already posted my "finished" version, so I probably won't repost with this small change right now. Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
On Fri, Jan 3, 2014 at 11:59 PM, Steven D'Aprano wrote: > thelist = vars()[name] I see: vars() certainly looks less dangerous than eval(), but I'm guessing that's still smelly code? I hadn't known about vars() or I probably would have used it. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copying [was Re: What's in a name?]
Thanks for all this. I ended up using newdict = dict(olddict), which seemed to work fine. I hadn't heard about the copy module until now. I had heard about deep/shallow copies, though in this particular example (all int dicts), I don't think there's a difference...? On Sat, Jan 4, 2014 at 12:50 AM, Mark Lawrence wrote: > On 04/01/2014 05:44, Steven D'Aprano wrote: > >> On Fri, Jan 03, 2014 at 01:53:42PM -0500, Keith Winston wrote: >> >> That's what I meant to do: make a copy when I wrote chute_nums = chutes. >>> So >>> I should have passed chute_nums to summarize_game, but it still wouldn't >>> work (because it's not a copy). >>> >> >> Python never makes a copy of objects when you pass them to a function or >> assign them to a name. If you want a copy, you have to copy them >> yourself: >> >> import copy >> >> acopy = copy.copy(something) >> >> >> ought to work for just about anything. (Python reserves the right to not >> actually make a copy in cases where it actually doesn't matter.) >> >> There are a couple of shortcuts for this: >> >> # copy a dictionary >> new = old.copy() >> >> # copy a list, or tuple >> new = old[:] # make a slice from the start to the end >> >> >> > Please be aware of the difference between deep and shallow copies see > http://docs.python.org/3/library/copy.html > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's in a name?
Thanks to both of you. In this particular case, the main use of eval() was only for 2 calls, which were essentially hard-coded (you could see the calls to summarize_game in my code). I was looking for a more general solution to what I was trying to do, but I don't need it for this project. Still, this has been an informative conversation. On Sat, Jan 4, 2014 at 12:56 AM, Steven D'Aprano wrote: > On Sat, Jan 04, 2014 at 12:32:19AM -0500, Keith Winston wrote: > > On Fri, Jan 3, 2014 at 11:59 PM, Steven D'Aprano >wrote: > > > > > thelist = vars()[name] > > > > > > I see: vars() certainly looks less dangerous than eval(), but I'm > guessing > > that's still smelly code? I hadn't known about vars() or I probably would > > have used it. > > Yes, it's still a bit smelly: but only a bit, since while "direct" code > is the idea, sometimes the only way to do things is with one (or two) > layers of indirection. > > Code should (as a general rule) not rely on, or be affected by, the name > of the variable. Functions which inspect the running environment (i.e. > peek deep inside the interpreter) or use eval or other techniques to > operate "behind the scenes" on *names* rather than values can often lead > to confusing code. As debugging tools, they're useful. Putting such > functionality inside normal everyday programs is a code smell. > > > -- > Steven > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python problems on android
Perhaps you could include the script? On Sat, Jan 4, 2014 at 5:47 AM, wrote: > > > Ok. Will try and explain the problem. > I wrote a script in python and found I could use it on my android phone > with SL4a. > It was really useful. > Haven't used it for a few months. > A few days ago I decided to improve it and found it no longer works. > the problem seems to be that when it reaches an input statement it will > only accept a number. > If I try to input a letter I get an error message the same as if I had > just typed the letter into the command line. > I am not sure what has happened, it used to work ok. > > Is there another way of getting input into my script without > using.input? > > Or better still does anyone understand what is going wrong? > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More or less final Chutes & Ladders
Thanks Alan & Denis: Alan, the improvement you suggested had already been made, and adopted. Good catch. Denis: alas, there are chutes and ladders dicts, but I guess your chutes & ladders lists are local to the results class... Your suggestion is quite shocking to me, I wouldn't have thought of creating a class for results... I guess it allows clearer modularity of the final candl_array? I don't really get it... I think you are using it, essentially, for nothing other than a data structure, right? And some cleaner print options, though I have only looked at the raw game data for debugging... it's a great suggestion, obviously, because I am a little dumbfounded by it, and it's making me think. My initial reaction is concern about having too many classes, but I don't really have any sense of how important that is. I was playing with storing ChutesAndLadders instances in my game array, but of course they include all their methods, etc: all kinds of overhead (at least, that's my impression), which is why I created the results method, so I could just pass a... list? composite object? Collection? I can't really sort out what the latter two mean in Python, and must be looking in the wrong place... while I was researching I found out about namedtuple(), which seems like a promising structure for game data, but I haven't really looked into it. It might also be a collection... The entire game is to be played in bulk (that is, it's really a statistical foray, albeit a silly one), so the candl_array might get large (perhaps millions of "records" -- results lists). Is there some way the Results class helps that? keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python problems on android
Well, I probably can't help you, I haven't installed SL4 (yet), and am a Python beginner myself anyway. I imagine others might be more prepared to help you with a copy of the script. However: something about the way you are responding to this thread keeps breaking it, so you end up starting a new thread (you've started three). That's a little hard to keep track of, and there are those here who truly hate that kind of thing. This message, for example, started a new thread (at least for me, I assume others), but your last reply didn't, so if you know what you did (or didn't do) that time, do that from now on! My only thought on the problem is: what is the script doing with the input? Maybe for some reason a number works in that context, but a letter doesn't? Even just including a few lines of the code around the input statement might be better than nothing. keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More or less final Chutes & Ladders
Thanks again Denis, I might just have to ruminate on this, but I am definitely having an allergic reaction. I understand that Python doesn't have composite objects, but neither does it dislallow my list of lists of ints and lists... which is, I imagine, very space efficient. I think what you are in essence saying is that it's a mistake for me to worry about space at the expense of clarity... ok, but if I really don't need to break out those single lists ever, to speak of... it seems like I've vastly enlarged my array for little gain. I'm not meaning to argue, but to understand. Especially in lieu of an upcoming project with, perhaps, larger and more complex structures. I am increasingly curious about whether namedtuples are a good strategy for some of this: they store their field names, as I understand it, and I can live with an immutable type in all these cases: I wonder if they are as efficient in named-field (key) lookup as dictionaries? I'm also a bit confused here: obviously tuples are immutable, but one can use lists in them... I think that makes those lists' contents immutable? And could one define a namedtuple that included lists that were different lengths for different instantiations (like my game results, for example)? I really should be playing with them instead of asking questions, at this point... Thanks as always! Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python, speed, game programming
Hi Danny, no, I don't think there's any disk access, and the memory of the two machines is rather different: one is 4 Gb or so, the other 9 changing to 12 any day... but I think I haven't been rigorous enough to justify a great deal more attention here. I am convinced that I should just keep developing my next project, and my programming skills, and worry about speed issues as I hit them. I was overreaching, or anticipating or something... On Sat, Jan 4, 2014 at 10:30 PM, Danny Yoo wrote: > There's an assumption in the question here that all programs are CPU bound. > > I actually do not think so. From prior discussion about what the > program is doing, I got the impression that it was trying to hold > gigabytes of data in RAM. Isn't that still true? If so, then I would > be very surprised if the program were not thrashing virtual memory. > Under such conditions, give up on any assumptions about program speed > being related to CPU speed. It's hitting disk hard, and that's a Game > Over. Under heavy virtual memory swapping conditions, it doesn't > matter how fast your CPU is: the time that your program is taking is > due to the physical act of moving spindles and spinning disks of metal > around. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More or less final Chutes & Ladders
Thanks all, interesting. I'll play more with tuples, I haven't knowingly used them at all... Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More or less final Chutes & Ladders
On Sun, Jan 5, 2014 at 2:52 AM, Mark Lawrence wrote: > Homework for you :) Write a line of code that creates a list of say 3 or > 4 integers, then write a line that creates a tuple with the same integers. > Use the dis module to compare the byte code that the two lines of code > produce. The difference is interesting. Well... that was a very interesting assignment, though I'm going to have to chew on it for a while to understand. I can see that the processing code for a tuple is considerably shorter... it is processed in a gulp instead of bite by byte... it doesn't have the "Build List" step at all (what goes on inside of THAT?)... but I can't claim to really understand what I'm looking at. I notice, for example, if I include only constants (immutable types) in my tuple, then it does that gulp thing. If I include a list in there too, all hell breaks loose, and suddenly I'm Building Tuples (what goes on inside of THAT?). A tuple of tuples still goes down in a single swallow, of course. Sadly, you can see how my mind works here... hey, this was FUN! You can assign me homework any time, teach! -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
Hi Amrita, I'm just a beginner but I notice that, after the first two entries on each line (i.e. 10,ALA), the rest might fit nicely into a dict, like this {H: 8.388, HB1: 1.389, ...}. That would give you a lot of flexibility in getting at the values later. It would be easy enough to replace the "=" with ':", and add some curly braces. In fact, if you enclosed each line in square braces, changed =/: and added the curly braces on the dict, then each line would already be a list containing a dictionary, and you'd be ready to do some rearranging very easily. Whether you do that in Python or when you are building your file in whatever system it's coming from is your call. If you left off the outer braces, each line is a tuple containing a dict, which works too. Sorry if all I've done is state the obvious here. I warned you I'm a beginner. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
I should have included an example. If you can, and if it doesn't make your file too long, and if I'm right that this is easy to do in the output module of wherever this is coming from, add some white space so you can read/debug easier, though it's not at all necessary (strings have to be quoted also: see below): [2, ALA, {C: 178.255, CA: 53.263, CB: 18.411,,}] This is ready to be accessed: If I do this at the command prompt: shift = [2, "ALA", {"C": 178.255, "CA": 53.263, "CB": 18.411}] Then: shift[0] returns 2 shift[2]['CA'] returns 53.263 And if you had a bunch of these in a list shift_list[] (like your original data set), you could do things like for i in shift_list: for atom in shift_list[i][2]: return shift_list[i][2][atom] I didn't check this code, it's pretty much guaranteed to be wrong but I think it might point in the right direction. I think I made this a little more complicated than it needs to be, but I have to run right now. Maybe this is helpful. Good luck! Actually, I think shift needs to be a class... but that's just my nascent OOP comment. Keith On Sun, Jan 5, 2014 at 3:01 PM, Keith Winston wrote: > Hi Amrita, I'm just a beginner but I notice that, after the first two > entries on each line (i.e. 10,ALA), the rest might fit nicely into a dict, > like this {H: 8.388, HB1: 1.389, ...}. That would give you a lot of > flexibility in getting at the values later. It would be easy enough to > replace the "=" with ':", and add some curly braces. In fact, if you > enclosed each line in square braces, changed =/: and added the curly braces > on the dict, then each line would already be a list containing a > dictionary, and you'd be ready to do some rearranging very easily. > > Whether you do that in Python or when you are building your file in > whatever system it's coming from is your call. If you left off the outer > braces, each line is a tuple containing a dict, which works too. > > Sorry if all I've done is state the obvious here. I warned you I'm a > beginner. > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
Amrita, on a closer read of your very first post I (think I) see you already successfully read your data into a series of dicts (mylist in your example), so if you still want the output you posted in the first post, then you can do some version of the loops that I described. That said, I'm sure Stephen is right about the csv module having helpful tools for parsing that original file, if you're not past that point. Anyway, if you had all your mylist in a list of lists (NOT a dict, as you have them) then getting the output you wanted seems easy. HOWEVER: I don't think you want to strip off the first two numbers, since I think the first one is critical to your final listing, no? I might be beating a tired horse here, but you could get your desired output from such a list of lists like this (I modified the names, and added back in that first number to the output) for i in mylist_list: for atom in mylist_list[i][2]: print(mylist_list[i][0], atom, " = ", mylist_list[i][2][atom]) # output something like "2 C = 178.255" This is pretty ugly, but you get the idea. As I mentioned before, putting it all into a class would allow you to do this more robustly, but I'll probably lead you astray if I expand on that. Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
Hi Amrita: I tried to figure out, for kicks, how to do what I THINK is what you're trying to do... I've never even opened a .txt file in Python before, so you can take all this with a big grain of salt... Anyway, if you take your example of your original database: 1 GLY HA2=3.7850 HA3=3.9130 2 SER H=8.8500 HA=4.3370 N=115.7570 3 LYS H=8.7530 HA=4.0340 HB2=1.8080 N=123.2380 4 LYS H=7.9100 HA=3.8620 HB2=1.7440 HG2=1.4410 N=117.9810 5 LYS H=7.4450 HA=4.0770 HB2=1.7650 HG2=1.4130 N=115.4790 6 LEU H=7.6870 HA=4.2100 HB2=1.3860 HB3=1.6050 HG=1.5130 HD11=0.7690 HD12=0.7690 HD13=0.7690 N=117.3260 7 PHE H=7.8190 HA=4.5540 HB2=3.1360 N=117.0800 8 PRO HD2=3.7450 9 GLN H=8.2350 HA=4.0120 HB2=2.1370 N=116.3660 10 ILE H=7.9790 HA=3.6970 HB=1.8800 HG21=0.8470 HG22=0.8470 HG23=0.8470 HG12=1.6010 HG13=2.1670 N=119.0300 11 ASN H=7.9470 HA=4.3690 HB3=2.5140 N=117.8620 12 PHE H=8.1910 HA=4.1920 HB2=3.1560 N=121.2640 13 LEU H=8.1330 HA=3.8170 HB3=1.7880 HG=1.5810 HD11=0.8620 HD12=0.8620 HD13=0.8620 N=119.1360 I put it in a file ashift.txt. Then: f = open('ashift.txt', 'r') lines = f.readlines() Now I could iterate through lines (it is a list of strings, one per line), but I just shortcut to play with a single line: tshift = lines[0] tshift = tshift.replace("=", ":") tshift = tshift.splitlines() # remove the final \n tshift = tshift.split(" ") At which point we have something like this: ['1', 'GLY', 'HA2:3.7850', 'HA3:3.9130'] I am out of time, plus I'm very conscious of doing this INCREDIBLY ineptly. I have spent a bit of time trying to sort out the right way, there might be some approach involving dialects associated with the csv module, but I couldn't sort that out. If one could massage the above line (or the original file) into [1, 'GLY', {'HA2' : 3.7850, 'HA3' : 3.9130}] This is what I'd talked about before, and would make reaching your final output pretty easy, following the stuff I said above. I KNOW there's a much, much easier way to do this, probably a one-liner (at least for the file parsing). You talked about printing this stuff out, but if you are going to process it further (analyzing it in some way, for example) there might be implications as to how you proceed with this. Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
oops, I see Steven pointed out a much cleaner approach. Oh well. Shock & surprise ;) Keith On Mon, Jan 6, 2014 at 3:27 AM, Keith Winston wrote: > Hi Amrita: I tried to figure out, for kicks, how to do what I THINK is > what you're trying to do... I've never even opened a .txt file in Python > before, so you can take all this with a big grain of salt... Anyway, if you > take your example of your original database: > > 1 GLY HA2=3.7850 HA3=3.9130 > 2 SER H=8.8500 HA=4.3370 N=115.7570 > 3 LYS H=8.7530 HA=4.0340 HB2=1.8080 N=123.2380 > 4 LYS H=7.9100 HA=3.8620 HB2=1.7440 HG2=1.4410 N=117.9810 > 5 LYS H=7.4450 HA=4.0770 HB2=1.7650 HG2=1.4130 N=115.4790 > 6 LEU H=7.6870 HA=4.2100 HB2=1.3860 HB3=1.6050 HG=1.5130 HD11=0.7690 > HD12=0.7690 HD13=0.7690 N=117.3260 > 7 PHE H=7.8190 HA=4.5540 HB2=3.1360 N=117.0800 > 8 PRO HD2=3.7450 > 9 GLN H=8.2350 HA=4.0120 HB2=2.1370 N=116.3660 > 10 ILE H=7.9790 HA=3.6970 HB=1.8800 HG21=0.8470 HG22=0.8470 HG23=0.8470 > HG12=1.6010 HG13=2.1670 N=119.0300 > 11 ASN H=7.9470 HA=4.3690 HB3=2.5140 N=117.8620 > 12 PHE H=8.1910 HA=4.1920 HB2=3.1560 N=121.2640 > 13 LEU H=8.1330 HA=3.8170 HB3=1.7880 HG=1.5810 HD11=0.8620 HD12=0.8620 > HD13=0.8620 N=119.1360 > > I put it in a file ashift.txt. Then: > > f = open('ashift.txt', 'r') > lines = f.readlines() > > Now I could iterate through lines (it is a list of strings, one per line), > but I just shortcut to play with a single line: > > tshift = lines[0] > tshift = tshift.replace("=", ":") > tshift = tshift.splitlines() # remove the final \n > tshift = tshift.split(" ") > > At which point we have something like this: > > ['1', 'GLY', 'HA2:3.7850', 'HA3:3.9130'] > > I am out of time, plus I'm very conscious of doing this INCREDIBLY > ineptly. I have spent a bit of time trying to sort out the right way, there > might be some approach involving dialects associated with the csv module, > but I couldn't sort that out. If one could massage the above line (or the > original file) into > > [1, 'GLY', {'HA2' : 3.7850, 'HA3' : 3.9130}] > > This is what I'd talked about before, and would make reaching your final > output pretty easy, following the stuff I said above. > > I KNOW there's a much, much easier way to do this, probably a one-liner > (at least for the file parsing). > > You talked about printing this stuff out, but if you are going to process > it further (analyzing it in some way, for example) there might be > implications as to how you proceed with this. > > Keith > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: arrangement of datafile
Amrita, it doesn't seem like the code you are providing is the code you are running. I wonder if you are running it all at the Python command line or something, and have to type it in every time? You should put it in a file, and save & run that file, and then cut and paste it directly into your emails, so we can see what you're actually running. There are a number of small things in the code you've posted that wouldn't run, I think... Keith On Mon, Jan 6, 2014 at 3:57 AM, Amrita Kumari wrote: > Hi Steven, > > I tried this code: > > import csv > with open('file.csv') as f: > reader = csv.reader(f) > for row in reader: > print(row) > row[0] = int(row[0]) > > up to this extent it is ok; it is ok it is giving the output as: > > ['1' , ' GLY' , 'HA2=3.7850' , 'HA3=3.9130' , ' ' , ' ' , ' ' , ' '] > [ '2' , 'SER' , 'H=8.8500' , 'HA=4.3370' , 'N=115.7570' , ' ' , ' ' , > ' '] > -- > --- > but the command : > > key, value = row[2].split('=', 1) > value = float(value.strip()) > print(value) > > is giving the value of row[2] element as > > ['1' , ' GLY' , 'HA2=3.7850' , 'HA3=3.9130' , ' ' , ' ' , ' ' , ' '] > 3.7850 > [ '2' , 'SER' , 'H=8.8500' , 'HA=4.3370' , 'N=115.7570' , ' ' , ' ' , > ' '] > 8.8500 > > -- > so this is not what I want I want to print all the chemical shift value of > similar atom from each row at one time > > like this: > > 1 HA2=3.7850 > 2 HA2=nil > 3 HA2=nil > . > > .. > 13 HA2=nil > > similarly, for atom HA3: > > 1 HA3=3.9130 > 2 HA3=nil > 3 HA3=nil > ... > > > 13 HA3=nil and so on. > > so how to split each item into a key and a numeric value and then search > for similar atom and print its chemical shift value at one time along with > residue no.. > > Thanks, > Amrita > > > > > > On Mon, Jan 6, 2014 at 6:44 AM, Steven D'Aprano wrote: > >> Hi Amrita, >> >> On Sun, Jan 05, 2014 at 10:01:16AM +0800, Amrita Kumari wrote: >> >> > I have saved my data in csv format now it is looking like this: >> >> If you have a file in CSV format, you should use the csv module to read >> the file. >> >> http://docs.python.org/3/library/csv.html >> >> If you're still using Python 2.x, you can read this instead: >> >> http://docs.python.org/2/library/csv.html >> >> >> I think that something like this should work for you: >> >> import csv >> with open('/path/to/your/file.csv') as f: >> reader = csv.reader(f) >> for row in reader: >> print(row) >> >> Of course, you can process the rows, not just print them. Each row will >> be a list of strings. For example, you show the first row as this: >> >> > 2,ALA,C=178.255,CA=53.263,CB=18.411,, >> >> so the above code should print this for the first row: >> >> ['2', 'ALA', 'C=178.255', 'CA=53.263', 'CB=18.411', '', '', '', >> '', '', '', '', '', ''] >> >> >> You can process each field as needed. For example, to convert the >> first field from a string to an int: >> >> row[0] = int(row[0]) >> >> To split the third item 'C=178.255' into a key ('C') and a numeric >> value: >> >> key, value = row[2].split('=', 1) >> value = float(value.strip()) >> >> >> >> Now you know how to read CSV files. What do you want to do with the data >> in the file? >> >> >> >> -- >> Steven >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
I had heard of Project Euler long ago, but completely forgotten it. It looks fun!! Thanks for reminding me of it. Keith On Tue, Jan 7, 2014 at 5:58 AM, eryksun wrote: > On Tue, Jan 7, 2014 at 4:49 AM, Jorge L. wrote: > > > > When i test that script against 600851475143 I get the following error > > You're trying to create a list with over 600 billion items. > sys.maxsize is a bit over 2 billion for 32-bit CPython, but switching > to 64-bit won't help unless you have a few terabytes of memory (8 > bytes per pointer). You need to rethink your approach to generating > primes. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
I think your approach is fine, you might need to fine tune your algorithm. hint below. if you want it: is_p doesn't need to be nearly as big as you specify. There are a couple other minor problems. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
um, I used his code, slightly fine-tuned it, and got the solution in .35 seconds (running time, not fine-tuning time ;). On my dinky old notebook. So I'm inclined to believe it is possible... Though perhaps my sense of what "fine-tuning" entails doesn't mesh with yours... spoiler alert, more below. Note that his code doesn't really (need to) create a sieve of Eratosthenes all the way to n, but only to sqrt(n). It then tests for divisibility. Though it is in this that some of the problems lie with the original code. I'm happy to share my revision with anyone that wants it. It is pretty rough, consistent with my Python (and math!) skills... it's really quite close to his code. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
In fact, I just used it to solve a number 3 orders of magnitude larger than 600851475143, the number from prob 3. It took 12s. I hardly dare go further than that... I'm not arguing that there's a big list being built in there... Oops, I dared. 5 orders of magnitude bigger crashes my machine. Oops oops, it finished. It says it took 161 seconds, but in fact it sort of half-crashed my machine for the last 30 minutes... -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
Hey Danny, I think you could use the same sqrt(n) on your algorithm to reduce the search space. I think you could also increment n += 2 to skip even numbers, the simplest of sieves. I think the sieve concept is based on the idea that adding is much less intensive than dividing, so creating the sieve is fairly quick compared to all those divides. that said, I might put a timer on your algorithm and compare them! Oops: I just did it. Yours won, .23s to .34s. What's more, on certain prime numbers (with large factors), mine breaks. well never mind then. I'm blaming Jorge. Dammit: I forgot to include the i += 2 suggestion I made in the above test (one also has to start i at 3, hopefully obviously). That improves your time to .11s. Poor Jorge. Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
On Tue, Jan 7, 2014 at 5:45 PM, Keith Winston wrote: > Hey Danny, > > I think you could use the same sqrt(n) on your algorithm to reduce the > search space. I think you could also increment n += 2 to skip even numbers, > the simplest of sieves. > > I think the sieve concept is based on the idea that adding is much less > intensive than dividing, so creating the sieve is fairly quick compared to > all those divides. that said, I might put a timer on your algorithm and > compare them! > > Oops: I just did it. Yours won, .23s to .34s. What's more, on certain > prime numbers (with large factors), mine breaks. well never mind then. I'm > blaming Jorge. > > Dammit: I forgot to include the i += 2 suggestion I made in the above test > (one also has to start i at 3, hopefully obviously). That improves your > time to .11s. Poor Jorge. > > Keith > > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
Sorry for the blank message. I was just going to say that I fixed my version of Eratosthenes' algorithm, but Danny's is still faster in all cases. Also, Jorge: I hope it's obvious that I'm kidding around, I wouldn't want you to feel uncomfortable with asking your questions just because I'm being goofy. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
oops, I'm sorry I didn't realize Danny's message to me was private, but you're right that's better than publishing this right out. Also: although the sqrt(n) algorithm worked to solve the problem, it isn't actually correct. There are cases in which it will fail, many in fact. Oops. One would need to take the Sieve all the way up to n/m for it to work in this simple version, where m is the lowest prime factor of the number (which this algorithm WILL always find). There's a nice recursive version in there somewhere. Keith On Tue, Jan 7, 2014 at 7:17 PM, Alan Gauld wrote: > On 07/01/14 21:22, Keith Winston wrote: > > Note that his code doesn't really (need to) create a sieve of >> Eratosthenes all the way to n, but only to sqrt(n). It then tests for >> divisibility. >> > > I'm not sure what you mean here. > His algorithm was using an array of booleans to indicate whether a number > was prime. How does building a list up to sqrt(n) identify primes above > sqrt(n)? > > For example if n is 100 how do you detect that 97 is prime > if you only build a sieve up to 10? > > I know you can find the highest prime factor without finding all the > primes themselves but that's exactly the kind of alternative algorithm > Euler is looking for. But then it's no longer using Jorge's algorithm, > which is to test all numbers for primeness then find the highest one that > is a factor of n, is it? > > Ah, OK a light bulb just went on. Amazing what happens when you start > typing your thoughts! What you mean is to build a seive to identify all > primes up to sqrt(n) and then test those primes as factors. I had thought > you intended testing *all* numbers up to sqrt(n) then seeing if they were > prime. Quite different... > > As you say, it's maybe a matter of defining 'tuning' versus creating > an entirely new algorithm! ;-) > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
Hey Steven, That's a cool primes module, I'm going to look that over more carefully. I can see that you've thought through this stuff before ;) And yeah, I'd like to see your Stopwatch code... I haven't looked at "with" yet, that's interesting. As usual, I don't totally get it... Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
I'm also happy to note that the sieve I adapted from Jorge generates a list of primes the same length as yours, which would seem to imply it's correct, and in 33s, though since it's timed by a different mechanism that may not mean anything... On Tue, Jan 7, 2014 at 10:36 PM, Keith Winston wrote: > Hey Steven, > > That's a cool primes module, I'm going to look that over more carefully. I > can see that you've thought through this stuff before ;) > > And yeah, I'd like to see your Stopwatch code... I haven't looked at > "with" yet, that's interesting. As usual, I don't totally get it... > > Keith > -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] garbage collecting
Iirc, Python periodically cleans memory of bits & pieces that are no longer being used. I periodically do something stupid -- I mean experimental -- and end up with a semi-locked up system. Sometimes it comes back, sometimes everything after that point runs very slowly, etc. I just saw where I could do os.system('python'), but in restarting the interpreter I'd lose everything currently loaded: my real question involves merely pushing the garbage collector into action, I think. -- Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] garbage collecting
well, fair enough. Generally, the issue involves instances when Python will come back, but it might take several minutes or much longer. And weird behaviour ensues: like timers I put on the program don't actually time the amount of time it's busy (by a very, very large margin). Also, often several minutes after such a thing (sometimes quite a bit later), things will suddenly start working quickly again. Also, on my laptop I can actually tell when it's struggling, b/c the fan turns on and/or speeds up, and in many of these cases it will go into all-out mode, even though I'm not doing anything. But your point about having no lever with which to move the world is a good one. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OverflowError: cannot fit 'long' into an index-sized integer
Yes, I did read the context manager stuff, but I can only absorb a bit of it, I just don't have the background. It's getting better though. Plus I think most things will come only after I play with them, and I haven't really played much with wrappers, decorators and the like. Which is fine, the year is young. I've just knocked off the first 11 Euler project problems, which forced me to do a bunch of things for the first time (mostly quite uglily). Keith ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] garbage collecting
Well, thanks everyone. I get the picture. And there's nothing subtle going on here: I'm playing around, trying to factor million-digit numbers and the like. No biggie, this was interesting. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor