From alan.gauld at btinternet.com Mon Dec 1 01:57:04 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Dec 2008 00:57:04 -0000 Subject: [Tutor] Reading gzip files References: Message-ID: "Dinesh B Vadhia" wrote > I'm reading gzip files and writing the content out to a text file > line by line. > File "C:\Python25\lib\gzip.py", line 275, in _read > self._read_eof() > File "C:\Python25\lib\gzip.py", line 311, in _read_eof > raise IOError, "CRC check failed" > IOError: CRC check failed > > I've checked the Python docs and online but cannot find a > solution to the problem. Thanks. At great risk of stating the obvious but have you actually checked that the CRC of the gzipped file is correct? Does the problem only happen with this file or with all gzipped files? And are the files actually zipped by gzip or by some other "compatible" program? If other does it work for genuine gzipped files? Just some ideas to play with. Alan G From david at abbottdavid.com Mon Dec 1 02:17:00 2008 From: david at abbottdavid.com (David) Date: Sun, 30 Nov 2008 20:17:00 -0500 Subject: [Tutor] Reading gzip files In-Reply-To: References: Message-ID: <49333B0C.7020300@abbottdavid.com> Dinesh B Vadhia wrote: > I'm reading gzip files and writing the content out to a text file line > by line. The code is simply: > > import gzip > list_zipfiles = dircache.listdir(zipfolder) > writefile = "out_file.txt" > fw = open(writefile, 'w') > > for ziparchive in list_zipfiles: > zfile = gzip.GzipFile(zipfolder + ziparchive, "r") > for line in zfile: > fw.write(line) > zfile.close() > fw.close() I am learning also. I came up with this; #!/usr/bin/python import tarfile tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r") for f in tFile.getnames(): print f tFile.close() #fname = "out.txt" #fobj = open(fname, 'w') #for line in f: # fobj.write(line + '/n') #tFile.close() #fobj.close() My problem is I can not figure out how to write to the file with new lines, as you can see my attempts at the bottom. This works fine in Linux; ./py_list_zip.py >> out.txt but I want to learn how to do it within python, plus to understand. any hints :) thanks -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com From david at abbottdavid.com Mon Dec 1 02:35:12 2008 From: david at abbottdavid.com (David) Date: Sun, 30 Nov 2008 20:35:12 -0500 Subject: [Tutor] Reading gzip files In-Reply-To: <49333B0C.7020300@abbottdavid.com> References: <49333B0C.7020300@abbottdavid.com> Message-ID: <49333F50.7000609@abbottdavid.com> David wrote: > #!/usr/bin/python > import tarfile > tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r") > for f in tFile.getnames(): > print f > tFile.close() > #fname = "out.txt" > #fobj = open(fname, 'w') > #for line in f: > # fobj.write(line + '/n') > #tFile.close() > #fobj.close() > > My problem is I can not figure out how to write to the file with new > lines, as you can see my attempts at the bottom. This works fine in Linux; > ./py_list_zip.py >> out.txt > > but I want to learn how to do it within python, plus to understand. > > any hints :) > > thanks > -david > > > oops should have been; fobj.write(line + '\n') still does not work, now here is the output from the print statment; zip/ zip/py_database.py zip/print_time.py and out.txt z i p / p r i n t _ t i m e . p y -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com From dineshbvadhia at hotmail.com Mon Dec 1 03:42:04 2008 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Sun, 30 Nov 2008 18:42:04 -0800 Subject: [Tutor] Reading gzip files Message-ID: Hi Alan A bunch of gzipped files are read with the majority working but a few don't. I don't know if these files were originally zipped with gzip but I'd guess that they were. Strangely, for the files that don't work I can read/print the file almost to the end and then it falls over with the CRC error. Dinesh -------------------------------------------------------------------------------- Message: 5 Date: Mon, 1 Dec 2008 00:57:04 -0000 From: "Alan Gauld" Subject: Re: [Tutor] Reading gzip files To: tutor at python.org Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Dinesh B Vadhia" wrote > I'm reading gzip files and writing the content out to a text file > line by line. > File "C:\Python25\lib\gzip.py", line 275, in _read > self._read_eof() > File "C:\Python25\lib\gzip.py", line 311, in _read_eof > raise IOError, "CRC check failed" > IOError: CRC check failed > > I've checked the Python docs and online but cannot find a > solution to the problem. Thanks. At great risk of stating the obvious but have you actually checked that the CRC of the gzipped file is correct? Does the problem only happen with this file or with all gzipped files? And are the files actually zipped by gzip or by some other "compatible" program? If other does it work for genuine gzipped files? Just some ideas to play with. Alan G -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Mon Dec 1 12:35:08 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 1 Dec 2008 06:35:08 -0500 Subject: [Tutor] Reading gzip files In-Reply-To: References: Message-ID: <1c2a2c590812010335h6406da25o2db597ae38f25433@mail.gmail.com> On Sun, Nov 30, 2008 at 9:42 PM, Dinesh B Vadhia wrote: > Strangely, for the files that don't work I can read/print the file almost to > the end and then it falls over with the CRC error. That's because the CRC isn't checked until after the file is read; as you can see from the traceback, the error is in _read_eof(). You could put a print statement or breakpoint in that function to get more information about the failure. Kent From denis.spir at free.fr Mon Dec 1 13:35:27 2008 From: denis.spir at free.fr (spir) Date: Mon, 01 Dec 2008 13:35:27 +0100 Subject: [Tutor] attribute of built-in type In-Reply-To: <1c2a2c590811300520y3a69329fp1a042461c46d640b@mail.gmail.com> References: <49305ADF.7090706@free.fr> <1c2a2c590811281418x5f46b1ddrbe96e2be9bf7900b@mail.gmail.com> <49307855.9090900@free.fr> <1c2a2c590811290529n3a46150exc07a647e41f339dc@mail.gmail.com> <4931BD94.9000809@free.fr> <1c2a2c590811300520y3a69329fp1a042461c46d640b@mail.gmail.com> Message-ID: <4933DA0F.2030207@free.fr> Kent Johnson a ?crit : [...big snip...] > Do you know that you can probably just assign a __name__ attribute to > the objects? Or name, or whatever you like? > > In [13]: class Foo(object): pass > ....: > > In [14]: f=Foo() > > In [15]: f.name > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > > /Users/kent/ in () > > AttributeError: 'Foo' object has no attribute 'name' > > In [16]: f.name = 'f' > > In [17]: f.name > Out[17]: 'f' > > This will work for custom objects that do not have a __slots__ > attribute. Perhaps you could wrap the creation of the objects in a > function that gives them a name? Exactly. I presently use a tool func (actually, a Classmethod) that executes this (pseudocode): def set_names(scope): for name,obj in scope.__dict__.items(): # exclude not_to_be_named objects of the scope if name_has_the_proper_form: # 'name' attr already used for other purpose obj.id = name Which works. I will use this method if I cannot find a way to let the objects natively have __name__ attributes. Conceptually , it is not the same thing -- at least for me. That a class of objects has such an attribute may be seen as an interface characteristics (comparable to 'iterable' or 'ordered') that could be inherited. If I want a class to define/construct ordered containers, I will simply let it inherit list's interface. The facts that python sets __name__ attributes at a low level (as you explain below) and that workarounds are not satisfying, both confirm that this is a fondamental interface charasteristics, not a superficial one. Also, I need a name format rule to distinguish to_be_named from not_to_be_named objects. This is not a big issue, as this rule has only to be followed inside a specific scope. But maybe you understand that I do not find this conceptually /satisfying/. A logical feature that should not depend on such a random detail, rather it should be a basic property. Imagine a case where you would need to give objects characteristics such as iterable or ordered (or mutable / immutable!), by implementing the proper __xxx__ methods, based on the fact that their name has this or that format. Which may happen if such characteristics could not be inherited in python. [...smaller snip...] >> It is an illustration of what i'm trying to do: let a class inherit from >> 'function' so that its instances get a __name__. This wouldn't be a harmful >> overload I guess, as these objects have a __call__ method anyway (the reason >> why I chose 'function'). > > I doubt this would do what you want. AFAICT the function name is > assigned by the compiler, not by the function constructor. (That is a > bit of oversimplification but close enough. I think the compiler > creates a code object, passing its name to the constructor; when a > function object is wrapped around the code object, it pulls its name > from the code object.) This let me play a bit further with functions. The following confirms both that __name__ is a very low-level attribute, and that it lies in the code itself: ============================= import types ; Function = types.FunctionType def typ(obj): return obj.__class__.__name__ def f():pass g = f print id(f) print "%s: %s at %s \n" % ( g.__name__,typ(g),id(g) ) print dir(f) cod = g.func_code print "cod is a '%s' object\n" % typ(cod) h = Function(cod,{}) print "%s: %s at %s \n" % ( h.__name__,typ(h),id(h) ) ==> 10464752 f: function at 10464752 ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] cod is a 'code' object f: function at 10464816 ========================== g points to the same, unique, function as f. So that we can understand that g's __name__ actually is 'f'. But this shows that an object's "birthname" is somehwhat different from further name to which the object may be bound. Now, h points to brand new object (as confirmed by its id); still, its __name__ is 'f'. This attr is well carried by the code and retrieved there, but it is not an attribute of code objects (cod.__name__ does not exist). As I see it, the __name__ is somehow a third kind of name/id for objects. In fact, if 'id()' was called 'address()' instead, then __name__ could well be called __id__. I also find highly meaningful that, among built_in types, the bool, int, float, str,... series do not have a __name__. (But this is probably an off-topic subject for the python_tutor list.) >>> Kent Salutation, denis From wferguson1 at socal.rr.com Mon Dec 1 21:20:03 2008 From: wferguson1 at socal.rr.com (WM.) Date: Mon, 01 Dec 2008 12:20:03 -0800 Subject: [Tutor] Tutor Digest, Vol 58, Issue 2 In-Reply-To: References: Message-ID: <493446F3.8060002@socal.rr.com> Stooges.py i,j,k = 3,3,3 while i != 1: print 'Larry, Moe & Curly Joe!' i -= 1 while j != 1: print 'Go Mad!!' j -= 1 while k != 1: print 'Go-go bad-bad!!' k -= 1 print '\nBye-bye.' I am trying to learn loops. These nested 'whiles' work OK but I would like to wrap this script in a 'for' loop. I have not been able to make that work. From srilyk at gmail.com Mon Dec 1 21:38:28 2008 From: srilyk at gmail.com (W W) Date: Mon, 1 Dec 2008 14:38:28 -0600 Subject: [Tutor] Tutor Digest, Vol 58, Issue 2 In-Reply-To: <493446F3.8060002@socal.rr.com> References: <493446F3.8060002@socal.rr.com> Message-ID: <333efb450812011238m79126e4y7e54768781129ce@mail.gmail.com> Try this: for x in xrange(3, 0, -1): ....: print x ....: ....: 3 2 1 HTH, Wayne On Mon, Dec 1, 2008 at 2:20 PM, WM. wrote: > Stooges.py > > i,j,k = 3,3,3 > while i != 1: > print 'Larry, Moe & Curly Joe!' > i -= 1 > while j != 1: > print 'Go Mad!!' > j -= 1 > while k != 1: > print 'Go-go bad-bad!!' > k -= 1 > print '\nBye-bye.' > > I am trying to learn loops. These nested 'whiles' work OK but I would like > to wrap this script in a 'for' loop. I have not been able to make that work. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- 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 eosher at gmail.com Mon Dec 1 21:48:59 2008 From: eosher at gmail.com (Erica Osher) Date: Mon, 1 Dec 2008 15:48:59 -0500 Subject: [Tutor] converting processing code to python code Message-ID: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> I have a simple processing code that I'm trying to work with in python, but I keep getting syntax errors. Any help on changing the code would be greatly appreciated. Thanks. void setup() { size(550, 500); noStroke(); smooth(); fill(255, 255, 255, 150); } //Square 1 vars int x1 = 5; int y1 = 10; int x1Speed = 2; int y1Speed = 2; //Square 2 Vars int x2 = 150; int y2 = 100; int x2Speed = 4; int y2Speed = 4; int size = 100; void draw() { background(180, 0, 0); drawSquare1(); drawSquare2(); checkCollision(); } void drawSquare1() { if(x1<0 || x1>width-size) { x1Speed = -x1Speed; } if(y1<0 || y1>height-size) { y1Speed = -y1Speed; } x1+= x1Speed; y1+= y1Speed; rect(x1, y1, size, size); } void drawSquare2() { if(x2<0 || x2>width-size) { x2Speed = -x2Speed; } if(y2<0 || y2>height-size) { y2Speed = -y2Speed; } x2+= x2Speed; y2+= y2Speed; rect(x2, y2, size, size); } void checkCollision() { if(abs(x1-x2) < size && abs(y1-y2) < size) { println("Collision"); //fill(255, 255, 255, 200); x1Speed=-x1Speed; x2Speed=-x2Speed; y1Speed=-y1Speed; y2Speed=-y2Speed; } else { fill(255, 255, 255, 100); }; } -- Erica Osher -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Mon Dec 1 21:59:23 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 1 Dec 2008 12:59:23 -0800 Subject: [Tutor] converting processing code to python code In-Reply-To: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> Message-ID: <20081201205923.GB61196@dragon.alchemy.com> On Mon, Dec 01, 2008 at 03:48:59PM -0500, Erica Osher wrote: > I have a simple processing code that I'm trying to work with in python, but > I keep getting syntax errors. Any help on changing the code would be greatly > appreciated. Thanks. Could you show us what you have so far in Python and what syntax errors you get? Just as one example, this would be pretty straightforward to convert straight to Python, syntactically. Whether you'd want to restructure the application to be more optimally "Pythonic" is another topic depending on what else is going on (or is this the entire program)? > void drawSquare1() { > if(x1<0 || x1>width-size) { > x1Speed = -x1Speed; > } > > if(y1<0 || y1>height-size) { > y1Speed = -y1Speed; > } def drawSquare1(): if (x1 < 0 or x1 > width-size): x1 += x1Speed y1 += y1Speed rect(x1, y1, size, size) Is that the sort of code you're coming up with? One thing that strikes me off the top here is that (in either language) you'd be better off not using all those global variables. Make your functions take parameters and use them in your calculations. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From steve at alchemy.com Mon Dec 1 22:02:02 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 1 Dec 2008 13:02:02 -0800 Subject: [Tutor] converting processing code to python code In-Reply-To: <20081201205923.GB61196@dragon.alchemy.com> References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> <20081201205923.GB61196@dragon.alchemy.com> Message-ID: <20081201210202.GC61196@dragon.alchemy.com> On Mon, Dec 01, 2008 at 12:59:23PM -0800, Steve Willoughby wrote: > > void drawSquare1() { > > if(x1<0 || x1>width-size) { > > x1Speed = -x1Speed; > > } > > > > if(y1<0 || y1>height-size) { > > y1Speed = -y1Speed; > > } > > def drawSquare1(): > if (x1 < 0 or x1 > width-size): > x1 += x1Speed > y1 += y1Speed > rect(x1, y1, size, size) I think my eyes skipped somewhere while copying that over. The exact translation of that code snippet would of course have been: def drawSquare1(): if x1 < 0 or x1 > width-size: x1Speed = -x1Speed if y1 < 0 or y1 > height-size: y1Speed = -y1Speed Sorry 'bout that. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From alan.gauld at btinternet.com Tue Dec 2 00:24:38 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Dec 2008 23:24:38 -0000 Subject: [Tutor] Tutor Digest, Vol 58, Issue 2 References: <493446F3.8060002@socal.rr.com> <333efb450812011238m79126e4y7e54768781129ce@mail.gmail.com> Message-ID: "W W" wrote > for x in xrange(3, 0, -1): > ....: print x > ....: > ....: > 3 > 2 > 1 Since the OP isn't using the loop counter a simpler solution is simply for x in range(3): >> i,j,k = 3,3,3 >> while i != 1: >> print 'Larry, Moe & Curly Joe!' >> i -= 1 >> while j != 1: >> print 'Go Mad!!' >> j -= 1 >> while k != 1: >> print 'Go-go bad-bad!!' >> k -= 1 >> print '\nBye-bye.' >> >> I am trying to learn loops. These nested 'whiles' work OK but I >> would like >> to wrap this script in a 'for' loop. I have not been able to make >> that work. To the OP: It helps if you show us what you tried so that we can see where the misunderstanding is. What did you do and what happened? -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Tue Dec 2 00:30:20 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 1 Dec 2008 23:30:20 -0000 Subject: [Tutor] converting processing code to python code References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> Message-ID: "Erica Osher" wrote >I have a simple processing code that I'm trying to work with in >python, but > I keep getting syntax errors. Any help on changing the code would be > greatly > appreciated. Thanks. It would help to have some background. What language are you translating from? It could be C/C++/JavaScript or Java. Or possibly other C type languages. It might be significant! Also show us what you tried and the syntax error messages. That way we can figure out what it is you are doing wrong. Otherwise we wind up writing your code for you and you learn nothing. Then we have to do it all o er again next time you get stuck. That's inefficient for both you and us! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From eosher at gmail.com Tue Dec 2 00:40:03 2008 From: eosher at gmail.com (Erica Osher) Date: Mon, 1 Dec 2008 18:40:03 -0500 Subject: [Tutor] converting processing code to python code In-Reply-To: References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> Message-ID: <60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> The code was originally created for a Processing Project and I'm just starting to learn python and I'd like to build on this code. The syntax error I get is *Traceback (most recent call last): File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript File "", line 8 int x1 = 5; ^ SyntaxError: invalid syntax * any suggestions? Thanks. On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld wrote: > > "Erica Osher" wrote > > I have a simple processing code that I'm trying to work with in python, >> but >> I keep getting syntax errors. Any help on changing the code would be >> greatly >> appreciated. Thanks. >> > > It would help to have some background. > What language are you translating from? It could be C/C++/JavaScript or > Java. > Or possibly other C type languages. It might be significant! > > Also show us what you tried and the syntax error messages. > That way we can figure out what it is you are doing wrong. > > Otherwise we wind up writing your code for you and you learn nothing. > Then we have to do it all o er again next time you get stuck. > That's inefficient for both you and us! > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Erica Osher -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Tue Dec 2 00:45:25 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 1 Dec 2008 15:45:25 -0800 Subject: [Tutor] converting processing code to python code In-Reply-To: <60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> <60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> Message-ID: <20081201234525.GD61196@dragon.alchemy.com> On Mon, Dec 01, 2008 at 06:40:03PM -0500, Erica Osher wrote: > The code was originally created for a Processing Project and I'm just > starting to learn python and I'd like to build on this code. > > The syntax error I get is > > *Traceback (most recent call last): > File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript > File "", line 8 > int x1 = 5; That's not even close to Python syntax. That looks like a C-derived language. I'd recommend going through a basic Python tutorial first, so you can see how Python works in a general sense before trying to convert code. In Python, you don't need to declare variables like that, you just assign them values. So that line would be simply: x1 = 5 > SyntaxError: invalid syntax > * > any suggestions? > > Thanks. > > On Mon, Dec 1, 2008 at 6:30 PM, Alan Gauld wrote: > > > > > "Erica Osher" wrote > > > > I have a simple processing code that I'm trying to work with in python, > >> but > >> I keep getting syntax errors. Any help on changing the code would be > >> greatly > >> appreciated. Thanks. > >> > > > > It would help to have some background. > > What language are you translating from? It could be C/C++/JavaScript or > > Java. > > Or possibly other C type languages. It might be significant! > > > > Also show us what you tried and the syntax error messages. > > That way we can figure out what it is you are doing wrong. > > > > Otherwise we wind up writing your code for you and you learn nothing. > > Then we have to do it all o er again next time you get stuck. > > That's inefficient for both you and us! > > > > > > -- > > Alan Gauld > > Author of the Learn to Program web site > > http://www.freenetpages.co.uk/hp/alan.gauld > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Erica Osher > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From srilyk at gmail.com Tue Dec 2 01:23:31 2008 From: srilyk at gmail.com (W W) Date: Mon, 1 Dec 2008 18:23:31 -0600 Subject: [Tutor] Tutor Digest, Vol 58, Issue 2 In-Reply-To: References: <493446F3.8060002@socal.rr.com> <333efb450812011238m79126e4y7e54768781129ce@mail.gmail.com> Message-ID: <333efb450812011623q61817bd9m3f7106f7f8081ce5@mail.gmail.com> On Mon, Dec 1, 2008 at 5:24 PM, Alan Gauld wrote: > Since the OP isn't using the loop counter a simpler solution > is simply > > for x in range(3): but the OP was looping from 3 to 1, and that's the easiest way I knew of. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From wferguson1 at socal.rr.com Tue Dec 2 01:44:02 2008 From: wferguson1 at socal.rr.com (WM.) Date: Mon, 01 Dec 2008 16:44:02 -0800 Subject: [Tutor] 'for' loops Message-ID: <493484D2.50603@socal.rr.com> I recently asked a question about 'for' loops, expecting them to be similar to 'for-next' loops. I have looked at several on-line tutors but am still in the dark about what 'for' loops do. Does anyone have a plain English about the use of 'for' loops? Are 'while' loops the only way Python runs a sub-routine over & over? From steve at alchemy.com Tue Dec 2 01:52:05 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 1 Dec 2008 16:52:05 -0800 Subject: [Tutor] 'for' loops In-Reply-To: <493484D2.50603@socal.rr.com> References: <493484D2.50603@socal.rr.com> Message-ID: <20081202005205.GE61196@dragon.alchemy.com> On Mon, Dec 01, 2008 at 04:44:02PM -0800, WM. wrote: > I recently asked a question about 'for' loops, expecting them to be > similar to 'for-next' loops. I have looked at several on-line tutors but > am still in the dark about what 'for' loops do. > Does anyone have a plain English about the use of 'for' loops? > Are 'while' loops the only way Python runs a sub-routine over & over? No, both 'while' and 'for' loops are for running a block of code (whether subroutine calls or whatever) over and over. The difference between the two is that 'while' will continue repeating the block for however many iterations it takes until the condition is satisfied ('while x is true, for some expression x'), a 'for' loop will run the block of code a set number of times ('once for each element of some set of values'). So if you want to execute 'print' for every line of a file, you would do this: for line in file: print line If you wanted to double a value until it exceeded 100, you would use a while loop: while x <= 100: x *= 2 If you just want something executed a specific number of times, (like print "hello" 10 times), you can use a for loop: for i in range(10): print "hello" This is, just like any 'for' loop, executing the block once per element of a list. The list in this case is range(10) which is an expression that generates the list (0, 1, 2, ..., 9), so you get one run through the code for each of those. Does that help? > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From srilyk at gmail.com Tue Dec 2 01:53:28 2008 From: srilyk at gmail.com (W W) Date: Mon, 1 Dec 2008 18:53:28 -0600 Subject: [Tutor] 'for' loops In-Reply-To: <493484D2.50603@socal.rr.com> References: <493484D2.50603@socal.rr.com> Message-ID: <333efb450812011653v75b9d4f6ode34ea90d6ea71e7@mail.gmail.com> On Mon, Dec 1, 2008 at 6:44 PM, WM. wrote: > I recently asked a question about 'for' loops, expecting them to be similar > to 'for-next' loops. I have looked at several on-line tutors but am still > in the dark about what 'for' loops do. > Does anyone have a plain English about the use of 'for' loops? > Are 'while' loops the only way Python runs a sub-routine over & over? For loops are mainly used when you want a specific number of iterations, such as looping over the elements of a list. In C/C++ you would do something like this: int myarray[] = {1, 2, 3, 4, 5}; for(int x = 0; x < 5; x++) printf("%d", myarray[x]) In python it would be much cleaner: myarray = [1, 2, 3, 4, 5] for x in myarray: print x 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 john at fouhy.net Tue Dec 2 01:56:17 2008 From: john at fouhy.net (John Fouhy) Date: Tue, 2 Dec 2008 13:56:17 +1300 Subject: [Tutor] 'for' loops In-Reply-To: <493484D2.50603@socal.rr.com> References: <493484D2.50603@socal.rr.com> Message-ID: <5e58f2e40812011656o418d08efo24295b62fc0c2a04@mail.gmail.com> On 02/12/2008, WM. wrote: > I recently asked a question about 'for' loops, expecting them to be similar > to 'for-next' loops. I have looked at several on-line tutors but am still > in the dark about what 'for' loops do. > Does anyone have a plain English about the use of 'for' loops? > Are 'while' loops the only way Python runs a sub-routine over & over? I'm not sure exactly what you understand by a "for-next loop". A for loop, essentially, iterates over a list [1]. e.g. for fruit in ['apple', 'pear', 'banana', 'tomato']: print fruit The loop will set the variable 'fruit' to be 'apple', 'pear', etc. on each pass through the loop. If you just want to do something n times, the usual idiom is: for i in range(n): # do something, possibly involving i range(n) is a function that will produce the list [0, 1, 2, ..., n-1]. Tutorials should cover this, so I'm not sure if I'm telling you anything new. If there's something particular you're stuck on, ask :-) -- John. [1] Technically, it iterates over an iterator, which you can think of as an object that behaves like a list when you throw it at a for loop. From alan.gauld at btinternet.com Tue Dec 2 02:17:41 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 01:17:41 -0000 Subject: [Tutor] 'for' loops References: <493484D2.50603@socal.rr.com> Message-ID: "WM." wrote >I recently asked a question about 'for' loops, expecting them to be >similar to 'for-next' loops. I have looked at several on-line tutors >but am still in the dark about what 'for' loops do. Python for loops are like foreach loops in other languages. A Python for loop executes a bit of code for each element in a sequence (list, string, dictionary, set, file etc) It will keep looping until it runs out of items in the sequence. Thus to print each letter in a string: mystring = 'foobar' for ch in mystring: print ch Or to print each element of a list: mlist = [1,'2,'a',45, True] for item in mylist: print item And if you want to loop for a fixed number of iterations simply construct a list with that number of elements. The range() function does that for us, thus: for n in range(12): print 'hi' will print 'hi' 12 times. > Does anyone have a plain English about the use of 'for' loops? > Are 'while' loops the only way Python runs a sub-routine over & > over? while loops are used much less in Python than in other languages because for loops are so powerful. while lops are generally used in cases where you don't know how many times you need to loop or you want to loop 'forever'. while True: print 'Can't stop me now!' will keep on looping until you close the program c = 0 while c != -1: c = int(raw_input('Enter a number(-1 to stop) ')) print c will keep looping until the user enters -1 More info and a comparison with JabaScript and VBScript can be found in my tutor in the looping topic. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Tue Dec 2 02:23:59 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 01:23:59 -0000 Subject: [Tutor] converting processing code to python code References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> <60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> Message-ID: "Erica Osher" wrote > The code was originally created for a Processing Project and I'm > just > starting to learn python and I'd like to build on this code. The fact that you still don't tell us what the original language is suggests you are not a very experienced programmer in any language. Is that assumption correct? > The syntax error I get is > > *Traceback (most recent call last): > File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript > File "", line 8 > int x1 = 5; > ^ > SyntaxError: invalid syntax In Python variables are just names that refer to objects. The objects can be of any type so we don't need to declare them as int, float, char etc. You just need x1 = 5 No semi colons are needed either. However since this is such a basic Python statement I do think you should take an hour or two to go through some of the complete beginners tutorials found here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Try mine if you like but there is a variety of styles for you to choose from. Once you are familiar with the basics try converting your code again and ask specific questions here for help. That will be much more efficient for all of us than simply randomly trying things then asking for help. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From eosher at gmail.com Tue Dec 2 02:28:10 2008 From: eosher at gmail.com (Erica Osher) Date: Mon, 1 Dec 2008 20:28:10 -0500 Subject: [Tutor] converting processing code to python code In-Reply-To: References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com> <60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> Message-ID: <60b0585d0812011728n11549396oc40c1477792cdba6@mail.gmail.com> I wrote the original code in a program called Processing. ( http://processing.org/) Thanks for your advice, I definitely need some tutorials. On Mon, Dec 1, 2008 at 8:23 PM, Alan Gauld wrote: > "Erica Osher" wrote > > The code was originally created for a Processing Project and I'm just >> starting to learn python and I'd like to build on this code. >> > > The fact that you still don't tell us what the original language is > suggests > you are not a very experienced programmer in any language. Is that > assumption correct? > > The syntax error I get is >> >> *Traceback (most recent call last): >> File "nodebox/gui/mac/__init__.pyo", line 332, in _compileScript >> File "", line 8 >> int x1 = 5; >> ^ >> SyntaxError: invalid syntax >> > > In Python variables are just names that refer to objects. The objects > can be of any type so we don't need to declare them as int, float, > char etc. You just need > > x1 = 5 > > No semi colons are needed either. > > However since this is such a basic Python statement I do think > you should take an hour or two to go through some of the complete > beginners tutorials found here: > > http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > > Try mine if you like but there is a variety of styles for you to choose > from. > > Once you are familiar with the basics try converting your code again > and ask specific questions here for help. That will be much more > efficient for all of us than simply randomly trying things then asking > for help. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Erica Osher -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 2 02:27:08 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 01:27:08 -0000 Subject: [Tutor] Tutor Digest, Vol 58, Issue 2 References: <493446F3.8060002@socal.rr.com><333efb450812011238m79126e4y7e54768781129ce@mail.gmail.com> <333efb450812011623q61817bd9m3f7106f7f8081ce5@mail.gmail.com> Message-ID: "W W" wrote >> for x in range(3): > > but the OP was looping from 3 to 1, and that's the easiest way I > knew of. Yes, but they weren't using the counter so it didn't matter which way they looped. I suspect the decrement pattern was just the one they were most familiar with in some other language (like Java? or VB?) Alan G From alan.gauld at btinternet.com Tue Dec 2 02:41:34 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 01:41:34 -0000 Subject: [Tutor] converting processing code to python code References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com><60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> <60b0585d0812011728n11549396oc40c1477792cdba6@mail.gmail.com> Message-ID: "Erica Osher" wrote >I wrote the original code in a program called Processing. ( > http://processing.org/) Aha! The language is actually called Processing. Now that's a new one on me. Thanks for the link, it does help to have the context. :-) Now the next question, are you experienced in Processing or are you a beginner there too? If you are experienced in another language the best tutiorial is the standard Python tutorial found on the Python website. If you are a beginner in Processing as well stick to the newbies tutors I mentioned last time. Regards, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Tue Dec 2 02:49:36 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 01:49:36 -0000 Subject: [Tutor] converting processing code to python code References: <60b0585d0812011248i78504df4j931f41d496190d76@mail.gmail.com><60b0585d0812011540h772d0730yf1202ee0cd10b616@mail.gmail.com> <60b0585d0812011728n11549396oc40c1477792cdba6@mail.gmail.com> Message-ID: "Erica Osher" wrote >I wrote the original code in a program called Processing. ( > http://processing.org/) Having had a look at the web site it is obvious that the hardest bit of porting the Processing code to Python is that Python does not support all the visual drawing functions that Processing does. Most of them can be replicated by writing equivalent functions in Python but that is a lot of work - effectively building a python vesion of Processing! (That would be an exellent project BTW but not one for a beginner!) I tried a Google search. The best I could find was: http://i.document.m05.de/?p=483 which suggests using Jython whicjh allows you to call the Java processing libraies/classes from within Python code. But to understand it I think you still need to go through the tutorials first. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From bryan.fodness at gmail.com Tue Dec 2 02:44:20 2008 From: bryan.fodness at gmail.com (Bryan Fodness) Date: Mon, 1 Dec 2008 20:44:20 -0500 Subject: [Tutor] try except block for multiple statements Message-ID: I would like to use a try except to see if a value exists. But, when I use the following, if a does not exist it exits. I understand why this does this, but is there a way to get b,c, and d if a does not exist without using a try except for every statement? try: fo.write("a = %s\n" %plan.a) fo.write("b = %s\n" %plan.b) fo.write("c = %s\n" %plan.c) fo.write("d = %s\n" %plan.d) except AttributeError: pass -- "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. " -Albert Einstein -------------- next part -------------- An HTML attachment was scrubbed... URL: From john at fouhy.net Tue Dec 2 02:54:26 2008 From: john at fouhy.net (John Fouhy) Date: Tue, 2 Dec 2008 14:54:26 +1300 Subject: [Tutor] try except block for multiple statements In-Reply-To: References: Message-ID: <5e58f2e40812011754g3389273bj3e1187b63a01da13@mail.gmail.com> On 02/12/2008, Bryan Fodness wrote: > I would like to use a try except to see if a value exists. But, when I use > the following, if a does not exist it exits. I understand why this does > this, but is there a way to get b,c, and d if a does not exist without using > a try except for every statement? > > try: > fo.write("a = %s\n" %plan.a) > fo.write("b = %s\n" %plan.b) > fo.write("c = %s\n" %plan.c) > fo.write("d = %s\n" %plan.d) > except AttributeError: > pass AFAIK, no -- but you could always use a loop. attrs = ['a', 'b', 'c', 'd'] for attr in attrs: try: fo.write('A = %s\n' % getattr(plan, attr)) except AttributeError: pass -- John. From bgailer at gmail.com Tue Dec 2 03:31:57 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 01 Dec 2008 21:31:57 -0500 Subject: [Tutor] try except block for multiple statements In-Reply-To: References: Message-ID: <49349E1D.5060508@gmail.com> Bryan Fodness wrote: > I would like to use a try except to see if a value exists. But, when > I use the following, if a does not exist it exits. I understand why > this does this, but is there a way to get b,c, and d if a does not > exist without using a try except for every statement? > > try: > fo.write("a = %s\n" %plan.a) > fo.write("b = %s\n" %plan.b) > fo.write("c = %s\n" %plan.c) > fo.write("d = %s\n" %plan.d) > except AttributeError: > pass > def foo(obj, attr): val = getattr(obj, attr, None) if val is not None: obj.write("%s = %s\n" % (attr, val)) foo(plan, "a") foo(plan, "b") foo(plan, "c") foo(plan, "d") > -- > "Any intelligent fool can make things bigger, more complex, and more > violent. It takes a touch of genius - and a lot of courage - to move > in the opposite direction. " -Albert Einstein > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From kent37 at tds.net Tue Dec 2 04:00:36 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 1 Dec 2008 22:00:36 -0500 Subject: [Tutor] 'for' loops In-Reply-To: <5e58f2e40812011656o418d08efo24295b62fc0c2a04@mail.gmail.com> References: <493484D2.50603@socal.rr.com> <5e58f2e40812011656o418d08efo24295b62fc0c2a04@mail.gmail.com> Message-ID: <1c2a2c590812011900t326e6241r630ac4a98c7e29a7@mail.gmail.com> On Mon, Dec 1, 2008 at 7:56 PM, John Fouhy wrote: > [1] Technically, it iterates over an iterator, which you can think of > as an object that behaves like a list when you throw it at a for loop. The object of the 'in' must be an iterable, which is an object that can produce an iterator when asked. A list is an iterable, not an iterator. Kent From kent37 at tds.net Tue Dec 2 04:03:40 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 1 Dec 2008 22:03:40 -0500 Subject: [Tutor] attribute of built-in type In-Reply-To: <4933DA0F.2030207@free.fr> References: <49305ADF.7090706@free.fr> <1c2a2c590811281418x5f46b1ddrbe96e2be9bf7900b@mail.gmail.com> <49307855.9090900@free.fr> <1c2a2c590811290529n3a46150exc07a647e41f339dc@mail.gmail.com> <4931BD94.9000809@free.fr> <1c2a2c590811300520y3a69329fp1a042461c46d640b@mail.gmail.com> <4933DA0F.2030207@free.fr> Message-ID: <1c2a2c590812011903j493e9ef2t8c7c6b88fd228886@mail.gmail.com> Here is an idea that might help - you do have some control over assignment to attributes of an object. If you stored your objects in another object you could assign __name__ attributes automatically. For example: class Container(object): def __setattr__(self, name, value): if not hasattr(value, '__name__'): try: value.__name__ = name except: pass object.__setattr__(self, name, value) class Item(object): pass c = Container() c.s = 'string' c.i = Item() print c.i.__name__ # i print c.s.__name__ # AttributeError, can't add __name__ attribute to string Kent From wferguson1 at socal.rr.com Tue Dec 2 04:57:12 2008 From: wferguson1 at socal.rr.com (WM.) Date: Mon, 01 Dec 2008 19:57:12 -0800 Subject: [Tutor] I asked about loops Message-ID: <4934B218.9070909@socal.rr.com> and your response was most gratifying. I think that I now have a handle on the subject and want to say, "Thanks to you all." WM From cspears2002 at yahoo.com Tue Dec 2 06:43:41 2008 From: cspears2002 at yahoo.com (Christopher Spears) Date: Mon, 1 Dec 2008 21:43:41 -0800 (PST) Subject: [Tutor] noise function Message-ID: <924207.13128.qm@web51604.mail.re2.yahoo.com> Hi! Does anyone know if python has a noise function? From aivars868 at gmail.com Tue Dec 2 08:12:54 2008 From: aivars868 at gmail.com (aivars) Date: Tue, 2 Dec 2008 09:12:54 +0200 Subject: [Tutor] sqlite3 Python25 parameter binding problem with UPDATE please help Message-ID: Hello, Does sqlite3 in python 2.5 supports parameter bindings in UPDATE statement? When I do like the following: oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, [year])) oCon.commit() it throws me the error: sqlite3.InterfaceError: error binding parameter 1 probably unsupported type All works OK when using INSERT with the same parameters. On Google I found that python with MySQL supports this syntax (sorry I am not able to find the link now) but I am not able to get it working with sqlite3 Thanks in advance Maybe I should as this question on sqlite list? Aivars From jmorcombe at westnet.com.au Tue Dec 2 09:39:15 2008 From: jmorcombe at westnet.com.au (Jim Morcombe) Date: Tue, 02 Dec 2008 17:39:15 +0900 Subject: [Tutor] cgi on novell/Appache Message-ID: <4934F433.3020804@westnet.com.au> I have a cgi script that I want to get running on an Apache server running under Novel. I know absolutely nothing about novell. I need to have the first line of the script point to the Python interpretter, but have no idea where it is. (I don't have access to the server to play around on it. I have to change the script, give it to the admin guy and let him load it. He knows nothing about cgi, python, etc. ) What is the novel equivalent of "/usr/bin/python"? Jim From alan.gauld at btinternet.com Tue Dec 2 09:51:10 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 08:51:10 -0000 Subject: [Tutor] noise function References: <924207.13128.qm@web51604.mail.re2.yahoo.com> Message-ID: "Christopher Spears" wrote > Does anyone know if python has a noise function? What kind of noise function? What would you expect it to produce? A stream of random numbers perhaps? A single number each time it is called? And which noise profile should it follow? Or would it be a multi dimensional noise model? I don't know of any such function and I'm not sure wiothout more details how it would work in a general way even if it did exist. Can you give a speciofic example of what you expect? -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Tue Dec 2 09:55:17 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 08:55:17 -0000 Subject: [Tutor] sqlite3 Python25 parameter binding problem with UPDATEplease help References: Message-ID: "aivars" wrote > oCon.execute("UPDATE rezerve SET latusaldo =? where gads > =?;",(result, [year])) > oCon.commit() > > it throws me the error: > sqlite3.InterfaceError: error binding parameter 1 probably > unsupported type I assume its the [year] value it objects to. I'm not sure what you expect SQLite to do with a list as a value, it does not support a list type field. > All works OK when using INSERT with the same parameters. Are you sure? You can insert a list into a field? > Maybe I should as this question on sqlite list? Perhaps but I think it is the list parameter that it doesn't like. Unless I misunderstand the syntax. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Tue Dec 2 10:01:21 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 2 Dec 2008 09:01:21 -0000 Subject: [Tutor] cgi on novell/Appache References: <4934F433.3020804@westnet.com.au> Message-ID: "Jim Morcombe" wrote > running under Novel. I know absolutely nothing about novell. I > need to have the first line of the script point to the Python > interpretter, I don't think that will do anything under Novell, assuming you mean the Novell network OS and not Novell Linux? If its NovelWare then you need to set the PATH to Python and set up the file associatiion between your .py file and python. Then you need to use the cgi-bin folder to store the file rather than your web app space. At least that's what I did the last time I used a DOS based server - but that was about 12 years ago! If it's Novell Linux the standard /env/python should work. > around on it. I have to change the script, give it to the admin guy > and let him load it. He knows nothing about cgi, python, etc. ) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From roadierich at googlemail.com Tue Dec 2 13:32:44 2008 From: roadierich at googlemail.com (Richard Lovely) Date: Tue, 2 Dec 2008 12:32:44 +0000 Subject: [Tutor] noise function In-Reply-To: References: <924207.13128.qm@web51604.mail.re2.yahoo.com> Message-ID: There's probably something like what you want within the "random" module. at the interactive prompt, try: import random help(random) --- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com 2008/12/2 Alan Gauld : > > "Christopher Spears" wrote >> >> Does anyone know if python has a noise function? > > What kind of noise function? > What would you expect it to produce? > A stream of random numbers perhaps? > A single number each time it is called? > > And which noise profile should it follow? > Or would it be a multi dimensional noise model? > > I don't know of any such function and I'm not sure wiothout more details how > it would work in a general way even if it did exist. Can you give a > speciofic example of what you expect? > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From jeff at dcsoftware.com Tue Dec 2 17:40:04 2008 From: jeff at dcsoftware.com (Jeff Johnson) Date: Tue, 02 Dec 2008 09:40:04 -0700 Subject: [Tutor] [Fwd: Python Course] Message-ID: <493564E4.1080408@dcsoftware.com> This was forwarded to me from one of our user group members: "Hi everyone! I just signed up for an online course in "Web Development with Python" through DePaul University, which I think you might find interesting: http://ipd.cdm.depaul.edu/wdp/Prog_WDP.htm I talked to the folks in the admissions department and they said they needed 10 students enrolled in order to go through with the course. So I told them I would mention this to the python programmers I knew to try to drum up enough applicants. Best regards, John T." If anyone has any objection to posting this type of information, please let me know. I thought members might be interested in available Python courses. -- Jeff Jeff Johnson jeff at dcsoftware.com Phoenix Python User Group - sunpiggies at googlegroups.com From aivars868 at gmail.com Tue Dec 2 19:33:20 2008 From: aivars868 at gmail.com (aivars) Date: Tue, 2 Dec 2008 20:33:20 +0200 Subject: [Tutor] sqlite3 Python25 parameter binding problem with UPDATEplease help In-Reply-To: References: Message-ID: Interestingly, when simply testing like this: import sqlite3, sys sPATH=r'e:\pythonexamples\aivars2.db' oCon=sqlite3.connect(sPATH) cur=oCon.cursor() oCon.execute("""UPDATE rezerve SET latusaldo = ? WHERE gads = ? """,(6000.0,'2006')) oCon.commit() it works. Therefore I am stuck since it looks like there is something wrong in below function. Thanks, Aivars 2008/12/2 aivars : > Alan, > Thanks. > > Ok I should have been more precise and give more code > There is what I do: > > def starpiba(year, month, day): > datumsno=str(year+'-01-01') #beginning of year date > datumsuz=str(year+'-'+month+'-'+day) #period end date > > result = (atlikumiBeiguKurss(year, month, day)- > atlikumiDienasKurss(year, month, day)) > ##print result > oCon=sqlite3.connect(sPATH) > if result<=0: > print abs(result) > oCon.execute("UPDATE rezerve SET latusaldo =? where gads > =?;",(result, [year])) > oCon.commit() > else: > ##print 'aivars' > oCon.execute("UPDATE rezerve SET latusaldo =? where gads > =?;",(result, [year])) > > Please bear in mind I am a noob in Python and I write spaggeti code. > > There is a table in my sqlite database called rezerve which shows the > breakdown of a consolidated reserves item in a consolidated balance > sheet report (if that says anything to you) by years. For the previous > years the numbers does not change since they are already reported but > for the current year it may change month by month until reported. > Therefore, I wanted to update the numbers for the current year. > > The above python function works OK. > > Thanks > > Aivars > > > 2008/12/2 Alan Gauld : >> >> "aivars" wrote >> >>> oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, >>> [year])) >>> oCon.commit() >>> >>> it throws me the error: >>> sqlite3.InterfaceError: error binding parameter 1 probably unsupported >>> type >> >> I assume its the [year] value it objects to. >> I'm not sure what you expect SQLite to do with a list as a value, it does >> not support a list type field. >> >>> All works OK when using INSERT with the same parameters. >> >> Are you sure? You can insert a list into a field? >> >>> Maybe I should as this question on sqlite list? >> >> Perhaps but I think it is the list parameter that it doesn't like. >> Unless I misunderstand the syntax. >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.freenetpages.co.uk/hp/alan.gauld >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > From aivars868 at gmail.com Tue Dec 2 20:23:02 2008 From: aivars868 at gmail.com (aivars) Date: Tue, 2 Dec 2008 21:23:02 +0200 Subject: [Tutor] sqlite3 Python25 parameter binding problem with UPDATEplease help In-Reply-To: References: Message-ID: Hello again, Finally I managed to get it working! I had to remove [year] braces from the second argument - year. The reason I used [] was the posts we exchanged recently (7th of November) about CGI script where it was recommended by Kent that I put [] around string argument: Quote The second argument to execute() is a *sequence* of parameter values. A string is a sequence of characters, so by passing a plain string you are saying that each character of the string is a parameter to the SQL. Try adding braces around the parameter to make list: cur.execute("insert into test (name) values (?)", [sPath]) Kent Unquote Now the year argument is string so I thought I should do the same again but it is not working. The reason is of course is that I am new to python and there are so many things to learn so the question really is why in this case there were no [] or () necessary? Thanks in advance Aivars 2008/12/2 aivars : > Interestingly, when simply testing like this: > > import sqlite3, sys > sPATH=r'e:\pythonexamples\aivars2.db' > oCon=sqlite3.connect(sPATH) > > cur=oCon.cursor() > > oCon.execute("""UPDATE rezerve SET latusaldo = ? WHERE gads = ? > """,(6000.0,'2006')) > > oCon.commit() > > > it works. Therefore I am stuck since it looks like there is something > wrong in below function. > > Thanks, > > Aivars > > > 2008/12/2 aivars : >> Alan, >> Thanks. >> >> Ok I should have been more precise and give more code >> There is what I do: >> >> def starpiba(year, month, day): >> datumsno=str(year+'-01-01') #beginning of year date >> datumsuz=str(year+'-'+month+'-'+day) #period end date >> >> result = (atlikumiBeiguKurss(year, month, day)- >> atlikumiDienasKurss(year, month, day)) >> ##print result >> oCon=sqlite3.connect(sPATH) >> if result<=0: >> print abs(result) >> oCon.execute("UPDATE rezerve SET latusaldo =? where gads >> =?;",(result, [year])) >> oCon.commit() >> else: >> ##print 'aivars' >> oCon.execute("UPDATE rezerve SET latusaldo =? where gads >> =?;",(result, [year])) >> >> Please bear in mind I am a noob in Python and I write spaggeti code. >> >> There is a table in my sqlite database called rezerve which shows the >> breakdown of a consolidated reserves item in a consolidated balance >> sheet report (if that says anything to you) by years. For the previous >> years the numbers does not change since they are already reported but >> for the current year it may change month by month until reported. >> Therefore, I wanted to update the numbers for the current year. >> >> The above python function works OK. >> >> Thanks >> >> Aivars >> >> >> 2008/12/2 Alan Gauld : >>> >>> "aivars" wrote >>> >>>> oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, >>>> [year])) >>>> oCon.commit() >>>> >>>> it throws me the error: >>>> sqlite3.InterfaceError: error binding parameter 1 probably unsupported >>>> type >>> >>> I assume its the [year] value it objects to. >>> I'm not sure what you expect SQLite to do with a list as a value, it does >>> not support a list type field. >>> >>>> All works OK when using INSERT with the same parameters. >>> >>> Are you sure? You can insert a list into a field? >>> >>>> Maybe I should as this question on sqlite list? >>> >>> Perhaps but I think it is the list parameter that it doesn't like. >>> Unless I misunderstand the syntax. >>> >>> -- >>> Alan Gauld >>> Author of the Learn to Program web site >>> http://www.freenetpages.co.uk/hp/alan.gauld >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >> > From constantfables at gmail.com Tue Dec 2 20:56:33 2008 From: constantfables at gmail.com (Daniel J Kramer) Date: Tue, 2 Dec 2008 14:56:33 -0500 Subject: [Tutor] importing images and sound Message-ID: Hi I am writing a program based around a pub quiz. I have been able to write the program so it runs through a series of questions and gives users a score at the end. I need to import a specific font, images and sound. Does anyone know a good place to look for tutorials for this? At the moment, I do not know where to begin thank you -- Daniel J Kramer Constant Fables 249 12th st #3 Brooklyn, NY 11215 (h) 347 223 4571 (m) 646 427 7430 -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Tue Dec 2 21:02:07 2008 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 2 Dec 2008 12:02:07 -0800 Subject: [Tutor] importing images and sound In-Reply-To: References: Message-ID: <20081202200207.GA16958@dragon.alchemy.com> On Tue, Dec 02, 2008 at 02:56:33PM -0500, Daniel J Kramer wrote: > Hi > > I am writing a program based around a pub quiz. I have been able to write > the program so it runs through a series of questions and gives users a score > at the end. > > I need to import a specific font, images and sound. Does anyone know a good > place to look for tutorials for this? At the moment, I do not know where to > begin That depends on what GUI toolkit you're using. If you're going to be doing a lot with sound and images, particularly video clips, you might want to look at pymedia or pygame. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From srilyk at gmail.com Tue Dec 2 21:49:20 2008 From: srilyk at gmail.com (W W) Date: Tue, 2 Dec 2008 14:49:20 -0600 Subject: [Tutor] importing images and sound In-Reply-To: <20081202200207.GA16958@dragon.alchemy.com> References: <20081202200207.GA16958@dragon.alchemy.com> Message-ID: <333efb450812021249u6c35c8c2g385efef39450552@mail.gmail.com> On Tue, Dec 2, 2008 at 2:02 PM, Steve Willoughby wrote: > > That depends on what GUI toolkit you're using. If you're > going to be doing a lot with sound and images, particularly > video clips, you might want to look at pymedia or pygame Pyglet is another option. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeremiah.jester at panasonic.aero Tue Dec 2 22:08:09 2008 From: jeremiah.jester at panasonic.aero (Jeremiah Jester) Date: Tue, 02 Dec 2008 13:08:09 -0800 Subject: [Tutor] dictionary looping problem Message-ID: <1228252089.16380.11.camel@M13425> Hello, I'm trying to gather a list of files and md5 hash them to do a checksum. I've created a function for each dictionary. However, when i print out the dictionary I don't get all the items. Any ideas? Thanks< JJ CODE: #!/usr/bin/python import os import md5 import sys source={} target={} source_path="/path/source" target_path="/path/target" def gather_files(dir,dict): os.chdir(dir) names=os.listdir(dir) for filename in names: hash=os.system("md5 "+ filename) dict[hash] = filename print dict gather_files(source_path,source) gather_files(target_path,target) OUTPUT: # python new.py b481bb58b296a62d744d3006d0156f61 1 e07910a06a086c83ba41827aa00b26ed 3 749a92a5ba327db2f2711b1a8d3cc0ab 2 {0: '2'} b481bb58b296a62d744d3006d0156f61 1 e6139f93eb47c9fe7eb7fc5ddb586511 3 7ce1a3a4baf75dea4397e36c97e1fc0b 2 {0: '2'} Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. From steve at alchemy.com Tue Dec 2 22:25:34 2008 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 2 Dec 2008 13:25:34 -0800 Subject: [Tutor] dictionary looping problem In-Reply-To: <1228252089.16380.11.camel@M13425> References: <1228252089.16380.11.camel@M13425> Message-ID: <20081202212534.GD16958@dragon.alchemy.com> On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > Hello, > > I'm trying to gather a list of files and md5 hash them to do a checksum. > I've created a function for each dictionary. However, when i print out > the dictionary I don't get all the items. Any ideas? Yep. Don't use os.system() there. 1. you're running the "md5" program externally when you don't need to, since Python has the ability to compute md5 checksums on its own, which you already know because you imported that module at the top of your script (and then didn't use). 2. The return value from os.system() is NOT the hash, so what you're storing in the dictionary is not going to be that unique, and so each call which yields the same return value (0, usually) will overwrite that element in the dict. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From jeremiah.jester at panasonic.aero Tue Dec 2 23:26:40 2008 From: jeremiah.jester at panasonic.aero (Jeremiah Jester) Date: Tue, 02 Dec 2008 14:26:40 -0800 Subject: [Tutor] dictionary looping problem In-Reply-To: <20081202212534.GD16958@dragon.alchemy.com> References: <1228252089.16380.11.camel@M13425> <20081202212534.GD16958@dragon.alchemy.com> Message-ID: <1228256800.16380.17.camel@M13425> Thanks for clearing this up for me. On Tue, 2008-12-02 at 13:25 -0800, Steve Willoughby wrote: > On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > > Hello, > > > > I'm trying to gather a list of files and md5 hash them to do a > checksum. > > I've created a function for each dictionary. However, when i print > out > > the dictionary I don't get all the items. Any ideas? > > Yep. Don't use os.system() there. > > 1. you're running the "md5" program externally when you don't need to, > since Python has the ability to compute md5 checksums on its own, > which you already know because you imported that module at the top > of your script (and then didn't use). > > 2. The return value from os.system() is NOT the hash, so what you're > storing in the dictionary is not going to be that unique, and so > each call which yields the same return value (0, usually) will > overwrite that element in the dict. > > -- > Steve Willoughby | Using billion-dollar satellites > steve at alchemy.com | to hunt for Tupperware. > > > Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. From alan.gauld at btinternet.com Wed Dec 3 01:02:14 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 00:02:14 -0000 Subject: [Tutor] sqlite3 Python25 parameter binding problem withUPDATEplease help References: Message-ID: "aivars" wrote > Finally I managed to get it working! > > I had to remove [year] braces from the second argument - year. You really need to do some reading on basicv Python before you go any further, otherwise you will never understand what you are doing. That is what I told you was wrong in the first place - you can't pass a list of values (even of one value) into a database field. But you obviously didn't realize that putting square brackets (not braces which are {} and signify a dictionary) indicates a list. > reason I used [] was the posts we exchanged recently (7th of > November) > about CGI script where it was recommended by Kent that I put [] > around > string argument: But that was an entirely different scenario, again you need to do some basic level reading so that you understand what we are telling you. Blindly using "poke and hope" techniques leads to an excercise in frustration for you and us both. > The second argument to execute() is a *sequence* of parameter > values. > A string is a sequence of characters, so by passing a plain string > you > are saying that each character of the string is a parameter to the > SQL. > > Try adding braces around the parameter to make list: > cur.execute("insert into test (name) values (?)", [sPath]) Notice, Kent told you to "make a list" by putting[] around your string. But in that case the arument was looking for a sequence (eg list) of values. In your case you were trying to insert a list of values into a single field which you can't do in normal SQL > Now the year argument is string so I thought I should do the same > again but it is not working. Yes but it is a single string so you need to pass a single string. In the previous case the SQL was looking for a list of arguments but you passed a single string so the execute interpreted that as a list of letters. You must ensure that the arguments in the execute match what the SQL is expecting. And that usually means both matching what you defined the database to be and the nature of the operation. > The reason is of course is that I am new to python and there are so > many things to learn so the question really is why in this case > there > were no [] or () necessary? See above, but you really need to take the time out to understand the basics of Python. Try reading my tutorial, in particular the "raw materials" topic which covers the different Python data types. Then try my database topic near the end. But ideally just take a few hours - literally an afternoon will do - to go through any beginners tutorial. They will all cover these elementary issues. >>>> Perhaps but I think it is the list parameter that it doesn't >>>> like. >>>> Unless I misunderstand the syntax. >>>> >>>> -- >>>> Alan Gauld >>>> Author of the Learn to Program web site >>>> http://www.freenetpages.co.uk/hp/alan.gauld Alan G. From alan.gauld at btinternet.com Wed Dec 3 01:09:40 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 00:09:40 -0000 Subject: [Tutor] dictionary looping problem References: <1228252089.16380.11.camel@M13425> Message-ID: "Jeremiah Jester" wrote > I've created a function for each dictionary. However, when i print > out > the dictionary I don't get all the items. Any ideas? You should only get a very small number indeed! > for filename in names: > hash=os.system("md5 "+ filename) > dict[hash] = filename os.system only returns the exit code of the command not the output of the command. In most cases that will be 0 to indicate no errors. You need to use the subprocess module (or any of the older alternatives such as popen) to fetch the actual output of the command from stdout and assign it to hash. But better still use one of the python modules to do the hashing instead of calling the OS! I think the hashlib module's md5 function does what you want. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From juryef at yahoo.com Wed Dec 3 02:08:21 2008 From: juryef at yahoo.com (Judith Flores) Date: Tue, 2 Dec 2008 17:08:21 -0800 (PST) Subject: [Tutor] Making a dictionary of dictionaries from csv file Message-ID: <811719.59207.qm@web34707.mail.mud.yahoo.com> Dear Python community, I have been trying to create a dictionary of dictionaries (and more dictionaries) from a csv file. The csv file contains longitudinal data corresponding to names. The following is just a very (very) simple example of how the data looks: NameDayweighttemp name114537 name135536 name215936 name233436.5 name316637 name338736.8 So far I have written this: from csv import * f=open('myfile.csv',"rt") row={} maindict={} reader=DictReader(f) for row in reader: maindict[row['Name']=row then I can access the weight of a given name like this: wg=int(maindict[['name1']['weight']) My question is the following: How can I convert the csv to a dictionary that would have the following structure? maindict = { 'name1' : { 'Day' : { 1 : { 'weight' : '45', 'temp' : '37' } , 3 : { 'weight' : '55', 'temp' : '36' } } }, 'name2' : { ..... # and we repeat the same structure for the rest of the names. } >From my code above you can of course guess that it doesn't make beyond the level of name. Thank you very much, Judith From aivars868 at gmail.com Wed Dec 3 06:43:39 2008 From: aivars868 at gmail.com (aivars) Date: Wed, 3 Dec 2008 07:43:39 +0200 Subject: [Tutor] sqlite3 Python25 parameter binding problem withUPDATEplease help In-Reply-To: References: Message-ID: Thanks, Alan, for your advice. I fully agree with you - I still need to understand basic Python (I actually warned that I am a complete noob) and how it works. And probably I need a couple of goof books. I once told you that I consider your website an excellent one and I am going there from time to time and will do it again. Maybe I am trying to do the things which are too advanced for my level of python knowledge - databases, web development, which I understand are considered as advanced topics but the reason is that for me I need a real project to work with to learn a programming language. That was the case also with VBA (visual basic for applications) - learning for the sake of learning almost never worked with me. It does not mean I am an expert in VBA - far from it! But I had this project working with MS Excel VBA+ MS SQL Server Express 2005 T-SQL - an automated financial accounting consolidated reporting application. I am pumping data from two remote Interbase/Firebird databases, putting it in a local MS SQL Server DB, processing with T-SQL and doing the reporting with Excel. Now I am working to do the same with Python and SQLite at the same time learning on my way them both. And if you work on a real thing there are many bits and pieces which are coming up - and its not like this 'spam' and 'egg' thing all the time. Also I am completely self-taught in programming. I see what was my problem in particular in this case and I understand it now. And I will try not to bother people on this list too often. Or If I do please feel free tell me to !@#$% off and do some more reading! Again, thank you Alan for the help! Aivars 2008/12/3 Alan Gauld : > "aivars" wrote > >> Finally I managed to get it working! >> >> I had to remove [year] braces from the second argument - year. > > You really need to do some reading on basicv Python before > you go any further, otherwise you will never understand what > you are doing. > > That is what I told you was wrong in the first place - you can't > pass a list of values (even of one value) into a database field. > But you obviously didn't realize that putting square brackets > (not braces which are {} and signify a dictionary) indicates > a list. > >> reason I used [] was the posts we exchanged recently (7th of November) >> about CGI script where it was recommended by Kent that I put [] around >> string argument: > > But that was an entirely different scenario, again you need to do some > basic level reading so that you understand what we are telling you. > Blindly using "poke and hope" techniques leads to an excercise > in frustration for you and us both. > >> The second argument to execute() is a *sequence* of parameter values. >> A string is a sequence of characters, so by passing a plain string you >> are saying that each character of the string is a parameter to the >> SQL. >> >> Try adding braces around the parameter to make list: >> cur.execute("insert into test (name) values (?)", [sPath]) > > Notice, Kent told you to "make a list" by putting[] around > your string. But in that case the arument was looking for a sequence > (eg list) of values. In your case you were trying to insert a list of values > into a single field which you can't do in normal SQL > >> Now the year argument is string so I thought I should do the same >> again but it is not working. > > Yes but it is a single string so you need to pass a single string. > In the previous case the SQL was looking for a list of arguments > but you passed a single string so the execute interpreted that > as a list of letters. > > You must ensure that the arguments in the execute match > what the SQL is expecting. And that usually means both matching > what you defined the database to be and the nature of the operation. > >> The reason is of course is that I am new to python and there are so >> many things to learn so the question really is why in this case there >> were no [] or () necessary? > > See above, but you really need to take the time out to understand > the basics of Python. > > Try reading my tutorial, in particular the "raw materials" topic > which covers the different Python data types. Then try my database > topic near the end. But ideally just take a few hours - literally an > afternoon will do - to go through any beginners tutorial. They will > all cover these elementary issues. > > >>>>> Perhaps but I think it is the list parameter that it doesn't like. >>>>> Unless I misunderstand the syntax. >>>>> >>>>> -- >>>>> Alan Gauld >>>>> Author of the Learn to Program web site >>>>> http://www.freenetpages.co.uk/hp/alan.gauld > > > Alan G. > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Wed Dec 3 09:20:25 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 08:20:25 -0000 Subject: [Tutor] sqlite3 Python25 parameter binding problemwithUPDATEplease help References: Message-ID: "aivars" wrote > I fully agree with you - I still need to understand basic Python (I > actually warned that I am a complete noob) and how it works. And > probably I need a couple of goof books. To be honest you don;t need many books on Python, the web resources are adequate. Pesonally I only use about 4 or 5 books asnd they are almost all on specific topics: Tkinter, wxPythn,TurboGears, Networks etc. Python in a Nutshell is the only general book I use as a reference. > Maybe I am trying to do the things which are too advanced for my > level Just on the edge given your knowledge level. > are considered as advanced topics but the reason is that for me I > need > a real project to work with to learn a programming language. Thats a good thing. But be aware of the dangers - you will learn a lot of bad habits that will cause you problems later. Its liker learning to play a musical instrument by ear just by puicking out the notes of your favourite tune. Unless you learn chords and proper finger positions etc you will never get beyond being a beginner/intermediate level. Same with programming, you do need to knuckle down and learn some of the basic theory and principles if you ever want to progress beyond that stage. > Also I am completely self-taught in programming. Not in itself a problem provided you do take the time to actually learn rather than just picking up bits n pieces ad hoc. > it now. And I will try not to bother people on this list too often. Its not the frequency of asking thats an issue, we don't ,mind answering questions, thats what we are here for. But do take the time to *understand* the answer, don't just think "OK I'll stick [] around values and that'll mayubermake it work" Take the time to really understand why [] worked in the first case but actually caused the problem in the second. And if you don't understand, ask for the explanation. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Wed Dec 3 09:34:00 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 08:34:00 -0000 Subject: [Tutor] Making a dictionary of dictionaries from csv file References: <811719.59207.qm@web34707.mail.mud.yahoo.com> Message-ID: "Judith Flores" wrote > I have been trying to create a dictionary of dictionaries > (and more dictionaries) from a csv file. Your code below suffers from sloppiness in the number of [] which makes it hard to know exactly what the problem might be. But in principle what you are doing is fine and should work beyond 2 levels. However I would suggest that it might be esier to use classes instead of dictionaries. Dictionaries are fine for many things but can become a bit tortuous to navigate and maintain. > NameDayweighttemp > name114537 > name135536 > name215936 I assume there should be some commas (or other separators) in there? Othewise how do you expect the csv module to separate the data? > row={} > maindict={} > reader=DictReader(f) > > for row in reader: > maindict[row['Name']=row I assume you have a second closing ] after Name? Also you are overwriting the row for each name. So you only store the last entry. So you need to separate the data further. Something like maindict[row[name]] [row[day]] = (row.weight, row,temp) Which would look like {name1: {day1: (w,t), day2 : (w,t), day2: (w,t)}, name2: {day3: (w,t).... Which looks a bit like what you want I think? > then I can access the weight of a given name like this: > > wg=int(maindict[['name1']['weight']) I assume thats a single open { before name1? > My question is the following: > > How can I convert the csv to a dictionary that would have the > following structure? > > maindict = { > > 'name1' : { > 'Day' : { > > 1 : { 'weight' : '45', 'temp' : '37' } , > > 3 : { 'weight' : '55', 'temp' : '36' } > > } > > }, See above HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Wed Dec 3 12:43:01 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Dec 2008 06:43:01 -0500 Subject: [Tutor] Making a dictionary of dictionaries from csv file In-Reply-To: References: <811719.59207.qm@web34707.mail.mud.yahoo.com> Message-ID: <1c2a2c590812030343n7007f488v433c3608c61ce627@mail.gmail.com> On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld wrote: > Also you are overwriting the row for each name. So you only > store the last entry. So you need to separate the data further. > Something like > > maindict[row[name]] [row[day]] = (row.weight, row,temp) This will not quite work. It will give KeyErrors because the second-level dicts don't exist in maindict. One way to fix this is to use collections.defaultdict: from collections import defaultdict maindict = defaultdict(dict) Now maindict uses an empty dictionary as its default value. For another level of nesting (the 'Day' level you show in your example) this becomes a bit more complex, see this discussion and the link: http://thread.gmane.org/gmane.comp.python.tutor/46006 Kent From roadierich at googlemail.com Wed Dec 3 14:57:42 2008 From: roadierich at googlemail.com (Rich Lovely) Date: Wed, 3 Dec 2008 13:57:42 +0000 Subject: [Tutor] Making a dictionary of dictionaries from csv file Message-ID: <992203C7-FE30-460B-B730-61C5AAB9DB03@googlemail.com> How about this: def recursiveDictFactory(): return defaultdict(recursiveDictFactory) dictOfDictsOfDictsEtc = defaultdict(recursiveDictFactory) --- Richard "Roadie Rich" Lovely Part of the JNP|UK Famille www.theJNP.com (Sent from my iPod - please allow me a few typos: it's a very small keyboard) On 3 Dec 2008, at 11:43, "Kent Johnson" wrote: > On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld > wrote: > >> Also you are overwriting the row for each name. So you only >> store the last entry. So you need to separate the data further. >> Something like >> >> maindict[row[name]] [row[day]] = (row.weight, row,temp) > > This will not quite work. It will give KeyErrors because the > second-level dicts don't exist in maindict. One way to fix this is to > use collections.defaultdict: > from collections import defaultdict > maindict = defaultdict(dict) > > Now maindict uses an empty dictionary as its default value. > > For another level of nesting (the 'Day' level you show in your > example) this becomes a bit more complex, see this discussion and the > link: > http://thread.gmane.org/gmane.comp.python.tutor/46006 > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From timmichelsen at gmx-topmail.de Wed Dec 3 22:46:44 2008 From: timmichelsen at gmx-topmail.de (Tim Michelsen) Date: Wed, 03 Dec 2008 22:46:44 +0100 Subject: [Tutor] using windows wide proxy settings Message-ID: Hello, is there any possibility in python to retrieve the system wide internet connection settings? I would like to access the proxy settings stored in Internet Explorer -> Extras -> Options -> Connection -> LAN settings. This would later be used by urllib. My aim is not to bother the user with another place to set the proxy when being behind a corporate firewall. Thanks in advance & regards, Timmie From denis.spir at free.fr Wed Dec 3 22:35:03 2008 From: denis.spir at free.fr (spir) Date: Wed, 03 Dec 2008 22:35:03 +0100 Subject: [Tutor] [Fwd: Re: Making a dictionary of dictionaries from csv file] Message-ID: <4936FB87.9000104@free.fr> Judith Flores a ?crit : > Dear Python community, > > I have been trying to create a dictionary of dictionaries (and more dictionaries) from a csv file. The csv file contains longitudinal data corresponding to names. The following is just a very (very) simple example of how the data looks: > > NameDayweighttemp > name114537 > name135536 > name215936 > name233436.5 > name316637 > name338736.8 Apart from the lack of ',', I'm not really sure to understand the logical structure of you data: for instance, can there be the same day for several names? If not, then the problem is simpler as the day uniquely identifiesyour data. Anyway, it seems clear that a (name+day) key is unique for a (weight,temp) data pair. right? > So far I have written this: > > from csv import * > > f=open('myfile.csv',"rt") > > row={} > maindict={} > > reader=DictReader(f) > > for row in reader: > maindict[row['Name']=row > > > then I can access the weight of a given name like this: > > wg=int(maindict[['name1']['weight']) > > > > My question is the following: > > How can I convert the csv to a dictionary that would have the following structure? > > maindict = { > 'name1' : { > 'Day' : { > 1 : { 'weight' : '45', 'temp' : '37' } , > 3 : { 'weight' : '55', 'temp' : '36' } > } > }, > 'name2' : { ..... # and we repeat the same structure for the rest of the names. > } >>From my code above you can of course guess that it doesn't make beyondthe > > level of name. If I understand well, you can clarify your problem for instance with such a data type (all pseudo code, untested): class Measure(object): def __init__(self,weight,temp): self.weight = weight self.temp = temp This will remove one dict level. You can create a data object x = Measure(weight,temp) which weight and temp attributes can be set, changed, & read individually using 'x.weight' and 'x.temp'. Much nicer, I think. At higher level, you are then able to put such measure objects in a simple dict with day keys and Measure values. So that an individual Measure object will actually be accessed as a dict value, e.g.: day_measures = {day:measure, day:measure...} measure = day_measures[day] temp = day_measures[day].temp (=measure.temp) At top-level, you then need a construct to hold the set of name data objects. As they are named, it could be a nesting dictionary of (name:day_measures) pairs. This will be especially efficient if you frequently need accessing the data through the 'name' key. An alternative (which I would chose to avoid nested dicts) is to use the python ability to create attributes which names are known at runtime only, using the setattr() built-in function: setattr(obj,name,val) will give the object 'obj' an attribute which full name will be obj.name, and value will be val. Very nice (*). That way, you can have an overall Data object (chose an appropriate name) with a whole bunch of attributes called by their 'name' and which individual value is a day_measures dict: data name1 : day_measures dict name2 : day_measures dict ... You need to create an attribute for each set of (named) day_measures dict you read from the CSV file. class Data(object): pass data = Data() # new empty object while not end_of_file: # create attribute to store the measures into data object setattr(data, name, dict) # done Now, from the outest scope, an individual data item is spelled e.g.: data.name[day].temp (which I find much more legible) > Thank you very much, > > Judith Hope I haven't totally miusunderstood your problem. Salutation, denis (*) The ordinary way to create an attr beeing to write obj.name = value you need to know the name at coding time. The setattr() syntax instead allows to set the name at runtime like a dict key. So that any ordinary object becomes an alternative to dicts. [A major redondance, indeed -- I personly do not really know anymore what the proper use of a dict should be. Even less, as attr are implemented asdict, there shouldn't even be a performance loss. What do you think, list members?] From alan.gauld at btinternet.com Thu Dec 4 00:20:16 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 23:20:16 -0000 Subject: [Tutor] using windows wide proxy settings References: Message-ID: "Tim Michelsen" wrote > is there any possibility in python to retrieve the system wide > internet connection settings? Not as such, but Python does provide the hooks to get it from the OS. > I would like to access the proxy settings stored in > Internet Explorer -> Extras -> Options -> Connection -> LAN > settings. Assuming IE implies Windows and not MacOS you can read the registry using the Windows API or alternativey you can use Windows Script Host and access it via the Registry object. In either case you will need the Pythonwin extensions or ctypes. Its also possible to get at most(all?) of what you need via the subprocess module and the Windows command line tools. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Thu Dec 4 00:23:03 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 3 Dec 2008 23:23:03 -0000 Subject: [Tutor] using windows wide proxy settings References: Message-ID: "Tim Michelsen" wrote > is there any possibility in python to retrieve the system wide > internet connection settings? Further to my earlier mail I had a thought and sure enough there is a winreg module that makes accessing the registry much easier direct from Python. Now all you need to do is find the right keys in the registry... and ninternet search shild throw it up or just search for a key definition using regedit.. Batteries included... Alan G From john at fouhy.net Thu Dec 4 00:35:03 2008 From: john at fouhy.net (John Fouhy) Date: Thu, 4 Dec 2008 12:35:03 +1300 Subject: [Tutor] using windows wide proxy settings In-Reply-To: References: Message-ID: <5e58f2e40812031535lc402c44s652b3f87fabef436@mail.gmail.com> On 04/12/2008, Tim Michelsen wrote: > Hello, > is there any possibility in python to retrieve the system wide internet > connection settings? > > I would like to access the proxy settings stored in > Internet Explorer -> Extras -> Options -> Connection -> LAN settings. Install pythonwin, then look here: http://www.microsoft.com/technet/scriptcenter/scripts/python/network/client/list/nwlspy02.mspx?mfr=true General Microsoft Windows python script repository: http://www.microsoft.com/technet/scriptcenter/scripts/python/default.mspx?mfr=true HTH! -- John. From mwalsh at mwalsh.org Thu Dec 4 00:37:49 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Wed, 03 Dec 2008 17:37:49 -0600 Subject: [Tutor] using windows wide proxy settings In-Reply-To: References: Message-ID: <4937184D.8060304@mwalsh.org> Tim Michelsen wrote: > Hello, > is there any possibility in python to retrieve the system wide internet > connection settings? > > I would like to access the proxy settings stored in > Internet Explorer -> Extras -> Options -> Connection -> LAN settings. I wouldn't believe it if I didn't just see it work this way, but... On windows, proxy settings configured the way you describe above (if no auth is required) are retrieved from the registry and used automatically by urllib(2).urlopen. from the urllib docs, http://www.python.org/doc/2.5.2/lib/module-urllib.html """ The urlopen() function works transparently with proxies which do not require authentication. In a Unix or Windows environment, set the http_proxy, ftp_proxy or gopher_proxy environment variables to a URL that identifies the proxy server before starting the Python interpreter. For example (the "%" is the command prompt): ... In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry's Internet Settings section. """ The urllib and urllib2 modules also provide a helper function for retrieving proxy info... >>> import urllib2 >>> urllib2.getproxies() {'ftp': 'ftp://10.0.0.100:8888', 'http': 'http://10.0.0.100:8888'} HTH, Marty From lawrence.wickline at gmail.com Thu Dec 4 01:58:14 2008 From: lawrence.wickline at gmail.com (Lawrence Wickline) Date: Wed, 3 Dec 2008 16:58:14 -0800 Subject: [Tutor] Sorting a dictionary on a value in a list. In-Reply-To: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> Message-ID: I am working on a reducer that needs to produce a sorted output of files sorted on their overall bandwidth use. I create a dictionary with the file name as the key (it is always unique) and in the values I am populating a list with the two values of bytes and bytes sent. Each entry looks like {filename:[bytes, bytes_sent]} how would I sort on bytes sent? how would I make this more efficient? code: # Expect as input: # URI,1,return_code,bytes,referer,ip,time_taken,bytes_sent,ref_dom # index 0 1 2 3 4 5 6 7 8 import sys dict = {} def update_dict(filename, bytes, bytes_sent): # Build and update our dictionary adding total bytes sent. if dict.has_key(filename): bytes_sent += dict[filename][1] dict[filename] = [bytes, bytes_sent] else: dict[filename] = [bytes, bytes_sent] # input comes from STDIN for line in sys.stdin: # remove leading and trailing whitespace and split on tab words = line.rstrip().split('\t') file = words[0] bytes = words[3] bytes_sent = int(words[7]) update_dict(file, bytes, bytes_sent) -------------- next part -------------- An HTML attachment was scrubbed... URL: From michele.alzetta at gmail.com Thu Dec 4 02:03:03 2008 From: michele.alzetta at gmail.com (Michele Alzetta) Date: Thu, 4 Dec 2008 02:03:03 +0100 Subject: [Tutor] Graph theory Message-ID: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> Hallo all! It's a long time since I last wrote here. I have been thinking about a problem, and I'm wondering what the best approach for a pythonic solution would be. The actual problem is very complex, but the very first step in the solution would be to come up with a simple way of handling graphs. For example, given that: Definition 1: A network is a figure made up of points (vertices) connected by non-intersecting curves (arcs). Definition 2: A vertex is called odd if it has an odd number of arcs leading to it, other wise it is called even. Definition 3: An Euler path is a continuous path that passes through every arc once and only once. Given the following theorems: Theorem 1: If a network has more than two odd vertices, it does not have an Euler path. Theorem 2: If a network has two or zero odd vertices, it has at least one Euler path. In particular, if a network has exactly two odd vertices, then its Euler paths can only start on one of the odd vertices, and end on the other -- (Euler circuit). Which would be the best approach for representing figures such as those found here ( http://mathforum.org/isaac/problems/bridges2.html ) such that one could elaborate a script capable of finding and describing paths in the figure? For example, Euler paths. I realize that it is quite feasable to do this, but would take a lot of coding - vertices and arcs could be represented as instances of respective classes, and so forth. But is there an elegant and simple solution already out there? -- Michele Alzetta From john at fouhy.net Thu Dec 4 02:16:56 2008 From: john at fouhy.net (John Fouhy) Date: Thu, 4 Dec 2008 14:16:56 +1300 Subject: [Tutor] Graph theory In-Reply-To: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> References: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> Message-ID: <5e58f2e40812031716u2aa5b4a5ide2d1f743534f7f@mail.gmail.com> On 04/12/2008, Michele Alzetta wrote: > I have been thinking about a problem, and I'm wondering what the best > approach for a pythonic solution would be. > The actual problem is very complex, but the very first step in the > solution would be to come up with a simple way of handling graphs. Interestingly, Guido wrote an essay on implementing graphs in python using dictionaries and lists: http://python.org/doc/essays/graphs/ I'm sure if you googled around you will be able to find more full-featured graph libraries (unless, of course, building your own is part of the attraction :-) ). -- John. From kent37 at tds.net Thu Dec 4 02:42:47 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Dec 2008 20:42:47 -0500 Subject: [Tutor] Sorting a dictionary on a value in a list. In-Reply-To: References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> Message-ID: <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> On Wed, Dec 3, 2008 at 7:58 PM, Lawrence Wickline wrote: > how would I sort on bytes sent? You can't actually sort a dictionary; what you can do is sort the list of items. In this case each item will look be a tuple (filename, (bytes, bytes_sent)) and dict.items() will be a list of such tuples. The best way to sort a list is to make a key function that extracts a key from a list item, then pass that to the list sort() method. In your case, you want to extract the second element of the second element, so you could use the function def make_key(item): return item[1][1] Then you can make a sorted list with sorted(dict.items(), key=make_key) > how would I make this more efficient? It looks pretty good to me. A few minor notes below. > code: > > # Expect as input: > # URI,1,return_code,bytes,referer,ip,time_taken,bytes_sent,ref_dom > # index 0 1 2 3 4 5 6 7 8 > > import sys > > > dict = {} Don't use dict as the name of a variable, it shadows the built-in dict() function. > def update_dict(filename, bytes, bytes_sent): > # Build and update our dictionary adding total bytes sent. > if dict.has_key(filename): > bytes_sent += dict[filename][1] > dict[filename] = [bytes, bytes_sent] > else: > dict[filename] = [bytes, bytes_sent] If you really want to squeeze every bit of speed, filename in dict is probably faster than dict.has_key(filename) and you might try also using a try / catch block instead of has_key(). You could also try passing dict as a parameter, that might be faster than having it as a global. None of these will matter unless you have many thousand lines of input. How many lines do you have? How long does it take to process? > # input comes from STDIN > for line in sys.stdin: > # remove leading and trailing whitespace and split on tab > words = line.rstrip().split('\t') rstrip() removes only trailing white space. It is not needed since you don't use the last field anyway. > file = words[0] > bytes = words[3] > bytes_sent = int(words[7]) > update_dict(file, bytes, bytes_sent) If you put all this into a function it will run a little faster. Kent From kent37 at tds.net Thu Dec 4 02:46:21 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Dec 2008 20:46:21 -0500 Subject: [Tutor] using windows wide proxy settings In-Reply-To: <4937184D.8060304@mwalsh.org> References: <4937184D.8060304@mwalsh.org> Message-ID: <1c2a2c590812031746t5da5dd00g1b35bb53bfcf7f79@mail.gmail.com> On Wed, Dec 3, 2008 at 6:37 PM, Martin Walsh wrote: > The urllib and urllib2 modules also provide a helper function for > retrieving proxy info... > >>>> import urllib2 >>>> urllib2.getproxies() > {'ftp': 'ftp://10.0.0.100:8888', 'http': 'http://10.0.0.100:8888'} And, since urllib is a Python module, you can easily look at the source to see where it comes from. Kent From kent37 at tds.net Thu Dec 4 02:49:12 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 3 Dec 2008 20:49:12 -0500 Subject: [Tutor] Graph theory In-Reply-To: <5e58f2e40812031716u2aa5b4a5ide2d1f743534f7f@mail.gmail.com> References: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> <5e58f2e40812031716u2aa5b4a5ide2d1f743534f7f@mail.gmail.com> Message-ID: <1c2a2c590812031749w718bdbb6ybd4d2e844b225278@mail.gmail.com> On Wed, Dec 3, 2008 at 8:16 PM, John Fouhy wrote: > Interestingly, Guido wrote an essay on implementing graphs in python > using dictionaries and lists: > > http://python.org/doc/essays/graphs/ Also googling "python graph" finds many alternatives. Kent From jhodgen at asymmetricsupplychain.com Thu Dec 4 07:21:45 2008 From: jhodgen at asymmetricsupplychain.com (Jim Hodgen) Date: Wed, 03 Dec 2008 22:21:45 -0800 Subject: [Tutor] Confused about __setitem__ Message-ID: <493776F9.4060403@asymmetricsupplychain.com> I am learning Python with a minesweeper-engine project. Future activities turn my attention to exploring the use of dictionaries as sparse matrices, hence the representation of the gameboard. Why can't I fill the dictionary-as-a-matrix with an instance of class Square? Why do I need a custom method to make the assignment? What am I not understanding? *_# basics module (mineswobj1)contents..._* class Square: mined = True # False if empty, True if mined, no other values allowed marked = 0 # False if 0, marked-as-mined if 1, marked-as-unknown if 2, no other values allowed displaystate = 0 # covered if 0, marked if 1, exposed if 2, selected if 3, no other values allowed minedneighbors = 0 # number of mined neighboring squares, max legit value = 8 class Baseboard: xdimension = 0 # x-dimension of the gameboard, integer value greater than 0 ydimension = 0 # y-dimension of the gameboard, integer value greater than 0 minenumber = 0 # number of mines on the board for play, value from 0 to (xdimension * ydimension) state = 0 # playable if 0, not-playable-exploded if 1, not-playable-solved if 2 boardsquares = {} # dictionary used to emulate an addressable 2 dimensional grid where an instance of class Square # is inserted with a key consisting of a tuple describing its zero-based address in the grid def initializeboard(xdim, ydim, minenum): gameboard = Baseboard() for yct in range(ydim): for xct in range(xdim): gameboard[(xct,yct)] = Square() print 'gameboard[(',xct,yct, ')] =', gameboard[(xct,yct)] *_# driver module contents_* import basics mineswobj1.initializeboard(3, 4, 5) _*#error message*_ Traceback (most recent call last): File "C:\jmh\Python\minedriver0.py", line 6, in mineswobj1.initializeboard(3, 4, 5) File "C:\jmh\Python\mineswobj1.py", line 26, in initializeboard gameboard[(xct,yct)] = Square() AttributeError: Baseboard instance has no attribute '__setitem__' -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmichelsen at gmx-topmail.de Thu Dec 4 09:26:19 2008 From: timmichelsen at gmx-topmail.de (Timmie) Date: Thu, 4 Dec 2008 08:26:19 +0000 (UTC) Subject: [Tutor] using windows wide proxy settings References: <4937184D.8060304@mwalsh.org> <1c2a2c590812031746t5da5dd00g1b35bb53bfcf7f79@mail.gmail.com> Message-ID: Hello all, thanks a lot for your answers. > > The urllib and urllib2 modules also provide a helper function for > > retrieving proxy info... > > > >>>> import urllib2 > >>>> urllib2.getproxies() > > {'ftp': 'ftp://10.0.0.100:8888', 'http': 'http://10.0.0.100:8888'} This seems to be the fastest method. Very neat. Since we use pac scripts I refined my search. Here is another module tailored to this: http://code.google.com/p/pacparser There is a python example at the wiki: http://code.google.com/p/pacparser/wiki/PacFilesInPythonWebclients It seems that the script can even test if connection is available. I was looking for this earlier. KInd regards, Timmie From alan.gauld at btinternet.com Thu Dec 4 09:54:46 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 4 Dec 2008 08:54:46 -0000 Subject: [Tutor] Confused about __setitem__ References: <493776F9.4060403@asymmetricsupplychain.com> Message-ID: "Jim Hodgen" wrote > Why can't I fill the dictionary-as-a-matrix with an instance of > class > Square? You can but you are not trying to do that in your code. You are assigning Square to the class that contains the dictionary, not the dictionary itself. > class Square: > mined = True # False if empty, True if mined, no > other > marked = 0 # False if 0, marked-as-mined if 1, > displaystate = 0 # covered if 0, marked if 1, exposed if > 2, > minedneighbors = 0 # number of mined neighboring squares, max > legit These are all class level variables so they will be shared if you create more than one instance of the class. That's probably not what you want?. You should initialise them inside an __init__ method if you want each square to have its own values. > class Baseboard: > boardsquares = {} # dictionary used to emulate an addressable 2 boardsquares is the dictionary not Baseboard > def initializeboard(xdim, ydim, minenum): > gameboard = Baseboard() > for yct in range(ydim): > for xct in range(xdim): > gameboard[(xct,yct)] = Square() So this needs to be gameboard.boardsquares[(xct,yct)] = Square() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Thu Dec 4 12:39:08 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 4 Dec 2008 06:39:08 -0500 Subject: [Tutor] Confused about __setitem__ In-Reply-To: References: <493776F9.4060403@asymmetricsupplychain.com> Message-ID: <1c2a2c590812040339r6334004o3a2daed06d7388a7@mail.gmail.com> On Thu, Dec 4, 2008 at 3:54 AM, Alan Gauld wrote: > These are all class level variables so they will be shared if you > create more than one instance of the class. That's probably > not what you want?. You should initialise them inside an __init__ > method if you want each square to have its own values. And likewise for the board variables. Kent From prasadaraon50 at gmail.com Thu Dec 4 15:46:04 2008 From: prasadaraon50 at gmail.com (prasad rao) Date: Thu, 4 Dec 2008 20:16:04 +0530 Subject: [Tutor] To print docstrings Message-ID: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> Hello friends. I am new to programing.I am learning Python. I failed in my attempts to retrive doc strings of methods in a module. """ for x in dir(logging): print x,x.__doc__ ============= for x in dir(collections): print x,collections.x.__doc__ ================== >>> def dd(o): zx=dir (o) for x in zx: ax=o+x print ax.__doc__ >>> dd(collections) ================ def dd(o): zx=dir (o) for x in zx: ax=str(o)+x print ax.__doc__ >>> >>> dd(collections) """ Above snippets of code generates str.__doc__ for every iteration. Please someone tell me how I can retrive all the docstrings ina module thanks in advance Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From a.t.hofkamp at tue.nl Thu Dec 4 16:10:45 2008 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Thu, 04 Dec 2008 16:10:45 +0100 Subject: [Tutor] To print docstrings In-Reply-To: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> References: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> Message-ID: <4937F2F5.5070800@tue.nl> prasad rao wrote: > Hello friends. > I am new to programing.I am learning Python. > I failed in my attempts to retrive doc strings of methods in > a module. > """ > for x in dir(logging): > print x,x.__doc__ > ============= > for x in dir(collections): > print x,collections.x.__doc__ > ================== I am not sure what you are aiming for. If you just want to collect them for reading, http://docs.python.org/library/ is much easier. If you want to produce documentation of your own modules, you may want to take a look at pydoc or epydoc. If you want to collect the doc strings just for the sake of it, add them in a list instead of printing: docs = [] for x in dir(collections): docs.append(x.__doc__) or in one line: docs = [x.__doc__ for x in dir(collections)] if you want to make a single (possibly long string of text from that list, join the strings together as in text = "\n".join(docs) or all in one line (with a generator to suppress creation of the intermediate list): text = "\n".join(x.__doc__ for x in dir(collections)) >>>> def dd(o): > zx=dir (o) > for x in zx: > ax=o+x > print ax.__doc__ > I have no idea what you are doing with 'ax' here. You seem to be adding a module and one of its functions and assign the result to ax, an operation I don't understand. If it is important for you, could you please provide some explanation of what you trying to do here? Sincerely, Albert From kent37 at tds.net Thu Dec 4 16:17:50 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 4 Dec 2008 10:17:50 -0500 Subject: [Tutor] To print docstrings In-Reply-To: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> References: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> Message-ID: <1c2a2c590812040717q2225ef98n422e441e79dae946@mail.gmail.com> On Thu, Dec 4, 2008 at 9:46 AM, prasad rao wrote: > > Hello friends. > I am new to programing.I am learning Python. > I failed in my attempts to retrive doc strings of methods in > a module. > """ > for x in dir(logging): > print x,x.__doc__ Here x is a string, that is why you print out str.__doc__. Try this: print x, getattr(logging, x).__doc__ or, just help(logging) Kent From kent37 at tds.net Thu Dec 4 16:42:51 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 4 Dec 2008 10:42:51 -0500 Subject: [Tutor] To print docstrings In-Reply-To: <4937F2F5.5070800@tue.nl> References: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> <4937F2F5.5070800@tue.nl> Message-ID: <1c2a2c590812040742j6ea3a449p9ee2ed07c040ecf@mail.gmail.com> On Thu, Dec 4, 2008 at 10:10 AM, A.T.Hofkamp wrote: > docs = [] > for x in dir(collections): > docs.append(x.__doc__) This will not work, see my previous email for solution. Kent From bgailer at gmail.com Thu Dec 4 17:23:57 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 04 Dec 2008 11:23:57 -0500 Subject: [Tutor] Confused about __setitem__ In-Reply-To: <493776F9.4060403@asymmetricsupplychain.com> References: <493776F9.4060403@asymmetricsupplychain.com> Message-ID: <4938041D.10508@gmail.com> Jim Hodgen wrote: > I am learning Python with a minesweeper-engine project. Future > activities turn my attention to exploring the use of dictionaries as > sparse matrices, hence the representation of the gameboard. > Why can't I fill the dictionary-as-a-matrix with an instance of class > Square? Why do I need a custom method to make the assignment? What > am I not understanding? You don't have a sparse matrix here. There is a dictionary entry for every square. You might as well use a nested list or an array from one of the array modules. If you want a Baseboard instance to directly accept keyed assignment use: class Basebaord(dict) and drop boardsquares. > > *_# basics module (mineswobj1)contents..._* > class Square: > mined = True # False if empty, True if mined, no > other values allowed > marked = 0 # False if 0, marked-as-mined if 1, > marked-as-unknown if 2, no other values allowed > displaystate = 0 # covered if 0, marked if 1, exposed if > 2, selected if 3, no other values allowed marked and display state are in conflict and overlap. Consider instead: selected = False displaystate is one of covered-blank, covered-marked, mined, exposed > minedneighbors = 0 # number of mined neighboring squares, max > legit value = 8 > > class Baseboard: > xdimension = 0 # x-dimension of the gameboard, integer > value greater than 0 > ydimension = 0 # y-dimension of the gameboard, integer > value greater than 0 > minenumber = 0 # number of mines on the board for play, > value from 0 to (xdimension * ydimension) > state = 0 # playable if 0, not-playable-exploded > if 1, not-playable-solved if 2 > boardsquares = {} # dictionary used to emulate an addressable 2 > dimensional grid where an instance of class Square > # is inserted with a key consisting > of a tuple describing its zero-based address in the grid > > def initializeboard(xdim, ydim, minenum): > gameboard = Baseboard() > for yct in range(ydim): > for xct in range(xdim): > gameboard[(xct,yct)] = Square() > print 'gameboard[(',xct,yct, ')] =', gameboard[(xct,yct)] > > *_# driver module contents_* > import basics > > mineswobj1.initializeboard(3, 4, 5) > > _*#error message*_ > Traceback (most recent call last): > File "C:\jmh\Python\minedriver0.py", line 6, in > mineswobj1.initializeboard(3, 4, 5) > File "C:\jmh\Python\mineswobj1.py", line 26, in initializeboard > gameboard[(xct,yct)] = Square() > AttributeError: Baseboard instance has no attribute '__setitem__' > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From joewoe at fsmail.de Thu Dec 4 17:38:41 2008 From: joewoe at fsmail.de (=?utf-8?Q?J=F6rg_W=F6lke?=) Date: Thu, 4 Dec 2008 17:38:41 +0100 Subject: [Tutor] Help Optimise Code In-Reply-To: References: Message-ID: <20081204163841.GA6274@localhost> * Richard Lovely [081123 11:35]: > I've tried a the sieve of erath-whatever as in test_generator, > implemented using itertools functions, but it hit max recusion depth > somewhere before 1000 primes, and I'm after millions of primes. I found an old implementation for some exercise of the "sieve of Eratosthenes" in my backups and its not recursive but iterative: #!/usr/bin/env python l=10000*[1] for i in range(2,len(l)): if l[i] == 1: print i for j in range(i+1,len(l)): if j%i == 0: l[j]=0 Yes, it is pretty slow for a range of a million, but it speeds up a little after half an hour or so :-) You might want to have a look at the bsd-games package, which includes a program named primes. It prints out primes at a reasonable speed - up to 4294967295 (the default). The manpage says: " BUGS primes won't get you a world record. " If you can get it to work faster in python? I doubt that. primes uses IIRC atkin-sieve: "http://cr.yp.to/papers/primesieves.pdf" -- You have an ambitious nature and may make a name for yourself. From kent37 at tds.net Thu Dec 4 19:10:39 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 4 Dec 2008 13:10:39 -0500 Subject: [Tutor] Help Optimise Code In-Reply-To: <20081204163841.GA6274@localhost> References: <20081204163841.GA6274@localhost> Message-ID: <1c2a2c590812041010n31c69e6evd1dc06af728c400f@mail.gmail.com> On Thu, Dec 4, 2008 at 11:38 AM, J?rg W?lke wrote: > #!/usr/bin/env python > > l=10000*[1] > for i in range(2,len(l)): > if l[i] == 1: > print i > for j in range(i+1,len(l)): > if j%i == 0: for j in range(2*i, len(l), i): would be much faster, avoiding all the division. Kent From alan.gauld at btinternet.com Thu Dec 4 19:27:12 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 4 Dec 2008 18:27:12 -0000 Subject: [Tutor] To print docstrings References: <9e3fac840812040646i39257f4dw36d80c4243e7dee7@mail.gmail.com> Message-ID: "prasad rao" wrote > Please someone tell me how I can retrive all the docstrings > in a module Unless its just as an exercise use help() Its much easier! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From lawrence.wickline at gmail.com Thu Dec 4 19:48:54 2008 From: lawrence.wickline at gmail.com (Lawrence Wickline) Date: Thu, 4 Dec 2008 10:48:54 -0800 Subject: [Tutor] Sorting a dictionary on a value in a list. In-Reply-To: <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> Message-ID: <045316B5-CB8D-4C92-9CA4-E23FC5B3B08D@gmail.com> Thanks for the help I think I got it. As far as lines go I believe it will be processing hundreds of thousands of lines if not a million or more lines per run. I haven't gotten to do a full run but it has been running acceptably fast on my test files. I ended up putting it into a main function and adding: if __name__ == "__main__": main() On Dec 3, 2008, at 5:42 PM, Kent Johnson wrote: > On Wed, Dec 3, 2008 at 7:58 PM, Lawrence Wickline > wrote: > >> how would I sort on bytes sent? > > You can't actually sort a dictionary; what you can do is sort the > list of items. > > In this case each item will look be a tuple > (filename, (bytes, bytes_sent)) > and dict.items() will be a list of such tuples. > > The best way to sort a list is to make a key function that extracts a > key from a list item, then pass that to the list sort() method. In > your case, you want to extract the second element of the second > element, so you could use the function > def make_key(item): > return item[1][1] > > Then you can make a sorted list with > sorted(dict.items(), key=make_key) > >> how would I make this more efficient? > > It looks pretty good to me. A few minor notes below. > >> code: >> >> # Expect as input: >> # URI, >> 1,return_code,bytes,referer,ip,time_taken,bytes_sent,ref_dom >> # index 0 1 2 3 4 5 6 7 8 >> >> import sys >> >> >> dict = {} > > Don't use dict as the name of a variable, it shadows the built-in > dict() function. > >> def update_dict(filename, bytes, bytes_sent): >> # Build and update our dictionary adding total bytes sent. >> if dict.has_key(filename): >> bytes_sent += dict[filename][1] >> dict[filename] = [bytes, bytes_sent] >> else: >> dict[filename] = [bytes, bytes_sent] > > If you really want to squeeze every bit of speed, > filename in dict > is probably faster than > dict.has_key(filename) > and you might try also using a try / catch block instead of has_key(). > You could also try passing dict as a parameter, that might be faster > than having it as a global. > > None of these will matter unless you have many thousand lines of > input. How many lines do you have? How long does it take to process? > >> # input comes from STDIN >> for line in sys.stdin: >> # remove leading and trailing whitespace and split on tab >> words = line.rstrip().split('\t') > > rstrip() removes only trailing white space. It is not needed since you > don't use the last field anyway. > >> file = words[0] >> bytes = words[3] >> bytes_sent = int(words[7]) >> update_dict(file, bytes, bytes_sent) > > If you put all this into a function it will run a little faster. > > Kent From dkuhlman at rexx.com Thu Dec 4 22:26:02 2008 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Thu, 4 Dec 2008 13:26:02 -0800 Subject: [Tutor] Graph theory In-Reply-To: <1c2a2c590812031749w718bdbb6ybd4d2e844b225278@mail.gmail.com> References: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> <5e58f2e40812031716u2aa5b4a5ide2d1f743534f7f@mail.gmail.com> <1c2a2c590812031749w718bdbb6ybd4d2e844b225278@mail.gmail.com> Message-ID: <20081204212602.GA37329@cutter.rexx.com> On Wed, Dec 03, 2008 at 08:49:12PM -0500, Kent Johnson wrote: > On Wed, Dec 3, 2008 at 8:16 PM, John Fouhy wrote: > > > Interestingly, Guido wrote an essay on implementing graphs in python > > using dictionaries and lists: > > > > http://python.org/doc/essays/graphs/ > > Also googling "python graph" finds many alternatives. And, if you are also interested in the visual display of graphs, then look for graphviz and python-pygraphviz. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From santiagopm at wanadoo.es Fri Dec 5 00:05:51 2008 From: santiagopm at wanadoo.es (=?UTF-8?Q?Santiago_Pay=C3=A0_i_Miralta?=) Date: Fri, 5 Dec 2008 00:05:51 +0100 Subject: [Tutor] Graph theory In-Reply-To: <20081204212602.GA37329@cutter.rexx.com> References: <8079280b0812031703l6beeaf16t6cd57334b5d2e6b7@mail.gmail.com> <5e58f2e40812031716u2aa5b4a5ide2d1f743534f7f@mail.gmail.com> <1c2a2c590812031749w718bdbb6ybd4d2e844b225278@mail.gmail.com> <20081204212602.GA37329@cutter.rexx.com> Message-ID: <20081204230551.GA28416@gorda.chocita> On Thu, Dec 04, 2008 at 01:26:02PM -0800, Dave Kuhlman wrote: > On Wed, Dec 03, 2008 at 08:49:12PM -0500, Kent Johnson wrote: > > On Wed, Dec 3, 2008 at 8:16 PM, John Fouhy wrote: > > > > > Interestingly, Guido wrote an essay on implementing graphs in python > > > using dictionaries and lists: > > > > > > http://python.org/doc/essays/graphs/ > > > > Also googling "python graph" finds many alternatives. > > And, if you are also interested in the visual display of graphs, > then look for graphviz and python-pygraphviz. > Networkx works fine for me. Try it! http://networkx.lanl.gov/ Santiago -- Santiago Pay? i Miralta 607 070 992 mailto:santiagopm at wanadoo.es From jeremiah.jester at panasonic.aero Fri Dec 5 02:25:39 2008 From: jeremiah.jester at panasonic.aero (Jeremiah Jester) Date: Thu, 04 Dec 2008 17:25:39 -0800 Subject: [Tutor] rrdtool examples. Message-ID: <1228440339.19777.21.camel@M13425> Is anyone on here using the python-rrdtool module for graphing and analysis? If so, do you have some sample scripts you could show me. There doesn't seem to be a lot out there as far as real world python examples. Thanks, JJ Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. From mortimerp at live.com Fri Dec 5 09:51:45 2008 From: mortimerp at live.com (Marty Pitts) Date: Fri, 5 Dec 2008 00:51:45 -0800 Subject: [Tutor] Writing to a file problem.... Message-ID: Hi, I am currently working my way through the .pdf Byte of Python tutorial by Swaroop, C H After about 1/2 way I ran into a problem that my own trouble shooting has failed :-(. The script created is for backing up files into a .zip format. It is written by the author in a *nix environment. However, I am currently on a *doz box. I have made certain changes in order for it to play nice with Windows, but I am still stumped. Here is the script: #!c:\python26\python.exe # Filename : backup_ver1.py import os import time #1. The files and directories to be backed up are specified in a list source = ['c:\Users\Marty\Downloads', 'c:\Users\Marty\Contacts'] #2. The backup must be stored in a main backup directory target_dir = 'J:\\backup\\' #3. The files are backed up into a zip file. #4. The name of the zip archive is the current date and time target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip' #5. We use the zip command to put files in a zip archive zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print 'Sucessful backup to', target else: print 'Backup FAILED' The error I get running the script from a command prompt is: C:\Python26>backup_ver1.py zip I/O error: Invalid argument zip error: Could not create output file ('J:/backup/20081205002535.zip') Backup FAILED >From what I can tell, it might be a problem writing the file in Windows. Maybe a rights issue. I don't know. I have tried granting the folder all the write rights I could think of. If I change the target_dir to just 'j:' I then get the zip program appearing to run through all the files compressing them, but I end up with this msg: total bytes=104598424, compressed=102594319 ->2% savings zip warning: new zip file left as 'J:zia03824 zip I/O error: Invalid argument zip error: Could not create output file (was replacing the original zip file) Backup FAILED Any suggestions would be appreciated. Thanks, Marty. _________________________________________________________________ Send e-mail faster without improving your typing skills. http://windowslive.com/Explore/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_speed_122008 From wescpy at gmail.com Fri Dec 5 10:37:56 2008 From: wescpy at gmail.com (wesley chun) Date: Fri, 5 Dec 2008 01:37:56 -0800 Subject: [Tutor] Writing to a file problem.... In-Reply-To: References: Message-ID: <78b3a9580812050137g1447cddet91f8325f41b49a22@mail.gmail.com> marty, i applaud you in your efforts to port this script to the Win32 platform. the task is not as simple as one may expect, due to the differing file pathname nomenclatures that the different operating systems use. because of this, i have a couple of suggestions: 1. highly recommend converting this script to use the built-in ZIP file support offered by Python via the zipfile module: http://docs.python.org/lib/module-zipfile.html 2. instead of filenames like: 'c:\Users\Marty\Downloads', 'c:\Users\Marty\Contacts', 'J:\\backup\\', etc., i would recommend using "raw strings." these will help deal with the backward slashes. in the 3rd example, you correctly use double-backslashes, but you don't in the 1st pair. for all 3, change all the files to single backslashes and place an "r" preceding the opening quote, i.e., r'c:\Users\Marty\Downloads', r'c:\Users\Marty\Contacts', r'J:\backup'. for the last case where you are using a directory/folder name, use os.path.join() instead of manually placing the filepath separator. if you combine these together, you should be able to rework your script so that things work properly without having to use external calls like os.system(). hope these help! -- 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 webmaster at mikehoy.net Fri Dec 5 11:49:21 2008 From: webmaster at mikehoy.net (Mike Hoy) Date: Fri, 5 Dec 2008 03:49:21 -0700 Subject: [Tutor] Jython question Message-ID: Is this the right forum to ask a jython question? I want to get started on using GUI with my python apps and I already study Java so I thought maybe I should combine the two. I did some playing around with it so far and I like what I see. I'll post my code if you guys handle this. Any thoughts? -- Mike Hoy From kent37 at tds.net Fri Dec 5 12:46:25 2008 From: kent37 at tds.net (Kent Johnson) Date: Fri, 5 Dec 2008 06:46:25 -0500 Subject: [Tutor] Writing to a file problem.... In-Reply-To: References: Message-ID: <1c2a2c590812050346i10890454o37ee94dcc874ddaa@mail.gmail.com> On Fri, Dec 5, 2008 at 3:51 AM, Marty Pitts wrote: > #5. We use the zip command to put files in a zip archive > zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) It might help to print zip_command, to make sure it is what you expect. You can take the printed command and try it on the command line yourself. Kent From TexasJerky100 at aol.com Thu Dec 4 21:28:49 2008 From: TexasJerky100 at aol.com (TexasJerky100 at aol.com) Date: Thu, 4 Dec 2008 15:28:49 EST Subject: [Tutor] Random equation generator Message-ID: I am starting out with 7 fixed reference points. From there I want a program that can randomly generate linear equations. After the equations are generated I would then like to randomly insert the 7 fixed reference points into the equations and calculate the results. I currently have several programs that can generate random string of words from a file that contains a list of word but is not much help creating random equations. Do you know if there is such a program that can do what I am trying to get accomplished?? Thanks Frank Hopkins Houston, Tx **************Make your life easier with all your friends, email, and favorite sites in one place. Try it now. (http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) -------------- next part -------------- An HTML attachment was scrubbed... URL: From roadierich at googlemail.com Fri Dec 5 14:30:03 2008 From: roadierich at googlemail.com (Rich Lovely) Date: Fri, 5 Dec 2008 13:30:03 +0000 Subject: [Tutor] Random equation generator In-Reply-To: References: Message-ID: When you say linear, I'm assuming fitting y=mx+c, and passing through points? The line through points (x1, y1) and (x2,y2) is y - y1 = (y2-y1) / (x2-x1) * (x-x1) That multiplies out to: y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1 That gives m = (y2-y1)/(x2-x1) and c = y1 - (y2-y1)/(x2-x1) you can then create a class to represent the line: class Line(object): def __init__(self, (x1,y1), (x2,y2)): "create a line passing through points (x1,y1) and (x2,y2) (inputted as tuples)" self.m = (y2-y1)/(x2-x1) self.c = y1 - self.m def is_on_line(self,(x,y)): 'Test if point represtented by an (x,y) tuple is on the line" if self. m * x + self.c == y: return True else: return False def __str__(self): "returns the equation of the line in the form y=mx+c. Might be quite long if floats are involved." print "y=%dx + %d" % (self.m, self.c) Then all you have to do is choose a pair of points, then iterate over the list, testing each point in turn, wash, rinse and repeat. Does that help? On 4 Dec 2008, at 20:28, TexasJerky100 at aol.com wrote: > I am starting out with 7 fixed reference points. From there I > want a program that can randomly > generate linear equations. After the equations are generated I > would then like to randomly insert > the 7 fixed reference points into the equations and calculate the > results. I currently have several > programs that can generate random string of words from a file that > contains a list of word but is not > much help creating random equations. > Do you know if there is such a program that can do what I am > trying to get accomplished?? > > Thanks > > Frank Hopkins > Houston, Tx > > > > Make your life easier with all your friends, email, and favorite > sites in one place. Try it now. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From nephish at gmail.com Fri Dec 5 19:05:38 2008 From: nephish at gmail.com (shawn bright) Date: Fri, 5 Dec 2008 12:05:38 -0600 Subject: [Tutor] best way to run several processes over and over Message-ID: <384c93600812051005qac907c1ta6d6c684ba19cc52@mail.gmail.com> Hey all. I have a rather large app that uses 14 threads that all run at the same time. i use threading.Thread().start() to set them off. each one runs somthing like this def run(): while 1: do a bunch of stuff time.sleep(60) i have the option i guess of having fewer threads, and just calling an outside script for some of the actions, like this def run(): while 1: do a bunch of stuff time.sleep(60) os.system("run_other_scripts.py") Does one have an advantage over the other? thanks shawn From alan.gauld at btinternet.com Fri Dec 5 19:29:20 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 5 Dec 2008 18:29:20 -0000 Subject: [Tutor] best way to run several processes over and over References: <384c93600812051005qac907c1ta6d6c684ba19cc52@mail.gmail.com> Message-ID: "shawn bright" wrote > I have a rather large app that uses 14 threads that all run at the > same time. > each one runs somthing like this > > def run(): > while 1: > do a bunch of stuff > time.sleep(60) > > i have the option i guess of having fewer threads, and just calling > an > outside script for some of the actions, like this > > def run(): > while 1: > do a bunch of stuff > time.sleep(60) > os.system("run_other_scripts.py") > > Does one have an advantage over the other? In general threads use a lot less resource than processes so your existing approach should be better. BUT the sleep(60) bothers me slightly. A one minute delay is an age in computer terms. It might be better to see if you can build the delay outside the threading and have each thread run just once then die. Then restart the threads after 60 seconds at the top level. But whether that is really any better would require you to try it and see I suspect. Guessing the effect of these kinds of changes is little more than speculation... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From denis.spir at free.fr Sat Dec 6 08:50:51 2008 From: denis.spir at free.fr (spir) Date: Sat, 06 Dec 2008 08:50:51 +0100 Subject: [Tutor] Writing to a file problem.... In-Reply-To: References: <1c2a2c590812050346i10890454o37ee94dcc874ddaa@mail.gmail.com> <4939AFC0.1080206@free.fr> Message-ID: <493A2EDB.7030608@free.fr> Marty Pitts a ?crit : > > >> Date: Fri, 5 Dec 2008 23:48:32 +0100 >> From: denis.spir at free.fr >> To: mortimerp at live.com >> Subject: Re: [Tutor] Writing to a file problem.... >> >>> zip_command = "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) >> What if you just double the '\' -- you did it properly for you "j:\\..." path. >> denis > > I tried that as well a bunch of variations such as r'j:\Backup\ and 'j:\\Backup\\' and 'j:/Backup/' > > None of which seems to have worked. Sorry. Also, is the '!' really a valid part of a zip argument? Have you tried for a test e.g. c:\Users\Marty\Zip\Zip -!rv 'test_target' test_source.txt and c:\Users\Marty\Zip\Zip -!rv 'test_target' test_source1.txt test_source2.txt on the command line? (test_sources existing) If yes, meaning that the overall zip_command format is right, then logially the only source of noise precisely is 'source'. Obviously, it must be a sequence of valid name files. Try: print "%s\n%s\n%s" % ( source , ' '.join(source) , ( "c:\Users\Marty\Zip\Zip -!rv '%s' %s" % (target, ' '.join(source)) ) ) to really check how you call zip. There must be something wrong, no? By the way, I just checked the output format above, and it gave me: c:\Users\Marty\Zip\Zip -!rv 'test.txt' test1.text test2.txt How come that the target is surrounded with ''? For zip, all of that arg list is plain text anyway... Is it necessary for zip to identify target? Or for filenames that include spaces? I would try without apostrophes, anyway, and the contrary, meaning to quote source list, too: ' '.join([("'%s'" %name) for name in source]) (untested) (Now, I think that this last version has higher chances to be right, because of the space_in_file_name issue. Try to use "", too.) Hope this helps. denis From lie.1296 at gmail.com Sat Dec 6 09:41:10 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 6 Dec 2008 08:41:10 +0000 (UTC) Subject: [Tutor] Sorting a dictionary on a value in a list. References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> <045316B5-CB8D-4C92-9CA4-E23FC5B3B08D@gmail.com> Message-ID: On Thu, 04 Dec 2008 10:48:54 -0800, Lawrence Wickline wrote: > Thanks for the help I think I got it. > > As far as lines go I believe it will be processing hundreds of thousands > of lines if not a million or more lines per run. I haven't gotten to do > a full run but it has been running acceptably fast on my test files. > > I ended up putting it into a main function and adding: > > if __name__ == "__main__": > main() > In most cases, in processing involving networking, the bottleneck is the network speed itself. To speed things up by optimizing your own code might not make your download significantly faster (getting 60 seconds faster is great for scripts that usually runs for 70 seconds, but is a waste of development time for scripts that usually run for 1 hour) Usually a multi-threading downloader might be a better chance to improvement, especially for 1) downloading from different site, 2) the remote sites have speed limit, 3) you have faster download link than the server can gives From ptmcg at austin.rr.com Sat Dec 6 10:20:51 2008 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sat, 6 Dec 2008 03:20:51 -0600 Subject: [Tutor] Random equation generator In-Reply-To: References: Message-ID: <2A737786909B4C99B8ED668074F077C3@AWA2> I would say, though, that you should be careful in your implementation of is_on_line, for floating point round-off errors. Try this at the command prompt (Python 2.5.2, with __future__ division imported): >>> 49 * (1/49) == 1 False >>> 1 - 49 * (1/49) 1.1102230246251565e-016 I would suggest a slightly modified version of is_on_line: class Line(object): EPSILON = 1e-15 ... def is_on_line(self,(x,y)): 'Test if point represtented by an (x,y) tuple is on the line' return abs(self. m * x + self.c - y) < Line.EPSILON -- Paul From roadierich at googlemail.com Sat Dec 6 17:05:42 2008 From: roadierich at googlemail.com (Rich Lovely) Date: Sat, 6 Dec 2008 16:05:42 +0000 Subject: [Tutor] Random equation generator In-Reply-To: References: Message-ID: Please use the reply-all button when responding, so that your message gets sent to the list as well. If that's the sort of equation you're after, than the easiest way would probably to decide on a generalised form: > y=(2x-1)*4x y = 4*x**2 - 4x > y=2+5(x-1) y = 5*x - 3 > y=(2x+5)+(5x-25) y = 10*x**2 + 75*x - 125 > y=((x+13)/((x-18))*(2x-1) y = (2*x**2 - 19*x -13) / (x - 18) Except for the last equation, they are all of the "general form" ax^2 +bx + c. These can be generated using tools from the random module: (pseudo python) equation = randint(minimum, maximum) * x ** 2 + randint(minimum,maximum) * x + randint(minimum,maximum) if you want to generate equations in the form of the last one you mentioned, it will be a bit more difficult. You could add a random number of terms with a random power, but actually impliementing it might be a bit harder: import random def generate_terms(minimum, maximum): "generate a list of random terms for an equation of the form (a*x)**b + ... " return [(random.randint(minimum, maximum), random.randint(-1,1)) for x in xrange(random.randint(1, 10))] class Line(object): epsilon = 1e-15 def __init__(self, equation_terms): self._equation_terms = equation_terms def is_on_line(self, (x,y)): calulated_y = sum((a * x) ** b for a,b in self._equation_terms) return abs(y - calculated_y) <= self.epsilon #see message from Paul Then, instead of picking two points, you can do: line = Line(generate_terms(-25, 25)) then proceed as with my previous message. getting a nice string representation won't be as easy... unless you're happy with seeing x**-1's dotted around. You'll need to tweak the values within the generate_terms function to get exactly what you want. Alternatively (and I'm probably going to get shouted at for suggesting it), you could write something that generates random python equations, and use some variety of eval() to get a value out of it... Does that help? On 5 Dec 2008, at 13:52, TexasJerky100 at aol.com wrote: > Thanks for the response. This will help me passing the points > and compairing the results. > I however still need to find something that will generate the actual > linear equations or similar equations in a > random fashion. For instance lets say the program would first > generate the actual equations. > > y=(2x-1)*4x > y=2+5(x-1) > y=(2x+5)+(5x-25) > y=((x+13)/((x-18))*(2x-1) > > Then I could use your program to pass through the values. > > > Frank Hopkins > Houston, Tx > > > > > > > In a message dated 12/5/2008 7:29:42 A.M. Central Standard Time, roadierich at googlemail.com > writes: > When you say linear, I'm assuming fitting y=mx+c, and passing > through points? > > The line through points (x1, y1) and (x2,y2) is > > y - y1 = (y2-y1) / (x2-x1) * (x-x1) > > That multiplies out to: > > y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1 > That gives m = (y2-y1)/(x2-x1) and c = y1 - (y2-y1)/(x2-x1) > > you can then create a class to represent the line: > > class Line(object): > def __init__(self, (x1,y1), (x2,y2)): > "create a line passing through points (x1,y1) and (x2,y2) > (inputted as tuples)" > self.m = (y2-y1)/(x2-x1) > self.c = y1 - self.m > > def is_on_line(self,(x,y)): > 'Test if point represtented by an (x,y) tuple is on the line" > if self. m * x + self.c == y: > return True > else: > return False > > def __str__(self): > "returns the equation of the line in the form y=mx+c. Might > be quite long if floats are involved." > print "y=%dx + %d" % (self.m, self.c) > > > Then all you have to do is choose a pair of points, then iterate > over the list, testing each point in turn, wash, rinse and repeat. > > Does that help? > > On 4 Dec 2008, at 20:28, TexasJerky100 at aol.com wrote: > >> I am starting out with 7 fixed reference points. From there I >> want a program that can randomly >> generate linear equations. After the equations are generated I >> would then like to randomly insert >> the 7 fixed reference points into the equations and calculate the >> results. I currently have several >> programs that can generate random string of words from a file that >> contains a list of word but is not >> much help creating random equations. >> Do you know if there is such a program that can do what I am >> trying to get accomplished?? >> >> Thanks >> >> Frank Hopkins >> Houston, Tx >> >> >> >> Make your life easier with all your friends, email, and favorite >> sites in one place. Try it now. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor > > = > > > > Make your life easier with all your friends, email, and favorite > sites in one place. Try it now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From TexasJerky100 at aol.com Sat Dec 6 17:34:06 2008 From: TexasJerky100 at aol.com (TexasJerky100 at aol.com) Date: Sat, 6 Dec 2008 11:34:06 EST Subject: [Tutor] Random equation generator Message-ID: I think this maybe will work out. I might have to just go with the generalized form as you suggest and forget about the random aspect. In a message dated 12/6/2008 10:05:16 A.M. Central Standard Time, roadierich at googlemail.com writes: Please use the reply-all button when responding, so that your message gets sent to the list as well. If that's the sort of equation you're after, than the easiest way would probably to decide on a generalised form: y=(2x-1)*4x y = 4*x**2 - 4x y=2+5(x-1) y = 5*x - 3 y=(2x+5)+(5x-25) y = 10*x**2 + 75*x - 125 y=((x+13)/((x-18))*(2x-1) y = (2*x**2 - 19*x -13) / (x - 18) Except for the last equation, they are all of the "general form" ax^2 +bx + c. These can be generated using tools from the random module: (pseudo python) equation = randint(minimum, maximum) * x ** 2 + randint(minimum,maximum) * x + randint(minimum,maximum) if you want to generate equations in the form of the last one you mentioned, it will be a bit more difficult. You could add a random number of terms with a random power, but actually impliementing it might be a bit harder: import random def generate_terms(minimum, maximum): "generate a list of random terms for an equation of the form (a*x)**b + ... " return [(random.randint(minimum, maximum), random.randint(-1,1)) for x in xrange(random.randint(1, 10))] class Line(object): epsilon = 1e-15 def __init__(self, equation_terms): self._equation_terms = equation_terms def is_on_line(self, (x,y)): calulated_y = sum((a * x) ** b for a,b in self._equation_terms) return abs(y - calculated_y) <= self.epsilon #see message from Paul Then, instead of picking two points, you can do: line = Line(generate_terms(-25, 25)) then proceed as with my previous message. getting a nice string representation won't be as easy... unless you're happy with seeing x**-1's dotted around. You'll need to tweak the values within the generate_terms function to get exactly what you want. Alternatively (and I'm probably going to get shouted at for suggesting it), you could write something that generates random python equations, and use some variety of eval() to get a value out of it... Does that help? On 5 Dec 2008, at 13:52, _TexasJerky100 at aol.com_ (mailto:TexasJerky100 at aol.com) wrote: Thanks for the response. This will help me passing the points and compairing the results. I however still need to find something that will generate the actual linear equations or similar equations in a random fashion. For instance lets say the program would first generate the actual equations. y=(2x-1)*4x y=2+5(x-1) y=(2x+5)+(5x-25) y=((x+13)/((x-18))*(2x-1) Then I could use your program to pass through the values. Frank Hopkins Houston, Tx In a message dated 12/5/2008 7:29:42 A.M. Central Standard Time, _roadierich at googlemail.com_ (mailto:roadierich at googlemail.com) writes: When you say linear, I'm assuming fitting y=mx+c, and passing through points? The line through points (x1, y1) and (x2,y2) is y - y1 = (y2-y1) / (x2-x1) * (x-x1) That multiplies out to: y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1 That gives m = (y2-y1)/(x2-x1) and c = y1 - (y2-y1)/(x2-x1) you can then create a class to represent the line: class Line(object): def __init__(self, (x1,y1), (x2,y2)): "create a line passing through points (x1,y1) and (x2,y2) (inputted as tuples)" self.m = (y2-y1)/(x2-x1) self.c = y1 - self.m def is_on_line(self,(x,y)): 'Test if point represtented by an (x,y) tuple is on the line" if self. m * x + self.c == y: return True else: return False def __str__(self): "returns the equation of the line in the form y=mx+c. Might be quite long if floats are involved." print "y=%dx + %d" % (self.m, self.c) Then all you have to do is choose a pair of points, then iterate over the list, testing each point in turn, wash, rinse and repeat. Does that help? On 4 Dec 2008, at 20:28, _TexasJerky100 at aol.com_ (mailto:TexasJerky100 at aol.com) wrote: I am starting out with 7 fixed reference points. From there I want a program that can randomly generate linear equations. After the equations are generated I would then like to randomly insert the 7 fixed reference points into the equations and calculate the results. I currently have several programs that can generate random string of words from a file that contains a list of word but is not much help creating random equations. Do you know if there is such a program that can do what I am trying to get accomplished?? Thanks Frank Hopkins Houston, Tx ____________________________________ Make your life easier with all your friends, email, and favorite sites in one place. _Try it now_ (http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) . _______________________________________________ Tutor maillist - _Tutor at python.org_ (mailto:Tutor at python.org) _http://mail.python.org/mailman/listinfo/tutor_ (http://mail.python.org/mailman/listinfo/tutor) = ____________________________________ Make your life easier with all your friends, email, and favorite sites in one place. _Try it now_ (http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) . = **************Make your life easier with all your friends, email, and favorite sites in one place. Try it now. (http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) -------------- next part -------------- An HTML attachment was scrubbed... URL: From newgat11 at yahoo.com Sat Dec 6 19:01:19 2008 From: newgat11 at yahoo.com (the New me) Date: Sat, 6 Dec 2008 10:01:19 -0800 (PST) Subject: [Tutor] Sorting on different fields Message-ID: <901698.73530.qm@web51009.mail.re2.yahoo.com> I would like to be able to sort a list of rows, each row has (say 4) fields to sort on, primary key to sort on first, then the second one and so on, any suggestions and where to look (may be), Thanks a bunch Peter From kent37 at tds.net Sat Dec 6 19:31:59 2008 From: kent37 at tds.net (Kent Johnson) Date: Sat, 6 Dec 2008 13:31:59 -0500 Subject: [Tutor] Sorting on different fields In-Reply-To: <901698.73530.qm@web51009.mail.re2.yahoo.com> References: <901698.73530.qm@web51009.mail.re2.yahoo.com> Message-ID: <1c2a2c590812061031l1a085c29j78a79820c721c2c8@mail.gmail.com> On Sat, Dec 6, 2008 at 1:01 PM, the New me wrote: > > > I would like to be able to sort a list of rows, > each row has (say 4) fields to sort on, > > primary key to sort on first, then the second one and so on, > > any suggestions and where to look (may be), http://personalpages.tds.net/~kent37/kk/00007.html#e7sorting-on-multiple-keys Kent From damontimm at gmail.com Sun Dec 7 00:08:33 2008 From: damontimm at gmail.com (Damon Timm) Date: Sat, 6 Dec 2008 18:08:33 -0500 Subject: [Tutor] Newbie Wondering About Threads Message-ID: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> Hi Everyone - I am a complete and utter Python newbie (as of today, honestly) -- am interested in expanding my programming horizons beyond bash scripting and thought Python would be a nice match for me. To start, I thought I may try re-writing some of my bash scripts in Python as a learning tool for me ... and the first one I wanted to talkle was a script that converts .flac audio files into .mp3 files ... basic idea is I supply a sourceDirectory and targetDirectory and then recursively convert the source file tree into an identical target file tree filled with mp3 files. I'm sure this has been done before (by those much wiser than me) but I figured I can learn something as I go ... for what I've accomplished so far, it seems pretty ugly! But I'm learning ... Anyhow, I think I got the basics down but I had a thought: can I thread this program to utilize all of my cores? And if so, how? Right now, the lame audio encoder is only hitting one core ... I could do all this faster if I could pass a variable that says: open 2 or 4 threads instead. Here is what I've been working on so far -- would appreciate any insight you may have. Thanks, Damon #!/usr/bin/env python import os import sys import fnmatch from os import system fileList = [] rootDir = sys.argv[1] targetDir = sys.argv[2] def shell_quote(s): """Quote and escape the given string (if necessary) for inclusion in a shell command""" return "\"%s\"" % s.replace('"', '\\"') def _mkdir(newdir): """works the way a good mkdir should :) - already exists, silently complete - regular file in the way, raise an exception - parent directory(ies) does not exist, make them as well http://code.activestate.com/recipes/82465/ """ if os.path.isdir(newdir): pass elif os.path.isfile(newdir): raise OSError("a file with the same name as the desired " \ "dir, '%s', already exists." % newdir) else: head, tail = os.path.split(newdir) if head and not os.path.isdir(head): _mkdir(head) #print "_mkdir %s" % repr(newdir) if tail: os.mkdir(newdir) # get all the flac files and directory structures for dirpath, subFolders, files in os.walk(rootDir): for file in files: if fnmatch.fnmatch(file, '*.flac'): flacFileInfo = [os.path.join(dirpath,file),dirpath+"/",file,dirpath.lstrip(rootDir)+"/"] fileList.append(flacFileInfo) # create new directory structure and mp3 files for sourceFile,dir,flacfile,strip in fileList: mp3File = shell_quote(targetDir + strip + flacfile.strip('.flac') + ".mp3") mp3FileDir = targetDir + strip sourceFile = shell_quote(sourceFile) _mkdir(mp3FileDir) flacCommand = "flac --decode --stdout --silent " + sourceFile + " | lame -V4 --slient - " + mp3File system(flacCommand) From damontimm at gmail.com Sun Dec 7 03:43:11 2008 From: damontimm at gmail.com (Damon Timm) Date: Sat, 6 Dec 2008 21:43:11 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> Message-ID: <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter wrote: > I'm on my phone so excuse the simple reply. > From what I skimmed you are wrapping shell commands which is what I do > all the time. Some hints. 1) look into popen or subprocess in place of > execute for more flexibility. I use popen a lot and assigning a popen > call to an object name let's you parse the output and make informed > decisions depending on what the shell program outputs. So I took a peak at subprocess.Popen --> looks like that's the direction I would be headed for parallel processes ... a real simple way to see it work for me was: p2 = subprocess.Popen(["lame","--silent","test.wav","test.mp3"]) p3 = subprocess.Popen(["lame","--silent","test2.wav","test2.mp3"]) p2.wait() p3.wait() top showed that both cores get busy and it takes half the time! So that's great -- when I tried to add the flac decoding through stdout I was able to accomplish it as well ... I was mimicing the command of "flac --decode --stdout test.flac | lame - test.mp3" ... see: p = subprocess.Popen(["flac","--decode","--stdout","test.flac"], stdout=subprocess.PIPE) p2 = subprocess.Popen(["lame","-","test.mp3"], stdin=subprocess.PIPE) p2.communicate(p.communicate()[0]) That did the trick - it worked! However, it was *very* slow! The python script has a "real" time of 2m22.504s whereas if I run it from the command line it is only 0m18.594s. Not sure why this is ... The last piece of my puzzle though, I am having trouble wrapping my head around ... I will have a list of files ["file1.flac","file2.flac","file3.flac","etc"] and I want the program to tackle compressing two at a time ... but not more than two at a time (or four, or eight, or whatever) because that's not going to help me at all (I have dual cores right now) ... I am having trouble thinking how I can create the algorithm that would do this for me ... Thanks everyone. Maybe after a good night's sleep it will come to me. If you have any ideas - would love to hear them. Damon From mwalsh at mwalsh.org Sun Dec 7 06:33:15 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Sat, 06 Dec 2008 23:33:15 -0600 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> Message-ID: <493B601B.10703@mwalsh.org> Damon Timm wrote: > On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter wrote: >> I'm on my phone so excuse the simple reply. >> From what I skimmed you are wrapping shell commands which is what I do >> all the time. Some hints. 1) look into popen or subprocess in place of >> execute for more flexibility. I use popen a lot and assigning a popen >> call to an object name let's you parse the output and make informed >> decisions depending on what the shell program outputs. > > So I took a peak at subprocess.Popen --> looks like that's the > direction I would be headed for parallel processes ... a real simple > way to see it work for me was: > > p2 = subprocess.Popen(["lame","--silent","test.wav","test.mp3"]) > p3 = subprocess.Popen(["lame","--silent","test2.wav","test2.mp3"]) > p2.wait() > p3.wait() > > top showed that both cores get busy and it takes half the time! So > that's great -- when I tried to add the flac decoding through stdout I > was able to accomplish it as well ... I was mimicing the command of > "flac --decode --stdout test.flac | lame - test.mp3" ... see: > > p = subprocess.Popen(["flac","--decode","--stdout","test.flac"], > stdout=subprocess.PIPE) > p2 = subprocess.Popen(["lame","-","test.mp3"], stdin=subprocess.PIPE) > p2.communicate(p.communicate()[0]) > > That did the trick - it worked! However, it was *very* slow! The > python script has a "real" time of 2m22.504s whereas if I run it from > the command line it is only 0m18.594s. Not sure why this is ... I'm not certain this completely explains the poor performance, if at all, but the communicate method of Popen objects will wait until EOF is reached and the process ends. So IIUC, in your example the process 'p' runs to completion and only then is its stdout (p.communicate()[0]) passed to stdin of 'p2' by the outer communicate call. You might try something like this (untested!) ... p1 = subprocess.Popen( ["flac","--decode","--stdout","test.flac"], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p2 = subprocess.Popen( ["lame","-","test.mp3"], stdin=p1.stdout, # <-- stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p2.communicate() ... where you directly assign the stdin of 'p2' to be the stdout of 'p1'. > > The last piece of my puzzle though, I am having trouble wrapping my > head around ... I will have a list of files > ["file1.flac","file2.flac","file3.flac","etc"] and I want the program > to tackle compressing two at a time ... but not more than two at a > time (or four, or eight, or whatever) because that's not going to help > me at all (I have dual cores right now) ... I am having trouble > thinking how I can create the algorithm that would do this for me ... Interesting problem, and not an easy one IMHO, unless you're content with waiting for a pair of processes to complete before starting two more. In which case you can just grab two filenames at a time from the list, define the Popen calls, and wait for (or communicate with) both before continuing with another pair. But since you probably want your script to stay busy, and it's reasonable to assume (I think!) that one of the processes may finish much sooner or much later than the other... well, it is a bit tricky (for me, anyway). Here is my simplistic, not-very-well-thought-out, attempt in pseudo-code, perhaps it will get you started ... paths = ["file1.flac","file2.flac", ... "file11.flac"] procs = [] while paths or procs: procs = [p for p in procs if p.poll() is None] while paths and len(procs) < 2: flac = paths.pop(0) procs.append(Popen(['...', flac], ...)) time.sleep(1) The idea here is to keep track of running processes in a list, remove them when they've terminated, and start (append) new processes as necessary up to the desired max, only while there are files remaining or processes running. HTH, Marty From newgat11 at yahoo.com Sun Dec 7 06:47:16 2008 From: newgat11 at yahoo.com (the New me) Date: Sat, 6 Dec 2008 21:47:16 -0800 (PST) Subject: [Tutor] Sorting on different fields In-Reply-To: <1c2a2c590812061031l1a085c29j78a79820c721c2c8@mail.gmail.com> Message-ID: <323605.57582.qm@web51005.mail.re2.yahoo.com> Thanks Kent: Is there an example for sorting on 2 or 3 fields, I saw this page http://personalpages.tds.net/~kent37/kk/00007.html#e7sorting-on-multiple-keys It just gives a cursory note: To sort a list of items with primary and secondary sort keys, provide a key function that returns a tuple of keys. In Python 2.5, both itemgetter() and attrgetter() accept multiple arguments and return functions that create the correct tuples. that does nt do it for me, Python is supposed to be programmer user freindly, is there a straightforward example? like in SQL one can easily say Order by field1 field2 field3 when it comes to sort, books on python give this gimmiky phrase: you can do easily using ... but no example!! is one supposed to make a class with attr and item and how are these constructed from the fields being given, and how is the whole row sorted by the field(s) that are part of the row. the books dont explain that, how are the rows sorted, by field1 say, then by field2, etc, how is one supposed to make the class, so that these things are attributes in the rows. am sure there must be something on the internet, but again it is not satisfactory, or the people writing the examples are so smart or everybody else is dum? Thanks all --- On Sat, 12/6/08, Kent Johnson wrote: > From: Kent Johnson > Subject: Re: [Tutor] Sorting on different fields > To: newgat11 at yahoo.com > Cc: tutor at python.org > Date: Saturday, December 6, 2008, 1:31 PM > On Sat, Dec 6, 2008 at 1:01 PM, the New me > wrote: > > > > > > I would like to be able to sort a list of rows, > > each row has (say 4) fields to sort on, > > > > primary key to sort on first, then the second one and > so on, > > > > any suggestions and where to look (may be), > > http://personalpages.tds.net/~kent37/kk/00007.html#e7sorting-on-multiple-keys > > Kent From lie.1296 at gmail.com Sun Dec 7 08:58:11 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 7 Dec 2008 07:58:11 +0000 (UTC) Subject: [Tutor] Newbie Wondering About Threads References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> Message-ID: On Sat, 06 Dec 2008 21:43:11 -0500, Damon Timm wrote: > On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter > wrote: >> I'm on my phone so excuse the simple reply. From what I skimmed you are >> wrapping shell commands which is what I do all the time. Some hints. 1) >> look into popen or subprocess in place of execute for more flexibility. >> I use popen a lot and assigning a popen call to an object name let's >> you parse the output and make informed decisions depending on what the >> shell program outputs. > > So I took a peak at subprocess.Popen --> looks like that's the direction > I would be headed for parallel processes ... a real simple way to see it > work for me was: > > p2 = subprocess.Popen(["lame","--silent","test.wav","test.mp3"]) > p3 = subprocess.Popen(["lame","--silent","test2.wav","test2.mp3"]) > p2.wait() > p3.wait() I think when you do that (p2.wait() then p3.wait() ), if p3 finishes first, you wouldn't start another p3 until p2 have finished (i.e. until p2.wait() returns) and if p2 finishes first, you wouldn't start another p2 until p3 finishes (i.e. until p3.wait() returns ). The solution would be to start and wait() the subprocessess in two threads. Use threading module or -- if you use python2.6 -- the new multiprocessing module. Alternatively, you could do a "non-blocking wait", i.e. poll the thread. while True: if p1.poll(): # start another p1 if p2.poll(): # start another p2 > > top showed that both cores get busy and it takes half the time! So > that's great -- when I tried to add the flac decoding through stdout I > was able to accomplish it as well ... I was mimicing the command of > "flac --decode --stdout test.flac | lame - test.mp3" ... see: > > p = subprocess.Popen(["flac","--decode","--stdout","test.flac"], > stdout=subprocess.PIPE) > p2 = subprocess.Popen(["lame","-","test.mp3"], stdin=subprocess.PIPE) > p2.communicate(p.communicate()[0]) > > That did the trick - it worked! However, it was *very* slow! The > python script has a "real" time of 2m22.504s whereas if I run it from > the command line it is only 0m18.594s. Not sure why this is ... > > The last piece of my puzzle though, I am having trouble wrapping my head > around ... I will have a list of files > ["file1.flac","file2.flac","file3.flac","etc"] and I want the program to > tackle compressing two at a time ... but not more than two at a time (or > four, or eight, or whatever) because that's not going to help me at all > (I have dual cores right now) ... I am having trouble thinking how I can > create the algorithm that would do this for me ... > > Thanks everyone. Maybe after a good night's sleep it will come to me. > If you have any ideas - would love to hear them. From lie.1296 at gmail.com Sun Dec 7 11:31:11 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 7 Dec 2008 10:31:11 +0000 (UTC) Subject: [Tutor] Sorting on different fields References: <1c2a2c590812061031l1a085c29j78a79820c721c2c8@mail.gmail.com> <323605.57582.qm@web51005.mail.re2.yahoo.com> Message-ID: On Sat, 06 Dec 2008 21:47:16 -0800, the New me wrote: > is there a straightforward example? >>> import operator >>> k = [[1, 2, 3, 4], [4, 3, 2, 1], [1, 3, 2, 4], [2, 4, 3, 1]] >>> sorted(l, key=operator.itemgetter(3, 2)) [[4, 3, 2, 1], [2, 4, 3, 1], [1, 3, 2, 4], [1, 2, 3, 4]] >>> for k in sorted(l, key=operator.itemgetter(0, 2)): print k ... [1, 3, 2, 4] [1, 2, 3, 4] [2, 4, 3, 1] [4, 3, 2, 1] >>> def genkey(item): ... return item[2] ... >>> for k in sorted(l, key=genkey): print k ... [4, 3, 2, 1] [1, 3, 2, 4] [1, 2, 3, 4] [2, 4, 3, 1] >>> def genkey(item): ... return item[2], item[1] ... >>> for k in sorted(l, key=genkey): print k ... [4, 3, 2, 1] [1, 3, 2, 4] [1, 2, 3, 4] [2, 4, 3, 1] From kent37 at tds.net Sun Dec 7 14:31:43 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 7 Dec 2008 08:31:43 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> Message-ID: <1c2a2c590812070531u17feef24g1672d86a8738945c@mail.gmail.com> On Sat, Dec 6, 2008 at 9:43 PM, Damon Timm wrote: > The last piece of my puzzle though, I am having trouble wrapping my > head around ... I will have a list of files > ["file1.flac","file2.flac","file3.flac","etc"] and I want the program > to tackle compressing two at a time ... but not more than two at a > time (or four, or eight, or whatever) because that's not going to help > me at all (I have dual cores right now) ... I am having trouble > thinking how I can create the algorithm that would do this for me ... A simple way to do this would be to use poll() instead of wait(). Then you can check both processes for completion in a loop and start a new process when one of the current ones ends. You could keep the list of active processes in a list. Make sure you put a sleep() in the polling loop, otherwise the loop will consume your CPU! Another approach is to use a thread pool with one worker for each process. The thread would call wait() on its child process; when it finishes the thread will take a new task off the queue. There are several thread pool recipes in the Python cookbook, for example http://code.activestate.com/recipes/203871/ http://code.activestate.com/recipes/576576/ (this one has many links to other pool implementations) Kent From damontimm at gmail.com Sun Dec 7 14:58:52 2008 From: damontimm at gmail.com (Damon Timm) Date: Sun, 7 Dec 2008 08:58:52 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <493B601B.10703@mwalsh.org> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> <493B601B.10703@mwalsh.org> Message-ID: <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> On Sun, Dec 7, 2008 at 12:33 AM, Martin Walsh wrote: > I'm not certain this completely explains the poor performance, if at > all, but the communicate method of Popen objects will wait until EOF is > reached and the process ends. So IIUC, in your example the process 'p' > runs to completion and only then is its stdout (p.communicate()[0]) > passed to stdin of 'p2' by the outer communicate call. > > You might try something like this (untested!) ... > > p1 = subprocess.Popen( > ["flac","--decode","--stdout","test.flac"], > stdout=subprocess.PIPE, stderr=subprocess.PIPE > ) > p2 = subprocess.Popen( > ["lame","-","test.mp3"], stdin=p1.stdout, # <-- > stdout=subprocess.PIPE, stderr=subprocess.PIPE > ) > p2.communicate() That did the trick! Got it back down to 20s ... which is what it was taking on the command line. Thanks for that! > Here is my simplistic, not-very-well-thought-out, attempt in > pseudo-code, perhaps it will get you started ... > > paths = ["file1.flac","file2.flac", ... "file11.flac"] > procs = [] > while paths or procs: > procs = [p for p in procs if p.poll() is None] > while paths and len(procs) < 2: > flac = paths.pop(0) > procs.append(Popen(['...', flac], ...)) > time.sleep(1) I think I got a little lost with the "procs = [p for p in procs if p.poll() is None]" statement -- I'm not sure exactly what that is doing ... but otherwise, I think that makes sense ... will have to try it out (if not one of the more "robust" thread pool suggestions (below). On Sun, Dec 7, 2008 at 2:58 AM, Lie Ryan wrote: > I think when you do that (p2.wait() then p3.wait() ), if p3 finishes > first, you wouldn't start another p3 until p2 have finished (i.e. until > p2.wait() returns) and if p2 finishes first, you wouldn't start another > p2 until p3 finishes (i.e. until p3.wait() returns ). > > The solution would be to start and wait() the subprocessess in two > threads. Use threading module or -- if you use python2.6 -- the new > multiprocessing module. > > Alternatively, you could do a "non-blocking wait", i.e. poll the thread. > > while True: > if p1.poll(): # start another p1 > if p2.poll(): # start another p2 Yea, looks like it - I think the trick, for me, will be getting a dynamic list that can be iterated through ... I experimented a little with the .poll() function and I think I follow how it is working ... but really, I am going to have to do a little more "pre-thinking" than I had to do with the bash version ... not sure if I should create a class containing the list of flac files or just a number of functions to handle the list ... whatever way it ends up being, is going to take a little thought to get it straightened out. And the objected oriented part is different than bash -- so, I have to "think different" too. On Sun, Dec 7, 2008 at 8:31 AM, Kent Johnson wrote: > A simple way to do this would be to use poll() instead of wait(). Then > you can check both processes for completion in a loop and start a new > process when one of the current ones ends. You could keep the list of > active processes in a list. Make sure you put a sleep() in the polling > loop, otherwise the loop will consume your CPU! Thanks for that tip - I already throttled my CPU and had to abort the first time (without the sleep() function) ... smile. > Another approach is to use a thread pool with one worker for each > process. The thread would call wait() on its child process; when it > finishes the thread will take a new task off the queue. There are > several thread pool recipes in the Python cookbook, for example > http://code.activestate.com/recipes/203871/ > http://code.activestate.com/recipes/576576/ (this one has many links > to other pool implementations) Oh neat! I will be honest, more than one screen full of code and I get a little overwhelmed (at this point) but I am going to check that idea out. I was thinking something along these lines, where I can send all the input/ouput variables along with a number argument (threads) to a class/function that would then handle everything ... so using a thread pool may make sense ... Looks like I would create a loop that went through the list of all the files to be converted and then sent them all off, one by one, to the thread pool -- which would then just dish them out so that no more than 2 (if I chose that) would be converting at a time? I gotta try and wrap my head around it ... also, I will be using two subprocesses to accomplish a single command (one for stdoutput and the other taking stdinput) as well ... so they have to be packaged together somehow ... hmm! Great help everyone. Not quite as simple as single threading but am learning quite a bit. One day, I will figure it out. Smile. Damon From dineshbvadhia at hotmail.com Sun Dec 7 15:48:27 2008 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Sun, 7 Dec 2008 06:48:27 -0800 Subject: [Tutor] Python on 64-bit ... Message-ID: I ask this question in trepidation but does anyone have experience of Python on 64-bit Windows Vista - there I said it! Feedback on performance and memory usage would be useful to know. Thanks! Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sun Dec 7 16:47:53 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 7 Dec 2008 10:47:53 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> <493B601B.10703@mwalsh.org> <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> Message-ID: <1c2a2c590812070747w36ae6da9i4f8ba9343af43a62@mail.gmail.com> On Sun, Dec 7, 2008 at 8:58 AM, Damon Timm wrote: > On Sun, Dec 7, 2008 at 12:33 AM, Martin Walsh wrote: >> Here is my simplistic, not-very-well-thought-out, attempt in >> pseudo-code, perhaps it will get you started ... >> >> paths = ["file1.flac","file2.flac", ... "file11.flac"] >> procs = [] >> while paths or procs: >> procs = [p for p in procs if p.poll() is None] >> while paths and len(procs) < 2: >> flac = paths.pop(0) >> procs.append(Popen(['...', flac], ...)) >> time.sleep(1) > > I think I got a little lost with the "procs = [p for p in procs if > p.poll() is None]" statement It's called a list comprehension http://personalpages.tds.net/~kent37/kk/00003.html Essentially it creates a new list from all the elements it the old list that are still running. > On Sun, Dec 7, 2008 at 2:58 AM, Lie Ryan wrote: > Yea, looks like it - I think the trick, for me, will be getting a > dynamic list that can be iterated through ... I experimented a little > with the .poll() function and I think I follow how it is working ... > but really, I am going to have to do a little more "pre-thinking" than > I had to do with the bash version ... not sure if I should create a > class containing the list of flac files or just a number of functions > to handle the list ... whatever way it ends up being, is going to take > a little thought to get it straightened out. And the objected > oriented part is different than bash -- so, I have to "think > different" too. I don't think you need any classes for this. A simple list of file names should be fine. A function that takes a file name as a parameter, starts a process to process the file, and returns the resulting Popen object would also be helpful. > On Sun, Dec 7, 2008 at 8:31 AM, Kent Johnson wrote: > Oh neat! I will be honest, more than one screen full of code and I > get a little overwhelmed (at this point) but I am going to check that > idea out. I was thinking something along these lines, where I can > send all the input/ouput variables along with a number argument > (threads) to a class/function that would then handle everything ... so > using a thread pool may make sense ... > > Looks like I would create a loop that went through the list of all the > files to be converted and then sent them all off, one by one, to the > thread pool -- which would then just dish them out so that no more > than 2 (if I chose that) would be converting at a time? Yes, that's right. > I gotta try > and wrap my head around it ... also, I will be using two subprocesses > to accomplish a single command (one for stdoutput and the other taking > stdinput) as well ... so they have to be packaged together somehow ... A function as mentioned above would help. For the threaded solution the function could just start the child process and wait for it to finish, it doesn't have to return anything. Each thread will block on its associated child. Kent From lie.1296 at gmail.com Sun Dec 7 18:14:14 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 7 Dec 2008 17:14:14 +0000 (UTC) Subject: [Tutor] 'for' loops References: <493484D2.50603@socal.rr.com> Message-ID: On Tue, 02 Dec 2008 01:17:41 +0000, Alan Gauld wrote: > while loops are used much less in Python than in other languages because > for loops are so powerful. Actually, I think python's for-loop is so powerful that while loop could be removed from the language and no power would be lost (although many idioms are much better (and faster) written in while loop). This is because python allows infinite-length iterable, you can emulate "while True" loop with something like this: eternity = __import__('itertools').count() for ever in eternity: print "can't stoooop...." # the code above is slightly obfuscated, # I wouldn't normally write it like that # ... # [1] > while lops are generally used in cases where you don't know how many > times you need to loop or you want to loop 'forever'. > > while True: > print 'Can't stop me now!' > > will keep on looping until you close the program > > c = 0 > while c != -1: > c = int(raw_input('Enter a number(-1 to stop) ')) > print c > > will keep looping until the user enters -1 while that can be turned into something like this: for ever in eternity: c = int(raw_input('Enter a number(-1 to stop) ')) print c OR from itertools import repeat from functools import partial def ask(): def stop(): raise StopIteration asker = partial(raw_input, 'Enter a number(-1 to stop) ') for n in repeat(asker): n = n() yield n if n != '-1' else stop() for c in ask(): print c > More info and a comparison with JabaScript and VBScript can be found in > my tutor in the looping topic. [1] I cheated slightly, there is a while loop in the C code for itertools [2] I'm not suggesting that while loop should go away, I'm illustrating the power of the for(ce). From lie.1296 at gmail.com Sun Dec 7 18:25:50 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 7 Dec 2008 17:25:50 +0000 (UTC) Subject: [Tutor] try except block for multiple statements References: Message-ID: On Mon, 01 Dec 2008 20:44:20 -0500, Bryan Fodness wrote: > I would like to use a try except to see if a value exists. But, when I > use the following, if a does not exist it exits. I understand why this > does this, but is there a way to get b,c, and d if a does not exist > without using a try except for every statement? > > try: > fo.write("a = %s\n" %plan.a) > fo.write("b = %s\n" %plan.b) > fo.write("c = %s\n" %plan.c) > fo.write("d = %s\n" %plan.d) > except AttributeError: > pass Or: texts = ["a = %s\n" % plan.a, "b = %s\n" % plan.b, "c = %s\n" % plan.c, "d = %s\n" % plan.d ] for text in texts: try: fo.write(text) except AttributeError: pass From newgat11 at yahoo.com Sun Dec 7 18:58:17 2008 From: newgat11 at yahoo.com (the New me) Date: Sun, 7 Dec 2008 09:58:17 -0800 (PST) Subject: [Tutor] Tutor Digest, Vol 58, Issue 22 Message-ID: <714025.14863.qm@web51006.mail.re2.yahoo.com> That worked, I tried it with string elements also Thanks a bunch --- On Sun, 12/7/08, tutor-request at python.org wrote: > From: tutor-request at python.org > Subject: Tutor Digest, Vol 58, Issue 22 > To: tutor at python.org > Date: Sunday, December 7, 2008, 6:00 AM > 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. Re: Newbie Wondering About Threads (Lie Ryan) > 2. Re: Sorting on different fields (Lie Ryan) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 7 Dec 2008 07:58:11 +0000 (UTC) > From: Lie Ryan > Subject: Re: [Tutor] Newbie Wondering About Threads > To: tutor at python.org > Message-ID: > Content-Type: text/plain; charset=UTF-8 > > On Sat, 06 Dec 2008 21:43:11 -0500, Damon Timm wrote: > > > On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter > > > wrote: > >> I'm on my phone so excuse the simple reply. > From what I skimmed you are > >> wrapping shell commands which is what I do all the > time. Some hints. 1) > >> look into popen or subprocess in place of execute > for more flexibility. > >> I use popen a lot and assigning a popen call to an > object name let's > >> you parse the output and make informed decisions > depending on what the > >> shell program outputs. > > > > So I took a peak at subprocess.Popen --> looks like > that's the direction > > I would be headed for parallel processes ... a real > simple way to see it > > work for me was: > > > > p2 = > subprocess.Popen(["lame","--silent","test.wav","test.mp3"]) > > > p3 = > subprocess.Popen(["lame","--silent","test2.wav","test2.mp3"]) > > > p2.wait() > > p3.wait() > > I think when you do that (p2.wait() then p3.wait() ), if p3 > finishes > first, you wouldn't start another p3 until p2 have > finished (i.e. until > p2.wait() returns) and if p2 finishes first, you > wouldn't start another > p2 until p3 finishes (i.e. until p3.wait() returns ). > > The solution would be to start and wait() the subprocessess > in two > threads. Use threading module or -- if you use python2.6 -- > the new > multiprocessing module. > > Alternatively, you could do a "non-blocking > wait", i.e. poll the thread. > > while True: > if p1.poll(): # start another p1 > if p2.poll(): # start another p2 > > > > > top showed that both cores get busy and it takes half > the time! So > > that's great -- when I tried to add the flac > decoding through stdout I > > was able to accomplish it as well ... I was mimicing > the command of > > "flac --decode --stdout test.flac | lame - > test.mp3" ... see: > > > > p = > subprocess.Popen(["flac","--decode","--stdout","test.flac"], > > stdout=subprocess.PIPE) > > p2 = > subprocess.Popen(["lame","-","test.mp3"], > stdin=subprocess.PIPE) > > p2.communicate(p.communicate()[0]) > > > > That did the trick - it worked! However, it was > *very* slow! The > > python script has a "real" time of 2m22.504s > whereas if I run it from > > the command line it is only 0m18.594s. Not sure why > this is ... > > > > The last piece of my puzzle though, I am having > trouble wrapping my head > > around ... I will have a list of files > > > ["file1.flac","file2.flac","file3.flac","etc"] > and I want the program to > > tackle compressing two at a time ... but not more than > two at a time (or > > four, or eight, or whatever) because that's not > going to help me at all > > (I have dual cores right now) ... I am having trouble > thinking how I can > > create the algorithm that would do this for me ... > > > > Thanks everyone. Maybe after a good night's sleep > it will come to me. > > If you have any ideas - would love to hear them. > > > > ------------------------------ > > Message: 2 > Date: Sun, 7 Dec 2008 10:31:11 +0000 (UTC) > From: Lie Ryan > Subject: Re: [Tutor] Sorting on different fields > To: tutor at python.org > Message-ID: > Content-Type: text/plain; charset=UTF-8 > > On Sat, 06 Dec 2008 21:47:16 -0800, the New me wrote: > > > is there a straightforward example? > > >>> import operator > >>> k = [[1, 2, 3, 4], [4, 3, 2, 1], [1, 3, 2, 4], > [2, 4, 3, 1]] > >>> sorted(l, key=operator.itemgetter(3, 2)) > [[4, 3, 2, 1], [2, 4, 3, 1], [1, 3, 2, 4], [1, 2, 3, 4]] > >>> for k in sorted(l, key=operator.itemgetter(0, > 2)): print k > ... > [1, 3, 2, 4] > [1, 2, 3, 4] > [2, 4, 3, 1] > [4, 3, 2, 1] > >>> def genkey(item): > ... return item[2] > ... > >>> for k in sorted(l, key=genkey): print k > ... > [4, 3, 2, 1] > [1, 3, 2, 4] > [1, 2, 3, 4] > [2, 4, 3, 1] > >>> def genkey(item): > ... return item[2], item[1] > ... > >>> for k in sorted(l, key=genkey): print k > ... > [4, 3, 2, 1] > [1, 3, 2, 4] > [1, 2, 3, 4] > [2, 4, 3, 1] > > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 58, Issue 22 > ************************************* From robert.johansson at math.umu.se Sun Dec 7 18:34:18 2008 From: robert.johansson at math.umu.se (Robert Johansson) Date: Sun, 7 Dec 2008 18:34:18 +0100 Subject: [Tutor] Psyco module Message-ID: <000901c95892$08655d20$19301760$@johansson@math.umu.se> Dear all, I'm trying to get my Python stuff to work on my new computer but I have problems getting the Psyco module installed. First I tried Python 2.6 (which worked fine) but discovered that Psyco wasn't prepared for it. I uninstalled Python 2.6 and when for 2.5.2 instead, but the problem with installing Psyco remains. Anyone how knows what to do? /Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sun Dec 7 20:45:12 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 7 Dec 2008 14:45:12 -0500 Subject: [Tutor] try except block for multiple statements In-Reply-To: References: Message-ID: <1c2a2c590812071145m48160d1chfea4eac186954d46@mail.gmail.com> On Sun, Dec 7, 2008 at 12:25 PM, Lie Ryan wrote: > texts = ["a = %s\n" % plan.a, > "b = %s\n" % plan.b, > "c = %s\n" % plan.c, > "d = %s\n" % plan.d > ] > > for text in texts: > try: > fo.write(text) > except AttributeError: > pass No, the AttributeError will come during the creation of texts, not in the loop. Kent From kent37 at tds.net Sun Dec 7 20:45:55 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 7 Dec 2008 14:45:55 -0500 Subject: [Tutor] Psyco module In-Reply-To: <6717622033615662971@unknownmsgid> References: <6717622033615662971@unknownmsgid> Message-ID: <1c2a2c590812071145y4d2b3e77hdb09baa2182df828@mail.gmail.com> On Sun, Dec 7, 2008 at 12:34 PM, Robert Johansson wrote: > Dear all, I'm trying to get my Python stuff to work on my new computer but I > have problems getting the Psyco module installed. First I tried Python 2.6 > (which worked fine) but discovered that Psyco wasn't prepared for it. I > uninstalled Python 2.6 and when for 2.5.2 instead, but the problem with > installing Psyco remains. Anyone how knows what to do? What OS? What problem do you have? Kent From damontimm at gmail.com Sun Dec 7 21:10:46 2008 From: damontimm at gmail.com (Damon Timm) Date: Sun, 7 Dec 2008 15:10:46 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <1c2a2c590812070747w36ae6da9i4f8ba9343af43a62@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> <493B601B.10703@mwalsh.org> <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> <1c2a2c590812070747w36ae6da9i4f8ba9343af43a62@mail.gmail.com> Message-ID: <262679b50812071210s651f35c4hac9e6a5f7dd98e11@mail.gmail.com> On Sun, Dec 7, 2008 at 10:47 AM, Kent Johnson wrote: > A function as mentioned above would help. For the threaded solution > the function could just start the child process and wait for it to > finish, it doesn't have to return anything. Each thread will block on > its associated child. I think I did it! Woo hoo! (cheers all around! drinks on me!) First, I found that using the Popen.communicate() function wasn't going to work (because it sits there and waits for until it's done before continuing); so, I ditched that, created my own little function that returned the Popen object and went from there ... I mixed in one super-long audio file file with all the others it seems to work without a hitch (so far) ... watching top I see both processors running at max during the lame processing. Check it out (there are probably sexier ways to populate the *.mp3 files but I was more interested in the threads): --- import time import subprocess totProcs = 2 #number of processes to spawn before waiting flacFiles = [["test.flac","test.mp3"],["test2.flac","test2.mp3"],\ ["test3.flac","test3.mp3"],["test4.flac","test4.mp3"],\ ["test5.flac","test5.mp3"],["test6.flac","test6.mp3"]] procs = [] def flac_to_mp3(flacfile,mp3file): print "beginning to process " + flacfile p = subprocess.Popen(["flac","--decode","--stdout","--silent",flacfile], stdout=subprocess.PIPE) p1 = subprocess.Popen(["lame","--silent","-",mp3file], stdin=p.stdout) return p1 while flacFiles or procs: procs = [p for p in procs if p.poll() is None] while flacFiles and len(procs) < totProcs: file = flacFiles.pop(0) procs.append(flac_to_mp3(file[0],file[1])) time.sleep(1) ------[EOF]------ Thanks again - onward I go! Damon From chudxx at gmail.com Sun Dec 7 22:14:14 2008 From: chudxx at gmail.com (R. Santos) Date: Sun, 7 Dec 2008 13:14:14 -0800 Subject: [Tutor] selecting attributes of all elements in numpy arrays Message-ID: <41c7a4a50812071314vdad7143wd64892a76dcd894d@mail.gmail.com> I have a numpy recarray (rd) with a datetime field (bar_dt) and I want to filter the recarray by year. So far, the only way I've been able to do this is by using rd[np.array([i.year for i in rd.bar_dt])==2008]. Is there a better way to do this? It seems like I must be overlooking something. Thanks for any help. From kent37 at tds.net Mon Dec 8 03:35:34 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 7 Dec 2008 21:35:34 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <262679b50812071210s651f35c4hac9e6a5f7dd98e11@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> <493B601B.10703@mwalsh.org> <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> <1c2a2c590812070747w36ae6da9i4f8ba9343af43a62@mail.gmail.com> <262679b50812071210s651f35c4hac9e6a5f7dd98e11@mail.gmail.com> Message-ID: <1c2a2c590812071835j1dac192cm5c88df65397fed01@mail.gmail.com> On Sun, Dec 7, 2008 at 3:10 PM, Damon Timm wrote: > I think I did it! Woo hoo! (cheers all around! drinks on me!) Cool! Where are we meeting for drinks? ;-) > flacFiles = [["test.flac","test.mp3"],["test2.flac","test2.mp3"],\ > ["test3.flac","test3.mp3"],["test4.flac","test4.mp3"],\ > ["test5.flac","test5.mp3"],["test6.flac","test6.mp3"]] There is no need to include both the flac file name and the mp3 file name if the roots match. You can use os.path functions to split the extension or the quick-and-dirty way: mp3file = flacfile.rsplit('.', 1)[0] + '.mp3' Kent From damontimm at gmail.com Mon Dec 8 03:46:00 2008 From: damontimm at gmail.com (Damon Timm) Date: Sun, 7 Dec 2008 21:46:00 -0500 Subject: [Tutor] Newbie Wondering About Threads In-Reply-To: <1c2a2c590812071835j1dac192cm5c88df65397fed01@mail.gmail.com> References: <262679b50812061508p50306f2dg51d5aa2c5421c293@mail.gmail.com> <262679b50812061843oe8ebf9fkc964e2ac5f986d3e@mail.gmail.com> <493B601B.10703@mwalsh.org> <262679b50812070558y5c63dc61ra81346bbd122476f@mail.gmail.com> <1c2a2c590812070747w36ae6da9i4f8ba9343af43a62@mail.gmail.com> <262679b50812071210s651f35c4hac9e6a5f7dd98e11@mail.gmail.com> <1c2a2c590812071835j1dac192cm5c88df65397fed01@mail.gmail.com> Message-ID: <262679b50812071846y52e6eadel4726f49c14a9af62@mail.gmail.com> On Sun, Dec 7, 2008 at 9:35 PM, Kent Johnson wrote: > There is no need to include both the flac file name and the mp3 file > name if the roots match. You can use os.path functions to split the > extension or the quick-and-dirty way: > mp3file = flacfile.rsplit('.', 1)[0] + '.mp3' That is *so* what I was looking for! You guys are awesome. Damon > > Kent > From pythonnutter at gmail.com Mon Dec 8 12:49:07 2008 From: pythonnutter at gmail.com (Python Nutter) Date: Mon, 8 Dec 2008 22:49:07 +1100 Subject: [Tutor] Psyco module In-Reply-To: <1c2a2c590812071145y4d2b3e77hdb09baa2182df828@mail.gmail.com> References: <6717622033615662971@unknownmsgid> <1c2a2c590812071145y4d2b3e77hdb09baa2182df828@mail.gmail.com> Message-ID: It only runs on Intel 386-compatible processors. Once we know what CPU you are using then we can figure it out better. From lawrence.wickline at gmail.com Mon Dec 8 17:55:40 2008 From: lawrence.wickline at gmail.com (Lawrence Wickline) Date: Mon, 8 Dec 2008 08:55:40 -0800 Subject: [Tutor] Sorting a dictionary on a value in a list. In-Reply-To: References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> <045316B5-CB8D-4C92-9CA4-E23FC5B3B08D@gmail.com> Message-ID: <9A73889B-3727-4289-BDDE-43642B5267EB@gmail.com> On Dec 6, 2008, at 12:41 AM, Lie Ryan wrote: > > In most cases, in processing involving networking, the bottleneck is > the > network speed itself. To speed things up by optimizing your own code > might not make your download significantly faster (getting 60 seconds > faster is great for scripts that usually runs for 70 seconds, but is a > waste of development time for scripts that usually run for 1 hour) > > Usually a multi-threading downloader might be a better chance to > improvement, especially for 1) downloading from different site, 2) > the > remote sites have speed limit, 3) you have faster download link than > the > server can gives In this particular case everything is on the local network. This is actually part of a hadoop map/reduce system I am learning, so reducing cpu is of high value. if network pull times become and issue the cluster can be expanded and the time between pulls can be reduced. As of this morning I am being directed to make the reducer usable both in the mapper and then again as a reducer. This has forced me to rework everything to work so that it can be called as a module. I have never learned java so that wasn't' an option and the more I am working with it python seems to be the perfect fit for hadoop type work. Really fun stuff. From sander.sweers at gmail.com Mon Dec 8 18:15:36 2008 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 8 Dec 2008 18:15:36 +0100 Subject: [Tutor] File IO flush Message-ID: Hello All, I was playing around with the zipfile module and wrote a simple script to unzip a zip file. I then looked around on the internet and found the recipe 252508 [1] on the active state cookbook website. In this recipe the author calls flush() and then close() on a file object being written to disk. Now I understand what flush does but would close() also flush the buffers to disk? Thanks Sander [1] http://code.activestate.com/recipes/252508/ From kent37 at tds.net Mon Dec 8 18:55:55 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 8 Dec 2008 12:55:55 -0500 Subject: [Tutor] File IO flush In-Reply-To: References: Message-ID: <1c2a2c590812080955p309b992eide1f26d4940b291f@mail.gmail.com> On Mon, Dec 8, 2008 at 12:15 PM, Sander Sweers wrote: > Hello All, I was playing around with the zipfile module and wrote a > simple script to unzip a zip file. I then looked around on the > internet and found the recipe 252508 [1] on the active state cookbook > website. > > In this recipe the author calls flush() and then close() on a file > object being written to disk. Now I understand what flush does but > would close() also flush the buffers to disk? Yes, close() will flush first; AFAIK there is no reason to call flush() immediately before close() on the same file object. Kent From jay at splitstreams.com Mon Dec 8 19:01:00 2008 From: jay at splitstreams.com (Jay Deiman) Date: Mon, 08 Dec 2008 12:01:00 -0600 Subject: [Tutor] rrdtool examples. In-Reply-To: <1228440339.19777.21.camel@M13425> References: <1228440339.19777.21.camel@M13425> Message-ID: <493D60DC.5020802@splitstreams.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jeremiah Jester wrote: > Is anyone on here using the python-rrdtool module for graphing and > analysis? If so, do you have some sample scripts you could show me. > There doesn't seem to be a lot out there as far as real world python > examples. > > Thanks, > JJ Actually, I was just playing around with the rrdtool library for python last week. It turns out that you basically just use the command line options as options for the different method calls (like create()). All you really have to do is check out the man pages for rrdtool to have all the documentation you need for the rrdtool python module. You literally pass in the command line opts just as they would appear on the command line. Here is a quick little script that I whipped up for playing purposes: - ------------------------ #!/usr/bin/env python import rrdtool , time , random stime = int(time.time()) - 5 * 86400 dpoints = 1000 etime = stime + (dpoints * 300) fname = 'test.rrd' gfname = 'test.png' rrdtool.create('test.rrd' , '--start' , str(stime) , 'DS:speed:COUNTER:600:U:U' , 'RRA:AVERAGE:0.5:1:576' , 'RRA:AVERAGE:0.5:6:336' ) ctime = stime cmiles = 0 for i in xrange(dpoints): bump = random.randint(1 , 20) cmiles += bump ctime += 300 rrdtool.update(fname , '%d:%d' % (ctime , cmiles)) rrdtool.graph(gfname , '--start' , str(etime - (24 * 3600)) , '--end' , str(etime) , '--vertical-label' , 'Speed m/h' , '--imgformat' , 'PNG' , '--title' , 'Speeds' , '--lower-limit' , '0' , 'DEF:myspeed=%s:speed:AVERAGE' % fname , 'CDEF:mph=myspeed,3600,*' , 'VDEF:msmax=mph,MAXIMUM' , 'VDEF:msavg=mph,AVERAGE' , 'VDEF:msmin=mph,MINIMUM' , 'VDEF:mspct=mph,95,PERCENT' , 'LINE1:mph#FF0000:My Speed' , r'GPRINT:msmax:Max\: %6.1lf mph' , r'GPRINT:msavg:Avg\: %6.1lf mph' , r'GPRINT:msmin:Min\: %6.1lf mph\l' , r'GPRINT:mspct:95th Perc\: %6.1lf mph\l' ) - ------------------------ That, coupled with the rrdtool man pages (which are very good, complete with examples) should be enough to get you started. - -- Jay Deiman \033:wq! -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkk9YNwACgkQQ0lr+ZVKSBiWTQCgoBuzQVeRHBlrJ7GONQAL0RFT qOwAn3cnbZot0q1qGf6mOFHS8QgQc53o =h7CZ -----END PGP SIGNATURE----- From jeremiah.jester at panasonic.aero Mon Dec 8 20:11:11 2008 From: jeremiah.jester at panasonic.aero (Jeremiah Jester) Date: Mon, 08 Dec 2008 11:11:11 -0800 Subject: [Tutor] rrdtool examples. In-Reply-To: <493D60DC.5020802@splitstreams.com> References: <1228440339.19777.21.camel@M13425> <493D60DC.5020802@splitstreams.com> Message-ID: <1228763471.9472.18.camel@M13425> Thanks Jay. This helps! JJ On Mon, 2008-12-08 at 10:01 -0800, Jay Deiman wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Jeremiah Jester wrote: > > Is anyone on here using the python-rrdtool module for graphing and > > analysis? If so, do you have some sample scripts you could show me. > > There doesn't seem to be a lot out there as far as real world python > > examples. > > > > Thanks, > > JJ > > Actually, I was just playing around with the rrdtool library for > python > last week. It turns out that you basically just use the command line > options as options for the different method calls (like create()). > All > you really have to do is check out the man pages for rrdtool to have > all > the documentation you need for the rrdtool python module. You > literally > pass in the command line opts just as they would appear on the command > line. Here is a quick little script that I whipped up for playing > purposes: > - ------------------------ > > #!/usr/bin/env python > > import rrdtool , time , random > > stime = int(time.time()) - 5 * 86400 > dpoints = 1000 > etime = stime + (dpoints * 300) > fname = 'test.rrd' > gfname = 'test.png' > > rrdtool.create('test.rrd' , > '--start' , str(stime) , > 'DS:speed:COUNTER:600:U:U' , > 'RRA:AVERAGE:0.5:1:576' , > 'RRA:AVERAGE:0.5:6:336' > ) > > ctime = stime > cmiles = 0 > for i in xrange(dpoints): > bump = random.randint(1 , 20) > cmiles += bump > ctime += 300 > rrdtool.update(fname , '%d:%d' % (ctime , cmiles)) > > rrdtool.graph(gfname , > '--start' , str(etime - (24 * 3600)) , > '--end' , str(etime) , > '--vertical-label' , 'Speed m/h' , > '--imgformat' , 'PNG' , > '--title' , 'Speeds' , > '--lower-limit' , '0' , > 'DEF:myspeed=%s:speed:AVERAGE' % fname , > 'CDEF:mph=myspeed,3600,*' , > 'VDEF:msmax=mph,MAXIMUM' , > 'VDEF:msavg=mph,AVERAGE' , > 'VDEF:msmin=mph,MINIMUM' , > 'VDEF:mspct=mph,95,PERCENT' , > 'LINE1:mph#FF0000:My Speed' , > r'GPRINT:msmax:Max\: %6.1lf mph' , > r'GPRINT:msavg:Avg\: %6.1lf mph' , > r'GPRINT:msmin:Min\: %6.1lf mph\l' , > r'GPRINT:mspct:95th Perc\: %6.1lf mph\l' > ) > > - ------------------------ > > That, coupled with the rrdtool man pages (which are very good, > complete > with examples) should be enough to get you started. > > - -- > Jay Deiman > > \033:wq! > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.9 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org > > iEYEARECAAYFAkk9YNwACgkQQ0lr+ZVKSBiWTQCgoBuzQVeRHBlrJ7GONQAL0RFT > qOwAn3cnbZot0q1qGf6mOFHS8QgQc53o > =h7CZ > -----END PGP SIGNATURE----- > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. From damontimm at gmail.com Tue Dec 9 01:05:36 2008 From: damontimm at gmail.com (Damon Timm) Date: Mon, 8 Dec 2008 19:05:36 -0500 Subject: [Tutor] list.index() question Message-ID: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> Hi again! (Now that everyone was so helpful the first time you'll never get rid of me!) I had a question about using the index() function on a list -- as I walk the directory path, I want to see if a directory contains any files ending in a certain type ... if it does, I wanna do some stuff ... if not, I would like to move on ... . for dirpath, subFolders, files in os.walk(rootDir): try: i = files.index("*.flac") #how do I make it search for files that end in ".flac" ? for file in files: #do some things in here to sort my files except ValueError: pass Basically: how do I make it match *.flac ? I couldn't find anything on google (searching for "python index" just gets me a lot of indexes of python docs - wink) Thanks again, Damon From john at fouhy.net Tue Dec 9 01:29:54 2008 From: john at fouhy.net (John Fouhy) Date: Tue, 9 Dec 2008 13:29:54 +1300 Subject: [Tutor] list.index() question In-Reply-To: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> References: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> Message-ID: <5e58f2e40812081629m1927e787u53c252e760533974@mail.gmail.com> On 09/12/2008, Damon Timm wrote: > Basically: how do I make it match *.flac ? I couldn't find anything > on google (searching for "python index" just gets me a lot of indexes > of python docs - wink) Hi Damon, The fnmatch module will help here. It basically implements unix-style filename patterns. For example: import os import fnmatch files = os.listdir('.') flac_files = fnmatch(files, '*.flac') So, to test whether you have any flac files, you can just test whether fnmatch(files, '*.flac') is empty. If you wanted to roll your own solution (the fnmatch module is a bit obscure, I think), you could do something with os.path.splitext: files = os.listdir('.') extensions = [os.path.splitext(f)[1] for f in files] if '.flac' in extensions: print 'FLAC files found!' HTH! -- John. From alan.gauld at btinternet.com Tue Dec 9 01:47:17 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 9 Dec 2008 00:47:17 -0000 Subject: [Tutor] list.index() question References: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> Message-ID: "Damon Timm" wrote > walk the directory path, I want to see if a directory contains any > files ending in a certain type ... if it does, I wanna do some stuff Check out the glob module. > for dirpath, subFolders, files in os.walk(rootDir): > try: > i = files.index("*.flac") #how do I make it search for files > that end in ".flac" ? If yu call glob.glob() with the dirpath you will get a list of all the flac files in the current dir. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Tue Dec 9 01:55:24 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 8 Dec 2008 19:55:24 -0500 Subject: [Tutor] list.index() question In-Reply-To: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> References: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> Message-ID: <1c2a2c590812081655s53847e9er74f4f0e7db7377ee@mail.gmail.com> On Mon, Dec 8, 2008 at 7:05 PM, Damon Timm wrote: > Hi again! > > (Now that everyone was so helpful the first time you'll never get rid of me!) That's fine, pretty soon you'll be answering other people's questions :-) > I had a question about using the index() function on a list -- as I > walk the directory path, I want to see if a directory contains any > files ending in a certain type ... if it does, I wanna do some stuff > ... if not, I would like to move on ... . index() searches for a specific matching item, it doesn't have any wildcard ability. > for dirpath, subFolders, files in os.walk(rootDir): > try: > i = files.index("*.flac") #how do I make it search for files > that end in ".flac" ? > for file in files: > #do some things in here to sort my files > except ValueError: > pass I'm not sure what you want to do, but if you just want to operate on files that end with .flac, you can just say for file in files: if not file.endswith('.flac'): continue though 'file' is not a good variable name, there is a builtin of that name. > Basically: how do I make it match *.flac ? I couldn't find anything > on google (searching for "python index" just gets me a lot of indexes > of python docs - wink) There is actually an index: http://docs.python.org/genindex.html Kent From damontimm at gmail.com Tue Dec 9 02:17:15 2008 From: damontimm at gmail.com (Damon Timm) Date: Mon, 8 Dec 2008 20:17:15 -0500 Subject: [Tutor] list.index() question In-Reply-To: <1c2a2c590812081655s53847e9er74f4f0e7db7377ee@mail.gmail.com> References: <262679b50812081605s1888bf06vcbca2de976f89013@mail.gmail.com> <1c2a2c590812081655s53847e9er74f4f0e7db7377ee@mail.gmail.com> Message-ID: <262679b50812081717y11590b2eu2dbe37d30b9f8ea8@mail.gmail.com> On Mon, Dec 8, 2008 at 7:55 PM, Kent Johnson wrote: > index() searches for a specific matching item, it doesn't have any > wildcard ability. Ah ha! > There is actually an index: > http://docs.python.org/genindex.html Heh heh - and the info I was looking for is at: http://docs.python.org/library/stdtypes.html#index-584 ... I've become google dependent ... if it's not on google I don't know where to look. Thanks for the .endswith() tip. On 12/8/08 7:47 PM, Alan Gauld wrote: > Check out the glob module. > >> for dirpath, subFolders, files in os.walk(rootDir): >> try: >> i = files.index("*.flac") #how do I make it search for files >> that end in ".flac" ? > > If yu call glob.glob() with the dirpath you will get a list of all > the flac files in the current dir. Heading to check out glob.glob() now ... On 12/8/08 7:29 PM, John Fouhy wrote: > The fnmatch module will help here. It basically implements unix-style > filename patterns. For example: > > import os > import fnmatch > > files = os.listdir('.') > flac_files = fnmatch(files, '*.flac') > > So, to test whether you have any flac files, you can just test whether > fnmatch(files, '*.flac') is empty. > > If you wanted to roll your own solution (the fnmatch module is a bit > obscure, I think), you could do something with os.path.splitext: > > files = os.listdir('.') > extensions = [os.path.splitext(f)[1] for f in files] > if '.flac' in extensions: > print 'FLAC files found!' And then to look at fnmatch! Thanks for the direction -- on my way ... On 12/8/08 7:55 PM, Kent Johnson wrote: > On Mon, Dec 8, 2008 at 7:05 PM, Damon Timm wrote: >> Hi again! >> >> (Now that everyone was so helpful the first time you'll never get rid of me!) > > That's fine, pretty soon you'll be answering other people's questions :-) Not quite there yet ... one day, maybe. I can show people where the index for index is! Damon From lie.1296 at gmail.com Tue Dec 9 22:06:19 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 9 Dec 2008 21:06:19 +0000 (UTC) Subject: [Tutor] Sorting a dictionary on a value in a list. References: <8191FB37-C507-446C-933F-246C5073689D@gmail.com> <1c2a2c590812031742m57ed6f0av1b34fa8f6331a548@mail.gmail.com> <045316B5-CB8D-4C92-9CA4-E23FC5B3B08D@gmail.com> <9A73889B-3727-4289-BDDE-43642B5267EB@gmail.com> Message-ID: On Mon, 08 Dec 2008 08:55:40 -0800, Lawrence Wickline wrote: > On Dec 6, 2008, at 12:41 AM, Lie Ryan wrote: >> >> In most cases, in processing involving networking, the bottleneck is >> the >> network speed itself. To speed things up by optimizing your own code >> might not make your download significantly faster (getting 60 seconds >> faster is great for scripts that usually runs for 70 seconds, but is a >> waste of development time for scripts that usually run for 1 hour) >> >> Usually a multi-threading downloader might be a better chance to >> improvement, especially for 1) downloading from different site, 2) the >> remote sites have speed limit, 3) you have faster download link than >> the >> server can gives > > > In this particular case everything is on the local network. This is > actually part of a hadoop map/reduce system I am learning, so reducing > cpu is of high value. if network pull times become and issue the cluster > can be expanded and the time between pulls can be reduced. As of this > morning I am being directed to make the reducer usable both in the > mapper and then again as a reducer. This has forced me to rework > everything to work so that it can be called as a module. > Just don't forget about the profile and cProfile module, it might reveal hidden bottlenecks. From gmorris at serif.com Wed Dec 10 13:48:38 2008 From: gmorris at serif.com (Gareth at Serif) Date: Wed, 10 Dec 2008 04:48:38 -0800 (PST) Subject: [Tutor] MP3Info class usage Message-ID: <20934673.post@talk.nabble.com> Has anyone ever used MP3Info to retrieve the ID3 tags from an MP3? It seems to be a well written class that can access ID3 tags in v.1 or v.2, but I've not used classes before and I'm struggling to figure out how to use it. Given that, for testing purposses, I have a property called 'testfile' set to the path of a valid MP3 file, I then try to output the artist name with the following, where ID3v2 is a class defined in MP3Info: song = ID3v2(testfile) print '\nArtist is: %s' % (song.artist) When I execute this script I get an error, "AttributeError: 'str' object has no attribute 'seek'". Is this a bug in the class or am I not using it correctly? Has anyone any better suggestions for retreiving ID3 tags from an MP3 file using python? Regards, Gareth -- View this message in context: http://www.nabble.com/MP3Info-class-usage-tp20934673p20934673.html Sent from the Python - tutor mailing list archive at Nabble.com. From tmz at pobox.com Wed Dec 10 15:05:34 2008 From: tmz at pobox.com (Todd Zullinger) Date: Wed, 10 Dec 2008 09:05:34 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: <20934673.post@talk.nabble.com> References: <20934673.post@talk.nabble.com> Message-ID: <20081210140534.GM13722@inocybe.teonanacatl.org> Gareth at Serif wrote: > Has anyone any better suggestions for retreiving ID3 tags from an > MP3 file using python? I'd recommend eyeD3? and/or mutagen? for tag reading. Both are pretty easy to use. ? http://eyed3.nicfit.net/ ? http://code.google.com/p/quodlibet/wiki/Development/Mutagen -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A paranoid is someone who knows a little of what's going on. -- William S. Burroughs -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From damontimm at gmail.com Wed Dec 10 15:12:46 2008 From: damontimm at gmail.com (Damon Timm) Date: Wed, 10 Dec 2008 09:12:46 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: <20081210140534.GM13722@inocybe.teonanacatl.org> References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> Message-ID: <262679b50812100612v78d5b49fh1acb94d861249ebf@mail.gmail.com> On 12/10/08, Todd Zullinger wrote: > I'd recommend eyeD3? and/or mutagen? for tag reading. Both are pretty > easy to use. I would second eyeD3 -- I use the command line version and it is pretty versatile. D > > ? http://eyed3.nicfit.net/ > ? http://code.google.com/p/quodlibet/wiki/Development/Mutagen > > -- > Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > A paranoid is someone who knows a little of what's going on. > -- William S. Burroughs > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > From kent37 at tds.net Wed Dec 10 15:20:25 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 10 Dec 2008 09:20:25 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: <20934673.post@talk.nabble.com> References: <20934673.post@talk.nabble.com> Message-ID: <1c2a2c590812100620n396a6ecs5afa8fa2caf0e524@mail.gmail.com> On Wed, Dec 10, 2008 at 7:48 AM, Gareth at Serif wrote: > Given that, for testing purposses, I have a property called 'testfile' set > to the path of a valid MP3 file, I then try to output the artist name with > the following, where ID3v2 is a class defined in MP3Info: > song = ID3v2(testfile) > print '\nArtist is: %s' % (song.artist) > > When I execute this script I get an error, "AttributeError: 'str' object has > no attribute 'seek'". The ID3v2 constructor wants a file object as a parameter, not a string. It is trying to call testfile.seek() which causes the error you see. Try ID3v2(open(testfile)) Kent From lie.1296 at gmail.com Wed Dec 10 15:55:20 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 10 Dec 2008 14:55:20 +0000 (UTC) Subject: [Tutor] MP3Info class usage References: <20934673.post@talk.nabble.com> Message-ID: On Wed, 10 Dec 2008 04:48:38 -0800, Gareth at Serif wrote: > Has anyone ever used MP3Info to retrieve the ID3 tags from an MP3? It > seems to be a well written class that can access ID3 tags in v.1 or v.2, > but I've not used classes before and I'm struggling to figure out how to > use it. > > Given that, for testing purposses, I have a property called 'testfile' > set to the path of a valid MP3 file, I then try to output the artist > name with the following, where ID3v2 is a class defined in MP3Info: song > = ID3v2(testfile) > print '\nArtist is: %s' % (song.artist) > > When I execute this script I get an error, "AttributeError: 'str' object > has no attribute 'seek'". Is this a bug in the class or am I not using > it correctly? Has anyone any better suggestions for retreiving ID3 tags > from an MP3 file using python? > > Regards, > Gareth You need to know about file interface. The testfile you gave is neither a file object nor a file-like object but a string while the function ID3v2 expects a file or file-like object. In short, ID3v2 tries to call testfile.seek() which doesn't exists. From jtp at nc.rr.com Wed Dec 10 16:18:24 2008 From: jtp at nc.rr.com (James) Date: Wed, 10 Dec 2008 10:18:24 -0500 Subject: [Tutor] sending html and text emails Message-ID: All, I'm trying to figure out a way to send html email using Python. I've found scripts such as the one in the link below: http://www.stevecoursen.com/2008/10/sending-html-email-from-python.html/ These don't seem to work well. I took a (very) basic html page and had the script in the page above read that page and then set up the text / html email. When my client received the message it was filled with visible html tags. The ultimate goal is to have a script that displays a simple HTML table that's easily readable by anyone using a client capable of interpreting html emails. Thoughts? -j From nephish at gmail.com Wed Dec 10 19:23:48 2008 From: nephish at gmail.com (shawn bright) Date: Wed, 10 Dec 2008 12:23:48 -0600 Subject: [Tutor] how to run a process forever Message-ID: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> Hey gents, I have an interesting problem. I need to have a python script start when a computer boots up, and i need it to run forever. I also am going to run a script by cron that will check to see if the process is running, if not, i need a python script to execute the script. What would be a good way to go about this? A few ways are possible, just asking for what you guys might thing the best solution is. thanks, shawn From steve at alchemy.com Wed Dec 10 19:28:37 2008 From: steve at alchemy.com (Steve Willoughby) Date: Wed, 10 Dec 2008 10:28:37 -0800 Subject: [Tutor] how to run a process forever In-Reply-To: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> Message-ID: <20081210182837.GC87165@dragon.alchemy.com> On Wed, Dec 10, 2008 at 12:23:48PM -0600, shawn bright wrote: > Hey gents, > > I have an interesting problem. I need to have a python script start > when a computer boots up, and i need it to run forever. > I also am going to run a script by cron that will check to see if the > process is running, if not, i need a python script to execute > the script. Maybe I'm misunderstanding your meaning, but usually programs which do this don't do anything unusual other than... well... simply run forever in the background (launched from the startup scripts or cron, disassociated from any TTY, of course). A watchdog re-launcher cron script is also not uncommon. If your script isn't actively doing something it's good to put it to sleep until some interesting event (or timer) wakes it up. Was there something beyond that you were looking for? -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From srilyk at gmail.com Wed Dec 10 19:34:32 2008 From: srilyk at gmail.com (W W) Date: Wed, 10 Dec 2008 12:34:32 -0600 Subject: [Tutor] how to run a process forever In-Reply-To: <20081210182837.GC87165@dragon.alchemy.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> <20081210182837.GC87165@dragon.alchemy.com> Message-ID: <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> while True: is also a common way to make your program "run forever". Although you still need to create some sleep/wait function or it will eat up your available processes (so I'm told). -HTH, Wayne On Wed, Dec 10, 2008 at 12:28 PM, Steve Willoughby wrote: > On Wed, Dec 10, 2008 at 12:23:48PM -0600, shawn bright wrote: > > Hey gents, > > > > I have an interesting problem. I need to have a python script start > > when a computer boots up, and i need it to run forever. > > I also am going to run a script by cron that will check to see if the > > process is running, if not, i need a python script to execute > > the script. > > Maybe I'm misunderstanding your meaning, but usually programs which do > this don't do anything unusual other than... well... simply run forever > in the background (launched from the startup scripts or cron, disassociated > from any TTY, of course). A watchdog re-launcher cron script is also > not uncommon. > > If your script isn't actively doing something it's good to put it to > sleep until some interesting event (or timer) wakes it up. > > Was there something beyond that you were looking for? > > -- > Steve Willoughby | Using billion-dollar satellites > steve at alchemy.com | to hunt for Tupperware. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- 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 nephish at gmail.com Wed Dec 10 20:15:18 2008 From: nephish at gmail.com (shawn bright) Date: Wed, 10 Dec 2008 13:15:18 -0600 Subject: [Tutor] how to run a process forever In-Reply-To: <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> <20081210182837.GC87165@dragon.alchemy.com> <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> Message-ID: <384c93600812101115g3496fdb1x5b780ffbb65a9943@mail.gmail.com> Sorry, was not very specific in my request. say i have a script like while 1: do_something_cool() time.sleep(2) i am running this on a linux computer. How would i check that it is running? (from cron part) and how do i kick it off when i boot the computer? ( the init script part) thanks for your responses. shawn On Wed, Dec 10, 2008 at 12:34 PM, W W wrote: > while True: > is also a common way to make your program "run forever". Although you still > need to create some sleep/wait function or it will eat up your available > processes (so I'm told). > -HTH, > Wayne > On Wed, Dec 10, 2008 at 12:28 PM, Steve Willoughby > wrote: >> >> On Wed, Dec 10, 2008 at 12:23:48PM -0600, shawn bright wrote: >> > Hey gents, >> > >> > I have an interesting problem. I need to have a python script start >> > when a computer boots up, and i need it to run forever. >> > I also am going to run a script by cron that will check to see if the >> > process is running, if not, i need a python script to execute >> > the script. >> >> Maybe I'm misunderstanding your meaning, but usually programs which do >> this don't do anything unusual other than... well... simply run forever >> in the background (launched from the startup scripts or cron, >> disassociated >> from any TTY, of course). A watchdog re-launcher cron script is also >> not uncommon. >> >> If your script isn't actively doing something it's good to put it to >> sleep until some interesting event (or timer) wakes it up. >> >> Was there something beyond that you were looking for? >> >> -- >> Steve Willoughby | Using billion-dollar satellites >> steve at alchemy.com | to hunt for Tupperware. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor > > > > -- > 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 > From steve at alchemy.com Wed Dec 10 20:29:10 2008 From: steve at alchemy.com (Steve Willoughby) Date: Wed, 10 Dec 2008 11:29:10 -0800 Subject: [Tutor] how to run a process forever In-Reply-To: <384c93600812101115g3496fdb1x5b780ffbb65a9943@mail.gmail.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> <20081210182837.GC87165@dragon.alchemy.com> <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> <384c93600812101115g3496fdb1x5b780ffbb65a9943@mail.gmail.com> Message-ID: <20081210192910.GG87165@dragon.alchemy.com> On Wed, Dec 10, 2008 at 01:15:18PM -0600, shawn bright wrote: > Sorry, was not very specific in my request. > > say i have a script like > > while 1: > do_something_cool() > time.sleep(2) Ah, ok. First of all, my preference would be to say "while True:" there, seems more clear to me. A more complex program might have different "do this forever" logic, but an infinite loop is pretty common as well. The sleep is a very important step for making sure that the script doesn't spin forever. Better would be to sleep as long as practical, or wake up on some event (assuming you don't just need this to run something every set interval of time). > i am running this on a linux computer. How would i check that it is > running? (from cron part) Typically you have your program write its PID to a file. The cron script can check that file and see if that process is still alive before deciding to start another. Or, you could just look at the processes and look for your script by name. Or any of a few other semaphore kinds of things (a file your script touches periodically, and if it's more than n minutes old is a sign your script has locked up/died is one simple example). > and how do i kick it off when i boot the computer? ( the init script part) Unix-like systems have a script run when the system is booted. BSD systems usually put this is /etc/rc.local. Linux usually has a directory of scripts, one per application, in /etc/init.d, with symlinks from /etc/rc.d indicating which run level you want that service started. See your distro's documentation for details specific to your flavor of Linux. You'll want to run it in the background without any associated terminal. In a Bourne-type shell, this usually looks like this: /path/to/myscript /dev/null 2>&1 & -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From nephish at gmail.com Wed Dec 10 21:06:38 2008 From: nephish at gmail.com (shawn bright) Date: Wed, 10 Dec 2008 14:06:38 -0600 Subject: [Tutor] how to run a process forever In-Reply-To: <20081210192910.GG87165@dragon.alchemy.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> <20081210182837.GC87165@dragon.alchemy.com> <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> <384c93600812101115g3496fdb1x5b780ffbb65a9943@mail.gmail.com> <20081210192910.GG87165@dragon.alchemy.com> Message-ID: <384c93600812101206j8728394qf89c92c4ae1056da@mail.gmail.com> cool thanks for the help. -shawn On Wed, Dec 10, 2008 at 1:29 PM, Steve Willoughby wrote: > On Wed, Dec 10, 2008 at 01:15:18PM -0600, shawn bright wrote: >> Sorry, was not very specific in my request. >> >> say i have a script like >> >> while 1: >> do_something_cool() >> time.sleep(2) > > Ah, ok. First of all, my preference would be to say "while True:" > there, seems more clear to me. A more complex program might have > different "do this forever" logic, but an infinite loop is pretty > common as well. > > The sleep is a very important step for making sure that the script > doesn't spin forever. Better would be to sleep as long as practical, > or wake up on some event (assuming you don't just need this to run > something every set interval of time). > >> i am running this on a linux computer. How would i check that it is >> running? (from cron part) > > Typically you have your program write its PID to a file. The cron > script can check that file and see if that process is still alive before > deciding to start another. > > Or, you could just look at the processes and look for your script by > name. > > Or any of a few other semaphore kinds of things (a file your script > touches periodically, and if it's more than n minutes old is a sign > your script has locked up/died is one simple example). > >> and how do i kick it off when i boot the computer? ( the init script part) > > Unix-like systems have a script run when the system is booted. BSD > systems usually put this is /etc/rc.local. Linux usually has a > directory of scripts, one per application, in /etc/init.d, with symlinks > from /etc/rc.d indicating which run level you want that service > started. See your distro's documentation for details specific to your > flavor of Linux. > > You'll want to run it in the background without any associated terminal. > In a Bourne-type shell, this usually looks like this: > > /path/to/myscript /dev/null 2>&1 & > > -- > Steve Willoughby | Using billion-dollar satellites > steve at alchemy.com | to hunt for Tupperware. > From tiagosaboga at gmail.com Thu Dec 11 00:56:13 2008 From: tiagosaboga at gmail.com (Tiago Saboga) Date: Wed, 10 Dec 2008 21:56:13 -0200 Subject: [Tutor] how to run a process forever In-Reply-To: <20081210192910.GG87165@dragon.alchemy.com> References: <384c93600812101023o3715f5aao1940bb09c79b8255@mail.gmail.com> <20081210182837.GC87165@dragon.alchemy.com> <333efb450812101034h34afa1f3k8d9cd59d1c8199bf@mail.gmail.com> <384c93600812101115g3496fdb1x5b780ffbb65a9943@mail.gmail.com> <20081210192910.GG87165@dragon.alchemy.com> Message-ID: <20081210235613.GB1388@localdomain> On Wed, Dec 10, 2008 at 11:29:10AM -0800, Steve Willoughby wrote: > On Wed, Dec 10, 2008 at 01:15:18PM -0600, shawn bright wrote: > > i am running this on a linux computer. How would i check that it is > > running? (from cron part) > > Typically you have your program write its PID to a file. The cron > script can check that file and see if that process is still alive before > deciding to start another. If you are in a debian-based system, see start-stop-daemon (possibly available in other distros too). Tiago Saboga. From kent37 at tds.net Thu Dec 11 04:18:37 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 10 Dec 2008 22:18:37 -0500 Subject: [Tutor] sending html and text emails In-Reply-To: References: Message-ID: <1c2a2c590812101918i690bd168je0f8b0edde24e5d6@mail.gmail.com> On Wed, Dec 10, 2008 at 10:18 AM, James wrote: > All, > > I'm trying to figure out a way to send html email using Python. I've > found scripts such as the one in the link below: > > http://www.stevecoursen.com/2008/10/sending-html-email-from-python.html/ > > These don't seem to work well. I took a (very) basic html page and had > the script in the page above read that page and then set up the text / > html email. When my client received the message it was filled with > visible html tags. > > The ultimate goal is to have a script that displays a simple HTML > table that's easily readable by anyone using a client capable of > interpreting html emails. It might help to see your code, if it is different at all from what is on that page. What was printed? Here is a redacted version of a function that worked for me. This sends HTML only - no text part: from email.mime.text import MIMEText from email.Utils import formatdate def emailReport(): report = ... # Get HTML text from somewhere msg = MIMEText(report, 'html', 'utf-8') msg['Subject'] = 'report' msg['From'] = 'me at example.com' msg['To'] = 'you at example.com' msg['Date'] = formatdate() server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) server.sendmail('me at example.com', 'you at example.com', msg.as_string()) try: server.quit() except: return Kent From vadud3 at gmail.com Thu Dec 11 17:49:16 2008 From: vadud3 at gmail.com (Asif Iqbal) Date: Thu, 11 Dec 2008 11:49:16 -0500 Subject: [Tutor] MIME parser Message-ID: I am looking for a way to feed a message from stdin to a python based mime parser, detach all attachments and drop them to a dir, and then send the email to the recipient. The attachment(s) will be replaced by an URL to a link. So basically decouple the attachments (>1MB in size) and modify the body by inserting an URL in the footer. Has anyone done anything like this? -- Asif Iqbal PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu From ravikondamuru at gmail.com Thu Dec 11 19:04:14 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Thu, 11 Dec 2008 10:04:14 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> Message-ID: <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> Hi, I am writing a script to read list output from a C executable. How should c program be written so that python can read the output as a list? Any pointers to info on this appreciated. thanks, Ravi. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Thu Dec 11 19:19:01 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 11 Dec 2008 13:19:01 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> Message-ID: <49415995.4040409@gmail.com> Ravi Kondamuru wrote: > Hi, > I am writing a script to read list output from a C executable. How > should c program be written so that python can read the output as a list? > Any pointers to info on this appreciated. Funny that a C programmer is asking for pointers, when C has lots of pointers and Python has none. Python I/O reads strings. So you must encode the list in some manner into a string and decode it into a list in the Python program. Take a look as json. http://www.json.org/ There are links to C and Python. You should be able there to find how to encode in C and decode in Python. You could "roll your own" encoding" but I think json would be easier. Remember to reply-all so a copy goes to the list. -- Bob Gailer Chapel Hill NC 919-636-4239 From steve at alchemy.com Thu Dec 11 19:27:22 2008 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Dec 2008 10:27:22 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> Message-ID: <49415B8A.3080701@alchemy.com> Ravi Kondamuru wrote: > Hi, > I am writing a script to read list output from a C executable. How > should c program be written so that python can read the output as a list? > Any pointers to info on this appreciated. The possibilities are truly wide open on this. Python can read a variety of standard formats (and of course can have custom code to read anything. Depending on what the data involved actually are, you need to decide what format works best. A simple approach is to have the C program write simple CSV output, and use Python's csv module to read it. A more complex solution might be to use XML. There are approximately 52,495,102 other possibilities available too, so you have lots of room to work out what's best for your application. More specific information on what you're trying to accomplish would help narrow it down as well. From ravikondamuru at gmail.com Thu Dec 11 19:29:10 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Thu, 11 Dec 2008 10:29:10 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <49415995.4040409@gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415995.4040409@gmail.com> Message-ID: <36601b010812111029i32957114ve358cd2dc512afa9@mail.gmail.com> I am expecting these lists to be huge and was hoping to avoid re-parsing in python. Any way for the c program to return a list that python can directly use. Thanks for the pointer to json :) I am going to explore and evaluate re-parsing overhead. thanks, Ravi. On Thu, Dec 11, 2008 at 10:19 AM, bob gailer wrote: > Ravi Kondamuru wrote: > >> Hi, >> I am writing a script to read list output from a C executable. How should >> c program be written so that python can read the output as a list? >> Any pointers to info on this appreciated. >> > > Funny that a C programmer is asking for pointers, when C has lots of > pointers and Python has none. > > Python I/O reads strings. So you must encode the list in some manner into a > string and decode it into a list in the Python program. > > Take a look as json. http://www.json.org/ > > There are links to C and Python. You should be able there to findThats how > to encode in C and decode in Python. > > You could "roll your own" encoding" but I think json would be easier. > > Remember to reply-all so a copy goes to the list. > > -- > Bob Gailer > Chapel Hill NC 919-636-4239 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ravikondamuru at gmail.com Thu Dec 11 19:35:14 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Thu, 11 Dec 2008 10:35:14 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <49415B8A.3080701@alchemy.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> Message-ID: <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> I am trying to read a binary log file to extract system counters. These counters will then be used to generate web-based graphs using the chart-director api in python. For extracting the counters I planning to write a program in C to read and give the output as lists for use by chart-director. If possible i would like to do the parsing of data only once in C on the log file and pass the processed output for direct use by python. I have thought about having to populate a database first but will prefer to avoid having another intermediate datastore (apart from the log file). thanks, Ravi. On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby wrote: > Ravi Kondamuru wrote: > >> Hi, >> I am writing a script to read list output from a C executable. How should >> c program be written so that python can read the output as a list? >> Any pointers to info on this appreciated. >> > > The possibilities are truly wide open on this. Python can read a variety > of standard formats (and of course can have custom code to read anything. > Depending on what the data involved actually are, you need to decide what > format works best. > > A simple approach is to have the C program write simple CSV output, and use > Python's csv module to read it. A more complex solution might be to use > XML. There are approximately 52,495,102 other possibilities available too, > so you have lots of room to work out what's best for your application. > More specific information on what you're trying to accomplish would help > narrow it down as well. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Thu Dec 11 19:39:40 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 11 Dec 2008 13:39:40 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111029i32957114ve358cd2dc512afa9@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415995.4040409@gmail.com> <36601b010812111029i32957114ve358cd2dc512afa9@mail.gmail.com> Message-ID: <49415E6C.7080502@gmail.com> Ravi Kondamuru wrote: > I am expecting these lists to be huge and was hoping to avoid > re-parsing in python. Any way for the c program to return a list that > python can directly use. Not as far as I know. Lists in Python are internal objects. If you were able to package the C and Python programs together (C extension, SWIG, ...???) then you could write the Python object directly from the C program. What will the Python program do with the lists? And what does huge mean. Does it have to be a list? Is performance an issue or storage space or what? -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Thu Dec 11 19:41:25 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 11 Dec 2008 13:41:25 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> Message-ID: <49415ED5.3030902@gmail.com> Ravi Kondamuru wrote: > I am trying to read a binary log file to extract system counters. > These counters will then be used to generate web-based graphs using > the chart-director api in python. For extracting the counters I > planning to write a program in C to read and give the output as lists > for use by chart-director. If possible i would like to do the parsing > of data only once in C on the log file and pass the processed output > for direct use by python. > > I have thought about having to populate a database first but will > prefer to avoid having another intermediate datastore (apart from the > log file). As you might guess I was typing my response before reading the above. So you answered my question. NOW: why not read and parse the log file in Python? -- Bob Gailer Chapel Hill NC 919-636-4239 From steve at alchemy.com Thu Dec 11 19:44:52 2008 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Dec 2008 10:44:52 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111029i32957114ve358cd2dc512afa9@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415995.4040409@gmail.com> <36601b010812111029i32957114ve358cd2dc512afa9@mail.gmail.com> Message-ID: <49415FA4.4020807@alchemy.com> Ravi Kondamuru wrote: > I am expecting these lists to be huge and was hoping to avoid re-parsing > in python. Any way for the c program to return a list that python can Mind if I ask the obvious question here? Why are you wanting to avoid parsing in Python? Any time you have one program (in any language) leave data behind for another program to consume, you're going to have some level of interpretation on the data, whether you're reading raw bytes and assuming you know how to put them together again or parsing ASCII characters or whatever. Try something simple first and see if you get reasonable performance from it before you get too paranoid about avoiding something that might not be where you need to focus your attention. Optimizing too early in the development effort often leads to wasted time on the things that surprisingly don't contribute to the real performance issues in a program. If you're output is binary data (like a stream of bytes or words), Python can read that with a small bit of code to put those bytes into integer values in a list. I'd suggest something like CSV as a first try, though, since that way the data will be intelligible for debugging purposes, leaves the possibility open for other scripts to read and alter the data too, and is very simple for Python to read via standard library modules too. > directly use. > Thanks for the pointer to json :) I am going to explore and evaluate > re-parsing overhead. > thanks, > Ravi. > > On Thu, Dec 11, 2008 at 10:19 AM, bob gailer > wrote: > > Ravi Kondamuru wrote: > > Hi, > I am writing a script to read list output from a C executable. > How should c program be written so that python can read the > output as a list? > Any pointers to info on this appreciated. > > > Funny that a C programmer is asking for pointers, when C has lots of > pointers and Python has none. > > Python I/O reads strings. So you must encode the list in some manner > into a string and decode it into a list in the Python program. > > Take a look as json. http://www.json.org/ > > There are links to C and Python. You should be able there to > findThats how to encode in C and decode in Python. > > You could "roll your own" encoding" but I think json would be easier. > > Remember to reply-all so a copy goes to the list. > > -- > Bob Gailer > Chapel Hill NC 919-636-4239 > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From steve at alchemy.com Thu Dec 11 19:45:51 2008 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Dec 2008 10:45:51 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> Message-ID: <49415FDF.3080601@alchemy.com> Ravi Kondamuru wrote: > I am trying to read a binary log file to extract system counters. These > counters will then be used to generate web-based graphs using the > chart-director api in python. For extracting the counters I planning to > write a program in C to read and give the output as lists for use by > chart-director. If possible i would like to do the parsing of data only > once in C on the log file and pass the processed output for direct use > by python. Wait.. Why not have Python read the binary log file itself, parse it and make the API calls right there? Why do you even need the C program? > > I have thought about having to populate a database first but will prefer > to avoid having another intermediate datastore (apart from the log file). > > thanks, > Ravi. > > On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby > wrote: > > Ravi Kondamuru wrote: > > Hi, > I am writing a script to read list output from a C executable. > How should c program be written so that python can read the > output as a list? > Any pointers to info on this appreciated. > > > The possibilities are truly wide open on this. Python can read a > variety of standard formats (and of course can have custom code to > read anything. Depending on what the data involved actually are, > you need to decide what format works best. > > A simple approach is to have the C program write simple CSV output, > and use Python's csv module to read it. A more complex solution > might be to use XML. There are approximately 52,495,102 other > possibilities available too, so you have lots of room to work out > what's best for your application. More specific information on > what you're trying to accomplish would help narrow it down as well. > > From ravikondamuru at gmail.com Thu Dec 11 19:59:14 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Thu, 11 Dec 2008 10:59:14 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <49415FDF.3080601@alchemy.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> Message-ID: <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> Reasons for C: 1. The log files I am working are 60-100MB files. I *assumed* using C will reduce the parse time. 2. The records themselves are variable length and hence was concerned about the complexity for implementation in python. 3. Since I am not using a database, each request to refresh the graph (changing time duration for display) will have to re-parse the file all over again. And hence speed in parsing the log is important. Hope that explains thanks, Ravi. On Thu, Dec 11, 2008 at 10:45 AM, Steve Willoughby wrote: > Ravi Kondamuru wrote: > >> I am trying to read a binary log file to extract system counters. These >> counters will then be used to generate web-based graphs using the >> chart-director api in python. For extracting the counters I planning to >> write a program in C to read and give the output as lists for use by >> chart-director. If possible i would like to do the parsing of data only once >> in C on the log file and pass the processed output for direct use by python. >> >> > > Wait.. > Why not have Python read the binary log file itself, parse it and make the > API calls right there? Why do you even need the C program? > > > >> I have thought about having to populate a database first but will prefer >> to avoid having another intermediate datastore (apart from the log file). >> >> thanks, >> Ravi. >> >> On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby > steve at alchemy.com>> wrote: >> >> Ravi Kondamuru wrote: >> >> Hi, >> I am writing a script to read list output from a C executable. >> How should c program be written so that python can read the >> output as a list? >> Any pointers to info on this appreciated. >> >> >> The possibilities are truly wide open on this. Python can read a >> variety of standard formats (and of course can have custom code to >> read anything. Depending on what the data involved actually are, >> you need to decide what format works best. >> >> A simple approach is to have the C program write simple CSV output, >> and use Python's csv module to read it. A more complex solution >> might be to use XML. There are approximately 52,495,102 other >> possibilities available too, so you have lots of room to work out >> what's best for your application. More specific information on >> what you're trying to accomplish would help narrow it down as well. >> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Thu Dec 11 20:16:46 2008 From: denis.spir at free.fr (spir) Date: Thu, 11 Dec 2008 20:16:46 +0100 Subject: [Tutor] [Fwd: Re: reading output from a c executable.] Message-ID: <4941671E.1040400@free.fr> Ravi Kondamuru a ?crit : > I am trying to read a binary log file to extract system counters. These > counters will then be used to generate web-based graphs using the > chart-director api in python. For extracting the counters I planning to > write a program in C to read and give the output as lists for use by > chart-director. If possible i would like to do the parsing of data onlyonce > in C on the log file and pass the processed output for direct use by > python. > I have thought about having to populate a database first but will prefer to > avoid having another intermediate datastore (apart from the log file). > > thanks, > Ravi. > > On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby wrote: > >> Ravi Kondamuru wrote: >> >>> Hi, >>> I am writing a script to read list output from a C executable. How should >>> c program be written so that python can read the output as a list? >>> Any pointers to info on this appreciated. >>> >> The possibilities are truly wide open on this. Python can read a variety >> of standard formats (and of course can have custom code to read anything. >> Depending on what the data involved actually are, you need to decide what >> format works best. >> >> A simple approach is to have the C program write simple CSV output, and use >> Python's csv module to read it. A more complex solution might be to use >> XML. There are approximately 52,495,102 other possibilities availabletoo, >> so you have lots of room to work out what's best for your application. >> More specific information on what you're trying to accomplish would help >> narrow it down as well. What type are these system counters? If you want to avoid reparsing in python, why not let C write a valid python literal, then simply import? (depends on what "huge" means for you). Assuming they are ints: ========= counters.py: counters = [ 123, 456, 789, ... ] ======= prog.py: from counters import counters denis From zstumgoren at gmail.com Thu Dec 11 20:31:39 2008 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Thu, 11 Dec 2008 14:31:39 -0500 Subject: [Tutor] advice on regex matching for dates? Message-ID: Hey everyone, I was wondering if there is a way to use the datetime module to check for variations on a month name when performing a regex match? In the script below, I created a regex pattern that checks for dates in the following pattern: "August 31, 2007". If there's a match, I can then print the capture date and the line from which it was extracted. While it works in this isolated case, it struck me as not very flexible. What happens when I inevitably get data that has dates formatted in a different way? Do I have to create some type of library that contains variations on each month name (i.e. - January, Jan., 01, 1...) and use that to parse each line? Or is there some way to use datetime to check for date patterns when using regex? Is there a "best practice" in this area that I'm unaware of in this area? Apologies if this question has been answered elsewhere. I wasn't sure how to research this topic (beyond standard datetime docs), but I'd be happy to RTM if someone can point me to some resources. Any suggestions are welcome (including optimizations of the code below). Regards, Serdar #!/usr/bin/env python import re, sys sourcefile = open(sys.argv[1],'r') pattern = re.compile(r'(?PJanuary|February|March|April|May|June|July|August|September|October|November|December)\s(?P\d{1,2}),\s(?P\d{4})') pattern2 = re.compile(r'Return to List') counter = 0 for line in sourcefile: x = pattern.search(line) break_point = pattern2.match(line) if x: counter +=1 print "%s %d, %d <== %s" % ( x.group('month'), int(x.group('day')), int(x.group('year')), line ), elif break_point: break print counter sourcefile.close() -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Thu Dec 11 20:34:45 2008 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Dec 2008 11:34:45 -0800 Subject: [Tutor] [Fwd: Re: reading output from a c executable.] In-Reply-To: <4941671E.1040400@free.fr> References: <4941671E.1040400@free.fr> Message-ID: <49416B55.6080306@alchemy.com> With the amount of information provided so far, I'd say you need to step back and question your initial assumptions. Python shouldn't have a great deal of trouble reading variable-length binary data blocks, and the overhead of doing that is probably a lot less than it would be to have another program read that, output some intermediate format, and then have the Python script parse that (and whether you're writing code yourself, or blindly eval()ing source created by the C program, or reading some other format, it's *still* having to do that). My recommendation: have the Python script read the binary data directly and act upon it, and avoid all the overhead and extra steps you're trying to design into your solution here. If that doesn't run fast enough, consider coding a small file-reading module in C, *as a C-implemented Python module*, and import that into your program. But try it in plain Python first and see what it's like. From kent37 at tds.net Thu Dec 11 21:11:50 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 11 Dec 2008 15:11:50 -0500 Subject: [Tutor] advice on regex matching for dates? In-Reply-To: References: Message-ID: <1c2a2c590812111211r2ea8dee3r1b3d5dab4699448d@mail.gmail.com> On Thu, Dec 11, 2008 at 2:31 PM, Serdar Tumgoren wrote: > Hey everyone, > > I was wondering if there is a way to use the datetime module to check for > variations on a month name when performing a regex match? Parsing arbitrary dates is hard, in fact the most general case is not solvable since 7/8/07 could refer to July 8 or August 7 and the century must be guessed. The only approach I know of is to try the formats you expect until you find one that works. Mark Pilgrim's feedparser has a pretty robust date parser you might look at; look at the _parse_date() function in this module: http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py Kent From vadud3 at gmail.com Thu Dec 11 18:25:26 2008 From: vadud3 at gmail.com (Asif Iqbal) Date: Thu, 11 Dec 2008 17:25:26 +0000 Subject: [Tutor] MIME parser In-Reply-To: References: Message-ID: <20081211172525.GC21103@qwestip.net> On Thu, Dec 11, 2008 at 11:49:16AM, Asif Iqbal wrote: > I am looking for a way to feed a message from stdin to a python based > mime parser, detach all attachments and drop them to a dir, and then > send the email to the recipient. The attachment(s) will be replaced > by an URL to a link. > > So basically decouple the attachments (>1MB in size) and modify the > body by inserting an URL in the footer. > > Has anyone done anything like this? > I am looking at email pkg. Now I need to find a way to feed the msg from stdin and then detach all attachments and update the body with urls pointing to attachments > -- Asif Iqbal Google Chat: vadud3 at gmail.com PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? From ravikondamuru at gmail.com Thu Dec 11 22:36:41 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Thu, 11 Dec 2008 13:36:41 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> Message-ID: <36601b010812111336k42667f9bq4f8da9582cac4c43@mail.gmail.com> This was a great info exchange for me. I am going to try out the all-python solution and see how it works out. thank you all, Ravi. On Thu, Dec 11, 2008 at 10:59 AM, Ravi Kondamuru wrote: > Reasons for C: > 1. The log files I am working are 60-100MB files. I *assumed* using C will > reduce the parse time. > 2. The records themselves are variable length and hence was concerned about > the complexity for implementation in python. > 3. Since I am not using a database, each request to refresh the graph > (changing time duration for display) will have to re-parse the file all over > again. And hence speed in parsing the log is important. > > Hope that explains > thanks, > Ravi. > > > On Thu, Dec 11, 2008 at 10:45 AM, Steve Willoughby wrote: > >> Ravi Kondamuru wrote: >> >>> I am trying to read a binary log file to extract system counters. These >>> counters will then be used to generate web-based graphs using the >>> chart-director api in python. For extracting the counters I planning to >>> write a program in C to read and give the output as lists for use by >>> chart-director. If possible i would like to do the parsing of data only once >>> in C on the log file and pass the processed output for direct use by python. >>> >>> >> >> Wait.. >> Why not have Python read the binary log file itself, parse it and make the >> API calls right there? Why do you even need the C program? >> >> >> >>> I have thought about having to populate a database first but will prefer >>> to avoid having another intermediate datastore (apart from the log file). >>> >>> thanks, >>> Ravi. >>> >>> On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby >> steve at alchemy.com>> wrote: >>> >>> Ravi Kondamuru wrote: >>> >>> Hi, >>> I am writing a script to read list output from a C executable. >>> How should c program be written so that python can read the >>> output as a list? >>> Any pointers to info on this appreciated. >>> >>> >>> The possibilities are truly wide open on this. Python can read a >>> variety of standard formats (and of course can have custom code to >>> read anything. Depending on what the data involved actually are, >>> you need to decide what format works best. >>> >>> A simple approach is to have the C program write simple CSV output, >>> and use Python's csv module to read it. A more complex solution >>> might be to use XML. There are approximately 52,495,102 other >>> possibilities available too, so you have lots of room to work out >>> what's best for your application. More specific information on >>> what you're trying to accomplish would help narrow it down as well. >>> >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bryan.fodness at gmail.com Thu Dec 11 22:34:30 2008 From: bryan.fodness at gmail.com (Bryan Fodness) Date: Thu, 11 Dec 2008 16:34:30 -0500 Subject: [Tutor] no attribute Message-ID: I am trying to change values in a file. The following code does not seem to find the attribute. def anonymize(obj, attr): try: obj.attr = 'Anonymize' except AttributeError: pass for fname in os.listdir(os.curdir): plan=ReadFile(fname) anonymize(plan, 'Name') It seems to be the obj.attr in the function. Can someone explain what I am doing wrong? Bryan -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 11 22:56:19 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Dec 2008 21:56:19 -0000 Subject: [Tutor] no attribute References: Message-ID: "Bryan Fodness" wrote >I am trying to change values in a file. The following code does not >seem to > find the attribute. > > def anonymize(obj, attr): > try: > obj.attr = 'Anonymize' > except AttributeError: > pass This code is looking for an attribute of obj called attr It is not using the name of attr stored in the attr parameter. To do that you will need to use the setattr function. HTH -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From denis.spir at free.fr Thu Dec 11 23:38:52 2008 From: denis.spir at free.fr (spir) Date: Thu, 11 Dec 2008 23:38:52 +0100 Subject: [Tutor] advice on regex matching for dates? In-Reply-To: References: Message-ID: <4941967C.2010504@free.fr> Serdar Tumgoren a ?crit : > Hey everyone, > > I was wondering if there is a way to use the datetime module to check for > variations on a month name when performing a regex match? > > In the script below, I created a regex pattern that checks for dates in the > following pattern: "August 31, 2007". If there's a match, I can then print > the capture date and the line from which it was extracted. > > While it works in this isolated case, it struck me as not very flexible. > What happens when I inevitably get data that has dates formatted in a > different way? Do I have to create some type of library that contains > variations on each month name (i.e. - January, Jan., 01, 1...) and use that > to parse each line? > > Or is there some way to use datetime to check for date patterns when using > regex? Is there a "best practice" in this area that I'm unaware of in this > area? > > Apologies if this question has been answered elsewhere. I wasn't sure how to > research this topic (beyond standard datetime docs), but I'd be happy to RTM > if someone can point me to some resources. > > Any suggestions are welcome (including optimizations of the code below). > > Regards, > Serdar > > #!/usr/bin/env python > > import re, sys > > sourcefile = open(sys.argv[1],'r') > > pattern = > re.compile(r'(?PJanuary|February|March|April|May|June|July|August|September|October|November|December)\s(?P\d{1,2}),\s(?P\d{4})') > > pattern2 = re.compile(r'Return to List') > > counter = 0 > > for line in sourcefile: > x = pattern.search(line) > break_point = pattern2.match(line) > > if x: > counter +=1 > print "%s %d, %d <== %s" % ( x.group('month'), int(x.group('day')), > int(x.group('year')), line ), > elif break_point: > break > > print counter > sourcefile.close() I just found a simple, but nice, trick to make regexes less unlegible. Using substrings to represent sub-patterns. E.g. instead of: p = re.compile(r'(?PJanuary|February|March|April|May|June|July|August|September|October|November|December)\s(?P\d{1,2}),\s(?P\d{4})') write first: month = r'(?PJanuary|February|March|April|May|June|July|August|September|October|November|December)' day = r'(?P\d{1,2})' year = r'(?P\d{4})' then: p = re.compile( r"%s\s%s,\s%s" % (month,day,year) ) or even: p = re.compile( r"%(month)s\s%(day)s,\s%(year)s" % {'month':month,'day':day,'year':year} ) denis From john at fouhy.net Thu Dec 11 23:45:56 2008 From: john at fouhy.net (John Fouhy) Date: Fri, 12 Dec 2008 11:45:56 +1300 Subject: [Tutor] advice on regex matching for dates? In-Reply-To: <4941967C.2010504@free.fr> References: <4941967C.2010504@free.fr> Message-ID: <5e58f2e40812111445v5b1c2f7ehe98cccf0c207c270@mail.gmail.com> On 12/12/2008, spir wrote: > I just found a simple, but nice, trick to make regexes less unlegible. > Using substrings to represent sub-patterns. E.g. instead of: [...] Another option is to use the re.VERBOSE flag. This allows you to put comments in your regular expression and use whitespace for formatting. See this article for an example: http://diveintopython.org/regular_expressions/verbose.html HTH! -- John. From eike.welk at gmx.net Fri Dec 12 00:11:49 2008 From: eike.welk at gmx.net (Eike Welk) Date: Fri, 12 Dec 2008 00:11:49 +0100 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111336k42667f9bq4f8da9582cac4c43@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <36601b010812111336k42667f9bq4f8da9582cac4c43@mail.gmail.com> Message-ID: <200812120011.49764.eike.welk@gmx.net> On Thursday 11 December 2008, Ravi Kondamuru wrote: > This was a great info exchange for me. > I am going to try out the all-python solution and see how it works > out. thank you all, You could try to optimize slow parts with the compiled Cython language. Nearly all Python code is legal Cython; but if you declare variables as C datatypes operations with these variables will be fast like in C. http://www.cython.org/ Introduction for C programmers: http://docs.cython.org/docs/language_basics.html The language is geared a bit towards numerical computations. However Python strings are automatically converted to 'char *', so it is useful for you. Cython is a fork of Pyrex, which can keep up fairly well with real C programs: http://www.scipy.org/PerformancePython#head-a73fa06d3c4f3bda71b3526d30d51c492d8f80df Kind regards, Eike. From lie.1296 at gmail.com Fri Dec 12 01:08:43 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 12 Dec 2008 00:08:43 +0000 (UTC) Subject: [Tutor] no attribute References: Message-ID: On Thu, 11 Dec 2008 21:56:19 +0000, Alan Gauld wrote: > "Bryan Fodness" wrote > >>I am trying to change values in a file. The following code does not >>seem to >> find the attribute. >> >> def anonymize(obj, attr): >> try: >> obj.attr = 'Anonymize' >> except AttributeError: >> pass > > This code is looking for an attribute of obj called attr It is not using > the name of attr stored in the attr parameter. > > To do that you will need to use the setattr function. > > HTH I don't think you'll even need setattr (for the piece of code given), because anonymize receives a fixed string. If that code is part of more complex code where the string might be anything setattr might be needed though. What are you trying to do though? From zstumgoren at gmail.com Fri Dec 12 01:23:34 2008 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Thu, 11 Dec 2008 19:23:34 -0500 Subject: [Tutor] advice on regex matching for dates? In-Reply-To: <5e58f2e40812111445v5b1c2f7ehe98cccf0c207c270@mail.gmail.com> References: <4941967C.2010504@free.fr> <5e58f2e40812111445v5b1c2f7ehe98cccf0c207c270@mail.gmail.com> Message-ID: Thanks to all for the replies! The FeedParser example is mind-bending (at least for this noob), but of course looks very powerful. And thanks for the tips on cleaning up the appearance of the code with sup-patterns. Those regex's can get hairy pretty fast. -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Thu Dec 11 19:58:31 2008 From: denis.spir at free.fr (spir) Date: Thu, 11 Dec 2008 19:58:31 +0100 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> Message-ID: <494162D7.7080202@free.fr> Ravi Kondamuru a ?crit : > I am trying to read a binary log file to extract system counters. These > counters will then be used to generate web-based graphs using the > chart-director api in python. For extracting the counters I planning to > write a program in C to read and give the output as lists for use by > chart-director. If possible i would like to do the parsing of data only once > in C on the log file and pass the processed output for direct use by > python. > I have thought about having to populate a database first but will prefer to > avoid having another intermediate datastore (apart from the log file). > > thanks, > Ravi. > > On Thu, Dec 11, 2008 at 10:27 AM, Steve Willoughby wrote: > >> Ravi Kondamuru wrote: >> >>> Hi, >>> I am writing a script to read list output from a C executable. How should >>> c program be written so that python can read the output as a list? >>> Any pointers to info on this appreciated. >>> >> The possibilities are truly wide open on this. Python can read a variety >> of standard formats (and of course can have custom code to read anything. >> Depending on what the data involved actually are, you need to decide what >> format works best. >> >> A simple approach is to have the C program write simple CSV output, and use >> Python's csv module to read it. A more complex solution might be to use >> XML. There are approximately 52,495,102 other possibilities available too, >> so you have lots of room to work out what's best for your application. >> More specific information on what you're trying to accomplish would help >> narrow it down as well. What type are these system counters? If you want to avoid reparsing in python, why not let C write a valid python literal, then simply import? (depends on what "huge" means for you). Assuming they are ints: ========= counters.py: counters = [ 123, 456, 789, ... ] ======= prog.py: from counters import counters denis From bgailer at gmail.com Fri Dec 12 04:54:22 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 11 Dec 2008 22:54:22 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> Message-ID: <4941E06E.1090608@gmail.com> Ravi Kondamuru wrote: > Reasons for C: > 1. The log files I am working are 60-100MB files. I *assumed* using C > will reduce the parse time. > 2. The records themselves are variable length and hence was concerned > about the complexity for implementation in python. > 3. Since I am not using a database, each request to refresh the graph > (changing time duration for display) will have to re-parse the file > all over again. And hence speed in parsing the log is important. There should never be a need to reparse the same file. Please present the case more specifically. -- Bob Gailer Chapel Hill NC 919-636-4239 From Gang.Chen at nokia.com Fri Dec 12 04:44:37 2008 From: Gang.Chen at nokia.com (Chen Gang (Nokia-D/Beijing)) Date: Fri, 12 Dec 2008 11:44:37 +0800 Subject: [Tutor] how to convert '\xf0' to 0xf0? Message-ID: <1229053477.20204.0.camel@localhost.localdomain> From steve at alchemy.com Fri Dec 12 05:50:29 2008 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 11 Dec 2008 20:50:29 -0800 Subject: [Tutor] how to convert '\xf0' to 0xf0? In-Reply-To: <1229053477.20204.0.camel@localhost.localdomain> References: <1229053477.20204.0.camel@localhost.localdomain> Message-ID: <4941ED95.1020206@alchemy.com> ord('\xf0') From alan.gauld at btinternet.com Fri Dec 12 10:01:22 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Dec 2008 09:01:22 -0000 Subject: [Tutor] how to convert '\xf0' to 0xf0? References: <1229053477.20204.0.camel@localhost.localdomain> Message-ID: What exactly do you want to achieve? I suspect its not what you asked! To convert the character whose hex value of F0 to an integer value of F0 you can use ord() (or just reinterpret the character as a string using the struct module). >>> ord('\xf0') 240 To display the hex value of a character as a hex string you can use string formatting or the hex() function: >>> c = '\xF0' >>> "0x%x" % ord(c) '0xf0' >>> "0x%X" % ord(c) '0xF0' >>> hex(ord(c)) '0xf0' Remember that the representation of a number is always a string. The value of a number is always binary and can be represented in any base with the default in Python being decimal. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From ravikondamuru at gmail.com Fri Dec 12 12:13:16 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Fri, 12 Dec 2008 03:13:16 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <4941E06E.1090608@gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> Message-ID: <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> Denis, These are 32bit, 64bit counters (essentially numbers). Bob, There are well over 10K counters in the log file that are updated every 5 secs. If a counter1's graph was requested, log will have to be parsed once to get the data points. If a user asked for a counter2, now it needs to be retrieved from the log file. Which essentially means having to go through the logfile again. This is because i want to try to avoid using a database to store values after parsing the file. thanks, Ravi. On Thu, Dec 11, 2008 at 7:54 PM, bob gailer wrote: > Ravi Kondamuru wrote: > >> Reasons for C: >> 1. The log files I am working are 60-100MB files. I *assumed* using C will >> reduce the parse time. >> 2. The records themselves are variable length and hence was concerned >> about the complexity for implementation in python. >> 3. Since I am not using a database, each request to refresh the graph >> (changing time duration for display) will have to re-parse the file all over >> again. And hence speed in parsing the log is important. >> > > There should never be a need to reparse the same file. Please present the > case more specifically. > > > -- > Bob Gailer > Chapel Hill NC 919-636-4239 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Fri Dec 12 12:33:55 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 12 Dec 2008 11:33:55 +0000 (UTC) Subject: [Tutor] reading output from a c executable. References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> Message-ID: On Fri, 12 Dec 2008 03:13:16 -0800, Ravi Kondamuru wrote: > Denis, These are 32bit, 64bit counters (essentially numbers). Bob, There > are well over 10K counters in the log file that are updated every 5 > secs. If a counter1's graph was requested, log will have to be parsed > once to get the data points. If a user asked for a counter2, now it > needs to be retrieved from the log file. Which essentially means having > to go through the logfile again. This is because i want to try to avoid > using a database to store values after parsing the file. thanks, > Ravi. Instead of using a file, why not use pipes? Using pipes is surely much faster than reading from a file, and using pipes allow you to send log entries in "messages" which means you don't need to reparse the whole logfile on every update, just the parts that have changed. From lie.1296 at gmail.com Fri Dec 12 12:43:46 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 12 Dec 2008 11:43:46 +0000 (UTC) Subject: [Tutor] advice on regex matching for dates? References: <4941967C.2010504@free.fr> Message-ID: On Thu, 11 Dec 2008 23:38:52 +0100, spir wrote: > Serdar Tumgoren a ?crit : >> Hey everyone, >> >> I was wondering if there is a way to use the datetime module to check >> for variations on a month name when performing a regex match? >> >> In the script below, I created a regex pattern that checks for dates in >> the following pattern: "August 31, 2007". If there's a match, I can >> then print the capture date and the line from which it was extracted. >> >> While it works in this isolated case, it struck me as not very >> flexible. What happens when I inevitably get data that has dates >> formatted in a different way? Do I have to create some type of library >> that contains variations on each month name (i.e. - January, Jan., 01, >> 1...) and use that to parse each line? >> >> Or is there some way to use datetime to check for date patterns when >> using regex? Is there a "best practice" in this area that I'm unaware >> of in this area? >> >> Apologies if this question has been answered elsewhere. I wasn't sure >> how to research this topic (beyond standard datetime docs), but I'd be >> happy to RTM if someone can point me to some resources. >> >> Any suggestions are welcome (including optimizations of the code >> below). >> >> Regards, >> Serdar >> >> #!/usr/bin/env python >> >> import re, sys >> >> sourcefile = open(sys.argv[1],'r') >> >> pattern = >> re.compile(r'(?PJanuary|February|March|April|May|June|July| August|September|October|November|December)\s(?P\d{1,2}),\s(?P \d{4})') >> >> pattern2 = re.compile(r'Return to List') >> >> counter = 0 >> >> for line in sourcefile: >> x = pattern.search(line) >> break_point = pattern2.match(line) >> >> if x: >> counter +=1 >> print "%s %d, %d <== %s" % ( x.group('month'), >> int(x.group('day')), >> int(x.group('year')), line ), >> elif break_point: >> break >> >> print counter >> sourcefile.close() > > I just found a simple, but nice, trick to make regexes less unlegible. > Using substrings to represent sub-patterns. E.g. instead of: > > p = > re.compile(r'(?PJanuary|February|March|April|May|June|July| August|September|October|November|December)\s(?P\d{1,2}),\s(?P \d{4})') > > write first: > > month = > r'(?PJanuary|February|March|April|May|June|July|August|September| October|November|December)' > day = r'(?P\d{1,2})' > year = r'(?P\d{4})' > > then: > p = re.compile( r"%s\s%s,\s%s" % (month,day,year) ) or even: > p = re.compile( r"%(month)s\s%(day)s,\s%(year)s" % > {'month':month,'day':day,'year':year} ) > You might realize that that wouldn't help much and might even hide bugs (or make it harder to see). regex IS hairy in the first place. I'd vote for namespace in regex to allow a pattern inside a special pair tags to be completely ignorant of anything outside it. And also I'd vote for a compiled-pattern to be used as a subpattern. But then, with all those limbs, I might as well use a full-blown syntax parser instead. From bgailer at gmail.com Fri Dec 12 15:12:42 2008 From: bgailer at gmail.com (bob gailer) Date: Fri, 12 Dec 2008 09:12:42 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> Message-ID: <4942715A.2010202@gmail.com> Ravi Kondamuru wrote: > Denis, These are 32bit, 64bit counters (essentially numbers). > Bob, There are well over 10K counters in the log file that are updated > every 5 secs. If a counter1's graph was requested, log will have to be > parsed once to get the data points. If a user asked for a counter2, > now it needs to be retrieved from the log file. Which essentially > means having to go through the logfile again. This is because i want > to try to avoid using a database to store values after parsing the file. Thank you. When you said "same file" I did not realize that it meant "same name modified data"! If the file is a mix of 32 and 64 bit numbers, how do you know the length of each counter? -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Fri Dec 12 15:30:11 2008 From: bgailer at gmail.com (bob gailer) Date: Fri, 12 Dec 2008 09:30:11 -0500 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> Message-ID: <49427573.5020902@gmail.com> Ravi Kondamuru wrote: > Denis, These are 32bit, 64bit counters (essentially numbers). > Bob, There are well over 10K counters in the log file that are updated > every 5 secs. If a counter1's graph was requested, log will have to be > parsed once to get the data points. If a user asked for a counter2, > now it needs to be retrieved from the log file. Which essentially > means having to go through the logfile again. This is because i want > to try to avoid using a database to store values after parsing the file. Here is a little test program I wrote to check timing on a file of *100,000* 64 bit counters. import time, struct ctrs = 100000 s = time.time() # create a file of 800,000 characters (100,000 64 bit numbers) f = open('ravi.dat', 'wb') for i in range(ctrs): f.write('abcdefgh') # it does not matter what we write as long as it is 8 bytes. print time.time() - s s = time.time() l = [] # read the file f = open('ravi.dat', 'rb').read() # unpack each 8 characters into an integer and collect in a list for b in range(0, ctrs, 8): n = struct.unpack('q', f[b:b+8]) l.append(n) print time.time() - s Writing the file took 0.14 seconds on my computer Reading and unpacking 0.04 seconds. I think you can set performance issues aside. There is a principle in programming that proposes: 1 - get a program running. Preferably in Python as development time is much faster. 2 - check its performance. 3 - refine as needed. -- Bob Gailer Chapel Hill NC 919-636-4239 From denis.spir at free.fr Fri Dec 12 17:40:39 2008 From: denis.spir at free.fr (spir) Date: Fri, 12 Dec 2008 17:40:39 +0100 Subject: [Tutor] regex sub/super patterns ( was:advice on regex matching for dates?) In-Reply-To: References: <4941967C.2010504@free.fr> Message-ID: <49429407.6040802@free.fr> Lie Ryan a ?crit : >> I just found a simple, but nice, trick to make regexes less unlegible. >> Using substrings to represent sub-patterns. E.g. instead of: >> >> p = >> re.compile(r'(?PJanuary|February|March|April|May|June|July| > August|September|October|November|December)\s(?P\d{1,2}),\s(?P > \d{4})') >> write first: >> >> month = >> r'(?PJanuary|February|March|April|May|June|July|August|September| > October|November|December)' >> day = r'(?P\d{1,2})' >> year = r'(?P\d{4})' >> >> then: >> p = re.compile( r"%s\s%s,\s%s" % (month,day,year) ) or even: >> p = re.compile( r"%(month)s\s%(day)s,\s%(year)s" % >> {'month':month,'day':day,'year':year} ) >> > > You might realize that that wouldn't help much and might even hide bugs > (or make it harder to see). regex IS hairy in the first place. > > I'd vote for namespace in regex to allow a pattern inside a special pair > tags to be completely ignorant of anything outside it. And also I'd vote > for a compiled-pattern to be used as a subpattern. But then, with all > those limbs, I might as well use a full-blown syntax parser instead. Just tried to implement something like that for fun. The trick I chose is to reuse the {} in pattern strings, as (as far as I know) the curly braces are used only for repetition; hence there should be not conflict. A format would then look like that: format = r"...{subpatname}...{subpatname}..." For instance: format = r"record: {num}{n} -- name:{id}" (Look at the code below.) Found 2 issues: -1- Regex patterns haven't any 'string' attribute to recover the string they have beeen built from. As they aren't python objects, we cannot set anything on them. So that we need to play with strings directly. (Which means that, once the sub-pattern tested, we must copy its string.) -2- There must be a scope to look for subpattern strings per name. the only practicle solution I could think at is to make super-patterns ,of full grammars, instances of a class that copes with the details. Subpattern strings have to be attrs of this object. _______________________________________________ from re import compile as pattern class Grammar(object): identifier = "[a-zA-Z_][a-zA-Z_0-9]*" sub_pattern = pattern("\{%s\}" % identifier) def subString(self,result): name = result.group()[1:-1] try: return self.__dict__[name] except KeyError: raise AttributeError("Cannot find sub-pattern string '%s'." % name) def makePattern(self, format=None): format = self.format if format is None else format self.pattern_string = Grammar.sub_pattern.sub(self.subString,format) print "%s -->\n%s" % (self.format,self.pattern_string) self.pattern = pattern(self.pattern_string) return self.pattern if __name__ == "__main__": record = Grammar() record.num = r"n[?\.oO]\ ?" record.n = r"[0-9]+" record.id = r"[a-zA-Z_][a-zA-Z_0-9]*" record.format = r"record: {num}{n} -- name:{id}" record.makePattern() text = """ record: no 123 -- name:blah record: n.456 -- name:foo record: n?789 -- name:foo_bar """ result = record.pattern.findall(text) print result,'\n' # with format attr and invalid format bloo_format = r"record: {num}{n} -- name:{id}{bloo_bar}" record.makePattern(bloo_format) result = record.pattern.findall(text) print result From technorapture at gmail.com Sat Dec 13 00:06:35 2008 From: technorapture at gmail.com (Shrutarshi Basu) Date: Fri, 12 Dec 2008 18:06:35 -0500 Subject: [Tutor] Ask a class for it's methods Message-ID: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> I have a list containing strings like : func1[] func2[1,2] func3[blah] I want to turn them into method calls (with numeric or string arguments) on a supplied object. I'm trying to figure out the best way to do this. Since these lists could be very big, and the methods could be rather complex (mainly graphics manipulation) I would like to start by getting a list of the object's methods and make sure that all the strings are valid. Is there a way to ask an object for a list of it's methods (with argument requirements if possible)? The next question is once I've validated the list, what's the easiest way to turn the list element into a method call? I'll be parsing the string to separate out the method name and arguments. If I store the method name (say func1) in a variable, say var, could I do object.var() and have if call the func1 method in object? Thanks for your help and I'll surely have more questions as I get along, Thanks Basu -- The ByteBaker : http://www.bytebaker.com From alan.gauld at btinternet.com Sat Dec 13 00:41:38 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 12 Dec 2008 23:41:38 -0000 Subject: [Tutor] Ask a class for it's methods References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> Message-ID: "Shrutarshi Basu" wrote >I have a list containing strings like : > > func1[] > func2[1,2] > func3[blah] > > I want to turn them into method calls (with numeric or string > arguments) on a supplied object. The easiest way is to call getattr() which will return a reference to the method if it exists. > be rather complex (mainly graphics manipulation) I would like to > start > by getting a list of the object's methods and make sure that all the > strings are valid. Why not just handle it using try/except? Thats exactly what exceptions were designed for. > Is there a way to ask an object for a list of it's > methods (with argument requirements if possible)? You can try dir() but that won't give you the parameters. But again try/except can catch an invalid call and you can detect whether the number of parameters is wrong in the except clause. > method name (say func1) in a variable, say var, could I do > object.var() and have if call the func1 method in object? no, but you can do this: >>> class C: ... def f(self, x): print x ... >>> c = C() >>> dir(c) ['__doc__', '__module__', 'f'] >>> m = getattr(c,'f') >>> m(42) 42 >>> try: m(43,'foo') ... except TypeError: ... # parse the error message here ... print 'Method of', c, 'takes ??? arguments' ... Method of <__main__.C instance at 0x01BF8350> takes ??? arguments >>> HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From technorapture at gmail.com Sat Dec 13 02:05:23 2008 From: technorapture at gmail.com (Shrutarshi Basu) Date: Fri, 12 Dec 2008 20:05:23 -0500 Subject: [Tutor] Ask a class for it's methods In-Reply-To: References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> Message-ID: <376fbdcf0812121705h5b554feei49304551bc1f482c@mail.gmail.com> I normally would use exceptions, because I think exceptions are a great idea. But since the functions may be time-consuming graphics functions and the lists could easily be hundreds of such calls, I don't want the user to sit around for something that might fail. Of course, I'm just starting so my assumptions about time might not turn out to be valid, so I could just use exceptions in the end. This is an option I'm exploring. Getattr and dir seem to be the way to go for now, so I'll be trying to apply them over the weekend and see how it turns out. Any more ideas welcome. Thanks, Basu -- The ByteBaker : http://www.bytebaker.com From lie.1296 at gmail.com Sat Dec 13 02:48:52 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 13 Dec 2008 01:48:52 +0000 (UTC) Subject: [Tutor] Ask a class for it's methods References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> <376fbdcf0812121705h5b554feei49304551bc1f482c@mail.gmail.com> Message-ID: On Fri, 12 Dec 2008 20:05:23 -0500, Shrutarshi Basu wrote: > I normally would use exceptions, because I think exceptions are a great > idea. But since the functions may be time-consuming graphics functions > and the lists could easily be hundreds of such calls, I don't want the > user to sit around for something that might fail. Of course, I'm just > starting so my assumptions about time might not turn out to be valid, so > I could just use exceptions in the end. This is an option I'm exploring. The general rule of thumb is usually that exception is slow on the exceptional cases arise, while being faster if the try block doesn't fail. On the other hand, using if-block is usually slow all over, but not as slow as exception's exceptional cases. From andreas at kostyrka.org Sat Dec 13 02:59:34 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Sat, 13 Dec 2008 02:59:34 +0100 Subject: [Tutor] Ask a class for it's methods In-Reply-To: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> Message-ID: <20081213015933.GA27333@hell.kostyrka.org> On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote: > I have a list containing strings like : > > func1[] > func2[1,2] > func3[blah] > > I want to turn them into method calls (with numeric or string > arguments) on a supplied object. I'm trying to figure out the best way > to do this. Since these lists could be very big, and the methods could > be rather complex (mainly graphics manipulation) I would like to start > by getting a list of the object's methods and make sure that all the > strings are valid. Is there a way to ask an object for a list of it's > methods (with argument requirements if possible)? Well, there are ways, but they are not reliable by design. Objects can return dynamically methods. So use something like this: if callable(getattr(obj, "func1")): # func1 exists. Guess nowaday with Python3 released, you should not use callable, but instead test on __call__ if hasattr(getattr(obj, "func1"), "__call__"): > The next question is once I've validated the list, what's the easiest > way to turn the list element into a method call? I'll be parsing the > string to separate out the method name and arguments. If I store the > method name (say func1) in a variable, say var, could I do > object.var() and have if call the func1 method in object? getattr(object, var)() # calls the method named in var: class A: def x(self, a, b): return a + b instance = A() name = "x" args_positional = (1, 2) print getattr(instance, name)(*args_positional) # prints 3 args_by_name = dict(a=1, b=2) print getattr(instance, name)(**args_by_name) # prints 3 too. Andreas From lie.1296 at gmail.com Sat Dec 13 09:03:10 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 13 Dec 2008 08:03:10 +0000 (UTC) Subject: [Tutor] Ask a class for it's methods References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> <20081213015933.GA27333@hell.kostyrka.org> Message-ID: On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote: > On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote: >> I have a list containing strings like : >> >> func1[] >> func2[1,2] >> func3[blah] >> >> I want to turn them into method calls (with numeric or string >> arguments) on a supplied object. I'm trying to figure out the best way >> to do this. Since these lists could be very big, and the methods could >> be rather complex (mainly graphics manipulation) I would like to start >> by getting a list of the object's methods and make sure that all the >> strings are valid. Is there a way to ask an object for a list of it's >> methods (with argument requirements if possible)? > > Well, there are ways, but they are not reliable by design. Objects can > return dynamically methods. > > So use something like this: > > if callable(getattr(obj, "func1")): > # func1 exists. > > Guess nowaday with Python3 released, you should not use callable, but > instead test on __call__ > > if hasattr(getattr(obj, "func1"), "__call__"): or the more pythonic version would just call func() and catch exception if it's not callable: try: func1() except TypeError: print "func1 is not callable" From andreas at kostyrka.org Sat Dec 13 10:19:34 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Sat, 13 Dec 2008 10:19:34 +0100 Subject: [Tutor] Ask a class for it's methods In-Reply-To: References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> <20081213015933.GA27333@hell.kostyrka.org> Message-ID: <20081213091934.GA28026@hell.kostyrka.org> On Sat, Dec 13, 2008 at 08:03:10AM +0000, Lie Ryan wrote: > On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote: > > > On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote: > >> I have a list containing strings like : > >> > >> func1[] > >> func2[1,2] > >> func3[blah] > >> > >> I want to turn them into method calls (with numeric or string > >> arguments) on a supplied object. I'm trying to figure out the best way > >> to do this. Since these lists could be very big, and the methods could > >> be rather complex (mainly graphics manipulation) I would like to start > >> by getting a list of the object's methods and make sure that all the > >> strings are valid. Is there a way to ask an object for a list of it's > >> methods (with argument requirements if possible)? > > > > Well, there are ways, but they are not reliable by design. Objects can > > return dynamically methods. > > > > So use something like this: > > > > if callable(getattr(obj, "func1")): > > # func1 exists. > > > > Guess nowaday with Python3 released, you should not use callable, but > > instead test on __call__ > > > > if hasattr(getattr(obj, "func1"), "__call__"): > > or the more pythonic version would just call func() and catch exception > if it's not callable: > > try: > func1() > except TypeError: > print "func1 is not callable" But it happens to be wrong :) Consider: def func1(): raise TypeError("except does not care where in the callstack the exception happens!!!") Common sources, IMHE, for TypeErrors include int(not_a_string_or_number), which raises a TypeError. Andreas From btkuhn at email.unc.edu Sat Dec 13 15:52:44 2008 From: btkuhn at email.unc.edu (btkuhn at email.unc.edu) Date: Sat, 13 Dec 2008 09:52:44 -0500 Subject: [Tutor] Working with lists Message-ID: <20081213095244.n4clmwk3k4gkgk04@webmail4.isis.unc.edu> Hi everyone, I seem to use this pattern alot when writing functions and I'm wondering if there is a more efficient method. It comes up whenever I want to work with more than one item in a list; for instance, say I want to add each consecutive number in alist and output the new list. Ideally, I'd be able to write: for num in list1: newlist.append(num+nextnum) This doesn't work, though because there's no way to access "nextnum" unless I implement a "count" variable like this: count=1 for num in list1: newlist.append(num+list1[count]) count+=1 Instead, it usually ends up easier to write: for index in range (len(list1)-1): newlist.append((list1[index]+list1[index+1])) It's not a big deal to have to write the additional code, but problems arise when I use this structure in the context of more complex functions, when I am passing multiple lists of varying length, because it is easy to get confused with the index numbers and I can't do anything to the last value of the list, since I then get an "indexerror" because the function tries to call "list[i+1]". Is there a simpler way to do a procedure like this? Thanks. From roadierich at googlemail.com Sat Dec 13 17:49:20 2008 From: roadierich at googlemail.com (Richard Lovely) Date: Sat, 13 Dec 2008 16:49:20 +0000 Subject: [Tutor] Working with lists In-Reply-To: <20081213095244.n4clmwk3k4gkgk04@webmail4.isis.unc.edu> References: <20081213095244.n4clmwk3k4gkgk04@webmail4.isis.unc.edu> Message-ID: When you need indexes for a list you're iterating over, you want to look at the function enumerate(), which returns an iterator yielding (key, value) tuples. If you're working with multiple lists, itertools.imap and itertools.imap_longest are handy if you want to keep the lists in lockstep: >>> l1 = [1,2,3,4,5] >>> l2 = ['a','b','c','d','e','f'] >>> for key, value in enumerate(l1): >>> print key, value 0 1 1 2 2 3 3 4 4 5 >>> import itertools >>> for i1, i2 in itertools.izip(l1, l2): >>> print i1, i2 1 'a' 2 'b' 3 'c' 4 'd' 5 'e' >>> for i1, i2 in intertools.izip_longest(l1, l2, fillvalue=0): >>> print i1, i2 1 'a' 2 'b' 3 'c' 4 'd' 5 'e' 0 'f' Preventing yourself from using invalid indexes is harder, and depends on exactly what you want to happen to the last value in the source list. To skip that last value, you could use something like the following: if value == longest_list[-1]: break using -1 as an index will always return the last value of that list. Passing a unique value to fillvalue on izip_longest is another way. I normally use a specially created class: class EndOfList(object): pass then you do: for values in izip_longest(l1, l2... fillvalue=EndOfList) if EndOfList in values: # one of the lists has run out of values # do something special, then break And another way of doing your first example, that's one of the extremly useful tools in python's armoury: newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] This is a list comprehension, and is a very useful tool when you want to build a list from another list (or any other iterable). The 'slices' each give you a list with one less item, the first missing the last item, the second, the first item. Hope there's the information somewhere in this somewhat jumbled response to help you. Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com 2008/12/13 : > Hi everyone, > > I seem to use this pattern alot when writing functions and I'm wondering if > there is a more efficient method. It comes up whenever I want to work with > more than one item in a list; for instance, say I want to add each > consecutive number in alist and output the new list. Ideally, I'd be able to > write: > > for num in list1: > newlist.append(num+nextnum) > > This doesn't work, though because there's no way to access "nextnum" unless > I implement a "count" variable like this: > > count=1 > for num in list1: > newlist.append(num+list1[count]) > count+=1 > > Instead, it usually ends up easier to write: > > for index in range (len(list1)-1): > newlist.append((list1[index]+list1[index+1])) > > It's not a big deal to have to write the additional code, but problems arise > when I use this structure in the context of more complex functions, when I > am passing multiple lists of varying length, because it is easy to get > confused with the index numbers and I can't do anything to the last value of > the list, since I then get an "indexerror" because the function tries to > call "list[i+1]". > > Is there a simpler way to do a procedure like this? > > Thanks. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Sat Dec 13 19:19:00 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Dec 2008 18:19:00 -0000 Subject: [Tutor] Working with lists References: <20081213095244.n4clmwk3k4gkgk04@webmail4.isis.unc.edu> Message-ID: wrote > Ideally, I'd be able to write: > > for num in list1: > newlist.append(num+nextnum) > > This doesn't work, though because there's no way to access "nextnum" > unless I implement a "count" variable like this: Or use an iterator: >>> L = [1,2,3,4,5,6] >>> i = iter(L) >>> L2 = [n+i.next() for n in i] >>> L2 [3, 7, 11] Is that what you want? -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From lie.1296 at gmail.com Sun Dec 14 03:35:35 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 14 Dec 2008 02:35:35 +0000 (UTC) Subject: [Tutor] Ask a class for it's methods References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> <20081213015933.GA27333@hell.kostyrka.org> <20081213091934.GA28026@hell.kostyrka.org> Message-ID: On Sat, 13 Dec 2008 10:19:34 +0100, Andreas Kostyrka wrote: > On Sat, Dec 13, 2008 at 08:03:10AM +0000, Lie Ryan wrote: >> On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote: >> >> > On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote: >> >> I have a list containing strings like : >> >> >> >> func1[] >> >> func2[1,2] >> >> func3[blah] >> >> >> >> I want to turn them into method calls (with numeric or string >> >> arguments) on a supplied object. I'm trying to figure out the best >> >> way to do this. Since these lists could be very big, and the methods >> >> could be rather complex (mainly graphics manipulation) I would like >> >> to start by getting a list of the object's methods and make sure >> >> that all the strings are valid. Is there a way to ask an object for >> >> a list of it's methods (with argument requirements if possible)? >> > >> > Well, there are ways, but they are not reliable by design. Objects >> > can return dynamically methods. >> > >> > So use something like this: >> > >> > if callable(getattr(obj, "func1")): >> > # func1 exists. >> > >> > Guess nowaday with Python3 released, you should not use callable, but >> > instead test on __call__ >> > >> > if hasattr(getattr(obj, "func1"), "__call__"): >> >> or the more pythonic version would just call func() and catch exception >> if it's not callable: >> >> try: >> func1() >> except TypeError: >> print "func1 is not callable" > > But it happens to be wrong :) > > Consider: > > def func1(): > raise TypeError("except does not care where in the callstack the > exception happens!!!") > > Common sources, IMHE, for TypeErrors include > int(not_a_string_or_number), which raises a TypeError. Then it's the lower function's fault (i.e. bug) for not handling that exception, and if you really need it, it's trivial to check the "kind" of TypeError reraising the exception if it's not what we expect. try: func1() except TypeError, e: if not e.message.endswith('object is not callable'): print 'There is something deeply wrong in your func1' raise print "func1 is not callable" From metolone+gmane at gmail.com Sun Dec 14 05:40:44 2008 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 13 Dec 2008 20:40:44 -0800 Subject: [Tutor] Working with lists References: <20081213095244.n4clmwk3k4gkgk04@webmail4.isis.unc.edu> Message-ID: wrote in message news:20081213095244.n4clmwk3k4gkgk04 at webmail4.isis.unc.edu... > Hi everyone, > > I seem to use this pattern alot when writing functions and I'm wondering > if there is a more efficient method. It comes up whenever I want to work > with more than one item in a list; for instance, say I want to add each > consecutive number in alist and output the new list. Ideally, I'd be able > to write: > > for num in list1: > newlist.append(num+nextnum) > > This doesn't work, though because there's no way to access "nextnum" > unless I implement a "count" variable like this: > > count=1 > for num in list1: > newlist.append(num+list1[count]) > count+=1 > > Instead, it usually ends up easier to write: > > for index in range (len(list1)-1): > newlist.append((list1[index]+list1[index+1])) > > It's not a big deal to have to write the additional code, but problems > arise when I use this structure in the context of more complex functions, > when I am passing multiple lists of varying length, because it is easy to > get confused with the index numbers and I can't do anything to the last > value of the list, since I then get an "indexerror" because the function > tries to call "list[i+1]". > > Is there a simpler way to do a procedure like this? >From your description, do you want: L = [1,2,3,4,5,6] result = [3,5,6,9,11]? I don't see anything in itertools, but this works: >>> def ishift(L,n): ... for i in xrange(len(L)-n+1): ... yield L[i:i+n] ... >>> for n in shift(L,2): ... print n ... [1, 2] [2, 3] [3, 4] [4, 5] [5, 6] >>> for a,b,c in shift(L,3): ... print a,b,c ... 1 2 3 2 3 4 3 4 5 4 5 6 >>> [sum(n) for n in ishift(L,2)] [3, 5, 7, 9, 11] >>> [sum(n) for n in ishift(L,3)] [6, 9, 12, 15] -Mark From david at hlacik.eu Sun Dec 14 13:38:31 2008 From: david at hlacik.eu (=?ISO-8859-2?Q?David_Hl=E1=E8ik?=) Date: Sun, 14 Dec 2008 13:38:31 +0100 Subject: [Tutor] stable algorithm Message-ID: Hi guys, i am really sorry for making offtopic, hope you will not kill me, but this is for me life important problem which needs to be solved within next 12 hours.. I have to create stable algorithm for sorting n numbers from interval [1,n^2] with time complexity O(n) . Can someone please give me a hint. Would be very very thankful! Thanks in advance! D. From ajoaugustine at gmail.com Sun Dec 14 13:39:35 2008 From: ajoaugustine at gmail.com (Ajo Augustine) Date: Sun, 14 Dec 2008 18:09:35 +0530 Subject: [Tutor] reading inputs from keyboard Message-ID: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> hi all; can u let me know how to read an input string from the keyboard? -- Ajo -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sun Dec 14 15:00:26 2008 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Dec 2008 09:00:26 -0500 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> Message-ID: <4945117A.1000500@gmail.com> Ajo Augustine wrote: > hi all; > can u let me know how to read an input string from the keyboard? In Python 2.6 and lower: raw_input(prompt) Example program: name = raw_input("Enter your name:") print "Hello", name raw_input("Press any key to exit.") In Python 3 Example program: name = input("Enter your name:") print ("Hello", name) input("Press any key to exit.") The purpose of the last line - when you run the program by double-clicking or equivalent this keeps the window open so you can see the results. -- Bob Gailer Chapel Hill NC 919-636-4239 From ptmcg at austin.rr.com Sun Dec 14 15:46:17 2008 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 14 Dec 2008 08:46:17 -0600 Subject: [Tutor] Working with lists In-Reply-To: References: Message-ID: <3E0E214CD0DB42FDA20AEDE2265C51EE@AWA2> Even simpler than Rich Lovely's: newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] is to just use the built-in zip: newlist = [a+b for a,b in zip(l1[:-1], l1[1:])] since you can be sure that l1[:-1] and l1[1:] will always be the same length, so there is no need for a fill value (one of the enhancements you get when using itertools.izip). I often use zip this way when I need to get each item and its next-highest neighbor from a list. -- Paul From btkuhn at email.unc.edu Sun Dec 14 15:49:46 2008 From: btkuhn at email.unc.edu (btkuhn at email.unc.edu) Date: Sun, 14 Dec 2008 09:49:46 -0500 Subject: [Tutor] Python Lib Files Message-ID: <20081214094946.4k7u890c8wgw0okc@webmail4.isis.unc.edu> Hello everyone, I discovered yesterday that the Python package has a number of built in example scripts in the /lib directory. Perhaps this is common knowledge but I did not know about it. I can't seem to find any kind of guide to the files, though. Is there a readme somewhere that someone can point me to, or perhaps documentation for the files online? I cant find anything on the python.org site. This is for version 2.5. Thanks very much, Ben From alan.gauld at btinternet.com Sun Dec 14 15:57:34 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Dec 2008 14:57:34 -0000 Subject: [Tutor] stable algorithm References: Message-ID: "David Hl?cik" wrote > this is for me life important problem which needs to be solved > within > next 12 hours.. Does that mean its homework? If so we can only offer pointers, but you need to tell us what you have done yourself first. If its not homework then please give a bit more background. What exactly are you doing that needs this sort? - can you avoid sorting in the first place, for example? What are the volumes? Why is it time critical? > I have to create stable algorithm for sorting n numbers from > interval > [1,n^2] with time complexity O(n) . Where have you looked for algorithms? Which ones have you considered? Why were they unsuitable? Or when you say "create" do you literally mean you have to invent a brand new algorithm? That seems a bit of a harsh demand in just 12 hours? > Can someone please give me a hint. Would be very very thankful! Google/Wikipedia would be good places to start. Then try the Python documentation on sorting. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Sun Dec 14 16:01:03 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Dec 2008 15:01:03 -0000 Subject: [Tutor] reading inputs from keyboard References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> Message-ID: "Ajo Augustine" wrote > can u let me know how to read an input string from the keyboard? If you need to ask that then you need to work through any of the many Python tutorials. They all cover this, usually fairly early in the course. Try some of those listed here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers or here http://wiki.python.org/moin/BeginnersGuide/Programmers depending on your past experience. If you have questions come back here and we will try to answer them or point you to the answer. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From david at hlacik.eu Sun Dec 14 17:08:24 2008 From: david at hlacik.eu (=?ISO-8859-2?Q?David_Hl=E1=E8ik?=) Date: Sun, 14 Dec 2008 17:08:24 +0100 Subject: [Tutor] stable algorithm In-Reply-To: References: Message-ID: > > Does that mean its homework? > Hi, yes it is homework , this is result : #! /usr/bin/python def sort(numbers): "sort n positive integers in O(n) provided that they are all < n^2" N = len(numbers) # get size of test numbers buckets_mod = [[] for i in xrange(N)] buckets_sorted = [[] for i in xrange(N+1)] # group numbers to buckets (list of numbers) with common modulus for n in numbers: buckets_mod[n % N].append(n) print "buckets_mod: %s" % buckets_mod # check numbers in buckets for l in buckets_mod: for n in l: # place number into bucket number grouped by result of division buckets_sorted[n / N].append(n) print "buckets_sorted: %s" % buckets_sorted # search through sorted buckets and return list of sorted numbers return [n for l in buckets_sorted for n in l] Hope it is optimal and OK, what you guys thinks? Thanks in advance! From david at hlacik.eu Sun Dec 14 17:09:28 2008 From: david at hlacik.eu (=?ISO-8859-2?Q?David_Hl=E1=E8ik?=) Date: Sun, 14 Dec 2008 17:09:28 +0100 Subject: [Tutor] stable algorithm In-Reply-To: References: Message-ID: > def sort(numbers): > "sort n positive integers in O(n) provided that they are all < n^2" Sorry about wrong comment, should be range from [1 to n^2] not only < n^2. D. From bgailer at gmail.com Sun Dec 14 17:16:08 2008 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Dec 2008 11:16:08 -0500 Subject: [Tutor] Working with lists In-Reply-To: <3E0E214CD0DB42FDA20AEDE2265C51EE@AWA2> References: <3E0E214CD0DB42FDA20AEDE2265C51EE@AWA2> Message-ID: <49453148.1020600@gmail.com> Paul McGuire wrote: > Even simpler than Rich Lovely's: > > newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] > > is to just use the built-in zip: > > newlist = [a+b for a,b in zip(l1[:-1], l1[1:])] > And then there's good old reduce which sadly is going to be harder to access in Python 3: v = [1,2,3,4] m = [] reduce(lambda x,y,m=m: (m.append(x+y), y)[1], v) print m # [3, 5, 7] -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Sun Dec 14 17:58:45 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Dec 2008 16:58:45 -0000 Subject: [Tutor] stable algorithm References: Message-ID: "David Hl?cik" wrote > yes it is homework , this is result : > > #! /usr/bin/python > def sort(numbers): > "sort n positive integers in O(n) provided that they are all > < n^2" > N = len(numbers) # get size of test numbers > > buckets_mod = [[] for i in xrange(N)] > buckets_sorted = [[] for i in xrange(N+1)] > > # group numbers to buckets (list of numbers) with common > modulus > for n in numbers: > buckets_mod[n % N].append(n) > print "buckets_mod: %s" % buckets_mod It would be interesting to see if using a dictionasry aws a sparce array was any faster - it would save the initialisation step and reduce the number of accesses of empty lists. > # check numbers in buckets > for l in buckets_mod: > for n in l: > buckets_sorted[n / N].append(n) > print "buckets_sorted: %s" % buckets_sorted > > # search through sorted buckets and return list of sorted > numbers > return [n for l in buckets_sorted for n in l] > > Hope it is optimal and OK, what you guys thinks? Its almost certainly not optimal but from some basic testing it seems to work and is linear as far as I can see (albeit with 5 loops over N). Alan G From bermanrl at embarqmail.com Sun Dec 14 18:21:59 2008 From: bermanrl at embarqmail.com (Robert Berman) Date: Sun, 14 Dec 2008 12:21:59 -0500 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> Message-ID: <494540B7.7000609@embarqmail.com> An HTML attachment was scrubbed... URL: From david at hlacik.eu Sun Dec 14 18:23:56 2008 From: david at hlacik.eu (=?ISO-8859-2?Q?David_Hl=E1=E8ik?=) Date: Sun, 14 Dec 2008 18:23:56 +0100 Subject: [Tutor] stable algorithm In-Reply-To: References: Message-ID: Hi, thank you very much. And how can i write single test which will tell me execution time of this algorithm? I need to write a table with number of data & execution time comparison (and it should be linear in case of this algorithm) Thanks! D. On Sun, Dec 14, 2008 at 5:58 PM, Alan Gauld wrote: > "David Hl?cik" wrote > >> yes it is homework , this is result : >> >> #! /usr/bin/python >> def sort(numbers): >> "sort n positive integers in O(n) provided that they are all < n^2" >> N = len(numbers) # get size of test numbers >> >> buckets_mod = [[] for i in xrange(N)] >> buckets_sorted = [[] for i in xrange(N+1)] >> >> # group numbers to buckets (list of numbers) with common modulus >> for n in numbers: >> buckets_mod[n % N].append(n) >> print "buckets_mod: %s" % buckets_mod > > It would be interesting to see if using a dictionasry aws a sparce > array was any faster - it would save the initialisation step and > reduce the number of accesses of empty lists. > >> # check numbers in buckets >> for l in buckets_mod: >> for n in l: >> buckets_sorted[n / N].append(n) >> print "buckets_sorted: %s" % buckets_sorted >> >> # search through sorted buckets and return list of sorted numbers >> return [n for l in buckets_sorted for n in l] >> >> Hope it is optimal and OK, what you guys thinks? > > Its almost certainly not optimal but from some basic testing it seems to > work and is linear as far as I can see (albeit with 5 loops over N). > > Alan G > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From denis.spir at free.fr Sun Dec 14 19:32:34 2008 From: denis.spir at free.fr (spir) Date: Sun, 14 Dec 2008 19:32:34 +0100 Subject: [Tutor] stable algorithm In-Reply-To: References: Message-ID: <49455142.5030509@free.fr> David Hl??ik a ?crit : > Hi, > > thank you very much. > > And how can i write single test which will tell me execution time of > this algorithm? > > I need to write a table with number of data & execution time > comparison (and it should be linear in case of this algorithm) > > Thanks! > > D. > First, google will give you tons of links on the topic. time.time() returns present time in ms The func below is practicle, too. denis def timer(n,f,*args,**kw_args): t0 = time() for i in range(n): f(*args,**kw_args) t = time() - t0 arg_str = ','.join(str(arg) for arg in args) kw_arg_str = ','+str(kw_args)[1:-1] if kw_args else '' print "%s(%s%s) %s time(s) <--> %0.3f s" % (f.__name__,arg_str,kw_arg_str,n,t) From bermanrl at embarqmail.com Sun Dec 14 19:48:26 2008 From: bermanrl at embarqmail.com (Robert Berman) Date: Sun, 14 Dec 2008 13:48:26 -0500 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <4945517F.6060806@free.fr> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> <494540B7.7000609@embarqmail.com> <4945517F.6060806@free.fr> Message-ID: <494554FA.7020800@embarqmail.com> An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Sun Dec 14 19:58:43 2008 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sun, 14 Dec 2008 12:58:43 -0600 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <494554FA.7020800@embarqmail.com> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> <494540B7.7000609@embarqmail.com> <4945517F.6060806@free.fr> <494554FA.7020800@embarqmail.com> Message-ID: A more gentle nudging in the correct direction is generally more well-received. Honestly, it doesn't help anyone to be rude, and we'll all just think less of you for this unnecessary divergence. If you're not going to at be witty and subtle about it, you may as well direct them to Eric Raymond's article on how to ask smart questions. He's already written this chastisement, and better than you. Please don't pollute our space with noise and needless posturing. If your response is not going to be more helpful than "RTFM" or "GIYF" perhaps you should reconsider hitting that send button. On Sun, Dec 14, 2008 at 12:48 PM, Robert Berman wrote: > Rather than being impressed, perhaps you might consider performing the > action. > > spir wrote: > > Robert Berman a ?crit : > > RTFM. > > how clever! > I'm impressed... > Denis > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From marc.tompkins at gmail.com Sun Dec 14 20:04:54 2008 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 14 Dec 2008 11:04:54 -0800 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <494554FA.7020800@embarqmail.com> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> <494540B7.7000609@embarqmail.com> <4945517F.6060806@free.fr> <494554FA.7020800@embarqmail.com> Message-ID: <40af687b0812141104h4294518fk6e198fdc5fc389ff@mail.gmail.com> On Sun, Dec 14, 2008 at 10:48 AM, Robert Berman wrote: > > Rather than being impressed, perhaps you might consider performing the action. > > spir wrote: > > Robert Berman a ?crit : > > RTFM. > > how clever! > I'm impressed... > Denis Also, you might have noticed that Denis wasn't the original poster. More of a community response, looks like. We're ALL impressed, and some of us do read the fine manual. -- www.fsrtechnologies.com From alan.gauld at btinternet.com Sun Dec 14 20:21:50 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Dec 2008 19:21:50 -0000 Subject: [Tutor] stable algorithm References: <49455142.5030509@free.fr> Message-ID: "spir" wrote >> And how can i write single test which will tell me execution time >> of >> this algorithm? > First, google will give you tons of links on the topic. > time.time() returns present time in ms You could also use the python profiler which will give you timing information for just the sort function itself. That may be more useful if trying to prove the linearity of your sort rather than of the overall program including data production etc. You should be able to write a function to generate a list of N random numbers easily enough.... Now write a loop to do that for 2,20,200,200 and 200,000 numbers. Tabulate the results. Alan G From sander.sweers at gmail.com Sun Dec 14 20:43:21 2008 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 14 Dec 2008 20:43:21 +0100 Subject: [Tutor] reading inputs from keyboard In-Reply-To: <494540B7.7000609@embarqmail.com> References: <3eff71880812140439i4bce6028s5a350b97591da1db@mail.gmail.com> <494540B7.7000609@embarqmail.com> Message-ID: On Sun, Dec 14, 2008 at 18:21, Robert Berman wrote: > RTFM. Thank you for your non contribution python tutor mailing list.The minimum you could do is point to the FM which others luckily already did. Greets Sander From roadierich at googlemail.com Mon Dec 15 02:42:47 2008 From: roadierich at googlemail.com (Rich Lovely) Date: Mon, 15 Dec 2008 01:42:47 +0000 Subject: [Tutor] Working with lists In-Reply-To: <3E0E214CD0DB42FDA20AEDE2265C51EE@AWA2> References: <3E0E214CD0DB42FDA20AEDE2265C51EE@AWA2> Message-ID: <948041D5-B3A0-4034-9952-243F59DE3F15@googlemail.com> Yeah, you could do that, but it was quite a revelation when I discovered itertools, and I'm just trying to share the love. --- Richard "Roadie Rich" Lovely Part of the JNP|UK Famille www.theJNP.com (Sent from my iPod - please allow me a few typos: it's a very small keyboard) On 14 Dec 2008, at 14:46, "Paul McGuire" wrote: > Even simpler than Rich Lovely's: > > newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] > > is to just use the built-in zip: > > newlist = [a+b for a,b in zip(l1[:-1], l1[1:])] > > since you can be sure that l1[:-1] and l1[1:] will always be the same > length, so there is no need for a fill value (one of the > enhancements you > get when using itertools.izip). > > I often use zip this way when I need to get each item and its next- > highest > neighbor from a list. > > -- Paul > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From pyth0nc0d3r at gmail.com Mon Dec 15 13:51:07 2008 From: pyth0nc0d3r at gmail.com (Lamonte Harris) Date: Mon, 15 Dec 2008 06:51:07 -0600 Subject: [Tutor] Having Issues with CMD and the 'python' command Message-ID: Every time I start cmd on windows it requires me to "set path=%path%;C:\python26" why? I'm getting annoyed... -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Dec 15 14:31:30 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 15 Dec 2008 08:31:30 -0500 Subject: [Tutor] Having Issues with CMD and the 'python' command In-Reply-To: References: Message-ID: <49465C32.7020200@gmail.com> An HTML attachment was scrubbed... URL: From gmorris at serif.com Fri Dec 12 18:47:09 2008 From: gmorris at serif.com (Gareth at Serif) Date: Fri, 12 Dec 2008 09:47:09 -0800 (PST) Subject: [Tutor] MP3Info class usage In-Reply-To: <20081210140534.GM13722@inocybe.teonanacatl.org> References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> Message-ID: <20980334.post@talk.nabble.com> Okay, I've moved over to eyeD3.py... fif nothing else, the documentation for it's usage is far superior. However, I can't get anything to run. The help gives examples of some simple tasks, but as soon as I 'import eyeD3', not even calling any functions within it, I start getting errors such as: In eyeD3.py, the 'from eyeD3.tag import *;' line (and similar) didn't work until I removed the 'eyeD3.' part and just imported 'tag'. Then in tag.py, some of the class defs complained about undefined variables, for example 'def link(self, f, v = ID3_ANY_VERSION)' needed to have quotes around 'ID3_ANY_VERSION'. Now these issues are addressed I have an error in mp3.py, 'class EyeD3Driver(eyeD3.utils.FileHandler) NameError: name 'eyeD3' is not defined. I'm baffled that I'm having to jump through so many hoops because I imported eyeD3... is this typical? What have I done wrong? Cheers, Gareth Todd Zullinger wrote: > > I'd recommend eyeD3? and/or mutagen? for tag reading. Both are pretty > easy to use. > > ? http://eyed3.nicfit.net/ > ? http://code.google.com/p/quodlibet/wiki/Development/Mutagen > > -- View this message in context: http://www.nabble.com/MP3Info-class-usage-tp20934673p20980334.html Sent from the Python - tutor mailing list archive at Nabble.com. From prologic at shortcircuit.net.au Mon Dec 15 14:01:01 2008 From: prologic at shortcircuit.net.au (James Mills) Date: Mon, 15 Dec 2008 23:01:01 +1000 Subject: [Tutor] Having Issues with CMD and the 'python' command In-Reply-To: References: Message-ID: "cmd" has _nothing_ to do with Python. --JamesMills -- -- "Problems are solved by method" On Mon, Dec 15, 2008 at 10:51 PM, Lamonte Harris wrote: > Every time I start cmd on windows it requires me to "set > path=%path%;C:\python26" why? I'm getting annoyed... > > -- > http://mail.python.org/mailman/listinfo/python-list > > From rdmurray at bitdance.com Mon Dec 15 14:13:11 2008 From: rdmurray at bitdance.com (rdmurray at bitdance.com) Date: Mon, 15 Dec 2008 08:13:11 -0500 (EST) Subject: [Tutor] Having Issues with CMD and the 'python' command In-Reply-To: References: Message-ID: On Mon, 15 Dec 2008 at 23:01, James Mills wrote: > On Mon, Dec 15, 2008 at 10:51 PM, Lamonte Harris wrote: >> Every time I start cmd on windows it requires me to "set >> path=%path%;C:\python26" why? I'm getting annoyed... > > "cmd" has _nothing_ to do with Python. > (Top posting corrected.) But the answer is that you need to update your PATH string at the system level. You do that in Control Panel/System/Advanced/Environment variables (it's a button on the advanced screen, which is something that confused me the first time I went looking for it). ObPython: you know, it occurs to me that Windows follows exactly the opposite philosophy from Python when it comes to hierarchy. Python's Zen is "shallow is better than deep", whereas Windows' philosophy is "deep is better than shallow". Every release of Windows seems to bury the things one needs to do to administer the system deeper and deeper inside a nested set of windows...and every time I touch Windows I am reminded how sensible the Python Zen is :) --RDM From kent37 at tds.net Mon Dec 15 14:53:19 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 15 Dec 2008 08:53:19 -0500 Subject: [Tutor] Python Lib Files In-Reply-To: <20081214094946.4k7u890c8wgw0okc@webmail4.isis.unc.edu> References: <20081214094946.4k7u890c8wgw0okc@webmail4.isis.unc.edu> Message-ID: <1c2a2c590812150553x12527410jfa53a005eda70031@mail.gmail.com> On Sun, Dec 14, 2008 at 9:49 AM, wrote: > Hello everyone, > > I discovered yesterday that the Python package has a number of built in > example scripts in the /lib directory. Perhaps this is common knowledge but > I did not know about it. I can't seem to find any kind of guide to the > files, though. Is there a readme somewhere that someone can point me to, or > perhaps documentation for the files online? I cant find anything on the > python.org site. This is for version 2.5. Those are not sample scripts, they are the standard library. There is extensive documentation, for example http://docs.python.org/library/ or for Python 2.5 see http://www.python.org/doc/2.5.2/lib/lib.html Kent From alan.gauld at btinternet.com Mon Dec 15 16:14:26 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 15 Dec 2008 15:14:26 -0000 Subject: [Tutor] Python Lib Files References: <20081214094946.4k7u890c8wgw0okc@webmail4.isis.unc.edu> <1c2a2c590812150553x12527410jfa53a005eda70031@mail.gmail.com> Message-ID: "Kent Johnson" wrote >> I discovered yesterday that the Python package has a number of >> built in >> example scripts in the /lib directory. > > Those are not sample scripts, they are the standard library. There > is > extensive documentation, for example However there are some sample scripts in the Tools directory... For example a regex checker and reindentation tool. HTH, Alan G. From alan.gauld at btinternet.com Mon Dec 15 16:17:40 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 15 Dec 2008 15:17:40 -0000 Subject: [Tutor] Having Issues with CMD and the 'python' command References: <49465C32.7020200@gmail.com> Message-ID: "bob gailer" wrote > Try this: > > Start->Settings->Control Panel->System->Advanced->Environment > Variables > Highlight PATH under System Variables & Click Edit. > Add ;C:\python26 And notice that Bob said ADD - DO NOT REPLACE the existing setting or you will likely break stuff and its not easy to fix it afterwards unless you have a full backup to hand!! Alan G. From kent37 at tds.net Mon Dec 15 16:22:10 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 15 Dec 2008 10:22:10 -0500 Subject: [Tutor] Ask a class for it's methods In-Reply-To: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> References: <376fbdcf0812121506y200d9374ufb67a5e2ff35c0@mail.gmail.com> Message-ID: <1c2a2c590812150722o7150a45ax42592997be847175@mail.gmail.com> On Fri, Dec 12, 2008 at 6:06 PM, Shrutarshi Basu wrote: > Is there a way to ask an object for a list of it's > methods (with argument requirements if possible)? Take a look at the inspect module. If it does not directly give you what you need, look at the source - it looks at function attributes that you can directly access. Kent From benjamin.kaplan at case.edu Mon Dec 15 16:56:29 2008 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 15 Dec 2008 10:56:29 -0500 Subject: [Tutor] Having Issues with CMD and the 'python' command In-Reply-To: References: Message-ID: On Mon, Dec 15, 2008 at 8:13 AM, wrote: > On Mon, 15 Dec 2008 at 23:01, James Mills wrote: > >> On Mon, Dec 15, 2008 at 10:51 PM, Lamonte Harris >> wrote: >> >>> Every time I start cmd on windows it requires me to "set >>> path=%path%;C:\python26" why? I'm getting annoyed... >>> >> >> "cmd" has _nothing_ to do with Python. >> >> > (Top posting corrected.) > > But the answer is that you need to update your PATH string at the system > level. You do that in Control Panel/System/Advanced/Environment variables > (it's a button on the advanced screen, which is something that confused > me the first time I went looking for it). > > ObPython: you know, it occurs to me that Windows follows exactly the > opposite philosophy from Python when it comes to hierarchy. Python's > Zen is "shallow is better than deep", whereas Windows' philosophy > is "deep is better than shallow". Every release of Windows seems > to bury the things one needs to do to administer the system deeper > and deeper inside a nested set of windows...and every time I touch > Windows I am reminded how sensible the Python Zen is :) > It's not a question of sensibility. It's a question of purpose. The Zen is the philosophy of a language that tries to be easy to learn and easy to use. Python is used by programmers who want to experiment with it, but who usually know enough not to os.system("rm -r /") or anything similar. Windows, on the other hand, wants to hide everything that can potentially ruin the system as deep as possible so that many of the idiots who use that system don't do stupid things like delete the registry, wipe the environment settings, turn off the "Nag Screen" (UAC), and other things of that nature. > --RDM > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fisher.127 at wright.edu Mon Dec 15 17:32:09 2008 From: fisher.127 at wright.edu (Bradford Fisher) Date: Mon, 15 Dec 2008 11:32:09 -0500 Subject: [Tutor] Creating IDLE Shortcut Message-ID: <6640dc74df37.49464039@wright.edu> Hello, Does anyone know the proper method for re-creating the Windows XP shortcut for IDLE? I (not thinking clearly) deleted the original and now am not sure how to go about fixing the problem. I've attempted creating a shortcut and linking it to pythonw.exe with the target idle.pyw, but when loading IDLE with that shortcut all of my settings are lost and the load process is far greater. - Brad From jfabiani at yolo.com Mon Dec 15 19:18:28 2008 From: jfabiani at yolo.com (johnf) Date: Mon, 15 Dec 2008 10:18:28 -0800 Subject: [Tutor] need to clean a string Message-ID: <200812151018.28151.jfabiani@yolo.com> Hi, I have a string that I need to extract just the sql statement. The following is the string \000\000\000\000\000\000\000\000.\000\000\000\0000\000\000\000 \000$\000\000\000\000\000(\000\000\000\000X\000\000\000\000\000, \000\000\000\000P\000\000\000\000\000Q\000\000\000\000\000R\000 \000\000\000\000/\000\000\000? \000\000\000\000%???? \000\000\000 \000'????\000\000\000\000+brttyp\000?\000\000\000\000*SELECT Brttyp.ctrscode, Brttyp.cdescript, Brttyp.ctrstype, Brttyp.cstatus, Brttyp.lreqno, Brttyp.dcreate FROM brttyp Brttyp WHERE Brttyp.ctrscode = ( ?gcKey1 )\000\000\000\000\000 AMCONNECT\000\000\000\000\000) \000\000\000\000 \000\000\000 \000\000\000\000BR Transaction Code File\000 How can I get rid of all the junk? I can find the '*' with str.find('*') but I can't use find '\000' to get to the end? -- John Fabiani From andreas at kostyrka.org Mon Dec 15 20:31:12 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Mon, 15 Dec 2008 20:31:12 +0100 Subject: [Tutor] need to clean a string In-Reply-To: <200812151018.28151.jfabiani@yolo.com> References: <200812151018.28151.jfabiani@yolo.com> Message-ID: <20081215203112.62e7b58b@andi-lap> Am Mon, 15 Dec 2008 10:18:28 -0800 schrieb johnf : > Hi, > I have a string that I need to extract just the sql > statement. The following is the string > \000\000\000\000\000\000\000\000.\000\000\000\0000\000\000\000 > \000$\000\000\000\000\000(\000\000\000\000X\000\000\000\000\000, > \000\000\000\000P\000\000\000\000\000Q\000\000\000\000\000R\000 > \000\000\000\000/\000\000\000? \000\000\000\000%???? \000\000\000 > \000'????\000\000\000\000+brttyp\000?\000\000\000\000*SELECT > Brttyp.ctrscode, Brttyp.cdescript, Brttyp.ctrstype, Brttyp.cstatus, > Brttyp.lreqno, Brttyp.dcreate FROM brttyp Brttyp WHERE > Brttyp.ctrscode = ( ?gcKey1 )\000\000\000\000\000 > AMCONNECT\000\000\000\000\000) \000\000\000\000 \000\000\000 > \000\000\000\000BR Transaction Code File\000 > > How can I get rid of all the junk? > > I can find the '*' with str.find('*') but I can't use find '\000' to > get to the end? str.find has an optional second parameter specifying the start where to search: Given the above string as str, you can do: start_pos = str.find('*') sql = None if start_pos != -1: end_pos = str.find('\0', start_pos) if end_pos == -1: end_pos = len(str) sql = str[start_pos:end_pos] Andreas From mlknack at gmail.com Mon Dec 15 21:33:57 2008 From: mlknack at gmail.com (Mary Lou Knack) Date: Mon, 15 Dec 2008 13:33:57 -0700 Subject: [Tutor] what does the "@" operator mean? Message-ID: <221405905FE34478B30A32066A739A26@MLKsHP> I'm looking at some code from Enthought's ETS examples and ran across the following statement: @mayavi2.standalone I have no idea what the "@" operator means, if anything. I tried searching for it in the documentation (locally and online), but no luck. Or rather, the local (windows) help said it couldn't search for that phrase (@) and the online search turned up a bazillion hits and I don't know how to refine the search. Knowledge isn't necessary for me to proceed, but it sure would be nice. Thanks. Mary Lou Knack -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Mon Dec 15 21:42:26 2008 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Mon, 15 Dec 2008 12:42:26 -0800 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <221405905FE34478B30A32066A739A26@MLKsHP> References: <221405905FE34478B30A32066A739A26@MLKsHP> Message-ID: <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> On Mon, Dec 15, 2008 at 12:33 PM, Mary Lou Knack wrote: > I'm looking at some code from Enthought's ETS examples and ran across the > following statement: > > @mayavi2.standalone > > I have no idea what the "@" operator means, if anything. I tried searching > for it in the documentation (locally and online), but no luck. Or rather, > the local (windows) help said it couldn't search for that phrase (@) and the > online search turned up a bazillion hits and I don't know how to refine the > search. It indicates a decorator. Try this: http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators If you're just starting out in Python, decorators can be hard to get your head around... -- www.fsrtechnologies.com From jfabiani at yolo.com Mon Dec 15 21:43:46 2008 From: jfabiani at yolo.com (johnf) Date: Mon, 15 Dec 2008 12:43:46 -0800 Subject: [Tutor] need to clean a string In-Reply-To: <20081215203112.62e7b58b@andi-lap> References: <200812151018.28151.jfabiani@yolo.com> <20081215203112.62e7b58b@andi-lap> Message-ID: <200812151243.46677.jfabiani@yolo.com> On Monday 15 December 2008 11:31:12 am Andreas Kostyrka wrote: > Am Mon, 15 Dec 2008 10:18:28 -0800 > > schrieb johnf : > > Hi, > > I have a string that I need to extract just the sql > > statement. The following is the string > > \000\000\000\000\000\000\000\000.\000\000\000\0000\000\000\000 > > > > \000$\000\000\000\000\000(\000\000\000\000X\000\000\000\000\000, > > \000\000\000\000P\000\000\000\000\000Q\000\000\000\000\000R\000 > > \000\000\000\000/\000\000\000? \000\000\000\000%???? \000\000\000 > > \000'????\000\000\000\000+brttyp\000?\000\000\000\000*SELECT > > Brttyp.ctrscode, Brttyp.cdescript, Brttyp.ctrstype, Brttyp.cstatus, > > Brttyp.lreqno, Brttyp.dcreate FROM brttyp Brttyp WHERE > > Brttyp.ctrscode = ( ?gcKey1 )\000\000\000\000\000 > > AMCONNECT\000\000\000\000\000) \000\000\000\000 \000\000\000 > > \000\000\000\000BR Transaction Code File\000 > > > > How can I get rid of all the junk? > > > > I can find the '*' with str.find('*') but I can't use find '\000' to > > get to the end? > > str.find has an optional second parameter specifying the start where to > search: > > Given the above string as str, you can do: > > start_pos = str.find('*') > sql = None > if start_pos != -1: > end_pos = str.find('\0', start_pos) > if end_pos == -1: > end_pos = len(str) > sql = str[start_pos:end_pos] > > Andreas Thanks I got it done just as you explained. What was interesting was my editor showed '\x000' and not '\000' so I was always looking for the wrong thing. I thought I was doing it wrong. Then while re-reading the message I sent to this list I noticed the lack of the 'x'. And sure enough that was the problem. Thanks for your help. -- John Fabiani From andreas at kostyrka.org Mon Dec 15 23:04:11 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Mon, 15 Dec 2008 23:04:11 +0100 Subject: [Tutor] need to clean a string In-Reply-To: <200812151243.46677.jfabiani@yolo.com> References: <200812151018.28151.jfabiani@yolo.com> <20081215203112.62e7b58b@andi-lap> <200812151243.46677.jfabiani@yolo.com> Message-ID: <20081215230411.67caa615@andi-lap> Am Mon, 15 Dec 2008 12:43:46 -0800 schrieb johnf : > On Monday 15 December 2008 11:31:12 am Andreas Kostyrka wrote: > > Am Mon, 15 Dec 2008 10:18:28 -0800 > > > > schrieb johnf : > > > Hi, > > > I have a string that I need to extract just the sql > > > statement. The following is the string > > > \000\000\000\000\000\000\000\000.\000\000\000\0000\000\000\000 > > > > > > \000$\000\000\000\000\000(\000\000\000\000X\000\000\000\000\000, > > > \000\000\000\000P\000\000\000\000\000Q\000\000\000\000\000R\000 > > > \000\000\000\000/\000\000\000? \000\000\000\000%???? \000\000\000 > > > \000'????\000\000\000\000+brttyp\000?\000\000\000\000*SELECT > > > Brttyp.ctrscode, Brttyp.cdescript, Brttyp.ctrstype, > > > Brttyp.cstatus, Brttyp.lreqno, Brttyp.dcreate FROM brttyp Brttyp > > > WHERE Brttyp.ctrscode = ( ?gcKey1 )\000\000\000\000\000 > > > AMCONNECT\000\000\000\000\000) \000\000\000\000 \000\000\000 > > > \000\000\000\000BR Transaction Code File\000 > > > > > > How can I get rid of all the junk? > > > > > > I can find the '*' with str.find('*') but I can't use find '\000' > > > to get to the end? > > > > str.find has an optional second parameter specifying the start > > where to search: > > > > Given the above string as str, you can do: > > > > start_pos = str.find('*') > > sql = None > > if start_pos != -1: > > end_pos = str.find('\0', start_pos) > > if end_pos == -1: > > end_pos = len(str) > > sql = str[start_pos:end_pos] > > > > Andreas > > Thanks I got it done just as you explained. What was interesting was > my editor showed '\x000' and not '\000' so I was always looking for > the wrong thing. I thought I was doing it wrong. Then while > re-reading the message I sent to this list I noticed the lack of the > 'x'. And sure enough that was the problem. Thanks for your help. Well \xHH is the hexadecimal notation, while \OOO is the octal notation. The problem is that \x000 means the 00 byte followed the character 0. Andreas > > > From technorapture at gmail.com Mon Dec 15 23:38:51 2008 From: technorapture at gmail.com (Shrutarshi Basu) Date: Mon, 15 Dec 2008 17:38:51 -0500 Subject: [Tutor] Reading module to import from a string Message-ID: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> Suppose I have a module that I want to import called ImMod1 that's saved in a variable like so: var = "ImMod1" Is there some way to import ImMod1 by using var? Thanks, Basu -- The ByteBaker : http://www.bytebaker.com From cbc at unc.edu Mon Dec 15 23:49:03 2008 From: cbc at unc.edu (Chris Calloway) Date: Mon, 15 Dec 2008 17:49:03 -0500 Subject: [Tutor] Reading module to import from a string In-Reply-To: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> References: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> Message-ID: <4946DEDF.7090503@unc.edu> On 12/15/2008 5:38 PM, Shrutarshi Basu wrote: > Suppose I have a module that I want to import called ImMod1 that's > saved in a variable like so: > > var = "ImMod1" > > Is there some way to import ImMod1 by using var? http://stackoverflow.com/questions/67631/how-to-import-module-from-file-name -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From cbc at unc.edu Mon Dec 15 23:50:50 2008 From: cbc at unc.edu (Chris Calloway) Date: Mon, 15 Dec 2008 17:50:50 -0500 Subject: [Tutor] Reading module to import from a string In-Reply-To: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> References: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> Message-ID: <4946DF4A.9040802@unc.edu> On 12/15/2008 5:38 PM, Shrutarshi Basu wrote: > Suppose I have a module that I want to import called ImMod1 that's > saved in a variable like so: > > var = "ImMod1" > > Is there some way to import ImMod1 by using var? http://docs.python.org/library/imp.html -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From s4027340 at student.uq.edu.au Tue Dec 16 01:38:48 2008 From: s4027340 at student.uq.edu.au (Mr Gerard Kelly) Date: Tue, 16 Dec 2008 10:38:48 +1000 Subject: [Tutor] numarray issues Message-ID: <1b9761ca94.1ca941b976@uq.edu.au> Hello, I am a python beginner, and I have a question about scalars vs arrays. I am making a (very simple) code, and part of it is: from numarray import * from math import * def source(x,y): f=sin(x) return f a=4. m=10 dx=a/(m+1.) x=zeros([m,1], Float) print x[0] print func(x[0]) When I run this code, I get: [ 0.] 0.0 However, if I change the line f=sin(x) to f=2*x, I will get: [ 0.] [ 0.] Why does func(x[0]) return a scalar for sin(x), but returns an array for 2*x? If I want a scalar returned for f=2*x, I need to put in func(x[0,0]). Why do x[0] and x[0,0] both work for sin(x), but only x[0,0] will work for 2*x? From cbc at unc.edu Mon Dec 15 22:22:16 2008 From: cbc at unc.edu (Chris Calloway) Date: Mon, 15 Dec 2008 16:22:16 -0500 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: <4946CA88.5070903@unc.edu> On 12/15/2008 3:42 PM, Marc Tompkins wrote: > If you're just starting out in Python, decorators can be hard to get > your head around... This would be a huge help: http://www.ddj.com/web-development/184406073 -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From alan.gauld at btinternet.com Tue Dec 16 01:58:56 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Dec 2008 00:58:56 -0000 Subject: [Tutor] Creating IDLE Shortcut References: <6640dc74df37.49464039@wright.edu> Message-ID: "Bradford Fisher" wrote > I've attempted creating a shortcut and linking it to pythonw.exe The one the installer created on my PC is a shortcut to: C:\Python24\Tools\scripts\idle.pyw with a start-in directory of C:\Python24\Tools\scripts HTH, Alan G. From alan.gauld at btinternet.com Tue Dec 16 02:03:55 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Dec 2008 01:03:55 -0000 Subject: [Tutor] what does the "@" operator mean? References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: "Marc Tompkins" wrote > If you're just starting out in Python, decorators can be hard to get > your head around... I've been using Python for oover 10 years and still find decorators hard to get my head around! :-) I confess I'm not a fan, they go against the Python spirit of explicit is best in my opinion. If I'm calling a function I like to know I'm calling a function... I know they make the code look pretty but IMHO they are a pain to debug and I'm never totally convinced I've got it exactly right. Alan G From bgailer at gmail.com Tue Dec 16 02:19:34 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 15 Dec 2008 20:19:34 -0500 Subject: [Tutor] numarray issues In-Reply-To: <1b9761ca94.1ca941b976@uq.edu.au> References: <1b9761ca94.1ca941b976@uq.edu.au> Message-ID: <49470226.4000303@gmail.com> Mr Gerard Kelly wrote: > Hello, I am a python beginner, and I have a question about scalars vs > arrays. > > I am making a (very simple) code, and part of it is: > > from numarray import * > from math import * > > def source(x,y): > f=sin(x) > return f > > a=4. > > m=10 > > dx=a/(m+1.) > > x=zeros([m,1], Float) > > print x[0] > > print func(x[0]) > > When I run this code, I get: > > [ 0.] > 0.0 > > However, if I change the line f=sin(x) to f=2*x, I will get: > > [ 0.] > [ 0.] > > Why does func(x[0]) return a scalar for sin(x), but returns an array for > 2*x? > > If I want a scalar returned for f=2*x, I need to put in func(x[0,0]). > Why do x[0] and x[0,0] both work for sin(x), but only x[0,0] will work > for 2*x? > sin() expects a scalar value. x[0] is a 1 element array. numarray magically converts the 1 element array to a scalar. x[0]*2 multiplies each element of the array by 2 giving an array result. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Tue Dec 16 02:49:04 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 15 Dec 2008 20:49:04 -0500 Subject: [Tutor] numarray issues In-Reply-To: <1f50f22726.227261f50f@uq.edu.au> References: <1f50f22726.227261f50f@uq.edu.au> Message-ID: <49470910.30509@gmail.com> An HTML attachment was scrubbed... URL: From mlknack at gmail.com Tue Dec 16 03:15:57 2008 From: mlknack at gmail.com (Mary Lou Knack) Date: Mon, 15 Dec 2008 19:15:57 -0700 Subject: [Tutor] what does the "@" operator mean? References: <221405905FE34478B30A32066A739A26@MLKsHP><40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> <4946CA88.5070903@unc.edu> Message-ID: Thanks Chris & Marc I certainly don't need to know about decorators at this stage of my development. Maybe I'll be able to avoid them entirely.... Mary Lou ----- Original Message ----- From: "Chris Calloway" To: Sent: Monday, December 15, 2008 2:22 PM Subject: Re: [Tutor] what does the "@" operator mean? > On 12/15/2008 3:42 PM, Marc Tompkins wrote: > > If you're just starting out in Python, decorators can be hard to get > > your head around... > > This would be a huge help: > > http://www.ddj.com/web-development/184406073 > > -- > Sincerely, > > Chris Calloway > http://www.secoora.org > office: 332 Chapman Hall phone: (919) 599-3530 > mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From marc.tompkins at gmail.com Tue Dec 16 03:33:01 2008 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Mon, 15 Dec 2008 18:33:01 -0800 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld wrote: > > "Marc Tompkins" wrote > >> If you're just starting out in Python, decorators can be hard to get >> your head around... > > I've been using Python for oover 10 years and still find decorators > hard to get my head around! :-) > > I confess I'm not a fan, they go against the Python spirit of > explicit is best in my opinion. If I'm calling a function I like to > know I'm calling a function... I know they make the code look > pretty but IMHO they are a pain to debug and I'm never totally > convinced I've got it exactly right. > > Alan G > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > I'm sorry I left it as flat as I did - that Dr. Dobbs article is a pretty good explanation, and there's an article somewhere in the Effbot bookshelf that does a decent job - but I couldn't find either one at that moment. I've only been using Python for a couple of years now, but my experience so far is the same as yours: decorators make my head hurt. Even in the Dr. Dobbs article, the examples seem horribly contrived. There must be plenty of programmers who use decorators all the time and would feel lost without them, but I have yet to see a compelling use case. (It's always something like "Here's another useless, made-up situation. We could handle it in a straightforward way, but wouldn't it be cooler if we used a decorator instead?") Actually, I'm being unnecessarily harsh: I can imagine a theoretical case, where there is some operation you wish to apply to several functions, and you don't want to write the code more than once. (Wrapping a timer around functions comes to mind.) But I've never run across a situation where a decorator actually seemed like the best way to do it, and (almost) all of the articles on the subject feel like they were written to check an action item off the editor's to-do list: this book won't be complete unless we mention decorators, so better slop something together. Does anybody who reads this list use decorators and have a nice word to say about them? I'd be interested to hear it. -- www.fsrtechnologies.com p.s. - Is anybody else as sick of the phrase "syntactic sugar" as I am? From steve at alchemy.com Tue Dec 16 03:50:17 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 15 Dec 2008 18:50:17 -0800 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> Message-ID: <49471769.3090604@alchemy.com> Marc Tompkins wrote: > On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld wrote: >> I've been using Python for oover 10 years and still find decorators >> hard to get my head around! :-) > I've only been using Python for a couple of years now, but my > experience so far is the same as yours: decorators make my head hurt. Interesting. I actually rather like them and find them to be a tool to clean up code quite a bit. Python's ability to have classes offer "attributes" which are really handled by wrapper functions is a similar animal (in the sense that they can be very nice but also could be horribly abused to make smelly code). That said, I don't find myself using them too often. Sort of like a special seasoning you add to the occasional dish but exotic enough to taste weird if you used it all the time. For example, creating web apps in TurboGears uses decorators to expose methods, apply error handling and output templates without unnecessarily cluttering all your method definitions. From tesujicamera at gmail.com Tue Dec 16 05:01:12 2008 From: tesujicamera at gmail.com (Lee Meredith) Date: Mon, 15 Dec 2008 23:01:12 -0500 Subject: [Tutor] problems finding position in list and changing it for GO game Message-ID: <913d9be80812152001k6bcd48d9id8990ad97db99185@mail.gmail.com> Hi I am looking for some help if you have or worked with pygame or not I'm sure if you have not you could still help me I am working on a *GO game* no AI a analog behavior only I have the stones going to the board and switching colors between black and white but I want to take *stones/pieces* off and count them and if you don't play go it's okay as well but might help the MOUSEBUTTONDOWN gives the black0 XandYpositions with the*.append* how do I reference the address in the list by using XandYpositions Then replace them or take it out off the list This code puts out an error when you hit the letter "b" Traceback (most recent call last): File "C:/Users/Lee/Desktop/pyGame01/GO/GO_1_2_3.py", line 46, in for stone in len(black0): TypeError: 'int' object is not iterable Which I'm not really sure what that means or how to remedy it as well Thank you everyone ## geting there black and white stones alternate now the issue ## of taking the stones off the board ## Or more precisely to .insert into the list at an address that I detected by finding the X. and the Y. by using the ## if event.pos == range( black0[stone][0] - (stoneWidth/2), black0[stone][0] + ((stoneWidth/2)+1)): import pygame from pygame.locals import * from sys import exit pygame.init() screen = pygame.display.set_mode((758, 758), 0, 32) ##Color color0 = (0,255) b = 0 white0 = [] #white0 black0 = [] #black0 stoneWidth = 20 while True: pressed_keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == QUIT: exit() if event.type == MOUSEBUTTONDOWN:##makes a variable out of the X. and Y. coordinates if b==0: b=1 black0.append(event.pos)##black0.append [X. and Y.] black1.append(event.pos) print "black points1" print black0 print else: b=0 white0.append(event.pos) white1.append(event.pos) print "white points" print white0 print if event.type == KEYDOWN: if event.key == K_b: print "cut black" for stone in len(black0):##I'm not sure here either is this the way I would look for detection of mouse in the range Circle if event.pos == range( black0[stone][0] - (stoneWidth/2), black0[stone][0] + ((stoneWidth/2)+1)): ## this is where I get confused what should I do <<<<<<< ## black0.insert(event.pos,0) if event.pos == range( black0[stone][1] - (stoneWidth/2), black0[stone][1] + ((stoneWidth/2)+1)): ## this is where I get confused what should I do <<<<<<< ##black0.insert(event.pos,1) screen.fill((229,181,83)) screen.lock() for white in white0: #you're drawing pygame.draw.circle(screen, (color0[1],color0[1],color0[1]), white, 20) for black in black0: pygame.draw.circle(screen, (color0[0],color0[0],color0[0]), black, 20) screen.unlock() pygame.display.update() -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Tue Dec 16 05:45:48 2008 From: srilyk at gmail.com (W W) Date: Mon, 15 Dec 2008 22:45:48 -0600 Subject: [Tutor] problems finding position in list and changing it for GO game In-Reply-To: <913d9be80812152001k6bcd48d9id8990ad97db99185@mail.gmail.com> References: <913d9be80812152001k6bcd48d9id8990ad97db99185@mail.gmail.com> Message-ID: <333efb450812152045h469d7f29p1c4db4a40a2aae0c@mail.gmail.com> On Mon, Dec 15, 2008 at 10:01 PM, Lee Meredith wrote: > > the MOUSEBUTTONDOWN gives the black0 XandYpositions with the*.append* > how do I reference the address in the list by using XandYpositions > Then replace them or take it out off the list > > This code puts out an error when you hit the letter "b" This error is pretty verbose, as most python errors are: > Traceback (most recent call last): > File "C:/Users/Lee/Desktop/pyGame01/GO/GO_1_2_3.py", line 46, in This line tells you that the offending statement, the one that "broke" your program/contained a bug is on line 46. Usually this is a pretty good place to start looking. > for stone in len(black0): That tells you the statement that broke > TypeError: 'int' object is not iterable This line tells you /why/ it broke, which is usually the most important bit, and this one gives you some good information. First off, it tells you the type of error, in this case TypeError. AFAIK, this means you tried to do something with a type that isn't allowed. In this case you tried to iterate over an integer. This doesn't work. Would you expect this to work? for num in 7: Hopefully your experience with python will tell you that it would be silly to think of such a thing.However, if you were to say: for num in range(0, 7): - that would be a little more sane. In this case, you are performing something similar to the prior example: you're trying to iterate over a single integer. len() returns the length of the list as an integer value. If you look at some of your other statements you have "for white in white0:" - a list is iterable, and white0 is a list. I hope this helps, Wayne > > > Which I'm not really sure what that means or how to remedy it as well > > Thank you everyone > > ## geting there black and white stones alternate now the issue > ## of taking the stones off the board > ## Or more precisely to .insert into the list at an address that I detected > by finding the X. and the Y. by using the > ## if event.pos == range( black0[stone][0] - (stoneWidth/2), > black0[stone][0] + ((stoneWidth/2)+1)): > import pygame > from pygame.locals import * > from sys import exit > pygame.init() > screen = pygame.display.set_mode((758, 758), 0, 32) > ##Color > color0 = (0,255) > > b = 0 > > white0 = [] #white0 > black0 = [] #black0 > > stoneWidth = 20 > > while True: > pressed_keys = pygame.key.get_pressed() > for event in pygame.event.get(): > if event.type == QUIT: > exit() > if event.type == MOUSEBUTTONDOWN:##makes a variable out of the X. > and Y. coordinates > if b==0: > b=1 > black0.append(event.pos)##black0.append [X. and Y.] > black1.append(event.pos) > print "black points1" > print black0 > print > else: > b=0 > white0.append(event.pos) > white1.append(event.pos) > print "white points" > print white0 > print > if event.type == KEYDOWN: > if event.key == K_b: > print "cut black" > for stone in len(black0):##I'm not sure here either is > this the way I would look for detection of mouse in the range > Circle if event.pos == range( black0[stone][0] - > (stoneWidth/2), black0[stone][0] + ((stoneWidth/2)+1)): > ## this is where I get confused what should I do > <<<<<<< > ## black0.insert(event.pos,0) > if event.pos == range( black0[stone][1] - > (stoneWidth/2), black0[stone][1] + ((stoneWidth/2)+1)): > ## this is where I get confused what should I do > <<<<<<< > ##black0.insert(event.pos,1) > > screen.fill((229,181,83)) > screen.lock() > for white in white0: #you're drawing > pygame.draw.circle(screen, (color0[1],color0[1],color0[1]), white, > 20) > for black in black0: > pygame.draw.circle(screen, (color0[0],color0[0],color0[0]), black, > 20) > screen.unlock() > pygame.display.update() > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- 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 tmz at pobox.com Tue Dec 16 05:50:37 2008 From: tmz at pobox.com (Todd Zullinger) Date: Mon, 15 Dec 2008 23:50:37 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: <20980334.post@talk.nabble.com> References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> <20980334.post@talk.nabble.com> Message-ID: <20081216045037.GA12325@inocybe.teonanacatl.org> Gareth at Serif wrote: > Okay, I've moved over to eyeD3.py... fif nothing else, the > documentation for it's usage is far superior. > > However, I can't get anything to run. The help gives examples of > some simple tasks, but as soon as I 'import eyeD3', not even calling > any functions within it, I start getting errors such as: > > In eyeD3.py, the 'from eyeD3.tag import *;' line (and similar) > didn't work until I removed the 'eyeD3.' part and just imported > 'tag'. How have you installed eyeD3? Perhaps there's something wrong with the installation. Certainly, 'import eyeD3' should not create any errors. > Then in tag.py, some of the class defs complained about undefined > variables, for example 'def link(self, f, v = ID3_ANY_VERSION)' > needed to have quotes around 'ID3_ANY_VERSION'. That's not what you want to do. ID3_ANY_VERSION is defined in eyeD3/__init__.py. If you quote it, you're breaking it. > I'm baffled that I'm having to jump through so many hoops because I > imported eyeD3... is this typical? What have I done wrong? That's hard to guess at. If you can explain what you have done and how you've installed eyeD3, that would help. -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Don't look for me in daylight where robots all assemble. You'll find me in my dark world, in my smoke-filled temple. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From xboxmuncher at gmail.com Tue Dec 16 07:57:41 2008 From: xboxmuncher at gmail.com (xbmuncher) Date: Tue, 16 Dec 2008 01:57:41 -0500 Subject: [Tutor] listen in on other program's tcp connections Message-ID: On windows XP, I'm running a program that sends TCP connections on port 5039. I'v ran wireshark to determine this. I want to create a simple program that listens for these connections and intercepts and then turns the data transferred into a string. From there I'd obviously like my program to act and manipulate those strings, but for now just spitting out that intercepted TCP data is good enough. I was reading up on Twisted. http://twistedmatrix.com/ Can someone get me started on how to do this with twisted framework or anything else? -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Tue Dec 16 09:55:55 2008 From: srilyk at gmail.com (W W) Date: Tue, 16 Dec 2008 02:55:55 -0600 Subject: [Tutor] listen in on other program's tcp connections In-Reply-To: References: Message-ID: <333efb450812160055w3cf9a93m764a830037ca664a@mail.gmail.com> On Tue, Dec 16, 2008 at 12:57 AM, xbmuncher wrote: > On windows XP, I'm running a program that sends TCP connections on port > 5039. I'v ran wireshark to determine this. I want to create a simple program > that listens for these connections and intercepts and then turns the data > transferred into a string. From there I'd obviously like my program to act > and manipulate those strings, but for now just spitting out that intercepted > TCP data is good enough. > > I was reading up on Twisted. http://twistedmatrix.com/ > Can someone get me started on how to do this with twisted framework or > anything else? I've never done anything like this specifically... but my guess is that it's pretty similar to creating a proxy. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Tue Dec 16 10:20:29 2008 From: srilyk at gmail.com (W W) Date: Tue, 16 Dec 2008 03:20:29 -0600 Subject: [Tutor] Having Issues with CMD and the 'python' command In-Reply-To: References: Message-ID: <333efb450812160120g57f18631hf6c7149dc307d3@mail.gmail.com> On Mon, Dec 15, 2008 at 9:56 AM, Benjamin Kaplan wrote: > It's not a question of sensibility. It's a question of purpose. The Zen is > the philosophy of a language that tries to be easy to learn and easy to use. > Python is used by programmers who want to experiment with it, but who > usually know enough not to os.system("rm -r /") or anything similar. > Windows, on the other hand, wants to hide everything that can potentially > ruin the system as deep as possible so that many of the idiots who use that > system don't do stupid things like delete the registry, wipe the environment > settings, turn off the "Nag Screen" (UAC), and other things of that nature > But if it were sensible, it would (like certain other OS's) make it more difficult to have built-in permissions that allow you to totally hose the system, i.e. it would be secure.While it may not technically be using the same comparison criteria we do, Python still gives you the proper result: In [5]: 'obfuscation' is not 'security' and 'obfuscation' != 'security' Out[5]: True I think that's an important concept for programmers to realize. Allowing people to break things if they fool around enough is not a very sensible, or secure way of running things. And if you're writing OSes, the sensible thing is to write secure code. And if you're writing programs for general consumption, it's /also/ best to write secure code. That's my 2c anyways :) -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From phpfood at gmail.com Tue Dec 16 07:55:51 2008 From: phpfood at gmail.com (phpfood) Date: Tue, 16 Dec 2008 01:55:51 -0500 Subject: [Tutor] listen in on other program's tcp connections Message-ID: On windows XP, I'm running a program that sends TCP connections on port 5039. I'v ran wireshark to determine this. I want to create a simple program that listens for these connections and intercepts and then turns the data transferred into a string. From there I'd obviously like my program to act and manipulate those strings, but for now just spitting out that intercepted TCP data is good enough. I was reading up on Twisted. http://twistedmatrix.com/ Can someone get me started on how to do this with twisted framework or anything else? -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 16 14:49:52 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 16 Dec 2008 08:49:52 -0500 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> Message-ID: <1c2a2c590812160549t3a19adc7p727e58f1400cd02b@mail.gmail.com> On Mon, Dec 15, 2008 at 9:33 PM, Marc Tompkins wrote: > I'm sorry I left it as flat as I did - that Dr. Dobbs article is a > pretty good explanation, and there's an article somewhere in the > Effbot bookshelf that does a decent job - but I couldn't find either > one at that moment. FWIW my attempt at explaining decorators is here: http://personalpages.tds.net/~kent37/kk/00001.html > I've only been using Python for a couple of years now, but my > experience so far is the same as yours: decorators make my head hurt. > Even in the Dr. Dobbs article, the examples seem horribly contrived. It's hard to find a simple example of decorators that is not contrived. The real-world cases are often too complex for an introduction. I cite a few examples in my article. Some other examples: Django's login_required decorator: http://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator In the standard lib, contextlib.contextmanager makes it easy to define context managers (objects that work with the 'with' statement): http://docs.python.org/library/contextlib.html#contextlib.contextmanager and functools.wraps helps to make well-behaved decorators ;-) http://docs.python.org/library/functools.html#functools.wraps I told you it was hard to find simple examples!) Kent From metolone+gmane at gmail.com Tue Dec 16 16:39:57 2008 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Tue, 16 Dec 2008 07:39:57 -0800 Subject: [Tutor] Reading module to import from a string References: <376fbdcf0812151438w53c8f3f7rc1dc481b524282bc@mail.gmail.com> Message-ID: "Shrutarshi Basu" wrote in message news:376fbdcf0812151438w53c8f3f7rc1dc481b524282bc at mail.gmail.com... > Suppose I have a module that I want to import called ImMod1 that's > saved in a variable like so: > > var = "ImMod1" > > Is there some way to import ImMod1 by using var? > Thanks, > Basu mod = __import__(var) -Mark From iaidas4 at gmail.com Tue Dec 16 16:53:06 2008 From: iaidas4 at gmail.com (i i) Date: Tue, 16 Dec 2008 20:53:06 +0500 Subject: [Tutor] (no subject) Message-ID: <6fb034600812160753s4c1835d6o86f24c22094fb214@mail.gmail.com> Hi, Is their any site where i can find out about custom widget, GtkHTML, python gtkHtml. thanx a lot -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue Dec 16 17:16:46 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 16 Dec 2008 16:16:46 +0000 (UTC) Subject: [Tutor] listen in on other program's tcp connections References: <333efb450812160055w3cf9a93m764a830037ca664a@mail.gmail.com> Message-ID: On Tue, 16 Dec 2008 02:55:55 -0600, W W wrote: > On Tue, Dec 16, 2008 at 12:57 AM, xbmuncher > wrote: > >> On windows XP, I'm running a program that sends TCP connections on port >> 5039. I'v ran wireshark to determine this. I want to create a simple >> program that listens for these connections and intercepts and then >> turns the data transferred into a string. From there I'd obviously like >> my program to act and manipulate those strings, but for now just >> spitting out that intercepted TCP data is good enough. >> >> I was reading up on Twisted. http://twistedmatrix.com/ Can someone get >> me started on how to do this with twisted framework or anything else? > > > I've never done anything like this specifically... but my guess is that > it's pretty similar to creating a proxy. In the case of proxy, I think the program have to voluntarily send the data to the proxy[1]. I think you've got to go round your head around wireshark. I don't know wireshark enough to know whether it is possible for wireshark to intercept the packet and send it to an external program. [1] or if you're on windows, you could hack the HOSTS file so data sent by the program would instead be redirected to 127.0.0.1 (i.e. localhost, i.e. your own computer) From dineshbvadhia at hotmail.com Tue Dec 16 17:29:48 2008 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 16 Dec 2008 08:29:48 -0800 Subject: [Tutor] Fw: Reading gzip files Message-ID: Hi! I sent the note below earlier and thank-you for the various responses. The program reads a bunch of gzip files, writes the content out to a text file line by line (see code below). What I'd like to do is ... when an "IOError: CRC check failed" error happens then close the offending file and move on to the next file in the list. How do I achieve this with this particular type of error? Dinesh From: Dinesh B Vadhia Sent: Sunday, November 30, 2008 2:51 PM To: tutor at python.org Subject: Reading gzip files I'm reading gzip files and writing the content out to a text file line by line. The code is simply: import gzip list_zipfiles = dircache.listdir(zipfolder) writefile = "out_file.txt" fw = open(writefile, 'w') for ziparchive in list_zipfiles: zfile = gzip.GzipFile(zipfolder + ziparchive, "r") for line in zfile: fw.write(line) zfile.close() fw.close() The Traceback is: Traceback (most recent call last): File "....py", line 47, in for line in zfile: File "C:\Python25\lib\gzip.py", line 444, in next line = self.readline() File "C:\Python25\lib\gzip.py", line 399, in readline c = self.read(readsize) File "C:\Python25\lib\gzip.py", line 227, in read self._read(readsize) File "C:\Python25\lib\gzip.py", line 275, in _read self._read_eof() File "C:\Python25\lib\gzip.py", line 311, in _read_eof raise IOError, "CRC check failed" IOError: CRC check failed I've checked the Python docs and online but cannot find a solution to the problem. Thanks. Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 16 17:34:22 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 16 Dec 2008 11:34:22 -0500 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <1c2a2c590812160549t3a19adc7p727e58f1400cd02b@mail.gmail.com> References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> <1c2a2c590812160549t3a19adc7p727e58f1400cd02b@mail.gmail.com> Message-ID: <1c2a2c590812160834i17b22a7focabd7a82b849f23@mail.gmail.com> Here is an interesting example of a decorator that munges the bytecodes of a function to optimize it: http://code.activestate.com/recipes/277940/ Kent From mwalsh at mwalsh.org Tue Dec 16 17:34:47 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 16 Dec 2008 10:34:47 -0600 Subject: [Tutor] listen in on other program's tcp connections In-Reply-To: References: Message-ID: <4947D8A7.8080502@mwalsh.org> phpfood wrote: > On windows XP, I'm running a program that sends TCP connections on port > 5039. I'v ran wireshark to determine this. I want to create a simple > program that listens for these connections and intercepts and then turns > the data transferred into a string. From there I'd obviously like my > program to act and manipulate those strings, but for now just spitting > out that intercepted TCP data is good enough. IIRC, wireshark uses libpcap for which there are no fewer than two python extension modules (though, I have not used either): http://pylibpcap.sourceforge.net/ http://code.google.com/p/pypcap/ Less like a socket proxy, more like packet sniffing. > > I was reading up on Twisted. http://twistedmatrix.com/ > Can someone get me started on how to do this with twisted framework or > anything else? From ptmcg at austin.rr.com Tue Dec 16 17:48:18 2008 From: ptmcg at austin.rr.com (Paul McGuire) Date: Tue, 16 Dec 2008 10:48:18 -0600 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: References: Message-ID: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> The Python Wiki has some example decorators at http://wiki.python.org/moin/PythonDecoratorLibrary. I think the CGIMethod wrapper is a good intuitive example, and memoize is a good technique to add to your Python toolkit. -- Paul From kent37 at tds.net Tue Dec 16 18:06:25 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 16 Dec 2008 12:06:25 -0500 Subject: [Tutor] Fw: Reading gzip files In-Reply-To: References: Message-ID: <1c2a2c590812160906q6e8c652dpbd428ec14be10505@mail.gmail.com> On Tue, Dec 16, 2008 at 11:29 AM, Dinesh B Vadhia wrote: > What I'd like to do is ... when an "IOError: CRC check failed" error happens > then close the offending file and move on to the next file in the list. How > do I achieve this with this particular type of error? You can catch IOError specifically or you can catch all exceptions using try / except / finally. To trap all exceptions and continue with the next file: for ziparchive in list_zipfiles: zfile = gzip.GzipFile(zipfolder + ziparchive, "r") try: for line in zfile: fw.write(line) except: pass finally: zfile.close() fw.close() Kent From marc.tompkins at gmail.com Tue Dec 16 18:11:53 2008 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 16 Dec 2008 09:11:53 -0800 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> References: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> Message-ID: <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> OK, having looked at http://wiki.python.org/moin/PythonDecoratorLibrary again with a less jaundiced eye, I believe my code may soon begin to contain a few of the dreaded sleepycats... By the way, (totally off-topic, of course, my apologies): what do all y'all call the "@" operator? Here in the States, we call it the "at-sign", which I find boring; I believe "sleepycat" is a Scandinavian thing (I picked it up in some long-forgotten article); some Russians refer to it as ?????? ("sobaka", dog) - again because it looks like an animal with its tail curled up. It occurs to me that Pythonistas should have their own name for the thing. I propose "ourobouros" - the snake eating its own tail. Of course, technically that's a capital O, but hey. -- www.fsrtechnologies.com From alan.gauld at btinternet.com Tue Dec 16 18:54:05 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Dec 2008 17:54:05 -0000 Subject: [Tutor] what does the "@" operator mean? References: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> Message-ID: "Marc Tompkins" wrote > y'all call the "@" operator? Here in the States, we call it the > "at-sign", Thats what I've always heard in the UK too. Alan G. From alan.gauld at btinternet.com Tue Dec 16 18:56:43 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Dec 2008 17:56:43 -0000 Subject: [Tutor] GtkHTML References: <6fb034600812160753s4c1835d6o86f24c22094fb214@mail.gmail.com> Message-ID: "i i" wrote in message news:6fb034600812160753s4c1835d6o86f24c22094fb214 at mail.gmail.com... > Hi, > Is their any site where i can find out about custom widget, > GtkHTML, > python gtkHtml. A google search brought up this.... http://www.fcoutant.freesurf.fr/python-gtkhtml.html Now what do you actually want to know about it? Alan G From gregor.lingl at aon.at Tue Dec 16 19:19:43 2008 From: gregor.lingl at aon.at (Gregor Lingl) Date: Tue, 16 Dec 2008 19:19:43 +0100 Subject: [Tutor] TOT: what does the "@" operator mean? In-Reply-To: <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> References: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> Message-ID: <4947F13F.9090104@aon.at> Marc Tompkins schrieb: > By the way, (totally off-topic, of course, my apologies): what do all > y'all call the "@" operator? Here in the States, we call it the > "at-sign", which I find boring; I believe "sleepycat" is a > Scandinavian thing (I picked it up in some long-forgotten article); > some Russians refer to it as ?????? ("sobaka", dog) - again because it > looks like an animal with its tail curled up. > > In German we call it 'Klammeraffe' - which means 'spider monkey': http://en.wikipedia.org/wiki/Spider_monkey Don't know why, perhaps also because of it's long tail Regards, Gregor From saluk64007 at gmail.com Tue Dec 16 19:36:07 2008 From: saluk64007 at gmail.com (Patrick Mullen) Date: Tue, 16 Dec 2008 10:36:07 -0800 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: On Mon, Dec 15, 2008 at 5:03 PM, Alan Gauld wrote: > > "Marc Tompkins" wrote > >> If you're just starting out in Python, decorators can be hard to get >> your head around... > > I've been using Python for oover 10 years and still find decorators > hard to get my head around! :-) > > I confess I'm not a fan, they go against the Python spirit of > explicit is best in my opinion. If I'm calling a function I like to > know I'm calling a function... I know they make the code look > pretty but IMHO they are a pain to debug and I'm never totally > convinced I've got it exactly right. I thought this way for a while, but I "get" them now. Mostly I use them as a shortcut to setting some variables on a function, which could later be used for introspection. For instance, a dead simple test framework I wrote uses decorators like this: @test("Just load something",removefiles=["sprite1.txt"],teardown=sprite_cleanup) def load_sprite(): f = open("sprite1.txt","w") f.write("""texture metatex1.png horizontal 2\nvertical 2\nlength 4\nloops 3""") f.close() The decorator signifies to the testing framework that this function should be run, the file sprite1.txt should be removed after running the test, and the function sprite_cleanup should be run as well. This could have been done in other ways, but it is a lot more clear with a decorator. "Hey testing framework, here is how you should run this function" The actual code for the decorator is not complex either: def test(t,removefiles=[],teardown=None): def dec(f): f.a_test = True f.test_desc = t f.removefiles = removefiles f.teardown = teardown return f return dec The function in a function is a bit headache inducing, I'll grant that. But the dec function just sets some variables on the function. This could be done in the old way: def test(f,t,removefiles=[],teardown=None): f.a_test = True f.test_desc = t With the functions decorated like this: def test_sprites: [code] test(test_sprites,"Just load something",removefiles=["sprite1.txt"],teardown=sprite_cleanup) For me though, it is MUCH better to have this information before the function instead of after. Another place I am using them is in an interpreter class. The class has various methods tied to different commands that it understands. I also have a gui where commands can be chosen, according to the category of the command. The decorator is used to add some information that the gui can use about the nature of the command, and which category it falls under. I don't used any advanced features of decorators at all, they just make the code a little bit more clear and allow me to do a bit more introspection of functions for various purposes. I don't use them often, but I do like them. From sierra_mtnview at sbcglobal.net Tue Dec 16 19:50:30 2008 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 16 Dec 2008 10:50:30 -0800 Subject: [Tutor] WinMerge Message-ID: <4947F876.4090103@sbcglobal.net> An HTML attachment was scrubbed... URL: From ptmcg at austin.rr.com Tue Dec 16 20:24:28 2008 From: ptmcg at austin.rr.com (Paul McGuire) Date: Tue, 16 Dec 2008 13:24:28 -0600 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: References: Message-ID: > By the way, (totally off-topic, of course, my apologies): what do all > y'all call the "@" operator? Back when the "what syntax should we use for decorators?" debate raged, this symbol was referred to as a "pie", I guess because it looks like the swirl on top of a cream pie. I think this term is of Perl/Unix-y origin, similar to "bang"(!), "splat"(*), and "hash"(#). I much prefer the "spidermonkey" or "sleepycat" terms, though. Unfortunately, here in the US, far fewer would understand what I was saying than if I called it the prosaic "at-sign" (so named because on old-style store receipts, one might find "3 apples @ 5 cents", meaning "at the unit price of" or just "at" for short). Use of "dot" and "star" for "." and "*" are fairly commonplace now, in place of their older forms "period" and "asterisk". Did you ever hear anyone say "go to 'www period google period com'"? But it is interesting that the clunky "ampersand" persists. Perhaps as we move forward into the unicode world, we will have to have more names for symbols like "?" and "?". My Windows character map names these (when mousing over them) as the "section sign" and "Pilcrow sign". "@" is referred to here as the "Commercial At", and "#" as the "Number sign", although all of us have probably been instructed by voice mail menus to press the "pound key" when they mean this sign. Much more good info on all this trivia at http://en.wikipedia.org/wiki/Punctuation. -- Paul Mary love to wear her skates/Upon the ice to frisk/Wasn't she a silly girl/Her little * ? From denis.spir at free.fr Tue Dec 16 20:41:38 2008 From: denis.spir at free.fr (spir) Date: Tue, 16 Dec 2008 20:41:38 +0100 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> <40af687b0812151833qe15c6cbu8e344f9acf0ff5dc@mail.gmail.com> Message-ID: <1229456498.7044.1.camel@o> I use decorators only to declare class methods. Denis Le lundi 15 d?cembre 2008 ? 18:33 -0800, Marc Tompkins a ?crit : > Does anybody who reads this list use decorators and have a nice word > to say about them? I'd be interested to hear it. > From denis.spir at free.fr Tue Dec 16 21:06:33 2008 From: denis.spir at free.fr (spir) Date: Tue, 16 Dec 2008 21:06:33 +0100 Subject: [Tutor] reciprocal import Message-ID: <1229457993.7044.10.camel@o> Is it legal or possible at all for two modules to import each other? I tried several ways and had several kinds of error messages. Usually "can't import...". Denis From steve at alchemy.com Tue Dec 16 21:08:29 2008 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 16 Dec 2008 12:08:29 -0800 Subject: [Tutor] reciprocal import In-Reply-To: <1229457993.7044.10.camel@o> References: <1229457993.7044.10.camel@o> Message-ID: <49480ABD.4090500@alchemy.com> spir wrote: > Is it legal or possible at all for two modules to import each other? I > tried several ways and had several kinds of error messages. Usually > "can't import...". My first impression here is that this sounds like a bad class/module design if they're really that interdependent. From kent37 at tds.net Tue Dec 16 21:38:19 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 16 Dec 2008 15:38:19 -0500 Subject: [Tutor] reciprocal import In-Reply-To: <1229457993.7044.10.camel@o> References: <1229457993.7044.10.camel@o> Message-ID: <1c2a2c590812161238r12af7c26i8d4816c85d9dbc49@mail.gmail.com> On Tue, Dec 16, 2008 at 3:06 PM, spir wrote: > Is it legal or possible at all for two modules to import each other? I > tried several ways and had several kinds of error messages. Usually > "can't import...". It is possible but better to avoid it, perhaps by putting common functionality into a third module. The problem is that 'import' is an executable statement and modules are executed when they are imported. If A.py imports B.py and vice-versa, then A is not completely defined when B is loaded, and access to names in A may fail. For example, this will not work: # A.py import B def hello(): print 'hello' # B.py import A A.hello() Running A.py will get a NameError in B because A.hello doesn't exist at the point where B is loaded. If neither A or B tries to access the contents of the other at load time you are probably OK, but still it is a bad design and better to find another solution. Kent From christopher.henk at allisontransmission.com Tue Dec 16 21:42:45 2008 From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com) Date: Tue, 16 Dec 2008 15:42:45 -0500 Subject: [Tutor] reciprocal import In-Reply-To: <49480ABD.4090500@alchemy.com> Message-ID: Although you generally would want to design so that this does not happen, if the import of the first module is only needed in some of the functions in the second module, you can include the import within the function definition and that would work ok. But you can't have the second import at the module level. Chris Steve Willoughby Sent by: tutor-bounces+christopher.henk=allisontransmission.com at python.org 12/16/2008 03:08 PM To spir cc *tutor Subject Re: [Tutor] reciprocal import spir wrote: > Is it legal or possible at all for two modules to import each other? I > tried several ways and had several kinds of error messages. Usually > "can't import...". My first impression here is that this sounds like a bad class/module design if they're really that interdependent. _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjampman at gmail.com Tue Dec 16 22:57:06 2008 From: tjampman at gmail.com (Ole Henning Jensen) Date: Tue, 16 Dec 2008 22:57:06 +0100 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> References: <3C9CBC62A6224175BC6F6C59CB42CA00@AWA2> <40af687b0812160911v36bebe6pe9f6862c90bb0e25@mail.gmail.com> Message-ID: <49482432.3030104@gmail.com> Marc Tompkins wrote: > By the way, (totally off-topic, of course, my apologies): what do all > y'all call the "@" operator? Here in the States, we call it the > "at-sign", which I find boring; I believe "sleepycat" is a > Scandinavian thing (I picked it up in some long-forgotten article) Continuing of the OT lane I'm not sure that "sleepycat" is scandinavien, I'm Danish myself and to my knowledge in both Denmark and Sweden the at-sign is called "snabel-a" which translates to "trunk-a" (a trunk as on an elefant) And in Norwegian I belive to be "kr?lle-alfa" translated means "curly-alpha" From denis.spir at free.fr Tue Dec 16 23:18:25 2008 From: denis.spir at free.fr (spir) Date: Tue, 16 Dec 2008 23:18:25 +0100 Subject: [Tutor] reciprocal import In-Reply-To: <1c2a2c590812161238r12af7c26i8d4816c85d9dbc49@mail.gmail.com> References: <1229457993.7044.10.camel@o> <1c2a2c590812161238r12af7c26i8d4816c85d9dbc49@mail.gmail.com> Message-ID: <1229465905.8493.9.camel@o> Steve & Kent: Actually, I have 2 main modules that work together to achieve the task. In the first one is defined a set of objects that outline the creation of objects which classes & subclasses are in the second module. Only for clarity I need two modules. A third one copes with exceptions -- so it needs to be referenced in both other modules. But, in order to provide the user meaning- and help-ful information, I need to access some elements from the module that launched the exception. Actually, the whole of it forms a unity and can be put in a single file -- but isn't this true for any application? Denis Le mardi 16 d?cembre 2008 ? 15:38 -0500, Kent Johnson a ?crit : > On Tue, Dec 16, 2008 at 3:06 PM, spir wrote: > > Is it legal or possible at all for two modules to import each other? I > > tried several ways and had several kinds of error messages. Usually > > "can't import...". > > It is possible but better to avoid it, perhaps by putting common > functionality into a third module. > > The problem is that 'import' is an executable statement and modules > are executed when they are imported. If A.py imports B.py and > vice-versa, then A is not completely defined when B is loaded, and > access to names in A may fail. For example, this will not work: > > # A.py > import B > > def hello(): > print 'hello' > > # B.py > import A > A.hello() > > > Running A.py will get a NameError in B because A.hello doesn't exist > at the point where B is loaded. > > If neither A or B tries to access the contents of the other at load > time you are probably OK, but still it is a bad design and better to > find another solution. > > Kent > From steve at alchemy.com Tue Dec 16 23:33:39 2008 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 16 Dec 2008 14:33:39 -0800 Subject: [Tutor] reciprocal import In-Reply-To: <1229465905.8493.9.camel@o> References: <1229457993.7044.10.camel@o> <1c2a2c590812161238r12af7c26i8d4816c85d9dbc49@mail.gmail.com> <1229465905.8493.9.camel@o> Message-ID: <49482CC3.1020108@alchemy.com> spir wrote: > Steve & Kent: > Actually, I have 2 main modules that work together to achieve the task. > In the first one is defined a set of objects that outline the creation > of objects which classes & subclasses are in the second module. Only for > clarity I need two modules. In my experience (and I realize the limits of speaking in such generalities here), every time this situation has come up, refactoring the code so this interdependence is resolved has always resulted in a clearer, cleaner design. One module can define a base class, another a derived class (without the need for the base class to ever load or be aware of the derived class at all), for example. From alan.gauld at btinternet.com Wed Dec 17 01:49:47 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Dec 2008 00:49:47 -0000 Subject: [Tutor] reciprocal import References: <1229457993.7044.10.camel@o><1c2a2c590812161238r12af7c26i8d4816c85d9dbc49@mail.gmail.com> <1229465905.8493.9.camel@o> Message-ID: "spir" wrote > Actually, I have 2 main modules that work together to achieve the > task. > In the first one(A) is defined a set of objects that outline the > creation > of objects which classes & subclasses are in the second module(B). So far so good. A needs to import B but B does not need to import A. > A third one(C) copes with exceptions -- so it needs to be referenced > in > both other modules. Again that's OK, both A and B can import C. > But, in order to provide the user meaning- and help-ful information, > I need to access some elements from the module that launched > the exception. And this is where the design starts to smell bad. The exceptions should not need to access the raising class, the raiser should pass information (perhaps the whole object via self) into the exception. But exceptions are usually fairly dumb classes - just transporters of information, it's the handlers where the intelligence should lie. So how is your exception handling working? It should be in the place where the exception is caught. > Actually, the whole of it forms a unity and can be put in a single > file > -- but isn't this true for any application? No, most well designed applications are modular in nature and chunks can be taken out and reused and other chunks from other apps inserted without undue disturbance. But that does require thinking carefully about data flow between modules and the location of responsibilities for processing that data. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Wed Dec 17 01:55:54 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Dec 2008 00:55:54 -0000 Subject: [Tutor] what does the "@" operator mean? References: Message-ID: "Paul McGuire" wrote > and "#" as the "Number sign", although all of us have probably > been instructed by voice mail menus to press the "pound key" > when they mean this sign. In the UK its usually referred to as hash or square. When tone dial phones were first introduced in the UK in the late 70's the official documentation nearly always referred to it as square, but nowadays hash seems much more common. ( I was recently on an IVR where it started out using hash but then switched to square for a few options then back to hash again. I suspect I found a route to a relic from the past :-) Alan G. From srilyk at gmail.com Wed Dec 17 05:54:53 2008 From: srilyk at gmail.com (W W) Date: Tue, 16 Dec 2008 22:54:53 -0600 Subject: [Tutor] WinMerge In-Reply-To: <4947F876.4090103@sbcglobal.net> References: <4947F876.4090103@sbcglobal.net> Message-ID: <333efb450812162054h2c0c73ccod6ae037d936ff639@mail.gmail.com> Perhaps bold/italics/underline/strikethrough? -Wayne On Tue, Dec 16, 2008 at 12:50 PM, Wayne Watson wrote: > Is there a way to highlight differences between the two files when > printing in b/w? Help suggests there may be some texturing, but all I see is > color choices. > -- > > Wayne Watson (Watson Adventures, Prop., Nevada City, CA) > > (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) * "*Nothing bothers me more than greener-than-thou people > who say I don't think we should have nuclear, we should > have solar. The fact is that we should go in all of these > directions, or we are not going to solve the [energy] > problem." -- Dr. Richard Muller, UC Berkeley, physicist > > ** Web Page: > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- 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 d.conca at gmail.com Wed Dec 17 09:27:13 2008 From: d.conca at gmail.com (Daniele) Date: Wed, 17 Dec 2008 09:27:13 +0100 Subject: [Tutor] what does the "@" operator mean? Message-ID: <537341c70812170027s2d7c33b8sd96bfedd12071606@mail.gmail.com> > From: "Alan Gauld" > Subject: Re: [Tutor] what does the "@" operator mean? Thinking it's quite funny, I'll keep on with italian words: the @ is called "chiocciola" which means snail, while # is called "cancelletto" which is a small gate As you see italian words are quite close to the sign shape, like in Denmark i guess (trunk-a for @ is fantastic! :-P ) From sudhir.tact.mca at gmail.com Wed Dec 17 12:59:31 2008 From: sudhir.tact.mca at gmail.com (Lucky Boy Sudhir) Date: Wed, 17 Dec 2008 17:29:31 +0530 Subject: [Tutor] Lucky Boy Sudhir has invited you to open a Google mail account Message-ID: <4c0a2dab0812170359y52e56bf1p@mail.gmail.com> I've been using Gmail and thought you might like to try it out. Here's an invitation to create an account. ----------------------------------------------------------------------- Lucky Boy Sudhir has invited you to open a free Gmail account. To accept this invitation and register for your account, visit http://mail.google.com/mail/a-4696b4693-1006ddb730-25c77422c3 Once you create your account, Lucky Boy Sudhir will be notified with your new email address so you can stay in touch with Gmail! If you haven't already heard about Gmail, it's a new search-based webmail service that offers: - Over 2,700 megabytes (two gigabytes) of free storage - Built-in Google search that instantly finds any message you want - Automatic arrangement of messages and related replies into "conversations" - Powerful spam protection using innovative Google technology - No large, annoying ads--just small text ads and related pages that are relevant to the content of your messages To learn more about Gmail before registering, visit: http://mail.google.com/mail/help/benefits.html And, to see how easy it can be to switch to a new email service, check out our new switch guide: http://mail.google.com/mail/help/switch/ We're still working every day to improve Gmail, so we might ask for your comments and suggestions periodically. We hope you'll like Gmail. We do. And, it's only going to get better. Thanks, The Gmail Team (If clicking the URLs in this message does not work, copy and paste them into the address bar of your browser). From lie.1296 at gmail.com Wed Dec 17 14:39:24 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 17 Dec 2008 13:39:24 +0000 (UTC) Subject: [Tutor] what does the "@" operator mean? References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: On Tue, 16 Dec 2008 01:03:55 +0000, Alan Gauld wrote: > "Marc Tompkins" wrote > >> If you're just starting out in Python, decorators can be hard to get >> your head around... > > I've been using Python for oover 10 years and still find decorators hard > to get my head around! :-) > > I confess I'm not a fan, they go against the Python spirit of explicit > is best in my opinion. If I'm calling a function I like to know I'm > calling a function... I know they make the code look pretty but IMHO > they are a pain to debug and I'm never totally convinced I've got it > exactly right. I usually think about decorator as a tag or marker instead of function calling. When I put the property decorator, I'll be thinking about attaching a property tag to the function so the interpreter would handle the function as if it is a property. This way, I don't think of it as implicit function calling but as an explicit tag (how the tag system works is a detail I need not to care). From sudhir.tact.mca at gmail.com Wed Dec 17 13:01:31 2008 From: sudhir.tact.mca at gmail.com (Lucky Boy Sudhir) Date: Wed, 17 Dec 2008 04:01:31 -0800 Subject: [Tutor] Lucky Boy Sudhir wants to chat Message-ID: <4c0a2dab0812170401ofc80c88x@mail.gmail.com> ----------------------------------------------------------------------- Lucky Boy Sudhir wants to stay in better touch using some of Google's coolest new products. If you already have Gmail or Google Talk, visit: http://mail.google.com/mail/b-4696b4693-d5d69810df-aafd3a6b94976b5f You'll need to click this link to be able to chat with Lucky Boy Sudhir . To get Gmail - a free email account from Google with over 2,800 megabytes of storage - and chat with Lucky Boy Sudhir , visit: http://mail.google.com/mail/a-4696b4693-d5d69810df-ee3cdf55e5 Gmail offers: - Instant messaging right inside Gmail - Powerful spam protection - Built-in search for finding your messages and a helpful way of organizing emails into "conversations" - No pop-up ads or untargeted banners - just text ads and related information that are relevant to the content of your messages All this, and its yours for free. But wait, there's more! By opening a Gmail account, you also get access to Google Talk, Google's instant messaging service: http://www.google.com/talk/ Google Talk offers: - Web-based chat that you can use anywhere, without a download - A contact list that's synchronized with your Gmail account - Free, high quality PC-to-PC voice calls when you download the Google Talk client Gmail and Google Talk are still in beta. We're working hard to add new features and make improvements, so we might also ask for your comments and suggestions periodically. We appreciate your help in making our products even better! Thanks, The Google Team To learn more about Gmail and Google Talk, visit: http://mail.google.com/mail/help/about.html http://www.google.com/talk/about.html (If clicking the URLs in this message does not work, copy and paste them into the address bar of your browser). From kent37 at tds.net Wed Dec 17 15:51:57 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 17 Dec 2008 09:51:57 -0500 Subject: [Tutor] Lucky Boy Sudhir wants to chat In-Reply-To: <4c0a2dab0812170401ofc80c88x@mail.gmail.com> References: <4c0a2dab0812170401ofc80c88x@mail.gmail.com> Message-ID: <1c2a2c590812170651q2466b813waa6b72aa726748e5@mail.gmail.com> I have unsubscribed Lucky Boy from the list as he is just spamming it. Kent On Wed, Dec 17, 2008 at 7:01 AM, Lucky Boy Sudhir wrote: > ----------------------------------------------------------------------- > > Lucky Boy Sudhir wants to stay in better touch using some of Google's > coolest new > products. > > If you already have Gmail or Google Talk, visit: > http://mail.google.com/mail/b-4696b4693-d5d69810df-aafd3a6b94976b5f > You'll need to click this link to be able to chat with Lucky Boy Sudhir . > > To get Gmail - a free email account from Google with over 2,800 megabytes of > storage - and chat with Lucky Boy Sudhir , visit: > http://mail.google.com/mail/a-4696b4693-d5d69810df-ee3cdf55e5 > > Gmail offers: > - Instant messaging right inside Gmail > - Powerful spam protection > - Built-in search for finding your messages and a helpful way of organizing > emails into "conversations" > - No pop-up ads or untargeted banners - just text ads and related information > that are relevant to the content of your messages > > All this, and its yours for free. But wait, there's more! By opening a Gmail > account, you also get access to Google Talk, Google's instant messaging > service: > > http://www.google.com/talk/ > > Google Talk offers: > - Web-based chat that you can use anywhere, without a download > - A contact list that's synchronized with your Gmail account > - Free, high quality PC-to-PC voice calls when you download the Google Talk > client > > Gmail and Google Talk are still in beta. We're working hard to add new features > and make improvements, so we might also ask for your comments and suggestions > periodically. We appreciate your help in making our products even better! > > Thanks, > The Google Team > > To learn more about Gmail and Google Talk, visit: > http://mail.google.com/mail/help/about.html > http://www.google.com/talk/about.html > > (If clicking the URLs in this message does not work, copy and paste them into > the address bar of your browser). > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From wolfram.kraus at fen-net.de Wed Dec 17 16:14:57 2008 From: wolfram.kraus at fen-net.de (Wolfram Kraus) Date: Wed, 17 Dec 2008 16:14:57 +0100 Subject: [Tutor] what does the "@" operator mean? In-Reply-To: References: <221405905FE34478B30A32066A739A26@MLKsHP> <40af687b0812151242w3efaeee4hf8244a4b632af06f@mail.gmail.com> Message-ID: Am 16.12.2008 02:03, Alan Gauld schrieb: > > "Marc Tompkins" wrote > >> If you're just starting out in Python, decorators can be hard to get >> your head around... > > I've been using Python for oover 10 years and still find decorators > hard to get my head around! :-) > > I confess I'm not a fan, they go against the Python spirit of > explicit is best in my opinion. If I'm calling a function I like to > know I'm calling a function... I know they make the code look > pretty but IMHO they are a pain to debug and I'm never totally > convinced I've got it exactly right. > > Alan G Just found this via dzone.org: http://gumuz.nl/weblog/simple-python-decorator-classes/ Very interesting read. HTH, Wolfram From simon_ecc at yahoo.co.uk Wed Dec 17 16:20:17 2008 From: simon_ecc at yahoo.co.uk (ppaarrkk) Date: Wed, 17 Dec 2008 07:20:17 -0800 (PST) Subject: [Tutor] sys.path.append Message-ID: <21054555.post@talk.nabble.com> I can do this : >>> sys.path.append ( 'C:\dump1' ) but not : >>> x = 'C:\dir1' >>> sys.path.append(x) or : but not : >>> x = ['C:\dir1'] >>> sys.path.append(x) Can I append variables to the path, rather than explicit strings ? -- View this message in context: http://www.nabble.com/sys.path.append-tp21054555p21054555.html Sent from the Python - tutor mailing list archive at Nabble.com. From kent37 at tds.net Wed Dec 17 17:17:14 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 17 Dec 2008 11:17:14 -0500 Subject: [Tutor] sys.path.append In-Reply-To: <21054555.post@talk.nabble.com> References: <21054555.post@talk.nabble.com> Message-ID: <1c2a2c590812170817m69c8b22cl8c78068c3a4d1857@mail.gmail.com> On Wed, Dec 17, 2008 at 10:20 AM, ppaarrkk wrote: > > I can do this : > >>>> sys.path.append ( 'C:\dump1' ) Note you should use raw strings r'C:\dump1' or double backslash 'C:\\dump1' because the \ is a string escape character. > but not : > >>>> x = 'C:\dir1' >>>> sys.path.append(x) That should work fine (other than the single \). What happens when you try it? > or : ?? > but not : >>>> x = ['C:\dir1'] >>>> sys.path.append(x) Here you are appending a list of strings to sys.path, that will not do what you want. > Can I append variables to the path, rather than explicit strings ? Sure. Kent From WilliTf at dshs.wa.gov Wed Dec 17 17:19:07 2008 From: WilliTf at dshs.wa.gov (Williams, Thomas (DSHS/RDA)) Date: Wed, 17 Dec 2008 08:19:07 -0800 Subject: [Tutor] Lucky Boy Sudhir wants to chat In-Reply-To: <1c2a2c590812170651q2466b813waa6b72aa726748e5@mail.gmail.com> References: <4c0a2dab0812170401ofc80c88x@mail.gmail.com> <1c2a2c590812170651q2466b813waa6b72aa726748e5@mail.gmail.com> Message-ID: <941871A13165C2418EC144ACB212BDB0CFAB61@dshsmxoly1504g.dshs.wa.lcl> Thank you Tom -----Original Message----- From: Kent Johnson [mailto:kent37 at tds.net] Sent: Wednesday, December 17, 2008 6:52 AM To: Lucky Boy Sudhir Cc: tutor at python.org Subject: Re: [Tutor] Lucky Boy Sudhir wants to chat I have unsubscribed Lucky Boy from the list as he is just spamming it. Kent On Wed, Dec 17, 2008 at 7:01 AM, Lucky Boy Sudhir wrote: > ----------------------------------------------------------------------- > > Lucky Boy Sudhir wants to stay in better touch using some of Google's > coolest new > products. > > If you already have Gmail or Google Talk, visit: > http://mail.google.com/mail/b-4696b4693-d5d69810df-aafd3a6b94976b5f > You'll need to click this link to be able to chat with Lucky Boy Sudhir . > > To get Gmail - a free email account from Google with over 2,800 megabytes of > storage - and chat with Lucky Boy Sudhir , visit: > http://mail.google.com/mail/a-4696b4693-d5d69810df-ee3cdf55e5 > > Gmail offers: > - Instant messaging right inside Gmail > - Powerful spam protection > - Built-in search for finding your messages and a helpful way of organizing > emails into "conversations" > - No pop-up ads or untargeted banners - just text ads and related information > that are relevant to the content of your messages > > All this, and its yours for free. But wait, there's more! By opening a Gmail > account, you also get access to Google Talk, Google's instant messaging > service: > > http://www.google.com/talk/ > > Google Talk offers: > - Web-based chat that you can use anywhere, without a download > - A contact list that's synchronized with your Gmail account > - Free, high quality PC-to-PC voice calls when you download the Google Talk > client > > Gmail and Google Talk are still in beta. We're working hard to add new features > and make improvements, so we might also ask for your comments and suggestions > periodically. We appreciate your help in making our products even better! > > Thanks, > The Google Team > > To learn more about Gmail and Google Talk, visit: > http://mail.google.com/mail/help/about.html > http://www.google.com/talk/about.html > > (If clicking the URLs in this message does not work, copy and paste them into > the address bar of your browser). > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From david at abbottdavid.com Thu Dec 18 00:33:00 2008 From: david at abbottdavid.com (David) Date: Wed, 17 Dec 2008 18:33:00 -0500 Subject: [Tutor] How to exit program without sys.exit() Message-ID: <49498C2C.2020703@abbottdavid.com> Hi, I make these silly programs to learn from examples I find on the list. I put a couple together just to practice. I have heard it is not a good idea to use sys.exit() but I can not figure out how to do it. Also any and all comments are welcome. Thanks #!/usr/bin/python import sys _count = 0 def getinfo(): answer = yesno("Enter y to continue, n to exit: y/n >> ") if answer == "y": getnumber() else: sys.exit() def counter(): global _count _count += 1 return _count def yesno(question): responce = None while responce not in ("y", "n"): responce = raw_input(question).lower() return responce def getnumber(): try: num = int(raw_input("Enter a number between 25 and 75: ")) if 25 < num < 75: print "WOW you are smart ", sayit() except ValueError: print "Please Enter a number!", getnumber() def sayit(): print "Your total correct answers is", counter() again() def again(): onemore = raw_input("Again? y/n >> " ) if onemore.lower() == "y": getnumber() elif onemore.lower() == "n": getinfo() else: sys.exit def main(): getinfo() if __name__=="__main__": main() -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From wescpy at gmail.com Thu Dec 18 01:54:53 2008 From: wescpy at gmail.com (wesley chun) Date: Wed, 17 Dec 2008 16:54:53 -0800 Subject: [Tutor] How to exit program without sys.exit() In-Reply-To: <49498C2C.2020703@abbottdavid.com> References: <49498C2C.2020703@abbottdavid.com> Message-ID: <78b3a9580812171654p395c51c1q660c7cdd17092b13@mail.gmail.com> > I make these silly programs to learn from examples I find on the list. I put > a couple together just to practice. I have heard it is not a good idea to > use sys.exit() but I can not figure out how to do it. Also any and all > comments are welcome. Thanks david, welcome to Python! it's definitely a good thing to read this list and even more importantly, to practice. as far as your inquiry goes, you don't need *any* sys.exit() calls in your code. just remove them plus the else statement above each, as in: > #!/usr/bin/python > import sys > _count = 0 > > def getinfo(): > answer = yesno("Enter y to continue, n to exit: y/n >> ") > if answer == "y": > getnumber() > > def counter(): > global _count > _count += 1 > return _count > > def yesno(question): > responce = None > while responce not in ("y", "n"): > responce = raw_input(question).lower() > return responce > > def getnumber(): > try: > num = int(raw_input("Enter a number between 25 and 75: ")) > if 25 < num < 75: > print "WOW you are smart ", sayit() > except ValueError: > print "Please Enter a number!", getnumber() > > > def sayit(): > print "Your total correct answers is", counter() > again() > > > def again(): > onemore = raw_input("Again? y/n >> " ) > if onemore.lower() == "y": > getnumber() > elif onemore.lower() == "n": > getinfo() > > def main(): > getinfo() > > if __name__=="__main__": > main() it should still work the same as before. your functions will return to the caller, and they will exit in the same way. finally, when main() is done, your app is automatically exits. one other suggestion... you have a lot of functions. it's possible to reduce the total number, esp. since they're pretty much all one-offs. generally, you want to employ functions for code that is used more than once during a single execution of your code. with your app, you can probably get away with not having any functions at all. cheers, -- 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.gauld at btinternet.com Thu Dec 18 02:26:13 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 18 Dec 2008 01:26:13 -0000 Subject: [Tutor] How to exit program without sys.exit() References: <49498C2C.2020703@abbottdavid.com> Message-ID: "David" wrote > put a couple together just to practice. I have heard it is not a > good idea to use sys.exit() sys.exit is there to exit from a program so there is absolutely nothing wrong with using it. However the way you are using it is not optimal. A program will terminate or exit by itself if you allow it to "run off the end". So you can remove most of your sys.exit() calls. Where you want to use sys.exit() is where something happens that would leave your program running but in a state where it might then do some damage or produce a wriong result. You can then force a premature exit with sys.exit. However even in these cases it's often possible to restructure your code so that an explicit call to sys.exit is not needed. So to answer your question, using sys.exit is not bad or wrong, but mostly you don't need to. It's better to reserve its use for occasions where the program has to be forced to exit early. The other place where you should use sys.exit() is where you want to return a specific error code to the Operating System - usually for other scripts to use to detect errors by your program. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From david at abbottdavid.com Thu Dec 18 04:17:12 2008 From: david at abbottdavid.com (David) Date: Wed, 17 Dec 2008 22:17:12 -0500 Subject: [Tutor] How to exit program without sys.exit() In-Reply-To: <78b3a9580812171654p395c51c1q660c7cdd17092b13@mail.gmail.com> References: <49498C2C.2020703@abbottdavid.com> <78b3a9580812171654p395c51c1q660c7cdd17092b13@mail.gmail.com> Message-ID: <4949C0B8.3010903@abbottdavid.com> >> one other suggestion... you have a lot of functions. it's possible to >> reduce the total number, esp. since they're pretty much all one-offs. >> generally, you want to employ functions for code that is used more >> than once during a single execution of your code. with your app, you >> can probably get away with not having any functions at all. >> >> cheers, >> -- wesley I was practicing how to use a global counter and trying to understand how functions can interact with each other. I can understand if I can see the error's when I run the program. I know my terminology may be hard to follow. Also I like your book :) > So to answer your question, using sys.exit is not bad or wrong, > but mostly you don't need to. It's better to reserve its use for occasions > where the program has to be forced to exit early. The other place where > you should use sys.exit() is where you want to return a specific error > code to the Operating System - usually for other scripts to use to detect > errors by your program. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld Thanks, That does HAL (help a lot). Also thanks for the tutorial/book I have it bookmarked :) -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From wescpy at gmail.com Thu Dec 18 06:22:56 2008 From: wescpy at gmail.com (wesley chun) Date: Wed, 17 Dec 2008 21:22:56 -0800 Subject: [Tutor] How to exit program without sys.exit() In-Reply-To: <4949C0B8.3010903@abbottdavid.com> References: <49498C2C.2020703@abbottdavid.com> <78b3a9580812171654p395c51c1q660c7cdd17092b13@mail.gmail.com> <4949C0B8.3010903@abbottdavid.com> Message-ID: <78b3a9580812172122l7038ee3ax17b0900becce0ccb@mail.gmail.com> > I was practicing how to use a global counter and trying to understand > how functions can interact with each other. I can understand if I can > see the error's when I run the program. I know my terminology may be > hard to follow. Also I like your book :) you are well read since you already have seen places where it is suggested that using sys.exit() is not the best choice. as alan has said, there isn't anything really "evil" about it... it just not necessary, and you can write applications without using it at all. another thing you'll read about is how it's "not good" to have global variables. as with sys.exit() it is possible to have a universal counter without using a global variable. they are a bit worse than sys.exit() because they take up extra memory, there is less control -- they can be updated in multiple places in an application; such access may result in race conditions and unexpected behavior if modified by concurrent threads, etc. to find a suitable replacement will depend on what you're counting, and where a better place may be to store the counter. for example, you can use a static member of a class, a closure, etc. if you have specific application requirements, just post them to the list, and folks will be glad to help you get around using global counters. glad you enjoy the book! pssst... there is a DVD version of it coming out at the end of this month called "Python Fundamentals"... keep an eye out for it! ;-) best regards, -- 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 gmorris at serif.com Thu Dec 18 12:41:08 2008 From: gmorris at serif.com (Gareth at Serif) Date: Thu, 18 Dec 2008 03:41:08 -0800 (PST) Subject: [Tutor] MP3Info class usage In-Reply-To: <20081216045037.GA12325@inocybe.teonanacatl.org> References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> <20980334.post@talk.nabble.com> <20081216045037.GA12325@inocybe.teonanacatl.org> Message-ID: <21071143.post@talk.nabble.com> Todd Zullinger wrote: > > That's hard to guess at. If you can explain what you have done and > how you've installed eyeD3, that would help. > I've not installed it, I've just imported it in my main program. How do you install eyeD3, there's no installation package? I read the readme, which talks about executing 'configure', but that just reports back that it is not recognized as an internal or external command, operable program or batch file. Clearly I'm missing a huge step, can you help me out? -- View this message in context: http://www.nabble.com/MP3Info-class-usage-tp20934673p21071143.html Sent from the Python - tutor mailing list archive at Nabble.com. From tmz at pobox.com Thu Dec 18 15:51:58 2008 From: tmz at pobox.com (Todd Zullinger) Date: Thu, 18 Dec 2008 09:51:58 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: <21071143.post@talk.nabble.com> References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> <20980334.post@talk.nabble.com> <20081216045037.GA12325@inocybe.teonanacatl.org> <21071143.post@talk.nabble.com> Message-ID: <20081218145157.GO12325@inocybe.teonanacatl.org> Gareth at Serif wrote: > I've not installed it, I've just imported it in my main program. > How do you install eyeD3, there's no installation package? What OS are you running? I can help if you run some sort of *nix system. If you're on Windows, then I'll have to pass as I know nothing about installing python modules on Windows. :) > I read the readme, which talks about executing 'configure', but that > just reports back that it is not recognized as an internal or > external command, operable program or batch file. Hmm, "... batch file" makes me think this is Windows. On *nix systems, you run ./configure; make; sudo make install. The ./ in front of configure tells the shell that the program you are trying to run is in the current directory. Otherwise, the shell would look for configure in your $PATH, which does not include the current directory. -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mollison's Bureaucracy Hypothesis: If an idea can survive a bureaucratic review and be implemented it wasn't worth doing. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From jbastosneto at gmail.com Thu Dec 18 17:03:55 2008 From: jbastosneto at gmail.com (Jose Neto) Date: Thu, 18 Dec 2008 14:03:55 -0200 Subject: [Tutor] PyLucene on Python 2.6 Message-ID: I don't know if it's off-topic or not. If it is, please forgive me and ignore this question. Does anyone know if there is a win32 binary of PyLucene? I tried to compile the sources, but the python "explodes" in the middle of the compilation. Log file: $ make /cygdrive/f/Python26/python.exe -v -m jcc.__main__ --shared --jar lucene-java-2 .4.0/build/lucene-core-2.4.0.jar --jar lucene-java-2.4.0/build/contrib/snowball/ lucene-snowball-2.4.0.jar --jar lucene-java-2.4.0/build/contrib/highlighter/luce ne-highlighter-2.4.0.jar --jar lucene-java-2.4.0/build/contrib/analyzers/lucene- analyzers-2.4.0.jar --jar lucene-java-2.4.0/build/contrib/regex/lucene-regex-2.4 .0.jar --jar lucene-java-2.4.0/build/contrib/queries/lucene-queries-2.4.0.jar -- jar lucene-java-2.4.0/build/contrib/instantiated/lucene-instantiated-2.4.0.jar - -jar build/jar/extensions.jar --package java.lang java.lang.System java.lang.Run time --package java.util java.text.SimpleDateFormat --package java.io java.io.St ringReader java.io.InputStreamReader java.io.FileInputStream --exclude org.apach e.lucene.queryParser.Token --exclude org.apache.lucene.queryParser.TokenMgrError --exclude org.apache.lucene.queryParser.QueryParserTokenManager --exclude org.a pache.lucene.queryParser.ParseException --python lucene --mapping org.apache.luc ene.document.Document 'get:(Ljava/lang/String;)Ljava/lang/String;' --mapping jav a.util.Properties 'getProperty:(Ljava/lang/String;)Ljava/lang/String;' --sequenc e org.apache.lucene.search.Hits 'length:()I' 'doc:(I)Lorg/apache/lucene/document /Document;' --version 2.4.0 --files 2 --build [...] import zlib # builtin # f:\Python26\lib\site-packages\jcc-2.1-py2.6-win32.egg\jcc\python.pyc matches f :\Python26\lib\site-packages\jcc-2.1-py2.6-win32.egg\jcc\python.py import jcc.python # precompiled from f:\Python26\lib\site-packages\jcc-2.1-py2.6 -win32.egg\jcc\python.pyc # f:\Python26\lib\platform.pyc matches f:\Python26\lib\platform.py import platform # precompiled from f:\Python26\lib\platform.pyc # f:\Python26\lib\string.pyc matches f:\Python26\lib\string.py import string # precompiled from f:\Python26\lib\string.pyc import strop # builtin # f:\Python26\lib\py_compile.pyc matches f:\Python26\lib\py_compile.py import py_compile # precompiled from f:\Python26\lib\py_compile.pyc import marshal # builtin # f:\Python26\lib\traceback.pyc matches f:\Python26\lib\traceback.py import traceback # precompiled from f:\Python26\lib\traceback.pyc make: *** [compile] Error 255 Regards Jose -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreas at kostyrka.org Thu Dec 18 18:16:37 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Thu, 18 Dec 2008 18:16:37 +0100 Subject: [Tutor] sys.path.append In-Reply-To: <1c2a2c590812170817m69c8b22cl8c78068c3a4d1857@mail.gmail.com> References: <21054555.post@talk.nabble.com> <1c2a2c590812170817m69c8b22cl8c78068c3a4d1857@mail.gmail.com> Message-ID: <20081218181637.18874a8a@andi-lap> Am Wed, 17 Dec 2008 11:17:14 -0500 schrieb "Kent Johnson" : > On Wed, Dec 17, 2008 at 10:20 AM, ppaarrkk > wrote: > > > > I can do this : > > > >>>> sys.path.append ( 'C:\dump1' ) > > Note you should use raw strings r'C:\dump1' or double backslash > 'C:\\dump1' because the \ is a string escape character. > > > but not : > > > >>>> x = 'C:\dir1' > >>>> sys.path.append(x) > > That should work fine (other than the single \). What happens when > you try it? > > > or : > > ?? > > > but not : > > >>>> x = ['C:\dir1'] > >>>> sys.path.append(x) > > Here you are appending a list of strings to sys.path, that will not do > what you want. The correct thing to do if you want to add a list of paths to sys.path (btw, that probably is not what you want in real life, but that's a complete different question.), you should do sys.path.extend(x) which is an efficient way to do for i in x: sys.path.append(i) Or to visualize it with something simpler: a = [1, 2, 3] b = [4, 5, 6] c = 7 a # [1,2,3] a.append(c) a # [1,2,3,7] a.append(b) a # [1,2,3,7,[4,5,6]] a.extend(b) a # [1,2,3,7,[4,5,6],4,5,6] Basically append just appends it's parameter to the list, and if the parameter happens to be a list, it appends the list as one item. Extend on the other hand, appends all iterable items in the parameter to the list. Andreas From bgailer at gmail.com Thu Dec 18 20:31:05 2008 From: bgailer at gmail.com (bob gailer) Date: Thu, 18 Dec 2008 14:31:05 -0500 Subject: [Tutor] How to exit program without sys.exit() In-Reply-To: <49498C2C.2020703@abbottdavid.com> References: <49498C2C.2020703@abbottdavid.com> Message-ID: <494AA4F9.5080003@gmail.com> David wrote: > Hi, > I make these silly programs to learn from examples I find on the list. > I put a couple together just to practice. I have heard it is not a > good idea to use sys.exit() but I can not figure out how to do it. > Also any and all comments are welcome. Thanks Others have made valuable comments. I add: It is better to use a while loop rather than recursive function calls. The logic is a lot easier to follow, and you won't run into the recursive depth limit. Also restrict try blocks to just the statement likely to raise the error you are expecting. def getinfo(): while True: answer = yesno("Enter y to continue, n to exit: y/n >> ") if answer == "y": getnumber() else: break def getnumber(): num = 0 while True: try: num = int(raw_input("Enter a number between 25 and 75: ")) except ValueError: print "Please Enter a number!" if 25 < num < 75: print "WOW you are smart ", sayit() break There are a lot more recursive calls that I will not try to "fix". I hope you get the idea. In fact I'd start out with no functions at all - just one program in a loop. See what you can accomplish that way, then add functions only as needed. > > #!/usr/bin/python > import sys > _count = 0 > > def getinfo(): > answer = yesno("Enter y to continue, n to exit: y/n >> ") > if answer == "y": > getnumber() > else: > sys.exit() > > def counter(): > global _count > _count += 1 > return _count > > def yesno(question): > responce = None > while responce not in ("y", "n"): > responce = raw_input(question).lower() > return responce > > def getnumber(): > try: > num = int(raw_input("Enter a number between 25 and 75: ")) > if 25 < num < 75: > print "WOW you are smart ", sayit() > except ValueError: > print "Please Enter a number!", getnumber() > > > def sayit(): > print "Your total correct answers is", counter() > again() > > > def again(): > onemore = raw_input("Again? y/n >> " ) > if onemore.lower() == "y": > getnumber() > elif onemore.lower() == "n": > getinfo() > else: > sys.exit > > > def main(): > getinfo() > > if __name__=="__main__": > main() > > > -- Bob Gailer Chapel Hill NC 919-636-4239 From cbc at unc.edu Thu Dec 18 21:59:15 2008 From: cbc at unc.edu (Chris Calloway) Date: Thu, 18 Dec 2008 15:59:15 -0500 Subject: [Tutor] PyLucene on Python 2.6 In-Reply-To: References: Message-ID: <494AB9A3.6020001@unc.edu> On 12/18/2008 11:03 AM, Jose Neto wrote: > Does anyone know if there is a win32 binary of PyLucene? http://code.google.com/p/pylucene-win32-binary/downloads/list -- Sincerely, Chris Calloway http://www.secoora.org office: 332 Chapman Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From emadnawfal at gmail.com Fri Dec 19 15:01:24 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 19 Dec 2008 09:01:24 -0500 Subject: [Tutor] py_compile and chmod +x Message-ID: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> Hi Tutors, #! /usr/bin/env python print "Hello Tutors" I have this script saved as hello.py. Why can I execute it, but not the compiled version? or am I doing something wrong? Just curious. Any help appreciated. For example : emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.py emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py Hello Tutors Now I compile it in Python: >>> import py_compile >>> py_compile.compile("hello.py") >>> emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc : command not found: ? ./hello.pyc: line 2: syntax error near unexpected token `(' ./hello.pyc: line 2: `?KIc@s dGHdS(s Hello TutorsN((((hello.pys' emad at emad-laptop:~/Desktop/Programming/Haskell$ -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "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 eduardo.susan at gmail.com Fri Dec 19 16:54:45 2008 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Fri, 19 Dec 2008 08:54:45 -0700 Subject: [Tutor] Looping, and Win32com.client Message-ID: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> Hello, this is my first post in this list and I'm not a programmer, but am enjoying learning python. I'm trying to make a script to add worksheets to excel files: I managed to implement this code: import os folder = 'C:\\Personal\\oldxlsformat\\' from win32com.client import Dispatch xl = Dispatch('Excel.Application') xl.Application.AskToUpdateLinks = "False" for ficheiro in os.listdir(folder): if ficheiro.endswith('.xls'): wb = xl.Workbooks.Open(ficheiro, 2) wb.Worksheets.Add().Name = "BVTest" wb.Close(SaveChanges = 1) xl.Quit() but then I get this error: Traceback (most recent call last): File "C:\Personal\exceltests4.py", line 8, in wb = xl.Workbooks.Open(ficheiro, 2) File ">", line 8, in Open com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"'AVERY GRAPHICS INK JET.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", u'C:\\Program Files\\Microsoft Office\\Office12\\1033\\XLMAIN11.CHM', 0, -2146827284), None) The files I have as a test are these: C:\Personal\oldxlsformat\AVERY GRAPHICS INK JET.xls C:\Personal\oldxlsformat\CUTTING MATS & RULERS.xls C:\Personal\oldxlsformat\Oracal - Ontario Stocking Order.xls C:\Personal\oldxlsformat\ORACAL 8100-8500.xls From mail at timgolden.me.uk Fri Dec 19 16:57:49 2008 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 19 Dec 2008 15:57:49 +0000 Subject: [Tutor] Looping, and Win32com.client In-Reply-To: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> References: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> Message-ID: <494BC47D.6030002@timgolden.me.uk> Eduardo Vieira wrote: > Hello, this is my first post in this list and I'm not a programmer, > but am enjoying learning python. > I'm trying to make a script to add worksheets to excel files: > I managed to implement this code: > > import os > folder = 'C:\\Personal\\oldxlsformat\\' > from win32com.client import Dispatch > xl = Dispatch('Excel.Application') > xl.Application.AskToUpdateLinks = "False" > for ficheiro in os.listdir(folder): os.listdir returns a list of files relative to the directory in question. (Try it at the interpreter and see). Excel requires, pretty much, a list of absolute filenames. So try using something like os.path.join (folder, ficheiro) when opening in Excel. TJG From eduardo.susan at gmail.com Fri Dec 19 17:23:53 2008 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Fri, 19 Dec 2008 09:23:53 -0700 Subject: [Tutor] Looping, and Win32com.client In-Reply-To: <494BC47D.6030002@timgolden.me.uk> References: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> <494BC47D.6030002@timgolden.me.uk> Message-ID: <9356b9f30812190823x6b279da1xda5ffc950c16520d@mail.gmail.com> Thank you, now it worked! I made these changes to my code: for ficheiro in os.listdir(folder): file = os.path.join(folder, ficheiro) #added this if ficheiro.endswith('.xls'): wb = xl.Workbooks.Open(file, 2) # changed this On Fri, Dec 19, 2008 at 8:57 AM, Tim Golden wrote: > Eduardo Vieira wrote: >> >> Hello, this is my first post in this list and I'm not a programmer, >> but am enjoying learning python. >> I'm trying to make a script to add worksheets to excel files: >> I managed to implement this code: >> >> import os >> folder = 'C:\\Personal\\oldxlsformat\\' >> from win32com.client import Dispatch >> xl = Dispatch('Excel.Application') >> xl.Application.AskToUpdateLinks = "False" >> for ficheiro in os.listdir(folder): > > > os.listdir returns a list of files relative to > the directory in question. (Try it at the > interpreter and see). Excel requires, pretty > much, a list of absolute filenames. So try > using something like os.path.join (folder, ficheiro) > when opening in Excel. > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From mail at timgolden.me.uk Fri Dec 19 17:28:15 2008 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 19 Dec 2008 16:28:15 +0000 Subject: [Tutor] Looping, and Win32com.client In-Reply-To: <494BC47D.6030002@timgolden.me.uk> References: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> <494BC47D.6030002@timgolden.me.uk> Message-ID: <494BCB9F.8020702@timgolden.me.uk> Tim Golden wrote: > Eduardo Vieira wrote: >> Hello, this is my first post in this list and I'm not a programmer, >> but am enjoying learning python. >> I'm trying to make a script to add worksheets to excel files: >> I managed to implement this code: >> >> import os >> folder = 'C:\\Personal\\oldxlsformat\\' >> from win32com.client import Dispatch >> xl = Dispatch('Excel.Application') >> xl.Application.AskToUpdateLinks = "False" >> for ficheiro in os.listdir(folder): > > > os.listdir returns a list of files relative to > the directory in question. (Try it at the > interpreter and see). Excel requires, pretty > much, a list of absolute filenames. Of course, I meant this it requires one absolute filename, not a list :) TJG From bgailer at gmail.com Fri Dec 19 18:12:46 2008 From: bgailer at gmail.com (bob gailer) Date: Fri, 19 Dec 2008 12:12:46 -0500 Subject: [Tutor] py_compile and chmod +x In-Reply-To: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> References: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> Message-ID: <494BD60E.8000005@gmail.com> Emad Nawfal (???? ????) wrote: > Hi Tutors, > #! /usr/bin/env python > print "Hello Tutors" > > I have this script saved as hello.py. Why can I execute it, but not > the compiled version? or am I doing something wrong? Just curious. > Any help appreciated. There are 2 issues here. 1 - The shell inspects the first line of the file (#! /usr/bin/env python) to find the program to run The compiled file does not have this first line. 2 - The python interpreter expects a script not a compiled program. So even if you got the interpreter to run it would try to treat the file as a script and bomb anyway. However when you import hello then it looks first for hello.pyc, and runs that. If hello.pyc is missing or older than hello.py it will compile hello.py and then run hello.pyc. > For example : > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.py > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py > Hello Tutors > > Now I compile it in Python: > >>> import py_compile > >>> py_compile.compile("hello.py") > >>> > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc > : command not found: ? > ./hello.pyc: line 2: syntax error near unexpected token `(' > ./hello.pyc: line 2: `?KIc@s dGHdS(s > Hello > TutorsN((((hello.pys' > emad at emad-laptop:~/Desktop/Programming/Haskell$ > > > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? > ????????.....???? ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From andreas at kostyrka.org Fri Dec 19 18:21:06 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Fri, 19 Dec 2008 18:21:06 +0100 Subject: [Tutor] py_compile and chmod +x In-Reply-To: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> References: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> Message-ID: <20081219182106.13aaf7e8@andi-lap> Am Fri, 19 Dec 2008 09:01:24 -0500 schrieb "Emad Nawfal (???? ????)" : You are not supposed to compile the main script. pyc files are automatically generated when you import the py file (but not execute it), if you are allowed to write them beside the py file. When a pyc file exists, and it's newer than the py file, python will import the py file from the pyc file, thus saving on parsing the py file. In effect the pyc file is nothing more than a file cache for the parsed and bytecompiled version of the py file. Andreas > Hi Tutors, > #! /usr/bin/env python > print "Hello Tutors" > > I have this script saved as hello.py. Why can I execute it, but not > the compiled version? or am I doing something wrong? Just curious. > Any help appreciated. > For example : > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.py > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py > Hello Tutors > > Now I compile it in Python: > >>> import py_compile > >>> py_compile.compile("hello.py") > >>> > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc > : command not found: ? > ./hello.pyc: line 2: syntax error near unexpected token `(' > ./hello.pyc: line 2: `?KIc@s dGHdS(s > Hello > TutorsN((((hello.pys' > emad at emad-laptop:~/Desktop/Programming/Haskell$ > > From andreas at kostyrka.org Fri Dec 19 18:29:18 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Fri, 19 Dec 2008 18:29:18 +0100 Subject: [Tutor] Looping, and Win32com.client In-Reply-To: <494BC47D.6030002@timgolden.me.uk> References: <9356b9f30812190754v356d1a3asa82960981d5f4151@mail.gmail.com> <494BC47D.6030002@timgolden.me.uk> Message-ID: <20081219182918.73af1df1@andi-lap> Am Fri, 19 Dec 2008 15:57:49 +0000 schrieb Tim Golden : > Eduardo Vieira wrote: > > Hello, this is my first post in this list and I'm not a programmer, > > but am enjoying learning python. > > I'm trying to make a script to add worksheets to excel files: > > I managed to implement this code: > > > > import os > > folder = 'C:\\Personal\\oldxlsformat\\' > > from win32com.client import Dispatch > > xl = Dispatch('Excel.Application') > > xl.Application.AskToUpdateLinks = "False" > > for ficheiro in os.listdir(folder): > > > os.listdir returns a list of files relative to > the directory in question. (Try it at the > interpreter and see). Excel requires, pretty > much, a list of absolute filenames. So try > using something like os.path.join (folder, ficheiro) > when opening in Excel. The above should be enough, but if problems persists, it's often helpful in Win32 OSes to use the 8.3 filename: shortname = win32api.GetShortPathName(os.path.join(folder, ficheiro)) Another alternative would be to use glob: import glob ... for ficheiro in glob.glob(os.path.join(folder, "*.xls")): glob.glob returns the full path that was found. Andreas > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From emadnawfal at gmail.com Fri Dec 19 18:35:16 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 19 Dec 2008 12:35:16 -0500 Subject: [Tutor] py_compile and chmod +x In-Reply-To: <20081219182106.13aaf7e8@andi-lap> References: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> <20081219182106.13aaf7e8@andi-lap> Message-ID: <652641e90812190935s18ceb6b8v4a349fd92591cc21@mail.gmail.com> Thank you so much everybody. I was just curious. This has been very useful. On Fri, Dec 19, 2008 at 12:21 PM, Andreas Kostyrka wrote: > Am Fri, 19 Dec 2008 09:01:24 -0500 > schrieb "Emad Nawfal (???? ????)" : > > You are not supposed to compile the main script. > > pyc files are automatically generated when you import the py file (but > not execute it), if you are allowed to write them beside the py file. > > When a pyc file exists, and it's newer than the py file, python will > import the py file from the pyc file, thus saving on parsing the py > file. > > In effect the pyc file is nothing more than a file cache for the parsed > and bytecompiled version of the py file. > > Andreas > > > Hi Tutors, > > #! /usr/bin/env python > > print "Hello Tutors" > > > > I have this script saved as hello.py. Why can I execute it, but not > > the compiled version? or am I doing something wrong? Just curious. > > Any help appreciated. > > For example : > > > > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.py > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py > > Hello Tutors > > > > Now I compile it in Python: > > >>> import py_compile > > >>> py_compile.compile("hello.py") > > >>> > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc > > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc > > : command not found: ? > > ./hello.pyc: line 2: syntax error near unexpected token `(' > > ./hello.pyc: line 2: `?KIc @s dGHd S( s > > Hello > > TutorsN((((hello.py s' > > emad at emad-laptop:~/Desktop/Programming/Haskell$ > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Fri Dec 19 18:36:08 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 19 Dec 2008 12:36:08 -0500 Subject: [Tutor] py_compile and chmod +x In-Reply-To: <20081219182106.13aaf7e8@andi-lap> References: <652641e90812190601x2c803cffm7b3ef60457083c98@mail.gmail.com> <20081219182106.13aaf7e8@andi-lap> Message-ID: <652641e90812190936g6c8ae938o4d925f292860685f@mail.gmail.com> Thank you so much. This has been very helpful. On Fri, Dec 19, 2008 at 12:21 PM, Andreas Kostyrka wrote: > Am Fri, 19 Dec 2008 09:01:24 -0500 > schrieb "Emad Nawfal (???? ????)" : > > You are not supposed to compile the main script. > > pyc files are automatically generated when you import the py file (but > not execute it), if you are allowed to write them beside the py file. > > When a pyc file exists, and it's newer than the py file, python will > import the py file from the pyc file, thus saving on parsing the py > file. > > In effect the pyc file is nothing more than a file cache for the parsed > and bytecompiled version of the py file. > > Andreas > > > Hi Tutors, > > #! /usr/bin/env python > > print "Hello Tutors" > > > > I have this script saved as hello.py. Why can I execute it, but not > > the compiled version? or am I doing something wrong? Just curious. > > Any help appreciated. > > For example : > > > > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.py > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py > > Hello Tutors > > > > Now I compile it in Python: > > >>> import py_compile > > >>> py_compile.compile("hello.py") > > >>> > > emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc > > > > emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc > > : command not found: ? > > ./hello.pyc: line 2: syntax error near unexpected token `(' > > ./hello.pyc: line 2: `?KIc @s dGHd S( s > > Hello > > TutorsN((((hello.py s' > > emad at emad-laptop:~/Desktop/Programming/Haskell$ > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From carroll at tjc.com Sat Dec 20 01:25:08 2008 From: carroll at tjc.com (Terry Carroll) Date: Fri, 19 Dec 2008 16:25:08 -0800 (PST) Subject: [Tutor] MP3Info class usage In-Reply-To: <21071143.post@talk.nabble.com> Message-ID: On Thu, 18 Dec 2008, Gareth at Serif wrote: > I've not installed it, I've just imported it in my main program. How do you > install eyeD3, there's no installation package? I read the readme, which > talks about executing 'configure', but that just reports back that it is not > recognized as an internal or external command, operable program or batch > file. I'm betting that you'e on windows, like me. configure is s shell script. You'll need a version os shell that runs on windows. make, too, I'll bet. I run Cygwin on windows, which is a pretty good thing to have apart from this. It's free and avaliable from http://www.cygwin.com/ By default, I believe make is not installed; you'll have to request it. You'll still get the error that "configure" is not recognized as a valid command, but invoking it with "sh congigure" works for me. It would be nice if Eyed3, which is not OS-specific, did not have such an OS-specific install process. From carroll at tjc.com Sat Dec 20 02:07:29 2008 From: carroll at tjc.com (Terry Carroll) Date: Fri, 19 Dec 2008 17:07:29 -0800 (PST) Subject: [Tutor] MP3Info class usage In-Reply-To: Message-ID: On Fri, 19 Dec 2008, Terry Carroll wrote: > configure is s shell script. You'll need a version os shell that runs on > windows. > make, too, I'll bet. > > I run Cygwin on windows, which is a pretty good thing to have apart from > this. It's free and avaliable from http://www.cygwin.com/ ... > It would be nice if Eyed3, which is not OS-specific, did not have such an > OS-specific install process. You know, I just reinstalled Eyed3, and compared the installed result to the six files distributed in the zipped tarfile, and they're identical except that one file is named __init__.py.in instead of __init__.py, and has two doc variables defined with templates. If you want to avoid having to install a Unix-like environment, I suggest you just 1) unzip and untar the zipped tarfile, 2) copy the eyeD3 directory from eyeD3-0.6.16/src/ to Python's Lib/site-packages/ directory 3) rename __init__.py.in to __init__.py 4) (probably cosmetic) in __init__.py, edit the lines: eyeD3Version = "@PACKAGE_VERSION@"; eyeD3Maintainer = "@PACKAGE_BUGREPORT@"; to: eyeD3Version = "0.6.16"; eyeD3Maintainer = "Travis Shirk "; Someone else will now explain why this is a terrible idea. From Jaggojaggo+Py at gmail.com Sat Dec 20 17:23:29 2008 From: Jaggojaggo+Py at gmail.com (Omer) Date: Sat, 20 Dec 2008 18:23:29 +0200 Subject: [Tutor] Class Extend Help Message-ID: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> Hey. I'm trying to do something I think is basic and am failing. The goal is: [mimicking the google urlopen syntax] try: from google.appengine.api.urlfetch import fetch except: from urllib import urlopen as fetch How do I add this "fetch" the property of content? I basically want fetch(url.com).content to replace urlopen(url.com).read() Mind, content is not a method. Thoughts? Thx. Omer. -------------- next part -------------- An HTML attachment was scrubbed... URL: From roadierich at googlemail.com Sat Dec 20 17:49:24 2008 From: roadierich at googlemail.com (Richard Lovely) Date: Sat, 20 Dec 2008 16:49:24 +0000 Subject: [Tutor] Class Extend Help In-Reply-To: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> Message-ID: There are three ways as I see it: using __getattr__, using a new init, or using a property decorator. The last two are probably the most pythonic, but I'm not familiar with decorators, so here's how I'd do the second of the three: try: from google.appengine.api.urlfetch import fetch except: from urllib import urlopen class fetch(urlopen): def __init__(self, *args): urlopen.__init__(self, *args) self.content = self.read() This probably doesn't behave exactly the same way as the google class, but it should be similar enough? --- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com 2008/12/20 Omer : > Hey. > > I'm trying to do something I think is basic and am failing. > > The goal is: > [mimicking the google urlopen syntax] > > try: > from google.appengine.api.urlfetch import fetch > except: > from urllib import urlopen as fetch > > > How do I add this "fetch" the property of content? > > I basically want fetch(url.com).content to replace urlopen(url.com).read() > > Mind, content is not a method. > > Thoughts? > > Thx. Omer. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From mwalsh at mwalsh.org Sat Dec 20 18:35:02 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Sat, 20 Dec 2008 11:35:02 -0600 Subject: [Tutor] Class Extend Help In-Reply-To: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> Message-ID: <494D2CC6.1090004@mwalsh.org> Omer wrote: > Hey. > > I'm trying to do something I think is basic and am failing. > > The goal is: > [mimicking the google urlopen syntax] > > try: > from google.appengine.api.urlfetch import fetch > except: > from urllib import urlopen as fetch > > > How do I add this "fetch" the property of content? Apparently, the urlfetch.fetch function returns a 'Response' object which holds the attribute 'content' (among others). http://code.google.com/appengine/docs/urlfetch/responseobjects.html So, I suppose it makes sense to do something similar. Here's another idea (untested): from urllib2 import urlopen def fetch(url): class Response(object): pass response = Response() response.content = urlopen('http://www.example.com').read() return response ... or if you later want to match the appengine api more closely, then just add what you need (something like) ... from urllib2 import urlopen class Response(object): def __init__(self, url): response = urlopen(url) self.status_code = response.code ... self.content = response.read() def fetch(url): return Response(url) HTH, Marty From tmz at pobox.com Sat Dec 20 19:49:31 2008 From: tmz at pobox.com (Todd Zullinger) Date: Sat, 20 Dec 2008 13:49:31 -0500 Subject: [Tutor] MP3Info class usage In-Reply-To: References: Message-ID: <20081220184931.GY12325@inocybe.teonanacatl.org> Terry Carroll wrote: > On Fri, 19 Dec 2008, Terry Carroll wrote: >> It would be nice if Eyed3, which is not OS-specific, did not have >> such an OS-specific install process. Perhaps. But someone who cares about windows support would have to submit patches. I don't know if Travis uses windows or not. I have submitted a number of patches to eyeD3, but I don't use windows at all myself. I can't see spending any time to support a proprietary OS. :) And, to be fair, the install process isn't really OS-specific. It works on a large number of operating systems -- just not on Windows. > You know, I just reinstalled Eyed3, and compared the installed > result to the six files distributed in the zipped tarfile, and > they're identical except that one file is named __init__.py.in > instead of __init__.py, and has two doc variables defined with > templates. > > If you want to avoid having to install a Unix-like environment, > I suggest you just > > 1) unzip and untar the zipped tarfile, > 2) copy the eyeD3 directory from eyeD3-0.6.16/src/ to Python's > Lib/site-packages/ directory > 3) rename __init__.py.in to __init__.py > 4) (probably cosmetic) in __init__.py, edit the lines: > > eyeD3Version = "@PACKAGE_VERSION@"; > eyeD3Maintainer = "@PACKAGE_BUGREPORT@"; > > to: > > eyeD3Version = "0.6.16"; > eyeD3Maintainer = "Travis Shirk "; > > Someone else will now explain why this is a terrible idea. I don't think it is a bad idea at all. I was going to suggest something similar. If you do that, you should be able to copy the src/eyeD3 directory somewhere in your PYTHONPATH and use it just fine. -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I like this 'God'; he's so deliciously evil. -- Stewie Griffin -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From ravikondamuru at gmail.com Sat Dec 20 20:54:16 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Sat, 20 Dec 2008 11:54:16 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <49427573.5020902@gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> <49427573.5020902@gmail.com> Message-ID: <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> I am trying to use xstruct module to unpack a varaible size record with the following structure. struct nameid { u32bits len /* total length */ u32bits id; char name; /* name variable length */ } As can be seen the length of the name = len - (sizeof(len) + sizeof(id)). How do I use xstruct or struct to unpack such a structure? n = xstruct.structdef(xstruct.little_endian, [ ('len', (xstruct. unsigned_long, 1)), ('id', (xstruct.unsigned_long, 1)), ('name', (xstruct.string, )), ]) xstruct seems to expect the exact len to be specified in structdef. Is there a way to tell it to go till the end of the buffer passed? thanks, Ravi. On Fri, Dec 12, 2008 at 6:30 AM, bob gailer wrote: > Ravi Kondamuru wrote: > >> Denis, These are 32bit, 64bit counters (essentially numbers). >> Bob, There are well over 10K counters in the log file that are updated >> every 5 secs. If a counter1's graph was requested, log will have to be >> parsed once to get the data points. If a user asked for a counter2, now it >> needs to be retrieved from the log file. Which essentially means having to >> go through the logfile again. This is because i want to try to avoid using a >> database to store values after parsing the file. >> > Here is a little test program I wrote to check timing on a file of > *100,000* 64 bit counters. > > import time, struct > ctrs = 100000 > s = time.time() > # create a file of 800,000 characters (100,000 64 bit numbers) > f = open('ravi.dat', 'wb') > for i in range(ctrs): > f.write('abcdefgh') # it does not matter what we write as long as it is 8 > bytes. > print time.time() - s > > s = time.time() > l = [] > # read the file > f = open('ravi.dat', 'rb').read() > > # unpack each 8 characters into an integer and collect in a list > for b in range(0, ctrs, 8): > n = struct.unpack('q', f[b:b+8]) > l.append(n) > print time.time() - s > > Writing the file took 0.14 seconds on my computer > Reading and unpacking 0.04 seconds. > I think you can set performance issues aside. > > There is a principle in programming that proposes: > 1 - get a program running. Preferably in Python as development time is much > faster. > 2 - check its performance. > 3 - refine as needed. > > > -- > Bob Gailer > Chapel Hill NC 919-636-4239 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bill at celestial.net Sat Dec 20 21:54:21 2008 From: bill at celestial.net (Bill Campbell) Date: Sat, 20 Dec 2008 12:54:21 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> <49427573.5020902@gmail.com> <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> Message-ID: <20081220205421.GB25586@ayn.mi.celestial.com> On Sat, Dec 20, 2008, Ravi Kondamuru wrote: >I am trying to use xstruct module to unpack a varaible size record with the >following structure. > >struct nameid { > u32bits len /* total length */ >u32bits id; >char name; /* name variable length */ >} > >As can be seen the length of the name = len - (sizeof(len) + sizeof(id)). > >How do I use xstruct or struct to unpack such a structure? > >n = xstruct.structdef(xstruct.little_endian, [ > ('len', (xstruct. unsigned_long, 1)), > ('id', (xstruct.unsigned_long, 1)), > ('name', (xstruct.string, )), >]) > >xstruct seems to expect the exact len to be specified in structdef. Is there >a way to tell it to go till the end of the buffer passed? I haven't done this in python yet, but when handling things like this in C and perl, I have done it with two reads, the first to get the length, the second to read the data with that length. Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Independent self-reliant people would be a counterproductive anachronism in the collective society of the future where people will be defined by their associations. 1896 John Dewey, educational philosopher, proponent of modern public schools. From rellsworthpollard at gmail.com Sat Dec 20 21:40:03 2008 From: rellsworthpollard at gmail.com (R. Ellsworth Pollard) Date: Sat, 20 Dec 2008 15:40:03 -0500 Subject: [Tutor] Compiling Python 2.6.1 on Leopard Message-ID: Where might I find instructions for compiling Python on Leopard? Robert -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Dec 21 02:00:22 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 21 Dec 2008 01:00:22 -0000 Subject: [Tutor] reading output from a c executable. References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com><36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com><49415B8A.3080701@alchemy.com><36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com><49415FDF.3080601@alchemy.com><36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com><4941E06E.1090608@gmail.com><36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com><49427573.5020902@gmail.com> <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> Message-ID: "Ravi Kondamuru" wrote > struct nameid { > u32bits len /* total length */ > u32bits id; > char name; /* name variable length */ > } > > As can be seen the length of the name = len - (sizeof(len) + > sizeof(id)). > How do I use xstruct or struct to unpack such a structure? I think you will need to read the two fixed length fieldfs then read the name char by char until you hit the null character. After all, thats what you'd need to do to read it from a file or stream in C too... Something like: ltrs = '' ln = struct.unpack(....) id = struct.unpack(....) c = struct.unpack(...) while c != 0: ltrs.append(c) name = ''.join(ltrs) You probably could read the two ints and the first char in one call to struct... HTH, Alan G. From alan.gauld at btinternet.com Sun Dec 21 02:07:44 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 21 Dec 2008 01:07:44 -0000 Subject: [Tutor] Compiling Python 2.6.1 on Leopard References: Message-ID: "R. Ellsworth Pollard" wrote in message news:d52d80370812201240te2d4abby16a7b7745768bf06 at mail.gmail.com... > Where might I find instructions for compiling Python on Leopard? Have you tried the Mac mailing list or the MacPython web site? I expect the procedure to be the same as for previous versions of Python on a Mac except you might need to update some of the compiler libraries etc. Alan G. Still on a G3 iBook with Tiger... From alan.gauld at btinternet.com Sun Dec 21 02:05:21 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 21 Dec 2008 01:05:21 -0000 Subject: [Tutor] reading output from a c executable. References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com><36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com><49415B8A.3080701@alchemy.com><36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com><49415FDF.3080601@alchemy.com><36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com><4941E06E.1090608@gmail.com><36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com><49427573.5020902@gmail.com><36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> <20081220205421.GB25586@ayn.mi.celestial.com> Message-ID: "Bill Campbell" wrote >>struct nameid { >> u32bits len /* total length */ >>u32bits id; >>char name; /* name variable length */ >>} >> > I haven't done this in python yet, but when handling things like > this in C and perl, I have done it with two reads, the first to > get the length, the second to read the data with that length. Ah yes, I didn't notice that len was the total length of the record, in that case its much easier. As Bill says you can construct the struct format string using the calculated length value. Something like: fmt = "%dc" % len - (2 * LEN32) name = struct.unpack(fmt) You can see a working example of something similar in my tutor in the handling files topic in the binary files section: In our case we know it must be like the one we created in formatAddress(), namely 'iNs' where N is a variable number. How do we determine the value of N? The struct module provides some helper functions that return the size of each data type, so by firing up the Python prompt and experimenting we can find out how many bytes of data we will get back for each data type: >>> import struct >>> print struct.calcsize('i') 4 >>> print struct.calcsize('s') 1 Ok, we know that our data will comprise 4 bytes for the number and one byte for each character. So N will be the length of the data minus 4. Let's try using that to read our file: import struct f = file('address.bin','rb') data = f.read() f.close() fmtString = "i%ds" % (len(data) - 4) number, rest = struct.unpack(fmtString, data) address = ' '.join((str(number),rest)) print "Address after restoring data:", addressHTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Sun Dec 21 14:41:51 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 21 Dec 2008 08:41:51 -0500 Subject: [Tutor] Compiling Python 2.6.1 on Leopard In-Reply-To: References: Message-ID: <1c2a2c590812210541x1c1d84ffh9388ac7003b72046@mail.gmail.com> On Sat, Dec 20, 2008 at 3:40 PM, R. Ellsworth Pollard wrote: > Where might I find instructions for compiling Python on Leopard? If your goal is just to install Python, there is now a Mac installer available. http://www.python.org/download/releases/2.6.1/ If you really want to build, download the source and see the Mac/README file. A basic framework build is ./configure --enable-framework make sudo make install If you want it to work with Tkinter, there is a problem in the released setup.py, see this bug for a fix: http://bugs.python.org/issue4017 in particular http://bugs.python.org/msg74544 Kent From simon_ecc at yahoo.co.uk Sun Dec 21 03:02:33 2008 From: simon_ecc at yahoo.co.uk (ppaarrkk) Date: Sat, 20 Dec 2008 18:02:33 -0800 (PST) Subject: [Tutor] Equivalent of grep in python Message-ID: <21111356.post@talk.nabble.com> The following works : file1 = open (file0, "r") re.findall ( 'some_text', file1.readline() ) But this doesn't : re.findall ( 'some_text', file1.readlines() ) How do I use grep for a whole text file, not just a single string ? -- View this message in context: http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html Sent from the Python - tutor mailing list archive at Nabble.com. From katcipis at inf.ufsc.br Sun Dec 21 14:57:14 2008 From: katcipis at inf.ufsc.br (Tiago Katcipis) Date: Sun, 21 Dec 2008 11:57:14 -0200 Subject: [Tutor] Equivalent of grep in python In-Reply-To: <21111356.post@talk.nabble.com> References: <21111356.post@talk.nabble.com> Message-ID: <60a9403b0812210557l3f411cc6m2d11a6386bb39026@mail.gmail.com> i believe that the following should work file1 = open(fileO, 'r') re.findall ('some_text', file1.read()) readlines returns a list with lists inside, where every list is a line of the text. The read function returns the entire file as one string, so it should work to what you are wanting to do. best regards Katcipis On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk wrote: > > The following works : > > file1 = open (file0, "r") > > re.findall ( 'some_text', file1.readline() ) > > > But this doesn't : > > re.findall ( 'some_text', file1.readlines() ) > > > > How do I use grep for a whole text file, not just a single string ? > -- > View this message in context: > http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov -------------- next part -------------- An HTML attachment was scrubbed... URL: From katcipis at inf.ufsc.br Sun Dec 21 14:58:30 2008 From: katcipis at inf.ufsc.br (Tiago Katcipis) Date: Sun, 21 Dec 2008 11:58:30 -0200 Subject: [Tutor] Equivalent of grep in python In-Reply-To: <60a9403b0812210557l3f411cc6m2d11a6386bb39026@mail.gmail.com> References: <21111356.post@talk.nabble.com> <60a9403b0812210557l3f411cc6m2d11a6386bb39026@mail.gmail.com> Message-ID: <60a9403b0812210558k7d5cf276t799f46576c3bace8@mail.gmail.com> i forgot, this might help you http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis wrote: > i believe that the following should work > > file1 = open(fileO, 'r') > re.findall ('some_text', file1.read()) > > readlines returns a list with lists inside, where every list is a line of > the text. The read function returns the entire file as one string, so it > should work to what you are wanting to do. > > best regards > > Katcipis > > > On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk wrote: > >> >> The following works : >> >> file1 = open (file0, "r") >> >> re.findall ( 'some_text', file1.readline() ) >> >> >> But this doesn't : >> >> re.findall ( 'some_text', file1.readlines() ) >> >> >> >> How do I use grep for a whole text file, not just a single string ? >> -- >> View this message in context: >> http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html >> Sent from the Python - tutor mailing list archive at Nabble.com. >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > "it might be a profitable thing to learn Java, but it has no intellectual > value whatsoever" Alexander Stepanov > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov -------------- next part -------------- An HTML attachment was scrubbed... URL: From katcipis at inf.ufsc.br Sun Dec 21 20:14:00 2008 From: katcipis at inf.ufsc.br (Tiago Katcipis) Date: Sun, 21 Dec 2008 17:14:00 -0200 Subject: [Tutor] Equivalent of grep in python In-Reply-To: References: <21111356.post@talk.nabble.com> <60a9403b0812210557l3f411cc6m2d11a6386bb39026@mail.gmail.com> <60a9403b0812210558k7d5cf276t799f46576c3bace8@mail.gmail.com> Message-ID: <60a9403b0812211114x3132e867j615805c3e9539b48@mail.gmail.com> Sorry its true, i made a mistake. Readlines is a list with all the lines inside. I never used readlines (i usually use read), i just read about it on the tutorial long time ago. >>> f.readlines() ['This is the first line of the file.\n', 'Second line of the file\n' Thanks for the help. On Sun, Dec 21, 2008 at 4:44 PM, Luke Paireepinart wrote: > I believe readlines returns a list of strings, not a list of lists. > You can iterate over the characters in a string if you want, though. > > On 12/21/08, Tiago Katcipis wrote: > > i forgot, this might help you > > > > > http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files > > > > > > On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis > > wrote: > > > i believe that the following should work > > > > > > file1 = open(fileO, 'r') > > > re.findall ('some_text', file1.read()) > > > > > > readlines returns a list with lists inside, where every list is a line > of > > the text. The read function returns the entire file as one string, so it > > should work to what you are wanting to do. > > > > > > best regards > > > > > > Katcipis > > > > > > > > > > > > > > > > > > On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk > wrote: > > > > > > > > > > > The following works : > > > > > > > > file1 = open (file0, "r") > > > > > > > > re.findall ( 'some_text', file1.readline() ) > > > > > > > > > > > > But this doesn't : > > > > > > > > re.findall ( 'some_text', file1.readlines() ) > > > > > > > > > > > > > > > > How do I use grep for a whole text file, not just a single string ? > > > > -- > > > > View this message in context: > > > http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html > > > > Sent from the Python - tutor mailing list archive at Nabble.com. > > > > > > > > _______________________________________________ > > > > Tutor maillist - Tutor at python.org > > > > http://mail.python.org/mailman/listinfo/tutor > > > > > > > > > > > > > > > > -- > > > "it might be a profitable thing to learn Java, but it has no > intellectual > > value whatsoever" Alexander Stepanov > > > > > > > > > > > -- > > "it might be a profitable thing to learn Java, but it has no intellectual > > value whatsoever" Alexander Stepanov > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov -------------- next part -------------- An HTML attachment was scrubbed... URL: From msh at blisses.org Sun Dec 21 21:31:43 2008 From: msh at blisses.org (Matt Herzog) Date: Sun, 21 Dec 2008 15:31:43 -0500 Subject: [Tutor] Equivalent of grep in python Message-ID: <20081221203143.GB20229@chicago.blisses.org> ----- Forwarded message from Tiago Katcipis ----- i forgot, this might help you http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files I can't help wondering how to do this in python: perl -wnl -e '/string/ and print;' filename(s) Not that I want to forget the precious few bits of Perl I do know, but I'd rather be totally reliant on python for such tasks. On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis wrote: > i believe that the following should work > > file1 = open(fileO, 'r') > re.findall ('some_text', file1.read()) > > readlines returns a list with lists inside, where every list is a line of > the text. The read function returns the entire file as one string, so it > should work to what you are wanting to do. > > best regards > > Katcipis > > > On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk wrote: > >> >> The following works : >> >> file1 = open (file0, "r") >> >> re.findall ( 'some_text', file1.readline() ) >> >> >> But this doesn't : >> >> re.findall ( 'some_text', file1.readlines() ) >> >> >> >> How do I use grep for a whole text file, not just a single string ? >> -- >> View this message in context: >> http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21111356.html >> Sent from the Python - tutor mailing list archive at Nabble.com. >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > "it might be a profitable thing to learn Java, but it has no intellectual > value whatsoever" Alexander Stepanov > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor ----- End forwarded message ----- -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From simon_ecc at yahoo.co.uk Sun Dec 21 21:00:29 2008 From: simon_ecc at yahoo.co.uk (ppaarrkk) Date: Sun, 21 Dec 2008 12:00:29 -0800 (PST) Subject: [Tutor] Equivalent of grep in python In-Reply-To: <21111356.post@talk.nabble.com> References: <21111356.post@talk.nabble.com> Message-ID: <21118578.post@talk.nabble.com> file1.read() works. What do I do if I want line numbers ? I found this code : http://snippets.dzone.com/posts/show/1638 src = open('2.htm').read() pattern = '

([^<]+)' # or anything else for m in re.finditer(pattern, src): start = m.start() lineno = src.count('\n', 0, start) + 1 offset = start - src.rfind('\n', 0, start) word = m.group(1) print "2.htm(%s,%s): %s" % (lineno, offset, word) Is there not a simpler way than this ? ppaarrkk wrote: > > The following works : > > file1 = open (file0, "r") > > re.findall ( 'some_text', file1.readline() ) > > > But this doesn't : > > re.findall ( 'some_text', file1.readlines() ) > > > > How do I use grep for a whole text file, not just a single string ? > -- View this message in context: http://www.nabble.com/Equivalent-of-grep-in-python-tp21111356p21118578.html Sent from the Python - tutor mailing list archive at Nabble.com. From david at abbottdavid.com Mon Dec 22 00:51:48 2008 From: david at abbottdavid.com (David) Date: Sun, 21 Dec 2008 18:51:48 -0500 Subject: [Tutor] subprocess adds %0A to end of string Message-ID: <494ED694.7010506@abbottdavid.com> Hi everyone. Just learning :) I have one program to parse a podcast feed and put it into a file. #!/usr/bin/python """Get feed date and link details""" import feedparser import sys def getFeed(): url = raw_input("Please enter the feed: ") data = feedparser.parse(url) for entry in data.entries: sys.stdout = open("podcast.txt", "a") print '%s: %s' % (entry.updated, entry.link) sys.stdout.close() for entry in data.entries: sys.stdout = open("podcast_links.txt", "a") print '%s' % (entry.link) sys.stdout.close() getFeed() next to get the latest item; #!/usr/bin/python """read the last entry and download podcast""" import subprocess fname = "podcast.txt" f = open(fname, 'r') print "The Latest Podcast", f.readline() print f.readline() f.close() lname = "podcast_links.txt" L = open(lname, 'r') print "The Latest Link\n" download = L.readline() print download answer = raw_input("Do you want to download the podcast? ") if answer == "y": wget = "wget" subprocess.call([wget, download]) else: print "oops" and here is the output; david [06:37 PM] opteron ~ $ ./py_read_podcast.py The Latest Podcast Sat, 20 Dec 2008 01:52:00 GMT: http://linuxcrazy.com/podcasts/LC-44-arne.mp3 The Latest Link http://linuxcrazy.com/podcasts/LC-44-arne.mp3 Do you want to download the podcast? y --2008-12-21 18:38:46-- http://linuxcrazy.com/podcasts/LC-44-arne.mp3%0A Resolving linuxcrazy.com... 74.220.207.171 Connecting to linuxcrazy.com|74.220.207.171|:80... connected. HTTP request sent, awaiting response... 404 Not Found 2008-12-21 18:38:46 ERROR 404: Not Found. See how it adds the %0A to the end ? I am just trying to get it to work, I know it needs a lot of work, error checking etc. And I am sure there are better ways, I am new. thanks -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From alan.gauld at btinternet.com Mon Dec 22 01:08:03 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Dec 2008 00:08:03 -0000 Subject: [Tutor] Equivalent of grep in python References: <20081221203143.GB20229@chicago.blisses.org> Message-ID: "Matt Herzog" wrote > I can't help wondering how to do this in python: > > perl -wnl -e '/string/ and print;' filename(s) The first thing to say is that python is not Perl so there will be things Perl does more easily than Python and vice versa. (For example Pythons intersactive mode is miles better than perls "read from stdin" mode) Perl is better than python for quick n dirty one liners, no question. And for every Perl one liner there will be a relatively short script that can be written. But you will probably be as well using Perl for those - its not a bad thing to know and use more than one language, quite the opposite. > Not that I want to forget the precious few bits of Perl I do know, > but I'd rather be totally reliant on python for such tasks. Why? I'd hate to be totally reliant on any one language for anything Whether it be Python, Lisp, C or assembler! To answer your question I think the answer to your one liner would look something like: import fileinput # for iterating over several files for line in fileinput.input(inplace=1): # files = argv[1:] try: line.replace('string', 'edit') print line except IOError: continue # ignore bad filenames This also should help the OP answer his question about how to generate filenames, line numbers etc - use fileinput and the filename(), filelineno() etc functions... Not as short as Perl but more maintainable. If you really must do a one liner its probably possible although it will be longer in Python, but I'm not even going to attempt it - thats what awk/perl are there for! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From david at abbottdavid.com Mon Dec 22 01:29:23 2008 From: david at abbottdavid.com (David) Date: Sun, 21 Dec 2008 19:29:23 -0500 Subject: [Tutor] subprocess adds %0A to end of string In-Reply-To: <494ED694.7010506@abbottdavid.com> References: <494ED694.7010506@abbottdavid.com> Message-ID: <494EDF63.4090704@abbottdavid.com> This seems to work; download = L.readline() print download download = download[0:-1] What is that last character that is added; .mp3%0A -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From kent37 at tds.net Mon Dec 22 02:19:41 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 21 Dec 2008 20:19:41 -0500 Subject: [Tutor] subprocess adds %0A to end of string In-Reply-To: <494EDF63.4090704@abbottdavid.com> References: <494ED694.7010506@abbottdavid.com> <494EDF63.4090704@abbottdavid.com> Message-ID: <1c2a2c590812211719x73afaec1la2a6d49dc312bc81@mail.gmail.com> On Sun, Dec 21, 2008 at 7:29 PM, David wrote: > This seems to work; > > download = L.readline() > print download > download = download[0:-1] > > What is that last character that is added; > .mp3%0A It is a newline character (line feed). readline() includes the line endings in the returned lines. Try download = L.readline().rstrip() Kent From mwalsh at mwalsh.org Mon Dec 22 02:25:52 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Sun, 21 Dec 2008 19:25:52 -0600 Subject: [Tutor] subprocess adds %0A to end of string In-Reply-To: <494ED694.7010506@abbottdavid.com> References: <494ED694.7010506@abbottdavid.com> Message-ID: <494EECA0.8010900@mwalsh.org> Hi David, David wrote: > Hi everyone. > Just learning :) I have one program to parse a podcast feed and put it > into a file. Welcome! > > def getFeed(): > url = raw_input("Please enter the feed: ") > data = feedparser.parse(url) > for entry in data.entries: > sys.stdout = open("podcast.txt", "a") You should probably try to avoid reassigning sys.stdout. This is usually a bad idea, and can cause odd behavior that is difficult to troubleshoot, especially for a beginner. A reasonable approach is to assign the open file object to a name of your own choosing... .>>> podcast_file = open('podcast.txt', 'a') ... and then, use the write method of the file object ... .>>> podcast_file.write('%s: %s' % (entry.updated, entry.link)) More info here: http://www.python.org/doc/2.5.3/tut/node9.html#SECTION009200000000000000000 > print '%s: %s' % (entry.updated, entry.link) > sys.stdout.close() > for entry in data.entries: > sys.stdout = open("podcast_links.txt", "a") > print '%s' % (entry.link) > sys.stdout.close() > getFeed() > > next to get the latest item; > > lname = "podcast_links.txt" > L = open(lname, 'r') > print "The Latest Link\n" > download = L.readline() The readline method returns a line from the file *including* the newline character(s) ('\n'). > print download > > answer = raw_input("Do you want to download the podcast? ") > if answer == "y": > wget = "wget" > subprocess.call([wget, download]) > else: > print "oops" OK. There's the problem. Let's assume that after 'download = L.readline()' that download equals this (you can confirm by adding a 'print repr(download)'): 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n' ... then the call becomes (behind the scenes) subprocess.call(['wget', 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n']) ... so the newline is passed as part of the first argument to the wget command. Not so coincidentally, the '%0A' represents a newline ('\n') in a properly quoted/escaped URL. .>>> import urllib2 .>>> urllib2.unquote('%0A') '\n' I suspect it is the wget command which is quoting the newline, not the subprocess call, as subprocess doesn't know anything about valid characters for urls. You can work around this problem as you already have by dropping the last character as in 'download[:-1]', or use the strip (or rstrip) str method: .>>> download.rstrip() 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3' More info here: http://www.python.org/doc/2.5.3/lib/string-methods.html HTH, Marty From david at abbottdavid.com Mon Dec 22 03:18:46 2008 From: david at abbottdavid.com (David) Date: Sun, 21 Dec 2008 21:18:46 -0500 Subject: [Tutor] subprocess adds %0A to end of string In-Reply-To: <494EECA0.8010900@mwalsh.org> References: <494ED694.7010506@abbottdavid.com> <494EECA0.8010900@mwalsh.org> Message-ID: <494EF906.2040509@abbottdavid.com> Martin Walsh wrote: > > Welcome! > thanks > You should probably try to avoid reassigning sys.stdout. This is usually > a bad idea, and can cause odd behavior that is difficult to > troubleshoot, especially for a beginner. A reasonable approach is to > assign the open file object to a name of your own choosing... > > .>>> podcast_file = open('podcast.txt', 'a') > > ... and then, use the write method of the file object ... > > .>>> podcast_file.write('%s: %s' % (entry.updated, entry.link)) > > More info here: > http://www.python.org/doc/2.5.3/tut/node9.html#SECTION009200000000000000000 > > >> print '%s: %s' % (entry.updated, entry.link) >> sys.stdout.close() >> for entry in data.entries: >> sys.stdout = open("podcast_links.txt", "a") >> print '%s' % (entry.link) >> sys.stdout.close() >> getFeed() This "podcast_file.write('%s: %s' % (entry.updated, entry.link))" writes it in one very long string The Latest Link http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas=>> and sys.stdout prints to the file one line at a time How do I split the long string, not even sure if that is the correct term? for i in somefile: ? re.somefile(do_something) ? -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From alan.gauld at btinternet.com Mon Dec 22 03:38:42 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Dec 2008 02:38:42 -0000 Subject: [Tutor] subprocess adds %0A to end of string References: <494ED694.7010506@abbottdavid.com> <494EECA0.8010900@mwalsh.org> <494EF906.2040509@abbottdavid.com> Message-ID: "David" wrote >>> sys.stdout = open("podcast_links.txt", "a") >>> print '%s' % (entry.link) >>> sys.stdout.close() >>> getFeed() > This "podcast_file.write('%s: %s' % (entry.updated, entry.link))" > writes it in one very long string Use podcastfile.writeline() to write it line by line. Read any tutorial on file handling to understand how/why these various methods work. Get to know files, you'll use them a lot! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From mwalsh at mwalsh.org Mon Dec 22 06:18:11 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Sun, 21 Dec 2008 23:18:11 -0600 Subject: [Tutor] subprocess adds %0A to end of string In-Reply-To: <494EF906.2040509@abbottdavid.com> References: <494ED694.7010506@abbottdavid.com> <494EECA0.8010900@mwalsh.org> <494EF906.2040509@abbottdavid.com> Message-ID: <494F2313.6090807@mwalsh.org> David wrote: > Martin Walsh wrote: > >> Welcome! > > thanks welcome (uh oh, infinite loop warning) > This "podcast_file.write('%s: %s' % (entry.updated, entry.link))" > writes it in one very long string Copy and paste gets me every time. Try this, and note the presence of the newline ('\n'): podcast_file.write('%s: %s\n' % (entry.updated, entry.link)) > > The Latest Link > http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas=>> > > > and sys.stdout prints to the file one line at a time The primary issue is not that you're writing to sys.stdout, it's that you're using the print statement which implicitly adds a newline. Technically you can also do the following (python <= 2.5, and maybe 2.6) which should demonstrate the concept... podcastfile = file(somepath, 'a') print >> podcastfile, '%s: %s' (entry.updated, entry.link) ... however, I would recommend against using the above print statement syntax for anything other than experimentation because 1) it's not common practice IMHO, and 2) it's gone starting with python 3.0. 'print' will be a builtin function instead of a statement going forward. I second Alan's recommendation to read a tutorial or two to solidify your understanding of the basic concepts. Alan's tutorial is very good, and covers everything we have discussed so far, particularly the "Handling Files" section. Check the link in his email sig. HTH, Marty From jfabiani at yolo.com Mon Dec 22 16:18:41 2008 From: jfabiani at yolo.com (johnf) Date: Mon, 22 Dec 2008 07:18:41 -0800 Subject: [Tutor] python parser Message-ID: <200812220718.41305.jfabiani@yolo.com> I've been in the programming business for over 20 years and I have never had a need for a parser. But recently I have need to convert code from one language to a my new language python. What a better way to learn the new language python than a new project. I decided it might be time to learn a little something about parsers too. However, I soon discovered that I was walking into the world of compiler writers and theories of computer scientist. I paid for and downloaded a paper from O'Reilly books on what I thought was going to be on 'pyparser'. But that turned out to be mostly theory. And nothing about the use of pyparser. So I ask you guys is there a link to a practical tutorial that provides hands on information on the use of a python parser. I'd like to see something that demo's converting a real language to python. When I google parsers I have found a few simple code examples of parsing a float. Not really much help (I could have done that using Regex) when you want to parse 'if,then' statements that can be recursive. Thanks in advance. -- John Fabiani From bryan.fodness at gmail.com Mon Dec 22 16:45:22 2008 From: bryan.fodness at gmail.com (Bryan Fodness) Date: Mon, 22 Dec 2008 10:45:22 -0500 Subject: [Tutor] Script to take a screenshot of my website. Message-ID: I would like to take a screenshot of my website without opening the browser or importing extra packages. Is there a way to do this? I would like to create a function like screenshot(' http://www.scatterworks.com") -- "The game of science can accurately be described as a never-ending insult to human intelligence." - Jo?o Magueijo -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Dec 22 18:15:13 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 22 Dec 2008 12:15:13 -0500 Subject: [Tutor] python parser In-Reply-To: <200812220718.41305.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> Message-ID: <494FCB21.606@gmail.com> johnf wrote: > I've been in the programming business for over 20 years and I have never had a > need for a parser. But recently I have need to convert code from one > language to a my new language python. Pray tell: what is the other language, and why do you want to convert programs? I assume you want to convert so you can then modify / extend the old programs. If you just plan to run them in Python "as is" I see no value in converting! It is hard to map programs in other languages to Python because Python is so different. A direct translation would not take advantage of python's uniqueness. > What a better way to learn the new > language python than a new project. I decided it might be time to learn a > little something about parsers too. However, I soon discovered that I was > walking into the world of compiler writers and theories of computer > scientist. I paid for and downloaded a paper from O'Reilly books on what I > thought was going to be on 'pyparser'. But that turned out to be mostly > theory. And nothing about the use of pyparser. > > So I ask you guys is there a link to a practical tutorial that provides hands > on information on the use of a python parser. I'd like to see something that > demo's converting a real language to python. When I google parsers I have > found a few simple code examples of parsing a float. Not really much help (I > could have done that using Regex) when you want to parse 'if,then' statements > that can be recursive. > FWIW I have written a parser for parts of the CMS Pipelines language. I provide a form of BNF for the syntax, generate parsers from that, run a "program" through that, which generates lists or dictionaries. I have no need to create a program from that. The Pipelines language is very simple, so I am not dealing with control structures, just sequences of words and symbols. -- Bob Gailer Chapel Hill NC 919-636-4239 From mwalsh at mwalsh.org Mon Dec 22 18:23:55 2008 From: mwalsh at mwalsh.org (Martin Walsh) Date: Mon, 22 Dec 2008 11:23:55 -0600 Subject: [Tutor] Script to take a screenshot of my website. In-Reply-To: References: Message-ID: <494FCD2B.4090604@mwalsh.org> Bryan Fodness wrote: > I would like to take a screenshot of my website without opening the > browser or importing extra packages. Is there a way to do this? Unless you're keen on writing your own html/css/javascript/etc rendering engine, I'm pretty sure you're going to need extra package(s), or a browser (even if opened w/o a visible ui). Perhaps these links will provide inspiration... http://lapin-blanc.net/09/11/2008/django-website-thumbnail-generator/ http://www.gruppo4.com/~tobia/pythumbnail.shtml HTH, Marty From andreas at kostyrka.org Mon Dec 22 18:39:01 2008 From: andreas at kostyrka.org (Andreas Kostyrka) Date: Mon, 22 Dec 2008 18:39:01 +0100 Subject: [Tutor] python parser In-Reply-To: <200812220718.41305.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> Message-ID: <20081222183901.72a31e3f@andi-lap> Am Mon, 22 Dec 2008 07:18:41 -0800 schrieb johnf : > I've been in the programming business for over 20 years and I have > never had a need for a parser. But recently I have need to convert > code from one language to a my new language python. What a better > way to learn the new language python than a new project. I decided > it might be time to learn a little something about parsers too. > However, I soon discovered that I was walking into the world of > compiler writers and theories of computer scientist. I paid for and > downloaded a paper from O'Reilly books on what I thought was going to > be on 'pyparser'. But that turned out to be mostly theory. And > nothing about the use of pyparser. Well, scanning and parsing are rather one of the older fields in C.S. So if I got that correct you want to convert programs from language A to language Python, and on tops you'll probably want the generated program to be human-readable and as fast as possible. Without knowing what A is, it's hard to tell you more, but writing a parser for almost any programming language is hard, and emitting fast and human readable code is even harder. Furthermore, doing it without the theoretical background is not simpler either. Btw, possible implementations of a parser depend upon a number of "theoretical" properties of the grammer you want to implement. > > So I ask you guys is there a link to a practical tutorial that > provides hands on information on the use of a python parser. I'd > like to see something that demo's converting a real language to > python. When I google parsers I have found a few simple code > examples of parsing a float. Not really much help (I could have done > that using Regex) when you want to parse 'if,then' statements that > can be recursive. Take a look at the examples at the pyparsing website, it even includes a python parser :) Andreas > > Thanks in advance. From jfabiani at yolo.com Mon Dec 22 18:39:37 2008 From: jfabiani at yolo.com (johnf) Date: Mon, 22 Dec 2008 09:39:37 -0800 Subject: [Tutor] python parser In-Reply-To: <494FCB21.606@gmail.com> References: <200812220718.41305.jfabiani@yolo.com> <494FCB21.606@gmail.com> Message-ID: <200812220939.37750.jfabiani@yolo.com> On Monday 22 December 2008 09:15:13 am bob gailer wrote: > johnf wrote: > > I've been in the programming business for over 20 years and I have never > > had a need for a parser. But recently I have need to convert code from > > one language to a my new language python. > > Pray tell: what is the other language, and why do you want to convert > programs? > > I assume you want to convert so you can then modify / extend the old > programs. If you just plan to run them in Python "as is" I see no value > in converting! > > It is hard to map programs in other languages to Python because Python > is so different. A direct translation would not take advantage of > python's uniqueness. > > > What a better way to learn the new > > language python than a new project. I decided it might be time to learn > > a little something about parsers too. However, I soon discovered that I > > was walking into the world of compiler writers and theories of computer > > scientist. I paid for and downloaded a paper from O'Reilly books on what > > I thought was going to be on 'pyparser'. But that turned out to be > > mostly theory. And nothing about the use of pyparser. > > > > So I ask you guys is there a link to a practical tutorial that provides > > hands on information on the use of a python parser. I'd like to see > > something that demo's converting a real language to python. When I > > google parsers I have found a few simple code examples of parsing a > > float. Not really much help (I could have done that using Regex) when > > you want to parse 'if,then' statements that can be recursive. > > FWIW I have written a parser for parts of the CMS Pipelines language. I > provide a form of BNF for the syntax, generate parsers from that, run a > "program" through that, which generates lists or dictionaries. I have no > need to create a program from that. > > The Pipelines language is very simple, so I am not dealing with control > structures, just sequences of words and symbols. Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers) to Python and Postgres (functions and triggers). Actually, python is very close to VFP and I would convert very well. MsSQL is another question. It has been suggested that I just read one line at a time and convert. I could do that but I did want to expand my understanding by using a parser. And the one line at a time does not lend it self to control structures. Even if I do a get the convert to work there would still be a need for hand coding. But it would provide a good start. So far no links to tutorials? -- John Fabiani From eduardo.susan at gmail.com Mon Dec 22 19:33:10 2008 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Mon, 22 Dec 2008 11:33:10 -0700 Subject: [Tutor] Very basic question about lists Message-ID: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> Hello, I'm trying to teach my self programming with python and there are some basic things that stumps me: Given this code: ### list1 = ['arr', 'bre', 'grau', 'lower', 'tudo'] for item in list1: if 'arr' in item: print list1 ### The output is (as expected): ['arr', 'bre', 'grau', 'lower', 'tudo'] If I change my condition to: if 'arr' or 'bell' in item: or to this: if 'arr' or 'grau' in item: I get this result: ['arr', 'bre', 'grau', 'lower', 'tudo'] ['arr', 'bre', 'grau', 'lower', 'tudo'] ['arr', 'bre', 'grau', 'lower', 'tudo'] ['arr', 'bre', 'grau', 'lower', 'tudo'] ['arr', 'bre', 'grau', 'lower', 'tudo'] Why this? I guess I'm not grasping the use of AND and OR Thanks, Eduardo From alan.gauld at btinternet.com Mon Dec 22 19:34:12 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Dec 2008 18:34:12 -0000 Subject: [Tutor] python parser References: <200812220718.41305.jfabiani@yolo.com> Message-ID: "johnf" wrote > So I ask you guys is there a link to a practical tutorial that > provides hands > on information on the use of a python parser. One place to loook for all things to do with processing text is Mertz' Text Processing in Python. He discuissed parsers in some detail although, infuriatingly, doesn't spend a lot of time on the standard library parsers. But the principles are common and the modules he uses readily available. Its available online at: http://gnosis.cx/TPiP/ HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Mon Dec 22 19:39:53 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Dec 2008 18:39:53 -0000 Subject: [Tutor] Script to take a screenshot of my website. References: Message-ID: "Bryan Fodness" wrote > I would like to take a screenshot of my website without opening the > browser I dont really know what you mean by this? The appearance of a web site depends on many factors, not least the size of the browser window. But different browsers render pages differently too - and thats not due to bugs its deliberately included in the concept of HTML. - it is not a page layout language. Indeed if viewed in a text browser like lynx it will look very different! But even something "standard" like IE looks totally different between Mac and PC versions. And thats without taking account of user preferences over the colours of hyperlinks, font styles and sizes, security settings, etc etc. So I'm not sure what you think you could capture without knowing what computer, OS and browser (and the user settings thereof) was going to be used... Alan G. From bgailer at gmail.com Mon Dec 22 19:42:24 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 22 Dec 2008 13:42:24 -0500 Subject: [Tutor] python parser In-Reply-To: <200812220939.37750.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> <494FCB21.606@gmail.com> <200812220939.37750.jfabiani@yolo.com> Message-ID: <494FDF90.1040306@gmail.com> johnf wrote: > On Monday 22 December 2008 09:15:13 am bob gailer wrote: > >> johnf wrote: >> >>> I've been in the programming business for over 20 years and I have never >>> had a need for a parser. But recently I have need to convert code from >>> one language to a my new language python. >>> >> Pray tell: what is the other language, and why do you want to convert >> programs? >> >> I assume you want to convert so you can then modify / extend the old >> programs. If you just plan to run them in Python "as is" I see no value >> in converting! >> >> It is hard to map programs in other languages to Python because Python >> is so different. A direct translation would not take advantage of >> python's uniqueness. >> >> >>> What a better way to learn the new >>> language python than a new project. I decided it might be time to learn >>> a little something about parsers too. However, I soon discovered that I >>> was walking into the world of compiler writers and theories of computer >>> scientist. I paid for and downloaded a paper from O'Reilly books on what >>> I thought was going to be on 'pyparser'. But that turned out to be >>> mostly theory. And nothing about the use of pyparser. >>> >>> So I ask you guys is there a link to a practical tutorial that provides >>> hands on information on the use of a python parser. I'd like to see >>> something that demo's converting a real language to python. When I >>> google parsers I have found a few simple code examples of parsing a >>> float. Not really much help (I could have done that using Regex) when >>> you want to parse 'if,then' statements that can be recursive. >>> >> FWIW I have written a parser for parts of the CMS Pipelines language. I >> provide a form of BNF for the syntax, generate parsers from that, run a >> "program" through that, which generates lists or dictionaries. I have no >> need to create a program from that. >> >> The Pipelines language is very simple, so I am not dealing with control >> structures, just sequences of words and symbols. >> > > Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers) > to Python and Postgres (functions and triggers). Actually, python is very > close to VFP and I would convert very well. MsSQL is another question. > I started writing a VFP - Python converter a few years ago. I will see if I have it around. > It has been suggested that I just read one line at a time and convert. I > could do that but I did want to expand my understanding by using a parser. > And the one line at a time does not lend it self to control structures. > > Even if I do a get the convert to work there would still be a need for hand > coding. But it would provide a good start. > > So far no links to tutorials? > -- Bob Gailer Chapel Hill NC 919-636-4239 From jfabiani at yolo.com Mon Dec 22 19:42:09 2008 From: jfabiani at yolo.com (johnf) Date: Mon, 22 Dec 2008 10:42:09 -0800 Subject: [Tutor] python parser In-Reply-To: References: <200812220718.41305.jfabiani@yolo.com> Message-ID: <200812221042.09085.jfabiani@yolo.com> On Monday 22 December 2008 10:34:12 am Alan Gauld wrote: > "johnf" wrote > > > So I ask you guys is there a link to a practical tutorial that > > provides hands > > on information on the use of a python parser. > > One place to loook for all things to do with processing text is Mertz' > Text > Processing in Python. He discuissed parsers in some detail although, > infuriatingly, doesn't spend a lot of time on the standard library > parsers. > But the principles are common and the modules he uses readily > available. > > Its available online at: > > http://gnosis.cx/TPiP/ > > HTH, Interesting - I'll start reading right now. -- John Fabiani From kent37 at tds.net Mon Dec 22 19:55:38 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 22 Dec 2008 13:55:38 -0500 Subject: [Tutor] python parser In-Reply-To: <200812220939.37750.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> <494FCB21.606@gmail.com> <200812220939.37750.jfabiani@yolo.com> Message-ID: <1c2a2c590812221055u59464978of2ff27ce04691d7e@mail.gmail.com> On Mon, Dec 22, 2008 at 12:39 PM, johnf wrote: > I paid for and downloaded a paper from O'Reilly books on what I > thought was going to be on 'pyparser'. But that turned out to be mostly > theory. And nothing about the use of pyparser. Was that "Getting Started with Pyparsing" ? I would be very surprised if that was all theory, it is written by the author of pyparsing and he likes examples :-) and the ToC looks like it is mostly extended examples. > So far no links to tutorials? David Mertz has written some columns on parsing, see http://www.ibm.com/developerworks/views/linux/libraryview.jsp?sort_by=Relevance&show_abstract=true&show_all=false&search_flag=true&topic_by=All+topics+and+related+products&type_by=Articles&search_by=charming+python&Submit.x=0&Submit.y=0 You might also try his book: http://gnosis.cx/TPiP/ Kent From bermanrl at cfl.rr.com Mon Dec 22 20:00:02 2008 From: bermanrl at cfl.rr.com (Robert Berman) Date: Mon, 22 Dec 2008 14:00:02 -0500 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> Message-ID: <494FE3B2.6070908@cfl.rr.com> An HTML attachment was scrubbed... URL: From ravikondamuru at gmail.com Mon Dec 22 20:16:11 2008 From: ravikondamuru at gmail.com (Ravi Kondamuru) Date: Mon, 22 Dec 2008 11:16:11 -0800 Subject: [Tutor] reading output from a c executable. In-Reply-To: <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> References: <36601b010812111002t3b635c4bsc8c1815af3477618@mail.gmail.com> <36601b010812111004x2e390ad3t741c25f2f4b21071@mail.gmail.com> <49415B8A.3080701@alchemy.com> <36601b010812111035q3039ef62r1d5758c2f2fd7858@mail.gmail.com> <49415FDF.3080601@alchemy.com> <36601b010812111059k5ac76735r4a1273a4fefeeaaa@mail.gmail.com> <4941E06E.1090608@gmail.com> <36601b010812120313k6d727537w140316aebb16b843@mail.gmail.com> <49427573.5020902@gmail.com> <36601b010812201154m6cfd91f9i5ea7d80bd4f2ef6@mail.gmail.com> Message-ID: <36601b010812221116q64bf8291wfc3cdd7e6689a1a2@mail.gmail.com> I tried what Bill Campbell suggested: read the len first and then use that to populate the structdef length field for the string len = xstruct.structdef(xstruct.little_endian, [ ('len', (xstruct. unsigned_long, 1)), ]) l = len(buf[0:3]) rec = xstruct.structdef(xstruct.little_endian, [ ('len', (xstruct. unsigned_long, 1)), ('id', (xstruct.unsigned_long, 1)), ('name', (xstruct.string, (l.len-8))), ]) n = rec(buf[0:l.len]) print n.len, n.id, n.name On Sat, Dec 20, 2008 at 11:54 AM, Ravi Kondamuru wrote: > I am trying to use xstruct module to unpack a varaible size record with the > following structure. > > struct nameid { > u32bits len /* total length */ > u32bits id; > char name; /* name variable length */ > } > > As can be seen the length of the name = len - (sizeof(len) + sizeof(id)). > > How do I use xstruct or struct to unpack such a structure? > > n = xstruct.structdef(xstruct.little_endian, [ > ('len', (xstruct. unsigned_long, 1)), > ('id', (xstruct.unsigned_long, 1)), > ('name', (xstruct.string, )), > ]) > > xstruct seems to expect the exact len to be specified in structdef. Is > there a way to tell it to go till the end of the buffer passed? > > thanks, > Ravi. > > On Fri, Dec 12, 2008 at 6:30 AM, bob gailer wrote: > >> Ravi Kondamuru wrote: >> >>> Denis, These are 32bit, 64bit counters (essentially numbers). >>> Bob, There are well over 10K counters in the log file that are updated >>> every 5 secs. If a counter1's graph was requested, log will have to be >>> parsed once to get the data points. If a user asked for a counter2, now it >>> needs to be retrieved from the log file. Which essentially means having to >>> go through the logfile again. This is because i want to try to avoid using a >>> database to store values after parsing the file. >>> >> Here is a little test program I wrote to check timing on a file of >> *100,000* 64 bit counters. >> >> import time, struct >> ctrs = 100000 >> s = time.time() >> # create a file of 800,000 characters (100,000 64 bit numbers) >> f = open('ravi.dat', 'wb') >> for i in range(ctrs): >> f.write('abcdefgh') # it does not matter what we write as long as it is 8 >> bytes. >> print time.time() - s >> >> s = time.time() >> l = [] >> # read the file >> f = open('ravi.dat', 'rb').read() >> >> # unpack each 8 characters into an integer and collect in a list >> for b in range(0, ctrs, 8): >> n = struct.unpack('q', f[b:b+8]) >> l.append(n) >> print time.time() - s >> >> Writing the file took 0.14 seconds on my computer >> Reading and unpacking 0.04 seconds. >> I think you can set performance issues aside. >> >> There is a principle in programming that proposes: >> 1 - get a program running. Preferably in Python as development time is >> much faster. >> 2 - check its performance. >> 3 - refine as needed. >> >> >> -- >> Bob Gailer >> Chapel Hill NC 919-636-4239 >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wescpy at gmail.com Mon Dec 22 20:36:25 2008 From: wescpy at gmail.com (wesley chun) Date: Mon, 22 Dec 2008 11:36:25 -0800 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> Message-ID: <78b3a9580812221136l251a3556o4403c334982f44d9@mail.gmail.com> eduardo, welcome to programming, and even better, welcome to Python! you've done your research and found a list of great people who can help you out. with regards to your question, my comment are below... > list1 = ['arr', 'bre', 'grau', 'lower', 'tudo'] > for item in list1: > if 'arr' in item: > print list1 > ### > The output is (as expected): > ['arr', 'bre', 'grau', 'lower', 'tudo'] you need to question your output here. sure it is what you *want*, but i don't think you got it the way you originally thought. what are you trying to do? are you trying to see whether the string 'arr' is in the entire list, or are you comparing one element at a time and checking if a string is found in a larger string? (your code is doing the latter.) here is some code (and output) which will hopefully make it clear: >>> list1 = ['arr', 'bre', 'grau', 'lower', 'tudo'] >>> 'arr' in list1 True >>> 'bell' in list1 False >>> 'grau' in list1 True the "in" operator checks if an entire string is in a list or not. you do not have to loop through it to get an answer. now let's iterate through each string. >>> for item in list1: ... print item ... arr bre grau lower tudo notice that item represents each string in the list in each iteration of the loop. if you are asking whether 'arr' is in item, you are asking if 'arr' is in 'arr', 'arr' is in 'bre', 'arr' is in 'grau', etc., so your (single) output is due to the fact that on the first pass, 'arr' is in 'arr'. in other words, you did this: >> tmp = 'arr' >>> 'arr' in tmp True >>> 'arr' in 'arr' True so that's why your list printed out, because 'arr' in 'arr' returned True. so, to really illustrate what you're doing, take a look at this example: >>> for item in list1: ... if 'owe' in item: ... print list1 ... ['arr', 'bre', 'grau', 'lower', 'tudo'] this time, the output *is* expected, because 'owe' is *in* one of the 5 strings in your list... in particular, 'owe' is in 'lower', so again, this is why list1 is printed, because that statement is True. > Why this? I guess I'm not grasping the use of AND and OR i don't think you have a problem with AND and OR... i think your problem was with IN instead. :-) remember, for strings, IN means whether a substring is found in a larger string (or not), and for lists, IN means whether an object is found in the list (or not). 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 garry.bettle at gmail.com Mon Dec 22 20:27:46 2008 From: garry.bettle at gmail.com (Garry Bettle) Date: Mon, 22 Dec 2008 20:27:46 +0100 Subject: [Tutor] python parser Message-ID: On Mon, 22 Dec 2008 09:39:37 -0800, johnf wrote: > Well I'm attempting to convert VFP and MsSQL (stored procedures and triggers) > to Python and Postgres (functions and triggers). Actually, python is very > close to VFP and I would convert very well. MsSQL is another question. Hi John, I hope this message finds you well. You might be interested in the following: http://dabodev.com/ "Dabo is a 3-tier, cross-platform application development framework, written in Python atop the wxPython GUI toolkit. And while Dabo is designed to create database-centric apps, that is not a requirement. Lots of people are using Dabo for the GUI tools to create apps that have no need to connect to a database at all." NB: The VFP-angle is well and truely catered for: "Background Dabo's authors, Ed Leafe and Paul McNett, have strong backgrounds in database application development using the awesome and underrated Microsoft Visual FoxPro development environment. While Visual FoxPro shines at developing data-centric applications, it has one limitation that cannot be ignored: it only runs on Microsoft Windows, and Ed and Paul both have clients that want their applications to run on Linux and Macintosh. We are sure we are not alone in this regard: it is a multi-platform world with more diverse needs than one vendor can fulfill. Ed and Paul got to talking one day: Paul had been researching various multiplatform GUI toolkits for about 18 months, and Ed has lots of experience developing the Visual FoxPro Codebook framework. We decided to work together to make a framework for developing robust data-centric applications for multi-platform deployment. We've come up with a design that is simple, flexible, and robust, and we've begun developing our own client applications using the Dabo framework." G. From kent37 at tds.net Mon Dec 22 20:56:16 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 22 Dec 2008 14:56:16 -0500 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> Message-ID: <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> On Mon, Dec 22, 2008 at 1:33 PM, Eduardo Vieira wrote: > if 'arr' or 'bell' in item: The interpreter sees this as if ('arr') or ('bell' in item): 'arr' always evaluates to True so the condition is always true. The correct way to express this condition is if 'arr' in item or 'bell' in item: Kent From wescpy at gmail.com Mon Dec 22 21:19:40 2008 From: wescpy at gmail.com (wesley chun) Date: Mon, 22 Dec 2008 12:19:40 -0800 Subject: [Tutor] Very basic question about lists In-Reply-To: <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> Message-ID: <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> >> if 'arr' or 'bell' in item: > > The interpreter sees this as > if ('arr') or ('bell' in item): > > 'arr' always evaluates to True so the condition is always true. The > correct way to express this condition is > if 'arr' in item or 'bell' in item: arrgh. yes, i saw this too but forgot to mention it in my reply. the reason why it is, as kent has stated is that any non-empty container, i.e., 'arr', always evaluates to Boolean True. the only string that has a False value is the empty string, or "". in addition, i think (and i may be wrong about this) that he really wanted to do: if 'arr' in list1 or 'bell' in list1... "in item" is a substring check and not a list membership query. -- wesley From denis.spir at free.fr Mon Dec 22 21:36:40 2008 From: denis.spir at free.fr (spir) Date: Mon, 22 Dec 2008 21:36:40 +0100 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> Message-ID: <1229978200.7389.24.camel@o> Le lundi 22 d?cembre 2008 ? 11:33 -0700, Eduardo Vieira a ?crit : > Hello, I'm trying to teach my self programming with python and there > are some basic things that stumps me: > Given this code: > ### > list1 = ['arr', 'bre', 'grau', 'lower', 'tudo'] > for item in list1: > if 'arr' in item: > print list1 > ### > The output is (as expected): > ['arr', 'bre', 'grau', 'lower', 'tudo'] > > If I change my condition to: > if 'arr' or 'bell' in item: > or to this: > if 'arr' or 'grau' in item: > > I get this result: > ['arr', 'bre', 'grau', 'lower', 'tudo'] > ['arr', 'bre', 'grau', 'lower', 'tudo'] > ['arr', 'bre', 'grau', 'lower', 'tudo'] > ['arr', 'bre', 'grau', 'lower', 'tudo'] > ['arr', 'bre', 'grau', 'lower', 'tudo'] > > Why this? I guess I'm not grasping the use of AND and OR Actually, you are right, I guess. There are here two rather subtile issues. (In addition to the questions about what you really *mean* with this piece of code): -1- What python understands reading "if 'arr' or 'bell' in item:" -2- What logical value python given to several kinds of objects. About the first point, notice that you write an 'or' in the middle of a logical expression: if 'arr' *or* 'bell' in item Then, the problem for any reader, be it human or mechanical, is to guess what you intend to relate with 'or'. On left side, no problem, there is only 'arr'. On right hand, you probably mean to englobe only 'bell', but how does python know that? To solve this question, any language defines an operator precedence (priority) order -- which is nearly the same for all computer languages and inherits from conventional logic. Below the table for python: =================================== Operators and their evaluation order Highest Operator Comment , [...] {...} `...` Tuple, list & dict. creation; string conv. s[i] s[i:j] s.attr f(...) indexing & slicing; attributes, fct calls +x, -x, ~x Unary operators x**y Power x*y x/y x%y mult, division, modulo x+y x-y addition, substraction x<>y Bit shifting x&y Bitwise and x^y Bitwise exclusive or x|y Bitwise or xy x>=y x==y x!=y x<>y x is y x is not y x in s x not in s Comparison, identity, membership not x boolean negation x and y boolean and x or y boolean or Lowest lambda args: expr anonymous function ================================= As you can see, 'or' is very low, which means that it will be aplied very late in any logical expression. 'in' comes befores. As you can now guess, this means that your expression is equal to: if ('arr') or ('bell' in item) Lookind at the table, you will understand that, for instance: if a and b or c <==> if (a and b) or c if a or b and c <==> if a or (b and c) Right? Now, to the second issue -- which is /according to me/ (others will not agree) a serious default of python: in addition to logical literals (True and False) and results of logical operators (and, or, in,...), python gives a boolean logical value to *all* objects. Which means that: if ( "todu b?m!" and 3 or file("/home/spir/example.txt",'r') ) is perfectly valid in python. Actually, python considers that anything that is not "nul" (zero, or empty) has a True logical value. So that 'arr' is True for python. Then your expression above is always True whatever the right side of the 'or' operator. for item in list1: if 'arr' or (whatever you like): print list1 Your loop will test the condition 5 times, because there are 5 items in the list. The condition will be True 5 times: the list will print 5 times. Note that you stepped right from the start on a very tricky trap of python: most of the language items, and examples and trials you may find or do will work in a coherent way, meaning the way you expect -- if ever you have a consistent view of the problem. Ecco! Denis > Thanks, > > Eduardo > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From eike.welk at gmx.net Mon Dec 22 21:54:27 2008 From: eike.welk at gmx.net (Eike Welk) Date: Mon, 22 Dec 2008 21:54:27 +0100 Subject: [Tutor] python parser In-Reply-To: <200812220939.37750.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> <494FCB21.606@gmail.com> <200812220939.37750.jfabiani@yolo.com> Message-ID: <200812222154.27480.eike.welk@gmx.net> Hi John! I have written a (currently mostly defunct) compiler in Python for a specialized programming language. The parser may serve as an example for you. Introduction: http://freeode.berlios.de/ The parser: https://svn.berlios.de/wsvn/freeode/trunk/freeode_py/freeode/simlparser.py The method --- Parser._defineLanguageSyntax --- creates the Pyparsing object. There are gigantic amounts of (mostly correct) comments in the file. Excuse the bad English and the typos. The style is somewhat adapted to the IDE Pydev. The parser reads the program text (no scanner) and creates a tree structure from it. I call it AST or Parse Tree (I can't decide how to call it). This tree is used in subsequent passes of the compiler. I use a modified version of Pyparsing where I fixed things that I considered bugs, and where I implemented some of my wishes: https://svn.berlios.de/wsvn/freeode/trunk/freeode_py/freeode/third_party/pyparsing.py Kind regards, Eike. From eduardo.susan at gmail.com Mon Dec 22 22:27:02 2008 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Mon, 22 Dec 2008 14:27:02 -0700 Subject: [Tutor] Very basic question about lists In-Reply-To: <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> Message-ID: <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> Wesley wrote: On Mon, Dec 22, 2008 at 1:19 PM, wesley chun wrote: > > in addition, i think (and i may be wrong about this) that he really > wanted to do: > > if 'arr' in list1 or 'bell' in list1... > Thanks for all the replies. Yes that was actually what I meant. My mistake too was that I gave you all a wrong example to explain my problem, sorry. The following example will explain better: list1 = ['ar', 'fir', 'wo'] list2 = ['ber', 'gar', 'gt'] list3 = ['hu', 'mo', 'ko', 'tr'] list4 = ['q', 'wer', 'duh'] whole = [list1, list2, list3, list4] for item in whole: if 'ar' or 'ko' in item: print item So, the unexpected result was that I got all lists printed, when I expected only list1 and list3. Now, I don't know if I still understand the explanation given by spir -- I'll reread more carefully--, but I know that the code to get what I expect is this instead: for item in whole: for word in item: if word == 'ar' or word == 'ko': print item I see I have to do a loop inside a loop and that this the right expression if word == 'ar' or word == 'ko': but this is not: if word == 'ar' or 'ko': From bgailer at gmail.com Tue Dec 23 00:03:44 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 22 Dec 2008 18:03:44 -0500 Subject: [Tutor] python parser In-Reply-To: <200812221042.09085.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> <200812221042.09085.jfabiani@yolo.com> Message-ID: <49501CD0.4010903@gmail.com> More thoughts on converting VFP to Python: line-by-line can take into account control structures. One just increases or decreases the indent when encountering the start or end of a structure. a bigger challenge is handling the work-area related commands such as Scan, Replace, Skip, Set Relation, .... the concept of data sessions macro substitution all the GUI stuff would be translated to one of the python GUI packages. Not a trivial task! An outline: create initial dictionary of all initial special characters, keywords including their abbreviations, ...? create dictionary to hold all user-generated names (variables, classes, procedures, windows, ...?) awareness while scanning of #define and blocks such as text .. endtext to ignore scan file for procedure/function/define certain initial characters take precedence = ? & # * && @ \ this list may be extended temporarily by commands like text ; is a special final character assignment statement? initial word a keyword or function? if none of the above error? and on and on. -- Bob Gailer Chapel Hill NC 919-636-4239 From wescpy at gmail.com Tue Dec 23 00:36:11 2008 From: wescpy at gmail.com (wesley chun) Date: Mon, 22 Dec 2008 15:36:11 -0800 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> Message-ID: <78b3a9580812221536i64252c4csad369c992b104911@mail.gmail.com> > list1 = ['ar', 'fir', 'wo'] > list2 = ['ber', 'gar', 'gt'] > list3 = ['hu', 'mo', 'ko', 'tr'] > list4 = ['q', 'wer', 'duh'] > > whole = [list1, list2, list3, list4] > for item in whole: > if 'ar' or 'ko' in item: > print item > > So, the unexpected result was that I got all lists printed, when I > expected only list1 and list3. Now, I don't know if I still understand > the explanation given by spir -- I'll reread more carefully--, but I > know that the code to get what I expect is this instead: > for item in whole: > for word in item: > if word == 'ar' or word == 'ko': > print item > > I see I have to do a loop inside a loop and that this the right expression > if word == 'ar' or word == 'ko': > > but this is not: > if word == 'ar' or 'ko': correct, but i *still* don't think you need to do an inner loop... it seems you're just trying to see whether a word is in the list, not checking for substrings. in fact, the lists can be inside another list too. couple this with *not* looping through each list looking for something, we have the following: >>> whole = [ ... ['ar', 'fir', 'wo'], ... ['ber', 'gar', 'gt'], ... ['hu', 'mo', 'ko', 'tr'], ... ['q', 'wer', 'duh'], ... ] >>> >>> for item in whole: ... if 'ar' in item or 'ko' in item: ... print item ... ['ar', 'fir', 'wo'] ['hu', 'mo', 'ko', 'tr'] there... those are list1 and list3 right? again, the secret is in IN. :-) 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 steve at alchemy.com Tue Dec 23 01:01:31 2008 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 22 Dec 2008 16:01:31 -0800 Subject: [Tutor] Very basic question about lists In-Reply-To: <78b3a9580812221536i64252c4csad369c992b104911@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> <78b3a9580812221536i64252c4csad369c992b104911@mail.gmail.com> Message-ID: <49502A5B.6020306@alchemy.com> >> if 'ar' or 'ko' in item: This is incorrect. What you meant to say was: if 'ar' in item or 'ko' in item: or something equivalent to that. "if 'ar' or 'ko' in item" means "if ('ar') is True or ('ko' in item) is True". Since 'ar' is True anyway, you'll get a match every time. From eric at ericabrahamsen.net Tue Dec 23 08:10:58 2008 From: eric at ericabrahamsen.net (Eric Abrahamsen) Date: Tue, 23 Dec 2008 15:10:58 +0800 Subject: [Tutor] more encoding strangeness Message-ID: <2DBE563D-846C-4C96-9580-6951B675B32F@ericabrahamsen.net> Hi there, I'm configuring a python command to be used by emacs to filter a buffer through python markdown, and noticed something strange. If I run this command in the terminal: python -c "import sys,markdown; print markdown.markdown(sys.stdin.read().decode('utf-8'))" < markdown_source.md The file (which is encoded as utf-8 and contains Chinese characters) is converted and output correctly to the terminal. But if I do this to write the output to a file: python -c "import sys,markdown; print markdown.markdown(sys.stdin.read().decode('utf-8'))" < markdown_source.md > output.hml I get a UnicodeEncodeError, 'ascii' codec can't encode character u'\u2014'. I'm not sure where exactly this is going wrong, as print and sys.stdout.write() and whatnot don't provide encoding parameters. What's the difference between this command writing to the terminal, and writing to the file? Thanks, Eric From kent37 at tds.net Tue Dec 23 12:40:45 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 23 Dec 2008 06:40:45 -0500 Subject: [Tutor] more encoding strangeness In-Reply-To: <2DBE563D-846C-4C96-9580-6951B675B32F@ericabrahamsen.net> References: <2DBE563D-846C-4C96-9580-6951B675B32F@ericabrahamsen.net> Message-ID: <1c2a2c590812230340g1494ed02v406b1599ab21a9e8@mail.gmail.com> On Tue, Dec 23, 2008 at 2:10 AM, Eric Abrahamsen wrote: > Hi there, > > I'm configuring a python command to be used by emacs to filter a buffer > through python markdown, and noticed something strange. If I run this > command in the terminal: > > python -c "import sys,markdown; print > markdown.markdown(sys.stdin.read().decode('utf-8'))" < markdown_source.md > > The file (which is encoded as utf-8 and contains Chinese characters) is > converted and output correctly to the terminal. But if I do this to write > the output to a file: > > python -c "import sys,markdown; print > markdown.markdown(sys.stdin.read().decode('utf-8'))" < markdown_source.md > > output.hml > > I get a UnicodeEncodeError, 'ascii' codec can't encode character u'\u2014'. > I'm not sure where exactly this is going wrong, as print and > sys.stdout.write() and whatnot don't provide encoding parameters. What's the > difference between this command writing to the terminal, and writing to the > file? sys.stdout does have an encoding: In [1]: import sys In [2]: sys.stdout.encoding Out[2]: 'UTF-8' I think print converts to the encoding of stdout, e.g. In [3]: print u'\u2014' ? Probably when you pipe the output, sys.stdout.encoding is ascii so the conversion fails. The simple solution is to convert explicitly: print markdown.markdown(sys.stdin.read().decode('utf-8')).encode('utf-8') Kent From denis.spir at free.fr Tue Dec 23 13:16:47 2008 From: denis.spir at free.fr (spir) Date: Tue, 23 Dec 2008 13:16:47 +0100 Subject: [Tutor] Very basic question about lists In-Reply-To: <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> References: <9356b9f30812221033k6536cdcetd8b195c6b4be23f3@mail.gmail.com> <1c2a2c590812221156m248b2a08g1cfaf098d41c7e18@mail.gmail.com> <78b3a9580812221219x79bfd03am526cf28172925e64@mail.gmail.com> <9356b9f30812221327t2d474866n47415604efdaa76b@mail.gmail.com> Message-ID: <1230034607.5833.55.camel@o> > I see I have to do a loop inside a loop and that this the right expression > if word == 'ar' or word == 'ko': > > but this is not: > if word == 'ar' or 'ko': In the last example: as the 'or' operator has the least priority, it will be applied last. Which means that all other operations in the expression will be done first. In this case, there is only one, namely (word=='ar'): if ( (word == 'ar') or ('ko') ): So that, in order to evaluate the or operation, python needs to know whether either (word=='ar') or ('ko') is True. If either is True, the whole is True too: this is precisely the sense of 'or'. Right? * on left side, (word == 'ar') is true if word equals 'ar' * on right side, 'ko' is true if... what? This is the second point of my explanation. Conceptually, only logical (boolean) values, and operations on these values, could/should be allowed in logical expressions. But python also implicitely gives a logic value to everything: * numbers equal to zero are False * empty containers (list, string) are False * anything else is considered True if 0 --> False if "" --> False if [] --> False if 1 --> True if [1,2,3] --> True if 'abc' --> True if 'a' and x=1 --> True if x==1, because 'a' is True if 'a' or x=1 --> alwaysTtrue, because 'a' is True if word == 'ar' or 'ko' --> always True if 'ar' or 'ko' in seq --> always True, because 'ar' is True Note: This comes from old times of programmation when False was represented by 0 and True by 1, -1, or any non-zero value (like in python). This allowed very practicle and "clever" programming tricks, but difficult to understand (as you see). Because conceptually wrong, in my opinion. Python even extended that by giving a False value not only to numbers equal to zero, but also to empty sets. This allows to write, for instance: if number: # means: if number not null do something if sequence: # means: if sequence not empty do something if max_number and total References: <20934673.post@talk.nabble.com> <20081210140534.GM13722@inocybe.teonanacatl.org> <20980334.post@talk.nabble.com> <20081216045037.GA12325@inocybe.teonanacatl.org> <21071143.post@talk.nabble.com> Message-ID: <21143262.post@talk.nabble.com> Your advice is spot on and I'm well on my way... your Windows installation advice should be in the readme, if anyone's listening :-) Thanks again, Gareth -- View this message in context: http://www.nabble.com/MP3Info-class-usage-tp20934673p21143262.html Sent from the Python - tutor mailing list archive at Nabble.com. From Jaggojaggo+Py at gmail.com Tue Dec 23 15:31:54 2008 From: Jaggojaggo+Py at gmail.com (Omer) Date: Tue, 23 Dec 2008 16:31:54 +0200 Subject: [Tutor] Fwd: Class Extend Help In-Reply-To: <515008f10812220958y39399988k1fd2d080cb7b327b@mail.gmail.com> References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> <515008f10812220958y39399988k1fd2d080cb7b327b@mail.gmail.com> Message-ID: <515008f10812230631p348d3abdt949cdabc80c55acd@mail.gmail.com> Hey! Thx for the help, @Martin, At the moment my only need is basic retrievals of source code of basic files from the internet, so no need for urllib2 or response objects just yet. Now, copying and pasting line by line into the interactive mode I get this: >>> from urllib import urlopen >>> class fetch(urlopen): ... def __init__(self,*args): ... urlopen.__init__(self, *args) ... self.content = self.read() ... Traceback (most recent call last): File "", line 1, in TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str Anybody, any idea what's up with that? On Sat, Dec 20, 2008 at 6:49 PM, Richard Lovely wrote: > There are three ways as I see it: using __getattr__, using a new init, > or using a property decorator. The last two are probably the most > pythonic, but I'm not familiar with decorators, so here's how I'd do > the second of the three: > > try: > from google.appengine.api.urlfetch import fetch > except: > from urllib import urlopen > > class fetch(urlopen): > def __init__(self, *args): > urlopen.__init__(self, *args) > self.content = self.read() > > > This probably doesn't behave exactly the same way as the google class, > but it should be similar enough? > --- > Richard "Roadie Rich" Lovely, part of the JNP|UK Famile > www.theJNP.com > > > > 2008/12/20 Omer >: > > Hey. > > > > I'm trying to do something I think is basic and am failing. > > > > The goal is: > > [mimicking the google urlopen syntax] > > > > try: > > from google.appengine.api.urlfetch import fetch > > except: > > from urllib import urlopen as fetch > > > > > > How do I add this "fetch" the property of content? > > > > I basically want fetch(url.com).content to replace urlopen(url.com > ).read() > > > > Mind, content is not a method. > > > > Thoughts? > > > > Thx. Omer. > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 23 16:23:23 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 23 Dec 2008 10:23:23 -0500 Subject: [Tutor] Fwd: Class Extend Help In-Reply-To: <515008f10812230631p348d3abdt949cdabc80c55acd@mail.gmail.com> References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> <515008f10812220958y39399988k1fd2d080cb7b327b@mail.gmail.com> <515008f10812230631p348d3abdt949cdabc80c55acd@mail.gmail.com> Message-ID: <1c2a2c590812230723i336e8b3fo93c782d3dc0cdb57@mail.gmail.com> On Tue, Dec 23, 2008 at 9:31 AM, Omer wrote: >>>> from urllib import urlopen >>>> class fetch(urlopen): > ... def __init__(self,*args): > ... urlopen.__init__(self, *args) > ... self.content = self.read() > ... > > Traceback (most recent call last): > File "", line 1, in > TypeError: Error when calling the metaclass bases > function() argument 1 must be code, not str > > Anybody, any idea what's up with that? That's pretty obscure, not sure why you get exactly that error! Anyway the code should be class fetch(object): urlopen is a function, not a class, so it can't be a base class of fetch. There is no need for it to be a base class, either; just use object. Kent From mujilias at gmail.com Tue Dec 23 16:30:37 2008 From: mujilias at gmail.com (moham ilias) Date: Tue, 23 Dec 2008 21:00:37 +0530 Subject: [Tutor] help needed from python beginner Message-ID: <74b58e470812230730n4c3c5d1eg9f44a87b1bf92d8@mail.gmail.com> can anybody help me to get the example codes for some existing python projects. pls help in this regard -------------- next part -------------- An HTML attachment was scrubbed... URL: From roadierich at googlemail.com Tue Dec 23 17:03:41 2008 From: roadierich at googlemail.com (Richard Lovely) Date: Tue, 23 Dec 2008 16:03:41 +0000 Subject: [Tutor] Fwd: Class Extend Help In-Reply-To: <1c2a2c590812230723i336e8b3fo93c782d3dc0cdb57@mail.gmail.com> References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> <515008f10812220958y39399988k1fd2d080cb7b327b@mail.gmail.com> <515008f10812230631p348d3abdt949cdabc80c55acd@mail.gmail.com> <1c2a2c590812230723i336e8b3fo93c782d3dc0cdb57@mail.gmail.com> Message-ID: OK, going on Kent's post: from urllib import urlopen class fetch(object): def __init__(self, *args): self.response = urlopen(*args) self.content = self.response.read() I forgot urlopen was a function. It made sense in my head for it to be a class, instead of _returning_ a class. It's trying to call __init__ on a functionType instance, and it's getting confused. See http://dpaste.com/hold/101951/ for another example. The error message is something cryptic, but between #python and I we think it's something to do with calling type(). --- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com 2008/12/23 Kent Johnson : > On Tue, Dec 23, 2008 at 9:31 AM, Omer wrote: > >>>>> from urllib import urlopen >>>>> class fetch(urlopen): >> ... def __init__(self,*args): >> ... urlopen.__init__(self, *args) >> ... self.content = self.read() >> ... >> >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: Error when calling the metaclass bases >> function() argument 1 must be code, not str >> >> Anybody, any idea what's up with that? > > That's pretty obscure, not sure why you get exactly that error! > > Anyway the code should be > class fetch(object): > > urlopen is a function, not a class, so it can't be a base class of > fetch. There is no need for it to be a base class, either; just use > object. > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Tue Dec 23 19:09:53 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 23 Dec 2008 13:09:53 -0500 Subject: [Tutor] help needed from python beginner In-Reply-To: <74b58e470812230730n4c3c5d1eg9f44a87b1bf92d8@mail.gmail.com> References: <74b58e470812230730n4c3c5d1eg9f44a87b1bf92d8@mail.gmail.com> Message-ID: <1c2a2c590812231009l34d238abk1109aa25dda66c68@mail.gmail.com> On Tue, Dec 23, 2008 at 10:30 AM, moham ilias wrote: > can anybody help me to get the example codes for some existing python > projects. pls help in this regard I'm not sure I understand your question. Do you want examples to look at? Most of the Python standard library is written in Python so that is a good source for examples. You can also look at projects in the Python Package Index. Sourceforge and Google Code also have many Python projects. http://pypi.python.org/pypi http://sourceforge.net/search/?type_of_search=soft&words=python http://code.google.com/hosting/search?q=label%3aPython Kent From alan.gauld at btinternet.com Tue Dec 23 19:58:44 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 23 Dec 2008 18:58:44 -0000 Subject: [Tutor] help needed from python beginner References: <74b58e470812230730n4c3c5d1eg9f44a87b1bf92d8@mail.gmail.com> Message-ID: "moham ilias" wrote in message news:74b58e470812230730n4c3c5d1eg9f44a87b1bf92d8 at mail.gmail.com... > can anybody help me to get the example codes for some existing > python > projects. pls help in this regard > For simple examples try the Useless Python web site. In particular check out the link to the old site, it has more examples than the new site last time I looked... -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From msh at blisses.org Tue Dec 23 21:10:29 2008 From: msh at blisses.org (Matt Herzog) Date: Tue, 23 Dec 2008 15:10:29 -0500 Subject: [Tutor] optparse Message-ID: <20081223201029.GA29361@chicago.blisses.org> Hi All. I want to write a script that will emulate grep to some extent. This is just an exercise for me. I want to run the script like this: ./pythongrep directory searchstring Just like grep, I want it to print: filename, instance_of_match As of now, the script can't find anything I tell it to look for. It prints, "None." Thanks for any advice. import optparse import os import re def main(): p = optparse.OptionParser(description="Python grepper", prog="greppar", version="0.1", usage="usage: %prog directory regex") options, arguments = p.parse_args() if len(arguments) == 2: directory = arguments[0] regex = arguments[1] s = re.compile(regex) for filename in os.listdir(directory): result = re.match(s, filename) print result else: p.print_help() if __name__ == '__main__': main() -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From bgailer at gmail.com Tue Dec 23 21:47:31 2008 From: bgailer at gmail.com (bob gailer) Date: Tue, 23 Dec 2008 15:47:31 -0500 Subject: [Tutor] python parser In-Reply-To: <200812222159.49108.jfabiani@yolo.com> References: <200812220718.41305.jfabiani@yolo.com> <200812221042.09085.jfabiani@yolo.com> <49501CD0.4010903@gmail.com> <200812222159.49108.jfabiani@yolo.com> Message-ID: <49514E63.4010408@gmail.com> I'm forwarding this to the list. Please always reply-all so a copy goes to the list. johnf wrote: > On Monday 22 December 2008 03:03:44 pm you wrote: > >> More thoughts on converting VFP to Python: >> >> line-by-line can take into account control structures. One just >> increases or decreases the indent when encountering the start or end of >> a structure. >> >> a bigger challenge is handling the work-area related commands such as >> Scan, Replace, Skip, Set Relation, .... >> > > Actually Bob I'm using Dabo which has the above covered. You should take a > close look at Dabo. I am able to do most everything I've ever done in VFP. > I don't use IDE Dabo provides and I'm able to get everything done. At this > point I can almost copy any VFP form. > >> the concept of data sessions >> > > So far I'm able to read the dbc and have most of what is required for the data > sessions > >> macro substitution >> > > That looks like a problem for the moment. > >> all the GUI stuff would be translated to one of the python GUI packages. >> Not a trivial task! >> > > So far it has not been to bad. The real issue is converting to sizers. But > the windows side complete. > >> An outline: >> >> create initial dictionary of all initial special characters, keywords >> including their abbreviations, ...? >> create dictionary to hold all user-generated names (variables, classes, >> procedures, windows, ...?) >> awareness while scanning of #define and blocks such as text .. endtext >> to ignore >> scan file for procedure/function/define >> >> certain initial characters take precedence = ? & # * && @ \ >> this list may be extended temporarily by commands like text >> ; is a special final character >> >> assignment statement? >> initial word a keyword or function? >> if none of the above error? >> >> and on and on. >> > > If I had a decent parser I think the rest would be easy. But I don't and I do > not understand the all the CS stuff. > > -- Bob Gailer Chapel Hill NC 919-636-4239 From kent37 at tds.net Tue Dec 23 22:22:29 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 23 Dec 2008 16:22:29 -0500 Subject: [Tutor] optparse In-Reply-To: <20081223201029.GA29361@chicago.blisses.org> References: <20081223201029.GA29361@chicago.blisses.org> Message-ID: <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> On Tue, Dec 23, 2008 at 3:10 PM, Matt Herzog wrote: > for filename in os.listdir(directory): > result = re.match(s, filename) > print result You never open and read the files. You are searching for the pattern in the filename, not in the contents of the file. If there is no match, result will be None. You need to test it and print it only if it is not None. (Actually I don't think you want to print the match object, you want to print the filename and the line of the file that matches. But one thing at a time...) This test and print should be inside the "for filename" loop; your print statement is outside the loop. Kent From alan.gauld at btinternet.com Wed Dec 24 02:12:55 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Dec 2008 01:12:55 -0000 Subject: [Tutor] optparse References: <20081223201029.GA29361@chicago.blisses.org> <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> Message-ID: "Kent Johnson" wrote >> for filename in os.listdir(directory): >> result = re.match(s, filename) >> print result > > You never open and read the files. You are searching for the pattern > in the filename, not in the contents of the file. Also note that match() only searches starting at the start of the string. Thus match will find foo at foobar but not in sofoo You usually need to use search() to find the pattern anywhere within the string. Also look at the thread earlier this week on using listdir() and the fileinput module. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From ajarncolin at gmail.com Wed Dec 24 02:20:21 2008 From: ajarncolin at gmail.com (col speed) Date: Wed, 24 Dec 2008 08:20:21 +0700 Subject: [Tutor] help finding recurring cycle Message-ID: <6a1b26420812231720h92e2655qc8cebe1a2ecfdfff@mail.gmail.com> Hello there, I am learning python as a hobby in my spare time. I enjoy doing "project euler", not that I am any good at maths, but it gives me problems to solve in python! Please Note: I do not expect (or want) you to give me the solution, if you could just point me in the right direction - that would be great. The problem is as follows: *A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:* [image: ^(]*1/2*[image: )][image: _(][image: )]*= **0.5* [image: ^(]*1/3*[image: )][image: _(][image: )]*= **0.(3)* [image: ^(]*1/4*[image: )][image: _(][image: )]*= **0.25* [image: ^(]*1/5*[image: )][image: _(][image: )]*= **0.2* [image: ^(]*1/6*[image: )][image: _(][image: )]*= **0.1(6)* [image: ^(]*1/7*[image: )][image: _(][image: )]*= **0.(142857)* [image: ^(]*1/8*[image: )][image: _(][image: )]*= **0.125* [image: ^(]*1/9*[image: )][image: _(][image: )]*= **0.(1)* [image: ^(]*1/10*[image: )][image: _(][image: )]*= **0.1* *Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.*[image: ^(][image: )][image: _(][image: )] *Find the value of **d [image: <] 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.* I've written a "division" function that gives more decimal places than the one already in python. What my poor old brain can't work out is how to find a "recurring cycle" which isn't disastrously complicated (as the cycle doesn't always include the first decimal places and the length is unknown). Any ideas would be greatly appreciated. Thanks in advance Colin [image: ^(][image: )][image: _(][image: )] -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Dec 24 03:38:53 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 23 Dec 2008 21:38:53 -0500 Subject: [Tutor] help finding recurring cycle In-Reply-To: <6a1b26420812231720h92e2655qc8cebe1a2ecfdfff@mail.gmail.com> References: <6a1b26420812231720h92e2655qc8cebe1a2ecfdfff@mail.gmail.com> Message-ID: <1c2a2c590812231838k58a20b3amc544db313ed4dd35@mail.gmail.com> On Tue, Dec 23, 2008 at 8:20 PM, col speed wrote: > I've written a "division" function that gives more decimal places than the > one already in python. What my poor old brain can't work out is how to find > a "recurring cycle" which isn't disastrously complicated (as the cycle > doesn't always include the first decimal places and the length is unknown). I would look for repetition in the remainder (the dividend) at each step. Perhaps keep a dict which maps dividend to position, then when you get a repeat you can figure out how long the cycle is. Kent From bgailer at gmail.com Wed Dec 24 03:48:51 2008 From: bgailer at gmail.com (bob gailer) Date: Tue, 23 Dec 2008 21:48:51 -0500 Subject: [Tutor] help finding recurring cycle In-Reply-To: <1c2a2c590812231838k58a20b3amc544db313ed4dd35@mail.gmail.com> References: <6a1b26420812231720h92e2655qc8cebe1a2ecfdfff@mail.gmail.com> <1c2a2c590812231838k58a20b3amc544db313ed4dd35@mail.gmail.com> Message-ID: <4951A313.6060905@gmail.com> Kent Johnson wrote: > On Tue, Dec 23, 2008 at 8:20 PM, col speed wrote: > > >> I've written a "division" function that gives more decimal places than the >> one already in python. What my poor old brain can't work out is how to find >> a "recurring cycle" which isn't disastrously complicated (as the cycle >> doesn't always include the first decimal places and the length is unknown). >> > > I would look for repetition in the remainder (the dividend) at each > step. Perhaps keep a dict which maps dividend to position, then when > you get a repeat you can figure out how long the cycle is. > > Kent > Check out http://en.wikipedia.org/wiki/Repeating_decimal. Section 5 How a repeating or terminating decimal expansion is found illustrates the above. -- Bob Gailer Chapel Hill NC 919-636-4239 From ballerz4ishi at sbcglobal.net Wed Dec 24 04:17:33 2008 From: ballerz4ishi at sbcglobal.net (Ishan Puri) Date: Tue, 23 Dec 2008 19:17:33 -0800 (PST) Subject: [Tutor] Word Frequency Chart Message-ID: <744263.77070.qm@web83304.mail.sp1.yahoo.com> Hello, I am a beginner with Python but I understand a lot of linguistics. I am a high school student. I needed help (from the beginning) making a word frequency chart that I can use to chart out the numerical frequencies of words. Usually I can understand the code if it is annotated well, but I am not familiar with the functions and so forth. It would be awesome if someone could make a program that would chart frequencies for me. I have the corpora already. If the program could have something like "Type filename:" when I run it that would be fantastic. I have pylab as well, so the code could include something that would make the chart when I type in the filename. That would be great. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ajarncolin at gmail.com Wed Dec 24 06:03:53 2008 From: ajarncolin at gmail.com (col speed) Date: Wed, 24 Dec 2008 12:03:53 +0700 Subject: [Tutor] help finding recurring cycle In-Reply-To: <4951A313.6060905@gmail.com> References: <6a1b26420812231720h92e2655qc8cebe1a2ecfdfff@mail.gmail.com> <1c2a2c590812231838k58a20b3amc544db313ed4dd35@mail.gmail.com> <4951A313.6060905@gmail.com> Message-ID: <6a1b26420812232103k745c965nc449647aa50f8ea1@mail.gmail.com> Thank you, thank you thank you! I'm now well on my way to solving my 35th problem!! The wiki is fantastic, I thought "of course - I should have known that!" It's easy when you know how. Thanks again, especially for the prompt replies 2008/12/24 bob gailer > Kent Johnson wrote: > >> On Tue, Dec 23, 2008 at 8:20 PM, col speed wrote: >> >> >> >>> I've written a "division" function that gives more decimal places than >>> the >>> one already in python. What my poor old brain can't work out is how to >>> find >>> a "recurring cycle" which isn't disastrously complicated (as the cycle >>> doesn't always include the first decimal places and the length is >>> unknown). >>> >>> >> >> I would look for repetition in the remainder (the dividend) at each >> step. Perhaps keep a dict which maps dividend to position, then when >> you get a repeat you can figure out how long the cycle is. >> >> Kent >> >> > > Check out http://en.wikipedia.org/wiki/Repeating_decimal. > > Section 5 How a repeating or terminating decimal expansion is found > illustrates the above. > > -- > Bob Gailer > Chapel Hill NC 919-636-4239 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 24 09:43:32 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Dec 2008 08:43:32 -0000 Subject: [Tutor] Word Frequency Chart References: <744263.77070.qm@web83304.mail.sp1.yahoo.com> Message-ID: "Ishan Puri" wrote > I am a high school student. I needed help (from the beginning) > making > a word frequency chart that I can use to chart out the numerical > frequencies > of words. We are happy to help but as a rile we don;t write programs for you, we help you write your own programs. (Teach a man to fish and you feed him forever etc) > program could have something like "Type filename:" when I run it > that > would be fantastic. OK, You can use raw_input for that. And to capture the counts you can use a dictionary. You will need to: 1) Capture the filename 2) open the file 3) iterate over the file, line by line 4) iterate over each line word by word 5) if the word exists in your dictionary increment the count, else add it to the dictionary with a count of 1 Thats probably enough for a first stage. If you don't know how to do any of the steps above let us know but they are all covered in any of the beginners tutiorials. > I have pylab as well, so the code could include something that would > make the chart when I type in the filename. That would be great. You can make charts directly from Python or you could save the data to a file that some other charting software can read (say Excel). But get the basic daya capture right first, worry about the presentation later. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From norman at khine.net Wed Dec 24 13:25:51 2008 From: norman at khine.net (Norman Khine) Date: Wed, 24 Dec 2008 13:25:51 +0100 Subject: [Tutor] strip function Message-ID: <49522A4F.8050106@khine.net> Hello, I have difficulties in trying to stripping the following: >>> item_path = '/companies/company/news' >>> item_strip = item_path.strip('/companies') >>> item_strip 'y/new' >>> I would like to return: >>> item_strip '/company/news' What is the best way to do this. Thanks From bgailer at gmail.com Wed Dec 24 13:58:50 2008 From: bgailer at gmail.com (bob gailer) Date: Wed, 24 Dec 2008 07:58:50 -0500 Subject: [Tutor] strip function In-Reply-To: <49522A4F.8050106@khine.net> References: <49522A4F.8050106@khine.net> Message-ID: <4952320A.1040408@gmail.com> An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Dec 24 14:09:12 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 24 Dec 2008 08:09:12 -0500 Subject: [Tutor] strip function In-Reply-To: <49522A4F.8050106@khine.net> References: <49522A4F.8050106@khine.net> Message-ID: <1c2a2c590812240509xc509a83kf6cabb5fb2b3677a@mail.gmail.com> On Wed, Dec 24, 2008 at 7:25 AM, Norman Khine wrote: > > Hello, > I have difficulties in trying to stripping the following: > >>>> item_path = '/companies/company/news' >>>> item_strip = item_path.strip('/companies') >>>> item_strip > 'y/new' strip() doesn't do what you want. It removes any of the characters in the given string, in any order. > I would like to return: > >>>> item_strip > '/company/news' > > What is the best way to do this. To remove a fixed string use slicing: item_strip = item_path[len('/companies'):] Kent From juhasecke at googlemail.com Wed Dec 24 15:46:22 2008 From: juhasecke at googlemail.com (Jan Ulrich Hasecke) Date: Wed, 24 Dec 2008 15:46:22 +0100 Subject: [Tutor] Word Frequency Chart In-Reply-To: <744263.77070.qm@web83304.mail.sp1.yahoo.com> References: <744263.77070.qm@web83304.mail.sp1.yahoo.com> Message-ID: <9E2BBE7D-0CDC-420D-ABB2-87C391E5ABA7@googlemail.com> Look at http://www.nltk.org/ Merry Christmas to all! juh -- Business: http://hasecke.com --- Private: http://hasecke.eu --- Blog: http://www.sudelbuch.de --- History: www.generationenprojekt.de --- Europe: www.wikitution.org -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2295 bytes Desc: not available URL: From cbabcock at asciiking.com Wed Dec 24 15:06:53 2008 From: cbabcock at asciiking.com (Chris Babcock) Date: Wed, 24 Dec 2008 07:06:53 -0700 Subject: [Tutor] Word Frequency Chart In-Reply-To: <744263.77070.qm@web83304.mail.sp1.yahoo.com> References: <744263.77070.qm@web83304.mail.sp1.yahoo.com> Message-ID: <20081224070653.57aae034@mail.asciiking.com> On Tue, 23 Dec 2008 19:17:33 -0800 (PST) Ishan Puri wrote: > Hello, > I am a beginner with Python but I understand a lot of > linguistics. I am a high school student. I needed help (from the > beginning) making a word frequency chart that I can use to chart out > the numerical frequencies of words. Usually I can understand the code > if it is annotated well, but I am not familiar with the functions and > so forth. It would be awesome if someone could make a program that > would chart frequencies for me. I have the corpora already. If the > program could have something like "Type filename:" when I run it that > would be fantastic. I have pylab as well, so the code could include > something that would make the chart when I type in the filename. That > would be great. Thank you. The Natural Language Toolkit is the place to look: http://www.nltk.org/ This is a *deep* resource for lexical analysis. You'll probably want to follow the examples in the book first and try them with your corpora as you go. Chris From emadnawfal at gmail.com Wed Dec 24 22:04:21 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Wed, 24 Dec 2008 16:04:21 -0500 Subject: [Tutor] beginsWith multiple prefixes Message-ID: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> Hi Tutors, I want a function that acts like the startswith method, but can take multiple prefixes. As an amateur programmer, I came up with this one, and it works fine, but my experience tells me that my solutions are not always the best ones around. Can you please tell me what a better option might be: def beginsWith(word, listname): count = 0 x = range(len(word)) for i in x: if word[:i] in listname: count+=1 break if count > 0: return True else: return False # main text = "ana mary fify floor school security".split() prefixes = ["an", "ma", "fi", "sec"] for word in text: if beginsWith(word, prefixes): print(word+" (Match)") else: print(word) #This produces the following: IDLE 3.0 ==== No Subprocess ==== >>> ana (Match) mary (Match) fify (Match) floor school security (Match) >>> -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Thu Dec 25 00:04:37 2008 From: bgailer at gmail.com (bob gailer) Date: Wed, 24 Dec 2008 18:04:37 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> Message-ID: <4952C005.3010200@gmail.com> Emad Nawfal (???? ????) wrote: > Hi Tutors, > I want a function that acts like the startswith method, but can take > multiple prefixes. As an amateur programmer, I came up with this one, > and it works fine, but my experience tells me that my solutions are > not always the best ones around. Can you please tell me what a better > option might be: > > > > def beginsWith(word, listname): > count = 0 > x = range(len(word)) > for i in x: > if word[:i] in listname: > count+=1 > > break > > if count > 0: > return True > else: > return False Above does a lot more work than necessary. Try: def beginsWith(word, listname): for prefix in listname: if word.startswith(prefix): return True > > > # main > text = "ana mary fify floor school security".split() > > prefixes = ["an", "ma", "fi", "sec"] > > for word in text: > if beginsWith(word, prefixes): > print(word+" (Match)") > else: > print(word) > > #This produces the following: > IDLE 3.0 ==== No Subprocess ==== > >>> > ana (Match) > mary (Match) > fify (Match) > floor > school > security (Match) > >>> > > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? > ????????.....???? ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > http://emnawfal.googlepages.com > -------------------------------------------------------- > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From bill at celestial.net Thu Dec 25 06:51:12 2008 From: bill at celestial.net (Bill Campbell) Date: Wed, 24 Dec 2008 21:51:12 -0800 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: <4952C005.3010200@gmail.com> References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <4952C005.3010200@gmail.com> Message-ID: <20081225055112.GA31743@ayn.mi.celestial.com> On Wed, Dec 24, 2008, bob gailer wrote: > Emad Nawfal (???? ????) wrote: >> Hi Tutors, >> I want a function that acts like the startswith method, but can take >> multiple prefixes. As an amateur programmer, I came up with this one, >> and it works fine, but my experience tells me that my solutions are >> not always the best ones around. Can you please tell me what a better >> option might be: ... > Above does a lot more work than necessary. Try: > > def beginsWith(word, listname): > for prefix in listname: > if word.startswith(prefix): > return True It might be more useful to return the prefix that matched as the caller already knows what ``word'' is. Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Basic Definitions of Science: If it's green or wiggles, it's biology. If it stinks, it's chemistry. If it doesn't work, it's physics. From emadnawfal at gmail.com Thu Dec 25 12:10:51 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Thu, 25 Dec 2008 06:10:51 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: <20081225055112.GA31743@ayn.mi.celestial.com> References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <4952C005.3010200@gmail.com> <20081225055112.GA31743@ayn.mi.celestial.com> Message-ID: <652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> On 12/25/08, Bill Campbell wrote: > > On Wed, Dec 24, 2008, bob gailer wrote: > > Emad Nawfal (???? ????) wrote: > >> Hi Tutors, > >> I want a function that acts like the startswith method, but can take > >> multiple prefixes. As an amateur programmer, I came up with this one, > >> and it works fine, but my experience tells me that my solutions are > >> not always the best ones around. Can you please tell me what a better > >> option might be: > > ... > > > Above does a lot more work than necessary. Try: > > > > def beginsWith(word, listname): > > for prefix in listname: > > if word.startswith(prefix): > > return True > > > It might be more useful to return the prefix that matched as the > caller already knows what ``word'' is. > > Bill > -- > INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC > URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way > Voice: (206) 236-1676 Mercer Island, WA 98040-0820 > Fax: (206) 232-9186 > > Basic Definitions of Science: > If it's green or wiggles, it's biology. > If it stinks, it's chemistry. > If it doesn't work, it's physics. > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you so much Bob and Bill. This is really much better than mine. Bill's suggestion is not applicable in my current script, but I believe I will need that soon. Thank you both. Thank you all tutors -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 25 12:40:22 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 25 Dec 2008 11:40:22 -0000 Subject: [Tutor] beginsWith multiple prefixes References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com><4952C005.3010200@gmail.com><20081225055112.GA31743@ayn.mi.celestial.com> <652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> Message-ID: "Emad Nawfal (???? ????)" wrote >> >> I want a function that acts like the startswith method, but can >> >> take >> >> multiple prefixes. >> Above does a lot more work than necessary. Try: >> >> def beginsWith(word, listname): >> for prefix in listname: >> if word.startswith(prefix): >> return True You could also make this a method of a subclass of string if you prefer: class MyString(str): def beginswith(self, prefixes): for prefix in prefixes: if self.startswith(prefix): return prefix Now you can create instances of MyString that will have all the regular vstring methiods plus the new beginswith(): s = MyString("Welcome to my world") if s.beginswith(["Welcome", "Hello","Howdy"]): print "It's friendly" Which looks more consistent. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Thu Dec 25 14:17:25 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 25 Dec 2008 08:17:25 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> Message-ID: <1c2a2c590812250517o269f2cbbwb406f154d55eec2b@mail.gmail.com> On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (???? ????) wrote: > Hi Tutors, > I want a function that acts like the startswith method, but can take > multiple prefixes. As an amateur programmer, I came up with this one, and it > works fine, but my experience tells me that my solutions are not always the > best ones around. Can you please tell me what a better option might be: Since Python 2.5, startswith() accepts a tuple as the match parameter, so you can write def beginsWith(word, listname): return word.startswith(tuple(listname)) or just us startswith() directly. Kent From kent37 at tds.net Thu Dec 25 14:20:31 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 25 Dec 2008 08:20:31 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <4952C005.3010200@gmail.com> <20081225055112.GA31743@ayn.mi.celestial.com> <652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> Message-ID: <1c2a2c590812250520r7fe2eb4bnf0b3dda87da92ff9@mail.gmail.com> 2008/12/25 Alan Gauld : > You could also make this a method of a subclass of string if you prefer: > > class MyString(str): > def beginswith(self, prefixes): > for prefix in prefixes: > if self.startswith(prefix): > return prefix > > Now you can create instances of MyString that will have all > the regular vstring methiods plus the new beginswith(): > > s = MyString("Welcome to my world") > if s.beginswith(["Welcome", "Hello","Howdy"]): > print "It's friendly" > > Which looks more consistent. It's an appealing idea but the extra step of wrapping strings in MyString kind of takes the shine off of it. For example something like this is awkward: for d in os.listdir(): d = MyString(d) if d.beginswith(...): Some languages allow you to extend a built-in class, this technique works better there. Kent From emadnawfal at gmail.com Thu Dec 25 14:31:51 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Thu, 25 Dec 2008 08:31:51 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: <1c2a2c590812250517o269f2cbbwb406f154d55eec2b@mail.gmail.com> References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <1c2a2c590812250517o269f2cbbwb406f154d55eec2b@mail.gmail.com> Message-ID: <652641e90812250531q104e6139lc1f6f060fa33690a@mail.gmail.com> On 12/25/08, Kent Johnson wrote: > > On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (???? ????) > wrote: > > Hi Tutors, > > I want a function that acts like the startswith method, but can take > > multiple prefixes. As an amateur programmer, I came up with this one, and > it > > works fine, but my experience tells me that my solutions are not always > the > > best ones around. Can you please tell me what a better option might be: > > > Since Python 2.5, startswith() accepts a tuple as the match parameter, > so you can write > def beginsWith(word, listname): > return word.startswith(tuple(listname)) > > or just us startswith() directly. > > > Kent > Thank you Kent. I read the documentation, but did not notice it, now I can see it clearly. This also applies to endswith Thank you all for your helpfulness. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 25 18:46:40 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 25 Dec 2008 17:46:40 -0000 Subject: [Tutor] beginsWith multiple prefixes References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com><4952C005.3010200@gmail.com><20081225055112.GA31743@ayn.mi.celestial.com><652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> <1c2a2c590812250520r7fe2eb4bnf0b3dda87da92ff9@mail.gmail.com> Message-ID: "Kent Johnson" wrote >> You could also make this a method of a subclass of string if you >> prefer: >> >> class MyString(str): >> def beginswith(self, prefixes): >> for prefix in prefixes: >> if self.startswith(prefix): >> return prefix > > It's an appealing idea but the extra step of wrapping strings in > MyString kind of takes the shine off of it. For example something > like > this is awkward: > for d in os.listdir(): > d = MyString(d) > if d.beginswith(...): I would write that as: for d in os.listdir(): if MyString(d).beginswith(....): Which isn't that cumbersome. d is still available for subsequent processing and acces to normal string methods is still useful , as in: for d in os.listdir(): if MyString(d).upper().beginswith(....): But it's a matter of taste I guess. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld > > Some languages allow you to extend a built-in class, this technique > works better there. > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Fri Dec 26 03:25:17 2008 From: kent37 at tds.net (Kent Johnson) Date: Thu, 25 Dec 2008 21:25:17 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <4952C005.3010200@gmail.com> <20081225055112.GA31743@ayn.mi.celestial.com> <652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> <1c2a2c590812250520r7fe2eb4bnf0b3dda87da92ff9@mail.gmail.com> Message-ID: <1c2a2c590812251825t619f8977g4b66443dffa63bbb@mail.gmail.com> On Thu, Dec 25, 2008 at 12:46 PM, Alan Gauld wrote: > for d in os.listdir(): > if MyString(d).beginswith(....): > > Which isn't that cumbersome. d is still available for > subsequent processing and acces to normal string > methods is still useful , as in: > > for d in os.listdir(): > if MyString(d).upper().beginswith(....): But that won't work, the result of calling upper() will be a normal str, not a MyString. Kent From prasadaraon50 at gmail.com Fri Dec 26 07:21:19 2008 From: prasadaraon50 at gmail.com (prasad rao) Date: Thu, 25 Dec 2008 22:21:19 -0800 Subject: [Tutor] to sort Message-ID: <9e3fac840812252221pe3345bam7e3e3563e050c940@mail.gmail.com> hello I am trying to sort a list(I know there is a builtin sort method). a=[21,56,35,47,94,12] b=[] for x in a: b.append (min(a)) a.remove (min(a)) >>> a [56, 47, 94] >>> b [12, 21, 35] It is not Compleating .Doing only 3 rounds.Why? By the way how can I view the builtin code for sort method? Wish you all Happy Xmass. thanks Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From metolone+gmane at gmail.com Fri Dec 26 08:27:31 2008 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Thu, 25 Dec 2008 23:27:31 -0800 Subject: [Tutor] to sort References: <9e3fac840812252221pe3345bam7e3e3563e050c940@mail.gmail.com> Message-ID: "prasad rao" wrote in message news:9e3fac840812252221pe3345bam7e3e3563e050c940 at mail.gmail.com... > hello > I am trying to sort a list(I know there is a builtin sort method). > > > a=[21,56,35,47,94,12] > b=[] > > > for x in a: > b.append (min(a)) > a.remove (min(a)) > >>> a > [56, 47, 94] > >>> b > [12, 21, 35] > > It is not Compleating .Doing only 3 rounds.Why? You should not change a list while iterating over it. Instead, iterate until a is empty: >>> a=[21,56,35,47,94,12] >>> b=[] >>> while a: ... b.append(min(a)) ... a.remove(min(a)) ... >>> a [] >>> b [12, 21, 35, 47, 56, 94] >>> -Mark From alan.gauld at btinternet.com Fri Dec 26 12:47:41 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 26 Dec 2008 11:47:41 -0000 Subject: [Tutor] beginsWith multiple prefixes References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com><4952C005.3010200@gmail.com><20081225055112.GA31743@ayn.mi.celestial.com><652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com><1c2a2c590812250520r7fe2eb4bnf0b3dda87da92ff9@mail.gmail.com> <1c2a2c590812251825t619f8977g4b66443dffa63bbb@mail.gmail.com> Message-ID: "Kent Johnson" wrote >> for d in os.listdir(): >> if MyString(d).upper().beginswith(....): > > But that won't work, the result of calling upper() will be a normal > str, not a MyString. Ah yes. Immutability of strings strikes again. upper() returns a new string, I forgot about that. pity. You can do if MyString(d.upper()).beginswith(...) But that loses a lot in elegance and is hardly better than using a fiunction. Alan G From alan.gauld at btinternet.com Fri Dec 26 12:59:55 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 26 Dec 2008 11:59:55 -0000 Subject: [Tutor] to sort References: <9e3fac840812252221pe3345bam7e3e3563e050c940@mail.gmail.com> Message-ID: "prasad rao" wrote > a=[21,56,35,47,94,12] > b=[] > > for x in a: > b.append (min(a)) > a.remove (min(a)) > It is not Compleating .Doing only 3 rounds.Why? Think about what is happening. for x in a x takes the next value of x after each iteration. Each iteration reduces the size of a so after 3 iterations x will be the 3rd value and there are only 3 values left. So x is at the end of the list and the loop stops. So as Mark said, don't change the list while iterating over it - its a bit like cutting off the branch of the tree that you are sitting on! But why are you trying to sort in this fashion? Its extremely inefficient compared to using the built in methods. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From emadnawfal at gmail.com Fri Dec 26 14:09:34 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 26 Dec 2008 08:09:34 -0500 Subject: [Tutor] commands versus subprocess, I'm confused Message-ID: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com> Hello Tutors, and Happy New Year and Holidays, suppose I have an external program that prints "testing the subprocess module" I know I can run it through the commands module like this: >>> a = commands.getoutput("python3.0 hello.py") >>> a 'testing the subprocess module' >>> len(a) 29 >>> b = a.split() >>> b ['testing', 'the', 'subprocess', 'module'] >>> for word in b: ... if word[-1] == 'e': print word ... the module >>> I cannot figure out how to do the same thing in the subprocess module. Can somebody please explain how to get the same behaviour from, say, subprocess.call -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "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 emadnawfal at gmail.com Fri Dec 26 14:14:34 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 26 Dec 2008 08:14:34 -0500 Subject: [Tutor] beginsWith multiple prefixes In-Reply-To: References: <652641e90812241304g37c74c90k6adf0060f895ef86@mail.gmail.com> <4952C005.3010200@gmail.com> <20081225055112.GA31743@ayn.mi.celestial.com> <652641e90812250310s472c98f3tbd9de651a9511bee@mail.gmail.com> <1c2a2c590812250520r7fe2eb4bnf0b3dda87da92ff9@mail.gmail.com> <1c2a2c590812251825t619f8977g4b66443dffa63bbb@mail.gmail.com> Message-ID: <652641e90812260514n19af42dud2e837697ec69bd1@mail.gmail.com> On Fri, Dec 26, 2008 at 6:47 AM, Alan Gauld wrote: > > "Kent Johnson" wrote > >> for d in os.listdir(): >>> if MyString(d).upper().beginswith(....): >>> >> >> But that won't work, the result of calling upper() will be a normal >> str, not a MyString. >> > > Ah yes. Immutability of strings strikes again. upper() returns a new > string, I forgot about that. pity. > > You can do > > if MyString(d.upper()).beginswith(...) > > But that loses a lot in elegance and is hardly better than using a > fiunction. > > Alan G > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you Alan and everybody. I simply prefer the built-in one. I had no idea it could take a tuple. What is amazing is that I learn more from this list than I do from any other source. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Fri Dec 26 14:32:40 2008 From: kent37 at tds.net (Kent Johnson) Date: Fri, 26 Dec 2008 08:32:40 -0500 Subject: [Tutor] to sort In-Reply-To: <9e3fac840812252221pe3345bam7e3e3563e050c940@mail.gmail.com> References: <9e3fac840812252221pe3345bam7e3e3563e050c940@mail.gmail.com> Message-ID: <1c2a2c590812260532g184bb24j176a0b5d42382649@mail.gmail.com> On Fri, Dec 26, 2008 at 1:21 AM, prasad rao wrote: > By the way how can I view the builtin code for sort method? Look at the source - Objects/listobject.c - starting at the comment "Lots of code for an adaptive, stable, natural mergesort." http://svn.python.org/view/python/trunk/Objects/listobject.c?rev=67498&view=markup The source also includes an interesting and detailed description of the sort algorithm: http://svn.python.org/view/python/trunk/Objects/listsort.txt?rev=51013&view=auto Kent From kent37 at tds.net Fri Dec 26 14:39:39 2008 From: kent37 at tds.net (Kent Johnson) Date: Fri, 26 Dec 2008 08:39:39 -0500 Subject: [Tutor] commands versus subprocess, I'm confused In-Reply-To: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com> References: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com> Message-ID: <1c2a2c590812260539y17e0d815g969ecdbf0ad1fa64@mail.gmail.com> On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (???? ????) wrote: > suppose I have an external program that prints "testing the subprocess > module" > I know I can run it through the commands module like this: > >>>> a = commands.getoutput("python3.0 hello.py") >>>> a > 'testing the subprocess module' > I cannot figure out how to do the same thing in the subprocess module. Can > somebody please explain how to get the same behaviour from, say, > subprocess.call Sometthing like this, I think: proc = subprocess.Popen('python3.0 hello.py', shell=True, stdout=subprocess.PIPE, ) stdout_value = proc.communicate()[0] (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html) Kent From emadnawfal at gmail.com Fri Dec 26 14:57:43 2008 From: emadnawfal at gmail.com (=?WINDOWS-1256?Q?Emad_Nawfal_(=DA=E3=C7=CF_=E4=E6=DD=E1)?=) Date: Fri, 26 Dec 2008 08:57:43 -0500 Subject: [Tutor] commands versus subprocess, I'm confused In-Reply-To: <1c2a2c590812260539y17e0d815g969ecdbf0ad1fa64@mail.gmail.com> References: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com> <1c2a2c590812260539y17e0d815g969ecdbf0ad1fa64@mail.gmail.com> Message-ID: <652641e90812260557l5114507apbc89115bf0bda3d2@mail.gmail.com> 2008/12/26 Kent Johnson > On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (???? ????) > wrote: > > suppose I have an external program that prints "testing the subprocess > > module" > > I know I can run it through the commands module like this: > > > >>>> a = commands.getoutput("python3.0 hello.py") > >>>> a > > 'testing the subprocess module' > > > > I cannot figure out how to do the same thing in the subprocess module. > Can > > somebody please explain how to get the same behaviour from, say, > > subprocess.call > > Sometthing like this, I think: > > proc = subprocess.Popen('python3.0 hello.py', > shell=True, > stdout=subprocess.PIPE, > ) > stdout_value = proc.communicate()[0] > > (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html) > > Kent > Thank you Kent. It works, but isn't the commands module much simpler? I don't know why it's no more available in Python3.0 -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Dec 26 15:06:14 2008 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 26 Dec 2008 14:06:14 +0000 (GMT) Subject: [Tutor] beginsWith multiple prefixes Message-ID: <434437.35455.qm@web86705.mail.ird.yahoo.com> > ....I simply prefer the built-in one. I had no idea it could take a tuple. Me neither, that was a surprise goody in 2.5 that I hadn't seen before. > What is amazing is that I learn more from this list than I do from any other source. Me too, and I've been subscribed for over 10 years now! :-) Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Dec 26 15:28:57 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 26 Dec 2008 14:28:57 -0000 Subject: [Tutor] commands versus subprocess, I'm confused References: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com><1c2a2c590812260539y17e0d815g969ecdbf0ad1fa64@mail.gmail.com> <652641e90812260557l5114507apbc89115bf0bda3d2@mail.gmail.com> Message-ID: "Emad Nawfal (???? ????)" wrote >> proc = subprocess.Popen('python3.0 hello.py', >> shell=True, >> stdout=subprocess.PIPE, >> ) >> stdout_value = proc.communicate()[0] > Thank you Kent. > It works, but isn't the commands module much simpler? I don't know > why it's > no more available in Python3.0 commands was one of many different ways to access command ouitput. Subprocess was designed to replace all of them and provide a single consistent mechanism. So, yes we lost some simplicity but gained consistency. You no longer need to figure out whether its best to use os,system, os.popen(and which version of popen), os.spawn, commands etc You just use subprocess... HTH, Alan G From reed at reedobrien.com Fri Dec 26 16:02:56 2008 From: reed at reedobrien.com (Reed O'Brien) Date: Fri, 26 Dec 2008 10:02:56 -0500 Subject: [Tutor] commands versus subprocess, I'm confused In-Reply-To: <652641e90812260557l5114507apbc89115bf0bda3d2@mail.gmail.com> References: <652641e90812260509t5f5af359sa67ec8af6c8330df@mail.gmail.com> <1c2a2c590812260539y17e0d815g969ecdbf0ad1fa64@mail.gmail.com> <652641e90812260557l5114507apbc89115bf0bda3d2@mail.gmail.com> Message-ID: <92013FCA-91D2-414F-87DA-887FEF646522@reedobrien.com> On Dec 26, 2008, at 8:57, "Emad Nawfal (???? ????)" wrote: > > > 2008/12/26 Kent Johnson > On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (???? ????) > wrote: > > suppose I have an external program that prints "testing the > subprocess > > module" > > I know I can run it through the commands module like this: > > > >>>> a = commands.getoutput("python3.0 hello.py") > >>>> a > > 'testing the subprocess module' > > > > I cannot figure out how to do the same thing in the subprocess > module. Can > > somebody please explain how to get the same behaviour from, say, > > subprocess.call > > Sometthing like this, I think: > > proc = subprocess.Popen('python3.0 hello.py', > shell=True, > stdout=subprocess.PIPE, > ) > stdout_value = proc.communicate()[0] > > (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html > ) > > Kent > Thank you Kent. > It works, but isn't the commands module much simpler? I don't know > why it's no more available in Python3.0 Subprocess was designed to replace all the os.popen classes since 2.4. Commands is a wrapper for os.popen. Aside from that commands AFAIK is unix only. Therefore it is less portable. ~ro -------------- next part -------------- An HTML attachment was scrubbed... URL: From prasadaraon50 at gmail.com Sat Dec 27 07:02:10 2008 From: prasadaraon50 at gmail.com (prasad rao) Date: Fri, 26 Dec 2008 22:02:10 -0800 Subject: [Tutor] Repply Message-ID: <9e3fac840812262202s5a9c5059i88f0ad2cf056baa7@mail.gmail.com> Thanks for help. > http://svn.python.org/view/python/trunk/Objects/listobject.c?rev=67498&view=markup Kent ! This is grek and latin to me.From the presence of header files it looks C++.But headerfiles are not between '<' and '>' . >But why are you trying to sort in this fashion? Alan Gauld! Basic exercises in C++ are to find min , malimum and sorting.So I am just trying it in python. Thank you Mark. Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Dec 27 10:42:42 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 27 Dec 2008 09:42:42 -0000 Subject: [Tutor] Repply References: <9e3fac840812262202s5a9c5059i88f0ad2cf056baa7@mail.gmail.com> Message-ID: "prasad rao" wrote > http://svn.python.org/view/python/trunk/Objects/listobject.c?rev=67498&view=markup > > Kent ! This is grek and latin to me.From the presence of header > files it > looks C++.But headerfiles are not between '<' and '>' . Its C rather than C++. The <> in include statements are a variation on the "" which can also be used. The differences are subtle and have to do with the search path I think. But its been so long since I did serious C/++ I can't recall exactly! > Alan Gauld! Basic exercises in C++ are to find min , malimum and > sorting.So > I > am just trying it in python. Thats OK, I just wondered if you had some special algorithm you needed to use or something. Alan G. From srilyk at gmail.com Sat Dec 27 18:36:34 2008 From: srilyk at gmail.com (W W) Date: Sat, 27 Dec 2008 11:36:34 -0600 Subject: [Tutor] Repply In-Reply-To: References: <9e3fac840812262202s5a9c5059i88f0ad2cf056baa7@mail.gmail.com> Message-ID: <333efb450812270936w520727b3n7e1753f1bc077d98@mail.gmail.com> On Sat, Dec 27, 2008 at 3:42 AM, Alan Gauld wrote: > > Its C rather than C++. > The <> in include statements are a variation on the "" which can also be > used. > The differences are subtle and have to do with the search path I think. But > its > been so long since I did serious C/++ I can't recall exactly! It's only been about 2 weeks since I've been in class so that's a topic on which I can expound. "" searches the current directory before searching in the default library. It's pretty similar to the python "import" behavior, AFAICT. "And now you know" -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From msh at blisses.org Sat Dec 27 20:55:02 2008 From: msh at blisses.org (Matt Herzog) Date: Sat, 27 Dec 2008 14:55:02 -0500 Subject: [Tutor] Redux: optparse In-Reply-To: References: <20081223201029.GA29361@chicago.blisses.org> <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> Message-ID: <20081227195502.GC15963@chicago.blisses.org> On Wed, Dec 24, 2008 at 01:12:55AM -0000, Alan Gauld wrote: > > "Kent Johnson" wrote > > >> for filename in os.listdir(directory): > >> result = re.match(s, filename) > >> print result > > > >You never open and read the files. You are searching for the pattern > >in the filename, not in the contents of the file. > > Also note that match() only searches starting at the start of the > string. > > Thus match will find foo at > > foobar > > but not in > > sofoo > > You usually need to use search() to find the pattern anywhere > within the string. > > Also look at the thread earlier this week on using listdir() and > the fileinput module. Hello again and thanks for the encouragement. I have been working on this problem again today and switched to the fileinput method. What I can't figure out now is how to pass a compiled regex to an optparse option. I'm confused ias to "option" versus "arg" when using the optparse module. In fact there seems to be no way to define what the arg should be; only options. Is the arg always implied? I have read several pages on optparse and am none the wiser. How do I fix the rx = re.compile('-x') line below so that the string I pass on the command line gets passed into the re.compile? #!/usr/bin/python import fileinput, sys, string, optparse, re #def main(): optparser = optparse.OptionParser() optparser.add_option("-x", "--regx", help="regular expression") # take the first argument out of sys.argv and assign it to searchterm #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] (options, args) = optparser.parse_args() rx = re.compile('-x') for line in fileinput.input(): num_matches = string.count(line, rx) if num_matches: print "found '%s' %d times in %s on line %d." % (rx, num_matches, fileinput.filename(), fileinput.filelineno()) #if __name__ == '__main__': #main() > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From denis.spir at free.fr Sat Dec 27 21:48:13 2008 From: denis.spir at free.fr (spir) Date: Sat, 27 Dec 2008 21:48:13 +0100 Subject: [Tutor] Redux: optparse In-Reply-To: <20081227195502.GC15963@chicago.blisses.org> References: <20081223201029.GA29361@chicago.blisses.org> <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> <20081227195502.GC15963@chicago.blisses.org> Message-ID: <1230410893.6321.17.camel@o> Le samedi 27 d?cembre 2008 ? 14:55 -0500, Matt Herzog a ?crit : > On Wed, Dec 24, 2008 at 01:12:55AM -0000, Alan Gauld wrote: > > > > "Kent Johnson" wrote > > > > >> for filename in os.listdir(directory): > > >> result = re.match(s, filename) > > >> print result > > > > > >You never open and read the files. You are searching for the pattern > > >in the filename, not in the contents of the file. > > > > Also note that match() only searches starting at the start of the > > string. > > > > Thus match will find foo at > > > > foobar > > > > but not in > > > > sofoo > > > > You usually need to use search() to find the pattern anywhere > > within the string. > > > > Also look at the thread earlier this week on using listdir() and > > the fileinput module. > > Hello again and thanks for the encouragement. > > I have been working on this problem again today and switched to the fileinput method. What I can't figure out now is how to pass a compiled regex to an optparse option. I'm confused ias to "option" versus "arg" when using the optparse module. In fact there seems to be no way to define what the arg should be; only options. Is the arg always implied? I have read several pages on optparse and am none the wiser. > > How do I fix the rx = re.compile('-x') line below so that the string I pass on the command line gets passed into the re.compile? > > #!/usr/bin/python > import fileinput, sys, string, optparse, re > > #def main(): > optparser = optparse.OptionParser() > optparser.add_option("-x", "--regx", help="regular expression") > # take the first argument out of sys.argv and assign it to searchterm > #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] > (options, args) = optparser.parse_args() Do you want to use optparse, or get the command line arguments yourself? It seems the pattern string will be the first arg, will it? > rx = re.compile('-x') Then, if the note above is right, you have: pat_string = sys.argv[1] rx = re.compile(pat_string) # pattern object Is that what you wish to do? > for line in fileinput.input(): > num_matches = string.count(line, rx) Above you are counting the number of *regex pattern* may be inside a string. Not the number of (sub)strings matching the pattern, which is probably what you intend.[ Also, you are using a deprecated function "count" of the string module. Use line.count(substring) instead. Anyway, this is probably not what you want.] To count the number of pattern matches, you must use the "matching" (lol) regex method, namely findall: result_list = rx.findall(line) > if num_matches: > print "found '%s' %d times in %s on line %d." % (rx, num_matches, > fileinput.filename(), fileinput.filelineno()) Do you want to print only the last match? If not, you need to record matches found by the search loop into a list. > #if __name__ == '__main__': > #main() From msh at blisses.org Sat Dec 27 22:46:59 2008 From: msh at blisses.org (Matt Herzog) Date: Sat, 27 Dec 2008 16:46:59 -0500 Subject: [Tutor] Redux: optparse In-Reply-To: <1230410893.6321.17.camel@o> References: <20081223201029.GA29361@chicago.blisses.org> <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> <20081227195502.GC15963@chicago.blisses.org> <1230410893.6321.17.camel@o> Message-ID: <20081227214659.GF15963@chicago.blisses.org> > Do you want to use optparse, or get the command line arguments yourself? > It seems the pattern string will be the first arg, will it? Again I am confused. I assumed that optparse was the best way to pass in arguments (such as filenames) from the command line. Like so: ./script.py -x regex *.html > > rx = re.compile('-x') > > Then, if the note above is right, you have: > > pat_string = sys.argv[1] > rx = re.compile(pat_string) # pattern object > > Is that what you wish to do? Probablement. :) The below code does what I want: ------------------------------------------------------------------------------- #!/usr/bin/python import fileinput, sys, string # Take the first argument out of sys.argv and assign it to searchterm. searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] for line in fileinput.input(): num_matches = line.count(searchterm) if num_matches: # A nonzero count means there was a match. print "found '%s' %d times in %s on line %d." % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno()) ------------------------------------------------------------------------------- I wanted to use optparse instead of sys.argv. Perhaps I should study fileinput instead. I don't understand that either. > > for line in fileinput.input(): > > num_matches = string.count(line, rx) > Above you are counting the number of *regex pattern* may be inside a > string. Not the number of (sub)strings matching the pattern, which is > probably what you intend.[ Also, you are using a deprecated function > "count" of the string module. Use line.count(substring) instead. Anyway, > this is probably not what you want.] > To count the number of pattern matches, you must use the > "matching" (lol) regex method, namely findall: > > result_list = rx.findall(line) > > > if num_matches: > > print "found '%s' %d times in %s on line %d." % (rx, num_matches, > > fileinput.filename(), fileinput.filelineno()) > > Do you want to print only the last match? If not, you need to record > matches found by the search loop into a list. > > > #if __name__ == '__main__': > > #main() > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- I fear you speak upon the rack, Where men enforced do speak anything. - William Shakespeare From btkuhn at email.unc.edu Sun Dec 28 01:32:29 2008 From: btkuhn at email.unc.edu (btkuhn at email.unc.edu) Date: Sat, 27 Dec 2008 19:32:29 -0500 Subject: [Tutor] Anagram creator Message-ID: <20081227193229.msc933mmkook48wg@webmail4.isis.unc.edu> Hi everyone, I'm having trouble with an anagram generating program that I am writing in Python. My output is not what it should be and I think the reason has something to do with my helper functions creating a reference to a dictionary each time it is called rather than a new "clone" dictionary. Here is my main function, with explanation afterward: def anagramsHelper(phrase,dictionary,anagramsofar): if isSolved(phrase): #returns true if the dictonary for the phrase is empty print anagramsofar return True for word in dictionary: if wordCanBeMade(word, phrase): anagramsofar=anagramsofar+" "+word phraseclone=removeLetters(word, phrase) anagramsHelper(phraseclone,dictionary,anagramsofar) Initially it takes a dictionary representing a word or phrase (eg {'a':2,'d':1, 'e':2}), a dictionary in the form of a list of words, and anagramsofar which is initially passed as "". Next, isSolved returns true if the dictionary is empty, meaning that all letters in the phrase have been used up. Otherwise, for each word in the dictionary, the program tests whether the word can be made from letters in the phrase. If it can, the word is appended to phrasesofar. removeLetters then removes the letters that were used to make the word. I saved the new phrase as phraseclone as an attempt to troubleshoot; originally I just used the original phrase variable. Next, the function is recursively called with the letters removed from the phrase. The end result is that when the phrase dictionary is empty, "anagramsofar" is printed. This should generate all anagrams of a given phrase using a given dictionary of words. So, for instance, I might call (from my main function): dictionary=importDictionary() #A list of 10,000 strings, representing words in the dictionary anagramsHelper("george bush",dictionary,"") wordCanBeMade returns a boolean "T" or "F", and I've posted the code for removeLetters below. My code is not working as expected, and after a good deal of debugging it seems that during the recursive calls, when I remove letters from the dictionary they "stay removed", even though I'm trying to create a new dictionary with each call (so that removed letters are not remembered). As a result, anagramsofar gets longer with every call and isSolved returns True every time, since all keys are removed after the first few calls. Here is the function for removeLetters: def removeLetters(word, phrase): phraseclone=phrase.copy() for letter in word: phraseclone[letter]=phraseclone[letter]-1 return phraseclone I'm trying to create a copy of phrase with each function call and then return the copy to prevent the program remembering the removed keys...but it's not working. Does anyone see what I'm doing wrong? I can post the entire program if needed; I just wanted to keep things somewhat concise. Thanks. From denis.spir at free.fr Sun Dec 28 12:03:19 2008 From: denis.spir at free.fr (spir) Date: Sun, 28 Dec 2008 12:03:19 +0100 Subject: [Tutor] Anagram creator In-Reply-To: <20081227193229.msc933mmkook48wg@webmail4.isis.unc.edu> References: <20081227193229.msc933mmkook48wg@webmail4.isis.unc.edu> Message-ID: <1230462199.6102.88.camel@o> Le samedi 27 d?cembre 2008 ? 19:32 -0500, btkuhn at email.unc.edu a ?crit : > Hi everyone, > > I'm having trouble with an anagram generating program that I am writing > in Python. My output is not what it should be and I think the reason > has something to do with my helper functions creating a reference to a > dictionary each time it is called rather than a new "clone" dictionary. > Here is my main function, with explanation afterward: > > > def anagramsHelper(phrase,dictionary,anagramsofar): if > isSolved(phrase): #returns true if the dictonary for the phrase is empty > print anagramsofar > return True > for word in dictionary: > if wordCanBeMade(word, phrase): > anagramsofar=anagramsofar+" "+word > phraseclone=removeLetters(word, phrase) > anagramsHelper(phraseclone,dictionary,anagramsofar) > There is a layout issue with this func. I guess the proper code is probably as below (I renamed the func for the code to fit in usual width) : def anagrHelp(phrase,dictionary,anagramsofar): if isSolved(phrase): #returns true if the dict is empty print anagramsofar return True for word in dictionary: if wordCanBeMade(word, phrase): anagramsofar=anagramsofar+" "+word phraseclone=removeLetters(word, phrase) anagrHelp(phraseclone,dictionary,anagramsofar) > Initially it takes a dictionary representing a word or phrase (eg > {'a':2,'d':1, 'e':2}), a dictionary in the form of a list of words, and > anagramsofar which is initially passed as "". > Next, isSolved returns true if the dictionary is empty, meaning that > all letters in the phrase have been used up. Otherwise, for each word > in the dictionary, the program tests whether the word can be made from > letters in the phrase. If it can, the word is appended to phrasesofar. > removeLetters then removes the letters that were used to make the word. > I saved the new phrase as phraseclone as an attempt to troubleshoot; > originally I just used the original phrase variable. Next, the function > is recursively called with the letters removed from the phrase. The end > result is that when the phrase dictionary is empty, "anagramsofar" is > printed. This should generate all anagrams of a given phrase using a > given dictionary of words. I think the issue lays in the combination of loop and recursive call (which is often a very subtile feature). You current status is both held by angramsofar (constructed new phrase) and phrase (unused char list). So that if you want to find it back when backtracking to a previous stage, you need to find them both at their value for this stage. Which means that both must be copied to be passed as arguments for a recursive call. As of now, only phrase is cloned. So that anagramsofar can only grow... Denis From kent37 at tds.net Sun Dec 28 16:06:12 2008 From: kent37 at tds.net (Kent Johnson) Date: Sun, 28 Dec 2008 10:06:12 -0500 Subject: [Tutor] Redux: optparse In-Reply-To: <20081227195502.GC15963@chicago.blisses.org> References: <20081223201029.GA29361@chicago.blisses.org> <1c2a2c590812231322n75fe30b6gaf632b107079f4f1@mail.gmail.com> <20081227195502.GC15963@chicago.blisses.org> Message-ID: <1c2a2c590812280706m2af34c9eqfb7cc94408f836a0@mail.gmail.com> On Sat, Dec 27, 2008 at 2:55 PM, Matt Herzog wrote: > What I can't figure out now is how to pass a compiled regex to an optparse option. I'm confused ias to "option" versus "arg" when using the optparse module. In fact there seems to be no way to define what the arg should be; only options. Is the arg always implied? I have read several pages on optparse and am none the wiser. You want to *retrieve* the regex from the option, not pass it to the option. See below. "args" is the positional arguments - any parameters that aren't prefixed with a switch like "-x". They don't need any configuration. > How do I fix the rx = re.compile('-x') line below so that the string I pass on the command line gets passed into the re.compile? > > #!/usr/bin/python > import fileinput, sys, string, optparse, re > > #def main(): > optparser = optparse.OptionParser() > optparser.add_option("-x", "--regx", help="regular expression") > # take the first argument out of sys.argv and assign it to searchterm > #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] > (options, args) = optparser.parse_args() > > rx = re.compile('-x') Try rx = re.compile(options.regx) Kent From sander.sweers at gmail.com Sun Dec 28 19:09:51 2008 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 28 Dec 2008 19:09:51 +0100 Subject: [Tutor] telnetlib unable to cath gaierror Message-ID: Hello All, I am having issues cathing exceptions from telnetlib. What I am doing is: ----------------------------- tc = telnetlib.Telnet() try: tc.open('abc') except gaierror: print 'Invalid hostname' ----------------------------- Which gives me "NameError: name 'gaierror' is not defined" :-( >From the interactive promt I see: >>> tc = telnetlib.Telnet('abc') Traceback (most recent call last): File "", line 1, in tc = telnetlib.Telnet('blah') File "C:\Python25\lib\telnetlib.py", line 208, in __init__ self.open(host, port) File "C:\Python25\lib\telnetlib.py", line 225, in open for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): gaierror: (11001, 'getaddrinfo failed') So why can I not catch it with try/except? Thanks Sander From steve at alchemy.com Sun Dec 28 19:17:30 2008 From: steve at alchemy.com (Steve Willoughby) Date: Sun, 28 Dec 2008 10:17:30 -0800 Subject: [Tutor] telnetlib unable to cath gaierror In-Reply-To: References: Message-ID: <20081228181730.GA74533@dragon.alchemy.com> On Sun, Dec 28, 2008 at 07:09:51PM +0100, Sander Sweers wrote: > I am having issues cathing exceptions from telnetlib. What I am doing is: > except gaierror: > Which gives me "NameError: name 'gaierror' is not defined" :-( This error message contains your clue. If the name you're referencing is not defined, you usually forgot to either explicitly import it, or to name it with its namespace attached. So you either want to say: from telnetlib import gaierror at the top of your script, or if you earlier said simply "import telnetlib", then your try/except clause would be like: except telnetlib.gaierror: -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From sander.sweers at gmail.com Sun Dec 28 20:07:41 2008 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 28 Dec 2008 20:07:41 +0100 Subject: [Tutor] telnetlib unable to cath gaierror In-Reply-To: <20081228181730.GA74533@dragon.alchemy.com> References: <20081228181730.GA74533@dragon.alchemy.com> Message-ID: On Sun, Dec 28, 2008 at 19:17, Steve Willoughby wrote: > On Sun, Dec 28, 2008 at 07:09:51PM +0100, Sander Sweers wrote: >> I am having issues cathing exceptions from telnetlib. What I am doing is: >> except gaierror: >> Which gives me "NameError: name 'gaierror' is not defined" :-( > > This error message contains your clue. If the name you're referencing is not defined, you usually forgot to either explicitly import it, or to name it with its namespace attached. Looking more closely it is not an error from telnetlib. It is an error from the socket module. > from telnetlib import gaierror It works with import socket. > then your try/except clause would be like: > except telnetlib.gaierror: I can catch it now with socket.gaierror but I do not understand why telnetlib does not raise an error instead. To me it would be more logically that telnetlib catches this and raises an error. Now this comes from a very novice programmer so I might be way off on this :-) Another question, read_until can be passed a timeout. How would catch if the timeout hit? It does not raise an error it afaics just returns whatever it read till the timeout. Thanks Sander From denis.spir at free.fr Sun Dec 28 20:25:37 2008 From: denis.spir at free.fr (spir) Date: Sun, 28 Dec 2008 20:25:37 +0100 Subject: [Tutor] telnetlib unable to cath gaierror In-Reply-To: <20081228181730.GA74533@dragon.alchemy.com> References: <20081228181730.GA74533@dragon.alchemy.com> Message-ID: <1230492337.6482.1.camel@o> Le dimanche 28 d?cembre 2008 ? 10:17 -0800, Steve Willoughby a ?crit : > On Sun, Dec 28, 2008 at 07:09:51PM +0100, Sander Sweers wrote: > > I am having issues cathing exceptions from telnetlib. What I am doing is: > > except gaierror: > > Which gives me "NameError: name 'gaierror' is not defined" :-( > > This error message contains your clue. If the name you're referencing is not defined, you usually forgot to either explicitly import it, or to name it with its namespace attached. > > So you either want to say: > > from telnetlib import gaierror > > at the top of your script, or if you earlier said simply "import telnetlib", > > then your try/except clause would be like: > > except telnetlib.gaierror: > Well, actually, gaierror is an Exception of the socket module: import telnetlib,socket tc = telnetlib.Telnet() try: tc.open("abc") except socket.gaierror: print "Invalid hostname" raise ==> spir at o:~/prog/parse$ python "___test.py" Invalid hostname Traceback (most recent call last): File "___test.py", line 6, in tc.open("abc") File "/usr/lib/python2.5/telnetlib.py", line 225, in open for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): socket.gaierror: (-2, 'Name or service not known') Denis From Jaggojaggo+Py at gmail.com Sun Dec 28 23:49:53 2008 From: Jaggojaggo+Py at gmail.com (Omer) Date: Mon, 29 Dec 2008 00:49:53 +0200 Subject: [Tutor] Fwd: Class Extend Help In-Reply-To: References: <515008f10812200823mac79878yb872cb6a549c77db@mail.gmail.com> <515008f10812220958y39399988k1fd2d080cb7b327b@mail.gmail.com> <515008f10812230631p348d3abdt949cdabc80c55acd@mail.gmail.com> <1c2a2c590812230723i336e8b3fo93c782d3dc0cdb57@mail.gmail.com> Message-ID: <515008f10812281449k7367aa0ft3494e35a81966c28@mail.gmail.com> Thx for help, extreme lack of free time lately, will look into it. [Wouldn't've noticed myself that urlopen is not a class.] On Tue, Dec 23, 2008 at 6:03 PM, Richard Lovely wrote: > OK, going on Kent's post: > > from urllib import urlopen > > class fetch(object): > def __init__(self, *args): > self.response = urlopen(*args) > self.content = self.response.read() > > > I forgot urlopen was a function. It made sense in my head for it to > be a class, instead of _returning_ a class. > > It's trying to call __init__ on a functionType instance, and it's > getting confused. > > See http://dpaste.com/hold/101951/ for another example. The error > message is something cryptic, but between #python and I we think it's > something to do with calling type(). > --- > Richard "Roadie Rich" Lovely, part of the JNP|UK Famile > www.theJNP.com > > > > 2008/12/23 Kent Johnson : > > On Tue, Dec 23, 2008 at 9:31 AM, Omer > > wrote: > > > >>>>> from urllib import urlopen > >>>>> class fetch(urlopen): > >> ... def __init__(self,*args): > >> ... urlopen.__init__(self, *args) > >> ... self.content = self.read() > >> ... > >> > >> Traceback (most recent call last): > >> File "", line 1, in > >> TypeError: Error when calling the metaclass bases > >> function() argument 1 must be code, not str > >> > >> Anybody, any idea what's up with that? > > > > That's pretty obscure, not sure why you get exactly that error! > > > > Anyway the code should be > > class fetch(object): > > > > urlopen is a function, not a class, so it can't be a base class of > > fetch. There is no need for it to be a base class, either; just use > > object. > > > > Kent > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at abbottdavid.com Mon Dec 29 02:41:17 2008 From: david at abbottdavid.com (David) Date: Sun, 28 Dec 2008 20:41:17 -0500 Subject: [Tutor] Exception Handling Message-ID: <49582ABD.2030906@abbottdavid.com> Hi, Is this the correct way to handle a ValueError exception and should I get in the practice of catching them? Also any suggestions on the program. thanks -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From david at abbottdavid.com Mon Dec 29 02:42:45 2008 From: david at abbottdavid.com (David) Date: Sun, 28 Dec 2008 20:42:45 -0500 Subject: [Tutor] Exception Handling In-Reply-To: <49582ABD.2030906@abbottdavid.com> References: <49582ABD.2030906@abbottdavid.com> Message-ID: <49582B15.7000108@abbottdavid.com> David wrote: > Hi, > Is this the correct way to handle a ValueError exception and should I > get in the practice of catching them? Also any suggestions on the program. > thanks > -david > Might help if I included the program :) #!/usr/bin/python import time print "Enter year as 0000" print "Enter month and day as 00" while True: try: yr = int(raw_input("What year were you born? ")) mn = int(raw_input("What month were you born? ")) dy = int(raw_input("What day were you born? ")) curr_date = time.strftime("%Y %m %d", time.gmtime()) ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) mnum = int(time.strftime("%m", time.gmtime())) dnum = int(time.strftime("%d", time.gmtime())) mn = int(mn) dy = int(dy) if mn - mnum: print "You are %i" % ynum, "years old." break elif mn == mnum and dy < dnum: print "You are %i" % ynum, "years old." break else: ret = int(ynum) - 1 print "You are %i" % ret, "years old." break except ValueError: print "Oops, You must enter a number!" -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From wormwood_3 at yahoo.com Mon Dec 29 02:49:59 2008 From: wormwood_3 at yahoo.com (wormwood_3) Date: Sun, 28 Dec 2008 17:49:59 -0800 (PST) Subject: [Tutor] Printing the code of a function Message-ID: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Hello all, This might be trivially easy, but I was having a hard time searching on it since all the component terms are overloaded:-) I am wondering if there is a way to print out the code of a defined function. So if I have: def foo(): print "Show me the money." then I would like to do something like: >>> foo.show_code def foo(): print "Show me the money." I checked out everything in dir(foo), but everything that looked promising (namely foo.func_code), didn't end up being anything close. Thanks for any help! Cordially, Sam _______________________ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins -------------- next part -------------- An HTML attachment was scrubbed... URL: From motoom at xs4all.nl Mon Dec 29 03:03:17 2008 From: motoom at xs4all.nl (Michiel Overtoom) Date: Mon, 29 Dec 2008 03:03:17 +0100 Subject: [Tutor] Printing the code of a function In-Reply-To: <749311.18420.qm@web110812.mail.gq1.yahoo.com> References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Message-ID: <49582FE5.2030607@xs4all.nl> wormwood_3 wrote: > I am wondering if there is a way to > print out the code of a defined function. When Python compiles source code, it doesn't store the source code itself; only the compiled intermediate code. With the 'dis' package you can disassemble that: def foo(): print "Show me the money." import dis dis.dis(foo) >>> 2 0 LOAD_CONST 1 ('Show me the money.') 3 PRINT_ITEM 4 PRINT_NEWLINE 5 LOAD_CONST 0 (None) 8 RETURN_VALUE >>> -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Vallopillil http://www.catb.org/~esr/halloween/halloween4.html From bgailer at gmail.com Mon Dec 29 03:07:12 2008 From: bgailer at gmail.com (bob gailer) Date: Sun, 28 Dec 2008 21:07:12 -0500 Subject: [Tutor] Printing the code of a function In-Reply-To: <749311.18420.qm@web110812.mail.gq1.yahoo.com> References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Message-ID: <495830D0.1030302@gmail.com> An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Dec 29 03:13:41 2008 From: bgailer at gmail.com (bob gailer) Date: Sun, 28 Dec 2008 21:13:41 -0500 Subject: [Tutor] Exception Handling In-Reply-To: <49582B15.7000108@abbottdavid.com> References: <49582ABD.2030906@abbottdavid.com> <49582B15.7000108@abbottdavid.com> Message-ID: <49583255.7080507@gmail.com> David wrote: > David wrote: >> Hi, >> Is this the correct way to handle a ValueError exception and should I >> get in the practice of catching them? Well yes and no. I think it is better to use the string method isdigit to test for digits only. Also IMHO it is bad design to put a lot of code inside a try block. In this case the user might make a mistake on day and then is forced to reenter the year and month! Better to write a function for requesting an integer from the user which loops until the user gets it right (with some way to escape if he gets frustrated and just wants to quit. >> Also any suggestions on the program. >> thanks >> -david >> > Might help if I included the program :) > > #!/usr/bin/python > import time > > print "Enter year as 0000" > print "Enter month and day as 00" > > while True: > try: > yr = int(raw_input("What year were you born? ")) > mn = int(raw_input("What month were you born? ")) > dy = int(raw_input("What day were you born? ")) > curr_date = time.strftime("%Y %m %d", time.gmtime()) > > ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) > mnum = int(time.strftime("%m", time.gmtime())) > dnum = int(time.strftime("%d", time.gmtime())) > mn = int(mn) > dy = int(dy) > > if mn - mnum: > print "You are %i" % ynum, "years old." > break > elif mn == mnum and dy < dnum: > print "You are %i" % ynum, "years old." > break > else: > ret = int(ynum) - 1 > print "You are %i" % ret, "years old." > break > except ValueError: > print "Oops, You must enter a number!" > > > -- Bob Gailer Chapel Hill NC 919-636-4239 From wormwood_3 at yahoo.com Mon Dec 29 05:45:41 2008 From: wormwood_3 at yahoo.com (wormwood_3) Date: Sun, 28 Dec 2008 20:45:41 -0800 (PST) Subject: [Tutor] Printing the code of a function References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> <495830D0.1030302@gmail.com> Message-ID: <448325.4439.qm@web110802.mail.gq1.yahoo.com> I actually didn't have a definite use case in mind, it was pure curiosity that arose while writing a few simple test functions. After having considered it more, I can't come up with a case where it would really be necessary, so I apologize for that. It makes sense why it wouldn't be possible without a disassembler now. Thanks for the info! _______________________ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ________________________________ From: bob gailer To: wormwood_3 Cc: tutor at python.org Sent: Sunday, December 28, 2008 9:07:12 PM Subject: Re: [Tutor] Printing the code of a function wormwood_3 wrote: Hello all, This might be trivially easy, but I was having a hard time searching on it since all the component terms are overloaded:-) I am wondering if there is a way to print out the code of a defined function. Python does not store the source when compiling things. So the short answer is NO. There are some "disassemblers" for Python but I have no experience with them. They will not be able to reconstruct the exact source. But ... why do you want to do this? Perhaps if we had more information about the "use case" we could steer you to a solution. So if I have: def foo(): print "Show me the money." then I would like to do something like: >>> foo.show_code def foo(): print "Show me the money." I checked out everything in dir(foo), but everything that looked promising (namely foo.func_code), didn't end up being anything close. Thanks for any help! Cordially, Sam _______________________ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ________________________________ _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Dec 29 10:02:29 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 29 Dec 2008 09:02:29 -0000 Subject: [Tutor] Exception Handling References: <49582ABD.2030906@abbottdavid.com> <49582B15.7000108@abbottdavid.com> Message-ID: "David" wrote >> Is this the correct way to handle a ValueError exception and should >> I get in the practice of catching them? Yes and Yes. Although I woulfd move the except clause up to just after the input section (which is where the errors will be raised). A couple of other comments below: > while True: > try: > yr = int(raw_input("What year were you born? ")) > mn = int(raw_input("What month were you born? ")) > dy = int(raw_input("What day were you born? ")) > curr_date = time.strftime("%Y %m %d", time.gmtime()) > > ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) > mnum = int(time.strftime("%m", time.gmtime())) > dnum = int(time.strftime("%d", time.gmtime())) > mn = int(mn) > dy = int(dy) Put the except here, its easier to see where the error came from. And since the error message only applies to these int() calls its more accurate. > if mn - mnum: > print "You are %i" % ynum, "years old." This is an odd use of string formatting, more usually you would only use one string: print "You are %i years old" % ynum Either that or just insert the value and not use formattting: print "You are", ynum, "years old." > else: > ret = int(ynum) - 1 You don't need the int conversion here since yuou already did it at the input stage. But... > print "You are %i" % ret, "years old." Why not just print "You are %i" % ynum-1, "years old." HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Mon Dec 29 10:10:45 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 29 Dec 2008 09:10:45 -0000 Subject: [Tutor] Exception Handling References: <49582ABD.2030906@abbottdavid.com><49582B15.7000108@abbottdavid.com> <49583255.7080507@gmail.com> Message-ID: "bob gailer" wrote > Also IMHO it is bad design to put a lot of code inside a try block. > In this case the user might make a mistake on day and then is forced > to reenter the year and month! Obviously there is no absolute rule here but I disagree. One of the biggest advantages of try/except error handling is that it keeps the mess of handling errors out of the main logic of the code. This has two effects: 1) Code that is much easier to read 2) a lot less error handling code Using "big bite" try/except does mean the except clauses become more complex if you want to find out exactly which line caused the error, as in the example above, but it can be done by examining the traceback, but in general I much prefer to wrap logical blocks of code using try/except rather than only one or two lines > Better to write a function for requesting an integer from the user > which loops until the user gets it right (with some way to escape if > he gets frustrated and just wants to quit. And this I agree with. If you do want to check each line use a function that incorporates the checking within it and call that to get a guaranteed result. And that keeps both Bob and I happy with the try/excepts stylistically speaking :-) -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From alan.gauld at btinternet.com Mon Dec 29 10:18:43 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 29 Dec 2008 09:18:43 -0000 Subject: [Tutor] Printing the code of a function References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Message-ID: "wormwood_3" wrote > I am wondering if there is a way to print out the code of a defined > function. Its not reliable but I think you can use func.func_code.filename func.func_code.firstlineno To find the first line of code in the original source file. Its up to you to figure out the last line though! I guess checking for the next line with equivalent indentation might work. But I'm not sure how much good it would do you... unless you were writing a debugger maybe? Alan G From prasadaraon50 at gmail.com Mon Dec 29 16:40:32 2008 From: prasadaraon50 at gmail.com (prasad rao) Date: Mon, 29 Dec 2008 07:40:32 -0800 Subject: [Tutor] optional parameter in list comprehension Message-ID: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> hello! I am a novice looking up to the tutor as my Guide. I got a problem while using optional parameter. #! user/bin/env python import os def myfiles(directory,extension=None): for x in [os.path.join(x,i) for x,y,z in os.walk(directory)\ for i in z if i.endswith(extension)]:print x if __name__=='__main__': import sys myfiles(sys.argv[1],sys.argv[2]) >>> myfiles.myfiles('C:\\python26') Traceback (most recent call last): File "", line 1, in myfiles.myfiles('C:\\python26') File "C:\Python26\lib\site-packages\myfiles.py", line 6, in myfiles for i in z if i.endswith(extension)]:print x TypeError: expected a character buffer object >>> myfiles.myfiles('C:\\python26','doc') C:\python26\Doc\examples.doc How can I rectify this? Thanks Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From digitalxero at gmail.com Mon Dec 29 16:43:24 2008 From: digitalxero at gmail.com (Dj Gilcrease) Date: Mon, 29 Dec 2008 07:43:24 -0800 Subject: [Tutor] optional parameter in list comprehension In-Reply-To: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> References: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> Message-ID: def myfiles(directory,extension=None): for x in [os.path.join(x,i) for x,y,z in os.walk(directory)\ if extension: for i in z if i.endswith(extension)]:print x Dj Gilcrease OpenRPG Developer ~~http://www.openrpg.com From bgailer at gmail.com Mon Dec 29 16:56:20 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 29 Dec 2008 10:56:20 -0500 Subject: [Tutor] optional parameter in list comprehension In-Reply-To: References: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> Message-ID: <4958F324.8010402@gmail.com> Dj Gilcrease wrote: > def myfiles(directory,extension=None): > for x in [os.path.join(x,i) for x,y,z in os.walk(directory)\ > if extension: << SYNTAX ERROR! > for i in z if i.endswith(extension)]:print x > > -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Mon Dec 29 16:57:03 2008 From: bgailer at gmail.com (bob gailer) Date: Mon, 29 Dec 2008 10:57:03 -0500 Subject: [Tutor] optional parameter in list comprehension In-Reply-To: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> References: <9e3fac840812290740vd6bfe0cu62987dc76acf2253@mail.gmail.com> Message-ID: <4958F34F.8000006@gmail.com> prasad rao wrote: > hello! > I am a novice looking up to the tutor as my Guide. > I got a problem while using optional parameter. > > #! user/bin/env python > import os > > def myfiles(directory,extension=None): change that to def myfiles(directory,extension=""): > for x in [os.path.join(x,i) for x,y,z in os.walk(directory)\ > for i in z if i.endswith(extension)]:print x > > if __name__=='__main__': > import sys > myfiles(sys.argv[1],sys.argv[2]) > > >>> myfiles.myfiles('C:\\python26') > > Traceback (most recent call last): > File "", line 1, in > myfiles.myfiles('C:\\python26') > File "C:\Python26\lib\site-packages\myfiles.py", line 6, in myfiles > for i in z if i.endswith(extension)]:print x > TypeError: expected a character buffer object > >>> myfiles.myfiles('C:\\python26','doc') > C:\python26\Doc\examples.doc > > How can I rectify this? > > Thanks > > Prasad > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From denis.spir at free.fr Mon Dec 29 16:57:44 2008 From: denis.spir at free.fr (spir) Date: Mon, 29 Dec 2008 16:57:44 +0100 Subject: [Tutor] Exception Handling In-Reply-To: References: <49582ABD.2030906@abbottdavid.com> <49582B15.7000108@abbottdavid.com> <49583255.7080507@gmail.com> Message-ID: <20081229165744.7a1e262c@o> On Mon, 29 Dec 2008 09:10:45 -0000 "Alan Gauld" wrote: > > "bob gailer" wrote > > > Also IMHO it is bad design to put a lot of code inside a try block. > > In this case the user might make a mistake on day and then is forced > > to reenter the year and month! > > Obviously there is no absolute rule here but I disagree. > One of the biggest advantages of try/except error handling is > that it keeps the mess of handling errors out of the main logic > of the code. This has two effects: > 1) Code that is much easier to read > 2) a lot less error handling code > > Using "big bite" try/except does mean the except clauses > become more complex if you want to find out exactly which > line caused the error, as in the example above, but it can > be done by examining the traceback, but in general I much > prefer to wrap logical blocks of code using try/except rather > than only one or two lines I often use the else clause of try...except. This allows putting only the minimum problematic code lines inside the try block, which is good both for legibility andto avoid catching unexpected errors. The else will be executed only in case of no exception: try: problematic_instructions except ExceptionType, exception: expected_exception_handler else: further_standard_process denis ------ la vida e estranya From denis.spir at free.fr Mon Dec 29 17:20:56 2008 From: denis.spir at free.fr (spir) Date: Mon, 29 Dec 2008 17:20:56 +0100 Subject: [Tutor] Printing the code of a function In-Reply-To: References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Message-ID: <20081229172056.36ffbbb5@o> On Mon, 29 Dec 2008 09:18:43 -0000 "Alan Gauld" wrote: > > "wormwood_3" wrote > > > I am wondering if there is a way to print out the code of a defined > > function. > > Its not reliable but I think you can use > > func.func_code.filename > func.func_code.firstlineno > > To find the first line of code in the original source file. > Its up to you to figure out the last line though! I guess checking > for the next line with equivalent indentation might work. > > But I'm not sure how much good it would do you... unless you > were writing a debugger maybe? > > Alan G I would do it so by parsing source file: * (convert indents to tabs (easier to parse)) * search for a line that start with (n indents) + "def func_name" * record all following lines that start with (n+1 indents) or more * stop where a line starts with (n indents) or less denis ------ la vida e estranya From kent37 at tds.net Mon Dec 29 21:05:24 2008 From: kent37 at tds.net (Kent Johnson) Date: Mon, 29 Dec 2008 15:05:24 -0500 Subject: [Tutor] Printing the code of a function In-Reply-To: <749311.18420.qm@web110812.mail.gq1.yahoo.com> References: <749311.18420.qm@web110812.mail.gq1.yahoo.com> Message-ID: <1c2a2c590812291205t57d8d26do7fc049accc4bab7b@mail.gmail.com> On Sun, Dec 28, 2008 at 8:49 PM, wormwood_3 wrote: > Hello all, > > This might be trivially easy, but I was having a hard time searching on it > since all the component terms are overloaded:-) I am wondering if there is a > way to print out the code of a defined function. If the source code is available try inspect.getsource(). Kent From eduardo.susan at gmail.com Mon Dec 29 21:18:55 2008 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Mon, 29 Dec 2008 13:18:55 -0700 Subject: [Tutor] Modifying a QIF Message-ID: <9356b9f30812291218v4c64ea10o1969a164e38ae350@mail.gmail.com> Hello, this weekend I had fun using Python for text processing. I needed to change a qif converted from a ofx, using the tool MT2OFX (http://www.xs4all.nl/~csmale/mt2ofx/en/index.htm) I wanted to change transactions like these: D11/14/2008 MCHEQUE 102 17590807;Cheque or Preauth. Debit T-500.00 N102 ^ Into this: D11/14/2008 MCHEQUE 102 17590807;Cheque or Preauth. Debit T-500.00 N102 PLorne Koehn LHousing:Rent == That is, insert those given Payee and Category if the transaction was a cheque of 500.00 I came up with these two versions and would like to know which should be more efficient or practical. Could you point improvements? VERSION ONE: f = open('cibc.QIF', 'r').readlines() for line in range(len(f)): if f[line] == 'T-500.00\n': f[line+1] += 'PLorne Koehn\nLHousing:Rent\n' new = open('cibc.QIF', 'w') new.writelines(f) VERSION TWO: f = open('cibc.QIF', 'rw') g = open('newresult.qif', 'w') flag = 0 for line in f: if line == 'T-500.00\n': flag = 1 elif line.startswith('N') and flag: flag = 0 line += 'PLorne Koehn\nLHousing:Rent\n' g.write(line) f.close() #====== One thing like about version 1 is that I can overwrite the same file, and don't need to create another one. I haven't figured out how to do the same with the second version and overcoming the IOError. Thanks for your attention Edu From denis.spir at free.fr Mon Dec 29 23:52:24 2008 From: denis.spir at free.fr (spir) Date: Mon, 29 Dec 2008 23:52:24 +0100 Subject: [Tutor] Modifying a QIF In-Reply-To: <9356b9f30812291218v4c64ea10o1969a164e38ae350@mail.gmail.com> References: <9356b9f30812291218v4c64ea10o1969a164e38ae350@mail.gmail.com> Message-ID: <20081229235224.09b0d3de@o> On Mon, 29 Dec 2008 13:18:55 -0700 "Eduardo Vieira" wrote: > Hello, this weekend I had fun using Python for text processing. I > needed to change a qif converted from a ofx, using the tool MT2OFX > (http://www.xs4all.nl/~csmale/mt2ofx/en/index.htm) > I wanted to change transactions like these: > D11/14/2008 > MCHEQUE 102 17590807;Cheque or Preauth. Debit > T-500.00 > N102 > ^ > > Into this: > D11/14/2008 > MCHEQUE 102 17590807;Cheque or Preauth. Debit > T-500.00 > N102 > PLorne Koehn > LHousing:Rent > > == That is, insert those given Payee and Category if the transaction > was a cheque of 500.00 > > I came up with these two versions and would like to know which should > be more efficient or practical. Could you point improvements? > VERSION ONE: > f = open('cibc.QIF', 'r').readlines() > for line in range(len(f)): > if f[line] == 'T-500.00\n': > f[line+1] += 'PLorne Koehn\nLHousing:Rent\n' > > new = open('cibc.QIF', 'w') > new.writelines(f) > I would change 'line' --> 'line_nr' or 'lineno' or for line,index in enumerate(f) no! for index,line in enumerate(f) ;-) > VERSION TWO: > f = open('cibc.QIF', 'rw') > g = open('newresult.qif', 'w') > flag = 0 > for line in f: > if line == 'T-500.00\n': > flag = 1 > elif line.startswith('N') and flag: > flag = 0 > line += 'PLorne Koehn\nLHousing:Rent\n' > > g.write(line) > > f.close() > > #====== > > One thing like about version 1 is that I can overwrite the same > file, and don't need to create another one. I haven't figured out how > to do the same with the second version and overcoming the IOError. maybe by first writing on a string instead directly to a file -- then flush the whole text to the same file: text = "" # replaces g ... text += line ... f = open('cibc.QIF', 'w') f.write(text) > Thanks for your attention > > Edu > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > ------ la vida e estranya From alan.gauld at btinternet.com Tue Dec 30 00:02:21 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 29 Dec 2008 23:02:21 -0000 Subject: [Tutor] Exception Handling References: <49582ABD.2030906@abbottdavid.com><49582B15.7000108@abbottdavid.com> <49583255.7080507@gmail.com> <20081229165744.7a1e262c@o> Message-ID: "spir" wrote > I often use the else clause of try...except. This allows putting > only the > minimum problematic code lines inside the try block, which is good > both for > legibility andto avoid catching unexpected errors. The else will be > executed > only in case of no exception: > > try: > problematic_instructions > except ExceptionType, exception: > expected_exception_handler > else: > further_standard_process Thats exactly what I try to avoid. The big win for me in try/except is that I can put the whole logic of a function together with no error code confusing the picture. The above is no better than the old BASIC style error handling problemInstruction if errorcode == XXX: handle error further instruction I find that much harder to follow the normal case code than: try: problematic instructions more instructions more problems except Error1 handleError1 except Error2: handleError2 Now I can see the real algorithm and the errors are all tiucked out of the way at the bottom where I don't need to read them until its necessary. Assuming you don't wrte functions that are hundreds of lines long that doesn't cause me any problerms when debugging but greatly increases the readability The only somewhat valid case for doing line by line exceptions is IMHO the one that started this - where users are entering input and would need to start right at the beginning in the event of an error. But as Bob pointed out you can avoid that by writing a convertion function to do the error handling and validation and call it repeatedly. But it is largely a matter of personal taste. Alan G. From prasadaraon50 at gmail.com Tue Dec 30 11:25:55 2008 From: prasadaraon50 at gmail.com (prasad rao) Date: Tue, 30 Dec 2008 02:25:55 -0800 Subject: [Tutor] reply Message-ID: <9e3fac840812300225m4e28b586k5014ce054f91f39d@mail.gmail.com> Hello. Thank you.Just a small change at the optional parameter made all the difference. It seems to be a mirracle.It is an enlitenment to me. Thank you. Prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue Dec 30 13:59:36 2008 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 30 Dec 2008 12:59:36 +0000 (UTC) Subject: [Tutor] Exception Handling References: <49582ABD.2030906@abbottdavid.com> <49582B15.7000108@abbottdavid.com> <49583255.7080507@gmail.com> <20081229165744.7a1e262c@o> Message-ID: On Mon, 29 Dec 2008 16:57:44 +0100, spir wrote: > On Mon, 29 Dec 2008 09:10:45 -0000 > "Alan Gauld" wrote: > > >> "bob gailer" wrote >> >> > Also IMHO it is bad design to put a lot of code inside a try block. >> > In this case the user might make a mistake on day and then is forced >> > to reenter the year and month! >> >> Obviously there is no absolute rule here but I disagree. One of the >> biggest advantages of try/except error handling is that it keeps the >> mess of handling errors out of the main logic of the code. This has two >> effects: >> 1) Code that is much easier to read >> 2) a lot less error handling code >> >> Using "big bite" try/except does mean the except clauses become more >> complex if you want to find out exactly which line caused the error, as >> in the example above, but it can be done by examining the traceback, >> but in general I much prefer to wrap logical blocks of code using >> try/except rather than only one or two lines > > I often use the else clause of try...except. This allows putting only > the minimum problematic code lines inside the try block, which is good > both for legibility andto avoid catching unexpected errors. The else > will be executed only in case of no exception: I think it's better to put less in a try-clause so I'd rather not catch errors on the raw_input, and put validation code in a function, which I'd enclose in a try block, like this: while True: yr = raw_input("What year were you born? ") mn = raw_input("What month were you born? ") dy = raw_input("What day were you born? ") try: date = is_valid_date(yr, mn, dy) except InvalidDate, e: # e might contain input not a number, or there is no 31 february print 'You entered an Invalid Date: ', e else: do_things(date) Also, I'd rather ask for the dates in one raw_input, cuts much of the mess for the user (although it's a bit of extra codes) From norman at khine.net Tue Dec 30 15:32:38 2008 From: norman at khine.net (Norman Khine) Date: Tue, 30 Dec 2008 15:32:38 +0100 Subject: [Tutor] optimize code Message-ID: <495A3106.8040105@khine.net> Hello, I have this piece of code which I would like to further streamline it. http://paste.lisp.org/display/72830 More specifically, I feel there is too many if statements. What the code does is that it takes a form value and builds a dictionary http://localhost/;search?level1=air-brokers-or-charters returns {'root_title': u'Expert Travel', 'here_title': 'Air brokers/air charter', 'country': 'UK'} http://localhost/;search?level1=air-brokers-or-charters&level2=East+Anglia returns {'root_title': u'Expert Travel', 'level2': 'East Anglia', 'here_title': 'Air brokers/air charter', 'country': 'UK'} etc.. Thanks Norman From kent37 at tds.net Tue Dec 30 16:04:00 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 30 Dec 2008 10:04:00 -0500 Subject: [Tutor] optimize code In-Reply-To: <495A3106.8040105@khine.net> References: <495A3106.8040105@khine.net> Message-ID: <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> On Tue, Dec 30, 2008 at 9:32 AM, Norman Khine wrote: > Hello, > I have this piece of code which I would like to further streamline it. There is no need to re-create the mapping at each level of if; you can just add new key/value pairs. For example if level2 is not None: mapping['level2'] = level2 > What the code does is that it takes a form value and builds a dictionary It seems that the you are really returning a string, or at least building a string to pass to gettext(). I would have each if statement just append to the string. That would consolidate the return statements. For example, if value is the string being built, if level2 is not None: value = '%s: %s' % (value, level2) Kent From norman at khine.net Tue Dec 30 16:56:32 2008 From: norman at khine.net (Norman Khine) Date: Tue, 30 Dec 2008 16:56:32 +0100 Subject: [Tutor] optimize code In-Reply-To: <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> References: <495A3106.8040105@khine.net> <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> Message-ID: <495A44B0.9090806@khine.net> Kent Johnson wrote: > On Tue, Dec 30, 2008 at 9:32 AM, Norman Khine wrote: >> Hello, >> I have this piece of code which I would like to further streamline it. > > There is no need to re-create the mapping at each level of if; you can > just add new key/value pairs. For example > if level2 is not None: > mapping['level2'] = level2 > >> What the code does is that it takes a form value and builds a dictionary > > It seems that the you are really returning a string, or at least > building a string to pass to gettext(). I would have each if statement > just append to the string. That would consolidate the return > statements. For example, if value is the string being built, > if level2 is not None: > value = '%s: %s' % (value, level2) Thanks, it is very helpful. Norman From yodaalmighty2000 at gmail.com Tue Dec 30 18:05:29 2008 From: yodaalmighty2000 at gmail.com (Pearce Michal) Date: Tue, 30 Dec 2008 12:05:29 -0500 Subject: [Tutor] IDLE problems Message-ID: <77d9eeef0812300905l2dbae2cdrf5c4729e3cda94a0@mail.gmail.com> ok, so I just started working with python, however I have been working with Java for awhile, and am fairly familiar with it. my problem with Python is for the IDLE editor, and it is this: the return key, or enter key not only moves the editor to the next line, but also executes the script. so if I tried to enter a line like: x = input("enter a value: ") if x == 1: print "you entered one" elif x == 2: print "you entered two" elif x == 3: print "you entered three" after I entered the line " x = input("enter a value: ") and ask "enter a value: " I would type a number, and the script would end. basically, I am wondering if there is a way to make it not execute when you press return. I looked through the options, and it said the F5 key is what executes the script, (but I am on a laptop, so pressing F5 turns up the volume) anyway, please help! thanking to you in advance, Yoda Almighty. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 30 19:04:49 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 30 Dec 2008 13:04:49 -0500 Subject: [Tutor] IDLE problems In-Reply-To: <77d9eeef0812300905l2dbae2cdrf5c4729e3cda94a0@mail.gmail.com> References: <77d9eeef0812300905l2dbae2cdrf5c4729e3cda94a0@mail.gmail.com> Message-ID: <1c2a2c590812301004g5df08e81sa7f59b25bda1877f@mail.gmail.com> On Tue, Dec 30, 2008 at 12:05 PM, Pearce Michal wrote: > ok, so I just started working with python, however I have been working with > Java for awhile, and am fairly familiar with it. my problem with Python is > for the IDLE editor, and it is this: the return key, or enter key not only > moves the editor to the next line, but also executes the script. IDLE has two kinds of windows - a shell window, which executes commands as they are typed, and an editor window. The shell window is handy for interactive exploration but, as you discovered, pretty useless for creating a real program. File / New Window will open an editor window that behaves the way you want. Kent From norman at khine.net Tue Dec 30 21:42:00 2008 From: norman at khine.net (Norman Khine) Date: Tue, 30 Dec 2008 21:42:00 +0100 Subject: [Tutor] optimize code In-Reply-To: <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> References: <495A3106.8040105@khine.net> <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> Message-ID: <495A8798.5040402@khine.net> Hi, I have an updated version at http://paste.lisp.org/display/72843 Thanks for the advice, the code is much smaller and leaner than the first attempt. Norman Kent Johnson wrote: > On Tue, Dec 30, 2008 at 9:32 AM, Norman Khine wrote: >> Hello, >> I have this piece of code which I would like to further streamline it. > > There is no need to re-create the mapping at each level of if; you can > just add new key/value pairs. For example > if level2 is not None: > mapping['level2'] = level2 > >> What the code does is that it takes a form value and builds a dictionary > > It seems that the you are really returning a string, or at least > building a string to pass to gettext(). I would have each if statement > just append to the string. That would consolidate the return > statements. For example, if value is the string being built, > if level2 is not None: > value = '%s: %s' % (value, level2) > > Kent > From david at abbottdavid.com Tue Dec 30 22:33:02 2008 From: david at abbottdavid.com (David) Date: Tue, 30 Dec 2008 16:33:02 -0500 Subject: [Tutor] Exception Handling Message-ID: <495A938E.1020509@abbottdavid.com> > On Mon, 29 Dec 2008 16:57:44 +0100, spir wrote: > >> > On Mon, 29 Dec 2008 09:10:45 -0000 >> > "Alan Gauld" wrote: >> > >> > >>> >> "bob gailer" wrote >>> >> >>>> >> > Also IMHO it is bad design to put a lot of code inside a try block. >>>> >> > In this case the user might make a mistake on day and then is forced >>>> >> > to reenter the year and month! >>> >> >>> >> Obviously there is no absolute rule here but I disagree. One of the >>> >> biggest advantages of try/except error handling is that it keeps the >>> >> mess of handling errors out of the main logic of the code. This has two >>> >> effects: >>> >> 1) Code that is much easier to read >>> >> 2) a lot less error handling code > Also, I'd rather ask for the dates in one raw_input, cuts much of the > mess for the user (although it's a bit of extra codes) Thank you all for the tips. Next to do is to get the dates in one raw_input with the correct format and to check for a valid year, month, and day. Here is what I have now; #!/usr/bin/python import time curr_date = time.strftime("%Y %m %d", time.gmtime()) print "Please enter the date format as: ", curr_date while True: yr = raw_input("\nWhat year were you born? ") mn = raw_input("What month were you born? ") dy = raw_input("What day were you born? ") try: ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) mnum = int(time.strftime("%m", time.gmtime())) dnum = int(time.strftime("%d", time.gmtime())) except ValueError: print "Oops, You must enter a number!" else: mn = int(mn) dy = int(dy) if mn <= mnum: print "You are", ynum, "years old." break elif mn == mnum and dy < dnum: print "You are", ynum, "years old." break else: ret = int(ynum) - 1 print "You are", ret, "years old." break -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From jervisau at gmail.com Tue Dec 30 23:12:09 2008 From: jervisau at gmail.com (Jervis Whitley) Date: Wed, 31 Dec 2008 09:12:09 +1100 Subject: [Tutor] Exception Handling In-Reply-To: <495A938E.1020509@abbottdavid.com> References: <495A938E.1020509@abbottdavid.com> Message-ID: <8e63a5ce0812301412i3dfa74bchfd60fc40ab625a2f@mail.gmail.com> On Wed, Dec 31, 2008 at 8:33 AM, David wrote: > On Mon, 29 Dec 2008 16:57:44 +0100, spir wrote: >> >> > On Mon, 29 Dec 2008 09:10:45 -0000 >>> > "Alan Gauld" wrote: >>> > > >>> >>>> >> "bob gailer" wrote >>>> >> >>>> >>>>> >> > Also IMHO it is bad design to put a lot of code inside a try >>>>> block. >>>>> >> > In this case the user might make a mistake on day and then is >>>>> forced >>>>> >> > to reenter the year and month! >>>>> >>>> >> >> Obviously there is no absolute rule here but I disagree. One of >>>> the >>>> >> biggest advantages of try/except error handling is that it keeps the >>>> >> mess of handling errors out of the main logic of the code. This has >>>> two >>>> >> effects: >>>> >> 1) Code that is much easier to read >>>> >> 2) a lot less error handling code >>>> >>> Also, I'd rather ask for the dates in one raw_input, cuts much of the >> mess for the user (although it's a bit of extra codes) >> > Thank you all for the tips. Next to do is to get the dates in one raw_input > with the correct format and to check for a valid year, month, and day. Here > is what I have now; > #!/usr/bin/python > import time > > curr_date = time.strftime("%Y %m %d", time.gmtime()) > print "Please enter the date format as: ", curr_date > > while True: > yr = raw_input("\nWhat year were you born? ") > mn = raw_input("What month were you born? ") > dy = raw_input("What day were you born? ") > try: > ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) > mnum = int(time.strftime("%m", time.gmtime())) > dnum = int(time.strftime("%d", time.gmtime())) > except ValueError: > print "Oops, You must enter a number!" > else: > mn = int(mn) > dy = int(dy) > > if mn <= mnum: > print "You are", ynum, "years old." > break > elif mn == mnum and dy < dnum: > print "You are", ynum, "years old." > break > else: > ret = int(ynum) - 1 > print "You are", ret, "years old." > break > > > -- > Powered by Gentoo GNU/LINUX > http://www.linuxcrazy.com > pgp.mit.edu > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Hi David, If the user enters incorrect data for the month or day, a ValueError will still be raised on the conversion to integer. I suggest that you wrap your request for user information in a function that does the checking for you. You can re-use this function for each piece of integer information you require from the user. example: import time class BadUserError(Exception): pass def get_integer(retrieve, question, attempts=3): """ A small function to attempt to retrieve information from a user, given a prompt question. retrive - any function that will accept a string as an argument and return a string or otherwise response from the user. question - a string type question that you would like to ask the user to respond to. attempts[optional] - how many times the user can incorrectly enter data before the BadUserError is raised. """ while attempts > 0: num = retrieve(question) try: # try casting the user input as an integer. return int(num) except ValueError: print "Oops, You must enter a number!" attempts -= 1 raise BadUserError("Too many incorrect tries!") curr_date = time.strftime("%Y %m %d", time.gmtime()) print "Please enter the date format as: ", curr_date yr = get_integer(raw_input, "\nWhat year were you born? ") mn = get_integer(raw_input, "What month were you born? ") dy = get_integer(raw_input, "What day were you born? ") today = time.gmtime() ynum = today.tm_year - yr mnum = today.tm_mon dnum = today.tm_mday if mn <= mnum: print "You are", ynum, "years old." elif mn == mnum and dy < dnum: print "You are", ynum, "years old." else: ret = int(ynum) - 1 print "You are", ret, "years old." -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Dec 30 23:23:42 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 30 Dec 2008 17:23:42 -0500 Subject: [Tutor] optimize code In-Reply-To: <495A8798.5040402@khine.net> References: <495A3106.8040105@khine.net> <1c2a2c590812300704n618f6665o19facfaf6a6905d1@mail.gmail.com> <495A8798.5040402@khine.net> Message-ID: <1c2a2c590812301423p758d389dpb60aba74e643d447@mail.gmail.com> On Tue, Dec 30, 2008 at 3:42 PM, Norman Khine wrote: > Hi, > I have an updated version at http://paste.lisp.org/display/72843 > Thanks for the advice, the code is much smaller and leaner than the first > attempt. The level2-4 code could all be consolidated into a loop: for levelName in 'level2 level3 level4'.split(): level = context.get_form_value(levelName) if level is None: break title = '%s: %s' % (title, level) Kent From kent37 at tds.net Tue Dec 30 23:29:22 2008 From: kent37 at tds.net (Kent Johnson) Date: Tue, 30 Dec 2008 17:29:22 -0500 Subject: [Tutor] Exception Handling In-Reply-To: <495A938E.1020509@abbottdavid.com> References: <495A938E.1020509@abbottdavid.com> Message-ID: <1c2a2c590812301429w76745713rbbd15a520e8315cf@mail.gmail.com> On Tue, Dec 30, 2008 at 4:33 PM, David wrote: > Thank you all for the tips. Next to do is to get the dates in one raw_input > with the correct format and to check for a valid year, month, and day. Here > is what I have now; > ynum = int(time.strftime("%Y", time.gmtime())) - int(yr) > mnum = int(time.strftime("%m", time.gmtime())) > dnum = int(time.strftime("%d", time.gmtime())) Yikes! How about time.gmtime().tm_year, etc, instead of all the calls to strftime() and int()? And you might want to put the value of time.gmtime() in a variable so you always use the same value, just in case someone runs your program on New Year's Eve at midnight. You might also want to look at the datetime module, in particular datetime.timedelta. Kent From david at abbottdavid.com Wed Dec 31 01:18:47 2008 From: david at abbottdavid.com (David) Date: Tue, 30 Dec 2008 19:18:47 -0500 Subject: [Tutor] Exception Handling In-Reply-To: <8e63a5ce0812301412i3dfa74bchfd60fc40ab625a2f@mail.gmail.com> References: <495A938E.1020509@abbottdavid.com> <8e63a5ce0812301412i3dfa74bchfd60fc40ab625a2f@mail.gmail.com> Message-ID: <495ABA67.9020706@abbottdavid.com> Jervis Whitley wrote: > > > On Wed, Dec 31, 2008 at 8:33 AM, David > wrote: > > On Mon, 29 Dec 2008 16:57:44 +0100, spir wrote: > > > On Mon, 29 Dec 2008 09:10:45 -0000 > > "Alan Gauld" > wrote: > > > > > >> "bob gailer" > wrote > >> > Hi David, > > If the user enters incorrect data for the month or day, a ValueError > will still be raised > on the conversion to integer. > > I suggest that you wrap your request for user information in a function > that does the checking > for you. You can re-use this function for each piece of integer > information you require from the user. > > example: > > import time > > > class BadUserError(Exception): > pass > > def get_integer(retrieve, question, attempts=3): > """ > A small function to attempt to retrieve > information from a user, given a prompt question. > > retrive - any function that will accept a string as an argument > and return a string or otherwise response from the user. > question - a string type question that you would like to ask the user to > respond to. > attempts[optional] - how many times the user can incorrectly > enter data before the BadUserError is raised. > """ > > while attempts > 0: > num = retrieve(question) > try: > # try casting the user input as an integer. > return int(num) > except ValueError: > print "Oops, You must enter a number!" > > attempts -= 1 > raise BadUserError("Too many incorrect tries!") > WOW, thanks Jervis, I had to edit some of my mistakes to get it to work right if your birthday is today. I also looked at tm_year, tm_mon, and tm_day. I still need to get it to prduce an error if the year is 0 or 2009, the month is 0 or 13 and the day is 0 or 32. david [06:56 PM] opteron ~ $ ./py_get_age.py Please enter the date format as: 2008 12 30 What year were you born? 2010 What month were you born? 14 What day were you born? 34 You are -3 years old. Here is a link to the current program; http://dwabbott.com/code/ Thanks again everyone, -david -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From jervisau at gmail.com Wed Dec 31 01:37:52 2008 From: jervisau at gmail.com (Jervis Whitley) Date: Wed, 31 Dec 2008 11:37:52 +1100 Subject: [Tutor] Exception Handling In-Reply-To: <495ABA67.9020706@abbottdavid.com> References: <495A938E.1020509@abbottdavid.com> <8e63a5ce0812301412i3dfa74bchfd60fc40ab625a2f@mail.gmail.com> <495ABA67.9020706@abbottdavid.com> Message-ID: <8e63a5ce0812301637g64f048d3k349e961b511e8ed1@mail.gmail.com> On Wed, Dec 31, 2008 at 11:18 AM, David wrote: > > . I still need to get it to prduce an error if the year is 0 or 2009, the > month is 0 or 13 and the day is 0 or 32. Try using the datetime module to check validity of entered data. example: >>> import datetime >>> datetime.datetime(2008, 12, 32) # should be an error, there are only 31 days in December! ValueError: day is out of range for month > david [06:56 PM] opteron ~ $ ./py_get_age.py > Please enter the date format as: 2008 12 30 > > What year were you born? 2010 > What month were you born? 14 > What day were you born? 34 > You are -3 years old. You can also use it to check if the input is in the future. >>> userdate = datetime.datetime(2010, 1, 1) >>> datetime.datetime.now() > userdate # a check to see if userdate is in the past. False Cheers, Jervis -------------- next part -------------- An HTML attachment was scrubbed... URL: From sdragon1984 at gmail.com Wed Dec 31 08:22:14 2008 From: sdragon1984 at gmail.com (nathan virgil) Date: Wed, 31 Dec 2008 02:22:14 -0500 Subject: [Tutor] Creating sub-menus? Message-ID: <111a9ddb0812302322sdef57daxb3639ff57ef326ab@mail.gmail.com> I was reading the Non-Programmer's Tutorial for Python, and became really proud of myself when I realized I could create a menu for functions. I decided to try to take this one step further and see if I could create not just a menu, but a menu with sub-menus, too! Ultimately, the idea I came up with is this: Each menu is a function that prints out options, saves a raw_input as the variable choice, and returns choice. In the main menu, each option leads to a sub-menu. After choice is defined, however, the sub-menu "tags" the value of choice. Each sub-menu has it's own "tag", so that the program can tell the first choice of sub-menu A from the first choice of sub-menu B. A sub-menu function would end with code similar to: choice = raw_input("What's your choice?") choice = "a" + choice return choice Once I get all the menus and other functions out of the way, I get to the part of the code that actually chooses which function to run. Here, I start out with: choice = "start" current_menu = main_menu() Then create a loop of while choice !=q, run current_menu, and include a bunch of statements along the lines of: if choice == : current_menu = elif choice == This seems like it would work, but for some reason, every time I run the code, it freezes after I give input from the main menu. Can anybody help? I can show my source code, but indentation doesn't seem to copy/paste very well, so it may be a bit hard to read... -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 31 10:10:45 2008 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 31 Dec 2008 09:10:45 -0000 Subject: [Tutor] Creating sub-menus? References: <111a9ddb0812302322sdef57daxb3639ff57ef326ab@mail.gmail.com> Message-ID: "nathan virgil" wrote > Each menu is a function that prints out options, saves a raw_input > as the > variable choice, and returns choice. In the main menu, each option > leads to > a sub-menu. After choice is defined, however, the sub-menu "tags" > the value > of choice. Yes that can all work. > Then create a loop of while choice !=q, run current_menu, and > include a > bunch of statements along the lines of: > > if choice == : > current_menu = Consider using a dictionary keyed by your combined choice values. Then the big if/elif chain shrinks to returnValue = FuncDict[choice](params) The only challenge with this route is making all the functions take a single input argument. But that argument can be a tuple :-) > This seems like it would work, but for some reason, every time I run > the > code, it freezes after I give input from the main menu. Can anybody > help? I > can show my source code, but indentation doesn't seem to copy/paste > very > well, so it may be a bit hard to read... Try putting it on the pastebin web site and sending us the URL. That gives us colour coding of syntax too which helps read it! HTH -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld From kent37 at tds.net Wed Dec 31 14:05:39 2008 From: kent37 at tds.net (Kent Johnson) Date: Wed, 31 Dec 2008 08:05:39 -0500 Subject: [Tutor] Creating sub-menus? In-Reply-To: <111a9ddb0812302322sdef57daxb3639ff57ef326ab@mail.gmail.com> References: <111a9ddb0812302322sdef57daxb3639ff57ef326ab@mail.gmail.com> Message-ID: <1c2a2c590812310505g75f3daddj1171384c16ec391a@mail.gmail.com> On Wed, Dec 31, 2008 at 2:22 AM, nathan virgil wrote: > I was reading the Non-Programmer's Tutorial for Python, and became really > proud of myself when I realized I could create a menu for functions. I > decided to try to take this one step further and see if I could create not > just a menu, but a menu with sub-menus, too! Ultimately, the idea I > came up with is this: > > Each menu is a function that prints out options, saves a raw_input as the > variable choice, and returns choice. In the main menu, each option leads to > a sub-menu. After choice is defined, however, the sub-menu "tags" the value > of choice. Each sub-menu has it's own "tag", so that the program can tell > the first choice of sub-menu A from the first choice of sub-menu B. A > sub-menu function would end with code similar to: > > choice = raw_input("What's your choice?") > choice = "a" + choice > return choice > > Once I get all the menus and other functions out of the way, I get to the > part of the code that actually chooses which function to run. Here, I start > out with: > > choice = "start" > current_menu = main_menu() > > Then create a loop of while choice !=q, run current_menu, and include a > bunch of statements along the lines of: > > if choice == : > current_menu = > elif choice == > You might want to look at the cmd module in the std lib, either as something to build on or an example of dispatching commands: http://docs.python.org/library/cmd.html cmd uses introspection to dispatch to commands, rather than if/elif or a hand-build dispatch dict. There are a couple of enhancements to cmd, also: http://catherine.devlin.googlepages.com/cmd2.html http://code.google.com/p/cmdln/ HTH, Kent