Re: can't load an script from html...
Ricardo wrote: > Hi everyone > I'm trying to use the cgi library to create a python script and loading it > from a web page. I have already done the necessary imports, and the > default commands to receive data from "html" are written too. The final > version is something like this: > > #!/usr/bin/python > > import subprocess > import cgi > import cgitb > > cgitb.enable() > > input = cgi.FieldStorage() > > …. my code (do something with input)…. > > > #printing the response > > print "Content-Type: text/html" > print > print "My title:" > print "" > print "" > print ….. bla bla … > print "%s"%theoutput > print "" > > Besides, my call from my index.html is like this: > > > > > > > well, the thing is that when i do the call from the browser: > > http://localhost/index.html >| > V > put the data and click on the "accept" button >| > V > http:/localhost/scripts/python_script.py > > I only get the python_script.py as a plain test by response (the script > printed on my browser). I have already changed the permissions for > python_script.py. I have checked the import cgi,cgitb in the python shell > (i am using v2.7) and they work fine. So, i don't know what it is going > wrong here. > > A little help please… any idea? Is your webserver configured to allow cgi scripts? In the scripts directory? For Apache see http://httpd.apache.org/docs/current/howto/cgi.html Python also comes with a CGI Server. A quick-and-dirty setup goes like this: $ cat cgi-bin/script.py #!/usr/bin/python # -*- coding: utf-8 -*- import cgi import cgitb cgitb.enable() input = cgi.FieldStorage() print "Content-Type: text/html" print print "My title:" print "" print "" print "Hello world" print "" $ chmod a+x cgi-bin/script.py $ python -m CGIHTTPServer Serving HTTP on 0.0.0.0 port 8000 ... If you then point your browser to http://localhost:8000/cgi-bin/script.py you should see Hello world in the browser and (something like) localhost - - [24/Sep/2011 08:41:27] "GET /cgi-bin/script.py HTTP/1.1" 200 - in the shell. Note that the script must be in cgi-bin (or htbin) unless you start the server with a custom script that modifies CGIHTTPRequestHandler.cgi_directories accordingly. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Sep 23, 7:03 pm, Matt Joiner wrote: > how do you get the call stacks like this? > > > > > > > > On Sat, Sep 24, 2011 at 3:59 AM, Atherun wrote: > > On Sep 23, 10:47 am, Nobody wrote: > >> On Fri, 23 Sep 2011 06:59:12 +0100, Nobody wrote: > >> >> kernel32.dll!WaitForSingleObject+0x12 > >> >> python26.dll!_Py_svnversion+0xcf8 > > >> > I haven't a clue how this happens. _Py_svnversion just returns a string: > > >> In retrospect, I think that's a red herring. 0xcf8 seems like too large an > >> offset for such a small function. I think that it's more likely to be in a > >> non-exported function, and _Py_svnversion just happens to be the last > >> exported symbol prior to that point in the code. > > > I have the call stacks for each python thread running up until the > > dead lock: > > > # ThreadID: 992 > > out, err = proc.communicate("change: new\ndescription: %s > > \n"%changelistDesc) > > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > > communicate > > return self._communicate(input) > > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > > _communicate > > stdout_thread.join() > > File: "c:\src\extern\python\lib\threading.py", line 637, in join > > self.__block.wait() > > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > > waiter.acquire() > > > # ThreadID: 5516 > > File: "c:\src\extern\python\lib\threading.py", line 497, in > > __bootstrap > > self.__bootstrap_inner() > > File: "c:\src\extern\python\lib\threading.py", line 525, in > > __bootstrap_inner > > self.run() > > File: "c:\src\extern\python\lib\threading.py", line 477, in run > > self.__target(*self.__args, **self.__kwargs) > > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > > _readerthread > > buffer.append(fh.read()) > > > # ThreadID: 2668 > > File: "c:\src\extern\python\lib\threading.py", line 497, in > > __bootstrap > > self.__bootstrap_inner() > > File: "c:\src\extern\python\lib\threading.py", line 525, in > > __bootstrap_inner > > self.run() > > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 69, in > > run > > self.stacktraces() > > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 86, in > > stacktraces > > fout.write(stacktraces()) > > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 26, in > > stacktraces > > for filename, lineno, name, line in traceback.extract_stack(stack): > > > # ThreadID: 3248 > > out, err = proc.communicate("change: new\ndescription: %s > > \n"%changelistDesc) > > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > > communicate > > return self._communicate(input) > > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > > _communicate > > stdout_thread.join() > > File: "c:\src\extern\python\lib\threading.py", line 637, in join > > self.__block.wait() > > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > > waiter.acquire() > > > # ThreadID: 7700 > > File: "c:\src\extern\python\lib\threading.py", line 497, in > > __bootstrap > > self.__bootstrap_inner() > > File: "c:\src\extern\python\lib\threading.py", line 525, in > > __bootstrap_inner > > self.run() > > File: "c:\src\extern\python\lib\threading.py", line 477, in run > > self.__target(*self.__args, **self.__kwargs) > > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > > _readerthread > > buffer.append(fh.read()) > > > # ThreadID: 8020 > > out, err = proc.communicate("change: new\ndescription: %s > > \n"%changelistDesc) > > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > > communicate > > return self._communicate(input) > > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > > _communicate > > stdout_thread.join() > > File: "c:\src\extern\python\lib\threading.py", line 637, in join > > self.__block.wait() > > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > > waiter.acquire() > > > # ThreadID: 4252 > > File: "c:\src\extern\python\lib\threading.py", line 497, in > > __bootstrap > > self.__bootstrap_inner() > > File: "c:\src\extern\python\lib\threading.py", line 525, in > > __bootstrap_inner > > self.run() > > File: "c:\src\extern\python\lib\threading.py", line 477, in run > > self.__target(*self.__args, **self.__kwargs) > > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > > _readerthread > > buffer.append(fh.read()) > > > The StackTracer thread freezes trying to update my output file, and > > yes I'm trying to 3 tasks in parallel which each one starts by > > creating a changelist in perforce. This is just an easy repro case > > for me, it happens with commands other then p4. This almost looks > > like a threading issue more then the output deadlock. > > -- > >http://mail.python.org/mailman/listinfo/python-list I found the code for it here: http://code.activestate.com/recipes/577334-how-to-debug-deadlocked-multi-threaded-programs/ There is some bugs in the code given but its pretty straight forward to fix it. -- http://mail.pyt
Re: Why is the shutil module called shutil?
On Sep 23, 11:58 pm, Chris Rebert wrote: > On Fri, Sep 23, 2011 at 8:36 PM, Fletcher Johnson > wrote: > > The topic says it all: > > Why is shutil named shutil? What does it stand for? This is just a > > mild curiosity of mine. > > "sh" is short for "shell", in line with Unix convention, where the > default shell is located at > /bin/sh.http://en.wikipedia.org/wiki/Shell_(computing)http://en.wikipedia.org/wiki/Unix_shell > > "util" is short for "utilities". > > shutil is a utility module used to accomplish tasks which one often > does when in the shell, such as copying, moving, or removing directory > trees. But shutil (to my knowledge) is not implemented using shell > commands or by running external programs, so it thus avoids a whole > host of shell-related issues. > > It's not the best name, but what with backwards compatibility and all, > it's unlikely to change any time soon. > > Cheers, > Chris > --http://rebertia.com I had a hunch it might have been that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is the shutil module called shutil?
On Fri, Sep 23, 2011 at 8:36 PM, Fletcher Johnson wrote: > The topic says it all: > Why is shutil named shutil? What does it stand for? This is just a > mild curiosity of mine. "sh" is short for "shell", in line with Unix convention, where the default shell is located at /bin/sh. http://en.wikipedia.org/wiki/Shell_(computing) http://en.wikipedia.org/wiki/Unix_shell "util" is short for "utilities". shutil is a utility module used to accomplish tasks which one often does when in the shell, such as copying, moving, or removing directory trees. But shutil (to my knowledge) is not implemented using shell commands or by running external programs, so it thus avoids a whole host of shell-related issues. It's not the best name, but what with backwards compatibility and all, it's unlikely to change any time soon. Cheers, Chris -- http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: pyWin build 216
On 24/09/2011 12:25 AM, python wrote: I have used pyWin for several years now with out issue. I recently installed build 216 for python 2.7 on windows XP pro. The program crashes every time I exit a wxPython program and has crashed a few other times. There are a number of issues using Pythonwin to run a program which uses a different UI toolkit (eg, wx, Tkinter) and while these have been around for many years I don't plan on trying to fix it. IOW, "don't do that" :) I does not seem that pyWin has been updated since February of this year. Is there a direction change for the windows extensions? Is it time I make the move to 3.x? Mark Hammond has given much to the Python community and I do not intend for this post to be negative in any way. No problem. There have been no updates as there is very little to update (ie, the code hasn't change a huge amount in hg since then). There will probably be a new version in the next month or so, but that is quite orthogonal to whether you should move to 3.x - the 3.x version of Pythonwin hasn't been updated in the same period and is built from the same source tree, so is likely to have exactly the same problems (infact is likely to have a few more - there are probably a few 3.x specific issues still hiding away). Mark -- http://mail.python.org/mailman/listinfo/python-list
Why is the shutil module called shutil?
The topic says it all: Why is shutil named shutil? What does it stand for? This is just a mild curiosity of mine. The shutil module for reference: http://docs.python.org/library/shutil.html#module-shutil -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
how do you get the call stacks like this? On Sat, Sep 24, 2011 at 3:59 AM, Atherun wrote: > On Sep 23, 10:47 am, Nobody wrote: >> On Fri, 23 Sep 2011 06:59:12 +0100, Nobody wrote: >> >> kernel32.dll!WaitForSingleObject+0x12 >> >> python26.dll!_Py_svnversion+0xcf8 >> >> > I haven't a clue how this happens. _Py_svnversion just returns a string: >> >> In retrospect, I think that's a red herring. 0xcf8 seems like too large an >> offset for such a small function. I think that it's more likely to be in a >> non-exported function, and _Py_svnversion just happens to be the last >> exported symbol prior to that point in the code. > > I have the call stacks for each python thread running up until the > dead lock: > > # ThreadID: 992 > out, err = proc.communicate("change: new\ndescription: %s > \n"%changelistDesc) > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > communicate > return self._communicate(input) > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > _communicate > stdout_thread.join() > File: "c:\src\extern\python\lib\threading.py", line 637, in join > self.__block.wait() > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > waiter.acquire() > > # ThreadID: 5516 > File: "c:\src\extern\python\lib\threading.py", line 497, in > __bootstrap > self.__bootstrap_inner() > File: "c:\src\extern\python\lib\threading.py", line 525, in > __bootstrap_inner > self.run() > File: "c:\src\extern\python\lib\threading.py", line 477, in run > self.__target(*self.__args, **self.__kwargs) > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > _readerthread > buffer.append(fh.read()) > > # ThreadID: 2668 > File: "c:\src\extern\python\lib\threading.py", line 497, in > __bootstrap > self.__bootstrap_inner() > File: "c:\src\extern\python\lib\threading.py", line 525, in > __bootstrap_inner > self.run() > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 69, in > run > self.stacktraces() > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 86, in > stacktraces > fout.write(stacktraces()) > File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 26, in > stacktraces > for filename, lineno, name, line in traceback.extract_stack(stack): > > # ThreadID: 3248 > out, err = proc.communicate("change: new\ndescription: %s > \n"%changelistDesc) > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > communicate > return self._communicate(input) > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > _communicate > stdout_thread.join() > File: "c:\src\extern\python\lib\threading.py", line 637, in join > self.__block.wait() > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > waiter.acquire() > > > # ThreadID: 7700 > File: "c:\src\extern\python\lib\threading.py", line 497, in > __bootstrap > self.__bootstrap_inner() > File: "c:\src\extern\python\lib\threading.py", line 525, in > __bootstrap_inner > self.run() > File: "c:\src\extern\python\lib\threading.py", line 477, in run > self.__target(*self.__args, **self.__kwargs) > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > _readerthread > buffer.append(fh.read()) > > # ThreadID: 8020 > out, err = proc.communicate("change: new\ndescription: %s > \n"%changelistDesc) > File: "c:\src\extern\python\lib\subprocess.py", line 689, in > communicate > return self._communicate(input) > File: "c:\src\extern\python\lib\subprocess.py", line 903, in > _communicate > stdout_thread.join() > File: "c:\src\extern\python\lib\threading.py", line 637, in join > self.__block.wait() > File: "c:\src\extern\python\lib\threading.py", line 237, in wait > waiter.acquire() > > # ThreadID: 4252 > File: "c:\src\extern\python\lib\threading.py", line 497, in > __bootstrap > self.__bootstrap_inner() > File: "c:\src\extern\python\lib\threading.py", line 525, in > __bootstrap_inner > self.run() > File: "c:\src\extern\python\lib\threading.py", line 477, in run > self.__target(*self.__args, **self.__kwargs) > File: "c:\src\extern\python\lib\subprocess.py", line 877, in > _readerthread > buffer.append(fh.read()) > > The StackTracer thread freezes trying to update my output file, and > yes I'm trying to 3 tasks in parallel which each one starts by > creating a changelist in perforce. This is just an easy repro case > for me, it happens with commands other then p4. This almost looks > like a threading issue more then the output deadlock. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: can't load an script from html...
On 24/09/2011 02:42, Ricardo wrote: Hi everyone I'm trying to use the cgi library to create a python script and loading it from a web page. I have already done the necessary imports, and the default commands to receive data from "html" are written too. The final version is something like this: #!/usr/bin/python import subprocess import cgi import cgitb cgitb.enable() input = cgi.FieldStorage() …. my code (do something with input)…. #printing the response print "Content-Type: text/html" print print "My title:" print "" print "" print ….. bla bla … print "%s"%theoutput print "" Besides, my call from my index.html is like this: well, the thing is that when i do the call from the browser: http://localhost/index.html | V put the data and click on the "accept" button | V http:/localhost/scripts/python_script.py I only get the python_script.py as a plain test by response (the script printed on my browser). I have already changed the permissions for python_script.py. I have checked the import cgi,cgitb in the python shell (i am using v2.7) and they work fine. So, i don't know what it is going wrong here. A little help please… any idea? Thanks anyway for your time. If it helps, the responses on my home-make stuff start something like this: -- http://mail.python.org/mailman/listinfo/python-list
can't load an script from html...
Hi everyone I'm trying to use the cgi library to create a python script and loading it from a web page. I have already done the necessary imports, and the default commands to receive data from "html" are written too. The final version is something like this: #!/usr/bin/python import subprocess import cgi import cgitb cgitb.enable() input = cgi.FieldStorage() …. my code (do something with input)…. #printing the response print "Content-Type: text/html" print print "My title:" print "" print "" print ….. bla bla … print "%s"%theoutput print "" Besides, my call from my index.html is like this: well, the thing is that when i do the call from the browser: http://localhost/index.html | V put the data and click on the "accept" button | V http:/localhost/scripts/python_script.py I only get the python_script.py as a plain test by response (the script printed on my browser). I have already changed the permissions for python_script.py. I have checked the import cgi,cgitb in the python shell (i am using v2.7) and they work fine. So, i don't know what it is going wrong here. A little help please… any idea? Thanks anyway for your time. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
Thank You Christian Im running on Ubuntu Natty and I am not running a self-compiled install, its a regular release. In order to do this: $ make distclean $ export LDFLAGS="-L/usr/lib/$(dpkg-architecture - qDEB_HOST_MULTIARCH)" $ ./configure $ make $ make install $ unset LDFLAGS If you can, can you explain the steps for this, can I run this in a regular release of Python2.5? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
No idea, as I said before, if you ask for it they might put in the alpha. On Sat, Sep 24, 2011 at 8:30 AM, Jesse Ramirez wrote: > > Thanks Alec, might you know when the 2.7 support might come? -- http://mail.python.org/mailman/listinfo/python-list
Re: Context manager with class methods
Terry Reedy wrote: it is normal to look for special methods on the class (and superclasses) > of an object rather than starting with the object itself. I suspect there was a deliberate change to correct an anomaly, though this might have been done as part of some other change. It's a necessary consequence of the fact that new-style classes are also instances. Without it, there would be an ambiguity as to whether a special method defined the behaviour of instances of a class or of the class object itself. It also increases efficiency, because for those special methods that correspond to C-level type slots, you only have to look in the type slot to find an implementation of the method, rather than having to look in the instance dict first. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: strange behavior from recursive generator
I don't know how many times I stared at that code without seeing the error. Thanks so much! Phillip On Fri, Sep 23, 2011 at 1:26 PM, Arnaud Delobelle wrote: > On 23 September 2011 21:09, Dr. Phillip M. Feldman > wrote: > > > > A few weeks ago, I wrote a class that creates an iterator for solving the > > general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert > > converted my code to a generator, which made the code cleaner, and I > > subsequently simplified it somewhat further. > > > > My problem is the following: All of these versions of the code work fine > for > > very small problems, but do not produce the full set of occupancy > > distributions for larger problems. The following sample input and output > > show what happens with two balls and two boxes (to keep things simple, > I've > > made the boxes large enough so that each box can hold both balls). > > > > In [6]: x= balls_in_labeled_boxes(2,[2,2]) > > > > In [7]: list(x) > > balls=2, box_sizes=[2, 2] > > About to make recursive call. balls_in_other_boxes=0, box_sizes=[2] > > i=0, distribution_other=(0,) > > About to make recursive call. balls_in_other_boxes=1, box_sizes=[2] > > i=0, distribution_other=(1,) > > About to make recursive call. balls_in_other_boxes=2, box_sizes=[2] > > i=0, distribution_other=(2,) > > Out[7]: [(2, 0), (1, 1), (0, 2)] > > > > Note that Out[7] above gives the correct result, showing all three > possible > > distributions. Now lets try the same thing with three boxes. > > > > In [8]: x= balls_in_labeled_boxes(2,[2,2,2]) > > > > In [9]: list(x) > > balls=2, box_sizes=[2, 2, 2] > > About to make recursive call. balls_in_other_boxes=0, box_sizes=[2, 2] > > i=0, distribution_other=(0, 0) > > About to make recursive call. balls_in_other_boxes=1, box_sizes=[2, 2] > > i=0, distribution_other=(1, 0) > > About to make recursive call. balls_in_other_boxes=2, box_sizes=[2, 2] > > i=0, distribution_other=(2, 0) > > i=1, distribution_other=(1, 1) > > Out[9]: [(2, 0, 0), (1, 1, 0), (0, 2, 0), (0, 1, 1)] > > > > When there are no balls in the initial box, the recursive call should > > produce the same three occupancy distributions that we saw above, but one > of > > them is now missing. If someone can shed light on why this is happening, > I'd > > be grateful. > > Line 46: > > for distribution_other in _balls_in_unlabeled_boxes( > > Should be: > > > for distribution_other in _balls_in_labeled_boxes( > > HTH > > -- > Arnaud > -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
Thank You Christian Im running on Ubuntu Natty and I am not running a self-compiled install, its a regular release. In order to do this: $ make distclean $ export LDFLAGS="-L/usr/lib/$(dpkg-architecture - qDEB_HOST_MULTIARCH)" $ ./configure $ make $ make install $ unset LDFLAGS If you can, can you explain the steps for this, can I run this in a regular release of Python2.5? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
Am 23.09.2011 17:41, schrieb Jesramz: > Python 2.5.6 (r256:88840, Sep 22 2011, 13:45:58) > [GCC 4.5.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. import zlib > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named zlib Are you running a self-compiled installation of Python 2.5 on recent Debian or Ubuntu? Check out my blog http://lipyrary.blogspot.com/2011/05/how-to-compile-python-on-ubuntu-1104.html Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: strange behavior from recursive generator
On 23 September 2011 21:09, Dr. Phillip M. Feldman wrote: > > A few weeks ago, I wrote a class that creates an iterator for solving the > general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert > converted my code to a generator, which made the code cleaner, and I > subsequently simplified it somewhat further. > > My problem is the following: All of these versions of the code work fine for > very small problems, but do not produce the full set of occupancy > distributions for larger problems. The following sample input and output > show what happens with two balls and two boxes (to keep things simple, I've > made the boxes large enough so that each box can hold both balls). > > In [6]: x= balls_in_labeled_boxes(2,[2,2]) > > In [7]: list(x) > balls=2, box_sizes=[2, 2] > About to make recursive call. balls_in_other_boxes=0, box_sizes=[2] > i=0, distribution_other=(0,) > About to make recursive call. balls_in_other_boxes=1, box_sizes=[2] > i=0, distribution_other=(1,) > About to make recursive call. balls_in_other_boxes=2, box_sizes=[2] > i=0, distribution_other=(2,) > Out[7]: [(2, 0), (1, 1), (0, 2)] > > Note that Out[7] above gives the correct result, showing all three possible > distributions. Now lets try the same thing with three boxes. > > In [8]: x= balls_in_labeled_boxes(2,[2,2,2]) > > In [9]: list(x) > balls=2, box_sizes=[2, 2, 2] > About to make recursive call. balls_in_other_boxes=0, box_sizes=[2, 2] > i=0, distribution_other=(0, 0) > About to make recursive call. balls_in_other_boxes=1, box_sizes=[2, 2] > i=0, distribution_other=(1, 0) > About to make recursive call. balls_in_other_boxes=2, box_sizes=[2, 2] > i=0, distribution_other=(2, 0) > i=1, distribution_other=(1, 1) > Out[9]: [(2, 0, 0), (1, 1, 0), (0, 2, 0), (0, 1, 1)] > > When there are no balls in the initial box, the recursive call should > produce the same three occupancy distributions that we saw above, but one of > them is now missing. If someone can shed light on why this is happening, I'd > be grateful. Line 46: for distribution_other in _balls_in_unlabeled_boxes( Should be: for distribution_other in _balls_in_labeled_boxes( HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
strange behavior from recursive generator
A few weeks ago, I wrote a class that creates an iterator for solving the general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert converted my code to a generator, which made the code cleaner, and I subsequently simplified it somewhat further. My problem is the following: All of these versions of the code work fine for very small problems, but do not produce the full set of occupancy distributions for larger problems. The following sample input and output show what happens with two balls and two boxes (to keep things simple, I've made the boxes large enough so that each box can hold both balls). In [6]: x= balls_in_labeled_boxes(2,[2,2]) In [7]: list(x) balls=2, box_sizes=[2, 2] About to make recursive call. balls_in_other_boxes=0, box_sizes=[2] i=0, distribution_other=(0,) About to make recursive call. balls_in_other_boxes=1, box_sizes=[2] i=0, distribution_other=(1,) About to make recursive call. balls_in_other_boxes=2, box_sizes=[2] i=0, distribution_other=(2,) Out[7]: [(2, 0), (1, 1), (0, 2)] Note that Out[7] above gives the correct result, showing all three possible distributions. Now lets try the same thing with three boxes. In [8]: x= balls_in_labeled_boxes(2,[2,2,2]) In [9]: list(x) balls=2, box_sizes=[2, 2, 2] About to make recursive call. balls_in_other_boxes=0, box_sizes=[2, 2] i=0, distribution_other=(0, 0) About to make recursive call. balls_in_other_boxes=1, box_sizes=[2, 2] i=0, distribution_other=(1, 0) About to make recursive call. balls_in_other_boxes=2, box_sizes=[2, 2] i=0, distribution_other=(2, 0) i=1, distribution_other=(1, 1) Out[9]: [(2, 0, 0), (1, 1, 0), (0, 2, 0), (0, 1, 1)] When there are no balls in the initial box, the recursive call should produce the same three occupancy distributions that we saw above, but one of them is now missing. If someone can shed light on why this is happening, I'd be grateful. Phillip http://old.nabble.com/file/p32503886/balls_in_labeled_boxes.py balls_in_labeled_boxes.py -- View this message in context: http://old.nabble.com/strange-behavior-from-recursive-generator-tp32503886p32503886.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
comments on runpy module
Hi! I've just had fun with the runpy module in Python 2.7. I'm writing to share it :) What I've tried is to "load" a python script using runpy.run_path(), take a function from the resulting namespace and call it with arbitrary arguments. All the functions in the namespace seem to be ok. repr(namespace["f"]) gives "". But if the f() is referring the modules namespace (I supposed it is the same as the returned one), all the values appear to be None. Example script.py: """ def f(arg): return g(arg) def g(arg): return arg """ Then running main.py: """ import runpy namespace = runpy.run_path("./script.py") print namespace["f"] print namespace["g"] print namespace["f"]("abc") """ gives such an output """ Traceback (most recent call last): File "main.py", line 7, in print namespace["f"]("abc") File "./script.py", line 2, in f return g(arg) TypeError: 'NoneType' object is not callable """ Reading the Lib/runpy.py I've found, that the temporary module created inside the run_path() calls, is destroyed right after the script.py code executed in the resulting namespace. I suppose that it is ether an issue or a feature that should be documented :) -- Have a good time and a good mood! Alex. -- http://mail.python.org/mailman/listinfo/python-list
Re: comments on runpy module
Example script.py: """ def f(arg): return g(arg) def g(arg): return arg """ Reading the Lib/runpy.py I've found, that the temporary module created inside the run_path() calls, is destroyed right after the script.py code executed in the resulting namespace. I've got an idea. It would be nice if there existed such a way to use it: with runpy.run_path("script.py") as a_namespace: a_namespace["f"]("abc") -- Regards, Alex. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Sep 23, 10:47 am, Nobody wrote: > On Fri, 23 Sep 2011 06:59:12 +0100, Nobody wrote: > >> kernel32.dll!WaitForSingleObject+0x12 > >> python26.dll!_Py_svnversion+0xcf8 > > > I haven't a clue how this happens. _Py_svnversion just returns a string: > > In retrospect, I think that's a red herring. 0xcf8 seems like too large an > offset for such a small function. I think that it's more likely to be in a > non-exported function, and _Py_svnversion just happens to be the last > exported symbol prior to that point in the code. I have the call stacks for each python thread running up until the dead lock: # ThreadID: 992 out, err = proc.communicate("change: new\ndescription: %s \n"%changelistDesc) File: "c:\src\extern\python\lib\subprocess.py", line 689, in communicate return self._communicate(input) File: "c:\src\extern\python\lib\subprocess.py", line 903, in _communicate stdout_thread.join() File: "c:\src\extern\python\lib\threading.py", line 637, in join self.__block.wait() File: "c:\src\extern\python\lib\threading.py", line 237, in wait waiter.acquire() # ThreadID: 5516 File: "c:\src\extern\python\lib\threading.py", line 497, in __bootstrap self.__bootstrap_inner() File: "c:\src\extern\python\lib\threading.py", line 525, in __bootstrap_inner self.run() File: "c:\src\extern\python\lib\threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) File: "c:\src\extern\python\lib\subprocess.py", line 877, in _readerthread buffer.append(fh.read()) # ThreadID: 2668 File: "c:\src\extern\python\lib\threading.py", line 497, in __bootstrap self.__bootstrap_inner() File: "c:\src\extern\python\lib\threading.py", line 525, in __bootstrap_inner self.run() File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 69, in run self.stacktraces() File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 86, in stacktraces fout.write(stacktraces()) File: "c:\src\scripts\auto\Autobuilder\StackTracer.py", line 26, in stacktraces for filename, lineno, name, line in traceback.extract_stack(stack): # ThreadID: 3248 out, err = proc.communicate("change: new\ndescription: %s \n"%changelistDesc) File: "c:\src\extern\python\lib\subprocess.py", line 689, in communicate return self._communicate(input) File: "c:\src\extern\python\lib\subprocess.py", line 903, in _communicate stdout_thread.join() File: "c:\src\extern\python\lib\threading.py", line 637, in join self.__block.wait() File: "c:\src\extern\python\lib\threading.py", line 237, in wait waiter.acquire() # ThreadID: 7700 File: "c:\src\extern\python\lib\threading.py", line 497, in __bootstrap self.__bootstrap_inner() File: "c:\src\extern\python\lib\threading.py", line 525, in __bootstrap_inner self.run() File: "c:\src\extern\python\lib\threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) File: "c:\src\extern\python\lib\subprocess.py", line 877, in _readerthread buffer.append(fh.read()) # ThreadID: 8020 out, err = proc.communicate("change: new\ndescription: %s \n"%changelistDesc) File: "c:\src\extern\python\lib\subprocess.py", line 689, in communicate return self._communicate(input) File: "c:\src\extern\python\lib\subprocess.py", line 903, in _communicate stdout_thread.join() File: "c:\src\extern\python\lib\threading.py", line 637, in join self.__block.wait() File: "c:\src\extern\python\lib\threading.py", line 237, in wait waiter.acquire() # ThreadID: 4252 File: "c:\src\extern\python\lib\threading.py", line 497, in __bootstrap self.__bootstrap_inner() File: "c:\src\extern\python\lib\threading.py", line 525, in __bootstrap_inner self.run() File: "c:\src\extern\python\lib\threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) File: "c:\src\extern\python\lib\subprocess.py", line 877, in _readerthread buffer.append(fh.read()) The StackTracer thread freezes trying to update my output file, and yes I'm trying to 3 tasks in parallel which each one starts by creating a changelist in perforce. This is just an easy repro case for me, it happens with commands other then p4. This almost looks like a threading issue more then the output deadlock. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Fri, 23 Sep 2011 06:59:12 +0100, Nobody wrote: >> kernel32.dll!WaitForSingleObject+0x12 >> python26.dll!_Py_svnversion+0xcf8 > > I haven't a clue how this happens. _Py_svnversion just returns a string: In retrospect, I think that's a red herring. 0xcf8 seems like too large an offset for such a small function. I think that it's more likely to be in a non-exported function, and _Py_svnversion just happens to be the last exported symbol prior to that point in the code. -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: Urwid 1.0.0 - Console UI Library
On 22/09/11 Ian Ward said: > Announcing Urwid 1.0.0 > -- Congrats. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Sep 23, 7:58 am, Atherun wrote: > On Sep 23, 12:08 am, Chris Angelico wrote: > > > On Fri, Sep 23, 2011 at 3:59 PM, Nobody wrote: > > > It doesn't even > > > You intrigue me, sir. Does it odd? > > > What is the remainder of this aborted sentence? > > > ChrisA > > That is odd, I also find it odd that it deadlocks the entire python > system, even threads that have nothing to do with the subprocess stop > working, the entire thing just stops. I may not have pasted the full > stack trace looking at it again, I'll double check in a few. > > I was able to repro this on a tool I have the source for, when I > attach to it to debug visual studio tells me the threads are > deadlocked and the only stack trace I have available to me is a low > level os call to a WriteFile function. Ya heres the full python stack: ntoskrnl.exe!memset+0x64a ntoskrnl.exe!KeWaitForMultipleObjects+0xd52 ntoskrnl.exe!KeWaitForMutexObject+0x19f ntoskrnl.exe!__misaligned_access+0xba4 ntoskrnl.exe!__misaligned_access+0x1821 ntoskrnl.exe!KeWaitForMultipleObjects+0xf5d ntoskrnl.exe!KeWaitForMutexObject+0x19f ntoskrnl.exe!NtWaitForSingleObject+0xde ntoskrnl.exe!KeSynchronizeExecution+0x3a43 wow64cpu.dll!TurboDispatchJumpAddressEnd+0x6c0 wow64cpu.dll!TurboDispatchJumpAddressEnd+0x4a8 wow64.dll!Wow64SystemServiceEx+0x1ce wow64.dll!Wow64LdrpInitialize+0x429 ntdll.dll!RtlUniform+0x6e6 ntdll.dll!RtlCreateTagHeap+0xa7 ntdll.dll!LdrInitializeThunk+0xe ntdll.dll!ZwWaitForSingleObject+0x15 kernel32.dll!WaitForSingleObjectEx+0x43 kernel32.dll!WaitForSingleObject+0x12 python26.dll!_Py_svnversion+0xcf8 python26.dll!PyObject_AsReadBuffer+0x46d python26.dll!PyEval_EvalCodeEx+0x738 python26.dll!PyEval_EvalFrameEx+0x467 python26.dll!PyObject_Realloc+0x90 python26.dll!PyEval_EvalCodeEx+0x8ef python26.dll!PyEval_EvalFrameEx+0x467 -- http://mail.python.org/mailman/listinfo/python-list
Re: python install on locked down windows box?
Hi Steve On 22/09/2011 13:58, Steven D'Aprano wrote: (7) If all else fails, as an absolute last resort, simply run the Windows installer as a regular, unprivileged user, after selecting the option for a Non-Admin Install under Advanced Options first. Thanks for this, will send on to my friend in need... Hadn't seen this before, but it's a long time since I've run the Windows installer ;-) Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
Python 2.5.6 (r256:88840, Sep 22 2011, 13:45:58) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import zlib Traceback (most recent call last): File "", line 1, in ImportError: No module named zlib >>> But if I run Python2.7 I get: Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import zlib >>> It seems to work. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Sep 23, 12:08 am, Chris Angelico wrote: > On Fri, Sep 23, 2011 at 3:59 PM, Nobody wrote: > > It doesn't even > > You intrigue me, sir. Does it odd? > > What is the remainder of this aborted sentence? > > ChrisA That is odd, I also find it odd that it deadlocks the entire python system, even threads that have nothing to do with the subprocess stop working, the entire thing just stops. I may not have pasted the full stack trace looking at it again, I'll double check in a few. I was able to repro this on a tool I have the source for, when I attach to it to debug visual studio tells me the threads are deadlocked and the only stack trace I have available to me is a low level os call to a WriteFile function. -- http://mail.python.org/mailman/listinfo/python-list
Re: pyWin build 216
On Fri, Sep 23, 2011 at 09:25, python wrote: > I have used pyWin for several years now with out issue. I recently > installed build 216 for python 2.7 on windows XP pro. The program > crashes every time I exit a wxPython program and has crashed a few > other times. I does not seem that pyWin has been updated since > February of this year. Is there a direction change for the windows > extensions? Is it time I make the move to 3.x? Mark Hammond has > given much to the Python community and I do not intend for this post > to be negative in any way. pywin32 has been available for 3.x for some time, but you wouldn't be able to use it since you're currently using wxPython. You may want to post a more detailed question to http://mail.python.org/mailman/listinfo/python-win32 -- Mark hangs out there and there are plenty of pywin32 experts around who could help as well. -- http://mail.python.org/mailman/listinfo/python-list
pyWin build 216
I have used pyWin for several years now with out issue. I recently installed build 216 for python 2.7 on windows XP pro. The program crashes every time I exit a wxPython program and has crashed a few other times. I does not seem that pyWin has been updated since February of this year. Is there a direction change for the windows extensions? Is it time I make the move to 3.x? Mark Hammond has given much to the Python community and I do not intend for this post to be negative in any way. -- http://mail.python.org/mailman/listinfo/python-list
Re: extending class
Andrea Crotti wrote: > I wanted to add a couple of parameters to a class from a given library > (paste-script), but without changing the original code. > So I thought, I create a wrapper class which adds what I need, and then > dispatch all the calls to the super class. You don't need to use a wrapper class if all you want is to add additional attributes to an instance. >>> class Spam(object): ... def __init__(self): ... self.x = 1 ... >>> s = Spam() >>> s.name = "Fred" >>> s.name 'Fred' > My following attempt gives, however, a recursion error, but why? Here is an old recipe showing how to do automatic delegation correctly: http://code.activestate.com/recipes/52295/ -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Could anybody tell me how to make a version of Nokia Symbian os
I like the Python in my mobile phone.But a don't have the enough money to buy a new phone.And my mobile type is Nokia N72 which is a part of S60v2.And I could program in some boring classes.But the version of my mobile's python is to old.So I hope I coule learn how to compile it for my N72.My notebook has the Windows 7 and Ubuntu GNU/Linux.Ask for help. -- http://mail.python.org/mailman/listinfo/python-list
Re: extending class
Jean-Michel Pichavant writes: > Did you consider subclassing your Var class ? This is how you extend a > class behavior in OOP. > > class PSIVar(var): >def __init__(self, name, desc, other=None, fun=None): >var.__init__(self, name, desc) >if other is not None: >self.other = other >self.fun = fun >assert callable(self.fun) > > v1 = PSIVar('name', 'desc') > > that's it. > > By the way, don't create instance attribute conditionally. Your PSIVar > instance should always have a 'other' attribute, its value can be None > though. > > > > JM Yes thanks, first I tried that, but I didn't want to have to change my class definition if the one from the library changes. So I started to mess with *args and **kwargs and it was really not beautiful ;) Anyway since I want to other and more deep changes I just forked the project and start work on that... -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behavior with imp.reload and logging
On 2011.09.22 03:12 AM, Chris Angelico wrote: > In theory, this should mean that you load it fresh every time - I > think. If not, manually deleting entries from sys.modules might help, > either with or without the list of modules. I've played around with sys.modules, and it seems there are issues with logging being reloaded (not sure if it's fair to call it a bug), not my module. The only reliable way I've found to start fresh is to delete at least logging from sys.modules (I can even do this from within my module). Even if the module that imports logging is deleted, things in logging persist (I can confirm at least handlers and handles to log files; the latter requiring garbage collection as well to free up). However, this would probably wreak havoc on other code that uses logging (although, I could always import logging as something else). On a side note, this actually gives me a good way to detect that the module is reloaded (I can't think of any other way to detect it, at least not off the top of my head). -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 6.0.2 -- http://mail.python.org/mailman/listinfo/python-list
Re: extending class
Andrea Crotti wrote: On 09/23/2011 10:31 AM, Peter Otten wrote: Inside __getattribute__() you ask for self.first_var which triggers another __getattribute__() call that once again trys to determine the first_var attribute before it returns... Try using __getattr__() instead which is only triggered for non-existent attributes def __getattr__(self, name): return getattr(self.first_var, name) or check for the attributes you don't want to delegate explicitly: def __getattribute__(self, name): if name == "first_var": return super(PSIVar, self).__getattribute__(name) Right thanks a lot it works perfectly. I don't like too much, however, to mess around in this way, maybe it's better if I just fork the project and patch the original code. In this way maybe I can also contribute to it with patches (if they are accepted)... Did you consider subclassing your Var class ? This is how you extend a class behavior in OOP. class PSIVar(var): def __init__(self, name, desc, other=None, fun=None): var.__init__(self, name, desc) if other is not None: self.other = other self.fun = fun assert callable(self.fun) v1 = PSIVar('name', 'desc') that's it. By the way, don't create instance attribute conditionally. Your PSIVar instance should always have a 'other' attribute, its value can be None though. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help with file encoding-decoding
On Sep 23, 2011, at 7:44 AM, Yaşar Arabacı wrote: > Hi, > > I'am trying to write a mass html downloader, and it processes files after it > downloaded them. I have problems with encodings, and decodings. Sometimes I > get UnicodeDecodeErrors, or > I get half-pages in after processing part. Or more generally, some things > don't feel right. Can you check my approach, and provide me some feedback > please? Here is what I am doing. > > 1) send a HEAD request to file's source to get file encoding, set encoding > variable accordingly. Hi Yaşar This is a pretty optimistic algorithm, at least by the statistics from 2008 (see below). > 2) if server doesn't provide an encoding, set encoding variable as utf-8 This is statistically a good guess but it doesn't follow the HTTP specification. > 4) in this step, I need to parse the content I get, because I will search > for further links \ >I feed content to parser (subclass of HTMLParser.HTMLParser) like Does HTMLParser.HTMLParser handle broken HTML? Because there's lots of it out there. I used to run an automated site validator, and I wrote a couple of articles you might find interesting. One is about how to get the encoding of a Web page: http://NikitaTheSpider.com/articles/EncodingDivination.html I also wrote an article examining the statistics I'd seen run through the crawler/validator. One thing I saw was that almost 2/3 of Web pages specified the encoding in the META HTTP-EQUIV Content-Type tag rather than in the HTTP Content-Type header. Mind you, this was three years ago so the character of the Web has likely changed since then, but probably not too dramatically. http://NikitaTheSpider.com/articles/ByTheNumbers/fall2008.html You can also do some straightforward debugging. Save the raw bytes you get from each site, and when you encounter a decode error, check the raw bytes. Are they really in the encoding specified? Webmasters make all kinds of mistakes. Hope this helps Philip > this -> content.decode(encoding) > 5) open a file in binary mod open(file_path,"wb") > 6) I write as I read without modifing. > > ## > # After processing part > ## > > (Note: encoding variable is same as the downloading part) > > 1) open local file in binary mod for reading file_name = > open(file_path,"rb") > 2) decode the file contents into a variable => decoded_content = > file_name.read().decode(encoding) > 3) send decoded content to a parser, parser contstruct new html content. (as > str) > 4) open same file for writing, in binary mod, write parsers output like > this: file_name.write(parser.output.encode(encoding)) > -- > http://yasar.serveblog.net/ > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: python install on locked down windows box?
Hi Matt, Enjoyed your list options :) I'm a consultant and have to do what your subject line asks at most clients I work at. Here's the technique I recommend: Install Python for the ***current user*** on another workstation with the appropriate priviledges. Then xcopy this Python folder to a USB drive. Then xcopy this folder from your USB drive to a matching folder on your locked down workstation. The xcopy-ed version of Python will run without problems when you start the python.exe executable from a command line with a python script as a command line parameter. The only thing you won't be able to do is click on .py* files and have them automatically invoke the Python interpreter because file associations require admin rights to update the registery. I don't consider this a big deal. Good luck! Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: [TIP] TestFixtures 2.0.0 Released!
On 23/09/2011 08:46, Chris Withers wrote: I'm happy to announce a new release major release of TestFixtures. This release is 99% backwards compatible, but a lot has changed under the hood and there's some major new functionality, so thought it was time for a bump. Of course, a 2.0.0 release wouldn't be complete without a fairly show-stopping lack of backwards compatibility... Thankfully, I've managed to return the case (comparison of generators with iterators) to its previous state with the 2.0.1 release I've just pushed out... cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Need help with file encoding-decoding
Hi, I'am trying to write a mass html downloader, and it processes files after it downloaded them. I have problems with encodings, and decodings. Sometimes I get UnicodeDecodeErrors, or I get half-pages in after processing part. Or more generally, some things don't feel right. Can you check my approach, and provide me some feedback please? Here is what I am doing. 1) send a HEAD request to file's source to get file encoding, set encoding variable accordingly. 2) if server doesn't provide an encoding, set encoding variable as utf-8 3) read html page from internet, read it to a variable let's say content. 4) in this step, I need to parse the content I get, because I will search for further links \ I feed content to parser (subclass of HTMLParser.HTMLParser) like this -> content.decode(encoding) 5) open a file in binary mod open(file_path,"wb") 6) I write as I read without modifing. ## # After processing part ## (Note: encoding variable is same as the downloading part) 1) open local file in binary mod for reading file_name = open(file_path,"rb") 2) decode the file contents into a variable => decoded_content = file_name.read().decode(encoding) 3) send decoded content to a parser, parser contstruct new html content. (as str) 4) open same file for writing, in binary mod, write parsers output like this: file_name.write(parser.output.encode(encoding)) -- http://yasar.serveblog.net/ -- http://mail.python.org/mailman/listinfo/python-list
Develop inter-dependent eggs
Develop inter-dependent eggs: On a Linux machine I have many eggs to develop, for example - egg1 - egg2 ... Now the eggs depend from each other, so running "python setup.py develop" in order, doesn't work, because if the dependency required is not already installed then easy_install tries to fetch it from PyPi. I looked around everywhere, but I can't find any feasible option, how do I tell easy_install to install the dependencies needed from a filesystem position and not from the PyPi server? -- http://mail.python.org/mailman/listinfo/python-list
cProfile and name spaces.
Hi I have following piece of code in file f1.py # f1.py starts here ### def f(): pass def main(): import profile profile.run('f()') if __name__ == '__main__': main() # -- end of f1.py executing f1.py works as expected. Now I have a file f2.py # f2.py starts here ### import f1 f1.main() # -- end of f2.py If I run f2.py I get the error message: . . . . > File "C:\Python26\lib\profile.py", line 70, in run > prof = prof.run(statement) > File "C:\Python26\lib\profile.py", line 456, in run > return self.runctx(cmd, dict, dict) > File "C:\Python26\lib\profile.py", line 462, in runctx > exec cmd in globals, locals > File "", line 1, in > NameError: name 'f' is not defined So cProfile doesn't find my function f any more. I can fix this by changing the code in f1.py to profile.run('f1.f()') However now I can't run f1.py anymore. Is it intentional, that cProfile always uses the name space of the initial module? I consider it surprising especially as I did not find any mentioning of this particularity in the documentation, which states: > cProfile.run(command[, filename]) > > This function takes a single argument that can be passed to the exec > statement, and an optional file name. In all cases this routine attempts > to exec its first argument, and gather profiling statistics from the > execution. If no file name is present, then this function automatically > prints a simple profiling report, sorted by the standard name string > (file/line/function-name) that is presented in each line. The following > is a typical output from such a call: I'm using python 2.6.5 The reason why I don't profile at the top level is, that I do not want to profile some parts of the code and as I want to conditionally profile a cetain function within different contexts / applications WI have also difficulties implementing something like this in a module, which is not the main module. arg1 = f(1) arg2 = f2() if do_profile: CProfile('result = function_name(arg1, arg2)', fname) else: result = function_name(arg1, arg2) Any tips / suggestions?? -- http://mail.python.org/mailman/listinfo/python-list
Re: PyEval_EvalCodeEx return value
On 23/09/11 00:47, Mark Hammond wrote: On 20/09/2011 8:34 PM, Mateusz Loskot wrote: I'm trying to dig out details about what exactly is the return value the of PyEval_EvalCodeEx function in Python 3.x The documentation is sparse, unfortunately. Perhaps I'm looking at wrong function. My aim is simple, I need to execute Python code using Python interpreter embedded in my C++ application. The Python code is a simple script that always returns single value. For example: #! /usr/bin/env python def foo(a, b): return a + b f = foo(2, 3) But, f can be of different type for different script: one returns numeric value, another returns a sequence, so the type is not possible to be determined in advance. I know how to capture Python stdout/stderr. I also know how to access the "f" attribute using PyObject_GetAttrString and then I can convert "f" value to C++ type depending on PyObject type. However, I guess there shall be a way to access "f" value directly from PyEval_EvalCode return object: PyObject* evalRet = ::PyEval_EvalCode(...); But, I can't find any details what the "evalRet" actually is. Eval is to eval an expression. If you simply eval the expression "f" in the context of the module you should get the result returned. Obviously though it is designed to eval more complex expressions and in your specific example, doing the getattr thing will also work fine. Hi Mark, So, the result of PyEval_EvalCode strictly depends on the code being evaluated. It makes sense. Thanks for help! Best regards, -- Mateusz Loskot, http://mateusz.loskot.net Charter Member of OSGeo, http://osgeo.org Member of ACCU, http://accu.org -- http://mail.python.org/mailman/listinfo/python-list
Re: extending class
On 09/23/2011 10:31 AM, Peter Otten wrote: Inside __getattribute__() you ask for self.first_var which triggers another __getattribute__() call that once again trys to determine the first_var attribute before it returns... Try using __getattr__() instead which is only triggered for non-existent attributes def __getattr__(self, name): return getattr(self.first_var, name) or check for the attributes you don't want to delegate explicitly: def __getattribute__(self, name): if name == "first_var": return super(PSIVar, self).__getattribute__(name) Right thanks a lot it works perfectly. I don't like too much, however, to mess around in this way, maybe it's better if I just fork the project and patch the original code. In this way maybe I can also contribute to it with patches (if they are accepted)... -- http://mail.python.org/mailman/listinfo/python-list
Re: extending class
Andrea Crotti wrote: > I wanted to add a couple of parameters to a class from a given library > (paste-script), but without changing the original code. > So I thought, I create a wrapper class which adds what I need, and then > dispatch all the calls to the super class. > > My following attempt gives, however, a recursion error, but why? Inside __getattribute__() you ask for self.first_var which triggers another __getattribute__() call that once again trys to determine the first_var attribute before it returns... Try using __getattr__() instead which is only triggered for non-existent attributes def __getattr__(self, name): return getattr(self.first_var, name) or check for the attributes you don't want to delegate explicitly: def __getattribute__(self, name): if name == "first_var": return super(PSIVar, self).__getattribute__(name) return getattr(self.first_var, name) > class PSIVar(object): > """Extend var implementation from the paste-script, to add the > ability of correlating variables > >>> v = var("name", "desc") > >>> v.name == 'name' > True > >>> v1 = PSIVar(v) > >>> v1.name == 'name' > True > """ > def __init__(self, first_var, other=None, fun=None): > # this is of type defined there > self.first_var = first_var > if other is not None: > self.other = other > self.fun = fun > assert callable(self.fun) > > # now try to dispatch every method call to the other class > # must probably call the super class > def __getattribute__(self, attr): > return self.first_var.__getattribute__(attr) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Mixins
Matt wrote: > I'm curious about what people's opinions are about using mixins in > Python. I really like, for example, the way that class based views > were implemented in Django 1.3 using mixins. It makes everything > extremely customizable and reusable. I think this is a very good > practice to follow, however, in Python mixins are achieved by using > (or perhaps misusing) inheritance and often multiple inheritance. > I think Mixins are great, in moderation, but wait until you have to debug code on an object with 70 base classes. Reinout van Rees wrote a very insightful article recently about Django's use of multiple inheritance: http://reinout.vanrees.org/weblog/2011/08/23/class-based-views.html He points out the problems that arise from overuse of mixins; why Zope went through so much upheaval to get away from mixins everywhere and switched to a component architecture instead; and suggests that Django will do the same in a few years time. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
extending class
I wanted to add a couple of parameters to a class from a given library (paste-script), but without changing the original code. So I thought, I create a wrapper class which adds what I need, and then dispatch all the calls to the super class. My following attempt gives, however, a recursion error, but why? class PSIVar(object): """Extend var implementation from the paste-script, to add the ability of correlating variables >>> v = var("name", "desc") >>> v.name == 'name' True >>> v1 = PSIVar(v) >>> v1.name == 'name' True """ def __init__(self, first_var, other=None, fun=None): # this is of type defined there self.first_var = first_var if other is not None: self.other = other self.fun = fun assert callable(self.fun) # now try to dispatch every method call to the other class # must probably call the super class def __getattribute__(self, attr): return self.first_var.__getattribute__(attr) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 zlib trouble
Jesramz wrote: > Hello, > > I am trying to deploy an app on google app engine using bottle, a > micro-framework, similar to flask. [...] > ImportError: No module named zlib What happens if you explicitly launch Python2.5 and then try to import zlib? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
TestFixtures 2.0.0 Released!
Hi All, I'm happy to announce a new release major release of TestFixtures. This release is 99% backwards compatible, but a lot has changed under the hood and there's some major new functionality, so thought it was time for a bump. The big changes are: - compare now uses a registry of comparers in the same way that unitest2's assertEquals does. You can register your own globally, register the default comparers for your own types and use a specific registry for a specific call to compare. - The handling of timezones has been reworked in `test_datetime` again. (did anyone ever mention that timezones are hard ;-) ) It feels more intuitive now, but it is backwards incompatible in the case where the `tzinfo` parameter to the `test_datetime` constructor was used. For details, read: http://packages.python.org/testfixtures/datetime.html#timezones The full list of changes can be found here: http://packages.python.org/testfixtures/changes.html The package is on PyPI and a full list of all the links to docs, issue trackers and the like can be found here: http://www.simplistix.co.uk/software/python/testfixtures cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: Python deadlock using subprocess.popen and communicate
On Fri, Sep 23, 2011 at 3:59 PM, Nobody wrote: > It doesn't even > You intrigue me, sir. Does it odd? What is the remainder of this aborted sentence? ChrisA -- http://mail.python.org/mailman/listinfo/python-list