ANN: sympycore version 0.1 released
We are proud to present a new Python package: sympycore - an efficient pure Python Computer Algebra System Sympycore is available for download from http://sympycore.googlecode.com/ Sympycore is released under the New BSD License. Sympycore provides efficient data structures for representing symbolic expressions and methods to manipulate them. Sympycore uses a very clear algebra oriented design that can be easily extended. Sympycore is a pure Python package with no external dependencies, it requires Python version 2.5 or higher to run. Sympycore uses Mpmath for fast arbitrary-precision floating-point arithmetic that is included into sympycore package. Sympycore is to our knowledge the most efficient pure Python implementation of a Computer Algebra System. Its speed is comparable to Computer Algebra Systems implemented in compiled languages. Some comparison benchmarks are available in * http://code.google.com/p/sympycore/wiki/Performance * http://code.google.com/p/sympycore/wiki/PerformanceHistory and it is our aim to continue seeking for more efficient ways to manipulate symbolic expressions: http://cens.ioc.ee/~pearu/sympycore_bench/ Sympycore version 0.1 provides the following features: * symbolic arithmetic operations * basic expression manipulation methods: expanding, substituting, and pattern matching. * primitive algebra to represent unevaluated symbolic expressions * calculus algebra of symbolic expressions, unevaluated elementary functions, differentiation and polynomial integration methods * univariate and multivariate polynomial rings * matrix rings * expressions with physical units * SympyCore User's Guide and API Docs are available online. Take a look at the demo for sympycore 0.1 release: http://sympycore.googlecode.com/svn/trunk/doc/html/demo0_1.html However, one should be aware that sympycore does not implement many features that other Computer Algebra Systems do. The version number 0.1 speaks for itself:) Sympycore is inspired by many attempts to implement CAS for Python and it is created to fix SymPy performance and robustness issues. Sympycore does not yet have nearly as many features as SymPy. Our goal is to work on in direction of merging the efforts with the SymPy project in the near future. Enjoy! * Pearu Peterson pearu.peterson AT gmail DOT com * Fredrik Johansson Acknowledgments: * The work of Pearu Peterson on the SympyCore project is supported by a Center of Excellence grant from the Norwegian Research Council to Center for Biomedical Computing at Simula Research Laboratory. PA HREF=http://sympycore.googlecode.com/;SympyCore 0.1/A - an efficient pure Python Computer Algebra System. (29-Feb-08) -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: rstrip error python2.4.3 not in 2.5.1?
What is the actual error message [SyntaxError, NameError? etc] that you clipped? Here it is : I tought that I didn't matter because the deliciousapi worked fine on my mac. Traceback (most recent call last): File delgraph.py, line 62, in ? url_metadata = d.get_url(site.rstrip()) File deliciousapi.py, line 269, in get_url document.bookmarks = self._extract_bookmarks_from_url_history(data) File deliciousapi.py, line 297, in _extract_bookmarks_from_url_history timestamp = datetime.datetime.strptime(month_string, '%b lsquo; %y') AttributeError: type object 'datetime.datetime' has no attribute 'strptime' -- http://mail.python.org/mailman/listinfo/python-list
Re: rstrip error python2.4.3 not in 2.5.1?
dirkheld [EMAIL PROTECTED] wrote: Here it is : I tought that I didn't matter because the deliciousapi worked fine on my mac. Traceback (most recent call last): File delgraph.py, line 62, in ? url_metadata = d.get_url(site.rstrip()) File deliciousapi.py, line 269, in get_url document.bookmarks = self._extract_bookmarks_from_url_history(data) File deliciousapi.py, line 297, in _extract_bookmarks_from_url_history timestamp = datetime.datetime.strptime(month_string, '%b lsquo; %y') AttributeError: type object 'datetime.datetime' has no attribute 'strptime' The answer is right there: datetime.datetime has method strptime in python 2.5 but which was not available in 2.4. A quick look into the library reference confirms this: -- strptime( date_string, format) Return a datetime corresponding to date_string, parsed according to format. This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])). ValueError is raised if the date_string and format can't be parsed by time.strptime() or if it returns a value which isn't a time tuple. New in version 2.5. -- cu Philipp -- Dr. Philipp Pagel Lehrstuhl f. Genomorientierte Bioinformatik Technische Universität München http://mips.gsf.de/staff/pagel -- http://mail.python.org/mailman/listinfo/python-list
A python STUN client is ready on Google Code.
http://code.google.com/p/boogu/ Enjoy it! Hawk -- http://mail.python.org/mailman/listinfo/python-list
Re: Run wxPython app remotely under XWindows
Sean DiZazzo wrote: On Feb 28, 3:50 pm, Bjoern Schliessmann usenet- $ ssh some-other-machine $ DISPLAY=:0 ./my_app.py Should wxPython apps work this way? I think so; at least it works for me. Do you think it's something with the server? I have no idea. Regards, Björn -- BOFH excuse #325: Your processor does not develop enough heat. -- http://mail.python.org/mailman/listinfo/python-list
xsd, data binding. Modern approach?
Hi, What is the most actual approach to python XML data-binding? The answers given by google seam to be rather outdated. Can't believe nothing's changed since 2003. To be concrete, i've faced the following task: I HAVE: - XSD schema (a huge collection of *.xsd files) TODO: - create python classes corresponding to the given schema - create, serialize/deserialize (to/from XML) python objects of these classes (according to the given schema) Thanks! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: feedback requested
On Feb 29, 12:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 08:09:01 -0800 (PST), [EMAIL PROTECTED] declaimed the following in comp.lang.python: My goal is to return Deadlock from acquire() if its blocking would directly create deadlock. Basic example: [ The safeguard is never worth the cost.] I am NOT going to look any further into this line... I am. -- http://mail.python.org/mailman/listinfo/python-list
Re: xsd, data binding. Modern approach?
Vladimir Kropylev wrote: What is the most actual approach to python XML data-binding? The answers given by google seam to be rather outdated. Can't believe nothing's changed since 2003. To be concrete, i've faced the following task: I HAVE: - XSD schema (a huge collection of *.xsd files) TODO: - create python classes corresponding to the given schema - create, serialize/deserialize (to/from XML) python objects of these classes (according to the given schema) If you don't insist on generating code, lxml.objectify might do what you want. http://codespeak.net/lxml/objectify.html http://codespeak.net/lxml/objectify.html#asserting-a-schema Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
[EMAIL PROTECTED] wrote: On Feb 27, 6:02 pm, Tamer Higazi [EMAIL PROTECTED] wrote: Hi! Can somebody of you make me a sample how to define a function based on call by reference ??? I am a python newbie and I am not getting smart how to define functions, that should modify the variable I passed by reference. thanks in advance Tamer If it's a mutable object, avoid the pitfalls of rebinding the parameter, and just modify the object. BAD: def f( a ): a= { 'this': 'that' } GOOD: def f( a ): a.clear() a[ 'this' ]= 'that' BETTER: class Thang: pass def f(a): a.this = that thang = Thang() f(thang) regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: (Newbie) Help with sockets.
On Feb 28, 11:22 pm, Grant Edwards [EMAIL PROTECTED] wrote: On 2008-02-29, Gabriel Genellina [EMAIL PROTECTED] wrote: En Fri, 29 Feb 2008 00:20:26 -0200, [EMAIL PROTECTED] escribió: Hi everyone. I'm fairly new to Python, and even more new to socket programming. I think I've wrapped my head around sockets, and with that I want to create a Telnet-based chat server, the idea being people connect to the telnet servers with their clients and they all communicate. I've got the code working, but the server sends each letter to the clients on a new line! I've read about this kind of thing on Windows on Google, but I can't find a solution to this issue. The telnet client can use line mode or character mode. In line mode nothing is sent until the user press Enter; line editing is made on the client side. In character mode keystrokes are sent as soon as typed; probably your telnet client is using this mode. That's definitely going to be a problem. To the OP: You claim to be writing a telnet server, yet I don't see any code that actually implements the telnet protocol. Different telnet clients default to different modes, so if you want them in a certain mode, you have to put them in the desired mode by implementing the telnet feature negotiation protocol. -- Grant Edwards grante Yow! It's strange, but I'm at only TRULY ALIVE when I'm visi.com covered in POLKA DOTS and TACO SAUCE... OP here - sorry for the confusion, it's just a listener program that people connect to via telnet. I'll take a look at the Twisted engine and see if that helps at all. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested module import clutters package namespace?
Arnaud Delobelle wrote: On Feb 29, 12:16 am, Gabriel Genellina [EMAIL PROTECTED] wrote: En Thu, 28 Feb 2008 11:00:08 -0200, Dr. Rüdiger Kupper [EMAIL PROTECTED] escribió: I'd be grateful for help with a problem of package and module namespaces. The behaviour I observe is unexpected (to me), and I couldn't find the answer in the docs, the tutorial, or the mailing list archive. So here we go: I have a package named 'pack'. Apart from the '__init__.py' file the directory contains two modules 'x.py' and 'y.py': pack/ __init__.py x.py y.py __init__.py print pack: Here is pack. print pack: I now assign y='hello'. y=hello print pack: My y is now:, repr(y) print pack: I now 'import x' which in turn does 'import y as q'. import x print pack: My y is now:, repr(y) print pack: Why? = x.py === print ' x: Here is x.' print x: I now 'import y as q'. import y as q = y.py === print 'y: Here is y.' = Looks like a bug to me - or perhaps someone who actually understands how import works could explain it? Say: pack/ __init__.py x.py importing x in any manner will automatically load pack and put x in pack's namespace. Try: from pack import x as foo sys.modules['pack'].x This explain the behaviour observed by the OP. AFAIK it is not a bug. The tutoria hints at this without going into too much detail. Thanks Gabriel and Arnaud for your comments. Since I felt unable to decide if this is a bug or known behaviour I filed the issue in the bugtracker. It certainly is inconsistent with what the docs and tutorial tells the user (they all say that from the user's view, import statements behave like assignments), so it should at least be clearly documented. Cheers, Rüdiger -- Dr. Rüdiger Kupper Honda Research Institute Europe GmbH Carl-Legien-Straße 30 D-63073 Offenbach/Main, Germany Phone : +049 (0)69-890 11-725 Fax : +049 (0)69-890 11-749 E-Mail: [EMAIL PROTECTED] PGP ID: C2303358 signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: is there enough information?
On Feb 29, 12:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 00:54:44 -0800 (PST), [EMAIL PROTECTED] declaimed the following in comp.lang.python: On Feb 28, 2:30 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: It is time to show your cards or fold Here. Run it. Download Python 3.0a2. I'm unlikely to download an alpha release when I haven't even upgraded to 2.5 (maybe the next three day weekend I'll have time to track down new versions of all the third party modules I have installed and then download 2.5) But I'll meet you half-way [snip] The RESET is the one executed as part of initializing the StepLock member of Worker. Even though thrd.start() is invoked, it is likely the thread does not gain control at that point. That would mean the ACQUIRE 0 is the one in the range(5) loop, along with the NOTIFY and RELEASE, followed by calling op100. ACQUIRE 5 is a result of the acquirestep(0). NOW the thread actually gained control (worker entered) and ACQUIRE 8 8 are part of its acquirestep(1). The thread actually gets the condition lock immediately, tests the current step level against the step is want and finds a mismatch. The thread then signals for any other thread waiting on the condition via NOTIFY, and then waits for another thread to trigger a notify on it (WAIT 12). Control returns to op100 and the acquirestep(0) ACQUIRE 5 is answered with 5 taken; the step level matches the desired step, and the code for step 0 is executed... Pretty cool. First, thanks for taking the time to -edit- my code, along with getting it running! I appreciate your effort. And I'm happy you like it enough to put it in. It runs on my machine in 2.5. First change was to run it indefinitely, and it's doing so. Still not conclusive evidence that it's free of deadlocks, nor is that I don't see any by inspection. ...But it's running. Second, thanks for picking better names for the identifiers. Mine always suck. Third. StepLock.acquirestep can use notifyAll: wake all of them up, and all but one go back to sleep. However, the current implementation wakes them up in order wait was called, so you do save some time here, (Even though the docs say otherwise). The while self._step!= step is suspicious, but the alternative is an -sequence- of locks, the penalty is only large for high step lengths, and it's not exactly polling-- so maybe that's just the purist in me. Fourth: wait is a private member, unless you're enabling the functionality of waiting for the next change in step by another thread. notifyAll isn't used anywhere. condition is also private, I believe. Fifth: My webreader gives me this information: Note: The author of this message requested that it not be archived. This message will be removed from Groups in 6 days (Mar 7, 12:55 am). Curious if you intentially have it. It could be nice to retrieve this-- same with Stage. Sixth: If I comment out fmain:worktask.slock.release() and threadbody:worker.slock.release() there is no effect. It's bizarre. Is it a requirement to use them? Seventh: What is the advantage of using a Condition over an Event? -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Feb 29, 5:56 am, Steve Holden [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On Feb 27, 6:02 pm, Tamer Higazi [EMAIL PROTECTED] wrote: Hi! Can somebody of you make me a sample how to define a function based on call by reference ??? I am a python newbie and I am not getting smart how to define functions, that should modify the variable I passed by reference. thanks in advance Tamer If it's a mutable object, avoid the pitfalls of rebinding the parameter, and just modify the object. BAD: def f( a ): a= { 'this': 'that' } GOOD: def f( a ): a.clear() a[ 'this' ]= 'that' BETTER: class Thang: pass def f(a): a.this = that thang = Thang() f(thang) regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/- Hide quoted text - - Show quoted text - What does __coerce__ look like, so you could operate on a.this without accessing 'this' member every time? For numbers maybe, but what about __getattr__( self, name ): return getattr( self.this, name ) for strings? Then thang.this= that; thang.find( 'at' ) - thang.this.find( 'at' ). Awesome! -- http://mail.python.org/mailman/listinfo/python-list
Re: xsd, data binding. Modern approach?
Vladimir Kropylev wrote: Hi, What is the most actual approach to python XML data-binding? The answers given by google seam to be rather outdated. Can't believe nothing's changed since 2003. To be concrete, i've faced the following task: I HAVE: - XSD schema (a huge collection of *.xsd files) TODO: - create python classes corresponding to the given schema - create, serialize/deserialize (to/from XML) python objects of these classes (according to the given schema) http://www.rexx.com/~dkuhlman/generateDS.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Indentation and optional delimiters
On Feb 28, 3:18 pm, Terry Reedy [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | But the default behavior may become the true copy, that seems | simpler for a newbie to grasp. To me, it is the opposite. If I say gvr = Guido_van_Russum # or any natural language equivalent do you really think a copy is made? Copying is much more work than defining an alias or nickname. It's interesting. If I say minigvr= Guido_van_Rossum except smaller, my listener carries both the original model and the exception around during the conversation. minigvr= type( 'MiniGvr', ( Guido_van_Rossum, ), dict( size= 0.5 ) )(), creates a MiniGvr class - and- -instantiates- it, just with a different size. It depends-- if you say, what if mini-guido were to go to the store it's very different from saying, what if mini-guidos were to go to the store? The first 'mini-guido' is an instance, and you're running the hypothesis in a sandbox or playpen. The second is different. The listener subclasses Guido_van_Rossum, but still in the sandbox. If you said, 'a mini-guido went to the store yesterday', the listener would run type( 'MiniGvr', ( Guido_van_Rossum, ), dict( size= 0.5 ) ) ().goestostore( time= Time.Yesterday ). What's more, you can the next day say, Remember that miniguido that went to the store, Remember that miniguido I told you about yesterday, or even, Remember that miniguido that went to the store that I told you about? and your listener can say, Yeah, that was two days ago now. However, isinstance checks sometimes don't work: Remember that GvR that went to the store? No, you never told me GvR went to the store wait, unless you mean the -Mini-GvR. Your actual call is closer to this: memory.add( event= Event.GoToStore( type( 'MiniGvr', ( Guido_van_Rossum, ), { 'size': 0.5 } )() ), time= Time.Yesterday ), but Go is also abstracted ( (destination= Store) ), Store is abstracted (SomeStore), (unless you and the listener have a the store you always call in common), and he may actually interpret was at the store instead of went to, depending on your interaction's particular idiolect; you might use slightly different words to tell a co-worker the same story. Last but not least, the data that filters down into the hardware of the brain is partly non-propositional-- the listener gets a somewhat clear picture in your preamble, which varies in clarity and what detail from speaker to speaker and listener to listener pair. If you want a computer language to model human thought, then is there even such thing as subclassing? Otherwise, it's a toolbox, and pass- by-reference is to a flathead screwdriver as pass-by-value is to a Phillips. Which one do you want to carry, and which is the snap-on extension? Doesn't that vary trade-to-trade, tradesman-to-tradesman, and even site-to-site? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's BNF
Gabriel and Steve, Poor globals! They take such a beating and they really don't deserve it. The use of globals was deprecated, if memory serves, during the structured design craze. Using globals is now considered bad practice, but it's considered bad practice for reasons that don't stand close scrutiny, this being a perfect example. ofile = ... # global writeHTML() def writeHTML(): ofile.write( .. ) writeBody() def writeBody(): ofile.write( ... ) writeEntries() def writeEntries() ofile.write( ... ) writeEntry() def writeEntry(): ofile.write( ... ) ... # fixed to eliminate the evil global writeHTML(ofile) def writeHTML(ofile): ofile.write( .. ) writeBody(ofile) def writeBody(ofile): ofile.write( ... ) writeEntries(ofile) def writeEntries(ofile) ofile.write( ... ) writeEntry(ofile) def writeEntry(ofile): ofile.write( ... ) ... # repeat above for another half dozen subs that also use ofile The code's simpler before the fix. So, as a nod to the anti-global school of thought, I changed 'ofile' to 'OFILE' so that it would at least look like a global constant. Then I changed to '_OFILE' as a reminder that this is a modular, not global, constant. Ditto for '_PRODUCTIONS'. Modular constants share exactly none of the coupling problems that globals can have. You'll let me use modular constants, right? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python app at startup!
[EMAIL PROTECTED] wrote: On Feb 28, 5:07 pm, SMALLp [EMAIL PROTECTED] wrote: Hy. I create simple application. Yust an windows and compile it with py2exe. I add registry value reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v MyApp /t REG_SZ /d C:\myapp.exe /f' And it wont start. When i use console instead od window in py2exe i get console opend but it closes. Does it do the same thing when you run it with the Python interpreter? No. The programm works fine! In interupter and when i compile it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python app at startup!
On Feb 28, 5:07 pm, SMALLp [EMAIL PROTECTED] wrote: Hy. I create simple application. Yust an windows and compile it with py2exe. I add registry value reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v MyApp /t REG_SZ /d C:\myapp.exe /f' And it wont start. When i use console instead od window in py2exe i get console opend but it closes. Does it do the same thing when you run it with the Python interpreter? -- http://mail.python.org/mailman/listinfo/python-list
Re: You have to see this - http://ilaarijs.blogspot.com/ :D
[EMAIL PROTECTED] wrote: You have to see this - http://... :D Sorry. I don't. -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: pySQLite Insert speed
[EMAIL PROTECTED] wrote: (B) is better than (A). The parameter binding employed in (B) is not only faster on many databases, but more secure. See, for example,http://informixdb.blogspot.com/2007/07/filling-in- blanks.html Thx. The link was helpful, and I think I have read similar things before-- that B is faster. So ... I just rewrote the test code from scratch and B is faster. I must have had something wrong in my original timing. Don't forget, by the way, that your original (B) code was performing the string substitution of the parameter markers into the SQL statement each time the statement was executed. As Carsten pointed out, this overhead should be performed at most once, and only then if you want your code to be portable over database backends with different paramstyles. Forget (A). regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: (Newbie) Help with sockets.
On 2008-02-29, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: You claim to be writing a telnet server, yet I don't see any code that actually implements the telnet protocol. Different telnet clients default to different modes, so if you want them in a certain mode, you have to put them in the desired mode by implementing the telnet feature negotiation protocol. OP here - sorry for the confusion, it's just a listener program that people connect to via telnet. That's what a telnet server _is_: something that people connect to via telnet. I'll take a look at the Twisted engine and see if that helps at all. Make sure it has telnet protocol support, or you're going to end up with similar problem. -- Grant Edwards grante Yow! Four thousand at different MAGNATES, MOGULS visi.com NABOBS are romping in my gothic solarium!! -- http://mail.python.org/mailman/listinfo/python-list
Re: pySQLite Insert speed
(B) is better than (A). The parameter binding employed in (B) is not only faster on many databases, but more secure. See, for example,http://informixdb.blogspot.com/2007/07/filling-in- blanks.html Thx. The link was helpful, and I think I have read similar things before-- that B is faster. So ... I just rewrote the test code from scratch and B is faster. I must have had something wrong in my original timing. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to configure Python in Windows
[EMAIL PROTECTED] wrote: Hi Folks, I just downloaded precompiled Python for Windows, and it runs. Now I have got the command line coding. However, I can't run my python scripts. My python script, foo.py, is located in C:\\\pydir, and I have set the python interpreter on the directory. When I run os.listdir(), I found my script of foo.py is right in the pydir. But, when I tried to run command-line python foo.py, to compile it, I got Syntax error: invalid syntax. I guess the interpreter did not find my script foo.py. In the past, I worked python on unix/linux, and I knew how set the path, and I ran my python scripts smoothly. I wonder anybody can help me to configure the python interpreter? Thanks! http://www.python.org/doc/faq/windows/ regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
why not bisect options?
Hi all, I thought it would be useful if insort and consorts* could accept the same options than list.sort, especially key and cmp. The only catch I can think of is that nothing prevents a crazy developer to insort elements using different options to the same list. I foresee two courses of actions: 1) let the developer be responsible for the homogeneity of successive insort calls on the same list (remember that the developer is already responsible for giving a sorted list), or 2) make bisect a class which keeps the key and cmp options internally and always use them for comparison, something like: class Bisect: def __init__(self, lst = [], key = None, cmp = None): self.key = key self.cmp = cmp self.lst = lst self.lst.sort(key = key, cmp = cmp) def compare_elements(self, a, b): if self.cmp is not None: return self.cmp(a, b) if self.key is not None: return cmp(self.key(a), self.key(b)) return cmp(a,b) def insort_right(self, elt, lo = 0, hi = None): Inspired from bisect in the python standard library if hi is None: hi = len(self.lst) while lo hi: mid = (lo + hi) / 2 if self.compare_elements(elt, self.lst[mid]) 0: hi = mid else: lo = mid + 1 self.lst.insert(lo, elt) ... Any thoughts about this? RB * at this point you should smile... -- http://mail.python.org/mailman/listinfo/python-list
Re: Indentation and optional delimiters
Steve Holden wrote: [EMAIL PROTECTED] wrote: [...] If you want a computer language to model human thought, then is there even such thing as subclassing? Kindly try to limit your ramblings to answerable questions. Without keen insight into the function of the mind that is currently available to the psychological and psychiatric professions, such philosophical speculation is unlikely to do anyone any good, least of all you. ^available^unavailable^ -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
[EMAIL PROTECTED] wrote: On Feb 29, 5:56 am, Steve Holden [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On Feb 27, 6:02 pm, Tamer Higazi [EMAIL PROTECTED] wrote: Hi! Can somebody of you make me a sample how to define a function based on call by reference ??? I am a python newbie and I am not getting smart how to define functions, that should modify the variable I passed by reference. thanks in advance Tamer If it's a mutable object, avoid the pitfalls of rebinding the parameter, and just modify the object. BAD: def f( a ): a= { 'this': 'that' } GOOD: def f( a ): a.clear() a[ 'this' ]= 'that' BETTER: class Thang: pass def f(a): a.this = that thang = Thang() f(thang) [please refrain from quoting signatures in your replies] [better still, use a mailer that omits them from the quote!] - Show quoted text - What does __coerce__ look like, so you could operate on a.this without accessing 'this' member every time? For numbers maybe, but what about __getattr__( self, name ): return getattr( self.this, name ) for strings? Then thang.this= that; thang.find( 'at' ) - thang.this.find( 'at' ). Awesome! Where did __coerce__ come from? Stick with the main party, please. __coerce__ is an old mechanism intended to be used to bring numeric types into alignment, and AFAICS has nothing at all to do with whatever idea you are suggesting. As near as I can make out you appear to want to have thang delegate certain of its method to thang.this. The easiest way to do that would be to implement a __getattr__() in the Thang class to do so, but remember that it won't be called for cases where the class has a real attribute with the correct name. Hope this helps. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Indentation and optional delimiters
[EMAIL PROTECTED] wrote: [...] If you want a computer language to model human thought, then is there even such thing as subclassing? Kindly try to limit your ramblings to answerable questions. Without keen insight into the function of the mind that is currently available to the psychological and psychiatric professions, such philosophical speculation is unlikely to do anyone any good, least of all you. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... The following iterator yields things that look like each of those items: def category_iterator(source): source = iter(source) last_cat = None entries = [] try: while True: item = source.next() if item == item.upper(): # categories are uppercase if last_cat: yield [last_cat] + entries last_cat = item entries = [] else: title = item subtitle = source.next() link = source.next() entries.append('%s %s' % (title, subtitle)) entries.append(link) except StopIteration: if last_cat: yield [last_cat] + entries if __name__ == '__main__': data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1', 'NOTICES', 'title2','subtitle2','pdf', 'title3','subtitle3','pdf'] for compact_category_info in category_iterator(data): print repr(compact_category_info) If your input data is malformed, you may get peculiar results depending on how pathologically malformed that data is. Hope this helps, -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
[EMAIL PROTECTED] a écrit : Hi all, I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... I don't know where your data come from, but the data structure is obviously wrong. It should at least be a list of tuples, ie: data = [ ('RULES', [ ('title', 'subtitle', 'pdf'), ('title1', 'subtitle1', 'pdf1') ] ), ('NOTICES',[ ('title2','subtitle2','pdf',) ('title3','subtitle3','pdf') ] ), ] I've racked my brain for a while about this and I can't seem to figure it out. Any ideas would be much appreciated. If possible, fix the code generating the dataset. Any other solution will be at best a dirty - and brittle - hack. -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
[EMAIL PROTECTED] wrote: Hi all, I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... I've racked my brain for a while about this and I can't seem to figure it out. Any ideas would be much appreciated. Thanks data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] olist = [] while data: if data[0] == data[0].upper(): olist.append([data[0]]) del data[0] else: olist[-1].append(data[0]+' '+data[1]) olist[-1].append(data[2]) del data[:3] print olist However, I suspect you should be asking yourself whether this is really an appropriate data structure for your needs. If you say what you are trying to achieve in the large rather than focusing on a limited programming issue there may be much better solutions. I suspect, for example, that a dict indexed by the categories and with the entries each containing a list of tuples might suit your needs much better, i.e. { 'RULES': [('title subtitle', 'pdf'), ('title1 subtitle1', 'pdf')], 'NOTICES': [('title2 subtitle2', 'pdf'), 'title3 subtitle3', 'pdf')]} One final observation: if all the files are PDFs then you might just as well throw the 'pdf' strings away and use a constant extension when you try and open them or whatever :-). Then the lists of tuples i the dict example could just become lists of strings. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Problem round-tripping with xml.dom.minidom pretty-printer
Hello I have run into a problem using minidom. I have an HTML file that I want to make occasional, automated changes to (adding new links). My strategy is to parse it with minidom, add a node, pretty print it and write it back to disk. However I find that every time I do a round trip minidom's pretty printer puts extra blank lines around every element, so my file grows without limit. I have found that normalizing the document doesn't make any difference. Obviously I can fix the problem by doing without the pretty-printing, but I don't really like producing non-human readable HTML. Here is some code that shows the behaviour: import xml.dom.minidom as dom def p(t): d = dom.parseString(t) d.normalize() t2 = d.toprettyxml() print t2 p(t2) p('abc//b/a') Does anyone know how to fix this behaviour? If not, can anyone recommend an alternative XML tool for simple tasks like this? Thanks Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
I tried to make a simple abstraction of my problem, but it's probably better to get down to it. For the funkiness of the data, I'm relatively new to Python and I'm either not processing it well or it's because of BeautifulSoup. Basically, I'm using BeautifulSoup to strip the tables from the Federal Register (http://www.access.gpo.gov/su_docs/aces/fr- cont.html).So far my code strips the html and gets only the departments I'd like to see. Now I need to put it into an Excel file (with pyExcelerator) with the name of the record and the pdf. A snippet from my data from BeautifulSoup like this: ['Environmental Protection Agency', 'RULES', 'Approval and Promulgation of Air Quality Implementation Plans:', 'Illinois; Revisions to Emission Reduction Market System, ', '11042 [E8-3800]', 'E8-3800.pdf', 'Ohio; Oxides of Nitrogen Budget Trading Program; Correction, ', '11192 [Z8-2506]', 'Z8-2506.pdf', 'NOTICES', 'Agency Information Collection Activities; Proposals, Submissions, and Approvals, ', '11108-0 [E8-3934]', 'E8-3934.pdf', 'Data Availability for Lead National Ambient Air Quality Standard Review, ', '0-1 [E8-3935]', 'E8-3935.pdf', 'Environmental Impacts Statements; Notice of Availability, ', '2 [E8-3917]', 'E8-3917.pdf'] What I'd like to see in Excel is this: 'Approval and Promulgation of Air Quality Implementation Plans: Illinois; Revisions to Emission Reduction Market System, 11042 [E8-3800]' | 'E8-3800.pdf' | RULES 'Ohio; Oxides of Nitrogen Budget Trading Program; Correction, 11192 [Z8-2506]' | 'Z8-2506.pdf' | RULES 'Agency Information Collection Activities; Proposals, Submissions, and Approvals, 11108-0 [E8-3934]' | 'E8-3934.pdf' | NOTICES 'Data Availability for Lead National Ambient Air Quality Standard Review, 0-1 [E8-3935]' | 'E8-3935.pdf' | NOTICES 'Environmental Impacts Statements; Notice of Availability, 2 [E8-3917]' | 'E8-3917.pdf' | NOTICES etc...for every department I want. Now that I look at it I've got another problem because 'Approval and Promulgation of Air Quality Implementation Plans:' should be joined to both Illinois and Ohio...I love finding these little inconsistencies! Once I get the data organized with all the titles joined together appropriately, outputting it to Excel should be relatively easy. So my problem is how to join these titles together. There are a couple patterns. Every law is followed by a number, which is always followed by the pdf. Any ideas would be much appreciated. My code so far (excuse the ugliness): import urllib import re, codecs, os import pyExcelerator from pyExcelerator import * from BeautifulSoup import BeautifulSoup as BS #Get the url, make the soup, and get the table to be processed url = http://www.access.gpo.gov/su_docs/aces/fr-cont.html; site = urllib.urlopen(url) soup = BS(site) body = soup('table')[1] tds = body.findAll('td') mess = [] for td in tds: mess.append(str(td)) spacer = re.compile(r'td colspan=4 height=10.*') data = [] x=0 for n, t in enumerate(mess): if spacer.match(t): data.append(mess[x:n]) x = n dept = re.compile(r'td colspan=4.*') title = re.compile(r'td colspan=3.*') title2 = re.compile(r'td colspan=2.*') link = re.compile(r'td align=right.*') none = re.compile(r'None') #Strip the html and organize by department group = [] db_list = [] for d in data: pre_list = [] for item in d: if dept.match(item): dept_soup = BS(item) try: dept_contents = dept_soup('a')[0]['name'] pre_list.append(str(dept_contents)) except IndexError: break elif title.match(item) or title2.match(item): title_soup = BS(item) title_contents = title_soup.td.string if none.match(str(title_contents)): pre_list.append(str(title_soup('a')[0]['href'])) else: pre_list.append(str(title_contents)) elif link.match(item): link_soup = BS(item) link_contents = link_soup('a')[1]['href'] pre_list.append(str(link_contents)) db_list.append(pre_list) for db in db_list: for n, dash_space in enumerate(db): dash_space = dash_space.replace('#8211;','-') dash_space = dash_space.replace('nbsp;', ' ') db[n] = dash_space download = re.compile(r'http://.*') for db in db_list: for n, pdf in enumerate(db): if download.match(pdf): filename = re.split('http://.*/',pdf) db[n] = filename[1] #Strip out these departments AgrDep = re.compile(r'Agriculture Department') EPA = re.compile(r'Environmental Protection Agency') FDA = re.compile(r'Food and Drug Administration') key_data = [] for list in db_list: for db in list: if AgrDep.match(db) or EPA.match(db) or FDA.match(db): key_data.append(list) #Get appropriate links from covered departments as well LINK = re.compile(r'^#.*') links = [] for kd in key_data:
Re: Python's BNF
On Feb 29, 7:21 am, [EMAIL PROTECTED] wrote: So, as a nod to the anti-global school of thought, I changed 'ofile' to 'OFILE' so that it would at least look like a global constant. Unfortunately, it's not constant at all. Actually, what you have done is worse. Now you have taken a variable that has mutable state, that changes in various places throughout the module, and made it look like a nice safe dependable constant. _OFILE = None still_reading = False def open_file(name): global _OFILE, still_reading _OFILE = open(name) still_reading = True def process_line(function): function( _OFILE.readline() ) def extract_data(dataline): global _OFILE, still_reading if dataline.startswith(END): close_file() def close_file(): global _OFILE, still_reading OFILE.close() OFILE = None still_reading = False # main code open_file() while still_reading: process_line(extract_data) close_file() Of course, this is a semi-contrived example, but the scattered access of OFILE and still_reading make sorting out this mess a, well, a mess. And I wouldn't really say that just passing around ofile as an argument to every function is necessarily a sufficient solution. I've seen this approach run amok, with a global data structure (representing the current runtime environment plus recent history, plus database connection info, plus sundry other parts of the kitchen sink) that was passed BY PROJECT CODING STANDARDS to EVERY FUNCTION IN EVERY MODULE! Supposedly, this was done to cure access problems to a global data structure. In fact, it solved none of the problems, AND polluted every function signature to boot! The main point is that access to ofile should be limited to those functions that actually update it. If ofile is a global, then there is no control within a module over it, and if not named with a leading '_', it is even visible externally to the module. As Steve H said, ideally this would be a hidden attribute within a class, accessed as self._ofile, so that we would at least know that there is no access beyond the boundaries of the class to this variable. It would also enhance the flexibility of the code - now by instantiating a second instance of this class, I might be able to work with *two* files at the same time, without any global array or list legerdemain. -- Paul class Horse: def lead_to_water(self): self.at_water = True def make_drink(self): raise AttributeException(can't do this) -- http://mail.python.org/mailman/listinfo/python-list
joining strings question
Hi all, I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... I've racked my brain for a while about this and I can't seem to figure it out. Any ideas would be much appreciated. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's BNF
[EMAIL PROTECTED] wrote: Gabriel and Steve, Poor globals! They take such a beating and they really don't deserve it. The use of globals was deprecated, if memory serves, during the structured design craze. Using globals is now considered bad practice, but it's considered bad practice for reasons that don't stand close scrutiny, this being a perfect example. Times move on, but some ideas remain bad ideas, and the *uncontrolled* used of globals is exactly one such bad idea. You seem to believe that the tenets of structured programming have been deprecated, but in fact they have been incorporated into the mainstream. ofile = ... # global writeHTML() def writeHTML(): ofile.write( .. ) writeBody() def writeBody(): ofile.write( ... ) writeEntries() def writeEntries() ofile.write( ... ) writeEntry() def writeEntry(): ofile.write( ... ) ... # fixed to eliminate the evil global writeHTML(ofile) def writeHTML(ofile): ofile.write( .. ) writeBody(ofile) def writeBody(ofile): ofile.write( ... ) writeEntries(ofile) def writeEntries(ofile) ofile.write( ... ) writeEntry(ofile) def writeEntry(ofile): ofile.write( ... ) ... # repeat above for another half dozen subs that also use ofile The code's simpler before the fix. It's also more error-prone and more difficult to read. But since you should probably be encapsulating all this into an HTMLWriter object class anyway I suppose there's no point trying to convince you any further. See below for a point that *might* (just possibly) change your mind, though. So, as a nod to the anti-global school of thought, I changed 'ofile' to 'OFILE' so that it would at least look like a global constant. Then I changed to '_OFILE' as a reminder that this is a modular, not global, constant. Ditto for '_PRODUCTIONS'. Modular constants share exactly none of the coupling problems that globals can have. You'll let me use modular constants, right? Nope. You are changing the form without changing the substance, so my objections still stand. Your code makes the assumption that nobody else will ever want to reuse it, and by doing so almost guarantees that nobody will. The principles of modularity are there for a reason, and while I can admit I have written similar programs myself as quick throwaways I would never dream of promoting them as examples of sound practice. I can't believe I am going to have to mention coupling and coherence as useful design principles yet again. One final issue: in cases where your functions and methods do significant work, the ofile argument is passed into the local namespace of the function or method. This means that no late-binding name lookup need be performed, which represents a substantial improvement in execution speed in the event there are many uses of the same global. Or perhaps you would rather optimize that by writing ofile = ... writeHTML() def writeHTML(): global ofile my_ofile = ofile my_ofile.write( .. ) writeBody() I wish you'd stop trying to defend this code and simply admit that it's just a throwaway program to which no real significance should be attached. *Then* I'll leave you alone ;-) regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Is crawling the stack bad? Why?
I will (mostly)... I knew it was bad code and a total hack, I just was looking for a concise reason as to why. I appreciate the comments, guys... thanks! There is another one: crawling the stack is O(n), whilst using thread-local storage is O(1) Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
[EMAIL PROTECTED] wrote: Hi all, I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... I've racked my brain for a while about this and I can't seem to figure it out. Any ideas would be much appreciated. As others already said, the data structure is quite unfit. Therefore I give you one of the ugliest piece of code I've produced in years: r = [] for i in xrange(0, len(data), 7): r.append([data[i], ' '.join((data[i+1], data[i+2],)), data[i+3], ' '.join((data[i+4], data[i+5],)), data[i+6]]) print r Cheers, RB -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's BNF
Steve Holden wrote: I wish you'd stop trying to defend this code and simply admit that it's just a throwaway program to which no real significance should be attached. *Then* I'll leave you alone ;-) You're hurting my program's feelings! Actually, I intend to keep this program as the nice HTML version of the BNF that it produces is a big advance over the source from GvR. I'll rerun this from time to time to keep my BNF up to date. A second intent was to have the next sorry coder who searched for 'Python BNF' get a better answer than I got, which is already the case. The discussion re globals is entirely academic, of course, with this little program being merely illustrative. -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
On Feb 29, 4:09 pm, [EMAIL PROTECTED] wrote: Hi all, I have some data with some categories, titles, subtitles, and a link to their pdf and I need to join the title and the subtitle for every file and divide them into their separate groups. So the data comes in like this: data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] What I'd like to see is this: [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'], ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc... For any kind of data partitioning, you should always keep `itertools.groupby` in mind as a possible solution: [code] import itertools as it data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1', 'NOTICES','title2','subtitle2','pdf', 'title3','subtitle3','pdf'] def partition(s): return s == s.upper() #first method newdata = [] for k,g in it.groupby(data, partition): if k: newdata.append(list(g)) else: newdata[-1].extend(list(g)) for item in newdata: print item print #second method keys = [] vals = [] for k,g in it.groupby(data, partition): if k: keys.append(list(g)[0]) else: vals.append(list(g)) newdata = dict(zip(keys, vals)) print newdata [/code] [output] ['RULES', 'title', 'subtitle', 'pdf', 'title1', 'subtitle1', 'pdf1'] ['NOTICES', 'title2', 'subtitle2', 'pdf', 'title3', 'subtitle3', 'pdf'] {'RULES': ['title', 'subtitle', 'pdf', 'title1', 'subtitle1', 'pdf1'], 'NOTICES' : ['title2', 'subtitle2', 'pdf', 'title3', 'subtitle3', 'pdf']} [/output] HTH Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: Run wxPython app remotely under XWindows
On Feb 28, 7:49 pm, Sean DiZazzo [EMAIL PROTECTED] wrote: On Feb 28, 5:26 pm, Sean DiZazzo [EMAIL PROTECTED] wrote: On Feb 28, 3:50 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Sean DiZazzo wrote: Is there something special you have to do to get a wxPython app to run remotely under xwindows? My Tkinter apps always automatically work that way, so I was surprised to even be confronted with this problem. Could you please provide more detail? My wxPython apps run perfectly remotely in the X Window System like this: $ ssh some-other-machine $ DISPLAY=:0 ./my_app.py Should wxPython apps work this way? Do you think it's something with the server? Just to close the loop I think think this is a problem with the ssh server. ~Sean If it's not the server, then please post the issue to the wxPython list. They can probably help: http://wxpython.org/maillist.php Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's BNF
Paul McGuire wrote: plus sundry other parts of the kitchen sink) that was passed BY PROJECT CODING STANDARDS to EVERY FUNCTION IN EVERY MODULE! Supposedly, this was done to cure access problems to a global data structure. Beautiful example of how totally stupid actions can be taken in the name of avoiding globals. Wikipedia and PEP 8 both agree with me, re globals. OK way to eliminate chained argument passing, but keep them within one module. _OFILE, by the way, is the output file. It's contract with the rest of the module was to be available for writing to anyone with data to write. I use the Java convention of ALLCAPS for naming things that I would declare as CONSTANT if Python had such a declaration. -- http://mail.python.org/mailman/listinfo/python-list
Re: is there enough information?
On Feb 29, 7:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 00:54:44 -0800 (PST), [EMAIL PROTECTED] declaimed the following in comp.lang.python: On Feb 28, 2:30 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: ### I smell Java burning... +1 QOTW Mistah Kurtz - he dead. (http://en.wikiquote.org/wiki/Heart_of_Darkness) ;-] -- http://mail.python.org/mailman/listinfo/python-list
Re: Raising exception on STDIN read
On 2008-02-28, Gabriel Genellina [EMAIL PROTECTED] wrote: I hope it's more clear now. Python objects are only meaningful *inside* a Python program, but an *external* program can't use them directly. Yes, sorry. This is what I was getting hung up on. My thinking was that subprocess was orchestrating it such that any reads would be funneled back into the Python object. Granted, that was my gut reaction to it. Thinking (and playing) with it a bit more I now realize that that makes no sense. Thank you for reminding me about the seperation of OS and Python files. Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem round-tripping with xml.dom.minidom pretty-printer
Ben Butler-Cole wrote: Hello I have run into a problem using minidom. I have an HTML file that I want to make occasional, automated changes to (adding new links). My strategy is to parse it with minidom, add a node, pretty print it and write it back to disk. However I find that every time I do a round trip minidom's pretty printer puts extra blank lines around every element, so my file grows without limit. I have found that normalizing the document doesn't make any difference. Obviously I can fix the problem by doing without the pretty-printing, but I don't really like producing non-human readable HTML. Here is some code that shows the behaviour: import xml.dom.minidom as dom def p(t): d = dom.parseString(t) d.normalize() t2 = d.toprettyxml() print t2 p(t2) p('abc//b/a') Does anyone know how to fix this behaviour? If not, can anyone recommend an alternative XML tool for simple tasks like this? Hi, The last line of p() calls itself: it is an unconditional recursive call so, no matter what it does, it will never stop. And since p() also prints something, calling it will print endlessly. By removing this line, you get something like: ?xml version=1.0 ? a b c/ /b /a That seems sensible, imo. Was that what you wanted? An additional thing to keep in mind is that toprettyxml does not print an XML identical to the original DOM tree: it adds newlines and tabs. When parsed again these blank characters are inserted in the DOM tree as character nodes. If you toprettyxml an XML document twice in a row, then the second one will also add newlines and tabs around the newlines and tabs added by the first. Since you call toprettyxml an infinite number of times, it is expected that lots of blank characters appear. Finally, normalize() is supposed to merge consecutive sibling character nodes, however it will never remove character contents even if they are blank. That means that several character nodes will be replaced by a single one whose content is the concatenation of the respective content of the original nodes. Clear enough? Cheers, RB -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem round-tripping with xml.dom.minidom pretty-printer
The last line of p() calls itself: it is an unconditional recursive call so, no matter what it does, it will never stop. And since p() also prints something, calling it will print endlessly. Sorry, I wasn't clear. I realize that this recurses endlessly. The problem is that it also adds blank lines endlessly. By removing this line, you get something like: ?xml version=1.0 ? a b c/ /b /a That seems sensible, imo. Was that what you wanted? Sure. That's fine unless you then re-parse this out put and print it again in which case you get the behaviour you describe: An additional thing to keep in mind is that toprettyxml does not print an XML identical to the original DOM tree: it adds newlines and tabs. When parsed again these blank characters are inserted in the DOM tree as character nodes. If you toprettyxml an XML document twice in a row, then the second one will also add newlines and tabs around the newlines and tabs added by the first. Since you call toprettyxml an infinite number of times, it is expected that lots of blank characters appear. Right. That's the behaviour I'm asking about, which I consider to be problematic. I would expect a module providing a parser and pretty- printer (not just for XML parsers) to be able to conservatively round- trip. As far as I can see (and your comments back this up) minidom doesn't have this property. Unless anyone knows how to get it to behave that way... Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
On Feb 29, 8:12 am, Steve Holden [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On Feb 29, 5:56 am, Steve Holden [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On Feb 27, 6:02 pm, Tamer Higazi [EMAIL PROTECTED] wrote: Hi! Can somebody of you make me a sample how to define a function based on call by reference ??? I am a python newbie and I am not getting smart how to define functions, that should modify the variable I passed by reference. thanks in advance Tamer If it's a mutable object, avoid the pitfalls of rebinding the parameter, and just modify the object. BAD: def f( a ): a= { 'this': 'that' } GOOD: def f( a ): a.clear() a[ 'this' ]= 'that' BETTER: class Thang: pass def f(a): a.this = that thang = Thang() f(thang) [please refrain from quoting signatures in your replies] [better still, use a mailer that omits them from the quote!] - Show quoted text - What does __coerce__ look like, so you could operate on a.this without accessing 'this' member every time? For numbers maybe, but what about __getattr__( self, name ): return getattr( self.this, name ) for strings? Then thang.this= that; thang.find( 'at' ) - thang.this.find( 'at' ). Awesome! Where did __coerce__ come from? Stick with the main party, please. __coerce__ is an old mechanism intended to be used to bring numeric types into alignment, and AFAICS has nothing at all to do with whatever idea you are suggesting. As near as I can make out you appear to want to have thang delegate certain of its method to thang.this. The easiest way to do that would be to implement a __getattr__() in the Thang class to do so, but remember that it won't be called for cases where the class has a real attribute with the correct name. In your example, class Thang: pass def f(a): a.this = that thang = Thang() f(thang) Thang -wasn't- doing anything else. It can delegate everything. (Thang operator=( const Thang );.) Then there's __getattribute__, which is called unconditionally, just in case you implement something in Thang besides 'this' on accident. -- http://mail.python.org/mailman/listinfo/python-list
Re: Indentation and optional delimiters
On Feb 29, 8:59 am, Steve Holden [EMAIL PROTECTED] wrote: Steve Holden wrote: [EMAIL PROTECTED] wrote: [...] If you want a computer language to model human thought, then is there even such thing as subclassing? Kindly try to limit your ramblings to answerable questions. Without keen insight into the function of the mind that is currently available to the psychological and psychiatric professions, such philosophical speculation is unlikely to do anyone any good, least of all you. ^available^unavailable^ -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ In the neuroscience book I read (_Neuroscience_), my favorite section was entitled, Mechanisms of Long-Term Synaptic Plasticity in the Mammalian Nervous System. Maybe reptiles do, I guess. subclasses Food... and Python -is- a reptile... -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem round-tripping with xml.dom.minidom pretty-printer
Ben Butler-Cole wrote: An additional thing to keep in mind is that toprettyxml does not print an XML identical to the original DOM tree: it adds newlines and tabs. When parsed again these blank characters are inserted in the DOM tree as character nodes. If you toprettyxml an XML document twice in a row, then the second one will also add newlines and tabs around the newlines and tabs added by the first. Since you call toprettyxml an infinite number of times, it is expected that lots of blank characters appear. Right. That's the behaviour I'm asking about, which I consider to be problematic. I would expect a module providing a parser and pretty- printer (not just for XML parsers) to be able to conservatively round- trip. As far as I can see (and your comments back this up) minidom doesn't have this property. Unless anyone knows how to get it to behave that way... minidom --any DOM parser, btw-- has no means to know which blank character is a pretty print artefact or actual blank content from the original XML. You could write a function that strips all-blank nodes recursively down the elements tree, before doing so I suggest you take a look at section 2.10 of http://www.w3.org/TR/REC-xml/. RB -- http://mail.python.org/mailman/listinfo/python-list
Re: feedback requested
On Feb 29, 5:52 am, [EMAIL PROTECTED] wrote: On Feb 29, 12:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 08:09:01 -0800 (PST), [EMAIL PROTECTED] declaimed the following in comp.lang.python: My goal is to return Deadlock from acquire() if its blocking would directly create deadlock. Basic example: [ The safeguard is never worth the cost.] I am NOT going to look any further into this line... I am. Can anyone tell me if this deadlocks? Sample test block included, feel free to shred. Does not currently permit reentrance. Simplification of above, denies reentrant calls, eliminates ThreadDesc class. Getting a /beep/ on threaded printing in 3.0, none in 2.5. Got a case too, where 2444 == 2444 but 2444 is not 2444. Of course they aren't guaranteed to, but I am testing for identity in the return from thread.get_ident(). The 'is' operator compares the identity of two objects. [After] a = 1; b = 1, a and b may or may not refer to the same object. Humph. Anyway, without further ado, direct to you at no extra cost, thin and narrow for your cut-and-pasting convenience, drumroll please. -=-=-=-=-=-=-=- from __future__ import with_statement import threading import thread from time import sleep from collections import deque try: from collections import namedtuple ThreadLockPair= namedtuple( ThreadLockPair, thread lock ) except: class ThreadLockPair: def __init__( self, thread, lock ): self.thread, self.lock= thread, lock Acquires, Deadlocks, Timesout= \ object(), object(), object() resultdict= { Acquires: 'Acquires', Deadlocks: 'Deadlocks', Timesout: 'Timesout' } class Trylock: _count= 0 _alllocks= set() _goplock= threading.Lock() def __init__( self, *ar, **kwar ): self._lock= threading.Lock() self._owner= None self._waiters= set() Trylock._alllocks.add( self ) self.id= Trylock._count Trylock._count+= 1 def __repr__( self ): return 'Trylock %i'% self.id def acquire( self ): caller= thread.get_ident() with Trylock._goplock: if caller in self._waiters: return Deadlocks if self._cycles(): return Deadlocks self._waiters.add( caller ) assert self._lock.acquire() with Trylock._goplock: assert self._owner is None self._owner= caller return Acquires def release( self ): with Trylock._goplock: self._waiters.remove( self._owner ) self._owner= None self._lock.release() def __enter__( self ): if self.acquire() is Deadlock: raise Exception( 'Deadlock' ) def __exit__( self, t, v, tb ): self.release() def _cycles( self, thd= None ): lck= self if thd is None: thd= thread.get_ident() edges= [ ThreadLockPair( th, ck ) for ck in Trylock._alllocks for th in ck._waiters ] inpair= ThreadLockPair( thd, lck ) edges.append( inpair ) d= deque( [ e for e in edges if e.lock is lck ] ) while d: cur= d.popleft() locks= [ e.lock for e in edges if e.thread== cur.thread and e.lock is not cur.lock ] for ck in locks: nexts= [ e for e in edges if ck is e.lock and e.thread!= cur.thread ] if inpair in nexts: return True d.extend( nexts ) return False def main( func ): if __name__== '__main__': func() @main def fmain(): import random locks= [ Trylock() for _ in range( 20 ) ] def th1( i ): while 1: lock= random.choice( locks ) ret= lock.acquire() if ret is not Acquires: continue print( '%i th lock %s acquire\n'% ( i, lock ) ), sleep( .0001 ) lock2= random.choice( locks ) if lock2.acquire() is Acquires: print( '%i th lock2 %s acquire\n'% ( i, lock ) ), sleep( .0001 ) lock2.release() lock.release() print( '%i th lock %s release\n'% ( i, lock ) ), ths= [ threading.Thread( target= th1, args= ( i, ) ) for i in range( 6 ) ] [ th.start() for th in ths ] -=-=-=-=-=-=- -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested module import clutters package namespace?
[EMAIL PROTECTED] wrote: I'd be grateful for help with a problem of package and module=20 namespaces. The behaviour I observe is unexpected (to me), and I=20 couldn't find the answer in the docs, the tutorial, or the mailing=20 list archive. So here we go: I have a package named 'pack'. Apart from the '__init__.py' file the=20 directory contains two modules 'x.py' and 'y.py': pack/ __init__.py x.py y.py The files have the following contents: __init__.py import x = x.py === import y = y.py === pass = I then do import pack This (1) introduces variable 'x' bound to module 'pack.x' in pack's namespace (expected) (2) introduces variable 'q' bound to module 'pack.y' in x's namespace (expected) but also (3) introduces variable 'y' bound to module 'pack.y' in pack's namespace (*totally unexpected*) That's right, Python has behaved like this since packages were introduced. There's no doubt that this is intentional, but the state of the documentation is rather embarrassing. The reference manual's explanation of package imports consists largely of this reference: XXX Can't be bothered to spell this out right now; see the URL http://www.python.org/doc/essays/packages.html for more details, The referenced essay is over 10 years old and unsurprisingly somewhat out of date. This behaviour gets even more exciting when circular imports are involved. See for example message [EMAIL PROTECTED] on this list. -M- -- http://mail.python.org/mailman/listinfo/python-list
Re: call by reference howto????
In article [EMAIL PROTECTED], Steve Holden [EMAIL PROTECTED] wrote: As near as I can make out you appear to want to have thang delegate certain of its method to thang.this. The easiest way to do that would be to implement a __getattr__() in the Thang class to do so, but remember that it won't be called for cases where the class has a real attribute with the correct name. ...unless thang is a new-style class using __getattribute__()... (Just a drive-by nitpicking. ;-) -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ All problems in computer science can be solved by another level of indirection. --Butler Lampson -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
On Fri, 29 Feb 2008 08:18:54 -0800, baku wrote: return s == s.upper() A couple of people in this thread have used this to test for an upper case string. Is there a reason to prefer it to s.isupper() ? -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
I V wrote: On Fri, 29 Feb 2008 08:18:54 -0800, baku wrote: return s == s.upper() A couple of people in this thread have used this to test for an upper case string. Is there a reason to prefer it to s.isupper() ? For my part? forgetfulness brought on by underuse of .isupper() -tkc -- http://mail.python.org/mailman/listinfo/python-list
at-exit-thread
The Python main interpreter has an at-exit list of callables, which are called when the interpreter exits. Can threads have one? What's involved, or is the best way merely to subclass Thread? -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
I V wrote: On Fri, 29 Feb 2008 08:18:54 -0800, baku wrote: return s == s.upper() A couple of people in this thread have used this to test for an upper case string. Is there a reason to prefer it to s.isupper() ? In my case you can put it down to ignorance or forgetfulness, depending on how forgiving you feel. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: why not bisect options?
[Robert Bossy] I thought it would be useful if insort and consorts* could accept the same options than list.sort, especially key and cmp. If you're going to do many insertions or searches, wouldn't it be *much* more efficient to store your keys in a separate array? The sort() function guarantees that it calls the key function exactly once for each member of the list. With and bisect/insort, successive searches can call the key function over and over again with the same value. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
On Feb 29, 1:55 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] schrieb: The Python main interpreter has an at-exit list of callables, which are called when the interpreter exits. Can threads have one? What's involved, or is the best way merely to subclass Thread? Is that some sort of trick-question? class MyThread(Thread): def run(self): while some_condition: do_something() do_something_after_the_thread_ends() The atexit stuff is for process-termination which is/may be induced by external signals - which is the reason why these callbacks extist. Threads don't have that, thus no need. That depends. If a thread adds an object it creates to a nonlocal collection, such as a class-static set, does it have to maintain a list of all such objects, just to get the right ones destroyed on completion? Processes destroy their garbage hassle-free; how can threads? And don't forget Thread.run( self ) in the example, if anyone ever wants to make use of the 'target' keyword. -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
I V wrote: On Fri, 29 Feb 2008 08:18:54 -0800, baku wrote: return s == s.upper() A couple of people in this thread have used this to test for an upper case string. Is there a reason to prefer it to s.isupper() ? Note that these tests are not equivalent: s = 123 s.isupper(), s.upper() == s (False, True) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: joining strings question
On Feb 29, 7:56 pm, I V [EMAIL PROTECTED] wrote: On Fri, 29 Feb 2008 08:18:54 -0800, baku wrote: return s == s.upper() A couple of people in this thread have used this to test for an upper case string. Is there a reason to prefer it to s.isupper() ? Premature decreptiude, officer... Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
[EMAIL PROTECTED] schrieb: The Python main interpreter has an at-exit list of callables, which are called when the interpreter exits. Can threads have one? What's involved, or is the best way merely to subclass Thread? Is that some sort of trick-question? class MyThread(Thread): def run(self): while some_condition: do_something() do_something_after_the_thread_ends() The atexit stuff is for process-termination which is/may be induced by external signals - which is the reason why these callbacks extist. Threads don't have that, thus no need. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
On Feb 29, 2:12 pm, [EMAIL PROTECTED] wrote: On Feb 29, 1:55 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] schrieb: The Python main interpreter has an at-exit list of callables, which are called when the interpreter exits. Can threads have one? What's involved, or is the best way merely to subclass Thread? Is that some sort of trick-question? class MyThread(Thread): def run(self): while some_condition: do_something() do_something_after_the_thread_ends() The atexit stuff is for process-termination which is/may be induced by external signals - which is the reason why these callbacks extist. Threads don't have that, thus no need. That depends. If a thread adds an object it creates to a nonlocal collection, such as a class-static set, does it have to maintain a list of all such objects, just to get the right ones destroyed on completion? Processes destroy their garbage hassle-free; how can threads? And don't forget Thread.run( self ) in the example, if anyone ever wants to make use of the 'target' keyword.- Hide quoted text - Two snippets from the docs that are worrisome: # If they invoke anything in threading.py that calls currentThread(), they # leave an entry in the _active dict forever after. -threading.py thread.get_ident() ... Thread identifiers may be recycled when a thread exits and another thread is created. -thread module doc If you try to look up something by thread_id, you can get a terminated thread's old resources. at-thread-exit could prevent it. What is the scope of a resource a daemon thread allocates? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to subclass a built-in int type and prevent comparisons
Bronner, Gregory [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | The native implementation of int goes to great lengths to allow | illogical comparisons such as the one below. | import xml as x | x | module 'xml' from 'c:\python25\lib\xml\__init__.pyc' | | x4 | True | x4 | False Python once made all objects comparable. No longer true. 'Illogical' comparisons will raise exceptions in 3.0 but must be maintained in 2.x for back compatibility. tjr -- http://mail.python.org/mailman/listinfo/python-list
How to subclass a built-in int type and prevent comparisons
I'm trying to create a type-safe subclass of int (SpecialInt) such that instances of the class can only be compared with ints, longs, and other subclasses of SpecialInt -- I do not want them to be compared with floats, bools, or strings, which the native int implementation supports. Obviously, I could overload __lt_, __eq__, __le__, etc, and write a bunch of boilerplate code. Should this code throw an exception if the types are not comparable? What would I lose by doing that? Is this code likely to be efficient? def __gt__(self, other): if(other is self): return False if(self.__isComparable(other)): return int(self)int(other) else: raise ValueError(str(self) + and + str(other) + are not comparable) The native implementation of int goes to great lengths to allow illogical comparisons such as the one below. import xml as x x module 'xml' from 'c:\python25\lib\xml\__init__.pyc' x4 True x4 False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. IRS Circular 230 Disclosure: Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein. -- http://mail.python.org/mailman/listinfo/python-list
Re: convert string number to real number - ValueError: invalid literal for int() with base 10: '2'
You have to get rid of the double quotes first. you mean replace them with nothing? li[4].replace('','') once i do that, i should be able to use them as numbers. -- http://mail.python.org/mailman/listinfo/python-list
Re: why not bisect options?
On Feb 29, 1:15 pm, Raymond Hettinger [EMAIL PROTECTED] wrote: [Robert Bossy] I thought it would be useful if insort and consorts* could accept the same options than list.sort, especially key and cmp. If you're going to do many insertions or searches, wouldn't it be *much* more efficient to store your keys in a separate array? The sort() function guarantees that it calls the key function exactly once for each member of the list. With and bisect/insort, successive searches can call the key function over and over again with the same value. Raymond Since sort time is at least linear, sort ( key, obj ) pairs; return obj's. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to subclass a built-in int type and prevent comparisons
On Feb 29, 3:09 pm, Terry Reedy [EMAIL PROTECTED] wrote: Bronner, Gregory [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | The native implementation of int goes to great lengths to allow | illogical comparisons such as the one below. | import xml as x | x | module 'xml' from 'c:\python25\lib\xml\__init__.pyc' | | x4 | True | x4 | False Python once made all objects comparable. No longer true. 'Illogical' comparisons will raise exceptions in 3.0 but must be maintained in 2.x for back compatibility. tjr Tell Wall. But why not [ 2, 3 ]= 2? Back to your question, another option is to not subclass. -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Ross Ridge [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | Ross Ridge wrote: | You're just going to have to accept that there that there is no | concensus on this issue and there never was. | | Steven D'Aprano [EMAIL PROTECTED] wrote: | But that's not true. The consensus, across the majority of people (both | programmers and non-programmers alike) is that 1/2 should return 0.5. | | You're deluding yourself. As a major participant in the discussion, who initially opposed the change, I think Steven is right. | If there were a concensus then this issue then | it wouldn't be so controversial. The controversy was initially inflamed by issues that did not directly bear on the merit of the proposal. Worst was its cloaking it in a metaphysical argument about the nature of integers. It also did not help that Guido initially had trouble articulating the *practical*, Pythonic reason for the proposal. To me, the key is this (very briefly): The current overloading of '/' was copied from C. But Python is crucially different from C in that expressions can generally be generic, with run-time rather than compile time typing of variables. But there are no practical use cases that anyone ever presented for expr_a / expr_b having two different numerical values, giving fixed numerical values for expr_a and expr_b, depending on the number types of the two expressions. Beyond the change itself, another issue was its timing. When I proposed that the version making 1/2=.5 the default be called 3.0, and Guido agreed, many who agreed with the change in theory but were concerned with stability of the 2.x series agreed that that would make it more palatable. A third issue was the work required to make the change. The future mechanism eased that, and the 2to3 conversion program will also issue warnings. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Arnaud Delobelle [EMAIL PROTECTED] wrote in message | What screws me is that I'm going to have to type p//q in the future. When I compare that pain to the gain of not having to type an otherwise extraneous 'float(...)', and the gain of disambiguating the meaning of a/b (for builtin numbers at least), I think there will be a net gain for the majority. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: convert string number to real number - ValueError: invalid literal for int() with base 10: '2'
davidj411 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] |i am parsing a cell phone bill to get a list of all numbers and the | total talktime spend on each number. | | i already have a unique list of the phone numbers. | now i must go through the list of numbers and add up the totals for | each number. | here is the function i wrote to get one number at a time's total | talktime. | | def getsinglenumbertalktime(number,talktime): | for line in file[0:-2]: ... | | talktime = talktime + li[5] | return talktime It would appear that you are calling this function and rescanning scanning file for each number. It would be more efficient to scan the file once, for each line parsing out the number and minutes and incrementing the minutes for that number. Also, if you had printed repr(li[5]) and len(li[5]) instead of or in addition to just li[5] (which prints str(li[5]), a='2' print a, repr(a), len(a) 2 '2' 3 you might have seen for yourself the problem that the string contains quote marks and not just digits. Repr and len are useful debugging aids. tjr -- http://mail.python.org/mailman/listinfo/python-list
Telnet versus telnetlib
I have used command-line telnet to login to a host, paste into the window a small XML file, and then ^] to leave the window, and quit. This results in a page (described by the XML file) being printed on a printer. When I do an analogous process using telnetlib, I get no debug output, and most importantly, when I send the XML file to the host, I get no printed page. Unfortunately, I do not have access to the host to do troubleshooting there, so I have to feel my way around. Any suggestions on what might be going wrong? Thanks, Sean In [1]: import telnetlib In [2]: tn=telnetlib.Telnet() In [3]: tn.set_debuglevel(1) In [4]: tn.open('labmatr',56423) In [12]: tn.write(?xml version=1.0 encoding=UTF-8? : labels _FORMAT=C:\labels\anzick_primary_sample.lbl _PRINTERNAME=Anzick1 _QUANTITY=1 _JOBNAME=levineja_2_1198259890656 : label : variable name=barcodeF3B85FCE-55CF-4541-80EB- D1450377F7E0/variable : variable name=Labmatrix_External_BarcodeBP10004 0701/variable : /label : /labels) Telnet(labmatr,56423): send '?xml version=1.0 encoding=UTF-8? \nlabels _FORMAT=C:\\labels\x07nzick_primary_sample.lbl _PRINTERNAME=Anzick1 _QUANTITY=1 _JOBNAME=levineja_2_1198259890656\nlabel\nvariable name=barcodeF3B85FCE-55CF-4541-80EB-D1450377F7E0/variable \nvariable name=Labmatrix_External_BarcodeBP10004 0701/ variable\n/label\n/labels' In [13]: tn.write(\n) Telnet(labmatr,56423): send '\n' In [14]: tn.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
On Feb 29, 2:12 pm, [EMAIL PROTECTED] wrote: If a thread adds an object it creates to a nonlocal collection, such as a class-static set, does it have to maintain a list of all such objects, just to get the right ones destroyed on completion? Yes. Processes destroy their garbage hassle-free; how can threads? If you're asking is there some automatic way to associate a external resource with a thread so that it's deleted when the thread is done then the answer (to the best of my knowledge) is no. You would have to take care of these details yourself once something outside your thread got a reference to the resource. If you keep all references to the resource inside the thread, then you can use the __del__ method of the resource to take any cleanup actions. Also, processes don't always destroy their garbage hassle free if you consider inter process communication. For instance, if you create an object in one process then serialize it and pass it to another process through e.g. an XMLRPC call, there's no automatic way for the other process to be notified that the originating process exited. You'd have to build that feature in. Exact same thing with threads. And don't forget Thread.run( self ) in the example, if anyone ever wants to make use of the 'target' keyword. I don't understand what you're referring to. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's BNF
En Fri, 29 Feb 2008 15:01:49 -0200, [EMAIL PROTECTED] escribió: _OFILE, by the way, is the output file. It's contract with the rest of the module was to be available for writing to anyone with data to write. I use the Java convention of ALLCAPS for naming things that I would declare as CONSTANT if Python had such a declaration. (Uhm... I'd consider _OFILE a variable, not a constant!) What if you want to generate many files, maybe one per declaration? You might want to reuse some of those functions, but you can't do that with a global _OFILE. About the generated page: I think it would be more useful if each symbol links to its definition, instead of showing an alert(). This way it's easier to navigate the tree, specially with complex declarations. You can place the current text into a title attribute and most browsers will show it as a tooltip. Also it would be nice if at least it were valid HTML, and semantically correct - using DL, DT and DD tags instead of tables, by example. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
On Feb 29, 10:10 pm, Terry Reedy [EMAIL PROTECTED] wrote: Arnaud Delobelle [EMAIL PROTECTED] wrote in message | What screws me is that I'm going to have to type p//q in the future. When I compare that pain to the gain of not having to type an otherwise extraneous 'float(...)', and the gain of disambiguating the meaning of a/b (for builtin numbers at least), I think there will be a net gain for the majority. You may be right. I can see the rationale for this change (although many aspects feel funny, such as doing integral arithmetic with floats, i.e. 3.0//2.0). Perhaps it'll be like when I quit smoking six years ago. I didn't enjoy it although I knew it was good for me... And now I don't regret it even though I still have the occasional craving. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: convert string number to real number - ValueError: invalid literal for int() with base 10: '2'
On Fri, 29 Feb 2008 13:32:15 -0800 (PST) [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: You have to get rid of the double quotes first. you mean replace them with nothing? li[4].replace('','') Sure, that will do. However, look at the csv module for another way of handling this. once i do that, i should be able to use them as numbers. You still need to apply int() or float() to the result. -- D'Arcy J.M. Cain [EMAIL PROTECTED] | 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: more pythonic
Paul McGuire wrote: In general, whenever you have: someNewList = [] for smthg in someSequence: if condition(smthg): someNewList.append( elementDerivedFrom(smthg) ) replace it with: someNewList = [ elementDerivedFrom(smthg) for smthg in someSequence if condition(smthg) ] What is the gain? (Real question.) I think the first is often easier to read. Is the second more efficient? Also, I think list comprehensions are often easier to read as equivalent generator expressions: someNewList = list( elementDerivedFrom(smthg) for smthg in someSequence if condition(smthg) ) Tastes vary of course. Cheers, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
Getting a free TCP port blocking it
Hi all, I'm attempting to block a TCP port from any other application from using it until I free it from python, this is so that: 1). Generate a random free user-space port 2). Generate the script for the external program with the port 3). Free the port before external program execution. This is what I have so far: class portFinder: port = None socketobj = None def __init__(self): i=2000 portStatus=0 while portStatus!=111: sockobj = socket(AF_INET, SOCK_STREAM) portStatus=sockobj.connect_ex(('localhost',i)) if portStatus!=111: i+=1 sockobj.close() self.socketobj=socket(AF_INET, SOCK_STREAM) self.socketobj.bind(('localhost',i)) self.socketobj.setblocking(1) self.port=i def getPort(self): return self.port def free(self): self.socketobj.close() -- http://mail.python.org/mailman/listinfo/python-list
Python COM automation - Controlling Microsoft Agent
Here's a snippet of code for pythoners to enjoy. Make the Microsoft genie speak text and dance about! ''' Requires: -- comtypes from: http://starship.python.net/crew/theller/comtypes MS Agent from: http://www.microsoft.com/msagent To do: -- 1. List available animations using self.char.AnimationNames above 2. Get a RequestComplete event when the animation is done playing. 3. Change the voice to something else. If you can do any of that, please send me a copy at [EMAIL PROTECTED] Thanks! ''' import comtypes import comtypes.client import time class Genie: CLSID_AgentServer = {D45FD31B-5C6E-11D1-9EC1-00C04FD7081F} IID_IAgentEx= {48D12BA0-5B77-11d1-9EC1-00C04FD7081F} CLSCTX_SERVER = 5 char= None agent = None animlist= ['Acknowledge', 'Alert', 'Announce', 'Blink', 'Confused', 'Congratulate', 'Congratulate_2', 'Decline', 'DoMagic1', 'DoMagic2', 'DontRecognize', 'Explain', 'GestureDown', 'GestureLeft', 'GestureRight', 'GestureUp', 'GetAttention', 'GetAttentionContinued', 'GetAttentionReturn', 'Greet', 'Hearing_1', 'Hearing_2', 'Hearing_3', 'Hearing_4', 'Idle1_1', 'Idle1_2', 'Idle1_3', 'Idle1_4', 'Idle1_5', 'Idle1_6', 'Idle2_1', 'Idle2_2', 'Idle2_3', 'Idle3_1', 'Idle3_2', 'LookDown', 'LookDownBlink', 'LookDownReturn', 'LookLeft', 'LookLeftBlink', 'LookLeftReturn', 'LookRight', 'LookRightBlink', 'LookRightReturn', 'LookUp', 'LookUpBlink', 'LookUpReturn', 'LookUpLeft', 'LookUpLeftBlink', 'LookUpLeftReturn', 'LookUpRight', 'LookUpRightBlink', 'LookUpRightReturn', 'MoveDown', 'MoveLeft', 'MoveRight', 'MoveUp', 'Pleased', 'Process', 'Processing', 'Read', 'ReadContinued', 'ReadReturn', 'Reading', 'RestPose', 'Sad', 'Search', 'Searching', 'StartListening', 'StopListening', 'Suggest', 'Surprised', 'Think', 'Thinking', 'Uncertain', 'Wave', 'Write', 'WriteContinued', 'WriteReturn', 'Writing', 'Hide', 'Show'] class Curry: def __init__(self, fun, *args, **kwargs): self.fun = fun self.args = args[:] self.kwargs = kwargs.copy() def __call__(self, *args, **kwargs): if kwargs and self.kwargs: kw = self.kwargs.copy() kw.update(kwargs) else: kw = kwargs or self.kwargs return self.fun(*(self.args + args), **kw) def __init__(self): self.agent = comtypes.client.CreateObject(self.CLSID_AgentServer, self.CLSCTX_SERVER, self.IID_IAgentEx) try: self.agent.Connected = True except comtypes.COMError, args: if args[0] == -2147418094: self.agent = comtypes.client.CreateObject(self.CLSID_AgentServer, self.CLSCTX_SERVER, self.IID_IAgentEx) self.agent.Connected = True else: raise self.agent.Characters.Load(mychar) self.char = self.agent.Characters(mychar) self.char.Show() # How do I get the list of names from this? print 'List of animations:' anims = self.char.AnimationNames print anims def __getattr__(self, attrname): if attrname in self.animlist: return self.Curry(self.char.Play, attrname) else: return object.__getattr__(self, attrname) def Move(self, x, y): self.char.MoveTo(x, y) def Play(self, anim): try: retval = self.char.Play(anim) except: print 'animation %s not found' % anim return def GestureAt(self, x, y): self.char.GestureAt(x, y) def Speak(self, text): retval = self.char.Speak(text) def Think(self, text): self.char.Think(text) def ChooseAgent(self): self.agent.ShowDefaultCharacterProperties() def GetAnimations(self): return self.animlist def Stop(self): self.char.StopAll() def main2(): genie = Genie() genie.Move(400, 300) genie.Greet() genie.GestureAt(400, 600) genie.Speak(You killed my father, prepare to die.) time.sleep(5) genie.Idle2_1() genie.Think('Using Python to command me is cool!') anims = genie.GetAnimations() for i in range(len(anims)): genie.Think('%d - %s' % (i, anims[i])) genie.Play(anims[i]) time.sleep(5) genie.Stop() main2() -- http://mail.python.org/mailman/listinfo/python-list
Re: (Newbie) Help with sockets.
On Feb 29, 9:42 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2008-02-29, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: You claim to be writing a telnet server, yet I don't see any code that actually implements the telnet protocol. Different telnet clients default to different modes, so if you want them in a certain mode, you have to put them in the desired mode by implementing the telnet feature negotiation protocol. OP here - sorry for the confusion, it's just a listener program that people connect to via telnet. That's what a telnet server _is_: something that people connect to via telnet. I'll take a look at the Twisted engine and see if that helps at all. Make sure it has telnet protocol support, or you're going to end up with similar problem. -- Grant Edwards grante Yow! Four thousand at different MAGNATES, MOGULS visi.com NABOBS are romping in my gothic solarium!! Thanks for all the responses, the Twisted engine was exactly what I needed. Thanks for all your help guys! -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
On Feb 28, 10:00 am, Paul Rubin http://[EMAIL PROTECTED] wrote: More examples: x = 1 y = len(s) + x = ok, decides that x is an int x = 1 y = x + 3.0 = ok, decides that x is a float x = 1 y = x + 3.0 z = len(s) + x = forbidden, x cannot be an int and float at the same time. I am so glad you're not the designer of Python. This is how Haskell works and I don't notice much complaints about it. Ok, that means the line y = x + 3.0 have a side effect of x = float(x)? I think I would say that is an implicit behavior. On Feb 28, 11:22 pm, D'Arcy J.M. Cain [EMAIL PROTECTED] wrote: You people can't tell the difference between obvious and learned conventions that came about because in limitations in the hardware at the time. Nobody would have come up with a silly rule like x op y must always have the same type as x and y if computer hardware had been up to the task when these languages were created. What makes you say they weren't? Calculating machines that handled floating point are older than Python by far. But much younger than the first programming language (if it could be called as a language) that set the convention of int op int should result in int 'cause our hardware can't handle floats. On Feb 29, 6:15 am, Paul Rubin http://[EMAIL PROTECTED] wrote: Can you name an example of a calculating machine that both: 2) says 1/2 = 0.5 ? Any pocket calculator to scientific calculator. Except perhaps my calculator which is fraction scientific calculator where it stores 1/2 as rational and have another division operator for 1/2 == 0.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
On Feb 29, 12:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 10:39:51 -, Steven D'Aprano [EMAIL PROTECTED] declaimed the following in comp.lang.python: By that logic, we should see this: len(a string) '8' Why? len() is a function that /counts/ the elements of the argument -- said count remains in integral value (I presume we don't have a 1.5 character long string) The relevant point is that len() is a function that returns a DIFFERENT type than its argument, and nobody ever complains about is. And rightly rejected by many other programming languages, including modern Python, not to mention calculators, real mathematics and common sense. I know of no calculator that has integers for normal math -- and the HP50 even emphasizes this by putting a decimal point into integer quantities. Heck -- most calculators work in BCD floats. Most merely suppress the decimal point if the trailing digits are all 0s My TI-89 treats them differently: 1.0/2.0 is 0.5, while 1/2 is the symbolic expression 1/2. -- http://mail.python.org/mailman/listinfo/python-list
Odd behaviour with list comprehension
Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print [x for x in range(10)] print x It outputs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 9 So the list comprehension actually creates a variable x which is somewhat unexpected. Is there a way for me keep the iterating variable in list comprehension local to the list comprehension? Any comments on the current behaviour of Python is greatly appreciated. Ken -- http://mail.python.org/mailman/listinfo/python-list
Re: more pythonic
On Feb 29, 5:57 pm, Alan Isaac [EMAIL PROTECTED] wrote: Paul McGuire wrote: In general, whenever you have: someNewList = [] for smthg in someSequence: if condition(smthg): someNewList.append( elementDerivedFrom(smthg) ) replace it with: someNewList = [ elementDerivedFrom(smthg) for smthg in someSequence if condition(smthg) ] What is the gain? (Real question.) I think the first is often easier to read. Is the second more efficient? Also, I think list comprehensions are often easier to read as equivalent generator expressions: someNewList = list( elementDerivedFrom(smthg) for smthg in someSequence if condition(smthg) ) Tastes vary of course. Cheers, Alan Isaac I think there is a performance gain in list comps over explicit for looping - I'm sure google will turn up some stats for this in this newsgroup in the past. As for list(generator-expr) over [list-comprehnesion], that's why they make chocolate and vanilla. (I believe that at one time, Guido was considering discarding list comps in Py3K, with this list +generator expression alternative being the rationale for dropping them, but later changed his mind.) -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: convert string number to real number - ValueError: invalid literal for int() with base 10: '2'
On Feb 28, 5:56 pm, davidj411 [EMAIL PROTECTED] wrote: i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. (snipped) I actually found a good solution. (snipped) If you post 1-2 samples of the cell phone bill input, I am sure you'll get much better solutions in terms of clarity, simplicity and elegance, commonly known as pythonicity :) George -- http://mail.python.org/mailman/listinfo/python-list
Re: Is crawling the stack bad? Why?
Seriously, crawling the stack introduces the potential for disaster in your program, since there is no guarantee that the calling code will provide the same environment i future released. So at best you tie your solution to a particular version of a particular implementation of Python. I'm gathering that the general argument is entirely centered around portability and future-proofing of code. This certainly makes sense. I could try and argue that that doesn't matter for write-once-change- never code, but anything I'd say there might as well be applied to an argument saying that writing crappy code is actually ok. And then I would need to be committed for thinking that write-once-change-never code actually exists. I'm making myself sick as I type this. There's a difference between perfect future-proofing and writing code that will at least minimize the amount of maintenance with Python upgrades. Consider: if you walk the stack to get this data instead of using thread local storage, there is zero guarantee that your code will work from one point release to another. It might work in 2.5.1; but a small and inconsequential bug could make them alter the private internals of a class you're walking into so that a variable name is different inside. They might not /need/ to-- but what if the maintainer did a minor style update at the same time? As long as the API is the same, the behavior the same,... it'd get through, and it likely wouldn't even be documented in the release notes for you to catch. If you rely on private internals-- and local variables up your calling stack oh so count-- then you're just inviting breakage. The published API's aren't likely to change except extremely rarely, and when they do they're apt to have a very clear notice provided, and there's usually a very straight-forward way to support both. Crawling the stack into code you don't control ties you directly to one specific implementation, in one specific version-- if it works later, its pure chance you can't rely on, and it could break at any point. Relying on API's have unit tests, deprecation warnings, documentation, and behavior reversions being classified all as bugs, all providing you with confidence that something you code might still work in the next point release... and likely the next major release, too. --Stephen -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour with list comprehension
Ken Pu [EMAIL PROTECTED] writes: Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print [x for x in range(10)] print x It outputs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 9 So the list comprehension actually creates a variable x which is somewhat unexpected. Yeah, it's not really desired, either. The Python Reference Manual, in the List Displays section, has this footnote: In Python 2.3, a list comprehension leaks the control variables of each for it contains into the containing scope. However, this behavior is deprecated, and relying on it will not work once this bug is fixed in a future release. In the meantime, of course, we can't depend on it _not_ leaking, either. Is there a way for me keep the iterating variable in list comprehension local to the list comprehension? Not really, AFAIK. Other than to do it within a separate block, of course, but that's rather cumbersome (writing a new function would do it, but that's more trouble than it's worth). -- Micah J. Cowan Programmer, musician, typesetting enthusiast, gamer... http://micah.cowan.name/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour with list comprehension
On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu [EMAIL PROTECTED] wrote: Is there a way for me keep the iterating variable in list comprehension local to the list comprehension? Kind of. You can use a generator expression instead of a list comprehension, and those don't leak their internal variables into the enclosing scope: list(x for x in range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x Traceback (most recent call last): File pyshell#2, line 1, in module x NameError: name 'x' is not defined You have to pass it to the list constructor to get a list out of it, though. For more on generator expressions see PEP 289: http://www.python.org/dev/peps/pep-0289/ -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour with list comprehension
Jerry Hill [EMAIL PROTECTED] writes: On Fri, Feb 29, 2008 at 10:01 PM, Ken Pu [EMAIL PROTECTED] wrote: Is there a way for me keep the iterating variable in list comprehension local to the list comprehension? Kind of. You can use a generator expression instead of a list comprehension, and those don't leak their internal variables into the enclosing scope: Whoa, that's cool. I didn't even think to try that, just assuming it would do the same. Though now that I think about it, I don't see how it possibly could, since it just evaluates to an object that you could pass around, and return, using it the same way elsewhere. -- Micah J. Cowan Programmer, musician, typesetting enthusiast, gamer... http://micah.cowan.name/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour with list comprehension
Ken Pu wrote: So the list comprehension actually creates a variable x which is somewhat unexpected. Is there a way for me keep the iterating variable in list comprehension local to the list comprehension? Not with a list comprehension, but generator expressions do not leak their iterating variable: list(x for x in range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x Traceback (most recent call last): File stdin, line 1, in module NameError: name 'x' is not defined Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: anydbm safe for simultaneous writes?
chris wrote: I need simple data persistence for a cgi application that will be used potentially by multiple clients simultaneously. So I need something that can handle locking among writes. Sqlite probably does this, but I am using Python 2.4.4, which does not include sqlite. The dbm-style modules would probably be fine, but I have no idea if they are write safe (I have no experience with the underlying unix stuff). Any tips appreciated. the often repeated answer that you need locking is correct but an incomplete answer. it really depends on which DBM you are using. If you are using a fairly recent bsdbm (a.k.a. sleepy cat) it does have the kind of lucky needs to fairly complex transactions. Unfortunately, the API is a sufficiently unintelligible that it will take more than an afternoon to figure out how to even start to use it. gdbm is a nice DBM that permits single writer/multiple readers. If you open a DBM for read, any writer blocks. You open it for read and some times multiple readers can get in but not always (or at least that's the way it seems here in practice). when the DBM is busy, you will get an exception with an error value of: (11, 'Resource temporarily unavailable'). Just busy wait until this exception goes away and you'll get access to the DBM file. Yes, this officially sucks but at least it's a workaround for the problem. another way to solve this particular problem with DBM files is to stick inside a Pyro daemon. Performance won't be too bad and you should be able to get it working relatively easily. I will warn you that the RPC model for Pyro does take some getting used to if you're familiar with more traditional RPC environments. Once you wrap your head around the Pyro model, it's pretty nice. If you want, I can send you a copy of my Pyro daemon I use to wrap a DBM so I don't have to worry about multiple processes accessing the same DBM. the one thing that really bothers me about the DBM interfaces is that the two main DBM's are really quite full-featured but the documentation presents a very sketchy description of what they support and how. As a result, I suspect that DBMS don't get used as often as they could and people are pushed into more complex databases because they don't understand what DBM's are capable of. Other folks have recommended some form of SQL and while SQL light is a very nice small database, personally, I find SQL unintelligible and I have lost more days than I care to think about trying to figure out how to do something in SQL. As result, I tend to go more towards databases such as metakit and buzhug (http://buzhug.sourceforge.net/). the former is like gdbm and only handles a single writer. It's really intended for single process use but I don't know if you can put it in a Pyro controlled deamon. The latter looks pretty interesting because the documentation implies that it supports concurrent access on a per record level (menu item: concurrency control). Given that I'm currently replacing a DBM for very much the same reason you are, I'm going to try using buzhug rather than facing SQL again. I would be glad to compare notes with you if you care to go the same route. Just let me know off list. I wish you the best of luck in your project. ---eric -- Speech-recognition in use. It makes mistakes, I correct some. -- http://mail.python.org/mailman/listinfo/python-list
Re: Backup Script over ssh
Gabriel Genellina wrote: En Wed, 27 Feb 2008 13:32:07 -0200, Christian Kortenhorst [EMAIL PROTECTED] escribi�: But there is no rsync for windows without using cygwin That's no big deal; rsync doesn't require tons of libraries, just cygpopt-0.dll and cygwin1.dll. See this page: http://www.brentnorris.net/rsyncntdoc.html If you prefer a nice GUI around it (mmm... not so nice actually :) ) http://www.aboutmyip.com/AboutMyXApp/DeltaCopy.jsp windows rsync sans gui http://www.itefix.no/phpws/index.php?module=pagemasterPAGE_user_op=view_pagePAGE_id=6MMN_position=23:23 or google cwrsync I use in conjunction with rsnapshot for backing up xp and vista machines --- eric -- Speech-recognition in use. It makes mistakes, I correct some. -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Dan Bishop wrote: On Feb 29, 12:55 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 28 Feb 2008 10:39:51 -, Steven D'Aprano [EMAIL PROTECTED] declaimed the following in comp.lang.python: By that logic, we should see this: len(a string) '8' Why? len() is a function that /counts/ the elements of the argument -- said count remains in integral value (I presume we don't have a 1.5 character long string) The relevant point is that len() is a function that returns a DIFFERENT type than its argument, and nobody ever complains about is. A language that restricted its functions to returning the same types as its arguments wouldn't be very much use, would it? And what about functions that have multiple arguments of different types? And rightly rejected by many other programming languages, including modern Python, not to mention calculators, real mathematics and common sense. I know of no calculator that has integers for normal math -- and the HP50 even emphasizes this by putting a decimal point into integer quantities. Heck -- most calculators work in BCD floats. Most merely suppress the decimal point if the trailing digits are all 0s My TI-89 treats them differently: 1.0/2.0 is 0.5, while 1/2 is the symbolic expression 1/2. Any you don't even have to import anything from __future__! regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Arnaud Delobelle [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] | Perhaps it'll be like when I quit smoking six years ago. I didn't | enjoy it although I knew it was good for me... And now I don't regret | it even though I still have the occasional craving. In following the development of Py3, there have been a few decisions that I wish had gone otherwise. But I agree with more than the majority and am not going to deprive myself of what I expect to be an improved experience without giving Py3 a fair trial. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
On Feb 29, 4:34 pm, Preston Landers [EMAIL PROTECTED] wrote: On Feb 29, 2:12 pm, [EMAIL PROTECTED] wrote: If a thread adds an object it creates to a nonlocal collection, such as a class-static set, does it have to maintain a list of all such objects, just to get the right ones destroyed on completion? Yes. Processes destroy their garbage hassle-free; how can threads? If you're asking is there some automatic way to associate a external resource with a thread so that it's deleted when the thread is done then the answer (to the best of my knowledge) is no. You would have to take care of these details yourself once something outside your thread got a reference to the resource. If you keep all references to the resource inside the thread, then you can use the __del__ method of the resource to take any cleanup actions. Also, processes don't always destroy their garbage hassle free if you consider inter process communication. For instance, if you create an object in one process then serialize it and pass it to another process through e.g. an XMLRPC call, there's no automatic way for the other process to be notified that the originating process exited. You'd have to build that feature in. Exact same thing with threads. And don't forget Thread.run( self ) in the example, if anyone ever wants to make use of the 'target' keyword. I don't understand what you're referring to. In the initial counterexample, class MyThread(Thread): def run(self): while some_condition: do_something() do_something_after_the_thread_ends() , MyThread overrides 'run', so target is never called. It did create the picture to me though that: class MemHoldingThread( Thread ): def __init__( self, *a, **kwa ): Thread.__init__( self, *a, **kwa ) self.objs= set() self._args= ( self, )+ self._args def hold( self, obj ): self.objs.add( obj ) return obj def run( self ): Thread.run( self ) self._cleanup() def _cleanup( self ): for obj in self.objs: obj.clean() Then, def f( thd ): resrc= thd.hold( NewResource() ) infinite_loop( resrc ) MemHoldingThread( target= f ) thd is prepended to f's list of arguments so that it has a handle to its own thread object, but doesn't need an entire thread class-- middle ground. Then thd.hold adds any obj created to a thread-local set (and returns it). Then, _cleanup iterates over the set, and calls clean() on its items when f returns. At this point, I've gotten really really complicated. Not only have I still not shown the _at_exit call to MemHoldingThread static member _clean_all_remaining, but it's only useful if NewResource provides a list of globals/externals that it adds itself to, or a clean() method to do them itself. But, you do get the benefit of not having to store each global/external in a Thread subclass object-- it does that for you-- as well as the benefit of being able to invoke the behavior without explicit additional subclasses. thing1= MemHoldingThread( target= thingf ); thing1.start() daemon1= MemHoldingThread( target= daemonf ); daemon1.start() server1= MemHoldingThread( target= serverf ); server1.start() rather than class Thing( Thread ):... thing1= Thing(); thing1.start() class Daemon( Thread ):... daemon1= Daemon(); daemon1.start() class Server( Thread ):... server1= Server(); server1.start() , which is useful sometimes, if even only a negligible proportion of them. digressions (Honestly, if Thread.start() returned self, class Thing( Thread ):... thing1= Thing().start() , or Thread( start= True ) launched it, that could go a long way, -- you just get keyword name collisions with target's parameters pretty soon.) What are the disadvantages to specifying an internal storage byte order for conversion to a string to use mmap with, by the way? And for all my seniors out there, you may be pleased to learn that I no longer think that all of my general little 5-liners should go in the standard library. /digressions you consider inter process communication. For instance, if you create an object in one process then serialize it and pass it to another process through e.g. an XMLRPC call, there's no automatic way for the other process to be notified that the originating process exited. Maybe... but if all else fails you can pass the process ID in on connection creation, and periodically check if it's still alive. Do any Op. Sys.s have a OnProcTerminate( hproc, callback ) API? Sadly, stdin IPC is before my time. Windows actually even has ThreadSuspend and ThreadResume API; if suspending threads (and raising exceptions in them from others) threatens state consistency, what are their semantics? -- http://mail.python.org/mailman/listinfo/python-list
Re: at-exit-thread
En Fri, 29 Feb 2008 18:12:13 -0200, [EMAIL PROTECTED] escribió: On Feb 29, 1:55 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] schrieb: The Python main interpreter has an at-exit list of callables, which are called when the interpreter exits. Can threads have one? What's involved, or is the best way merely to subclass Thread? Is that some sort of trick-question? class MyThread(Thread): def run(self): while some_condition: do_something() do_something_after_the_thread_ends() The atexit stuff is for process-termination which is/may be induced by external signals - which is the reason why these callbacks extist. Threads don't have that, thus no need. That depends. If a thread adds an object it creates to a nonlocal collection, such as a class-static set, does it have to maintain a list of all such objects, just to get the right ones destroyed on completion? Yes, like any other objects. All threads in a process share the same memory space; any thread can see any other created object, and the general rules on reference counting apply: an object is destroyed when it is no more referenced. There are threading.local objects, which are specially designed to provide per-thread storage; they are not shared among threads. (BTW, you should use a threading.local instance instead of indexing by get_ident()) Processes destroy their garbage hassle-free; how can threads? And don't forget Thread.run( self ) in the example, if anyone ever wants to make use of the 'target' keyword. Any object created inside a thread will be destroyed when the last reference to it is removed, as any other object. Threads are not special in this regard. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Telnet versus telnetlib
En Fri, 29 Feb 2008 20:34:41 -0200, Sean Davis [EMAIL PROTECTED] escribió: When I do an analogous process using telnetlib, I get no debug output, and most importantly, when I send the XML file to the host, I get no printed page. Unfortunately, I do not have access to the host to do troubleshooting there, so I have to feel my way around. Any suggestions on what might be going wrong? In [12]: tn.write(?xml version=1.0 encoding=UTF-8? : labels _FORMAT=C:\labels\anzick_primary_sample.lbl \ is the quote character. You have to duplicate it C:\\labels... or use a raw string r?xml ... _FORMAT=C:\labels... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: rstrip error python2.4.3 not in 2.5.1?
dirkheld [EMAIL PROTECTED] wrote: What is the actual error message [SyntaxError, NameError? etc] that you clipped? Here it is : I tought that I didn't matter because the deliciousapi worked fine on my mac. Traceback (most recent call last): File delgraph.py, line 62, in ? url_metadata = d.get_url(site.rstrip()) File deliciousapi.py, line 269, in get_url document.bookmarks = self._extract_bookmarks_from_url_history(data) File deliciousapi.py, line 297, in _extract_bookmarks_from_url_history timestamp = datetime.datetime.strptime(month_string, '%b lsquo; %y') AttributeError: type object 'datetime.datetime' has no attribute 'strptime' I suppose it is cruel of me, but I find it hilarious that you looked at this traceback and came to the conclusion that the problem was in rstrip. -- Tim Roberts, [EMAIL PROTECTED] Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: pySQLite Insert speed
[EMAIL PROTECTED] wrote: I hav read on this forum that SQL coding (A) below is preferred over (B), but I find (B) is much faster (20-40% faster) (A) sqla= 'INSERT INTO DTABLE1 VALUES (%d, %d, %d, %f)' % values curs.execute(sqla) (B) pf= '?, ?, ?, ?' sqlxb= 'INSERT INTO DTABLE2 VALUES ( %s ) ' % pf curs.execute( sqlxb, values ) Any intution on why (A) is slower? I think you misunderstood. (B) is *ALWAYS* the proper way of doing parameterized SQL queries. Unconditionally. The (A) style is way too vulnerable to SQL injection attacks. -- Tim Roberts, [EMAIL PROTECTED] Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorators and buffer flushing
On Thu, 28 Feb 2008 15:04:38 -0500, Ethan Metsger [EMAIL PROTECTED] wrote: I can reproduce the issue in the console. I'm not convinced it's actually a bug, unless for some reason the interpreter is preventing a buffer flush. Quick question. Having eliminated some of the other variables in my code, I'm wondering if the following might cause a problem flushing the stdout buffer: proc = Popen (['ant'] + self.args, stdout=PIPE, stderr=PIPE) (out, err) = proc.communicate() Is it possible that the Popen object has somehow mangled things? For what it's worth, I'm also passing '-u' to the interpreter with no effect. Best, Ethan ([EMAIL PROTECTED]) http://uppertank.net/ethanm/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting a free TCP port blocking it
theneb [EMAIL PROTECTED] wrote: Hi all, I'm attempting to block a TCP port from any other application from using it until I free it from python, this is so that: 1). Generate a random free user-space port 2). Generate the script for the external program with the port 3). Free the port before external program execution. What's the point? Why can't the actual user of the port create the port, and then notify the other side of the port number? And why don't you just specify a port number of 0 and let the system assign you a free port number? -- Tim Roberts, [EMAIL PROTECTED] Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Run wxPython app remotely under XWindows
On Feb 29, 8:19 am, Mike Driscoll [EMAIL PROTECTED] wrote: On Feb 28, 7:49 pm, Sean DiZazzo [EMAIL PROTECTED] wrote: On Feb 28, 5:26 pm, Sean DiZazzo [EMAIL PROTECTED] wrote: On Feb 28, 3:50 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Sean DiZazzo wrote: Is there something special you have to do to get a wxPython app to run remotely under xwindows? My Tkinter apps always automatically work that way, so I was surprised to even be confronted with this problem. Could you please provide more detail? My wxPython apps run perfectly remotely in the X Window System like this: $ ssh some-other-machine $ DISPLAY=:0 ./my_app.py Should wxPython apps work this way? Do you think it's something with the server? Just to close the loop I think think this is a problem with the ssh server. ~Sean If it's not the server, then please post the issue to the wxPython list. They can probably help: http://wxpython.org/maillist.php Mike To follow up with a solution. I learned that the default install of wxPython, and Tkinter for that matter is compiled to run under Aqua as opposed to X11. It won't run remotely, as I first posted, compiled this way. To get Tkinter to work under X11 you simply need to configure and install a version of python for the X11 environment. The default Mac install (Leopard) and the MacPython distro are configured to run under Aqua. Instead of compiling my own, I found that the default Fink install is configured to run under X11, so if you install it, and run that version of Python, you can run Tkinter apps remotely from a Mac box. wxPython is a bit more complicated. The idea is the same, but the process is much more complicated to get it working. You need to compile wxPython under GTK2, which I found is not well supported for Mac. I followed the instructions at this site: http://wiki.wxpython.org/wxGTK_on_Mac_OSX to get it working. Even with the instructions, I had to finagle a few things to get it to work properly. But it does work! My first successful install was on Tiger, but I'm trying an install on Leopard as we speak. Thanks to Cody Precord for the instructions. I would have never been able to do it without. Now my users will have a prettier GUI! ~Sean -- http://mail.python.org/mailman/listinfo/python-list