Re: strange interaction between open and cwd
On Wed, 05 May 2010 02:41:09 +0100, Baz Walter wrote: > i think the algorithm also can't guarantee the intended result when > crossing filesystem boundaries. IIUC, a stat() call on the root directory > of a mounted filesystem will give the same inode number as its parent. Nope; it will have the same dev/inode pair as if it wasn't mounted, i.e. the device will refer to the mounted device, not the device it's mounted on, and the inode will be the mounted filesystem's root inode (typically #2 for Linux ext2/ext3 filesystems). And stat()ing the appropriate entry in the parent directory will return the same information, i.e. the root inode of the mounted device, not the subdirectory of the parent device (as you would see if the filesystem was unmounted). IOW, if stat("foo") reports a different device to stat("."), "foo" is a mount point, while if stat("..") reports a different device to stat("."), the current directory is the root of a mounted filesystem. > so > if several filesystems are mounted in the same parent directory, there is > no way to tell which of them is the "right" one. The only case which would cause a problem here is if you mount the same device on two different subdirectories of a common directory. But in that case, it doesn't really matter which answer you get, as they're both equivalent in any sense that matters. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast Efficient way to transfer an object to another list
En Fri, 30 Apr 2010 23:16:04 -0300, Jimbo escribió: Hello I have a relatively simple thing to do; move an object from one to list into another. But I think my solution maybe inefficient & slow. Is there a faster better way to move my stock object from one list to another? (IE, without having to use a dictionary instead of a list or is that my only solution?) [code] class stock: code = "NULL" price = 0 stock_list1 = [] stock_list2 = [] def transfer_stock(stock_code, old_list, new_list): """ Transfer a stock from one list to another """ # is there a more efficient & faster way to index = 0 for stock in old_list: temp_stock = stock if temp_stock.code == stock_code: new_list.append(temp_stock) del old_list[index] index += 1 return new_list[/code] I'd do that in two steps: def transfer_stock(stock_code, old_list, new_list): # find the indexes to transfer indexes = [i for i,stock in enumerate(old_list) if stock.code==stock_code] # actually transfer them for index in reversed(indexes): stock = old_list[index] new_list.append(stock) del old_list[index] # I would not return anything -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Sphinx hosting
On Wed, May 5, 2010 at 3:35 PM, Michele Simionato wrote: > I am sure it has, but I was talking about just putting in the > repository an index.html file and have it published, the wayI hear it > works in BitBucket and GitHub. I'm pretty sure Google Code Hosting doesn't support rendering text/html mime-type files in the repository (like Trac can). On a side-note, not sure if you're interested in this at all... I wrote (for the hell/fun of it) a "Sphinx Server", here's the code: http://codepad.org/ywo8pscb This uses the latest development version of circuits (1) cheers James 1. http://bitbucket.org/prologic/circuits/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Sphinx hosting
On May 5, 6:39 am, James Mills wrote: > On Wed, May 5, 2010 at 2:08 PM, Michele Simionato > > wrote: > > Interesting. I tried to see if the same was true for the Wiki in > > Google code but apparently it does not work. Does anybody here know if > > it is possible to publish raw html in the Google Code wiki and how > > does it work? > > I may be wrong, but I recall that Google Code Hosting's Wiki Engine > has a macro that will allow you to render raw HTML. > > cheers > James I am sure it has, but I was talking about just putting in the repository an index.html file and have it published, the wayI hear it works in BitBucket and GitHub. -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI python 3 write RAW BINARY
En Sat, 01 May 2010 07:52:01 -0300, Dodo escribió: Le 30/04/2010 17:52, Antoine Pitrou a écrit : Le Thu, 29 Apr 2010 23:37:32 +0200, Dodo a écrit : I don't get a thing. Now with the fix : All browsers shows a different thing, but not the image! http://ddclermont.homeip.net/misc/python/ If I save it to computer : * Windows image viewer won't read it * Irfanview can read it without problems Did you set the content-type and content-length in the HTTP headers? Can you post your code? I didn't know about content-lenght Here's the new code (I used a fixed image patch to make sure this is not the source of the problem) #!/usr/bin/python3 import cgi, sys, cgitb cgitb.enable() f = open("/home/dodo/54.jpg", "rb") data = f.read() l = len(data) f.close() print("Content-type:image/jpg\nContent-length:%d\n\n" % l) sys.stdout.flush() sys.stdout.buffer.write( data ) Computers are dumb. You have to speak to them very slow and clearly in order to be understood :) You need a space after those ':'. The correct media type for JPEG images is image/jpeg. And (the important thing) you have one more \n than necessary. Each header field finishes with \n; an empty line (just \n) marks the end of all headers; the body [your image] must follow immediately. #!/usr/bin/python3 import cgi, sys, cgitb cgitb.enable() f = open("/home/dodo/54.jpg", "rb") data = f.read() l = len(data) f.close() print("Content-Type: image/jpeg\nContent-Length: %d\n" % l) sys.stdout.flush() sys.stdout.buffer.write( data ) (Probably, a better way would be to replace all those \n with \r\n, and not use print at all, but the above code is good enough). -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Sphinx hosting
On Wed, May 5, 2010 at 2:08 PM, Michele Simionato wrote: > Interesting. I tried to see if the same was true for the Wiki in > Google code but apparently it does not work. Does anybody here know if > it is possible to publish raw html in the Google Code wiki and how > does it work? I may be wrong, but I recall that Google Code Hosting's Wiki Engine has a macro that will allow you to render raw HTML. cheers James -- http://mail.python.org/mailman/listinfo/python-list
Re: Sharing a program I wrote
On Tue, May 4, 2010 at 9:16 PM, Lie Ryan wrote: > On 05/05/10 13:25, Scott wrote: >> I would like to post it to >> comp.lang.python but the main file is 169 lines long and the file for >> functions is 316 lines long. I'm thinking that is a little long for >> this format. Maybe I can put them up on a basic web page or file >> sharing site and just post a link. Is that well received on this >> forum? >> > Make it as easy as possible for people to get to your code; file sharing > site isn't very good for this (especially those that have wait time and > download limits and if you have to zip your file). If you're setting up > a webpage you need to take care not to mess up special characters. Using > pastebin is fairly well-received as people won't have to save the file > to their harddrive to see the content and their page is specifically set > up for displaying code (syntax highlighting and stuffs). > > If your code is specifically reusable or if it illustrates a certain > concept, posting it as Activestate recipe is great as well. In case Scott's not familiar, obligatory link: http://code.activestate.com/recipes/langs/python/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Sharing a program I wrote
On 05/05/10 13:25, Scott wrote: > James, > > Thanks for the comprehensive reply. I would like to post it to > comp.lang.python but the main file is 169 lines long and the file for > functions is 316 lines long. I'm thinking that is a little long for > this format. Maybe I can put them up on a basic web page or file > sharing site and just post a link. Is that well received on this > forum? > > Thanks, > Scott Make it as easy as possible for people to get to your code; file sharing site isn't very good for this (especially those that have wait time and download limits and if you have to zip your file). If you're setting up a webpage you need to take care not to mess up special characters. Using pastebin is fairly well-received as people won't have to save the file to their harddrive to see the content and their page is specifically set up for displaying code (syntax highlighting and stuffs). If your code is specifically reusable or if it illustrates a certain concept, posting it as Activestate recipe is great as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sphinx hosting
On May 4, 9:48 am, James Mills wrote: > On Tue, May 4, 2010 at 5:27 PM, Michele Simionato > > wrote: > > Cool, that's good to know. I am still accepting recommendations for > > non-Python projects ;) > > bitbucket (1) also provide static file hosting through the wiki. From > what I understand (tested) > you simply clone the wiki repository (which is it's own repository) > and commit a bunch of .html > files. > > cheers > James > > 1.http://bitbucket.org/ Interesting. I tried to see if the same was true for the Wiki in Google code but apparently it does not work. Does anybody here know if it is possible to publish raw html in the Google Code wiki and how does it work? -- http://mail.python.org/mailman/listinfo/python-list
Re: Exclusively lock a file to prevent other processes from reading it?
Philip Semanchuk wrote: On May 4, 2010, at 5:37 PM, pyt...@bdurham.com wrote: Is there a way to exclusively lock a file to prevent other processes from reading it while we have it open? If you can use SQLite to store the data, it will deal with your locking problems. The pain of getting locking right has already been dealt with by others. Classic lock files are iffy. They're not race condition free on NTFS, and all the junk needed to clean up lock files properly after a crash is a headache. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Sharing a program I wrote
James, Thanks for the comprehensive reply. I would like to post it to comp.lang.python but the main file is 169 lines long and the file for functions is 316 lines long. I'm thinking that is a little long for this format. Maybe I can put them up on a basic web page or file sharing site and just post a link. Is that well received on this forum? Thanks, Scott -- http://mail.python.org/mailman/listinfo/python-list
A python interface to google-sparsehash?
Anybody knows if a python sparsehash module is there in the wild? -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 05/05/10 00:44, Nobody wrote: On Tue, 04 May 2010 14:36:06 +0100, Baz Walter wrote: this will work so long as the file is in a part of the filesystem that can be traversed from the current directory to the root. what i'm not sure about is whether it's possible to cross filesystem boundaries using this kind of technique. At least on Linux, the kernel "fixes" the links at mount points, i.e. within the root directory of a mounted filesystem, ".." refers to the directory containing the mount point on the parent filesystem, while the mount point refers to the root directory of the mounted filesystem. This also appears to work correctly for bind mounts (mounting an arbitrary directory to another directory, which results in a directory hierarchy appearing at multiple locations within the filesystem), i.e. ".." refers to the appropriate directory for each "instance". OTOH, the algorithm can fail if a directory is moved (whether by rename() or remounting) between the stat("..") and the listdir(). i think the algorithm also can't guarantee the intended result when crossing filesystem boundaries. IIUC, a stat() call on the root directory of a mounted filesystem will give the same inode number as its parent. so if several filesystems are mounted in the same parent directory, there is no way to tell which of them is the "right" one. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?
On May 5, 3:43 am, Terry Reedy wrote: > On 5/4/2010 11:37 AM, Stefan Behnel wrote: > > > Barak, Ron, 04.05.2010 16:11: > >> The XML file seems to be valid XML (all XML viewers I tried were able > >> to read it). > > From Internet Explorer: > > The XML page cannot be displayed > Cannot view XML input using XSL style sheet. Please correct the error > and then click the Refresh button, or try again later. > > > > An invalid character was found in text content. Error processing > resource 'file:///C:/Documents and Settings... > > "BROLB21 > > > > > This is what xmllint gives me: > > > --- > > $ xmllint /home/sbehnel/tmp.xml > > tmp.xml:6: parser error : Char 0x0 out of allowed range > > "MainStorage_snap > > ^ > > tmp.xml:6: parser error : Premature end of data in tag m_sanApiName1 line 6 > > "MainStorage_snap > > ^ > > tmp.xml:6: parser error : Premature end of data in tag DbHbaGroup line 5 > > "MainStorage_snap > > ^ > > tmp.xml:6: parser error : Premature end of data in tag database line 4 > > "MainStorage_snap > > ^ > > --- > > > The file contains 0-bytes - clearly not XML. > > IE agrees. Look closer. IE *DOESN'T* agree. It has ignored the problem on line 6 and lurched on to the next problem (in line 11). If you edit that file to remove the line noise in line 11, leaving the 3 cases of multiple \x00 bytes, IE doesn't complain at all about the (invalid) \x00 bytes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ed Keith wrote: > Knuth wanted the generated source to be unreadable, so people would not be > tempted to edit the generated code. This is my biggest issue with Knuth's view of literate programming. If the generated source isn't readable, am I just supposed to trust it? How can I tell if an error lies in my expression of the algorithm or in the code generation itself? -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating nested code with context managers
On May 5, 6:36 am, Terry Reedy wrote: > The relatively new with statement and associated context managers are > designed, among other things, for this situation, where one needs to > alter and restore a global context. So here is my updated (3.1) > proof-of-concept version. This is what I love about c.l.p, you wait 10 mins and someone proves your point with working code :) I had a more generic 'with indentation:' context manager in mind, but I really like your approach. Cheers. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?
On May 5, 12:11 am, "Barak, Ron" wrote: > > -Original Message- > > From: Stefan Behnel [mailto:stefan...@behnel.de] > > Sent: Tuesday, May 04, 2010 10:24 AM > > To: python-l...@python.org > > Subject: Re: How to get xml.etree.ElementTree not bomb on > > invalid characters in XML file ? > > > Barak, Ron, 04.05.2010 09:01: > > > I'm parsing XML files using ElementTree from xml.etree (see code > > > below (and attached xml_parse_example.py)). > > > > However, I'm coming across input XML files (attached an example: > > > tmp.xml) which include invalid characters, that produce the > > following > > > traceback: > > > > $ python xml_parse_example.py > > > Traceback (most recent call last): > > > xml.parsers.expat.ExpatError: not well-formed (invalid > > token): line 6, > > > column 34 > > > I hope you are aware that this means that the input you are > > parsing is not XML. It's best to reject the file and tell the > > producers that they are writing broken output files. You > > should always fix the source, instead of trying to make sense > > out of broken input in fragile ways. > > > > I read the documentation for xml.etree.ElementTree and see > > that it may > > > take an optional parser parameter, but I don't know what > > this parser > > > should be - to ignore the invalid characters. > > > > Could you suggest a way to call ElementTree, so it won't > > bomb on these > > > invalid characters ? > > > No. The parser in lxml.etree has a 'recover' option that lets > > it try to recover from input errors, but in general, XML > > parsers are required to reject non well-formed input. > > > Stefan > > Hi Stefan, > The XML file seems to be valid XML (all XML viewers I tried were able to read > it). > You can verify this by trying to read the XML example I attached to the > original message (attached again here). > Actually, when trying to view the file with an XML viewer, these offensive > characters are not shown. > It's just that some of the fields include characters that the parser used by > ElementTree seems to chock on. > Bye, > Ron. > > tmp_small.xml > < 1KViewDownload Have a look at your file with e.g. a hex editor or just Python repr() -- see below. You will see that there are four cases of good_data\x00garbage where "garbage" is repeated \x00 or just random line noise or uninitialised memory. "MainStorage_snap\x00\x00*SNIP*\x00\x00" "BROLB21\x00\xee"\x00\x00\x00\x90,\x02G\xdc\xfb\x04P\xdc \xfb\x04\x01a\xfc>(\xe8\xfb\x04" It's a toss-up whether the > in there is accidental or a deliberate attempt to sanitise the garbage !-) "Alstom\x00\x00o\x00m\x00\x00*SNIP*\x00\x00" "V5R1.28.1 [R - LA]\x00\x00*SNIP*\x00\x00" The garbage in the 2nd case is such as to make the initial declaration encoding="UTF-8" an outright lie and I'm curious as to how the XML parser managed to get as far as it did -- it must decode a line at a time. As already advised: it's much better to reject that rubbish outright than to attempt to repair it. Repair should be contemplated only if it's a one-off exercise AND you can't get a fixed copy from the source. And while we're on the subject of rubbish: """The XML file seems to be valid XML (all XML viewers I tried were able to read it).""" The conclusion from that is that all XML viewers that you tried are rubbish. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sharing a program I wrote
On 4 May, 22:40, Scott wrote: > I'm looking for suggestions on what to do (and how to do it) if I want > to share a program that I wrote in Python. There seem to be quite a > few places to post code and I don't know how to choose. Perhaps look at the options and then select whichever suits your needs best. > I wrote a program (script?) that takes a text file containing the > output of the "show access-list" command on a Cisco PIX/ASA/FWSM > firewall and any number of text files containing syslog output from > the same firewall and creates a report showing which access-list rules > allowed which actual connections. It is written in Python 2.6 and runs > on Windows. > > Since this is obviously something mankind has long been waiting for I > am thinking about sharing it - but since I am new to Python and > programming in general I am not at all familiar with dealing with > source code. > > I'm sure that improvements and additions could be made if it was > reviewed by "actual programmers" but I wouldn't exactly call it a > "project" either. Of course I'd love to add a gui interface... An option if you want reviews - and your code is short - is to post it here to comp.lang.python. > I've seen pypi. It seems to index code that is posted on all sorts of > sites - including pypi itself? And what is a "package" anyway? I've > seen sourceforge. It looks like a good home for big applications or > multi-developer projects. Freshmeat? Google code? My own website? Your > blog? Yes, many of these are well suited for significant projects. I set up http://codewiki.wikispaces.com/ Its focus is not on code size per se but on promoting sharing and updates. However, *smaller* pieces of code are preferred, i.e. code that can be viewed online and learned from. For example, see the Python programs at http://codewiki.wikispaces.com/index_by_language#x-Portable%20Python > > Another detail is that my program uses a library that was written by > someone else. It is the most excellent netaddr written by David P. D. > Moss and it lives at code.google.com. It uses the New BSD License. > Since this library is required would I simply provide a link to it? > Would I post the actual library? Do I have to post a copy of his > copyright info anywhere? Please don't tell me I have to write some > kind of installer that takes care of providing that. Unless you are worried that the original copy of the library may be deleted or lost it should be enough to post a link. Then the web site that contains the code would be responsible for explaining its licence and will include other relevant documentation. For my own code on codewiki I include installation instructions as text, where necessary. > I really just want anyone who might need a little networking/security > tool like this to be able to find it. Any advice? That was similar to my motive. Interestingly the highest number of page hits on the site is for a networking utility. James -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 04May2010 14:48, Baz Walter wrote: | On 04/05/10 09:08, Gregory Ewing wrote: | >Grant Edwards wrote: | >>except that Python objects can form a generalized graph, and Unix | >>filesystems are constrained to be a tree. | > | >Actually I believe that root is allowed to create arbitrary | >hard links to directories in Unix, so it's possible to turn | >the file system in to a general graph. It's highly | >unrecommended, though, because it confuses the heck out of | >programs that recursively traverse directories (which is | >why only root is allowed to do it). | | i think there are versions of mac osx that use hard-linked | directories in their backup systems. I believe TimeMachine exploits that. I think one can make loops with recursive mounts. -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ You can't go around hoping that most people have sterling moral characters. The most you can hope for is that people will pretend that they do. - Fran Lebowitz -- http://mail.python.org/mailman/listinfo/python-list
Re: Exclusively lock a file to prevent other processes from reading it?
On May 4, 2010, at 5:37 PM, pyt...@bdurham.com wrote: > Is there a way to exclusively lock a file to prevent other processes > from reading it while we have it open? > My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking > for a cross-platform solution if that's possible. Some Unix systems support mandatory locking; Linux used to, but the option appears to have been removed (and was seldom enabled). On Unix systems which support mandatory locking, it is enabled for a particular file by setting the setgid bit on a file which lacks group-execute permission. This causes fcntl() locks to be mandatory, i.e. they cause read()/write() operations to block rather than merely preventing conflicting locks from being obtained. But in general, you can't rely upon mandatory locking being available. -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On Tue, 04 May 2010 14:36:06 +0100, Baz Walter wrote: > this will work so long as the file is in a part of the filesystem that can > be traversed from the current directory to the root. what i'm not sure > about is whether it's possible to cross filesystem boundaries using this > kind of technique. At least on Linux, the kernel "fixes" the links at mount points, i.e. within the root directory of a mounted filesystem, ".." refers to the directory containing the mount point on the parent filesystem, while the mount point refers to the root directory of the mounted filesystem. This also appears to work correctly for bind mounts (mounting an arbitrary directory to another directory, which results in a directory hierarchy appearing at multiple locations within the filesystem), i.e. ".." refers to the appropriate directory for each "instance". OTOH, the algorithm can fail if a directory is moved (whether by rename() or remounting) between the stat("..") and the listdir(). -- http://mail.python.org/mailman/listinfo/python-list
Re: Sharing a program I wrote
--- On Tue, 5/4/10, Scott wrote: > From: Scott > Subject: Sharing a program I wrote > To: python-list@python.org > Date: Tuesday, May 4, 2010, 5:40 PM > I'm looking for suggestions on what > to do (and how to do it) if I want > to share a program that I wrote in Python. There seem to be > quite a > few places to post code and I don't know how to choose. > > I wrote a program (script?) that takes a text file > containing the > output of the "show access-list" command on a Cisco > PIX/ASA/FWSM > firewall and any number of text files containing syslog > output from > the same firewall and creates a report showing which > access-list rules > allowed which actual connections. It is written in Python > 2.6 and runs > on Windows. > > Since this is obviously something mankind has long been > waiting for I > am thinking about sharing it - but since I am new to Python > and > programming in general I am not at all familiar with > dealing with > source code. > > I'm sure that improvements and additions could be made if > it was > reviewed by "actual programmers" but I wouldn't exactly > call it a > "project" either. Of course I'd love to add a gui > interface... > > I've seen pypi. It seems to index code that is posted on > all sorts of > sites - including pypi itself? And what is a "package" > anyway? I've > seen sourceforge. It looks like a good home for big > applications or > multi-developer projects. Freshmeat? Google code? My own > website? Your > blog? > > Another detail is that my program uses a library that was > written by > someone else. It is the most excellent netaddr written by > David P. D. > Moss and it lives at code.google.com. It uses the New BSD > License. > Since this library is required would I simply provide a > link to it? > Would I post the actual library? Do I have to post a copy > of his > copyright info anywhere? Please don't tell me I have to > write some > kind of installer that takes care of providing that. > > I really just want anyone who might need a little > networking/security > tool like this to be able to find it. Any advice? > > Thanks, > Scott > -- > http://mail.python.org/mailman/listinfo/python-list > It depends on exactly what you want to do. I'd suggest you look at the following sites: http://developer.berlios.de/ http://codepad.org/ http://pastebin.com/ http://ideone.com/ One of them might be what your looking for. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Exclusively lock a file to prevent other processes from reading it?
pyt...@bdurham.com wrote: Is there a way to exclusively lock a file to prevent other processes from reading it while we have it open? I need to cache some overflow data to disk in a temp file and I want to make sure no other processes can read the contents of this file while I'm using it. I tried the following using an 'append binary' mode, but I can use NotePad to read the file while I'm using it: fd = open( r'a-temp-file.dat', 'ab' ) My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking for a cross-platform solution if that's possible. I know of now mandatory file lock solution that works cross platform. On Unix file locks (flock(2)) are advisory locks. All applications must check the file lock and act accordantly. Your best choice is other a temporary file or a temporary directory owned by your application. Python's tempfile module contains several solutions to securely work with temporary files and directories. Please don't try to come up with your own solution. Your app can easily become vulnerable by symlink attacks. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Django as exemplary design
Thanks to everyone for their comments. On 2010-05-04 07:11:08 -0700, alex23 said: TomF wrote: I'm interested in improving my python design by studying a large, well-designed codebase. Someone (not a python programmer) suggested Django. I realize that Django is popular, but can someone comment on whether its code is well-designed and worth studying? Here's a viewpoint that says no: http://mockit.blogspot.com/2010/04/mess-djangos-in.html There's a lot of good counterpoint in the comments too. I read most of the discussion. Yep, there is a LOT of disagreement about the code quality. I guess I'll dig in and see whether I can learn anything. (I also think there's value to be gained in studying _bad_ code, too...) True, although whether that's time well spent is another question. Regards, -Tom -- http://mail.python.org/mailman/listinfo/python-list
Re: ooolib, reading writing a spread sheet and keep formatting
News123 wrote: Hi Chris, Chris Withers wrote: News123 wrote: from xlrd import open_workbook from xlutils.copy import copy rb = open_workbook('doc1.xls') open_workbook('doc1.xls',formatting_info=True) I'll try, but the doc mentioned explicitely, that formulas will be lost. I'll keep you updated. Yup, for them to be preserved, someone needs to either provide the patches or stump up the cash to get the features implemented. cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: ooolib, reading writing a spread sheet and keep formatting
Hi Chris, Chris Withers wrote: > News123 wrote: >> >> from xlrd import open_workbook >> from xlutils.copy import copy >> >> rb = open_workbook('doc1.xls') > > open_workbook('doc1.xls',formatting_info=True) I'll try, but the doc mentioned explicitely, that formulas will be lost. I'll keep you updated. > >> print "WB with %d sheets" % rb.nsheets >> wb = copy(rb) >> wb.save("doc2.xls") # file is created, but ALL formattng is lost and >> formulas are now diplayed as text > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Exclusively lock a file to prevent other processes from reading it?
On Tue, May 4, 2010 at 2:37 PM, wrote: > Is there a way to exclusively lock a file to prevent other processes from > reading it while we have it open? > > I need to cache some overflow data to disk in a temp file and I want to make > sure no other processes can read the contents of this file while I'm using > it. > > I tried the following using an 'append binary' mode, but I can use NotePad > to read the file while I'm using it: > > fd = open( r'a-temp-file.dat', 'ab' ) > > My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking for a > cross-platform solution if that's possible. I remember this recipe (or a variant) from the dead-tree Python Cookbook: http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Exclusively lock a file to prevent other processes from reading it?
On May 4, 2010, at 5:37 PM, pyt...@bdurham.com wrote: Is there a way to exclusively lock a file to prevent other processes from reading it while we have it open? I need to cache some overflow data to disk in a temp file and I want to make sure no other processes can read the contents of this file while I'm using it. I tried the following using an 'append binary' mode, but I can use NotePad to read the file while I'm using it: fd = open( r'a-temp-file.dat', 'ab' ) My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking for a cross-platform solution if that's possible. Have a look at tempfile.mkstemp() in the standard library. -- http://mail.python.org/mailman/listinfo/python-list
Sharing a program I wrote
I'm looking for suggestions on what to do (and how to do it) if I want to share a program that I wrote in Python. There seem to be quite a few places to post code and I don't know how to choose. I wrote a program (script?) that takes a text file containing the output of the "show access-list" command on a Cisco PIX/ASA/FWSM firewall and any number of text files containing syslog output from the same firewall and creates a report showing which access-list rules allowed which actual connections. It is written in Python 2.6 and runs on Windows. Since this is obviously something mankind has long been waiting for I am thinking about sharing it - but since I am new to Python and programming in general I am not at all familiar with dealing with source code. I'm sure that improvements and additions could be made if it was reviewed by "actual programmers" but I wouldn't exactly call it a "project" either. Of course I'd love to add a gui interface... I've seen pypi. It seems to index code that is posted on all sorts of sites - including pypi itself? And what is a "package" anyway? I've seen sourceforge. It looks like a good home for big applications or multi-developer projects. Freshmeat? Google code? My own website? Your blog? Another detail is that my program uses a library that was written by someone else. It is the most excellent netaddr written by David P. D. Moss and it lives at code.google.com. It uses the New BSD License. Since this library is required would I simply provide a link to it? Would I post the actual library? Do I have to post a copy of his copyright info anywhere? Please don't tell me I have to write some kind of installer that takes care of providing that. I really just want anyone who might need a little networking/security tool like this to be able to find it. Any advice? Thanks, Scott -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating nested code with context managers
Terry, > So here is my updated (3.1) proof-of-concept version. Very clever! An excellent example of how to use the 'with' statement with something besides the traditional file open example. Not the original OP, but thank you anyway. Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Exclusively lock a file to prevent other processes from reading it?
Is there a way to exclusively lock a file to prevent other processes from reading it while we have it open? I need to cache some overflow data to disk in a temp file and I want to make sure no other processes can read the contents of this file while I'm using it. I tried the following using an 'append binary' mode, but I can use NotePad to read the file while I'm using it: fd = open( r'a-temp-file.dat', 'ab' ) My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking for a cross-platform solution if that's possible. Thanks, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: py3 tkinter acceps bytes. why?
> In a recent thread named "py3 tkinter Text accepts what bytes?" > (google groups link: > http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot) > I asked what kinds of bytes are accepted as tkinter parameters. > I still wonder why they are accepted at all. That's basically because the port from 2.x was shallow: it supports the exact same feature set that 2.x supports, with just the type names swapped. Tkinter in 2.x will already use Unicode strings in most situations. However, 2.x code would often also provide byte strings for text properties, and Tcl supports that "nicely" (*) Regards, Martin (*) I think you also asked how Tcl interprets things. IIUC, it first assumes that a byte string is UTF-8 (starting from 8.1 or so); if it's not, it then assumes that it's in the locale's encoding. If that turns out incorrect also, it just tries to pass it as-is to the operating system GUI API. Or something like that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Django as exemplary design
On May 4, 12:37 am, Bruno Desthuilliers wrote: > TomF a écrit : > > > I'm interested in improving my python design by studying a large, > > well-designed codebase. Someone (not a python programmer) suggested > > Django. I realize that Django is popular, but can someone comment on > > whether its code is well-designed and worth studying? > > Carl makes some valid points in his answer, and there are indeed a > couple dark corners in this area - bit it's just a part of the whole > framework. There are still things worth studying in Django IHMO, > specially if you're interested in seeing metaclasses and descriptors at > work. Absolutely. I said it was "less than exemplary", i.e., still pretty good. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Recursive functions not returning lists as expected
On 5/4/2010 4:10 PM, rickhg12hs wrote: On May 4, 1:32 pm, Terry Reedy wrote: On 5/4/2010 1:45 AM, rickhg12hs wrote: [snip] [To bad there's no tail recursion optimization.] 8-( This is prinarily a space optimization to conserve stack frame space. The speedup would be minimal. Using while or for loops has the same space saving with better speedup (slow function are also gone) and easier optimization in other ways, as I showed. That issue is much more complicated than you probably imagine. [snip] No imagination is necessary - functional languages (e.g., Erlang) do it quite well. More complicated in the Python context. Does Erlang use static or dynamic name binding, or early versus late local name resolution? Python, by design, resolves global names within a function each time the function is called. So whether a call is recursive cannot be determined until the call is made. It is not difficult for CPython to space-optimize *all* tail calls, recursive or not. But this would have the bad effect of leaving gaps in error tracebacks and is not done for that reason. To only space-optimize simple recursive tail calls would take more time, and one would still lose traceback information. If you imagined all of the above, then indeed I underestimated you ;-). Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Generating nested code with context managers
In a current thread, people have claimed that generating properly indented nested blocks is a pain because of the need to keep track of indent levels. Someone countered with the now rather ancient http://effbot.org/zone/python-code-generator.htm The usage example c = CodeGeneratorBackend() c.begin(tab="") c.write("for i in range(1000):\n") c.indent() c.write("print 'code generation is trivial'") c.dedent() illustrates three problems with the CodeGeneratorBackend class. 1) it requires explicit \n on all lines (which the second omits, though it is non-fatal since it is also the last) 2) the user still has to manually match indents and dedents, and 3) the user *cannot* indent lines that produce indented code. The relatively new with statement and associated context managers are designed, among other things, for this situation, where one needs to alter and restore a global context. So here is my updated (3.1) proof-of-concept version. class PyCodeGen: def __init__(self, tab=""): self.code = [] self.tab = tab self.level = 0 # all attributes should be treated as read-only def end(self): return '\n'.join(self.code) def line(self, string): # new line self.code.append(self.tab * self.level + string) class For: def __init__(self, target, in_expression): target.line('for ' + in_expression + ':') self.target = target def __enter__(self): self.target.level += 1 def __exit__(self, t, v, tb): self.target.level -= 1 c = PyCodeGen() with For(c, 'i in range(1000)'): c.line('print("Code gen is easy")') c.line('# done') print(c.end()) # prints for i in range(1000): print("Code gen is easy") # done Note that the absence of .indent and .dedent is intentional. In a fleshed out system, there would be a context manager for each compound statement and these would handle all indents and dedents. If one really preferred to write, for instance, 'c.For(s); instead of 'For(c,s)' in the with statement, one could add a wrapper method like def For(self, s): return For(self, s) for each context manager. I left that out. Similar methods can be used to auto-match C braces and other open/close pairs. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Recursive functions not returning lists as expected
On May 4, 1:32 pm, Terry Reedy wrote: > On 5/4/2010 1:45 AM, rickhg12hs wrote: [snip] > > [To bad there's no tail recursion optimization.] 8-( > > That issue is much more complicated than you probably imagine. [snip] No imagination is necessary - functional languages (e.g., Erlang) do it quite well. -- http://mail.python.org/mailman/listinfo/python-list
Re: str.count algorithm
Raymond Hettinger wrote: > On May 4, 12:12 pm, Hellnar wrote: >> Hello, >> I am trying to find what algorithm Python uses for the built-in >> str.count function, if it has a name. > > Roughly the same as: > >sum(1 for c in s if c == tgt) That would be list.count(), I think. OP, the source http://svn.python.org/view/python/trunk/Objects/stringlib/fastsearch.h?revision=77470&view=markup has a reference to http://effbot.org/zone/stringlib.htm Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: str.count algorithm
On May 4, 12:12 pm, Hellnar wrote: > Hello, > I am trying to find what algorithm Python uses for the built-in > str.count function, if it has a name. Roughly the same as: sum(1 for c in s if c == tgt) Raymond -- http://mail.python.org/mailman/listinfo/python-list
str.count algorithm
Hello, I am trying to find what algorithm Python uses for the built-in str.count function, if it has a name. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, Stefan Behnel wrote: > From: Stefan Behnel > Subject: Re: Teaching Programming > To: python-list@python.org > Date: Tuesday, May 4, 2010, 11:52 AM > Ed Keith, 04.05.2010 17:43: > > The PITA is having to keep track of the indentation of > each embedded > > chunk and summing it for each level of indentation. > This requires a fair > > amount of bookkeeping that would not otherwise be > necessary. > > > > The original prototype simply replaced each embedded > chunk with the text > > from the chunk definition, all indenting information > was lost. It worked > > for most languages, but not Python. > > > > In testing the program I used two languages, Python > and J. > > Well, then both of the language generators have benefited > from your effort because the generated complete code is > properly indented and therefore much more readable during > debugging. I'd say it was worth it. > > Stefan > > -- http://mail.python.org/mailman/listinfo/python-list > I agree, but some propellants of literate programming would not. Knuth wanted the generated source to be unreadable, so people would not be tempted to edit the generated code. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Tue, May 4, 2010 at 2:16 PM, superpollo wrote: > superpollo ha scritto: >> >> James Mills ha scritto: >>> >>> On Tue, May 4, 2010 at 9:56 PM, superpollo wrote: of course! *but* if i must generate on-the-fly python code that defines a function i am back again to the problem: >>> >>> One-liner: >>> >>> $ python >>> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49) >>> [GCC 4.4.1 (CRUX)] on linux2 >>> Type "help", "copyright", "credits" or "license" for more information. >> >> a, b = 2, 3 >> print a + b if a > b else a**b - b**2 >>> >>> -1 >> >> a, b = 3, 2 >> print a + b if a > b else a**b - b**2 >>> >>> 5 >>> >>> --James >> >> what if i want an elif clause? >> > > my first try obviously falied: > print a + b if a > b elif a=b "WOW" else a**b - b**2 > File "", line 1 > print a + b if a > b elif a=b "WOW" else a**b - b**2 > ^ > SyntaxError: invalid syntax That's because this technically isn't an if statement. It's the ternary conditional expression. If you want multiple conditions, you have to repeat the expression print a+b if a > b else "WOW" if a==b else a**b - b**2 > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
superpollo ha scritto: superpollo ha scritto: James Mills ha scritto: On Tue, May 4, 2010 at 9:56 PM, superpollo wrote: of course! *but* if i must generate on-the-fly python code that defines a function i am back again to the problem: One-liner: $ python Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49) [GCC 4.4.1 (CRUX)] on linux2 Type "help", "copyright", "credits" or "license" for more information. a, b = 2, 3 print a + b if a > b else a**b - b**2 -1 a, b = 3, 2 print a + b if a > b else a**b - b**2 5 --James what if i want an elif clause? my first try obviously falied: >>> print a + b if a > b elif a=b "WOW" else a**b - b**2 File "", line 1 print a + b if a > b elif a=b "WOW" else a**b - b**2 ^ SyntaxError: invalid syntax ok i got it: >>> a,b=2,3 >>> print a + b if a > b else "WOW" if a < b else a**b - b**2 WOW >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On 5/4/2010 1:44 PM, Dennis Lee Bieber wrote: On Tue, 04 May 2010 12:06:10 -0400, Terry Reedy declaimed the following in gmane.comp.python.general: Speak for yourself, please. For two decades before I met Python, I indented code nicely whenever it was allowed. That option was one of the great advancements of Fortran77 over FortranIV. Coming from C, I was immediately glad to be done with those darn braces. What kept you from indenting FORTRAN-IV? There was a several year gap with learning of 'structured programming' in between. Perhaps ignorance of both the possibility (no one did it, that I know of) and desirability. Glass screens made consistent indentation a lot easier that with cards and teletypes. The new structured block constructs made indentation more useful, too. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
superpollo ha scritto: James Mills ha scritto: On Tue, May 4, 2010 at 9:56 PM, superpollo wrote: of course! *but* if i must generate on-the-fly python code that defines a function i am back again to the problem: One-liner: $ python Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49) [GCC 4.4.1 (CRUX)] on linux2 Type "help", "copyright", "credits" or "license" for more information. a, b = 2, 3 print a + b if a > b else a**b - b**2 -1 a, b = 3, 2 print a + b if a > b else a**b - b**2 5 --James what if i want an elif clause? my first try obviously falied: >>> print a + b if a > b elif a=b "WOW" else a**b - b**2 File "", line 1 print a + b if a > b elif a=b "WOW" else a**b - b**2 ^ SyntaxError: invalid syntax -- http://mail.python.org/mailman/listinfo/python-list
Re: py3 tkinter acceps bytes. why?
On 5/4/2010 10:17 AM, Matthias Kievernagel wrote: From: Matthias Kievernagel Subject: py3 tkinter acceps bytes. why? Newsgroups: comp.lang.python Summary: Keywords: In a recent thread named "py3 tkinter Text accepts what bytes?" (google groups link: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot) I asked what kinds of bytes are accepted as tkinter parameters. I still wonder why they are accepted at all. Does anyone know a reason for this or has a link to some relevant discussion? I do not remember any particular public discussion of tkinter on the dev lists. I suspect the reason is a) avoid breaking old code and b) tkinter (tk inter-face) passes params on to tk which expects byte strings. I would not be surprised if tkinter has to encode py3 (unicode) strings before passing them on. The Py3 changes were greatly complicated by the need to interface with the older bytes world. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
James Mills ha scritto: On Tue, May 4, 2010 at 9:56 PM, superpollo wrote: of course! *but* if i must generate on-the-fly python code that defines a function i am back again to the problem: One-liner: $ python Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49) [GCC 4.4.1 (CRUX)] on linux2 Type "help", "copyright", "credits" or "license" for more information. a, b = 2, 3 print a + b if a > b else a**b - b**2 -1 a, b = 3, 2 print a + b if a > b else a**b - b**2 5 --James what if i want an elif clause? -- http://mail.python.org/mailman/listinfo/python-list
Re: scrolledtext download location
On 5/4/2010 1:32 PM, Robin wrote: Does anyone know where I can download the ScrolledText tkintewr widget, looked all over for it and had no luck, Since this is a module included with tkinter which is included with Python, (at least on Windows) I am puzzled. Perhaps you need to supply more info as to your problem. (3.1) Lib ref 24.4: "tkinter.scrolledtext module provides a class of the same name which implements a basic text widget which has a vertical scroll ..." -- http://mail.python.org/mailman/listinfo/python-list
Re: new extension generator for C++
On 05/04/2010 03:06 AM, Samuel Williams wrote: Dear Rouslan, It looks interesting. I say go for it. You will learn something and might make some improvements on existing ideas. I recommend putting the code on www.github.com Kind regards, Samuel Thanks for the suggestion. I think I'll do just that. The "social" aspect sounds interesting, since up until now, this project has been a solitary exercise. On 05/04/2010 01:51 AM, Stefan Behnel wrote: > Last I heard, that was basically what PyBindGen does (and probably some > other existing binding generators). You should take a look at them > before investing too much time into a duplicated effort. > > Also, if you're interested in performance, you should take a look at > Cython, which is an optimising compiler for (basically) Python code that > can talk to C, C++ and Fortran code. It's been used a lot for writing > performance critical library wrappers. > > Stefan > I took a brief look at PyBindGen. I noticed a couple of things in the generated code of a test module. The PyObject definitions held a pointer to the contained datatype, which is one of the things I was trying to steer away from. It would generate a std::map for each new type, apparently to allow the contained type to be matched back to the PyObject when needed. I intend to use the same method that Boost.Python uses, sneak the pointer into the destruct functor of a shared_ptr. And when parsing arguments from an overloaded set, PyBindGen appears to generate complete separate functions for each overload and then try one after the other. The reason mine doesn't support overloaded named arguments is because it instead uses a bunch of nested if-statements to match the overload with as few checks as possible. Mine also orders the checks so that a base-class is never checked before a derived-class and that numeric types and string types are matched to their best fit (given func(double x) and func(int x), a python float value will always call func(double)). I'm not certain that PyBindGen doesn't generate code that takes this into consideration I haven't studied its code. I'll definitely take a look at how Cython handles arguments some time. It's also interesting to note that while PyBindGen will parse C++ types itself, to interpret arguments and return types, mine uses gccxml to do all the work. When given a specific overload, it will generate a dummy function with the same set of arguments in the file given to gccxml, along with typedefs for all built-in types. That way, you can specify types either explicitly, or with typedefs regardless of how the interface you want to expose does it, and my code doesn't have to do any extra work. The only question I have now is what about licensing? Is that something I need to worry about? Should I go with LGPL, MIT, or something else? And should the license be mentioned in the code or be in a single separate file at the top-level directory? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?
On 5/4/2010 11:37 AM, Stefan Behnel wrote: Barak, Ron, 04.05.2010 16:11: The XML file seems to be valid XML (all XML viewers I tried were able to read it). From Internet Explorer: The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. An invalid character was found in text content. Error processing resource 'file:///C:/Documents and Settings... "BROLB21 This is what xmllint gives me: --- $ xmllint /home/sbehnel/tmp.xml tmp.xml:6: parser error : Char 0x0 out of allowed range "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag m_sanApiName1 line 6 "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag DbHbaGroup line 5 "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag database line 4 "MainStorage_snap ^ --- The file contains 0-bytes - clearly not XML. IE agrees. -- http://mail.python.org/mailman/listinfo/python-list
Re: long int computations
Mensanator wrote: > You could try using the gmpy module. It supports arbitrary precision > floats, so converting long to float is no problem. I fear I may actually have to go symbolic. I'm now having to use the 12th root of 2, and I would like the twelfth power of that to be exactly 2. Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu -- http://mail.python.org/mailman/listinfo/python-list
scrolledtext download location
Does anyone know where I can download the ScrolledText tkintewr widget, looked all over for it and had no luck, Thanks, -Robin -- http://mail.python.org/mailman/listinfo/python-list
Re: Recursive functions not returning lists as expected
On 5/4/2010 1:45 AM, rickhg12hs wrote: On May 4, 1:34 am, Cameron Simpson wrote: On 03May2010 22:02, rickhg12hs wrote: | Would a kind soul explain something basic to a python noob? | | Why doesn't this function always return a list? | | def recur_trace(x,y): | print x,y | if not x: | return y | recur_trace(x[1:], y + [x[0]]) You need: return recur_trace(x[1:], y + [x[0]]) Otherwise the function returns None. Ah, an explicit "return" is required. Thanks! [To bad there's no tail recursion optimization.] 8-( That issue is much more complicated than you probably imagine. Here is part of it, using your toy example of computing y +x. [Code snippets below are untested and might have typos.] Write your tail recursive function with the recursive branch first: def t_r(x,y): if x: return t_r(x[1:], y+[x[0]]) else: return y and the conversion ('optimization') to while form is trivial: def t_w(x,y): while x: x,y = x[1:], y+[x[0]] else: return y Of course, most would omit the 'else:', which I left to emphasize the paralellism. More importantly, 'while' loops are relatively rare in Python because scanning an iterable (a typical use of tail recursion) can be and usually is done (generically) with for loops instead: def t_f(x,y): for o in x: y = y + [o] return y Of course, this, like your original code, wastefully creates numerous temporary lists (two per iteration) when only one new list is needed for the entire process. This is easily fixed in the loop version: def t_f2(x,y): r = y[:] for o in x: r.append(o) return r Making that important fix in the recursive version is harder since the copying should be done exactly once, and not with every recursive call. I leave it to you to try either of the two main approaches. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: long int computations
On May 3, 10:17 am, s...@sig.for.address (Victor Eijkhout) wrote: > I have two long ints, both too long to convert to float, but their ratio > is something reasonable. How can I compute that? The obvious "(1.*x)/y" > does not work. You could try using the gmpy module. It supports arbitrary precision floats, so converting long to float is no problem. YY = 3**10684 float(YY) Traceback (most recent call last) File "pyshell#78>", line 1, in float(YY) OverflowError: Python int too large to convert to C double import gmpy gmpy.mpf(YY) mpf('3.6600365709...0197681e5097',16936) > > Victor. > > -- > Victor Eijkhout -- eijkhout at tacc utexas edu -- http://mail.python.org/mailman/listinfo/python-list
Re: HTTP server + SQLite?
On 5/4/2010 2:07 AM, Bryan wrote: The SQLite developers state the situation brilliantly at http://www.sqlite.org/whentouse.html: For future reference, that link does not work with Thunderbird. This one does. http://www.sqlite.org/whentouse.html When posting links, best to put them on a line by themselves, with NO punctuation, even if 'proper' English seems to require it. A space before the colon probably would have worked too, if its complete absence bothers one too much ;=). Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
superpollo, 04.05.2010 17:55: since i have some kind of computer literacy (as opposed to most of my colleagues), some years ago i was kindly asked to try and solve a "simple" particular problem, that is to write a program that generates math exercises (q+a) from an example taken from the textbook. for instance, this: %%TITLE:Sample worksheet %% %%SCHEMA:\lim_{x \to } %%SCHEMA:\frac %%SCHEMA:{x^3-x^2-x} %%SCHEMA:{x^3-x^2+x-}\\ %% %%ANS:FRAC %%ANSNUM: %%ANSDEN: %% %%AMIN:1 %%AINC:1 %%AMAX:2 %%BMIN:3 %%BINC:1 %%BMAX:4 %%CMIN:2 %%CINC:1 %%CMAX:3 should generate this latex source document: \documentclass[a4paper,10pt,twocolumn,fleqn]{article} \title{Sample worksheet} \pagestyle{empty} \usepackage[italian]{babel} \usepackage{amsmath} \usepackage{amssymb} \usepackage{cancel} \usepackage{mathrsfs} \usepackage[dvips]{graphicx} \usepackage{eurosym} \usepackage{pstricks} \usepackage{pst-eucl} \usepackage{pst-poly} \usepackage{pst-plot} \frenchspacing \begin{document} \section*{\center{\framebox{Sample worksheet}}} \noindent \begin{enumerate} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+3x^2-4x} {x^3-x^2+2x-2}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+x^2-6x} {x^3-2x^2+2x-4}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+2x^2-8x} {x^3-2x^2+2x-4}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+2x^2-3x} {x^3-x^2+2x-2}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+2x^2-3x} {x^3-x^2+3x-3}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+3x^2-4x} {x^3-x^2+3x-3}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+x^2-6x} {x^3-2x^2+3x-6}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+2x^2-8x} {x^3-2x^2+3x-6}\\ \end{multline*} \end{enumerate} \subsection*{\center{Answers}} \begin{enumerate} \item \begin{displaymath} \frac{5}{3} \end{displaymath} \item \begin{displaymath} \frac{5}{3} > [...] I'm not exactly sure I understand the mapping between the two formats, but it seems to me that you'll need a proper math expression parser (with a strong emphasis on *parser*) for this. Math expressions are not exactly trivial (recursion, prefix/infix/postfix notations, functions), which is why 'real' programmers don't write parsers for them but download tested, working code for that from somewhere. another thing is that for some features i intended to include i found convenient to use python (since it's the language i feel i am more at ease with), so i had to cope with template lines like this: %%SCHEMA: You should assure that the math module is always imported, and otherwise restrict the expressiveness to expressions (terms that result in a value), not arbitrary statements (executable commands that do things without returning anything). to make a long story short: the whole program is now 4775 lines of bash code, Argh! *but* it was and it is a wonderful experience to learn to program It does sound like you almost completely skipped over two very important programming lessons, though: reading code is harder than writing it (i.e. maintenance cost matters), and the best code is the code that you don't need to write (and debug and maintain). Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Terry Reedy wrote: On 5/3/2010 7:46 PM, cjw wrote: Nobody likes indentation at first, Speak for yourself, please. For two decades before I met Python, I indented code nicely whenever it was allowed. That option was one of the great advancements of Fortran77 over FortranIV. Coming from C, I was immediately glad to be done with those darn braces. tjr Right. Somewhere in the 80's I read a paper in sigplan which demonstrated that indentation-only was sufficient to communicate the structure of a program.I immediately *knew* that was the right way to go. Sadly, I had to wait a decade or so before discovering Python in the mid 90's, but I never forgot that paper nor lost my eager anticipation waiting for language design to catch up with that idea. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ethan Furman wrote: Andre Engels wrote: On Tue, May 4, 2010 at 4:35 PM, James Mills wrote: On Wed, May 5, 2010 at 12:21 AM, Ed Keith wrote: To deal with indentation I had to 1) keep track of indentation of all chunks of code embedded in the document and indent inserted chunks to the sum of all the indentation of the enclosing chunks. In my experience of non-indentation sensitive languages such as C-class (curly braces) it's just as hard to keep track of opening and closing braces. Although I have little or no experience with this, I still dare to say that I don't agree. The difference is that in C you do not _need_ to know where in the braces-defined hierarchy you are. You just embed or change a piece of code at the right location. In Python however you _do_ need to know how far your code is to be indented. And how do you know where the right location is? You have to figure it out from the different (nested) braces at the 'right' location. For me, at least, it's much easier to get that information from the already indented Python code, as opposed to indenting (and double-checking the indents) on the braces language. ~Ethan~ Much more important to be able to read code reliably than write it quickly. When I was heavily doing C++ I found myself wishing that incorrect indentation would be an error or at least a warning. And I hadn't ever heard of Python. When I did, I said "About time!" Of course, I was surrounded by many programmers who ignored any warning that the compiler produced, while I cranked up warnings to max, with pedantic. On one project I generated C++ code, about 20k lines. And it was all indented and commented. Most of that code went through the code generator twice. The "source" was a header file from outside my organization. The generator used that to create a new header, more detailed. Then the generator used that to create source & headers that were actually used by the project. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On 5/4/2010 8:46 AM, superpollo wrote: but i do not think i can use it myself, since my template system wants the input to generate the code to stay on a single line ( don't ask :-( ) I think we can agree that Python (unlike C, for instance) is not good for writing non-humanly-readable one-unbounded-length-line code. That is contrary to its design goal. But I would think that you would have similar problems with other manditorily multiline languages, like Fortran and Basic. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Tue, May 4, 2010 at 8:52 AM, Stefan Behnel wrote: > Ed Keith, 04.05.2010 17:43: >> The PITA is having to keep track of the indentation of each embedded >> chunk and summing it for each level of indentation. This requires a fair >> amount of bookkeeping that would not otherwise be necessary. >> >> The original prototype simply replaced each embedded chunk with the text >> from the chunk definition, all indenting information was lost. It worked >> for most languages, but not Python. >> >> In testing the program I used two languages, Python and J. > > Well, then both of the language generators have benefited from your effort > because the generated complete code is properly indented and therefore much > more readable during debugging. I'd say it was worth it. Though there are auto-re-indenting programs for curly-brace languages you know. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Tue, May 4, 2010 at 8:49 AM, D'Arcy J.M. Cain wrote: > On Wed, 5 May 2010 00:35:18 +1000 > James Mills wrote: >> In my experience of non-indentation sensitive languages >> such as C-class (curly braces) it's just as hard to keep track >> of opening and closing braces. > > Harder. That was the big "Aha!" for me with Python. My first > programming language was Fortran in 1969 so when I saw indentation as > syntax I also recoiled in horror for about 0.5 seconds. The amount of mental scaring that Fortran has caused regarding indentation is astounding. Maybe the PSF should run re-education camps for Fortran programmers... :P > However, I > immediately realized that from now on I could be sure that if it looked > right then it was right. > > for (x = 0; x++; x < 10); > printf("Current number is %d\n", x); > > Or... > > for (x = 0; x++; x < 10); > { > printf("Current number is %d\n", x); > } > > Oops. Looks right but isn't. Try to make that mistake in Python. Technically, that pitfall /could/ be eliminated if curly-braces languages simply always required the curly-braces for bodies and stopped special-casing the null body case, in which case both your examples would be syntax errors. Removing the special-casing of single-line bodies too would remove a further class of errors. However, I've yet to encounter a language that takes such an approach. Quite a pity. Cheers, Chris -- I've thought a lot about this. http://blog.rebertia.com/2010/01/24/of-braces-and-semicolons/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Tue, 4 May 2010 17:00:11 +0200 Andre Engels wrote: > Although I have little or no experience with this, I still dare to say > that I don't agree. The difference is that in C you do not _need_ to > know where in the braces-defined hierarchy you are. You just embed or > change a piece of code at the right location. In Python however you > _do_ need to know how far your code is to be indented. Well, I'm afraid your lack of experience shows. Experienced C coders will tell you that one of the most annoying things is counting braces to make sure that you have the right number in the right places. In fact, they add indentation so that they can use the visual layout to check the brace layout. Braces are the added step. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On 5/3/2010 7:46 PM, cjw wrote: Nobody likes indentation at first, Speak for yourself, please. For two decades before I met Python, I indented code nicely whenever it was allowed. That option was one of the great advancements of Fortran77 over FortranIV. Coming from C, I was immediately glad to be done with those darn braces. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
superpollo ha scritto: Stefan Behnel ha scritto: superpollo, 04.05.2010 14:46: my template system wants the input to generate the code to stay on a single line ( don't ask :-( ) I hope you don't mind if I still ask. What are you generating and for what templating system? ok, since you asked for it, prepare yourself for a bit of a horror story ;-) i will answer in my next post ok here it is. i am not a programmer or it-guy in the first place; my job is as a high school teacher in a field only remotely connected with computers (math and physics). since i have some kind of computer literacy (as opposed to most of my colleagues), some years ago i was kindly asked to try and solve a "simple" particular problem, that is to write a program that generates math exercises (q+a) from an example taken from the textbook. for instance, this: %%TITLE:Sample worksheet %% %%SCHEMA:\lim_{x \to } %%SCHEMA:\frac %%SCHEMA:{x^3-x^2-x} %%SCHEMA:{x^3-x^2+x-}\\ %% %%ANS:FRAC %%ANSNUM: %%ANSDEN: %% %%AMIN:1 %%AINC:1 %%AMAX:2 %%BMIN:3 %%BINC:1 %%BMAX:4 %%CMIN:2 %%CINC:1 %%CMAX:3 should generate this latex source document: \documentclass[a4paper,10pt,twocolumn,fleqn]{article} \title{Sample worksheet} \pagestyle{empty} \usepackage[italian]{babel} \usepackage{amsmath} \usepackage{amssymb} \usepackage{cancel} \usepackage{mathrsfs} \usepackage[dvips]{graphicx} \usepackage{eurosym} \usepackage{pstricks} \usepackage{pst-eucl} \usepackage{pst-poly} \usepackage{pst-plot} \frenchspacing \begin{document} \section*{\center{\framebox{Sample worksheet}}} \noindent \begin{enumerate} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+3x^2-4x} {x^3-x^2+2x-2}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+x^2-6x} {x^3-2x^2+2x-4}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+2x^2-8x} {x^3-2x^2+2x-4}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+2x^2-3x} {x^3-x^2+2x-2}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+2x^2-3x} {x^3-x^2+3x-3}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 1} \frac {x^3+3x^2-4x} {x^3-x^2+3x-3}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+x^2-6x} {x^3-2x^2+3x-6}\\ \end{multline*} \item \begin{multline*} \lim_{x \to 2} \frac {x^3+2x^2-8x} {x^3-2x^2+3x-6}\\ \end{multline*} \end{enumerate} \subsection*{\center{Answers}} \begin{enumerate} \item \begin{displaymath} \frac{5}{3} \end{displaymath} \item \begin{displaymath} \frac{5}{3} \end{displaymath} \item \begin{displaymath} 2 \end{displaymath} \item \begin{displaymath} \frac{4}{3} \end{displaymath} \item \begin{displaymath} 1 \end{displaymath} \item \begin{displaymath} \frac{5}{4} \end{displaymath} \item \begin{displaymath} \frac{10}{7} \end{displaymath} \item \begin{displaymath} \frac{12}{7} \end{displaymath} \end{enumerate} \end{document} which in turn can be used to generate the following pdf file: http://www.datafilehost.com/download-cc88a19e.html fine huh? now, for the horror part. when i began putting down some code i thought that maybe it would take a couple of evenings to put up a working prototype, and i was w-r-o-n-g: 1) as i said i am not a professional 2) i decided to use bash shell as a core language for the parser/generator ( *sigh* ) 3) feautiritis soon crept in if some of you knows a bit about math and especially algebra, you will understand that some of the biggest problems in generating standard math notation in to cope with the various layers of traditions and innuendos: for example i could generate the string "2x" which is fine; but what about "1x" or "-1x" or "0x"? and what about plus signs at the start of an expression (normally omitted)? when does a subexpression start? etc ... there are plenty of icompatible rules and as much as exceptions ... as you can see i had many problems to solve and i am not trained to cope with such issues from a programmatic standpoint. another thing is that for some features i intended to include i found convenient to use python (since it's the language i feel i am more at ease with), so i had to cope with template lines like this: %%SCHEMA: or even worse: %%ANSFRCEMBPYC:"~longmapsto~" , $C**2*(3*$A+3*$B+$C) , "~mathrm{(max~loc.)}">\\ to make a long story short: the whole program is now 4775 lines of bash code, written by an unqualified amateur under time pressure; sometimes i have to hand-modify it to get certain outputs as i expect them to be; this in turn breaks other subsystems. i am ashamed to post the code, and it is a true design nightmare. also it is offtopic (being bash). *but* it was and it is a wonderful experience to learn to program, to acknowledge weaknesses end strongnesses of various approaches, to better understand the structure of math notation, problem solving end teaching techniques. it is still work in progress though, and i hope i will never see the day i have to refurbish it. i hope -- instead -- that the next time
Re: Teaching Programming
Ed Keith, 04.05.2010 17:43: The PITA is having to keep track of the indentation of each embedded chunk and summing it for each level of indentation. This requires a fair amount of bookkeeping that would not otherwise be necessary. The original prototype simply replaced each embedded chunk with the text from the chunk definition, all indenting information was lost. It worked for most languages, but not Python. In testing the program I used two languages, Python and J. Well, then both of the language generators have benefited from your effort because the generated complete code is properly indented and therefore much more readable during debugging. I'd say it was worth it. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ed Keith wrote: > Tabs are always a problem when writing Python. I get > around this problem by setting my text editor to expand > all tabs with spaces when editing Python, but I have had > problems when coworkers have not done this. It's best not to trust others to do the right thing. I do trust Tools/ scripts/reindent.py more though :) > Don't get me wrong, I love working in Python. It is one > of my favorite languages, but it, like all other languages, > is not perfect. I was genuinely interested in the problems you hit as I have some hazy projects in mind that I was planning on trying to handle via code generation. Context managers seem the best place to first try dealing with it. If you're interested I can keep you posted when I finally have some working code. Cheers for the clarification. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Wed, 5 May 2010 00:35:18 +1000 James Mills wrote: > In my experience of non-indentation sensitive languages > such as C-class (curly braces) it's just as hard to keep track > of opening and closing braces. Harder. That was the big "Aha!" for me with Python. My first programming language was Fortran in 1969 so when I saw indentation as syntax I also recoiled in horror for about 0.5 seconds. However, I immediately realized that from now on I could be sure that if it looked right then it was right. for (x = 0; x++; x < 10); printf("Current number is %d\n", x); Or... for (x = 0; x++; x < 10); { printf("Current number is %d\n", x); } Oops. Looks right but isn't. Try to make that mistake in Python. Note untested - I wasn't about to fire up an editor, create a C program, add the includes, compile and run the a.out just to test this. Another way that Python excels. However, I am pretty sure that the above would compile. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, Stefan Behnel wrote: > From: Stefan Behnel > Subject: Re: Teaching Programming > To: python-list@python.org > Date: Tuesday, May 4, 2010, 11:33 AM > Ed Keith, 04.05.2010 15:19: > > --- On Tue, 5/4/10, Stefan Behnel wrote: > >> Ed Keith, 04.05.2010 14:15: > >>> Python is a great language to write in > (although I do > >>> wish it did a better job with closures). But > it is a PITA to > >>> generate code for! > >> > >> Interesting. Could you elaborate a bit? Could you > give a > >> short example of what kind of document text you > translate > >> into what kind of Python code, and what the > problems were > >> that you faced in doing so? > > > > The program is written using itself. If you click on > the link above you > > will see an HTML file that fully describes the > program. > > I did. I find it a bit hard to read due to the block > splitting (basically like 'include' based spaghetti > programming), but so far, the actual code that does the code > merging looks pretty simple and I can't figure out where the > "PITA" bit is on that page. That's why I asked. > > Stefan > > -- http://mail.python.org/mailman/listinfo/python-list > The PITA is having to keep track of the indentation of each embedded chunk and summing it for each level of indentation. This requires a fair amount of bookkeeping that would not otherwise be necessary. The original prototype simply replaced each embedded chunk with the text from the chunk definition, all indenting information was lost. It worked for most languages, but not Python. In testing the program I used two languages, Python and J. I figured if I could make both of them work I would not have any problem with anything else. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?
Barak, Ron, 04.05.2010 16:11: I'm parsing XML files using ElementTree from xml.etree (see code below (and attached xml_parse_example.py)). However, I'm coming across input XML files (attached an example: tmp.xml) which include invalid characters, that produce the following traceback: $ python xml_parse_example.py Traceback (most recent call last): xml.parsers.expat.ExpatError: not well-formed (invalid token): line 6, column 34 I hope you are aware that this means that the input you are parsing is not XML. It's best to reject the file and tell the producers that they are writing broken output files. You should always fix the source, instead of trying to make sense out of broken input in fragile ways. The XML file seems to be valid XML (all XML viewers I tried were able to read it). This is what xmllint gives me: --- $ xmllint /home/sbehnel/tmp.xml tmp.xml:6: parser error : Char 0x0 out of allowed range "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag m_sanApiName1 line 6 "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag DbHbaGroup line 5 "MainStorage_snap ^ tmp.xml:6: parser error : Premature end of data in tag database line 4 "MainStorage_snap ^ --- The file contains 0-bytes - clearly not XML. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ed Keith, 04.05.2010 15:19: --- On Tue, 5/4/10, Stefan Behnel wrote: Ed Keith, 04.05.2010 14:15: Python is a great language to write in (although I do wish it did a better job with closures). But it is a PITA to generate code for! Interesting. Could you elaborate a bit? Could you give a short example of what kind of document text you translate into what kind of Python code, and what the problems were that you faced in doing so? The program is written using itself. If you click on the link above you will see an HTML file that fully describes the program. I did. I find it a bit hard to read due to the block splitting (basically like 'include' based spaghetti programming), but so far, the actual code that does the code merging looks pretty simple and I can't figure out where the "PITA" bit is on that page. That's why I asked. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: HTTP server + SQLite?
On Mon, 3 May 2010 23:07:08 -0700 (PDT), Bryan wrote: >I love SQLite because it solves problems I actually have. For the vast >majority of code I write, "lite" is a good thing, and lite as it is, >SQLite can handle several transactions per second. I give SQLite a >file path and in a split second I have a relational, transactional >database. Great. I did not want to configure a server and I sure did >not want to inflict complexity upon my users. Exactly. I need a safe way to share an SQLite database among a few years, but sharing a drive isn't safe but I also don't need a full-fledged DBMS like MySQL. At this point, as an alternative to a Python-based solution, it seems like Mongoose + Lua + SQLite could do the trick. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Andre Engels wrote: On Tue, May 4, 2010 at 4:35 PM, James Mills wrote: On Wed, May 5, 2010 at 12:21 AM, Ed Keith wrote: To deal with indentation I had to 1) keep track of indentation of all chunks of code embedded in the document and indent inserted chunks to the sum of all the indentation of the enclosing chunks. In my experience of non-indentation sensitive languages such as C-class (curly braces) it's just as hard to keep track of opening and closing braces. Although I have little or no experience with this, I still dare to say that I don't agree. The difference is that in C you do not _need_ to know where in the braces-defined hierarchy you are. You just embed or change a piece of code at the right location. In Python however you _do_ need to know how far your code is to be indented. And how do you know where the right location is? You have to figure it out from the different (nested) braces at the 'right' location. For me, at least, it's much easier to get that information from the already indented Python code, as opposed to indenting (and double-checking the indents) on the braces language. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, Andre Engels wrote: > From: Andre Engels > Subject: Re: Teaching Programming > To: "James Mills" > Cc: "python list" > Date: Tuesday, May 4, 2010, 11:00 AM > On Tue, May 4, 2010 at 4:35 PM, James > Mills > > wrote: > > On Wed, May 5, 2010 at 12:21 AM, Ed Keith > wrote: > >> To deal with indentation I had to > >> > >> 1) keep track of indentation of all chunks of > code embedded in the > >> document and indent inserted chunks to the > sum of all the > >> indentation of the enclosing chunks. > > > > In my experience of non-indentation sensitive > languages > > such as C-class (curly braces) it's just as hard to > keep track > > of opening and closing braces. > > Although I have little or no experience with this, I still > dare to say > that I don't agree. The difference is that in C you do not > _need_ to > know where in the braces-defined hierarchy you are. You > just embed or > change a piece of code at the right location. In Python > however you > _do_ need to know how far your code is to be indented. > > For a programmer, it is harder to keep track of braced. For a code generator, it is harder to keep track of indentation. It is a matter of which you are more interested in catering to. Python is easier to write, C is easier to generate. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, James Mills wrote: > From: James Mills > Subject: Re: Teaching Programming > To: "python list" > Date: Tuesday, May 4, 2010, 10:35 AM > On Wed, May 5, 2010 at 12:21 AM, Ed > Keith > wrote: > > To deal with indentation I had to > > > > 1) keep track of indentation of all chunks of code > embedded in the > > document and indent inserted chunks to the sum > of all the > > indentation of the enclosing chunks. > > In my experience of non-indentation sensitive languages > such as C-class (curly braces) it's just as hard to keep > track > of opening and closing braces. > > --James > -- > http://mail.python.org/mailman/listinfo/python-list > Not in this case, because the user is required to include all text of the program in the chunks, the tools does not generate any text, It only needs to generate white space if the language is white space sensitive. I can see how it could be a problem in other cases. In the interest of completeness, I should mention that I had to take care not to introduce extraneous line breaks for languages that are sensitive to them. It is much easier to generate code for languages that are completely white space agnostic. I actually find the fact that I need to think about where I can, and where I can not, break a line to be a bigger problem when write code, than keeping track of indentation level. And Python is not the only language that has this problem. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Tue, May 4, 2010 at 4:35 PM, James Mills wrote: > On Wed, May 5, 2010 at 12:21 AM, Ed Keith wrote: >> To deal with indentation I had to >> >> 1) keep track of indentation of all chunks of code embedded in the >> document and indent inserted chunks to the sum of all the >> indentation of the enclosing chunks. > > In my experience of non-indentation sensitive languages > such as C-class (curly braces) it's just as hard to keep track > of opening and closing braces. Although I have little or no experience with this, I still dare to say that I don't agree. The difference is that in C you do not _need_ to know where in the braces-defined hierarchy you are. You just embed or change a piece of code at the right location. In Python however you _do_ need to know how far your code is to be indented. -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 2010-05-04, Gregory Ewing wrote: > Grant Edwards wrote: > >> except that Python objects can form a generalized graph, and Unix >> filesystems are constrained to be a tree. > > Actually I believe that root is allowed to create arbitrary > hard links to directories in Unix, I know that used to be the case in SunOS (as I found out the hard way), but I've been told by Solaris experts that it is no longer allowed. I was also under the impression that it wasn't allowed in Linux, but I've never tried it. According the the Linux ln (1) man page: -d, -F, --directory allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) > so it's possible to turn the file system in to a general graph. It's > highly unrecommended, though, because it confuses the heck out of > programs that recursively traverse directories (which is why only > root is allowed to do it). It sure caused trouble when I did it on a SunOS system back in the day (1990 or thereabouts). -- Grant Edwards grant.b.edwardsYow! I feel like I'm at in a Toilet Bowl with a gmail.comthumbtack in my forehead!! -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] strange interaction between open and cwd
On 04/05/10 03:25, Grant Edwards wrote: On 2010-05-04, Charles wrote: I don't see how it's inelegant at all. Perhaps it's counter-intuitive if you don't understand how a Unix filesystem works, but the underlying filesystem model is very simple, regular, and elegant. but probably makes some bit of the OS's job slightly easier and is usually good enough in practice. Pragmatism is a bitch sometimes. :-) I agree that the Unix file system is quite elegant, but can be counter-intuitive for people who are used to the "one file, one name" paradigm. I guess I've been using Unix for too long (almost 30 years). I don't think I was consciously aware of a "one file, one name" paradigm. Is that a characteristic of Dos, Windows or Mac filesystems? [...] In the OP's case, references to the directory have been removed from the file system, but his process still has the current working directory reference to it, so it has not actually been deleted. When he opens "../abc.txt", the OS searches the current directory for ".." and finds the inode for /home/baz/tmp, then searches that directory (/home/baz/tmp) for abc.txt and finds it. Exactly. I probably should have taken the time to explain that as well as you did. One forgets that there are a log of new Unix users who've never been taught how the filesystem works. actually, what i failed to grok is that whatever '..' refers to is all that is needed to find the file 'abc.txt' *even if the current directory has been deleted*. an absolute path just isn't needed. this has really got nothing to do with how unix filesystems work per se or "one file, one name", but more to do with simple reference counting. when i mentioned in my original post how windows handles attempts to delete the cwd differently, i should have put two and two together and figured this all out for myself. but i don't think it's immediately obvious what the consequences of (re)moving the cwd are, even if you've been using unix filesystems for a while. in know for a fact that i have used several linux programs in the past that didn't handle this possiblity gracefully, so it's not just new users that can be caught out by this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
On Wed, May 5, 2010 at 12:21 AM, Ed Keith wrote: > To deal with indentation I had to > > 1) keep track of indentation of all chunks of code embedded in the > document and indent inserted chunks to the sum of all the > indentation of the enclosing chunks. In my experience of non-indentation sensitive languages such as C-class (curly braces) it's just as hard to keep track of opening and closing braces. --James -- http://mail.python.org/mailman/listinfo/python-list
RE: Django as exemplary design
> From: alex23 > (I also think there's value to be gained in studying _bad_ code, > too...) Oh, very true. And not just true for python. But, only if an 'expoert' points out why it is bad and provides an alternative. And saying things like, "it isn't pyhonic" or that such and such is a more "pythonic way" is NOT helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, alex23 wrote: > From: alex23 > Subject: Re: Teaching Programming > To: python-list@python.org > Date: Tuesday, May 4, 2010, 10:06 AM > Ed Keith > wrote: > > For more information on Literate Programming in > general see the following links. > > None of which address the question of what you found > problematic about > generating Python code. Was it issues with indentation? > > > -- > http://mail.python.org/mailman/listinfo/python-list > Yes, the problem was indentation. To deal with indentation I had to 1) keep track of indentation of all chunks of code embedded in the document and indent inserted chunks to the sum of all the indentation of the enclosing chunks. and 2) expand all tabls to spaces and allow the user to set the tab expansion size from the command line. Fortunately Python provides good support for this. Neither of these problems exist in languages that are not indention sensitive. Tabs are always a problem when writing Python. I get around this problem by setting my text editor to expand all tabs with spaces when editing Python, but I have had problems when coworkers have not done this. Don't get me wrong, I love working in Python. It is one of my favorite languages, but it, like all other languages, is not perfect. -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
py3 tkinter acceps bytes. why?
From: Matthias Kievernagel Subject: py3 tkinter acceps bytes. why? Newsgroups: comp.lang.python Summary: Keywords: In a recent thread named "py3 tkinter Text accepts what bytes?" (google groups link: http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot) I asked what kinds of bytes are accepted as tkinter parameters. I still wonder why they are accepted at all. Does anyone know a reason for this or has a link to some relevant discussion? Thanks for any hint, Matthias Kievernagel -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools: problem with nested groupby, list()
Nico Schlömer wrote: > Hi, > > I ran into a bit of an unexpected issue here with itertools, and I > need to say that I discovered itertools only recently, so maybe my way > of approaching the problem is "not what I want to do". > > Anyway, the problem is the following: > I have a list of dictionaries, something like > > [ { "a": 1, "b": 1, "c": 3 }, > { "a": 1, "b": 1, "c": 4 }, > ... > ] > > and I'd like to iterate through all items with, e.g., "a":1. What I do > is sort and then groupby, > > my_list.sort( key=operator.itemgetter('a') ) > my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') ) > > and then just very simply iterate over my_list_grouped, > > for my_item in my_list_grouped: > # do something with my_item[0], my_item[1] > > Now, inside this loop I'd like to again iterate over all items with > the same 'b'-value -- no problem, just do the above inside the loop: > > for my_item in my_list_grouped: > # group by keyword "b" > my_list2 = list( my_item[1] ) > my_list2.sort( key=operator.itemgetter('b') ) > my_list_grouped = itertools.groupby( my_list2, > operator.itemgetter('b') ) > for e in my_list_grouped: > # do something with e[0], e[1] > > That seems to work all right. > > Now, the problem occurs when this all is wrapped into an outer loop, such > as > > for k in [ 'first pass', 'second pass' ]: > for my_item in my_list_grouped: > # bla, the above > > To be able to iterate more than once through my_list_grouped, I have > to convert it into a list first, so outside all loops, I go like > > my_list.sort( key=operator.itemgetter('a') ) > my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') ) > my_list_grouped = list( my_list_grouped ) > > This, however, makes it impossible to do the inner sort and > groupby-operation; you just get the very first element, and that's it. > > An example file is attached. > > Hints, anyone? If you want a reusable copy of a groupby(...) it is not enough to convert it to a list as a whole: >>> from itertools import groupby >>> from operator import itemgetter >>> items = [(1,1), (1,2), (1,3), (2,1), (2,2)] >>> grouped_items = list(groupby(items, key=itemgetter(0))) # WRONG >>> for run in 1, 2: ... print "run", run ... for k, g in grouped_items: ... print k, list(g) ... run 1 1 [] 2 [(2, 2)] run 2 1 [] 2 [] Instead, you have to process the groups, too: >>> grouped_items = [(k, list(g)) for k, g in groupby(items, key=itemgetter(0))] >>> for run in 1, 2: ... print "run", run ... for k, g in grouped_items: ... print k, list(g) ... run 1 1 [(1, 1), (1, 2), (1, 3)] 2 [(2, 1), (2, 2)] run 2 1 [(1, 1), (1, 2), (1, 3)] 2 [(2, 1), (2, 2)] But usually you don't bother and just run groupby() twice: >>> for run in 1, 2: ... print "run", run ... for k, g in groupby(items, key=itemgetter(0)): ... print k, list(g) ... run 1 1 [(1, 1), (1, 2), (1, 3)] 2 [(2, 1), (2, 2)] run 2 1 [(1, 1), (1, 2), (1, 3)] 2 [(2, 1), (2, 2)] The only caveat then is that list(items) == list(items) must hold. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Django as exemplary design
TomF wrote: > I'm interested in improving my python design by studying a large, > well-designed codebase. Someone (not a python programmer) suggested > Django. I realize that Django is popular, but can someone comment on > whether its code is well-designed and worth studying? Here's a viewpoint that says no: http://mockit.blogspot.com/2010/04/mess-djangos-in.html There's a lot of good counterpoint in the comments too. (I also think there's value to be gained in studying _bad_ code, too...) -- http://mail.python.org/mailman/listinfo/python-list
RE: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?
> -Original Message- > From: Stefan Behnel [mailto:stefan...@behnel.de] > Sent: Tuesday, May 04, 2010 10:24 AM > To: python-list@python.org > Subject: Re: How to get xml.etree.ElementTree not bomb on > invalid characters in XML file ? > > Barak, Ron, 04.05.2010 09:01: > > I'm parsing XML files using ElementTree from xml.etree (see code > > below (and attached xml_parse_example.py)). > > > > However, I'm coming across input XML files (attached an example: > > tmp.xml) which include invalid characters, that produce the > following > > traceback: > > > > $ python xml_parse_example.py > > Traceback (most recent call last): > > xml.parsers.expat.ExpatError: not well-formed (invalid > token): line 6, > > column 34 > > I hope you are aware that this means that the input you are > parsing is not XML. It's best to reject the file and tell the > producers that they are writing broken output files. You > should always fix the source, instead of trying to make sense > out of broken input in fragile ways. > > > > I read the documentation for xml.etree.ElementTree and see > that it may > > take an optional parser parameter, but I don't know what > this parser > > should be - to ignore the invalid characters. > > > > Could you suggest a way to call ElementTree, so it won't > bomb on these > > invalid characters ? > > No. The parser in lxml.etree has a 'recover' option that lets > it try to recover from input errors, but in general, XML > parsers are required to reject non well-formed input. > > Stefan > > > Hi Stefan, The XML file seems to be valid XML (all XML viewers I tried were able to read it). You can verify this by trying to read the XML example I attached to the original message (attached again here). Actually, when trying to view the file with an XML viewer, these offensive characters are not shown. It's just that some of the fields include characters that the parser used by ElementTree seems to chock on. Bye, Ron. tmp_small.xml Description: tmp_small.xml -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ed Keith wrote: > For more information on Literate Programming in general see the following > links. None of which address the question of what you found problematic about generating Python code. Was it issues with indentation? -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On Mon, 03 May 2010 06:18:55 -0700, Chris Rebert wrote: >> but how can python determine the >> parent directory of a directory that no longer exists? > > Whether or not /home/baz/tmp/xxx/ exists, we know from the very structure > and properties of directory paths that its parent directory is, *by > definition*, /home/baz/tmp/ (just chop off everything after the > second-to-last slash). Not necessarily. There are two common interpretations for the concept of a "parent directory". One is the directory referenced by removing the last component from the (normalised) path, the other is the directory referenced by appending ".." to the path. The two don't necessarily refer to the same directory if the last component is a symlink (or an additional hard link, on platforms which allow creating additional hard links to directories). For the current directory[1], or a directory referenced by a descriptor rather than a path, the latter definition has to be used, as there is no path to manipulate. [1] And also for e.g. "./../..". -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 04/05/10 09:08, Gregory Ewing wrote: Grant Edwards wrote: except that Python objects can form a generalized graph, and Unix filesystems are constrained to be a tree. Actually I believe that root is allowed to create arbitrary hard links to directories in Unix, so it's possible to turn the file system in to a general graph. It's highly unrecommended, though, because it confuses the heck out of programs that recursively traverse directories (which is why only root is allowed to do it). i think there are versions of mac osx that use hard-linked directories in their backup systems. -- http://mail.python.org/mailman/listinfo/python-list
Re: design question
On May 4, 3:39 am, Bruno Desthuilliers wrote: > Alf P. Steinbach a écrit : > (snip) > > > Re efficiency it seems to be a complete non-issue, but correctness is > > much more important: is there any way that the config details can be > > (inadvertently) changed while the build is going on? > > +1 Thanks everyone, I'm new at this type of development so it helps to get input from folks who know what they're talking about. I'm just been coding and doing what is right in my own eyes... There is almost no way for the configuration to change during a build, so it looks like I was just worrying about something that doesn't need worry. Now on to the things that do! thanks again, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On Tue, 04 May 2010 20:08:36 +1200, Gregory Ewing wrote: >> except that Python objects can form a generalized graph, and Unix >> filesystems are constrained to be a tree. > > Actually I believe that root is allowed to create arbitrary hard links to > directories in Unix, so it's possible to turn the file system in to a > general graph. It's highly unrecommended, though, because it confuses the > heck out of programs that recursively traverse directories (which is why > only root is allowed to do it). The 1980s are over already ;) I don't know of any modern Unix which allows creating additional hard links to directories (even for root). It creates too many problems, and isn't necessary if you have symlinks. If you want to turn the filesystem into a cyclic graph, you typically need to bypass the kernel's filesystem interface and modify the block device directly (e.g. with debugfs). If fsck finds out, it's likely to fix it. -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 04/05/10 09:23, Gregory Ewing wrote: Grant Edwards wrote: In your example, it's simply not possible to determine the file's absolute path within the filesystem given the relative path you provided. Actually, I think it *is* theoretically possible to find an absolute path for the file in this case. I suspect that what realpath() is doing for a relative path is something like: 1. Use getcwd() to find an absolute path for the current directory. 2. Chop off a trailing pathname component for each ".." on the front of the original path. 3. Tack the filename on the end of what's left. Step 1 fails because the current directory no longer has an absolute pathname -- specifically, it has no name in what used to be its parent directory. What realpath() is failing to realise is that it doesn't actually need to know the full path of the current directory, only of its parent directory, which is still reachable via ".." (if it weren't, the file wouldn't be reachable either, and we wouldn't be having this discussion). A smarter version of realpath() wouldn't try to find the path of the *current* directory, but would follow the ".." links until it got to a directory that it did need to know an absolute path for, and start with that. Unfortunately, there is no C stdlib routine that does the equivalent of getcwd() for an arbitrary directory, so this would require realpath() to duplicate much of getcwd()'s functionality, which is probably why it's done the way it is. actually, this part of the problem can be achieved using pure python. given the basename of a file, all you have to do is use os.stat and os.listdir to recursively climb up the tree and build a dirpath for it. start by doing os.stat(basename) to make sure you have a legal file in the current directory; then use os.stat('..') to get the parent directory inode, and stat each of the items in os.listdir('../..') to find a name matching that inode etc. (note that the possibility of hard-linked directories doesn't really spoil this - for relative paths, we don't care exactly which absolute path is found). this will work so long as the file is in a part of the filesystem that can be traversed from the current directory to the root. what i'm not sure about is whether it's possible to cross filesystem boundaries using this kind of technique. -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] strange interaction between open and cwd
On Tue, 04 May 2010 23:02:29 +1000, Charles wrote: > I am by no means an expert in this area, but what I think happens (and I > may well be wrong) is that the directory is deleted on the file system. > The link from the parent is removed, and the parent's link count is > decremented, as you observed, but the directory itself is still intact > with it's original contents, including the "." and ".." names and > associated inode numbers. Unix does not normally zero out files on > deletion - the file's blocks usually retain their contents, and I would > not expect directories to be a special case. You are correct. System calls don't "delete" inodes (files, directories, etc), they "unlink" them. Deletion occurs when the inode's link count reaches zero and no process holds a reference to the inode (a reference could be a descriptor, or the process' cwd, chroot directory, or an mmap()d file, etc). IOW, reference-counted garbage collection. -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
Baz Walter writes: > On 04/05/10 02:12, Ben Finney wrote: > > Baz Walter writes: > >> yes, of course. i forgot about hard links > > > > Rather, you forgot that *every* entry that references a file is a > > hard link. > > i'm not a frequent poster on this list, but i'm aware of it's > reputation for pointless pedantry ;-) Only pointless if you view this as a conversation entirely for the benefit of you and I. I, on the other hand, am trying to make this useful to whoever may read it now and in the future. > note that i said hard links (plural) - i think a more generous reader > would assume i was referring to additional hard links. The point, though, was that this is normal operation, rather than exceptional. Files have zero or more hard links, and “this file has exactly one hard link” is merely a common case among that spectrum. I'm glad you already knew this, and hope you can appreciate that it's better explicit than implicit. -- \“The difference between religions and cults is determined by | `\ how much real estate is owned.” —Frank Zappa | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Stefan Behnel ha scritto: superpollo, 04.05.2010 14:46: my template system wants the input to generate the code to stay on a single line ( don't ask :-( ) I hope you don't mind if I still ask. What are you generating and for what templating system? ok, since you asked for it, prepare yourself for a bit of a horror story ;-) i will answer in my next post bye -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
--- On Tue, 5/4/10, Stefan Behnel wrote: > From: Stefan Behnel > Subject: Re: Teaching Programming > To: python-list@python.org > Date: Tuesday, May 4, 2010, 8:40 AM > Ed Keith, 04.05.2010 14:15: > > I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is > > a literate programming tool. It generates code from a > document. It can > > generate code in any language the author wants. It > would have been a LOT > > easier to write if it did not generate Python code. > > > > Python is a great language to write in (although I do > wish it did a > > better job with closures). But it is a PITA to > generate code for! > > Interesting. Could you elaborate a bit? Could you give a > short example of what kind of document text you translate > into what kind of Python code, and what the problems were > that you faced in doing so? > > Stefan > > -- http://mail.python.org/mailman/listinfo/python-list > The program is written using itself. If you click on the link above you will see an HTML file that fully describes the program. That HTML is generated from an AcsiiDoc (http://www.methods.co.nz/asciidoc/) document. The same document is used to generate the python code that it describes. The source document, and the generated HTML and Python code are all avalable at BerliOS (http://developer.berlios.de/projects/asciilitprog/). For more information on Literate Programming in general see the following links. http://www.literateprogramming.com/ http://en.wikipedia.org/wiki/Literate_programming http://en.literateprograms.org/LiteratePrograms:Welcome -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
superpollo, 04.05.2010 14:46: my template system wants the input to generate the code to stay on a single line ( don't ask :-( ) I hope you don't mind if I still ask. What are you generating and for what templating system? Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] strange interaction between open and cwd
"Gregory Ewing" wrote in message news:84a1mcffn...@mid.individual.net... > Charles wrote: > >> In the OP's case, references to the directory have been removed >> from the file system, but his process still has the current working >> directory reference to it, so it has not actually been deleted. >> When he opens "../abc.txt", the OS searches the current directory >> for ".." and finds the inode for /home/baz/tmp, > > This doesn't seem to be quite correct. An experiment I just did > reveals that the link count on the parent directory goes down by > one when the current directory is deleted, suggesting that the .. > link has actually been removed... yet it still works! > > I think what must be happening is that the kernel is maintaining > an in-memory reference to the parent directory, and treating ".." > as a special case when looking up a name. > > (This probably shouldn't be too surprising, because ".." is special > in another way as well -- at the root of a mounted file system, it > leads to the parent of the mount point, even though the actual ".." > link on disk just points back to the same directory. Probably it > simplifies the name lookup logic to always treat it specially.) I am by no means an expert in this area, but what I think happens (and I may well be wrong) is that the directory is deleted on the file system. The link from the parent is removed, and the parent's link count is decremented, as you observed, but the directory itself is still intact with it's original contents, including the "." and ".." names and associated inode numbers. Unix does not normally zero out files on deletion - the file's blocks usually retain their contents, and I would not expect directories to be a special case. The blocks from the directory will be re-cycled when the memory reference (the process's current working directory) disappears, but until then the directory and its contents are still accessible via the process's current directory. This is all supposition, and based on distant memories form the mid 1980s, I could very well be wrong. Charles -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 04/05/10 03:19, Grant Edwards wrote: On 2010-05-03, Baz Walter wrote: On 03/05/10 19:12, Grant Edwards wrote: Even though the user provided a legal and openable path? that sounds like an operational definition to me: what's the difference between "legal" and "openable"? Legal as in meets the syntactic requirements for a path (not sure if there really are any requirements other than it being a null-terminated string). Openable meaning that it denotes a path file that exists and for which the caller has read permissions on the file and execute premissions on the directories within the path. openable is not the same as accessible. a file can still openable, even though a user may not have permission to access it. a better definition of "legal path" might be whether any useful information can be gained from a stat() call on it. -- http://mail.python.org/mailman/listinfo/python-list
Re: strange interaction between open and cwd
On 04/05/10 02:12, Ben Finney wrote: Baz Walter writes: On 03/05/10 18:41, Grant Edwards wrote: Firstly, a file may have any number of paths (including 0). yes, of course. i forgot about hard links Rather, you forgot that *every* entry that references a file is a hard link. i'm not a frequent poster on this list, but i'm aware of it's reputation for pointless pedantry ;-) but what the heck, when in rome... note that i said hard links (plural) - i think a more generous reader would assume i was referring to additional hard links. -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Stefan Behnel ha scritto: superpollo, 04.05.2010 13:56: Stefan Behnel ha scritto: The question is: why do you have to generate the above code in the first place? Isn't a function enough that does the above? of course! *but* if i must generate on-the-fly python code that defines a function [...] Well, could you provide a use case where you have to generate Python code, and where normally written code with some kind of parametrisation won't work? i see your point, and mr mills gave me hints as to how to attack some of my problems in such a fashion. The only thing that currently comes to my mind is a template compiler that translates a user provided template into an executable Python program. But that should be pretty trivial to do as well. I just remembered this little thing on Fredrik's effbot site, which should help here: http://effbot.org/zone/python-code-generator.htm very good. i will most certainly give it a try. but i do not think i can use it myself, since my template system wants the input to generate the code to stay on a single line ( don't ask :-( ) bye -- http://mail.python.org/mailman/listinfo/python-list
Re: Teaching Programming
Ed Keith, 04.05.2010 14:15: I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is a literate programming tool. It generates code from a document. It can generate code in any language the author wants. It would have been a LOT easier to write if it did not generate Python code. Python is a great language to write in (although I do wish it did a better job with closures). But it is a PITA to generate code for! Interesting. Could you elaborate a bit? Could you give a short example of what kind of document text you translate into what kind of Python code, and what the problems were that you faced in doing so? Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools: problem with nested groupby, list()
> Are you basically after this, then? > > for a, a_iter in groupby(my_list, itemgetter('a')): >print 'New A', a >for b, b_iter in groupby(a_iter, itemgetter('b')): >b_list = list(b_iter) >for p in ['first', 'second']: >for b_data in b_list: >#whatever... Yes. Moving the 'first', 'second' operation to the innermost loop works all right, and I guess that's what I'll do. > Cos that looks like it could be simplified to (untested) > for (a, b), data_iter in groupby(my_list, itemgetter('a','b')): > data = list(data) # take copy > for pass_ in ['first', 'second']: > # do something with data Potentially yes, but for now I actually need to do something at "print 'New A', a", so I can't just skip this. Anyway, the above suggestion works well for now. Thanks! --Nico On Tue, May 4, 2010 at 1:52 PM, Jon Clements wrote: > On 4 May, 12:36, Nico Schlömer wrote: >> > Does this example help at all? >> >> Thanks, that clarified things a lot! >> >> To make it easier, let's just look at 'a' and 'b': >> >> > my_list.sort( key=itemgetter('a','b','c') ) >> > for a, a_iter in groupby(my_list, itemgetter('a')): >> > print 'New A', a >> > for b, b_iter in groupby(a_iter, itemgetter('b')): >> > print '\t', 'New B', b >> > for b_data in b_iter: >> > print '\t'*3, a, b, b_data >> > print '\t', 'End B', b >> > print 'End A', a >> >> That works well, and I can wrap the outer loop in another loop without >> problems. What's *not* working, though, is having more than one pass >> on the inner loop, as in >> >> === *snip* === >> my_list.sort( key=itemgetter('a','b','c') ) >> for a, a_iter in groupby(my_list, itemgetter('a')): >> print 'New A', a >> for pass in ['first pass', 'second pass']: >> for b, b_iter in groupby(a_iter, itemgetter('b')): >> print '\t', 'New B', b >> for b_data in b_iter: >> print '\t'*3, a, b, b_data >> print '\t', 'End B', b >> print 'End A', a >> === *snap* === >> >> I tried working around this by >> >> === *snip* === >> my_list.sort( key=itemgetter('a','b','c') ) >> for a, a_iter in groupby(my_list, itemgetter('a')): >> print 'New A', a >> inner_list = list( groupby(a_iter, itemgetter('b')) ) >> for pass in ['first pass', 'second pass']: >> for b, b_iter in inner_list: >> print '\t', 'New B', b >> for b_data in b_iter: >> print '\t'*3, a, b, b_data >> print '\t', 'End B', b >> print 'End A', a >> === *snap* === >> >> which don't work either, and I don't understand why. -- I'll look at >> Uli's comments. >> >> Cheers, >> Nico >> >> On Tue, May 4, 2010 at 1:08 PM, Jon Clements wrote: >> > On 4 May, 11:10, Nico Schlömer wrote: >> >> Hi, >> >> >> I ran into a bit of an unexpected issue here with itertools, and I >> >> need to say that I discovered itertools only recently, so maybe my way >> >> of approaching the problem is "not what I want to do". >> >> >> Anyway, the problem is the following: >> >> I have a list of dictionaries, something like >> >> >> [ { "a": 1, "b": 1, "c": 3 }, >> >> { "a": 1, "b": 1, "c": 4 }, >> >> ... >> >> ] >> >> >> and I'd like to iterate through all items with, e.g., "a":1. What I do >> >> is sort and then groupby, >> >> >> my_list.sort( key=operator.itemgetter('a') ) >> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') ) >> >> >> and then just very simply iterate over my_list_grouped, >> >> >> for my_item in my_list_grouped: >> >> # do something with my_item[0], my_item[1] >> >> >> Now, inside this loop I'd like to again iterate over all items with >> >> the same 'b'-value -- no problem, just do the above inside the loop: >> >> >> for my_item in my_list_grouped: >> >> # group by keyword "b" >> >> my_list2 = list( my_item[1] ) >> >> my_list2.sort( key=operator.itemgetter('b') ) >> >> my_list_grouped = itertools.groupby( my_list2, >> >> operator.itemgetter('b') ) >> >> for e in my_list_grouped: >> >> # do something with e[0], e[1] >> >> >> That seems to work all right. >> >> >> Now, the problem occurs when this all is wrapped into an outer loop, such >> >> as >> >> >> for k in [ 'first pass', 'second pass' ]: >> >> for my_item in my_list_grouped: >> >> # bla, the above >> >> >> To be able to iterate more than once through my_list_grouped, I have >> >> to convert it into a list first, so outside all loops, I go like >> >> >> my_list.sort( key=operator.itemgetter('a') ) >> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') ) >> >> my_list_grouped = list( my_list_grouped ) >> >> >> This, however, makes it impo