Re: Do I have to use threads?
aditya shukla wrote: Hello people, I have 5 directories corresponding 5 different urls .I want to download images from those urls and place them in the respective directories.I have to extract the contents and download them simultaneously.I can extract the contents and do then one by one. My questions is for doing it simultaneously do I have to use threads? Please point me in the right direction. Thanks Aditya You've been given some bad advice here. First -- threads are lighter-weight than processes, so threads are probably *more* efficient. However, with only five thread/processes, the difference is probably not noticeable.(If the prejudice against threads comes from concerns over the GIL -- that also is a misplaced concern in this instance. Since you only have network connection, you will receive only one packet at a time, so only one thread will be active at a time. If the extraction process uses a significant enough amount of CPU time so that the extractions are all running at the same time *AND* if you are running on a machine with separate CPU/cores *AND* you would like the extractions to be running truly in parallel on those separate cores, *THEN*, and only then, will processes be more efficient than threads.) Second, running 5 wgets is equivalent to 5 processes not 5 threads. And third -- you don't have to use either threads *or* processes. There is another possibility which is much more light-weight: asynchronous I/O, available through the low level select module, or more usefully via the higher-level asyncore module. (Although the learning curve might trip you up, and some people find the programming model for asyncore hard to fathom, I find it more intuitive in this case than threads/processes.) In fact, the asyncore manual page has a ~20 line class which implements a web page retrieval. You could replace that example's single call to http_client with five calls, one for each of your ULRs. Then when you enter the last line (that is the asyncore.loop() call) the five will be downloading simultaneously. See http://docs.python.org/library/asyncore.html Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: chown'ing by script
Carsten Haese wrote: > What is the underlying problem you're trying to solve with this > approach? To be paid for developing a web site shopping cart without actually having to learn Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: chown'ing by script
On Wed, Jan 6, 2010 at 1:41 AM, Carsten Haese wrote: > Victor Subervi wrote: > > Hi; > > I have a script that is called via the web. This script writes another > > script that is also called by the web, which in turn needs to have > > execution privileges. The problem is that the programmatically created > > file is owned by apache.apache and thus doesn't have execution > > privileges. I've tried os.chown(...) but this throws an OSError. I > > understand that chown'ing programmatically opens a big security hole. > > However, setting the gid to give apache execution privileges isn't any > > better. What do you suggest? > > I suggest you find a way to achieve whatever it is you are trying to > achieve without having to execute programmatically created scripts. What > is the underlying problem you're trying to solve with this approach? > As I was writing to respond, I re-thought the problem out and now I don't need any more advice...at least on this issue ;) Thanks! beno -- http://mail.python.org/mailman/listinfo/python-list
TypeError
Hi; I get this error: /var/www/html/angrynates.com/christians/cart/simplemail/mail.py 153 154 ''' 155 commitSale() 156 myMail() 157 print ''' commitSale = /var/www/html/angrynates.com/christians/cart/simplemail/mail.py in commitSale() 98 cursor.execute('select max(ID) from %sCustomerData;' % store) 99 custID = cursor.fetchone()[0] 100 customerData(store, tmpTable, custID, patientID) 101 102 def myMail(): global customerData = , global store = 'products', tmpTable = 'tem12627568064', custID = 1, global patientID = 'None' /var/www/html/angrynates.com/christians/cart/customerData.py in customerData(store='products', tmpTable='tem12627568064', custID=1, patientID='None') 39 40 """ 41 print """ 42 print '%s Customer Data' % (store[0].upper() + store[1:]) 43 cursor.execute('describe %sCustomerData' % store) store = 'products' ValueError: unsupported format character '(' (0x28) at index 54 args = ("unsupported format character '(' (0x28) at index 54",) Apparently that character is a "file separator", which I presume is an invisible character. I tried retyping the area in question, but with no avail (threw same error). Please advise. Complete code follows. TIA, beno #!/usr/bin/python import sys,os sys.path.append(os.getcwd()) import MySQLdb from login import login import re, string def customerData(store, tmpTable, custID, patientID): user, passwd, db, host = login() db = MySQLdb.connect(host, user, passwd, db) cursor = db.cursor() page= """#!/usr/bin/python import sys,os sys.path.append(os.getcwd()) import MySQLdb from login import login import re, string from particulars import ourStores, ourOptions import fpformat from sets import Set from processOrder import processOrder def %sCustomerData(): print 'Content-Type: text/html' print print ''' http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd";> http://www.w3.org/1999/xhtml";> .text { font-family: Arial, Helvetica, sans-serif; font-size: 16px; text-decoration: none; text-align: justify} """ print """ print '%s Customer Data' % (store[0].upper() + store[1:]) cursor.execute('describe %sCustomerData' % store) cols = [itm[0] for itm in cursor] cursor.execute('select * from %sCustomerData where ID=%s' % (store, custID)) ourCustomerData = cursor.fetchone() ourColsAndCustomerDataDict = dict(zip(cols, ourCustomerData)) for col, data in ourColsAndCustomerDataDict.iteritems(): print '%s: %s' % (col, data) print "Customer Order" print "\n " numberShippingFields, totalTotal, html = processOrder(patientID, store, tmpTable) print html print " " if store != 'prescriptions': numberCols = 12 + numberShippingFields else: numberCols = 8 + numberShippingFields print ' \nTOTAL\n $%s\n ' % (numberCols - 1, str(fpformat.fix(round(int(totalTotal * 100))/100,2))) print "" cursor.close() print ''' ''' %sCustomerData() """ % (store, store) os.chdir('..') try: os.delete('%s/%sCustomerData.py' % (os.getcwd(), store)) except AttributeError: pass file = '%sCustomerData.py' % store f = open(file, 'w') f.write(page) f.close() # os.chown('%s/%s' % (os.getcwd(), file), 500, 500) os.chmod('%s/%s' % (os.getcwd(), file), 0755) -- The Logos has come to bear http://logos.13gems.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, Jan 5, 2010 at 8:45 PM, Phlip wrote: > > Here's a super easy example: > > { 42: 'forty two' }.get(41, None) > > Because I can supply a default, I can decide what is an error and what > is . > > Now the equivalent in a language that does not enjoy this false "Zen": > > { 42: 'forty two' }[41] # returns None > { 42: 'forty two' }.fetch(41, None) # ibid > { 42: 'forty two' }.fetch(41) # raises an exception Here's another super easy example. Should {42 : 'forty two'}[41] be treated the same as {41: None}[41] ? In cases where None is a valid result, you can't use it to signal failure. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
Steven D'Aprano wrote: > Stick around and you might learn something, > but if you bite every time somebody tries to teach you, you'll soon run > out of people willing to help you. The ongoing crowdsourced development by this group of "Victor Subervi's" project would seem to indicate that this isn't the case. Or that if it is, that it will be a good handful of months at the very soonest before it becomes true. -- http://mail.python.org/mailman/listinfo/python-list
Re: chown'ing by script
Victor Subervi wrote: > Hi; > I have a script that is called via the web. This script writes another > script that is also called by the web, which in turn needs to have > execution privileges. The problem is that the programmatically created > file is owned by apache.apache and thus doesn't have execution > privileges. I've tried os.chown(...) but this throws an OSError. I > understand that chown'ing programmatically opens a big security hole. > However, setting the gid to give apache execution privileges isn't any > better. What do you suggest? I suggest you find a way to achieve whatever it is you are trying to achieve without having to execute programmatically created scripts. What is the underlying problem you're trying to solve with this approach? -Carsten -- http://mail.python.org/mailman/listinfo/python-list
Re: chown'ing by script
On 06Jan2010 01:21, Victor Subervi wrote: | I have a script that is called via the web. This script writes another | script that is also called by the web, which in turn needs to have execution | privileges. The problem is that the programmatically created file is owned | by apache.apache and thus doesn't have execution privileges. I've tried | os.chown(...) but this throws an OSError. I understand that chown'ing | programmatically opens a big security hole. However, setting the gid to give | apache execution privileges isn't any better. What do you suggest? Are you sure you don't want chmod? -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Winter is gods' way of telling us to polish. - Peter Harper -- http://mail.python.org/mailman/listinfo/python-list
Re: Do I have to use threads?
Thanks.i will look into multiprocessing. Aditya -- http://mail.python.org/mailman/listinfo/python-list
chown'ing by script
Hi; I have a script that is called via the web. This script writes another script that is also called by the web, which in turn needs to have execution privileges. The problem is that the programmatically created file is owned by apache.apache and thus doesn't have execution privileges. I've tried os.chown(...) but this throws an OSError. I understand that chown'ing programmatically opens a big security hole. However, setting the gid to give apache execution privileges isn't any better. What do you suggest? TIA, beno -- The Logos has come to bear http://logos.13gems.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python] Re: Printing plain text with exact positioning on Windows
KvS wrote: Sorry, one more. I completely forgot it's not exactly plain text, but occasionally also a limited number of non-ASCII characters (accents in names etc.). Would this be possible through your method? If Windows can print it, then MSWinPrint.py should be able to also. But I haven't tested that extensively. YMMV. -- http://mail.python.org/mailman/listinfo/python-list
Re: Minor bug in multiprocessing?
"Aahz" wrote: > Frank Millman wrote: >> >>Is this worth reporting, if it has not been reported already? > > Defiitely report it. Thanks, Aahz. I took the lack of responses to indicate that there was no reason *not* to report it, so I reported it on 24th December (issue 7571), and it was fixed on the same day (r77038). Frank -- http://mail.python.org/mailman/listinfo/python-list
Re: Do I have to use threads?
On Tue, Jan 5, 2010 at 9:36 PM, Philip Semanchuk wrote: > > On Jan 5, 2010, at 11:26 PM, aditya shukla wrote: > > Hello people, >> >> I have 5 directories corresponding 5 different urls .I want to download >> images from those urls and place them in the respective directories.I have >> to extract the contents and download them simultaneously.I can extract the >> contents and do then one by one. My questions is for doing it >> simultaneously >> do I have to use threads? >> > > No. You could spawn 5 copies of wget (or curl or a Python program that > you've written). Whether or not that will perform better or be easier to > code, debug and maintain depends on the other aspects of your program(s). > > bye > Philip Obviously, spawning 5 copies of wget is equivalent to starting 5 threads. The answer is 'yes'. -- http://mail.python.org/mailman/listinfo/python-list
Re: Do I have to use threads?
On Tue, Jan 5, 2010 at 11:26 PM, aditya shukla wrote: > Hello people, > > I have 5 directories corresponding 5 different urls .I want to download > images from those urls and place them in the respective directories.I have > to extract the contents and download them simultaneously.I can extract the > contents and do then one by one. My questions is for doing it simultaneously > do I have to use threads? > > Please point me in the right direction. > > Threads in python are very easy to work with but not very efficient and for most cases slower than running multiple processes. Look at using multiple processes instead of going with threads performance will be much better. > Thanks > > Aditya > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- [ Rodrick R. Brown ] http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, 05 Jan 2010 17:45:58 -0800, Phlip wrote: > On Jan 5, 5:01 pm, Chris Rebert wrote: > >> > Why can't int('nonnumeric') return None? >> >> Errors should never pass silently. > > You are saying I, as the programmer, cannot decide what is an error and > what is a pass-thru. The decision is made for me. Every function decides for you what is an error and what isn't. >>> max() Traceback (most recent call last): File "", line 1, in TypeError: max expected 1 arguments, got 0 But I wanted it to return (sys.maxint - 7). How DARE the creator of the language decide that it should be an error instead of returning the arbitrary result I choose! Not. > (Yes yes I can write int_or_None(), etc...) That's right. As a programmer, your job is to program. If a language doesn't provide a function you want, write your own using the primitives available to you. > Here's a super easy example: > > { 42: 'forty two' }.get(41, None) > > Because I can supply a default, I can decide what is an error and what > is . You can ALWAYS decide what is an error. d = {42: 'forty two'} try: d[41] except KeyError: print "All is good, no problems, dict does not contain 41" except: print "WARNING WARNING WARNING!!!" print "FATAL ERROR: dict contains 41!!!" sys.exit() You do so by programming. As a programmer, that's your job. > Now the equivalent in a language that does not enjoy this false "Zen": > > { 42: 'forty two' }[41] # returns None Suppose you have a dict d supplied from somewhere else. You don't know what's in it. You do this: d[41] and you get a result None. Does this mean that the dict looks like this? d = {} or like this? d = {41: None} > { 42: 'forty two' }.fetch(41, None) # ibid In Python, "fetch" is spelled "get". > { 42: 'forty two' }.fetch(41) # raises an exception In Python, that would be spelled {42: 'forty two'}[41] > The quicky validation is available if I _request_ it. When you write d[41] you are requesting it. If you don't want it, use get instead. >> Quibbling over a mere one more line of code (or writing one short >> function) seems a bit petty. > > Because that "Zen of Python" is an empty sophistry that forces me to add > a "mere one more line of code" over and over again... If you're writing that one line of code over and over again, that's a good sign that you're doing it wrong and should rethink your strategy. >> > (A related question - why can't I just go 'if record = method(): use >> > (record)'. Why extra lines just to trap and assign the variable >> > before using it?) >> >> I believe that's disallowed so as to prevent the subtle bugs seen in C >> code which result from when someone makes a typo and omits the second >> "=" in their `if foo == bar():` test. > > Don't prevent me from using a technique just because others had trouble > with it. Any language allows and prevents certain techniques, simply by the very nature of the language. Every language has it's own syntax: you can't write weakly-typed stack-based concatenative code (like Forth) in Java, or strongly-typed dynamic object-oriented Python code in Pascal. Every language forces the programmer to use some features and avoid others. > And if bar() == foo is the superior technique anyway, because the == > happens in chronological and lexical order after the bar() call. That makes no sense. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Tue, 05 Jan 2010 15:30:09 -0800, Phlip wrote: >> > Does it say how to convert a string containing either an integer >> > representation, or something alphabetic, into an integer, or a zero, >> > in like 1 method call? (No except: ?) >> >> If you mean something like this: >> >> >>> int('153') >> >> 153 > > The point: int('') or int('something') both throw an error. Well, that's a bug. Obviously they should return 42. Or is that 23? I forget. But seriously... of course they do. What did you expect them to do? Any Python function will raise an exception if you pass invalid data to it. So the solutions are, don't pass invalid data, or wrap the function in a try block. If you don't want to use try, then make sure that your data is good. > In general, > this is hand-holding, but in specific I don't think the "rich and > structured" documentation will cover how to beat a 0 out of it in less > than 3 lines. So I will persist in my idiotic questions here! > >> Then perhaps you should work through the tutorial to learn the basics. > > They will tell me how to use except: (which is a good example why a > program should not use exceptions for its normal control flow if at all > possible). Huh? So because YOU don't know how to use except, programs shouldn't use exceptions for flow control? I reject that, and I will continue using exceptions for flow control when I think it is appropriate. > Please, please, please save your newbie admonitions for those who > qualify! Oh please, get off your high horse. You're asking newbie questions, no matter how many years of using Python you may or may not have, your experience is obviously low. Stick around and you might learn something, but if you bite every time somebody tries to teach you, you'll soon run out of people willing to help you. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, 05 Jan 2010 15:51:29 -0800, Phlip wrote: > Why can't int('nonnumeric') return None? It could do that, but it shouldn't, because returning magic values instead of raising exceptions is usually a bad, bad idea. > (A related question - why can't I just go 'if record = method(): use > (record)'. Why extra lines just to trap and assign the variable before > using it?) Because that idiom is responsible for probably the most common error in C of all, at least one of the most common errors. Thank goodness Python forbids such a dangerous construct. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Wed, 06 Jan 2010 00:58:58 +, r0g wrote: > Steven D'Aprano wrote: >> On Wed, 06 Jan 2010 09:39:08 +1100, Ben Finney wrote: >> >>> r0g writes: >>> Of course I'm now guilty of pedantry too :/ I might have let it slip had you not started your reply with the word "No", that just p* me off. >>> Well, if being told “no” is going to piss you off, I think you're in >>> for a rough time. >> >> Oh, you're in trouble now! If you think he gets upset at being told no, >> you should see how upset he gets at being told he's in for a rough >> time!!! >> >> *wink* >> >> >> >> > > NO! It's a rude way to start a sentence don't you think? No. I'm not Japanese, I don't feel any social prohibition at saying No in this context. I'm also happy to start a sentence with "You're wrong", and occasionally I give in to temptation to start it with "Are you on crack?". > Just because > you're correcting someone doesn't mean you have to be combative and try > and make them feel small. Unless they've adopted a hostile or wilfully > ignorant tone there's no reason to be so brusqe with people. You can be > both nice AND terse you know. Now I feel hurt that you're publicly rebuking me and making me feel as if I've done something wrong by trying to lighten the mood with a small joke... Nah, just kidding. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Do I have to use threads?
On Jan 5, 2010, at 11:26 PM, aditya shukla wrote: Hello people, I have 5 directories corresponding 5 different urls .I want to download images from those urls and place them in the respective directories.I have to extract the contents and download them simultaneously.I can extract the contents and do then one by one. My questions is for doing it simultaneously do I have to use threads? No. You could spawn 5 copies of wget (or curl or a Python program that you've written). Whether or not that will perform better or be easier to code, debug and maintain depends on the other aspects of your program(s). bye Philip -- http://mail.python.org/mailman/listinfo/python-list
Do I have to use threads?
Hello people, I have 5 directories corresponding 5 different urls .I want to download images from those urls and place them in the respective directories.I have to extract the contents and download them simultaneously.I can extract the contents and do then one by one. My questions is for doing it simultaneously do I have to use threads? Please point me in the right direction. Thanks Aditya -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
Phlip wrote: > They will tell me how to use except: (which is a good example why a > program should not use exceptions for its normal control flow if at > all possible). Really? Magic functions that coerce and eat errors are a better coding technique than exceptions and explicit handling? What kool-aid have you been drinking? -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
Antoine Pitrou wrote: The point: int('') or int('something') both throw an error. In general, this is hand-holding, but in specific I don't think the "rich and structured" documentation will cover how to beat a 0 out of it in less than 3 lines. Because it's a bad idea to do so and Python doesn't encourage such sloppy behaviour. If you like it, though, you might prefer PHP. By the way: error reporting is definitely *not* hand-holding. It is simply not good to let errors pass silently by default. Again, if you think the contrary, PHP is your friend ;-) Someone wrote about a port of a Perl script to Python. The script would parse the output from a program it would call, but the Python script sometimes raised a ValueError when it tried to convert a certain field to a number. It turned out that sometimes the field would contain "ERROR" instead of a number. The Perl script had been silently 'converting' any such "ERROR" to 0! -- http://mail.python.org/mailman/listinfo/python-list
Re: it gets worse (was: How do you configure IDLE on a Mac...)
On Jan 5, 4:03 pm, Ned Deily wrote: > In article > <6672dad2-26ba-458b-8075-21bac6506...@e37g2000yqn.googlegroups.com>, Mensanator > wrote: > > [...] > > > > > > > So, for all practical purposes, the macports install is broken also. > > > IDLE simply does not work in an X11 window (you think someone would > > have noticed that). The missing preferences is just the beginning. > > Apparently NONE of the menu item shortcuts work. > > > For example, the Cut, Copy, Paste shortcuts are given as Command-X, > > Command-C and Command-V. But that doesn't work in an X11 window, > > apperently only in an Aqua Tk (parent application appears as IDLE). > > > Of course, I can do Control-X, Control-C and Control-V to do Cut, > > Copy and Paste. Don't know if this works for all shortcuts, but > > I suppose I could just pick them from the menu (and I can bang > > my head against the wall while I'm at it). > > > What do you think, suppose I copy the gmpy built with the macports > > install over to the directory where the python.org version is? Would > > it > > import? If that'll work, I can switch back to using the python.org > > install and use it's version of IDLE. I certainly won't be needing > > distutils once I have a working version of gmpy. > > Let's go back to your original problem, which, if I understand > correctly, was trying to get going with Python 3 and gmpy on OS X 10.6. Right. > (Sorry I was away over the holidays and didn't get a chance to respond > to your original postings at the time.) Hey, no problem. I bought this dingus with the idea I would spend my the holiday time to get it to work. Boy, was I right. > I believe the problems you > originally encountered with installing gmpy were all due to a couple of > problems with building C extension modules on 10.6 when using the > current 3.1.1 OS X python.org. Yeah, I used the Mac disk image for Python 3.1. > Unfortunately, 3.1.1 was released before > 10.6 was so there are a couple of important fixes that haven't yet been > released for 3.1 (but are in the 2.6.4 installer which was released > after 10.6 came out). Fortunately, though, there are simple workarounds > for the problems. Keep in mind, though, that, at the moment, the > python.org installers for OS X are 32-bit only; I just checked, I was told to check sys.maxint but that doesn't exist, I assume it's now sys.maxsize. On the python.org disk image, that returns >>> hex(sys.maxsize) '0x7fff' looks like 32 bits. > that will change in the > future but if you do need a 64-bit Python 3 you'll need to stick to > other solutions like MacPorts for the time being. The macports install of 3.1 gives: >>> hex(sys.maxsize) '07x7fff' so the macports must be 64 bits. > > First, make sure the gmp library you've installed has 32-bit support. Uh, why would I want that? If it comes down to a choice between IDLE and 64 bits, I'll live without IDLE. > If you installed it using MacPorts, check with the file command: > > $ file /opt/local/lib/libgmp.dylib > /opt/local/lib/libgmp.dylib: Mach-O universal binary with 2 architectures > /opt/local/lib/libgmp.dylib (for architecture i386): Mach-O dynamically > linked shared library i386 > /opt/local/lib/libgmp.dylib (for architecture x86_64): Mach-O 64-bit > dynamically linked shared library x86_64 I get /opt/local/lib/libgmpdylib: Mach-0 64-bit dynamically linked shared library x86_64 > > If it doesn't have an i386 variant, reinstall the gmp library from > MacPorts: But I only need that if I want to run the 32 bit version of Python from python.org. If I'm willing to use the 64 bit version from macports, I don't care, right? > > $ sudo port selfupdate # make sure MacPorts is up-to-date > $ sudo port clean gmp > $ sudo port install gmp +universal # install 32-/64-bit variants > > Second, you need to install the MacOSX10.4u SDK because the current > python.org pythons are built with it. Then I'll just not use the download from python.org. Is there a way to uninstall that disk image? > That SDK is included in the Snow > Leopard Xcode installer package but it is not installed by default. > There should be an Xcode.mpkg somewhere, perhaps on your hard disk if > your system came with Snow Leopard factory-installed or perhaps on a > restore DVD. If not, it's on the retail Snow Leopard DVD and can be > downloaded from the Apple Developer site. After launching the Xcode > installer, just select and install the "Mac OS 10.4 Support" package > from the Custom Install menu. > > Third, you need to tell Distutils to use the older gcc-4.0 instead of > the gcc-4.2 which is now the default on 10.6. > > $ cd /path/to/gmpy-1.11rc1 > $ export CC=/usr/bin/gcc-4.0 > $ /usr/local/bin/python3.1 setup.py install > ... > $ /usr/local/bin/python3.1 test3/gmpy_test.py > Unit tests for gmpy 1.11 > on Python 3.1.1 (r311:74543, Aug 24 2009, 18:44:04) > [GCC 4.0.1 (Apple Inc. build 5493)] > Testing gmpy 1.11 (GMP 4.3.1), default caching (100,
Re: Exception as the primary error handling mechanism?
On Tue, Jan 5, 2010 at 5:45 PM, Phlip wrote: > On Jan 5, 5:01 pm, Chris Rebert wrote: >> > Why can't int('nonnumeric') return None? >> >> Errors should never pass silently. > > You are saying I, as the programmer, cannot decide what is an error > and what is a pass-thru. The decision is made for me. (Yes yes I can > write int_or_None(), etc...) No, you can certainly decide yourself. You merely need be explicit about it; you're leaving out the other tandem half of the couplet: "*Unless* explicitly silenced." Throwing exceptions is only the default because it tends to lead to more reliable code in that you're forced to deal with the error condition by either catching an exception or preventing one from being thrown (by, say, using a different method in the API that just returns a default value instead). As an aside, I would guess the built-in types don't have default value parameters for conversions partly because it'd be a bit less elegant to implement; since None is a commonly used default value, they'd have to use a different sentinel as said parameter's default value to indicate that the caller wanted an exception raised, and having a hierarchy of nil values in the core language detracts from the language's elegance. At any rate, if you like dict.get(), I don't see why, say, my_int('nonnumeric', None) should be such a problem. > Here's a super easy example: > > { 42: 'forty two' }.get(41, None) > > Because I can supply a default, I can decide what is an error and what > is . Exactly, that's explicitly silencing the error; no one ever said an `except` clause was the only silencing mechanism. You're making your intention clear by using .get() and passing in a desired default. I agree this is a fine thing. > Now the equivalent in a language that does not enjoy this false "Zen": > > { 42: 'forty two' }[41] # returns None > { 42: 'forty two' }.fetch(41, None) # ibid > { 42: 'forty two' }.fetch(41) # raises an exception > > The quicky validation is available if I _request_ it. Seems like rather "fast-and-loose" programming if you don't care about validation enough of the time to merit it being default. If your programming is indeed so fast-and-loose, I refer you to the recent comment about PHP (and other less "exception-happy" languages). Anyway, I do totally agree that you should, if feasible, be provided an easy way to designate a common error-handling strategy (for example, in this case, by using a default value via .fetch()). However, go too loose on error handling and exceptions and one ends up with something like JavaScript with its infamous `undefined` value which can make debugging a nightmare (whoever came up with the idea of JS's `undefined` should be slapped upside the head). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Minor bug in multiprocessing?
[p&e] In article , Frank Millman wrote: > >Is this worth reporting, if it has not been reported already? Defiitely report it. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
> The point: int('') or int('something') both throw an error. In general, > this is hand-holding, but in specific I don't think the "rich and > structured" documentation will cover how to beat a 0 out of it in less > than 3 lines. Because it's a bad idea to do so and Python doesn't encourage such sloppy behaviour. If you like it, though, you might prefer PHP. By the way: error reporting is definitely *not* hand-holding. It is simply not good to let errors pass silently by default. Again, if you think the contrary, PHP is your friend ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Fwd: I would like to install Python on my 64 bit Win 7
On 1/5/2010 11:31 AM, aung paing Soe wrote: Hello , I would like to study about Python Programming . So I want to install Python . But my laptop is Window 7 64-bit home basic . So please give me a advice how to install Python in my 64 bit computer. I really want to study python programming . I am looking forward your reply. Thank you very much Even though your laptop runs a 64-bit operating system, you do not have to install 64-bit Python build. You may also install a 32-bit build. (Some Python packages may not even build on 64-bit Python) You can install Python from one of the two places: 1. http://python.org/download/releases/2.6.4/ (AMD64 == 64-bit) 2. http://www.activestate.com/activepython/ (Includes PyWin32, a package manager and extra documentation and tutorials). -srid -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
Tim Chase wrote: > vsoler wrote: >> Hence, I need to parse Excel formulas. Can I do it by means only of re >> (regular expressions)? >> >> I know that for simple formulas such as "=3*A7+5" it is indeed >> possible. What about complex for formulas that include functions, >> sheet names and possibly other *.xls files? > > Where things start getting ugly is when you have nested function calls, > such as > > =if(Sum(A1:A25)>42,Min(B1:B25), if(Sum(C1:C25)>3.14, > (Min(C1:C25)+3)*18,Max(B1:B25))) > > Regular expressions don't do well with nested parens (especially > arbitrarily-nesting-depth such as are possible), so I'd suggest going > for a full-blown parsing solution like pyparsing. > > If you have fair control over what can be contained in the formulas and > you know they won't contain nested parens/functions, you might be able > to formulate some sort of "kinda, sorta, maybe parses some forms of > formulas" regexp. > And don't forget about named ranges, which can reference cells without using anything but a plain identifier ... regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest inconsistent
On Jan 5, 8:14 pm, Matt Haggard wrote: > Can anyone tell me why this test fails? > > http://pastebin.com/f20039b17 > > This is a minimal example of a much more complex thing I'm trying to > do. I'm trying to hijack a function and inspect the args passed to it > by another function. > > The reason the 'Tester' object has no attribute 'arg1' is because > "self" still refers to the object made for testA. Quick answer: change faketest.py as follows: #-- # faketest.py #-- #from importme import render import importme def run(somearg): return importme.render(somearg) = A long answer, with explanation, will cost you twice as much ;-) (but will have to wait) André -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
> Errors should never pass silently. > Unless explicitly silenced. > -- The Zen of Python (http://www.python.org/dev/peps/pep-0020/) "The person who says it cannot be done should never interrupt the person doing it" -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
>>> Python is a truly awesome programming language. Not only is Guido a >>> genius language designer, but he is also a great project leader. What >>> an accomplishment. Congratulations to everybody who has contributed >>> to Python in the last two decades! >> >> The more languages you learn before getting to Smalltalk, the more >> awesome Smalltalk will be for you. >> > After implementing SmallTalk I more or less gave up OO programming for > ten years, But why? What was so frightening about the OO model of SmallTalk? Cheers, Daniel > resuming it only after I met Python. SmallTalk didn't seem > that awesome to me. > > Though for its time it was an incredible system, its insistence on a > SmallTalk-only VM environment seemed a little solipsistic. -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest inconsistent
On Jan 5, 4:14 pm, Matt Haggard wrote: > Can anyone tell me why this test fails? > > http://pastebin.com/f20039b17 > > This is a minimal example of a much more complex thing I'm trying to > do. I'm trying to hijack a function and inspect the args passed to it > by another function. > > The reason the 'Tester' object has no attribute 'arg1' is because > "self" still refers to the object made for testA. I hope someone else can spot the low-level reason... ...but why aren't you using http://pypi.python.org/pypi/mock/ ? Look up its patch_object facility... -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Jan 5, 5:01 pm, Chris Rebert wrote: > > Why can't int('nonnumeric') return None? > > Errors should never pass silently. You are saying I, as the programmer, cannot decide what is an error and what is a pass-thru. The decision is made for me. (Yes yes I can write int_or_None(), etc...) Here's a super easy example: { 42: 'forty two' }.get(41, None) Because I can supply a default, I can decide what is an error and what is . Now the equivalent in a language that does not enjoy this false "Zen": { 42: 'forty two' }[41] # returns None { 42: 'forty two' }.fetch(41, None) # ibid { 42: 'forty two' }.fetch(41) # raises an exception The quicky validation is available if I _request_ it. > Quibbling over a mere one more line of code (or writing one short > function) seems a bit petty. Because that "Zen of Python" is an empty sophistry that forces me to add a "mere one more line of code" over and over again... > > (A related question - why can't I just go 'if record = method(): use > > (record)'. Why extra lines just to trap and assign the variable before > > using it?) > > I believe that's disallowed so as to prevent the subtle bugs seen in C > code which result from when someone makes a typo and omits the second > "=" in their `if foo == bar():` test. Don't prevent me from using a technique just because others had trouble with it. And if bar() == foo is the superior technique anyway, because the == happens in chronological and lexical order after the bar() call. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Lie Ryan wrote: > On 1/6/2010 1:48 AM, r0g wrote: >> Steven D'Aprano wrote: >>> On Tue, 05 Jan 2010 13:06:20 +, r0g wrote: If that's the case how can you expect it to validate anything at all in production? >>> >>> The asserts still operate so long as you don't use the -O switch. >>> > checking, since the function relies on the caller obeying the > contract[2] and never calling it with an invalid input. > > DbC uses assertions[1] spuriously, unlike the traditional approach which > is much more conservative when using assertions. > > [1] or explicit language support which is just syntax sugar for assertions > [2] of course, on a debug release, the contract validation code will > still be enforced to catch logic/consistency bugs that causes the violation Thanks for the responses Steven/Dave/Lie, that's some really insightful stuff :) Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, Jan 5, 2010 at 3:51 PM, Phlip wrote: > Peng Yu wrote: >> Otherwise, could some python expert explain to me why exception is >> widely used for error handling in python? Is it because the efficiency >> is not the primary goal of python? > > It's not about efficiency, it's about making assumptions for the > programmer about what kind of rigor they need. > > Why can't int('nonnumeric') return None? Errors should never pass silently. Unless explicitly silenced. -- The Zen of Python (http://www.python.org/dev/peps/pep-0020/) Better to throw an exception and ensure the case is specifically dealt with one way or another than to silently return an error flag result which may only delay the error until later in the program, making it harder to debug. Is it that much of a burden to write and use the small function that does what you want? def int_or_None(string): try: return int(string) except ValueError: return None Heck, you can even write it inline and dispense with the function if you want: try: foo = int(bar) except ValueError: foo = None Quibbling over a mere one more line of code (or writing one short function) seems a bit petty. > (A related question - why can't I just go 'if record = method(): use > (record)'. Why extra lines just to trap and assign the variable before > using it?) I believe that's disallowed so as to prevent the subtle bugs seen in C code which result from when someone makes a typo and omits the second "=" in their `if foo == bar():` test. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Steven D'Aprano wrote: > On Wed, 06 Jan 2010 09:39:08 +1100, Ben Finney wrote: > >> r0g writes: >> >>> Of course I'm now guilty of pedantry too :/ I might have let it slip >>> had you not started your reply with the word "No", that just p* me >>> off. >> Well, if being told “no” is going to piss you off, I think you're in for >> a rough time. > > Oh, you're in trouble now! If you think he gets upset at being told no, > you should see how upset he gets at being told he's in for a rough time!!! > > *wink* > > > NO! It's a rude way to start a sentence don't you think? Just because you're correcting someone doesn't mean you have to be combative and try and make them feel small. Unless they've adopted a hostile or wilfully ignorant tone there's no reason to be so brusqe with people. You can be both nice AND terse you know. I can't imagine why I expect good manners on usenet though, AFAICT it's never been like that (well not since I got on it anyway). Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Jan 5, 2010, at 4:30 PM, Phlip wrote: The point: int('') or int('something') both throw an error. In general, this is hand-holding, but in specific I don't think the "rich and structured" documentation will cover how to beat a 0 out of it in less than 3 lines. So I will persist in my idiotic questions here! What does an extra two or three lines of code matter? Dave-- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess.Popen does not close pipe in an error case
BTW, I'm using Python 2.6.2 on Linux. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Peng Yu wrote: > Otherwise, could some python expert explain to me why exception is > widely used for error handling in python? Is it because the efficiency > is not the primary goal of python? It's not about efficiency, it's about making assumptions for the programmer about what kind of rigor they need. Why can't int('nonnumeric') return None? Why can't a Django Record.objects.get(pk=-1) return a None? That's what it's for. (A related question - why can't I just go 'if record = method(): use (record)'. Why extra lines just to trap and assign the variable before using it?) There are workarounds that sometimes benefit the code. In the case of collections, like recordsets, you might be better off using for ... all (): Then your controlled block efficiently does not happen if it saw no records. "Efficiently" in terms of programmer complexity - the number and meaning of lines that a programmer must comprehend. And why can't Record.objects.get(pk='nonnumeric') return None? Because, of course, deep inside it calls int(). I can't simplify the calling code, and rely on garbage-in-None-out, because Python decided which simplifications I should avoid with self-righteous indignation. The Samurai Principle (return victorious, or not at all) is very useful, sometimes. But other times it just prematurely depletes your supply of Samurai... -- Phlip http://zeekland.zeroplayer.com/ -- http://mail.python.org/mailman/listinfo/python-list
subprocess.Popen does not close pipe in an error case
Below, I have a Python script that launches 2 child programs, prog1 and prog2, with prog1's stdout connected to prog2's stdin via a pipe. (It's like executing "prog1 | prog2" in the shell.) If both child programs exit with 0, then the script runs to completion. But if prog2 exits with non-0, prog1 does not exit and the script hangs (i.e. prog1.poll() always returns None) -- unless I uncomment the 2 lines marked by XXX to close prog1.stdout. I was expecting that I don't have to explicitly close prog1.stdout, whether prog2 succeeds or fails. Is the current behavior a bug in the subprocess module or is it expected? Or am I doing something wrong? Thanks. import subprocess import time # prog1: a program that writes lots of data to the pipe cmd = ['zcat', '--force', 'a_large_file'] prog1 = subprocess.Popen(cmd, bufsize=-1, stdout=subprocess.PIPE) # prog2: a program that fails without reading much data from the pipe cmd = ['python', '-c', 'import time; time.sleep(10); asdf'] prog2 = subprocess.Popen(cmd, bufsize=-1, stdin=prog1.stdout, stdout=open('popen.out', 'w')) print 'waiting for a while' retCodeProg2 = prog2.wait() print 'prog2 returns', retCodeProg2 # XXX # if retCodeProg2 != 0: # prog1.stdout.close() while prog1.poll() is None: print 'sleep a bit' time.sleep(1) retCodeProg1 = prog1.poll() print 'prog1 returns', retCodeProg1 -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
> > Does it say how to convert a string containing either an integer > > representation, or something alphabetic, into an integer, or a zero, in > > like 1 method call? (No except: ?) > > If you mean something like this: > > >>> int('153') > > 153 The point: int('') or int('something') both throw an error. In general, this is hand-holding, but in specific I don't think the "rich and structured" documentation will cover how to beat a 0 out of it in less than 3 lines. So I will persist in my idiotic questions here! > Then perhaps you should work through the tutorial to learn the basics. They will tell me how to use except: (which is a good example why a program should not use exceptions for its normal control flow if at all possible). Please, please, please save your newbie admonitions for those who qualify! -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Tue, 05 Jan 2010 14:40:49 -0800, Phlip wrote: > On Jan 5, 1:10 pm, Antoine Pitrou wrote: > >> http://docs.python.org/library/functions.html >> >> Don't forget that the Python documentation is rich and structured. And >> good luck. > > Does it say how to convert a string containing either an integer > representation, or something alphabetic, into an integer, or a zero, in > like 1 method call? (No except: ?) If you mean something like this: >>> int('153') 153 then yes it does. But if you mean something like this: >>> some_mysterious_function('one hundred and fifty-three') 153 then no, it doesn't. > Nothing personal, but I'm finding the super-hard stuff very facile & > tractable, and the easy stuff absurdly hard around here... Then perhaps you should work through the tutorial to learn the basics. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Wed, 06 Jan 2010 09:39:08 +1100, Ben Finney wrote: > r0g writes: > >> Of course I'm now guilty of pedantry too :/ I might have let it slip >> had you not started your reply with the word "No", that just p* me >> off. > > Well, if being told “no” is going to piss you off, I think you're in for > a rough time. Oh, you're in trouble now! If you think he gets upset at being told no, you should see how upset he gets at being told he's in for a rough time!!! *wink* -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Python multiprocessing: Permission denied
On Tue, 05 Jan 2010 13:52:18 -0800, t0ster wrote: > It looks like the user don't have permission to access shared memory. > When executing with root privileges it works fine. > > Is there any solution to run it as normal user(not root)? Then give the user permission to access shared memory. Why do you expect that Python would be able to over-ride the operating system's security? This problem is no different from saying "It looks like the user doesn't have permission to access this file". -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Jan 5, 1:10 pm, Antoine Pitrou wrote: > http://docs.python.org/library/functions.html > > Don't forget that the Python documentation is rich and structured. > And good luck. Does it say how to convert a string containing either an integer representation, or something alphabetic, into an integer, or a zero, in like 1 method call? (No except: ?) Nothing personal, but I'm finding the super-hard stuff very facile & tractable, and the easy stuff absurdly hard around here... -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
r0g writes: > Of course I'm now guilty of pedantry too :/ I might have let it slip > had you not started your reply with the word "No", that just p* me > off. Well, if being told “no” is going to piss you off, I think you're in for a rough time. > Having said that I find the mental image of you slamming your fist on > the table and shouting it out loud whenever you read something you > disagree with on usenet quite amusing! In return, I find myself quite amused that you would get such an image. To reduce the stress you describe above, you might want to read what is written, rather than inventing fanciful emotion where it wasn't in the message to begin with. -- \ “One time I went to a drive-in in a cab. The movie cost me | `\ ninety-five dollars.” —Steven Wright | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
Mensanator wrote: On Jan 5, 12:35 pm, MRAB wrote: vsoler wrote: Hello, I am acessing an Excel file by means of Win 32 COM technology. For a given cell, I am able to read its formula. I want to make a map of how cells reference one another, how different sheets reference one another, how workbooks reference one another, etc. Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? For example"=Book1!A5+8" should be parsed into ["=","Book1", "!", "A5","+","8"] Can anybody help? Any suggestions? Do you mean "how" or do you really mean "whether", ie, get a list of the other cells that are referred to by a certain cell, for example, "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5] Ok, although "Book1" would be the default name of a workbook, with default worksheets labeled "Sheet1". "Sheet2", etc. If I had a worksheet named "Sheety" that wanted to reference a cell on "Sheetx" OF THE SAME WORKBOOK, it would be =Sheet2!A7. If the reference was to a completely different workbook (say Book1 with worksheets labeled "Sheet1", "Sheet2") then the cell might have =[Book1]Sheet1!A7. And don't forget the $'s! You may see =[Book1]Sheet1!$A$7. I forgot about the dollars! In that case, the regex is: references = re.findall(r"\b((?:\w+!)?\$?[A-Za-z]+\$?\d+)\b", formula) -- http://mail.python.org/mailman/listinfo/python-list
Re: it gets worse (was: How do you configure IDLE on a Mac...)
In article <6672dad2-26ba-458b-8075-21bac6506...@e37g2000yqn.googlegroups.com>, Mensanator wrote: [...] > So, for all practical purposes, the macports install is broken also. > > IDLE simply does not work in an X11 window (you think someone would > have noticed that). The missing preferences is just the beginning. > Apparently NONE of the menu item shortcuts work. > > For example, the Cut, Copy, Paste shortcuts are given as Command-X, > Command-C and Command-V. But that doesn't work in an X11 window, > apperently only in an Aqua Tk (parent application appears as IDLE). > > Of course, I can do Control-X, Control-C and Control-V to do Cut, > Copy and Paste. Don't know if this works for all shortcuts, but > I suppose I could just pick them from the menu (and I can bang > my head against the wall while I'm at it). > > What do you think, suppose I copy the gmpy built with the macports > install over to the directory where the python.org version is? Would > it > import? If that'll work, I can switch back to using the python.org > install and use it's version of IDLE. I certainly won't be needing > distutils once I have a working version of gmpy. Let's go back to your original problem, which, if I understand correctly, was trying to get going with Python 3 and gmpy on OS X 10.6. (Sorry I was away over the holidays and didn't get a chance to respond to your original postings at the time.) I believe the problems you originally encountered with installing gmpy were all due to a couple of problems with building C extension modules on 10.6 when using the current 3.1.1 OS X python.org. Unfortunately, 3.1.1 was released before 10.6 was so there are a couple of important fixes that haven't yet been released for 3.1 (but are in the 2.6.4 installer which was released after 10.6 came out). Fortunately, though, there are simple workarounds for the problems. Keep in mind, though, that, at the moment, the python.org installers for OS X are 32-bit only; that will change in the future but if you do need a 64-bit Python 3 you'll need to stick to other solutions like MacPorts for the time being. First, make sure the gmp library you've installed has 32-bit support. If you installed it using MacPorts, check with the file command: $ file /opt/local/lib/libgmp.dylib /opt/local/lib/libgmp.dylib: Mach-O universal binary with 2 architectures /opt/local/lib/libgmp.dylib (for architecture i386): Mach-O dynamically linked shared library i386 /opt/local/lib/libgmp.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64 If it doesn't have an i386 variant, reinstall the gmp library from MacPorts: $ sudo port selfupdate# make sure MacPorts is up-to-date $ sudo port clean gmp $ sudo port install gmp +universal # install 32-/64-bit variants Second, you need to install the MacOSX10.4u SDK because the current python.org pythons are built with it. That SDK is included in the Snow Leopard Xcode installer package but it is not installed by default. There should be an Xcode.mpkg somewhere, perhaps on your hard disk if your system came with Snow Leopard factory-installed or perhaps on a restore DVD. If not, it's on the retail Snow Leopard DVD and can be downloaded from the Apple Developer site. After launching the Xcode installer, just select and install the "Mac OS 10.4 Support" package from the Custom Install menu. Third, you need to tell Distutils to use the older gcc-4.0 instead of the gcc-4.2 which is now the default on 10.6. $ cd /path/to/gmpy-1.11rc1 $ export CC=/usr/bin/gcc-4.0 $ /usr/local/bin/python3.1 setup.py install ... $ /usr/local/bin/python3.1 test3/gmpy_test.py Unit tests for gmpy 1.11 on Python 3.1.1 (r311:74543, Aug 24 2009, 18:44:04) [GCC 4.0.1 (Apple Inc. build 5493)] Testing gmpy 1.11 (GMP 4.3.1), default caching (100, 128) ... 1500 tests in 42 items. 1500 passed and 0 failed. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Python multiprocessing: Permission denied
Hi guys, I'm getting an error when trying to execute python program that uses multiprocessing package: File "/usr/local/lib/python2.6/multiprocessing/__init__.py", line 178, in RLock return RLock() File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 142, in __init__ SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1) File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 49, in __init__ sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue) OSError: [Errno 13] Permission denied It looks like the user don't have permission to access shared memory. When executing with root privileges it works fine. Is there any solution to run it as normal user(not root)? Python version 2.6.2 , OS is Linux 2.6.18 (CentOS release 5.4) and it's VPS machine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic text color
John Posner wrote: On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick wrote: It sounds like the program is doing exactly what you TOLD it to do (which might not be what you WANT it to do): 1. In an earlier pass on the text, color the string "dog" red. 2. In a later pass, color the string "do" green. You need to decide what you WANT to happen if one word to be colored is a substring of another word to be colored differently. Or maybe you want to outlaw such situations. After making that decision, you can start to think about how to write the appropriate code. Best, John Darn thing doing what I told it to do... Guess that means I did something right :) But it is not what I am wanting. I first thought to make it look for a space but that would not work when a single character like "#" is to be colored if there is a "string" of them. Or if all of the characters between quotes are to be colored. I always did like puzzles! Dave -- http://mail.python.org/mailman/listinfo/python-list
Commands for a breakpoint in .pdbrc
Hi all, I'd like to save the commands for a breakpoint in a .pdbrc, something like: b 81 commands 1 pp foo.attr1 pp foo.attr2 end b 108 commands 2 pp bar.attr1 pp bar.attr2 end This would automate setting the environment for the debugging session. However, this does not work with 'python -m pdb script.py', because at the line 'commands 1', the pdb prompt starts and asks me for the commands for the first breakpoint, ignoring what I wrote in .pdbrc; further, it raises a NameError after I type 'end' at the pdb prompt, because of 'foo.attr1', 'foo.attr2' and even 'end'. The same happens for the rest of the breakpoints, so I end up with them set but not their commands. What would be the correct way to do this? Is it even possible? Thanks a lot, Pablo Torres N. -- http://mail.python.org/mailman/listinfo/python-list
Re: A null program - what is it doing?
r0g wrote: Gib Bogle wrote: No doubt a dumb question from a noob: The following program (a cut down version of some test code) uses no CPU, and does not terminate: import sys from PyQt4.QtCore import * if __name__=="__main__": app = QCoreApplication(sys.argv) sys.exit(app.exec_()) What is the program doing? What is the correct way to terminate the execution? Thanks in advance for educating me. I've never used QT but other graphical toolkits I have used all start their own "main loop" which is a loop that cycles round receiving, queuing and dispatching "events". You probably need to call the QCoreApplication's quit method to break out of this e.g. app.exit() or something similar, have a look at some complete PyQt4 examples or google for PyQt4 mainloop. Roger. Thanks. I've realized that QCoreApplication (or QApplication) manages the event loop, and normally when it is executed you are showing a widget of some kind, closing which ends the application. Alt-F4 acts like Ctrl-C to terminate from the keyboard. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
>> Couldn't you just use the built-in enumerate() to replace the whole >> thing? > > Because that would involve, like, reading an entire Python book just to > locate that method? Actually, no. It just involves reading one of the most important pages in the documentation, the page which describes the built-in functions: http://docs.python.org/library/functions.html Don't forget that the Python documentation is rich and structured. And good luck. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
> > My Pythonic sequencing skills are obviously feeble. Can anything think > > of a way to write that in fewer lines? Thanks, all! > Couldn't you just use the built-in enumerate() to replace the whole thing? Because that would involve, like, reading an entire Python book just to locate that method? GMAB I'm too busy writing high-end Django via TDD & BDD! C-: -- Phlip http://zeekland.zeroplayer.com/Pigleg_Too/1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic text color
On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick wrote: It's certainly a mistake to use the expression "str(rList).split()". Using str() to convert the list "rList" into a string creates a mess that includes square-bracket characters. Did this actually work for you? It sort of worked. With one color file it seemed fine but after I posted I added another color file and things fell apart. Now with the above fixes it works with three colors from three files. When the list are printed to the shell the list look like this: redList ['red', 'dog', 'apple', '#'] blueList ['blue', 'ball', 'berry'] greenList ['green', 'grass', 'do'] But another problem is noticed. It does not matter if the list is built in code or from a file. If dog is entered, "do" will be green with the "g" being red. Back to the drawing board. It sounds like the program is doing exactly what you TOLD it to do (which might not be what you WANT it to do): 1. In an earlier pass on the text, color the string "dog" red. 2. In a later pass, color the string "do" green. You need to decide what you WANT to happen if one word to be colored is a substring of another word to be colored differently. Or maybe you want to outlaw such situations. After making that decision, you can start to think about how to write the appropriate code. Best, John -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Jan 5, 8:58 pm, Phlip wrote: > Hypo Nt: > > def each_with_index(seq): > index = 0 > result = [] > > for item in seq: > result.append([item, index]) > index += 1 > > return result > > My Pythonic sequencing skills are obviously feeble. Can anything think > of a way to write that in fewer lines? > > -- > Phlip > http://c2.com/cgi/wiki?MoreliaViridis You could use the build-in function enumerate inside a list comprehension. >>> seq = range(5) >>> [ (i,s) for i,s in enumerate(seq) ] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] This will reduce the function to a one-liner. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
Phlip wrote: > Hypo Nt: > > def each_with_index(seq): > index = 0 > result = [] > > for item in seq: > result.append([item, index]) > index += 1 > > return result > > My Pythonic sequencing skills are obviously feeble. Can anything think > of a way to write that in fewer lines? Couldn't you just use the built-in enumerate() to replace the whole thing? -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Jan 6, 8:58 am, Phlip wrote: > Hypo Nt: > > def each_with_index(seq): > index = 0 > result = [] > > for item in seq: > result.append([item, index]) > index += 1 > > return result > > My Pythonic sequencing skills are obviously feeble. Can anything think > of a way to write that in fewer lines? > > -- > Phlip > http://c2.com/cgi/wiki?MoreliaViridis y = [[seq[i],i] for i in range(len(seq))] gives the same result. The index is accessible without assigning it to another variable. -- Anita -- http://mail.python.org/mailman/listinfo/python-list
Re: Fwd: I would like to install Python on my 64 bit Win 7
On Tue, Jan 5, 2010 at 3:03 PM, Dave McCormick wrote: > > > aung paing Soe wrote: > > -- Forwarded message -- > From: aung paing Soe > Date: Tue, Jan 5, 2010 at 11:27 AM > Subject: I would like to install Python on my 64 bit Win 7 > To: webmas...@python.org > > > Hello , > I would like to study about Python Programming . So I want to > install Python . > But my laptop is Window 7 64-bit home basic . > So please give me a advice how to install Python in my 64 bit computer. > I really want to study python programming . > I am looking forward your reply. > Thank you very much > > Yours, > Beginner > > I am using WIN7 64 bit also. > Go to python.org and look for > http://www.python.org/download/releases/2.5.4/ > python-2.5.4.amd64.msi > > Dave Unless you need a package that requires Python 2.5, it's a good idea to use Python 2.6 instead. 2.5 isn't getting any more bug fixes and 2.6 includes quite a few new features. http://python.org/download/releases/2.6.4/ > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
On Jan 5, 8:22 am, n00m wrote: > Stick your English into your ass Most people would say "up your ass". And use a period at the end of the sentence. Got any more funny insults? -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic text color
John Posner wrote: Dave, you're doing exactly the right thing: gradually expanding your program, to provide more functionality and to learn more about the available programming tools. It's also very good that you take care to close() the file after processing it. Now for the bad news ... Seems like there is always bad news :) 1. Don't use "file" as a variable name -- it's a built-in object type. (Some people don't like the fact that Python allows you to redefine such "reserved words".) 2. It's probably not the best idea to use a single variable (you use "file") to do double-duty: to hold the name of a file, and to hold the open-file object returned by the open() function. It's perfectly legal, but it hides information that might be useful when you're debugging a program. This is better: 3. It might be better to use read() rather than readlines() to process the "red.txt" file. It depends on what that file is supposed to contain. For example, if you expect "red.txt" to contain exactly one line, which has one or more words, you can process the open-file object like this: All noted and fixed. It's certainly a mistake to use the expression "str(rList).split()". Using str() to convert the list "rList" into a string creates a mess that includes square-bracket characters. Did this actually work for you? It sort of worked. With one color file it seemed fine but after I posted I added another color file and things fell apart. Now with the above fixes it works with three colors from three files. When the list are printed to the shell the list look like this: redList ['red', 'dog', 'apple', '#'] blueList ['blue', 'ball', 'berry'] greenList ['green', 'grass', 'do'] But another problem is noticed. It does not matter if the list is built in code or from a file. If dog is entered, "do" will be green with the "g" being red. Back to the drawing board. Here is the complete code" ## from Tkinter import * import re RFfile = 'red.txt' inpRF = open(RFfile,"r") rList = inpRF.read() inpRF.close() BFfile = 'blue.txt' inpBF = open(BFfile,"r") bList = inpBF.read() inpBF.close() GFfile = 'green.txt' inpGF = open(GFfile,"r") gList = inpGF.read() inpGF.close() def get_complete_text(event): complete_text = Tbox.get("1.0", END) redList = str(rList).split() blueList = str(bList).split() greenList = str(gList).split() print "redList",redList print "blueList",blueList print "greenList",greenList Tbox.tag_remove("red", "1.0", END) Tbox.tag_remove("blue", "1.0", END) Tbox.tag_remove("green", "1.0", END) RED redList_regexp = "|".join(redList) for matchobj in re.finditer(redList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("red", foreground="red") BLUE### blueList_regexp = "|".join(blueList) for matchobj in re.finditer(blueList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("blue", foreground="blue") GREEN### greenList_regexp = "|".join(greenList) for matchobj in re.finditer(greenList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("green", foreground="green") root = Tk() Tbox = Text(root, width=40, height=15, wrap=CHAR, font="Times 14 bold", bg="#dd") Tbox.pack() Tbox.bind("", get_complete_text) Tbox.focus() root.mainloop() Thanks again, Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: Fwd: I would like to install Python on my 64 bit Win 7
aung paing Soe wrote: -- Forwarded message -- From: aung paing SoeDate: Tue, Jan 5, 2010 at 11:27 AM Subject: I would like to install Python on my 64 bit Win 7 To: webmas...@python.org Hello , I would like to study about Python Programming . So I want to install Python . But my laptop is Window 7 64-bit home basic . So please give me a advice how to install Python in my 64 bit computer. I really want to study python programming . I am looking forward your reply. Thank you very much Yours, Beginner I am using WIN7 64 bit also. Go to python.org and look for http://www.python.org/download/releases/2.5.4/ python-2.5.4.amd64.msi Dave -- http://mail.python.org/mailman/listinfo/python-list
please help shrink this each_with_index() implementation
Hypo Nt: def each_with_index(seq): index = 0 result = [] for item in seq: result.append([item, index]) index += 1 return result My Pythonic sequencing skills are obviously feeble. Can anything think of a way to write that in fewer lines? -- Phlip http://c2.com/cgi/wiki?MoreliaViridis -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
On 5 ene, 20:21, vsoler wrote: > On 5 ene, 20:05, Mensanator wrote: > > > > > On Jan 5, 12:35 pm, MRAB wrote: > > > > vsoler wrote: > > > > Hello, > > > > > I am acessing an Excel file by means of Win 32 COM technology. > > > > For a given cell, I am able to read its formula. I want to make a map > > > > of how cells reference one another, how different sheets reference one > > > > another, how workbooks reference one another, etc. > > > > > Hence, I need to parse Excel formulas. Can I do it by means only of re > > > > (regular expressions)? > > > > > I know that for simple formulas such as "=3*A7+5" it is indeed > > > > possible. What about complex for formulas that include functions, > > > > sheet names and possibly other *.xls files? > > > > > For example "=Book1!A5+8" should be parsed into ["=","Book1", "!", > > > > "A5","+","8"] > > > > > Can anybody help? Any suggestions? > > > > Do you mean "how" or do you really mean "whether", ie, get a list of the > > > other cells that are referred to by a certain cell, for example, > > > "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5] > > > Ok, although "Book1" would be the default name of a workbook, with > > default > > worksheets labeled "Sheet1". "Sheet2", etc. > > > If I had a worksheet named "Sheety" that wanted to reference a cell on > > "Sheetx" > > OF THE SAME WORKBOOK, it would be =Sheet2!A7. If the reference was to > > a completely > > different workbook (say Book1 with worksheets labeled "Sheet1", > > "Sheet2") then > > the cell might have =[Book1]Sheet1!A7. > > > And don't forget the $'s! You may see =[Book1]Sheet1!$A$7. > > Yes, Mensanator, but... what re should I use? I'm looking for the re > statement. No doubt you can help! > > Thank you. Let me give you an example: >>> import re >>> re.split("([^0-9])", "123+456*/") [’123’, ’+’, ’456’, ’*’, ’’, ’/’, ’’] I find it excellent that one single statement is able to do a lexical analysis of an expression! If the expression contains variables, such as A12 or B9, I can try another re expression. Which one should I use? And if my expression contains parenthesis? And the sin() function? Vicente Soler -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
vsoler wrote: Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? Where things start getting ugly is when you have nested function calls, such as =if(Sum(A1:A25)>42,Min(B1:B25), if(Sum(C1:C25)>3.14, (Min(C1:C25)+3)*18,Max(B1:B25))) Regular expressions don't do well with nested parens (especially arbitrarily-nesting-depth such as are possible), so I'd suggest going for a full-blown parsing solution like pyparsing. If you have fair control over what can be contained in the formulas and you know they won't contain nested parens/functions, you might be able to formulate some sort of "kinda, sorta, maybe parses some forms of formulas" regexp. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Fwd: I would like to install Python on my 64 bit Win 7
-- Forwarded message -- From: aung paing Soe Date: Tue, Jan 5, 2010 at 11:27 AM Subject: I would like to install Python on my 64 bit Win 7 To: webmas...@python.org Hello , I would like to study about Python Programming . So I want to install Python . But my laptop is Window 7 64-bit home basic . So please give me a advice how to install Python in my 64 bit computer. I really want to study python programming . I am looking forward your reply. Thank you very much Yours, Beginner -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
On 5 ene, 20:05, Mensanator wrote: > On Jan 5, 12:35 pm, MRAB wrote: > > > > > vsoler wrote: > > > Hello, > > > > I am acessing an Excel file by means of Win 32 COM technology. > > > For a given cell, I am able to read its formula. I want to make a map > > > of how cells reference one another, how different sheets reference one > > > another, how workbooks reference one another, etc. > > > > Hence, I need to parse Excel formulas. Can I do it by means only of re > > > (regular expressions)? > > > > I know that for simple formulas such as "=3*A7+5" it is indeed > > > possible. What about complex for formulas that include functions, > > > sheet names and possibly other *.xls files? > > > > For example "=Book1!A5+8" should be parsed into ["=","Book1", "!", > > > "A5","+","8"] > > > > Can anybody help? Any suggestions? > > > Do you mean "how" or do you really mean "whether", ie, get a list of the > > other cells that are referred to by a certain cell, for example, > > "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5] > > Ok, although "Book1" would be the default name of a workbook, with > default > worksheets labeled "Sheet1". "Sheet2", etc. > > If I had a worksheet named "Sheety" that wanted to reference a cell on > "Sheetx" > OF THE SAME WORKBOOK, it would be =Sheet2!A7. If the reference was to > a completely > different workbook (say Book1 with worksheets labeled "Sheet1", > "Sheet2") then > the cell might have =[Book1]Sheet1!A7. > > And don't forget the $'s! You may see =[Book1]Sheet1!$A$7. Yes, Mensanator, but... what re should I use? I'm looking for the re statement. No doubt you can help! Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
Phlip wrote: > On Dec 31 2009, 2:06 pm, Steve Howell wrote: > >> Python is a truly awesome programming language. Not only is Guido a >> genius language designer, but he is also a great project leader. What >> an accomplishment. Congratulations to everybody who has contributed >> to Python in the last two decades! > > The more languages you learn before getting to Smalltalk, the more > awesome Smalltalk will be for you. > After implementing SmallTalk I more or less gave up OO programming for ten years, resuming it only after I met Python. SmallTalk didn't seem that awesome to me. Though for its time it was an incredible system, its insistence on a SmallTalk-only VM environment seemed a little solipsistic. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
vsoler wrote: On 5 ene, 19:35, MRAB wrote: vsoler wrote: Hello, I am acessing an Excel file by means of Win 32 COM technology. For a given cell, I am able to read its formula. I want to make a map of how cells reference one another, how different sheets reference one another, how workbooks reference one another, etc. Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? For example"=Book1!A5+8" should be parsed into ["=","Book1", "!", "A5","+","8"] Can anybody help? Any suggestions? Do you mean "how" or do you really mean "whether", ie, get a list of the other cells that are referred to by a certain cell, for example, "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5"]? I'd like to know how to do it, should it be possible. Something like this should work: references = re.findall(r"\b((?:\w+!)?[A-Za-z]+\d+)\b", formula) -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
On Tue, 05 Jan 2010 04:40:14 -0800, KvS wrote: >> Did you mean borderless printing? >> Every printer needs his margins, some more some less. Some printers have the >> ability to do borderless printing but usualy they can do it only on special >> or photo paper. So you can adjust the pdf as you wish, even with no margins, >> and then try to find under printer options "borderless printing". That is >> why I didn't understand :-)) it is a printer thing not pdf! > > As much as possible "borderless", yes. Of course the printer will > still apply some small margin, but that's ok. A margin of say <0.5 cm. > is fine. So it's not a printer thing, I accept the (physical) > limitations of the printer, but I want to avoid any extra margins due > to software settings. "Hardcopy" document formats such as PostScript and PDF use positions relative to the edges of the page, not the margins. -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Jens Müller wrote: > Hi and sorry for double posting - had mailer problems, > >> Terry said "queue". not "list". Use the Queue class (it's thread-safe) >> in the "Queue" module (assuming you're using Python 2.x; in Python 3.x >> it's called the "queue" module). > > Yes yes, I know. I use a queue to realize the thread pool queue, that > works all right. > > But each worker thread calculates a result and needs to make it > avaialable to the application in the main thread again. Therefore, it > appends its result to a common list. This seems works as well, but I was > thinking of possible conflict situations that maybe could happen when > two threads append their results to that same result list at the same > moment. > If you don't need to take anything off the list ever, just create a separate thread that reads items from an output Queue and appends them to the list. If you *do* take them off, then use a Queue. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the best data structure for a very simple spreadsheet?
On 3 ene, 22:40, mdipierro wrote: > Perhaps this can be useful:http://www.web2py.com/examples/spreadsheet > > The code is in a single file with not dependencies and it does not > require web2py to > run:http://code.google.com/p/web2py/source/browse/gluon/contrib/spreadshe... > > Here is a sample controller that shows you how to embed the > spreadsheet in web > page:http://code.google.com/p/web2py/source/browse/applications/examples/c... > > Massimo > > On Jan 3, 5:27 am, vsoler wrote: > > > Hi, > > > Not sure this is the best group to post, but I cannot think of any > > other. > > > My application would contain a limited set of "cells" represented by > > the instances of a Cell class: > > > class Cell: > > ... > > > A1=Cell(7) > > A2=Cell(2*A1) > > A3=Cell(3*A1+A2) > > A4=Cell(A3*4) > > > Of course, A1 = 7, A2 = 14, A3 = 35 and A4 = 140 > > > Now, I somehow want to be able to show a dependency tree > > > 1 level dependency trees > > A1: None > > A2: A1 > > A3: A1, A2 > > A4: A3 > > > All levels dependency trees > > > A1: None > > A2: A1 > > A3: A1, A2 > > A4: A3, A2, A1 > > > Leaf + values dependency trees: > > > A1: 7 > > A2: A1=7, 2 > > A3: 3, A1=7, 2 > > A4: 3, A1=7, 2, 4 > > > What I'd like to know is: > > > 1) what are, in your opinion, the basic elements of the Cell class? > > 2) Do I need a parser to evaluate the formulas like “3*A1+A2”? Can you > > recommend one library that already contains one? > > 3) Do I need a tree data structure to represent my data? would the > > tree be an attribute of the class instance? > > > I imagine a lot can be said on these questions. What I am looking for > > is some hints that help me get out of where I am now. > > > Any help is highly appreciated. > > > Vicente Soler > > There is something that I appreciate in this group, and it is the high degree of knowledge of all the participants that are helping with their answers. After studying your suggestions, I'll come back to you. Thank you very much. Vicente Soler -- http://mail.python.org/mailman/listinfo/python-list
Re: fsync() doesn't work as advertised?
On Mon, 04 Jan 2010 08:09:56 -0800, Brian D wrote: > If I'm running a process in a loop that runs for a long time, I > occasionally would like to look at a log to see how it's going. > > I know about the logging module, and may yet decide to use that. > > Still, I'm troubled by how fsync() doesn't seem to work as advertised: > > http://docs.python.org/library/os.html > > "If you’re starting with a Python file object f, first do f.flush(), > and then do os.fsync(f.fileno())" The .flush() method (and the C fflush() function) causes the contents of application buffers to be sent to the OS, which basically copies the data into the OS-level buffers. fsync() causes the OS-level buffers to be written to the physical drive. File operations normally use the OS-level buffers; e.g. if one process write()s to a file and another process read()s it, the latter will see what the former has written regardless of whether the data has been written to the drive. The main reason for using fsync() is to prevent important data from being lost in the event of an unexpected reboot or power-cycle (an expected reboot via the "shutdown" or "halt" commands will flush all OS-level buffers to the drive first). Other than that, fsync() is almost invisible (I say "almost", as there are mechanisms to bypass the OS-level buffers, e.g. the O_DIRECT open() flag). -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
On Jan 5, 12:35 pm, MRAB wrote: > vsoler wrote: > > Hello, > > > I am acessing an Excel file by means of Win 32 COM technology. > > For a given cell, I am able to read its formula. I want to make a map > > of how cells reference one another, how different sheets reference one > > another, how workbooks reference one another, etc. > > > Hence, I need to parse Excel formulas. Can I do it by means only of re > > (regular expressions)? > > > I know that for simple formulas such as "=3*A7+5" it is indeed > > possible. What about complex for formulas that include functions, > > sheet names and possibly other *.xls files? > > > For example "=Book1!A5+8" should be parsed into ["=","Book1", "!", > > "A5","+","8"] > > > Can anybody help? Any suggestions? > > Do you mean "how" or do you really mean "whether", ie, get a list of the > other cells that are referred to by a certain cell, for example, > "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5] Ok, although "Book1" would be the default name of a workbook, with default worksheets labeled "Sheet1". "Sheet2", etc. If I had a worksheet named "Sheety" that wanted to reference a cell on "Sheetx" OF THE SAME WORKBOOK, it would be =Sheet2!A7. If the reference was to a completely different workbook (say Book1 with worksheets labeled "Sheet1", "Sheet2") then the cell might have =[Book1]Sheet1!A7. And don't forget the $'s! You may see =[Book1]Sheet1!$A$7. -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
On Tue, 05 Jan 2010 13:12:00 -0500, vsoler wrote: Hello, I am acessing an Excel file by means of Win 32 COM technology. For a given cell, I am able to read its formula. I want to make a map of how cells reference one another, how different sheets reference one another, how workbooks reference one another, etc. Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? For example"=Book1!A5+8" should be parsed into ["=","Book1", "!", "A5","+","8"] Can anybody help? Any suggestions? It seems like you want to recreate data structures that Excel, itself, must maintain in order to recalculate cells in the correct order. As long as you're using COM, you might be able to tap into those data structures. My 15-year-old (!) "Using Excel Visual Basic for Applications" book wasn't any help. :-( After a short Google session, I came up with one possible lead: http://www.decisionmodels.com/ Good luck! John -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
On 5 ene, 19:35, MRAB wrote: > vsoler wrote: > > Hello, > > > I am acessing an Excel file by means of Win 32 COM technology. > > For a given cell, I am able to read its formula. I want to make a map > > of how cells reference one another, how different sheets reference one > > another, how workbooks reference one another, etc. > > > Hence, I need to parse Excel formulas. Can I do it by means only of re > > (regular expressions)? > > > I know that for simple formulas such as "=3*A7+5" it is indeed > > possible. What about complex for formulas that include functions, > > sheet names and possibly other *.xls files? > > > For example "=Book1!A5+8" should be parsed into ["=","Book1", "!", > > "A5","+","8"] > > > Can anybody help? Any suggestions? > > Do you mean "how" or do you really mean "whether", ie, get a list of the > other cells that are referred to by a certain cell, for example, > "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5"]? I'd like to know how to do it, should it be possible. Vicente -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
On Dec 31 2009, 2:06 pm, Steve Howell wrote: > Python is a truly awesome programming language. Not only is Guido a > genius language designer, but he is also a great project leader. What > an accomplishment. Congratulations to everybody who has contributed > to Python in the last two decades! The more languages you learn before getting to Smalltalk, the more awesome Smalltalk will be for you. -- Phlip -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Mon, 04 Jan 2010 21:30:31 -0800, cassiope wrote: > One more tidbit observed: my last note, that it works when using > seteuid/setegid? > Well - that only applies if the daemon is running under strace (!). > It fails > if started directly by root, or if the strace session has ended, > leaving the > main body of the daemon running in its normal headless manner. > > I wonder if running under "strace -f" - might setegid/seteuid be > prevented from > having their normal effect? Possibly. The ptrace() syscall on which strace depends will fail if you try to trace a "privileged" process and you aren't root, so it's possible that a ptrace()d process will refuse to become privileged. Here, "privileged" includes a process which has changed any of its UIDs or GIDs (this prevents a normal user from tracing, killing, etc an otherwise privileged process which has switched to the user's UID for the time being). -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Hi and sorry for double posting - had mailer problems, Terry said "queue". not "list". Use the Queue class (it's thread-safe) in the "Queue" module (assuming you're using Python 2.x; in Python 3.x it's called the "queue" module). Yes yes, I know. I use a queue to realize the thread pool queue, that works all right. But each worker thread calculates a result and needs to make it avaialable to the application in the main thread again. Therefore, it appends its result to a common list. This seems works as well, but I was thinking of possible conflict situations that maybe could happen when two threads append their results to that same result list at the same moment. Regards, Jens -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing an Excel formula with the re module
vsoler wrote: Hello, I am acessing an Excel file by means of Win 32 COM technology. For a given cell, I am able to read its formula. I want to make a map of how cells reference one another, how different sheets reference one another, how workbooks reference one another, etc. Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? For example"=Book1!A5+8" should be parsed into ["=","Book1", "!", "A5","+","8"] Can anybody help? Any suggestions? Do you mean "how" or do you really mean "whether", ie, get a list of the other cells that are referred to by a certain cell, for example, "=3*A7+5" should give ["A7"] and "=Book1!A5+8" should give ["Book1!A5"]? -- http://mail.python.org/mailman/listinfo/python-list
it gets worse (was: How do you configure IDLE on a Mac...)
On Jan 5, 12:32 am, Ned Deily wrote: > In article > <0d70cb54-3d77-4176-b621-e764ecf61...@26g2000yqo.googlegroups.com>, > > > > > > Mensanator wrote: > > I assume I've been using the IDLE from macports. From the command > > prompt I've > > been typing "idle". This launches a "shell" window which appears to > > have an X11 > > parent application for which there are no "preferences" applicable to > > fonts. > > > However, if I use the quick launcher from the python.org, I get a > > "shell" whose > > parent is named "IDLE"! And that one has a completely different > > preferences, > > one similar the the Windows Configure which allows me to set the font! > > > Now, if I close this shell and start IDLE from the command line again, > > I still > > get a "shell" with an X11 parent, but, lo and behold, the font has > > changed to > > what I had previously set with the IDLE parent. > > > Course, I can't import gmpy, cause the python.org version can't > > compile it, so I > > still have to use the macports install of 3.1, but that's ok, once I > > use > > the IDLE application to set the preferences, I can switch back to the > > X11 version and the preferences will follow. > > The prefs follow because all versions of IDLE use the same (unversioned) > directory for configuration files, ~/.idlerc/. In particular, the > configuration file ~/.idlerc/config-main.cfg contains, among other > things, any changes to the default font. So, if you're successful at > changing it in one version of IDLE, it will likely affect all versions > you have. Note the file is a simple ini format: > > [EditorWindow] > font = monaco > > so you can edit it by hand. Good to know. But, as the subject says... > > BTW, the python.org IDLEs and the Apple-supplied IDLEs use the > system-supplied Aqua (aka Quartz) Tk not the X11 one that MacPorts > builds by default. The MacPorts Tk port does have a "quartz" variant > but that doesn't yet work in 64-bit mode. So, for all practical purposes, the macports install is broken also. IDLE simply does not work in an X11 window (you think someone would have noticed that). The missing preferences is just the beginning. Apparently NONE of the menu item shortcuts work. For example, the Cut, Copy, Paste shortcuts are given as Command-X, Command-C and Command-V. But that doesn't work in an X11 window, apperently only in an Aqua Tk (parent application appears as IDLE). Of course, I can do Control-X, Control-C and Control-V to do Cut, Copy and Paste. Don't know if this works for all shortcuts, but I suppose I could just pick them from the menu (and I can bang my head against the wall while I'm at it). What do you think, suppose I copy the gmpy built with the macports install over to the directory where the python.org version is? Would it import? If that'll work, I can switch back to using the python.org install and use it's version of IDLE. I certainly won't be needing distutils once I have a working version of gmpy. > > -- > Ned Deily, > n...@acm.org- Hide quoted text - > > - Show quoted text - -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman wrote: > KvS wrote: > > ... can I adjust the options normally appearing in > > the Printing Dialog through Python? > > Yes, if you use my method or my module, as I gave in my previous post. > If you use Adobe Reader to print, I'm not sure how to automate the print > settings. Sorry, one more. I completely forgot it's not exactly plain text, but occasionally also a limited number of non-ASCII characters (accents in names etc.). Would this be possible through your method? -- http://mail.python.org/mailman/listinfo/python-list
parsing an Excel formula with the re module
Hello, I am acessing an Excel file by means of Win 32 COM technology. For a given cell, I am able to read its formula. I want to make a map of how cells reference one another, how different sheets reference one another, how workbooks reference one another, etc. Hence, I need to parse Excel formulas. Can I do it by means only of re (regular expressions)? I know that for simple formulas such as "=3*A7+5" it is indeed possible. What about complex for formulas that include functions, sheet names and possibly other *.xls files? For example"=Book1!A5+8" should be parsed into ["=","Book1", "!", "A5","+","8"] Can anybody help? Any suggestions? Vicente Soler -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic text color
On Tue, 05 Jan 2010 10:31:09 -0500, Dave McCormick wrote: ... But this is what I have so far. ## file = 'red.txt' file = open("red.txt","r") rList = file.readlines() file.close() redList = str(rList).split() Dave, you're doing exactly the right thing: gradually expanding your program, to provide more functionality and to learn more about the available programming tools. It's also very good that you take care to close() the file after processing it. Now for the bad news ... 1. Don't use "file" as a variable name -- it's a built-in object type. (Some people don't like the fact that Python allows you to redefine such "reserved words".) 2. It's probably not the best idea to use a single variable (you use "file") to do double-duty: to hold the name of a file, and to hold the open-file object returned by the open() function. It's perfectly legal, but it hides information that might be useful when you're debugging a program. This is better: fname = 'red.txt' inpf = open(fname, "r") 3. It might be better to use read() rather than readlines() to process the "red.txt" file. It depends on what that file is supposed to contain. For example, if you expect "red.txt" to contain exactly one line, which has one or more words, you can process the open-file object like this: file_contents = inpf.read() redList = file_contents.split() ... or ... redList = inpf.read().split() It's certainly a mistake to use the expression "str(rList).split()". Using str() to convert the list "rList" into a string creates a mess that includes square-bracket characters. Did this actually work for you? Best, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
D'Arcy J.M. Cain wrote: > On 05 Jan 2010 14:02:50 GMT > Steven D'Aprano wrote: >> shouldn't use assert for validating user data except for quick-and-dirty >> scripts you intend to use once and throw away. > > A mythcial beast that has yet to be spotted in the wild. > Not true (he wrote, picking nits). Such programs are written all the time. The fact that they invariably get used more often than intended doesn't negate the intentions of the author. ;-) regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Le Tue, 05 Jan 2010 15:04:56 +0100, Jens Müller a écrit : > > Is a list thrad-safe or do I need to lock when adding the results of my > worker threads to a list? The order of the elements in the list does not > matter. The built-in list type is thread-safe, but is doesn't provide the waiting features that queue.Queue provides. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On 1/6/2010 1:48 AM, r0g wrote: Steven D'Aprano wrote: On Tue, 05 Jan 2010 13:06:20 +, r0g wrote: If that's the case how can you expect it to validate anything at all in production? The asserts still operate so long as you don't use the -O switch. Do you mean for debugging in situ or something? Could you maybe give me an example scenario to illustrate your point? There are at least two sorts of validation that you will generally need to perform: validating user data, and validating your program logic. Cool, that's what I thought i.e. you can't rely on asserts being there so don't use them for anything critical but it's still a good idea to use them for logic/consistency checking in production code as, should you be running your production code unoptimised, it might catch something you'd otherwise miss. Steven described the traditional approach to using assertions; another approach to when to use assertion is the one inspired by Design-by-Contract paradigm. DbC extends the traditional approach by focusing on writing a contract (instead of writing assertions) and generating assertions[1] to validate the contract. Just like assertions, these contracts are meant to be removed in production releases. In Design-by-Contract, only codes that interacts with the outer-world (e.g. getting user/file/network input, etc) need to do any sort of validations. Codes that doesn't interact directly with outside world only need to have a "contract" and simplified by *not* needing argument checking, since the function relies on the caller obeying the contract[2] and never calling it with an invalid input. DbC uses assertions[1] spuriously, unlike the traditional approach which is much more conservative when using assertions. [1] or explicit language support which is just syntax sugar for assertions [2] of course, on a debug release, the contract validation code will still be enforced to catch logic/consistency bugs that causes the violation -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Jens Müller wrote: Hello, The fairly obvious thing to do is use a queue.queue for tasks and another for results and a pool of threads that read, fetch, and write. Thanks, indeed. Is a list thrad-safe or do I need to lock when adding the results of my worker threads to a list? The order of the elements in the list does not matter. Terry said "queue". not "list". Use the Queue class (it's thread-safe) in the "Queue" module (assuming you're using Python 2.x; in Python 3.x it's called the "queue" module). -- http://mail.python.org/mailman/listinfo/python-list
Re: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?
On Jan 5, 12:16 am, Stefan Behnel wrote: > Note that there are tons of ways to generate HTML with Python. Forgot to note - I'm generating schematic XML, and I'm trying to find a way better than the Django template I started with! -- http://mail.python.org/mailman/listinfo/python-list
Re: A null program - what is it doing?
No doubt a dumb question from a noob: The following program (a cut down version of some test code) uses no CPU, and does not terminate: import sys from PyQt4.QtCore import * if __name__=="__main__": app = QCoreApplication(sys.argv) sys.exit(app.exec_()) What is the program doing? What is the correct way to terminate the execution? Are you trying to understand QCoreApplication()? I can't help you there, since I've never used it. If you're just trying to get started with PyQt, use QApplication() instead: import sys from PyQt4.QtCore import * from PyQt4.QtGui import * if __name__=="__main__": app = QApplication(sys.argv) window = QLabel("I'm a PyQt window") window.show() sys.exit(app.exec_()) To terminate execution, just close the window by clicking the "X" in the window banner. HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman wrote: > KvS wrote: > > ... can I adjust the options normally appearing in > > the Printing Dialog through Python? > > Yes, if you use my method or my module, as I gave in my previous post. > If you use Adobe Reader to print, I'm not sure how to automate the print > settings. Ok, actually I quite like being able to print straightforward through your code, i.e. without any extra modules installed. I understand that sending text to the printer is in principle as simple as dc.TextOut(scale_factor * 72, -1 * scale_factor * 72, "Testing...") I didn't see you do anything with adjusting margins in the code. Does that mean that if I would e.g. do dc.TextOut(0, 0, "Testing...") the printout would appear in the upper left corner of the paper, as close to the edges as the printer is capable of? (Sorry, but I only have Ubuntu available at the moment, no Windows). -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman wrote: > KvS wrote: > > ... can I adjust the options normally appearing in > > the Printing Dialog through Python? > > Yes, if you use my method or my module, as I gave in my previous post. > If you use Adobe Reader to print, I'm not sure how to automate the print > settings. Thanks Chris, I'll go on and have a look. -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Hello, The fairly obvious thing to do is use a queue.queue for tasks and another for results and a pool of threads that read, fetch, and write. Thanks, indeed. Is a list thrad-safe or do I need to lock when adding the results of my worker threads to a list? The order of the elements in the list does not matter. Jens -- http://mail.python.org/mailman/listinfo/python-list
Re: Speeding up network access: threading?
Hello, The fairly obvious thing to do is use a queue.queue for tasks and another for results and a pool of threads that read, fetch, and write. Thanks, indeed. Is a list thrad-safe or do I need to lock when adding the results of my worker threads to a list? The order of the elements in the list does not matter. Jens -- http://mail.python.org/mailman/listinfo/python-list
Re: embedded python on mac - linking problem
On Tue, Jan 5, 2010 at 7:33 AM, Krzysztof Kobus wrote: > Hi, > > I have a problem with linking python module with my application on mac in > order to make the module available in "embedded python". > > My python module is contained in j3kmodule.cxx file and module > initialization function is exported in j3kmodule.h > > j3kmodule.h: > > PyMODINIT_FUNC PyInit_j3k(void); > > > j3kmodule.cxx: > -- > PyMODINIT_FUNC > PyInit_j3k(void) > { > PyObject *m = NULL; > > if ((m = PyModule_Create(&j3k_module)) == NULL) > return NULL; > > return m; > } > > > Then in my application in KkPython.cxx file I have: > > KkPython.cxx: > - > > #include "j3kmodule.h" > > /* Add a builtin module, before Py_Initialize */ > PyImport_AppendInittab("j3k", PyInit_j3k); > > > I link my application with the module and get following linking error on mac > although on open suse linux exactly the same procedure works fine: > > g++ -headerpad_max_install_names -o > ../../bin/render.app/Contents/MacOS/render main.o KkPython.o -lkkbase > -L/Users/kk/dev/J3K/trunk/j3ksrc/examples/render/../../lib/ > -lQtSolutions_PropertyBrowser-2.5 -lpython3.1 > /Users/kk/dev/J3K/trunk/j3ksrc/python/modules/build/lib.macosx-10.3-i386-3.1/j3k.so > -framework OpenGL -framework AGL > ld: warning in > /Users/kk/dev/J3K/trunk/j3ksrc/python/modules/build/lib.macosx-10.3-i386-3.1/j3k.so, > file is not of required architecture > Undefined symbols: > "_PyInit_j3k", referenced from: > _PyInit_j3k$non_lazy_ptr in KkPython.o > ld: symbol(s) not found > collect2: ld returned 1 exit status > make: *** [../../bin/render.app/Contents/MacOS/render] Error 1 > > > I appreciate any hints, > > best greetings, > > Krzysztof Kobus > Well, it seems that one of your files is a different architecture than the others. Based on the location, I'd say it's i386 while the rest of it would be PowerPC. You can cross-compile but you can't link an i386 library to a PowerPC library. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
r0g wrote: Dave Angel wrote: r0g wrote: Maybe, although I recently learned on here that one can't rely on assert statements in production code, their intended use is to aid debugging and testing really. Hopefully, what you learned is that you can't use assert() in production code to validate user data. It's fine to use it to validate program logic, because that shouldn't still need testing in production. DaveA Well maybe I didn't quite get it then, could you explain a bit further? My understanding was that asserts aren't executed at all if python is started with the -O or -OO option, or run through an optimizer. If that's the case how can you expect it to validate anything at all in production? Do you mean for debugging in situ or something? Could you maybe give me an example scenario to illustrate your point? Cheers, Roger. You understand the -O and -OO options fine. But the point is that you should not use assert() for anything that will be properly debugged before going to the user. You use if statements, and throw's to catch the error, and print to stderr, or GUI dialog boxes, or whatever mechanism you use to tell your user. But those errors are ones caused by his data, not by your buggy code. And the message tells him what's wrong with his data, not that you encountered a negative value for some low level function. I agree with Steve's pessimistic view of the state of most released software. But if you view a particular internal check as useful for production, then it should be coded in another mechanism, not in assert. Go ahead and write one, with a UI that's appropriate for your particular application. But it should do a lot more than assert does, including telling the user your contact information to call for support. def production_assert(expression, message): if not expression: dialog_box("Serious internal bug, call NNN-NNN- immediately", message) For an overly simplified example showing a user validation, and an assert : import sys def main(): try: text = raw_input("Enter your age, between 1 and 22 ") age = int(text) except ValueError, e: age = -1 if not 1 <= age <= 22: #not an assert print "Age must be between 1 and 22" print "Run program again" sys.exit(2) grade = calc_grade(age) print "Your grade is probably", grade table = [0, 0, 0, 0, 0, "K", "First", "2nd", 3] def calc_grade(age): """ calculate a probable grade value, given an i2nteger age between 1 and 22, inclusive """ assert(1 <= age <= len(table)) grade = table[age]#assume I have a fixed-length table for this return grade main() Note a few things. One I have a bug, in that the table isn't as big as the limit I'm checking for. With defensive coding, I'd have another assert for that, or even have the table size be available as a global constant (all uppers) so that everyone's in synch on the upper limit. But in any case, the test suite would be checking to make sure the code worked for 1, for 22, for a couple of values in between, and that a proper error response happened when a non-integer was entered, or one outside of the range. That all happens in separate code, not something in this file. And the test suite is run after every change to the sources, and certainly before release to production. Next, see the docstring. It establishes a precondition for the function. Since the function is called only by me (not the user), any preconditions can be checked with an assert. An assert without a supporting comment (or docstring) is almost worthless. And finally, notice that I check the user's input *before* passing it on to any uncontrolled code. So any asserts after that cannot fire, unless I have a bug which was not caught during testing. All opinions my own, of course. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic text color
John Posner wrote: On Fri, 01 Jan 2010 21:01:04 -0500, Cousin Stanley wrote: I was not familiar with the re.finditer method for searching strings ... Stanley and Dave -- So far, we've just been using finditer() to perform standard-string searches (e.g. on the word "red"). Since Dave now wants to color multiple words the same color (e.g. the words in redList), we can use a single regular-expression search to locate *all* the words in a list. This eliminates the need to use a "for" loop to handle the list. Here's what I mean: >>> import re >>> s = "it is neither red nor crimson, but scarlet, you see" ## individual searches >>> [matchobj.span() for matchobj in re.finditer("red", s)] [(14, 17)] >>> [matchobj.span() for matchobj in re.finditer("crimson", s)] [(22, 29)] >>> [matchobj.span() for matchobj in re.finditer("scarlet", s)] [(35, 42)] ## one "swell foop" >>> redList = "red crimson scarlet".split() >>> redList_regexp = "|".join(redList) >>> redList_regexp 'red|crimson|scarlet' >>> [matchobj.span() for matchobj in re.finditer(redList_regexp, s)] [(14, 17), (22, 29), (35, 42)] -John Thanks again John, This is fun!!! I made a "red.text" file to hold the "red words", they are separated by a space in the file. Still need to add the extra parameter "color" someplace. But this is what I have so far. ## file = 'red.txt' file = open("red.txt","r") rList = file.readlines() file.close() redList = str(rList).split() blueList = "blue ball".split() greenList = "green grass".split() def get_complete_text(event): complete_text = Tbox.get("1.0", END) Tbox.tag_remove("red", "1.0", END) Tbox.tag_remove("blue", "1.0", END) Tbox.tag_remove("green", "1.0", END) RED redList_regexp = "|".join(redList) for matchobj in re.finditer(redList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("red", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("red", foreground="red") BLUE### blueList_regexp = "|".join(blueList) for matchobj in re.finditer(blueList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("blue", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("blue", foreground="blue") GREEN### greenList_regexp = "|".join(greenList) for matchobj in re.finditer(greenList_regexp, complete_text): start,end = matchobj.span() Tbox.tag_add("green", "1.0 + %d chars" % start,"1.0 + %d chars" % end) Tbox.tag_config("green", foreground="green") -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes: How to call unexported functions in a dll
Am 05.01.2010 12:19, schrieb Coert Klaver (DT): > Hi, > > I am using ctypes in python 3 on a WXP machine > > Loading a dll and using its exported functions works fine. > > Now I want to use a function in the dll that is not exported. > > In C this can be done by just casting the address in the dll of that > function to an apropriate function pointer and call it (you need to be > sure about the address). Can a similar thing be done directly with > ctypes? > > A work around I see is writing in wrapper dll in c that loads the main > dll, exports a function that calls the unexported function in the main > dll, but I don't find that an elegant solution. No need for a workaround. One solution is to first create a prototype for the function by calling WINFUNCTYPE or CFUNCTYPE, depending on the calling convention: stdcall or cdecl, then call the prototype with the address of the dll function which will return a Python callable. Demonstrated by the following script using GetProcAddress to get the address of the GetModuleHandleA win32 api function: >>> from ctypes import * >>> dll = windll.kernel32 >>> addr = dll.GetProcAddress(dll._handle, "GetModuleHandleA") >>> print hex(addr) 0x7c80b741 >>> proto = WINFUNCTYPE(c_int, c_char_p) >>> func = proto(addr) >>> func >>> func(None) 486539264 >>> hex(func(None)) '0x1d00' >>> hex(func("python24.dll")) '0x1e00' >>> hex(func("python.exe")) '0x1d00' >>> Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Steven D'Aprano wrote: > On Tue, 05 Jan 2010 13:06:20 +, r0g wrote: >> Well maybe I didn't quite get it then, could you explain a bit further? >> >> My understanding was that asserts aren't executed at all if python is >> started with the -O or -OO option, > > Correct. > > >> or run through an optimizer. > > I don't know what you mean by that. I've never used them but I heard there are optimizers for python (psycho?). I assumed these would do everythin -O does and more, including losing the asserts. > >> If >> that's the case how can you expect it to validate anything at all in >> production? > > The asserts still operate so long as you don't use the -O switch. > >> Do you mean for debugging in situ or something? Could you >> maybe give me an example scenario to illustrate your point? > > > There are at least two sorts of validation that you will generally need > to perform: validating user data, and validating your program logic. > Cool, that's what I thought i.e. you can't rely on asserts being there so don't use them for anything critical but it's still a good idea to use them for logic/consistency checking in production code as, should you be running your production code unoptimised, it might catch something you'd otherwise miss. Thanks for responding is such detail :) Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
Stick your English into your ass -- http://mail.python.org/mailman/listinfo/python-list