From eike.welk at gmx.net Tue Dec 1 00:37:11 2009 From: eike.welk at gmx.net (Eike Welk) Date: Tue, 01 Dec 2009 00:37:11 +0100 Subject: [Tutor] numerical simulation + SQLite In-Reply-To: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> References: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> Message-ID: <200912010037.12666.eike.welk@gmx.net> Hello Faisal! Just in case you don't know it, maybe Pytables is the right solution for you. It is a disk storage library specially for scientific applications: http://www.pytables.org/moin The makers claim, that it is fast. It has on the fly data compression which allegedly makes the library faster because fewer data has to be written to disk. The lead developer gave a talk about the topic with interesting slides. He proposes to compress not only data that is stored on disks, but also data resides in RAM; because even RAM is very slow compared to the speed of modern processors. (The slides are near the top of the page.) http://www.euroscipy.org/presentations/abstracts/abstract_alted.html Eike. From questions.anon at gmail.com Tue Dec 1 00:55:10 2009 From: questions.anon at gmail.com (questions anon) Date: Mon, 30 Nov 2009 15:55:10 -0800 Subject: [Tutor] read in ascii and plot Message-ID: I would like to read in two columns of data from a *.txt file I type f=open("e:/testascii.txt") import pylab pylab.scatter(f) and then receive an error. How do I point it to each column and do I need to do anything about the space gap between the two columns? Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Tue Dec 1 01:50:53 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 30 Nov 2009 19:50:53 -0500 Subject: [Tutor] python closures In-Reply-To: References: <20091130112445.7451eaf6@o> Message-ID: <4B14686D.3000003@ieee.org> Alan Gauld wrote: >
> "spir" wrote > > I did wonder if you would need n=n but I didn't think you would need x=x. > > Its an interesting example and I confess I don't fully understand how > Python's > naming/reference rules are working here. > >> to let the inner func g0 "remember" outer values. >> Why is this idiom used, then? Has something changed, or do I miss >> a relevant point? > > I thought you might need to do it if n had been a parameter of f()... but > having tried it no, it works as above. > > I look forward to the explanation. > > Alan G. > > Maybe a more complex example might show the various linkages. glob = 42 def outer(parm1): free = 12 free3 = 19 def inner(parm2, parm3=free3): print "global", glob, ", free vars", parm1, free, free3, ", locals", parm2, parm3 free = 49 free3 = 48 return inner newfunc = outer(10) newfunc(45) produces output: global 42 , free vars 10 49 48 , locals 45 19 So when the inner() function is actually called, glob is just a global. parm1, fre, and free3 hold the values they ended up with when outer() returned, and local parm2 is passed by top-level code, while local parm3 gets its default value assigned when "def inner(...) was executed. Notice that the free variables free, free3, and parm1 are referring to the function's ending state, not to the state when the function was defined. This has an impact when you've got inner being defined in a loop. And this example could be made more complex if outer() is a generator, in which case it may not have actually ended when inner gets called. HTH DaveA From davea at ieee.org Tue Dec 1 02:04:10 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 30 Nov 2009 20:04:10 -0500 Subject: [Tutor] x is a global variable In-Reply-To: <20091130190208.4c2545d3@o> References: <20091130112445.7451eaf6@o> <200911301809.39269.eike.welk@gmx.net> <20091130190208.4c2545d3@o> Message-ID: <4B146B8A.70208@ieee.org> spir wrote: > Hello Eike! > > Eike Welk dixit: > > > > Well, this is certainly not specific to closures. > > i = 0 > def f(): > i = i+1 > print i > f() > ==> UnboundLocalError > > Imo, in this case, "i = i+1" is a kind of "paradoxal injonction" (lol! not sure of the exact idiom in english). You tell python both to create a local i (thus ignore any other scope to lookup for variables called 'i') and to use global i to define the local one. > If I were the victim of such a "paradoxal injonction" I would reply with a naughty word! > > > I believe the easiest model to understand the behavior is: The compiler scans the entire function to find which variables are assigned (via =, as, or other syntax) anywhere in the function. Then for those variables, all references are done without any dictionary accesses. Thus an assignment anywhere (not just on the same line) cause all references to be to the (unbound) local. DaveA From waynejwerner at gmail.com Tue Dec 1 02:26:33 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 30 Nov 2009 19:26:33 -0600 Subject: [Tutor] read in ascii and plot In-Reply-To: References: Message-ID: <333efb450911301726jb6d1ab5l9606566836b74442@mail.gmail.com> On Mon, Nov 30, 2009 at 5:55 PM, questions anon wrote: > I would like to read in two columns of data from a *.txt file > > I type > > f=open("e:/testascii.txt") > import pylab > pylab.scatter(f) > > and then receive an error. > How do I point it to each column and do I need to do anything about the > space gap between the two columns? > Thanks in advance. > A sample of the data is always helpful, but I'll take a shot in the dark. If you have data like this: 2.31 72 98 23 ... .... 34 7.32 And those are x y pairs you could do something like this: f = open('input.txt') #List comprehension to read all the lines as [[x1, y1], [x2, y2], ... [xn, yn]] data = [line.split() for line in f] # Reorient values as [(x1, x2,... xn), (y1, y2, ... yn)] data = zip(*data) # plot the xy vals pylab.scatter(data[0], data[1]) That should be something along the lines of what you're looking for. HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Dec 1 02:48:17 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 30 Nov 2009 20:48:17 -0500 Subject: [Tutor] numerical simulation + SQLite In-Reply-To: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> References: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> Message-ID: <4B1475E1.9060706@gmail.com> Faisal Moledina wrote: > Hey everyone, > > I have a general issue that I'd like to discuss. I'm using Python to > run a numerical simulation where at each time step, I run a number of > operations and store the results before moving to the next timestep. What do you do with the results after the simulation run? How precise do the numbers have to be? -- Bob Gailer Chapel Hill NC 919-636-4239 From kent37 at tds.net Tue Dec 1 04:00:01 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 30 Nov 2009 22:00:01 -0500 Subject: [Tutor] python closures In-Reply-To: <4B14686D.3000003@ieee.org> References: <20091130112445.7451eaf6@o> <4B14686D.3000003@ieee.org> Message-ID: <1c2a2c590911301900v78595472ga6182805116a3122@mail.gmail.com> On Mon, Nov 30, 2009 at 7:50 PM, Dave Angel wrote: > And this > example could be made more complex if outer() is a generator, in which case > it may not have actually ended when inner gets called. Indeed. In [8]: def gen(): ...: for i in range(5): ...: def inner(): ...: print i ...: yield inner In [9]: g = gen() In [10]: outer = g.next() In [11]: outer() 0 In [12]: g.next() In [13]: outer() 1 Kent From kent37 at tds.net Tue Dec 1 04:03:06 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 30 Nov 2009 22:03:06 -0500 Subject: [Tutor] read in ascii and plot In-Reply-To: <333efb450911301726jb6d1ab5l9606566836b74442@mail.gmail.com> References: <333efb450911301726jb6d1ab5l9606566836b74442@mail.gmail.com> Message-ID: <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> On Mon, Nov 30, 2009 at 8:26 PM, Wayne Werner wrote: > A sample of the data is always helpful, but I'll take a shot in the dark. > If you have data like this: > 2.31 ? ? 72 > 98 ? ? ? ?23 > ... ? ? ? ? .... > 34 ? ? ? ?7.32 > And those are x y pairs you could do something like this: > f = open('input.txt') > #List comprehension to read all the lines as [[x1, y1], [x2, y2], ... [xn, > yn]] > data = [line.split() for line in f] You have to convert the text strings to float somewhere, for example data = [ map(float, line.split()) for line in f ] > # Reorient values as [(x1, x2,... xn), (y1, y2, ... yn)] > data = zip(*data) > # plot the xy vals > pylab.scatter(data[0], data[1]) Or, IMO a little clearer, x, y = zip(*data) pylab.scatter(x, y) Kent From emailkgnow at gmail.com Tue Dec 1 05:07:07 2009 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Tue, 1 Dec 2009 07:07:07 +0300 Subject: [Tutor] [Errno 9] Bad file descriptor Message-ID: Hi everybody, I'm running python 2.6.1 on vista and I'm trying to use the csv module to write to a csv file and get the average of some numbers, but I keep getting the following error: Traceback (most recent call last): File "C:\Python31\MyCSVProjectFinal.py", line 83, in writer.writerow(headings) IOError: [Errno 9] Bad file descriptor line 83 refers to the following code, specifically to the one in capital (in the actual code it's not in capital by the way): headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) if csvOutFileName is not None: print "Saving using: "+csvOutFileName csvOut = file(csvOutFileName, 'rb') writer = csv.writer(csvOut) * WRITER.WRITEROW(HEADINGS)* for index in range(len(measured1)): writer.writerow([measured1[index], measured2[index]]) writer.writerow([averaged1, averaged2]) else: print "No filename for saving" so, my problem is I don't know why it keeps giving me this error. I've checked on the internet, but I haven't found anything to help resolve this error. I hope you can be of help. PS: I've added the complete code below for reference. thanks import csv import sys import easygui def getFileAndPath(): "Get fully-qualified path to the csv file" # TODO argInitialFile = '*.csv' fileAndPath = easygui.fileopenbox(title="Select .CSV file") print "Using:",fileAndPath return fileAndPath def getLinesInCSVFile(fileAndPath): "read lines in CSV file, return a list of these lines" linesInCSV = [] reader = csv.reader(open(fileAndPath, "rb")) for row in reader: linesInCSV.append(row) return linesInCSV def justNumbers(listOfStrings): "True if the list contains just numbers represented as strings" # e.g. ['22.4', '23.9'] isJustNumbers = True for item in listOfStrings: try: nbr = float(item) except ValueError: isJustNumbers = False return isJustNumbers def getNumbers(listOfStrings): "Convert a list of strings-of-numbers to a list of numbers, e.g. ['22.4', '23.9'] -> [22.4, 23.9]" numbers = [] for item in listOfStrings: nbr = float(item) numbers.append(nbr) return numbers def average(values): """Computes the arithmetic mean of a list of numbers""" return sum(values, 0.0) / len(values) if __name__ == "__main__": # get the file-name #fileAndPath = getFileAndPath() # NOTE quick hack to make our test/development process quicker fileAndPath = "c:\\testing\\measured2.csv" # read the CSV file linesInCSV = getLinesInCSVFile(fileAndPath) measured1 = [] measured2 = [] for n in range(1,4): line = linesInCSV[n] isJustNumbers = justNumbers(line) if not isJustNumbers: print "ERROR! Expected a line of numbers, instead we got:",line sys.exit() # we only get here if justNumbers reports that we only have numbers # so we can extract the list of floating-point numbers numbers = getNumbers(line) measured1.append(numbers[0]) measured2.append(numbers[1]) averaged1 = average(measured1) averaged2 = average(measured2) # Show values of Measured1 in a choicebox # We don't care about the choices, this is just for output #easygui.choicebox(message = "Sorted values in Measured1", title = "Measured1", choices = measured1) headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] csvOutFileName = easygui.filesavebox(title = "Choose output file for averages", ) if csvOutFileName is not None: print "Saving using: "+csvOutFileName csvOut = file(csvOutFileName, 'rb') writer = csv.writer(csvOut) writer.writerow(headings) for index in range(len(measured1)): writer.writerow([measured1[index], measured2[index]]) writer.writerow([averaged1, averaged2]) else: print "No filename for saving" -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Tue Dec 1 05:49:30 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Tue, 1 Dec 2009 10:19:30 +0530 Subject: [Tutor] does poplib have proxy support ? Message-ID: Does poplib/imaplib have proxy support like urllib? I was unable to get connected to my mail. Here is what i tried : >>> proxies = {'http': 'http://username:password at proxy:8080'} >>> host = 'pop.gmail.com' >>> me = 'username at gmail.com' >>> pass = '******' >>> pop = poplib.POP3_SSL(host) Traceback (most recent call last): File "", line 1, in TypeError: __init__() got an unexpected keyword argument 'proxies' >>> pop = poplib.POP3_SSL(host) Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/poplib.py", line 357, in __init__ raise socket.error, msg socket.error: (60, 'Operation timed out') -------------- next part -------------- An HTML attachment was scrubbed... URL: From bibsmendez at gmail.com Tue Dec 1 06:46:40 2009 From: bibsmendez at gmail.com (bibi midi) Date: Tue, 1 Dec 2009 08:46:40 +0300 Subject: [Tutor] Equivalent exception of os.path.exists() In-Reply-To: References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o> <4B142564.4020600@gmail.com> Message-ID: Thanks to all of you that replied. Your inputs are very helpful and informative. On Tue, Dec 1, 2009 at 12:14 AM, Sander Sweers wrote: > > There is no exception to alert you a file already exists. Depending on > how you open the file (mode) it will either read, write or append to > the file. If the file exists and you open it in write mode it *will* > overwrite the file. See [1] for more info. > > So what you need to do is check yourself if the file exists and make > the python script take appriate action. For example: > > import os > fname = "C:\\testfile.txt" > if os.path.exists(fname): > do something if exists > else: > do something else if not exists > > Greets > Sander > Like i thought so, there is no exception to catch if a file already exist. I've been browsing the many types of exceptions and cant find anything thats close. Thank you for clarifying. I may have misunderstood the exercise question and will have to return to it to do another approach. For the record below is the working copy of the script. Here i understand how the os.path.exists() method works. while True: fname = raw_input('please enter filename: ') if os.path.exists(fname): print('ERROR: %s exists!') % fname elif fname.isspace(): print('Space is not allowed!') else: print('you entered %s') % fname break all = [] # container list to hold user input lines print "\nPopulate your file! \n" quit_prompt = "[to quit enter a dot '.' by itself]--> " while True: entry = raw_input(quit_prompt) if entry == '.': break else: all.append(entry) confirm = raw_input('save file to disk?(y/N)') confirm = confirm.lower() #convert to lowercase if confirm == 'y': fobj = open(fname, 'w') fobj.write('\n'.join(all)) fobj.close() print('DONE! open %s to view your file') % fname else: print 'not saving to disk!' sys.exit() Thank you. -- Best Regards, bibimidi -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Tue Dec 1 09:01:56 2009 From: denis.spir at free.fr (spir) Date: Tue, 1 Dec 2009 09:01:56 +0100 Subject: [Tutor] Equivalent exception of os.path.exists() In-Reply-To: References: <4B13C1D3.6060305@gmail.com> <20091130180818.10edde94@o> <4B142564.4020600@gmail.com> Message-ID: <20091201090156.0d08669a@o> bibi midi dixit: > Like i thought so, there is no exception to catch if a file already exist. > I've been browsing the many types of exceptions and cant find anything thats > close. Thank you for clarifying. > Actually, you can. I guess it's not the book author's intent, but you can try it for the sake of experiment. If you're working under a unix-like system, just try to write into another user's home dir (*), or any file outside your own home. The filesystem will refuse the permission, so you will get an error from python. In fact, in this case, you'll get an error both for creation and change. I don't know the equivalent under other OSes, but it sure exists. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Tue Dec 1 08:42:47 2009 From: denis.spir at free.fr (spir) Date: Tue, 1 Dec 2009 08:42:47 +0100 Subject: [Tutor] python closures In-Reply-To: <4B14686D.3000003@ieee.org> References: <20091130112445.7451eaf6@o> <4B14686D.3000003@ieee.org> Message-ID: <20091201084247.25647cfb@o> Dave Angel dixit: > Maybe a more complex example might show the various linkages. > > glob = 42 > > def outer(parm1): > free = 12 > free3 = 19 > def inner(parm2, parm3=free3): > print "global", glob, ", free vars", parm1, free, free3, ", > locals", parm2, parm3 > free = 49 > free3 = 48 > return inner > > newfunc = outer(10) > newfunc(45) > > > produces output: > global 42 , free vars 10 49 48 , locals 45 19 > > So when the inner() function is actually called, glob is just a global. > parm1, fre, and free3 hold the values they ended up with when outer() > returned, and local parm2 is passed by top-level code, while local parm3 > gets its default value assigned when "def inner(...) was executed. > > Notice that the free variables free, free3, and parm1 are referring to > the function's ending state, not to the state when the function was > defined. This has an impact when you've got inner being defined in a > loop. And this example could be made more complex if outer() is a > generator, in which case it may not have actually ended when inner gets > called. > > HTH > DaveA Great example, thank you. By the way, do you know the idiom: def makeInc(start): def inc(): inc.n += 1 print inc.n inc.n = start # 'start' may change now # ... return inc inc= makeInc(start=3) inc() I find it much nicer than a pseudo default value, for it explicitely shows that 'n' is, conceptually speaking, an attribute of the func (read: a closure upvalue). Let's take advantage of the fact python funcs are real objects! Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Tue Dec 1 09:28:49 2009 From: denis.spir at free.fr (spir) Date: Tue, 1 Dec 2009 09:28:49 +0100 Subject: [Tutor] python closures In-Reply-To: <4B14686D.3000003@ieee.org> References: <20091130112445.7451eaf6@o> <4B14686D.3000003@ieee.org> Message-ID: <20091201092849.396f6182@o> May I suggest that upvalues are analog to parameters passed by name? (which is indeed not Python's paradigm) Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From hugo.yoshi at gmail.com Tue Dec 1 09:36:53 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 1 Dec 2009 09:36:53 +0100 Subject: [Tutor] python closures In-Reply-To: <20091201084247.25647cfb@o> References: <20091130112445.7451eaf6@o> <4B14686D.3000003@ieee.org> <20091201084247.25647cfb@o> Message-ID: <29179d160912010036p6f52feb5jda68442860572c4b@mail.gmail.com> On Tue, Dec 1, 2009 at 8:42 AM, spir wrote: > Great example, thank you. > > By the way, do you know the idiom: > > def makeInc(start): > ? def inc(): > ? ? ?inc.n += 1 > ? ? ?print inc.n > ? inc.n = start > ? # 'start' may change now > ? # ... > ? return inc > > inc= makeInc(start=3) > inc() > > I find it much nicer than a pseudo default value, for it explicitely shows that 'n' is, conceptually speaking, an attribute of the func (read: a closure upvalue). Let's take advantage of the fact python funcs are real objects! Well, if you need an attribute maintained between calls like that I think a generator is much nicer to write: def inc(start): while True: yield start start += 1 >>> i = inc(3) >>> i.next() 3 >>> i.next() 4 There might be a use-case where function attributes fit better, can't think of one right now though. Hugo From davea at ieee.org Tue Dec 1 09:47:10 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 01 Dec 2009 03:47:10 -0500 Subject: [Tutor] [Errno 9] Bad file descriptor In-Reply-To: References: Message-ID: <4B14D80E.9050905@ieee.org> Khalid Al-Ghamdi wrote: > Hi everybody, > > I'm running python 2.6.1 on vista and I'm trying to use the csv module to > write to a csv file and get the average of some numbers, but I keep getting > the following error: > > Traceback (most recent call last): > File "C:\Python31\MyCSVProjectFinal.py", line 83, in > writer.writerow(headings) > IOError: [Errno 9] Bad file descriptor > > line 83 refers to the following code, specifically to the one in capital (in > the actual code it's not in capital by the way): > > headings = linesInCSV[0] # e.g. ['Measured1', 'Measured2'] > csvOutFileName = easygui.filesavebox(title = "Choose output file for > averages", ) > if csvOutFileName is not None: > print "Saving using: "+csvOutFileName > csvOut = file(csvOutFileName, 'rb') > If this is an output file, why would you use 'rb' as the file mode? Don't you mean 'wb' ? > writer = csv.writer(csvOut) > * WRITER.WRITEROW(HEADINGS)* > for index in range(len(measured1)): > writer.writerow([measured1[index], measured2[index]]) > writer.writerow([averaged1, averaged2]) > else: > print "No filename for saving" > > > so, my problem is I don't know why it keeps giving me this error. I've > checked on the internet, but I haven't found anything to help resolve this > error. > > > From faisal.moledina at gmail.com Tue Dec 1 17:48:34 2009 From: faisal.moledina at gmail.com (Faisal Moledina) Date: Tue, 1 Dec 2009 11:48:34 -0500 Subject: [Tutor] numerical simulation + SQLite In-Reply-To: <4B1475E1.9060706@gmail.com> References: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> <4B1475E1.9060706@gmail.com> Message-ID: <946A027A-9664-46DC-BEDD-7285344C9305@gmail.com> Thanks everyone for your responses! Alan Gauld wrote: > You may need to be realistic in your expectations. > A database is writing to disk which will be slower than working in memory. And a 3GB file takes a while to read/traverse, even with indexes. It depends a lot on exactly what you are doing. If its mainly writing it should not be much slower than writing to a flat file. If you are doing a lot of reading - and you have used indexes - then it should be a lot faster than a file. > > But RAM - if you have enough - will always be fastest, by about 100 times. > The problem is when you run out, you revert to using files and that's usually slower than a database... > > But without details of your usage pattern and database schema and SQL code etc it is, as you say, impossible to be specific. I'm running a stochastic simulation of Brownian motion of a number of particles, for which I'll present a simplified version here. At each time step, I determine if some particles have left the system, determine the next position of the remaining particles, and then introduce new particles into the system at defined starting points. I have two tables in my SQLite database: one for information on each particle and one for all the x y z locations for each particle. sqlite> .schema Particles CREATE TABLE Particles (part_id INTEGER PRIMARY KEY, origin INTEGER, endpoint INTEGER, status TEXT, starttime REAL, x REAL, y REAL, z REAL); sqlite> .schema Locations CREATE TABLE Locations (id INTEGER PRIMARY KEY AUTOINCREMENT, timepoint REAL, part_id INTEGER, x REAL, y REAL, z REAL); For particles that have left the system, I create a list of part_id values whose status I'd like to update in the database and issue a command within my script (for which db=sqlite3.connect('results.db')): db.executemany("UPDATE Particles SET status='left' WHERE part_id=?",part_id) db.commit() To update the position, something like: db.executemany("UPDATE Particles SET x=?,y=?,z=? WHERE part_id=?",Particle_entries) db.executemany("INSERT INTO Locations (timepoint,lig,x,y,z) VALUES (?,?,?,?,?)",Location_entries) db.commit() That's about it. Just for many particles (i.e. 1e4 to 1e5). I'm considering whether I need every location entry or if I could get away with every 10 location entries, for example. Eike Welk wrote: > Just in case you don't know it, maybe Pytables is the right solution > for you. It is a disk storage library specially for scientific > applications: > http://www.pytables.org/moin Wow, that looks pretty good. I work with a lot of numpy.array's in this simulation so I'll definitely look into that. bob gailer wrote: > What do you do with the results after the simulation run? > > How precise do the numbers have to be? I'm interested in the particles that have left the system (I actually have a few ways they can leave) and I'm also interested in the ensemble average of the trajectories. As far as precision is concerned, I'm working on the scale of ?m and each movement is on the order of 0.1 to 10 ?m. Faisal From questions.anon at gmail.com Tue Dec 1 18:54:42 2009 From: questions.anon at gmail.com (questions anon) Date: Tue, 1 Dec 2009 09:54:42 -0800 Subject: [Tutor] read in ascii and plot In-Reply-To: <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> References: <333efb450911301726jb6d1ab5l9606566836b74442@mail.gmail.com> <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> Message-ID: Excellent thank you!! On Mon, Nov 30, 2009 at 7:03 PM, Kent Johnson wrote: > On Mon, Nov 30, 2009 at 8:26 PM, Wayne Werner > wrote: > > > A sample of the data is always helpful, but I'll take a shot in the dark. > > If you have data like this: > > 2.31 72 > > 98 23 > > ... .... > > 34 7.32 > > And those are x y pairs you could do something like this: > > f = open('input.txt') > > #List comprehension to read all the lines as [[x1, y1], [x2, y2], ... > [xn, > > yn]] > > data = [line.split() for line in f] > > You have to convert the text strings to float somewhere, for example > data = [ map(float, line.split()) for line in f ] > > > # Reorient values as [(x1, x2,... xn), (y1, y2, ... yn)] > > data = zip(*data) > > # plot the xy vals > > pylab.scatter(data[0], data[1]) > > Or, IMO a little clearer, > x, y = zip(*data) > pylab.scatter(x, y) > > Kent > -------------- next part -------------- An HTML attachment was scrubbed... URL: From questions.anon at gmail.com Tue Dec 1 20:15:38 2009 From: questions.anon at gmail.com (questions anon) Date: Tue, 1 Dec 2009 11:15:38 -0800 Subject: [Tutor] read in ascii and plot In-Reply-To: <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> References: <333efb450911301726jb6d1ab5l9606566836b74442@mail.gmail.com> <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> Message-ID: I would now like to add a line of best fit. I think the command is polyfit()?? But I can't seem to get it to work f=open('e:/testscatter.txt') data=[map(float,line.split()) for line in f] x, y=zip(*data) pylab.polyfit(x,y,1) pylab.scatter(x,y) pylab.show() Any feedback will be greatly appreciated. On Mon, Nov 30, 2009 at 7:03 PM, Kent Johnson wrote: > On Mon, Nov 30, 2009 at 8:26 PM, Wayne Werner > wrote: > > > A sample of the data is always helpful, but I'll take a shot in the dark. > > If you have data like this: > > 2.31 72 > > 98 23 > > ... .... > > 34 7.32 > > And those are x y pairs you could do something like this: > > f = open('input.txt') > > #List comprehension to read all the lines as [[x1, y1], [x2, y2], ... > [xn, > > yn]] > > data = [line.split() for line in f] > > You have to convert the text strings to float somewhere, for example > data = [ map(float, line.split()) for line in f ] > > > # Reorient values as [(x1, x2,... xn), (y1, y2, ... yn)] > > data = zip(*data) > > # plot the xy vals > > pylab.scatter(data[0], data[1]) > > Or, IMO a little clearer, > x, y = zip(*data) > pylab.scatter(x, y) > > Kent > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sanelson at gmail.com Wed Dec 2 01:27:23 2009 From: sanelson at gmail.com (Stephen Nelson-Smith) Date: Wed, 2 Dec 2009 00:27:23 +0000 Subject: [Tutor] Monitoring a logfile Message-ID: Varnish has a dedicated (but not always) reliable logger service. I'd like to monitor the logs - specifically I want to check that a known entry appears in there every minute (it should be there about 10 times a minute). What's going to be the best way to carry out this kind of check? I had a look at SEC, but it looks horrifically complicated. Could someone point me in the right direction? I think I basically want to be able to check the logfile every minute and check that an entry is in there since the last time I met.... I just can't see the right way to get started. S. From amonroe at columbus.rr.com Wed Dec 2 03:31:42 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 1 Dec 2009 21:31:42 -0500 Subject: [Tutor] Monitoring a logfile In-Reply-To: References: Message-ID: <5177631039.20091201213142@columbus.rr.com> > Varnish has a dedicated (but not always) reliable logger service. I'd > like to monitor the logs - specifically I want to check that a known > entry appears in there every minute (it should be there about 10 times > a minute). > What's going to be the best way to carry out this kind of check? I > had a look at SEC, but it looks horrifically complicated. Ever used the seek() and tell() and readline() methods of a file object? You could probably hack something together pretty quickly with those. Alan From eike.welk at gmx.net Wed Dec 2 09:49:53 2009 From: eike.welk at gmx.net (Eike Welk) Date: Wed, 02 Dec 2009 09:49:53 +0100 Subject: [Tutor] Polyfit works like this In-Reply-To: References: <1c2a2c590911301903v34884c3am85c25a23905034bc@mail.gmail.com> Message-ID: <200912020949.53867.eike.welk@gmx.net> On Tuesday 01 December 2009, questions anon wrote: > I would now like to add a line of best fit. I think the command is > polyfit()?? > But I can't seem to get it to work These are the steps to make polyval work. I typed it into an 'ipython -pylab' session; I hope I included all relevant lines and I removed some of the output: #this is the input data In [13]:d_x = [0, 0.1, 0.3, 0.5, 0.7, 1, 1.2, 1.5, 1.8, 2] In [14]:d_y = [0.1, 0.11, 0.15, 0.3, 0.5, 0.8, 1, 1.2, 1.1, 1.2] #show input data In [15]:plot(d_x,d_y, label='data') #make 2nd degree polynom, and plot smoothly with 50 points In [17]:pol2 = polyfit(d_x, d_y, 2) In [18]:p_x = linspace(0, 2, 50) In [19]:p2_y = polyval(pol2, p_x) In [20]:plot(p_x,p2_y, label='pol 2nd') #also create line and plot it too (would not need 50 points) In [21]:pol1 = polyfit(d_x, d_y, 1) In [22]:p1_y = polyval(pol1, p_x) In [24]:plot(p_x,p1_y, label='line') #create legend so you can recognize the curves In [25]:legend() Eike. From kent37 at tds.net Wed Dec 2 13:29:10 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 2 Dec 2009 07:29:10 -0500 Subject: [Tutor] Fwd: Moving from Python 2 to Python 3: A 4 page "cheat sheet" Message-ID: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> Forwarded from python-announce. A helpful summary of the differences between Python 2 and 3 (though some of the differences were introduced well before Python 3). Kent ---------- Forwarded message ---------- From:?Mark Summerfield To:?comp-lang-python-announce at moderators.isc.org Date:?Tue, 1 Dec 2009 06:05:09 -0800 (PST) Subject:?Moving from Python 2 to Python 3: A 4 page "cheat sheet" I've produced a 4 page document that provides a very concise summary of Python 2<->3 differences plus the most commonly used new Python 3 features. It is aimed at existing Python 2 programmers who want to start writing Python 3 programs and want to use Python 3 idioms rather than those from Python 2 where the idioms differ. It uses Python 3.1 syntax since that looks like being the standard for a few years in view of the language moratorium. The document is U.S. Letter size but will also print fine on A4 printers. It is available as a free PDF download (no registration or anything) from InformIT's website. Here's the direct link: http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf And of course, if you want more on Python 3, there's always the documentation---or my book:-) "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561. -- Mark Summerfield, www.qtrac.eu From alan.gauld at btinternet.com Wed Dec 2 14:56:05 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 2 Dec 2009 13:56:05 -0000 Subject: [Tutor] Moving from Python 2 to Python 3: A 4 page "cheatsheet" References: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> Message-ID: "Kent Johnson" wrote > Forwarded from python-announce. A helpful summary of the differences > between Python 2 and 3 (though some of the differences were introduced > well before Python 3). > It is available as a free PDF download (no registration or anything) > from InformIT's website. Here's the direct link: > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf It didn't work for me I always got forwarded to the Book "home page" on InformIT Alan G From skylarstruble at gmail.com Wed Dec 2 15:38:53 2009 From: skylarstruble at gmail.com (Skylar Struble) Date: Wed, 2 Dec 2009 09:38:53 -0500 Subject: [Tutor] pygame help with livewires Message-ID: <664a3e4c0912020638i69a8aabfofb4ea44b59169ab6@mail.gmail.com> ok so its working and drops a normal cow and randomly a green cow but i want it to be able to tell when you collide with the green cow cause its gonna be a powerup. heres the code for my program. # Pizza Panic # Player must catch falling pizzas before they hit the ground from livewires import games, color import random hscorefile = open("hscores.txt", 'r') score = 0 level = 1 pizzascore = 2 stl = 15 lstl = 15 pizza_speed = 1 cook_speed = 4 pizza_drop = 0.5 level_up_drop = 1 highscore = int(hscorefile.read(100000000)) hscorefile.close() games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite): """ A pan controlled by player to catch falling pizzas. """ global score image = games.load_image("pan.bmp") def __init__(self): """ Initialize Pan object and create Text object for score. """ global level global hs global score super(Pan, self).__init__(image = Pan.image, x = games.mouse.x, bottom = games.screen.height) self.score = games.Text(value = 0, size = 25, color = color.black, top = 5, right = games.screen.width - 10) games.screen.add(self.score) self.leveltxt = games.Text(value = "level:", size = 25, color = color.black, top = 5, right = games.screen.width - 580) games.screen.add(self.leveltxt) self.level = games.Text(value = level, size = 25, color = color.black, top = 5, right = games.screen.width - 566) games.screen.add(self.level) self.hs = games.Text(value = "Highscore:" + str(highscore), size = 25, color = color.black, top = 5, right = games.screen.width - 320) games.screen.add(self.hs) def update(self): """ Move to mouse x position. """ self.x = games.mouse.x if self.left < 0: self.left = 0 if self.right > games.screen.width: self.right = games.screen.width self.check_catch() def check_catch(self): """ Check if catch pizzas. """ global pizza_drop global level global score global score_to_level global cook_speed global pizza_speed global stl global lstl global pizzascore stl = (lstl *2.5) for pizza in self.overlapping_sprites: self.score.value = self.score.value + pizzascore score = self.score.value if self.score.value >= stl: lstl = stl pizza_drop += 0.2 self.level.value += 1 pizza_speed += .25 pizzascore = pizzascore * 2 cook_speed += 5 level = level + 1 lvlup_message = games.Message(value = "Level " + str(level), size = 90, color = color.red, x = games.screen.width/2, y = games.screen.height/2, lifetime = 2 * games.screen.fps) games.screen.add(lvlup_message) self.score.right = games.screen.width - 10 pizza.handle_caught() class Pizza(games.Sprite): """ A pizza which falls to the ground. """ global pizza_speed speed = 1.5 image = games.load_image("pizza.bmp") def __init__(self, x, y = 90): """ Initialize a Pizza object. """ super(Pizza, self).__init__(image = Pizza.image, x = x, y = y, dy = pizza_speed) def update(self): """ Check if bottom edge has reached screen bottom. """ if self.bottom > games.screen.height: self.end_game() self.destroy() def handle_caught(self): """ Destroy self if caught. """ self.destroy() def end_game(self): global highscore global score global name """ End the game. """ if score > highscore: hsfile = open("hscores.txt", "w") hsfile.write(str(score)) hsfile.close() hs_message = games.Message(value ="New highscore of " + str(score), size = 45, color = color.red, x = games.screen.width/2, y = games.screen.height/2, lifetime = 1 * games.screen.fps, after_death = games.screen.quit) games.screen.add(hs_message) else: end_message = games.Message(value = "game over", size = 45, color = color.red, x = games.screen.width/2, y = games.screen.height/2, lifetime = 0 * games.screen.fps, after_death = games.screen.quit) games.screen.add(end_message) class power_pizza1(games.Sprite): """ A pizza which falls to the ground. """ global pizza_speed speed = 1.5 image = games.load_image("power_pizza1.bmp") def __init__(self, x, y = 90): """ Initialize a Pizza object. """ super(power_pizza1, self).__init__(image = power_pizza1.image, x = x, y = y, dy = pizza_speed) def update(self): """ Check if bottom edge has reached screen bottom. """ if self.bottom > games.screen.height: self.destroy() def handle_caught(self): """ Destroy self if caught. """ self.destroy() class Chef(games.Sprite): """ A chef which moves left and right, dropping pizzas. """ global cook_speed image = games.load_image("chef.bmp") def __init__(self, y = 55, speed = 1000, odds_change = 55): """ Initialize the Chef object. """ super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = y, dx = cook_speed) self.odds_change = odds_change self.time_til_drop = 0 def update(self): """ Determine if direction needs to be reversed. """ if self.left < 0 or self.right > games.screen.width: self.dx = -self.dx elif random.randrange(self.odds_change) == 0: self.dx = -self.dx self.check_drop() def check_drop(self): rpp = 5 """ Decrease countdown or drop pizza and reset countdown. """ global pizza_drop if self.time_til_drop > 0: self.time_til_drop -= pizza_drop else: new_pizza = Pizza(x = random.randrange(40, 600)) rpp = random.randrange(1, 5) if rpp == 3: power_pizza = power_pizza1 (x = random.randrange(40, 600)) games.screen.add(power_pizza) else: games.screen.add(new_pizza) # set buffer to approx 30% of pizza height, regardless of pizza speed self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 def main(): """ Play the game. """ wall_image = games.load_image("wall.jpg", transparent = False) games.screen.background = wall_image the_chef = Chef() games.screen.add(the_chef) the_pan = Pan() games.screen.add(the_pan) games.mouse.is_visible = False games.screen.event_grab = True games.screen.mainloop() # start it up! main() From kent37 at tds.net Wed Dec 2 16:13:25 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 2 Dec 2009 10:13:25 -0500 Subject: [Tutor] Moving from Python 2 to Python 3: A 4 page "cheatsheet" In-Reply-To: References: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> Message-ID: <1c2a2c590912020713l72b85952ka4d5b8f3d1e2ad7e@mail.gmail.com> On Wed, Dec 2, 2009 at 8:56 AM, Alan Gauld wrote: > > "Kent Johnson" wrote > >> Forwarded from python-announce. A helpful summary of the differences >> between Python 2 and 3 (though some of the differences were introduced >> well before Python 3). > >> It is available as a free PDF download (no registration or anything) >> from InformIT's website. Here's the direct link: >> >> http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf > > > It didn't work for me I always got forwarded to the Book "home page" on > InformIT Strange. Worked for me yesterday and again just now. Kent From joewoe at fsmail.de Wed Dec 2 15:13:20 2009 From: joewoe at fsmail.de (Joerg Woelke) Date: Wed, 2 Dec 2009 15:13:20 +0100 Subject: [Tutor] Moving from Python 2 to Python 3: A 4 page "cheatsheet" In-Reply-To: References: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> Message-ID: <20091202141320.GC9158@localhost> * Alan Gauld [091202 15:07]: > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf > > > It didn't work for me I always got forwarded to the Book "home page" > on InformIT Worked for me with wget(1). -- You are capable of planning your future. From waynejwerner at gmail.com Wed Dec 2 16:26:08 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Wed, 2 Dec 2009 09:26:08 -0600 Subject: [Tutor] Moving from Python 2 to Python 3: A 4 page "cheatsheet" In-Reply-To: <20091202141320.GC9158@localhost> References: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> <20091202141320.GC9158@localhost> Message-ID: <333efb450912020726x22cffc60q6b911387dc13a2ae@mail.gmail.com> On Wed, Dec 2, 2009 at 8:13 AM, Joerg Woelke wrote: > Alan Gauld [091202 15:07]: > > > > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf > > > > > > It didn't work for me I always got forwarded to the Book "home page" > > on InformIT > Worked for me with wget(1). > And for me with Google Chrome on Ubuntu... -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From eddie9139 at gmail.com Wed Dec 2 18:46:19 2009 From: eddie9139 at gmail.com (Eddie) Date: Thu, 3 Dec 2009 04:46:19 +1100 Subject: [Tutor] Moving from Python 2 to Python 3: A 4 page "cheatsheet" In-Reply-To: <333efb450912020726x22cffc60q6b911387dc13a2ae@mail.gmail.com> References: <1c2a2c590912020429k247b4fe1gca44c5b9839c239e@mail.gmail.com> <20091202141320.GC9158@localhost> <333efb450912020726x22cffc60q6b911387dc13a2ae@mail.gmail.com> Message-ID: Thanks 2009/12/3 Wayne Werner > > > On Wed, Dec 2, 2009 at 8:13 AM, Joerg Woelke wrote: > >> Alan Gauld [091202 15:07]: >> > > >> http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf >> > >> > >> > It didn't work for me I always got forwarded to the Book "home page" >> > on InformIT >> Worked for me with wget(1). >> > > > And for me with Google Chrome on Ubuntu... > -Wayne > -- > To be considered stupid and to be told so is more painful than being called > gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, > every vice, has found its defenders, its rhetoric, its ennoblement and > exaltation, but stupidity hasn?t. - Primo Levi > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Wed Dec 2 19:08:03 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 02 Dec 2009 13:08:03 -0500 Subject: [Tutor] working with bit arrays Message-ID: <1259777283.2855.15.camel@bermanrl-desktop> Hi, I am trying to represent a number as a list of bits: for example the bit representation of the integer 8. I did find a number of articles pertaining to a module called bitarray but I was unable to download/install that package. I am using Linux on Ubuntu 9.10; Python 2.6.2. I am almost certain there is a relatively easy way to convert an integer that can be represented by 32 bits into an array of bits that I can iterate over looking for switched on bits or switched off bits. Any information such as recipes or past articles in this list providing methods to create and manipulate bit arrays would be most appreciated. Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Wed Dec 2 19:48:00 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Wed, 2 Dec 2009 12:48:00 -0600 Subject: [Tutor] working with bit arrays In-Reply-To: <1259777283.2855.15.camel@bermanrl-desktop> References: <1259777283.2855.15.camel@bermanrl-desktop> Message-ID: <333efb450912021048l10338aebr694d5e341761f93d@mail.gmail.com> On Wed, Dec 2, 2009 at 12:08 PM, Robert Berman wrote: > Hi, > > I am trying to represent a number as a list of bits: for example the bit > representation of the integer 8. I did find a number of articles pertaining > to a module called bitarray but I was unable to download/install that > package. I am using Linux on Ubuntu 9.10; Python 2.6.2. > > I am almost certain there is a relatively easy way to convert an integer > that can be represented by 32 bits into an array of bits that I can iterate > over looking for switched on bits or switched off bits. > > Any information such as recipes or past articles in this list providing > methods to create and manipulate bit arrays would be most appreciated. > Python 2.6+ (as far as I know) has the bin() function: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> bin(8) '0b1000' >>> And larger: >>> bin(99999999999999999999999) '0b10101001011010000001011000111111000010100101011110110011111111111111111111111' You can convert them to integers to use ^ (XOR) or & (AND) and other binary operations on them: http://docs.python.org/reference/expressions.html#binary-bitwise-operations >>> a = int('01001', 2) >>> b = int('00001', 2) >>> a & b == b True >>> a = int('01110', 2) >>> a & b == b False There may be some other way to check, but that's probably the easiest I know of. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Wed Dec 2 20:03:33 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 02 Dec 2009 14:03:33 -0500 Subject: [Tutor] working with bit arrays In-Reply-To: <333efb450912021048l10338aebr694d5e341761f93d@mail.gmail.com> References: <1259777283.2855.15.camel@bermanrl-desktop> <333efb450912021048l10338aebr694d5e341761f93d@mail.gmail.com> Message-ID: <1259780614.2855.16.camel@bermanrl-desktop> Wayne, Thank you very much. Robert On Wed, 2009-12-02 at 12:48 -0600, Wayne Werner wrote: > On Wed, Dec 2, 2009 at 12:08 PM, Robert Berman > wrote: > > Hi, > > I am trying to represent a number as a list of bits: for > example the bit representation of the integer 8. I did find a > number of articles pertaining to a module called bitarray but > I was unable to download/install that package. I am using > Linux on Ubuntu 9.10; Python 2.6.2. > > I am almost certain there is a relatively easy way to convert > an integer that can be represented by 32 bits into an array of > bits that I can iterate over looking for switched on bits or > switched off bits. > > Any information such as recipes or past articles in this list > providing methods to create and manipulate bit arrays would > be most appreciated. > > > > Python 2.6+ (as far as I know) has the bin() function: > > > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> bin(8) > '0b1000' > >>> > > > And larger: > > > >>> bin(99999999999999999999999) > '0b10101001011010000001011000111111000010100101011110110011111111111111111111111' > > > You can convert them to integers to use ^ (XOR) or & (AND) and other > binary operations on > them: http://docs.python.org/reference/expressions.html#binary-bitwise-operations > > > >>> a = int('01001', 2) > >>> b = int('00001', 2) > >>> a & b == b > True > >>> a = int('01110', 2) > >>> a & b == b > False > > > There may be some other way to check, but that's probably the easiest > I know of. > > > HTH, > Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From cfuller084 at thinkingplanet.net Wed Dec 2 19:51:29 2009 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Wed, 2 Dec 2009 12:51:29 -0600 Subject: [Tutor] working with bit arrays In-Reply-To: <1259777283.2855.15.camel@bermanrl-desktop> References: <1259777283.2855.15.camel@bermanrl-desktop> Message-ID: <200912021251.29364.cfuller084@thinkingplanet.net> My approach has been to store it as an array and then build the integer as needed. This code requires Python 2.5 or later. def bits2int(l): return sum([2**i if j else 0 for i,j in enumerate(l)]) To convert the other way: def int2bits(m, n): return [int(bool(m&(1<>= inc return i floating point is so messy and slow :) Cheers On Wednesday 02 December 2009 12:08, Robert Berman wrote: > Hi, > > I am trying to represent a number as a list of bits: for example the bit > representation of the integer 8. I did find a number of articles > pertaining to a module called bitarray but I was unable to > download/install that package. I am using Linux on Ubuntu 9.10; Python > 2.6.2. > > I am almost certain there is a relatively easy way to convert an integer > that can be represented by 32 bits into an array of bits that I can > iterate over looking for switched on bits or switched off bits. > > Any information such as recipes or past articles in this list providing > methods to create and manipulate bit arrays would be most appreciated. > > Robert From aivars868 at gmail.com Wed Dec 2 20:15:16 2009 From: aivars868 at gmail.com (aivars) Date: Wed, 2 Dec 2009 21:15:16 +0200 Subject: [Tutor] Tutor Digest, Vol 70, Issue 5 In-Reply-To: References: Message-ID: Mark, I have the first edition of your book. What is the difference between two editions? 2009/12/2 : > Send Tutor mailing list submissions to > ? ? ? ?tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > ? ? ? ?http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > ? ? ? ?tutor-request at python.org > > You can reach the person managing the list at > ? ? ? ?tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > ? 1. Fwd: Moving from Python 2 to Python 3: A 4 page "cheat ? ?sheet" > ? ? ?(Kent Johnson) > ? 2. Re: Moving from Python 2 to Python 3: A 4 page "cheatsheet" > ? ? ?(Alan Gauld) > ? 3. pygame help with livewires (Skylar Struble) > ? 4. Re: Moving from Python 2 to Python 3: A 4 page "cheatsheet" > ? ? ?(Kent Johnson) > ? 5. Re: Moving from Python 2 to Python 3: A 4 page "cheatsheet" > ? ? ?(Joerg Woelke) > ? 6. Re: Moving from Python 2 to Python 3: A 4 page "cheatsheet" > ? ? ?(Wayne Werner) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Wed, 2 Dec 2009 07:29:10 -0500 > From: Kent Johnson > To: "*tutor python" > Subject: [Tutor] Fwd: Moving from Python 2 to Python 3: A 4 page > ? ? ? ?"cheat ?sheet" > Message-ID: > ? ? ? ?<1c2a2c590912020429k247b4fe1gca44c5b9839c239e at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Forwarded from python-announce. A helpful summary of the differences > between Python 2 and 3 (though some of the differences were introduced > well before Python 3). > > Kent > > ---------- Forwarded message ---------- > From:?Mark Summerfield > To:?comp-lang-python-announce at moderators.isc.org > Date:?Tue, 1 Dec 2009 06:05:09 -0800 (PST) > Subject:?Moving from Python 2 to Python 3: A 4 page "cheat sheet" > I've produced a 4 page document that provides a very concise summary > of Python 2<->3 differences plus the most commonly used new Python 3 > features. It is aimed at existing Python 2 programmers who want to > start writing Python 3 programs and want to use Python 3 idioms rather > than those from Python 2 where the idioms differ. > > It uses Python 3.1 syntax since that looks like being the standard for > a few years in view of the language moratorium. > > The document is U.S. Letter size but will also print fine on A4 > printers. > > It is available as a free PDF download (no registration or anything) > from InformIT's website. Here's the direct link: > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf > > And of course, if you want more on Python 3, there's always the > documentation---or my book:-) > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561. > -- > Mark Summerfield, www.qtrac.eu > > > ------------------------------ > > Message: 2 > Date: Wed, 2 Dec 2009 13:56:05 -0000 > From: "Alan Gauld" > To: tutor at python.org > Subject: Re: [Tutor] Moving from Python 2 to Python 3: A 4 page > ? ? ? ?"cheatsheet" > Message-ID: > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > ? ? ? ?reply-type=original > > > "Kent Johnson" wrote > >> Forwarded from python-announce. A helpful summary of the differences >> between Python 2 and 3 (though some of the differences were introduced >> well before Python 3). > >> It is available as a free PDF download (no registration or anything) >> from InformIT's website. Here's the direct link: >> http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf > > > It didn't work for me I always got forwarded to the Book "home page" on > InformIT > > Alan G > > > > > ------------------------------ > > Message: 3 > Date: Wed, 2 Dec 2009 09:38:53 -0500 > From: Skylar Struble > To: tutor at python.org > Subject: [Tutor] pygame help with livewires > Message-ID: > ? ? ? ?<664a3e4c0912020638i69a8aabfofb4ea44b59169ab6 at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > ok so its working and drops a normal cow and randomly a green cow but > i want it to be able to tell when you collide with the green cow cause > its gonna be a powerup. > > heres the code for my program. > > # Pizza Panic > # Player must catch falling pizzas before they hit the ground > > from livewires import games, color > import random > hscorefile = open("hscores.txt", 'r') > score = 0 > level = 1 > pizzascore = 2 > stl = 15 > lstl = 15 > pizza_speed = 1 > cook_speed = 4 > pizza_drop = 0.5 > level_up_drop = 1 > highscore = int(hscorefile.read(100000000)) > hscorefile.close() > games.init(screen_width = 640, screen_height = 480, fps = 50) > > class Pan(games.Sprite): > ? ?""" > ? ?A pan controlled by player to catch falling pizzas. > ? ?""" > ? ?global score > ? ?image = games.load_image("pan.bmp") > > ? ?def __init__(self): > ? ? ? ?""" Initialize Pan object and create Text object for score. """ > ? ? ? ?global level > ? ? ? ?global hs > ? ? ? ?global score > ? ? ? ?super(Pan, self).__init__(image = Pan.image, x = > games.mouse.x, bottom = games.screen.height) > > ? ? ? ?self.score = games.Text(value = 0, size = 25, color = color.black, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? top = 5, right = games.screen.width - 10) > ? ? ? ?games.screen.add(self.score) > ? ? ? ?self.leveltxt = games.Text(value = "level:", size = 25, color > = color.black, > ? ? ? ? ? ? ? ? ? ? ? ?top = 5, right = games.screen.width - 580) > ? ? ? ?games.screen.add(self.leveltxt) > ? ? ? ?self.level = games.Text(value = level, size = 25, color = color.black, > ? ? ? ? ? ? ? ? ? ? ? ?top = 5, right = games.screen.width - 566) > ? ? ? ?games.screen.add(self.level) > ? ? ? ?self.hs = games.Text(value = "Highscore:" + str(highscore), > size = 25, color = color.black, > ? ? ? ? ? ? ? ?top = 5, right = games.screen.width - 320) > ? ? ? ?games.screen.add(self.hs) > > ? ?def update(self): > ? ? ? ?""" Move to mouse x position. """ > ? ? ? ?self.x = games.mouse.x > > ? ? ? ?if self.left < 0: > ? ? ? ? ? ?self.left = 0 > > ? ? ? ?if self.right > games.screen.width: > ? ? ? ? ? ?self.right = games.screen.width > > ? ? ? ?self.check_catch() > > ? ?def check_catch(self): > ? ? ? ?""" Check if catch pizzas. """ > ? ? ? ?global pizza_drop > ? ? ? ?global level > ? ? ? ?global score > ? ? ? ?global score_to_level > ? ? ? ?global cook_speed > ? ? ? ?global pizza_speed > ? ? ? ?global stl > ? ? ? ?global lstl > ? ? ? ?global pizzascore > ? ? ? ?stl = (lstl *2.5) > ? ? ? ?for pizza in self.overlapping_sprites: > ? ? ? ? ? ?self.score.value = self.score.value + pizzascore > ? ? ? ? ? ?score = self.score.value > ? ? ? ? ? ?if self.score.value >= stl: > ? ? ? ? ? ? ? ?lstl = stl > ? ? ? ? ? ? ? ?pizza_drop += 0.2 > ? ? ? ? ? ? ? ?self.level.value += 1 > ? ? ? ? ? ? ? ?pizza_speed += .25 > ? ? ? ? ? ? ? ?pizzascore = pizzascore * 2 > ? ? ? ? ? ? ? ?cook_speed += 5 > ? ? ? ? ? ? ? ?level = level + 1 > ? ? ? ? ? ? ? ?lvlup_message = games.Message(value = "Level " + str(level), > ? ? ? ? ? ? ? ? ? ? ? ? ? ?size = 90, > ? ? ? ? ? ? ? ? ? ? ? ? ? ?color = color.red, > ? ? ? ? ? ? ? ? ? ? ? ? ? ?x = games.screen.width/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ?y = games.screen.height/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ?lifetime = 2 * games.screen.fps) > ? ? ? ? ? ? ? ?games.screen.add(lvlup_message) > ? ? ? ? ? ?self.score.right = games.screen.width - 10 > ? ? ? ? ? ?pizza.handle_caught() > class Pizza(games.Sprite): > ? ?""" > ? ?A pizza which falls to the ground. > ? ?""" > ? ?global pizza_speed > ? ?speed = 1.5 > ? ?image = games.load_image("pizza.bmp") > > ? ?def __init__(self, x, y = 90): > ? ? ? ?""" Initialize a Pizza object. """ > ? ? ? ?super(Pizza, self).__init__(image = Pizza.image, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x = x, y = y, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?dy = pizza_speed) > > ? ?def update(self): > ? ? ? ?""" Check if bottom edge has reached screen bottom. """ > ? ? ? ?if self.bottom > games.screen.height: > ? ? ? ? ? ?self.end_game() > ? ? ? ? ? ?self.destroy() > > ? ?def handle_caught(self): > ? ? ? ?""" Destroy self if caught. """ > ? ? ? ?self.destroy() > > ? ?def end_game(self): > ? ? ? ?global highscore > ? ? ? ?global score > ? ? ? ?global name > ? ? ? ?""" End the game. """ > ? ? ? ?if score > highscore: > ? ? ? ? ? ?hsfile = open("hscores.txt", "w") > ? ? ? ? ? ?hsfile.write(str(score)) > ? ? ? ? ? ?hsfile.close() > ? ? ? ? ? ?hs_message = games.Message(value ="New highscore of " + str(score), > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?size = 45, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?color = color.red, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x = games.screen.width/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y = games.screen.height/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lifetime = 1 * games.screen.fps, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?after_death = games.screen.quit) > ? ? ? ? ? ?games.screen.add(hs_message) > ? ? ? ?else: > ? ? ? ? ? ?end_message = games.Message(value = "game over", > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?size = 45, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?color = color.red, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x = games.screen.width/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y = games.screen.height/2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lifetime = 0 * games.screen.fps, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?after_death = games.screen.quit) > ? ? ? ? ? ?games.screen.add(end_message) > > > class power_pizza1(games.Sprite): > ? ?""" > ? ?A pizza which falls to the ground. > ? ?""" > ? ?global pizza_speed > ? ?speed = 1.5 > ? ?image = games.load_image("power_pizza1.bmp") > > ? ?def __init__(self, x, y = 90): > ? ? ? ?""" Initialize a Pizza object. """ > ? ? ? ?super(power_pizza1, self).__init__(image = power_pizza1.image, > x = x, y = y, dy = pizza_speed) > > > ? ?def update(self): > ? ? ? ?""" Check if bottom edge has reached screen bottom. """ > ? ? ? ?if self.bottom > games.screen.height: > ? ? ? ? ? ?self.destroy() > > ? ?def handle_caught(self): > ? ? ? ?""" Destroy self if caught. """ > ? ? ? ?self.destroy() > class Chef(games.Sprite): > ? ?""" > ? ?A chef which moves left and right, dropping pizzas. > ? ?""" > ? ?global cook_speed > ? ?image = games.load_image("chef.bmp") > > ? ?def __init__(self, y = 55, speed = 1000, odds_change = 55): > ? ? ? ?""" Initialize the Chef object. """ > ? ? ? ?super(Chef, self).__init__(image = Chef.image, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x = games.screen.width / 2, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y = y, > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dx = cook_speed) > > ? ? ? ?self.odds_change = odds_change > ? ? ? ?self.time_til_drop = 0 > > > ? ?def update(self): > ? ? ? ?""" Determine if direction needs to be reversed. """ > ? ? ? ?if self.left < 0 or self.right > games.screen.width: > ? ? ? ? ? ?self.dx = -self.dx > ? ? ? ?elif random.randrange(self.odds_change) == 0: > ? ? ? ? ? self.dx = -self.dx > > ? ? ? ?self.check_drop() > > > ? ?def check_drop(self): > ? ? ? ?rpp = 5 > ? ? ? ?""" Decrease countdown or drop pizza and reset countdown. """ > ? ? ? ?global pizza_drop > ? ? ? ?if self.time_til_drop > 0: > ? ? ? ? ? ?self.time_til_drop -= pizza_drop > ? ? ? ?else: > ? ? ? ? ? ?new_pizza = Pizza(x = random.randrange(40, 600)) > ? ? ? ? ? ?rpp = random.randrange(1, 5) > ? ? ? ? ? ?if rpp == 3: > ? ? ? ? ? ? ? ?power_pizza = power_pizza1 (x = random.randrange(40, 600)) > ? ? ? ? ? ? ? ?games.screen.add(power_pizza) > ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ?games.screen.add(new_pizza) > > ? ? ? ? ? ?# set buffer to approx 30% of pizza height, regardless of > pizza speed > ? ? ? ? ? ?self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 > > > def main(): > ? ?""" Play the game. """ > ? ?wall_image = games.load_image("wall.jpg", transparent = False) > ? ?games.screen.background = wall_image > > ? ?the_chef = Chef() > ? ?games.screen.add(the_chef) > > ? ?the_pan = Pan() > ? ?games.screen.add(the_pan) > > ? ?games.mouse.is_visible = False > > ? ?games.screen.event_grab = True > ? ?games.screen.mainloop() > > # start it up! > main() > > > ------------------------------ > > Message: 4 > Date: Wed, 2 Dec 2009 10:13:25 -0500 > From: Kent Johnson > To: Alan Gauld > Cc: tutor at python.org > Subject: Re: [Tutor] Moving from Python 2 to Python 3: A 4 page > ? ? ? ?"cheatsheet" > Message-ID: > ? ? ? ?<1c2a2c590912020713l72b85952ka4d5b8f3d1e2ad7e at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > On Wed, Dec 2, 2009 at 8:56 AM, Alan Gauld wrote: >> >> "Kent Johnson" wrote >> >>> Forwarded from python-announce. A helpful summary of the differences >>> between Python 2 and 3 (though some of the differences were introduced >>> well before Python 3). >> >>> It is available as a free PDF download (no registration or anything) >>> from InformIT's website. Here's the direct link: >>> >>> http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf >> >> >> It didn't work for me I always got forwarded to the Book "home page" on >> InformIT > > Strange. Worked for me yesterday and again just now. > > Kent > > > ------------------------------ > > Message: 5 > Date: Wed, 2 Dec 2009 15:13:20 +0100 > From: Joerg Woelke > To: tutor at python.org > Subject: Re: [Tutor] Moving from Python 2 to Python 3: A 4 page > ? ? ? ?"cheatsheet" > Message-ID: <20091202141320.GC9158 at localhost> > Content-Type: text/plain; charset=us-ascii > > * Alan Gauld [091202 15:07]: >> >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf >> >> >> It didn't work for me I always got forwarded to the Book "home page" >> on InformIT > Worked for me with wget(1). > > -- > You are capable of planning your future. > > > ------------------------------ > > Message: 6 > Date: Wed, 2 Dec 2009 09:26:08 -0600 > From: Wayne Werner > To: Joerg Woelke > Cc: tutor at python.org > Subject: Re: [Tutor] Moving from Python 2 to Python 3: A 4 page > ? ? ? ?"cheatsheet" > Message-ID: > ? ? ? ?<333efb450912020726x22cffc60q6b911387dc13a2ae at mail.gmail.com> > Content-Type: text/plain; charset="windows-1252" > > On Wed, Dec 2, 2009 at 8:13 AM, Joerg Woelke wrote: > >> ?Alan Gauld [091202 15:07]: >> > > >> http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf >> > >> > >> > It didn't work for me I always got forwarded to the Book "home page" >> > on InformIT >> Worked for me with wget(1). >> > > > And for me with Google Chrome on Ubuntu... > -Wayne > -- > To be considered stupid and to be told so is more painful than being called > gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, > every vice, has found its defenders, its rhetoric, its ennoblement and > exaltation, but stupidity hasn?t. - Primo Levi > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 70, Issue 5 > ************************************ > -- Regards, Aivars Enkuzens From kent37 at tds.net Wed Dec 2 20:30:14 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 2 Dec 2009 14:30:14 -0500 Subject: [Tutor] working with bit arrays In-Reply-To: <1259777283.2855.15.camel@bermanrl-desktop> References: <1259777283.2855.15.camel@bermanrl-desktop> Message-ID: <1c2a2c590912021130tec05fa5uad1dfe1fcad6f4d8@mail.gmail.com> On Wed, Dec 2, 2009 at 1:08 PM, Robert Berman wrote: > Hi, > > I am trying to represent a number as a list of bits: for example the bit > representation of the integer 8. I did find a number of articles pertaining > to a module called bitarray but I was unable to download/install that > package. I am using Linux on Ubuntu 9.10; Python 2.6.2. > > I am almost certain there is a relatively easy way to convert an integer > that can be represented by 32 bits into an array of bits that I can iterate > over looking for switched on bits or switched off bits. If all you want to do is test bits, you can do that directly using bit-wise logical operators & and |. There is no need to convert to a different representation. For example In [1]: 0xff & 4 Out[1]: 4 In [2]: 0xf0 & 4 Out[2]: 0 Kent From kent37 at tds.net Wed Dec 2 20:34:09 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 2 Dec 2009 14:34:09 -0500 Subject: [Tutor] Moving from Python 2 to Python 3 Message-ID: <1c2a2c590912021134x16ffa127y2264c835e9ec09c9@mail.gmail.com> I don't know that you will reach Mark through this list, I forwarded his post from another list. There is some info on the book web site: http://www.qtrac.eu/py3book.html When replying to a digest, please - change the subject line to something relevant - don't quote the entire digest in your reply. Kent On Wed, Dec 2, 2009 at 2:15 PM, aivars wrote: > Mark, > I have the first edition of your book. What is the difference between > two editions? From wescpy at gmail.com Wed Dec 2 21:06:56 2009 From: wescpy at gmail.com (wesley chun) Date: Wed, 2 Dec 2009 12:06:56 -0800 Subject: [Tutor] Moving from Python 2 to Python 3 In-Reply-To: <1c2a2c590912021134x16ffa127y2264c835e9ec09c9@mail.gmail.com> References: <1c2a2c590912021134x16ffa127y2264c835e9ec09c9@mail.gmail.com> Message-ID: <78b3a9580912021206w6428b0aayb08ebb21663a7a6e@mail.gmail.com> >> I have the first edition of your book. What is the difference between >> two editions? i believe the 1st ed is 3.0 and the 2nd ed is 3.1 but haven't confirmed with him yet. -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.plum at uni-koeln.de Wed Dec 2 23:32:39 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Wed, 02 Dec 2009 23:32:39 +0100 Subject: [Tutor] working with bit arrays In-Reply-To: <1259777283.2855.15.camel@bermanrl-desktop> References: <1259777283.2855.15.camel@bermanrl-desktop> Message-ID: <1259793159.2999.11.camel@kallisti> On Mi, 2009-12-02 at 13:08 -0500, Robert Berman wrote: > Hi, > > I am trying to represent a number as a list of bits: for example the > bit representation of the integer 8. I did find a number of articles > pertaining to a module called bitarray but I was unable to > download/install that package. I am using Linux on Ubuntu 9.10; Python > 2.6.2. > > I am almost certain there is a relatively easy way to convert an > integer that can be represented by 32 bits into an array of bits that > I can iterate over looking for switched on bits or switched off bits. If all you want is to treat integers as lists of bits, you could create a wrapper class around an integer and implement the __getitem__ and __setitem__ methods to make it behave like a list. Using the bitwise operators, you could make the setter actually modify the bit in question (you'd probably have to either make sure you only receive 1 or 0 as value or you could simply use booleans instead). For a challenge you could try to extend the built-in integer type this way. I'm not sure why you'd need to be able to address bits directly like that, though. Normally the bitwise &, |, << and >> should suffice for all intents and purposes. Making an ACTUAL lists of ACTUAL integers representing the bits would be overkill, though. We're talking several bytes worth of nulls to represent a single bit. I'm all for late optimisation, but this just doesn't feel right. Cheers, Alan From x7-g5W_rt at earthlink.net Thu Dec 3 01:10:26 2009 From: x7-g5W_rt at earthlink.net (GilJohnson) Date: Thu, 3 Dec 2009 00:10:26 +0000 (UTC) Subject: [Tutor] working with bit arrays Message-ID: As`Kent Johnson pointed out, you don't need to convert anything to strings, etc. An integer _is_ a bit array, and individual bits can be tested using the bitwise operators. For your example, if A is an integer you can test bit 8 with: if A & (1 << 8): dosomething There is a simple example on the Python wiki at http://wiki.python.org/moin/BitArrays It uses an array of 32-bit integers as a bit array. The advantage of using 'bitarray' or one of the other packages on the Python Package Index is that they have implemented all of the slicing, etc. of Python lists, strings, etc. Using an array of 32 bit integers, you have to go to some trouble to slice out, say, bits 20 to 40. HTH, Gil From alan.gauld at btinternet.com Thu Dec 3 01:53:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 3 Dec 2009 00:53:02 -0000 Subject: [Tutor] working with bit arrays References: <1259777283.2855.15.camel@bermanrl-desktop> Message-ID: "Robert Berman" wrote > I am trying to represent a number as a list of bits: for example the bit > representation of the integer 8. Numbers are already represented as arrays of bits, thats how they are stored. > I am almost certain there is a relatively easy way to convert an integer > that can be represented by 32 bits into an array of bits that I can > iterate over looking for switched on bits or switched off bits. You can do that using bitmasks. For example to extract the 4th bit use bit4 = value & 0x08 # 0x08 = 00001000 For bit 2 use bit2 = value & 0x02 # 0x02 = 00000010 You can iterate over each bit using for index in range(number_of_bits): print "bit", index + 1, "is", int(bool(value & (2**index))) # int(bool()) prints 1/0 Or am I missing something? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Dec 3 01:57:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 3 Dec 2009 00:57:02 -0000 Subject: [Tutor] Moving from Python 2 to Python 3 References: <1c2a2c590912021134x16ffa127y2264c835e9ec09c9@mail.gmail.com> <78b3a9580912021206w6428b0aayb08ebb21663a7a6e@mail.gmail.com> Message-ID: "wesley chun" wrote > i believe the 1st ed is 3.0 and the 2nd ed is 3.1 but haven't > confirmed with him yet. I bought the 1st edition which is definitely 3.0. It was quite good I thought. I doubt I'll buy another edition just for the 3.1 uplift, but if he covers the new themed widgets in tkinter I might change my mind! :-) Alan G. From emile at fenx.com Thu Dec 3 02:17:15 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 02 Dec 2009 17:17:15 -0800 Subject: [Tutor] working with bit arrays In-Reply-To: References: Message-ID: On 12/2/2009 4:10 PM GilJohnson said... > Using an array of 32 bit integers, you have to go to some > trouble to slice out, say, bits 20 to 40. I think I agree -- if in fact it's not impossible. Tell me that's a typo or take a moment to explain what I'm misunderstanding... Emile From bermanrl at cfl.rr.com Thu Dec 3 02:38:50 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 02 Dec 2009 20:38:50 -0500 Subject: [Tutor] working with bit arrays In-Reply-To: References: Message-ID: <1259804330.2855.23.camel@bermanrl-desktop> Emille, I do think he meant bit 20 to 32 rather than 20 to 40. Unless, of course, he's dealing with a 64 bit word. I am delighted with all the help I have received on this topic and I am gleefully learning anding and oring, but not too much on the EOR side. Thanks again for all the assistance. Robert On Wed, 2009-12-02 at 17:17 -0800, Emile van Sebille wrote: > On 12/2/2009 4:10 PM GilJohnson said... > > > Using an array of 32 bit integers, you have to go to some > > trouble to slice out, say, bits 20 to 40. > > I think I agree -- if in fact it's not impossible. Tell me that's a > typo or take a moment to explain what I'm misunderstanding... > > Emile > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Dec 3 03:19:23 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 2 Dec 2009 21:19:23 -0500 Subject: [Tutor] Do you use unit testing? In-Reply-To: <1c2a2c590911171332w41fd15dcq96eed9adba854d02@mail.gmail.com> References: <64c038660911161254m28e59314v8c2a4d1a747912ba@mail.gmail.com> <1c2a2c590911171332w41fd15dcq96eed9adba854d02@mail.gmail.com> Message-ID: <1c2a2c590912021819u530d04b7k38ceea8a14a8f3da@mail.gmail.com> Uncle Bob Martin has written a great post about TDD: http://blog.objectmentor.com/articles/2009/10/06/echoes-from-the-stone-age "Look, TDD is not my religion, it is one of my disciplines. It?s like dual entry bookkeeping for accountants, or sterile procedure for surgeons. Professionals adopt such disciplines because they understand the theory behind them, and have directly experienced the benefits of using them." The rest of the post is good too! Kent From davea at ieee.org Thu Dec 3 05:04:28 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 02 Dec 2009 23:04:28 -0500 Subject: [Tutor] working with bit arrays In-Reply-To: <1259804330.2855.23.camel@bermanrl-desktop> References: <1259804330.2855.23.camel@bermanrl-desktop> Message-ID: <4B1738CC.7070304@ieee.org> Robert Berman wrote: > Emille, > > I do think he meant bit 20 to 32 rather than 20 to 40. Unless, of > course, he's dealing with a 64 bit word. > > You posted out of order (top--osted). So I am forced to put my response elsewhere. > I am delighted with all the help I have received on this topic and I am > gleefully learning anding and oring, but not too much on the EOR side. > > Thanks again for all the assistance. > > Robert > > On Wed, 2009-12-02 at 17:17 -0800, Emile van Sebille wrote: > > >> On 12/2/2009 4:10 PM GilJohnson said... >> >> >>> Using an array of 32 bit integers, you have to go to some >>> trouble to slice out, say, bits 20 to 40. >>> >> I think I agree -- if in fact it's not impossible. Tell me that's a >> typo or take a moment to explain what I'm misunderstanding... >> >> Emile >> >> >> Once you have an *array* of integers, you have much more than 32 bits to work with. For example, with an array of size 10, you now have 320 bits to work with. He's just pointing out that it's a little bit awkward to address a group of bits that are not all in the same int. So bits 5-20 would be easy, while bits 29-40 would be much harder. DaveA From royhink at gmail.com Thu Dec 3 09:09:05 2009 From: royhink at gmail.com (Roy Hinkelman) Date: Thu, 3 Dec 2009 00:09:05 -0800 Subject: [Tutor] Can't loop thru file and don't see the problem Message-ID: Your list is great. I've been lurking for the past two weeks while I learned the basics. Thanks. I am trying to loop thru 2 files and scrape some data, and the loops are not working. The script is not getting past the first URL from state_list, as the test print shows. If someone could point me in the right direction, I'd appreciate it. I would also like to know the difference between open() and csv.reader(). I had similar issues with csv.reader() when opening these files. Any help greatly appreciated. Roy Code: Select all # DOWNLOAD USGS MISSING FILES import mechanize import BeautifulSoup as B_S import re # import urllib import csv # OPEN FILES # LOOKING FOR THESE SKUs _missing = open('C:\\Documents and Settings\\rhinkelman\\Desktop\\working DB files\\missing_topo_list.csv', 'r') # IN THESE STATES _states = open('C:\\Documents and Settings\\rhinkelman\\Desktop\\working DB files\\state_list.csv', 'r') # IF NOT FOUND, LIST THEM HERE _missing_files = [] # APPEND THIS FILE WITH META _topo_meta = open('C:\\Documents and Settings\\rhinkelman\\Desktop\\working DB files\\topo_meta.csv', 'a') # OPEN PAGE for each_state in _states: each_state = each_state.replace("\n", "") print each_state html = mechanize.urlopen(each_state) _soup = B_S.BeautifulSoup(html) # SEARCH THRU PAGE AND FIND ROW CONTAINING META MATCHING SKU _table = _soup.find("table", "tabledata") print _table #test This is returning 'None' for each_sku in _missing: each_sku = each_sku.replace("\n","") print each_sku #test try: _row = _table.find('tr', text=re.compile(each_sku)) except (IOError, AttributeError): _missing_files.append(each_sku) continue else: _row = _row.previous _row = _row.parent _fields = _row.findAll('td') _name = _fields[1].string _state = _fields[2].string _lat = _fields[4].string _long = _fields[5].string _sku = _fields[7].string _topo_meta.write(_name + "|" + _state + "|" + _lat + "|" + _long + "|" + _sku + "||") print x +': ' + _name print "Missing Files:" print _missing_files _topo_meta.close() _missing.close() _states.close() The message I am getting is: Code: >>> http://libremap.org/data/state/Colorado/drg/ None 33087c2 Traceback (most recent call last): File "//Dc1/Data/SharedDocs/Roy/_Coding Vault/Python code samples/usgs_missing_file_META.py", line 34, in _row = _table.find('tr', text=re.compile(each_sku)) AttributeError: 'NoneType' object has no attribute 'find' And the files look like: Code: state_list http://libremap.org/data/state/Colorado/drg/ http://libremap.org/data/state/Connecticut/drg/ http://libremap.org/data/state/Pennsylvania/drg/ http://libremap.org/data/state/South_Dakota/drg/ missing_topo_list 33087c2 34087b2 33086b7 34086c2 -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Thu Dec 3 12:46:43 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Thu, 03 Dec 2009 13:46:43 +0200 Subject: [Tutor] Can't loop thru file and don't see the problem In-Reply-To: References: Message-ID: <4B17A523.7040901@compuscan.co.za> Roy Hinkelman wrote: > > Your list is great. I've been lurking for the past two weeks while I > learned the basics. Thanks. > > I am trying to loop thru 2 files and scrape some data, and the loops > are not working. > > The script is not getting past the first URL from state_list, as the > test print shows. > > If someone could point me in the right direction, I'd appreciate it. > > I would also like to know the difference between open() and > csv.reader(). I had similar issues with csv.reader() when opening > these files. > > Any help greatly appreciated. > > Roy > > Code: Select all > # DOWNLOAD USGS MISSING FILES > > import mechanize > import BeautifulSoup as B_S > import re > # import urllib > import csv > > # OPEN FILES > # LOOKING FOR THESE SKUs > _missing = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB > files\\missing_topo_list.csv', 'r') > # IN THESE STATES > _states = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB files\\state_list.csv', 'r') > # IF NOT FOUND, LIST THEM HERE > _missing_files = [] > # APPEND THIS FILE WITH META > _topo_meta = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB files\\topo_meta.csv', 'a') > > # OPEN PAGE > for each_state in _states: > each_state = each_state.replace("\n", "") > print each_state > html = mechanize.urlopen(each_state) > _soup = B_S.BeautifulSoup(html) > > # SEARCH THRU PAGE AND FIND ROW CONTAINING META MATCHING SKU > _table = _soup.find("table", "tabledata") > print _table #test This is returning 'None' > If you take a look at the webpage you open up, you will notice there are no tables. Are you certain you are using the correct URLs for this ? > for each_sku in _missing: The for loop `for each_sku in _missing:` will only iterate once, you can either pre-read it into a list / dictionary / set (whichever you prefer) or change it to _missing_filename = 'C:\\Documents and Settings\\rhinkelman\\Desktop\\working DB files\\missing_topo_list.csv' for each_sku in open(_missing_filename): # carry on here > each_sku = each_sku.replace("\n","") > print each_sku #test > try: > _row = _table.find('tr', text=re.compile(each_sku)) > except (IOError, AttributeError): > _missing_files.append(each_sku) > continue > else: > _row = _row.previous > _row = _row.parent > _fields = _row.findAll('td') > _name = _fields[1].string > _state = _fields[2].string > _lat = _fields[4].string > _long = _fields[5].string > _sku = _fields[7].string > > _topo_meta.write(_name + "|" + _state + "|" + _lat + > "|" + _long + "|" + _sku + "||") > > print x +': ' + _name > > print "Missing Files:" > print _missing_files > _topo_meta.close() > _missing.close() > _states.close() > > > The message I am getting is: > > Code: > >>> > http://libremap.org/data/state/Colorado/drg/ > None > 33087c2 > Traceback (most recent call last): > File "//Dc1/Data/SharedDocs/Roy/_Coding Vault/Python code > samples/usgs_missing_file_META.py", line 34, in > _row = _table.find('tr', text=re.compile(each_sku)) > AttributeError: 'NoneType' object has no attribute 'find' > > > And the files look like: > > Code: > state_list > http://libremap.org/data/state/Colorado/drg/ > http://libremap.org/data/state/Connecticut/drg/ > http://libremap.org/data/state/Pennsylvania/drg/ > http://libremap.org/data/state/South_Dakota/drg/ > > missing_topo_list > 33087c2 > 34087b2 > 33086b7 > 34086c2 > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hope the comments above help in your endeavours. -- Kind Regards, Christian Witts From skrabbit at comcast.net Thu Dec 3 18:46:59 2009 From: skrabbit at comcast.net (skrabbit at comcast.net) Date: Thu, 3 Dec 2009 17:46:59 +0000 (UTC) Subject: [Tutor] Python at my work Message-ID: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> My dept at work is reviewing all the different programming languages that we use and Python is kind of on the chopping block because I'm the only one using it so far. Basically, it would mean that no new development would be done in Python. What I need to do is show that Python is a good tool and it should be in our toolbox. The other language that would be used instead of Python is Perl. Perl is currently used more in our dept. I can't say I'm a big fan of Perl, but I'll use if I have to. I'd prefer to use Python. Here's some of the reason's I think we should still use Python, and maybe I could get some other reasons from you guys. Note: We primarily use Perl and Python for web application development, but it also used on servers too. - Clean easy to read syntax - Easy to learn - Object Oriented as needed - Large community - Multi-platform - Fits in your head - Batteries included - Large library(Cheese Shop) Anything else? Mike From bricker.steve at imonmail.com Thu Dec 3 19:13:31 2009 From: bricker.steve at imonmail.com (Steve Bricker) Date: Thu, 3 Dec 2009 12:13:31 -0600 Subject: [Tutor] Python at my work Message-ID: <36486.1259864011@imonmail.com> BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px; }We have Python applications for work with testing reports printed to PDF files. Don't know if you have that needed. Steve Bricker Now blogging at srbricker.blogspot.com On Thu 09/12/03 11:46 , skrabbit at comcast.net sent: My dept at work is reviewing all the different programming languages that we use and Python is kind of on the chopping block because I'm the only one using it so far. Basically, it would mean that no new development would be done in Python. What I need to do is show that Python is a good tool and it should be in our toolbox. The other language that would be used instead of Python is Perl. Perl is currently used more in our dept. I can't say I'm a big fan of Perl, but I'll use if I have to. I'd prefer to use Python. Here's some of the reason's I think we should still use Python, and maybe I could get some other reasons from you guys. Note: We primarily use Perl and Python for web application development, but it also used on servers too. - Clean easy to read syntax - Easy to learn - Object Oriented as needed - Large community - Multi-platform - Fits in your head - Batteries included - Large library(Cheese Shop) Anything else? Mike _______________________________________________ Tutor maillist - To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From warpcat at gmail.com Thu Dec 3 19:44:26 2009 From: warpcat at gmail.com (Eric Pavey) Date: Thu, 3 Dec 2009 10:44:26 -0800 Subject: [Tutor] Python at my work In-Reply-To: <36486.1259864011@imonmail.com> References: <36486.1259864011@imonmail.com> Message-ID: <23cba4bf0912031044v2cb65d78w2cabf5a3f3221fd0@mail.gmail.com> On Thu, Dec 3, 2009 at 10:13 AM, Steve Bricker wrote: > We have Python applications for work with testing reports printed to PDF > files. Don't know if you have that needed. > > Steve Bricker > Now blogging at srbricker.blogspot.com > > On Thu 09/12/03 11:46 , skrabbit at comcast.net sent: > > My dept at work is reviewing all the different programming languages > that we use and Python is kind of on the chopping block because I'm > the only one using it so far. Basically, it would mean that no new > development would be done in Python. > > What I need to do is show that Python is a good tool and it should be > in our toolbox. The other language that would be used instead of > Python is Perl. Perl is currently used more in our dept. I can't say > I'm a big fan of Perl, but I'll use if I have to. I'd prefer to use > Python. > > Here's some of the reason's I think we should still use Python, and > maybe I could get some other reasons from you guys. Note: We primarily > use Perl and Python for web application development, but it also used > on servers too. > > - Clean easy to read syntax > - Easy to learn > - Object Oriented as needed > - Large community > - Multi-platform > - Fits in your head > - Batteries included > - Large library(Cheese Shop) > > Anything else? > > Mike > > I'm in the CG\Game industry, and two of the top DCC apps, Autodesk Maya and Autodesk XSI implement Python as internal scripting languages. In addition, the OpenSource 3D software Blender uses Python as it's scripting language of choice. May be an unrelated field, but possibly some ammunition ;) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Dec 3 20:40:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 3 Dec 2009 14:40:22 -0500 Subject: [Tutor] Python at my work In-Reply-To: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> References: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> Message-ID: <1c2a2c590912031140i1d85b72au6854eec1a68a4407@mail.gmail.com> On Thu, Dec 3, 2009 at 12:46 PM, wrote: > What I need to do is show that Python is a good tool and it should be > in our toolbox. The other language that would be used instead of > Python is Perl. Perl is currently used more in our dept. I can't say > I'm a big fan of Perl, but I'll use if I have to. I'd prefer to use > Python. So would Eric Raymond; here's why: http://www.python.org/about/success/esr/ > Here's some of the reason's I think we should still use Python, and > maybe I could get some other reasons from you guys. Note: We primarily > use Perl and Python for web application development, but it also used > on servers too. > > Anything else? Lots of good choices for web app development in Python - Django, Pylons, etc. Kent From stefan_ml at behnel.de Thu Dec 3 20:45:49 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 03 Dec 2009 20:45:49 +0100 Subject: [Tutor] Python at my work In-Reply-To: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> References: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> Message-ID: skrabbit at comcast.net, 03.12.2009 18:46: > My dept at work is reviewing all the different programming languages > that we use and Python is kind of on the chopping block because I'm > the only one using it so far. Basically, it would mean that no new > development would be done in Python. > > What I need to do is show that Python is a good tool and it should be > in our toolbox. Maybe you should reveal a bit about what this toolbox is actually used for. That would allow others to comment on suitable tools in the Python area. Stefan From x7-g5W_rt at earthlink.net Thu Dec 3 22:13:48 2009 From: x7-g5W_rt at earthlink.net (GilJohnson) Date: Thu, 3 Dec 2009 21:13:48 +0000 (UTC) Subject: [Tutor] working with bit arrays References: <1259804330.2855.23.camel@bermanrl-desktop> <4B1738CC.7070304@ieee.org> Message-ID: Dave Angel ieee.org> writes: > Once you have an *array* of integers, you have much more than 32 bits to > work with. For example, with an array of size 10, you now have 320 bits > to work with. He's just pointing out that it's a little bit awkward to > address a group of bits that are not all in the same int. So bits 5-20 > would be easy, while bits 29-40 would be much harder. > > DaveA > To all, especially DaveA, Dave is right, I'm talking about a bit array made up of integers, so it can be of arbitrary size. To anyone who tried _using_ the bit array on the Python Wiki, I-have to apologize - I screwed up the listing. In the definition of makeBitArray(), the initialization of the integer array should read: bitArray = array.array('I') # 'I' = unsigned 32-bit integer bitArray.extend((fill,) * intSize) instead of the one-line: bitArray = array.array('I', fill) * intsize The new version works in Python 2.6 and 3.x, and the Wiki has been corrected. Gil From bgailer at gmail.com Thu Dec 3 22:52:14 2009 From: bgailer at gmail.com (bob gailer) Date: Thu, 03 Dec 2009 16:52:14 -0500 Subject: [Tutor] Python at my work In-Reply-To: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> References: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> Message-ID: <4B18330E.6070805@gmail.com> skrabbit at comcast.net wrote: > My dept at work is reviewing all the different programming languages > that we use and Python is kind of on the chopping block because I'm > the only one using it so far. Basically, it would mean that no new > development would be done in Python. > If most of the developers are Perl experts management might see Perl as the best way to continue. Perl certainly is widely and effectively used. It is hard to gain acceptance for some other language (or in general getting anyone to accept something different). If there is room for the "bigger picture" then you might investigate differences in development and maintenance costs. Put yourself in management's seat and ask WIIFM? (What's in it for me?) People choose things for their own reasons, not yours. What difficulties are present currently? How might Python address them. You might consider a programming contest. You and a Perl developer - take the same problem, see how long it takes to write the correct code, then see how long it takes 2 other programmers to read and understand the code. > Here's some of the reason's I think we should still use Python, and > maybe I could get some other reasons from you guys. Note: We primarily > use Perl and Python for web application development, but it also used > on servers too. > > Let's break this down into -at least meets Perl - Object Oriented as needed - Large community - Multi-platform - is better than Perl - easier to read syntax - indentation conveys structure - no need for braces - no need to prefix names with symbols - this is a HUGE one for me - in general - one way to do something - no need for parentheses in if statements - dictionary and list indexing use identical syntax - makes it easy to convert a list to a dictionary - better class definitions - do not need to be in a separate module - simpler constructor: - easier to learn and easier to read/maintain - functions - returning values is always explicit (no witch hunts). - no need for local or my statements. I don't see in Perl generators, comprehensions, generator expressions, with statement, threading. Exception handling seems limited. I think the syntax is ugly. - Fits in your head - what does that mean? - Batteries included - what does that mean? - Large library(Cheese Shop) - does Perl also have this? -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Thu Dec 3 23:07:06 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 3 Dec 2009 22:07:06 -0000 Subject: [Tutor] Python at my work References: <18715976.9338111259862419008.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> Message-ID: Playing Devil's Advocate here... wrote > - Clean easy to read syntax > - Easy to learn But if the rest already know Perl that's not such a compelling argument. > - Object Oriented as needed > - Large community Yep, Perl does that too. > - Multi-platform Yep, Perl too. > - Fits in your head Hmmm.... I know what you mean but I'm not sure a Perl fan would understand what that means. > - Batteries included > - Large library(Cheese Shop) Perl fans would say the same - CPAN is bigger and easier to use than the CheeseShop, we can't really argue that point. > Anything else? The main advantage over Perl is its suitability for large scale programming - packages as well as simple modules - and maybe ease of integration with Java via Jython if that matters (I think there is a Java Perl but I'm not sure how mature it is) And of course Perl fans will trawl out the usual "disadvantages" of poorer performance and a smaller installed base. You need to consider the counter arguments when constructing a case. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From royhink at gmail.com Fri Dec 4 01:01:28 2009 From: royhink at gmail.com (Roy Hinkelman) Date: Thu, 3 Dec 2009 16:01:28 -0800 Subject: [Tutor] Can't loop thru file and don't see the problem In-Reply-To: <4B17A523.7040901@compuscan.co.za> References: <4B17A523.7040901@compuscan.co.za> Message-ID: Thank you very much! I had forgotten that unix URLs are case sensitive. Also, I changed my 'For' statements to your suggestion, tweaked the exception code a little, and it's working. So, there are obviously several ways to open files. Do you have a standard practice, or does it depend on the file format? I will eventually be working with Excel and possibly mssql tables. Thanks again for your help. Roy On Thu, Dec 3, 2009 at 3:46 AM, Christian Witts wrote: > Roy Hinkelman wrote: > >> >> Your list is great. I've been lurking for the past two weeks while I >> learned the basics. Thanks. >> >> I am trying to loop thru 2 files and scrape some data, and the loops are >> not working. >> >> The script is not getting past the first URL from state_list, as the test >> print shows. >> >> If someone could point me in the right direction, I'd appreciate it. >> >> I would also like to know the difference between open() and csv.reader(). >> I had similar issues with csv.reader() when opening these files. >> >> Any help greatly appreciated. >> >> Roy >> >> Code: Select all >> # DOWNLOAD USGS MISSING FILES >> >> import mechanize >> import BeautifulSoup as B_S >> import re >> # import urllib >> import csv >> >> # OPEN FILES >> # LOOKING FOR THESE SKUs >> _missing = open('C:\\Documents and >> Settings\\rhinkelman\\Desktop\\working DB files\\missing_topo_list.csv', >> 'r') >> # IN THESE STATES >> _states = open('C:\\Documents and >> Settings\\rhinkelman\\Desktop\\working DB files\\state_list.csv', 'r') >> # IF NOT FOUND, LIST THEM HERE >> _missing_files = [] >> # APPEND THIS FILE WITH META >> _topo_meta = open('C:\\Documents and >> Settings\\rhinkelman\\Desktop\\working DB files\\topo_meta.csv', 'a') >> >> # OPEN PAGE >> for each_state in _states: >> each_state = each_state.replace("\n", "") >> print each_state >> html = mechanize.urlopen(each_state) >> _soup = B_S.BeautifulSoup(html) >> # SEARCH THRU PAGE AND FIND ROW CONTAINING META MATCHING SKU >> _table = _soup.find("table", "tabledata") >> print _table #test This is returning 'None' >> >> If you take a look at the webpage you open up, you will notice there are > no tables. Are you certain you are using the correct URLs for this ? > > for each_sku in _missing: >> > The for loop `for each_sku in _missing:` will only iterate once, you can > either pre-read it into a list / dictionary / set (whichever you prefer) or > change it to > _missing_filename = 'C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB files\\missing_topo_list.csv' > for each_sku in open(_missing_filename): > # carry on here > >> each_sku = each_sku.replace("\n","") >> print each_sku #test >> try: >> _row = _table.find('tr', text=re.compile(each_sku)) >> except (IOError, AttributeError): >> _missing_files.append(each_sku) >> continue >> else: >> _row = _row.previous >> _row = _row.parent >> _fields = _row.findAll('td') >> _name = _fields[1].string >> _state = _fields[2].string >> _lat = _fields[4].string >> _long = _fields[5].string >> _sku = _fields[7].string >> >> _topo_meta.write(_name + "|" + _state + "|" + _lat + "|" + >> _long + "|" + _sku + "||") >> print x +': ' + _name >> >> print "Missing Files:" >> print _missing_files >> _topo_meta.close() >> _missing.close() >> _states.close() >> >> >> The message I am getting is: >> >> Code: >> >>> >> http://libremap.org/data/state/Colorado/drg/ >> None >> 33087c2 >> Traceback (most recent call last): >> File "//Dc1/Data/SharedDocs/Roy/_Coding Vault/Python code >> samples/usgs_missing_file_META.py", line 34, in >> _row = _table.find('tr', text=re.compile(each_sku)) >> AttributeError: 'NoneType' object has no attribute 'find' >> >> >> And the files look like: >> >> Code: >> state_list >> http://libremap.org/data/state/Colorado/drg/ >> http://libremap.org/data/state/Connecticut/drg/ >> http://libremap.org/data/state/Pennsylvania/drg/ >> http://libremap.org/data/state/South_Dakota/drg/ >> >> missing_topo_list >> 33087c2 >> 34087b2 >> 33086b7 >> 34086c2 >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > Hope the comments above help in your endeavours. > > -- > Kind Regards, > Christian Witts > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cappy2112 at gmail.com Fri Dec 4 02:08:47 2009 From: cappy2112 at gmail.com (Tony Cappellini) Date: Thu, 3 Dec 2009 17:08:47 -0800 Subject: [Tutor] When max() doesn't work as expected Message-ID: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> I have a list of 2300 strings. When I call max() on the list, it returned an item with 37 characters. I am only passing 1 argument to max(). I know for a fact that the largest item has 57 characters, and when I called mylist.index('my_57_character_string') the index was found. Printing len(mylist[index]) does indeed return 57 characters. What are the assumptions when calling max on a list of strings? Does the list need to be sorted? In my case, the list is sorted. Does max have any undocumented limitations I'm not aware of? 2.1 Built-in Functions *max*( iterable[, args...][key]) With a single argument iterable, return the largest item of a non-empty iterable (such as a string, tuple or list). With more than one argument, return the largest of the arguments. The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, "max(a,b,c,key=func)"). Changed in version 2.5: Added support for the optional key argument. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jm_hannon at yahoo.com Fri Dec 4 02:49:15 2009 From: jm_hannon at yahoo.com (Michael Hannon) Date: Thu, 3 Dec 2009 17:49:15 -0800 (PST) Subject: [Tutor] Example of use of (?P) and (?P=name) in Python regular expressions? In-Reply-To: <401609.96855.qm@web38805.mail.mud.yahoo.com> References: <401609.96855.qm@web38805.mail.mud.yahoo.com> Message-ID: <516107.92926.qm@web38806.mail.mud.yahoo.com> Hi, folks. I just wanted to thank Martin Walsh and Kent Johnson, both of whom furthered my education by replying (on-list) that the "P=" syntax is used to refer to a named group in the same regular expression, i.e., the one in which the "P" construct was used in the first place. This is as opposed to the thing I was trying to do, which was to use the "P=" syntax in a substitution. -- Mike ----- Original Message ---- From: Michael Hannon To: tutor at python.org Sent: Sat, November 28, 2009 3:15:36 PM Subject: [Tutor] Example of use of (?P) and (?P=name) in Python regular expressions? Greetings. While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using: (?P...) and then refer to them using: (?P=name) I was able to get this to work in the following, nonsensical, example: >>> x = 'Free Fri Fro From' >>> y = re.sub(r'(?P\bFro\b)', r'Frodo (--matched from \g)', x) >>> y 'Free Fri Frodo (--matched from Fro) From' >>> But, as you can see, to refer to the match I used the "\g" notation (that I found some place on the web). I wasn't able to find a way to use the "P=" syntax, and I wasn't able to find any working examples of this syntax on the web. If you have a working example of the use of the "P=" syntax, will you please send it to me? Thanks. -- Mike _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From kent37 at tds.net Fri Dec 4 03:22:40 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 3 Dec 2009 21:22:40 -0500 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> References: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> Message-ID: <1c2a2c590912031822i50e80793r4e28f03c67c4c1c8@mail.gmail.com> On Thu, Dec 3, 2009 at 8:08 PM, Tony Cappellini wrote: > > I have a list of 2300 strings. > > When I call max() on the list, it returned an item with 37 characters. I am > only passing 1 argument to max(). > I know for a fact that the largest item has 57 characters, and when I > called mylist.index('my_57_character_string') the index was found. > > Printing len(mylist[index]) does indeed return 57 characters. > > What are the assumptions when calling max on a list of strings? > Does the list need to be sorted? In my case, the list is sorted. > > Does max have any undocumented limitations I'm not aware of? > max() finds the 'largest' in sort order. Strings sort in dictionary order so the max of a list strings will be the one that comes last in dictionary order. The key= argument to sort lets you modify the way list items are compared; instead of using the default comparison of list objects, the corresponding keys are compared. Try max(my_list, key=len) to use the length of the strings as the key and find the longest string. Kent > > *max*( iterable[, args...][key]) With a single argument iterable, return > the largest item of a non-empty iterable (such as a string, tuple or list). > With more than one argument, return the largest of the arguments. > > The optional key argument specifies a one-argument ordering function like > that used for list.sort(). The key argument, if supplied, must be in > keyword form (for example, "max(a,b,c,key=func)"). Changed in version 2.5: > Added support for the optional key argument. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri Dec 4 03:23:35 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 04 Dec 2009 13:23:35 +1100 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> References: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> Message-ID: On 12/4/2009 12:08 PM, Tony Cappellini wrote: > > What are the assumptions when calling max on a list of strings? > Does the list need to be sorted? In my case, the list is sorted. max determines whether one is larger than the other with "<" operator. max on a list of string will determine the last item if the list has been sorted in lexicographical order (i.e. the item closest to z, in a list of string with alphanumerics characters only). If you want the longest string use: _, longest = max((len(s), s) for s in mylist) From hugo.yoshi at gmail.com Fri Dec 4 03:25:58 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 4 Dec 2009 03:25:58 +0100 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> References: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> Message-ID: <29179d160912031825t537b71d8wad2b60ad471a5263@mail.gmail.com> On Fri, Dec 4, 2009 at 2:08 AM, Tony Cappellini wrote: > > I have a list of 2300 strings. > > When I call max() on the list, it returned an item with 37 characters. I am > only passing 1 argument to max(). > I know for a fact that the largest item has 57 characters, and when I > called mylist.index('my_57_character_string') the index was found. > > Printing len(mylist[index]) does indeed return 57 characters. > > What are the assumptions when calling max on a list of strings? > Does the list need to be sorted? In my case, the list is sorted. > > Does max have any undocumented limitations I'm not aware of? > > Max gives you the largest item in the iterable. That's not the same as the longest item. e.g.: >>> max(['aaa', 'z']) 'z' >>> 'aaa' < 'z' True When you're comparing strings, the 'largest' one is not the longest string, but the string that comes last alphabetically speaking. If you want the item whose length is greatest, use the 'key' argument, like so: >>> max(['aaa', 'z'], key=len) 'aaa' -------------- next part -------------- An HTML attachment was scrubbed... URL: From asweigart at gmail.com Fri Dec 4 03:20:21 2009 From: asweigart at gmail.com (Albert Sweigart) Date: Thu, 3 Dec 2009 18:20:21 -0800 Subject: [Tutor] When max() doesn't work as expected Message-ID: <716dd5b60912031820s1241aa42udb99807e1848537e@mail.gmail.com> max() for strings returns the largest string in an alphabetical sense. So max(['z', 'aaaaaaaaaa']) would return 'z'. You need to specify an ordering function, in your case, len(): max( ['z', 'aaaaaaaa'], key=len) ...which will return 'aaaaaaaa' because it is ordering by key. -Al Sweigart ------------------------------------ You should check out my free book for Python beginners, "Invent Your Own Computer Games with Python" http://inventwithpython.com From wescpy at gmail.com Fri Dec 4 03:58:55 2009 From: wescpy at gmail.com (wescpy at gmail.com) Date: Thu, 3 Dec 2009 18:58:55 -0800 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <29179d160912031825t537b71d8wad2b60ad471a5263@mail.gmail.com> References: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> <29179d160912031825t537b71d8wad2b60ad471a5263@mail.gmail.com> Message-ID: <80FCF99D-51A8-4933-BD16-71D85ECA23A2@gmail.com> On Dec 3, 2009, at 18:25, Hugo Arts wrote: > On Fri, Dec 4, 2009 at 2:08 AM, Tony Cappellini > wrote: > > I have a list of 2300 strings. > > When I call max() on the list, it returned an item with 37 > characters. I am only passing 1 argument to max(). > I know for a fact that the largest item has 57 characters, [...] > What are the assumptions when calling max on a list of strings? > > Max gives you the largest item in the iterable. That's not the same > as the longest item. e.g.: > > >>> max(['aaa', 'z']) > 'z' > >>> 'aaa' < 'z' > True > > When you're comparing strings, the 'largest' one is not the longest > string, but the string that comes last alphabetically speaking. If > you want the item whose length is greatest, use the 'key' argument, > like so: > > >>> max(['aaa', 'z'], key=len) everyone is spot-on. the whole crux of the problem is that "largest" ! = "longest", so that's where 'key' comes in. "largest" (max) means the largest lexicographically, so it's ASCII sort order and presumably code point order for Unicode -- pls correct me if i'm wrong. and of course, the opposite for min/"smallest". cheers, -wesley -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri Dec 4 06:49:32 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 04 Dec 2009 16:49:32 +1100 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <1c2a2c590912031822i50e80793r4e28f03c67c4c1c8@mail.gmail.com> References: <8249c4ac0912031708s2269e0feubcaa712d9a952e3b@mail.gmail.com> <1c2a2c590912031822i50e80793r4e28f03c67c4c1c8@mail.gmail.com> Message-ID: On 12/4/2009 1:22 PM, Kent Johnson wrote: > max() finds the 'largest' in sort order. Strings sort in dictionary > order so the max of a list strings will be the one that comes last in > dictionary order.\ To prevent confusion: When Kent said "dictionary order" it means real-life dictionary, the thick book where you find definitions of words, not python dictionary (which is basically unordered). From denis.spir at free.fr Fri Dec 4 08:21:28 2009 From: denis.spir at free.fr (spir) Date: Fri, 4 Dec 2009 08:21:28 +0100 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <716dd5b60912031820s1241aa42udb99807e1848537e@mail.gmail.com> References: <716dd5b60912031820s1241aa42udb99807e1848537e@mail.gmail.com> Message-ID: <20091204082128.16087a15@o> Albert Sweigart dixit: > You need to specify an ordering function, in your case, len(): By the way, is there any reason why the compare func parameter is called 'key'? Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From prachit915 at gmail.com Fri Dec 4 08:43:42 2009 From: prachit915 at gmail.com (Prasad Mehendale) Date: Fri, 4 Dec 2009 13:13:42 +0530 Subject: [Tutor] saving output data in a file Message-ID: <200912041313.42386.microteacher@gmail.com> I am a beginner. I want to save the output data of the following programme in a file through the programme. Please suggest me the way. I am using Python 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file presently. Thanks in advance. #programme to calculate various parameters for a dc-generator. import math #import os #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding polerpm=[] for ConductorsPerSlot in range(1,11): """ we consider that output voltage is 20 V DC """ PoleRpmProduct=20000/ConductorsPerSlot polerpm.append(PoleRpmProduct) print '(Pole*RPM) product for various values of conductors/slot is: \n', polerpm for poles in range(2,18,2): print print '\n For number of poles='+str(poles) +' RPM values are: ' for i in range(len(polerpm)): rpm=polerpm[i]/poles print rpm, -- --prasad mehendale From cwitts at compuscan.co.za Fri Dec 4 09:00:44 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Fri, 04 Dec 2009 10:00:44 +0200 Subject: [Tutor] Can't loop thru file and don't see the problem In-Reply-To: References: <4B17A523.7040901@compuscan.co.za> Message-ID: <4B18C1AC.40704@compuscan.co.za> Roy Hinkelman wrote: > Thank you very much! > > I had forgotten that unix URLs are case sensitive. > > Also, I changed my 'For' statements to your suggestion, tweaked the > exception code a little, and it's working. > > So, there are obviously several ways to open files. Do you have a > standard practice, or does it depend on the file format? > > I will eventually be working with Excel and possibly mssql tables. > > Thanks again for your help. > > Roy > > > > On Thu, Dec 3, 2009 at 3:46 AM, Christian Witts > > wrote: > > Roy Hinkelman wrote: > > > Your list is great. I've been lurking for the past two weeks > while I learned the basics. Thanks. > > I am trying to loop thru 2 files and scrape some data, and the > loops are not working. > > The script is not getting past the first URL from state_list, > as the test print shows. > > If someone could point me in the right direction, I'd > appreciate it. > > I would also like to know the difference between open() and > csv.reader(). I had similar issues with csv.reader() when > opening these files. > > Any help greatly appreciated. > > Roy > > Code: Select all > # DOWNLOAD USGS MISSING FILES > > import mechanize > import BeautifulSoup as B_S > import re > # import urllib > import csv > > # OPEN FILES > # LOOKING FOR THESE SKUs > _missing = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB > files\\missing_topo_list.csv', 'r') > # IN THESE STATES > _states = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB > files\\state_list.csv', 'r') > # IF NOT FOUND, LIST THEM HERE > _missing_files = [] > # APPEND THIS FILE WITH META > _topo_meta = open('C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB > files\\topo_meta.csv', 'a') > > # OPEN PAGE > for each_state in _states: > each_state = each_state.replace("\n", "") > print each_state > html = mechanize.urlopen(each_state) > _soup = B_S.BeautifulSoup(html) > # SEARCH THRU PAGE AND FIND ROW CONTAINING META > MATCHING SKU > _table = _soup.find("table", "tabledata") > print _table #test This is returning 'None' > > If you take a look at the webpage you open up, you will notice > there are no tables. Are you certain you are using the correct > URLs for this ? > > for each_sku in _missing: > > The for loop `for each_sku in _missing:` will only iterate once, > you can either pre-read it into a list / dictionary / set > (whichever you prefer) or change it to > _missing_filename = 'C:\\Documents and > Settings\\rhinkelman\\Desktop\\working DB > files\\missing_topo_list.csv' > for each_sku in open(_missing_filename): > # carry on here > > each_sku = each_sku.replace("\n","") > print each_sku #test > try: > _row = _table.find('tr', text=re.compile(each_sku)) > except (IOError, AttributeError): > _missing_files.append(each_sku) > continue > else: > _row = _row.previous > _row = _row.parent > _fields = _row.findAll('td') > _name = _fields[1].string > _state = _fields[2].string > _lat = _fields[4].string > _long = _fields[5].string > _sku = _fields[7].string > > _topo_meta.write(_name + "|" + _state + "|" + > _lat + "|" + _long + "|" + _sku + "||") > print x +': ' + _name > > print "Missing Files:" > print _missing_files > _topo_meta.close() > _missing.close() > _states.close() > > > The message I am getting is: > > Code: > >>> > http://libremap.org/data/state/Colorado/drg/ > None > 33087c2 > Traceback (most recent call last): > File "//Dc1/Data/SharedDocs/Roy/_Coding Vault/Python code > samples/usgs_missing_file_META.py", line 34, in > _row = _table.find('tr', text=re.compile(each_sku)) > AttributeError: 'NoneType' object has no attribute 'find' > > > And the files look like: > > Code: > state_list > http://libremap.org/data/state/Colorado/drg/ > http://libremap.org/data/state/Connecticut/drg/ > http://libremap.org/data/state/Pennsylvania/drg/ > http://libremap.org/data/state/South_Dakota/drg/ > > missing_topo_list > 33087c2 > 34087b2 > 33086b7 > 34086c2 > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > Hope the comments above help in your endeavours. > > -- > Kind Regards, > Christian Witts > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Generally I just open files in read or read-binary mode, depending on the data in them. The only times I use it in the for loop situation is for things similar to yours when you need to iterate over the file alot (although if the file is small enough I generally prefer loading it into a dictionary as it will be faster, you build it once and never have to read it off of the disk again as it is in memory). For the Excel you want to work with later take a look at http://www.python-excel.org/ xlrd is the one I use still (works in the UNIX environment), haven't had a need to change it to anything else. For MS SQL you can look at http://pymssql.sourceforge.net/ which is also supported under UNIX. -- Kind Regards, Christian Witts From alan.plum at uni-koeln.de Fri Dec 4 09:21:08 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Fri, 04 Dec 2009 09:21:08 +0100 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <20091204082128.16087a15@o> References: <716dd5b60912031820s1241aa42udb99807e1848537e@mail.gmail.com> <20091204082128.16087a15@o> Message-ID: <1259914868.3101.6.camel@kallisti> On Fr, 2009-12-04 at 08:21 +0100, spir wrote: > By the way, is there any reason why the compare func parameter is called 'key'? I'd guess because what you provide creates keys for the values in the collection to sort them by. What else to call it? "Comparators" compare two values, "hashes" don't need to provide information relevant to ordering, and "indexes" are not directly related to the value at all. Cheers, Alan From orsenthil at gmail.com Fri Dec 4 10:10:24 2009 From: orsenthil at gmail.com (Senthil Kumaran) Date: Fri, 4 Dec 2009 14:40:24 +0530 Subject: [Tutor] saving output data in a file In-Reply-To: <200912041313.42386.microteacher@gmail.com> References: <200912041313.42386.microteacher@gmail.com> Message-ID: <20091204091024.GA10370@ubuntu.ubuntu-domain> On Fri, Dec 04, 2009 at 01:13:42PM +0530, Prasad Mehendale wrote: > I am a beginner. I want to save the output data of the following programme in > a file through the programme. Please suggest me the way. I am using Python > 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file > presently. To save the output to a file, you have a open a file object and write to it. When you are done, just close it. If your program, open a fileobj somewhere on the top. fileobj = open('dc-generator-output.txt','w') And where you put the print statement, replace it with fileobj.write(" ") # Within in the " " the output which you want to put. One strategy would be create a string in place of your print and write the string. For eg. Instead of print '(Pole*RPM) product for various values of conductors/slot is: \n', polerpm You will do msg = '(Pole*RPM) product for various values of conductors/slot is: \n', polerpm fileobj.write(msg) And in the end, close the fileobj. fileobj.close() -- Senthil Flee at once, all is discovered. From alan.gauld at btinternet.com Fri Dec 4 10:14:49 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 4 Dec 2009 09:14:49 -0000 Subject: [Tutor] saving output data in a file References: <200912041313.42386.microteacher@gmail.com> Message-ID: "Prasad Mehendale" wrote >I am a beginner. I want to save the output data of the following programme >in > a file through the programme. The easiest way is to use file redirection at run-time $ python foo.py > results.txt This will work on Linux/MacOS/Windows The alternatives are 1) open a file in your program and replace all of the print statements with write() functions 2) Use the file output trick with print resultFile = open('results.txt',w) print >>resultFile 'my output string' You can find out more about file handling in the Handling Files topic of my tutorial ... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Dec 4 11:09:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 4 Dec 2009 10:09:33 -0000 Subject: [Tutor] saving output data in a file References: <200912041313.42386.microteacher@gmail.com> <20091204091024.GA10370@ubuntu.ubuntu-domain> Message-ID: "Senthil Kumaran" wrote > Instead of > > print '(Pole*RPM) product for various values of conductors/slot is: \n', > polerpm > > You will do > > msg = '(Pole*RPM) product for various values of conductors/slot is: \n', > polerpm You would need to do a bit more since polerpm will not automatically be appended to the string as it would in a print. So you probably want to use a format string or at least convert polerpm to a string using str() -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at gmail.com Fri Dec 4 08:50:55 2009 From: denis.spir at gmail.com (spir) Date: Fri, 4 Dec 2009 08:50:55 +0100 Subject: [Tutor] saving output data in a file In-Reply-To: <200912041313.42386.microteacher@gmail.com> References: <200912041313.42386.microteacher@gmail.com> Message-ID: <20091204085055.7538ab7b@o> Prasad Mehendale dixit: > I am a beginner. I want to save the output data of the following programme in > a file through the programme. Please suggest me the way. I am using Python > 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file > presently. > Thanks in advance. You just need to create/open a file before the loop and add a writing instruction -- see below lines inserted in your code. > #programme to calculate various parameters for a dc-generator. > import math > #import os > #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding > polerpm=[] > for ConductorsPerSlot in range(1,11): > """ we consider that output voltage is 20 V DC """ > PoleRpmProduct=20000/ConductorsPerSlot > polerpm.append(PoleRpmProduct) > print '(Pole*RPM) product for various values of conductors/slot is: \n', > polerpm savefile = file("save_file_path", 'w') ### > for poles in range(2,18,2): > print > print '\n For number of poles='+str(poles) +' RPM values are: ' > for i in range(len(polerpm)): > rpm=polerpm[i]/poles > print rpm, savefile.write(rpm) ### savefile.close() ### Search for file in online or local documentation. Notes: * Always close a file if ever... * open() and file() are aliases. * The 'w' argument stands for 'writing'. Another approach is to redirect sys.stdout to a file. Search for this in the doc. The disadvantages imo are (1) this is less obvious (redirection can easily be overlooked) and (2) you lose output to console for checking. Denis Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From kent37 at tds.net Fri Dec 4 12:52:58 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 4 Dec 2009 06:52:58 -0500 Subject: [Tutor] When max() doesn't work as expected In-Reply-To: <20091204082128.16087a15@o> References: <716dd5b60912031820s1241aa42udb99807e1848537e@mail.gmail.com> <20091204082128.16087a15@o> Message-ID: <1c2a2c590912040352y439d9f6s5db3b4181f656830@mail.gmail.com> On Fri, Dec 4, 2009 at 2:21 AM, spir wrote: > Albert Sweigart dixit: > >> You need to specify an ordering function, in your case, len(): > > By the way, is there any reason why the compare func parameter is called 'key'? It is conventional terminology - the sort key is the value the sort is done on. Google 'sort key' for lots of usage examples. Kent From mmadlavana at gmail.com Fri Dec 4 13:17:27 2009 From: mmadlavana at gmail.com (Mkhanyisi Madlavana) Date: Fri, 4 Dec 2009 14:17:27 +0200 Subject: [Tutor] How do I plot a horizontal line and a vertical line in python Message-ID: <52d606870912040417n477bded2ief117992e5eb02f5@mail.gmail.com> How can I do this using matplotlib? The snippet of my code looks like: k = 1.195*ones((1,data[0].size)) plot(data[0], k,'--') but I get this error: Traceback (most recent call last): File "./plot_detector.py", line 26, in plot(data[0], k,'--') File "/usr/lib/pymodules/python2.6/matplotlib/pyplot.py", line 2142, in plot ret = ax.plot(*args, **kwargs) File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 3418, in plot for line in self._get_lines(*args, **kwargs): File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 297, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 274, in _plot_args x, y = self._xy_from_xy(x, y) File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 214, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension Am I doing this all the wrong way? Regards Mkha From skrabbit at comcast.net Fri Dec 4 16:22:45 2009 From: skrabbit at comcast.net (skrabbit at comcast.net) Date: Fri, 4 Dec 2009 15:22:45 +0000 (UTC) Subject: [Tutor] Python at my work In-Reply-To: Message-ID: <677653918.9722421259940165290.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> ----- Original Message ----- From: "Alan Gauld" To: tutor at python.org Sent: Thursday, December 3, 2009 3:07:06 PM GMT -07:00 US/Canada Mountain Subject: Re: [Tutor] Python at my work Playing Devil's Advocate here... wrote > - Clean easy to read syntax > - Easy to learn But if the rest already know Perl that's not such a compelling argument. > - Object Oriented as needed > - Large community Yep, Perl does that too. > - Multi-platform Yep, Perl too. > - Fits in your head Hmmm.... I know what you mean but I'm not sure a Perl fan would understand what that means. > - Batteries included > - Large library(Cheese Shop) Perl fans would say the same - CPAN is bigger and easier to use than the CheeseShop, we can't really argue that point. > Anything else? The main advantage over Perl is its suitability for large scale programming - packages as well as simple modules - and maybe ease of integration with Java via Jython if that matters (I think there is a Java Perl but I'm not sure how mature it is) And of course Perl fans will trawl out the usual "disadvantages" of poorer performance and a smaller installed base. You need to consider the counter arguments when constructing a case. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ --------------------------------------- Our dept has already decided to keep Perl in its arsenal of languages. I just need to give enough justification to keep Python in that arsenal. Thanks for all of your responses. Alan did a good job of Devil's Advocate. Is Python easier to learn that Perl? When we get new developers into our group, the new developer will need to get up to speed on the tools in our arsenal. The best explanation of "fits in your head" is many times I have guessed on how to do something in Python and it usually works. I don't have to look it up in the docs or a book. I can't say I've had that experience in Perl. The biggest advantage I see is Python's clean easy to read syntax. Perl cannot claim that. I'll need to reread Eric Raymond's article and see if I can pick anything that will help. Thanks. From timgoddardsemail at gmail.com Fri Dec 4 17:10:46 2009 From: timgoddardsemail at gmail.com (Tim Goddard) Date: Fri, 4 Dec 2009 10:10:46 -0600 Subject: [Tutor] Python equivalent to Matlab keyboard function Message-ID: This is probably an easy one. When I was writing Matlab m-files, I really enjoyed the ability to stop the code to check how values were being stored or to 'step' into a function with the keyboard function. I have numerous 'environments'? as part of Python (x,y) including IDLE, Eclipse, and Spyder. I know that Spyder is very Matlab esque, but I prefer to use Eclipse as my programs up to this point have not been mathematics based. To do what I want In Spyder I paste a portion of my program and click the run icon. The interactive console then allows me to proceed through the next lines manually so that I can get real-time feedback. This is really helpful in understanding what I'm doing with OOP as I'm learning from a book and would like to follow along just like it is written which is usually a few lines at a time. Is there a way in Eclipse to do the same thing so I am not switching between environments? When I try the same thing it just terminates at the end of the code. (I am following the learning python for absolute beginners book. I love Python btw, it is very easy to use IMO, and has the muscle to do some really cool things.) From kent37 at tds.net Fri Dec 4 17:57:45 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 4 Dec 2009 11:57:45 -0500 Subject: [Tutor] Python equivalent to Matlab keyboard function In-Reply-To: References: Message-ID: <1c2a2c590912040857nacae64jcd9feab87af5839b@mail.gmail.com> On Fri, Dec 4, 2009 at 11:10 AM, Tim Goddard wrote: > This is probably an easy one. > > When I was writing Matlab m-files, I really enjoyed the ability to > stop the code to check how values were being stored or to 'step' into > a function with the keyboard function. > > I have numerous 'environments'? as part of Python (x,y) including > IDLE, Eclipse, and Spyder. > > I know that Spyder is very Matlab esque, but I prefer to use Eclipse > as my programs up to this point have not been mathematics based. > > To do what I want In Spyder I paste a portion of my program and click > the run icon. ?The interactive console then allows me to proceed > through the next lines manually so that I can get real-time feedback. > This is really helpful in understanding what I'm doing with OOP as I'm > learning from a book and would like to follow along just like it is > written which is usually a few lines at a time. > > Is there a way in Eclipse to do the same thing so I am not switching > between environments? ?When I try the same thing it just terminates at > the end of the code. I'm not really sure what you are doing in Spyder but you should be able to do what you want in PyDev using the debugger or the interactive console. Kent From emailkgnow at gmail.com Fri Dec 4 21:32:01 2009 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Fri, 4 Dec 2009 23:32:01 +0300 Subject: [Tutor] Dictionary Comprehensions Message-ID: Hi everyone! I'm using python 3.1 and I want to to know why is it when I enter the following in a dictionary comprehension: >>> dc={y:x for y in list("khalid") for x in range(6)} I get the following: {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} instead of the expected: {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} and is there a way to get the target (expected) dictionary using a dictionary comprehension. note that I tried sorted(range(6)) also but to no avail. thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri Dec 4 21:57:49 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 05 Dec 2009 07:57:49 +1100 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: On 12/5/2009 7:32 AM, Khalid Al-Ghamdi wrote: > Hi everyone! > > I'm using python 3.1 and I want to to know why is it when I enter the > following in a dictionary comprehension: > > >>> dc={y:x for y in list("khalid") for x in range(6)} are you sure you want this? {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} instead of: {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} for the former case, you can't, you can't guarantee any sort of ordering in dictionary. You should use ordered dictionary instead. For the latter case, it's easy with zip() dc={y:x for x, y in zip("khalid", range(6))} as for why python did that, it's because dictionary comprehension is supposed to have similar semantic with: dc = {x: y for x, y in lst} dc = dict( (x, y) for x, y in lst ) so this: dc={y:x for y in list("khalid") for x in range(6)} becomes: dc=dict( (y, x) for y in list("khalid") for x in range(6) ) note that: >>> [(y, x) for y in list("khalid") for x in range(6)] [('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('h', 0), ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('a', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('l', 0), ('l', 1), ('l', 2), ('l', 3), ('l', 4), ('l', 5), ('i', 0), ('i', 1), ('i', 2), ('i', 3), ('i', 4), ('i', 5), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4), ('d', 5)] and when that big list is turned into a dict it gives: >>> dict(_) {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} From hugo.yoshi at gmail.com Fri Dec 4 22:01:22 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 4 Dec 2009 22:01:22 +0100 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: <29179d160912041301m9837033sf008af4712e908c0@mail.gmail.com> On Fri, Dec 4, 2009 at 9:32 PM, Khalid Al-Ghamdi wrote: > Hi everyone! > I'm using python 3.1 and I want to to know why is it when I enter the > following in a dictionary comprehension: >>>> dc={y:x for y in list("khalid") for x in range(6)} > I get the following: > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} > instead of the expected: > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > and is there a way to get the target (expected) dictionary using a > dictionary comprehension. > note that I tried sorted(range(6)) also but to no avail. > thanks That dictionary comprehension is equivalent to the following code: dc = {} for x in range(6): for y in list("khalid"): dc[y] = x This makes it clear what is wrong. The two for loops come out as nested, rather than zipped. The general fix for something like this is the zip function: bc = {x: y for x, y in zip("khalid", xrange(6))} However, in this case, the idiomatic way to write this would be the enumerate function: bc = {y: x for x, y in enumerate("khalid")} Note that your output is like so: {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} The first character in your original string gets a zero, the second a one, so on and so forth. I'm hoping that's what you meant. If you really want this: {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} I'm not sure how to do that programmatically. The dict object prints its objects in no particular order, so figuring out that order is hard (and very likely implementation/platform dependent). My best guess was sorted("khalid", key=hash): {'a': 0, 'd': 1, 'i': 3, 'h': 2, 'k': 4, 'l': 5} close, but no cigar. Anyone who can think of a clever hack for this? Not that it's very useful, but fun. Hugo From emile at fenx.com Fri Dec 4 22:09:48 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 04 Dec 2009 13:09:48 -0800 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: On 12/4/2009 12:32 PM Khalid Al-Ghamdi said... > Hi everyone! > > I'm using python 3.1 and I want to to know why is it when I enter the > following in a dictionary comprehension: > > >>> dc={y:x for y in list("khalid") for x in range(6)} Try breaking this into pieces... First see what [(x,y) for y in in list("khalid") for x in range(6)] gets you, then see how that fits into dict(). To get where you want, take a look at zip'ing the two lists. (II don't remember -- s zip still in 3.1?) Emile > > I get the following: > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} > > instead of the expected: > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > and is there a way to get the target (expected) dictionary using a > dictionary comprehension. > > note that I tried sorted(range(6)) also but to no avail. > > thanks > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From davea at ieee.org Fri Dec 4 22:44:13 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 04 Dec 2009 16:44:13 -0500 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: <4B1982AD.5000103@ieee.org> Khalid Al-Ghamdi wrote: > Hi everyone! > > I'm using python 3.1 and I want to to know why is it when I enter the > following in a dictionary comprehension: > > >>>> dc={y:x for y in list("khalid") for x in range(6)} >>>> > > I get the following: > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} > > instead of the expected: > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > and is there a way to get the target (expected) dictionary using a > dictionary comprehension. > > note that I tried sorted(range(6)) also but to no avail. > > thanks > > You're confused about what two for loops do here. It's basically a doubly-nested loop, with the outer loop iterating from k through d, and the inner loop iterating from 0 to 5. So there are 36 entries in the dictionary, but of course the dictionary overwrites all the ones with the same key. For each letter in the outer loop, it iterates through all six integers, and settles on 5. To do what you presumably want, instead of a doubly nested loop you need a single loop with a two-tuple for each item, consisting of one letter and one digit. dc = { y:x for y,x in zip("khalid", range(6)) } The output for this is: {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} Now, this isn't the same values for each letter as you "expected," but I'm not sure how you came up with that particular order.I expect, and get, 0 for the first letter 'k' and 1 for the 'h'. etc. Perhaps printing out the zip would make it clearer: list( zip("khalid", range(6)) ) yields [('k', 0), ('h', 1), ('a', 2), ('l', 3), ('i', 4), ('d', 5)] DaveA From timgoddardsemail at gmail.com Fri Dec 4 23:00:32 2009 From: timgoddardsemail at gmail.com (Tim Goddard) Date: Fri, 4 Dec 2009 16:00:32 -0600 Subject: [Tutor] Python equivalent to Matlab keyboard function Message-ID: > > Message: 6 > Date: Fri, 4 Dec 2009 11:57:45 -0500 > From: Kent Johnson > To: Tim Goddard > Cc: tutor at python.org > Subject: Re: [Tutor] Python equivalent to Matlab keyboard function > Message-ID: > ? ? ? ?<1c2a2c590912040857nacae64jcd9feab87af5839b at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > On Fri, Dec 4, 2009 at 11:10 AM, Tim Goddard wrote: >> This is probably an easy one. >> >> When I was writing Matlab m-files, I really enjoyed the ability to >> stop the code to check how values were being stored or to 'step' into >> a function with the keyboard function. >> >> I have numerous 'environments'? as part of Python (x,y) including >> IDLE, Eclipse, and Spyder. >> >> I know that Spyder is very Matlab esque, but I prefer to use Eclipse >> as my programs up to this point have not been mathematics based. >> >> To do what I want In Spyder I paste a portion of my program and click >> the run icon. ?The interactive console then allows me to proceed >> through the next lines manually so that I can get real-time feedback. >> This is really helpful in understanding what I'm doing with OOP as I'm >> learning from a book and would like to follow along just like it is >> written which is usually a few lines at a time. >> >> Is there a way in Eclipse to do the same thing so I am not switching >> between environments? ?When I try the same thing it just terminates at >> the end of the code. > > I'm not really sure what you are doing in Spyder but you should be > able to do what you want in PyDev using the debugger or the > interactive console. > > Kent > Right, My problem is I can't run the code in the interactive console. I'm sure this is a configuration issue in eclipse. Copy and pasting the code into an interactive console window results in a bunch of errors that aren't really there. Running the code normally results in termination once it reaches the end. IDLE and Spyder are meant for python so there is no setup, and I can interact with the program as it remains 'running'. I guess I'll have to spend some time reading more about eclipse and getting the interactive feature working. From alan.gauld at btinternet.com Fri Dec 4 23:42:50 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 4 Dec 2009 22:42:50 -0000 Subject: [Tutor] python time References: <20091125171105.480f4963@o><1c2a2c590911260533m6bdb1511ub35a6d066a8e480d@mail.gmail.com><20091127092725.6ccd24d4@o> <20091128000713.66c835ec@o> Message-ID: Only just spotted this. "spir" wrote >> It's not C's function, it's a Unix system call. >> It's been part of Unix since BSD 4.2 >> > I am confused here. That's what I first thought (there _must_ be a way to > get time > more precise that seconds!). But on my system (ubuntu 9.10) I cannot find > the > proper manner to use these system calls. Even from the command-line > directly. You cannot call system calls from the command line. You can only execute Unixc commands from the shell. A command is a standaline program(executable), it is quite different to a system call. A system call in Unix is the C API provided by the kernel. It can only be called by C or some other language that can link with the kernel libraries - eg Fortran, Assembler etc. So when you use Python it calls the Unix system call via C. [Aside: There is an excellent O'Reilly book on using the Unix system calls from C: "Unix Systems Programming for SVR4" Much of it is directly applicable to Python - sockets, file handling, time, fork, spawn, etc It can be bought used for just a few dollars on Amazon...] > This let me think gettimeofday() and ftime() are C routines, They are C functions provided by the Unix kernel. This makes them Unix OS system calls rather than C standard library functions. >> time() is the lowest common denominator supported by any ANSI C system. Whereas time is provided by the C standard libvrary and is not part of the Unix kernel. > So, I still have the issue of not beeing smart enough to access one > of these systems-provided features. You access them by using Python. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Dec 4 23:46:46 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 4 Dec 2009 22:46:46 -0000 Subject: [Tutor] Python equivalent to Matlab keyboard function References: Message-ID: "Tim Goddard" wrote > My problem is I can't run the code in the interactive console. If its any consolation I couldn't get Eclipse's interactive session working well either. However... > I guess I'll have to spend some time reading more about eclipse and > getting the interactive feature working. I think for what you want you should investigate the debugger rather that interactive mode. Check out breakpoints, watches and step-over and step-into. The Eclipse debugger is good, much better than IDLE. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From dkuhlman at rexx.com Sat Dec 5 02:05:04 2009 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Fri, 4 Dec 2009 17:05:04 -0800 Subject: [Tutor] saving output data in a file In-Reply-To: <200912041313.42386.microteacher@gmail.com> References: <200912041313.42386.microteacher@gmail.com> Message-ID: <20091205010504.GA24139@cutter.rexx.com> On Fri, Dec 04, 2009 at 01:13:42PM +0530, Prasad Mehendale wrote: > I am a beginner. I want to save the output data of the following programme in > a file through the programme. Please suggest me the way. I am using Python > 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file > presently. > Thanks in advance. > > #programme to calculate various parameters for a dc-generator. > import math > #import os > #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding > polerpm=[] > for ConductorsPerSlot in range(1,11): > """ we consider that output voltage is 20 V DC """ > PoleRpmProduct=20000/ConductorsPerSlot > polerpm.append(PoleRpmProduct) > print '(Pole*RPM) product for various values of conductors/slot is: \n', > polerpm > for poles in range(2,18,2): > print > print '\n For number of poles='+str(poles) +' RPM values are: ' > for i in range(len(polerpm)): > rpm=polerpm[i]/poles > print rpm, > > Another suggestion is to define a class that contains a method named "write" which takes one argument which is the text to be printed. This approach is useful when there are print statements in code that is not under your control, for example imported modules. There is a note about this here: http://docs.python.org/library/sys.html#sys.stdout Here is an example: # ================================================ import sys class Redirect(object): def __init__(self, filename): self.outfile = open(filename, 'w') self.count = 0 def write(self, msg): self.count += 1 self.outfile.write('%d %s\n' % (self.count, msg, )) def close(self): self.outfile.close() def test(): print 'starting' save_stdout = sys.stdout redir = Redirect('/tmp/tmp1.txt') sys.stdout = redir print 'something' print 'something else' redir.close() sys.stdout = save_stdout print 'finished' test() # ================================================ A few notes: - Be sure to close or flush the file. - The chunks passed to your write method may not be whole lines. - Dave > > -- > --prasad mehendale > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Dave Kuhlman http://www.rexx.com/~dkuhlman From denis.spir at free.fr Sat Dec 5 11:16:24 2009 From: denis.spir at free.fr (spir) Date: Sat, 5 Dec 2009 11:16:24 +0100 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: <20091205111624.23055791@o> Lie Ryan dixit: > note that: > >>> [(y, x) for y in list("khalid") for x in range(6)] > [('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('h', 0), > ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('a', 0), ('a', 1), > ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('l', 0), ('l', 1), ('l', 2), > ('l', 3), ('l', 4), ('l', 5), ('i', 0), ('i', 1), ('i', 2), ('i', 3), > ('i', 4), ('i', 5), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4), > ('d', 5)] > > and when that big list is turned into a dict it gives: > >>> dict(_) > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} ... because a dict holds a single value per key, so last value overrides previous ones. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Sat Dec 5 11:25:21 2009 From: denis.spir at free.fr (spir) Date: Sat, 5 Dec 2009 11:25:21 +0100 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: <29179d160912041301m9837033sf008af4712e908c0@mail.gmail.com> References: <29179d160912041301m9837033sf008af4712e908c0@mail.gmail.com> Message-ID: <20091205112521.7452442e@o> Hugo Arts dixit: > bc = {y: x for x, y in enumerate("khalid")} > > Note that your output is like so: > {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} > > The first character in your original string gets a zero, the second a > one, so on and so forth. I'm hoping that's what you meant. If you > really want this: > > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > I'm not sure how to do that programmatically. # first need a list of sorted chars # otherwise python cannot guess what order you mean: chars = sorted(list("khalid")) print chars # ==> ['a', 'd', 'h', 'i', 'k', 'l'] # enumerate gives a list of (index, value) pairs # from which you can construct a dict: #~ dc = {index:char for (index,char) in enumerate(chars)} # or (python version < 3) dc = dict((index,char) for (index,char) in enumerate(chars)) print dc # ==> {0: 'a', 1: 'd', 2: 'h', 3: 'i', 4: 'k', 5: 'l'} Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Sat Dec 5 12:19:05 2009 From: denis.spir at free.fr (spir) Date: Sat, 5 Dec 2009 12:19:05 +0100 Subject: [Tutor] saving output data in a file In-Reply-To: <20091205010504.GA24139@cutter.rexx.com> References: <200912041313.42386.microteacher@gmail.com> <20091205010504.GA24139@cutter.rexx.com> Message-ID: <20091205121905.5afd5544@o> Dave Kuhlman dixit: > On Fri, Dec 04, 2009 at 01:13:42PM +0530, Prasad Mehendale wrote: > > I am a beginner. I want to save the output data of the following programme in > > a file through the programme. Please suggest me the way. I am using Python > > 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file > > presently. > > Thanks in advance. > > > > #programme to calculate various parameters for a dc-generator. > > import math > > #import os > > #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding > > polerpm=[] > > for ConductorsPerSlot in range(1,11): > > """ we consider that output voltage is 20 V DC """ > > PoleRpmProduct=20000/ConductorsPerSlot > > polerpm.append(PoleRpmProduct) > > print '(Pole*RPM) product for various values of conductors/slot is: \n', > > polerpm > > for poles in range(2,18,2): > > print > > print '\n For number of poles='+str(poles) +' RPM values are: ' > > for i in range(len(polerpm)): > > rpm=polerpm[i]/poles > > print rpm, > > > > > > Another suggestion is to define a class that contains a method > named "write" which takes one argument which is the text to be > printed. > > This approach is useful when there are print statements in code > that is not under your control, for example imported modules. > > There is a note about this here: > > http://docs.python.org/library/sys.html#sys.stdout > > Here is an example: > > # ================================================ > import sys > > class Redirect(object): > def __init__(self, filename): > self.outfile = open(filename, 'w') > self.count = 0 > def write(self, msg): > self.count += 1 > self.outfile.write('%d %s\n' % (self.count, msg, )) > def close(self): > self.outfile.close() > > def test(): > print 'starting' > save_stdout = sys.stdout > redir = Redirect('/tmp/tmp1.txt') > sys.stdout = redir > print 'something' > print 'something else' > redir.close() > sys.stdout = save_stdout > print 'finished' > > test() > # ================================================ Hello, Thank you Dave for your class, sure I will use it often. -1- print & sys.stdout.write() Just discovered that print(text) is not equivalent to sys.stdout.write(text+'\n') but to sys.stdout.write(text) sys.stdout.write('\n') As shown below due to line numbers. I just added a debug output to (original sys.stdout I call) console. ================ # change class Redirect(object): def __init__(self, filename): self.console = sys.stdout self.outfile = open(filename, 'w') self.count = 0 def write(self, msg): self.count += 1 self.outfile.write('%d %s\n' % (self.count, msg, )) self.console.write('%d %s\n' % (self.count, msg, )) def close(self): self.outfile.close() # ==> Hello starting 1 something 2 3 something else 4 finished ======================= I find this behaviour rather annoying. Requires an ugly trick to workaround. -2- file subtype Is there a reason why you don't make Redirect directly (lol) a subtype of file? Actually I just tried it and got an error "bad file descriptor": # this is my new stdout: # when trying to print: File "__essai__.py", line 74, in test() File "__essai__.py", line 68, in test print 'something' IOError: [Errno 9] Bad file descriptor Below code of new class. Denis class Out(file): def __init__(self, filename, toconsole=True, numberlines=True): file.__init__(self, filename, 'r') print self # debug output to console self.toconsole = toconsole # line numbering self.numberlines = numberlines if self.numberlines: self.linenumber = 0 # save default stdout self.console = sys.stdout def write(self, msg): if self.numberlines: self.linenumber += 1 linenumber = "%3d " % self.linenumber else: linenumber = "" text = "%s%s\n" %(linenumber, msg) self.write(text) if self.toconsole: self.console.write(text) def close(self): # restore default stdout sys.stdout = self.console # close file self.close() Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From evosweet at hotmail.com Sat Dec 5 12:26:12 2009 From: evosweet at hotmail.com (Rayon) Date: Sat, 5 Dec 2009 07:26:12 -0400 Subject: [Tutor] mod_python authentication Message-ID: I need to setup a login page for a web application but I am not finding any code in the mod_python doc that shows me how to do this. What is need is the code to tell apache to get this login data from a login page. -------------- next part -------------- An HTML attachment was scrubbed... URL: From transmogribenno at gmail.com Sat Dec 5 13:02:56 2009 From: transmogribenno at gmail.com (Benno Lang) Date: Sat, 5 Dec 2009 21:02:56 +0900 Subject: [Tutor] mod_python authentication In-Reply-To: References: Message-ID: <9b00d1a90912050402k334db0a9l9a858aacea6548ea@mail.gmail.com> On Sat, Dec 5, 2009 at 8:26 PM, Rayon wrote: > I need to ?setup a login page for a web application but I am not finding any > code in the mod_python doc that shows me how to do this. > > What is need is the code to tell apache to get this login data from a login > page. If you want Apache to directly handle logins via HTTP auth then you don't need to write any code, just configure your vhost or .htaccess file (see for example the AuthUserFile directive). OTOH if you want to build your own login system (e.g. with user details stored in a database) then you: 1) Make a regular HTML form with username and password fields 2) Write whatever login processing code you need, and have the form submit to it. 3) Check for an active login session on every page that requires authentication, and redirect them back to the login form if necessary. HTH, benno. From evosweet at hotmail.com Sat Dec 5 13:57:12 2009 From: evosweet at hotmail.com (Rayon) Date: Sat, 5 Dec 2009 08:57:12 -0400 Subject: [Tutor] mod_python authentication Message-ID: I need to setup a login page for a web application but I am not finding any code in the mod_python doc that shows me how to do this. What is need is the code to tell apache to get this login data from a login page. OTOH if you want to build your own login system (e.g. with user details stored in a database) then you: 1) Make a regular HTML form with username and password fields 2) Write whatever login processing code you need, and have the form submit to it. 3) Check for an active login session on every page that requires authentication, and redirect them back to the login form if necessary. I need to know how to configure the httpd file to allow for this al so. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sun Dec 6 12:56:18 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 06 Dec 2009 06:56:18 -0500 Subject: [Tutor] saving output data in a file In-Reply-To: <20091205121905.5afd5544@o> References: <200912041313.42386.microteacher@gmail.com> <20091205010504.GA24139@cutter.rexx.com> <20091205121905.5afd5544@o> Message-ID: <4B1B9BE2.4080406@ieee.org> spir wrote: > > > > class Out(file): > def __init__(self, filename, toconsole=True, numberlines=True): > file.__init__(self, filename, 'r') > print self > # debug output to console > self.toconsole = toconsole > # line numbering > self.numberlines = numberlines > if self.numberlines: > self.linenumber = 0 > # save default stdout > self.console = sys.stdout > def write(self, msg): > if self.numberlines: > self.linenumber += 1 > linenumber = "%3d " % self.linenumber > else: > linenumber = "" > text = "%s%s\n" %(linenumber, msg) > self.write(text) > if self.toconsole: > self.console.write(text) > def close(self): > # restore default stdout > sys.stdout = self.console > # close file > self.close() > > > > In your call to __init__(), shouldn't the file mode have been "w" not "r" ? Aren't those write() and close() methods infinitely recursive? I suspect you meant something like: file.write(self, text) and file.close(self) Also, I'm not really sure what you're doing with self.console versus sys.stdout. Since you "restore" it in close(), I'm assuming you meant to change it at some point in your code. And if you are going to change it, you should flush it first. If you hadn't had the "restore" code, I'd have guessed instead that you were trying to force all the output to go to the original stdout, even if the caller has reassigned it in the meantime. I didn't actually try your class, but these are problems that jumped out at me. DaveA From waynejwerner at gmail.com Mon Dec 7 00:40:52 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Sun, 6 Dec 2009 17:40:52 -0600 Subject: [Tutor] How do I plot a horizontal line and a vertical line in python In-Reply-To: <52d606870912040417n477bded2ief117992e5eb02f5@mail.gmail.com> References: <52d606870912040417n477bded2ief117992e5eb02f5@mail.gmail.com> Message-ID: <333efb450912061540t533f3f87ufedadbd8980f386c@mail.gmail.com> On Fri, Dec 4, 2009 at 6:17 AM, Mkhanyisi Madlavana wrote: > How can I do this using matplotlib? The snippet of my code looks like: > > Am I doing this all the wrong way? > > This way is easier: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axhline http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axvline HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Mon Dec 7 00:52:15 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Sun, 6 Dec 2009 17:52:15 -0600 Subject: [Tutor] Python at my work In-Reply-To: <677653918.9722421259940165290.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> References: <677653918.9722421259940165290.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> Message-ID: <333efb450912061552g352fddc5t6aed68362932d5e7@mail.gmail.com> On Fri, Dec 4, 2009 at 9:22 AM, wrote: > Is Python easier to learn that Perl? When we get new developers into > our group, the new developer will need to get up to speed on the tools > in our arsenal. > I haven't had any experience with teaching myself Perl, but I would say yes based on what I do know. One of Perl's philosophies is that there are more than one way to do one thing. Python's philosophy is that there really should be one obvious way of doing something. And from my minimal experience with reading Perl code (and a few modifications) and decent experience reading (and modifying) other's Python code, Python is tremendously easier - especially on those new to the language. Here's an interesting source: http://wiki.python.org/moin/PerlPhrasebook HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Dec 7 01:21:59 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 7 Dec 2009 00:21:59 -0000 Subject: [Tutor] Python at my work References: <677653918.9722421259940165290.JavaMail.root@sz0139a.emeryville.ca.mail.comcast.net> <333efb450912061552g352fddc5t6aed68362932d5e7@mail.gmail.com> Message-ID: "Wayne Werner" wrote > One of Perl's philosophies is that there are more than one way to do one > thing. Python's philosophy is that there really should be one obvious way > of > doing something. And from my minimal experience with reading Perl code > (and > a few modifications) and decent experience reading (and modifying) > other's > Python code, Python is tremendously easier - especially on those new to > the > language. Whiler I would agree Perl fans wouldn't. Remember Perl's Larry Wall does not coming from a computing background (as does Guido) but from a Nartural Language backgropund. Thus he designed Perl to have the natural expressibeness of English including multiple idioms for saying the same thing. Thus to Perl fans "more than one way" is the right way! Programming language choice is a hugely subjective topic. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From evosweet at hotmail.com Mon Dec 7 00:34:50 2009 From: evosweet at hotmail.com (Rayon) Date: Sun, 6 Dec 2009 19:34:50 -0400 Subject: [Tutor] mod_python authentication Message-ID: how to check whether a user is authenticated with mod_python -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Mon Dec 7 07:02:28 2009 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 07 Dec 2009 08:02:28 +0200 Subject: [Tutor] Dictionary Comprehensions In-Reply-To: References: Message-ID: <4B1C9A74.4010203@compuscan.co.za> Khalid Al-Ghamdi wrote: > Hi everyone! > > I'm using python 3.1 and I want to to know why is it when I enter the > following in a dictionary comprehension: > > >>> dc={y:x for y in list("khalid") for x in range(6)} > > I get the following: > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} > > instead of the expected: > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > and is there a way to get the target (expected) dictionary using a > dictionary comprehension. > > note that I tried sorted(range(6)) also but to no avail. > > thanks > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Are you maybe looking for `dc = {y:x for y,x in zip('khalid', range(6))}` ? -- Kind Regards, Christian Witts From jojo.mwebaze at gmail.com Mon Dec 7 08:38:24 2009 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Mon, 7 Dec 2009 08:38:24 +0100 Subject: [Tutor] urllib Message-ID: <3124be320912062338m216d3f3o48571a38ee2a715e@mail.gmail.com> hello Tutor, I need help on something very small... i am using urllib to write a query and what i want returned is 'FHI=128%2C128&FLO=1%2C1' i have tried the statement below and i have failed to get the above.. x1,y1,x2,y2 = 1,1,128,128 query = urllib.urlencode({'FHI':'x2,y2,', 'FLO':'x1,y1'}) when that failed, i tried to use query ='FHI=%(x2)d%2C%(y2)d&FLO=%(x1)d%2C%(y1)d' % vars() returned an error "TypeError: not enough arguments for format string" i also tied query ='FHI=%d\%2C%d&FLO=%d\%2C%d' %(x1,x2,y1,y2) i got the error ValueError: unsupported format character 'C' (0x43) at index 8 Where could i be going wrong! Johnson -------------- next part -------------- An HTML attachment was scrubbed... URL: From orsenthil at gmail.com Mon Dec 7 09:10:53 2009 From: orsenthil at gmail.com (Senthil Kumaran) Date: Mon, 7 Dec 2009 13:40:53 +0530 Subject: [Tutor] urllib In-Reply-To: <3124be320912062338m216d3f3o48571a38ee2a715e@mail.gmail.com> References: <3124be320912062338m216d3f3o48571a38ee2a715e@mail.gmail.com> Message-ID: <20091207081053.GA10037@ubuntu.ubuntu-domain> On Mon, Dec 07, 2009 at 08:38:24AM +0100, Jojo Mwebaze wrote: > I need help on something very small... > > i am using urllib to write a query and what i want returned is 'FHI=128%2C128& > FLO=1%2C1' > The way to use urllib.encode is like this: >>> urllib.urlencode({"key":"value"}) 'key=value' >>> urllib.urlencode({"key":"value","key2":"value2"}) 'key2=value2&key=value' For your purpses, you need to construct the dict this way: >>> urllib.urlencode({"FHI":'128,128',"FHO":'1,1'}) 'FHO=1%2C1&FHI=128%2C128' >>> And if you are to use variables, one way to do it would be: >>> x1,y1,x2,y2 = 1,1,128,128 >>> fhi = str(x2) + ',' + str(y2) >>> fho = str(x1) + ',' + str(y1) >>> urllib.urlencode({"FHI":fhi,"FHO":fho}) 'FHO=1%2C1&FHI=128%2C128' -- Senthil From jojo.mwebaze at gmail.com Mon Dec 7 10:18:15 2009 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Mon, 7 Dec 2009 12:18:15 +0300 Subject: [Tutor] urllib In-Reply-To: <20091207081053.GA10037@ubuntu.ubuntu-domain> References: <3124be320912062338m216d3f3o48571a38ee2a715e@mail.gmail.com> <20091207081053.GA10037@ubuntu.ubuntu-domain> Message-ID: <3124be320912070118h9273b8du5bbde84cc7583a49@mail.gmail.com> thanks, Senthil On Mon, Dec 7, 2009 at 11:10 AM, Senthil Kumaran wrote: > On Mon, Dec 07, 2009 at 08:38:24AM +0100, Jojo Mwebaze wrote: > > I need help on something very small... > > > > i am using urllib to write a query and what i want returned is > 'FHI=128%2C128& > > FLO=1%2C1' > > > > The way to use urllib.encode is like this: > > >>> urllib.urlencode({"key":"value"}) > 'key=value' > >>> urllib.urlencode({"key":"value","key2":"value2"}) > 'key2=value2&key=value' > > For your purpses, you need to construct the dict this way: > > >>> urllib.urlencode({"FHI":'128,128',"FHO":'1,1'}) > 'FHO=1%2C1&FHI=128%2C128' > >>> > > > And if you are to use variables, one way to do it would be: > > >>> x1,y1,x2,y2 = 1,1,128,128 > >>> fhi = str(x2) + ',' + str(y2) > >>> fho = str(x1) + ',' + str(y1) > >>> urllib.urlencode({"FHI":fhi,"FHO":fho}) > 'FHO=1%2C1&FHI=128%2C128' > > -- > Senthil > -------------- next part -------------- An HTML attachment was scrubbed... URL: From evosweet at hotmail.com Mon Dec 7 14:35:29 2009 From: evosweet at hotmail.com (Rayon) Date: Mon, 7 Dec 2009 09:35:29 -0400 Subject: [Tutor] mod_python authentication Message-ID: How do I Check for an active login session on every page that requires authentication Been at this for days and it's holding me back can someone plz help me with some code examples. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris_schueler at hotmail.com Mon Dec 7 07:30:30 2009 From: chris_schueler at hotmail.com (Christopher schueler) Date: Mon, 7 Dec 2009 02:30:30 -0400 Subject: [Tutor] Question : Creating cribbage game Message-ID: My name is Chris Schueler and i am having some troubles with my Python programming Our current project is to create the game of cribbage from scratch. The only problem is we are not allowed to use classes, only user-defind functions and arrays. I was wondering if anybody could give me tips or pointers on adding codes or modifying some of my program Here is my Program so far I will also include a .py file of it incase this doesnt look legible from random import* def DisplayTitle(): print print "Welcome to Tech-Sauve Cribbage" print "--------------------------------------------" print " Insctructions " print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "1) Only played with two players (for now) " print "2) The program starts with a full deck of 52 cards" print "3) Deals out 6 cards to each player with a Suit letter" print "4) Then asks each player what 2 cards they want to discard to the crib" print "5) Then the program saves the crib in a temporary deck" print "6) Players start showing cards to get an ammount equal to 31" print "7) Once all the cards have been played, program counts the score" print "8) Then the program will count all possible scores in each hand" print " And it will add the players points to their total score" print "9) First player to reach a score of 121 wins the game" #Gets players names def GetPlayer1(): print Player1 = str(raw_input("Player 1's name ")) return Player1 def GetPlayer2(): print Player2 = str(raw_input("Player 2's name ")) return Player2 #Building the deck def Build_Deck(): for R in range (0,52): cardnumb = numbers[R] cardsuit = suits[R] card = str(numbers[R])+str(suits[R]) Deck.append(card) return Deck,numbers,suits,card,cardnumb,cardsuit #Variables Needed numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4 suits = ["H","C","S","D"]*13 suits.sort() Deck = [] P1hand = [] P2hand = [] Crib = [] Cribcard = [] Cribsuit = [] P1_score = 0 P2_score = 0 Winner = 121 ele = 52 Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck() for X in range(0,6): Y = randint(0,ele) draw = Deck[Y] P1hand.append(draw) Deck.pop(Y) ele -= 1 for X2 in range (0,6): Y1 = randint(0,ele) draw2 = Deck[Y1] P2hand.append(draw2) Deck.pop(Y1) ele -= 1 print Top = randint(0,47) Topcard = Deck[Top] print for count in range(0,2): print P1hand print option = str(raw_input("Player 1,what CARD would you like to add to the crib? CARDS 1 thru 6 ")) if option == "1": Crib.append(P1hand[0]) P1hand.pop(0) elif option == "2": Crib.append(P1hand[1]) P1hand.pop(1) elif option == "3": Crib.append(P1hand[2]) P1hand.pop(2) elif option == "4": Crib.append(P1hand[3]) P1hand.pop(3) elif option == "5": Crib.append(P1hand[4]) P1hand.pop(4) elif option == "6": Crib.append(P1hand[5]) P1hand.pop(5) print for c2 in range(0,2): print P2hand print option1 = str(raw_input("Player 2, what CARD would you like to add to the crib? CARDS 1 thru 6 ")) if option1 == "1": Crib.append(P2hand[0]) P2hand.pop(0) elif option1 == "2": Crib.append(P2hand[1]) P2hand.pop(1) elif option1 == "3": Crib.append(P2hand[2]) P2hand.pop(2) elif option1 == "4": Crib.append(P2hand[3]) P2hand.pop(3) elif option1 == "5": Crib.append(P2hand[4]) P2hand.pop(4) elif option1 == "6": Crib.append(P2hand[5]) P2hand.pop(5) print Deck print "The TOP CARD is ",Topcard print "Player 1's Hand is ",P1hand print "Player 2's Hand is ",P2hand print "The 4 cards in the Crib are ",Crib _________________________________________________________________ Ready. Set. Get a great deal on Windows 7. See fantastic deals on Windows 7 now http://go.microsoft.com/?linkid=9691818 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: Cribbage game.py URL: From alan.plum at uni-koeln.de Mon Dec 7 15:58:15 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Mon, 07 Dec 2009 15:58:15 +0100 Subject: [Tutor] mod_python authentication In-Reply-To: References: Message-ID: <1260197895.3663.39.camel@kallisti> On Mo, 2009-12-07 at 09:35 -0400, Rayon wrote: > How do I Check for an active login session on every page that requires > authentication > > Been at this for days and it?s holding me back can someone plz help > me with some code examples. To understand sessions you first need to understand that HTTP is a stateless protocol: you connect, send your request, receive a response and the connection is closed. Sessions add a layer of abstraction to create functionality the protocol doesn't provide: multiple requests are grouped and treated as belonging together. There are several ways to accomplish this. The most straightforward way would be remembering the client's IP and persisting variables as relative to that IP -- problem is, IPs are unreliable, can be faked, and do not provide a strong indicator of identity (while an IP only resolves to one machine at a time, that machine may be acting as a gateway or proxy for multiple users connected from other machines -- also, many IPs are dynamically allocated thanks to ISPs). Another method is putting the session's ID in the URLs you display to your users. This creates a lot of problems, though: the session is only maintained as long as the user uses exactly the URLs you provide (they won't stay logged in, for example, if they bookmark a plain URL without the session ID) and it may accidentally be shared between different users by passing the URL verbatim (most users don't know enough about URLs to clean session IDs out of them before sending them to other people -- or don't care!). The fix for this is usually to restrict the session to an IP (which is why you often see the checkbox "Restrict my session to this IP" in log-in forms), but that screws you over if your IP may randomly change between consecutive requests and thus may break the illusion. The most common and reliable choice is the good old session cookie: a cookie (a small data string) is sent to the browser, containing just the session ID (and, sometimes, non-critical data such as accessibility settings if the website provides them). Because the browser is normally restricted to a single user, the session ID is stored in a safe place -- except it isn't really because some people use e.g. internet caf?s and such which may not dispose of session data regularly. Also, a user may access the same site from different devices or places, therefore hoarding cookies for different sessions creating consistency problems. Still, cookies are the easiest and most reliable way to store a session ID and non-critical data. If you couple them with IP restrictions and a conservative expiry time (i.e. duration of inactivity until the session becomes invalid or "expired" and all associated variables are wiped) and provide a fallback mechanism for users who disabled (or can't accept) cookies, you should have most scenarios covered (although some sites actually just stick to cookies and provide no fallbacks). So once you've decided on a mechanism to persist the session ID, let's see what a session actually is. In most cases you want to use them for a log-in mechanism: the user enters their username and password, successfully, and is welcomed by a personal greeting and a new navigation subtree that was previously unavailable. In this case it may be tempting to simply store the user's ID and log-in state in a cookie, but that'd be incredibly silly because the user can easily edit them if he knows about cookies (even worse things can happen if you provide useful variables like "is_admin: False"). Instead you should store those variables in a safe place ("persist" them) like a database or special session files. The session ID acts as a key to the session file or database entry, so you need to make sure it's not easily guessable: many websites use very long seemingly-randomly generated strings (a hash of the user's IP and the millisecond time of the session's creation may yield good results). Also, if you want to persist something, make sure it's easily persistable. A string variable is child's play, an open file on the other hand may cause locking problems and if you deal with large volumes of data (e.g. binary file uploads kept in memory) you may quickly run out of space. If you don't want to have to deal with all of these considerations and instead prefer something shrinkwrapped and ready for use, Google is your friend. Depending on what you use there are plenty of CGI-compatible packages and WSGI frameworks to choose from. Cheers, Alan Plum From aivars868 at gmail.com Mon Dec 7 16:30:57 2009 From: aivars868 at gmail.com (aivars) Date: Mon, 7 Dec 2009 17:30:57 +0200 Subject: [Tutor] mod_python authentication In-Reply-To: <1260197895.3663.39.camel@kallisti> References: <1260197895.3663.39.camel@kallisti> Message-ID: Alan, I am very impressed! This one goes to my knowledge base. Thanks a lot. 2009/12/7 Alan Plum : > On Mo, 2009-12-07 at 09:35 -0400, Rayon wrote: >> How do I Check for an active login session on every page that requires >> authentication >> >> Been at this for days and it?s holding me back can someone ?plz help >> me with some code examples. > > To understand sessions you first need to understand that HTTP is a > stateless protocol: you connect, send your request, receive a response > and the connection is closed. > > Sessions add a layer of abstraction to create functionality the protocol > doesn't provide: multiple requests are grouped and treated as belonging > together. > > There are several ways to accomplish this. The most straightforward way > would be remembering the client's IP and persisting variables as > relative to that IP -- problem is, IPs are unreliable, can be faked, and > do not provide a strong indicator of identity (while an IP only resolves > to one machine at a time, that machine may be acting as a gateway or > proxy for multiple users connected from other machines -- also, many IPs > are dynamically allocated thanks to ISPs). > > Another method is putting the session's ID in the URLs you display to > your users. This creates a lot of problems, though: the session is only > maintained as long as the user uses exactly the URLs you provide (they > won't stay logged in, for example, if they bookmark a plain URL without > the session ID) and it may accidentally be shared between different > users by passing the URL verbatim (most users don't know enough about > URLs to clean session IDs out of them before sending them to other > people -- or don't care!). > > The fix for this is usually to restrict the session to an IP (which is > why you often see the checkbox "Restrict my session to this IP" in > log-in forms), but that screws you over if your IP may randomly change > between consecutive requests and thus may break the illusion. > > The most common and reliable choice is the good old session cookie: a > cookie (a small data string) is sent to the browser, containing just the > session ID (and, sometimes, non-critical data such as accessibility > settings if the website provides them). Because the browser is normally > restricted to a single user, the session ID is stored in a safe place -- > except it isn't really because some people use e.g. internet caf?s and > such which may not dispose of session data regularly. Also, a user may > access the same site from different devices or places, therefore > hoarding cookies for different sessions creating consistency problems. > > Still, cookies are the easiest and most reliable way to store a session > ID and non-critical data. If you couple them with IP restrictions and a > conservative expiry time (i.e. duration of inactivity until the session > becomes invalid or "expired" and all associated variables are wiped) and > provide a fallback mechanism for users who disabled (or can't accept) > cookies, you should have most scenarios covered (although some sites > actually just stick to cookies and provide no fallbacks). > > So once you've decided on a mechanism to persist the session ID, let's > see what a session actually is. In most cases you want to use them for a > log-in mechanism: the user enters their username and password, > successfully, and is welcomed by a personal greeting and a new > navigation subtree that was previously unavailable. > > In this case it may be tempting to simply store the user's ID and log-in > state in a cookie, but that'd be incredibly silly because the user can > easily edit them if he knows about cookies (even worse things can happen > if you provide useful variables like "is_admin: False"). Instead you > should store those variables in a safe place ("persist" them) like a > database or special session files. > > The session ID acts as a key to the session file or database entry, so > you need to make sure it's not easily guessable: many websites use very > long seemingly-randomly generated strings (a hash of the user's IP and > the millisecond time of the session's creation may yield good results). > > Also, if you want to persist something, make sure it's easily > persistable. A string variable is child's play, an open file on the > other hand may cause locking problems and if you deal with large volumes > of data (e.g. binary file uploads kept in memory) you may quickly run > out of space. > > If you don't want to have to deal with all of these considerations and > instead prefer something shrinkwrapped and ready for use, Google is your > friend. Depending on what you use there are plenty of CGI-compatible > packages and WSGI frameworks to choose from. > > > Cheers, > > Alan Plum > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From timgoddardsemail at gmail.com Mon Dec 7 17:08:44 2009 From: timgoddardsemail at gmail.com (Tim Goddard) Date: Mon, 7 Dec 2009 10:08:44 -0600 Subject: [Tutor] Question : Creating cribbage game Message-ID: > Message: 2 > Date: Mon, 7 Dec 2009 02:30:30 -0400 > From: Christopher schueler > To: > Subject: [Tutor] Question : Creating cribbage game > Message-ID: > Content-Type: text/plain; charset="iso-8859-1" > > > My name is Chris Schueler and i am having some troubles with my Python programming > > > > Our current project is to create the game of cribbage from scratch. > > The only problem is we are not allowed to use classes, only user-defind functions and arrays. I was wondering if anybody could give me tips or pointers on adding codes or modifying some of my program > > > > Here is my Program so far > > I will also include a .py file of it incase this doesnt look legible > > > > from random import* > > > > > def DisplayTitle(): > ? ?print > ? ?print "Welcome to Tech-Sauve Cribbage" > ? ?print "--------------------------------------------" > ? ?print " ? ? ? ? ? ? ? Insctructions ? ? ? ? ? ? ? ?" > ? ?print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" > ? ?print "1) Only played with two players (for now) ? " > ? ?print "2) The program starts with a full deck of 52 cards" > ? ?print "3) Deals out 6 cards to each player with a Suit letter" > ? ?print "4) Then asks each player what 2 cards they want to discard to the crib" > ? ?print "5) Then the program saves the crib in a temporary deck" > ? ?print "6) Players start showing cards to get an ammount equal to 31" > ? ?print "7) Once all the cards have been played, program counts the score" > ? ?print "8) Then the program will count all possible scores in each hand" > ? ?print " ? And it will add the players points to their total score" > ? ?print "9) First player to reach a score of 121 wins the game" > #Gets players names > def GetPlayer1(): > ? ?print > ? ?Player1 = str(raw_input("Player 1's name ")) > ? ?return Player1 > def GetPlayer2(): > ? ?print > ? ?Player2 = str(raw_input("Player 2's name ")) > ? ?return Player2 > #Building the deck > def Build_Deck(): > ? ?for R in range (0,52): > ? ? ? ?cardnumb = numbers[R] > ? ? ? ?cardsuit = suits[R] > ? ? ? ?card = str(numbers[R])+str(suits[R]) > ? ? ? ?Deck.append(card) > ? ?return Deck,numbers,suits,card,cardnumb,cardsuit > > > #Variables Needed > numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4 > suits = ["H","C","S","D"]*13 > suits.sort() > Deck = [] > P1hand = [] > P2hand = [] > Crib = [] > Cribcard = [] > Cribsuit = [] > P1_score = 0 > P2_score = 0 > Winner = 121 > ele = 52 > Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck() > for X in range(0,6): > ? ?Y = randint(0,ele) > ? ?draw = Deck[Y] > ? ?P1hand.append(draw) > ? ?Deck.pop(Y) > ? ?ele -= 1 > for X2 in range (0,6): > ? ?Y1 = randint(0,ele) > ? ?draw2 = Deck[Y1] > ? ?P2hand.append(draw2) > ? ?Deck.pop(Y1) > ? ?ele -= 1 > print > Top = randint(0,47) > Topcard = Deck[Top] > print > for count in range(0,2): > ? ?print P1hand > ? ?print > ? ?option = str(raw_input("Player 1,what CARD would you like to add to the crib? ?CARDS 1 thru 6 ")) > ? ?if option == "1": > ? ? ? ?Crib.append(P1hand[0]) > ? ? ? ?P1hand.pop(0) > ? ?elif option == "2": > ? ? ? ?Crib.append(P1hand[1]) > ? ? ? ?P1hand.pop(1) > ? ?elif option == "3": > ? ? ? ?Crib.append(P1hand[2]) > ? ? ? ?P1hand.pop(2) > ? ?elif option == "4": > ? ? ? ?Crib.append(P1hand[3]) > ? ? ? ?P1hand.pop(3) > ? ?elif option == "5": > ? ? ? ?Crib.append(P1hand[4]) > ? ? ? ?P1hand.pop(4) > ? ?elif option == "6": > ? ? ? ?Crib.append(P1hand[5]) > ? ? ? ?P1hand.pop(5) > print > for c2 in range(0,2): > ? ?print P2hand > ? ?print > ? ?option1 = str(raw_input("Player 2, what CARD would you like to add to the crib? ?CARDS 1 thru 6 ")) > ? ?if option1 == "1": > ? ? ? ?Crib.append(P2hand[0]) > ? ? ? ?P2hand.pop(0) > ? ?elif option1 == "2": > ? ? ? ?Crib.append(P2hand[1]) > ? ? ? ?P2hand.pop(1) > ? ?elif option1 == "3": > ? ? ? ?Crib.append(P2hand[2]) > ? ? ? ?P2hand.pop(2) > ? ?elif option1 == "4": > ? ? ? ?Crib.append(P2hand[3]) > ? ? ? ?P2hand.pop(3) > ? ?elif option1 == "5": > ? ? ? ?Crib.append(P2hand[4]) > ? ? ? ?P2hand.pop(4) > ? ?elif option1 == "6": > ? ? ? ?Crib.append(P2hand[5]) > ? ? ? ?P2hand.pop(5) > > print Deck > print "The TOP CARD is ",Topcard > print "Player 1's Hand is ",P1hand > print "Player 2's Hand is ",P2hand > print "The 4 cards in the Crib are ",Crib > Unfortunately I had to read a few wiki pages of cribbage first, so my understanding of the game is weak. My suggestions: Start with an outline of play (more to help us understand cribbage) >From my quick lesson, it sounds like you have so far: Get player names (two players) Create deck Ask player which cards to put in crib So for what you have now here are some suggestions: You are creating variables "numbers" and "suits" in your global namespace. Then you use them in your Build_Deck function which is fine, but then you are returning them at the end of the function, overwriting the original variable definition. I don't think it would mess up your code but it is messy. I also don't see where you are using card, cardnumb, or cardsuit elsewhere. I see your technique for chooosing cards at random, however the random module includes a shuffle function so you could create a shuffled deck with: import from random * #shuffle deck Deck = shuffle(Deck) # your randomly determined top card would then be: top_card = Deck[0] # removing the top card using your method is easy Deck.pop(0) Lastly, your lines: > Top = randint(0,47) > Topcard = Deck[Top] comes after you've reduced the Deck by 12 cards so I think your randint(0,47) is incorrect? should it be 52-12 = 40? Good luck with the rest of it. Think about writing functions for repetitive procedures (i.e. drawing cards from a deck) which is loosely the DRY principle (Don't repeat yourself) From denis.spir at free.fr Mon Dec 7 20:14:56 2009 From: denis.spir at free.fr (spir) Date: Mon, 7 Dec 2009 20:14:56 +0100 Subject: [Tutor] Question : Creating cribbage game In-Reply-To: References: Message-ID: <20091207201456.72cfda17@o> Christopher schueler dixit: > > My name is Chris Schueler and i am having some troubles with my Python programming > > > > Our current project is to create the game of cribbage from scratch. > > The only problem is we are not allowed to use classes, only user-defind functions and arrays. I was wondering if anybody could give me tips or pointers on adding codes or modifying some of my program From my limited experience in coding games. You have to model several distinct aspects: * Constant data about the game, such as a card set. * The game logic, mirroring the (real) game rules, ie what players can do, and what comes out of their actions. * The game state, what's the situation at a given point in time, constantly modified by the above actions. * Possibly some AI if the computer plays a role. Note that first 2 points are predefined aspects (constants in the plain sense of the word). Forbidding OO is a very bad thing because game modelling is precisely a programming domain in which this paradigm applies very naturally : every element in the game (state) is an "object" that can be modified through methods representing game rules. Python dicts offer a powerful tool to represent kinds of objects, when used as records (lookup in wikipedia if you don't see what I mean). Moreover, python functions beeing "namable" objects, you can even attach relevant funcs to records so as to simulate methods. All you miss then is typical OO syntactic sugar where 'self' is automagically inserted as first argument of a method call. Instead of hand.popCard(card) you need to write hand.popCard(hand, card) Some more comments below in your code. > Here is my Program so far > > I will also include a .py file of it incase this doesnt look legible > > > > from random import* > > > > > def DisplayTitle(): > print > print "Welcome to Tech-Sauve Cribbage" > print "--------------------------------------------" > print " Insctructions " > print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" > print "1) Only played with two players (for now) " > print "2) The program starts with a full deck of 52 cards" > print "3) Deals out 6 cards to each player with a Suit letter" > print "4) Then asks each player what 2 cards they want to discard to the crib" > print "5) Then the program saves the crib in a temporary deck" > print "6) Players start showing cards to get an ammount equal to 31" > print "7) Once all the cards have been played, program counts the score" > print "8) Then the program will count all possible scores in each hand" > print " And it will add the players points to their total score" > print "9) First player to reach a score of 121 wins the game" This is a single string to write. Use multiline strings inside triple quotes """...""" and write in a single instruction. No need for a func. > #Gets players names > def GetPlayer1(): > print > Player1 = str(raw_input("Player 1's name ")) > return Player1 > def GetPlayer2(): > print > Player2 = str(raw_input("Player 2's name ")) > return Player2 This is twice the same func. Write a single one with a parameter representing a player, then call it twice. You'd better use a dict for each player because doubt the only relevant info is their name. Why not attach their hand, score, or whatever to the structures representing players? player1 = {name:None, more:foo} player2 = {name:None, more:foo} def getPlayerName(player): # (raw_input already returns a string) player["name"] = raw_input("Player 1's name ") getPlayerName(player1) getPlayerName(player2) > #Building the deck > def Build_Deck(): > for R in range (0,52): > cardnumb = numbers[R] > cardsuit = suits[R] > card = str(numbers[R])+str(suits[R]) > Deck.append(card) > return Deck,numbers,suits,card,cardnumb,cardsuit This func should only return Deck. Card cardnum, cardsuit are local variables used obly in the func, suits and numbers are input instead: def Build_Deck(suits, number, card_count): # 52 is also a predefined constant, namely here called card_count for R in range (0,card_count): cardnumb = numbers[R] cardsuit = suits[R] card = str(numbers[R])+str(suits[R]) Deck.append(card) return Deck ... define constants about card: card_count, suits and numbers ... Deck = Build_Deck(suits, number) You'd better represent each card with a pair {"suit":suit, "number":number} so as to be able to compare their strength (unless this is irrelevant for this game). > #Variables Needed > numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4 > suits = ["H","C","S","D"]*13 > suits.sort() > Deck = [] > P1hand = [] > P2hand = [] > Crib = [] > Cribcard = [] > Cribsuit = [] > P1_score = 0 > P2_score = 0 > Winner = 121 > ele = 52 All non-null things above are constants that define the game. Empty and zero things are variables that will be part of the game state. You'd better separate this clearly. You define ele (?) but don't use it. I haven't gone farther in reading the code. Strongly missing is some structure in code, both for data and logic. Start woth grouping things inside dictionaries and organising code with a main() func. Denis > Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck() > for X in range(0,6): > Y = randint(0,ele) > draw = Deck[Y] > P1hand.append(draw) > Deck.pop(Y) > ele -= 1 > for X2 in range (0,6): > Y1 = randint(0,ele) > draw2 = Deck[Y1] > P2hand.append(draw2) > Deck.pop(Y1) > ele -= 1 > print > Top = randint(0,47) > Topcard = Deck[Top] > print > for count in range(0,2): > print P1hand > print > option = str(raw_input("Player 1,what CARD would you like to add to the crib? CARDS 1 thru 6 ")) > if option == "1": > Crib.append(P1hand[0]) > P1hand.pop(0) > elif option == "2": > Crib.append(P1hand[1]) > P1hand.pop(1) > elif option == "3": > Crib.append(P1hand[2]) > P1hand.pop(2) > elif option == "4": > Crib.append(P1hand[3]) > P1hand.pop(3) > elif option == "5": > Crib.append(P1hand[4]) > P1hand.pop(4) > elif option == "6": > Crib.append(P1hand[5]) > P1hand.pop(5) > print > for c2 in range(0,2): > print P2hand > print > option1 = str(raw_input("Player 2, what CARD would you like to add to the crib? CARDS 1 thru 6 ")) > if option1 == "1": > Crib.append(P2hand[0]) > P2hand.pop(0) > elif option1 == "2": > Crib.append(P2hand[1]) > P2hand.pop(1) > elif option1 == "3": > Crib.append(P2hand[2]) > P2hand.pop(2) > elif option1 == "4": > Crib.append(P2hand[3]) > P2hand.pop(3) > elif option1 == "5": > Crib.append(P2hand[4]) > P2hand.pop(4) > elif option1 == "6": > Crib.append(P2hand[5]) > P2hand.pop(5) > > print Deck > print "The TOP CARD is ",Topcard > print "Player 1's Hand is ",P1hand > print "Player 2's Hand is ",P2hand > print "The 4 cards in the Crib are ",Crib > > _________________________________________________________________ > Ready. Set. Get a great deal on Windows 7. See fantastic deals on Windows 7 now > http://go.microsoft.com/?linkid=9691818 ________________________________ la vita e estrany http://spir.wikidot.com/ From hultgren1946 at yahoo.com Mon Dec 7 20:53:40 2009 From: hultgren1946 at yahoo.com (Richard Hultgren) Date: Mon, 7 Dec 2009 11:53:40 -0800 (PST) Subject: [Tutor] loops Message-ID: <188020.25307.qm@web113212.mail.gq1.yahoo.com> a = 0 b = 1 count = 0 max_count = 20 while count < max_count: ??? count = count + 1 ??? # we need to keep track of a since we change it ??? old_a = a????????????????# especially here ??? old_b = b ??? a = old_b ??? b = old_a + old_b ??? # Notice that the , at the end of a print statement keeps it ??? # from switching to a new line ??? print old_a, -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Mon Dec 7 22:04:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 7 Dec 2009 16:04:22 -0500 Subject: [Tutor] loops In-Reply-To: <188020.25307.qm@web113212.mail.gq1.yahoo.com> References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> Message-ID: <1c2a2c590912071304g3df6a54cw4ad7b8646371c8e3@mail.gmail.com> Is there a question here? Please skip the giant type size. Kent On Mon, Dec 7, 2009 at 2:53 PM, Richard Hultgren wrote: > a = 0 > b = 1 > count = 0 > max_count = 20 > while count < max_count: > ??? count = count + 1 > ??? # we need to keep track of a since we change it > ??? old_a = a????????????????# especially here > ??? old_b = b > ??? a = old_b > ??? b = old_a + old_b > ??? # Notice that the , at the end of a print statement keeps it > ??? # from switching to a new line > ??? print old_a, > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From pine508 at hotmail.com Tue Dec 8 02:37:34 2009 From: pine508 at hotmail.com (Che M) Date: Mon, 7 Dec 2009 20:37:34 -0500 Subject: [Tutor] functions--how long is too long? Message-ID: I have some functions that seem kind of long to me. One of them, with white space, comments, print statements, and some commented-out lines, is 118 lines long. If I remove all that, it is 57 lines long. I get the sense that is inappropriately long for a Python function. The length of it is due to a number of if statements--things it needs to check in terms of the state of the app at the time it is called. So there are a number of conditional (and "subconditional") parts to it, and what it does in response to those conditions. In fact the word "if" appears in it 12 times. I realize I can and should refactor parts that are used in other places in the code, but I don't there are that many in some of these. Is there a better way to think about organizing this? Thanks, Che _________________________________________________________________ Windows Live Hotmail gives you a free,exclusive gift. http://www.microsoft.com/windows/windowslive/hotmail_bl1/hotmail_bl1.aspx?ocid=PID23879::T:WLMTAGL:ON:WL:en-ww:WM_IMHM_7:092009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Tue Dec 8 03:03:29 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 7 Dec 2009 20:03:29 -0600 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: Message-ID: If your code is not sensitive information, it might help us if you post it to pastebin or something so we can take a look. In general though, functions should be as long as they need to be (and no longer!). 57 lines is not inordinately long. If it's hard for you to read, though, you should refactor it. I'd say my personal hard-limit for functions before I start refactoring is probably around 150-200 lines. But it's rare that functions get that long anyway. Remember to think of them as reusable units of code that do one specific procedure. Once you move into OO your functions will probably end up being rather small as well, that paradigm encourages many small functions interacting. On Mon, Dec 7, 2009 at 7:37 PM, Che M wrote: > I have some functions that seem kind of long to me. One of them, with > white space, comments, print statements, and some commented-out lines, > is 118 lines long. If I remove all that, it is 57 lines long. I get the > sense > that is inappropriately long for a Python function. > > The length of it is due to a number of if statements--things it needs to > check in terms of the state of the app at the time it is called. So there > are a number of conditional (and "subconditional") parts to it, and what > it does in response to those conditions. In fact the word "if" appears in > it 12 times. > > I realize I can and should refactor parts that are used in other places > in the code, but I don't there are that many in some of these. Is > there a better way to think about organizing this? > > Thanks, > Che > > ------------------------------ > Windows Live Hotmail gives you a free,exclusive gift. Click here to > download. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 8 04:08:23 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 7 Dec 2009 22:08:23 -0500 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: Message-ID: <1c2a2c590912071908n2d0d0f11i871bb087df173dd7@mail.gmail.com> On Mon, Dec 7, 2009 at 8:37 PM, Che M wrote: > I have some functions that seem kind of long to me.? One of them, with > white space, comments, print statements, and some commented-out lines, > is 118 lines long.? If I remove all that, it is 57 lines long.? I get the > sense > that is inappropriately long for a Python function. > > The length of it is due to a number of if statements--things it needs to > check in terms of the state of the app at the time it is called.? So there > are a number of conditional (and "subconditional") parts to it, and what > it does in response to those conditions.? In fact the word "if" appears in > it 12 times. Perhaps you can extract some functions from the blocks that make up the if statements, or move some of the conditionals themselves into functions. Without seeing some code it is hard to be specific. Kent From marc at marcd.org Tue Dec 8 05:10:48 2009 From: marc at marcd.org (Marc) Date: Mon, 7 Dec 2009 23:10:48 -0500 Subject: [Tutor] mod_python authentication In-Reply-To: References: Message-ID: <029001ca77bc$6d6c46d0$4844d470$@org> > On Mo, 2009-12-07 at 09:35 -0400, Rayon wrote: > > How do I Check for an active login session on every page that > requires > > authentication > > > To understand sessions you first need to understand that HTTP is a > stateless protocol: you connect, send your request, receive a response > and the connection is closed. > > There are several ways to accomplish this. The most straightforward way > would be remembering the client's IP > Another method is putting the session's ID in the URLs you display to > your users. > The most common and reliable choice is the good old session cookie While I agree with the cookie (as long as it has a short expiration), another way to do this is by using expiring tokenization (credentials + some unique data for the transaction) in the URL header (see section 14.8 at http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). Tokenization substitutes some random string for confidential data (such as credentials). The payment card industry uses this in the form of an authorization code for card transactions. Add to the data represented by the token some unique data (maybe a random number or some data from the last transaction - it doesn't matter as the token does not expose the data in any way) for each http transaction so you have unique token in each header and you can get an essentially stateful session with a method of checking authentication that has some spoof protection built in. Wrap it all in SSL/TLS and then you've got something. Granted, this requires some serious server side work, and is probably not a good beginner exercise, but if this level is what you need.... I have never coded anything like this in Python, but I can see abstractly how it could be done (I'm a novice with Python). If you're bored, you can read http://www.shift4.com/pdf/TokenizationWhitePaper.pdf especially sec1:7. Ok, Ok, I'll shut up now - I've got to go play with some XML anyhow...Thanks for listening. From alan.gauld at btinternet.com Tue Dec 8 10:27:44 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 8 Dec 2009 09:27:44 -0000 Subject: [Tutor] functions--how long is too long? References: Message-ID: "Che M" wrote > I have some functions that seem kind of long to me. One of them, with > I realize I can and should refactor parts that are used in other places > in the code, but I don't there are that many in some of these. Is > there a better way to think about organizing this? The length as such is not the critical factor, its the maintainability and readability. Can you function be easily understood by another programmer? Can you make changes to the function without changing the interface (which implies changes elsewhere in your code) Where you have a lot of if statements you might benefit from using a dictionary of functions or a function which returns a function. But there may be good reasons to keep the if statements too. Sorry, but a generalised question can only get a generalised answer. 57 lines of active code is big but not ridiculous. Over 100 liones I would be looking closely at breaking it up just for readability. Remember the old adage that a function should ideally fit on a single editor screen (or sheet of printout) - when I started that meant 25-60 lines was the range, now you can go up to 60-100 lines if needs be... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue Dec 8 11:17:13 2009 From: denis.spir at free.fr (spir) Date: Tue, 8 Dec 2009 11:17:13 +0100 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: Message-ID: <20091208111713.5f57e351@o> Luke Paireepinart dixit: > I'd say my personal hard-limit for functions before I start refactoring is > probably around 150-200 lines. But it's rare that functions get that long > anyway. ! Aside questions of personal style & taste, _I_ could not hold such long funcs. Far too much to manage for my head, about unmaintainable for me. My common limit is about one editor screen (~ 35 lines), comments included, which reveals not only practicle, but also about what my head is able to cope with at once. And my programming style rather leans toward clarity (--> low code density), meaning that 1 line of a "smart" coder may map to 2 or 3 of mine. An absolute limit would be about 2 screens. To sum up: 150 lines by luke * 2 (density) * 2 (comments) = 600 lines by myself ... compared to my ~ 50 lines medium limit means Luke is a ~ 12 times smarter coder than I am! ;-) This is a new evidence that coding efficiency is more a question of coder quality than anything else... Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From lie.1296 at gmail.com Tue Dec 8 11:27:42 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 08 Dec 2009 21:27:42 +1100 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: Message-ID: On 12/8/2009 8:27 PM, Alan Gauld wrote: > > Remember the old adage that a function should ideally fit on a single > editor screen (or sheet of printout) - when I started that meant > 25-60 lines was the range, now you can go up to 60-100 lines if needs be... I disagree. I keep my text editor not maximized so I can have multiple editors open (or multiple splits when using (g)vim); a 100-lines function would take the all the screen and doesn't fit in one window. I generally would consider something above ~50 lines as very large. A large screen is better utilized for multiple windows rather than for larger functions. While you should not refactor just for the sake of keeping line-counts, perhaps you should try the small editor approach. Keep your editor unmaximized, for around 20 lines, around half a screen. From davea at ieee.org Tue Dec 8 11:39:59 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 08 Dec 2009 05:39:59 -0500 Subject: [Tutor] loops In-Reply-To: <188020.25307.qm@web113212.mail.gq1.yahoo.com> References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> Message-ID: <4B1E2CFF.8060408@ieee.org> Richard Hultgren wrote: > a = 0 > b = 1 > count = 0 > max_count = 20 > while count < max_count: > count = count + 1 > # we need to keep track of a since we change it > old_a = a # especially here > old_b = b > a = old_b > b = old_a + old_b > # Notice that the , at the end of a print statement keeps it > # from switching to a new line > print old_a, > > > > > What's your question or comment? If you're just looking for a challenge, I'll point out that the same result could be gotten with 4 lines of Python. DaveA From lie.1296 at gmail.com Tue Dec 8 12:05:15 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 08 Dec 2009 22:05:15 +1100 Subject: [Tutor] loops In-Reply-To: <4B1E2CFF.8060408@ieee.org> References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> <4B1E2CFF.8060408@ieee.org> Message-ID: On 12/8/2009 9:39 PM, Dave Angel wrote: > Richard Hultgren wrote: >> a = 0 >> b = 1 >> count = 0 >> max_count = 20 >> while count < max_count: >> count = count + 1 >> # we need to keep track of a since we change it >> old_a = a # especially here >> old_b = b >> a = old_b >> b = old_a + old_b >> # Notice that the , at the end of a print statement keeps it >> # from switching to a new line >> print old_a, >> >> >> > What's your question or comment? > > If you're just looking for a challenge, I'll point out that the same > result could be gotten with 4 lines of Python. you lose, 1 line is all it takes for me: print "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181" From alan.gauld at btinternet.com Tue Dec 8 12:21:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 8 Dec 2009 11:21:20 -0000 Subject: [Tutor] Moving off topic: WAS Re: functions--how long is too long? References: <20091208111713.5f57e351@o> Message-ID: > This is a new evidence that coding efficiency is more a question of coder > quality than anything else... Don't know about "new", The book Peopleware was highlighting that fact 20 years ago! It also showed the type of environment that maximised the productivity of good programmers - sadly, most of that is still ignored in businesses today. But the real challenge is how to raise the standard of the average programmer to narrow the gap between the best and the norm. Higher level languages like Python are one way to do that. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Tue Dec 8 12:36:59 2009 From: denis.spir at free.fr (spir) Date: Tue, 8 Dec 2009 12:36:59 +0100 Subject: [Tutor] loops In-Reply-To: References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> <4B1E2CFF.8060408@ieee.org> Message-ID: <20091208123659.0d762354@o> Lie Ryan dixit: > On 12/8/2009 9:39 PM, Dave Angel wrote: > > Richard Hultgren wrote: > >> a = 0 > >> b = 1 > >> count = 0 > >> max_count = 20 > >> while count < max_count: > >> count = count + 1 > >> # we need to keep track of a since we change it > >> old_a = a # especially here > >> old_b = b > >> a = old_b > >> b = old_a + old_b > >> # Notice that the , at the end of a print statement keeps it > >> # from switching to a new line > >> print old_a, > >> > >> > >> > > What's your question or comment? > > > > If you're just looking for a challenge, I'll point out that the same > > result could be gotten with 4 lines of Python. > > you lose, 1 line is all it takes for me: > print "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181" you lose, I don't even need a one-liner python prog to write a text into whatever stdout may be -- except if remote ;-) Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From alan.gauld at btinternet.com Tue Dec 8 12:43:54 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 8 Dec 2009 11:43:54 -0000 Subject: [Tutor] functions--how long is too long? References: Message-ID: "Lie Ryan" wrote >> editor screen (or sheet of printout) - when I started that meant >> 25-60 lines was the range, now you can go up to 60-100 lines if needs >> be... > > I disagree. I keep my text editor not maximized so I can have multiple > editors open (or multiple splits when using (g)vim); a 100-lines function > would take the all the screen and doesn't fit in one window. Personally I tend to have about 35-40 lines available in my editor, but "if needs be" - which is what I said, an exceptional case, not normal - I can get >100 lines on screen(*) using a small font. Which means I can at a pinch see a full 100 line function on screen. I don't recommend it, but it can be done! (*)Using a 24inch monitor set to 1600x1200 resolution > While you should not refactor just for the sake of keeping line-counts, > perhaps you should try the small editor approach. Keep your editor > unmaximized, for around 20 lines, around half a screen. Hmm, I spent the first 10 years of my programming life using VT100 terminals with 24 lines max(*), I really don't want to go back there thanks! :-) (*)Actually I'm amazed how many Linux users with X Windows still bring up the default sized console/xterm with 24 lines. One of the first things I do on any new Linux install is set the xdefaults for xterm to 40 lines! (And change the colours!) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.plum at uni-koeln.de Tue Dec 8 13:56:35 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Tue, 08 Dec 2009 13:56:35 +0100 Subject: [Tutor] mod_python authentication In-Reply-To: <029001ca77bc$6d6c46d0$4844d470$@org> References: <029001ca77bc$6d6c46d0$4844d470$@org> Message-ID: <1260276995.3837.4.camel@kallisti> On Mo, 2009-12-07 at 23:10 -0500, Marc wrote: > While I agree with the cookie (as long as it has a short expiration), > another way to do this is by using expiring tokenization (credentials+ some > unique data for the transaction) in the URL header (see section 14.8 at > http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). Tokenization > substitutes some random string for confidential data (such as credentials). This is essentially what I said about passing variables in the URL. The important thing still is information hiding, though: you don't want the user to have any clue what those variables mean so they can't modify them too easily. This is also why you should never expose critical variables (e.g. user ID) to possible modification. > The payment card industry uses this in the form of an authorization code for > card transactions. Add to the data represented by the token some unique > data (maybe a random number or some data from the last transaction - it > doesn't matter as the token does not expose the data in any way) for each > http transaction so you have unique token in each header and you can get an > essentially stateful session with a method of checking authentication that > has some spoof protection built in. The problem to keep in mind, though, is that your users may be multi-tabbing, i.e. keeping several pages open at the same time. Don't ever rely on a linear experience unless you're dealing with something inherently linear like a survey (though in that case you should probably try to keep it to one page, possibly updated with JavaScript requests if the form doesn't look too friendly on one page) or a check-out process. > Wrap it all in SSL/TLS and then you've > got something. Granted, this requires some serious server side work, and is > probably not a good beginner exercise, but if this level is what you > need.... I have never coded anything like this in Python, but I can see > abstractly how it could be done (I'm a novice with Python). If you're bored, > you can read http://www.shift4.com/pdf/TokenizationWhitePaper.pdf especially > sec1:7. I agree that if you're going for high security/safety tokenization can help, but for most intents and purposes it really isn't necessary. The idea is to avoid re-submission (especially of counter-intuitively side-effect non-free actions like GET requests) and make each action uniquely identifiable, if I understand the method correctly. This is normally only necessary for things like checkouts (keep track of the order ID and make sure the customer can't accidentally place an order twice e.g. by double-clicking the submit button or re-clicking it if the connection hangs) or form submission that affects the server state (e.g. posting a message to a forum, sending an e-mail, ...). SSL/TLS is also usually overkill for most purposes. Certificates provided by many non-commercial authorities will still trigger security warnings in most browsers, so the only way to get one that isn't counter-productive (i.e. diminishes trust rather than increasing it -- dodgy certificates are still valid certificates) is to shell out the big money -- and unless you're into serious business (i.e. anything big enough to justify the expenses), you probably can't be arsed. Cheers, Alan Plum From rabidpoobear at gmail.com Tue Dec 8 17:14:50 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Tue, 8 Dec 2009 10:14:50 -0600 Subject: [Tutor] functions--how long is too long? In-Reply-To: <20091208111713.5f57e351@o> References: <20091208111713.5f57e351@o> Message-ID: On Tue, Dec 8, 2009 at 4:17 AM, spir wrote: > Luke Paireepinart dixit: > > > I'd say my personal hard-limit for functions before I start refactoring > is > > probably around 150-200 lines. But it's rare that functions get that > long > > anyway. > > ! > > Aside questions of personal style & taste, _I_ could not hold such long > funcs. Far too much to manage for my head, about unmaintainable for me. > > My common limit is about one editor screen (~ 35 lines), comments included, > which reveals not only practicle, but also about what my head is able to > cope with at once. And my programming style rather leans toward clarity (--> > low code density), meaning that 1 line of a "smart" coder may map to 2 or 3 > of mine. An absolute limit would be about 2 screens. > > To sum up: > 150 lines by luke > * 2 (density) > * 2 (comments) > = 600 lines by myself > The 150-200 lines was including comments and everything else. And I just meant that is the point where I would be forced to refactor. For shorter functions that are unclear I would refactor before I got to that many lines. But like I said, most functions I write just tend to be naturally much shorter than that anyway. I was just trying to point out that there is a point where you are forced to refactor just because of the size, but before that point, refactoring may actually make your functions more confusing and harder to follow. It's a situational thing, which is why I asked for code originally. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From roadierich at googlemail.com Tue Dec 8 17:18:46 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Tue, 8 Dec 2009 16:18:46 +0000 Subject: [Tutor] loops In-Reply-To: <20091208123659.0d762354@o> References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> <4B1E2CFF.8060408@ieee.org> <20091208123659.0d762354@o> Message-ID: 2009/12/8 spir : > Lie Ryan dixit: > >> On 12/8/2009 9:39 PM, Dave Angel wrote: >> > Richard Hultgren wrote: >> >> a = 0 >> >> b = 1 >> >> count = 0 >> >> max_count = 20 >> >> while count < max_count: >> >> count = count + 1 >> >> # we need to keep track of a since we change it >> >> old_a = a # especially here >> >> old_b = b >> >> a = old_b >> >> b = old_a + old_b >> >> # Notice that the , at the end of a print statement keeps it >> >> # from switching to a new line >> >> print old_a, >> >> >> >> >> >> >> > What's your question or comment? >> > >> > If you're just looking for a challenge, I'll point out that the same >> > result could be gotten with 4 lines of Python. >> >> you lose, 1 line is all it takes for me: >> print "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181" > > you lose, I don't even need a one-liner python prog to write a text into whatever stdout may be -- except if remote ;-) > > Denis > ________________________________ > > la vita e estrany > > http://spir.wikidot.com/ > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > This, of course is a rather dirty, implementation (and probably version) specific hack, but I can /calculate/ the sequence, using just one line: >>> print " ".join(str(i) for i in [x if x<2 else (locals()['_[1]'][-1]+locals()['_[1]'][-2]) for x in xrange(20)]) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 (CPython 2.6, on osx10.6) It's slightly more typing than the plain string, but extend it to about 30 entries, and I think I win? Note to OP: don't _ever_ do it this way in a serious project. -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From hultgren1946 at yahoo.com Tue Dec 8 16:36:08 2009 From: hultgren1946 at yahoo.com (Richard Hultgren) Date: Tue, 8 Dec 2009 07:36:08 -0800 (PST) Subject: [Tutor] Fw: loops Message-ID: <736572.25272.qm@web113206.mail.gq1.yahoo.com> ----- Forwarded Message ---- From: Richard Hultgren To: tutor at python.org Sent: Mon, December 7, 2009 2:53:40 PM Subject: loops I'm quite new but determined.? Can you explain to me, step by step, what is going on in the computer in this loop.? I hope I am not being too dumb! a = 0 b = 1 count = 0 max_count = 20 while count < max_count: ??? count = count + 1 ??? # we need to keep track of a since we change it ??? old_a = a????????????????# especially here ??? old_b = b ??? a = old_b ??? b = old_a + old_b ??? # Notice that the , at the end of a print statement keeps it ??? # from switching to a new line ??? print old_a, -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Tue Dec 8 20:44:16 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 8 Dec 2009 14:44:16 -0500 Subject: [Tutor] Fw: loops In-Reply-To: <736572.25272.qm@web113206.mail.gq1.yahoo.com> References: <736572.25272.qm@web113206.mail.gq1.yahoo.com> Message-ID: > ----- Forwarded Message ---- > From: Richard Hultgren > To: tutor at python.org > Sent: Mon, December 7, 2009 2:53:40 PM > Subject: loops > I'm quite new but determined.? Can you explain to me, step by step, what is > going on in the computer in this loop.? I hope I am not being too dumb! > Hmm...that still seems like quite a broad question. Can you be more specific about which portion you find confusing or unclear? If not, you might do better by starting with a general programming text. The top one is a list of texts for beginning programmers, followed by a few specific links to online texts: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers http://www.freenetpages.co.uk/hp/alan.gauld/ http://swaroopch.com/notes/Python HTH, Serdar From royhink at gmail.com Tue Dec 8 20:50:26 2009 From: royhink at gmail.com (Roy Hinkelman) Date: Tue, 8 Dec 2009 11:50:26 -0800 Subject: [Tutor] File renaming using os.rename problem Message-ID: I can't find anything on this error I am getting when renaming some files. I'm pulling info from a csv file and parsing it to build new file names. Any pointers appreciated Roy My code: # RENAME FILES using META file - new name = [place]_[state]_[sku].tif import re, os, csv # DEFINE _meta_file = "C:\\Documents and Settings\\rhinkelman\\My Documents\\My Dropbox\\Public\\Python code examples\\topo_meta_TEST.csv" _files_to_mod = "\\\\Dc2\\inetpub2\\Image Production\\missing_topo\\topo sz3\\test" _del_space = re.compile( ' ' ) #OPEN file containing TOPO meta, DEFINE OLD AND NEW NAMES _meta = csv.reader(open(_meta_file, "r")) for _row in _meta: if _row[0] == "NAME": continue print '|'.join(_row) # test old_name = _row[4].lstrip('o') + ".pdf" new_name = _row[0] + "_" + _row[1] + "_" + _row[4] + ".pdf" new_name = _del_space.sub( '_', new_name ) print old_name + " - " + new_name # test # OPEN DIR OF FILES TO BE RENAMED AND LOOK FOR NAME, RENAME AND CONTINUE for fname in os.listdir(_files_to_mod): if fname == old_name: print fname # test os.rename(fname, new_name) break else: continue AND the error >>> Aberdeen|CA|36.875|-118.250|o36118h3 36118h3.pdf - Aberdeen_CA_o36118h3.pdf 36118h3.pdf Traceback (most recent call last): File "C:\Documents and Settings\rhinkelman\My Documents\My Dropbox\Public\Python code examples\Rename_topo_files.py", line 25, in os.rename(fname, new_name) WindowsError: [Error 2] The system cannot find the file specified -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Tue Dec 8 21:06:32 2009 From: denis.spir at free.fr (spir) Date: Tue, 8 Dec 2009 21:06:32 +0100 Subject: [Tutor] File renaming using os.rename problem In-Reply-To: References: Message-ID: <20091208210632.4ee00f7e@o> Roy Hinkelman dixit: > I can't find anything on this error I am getting when renaming some files. > I'm pulling info from a csv file and parsing it to build new file names. > > Any pointers appreciated > > Roy > > My code: > # RENAME FILES using META file - new name = [place]_[state]_[sku].tif > > import re, os, csv > > # DEFINE > _meta_file = "C:\\Documents and Settings\\rhinkelman\\My Documents\\My > Dropbox\\Public\\Python code examples\\topo_meta_TEST.csv" > _files_to_mod = "\\\\Dc2\\inetpub2\\Image Production\\missing_topo\\topo > sz3\\test" > _del_space = re.compile( ' ' ) > > #OPEN file containing TOPO meta, DEFINE OLD AND NEW NAMES > _meta = csv.reader(open(_meta_file, "r")) > for _row in _meta: > if _row[0] == "NAME": > continue > print '|'.join(_row) # test > old_name = _row[4].lstrip('o') + ".pdf" > new_name = _row[0] + "_" + _row[1] + "_" + _row[4] + ".pdf" > new_name = _del_space.sub( '_', new_name ) > print old_name + " - " + new_name # test > > # OPEN DIR OF FILES TO BE RENAMED AND LOOK FOR NAME, RENAME AND CONTINUE > for fname in os.listdir(_files_to_mod): > if fname == old_name: > print fname # test > os.rename(fname, new_name) > break > else: > continue > > > AND the error > >>> > Aberdeen|CA|36.875|-118.250|o36118h3 > 36118h3.pdf - Aberdeen_CA_o36118h3.pdf > 36118h3.pdf > Traceback (most recent call last): > File "C:\Documents and Settings\rhinkelman\My Documents\My > Dropbox\Public\Python code examples\Rename_topo_files.py", line 25, in > > os.rename(fname, new_name) > WindowsError: [Error 2] The system cannot find the file specified Why don't you simply print out fname? This should point you to the error. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From royhink at gmail.com Tue Dec 8 21:27:31 2009 From: royhink at gmail.com (Roy Hinkelman) Date: Tue, 8 Dec 2009 12:27:31 -0800 Subject: [Tutor] File renaming using os.rename problem (spir) Message-ID: Why don't you simply print out fname? This should point you to the error. Denis I did here: > if fname == old_name: > print fname # test and it looks correct. On an WinXP, should I use shutil instead? Roy -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Tue Dec 8 21:47:02 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 08 Dec 2009 15:47:02 -0500 Subject: [Tutor] File renaming using os.rename problem In-Reply-To: <20091208210632.4ee00f7e@o> References: <20091208210632.4ee00f7e@o> Message-ID: <4B1EBB46.4090603@ieee.org> spir wrote: > Roy Hinkelman dixit: > > >> I can't find anything on this error I am getting when renaming some files. >> I'm pulling info from a csv file and parsing it to build new file names. >> >> Any pointers appreciated >> >> Roy >> >> My code: >> # RENAME FILES using META file - new name = [place]_[state]_[sku].tif >> >> import re, os, csv >> >> # DEFINE >> _meta_file = "C:\\Documents and Settings\\rhinkelman\\My Documents\\My >> Dropbox\\Public\\Python code examples\\topo_meta_TEST.csv" >> _files_to_mod = "\\\\Dc2\\inetpub2\\Image Production\\missing_topo\\topo >> sz3\\test" >> _del_space = re.compile( ' ' ) >> >> #OPEN file containing TOPO meta, DEFINE OLD AND NEW NAMES >> _meta = csv.reader(open(_meta_file, "r")) >> for _row in _meta: >> if _row[0] == "NAME": >> continue >> print '|'.join(_row) # test >> old_name = _row[4].lstrip('o') + ".pdf" >> new_name = _row[0] + "_" + _row[1] + "_" + _row[4] + ".pdf" >> new_name = _del_space.sub( '_', new_name ) >> print old_name + " - " + new_name # test >> >> # OPEN DIR OF FILES TO BE RENAMED AND LOOK FOR NAME, RENAME AND CONTINUE >> for fname in os.listdir(_files_to_mod): >> if fname == old_name: >> print fname # test >> os.rename(fname, new_name) >> break >> else: >> continue >> >> >> AND the error >> >> Aberdeen|CA|36.875|-118.250|o36118h3 >> 36118h3.pdf - Aberdeen_CA_o36118h3.pdf >> 36118h3.pdf >> Traceback (most recent call last): >> File "C:\Documents and Settings\rhinkelman\My Documents\My >> Dropbox\Public\Python code examples\Rename_topo_files.py", line 25, in >> >> os.rename(fname, new_name) >> WindowsError: [Error 2] The system cannot find the file specified >> > > Why don't you simply print out fname? This should point you to the error. > > Denis > ________________________________ > > la vita e estrany > > http://spir.wikidot.com/ > > Actually he did, in the line immediately before the os.rename() call. See the line just before the traceback 36118h3.pdf Roy, your problem is that you've confirmed that the file 36118h3.pdf exists in the "test" directory (specified in _files_to_mod), but you're trying to rename a different file in the current directory. Your call to os.rename() needs to have a full path to the actual file. See os.path.join() to build such. DaveA From christopher.henk at allisontransmission.com Tue Dec 8 21:59:33 2009 From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com) Date: Tue, 8 Dec 2009 15:59:33 -0500 Subject: [Tutor] Fw: loops In-Reply-To: <736572.25272.qm@web113206.mail.gq1.yahoo.com> Message-ID: You should probably read some of the links sent to you earlier but here is a stab at an explanation. Richard Hultgren wrote on 12/08/2009 10:36:08 AM: > ----- Forwarded Message ---- > From: Richard Hultgren > To: tutor at python.org > Sent: Mon, December 7, 2009 2:53:40 PM > Subject: loops > I'm quite new but determined. Can you explain to me, step by step, what is going on in the computer in this loop. I > hope I am not being too dumb! > a = 0 > b = 1 > count = 0 > max_count = 20 The above steps are initialization of your variables. 'a' points to the value 0, 'b' points to 1, etc. Since the values of the variables are used within the loop we need to give it somewhere to start, otherwise we will get an error. > while count < max_count: The while loop will continue until 'count < max_count' evaluates to False (20 loops, due to line below) the loop is defined as everything that is indented below this line. > count = count + 1 Increase the value of count by one each loop iteration, otherwise the statement 'count < max_count' will never change value, and the loop will never end. > # we need to keep track of a since we change it > old_a = a # especially here > old_b = b Keep the old values of the variables since we want to add them together for the new value of b and will change the values in the next steps. > a = old_b > b = old_a + old_b reassign 'a' to the value of 'old_b'. If we didn't save the value of 'a' into 'old_a' above we wouldn't know what it was anymore. reassign 'b' to the sum of 'old_a' and 'old_b' > # Notice that the , at the end of a print statement keeps it > # from switching to a new line > print old_a, prints the value of old_a followed by a space. Without the comma at the end the print statement each loop would print on a new line. The best way to follow what is happening is to trace the variables through the program. you would get a table that loops something like : max_count count a b old_a old_b count From royhink at gmail.com Tue Dec 8 23:55:13 2009 From: royhink at gmail.com (Roy Hinkelman) Date: Tue, 8 Dec 2009 14:55:13 -0800 Subject: [Tutor] File renaming using os.rename problem (spir) In-Reply-To: References: Message-ID: I got it. Switched to shutil and made to paths complete paths. The added benefit is that it didn't trash the original file. shutil.copy2(_files_to_mod + "\\" + fname, _files_to_mod + "\\" + new_name) Thanks. ---------- Forwarded message ---------- From: Roy Hinkelman Date: Tue, Dec 8, 2009 at 12:27 PM Subject: Re: File renaming using os.rename problem (spir) To: tutor at python.org Why don't you simply print out fname? This should point you to the error. Denis I did here: > if fname == old_name: > print fname # test and it looks correct. On an WinXP, should I use shutil instead? Roy -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Wed Dec 9 00:02:50 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 8 Dec 2009 18:02:50 -0500 Subject: [Tutor] duplication in unit tests Message-ID: Hi everyone, I'm trying to apply some lessons from the recent list discussions on unit testing and Test-Driven Development, but I seem to have hit a sticking point. As part of my program, I'm planning to create objects that perform some initial data clean-up and then parse and database the cleaned data. Currently I'm expecting to have a FileCleaner and Parser classes. Using the TDD approach, I've so far come up with the below: class FileCleaner(object): def __init__(self, datastring): self.source = datastring def convertEmDashes(self): """Convert unicode emdashes to minus signs""" self.datastring = self.source.replace(u'\u2014','-') def splitLines(self): """Generate and store a list of cleaned, non-empty lines""" self.data = [x.strip() for x in self.datastring.strip().split('\n') if x.strip()] My confusion involves the test code for the above class and its methods. The only way I can get splitLines to pass its unit test is by first calling the convertEmDashes method, and then splitLines. class TestFileCleaner(unittest.TestCase): def setUp(self): self.sourcestring = u"""This line has an em\u2014dash.\n So does this \u2014\n.""" self.cleaner = FileCleaner(self.sourcestring) def test_convertEmDashes(self): """convertEmDashes should remove minus signs from datastring attribute""" teststring = self.sourcestring.replace(u'\u2014','-') self.cleaner.convertEmDashes() self.assertEqual(teststring, self.cleaner.datastring) def test_splitLines(self): """splitLines should create a list of cleaned lines""" teststring = self.sourcestring.replace(u'\u2014','-') data = [x.strip() for x in teststring.strip().split('\n') if x.strip()] self.cleaner.convertEmDashes() self.cleaner.splitLines() self.assertEqual(data, self.cleaner.data) Basically, I'm duplicating the steps from the first test method in the second test method (and this duplication will accrue as I add more "cleaning" methods). I understand that TestCase's setUp method is called before each test is run (and therefore the FileCleaner object is created anew), but this coupling of a test to other methods of the class under test seems to violate the principle of testing methods in isolation. So my questions -- Am I misunderstanding how to properly write unit tests for this case? Or perhaps I've structured my program incorrectly, and that's what this duplication reveals? I suspected, for instance, that perhaps I should group these methods (convertEmDashes, splitLines, etc.) into a single larger function or method. But that approach seems to violate the "best practice" of writing small methods. As you can tell, I'm a bit at sea on this. Your guidance is greatly appreciated!! Regards, Serdar ps - recommendations on cleaning up and restructuring code are also welcome! From lie.1296 at gmail.com Wed Dec 9 00:18:36 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 09 Dec 2009 10:18:36 +1100 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: Message-ID: On 12/8/2009 10:43 PM, Alan Gauld wrote: > > >> While you should not refactor just for the sake of keeping >> line-counts, perhaps you should try the small editor approach. Keep >> your editor unmaximized, for around 20 lines, around half a screen. > > Hmm, I spent the first 10 years of my programming life using VT100 > terminals > with 24 lines max(*), I really don't want to go back there thanks! :-) Whoaa... who puts 20 there!! A doppelganger of me? Ignore that. Half a screenful would be more than 20 lines even with laptop widescreen. From kent37 at tds.net Wed Dec 9 00:43:10 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 8 Dec 2009 18:43:10 -0500 Subject: [Tutor] duplication in unit tests In-Reply-To: References: Message-ID: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> On Tue, Dec 8, 2009 at 6:02 PM, Serdar Tumgoren wrote: > As part of my program, I'm planning to create objects that perform > some initial data clean-up and then parse and database the cleaned > data. Currently I'm expecting to have a FileCleaner and Parser > classes. Using the TDD approach, I've so far come up with the below: > > class FileCleaner(object): > ? ?def __init__(self, datastring): > ? ? ? ?self.source = datastring > > ? ?def convertEmDashes(self): > ? ? ? ?"""Convert unicode emdashes to minus signs""" > ? ? ? ?self.datastring = self.source.replace(u'\u2014','-') > > ? ?def splitLines(self): > ? ? ? ?"""Generate and store a list of cleaned, non-empty lines""" > ? ? ? ?self.data = [x.strip() for x in > self.datastring.strip().split('\n') if x.strip()] > > > My confusion involves the test code for the above class and its > methods. The only way I can get splitLines to pass its unit test is by > first calling the convertEmDashes method, and then splitLines. > > class TestFileCleaner(unittest.TestCase): > ? ?def setUp(self): > ? ? ? ?self.sourcestring = u"""This ? ?line ? has an em\u2014dash.\n > ? ? ? ? ? ? ? ?So ? does this ?\u2014\n.""" > ? ? ? ?self.cleaner = FileCleaner(self.sourcestring) > > ? ?def test_convertEmDashes(self): > ? ? ? ?"""convertEmDashes should remove minus signs from datastring > attribute""" > ? ? ? ?teststring = self.sourcestring.replace(u'\u2014','-') > ? ? ? ?self.cleaner.convertEmDashes() > ? ? ? ?self.assertEqual(teststring, self.cleaner.datastring) > > ? ?def test_splitLines(self): > ? ? ? ?"""splitLines should create a list of cleaned lines""" > ? ? ? ?teststring = self.sourcestring.replace(u'\u2014','-') > ? ? ? ?data = [x.strip() for x in teststring.strip().split('\n') if x.strip()] > ? ? ? ?self.cleaner.convertEmDashes() > ? ? ? ?self.cleaner.splitLines() > ? ? ? ?self.assertEqual(data, self.cleaner.data) > > Basically, I'm duplicating the steps from the first test method in the > second test method (and this duplication will accrue as I add more > "cleaning" methods). I see a few problems with this. You are confused about what splitLines() does. It does not create a list of cleaned lines, it just splits the lines. Because of your confusion about splitLines(), your test is not just testing splitLines(), it is testing convertEmDashes() and splitLines(). That is why you have code duplication. test_splitLines() could look like this: def test_splitLines(self): """splitLines should create a list of split lines""" teststring = self.sourcestring data = [x.strip() for x in teststring.strip().split('\n') if x.strip()] self.cleaner.splitLines() self.assertEqual(data, self.cleaner.data) Your tests are not very good. They don't really test anything because they use the same code that you are trying to test. What if str.replace() or split() or strip() does not work the way you expect? Your tests would not discover this. You should just hard-code the expected result strings. I would write test_splitLines() like this: def test_splitLines(self): """splitLines should create a list of split lines""" data = [u"This line has an em\u2014dash.", u"So does this \u2014", u"."] self.cleaner.splitLines() self.assertEqual(data, self.cleaner.data) You probably don't want to hard-code the source string in the setup method. Typically you want to test a function with multiple inputs so you can check its behaviour with typical values, edge cases and invalid input. For example test_splitLines() doesn't verify that splitLines() removes blank lines; that would be a good test case. You might make a list of pairs of (input value, expected result) and pass each one to splitLines(). > So my questions -- Am I misunderstanding how to properly write unit > tests for this case? Or perhaps I've structured my program > incorrectly, and that's what this duplication reveals? I suspected, > for instance, that perhaps I should group these methods > (convertEmDashes, splitLines, etc.) into a single larger function or > method. Yes, your tests are revealing a problem with the structure. You should probably have a single process() method that does all the cleanup methods and the split. Then you could also have a test for this. There is really no need for a class here. You could write separate functions for each cleanup and for the split, then another function that puts them all together. This would be easier to test, too. Kent From sander.sweers at gmail.com Wed Dec 9 00:50:15 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 09 Dec 2009 00:50:15 +0100 Subject: [Tutor] File renaming using os.rename problem (spir) In-Reply-To: References: Message-ID: <1260316215.5789.52.camel@infirit.homelinux.org> On Tue, 2009-12-08 at 14:55 -0800, Roy Hinkelman wrote: > shutil.copy2(_files_to_mod + "\\" + fname, _files_to_mod + "\\" + > new_name) You can make os.path.join sort out the directory seprator for you. It will add a / under linux and \ under windows. >>> os.path.join('Testing dir','oldname dir','filename') 'Testing dir/oldname dir/filename' So the code above can be like, import os oldname = os.path.join(_files_to_mod, fname) newname = os.path.join(_files_to_mod, new_name) shutil.copy2(oldname, newname) Not sure if it is a concern in your case but as far as I know shutil.copy2 will overwrite any existing files with the new_name without warning. Have a look at [1] for a great explanation on os.path. Greets Sander [1] http://blog.doughellmann.com/2008/01/pymotw-ospath.html From lie.1296 at gmail.com Wed Dec 9 01:02:08 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 09 Dec 2009 11:02:08 +1100 Subject: [Tutor] loops In-Reply-To: References: <188020.25307.qm@web113212.mail.gq1.yahoo.com> <4B1E2CFF.8060408@ieee.org> <20091208123659.0d762354@o> Message-ID: On 12/9/2009 3:18 AM, Rich Lovely wrote: > 2009/12/8 spir: > > This, of course is a rather dirty, implementation (and probably > version) specific hack, but I can /calculate/ the sequence, using just > one line: > >>>> print " ".join(str(i) for i in [x if x<2 else (locals()['_[1]'][-1]+locals()['_[1]'][-2]) for x in xrange(20)]) > 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 > > (CPython 2.6, on osx10.6) the best one-liner golf score for now (83 chars, requires python3), quite obfuscated: (lambda*n:n[0](*n))(lambda f,n,a=0,b=1:n<0 or print(a,end=' ')or f(f,n-1,b,a+b),19) and though not a pure one-liner, the most "straightforward" method is just a char away (84 chars, python2): a=[0,1];[a.append(sum(a[-2:]))for x in range(3)][0];print' '.join(str(x)for x in a) if you don't require one-liner you can make it even shorter (81 chars, '\n'==1char, requires python2) a=[0,1] for x in range(18):a.append(sum(a[-2:])) print' '.join(str(x) for x in a) All codes tested on command-line with -c argument (i.e. the shell's default repr()-ing of expressions is ignored), result must be printed to screen as numbers separated by space (printing with list repr, e.g. [0, 1, ...] is not acceptable). Trailing space is ignored. > It's slightly more typing than the plain string, but extend it to > about 30 entries, and I think I win? Think again... > Note to OP: don't _ever_ do it this way in a serious project. OP: In a real project, readability counts. Inglfng,spcecnts. PS: Perl fans might complain that their favorite pastime is stolen From alan.gauld at btinternet.com Wed Dec 9 01:15:50 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 9 Dec 2009 00:15:50 -0000 Subject: [Tutor] Fw: loops References: <736572.25272.qm@web113206.mail.gq1.yahoo.com> Message-ID: "Serdar Tumgoren" wrote http://www.freenetpages.co.uk/hp/alan.gauld/ Note the new URL in my sig. Freenet are due to close this site "soon". Its been locked for over a year. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From lie.1296 at gmail.com Wed Dec 9 01:14:55 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 09 Dec 2009 11:14:55 +1100 Subject: [Tutor] duplication in unit tests In-Reply-To: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> Message-ID: On 12/9/2009 10:43 AM, Kent Johnson wrote: >> So my questions -- Am I misunderstanding how to properly write unit >> tests for this case? Or perhaps I've structured my program >> incorrectly, and that's what this duplication reveals? I suspected, >> for instance, that perhaps I should group these methods >> (convertEmDashes, splitLines, etc.) into a single larger function or >> method. > > Yes, your tests are revealing a problem with the structure. You should > probably have a single process() method that does all the cleanup > methods and the split. Then you could also have a test for this. I should add, a unittest can be a white-box testing. You can have TestCases for the whole "process" (blackbox test), but you can also have TestCases for each splitLine, convertEmDashes, etc (whitebox test). The test for the large "process" will be, sort of, a simple integration test for each sub-processes. From zstumgoren at gmail.com Wed Dec 9 04:11:56 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 8 Dec 2009 22:11:56 -0500 Subject: [Tutor] duplication in unit tests In-Reply-To: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> Message-ID: Hi Kent and Lie, First, thanks to you both for the help. I reworked the tests and then the main code according to your suggestions (I really was muddling these TDD concepts!). The reworked code and tests are below. In the tests, I hard-coded the source data and the expected results; in the main program code, I eliminated the FileCleaner class and converted its methods to stand-alone functions. I'm planning to group them into a single, larger "process" function as you all suggested. Meantime, I'd be grateful if you could critique whether I've properly followed your advice. And of course, feel free to suggest other tests that might be appropriate. For instance, would it make sense to test convertEmDashes for non-unicode input? Thanks again! Serdar #### test_cleaner.py #### from cleaner import convertEmDashes, splitLines class TestCleanerMethods(unittest.TestCase): def test_convertEmDashes(self): """convertEmDashes to minus signs""" srce = u"""This line has an em\u2014dash.\nSo does this \u2014.\n""" expected = u"""This line has an em-dash.\nSo does this -.\n""" result = convertEmDashes(srce) self.assertEqual(result, expected) def test_splitLines(self): """splitLines should create a list of cleaned lines""" srce = u"""This line has an em\u2014dash.\nSo does this \u2014.\n""" expected = [u'This line has an em\u2014dash.', u'So does this \u2014.'] result = splitLines(srce) self.assertEqual(result, expected) #### cleaner.py #### def convertEmDashes(datastring): """Convert unicode emdashes to minus signs""" datastring = datastring.replace(u'\u2014','-') return datastring def splitLines(datastring): """Generate list of cleaned lines""" data = [x.strip() for x in datastring.strip().split('\n') if x.strip()] return data From pine508 at hotmail.com Wed Dec 9 04:19:33 2009 From: pine508 at hotmail.com (Che M) Date: Tue, 8 Dec 2009 22:19:33 -0500 Subject: [Tutor] functions--how long is too long? In-Reply-To: References: , , Message-ID: Thank you to all who replied. That does help me get a better idea of all this. I think if I apply a number of the thoughts expressed I can come to a good, readable re-do of these longer functions. Che _________________________________________________________________ Chat with Messenger straight from your Hotmail inbox. http://www.microsoft.com/windows/windowslive/hotmail_bl1/hotmail_bl1.aspx?ocid=PID23879::T:WLMTAGL:ON:WL:en-ww:WM_IMHM_4:092009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Dec 9 04:46:45 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 8 Dec 2009 22:46:45 -0500 Subject: [Tutor] duplication in unit tests In-Reply-To: References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> Message-ID: <1c2a2c590912081946r50dab0c5t677bba98672b9e20@mail.gmail.com> On Tue, Dec 8, 2009 at 10:11 PM, Serdar Tumgoren wrote: > Hi Kent and Lie, > > First, thanks to you both for the help. I reworked the tests and then > the main code according to your suggestions (I really was muddling > these TDD concepts!). > > The reworked code and tests are below. In the tests, I hard-coded the > source data and the expected results; in the main program code, I > eliminated the FileCleaner class and converted its methods to > stand-alone functions. I'm planning to group them into a single, > larger "process" function as you all suggested. > > Meantime, I'd be grateful if you could critique whether I've properly > followed your advice. Yes, this is much better. Notice how much less code it is! :-) > And of course, feel free to suggest other tests > that might be appropriate. For instance, would it make sense to test > convertEmDashes for non-unicode input? If you expect unicode input then it makes sense to test for it. If you don't expect unicode input, it might make sense to test for an expected error - how do you want the function to behave with invalid inputs? You could add other tests as well, for example does it work if there are two dashes in a row? Does splitLines() correctly remove blank lines? These are simple functions but the idea is to think of all the desired behaviours and write test cases to cover them. By the way I applaud your effort, unit testing is a valuable skill. Kent From faisal.moledina at gmail.com Wed Dec 9 06:03:52 2009 From: faisal.moledina at gmail.com (Faisal Moledina) Date: Wed, 9 Dec 2009 00:03:52 -0500 Subject: [Tutor] numerical simulation + SQLite In-Reply-To: <946A027A-9664-46DC-BEDD-7285344C9305@gmail.com> References: <408c7ce20911301047i3cb3e684xf012765668aa6947@mail.gmail.com> <4B1475E1.9060706@gmail.com> <946A027A-9664-46DC-BEDD-7285344C9305@gmail.com> Message-ID: <408c7ce20912082103i6a53760bub9546168647b1e09@mail.gmail.com> On Tue, Dec 1, 2009 at 11:48 AM, Faisal Moledina wrote: > Eike Welk wrote: >> Just in case you don't know it, maybe Pytables is the right solution >> for you. It is a disk storage library specially for scientific >> applications: >> http://www.pytables.org/moin > > Wow, that looks pretty good. I work with a lot of numpy.array's in this simulation so I'll definitely look into that. For those of you following along at home, my problem has been solved with Pytables. Discussion available at http://www.mail-archive.com/pytables-users at lists.sourceforge.net/msg01416.html on the Pytables-users list. Thanks again, Eike. Faisal From emailkgnow at gmail.com Wed Dec 9 06:37:34 2009 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Wed, 9 Dec 2009 08:37:34 +0300 Subject: [Tutor] What books do you recommend? Message-ID: Hi, I wan't to buy some books about python 3. Do you have any recommendations? I started with no previous programming experience, and I've finished a few tutorials and I guess I can be considered a beginner. My problem, though, is I still find it difficult to write meaningful code or use the built in libraries effectively and/or correctly because I can't find example code to mimic. I tried sifting through ActiveState recipes page, but most of the code seems uninteresting or useful only if utilized in a bigger project. I hope you have some suggestions. Thanks a lot. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Dec 9 07:19:35 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 09 Dec 2009 01:19:35 -0500 Subject: [Tutor] duplication in unit tests In-Reply-To: References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> Message-ID: <4B1F4177.6080101@ieee.org> Serdar Tumgoren wrote: > Hi Kent and Lie, > > First, thanks to you both for the help. I reworked the tests and then > the main code according to your suggestions (I really was muddling > these TDD concepts!). > > The reworked code and tests are below. In the tests, I hard-coded the > source data and the expected results; in the main program code, I > eliminated the FileCleaner class and converted its methods to > stand-alone functions. I'm planning to group them into a single, > larger "process" function as you all suggested. > > Meantime, I'd be grateful if you could critique whether I've properly > followed your advice. And of course, feel free to suggest other tests > that might be appropriate. For instance, would it make sense to test > convertEmDashes for non-unicode input? > > Thanks again! > Serdar > > #### test_cleaner.py #### > from cleaner import convertEmDashes, splitLines > > class TestCleanerMethods(unittest.TestCase): > def test_convertEmDashes(self): > """convertEmDashes to minus signs""" > srce = u"""This line has an em\u2014dash.\nSo does this > \u2014.\n""" > expected = u"""This line has an em-dash.\nSo does this -.\n""" > result = convertEmDashes(srce) > self.assertEqual(result, expected) > > def test_splitLines(self): > """splitLines should create a list of cleaned lines""" > srce = u"""This line has an em\u2014dash.\nSo does this > \u2014.\n""" > expected = [u'This line has an em\u2014dash.', u'So > does this \u2014.'] > result = splitLines(srce) > self.assertEqual(result, expected) > > > #### cleaner.py #### > def convertEmDashes(datastring): > """Convert unicode emdashes to minus signs""" > datastring = datastring.replace(u'\u2014','-') > I think the 'dash' should be a unicode one, at least if you're expecting the datastring to be unicode. datastring = datastring.replace(u'\u2014',u'-') It will probably be slightly more efficient, but more importantly, it'll make it clear what you're expecting. > return datastring > > def splitLines(datastring): > """Generate list of cleaned lines""" > data = [x.strip() for x in datastring.strip().split('\n') if x.strip()] > return data > > And in both these functions, the doc string doesn't reflect the function very well (any more). They both should indicate what kind of data they expect (unicode?), and the latter one should not say that the lines are cleaned. What it should say is that the lines in the list have no leading or trailing whitespace, and that blank lines are dropped. Once you have multiple "cleanup" functions, the unit tests become much more important. For example, the order of application of the cleanups could matter a lot. And pretty soon you'll have to document just what your public interface is. If your "user" may only call the overall cleanup() function, then blackbox testing only needs to examine that one, and whitebox testing can deal with the functions entirely independently. DaveA From denis.spir at free.fr Wed Dec 9 11:52:58 2009 From: denis.spir at free.fr (spir) Date: Wed, 9 Dec 2009 11:52:58 +0100 Subject: [Tutor] File renaming using os.rename problem (spir) In-Reply-To: References: Message-ID: <20091209115258.533b82fb@o> Roy Hinkelman dixit: > Why don't you simply print out fname? This should point you to the error. > > Denis > > > I did here: > > if fname == old_name: > > print fname # test > > and it looks correct. > > On an WinXP, should I use shutil instead? > > Roy Sorry Roy, I read your post too fast. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Wed Dec 9 12:26:55 2009 From: denis.spir at free.fr (spir) Date: Wed, 9 Dec 2009 12:26:55 +0100 Subject: [Tutor] duplication in unit tests In-Reply-To: References: Message-ID: <20091209122655.65607a96@o> Serdar Tumgoren dixit: > Hi everyone, > I'm trying to apply some lessons from the recent list discussions on > unit testing and Test-Driven Development, but I seem to have hit a > sticking point. > > As part of my program, I'm planning to create objects that perform > some initial data clean-up and then parse and database the cleaned > data. Currently I'm expecting to have a FileCleaner and Parser > classes. Using the TDD approach, I've so far come up with the below: > > class FileCleaner(object): > def __init__(self, datastring): > self.source = datastring > > def convertEmDashes(self): > """Convert unicode emdashes to minus signs""" > self.datastring = self.source.replace(u'\u2014','-') > > def splitLines(self): > """Generate and store a list of cleaned, non-empty lines""" > self.data = [x.strip() for x in > self.datastring.strip().split('\n') if x.strip()] > > > My confusion involves the test code for the above class and its > methods. The only way I can get splitLines to pass its unit test is by > first calling the convertEmDashes method, and then splitLines. > > class TestFileCleaner(unittest.TestCase): > def setUp(self): > self.sourcestring = u"""This line has an em\u2014dash.\n > So does this \u2014\n.""" > self.cleaner = FileCleaner(self.sourcestring) > > def test_convertEmDashes(self): > """convertEmDashes should remove minus signs from datastring > attribute""" > teststring = self.sourcestring.replace(u'\u2014','-') > self.cleaner.convertEmDashes() > self.assertEqual(teststring, self.cleaner.datastring) > > def test_splitLines(self): > """splitLines should create a list of cleaned lines""" > teststring = self.sourcestring.replace(u'\u2014','-') > data = [x.strip() for x in teststring.strip().split('\n') if x.strip()] > self.cleaner.convertEmDashes() > self.cleaner.splitLines() > self.assertEqual(data, self.cleaner.data) > > Basically, I'm duplicating the steps from the first test method in the > second test method (and this duplication will accrue as I add more > "cleaning" methods). > > I understand that TestCase's setUp method is called before each test > is run (and therefore the FileCleaner object is created anew), but > this coupling of a test to other methods of the class under test seems > to violate the principle of testing methods in isolation. > > So my questions -- Am I misunderstanding how to properly write unit > tests for this case? Or perhaps I've structured my program > incorrectly, and that's what this duplication reveals? I suspected, > for instance, that perhaps I should group these methods > (convertEmDashes, splitLines, etc.) into a single larger function or > method. > > But that approach seems to violate the "best practice" of writing > small methods. As you can tell, I'm a bit at sea on this. Your > guidance is greatly appreciated!! > > Regards, > Serdar > > ps - recommendations on cleaning up and restructuring code are also welcome! Hello, I guess you're first confused at the design level of your app. Test and design both require you to clearly express your expectations. Here, the cleanup phase may be written as follow (I don't mean it's particuliarly good, just an example): plain source data = input --> output = ready-to-process data As you see, this requirement is, conceptually speaking, a purely function-al one; in the plain sense of the word "function". At least, this is the way I see it. Building an object to implement it is imo a wrong interpretation of OO design. (It's also writing java in python ;-) I would rather chose to write it as a method of a higher-level object. Possibly, this method would split into smaller ones if needed. Then, expressing your tests is in a sense translating the requirement above into code: feeding the piece of code to be tested with raw input data and checking the output is as expected. As well expressed by Kent, you should test with typical, edge, *and wrong* input; in the latter case the test is expected to fail. You will have to hand-write or automatically produce input strings for each test. If the func is split, then you will have to do it for each mini-func to be tested. This can be rather unpleasant, especially in cases like yours where funcs look like logically operating in sequence, but there is no way to escape. Actually, the several cleanup tasks (translating special chars, skipping blank lines, etc...) are rather orthogonal: they don't need to be tested in sequence. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From pine508 at hotmail.com Wed Dec 9 17:29:17 2009 From: pine508 at hotmail.com (Che M) Date: Wed, 9 Dec 2009 11:29:17 -0500 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: > My problem, though, is I still find it difficult to write meaningful code or use the built in libraries > effectively and/or correctly because I can't find example code to mimic. I tried sifting through > ActiveState recipes page, but most of the code seems uninteresting or useful only if utilized > in a bigger project. What do you mean by "meaningful" code? I think that might be your issue. What I'd recommend is to figure out *what you want to accomplish*. Python is just a means to accomplish something, but what matters is the accomplishment. You may want to write a GUI desktop app for a specific purpose. You may want to create a web-based app. You may want to write code to process information, scrape web sites...create a game, create some kind of tool. Once you decide on that, you will be more focused on what you need to learn. If, for example, you need to have persistent storage of information, you then might want to read up on databases and perhaps SQLite in Python. Etc. Then you will find code that will be applicable to your concerns, and help you learn. I feel that learning something "in a vacuum", unrelated to some personal creative goal, just doesn't work well. Che _________________________________________________________________ Get gifts for them and cashback for you. Try Bing now. http://www.bing.com/shopping/search?q=xbox+games&scope=cashback&form=MSHYCB&publ=WLHMTAG&crea=TEXT_MSHYCB_Shopping_Giftsforthem_cashback_1x1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From jfabiani at yolo.com Wed Dec 9 17:33:38 2009 From: jfabiani at yolo.com (John) Date: Wed, 9 Dec 2009 08:33:38 -0800 Subject: [Tutor] win32com and ocx with properties Message-ID: <200912090833.38712.jfabiani@yolo.com> I realize that some may consider this an advance question. But there are many here that are advance. So I'm hoping some nice soul will help me. I'm attempting to use a OCX designed to talk with QuickBooks. I'm using win32com for the first time and have discovered an issue that I'm sure others have run into. But researching google etc. has not helped. obj = win32com.client.Dispatch("InQB.Invoice.1") #In the VB code #obj.ItemName(0) = 'string' #in python I'm trying obj.ItemName[0] = 'string' #error is "Instancemethod object does not support item assignment" I found and ran makepy.py and here is the code it created dealing with my obj. # The method ItemName is actually a property, but must be used as a method to correctly pass the arguments def ItemName(self, ItemIndex=defaultNamedNotOptArg): ? ? ? ? """Line item property: Reference to the kind of item.""" ? ? ? ? # Result is a Unicode object ? ? ? ? return self._oleobj_.InvokeTypes(19, LCID, 2, (8, 0), ((3,1),),ItemIndex ) As I read the above code it can only return a string not assign anything. I'm not sure what "InvokeTypes" does. So the question is how can I get around this issue? I'm sure others need to set properties in com objects. BTW does anyone know of a win32com forum or list? Johnf From RWeidner at ea.com Wed Dec 9 17:44:16 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Wed, 9 Dec 2009 08:44:16 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: > My problem, though, is I still find it difficult to write meaningful code or use the built in libraries > effectively and/or correctly because I can't find example code to mimic. I tried sifting through > ActiveState recipes page, but most of the code seems uninteresting or useful only if utilized > in a bigger project. What do you mean by "meaningful" code? I think that might be your issue. What I'd recommend is to figure out *what you want to accomplish*. Python is just a means to accomplish something, but what matters is the accomplishment. You may want to write a GUI desktop app for a specific purpose. You may want to create a web-based app. You may want to write code to process information, scrape web sites...create a game, create some kind of tool. Once you decide on that, you will be more focused on what you need to learn. If, for example, you need to have persistent storage of information, you then might want to read up on databases and perhaps SQLite in Python. Etc. Then you will find code that will be applicable to your concerns, and help you learn. I feel that learning something "in a vacuum", unrelated to some personal creative goal, just doesn't work well. Che I agree with Che. For me, picking a project then completing it is the best way for me to learn a language. That said this link is a great resource in my opinion... http://www.diveintopython.org/ The book there is fantastic and all the links on the right side of the page lead to other great resources. The only resource that I feel was left out was this one. I've learned a lot from this site too. http://www.uselesspython.com/ Good luck... -- Ronald Weidner -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Wed Dec 9 18:02:38 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Wed, 9 Dec 2009 12:02:38 -0500 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: > I wan't to buy some books about python 3. Do you have any?recommendations? > I started with no previous programming experience, and I've finished a few > tutorials ?and?I guess I can be considered a beginner. > My problem, though, is I still find it difficult to write meaningful code or > use the built in libraries effectively and/or correctly because I can't find > example code to mimic. I tried sifting through ActiveState recipes page, but > most of the code seems uninteresting or useful only if utilized in a bigger > project. You might want to check out the 2nd Edition of Beginning Python, published in Sept. 2008. http://hetland.org/writing/beginning-python-2/ It includes the standard coverage of core Python syntax with a forward look at core Python 3 features (it was forward at the time). It also contains a number of project chapters at the end that span everything from text processing, to GUI building to network programming. They're not industrial strength apps, but I'd say a level above the typical toy examples. HTH, Serdar From bibsmendez at gmail.com Wed Dec 9 19:11:54 2009 From: bibsmendez at gmail.com (biboy mendz) Date: Wed, 09 Dec 2009 21:11:54 +0300 Subject: [Tutor] Append mode dilemma Message-ID: <4B1FE86A.60904@gmail.com> Hello all! I'm trying to use the append mode when opening and writing to a file but i cant get it to work my way. When run in the present code, the user inputs are expectedly 'appended' but not in a newline below the last line of the existing file. Instead it starts from where the last line end. I want the append to start on a newline of its own. Can you please let me know what am I missing? I dont quite get it what my book reference states: "Any file opened with 'a' will be opened for append. All writes to files opened with 'a' will be from end-of-file, even if you seek elsewhere during access. If the file does not exist it will be created, making it the same as if you opened the file in 'w' mode." #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Chapter 3.1 : Core Python Programming 2nd Edition by Wesley Chun Filename: appendFile.py Created: 09-Dec-2009 v1.0: append user input to existing file ''' import os, sys def main(): while True: fname = raw_input('Enter filename to APPEND your input lines: ') if os.path.isdir(fname): print('%s is a directory!') % fname elif not os.path.exists(fname): print 'this is APPEND mode; select CREATE to create new file' elif fname.isspace(): print 'Space is not allowed' else: #os.path.exists(fname): #found candidate for edit break all = [] # container list to hold user input lines print("file '%s' will be appended by your input\n") % fname quit_prompt = "[start your input to quit enter 1 dot]--> " while True: entry = raw_input(quit_prompt) if entry == '.': break else: all.append(entry) confirm = raw_input('save file to disk?(y/N)') confirm = confirm.lower() #convert to lowercase if confirm == 'y': fobj = open(fname, 'a') fobj.write('\n'.join(all)) fobj.close() print('DONE! open %s to view your file') % fname else: print 'not saving to disk!' if __name__ == '__main__': main() -- Regards, bibs M. Host/Kernel/OS "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux 2009-02 ????? - kde-full - (200907141427) ] www.sidux.com From asweigart at gmail.com Wed Dec 9 19:18:11 2009 From: asweigart at gmail.com (Albert Sweigart) Date: Wed, 9 Dec 2009 10:18:11 -0800 Subject: [Tutor] What books do you recommend? Message-ID: <716dd5b60912091018s49dc1ab6gc2b90d636c3e1f9f@mail.gmail.com> I'd recommend Doug Hellman's Python Module of the Week blog (PyMOTW) at http://www.doughellmann.com/projects/PyMOTW/ He goes into each of the standard library modules and gives examples of them in use. Dive Into Python 3 by Mark Pilgrim is also good for getting up to speed on Python 3. http://diveintopython3.org/ And I recommend my book, "Invent Your Own Computer Games with Python" which is freely available at http://inventwithpython.com It doesn't go into the standard library modules, but it does have several complete programs that are easy to read and understand. The book is aimed at complete beginners and young adults (but is not "kidsy"). -Al Sweigart http://inventwithpython.com From rabidpoobear at gmail.com Wed Dec 9 19:36:40 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 9 Dec 2009 12:36:40 -0600 Subject: [Tutor] Append mode dilemma In-Reply-To: <4B1FE86A.60904@gmail.com> References: <4B1FE86A.60904@gmail.com> Message-ID: On Wed, Dec 9, 2009 at 12:11 PM, biboy mendz wrote: > Hello all! > I'm trying to use the append mode when opening and writing to a file > but i cant get it to work my way. When run in the present code, > the user inputs are expectedly 'appended' but not in a newline below the > last > line of the existing file. > That's because there is NOT a new line at the end of the file. It's a file you're appending to, it's up to YOU to create that new line. And all files should end with newlines anyway (on linux). So modify your code so that you output a new line at the end of your outputs. > fobj = open(fname, 'a') > fobj.write('\n'.join(all)) > fobj.close() > > Are you aware of how 'join' works? try print "#".join(['a','b','c']) at the interactive prompt and see if you can't figure out why you're missing a newline at the end of your output. HTH, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From bibsmendez at gmail.com Wed Dec 9 19:49:14 2009 From: bibsmendez at gmail.com (biboy mendz) Date: Wed, 09 Dec 2009 21:49:14 +0300 Subject: [Tutor] Append mode dilemma In-Reply-To: References: <4B1FE86A.60904@gmail.com> Message-ID: <4B1FF12A.60002@gmail.com> Luke Paireepinart wrote: > On Wed, Dec 9, 2009 at 12:11 PM, biboy mendz > wrote: > > Hello all! > I'm trying to use the append mode when opening and writing to a file > but i cant get it to work my way. When run in the present code, > the user inputs are expectedly 'appended' but not in a newline > below the last > line of the existing file. > > That's because there is NOT a new line at the end of the file. > It's a file you're appending to, it's up to YOU to create that new > line. And all files should end with newlines anyway (on linux). > So modify your code so that you output a new line at the end of your > outputs. > > fobj = open(fname, 'a') > fobj.write('\n'.join(all)) > fobj.close() > > Are you aware of how 'join' works? > try > print "#".join(['a','b','c']) > at the interactive prompt and see if you can't figure out why you're > missing a newline at the end of your output. > > > HTH, > -Luke > Hi Luke, Thank you. To be honest I'm confused of the different string methods like join(), split(), etc. Anyway I will practice them to see how they work. -- Regards, bibs M. Host/Kernel/OS "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux 2009-02 ????? - kde-full - (200907141427) ] www.sidux.com From rabidpoobear at gmail.com Wed Dec 9 19:58:31 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 9 Dec 2009 12:58:31 -0600 Subject: [Tutor] Append mode dilemma In-Reply-To: <4B1FF12A.60002@gmail.com> References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com> Message-ID: On Wed, Dec 9, 2009 at 12:49 PM, biboy mendz wrote: > >> Are you aware of how 'join' works? >> > Hi Luke, > > Thank you. To be honest I'm confused of the different string methods like > join(), split(), etc. Anyway I will practice them to see how they work. > > > It's very important to practice these string methods, you should get intimately acquainted with all the builtin objects and functions, they're extremely flexible and usually very fast as well. The main problem with your 'join' is that join takes an interable and creates a new string with the LHS argument in between each iterated item. So if you join 'a' 'b' 'c', with # you will get 'a#b#c'. Note that there is no # at the beginning or end. In your case, you are outputting to a file, and you want every file to end with a newline, so you simply need to append another newline to the end. "#".join(['a','b','c']) + "#" would do the trick for the # example. The result would be "a#b#c#". Just to be clear, make sure you have your extra newline at the end, not the beginning of the print. Otherwise your first line in the file will be blank. So it does matter where you put the newline. HTH, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From bibsmendez at gmail.com Wed Dec 9 20:02:22 2009 From: bibsmendez at gmail.com (biboy mendz) Date: Wed, 09 Dec 2009 22:02:22 +0300 Subject: [Tutor] Append mode dilemma In-Reply-To: <4B1FF12A.60002@gmail.com> References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com> Message-ID: <4B1FF43E.6020003@gmail.com> > Luke Paireepinart wrote: > That's because there is NOT a new line at the end of the file. > It's a file you're appending to, it's up to YOU to create that new > line. And all files should end with newlines anyway (on linux). > So modify your code so that you output a new line at the end of your > outputs. > > fobj = open(fname, 'a') > fobj.write('\n'.join(all)) > fobj.close() > Quickie hack just to achieve the newline. Doesn't python have a function that adds newline automatically in append mode? fobj = open(fname, 'a') fobj.write('\n'.join(all)) print fobj.close() -- Regards, bibs M. Host/Kernel/OS "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux 2009-02 ????? - kde-full - (200907141427) ] www.sidux.com >> Are you aware of how 'join' works? >> try >> print "#".join(['a','b','c']) >> at the interactive prompt and see if you can't figure out why you're >> missing a newline at the end of your output. >> >> >> HTH, >> -Luke >> > Hi Luke, > > Thank you. To be honest I'm confused of the different string methods > like join(), split(), etc. Anyway I will practice them to see how they > work. > From rabidpoobear at gmail.com Wed Dec 9 20:12:29 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 9 Dec 2009 13:12:29 -0600 Subject: [Tutor] Append mode dilemma In-Reply-To: <4B1FF43E.6020003@gmail.com> References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com> <4B1FF43E.6020003@gmail.com> Message-ID: On Wed, Dec 9, 2009 at 1:02 PM, biboy mendz wrote: > > Luke Paireepinart wrote: >> That's because there is NOT a new line at the end of the file. >> It's a file you're appending to, it's up to YOU to create that new line. >> And all files should end with newlines anyway (on linux). >> So modify your code so that you output a new line at the end of your >> outputs. >> >> fobj = open(fname, 'a') >> fobj.write('\n'.join(all)) >> fobj.close() >> >> > Quickie hack just to achieve the newline. Doesn't python have a function > that adds newline automatically in append mode? Why would it need a function for this? Some people might not want a newline, so it can't be automatically enabled. > > > > fobj = open(fname, 'a') > fobj.write('\n'.join(all)) > print > fobj.close() This won't work unless you have STDOUT redirected to your file. It would be better just to do fobj.write('\n'.join(all) + '\n') which is shorter than a function call that would enable this feature anyway. This isn't a hack, this is how you're supposed to do this. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 9 20:13:38 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 9 Dec 2009 19:13:38 -0000 Subject: [Tutor] What books do you recommend? References: Message-ID: "Khalid Al-Ghamdi" wrote > I wan't to buy some books about python 3. Do you have any > recommendations? There are very few Python 3 books out there. The only one I've used and can recommend is Programming in Python3 by Summerfield Other general Python books that will still be effective albeit written for Python 2 are all specialised topic guides such as: Python Network Programming - APress WxPython in Action - Manning Python Programming on Win32 - OReilly Otherwise try to get a cheap/secondhand copy of Python in a Nutshell(OReilly) HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From asweigart at gmail.com Wed Dec 9 20:15:34 2009 From: asweigart at gmail.com (Albert Sweigart) Date: Wed, 9 Dec 2009 11:15:34 -0800 Subject: [Tutor] Append mode dilemma Message-ID: <716dd5b60912091115u1b4a810em89c746b6ede7a125@mail.gmail.com> Your problem is on this line: fobj.write('\n'.join(all)) This puts a newline in between each line in "all", but not at the end. The fix is simple: fobj.write('\n'.join(all) + '\n') This will make sure that the last line has a newline at the end of it, so that when you later append data, it will appear on a fresh line. -Al Sweigart You should check out my free beginner's Python book, "Invent Your Own Computer Games with Python" http://inventwithpython.com From zstumgoren at gmail.com Wed Dec 9 20:34:47 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Wed, 9 Dec 2009 14:34:47 -0500 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: To Alan's list, I'd also add Learning Python 4th Edition. It came out in October and covers Python 3 and 2.6, though I'm not sure if it includes the heftier code samples the OP was interested in... On Wed, Dec 9, 2009 at 2:13 PM, Alan Gauld wrote: > > "Khalid Al-Ghamdi" wrote > >> I wan't to buy some books about python 3. Do you have any recommendations? > > There are very few Python 3 books out there. > The only one I've used and can recommend is Programming in Python3 by > Summerfield > > Other general Python books that will still be effective albeit written for > Python 2 > are all specialised topic guides such as: > > Python Network Programming - APress > WxPython in Action - Manning > Python Programming on Win32 - OReilly > > Otherwise try to get a cheap/secondhand copy of Python in a > Nutshell(OReilly) > > HTH, > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/l2p/ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From bibsmendez at gmail.com Wed Dec 9 20:38:06 2009 From: bibsmendez at gmail.com (biboy mendz) Date: Wed, 09 Dec 2009 22:38:06 +0300 Subject: [Tutor] Append mode dilemma In-Reply-To: <716dd5b60912091115u1b4a810em89c746b6ede7a125@mail.gmail.com> References: <716dd5b60912091115u1b4a810em89c746b6ede7a125@mail.gmail.com> Message-ID: <4B1FFC9E.1080902@gmail.com> -- Regards, bibs M. Host/Kernel/OS "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux 2009-02 ????? - kde-full - (200907141427) ] www.sidux.com Albert Sweigart wrote: > Your problem is on this line: > > fobj.write('\n'.join(all)) > > This puts a newline in between each line in "all", but not at the end. > The fix is simple: > > fobj.write('\n'.join(all) + '\n') > > This will make sure that the last line has a newline at the end of it, > so that when you later append data, it will appear on a fresh line. > > -Al Sweigart > You should check out my free beginner's Python book, "Invent Your Own > Computer Games with Python" > http://inventwithpython.com > My bad on the print line addon. It doesnt work as expected. Adding '\n' at the join call function solved my problem. Thank you guys very much. Have a lot more of reading up to do :-) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From ladymcse2000 at gmail.com Wed Dec 9 21:00:07 2009 From: ladymcse2000 at gmail.com (Becky Mcquilling) Date: Wed, 9 Dec 2009 12:00:07 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: Is there a good Tutorial that you guys recommend, where you actually get a useful exercise to try and do and then you can be walked through different solutions? I'm not a Programmer by trade, but I do a bit of coding for Administrator type functions and have to debug stuff and starting to do this on Python, so something not quite so incredibly dense would work better, I just don't know where to start. Becky On Wed, Dec 9, 2009 at 11:34 AM, Serdar Tumgoren wrote: > To Alan's list, I'd also add Learning Python 4th Edition. It came out > in October and covers Python 3 and 2.6, though I'm not sure if it > includes the heftier code samples the OP was interested in... > > On Wed, Dec 9, 2009 at 2:13 PM, Alan Gauld > wrote: > > > > "Khalid Al-Ghamdi" wrote > > > >> I wan't to buy some books about python 3. Do you have any > recommendations? > > > > There are very few Python 3 books out there. > > The only one I've used and can recommend is Programming in Python3 by > > Summerfield > > > > Other general Python books that will still be effective albeit written > for > > Python 2 > > are all specialised topic guides such as: > > > > Python Network Programming - APress > > WxPython in Action - Manning > > Python Programming on Win32 - OReilly > > > > Otherwise try to get a cheap/secondhand copy of Python in a > > Nutshell(OReilly) > > > > HTH, > > > > -- > > Alan G > > Author of the Learn to Program web site > > http://www.alan-g.me.uk/l2p/ > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Wed Dec 9 21:11:43 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Wed, 9 Dec 2009 15:11:43 -0500 Subject: [Tutor] duplication in unit tests In-Reply-To: <1c2a2c590912081946r50dab0c5t677bba98672b9e20@mail.gmail.com> References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> <1c2a2c590912081946r50dab0c5t677bba98672b9e20@mail.gmail.com> Message-ID: > Yes, this is much better. Notice how much less code it is! :-) Yes, it was amazing to see how much code melted away when I gave up on the OO design. As you and others suggested, clearly this was not the correct approach for this portion of my program. > If you expect unicode input then it makes sense to test for it. If you > don't expect unicode input, it might make sense to test for an > expected error - how do you want the function to behave with invalid > inputs? For invalid inputs, I'd like to log the error and place the data in a file for additional review. The input has to be unicode, and if it's not, it means my initial read and decoding of the source data was not performed properly. For that case, I'll plan to raise an exception and abort the program. You could add other tests as well, for example does it work if > there are two dashes in a row? Does splitLines() correctly remove > blank lines? So it seems I have a healthy list of test cases to start with! > By the way I applaud your effort, unit testing is a valuable skill. I'll admit, I learned the hard way on a project earlier this year. I got that project done (again with the help of folks on this list), but didn't do any test-writing up front. And now, as the inevitable bugs crop up, I'm forced to patch them hoping that I don't break something else. It drove home the fact that I need to get serious about testing, even if I don't go full-bore TDD on every project. I suspect an overdose of preparatory TDD reading had my head swirling a bit. I appreciate you all walking me through this first effort. No doubt I'll be writing again soon with more questions. Meantime, many thanks! Serdar From lie.1296 at gmail.com Wed Dec 9 21:46:44 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 10 Dec 2009 07:46:44 +1100 Subject: [Tutor] Append mode dilemma In-Reply-To: References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com> <4B1FF43E.6020003@gmail.com> Message-ID: On 12/10/2009 6:12 AM, Luke Paireepinart wrote: > This won't work unless you have STDOUT redirected to your file. It > would be better just to do > fobj.write('\n'.join(all) + '\n') except that if you're joining a very long string, you'll have python copy the whole string again. better to just redirect print print >> fobj, '\n'.join(all) but since this is a file object we're talking about, you can write while getting user input. with open(...) as fobj: entry = '' while entry != '.' fobj.write(raw_input()) From rabidpoobear at gmail.com Wed Dec 9 21:58:15 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 9 Dec 2009 14:58:15 -0600 Subject: [Tutor] Append mode dilemma In-Reply-To: References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com> <4B1FF43E.6020003@gmail.com> Message-ID: On Wed, Dec 9, 2009 at 2:46 PM, Lie Ryan wrote: > On 12/10/2009 6:12 AM, Luke Paireepinart wrote: > >> This won't work unless you have STDOUT redirected to your file. It >> would be better just to do >> fobj.write('\n'.join(all) + '\n') >> > > except that if you're joining a very long string, you'll have python copy > the whole string again. > > better to just redirect print > print >> fobj, '\n'.join(all) > I'm sorry, but this just seems like kind of an unreadable way to do this. You're implicitly relying on print adding a newline to the end of every output. You could also just append an empty string to the end of "all" and thus another newline would be added. Either approach is less clear than the concatenation. I guess it just depends on the size of your string. The most readable approach while still being efficient would probably be just to write a newline after the previous write. fobj.write('\n'.join(all)) fobj.write('\n') Then it's still absolutely clear that you're trying to get a newline there and it's not a side-effect of some other function. > > but since this is a file object we're talking about, you can write while > getting user input. > > with open(...) as fobj: > entry = '' > while entry != '.' > fobj.write(raw_input()) > > Again, the file will only have a newline at the end in this situation because you're relying on the implicit newline created by reading in the user's string from raw_input. So I don't think this is a good example to give in this situation, because as soon as the user starts splitting / stripping / parsing raw_input and then trying to write it back out they're going to get confused. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From wescpy at gmail.com Wed Dec 9 22:17:09 2009 From: wescpy at gmail.com (wesley chun) Date: Wed, 9 Dec 2009 13:17:09 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: <78b3a9580912091317r5e60cf9exf34d24bd775c9204@mail.gmail.com> > I wan't to buy some books about python 3. Do you have any?recommendations? > I started with no previous programming experience, and I've finished a few > tutorials ?and?I guess I can be considered a beginner. greetings khalid, and welcome to Python! based on your background, i would like you start with Python 2, and i have several reasons for making this suggestion: - most beginner-oriented tutorials and books are still on Python 2 - Python 2 is not obsolete, and 95% of worldwide code still uses it - knowing Python 2 is not going to stop you from learning Python 3 quickly with that said, the most well-known books and online tutorials for true beginners like yourself include (in no particular order): - hello world!: computer programming for kids and other beginners by sande http://cp4k.blogspot.com/- how to think like a computer scientist by downey and elkner http://openbookproject.net//thinkCSpy/ - a byte of python by swaroop (both python 2 and python 3 versions) http://www.swaroopch.com/notes/Python - learning to program by gauld http://www.freenetpages.co.uk/hp/alan.gauld/ - python for the absolute beginner by dawson -livewires course http://www.livewires.org.uk/python/home you can find more resources here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers some of the books that have been suggested in this thread do require some programming experience, so i wanted to provide a few specifically targeted towards non-programmer beginners. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From bgailer at gmail.com Wed Dec 9 22:45:38 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 09 Dec 2009 16:45:38 -0500 Subject: [Tutor] win32com and ocx with properties In-Reply-To: <200912090833.38712.jfabiani@yolo.com> References: <200912090833.38712.jfabiani@yolo.com> Message-ID: <4B201A82.3000703@gmail.com> John wrote: > I realize that some may consider this an advance question. But there are many > here that are advance. So I'm hoping some nice soul will help me. > > I'm attempting to use a OCX designed to talk with QuickBooks. I'm using > win32com for the first time and have discovered an issue that I'm sure others > have run into. But researching google etc. has not helped. > > obj = win32com.client.Dispatch("InQB.Invoice.1") > #In the VB code > #obj.ItemName(0) = 'string' > > #in python I'm trying > obj.ItemName[0] = 'string' > Try: obj.ItemName.Item[0] = 'string' In VB obj.ItemName(0) = 'string' Item is the "default" property. obj.ItemName.Item(0) = 'string' will also work in VB > #error is "Instancemethod object does not support item assignment" > > I found and ran makepy.py and here is the code it created dealing with my obj. > > # The method ItemName is actually a property, but must be used as a method > to correctly pass the arguments > def ItemName(self, ItemIndex=defaultNamedNotOptArg): > """Line item property: Reference to the kind of item.""" > # Result is a Unicode object > return self._oleobj_.InvokeTypes(19, LCID, 2, (8, 0), > ((3,1),),ItemIndex ) > > As I read the above code it can only return a string not assign anything. I'm > not sure what "InvokeTypes" does. > > So the question is how can I get around this issue? I'm sure others need to > set properties in com objects. > > BTW does anyone know of a win32com forum or list? > There is python-win32 at python.org -- Bob Gailer Chapel Hill NC 919-636-4239 From modulok at gmail.com Wed Dec 9 23:46:29 2009 From: modulok at gmail.com (Modulok) Date: Wed, 9 Dec 2009 15:46:29 -0700 Subject: [Tutor] More on unit testing - tests for external data... Message-ID: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> List, Unit testing functions and methods which rely on passed data is simple enough. However: How do I unit test something which relies on external data? For example, a function which creates a file only if it doesn't exist on disk, or a unit test for a function which makes an SQL query? If anyone could point to a few examples of such usage, that would be great! Thanks! -Modulok- From timgoddardsemail at gmail.com Thu Dec 10 00:13:21 2009 From: timgoddardsemail at gmail.com (Tim Goddard) Date: Wed, 9 Dec 2009 17:13:21 -0600 Subject: [Tutor] What books do you recommend? Message-ID: I just finished Michael Dawson's Python Programming for the absolute beginner. I thought it was pretty good, with only a few minor nit picks. My programming background was limited to MATLAB and some Visual Basic scripting for excel, access etc making me the target audience. I liked the examples, and by the end has you put together a simple asteroid-style game (with livewires and pygame). If my C class back in undergrad had promised I'd be making some shooting games in the end perhaps I'd have kept with it and gone into computer science or something. I also encountered a previous posters problem with the pizza not falling down. If you use the latest version of livewires the Sprite class does not accept velocity arguments and you have to invoke a separate Mover class to make it move. Considering I had no experience with python before this book, that I was able to open the games module and determine what changed says a lot about what can be learned from a 'beginners' book. From electroblog at gmail.com Thu Dec 10 00:48:18 2009 From: electroblog at gmail.com (Joe Veldhuis) Date: Wed, 9 Dec 2009 18:48:18 -0500 Subject: [Tutor] Fast fourier transform libraries? Message-ID: <20091209184818.77ccdfe5.electroblog@gmail.com> Hello to all. I'm working on a program that will need to do some simple signal analysis (namely, find the frequency of an audio signal) as part of its operation. Something like: ----- samples = list() for i in range(fft_length): samples.append(readSoundCard()) fft_bins = FFT(samples, sample_rate, window_function) for bin in fft_bins: #find strongest bin #return frequency in Hz corresponding to that bin ----- My question is, can someone recommend a library that provides an FFT function that will do what I want (i.e. take samples and return a list of bins)? Could be a binding to FFTW, or something else entirely. A search on PYPI returned nothing of interest. Any advice greatly appreciated! -Joe From alan.gauld at btinternet.com Thu Dec 10 01:04:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 00:04:39 -0000 Subject: [Tutor] Append mode dilemma References: <4B1FE86A.60904@gmail.com> <4B1FF12A.60002@gmail.com><4B1FF43E.6020003@gmail.com> Message-ID: "Lie Ryan" wrote > with open(...) as fobj: > entry = '' > while entry != '.' > fobj.write(raw_input()) But how does entry change in this example? I think you need with open(...) as fobj: entry = '' while entry != '.' fobj.write(entry) entry = raw_input() -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Dec 10 01:15:35 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 00:15:35 -0000 Subject: [Tutor] More on unit testing - tests for external data... References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> Message-ID: "Modulok" wrote > Unit testing functions and methods which rely on passed data is simple > enough. However: > > How do I unit test something which relies on external data? You have to build a test environment. This needs to be carefully planned to enable every test condition to be tested. > For example, a function which creates a file only if it doesn't exist > on disk, or a unit test for a function which makes an SQL query? So you would need a file that exists, one that doesn't(!), one that exists but is read-only, one that exists but wrong user ownership, and really a binary file and a text file and an empty file and a huge file too For the database it gets tricky, you need to create a database full of entries with all the possible scenarios you might encounter. For a large application with multiple joined tables designing such a test database can take a lot of time - but it is invaluable for testing and debugging and provided you store a clean copy somewhere before using it makes regression testing possible. Once you have the database you then need to write the test driver code that will provide the right keys for each test - database testing is one of the biggest challenges in system testing. And don't forget to test for behaviour with invalid user access on the database, and for locking and simultaneous updates etc. And if its on a separate server that you check behaviour when the server or network is down or running slowly (eg by lowering server priorioty and running a huge bulk update in the background). Remember, in testing you are not trying to prove it works but rather to demonstrate that it doesn't! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jfabiani at yolo.com Thu Dec 10 01:20:55 2009 From: jfabiani at yolo.com (John) Date: Wed, 9 Dec 2009 16:20:55 -0800 Subject: [Tutor] win32com and ocx with properties In-Reply-To: <4B201A82.3000703@gmail.com> References: <200912090833.38712.jfabiani@yolo.com> <4B201A82.3000703@gmail.com> Message-ID: <200912091620.55186.jfabiani@yolo.com> On Wednesday 09 December 2009 01:45:38 pm bob gailer wrote: > John wrote: > > I realize that some may consider this an advance question. But there are > > many here that are advance. So I'm hoping some nice soul will help me. > > > > I'm attempting to use a OCX designed to talk with QuickBooks. I'm using > > win32com for the first time and have discovered an issue that I'm sure > > others have run into. But researching google etc. has not helped. > > > > obj = win32com.client.Dispatch("InQB.Invoice.1") > > #In the VB code > > #obj.ItemName(0) = 'string' > > > > #in python I'm trying > > obj.ItemName[0] = 'string' > > Try: > > obj.ItemName.Item[0] = 'string' > > In VB obj.ItemName(0) = 'string' Item is the "default" property. > obj.ItemName.Item(0) = 'string' will also work in VB > > > #error is "Instancemethod object does not support item assignment" > > > > I found and ran makepy.py and here is the code it created dealing with my > > obj. > > > > # The method ItemName is actually a property, but must be used as a > > method to correctly pass the arguments > > def ItemName(self, ItemIndex=defaultNamedNotOptArg): > > """Line item property: Reference to the kind of item.""" > > # Result is a Unicode object > > return self._oleobj_.InvokeTypes(19, LCID, 2, (8, 0), > > ((3,1),),ItemIndex ) > > > > As I read the above code it can only return a string not assign anything. > > I'm not sure what "InvokeTypes" does. > > > > So the question is how can I get around this issue? I'm sure others need > > to set properties in com objects. > > > > BTW does anyone know of a win32com forum or list? > > There is python-win32 at python.org Someone suggested I use: obj.SetItemName(0,'test') And it works (who knew) Thanks to all that replied, Actually thanks to all you tutors... Johnf From alan.gauld at btinternet.com Thu Dec 10 01:24:24 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 00:24:24 -0000 Subject: [Tutor] Fast fourier transform libraries? References: <20091209184818.77ccdfe5.electroblog@gmail.com> Message-ID: "Joe Veldhuis" wrote > My question is, can someone recommend a library that provides an FFT > function > that will do what I want (i.e. take samples and return a list of bins)? I'm no expert of FFT (I stuidied it at uni but can't remember a thing about it!) but a Google search suggests the fftpack in scipy should help. Alan G. From alan.gauld at btinternet.com Thu Dec 10 01:19:37 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 00:19:37 -0000 Subject: [Tutor] What books do you recommend? References: Message-ID: "Becky Mcquilling" wrote > Is there a good Tutorial that you guys recommend, where you actually get > a > useful exercise to try and do and then you can be walked through > different > solutions? What an interesting question. I'm not aware of any tutorials that do this (although mine does in a few cases - eg the address book and myultiplication table examples - its not a general approach). I'll be interested to see if anyone else knows of anything. > Administrator type functions and have to debug stuff and starting to do > this > on Python, so something not quite so incredibly dense would work better, > I > just don't know where to start. There are plenty of beginners tutorials including mine. I just don't know of any that use multiple solutions to a single problem as a teaching tool. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From wescpy at gmail.com Thu Dec 10 01:58:00 2009 From: wescpy at gmail.com (wesley chun) Date: Wed, 9 Dec 2009 16:58:00 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: <78b3a9580912091317r5e60cf9exf34d24bd775c9204@mail.gmail.com> References: <78b3a9580912091317r5e60cf9exf34d24bd775c9204@mail.gmail.com> Message-ID: <78b3a9580912091658t34e11bc8qa5ef9d5fc8499db9@mail.gmail.com> > - learning to program by gauld http://www.freenetpages.co.uk/hp/alan.gauld/ update: alan's latest tutorial lives here: http://www.alan-g.me.uk/tutor/ -- 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 From denis.spir at free.fr Thu Dec 10 02:05:06 2009 From: denis.spir at free.fr (spir) Date: Thu, 10 Dec 2009 02:05:06 +0100 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: <20091210020506.5b3460fc@o> "Alan Gauld" dixit: > > "Khalid Al-Ghamdi" wrote > > > I wan't to buy some books about python 3. Do you have any > > recommendations? > > There are very few Python 3 books out there. > The only one I've used and can recommend is Programming in Python3 by > Summerfield > > Other general Python books that will still be effective albeit written for > Python 2 > are all specialised topic guides such as: > > Python Network Programming - APress > WxPython in Action - Manning > Python Programming on Win32 - OReilly > > Otherwise try to get a cheap/secondhand copy of Python in a > Nutshell(OReilly) > > HTH, > see also: http://inventwithpython.com/ Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Thu Dec 10 02:14:09 2009 From: denis.spir at free.fr (spir) Date: Thu, 10 Dec 2009 02:14:09 +0100 Subject: [Tutor] duplication in unit tests In-Reply-To: References: <1c2a2c590912081543q119f2797p16e19f78e5564f39@mail.gmail.com> <1c2a2c590912081946r50dab0c5t677bba98672b9e20@mail.gmail.com> Message-ID: <20091210021409.445aa590@o> Serdar Tumgoren dixit: > I'll admit, I learned the hard way on a project earlier this year. I > got that project done (again with the help of folks on this list), but > didn't do any test-writing up front. And now, as the inevitable bugs > crop up, I'm forced to patch them hoping that I don't break something > else. It drove home the fact that I need to get serious about testing, > even if I don't go full-bore TDD on every project. The great thing about testing is, once you have test suites for each module (in the general sense of the term), for relations between them, for global functionality, then you can update, refactor, enhace, etc... with some amount of confidence that you're not just adding bugs to bugs. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From ladymcse2000 at gmail.com Thu Dec 10 02:50:24 2009 From: ladymcse2000 at gmail.com (Becky Mcquilling) Date: Wed, 9 Dec 2009 17:50:24 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: And I did see yours which was great. But the reason I ask this, is because there are SO many different approaches you could take to a single problem, how do you know which is correct or why one is better than the other? You can dig yourself in to holes with more complex problems, and not understand why. Second thing, I'd like the answer to be somewhere, in a tutorial. If I really didn't get the solution, it helps to have the solution walked through and next time, I may be able to solve it (hopefully, anyway). I'm so new at this, that sometimes, I'm completely stumped and a few hints would go a long way, then having the answer presented, with an explanation, even further. Becky On Wed, Dec 9, 2009 at 4:19 PM, Alan Gauld wrote: > > "Becky Mcquilling" wrote > > > Is there a good Tutorial that you guys recommend, where you actually get a >> useful exercise to try and do and then you can be walked through different >> solutions? >> > > What an interesting question. > I'm not aware of any tutorials that do this (although mine does in a few > cases > - eg the address book and myultiplication table examples - its not a > general > approach). I'll be interested to see if anyone else knows of anything. > > > Administrator type functions and have to debug stuff and starting to do >> this >> on Python, so something not quite so incredibly dense would work better, I >> just don't know where to start. >> > > There are plenty of beginners tutorials including mine. > I just don't know of any that use multiple solutions to a single problem > as a teaching tool. > > > -- > Alan Gauld > > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pine508 at hotmail.com Thu Dec 10 04:34:47 2009 From: pine508 at hotmail.com (Che M) Date: Wed, 9 Dec 2009 22:34:47 -0500 Subject: [Tutor] What books do you recommend? In-Reply-To: References: , , , , , Message-ID: > But the reason I ask this, is because there are SO many different approaches you could > take to a single problem, I guess that depends a lot on what sorts of problems you are thinking in terms of. At least in many cases, perhaps one of the points of the Zen of Python is useful: "There should be one--and preferably only one--obvious way to do it." I myself have been trying to stick to that for now; to learn some standard ways to do certain things, to not reinvent the wheel but instead to use the standard library and modules to do what I need done (since someone already needed it done before and coded it well then). Yes, gaining more flexibility in how you could approach something is also good, but for learning I have tried to establish a core of basic approaches first, and alternate approaches second. I feel that if it works, it's readable, simple, and re-usable, I put it in the toolbox. > how do you know which is correct or why one is better than the > other? You can dig yourself in to holes with more complex problems, and not understand > why. This list is one good resource for comparing notes on "correctness" of approach. You'll see people ask if something is "Pythonic" or not, etc. _________________________________________________________________ Windows 7: Unclutter your desktop. Learn more. http://www.microsoft.com/windows/windows-7/videos-tours.aspx?h=7sec&slideid=1&media=aero-shake-7second&listid=1&stop=1&ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_7secdemo:122009 -------------- next part -------------- An HTML attachment was scrubbed... URL: From timgoddardsemail at gmail.com Thu Dec 10 06:30:21 2009 From: timgoddardsemail at gmail.com (Tim Goddard) Date: Wed, 9 Dec 2009 23:30:21 -0600 Subject: [Tutor] Sound problems Message-ID: I'm still learning, and this may be better served on a pygame mailing list but I thought I'd try here first. I'm following the python programming for absolute beginners which uses livewires and pygame to do some simple games. My difficulty comes from not using the module versions used in the book and using the latest online offerings. The book creates a simple program to play music and sound in a console environment. initially the code was (shortened for brevity): from livewires import games # load a sound file missile = games.load_sound("missile.wav") #load the music file games.load_music("theme.mid") choice == None while choice != "0": print """ 1 - play missile sound 2 - play music """ choice = raw_input("Choose your selection: ") if choice == "1": missile.play() elif choice == "2": games.play_music() Which gives an error because the load_music is not part of the latest games.py module. So after reading through games.py I discovered that most of it is a pass through to pygames. Also the sound does not play. My modified code which plays music but still not sound is: from livewires import games # If I don't keep the above line, I get an error that pygame.mixer is not initialized properly import pygame # load a sound file missile = pygame.mixer.Sound('pickup.wav') # load the music file pygame.mixer.music.load("theme.mid") if choice == "1": missile.play(3) # loop .wav three times. print "Playing missile sound" elif choice == "2": pygame.mixer.music.play() print "Playing theme music" My question is why won't the sound play in the console? When I use the same code in a program with a screen, it plays normally. Thanks in advance! From lie.1296 at gmail.com Thu Dec 10 06:40:09 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 10 Dec 2009 16:40:09 +1100 Subject: [Tutor] Sound problems In-Reply-To: References: Message-ID: On 12/10/2009 4:30 PM, Tim Goddard wrote: > My modified code which plays music but still not sound is: > from livewires import games > # If I don't keep the above line, I get an error that pygame.mixer is > not initialized properly I haven't inspected your code thoroughly, but did you make any call to pygame.init()? Many pygame modules won't work properly unless you do that before doing anything else. From cspears2002 at yahoo.com Thu Dec 10 07:05:19 2009 From: cspears2002 at yahoo.com (Christopher Spears) Date: Wed, 9 Dec 2009 22:05:19 -0800 (PST) Subject: [Tutor] parsing Spreadsheet document Message-ID: <817325.54526.qm@web51605.mail.re2.yahoo.com> I want to fill a database with the contents of a spreadsheet. The spreadsheet was created by OpenOffice and is a .sxc document. What is the best way to do this? I think the best approach is to save the spreadsheet as a .csv document and then just parse the file. Am I on the right track here? From alan.gauld at btinternet.com Thu Dec 10 10:05:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 09:05:16 -0000 Subject: [Tutor] parsing Spreadsheet document References: <817325.54526.qm@web51605.mail.re2.yahoo.com> Message-ID: "Christopher Spears" wrote >I want to fill a database with the contents of a spreadsheet. > The spreadsheet was created by OpenOffice and is a .sxc document. > I think the best approach is to save the spreadsheet as a .csv document If you have control over the document then yes I'd go that way too. You could save in Excel format and use pyExelerator etc as an alternative option. Alan G. From chris_schueler at hotmail.com Thu Dec 10 03:24:01 2009 From: chris_schueler at hotmail.com (Christopher schueler) Date: Wed, 9 Dec 2009 22:24:01 -0400 Subject: [Tutor] Still making Cribbage game, NEED HELP Please Message-ID: I am still working on my cribbage game.. if you want rules or an outline of the rules/project information, it is HERE -> http://jongarvin.com/cawthra/files/ics3u/cribbage_project.pdf I have attached a copy of what i have so far.. i have built a deck with 52 cards AND made a dictionary containing each card and its value such as Jacks and kings worth a value of 10. I was wondering if people could give me ways to finish this project WITHOUT CLASSES because we havent learnd them yet OR could somebody right their own program for this cribbage game so i can compare or get ideas from your coding. This also contains my PSUEDOCODE if anybody wants to review how i did things or what certain things do MY CODE for Cribbage:: from random import* ## December 2 - Jan 20, 2010 ## Last mode 8/12/09 ## Program by John Dananhy, Phil Sulinski & Chris Schueler 4 ## We present the Tech-Sauve Cribbage game ##Sudo Code #Functions #DisplayTitle #Printing the rules of how to play cribbage #GetPlayer names #ask of there names and set it to a variable named Player1/2 #Build the deck #loop through 52 times #call from two diffrent arrays holding the suite and card number #combined the suite and card value to create a new variable #append the new variable to the empty array Deck #return the new values #Main Program #ask the user if they would like to learn to play #if yes print the DisplayTitle function #otherwise continue with the main program #Get the players' names from the function GetPlayer1/2 #Creating the array holding the diffrent values of the cards (numbers) #Creating the array holding the diffrent suite values #these are the arrays that the Build_Deck function uses #sorts the suits in order #creates the various empty arrays that the program will use #Defining the winning value #Using the function Builed_Deck to build the deck #Player1's hand is randomly drawn from the deck and appended to the array P1hand #That card is removed from the deck so it won't be taken twice #The same is done for Player2 #Next the top card is drawn by randomly selecting a card from the deck #Player1 is asked to select a card from their hand to put in the crib #this is repeated telling Player1 to take another card from his hand #These cards are appended to the array crib and removed from the Players hand #This is repeated for Player2 #Print all the values to the screen #Displays title and instructions def DisplayTitle(): print print "Welcome to Tech-Sauve Cribbage" print "--------------------------------------------" print " Insctructions " print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" print "1) Only played with two players (for now) " print "2) The program starts with a full deck of 52 cards" print "3) Deals out 6 cards to each player with a Suit letter" print "4) Then asks each player what 2 cards they want to discard to the crib" print "5) Then the program saves the crib in a temporary deck" print "6) Players start showing cards to get an ammount equal to 31" print "7) Once all the cards have been played, program counts the score" print "8) Then the program will count all possible scores in each hand" print " And it will add the players points to their total score" print "9) First player to reach a score of 121 wins the game" #Gets players names def GetPlayer1(): print Player1 = str(raw_input("Player 1's name ")) return Player1 def GetPlayer2(): print Player2 = str(raw_input("Player 2's name ")) return Player2 #Building the deck def Build_Deck(): for R in range (0,52): cardnumb = numbers[R] cardsuit = suits[R] card = str(numbers[R])+str(suits[R]) #All cards are put into the deck Deck.append(card) return Deck,numbers,suits,card,cardnumb,cardsuit ##DICTIIONARY CARD VALUES cards = {"AH" : 1,"2H" : 2,"3H" : 3,"4H" : 4,"5H" : 5,"6H" : 6,"7H" : 7,"8H" : 8,"9H":9,"10H" : 10,"JH" : 10,"QH" : 10,"KH" : 10,\ "AC" : 1,"2C" : 2,"3C" : 3,"4C" : 4,"5C" : 5,"6C" : 6,"7C" : 7,"8C" : 8,"9C":9,"10C" : 10,"JC" : 10,"QC" : 10,"KC" : 10,\ "AD" : 1,"2D" : 2,"3D" : 3,"4D" : 4,"5D" : 5,"6D" : 6,"7D" : 7,"8D" : 8,"9D":9,"10D" : 10,"JD" : 10,"QD" : 10,"KD" : 10,\ "AS" : 1,"2S" : 2,"3S" : 3,"4S" : 4,"5S" : 5,"6S" : 6,"7S" : 7,"8S" : 8,"9S":9,"10S" : 10,"JS" : 10,"QS" : 10,"KS" : 10} ##Variables Needed & sorts the deck numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4 suits = ["H","C","S","D"]*13 suits.sort() ##List of arrays used Deck = [] P1hand = [] P1value = [] P2hand = [] P2value = [] Crib = [] CribValue = [] TCvalue = [] ##Setting start scores P1_score = 0 P2_score = 0 Winner = 121 ele = 51 ##Building the deck Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck() ## MAIN PROGRAM ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##Asks user if the user wants the menu and instructions to be displayed L2P=str(raw_input("would you like to learn to play? Y/N")) if L2P == "Y" or L2P == "y": DisplayTitle() else: pass ##Calling player names Player1=GetPlayer1() Player2=GetPlayer2() ##Players' hands are drawn randomly from the deck for X in range(0,6): Y = randint(0,ele) draw = Deck[Y] P1hand.append(draw) #Cards are removed from the deck, to not be chosen more than once Deck.pop(Y) ele -= 1 for X2 in range (0,6): Y1 = randint(0,ele) draw2 = Deck[Y1] P2hand.append(draw2) #Cards are removed from the deck, to not be chosen more than once Deck.pop(Y1) ele -= 1 print ##Top card is drawn randomly Top = randint(0,39) Topcard = Deck[Top] print ##User chooses on which cards to add to the crib for count in range(1,3): print P1hand.sort() print P1hand print print Player1 #Updates on how many cards remain in the player's hand print "you have the chose from 1 - ",6-(count-1) option = int(raw_input("what CARD would you like to add to the crib? ")) #Card is added to crib from player's hand Crib.append(P1hand[option-1]) #Card is removed from player's hand P1hand.pop(option-1) print for c2 in range(1,3): print P2hand.sort() print P2hand print print Player2 #Updates on how many cards remain in the player's hand print "you have the chose from 1 - ",6-(c2-1) option1 = int(raw_input("what CARD would you like to add to the crib? ")) #Card is added to crib from player's hand Crib.append(P2hand[option1-1]) #Card is removed from player's hand P2hand.pop(option1-1) ## Assigning values to each card in each players Hand & Crib ## This also prints each players hand and the cribs face values of each card in it def HandCardValues(P1hand,P2hand,Crib,P1value,P2value,CribValue,Topcard,TCvalue): P1card_1 = P1hand[0] P1card_2 = P1hand[1] P1card_3 = P1hand[2] P1card_4 = P1hand[3] P2card_1 = P2hand[0] P2card_2 = P2hand[1] P2card_3 = P2hand[2] P2card_4 = P2hand[3] Ccard_1 = Crib[0] Ccard_2 = Crib[1] Ccard_3 = Crib[2] Ccard_4 = Crib[3] Tcard_1 = Topcard[0] TCvalue.append(cards[Topcard]) P1value.append(cards[P1card_1]) P1value.append(cards[P1card_2]) P1value.append(cards[P1card_3]) P1value.append(cards[P1card_4]) P2value.append(cards[P2card_1]) P2value.append(cards[P2card_2]) P2value.append(cards[P2card_3]) P2value.append(cards[P2card_4]) CribValue.append(cards[Ccard_1]) CribValue.append(cards[Ccard_2]) CribValue.append(cards[Ccard_3]) CribValue.append(cards[Ccard_4]) return P1hand,P2hand,Crib,P1value,P2value,CribValue,Topcard,TCvalue #Prints final variables to the screen HandCardValues(P1hand,P2hand,Crib,P1value,P2value,CribValue,Topcard,TCvalue) print print Deck print print "The TOP CARD is ",Topcard, "with a value of ",TCvalue print Player1,"'s Hand is ",P1hand, "with a value of ",P1value print Player2,"'s Hand is ",P2hand, "with a value of ",P2value print "The 4 cards in the Crib are ",Crib, "with a value of ",CribValue _________________________________________________________________ Windows Live: Make it easier for your friends to see what you?re up to on Facebook. http://go.microsoft.com/?linkid=9691816 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: STAGE 1.py URL: From luhmann_br at yahoo.com Thu Dec 10 14:27:27 2009 From: luhmann_br at yahoo.com (Luhmann) Date: Thu, 10 Dec 2009 05:27:27 -0800 (PST) Subject: [Tutor] =?iso-8859-1?q?R=E9p=2E_=3A__parsing_Spreadsheet_document?= In-Reply-To: <817325.54526.qm@web51605.mail.re2.yahoo.com> Message-ID: <917761.52604.qm@web30903.mail.mud.yahoo.com> If you just want to load the data into python one time, rather than connecting to the file, it would be more handy to paste the cells from the clipboard.? Here's how I do it on windows: from win32clipboard import *?#("pywin32" module required) def pastetable(): OpenClipboard() try: a=GetClipboardData(13) except: CloseClipboard() return None CloseClipboard() a=a.split('\r\n') for n, b in enumerate(a): a[n]=b.split('\t') return a __________________________________________________________________ Obtenez l'adresse de courriel parfaite: @ymail.com or @rocketmail.com. Obtenez votre nouvelle adresse maintenant ? http://cf.new.mail.yahoo.com/addresses. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Dec 10 15:40:25 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 10 Dec 2009 09:40:25 -0500 Subject: [Tutor] Sound problems In-Reply-To: References: Message-ID: <4B210859.2040402@ieee.org> Tim Goddard wrote: > I'm still learning, and this may be better served on a pygame mailing > list but I thought I'd try here first. > > I'm following the python programming for absolute beginners which uses > livewires and pygame to do some simple games. My difficulty comes > from not using the module versions used in the book and using the > latest online offerings. > > The book creates a simple program to play music and sound in a console > environment. > > initially the code was (shortened for brevity): > > from livewires import games > > # load a sound file > missile = games.load_sound("missile.wav") > > #load the music file > games.load_music("theme.mid") > > choice == None > while choice != "0": > print """ > 1 - play missile sound > 2 - play music > """ > choice = raw_input("Choose your selection: ") > if choice == "1": > missile.play() > elif choice == "2": > games.play_music() > > Which gives an error because the load_music is not part of the latest > games.py module. So after reading through games.py I discovered that > most of it is a pass through to pygames. Also the sound does not > play. > > My modified code which plays music but still not sound is: > from livewires import games > # If I don't keep the above line, I get an error that pygame.mixer is > not initialized properly > > import pygame > > # load a sound file > missile = pygame.mixer.Sound('pickup.wav') > > # load the music file > pygame.mixer.music.load("theme.mid") > > > > if choice == "1": > missile.play(3) # loop .wav three times. > print "Playing missile sound" > > elif choice == "2": > pygame.mixer.music.play() > print "Playing theme music" > > My question is why won't the sound play in the console? When I use > the same code in a program with a screen, it plays normally. > > Thanks in advance! > > If I may make a guess (I've never used pygame), I'd suggest that the sound playing logic counts on using the event loop for its timing. So without an event loop, no sound. DaveA From waynejwerner at gmail.com Thu Dec 10 17:20:48 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Thu, 10 Dec 2009 10:20:48 -0600 Subject: [Tutor] More on unit testing - tests for external data... In-Reply-To: References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> Message-ID: <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> On Wed, Dec 9, 2009 at 6:15 PM, Alan Gauld wrote: > > Remember, in testing you are not trying to prove it works but rather to > demonstrate that it doesn't! > So in that way it's a bit like the the scientific method (or exactly like)? You create a hypothesis and design tests to invalidate your hypothesis... and if they fail to invalidate you may have a valid hypothesis. Simply replace hypothesis with program and you get the testing procedure? -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Thu Dec 10 17:55:12 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Thu, 10 Dec 2009 10:55:12 -0600 Subject: [Tutor] Sound problems In-Reply-To: <4B210859.2040402@ieee.org> References: <4B210859.2040402@ieee.org> Message-ID: <333efb450912100855m7f313a62x1b2f4de30d8bb6fa@mail.gmail.com> On Thu, Dec 10, 2009 at 8:40 AM, Dave Angel wrote: > If I may make a guess (I've never used pygame), I'd suggest that the sound > playing logic counts on using the event loop for its timing. So without an > event loop, no sound. > Also, livewires is a pretty ancient - AFAICT they haven't had a new release in over a year. Pygame is a little more active in its development and is really easy enough with so much documentation there's no reason to learn livewires. Here's a simple example of playing a midi file with pygame only: import pygame pygame.init() pygame.mixer.music.load('ISawHerStandingThere.mid') pygame.mixer.music.set_endevent(pygame.QUIT) pygame.mixer.music.play() while True: event = pygame.event.poll() if event.type == pygame.QUIT: print 'Quitting' break Of course, replace "ISawHerStandingThere.mid" with whatever music file you want to play... HTH, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Dec 10 18:34:12 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 10 Dec 2009 12:34:12 -0500 Subject: [Tutor] More on unit testing - tests for external data... In-Reply-To: <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> Message-ID: <1c2a2c590912100934h176260eah5d1fdff3e2be1f1@mail.gmail.com> On Thu, Dec 10, 2009 at 11:20 AM, Wayne Werner wrote: > On Wed, Dec 9, 2009 at 6:15 PM, Alan Gauld > wrote: >> >> Remember, in testing you are not trying to prove it works but rather to >> demonstrate that it doesn't! > > So in that way it's a bit like the the scientific method (or exactly like)? > You create a hypothesis and design tests to invalidate your hypothesis... > and if they fail to invalidate you may have a valid hypothesis. Simply > replace hypothesis with program and you get the testing procedure? > -Wayne Nice analogy! And like with a scientific hypothesis, you can never be sure your program is correct, only that all your attempts to prove it incorrect has failed. Kent From denis.spir at free.fr Thu Dec 10 18:36:56 2009 From: denis.spir at free.fr (spir) Date: Thu, 10 Dec 2009 18:36:56 +0100 Subject: [Tutor] More on unit testing - tests for external data... In-Reply-To: <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> Message-ID: <20091210183656.02235f3a@o> Wayne Werner dixit: > On Wed, Dec 9, 2009 at 6:15 PM, Alan Gauld wrote: > > > > > Remember, in testing you are not trying to prove it works but rather to > > demonstrate that it doesn't! > > > > So in that way it's a bit like the the scientific method (or exactly like)? > You create a hypothesis and design tests to invalidate your hypothesis... > and if they fail to invalidate you may have a valid hypothesis. Simply > replace hypothesis with program and you get the testing procedure? > > -Wayne > > programming is modelizing -- like a scientist's job Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From emile at fenx.com Thu Dec 10 19:15:16 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 10 Dec 2009 10:15:16 -0800 Subject: [Tutor] parsing Spreadsheet document In-Reply-To: <817325.54526.qm@web51605.mail.re2.yahoo.com> References: <817325.54526.qm@web51605.mail.re2.yahoo.com> Message-ID: On 12/9/2009 10:05 PM Christopher Spears said... > I want to fill a database with the contents of a spreadsheet. The spreadsheet was created by OpenOffice and is a .sxc document. What is the best way to do this? I think the best approach is to save the spreadsheet as a .csv document and then just parse the file. Am I on the right track here? OpenOffice can be scripted with python, and if you're planning on pushing the data with python anyway, I think I'd look at writing it as an OpenOffice script... Emile From ladymcse2000 at gmail.com Thu Dec 10 19:30:36 2009 From: ladymcse2000 at gmail.com (Becky Mcquilling) Date: Thu, 10 Dec 2009 10:30:36 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: Good points. I guess being as new as I am I'm not always sure of the obvious way to do something or what I think is right, may not be an having explained examples are best, particularly after I've spent time solving the problem. But others may not find this useful. I admit that learning this stuff does not come particularly easy to me, so I tend to need more hand holding than others. Becky On Wed, Dec 9, 2009 at 7:34 PM, Che M wrote: > > > > But the reason I ask this, is because there are SO many different > approaches you could > > take to a single problem, > > I guess that depends a lot on what sorts of problems you are thinking in > terms of. At least in many cases, perhaps one of the points of the Zen of > Python is useful: > > "There should be one--and preferably only one--obvious way to do it." > > I myself have been trying to stick to that for now; to learn some standard > ways to do certain things, to not reinvent the wheel but instead to use the > standard library and modules to do what I need done (since someone already > needed it done before and coded it well then). Yes, gaining more > flexibility in how you could approach something is also good, but for > learning I have tried to establish a core of basic approaches first, and > alternate approaches second. I feel that if it works, it's readable, > simple, and re-usable, I put it in the toolbox. > > > > how do you know which is correct or why one is better than the > > other? You can dig yourself in to holes with more complex problems, and > not understand > > why. > > This list is one good resource for comparing notes on "correctness" of > approach. You'll see people ask if something is "Pythonic" or not, etc. > > > > ------------------------------ > Windows 7: Unclutter your desktop. Learn more. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sanelson at gmail.com Thu Dec 10 22:29:47 2009 From: sanelson at gmail.com (Stephen Nelson-Smith) Date: Thu, 10 Dec 2009 21:29:47 +0000 Subject: [Tutor] Trying to send a URL via XMPP Message-ID: Hi, I'm trying to send a message to a user via XMPP - but I want them to receive a clickable word. I'm using Python 2.4 on RHEL 5.4 and python-xmpp-0.4.1-6 from EPEL. I've tried variations on: >>> jid = xmpp.protocol.JID('motherinlaw at jabber.sekrit.org.uk') >>> cl = xmpp.Client(jid.getDomain()) >>> cl.connect() >>> cl.auth(jid.getNode(),'notgoodenough') >>> cl.send(xmpp.protocol.Message("snelsonsmith at jabber.sekrit.org.uk", "

Ticket #77 updated.

")) But every time I just receive the raw html.... Any idea what I am doing wrong? S. -- Stephen Nelson-Smith Technical Director Atalanta Systems Ltd www.atalanta-systems.com From waynejwerner at gmail.com Thu Dec 10 22:38:38 2009 From: waynejwerner at gmail.com (Wayne Werner) Date: Thu, 10 Dec 2009 15:38:38 -0600 Subject: [Tutor] Trying to send a URL via XMPP In-Reply-To: References: Message-ID: <333efb450912101338u1277fb71y7bfd1f822cffff81@mail.gmail.com> On Thu, Dec 10, 2009 at 3:29 PM, Stephen Nelson-Smith wrote: > Hi, > > I'm trying to send a message to a user via XMPP - but I want them to > receive a clickable word. > > I'm using Python 2.4 on RHEL 5.4 and python-xmpp-0.4.1-6 from EPEL. > > I've tried variations on: > > >>> jid = xmpp.protocol.JID('motherinlaw at jabber.sekrit.org.uk') > >>> cl = xmpp.Client(jid.getDomain()) > >>> cl.connect() > >>> cl.auth(jid.getNode(),'notgoodenough') > >>> cl.send(xmpp.protocol.Message("snelsonsmith at jabber.sekrit.org.uk", > "

Ticket #77 > updated.

")) > > But every time I just receive the raw html.... > > Any idea what I am doing wrong? > > I don't know anything about xmpp, but you could try receiving a message and inspecting how that is built? HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 10 23:24:53 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Dec 2009 22:24:53 -0000 Subject: [Tutor] What books do you recommend? References: Message-ID: "Becky Mcquilling" wrote > Good points. I guess being as new as I am I'm not always sure of the > obvious way to do something or what I think is right, One thing to remember is that it is always subjective. There is no such thing as an absolute "right way to do it" There are too many factors and virtually all solutions reflect a set of compromises by the designer > But others may not find this useful. I admit that learning this stuff > does > not come particularly easy to me, Its not easy to learn a new way of thinking and thats essentially what programming is. Some people will always find it easier and some harder but it is fundamentally a challenging activity for everyone - and that's part of what makes it fun! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From modulok at gmail.com Fri Dec 11 05:19:36 2009 From: modulok at gmail.com (Modulok) Date: Thu, 10 Dec 2009 21:19:36 -0700 Subject: [Tutor] More on unit testing - tests for external data... In-Reply-To: <20091210183656.02235f3a@o> References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> <20091210183656.02235f3a@o> Message-ID: <64c038660912102019x3cad95f8naf21639fd4470bc3@mail.gmail.com> So, this, 'test environment', this is just like a directory where I place my code and associated files to test for the existence (and non-existence) of various stuff, right? Any real-world examples/case studies to point to? It seems like there are a lot of people on this list interested in getting more familiar with unit testing, but not a whole lot of non-trivial, python-specific examples being passed around. I can write a function in a programming 101 class that accepts two arguments and returns a value by computing the hypotenuse of a triangle (or whatever). I can then build a unit-test for that making sure it fails and passes as needed. Cake. But jump into the real-world where many things are not so trivial, and I'm at a loss for where this unit-testing business all fits in. Basically, I'm trying to become a better programmer. (Who isn't?) I'd like to transition from 'hacky but gets the job done' or 'oh my God it actually works' to 'eloquent and bulletproof'. Without some kind of a mentor or vast array of tutorials to lay down the law when I screw up, or pass on some kind of approval when I get something right - it's been frustrating as hell. Case studies/tutorials anyone? Thanks! -Modulok- On 12/10/09, spir wrote: > Wayne Werner dixit: > >> On Wed, Dec 9, 2009 at 6:15 PM, Alan Gauld >> wrote: >> >> > >> > Remember, in testing you are not trying to prove it works but rather to >> > demonstrate that it doesn't! >> > >> >> So in that way it's a bit like the the scientific method (or exactly >> like)? >> You create a hypothesis and design tests to invalidate your hypothesis... >> and if they fail to invalidate you may have a valid hypothesis. Simply >> replace hypothesis with program and you get the testing procedure? >> >> -Wayne >> >> > > programming is modelizing -- like a scientist's job > > Denis > ________________________________ > > la vita e estrany > > http://spir.wikidot.com/ > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From ladymcse2000 at gmail.com Fri Dec 11 12:03:31 2009 From: ladymcse2000 at gmail.com (Becky Mcquilling) Date: Fri, 11 Dec 2009 03:03:31 -0800 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: Thanks, Alan. It is fun and on many levels I welcome the challenge. I won't continue to divert this thread from good books, but I will continue to look for more and more tutorials and will post it The more the more merrier... Becky On Thu, Dec 10, 2009 at 2:24 PM, Alan Gauld wrote: > > "Becky Mcquilling" wrote > > Good points. I guess being as new as I am I'm not always sure of the >> obvious way to do something or what I think is right, >> > > One thing to remember is that it is always subjective. > There is no such thing as an absolute "right way to do it" > There are too many factors and virtually all solutions reflect > a set of compromises by the designer > > > But others may not find this useful. I admit that learning this stuff >> does >> not come particularly easy to me, >> > > Its not easy to learn a new way of thinking and thats essentially > what programming is. Some people will always find it easier > and some harder but it is fundamentally a challenging activity > for everyone - and that's part of what makes it fun! > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri Dec 11 12:39:24 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 11 Dec 2009 22:39:24 +1100 Subject: [Tutor] Still making Cribbage game, NEED HELP Please In-Reply-To: References: Message-ID: On 12/10/2009 1:24 PM, Christopher schueler wrote: > I am still working on my cribbage game.. > > if you want rules or an outline of the rules/project information, > it is HERE -> > http://jongarvin.com/cawthra/files/ics3u/cribbage_project..pdf > > > I have attached a copy of what i have so far.. > i have built a deck with 52 cards AND made a dictionary containing each > card and its value such as Jacks and kings worth a value of 10. > > I was wondering if people could give me ways to finish this project > WITHOUT CLASSES because we havent learnd them yet OR could somebody > right their own program for this cribbage game so i can compare or get > ideas from your coding. > > This also contains my PSUEDOCODE if anybody wants to review how i did > things or what certain things do > First, black box evaluation, I'll just write things that comes to mind before seeing the code: - disallow empty names - prompts for cards: you may want to allow user to type "3D" instead of the card's number (the prompt "what CARD..." teases me to type the "card" I want even if the prompt says "1 - 6"). - minor: invalid input ideally shouldn't crash the program (though many teacher considers this outside marking criteria, since it's not possible to draw the line). - super-minor: try to minimize Speling erorrs (Instruction, comments, etc). Use spell-checkers if necessary. - minor: stylistic issues: See other command line programs to get a "feel" of how prompt statements should look like. General suggestion: - See in the project description, there is a stage 1,2,3, and for each stage there are sub-stages? Make at least one function from each of those sub-stages. It'll make your program flow clearer. - If you and all your teammates never played cribbage before, you'd better seek other resources for the rule. The rule given in the project description leaves a *lot* of thing unexplained, it is simply impossible to write a game just from that alone. - From reading the game rules described in the project description, apparently the game is divided into six separate rounds. Make six functions, each representing one round, and you main program will simply look like this: def play_game(): pre_game() round_deal() round_cut() round_pegging() round_show() round_crib() round_end() - DRY: Don't Repeat Yourself. If you see two blocks of code that looks similar, try to make them into a function (or loops, or whatever seems appropriate to reduce similarity): e.g. this two blocks looks disturbingly similar for X in range(0,6): Y = randint(0,ele) draw = Deck[Y] P1hand.append(draw)once Deck.pop(Y) ele -= 1 for X2 in range (0,6): Y1 = randint(0,ele) draw2 = Deck[Y1] P2hand.append(draw2) Deck.pop(Y1) ele -= 1 - try to minimize variables, especially global variables (don't confuse between variables and constants). e.g. ele is just the number of card in the deck, you can use len(Deck) instead. - It depends on you, but I think it will be easier to random.shuffle the deck before the play and just Deck.pop() each time we need to draw a new card rather than randomly picking from a sorted deck. It also looks more like a real card game, where the dealer shuffled the deck before game and just pulled the top of the decks when dealing. - if you're using python 2.6 or above, take a look at itertools.product if you're using python 2.7 or python 3, take a look at the new combinatoric generators functions in itertools Specific suggestion: - try rewriting this using random.choice() Top = randint(0,39) Topcard = Deck[Top] - try to turn these two into one function only def GetPlayer1(): print Player1 = str(raw_input("Player 1's name ")) return Player1 def GetPlayer2(): print Player2 = str(raw_input("Player 2's name ")) return Player2 Crystal Ball: - my guess why your teacher disallowed using class and object is because his plan for your next project is to turn the cribbage game to use classes. So, make sure you choose designs where it will be easy to convert to objects-oriented at later date. This way, you'll have to know both procedural and OOP while putting OOP at much better light than if he only teach OOP. From roshan.s at live.com Fri Dec 11 14:22:33 2009 From: roshan.s at live.com (Roshan S) Date: Fri, 11 Dec 2009 17:22:33 +0400 Subject: [Tutor] typerror Message-ID: class Student: print"We have a new student " def __init__(self,name='',credit=0,grade=0,quality=0): self.name=name self.credit=credit self.grade=grade self.quality=quality def inputstudent(self): self.name=raw_input("Enter student Name ") self.credit=input("What da credit hours ") self.grade=input("What da grade ") def quality(self): self.quality=self.credit*self.grade print"Quality Points: ",self.quality def average(self): quality() gpa=self.quality/self.credit print"Grade point average: "+self.grade if gpa == 4: print "Grade: A" elif gpa == 3: print "Grade: B" elif gpa == 2: print "Grade: C" elif gpa == 1: print "Grade: D" def outputstudent(self): "Name: "+self.name #main #create new student stud1=Student() #run teh method stud1.inputstudent() stud1.outputstudent() stud1.quality() stud1.average() RUN >>> We have a new student Enter student Name r What da credit hours 3 What da grade 5 Traceback (most recent call last): File "C:\Users\Roshan\Desktop\no3.py", line 38, in stud1.quality() TypeError: 'int' object is not callable >>> PYTHON version 2.6.3, windows 7 From jeff at dcsoftware.com Fri Dec 11 15:02:43 2009 From: jeff at dcsoftware.com (Jeff Johnson) Date: Fri, 11 Dec 2009 07:02:43 -0700 Subject: [Tutor] What books do you recommend? In-Reply-To: References: Message-ID: <4B225103.7010904@dcsoftware.com> Becky Mcquilling wrote: > Thanks, Alan. It is fun and on many levels I welcome the challenge. > > I won't continue to divert this thread from good books, but I will > continue to look for more and more tutorials and will post it The more > the more merrier... > > Becky I read through all the posts to make sure someone didn't already recommend this, but The Python Phrasebook is a great one. It has working code for a whole bunch of things like sending emails and reading websites just to name two. You can type them in and run them. It allowed me to quickly learn and appreciate Python. http://www.amazon.com/Python-Phrasebook-Brad-Dayley/dp/0672329107 I am not suggesting Amazon, it was just the first link I found. I see it in bookstores like Borders. -- Jeff Jeff Johnson jeff at dcsoftware.com Phoenix Python User Group - sunpiggies at googlegroups.com From kent37 at tds.net Fri Dec 11 15:09:48 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 11 Dec 2009 09:09:48 -0500 Subject: [Tutor] More on unit testing - tests for external data... In-Reply-To: <64c038660912102019x3cad95f8naf21639fd4470bc3@mail.gmail.com> References: <64c038660912091446n1af58415na4c23defb5b4457e@mail.gmail.com> <333efb450912100820r6c8ba52ex9ef572278d542c39@mail.gmail.com> <20091210183656.02235f3a@o> <64c038660912102019x3cad95f8naf21639fd4470bc3@mail.gmail.com> Message-ID: <1c2a2c590912110609r1b4fc7co3014796afa539f83@mail.gmail.com> On Thu, Dec 10, 2009 at 11:19 PM, Modulok wrote: > It seems like there are a lot of people on this list interested in > getting more familiar with unit testing, but not a whole lot of > non-trivial, python-specific examples being passed around. > Case studies/tutorials anyone? Unit testing has become common, accepted practice in open source projects so real-world examples abound. Python itself has extensive unit tests. To a large extent they actually define the language and the libraries - Jython and IronPython use the CPython test suite to validate their implementations. Download the Python source to get a copy of the tests. The Python unit tests largely predate the unittest module so they are not necessarily good examples of unittest. I suggest you pick an open source Python project that you use or like and look at its test suite. Kent From denis.spir at free.fr Fri Dec 11 15:59:01 2009 From: denis.spir at free.fr (spir) Date: Fri, 11 Dec 2009 15:59:01 +0100 Subject: [Tutor] typerror In-Reply-To: References: Message-ID: <20091211155901.7d850611@o> "Roshan S" dixit: > class Student: > print"We have a new student " > def __init__(self,name='',credit=0,grade=0,quality=0): > self.name=name > self.credit=credit > self.grade=grade > self.quality=quality > > > def inputstudent(self): > self.name=raw_input("Enter student Name ") > self.credit=input("What da credit hours ") > self.grade=input("What da grade ") > > def quality(self): > self.quality=self.credit*self.grade > print"Quality Points: ",self.quality > > def average(self): > quality() > gpa=self.quality/self.credit > print"Grade point average: "+self.grade > if gpa == 4: print "Grade: A" > elif gpa == 3: print "Grade: B" > elif gpa == 2: print "Grade: C" > elif gpa == 1: print "Grade: D" > > def outputstudent(self): > "Name: "+self.name > > #main > #create new student > stud1=Student() > > #run teh method > stud1.inputstudent() > stud1.outputstudent() > stud1.quality() > stud1.average() > > > RUN > > > >>> > We have a new student > Enter student Name r > What da credit hours 3 > What da grade 5 > Traceback (most recent call last): > File "C:\Users\Roshan\Desktop\no3.py", line 38, in > stud1.quality() > TypeError: 'int' object is not callable > >>> > > > PYTHON version 2.6.3, windows 7 You've got 2 attributes called 'quality'. One is a method that is defined on the class, the other a piece of data later defined on the student object itself at init time. The second one will override the method, hiding it. Python does not make a difference between behaviour attributes (methods) and state ones (data), so you cannot have a method called like a piece of data. Anyway, in your case the method is an *action* that sets and prints out an attribute, so its name should reflect this fact. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From davea at ieee.org Fri Dec 11 16:12:32 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 11 Dec 2009 10:12:32 -0500 Subject: [Tutor] typerror In-Reply-To: References: Message-ID: <4B226160.7050100@ieee.org> Roshan S wrote: >
class > Student: > print"We have a new student " > def __init__(self,name='',credit=0,grade=0,quality=0): > self.name=name > self.credit=credit > self.grade=grade > self.quality=quality > > > def inputstudent(self): > self.name=raw_input("Enter student Name ") > self.credit=input("What da credit hours ") > self.grade=input("What da grade ") > > def quality(self): > self.quality=self.credit*self.grade > print"Quality Points: ",self.quality > > def average(self): > quality() > gpa=self.quality/self.credit > print"Grade point average: "+self.grade > if gpa == 4: print "Grade: A" > elif gpa == 3: print "Grade: B" > elif gpa == 2: print "Grade: C" > elif gpa == 1: print "Grade: D" > > def outputstudent(self): > "Name: "+self.name > > #main > #create new student > stud1=Student() > > #run teh method > stud1.inputstudent() > stud1.outputstudent() > stud1.quality() > stud1.average() > > > RUN > > >>>> > We have a new student > Enter student Name r > What da credit hours 3 > What da grade 5 > Traceback (most recent call last): > File "C:\Users\Roshan\Desktop\no3.py", line 38, in > stud1.quality() > TypeError: 'int' object is not callable >>>> > > > PYTHON version 2.6.3, windows 7 > > In your Student class, you're using attribute quality for two very different purposes. You have a method by that name, and you have an instance variable by that name. You need to call one of them something else, probably the instance variable. Don't forget to change all the references to it, both in __init__() and in the method quality(). HTH DaveA From roshan.s at live.com Fri Dec 11 16:20:13 2009 From: roshan.s at live.com (Roshan S) Date: Fri, 11 Dec 2009 19:20:13 +0400 Subject: [Tutor] typerror In-Reply-To: <4B226160.7050100@ieee.org> References: <4B226160.7050100@ieee.org> Message-ID: On Fri, 11 Dec 2009 19:12:32 +0400, Dave Angel wrote: > Roshan S wrote: >>
class >> Student: >> print"We have a new student " >> def __init__(self,name='',credit=0,grade=0,quality=0): >> self.name=name >> self.credit=credit >> self.grade=grade >> self.quality=quality >> >> >> def inputstudent(self): >> self.name=raw_input("Enter student Name ") >> self.credit=input("What da credit hours ") >> self.grade=input("What da grade ") >> >> def quality(self): >> self.quality=self.credit*self.grade >> print"Quality Points: ",self.quality >> >> def average(self): >> quality() >> gpa=self.quality/self.credit >> print"Grade point average: "+self.grade >> if gpa == 4: print "Grade: A" >> elif gpa == 3: print "Grade: B" >> elif gpa == 2: print "Grade: C" >> elif gpa == 1: print "Grade: D" >> >> def outputstudent(self): >> "Name: "+self.name >> >> #main >> #create new student >> stud1=Student() >> >> #run teh method >> stud1.inputstudent() >> stud1.outputstudent() >> stud1.quality() >> stud1.average() >> >> >> RUN >> >> >>>>> >> We have a new student >> Enter student Name r >> What da credit hours 3 >> What da grade 5 >> Traceback (most recent call last): >> File "C:\Users\Roshan\Desktop\no3.py", line 38, in >> stud1.quality() >> TypeError: 'int' object is not callable >>>>> >> >> >> PYTHON version 2.6.3, windows 7 >> >> > In your Student class, you're using attribute quality for two very > different purposes. You have a method by that name, and you have an > instance variable by that name. You need to call one of them something > else, probably the instance variable. Don't forget to change all the > references to it, both in __init__() and in the method quality(). > > > HTH > > DaveA > > Thanks!!! Problem Solved! From lavendula6654 at gmail.com Fri Dec 11 17:55:36 2009 From: lavendula6654 at gmail.com (Elaine Haight) Date: Fri, 11 Dec 2009 08:55:36 -0800 Subject: [Tutor] Software Development Courses Message-ID: <3652e3600912110855j5c60c249m8c104f8a0f541545@mail.gmail.com> Foothill College is offering two courses of interest to web application software developers: Ajax and Python. These 11-week courses are held from January through March. The Ajax class is entirely online, and the Python class meets Thursday evenings at the Middlefield campus in Palo Alto. ?Application Software Development with Ajax? is a course designed for students who are already familiar with some type of programming, and have introductory knowledge of JavaScript and html. For more information, go to: http://www.foothill.edu/schedule/schedule.php and choose Department: ?COIN?, quarter: ?Winter 2010?, and course number ?71?. ?Introduction to Python Programming? meets Thursday evenings and is also designed for students who are familiar with some type of programming. The instructor is Marilyn Davis. For more information or to register, go to: http://www.foothill.edu/schedule/schedule.php and choose Department: ?CIS?, quarter: ?Winter 2010?, and course number ?68K?. If you would like to sign up for a class, please register beforehand by going to: http://www.foothill.fhda.edu/reg/index.php If you do not register ahead of time, the class you want may be cancelled! If you have questions, you can contact: h a i g h t E l a i n e AT f o o t h i l l . e d u -------------- next part -------------- An HTML attachment was scrubbed... URL: From pine508 at hotmail.com Fri Dec 11 20:37:35 2009 From: pine508 at hotmail.com (Che M) Date: Fri, 11 Dec 2009 14:37:35 -0500 Subject: [Tutor] What books do you recommend? In-Reply-To: References: , , , , , , , , , Message-ID: > I won't continue to divert this thread from good books, but I will continue to look > for more and more tutorials and will post it The more the more merrier... > Becky Just on that topic of tutorials, have you seen www.ShowMeDo.com? They have right now 516 screencast tutorials related (directly or indirectly) to Python. This way you can watch a video and usually hear a person describing what they are doing and why. Oftentimes, videos are organized into a series on a particular topic, like GUI programming, databases, etc. It's a different way to learn, and I find it a great service to the Python (and other programming or computer use) community. Che _________________________________________________________________ Hotmail: Free, trusted and rich email service. http://clk.atdmt.com/GBL/go/171222984/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From grigor.kolev at gmail.com Fri Dec 11 21:14:26 2009 From: grigor.kolev at gmail.com (Grigor Kolev) Date: Fri, 11 Dec 2009 22:14:26 +0200 Subject: [Tutor] Learn Python Message-ID: <1260562466.4658.9.camel@dedal-laptop> Hi I'm trying to learn python but I have difficulties with classes. If someone has a code that shows the basic concepts and relationships in the classes, please send it to me. I will be happy if code possess very much comments -- Grigor Kolev From alan.gauld at btinternet.com Sat Dec 12 00:58:47 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 11 Dec 2009 23:58:47 -0000 Subject: [Tutor] What books do you recommend? References: , , , , , , , , , Message-ID: "Che M" wrote > Just on that topic of tutorials, have you seen www.ShowMeDo.com? I'll echo this. I found the ShowMeDo site to be an excellent source, especially for learning the more hands-on type skills like using Eclipse/PyDev etc. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Dec 12 01:02:58 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 12 Dec 2009 00:02:58 -0000 Subject: [Tutor] Learn Python References: <1260562466.4658.9.camel@dedal-laptop> Message-ID: "Grigor Kolev" wrote > I'm trying to learn python but I have difficulties with classes. > If someone has a code that shows the basic concepts and relationships in > the classes, please send it to me. Most tutorials should show that. You can try the OOP topic in mine if you like, also the Case study topic has a worked example -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Sat Dec 12 22:32:52 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 12 Dec 2009 16:32:52 -0500 Subject: [Tutor] Entry-level open source opportunities Message-ID: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> OpenHatch is a new web site that compiles entry-level opportunities with open-source programming projects. Many of the projects are in Python. If you are looking for a way to get involved with an open source project, this could be a useful resource. https://openhatch.org/search/ Kent From kbailey at howlermonkey.net Sat Dec 12 22:47:56 2009 From: kbailey at howlermonkey.net (Kirk Z Bailey) Date: Sat, 12 Dec 2009 16:47:56 -0500 Subject: [Tutor] Hat lands in ring Message-ID: <4B240F8C.1070605@howlermonkey.net> I'm Baaacccccck... -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-----+ | BOX | +-----+ think From rabidpoobear at gmail.com Sat Dec 12 23:24:32 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 12 Dec 2009 16:24:32 -0600 Subject: [Tutor] Hat lands in ring In-Reply-To: <4B240F8C.1070605@howlermonkey.net> References: <4B240F8C.1070605@howlermonkey.net> Message-ID: On Sat, Dec 12, 2009 at 3:47 PM, Kirk Z Bailey wrote: > I'm Baaacccccck... > > How'd your wiki sales go? Find lots of road warriors? -------------- next part -------------- An HTML attachment was scrubbed... URL: From asweigart at gmail.com Sun Dec 13 03:53:41 2009 From: asweigart at gmail.com (Albert Sweigart) Date: Sat, 12 Dec 2009 18:53:41 -0800 Subject: [Tutor] Learn Python Message-ID: <716dd5b60912121853n1f83acc8mdf776a23e456f162@mail.gmail.com> I'd recommend my book, "Invent Your Own Computer Games with Python", which is available for free at: http://inventwithpython.com Each chapter focuses on the complete source code for a small game, and teaches programming concepts from the code. -Al > Hi > I'm trying to learn python but I have difficulties with classes. > If someone has a code that shows the basic concepts and relationships in > the classes, please send it to me. > I will be happy if code possess very much comments > -- > Grigor Kolev From grigor.kolev at gmail.com Sun Dec 13 12:25:30 2009 From: grigor.kolev at gmail.com (Grigor Kolev) Date: Sun, 13 Dec 2009 13:25:30 +0200 Subject: [Tutor] What I do wrong Message-ID: <1260703530.15233.2.camel@dedal-laptop> class MyList (): def __init__(self, value): self.value = [] for x in value: self.value.append(x) def __add__(self , other): return MyList(self.value+other) def __mul__(self , other): return MyList(self.value*other) def __getitem__(self , item): return MyList(self.value+item) def __len__(self ): return len(self.value) def __getlice__(self , low, high): return MyList(self.value[low:high] def append(self , other): self.list=self.list.append(other) return self.list def __getattr__(self , name): return getattr(self.value, name) def __repr__(self ): return 'self.value' -- Grigor Kolev From arts.martijn at gmail.com Sun Dec 13 12:51:03 2009 From: arts.martijn at gmail.com (Martijn Arts) Date: Sun, 13 Dec 2009 12:51:03 +0100 Subject: [Tutor] What I do wrong In-Reply-To: <1260703530.15233.2.camel@dedal-laptop> References: <1260703530.15233.2.camel@dedal-laptop> Message-ID: <23739e0a0912130351y7cd05d38x4722ff06d08a0d6e@mail.gmail.com> What doesn't work, does it have errors, etc.! On Sun, Dec 13, 2009 at 12:25 PM, Grigor Kolev wrote: > class MyList (): > def __init__(self, value): > self.value = [] > for x in value: > self.value.append(x) > def __add__(self , other): > return MyList(self.value+other) > def __mul__(self , other): > return MyList(self.value*other) > def __getitem__(self , item): > return MyList(self.value+item) > def __len__(self ): > return len(self.value) > def __getlice__(self , low, high): > return MyList(self.value[low:high] > def append(self , other): > self.list=self.list.append(other) > return self.list > def __getattr__(self , name): > return getattr(self.value, name) > def __repr__(self ): > return 'self.value' > -- > Grigor Kolev > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From grigor.kolev at gmail.com Sun Dec 13 13:36:26 2009 From: grigor.kolev at gmail.com (Grigor Kolev) Date: Sun, 13 Dec 2009 14:36:26 +0200 Subject: [Tutor] Learning Python Message-ID: <1260707786.15530.12.camel@dedal-laptop> It is from book Learning Python -------------------------------------------- Operator overloading. Write a class called Mylist that shadows (?wraps?) a Python list: it should overload most list operators and operations, including +, indexing, iteration, slicing, and list methods such as append and sort. See the Python reference manual for a list of all possible methods to support. Also, provide a con- structor for your class that takes an existing list (or a Mylist instance) and copies its components into an instance member. Experiment with your class interac- tively. Things to explore: a. Why is copying the initial value important here? b. Can you use an empty slice (e.g., start[:]) to copy the initial value if it?s a Mylist instance? c. Is there a general way to route list method calls to the wrapped list? d. Can you add a Mylist and a regular list? How about a list and a Mylist instance? e. What type of object should operations like + and slicing return? What about indexing operations? f. If you are working with a more recent Python release (version 2.2 or later), you may implement this sort of wrapper class by embedding a real list in a standalone class, or by extending the built-in list type with a subclass. Which is easier, and why? ---------------------------------------------------- My answer is: class MyList (): def __init__(self, value=[]): self.list=[] for i in value: self.list.append(i) def __add__(self , other): return self.list def __mul__(self , other): return self .list def __delitem__(self , other): return self .list def __geritem__(self , other): return self.list def __repeat__(self , other): return self.list def sort(self ): self.list = self.list.sort() return self.list def append(self , other): self.list=self.list.append(other) return self.list This is work ------------------------------------------------------ In the book answer is this: class MyList: def _ _init_ _(self, start): #self.wrapped = start[:] # Copy start: no side effects self.wrapped = [] # Make sure it's a list here for x in start: self.wrapped.append(x) def _ _add_ _(self, other): return MyList(self.wrapped + other) def _ _mul_ _(self, time): return MyList(self.wrapped * time) def _ _getitem_ _(self, offset): return self.wrapped[offset] def _ _len_ _(self): return len(self.wrapped) def _ _getslice_ _(self, low, high): return MyList(self.wrapped[low:high]) def append(self, node): self.wrapped.append(node) def _ _getattr_ _(self, name): # Other members: sort/reverse/etc return getattr(self.wrapped, name) def _ _repr_ _(self): return repr(self.wrapped) Answer in the book does not work Where is the error -- Grigor Kolev From alan.gauld at btinternet.com Sun Dec 13 18:06:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 13 Dec 2009 17:06:33 -0000 Subject: [Tutor] Learning Python References: <1260707786.15530.12.camel@dedal-laptop> Message-ID: "Grigor Kolev" wrote > ---------------------------------------------------- > My answer is: > class MyList (): > def __init__(self, value=[]): > self.list=[] > for i in value: > self.list.append(i) > def __add__(self , other): > return self.list > def __mul__(self , other): > return self .list > def __delitem__(self , other): > return self .list > def __geritem__(self , other): > return self.list > def __repeat__(self , other): > return self.list > def sort(self ): > self.list = self.list.sort() > return self.list > def append(self , other): > self.list=self.list.append(other) > return self.list > > This is work Given that add does not add anything how do you define "work"? Do you mean it executes without error messages? > ------------------------------------------------------ > In the book answer is this: > > class MyList: > def _ _init_ _(self, start): > #self.wrapped = start[:] # Copy start: no side effects > self.wrapped = [] # Make sure it's a list here > for x in start: self.wrapped.append(x) > def _ _add_ _(self, other): > return MyList(self.wrapped + other) > def _ _mul_ _(self, time): > return MyList(self.wrapped * time) > def _ _getitem_ _(self, offset): > return self.wrapped[offset] > def _ _len_ _(self): > return len(self.wrapped) > def _ _getslice_ _(self, low, high): > return MyList(self.wrapped[low:high]) > def append(self, node): > self.wrapped.append(node) > def _ _getattr_ _(self, name): # Other members: sort/reverse/etc > return getattr(self.wrapped, name) > def _ _repr_ _(self): > return repr(self.wrapped) > > Answer in the book does not work > Where is the error What error do you get? These merthods do entirely different things to yours. But without an error description its hard to comment, I don't feel like reading through line buy line trying to guess what the problem might be. It will also help if you tell us which version of Python you are using and which Operating System. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Sun Dec 13 18:08:53 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 13 Dec 2009 09:08:53 -0800 Subject: [Tutor] Learning Python In-Reply-To: <1260707786.15530.12.camel@dedal-laptop> References: <1260707786.15530.12.camel@dedal-laptop> Message-ID: On 12/13/2009 4:36 AM Grigor Kolev said... Your __init__ looks OK. All the other methods return either self.list or (with sort and append) return None because you've reassigned self.list to the return value of self.list.sort() and self.list.append()(both sort and append methods change the object in place and return None -- which you've then assigned to self.list) I've added comment lines below that describe what I would expect each method to do. You'll need to write python code to perform each description. I've completed one to get you started. I'd advise you to revisit the tutorial and refresh your working knowledge of core python. Magic methods might a bit advanced. Emile > My answer is: > class MyList (): > def __init__(self, value=[]): > self.list=[] > for i in value: > self.list.append(i) > def __add__(self , other): # add other to self.list # return self.list += other > return self.list > def __mul__(self , other): # return the result of multiplying self.list by other > return self .list > def __delitem__(self , other): # delete item other from self.list > return self .list > def __geritem__(self , other): # return self.list's other item > return self.list > def __repeat__(self , other): # ?? maybe repr? > return self.list > def sort(self ): # sort self.list in place > self.list = self.list.sort() > return self.list > def append(self , other): # append item other to self.list > self.list=self.list.append(other) > return self.list > From ldl08 at gmx.net Sun Dec 13 19:03:06 2009 From: ldl08 at gmx.net (David) Date: Mon, 14 Dec 2009 02:03:06 +0800 Subject: [Tutor] Learning Python In-Reply-To: <46FEC89E.8080504@tds.net> References: <68FD12A7-5325-4B2A-BFF2-AA542C9FD17D@mac.com> <46FEC89E.8080504@tds.net> Message-ID: <4B252C5A.6050004@gmx.net> Hi, Kent Johnson wrote: > Python in a Nutshell is good if you want a compressed but readable > introduction. I am thinking of buying this one, but the topical 2nd edition is from 2006. Does anyone know if a new version, covering Python 3, is coming to market soonish? David From bibsmendez at gmail.com Sun Dec 13 20:35:19 2009 From: bibsmendez at gmail.com (biboy mendz) Date: Sun, 13 Dec 2009 22:35:19 +0300 Subject: [Tutor] Read-create-edit file Message-ID: <4B2541F7.5030106@gmail.com> Hi list, I finally manage to sort of complete my read-create-edit.py file (after finding precious free time). I appreciate your review and good/bad comments to my code. It look and runs ok from my side here :-) #read_create_edit.py #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Exercise 3.12: combined read, edit and write program use import to utilize codes from other module files Core Python Programming 2nd Edition by Wesley Chun Filename: read_create_edit.py Created: 13-Dec-2009 ''' from readFile import main as read_main from readFile import repeat from createFile1 import main as create_main from appendFile import main as edit_main def main(): '''heart of the program''' while True: print ''' (1) read file (2) create file (3) edit file (add data) (4) quit program ''' try: ask = int(raw_input('choose your option: ')) if ask == 1: read_main() elif ask == 2: create_main() elif ask == 3: edit_main() elif ask == 4: break else: print 'invalid choice: enter (1), (2), (3) or (4) only!' except Exception, e: print 'Error detected: ', e if __name__ == '__main__': main() ask = repeat() if ask == 'y': main() else: print 'thank you for using python, goodbye!!' #appendFile.py #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Chapter 3.1 : Core Python Programming 2nd Edition by Wesley Chun Filename: appendFile.py Created: 09-Dec-2009 v1.0: append user input to existing file ''' import os, sys def main(): while True: fname = raw_input('Enter filename to APPEND/edit: ') if os.path.isdir(fname): print('%s is a directory!') % fname elif not os.path.exists(fname): print 'this is APPEND mode; select CREATE to create new file' elif fname.isspace(): print 'Space is not allowed' else: #os.path.exists(fname): #found candidate to edit break all = [] # container list to hold user input lines print("file '%s' will be appended by your input\n") % fname quit_prompt = "[start your input to quit enter 1 dot]--> " while True: entry = raw_input(quit_prompt) if entry == '.': break else: all.append(entry) confirm = raw_input('save file to disk?(y/N)') confirm = confirm.lower() #convert to lowercase if confirm == 'y': fobj = open(fname, 'a') fobj.write('\n'.join(all) + '\n') #(a) fobj.close() print('DONE! open %s to view your file') % fname else: print 'not saving to disk!' if __name__ == '__main__': main() #(a) as per help from tutor list #readFile.py #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Exercise 3.10b: read text file line by line; raise error if file not found, encapsulate program body with main function for code re-use Core Python Programming 2nd Edition by Wesley Chun Filename: readFile.py Created: 01-Dec-2009 edited: 07-Dec-2009 Version1: replace try-except with os.path.exists() ''' import os def repeat(): '''ask to repeat or no''' ask = raw_input('\ntry again? [N/y]') ask = ask.lower() return ask def main(): while True: fname = raw_input('enter file name to READ: ') if os.path.isdir(fname): print('%s is a directory!') % fname elif not os.path.exists(fname): print 'Error: file not found!' else: fobj = open(fname, 'r') for eachline in fobj: print eachline, fobj.close() break if __name__ == '__main__': main() #createFile.py #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Chapter 3.1 : create a file and write to disk Core Python Programming 2nd Edition by Wesley Chun Filename: createFile1.py Created: 07-Dec-2009 rev. 1: fine tune script by checking if input name exist, is a file or a directory ''' import os, sys def main(): while True: fname = raw_input('Enter filename to CREATE: ') if os.path.isdir(fname): print('%s is a directory!') % fname elif os.path.exists(fname): print '%s already exist' % fname elif fname.isspace(): print 'Space is not allowed' else: break all = [] # container list to hold user input lines print("\nPopulate your file *%s*! \n") % fname quit_prompt = "[to quit enter a dot '.' by itself]--> " while True: entry = raw_input(quit_prompt) if entry == '.': break else: all.append(entry) confirm = raw_input('save file to disk?(y/N)') confirm = confirm.lower() #convert to lowercase if confirm == 'y': fobj = open(fname, 'w') fobj.write('\n'.join(all)) fobj.close() print('DONE! open %s to view your file') % fname else: print 'not saving to disk!' if __name__ == '__main__': main() -- Regards, bibs M. Host/Kernel/OS "cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux 2009-02 ????? - kde-full - (200907141427) ] www.sidux.com From roadierich at googlemail.com Sun Dec 13 23:17:32 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Sun, 13 Dec 2009 22:17:32 +0000 Subject: [Tutor] Read-create-edit file In-Reply-To: <4B2541F7.5030106@gmail.com> References: <4B2541F7.5030106@gmail.com> Message-ID: 2009/12/13 biboy mendz : > Hi list, > > I finally manage to sort of complete my read-create-edit.py file (after > finding precious free time). I appreciate your review and good/bad comments > to my code. It look and runs ok from my side here :-) > > -- > Regards, > bibs M. > > Host/Kernel/OS ?"cc000002695" running Linux 2.6.31-5.slh.4-sidux-686 [sidux > 2009-02 ????? - kde-full - (200907141427) ] > www.sidux.com > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > First of all it's too long to paste into an email, especially if you'rte using a client that strips leading whitespace. Paste it into a pastebin. I personally prefer python.codepad.org, as it lets you test code on the server, unless it depends on third party modules. Uplaod it to a pastebin, send us the link, and we might consider taking a look. -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From bibsmendez at gmail.com Mon Dec 14 04:09:17 2009 From: bibsmendez at gmail.com (bibi midi) Date: Mon, 14 Dec 2009 06:09:17 +0300 Subject: [Tutor] Read-create-edit file In-Reply-To: References: <4B2541F7.5030106@gmail.com> Message-ID: On Mon, Dec 14, 2009 at 1:17 AM, Rich Lovely wrote: > > First of all it's too long to paste into an email, especially if > you'rte using a client that strips leading whitespace. Paste it into > a pastebin. I personally prefer python.codepad.org, as it lets you > test code on the server, unless it depends on third party modules. > > Uplaod it to a pastebin, send us the link, and we might consider taking a > look. > > > > Yes my bad, found out too late all > -- > Rich "Roadie Rich" Lovely > > There are 10 types of people in the world: those who know binary, > those who do not, and those who are off by one. > -- Best Regards, bibimidi -------------- next part -------------- An HTML attachment was scrubbed... URL: From bibsmendez at gmail.com Mon Dec 14 04:10:58 2009 From: bibsmendez at gmail.com (bibi midi) Date: Mon, 14 Dec 2009 06:10:58 +0300 Subject: [Tutor] Read-create-edit file In-Reply-To: References: <4B2541F7.5030106@gmail.com> Message-ID: On Mon, Dec 14, 2009 at 6:09 AM, bibi midi wrote: > > > On Mon, Dec 14, 2009 at 1:17 AM, Rich Lovely wrote: > >> >> First of all it's too long to paste into an email, especially if >> you'rte using a client that strips leading whitespace. Paste it into >> a pastebin. I personally prefer python.codepad.org, as it lets you >> test code on the server, unless it depends on third party modules. >> >> Uplaod it to a pastebin, send us the link, and we might consider taking a >> look. >> >> >> >> Yes my bad, found out too late. Sorry for that. Will post in pastebin or equal and give the link. -- Best Regards, bibimidi Sent from Riyadh, 01, Saudi Arabia -------------- next part -------------- An HTML attachment was scrubbed... URL: From bibsmendez at gmail.com Mon Dec 14 08:19:52 2009 From: bibsmendez at gmail.com (bibi midi) Date: Mon, 14 Dec 2009 10:19:52 +0300 Subject: [Tutor] Read-create-edit file In-Reply-To: References: <4B2541F7.5030106@gmail.com> Message-ID: Here's the link: http://paste.debian.net/53933/ On Mon, Dec 14, 2009 at 6:10 AM, bibi midi wrote: > > > Yes my bad, found out too late. Sorry for that. Will post in pastebin or > equal and give the link. > > > -- > Best Regards, > bibimidi > > -- Best Regards, bibimidi -------------- next part -------------- An HTML attachment was scrubbed... URL: From castornightmare at gmail.com Mon Dec 14 13:51:53 2009 From: castornightmare at gmail.com (alex loutrbringa') Date: Mon, 14 Dec 2009 13:51:53 +0100 Subject: [Tutor] Telnet and pexpect module Message-ID: Hi everybody, I hope i'm in the right place. I'm trying to automate an operation we usually do manually with some RS232(serial port)/ethernet MOXA switches. This operation reboots a linux debian device by sending a "magic-sysrq+b" command to the kernel if this device does not respond through SSH. To do that manually, we do : # this is the connection to the RS/232-ethernet port, this switch is connected with the linux device through a serial port # and connected to my LAN with an ethernet cable. The switch serial port 1 is bound to tcp port 100. alex at jazz:~$ telnet 10.1.1.1 100 Trying 10.1.1.1 ... Connected to 10.1.1.1. Escape character is '^]'. # here, i can press "enter" to access to the linux prompt # or i can enter "ctrl+]" to access the telnet prompt # here, i enter "ctrl + ]" and get the switch prompt telnet> # now i enter "send brk" then "enter", this is the same thing than the linux "magic sysrq" telnet> send brk # now i press "b" (without enter), then the device directly "hard reboot" with a short message like "resetting system" So, i want to do this with pexpect, here is my script : ------------------------------- import pexpect import sys import time import os switch_ip_address = sys.argv[1] switch_port = sys.argv[2] sysrq_key = sys.argv[3] print('ip address is: ' + switch_ip_address) print('switch port number is: ' + switch_port) print('sysrq key is: ' + sysrq_key) telnet_child = pexpect.spawn('telnet ' + switch_ip_address + ' ' + switch_port) telnet_child.expect('Escape.*') telnet_child.sendcontrol(']') telnet_child.expect('telnet> ') telnet_child.sendline('send brk') #time.sleep(0.5) telnet_child.send(sysrq_key) ------------------------------- It looks like it is working almost fine, when i run my script with : python send_brk.py 10.1.1.1 100 b the script end without errors. Then i connect to the switch to check if the device has rebooted but it has not : ------------------------------- athaveau at athaveau:~$ telnet 10.1.1.1 100 Trying 10.1.1.1... Connected to 10.1.1.1. Escape character is '^]'. ------------------------------- Then when i press enter, i get that : ------------------------------- SysRq : HELP : loglevel0-8 reBoot tErm Full kIll show-backtrace-all-active-cpus(L) showMem Nice showPc Sync showTasks Unmount showLocks ------------------------------- this is the magic sysrq helper, so it looks like the last thing i sent: the "b" (sysrq_key variable) letter is not sent to the telnet command but i can't understand why. I know this is a long and certainly a little bit boring :-) but if someone has an idea ... it would very pleasant ! Thanks by advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Mon Dec 14 20:27:05 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 15 Dec 2009 06:27:05 +1100 Subject: [Tutor] Telnet and pexpect module In-Reply-To: References: Message-ID: On 12/14/2009 11:51 PM, alex loutrbringa' wrote: > Hi everybody, > > I hope i'm in the right place. I'm trying to automate an operation we > usually do manually with some RS232(serial port)/ethernet MOXA switches. > This operation reboots a linux debian device by sending a > "magic-sysrq+b" command to the kernel if this device does not respond > through SSH. > To do that manually, we do : For your circumstance, can you write to the special file instead? #for Alt+SysRq+b (reBoot) echo b > /proc/sysrq-trigger and I've never used this, but you might be interested to see sysrqd http://julien.danjou.info/sysrqd/ From grigor.kolev at gmail.com Tue Dec 15 17:42:24 2009 From: grigor.kolev at gmail.com (=?windows-1251?B?w/Do4+7w?=) Date: Tue, 15 Dec 2009 18:42:24 +0200 Subject: [Tutor] Python and database firebird Message-ID: Hi I have a damaged database and try to fix it. How do I delete the bad record. When you run the following code: --------------------------------- base=connect(dsn=r'C:\python26\vmv.gdb', user='eltrade', password='gdelchev', dialect=1, charset='win1251') cur = base.cursor() cur.execute('select * from posbills_plu') cur.fetchall() -------------------------------- Error is: Traceback (most recent call last): File "C:\Python26\base.py", line 9, in test() File "C:\Python26\base.py", line 7, in test cur.fetchall() ProgrammingError: (-902, 'fetch: \n database file appears corrupt ()\n bad checksum\n checksum error on database page 12839') -- ????? ???? ???? ???, ???? ???? ??????? ?? ???? ?? ???? ! -------------- next part -------------- An HTML attachment was scrubbed... URL: From bludvigsen at gmail.com Tue Dec 15 17:43:54 2009 From: bludvigsen at gmail.com (Bjorn Egil Ludvigsen) Date: Tue, 15 Dec 2009 10:43:54 -0600 Subject: [Tutor] Object references and caching/interning Message-ID: <5ec0fe120912150843k779d1d98t90d01cd0e6be7e@mail.gmail.com> Hi, This is my frist post here. I am learning Python and Qt and am trying to understand a pecularity with object references. I think I get the integer part, but for floating point it seems like the caching/interning process works a little bit here and there: 1) Same object on command line >>> a, b = 5.0, 5.0 >>> id(a), id(b) (11140648, 11140648) 2) Different object on command line >>> a = 5.0 >>> b = 5.0 >>> id(a), id(b) (11140616, 11140600) 3) Same object in script When running the last assignments for the float objects in a script (point 2) above, one line per assignment), it is cached again and the variables refer to the same object. Why is it different on a command line and in a script? How can you know when you create a new object or not, are there any rules? Thanks for a great forum. Regards, Bjorn -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Dec 15 18:09:03 2009 From: bgailer at gmail.com (bob gailer) Date: Tue, 15 Dec 2009 12:09:03 -0500 Subject: [Tutor] Python and database firebird In-Reply-To: References: Message-ID: <4B27C2AF.1070400@gmail.com> ?????? wrote: > Hi > I have a damaged database and try to fix it. > How do I delete the bad record. I think this is a Firebird issue, not something you can address via Python. > When you run the following code: > --------------------------------- > base=connect(dsn=r'C:\python26\vmv.gdb', user='eltrade', > password='gdelchev', dialect=1, charset='win1251') > cur = base.cursor() > cur.execute('select * from posbills_plu') > cur.fetchall() > -------------------------------- > Error is: > Traceback (most recent call last): > File "C:\Python26\base.py", line 9, in > test() > File "C:\Python26\base.py", line 7, in test > cur.fetchall() > ProgrammingError: (-902, 'fetch: \n database file appears corrupt ()\n > bad checksum\n checksum error on database page 12839') > > -- > ????? ???? ???? ???, ???? ???? ??????? ?? ???? ?? ???? ! > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Tue Dec 15 18:20:16 2009 From: bgailer at gmail.com (bob gailer) Date: Tue, 15 Dec 2009 12:20:16 -0500 Subject: [Tutor] Entry-level open source opportunities In-Reply-To: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> References: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> Message-ID: <4B27C550.8010001@gmail.com> Kent Johnson wrote: > OpenHatch is a new web site that compiles entry-level opportunities > with open-source programming projects. Many of the projects are in > Python. If you are looking for a way to get involved with an open > source project, this could be a useful resource. > https://openhatch.org/search/ > I do not understand what I see on this site or how to use it. Are there any instructions? -- Bob Gailer Chapel Hill NC 919-636-4239 From luhmann_br at yahoo.com Tue Dec 15 18:19:27 2009 From: luhmann_br at yahoo.com (Luhmann) Date: Tue, 15 Dec 2009 09:19:27 -0800 (PST) Subject: [Tutor] faster substring replacement Message-ID: <500540.10196.qm@web30903.mail.mud.yahoo.com> Hi folks, I'm trying to do something like this: >>> evildict= {'good' : 'bad' , 'love' : 'hate' , 'God': 'Satan'} >>> def make_evil(text) ... ?? ? ? for a in evildict: ... ? ? ? ?? ??? text=text.replace(a, evildict[a]) ... ? ? ? ? ? ?? return text ??? This works fine, but it soon gets too slow as the size of text and dictionary begin to grow. Can you guys suggest me a way to make it faster? Devenez un meilleur amigo gr?ce ? Yahoo! Courriel http://cf.promos.yahoo.com/courriel/visiteguidee2/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 15 18:36:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Dec 2009 17:36:20 -0000 Subject: [Tutor] Python and database firebird References: Message-ID: "??????" wrote > I have a damaged database and try to fix it. > How do I delete the bad record. You probably need to use Firebird's admin tools. I've never used Firebird although I did use the commercial version, Interbase and it had several admin tools. But if it's badly corrupted you may need to restore from a backup then apply updates from the audit logs (if you have them!) > ProgrammingError: (-902, 'fetch: \n database file appears corrupt ()\n > bad > checksum\n checksum error on database page 12839') This is a Firebird error rather than a Python issue. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jfabiani at yolo.com Tue Dec 15 20:17:46 2009 From: jfabiani at yolo.com (John) Date: Tue, 15 Dec 2009 11:17:46 -0800 Subject: [Tutor] Entry-level open source opportunities In-Reply-To: <4B27C550.8010001@gmail.com> References: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> <4B27C550.8010001@gmail.com> Message-ID: <200912151117.46683.jfabiani@yolo.com> On Tuesday 15 December 2009 09:20:16 am bob gailer wrote: > Kent Johnson wrote: > > OpenHatch is a new web site that compiles entry-level opportunities > > with open-source programming projects. Many of the projects are in > > Python. If you are looking for a way to get involved with an open > > source project, this could be a useful resource. > > https://openhatch.org/search/ > > I do not understand what I see on this site or how to use it. Are there > any instructions? Also the dates of the reports bug and issues are old. On the second page the date is 2006? Johnf From mail at timgolden.me.uk Tue Dec 15 21:25:57 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 15 Dec 2009 20:25:57 +0000 Subject: [Tutor] faster substring replacement In-Reply-To: <500540.10196.qm@web30903.mail.mud.yahoo.com> References: <500540.10196.qm@web30903.mail.mud.yahoo.com> Message-ID: <4B27F0D5.6010405@timgolden.me.uk> Luhmann wrote: > Hi folks, > > I'm trying to do something like this: > >>>> evildict= {'good' : 'bad' , 'love' : 'hate' , 'God': 'Satan'} > >>>> def make_evil(text) > ... for a in evildict: > ... text=text.replace(a, evildict[a]) > ... return text > > > This works fine, but it soon gets too slow as the size of text and dictionary begin to grow. > Can you guys suggest me a way to make it faster? You're basically moving slowly towards a templating mechanism. Depends on just how fancy you want to get. In short, to do this kind of thing, the steps are: 1) Roll-your-own dict solution [what you've done] 2) Use re.sub with/without callbacks 3) Use an existing templating solution (of which there are many in the Python world). Exactly how far down that line you go depends on the complexity and need for speed. But I'm surprised that the dictionary solution is that slow. Especially since the code you quote above only actually makes one replacement before it returns the altered string :) (You could probably squeeze a tiny bit more juice out of it my iterating over the dict items rather than they keys: for old, new in evildict.items (): text = text.replace (old, new) ) TJG From kent37 at tds.net Tue Dec 15 21:53:35 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 15 Dec 2009 15:53:35 -0500 Subject: [Tutor] Object references and caching/interning In-Reply-To: <5ec0fe120912150843k779d1d98t90d01cd0e6be7e@mail.gmail.com> References: <5ec0fe120912150843k779d1d98t90d01cd0e6be7e@mail.gmail.com> Message-ID: <1c2a2c590912151253j7c77d30eh87c5a77ca2a76e5d@mail.gmail.com> On Tue, Dec 15, 2009 at 11:43 AM, Bjorn Egil Ludvigsen wrote: > Hi, > > This is my frist post here. I am learning Python and Qt and am trying to > understand a pecularity with object references. I think I get the integer > part, but for floating point it seems like the caching/interning process > works a little bit here and there: > > 1) Same object on command line >>>> a, b = 5.0, 5.0 >>>> id(a), id(b) > (11140648, 11140648) > 2) Different object on command line >>>> a = 5.0 >>>> b = 5.0 >>>> id(a), id(b) > (11140616, 11140600) > > 3) Same object in script > When running the last assignments for the float objects in a script (point > 2) above, one line per assignment), it is cached again and the variables > refer to the same object. Why is it different on a command line and in a > script? > > How can you know when you create a new object or not, are there any rules? I don't know why it is different, but really, you shouldn't worry about it. Any caching or interning is implementation dependent and it is rare that you need to worry about ids or object identity (vs equality) except in special cases like None or marker objects that you create. Kent From kent37 at tds.net Tue Dec 15 22:08:07 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 15 Dec 2009 16:08:07 -0500 Subject: [Tutor] Entry-level open source opportunities In-Reply-To: <200912151117.46683.jfabiani@yolo.com> References: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> <4B27C550.8010001@gmail.com> <200912151117.46683.jfabiani@yolo.com> Message-ID: <1c2a2c590912151308q3c663a9djf6469918470ec056@mail.gmail.com> On Tue, Dec 15, 2009 at 2:17 PM, John wrote: > On Tuesday 15 December 2009 09:20:16 am bob gailer wrote: >> Kent Johnson wrote: >> > OpenHatch is a new web site that compiles entry-level opportunities >> > with open-source programming projects. Many of the projects are in >> > Python. If you are looking for a way to get involved with an open >> > source project, this could be a useful resource. >> > https://openhatch.org/search/ >> >> I do not understand what I see on this site or how to use it. Are there >> any instructions? It is basically an aggregator of bug reports that have been marked as "easy". You can browse them and find a bug that you are interested in fixing. Try browsing the bugs or the website itself. > Also the dates of the reports bug and issues are old. ?On the second page the > date is 2006? I'm not sure what second page you mean, but certainly there could be old bugs listed. Kent From kent37 at tds.net Tue Dec 15 22:13:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 15 Dec 2009 16:13:22 -0500 Subject: [Tutor] faster substring replacement In-Reply-To: <500540.10196.qm@web30903.mail.mud.yahoo.com> References: <500540.10196.qm@web30903.mail.mud.yahoo.com> Message-ID: <1c2a2c590912151313m7ccc86e5xc5303c72a67cc769@mail.gmail.com> On Tue, Dec 15, 2009 at 12:19 PM, Luhmann wrote: > Hi folks, > > I'm trying to do something like this: > > >>> evildict= {'good' : 'bad' , 'love' : 'hate' , 'God': 'Satan'} > > >>> def make_evil(text) > ... for a in evildict: > ... text=text.replace(a, evildict[a]) > ... return text > > > This works fine, but it soon gets too slow as the size of text and > dictionary begin to grow. > Can you guys suggest me a way to make it faster? > > Build a regex out of the keys. Provide a function to re.sub() that looks up the match in the dict and returns the associated value. See http://code.activestate.com/recipes/81330/ Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Tue Dec 15 22:46:37 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Tue, 15 Dec 2009 13:46:37 -0800 Subject: [Tutor] Need a better name for this function Message-ID: def float_to_exact_number_stored_in_computer(f): """ Given a float, return the exact number stored in computer. See http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error """ from decimal import Decimal return Decimal.from_float(f) I've just borrowed from the doc, but that name is too long, isn't it? Please suggest something shorter but still meaningful. Thanks, Dick Moores -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Tue Dec 15 22:51:16 2009 From: hugo.yoshi at gmail.com (Hugo) Date: Tue, 15 Dec 2009 22:51:16 +0100 Subject: [Tutor] Need a better name for this function In-Reply-To: References: Message-ID: <4B2804D4.8050909@gmail.com> On 15-12-2009 22:46, Richard D. Moores wrote: > def float_to_exact_number_stored_in_computer(f): > """ > Given a float, return the exact number stored in computer. > > See > http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error > """ > from decimal import Decimal > return Decimal.from_float(f) > > I've just borrowed from the doc, but that name is too long, isn't it? > Please suggest something shorter but still meaningful. > > Thanks, > > Dick Moores What's wrong with just using the Decimal.from_float function directly? This wrapper doesn't really do anything, seems like having a separate function would be unnecessary. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 15 23:08:09 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Dec 2009 22:08:09 -0000 Subject: [Tutor] Need a better name for this function References: Message-ID: "Richard D. Moores" wrote > def float_to_exact_number_stored_in_computer(f): > from decimal import Decimal > return Decimal.from_float(f) > > I've just borrowed from the doc, but that name is too long, isn't it? > Please > suggest something shorter but still meaningful. If it were C I'd say f2d() But since its Python why not float2Decimal()? Since thats what it actually does... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From zstumgoren at gmail.com Tue Dec 15 23:56:20 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 15 Dec 2009 17:56:20 -0500 Subject: [Tutor] another unit-testing question: regex testing Message-ID: Hi everyone, To continue on the popular topic of unit tests, I was wondering if it's generally preferable to use a single unit test or a series of unit tests when testing against a regex pattern. In my specific case, I started out with a single test that contained a (growing) list of bad input data intended to fail when tried against a regex pattern. But then I started thinking: Isn't each unit test supposed to test one thing and one thing only? So I broke up my list of inputs and devoted a single unit test to each (see below for both of my iterations). On the one hand, I can see the benefit of having individual unit tests for each of these varying inputs, since I can then see precisely which one failed when I run my tests. On the other hand, this one-test-per-pattern seems like a bit of overkill given that I built up this list of bad inputs sequentially. In other words, I added the first bad input to my initial list (in "testIDFormat"), then ran the tests; then I added another failing input and ran the tests; and so on. In that way, I figured I could gradually build up a list of known bad inputs, and simply append any new bad inputs that crop up later on. So my question -- in this particular case involving a regex, what downsides are there of simply lumping all the known bad values into a single unit test? Hopefully that makes sense... Regards, Serdar << FIRST ITERATION >>> class TestOffice(unittest.TestCase): def testIDFormat(self): "Office constructor fails unless ID format is 'LETTERS-DIGITS'" ids = ['ABERCROMBIE_1', 'abercrombie-1', 'NEIL ABERCROMBIE-1', 'ABERCROMBIE.JR-1', 'ABERCROMBIEJR-1234' ] for id in ids: self.assertRaises(models.IDError, models.Office, id) <<< SECOND ITERATION >>> class TestOfficeBadIDFormat(unittest.TestCase): def testLowerCase(self): """Office constructor fails with lower-case name in ID""" self.assertRaises(models.IDError, models.Office,'abercrombie-1') def testNoDigits(self): """Office constructor fails with no digits at end of ID""" self.assertRaises(models.IDError, models.Office,'ABERCROMBIE') def testTooManyDigits(self): """Office constructor fails with more than 3 digits at end of ID""" self.assertRaises(models.IDError, models.Office,'ABERCROMBIEJR-1234') def testWrongConnector(self): """Office constructor fails if connector is not a minus sign""" self.assertRaises(models.IDError, models.Office,'ABERCROMBIE_1') def testSpacesInID(self): """Office constructor fails if ID contains spaces""" self.assertRaises(models.IDError, models.Office,'NEIL ABERCROMBIE-1') def testDotsInID(self): """Office constructor fails if ID contains dots""" self.assertRaises(models.IDError, models.Office,'ABERCROMBIE.JR-1') From davea at ieee.org Wed Dec 16 00:05:33 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 15 Dec 2009 18:05:33 -0500 Subject: [Tutor] Need a better name for this function In-Reply-To: References: Message-ID: <4B28163D.8080403@ieee.org> Richard D. Moores wrote: > def float_to_exact_number_stored_in_computer(f): > """ > Given a float, return the exact number stored in computer. > > See > http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error > """ > from decimal import Decimal > return Decimal.from_float(f) > > I've just borrowed from the doc, but that name is too long, isn't it? Please > suggest something shorter but still meaningful. > > Thanks, > > Dick Moores > > As Hugo says, when your entire function consists of a single call to another one, you can usually simplify it. In this case, something like: import decimal float_to_decimal_approximation = decimal.Decimal.from_float No need to define a new function, all you're doing is giving it a new name. Actually, I think the existing name is clearer. As for the name being too long, the real question is what's the purpose of the function. It certainly doesn't give you an exact representation, as that cannot be done in general, without changing the defaults in the decimal module. For example, try 2.0 ** -50 The exact decimal version of that needs 50 digits, and Python 3.1 docs say it uses 28 by default.. DaveA From kent37 at tds.net Wed Dec 16 00:25:41 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 15 Dec 2009 18:25:41 -0500 Subject: [Tutor] another unit-testing question: regex testing In-Reply-To: References: Message-ID: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> On Tue, Dec 15, 2009 at 5:56 PM, Serdar Tumgoren wrote: > Hi everyone, > > To continue on the popular topic of unit tests, I was wondering if > it's generally preferable to use a single unit test or a series of > unit tests when testing against a regex pattern. > > In my specific case, I started out with a single test that contained a > (growing) list of bad input data intended to fail when tried against a > regex pattern. > > But then I started thinking: Isn't each unit test supposed to test one > thing and one thing only? So I broke up my list of inputs and devoted > a single unit test to each (see below for both of my iterations). > > On the one hand, I can see the benefit of having individual unit tests > for each of these varying inputs, since I can then see precisely which > one failed when I run my tests. > > On the other hand, this one-test-per-pattern seems like a bit of > overkill given that I built up this list of bad inputs sequentially. > In other words, I added the first bad input to my initial list (in > "testIDFormat"), then ran the tests; then I added another failing > input and ran the tests; and so on. > > In that way, I figured I could gradually build up a list of known bad > inputs, and simply append any new bad inputs that crop up later on. > > So my question -- in this particular case involving a regex, what > downsides are there of simply lumping all the known bad values into a > single unit test? I often use a list of test cases to drive a single test. Using a series of tests is just too painful compared to making a simple list of test cases. The disadvantages to using a list of test cases: - it will die at the first failure, which may hide other failures - the default failure messages are generally not helpful - they won't necessarily tell you which test case failed. I usually build a custom failure message and use the assertXxx() method that takes a message parameter. (In your case, you will have to use a try / catch with a fail() in the try block because assertRaises() doesn't take a msg parameter.) Some of the alternate test runners (nose, py.test) may have better support for this style of testing but it works OK in unittest. Kent From zstumgoren at gmail.com Wed Dec 16 02:05:32 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Tue, 15 Dec 2009 20:05:32 -0500 Subject: [Tutor] another unit-testing question: regex testing In-Reply-To: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> References: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> Message-ID: > I often use a list of test cases to drive a single test. Using a > series of tests is just too painful compared to making a simple list > of test cases. I kinda suspected that but wasn't sure. These tests are for a REALLY basic regex and I was having nightmares thinking how many tests would be needed for a modestly more complex pattern. I'm leaning toward collapsing the tests back into a single unit-test. I should add, however, that the process of breaking these out separately led me to refine the regex and discover a few obvious tests I'd missed (e.g. test for blank input or no input). > The disadvantages to using a list of test cases: > - it will die at the first failure, which may hide other failures > - the default failure messages are generally not helpful - they won't > necessarily tell you which test case failed. That's the main benefit I noticed with the other approach. I usually build a custom > failure message and use the assertXxx() method that takes a message > parameter. (In your case, you will have to use a try / catch with a > fail() in the try block because assertRaises() doesn't take a msg > parameter.) Not sure I follow here. The try/catch should go in the unit test or the code under test? And finally, I was also wondering about testing classes and subclasses. For instance, if I have an Office class and a subclass Member, can I perform tests on any shared code (i.e. inherited by Member) solely against the superclass? Are there any specific types of tests that are standard for testing proper inheritance and other such issues? Would be happy to read up elsewhere if you all can point me to resources. Just haven't stumbled into any examples yet on this specific topic of testing subclasses relative to parent classes (Also, I know it's slightly off-topic, so I'd be happy to start a different thread if appropriate). Regards, Serdar From rdmoores at gmail.com Wed Dec 16 02:22:39 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Tue, 15 Dec 2009 17:22:39 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <4B28163D.8080403@ieee.org> References: <4B28163D.8080403@ieee.org> Message-ID: On Tue, Dec 15, 2009 at 15:05, Dave Angel wrote: > > Richard D. Moores wrote: >> >> def float_to_exact_number_stored_in_computer(f): >> ? """ >> ? Given a float, return the exact number stored in computer. >> >> ? See >> http://docs.python.org/3.1/tutorial/floatingpoint.html#representation-error >> ? """ >> ? from decimal import Decimal >> ? return Decimal.from_float(f) >> >> I've just borrowed from the doc, but that name is too long, isn't it? Please >> suggest something shorter but still meaningful. >> >> Thanks, >> >> Dick Moores >> >> > > As Hugo says, when your entire function consists of a single call to another one, you can usually simplify it. ?In this case, something like: > > import decimal > float_to_decimal_approximation = decimal.Decimal.from_float > No need to define a new function, all you're doing is giving it a new name. ?Actually, I think the existing name is clearer. If I keep the function, renamed to Allan's suggested float2Decimal(), then that's all I'll have to remember. But I see yours and Hugo's point. > As for the name being too long, the real question is what's the purpose of the function. I don't know if it will be useful or not, but it will satisfy my curiosity, given a float, as to what the "exact number stored in my computer is". > ?It certainly doesn't give you an exact representation, as that cannot be done in general, without changing the defaults in the decimal module. ?For example, try 2.0 ** -50 ? ?The exact decimal version of that needs 50 digits, and Python 3.1 docs say it uses 28 by default.. For (2.0 ** -50) I get 8.8817841970012523233890533447265625E-16, or 35 digits. But for an ordinary 1.341 I get 1.3409999999999999698019337301957421004772186279296875 -- 53 digits. Dave, I just realized that you used the term, "exact representation". What does that mean? If it means "exact decimal version", what does THAT mean? I'm not being facetious. I'm truly confused by this stuff -- "the exact number stored in my computer" is the first phrase I've understood. With 2.x I was totally in the dark about repr(). Dick From kent37 at tds.net Wed Dec 16 02:54:52 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 15 Dec 2009 20:54:52 -0500 Subject: [Tutor] another unit-testing question: regex testing In-Reply-To: References: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> Message-ID: <1c2a2c590912151754p6d5753b5neffff162ffc21e78@mail.gmail.com> On Tue, Dec 15, 2009 at 8:05 PM, Serdar Tumgoren wrote: > I usually build a custom >> failure message and use the assertXxx() method that takes a message >> parameter. (In your case, you will have to use a try / catch with a >> fail() in the try block because assertRaises() doesn't take a msg >> parameter.) > > Not sure I follow here. The try/catch should go in the unit test or > the code under test? In the unit test. Instead of for id in ids: self.assertRaises(models.IDError, models.Office, id) use something like this: for id in ids: try: models.Office(id) self.fail('No exception for ' + id) except models.IDError: pass > > And finally, I was also wondering about testing classes and > subclasses. For instance, if I have an Office class and a subclass > Member, can I perform tests on any shared code (i.e. inherited by > Member) solely against the superclass? Are there any specific types of > tests that are standard for testing proper inheritance and other such > issues? I can't think of specific issues with testing derived classes. I would test the base class methods in the base class tests. Generally you just have to test what is added. I think it is similar to building a function that depends on other functions. The idea is to test something well, so you believe that it works, then you don't have to test it anywhere else, you can rely on it. Kent From amonroe at columbus.rr.com Wed Dec 16 04:02:04 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 15 Dec 2009 22:02:04 -0500 Subject: [Tutor] faster substring replacement In-Reply-To: <500540.10196.qm@web30903.mail.mud.yahoo.com> References: <500540.10196.qm@web30903.mail.mud.yahoo.com> Message-ID: <1381389017231.20091215220204@columbus.rr.com> > Hi folks, > I'm trying to do something like this: >>>> evildict= {'good' : 'bad' , 'love' : 'hate' , 'God': 'Satan'} >>>> def make_evil(text) > ... ?? ? ? for a in evildict: > ... ? ? ? ?? ??? text=text.replace(a, evildict[a]) > ... ? ? ? ? ? ?? return text > ??? > This works fine, but it soon gets too slow as the size of text and dictionary begin to grow. > Can you guys suggest me a way to make it faster? Would it help to do it in linear fashion? Reading the source text one word at a time, doing a dict check for that word, then flushing that word to output? I'm wondering whether text.replace has to shove oodles of text to the right in memory when you replace a shorter word with a longer word. Someone else on the list may know. Alan From davea at ieee.org Wed Dec 16 04:32:10 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 15 Dec 2009 22:32:10 -0500 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B28163D.8080403@ieee.org> Message-ID: <4B2854BA.2040908@ieee.org> Richard D. Moores wrote: > On Tue, Dec 15, 2009 at 15:05, Dave Angel wrote: > >> Richard D. Moores wrote: > If I keep the function, renamed to Allan's suggested float2Decimal(), > then that's all I'll have to remember. But I see yours and Hugo's > point. > > Just remember, if the entire body of the function is just a call to the other one, all you need is an assignment. It'll go faster, too. So whatever you decide to call it, you just need something like: float2Decimal = decimal.Decimal.from_float >> As for the name being too long, the real question is what's the purpose of the function. >> > > I don't know if it will be useful or not, but it will satisfy my > curiosity, given a float, as to what the "exact number stored in my > computer is". > > But it doesn't do that. I'll try to elaborate below. >> It certainly doesn't give you an exact representation, as that cannot be done in general, without changing the defaults in the decimal module. For example, try 2.0 ** -50 The exact decimal version of that needs 50 digits, and Python 3.1 docs say it uses 28 by default.. >> > > For (2.0 ** -50) I get 8.8817841970012523233890533447265625E-16, or 35 > digits. But for an ordinary 1.341 I get > 1.3409999999999999698019337301957421004772186279296875 -- 53 digits. > > Dave, I just realized that you used the term, "exact representation". > What does that mean? If it means "exact decimal version", what does > THAT mean? I'm not being facetious. I'm truly confused by this stuff > -- "the exact number stored in my computer" is the first phrase I've > understood. With 2.x I was totally in the dark about repr(). > > Dick > > Don't be misled by repr(). It's not even intended to give you an exact representation, only to get one that can be used to reproduce the binary fp value you happen to have. And the success at doing that has varied from version to version of CPython. My background: In 1975 I implemented a decimal floating point system for a proprietary machine architecture. I started by implementing add and subtract, and ended up with logs, and trig. I was at that time very familiar with the tradeoffs of binary and decimal floating point. Float in most Python implementations is implemented in terms of the IEEE standard (754?) for binary floating point. On most (?) machines these days, there is direct microcode support for such numbers. For example, in the Pentium, the instruction set deals directly with them. Even back to the Intel 8088, there was in the early 80's an 8087 aux chip that provided this floating point. At one time I was quite familiar with most of the details, though I may not have them all perfect right now. A binary fp number takes up 64 bits, of which some 53 represent the mantissa, and most of the rest are the exponent. There are a bunch of tricky things, such as the sticky bit and gradual underflow, as well as a few NAN values, and some infinities. But most numbers can be understood if you have a good knowledge of both base 10 and base 2 arithmetic. Conversely, the Decimal package is a decimal system, implemented as variable precision, and a variable number of bytes. I tried in about 1984 convince the IEEE committee to include decimal fp at that time, but since their main goal was codifying the 8087 chip, they declined. There were other reasons as well, one of which is that a decimal number format doesn't fit as efficiently into memory. But IEEE did have another, later standard (854?) that was radix-independent, and variable precision. I think Python's Decimal package implements that standard, which isn't accelerated significantly by any processor I'm familiar with. So Decimal takes more space, and runs slower.. So why bother? Because it can represent exactly numbers the way human beings deal with them, and can do roundoff in familiar ways, and avoid quantization effects in confusing places. It's perfect for accounting type applications. Not only that, but converting back and forth to decimal strings need not introduce any more errors. For integers of reasonable sizes, the two are interchangeable. But for fractional values each has different characteristics. Take the number 1/3. Try to represent it in decimal, and it takes an infinite number of digits to get exact representation. So we use some elipses notation, and nobody minds too much. But the computer is more literal, and you have to be careful. If you take 0.3333333 and multiply by 3, you do not get 1.0. You get 0.9999999 Maybe you can round it, and maybe you can't. But a decimal package will have the same issues as a hand calculation. A binary system cannot represent 1/3 either. But it'll get a different error, and be off by a different amount. And by the time you get done converting to decimal for printout., the problem may be masked, or it may be made worse. But what about 1/10 ? In a decimal package you can represent that precisely, with just a few digits of precision. In binary fp, it is not exact. and if you have that number stored it'll be off a little bit. Converting it to decimal to print it out may make the error vanish, or it may make it worse. So applications do some rounding, before converting to printable form or after, or both. If you know your binary value is correct to 50 bits or so, and you round to 14 digits, it'll seem to be always accurate for nice values like 1/10. But a 14 digit decimal representation is not an exact representation of the binary value. To exactly represent a binary value with a 53 bit mantissa would take 53 decimal digits, worst case. Would they all have meaning in a realistic calculation? Nope. But when you say exact representation, that's what you'd need. (disclaimer: the 53 bits is a long-term memory, and is likely off by a couple. And other calculations could be plus or minus 1 or 2. I'm just writing this quickly. So maybe you'd need 54 decimal digits, or whatever.) HTH DaveA From rdmoores at gmail.com Wed Dec 16 05:12:16 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Tue, 15 Dec 2009 20:12:16 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <4B2854BA.2040908@ieee.org> References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> Message-ID: On Tue, Dec 15, 2009 at 19:32, Dave Angel wrote: > > Richard D. Moores wrote: >> >> On Tue, Dec 15, 2009 at 15:05, Dave Angel wrote: >> >>> >>> Richard D. Moores wrote: > > >> >> If I keep the function, renamed to Allan's suggested float2Decimal(), >> then that's all I'll have to remember. But I see yours and Hugo's >> point. >> >> > > Just remember, if the entire body of the function is just a call to the other one, all you need is an assignment. ?It'll go faster, too. ?So whatever you decide to call it, you just need something like: > ?float2Decimal = decimal.Decimal.from_float >>> >>> As for the name being too long, the real question is what's the purpose of the function. >>> >> >> I don't know if it will be useful or not, but it will satisfy my >> curiosity, given a float, as to what the "exact number stored in my >> computer is". >> >> > > But it doesn't do that. ?I'll try to elaborate below. Before I can go below I need to know if you are saying that the relevant doc is wrong. I took the original name for my function almost directly from it. Near the bottom of we find "meaning that the exact number stored in the computer is equal to the decimal value 0.1000000000000000055511151231257827021181583404541015625." And coincidence or no, that's precisely what float2Decimal() returns for 0.1 . Thanks, Dick From anand.shashwat at gmail.com Wed Dec 16 06:17:45 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Wed, 16 Dec 2009 10:47:45 +0530 Subject: [Tutor] Entry-level open source opportunities In-Reply-To: <1c2a2c590912151308q3c663a9djf6469918470ec056@mail.gmail.com> References: <1c2a2c590912121332j45c79b37l683668f64d1006dc@mail.gmail.com> <4B27C550.8010001@gmail.com> <200912151117.46683.jfabiani@yolo.com> <1c2a2c590912151308q3c663a9djf6469918470ec056@mail.gmail.com> Message-ID: @Kent : thanks, the site seems promising (inspired from GNOME-love) and can make entry easier ! On Wed, Dec 16, 2009 at 2:38 AM, Kent Johnson wrote: > On Tue, Dec 15, 2009 at 2:17 PM, John wrote: > > On Tuesday 15 December 2009 09:20:16 am bob gailer wrote: > >> Kent Johnson wrote: > >> > OpenHatch is a new web site that compiles entry-level opportunities > >> > with open-source programming projects. Many of the projects are in > >> > Python. If you are looking for a way to get involved with an open > >> > source project, this could be a useful resource. > >> > https://openhatch.org/search/ > >> > >> I do not understand what I see on this site or how to use it. Are there > >> any instructions? > > It is basically an aggregator of bug reports that have been marked as > "easy". You can browse them and find a bug that you are interested in > fixing. Try browsing the bugs or the website itself. > > > Also the dates of the reports bug and issues are old. On the second page > the > > date is 2006? > > I'm not sure what second page you mean, but certainly there could be > old bugs listed. > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Wed Dec 16 08:30:33 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 16 Dec 2009 08:30:33 +0100 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> Message-ID: <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> On Wed, Dec 16, 2009 at 5:12 AM, Richard D. Moores wrote: > > Before I can go below I need to know if you are saying that the > relevant doc is wrong. I took the original name for my function almost > directly from it. Near the bottom of > > we find "meaning that the exact number stored in the computer is equal > to the decimal value > 0.1000000000000000055511151231257827021181583404541015625." And > coincidence or no, that's precisely what float2Decimal() returns for > 0.1 . > The docs are right. The function will give you the exact value of any floating point number stored in memory. I think what Dave is trying to say is that if you want to store the exact value 0.1 on your computer, this function won't help you do that. It also won't help you avoid any kinds of rounding errors, which is worth mentioning. If you want 0.1 represented exactly you'll need to use the Decimal module all the way and avoid floating point entirely. Of course, if all you want is to get a better understanding of what's actually stored on your computer when you try to store 0.1 in a float, this can be a useful tool. I also recommend the article "What Every Computer Scientist Should Know About Floating-point Arithmetic." It's a very detailed explanation, though somewhat technical: http://docs.sun.com/source/806-3568/ncg_goldberg.html Hugo From rdmoores at gmail.com Wed Dec 16 09:44:36 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 16 Dec 2009 00:44:36 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> Message-ID: On Tue, Dec 15, 2009 at 23:30, Hugo Arts wrote: > On Wed, Dec 16, 2009 at 5:12 AM, Richard D. Moores wrote: >> >> Before I can go below I need to know if you are saying that the >> relevant doc is wrong. I took the original name for my function almost >> directly from it. Near the bottom of >> >> we find "meaning that the exact number stored in the computer is equal >> to the decimal value >> 0.1000000000000000055511151231257827021181583404541015625." And >> coincidence or no, that's precisely what float2Decimal() returns for >> 0.1 . >> > > The docs are right. The function will give you the exact value of any > floating point number stored in memory. OK! > I think what Dave is trying to > say is that if you want to store the exact value 0.1 on your computer, > this function won't help you do that. Yes, I knew that. > It also won't help you avoid any > kinds of rounding errors, which is worth mentioning. If you want 0.1 > represented exactly you'll need to use the Decimal module all the way > and avoid floating point entirely. > > Of course, if all you want is to get a better understanding of what's > actually stored on your computer when you try to store 0.1 in a float, > this can be a useful tool. Yes, that's what I wanted the function for, and with a name that would be easy to remember. > I also recommend the article "What Every > Computer Scientist Should Know About Floating-point Arithmetic." It's > a very detailed explanation, though somewhat technical: > > http://docs.sun.com/source/806-3568/ncg_goldberg.html I'm sleepy now, but will dig into it tomorrow. Thanks for your help, Hugo. Dick From denis.spir at free.fr Wed Dec 16 10:51:17 2009 From: denis.spir at free.fr (spir) Date: Wed, 16 Dec 2009 10:51:17 +0100 Subject: [Tutor] another unit-testing question: regex testing In-Reply-To: References: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> Message-ID: <20091216105117.2623946f@o> Serdar Tumgoren dixit: > > I often use a list of test cases to drive a single test. Using a > > series of tests is just too painful compared to making a simple list > > of test cases. > > I kinda suspected that but wasn't sure. These tests are for a REALLY > basic regex and I was having nightmares thinking how many tests would > be needed for a modestly more complex pattern. I'm leaning toward > collapsing the tests back into a single unit-test. I should add, > however, that the process of breaking these out separately led me to > refine the regex and discover a few obvious tests I'd missed (e.g. > test for blank input or no input). I found existing test libs to be difficult to adapt to text matching/parsing/processing tasks. So I ended up writing my own testing utilities. But the context is different: it's for a custom matching library, with pattern objects for which testing tools are methods. Anyway, someone may find it interesting. There are two main tools: * One method performs at test match of a given input against a given pattern (with a given method, for there are 'match', 'parse', 'findFirst', 'findAll', 'replace) and clearly prints out source/pattern/outcome (which can be failure). Also, this method catches & prints out exceptions, so that any number of trials can be chained in a hand-written testing func, & run as expected without stopping at first failure. This is the base method for my tests at develoment time. * Another tool is rather used for lasting test suites, with maintenance in mind. A first method builds a 'testsuiteDict' from a set of source strings for a given pattern (again failures included). The outcome is a plain python dict with (source:result) pairs. Depending on the case, results are (like python repr()) re-readible object representations, or any kind of reliable string representations. The aim is that they can be compared to later results obtained from running the same test suite. Precisely, the second method 'testSuite' is passed a pattern and a previously built test-suite dict, runs the trials for the list of source strings and checks the outcomes against the previously recorded results. This provides a base for secure long-term maintenance. But testsuiteDict can also be used alone simply to run a test suite for a given pattern: in addition to returning the dict, it also prints it in clear format (but still grammatically correct to read it back), so one can easily chack the series of source:result pairs. It's a kind of compact form of the first method above, but for a single pattern. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From denis.spir at free.fr Wed Dec 16 10:55:49 2009 From: denis.spir at free.fr (spir) Date: Wed, 16 Dec 2009 10:55:49 +0100 Subject: [Tutor] Need a better name for this function In-Reply-To: <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> Message-ID: <20091216105549.0dfd4c29@o> Hugo Arts dixit: > The function will give you the exact value of any > floating point number stored in memory. To be even clearer: Seems you messed up "the exact value of any floating point number stored in memory" with "an accurate value (= the intended value)". ________________________________ la vita e estrany http://spir.wikidot.com/ From davea at ieee.org Wed Dec 16 11:48:23 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 16 Dec 2009 05:48:23 -0500 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> Message-ID: <4B28BAF7.2090208@ieee.org> Richard D. Moores wrote: > On Tue, Dec 15, 2009 at 23:30, Hugo Arts wrote: > >> On Wed, Dec 16, 2009 at 5:12 AM, Richard D. Moores wrote: >> >>> Before I can go below I need to know if you are saying that the >>> relevant doc is wrong. I took the original name for my function almost >>> directly from it. Near the bottom of >>> >>> we find "meaning that the exact number stored in the computer is equal >>> to the decimal value >>> 0.1000000000000000055511151231257827021181583404541015625." And >>> coincidence or no, that's precisely what float2Decimal() returns for >>> 0.1 . >>> >>> >> The docs are right. The function will give you the exact value of any >> floating point number stored in memory. >> > > OK! > > >> I think what Dave is trying to >> say is that if you want to store the exact value 0.1 on your computer, >> this function won't help you do that. >> > > Yes, I knew that. > > >> It also won't help you avoid any >> kinds of rounding errors, which is worth mentioning. If you want 0.1 >> represented exactly you'll need to use the Decimal module all the way >> and avoid floating point entirely. >> >> Of course, if all you want is to get a better understanding of what's >> actually stored on your computer when you try to store 0.1 in a float, >> this can be a useful tool. >> > > Yes, that's what I wanted the function for, and with a name that would > be easy to remember. > > >> I also recommend the article "What Every >> Computer Scientist Should Know About Floating-point Arithmetic." It's >> a very detailed explanation, though somewhat technical: >> >> http://docs.sun.com/source/806-3568/ncg_goldberg.html >> > > I'm sleepy now, but will dig into it tomorrow. > > Thanks for your help, Hugo. > > Dick > > Hugo is right, and I was a little bit wrong. Not about the decimal versus binary floating point, but about how 3.1's from_float() method works. It's new in 3.1, and I didn't play with it enough. Apparently, it adjusts the precision of the generated Decimal value so that it *can* represent the binary value exactly. So the doc is correct that the representation is exact. Note also that it takes 56 digits to do that, which is part of what I was trying to say. Although I was remembering 53 bits for the mantissa of binary-fp, the concepts were right. It takes about as many decimal digits to do it as it took for mantissa bits in the binary fp value. If you really want to see what binary fp does, you might need to resort to hex. Two methods of float are relevant, hex() and fromhex(). Check out the following function I just wrote. import decimal float2decimal = decimal.Decimal.from_float a = 0.1 print("internal repr of 0.1 is ", a.hex()) # 0x1.999999999999ap-4 def test(stringval): floatval = float.fromhex(stringval) decimalval = float2decimal(floatval) print( stringval, "--", floatval, "--", decimalval ) test(" 0x1.9999999999999p-4") test(" 0x1.999999999999ap-4") #This is 0.1, as best as float sees it test(" 0x1.999999999999bp-4") The output is (re-wordwrapped for email): internal repr of 0.1 is 0x1.999999999999ap-4 0x1.9999999999999p-4 -- 0.1 -- 0.09999999999999999167332731531132594682276248931884765625 0x1.999999999999ap-4 -- 0.1 -- 0.1000000000000000055511151231257827021181583404541015625 0x1.999999999999bp-4 -- 0.1 -- 0.10000000000000001942890293094023945741355419158935546875 Notice that these are the closest values to 0.1 that can be represented in a float, one below, and the first two above. You can't get any values between those. When you print any of them, it shows a value of 0.1, presumably due to rounding during conversion to a decimal string. The algorithm used in such conversion has changed many times in the evolution of CPython. And in case it's not obvious, the stringval items are 14 hex digits (56 bits), plus the letter "p", a sign, and an exponent value. These are encoded into 64bits as a float. You can see such a value for any float you like, by using the method hex() on the float object. Hugo: thanks for the article reference. I was pleased to see it was inspired by a lecture of professor Kahan. I haven't met him in a couple of decades. He was the brains behind much of Intel's 8087 implementation, which was more-or-less used as a reference implementation by the IEEE standard. And of course he was active on the standard. I do recall there were a few liberties that the 8087 took that he wanted to be illegal in the standard, but generally it was assumed that the standard needed a real silicon implementation in order to succeed. I wish I had a tenth of his smarts, with respect to floating point. DaveA From davea at ieee.org Wed Dec 16 11:55:02 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 16 Dec 2009 05:55:02 -0500 Subject: [Tutor] faster substring replacement In-Reply-To: <1381389017231.20091215220204@columbus.rr.com> References: <500540.10196.qm@web30903.mail.mud.yahoo.com> <1381389017231.20091215220204@columbus.rr.com> Message-ID: <4B28BC86.6080001@ieee.org> R. Alan Monroe wrote: > > > I'm wondering whether text.replace has to shove oodles of text to the > right in memory when you replace a shorter word with a longer word. > Someone else on the list may know. > > Alan > > > Since a string is immutable, replace() has to copy the string. So it doesn't need to be "shoved to the right" or left. First copy the part before the match. Then copy the substituted value. Then copy the part after the match. Still, that's lots of copying. So it definitely may pay off to deal with the text in smaller pieces, either lines or "words." But notice that the replace() method has to be able to handle strings that may contain white space, so the optimization needs to be done by the developer who knows the constraints of the actual dictionary. DaveA From zstumgoren at gmail.com Wed Dec 16 16:48:12 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Wed, 16 Dec 2009 10:48:12 -0500 Subject: [Tutor] another unit-testing question: regex testing In-Reply-To: <20091216105117.2623946f@o> References: <1c2a2c590912151525g3957d3f3rfb9b4cfe50609060@mail.gmail.com> <20091216105117.2623946f@o> Message-ID: > I found existing test libs to be difficult to adapt to text matching/parsing/processing tasks. So I ended up writing my own testing utilities. But the context is different: it's for a custom matching library, with pattern objects for which testing tools are methods. Anyway, someone may find it interesting. There are two main tools: Interesting. Are these open-source by any chance?;) From rdmoores at gmail.com Wed Dec 16 23:48:38 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 16 Dec 2009 14:48:38 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <4B28BAF7.2090208@ieee.org> References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> Message-ID: On Wed, Dec 16, 2009 at 02:48, Dave Angel wrote: > Richard D. Moores wrote: > If you really want to see what binary fp does, you might need to resort to > hex. ?Two methods of float are relevant, hex() and fromhex(). ?Check out the > following function I just wrote. > > import decimal > float2decimal = decimal.Decimal.from_float > > a = 0.1 > print("internal repr of 0.1 is ", a.hex()) ? ?# 0x1.999999999999ap-4 > > > def test(stringval): > ? floatval = float.fromhex(stringval) > ? decimalval = float2decimal(floatval) > ? print( stringval, "--", floatval, "--", decimalval ) > > > test(" 0x1.9999999999999p-4") > test(" 0x1.999999999999ap-4") ? ?#This is 0.1, as best as float sees it > test(" 0x1.999999999999bp-4") > > > The output is (re-wordwrapped for email): > > internal repr of 0.1 is ?0x1.999999999999ap-4 > 0x1.9999999999999p-4 -- 0.1 > ? ?-- 0.09999999999999999167332731531132594682276248931884765625 > 0x1.999999999999ap-4 -- 0.1 > ? ?-- 0.1000000000000000055511151231257827021181583404541015625 > 0x1.999999999999bp-4 -- 0.1 > ? ?-- 0.10000000000000001942890293094023945741355419158935546875 > > Notice that these are the closest values to 0.1 that can be represented in a > float, one below, and the first two above. ?You can't get any values between > those. ?When you print any of them, it shows a value of 0.1, presumably due > to rounding during conversion to a decimal string. ?The algorithm used in > such conversion has changed many times in the evolution of CPython. > > And in case it's not obvious, the stringval items are 14 hex digits (56 > bits), plus the letter "p", a sign, and an exponent value. ?These are > encoded into 64bits as a float. Dave, that's enlightening. Thanks very much. >You can see such a value for any float you > like, by using the method hex() on the float object. Yes, I'd like to generalize that and include it in my function (renamed of course). How can I compute the equivalents of your "0x1.9999999999999p-4 -- 0.1" and "0x1.999999999999bp-4 -- 0.1", which are, respectively, the closest hex value to a.hex() that is smaller than a.hex(), and the the closest hex value to a.hex() that is greater than a.hex()? This is what I have so far: ====================================================== import decimal float2decimal = decimal.Decimal.from_float a = .435 stringval = a.hex() print("internal repr of", a, "is ", stringval) def test(stringval): floatval = float.fromhex(stringval) decimalval = float2decimal(floatval) print( stringval, "--", floatval, "--", decimalval ) test(stringval) OUTPUT: internal repr of 0.435 is 0x1.bd70a3d70a3d7p-2 0x1.bd70a3d70a3d7p-2 -- 0.435 -- 0.434999999999999997779553950749686919152736663818359375 ============================================= Thanks, Dick From rdmoores at gmail.com Thu Dec 17 00:06:19 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 16 Dec 2009 15:06:19 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> Message-ID: On Wed, Dec 16, 2009 at 14:48, Richard D. Moores wrote: I just realized that my question was absurd, in that given real numbers n and x there is no x such that both x < n and x is greater than all other numbers less than n. So after inserting "(in 14 hex digits)" at 2 places, my question is: How can I compute the equivalents of your "0x1.9999999999999p-4 -- 0.1" and "0x1.999999999999bp-4 -- 0.1", which are, respectively, the closest hex value (in 14 hex digits) to a.hex() that is smaller than a.hex(), and the the closest hex value (in 14 hex digits) to a.hex() that is greater than a.hex()? > This is what I have so far: > ====================================================== > import decimal > float2decimal = decimal.Decimal.from_float > > a = .435 > stringval = a.hex() > print("internal repr of", a, "is ", stringval) > > > def test(stringval): > ? ?floatval = float.fromhex(stringval) > ? ?decimalval = float2decimal(floatval) > ? ?print( stringval, "--", floatval, "--", decimalval ) > > test(stringval) > > OUTPUT: > internal repr of 0.435 is ?0x1.bd70a3d70a3d7p-2 > 0x1.bd70a3d70a3d7p-2 -- 0.435 -- > 0.434999999999999997779553950749686919152736663818359375 > ============================================= > > Thanks, > > Dick > From davea at ieee.org Thu Dec 17 05:23:01 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 16 Dec 2009 23:23:01 -0500 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B28163D.8080403@ieee.org> <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> Message-ID: <4B29B225.2080902@ieee.org> Richard D. Moores wrote: > On Wed, Dec 16, 2009 at 14:48, Richard D. Moores wrote: > > I just realized that my question was absurd, in that given real > numbers n and x there is no x such that both x < n and x is greater > than all other numbers less than n. > > So after inserting "(in 14 hex digits)" at 2 places, my question is: > > How can I compute the equivalents of your > "0x1.9999999999999p-4 -- 0.1" and "0x1.999999999999bp-4 -- 0.1", which > are, respectively, the closest hex value (in 14 hex digits) to a.hex() > that is smaller > than a.hex(), and the the closest hex value (in 14 hex digits) to > a.hex() that is greater > than a.hex()? > > >> This is what I have so far: >> ====================================================== >> import decimal >> float2decimal = decimal.Decimal.from_float >> >> a = .435 >> stringval = a.hex() >> print("internal repr of", a, "is ", stringval) >> >> >> def test(stringval): >> floatval = float.fromhex(stringval) >> decimalval = float2decimal(floatval) >> print( stringval, "--", floatval, "--", decimalval ) >> >> test(stringval) >> >> OUTPUT: >> internal repr of 0.435 is 0x1.bd70a3d70a3d7p-2 >> 0x1.bd70a3d70a3d7p-2 -- 0.435 -- >> 0.434999999999999997779553950749686919152736663818359375 >> ============================================= >> >> Thanks, >> >> Dick >> >> > > There are conceivably better ways to get at the mantissa of the fp number, but you can simply parse the hex digits as I did manually, and add one and subtract one from the given mantissa (the part between the decimal point and the 'p'). Then it just remains to figure out which two of those three values actually span the desired value. Using the numbers and strings you supply, the three values would be: 0x1.bd70a3d70a3d6p-2 0x1.bd70a3d70a3d7p-2 0x1.bd70a3d70a3d8p-2 and the second one is somewhat less than .435, while the 3rd is more. Now, while this is good enough to do by hand, you have to realize there are some places it may not work, and another where it won't. I'm not sure whether trailing zeroes are always provided in the hex() method. So you may have to pad it out before adjusting the last digit. I'm also not positive how it normalizes the hex value printed out. As the example in the help indicates, there are more than one hex string that can be converted to the same float - if you denormalize, it'll still convert it. I just don't know if hex() ever produces a non-normalized value. More drastically, if you're trying to be complete, is the infinities, the NAN values, and the numbers, *very* close to zero, where gradual underflow takes effect. The notion here is that when the exponent gets as negative as it can safely store, the standard requires that the number be stored denormalized, rather than going immediately to zero. I don't know how those values are represented by the hex() method, but they have fewer significant digits, so the adjustment you would need would be in a different column. If I had to write a function to do what you ask, and if I couldn't get better specs as to what Python is actually doing with the hex() method, I'd work out an algorithm where tweak what seems to be the bottom digit, then see if the float has a new value. If it does not, I'm working on the wrong column. DaveA From rdmoores at gmail.com Thu Dec 17 05:39:59 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 16 Dec 2009 20:39:59 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <4B29B225.2080902@ieee.org> References: <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> <4B29B225.2080902@ieee.org> Message-ID: On Wed, Dec 16, 2009 at 20:23, Dave Angel wrote: > Richard D. Moores wrote: > There are conceivably better ways to get at the mantissa of the fp number, > but you can simply parse the hex digits as I did manually, and add one and > subtract one from the given mantissa ?(the part between the decimal point > and the 'p'). ?Then it just remains to figure out which two of those three > values actually span the desired value. > > Using the numbers and strings you supply, the three values would be: > > 0x1.bd70a3d70a3d6p-2 > 0x1.bd70a3d70a3d7p-2 > 0x1.bd70a3d70a3d8p-2 > > > and the second one is somewhat less than .435, while the 3rd is more. > > Now, while this is good enough to do by hand, you have to realize there are > some places it may not work, and another where it won't. Dave, I was hoping to find a way to NOT do it by hand, for the simple cases such as 0x1.bd70a3d70a3d7p-2 . I'm weak on hex arithmetic. For these simple cases, is there a general way to "add" something to the last digit of a hex value to bump it up and down by 1? After I can do that, I'll try to deal with the cases you mention below. Dick > I'm not sure whether trailing zeroes are always provided in the hex() > method. ?So you may have to pad it out before adjusting the last digit. ?I'm > also not positive how it normalizes the hex value printed out. ?As the > example in the help indicates, there are more than one hex string that can > be converted to the same float - if you denormalize, it'll still convert it. > ?I just don't know if hex() ever produces a non-normalized value. > > More drastically, if you're trying to be complete, is the infinities, the > NAN values, and the numbers, *very* close to zero, where gradual underflow > takes effect. ?The notion here is that when the exponent gets as negative as > it can safely store, the standard requires that the number be stored > denormalized, rather than going immediately to zero. ?I don't know how those > values are represented by the hex() method, but they have fewer significant > digits, so the adjustment you would need would be in a different column. > > > If I had to write a function to do what you ask, and if I couldn't get > better specs as to what Python is actually doing with the hex() method, I'd > work out an algorithm where tweak what seems to be the bottom digit, then > see if the float has a new value. ?If it does not, I'm working on the wrong > column. > > DaveA > > From davea at ieee.org Thu Dec 17 11:13:47 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 17 Dec 2009 05:13:47 -0500 Subject: [Tutor] Need a better name for this function In-Reply-To: References: <4B2854BA.2040908@ieee.org> <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> <4B29B225.2080902@ieee.org> Message-ID: <4B2A045B.7060100@ieee.org> Richard D. Moores wrote: > On Wed, Dec 16, 2009 at 20:23, Dave Angel wrote: > >> Richard D. Moores wrote: >> > > >> There are conceivably better ways to get at the mantissa of the fp number, >> but you can simply parse the hex digits as I did manually, and add one and >> subtract one from the given mantissa (the part between the decimal point >> and the 'p'). Then it just remains to figure out which two of those three >> values actually span the desired value. >> >> Using the numbers and strings you supply, the three values would be: >> >> 0x1.bd70a3d70a3d6p-2 >> 0x1.bd70a3d70a3d7p-2 >> 0x1.bd70a3d70a3d8p-2 >> >> >> and the second one is somewhat less than .435, while the 3rd is more. >> >> Now, while this is good enough to do by hand, you have to realize there are >> some places it may not work, and another where it won't. >> > > Dave, > > I was hoping to find a way to NOT do it by hand, for the simple cases > such as 0x1.bd70a3d70a3d7p-2 . I'm weak on hex arithmetic. For these > simple cases, is there a general way to "add" something to the last > digit of a hex value to bump it up and down by 1? After I can do that, > I'll try to deal with the cases you mention below. > > Dick > > >> I'm not sure whether trailing zeroes are always provided in the hex() >> method. So you may have to pad it out before adjusting the last digit. I'm >> also not positive how it normalizes the hex value printed out. As the >> example in the help indicates, there are more than one hex string that can >> be converted to the same float - if you denormalize, it'll still convert it. >> I just don't know if hex() ever produces a non-normalized value. >> >> More drastically, if you're trying to be complete, is the infinities, the >> NAN values, and the numbers, *very* close to zero, where gradual underflow >> takes effect. The notion here is that when the exponent gets as negative as >> it can safely store, the standard requires that the number be stored >> denormalized, rather than going immediately to zero. I don't know how those >> values are represented by the hex() method, but they have fewer significant >> digits, so the adjustment you would need would be in a different column. >> >> >> If I had to write a function to do what you ask, and if I couldn't get >> better specs as to what Python is actually doing with the hex() method, I'd >> work out an algorithm where tweak what seems to be the bottom digit, then >> see if the float has a new value. If it does not, I'm working on the wrong >> column. >> >> DaveA >> >> >> > > Try the following in Python 3.1: def increment(floatval, incr=1): #Given a float of "reasonable" size, increment it by smallest amount # and if incr is -1, then decrement stringval = floatval.hex() mantissa, exponent = stringval.split("p") mantissa = mantissa.replace(".", "") #ignore the period mantissa = hex(int(mantissa, 16) + incr) newstringval = mantissa[:3] + "." + mantissa[3:] + "p" + exponent newfloatval = float.fromhex(newstringval) #print(floatval, newstringval, newfloatval) return newfloatval You can specify an increment of +1 or -1, but larger values also work just as well. From limited testing, this works for any positive values that aren't in the gradual underflow range. The parsing and reassembly of the mantissa and exponent are pretty sloppy, but maybe they are even correct, for the output of the hex() method. DaveA From pedrooconnell at gmail.com Thu Dec 17 15:29:58 2009 From: pedrooconnell at gmail.com (pedro) Date: Thu, 17 Dec 2009 09:29:58 -0500 Subject: [Tutor] How to run two os.system commands simultaneously Message-ID: Hi, I have two computationally intensive commands that I would like to render simultaneously on two fifferent computers. When I run these two commands, os.system(theTerminalCommandMacPro4) os.system(theTerminalCommandMacPro1) the second command doesn't begin until the first command has finished rendering. Is there a way to send them both off at the same time? Pete From pedrooconnell at gmail.com Thu Dec 17 15:33:37 2009 From: pedrooconnell at gmail.com (pedro) Date: Thu, 17 Dec 2009 09:33:37 -0500 Subject: [Tutor] how to see a terminal window showing progress of os.system Message-ID: Hi I am sending commands to the command line using python's os.system. Is there a way to see a terminal window showing the progress of os.system as if you had just run the command from a normal terminal window? As it is now it runs completely in the background From kent37 at tds.net Thu Dec 17 15:52:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 17 Dec 2009 09:52:34 -0500 Subject: [Tutor] How to run two os.system commands simultaneously In-Reply-To: References: Message-ID: <1c2a2c590912170652u2eb498fdu2cd1f56618887f58@mail.gmail.com> On Thu, Dec 17, 2009 at 9:29 AM, pedro wrote: > Hi, I have two computationally intensive commands that I would like to > render simultaneously on two fifferent computers. When I run these two > commands, > > os.system(theTerminalCommandMacPro4) > os.system(theTerminalCommandMacPro1) > > > the second command doesn't begin until the first command has finished > rendering. Is there a way to send them both off at the same time? Use subprocess.Popen() to start the external process asynchronously: http://docs.python.org/library/subprocess.html#replacing-os-system Or call os.system() in a thread. Kent From pedrooconnell at gmail.com Thu Dec 17 16:14:44 2009 From: pedrooconnell at gmail.com (pedro) Date: Thu, 17 Dec 2009 10:14:44 -0500 Subject: [Tutor] How to run two os.system commands simultaneously References: <1c2a2c590912170652u2eb498fdu2cd1f56618887f58@mail.gmail.com> Message-ID: On 2009-12-17 09:52:34 -0500, Kent Johnson said: > call os.system() in a thread Hi Kent, pardon my ignorance but what do you mean by call os.system() in a thread? From rdmoores at gmail.com Thu Dec 17 16:40:11 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 17 Dec 2009 07:40:11 -0800 Subject: [Tutor] Need a better name for this function In-Reply-To: <4B2A045B.7060100@ieee.org> References: <29179d160912152330u3e809c0awe1439ff4d09a7a12@mail.gmail.com> <4B28BAF7.2090208@ieee.org> <4B29B225.2080902@ieee.org> <4B2A045B.7060100@ieee.org> Message-ID: On Thu, Dec 17, 2009 at 02:13, Dave Angel wrote: > Try the following in Python 3.1: > > def increment(floatval, incr=1): > ? #Given a float of "reasonable" size, increment it by smallest amount > ? # ?and if incr is -1, then decrement > ? stringval = floatval.hex() > ? mantissa, exponent = stringval.split("p") > ? mantissa = mantissa.replace(".", "") ? #ignore the period > ? mantissa = hex(int(mantissa, 16) + incr) > ? newstringval = mantissa[:3] + "." + mantissa[3:] + "p" + exponent > ? newfloatval = float.fromhex(newstringval) > ? #print(floatval, newstringval, newfloatval) > ? return newfloatval > > > You can specify an increment of +1 or -1, but larger values also work just > as well. ?From limited testing, this works for any positive values that > aren't in the gradual underflow range. > > The parsing and reassembly of the mantissa and exponent are pretty sloppy, > but maybe they are even correct, for the output of the hex() method. > > DaveA Thanks very much Dave. Enlightening. Dick From kent37 at tds.net Thu Dec 17 17:51:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 17 Dec 2009 11:51:34 -0500 Subject: [Tutor] How to run two os.system commands simultaneously In-Reply-To: References: <1c2a2c590912170652u2eb498fdu2cd1f56618887f58@mail.gmail.com> Message-ID: <1c2a2c590912170851o75e5a04cw3032095d8c5a922b@mail.gmail.com> On Thu, Dec 17, 2009 at 10:14 AM, pedro wrote: > On 2009-12-17 09:52:34 -0500, Kent Johnson said: > >> call os.system() in a thread > > Hi Kent, pardon my ignorance but what do you mean by call os.system() in a > thread? Your basic problem is that os.system() blocks - it waits until the new process completes before continuing execution of your code. You don't want to block - you want to be able to start the next process before the first one completes. One way to convert a blocking operation to a non-blocking one is to start a new thread and run the blocking operation in the second thread. Then the main program can continue, only the new thread is blocked. I think my first suggestion is simpler, though, especially if you don't know what threads are. Kent From rdmoores at gmail.com Fri Dec 18 01:57:57 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 17 Dec 2009 16:57:57 -0800 Subject: [Tutor] Please take a look at this function Message-ID: def prestrings2list(a_str): word = "" list_of_strings = [] length_of_a_string = len(a_str) for i, char in enumerate(a_str): if i == length_of_a_string - 1: word += char word = word.rstrip() list_of_strings.append(word) elif char == ",": word = word.strip() list_of_strings.append(word) word = "" elif char != " ": word += char elif char == " " and word != "" and a_str[i - 1] != " ": word += char return list_of_strings The idea for this came from looking at the interests of bloggers on a blogging site. Some have a great many interests listed -- so many that I thought it would handy to have a function that could put them in a Python list so they could be counted, sorted, dupes weeded out, etc. For example, here's a shorter one that I copied and pasted, modified, and then pasted again into a pair of quotes, thereby creating one long Python string: a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing, theoretical physic's, reason, love, hope, virtual reality, google operating system, space, life, mystery, truth's, universe, immortality, strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, n-space, hyper-time , theory of everything, light, nuclear theory, particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, Leibnitz,myrmecology" prestrings2list(a_str) returns ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google operating system', 'space', 'life', 'mystery', "truth's", 'universe', 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', 'theory of everything', 'light', 'nuclear theory', 'particle theory', 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', 'Leibnitz', 'myrmecology'] This is exactly what I wanted, but please tell me if the function name makes any sense, and if the function could be made to conform better to standard Python practice. And what a good docstring for it might be. I've assumed that the items in these strings will be separated by commas. But there could be some using semicolons instead. That revision will be easy to make. Thanks, Tutors, Dick Moores -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Dec 18 02:02:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 18 Dec 2009 01:02:03 -0000 Subject: [Tutor] how to see a terminal window showing progress of os.system References: Message-ID: "pedro" wrote > Hi I am sending commands to the command line using python's os.system. > > Is there a way to see a terminal window showing the progress of > os.system as if you had just run the command from a normal terminal > window? As it is now it runs completely in the background You can sometimes launch a terminal with your program running in it but thats usually not the best way to do it. Normally you would use the subprocess module and the Popen class to capture the output of the command and either monitor it for some event or display it within your own program. That looks more professional and gives you much more control You will find examples of using subprocess in the Using the OS topic of my tutorial. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Fri Dec 18 03:26:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 17 Dec 2009 21:26:53 -0500 Subject: [Tutor] Please take a look at this function In-Reply-To: References: Message-ID: <1c2a2c590912171826y89da407nee19248784ac037f@mail.gmail.com> On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores wrote: > def prestrings2list(a_str): > ??? word = "" > ??? list_of_strings = [] > ??? length_of_a_string = len(a_str) > ??? for i, char in enumerate(a_str): > ??????? if i == length_of_a_string - 1: > ??????????? word += char > ??????????? word = word.rstrip() > ??????????? list_of_strings.append(word) > ??????? elif char == ",": > ??????????? word = word.strip() > ??????????? list_of_strings.append(word) > ??????????? word = "" > ??????? elif char != " ": > ??????????? word += char > ??????? elif char == " " and word != "" and a_str[i - 1] != " ": > ??????????? word += char > ??? return list_of_strings I think you want def prestrings2list(a_str): return [i.strip() for i in a_str.split(',')] Kent From rdmoores at gmail.com Fri Dec 18 03:44:34 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 17 Dec 2009 18:44:34 -0800 Subject: [Tutor] Please take a look at this function In-Reply-To: <1c2a2c590912171826y89da407nee19248784ac037f@mail.gmail.com> References: <1c2a2c590912171826y89da407nee19248784ac037f@mail.gmail.com> Message-ID: On Thu, Dec 17, 2009 at 18:26, Kent Johnson wrote: > > On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores wrote: > > def prestrings2list(a_str): > > ??? word = "" > > ??? list_of_strings = [] > > ??? length_of_a_string = len(a_str) > > ??? for i, char in enumerate(a_str): > > ??????? if i == length_of_a_string - 1: > > ??????????? word += char > > ??????????? word = word.rstrip() > > ??????????? list_of_strings.append(word) > > ??????? elif char == ",": > > ??????????? word = word.strip() > > ??????????? list_of_strings.append(word) > > ??????????? word = "" > > ??????? elif char != " ": > > ??????????? word += char > > ??????? elif char == " " and word != "" and a_str[i - 1] != " ": > > ??????????? word += char > > ??? return list_of_strings > > I think you want > def prestrings2list(a_str): > ? ?return [i.strip() for i in a_str.split(',')] Wow, Kent! Perfect except for the extra interior spaces in items. E.g., 'synthetic DNA'. Could you fix that? Thanks, Dick From davea at ieee.org Fri Dec 18 04:40:38 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 17 Dec 2009 22:40:38 -0500 Subject: [Tutor] Please take a look at this function In-Reply-To: References: Message-ID: <4B2AF9B6.30103@ieee.org> Richard D. Moores wrote: > def prestrings2list(a_str): > word = "" > list_of_strings = [] > length_of_a_string = len(a_str) > for i, char in enumerate(a_str): > if i == length_of_a_string - 1: > word += char > word = word.rstrip() > list_of_strings.append(word) > elif char == ",": > word = word.strip() > list_of_strings.append(word) > word = "" > elif char != " ": > word += char > elif char == " " and word != "" and a_str[i - 1] != " ": > word += char > return list_of_strings > > > The idea for this came from looking at the interests of bloggers on a > blogging site. Some have a great many interests listed -- so many that I > thought it would handy to have a function that could put them in a Python > list so they could be counted, sorted, dupes weeded out, etc. > > For example, here's a shorter one that I copied and pasted, modified, and > then pasted again into a pair of quotes, thereby creating one long Python > string: > > a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing, > theoretical physic's, reason, love, hope, virtual reality, google operating > system, space, life, mystery, truth's, universe, immortality, strangeness, > fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, > n-space, hyper-time , theory of everything, light, nuclear theory, particle > theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's > Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, > Leibnitz,myrmecology" > > prestrings2list(a_str) returns > > ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', > "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google > operating system', 'space', 'life', 'mystery', "truth's", 'universe', > 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', > 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', > 'theory of everything', 'light', 'nuclear theory', 'particle theory', > 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's > Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', > 'Leibnitz', 'myrmecology'] > > This is exactly what I wanted, but please tell me if the function name makes > any sense, and if the function could be made to conform better to standard > Python practice. And what a good docstring for it might be. > > > I've assumed that the items in these strings will be separated by commas. > But there could be some using semicolons instead. That revision will be easy > to make. > > > Thanks, Tutors, > > > Dick Moores > > Seems to me the function would be much simpler using the string method .split() Something like: def myfunc(a_str): list_of_strings = a_str.split(",") return list_of_strings Now, if you want to also allow semicolons, you could pre-translate any semicolons to commas (using replace()). And if you need to strip leading and trailing whitespace, you could do a separate pass over the list. DaveA From hugo.yoshi at gmail.com Fri Dec 18 04:49:29 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 18 Dec 2009 04:49:29 +0100 Subject: [Tutor] Please take a look at this function In-Reply-To: References: <1c2a2c590912171826y89da407nee19248784ac037f@mail.gmail.com> Message-ID: <29179d160912171949x5dd39f0cv98d06ce3fc6a1624@mail.gmail.com> On Fri, Dec 18, 2009 at 3:44 AM, Richard D. Moores wrote: > On Thu, Dec 17, 2009 at 18:26, Kent Johnson wrote: >> >> On Thu, Dec 17, 2009 at 7:57 PM, Richard D. Moores wrote: >> > def prestrings2list(a_str): >> > ??? word = "" >> > ??? list_of_strings = [] >> > ??? length_of_a_string = len(a_str) >> > ??? for i, char in enumerate(a_str): >> > ??????? if i == length_of_a_string - 1: >> > ??????????? word += char >> > ??????????? word = word.rstrip() >> > ??????????? list_of_strings.append(word) >> > ??????? elif char == ",": >> > ??????????? word = word.strip() >> > ??????????? list_of_strings.append(word) >> > ??????????? word = "" >> > ??????? elif char != " ": >> > ??????????? word += char >> > ??????? elif char == " " and word != "" and a_str[i - 1] != " ": >> > ??????????? word += char >> > ??? return list_of_strings >> >> I think you want >> def prestrings2list(a_str): >> ? ?return [i.strip() for i in a_str.split(',')] > > Wow, Kent! ?Perfect except for the extra interior spaces in items. > E.g., 'synthetic ? DNA'. Could you fix that? Probably easiest to use a regular expression to fix that particular thing, as in: import re mult_space = re.compile(r'\s+') def prestrings2list(a_str): return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] Hugo From rdmoores at gmail.com Fri Dec 18 05:34:54 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 17 Dec 2009 20:34:54 -0800 Subject: [Tutor] Please take a look at this function In-Reply-To: <29179d160912171949x5dd39f0cv98d06ce3fc6a1624@mail.gmail.com> References: <1c2a2c590912171826y89da407nee19248784ac037f@mail.gmail.com> <29179d160912171949x5dd39f0cv98d06ce3fc6a1624@mail.gmail.com> Message-ID: On Thu, Dec 17, 2009 at 19:49, Hugo Arts wrote: > Probably easiest to use a regular expression to fix that particular > thing, as in: > > import re > mult_space = re.compile(r'\s+') > def prestrings2list(a_str): > return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] > > Hugo That's perfect, Hugo! ======================================== a_str = "blender , synthetic DNA, myrmecology, fungi, quorum sensing, theoretical physic's, reason, love, hope, virtual reality, google operating system, space, life, mystery, truth's, universe, immortality, strangeness, fun ,living, hope, eternity, knowledge, Egyptian secrets of the dead, n-space, hyper-time , theory of everything, light, nuclear theory, particle theory, myrmec, self replicating RNA, MMOG, MMOR%PG, symbiosis,Black's Plague, selddir, Da Vinci, Newton, Archimedes, Cantor7, Leibnitz, myrmecology" def prestrings2list(a_str): import re mult_space = re.compile(r'\s+') return [re.sub(mult_space, ' ', x).strip() for x in a_str.split(',')] lst = prestrings2list(a_str) print(lst) ================================================= OUTPUT ['blender', 'synthetic DNA', 'myrmecology', 'fungi', 'quorum sensing', "theoretical physic's", 'reason', 'love', 'hope', 'virtual reality', 'google operating system', 'space', 'life', 'mystery', "truth's", 'universe', 'immortality', 'strangeness', 'fun', 'living', 'hope', 'eternity', 'knowledge', 'Egyptian secrets of the dead', 'n-space', 'hyper-time', 'theory of everything', 'light', 'nuclear theory', 'particle theory', 'myrmec', 'self replicating RNA', 'MMOG', 'MMOR%PG', 'symbiosis', "Black's Plague", 'selddir', 'Da Vinci', 'Newton', 'Archimedes', 'Cantor7', 'Leibnitz', 'myrmecology'] Now for a better function name, and a proper docstring. Anyone? Dick -------------- next part -------------- An HTML attachment was scrubbed... URL: From bsc26 at yahoo.com Fri Dec 18 06:32:44 2009 From: bsc26 at yahoo.com (Benjamin Castillo) Date: Thu, 17 Dec 2009 21:32:44 -0800 (PST) Subject: [Tutor] What is URL to online Python interpreter? Message-ID: <602946.98850.qm@web51407.mail.re2.yahoo.com> What is URL to online Python interpreter? Ben From gdoghomes at gmail.com Fri Dec 18 06:54:01 2009 From: gdoghomes at gmail.com (Michael Morrissey) Date: Fri, 18 Dec 2009 12:54:01 +0700 Subject: [Tutor] Generating Unique Permutations Message-ID: I'm just a philosophy teacher, and I don't know much about mathematics or computers. I'm writing a python program (not the best language for this topic, but it is what I know), and I need to solve a problem which requires more knowledge than I have. I'm hoping you can help me. =) I'm looking for an efficient way to create all the unique, non-duplicated permutations of a list (I believe these are called "necklaces" in combinatorics). I need to do this without actually generating every possible permutation (which includes all the duplicates). For example: List = [a,a,b,b] My output would be something like: a,a,b,b a,b,a,b a,b,b,a b,a,a,b b,a,b,a b,b,a,a Importantly, you'll see that these are only generated once. There are four permutations which can be generated from the list which all look like (a,a,b,b), but I only want to generate this output a single time. My problem is large enough that I can't feasibly generate all the permutations (60! I think) and eliminate the duplicates from there, but I could feasibly generate the unique ones (a much small search space), especially if I use an efficient algorithm (I'm sorry to focus so much on efficiency, but it matters). What is the best way to do this? If you don't know how it would work in Python, can you explain in psuedocode? As I said, I'm not very smart about these things, so treat like a child to the topic (because I am a child to the topic, an interested child!). Oh, and thanks for this mailing/reading list! I spend countless hours browsing and reading other people's code. It is a lot of fun =). Sincerely, Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Fri Dec 18 07:10:36 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Fri, 18 Dec 2009 11:40:36 +0530 Subject: [Tutor] Generating Unique Permutations In-Reply-To: References: Message-ID: create your desired list >>> l = ['a', 'a', 'b', 'b'] do a permutation and take unique values >> import itertools >> set(itertools.permutations(l)) HTH On Fri, Dec 18, 2009 at 11:24 AM, Michael Morrissey wrote: > I'm just a philosophy teacher, and I don't know much about mathematics or > computers. I'm writing a python program (not the best language for this > topic, but it is what I know), and I need to solve a problem which requires > more knowledge than I have. I'm hoping you can help me. =) > > I'm looking for an efficient way to create all the unique, non-duplicated > permutations of a list (I believe these are called "necklaces" in > combinatorics). I need to do this without actually generating every possible > permutation (which includes all the duplicates). > > For example: > > List = [a,a,b,b] > > My output would be something like: > > a,a,b,b > a,b,a,b > a,b,b,a > b,a,a,b > b,a,b,a > b,b,a,a > > Importantly, you'll see that these are only generated once. There are four > permutations which can be generated from the list which all look like > (a,a,b,b), but I only want to generate this output a single time. > > My problem is large enough that I can't feasibly generate all the > permutations (60! I think) and eliminate the duplicates from there, but I > could feasibly generate the unique ones (a much small search space), > especially if I use an efficient algorithm (I'm sorry to focus so much on > efficiency, but it matters). > > What is the best way to do this? If you don't know how it would work in > Python, can you explain in psuedocode? As I said, I'm not very smart about > these things, so treat like a child to the topic (because I am a child to > the topic, an interested child!). > > Oh, and thanks for this mailing/reading list! I spend countless hours > browsing and reading other people's code. It is a lot of fun =). > > > > > Sincerely, > Michael > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From orsenthil at gmail.com Fri Dec 18 07:24:44 2009 From: orsenthil at gmail.com (Senthil Kumaran) Date: Fri, 18 Dec 2009 11:54:44 +0530 Subject: [Tutor] What is URL to online Python interpreter? In-Reply-To: <602946.98850.qm@web51407.mail.re2.yahoo.com> References: <602946.98850.qm@web51407.mail.re2.yahoo.com> Message-ID: <20091218062444.GA3953@ubuntu.ubuntu-domain> On Thu, Dec 17, 2009 at 09:32:44PM -0800, Benjamin Castillo wrote: > What is URL to online Python interpreter? Google could have helped you too. Anyways, http://shell.appspot.com/ -- Senthil From denis.spir at free.fr Fri Dec 18 12:09:46 2009 From: denis.spir at free.fr (spir) Date: Fri, 18 Dec 2009 12:09:46 +0100 Subject: [Tutor] Generating Unique Permutations In-Reply-To: References: Message-ID: <20091218120946.47ae6709@o> Michael Morrissey dixit: > I'm just a philosophy teacher, and I don't know much about mathematics or > computers. I'm writing a python program (not the best language for this > topic, but it is what I know), and I need to solve a problem which requires > more knowledge than I have. I'm hoping you can help me. =) > > I'm looking for an efficient way to create all the unique, non-duplicated > permutations of a list (I believe these are called "necklaces" in > combinatorics). I need to do this without actually generating every possible > permutation (which includes all the duplicates). > > For example: > > List = [a,a,b,b] > > My output would be something like: > > a,a,b,b > a,b,a,b > a,b,b,a > b,a,a,b > b,a,b,a > b,b,a,a > > Importantly, you'll see that these are only generated once. There are four > permutations which can be generated from the list which all look like > (a,a,b,b), but I only want to generate this output a single time. > > My problem is large enough that I can't feasibly generate all the > permutations (60! I think) and eliminate the duplicates from there, but I > could feasibly generate the unique ones (a much small search space), > especially if I use an efficient algorithm (I'm sorry to focus so much on > efficiency, but it matters). > > What is the best way to do this? If you don't know how it would work in > Python, can you explain in psuedocode? As I said, I'm not very smart about > these things, so treat like a child to the topic (because I am a child to > the topic, an interested child!). > > Oh, and thanks for this mailing/reading list! I spend countless hours > browsing and reading other people's code. It is a lot of fun =). > > > > > Sincerely, > Michael The critical point is indeed to generate every unique permutation without generating all. I don't know of any standard algorithm for this, but I think a key point may be: instead of starting with a list of characters considered individually, start with a list of (char, count) _pairs_. For instance: List = [a,a,a,b,b,c] ==> List = [(a,3), (b,2), (c,1)] From there, you can certainly generate permutations without duplicates. However, I don't exactly know how to ;-) Don't remember of this problem at school, but the overall number of permutations should be IIRC: n! / (n1! x n2! x n3! ...) So, in the case above: 6! / (3! x 2! x 1!) = 60 ? The process will be to sequentially place characters of each (char, count) pair at all possible places. Eg the a's will be at positions 123, 124, 125, 126, 134... For each of these positionings of a's, walk through all permutations for other letters. Then start with placing b's, then c's -- but only combine for positions that where not yet considered when starting with a's, then with a's & b's; this is the difficult part. Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From amonroe at columbus.rr.com Fri Dec 18 13:31:10 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Fri, 18 Dec 2009 07:31:10 -0500 Subject: [Tutor] Generating Unique Permutations In-Reply-To: References: Message-ID: <147174344864.20091218073110@columbus.rr.com> > I'm looking for an efficient way to create all the unique, non-duplicated > permutations of a list This may or may not help: http://code.activestate.com/recipes/190465/ Alan From anand.shashwat at gmail.com Fri Dec 18 14:09:32 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Fri, 18 Dec 2009 18:39:32 +0530 Subject: [Tutor] Generating Unique Permutations In-Reply-To: <147174344864.20091218073110@columbus.rr.com> References: <147174344864.20091218073110@columbus.rr.com> Message-ID: may be this can be helpful: http://blog.bjrn.se/2008/04/lexicographic-permutations-using.html On Fri, Dec 18, 2009 at 6:01 PM, R. Alan Monroe wrote: > > > I'm looking for an efficient way to create all the unique, non-duplicated > > permutations of a list > > This may or may not help: > http://code.activestate.com/recipes/190465/ > > Alan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Fri Dec 18 14:39:14 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 18 Dec 2009 08:39:14 -0500 Subject: [Tutor] Generating Unique Permutations In-Reply-To: References: Message-ID: <1c2a2c590912180539m6ecd990fq3a3cd9638ee031d5@mail.gmail.com> On Fri, Dec 18, 2009 at 12:54 AM, Michael Morrissey wrote: > I'm looking for an efficient way to create all the unique, non-duplicated > permutations of a list (I believe these are called "necklaces" in > combinatorics). I need to do this without actually generating every possible > permutation (which includes all the duplicates). This seems to do what you want: http://code.activestate.com/recipes/496819/ Kent From grigor.kolev at gmail.com Fri Dec 18 15:34:30 2009 From: grigor.kolev at gmail.com (=?windows-1251?B?w/Do4+7w?=) Date: Fri, 18 Dec 2009 16:34:30 +0200 Subject: [Tutor] Send Hex Message-ID: Hi Can I send date and time like Hex in to the Serial port or file. ------------------------------------ from time import * def TimeMake(): '''Formating time and data (dd-mm-yy hh:mm) and change it like ASCII''' Time=list(localtime()) #get Time and Date Time[0]=str(Time[0]) #Make (yy) str Time = '%02d-%02d-%s %02d:%02d' % (Time[2], Time[1], Time[0][2:], Time[4], Time[5]) #Formating (dd-mm-yy hh:mm) TimeHex = [] for i in Time: #Take ASCII TimeHex.append(ord(i)) Time =[] for i in TimeHex: #Make ASCII in hex format Time.append(hex(i)) return Time Time=TimeMake() #When I write it in the some file is a string but must to be like hex ------------------------------------ -- ????? ???? ???? ???, ???? ???? ??????? ?? ???? ?? ???? ! -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.johansson at math.umu.se Fri Dec 18 16:00:21 2009 From: robert.johansson at math.umu.se (Robert Johansson) Date: Fri, 18 Dec 2009 16:00:21 +0100 Subject: [Tutor] Generating Unique Permutations In-Reply-To: References: Message-ID: <002c01ca7ff2$d20c38f0$7624aad0$@johansson@math.umu.se> I think you mean a multiset not a necklace. A necklace is invariant under rotations and reflections aabb=abba=bbaa=baab and abab=baba. Seems that some people name them bags instead of multiset. Maybe this can help you finding more examples? I see that you got a few already. /Robert Fr?n: tutor-bounces+robert.johansson=math.umu.se at python.org [mailto:tutor-bounces+robert.johansson=math.umu.se at python.org] F?r Michael Morrissey Skickat: den 18 december 2009 06:54 Till: tutor at python.org ?mne: [Tutor] Generating Unique Permutations I'm just a philosophy teacher, and I don't know much about mathematics or computers. I'm writing a python program (not the best language for this topic, but it is what I know), and I need to solve a problem which requires more knowledge than I have. I'm hoping you can help me. =) I'm looking for an efficient way to create all the unique, non-duplicated permutations of a list (I believe these are called "necklaces" in combinatorics). I need to do this without actually generating every possible permutation (which includes all the duplicates). For example: List = [a,a,b,b] My output would be something like: a,a,b,b a,b,a,b a,b,b,a b,a,a,b b,a,b,a b,b,a,a Importantly, you'll see that these are only generated once. There are four permutations which can be generated from the list which all look like (a,a,b,b), but I only want to generate this output a single time. My problem is large enough that I can't feasibly generate all the permutations (60! I think) and eliminate the duplicates from there, but I could feasibly generate the unique ones (a much small search space), especially if I use an efficient algorithm (I'm sorry to focus so much on efficiency, but it matters). What is the best way to do this? If you don't know how it would work in Python, can you explain in psuedocode? As I said, I'm not very smart about these things, so treat like a child to the topic (because I am a child to the topic, an interested child!). Oh, and thanks for this mailing/reading list! I spend countless hours browsing and reading other people's code. It is a lot of fun =). Sincerely, Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: From sirgnip at gmail.com Fri Dec 18 16:09:15 2009 From: sirgnip at gmail.com (Scott Nelson) Date: Fri, 18 Dec 2009 09:09:15 -0600 Subject: [Tutor] What is URL to online Python interpreter? In-Reply-To: <20091218062444.GA3953@ubuntu.ubuntu-domain> References: <602946.98850.qm@web51407.mail.re2.yahoo.com> <20091218062444.GA3953@ubuntu.ubuntu-domain> Message-ID: <2682ac9b0912180709r562afd68i2d534d3bdcee2f78@mail.gmail.com> > > On Thu, Dec 17, 2009 at 09:32:44PM -0800, Benjamin Castillo wrote: > > What is URL to online Python interpreter? > There is also http://codepad.org/ which also supports lots of languages (Python, Ruby, Perl, PHP, C/C++...). Pretty slick. You can also use it as a public pastebin (this link will expire eventually) http://codepad.org/YCrMADrc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Fri Dec 18 16:19:50 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 18 Dec 2009 15:19:50 +0000 Subject: [Tutor] What is URL to online Python interpreter? In-Reply-To: <2682ac9b0912180709r562afd68i2d534d3bdcee2f78@mail.gmail.com> References: <602946.98850.qm@web51407.mail.re2.yahoo.com> <20091218062444.GA3953@ubuntu.ubuntu-domain> <2682ac9b0912180709r562afd68i2d534d3bdcee2f78@mail.gmail.com> Message-ID: <4B2B9D96.70501@timgolden.me.uk> Scott Nelson wrote: >> On Thu, Dec 17, 2009 at 09:32:44PM -0800, Benjamin Castillo wrote: >>> What is URL to online Python interpreter? > > There is also http://codepad.org/ which also supports lots of languages > (Python, Ruby, Perl, PHP, C/C++...). Pretty slick. You can also use it as > a public pastebin (this link will expire eventually) > http://codepad.org/YCrMADrc. And, assuming you're up for Silverlight / Moonlight, there's always: http://trypython.org which is especially geared up for running through the tutorial. TJG From johnfilben at yahoo.com Fri Dec 18 14:57:45 2009 From: johnfilben at yahoo.com (John Filben) Date: Fri, 18 Dec 2009 05:57:45 -0800 (PST) Subject: [Tutor] Field/Variable References Message-ID: <999910.85753.qm@web33807.mail.mud.yahoo.com> Can someone please let me know how to read a file one record at a time (just say fixed block for now - see?small example below) and assign columns to fields.? Then reference the field names with if-then-else logic. Sample Fixed Block File: John98762 John82634 John11234 Thank you. ?John Filben Cell Phone - 773.401.2822 Email - johnfilben at yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Fri Dec 18 16:41:24 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Fri, 18 Dec 2009 10:41:24 -0500 Subject: [Tutor] Field/Variable References In-Reply-To: <999910.85753.qm@web33807.mail.mud.yahoo.com> References: <999910.85753.qm@web33807.mail.mud.yahoo.com> Message-ID: > Can someone please let me know how to read a file one record at a time (just > say fixed block for now - see?small example below) and assign columns to > fields.? Then reference the field names with if-then-else logic. > > Sample Fixed Block File: > > John98762 > John82634 > John11234 > Hi John, Does your file only have one column? In any event, the easiest method is to open the file as an iterable object and process each line. If you fire up the python interpreter, you can do something like: >>> for line in open('/path/to/data_file.txt'): ... data = line ... # do stuff with data The above assumes you only have that one column you showed, which I imagine is not the case. If you can show us more details about your data, we could offer other suggestions on how to parse it. Meantime, you might want to check the below resources on working with files: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files http://www.freenetpages.co.uk/hp/alan.gauld/ http://diveintopython.org/file_handling/file_objects.html HTH, Serdar ps - Apologies to Alan if I provided the outdate URL to his tutorial (I *thought* I updated, but can't recall for certain) From zstumgoren at gmail.com Fri Dec 18 17:52:43 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Fri, 18 Dec 2009 11:52:43 -0500 Subject: [Tutor] Field/Variable References In-Reply-To: <460158.62265.qm@web33808.mail.mud.yahoo.com> References: <999910.85753.qm@web33807.mail.mud.yahoo.com> <460158.62265.qm@web33808.mail.mud.yahoo.com> Message-ID: I'm reposting John's follow-up question (below) to the list. > Thank you - can you please assume that the data provided is the following: > > Columns 1 - 4 = Name (in record 1 of example =?"John") > Column 5 = Answer1 (in record 1 of example =?"9") > Column?6 = Answer2 (in record 1 of example =?"8") > Column?7 = AreaCode (in record 1 of example =?"762") > > Sorry - I am new to Python and trying to see if I can do data manipulation > in the same detail that I am used to in the SAS Data Step. > In the above case, the simplest solution might be to use Python's slicing syntax. The below solution assumes that the name can vary in number of characters, but that the digits for your Answers and area code do not vary (in other words, Answer1 and Answer2 are always a single digit, and AreaCode is always three digits). for line in open('my_data_file.txt'): name = line[0:-5] #start of line up to 5 places before end of string; zero-indexed with non-inclusive endpoint Answer1 = line[-5] #grab fifth digit from end of line Answer2 = line[-4] #grab fourth digit from end of line AreaCode = line[-3:] #grab from 3 places before end of line, through the end # do stuff with name, Answer1, Answer2, AreaCode The slicing syntax can be tricky because the index varies depending on which direction you're counting from. If you're counting start to end, it's zero-indexed. In other words, the first "column" or character in the string is numbered 0 (so line[0] will give you the letter "J"). On the other hand, if you're counting from end-of-string to beginning, the characters are indexed starting with -1. So the "first" character from the right (represented as -1) will be 2. Others on the list can probably explain it better than me. Meantime, you might want to check out this intro page, giving special focus to the sections on strings and lists. http://docs.python.org/tutorial/introduction.html HTH! Serdar From denis.spir at free.fr Fri Dec 18 19:51:26 2009 From: denis.spir at free.fr (spir) Date: Fri, 18 Dec 2009 19:51:26 +0100 Subject: [Tutor] Field/Variable References In-Reply-To: References: <999910.85753.qm@web33807.mail.mud.yahoo.com> <460158.62265.qm@web33808.mail.mud.yahoo.com> Message-ID: <20091218195126.21bbf977@o> Serdar Tumgoren dixit: > > Thank you - can you please assume that the data provided is the following: > > > > Columns 1 - 4 = Name (in record 1 of example =?"John") > > Column 5 = Answer1 (in record 1 of example =?"9") > > Column?6 = Answer2 (in record 1 of example =?"8") > > Column?7 = AreaCode (in record 1 of example =?"762") > > > > Sorry - I am new to Python and trying to see if I can do data manipulation > > in the same detail that I am used to in the SAS Data Step. > > > > In the above case, the simplest solution might be to use Python's > slicing syntax. The below solution assumes that the name can vary in > number of characters, but that the digits for your Answers and area > code do not vary (in other words, Answer1 and Answer2 are always a > single digit, and AreaCode is always three digits). > > for line in open('my_data_file.txt'): > name = line[0:-5] #start of line up to 5 places before end of > string; zero-indexed with non-inclusive endpoint > Answer1 = line[-5] #grab fifth digit from end of line > Answer2 = line[-4] #grab fourth digit from end of line > AreaCode = line[-3:] #grab from 3 places before end of line, through the end > # do stuff with name, Answer1, Answer2, AreaCode Why negative indexes when fields lengths are known? (Reminds me of a fr. proverb: "Why do it simple when we can do it complicated?") Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From kent37 at tds.net Fri Dec 18 20:19:19 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 18 Dec 2009 14:19:19 -0500 Subject: [Tutor] Field/Variable References In-Reply-To: <999910.85753.qm@web33807.mail.mud.yahoo.com> References: <999910.85753.qm@web33807.mail.mud.yahoo.com> Message-ID: <1c2a2c590912181119g295f3794xea625aa27759c39f@mail.gmail.com> On Fri, Dec 18, 2009 at 8:57 AM, John Filben wrote: > Can someone please let me know how to read a file one record at a time (just > say fixed block for now - see?small example below) and assign columns to > fields.? Then reference the field names with if-then-else logic. > > Sample Fixed Block File: > > John98762 > John82634 > John11234 Here is a nice example: http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch34s05.html Kent From zstumgoren at gmail.com Fri Dec 18 21:27:12 2009 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Fri, 18 Dec 2009 15:27:12 -0500 Subject: [Tutor] Field/Variable References In-Reply-To: <20091218195126.21bbf977@o> References: <999910.85753.qm@web33807.mail.mud.yahoo.com> <460158.62265.qm@web33808.mail.mud.yahoo.com> <20091218195126.21bbf977@o> Message-ID: > Why negative indexes when fields lengths are known? I'll admit I made a few assumptions based on what I could glean from the OP: * names will vary in length in a questionnaire or poll (which this appears to be) * answers will always be represented by a single digit * area code (at least in US) will always be 3 digits Of course, those assumptions could be wrong, but I tried to allow for them just in case. Depending on the use, it very well might be overkill... Serdar From davea at ieee.org Sat Dec 19 03:35:00 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 18 Dec 2009 21:35:00 -0500 Subject: [Tutor] Field/Variable References In-Reply-To: <999910.85753.qm@web33807.mail.mud.yahoo.com> References: <999910.85753.qm@web33807.mail.mud.yahoo.com> Message-ID: <4B2C3BD4.1060004@ieee.org> John Filben wrote: > Can someone please let me know how to read a file one record at a time (just say fixed block for now - see small example below) and assign columns to fields. Then reference the field names with if-then-else logic. > > Sample Fixed Block File: > > John98762 > John82634 > John11234 > > Thank you. > John Filben > Cell Phone - 773.401.2822 > Email - johnfilben at yahoo.com > > > The other responses all seem to assume that you have linefeeds between records. But you said it was fixed-block, which implies no linefeeds. Anyway, to fetch those blocks, use the file.read() method, where you can give the fixed size. You can separate the blocks with slicing, or you can do something trickier. DaveA From metolone+gmane at gmail.com Sat Dec 19 05:10:36 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Fri, 18 Dec 2009 20:10:36 -0800 Subject: [Tutor] Send Hex References: Message-ID: "??????" wrote in message news:acd355e80912180634l1c7a545fo7be9d5e99138bd08 at mail.gmail.com... > Hi > Can I send date and time like Hex in to the Serial port or file. > > ------------------------------------ > from time import * It preferable to just use "import time" import time > def TimeMake(): > '''Formating time and data (dd-mm-yy hh:mm) and change it like > ASCII''' > Time=list(localtime()) #get Time and Date > Time[0]=str(Time[0]) #Make (yy) str > Time = '%02d-%02d-%s %02d:%02d' % (Time[2], Time[1], Time[0][2:], > Time[4], Time[5]) #Formating (dd-mm-yy hh:mm) The previous lines have a bug ("dd-mm-yy mm:ss" is the result). A less error-prone version is: Time = time.strftime('%d-%m-%y %H:%M',localtime()) > TimeHex = [] > for i in Time: #Take ASCII > TimeHex.append(ord(i)) > Time =[] > for i in TimeHex: #Make ASCII in hex format > Time.append(hex(i)) > return Time > Time=TimeMake() > #When I write it in the some file is a string but must to be like hex > ------------------------------------ The resulting string from strftime *can* be sent directly to a serial port or file. Each character in the string is a byte of data. If you actually need to send hexadecimal characters as text, the binascii module has hexlify: >>> import binascii >>> import time >>> binascii.hexlify(time.strftime('%d-%m-%y %H:%M',localtime())) '31382d31322d30392032303a3039' -Mark From rdmoores at gmail.com Sat Dec 19 07:29:40 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 18 Dec 2009 22:29:40 -0800 Subject: [Tutor] No beep from winsound.Beep(500, 500) Message-ID: My laptop, a Toshiba Satellite running Python 3.1 and Vista 64-bit SP2, with the speakers on and otherwise working, won't give me a beep: >>> from winsound import Beep >>> Beep(500, 500) Traceback (most recent call last): File "", line 1, in Beep(500, 500) RuntimeError: Failed to beep Please give me some hints as to what I can do about this. Thanks, Dick Moores From alan.gauld at btinternet.com Sat Dec 19 10:18:32 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 19 Dec 2009 09:18:32 -0000 Subject: [Tutor] No beep from winsound.Beep(500, 500) References: Message-ID: "Richard D. Moores" wrote >>>> from winsound import Beep >>>> Beep(500, 500) Works ok for me. I suspect it may be a system setting Try echo ^G in a DOS box (ie Control-G) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Sat Dec 19 10:35:48 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 19 Dec 2009 01:35:48 -0800 Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: References: Message-ID: On Sat, Dec 19, 2009 at 01:18, Alan Gauld wrote: > > "Richard D. Moores" wrote >>>>> >>>>> from winsound import Beep >>>>> Beep(500, 500) > > Works ok for me. > > I suspect it may be a system setting > > Try > echo ^G > in a DOS box (ie Control-G) You mean the command line? C:\Users\Dick>echo ^G C:\Users\Dick> Nothing. Dick From cfuller084 at thinkingplanet.net Sat Dec 19 11:02:15 2009 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Sat, 19 Dec 2009 04:02:15 -0600 Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: References: Message-ID: <200912190402.15622.cfuller084@thinkingplanet.net> Something to keep in mind is that the audio output of your computer and the system speaker are independent. Sometimes the BELL character (ACSII 0x07) will sound the system speaker, spending on your OS, drivers, etc. The winsound docs say it's the speaker, which is connected to the motherboard with an odd four pin connector (with only two wires). It's normally only used during boot up, so it could have been left disconnected and not missed. Cheers On Saturday 19 December 2009, Richard D. Moores wrote: > My laptop, a Toshiba Satellite running Python 3.1 and Vista 64-bit > > SP2, with the speakers on and otherwise working, won't give me a beep: > >>> from winsound import Beep > >>> Beep(500, 500) > > Traceback (most recent call last): > File "", line 1, in > Beep(500, 500) > RuntimeError: Failed to beep > > Please give me some hints as to what I can do about this. > > Thanks, > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From marc.tompkins at gmail.com Sat Dec 19 11:36:36 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sat, 19 Dec 2009 02:36:36 -0800 Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: <200912190402.15622.cfuller084@thinkingplanet.net> References: <200912190402.15622.cfuller084@thinkingplanet.net> Message-ID: <40af687b0912190236n3c06155ehd632d1d7a35b1a15@mail.gmail.com> Richard - It works (in Python 2.6, mind you) on my 64-bit Win7 machine, but I suspect that that may be because of Microsoft's compatibility twiddling on the back end. I Googled, and there seem to be a number of reports of people having trouble with 64-bit Vista and winsound.Beep(). I suspect that allowing programs access to the system speaker is one of those things that MS decided to cut out in Vista, but then decided to bring back in 7. Chris - if it were just a disconnected speaker, I don't think he'd get the runtime error... On Sat, Dec 19, 2009 at 2:02 AM, Chris Fuller wrote: > > Something to keep in mind is that the audio output of your computer and the > system speaker are independent. Sometimes the BELL character (ACSII 0x07) > will sound the system speaker, spending on your OS, drivers, etc. > > The winsound docs say it's the speaker, which is connected to the > motherboard > with an odd four pin connector (with only two wires). It's normally only > used > during boot up, so it could have been left disconnected and not missed. > > Cheers > > On Saturday 19 December 2009, Richard D. Moores wrote: > > My laptop, a Toshiba Satellite running Python 3.1 and Vista 64-bit > > > > SP2, with the speakers on and otherwise working, won't give me a beep: > > >>> from winsound import Beep > > >>> Beep(500, 500) > > > > Traceback (most recent call last): > > File "", line 1, in > > Beep(500, 500) > > RuntimeError: Failed to beep > > > > Please give me some hints as to what I can do about this. > > > > Thanks, > > > > Dick Moores > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Sat Dec 19 11:55:00 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 19 Dec 2009 02:55:00 -0800 Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: <40af687b0912190236n3c06155ehd632d1d7a35b1a15@mail.gmail.com> References: <200912190402.15622.cfuller084@thinkingplanet.net> <40af687b0912190236n3c06155ehd632d1d7a35b1a15@mail.gmail.com> Message-ID: On Sat, Dec 19, 2009 at 02:36, Marc Tompkins wrote: > Richard -? It works (in Python 2.6, mind you) on my 64-bit Win7 machine, but > I suspect that that may be because of Microsoft's compatibility twiddling on > the back end.? I Googled, and there seem to be a number of reports of people > having trouble with 64-bit Vista and winsound.Beep().? I suspect that > allowing programs access to the system speaker is one of those things that > MS decided to cut out in Vista, but then decided to bring back in 7. Marc, that's actually encouraging, because I'll be getting the Win 7 upgrade for the laptop in January. I'll just wait for that. Thanks, Dick From alan.gauld at btinternet.com Sat Dec 19 12:48:12 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 19 Dec 2009 11:48:12 +0000 (GMT) Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: References: Message-ID: <716397.48224.qm@web86707.mail.ird.yahoo.com> >> Try >> echo ^G >> in a DOS box (ie Control-G) > >You mean the command line? > > C:\Users\Dick>echo ^G > > Nothing. You should get a beep - that's what Ctrl-G is, the Beep character. That means your windows sound setup is faulty somehow, it might be turned off in the BIOS... Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ________________________________ From: Richard D. Moores To: Alan Gauld Cc: tutor at python.org Sent: Saturday, 19 December, 2009 9:35:48 Subject: Re: [Tutor] No beep from winsound.Beep(500, 500) On Sat, Dec 19, 2009 at 01:18, Alan Gauld wrote: > > "Richard D. Moores" wrote >>>>> >>>>> from winsound import Beep >>>>> Beep(500, 500) > > Works ok for me. > > I suspect it may be a system setting > Dick -------------- next part -------------- An HTML attachment was scrubbed... URL: From hultgren1946 at yahoo.com Sat Dec 19 13:34:01 2009 From: hultgren1946 at yahoo.com (Richard Hultgren) Date: Sat, 19 Dec 2009 04:34:01 -0800 (PST) Subject: [Tutor] (no subject) Message-ID: <266839.36145.qm@web113212.mail.gq1.yahoo.com> Hello,???? ??? I am a newcomer, I guess I'm have trouble thinking like a computer yet. My question is: in this program: >>> resp = raw_input("What's your name? ") >>> print "Hi, %s, nice to meet you" % resp ??? ??? does %s mean 'place the user response here' and secondly what and why do I put % resp at the end of the print statement? I know this is very basic and future questiions will be more interesting I hope. ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Richard H. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Sat Dec 19 13:55:38 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 19 Dec 2009 04:55:38 -0800 Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: <716397.48224.qm@web86707.mail.ird.yahoo.com> References: <716397.48224.qm@web86707.mail.ird.yahoo.com> Message-ID: On Sat, Dec 19, 2009 at 03:48, ALAN GAULD wrote: >>> Try >>> echo ^G >>> in a DOS box (ie Control-G) >> >>You mean the command line? >> >> C:\Users\Dick>echo ^G >> >> Nothing. > > You should get a beep - that's what Ctrl-G is, > the Beep character. > > That means your windows sound setup is > faulty somehow, it might be turned off > in the BIOS... Alan, did you see Marc Tompkins post suggesting that my 64-bit Vista OS might be the cause? "Richard - It works (in Python 2.6, mind you) on my 64-bit Win7 machine, but I suspect that that may be because of Microsoft's compatibility twiddling on the back end. I Googled, and there seem to be a number of reports of people having trouble with 64-bit Vista and winsound.Beep(). I suspect that allowing programs access to the system speaker is one of those things that MS decided to cut out in Vista, but then decided to bring back in 7. "Chris - if it were just a disconnected speaker, I don't think he'd get the runtime error..." What do you think? Dick From akadon1 at live.co.uk Sat Dec 19 02:24:02 2009 From: akadon1 at live.co.uk (james kahn) Date: Sat, 19 Dec 2009 01:24:02 +0000 Subject: [Tutor] Keylogger Message-ID: Hi I am currently developing a keylogger in python 2.5, I have developed a basic keyloger which logs basic keystrokes but I also want it to log URLS which is where I am stuck. Could you please help or guide me as to how I can make it log URLS Regards James _________________________________________________________________ Got more than one Hotmail account? Save time by linking them together http://clk.atdmt.com/UKM/go/186394591/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Dec 19 15:44:23 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 19 Dec 2009 14:44:23 +0000 (GMT) Subject: [Tutor] No beep from winsound.Beep(500, 500) In-Reply-To: References: <716397.48224.qm@web86707.mail.ird.yahoo.com> Message-ID: <6735.29413.qm@web86705.mail.ird.yahoo.com> >> That means your windows sound setup is >> faulty somehow, it might be turned off >> in the BIOS... > Alan, did you see Marc Tompkins post suggesting that my 64-bit Vista > OS might be the cause? I didn't notice you were on Vista let alone 64 bit! It could be, I don't trust Vista at all, haven't gone near it. I'm waiting for Win7 SP1... Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Sat Dec 19 16:13:21 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 20 Dec 2009 02:13:21 +1100 Subject: [Tutor] (no subject) In-Reply-To: <266839.36145.qm@web113212.mail.gq1.yahoo.com> References: <266839.36145.qm@web113212.mail.gq1.yahoo.com> Message-ID: On 12/19/2009 11:34 PM, Richard Hultgren wrote: > Hello, > I am a newcomer, I guess I'm have trouble thinking like a computer yet. > My question is: > in this program: > >>>> resp = raw_input("What's your name? ") > > >>> print "Hi, %s, nice to meet you" % resp > > does %s mean 'place the user response here' No. %s means "call str() on the {first|second|third|etc} element of the tuple on the right-hand side of % operator, and put it in here". The str type overloads %-operator to mean "string interpolation". Basically what it does is this: >>> a = 1 >>> b = 2 >>> c = 4 >>> '%s %s %s' % (a, b, c) '1 2 4' but for convenience, if the right-hand side of the %-operator contains a non-tuple argument, then it will just call str() on the right-hand argument and put it in place of the %s. >>> nottuple = 10 >>> 'hello %s world' % nottuple 'hello 10 world' you may learn that instead of tuple, you can also use dict with %(name)s: >>> '%(a)s %(foo)s' % {'a': 'hello', 'foo': 'world'} 'hello world' and you may also learn later that instead of 's', you can also use other format codes like %f, %d, %x, %o. You can read more here: http://www.python.org/doc/2.5.2/lib/typesseq-strings.html btw, you should also know that the recommended way to interpolate string is to use the new '{0} {1} {2}'.format(a, b, c) > and secondly what and why do > I put % resp at the end of the print statement? I know this is very > basic and future questiions will be more interesting I hope. because python doesn't magically knows what string you want to put in place of %s. You need to specify that you want to put the value of `resp` in place of '%s' From alan.gauld at btinternet.com Sat Dec 19 19:21:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 19 Dec 2009 18:21:20 -0000 Subject: [Tutor] (no subject) References: <266839.36145.qm@web113212.mail.gq1.yahoo.com> Message-ID: "Richard Hultgren" wrote > I am a newcomer, I guess I'm have trouble thinking like a computer yet. > My question is: in this program: >>> resp = raw_input("What's your name? ") > >>> print "Hi, %s, nice to meet you" % resp > > does %s mean 'place the user response here' and secondly what and why > do I put % resp at the end of the print statement? This is known as a format string and the structure is: % So the format string in your case is "Hi, %s, nice to meet you" And every % marker in the string expects a correspomnding argument The % separates the string from its arguments and in your case tyou only have %s so you only need one argument which is resp. So the format operator(%) substitures the argument resp into the format string in place of the %s to give the required result. The format string can contain many % markers each indicating things like different types, how much space to use, wjether to right pr left justify the value etc. You can find out more in vthe Simple sequences topic of my tutor or by searching for format strings in the docs. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Dec 19 19:24:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 19 Dec 2009 18:24:33 -0000 Subject: [Tutor] Keylogger References: Message-ID: "james kahn" wrote > I am currently developing a keylogger in python 2.5, In which OS, this is very likely going to be OS specific. > I have developed a basic keyloger which logs basic > keystrokes but I also want it to log URLS What do you mean log URLs? You mean as users type them? Where, in any program? Or just within a web browser? Or do you mean when users click on URLs? You need to be much more specific about what you want to record. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From dperlman at wisc.edu Sun Dec 20 05:41:21 2009 From: dperlman at wisc.edu (David Perlman) Date: Sat, 19 Dec 2009 22:41:21 -0600 Subject: [Tutor] subclass question Message-ID: If I make a subclass of a built-in class, like this: class mylist(list): def __init__(self): list.__init__(self) Then it is valid for me to do this: >>> x=mylist() >>> x.hello=3 >>> But I can't do this: >>> y=list() >>> y.hello=3 Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'hello' >>> What is it that is special about built-in classes that prevents you from adding methods or, uh, whatever the generic term is for sub- variables? Is there a way to make your own classes restricted like that? OK thanks! -- -dave---------------------------------------------------------------- Unfortunately, as soon as they graduate, our people return to a world driven by a tool that is the antithesis of thinking: PowerPoint. Make no mistake, PowerPoint is not a neutral tool ? it is actively hostile to thoughtful decision-making. It has fundamentally changed our culture by altering the expectations of who makes decisions, what decisions they make and how they make them. -Colonel T. X. Hammes, USMC From roadierich at googlemail.com Sun Dec 20 10:39:25 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Sun, 20 Dec 2009 09:39:25 +0000 Subject: [Tutor] subclass question In-Reply-To: References: Message-ID: 2009/12/20 David Perlman : > If I make a subclass of a built-in class, like this: > > class mylist(list): > ? ?def __init__(self): > ? ? ? ?list.__init__(self) > > Then it is valid for me to do this: > >>>> x=mylist() >>>> x.hello=3 >>>> > > But I can't do this: > >>>> y=list() >>>> y.hello=3 > Traceback (most recent call last): > ?File "", line 1, in > AttributeError: 'list' object has no attribute 'hello' >>>> > > What is it that is special about built-in classes that prevents you from > adding methods or, uh, whatever the generic term is for sub-variables? ?Is > there a way to make your own classes restricted like that? > > OK thanks! > > > -- > -dave---------------------------------------------------------------- > Unfortunately, as soon as they graduate, our people return > to a world driven by a tool that is the antithesis of thinking: > PowerPoint. Make no mistake, PowerPoint is not a neutral tool ? > it is actively hostile to thoughtful decision-making. It has > fundamentally changed our culture by altering the expectations > of who makes decisions, what decisions they make and how > they make them. ?-Colonel T. X. Hammes, USMC > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > You can use the __slots__ attribute to get the same behaviour: >>> class Demo(object): ... __slots__ = [] ... >>> d = Demo() >>> d.a=1 Traceback (most recent call last): File "", line 1, in AttributeError: 'Demo' object has no attribute 'a' But beware: You need to add every attribute you want your class to have to __slots__, if they're not defined on the class (i.e. instance variables, defined within methods): >>> class Demo(object): ... def __init__(self): ... self.a = 1 ... __slots__ = [] ... >>> d = Demo() Traceback (most recent call last): File "", line 1, in File "", line 3, in __init__ AttributeError: 'Demo' object has no attribute 'a' >>> class Demo(object): ... def __init__(self): ... self.a = 1 ... __slots__ = ['a'] ... >>> d = Demo() >>> d.a 1 This will need updating every time you add a new instance variable to a class, but it does help detect typos. There is also the option of using __getattr__, __setattr__ and so on, but that's a little more complicated. I will however say, that the original behaviour is entirely intentional. Python was designed to be a language used by consenting adults who should know better than doing things that are likely to break stuff. For instance, there is nothing to stop you from overwriting any of the builtin functions or types. See http://docs.python.org/reference/datamodel.html#slots for some of the other side effects. -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From rdmoores at gmail.com Sun Dec 20 10:48:55 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 20 Dec 2009 01:48:55 -0800 Subject: [Tutor] A Python Pastebin where scripts can be run Message-ID: A couple of days ago I had a link to a Python Pastebin where scripts can be run, but I failed to bookmark it. Of course, I've Googled for it, and searched my Firefox history, but have failed to find it. I'm hoping some Tutor could post the link. Thanks, Dick Moores From roadierich at googlemail.com Sun Dec 20 11:45:16 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Sun, 20 Dec 2009 10:45:16 +0000 Subject: [Tutor] A Python Pastebin where scripts can be run In-Reply-To: References: Message-ID: python.codepad.org is the one I prefer. 2009/12/20 Richard D. Moores : > A couple of days ago I had a link to a Python Pastebin where scripts > can be run, but I failed to bookmark it. Of course, I've Googled for > it, and searched my Firefox history, but have failed to find it. I'm > hoping some Tutor could post the link. > > Thanks, > > Dick Moores > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From rdmoores at gmail.com Sun Dec 20 13:33:38 2009 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 20 Dec 2009 04:33:38 -0800 Subject: [Tutor] A Python Pastebin where scripts can be run In-Reply-To: References: Message-ID: On Sun, Dec 20, 2009 at 02:45, Rich Lovely wrote: > python.codepad.org is the one I prefer. That's it! Thank you. Dick Moores From hultgren1946 at yahoo.com Sun Dec 20 13:53:19 2009 From: hultgren1946 at yahoo.com (Richard Hultgren) Date: Sun, 20 Dec 2009 04:53:19 -0800 (PST) Subject: [Tutor] running function program Message-ID: <906468.73645.qm@web113210.mail.gq1.yahoo.com> Hello, when i try to run this (see below) i get message: socket error: no connection could be made because the target maching actively refused it, and, idle subprocess didn't make connection.? either idle can't start a subprocess or personal firewall software is blocking the connection. def mult(a, b): if b == 0: return 0 rest = mult(a, b - 1) value = a + rest return value print "3 * 2 = ", mult(3, 2) i used idle window(gui) and new window and it works sometimes. Help!? Richard -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Mon Dec 21 00:33:28 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 21 Dec 2009 10:33:28 +1100 Subject: [Tutor] running function program In-Reply-To: <906468.73645.qm@web113210.mail.gq1.yahoo.com> References: <906468.73645.qm@web113210.mail.gq1.yahoo.com> Message-ID: On 12/20/2009 11:53 PM, Richard Hultgren wrote: > Hello, > when i try to run this (see below) i get message: socket error: no > connection could be made because the target maching actively refused it, > and, idle subprocess didn't make connection. either idle can't start a > subprocess or personal firewall software is blocking the connection. It's not the problem with the program. It's a problem with IDLE. You've got a software firewall or some other programs blocking IDLE to make connection with the python subprocess: The workarounds are either (from best to worst): - create a rule to allow python to connect to localhost in your firewall - don't start more than one instance of IDLE, it could messes things up sometimes - restart your machine, it might be some previous IDLE stuck and messing things up - start your program from the command line (without IDLE) - start IDLE without subprocess, be warned that this means your program runs in the same python instance as IDLE itself and if your program pokes around too much it will messes IDLE up. Run this in command line or make a shortcut: C:\Python26\python.exe -m idlelib.idle -n (adjust path and version appropriately) From pedrooconnell at gmail.com Mon Dec 21 07:20:36 2009 From: pedrooconnell at gmail.com (pedro) Date: Mon, 21 Dec 2009 01:20:36 -0500 Subject: [Tutor] how to see a terminal window showing progress of os.system References: Message-ID: On 2009-12-17 20:02:03 -0500, "Alan Gauld" said: > > "pedro" wrote > >> Hi I am sending commands to the command line using python's os.system. >> >> Is there a way to see a terminal window showing the progress of >> os.system as if you had just run the command from a normal terminal >> window? As it is now it runs completely in the background > > You can sometimes launch a terminal with your program running in it but > thats usually not the best way to do it. Normally you would use the > subprocess module and the Popen class to capture the output of the > command and either monitor it for some event or display it within your > own program. That looks more professional and gives you much more > control > > You will find examples of using subprocess in the Using the OS topic > of my tutorial. > > HTH, Thanks I'll check out your site. Pete From m.kossner at tu-bs.de Mon Dec 21 14:48:59 2009 From: m.kossner at tu-bs.de (markus kossner) Date: Mon, 21 Dec 2009 14:48:59 +0100 Subject: [Tutor] saving a numpy ndarray in sqlite3 Message-ID: <4B2F7CCB.5000706@tu-bs.de> Dear Folks, I am wondering about the most efficient way to save a numpy.ndarray in a sqlite3 database file. I was initially thinking about using the 'buffer' type and creating a 'blob' field in the database. however the .from_buffer() routines in numpy assume the array to be recreated having dimension one. so if I do not know the shape of the array when reading from the sqlite3 database, I would have to use additional fields for storing the array shape. Is there a more convenient way? From bgailer at gmail.com Mon Dec 21 16:43:59 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 21 Dec 2009 10:43:59 -0500 Subject: [Tutor] subclass question In-Reply-To: References: Message-ID: <4B2F97BF.4010200@gmail.com> David Perlman wrote: > If I make a subclass of a built-in class, like this: > > class mylist(list): > def __init__(self): > list.__init__(self) > > Then it is valid for me to do this: > > >>> x=mylist() > >>> x.hello=3 > >>> > > But I can't do this: > > >>> y=list() > >>> y.hello=3 > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'list' object has no attribute 'hello' > >>> > > What is it that is special about built-in classes that prevents you > from adding methods or, uh, whatever the generic term is for > sub-variables? Is there a way to make your own classes restricted > like that? Classes, instances, functions may have user-defined attributes. Built-in data types such as list may not. Hence the need to subclass (e.g. list) to add additional attributes. -- Bob Gailer Chapel Hill NC 919-636-4239 From emadnawfal at gmail.com Mon Dec 21 17:40:01 2009 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Mon, 21 Dec 2009 11:40:01 -0500 Subject: [Tutor] How can I make this run faster? Message-ID: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Dear Tutors, The purpose of this script is to see how many vocalized forms map to a single consonantal form. For example, the form "fn" could be fan, fin, fun. The input is a large list (taken from a file) that has ordinary words. The script creates a devocalized list, then compares the two lists. The problem: It takes over an hour to process 1 file. The average file size is 400,000 words. Question: How can I make it run faster? I have a large number of files. Note: I'm not a programmer, so please avoid very technical terms. Thank you in anticipation. def devocalize(word): vowels = "aiou" return "".join([letter for letter in word if letter not in vowels]) vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large list of around 500,000 items vowelled = set(vowelled) unvowelled = set([devocalize(word) for word in vowelled]) for lex in unvowelled: d = {} d[lex] = [word for word in vowelled if devocalize(word) == lex] print lex, " ".join(d[lex]) -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From shawn at skrite.net Mon Dec 21 18:25:33 2009 From: shawn at skrite.net (shawn bright) Date: Mon, 21 Dec 2009 11:25:33 -0600 Subject: [Tutor] problem with socket connection Message-ID: <384c93600912210925n3c57ca88lc39e9ba55b85209@mail.gmail.com> Hey all, I keep getting a connection error 111 connection refused. When i try to connect to a server at a remote ip address. I am using linux on both computers. the socket server looks like this: #!/usr/bin/python import SocketServer class MyTCPHandler(SocketServer.BaseRequestHandler): def handle(self): self.data = self.request.recv(1024).strip() print "%s wrote:" % self.client_address[0] print self.data # just send back the same data, but upper-cased self.request.send(self.data.upper()) if __name__ == "__main__": HOST, PORT = "127.0.0.1", 3000 # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever() and the client looks like this: #!/usr/bin/python import socket import sys HOST, PORT = "ip.address.of.server", 3000 data = "hello world" # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to server and send data sock.connect((HOST, PORT)) sock.send(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) sock.close() print "Sent: %s" % data print "Received: %s" % received the server starts fine on the remote computer, but the client dies immediatly when executed ( to time to find the server, i don't think) with the connection refused error. The port forwarding is set up correctly on both computers, i know because i have another app that runs on 3000 ( but is not running when i am trying this) any tips would be greatly appreciated, thanks sk -------------- next part -------------- An HTML attachment was scrubbed... URL: From nephish at gmail.com Mon Dec 21 19:10:01 2009 From: nephish at gmail.com (shawn bright) Date: Mon, 21 Dec 2009 12:10:01 -0600 Subject: [Tutor] problem with socket connection Message-ID: <384c93600912211010s5c67ae6dvcd47f2cc92b6d807@mail.gmail.com> Hey all, I keep getting a connection error 111 connection refused. When i try to connect to a server at a remote ip address. I am using linux on both computers. the socket server looks like this: #!/usr/bin/python import SocketServer class MyTCPHandler(SocketServer.BaseRequestHandler): def handle(self): self.data = self.request.recv(1024).strip() print "%s wrote:" % self.client_address[0] print self.data # just send back the same data, but upper-cased self.request.send(self.data.upper()) if __name__ == "__main__": HOST, PORT = "127.0.0.1", 3000 # Create the server, binding to localhost on port 9999 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever() and the client looks like this: #!/usr/bin/python import socket import sys HOST, PORT = "ip.address.of.server", 3000 data = "hello world" # Create a socket (SOCK_STREAM means a TCP socket) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to server and send data sock.connect((HOST, PORT)) sock.send(data + "\n") # Receive data from the server and shut down received = sock.recv(1024) sock.close() print "Sent: %s" % data print "Received: %s" % received the server starts fine on the remote computer, but the client dies immediatly when executed ( to time to find the server, i don't think) with the connection refused error. The port forwarding is set up correctly on both computers, i know because i have another app that runs on 3000 ( but is not running when i am trying this) also, it does not work between two linux computer on the same LAN, it does work if i use localhost and run both server and client on the same computer. any tips would be greatly appreciated, thanks sk -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Dec 21 19:23:56 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 21 Dec 2009 13:23:56 -0500 Subject: [Tutor] How can I make this run faster? In-Reply-To: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> References: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Message-ID: <4B2FBD3C.2070901@gmail.com> Emad Nawfal (??? ???? ???) wrote: > Dear Tutors, > The purpose of this script is to see how many vocalized forms map to a > single consonantal form. For example, the form "fn" could be fan, fin, > fun. > > The input is a large list (taken from a file) that has ordinary words. What does "ordinary" mean. Are all the letteers lower case? > The script creates a devocalized list, then compares the two lists. > > The problem: It takes over an hour to process 1 file. The average file > size is 400,000 words. > > Question: How can I make it run faster? I have a large number of files. The major consumer of time is the "outer product:" (for loop with list comprehension). That is 400,000 squared operations! And it is not necessary! Also the program repeatedly call devocalize for the same word! > > Note: I'm not a programmer, so please avoid very technical terms. You are writing a program. Does not that make you a programmer? How can I tell what terms are too technical for you? > > Thank you in anticipation. You are welcome in retrospect. Observations follow: > > def devocalize(word): > vowels = "aiou" > return "".join([letter for letter in word if letter not in vowels]) What happenex to"e"? Use translate to delete the vowels. That will improve speed. "translate( table[, deletechars]) Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256." Here is a program that will be much faster. I changed names to be more meaningful (to me), and created a main function (standard operating procedure in Python). import collections def devowel(word, table=''.join(chr(i) for i in range(256)), vowels="aiou"): return word.translate(table, vowels) def main(): allOriginalWords = ['him', 'ham', 'hum', 'fun', 'fan'] uniqueOriginalWords = set(allOriginalWords) wordDict = collections.defaultdict(list) for word in uniqueOriginalWords: wordDict[devowel(word)].append(word) for lex, wordList in wordDict.iteritems(): print lex, " ".join(wordList) main() -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Mon Dec 21 19:19:19 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 21 Dec 2009 18:19:19 -0000 Subject: [Tutor] How can I make this run faster? References: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Message-ID: "Emad Nawfal (??? ???? ???)" wrote > def devocalize(word): > vowels = "aiou" Should this include 'e'? > return "".join([letter for letter in word if letter not in vowels]) Its probably faster to use a regular expression replacement. Simply replace any vowel with the empty string. > vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large > list > of around 500,000 items > vowelled = set(vowelled) How do you process the file? Do you read it all into memory and then convert it to a set? Or do you process each line (one word per line?) and add the words to the set one by one? The latter is probably faster. > unvowelled = set([devocalize(word) for word in vowelled]) > for lex in unvowelled: > d = {} > d[lex] = [word for word in vowelled if devocalize(word) == lex] I think you could remove the comprehensions and do all of this inside a single loop. One of those cases where a single explicit loop is faster than 2 comprehesions and a loop. But the only way to be sure is to test/profile to see whee the slowdown occurs. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emadnawfal at gmail.com Mon Dec 21 20:36:34 2009 From: emadnawfal at gmail.com (=?UTF-8?B?RW1hZCBOYXdmYWwgKNi52YXZgCDZhtmI2YHZhCDZgNin2K8p?=) Date: Mon, 21 Dec 2009 14:36:34 -0500 Subject: [Tutor] How can I make this run faster? In-Reply-To: References: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Message-ID: <652641e90912211136n5f0612edu16eb55c143daecde@mail.gmail.com> 2009/12/21 Alan Gauld > > "Emad Nawfal (??? ???? ???)" wrote > > > def devocalize(word): >> vowels = "aiou" >> > Should this include 'e'? > > return "".join([letter for letter in word if letter not in vowels]) >> > > Its probably faster to use a regular expression replacement. > Simply replace any vowel with the empty string. > > > vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large >> list >> of around 500,000 items >> vowelled = set(vowelled) >> > > > How do you process the file? Do you read it all into memory and > then convert it to a set? Or do you process each line (one word > per line?) and add the words to the set one by one? The latter > is probably faster. > > > unvowelled = set([devocalize(word) for word in vowelled]) >> for lex in unvowelled: >> d = {} >> d[lex] = [word for word in vowelled if devocalize(word) == lex] >> > > I think you could remove the comprehensions and do all of > this inside a single loop. One of those cases where a single > explicit loop is faster than 2 comprehesions and a loop. > > But the only way to be sure is to test/profile to see whee the slowdown > occurs. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Thank you so much Bob and Alan The script is meant to process Semitic languages, so I was just giving examples from English. I totally forgot the 'e'. Bob's script runs perfectly. I'm a non-programmer in the sense that I know how to do basic things, but not a professional. For example, my script does what I want, but when I needed to look into effeciency, I got stuck. Thank you all for the help. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Mon Dec 21 20:40:53 2009 From: denis.spir at free.fr (spir) Date: Mon, 21 Dec 2009 20:40:53 +0100 Subject: [Tutor] How can I make this run faster? In-Reply-To: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> References: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Message-ID: <20091221204053.622bd2bf@o> Emad Nawfal (??? ???? ???) dixit: > Dear Tutors, > The purpose of this script is to see how many vocalized forms map to a > single consonantal form. For example, the form "fn" could be fan, fin, fun. > > The input is a large list (taken from a file) that has ordinary words. The > script creates a devocalized list, then compares the two lists. > > The problem: It takes over an hour to process 1 file. The average file size > is 400,000 words. > > Question: How can I make it run faster? I have a large number of files. > > Note: I'm not a programmer, so please avoid very technical terms. > > Thank you in anticipation. > > > > > > def devocalize(word): > vowels = "aiou" > return "".join([letter for letter in word if letter not in vowels]) > > > vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large list > of around 500,000 items > > vowelled = set(vowelled) > > unvowelled = set([devocalize(word) for word in vowelled]) > Your problem is algorithmic: the last part below is useless and it's the one that consume most time (a loop over all words on a loop over all words). Instead, as you produce unvowelled lexem versions, just feed a dictionary with unvowelled keys and a list of original vowelled lexems. So, to replace the first list comprehension above (untested): wordmap = {} for lexem in vowelled: unvowelled = devocalize(lexem) # add lexem to list if unvowelled already registered if unvowelled in wordmap: wordmap[unvowelled].append(lexem) # else register unvowelled with first lexem else: wordmap[unvowelled] = [lexem] for (unvowelled,lexems) in wordmap.items(): # items = list of (key:value) pairs print unvowelled, " ".join(lexems) > for lex in unvowelled: > d = {} > d[lex] = [word for word in vowelled if devocalize(word) == lex] > > print lex, " ".join(d[lex]) > Note: If you really had to double loop over a whole lexicon, the trick #1 to highly speed things up is: to first split the list into parts separated on the criterion of (at least) first letter (or other script char you use), and do all process on list list parts. 1 list of 100 --> 10000 loops 10 lists of 10 --> 10 x 100 loops (In your case, it would be more clever to distinguish words on first _consonant_ char!) Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From luhmann_br at yahoo.com Mon Dec 21 22:40:28 2009 From: luhmann_br at yahoo.com (Luhmann) Date: Mon, 21 Dec 2009 13:40:28 -0800 (PST) Subject: [Tutor] =?utf-8?q?R=C3=A9p=2E_=3A__How_can_I_make_this_run_faster?= =?utf-8?q?=3F?= In-Reply-To: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> Message-ID: <649725.30767.qm@web30907.mail.mud.yahoo.com> #Here's my try: vowel_killer_dict = { ord(a): None for a in 'aeiou'} def devocalize(word): ??? return word.translate(vowel_killer_dict) vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] vowelled = set(vowelled) devocalise_dict={} for a in vowelled: ??? devocalise_dict[a]= devocalize(a) ??? unvowelled=set(devocalise_dict.values()) for lex in unvowelled: ??? d={} ??? d[lex] = [word for word in vowelled if devocalise_dict[word] == lex] ??? print lex, " ".join(d[lex]) --- En date de?: Lun, 21.12.09, Emad Nawfal (??? ???? ???) a ?crit?: De: Emad Nawfal (??? ???? ???) Objet: [Tutor] How can I make this run faster? ?: "tutor" Date: lundi 21 D?cembre 2009, 8 h 40 Dear Tutors, The purpose of this script is to see how many vocalized forms map to a single consonantal form. For example, the form "fn" could be fan, fin, fun. The input is a large list (taken from a file) that has ordinary words. The script creates a devocalized list, then compares the two lists. The problem: It takes over an hour to process 1 file. The average file size is 400,000 words. Question: How can I make it run faster? I have a large number of files. Note: I'm not a programmer, so please avoid very technical terms. Thank you in anticipation. def devocalize(word): ??? vowels = "aiou" ??? return "".join([letter for letter in word if letter not in vowels]) ??? ??? vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large list of around 500,000 items vowelled = set(vowelled) ??? unvowelled = set([devocalize(word) for word in vowelled]) for lex in unvowelled: ??? d = {} ??? d[lex] = [word for word in vowelled if devocalize(word) == lex] ??? ??? print lex, " ".join(d[lex]) -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -----La pi?ce jointe associ?e suit----- _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor D?couvrez les photos les plus int?ressantes du jour. http://www.flickr.com/explore/interesting/7days/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfjennings at gmail.com Tue Dec 22 02:40:44 2009 From: dfjennings at gmail.com (Don Jennings) Date: Mon, 21 Dec 2009 20:40:44 -0500 Subject: [Tutor] saving a numpy ndarray in sqlite3 (markus kossner) Message-ID: <4BAA523C-90D9-47CF-B103-8033107D7CD3@gmail.com> I am not familiar with numpy, but you might like to take a look at y_serial which allows you to store python objects in sqlite: http://yserial.sourceforge.net/ Take care, Don From lopoff at gmx.net Tue Dec 22 10:53:19 2009 From: lopoff at gmx.net (MK) Date: Tue, 22 Dec 2009 10:53:19 +0100 Subject: [Tutor] print IP address range to stdout Message-ID: <1261475599.5933.6.camel@Lnx64> Hi there, i have some logical problem. I dont get it done to write my for loops in that way that the ip address range which is given as arguments are correct processed. Meaning that only the ips are printed which the user defines as argument. I tried to make an if statement to stop at the end_adress but it didnt work as it will stop at the end range every time. Here is my program so far: --------------------------------------------------------- sai = start_adress.split(".") eai = end_adress.split(".") # Pr?fen auf g?ltige IP if eai < sai: help_here() #print sai,eai sa1=int(sai[0]) sa2=int(sai[1]) sa3=int(sai[2]) sa4=int(sai[3]) ea1=int(eai[0]) ea2=int(eai[1]) ea3=int(eai[2]) ea4=int(eai[3]) #print sa1,sa2,sa3,sa4 #print ea1,ea2,ea3,ea4 e1=ea1+1 # mu? um 1 erh?ht werden da sonst nur bis ea1-1 e2=ea2+1 e3=ea3+1 e4=ea4+1 ip="" for i in range(sa4,255): ip=sai[0]+"."+sai[1]+"."+sai[2]+"."+str(i) print ip print "-------4--------" for i in range(sa3+1,255): for i2 in range(1,255): ip=sai[0]+"."+sai[1]+"."+str(i)+"."+str(i2) print ip print "-------3--------" sa3=sa3+1 for i in range(sa2+1,e2): for i2 in range(1,255): for i3 in range(1,255): ip=sai[0]+"."+str(i)+"."+str(i2)+"."+str(i3) print ip print "-------2--------" for i in range(sa1+1,e1): for i2 in range(1,255): for i3 in range(1,255): for i4 in range(1,255): ip=str(i)+"."+str(i2)+"."+str(i3)+"."+str(i4) print ip print "-------1--------" --------------------------------------------------------- The start_adress and end_adress are the ip-range. For example: printdomains.py -s 192.168.178.0 -e 193.170.180.4 This should make all ips and stop at the end_adress. Maybe you can help. Thank you. Mac From alan.plum at uni-koeln.de Tue Dec 22 12:30:02 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Tue, 22 Dec 2009 12:30:02 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261475599.5933.6.camel@Lnx64> References: <1261475599.5933.6.camel@Lnx64> Message-ID: <1261481402.2719.0.camel@kallisti> On Di, 2009-12-22 at 10:53 +0100, MK wrote: > Hi there, > > i have some logical problem. I dont get it done to write my for loops in > that way that the ip address range which is given as arguments are > correct processed. Meaning that only the ips are printed which the > user defines as argument. I tried to make an if statement to stop > at the end_adress but it didnt work as it will stop at the end range > every time. > > Here is my program so far: > The start_adress and end_adress are the ip-range. > > For example: > printdomains.py -s 192.168.178.0 -e 193.170.180.4 > > This should make all ips and stop at the end_adress. > > Maybe you can help. > > Thank you. > > Mac > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From davea at ieee.org Tue Dec 22 12:32:09 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 22 Dec 2009 06:32:09 -0500 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261475599.5933.6.camel@Lnx64> References: <1261475599.5933.6.camel@Lnx64> Message-ID: <4B30AE39.3000008@ieee.org> MK wrote: > Hi there, > > i have some logical problem. I dont get it done to write my for loops in > that way that the ip address range which is given as arguments are > correct processed. Meaning that only the ips are printed which the > user defines as argument. I tried to make an if statement to stop > at the end_adress but it didnt work as it will stop at the end range > every time. > > Here is my program so far: > > --------------------------------------------------------- > sai = start_adress.split(".") > eai = end_adress.split(".") > > # Pr?fen auf g?ltige IP > if eai < sai: > help_here() > > #print sai,eai > > sa1=int(sai[0]) > sa2=int(sai[1]) > sa3=int(sai[2]) > sa4=int(sai[3]) > ea1=int(eai[0]) > ea2=int(eai[1]) > ea3=int(eai[2]) > ea4=int(eai[3]) > > #print sa1,sa2,sa3,sa4 > #print ea1,ea2,ea3,ea4 > > e1=ea1+1 # mu? um 1 erh?ht werden da sonst nur bis ea1-1 > e2=ea2+1 > e3=ea3+1 > e4=ea4+1 > > ip="" > for i in range(sa4,255): > ip=sai[0]+"."+sai[1]+"."+sai[2]+"."+str(i) > print ip > print "-------4--------" > > for i in range(sa3+1,255): > for i2 in range(1,255): > ip=sai[0]+"."+sai[1]+"."+str(i)+"."+str(i2) > print ip > print "-------3--------" > > sa3=sa3+1 > for i in range(sa2+1,e2): > for i2 in range(1,255): > for i3 in range(1,255): > ip=sai[0]+"."+str(i)+"."+str(i2)+"."+str(i3) > print ip > print "-------2--------" > > for i in range(sa1+1,e1): > for i2 in range(1,255): > for i3 in range(1,255): > for i4 in range(1,255): > ip=str(i)+"."+str(i2)+"."+str(i3)+"."+str(i4) > print ip > print "-------1--------" > > --------------------------------------------------------- > > The start_adress and end_adress are the ip-range. > > For example: > printdomains.py -s 192.168.178.0 -e 193.170.180.4 > > This should make all ips and stop at the end_adress. > > Maybe you can help. > > Thank you. > > Mac > > > Trying to write nested loops as you have done is going to be very difficult, as the start and end conditions for each nested loop depends on the state of the outer loop. There are several ways you could accomplish the desired loop, but the easiest would probably be to write two functions. The first converts from the four integers in the ip address into a single, larger one. And the other function converts back. Then the main loop is simply a non-nested loop. def to_integer(ip_string): #convert the ip string into a single 32-bit integer def to_string(ip_int): #convert the integer back into a string of four values, with periods between sai = to_integer(start_address) eai = to_integer(end_address) for ip in xrange(sai, eai): result = to_string(ip) print result If you need help writing the two functions, I'm sure many people here could help. But try it for yourself. Note that the int you're looking for in the first function will be gotten by multiplying the various parts of the IP address by different powers of 256. And note that whatever valid IP address you plug into the first function, if you then apply the second function you should get back the string you started with. So it should be easy to test while you're working on it. DaveA From alan.plum at uni-koeln.de Tue Dec 22 12:46:34 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Tue, 22 Dec 2009 12:46:34 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261475599.5933.6.camel@Lnx64> References: <1261475599.5933.6.camel@Lnx64> Message-ID: <1261482394.2719.17.camel@kallisti> On Di, 2009-12-22 at 10:53 +0100, MK wrote: > Here is my program so far: > Please translate comments if you post to an English list. Not everyone speaks German. > The start_adress and end_adress are the ip-range. > > For example: > printdomains.py -s 192.168.178.0 -e 193.170.180.4 > > This should make all ips and stop at the end_adress. IP addresses consist of four blocks of values between 0 and 255 (inclusive). This means they can easily be translated into a hexadecimal value: 255.255.255.0 is ff.ff.ff.00 or 0xffffff00. Knowing this, you could simplify the problem: each block of the start address is offset by 8 bits from the next, so we can do something like this: # Translate the start address blocks into integers: start_blocks = [int(block) for block in start_address.split('.')] # Now offset the blocks and add them together: start = 0 for k, block in enumerate(start_blocks): start += block << (8 * k) # Do the same for the end address: end_blocks = [int(block) for block in end_address.split('.')] end = 0 for k, block in enumerate(end_blocks): end += block << (8 * k) # Now generate the addresses: for ip in range(start, end+1): blocks = [] for i in range(4): blocks.append((ip & (0xff << (8 * i))) >> (8 * i)) print '.'.join(blocks) Hope this helps. I haven't run this code, so you might want to make sure it works correctly before using it. Cheers, Alan Plum From alan.plum at uni-koeln.de Tue Dec 22 13:02:49 2009 From: alan.plum at uni-koeln.de (Alan Plum) Date: Tue, 22 Dec 2009 13:02:49 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261482394.2719.17.camel@kallisti> References: <1261475599.5933.6.camel@Lnx64> <1261482394.2719.17.camel@kallisti> Message-ID: <1261483369.2719.31.camel@kallisti> Hi again, On Di, 2009-12-22 at 12:46 +0100, Alan Plum wrote: > # Now generate the addresses: > for ip in range(start, end+1): > blocks = [] > for i in range(4): > blocks.append((ip & (0xff << (8 * i))) >> (8 * i)) > print '.'.join(blocks) I just realised this gives you the blocks in reverse order. You probably want to use prepend() rather than append() -- or just use reversed() before you print. Also note that there's probably a better way to create the actual blocks from the integer, but this should suffice. If you're not familiar with the bitwise operators: the double brackets are shift operators (left shift and right shift respectively) and shift the value by a number of bits (`8 * i for i in range(4)`, i.e. `for j in [0,8,16,24]`). The ampersand is a bitwise AND which "switches off" all bits other than the ones you select (in this case 0xff, i.e. one block of 8 bits). By combining them you first define the bits you want (0xff shifted by i bits to the left, i.e. [0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000]. Then you apply the mask to the integer, switching off all the bits that are not part of the block you want. Then you shift the result back (i.e. to the right) the number of bits you shifted the mask to the left, leaving the exact block value (otherwise your block value would be off by several orders of hexadecimal magnitude, i.e. 256 (256^1) for the second (counting from the right), 256*256 (256^2) for the third and 256*256*256 (256^3) for the fourth. Also, range() takes endpoints to be exclusive, which is why you iterate over range(start, end+1) rather than range(start, end). Provided you want it to behave that way. Cheers, Alan Plum From lopoff at gmx.net Tue Dec 22 13:56:40 2009 From: lopoff at gmx.net (MK) Date: Tue, 22 Dec 2009 13:56:40 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <4B30AE39.3000008@ieee.org> References: <1261475599.5933.6.camel@Lnx64> <4B30AE39.3000008@ieee.org> Message-ID: <1261486600.10306.4.camel@Lnx64> Ok. That was very helpful. As i dont know how to do it i googled and found this one: http://anonymouse.org/cgi-bin/anon-www.cgi/http://snipplr.com/view/14807/convert-ip-to-int-and-int-to-ip/ But frankly i dont understand it. The program works now like it should but i want to understand the code i use. So any help would be great. First function the ip is splitted as i did it. Alright. The use 256 as it is the maximum for any digit. ok. But what is that ** and exp meaning ???? ---------------------------------------------------------- def ip_to_int(dotted_ip): exp = 3 intip = 0 for quad in dotted_ip.split('.'): intip = intip + (int(quad) * (256 ** exp)) exp = exp - 1 return(intip) --------------------------------------------------- def int_to_ip(intip): octet = '' for exp in [3,2,1,0]: octet = octet + str(intip / (256 ** exp)) + "." intip = intip % ( 256 ** exp) return (octet.rstrip(".")) Am Dienstag, den 22.12.2009, 06:32 -0500 schrieb Dave Angel: > > MK wrote: > > Hi there, > > > > i have some logical problem. I dont get it done to write my for loops in > > that way that the ip address range which is given as arguments are > > correct processed. Meaning that only the ips are printed which the > > user defines as argument. I tried to make an if statement to stop > > at the end_adress but it didnt work as it will stop at the end range > > every time. > > > > Here is my program so far: > > > > --------------------------------------------------------- > > sai = start_adress.split(".") > > eai = end_adress.split(".") > > > > # Pr?fen auf g?ltige IP > > if eai < sai: > > help_here() > > > > #print sai,eai > > > > sa1=int(sai[0]) > > sa2=int(sai[1]) > > sa3=int(sai[2]) > > sa4=int(sai[3]) > > ea1=int(eai[0]) > > ea2=int(eai[1]) > > ea3=int(eai[2]) > > ea4=int(eai[3]) > > > > #print sa1,sa2,sa3,sa4 > > #print ea1,ea2,ea3,ea4 > > > > e1=ea1+1 # mu? um 1 erh?ht werden da sonst nur bis ea1-1 > > e2=ea2+1 > > e3=ea3+1 > > e4=ea4+1 > > > > ip="" > > for i in range(sa4,255): > > ip=sai[0]+"."+sai[1]+"."+sai[2]+"."+str(i) > > print ip > > print "-------4--------" > > > > for i in range(sa3+1,255): > > for i2 in range(1,255): > > ip=sai[0]+"."+sai[1]+"."+str(i)+"."+str(i2) > > print ip > > print "-------3--------" > > > > sa3=sa3+1 > > for i in range(sa2+1,e2): > > for i2 in range(1,255): > > for i3 in range(1,255): > > ip=sai[0]+"."+str(i)+"."+str(i2)+"."+str(i3) > > print ip > > print "-------2--------" > > > > for i in range(sa1+1,e1): > > for i2 in range(1,255): > > for i3 in range(1,255): > > for i4 in range(1,255): > > ip=str(i)+"."+str(i2)+"."+str(i3)+"."+str(i4) > > print ip > > print "-------1--------" > > > > --------------------------------------------------------- > > > > The start_adress and end_adress are the ip-range. > > > > For example: > > printdomains.py -s 192.168.178.0 -e 193.170.180.4 > > > > This should make all ips and stop at the end_adress. > > > > Maybe you can help. > > > > Thank you. > > > > Mac > > > > > > > Trying to write nested loops as you have done is going to be very > difficult, as the start and end conditions for each nested loop depends > on the state of the outer loop. > > There are several ways you could accomplish the desired loop, but the > easiest would probably be to write two functions. The first converts > from the four integers in the ip address into a single, larger one. And > the other function converts back. Then the main loop is simply a > non-nested loop. > > def to_integer(ip_string): > #convert the ip string into a single 32-bit integer > > def to_string(ip_int): > #convert the integer back into a string of four values, with > periods between > > sai = to_integer(start_address) > eai = to_integer(end_address) > for ip in xrange(sai, eai): > result = to_string(ip) > print result > > If you need help writing the two functions, I'm sure many people here > could help. But try it for yourself. Note that the int you're looking > for in the first function will be gotten by multiplying the various > parts of the IP address by different powers of 256. > > And note that whatever valid IP address you plug into the first > function, if you then apply the second function you should get back the > string you started with. So it should be easy to test while you're > working on it. > > DaveA From roadierich at googlemail.com Tue Dec 22 18:36:21 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Tue, 22 Dec 2009 17:36:21 +0000 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261486600.10306.4.camel@Lnx64> References: <1261475599.5933.6.camel@Lnx64> <4B30AE39.3000008@ieee.org> <1261486600.10306.4.camel@Lnx64> Message-ID: 2009/12/22 MK : > Ok. That was very helpful. As i dont know how to do it i googled > and found this one: > http://anonymouse.org/cgi-bin/anon-www.cgi/http://snipplr.com/view/14807/convert-ip-to-int-and-int-to-ip/ > > But frankly i dont understand it. The program works now like it should > but i want to understand the code i use. So any help would be great. > > First function the ip is splitted as i did it. Alright. > The use 256 as it is the maximum for any digit. ok. > But what is that ** and exp meaning ???? > > ---------------------------------------------------------- > def ip_to_int(dotted_ip): > ? ? ? ?exp = 3 > ? ? ? ?intip = 0 > ? ? ? ?for quad in dotted_ip.split('.'): > ? ? ? ? ? ? ? ?intip = intip + (int(quad) * (256 ** exp)) > ? ? ? ? ? ? ? ?exp = exp - 1 > ? ? ? ?return(intip) > > --------------------------------------------------- > > def int_to_ip(intip): > ? ? ? ?octet = '' > ? ? ? ?for exp in [3,2,1,0]: > ? ? ? ? ? ? ? ?octet = octet + str(intip / (256 ** exp)) + "." > ? ? ? ? ? ? ? ?intip = intip % ( 256 ** exp) > ? ? ? ?return (octet.rstrip(".")) > > Am Dienstag, den 22.12.2009, 06:32 -0500 schrieb Dave Angel: > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The ** operator means "to the power of", and is sometimes seen in text as "^" (but that means something different in python). 3**5 is 3 to the power of 5,or 3*3*3*3*3 (3 times itself five times) or 243. As it's only a short loop, we can unroll it quite easily, to make it clear what is happening. def ip_to_int(dotted_ip): exp = 3 intip = 0 for quad in dotted_ip.split('.'): intip = intip + (int(quad) * (256 ** exp)) exp = exp - 1 return(intip) Unrolling the for loop: def ip_to_int(dotted_ip): exp = 3 ?? ?intip = 0 quads = dotted_ip.split('.') #Unrolled ? ?quad = quads[0] ? ?intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * (256**3 (=16777216, =0xff000000)) ? ?exp = exp - 1 # exp = 2 quad = quads[1] ? ?intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * (256**2 (=65536, =0x00ff000000) ? ?exp = exp - 1 # exp = 1 quad = quads[2] ? ?intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * (256**1 (=256, =0x0000ff00) ? ?exp = exp - 1 # exp = 0 quad = quads[3] ? ?intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * (256**0) ? ?exp = exp - 1 # exp = -1 ? ?return(intip) Now, cleaning that up, we get: def ip_to_int(dotted_ip): quads = dotted_ip.split('.') intip = int(quads[0]) * 0xff000000 + int(quads[1]) * 0xff000000 intip += int(quads[2]) * 0xff00 + int(quads[3]) return(intip) So, what it does is it takes each "quad" (the term for each number in an IP address), multiply it by a certain constant depending on where in the address it falls, and then adding it to the numeric address. Perhaps there's a library function to do this, but it's a useful learning experience - although a quick search of the docs hasn't turned anything up. -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From denis.spir at free.fr Tue Dec 22 19:09:16 2009 From: denis.spir at free.fr (spir) Date: Tue, 22 Dec 2009 19:09:16 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <1261486600.10306.4.camel@Lnx64> References: <1261475599.5933.6.camel@Lnx64> <4B30AE39.3000008@ieee.org> <1261486600.10306.4.camel@Lnx64> Message-ID: <20091222190916.733db48d@o> MK dixit: > First function the ip is splitted as i did it. Alright. > The use 256 as it is the maximum for any digit. ok. > But what is that ** and exp meaning ???? > > ---------------------------------------------------------- > def ip_to_int(dotted_ip): > exp = 3 > intip = 0 > for quad in dotted_ip.split('.'): > intip = intip + (int(quad) * (256 ** exp)) > exp = exp - 1 > return(intip) > > --------------------------------------------------- As a global explaination, this convert an ip address to a single integer, like if it were a number written in base 256 with 4 digits A,B,C,D: aaa.bbb.ccc.ddd --> (A,B,C,D) --> A*(256^3) + B*(256^2) + C*(256^1) + D * (256^0) --> n Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From dwbarne at earthlink.net Tue Dec 22 19:18:56 2009 From: dwbarne at earthlink.net (dwbarne at earthlink.net) Date: Tue, 22 Dec 2009 11:18:56 -0700 (GMT-07:00) Subject: [Tutor] python and kiviat diagrams Message-ID: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> One of the drawbacks of Matplotlib is that it does not have the capability of drawing Kiviat diagrams. Does anyone know of a software package for drawing Kiviat diagrams written in Python? Daniel From davea at ieee.org Tue Dec 22 20:32:21 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 22 Dec 2009 14:32:21 -0500 Subject: [Tutor] print IP address range to stdout In-Reply-To: References: <1261475599.5933.6.camel@Lnx64> <4B30AE39.3000008@ieee.org> <1261486600.10306.4.camel@Lnx64> Message-ID: <4B311EC5.5050504@ieee.org> Rich Lovely wrote: > 2009/12/22 MK : > >> Ok. That was very helpful. As i dont know how to do it i googled >> and found this one: >> http://anonymouse.org/cgi-bin/anon-www.cgi/http://snipplr.com/view/14807/convert-ip-to-int-and-int-to-ip/ >> >> But frankly i dont understand it. The program works now like it should >> but i want to understand the code i use. So any help would be great. >> >> First function the ip is splitted as i did it. Alright. >> The use 256 as it is the maximum for any digit. ok. >> But what is that ** and exp meaning ???? >> >> ---------------------------------------------------------- >> def ip_to_int(dotted_ip): >> exp = 3 >> intip = 0 >> for quad in dotted_ip.split('.'): >> intip = intip + (int(quad) * (256 ** exp)) >> exp = exp - 1 >> return(intip) >> >> --------------------------------------------------- >> >> def int_to_ip(intip): >> octet = '' >> for exp in [3,2,1,0]: >> octet = octet + str(intip / (256 ** exp)) + "." >> intip = intip % ( 256 ** exp) >> return (octet.rstrip(".")) >> >> Am Dienstag, den 22.12.2009, 06:32 -0500 schrieb Dave Angel: >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > The ** operator means "to the power of", and is sometimes seen in text > as "^" (but that means something different in python). > > 3**5 is 3 to the power of 5,or 3*3*3*3*3 (3 times itself five times) or 243. > > As it's only a short loop, we can unroll it quite easily, to make it > clear what is happening. > > def ip_to_int(dotted_ip): > exp = 3 > intip = 0 > for quad in dotted_ip.split('.'): > intip = intip + (int(quad) * (256 ** exp)) > exp = exp - 1 > return(intip) > > Unrolling the for loop: > def ip_to_int(dotted_ip): > exp = 3 > intip = 0 > quads = dotted_ip.split('.') > > #Unrolled > > quad = quads[0] > intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * > (256**3 (=16777216, =0xff000000)) > exp = exp - 1 # exp = 2 > quad = quads[1] > intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * > (256**2 (=65536, =0x00ff000000) > exp = exp - 1 # exp = 1 > quad = quads[2] > intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * > (256**1 (=256, =0x0000ff00) > exp = exp - 1 # exp = 0 > quad = quads[3] > intip = intip + (int(quad) * (256 ** exp)) # intip + int(quad) * (256**0) > exp = exp - 1 # exp = -1 > > return(intip) > > Now, cleaning that up, we get: > > def ip_to_int(dotted_ip): > quads = dotted_ip.split('.') > intip = int(quads[0]) * 0xff000000 + int(quads[1]) * 0xff000000 > intip += int(quads[2]) * 0xff00 + int(quads[3]) > return(intip) > > So, what it does is it takes each "quad" (the term for each number in > an IP address), multiply it by a certain constant depending on where > in the address it falls, and then adding it to the numeric address. > > Perhaps there's a library function to do this, but it's a useful > learning experience - although a quick search of the docs hasn't > turned anything up. > > MK: My description for the first function was: >>>Note that the int you're looking for in the first function will be gotten by multiplying the various parts of the IP address by different powers of 256. and that's just what the ** operator does. I personally would shorten it to: def ip_to_int(dotted_ip): intip = 0 for quad in dotted_ip.split('.'): intip = intip * 256 + int(quad) return(intip) Rich, re. library functions: I'm sure one could format a string of 8 hex digits by using a zero-filled conversion of each int. Then convert that to a single int with int(hex_string, 16). But I don't think it'd be worth it. Similarly, we could do 4 slices of hex() of the int, and convert each to decimal for display. DaveA From hgfernan at lsi.usp.br Tue Dec 22 20:26:24 2009 From: hgfernan at lsi.usp.br (Hilton Garcia Fernandes) Date: Tue, 22 Dec 2009 17:26:24 -0200 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> References: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> Message-ID: <200912221726.26864.hgfernan@lsi.usp.br> Hello Daniel. There's a statistical package named R, from http://r-project.org that has pretty much everything related to statistics and data analysis. Kiviat diagrams, also known as star plots, or web plots etc., are among its abilities -- directly or using packages like 'rela' or 'homals', all available from the Comprehensive R Archive Network, or cran, at http://cran.r-project.org/ Its many abilities can be easily used in Python through the rpy package, that is available from http://rpy.sourceforge.net/ All the best, hilton On Tuesday 22 December 2009 16:18:56 dwbarne at earthlink.net wrote: > One of the drawbacks of Matplotlib is that it does not have the capability > of drawing Kiviat diagrams. > > Does anyone know of a software package for drawing Kiviat diagrams written > in Python? > > Daniel > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Hilton Garcia Fernandes Nucleo de Tecnologias sem Fio (NTSF) -- Wireless Technologies Team Lab de Sistemas Integraveis Tecnologico (LSI) -- Integrable Systems Lab Escola Politecnica (Poli) -- Engineering School Univ S Paulo (USP) Tel: (5511)3091-5311 (work) ? ? ?(5511)8131-5213 (mobile) Av. Prof. Luciano Gualberto,158 trav.3 CEP 05508-010 S. Paulo -- SP -- Brazil Pagina inicial: http://www.lsi.usp.br/~hgfernan From robert.johansson at math.umu.se Tue Dec 22 22:14:54 2009 From: robert.johansson at math.umu.se (Robert Johansson) Date: Tue, 22 Dec 2009 22:14:54 +0100 Subject: [Tutor] importing Message-ID: <000001ca834b$cefcf2b0$6cf6d810$@johansson@math.umu.se> Hi all, suppose I need to import a module inside a class and that I need to use it in to different methods. Is this is the way to do it? class test(): import time def method1(self): print 'method 1: ', time.clock() def method2(self): print 'method 2: ', time.clock() If I only need the module in one method should I then place it inside that method or keep it outside? class test(): def method1(self): import time print 'method 1: ', time.clock() def method2(self): print 'method 2: ' thanks, Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue Dec 22 22:29:55 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 23 Dec 2009 08:29:55 +1100 Subject: [Tutor] importing In-Reply-To: <15786.7137728774$1261516604@news.gmane.org> References: <15786.7137728774$1261516604@news.gmane.org> Message-ID: On 12/23/2009 8:14 AM, Robert Johansson wrote: > Hi all, suppose I need to import a module inside a class and that I need > to use it in to different methods. Is this is the way to do it? generally you should keep all imports at the top of the file. There are times when importing only to specific methods or class is useful, but in general keep them at top. and in those cases where you import to inside a class, the module becomes a class attribute so you use self. or ClassName. to reach the module: class ClassName(object): import random def foo(self): print self.random.random() print ClassName.random.random() From emile at fenx.com Tue Dec 22 22:36:49 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 22 Dec 2009 13:36:49 -0800 Subject: [Tutor] importing In-Reply-To: <15786.7137728774$1261516604@news.gmane.org> References: <15786.7137728774$1261516604@news.gmane.org> Message-ID: On 12/22/2009 1:14 PM Robert Johansson said... > Hi all, suppose I need to import a module inside a class and that I need > to use it in to different methods. Is this is the way to do it? Well, neither is really (see comments below). I generally import only at the top of a module. You can of course import within a method as needed, and I sometimes will do so if the imported module will only be used in the single method, but I tend not to as I've come over time to expect the imports at the top. > > > > class test(): > import time > def method1(self): > print 'method 1: ', time.clock() this won't work -- time isn't within method1's accessible scope (local, global, builtin). You could do test.time.clock() to access it, but importing globally won't confuse a reader by thinking it's something different. Emile > def method2(self): > print 'method 2: ', time.clock() > > > If I only need the module in one method should I then place it inside > that method or keep it outside? > > > > class test(): > def method1(self): > import time > print 'method 1: ', time.clock() > def method2(self): > print 'method 2: ' From kent37 at tds.net Tue Dec 22 22:45:18 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 22 Dec 2009 16:45:18 -0500 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> References: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> Message-ID: <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> On Tue, Dec 22, 2009 at 1:18 PM, wrote: > One of the drawbacks of Matplotlib is that it does not have the capability of drawing Kiviat diagrams. > > Does anyone know of a software package for drawing Kiviat diagrams written in Python? I don't know what a Kiviat diagram is but the images google shows me look a lot like this: http://matplotlib.sourceforge.net/examples/api/radar_chart.html Kent From hgfernan at lsi.usp.br Tue Dec 22 23:13:47 2009 From: hgfernan at lsi.usp.br (Hilton Garcia Fernandes) Date: Tue, 22 Dec 2009 20:13:47 -0200 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> References: <1664571.1261505936307.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> Message-ID: <200912222013.57729.hgfernan@lsi.usp.br> Congratulations ! It looks like matplotlib does the trick. I became an rpy evangelist because this binding connect two very interesting, powerful and flexible things: R and Python. Thanks, hilton On Tuesday 22 December 2009 19:45:18 Kent Johnson wrote: > On Tue, Dec 22, 2009 at 1:18 PM, wrote: > > One of the drawbacks of Matplotlib is that it does not have the > > capability of drawing Kiviat diagrams. > > > > Does anyone know of a software package for drawing Kiviat diagrams > > written in Python? > > I don't know what a Kiviat diagram is but the images google shows me > look a lot like this: > http://matplotlib.sourceforge.net/examples/api/radar_chart.html > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Hilton Garcia Fernandes Nucleo de Tecnologias sem Fio (NTSF) -- Wireless Technologies Team Lab de Sistemas Integraveis Tecnologico (LSI) -- Integrable Systems Lab Escola Politecnica (Poli) -- Engineering School Univ S Paulo (USP) Tel: (5511)3091-5311 (work) ? ? ?(5511)8131-5213 (mobile) Av. Prof. Luciano Gualberto,158 trav.3 CEP 05508-010 S. Paulo -- SP -- Brazil Pagina inicial: http://www.lsi.usp.br/~hgfernan From emadnawfal at gmail.com Tue Dec 22 23:27:17 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrj3CDk5t3hINzHzyk=?=) Date: Tue, 22 Dec 2009 17:27:17 -0500 Subject: [Tutor] =?iso-8859-1?q?R=E9p=2E_=3A__How_can_I_make_this_run_fast?= =?iso-8859-1?q?er=3F?= In-Reply-To: <649725.30767.qm@web30907.mail.mud.yahoo.com> References: <652641e90912210840m1a3754f2s4b1ba342e4e977c6@mail.gmail.com> <649725.30767.qm@web30907.mail.mud.yahoo.com> Message-ID: <652641e90912221427w6c9c40b8g5b10c2524dc2c358@mail.gmail.com> On Mon, Dec 21, 2009 at 4:40 PM, Luhmann wrote: > #Here's my try: > > vowel_killer_dict = { ord(a): None for a in 'aeiou'} > > def devocalize(word): > return word.translate(vowel_killer_dict) > > vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] > vowelled = set(vowelled) > > > devocalise_dict={} > > > for a in vowelled: > devocalise_dict[a]= devocalize(a) > > > unvowelled=set(devocalise_dict.values()) > > for lex in unvowelled: > d={} > d[lex] = [word for word in vowelled if devocalise_dict[word] == lex] > print lex, " ".join(d[lex]) > > > > > --- En date de : *Lun, 21.12.09, Emad Nawfal (??? ???? ???) < > emadnawfal at gmail.com>* a ?crit : > > > De: Emad Nawfal (??? ???? ???) > Objet: [Tutor] How can I make this run faster? > ?: "tutor" > Date: lundi 21 D?cembre 2009, 8 h 40 > > Dear Tutors, > The purpose of this script is to see how many vocalized forms map to a > single consonantal form. For example, the form "fn" could be fan, fin, fun. > > The input is a large list (taken from a file) that has ordinary words. The > script creates a devocalized list, then compares the two lists. > > The problem: It takes over an hour to process 1 file. The average file size > is 400,000 words. > > Question: How can I make it run faster? I have a large number of files. > > Note: I'm not a programmer, so please avoid very technical terms. > > Thank you in anticipation. > > > > > > def devocalize(word): > vowels = "aiou" > return "".join([letter for letter in word if letter not in vowels]) > > > vowelled = ['him', 'ham', 'hum', 'fun', 'fan'] # input, usually a large > list of around 500,000 items > > vowelled = set(vowelled) > > unvowelled = set([devocalize(word) for word in vowelled]) > > > for lex in unvowelled: > d = {} > d[lex] = [word for word in vowelled if devocalize(word) == lex] > > print lex, " ".join(d[lex]) > Thank you Dennis and Luhman. I really appreciate your help. > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? > ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- > > -----La pi?ce jointe associ?e suit----- > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > D?couvrez les photos * les plus int?ressantes du jour!* -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From hgfernan at lsi.usp.br Tue Dec 22 23:45:28 2009 From: hgfernan at lsi.usp.br (Hilton Garcia Fernandes) Date: Tue, 22 Dec 2009 20:45:28 -0200 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <21194118.1261521575781.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> References: <21194118.1261521575781.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> Message-ID: <200912222045.34610.hgfernan@lsi.usp.br> OK. But, please don't you forget to try R and rpy ! :) These guys do very clever tricks. On Tuesday 22 December 2009 20:39:35 dwbarne at earthlink.net wrote: > Kent and Hilton, > > I'm a victim of outdated documentation. Mine is v0.98 (no radar charts), > while latest is v0.99. A small step in numbers, but a giant leap in > capability! > > Thanks! Looks like Matplotlib will do the trick after all. > > Daniel > > > -----Original Message----- > > >From: Kent Johnson > >Sent: Dec 22, 2009 4:45 PM > >To: dwbarne at earthlink.net > >Cc: python tutor > >Subject: Re: [Tutor] python and kiviat diagrams > > > >On Tue, Dec 22, 2009 at 1:18 PM, wrote: > >> One of the drawbacks of Matplotlib is that it does not have the > >> capability of drawing Kiviat diagrams. > >> > >> Does anyone know of a software package for drawing Kiviat diagrams > >> written in Python? > > > >I don't know what a Kiviat diagram is but the images google shows me > >look a lot like this: > >http://matplotlib.sourceforge.net/examples/api/radar_chart.html > > > >Kent -- Hilton Garcia Fernandes Nucleo de Tecnologias sem Fio (NTSF) -- Wireless Technologies Team Lab de Sistemas Integraveis Tecnologico (LSI) -- Integrable Systems Lab Escola Politecnica (Poli) -- Engineering School Univ S Paulo (USP) Tel: (5511)3091-5311 (work) ? ? ?(5511)8131-5213 (mobile) Av. Prof. Luciano Gualberto,158 trav.3 CEP 05508-010 S. Paulo -- SP -- Brazil Pagina inicial: http://www.lsi.usp.br/~hgfernan From dwbarne at earthlink.net Tue Dec 22 23:39:35 2009 From: dwbarne at earthlink.net (dwbarne at earthlink.net) Date: Tue, 22 Dec 2009 17:39:35 -0500 (EST) Subject: [Tutor] python and kiviat diagrams Message-ID: <21194118.1261521575781.JavaMail.root@elwamui-karabash.atl.sa.earthlink.net> Kent and Hilton, I'm a victim of outdated documentation. Mine is v0.98 (no radar charts), while latest is v0.99. A small step in numbers, but a giant leap in capability! Thanks! Looks like Matplotlib will do the trick after all. Daniel -----Original Message----- >From: Kent Johnson >Sent: Dec 22, 2009 4:45 PM >To: dwbarne at earthlink.net >Cc: python tutor >Subject: Re: [Tutor] python and kiviat diagrams > >On Tue, Dec 22, 2009 at 1:18 PM, wrote: >> One of the drawbacks of Matplotlib is that it does not have the capability of drawing Kiviat diagrams. >> >> Does anyone know of a software package for drawing Kiviat diagrams written in Python? > >I don't know what a Kiviat diagram is but the images google shows me >look a lot like this: >http://matplotlib.sourceforge.net/examples/api/radar_chart.html > >Kent From lopoff at gmx.net Wed Dec 23 11:14:43 2009 From: lopoff at gmx.net (MK) Date: Wed, 23 Dec 2009 11:14:43 +0100 Subject: [Tutor] print IP address range to stdout In-Reply-To: <20091222190916.733db48d@o> References: <1261475599.5933.6.camel@Lnx64> <4B30AE39.3000008@ieee.org> <1261486600.10306.4.camel@Lnx64> <20091222190916.733db48d@o> Message-ID: <1261563283.9142.8.camel@Lnx64> Am Dienstag, den 22.12.2009, 19:09 +0100 schrieb spir: > MK dixit: > > > First function the ip is splitted as i did it. Alright. > > The use 256 as it is the maximum for any digit. ok. > > But what is that ** and exp meaning ???? > > > > ---------------------------------------------------------- > > def ip_to_int(dotted_ip): > > exp = 3 > > intip = 0 > > for quad in dotted_ip.split('.'): > > intip = intip + (int(quad) * (256 ** exp)) > > exp = exp - 1 > > return(intip) > > > > --------------------------------------------------- > > As a global explaination, this convert an ip address to a single integer, like if it were a number written in base 256 with 4 digits A,B,C,D: > aaa.bbb.ccc.ddd > --> (A,B,C,D) > --> A*(256^3) + B*(256^2) + C*(256^1) + D * (256^0) > --> n > > Denis > ________________________________ > > la vita e estrany > > http://spir.wikidot.com/ Ok. Thank you. I have something to learn. But now its clear. Like the binarysystem with 2**n etc. So to convert betweenn numbersystems i go always this way. From fomcl at yahoo.com Wed Dec 23 23:27:29 2009 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 23 Dec 2009 14:27:29 -0800 (PST) Subject: [Tutor] python and kiviat diagrams In-Reply-To: <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> Message-ID: <964793.20906.qm@web110703.mail.gq1.yahoo.com> I was studying the code on http://matplotlib.sourceforge.net/examples/api/radar_chart.html. Isn't it very unusual that a class is defined within a function? Why not use a class instance inside the function instead? No methods of the class can currently be inherited outside the scope of the function, right? Just curious.. Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Tue, 12/22/09, Kent Johnson wrote: From: Kent Johnson Subject: Re: [Tutor] python and kiviat diagrams To: dwbarne at earthlink.net Cc: "python tutor" Date: Tuesday, December 22, 2009, 10:45 PM On Tue, Dec 22, 2009 at 1:18 PM,? wrote: > One of the drawbacks of Matplotlib is that it does not have the capability of drawing Kiviat diagrams. > > Does anyone know of a software package for drawing Kiviat diagrams written in Python? I don't know what a Kiviat diagram is but the images google shows me look a lot like this: http://matplotlib.sourceforge.net/examples/api/radar_chart.html Kent _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Dec 24 00:44:00 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 23 Dec 2009 15:44:00 -0800 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <964793.20906.qm@web110703.mail.gq1.yahoo.com> References: <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> <964793.20906.qm@web110703.mail.gq1.yahoo.com> Message-ID: On 12/23/2009 2:27 PM Albert-Jan Roskam said... > I was studying the code on > http://matplotlib.sourceforge.net/examples/api/radar_chart.html. > Isn't it very unusual that a class is defined within a function? Not when defining factory creation functions which a quick look at this code confirms that's what this says it's doing. They're often used when creating compatibility layers to provide common named methods. > Why not use a class instance inside the function instead? That's a question for the original developer. You can get to the same place in many different ways, and the concept of 'There should be one-- and preferably only one --obvious way to do it.' no longer commands the same respect at this level of design. > No methods of the > class can currently be inherited outside the scope of the function, right? Yes -- the class has served it's purpose once it's been registered. It can of course be reached and could likely be invoked and could even possibly be used to create new instances, but you'd have to introspect and find the right __class__ attribute. Both this level of introspection and factory methods are intermediate to advanced techniques that most of us will only rarely need, but it's good to know it can be done. Emile From kent37 at tds.net Thu Dec 24 14:19:15 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 24 Dec 2009 08:19:15 -0500 Subject: [Tutor] python and kiviat diagrams In-Reply-To: <964793.20906.qm@web110703.mail.gq1.yahoo.com> References: <1c2a2c590912221345s7ceaac50o6159014976c59bc0@mail.gmail.com> <964793.20906.qm@web110703.mail.gq1.yahoo.com> Message-ID: <1c2a2c590912240519k3751c57ahf6573f30c22793f7@mail.gmail.com> On Wed, Dec 23, 2009 at 5:27 PM, Albert-Jan Roskam wrote: > > I was studying the code on http://matplotlib.sourceforge.net/examples/api/radar_chart.html. > Isn't it very unusual that a class is defined within a function? Unusual, but not un-heard of. Usually the function is a factory function that returns the class to the caller. In this case the class is registered as the handler for projections of type 'radar'. Presumably RadarAxes is implementing a protocol (interface) that is required by register_projection(). > Why not use a class instance inside the function instead? Presumably register_projection() requires a class (strictly speaking, it probably requires a callable that returns an instance of a class that implements the required protocol). There don't seem to be any docs for register_projection() so it's hard to know for sure what it wants. > No methods of the class can currently be inherited outside the scope of the function, right? Do you mean, the class cannot be subclassed? Not easily, only because the function doesn't return the class. There is no inherent reason this class could not be subclassed if a reference to the class were available. Kent From ziyad.albatly at gmail.com Thu Dec 24 16:25:46 2009 From: ziyad.albatly at gmail.com (=?UTF-8?B?2LLZitin2K8g2KjZhiDYudio2K/Yp9mE2LnYstmK2LIg2KfZhNio2Kc=?= =?UTF-8?B?2KrZhNmK?=) Date: Thu, 24 Dec 2009 18:25:46 +0300 Subject: [Tutor] problem with socket connection In-Reply-To: <384c93600912211010s5c67ae6dvcd47f2cc92b6d807@mail.gmail.com> References: <384c93600912211010s5c67ae6dvcd47f2cc92b6d807@mail.gmail.com> Message-ID: <20091224182546.111c00e4@ubuntu> On Mon, 21 Dec 2009 12:10:01 -0600 shawn bright wrote: > Hey all, Hi... > > I keep getting a connection error 111 connection refused. When i try > to connect to a server at a remote ip address. The keywords here: "connection refused" and "remote ip address". > > I am using linux on both computers. > > the socket server looks like this: > #!/usr/bin/python > import SocketServer > > class MyTCPHandler(SocketServer.BaseRequestHandler): > > def handle(self): > self.data = self.request.recv(1024).strip() > print "%s wrote:" % self.client_address[0] > print self.data > # just send back the same data, but upper-cased > self.request.send(self.data.upper()) > > if __name__ == "__main__": > HOST, PORT = "127.0.0.1", 3000 The server won't accept _any_ connections not coming from "127.0.0.1"! You want it to accept connection from _anywhere_ use: "0.0.0.0". ----8<---- > > it does work if i use localhost and run both server and client on the > same computer. As expected. > > any tips would be greatly appreciated, > thanks > > sk Hope that helps Ziyad. From mmadlavana at gmail.com Fri Dec 25 08:50:26 2009 From: mmadlavana at gmail.com (Mkhanyisi Madlavana) Date: Fri, 25 Dec 2009 07:50:26 +0000 Subject: [Tutor] Listing available variables Message-ID: <52d606870912242350x5ce6c5bco5fb342aabe9c5ed@mail.gmail.com> How do I list all the available variables in python. for example if I say: >>>a = range(10) >>>b = 16 >>>c = "" ... (some variables) >>>z = ["this","that","none"] I then need a command that will list the variables I assigned like: >>>some_command a, b, c, ... (some variables), z Regards Mkhanyisi From lie.1296 at gmail.com Fri Dec 25 09:16:36 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 25 Dec 2009 19:16:36 +1100 Subject: [Tutor] Listing available variables In-Reply-To: <52d606870912242350x5ce6c5bco5fb342aabe9c5ed@mail.gmail.com> References: <52d606870912242350x5ce6c5bco5fb342aabe9c5ed@mail.gmail.com> Message-ID: On 12/25/2009 6:50 PM, Mkhanyisi Madlavana wrote: > How do I list all the available variables in python. for example if I say: >>>> a = range(10) >>>> b = 16 >>>> c = "" > .... (some variables) >>>> z = ["this","that","none"] > I then need a command that will list the variables I assigned like: >>>> some_command > a, b, c, ... (some variables), z dir() >>> a = range(10) >>> b = 16 >>> c = "" >>> z = ["this","that","none"] >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'z'] From fomcl at yahoo.com Fri Dec 25 13:53:37 2009 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 25 Dec 2009 04:53:37 -0800 (PST) Subject: [Tutor] python and kiviat diagrams In-Reply-To: <1c2a2c590912240519k3751c57ahf6573f30c22793f7@mail.gmail.com> Message-ID: <598285.93865.qm@web110705.mail.gq1.yahoo.com> Hi all, Thanks for your insightful answers, and a merry Christmas + a happy new year! Btw, somebody on the list mentioned rpy as an alternative way to create spider charts. I also think that's a better route to take. R is thoroughly tested and built for the job, and if you ever need more statistical/plotting things, you could easily extend your program. Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Thu, 12/24/09, Kent Johnson wrote: From: Kent Johnson Subject: Re: [Tutor] python and kiviat diagrams To: "Albert-Jan Roskam" Cc: "dwbarne" , "python tutor" Date: Thursday, December 24, 2009, 2:19 PM On Wed, Dec 23, 2009 at 5:27 PM, Albert-Jan Roskam wrote: > > I was studying the code on http://matplotlib.sourceforge.net/examples/api/radar_chart.html. > Isn't it very unusual that a class is defined within a function? Unusual, but not un-heard of. Usually the function is a factory function that returns the class to the caller. In this case the class is registered as the handler for projections of type 'radar'. Presumably RadarAxes is implementing a protocol (interface) that is required by register_projection(). > Why not use a class instance inside the function instead? Presumably register_projection() requires a class (strictly speaking, it probably requires a callable that returns an instance of a class that implements the required protocol). There don't seem to be any docs for register_projection() so it's hard to know for sure what it wants. > No methods of the class can currently be inherited outside the scope of the function, right? Do you mean, the class cannot be subclassed? Not easily, only because the function doesn't return the class. There is no inherent reason this class could not be subclassed if a reference to the class were available. Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Fri Dec 25 13:57:28 2009 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 25 Dec 2009 04:57:28 -0800 (PST) Subject: [Tutor] Listing available variables In-Reply-To: Message-ID: <781551.7140.qm@web110713.mail.gq1.yahoo.com> This may also be useful: >>> a = 1 >>> b = 2 >>> c = 3 >>> locals() {'a': 1, 'c': 3, 'b': 2, '__builtins__': , '__name__': '__main__', '__doc__': None} >>> locals().keys() ['a', 'c', 'b', '__builtins__', '__name__', '__doc__'] >>> Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In the face of ambiguity, refuse the temptation to guess. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Fri, 12/25/09, Lie Ryan wrote: From: Lie Ryan Subject: Re: [Tutor] Listing available variables To: tutor at python.org Date: Friday, December 25, 2009, 9:16 AM On 12/25/2009 6:50 PM, Mkhanyisi Madlavana wrote: > How do I list all the available variables in python. for example if I say: >>>> a = range(10) >>>> b = 16 >>>> c = "" > .... (some variables) >>>> z = ["this","that","none"] > I then need a command that will list the variables I assigned like: >>>> some_command > a, b, c, ... (some variables), z dir() >>> a = range(10) >>> b = 16 >>> c = "" >>> z = ["this","that","none"] >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c', 'z'] _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdole1 at cogeco.ca Fri Dec 25 17:49:36 2009 From: rdole1 at cogeco.ca (rick) Date: Fri, 25 Dec 2009 11:49:36 -0500 Subject: [Tutor] interactive mode questions Message-ID: <1261759776.27204.12.camel@rick-desktop> I'm working my way through Mr Lutz's "Learning Python", and I come to this snippet (page 271): while True: reply = input('Enter text:') if reply == 'stop': break print(reply.upper()) which works "as advertised" in an interactive session, but not in a script. Yes, I tried tossing the script to both versions of interpreter, it doesn't work with either 2.6 or 3.1. text given as input results in NameError: name 'text' is not defined. numerical input results in AttributeError: 'int' object has no attribute 'upper' either way, enclosing the input in quotes works. From timomlists at gmail.com Fri Dec 25 18:17:06 2009 From: timomlists at gmail.com (Timo) Date: Fri, 25 Dec 2009 18:17:06 +0100 Subject: [Tutor] interactive mode questions In-Reply-To: <1261759776.27204.12.camel@rick-desktop> References: <1261759776.27204.12.camel@rick-desktop> Message-ID: <4B34F392.6010506@gmail.com> Op 25-12-09 17:49, rick schreef: > I'm working my way through Mr Lutz's "Learning Python", and I come to > this snippet (page 271): > > while True: > reply = input('Enter text:') > Try this: reply = raw_input('Enter text: ') Cheers, Timo > if reply == 'stop': break > print(reply.upper()) > > which works "as advertised" in an interactive session, but not in a > script. Yes, I tried tossing the script to both versions of > interpreter, it doesn't work with either 2.6 or 3.1. > > text given as input results in NameError: name 'text' is not defined. > numerical input results in AttributeError: 'int' object has no attribute > 'upper' > > either way, enclosing the input in quotes works. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From denis.spir at free.fr Fri Dec 25 18:39:03 2009 From: denis.spir at free.fr (spir) Date: Fri, 25 Dec 2009 18:39:03 +0100 Subject: [Tutor] unicode ordinals to/from utf8 Message-ID: <20091225183903.06ed2745@o> Special offer for coders coding on Christmas day! I'm looking for the simplest way to decode/encode unicode ordinals (called 'codes' below) to/from utf8. Find this rather tricky, esp because of variable number of meaningful bits in first octet. Specifically, for encoding, is there a way to avoid paasing through binary (and back)? Below what I have so far (test by converting to utf8 & back ;-). Denis ________________________________ la vita e estrany http://spir.wikidot.com/ ========================================= # coding: utf8 import sys ; end = sys.exit sizes_to_values = {2:192, 3:224, 4:240} def ordinalFromUtf8(s): n = len(s) byte0 = ord(s[0]) # case ASCII if n == 1: return byte0 # case else # get actual value for byte #0 value0 = byte0 - sizes_to_values[n] ordinal = value0 * 64**(n-1) # compute other bytes for i in range(1,n): byte = ord(s[i]) value = byte - 128 weight = 64**(n-i-1) ordinal = ordinal + (byte - 128) * 64**(n-i-1) return ordinal def ordinalToUtf8(o): # case ASCII if o < 128 : return chr(o) # case else # split into octets, # each holding '10' & 6 meaningful bits binary = bin(o)[2:] octets = list() while len(binary) > 6: octet = '10' + binary[-6:] octets.insert(0, octet) binary = binary[:-6] # first octet can have 3 to 5 free bits, # depending on overall length bit_count = 6 - len(octets) rest_bit_count = len(binary) if rest_bit_count > bit_count: octet = '10' + '0' * (6 - rest_bit_count) + binary octets.insert(0, octet) binary = binary[:-6] zero_count = 7 - len(octets) - len(binary) octet = '1' * (len(octets)+1) + '0' * zero_count + binary octets.insert(0, octet) # convert to ordinals --> chars --> string ordinals = [int(bits,2) for bits in octets] chars = [chr(o) for o in ordinals] return ''.join(chars) def test(): def ue(u): return unicode.encode(u, 'utf8') # ASCII, latin, BMP, >BMP chars = ['\n','\t',' ','A','a','~', ue(u'\u00a0'),'?','?','?','?','?','?', ue(u'\u0100'),'?',ue(u'\u1234'),ue(u'\uffff'), ue(u'\U00010000'),ue(u'\U000fffff')] for char in chars: o = ordinalFromUtf8(char) s = ordinalToUtf8(o) print char,repr(char), " -->", o,'=',hex(o), " -->", s,repr(s) test() From beachkid at insightbb.com Fri Dec 25 21:00:41 2009 From: beachkid at insightbb.com (Ken G.) Date: Fri, 25 Dec 2009 15:00:41 -0500 Subject: [Tutor] Error writing to file... Message-ID: <4B3519E9.1000504@insightbb.com> In writing the following program in creating random numbers and writing them to the file, I get an type error in writing to file. I have tried number = random(10,99), number = randint(10.99), and number = random.random(10,00) and still get various errors of writing to file. If I were to REM out the file.write(number) and file.write('\n') lines, the program run fine. Do I need to convert a numeric random number to a string number? TIA, Ken PROGRAM: import random print ("First Group - Write") file = open("Numbers.txt","w") for i in range(1,10): number = random.randint(10,99) print i, number file.write(number) file.write('\n') file.close() ERROR MESSAGE: Traceback (most recent call last): File "/home/ken/Python 262/random.py", line 8, in file.write(number) TypeError: argument 1 must be string or read-only character buffer, not int From beachkid at insightbb.com Fri Dec 25 21:09:58 2009 From: beachkid at insightbb.com (Ken G.) Date: Fri, 25 Dec 2009 15:09:58 -0500 Subject: [Tutor] Error writing to file... In-Reply-To: <200912252001.nBPK1EoW016244@tlgmlp01.teamlog.com> References: <4B3519E9.1000504@insightbb.com> <200912252001.nBPK1EoW016244@tlgmlp01.teamlog.com> Message-ID: <4B351C16.4090705@insightbb.com> Thinking it was a reply to me on Python Tutor, I translated the following into English for the board. Ken ? DIAGORN wrote: > Bonjour, > Je suis absente jusqu'au 03/01/10 inclus. > En cas d'urgence Soprane, contacter notre adresse g?n?rique > projet.soprane at teamlog.com. > Joyeuses f?tes de fin d'ann?e. Cordialement. > > Genevi?ve > Hello, I am leaves to THE 03/01/10 include. In case of urgency Soprane, contact our generic address projet.soprane at teamlog.com. Joyous end of year holidays. Cordially. Genevi?ve From beachkid at insightbb.com Fri Dec 25 21:33:58 2009 From: beachkid at insightbb.com (Ken G.) Date: Fri, 25 Dec 2009 15:33:58 -0500 Subject: [Tutor] Error writing to file...(SOLVED) In-Reply-To: References: <4B3519E9.1000504@insightbb.com> Message-ID: <4B3521B6.8030007@insightbb.com> Thanks! So a file will only take a numeric as a string? Lesson learned. Again, thanks. Ken Amit Sethi wrote: > It is as the Error says a type error , the function takes a string and > u are passing an int > if you put > file.write(str(number)) you will not get error .. > > From x7-g5W_rt at earthlink.net Fri Dec 25 21:38:37 2009 From: x7-g5W_rt at earthlink.net (GilJohnson) Date: Fri, 25 Dec 2009 20:38:37 +0000 (UTC) Subject: [Tutor] Error writing to file... References: <4B3519E9.1000504@insightbb.com> Message-ID: Ken G. insightbb.com> writes: > [...] > Do I need to convert a numeric random number to a string number? > [...] Yes, as long as you open the file as "w" you need to use "repr()" [repr(int)]. If you want to save the integer, you need to open a file as "wb", write binary. Check your documentation, both "wb" and "pickle" - I'm pretty new to Python, so I won't try to advise you on the "read-only character buffer" mentioned in the error message, but also look up "bytes" objects - they may work as long as your integer range is in range(255). Hope this helps, Gil From david at pythontoo.com Fri Dec 25 21:39:00 2009 From: david at pythontoo.com (David) Date: Fri, 25 Dec 2009 15:39:00 -0500 Subject: [Tutor] Error writing to file... In-Reply-To: <4B3519E9.1000504@insightbb.com> References: <4B3519E9.1000504@insightbb.com> Message-ID: <4B3522E4.9070901@pythontoo.com> On 12/25/09 15:00, Ken G. wrote: > In writing the following program in creating random numbers and writing > them to the file, I get an type error in writing to file. I have tried > number = random(10,99), number = randint(10.99), and number = > random.random(10,00) and still get various errors of writing to file. > If I were to REM out the file.write(number) and file.write('\n') lines, > the program run fine. Do I need to convert a numeric random number to a > string number? > > TIA, > > Ken > > PROGRAM: > > import random > print ("First Group - Write") > file = open("Numbers.txt","w") > for i in range(1,10): > number = random.randint(10,99) > print i, number > file.write(number) > file.write('\n') > file.close() > > ERROR MESSAGE: > > Traceback (most recent call last): > File "/home/ken/Python 262/random.py", line 8, in > file.write(number) > TypeError: argument 1 must be string or read-only character buffer, not int Hi Ken, From the docs if you are using 2.6 [snip] To write something other than a string, it needs to be converted to a string first: >>> value = ('the answer', 42) >>> s = str(value) >>> f.write(s) http://docs.python.org/tutorial/inputoutput.html -- David Abbott (dabbott) From alan.gauld at btinternet.com Fri Dec 25 23:46:40 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Dec 2009 22:46:40 -0000 Subject: [Tutor] interactive mode questions References: <1261759776.27204.12.camel@rick-desktop> Message-ID: "rick" wrote > while True: > reply = input('Enter text:') > if reply == 'stop': break > print(reply.upper()) > > which works "as advertised" in an interactive session, but not in a > script. Yes, I tried tossing the script to both versions of > interpreter, it doesn't work with either 2.6 or 3.1. Somehow you are picking up an older v2 interpreter. How are you running the script? >From within IDLE? Or from an OS command? If the latter try using the full path to the v3 Python interpreter. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Dec 25 23:52:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Dec 2009 22:52:21 -0000 Subject: [Tutor] Error writing to file... References: <4B3519E9.1000504@insightbb.com> Message-ID: "Ken G." wrote > the program run fine. Do I need to convert a numeric random number to a > string number? You need to convert your data to a string if you use a text file, which is the default. If you open the file with 'wb' you can write any kind of data to it, but you will have to decode it when you read it back. Its usually easier to use a text file and convert to a string when writing. > import random > print ("First Group - Write") > file = open("Numbers.txt","w") The fact you use 'w' here tells Python you want to write text. > for i in range(1,10): > number = random.randint(10,99) > print i, number > file.write(number) > file.write('\n') > file.close() > > ERROR MESSAGE: > > Traceback (most recent call last): > File "/home/ken/Python 262/random.py", line 8, in > file.write(number) > TypeError: argument 1 must be string or read-only character buffer, not > int But, as the error says, you are passing an int. If you really want to use a binary file see the File Handling topic in my tutor for an example. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bkjones at gmail.com Sat Dec 26 03:03:10 2009 From: bkjones at gmail.com (Brian Jones) Date: Fri, 25 Dec 2009 21:03:10 -0500 Subject: [Tutor] oo design/interaction quandary Message-ID: <6e5927ff0912251803l358c3656ue208e5c202e746dd@mail.gmail.com> Hi all, I'm having a design issue that's really bothering me. The code I'm writing is fairly large by now, but I've written what I think is a decent example that illustrates my problem. My app launches threads that each consume messages from a queue, send them to a processor object, and then the processor needs to return the message to the main thread -- if the processing was successful -- and then the main thread puts the message into a different queue. Here's a shortened version of my code. class Processor(object): def __init__(self): self.qhost = 'localhost' self.qport = '5672' self.uname = 'guest' self.pwd = 'guest' self.ssl = false self.vhost = '/' self.exch_name = 'fooX' self.exch_type = 'direct' self.queue_name = 'fooQ' self.conn = amqp.Connection(userid=self.uname, password=self.pwd, host=self.qhost, virtual_host=self.vhost, ssl=self.ssl) self.chan = self.conn.channel() self.chan.exchange_declare(self.exch_name, type=self.exch_type) self.chan.queue_declare(self.qname) self.chan.queue_bind(self.qname, self.exch_name) def consume(self, callback): self.chan.basic_consume(self.qname, callback=callback) while True: self.chan.wait() class Munger(object): def munge(self,msg): if msg % 2 == 0: yield msg class Sender(object): def run(self): p = Processor() m = Munger() for msg in p.consume(m.munge): """ I know this doesn't work right now. This piece of the code should send 'msg' to another queue. """ pass if __name__ == '__main__': s = Sender() s.run() The problem might be obvious to you, but I'll quickly explain: The Sender object (a thread in the real code), is calling p.consume, which just wraps the basic_consume method in py-amqplib. The basic_consume method registers a consumer and a callback with the amqp server. The chan.wait() call blocks and waits for messages. When the messages arrive they are passed to the python callable we passed to the basic_consume method. Therein lies the problem. Messages go from my Processor object, to the Munger object, and this is all initiated by the Sender object, but I can't find a clean way to get messages successfully processed by Munger back to Sender, so that Sender can requeue the message to the new queue. I've thought about having a Queue object in Sender, or maybe registering Sender as an observer of (at different times) the Munger or Processor objects, but I'd like this to be as simple and understandable as possible by other developers, because I'm wanting to make the Processor and Munger objects pluggable (so the Processor can support different queuing protocols, Munger can do literally anything... etc). Ideas welcome. If I've failed to explain some aspect of my issue, give me a poke and I'll expand as best I can. brian Brian K. Jones Python Magazine http://www.pythonmagazine.com My Blog http://www.protocolostomy.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sat Dec 26 03:24:55 2009 From: bgailer at gmail.com (bob gailer) Date: Fri, 25 Dec 2009 21:24:55 -0500 Subject: [Tutor] Error writing to file... In-Reply-To: References: <4B3519E9.1000504@insightbb.com> Message-ID: <4B3573F7.5010408@gmail.com> The docs say "write( str) - Write a string to the file." Only a string. Regardless of mode. Mode mostly affects interpretation of line ends. -- Bob Gailer Chapel Hill NC 919-636-4239 From kent37 at tds.net Sat Dec 26 04:24:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 25 Dec 2009 22:24:42 -0500 Subject: [Tutor] oo design/interaction quandary In-Reply-To: <6e5927ff0912251803l358c3656ue208e5c202e746dd@mail.gmail.com> References: <6e5927ff0912251803l358c3656ue208e5c202e746dd@mail.gmail.com> Message-ID: <1c2a2c590912251924l22ba9cd5pddaf0eec7df9bc8d@mail.gmail.com> On Fri, Dec 25, 2009 at 9:03 PM, Brian Jones wrote: > Hi all, > I'm having a design issue that's really bothering me. The code I'm writing > is fairly large by now, but I've written what I think is a decent example > that illustrates my problem. > My app launches threads that each consume messages from a queue, send them > to a processor object, and then the processor needs to return the message to > the main thread -- if the processing was successful -- and then the main > thread puts the message into a different queue. > Here's a shortened version of my code. > > class Processor(object): > def __init__(self): > self.qhost = 'localhost' > self.qport = '5672' > self.uname = 'guest' > self.pwd = 'guest' > self.ssl = false > self.vhost = '/' > self.exch_name = 'fooX' > self.exch_type = 'direct' > self.queue_name = 'fooQ' > self.conn = amqp.Connection(userid=self.uname, password=self.pwd, > host=self.qhost, > virtual_host=self.vhost, ssl=self.ssl) > self.chan = self.conn.channel() > self.chan.exchange_declare(self.exch_name, type=self.exch_type) > self.chan.queue_declare(self.qname) > self.chan.queue_bind(self.qname, self.exch_name) > > def consume(self, callback): > self.chan.basic_consume(self.qname, callback=callback) > while True: > self.chan.wait() > > > class Munger(object): > def munge(self,msg): > if msg % 2 == 0: > yield msg Why is this a generator function? From what I understand looking at channel.py in py-amqplib this should just be a callable that processes the message. If you want to notify the Sender that the message has been processed, I would either make another Queue to pass messages back, or give the Munger a callback function to pass successful messages to. The difference between them is that a Queue is asynchronous and you would have to process the output in another thread; a callback is synchronous and will run in the same thread as the munger. Either of these can be handled in a base class leaving subclasses to just implement the message handling. For example, using a callback (a Queue would be similar): class MungerBase(object): def __init__(self, callback): self.callback = callback def munge(self, msg): if self.process(msg): self.callback(msg) A real munger looks like this: class Munger(MungerBase): def process(self, msg): if msg % 2 == 0: return True return False > class Sender(object): > def run(self): > p = Processor() > m = Munger() > for msg in p.consume(m.munge): > """ > I know this doesn't work right now. This > piece of the code should send 'msg' to another queue. > > """ > pass This would look like class Sender(object): def run(self): p = Processor() m = Munger(self.handle_processed_message) p.consume(m.munge) def handle_processed_message(self, msg): # send 'msg' to another queue or whatever Kent From rdole1 at cogeco.ca Sat Dec 26 05:45:52 2009 From: rdole1 at cogeco.ca (rick) Date: Fri, 25 Dec 2009 23:45:52 -0500 Subject: [Tutor] interactive questions In-Reply-To: References: Message-ID: <1261802752.28133.23.camel@rick-desktop> On Sat, 2009-12-26 at 03:03 +0100, tutor-request at python.org wrote: > > which works "as advertised" in an interactive session, but not in a > > script. Yes, I tried tossing the script to both versions of > > interpreter, it doesn't work with either 2.6 or 3.1. > > Somehow you are picking up an older v2 interpreter. > How are you running the script? > >From within IDLE? Or from an OS command? > > If the latter try using the full path to the v3 Python interpreter. > > HTH, > > Hi Alan, from within geany. The first line is either #!/usr/bin/python or #!/usr/bin/python3 rick at rick-desktop:~$ file /usr/bin/python /usr/bin/python: symbolic link to `python2.6' rick at rick-desktop:~$ file /usr/bin/python3 /usr/bin/python3: symbolic link to `python3.1' I just think it odd that there is one behaviour in an interactive session, and a different behaviour when run from a script. interesting again, running it right from bash, it works just fine, as long as the first line calls v3.1. Could geany be calling the 2.6 interpreter, even with v3 in the first line? oh my, that is unexpected, just tried it. #!/usr/bin/python3 import sys print(sys.version) from geany yields: 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [GCC 4.4.1] but from bash yields: 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) [GCC 4.4.1] Rick From alan.gauld at btinternet.com Sat Dec 26 12:25:12 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 26 Dec 2009 11:25:12 +0000 (GMT) Subject: [Tutor] Error writing to file... In-Reply-To: <4B3573F7.5010408@gmail.com> References: <4B3519E9.1000504@insightbb.com> <4B3573F7.5010408@gmail.com> Message-ID: <876087.13236.qm@web86701.mail.ird.yahoo.com> > The docs say > > "write( str) - Write a string to the file." > > Only a string. Regardless of mode. > Mode mostly affects interpretation of line ends. Thats true of course, you need to encode the binary data with struct to write it as binary. Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Sat Dec 26 16:41:07 2009 From: denis.spir at free.fr (spir) Date: Sat, 26 Dec 2009 16:41:07 +0100 Subject: [Tutor] unicode ordinals to/from utf8 In-Reply-To: <20091225183903.06ed2745@o> References: <20091225183903.06ed2745@o> Message-ID: <20091226164107.1de9b467@o> OK, I'll answer myself ;-) Found needed information at http://www1.tip.nl/~t876506/utf8tbl.html See below new version, Denis ________________________________ la vita e estrany http://spir.wikidot.com/ ============================= # coding: utf8 import sys ; end = sys.exit # constant max_code = 1114111 # U+00101111 def toUTF8(code): ''' UTF-8 single character octet string, from unicode code ''' # case 1 octet: ASCII if code<128 : o1 = code octets = (o1,) # case 2 octets elif code < 2048: o1 = code // 64 + 192 o2 = code % 64 + 128 octets = (o1,o2) # case 3 octets elif code < 65536: o1 = code // 4096 + 224 o2 = (code//64) % 64 + 128 o3 = code % 64 + 128 octets = (o1,o2,o3) # case 4 octets elif code > 65535 and code <= max_code: o1 = code // 262144 + 240 o2 = (code//4096) % 64 + 128 o3 = (code//64) % 64 + 128 o4 = code % 64 + 128 octets = (o1,o2,o3,o4) # case error else: message = "Invalid unicode code: %s" %code raise ValueError(message) # octet string return ''.join(chr(o) for o in octets) def fromUTF8(string): ''' unicode code, from UTF-8 single character octet string ''' octets = [ord(o) for o in string] o1 = octets[0] # case o1 = 0xxxxxxx --> 1 octet: ASCII if o1<128: return o1 # case o1 = 110xxxxx --> 2 octets elif o1>192 and o1<224: o2 = octets[1] return (o1-192)*64 + (o2-128) # case o1 = 1110xxxx --> 3 octets elif o1>223 and o1<240: o2,o3 = octets[1:] return (o1-224)*4096 + (o2-128)*64 + (o3-128) # case o1 = 11110xxx --> 4 octets elif o1>239 and o1<248: o2,o3,o4 = octets[1:] return (o1-240)*262144 + (o2-128)*4096 + (o3-128)*64 + (o4-128) # case error else: decseq = " ".join(str(v) for v in octets) hexseq = " ".join(hex(v)[2:] for v in octets) message = "Invalid UTF-8 sequence: %u [%s] = [%s] (hex)." \ %(ord(string),decseq,hexseq) raise ValueError(message) # def test(): # ASCII, latin, BMP, >BMP codes = [ 9,10,32,57,65,97,126,127, 160,233,255, 256,2048,65535, 65536,max_code, ] for c1 in codes: u = toUTF8(c1) c2 = fromUTF8(u) print c1,"\t --> ",u,"\t --> ",c2 test() From norman at khine.net Sat Dec 26 16:46:18 2009 From: norman at khine.net (Norman Khine) Date: Sat, 26 Dec 2009 16:46:18 +0100 Subject: [Tutor] using mechanize to authenticate and pull data out of site Message-ID: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> Hello, I am trying to authenticate on http://commerce.sage.com/Solidarmonde/ using urllib but have a problem in that there are some hidden fields that use javascript to create a security token which then is passed to the submit button and to the header. Here is the output of the LiveHeader during authentication http://paste.lisp.org/display/92656 Here is what I have so far: http://paste.lisp.org/+1ZHS/1 >>> print results But the page returned prints out that the session is out of time. Here are details of the forms: http://paste.lisp.org/+1ZHS/2 Any help much appreciated. Norman From alan.gauld at btinternet.com Sat Dec 26 17:06:04 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 26 Dec 2009 16:06:04 -0000 Subject: [Tutor] oo design/interaction quandary References: <6e5927ff0912251803l358c3656ue208e5c202e746dd@mail.gmail.com> Message-ID: "Brian Jones" wrote > My app launches threads that each consume messages from a queue, send > them > to a processor object, and then the processor needs to return the message > to > the main thread -- if the processing was successful -- and then the main > thread puts the message into a different queue. Why don't the messages munge and process themselves? What is the purpose of the processor and munger classes? Aren't they just functions and therefore should e metjods of the thing upon which they operate - ie Message? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Dec 26 17:11:02 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 26 Dec 2009 16:11:02 -0000 Subject: [Tutor] interactive questions References: <1261802752.28133.23.camel@rick-desktop> Message-ID: "rick" wrote >> Somehow you are picking up an older v2 interpreter. >> How are you running the script? > Could geany be calling the 2.6 interpreter, even with v3 in the first > line? oh my, that is unexpected, just tried it. > > #!/usr/bin/python3 > import sys > print(sys.version) > > from geany yields: > 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) Very few IDEs (none that I know of!) honour the shebang line, that is usually only applicable to the shell. You need to change the interpreter setup from within the IDE to change interpreter. Another reason you should *always* test your scripts from the OS level before assuming they work, rather than relying on the IDE. Alan G. From david at pythontoo.com Sat Dec 26 23:01:08 2009 From: david at pythontoo.com (David Abbott) Date: Sat, 26 Dec 2009 17:01:08 -0500 Subject: [Tutor] using mechanize to authenticate and pull data out of site In-Reply-To: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> References: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> Message-ID: <1261864868.1789.17.camel@opteron.dwabbott.com> On Sat, 2009-12-26 at 16:46 +0100, Norman Khine wrote: > Hello, > > I am trying to authenticate on http://commerce.sage.com/Solidarmonde/ > using urllib but have a problem in that there are some hidden fields > that use javascript to create a security token which then is passed to > the submit button and to the header. I used pycurl to login and post to a drupal site; http://code.google.com/p/gentoo-community-portal/source/browse/trunk/gentooligans.py I had to go to the page and store the token then use that to login or enter data. I am new to programming so I am sure I make some mistakes but it may help. Go down to line 374 I store the token and then use that later to enter data. From kent37 at tds.net Sat Dec 26 23:31:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 26 Dec 2009 17:31:13 -0500 Subject: [Tutor] unicode ordinals to/from utf8 In-Reply-To: <20091226164107.1de9b467@o> References: <20091225183903.06ed2745@o> <20091226164107.1de9b467@o> Message-ID: <1c2a2c590912261431o3b73dd09x63eed8164f8edc66@mail.gmail.com> On Sat, Dec 26, 2009 at 10:41 AM, spir wrote: > OK, I'll answer myself ;-) > Found needed information at http://www1.tip.nl/~t876506/utf8tbl.html > See below new version, I'm not at all sure what you are trying to do here. Is it more than conversion between unicode and utf-8? It looks like you have written your own utf-8 codec. Why don't you use str.decode() and unicode.encode()? Kent From modulok at gmail.com Sun Dec 27 09:36:38 2009 From: modulok at gmail.com (Modulok) Date: Sun, 27 Dec 2009 01:36:38 -0700 Subject: [Tutor] How to create memory backed file? Message-ID: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> List, How do I create a file which exists only in memory? (Think diskless.) I need to create an in-memory file. I need to pass this file, as a regular file object, to a subprocess. Unfortunately, the 'mmap' module's file-like object doesn't appear to work quite like a regular file would. (I simply assume that mmap would be the way to go. Other suggestions welcome!) That, or I'm doing something terribly wrong. For example: import mmap import sys from subprocess import Popen, PIPE fishes = "one fish\ntwo fish\nred fish\nblue fish\n" ####################################### # With a real file, this works: ####################################### fd = open('fish.txt', 'w') fd.write(fishes) fd = open('fish.txt', 'r') proc1 = Popen(['cat'], stdin=fd, stdout=sys.stdout, stderr=sys.stderr) ####################################### # This doesn't (with mmap file): ####################################### vfd = mmap.mmap(-1, len(fishes), mmap.MAP_PRIVATE) vfd.write(fishes) vfd.seek(0) proc2 = Popen(['cat'], stdin=vfd.fileno(), stdout=sys.stdout, stderr=sys.stderr) I just get the following error: Traceback (most recent call last): File "", line 1, in AttributeError: fileno I'm using Python 2.5, so I cannot use that slick 'SpooledTemporaryFile' method of the 'tempfile' module. The 'cat' is just a simple Unix system command for the purpose of illustration. Thanks! -Modulok- From alan.gauld at btinternet.com Sun Dec 27 21:03:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 27 Dec 2009 20:03:03 -0000 Subject: [Tutor] How to create memory backed file? References: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> Message-ID: "Modulok" wrote > How do I create a file which exists only in memory? (Think diskless.) The nearest I can think of is probably StringIO Take a look at the docs for that and see if it works for you... Alan G. From emile at fenx.com Sun Dec 27 22:41:26 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 27 Dec 2009 13:41:26 -0800 Subject: [Tutor] How to create memory backed file? In-Reply-To: References: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> Message-ID: On 12/27/2009 12:03 PM Alan Gauld said... > > "Modulok" wrote > >> How do I create a file which exists only in memory? (Think diskless.) > > The nearest I can think of is probably StringIO > > Take a look at the docs for that and see if it works for you... > There's also http://docs.python.org/library/mmap.html -- but I've never tried it. Emile From kent37 at tds.net Mon Dec 28 03:07:50 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 27 Dec 2009 21:07:50 -0500 Subject: [Tutor] How to create memory backed file? In-Reply-To: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> References: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> Message-ID: <1c2a2c590912271807t24e87949n7a88ef666dae736@mail.gmail.com> On Sun, Dec 27, 2009 at 3:36 AM, Modulok wrote: > List, > > How do I create a file which exists only in memory? (Think diskless.) > > I need to create an in-memory file. I need to pass this file, as a > regular file object, to a subprocess. Unfortunately, the 'mmap' > module's file-like object doesn't appear to work quite like a regular > file would. (I simply assume that mmap would be the way to go. Other > suggestions welcome!) That, or I'm doing something terribly wrong. How about using a pipe for stdin? This seems to work: import sys from subprocess import Popen, PIPE fishes = "one fish\ntwo fish\nred fish\nblue fish\n" proc1 = Popen(['cat'], stdin=PIPE, stdout=sys.stdout, stderr=sys.stderr) proc1.stdin.write(fishes) proc1.stdin.close() Kent From modulok at gmail.com Mon Dec 28 04:57:14 2009 From: modulok at gmail.com (Modulok) Date: Sun, 27 Dec 2009 20:57:14 -0700 Subject: [Tutor] How to create memory backed file? In-Reply-To: <1c2a2c590912271807t24e87949n7a88ef666dae736@mail.gmail.com> References: <64c038660912270036r4a4d80baobcc5980f39f4a2a2@mail.gmail.com> <1c2a2c590912271807t24e87949n7a88ef666dae736@mail.gmail.com> Message-ID: <64c038660912271957n2a948fb6pe2a16ba415294a54@mail.gmail.com> Kent, Thanks! I think that'll do it. I don't know what this list would do without you! -Modulok- On 12/27/09, Kent Johnson wrote: > On Sun, Dec 27, 2009 at 3:36 AM, Modulok wrote: >> List, >> >> How do I create a file which exists only in memory? (Think diskless.) >> >> I need to create an in-memory file. I need to pass this file, as a >> regular file object, to a subprocess. Unfortunately, the 'mmap' >> module's file-like object doesn't appear to work quite like a regular >> file would. (I simply assume that mmap would be the way to go. Other >> suggestions welcome!) That, or I'm doing something terribly wrong. > > How about using a pipe for stdin? This seems to work: > > import sys > from subprocess import Popen, PIPE > > fishes = "one fish\ntwo fish\nred fish\nblue fish\n" > > proc1 = Popen(['cat'], stdin=PIPE, stdout=sys.stdout, stderr=sys.stderr) > proc1.stdin.write(fishes) > proc1.stdin.close() > > Kent > From roadierich at googlemail.com Mon Dec 28 10:21:01 2009 From: roadierich at googlemail.com (Rich Lovely) Date: Mon, 28 Dec 2009 09:21:01 +0000 Subject: [Tutor] using mechanize to authenticate and pull data out of site In-Reply-To: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> References: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> Message-ID: 2009/12/26 Norman Khine : > Hello, > > I am trying to authenticate on http://commerce.sage.com/Solidarmonde/ > using urllib but have a problem in that there are some hidden fields > that use javascript to create a security token which then is passed to > the submit button and to the header. > > Here is the output of the LiveHeader during authentication > > http://paste.lisp.org/display/92656 > > Here is what I have so far: > > http://paste.lisp.org/+1ZHS/1 > >>>> print results > But the page returned prints out that the session is out of time. > > Here are details of the forms: > > http://paste.lisp.org/+1ZHS/2 > > Any help much appreciated. > > Norman > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The first thing to try is to attempt to login with javascript disabled. If it will let you do that, transfer the relevant form info to the mechanize browser, and it should be fine. If not, you will need to look through all of the javascript files, to find out which one generates/receives the security token. Looking at it, the element will be called "_xmlToken". The "xml" suggests that it might be received over ajax, which means you will need to find the page that it comes from, and fake an ajax request to it - fortunately, this is just a simple http request, much like you are already doing - it's just handled under the surface by javascript. -- Rich "Roadie Rich" Lovely There are 10 types of people in the world: those who know binary, those who do not, and those who are off by one. From norman at khine.net Mon Dec 28 22:17:22 2009 From: norman at khine.net (Norman Khine) Date: Mon, 28 Dec 2009 22:17:22 +0100 Subject: [Tutor] using mechanize to authenticate and pull data out of site In-Reply-To: References: <9c2c8ffb0912260746i1e971c91n879283ef60e9a9b5@mail.gmail.com> Message-ID: <9c2c8ffb0912281317o1828cd6ag471df6ae927487@mail.gmail.com> hello, thank you all for the replies. On Mon, Dec 28, 2009 at 10:21 AM, Rich Lovely wrote: > 2009/12/26 Norman Khine : >> Hello, >> >> I am trying to authenticate on http://commerce.sage.com/Solidarmonde/ >> using urllib but have a problem in that there are some hidden fields >> that use javascript to create a security token which then is passed to >> the submit button and to the header. >> >> Here is the output of the LiveHeader during authentication >> >> http://paste.lisp.org/display/92656 >> >> Here is what I have so far: >> >> http://paste.lisp.org/+1ZHS/1 >> >>>>> print results >> But the page returned prints out that the session is out of time. >> >> Here are details of the forms: >> >> http://paste.lisp.org/+1ZHS/2 >> >> Any help much appreciated. >> >> Norman >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > The first thing to try is to attempt to login with javascript > disabled. ?If it will let you do that, transfer the relevant form info > to the mechanize browser, and it should be fine. It does not work, i need javascript enabled in order to login. > > If not, you will need to look through all of the javascript files, to > find out which one generates/receives the security token. ?Looking at > it, the element will be called "_xmlToken". Looking at the javascript - http://paste.lisp.org/+1ZHS/4 the 'function browser_localForm_form_onsubmit' has contextKey that is passed to it. i think the verification between the two tokens comes: securityToken = _browser.getElement("_xmlToken"); document.localForm.__sgx_contextSecurity.value = securityToken.value; also there seems to be a lot of hash keys being generated at the begining of the javascripts, here are some examples: http://paste.lisp.org/+1ZHS/3 > > The "xml" suggests that it might be received over ajax, which means > you will need to find the page that it comes from, and fake an ajax > request to it - fortunately, this is just a simple http request, much > like you are already doing - it's just handled under the surface by > javascript. how would i fake the ajax before i submit the form everything seems to come form this page /solidarmonde/defaultsgx.asp thanks > > -- > Rich "Roadie Rich" Lovely > > There are 10 types of people in the world: those who know binary, > those who do not, and those who are off by one. > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From abdulhafid at gmail.com Tue Dec 29 00:13:03 2009 From: abdulhafid at gmail.com (Abdulhafid Igor Ryabchuk) Date: Tue, 29 Dec 2009 01:13:03 +0200 Subject: [Tutor] Python and Computational Geometry Message-ID: <787e86df0912281513n55cacaa3v5b65e122d6d653dd@mail.gmail.com> Dear Pythonistas, I am starting a small project that centres around implementation of computational geometry algorithms. I was wondering if there are any particular Python modules I should have a look at. Regards, AH From kent37 at tds.net Tue Dec 29 00:38:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 28 Dec 2009 18:38:43 -0500 Subject: [Tutor] Python and Computational Geometry In-Reply-To: <787e86df0912281513n55cacaa3v5b65e122d6d653dd@mail.gmail.com> References: <787e86df0912281513n55cacaa3v5b65e122d6d653dd@mail.gmail.com> Message-ID: <1c2a2c590912281538r4eb24c5cub7514f735aa94554@mail.gmail.com> Googling "python computational geometry" points to http://www.cgal.org/ and http://cgal-python.gforge.inria.fr/ Kent On Mon, Dec 28, 2009 at 6:13 PM, Abdulhafid Igor Ryabchuk wrote: > Dear Pythonistas, > > I am starting a small project that centres around implementation of > computational geometry algorithms. I was wondering if there are any > particular Python modules I should have a look at. > > Regards, > > AH > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From hultgren1946 at yahoo.com Tue Dec 29 03:19:37 2009 From: hultgren1946 at yahoo.com (Richard Hultgren) Date: Mon, 28 Dec 2009 18:19:37 -0800 (PST) Subject: [Tutor] computer basics Message-ID: <131904.28135.qm@web113213.mail.gq1.yahoo.com> I am learning Python slowly.? I would like to begin learning all about how computers work from the bottom up.? I have an understanding of binary code.? Where should I go from here; can you suggest continued reading, on line or off to continue my education? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Thank You ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Richard -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Tue Dec 29 08:33:29 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Tue, 29 Dec 2009 13:03:29 +0530 Subject: [Tutor] extracting informations (images and text) from a PDF and creating a database from it Message-ID: I need to make a database from some PDFs. I need to extract logos as well as the information (i.e. name,address) beneath the logo and fill it up in database. The logo can be text as well as picture as shown in two of the screenshots of one of the sample PDF file: http://imagebin.org/77378 http://imagebin.org/77379 Will converting to html a good option? Later on I need to apply some image processing too. What should be the ideal way towards it ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 29 10:23:40 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Dec 2009 09:23:40 -0000 Subject: [Tutor] computer basics References: <131904.28135.qm@web113213.mail.gq1.yahoo.com> Message-ID: "Richard Hultgren" wrote > I am learning Python slowly. I would like to begin learning all about > how computers work from the bottom up. I have an understanding > of binary code. Where should I go from here; Wikipedia is your friend most of the time. The next stage is probably to look at logic gates and combinatorial logic and boolean algebra. Look at AND, OR and NOT gates, NOR and NAND gates and HALF-ADDER and ADDER arrangements. Next you look at REGISTERS and maybe LATCHES and read a little about the different kinds of Memory. Once you've done that you've got the basic building blocks of the hardware (at the same level as binary is for software). The next level from that is harder to determine because you could go in any of several directions. Probably the BIOS and OS areas. The good news is you can use Python to explore most of these topics by building simple functions that emulate the hardware operations. Have fun, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Dec 29 10:29:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Dec 2009 09:29:16 -0000 Subject: [Tutor] extracting informations (images and text) from a PDF andcreating a database from it References: Message-ID: "Shashwat Anand" wrote >I need to make a database from some PDFs. I need to extract logos as well >as > the information (i.e. name,address) beneath the logo and fill it up in > database. The logo can be text as well as picture as shown in two of the > screenshots of one of the sample PDF file: > http://imagebin.org/77378 > http://imagebin.org/77379 You could try PDFMiner to extract direct from the PDF using Python. > Will converting to html a good option? Later on I need to apply some > image > processing too. What should be the ideal way towards it ? Converting to html (assuming you have a tool to do that!) may be better since there are a wider choice of tools and more experience to help you. Or there are various commercial tools for converting PDF into Word etc. I've never personally had to extract data from a PDF, I've always had access to the source documents so I can't comment on how effective each approach is... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From anand.shashwat at gmail.com Tue Dec 29 10:51:57 2009 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Tue, 29 Dec 2009 15:21:57 +0530 Subject: [Tutor] extracting informations (images and text) from a PDF andcreating a database from it In-Reply-To: References: Message-ID: I used PDFMiner and I was pretty satisfied with the text portions. I retrieved all the text and was able to manipulate it according to my wish. However I failed on Image part. So Technically my question reduces to 'If there a PDF document and some verbose text below them and the pattern is followed i.e. per page of PDF there will be one image and some texts following it, how can I retrieve both the images and the text without loss' ? On Tue, Dec 29, 2009 at 2:59 PM, Alan Gauld wrote: > "Shashwat Anand" wrote > > > I need to make a database from some PDFs. I need to extract logos as well >> as >> the information (i.e. name,address) beneath the logo and fill it up in >> database. The logo can be text as well as picture as shown in two of the >> screenshots of one of the sample PDF file: >> http://imagebin.org/77378 >> http://imagebin.org/77379 >> > > You could try PDFMiner to extract direct from the PDF using Python. > > > Will converting to html a good option? Later on I need to apply some >> image >> processing too. What should be the ideal way towards it ? >> > > Converting to html (assuming you have a tool to do that!) may be better > since there are a wider choice of tools and more experience to help you. > Or there are various commercial tools for converting PDF into Word etc. > > I've never personally had to extract data from a PDF, I've always had > access > to the source documents so I can't comment on how effective each approach > is... > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at freenet.co.uk Tue Dec 29 12:06:35 2009 From: alan.gauld at freenet.co.uk (Alan Gauld) Date: Tue, 29 Dec 2009 11:06:35 -0000 Subject: [Tutor] My tutorial update Message-ID: I've been doing some updates to my web tutor over the holidays and there is now a new "home page" that has links to both v2 and v3 tutorials. This will now be the common source of all news updates etc and the intro page of each tutor will now be a lot cleaner and easier to access the real content. The v3 is still under construction but is at last in what I consider to be a usable state. Its still not pretty but it seems to work. If anyone finds problems with it please let me know. Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sanelson at gmail.com Tue Dec 29 16:11:42 2009 From: sanelson at gmail.com (Stephen Nelson-Smith) Date: Tue, 29 Dec 2009 15:11:42 +0000 Subject: [Tutor] Modules and Test Suites Message-ID: I do quite a lot of programming in Ruby. When I do so, my code tends to have the following layout: /path/to/src/my_project Inside my_project: lib/ test/ my_project.rb my_project.rb uses classes and helper methods in lib Inside test, I have a test suite that also uses classes and helper methods in ../lib This seems like a sensible way to keep tests and other code separate. In Python I don't know how to do this.... so I just have all my tests in the same place as the rest of the code. a) Is my above way a sensible and pythonic approach? b) If so - how can I do it in Python? c) If not, is there a better way than having all the tests in the same place as the rest of the code? S. -- Stephen Nelson-Smith Technical Director Atalanta Systems Ltd www.atalanta-systems.com From eike.welk at gmx.net Tue Dec 29 16:47:55 2009 From: eike.welk at gmx.net (Eike Welk) Date: Tue, 29 Dec 2009 16:47:55 +0100 Subject: [Tutor] Modules and Test Suites In-Reply-To: References: Message-ID: <200912291647.56404.eike.welk@gmx.net> Hello Stephen! On Tuesday 29 December 2009, Stephen Nelson-Smith wrote: > I do quite a lot of programming in Ruby. When I do so, my code > tends to have the following layout: > > /path/to/src/my_project > > Inside my_project: > > lib/ > test/ > my_project.rb > > b) If so - how can I do it in Python? I think the test discovery programs "py.test" and "nose" do what you want. If I understand you correctly you want something that sets the right search path for library modules. http://codespeak.net/py/dist/test/ http://somethingaboutorange.com/mrl/projects/nose/0.11.1/ You could also tweak the environment variable "$PYTHONPATH" or the variable "sys.path". See section 6.1.2: http://docs.python.org/tutorial/modules.html I use "py.test" and like it. But I keep tests and libraries in the same directory. I know however that "py.test" also supports separate directories for tests. Eike. From x7-g5W_rt at earthlink.net Tue Dec 29 18:39:53 2009 From: x7-g5W_rt at earthlink.net (GilJohnson) Date: Tue, 29 Dec 2009 17:39:53 +0000 (UTC) Subject: [Tutor] computer basics References: <131904.28135.qm@web113213.mail.gq1.yahoo.com> Message-ID: Richard Hultgren yahoo.com> writes: > I am learning Python slowly.? I would like to begin learning all about how > computers work from the bottom up.? I have an understanding of binary code. > Where should I go from here; can you suggest continued reading, on line or > off to continue my education? Richard, I learned a lot from Randall Hyde's book: Write Great Code: Volume 1: Understanding the Machine Also, take a look at his website: http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/ There is a lot of information from "Understanding the Machine" in "The Art of Assembly" The online version is not the final, published version, he notes that the hardcopy has been through the proofing process. Either or both books would give you a lot of information, I'd make the choice based on your interest in assembly. Hope this helps, Gil From grigor.kolev at gmail.com Tue Dec 29 21:47:38 2009 From: grigor.kolev at gmail.com (Grigor Kolev) Date: Tue, 29 Dec 2009 22:47:38 +0200 Subject: [Tutor] try and except Message-ID: <1262119658.23352.6.camel@dedal-laptop> Hi. Can someone explain me where is my mistake. ----------------------------------------------- class Error(Exception): def printer(self , value ): self.value = value print self.value def oops(): raise Error def domoed(): try: oops() except Error: Test = Error() print 'Error: ', Test.printer('Test') else: print 'no error' if __name__ == "__main__": domoed() ------------------------------------------------------ Result is: Error: Test None >From where come it None -- Grigor Kolev From hugo.yoshi at gmail.com Tue Dec 29 22:17:28 2009 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 29 Dec 2009 22:17:28 +0100 Subject: [Tutor] try and except In-Reply-To: <1262119658.23352.6.camel@dedal-laptop> References: <1262119658.23352.6.camel@dedal-laptop> Message-ID: <29179d160912291317m3cf4629fye9004c4e0caab81c@mail.gmail.com> On Tue, Dec 29, 2009 at 9:47 PM, Grigor Kolev wrote: > Hi. > Can someone explain me where is my mistake. > ----------------------------------------------- > class Error(Exception): > ? ? ? ?def printer(self , value ): > ? ? ? ? ? ? ? ?self.value = value > ? ? ? ? ? ? ? ?print self.value > def oops(): > ? ? ? ?raise Error > def domoed(): > ? ? ? ?try: > ? ? ? ? ? ? ? ?oops() > ? ? ? ?except Error: > ? ? ? ? ? ? ? ?Test = Error() > ? ? ? ? ? ? ? ?print 'Error: ', Test.printer('Test') > ? ? ? ?else: > ? ? ? ? ? ? ? ?print 'no error' > if __name__ == "__main__": > ? ? ? ?domoed() > ------------------------------------------------------ > Result is: > Error: ?Test > None > >From where come it None The line "print 'Error: ', Test.printer('Test')" is where it goes wrong. You should follow closely the execution of this statement: * print 'Error: ', The string "Error: " is printed to the screen. There is a comma, so the next time something prints, it will be on the same line. * Test.printer('Test') Still on the same line, so the print statement will print the return value of this function. To do that, we need to execute the function * self.value = value print self.value the second statement will result in the string 'Test' being printed to the screen, on the same line as what was previously printed (because of our comma earlier). There is no comma here, so the next thing that is printed will be on a new line. * Test.printer('Test') The function is now done executing. What did it return? well, there was no return statement in the function, so it returned nothing. Nothing in python is written as 'None.' So, None is printed to the screen. To fix this, you could either make sure the return value is not printed, like so: print 'Error: ', Test.printer('Test') or, make the printer function a 'returner' function, like so: def returner(self , value ): self.value = value return self.value In general, the 'returner' function is preferable, since it is more general and thus more flexible. Hugo From lie.1296 at gmail.com Tue Dec 29 22:29:42 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 30 Dec 2009 08:29:42 +1100 Subject: [Tutor] try and except In-Reply-To: <1262119658.23352.6.camel@dedal-laptop> References: <1262119658.23352.6.camel@dedal-laptop> Message-ID: On 12/30/2009 7:47 AM, Grigor Kolev wrote: > Hi. > Can someone explain me where is my mistake. > ----------------------------------------------- > class Error(Exception): > def printer(self , value ): > self.value = value > print self.value > def oops(): > raise Error > def domoed(): > try: > oops() > except Error: > Test = Error() > print 'Error: ', Test.printer('Test') > else: > print 'no error' > if __name__ == "__main__": > domoed() > ------------------------------------------------------ > Result is: > Error: Test > None >> From where come it None Test.printer(...) returns "None" and when you: print 'Error: ', Test.printer('Test') turns to: print 'Error: ', None And rather your approach seems "unconventional". You shouldn't create a new Exception object in the except-clause unless you want to raise that new exception. class Error(Exception): def __init__(self, value): self.value = value def printer(self, value): print self.value def oops(): raise Error('some error') def domoed(): try: oops() except Error, e: e.printer() else: print 'no error' if __name__ == "__main__": domoed() From lie.1296 at gmail.com Tue Dec 29 22:40:32 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 30 Dec 2009 08:40:32 +1100 Subject: [Tutor] Modules and Test Suites In-Reply-To: References: Message-ID: On 12/30/2009 2:11 AM, Stephen Nelson-Smith wrote: > I do quite a lot of programming in Ruby. When I do so, my code tends > to have the following layout: > > /path/to/src/my_project > > Inside my_project: > > lib/ > test/ > my_project.rb > > my_project.rb uses classes and helper methods in lib > > Inside test, I have a test suite that also uses classes and helper > methods in ../lib > > This seems like a sensible way to keep tests and other code separate. > > In Python I don't know how to do this.... so I just have all my tests > in the same place as the rest of the code. > > a) Is my above way a sensible and pythonic approach? Yes, python does not enforce how you should structure your directory tree (though you will need to put several __init__.py files here and there). > b) If so - how can I do it in Python? /path/to/src/my_project -> lib/ ` __init__.py ` somelib.py -> tests/ ` __init__.py ` test_one.py ` my_project.py ` run_test.py and you can reference your libraries and tests like so: # my_project.py from lib.somelib import SomeClass import lib.somelib as sl # run_test.py import tests tests.test_one.run_test() > c) If not, is there a better way than having all the tests in the same > place as the rest of the code? From amonroe at columbus.rr.com Wed Dec 30 02:09:42 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 29 Dec 2009 20:09:42 -0500 Subject: [Tutor] computer basics In-Reply-To: <131904.28135.qm@web113213.mail.gq1.yahoo.com> References: <131904.28135.qm@web113213.mail.gq1.yahoo.com> Message-ID: <97202384683.20091229200942@columbus.rr.com> > I am learning Python slowly.? I would like to begin learning all > about how computers work from the bottom up.? I have an > understanding of binary code.? Where should I go from here; can you > suggest continued reading, on line or off to continue my education? This might be worth a look: Digi-Comp I http://www.mindsontoys.com/kits.htm Alan From didar.hossain at gmail.com Wed Dec 30 07:05:55 2009 From: didar.hossain at gmail.com (Didar Hossain) Date: Wed, 30 Dec 2009 11:35:55 +0530 Subject: [Tutor] extracting informations (images and text) from a PDF andcreating a database from it In-Reply-To: References: Message-ID: <62d32bf20912292205p71ce5c23na477d9392a291eb0@mail.gmail.com> On Tue, Dec 29, 2009 at 3:21 PM, Shashwat Anand wrote: > I used PDFMiner and I was pretty satisfied with the text portions. I > retrieved all the text and was able to manipulate it according to my wish. > However I failed on Image part. So Technically my question reduces to 'If > there? a PDF document and some verbose text below them and the pattern is > followed i.e. per page of PDF there will be one image and some texts > following it, how can I retrieve both the images and the text without loss' > ? You can use `pdftohtml' [http://pdftohtml.sf.net]. It is available on Ubuntu. Regards, Didar From rabidpoobear at gmail.com Wed Dec 30 08:28:17 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 30 Dec 2009 01:28:17 -0600 Subject: [Tutor] computer basics In-Reply-To: <131904.28135.qm@web113213.mail.gq1.yahoo.com> References: <131904.28135.qm@web113213.mail.gq1.yahoo.com> Message-ID: Pqazzzzzzzz pAzazqzz /// / // @?&// ?/& /@ /q On 12/28/09, Richard Hultgren wrote: > I am learning Python slowly.? I would like to begin learning all about how > computers work from the bottom up.? I have an understanding of binary code. > Where should I go from here; can you suggest continued reading, on line or > off to continue my education? > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Thank You > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Richard > > > -- Sent from my mobile device From rabidpoobear at gmail.com Wed Dec 30 09:38:17 2009 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Wed, 30 Dec 2009 02:38:17 -0600 Subject: [Tutor] computer basics In-Reply-To: References: <131904.28135.qm@web113213.mail.gq1.yahoo.com> Message-ID: Srry sorry! Phone left on in pocket!! On 12/30/09, Luke Paireepinart wrote: > Pqazzzzzzzz > pAzazqzz /// / // @?&// ?/& /@ /q > > On 12/28/09, Richard Hultgren wrote: >> I am learning Python slowly.? I would like to begin learning all about >> how >> computers work from the bottom up.? I have an understanding of binary >> code. >> Where should I go from here; can you suggest continued reading, on line >> or >> off to continue my education? >> ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Thank You >> ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Richard >> >> >> > > -- > Sent from my mobile device > -- Sent from my mobile device From denis.spir at free.fr Wed Dec 30 14:39:52 2009 From: denis.spir at free.fr (spir) Date: Wed, 30 Dec 2009 14:39:52 +0100 Subject: [Tutor] try and except In-Reply-To: References: <1262119658.23352.6.camel@dedal-laptop> Message-ID: <20091230143952.3881ab75@o> Lie Ryan dixit: > class Error(Exception): > def __init__(self, value): > self.value = value > def printer(self, value): > print self.value You can also use __str__ instead of printer. This will give a standard output form for your error automatically used by print and also, for exceptions, when python writes it to stderr: you don't need to catch the error to write it yourself. def __str_(self, value): print self.value So that a test case may be (untested): def oops(): raise Error('some error') def domoed(catch_error_and_go_on = False): if catch_error_and_go_on: try: oops() except Error, e: print e # just for feedback else: print 'no error' else: oops() # will print error if __name__ == "__main__": print "=== case error uncatched" domoed() print "\n=== case error catched" domoed(true) Denis ________________________________ la vita e estrany http://spir.wikidot.com/ From kent37 at tds.net Wed Dec 30 16:02:52 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 30 Dec 2009 10:02:52 -0500 Subject: [Tutor] try and except In-Reply-To: <20091230143952.3881ab75@o> References: <1262119658.23352.6.camel@dedal-laptop> <20091230143952.3881ab75@o> Message-ID: <1c2a2c590912300702p2c5b85b0g7a63050c72cb344a@mail.gmail.com> On Wed, Dec 30, 2009 at 8:39 AM, spir wrote: > Lie Ryan dixit: > >> class Error(Exception): >> ? ? ?def __init__(self, value): >> ? ? ? ? ?self.value = value >> ? ? ?def printer(self, value): >> ? ? ? ? ?print self.value > > You can also use __str__ instead of printer. This will give a standard output form for your error automatically used by print and also, for exceptions, when python writes it to stderr: you don't need to catch the error to write it yourself. > > ? ? def __str_(self, value): > ? ? ? ? print self.value Should be def __str__(self): return self.value i.e. __str__() takes just one argument (self) and returns (rather than prints) a string. Kent From michaelsprayberry at gmail.com Wed Dec 30 16:26:04 2009 From: michaelsprayberry at gmail.com (Michael Sprayberry) Date: Wed, 30 Dec 2009 10:26:04 -0500 Subject: [Tutor] Computer Basics Message-ID: <27d2fde10912300726vaa37240qb3dc82ea32ea3691@mail.gmail.com> Alan, it is great that you are taking such an active interest in computers. As far as continuing your education in the field and Python I would suggest reading "Python Programming: An Introduction to Computer Science" and "Gray Hat Python: Python Programming for Hackers and Reverse Engineers". Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Thu Dec 31 00:49:24 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Wed, 30 Dec 2009 15:49:24 -0800 Subject: [Tutor] getting a wx.gauge to close! Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90B8CFEC2@HCAMAIL03.ochca.com> Pythonistas, I have got a wx.gauge running and I cannot get it to go away! Any quick and dirty ideas or do you need my ugly syntax. I'm a newbie! Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 31 11:43:58 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 31 Dec 2009 10:43:58 -0000 Subject: [Tutor] getting a wx.gauge to close! References: <97D6F0A82A6E894DAF44B9F575305CC90B8CFEC2@HCAMAIL03.ochca.com> Message-ID: "Pirritano, Matthew" wrote > I have got a wx.gauge running and I cannot get it to go away! > Any quick and dirty ideas or do you need my ugly syntax. I'm a newbie! I've no idea what you've done but guages don't normally go away. The window/dialog they are in sometimes goes away, but without knowing how you have constructed your GUI for the guage, what events it is monitoring etc it is impossible to give you any real advice. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From evosweet at hotmail.com Thu Dec 31 14:43:37 2009 From: evosweet at hotmail.com (Rayon) Date: Thu, 31 Dec 2009 09:43:37 -0400 Subject: [Tutor] printing xps or gif Message-ID: Can I use python to print xps or gif. -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Thu Dec 31 14:49:10 2009 From: norman at khine.net (Norman Khine) Date: Thu, 31 Dec 2009 14:49:10 +0100 Subject: [Tutor] using re to match text and extract info Message-ID: <9c2c8ffb0912310549u102d7224v556cd818633db113@mail.gmail.com> hello, >>> import re >>> line = "ALSACE 67000 Strasbourg 24 rue de la Division Leclerc 03 88 23 05 66 strasbourg at artisansdumonde.org" >>> m = re.search('[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}', line) >>> emailAddress .search(r"(\d+)", line) >>> phoneNumber = re.compile(r'(\d{2}) (\d{2}) (\d{2}) (\d{2}) (\d{2})') >>> phoneNumber.search(line) but this jumbles the phone number and also includes the 67000. how can i split the 'line' into a list? thanks norman From davea at ieee.org Thu Dec 31 15:58:33 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 31 Dec 2009 09:58:33 -0500 Subject: [Tutor] try and except In-Reply-To: <20091230143952.3881ab75@o> References: <1262119658.23352.6.camel@dedal-laptop> <20091230143952.3881ab75@o> Message-ID: <4B3CBC19.9030409@ieee.org> spir wrote: > Lie Ryan dixit: > > >> class Error(Exception): >> def __init__(self, value): >> self.value = value >> def printer(self, value): >> print self.value >> > > You can also use __str__ instead of printer. This will give a standard output form for your error automatically used by print and also, for exceptions, when python writes it to stderr: you don't need to catch the error to write it yourself. > > def __str_(self, value): > print self.value > > The __str__() method needs to return a string, not print it. And it doesn't take a "value" argument. I think you want (untested): def __str__(self): return self.value > So that a test case may be (untested): > > def oops(): > raise Error('some error') > > def domoed(catch_error_and_go_on = False): > if catch_error_and_go_on: > try: > oops() > except Error, e: > print e # just for feedback > else: > print 'no error' > else: > oops() # will print error > if __name__ == "__main__": > print "=== case error uncatched" > domoed() > print "\n=== case error catched" > domoed(true) > > > Denis > ________________________________ > > la vita e estrany > > http://spir.wikidot.com/ > > From MPirritano at ochca.com Thu Dec 31 16:59:00 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Thu, 31 Dec 2009 07:59:00 -0800 Subject: [Tutor] getting a wx.gauge to close! In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC90B8CFEC2@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90B8CFFA6@HCAMAIL03.ochca.com> Alan, Thanks for the reply. I was hoping there might be a quick fix, and it is really a wx.Frame, that contains the wx.gauge. Here's the story. I'm using SPSS. I have written this program to create a list of doctors from medical claims data to be used in another module where I have set up check boxes that trigger an analysis to be run for whatever doctor's box is checked. The program is running with the SPSS Python-Integration Package, and is running externally, not in SPSS. I just needed some indicator that the process was running because it can take a long time (15, 20, or 30 minutes, depending on the size of the claims file which can be more than 4gb). Okay, here's the syntax. Please excuse any sloppiness in my form. I'm just learning. I really appreciate the help! ############################ # Module: lice.py # Author: Matthew Pirritano # Date: 2009/12/15 # Version: Draft 0.1 ''' Create a list of providers for drop down to select for further analyses ''' ############################### # Log: # 2009/12/15 MP - File created # ################################ # Ask the user to select the AMM file they would like to use import wx, os, spss, sys, thread class MyFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, -1, title, pos=(350, 150), size=(250, 150)) # Create the menubar menuBar = wx.MenuBar() # and a menu menu = wx.Menu() # Now create the Panel to put the other controls on. panel = wx.Panel(self) # and a few controls text = wx.StaticText(panel, -1, "I'm working on it!!!") text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)) text.SetSize(text.GetBestSize()) btn = wx.Button(panel, -1, "Close", (10,30)) self.count = 0 self.g2 = wx.Gauge(panel, -1 , 50, (50, 60), (125, 25)) self.Bind(wx.EVT_END_PROCESS, self.on_end_process) self.Bind(wx.EVT_TIMER, self.TimerHandler) self.timer = wx.Timer(self) self.timer.Start(100) # bind the button events to handlers self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn) # Use a sizer to layout the controls, stacked vertically and with # a 10 pixel border around each sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(text, 0, wx.ALL, 10) panel.SetSizer(sizer) panel.Layout() self.chooserThread() def chooserThread(self): thread.start_new_thread(self.chooser,()) def OnTimeToClose(self, evt): """Event handler for the button click.""" print "See ya later!" self.Close() def TimerHandler(self, event): self.count = self.count + 1 if self.count >= 50: self.count = 0 self.g2.Pulse() def on_end_process(self): self.Frame.Destroy() self.kill() app.Destroy() self.g2.Destroy self.__init__(None,-1) def onProvlistDone(self): self.gauge.abort self.Close self.kill def chooser(self): app = wx.PySimpleApp() self.infile = '' fileWildcard = "sav files (*.sav)|*.sav|" \ "All files (*.*)|*.*" dlg = wx.FileDialog(None, message="Choose the file to derive list from...", defaultDir="d:/data/", defaultFile="", wildcard=fileWildcard, style=wx.OPEN| wx.MULTIPLE | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: self.infile = dlg.GetPath() else: self.infile = None dlg.Destroy() self.on_end_process app.Destroy() thread.start_new_thread(self.provlist,()) def getfile(self): return self.infile def provlist(self): spss.Submit(r""" GET FILE= '%s'. save outfile = '%s' /compressed. get file = '%s'. SORT CASES BY license2. AGGREGATE /OUTFILE='D:\Data\AMM\providers\list.sav' /PRESORTED /BREAK=license2 /N_BREAK=N. get file = 'D:\Data\AMM\providers\list.sav'. delete variables N_BREAK. """ % (self.infile, self.infile, self.infile)) i = [0] dataCursor=spss.Cursor(i) oneVar=dataCursor.fetchall() dataCursor.close() uniqueCount=len(set(oneVar)) licenses =[] i = [0] for i in range(uniqueCount): licenses.append(oneVar[i][:1][0]) print licenses self.getfile print self.infile self.on_end_process() if __name__ == "__main__": app = wx.PySimpleApp() app.TopWindow = MyFrame(None, "Please be patient!") app.TopWindow.Show() app.MainLoop() Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Alan Gauld Sent: Thursday, December 31, 2009 2:44 AM To: tutor at python.org Subject: Re: [Tutor] getting a wx.gauge to close! "Pirritano, Matthew" wrote > I have got a wx.gauge running and I cannot get it to go away! > Any quick and dirty ideas or do you need my ugly syntax. I'm a newbie! I've no idea what you've done but guages don't normally go away. The window/dialog they are in sometimes goes away, but without knowing how you have constructed your GUI for the guage, what events it is monitoring etc it is impossible to give you any real advice. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From kent37 at tds.net Thu Dec 31 17:37:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 31 Dec 2009 11:37:53 -0500 Subject: [Tutor] extracting informations (images and text) from a PDF and creating a database from it In-Reply-To: References: Message-ID: <1c2a2c590912310837o25b370dcodaad53c45922b6da@mail.gmail.com> On Tue, Dec 29, 2009 at 2:33 AM, Shashwat Anand wrote: > I need to make a database from some PDFs. I need to extract logos as well as > the information (i.e. name,address) beneath the logo and fill it up in > database. Here is a commercial solution: http://www.addtoit.com Kent From emmanuel.ruellan at laposte.net Thu Dec 31 18:37:58 2009 From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan) Date: Thu, 31 Dec 2009 18:37:58 +0100 Subject: [Tutor] using re to match text and extract info In-Reply-To: <9c2c8ffb0912310549u102d7224v556cd818633db113@mail.gmail.com> References: <9c2c8ffb0912310549u102d7224v556cd818633db113@mail.gmail.com> Message-ID: <7296745c0912310937x6ebe9903g2bb7ab2630fae7b4@mail.gmail.com> What's wrong with the phone number? >>> phoneNumber.search(line).groups() ('03', '88', '23', '05', '66') This looks fine to me. Here is a regex that splits the line into several named groups. Test it with other strings, though >>> line = "ALSACE 67000 Strasbourg 24 rue de la Division Leclerc 03 88 23 05 66 strasbourg at artisansdumonde.org" >>> details_re = re.compile(r'(?P^\D+)(?P\d+)\s+(?P[\D\s]+)(?P
.+?)(?P\d{2} \d{2} \d{2} \d{2} \d{2})\s+(?P[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})') >>> m = details_re.search(line) >>> print m.groups() ('ALSACE ', '67000', 'Strasbourg ', '24 rue de la Division Leclerc ', '03 88 23 05 66', 'strasbourg at artisansdumonde.org') >>> print m.group('phone') 03 88 23 05 66 >>> print m.group('email') strasbourg at artisansdumonde.org Emmanuel On Thu, Dec 31, 2009 at 2:49 PM, Norman Khine wrote: > > > hello, > > >>> import re > >>> line = "ALSACE 67000 Strasbourg 24 rue de la Division Leclerc 03 88 23 > 05 66 strasbourg at artisansdumonde.org" > >>> m = re.search('[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}', line) > >>> emailAddress .search(r"(\d+)", line) > >>> phoneNumber = re.compile(r'(\d{2}) (\d{2}) (\d{2}) (\d{2}) (\d{2})') > >>> phoneNumber.search(line) > > but this jumbles the phone number and also includes the 67000. > > how can i split the 'line' into a list? > > thanks > norman > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 31 19:07:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 31 Dec 2009 18:07:03 -0000 Subject: [Tutor] printing xps or gif References: Message-ID: "Rayon" wrote > Can I use python to print xps or gif. Yes. But how you do it depends on the context of what you are doing. Care to expound? Alan G. From davea at ieee.org Thu Dec 31 19:19:19 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 31 Dec 2009 13:19:19 -0500 Subject: [Tutor] using re to match text and extract info In-Reply-To: <9c2c8ffb0912310549u102d7224v556cd818633db113@mail.gmail.com> References: <9c2c8ffb0912310549u102d7224v556cd818633db113@mail.gmail.com> Message-ID: <4B3CEB27.2090805@ieee.org> Norman Khine wrote: > hello, > > >>>> import re >>>> line = "ALSACE 67000 Strasbourg 24 rue de la Division Leclerc 03 88 23 05 66 strasbourg at artisansdumonde.org" >>>> m = re.search('[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}', line) >>>> emailAddress .search(r"(\d+)", line) >>>> phoneNumber = re.compile(r'(\d{2}) (\d{2}) (\d{2}) (\d{2}) (\d{2})') >>>> phoneNumber.search(line) >>>> > > but this jumbles the phone number and also includes the 67000. > > how can i split the 'line' into a list? > > thanks > norman > > lst = line.split() will split the line strictly by whitespace. Before you can write code to parse a line, you have to know for sure the syntax of that line. This particular one has 15 fields, delimited by spaces. So you can parse it with str.split(), and use slices to get the particular set of numbers representing the phone number. (elements 9-14) If the address portion might be a variable number of words, then you could still use split and slice, but use negative slice parameters to get the phone number relative to the end. (elements -6 to -2) If the email address might have a space within it, then you have to get fancier. If the phone number might have more or less than 5 "words", you have to get fancier. Without a spec, all the regular expressions in the world are just noise. DaveA