python-graph-1.6.3 released
python-graph release 1.6.3 http://code.google.com/p/python-graph/ python-graph is a library for working with graphs in Python. This software provides a suitable data structure for representing graphs and a whole set of important algorithms. The code is appropriately documented and API reference is generated automatically by epydoc. Provided features and algorithms: * Support for directed, undirected, weighted and non-weighted graphs * Support for hypergraphs * Canonical operations * XML import and export * DOT-Language output (for usage with Graphviz) * Random graph generation * Accessibility (transitive closure) * Breadth-first search * Critical path algorithm * Cut-vertex and cut-edge identification * Depth-first search * Heuristic search (A* algorithm) * Identification of connected components * Minimum spanning tree (Prim's algorithm) * Mutual-accessibility (strongly connected components) * Shortest path search (Dijkstra's algorithm) * Topological sorting * Transitive edge identification The 1.6.x series is our refactoring series. Along the next releases, we'll change the API so we can better prepare the codebase to new features. If you want a softer, directed transition, upgrade your code to every release in the 1.6.x series. On the other hand, if you'd rather fix everything at once, you can wait for 1.7.0. This release adds Python 3.x compatibility. Download: http://code.google.com/p/python-graph/downloads/list (tar.bz2, zip and sdist packages are available.) Installing: If you have easy_install on your system, you can simply run: # easy_install python-graph-core And, optionally, for Dot-Language support: # easy_install python-graph-dot -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Introduction ot Python: Change of Dates
Please note that the dates of our upcoming Introduction to Python three-day class in New York city have been changed to avoid the federal holiday on Martin Luther King Day. The class will now run from January 19-21. Details at http://hwebpyintnyc01.eventbrite.com/ regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: power of explicit self?
Fire Crow a écrit : I'm looking for an explanation of how explicit self is implimented It's not implemented - it's just the first argument of the function, and you have to declare it explicitely in the function's args list. If your question is about how obj.func() becomes obj.___class__.func(obj), then the answer is protocol deescriptor + method type. The function type implements the protocol descriptor so that its __get__ method returns a method instance which wraps around the function, class and instance. Then the method's __call__ method will delegate to the function, injecting the instance as first positional param. and what features are only possible because of, or are greatly improved, because of it. Simplicity (methods are built of the combination of two general purpose features - descriptor protocol and callable objects), flexibility (you can use any callable object as a 'method' as long as it correctly implements the descriptor protocol), and of course readability (no special rules wrt/ 'self', it's nothing else than the first argument of the function, period). Oh, and yes - you can use methods as functions too, it's sometimes handy for dispatching purposes !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Is it Possible
zeph a écrit : (snip) 4) It's better to collect all your eventual output into a string that you print Yuck ! Definitly one of the worst advises you could give. OP By all mean, *DONT* do that. Use a templating system instead. /OP -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Is it Possible
Yes, I understood. And I'm using large Global dictionary (or Array) to replicate those objects. State of the thing will store in there. But it wasn't an object. Just Assocative array. Staying in global space, Because. In web programming we do not store anything except session. Every object we created was destroyed after execution. Using objects in this conditions was non sense to me. (of course I'm not very capable programmer probably it was my fault to take full advantage of oo programming) Plus. In php we can store arrays in files very easy. Combining this with any PHP opcode cache can save those arrays in memory. So we got damn cheap state saver. This is possible in python, too. But damn cheap... well, the cheapest solution in terms of speed is to just keep the things in memory. Which you can't do with PHP, as everything lives just one request, but in Python with certain app-servers, you can do this. Of course things may differ in python. Anyhow I generate a Registry class to replicate global dictionary. Probably it much better than my PHP Direct $GLOBAL usage. So I have no problem with that. Anyway, I need to save my lots and lots of config variables in dictionary style global accessible location. Because. In my design We got lots of plugins, and those plugins may show in multiple times and multiple locations in a page. Each plugin may have setup values to affect entire page output. Because of this. I have to put those values in global location for future use. No, you don't. Because of this, you can e.g. use ToscaWidgets as a framework for creating widgets that encapsulate code, HTML, javascript and CSS. And no global state is shared. Also, I think you should *really* look into one of the available web-frameworks such as Django or Turbogears to learn how to write webapps in python - instead of shoehorning your tried trusted PHP techniques that don't translate well. Yes I download the django trying to learn but it was much different. My problem is not writing web apps. I'm doing well. My design was very good and I'm very proud its abilities. My problem is with PHP syntax and performance. I'm just trying to replicate my recepies in python... Then the result will be a twice as horrible program in python. Because you work against the language. In the end of course, what matters is what works for you. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Is it Possible
Sancar Saran a écrit : (snip) My problem is with PHP syntax and performance. I'm just trying to replicate my recepies in python... Python is not PHP, and trying to write PHP in Python won't buy you much except pain and frustration. -- http://mail.python.org/mailman/listinfo/python-list
Re: eiger replacement?
On 12/12/2009 05:38, Tim Roberts wrote: Steven D'Apranost...@remove-this-cybersource.com.au wrote: On Fri, 11 Dec 2009 17:45:24 +, Robin Becker wrote: The current hardware CPU: Intel(R) Pentium(R) 4 CPU 2.40GHz (2394.01-MHz 686-class CPU) [...] What does this have to do with Python? I'm guessing Robin had a slight address book malfunction when he sent this. indeed, an excess of Christmas Spirit as well. Never a doh! moment should be passed by. Sorry for any confusion -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Is it Possible
Bruno Desthuilliers wrote: Sancar Saran a écrit : (snip) My problem is with PHP syntax and performance. I'm just trying to replicate my recepies in python... Python is not PHP, and trying to write PHP in Python won't buy you much except pain and frustration. I think people are being a little harsh here. Replicating exactly what PHP code does on a micro level i.e. line by line is probably a bad idea but for all we know a lot of the macro level stuff might be fine, or mostly fine i.e. structures, algorithms, classes and functions etc. If this is the case rewriting the same bits in Python might not be painful and frustrating, indeed seeing how much terser those things can be written in Python would probably be quite satisfying. Of course, some PHP is never going to port well but you can't say for sure without seeing it. Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On 12/14/09, exar...@twistedmatrix.com exar...@twistedmatrix.com wrote: On 02:50 am, lie.1...@gmail.com wrote: On 12/14/2009 9:45 AM, exar...@twistedmatrix.com wrote: On 08:18 pm, st...@remove-this-cybersource.com.au wrote: On Sun, 13 Dec 2009 14:35:21 +, exarkun wrote: StopIteration is intended to be used only within the .__next__ method of iterators. The devs know that other 'off-label' use results in the inconsistency you noted, but their and my view is 'don't do that'. Which is unfortunate, because it's not that hard to get StopIteration without explicitly raising it yourself and this behavior makes it difficult to debug such situations. I can't think of any way to get StopIteration without explicitly raising it yourself. It's not like built-ins or common data structures routinely raise StopIteration. I don't think I've *ever* seen a StopIteration that I didn't raise myself. Call next on an iterator. For example: iter(()).next() .next() is not meant to be called directly Doesn't matter. Sometimes it makes sense to call it directly. And I was just giving an example of a way to get StopIteration raised without doing it yourself - which is what Steve said he couldn't think of. I'm surprised to hear you say that the magical faerie land behavior isn't desirable either, though. I'd love a tool that did what I wanted, not what I asked. The only serious argument against this, I think, is that it is beyond our current ability to create (and so anyone claiming to be able to do it is probably mistaken). In your world, this is what happens: list = [a, b, c] # print list print list [a, b, c] # make a copy of list alist = list(llst) # oops a mistype alist = alist - ] + , d] print alist [a, b, c, d] alist[:6] + i, + alist[6:] print alist [a, i, b, c, d] print alist # hearing the sound of my deskjet printer... C:\fikle.text.write(alist) print open(C:\file.txt).read() h1a/h1 ul lii/li lib/li lic d/li # great, exactly what I needed I don't understand the point of this code listing, sorry. I suspect you didn't completely understand the magical faerie land I was describing - where all your programs would work, no matter what mistakes you made while writing them. Exactly, that's what's happening. It just works. It knows that when I said alist[:6] + i, + alist[6:] ; I want to insert i between the sixth character of the textual representation of the list. It knows to find the correct variable when I made a typo. It correctly guess that I want to print to a paper instead of to screen. It knows that when I wrote to C:\path.write(), it knows I wanted a HTML output in that specific format. It just works (TM), whatever mistakes I made. That's what you wanted, right? -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I get the target platform info of a dll with Python 3.1.1?
On dec. 12, 03:18, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Fri, 11 Dec 2009 16:39:37 -0300, Isti istvan.szir...@gmail.com escribió: I have manydllfiles and I would like to select them into two different folders (PC and PPC). For this I need to know the target platform of thedllfile or any other details about its platform. Look at sys.platform and the platform module. -- Gabriel Genellina Hi, The platform module gives you information about the platform where you are running on with your script and not about the not loaded dll(s). So, thanks but, this does not work in this case. However, I found a solution: def DLLIdentifier( self ): ''' Microsoft Portable Executable and Common Object File Format Specification http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx After the MS DOS stub, at the file offset specified at offset 0x3c, is a 4-byte signature that identifies the file as a PE format image file. This signature is PE\0\0 (the letters P and E followed by two null bytes). At the beginning of an object file, or immediately after the signature of an image file, is a standard COFF file header in the following format. Note that the Windows loader limits the number of sections to 96. The Machine field has one of the following values that specifies its CPU type. An image file can be run only on the specified machine or on a system that emulates the specified machine. ''' Platform = 'UNKNOWN' for Row in codecs.open( os.path.join( self.Root, self.File ), 'rb' ): if b'\x00PE\x00\x00' in Row: # IMAGE_FILE_MACHINE_UNKNOWN 0x0 The contents of this field are assumed to be applicable to any machine type if b'\x00PE\x00\x00\x00\x00' in Row: Platform = 'UNKNOWN' break # IMAGE_FILE_MACHINE_AM33 0x1d3 Matsushita AM33 elif b'\x00PE\x00\x00\xD3\x01' in Row: Platform = 'AM33' break # IMAGE_FILE_MACHINE_AMD640x8664 x64 elif b'\x00PE\x00\x00\x664\x08' in Row: Platform = 'AMD64' break # IMAGE_FILE_MACHINE_ARM 0x1c0 ARM little endian elif b'\x00PE\x00\x00\xC0\x01' in Row: Platform = 'ARM' break # IMAGE_FILE_MACHINE_EBC 0xebc EFI byte code elif b'\x00PE\x00\x00\xBC\x0E' in Row: Platform = 'EBC' break # IMAGE_FILE_MACHINE_I386 0x14c Intel 386 or later processors and compatible processors elif b'\x00PE\x00\x00\x4C\x01' in Row: Platform = 'I386' break # IMAGE_FILE_MACHINE_IA64 0x200 Intel Itanium processor family elif b'\x00PE\x00\x00\x00\x02' in Row: Platform = 'IA64' break # IMAGE_FILE_MACHINE_M32R 0x9041 Mitsubishi M32R little endian elif b'\x00PE\x00\x00\x041\x09' in Row: Platform = 'M32R' break # IMAGE_FILE_MACHINE_MIPS16 0x266 MIPS16 elif b'\x00PE\x00\x00\x66\x02' in Row: Platform = 'MIPS16' break # IMAGE_FILE_MACHINE_MIPSFPU 0x366 MIPS with FPU elif b'\x00PE\x00\x00\x66\x03' in Row: Platform = 'MIPSFPU' break # IMAGE_FILE_MACHINE_MIPSFPU160x466 MIPS16 with FPU elif b'\x00PE\x00\x00\x66\x04' in Row: Platform = 'MIPSFPU16' break # IMAGE_FILE_MACHINE_POWERPC 0x1f0 Power PC little endian elif b'\x00PE\x00\x00\xF0\x01' in Row: Platform = 'POWERPC' break # IMAGE_FILE_MACHINE_POWERPCFP0x1f1 Power PC with floating point support elif b'\x00PE\x00\x00\xF1\x01' in Row: Platform = 'POWERPCFP' break # IMAGE_FILE_MACHINE_R40000x166 MIPS little endian elif b'\x00PE\x00\x00\x66\x01' in Row: Platform = 'R4000' break # IMAGE_FILE_MACHINE_SH3 0x1a2 Hitachi SH3 elif b'\x00PE\x00\x00\xA2\x01' in Row: Platform = 'SH3' break # IMAGE_FILE_MACHINE_SH3DSP 0x1a3 Hitachi SH3 DSP elif b'\x00PE\x00\x00\xA3\x01' in Row: Platform = 'SH3DSP' break
Re: Dangerous behavior of list(generator)
Terry Reedy wrote: On 12/13/2009 11:33 PM, exar...@twistedmatrix.com wrote: This could provide behavior roughly equivalent to the behavior of a list comprehension. Impossible. The only serious option for consistency is to special case list comps to also trap StopIteration raised in the expression part, but the devs decided not to do this as doing do is arguably a bug. A viable option might be to introduce a different exception type and translate (expr(v) for v in items if cond(v)) into def gen(items, expr, cond): for v in items: try: if cond(v): yield expr(v) except StopIteration: raise TypeError(StopIteration raised in 'expr' or 'cond' part of a generator expression) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Is it Possible
r0g a écrit : Bruno Desthuilliers wrote: Sancar Saran a écrit : (snip) My problem is with PHP syntax and performance. I'm just trying to replicate my recepies in python... Python is not PHP, and trying to write PHP in Python won't buy you much except pain and frustration. I think people are being a little harsh here. Replicating exactly what PHP code does on a micro level i.e. line by line is probably a bad idea but for all we know a lot of the macro level stuff might be fine, or mostly fine i.e. structures, algorithms, classes and functions etc. I was talking about trying to replicate PHP's execution model and idioms in Python - the framework part -, not about application specific algos, data structures etc. -- http://mail.python.org/mailman/listinfo/python-list
Moving from PHP to Python. Part Two
Hello Again. I hope, I don't bug too much. First of all. I want to Thank to everyone who respond my messages. I was able to do some of my needs and stuck some others. So ? I need help again. And here my progress.. Following was my globalized registry solution # -*- coding: utf-8 -*- class Registry: data = {} def __init__(self,environ): self.data['env'] = environ self.data['init'] = 'hede' def set_entry(self,key,data): self.data[key] = data def get_entry(self,key): return self.data[key] def debug(self): r = 'pre' r += repr(self.data) r += '/pre' return r I have some questions about this code. First of all. when execute debug function. It wont work in every request. # -*- coding: utf-8 -*- import os, sys, cgi, pprint import cgitb cgitb.enable() def application(environ, start_response): sys.path.append(environ['DOCUMENT_ROOT']+core) import registry, k5 # new registry r = registry.Registry(environ) r.set_entry('hede','hodo') #response_headers = [('Content-type',k5.headers['content-type']+'; charset='+k5.headers['charset'])] #start_response(kk5.headers['status'], response_headers) response_body = 'The request method was %s' % environ['REQUEST_METHOD'] response_body += 'br/' response_body += r.debug() status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] In first request I can see elements of my registry and second request it was shows noting. Then 3rd request I can see my registry elements again. next request was empty too. And it was go like that. I don't understand why ? Second problem is. Formatting. I need to see my dictionary elements like this. [k5req] = Array ( [raw] = heede [post] = Array ( ) [proto] = http:// [base_url] = http://k5.int/? [bend_url] = http://k5.int/?backend/ [ajax_url] = http://k5.int/?ajax/ [domain] = k5.int [path] = Array ( [0] = heede ) [location] = frontend [page] = heede [dom_stat] = 1 ) Is there any available solution (like php's print_r) or have I write to my own ? And If I understood correctly PSP template execution in mod_wsgi is impossible. So I have to look something like cheetah or similar marker based template systems. And If I understood correctly I have to import every module in sub imported module. And I want to make sure to my 5 different base module was available every other sub imported module. Is there any way to this from do and forget from start ? Regards. -- http://mail.python.org/mailman/listinfo/python-list
Re: power of explicit self?
On Dec 12, 12:20 pm, Fire Crow m...@firecrow.com wrote: It's not implemented in the compiler. There's a place in the runtime for invoking a method where the object is inserted at the beginning of the parameter list. IIRC, that's done by wrapping the function object. This is the source of Objects/methodobject.c it look like this is where self is added to the argument list, but I'll have to do some more digging. No, not really. That code sets the self argument only for functions implemented in C. The code that implements self behavior for Python methods is mostly found in the file classobject.c. Basically whenever a method is accessed through an object, the object creates an instancemethod for it. The instancemethod type is defined in classobject.c. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Issues with multiprocessing
Hi, I am resending this as I am not seeing any response, can anyone help here? Hi, I have an application that uses multiprocessing pools (multiprocessing.Pool(processes=.)). There are multiple such pools and each pool has configurable number of processes. Once the process is spawned, it keeps on executing and does the needed processing. If there is nothing to process (like ftp'ing files from some source, if the files are not there, the process would sleep for some time, and then again check for files, that way, it is a infinite loop with some sleep), the process 'sleeps' for some time and continues. I am using a T5220, Solaris box with Solaris 10. Problem -there are multiple pools and multiple processes, i am seeing that not all the processes get spawned. They get spawned when the sleep time is increased (say from 0.1 sec to 1 sec). If I further increase the number of processes, again some process do not get spawned. For that, I further need to increase the sleep time (say to 2 sec), then the processes get spawned. Typically, in a multiprocessing I would expect that if a process sleeps for even a small time, other processes should get their chance to execute, but this does not seem to be happening here. Can you please throw some light on it? Thanks and Regards, Yateen V. Joshi This email and any attachments may contain legally privileged and/or confidential information of Starent Networks, Corp. and is intended only for the individual or entity named in the message. The information transmitted may not be used to create or change any contractual obligations of Starent Networks, Corp. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this e-mail and its attachments by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify the sender immediately -- by replying to this message or by sending an email to postmas...@starentnetworks.com -- and destroy all copies of this message and any attachments without reading or disclosing their contents. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On 06:46 am, tjre...@udel.edu wrote: On 12/13/2009 10:29 PM, exar...@twistedmatrix.com wrote: Doesn't matter. Sometimes it makes sense to call it directly. It only makes sense to call next (or .__next__) when you are prepared to explicitly catch StopIteration within a try..except construct. You did not catch it, so it stopped execution. Let me repeat: StopIteration is intended only for stopping iteration. Outside that use, it is a normal exception with no special meaning. You cut out the part of my message where I wrote that one might have forgotten the exception handling code that you posit is required, and that the current behavior makes debugging this situation unnecessarily challenging. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
exar...@twistedmatrix.com wrote: On 08:45 am, tjre...@udel.edu wrote: Tom Machinski wrote: In most cases, `list(generator)` works as expected. Thus, `list(generator expression)` is generally equivalent to `[generator expression]`. Here's a minimal case where this equivalence breaks, causing a serious and hard-to-detect bug in a program: def sit(): raise StopIteration() StopIteration is intended to be used only within the .__next__ method of iterators. The devs know that other 'off-label' use results in the inconsistency you noted, but their and my view is 'don't do that'. Which is unfortunate, because it's not that hard to get StopIteration without explicitly raising it yourself and this behavior makes it difficult to debug such situations. What's with this view, exactly? Is it just that it's hard to implement the more desirable behavior? I'm not exactly sure what you're asking for. The StopIteration exception originated as part of the for-loop protocol. Later on it was generalized to apply to generators as well. The reason for using an exception is simple: raising and catching exceptions is fast at C level and since the machinery for communicating exceptions up the call stack was already there (and doesn't interfere with the regular return values), this was a convenient method to let the upper call levels know that an iteration has ended (e.g. a for-loop 4 levels up the stack). I'm not sure whether that answers your question, but it's the reason for things being as they are :-) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 14 2009) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Part Two
Sancar Saran wrote: Hello Again. I hope, I don't bug too much. First of all. I want to Thank to everyone who respond my messages. I was able to do some of my needs and stuck some others. So ? I need help again. And here my progress.. Following was my globalized registry solution # -*- coding: utf-8 -*- class Registry: data = {} def __init__(self,environ): self.data['env'] = environ self.data['init'] = 'hede' def set_entry(self,key,data): self.data[key] = data def get_entry(self,key): return self.data[key] def debug(self): r = 'pre' r += repr(self.data) r += '/pre' return r I have some questions about this code. First of all. when execute debug function. It wont work in every request. # -*- coding: utf-8 -*- import os, sys, cgi, pprint import cgitb cgitb.enable() def application(environ, start_response): sys.path.append(environ['DOCUMENT_ROOT']+core) import registry, k5 # new registry r = registry.Registry(environ) r.set_entry('hede','hodo') #response_headers = [('Content-type',k5.headers['content-type']+'; charset='+k5.headers['charset'])] #start_response(kk5.headers['status'], response_headers) response_body = 'The request method was %s' % environ['REQUEST_METHOD'] response_body += 'br/' response_body += r.debug() status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) return [response_body] In first request I can see elements of my registry and second request it was shows noting. Then 3rd request I can see my registry elements again. next request was empty too. And it was go like that. I don't understand why ? Second problem is. Formatting. I need to see my dictionary elements like this. [k5req] = Array ( [raw] = heede [post] = Array ( ) [proto] = http:// [base_url] = http://k5.int/? [bend_url] = http://k5.int/?backend/ [ajax_url] = http://k5.int/?ajax/ [domain] = k5.int [path] = Array ( [0] = heede ) [location] = frontend [page] = heede [dom_stat] = 1 ) Is there any available solution (like php's print_r) or have I write to my own ? import pprint pprint.pformat({foo : 10}) If I understood correctly I have to import every module in sub imported module. And I want to make sure to my 5 different base module was available every other sub imported module. Is there any way to this from do and forget from start ? Not really. In python, each module must import whatever dependencies it has. You *can* put stuff into the __builtins__-namespace, and this will make them available in each piece of code running. However, I (and any other sane person on this list) will *STRONGLY* advise you against doing that - polluting this global namespace will very likely create collisions which will re-define names and thus introduce nasty bugs. Python has namespaces. Use them. Diez -- http://mail.python.org/mailman/listinfo/python-list
What is the differences between tkinter in windows and Tkinter in the other platform?
Hi, I'm hidekazu. I'm making a Tk application with python. In the below code, the class App was inherited from Tkinter.Tk and the __init__ method calls Tk's constructor with `super` method. In windows, this code is valid (but, Tkinter - tkinter). Why does this code happen a type error in not windows platform? thank you. # from Tkinter import * class App(Tk): def __init__(self): super(Tk,self).__init__() App().mainloop() #Traceback (most recent call last): # File ./app.py, line 16, in module #App().mainloop() # File ./app.py, line 7, in __init__ #super(Tk,self).__init__() #TypeError: super() argument 1 must be type, not classobj / 岩城 秀和(いわき ひでかず) e-mail: iw...@iwakihidekazu.net / -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On 02:58 pm, m...@egenix.com wrote: exar...@twistedmatrix.com wrote: On 08:45 am, tjre...@udel.edu wrote: Tom Machinski wrote: In most cases, `list(generator)` works as expected. Thus, `list(generator expression)` is generally equivalent to `[generator expression]`. Here's a minimal case where this equivalence breaks, causing a serious and hard-to-detect bug in a program: def sit(): raise StopIteration() StopIteration is intended to be used only within the .__next__ method of iterators. The devs know that other 'off-label' use results in the inconsistency you noted, but their and my view is 'don't do that'. Which is unfortunate, because it's not that hard to get StopIteration without explicitly raising it yourself and this behavior makes it difficult to debug such situations. What's with this view, exactly? Is it just that it's hard to implement the more desirable behavior? I'm not exactly sure what you're asking for. The StopIteration exception originated as part of the for-loop protocol. Later on it was generalized to apply to generators as well. The reason for using an exception is simple: raising and catching exceptions is fast at C level and since the machinery for communicating exceptions up the call stack was already there (and doesn't interfere with the regular return values), this was a convenient method to let the upper call levels know that an iteration has ended (e.g. a for-loop 4 levels up the stack). I'm not sure whether that answers your question, but it's the reason for things being as they are :-) I'm asking about why the behavior of a StopIteration exception being handled from the `expression` of a generator expression to mean stop the loop is accepted by the devs as acceptable. To continue your comparison to for loops, it's as if a loop like this: for a in b: c actually meant this: for a in b: try: c except StopIteration: break Note, I know *why* the implementation leads to this behavior. I'm asking why the devs *accept* this. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the differences between tkinter in windows and Tkinter in the other platform?
Hidekazu IWAKI wrote: Hi, I'm hidekazu. I'm making a Tk application with python. In the below code, the class App was inherited from Tkinter.Tk and the __init__ method calls Tk's constructor with `super` method. In windows, this code is valid (but, Tkinter - tkinter). Why does this code happen a type error in not windows platform? thank you. # from Tkinter import * class App(Tk): def __init__(self): super(Tk,self).__init__() App().mainloop() #Traceback (most recent call last): # File ./app.py, line 16, in module #App().mainloop() # File ./app.py, line 7, in __init__ #super(Tk,self).__init__() #TypeError: super() argument 1 must be type, not classobj In Python 2.x Tkinter uses classic classes (classes that do not inherit from object), and super() can't handle these. In 3.x classic classes are gone, and tkinter uses newstyle classes. So the behaviour changes from 2.x to 3.x, but should be the same on all platforms. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
exar...@twistedmatrix.com wrote: [ ... ] it's as if a loop like this: for a in b: c actually meant this: for a in b: try: c except StopIteration: break Note, I know *why* the implementation leads to this behavior. I'm asking why the devs *accept* this. It's part of the price Python pays for letting people get their hands on the controls. Consider also: Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type help, copyright, credits or license for more information. class dict2(dict): ... def __getitem__ (self, key): ... if key == 'fatal': ... raise KeyError ... d = dict2() d['fatal'] = 'Hello, world!' print d['fatal'] Traceback (most recent call last): File stdin, line 1, in module File stdin, line 4, in __getitem__ KeyError KeyError when we just put the item into the dict? Yep. Mel. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
setup.py and PyPI
Greetings! I'm using Python 2.5 on Windows XP, and trying to get the upload portion of setup.py to work. According to what I have found, I can put my info into a .pypirc file to have the process pick up my username/password and upload the files. I have tried putting this file into the same folder I'm running setup.py from, into my home folder (USERPROFILE, I think) -- and the really frustrating part is that it worked a couple times (but maybe that was from my home computer) but mostly it does not. Any and all pointers gratefully accepted! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
Il Sun, 13 Dec 2009 21:17:28 -0800, knifenomad ha scritto: On 12월14일, 오후12시42분, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Sun, 13 Dec 2009 17:19:17 -0800, knifenomad wrote: this makes the set type hashable. class Set(set): __hash__ = lambda self: id(self) That's a *seriously* broken hash function. key = voila d = { Set(key): 1 } d {Set(['i', 'a', 'l', 'o', 'v']): 1} d[ Set(key) ] Traceback (most recent call last): File stdin, line 1, in module KeyError: Set(['i', 'a', 'l', 'o', 'v']) -- Steven of course it is broken as long as it uses it's instance id. i added this to notify that unhashable can become hashable implementing __hash__ inside the class. which probably set to None by default. Ok, nice example, but I believe that using id() as the hash function can lead to unexpected collisions. -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py and PyPI
On Dec 14, 2009, at 12:04 PM, Ethan Furman wrote: Greetings! I'm using Python 2.5 on Windows XP, and trying to get the upload portion of setup.py to work. According to what I have found, I can put my info into a .pypirc file to have the process pick up my username/password and upload the files. I have tried putting this file into the same folder I'm running setup.py from, into my home folder (USERPROFILE, I think) -- and the really frustrating part is that it worked a couple times (but maybe that was from my home computer) but mostly it does not. When it fails, how does it fail? -- http://mail.python.org/mailman/listinfo/python-list
Re: md5 strange error
now i have Fedora 12 Now when i try to use md5 , python say : python Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 Type help, copyright, credits or license for more information. import md5 __main__:1: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5 Why ? -- http://mail.python.org/mailman/listinfo/python-list
Re: md5 strange error
On Mon, Dec 14, 2009 at 09:33, catalinf...@gmail.com catalinf...@gmail.com wrote: now i have Fedora 12 Now when i try to use md5 , python say : python Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 Type help, copyright, credits or license for more information. import md5 __main__:1: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5 Why ? As the message says: the md5 module is deprecated, and you should use the hashlib module instead. I believe md5 is deprecated from Python 2.6 onwards, which may be why you have not seen this message before (Fedora 12 is the first Fedora to ship with Python 2.6). Rami Chowdhury Never assume malice when stupidity will suffice. -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On 12/14/2009 10:21 AM, exar...@twistedmatrix.com wrote: I'm asking about why the behavior of a StopIteration exception being handled from the `expression` of a generator expression to mean stop the loop is accepted by the devs as acceptable. Any unhandled exception within a loop stops the loop, and the exception is passed to the surrounding code. To continue your comparison to for loops, it's as if a loop like this: for a in b: c actually meant this: for a in b: try: c except StopIteration: break No it does not. If c raises any exception, the loop stops *and* the exception is passed up to the surrounding code. Note, I know *why* the implementation leads to this behavior. You do not seem to know what the behavior is. Read what I wrote last night. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Seek support for new slice syntax PEP.
Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. Following example comes from projecteuler.net problem 166. The Numeric community would also like this, as would the general python user. The slice notation would require one : between the brackets to differentiate it from a list, which is similar to the set notation requirement that disambiguates it from a dictionary. Several times now I've wanted python slice notation. Perhaps I'll write a Python Enhancement Proposal. I stored slices of vector array entries to add edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ slice(i*n,(i+1)*n,1), # rows slice(i,nn,n), # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = slice(n-1,n*(n-1)+1,n-1) backslash = slice(0,nn,n+1) Which could have been written in a manner completely consistent with other python shorthand notations and for which python cannot possibly use the notation for some other purpose, edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ [i*n: (i+1)*n] # rows [i: nn: n], # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = [n-1: n*(n-1)+1: n-1] backslash = [0: nn: n+1] -- http://mail.python.org/mailman/listinfo/python-list
str and unicode proper usage
Hi all, If an entire application operates on Unicode strings from UI to database, is there a use case for str() and unicode() functions? The application should be able to read/write files, open sockets and execute external processes and parse their output. From my own experiments, the open() command for files accepts unicode strings. I am just wondering if there is a place where str() would have to be used, other than the usual use case of converting a non-string python construct (such as an integer) into a string. The reason I am asking is, I work on a project with several other developers and our NLS testing is not going so well. Major reason is (I think) that there is a lot of str() functions interspersed everywhere. So whenever a unicode character is used in those variables, the application breaks. My recommendation to the team was to remove these functions and only leave the necessary ones. However, I do not have a generic answer on when a str() function is necessary. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
http://www.python.org/dev/peps/pep-3003/ Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the differences between tkinter in windows and Tkinter in the other platform?
On 12/14/2009 10:02 AM, Hidekazu IWAKI wrote: Hi, I'm hidekazu. I'm making a Tk application with python. In the below code, the class App was inherited from Tkinter.Tk and the __init__ method calls Tk's constructor with `super` method. In windows, this code is valid (but, Tkinter - tkinter). Why does this code happen a type error in not windows platform? You question is not clear. Specify system and Python version is both cases. Note that Tkinter in 2.x was renamed tkinter in 3.x. This difference has nothing to do with the platform. tjr thank you. # from Tkinter import * class App(Tk): def __init__(self): super(Tk,self).__init__() App().mainloop() #Traceback (most recent call last): # File ./app.py, line 16, inmodule #App().mainloop() # File ./app.py, line 7, in __init__ #super(Tk,self).__init__() #TypeError: super() argument 1 must be type, not classobj / 岩城 秀和(いわき ひでかず) e-mail: iw...@iwakihidekazu.net / -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py and PyPI
Philip Semanchuk wrote: On Dec 14, 2009, at 12:04 PM, Ethan Furman wrote: Greetings! I'm using Python 2.5 on Windows XP, and trying to get the upload portion of setup.py to work. According to what I have found, I can put my info into a .pypirc file to have the process pick up my username/password and upload the files. I have tried putting this file into the same folder I'm running setup.py from, into my home folder (USERPROFILE, I think) -- and the really frustrating part is that it worked a couple times (but maybe that was from my home computer) but mostly it does not. When it fails, how does it fail? python setup.py sdist bdist --format wininst upload [snip] running upload Submitting dist\dbf-0.87.7.zip to http://pypi.python.org/pypi Upload failed (401): You must be identified to edit package information -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py and PyPI
On Dec 14, 2009, at 1:00 PM, Ethan Furman wrote: Philip Semanchuk wrote: On Dec 14, 2009, at 12:04 PM, Ethan Furman wrote: Greetings! I'm using Python 2.5 on Windows XP, and trying to get the upload portion of setup.py to work. According to what I have found, I can put my info into a .pypirc file to have the process pick up my username/password and upload the files. I have tried putting this file into the same folder I'm running setup.py from, into my home folder (USERPROFILE, I think) -- and the really frustrating part is that it worked a couple times (but maybe that was from my home computer) but mostly it does not. When it fails, how does it fail? python setup.py sdist bdist --format wininst upload [snip] running upload Submitting dist\dbf-0.87.7.zip to http://pypi.python.org/pypi Upload failed (401): You must be identified to edit package information Looks like there's known problems with this under Windows that might affect your version of Python, depending on which version you have. http://bugs.python.org/issue1741 Setting a HOME environment variable to point to wherever you have your .pypirc file might help. Hope this helps P -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On 14-Dec-09 13:03 PM, Dave wrote: Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. Following example comes from projecteuler.net problem 166. The Numeric community would also like this, as would the general python user. The slice notation would require one : between the brackets to differentiate it from a list, which is similar to the set notation requirement that disambiguates it from a dictionary. Several times now I've wanted python slice notation. Perhaps I'll write a Python Enhancement Proposal. I stored slices of vector array entries to add edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ slice(i*n,(i+1)*n,1), # rows slice(i,nn,n), # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = slice(n-1,n*(n-1)+1,n-1) backslash = slice(0,nn,n+1) Which could have been written in a manner completely consistent with other python shorthand notations and for which python cannot possibly use the notation for some other purpose, edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ [i*n: (i+1)*n] # rows [i: nn: n], # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = [n-1: n*(n-1)+1: n-1] backslash = [0: nn: n+1] Yes, we know that PEP 3003 applies but I see no harm in discussing possible enhancements. The existing slice seems a little different from what you are proposing: An object usually containing a portion of a sequence. A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5]. or: Slice objects Slice objects are used to represent slices when extended slice syntax is used. This is a slice using two colons, or multiple slices or ellipses separated by commas, e.g., a[i:j:step], a[i:j, k:l], or a[..., i:j]. They are also created by the built-in slice() function. If your scheme flies, would it be practicable to use the same syntax as a range generator? range(i, j, k) = i:j:k so range(10, 2) = :10:2 i.e. we could write for i in :10:2: or the more common: range(10) = :10 Colin W. -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
Yes, we know that PEP 3003 applies but I see no harm in discussing possible enhancements. I don't think the OP knew that the moratorium was in effect. That's why I brought it up. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
On Monday 14 December 2009 00:10:52 David Boddie wrote: Doesn't the author give his e-mail address at the end of the video? (Maybe I'm thinking of a different video.) Yes, in a quick and garbled way :) I have yet to try to contact the author or the youtube poster -- been too busy. I was hoping someone on the list may recognize what tools he was using, or have some insight into how they would attack the problem. I have pondered it from a wxPython pov, that being all I am experienced with and I would have no chance of recreating that demo. Is it using some kind of built-in QT/KDE voodoo? \d -- \/\/ave: donn.in...@googlewave.com home: http://otherwise.relics.co.za/ 2D vector animation : https://savannah.nongnu.org/projects/things/ Font manager : https://savannah.nongnu.org/projects/fontypython/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On 06:00 pm, tjre...@udel.edu wrote: On 12/14/2009 10:21 AM, exar...@twistedmatrix.com wrote: I'm asking about why the behavior of a StopIteration exception being handled from the `expression` of a generator expression to mean stop the loop is accepted by the devs as acceptable. Any unhandled exception within a loop stops the loop, and the exception is passed to the surrounding code. To continue your comparison to for loops, it's as if a loop like this: for a in b: c actually meant this: for a in b: try: c except StopIteration: break No it does not. No what does not? I said It is as if. This is a hypothetical. I'm not claiming this is the actual behavior of anything. Note, I know *why* the implementation leads to this behavior. You do not seem to know what the behavior is. Read what I wrote last night. Well, I'm a bit tired of this thread. Please disregard my question above. I'm done here. Sorry for the confusion. Have a nice day. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
might be related to this: http://code.google.com/p/rchi-zui/ geremy condra -- http://mail.python.org/mailman/listinfo/python-list
Python 2.6 ftplib has timeout parameter, but how to detect a timeout
I was quite happy to see that ftplib in Python 2.6 now has a timeout parameter. With large file downloads my script would often hang, presumably from timing out. Now that there is a timeout parameter, how would I detect when a timeout occurs? -- http://mail.python.org/mailman/listinfo/python-list
OS independent way to check if a python app is running?
Is there an os independent way to check if a python app is running? Goal: I have a server program based on cherrypy that I only want to have running once. If a system administrator accidentally attempts to run this program more than once, I would like the 2nd instance of the program to detect that its already running and exit. Thank you, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On 12/14/2009 1:03 PM, Dave wrote: Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. I believe this has been proposed and rejected on one of the py-dev, py-ideas, or py-3k lists, but I would have to check to be sure. Extended slices would also have to be allowed. The Numeric community would also like this, Evidence? Are you one of the leaders thereof? as would the general python user. A few might but most would find it useless since they never write explicit slice objects and would have to learning something new to read code like the below. Many more people uses range objects (xrange in 2.x). A range object has the same info as a slice object *plus* it is iterable. So it would be MUCH more useful if that notation created a range object. for i in [1:n]: ... So I would oppose the slice proposal in favor of a range proposal. However, his has also, I believe, been rejected, as an abbreviation too far. Several times now I've wanted python slice notation. Perhaps I'll write a Python Enhancement Proposal. That could be useful, even if it gets rejected. Or perhaps this should be added to 3099. edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ slice(i*n,(i+1)*n,1), # rows slice(i,nn,n), # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = slice(n-1,n*(n-1)+1,n-1) backslash = slice(0,nn,n+1) Which could have been written in a manner completely consistent with other python shorthand notations Python avoids getting to chicken-scratchy. There was even a proposal (rejected, see 3099) to deprecate [1,2,3], etc, in favor of list(1,2,3), etc. and for which python cannot possibly use the notation for some other purpose, But it could, see above. edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ [i*n: (i+1)*n] # rows [i: nn: n], # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = [n-1: n*(n-1)+1: n-1] backslash = [0: nn: n+1] I find this currently to be less readable. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: str and unicode proper usage
gizli wrote: Hi all, If an entire application operates on Unicode strings from UI to database, is there a use case for str() and unicode() functions? The application should be able to read/write files, open sockets and execute external processes and parse their output. From my own experiments, the open() command for files accepts unicode strings. I am just wondering if there is a place where str() would have to be used, other than the usual use case of converting a non-string python construct (such as an integer) into a string. The reason I am asking is, I work on a project with several other developers and our NLS testing is not going so well. Major reason is (I think) that there is a lot of str() functions interspersed everywhere. So whenever a unicode character is used in those variables, the application breaks. My recommendation to the team was to remove these functions and only leave the necessary ones. However, I do not have a generic answer on when a str() function is necessary. Thanks! Consider switching to Python 3.x, if you aren't using any incompatible 3rd party libraries. There, the str type is always Unicode, and literals are interpreted as Unicode. But if 3.x isn't an option, I'd say you only need 8bit strings when doing I/O to 8 bit devices and files. You might also need them when talking to a program not under your own control. But if it's feasible, convert input data immediately to Unicode, do all your processing (including all literal strings) in Unicode, and convert back on output. You may also need 8bit strings for some OS calls, but if you're writing portable code, those should be minimized. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On 12/14/2009 1:10 PM, geremy condra wrote: http://www.python.org/dev/peps/pep-3003/ The moratorium does not stop proposals for things to be added after the moratorium ends. But it does show that Guido and the devs are reluctant to make *any* change to the core syntax of 3.x without really good reason. Absent that, I would not mind if the syntax remains frozen for the rest of 3.x. A minor abbreviation that makes the language look more like Perl will not cut it. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
2009/12/14 pyt...@bdurham.com: Is there an os independent way to check if a python app is running? if True: print I'm running. ;-) -- Cheers, Simon B. -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
On 12/15/2009 4:13 AM, mattia wrote: of course it is broken as long as it uses it's instance id. i added this to notify that unhashable can become hashable implementing __hash__ inside the class. which probably set to None by default. Ok, nice example, but I believe that using id() as the hash function can lead to unexpected collisions. For dict and set to work correctly, the hash function must conform to the contract that: - if A == B then hash(A) == hash(B) If the id() of two objects differ but their content equal (i.e. they are two equivalent, but distinct object), they should have the same hash. If id() is used for the hash of an arbitrary object, the contract will be broken unless you define A == B in terms of id(A) == id(B). -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Part Two
On Dec 14, 12:55 pm, Sancar Saran sancar.sa...@evodot.com wrote: Hello Again. I hope, I don't bug too much. First of all. I want to Thank to everyone who respond my messages. I was able to do some of my needs and stuck some others. So ? I need help again. And here my progress.. Following was my globalized registry solution # -*- coding: utf-8 -*- class Registry: data = {} def __init__(self,environ): self.data['env'] = environ self.data['init'] = 'hede' def set_entry(self,key,data): self.data[key] = data def get_entry(self,key): return self.data[key] def debug(self): r = 'pre' r += repr(self.data) r += '/pre' return r I have some questions about this code. [snip] On a side note -- data will be a class level attribute, rather than I'm guessing a desired instance level one. class Blah: data = {} def __init__(self, whatever): self.data[whatever] = 'asfasdf' x = Blah(3) y = Blah(4) Blah.data {3: 'asfasdf', 4: 'asfasdf'} x.data {3: 'asfasdf', 4: 'asfasdf'} y.data {3: 'asfasdf', 4: 'asfasdf'} As opposed to: class Blah: def __init__(self, whatever): self.data = {} self.data[whatever] = 'asfasdf' x = Blah(3) y = Blah(4) x.data {3: 'asfasdf'} y.data {4: 'asfasdf'} Blah.data Traceback (most recent call last): File pyshell#18, line 1, in module Blah.data AttributeError: class Blah has no attribute 'data' Jon. -- http://mail.python.org/mailman/listinfo/python-list
race/deadlock when creating a multiprocessing.manager instance while importing a module ?
hi there, say I have this module ## farnsworth ## __all__ = [ 'mgr', 'maths', ] from multiprocessing.managers import BaseManager class MathsClass(object): def add(self, x, y): return x + y def mul(self, x, y): return x * y class MyManager(BaseManager): pass MyManager.register('Maths', MathsClass) def _setup(): print creating a manager... mgr = MyManager() print starting the manager... mgr.start() print sciencing faster... maths = mgr.Maths() print maths.add(4,3) print maths.mul(7,8) print done with sciencing. return (mgr, maths) # exec at module import mgr, maths = _setup() # prevent hysteresis + clean-up del _setup ## EOF ## if I use it like so: $ python -m farnsworth creating a manager... starting the manager... sciencing faster... 7 56 done with sciencing. all is fine, but if I try to use it thru an import: $ python py import farnsworth.mgr as mgr creating a manager... starting the manager... sciencing faster... [stuck for some time... hitting ^C] Traceback (most recent call last): File stdin, line 1, in module File farnsworth.py, line 32, in module mgr, maths = _setup() File farnsworth.py, line 25, in _setup maths = mgr.Maths() File /usr/lib/python2.6/multiprocessing/managers.py, line 634, in temp token, exp = self._create(typeid, *args, **kwds) File /usr/lib/python2.6/multiprocessing/managers.py, line 532, in _create conn = self._Client(self._address, authkey=self._authkey) File /usr/lib/python2.6/multiprocessing/connection.py, line 140, in Client answer_challenge(c, authkey) File /usr/lib/python2.6/multiprocessing/connection.py, line 372, in answer_challenge message = connection.recv_bytes(256) # reject large message KeyboardInterrupt is this a known limitation/feature of the multiprocessing module ? is there a workaround (acquiring some import lock maybe) ? cheers, sebastien. PS: $ python Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) [GCC 4.4.1] on linux2 Type help, copyright, credits or license for more information. $ python -c 'import sys; print sys.version_info' (2, 6, 4, 'final', 0) $ uname -a Linux farnsworth 2.6.31-ARCH #1 SMP PREEMPT Tue Nov 10 19:01:40 CET 2009 x86_64 Intel(R) Core(TM)2 Duo CPU T9400 @ 2.53GHz GenuineIntel GNU/Linux -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On 12/15/2009 5:03 AM, Dave wrote: Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. Following example comes from projecteuler.net problem 166. The Numeric community would also like this, as would the general python user. The slice notation would require one : between the brackets to differentiate it from a list, which is similar to the set notation requirement that disambiguates it from a dictionary. I would prefer [a: b, ...] syntax to become an ordered dictionary literal (if it would ever gain traction). -- http://mail.python.org/mailman/listinfo/python-list
Re: str and unicode proper usage
On 12/15/2009 5:05 AM, gizli wrote: Hi all, If an entire application operates on Unicode strings from UI to database, is there a use case for str() and unicode() functions? The application should be able to read/write files, open sockets and execute external processes and parse their output. From my own experiments, the open() command for files accepts unicode strings. I am just wondering if there is a place where str() would have to be used, other than the usual use case of converting a non-string python construct (such as an integer) into a string. The reason I am asking is, I work on a project with several other developers and our NLS testing is not going so well. Major reason is (I think) that there is a lot of str() functions interspersed everywhere. So whenever a unicode character is used in those variables, the application breaks. My recommendation to the team was to remove these functions and only leave the necessary ones. However, I do not have a generic answer on when a str() function is necessary. Thanks! str() is rightly renamed byte() in python 3. The new name reflects its real role: as generic binary string. Use str()/byte() to represent non-textual stream of binary data (e.g. audio/video stream, .doc file, or cipher-text). -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Part Two
class Registry: data = {} def __init__(self,environ): self.data['env'] = environ self.data['init'] = 'hede' def set_entry(self,key,data): self.data[key] = data def get_entry(self,key): return self.data[key] def debug(self): r = 'pre' r += repr(self.data) r += '/pre' return r Just thought of something else: set_entry and get_entry are probably (in your example) better written as: def __getitem__(self, key): return self.data[key] def __setitem__(self, key, val): self.data[key] = val Assuming the syntax makes sense for the object. Then just use object[4] = 'adsfasfd' and object[4] syntax... Or as a getter/setter as per http://docs.python.org/library/functions.html#property Or depending on the use case for your class, just inherit from the built-in dict and get its functionality. class Test(dict): def debug(self, whatever): print whatever x = Test() x[3] ='adfadsf' x[3] 'adfadsf' x.debug('test') test hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
On Mon, Dec 14, 2009 at 11:02, Donn donn.in...@gmail.com wrote: On Monday 14 December 2009 00:10:52 David Boddie wrote: Doesn't the author give his e-mail address at the end of the video? (Maybe I'm thinking of a different video.) Yes, in a quick and garbled way :) I have yet to try to contact the author or the youtube poster -- been too busy. I was hoping someone on the list may recognize what tools he was using, or have some insight into how they would attack the problem. I have pondered it from a wxPython pov, that being all I am experienced with and I would have no chance of recreating that demo. Is it using some kind of built-in QT/KDE voodoo? Doesn't look like he's using KDE -- the filemanager he's using to choose images looks rather like Nautilus. I know KDE 4's Plasma framework has a ZUI and pretty good Python bindings, but I know very little about them other than that they exist. Rami Chowdhury Never assume malice when stupidity will suffice. -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
one way is you can create a lock file, then when the program start you check to see if this lock file exists. -Alex Goretoy -- http://mail.python.org/mailman/listinfo/python-list
Re: md5 strange error
Rami Chowdhury wrote: On Mon, Dec 14, 2009 at 09:33, catalinf...@gmail.com catalinf...@gmail.com wrote: now i have Fedora 12 Now when i try to use md5 , python say : python Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 Type help, copyright, credits or license for more information. import md5 __main__:1: DeprecationWarning: the md5 module is deprecated; use hashlib instead import md5 Why ? As the message says: the md5 module is deprecated, and you should use the hashlib module instead. I believe md5 is deprecated from Python 2.6 onwards, which may be why you have not seen this message before (Fedora 12 is the first Fedora to ship with Python 2.6). When this gets in the way (package builds, etc.,) you can get rid of it by invoking Python with a command line argument: python -Wignore::DeprecationWarning Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On Dec 14, 1:57 pm, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Sun, 13 Dec 2009 22:56:55 -0800 (PST), sjdevn...@yahoo.com sjdevn...@yahoo.com declaimed the following in gmane.comp.python.general: The 3.1 documentation specifies that file.read returns bytes: file.read([size]) Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non- blocking mode, less data than was requested may be returned, even if no size parameter was given. Does it need fixing? I'm still running 2.5 (Maybe next spring I'll see if all the third party libraries I have exist in 2.6 versions)... BUT... ... are returned as a string object... Aren't strings in 3.x now unicode? Which would imply, to me, that the interpretation of the contents will not be plain bytes. I'm not even concerned (yet) about how the data is interpreted after it's read. First I'm trying to clarify what exactly gets read. The post I was replying to said In Python 3.x, f.read(1) will read one character, which may be more than one byte depending on the encoding. That seems at odds with the documentation saying Read at most size bytes from the file--the fact that it's documented to read size bytes rather than size (possibly multibyte) characters is emphasized by the later language saying that the underlying C fread() call may be called enough times to read as close to size bytes as possible. If the poster I was replying to is correct, it seems like a documentation update is in order. As a long-time programmer, I would be very surprised to make a call to f.read(X) and have it return more than X bytes if I hadn't read this here. -- http://mail.python.org/mailman/listinfo/python-list
Re: a huge shared read-only data in parallel accesses -- How? multithreading? multiprocessing?
On Dec 11, 11:00 am, Antoine Pitrou solip...@pitrou.net wrote: I was going to suggest memcached but it probably serializes non-atomic types. Atomic as well. memcached communicates through sockets[3] (albeit possibly unix sockets, which are faster than TCP ones). multiprocessing has shared memory schemes, but does a lot of internal copying (uses ctypes)... and are particularly unhelpful when your shared data is highly structured, since you can't share objects, only primitive types. I finished a patch that pushes reference counters into packed pools. It has lots of drawbacks, but manages to solve this particular problem, if the data is prominently non-numeric (ie: lists and dicts, as mentioned before). Of the drawbacks, perhaps the bigger is a bigger memory footprint - yep... I don't believe there's anything that can be done to change that. It can be optimized, to make the overhead a little less though. This test code[1] consumes roughly 2G of RAM on an x86_64 with python 2.6.1, with the patch, it *should* use 2.3G of RAM (as specified by its output), so you can see the footprint overhead... but better page sharing makes it consume about 6 times less - roughly 400M... which is the size of the dataset. Ie: near-optimal data sharing. This patch[2] has other optimizations intermingled - if there's interest in the patch without those (which are both unproven and nonportable) I could try to separate them. I will have to, anyway, to upload for inclusion into CPython (if I manage to fix the shortcomings, and if it gets approved). The most important shortcomings of the refcount patch are: 1) Tripled memory overhead of reference counting. Before, it was a single Py_ssize_t per object. Now, it's two pointers plus the Py_ssize_t. This could perhaps be optimized (by getting rid of the arena pointer, for instance). 2) Increased code output for Py_INCREF/DECREF. It's small, but it adds up to a lot. Timings on test_decimal.py (a small numeric benchmark I use, which might not be representative at all) shows a 10% performance loss in CPU time. Again, this might be optimized with a lot of work and creativity. 3) Breaks binary compatibility, and in weird cases source compatibility with extension modules. PyObject layout is different, so statically-initialized variables need to stick to using CPython's macros (I've seen cases when they don't), and code should use Py_REFCNT () for accessing the refcount, but many just do ob-ob_refcnt, which will break with the patch. 4) I'm also not really sure (haven't tested) what happens when CPython runs out of memory - I tried real hard not to segfault, even recover nicely, but you know how hard that is... [3] http://code.google.com/p/memcached/wiki/FAQ#How_does_it_compare_to_a_server_local_cache?_(PHP%27s_APC,_mm [2] http://www.deeplayer.com/claudio/misc/Python-2.6.1-refcount.patch [1] test code below import time from multiprocessing import Pool def usoMemoria(): import os import subprocess pid = os.getpid() cmd = ps -o vsz=,rss=,share= -p %s --ppid %s % (pid,pid) p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) info = p.stdout.readlines() s = sum( int(r) for v,r,s in map(str.split,map(str.strip, info)) ) return s def f(_): return sum(int(x) for d in huge_global_data for x in d if x != state) # my sofisticated formula goes here if __name__ == '__main__': huge_global_data = [] for i in xrange(50): d = {} d[str(i)] = str(i*10) d[str(i+1)] = str(i) d[state] = 3 huge_global_data.append(d) p = Pool(7) res= list(p.map(f, xrange(20))) print %.2fM % (usoMemoria() / 1024.0) -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
pyt...@bdurham.com wrote: Is there an os independent way to check if a python app is running? Goal: I have a server program based on cherrypy that I only want to have running once. If a system administrator accidentally attempts to run this program more than once, I would like the 2nd instance of the program to detect that its already running and exit. You could use lockfile: http://pypi.python.org/pypi/lockfile/0.7 If a certain file exists and is locked, then the app is already running. -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
Simon, if True: print I'm running. ;-) LOL! Yes, I should of worded my original post better (meant to say ... if a python app is already running. Enjoyed your post anyway - I'm still laughing :) Cheers, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
What type of info do you capture about a user's environment for debugging?
We're preparing to release a commercial software product based on Python. When a customer reports problems with our software, we would like to capture as much information about their environment as possible while being respectful of privacy concerns. The info we capture will be displayed to the user for review and optional submission to our support team. Here's what we've come up with so far: - platform module info - locale module info - os.environ (selected info) - sys.argv (application path and command line arguements) Free disk space and write status for the following paths: - application folder - startup folder (which may be different than application folder) - temp path - user path (My Documents, Home) Information captured via our browser interface - display resolution - browser version Anyone have any additional suggestions or feedback? Thanks! Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
MRAB schrieb: pyt...@bdurham.com wrote: Is there an os independent way to check if a python app is running? Goal: I have a server program based on cherrypy that I only want to have running once. If a system administrator accidentally attempts to run this program more than once, I would like the 2nd instance of the program to detect that its already running and exit. You could use lockfile: http://pypi.python.org/pypi/lockfile/0.7 If a certain file exists and is locked, then the app is already running. Not only exists, he should also use the OS' locking mechanisms. The file could otherwise be stale. We use this: import platform is_windows = False if platform.system() == 'Windows': is_windows = True import os class LockFileCreationException(Exception): pass class LockFile(object): def __init__(self, name, fail_on_lock=False, cleanup=True): self.name = name self.cleanup = cleanup try: self.fd = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_APPEND) except OSError, e: if e[0] == 2: raise LockFileCreationException() self.file = os.fdopen(self.fd, w) if is_windows: lock_flags = msvcrt.LK_LOCK else: lock_flags = fcntl.LOCK_EX if fail_on_lock: if is_windows: lock_flags = msvcrt.LK_NBLCK else: lock_flags |= fcntl.LOCK_NB try: if is_windows: msvcrt.locking(self.file.fileno(), lock_flags, 1) else: fcntl.flock(self.file, lock_flags) except IOError, e: if e[0] == 11: raise LockObtainException() raise def __enter__(self): return self.file def __exit__(self, unused_exc_type, unused_exc_val, unused_exc_tb): self.file.close() # we are told to cleanup after ourselves, # however it might be that another process # has done so - so we don't fail in that # case. if self.cleanup: try: os.remove(self.name) except OSError, e: if not e[0] == 2: raise Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Moving from PHP to Python. Part Two
On 12/14/2009 2:49 PM, Jon Clements wrote: class Registry: data = {} def __init__(self,environ): self.data['env'] = environ self.data['init'] = 'hede' def set_entry(self,key,data): self.data[key] = data def get_entry(self,key): return self.data[key] def debug(self): r = 'pre' r += repr(self.data) r += '/pre' return r Since this would be a singleton, skip it and just make a module 'registry'__ that you import everywhere Include the following function: def _debug(): r = ['pre'] d = {k:v for k,v in globals().items() if not k.startswith('_')} r += repr(d) r += '/pre' return ' '.join(r) Then a = 3 b = 'ab' print(_debug()) prints pre { ' a ' : 3 , ' b ' : ' a b ' } / p r e From outside the module, registry._debug() will produce the string. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: What type of info do you capture about a user's environment for debugging?
On Dec 14, 2009, at 3:39 PM, pyt...@bdurham.com wrote: We're preparing to release a commercial software product based on Python. When a customer reports problems with our software, we would like to capture as much information about their environment as possible while being respectful of privacy concerns. The info we capture will be displayed to the user for review and optional submission to our support team. Here's what we've come up with so far: - platform module info - locale module info - os.environ (selected info) - sys.argv (application path and command line arguements) Free disk space and write status for the following paths: - application folder - startup folder (which may be different than application folder) - temp path - user path (My Documents, Home) Information captured via our browser interface - display resolution - browser version Anyone have any additional suggestions or feedback? Python version? =) Also the contents of site-packages might be useful if you can get that. -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On Sun, 13 Dec 2009 22:56:55 -0800, sjdevn...@yahoo.com wrote: The 3.1 documentation specifies that file.read returns bytes: Does it need fixing? There are no file objects in 3.x. The file() function no longer exists. The return value from open(), will be an instance of _io.something depending upon the mode, e.g. _io.TextIOWrapper for 'r', _io.BufferedReader for 'rb', _io.BufferedRandom for 'w+b', etc. http://docs.python.org/3.1/library/io.html io.IOBase.read() doesn't exist, io.RawIOBase.read(n) reads n bytes, io.TextIOBase.read(n) reads n characters. -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On Mon, 14 Dec 2009 13:40:38 -0500, Colin W. wrote: Yes, we know that PEP 3003 applies but I see no harm in discussing possible enhancements. You bored? Looking for something to do? I've lost all enthusiasm for discussing language enhancements, regardless of whether I'm for or against the change, knowing that there's no way it could be added to the language, and when the Python moratorium ends the discussion will just happen all over again. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On Dec 14, 10:03 am, Dave b49p23t...@stny.rr.com wrote: Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. Following example comes from projecteuler.net problem 166. The Numeric community would also like this, as would the general python user. The slice notation would require one : between the brackets to differentiate it from a list, which is similar to the set notation requirement that disambiguates it from a dictionary. Several times now I've wanted python slice notation. Perhaps I'll write a Python Enhancement Proposal. I stored slices of vector array entries to add edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ slice(i*n,(i+1)*n,1), # rows slice(i,nn,n), # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = slice(n-1,n*(n-1)+1,n-1) backslash = slice(0,nn,n+1) Which could have been written in a manner completely consistent with other python shorthand notations and for which python cannot possibly use the notation for some other purpose, edge = 4 indexes = [] n = edge nn = n**2 for i in range(edge): indexes.extend([ [i*n: (i+1)*n] # rows [i: nn: n], # cols ]) row_slices = indexes[0::2] col_slices = indexes[1::2] slash = [n-1: n*(n-1)+1: n-1] backslash = [0: nn: n+1] -1 Explicit creation of slice objects is an uncommon need and there is no reason to support it with its own syntax. I'd agree with Terry Reedy that range/xrange is far more commonly used than slice objects, and if a floating slice syntax were ever added to Python it ought to be used for range. If you need to use a lot of slice objects you can lower your code footprint by defining a helper class like this (adapt as needed): class SliceCreator(object): def __getitem__(self,loc): if not isinstance(loc,slice): raise TypeError return loc slc = SliceCreator() slash = slc[n-1: n*(n-1)+1: n-1] It might have been a reasonable idea for slice (and, perhaps, range) to use slice notation rather than a function call, on the thinking that the notational convenience outweighs the fact that you're not actually getting an item, but it's too late for that. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On Mon, 14 Dec 2009 03:14:11 +, MRAB wrote: You originally stated that you want to scramble the bytes -- if you mean to implement some sort of encryption algorithm you should know that most of them work in blocks as the key is longer than one byte. Block ciphers work in blocks. Stream ciphers work on bytes, regardless of the length of the key. It's still more efficient to read in blocks, even if you're going to process the bytes one at a time. That's fine for a file. If you're reading from a pipe, socket, etc, you typically want to take what you can get when you can get it (although this is easier said than done in Python), rather than waiting for a complete block. This is often a primary reason for choosing a stream cipher over a block cipher, as it eliminates the need to add and remove padding for intermittent data flows. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
Le Mon, 14 Dec 2009 15:21:09 +, exarkun a écrit : I'm asking about why the behavior of a StopIteration exception being handled from the `expression` of a generator expression to mean stop the loop is accepted by the devs as acceptable. It's not accepted as acceptable, it's just a side effect of how various means of iterating (including for loops and generators) are implemented in CPython. Seeing how it doesn't seem to prevent or promote any useful programming idiom, there was no incentive to either 1) codify it as official spec or 2) change it. In other words, it should be considered undefined behaviour, and perhaps other Python implementations behave differently. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
mock any instance
Pythonistas: One important design principle is construction encapsulation. That's where nobody creates anything, they always use things passed to them. Without this principle, when I need to mock the encapsulated item, some mock libraries provide an any instance facility. For example, here's a mock on one method of only one object: object = Thang() object.method = Mock() object.method() # -- calls the mock, not the real method I need to mock the method on any instance spawned by a given class: Thang.method = MockAnyInstance() object = Thang() object.method() # -- calls the mock, not the real method That will avoid cutting a hole in the bulkhead just to get to the 'object = Thang()' line, to let me spoof out the Thang. Does anyone have any idea how to do that with the good-old Mocker here? http://python-mock.sourceforge.net/ if I should use a different mocker, I would prefer it behave like that mock, for aesthetic reasons, and also to avoid the need to replace all our existing Mocks with a new one, following the rule that we should not use too many classes to do the same thing (DRY). -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On Mon, 14 Dec 2009 10:03:16 -0800, Dave wrote: Just as sets may now be written as {3,'hi'}, I propose that slices should be available using [start:end] syntax. Following example comes from projecteuler.net problem 166. The Numeric community would also like this, as would the general python user. The slice notation would require one : between the brackets to differentiate it from a list, which is similar to the set notation requirement that disambiguates it from a dictionary. Several times now I've wanted python slice notation. Perhaps I'll write a Python Enhancement Proposal. Would it suffice to add the equivalent of numpy.s_ as a builtin? from numpy import s_ s_[1:2:3] slice(1, 2, 3) s_[1:2:3, ..., 4:5] (slice(1, 2, 3), Ellipsis, slice(4, 5, None)) Or would it be possible to define slice itself so that it implements __getitem__ and __getslice__? -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
On Mon, 14 Dec 2009 17:13:24 +, mattia wrote: Il Sun, 13 Dec 2009 21:17:28 -0800, knifenomad ha scritto: On 12월14일, 오후12시42분, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Sun, 13 Dec 2009 17:19:17 -0800, knifenomad wrote: this makes the set type hashable. class Set(set): __hash__ = lambda self: id(self) That's a *seriously* broken hash function. key = voila d = { Set(key): 1 } d {Set(['i', 'a', 'l', 'o', 'v']): 1} d[ Set(key) ] Traceback (most recent call last): File stdin, line 1, in module KeyError: Set(['i', 'a', 'l', 'o', 'v']) -- Steven of course it is broken as long as it uses it's instance id. i added this to notify that unhashable can become hashable implementing __hash__ inside the class. which probably set to None by default. Ok, nice example, but I believe that using id() as the hash function can lead to unexpected collisions. No, you have that backwards. Using id() as the hash function means that equal keys will hash unequal -- rather than unexpected collisions, it leads to unexpected failure-to-collide-when-it-should-collide. And it isn't a nice example, it is a terrible example. Firstly, the example fails to behave correctly. It simply doesn't work as advertised. Secondly, and much worse, it encourages people to do something dangerous without thinking about the consequences. If it is so easy to hash mutable objects, why don't built-in lists and sets don't have a __hash__ method? Do you think that the Python developers merely forgot? No, there is good reason why mutable items shouldn't be used as keys in dicts or in sets, and this example simply papers over the reasons why and gives the impression that using mutable objects as keys is easy and safe when it is neither. Using mutable objects as keys or set elements leads to surprising, unexpected behaviour and hard-to-find bugs. Consider the following set with lists as elements: L = [1, 2] s = Set() # set that allows mutable elements s.add(L) s.add([1, 2, 3]) So far so good. But what should happen now? L.append(3) The set now has two equal elements, which breaks the set invariant that it has no duplicates. Putting the problem of duplicates aside, there is another problem: L = [1, 2] s = Set([L]) L.append(3) There are two possibilities: either the hash function of L changes when the object changes, or it doesn't. Suppose it changes. Then the hash of L after the append will be different from the hash of L before the append, and so membership testing (L in s) will fail. Okay, suppose we somehow arrange matters so that the hash of the object doesn't change as the object mutates. This will solve the problem above: we can mutate L as often as we like, and L in s will continue to work correctly. But now the hash of an object doesn't just depend on it's value, but on its history. That means that two objects which are identical can hash differently, and we've already seen this is a problem. There is one final approach which could work: we give the object a constant hash function, so that all objects of that type hash identically. This means that the performance of searches and lookups in sets and dicts will fall to that of lists. There is no point in paying all the extra overhead of a dict to get behaviour as slow, or slower, than a list. In other words, no matter what you do, using mutable objects as keys or set elements leads to serious problems that need to be dealt with. It simply isn't true that all you need to do to make mutable objects usable in dicts or sets is to add a hash function. That part is trivial. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On Dec 14, 7:21 am, exar...@twistedmatrix.com wrote: Note, I know *why* the implementation leads to this behavior. I'm asking why the devs *accept* this. As noted, the problem isn't with generators but with iteration protocol. The devs allowed this because it was a necessary evil for correct functionality. As the system is set up it can't discriminate between a legitimate and a spurrious StopIteration. (Which is why Steven and others called you out for suggesting that the compiler has to read your mind.) However, as far as I'm concerned there is no reasonable argument that this behavior is good. So how, hypothetically, would one go about fixing it, short of ripping out and replacing the existing machinery? The first argument is that StopIteration has no place within a generator expression. Therefore a generator expression (but not a generator function) could deliberately catch StopIteration and raise a different exception. I don't like it, though: who says that StopIteration has no place within a generator expression? Currently it's possible to do something like this to terminate a genexp early, and I won't the one saying you shouldn't do it. def stop(): raise StopIteration list(x or stop() for x in stream) (Though personally I'd bite the bullet and write it as a generator function). What else? The way I see it, when you throw StopIteration you are trying to stop a specific generator. Therefore StopIteration should (somehow) contain a reference to the generator that it's being applied to. Perhaps it can be obtained by crawling ths stack, which shouldn't be a significant penalty (it'd be only called once per iteration, and most of the time it'd be only one or two frames up). The looping logic within Python should check whether the reference matches the object it's iterating over; if not it raises a LeakyLoopException or something like that. I haven't thought this out though, I'm just kind of throwing this out there. Any issues? But to answer your question, I think simple is better than complex rules the day. Right now StopIteration stops an iteration, simple as that. Any fix would add complexity. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: setup.py and PyPI
Philip Semanchuk wrote: On Dec 14, 2009, at 1:00 PM, Ethan Furman wrote: Philip Semanchuk wrote: On Dec 14, 2009, at 12:04 PM, Ethan Furman wrote: Greetings! I'm using Python 2.5 on Windows XP, and trying to get the upload portion of setup.py to work. According to what I have found, I can put my info into a .pypirc file to have the process pick up my username/password and upload the files. I have tried putting this file into the same folder I'm running setup.py from, into my home folder (USERPROFILE, I think) -- and the really frustrating part is that it worked a couple times (but maybe that was from my home computer) but mostly it does not. When it fails, how does it fail? python setup.py sdist bdist --format wininst upload [snip] running upload Submitting dist\dbf-0.87.7.zip to http://pypi.python.org/pypi Upload failed (401): You must be identified to edit package information Looks like there's known problems with this under Windows that might affect your version of Python, depending on which version you have. http://bugs.python.org/issue1741 Setting a HOME environment variable to point to wherever you have your .pypirc file might help. Hope this helps P The HOME variable did it! Many, many thanks!! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing html with Beautifulsoup
En Mon, 14 Dec 2009 03:58:34 -0300, Johann Spies jsp...@sun.ac.za escribió: On Sun, Dec 13, 2009 at 07:58:55AM -0300, Gabriel Genellina wrote: cell.findAll(text=True) returns a list of all text nodes inside a td cell; I preprocess all \n and nbsp; in each text node, and join them all. lines is a list of lists (each entry one cell), as expected by the csv module used to write the output file. I have struggled a bit to find the documentation for (text=True). Most of documentation for Beautifulsoup I saw mostly contained some examples without explaining what the options do. Thanks for your explanation. See http://www.crummy.com/software/BeautifulSoup/documentation.html#arg-text As far as I can see there was no documentation installed with the debian package. BeautifulSoup is very small - a single .py file, no dependencies. The whole documentation is contained in the above linked page. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On Mon, 14 Dec 2009 14:31:44 +, exarkun wrote: On 06:46 am, tjre...@udel.edu wrote: On 12/13/2009 10:29 PM, exar...@twistedmatrix.com wrote: Doesn't matter. Sometimes it makes sense to call it directly. It only makes sense to call next (or .__next__) when you are prepared to explicitly catch StopIteration within a try..except construct. You did not catch it, so it stopped execution. Let me repeat: StopIteration is intended only for stopping iteration. Outside that use, it is a normal exception with no special meaning. You cut out the part of my message where I wrote that one might have forgotten the exception handling code that you posit is required, and that the current behavior makes debugging this situation unnecessarily challenging. I don't see why you think this is any more challenging to debug than any other equivalent bug. If anything I would think it was easier to debug: if the problem is that you get a StopIteration traceback, well that's easy and straightforward, and if the problem is that you don't (and consequently you end up with fewer items in the list than you expect), the obvious debugging technique is to build the list by hand and inspect each item before adding it to the list: L = [] for i, item in enumerate(iterable): print i, item, value = item() # raises StopIteration print value L.append(value) That will expose the StopIteration exception and reveal the problem. But even if I have missed something, and it is a challenging problem to debug, oh well. It should be a quite unusual situation to come across. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
print format
Hi all, I wanto to print just the first 5 characters of a string, why this doesn't work (py3.1)? print({0:5}.format(123456789)) 123456789 I know I could use print(123456789[:5]), yeah it's a stupid example, but isn't format for string formatting? Thanks, Mattia -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek support for new slice syntax PEP.
On Mon, 14 Dec 2009 18:40:38 -, Colin W. cjwilliam...@gmail.com wrote: If your scheme flies, would it be practicable to use the same syntax as a range generator? range(i, j, k) = i:j:k so range(10, 2) = :10:2 i.e. we could write for i in :10:2: or the more common: range(10) = :10 Ugh. Magic characters. Let's not. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: insert unique data in a list
Il Mon, 14 Dec 2009 21:53:38 +, Steven D'Aprano ha scritto: On Mon, 14 Dec 2009 17:13:24 +, mattia wrote: Il Sun, 13 Dec 2009 21:17:28 -0800, knifenomad ha scritto: On 12월14일, 오후12시42분, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Sun, 13 Dec 2009 17:19:17 -0800, knifenomad wrote: this makes the set type hashable. class Set(set): __hash__ = lambda self: id(self) That's a *seriously* broken hash function. key = voila d = { Set(key): 1 } d {Set(['i', 'a', 'l', 'o', 'v']): 1} d[ Set(key) ] Traceback (most recent call last): File stdin, line 1, in module KeyError: Set(['i', 'a', 'l', 'o', 'v']) -- Steven of course it is broken as long as it uses it's instance id. i added this to notify that unhashable can become hashable implementing __hash__ inside the class. which probably set to None by default. Ok, nice example, but I believe that using id() as the hash function can lead to unexpected collisions. No, you have that backwards. Using id() as the hash function means that equal keys will hash unequal -- rather than unexpected collisions, it leads to unexpected failure-to-collide-when-it-should-collide. And it isn't a nice example, it is a terrible example. Firstly, the example fails to behave correctly. It simply doesn't work as advertised. Secondly, and much worse, it encourages people to do something dangerous without thinking about the consequences. If it is so easy to hash mutable objects, why don't built-in lists and sets don't have a __hash__ method? Do you think that the Python developers merely forgot? No, there is good reason why mutable items shouldn't be used as keys in dicts or in sets, and this example simply papers over the reasons why and gives the impression that using mutable objects as keys is easy and safe when it is neither. Using mutable objects as keys or set elements leads to surprising, unexpected behaviour and hard-to-find bugs. Consider the following set with lists as elements: L = [1, 2] s = Set() # set that allows mutable elements s.add(L) s.add([1, 2, 3]) So far so good. But what should happen now? L.append(3) The set now has two equal elements, which breaks the set invariant that it has no duplicates. Putting the problem of duplicates aside, there is another problem: L = [1, 2] s = Set([L]) L.append(3) There are two possibilities: either the hash function of L changes when the object changes, or it doesn't. Suppose it changes. Then the hash of L after the append will be different from the hash of L before the append, and so membership testing (L in s) will fail. Okay, suppose we somehow arrange matters so that the hash of the object doesn't change as the object mutates. This will solve the problem above: we can mutate L as often as we like, and L in s will continue to work correctly. But now the hash of an object doesn't just depend on it's value, but on its history. That means that two objects which are identical can hash differently, and we've already seen this is a problem. There is one final approach which could work: we give the object a constant hash function, so that all objects of that type hash identically. This means that the performance of searches and lookups in sets and dicts will fall to that of lists. There is no point in paying all the extra overhead of a dict to get behaviour as slow, or slower, than a list. In other words, no matter what you do, using mutable objects as keys or set elements leads to serious problems that need to be dealt with. It simply isn't true that all you need to do to make mutable objects usable in dicts or sets is to add a hash function. That part is trivial. I agree with you, and in fact I'm inserting tuples in my set. All the workaroun to use somehow mutable object are poor attemps to solve in a quick-and-dirty way a difficult problem like hashing. But I think that during the discussion we have slowly forgot the main topic of my question, that was insert unique objects in a container. Hash are good to retrieve items in constant time, and when we are dealing with collisions we have to provide some solutions, like chaining or open addressing. Note also that in hash collisions happen and the hash function is used to retrieve items, not to insert unique items. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dangerous behavior of list(generator)
On Dec 14, 2:48 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Mon, 14 Dec 2009 14:31:44 +, exarkun wrote: On 06:46 am, tjre...@udel.edu wrote: On 12/13/2009 10:29 PM, exar...@twistedmatrix.com wrote: Doesn't matter. Sometimes it makes sense to call it directly. It only makes sense to call next (or .__next__) when you are prepared to explicitly catch StopIteration within a try..except construct. You did not catch it, so it stopped execution. Let me repeat: StopIteration is intended only for stopping iteration. Outside that use, it is a normal exception with no special meaning. You cut out the part of my message where I wrote that one might have forgotten the exception handling code that you posit is required, and that the current behavior makes debugging this situation unnecessarily challenging. I don't see why you think this is any more challenging to debug than any other equivalent bug. Errors should never pass silently. I'm not saying it's necessarily difficult to debug--although building a list by hand to test it is a lot more work than reading an exception traceback--but it'a stark violation of a Zen and common sense, so it is more serious than other sorts of errors. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
On Monday 14 December 2009 20:02, Donn wrote: On Monday 14 December 2009 00:10:52 David Boddie wrote: Doesn't the author give his e-mail address at the end of the video? (Maybe I'm thinking of a different video.) Yes, in a quick and garbled way :) I have yet to try to contact the author or the youtube poster -- been too busy. I managed to catch his address and sent him a message saying that people were discussing PyZUI in this thread. David -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
Diez, Thank you for your sample code. That was just what we were looking for. Regards, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: parse a string of parameters and values
En Sun, 13 Dec 2009 07:28:24 -0300, Peter Otten __pete...@web.de escribió: bsneddon wrote: I am going to read a text file that is an export from a control system. It has lines with information like base=1 name=first one color=blue I would like to put this info into a dictionary for processing. import shlex s = 'base=1 name=first one color=blue equal=alpha=beta empty' dict(t.partition(=)[::2] for t in shlex.split(s)) {'color': 'blue', 'base': '1', 'name': 'first one', 'empty': '', 'equal': 'alpha=beta'} Brilliant! -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
On Tuesday 15 December 2009 01:43:52 David Boddie wrote: I managed to catch his address and sent him a message saying that people were discussing PyZUI in this thread. Oooh. Sits,fidgets and waits. I want my socks back! (OP) :D \d -- \/\/ave: donn.in...@googlewave.com home: http://otherwise.relics.co.za/ 2D vector animation : https://savannah.nongnu.org/projects/things/ Font manager : https://savannah.nongnu.org/projects/fontypython/ -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
En Mon, 14 Dec 2009 18:09:52 -0300, Nobody nob...@nowhere.com escribió: On Sun, 13 Dec 2009 22:56:55 -0800, sjdevn...@yahoo.com wrote: The 3.1 documentation specifies that file.read returns bytes: Does it need fixing? There are no file objects in 3.x. The file() function no longer exists. The return value from open(), will be an instance of _io.something depending upon the mode, e.g. _io.TextIOWrapper for 'r', _io.BufferedReader for 'rb', _io.BufferedRandom for 'w+b', etc. http://docs.python.org/3.1/library/io.html io.IOBase.read() doesn't exist, io.RawIOBase.read(n) reads n bytes, io.TextIOBase.read(n) reads n characters. So basically this section [1] should not exist, or be completely rewritten? At least the references to C stdio library seem wrong to me. [1] http://docs.python.org/3.1/library/stdtypes.html#file-objects -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
multiprocessing callbacks?
Hey, everyone - I'm trying to use multiprocessing to create a process pool similar to multiprocessing.Pool, except that it works across multiple hosts. For various reasons, I would like the result of async_apply() to return a Twisted Deferred object. My first attempt at the implementation is: class DeferredProxy(managers.BaseProxy): _exposed_ = (callback,errback,addCallback,addErrback) def callback(self, v): return self._callmethod(callback,(v,)) def errback(self, v): return self._callmethod(errback,(v,)) def addCallback(self, cb): return self._callmethod(addCallback,(cb,)) def addErrback(self, eb): return self.__callmethod(adderrback,(eb,)) class DispatchManager(managers.BaseManager): pass DispatchManager.register(Deferred, defer.Deferred, DeferredProxy) This, however, fails, because cb and eb cannot be pickled. I don't actually need to pickle them, though, because I want them to run on the machine on which they were created. Is there a way, in multiprocessing, that I can pass some sort of callback around that serves as a proxy to a remote function? Thanks in advance, Pete -- http://mail.python.org/mailman/listinfo/python-list
Re: parse a string of parameters and values
Gabriel Genellina wrote: Peter Otten escribió: bsneddon wrote: I am going to read a text file that is an export from a control system. It has lines with information like base=1 name=first one color=blue I would like to put this info into a dictionary for processing. import shlex s = 'base=1 name=first one color=blue equal=alpha=beta empty' dict(t.partition(=)[::2] for t in shlex.split(s)) {'color': 'blue', 'base': '1', 'name': 'first one', 'empty': '', 'equal': 'alpha=beta'} Brilliant! The thing I appreciated about Peter's solution was learning a purpose for .partition() as I've always just used .split(), so I would have done something like dict('=' in s and s.split('=', 1) or (s, '') for s in shlex.split(s)) {'color': 'blue', 'base': '1', 'name': 'first one', 'empty': '', 'equal': 'alpha=beta'} Using .partition() makes that a lot cleaner. However, it looks like .partition() was added in 2.5, so for my code stuck in 2.4 deployments, I'll stick with the uglier .split() -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: print format
mattia wrote: Hi all, I wanto to print just the first 5 characters of a string, why this doesn't work (py3.1)? print({0:5}.format(123456789)) 123456789 I know I could use print(123456789[:5]), yeah it's a stupid example, but isn't format for string formatting? That's because it's the _minimum_ width. -- http://mail.python.org/mailman/listinfo/python-list
Re: race/deadlock when creating a multiprocessing.manager instance while importing a module ?
Sebastien Binet wrote: hi there, say I have this module ## farnsworth ## __all__ = [ 'mgr', 'maths', ] from multiprocessing.managers import BaseManager class MathsClass(object): def add(self, x, y): return x + y def mul(self, x, y): return x * y class MyManager(BaseManager): pass MyManager.register('Maths', MathsClass) def _setup(): print creating a manager... mgr = MyManager() print starting the manager... mgr.start() print sciencing faster... maths = mgr.Maths() print maths.add(4,3) print maths.mul(7,8) print done with sciencing. return (mgr, maths) # exec at module import mgr, maths = _setup() # prevent hysteresis + clean-up del _setup ## EOF ## if I use it like so: $ python -m farnsworth creating a manager... starting the manager... sciencing faster... 7 56 done with sciencing. all is fine, but if I try to use it thru an import: $ python py import farnsworth.mgr as mgr creating a manager... starting the manager... sciencing faster... [stuck for some time... hitting ^C] Traceback (most recent call last): File stdin, line 1, in module File farnsworth.py, line 32, in module mgr, maths = _setup() File farnsworth.py, line 25, in _setup maths = mgr.Maths() File /usr/lib/python2.6/multiprocessing/managers.py, line 634, in temp token, exp = self._create(typeid, *args, **kwds) File /usr/lib/python2.6/multiprocessing/managers.py, line 532, in _create conn = self._Client(self._address, authkey=self._authkey) File /usr/lib/python2.6/multiprocessing/connection.py, line 140, in Client answer_challenge(c, authkey) File /usr/lib/python2.6/multiprocessing/connection.py, line 372, in answer_challenge message = connection.recv_bytes(256) # reject large message KeyboardInterrupt is this a known limitation/feature of the multiprocessing module ? is there a workaround (acquiring some import lock maybe) ? cheers, sebastien. PS: $ python Python 2.6.4 (r264:75706, Oct 27 2009, 06:25:13) [GCC 4.4.1] on linux2 Type help, copyright, credits or license for more information. $ python -c 'import sys; print sys.version_info' (2, 6, 4, 'final', 0) $ uname -a Linux farnsworth 2.6.31-ARCH #1 SMP PREEMPT Tue Nov 10 19:01:40 CET 2009 x86_64 Intel(R) Core(TM)2 Duo CPU T9400 @ 2.53GHz GenuineIntel GNU/Linux Since I don't see any other responses, I'll give my guess, even though I'm not very experienced with the multiprocessing module. It's my understanding that threads may not be created or destroyed during an import. So you need to find a way to defer the creation till the imports are done. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: pyZui - anyone know about this?
Hi, Yes, the toolkit used is PyQt. The ZUI is implemented using a simple QPainter, and employs pyramidal tiling for efficiency (I haven't used any Qt/KDE voodoo in this regard). I'm using Gnome at the moment, but it should work just as well on KDE. Web pages are rendered using QtWebKit, and PDF with the pdftoppm utility. The project is opensource (GPLv2), but just hasn't been published yet :) . I'll try to make a release over the next few days, and I'll post a link here when I do. -- David Roberts http://da.vidr.cc/ On Dec 15, 10:33 am, Donn donn.in...@gmail.com wrote: On Tuesday 15 December 2009 01:43:52 David Boddie wrote: I managed to catch his address and sent him a message saying that people were discussing PyZUI in this thread. Oooh. Sits,fidgets and waits. I want my socks back! (OP) :D \d -- \/\/ave: donn.in...@googlewave.com home:http://otherwise.relics.co.za/ 2D vector animation :https://savannah.nongnu.org/projects/things/ Font manager :https://savannah.nongnu.org/projects/fontypython/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python to Execute a C or FORTRAN Program (Windows)
On Mon, Dec 14, 2009 at 6:14 PM, W. eWatson wolftra...@invalid.com wrote: I think Python is capable of executing a compiled C or FORTRAN program, and maybe even getting some parameters passed back. Does anyone have a example of how this might be done? I'm running under Win XP Pro. import subprocess proc = subprocess.Popen(rC:\mydir\myprog.exe, stdin=subprocess.PIPE, stdout=subprocess.PIPE) out, err = proc.communicate() print Command returned, out See the subprocess module documentation for details. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
On Mon, 14 Dec 2009 14:14:05 -0500, python wrote: Is there an os independent way to check if a python app is running? Goal: I have a server program based on cherrypy that I only want to have running once. If a system administrator accidentally attempts to run this program more than once, I would like the 2nd instance of the program to detect that its already running and exit. Maybe I'm missing something, but the locking mechanism already exists: at some point, your server program has to bind to an IP port to listen for incoming request, and any respectable OS won't let two programs bind to the same port at the same time. So if binding to the input port works, then there *can't* be another instance of the program running (multiple configuration files notwithstanding, but then you'd need a second process anyway). I guess I am assuming that a server program based on cherrypy takes its input from an IP port, but that seems safe enough given the nature of cherrypy. Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: print format
mattia wrote: Hi all, I wanto to print just the first 5 characters of a string, why this doesn't work (py3.1)? print({0:5}.format(123456789)) 123456789 I know I could use print(123456789[:5]), yeah it's a stupid example, but isn't format for string formatting? Thanks, Mattia I tested following statement in 2.6, not 3.1, but I think it's the same. The width field of the format is a *minimum* width. So you can use it to force padding, but not truncation. If you need the string truncated, just use slicing, as you already showed. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On Dec 14, 4:09 pm, Nobody nob...@nowhere.com wrote: On Sun, 13 Dec 2009 22:56:55 -0800, sjdevn...@yahoo.com wrote: The 3.1 documentation specifies that file.read returns bytes: Does it need fixing? There are no file objects in 3.x. Then the documentation definitely needs fixing; the excerpt I posted earlier is from the 3.1 documentation's section about file objects: http://docs.python.org/3.1/library/stdtypes.html#file-objects Which begins: 5.9 File Objects File objects are implemented using C’s stdio package and can be created with the built-in open() function. File objects are also returned by some other built-in functions and methods, such as os.popen () and os.fdopen() and the makefile() method of socket objects. (It goes on to describe the read method's operation on bytes that I quoted upthread.) Sadly I'm not familiar enough with 3.x to suggest an appropriate edit. -- http://mail.python.org/mailman/listinfo/python-list
Re: read text file byte by byte
On 12/14/2009 7:37 PM, Gabriel Genellina wrote: En Mon, 14 Dec 2009 18:09:52 -0300, Nobody nob...@nowhere.com escribió: On Sun, 13 Dec 2009 22:56:55 -0800, sjdevn...@yahoo.com wrote: The 3.1 documentation specifies that file.read returns bytes: Does it need fixing? There are no file objects in 3.x. The file() function no longer exists. The return value from open(), will be an instance of _io.something depending upon the mode, e.g. _io.TextIOWrapper for 'r', _io.BufferedReader for 'rb', _io.BufferedRandom for 'w+b', etc. http://docs.python.org/3.1/library/io.html io.IOBase.read() doesn't exist, io.RawIOBase.read(n) reads n bytes, io.TextIOBase.read(n) reads n characters. So basically this section [1] should not exist, or be completely rewritten? At least the references to C stdio library seem wrong to me. [1] http://docs.python.org/3.1/library/stdtypes.html#file-objects I agree. http://bugs.python.org/issue7508 Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
treaps in python
I've just released my treap.py module: http://stromberg.dnsalias.org/~dstromberg/treap/ It's code that implements a datastructure that is a hybrid of a binary tree and a binary heap, having some of the advantages of each. The URL has a table comparing the asymptotic performance of treaps against some other common python datastructures. This particular implementation is pretty dicitionary-like, but you can also get the smallest value or the largest value in O(log2(n)) time, or get a forward or reverse ordered list of values in O(n) time. The price of course is that adding and deleting things is O(log2(n)) time. It's currently GPLv3-licensed, but I'd like to dual license it in such a way that it could eventually be included in the standard library. How would I go about this? Also, what's the best way to package something like this for consumption by python-folk? There seem to be so many ways of packaging things anymore. Are dist utils, a .deb and a .rpm the way to go? Right now, it's just using make to stuff things in /usr/local. There are two versions: One that is pure python, and one that is part python and part cython. They're automatically generated from a common m4 template. There are rather plentiful unit tests included in the tar archive. I hope someone besides me finds it useful. -- http://mail.python.org/mailman/listinfo/python-list
Re: treaps in python
On Dec 14, 8:49 pm, Dan Stromberg drsali...@gmail.com wrote: It's currently GPLv3-licensed, but I'd like to dual license it in such a way that it could eventually be included in the standard library. How would I go about this? I'm not going to try to talk you out of dual licensing it as something other than GPL, but you should note that we just had a thread discussing a proposal to include a data structure I consider more generally useful (graph), and that ended up pretty much dead. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: print format
En Tue, 15 Dec 2009 00:28:11 -0300, Dave Angel da...@ieee.org escribió: mattia wrote: Hi all, I wanto to print just the first 5 characters of a string, why this doesn't work (py3.1)? print({0:5}.format(123456789)) 123456789 I know I could use print(123456789[:5]), yeah it's a stupid example, but isn't format for string formatting? The width field of the format is a *minimum* width. So you can use it to force padding, but not truncation. If you need the string truncated, just use slicing, as you already showed. Or use the .precision specifier: py {0:.5}.format(123412341234) '12341' {0:5.5} has the same effect. See http://docs.python.org/library/string.html#format-string-syntax -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
dictionary with tuple keys
Hi all, I am probably not thinking straight anymore about this problem. I have a dictionary with tuple keys in the format (a, b, A, B) and float values. I want to collect all the keys with identical (a, b...), disregarding whatever (... A, B) might be. Specifically, I want to sum the float values once I've collected these keys. I am staring at my screen, pondering ugly things, and I just know I must be missing a Pythonic solution. Any suggestions? Thanks for any help, Brandon -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the differences between tkinter in windows and Tkinter in the other platform?
Thank you for your point. Python versions were difficult. And I didn't know that Tkinter was renamed. To make sure, I specify their systems. The system which `tkinter` module has is python 3.11 in Windows XP SP3. And the other system, which `Tkinter` module has is python 2.51 in Ubuntu 8.04. I've solved the mistake that even I don't believe. Thank you very much! At 14 Dec 2009 13:19:18 -0500 Terry Reedy wrote: On 12/14/2009 10:02 AM, Hidekazu IWAKI wrote: Hi, I'm hidekazu. I'm making a Tk application with python. In the below code, the class App was inherited from Tkinter.Tk and the __init__ method calls Tk's constructor with `super` method. In windows, this code is valid (but, Tkinter - tkinter). Why does this code happen a type error in not windows platform? You question is not clear. Specify system and Python version is both cases. Note that Tkinter in 2.x was renamed tkinter in 3.x. This difference has nothing to do with the platform. tjr thank you. # from Tkinter import * class App(Tk): def __init__(self): super(Tk,self).__init__() App().mainloop() #Traceback (most recent call last): # File ./app.py, line 16, inmodule #App().mainloop() # File ./app.py, line 7, in __init__ #super(Tk,self).__init__() #TypeError: super() argument 1 must be type, not classobj / 岩城 秀和(いわき ひでかず) e-mail: iw...@iwakihidekazu.net / -- http://mail.python.org/mailman/listinfo/python-list ** XREA.COM -Free Web Hosting- http://www.xrea.com/ ** / 岩城 秀和(いわき ひでかず) e-mail: i.hidekazu at gmail.com / -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary with tuple keys
On Mon, Dec 14, 2009 at 9:49 PM, Brandon Devine your.mas...@gmail.com wrote: Hi all, I am probably not thinking straight anymore about this problem. I have a dictionary with tuple keys in the format (a, b, A, B) and float values. I want to collect all the keys with identical (a, b...), disregarding whatever (... A, B) might be. Specifically, I want to sum the float values once I've collected these keys. I am staring at my screen, pondering ugly things, and I just know I must be missing a Pythonic solution. Any suggestions? from collections import defaultdict new_dict = defaultdict(int) for tupkey in your_dict: new_key = tupkey[:2] new_dict[new_key] += your_dict[tupkey] Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python to Execute a C or FORTRAN Program (Windows)
On Dec 14, 8:14�pm, W. eWatson wolftra...@invalid.com wrote: I think Python is capable of executing a compiled C or FORTRAN program, Sure, if it was compiled to an .exe file. and maybe even getting some parameters passed back. Sure, if the program prints to stdout. Does anyone have a example of how this might be done? I'm running under Win XP Pro. Here's one. The test program is factor.exe (included in the MIRACL library). I recompiled it (factor!.exe) to produce consitent output. Instead of stupidly saying C:\factor!\miracl\sourcefactor 31 31 this number is prime! I now have it print C:\factor!\miracl\sourcefactor! 31 PRIME_FACTOR 31 I now call it from Python and have Python fix its mistakes. ##Mistakes? - numbers reported as COMPOSITE may still ##be factorable. Composites are not returned to the ##start of the algorithm for re-factoring. But that's ok, ##if I don't like it, I'm free to fix it myself since ##I have the source code (so says the author). ## ##Hey, I won't look a gift horse in the mouth, but you ##are advised that when you see... ## ##C:\factor!\miracl\sourcefactor! 5081842980034330599 ##30221143303110332712493139579190463526792062 ##62204589342623811236647989889145173098650749 ## ##PRIME_FACTOR 37 ##PRIME_FACTOR 43 ##PRIME_FACTOR 167 ##COMPOSITE_FACTOR 507787751 ##PRIME_FACTOR 69847 ##PRIME_FACTOR 30697 ##PRIME_FACTOR 89017 ##PRIME_FACTOR 3478697 ##PRIME_FACTOR 434593 ##PRIME_FACTOR 49998841 ##PRIME_FACTOR 161610704597143 ##PRIME_FACTOR 14064370273 ##COMPOSITE_FACTOR 963039394703598565337297 ##PRIME_FACTOR 11927295803 ## ##...you should follow that with... ## ##C:\factor!\miracl\sourcefactor! 507787751 ##PRIME_FACTOR 29819 ##PRIME_FACTOR 17029 ## ##C:\factor!\miracl\sourcefactor! 963039394703598565337297 ##PRIME_FACTOR 518069464441 ##PRIME_FACTOR 1858900129817 import os import random import time factor_program = 'factor! -d200 ' the_composites = [['COMPOSITE_FACTOR','50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749']] ##random_digits = [str(random.randint(0,9)) for d in range(30)] ##test_number = ''.join(random_digits) ##the_composites = [['COMPOSITE_FACTOR',test_number]] the_primes = [] the_intractables = [] phase = 1 the_times = [] while the_composites: print =*40 print 'Phase',phase the_comp = the_composites.pop(0) print the_comp print the_times.append(time.time()) # time how long it takes to run factor!.exe the_output = os.popen(factor_program+the_comp[1]).readlines() the_times.append(time.time()) new_factors = [i.split() for i in the_output] for i in new_factors: print i print if len(new_factors) == 1: # it's prime or intractable if new_factors[0][0] == 'PRIME_FACTOR': the_primes.append([new_factors[0][0],long(new_factors[0][1])]) else: the_intractables.append([new_factors[0][0],long(new_factors[0] [1])]) new_factors.pop() while new_factors: j = new_factors.pop(0) if j[0] == 'PRIME_FACTOR': the_primes.append([j[0],long(j[1])]) else: the_composites.append(j) print the_times[phase] - the_times[phase-1],'seconds' phase += 1 print =*40 print print 'Factoring complete' print the_primes.sort() the_intractables.sort() the_primes.extend(the_intractables) for i in the_primes: print i[0],i[1] print print =*40 ## ##Phase 1 ##['COMPOSITE_FACTOR', '50818429800343305993022114330311033271249313957919046352679206262204589342623811236647989889145173098650749'] ## ##['PRIME_FACTOR', '37'] ##['PRIME_FACTOR', '43'] ##['PRIME_FACTOR', '167'] ##['COMPOSITE_FACTOR', '507787751'] ##['PRIME_FACTOR', '69847'] ##['PRIME_FACTOR', '30697'] ##['PRIME_FACTOR', '89017'] ##['PRIME_FACTOR', '3478697'] ##['PRIME_FACTOR', '434593'] ##['PRIME_FACTOR', '49998841'] ##['PRIME_FACTOR', '161610704597143'] ##['PRIME_FACTOR', '14064370273'] ##['COMPOSITE_FACTOR', '963039394703598565337297'] ##['PRIME_FACTOR', '11927295803'] ## ## ##Phase 2 ##['COMPOSITE_FACTOR', '507787751'] ## ##['PRIME_FACTOR', '29819'] ##['PRIME_FACTOR', '17029'] ## ## ##Phase 3 ##['COMPOSITE_FACTOR', '963039394703598565337297'] ## ##['PRIME_FACTOR', '518069464441'] ##['PRIME_FACTOR', '1858900129817'] ## ## ## ##Factoring complete ## ##PRIME_FACTOR 37 ##PRIME_FACTOR 43 ##PRIME_FACTOR 167 ##PRIME_FACTOR 17029 ##PRIME_FACTOR 29819 ##PRIME_FACTOR 30697 ##PRIME_FACTOR 69847 ##PRIME_FACTOR 89017 ##PRIME_FACTOR 434593 ##PRIME_FACTOR 3478697 ##PRIME_FACTOR 49998841 ##PRIME_FACTOR