Re: [Tutor] Flatten
Sorry for the delay in answering. Bill Mill wrote: [cut] 1) you should special-case dictionaries: > >> x = [1, 2, [3, 4, 5, [[6, 7], 8]], 'abc', 9, [10, 11], {'test': 12}] > >> flatten(x) > >> x [1, 2, 3, 4, 5, 6, 7, 8, 'abc', 9, 10, 11, 'test'] OK, now it only handles lists and tuples 2) What's different about your flatten than those ASPN entries? Just that it flattens in-place? I see a general-purpose flattener and a flattening generator. The flatten procedure by Ganel does excessive work, it appears, by doing one level of flattening per pass through the entire list. Initial non-iterable items are rechecked as long as any iterables remain in the list. It is also not an "in-place" routine. The one by Rodrigues and the one by Rol is recursive (and the one by Yoo is admittedly complicated). The one I presented is essentially the same as Fletcher's. Fletcher's does not actually run through all indices as I thought: as soon as it steps on an out-of-range index it exits. Fletcher's routine is a non-recursive version that runs in place that can be recommended if recursion is a problem. I'll add a note to the ASPN pages. /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Talking to mssql?
I couldn't compile it either, but got it working by copying the MSSQL.py and bin/python2.3/mssqldb.pyd from the archive to my site-packages directory. He scarcely mentions that option at the bottom of the install page: http://www.object-craft.com.au/projects/mssql/install.html. Not sure if the binary would work on Linux, though. Thanks, Ryan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of j2 Sent: Friday, April 08, 2005 6:51 PM To: tutor@python.org Subject: RE: [Tutor] Talking to mssql? >I had the same question. The best I found is this: >http://www.object-craft.com.au/projects/mssql/ >but that has a lot of strong language saying "this isn't ready for use". > >I found it perfectly usable for simply running queries, but haven't tried >to do anything more complicated with it. Not sure how >well it works on Linux, but he has binaries that worked for me. Well, doesn't fly for me, because I can't install it. Can you offer any help on the below? I am trying to install http://www.object-craft.com.au/projects/mssql/ on my Debian system, but when i try to build, i end up with the below. Also, my package list is at the bottom. hooch:~/Download/Asterisk/MSSQL-0.09# python setup.py install running install running build running build_py not copying MSSQL.py (output up-to-date) running build_ext building 'mssqldb' extension creating build/temp.linux-i686-2.2 gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC -DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD -DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE -I/usr/include -I/usr/include/python2.2 -c mssqldb.c -o build/temp.linux-i686-2.2/mssqldb.o gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC -DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD -DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE -I/usr/include -I/usr/include/python2.2 -c loginrec.c -o build/temp.linux-i686-2.2/loginrec.o gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC -DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD -DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE -I/usr/include -I/usr/include/python2.2 -c dbproc.c -o build/temp.linux-i686-2.2/dbproc.o dbproc.c:199: warning: `wrap_DbProcIntFunc_bool' defined but not used gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC -DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD -DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE -I/usr/include -I/usr/include/python2.2 -c databuf.c -o build/temp.linux-i686-2.2/databuf.o databuf.c: In function `get_buff_value': databuf.c:86: error: `DBVARYCHAR' undeclared (first use in this function) databuf.c:86: error: (Each undeclared identifier is reported only once databuf.c:86: error: for each function it appears in.) databuf.c:86: error: parse error before ')' token databuf.c:87: error: parse error before ')' token databuf.c:90: error: `DBBIT' undeclared (first use in this function) databuf.c:90: error: parse error before ')' token databuf.c: In function `DataBuf_set': databuf.c:155: error: `DBVARYCHAR' undeclared (first use in this function) databuf.c:155: error: parse error before ')' token databuf.c:160: error: parse error before ')' token databuf.c:161: error: parse error before ')' token databuf.c:169: error: `DBBIT' undeclared (first use in this function) databuf.c:169: error: parse error before ')' token databuf.c: In function `DataBuf__init__': databuf.c:270: error: `DBVARYCHAR' undeclared (first use in this function) databuf.c:273: error: `DBBIT' undeclared (first use in this function) error: command 'gcc' failed with exit status 1 hooch:~/Download/Asterisk/MSSQL-0.09# ___ 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] Re: Class - superclass
Thanks a lot, now it's clear. Bernard On Apr 8, 2005 3:48 PM, Andrei <[EMAIL PROTECTED]> wrote: > Bernard Lebel wrote on Fri, 8 Apr 2005 15:05:13 -0400: > > > I'm experimenting with basic inheritance concepts, and something that > > I would assume to work returns an error. > > > class A: > > ... def __init__( self ): > > ... self.a = 13 > > ... > class B( A ): # create sub-class of class A > > ... def __init__( self ): > > ... self.b = 14 > > Call the __init__ of the ancestor explicitly: > > >>> class B(A): > ... def __init__(self): > ... A.__init__(self) > ... self.b = 14 > >>> b = B() > >>> b.a, b.b > (13, 14) > > B inherits everything from A, but by defining B.__init__, the __init__ > inherited from A is replaced, so you'll need to call it explicitly. Python > has no way of knowing that you still want to use the original __init__ too > unless you tell it so. To demonstrate the fact that __init__ is indeed > inherited: > > >>> class C(A): > ... pass > >>> c = C() # inherited __init__ (A.__init__) is called > >>> c.a > 13 > > -- > Yours, > > Andrei > > = > Real contact info (decode with rot13): > [EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq > gur yvfg, fb gurer'f ab arrq gb PP. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: [Python-Help] Support with Image
Sorry, Copy Paste error I did that cursor.execute("insert into image values('',%s)",outfile.getvalue()) My question now is where should I have to retrieve the database value to show the image? And another question yet: I'm making this application to run as a daemon under Linux (the linux part is not a problem) How can I make my application to start when the OS start??? and How can I make my application to check every x time if it has photos to store? Is it more efficient to store the data in the database (we're talking about thousands of images) or the path and the photos in HD Thanks in advanced Alberto >From: Danny Yoo <[EMAIL PROTECTED]> >To: Alberto Troiano <[EMAIL PROTECTED]> >CC: [EMAIL PROTECTED] >Subject: Re: [Python-Help] Support with Image >Date: Sat, 9 Apr 2005 14:37:03 -0700 (PDT) > > > Hey Danny it worked > >Hi Alberto, > >Good, I'm glad that it's working now. > > > > > but now I don't know how to retrieve the image > > > > This is what I'm doing: > > > > import MySQLdb > > import Image > > from StringIO import StringIO > > im = Image.open("auto.jpg") > > outFile = StringIO() > > im.save(outFile, "JPEG") > >Ok, looks good so far. At the end of this, outFile is a file-like object >whose content should be that JPEG image. > > > > db=MySQLdb.connect(connection string) > > cursor=db.cursor() > > cursor.execute("insert into image values('',%s)",outfile) > >This last statement is problematic. Remember: outFile is a StringIO >object, not a string. A StringIO object responds to a str() call with >the following: > >## > >>> from StringIO import StringIO > >>> s = StringIO("hello world") > >>> s > > >>> str(s) >'' >## > > > >So we can't use the default. We have to be more specific and use >getvalue() to get at the content: > >## >cursor.execute("insert into image values('',%s)",outfile.getvalue()) >## > > >Hope this helps! > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: [Python-Help] Support with Image
Thanks for the reply I read first the one Danny sent me. I'll try the code and let you know how it went About the overwhelming of the machine, is a Dedicated Server so I don't think that would happen. The person in charge of the project told me that he needed a daemon to run under Linux to store in the database so he could take out the binary string into php and show it in a web site, so I think that's why he wants to store it in a database. Sorry I didn't comment this before it's just that I found out this yesterday :P Thanks in advanced Alberto >From: Matthew Dixon Cowles <[EMAIL PROTECTED]> >Reply-To: [EMAIL PROTECTED] >To: Alberto Troiano <[EMAIL PROTECTED]> >CC: [EMAIL PROTECTED] >Subject: Re: [Python-Help] Support with Image >Date: Sat, 9 Apr 2005 13:52:52 -0500 (CDT) > >Dear Alberto, > > > but now I don't know how to retrieve the image > > > outFile = StringIO() > > im.save(outFile, "JPEG") > > > cursor.execute("insert into image values('',%s)",outfile) > >It would surprise me very much if that worked correctly. Have you >checked to see what's stored in your database after you've done that? > >What Danny said was: > > Here, we use StringIO to use an in-memory file-like object: > > http://www.python.org/doc/lib/module-StringIO.html > > and we can use getvalue() to get at the binary bytes that you'll > probably want to store into your database. > >Depending on just how you're getting the data for your images, you >may want to go through a StringIO object or you may be able to just >work with the bytes that are in the file. > >But it's pretty common that it's not a very good idea to store image >data in a database. If your images are pretty small and your database >has a "BLOB" (binary large object) type, you may have some luck. If >either of those isn't the case, the overhead of getting your images >into and out of the database may overwhelm your machine. You might >want to consider storing your image files semi-randomly in a >directory hierarchy and recording in your database only the path to >the file. If you have only a few hundred or a few thousand images, a >single directory may be sufficient. If you have many more of them >than that, you may want to split them up according to some rule that >results in approximately equal numbers going in different directories. > > > cursor.execute("select * from image") > > res=cursor.fetchall() > > > > Now I have the binary string in res[1] but when I try to open it it > > gives me an error > >Here's where you may want a StringIO object. If whatever you're >giving the image to expects it in a file rather than a string, you >could do somwthing like: > > >>> s=StringIO.StringIO() > >>> s.write(res[1]) > >>> s.seek(0) > >and then give the variable s to whatever wants to read the file. > >Regards, >Matt > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.split and quotes
Thanks Kent but now I need you to explain me the code :( To give you a graphic example how can make this function to run every 5 seconds def foo(): print "Hello world!" Thanks in advanced Alberto>From: Kent Johnson <[EMAIL PROTECTED]> >To: Alberto Troiano <[EMAIL PROTECTED]> >Subject: Re: [Tutor] str.split and quotes >Date: Sat, 09 Apr 2005 11:37:56 -0400 > >Alberto Troiano wrote: >>Where and How can I see the post you make?? > >In the list archives >http://mail.python.org/pipermail/tutor/2005-April/037362.html > >Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.split and quotes
On Apr 11, 2005 11:00 AM, Alberto Troiano <[EMAIL PROTECTED]> wrote: > > To give you a graphic example how can make this function to run every 5 > seconds > > def foo(): > > print "Hello world!" > I'm not Kent, but I play him on TV ;-) import time def foo(): print "Hello world!" while true: foo() time.sleep(5) -- Kristian kristian.zoerhoff(AT)gmail.com zoerhoff(AT)freeshell.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting of files based on filesize
Hi Some of my harddrives are getting full and i would like to burn the files to some cheep DVD's. Filesizes range from lets say 1Mb to 1Gb. Ofcourse i would like to optimize the size of each DVD to be as close to 4.7Gb as possible (to save this cheep media :) ). There are plenty of software that are able to index my DVD's so sorting between disks is not a problem. Just size. What i would like to do is write a Python program that could sort my files (based on size) into chunks as close to a given storage size (DVD 4.7Gb, CD 650Mb...), and minimize the use of disks. Does anyone know of if something like this is done before in Python, that i could look at? With a couple of hundred files it should be a lot of possible combinations. So I am not lookng for something absolute, just something that could help me get a little closer to the truth :) Its the "sorting function" that i am looking for, getting filesizes and do the actual sorting of files on disk i will worry about later. Kind regards Klas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.split and quotes
On Apr 11, 2005 11:03 AM, Kristian Zoerhoff <[EMAIL PROTECTED]> wrote: > while true: > foo() > time.sleep(5) Err, make that while True: Note to self: test before posting. -- Kristian kristian.zoerhoff(AT)gmail.com zoerhoff(AT)freeshell.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems with doctest: apparent interferance between tests (LONG)
Rich Krauter said unto the world upon 2005-04-10 20:53: Brian van den Broek wrote: [text and code snipped] FWIW, I ran your code using python 2.3.4 on linux with the following modifications, and it worked fine (no failing tests out of 9): Hi Rich, Kent, and all, thanks for these additional datapoints, Rich. And thanks too, Kent for the additional pointers in your most recent post. But for the list's help, I don't know if I would have emerged from the weekend still retaining what sanity I have left :-) Best to all, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting of files based on filesize
This is a hard problem. It is a version of the "0-1 knapsack problem" - googling for that might give you some ideas. Kent Klas Marteleur wrote: Hi Some of my harddrives are getting full and i would like to burn the files to some cheep DVD's. Filesizes range from lets say 1Mb to 1Gb. Ofcourse i would like to optimize the size of each DVD to be as close to 4.7Gb as possible (to save this cheep media :) ). There are plenty of software that are able to index my DVD's so sorting between disks is not a problem. Just size. What i would like to do is write a Python program that could sort my files (based on size) into chunks as close to a given storage size (DVD 4.7Gb, CD 650Mb...), and minimize the use of disks. Does anyone know of if something like this is done before in Python, that i could look at? With a couple of hundred files it should be a lot of possible combinations. So I am not lookng for something absolute, just something that could help me get a little closer to the truth :) Its the "sorting function" that i am looking for, getting filesizes and do the actual sorting of files on disk i will worry about later. Kind regards Klas ___ 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] UselessPython 2.0
On Apr 10, 2005 7:18 AM, Dick Moores <[EMAIL PROTECTED]> wrote: > I'm a newbie, but would this qualify as a contribution to UselessPython 2.0? Hello Dick, don't be shy, or do you suspect it might be too usefull? ;-) I found it funny, so it must be good enough. here my remarks: > def makeStringAllLowercaseAlpha(s): > """ > Take any string, convert all uppercase alphabetic characters to > lower case, > then strip all non-alphabetic characters [what's bad about non-alphabetic characters?] > """ > s1 = string.lower(userstring) oops: this function gets an argument s, here you're using the global variable userstring. I know, it works, but there will be functions were mistakes like this weren't just ugly but buggy. > s2 = "" > for index in range(len(s1)): > if s1[index] in string.ascii_lowercase: > s2 += s1[index] or easier (much more readable, helps understanding the programm): for char in s1: if char in string.ascii_lowercase: s2 += char regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] UselessPython 2.0
On Apr 11, 2005 7:30 PM, Michael Janssen <[EMAIL PROTECTED]> wrote: > [what's bad about non-alphabetic characters?] I found it out for myself. To quote from Dick's post: "A man, a plan, a canal, Panama!" Seems like palindromes are allowed not to reflect whitespace and punctuation (an how could this be?). cheers Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using python with MS Excel (Newbie)
Hi All, I am trying to create a couple of python programs that creates and modifies MS Excel docs. I have been searching the web and have found some interesting pieces of information and programs but i need something more complete. Is the any documentation available explaning all the options available programming with win32com. I thank you in advance for the help. Regards, Andre ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Cell Bio Newbie Here
Hey all, Sorry for the bother, thanks for the help. I'm trying to write a password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print ``That must have been complicated.'' Following is what I got. If I type "unicorn" it goes straight to "welcome in." Great. But after my third mistake, it just loops in "That must have been complicated." I'd like for someone to tell me "why" i screwed up. Not to just fix it. Thank you so much in advance. And to give you a little smile for a Monday, I've been working on this for daysargh #first of all, why does this have to be here? password="foobar" count=3 current_count=0 while password !="unicorn": if current_count print "Welcome in" Best, Gary Gary Laevsky, Ph.D. Keck Facility Manager, CenSSIS Northeastern University 302 Stearns 360 Huntington Ave. Boston, MA 02115 voice(617) 373 - 2589 fax(617) 373 - 7783 http://www.censsis.neu.edu http://www.ece.neu.edu/groups/osl http://www.keck3dfm.neu.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Hi Gary, In a while loop, you could looping until the while condition is no longer true. So your one - while password != "unicorn" So while password isn't unicorn, your condition is True, so your while loop will keep looping until the password equals 'unicorn' After your 3rd mistake, the if current_count Python goes Is password unicorn? Nope. Is current_count < count? Nope. better print "That must have been complicated" Is password unicorn? Nope. Is current_count < count? Nope. better print"That must have been complicated" Is password unicorn? Nope. Is current_count < count? Nope. better print "That must have been complicated" if you get the drift. If you want it to be Is password unicorn? Nope. Is current_count < count? Yup. Get password Is password unicorn? Nope. Is current_count < count? Yup. Get password Is password unicorn? Nope. Is current_count < count? Yup. Get password Is password unicorn? Nope. Is current_count < count? Nope. Get password better print "That must have been complicated" Now exit the loop you need is to check out the break command in a while loop - while 1 == 1: x = raw_input('Secret? ') if x == 'foo': print 'broke' break else: print 'loopy, loopy'. print 'ended loop' Regards, Liam Clarke On Apr 12, 2005 6:54 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:Hey all,Sorry for the bother, thanks for the help.I'm trying to write a password guessing program to keep track ofhow many times the user has entered the password wrong.If it is more than 3 times, print ``That must have been complicated.''Following is what I got. If I type "unicorn" it goes straight to "welcomein." Great. But after my third mistake, it just loops in "That must havebeen complicated."I'd like for someone to tell me "why" i screwed up. Not to just fix it.Thank you so much in advance. And to give you a little smile for a Monday,I've been working on this for daysargh#first of all, why does this have to be here?password="foobar"count=3current_count=0while password !="unicorn": if current_count password=raw_input("Password:") current_count=current_count+1 else: print "That must have been complicated"print "Welcome in"Best,GaryGary Laevsky, Ph.D.Keck Facility Manager, CenSSISNortheastern University302 Stearns360 Huntington Ave.Boston, MA 02115voice(617) 373 - 2589fax(617) 373 - 7783http://www.censsis.neu.eduhttp://www.ece.neu.edu/groups/oslhttp://www.keck3dfm.neu.edu___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor-- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.'___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Cell Bio Newbie Here
Make a flowchart, by hand, on paper of what this program does. That should help explain the why. Basically, after 3 attempts, your "while" loop becomes an infinte loop. Once you are over the count, the user is never prompted for another password, so the password remains not equal to "unicorn" until the end of time. Thanks, Ryan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Monday, April 11, 2005 2:55 PM To: tutor@python.org Subject: [Tutor] Cell Bio Newbie Here Hey all, Sorry for the bother, thanks for the help. I'm trying to write a password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print ``That must have been complicated.'' Following is what I got. If I type "unicorn" it goes straight to "welcome in." Great. But after my third mistake, it just loops in "That must have been complicated." I'd like for someone to tell me "why" i screwed up. Not to just fix it. Thank you so much in advance. And to give you a little smile for a Monday, I've been working on this for daysargh #first of all, why does this have to be here? password="foobar" count=3 current_count=0 while password !="unicorn": if current_count fax(617) 373 - 7783 http://www.censsis.neu.edu http://www.ece.neu.edu/groups/osl http://www.keck3dfm.neu.edu ___ 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] Cell Bio Newbie Here
[EMAIL PROTECTED] said unto the world upon 2005-04-11 14:54: Hey all, Sorry for the bother, thanks for the help. I'm trying to write a password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print ``That must have been complicated.'' Following is what I got. If I type "unicorn" it goes straight to "welcome in." Great. But after my third mistake, it just loops in "That must have been complicated." I'd like for someone to tell me "why" i screwed up. Not to just fix it. Thank you so much in advance. And to give you a little smile for a Monday, I've been working on this for daysargh Hi Gary, #first of all, why does this have to be here? password="foobar" Given that you've structured things with a while loop that checks whether password is not equal to something else, you need to have a value for password, else you cannot check if the value of password is . (If I asked you "Is graffleb less than 4?" you reasonably would reply "How can I answer that? You've not told me to what `graffleb' refers!") That said, I'd use >>> password = None instead. count=3 current_count=0 while password !="unicorn": if current_count print "Welcome in" You'd like the loop to exit after your third mistake, right? Well, the loop hasn't been told to do that :-) The word you are looking for is `break'. If that doesn't get you to where you can make it behave as desired, post again. HTH, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Cell Bio Newbie Here
Hey Gary password="foobar" ### the variable password has to be here because you are referiencing before the assignment inside the while sentence. You can also set it to password="" and still will work because you have to tell (in this example) that password is a reserved word(variable) count=3 current_count=0 ### Here you have two options: Option 1: while password !="unicorn": if current_count password=raw_input("Password:") current_count=current_count+1 else: current_count=2 print "That must have been complicated" print "Welcome in" Add this line inside the "else" clause: current_count=2 This will make you have more chances and if you fail it will complain. The problem is that current_count doesn't decrement in the loop, so let's say you fail 3 times the current_count will keep looping because its value is 3 and it won't change in your code. Also I think that if you're making an application to restrict the error to 3 times you may want to finish the app to start over so in that case you may want to try option 2. ### Option 2: while password !="unicorn" and current_count <= count: if current_count password=raw_input("Password:") current_count=current_count+1 else: current_count=current_count+1 print "That must have been complicated" if password="unicorn": print "Try again Later" else: print "Welcome in" Here you will lock your prog when the user fails 3 times and will print your line once and then will jump to Try Again later and it will finish Hope that helps Regards Alberto >From: [EMAIL PROTECTED] >To: tutor@python.org >Subject: [Tutor] Cell Bio Newbie Here >Date: Mon, 11 Apr 2005 14:54:37 -0400 > >Hey all, > >Sorry for the bother, thanks for the help. > >I'm trying to write a password guessing program to keep track of >how many times the user has entered the password wrong. >If it is more than 3 times, print ``That must have been >complicated.'' > >Following is what I got. If I type "unicorn" it goes straight to >"welcome in." Great. But after my third mistake, it just loops in >"That must have been complicated." > >I'd like for someone to tell me "why" i screwed up. Not to just fix >it. > >Thank you so much in advance. And to give you a little smile for a >Monday, I've been working on this for daysargh > > > >#first of all, why does this have to be here? >password="foobar" > >count=3 >current_count=0 > >while password !="unicorn": > if current_count password=raw_input("Password:") > current_count=current_count+1 > else: > print "That must have been complicated" > > >print "Welcome in" >Best, > >Gary > > > >Gary Laevsky, Ph.D. >Keck Facility Manager, CenSSIS >Northeastern University >302 Stearns >360 Huntington Ave. >Boston, MA 02115 >voice(617) 373 - 2589 >fax(617) 373 - 7783 > >http://www.censsis.neu.edu > >http://www.ece.neu.edu/groups/osl > >http://www.keck3dfm.neu.edu > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Sorting of files based on filesize
Klas Marteleur telia.com> writes: > Some of my harddrives are getting full and i would like to burn the files to > some cheep DVD's. Filesizes range from lets say 1Mb to 1Gb. > > Ofcourse i would like to optimize the size of each DVD to be as close to > 4.7Gb > as possible (to save this cheep media :) ). > > There are plenty of software that are able to index my DVD's so sorting > between disks is not a problem. Just size. > > What i would like to do is write a Python program that could sort my files > (based on size) into chunks as close to a given storage size (DVD 4.7Gb, CD > 650Mb...), and minimize the use of disks. Does anyone know of if something > like this is done before in Python, that i could look at? Depends on how optimal you want things done. The easiest way is probably to sort them by size, start with the largest and then keep adding smaller ones to the compilation until it's full. Then start with the largest that's left and repeat the process until you're left with (part of) a DVD with all kinds of small files. I have some doubts about how optimal this will turn out to be. So instead, I went and implemented a fun 100-line script which uses a simple genetic algorithm to generate disc layouts. Lacking real files, the script listed below first generates 500 files with normally distributed sizes with an average of 200 MB and a standard deviation of 200MB (with some sanity checks to prevent files with negative size and files larger than 650MB) and tries to cram those in 650MB CD's. Here's the link: http://ww3.6URL.com/FOK It does this by first shuffling the list of files in a random manner several times and determining the best fit of those (the solution that fills up the discs as much as possible). This is the initial parent. Then it randomly shuffles parts of this parent several times and determines the best fit of those combinations. Then it uses that best fit to generate new children, etc. The siblings variable determines how many shuffles are performed before a best fit in that group is selected. The generations variable determines how many times the process of selecting the best is repeated. The algorithm seems to work reasonably well (I get 190-ish CD's for files which would cover 180-ish CD's if those CD's could be filled perfectly, even though initial random file distributions give 240+ discs), but I haven't checked the results very thoroughly. It might be doing something really stupid. It's also reasonably fast (1000 generations of 1000 individuals each is feasible for example, but not necessarily very useful). There's also no guarantee that the result is the absolute best you can get. Yours, Andrei ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.split and quotes
Hey Kristian, thanks for the reply The problem to use sleep is that all widgets gets locked up and they doesn't refresh themselves I just made that example so anyone can explain the code Kent posted earlier In my real application is a Tkinter GUI so I should not use sleep Thanks anyway Regards Alberto>From: Kristian Zoerhoff <[EMAIL PROTECTED]> >Reply-To: Kristian Zoerhoff <[EMAIL PROTECTED]> >To: Alberto Troiano <[EMAIL PROTECTED]> >CC: [EMAIL PROTECTED], tutor@python.org >Subject: Re: [Tutor] str.split and quotes >Date: Mon, 11 Apr 2005 11:03:13 -0500 > >On Apr 11, 2005 11:00 AM, Alberto Troiano <[EMAIL PROTECTED]> wrote: > > > > To give you a graphic example how can make this function to run every 5 > > seconds > > > > def foo(): > > > > print "Hello world!" > > > >I'm not Kent, but I play him on TV ;-) > >import time > >def foo(): > print "Hello world!" > >while true: > foo() > time.sleep(5) > >-- >Kristian > >kristian.zoerhoff(AT)gmail.com >zoerhoff(AT)freeshell.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Alberto Troiano said unto the world upon 2005-04-11 16:09: Hey Gary password="foobar" ### the variable password has to be here because you are referiencing before the assignment inside the while sentence. You can also set it to password="" and still will work because you have to tell (in this example) that password is a reserved word(variable) Hi Alberto, Gary, and all, Alberto, if I may, I think there are some problems in what you posted. (But, if you want to see some *really* troubled postings, just find some of my earliest attempts to answer on the tutor list!) I think it is important to keep the distinction between variables and reserved words clear. if = "Won't work as 'if' is a reserved word" Traceback ( File "", line 1 if = "Won't work as 'if' is a reserved word" ^ SyntaxError: invalid syntax "Reserved words" or "keywords" are the fixed words of the Python language itself. See Section 2.3.1 Keywords of the Language Reference. count=3 current_count=0 ### Here you have two options: Option 1: while password !="unicorn": if current_count password=raw_input("Password:") current_count=current_count+1 else: current_count=2 print "That must have been complicated" print "Welcome in" Add this line inside the "else" clause: current_count=2 This will make you have more chances and if you fail it will complain. That isn't going to solve the infinite looping problem in the case that password never is equal to 'unicorn' The problem is that current_count doesn't decrement in the loop, so let's say you fail 3 times the current_count will keep looping because its value is 3 and it won't change in your code. Also I think that if you're making an application to restrict the error to 3 times you may want to finish the app to start over so in that case you may want to try option 2. ### Option 2: while password !="unicorn" and current_count <= count: if current_count password=raw_input("Password:") current_count=current_count+1 else: current_count=current_count+1 print "That must have been complicated" if password="unicorn": print "Try again Later" else: print "Welcome in" Here you will lock your prog when the user fails 3 times and will print your line once and then will jump to Try Again later and it will finish I'm pretty sure there are some crossed wires here. :-) Maybe you intended the last two print statements to be swapped? But, either way, since Option 2 will never exit the while loop unless password does equal 'unicorn', the final else clause will never be reached. Or, so it seems to me. Last, I don't see why the first else block increments current_count. Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Ok, it's a logic error in the while loop. Starting at the beginning: you can't compare the value of "password" until the user inputs the value, which is why it's requiring you to put password = "foobar" at the top. Otherwise, password has no value, and as far as the interpreter is concerned, it doesn't exist yet. The rest of it is a logic error because your while loop is executing as long as "unicorn" != "foobar" and therefore it's continuously looping. Each time it loops, it asks for the password again, and sets a LOCAL variable called password to the new raw_input. Then, when you get to the third time, it does the "if current_count in." Great. But after my third mistake, it just loops in "That must have > been complicated." > > I'd like for someone to tell me "why" i screwed up. Not to just fix it. > #first of all, why does this have to be here? > password="foobar" > > count=3 > current_count=0 > > while password !="unicorn": > if current_count password=raw_input("Password:") > current_count=current_count+1 > else: > print "That must have been complicated" > > > print "Welcome in" > Best, > > Gary > > > > Gary Laevsky, Ph.D. > Keck Facility Manager, CenSSIS > Northeastern University > 302 Stearns > 360 Huntington Ave. > Boston, MA 02115 > voice(617) 373 - 2589 > fax(617) 373 - 7783 > > http://www.censsis.neu.edu > > http://www.ece.neu.edu/groups/osl > > http://www.keck3dfm.neu.edu > > ___ > 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] str.split and quotes
Alberto Troiano wrote: Thanks Kent but now I need you to explain me the code :( That code won't work for you. It is for timing how long it takes to do something, not for generating repeated events. To give you a graphic example how can make this function to run every 5 seconds def foo(): print "Hello world!" Here is a Tkinter program that updates the text of a label every second. It uses the root.after() method to schedule a callback. Since it is a one-time callback, the callback reschedules itself each time it is called. This is not a great example of code structure but it is simple and demonstrates the concept. from Tkinter import * count = 0 def update(): ''' Callback method updates the label ''' global count count += 1 label.configure(text=str(count)) # Schedule another callback root.after(1000, update) root=Tk() label=Label(root,text="0") label.pack() b=Button(root,text="Bye",command='exit') b.pack() # Schedule the initial callback root.after(1000, update) root.mainloop() Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] str.split and quotes
Gotcha!! I'll try it with my app and let you know how it went Thanks Alberto Gaucho>From: Kent Johnson <[EMAIL PROTECTED]> >CC: tutor@python.org >Subject: Re: [Tutor] str.split and quotes >Date: Mon, 11 Apr 2005 16:55:22 -0400 > >Alberto Troiano wrote: >>Thanks Kent but now I need you to explain me the code :( > >That code won't work for you. It is for timing how long it takes to >do something, not for generating repeated events. > >> >>To give you a graphic example how can make this function to run >>every 5 seconds >> >>def foo(): >> >> print "Hello world!" > >Here is a Tkinter program that updates the text of a label every >second. It uses the root.after() method to schedule a callback. >Since it is a one-time callback, the callback reschedules itself >each time it is called. > >This is not a great example of code structure but it is simple and >demonstrates the concept. > >from Tkinter import * > >count = 0 > >def update(): > ''' Callback method updates the label ''' > global count > count += 1 > label.configure(text=str(count)) > > # Schedule another callback > root.after(1000, update) > > >root=Tk() >label=Label(root,text="0") >label.pack() > >b=Button(root,text="Bye",command='exit') >b.pack() > ># Schedule the initial callback >root.after(1000, update) > >root.mainloop() > > >Kent > >___ >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] Cell Bio Newbie Here
Hi Brian Thanks for correcting me about the variable and reserved word differences (just for the record the problem is that my english is not so good, you see I'm from Bolivia so pardon my francôis :P) About the code I posted let me stand for it because it sure works like a charm. I tested and the Option 1 gives the message of "That has been difficult" stuff when you pass the 3 errors and still ask you for password. The option 2 loops 3 times unless you put unicorn or whatever is the password and at the third time it gives the message and then increment the current_count once more to take you out of the while loop Test it and let me know how it went, and also if I have a few problems with my writing please let me know as I will try to correct them Regards Alberto >From: Brian van den Broek <[EMAIL PROTECTED]> >To: Alberto Troiano <[EMAIL PROTECTED]> >CC: [EMAIL PROTECTED], tutor@python.org >Subject: Re: [Tutor] Cell Bio Newbie Here >Date: Mon, 11 Apr 2005 16:45:55 -0400 > >Alberto Troiano said unto the world upon 2005-04-11 16:09: >>Hey Gary >> >>password="foobar" >> >>### >> >>the variable password has to be here because you are referiencing >>before the assignment inside the while sentence. You can also set >>it to password="" and still will work because you have to tell (in >>this example) that password is a reserved word(variable) > >Hi Alberto, Gary, and all, > >Alberto, if I may, I think there are some problems in what you >posted. (But, if you want to see some *really* troubled postings, >just find some of my earliest attempts to answer on the tutor list!) > >I think it is important to keep the distinction between variables >and >reserved words clear. > if = "Won't work as 'if' is a reserved word" >Traceback ( File "", line 1 > if = "Won't work as 'if' is a reserved word" > ^ >SyntaxError: invalid syntax > >"Reserved words" or "keywords" are the fixed words of the Python >language itself. See Section 2.3.1 Keywords of the Language >Reference. > >> >> >>count=3 >>current_count=0 >> >>### >> >>Here you have two options: >> >>Option 1: >> >> >>while password !="unicorn": >> if current_count> password=raw_input("Password:") >> current_count=current_count+1 >> else: current_count=2 print "That must have >>been complicated" >> >>print "Welcome in" >> >>Add this line inside the "else" clause: current_count=2 This >>will make you have more chances and if you fail it will complain. > >That isn't going to solve the infinite looping problem in the case >that password never is equal to 'unicorn' > >>The problem is that current_count doesn't decrement in the loop, so >>let's say you fail 3 times the current_count will keep looping >>because its value is 3 and it won't change in your code. Also I >>think that if you're making an application to restrict the error to >>3 times you may want to finish the app to start over so in that >>case you may want to try option 2. >> >>### >> >>Option 2: >> >>while password !="unicorn" and current_count <= count: >> if current_count> password=raw_input("Password:") >> current_count=current_count+1 >> else: current_count=current_count+1 >> print "That must have been complicated" if >>password="unicorn": print "Try again Later" >> else: print "Welcome in" >> >>Here you will lock your prog when the user fails 3 times and will >>print your line once and then will jump to Try Again later and it >>will finish > >I'm pretty sure there are some crossed wires here. :-) Maybe you >intended the last two print statements to be swapped? But, either >way, since Option 2 will never exit the while loop unless password >does >equal 'unicorn', the final else clause will never be reached. Or, so >it seems to me. > >Last, I don't see why the first else block increments current_count. > >Best, > >Brian vdB > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Sorry In the option 2 in the last IF clause there is a missing = it should be if password == "unicorn" and not if password="unicorn" Regards Alberto Gaucho>From: Brian van den Broek <[EMAIL PROTECTED]> >To: Alberto Troiano <[EMAIL PROTECTED]> >CC: [EMAIL PROTECTED], tutor@python.org >Subject: Re: [Tutor] Cell Bio Newbie Here >Date: Mon, 11 Apr 2005 16:45:55 -0400 > >Alberto Troiano said unto the world upon 2005-04-11 16:09: >>Hey Gary >> >>password="foobar" >> >>### >> >>the variable password has to be here because you are referiencing >>before the assignment inside the while sentence. You can also set >>it to password="" and still will work because you have to tell (in >>this example) that password is a reserved word(variable) > >Hi Alberto, Gary, and all, > >Alberto, if I may, I think there are some problems in what you >posted. (But, if you want to see some *really* troubled postings, >just find some of my earliest attempts to answer on the tutor list!) > >I think it is important to keep the distinction between variables >and >reserved words clear. > if = "Won't work as 'if' is a reserved word" >Traceback ( File "", line 1 > if = "Won't work as 'if' is a reserved word" > ^ >SyntaxError: invalid syntax > >"Reserved words" or "keywords" are the fixed words of the Python >language itself. See Section 2.3.1 Keywords of the Language >Reference. > >> >> >>count=3 >>current_count=0 >> >>### >> >>Here you have two options: >> >>Option 1: >> >> >>while password !="unicorn": >> if current_count> password=raw_input("Password:") >> current_count=current_count+1 >> else: current_count=2 print "That must have >>been complicated" >> >>print "Welcome in" >> >>Add this line inside the "else" clause: current_count=2 This >>will make you have more chances and if you fail it will complain. > >That isn't going to solve the infinite looping problem in the case >that password never is equal to 'unicorn' > >>The problem is that current_count doesn't decrement in the loop, so >>let's say you fail 3 times the current_count will keep looping >>because its value is 3 and it won't change in your code. Also I >>think that if you're making an application to restrict the error to >>3 times you may want to finish the app to start over so in that >>case you may want to try option 2. >> >>### >> >>Option 2: >> >>while password !="unicorn" and current_count <= count: >> if current_count> password=raw_input("Password:") >> current_count=current_count+1 >> else: current_count=current_count+1 >> print "That must have been complicated" if >>password="unicorn": print "Try again Later" >> else: print "Welcome in" >> >>Here you will lock your prog when the user fails 3 times and will >>print your line once and then will jump to Try Again later and it >>will finish > >I'm pretty sure there are some crossed wires here. :-) Maybe you >intended the last two print statements to be swapped? But, either >way, since Option 2 will never exit the while loop unless password >does >equal 'unicorn', the final else clause will never be reached. Or, so >it seems to me. > >Last, I don't see why the first else block increments current_count. > >Best, > >Brian vdB > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting of files based on filesize
Quoting Kent Johnson <[EMAIL PROTECTED]>: > This is a hard problem. It is a version of the "0-1 knapsack problem" - > googling for that might give > you some ideas. > > Hi > > Some of my harddrives are getting full and i would like to burn the files > > to > > some cheep DVD's. Filesizes range from lets say 1Mb to 1Gb. > > Ofcourse i would like to optimize the size of each DVD to be as close to > > 4.7Gb > > as possible (to save this cheep media :) ). Hmm, I would have called it an example of bin packing... http://en.wikipedia.org/wiki/Bin_packing_problem -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Alberto Troiano said unto the world upon 2005-04-11 17:43: Hi Brian Thanks for correcting me about the variable and reserved word differences (just for the record the problem is that my english is not so good, you see I'm from Bolivia so pardon my francôis :P) Hi Alberto, I wouldn't have known you felt you had difficulties in English had you not said so. :-) About the code I posted let me stand for it because it sure works like a charm. I tested and the Option 1 gives the message of "That has been difficult" stuff when you pass the 3 errors and still ask you for password. The option 2 loops 3 times unless you put unicorn or whatever is the password and at the third time it gives the message and then increment the current_count once more to take you out of the while loop Test it and let me know how it went, and also if I have a few problems with my writing please let me know as I will try to correct them You are absolutely right that your `Option 2' code does exit the loop. I somehow missed that the while condition had an `and' in it :-[ My apologies. Your second post said you intended the final if clause to be: if password=="unicorn": # etc I've changed it to if password != "unicorn": # etc and get what I would think is correct behaviour. So, it now reads: password = None # necessary pre-setting of names current_count = 0 count = 3 while password != "unicorn" and current_count <= count: if current_count < count: password=raw_input("Password:") current_count=current_count+1 else: current_count=current_count+1 print "That must have been complicated" if password!="unicorn": print "Try again Later" else: print "Welcome in" I would suggest that it be done like this, though: # pre-sets as before go here while password != "unicorn": if current_count < count: password = raw_input("Password:") if password=='unicorn': print 'Welcome in' else: print "That must have been complicated" print "Try again Later" break current_count += 1 This pushes all of the actions consequent on the password into the password fetching loop and uses just a single incrementing line. Anyway, once again, sorry for misreading and mis-correcting you. Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using python with MS Excel (Newbie)
Hi Andre, The gent responsible for win32com Mark Hammond wrote a book about it, but there's quite a useful help file included with the Win32all package. http://www.oreilly.com/catalog/pythonwin32/ I do recommend the book, however. Also I recommend The Quick Python book, it has a nice run-down on creating COM objects to interact with MS software. Regards, Liam ClarkeOn Apr 12, 2005 5:42 AM, Andre Thompson <[EMAIL PROTECTED]> wrote:Hi All,I am trying to create a couple of python programs that creates andmodifies MS Excel docs.I have been searching the web and have found some interesting piecesof information and programs but i need something more complete. Is theany documentation available explaning all the options availableprogramming with win32com.I thank you in advance for the help.Regards,Andre___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor-- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.'___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using python with MS Excel (Newbie)
PS Check out the sample chapter http://www.oreilly.com/catalog/pythonwin32/chapter/ch12.htmlOn Apr 12, 2005 11:42 AM, Liam Clarke <[EMAIL PROTECTED]> wrote:Hi Andre, The gent responsible for win32com Mark Hammond wrote a book about it, but there's quite a useful help file included with the Win32all package. http://www.oreilly.com/catalog/pythonwin32/ I do recommend the book, however. Also I recommend The Quick Python book, it has a nice run-down on creating COM objects to interact with MS software. Regards, Liam ClarkeOn Apr 12, 2005 5:42 AM, Andre Thompson <[EMAIL PROTECTED]> wrote:Hi All,I am trying to create a couple of python programs that creates andmodifies MS Excel docs.I have been searching the web and have found some interesting piecesof information and programs but i need something more complete. Is theany documentation available explaning all the options availableprogramming with win32com.I thank you in advance for the help.Regards,Andre___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor-- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.' -- 'There is only one basic human right, and that is to do as you damn well please.And with it comes the only basic human duty, to take the consequences.'___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cell Bio Newbie Here
Ok, it's a logic error in the while loop. Starting at the beginning: you can't compare the value of "password" until the user inputs the value, which is why it's requiring you to put password = "foobar" at the top. Otherwise, password has no value, and as far as the interpreter is concerned, it doesn't exist yet. Looks good. The rest of it is a logic error because your while loop is executing as long as "unicorn" != "foobar" and therefore it's continuously looping. Each time it loops, it asks for the password again, and sets a LOCAL variable called password to the new raw_input. So you're telling us that --- x = 5 while x != 5: print "Not equal" x = x+1 print "Equal" will result in an infinite loop because stating x = x+1 sets a Local variable? I don't think so. Maybe what you're saying is the condition stated with the while loop is only executed once? That's not the case. Oh by the way Gary -- summing up the answers. password = raw_input("What's the password? ") count = 0 good = True while password != 'unicorn': if count > 2: print "That must be hard work." good = False break print "That's not it." count = count+1 password = raw_input("What's the password? ") if good: print "Welcome in. " else: print "I'm gonna kick you out. " Execution is as follows. We start off by asking the password. Count is zero. We assume that password is good # good = True We check with while loop condition. If good, skip loop and print "Welcome in " because good == True If bad, check if count greater than three. In this case no. print "That's not it" because whatever the password is, it's not unicorn, or we wouldn't be in the loop. Increment count Ask another password. Check to see if password is unicorn. If it is, good == True, so "Welcome in. " is printed. If not, check to see if count > 2 i.e. this is the third time. In this case it's not, so print "That's not it. " Increment count Ask another password. Check to see if password is unicorn. If it is, good still equals True, so print "Welcome in." If not, which it isn't yet, check to see if count > 2. In this case, it is, so print "This must be hard work." Set good = False break out of loop good != True so print "I'm gonna kick you out." HTH, Jacob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting of files based on filesize
--request for a method of sorting disk files based on size so as to fill backup disks-- You may want to check out the karp.py routine posted at http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/749797 Right now it is coded to split N numbers into 2 groups that have sums as nearly identical as possible. The set of numbers it is coded to test are are the first 50 square roots of integers; you would replace these with a list of N file sizes that are close to 2X a single disk capacity and it would tell you how to split up the group into two nearly-identically sized groups. It's "very, very, very clever, and runs in an eyeblink" says Tim Peters. You might even want to use Tim's greedy "knapsack filler" approach that he initially proposed as part of the thread above which will try random selections from a list of values and keep track of the one that came closest to a target value. Your target value would be 2X the storage limit and then you could use the karp routine to split it nicely. Tim's greedy approach is at http://mail.python.org/pipermail/tutor/2001-August/008075.html An alternative to breaking your list of file sizes into sublists that are close to 2X your disk capacity would be to generalize the algorithm to break the numbers into M groups rather than 2 groups...but I'm not sure how easy that will be. You're going to love using the karp for this problem :-) /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor