ZODB 3.3.1 release candidate 1 released
I'm pleased to announce the release of ZODB 3.3.1c1. In the absence of new critical bug reports, the same code will be released as ZODB 3.3.1 final in a week or two. You can download a source tarball or Windows installer from: http://zope.org/Products/ZODB3.3 There are several critical bugfixes and improvements in ZODB 3.3.1c1. See the news file for details: http://zope.org/Products/ZODB3.3/NEWS.html Note that ZODB 3.3.1 does not support any version of Zope 2.6 or 2.7. Zope 2.8, and current Zope 3, development have moved to ZODB 3.4, so ZODB 3.3.1 final is expected to be the last release in the ZODB 3.3 line. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
ZODB 3.4 alpha 1 released
I'm pleased to announce the release of ZODB 3.4 alpha 1. You can download a source tarball or Windows installer from: http://zope.org/Products/ZODB3.4 ZODB 3.4a1 contains all the bugfixes in the ZODB 3.3.1c1 released earlier today, plus new features, such as a new BTree type mapping integers to floats, an end to the limit on the number of open Connections per DB object, and new tool fsoids.py for heavy FileStorage debugging. See the news file for details: http://zope.org/Products/ZODB3.4/NEWS.html Note that ZODB 3.4 does not support any version of Zope 2.6 or 2.7. Zope 2.8a2 (to be released soon), and current Zope 3 development, use ZODB 3.4. The ZODB 3.3 line will be retired with the release of ZODB 3.3.1 final. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
gthumpy: GUI to handle images
GTK-GUI for images from a digital camera. You can enter metadata (date, title, description), switch between directories and display all images of a directory. You can created a slide show of static HTML files, too. The created files don't need a http server or CGI, you can burn a CD/DVD and give this to your friends (which might use windows). You only need a webbrowser to view the created HTML files. Changes since last release: - gtk2 - fullscreen support - keyboard shortcuts - change directory - change description of directory - list all images of a directory http://guettli.sourceforge.net/gthumpy/src/README.html http://guettli.sourceforge.net/gthumpy/download/gthumpy-2005-04-02.tgz Licence: Any Open Source Licence Thomas Güttler -- Thomas Guettler [EMAIL PROTECTED] http://www.thomas-guettler.de -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: Simple thread-safe counter?
Skip Montanaro [EMAIL PROTECTED] writes: How about (untested): import Queue counter = Queue.Queue() counter.put(0) def f(): i = counter.get() counter.put(i+1) return i Hmmm, that's a bit messier than I hoped for, but it looks sure to work. I think for my immediate requirement, I'm going to use xrange as Tim suggested. However, I can't be sure that will work in non-GIL implementations. -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python on a Windows 2000 Server
Mike Moum wrote: Hi, I'm a civil engineer who also doubles as chief programmer for technical applications at my company. Most of our software is written in Visual Basic because our VP in charge of I.T. likes to have consistency, and at the moment we're a Microsoft shop. He has assigned me the task of developing an new application, the exact nature of which is not important for my question. I told him that, in my opinion, that Visual Basic was not the best choice for developing this application, and that I wanted to use Python. After a bit of discussion of the pros and cons, he said to go ahead. I managed to keep my jaw from hitting the floor. :) We have a central server array running Windows Server 2000 (I think that's the right name; networking is not my specialty, but it's definately Windows). Some of our workstations run Windows 2000; others run Windows XP Pro. I would like to install Python on the server, and run the application that I'll be developing from the workstations, without having to install any Python components on the workstations themselves. In other words, the Python executable, and the various libraries, dll's, and what have you, as well as the application that I'm developing, should all reside on the server. The only thing on the workstations would be a shortcut to myapplication.py. Does anyone know whether it is possible to do this? I've done some Google searching, with no conclusive results, and poked about on python.org, but haven't really been able to find anything. Normally I'd be happy to just try it out and see what happens, but we're breaking new ground here (this is an amazingly big step for our hide-bound IS department!), so I'd like everything to go as smoothly as possible. Try Movable Python http://www.voidspace.org.uk/python/movpy/ When you will be creating a shortcut to you application it shouldn't point to yourapplication.py directly but rather at \\yourserver\movpy\movpyw.exe \\yourserver\yourapp\yourapplication.py Serge. -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python on a Windows 2000 Server
Mike Moum wrote: We have a central server array running Windows Server 2000 (I think that's the right name; networking is not my specialty, but it's definately Windows). Some of our workstations run Windows 2000; others run Windows XP Pro. I would like to install Python on the server, and run the application that I'll be developing from the workstations, without having to install any Python components on the workstations themselves. In other words, the Python executable, and the various libraries, dll's, and what have you, as well as the application that I'm developing, should all reside on the server. The only thing on the workstations would be a shortcut to myapplication.py. I would encourage you to try an administrative (or network) installation. On the server, run msiexec /a python-2.4.1.msi. This will unpack the MSI file, and create a new one. You need to make the resulting installation a network share. Then, on each workstation, you install the msi file on the share, and specify that you want to run Python from the network share. This mode of installation is only available since Python 2.4, and very few people are probably using it, but I'd like to hear whether it works (it should, in principle). Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
[Peter Hansen] If py.test provides a driver utility that does effectively this, well, that's nice for users. If it doesn't run them as separate processes, it wouldn't suit me anyway. Still, it sounds like it does have a strong following of smart people: enough to make me want to take a closer look at it to see what the fuss is about. :-) FWIW, py.test scales nicely. Also, it takes less time to try it out or read the docs than discuss it to death on a newsgroup. The learning curve is minimal. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
[Peter Hansen] This is pretty, but I *want* my tests to be contained in separate functions or methods. In py.test, those would read: def test1(): assert a == b def test2(): raises(Error, func, args) Enclosing classes are optional. Raymond -- http://mail.python.org/mailman/listinfo/python-list
How to reload local namespace definitions in the python interpreter?
Hi, I am a beginner using the python interpreter. To reduce typing effort, I created a module called aliases.py containing some aliases for objects I commonly use like - aliases.py : import filecmp, os, commands op = os.path go = commands.getoutput dc = filecmp.dircmp p1 = '/mnt/usbkey/flash/' p2 = '/mnt/fat32/myfiles/flash/' When I start up the interpreter, I can simply type - from aliases import * This works fine, but each time I change any of the definitions in aliases.py, I have to restart the interpreter and type from aliases import * again. Is there any way to reload these definitions without restarting the interpreter? -Slath -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Tim Peters wrote in news:mailman.1223.1112417955.1799.python- [EMAIL PROTECTED] in comp.lang.python: [Paul Rubin] I'd like to have a function (or other callable object) that returns 0, 1, 2, etc. on repeated calls. That is: print f() # prints 0 print f() # prints 1 print f() # prints 2 # etc. There should never be any possibility of any number getting returned twice, or getting skipped over, even if f is being called from multiple threads. What's the simplest and most natural way to do this? I can think of a few but am not sure that they work. And I can think of some ways that are sure to work, but are messier than I'd like. The GIL is your friend here: Yes but IIUC the GIL is only CPython, will this work with Jython, IornPython etc ? (*) import itertools f = itertools.count().next *) I'm either being rhetorical or stupid, not sure which :). Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Artie Gold wrote: Skip Montanaro wrote: counter = Queue.Queue() def f(): i = counter.get() I think you need: i = counter.get(True) The default value for the block argument to Queue.get is True. -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
On Sat, 02 Apr 2005 09:24:30 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: [Peter Hansen] If py.test provides a driver utility that does effectively this, well, that's nice for users. If it doesn't run them as separate processes, it wouldn't suit me anyway. Still, it sounds like it does have a strong following of smart people: enough to make me want to take a closer look at it to see what the fuss is about. :-) FWIW, py.test scales nicely. Also, it takes less time to try it out or read the docs than discuss it to death on a newsgroup. The learning curve is minimal. Is there a package that is accessible without svn? Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for Benchmarklets to improve pyvm
coffeebug wrote: Newbie here (new to the language and scripting in general). I'm trying to figure out what you mean by bytecode. Do you mean a virtual python environment that can be hosted by any anonymous operating system? For example, you want to run Python programs on BEOS so you crank up its version of PYVM and load a Python source code and run it? The pyc files. See module 'compile' and module 'dis'. Stelios -- http://mail.python.org/mailman/listinfo/python-list
Re: How to reload local namespace definitions in the python interpreter?
[EMAIL PROTECTED] wrote: Hi, I am a beginner using the python interpreter. To reduce typing effort, I created a module called aliases.py containing some aliases for objects I commonly use like - aliases.py : import filecmp, os, commands op = os.path go = commands.getoutput dc = filecmp.dircmp p1 = '/mnt/usbkey/flash/' p2 = '/mnt/fat32/myfiles/flash/' When I start up the interpreter, I can simply type - from aliases import * This works fine, but each time I change any of the definitions in aliases.py, I have to restart the interpreter and type from aliases import * again. Is there any way to reload these definitions without restarting the interpreter? -Slath reload(aliases) See: http://www.python.org/doc/2.4.1/lib/built-in-funcs.html By the way, are you aware of the import ... as ... idiom? e.g. import os.path as op -- Website: www DOT jarmania FULLSTOP com -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for Benchmarklets to improve pyvm
Skip Montanaro wrote: Take a look around for Marc Andre Lemburg's pybench suite. Thanks! Although pybench needs module.re and module.pickle, so I'll post results later. Moreover, I have similar tests. I'd prefer scripts that do *real* calculations. Stelios -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Paul Rubin wrote: Skip Montanaro [EMAIL PROTECTED] writes: How about (untested): import Queue counter = Queue.Queue() counter.put(0) def f(): i = counter.get() counter.put(i+1) return i Hmmm, that's a bit messier than I hoped for, but it looks sure to work. I think for my immediate requirement, I'm going to use xrange as Tim suggested. However, I can't be sure that will work in non-GIL implementations. Hello, Simple xrange stuff won't work in Jython as Sython is running on a JVM which doesn't have a GIL kinda thing. If you wanna return a sequential number then you'll need something like : PSEUDO CODE class COUNTER: DOC : THIS WILL RETURN AN INCRMENTING LIST OF NUMBERS - THREAD SAFE. FUNCTION INIT: OBJRLOCK= RLOCK() INTRETURNNUM = 0 FUNCTION NEXT TRY{ RLOCK.AQUIRE }FINALLY{ RLOCK.RELEASE } /PSUEDO CODE That basically all you'll need, if you make it iteratable of whatever - you need to wrap the business end of what you are doing around a recursive lock. Personally I dislike the GIL so I avoid writing code that takes advantages of it. Why psuedo code - this is similar to python code I know but it means I'm not posting untested python code!! Cheers, Neil -- http://mail.python.org/mailman/listinfo/python-list
Re: boring the reader to death (wasRe: Lambda: the Ultimate Design Flaw
Tim Peters wrote: [Aahz] The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death. --GvR [Sunnan] Can anyone please point me to the text that quote was taken from? I tried to use a search engine but I only found quotations, not the source. That's because it was originally in email to a company-internal mailing list. If you're willing to move to Fredericksburg, VA and work for Zope Corp, perhaps they'll let you in to the PythonLabs list archives. Fair warning: I work for Zope Corp, and I'm not sure I can get into those archives. So don't switch jobs _just_ for that. It's just that I'm having a hard time matching that quote to what I though python was about. I thought boring code was considered a virtue in python. (Explicit is better than implicit, sparse is better than dense.) Because what is boring? The opposite of dense, tense, intense. Utterly predictable; it's like the combination of all my prejudices. Even before I knew, I thought Bet Python separates statements from expressions. Sunnan PS. (People easily offended can substitute boring for readable in the above text.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary Operator in Python
Robert Kern wrote: Sunnan wrote: (((0.0 a) 1.0) b ) 2.0 Go on. Try it with a bunch of different values. My bad. (Of course. The subexpressions must return booleans, not the largest number. It couldn't work any other way.) Egg on my face, and all that (figuratively speaking). Not used to infix... -- http://mail.python.org/mailman/listinfo/python-list
Re: pagecrawling websites with Python
On 1 Apr 2005 11:58:11 -0800, writeson [EMAIL PROTECTED] wrote: We've got an application we wrote in Python called pagecrawler that snip / Does anyone have any insight if this is a reasonable approach to build web pages, or if we should look at another design? I don't have an answer to your particular question, but maybe you can have a look at how the HarvestMan works: http://freshmeat.net/projects/harvestman Regards, -- Swaroop C H Blog: http://www.swaroopch.info Book: http://www.byteofpython.info -- http://mail.python.org/mailman/listinfo/python-list
How To Do It Faster?!?
Hello Simo NG, Correct me if I'm wrong but since it _seems_ that the listing doesn't need to be up-to-date each minute/hour as the users will be looking primarily for old/unused files, why not have a daily cronjob on the Unix server to produce an appropriate file list on e.g. the root directory of your file server? You are correct. I don't need this list to be updated every minute/hour. $ find . -type f -printf %T@ %u %s %p\n /yourserverroot/files.txt That is a nice idea. I don't know very much about Unix, but I suppose that on a ksh I can run this command (or a similar one) in order to obtain the list I need. If anyone knows if that command will run also on a simple ksh, could please confirm that? Moreover, I could run this script in a while loop, like: while 1: do if -e [/yourserverroot/filesbackup.txt]; then find . -type f -printf %T@ %u %s %p\n /yourserverroot/files.txt copy /yourserverroot/files.txt /yourserverroot/filesbackup.txt else find . -type f -printf %T@ %u %s %p\n /yourserverroot/filesbackup.txt fi done or something similar (I don't have Unix at hand now, I can not test the commands and, as I said, I don't know Unix very well...). In this way, I always have the filesbackup.txt up-to-date, as a function of the find speed on the server. Then my GUI could scan the filesbackup.txt file and search for a particular user information. Thanks to all the NG for your suggestions! Andrea. -- http://mail.python.org/mailman/listinfo/python-list
Re: boring the reader to death (wasRe: Lambda: the Ultimate DesignFlaw
Sunnan wrote: ...Because what is boring? The opposite of dense, tense, intense. Utterly predictable; it's like the combination of all my prejudices. Even before I knew, I thought Bet Python separates statements from expressions. Python is for terse, pithy prose; Python is not for poetry. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary Operator in Python
Roy Smith wrote: ... How our tools warp our thinking. That is what it means to be human. I can think of no better reason for a programmer to regularly learn languages: our tools warp our thinking. A programmer is a professionally warped thinker. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda: the Ultimate Design Flaw
On Sat, 02 Apr 2005 00:40:15 -0500, Steve Holden [EMAIL PROTECTED] wrote: The danger in GOTO is that it allows the undisciplined programmer to develop a badly-structured solution to a programming problem. A disciplined programmer will write well-structured code with whatever tools come to hand. regards Steve And how that becomes really clear when you want to modify a large program that uses GOTOs librally. Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: Python plug-in Frameworks like Eclipse RCP...
Jim Hargrave wrote: Hum, maybe my question was too specific. What I would really like to know is what is the best way to implement a Python application with a pluggable architecture. In particular, I would like to use wxPython and have plug ins automatically register themselves with the GUI by adding themselves to the mean or adding a tab. Again this is much like Eclipse RCP - but forget that part :-) This is close to beeing trivial in python - just utilize __import__ to dynamically load modules that shall act as plugins. These modules then only have to follow a sort of protocol - lets say you have a module called pluginloader.py that has a register-function. The loaded module could import pluginloader and register itself, or a class defined in itself. Like this -plugin_example.py- import pluginloader class ExamplePlugin(pluginloader.PluginBase): def __init__(self,...): pluginloader.register(ExamplePlugin()) -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Bengt Richter) Is there a package that is accessible without svn? That seems to be its weak point right now. Fortunately, you can get pre-built svn clients for many platforms (http://subversion.tigris.org/project_packages.html#binary-packages), and from there you just have to run a single command (svn get URL). Still, the py.test folks would be doing themselves a favor if they made it available with more common tools. -- http://mail.python.org/mailman/listinfo/python-list
Re: __init__ method and raising exceptions
Or better yet, define your own string/class exception to catch your errors. In my code, things can break in more than a few ways. In each case I catch the exception(s) specific to that piece of code, print a warning message to the user at sys.stdout and raise a new exception to be caught by my wrapper exception clause. -- http://mail.python.org/mailman/listinfo/python-list
Docorator Disected
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a closer look at function def's and call's, which is something I tend to take for granted. I'm not sure this is 100%, or if there are other ways to view it, but it seems to make sense when viewed this way. Is there a way to do this same thing in a more direct way? Like taking values off the function stack directly. How much of it get's optimized out by the compiler? # # Follow the numbers starting with zero. # # (0) Read defined functions into memory def decorator(d_arg): # (7) Get 'Goodbye' off stack def get_function(function): # (8) Get func object off stack def wrapper(f_arg):# (9) Get 'Hello' off stack new_arg = f_arg+'-'+d_arg result = function(new_arg) # (10) Put new_arg on stack # (11) Call func object return result # (14) Return result to wrapper return wrapper# (15) Return result to get_function return get_function# (16) Return result to caller of func @decorator('Goodbye') # (5) Put 'Goodbye' on stack # (6) Do decorator def func(s):# (12) Get new_arg off stack return s# (13) Return s to result # (1) Done Reading definitions print func('Hello') # (2) Put 'Hello' on stack # (3) Put func object on stack # (4) Do @decorator # (17) print 'Hello-Goodbye' # Hello-Goodbye -- http://mail.python.org/mailman/listinfo/python-list
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python
[EVALUATION] - E02 - Support for MinGW Open Source Compiler http://groups-beta.google.com/group/comp.lang.python/msg/f5cd74aa26617f17 - In comparison to the E02 thread, now a more practical one. - Here is a simple evaluation template (first part) which can be applied to the Python language: http://lazaridis.com/case/lang/index.html If you like, please post the most elegant solutions (either to sections or to the whole document). I will collect the results and write them down in a document, which will compare python with other languages. This document can serve as an flash-start (for people which simply like to take a look on python). http://lazaridis.com/case/lang/python.html - You can see the ruby version here (which shows some limitations, which python can hopefully overcome): http://lazaridis.com/case/lang/ruby.html . -- http://lazaridis.com -- http://mail.python.org/mailman/listinfo/python-list
terminating an inactive process
I'm running a PyWin program that executes another program using subprocess.Popen(). Unfortunately, this other program isn't well behaved, and frequently terminates without terminating its process. After this happens enough times, all my memory is tied up, and the machine crashes. Using subprocess.poll(), I can keep my program from hanging, by timing out the process, and starting anew. This still leaves the previous process hogging memory. How do I kill the old process in Windows? Earl -- http://mail.python.org/mailman/listinfo/python-list
Re: Module subprocess: How to communicate more than once?
Edward C. Jones wrote: I have a program named octave (a Matlab clone). It runs in a terminal, types a prompt and waits for the user to type something. If I try # Run octave. oct = subprocess.Popen(octave, stdin=subprocess.PIPE) # Run an octave called startup. oct.communicate(startup) # Change directory inside octave. oct.communicate(cd /home/path/to/my/dir) I get: ValueError: I/O operation on closed file As defined in the docs. You can't use communicate() for what you're trying to do. How do I set up a subprocess so I can send it a command and get the answer, then send it another command and get an answer, etc.? communicate is not the only way of communicating... use the stdin/stdout handles that are provided. You can very likely find examples of usage in the list archives. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
Ron_Adam wrote: # (0) Read defined functions into memory def decorator(d_arg): # (7) Get 'Goodbye' off stack def get_function(function): # (8) Get func object off stack def wrapper(f_arg):# (9) Get 'Hello' off stack new_arg = f_arg+'-'+d_arg result = function(new_arg) # (10) Put new_arg on stack # (11) Call func object return result # (14) Return result to wrapper return wrapper# (15) Return result to get_function return get_function# (16) Return result to caller of func @decorator('Goodbye') # (5) Put 'Goodbye' on stack # (6) Do decorator def func(s):# (12) Get new_arg off stack return s# (13) Return s to result # (1) Done Reading definitions print func('Hello') # (2) Put 'Hello' on stack # (3) Put func object on stack # (4) Do @decorator # (17) print 'Hello-Goodbye' # Hello-Goodbye Is it possible that you mistakenly believe your @decorator() is being executed at the line func('Hello')? Please add a print statement to your code: def decorator(d_arg): def get_function(function): print 'decorator invoked' def wrapper(f_arg): new_arg = f_arg+'-'+d_arg result = function(new_arg) return result return wrapper return get_function When you run the program, you will see that the comment decorator invoked is printed out at the moment when you finish defining: @decorator('Goodbye') def func(s): return s That is, decorator is invoked before you run the line func('Hello'). Decorator feature is a metaprogramming feature. Not a programming feature. By metaprogramming I mean you are taking a function/code object, and try to do something with it (e.g., wrap it around.) By the time you finish defining the function func(s), the decorator get_function() was already invoked and will never be invoked again. It's better to view functions as individual objects. And try to think who holds reference to these objects. If no one holds reference to an object, it will be garbage collected and will be gone. After you define the function func() and before you execute func('Hello'), this is the situation: decorator() --- held by the module get_function() --- temporary object, garbage collected wrapper() --- held by the module, under the name func func() --- held by wrapper(), under the name function 'Goodbye' --- string object, held by the wrapper function object, under the name d_arg Objects can be rebound to different names. In your code you have rebound the original wrapper() and func() function objects to different names. I think the confusing part is that, for function name binding, Python does not use the = operator, but instead relies on the def keyword. Maybe this is something to be considered for Python 3K. Anonymous function or codeblock objects are good to have, when you are doing metaprogramming. -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
Roy Smith wrote: Actually, I believe it does. I'm just starting to play with this, but it looks like you can do: py.test test_sample.py and it'll run a single test file. Well, my driver script can do that too. I just meant I could do test_sample.py and have it run the test any time, if I wanted, and I was mainly trying to show that even the __name__ == '__main__' part of my example was not essential to the use of unittest. Comparing apples to apples, so to speak, since it looks like you don't use an import to get access to py.test. As near as I can tell, other than requiring an import statement, and a class statement, there is zero additional overhead with unittest versus py.test, given the way I want to structure my tests (in functions or methods). Is that true? If so, I stand by my claim that the difference in weight between the two is much much less than some have claimed. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest vs py.test?
Raymond Hettinger wrote: [Peter Hansen] This is pretty, but I *want* my tests to be contained in separate functions or methods. In py.test, those would read: def test1(): assert a == b def test2(): raises(Error, func, args) Enclosing classes are optional. So basically py.test skips the import statement, near as I can tell, at the cost of requiring a utility to be installed in the PATH. Where was all that weight that unittest supposedly has? (I'm not dissing py.test, and intend to check it out. I'm just objecting to claims that unittest somehow is heavy, when those claiming that it is seem to think you have to use TestSuites and TestRunner objects directly... I think they've overlooked the relatively lightweight approach that has worked so well for me for four years...) -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: terminating an inactive process
i use this to open/close netscape as it also doesnt like to close all the time. its a WMI script but easiely edited. check out script-o-matic from ms-downloads , it outputs python code as well as others. ## strComputer = . Set objWMIService = GetObject(winmgmts:\\ strComputer \root\cimv2) Set colItems = objWMIService.ExecQuery(Select * from Win32_Process,,48) For Each objItem in colItems strProcess = Ucase(objItem.Name) If strProcess = NETSCP.EXE Then objItem.Terminate() End If Next '- MyVar = MsgBox (Do you want to start Netscape Mail and News chr(13) chr(13) Any Zombie processes have been exorcised from the machine already. chr(13) chr(13) , 308, Warning!) Set objWMIService = Nothing Set colItems = Nothing If MyVar = 7 Then Wscript.Quit Else sExecStr = C:\Program Files\Netscape\Netscape\Netscp.exe -mail Dim oShell, obj Set oShell = WScript.CreateObject (WSCript.shell) Set obj = oShell.exec(sExecStr) Set oShell = Nothing Set obj = Nothing End If ' MyVar = MsgBox (Click to close Netscape) strComputer = . Set objWMIService = GetObject(winmgmts:\\ strComputer \root\cimv2) Set colItems = objWMIService.ExecQuery(Select * from Win32_Process,,48) For Each objItem in colItems strProcess = Ucase(objItem.Name) If strProcess = NETSCP.EXE Then objItem.Terminate() End If Next Set objWMIService = Nothing Set colItems = Nothing -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Leif K-Brooks wrote: Artie Gold wrote: Skip Montanaro wrote: counter = Queue.Queue() def f(): i = counter.get() I think you need: i = counter.get(True) The default value for the block argument to Queue.get is True. Right. I misparsed the entry in the documentation: If optional args block is true and timeout is None (the default), block if necessary... Thanks, --ag -- Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays -- http://mail.python.org/mailman/listinfo/python-list
Re: boring the reader to death (wasRe: Lambda: the Ultimate DesignFlaw
Quoth Scott David Daniels [EMAIL PROTECTED]: | Sunnan wrote: | ...Because what is boring? The opposite of dense, tense, intense. Utterly | predictable; it's like the combination of all my prejudices. Even before | I knew, I thought Bet Python separates statements from expressions. | | Python is for terse, pithy prose; Python is not for poetry. That's an odd thing to say. Poetry is verbose, florid? Python is Dutch. Donn -- http://mail.python.org/mailman/listinfo/python-list
Re: redundant importr
Peter Hansen wrote: max(01)* wrote: Peter Hansen wrote: Not required except for performance reasons. If the .pyc files don't exist, the .py files are recompiled and the resulting bytecode is simply held in memory and not cached and the next startup will recompile all over again. but the other files *are* compiled, right? Yes, definitely. I did say that. so the initial question remains unanswered: No it doesn't. I thought I was clear, but I can reword it for you: the files are compiled *in-memory* and the results are never written to disk. *if* they are compiled, where are they put, if the corresponding *.py files are on a non-writeable directory? They are not put anywhere. Compilation is a process by which the source is converted to executable bytecodes. This says nothing about where either the source or the compiled result resides. The actual compilation works on a long series of bytes in memory, and the result is just another long series of bytes. Nothing requires that either of these things are even stored in a file. ok, maybe it is an implementation-dependent issue after all. i am working on a debian woody platform with the standard python2.3 package. consider this: [EMAIL PROTECTED]:~/tmp/import-enigma$ ll total 8 -rw-r--r--1 max2 max2 36 2005-04-02 17:44 imported.py -rw-r--r--1 max2 max2 33 2005-04-02 17:44 importer.py [EMAIL PROTECTED]:~/tmp/import-enigma$ cat importer.py import imported imported.fun_1() [EMAIL PROTECTED]:~/tmp/import-enigma$ cat imported.py def fun_1(): print I am fun_1() [EMAIL PROTECTED]:~/tmp/import-enigma$ python importer.py I am fun_1() [EMAIL PROTECTED]:~/tmp/import-enigma$ ll total 12 -rw-r--r--1 max2 max2 36 2005-04-02 17:44 imported.py -rw-r--r--1 max2 max2 307 2005-04-02 17:45 imported.pyc -rw-r--r--1 max2 max2 33 2005-04-02 17:44 importer.py [EMAIL PROTECTED]:~/tmp/import-enigma$ see? bye macs -- http://mail.python.org/mailman/listinfo/python-list
Performance issue
Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. The words.txt here is just a copy of FreeBSD's /usr/share/dict/words Anyway, the code: import string # Need a function to convert a string to a list to be # able to use the sort() function def string2list(s): l = [] for i in range(0, len(s)): l.append(s[i]) return l words = [] found = [] anagram = raw_input(Find anagrams of word: ) f = open('words.txt', 'r') file = f.read() f.close() words = file.splitlines() sorted_anagram = anagram.lower() sorted_anagram = string2list(anagram) sorted_anagram.sort(lambda x, y: cmp(x, y)) while words: if len(words[0]) == len(sorted_anagram): wordlist = string2list(words[0]) wordlist.sort(lambda x, y: cmp(x, y)) sorted_wordlist = wordlist if sorted_anagram == sorted_wordlist: found.append(words[0]) del words[0] print Anagrams of + anagram + : while found: print found[0] + del found[0] -- http://mail.python.org/mailman/listinfo/python-list
instance name
hi. is there a way to define a class method which prints the instance name? e.g.: class class_1: ... def myName(self): ... what should i do here ... instance_1 = class_1() instance_1.myName() 'instance_1' bye macs -- http://mail.python.org/mailman/listinfo/python-list
Re: instance name
max(01)* [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] is there a way to define a class method which prints the instance name? The term the instance name is misleading, because it assumes, without saying so explicitly, that every instance has a unique name. In fact, there is no reason that an instance needs to have a name at all, or that it should have only one. You gave this example: instance_1 = class_1() instance_1.myName() but what if I did this instead? class_1().myName() Or this? instance_1 = class_1() instance_2 = instance_1 instance_2.myName() -- http://mail.python.org/mailman/listinfo/python-list
Re: instance name
max(01)* wrote: hi. is there a way to define a class method which prints the instance name? e.g.: class class_1: ... def myName(self): ... what should i do here ... instance_1 = class_1() instance_1.myName() 'instance_1' bye macs What should the following do, you think? a=class_1() b=a b is a True b.myName() print what There is no such thing as the instance name. (a and b both point to the same instance, in my example) Also: why do you want this? It smells like you're actually looking for the use of a dict to do your job. --Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: instance name
Andrew Koenig wrote: max(01)* [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] is there a way to define a class method which prints the instance name? The term the instance name is misleading, because it assumes, without saying so explicitly, that every instance has a unique name. In fact, there is no reason that an instance needs to have a name at all, or that it should have only one. You gave this example: instance_1 = class_1() instance_1.myName() but what if I did this instead? class_1().myName() Or this? instance_1 = class_1() instance_2 = instance_1 instance_2.myName() excellent points. i'll get back with proper questions afterwards. thanks macs -- http://mail.python.org/mailman/listinfo/python-list
Re: instance name
Irmen de Jong wrote: max(01)* wrote: hi. is there a way to define a class method which prints the instance name? e.g.: class class_1: ... def myName(self): ... what should i do here ... instance_1 = class_1() instance_1.myName() 'instance_1' bye macs What should the following do, you think? a=class_1() b=a b is a True b.myName() print what There is no such thing as the instance name. (a and b both point to the same instance, in my example) Also: why do you want this? It smells like you're actually looking for the use of a dict to do your job. right. it seems i need to dive deeper in the language spirit. thanks a lot bye macs -- http://mail.python.org/mailman/listinfo/python-list
Example Code : Shared Memory with Mutex (pywin32 and ctypes)
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py #~ a) Creates a shared memory #~ b) Creates or Opens a mutex #~ c) Reads the contents (null terminated string) of shared memory #~ d) Acquires a mutex and then writes a null terminated string to the shared memory #~ e) Sleeps upto 2 seconds. Sleep time is generated by a random number #~ f) Repeats steps (c), (d) and (e) indefintely import mmap, random, time import win32event, pywintypes, win32api def main(): SHMEMSIZE = 256 ERROR_ALREADY_EXISTS = 183 szName = MyFileMappingObject szMsg1 = Message from first process using win32all and mmap - szMutex = MyMutexObject shmem = mmap.mmap(0, SHMEMSIZE, szName, mmap.ACCESS_WRITE) try: hMutex = win32event.CreateMutex(None, pywintypes.FALSE, szMutex) if (win32api.GetLastError() == ERROR_ALREADY_EXISTS): printOpened existing mutex object, szMutex else: print Created new mutex i=0 random.seed() while 1: szMsg1 = szMsg1 + hex(i) + \0 if (len(szMsg1) SHMEMSIZE): print Current size of string message is, len(szMsg1), and greater than, SHMEMSIZE break shmem_read = shmem.read(SHMEMSIZE) shmem_read = shmem_read.rstrip(chr(0)) shmem_read = shmem_read.rstrip( ) print RECEIVED from SECOND Process: , shmem_read shmem.seek(0) wait_result = win32event.WaitForSingleObject(hMutex, 1000) if (wait_result == win32event.WAIT_OBJECT_0): shmem.write(szMsg1) while (shmem.tell() != SHMEMSIZE): shmem.write_byte( ) shmem.seek(0) print WROTE in FIRST process: , szMsg1 win32event.ReleaseMutex(hMutex) elif(wait_result == win32event.WAIT_TIMEOUT): print COULD NOT ACQUIRE MUTEX. TIMEOUT OCCURED elif (wait_result == win32event.WAIT_ABONDONED): print WAIT ABANDONED i = i + 1 szMsg1 = Message from first process using win32all and mmap - time.sleep(random.random()*2) except pywintypes.error, (errno, object, strerror): print Error in, object, :, strerror if __name__ == __main__: main() File: SharedMemCreate_Mutex_ctypes.py # This application should be used with SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py #~ a) Creates a shared memory #~ b) Creates or Opens a mutex #~ c) Reads the contents (null terminated string) of shared memory #~ d) Acquires a mutex and then writes a null terminated string to the shared memory #~ e) Sleeps upto 2 seconds. Sleep time is generated by a random number #~ f) Repeats steps (c), (d) and (e) indefintely # There are two options to implement this code - Option A or Option B. If Option B(A) is chosen then # Option A(B) should be commented. import random, time from ctypes import * def main(): try: SHMEMSIZE = 256 TRUE = 1 FALSE = 0 ERROR_ALREADY_EXISTS = 183 FILE_MAP_ALL_ACCESS = 0xF001F WAIT_OBJECT_0 = 0 WAIT_TIMEOUT = 0x102 WAIT_ABANDONED = 0x80 PAGE_READWRITE = 0x04 INVALID_HANDLE_VALUE = 0x szName = MyFileMappingObject szMsg1 = Message from first process using ctypes - szMutex = MyMutexObject hMutex = windll.kernel32.CreateMutexA(None, FALSE, szMutex) if (hMutex == 0): raise WinError() elif (windll.kernel32.GetLastError() == ERROR_ALREADY_EXISTS): printOpened existing mutex object, szMutex else: print Created new mutex hMap = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READWRITE, 0, SHMEMSIZE, szName) if (hMap == 0): print Could not open file mapping object raise WinError() MapViewOfFile = windll.kernel32.MapViewOfFile MapViewOfFile.restype = POINTER(c_char) # Option A pBuf = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0) if (pBuf == 0): raise WinError() i=0 random.seed() while 1: szMsg1 = szMsg1 + hex(i) + \0 pBuf_str = cast(pBuf, c_char_p) # Option A if (len(szMsg1) SHMEMSIZE): print Current size of string message is, len(szMsg1), and greater than, SHMEMSIZE break print RECEIVED from SECOND Process: , pBuf_str.value # Option A #~ print RECEIVED from SECOND Process: , string_at(pBuf) # Option B wait_result = windll.kernel32.WaitForSingleObject(hMutex, 1000) if (wait_result == WAIT_OBJECT_0):
Re: Performance issue
Tom Carrick wrote: Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. I like your attitude, not thinking that it's just Python that is slow :-) Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. String to list: list(irmen) # -- ['i','r','m','e','n'] Sorted list: sorted(irmen) # -- ['e', 'i', 'm', 'n', 'r'] (the latter works in Python 2.4+) The words.txt here is just a copy of FreeBSD's /usr/share/dict/words Anyway, the code: import string # Need a function to convert a string to a list to be # able to use the sort() function def string2list(s): l = [] for i in range(0, len(s)): l.append(s[i]) return l ... see above... just replace string2list(s) with sorted(s) words = [] found = [] anagram = raw_input(Find anagrams of word: ) f = open('words.txt', 'r') file = f.read() f.close() Style: don't use 'file' as a variable name, you're hiding the builtin 'file' function words = file.splitlines() You can obtain this list without reading the file in its entirety, by using the readlines method of file objects: words=open(words.txt).readlines() sorted_anagram = anagram.lower() sorted_anagram = string2list(anagram) sorted_anagram.sort(lambda x, y: cmp(x, y)) The lambda is optional and only slows it down :-) But to get a sorted list of letters, just use sorted(s) if you're on Python 2.4+ while words: if len(words[0]) == len(sorted_anagram): wordlist = string2list(words[0]) wordlist.sort(lambda x, y: cmp(x, y)) sorted_wordlist = wordlist (same here.. replacing this by sorted(words[0]) probably will speed it up rather significantly, partly because it avoids the creation of those temporary lists) if sorted_anagram == sorted_wordlist: found.append(words[0]) del words[0] print Anagrams of + anagram + : while found: print found[0] + del found[0] print .join(found) Cheers --Irmen de Jong -- http://mail.python.org/mailman/listinfo/python-list
Re: boring the reader to death (wasRe: Lambda: the Ultimate Design Flaw
In article [EMAIL PROTECTED], Sunnan [EMAIL PROTECTED] wrote: [Aahz] The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death. --GvR It's just that I'm having a hard time matching that quote to what I though python was about. I thought boring code was considered a virtue in python. (Explicit is better than implicit, sparse is better than dense.) Because what is boring? The opposite of dense, tense, intense. Utterly predictable; it's like the combination of all my prejudices. Even before I knew, I thought Bet Python separates statements from expressions. Note very, VERY, *VERY* carefully that the quote says nothing about boring code. The quote explicitly refers to reams of trivial code as boring -- and that's quite true. Consider this distinction: if foo == 'red': print 'foo is red' elif foo == 'blue': print 'foo is blue' versus print foo is, foo I'm sure you can think of many other examples -- real examples -- if you put your mind to work; Guido's point is about the essential necessity of refactoring and rewriting code for conciseness and clarity. -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death. --GvR -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
In [EMAIL PROTECTED], Tom Carrick wrote: [] Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. Use the `list()` builtin on the string and *just* the `sort()` method:: In [2]: characters = list('hello') In [3]: characters Out[3]: ['h', 'e', 'l', 'l', 'o'] In [4]: characters.sort() In [5]: characters Out[5]: ['e', 'h', 'l', 'l', 'o'] sorted_anagram = anagram.lower() sorted_anagram = string2list(anagram) sorted_anagram.sort(lambda x, y: cmp(x, y)) sorted_anagram = list(anagram.lower()) sorted_anagram.sort() while words: if len(words[0]) == len(sorted_anagram): wordlist = string2list(words[0]) wordlist.sort(lambda x, y: cmp(x, y)) sorted_wordlist = wordlist if sorted_anagram == sorted_wordlist: found.append(words[0]) del words[0] And here's the performance issue. Deleting the first element of a list results in moving all remaining elements one index down. Better iterate over the words in a for loop:: for word in words: # use `word` instead of `word[0]` in the loop body. ... Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
Ron_Adam wrote: def decorator(d_arg): # (7) Get 'Goodbye' off stack def get_function(function): # (8) Get func object off stack def wrapper(f_arg):# (9) Get 'Hello' off stack new_arg = f_arg+'-'+d_arg result = function(new_arg) # (10) Put new_arg on stack # (11) Call func object return result # (14) Return result to wrapper return wrapper# (15) Return result to get_function return get_function# (16) Return result to caller of func @decorator('Goodbye') # (5) Put 'Goodbye' on stack # (6) Do decorator def func(s):# (12) Get new_arg off stack return s# (13) Return s to result There is actually nothing mysterious about decorators. It is nothing more than ordinary function composition, executed when the decorated function is defined. In case of Your definition it, the composition rules are: decorator(Goodbye)(func)(s) = get_function(func)(s) = wrapper(s), where wrapper stores Goodbye in the local d_arg. Or a bit more formally we state the composition principle: Args x Func - Func, where decorator() is a function of Args, that returns a function Func - Func. As Guido had shown recently in his Artima blog, Func need not be an instance of an ordinary function but can be a function-object like his MultiMethod : http://www.artima.com/weblogs/viewpost.jsp?thread=101605 It is also possible to extend this view by chaining decorators. decorator : Args(2) x (Args(1) x Func - Func ) - Func. To understand decorator chains it is very helpfull to accept the functional view instead of arguing in a procedural picture i.e. pushing and popping arguments onto and from the stack. Someone asked once for a solution of the following problem that is similar in character to Guidos multimethod but some more general. def mul(m1,m2): def default(m1,m2): return default,1+m1*m2 def mul_dec(m1,m2): return mul_dec,Decimal(str(m1))*Decimal(str(m2)) def mul_float(m1,m2): return mul_float,m1*m2 return (default,mul_dec,mul_float) The function mul defines the inner functions default, mul_float and mul_dec. What we want is a unified access to this functions by means of mul. Guidos solution would decompose mul in three different versions of mul: @multimethod(int,float) def mul(m1,m2): return m1*m2 @multimethod(float,float) def mul(m1,m2): return m1*m2 @multimethod(Decimal,Decimal) def mul(m1,m2): return m1*m2 but it is hard to tell, what should be done if no argument tuple matches. An attempt like: @multimethod(object,object) def mul(m1,m2): return 1+m1*m2 would be useless, because there is no concrete match of argument types onto (object,object). So I introduced an external switch over argument tuples, using a decorator chain: @case(None,default) @case((float,float),'mul_float') @case((int,float),'mul_float') @case((Decimal,Decimal),'mul_dec') def mul(m1,m2): def default(m1,m2): return default,1+m1*m2 def mul_dec(m1,m2): return mul_dec,Decimal(str(m1))*Decimal(str(m2)) def mul_float(m1,m2): return mul_float,m1*m2 return (default,mul_dec,mul_float) Can You imagine how case works internally? Regards, Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
Tom Carrick [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] | Hi, | | In my attempted learning of python, I've decided to recode an old | anagram solving program I made in C++. The C++ version runs in less | than a second, while the python takes 30 seconds. I'm not willing to | think it's just python being slow, so I was hoping someone could find | a faster way of doing this. Also, I was wondering if there was a more | builtin, or just nicer way of converting a string to a list (or using | the sort function on a list) than making a function for it. | | The words.txt here is just a copy of FreeBSD's /usr/share/dict/words | | Anyway, the code: | | import string You're importing string, but never use it, so you can omit that line. | | # Need a function to convert a string to a list to be | # able to use the sort() function | def string2list(s): |l = [] |for i in range(0, len(s)): |l.append(s[i]) |return l No need to write your own function. list(s) already does the trick. | | words = [] | found = [] | | anagram = raw_input(Find anagrams of word: ) | | f = open('words.txt', 'r') | file = f.read() | f.close() I don't have a copy of words.txt, but here's what I would try (untested): anagram = raw_input(Find anagrams of word: ) sorted_anagram = list(sorted(anagram.lower())) # If you're Python is pre 2.4 ise # sorted_anagram = list(anagram.lower()) # sorted_anagram.sort() #--sort list in place found = [] # assuming words.txt contains a word per line # iterate over the lines of the file for line in open(/path/to/words.txt): word = line[:-1]# Get rid of trailing newline sorted_word = list(sorted(word.lower())) if sorted_word == sorted_anagram: found.append(word) if found: print Anagrams of %s: % anagram for w in found: print w else: print No anagrams for %s % anagram -- Vincent Wehren -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
Tom Carrick [EMAIL PROTECTED] writes: In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. Indeed, your program can be improved to run about ten times as fast, which (on my system, with 96274 entries in /usr/share/dict/words) is below a second. In general you should try to move the loops into C code, i.e. use built-in functions instead of long 'for' blocks. Some comments on your version: import string # Need a function to convert a string to a list to be # able to use the sort() function def string2list(s): [snipped] list() achieves the same thing a lot faster. words = [] You do not need to initialize 'words' here, as you're overwriting it a few lines afterwards. found = [] anagram = raw_input(Find anagrams of word: ) f = open('words.txt', 'r') file = f.read() f.close() words = file.splitlines() Try to avoid assigning to the names of built-in functions if you can. Names like 'file', 'list', 'dict', 'map' etc. are often an obvious choice, but overwriting them means that you don't just know what a later use refers to. sorted_anagram = anagram.lower() sorted_anagram = string2list(anagram) sorted_anagram.sort(lambda x, y: cmp(x, y)) Unless you *really* have to, don't use comparison functions with sort(), as they slow the operation considerably. In this (as in most) cases, a plain sorted_anagram.sort() does the trick, and in version 2.4 you can achieve custom sort orders with the optional 'key' argument. The sorted() built-in also comes in handy here. while words: if len(words[0]) == len(sorted_anagram): wordlist = string2list(words[0]) wordlist.sort(lambda x, y: cmp(x, y)) sorted_wordlist = wordlist if sorted_anagram == sorted_wordlist: found.append(words[0]) del words[0] Avoid this style of looping at all times! Removing the first element of a list is O(n), so looping through the whole list as above is O(n**2). In most cases you should use a for loop: for word in words: # do something which is O(n) of course. If you do have to loop destructively, pop() from the end (which is the default) like so: while words: word = words.pop() # do something This is also O(n), because removing the *last* element of a list is O(1) (amortized; I suppose the implementation will occasionally shrink the underlying array at linear cost). print Anagrams of + anagram + : while found: print found[0] + del found[0] I assume you meant not to print a newline between the words, which 'print' does by default. The best solution in that case is .join(found). A better version (2.4+ only): -- 8 -- 8 -- anagram = raw_input(Find anagrams of word: ) words = open('words.txt', 'r') sorted_anagram = sorted(anagram.lower()) found = [] for word in words.read().splitlines(): if len(word) == len(anagram) and sorted(word) == sorted_anagram: found.append(word) print Anagrams of %s: %s % (anagram, ' '.join(found)) -- 8 -- 8 -- Interestingly, the length comparison makes quite a difference! I removed it at first, thinking it was unnecessary. Here are some timings: * Your original version (for comparison): $ time echo stop | python2.4 anagram_slow.py [...] real0m9.090s user0m8.790s sys 0m0.013s * Your version, but with the O(n**2) loop replaced by an O(n) 'for': $ time echo stop | python2.4 anagram_forloop.py [...] real0m0.221s user0m0.134s sys 0m0.014s * My version but with the length comparison removed: $ time echo stop | python2.4 anagram_no_lencmp.py [...] real0m0.408s user0m0.353s sys 0m0.010s * My version as above: $ time echo stop | python2.4 anagram_fast.py [...] real0m0.144s user0m0.099s sys 0m0.008s Hope that helps :-) - Thomas -- If you want to reply by mail, substitute my first and last name for 'foo' and 'bar', respectively, and remove '.invalid'. -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
On 2 Apr 2005 07:22:39 -0800, El Pitonero [EMAIL PROTECTED] wrote: Is it possible that you mistakenly believe your @decorator() is being executed at the line func('Hello')? Please add a print statement to your code: def decorator(d_arg): def get_function(function): print 'decorator invoked' def wrapper(f_arg): new_arg = f_arg+'-'+d_arg result = function(new_arg) return result return wrapper return get_function Thanks, you are correct. I'll post a revised dissection with print statements documenting the flow in a few minutes. I'm still a bit fuzzy on how the arguments are stored and passed. Regards, Ron_Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary Operator in Python
Scott David Daniels wrote: Roy Smith wrote: ... How our tools warp our thinking. That is what it means to be human. I can think of no better reason for a programmer to regularly learn languages: our tools warp our thinking. A programmer is a professionally warped thinker. --Scott David Daniels [EMAIL PROTECTED] +1 QOTW STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
In article [EMAIL PROTECTED], Skip Montanaro [EMAIL PROTECTED] wrote: Obviously, if you want multiple counters for some reason a little information hiding with a class would help (also untested): import Queue class Counter: def __init__(self, start=0): self.counter = Queue.Queue() self.counter.put(start) def __call__(self): i = self.counter.get() self.counter.put(i+1) return i This is one case where'd recommend using a plan RLock() instead of using Queue -- the RLock() will be more efficient: import threading class Counter: def __init__(self, start=0, increment=1): self.counter = start self.increment = increment self.lock = threading.RLock() def __call__(self): self.lock.acquire() self.counter += self.increment i = self.counter self.lock.release() return i There are several tricks one can use to squeeze further efficiency gains from this, including using Lock() instead of RLock(). -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death. --GvR -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
statements documenting the flow in a few minutes. I'm still a bit fuzzy on how the arguments are stored and passed. The arguments are part of the outer scope of the function returned, and thus they ar kept around. That's standart python,too: def foo(): a = 10 def bar(): return a*a return bar print foo()() No decorator-specific magic here - just references kept to outer frames which form the scope for the inner function. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Name of IDLE on Linux
What is the name of the IDLE program on Linux and where is it installed in a normal Linux distribution ? I have installed all the Python 2.3.5 RPMs on my Fedora 3 system but I have no idea where they are installed or what IDLE is called. I lloked in the Python web pages to try to find a list of the files in the various installations, but was unable to find the information. -- http://mail.python.org/mailman/listinfo/python-list
Can I play too?
Thomas Rast wrote: Tom Carrick [EMAIL PROTECTED] writes: In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. Indeed, your program can be improved to run about ten times as fast, ... great stuff This problem inspired an all anagrams program. Using it I was able to find the largest anagram group in Shakespeare's first folio in about the time you originally found anagrams for an individual word. 7: owers = rowse = sower = sowre = swore = woers = worse def words(source): for line in source: for word in line.split(): yield word def all_anagrams(words): seen = dict() for word in words: word = word.lower() if word not in seen: dorw = ''.join(sorted(word)) try: seen[dorw].append(word) except KeyError: seen[dorw] = [word] if word == dorw: continue seen[word] = () for group in seen.itervalues(): if len(group) 1: yield -len(group), sorted(group) # conveniently sortable def main(sources): for filename in sources: dictionary = open(filename, 'r') print All anagrams from %s: % filename try: for nsize, group in sorted(all_anagrams(words(dictionary))): print '%2s: %s' % (-nsize, ' = '.join(group)) finally: dictionary.close() print if __name__ == '__main__': import sys main(sys.argv[1:] or ['anagrams.py']) -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorater inside a function? Is there a way?
It turns out it's not a how to inflate tires with a hammer request; I've actually written an optional type checking module using decorators. The implementation details are not easy to grok, but the usage is straightforward: from typecheck import * @returns(listOf(int, size=3)) @expects(x=str, y=containerOf(int)) def foo(x,y): return [len(x)] + list(y) foo('1',[2,3]) [1, 2, 3] foo('1',(2,3)) [1, 2, 3] foo(1,[2,3]) Traceback (most recent call last): ... TypeError: str expected (int given) foo('1',[2,'3']) Traceback (most recent call last): ... TypeError: containerint expected ([2, '3'] given) foo('1',[2,3,4]) Traceback (most recent call last): ... TypeError: container of size 3 expected ([1, 2, 3, 4] given) George -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
Tom Carrick wrote: Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. code snipped Others have already given a good commentary and alternate suggestions. Here is some more (and some disagreements): * Know your data structures (and how long different operations take). Like don't do del L[0] unless required. This generally comes from experience (and asking on this list). * list(any_sequence_here) will build a list from the sequence. There are usually easy ways of converting built-in types - the Python docs will help you here. * Try to write code as close to an english description of the problem as possible. For example 'for word in words:' rather than using counters and []. This is usually faster, clearer and IMO an important ingredient of being 'Pythonic'. Anyway here's my rendition of your program: ### anagram = raw_input(Find anagrams of word: ) lanagram = list(anagram) lanagram.sort() sorted_anagram = ''.join(lanagram).lower() f = open('/usr/share/dict/words', 'r') found = [] for word in f: word = word.strip('\n') if len(word)==len(sorted_anagram): sorted_word = list(word) sorted_word.sort() sorted_word = ''.join(sorted_word) if sorted_word == sorted_anagram: found.append(word) print Anagrams of %s: % anagram for word in found: print word ### Hopefully it is fast enough. Shalabh -- http://mail.python.org/mailman/listinfo/python-list
Re: Name of IDLE on Linux
On Sat, 2 Apr 2005, Edward Diener wrote: What is the name of the IDLE program on Linux and where is it installed in a normal Linux distribution ? I have installed all the Python 2.3.5 RPMs on my Fedora 3 system but I have no idea where they are installed or what IDLE is called. I lloked in the Python web pages to try to find a list of the files in the various installations, but was unable to find the information. Try idle (all lower case). On my RH-9 system it is in /usr/local/bin HTH. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
On Sat, 02 Apr 2005 19:59:30 +0200, Diez B. Roggisch [EMAIL PROTECTED] wrote: statements documenting the flow in a few minutes. I'm still a bit fuzzy on how the arguments are stored and passed. The arguments are part of the outer scope of the function returned, and thus they ar kept around. That's standart python,too: def foo(): a = 10 def bar(): return a*a return bar print foo()() No decorator-specific magic here - just references kept to outer frames which form the scope for the inner function. I followed that part. The part that I'm having problems with is the first nested function get's the argument for the function name without a previous reference to the argument name in the outer frames. So, a function call to it is being made with the function name as the argument, and that isn't visable so it looks as if it's magic. Ok, Since I was using the wrong model the first time, probably due to not sleeping well and mixing past language experience in improperly, we will try again. In the below model, the @decorator, (object or the interpreter executing the @decorator statement?), calls nested functions in the function of the same name until it reaches the inner loop which is then attached to the function name. Is this correct now? Cheers, Ron ### Decorator Dissection V.2 ### print \n(0) Start reading decorator defs def decorator(d_arg): print (3) decorator: gets '+d_arg+' def get_function(function): print (6) get_function: gets 'func' object def wrapper(f_arg): print (10) wrapper: gets '+f_arg+' new_arg = f_arg+'-'+d_arg print (11) wrapper: calls func('+new_arg+') result = function(new_arg) print (13) wrapper: returns '+result+' return result print (7) get_function: returns 'wrapper' object return wrapper w = get_function print (4) decorator: return 'get_function' object print '(5) @decorator: calls get_function(func)' # Need to print this here, done at *(5) return w print (1) Done reading decorator defs\n print (2) @decorator: calls decorator('goodbye') # *(5) @decorator: call get_funtion(func) @decorator('Goodbye') def func(s): print '(12) func returns:', s return s print (8) @decorator: func = wrapper\n print (9) Call func('Hello') which is now wrapper object: result = func('Hello') print (14) result gets '+result+'\n print result #---output--- (0) Start reading decorator defs (1) Done reading decorator defs (2) @decorator: calls decorator('Goodbye') (3) decorator: gets 'Goodbye' (4) decorator: return 'get_function' object (5) @decorator: calls get_function(func) (6) get_function: gets 'func' object (7) get_function: returns 'wrapper' object (8) @decorator: func = wrapper (9) Call func('Hello') which is now wrapper object: (10) wrapper: gets 'Hello' (11) wrapper: calls func('Hello-Goodbye') (12) func returns: Hello-Goodbye (13) wrapper: returns 'Hello-Goodbye' (14) result gets 'Hello-Goodbye' Hello-Goodbye -- http://mail.python.org/mailman/listinfo/python-list
Re: boring the reader to death (wasRe: Lambda: the Ultimate DesignFlaw
Donn Cave wrote: Quoth Scott David Daniels [EMAIL PROTECTED]: | Sunnan wrote: | ...Because what is boring? The opposite of dense, tense, intense. Utterly | predictable; it's like the combination of all my prejudices. Even before | I knew, I thought Bet Python separates statements from expressions. | | Python is for terse, pithy prose; Python is not for poetry. That's an odd thing to say. Poetry is verbose, florid? No, poetry is to be read slowly and carefully, appreciating the nuance at every point. You should be able to read past python, while poetry is at least as much about the form of the expression as it is about what is being expressed. Python is Dutch. Donn -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
Tom Carrick wrote: Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. code snipped Others have already given a good commentary and alternate suggestions. Here is some more (and some disagreements): * Know your data structures (and how long different operations take). Like don't do del L[0] unless required. This generally comes from experience (and asking on this list). * list(any_sequence_here) will build a list from the sequence. There are usually easy ways of converting built-in types - the Python docs will help you here. * Try to write code as close to an english description of the problem as possible. For example 'for word in words:' rather than using counters and []. This is usually faster, clearer and IMO an important ingredient of being 'Pythonic'. Anyway here's my rendition of your program: ### anagram = raw_input(Find anagrams of word: ) lanagram = list(anagram) lanagram.sort() sorted_anagram = ''.join(lanagram).lower() f = open('/usr/share/dict/words', 'r') found = [] for word in f: word = word.strip('\n') if len(word)==len(sorted_anagram): sorted_word = list(word) sorted_word.sort() sorted_word = ''.join(sorted_word) if sorted_word == sorted_anagram: found.append(word) print Anagrams of %s: % anagram for word in found: print word ### Hopefully it is fast enough. Shalabh -- http://mail.python.org/mailman/listinfo/python-list
Re: Can I play too?
Scott David Daniels wrote: if __name__ == '__main__': import sys main(sys.argv[1:] or ['anagrams.py']) This is *exactly* the kind of testcases I'm looking for to test the soon-to-be-released pyvm. Great! I'll be back with results. For now, a fast anagrams.py is -- import sys WORDS = [ i.rstrip () for i in file ('/usr/share/dict/words') ] def findana (anagram): sorted_anagram = sorted(anagram.lower()) len_anagram = len (anagram) found = [ word for word in WORDS if len(word)==len_anagram and sorted(word)==sorted_anagram ] print Anagrams of %s: %s % (anagram, ' '.join(found)) for i in sys.argv [1:]: findana (i) - And timings time python anagram.pyc stop step words lots pool eat fast slow lamp cold door xyzzy Anagrams of stop: opts post pots spot stop tops Anagrams of step: pest pets sept step Anagrams of words: sword words Anagrams of lots: lost lots slot Anagrams of pool: loop polo pool Anagrams of eat: ate eat tea Anagrams of fast: fast fats Anagrams of slow: lows owls slow Anagrams of lamp: lamp palm Anagrams of cold: clod cold Anagrams of door: door odor Anagrams of xyzzy: real0m1.491s user0m1.390s sys 0m0.040s time pyvm anagram.pyc stop step words lots pool eat fast slow lamp cold door xyzzy Anagrams of stop: opts post pots spot stop tops Anagrams of step: pest pets sept step Anagrams of words: sword words Anagrams of lots: lost lots slot Anagrams of pool: loop polo pool Anagrams of eat: ate eat tea Anagrams of fast: fast fats Anagrams of slow: lows owls slow Anagrams of lamp: lamp palm Anagrams of cold: clod cold Anagrams of door: door odor Anagrams of xyzzy: real0m0.923s user0m0.760s sys 0m0.070s --- Stelios -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
I followed that part. The part that I'm having problems with is the first nested function get's the argument for the function name without a previous reference to the argument name in the outer frames. So, a function call to it is being made with the function name as the argument, and that isn't visable so it looks as if it's magic. No, its not - but I stepped into that trap before - and thought its magic :) The trick is to know that - a decorator is a callable - get passed a callable - has to return a callable So this is the simplest decorator imaginable is: def identity(f): return f And the decorator _syntax_ is just a python expression that has to be _evaluated_ to a yield decorator. So @identity def foo(self): pass the @identity is just the expression evaluated - to the function reference to identity, which is callable and follows the decorator protocol - and the _result_ of that evaluation is called with the callable in question. So if you want to have _parametrized_ decorators, that expression is _evaluated_ and has to yield a decorator. Like this: def arg_decorator(arg): def real_decorator(f): return f return real_decorator So, this works @arg_decorator('fooobar') def foo(self): pass @arg_decorator('fooobar') is evaluated to real_decorator (which a scope containing arg), and _that_ gets called with foo. HTH - bit me the first time too :) -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: Pseudocode in the wikipedia
In article [EMAIL PROTECTED], Ivan Van Laningham [EMAIL PROTECTED] wrote: . . . 25 25 (_ | _) 25 There's clearly some interesting biometrics research to be done here, although there is a well-known ass-capturing attack based on readily commercially available machines from Xerox that might make it hard to make an ass-based identity system resistant to attacks. http://www.jacquelinestallone.com/rumps.html Metta, Ivan PS: I don't think this is an 0401 page; it's been there a while. . . . Welcome back, Ivan. Your follow-ups make one wonder about the span of related topics clp has been missing in your absence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Controling the ALU
Cesar == Cesar Andres Roldan Garcia [EMAIL PROTECTED] writes: Cesar Hi How can I control an ALU from a PC using Python? Cesar Thanks! Cesar Hola... Cesar Como puedo controlar la ALU de un PC usando Pyhton? Cesar Gracias! Cesar -- Atentamente, Cesar Cesar Andres Roldan Garcia Presidente Comunidad Académica Cesar Microsoft Javeriana Teléfono: 300 8169857 Cali - Colombia For a loose value of 'using', you could do your numerically-intensive work in C++, inline all the assembler necessary, and then fashion a python module thereof using boost::python. I wouldn't pretend to be capable of doing that at this very moment, but if I had such a requirement, this would be my initial course. Best, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How To Do It Faster?!?
[EMAIL PROTECTED] writes: $ find . -type f -printf %T@ %u %s %p\n /yourserverroot/files.txt That is a nice idea. I don't know very much about Unix, but I suppose that on a ksh I can run this command (or a similar one) in order to obtain the list I need. If anyone knows if that command will run also on a simple ksh, could please confirm that? That depends on the unix flavor you're using -- my example was for GNU utilities which are heavily used on (probably all) Linux systems. BSDs, Solaris and other unixen have slightly different 'find' syntax. Use man find to find out more about how 'find' works on your system. On all systems I know, it goes like: find directory [-switches ...] where the switches vary depending on the system. In my example, I used -type f (f as in file) to only list files (otherwise 'find' will include directories too in the output) and -printf to include desired data -- in this case, owner, last modified time, size, path -- in the output (otherwise 'find' will only print the path). You should at least go through the -printf formatting codes to see what information you're able to include in the output (= man find). I used %T@ to print the last modified time in Unix time because it's as simple as it can be: an integer, counting the number of seconds since Jan 1 1970. Python's time module groks Unix time just like that. Moreover, I could run this script in a while loop, like: Except that, I'd imagine, constantly traversing the filesystem will seriously degrade the performance of the file server. You want to run your script periodically over a day, maybe at times when the server is inactive. Or hourly between 8am-4pm and then once at night. In Unix, there's a facility called cron to do just that, it runs scripts and commands over and over again hourly, daily, weekly, or just whenever you want it. Consult your unix flavor's manual or newsgroup on that. copy /yourserverroot/files.txt /yourserverroot/filesbackup.txt always have the filesbackup.txt up-to-date, as a function of the find speed on the server. Yes, creating a temporary file is a good approach. I'd suggest moving the new list over the old one (mv tmpfile filelist.txt) instead of copying, since usually move is merely a rename operation on the filesystem and doesn't involve actually copying of any data. br, S -- [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
On 2 Apr 2005 08:39:35 -0800, Kay Schluehr [EMAIL PROTECTED] wrote: There is actually nothing mysterious about decorators. I've heard this quite a few times now, but *is* quite mysterious if you are not already familiar with how they work. Or instead of mysterious, you could say complex, as they can be used in quite complex ways. What is missing most with them is some really good documentation. I got the basic idea and syntax of decorators down right away, but ran into problems implementing them because, the structure of the functions being used for the decorators wasn't clear. It is nothing more than ordinary function composition, executed when the decorated function is defined. In case of Your definition it, the composition rules are: decorator(Goodbye)(func)(s) = get_function(func)(s) = wrapper(s), where wrapper stores Goodbye in the local d_arg. It worked as a model, but I mixed in concepts from cstacks and function calls, which apparently isn't correct. I posted another model, it should be a bit closer. (with the Subject line spelled correctly, continue this thread there. ;) Or a bit more formally we state the composition principle: Args x Func - Func, where decorator() is a function of Args, that returns a function Func - Func. As Guido had shown recently in his Artima blog, Func need not be an instance of an ordinary function but can be a function-object like his MultiMethod : http://www.artima.com/weblogs/viewpost.jsp?thread=101605 I read this, this morning it was very interesting. It is also possible to extend this view by chaining decorators. decorator : Args(2) x (Args(1) x Func - Func ) - Func. To understand decorator chains it is very helpfull to accept the functional view instead of arguing in a procedural picture i.e. pushing and popping arguments onto and from the stack. Understanding chains is next on my list. :) Someone asked once for a solution of the following problem that is similar in character to Guidos multimethod but some more general. def mul(m1,m2): def default(m1,m2): return default,1+m1*m2 def mul_dec(m1,m2): return mul_dec,Decimal(str(m1))*Decimal(str(m2)) def mul_float(m1,m2): return mul_float,m1*m2 return (default,mul_dec,mul_float) The function mul defines the inner functions default, mul_float and mul_dec. What we want is a unified access to this functions by means of mul. Guidos solution would decompose mul in three different versions of mul: This is similar to c++'s polymorphism which I've played with nearly 10 years ago. I generally found it useful only in small doses even then. I seem to think now that c++'s version of it was implemented at compile time, with each function call being matched up with the correct function, by the argument types. Where as Guido's version, is dynamic and handles the situation at run time. I may not be correct in this, it's been a while. @multimethod(int,float) def mul(m1,m2): return m1*m2 @multimethod(float,float) def mul(m1,m2): return m1*m2 @multimethod(Decimal,Decimal) def mul(m1,m2): return m1*m2 but it is hard to tell, what should be done if no argument tuple matches. It could then invoke the adapt() function to determine if a possible single way to continue is available. But with that you could run into some very subtle bugs. Or just annoying windows like behavior, such as a word processor auto correcting a word when don't want it to. An attempt like: @multimethod(object,object) def mul(m1,m2): return 1+m1*m2 would be useless, because there is no concrete match of argument types onto (object,object). So I introduced an external switch over argument tuples, using a decorator chain: @case(None,default) @case((float,float),'mul_float') @case((int,float),'mul_float') @case((Decimal,Decimal),'mul_dec') def mul(m1,m2): def default(m1,m2): return default,1+m1*m2 def mul_dec(m1,m2): return mul_dec,Decimal(str(m1))*Decimal(str(m2)) def mul_float(m1,m2): return mul_float,m1*m2 return (default,mul_dec,mul_float) Can You imagine how case works internally? Regards, Kay Sure, That should be fairly straight forward. Although I can imagine several ways of implementing it at the moment. I think after I play with decorator chains, one way will probably stand out as being cleaner than the others. Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: Suggesting methods with similar names
Is that last idea so stupid? Still, I'd like to know if you know some little Python search engines for such purpose. Thank you, Bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Pseudocode in the wikipedia
Hi All-- Cameron Laird wrote: Welcome back, Ivan. Your follow-ups make one wonder about the span of related topics clp has been missing in your absence. Thanks for the welcome. Absence was more a consequence of working for idiots for four years (at 60-80 hours/week) than anything else. Now that I'm not working I've got time to catch up on what's new in Python. Nice to see a few familiar faces, too. Metta, Ivan -- Ivan Van Laningham God N Locomotive Works http://www.andi-holmes.com/ http://www.foretec.com/python/workshops/1998-11/proceedings.html Army Signal Corps: Cu Chi, Class of '70 Author: Teach Yourself Python in 24 Hours -- http://mail.python.org/mailman/listinfo/python-list
Re: Name of IDLE on Linux
Jim Benson wrote: On Sat, 2 Apr 2005, Edward Diener wrote: What is the name of the IDLE program on Linux and where is it installed in a normal Linux distribution ? I have installed all the Python 2.3.5 RPMs on my Fedora 3 system but I have no idea where they are installed or what IDLE is called. I lloked in the Python web pages to try to find a list of the files in the various installations, but was unable to find the information. Try idle (all lower case). On my RH-9 system it is in /usr/local/bin Thanks. The system came with Python and the development libraries but not with the latest installation and tools. Once I was able to install all the latest Python RPM for Fedora 3, 'idle' showed up. Now all I have to do is find everything else, including the latest documentation files. It is a pity the Python Linux binary installations do not create folders on the desktop or in the Gnome menu system with links to the Python to the documentation and a readme telling me what executables were installed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
[EMAIL PROTECTED] (Aahz) writes: This is one case where'd recommend using a plan RLock() instead of using Queue -- the RLock() will be more efficient... I'm starting to believe the GIL covers up an awful lot of sloppiness in Python. I wonder if there could be a decorator approach: @synchronized def counter(): t = itertools.count() while True: yield t.next() or better yet, counter = synchronized(itertools.count()) -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
Ron_Adam wrote: On 2 Apr 2005 08:39:35 -0800, Kay Schluehr [EMAIL PROTECTED] wrote: There is actually nothing mysterious about decorators. I've heard this quite a few times now, but *is* quite mysterious if you are not already familiar with how they work. Or instead of mysterious, you could say complex, as they can be used in quite complex ways. If the syntax were like: decorator = function(d_arg) { return function(f) { return function(f_arg) { new_arg = f_arg+'-'+d_arg; return f(new_arg); } } } func = decorator('Goodbye') function(s) { return s; } Would you think it would be more easily understandable? Here, function() is a metafunction (or function factory) whose role is to manufacture a function given a parameter spec and a code body. And in the expression func = decorator('Goodbye')(function(s){return s;}) one pair of outter parenthesis have been omitted. Sure, it's not as readable as Python's def, but with today's syntax highlighters, the special word function can be highlighted easily. If the decorator does not have parameters, one has: func = decorator function(s) { } or in the general case: func = deco1 deco2 deco3 function(s) { } -- http://mail.python.org/mailman/listinfo/python-list
testing -- what to do for testing code with behaviour dependant upon which files exist?
Hi all, I'm just starting to employ unit testing (I'm using doctest), and I am uncertain how to handle writing tests where the behaviour being tested is dependant on whether certain file paths point to actual files. I have a class which takes, in its __init__, a list of file paths to process. The class has a method to validate that the paths passed in are appropriate ones for the class. One portion of the validation code ensures that the passed in file paths actually exist. The relevant part of the validation method code looks like: # self.universe_files is a list of file paths non_existent_files = [ x for x in self.universe_files if not os.path.isfile(x) ] if non_existent_files: raise Files_dont_existError, non_existent_files where Files_dont_existError is a custom exception with an informative error message nicely printing a listing of all the file paths which didn't point to extant files. I can test the custom error class just fine, but I don't see how to test the validation method itself. My problem is that I want to test how it behaves both when it is sent only existing file paths, and when it is sent at least 1 non-existent one. But it seems like that in order to test that, I have to know in advance details of what files exist on the system running the tests. And, it won't do to pick file paths with the right properties with respect to my computer, as I am writing this code to share with someone else, and so I'd have to know details of his file system, too. (If it matters, I am using Python 2.4.1 on windows, and I am sending the code to someone running Python 2.2 on Linux.) So, how does one handle such cases with tests? Thanks for any suggestions. Best, Brian vdB -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
On Sat, 02 Apr 2005 21:04:57 +0200, Diez B. Roggisch [EMAIL PROTECTED] wrote: I followed that part. The part that I'm having problems with is the first nested function get's the argument for the function name without a previous reference to the argument name in the outer frames. So, a function call to it is being made with the function name as the argument, and that isn't visable so it looks as if it's magic. No, its not - but I stepped into that trap before - and thought its magic :) It's magic until we understand it. ;) I get the feeling that those who have gotten to know decorators find them easy, and those who haven't, find them nearly impossible to understand. Which means there is a fairly large first few steps to get over, then it gets easy. There *is* some underlying processes being made, which is also the reason that makes them attractive. Less type/declare/organize/etc... but that is also what causes the difficulty in understanding and using them at first. The trick is to know that - a decorator is a callable - get passed a callable - has to return a callable So this is the simplest decorator imaginable is: def identity(f): return f And the decorator _syntax_ is just a python expression that has to be _evaluated_ to a yield decorator. So @identity def foo(self): pass This much I understand. the @identity is just the expression evaluated - to the function reference to identity, which is callable and follows the decorator protocol - and the _result_ of that evaluation is called with the callable in question. This tells me what it is, and what it does, but not how it works. How is the ***expression evaluated***, what is the ***decorator protocol***. Those are the parts I'm trying to understand at this point. I know this is the equivalent of looking behind the curtains to reveal the little man who is the wizard. But I can't resist. :) So if you want to have _parametrized_ decorators, that expression is _evaluated_ and has to yield a decorator. Like this: There's that word again... **evaluated**. How? def arg_decorator(arg): def real_decorator(f): return f return real_decorator So, this works @arg_decorator('fooobar') def foo(self): pass @arg_decorator('fooobar') is evaluated to real_decorator (which a scope containing arg), and _that_ gets called with foo. So if I'm following you right? When the interpreter gets to the line @arge_decorator('fooobar') it does the following? foo = arg_decorator('fooobar')(foo)() #? (experiment with idle a bit...) Ok I got it. :) I wasn't aware that the form: result = function(args)(args) Was a legal python statement. So python has a built in mechanism for passing multiple argument sets to nested defined functions! (click) Which means this is a decorator without the decorator syntax. def arg_decorator(arg1): def real_decorator(function): def wrapper(arg2) return f(arg2) return real_decorator def foo(arg2): pass foo = arg_decorator('fooobar')(foo)(2arg) The apparent magic is the silent passing of the second two arguments. So this isn't a decorator question any more. Each argument gets passed to the next inner defined function, via... a stack(?) ;) Somehow I think I've completed a circle. LOL Cheers, Ron HTH - bit me the first time too :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
On Sat, 02 Apr 2005 18:39:41 GMT, Ron_Adam [EMAIL PROTECTED] wrote: def foo(): a = 10 def bar(): return a*a return bar print foo()() --- *Here* No decorator-specific magic here - just references kept to outer frames which form the scope for the inner function. Thanks Kay, I wasn't aware of pythons ability to pass arguments to nested functions in this way. I missed it the first time. Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
In [EMAIL PROTECTED], Tom Carrick wrote: In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. The words.txt here is just a copy of FreeBSD's /usr/share/dict/words Here's my attempt which builds an anagram dictionary (sorted word - list of anagrams) for fast lookup of anagrams:: #!/usr/bin/env python2.4 from itertools import imap, ifilter WORDS = '/usr/share/dict/words' def make_anagram_map(words): anagram_map = dict() for word in imap(lambda w: w.strip().lower(), words): sorted_word = ''.join(sorted(list(word))) anagram_map.setdefault(sorted_word, list()).append(word) return dict(ifilter(lambda x: len(x[1]) 1, anagram_map.iteritems())) def main(): words_file = open(WORDS, 'r') anagram_map = make_anagram_map(words_file) words_file.close() while True: word = raw_input('Find anagrams of word (just enter to end): ') if not word: break try: print anagram_map[''.join(sorted(list(word.strip().lower(] except KeyError: print 'No anagrams found for %r' % word # # Print all anagrams sorted by number of anagrams. # print '\n'.join(map(str, sorted(anagram_map.values(), key=len))) # print len(anagram_map) if __name__ == '__main__': main() Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Name of IDLE on Linux
Edward Diener [EMAIL PROTECTED] writes: It is a pity the Python Linux binary installations do not create folders on the desktop or in the Gnome menu system with links to the Python to the documentation and a readme telling me what executables were installed. Imagine they did, and the other 1392 packages (on my system) too :-) Documentation usually resides in /usr/share/doc/package, /usr/share/doc/packages/package or similiar. You're on an RPM-based system, so $ rpm -ql package lists all files belonging to that package. You should have little trouble spotting the documentation files there. - Thomas -- If you want to reply by mail, substitute my first and last name for 'foo' and 'bar', respectively, and remove '.invalid'. -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
On Sat, 02 Apr 2005 14:29:08 GMT, Ron_Adam [EMAIL PROTECTED] wrote: I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a closer look at function def's and call's, which is something I tend to take for granted. I think it might help you to start out with very plain decorators rather than decorators as factory functions that return decorator functions that wrap the decorated function in a wrapper function. E.g., (this could obviously be parameterized as a single decorator factory, but I wanted to show the simplest level of decorator functionality) def decoa(f): ... f.decstr = getattr(f, 'decstr', '') + 'a' ... return f ... def decob(f): ... f.decstr = getattr(f, 'decstr', '') + 'b' ... return f ... def decoc(f): ... f.decstr = getattr(f, 'decstr', '') + 'c' ... return f ... @decoa ... @decoc ... @decob ... @decob ... def foo(): pass ... foo.decstr 'bbca' I.e., @decoa @decoc def foo(): pass is almost[1] exactly equal to (note calling order c then a) def foo(): pass foo = decoc(foo) foo = decoa(foo) [1] One difference is that foo = deco(foo) is a RE-binding of foo, and the binding wouldn't happen at all if the @deco version raised an exception in deco. E.g., def deco(f): raise NotImplementedError ... foo not yet defined: foo Traceback (most recent call last): File stdin, line 1, in ? NameError: name 'foo' is not defined Try the bad decorator: @deco ... def foo(): pass ... Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 1, in deco NotImplementedError No go, and foo still undefined: foo Traceback (most recent call last): File stdin, line 1, in ? NameError: name 'foo' is not defined But the other way around: bar undefined to start: bar Traceback (most recent call last): File stdin, line 1, in ? NameError: name 'bar' is not defined Define it sucessfully: def bar():pass ... bar function bar at 0x02EE8B54 Try to decorate the old-fashioned way: bar = deco(bar) Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 1, in deco NotImplementedError bar function bar at 0x02EE8B54 Still there, defined as before (well, strictly speaking, not necessarily as before: with bar already defined, deco could have messed with the existing bar and THEN raised the exception). Which would also happen with @deco, just that the new binding to bar wouln't happen. def decobomb(f): ... f.bomb = 'bombed' ... raise Exception, 'Did it bomb the existing function?' ... def foo(): pass ... vars(foo).keys() [] @decobomb ... def foo(): pass ... Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 3, in decobomb Exception: Did it bomb the existing function? vars(foo).keys() [] Nope, seems that was a new foo that got bombed and then not bound to replace the old foo. foo.bomb Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'function' object has no attribute 'bomb' I'm not sure this is 100%, or if there are other ways to view it, but it seems to make sense when viewed this way. I like annotated code walk-throughs. But as others have pointed out, it's still a bit buggy ;-) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: testing -- what to do for testing code with behaviour dependant upon which files exist?
* Brian van den Broek wrote: The relevant part of the validation method code looks like: # self.universe_files is a list of file paths non_existent_files = [ x for x in self.universe_files if not os.path.isfile(x) ] if non_existent_files: raise Files_dont_existError, non_existent_files I can test the custom error class just fine, but I don't see how to test the validation method itself. The logic is simple -- you don't want to test os.path.isfile, so mock it. Just encapsulate the os.path.isfile call in an own method, which can be overridden by your test. nd -- # Andr Malo, http://pub.perlig.de/ # -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
On Sat, 02 Apr 2005 10:29:19 -0800, Shalabh Chaturvedi [EMAIL PROTECTED] wrote: Tom Carrick wrote: Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it. code snipped Others have already given a good commentary and alternate suggestions. Here is some more (and some disagreements): * Know your data structures (and how long different operations take). Like don't do del L[0] unless required. This generally comes from experience (and asking on this list). * list(any_sequence_here) will build a list from the sequence. There are usually easy ways of converting built-in types - the Python docs will help you here. * Try to write code as close to an english description of the problem as possible. For example 'for word in words:' rather than using counters and []. This is usually faster, clearer and IMO an important ingredient of being 'Pythonic'. Anyway here's my rendition of your program: ### anagram = raw_input(Find anagrams of word: ) lanagram = list(anagram) lanagram.sort() sorted_anagram = ''.join(lanagram).lower() f = open('/usr/share/dict/words', 'r') found = [] for word in f: word = word.strip('\n') if len(word)==len(sorted_anagram): sorted_word = list(word) sorted_word.sort() sorted_word = ''.join(sorted_word) if sorted_word == sorted_anagram: found.append(word) print Anagrams of %s: % anagram for word in found: print word ### Hopefully it is fast enough. If doing more than one anagram, I think making a set of anagram dictionaries (one for each word length) and pickling them in separate files for later re-use might help. E.g., (untested!!) to make a dict (by wordlength) of anagram dicts, something like d = {} for word in open('/usr/share/dict/words'): word = word.strip().lower() d.setdefault(len(word), {}).setdefault(''.join(sorted(word)), []).append(word))) Then for wlen, adic in d.items(): pickle_file_name = 'anagram_%s'% wlen # pickle adic and write it out to the file ... Then the anagram utility can look for the appropriate pickle file per word length, (i.e., 'anagram_%s'%len(word.strip())) and just load it to anadict and print anadict(''.join(sorted(word.strip().lower())). That's a sketch. Untested!! Gotta go ;-/ Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: Performance issue
Marc 'BlackJack' Rintsch wrote: def make_anagram_map(words): anagram_map = dict() for word in imap(lambda w: w.strip().lower(), words): sorted_word = ''.join(sorted(list(word))) anagram_map.setdefault(sorted_word, list()).append(word) return dict(ifilter(lambda x: len(x[1]) 1, anagram_map.iteritems())) Or if you're afraid of map and filter like me, you can try: def make_anagram_map(words): anagram_map = {} for word in (w.strip().lower() for w in words): anagram_map.setdefault(''.join(sorted(word)), []).append(word) return dict(sortedword_wordlist for sortedword_wordlist in anagram_map.iteritems() if len(sortedword_wordlist[1]) 1) py make_anagram_map(['owers', 'pest', 'rowse', 'pets', 'sower', 'step']) {'epst': ['pest', 'pets', 'step'], 'eorsw': ['owers', 'rowse', 'sower']} STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Dissection
On Sat, 02 Apr 2005 21:04:57 +0200, Diez B. Roggisch [EMAIL PROTECTED] wrote: I followed that part. The part that I'm having problems with is the first nested function get's the argument for the function name without a previous reference to the argument name in the outer frames. So, a function call to it is being made with the function name as the argument, and that isn't visable so it looks as if it's magic. No, its not - but I stepped into that trap before - and thought its magic :) The trick is to know that - a decorator is a callable Strictly speaking, UIAM deco in @deco is a non-general very-limited-syntax expression that should evaluate to a callable. - get passed a callable - has to return a callable That last is a should ;-) Actually, you can abuse the def just to provide a binding name whose associated function is otherwise ignored (other than syntax check and compilation ), e.g., def maverick(f): return 'maverick' ... @maverick ... def foo(): pass ... foo 'maverick' Or @str ... def foo(): pass ... foo 'function foo at 0x02EE8D14' Note the quotes ;-) type(foo) type 'str' Just being picky about absolute statements ;-) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: redundant imports
max(01)* [EMAIL PROTECTED] writes: Peter Hansen wrote: max(01)* wrote: hi everybody. suppose that code-1.py imports code-2.py and code-3.py (because it uses names from both), and that code-2.py imports code-3.py. if python were c, code-1.c should only *include* code-2.c, because the latter in turns includes code-3.c. inclusion of modules in c is a purely preprocessing textual matter (compilation is deferred to after the fact), i guess, so that such things are possible. import of modules in python is a different beast, so the redundancy is (i think) necessary. any comment/suggestion/idea? You're mixed up about this whole idea. that's why i am asking for advice here. my concern was motivated by a (clumsy) attempt to understand the difference of mechanism between the approach to modular programming in a more traditional language (c) and python. [Names changed to be valid python module names.] I feel I ought to point out that you don't really *have* to import the code_3.py in code_1.py. You can get to things code_3.py in code_1.py as code_2.code_3.thing. The semantic behavior of include in C is the same as from module import * in python. Both cases add all the names in the included namespace directly to the including namespace. This usage is depreciated in Python, because it leads to problems figuring out where a specific variable came from. In C, it creates a problem called name space pollution. This is the case when a file1.c gets all the symbols for some_header.h, even though it doesn't include/need those symbols, because some header file1.c included needed a symbol from some_header.h. This is especially galling if the pollution collides with some_header2.h that file1.c actually needs. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
[Paul Rubin] I'm starting to believe the GIL covers up an awful lot of sloppiness in Python. The GIL is highly exploitable, and much of CPython does exploit it. If you don't want to exploit it, that's fine: there was always an obvious approach using an explicit mutex here, and the only thing stopping you from using it is a desire to be clever. Exploiting the GIL in CPython is clever; using an explicit mutex is utterly straightforward. Pick your poison. -- http://mail.python.org/mailman/listinfo/python-list
Re: (win32) speedfan api control
your script works ok on my W2K box :-). It makes me curious if I can get also the temperatures into Python script for further processing as easy as the setting of the checkbox is done? (I have not much experience with this kind of programming yet) May I ask how did you get the TJvXPCheckbox and the other values necessary to access the program GUI ? (as I can see, there is no source code of SpeedFan available) ? Claudio tlviewer [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] hello, If you run the Mainboard monitor, speedfan, here is an ActivePython script to force automatic fan control. http://www.almico.com/speedfan.php It's a great example of how clean the WinApi interface is in ActivePython. The script sets focus to the checkbox of interest and toggles the checkbox. AFAIK, speedfan will only start without fan control, requiring the user to manually check the checkbox to turn it on. hope someone finds it useful, tlviewer #!/usr/bin/python # author: [EMAIL PROTECTED] # date: April 1, 2005 # description: turn on SpeedFan automatic fan speed # keywords: speedfan readings #import win32api as ap import win32gui as wi import win32ui as ui import win32con as wc # dialog class name cl = TJvXPCheckbox try: hWnd = wi.FindWindowEx( 0, 0, TForm1, SpeedFan 4.20) print hWnd hWnd = wi.FindWindowEx( hWnd, 0, TPageControl, ) print hWnd hWnd = wi.FindWindowEx( hWnd, 0, TTabSheet, Readings) print hWnd hWnd = wi.FindWindowEx( hWnd, 0, cl, Automatic fan speed) print hWnd res = wi.SetForegroundWindow(hWnd) res=wi.SendMessageTimeout(hWnd,wc.WM_LBUTTONDOWN,wc.MK_LBUTTON,0,2,75) res = wi.SendMessageTimeout( hWnd,wc.WM_LBUTTONUP, 0, 0, 2, 75 ) print res except: pass #end code -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
Hello Ron , You have many good explanations already, but I thought that this __might__ help others. Like you I was confused by the decorator syntax. till I realized it was shorthand for ... def identity(f): return f def foo(): pass # this is the 'old way' foo = identity(foo) It just rebinds foo to the return value of the decorator function. With the new syntax it becomes. def identity(f): return f @identity def foo(self): pass This is the same as above but now the function is passed and rebound behind the scenes. Also note that decorators don't have to be a nested function, it really depends on what you are trying to achieve. hth, M.E.Farmer -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Am Samstag, 2. April 2005 22:28 schrieb Paul Rubin: I'm starting to believe the GIL covers up an awful lot of sloppiness in Python. I wonder if there could be a decorator approach: @synchronized def counter(): t = itertools.count() while True: yield t.next() Of course there could: def synchronized_iterator(f): def wrapper(*args,**kwargs): class iterator(object): def __init__(self,f,args,kwargs): self.iter = f(*args,**kwargs) self.lock = threading.RLock() def __iter__(self): return self def next(self): self.lock.acquire() try: return self.iter.next() finally: self.lock.release() return iterator(f,args,kwargs) return wrapper @synchronized_iterator def create_counter(): t = itertools.count() while True: yield t.next() or counter = synchronized_iterator(itertools.count) I used a class-based approach, as I don't want to destroy the semantics of calling the returned wrapper(), which should've already instantiated the wrapped generator object (which doesn't happen when making wrapper() a generator itself). -- --- Heiko. pgp4kxA2GZNHL.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Am Sonntag, 3. April 2005 00:57 schrieb Heiko Wundram: snip or Make that: create_counter = syncronized_iterator(itertools.count) and counter = create_counter() to create the actual counter regardless of iterator. -- --- Heiko. pgpuQ5CRv1IKe.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple thread-safe counter?
Tim Peters [EMAIL PROTECTED] writes: If you don't want to exploit it, that's fine: there was always an obvious approach using an explicit mutex here, and the only thing stopping you from using it is a desire to be clever. Exploiting the GIL in CPython is clever; using an explicit mutex is utterly straightforward. Pick your poison. For my immediate requirement I'm going to use xrange as you suggested (thanks!). It will work exactly as I want. I'd be uncomfortable publishing such a program for long-term use by other people, but this is just for a short-term hack (famous last words). I think using an explicit mutex goes against Guido's quote mentioned yesterday, about avoiding tedious code. But relying on the GIL results in a brittle program that only works correctly in one Python implementation. As use of other implementations becomes more widespread, one of three things has to happen: 1) Python programs will become uglier and more mistake-prone (from using explicit mutexes all over the place); 2) Python programs will become unreliable (from depending on a GIL that doesn't exist any more); or 3) Python will acquire some new mechanisms for handling reliable synchronization easily and cleanly, as other languages already have. I think (3) is the preferable choice and it's reasonable to try to figure out how such mechanisms should work. -- http://mail.python.org/mailman/listinfo/python-list
Re: instance name
max(01)* wrote: hi. is there a way to define a class method which prints the instance name? e.g.: class class_1: ... def myName(self): ... what should i do here ... instance_1 = class_1() instance_1.myName() 'instance_1' bye macs macs, The object instance doesn't know about the identifier to which you assigned it (but see my example below using the inspect module). If you are just trying to identify different instances, you can get a unique instance identifier from hash() function hash(instance1) or self.__hash__(), or hash(self) from within the object The default __str__ for a class will normally return something similar with more information , so that when you print instance_1 you get a string in the following format: module.classname object at hashvalue The following is a very, very, very weak example of how you might use inspect to get the information you want. This is not a robust example at all, and I imagine it could have many ways it will fail. The init uses inspect information to dip into the source file and parse out the info -- like I said, this is not a robust parse. # import os,sys class class_1(object): def __init__(self): import inspect self.lineno = inspect.currentframe().f_back.f_lineno self.filename = inspect.currentframe().f_back.f_code.co_filename self.initial_instance = None try: # i'm not sure if filename is full path or not at this time f=open(self.filename,'r') lines=f.readlines() s = lines[self.lineno-1] split = s.split() try: # find the assignment if possible i = split.index('=') self.initial_instance = split[i-1] except ValueError: pass finally: f.close() def myName(self): print 'my initial instance was \'%s\''%(self.initial_instance) if __name__ == '__main__': # two straight up examples instance_1 = class_1() instance_2 = class_1() instance_1.myName() instance_2.myName() # class_1().myName() c = instance_1 c.myName() # --- I got the following results: my initial instance was 'instance_1' my initial instance was 'instance_2' my initial instance was 'None' my initial instance was 'instance_1' Best-regards, Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: string goes away
Andreas Beyer [EMAIL PROTECTED] writes: OK, you won. I read in an (regretably old) guidline for improving Python's performance that you should prefer map() compared to list comprehensions. Apparently the performance of list comprehensions has improved a lot, which is great. (Or the overhead of calling map() got too big, but I hope this is not the case.) So, what is the purpose of map()? Should it too be deprecated? map() is already slated for removal in Python 3.0. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: testing -- what to do for testing code with behaviour dependant upon which files exist?
Can't you use the tempfile module to generate unique names for non-existent files and directories? Take a look at http://www.python.org/doc/lib/module-tempfile.html -- it works on all supported platforms. Grig -- http://mail.python.org/mailman/listinfo/python-list
Re: Docorator Disected
On Sat, 02 Apr 2005 21:28:36 GMT, [EMAIL PROTECTED] (Bengt Richter) wrote: I think it might help you to start out with very plain decorators rather than decorators as factory functions that return decorator functions that wrap the decorated function in a wrapper function. E.g., (this could obviously be parameterized as a single decorator factory, but I wanted to show the simplest level of decorator functionality) clipped interesting examples Thanks for the examples of stacked decorators! :-) I think I pretty much got it now, I had never needed to pass arguments to nested defined functions before and none of the documentation I have, ever mentioned that alternative. So I didn't know I could do this: def foo(a1): def fee(a2): return a1+a2 return fee fum = foo(2)(6) -- !!! # fum is 8 The interesting thing about this is the 'return fee' statement gets the (6) apparently appended to it. So it becomes 'return fee(6). That subtle action is confusing if you don't already know about it, which I didn't. In this example. def foo(a1): def fee(a2): return a1+a2 return fee fum = foo(2) There is no second set of arguments to append to 'return fee', so the name fum is pointed to object fee instead and fee is not evaluated. This second subtle action, is also confusing if you aren't aware of it. Since the two look the same when you examine the def statements. So there is no reason to think they would not act the same, both returning an function object. Now, add in the @decorator syntax to the mix. Which hides the extra argument sets that are passed to the nested defined functions and the obscuration is complete. There then is no visual indication of where the function calls get their arguments from, and this is what I believe caused me to have so much trouble with this. Another inconsistency, although not a bad one, is that nested 'defined' function share scope, but nested function calls do not. Now what this means, is it will be very difficult for some people to put it all together. I would have gotten it sooner or later, but I really happy to have help from comp.lang.python. on this one. :) I like annotated code walk-throughs. But as others have pointed out, it's still a bit buggy ;-) It helped a lot, but notice that it took me several tries. That's a strong indicator that decorators are more implicit than explicit and that goes against the Explicit is better than Implicit guideline that python tries to follow. Maybe there are ways to make decorators -and- nested function calls a bit more explicit? I think a having indicators on the return statements that are meant to return a value vs object would help readability and take some of the mystery out as far as the un initiated are concerned. def foo(a1): def fee(a2): def fiddle(a3): pass return a3 return fee # Always return a function object. # Error, if argument is passed to it. # and return fee(a2) # always require an argument, # error if none is passed to it. Or some other way if this breaks something. But it will make it more apparent what nested function should do. And give clearer feed back when trying to use or write decorators. I'm not sure what might make @decorator more explicit. Maybe allowing all the function to be specified as an option. Maybe it is already(?) @decorator(a1)(foo) def foo(): pass So we will have: def foo(a1): def fee(a2): def fiddle(a3): pass return a3 return fee # Object always returned here or # or error if argument is received. @decorator(a1)(fum) # Last argument optional. def fum(a3): return a3 These I think are small changes that might be acceptable. A little more aggressive alterations would be: Requiring the 'function' argument may have a use when using stacked decorators. Then it could be inserted into a sequence? @deco3(postcalc) @deco2(fum) @deco1(precalc) def fum(pointxyz): return translatepoint(pointxyz) ... and that reversed order... (yuck!), is it really necessary? Readability is important, and it is a big reason people don't jump ship for some other language. Why the exceptions here? Ok, don't mean to grip. :-) I'm sure there's been plenty of that in past discussions. Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list
Corectly convert from %PATH%=c:\\X; c:\\a; b TO ['c:\\X', 'c:\\a; b']
Hi, I am trying to treat an environment variable as a python list - and I'm sure there must be a standard and simple way to do so. I know that the interpreter itself must use it (to process $PATH / %PATH%, etc) but I am not able to find a simple function to do so. os.environ['PATH'].split(os.sep) is wrong on Windows for the case when PATH=c:\\A;B;c:\\D; where there is a ';' embedded in the quoted path. Does anyone know of a simple way (addons ok) which would do it in a cross platform way? If not - I will roll my own. My search has shown that generally people just use the simple split menthod as above and leave it there but it seemed like such a common operation that I believe there must be a way out for it which I am not seeing. Thanks, Chirayu. -- http://mail.python.org/mailman/listinfo/python-list
Re: testing -- what to do for testing code with behaviour dependant upon which files exist?
On Sat, 02 Apr 2005 15:30:13 -0500, Brian van den Broek wrote: So, how does one handle such cases with tests? When I had a similar situation, I created a directory for testing that was in a known state, and tested on that. If you can test based on a relative directory, that should work OK. Non-existant paths shouldn't be too hard to come up with; hardcoding a constant relative dir of THISDIRECTORYCANTPOSSIBLYEXISTANDIFITDOESYOURENUTS ought to do OK. -- http://mail.python.org/mailman/listinfo/python-list
Re: Corectly convert from %PATH%=c:\\X; c:\\a; b TO ['c:\\X', 'c:\\a; b']
chirayuk wrote: Hi, I am trying to treat an environment variable as a python list - and I'm sure there must be a standard and simple way to do so. I know that the interpreter itself must use it (to process $PATH / %PATH%, etc) but I am not able to find a simple function to do so. os.environ['PATH'].split(os.sep) is wrong on Windows for the case when PATH=c:\\A;B;c:\\D; where there is a ';' embedded in the quoted path. Does anyone know of a simple way (addons ok) which would do it in a cross platform way? If not - I will roll my own. My search has shown that generally people just use the simple split menthod as above and leave it there but it seemed like such a common operation that I believe there must be a way out for it which I am not seeing. Thanks, Chirayu. You may be able to bend the csv module to your purpose: test = \c:\\A;B;c:\\D; test1 = os.environ['PATH'] import csv class path(csv.excel): ... delimiter = ';' ... quotechar = '' ... csv.reader([test],path).next() ['c:\\A;B', 'c:\\D', ''] csv.reader([test1],path).next() ['C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\Program Files\\ATI Technologies\\ATI Control Panel', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools', 'C:\\WINDOWS\\system32', 'C:\\WINDOWS', 'C:\\WINDOWS\\System32\\Wbem', 'C:\\Program Files\\ATI Technologies\\ATI Control Panel', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin', 'C:\\PROGRA~1\\ATT\\Graphviz\\bin\\tools', 'c:\\python24', 'c:\\python24\\scripts', 'G:\\cabs\\python\\pypy\\py\\bin'] HTH Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Name of IDLE on Linux
Thomas Rast wrote: Edward Diener [EMAIL PROTECTED] writes: It is a pity the Python Linux binary installations do not create folders on the desktop or in the Gnome menu system with links to the Python to the documentation and a readme telling me what executables were installed. Imagine they did, and the other 1392 packages (on my system) too :-) I can understand that g. But I would rather have links somewhere than not know what it was a particular package does and how to use it. As a relative Linux newbie, I have found it disconcerting to have things installed on my system when I first booted the system or when I install new packages or upgrade existing ones, and not know what these packages are used for. One can always delete links, and they are cheap, so I would rather have too much to begin with than too little. Documentation usually resides in /usr/share/doc/package, /usr/share/doc/packages/package or similiar. You're on an RPM-based system, so $ rpm -ql package lists all files belonging to that package. You should have little trouble spotting the documentation files there. Thanks. That does help. -- http://mail.python.org/mailman/listinfo/python-list
Re: redundant imports
On Sat, 02 Apr 2005 16:44:29 -0600, Mike Meyer [EMAIL PROTECTED] wrote: max(01)* [EMAIL PROTECTED] writes: Peter Hansen wrote: max(01)* wrote: hi everybody. suppose that code-1.py imports code-2.py and code-3.py (because it uses names from both), and that code-2.py imports code-3.py. if python were c, code-1.c should only *include* code-2.c, because the latter in turns includes code-3.c. inclusion of modules in c is a purely preprocessing textual matter (compilation is deferred to after the fact), i guess, so that such things are possible. import of modules in python is a different beast, so the redundancy is (i think) necessary. any comment/suggestion/idea? You're mixed up about this whole idea. that's why i am asking for advice here. my concern was motivated by a (clumsy) attempt to understand the difference of mechanism between the approach to modular programming in a more traditional language (c) and python. [Names changed to be valid python module names.] I feel I ought to point out that you don't really *have* to import the code_3.py in code_1.py. You can get to things code_3.py in code_1.py as code_2.code_3.thing. The semantic behavior of include in C is the same as from module Bzzt ;-) It's not the same semantics! import * in python. Both cases add all the names in the included namespace directly to the including namespace. This usage is But a C #include results in processing as if the _source_ were substituted into the including source in place of the #include line. That's not what from module import * does, because when import executes the module's source (happening only the first time BTW, unlike in-place #include) it creates a module global that is distinct from the includer's global. The import * creates local bindings to the objects visible as bindings in the global space of the module, but the objects retain their module global references (if any, since there doesn't have to be any). IMO execfile('module.py') is closer to C's #include effect. Note: print '\n%s'%open('impex.py').read() g = 'g in global space of impex.py' def showg(): print g from impex import * g 'g in global space of impex.py' showg() g in global space of impex.py g = 'g in global space of importer' g 'g in global space of importer' showg() g in global space of impex.py Note that showg insisted on looking for g in it's idea of global. Now we'll bring in g and showg via execfile: execfile('impex.py') g 'g in global space of impex.py' showg() g in global space of impex.py g = 'g in global space of importer' showg() g in global space of importer Note that because execfile executed the definition of showg in the interactive global space, it sees the interactive change of g's binding (in what it now also showg's global space). (Execfile inside a function or class definition needs careful control, but can be done). depreciated in Python, because it leads to problems figuring out where a specific variable came from. In C, it creates a problem called name space pollution. This is the case when a file1.c gets all the symbols for some_header.h, even though it doesn't include/need those symbols, because some header file1.c included needed a symbol from some_header.h. This is especially galling if the pollution collides with some_header2.h that file1.c actually needs. Of course in C you could write some #ifxxx kludges to control inclusion of named things from a given header file somewhat. But name space pollution is a pox, to be sure ;-) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorater inside a function? Is there a way?
On 2 Apr 2005 10:23:53 -0800, George Sakkis [EMAIL PROTECTED] wrote: It turns out it's not a how to inflate tires with a hammer request; I've actually written an optional type checking module using decorators. The implementation details are not easy to grok, but the usage is straightforward: from typecheck import * @returns(listOf(int, size=3)) @expects(x=str, y=containerOf(int)) def foo(x,y): return [len(x)] + list(y) Hi George, I wrote one like that too yesterday once I figured out how to pass the arguments, except not with 'conainerof' emplemented. That's a nice touch. @define((int, int, float), (int, list)) def somefunction( a_int, b_int, c_float): # some stuff return an_int, an_list It checks both the inputs and returns for types and number of items. And gives error appropriate for both. Next, would be to surround the 'def define' statements with an if __debug__: statement so it can be turned off for the final version. I wonder if a decorator, that just passes values straight though, gets optimized out or not? Or if there's another way to turn of a decorator? I also have a test_type function that can be put inline that tries to adapt the value before it gives an error. These are interesting problems to solve and go into my tool box, although I don't have a need for them at the moment. :) Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list