The Python: Rag, January issue available
The January issue of The Python: Rag is available at: http://www.pythonrag.org or http://groups.google.co.uk/group/pythonrag A monthly, free, community run, Python magazine in pdf format, intended for anyone interested in Python. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
PyCon early-bird ends TOMORROW (Jan 6)
PyCon early-bird registration discounts end TOMORROW, Jan. 6. The time for raw panic has arrived. Run into the nearest open public place. Wave your arms in the air, tear your hair, and scream wildly. Sobbing hysterically is a nice touch. Or, if all that sounds too tiring for you, just stop by http://us.pycon.org/2010/registration/ and register. After tomorrow, rates will go from wonderfully-cheap $450/300/400 (corporate/individual/student) to still-pretty-darn-cheap $600/350/225. Remind your friends! See you in Atlanta! -- - Catherine http://catherinedevlin.blogspot.com/ *** PyCon * Feb 17-25, 2010 * Atlanta, GA * us.pycon.org *** -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
EPD 6.0 released
Hello, I am pleased to announce that EPD (Enthought Python Distribution) version 6.0 has been released. This is the first EPD release which is based on Python 2.6, and 64-bit Windows and MacOSX support is also available now. You may find more information about EPD, as well as download a 30 day free trial, here: http://www.enthought.com/products/epd.php You can find a complete list of updates in the change log: http://www.enthought.com/EPDChangelog.html About EPD - The Enthought Python Distribution (EPD) is a kitchen-sink-included distribution of the Python Programming Language, including over 75 additional tools and libraries. The EPD bundle includes NumPy, SciPy, IPython, 2D and 3D visualization, and many other tools. http://www.enthought.com/products/epdlibraries.php It is currently available as a single-click installer for Windows XP, Vista and 7, MacOS (10.5 and 10.6), RedHat 3, 4 and 5, as well as Solaris 10 (x86 and x86_64/amd64 on all platforms). EPD is free for academic use. An annual subscription including installation support is available for individual and commercial use. Additional support options, including customization, bug fixes and training classes are also available: http://www.enthought.com/products/support_level_table.php - Ilan -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?
Phlip, 04.01.2010 21:01: Not Hyp: I hope I'm wrong, but seems that DOMBuilder, found among the various xml.dom packages, cannot build DOM like this: var html = DomBuilder.apply(); var form = html.FORM( html.DIV( html.INPUT({type : 'text', name : 'email'}), html.INPUT({type : 'text', name : 'password'}), html.INPUT({type : 'submit'}), ) ); Do anyone know any good DOM builder packages that do build DOM good like a DOM builder should? You might be looking for something like this: http://codespeak.net/lxml/lxmlhtml.html#creating-html-with-the-e-factory Note that there are tons of ways to generate HTML with Python. A quick web search (or a quick read on PyPI or the Python Wiki) should get you started. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python] Printing plain text with exact positioning on Windows
KvS wrote: So now I'm looking for other ways to get this printing job done. I know of Tim Goldens page about Windows printing: http://timgolden.me.uk/python/win32_how_do_i/print.html and have been googling a lot, but I'm still not convinced what the best way to go is. E.g. I can't get clear whether the combination reportlab Acrobat Reader allows to print very close to the left and right edge of the paper (without a user having to change something in a Print dialog etc.). I have a page, and a module, for Windows printing which gives a lot of control. http://newcenturycomputers.net/projects/pythonicwindowsprinting.html But in your case, I think I'd consider ReportLab. Also, it seems to me that I've seen a simpler PDF generator module for Python. Dunno where, though. -- Chris. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, 05 Jan 2010 02:31:34 +, r0g wrote: A pattern I have used a few times is that of returning an explicit success/failure code alongside whatever the function normally returns. That doesn't work for languages that can only return a single result, e.g. C or Pascal. You can fake it by creating a struct that contains a flag and the result you want, but that means doubling the number of data types you deal with. While subsequent programmers might not intuit the need to test for (implicit) magic return values they ought to notice if they start getting tuples back where they expected scalars... What if they're expecting tuples as the result? def foo(x) if x0: return True, x*x else: return False, Bad value of x in foo:,str(x) ok, value = foo(-1) Oops, that gives: ValueError: too many values to unpack because you've returned three items instead of two. When an idiom is easy to get wrong, it's time to think hard about it. if ok: print foo of x is, value else: print ERROR:, value Whenever I come across a function that returns a flag and a result, I never know whether the flag comes first or second. Should I write: flag, result = foo(x) or result, flag = foo(x) I've seen APIs that do both. And I never know if the flag should be interpreted as a success or a failure. Should I write: ok, result = foo(x) if ok: process(result) else: fail() or err, result = foo(x) if err: fail() else: process(result) Again, I've seen APIs that do both. And if the flag indicates failure, what should go into result? An error code? An error message? That's impossible for statically-typed languages, unless they have variant records or the function normally returns a string. And even if you dismiss all those concerns, it still hurts readability by obfuscating the code. Consider somebody who wants to do this: result = foo(bar(x)) but instead has to do this: flag, result = bar(x) if flag: # I think this means success flag, result = foo(x) # oops, I meant result Again, it's error-prone and messy. Imagine writing: flag, a = sin(x) if flag: flag, b = sqrt(x) if flag: flag, c = cos(b) if flag: flag, d = exp(a + c) if flag: flag, e = log(x) if flag: # Finally, the result we want!!! flag, y = d/e if not flag: fail(y) else: fail(e) else: fail(d) else: fail(c) else: fail(b) else: fail(a) Compare that to the way with exceptions: y = exp(sin(x) + cos(sqrt(x)))/log(x) Which would you prefer? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
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? Thanks in advance for educating me. -- http://mail.python.org/mailman/listinfo/python-list
Re: python xmlrpc client with ssl client certificates and standard modules
Hi Martin, Thanks a lot for your reply. It helped me to find the correct solution. Unfortunaltely xmlrpclib.ServerProxy does not allow a host tuple, but just a uri. So the simplest solution, that I found is to create a custom transport import xmlrpclib class SafeTransportWithCert(xmlrpclib.SafeTransport): __cert_file = DFLT_CERTFILE __key_file = DFLT_KEYFILE def make_connection(self,host): host_with_cert = (host, { 'key_file' : self.__key_file, 'cert_file' : self.__cert_file } ) return \ xmlrpclib.SafeTransport.make_connection( self,host_with_cert) transport = SafeTransportWithCert() server = xmlrpclib.ServerProxy(server_url, transport = transport) rslt = server.mymethod(args) Perfect. Now the server can ensure, that only certified clients connect. My next task is how to find out at the client side, that the server certificate is a properly signed one. bye N Martin v. Loewis wrote: I can do xmlrpc over ssl WITHOUT certificates with following code: import xmlrpclib server_url = 'https://myserver' server = xmlrpclib.Server(server_url); and I can perform a https get request WITH certificates with below snippet: import httplib conn = httplib.HTTPSConnection( HOSTNAME, key_file = KEYFILE, cert_file = CERTFILE ) conn.putrequest('GET', '/') conn.endheaders() response = conn.getresponse() print response.read() I'm just lost of how to 'combine' both. In this case, read through the source of xmlrpclib: a) SafeTransport receives x509 parameters from get_host_info b) get_host_info supports a case where host is a tuple host, x509 So, without testing: server = xmlrpclib.Server((server_url, {'key_file': KEYFILE, 'cert_file': CERTFILE})) Please do read the code before trying this out. HTH, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Lie Ryan wrote: On 1/5/2010 1:31 PM, r0g wrote: Michi wrote: On Jan 4, 1:30 pm, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: snip A pattern I have used a few times is that of returning an explicit success/failure code alongside whatever the function normally returns. While subsequent programmers might not intuit the need to test for (implicit) magic return values they ought to notice if they start getting tuples back where they expected scalars... def foo(x) if x0: return True, x*x else: return False, Bad value of x in foo:,str(x) ok, value = foo(-1) if ok: print foo of x is, value else: print ERROR:, value Except that that is a reinvention of try-wheel: True, but there's more than one way to skin a cat! Mine's faster if you expect a high rate of failures (over 15%). def foo(x): if x 0: return x*x else: raise MathError(Bad value of x in foo: %s % x) try: print foo(-1) except MathError, e: print ERROR: System integrity is doubted or rather; that is perhaps a good example of when to use 'assert'. If the domain of foo() is positive integers, calling -1 on foo is a bug in the caller, not foo(). 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. Besides, that was just a toy example. I have been looking at Haskell recently and the way the pure functional language handled exceptions and I/O gives me a new distinct insight that exceptions can be thought of as a special return value that is implicitly wrapped and unwrapped up the call stack until it is explicitly handled. Yes there's some very interesting paradigms coming out of functional programming but, unless you're a maths major, functional languages are a long way off being productivity tools! Elegant: yes, provable: maybe, practical for everyday coding: not by a long shot! Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
much snippage On Tue, Jan 5, 2010 at 1:07 AM, r0g aioe@technicalbloke.com wrote: Lie Ryan wrote: I have been looking at Haskell recently and the way the pure functional language handled exceptions and I/O gives me a new distinct insight that exceptions can be thought of as a special return value that is implicitly wrapped and unwrapped up the call stack until it is explicitly handled. Yes there's some very interesting paradigms coming out of functional programming but, unless you're a maths major, functional languages are a long way off being productivity tools! Elegant: yes, provable: maybe, practical for everyday coding: not by a long shot! Methinks the authors of Real World Haskell (excellent read btw) have a bone to pick with you. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
lxml 2.2.4 on python3.1, Windows XP gives importerror
Dear All I have Python 3.1 installed on Windows XP and Works nice. I downloaded lxml 2.2.4 (lxml-2.2.4.win32-py3.1.exe) from pypi. When I try: from lxml import etree I get: ImportError: DLL load failed: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. For information: 'import lxml' works fine. After reinstalling python3.1 also the error message is the same. Any help is appreciated! Regards, Ashish Vyas -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficiently determine where documents differ
On Jan 5, 9:46 am, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Mon, 04 Jan 2010 19:04:12 -0300, Richard richar...@gmail.com escribió: I have been using the difflib library to find where 2 large HTML documents differ. The Differ().compare() method does this, but it is very slow - atleast 100x slower than the unix diff command. Differ compares sequences of lines *and* lines as sequences of characters to provide intra-line differences. The diff command only processes lines. If you aren't interested in intra-line differences, use a SequenceMatcher instead. Or, invoke the diff command using subprocess.Popen + communicate. -- Gabriel Genellina thank you very much Gabriel! Passing a list of the document lines makes the efficiency comparable to the diff command. Richard -- 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 02:31:34 +, r0g wrote: A pattern I have used a few times is that of returning an explicit success/failure code alongside whatever the function normally returns. That doesn't work for languages that can only return a single result, e.g. C or Pascal. You can fake it by creating a struct that contains a flag and the result you want, but that means doubling the number of data types you deal with. No, but that's why I try not to use languages where you can only return a single result, I always found that an arbitrary and annoying constraint to have. I leads to ugly practices like magic return values in C or explicitly packing things into hashtables like PHP, yuk! While subsequent programmers might not intuit the need to test for (implicit) magic return values they ought to notice if they start getting tuples back where they expected scalars... What if they're expecting tuples as the result? def foo(x) if x0: return True, x*x else: return False, Bad value of x in foo:,str(x) ok, value = foo(-1) Oops, that gives: ValueError: too many values to unpack because you've returned three items instead of two. When an idiom is easy to get wrong, it's time to think hard about it. That seems pretty clear to me, too many values to unpack, either I've not given it enough variables to unpack the result into or I've returned too many things. That would take a couple of seconds to notice and fix. In fact I was trying to make the point that it would be quite noticable if a function returned more things than the programmer was expecting, this illustrates that quite well :) if ok: print foo of x is, value else: print ERROR:, value Whenever I come across a function that returns a flag and a result, I never know whether the flag comes first or second. Should I write: Flag then result, isn't it obvious? The whole point of returning a flag AND a result is so you can test the flag so you know what to do with the result so that implies a natural order. Of course it doesn't matter technically which way you do it, make a convention and stick to it. If you get perpetually confused as to the order of parameters then you'd better avoid this kind of thing, can't say as I've ever had a problem with it though. And I never know if the flag should be interpreted as a success or a failure. Should I write: ok, result = foo(x) if ok: process(result) else: fail() Yes. That would be my strong preference anyway. Naturally you can do it the other way round if you like, as long as you document it properly in your API. As you say different APIs do it differently... Unix has a convention of returning 0 on no-error but unix has to encapsulate a lot in that error code which is a bit of an anachronism these days. I'd argue in favour of remaining positive and using names like ok or success, this is closer to the familiar paradigm of checking a result does not evaluate to false before using it... name = if name: print name And if the flag indicates failure, what should go into result? An error code? An error message? That's impossible for statically-typed languages, unless they have variant records or the function normally returns a string. Yeah, in my example it's an error message. Maybe I shouldn't have used the word pattern above though as it has overtones of universally applicable which it clearly isn't. Again, it's error-prone and messy. Imagine writing: flag, a = sin(x) if flag: flag, b = sqrt(x) if flag: snip Compare that to the way with exceptions: y = exp(sin(x) + cos(sqrt(x)))/log(x) Which would you prefer? LOL, straw man is straw! You know full well I'm not suggesting every function return a flag, that would be silly. There's no reason returning flag and a value shouldn't be quite readable and there may be times when it's preferable to raising an exception. I use exceptions a lot as they're often the right tool for the job and they seem pleasingly pythonic but from time to time they can be too slow or verbose, where's the sense in forcing yourself to use them then? Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
r0g aioe@technicalbloke.com writes: Steven D'Aprano wrote: On Tue, 05 Jan 2010 02:31:34 +, r0g wrote: A pattern I have used a few times is that of returning an explicit success/failure code alongside whatever the function normally returns. That doesn't work for languages that can only return a single result, e.g. C or Pascal. You can fake it by creating a struct that contains a flag and the result you want, but that means doubling the number of data types you deal with. No, but that's why I try not to use languages where you can only return a single result, I always found that an arbitrary and annoying constraint to have. I leads to ugly practices like magic return values in C or explicitly packing things into hashtables like PHP, yuk! Doesn't python just return a single result? (I know it can be a tuple and assignment statements will unpack a tuple for you.) -- http://mail.python.org/mailman/listinfo/python-list
Re: A null program - what is it doing?
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: A null program - what is it doing?
2010/1/5 r0g aioe@technicalbloke.com 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. app.exec_() starts the QT mainloop. If you would have created a window or app before starting the loop, you app would now be responsive and wait for you to do things with it. When you would then close the app, the function returns the error code with which the app closed, and then subsequently the Python process exits with that same error code. But since no widgets were created before starting the mainloop, I don't think you can stop the process in any way other than killing it. By the way, QT has excellent documentation: http://doc.trolltech.com/4.4/qapplication.html#exec Cheers, Almar -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Chris Rebert wrote: much snippage On Tue, Jan 5, 2010 at 1:07 AM, r0g aioe@technicalbloke.com wrote: Lie Ryan wrote: I have been looking at Haskell recently and the way the pure functional language handled exceptions and I/O gives me a new distinct insight that exceptions can be thought of as a special return value that is implicitly wrapped and unwrapped up the call stack until it is explicitly handled. Yes there's some very interesting paradigms coming out of functional programming but, unless you're a maths major, functional languages are a long way off being productivity tools! Elegant: yes, provable: maybe, practical for everyday coding: not by a long shot! Methinks the authors of Real World Haskell (excellent read btw) have a bone to pick with you. Cheers, Chris -- http://blog.rebertia.com LOL, it seems things have come a long way since ML! I'm impressed how many useful libraries Haskell has, and that they've included IF-THEN-ELSE in the syntax! :) For all its advantages I still think you need to be fundamentally cleverer to write the same programs in a functional language than an old fashioned English like language. Maybe I'm just mistrusting of the new school though and you'll see me on comp.lang.haskell in a few years having to eat my own monads! Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Paul Rudin wrote: r0g aioe@technicalbloke.com writes: Steven D'Aprano wrote: On Tue, 05 Jan 2010 02:31:34 +, r0g wrote: A pattern I have used a few times is that of returning an explicit success/failure code alongside whatever the function normally returns. That doesn't work for languages that can only return a single result, e.g. C or Pascal. You can fake it by creating a struct that contains a flag and the result you want, but that means doubling the number of data types you deal with. No, but that's why I try not to use languages where you can only return a single result, I always found that an arbitrary and annoying constraint to have. I leads to ugly practices like magic return values in C or explicitly packing things into hashtables like PHP, yuk! Doesn't python just return a single result? (I know it can be a tuple and assignment statements will unpack a tuple for you.) Yes, it returns a tuple if you return more than one value, it just has a lovely syntax for it. In static languages you'd need to manually create an new array or struct, pack your return vars into it and unpack them on the other side. That's something I'd be happy never to see again, sadly I have to write in PHP sometimes :( Roger. -- http://mail.python.org/mailman/listinfo/python-list
ctypes: How to call unexported functions in a dll
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. Thanks for any hint in the right direction. BR Coert -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
r0g wrote: snip 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. snip DaveA -- 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. Likewise, I haven't used QT, but have used others. Gib: It looks like app.exec() is the mainloop for QT, and it won't terminate till it gets a terminate event. That might be the user clicking the X in the corner, or it might be a special keystroke like Alt-F4. Or it might be some other event for which your event handler makes an explicit call to terminate. Once one of these things happens, the app.exec() will return, and the sys.exit() will execute. DaveA -- http://mail.python.org/mailman/listinfo/python-list
embedded python on mac - linking problem
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
On Jan 5, 9:21 am, alejandro aleksanda...@brisiovonet.hr 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. So I need the create a pdf without/less as possible margins, and then I need Acrobat Reader to (silently) print it without any/less as possible extra margins added. I believe the first step is possible, but I am wondering if the second step is possible as well, i.e. can I adjust the options normally appearing in the Printing Dialog through Python? -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
r0g aioe@technicalbloke.com writes: Paul Rudin wrote: Doesn't python just return a single result? (I know it can be a tuple and assignment statements will unpack a tuple for you.) Yes, it returns a tuple if you return more than one value, it just has a lovely syntax for it. No, there is nothing inherent to the ‘return’ statement for dealing with multiple values. The ‘return’ statement *always* returns a single value: whatever value you specify as the argument to the statement (or the value ‘None’ if no argument is specified. If you specify a tuple — and Python has a nice syntax for creating a literal tuple — that's the single value the statement will use. -- \ “There's no excuse to be bored. Sad, yes. Angry, yes. | `\Depressed, yes. Crazy, yes. But there's no excuse for boredom, | _o__) ever.” —Viggo Mortensen | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python] Re: Printing plain text with exact positioning on Windows
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Dave Angel wrote: r0g wrote: snip 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. snip 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
In article hhv255$gi...@speranza.aioe.org, r0g aioe@technicalbloke.com wrote: No, but that's why I try not to use languages where you can only return a single result, I always found that an arbitrary and annoying constraint to have. I leads to ugly practices like magic return values in C or explicitly packing things into hashtables like PHP, yuk! Python only lets you return a single result, just like C or C++. The difference is that in Python it's trivial to build tuples on the fly and return them. About the closest you get to that in C++ is std::pair, and that's about 5 bucks short and a week late. -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing plain text with exact positioning on Windows
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! -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On Tue, 05 Jan 2010 13:06:20 +, r0g wrote: Dave Angel wrote: r0g wrote: snip 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. snip 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, Correct. or run through an optimizer. I don't know what you mean by that. 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. You *always* need to validate user data (contents of user-editable config files, command line arguments, data files, text they type into fields, etc.) because you have no control over what they put into that. So you shouldn't use assert for validating user data except for quick-and-dirty scripts you intend to use once and throw away. Program logic, on the other hand, theoretically shouldn't need to be validated at all, because we, the programmers, are very clever and naturally never make mistakes. Since we never make mistakes, any logic validation we do is pointless and a waste of time, and therefore we should be able to optimise it away to save time. *cough* Since in reality we're not that clever and do make mistakes, we actually do want to do some such program validation, but with the option to optimise it away. Hence the assert statement. So, a totally made-up example: def function(x, y): if x 0: raise ValueError(x must be zero or positive) if y 0: raise ValueError(y must be zero or negative) z = x*y assert z 0, expected product of +ve and -ve number to be -ve return 1.0/(z-1) This example cunningly demonstrates: (1) Using explicit test-and-raise for ensuring that user-supplied arguments are always validated; (2) Using an assertion to test your program logic; (3) That the assertion in fact will catch an error in the program logic, since if you pass x or y equal to zero, the assertion will fail. Any time you are tempted to write a comment saying This can't happen, but we check for it just in case, that is a perfect candidate for an assertion. Since it can't happen, it doesn't matter if it goes away with the -O flag; but since we're imperfect, and we want to cover ourselves just in case it does happen, we perform the test when not optimized. From my own code, I have a global constant: UNICODE_NUMERALS = u'\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17 \uff18\uff19' And then to make sure I haven't missed any: assert len(UNICODE_NUMERALS) == 10 In another function, I validate a mapping {key:value} to ensure that all the values are unique: seen_values = set() for k,v in mapping.items(): if v in seen_values: raise ValueError('duplicate value %s' % k) seen_values.add(v) # If we get here without error, then the mapping contains no # duplicate values. assert len(seen_values) == len(mapping) The assertion acts as a double-check on my logic, not the data. If my logic is wrong (perhaps there is a way to get past the for-loop while there is a duplicate?) then the assertion will catch it. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Talking with ebay using easyBay
Hi, I made an application to manage auctions with easyBayhttp://pypi.python.org/pypi/easyBay/0.1dev my problem is that easybay site is down and there is any other source of information in the net regarding the packadge. Any one has the docs? or know how to make a search? Thank you in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
On 05 Jan 2010 14:02:50 GMT Steven D'Aprano st...@remove-this-cybersource.com.au 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. -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception as the primary error handling mechanism?
Ben Finney wrote: r0g aioe@technicalbloke.com writes: Paul Rudin wrote: Doesn't python just return a single result? (I know it can be a tuple and assignment statements will unpack a tuple for you.) Yes, it returns a tuple if you return more than one value, it just has a lovely syntax for it. No, there is nothing inherent to the ‘return’ statement for dealing with multiple values. The ‘return’ statement *always* returns a single value: whatever value you specify as the argument to the statement (or the value ‘None’ if no argument is specified. If you specify a tuple — and Python has a nice syntax for creating a literal tuple — that's the single value the statement will use. That's what I said Ben... Doesn't python just return a single result? Yes, See how I agree that The ‘return’ statement *always* returns a single value? it returns a tuple if you return more than one value, it just has a lovely syntax for it. You're confusing literal and conceptual returns. You can tell there are two senses of return at play because the two returns have different possessives within the same sentence. Look... Yes, IT returns a tuple - Speaking literally about Python... return 1, 2, 3 # Python returned one value YOU return more than one value - Speaking conceptually from a programmers perspective. return 1, 2, 3 # I returned three values Both are valid in their own context. The second (conceptual) statement MIGHT be ambiguous if it weren't for the first literal one. Both cannot be literally true and the first one clearly IS a literal factual statement about Python so the second cannot also be interpreted as literal. So let's disgard it... it returns a tuple, it just has a lovely syntax for it. The first two its are Python, the third could either be... (a) The construction of tuples or at a stretch... (b) The act of returning tuples i.e. some special syntax for returning tuples. You seem to think I meant (b) - I actually meant (a) Maybe I could have made that clearer but I don't think people want to read legalese and I think it takes a particular pedantic, nitpicking disposition to even spot such small ambiguities. 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. 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! 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
Re: Exception as the primary error handling mechanism?
Steven D'Aprano wrote: On Tue, 05 Jan 2010 13:06:20 +, r0g wrote: snip 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. snipped very detailed and clear response, thanks :) 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: 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 WinFunctionType object at 0x00AD4D50 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: Dynamic text color
John Posner wrote: On Fri, 01 Jan 2010 21:01:04 -0500, Cousin Stanley cousinstan...@gmail.com wrote: snip 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: Exception as the primary error handling mechanism?
r0g wrote: Dave Angel wrote: r0g wrote: snip 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. snip 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: embedded python on mac - linking problem
On Tue, Jan 5, 2010 at 7:33 AM, Krzysztof Kobus kk720...@yahoo.com 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: 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: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman chris.gonner...@newcenturycomputers.net 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: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman chris.gonner...@newcenturycomputers.net 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: 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: Where's a DOM builder that uses the Builder Pattern to ... build DOMs?
On Jan 5, 12:16 am, Stefan Behnel stefan...@behnel.de 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: 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: 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. snipped very detailed and clear response, thanks :) 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?
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?
D'Arcy J.M. Cain wrote: On 05 Jan 2010 14:02:50 GMT Steven D'Aprano st...@remove-this-cybersource.com.au 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: Dynamic text color
On Tue, 05 Jan 2010 10:31:09 -0500, Dave McCormick mackrac...@gmail.com 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
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: Printing plain text with exact positioning on Windows
On Jan 5, 12:56 pm, Chris Gonnerman chris.gonner...@newcenturycomputers.net 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
it gets worse (was: How do you configure IDLE on a Mac...)
On Jan 5, 12:32 am, Ned Deily n...@acm.org wrote: In article 0d70cb54-3d77-4176-b621-e764ecf61...@26g2000yqo.googlegroups.com, Mensanator mensana...@aol.com 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: 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
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: 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: twenty years ago Guido created Python
On Dec 31 2009, 2:06 pm, Steve Howell showel...@yahoo.com 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: parsing an Excel formula with the re module
On 5 ene, 19:35, MRAB pyt...@mrabarnett.plus.com 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: parsing an Excel formula with the re module
On Tue, 05 Jan 2010 13:12:00 -0500, vsoler vicente.so...@gmail.com 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 Jan 5, 12:35 pm, MRAB pyt...@mrabarnett.plus.com 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: 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: What is the best data structure for a very simple spreadsheet?
On 3 ene, 22:40, mdipierro massimodipierr...@gmail.com 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 vicente.so...@gmail.com 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: 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: parsing an Excel formula with the re module
vsoler wrote: On 5 ene, 19:35, MRAB pyt...@mrabarnett.plus.com 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: twenty years ago Guido created Python
Phlip wrote: On Dec 31 2009, 2:06 pm, Steve Howell showel...@yahoo.com 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
On 5 ene, 20:05, Mensanator mensana...@aol.com wrote: On Jan 5, 12:35 pm, MRAB pyt...@mrabarnett.plus.com 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
Fwd: I would like to install Python on my 64 bit Win 7
-- Forwarded message -- From: aung paing Soe aps.it2...@gmail.com 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
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
Re: parsing an Excel formula with the re module
On 5 ene, 20:21, vsoler vicente.so...@gmail.com wrote: On 5 ene, 20:05, Mensanator mensana...@aol.com wrote: On Jan 5, 12:35 pm, MRAB pyt...@mrabarnett.plus.com 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
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: Fwd: I would like to install Python on my 64 bit Win 7
aung paing Soe wrote: -- Forwarded message -- From: aung paing Soe aps.it2...@gmail.com 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 -- 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(KeyRelease, get_complete_text) Tbox.focus() root.mainloop() Thanks again, Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: twenty years ago Guido created Python
On Jan 5, 8:22 am, n00m n...@narod.ru 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: Fwd: I would like to install Python on my 64 bit Win 7
On Tue, Jan 5, 2010 at 3:03 PM, Dave McCormick mackrac...@gmail.com wrote: aung paing Soe wrote: -- Forwarded message -- From: aung paing Soe aps.it2...@gmail.com 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: please help shrink this each_with_index() implementation
On Jan 6, 8:58 am, Phlip phlip2...@gmail.com 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: 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 5, 8:58 pm, Phlip phlip2...@gmail.com 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: Dynamic text color
On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick mackrac...@gmail.com 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
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: 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: 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
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: Dynamic text color
John Posner wrote: On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick mackrac...@gmail.com 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
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: it gets worse (was: How do you configure IDLE on a Mac...)
In article 6672dad2-26ba-458b-8075-21bac6506...@e37g2000yqn.googlegroups.com, Mensanator mensana...@aol.com 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
Re: parsing an Excel formula with the re module
Mensanator wrote: On Jan 5, 12:35 pm, MRAB pyt...@mrabarnett.plus.com 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: Exception as the primary error handling mechanism?
r0g aioe@technicalbloke.com 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: please help shrink this each_with_index() implementation
On Jan 5, 1:10 pm, Antoine Pitrou solip...@pitrou.net 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: 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: Exception as the primary error handling mechanism?
On Wed, 06 Jan 2010 09:39:08 +1100, Ben Finney wrote: r0g aioe@technicalbloke.com 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: 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 solip...@pitrou.net 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: 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
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: 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
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: please help shrink this each_with_index() implementation
On Jan 5, 2010, at 4:30 PM, Phlip phlip2...@gmail.com 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: Exception as the primary error handling mechanism?
Steven D'Aprano wrote: On Wed, 06 Jan 2010 09:39:08 +1100, Ben Finney wrote: r0g aioe@technicalbloke.com 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: Exception as the primary error handling mechanism?
On Tue, Jan 5, 2010 at 3:51 PM, Phlip phlip2...@gmail.com 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?
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. snip 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 Jan 5, 5:01 pm, Chris Rebert c...@rebertia.com 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: unittest inconsistent
On Jan 5, 4:14 pm, Matt Haggard haggar...@gmail.com 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