Re: [Tutor] PYTHONPATH-corrected
"Steve Willoughby" wrote in directories for chapers. Now I am on modules, and some are reused in later chapters. I have set up a directory for the modules. How do I add it to my PYTHONPATH? So, for example, in a bash/sh shell, you'd say: export PYTHONPATH="/path/to/my/modules" And you can add multiple directories by separating them with colons: export PYTHONPATH="/path/to/my/modules:/path/to/more/modules:/path/ytoyet/more" HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
2009/7/1 Daniel Sato : > I have been going through some Python Programming exercises while following > the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with > the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password three > times, but can't get it to quit right away after the correct one is > entered. I know this is really basic, please forgive me. I have no > programming experience and have just started going through these tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > > -- > Daniel Sato > http://www.danielsato.com > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > > Hello daniel, some comments password = "qwerty" guess = "0" # <- no need to initialise that variable or # if you would need, using just "" would be enough #instead of "0" count = 0 while count != 3: # <- that works here, but i usually use "< 3" guess = raw_input("Enter your password: ") guess = str(guess) # no need to typecast here because raw_input alredy returns a str #when you don't know the type use type(yourvar) in python if guess != password # missing ":" print "Access Denied" count = count + 1 # <- has to be indented to the same level with the above line # also you can use count += 1 instead else: print "Password Confirmed" # to exit you can use the exit() function ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
On 6/30/2009 7:53 PM Daniel Sato said... I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e First, paste your code into a python command window. You'll get errors, but clean them up. Second, code blocks that are indented alike execute as part of the same block. Take a look where you increment count. HTH, Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] When are strings interned?
Hello, world! This is my first post to the Tutor list (although I've already posted to comp.lang.python a couple of times). I'm currently reading Chapter 4 of Wesley Chun's book, "Core Python Programming" (2nd ed.). I find this, in Python 2.5.4, on my Win98SE system (using IDLE): >>> n = "colourless" >>> o = "colourless" >>> n == o True >>> n is o True >>> p = "green ideas" >>> q = "green ideas" >>> p == q True >>> p is q False Why the difference? -- Angus Rodgers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
Daniel Sato wrote: am having some trouble with the first "If" Don't forget the colon at the end of the line. if condition: pass Greetings, -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculating a sort key and operator.attrgetter()
On Tue, Jun 30, 2009 at 11:39 PM, Vincent Davis wrote: > I have a class with an attribute which is a list "rank_list" this is a list > of instances f another class that has attributes "quality, is_observed" > if I want to sort the list by the attribute "quality" I can just use, > self.rank_list.sort(key=operator.attrgetter('quality')) > But I want to sort like this. > self.rank_list.sort(key=(operator.attrgetter('quality') * > operator.attrgetter('is_observed') * self.does_observe)) > Will this work or is there a better way? That won't work because attrgetter() returns a function and you can't multiply functions. What you can do is define your own function that returns the value you want for the key and use that for the sort. I'm leaving out self.does_observe because that won't change for the list items, wil it? def make_key(item): return item.quality * item.is_observed self.rank_list.sort(key=make_key) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: calculating a sort key and operator.attrgetter()
Forgot to reply-all... -- Forwarded message -- From: Rich Lovely Date: 2009/7/1 Subject: Re: [Tutor] calculating a sort key and operator.attrgetter() To: Vincent Davis 2009/7/1 Vincent Davis : > I have a class with an attribute which is a list "rank_list" this is a list > of instances f another class that has attributes "quality, is_observed" > if I want to sort the list by the attribute "quality" I can just use, > self.rank_list.sort(key=operator.attrgetter('quality')) > But I want to sort like this. > self.rank_list.sort(key=(operator.attrgetter('quality') * > operator.attrgetter('is_observed') * self.does_observe)) > Will this work or is there a better way? > > Thanks > Vincent Davis > > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > > The attrgetter returns a callable, so you'll get e syntax error along the lines of "You can't multiply two function objects". As written, it sounds as if self.does_observe will be the same for all classes within the container, therefore it has no effect on the sort results. The only relevant bits are the two attrgetters. The key argument expects a callable that takes one arguement, and returns the value to be compared. Attrgetter returns a function that takes an object as an argument and gets the attribute named in the call to the generator. When you know the name of the attribute you want in advance, you can get the same effect using a lambda: operator.attrgetter("is_observed") --> lambda o: o.is_observed There are two ways to do what you're after: 1. Define a new function (This can be inside the function that does the sorting, or higher if it's needed elsewhere) def keyFunc(o): """returns a sort key for object o""" return o.quality * o.is_observed self.rank_list.sort(key=keyFunc) 2. use a lambda: self.rank_list.sort(key=(lambda o: o.quality * o.is_observed)) If you want a reusable solution, method 1 is better. If you want a simple, easy to read, solution, 2 is probably better. If there is a chance of the wrong sort of class getting into the list, you'll need to use 1 if you want to use error handling, as you can't use try and except in a lambda. -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
On Tue, Jun 30, 2009 at 9:53 PM, Daniel Sato wrote: > I have been able to make the module quit after entering a password three > times, but can't get it to quit right away after the correct one is > entered. I know this is really basic, please forgive me. I have no > programming experience and have just started going through these tutorials Let's take a quick look at your code: 1. password = "qwerty" 2. guess = "0" 3. count = 0 4. while count != 3: 5. guess = raw_input("Enter your password: ") 6. guess = str(guess) 7. if guess != password 8. print "Access Denied" 9. count = count + 1 10. else: 11. print "Password Confirmed" On line 4 you enter a while loop. What criteria stops the loop? As an aside - with while loops it's better to use a < or > (or >= <=) comparison. Because what happens if something goes wrong inside your loop and count becomes 4? Your loop takes a LONG time to finish! Actually in python it probably won't ever finish. Also you should move line 9 either right after line 4 or right after line 11. When you're incrementing a value it's best to do it at the beginning or end of a loop. (and certainly never in between an if and else!) So with this in mind, what could you do once it prints "Password confirmed" to make sure the loop ends? HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] egg
Hi, I have a basic question concerning pythons eggs. I have setup easy install and used it to install mechanize-- this was easy as I only needed to type in 'Easy_Install Mechanize' in terminal. Now I need to install 'simplejson 2.0.9' from this here: http://pypi.python.org/pypi/simplejson Can anyone suggest the correct way to do this as it seems it is not as direct? I've read the EasyInstall documentation and believe I understand how, but am a little confused regarding my python paths. Using Mac OSX 10.5.7 Thanks so much, -- Pete Froslie http://www.froslie.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] egg
responding to myself: apparently it seems to be as simple again as: 'easy_install simplejson' from terminal. Anyone, have a suggestion for a resource where I can read more about the paths I have setup? I would like to make sure there are not any redundancies.. thanks On Wed, Jul 1, 2009 at 4:47 PM, Pete Froslie wrote: > Hi, I have a basic question concerning pythons eggs. I have setup easy > install and used it to install mechanize-- this was easy as I only needed to > type in 'Easy_Install Mechanize' in terminal. Now I need to install > 'simplejson 2.0.9' from this here: http://pypi.python.org/pypi/simplejson > > Can anyone suggest the correct way to do this as it seems it is not as > direct? I've read the EasyInstall documentation and believe I understand > how, but am a little confused regarding my python paths. Using Mac OSX > 10.5.7 > > Thanks so much, > > -- > Pete Froslie > http://www.froslie.net > > -- Pete Froslie 617.314.0957 http://www.froslie.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
1. Pay more attention to what ends an if statement (:) also watch your indentation. 2. Code is better than my commentary. This works the way you want it to work. password = "qwerty" guess = "0" count = 0 while count != 3: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password: print "Access Denied" count = count + 1 else: print "Password Confirmed" break Robert Berman On Tue, 2009-06-30 at 19:53 -0700, Daniel Sato wrote: > I have been going through some Python Programming exercises while > following the MIT OpenCourseWare Intro to CS syllabus and am having > some trouble with the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password > three times, but can't get it to quit right away after the correct one > is entered. I know this is really basic, please forgive me. I have > no programming experience and have just started going through these > tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e > > -- > Daniel Sato > http://www.danielsato.com > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
Daniel Sato wrote: I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page: http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e When the code is that small please just put it in the body of your post. Save pastebin for (IMHO) anything over say 60 lines. 1. password = "qwerty" 2. guess = "0" 3. count = 0 4. while count != 3: 5. guess = raw_input("Enter your password: ") 6. guess = str(guess) 7. if guess != password 8. print "Access Denied" 9. count = count + 1 10. else: 11. print "Password Confirmed" 2 ways - either 1. while count != 3 and guess != password: or 11. print "Password Confirmed" 12. break -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When are strings interned?
On Wed, 2009-07-01 at 16:44 +0100, Angus Rodgers wrote: > Hello, world! > > This is my first post to the Tutor list (although I've already > posted to comp.lang.python a couple of times). > > I'm currently reading Chapter 4 of Wesley Chun's book, "Core > Python Programming" (2nd ed.). > > I find this, in Python 2.5.4, on my Win98SE system (using IDLE): > > >>> n = "colourless" > >>> o = "colourless" > >>> n == o > True > >>> n is o > True > >>> p = "green ideas" > >>> q = "green ideas" > >>> p == q > True > >>> p is q > False > > Why the difference? The string p is equal to the string q. The object p is not the object q. Robert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
Daniel Sato wrote: I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page: http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e -- Daniel Sato http://www.danielsato.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Indent line 9, because Python is indentation sensitive and does not use braces for code blocks you have to make sure it is all in-line. Also line 8 should have a colon at the end of it. My memory is a bit fuzzy on whether or not raw_input() captures the new-line character as well, so if it doesn't match still you will need to do a .strip() on your guess. Hope that helps. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
Daniel Sato wrote: I have been going through some Python Programming exercises while following the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with the first "If" exercise listed on this page: http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is entered. I know this is really basic, please forgive me. I have no programming experience and have just started going through these tutorials. My code is here: http://python.pastebin.com/m6036b52e (for short functions, it's probably better to just include it in your message. That way, they'll be archived together, in case somebody is reading this message a year from now.) You have no context. Are you going to actually do something with the password, or are you done when the confirmation has been printed out? If the latter, just add a line after the print statement, setting count = 99. You could use a break as well, but then you wouldn't have anything to indicate whether the password was ever correctly entered. Normally, this would be inside a function (def), which might return a boolean indicating whether the password succeeded or not. In that case, you might put a return instead. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
On Wed, Jul 1, 2009 at 3:53 AM, Daniel Sato wrote: > I have been going through some Python Programming exercises while following > the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with > the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password three > times, but can't get it to quit right away after the correct one is > entered. I know this is really basic, please forgive me. I have no > programming experience and have just started going through these tutorials. You need a 'break' statement to terminate the loop once the condition has been met. http://www.swaroopch.com/notes/Python_en:Control_Flow#The_break_Statement Craig ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When are strings interned?
On Wed, Jul 1, 2009 at 5:29 PM, Robert Berman wrote: > > >>> n = "colourless" > > >>> o = "colourless" > > >>> n == o > > True > > >>> n is o > > True > > >>> p = "green ideas" > > >>> q = "green ideas" > > >>> p == q > > True > > >>> p is q > > False > > > > Why the difference? > > The string p is equal to the string q. The object p is not the object q. > > Robert > Yes, but did you read his first example? That one has me scratching my head. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calculating a sort key and operator.attrgetter()
Thanks for the help, Looks like I will define a function. And yes you are right self.does_observe is constant so it will not affect the outcome. Thanks again Vincent Davis On Tue, Jun 30, 2009 at 11:15 PM, Kent Johnson wrote: > On Tue, Jun 30, 2009 at 11:39 PM, Vincent Davis > wrote: > > I have a class with an attribute which is a list "rank_list" this is a > list > > of instances f another class that has attributes "quality, is_observed" > > if I want to sort the list by the attribute "quality" I can just use, > > self.rank_list.sort(key=operator.attrgetter('quality')) > > But I want to sort like this. > > self.rank_list.sort(key=(operator.attrgetter('quality') * > > operator.attrgetter('is_observed') * self.does_observe)) > > Will this work or is there a better way? > > That won't work because attrgetter() returns a function and you can't > multiply functions. What you can do is define your own function that > returns the value you want for the key and use that for the sort. I'm > leaving out self.does_observe because that won't change for the list > items, wil it? > > def make_key(item): > return item.quality * item.is_observed > > self.rank_list.sort(key=make_key) > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
"Daniel Sato" wrote I have been able to make the module quit after entering a password three times, but can't get it to quit right away after the correct one is password = "qwerty" guess = "0" count = 0 while count != 3: guess = raw_input("Enter your password: ") guess = str(guess) if guess != password print "Access Denied" count = count + 1 else: print "Password Confirmed" - There are a couple of ways to do it. The simplest is probably just to insert the break command into the code after printing "Password Confirmed" But you could alternatively change the test in the loop to be: while count <= 3 and guess != password: as before... Either way works. Notice that I changed the loop test from !=3 to <=3 This is a little bit safer because if for some reason (a bug say) the loop code changes the count beyond 3 your code will loop forever... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming exercise
2009/7/1 Daniel Sato : > I have been going through some Python Programming exercises while following > the MIT OpenCourseWare Intro to CS syllabus and am having some trouble with > the first "If" exercise listed on this page: > > http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises > > I have been able to make the module quit after entering a password three > times, but can't get it to quit right away after the correct one is > entered. I know this is really basic, please forgive me. I have no > programming experience and have just started going through these tutorials. > > My code is here: > > http://python.pastebin.com/m6036b52e Some genral comments. # You do not need to create the guess variable wirh value "0" as it gets created in the while loop. # raw_input() already gives a string so no need to use str() on guess You will need to break out of the while loop if the password matches. So add a break under print "Password Confirmed" and it will stop when you want it to . Below is a modified (untested) version. Greets Sander password = "qwerty" count = 0 while count != 3: guess = raw_input("Enter your password: ") if guess != password print "Access Denied" count = count + 1 else: print "Password Confirmed" break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When are strings interned?
n = "colourless" o = "colourless" n == o > True n is o > True p = "green ideas" q = "green ideas" p == q > True p is q > False > > Why the difference? angus, welcome to Python! you're definitely doing your homework when it comes to trying to understand how Python manages its memory. with regards to your query, you came up with a very good example. fortunately, it's not that important for beginners to fully understand this behavior because for the most part when dealing with strings, you will rarely *care* about whether two variables reference the same string object (or not). you will just use "==" as you did above, and both cases result in the same consistent answer. checking object identity (using 'is') occurs most commonly when comparing a variable against a well-known constant such as True, False, and None. the remaining use cases are where you really do care that a pair of variables refers to the exact same object. regardless, with respect to you original question (which is asked every now and then), there is no hardcoded rule or algorithm that is published because it may change in subsequent versions of Python. the main idea is that short and often-used strings (such as those which can be attribute names or dictionary keys) are interned while the rest are not. here is another reply that someone posted several years ago that may also help: http://groups.google.com/group/comp.lang.python/msg/e4c9c360e19d9c78 hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor