title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,476,219
<p>If you can use GLib on all platforms, you should use <code>glib.io_add_watch</code>; then you can use a normal GLib mainloop and process events as they happen, without any polling behavior.</p> <p><a href="http://library.gnome.org/devel/pygobject/stable/glib-functions.html#function-glib--io-add-watch" rel="nofollow...
-2
2009-09-25T08:59:54Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,477,235
<p>You can see <a href="http://code.activestate.com/recipes/576377/" rel="nofollow">here</a> how to do a "tail -f" like using inotify:</p> <blockquote> <p>This is an exemple[sic] to show how to use the inotify module, it could be very usefull unchanged though.</p> <p>A Watcher instance let you define callback...
0
2009-09-25T13:17:30Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
8,316,493
<p>Most implementations I've seen use readlines() / sleep(). A solution based on inotify or similar <em>might</em> be faster but consider this:</p> <ul> <li>once libinotify tells you a file has changed you would end up using readlines() anyway</li> <li><p>calling readlines() against a file which hasn't changed, which ...
0
2011-11-29T19:23:49Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
13,691,289
<p>To minimize the sleep issues I modified Tzury Bar Yochay's solution and now it polls quickly if there is activity and after a few seconds of no activity it only polls every second.</p> <pre><code>import time def follow(thefile): thefile.seek(0,2) # Go to the end of the file sleep = 0.00001 while T...
10
2012-12-03T20:37:45Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
32,337,655
<p>The simplest <code>C</code> implementation of <code>tail -f</code> for Linux is this:</p> <pre><code>#include &lt;unistd.h&gt; #include &lt;sys/inotify.h&gt; int main() { int inotify_fd = inotify_init(); inotify_add_watch(inotify_fd, "/tmp/f", IN_MODIFY); struct inotify_event event; while (1) { ...
0
2015-09-01T17:31:34Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
38,863,712
<p>There's an awesome library called <a href="https://pypi.python.org/pypi/sh" rel="nofollow">sh</a> can tail a file with thread block.</p> <pre><code>for line in sh.tail('-f', '/you_file_path', _iter=True): print(line) </code></pre>
0
2016-08-10T03:23:52Z
[ "python" ]
Python: Referencing another project
1,476,111
<p>I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders. </p> <p>One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the fil...
4
2009-09-25T08:34:30Z
1,476,130
<p>If by "run modules" you mean importing them, you might be interested in <a href="http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python">this</a> question I asked a while ago.</p>
0
2009-09-25T08:40:00Z
[ "python", "command-line" ]
Python: Referencing another project
1,476,111
<p>I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders. </p> <p>One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the fil...
4
2009-09-25T08:34:30Z
1,476,132
<p>First, I make sure that the module I want to include hasn't been installed globally. Then I add a symlink within the includee's directory:</p> <pre><code># With pwd == module to which I want to add functionality. ln -s /path/to/some_other_module_to_include . </code></pre> <p>and then I can do a standard import. Th...
1
2009-09-25T08:40:04Z
[ "python", "command-line" ]
Python: Referencing another project
1,476,111
<p>I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders. </p> <p>One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the fil...
4
2009-09-25T08:34:30Z
1,476,134
<p>If you import sys, it contains a list of the directories in PYTHONPATH as sys.path</p> <p>Adding directories to this list (sys.path.append("my/path")) allows you to import from those locations in the current module as normal without changing the global settings on your system.</p>
10
2009-09-25T08:40:16Z
[ "python", "command-line" ]
Python: Referencing another project
1,476,111
<p>I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders. </p> <p>One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the fil...
4
2009-09-25T08:34:30Z
1,476,332
<p>Take a look at tools like</p> <ol> <li><p>virtualenv, to set up a virtual python, in which you can install your modules without getting them globally. <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">http://pypi.python.org/pypi/virtualenv</a></p></li> <li><p>Setuptools, which allows you to specify (a...
5
2009-09-25T09:24:41Z
[ "python", "command-line" ]
Python: Referencing another project
1,476,111
<p>I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders. </p> <p>One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the fil...
4
2009-09-25T08:34:30Z
1,476,333
<p>I just realised that I have actually solved this problem before. Here is the approach I used - much more complex than mavnn, but I was also solving the problem of running a Python2.x program from a Python 3.0</p> <pre><code>import os import subprocess env=os.environ.copy() env['PYTHONPATH']=my_libraries kwargs={"st...
0
2009-09-25T09:25:07Z
[ "python", "command-line" ]
How can I make images so that appengine doesn't make transparent into black on resize?
1,476,514
<p>I'm on the google appengine, and trying to resize images. I do :</p> <pre><code>from google.appengine.api import images image = images.resize(contents, w, h) </code></pre> <p>And for some images I get a nice transparent resize, and others I get a black background.</p> <p>How can I keep the transparency for all im...
2
2009-09-25T10:18:21Z
1,476,806
<p>Article on this problem: <a href="http://doesnotvalidate.com/2009/resizing-transparent-images-with-django-pil/" rel="nofollow">http://doesnotvalidate.com/2009/resizing-transparent-images-with-django-pil/</a> Google-code patch: <a href="http://code.google.com/p/sorl-thumbnail/issues/detail?id=56" rel="nofollow">http:...
0
2009-09-25T11:35:22Z
[ "python", "google-app-engine", "resize", "python-imaging-library" ]
How can I make images so that appengine doesn't make transparent into black on resize?
1,476,514
<p>I'm on the google appengine, and trying to resize images. I do :</p> <pre><code>from google.appengine.api import images image = images.resize(contents, w, h) </code></pre> <p>And for some images I get a nice transparent resize, and others I get a black background.</p> <p>How can I keep the transparency for all im...
2
2009-09-25T10:18:21Z
1,480,970
<p>Is this on the dev appserver, or in production? There's a known bug on the dev appserver that turns transparent to black when compositing, but it should run fine in production.</p>
0
2009-09-26T10:44:11Z
[ "python", "google-app-engine", "resize", "python-imaging-library" ]
How can I make images so that appengine doesn't make transparent into black on resize?
1,476,514
<p>I'm on the google appengine, and trying to resize images. I do :</p> <pre><code>from google.appengine.api import images image = images.resize(contents, w, h) </code></pre> <p>And for some images I get a nice transparent resize, and others I get a black background.</p> <p>How can I keep the transparency for all im...
2
2009-09-25T10:18:21Z
1,541,352
<p>With PIL you have to convert your image in RGBA like this :</p> <pre><code>im = im.convert("RGBA") </code></pre> <p>If you want a better implementation, you can read the sorl-thumbnail code. It makes a good usage of PIL.</p>
0
2009-10-09T01:04:50Z
[ "python", "google-app-engine", "resize", "python-imaging-library" ]
Python code seems to be getting executed out of order
1,476,722
<p>At work I have a programming language encoded in a database record. I'm trying to write a print function in python to display what the record contains. </p> <p>This is the code I'm having trouble with:</p> <pre><code> # Un-indent the block if necessary. if func_option[row.FRML_FUNC_OPTN] in ['Endif', 'Else'...
0
2009-09-25T11:12:39Z
1,476,779
<p>From your debug log:</p> <pre><code>row: Else row.FRML_FUNC_OPTN is : Elsedecrementing indent row.FRML_FUNC_OPTN is : Elseincrementing indent </code></pre> <p>I suspect you already have indentation before "Else" when you enter the code fragment supplied.</p> <p>Try adding:</p> <pre><code>rowtext = rowtext.st...
1
2009-09-25T11:28:19Z
[ "python", "debugging" ]
Python code seems to be getting executed out of order
1,476,722
<p>At work I have a programming language encoded in a database record. I'm trying to write a print function in python to display what the record contains. </p> <p>This is the code I'm having trouble with:</p> <pre><code> # Un-indent the block if necessary. if func_option[row.FRML_FUNC_OPTN] in ['Endif', 'Else'...
0
2009-09-25T11:12:39Z
1,481,005
<p>Just because it is a "script language" doesn't mean you have to live without a full debugger with breakpoints !</p> <ul> <li>Install eric3</li> <li>Load your code</li> <li>Press "debug" ;)</li> </ul> <p>Also, you seem new to Python, so here are a few hints :</p> <ul> <li>you can multiply strings, much faster than...
2
2009-09-26T11:12:53Z
[ "python", "debugging" ]
Writing python client for SOAP with suds
1,476,814
<p>I want to convert a perl SOAP client into a python SOAP client. The perl client is initialized like</p> <pre><code>$url = 'https://host:port/cgi-devel/Service.cgi'; $uri = 'https://host/Service'; my $soap = SOAP::Lite -&gt; uri($uri) -&gt; proxy($url); </code></pre> <p>I tried to replicate this in python...
1
2009-09-25T11:36:23Z
1,477,183
<p>You should ask this in the suds's mailing list. This library is under development, is open source, and the authors are very keen to get feedback from the users. </p> <p>Your code looks fine, this could be an error of the wsdl itself or of the suds library, therefore I encourage you to ask the author directly (after...
0
2009-09-25T13:06:35Z
[ "python", "soap", "https", "suds" ]
Writing python client for SOAP with suds
1,476,814
<p>I want to convert a perl SOAP client into a python SOAP client. The perl client is initialized like</p> <pre><code>$url = 'https://host:port/cgi-devel/Service.cgi'; $uri = 'https://host/Service'; my $soap = SOAP::Lite -&gt; uri($uri) -&gt; proxy($url); </code></pre> <p>I tried to replicate this in python...
1
2009-09-25T11:36:23Z
1,478,331
<p><code>urllib2</code> module doesn't add Content-Length (required for POST method) header automatically when Request object is constructed manually as <code>suds</code> does. You have to patch suds, probably <code>suds.transport.HttpTransport.open()</code> method or <code>suds.transport.Request</code> class.</p>
3
2009-09-25T16:37:34Z
[ "python", "soap", "https", "suds" ]
Writing python client for SOAP with suds
1,476,814
<p>I want to convert a perl SOAP client into a python SOAP client. The perl client is initialized like</p> <pre><code>$url = 'https://host:port/cgi-devel/Service.cgi'; $uri = 'https://host/Service'; my $soap = SOAP::Lite -&gt; uri($uri) -&gt; proxy($url); </code></pre> <p>I tried to replicate this in python...
1
2009-09-25T11:36:23Z
3,519,865
<p>I had the same error, then switched to using a local WSDL file, this worked:</p> <pre><code>import suds wsdl = 'file:///tmp/my.wsdl' client = suds.client.Client(wsdl, username='lbuser', password='lbpass', location='https://path.to.our.loadbalancer:9090/soap') </code></pre>
3
2010-08-19T08:11:29Z
[ "python", "soap", "https", "suds" ]
How to find which view is resolved from url in presence of decorators
1,476,996
<p>For debugging purposes, I'd like a quick way (e.g. in manage.py shell) of looking up which view that will be called as a result of a specific URL being requested.<br /> I know this is what django.core.urlresolvers.resolve does, but when having a decorator on the view function it will return that decorator.<br /> Exa...
3
2009-09-25T12:20:42Z
1,479,845
<p>This isn't my area of expertise, but it might help. </p> <p>You might be able to introspect <code>Allow</code> to find out which object it's decorating.</p> <pre><code>&gt;&gt;&gt;from django.core.urlresolvers import resolve &gt;&gt;&gt;func, args, kwargs=resolve('/edit_settings/') &gt;&gt;&gt;func Allow </code></...
1
2009-09-25T22:32:52Z
[ "python", "django", "django-urls" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
1,477,394
<p>According to your error message you have missing freetype headers. Can you locate them using system search functionalities. I will not lecture on using a pre-built package since I love scratching my head and compiling from the start as well.</p>
7
2009-09-25T13:49:12Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
1,477,402
<p>I just got it to compile. I added freetype2 in the include path for the CFLAGS in the make.osx file. Now the top of make.osx is:</p> <pre><code>PREFIX=/usr/local PYVERSION=2.6 PYTHON=python${PYVERSION} ZLIBVERSION=1.2.3 PNGVERSION=1.2.33 FREETYPEVERSION=2.3.5 MACOSX_DEPLOYMENT_TARGET=10.6 ## You shouldn't need ...
0
2009-09-25T13:51:05Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
1,515,518
<p>You should really ask this on the matplotlib-users mailing list. It's monitored by actual matplotlib developers, which StackOverflow (AFAIK) isn't.</p>
-1
2009-10-04T03:29:20Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
1,666,277
<p>You can also build by using</p> <pre><code>$ python setup.py build </code></pre> <p>with the following patch applied to setupext.py</p> <pre><code>Index: setupext.py =================================================================== --- setupext.py (revision 7917) +++ setupext.py (working copy) @@ -334,6 +334,8 ...
3
2009-11-03T09:41:05Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
1,958,071
<p>as suggested elsewhere, macports works fine on multiple architecture and versions of MacOsX + allows updates and more:</p> <pre><code>$ port search matplot py-matplotlib @0.99.0 (python, graphics, math) matlab-like syntax for creating plots in python py-matplotlib-basemap @0.99.4 (python, graphics, math) m...
0
2009-12-24T11:56:02Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
5,629,968
<p>For Python.org 2.7.1:</p> <p>I used a mix of the instructions. It basically worked by using the libpng in OSX's /usr/X11</p> <ol> <li><p>Downloaded, built and installed (make install) freetype2 v2.4.4 &amp; zlib v1.2.5. Did not use make deps.</p></li> <li><p>Modified setupext.py to have</p> <pre><code>module.inc...
1
2011-04-12T03:09:02Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
Compile Matplotlib for Python on Snow Leopard
1,477,144
<p>I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (<a href="http://blog.hyperjeff.net/?p=160">http://blog.hyperjeff.net/?p=160</a>) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not al...
20
2009-09-25T12:56:40Z
16,070,041
<p>This solution worked for me on OSX 10.8.3:</p> <pre><code>ln -s /usr/local/include/freetype2/freetype/ /usr/include/freetype </code></pre> <p>(Credit really goes to: <a href="http://simpleyuan.blogspot.com/2012/08/matplotlib-error-mac-os-x.html">http://simpleyuan.blogspot.com/2012/08/matplotlib-error-mac-os-x.html...
6
2013-04-17T20:51:33Z
[ "python", "osx-snow-leopard", "numpy", "compilation", "matplotlib" ]
How to use buildout to create localized version of my project?
1,477,189
<p>I am trying to create a localized version of my project.</p> <p>I started from the following:</p> <pre><code>mkdir my cd my wget http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py </code></pre> <p>After the last command I get the following message:</p> <blockquote> <p>Warning: wildcards no...
0
2009-09-25T13:07:11Z
1,685,473
<p>You need to have sqlite installed before you start installing the python bindings.</p>
0
2009-11-06T04:48:04Z
[ "python", "sqlite", "buildout" ]
How to use buildout to create localized version of my project?
1,477,189
<p>I am trying to create a localized version of my project.</p> <p>I started from the following:</p> <pre><code>mkdir my cd my wget http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py </code></pre> <p>After the last command I get the following message:</p> <blockquote> <p>Warning: wildcards no...
0
2009-09-25T13:07:11Z
3,753,087
<p>You need install sqlite develop library.</p> <p>In ubuntu or debian, run:</p> <pre><code>sudo apt-get install libsqlite3-dev </code></pre>
4
2010-09-20T15:43:48Z
[ "python", "sqlite", "buildout" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,477,356
<p>Since Unicode is just a range of - well - codes, what about using unichr() to get the unicode string corresponding to a random number between 0 and 0xFFFF?<br /> (Of course that would give just one codepoint, so iterate as required)</p>
0
2009-09-25T13:44:08Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,477,359
<p>You could download a website written in greek or german that uses unicode and feed that to your code.</p>
0
2009-09-25T13:45:22Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,477,375
<p>There is a <a href="http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt">UTF-8 stress test from Markus Kuhn</a> you could use.</p> <p>See also <a href="http://stackoverflow.com/questions/1319022/really-good-bad-utf-8-example-test-data">Really Good, Bad UTF-8 example test data</a>.</p>
7
2009-09-25T13:47:12Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,477,415
<p>It depends how thoroughly you want to do the testing and how accurately you want to do the generation. In full, Unicode is a 21-bit code set (U+0000 .. U+10FFFF). However, some quite large chunks of that range are set aside for custom characters. Do you want to worry about generating combining characters at the s...
3
2009-09-25T13:53:47Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,477,572
<p>Here is an example function that probably creates a random well-formed UTF-8 sequence, as defined in Table 3–7 of Unicode 5.0.0:</p> <pre><code>#!/usr/bin/env python3.1 # From Table 3–7 of the Unicode Standard 5.0.0 import random def byte_range(first, last): return list(range(first, last+1)) first_value...
6
2009-09-25T14:20:35Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
1,487,464
<p>Answering revised question:</p> <p>Yes, on a strict definition of "control characters" -- note that you won't include CR, LF, and TAB; is that what you want?</p> <p>Please consider responding to my earlier invitation to tell us what you are really trying to do.</p>
0
2009-09-28T14:47:37Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
21,666,621
<p>People may find their way here based mainly on the question title, so here's a way to generate a random string containing a variety of Unicode characters. To include more (or fewer) possible characters, just extend that part of the example with the code point ranges that you want.</p> <pre><code>import random def...
8
2014-02-09T23:34:33Z
[ "python", "unicode", "utf-8", "random" ]
Generate random UTF-8 string in Python
1,477,294
<p>I'd like to test the Unicode handling of my code. Is there anything I can put in random.choice() to select from the entire Unicode range, preferably not an external module? Neither Google nor StackOverflow seems to have an answer.</p> <p>Edit: It looks like this is more complex than expected, so I'll rephrase the q...
15
2009-09-25T13:29:53Z
39,682,429
<p>Follows a code that print any printable character of UTF-8:</p> <pre><code>print(''.join(tuple(chr(l) for l in range(1, 0x10ffff) if chr(l).isprintable()))) </code></pre> <p>All characters are present, even those that are not handled by the used font. <code>and not chr(l).isspace()</code> can b...
1
2016-09-25T00:57:43Z
[ "python", "unicode", "utf-8", "random" ]
How to find and run my old (global) version of Python?
1,477,300
<p>I have locally installed a newer version of Python. For that I did the following:</p> <pre><code>$ cd $ mkdir opt $ mkdir downloads $ cd downloads $ wget http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tgz $ tar xvzf Python-2.6.2.tgz $ cd Python-2.6.2 $ ./configure --prefix=$HOME/opt/ --enable-unicode=ucs4 $ ma...
0
2009-09-25T13:32:07Z
1,477,320
<p>Undo your changes to <code>.bash_profile</code> and execute <code>source .bash_profile</code>.</p>
0
2009-09-25T13:35:55Z
[ "python", "installer" ]
How to find and run my old (global) version of Python?
1,477,300
<p>I have locally installed a newer version of Python. For that I did the following:</p> <pre><code>$ cd $ mkdir opt $ mkdir downloads $ cd downloads $ wget http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tgz $ tar xvzf Python-2.6.2.tgz $ cd Python-2.6.2 $ ./configure --prefix=$HOME/opt/ --enable-unicode=ucs4 $ ma...
0
2009-09-25T13:32:07Z
1,477,325
<p>You can directly call the program with something like "/usr/local/bin/python myscript.py". You just need to know where your standard installation of python is. If you don't know, you can undo your changes and then type "which python" to find out what actually gets executed when you type "python" on the command line...
2
2009-09-25T13:36:31Z
[ "python", "installer" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,558
<p>Starting in Python 2.4, one can also use a <a href="http://www.python.org/dev/peps/pep-0318/" rel="nofollow">decorator</a> as in:</p> <pre><code> @staticmethod def mystaticethod(params) ... return </code></pre> <p>But I do not have any insight as to the genesis of this feature, as implemented orgi...
2
2009-09-25T14:18:00Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,598
<p>Static methods were added to Python long after classes were (classes were added very early on, possibly even before 1.0; static methods didn't show up until sometime about 2.0). They were implemented as a modification of normal methods &mdash; you create a static method object from a function to get a static method...
12
2009-09-25T14:24:47Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,599
<p>Perhaps, the design did not initially think static method is needed when function can be used. Since python didn't have data hiding so there is really no need static methods rather than using classes as name spaces.</p>
0
2009-09-25T14:24:51Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,633
<p>Guido writes the blog <a href="http://www.python-history.blogspot.com/" rel="nofollow">The History of Python</a>. I think there is a way to contact him with request to expand on this particular topic.</p>
1
2009-09-25T14:31:29Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,746
<p>Guido has always been wary of adding new constructs to the language. When static methods were proposed, it was showed that <a href="http://code.activestate.com/recipes/52304/" rel="nofollow">you could already do it</a> (there is a <a href="http://www.python.org/dev/peps/pep-0318/#id67" rel="nofollow"><code>staticmet...
3
2009-09-25T14:48:39Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,774
<p>Static methods in python date back to the introduction of the so-called “new-style classes” in Python 2.2. Previous to this, methods on classes were just ordinary functions, stored as attributes on the class:</p> <pre><code>class OldStyleClass: def method(self): print "'self' is just the first argu...
4
2009-09-25T14:54:12Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,477,897
<p>voyager and adurdin do a good job, between them, of explaining what happened: with the introduction of new-style classes and descriptors in Python 2.2, new and deep semantic possibilities arose -- and the most obviously useful examples (static methods, class methods, properties) were supported by built-in descriptor...
11
2009-09-25T15:13:08Z
[ "python", "syntax", "static", "language-history" ]
How was the syntax chosen for static methods in Python?
1,477,545
<p>I've been working with Python for a while and I find the syntax for declaring methods as static to be peculiar.</p> <p>A regular method would be declared:</p> <pre><code>def mymethod(self, params) ... return </code></pre> <p>A static method is declared:</p> <pre><code>def mystaticethod(params) ... re...
14
2009-09-25T14:14:31Z
1,478,229
<p>The static method situation in Python is a rather direct consequence of the design decisions of <a href="http://python-history.blogspot.com/2009/02/first-class-everything.html" rel="nofollow">first-class everything</a> and <a href="http://python-history.blogspot.com/2009/03/how-everything-became-executable.html" rel...
2
2009-09-25T16:12:03Z
[ "python", "syntax", "static", "language-history" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,477,610
<p>Executing untrusted code is dangerous, and should usually be avoided unless it's impossible to do so. I think you're right to be worried about the time of the run() method, but the run() method could do other things as well: delete all your files, open sockets and make network connections, begin cracking your passwo...
0
2009-09-25T14:27:09Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,477,684
<p>a quick google for "python timeout" reveals a <a href="http://nick.vargish.org/clues/python-tricks.html" rel="nofollow">TimeoutFunction</a> class</p>
0
2009-09-25T14:38:37Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,477,721
<p>You could use the <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">multiprocessing</a> library to run the code in a separate process, and call .join() on the process to wait for it to finish, with the timeout parameter set to whatever you want. The library provides several ways of getting...
2
2009-09-25T14:45:04Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,477,749
<p>You could also use Stackless Python, as it allows for <a href="http://www.stackless.com/wiki/Scheduling" rel="nofollow">cooperative scheduling</a> of microthreads. Here you can specify a maximum number of instructions to execute before returning. Setting up the routines and getting the return value out is a little m...
2
2009-09-25T14:49:15Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,477,796
<p>The only "really good" solutions -- imposing essentially no overhead -- are going to be based on SIGALRM, either directly or through a nice abstraction layer; but as already remarked Windows does not support this. Threads are no use, not because it's hard to get results out (that would be trivial, with a Queue!), bu...
3
2009-09-25T14:57:37Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
1,479,478
<blockquote> <p>I could spawn a new thread for this, and specify a time for .join() method, but then I cannot easily get the result out of it </p> </blockquote> <p>If the timeout expires, that means the method didn't finish, so there's no result to get. If you have incremental results, you can store them somewhere ...
1
2009-09-25T20:40:11Z
[ "python", "multithreading" ]
Python: time a method call and stop it if time is exceeded
1,477,569
<p>I need to dynamically load code (comes as source), run it and get the results. The code that I load always includes a run method, which returns the needed results. Everything looks ridiculously easy, as usual in Python, since I can do</p> <pre><code>exec(source) #source includes run() definition result = run(params...
3
2009-09-25T14:19:36Z
28,915,021
<p>Consider using the stopit package that could be useful in some cases you need timeout control. Its doc emphasizes the limitations.</p> <p><a href="https://pypi.python.org/pypi/stopit" rel="nofollow">https://pypi.python.org/pypi/stopit</a></p>
0
2015-03-07T12:45:48Z
[ "python", "multithreading" ]
Would python be an appropriate choice for a video library for home use software
1,477,626
<p>I am thinking of creating a video library software which keep track of all my videos and keep track of videos that I already haven't watched and stats like this. The stats will be specific to each user using the software.</p> <p>My question is, is python appropriate to create this software or do I need something li...
0
2009-09-25T14:28:57Z
1,477,733
<p>Yes. Python is much easier to use than c++ for something like this. You may want to use it as a front-end to a DB such as sqlite3 </p>
1
2009-09-25T14:46:54Z
[ "python", "video", "video-library" ]
Would python be an appropriate choice for a video library for home use software
1,477,626
<p>I am thinking of creating a video library software which keep track of all my videos and keep track of videos that I already haven't watched and stats like this. The stats will be specific to each user using the software.</p> <p>My question is, is python appropriate to create this software or do I need something li...
0
2009-09-25T14:28:57Z
1,477,754
<p>Python is perfectly appropriate for such tasks - indeed the most popular video site, YouTube, is essentially programmed in Python (using, of course, lower-level components <em>called</em> from Python for such tasks as web serving, relational db, video transcoding -- there are plenty of such reusable opensource compo...
6
2009-09-25T14:49:39Z
[ "python", "video", "video-library" ]
Would python be an appropriate choice for a video library for home use software
1,477,626
<p>I am thinking of creating a video library software which keep track of all my videos and keep track of videos that I already haven't watched and stats like this. The stats will be specific to each user using the software.</p> <p>My question is, is python appropriate to create this software or do I need something li...
0
2009-09-25T14:28:57Z
1,477,782
<p>Maybe you should take a look at this project: <a href="http://www.moovida.com/" rel="nofollow">Moovida</a></p> <p>It's a complete media center, open source, written in python that is easy to extend. I don't know if it will do exactly what you want out of the box but you can probably easily add the features you want...
1
2009-09-25T14:55:18Z
[ "python", "video", "video-library" ]
Would python be an appropriate choice for a video library for home use software
1,477,626
<p>I am thinking of creating a video library software which keep track of all my videos and keep track of videos that I already haven't watched and stats like this. The stats will be specific to each user using the software.</p> <p>My question is, is python appropriate to create this software or do I need something li...
0
2009-09-25T14:28:57Z
1,478,325
<p>Of course you can use almost any programming language for almost any task. But after noting that, it's also obvious that different languages are also differently well adapted for different tasks.</p> <p>C/C++ are languages that are very "hardware friendly". Basically, the languages are just one abstraction level ab...
1
2009-09-25T16:36:38Z
[ "python", "video", "video-library" ]
Would python be an appropriate choice for a video library for home use software
1,477,626
<p>I am thinking of creating a video library software which keep track of all my videos and keep track of videos that I already haven't watched and stats like this. The stats will be specific to each user using the software.</p> <p>My question is, is python appropriate to create this software or do I need something li...
0
2009-09-25T14:28:57Z
1,481,018
<p>If you want your code to be REAL FAST, use C++ (or parallel fortran).</p> <p>However in your application, 99% of the runtime isn't going to be in YOUR code, it's going to be in GUI libraries, OS calls, waiting for user interaction, calling libraries (written in C) to open video files and make thumbnails, that kind ...
1
2009-09-26T11:22:37Z
[ "python", "video", "video-library" ]
Python chat : delete variables to clean memory in functions?
1,477,980
<p>I'm creating a chat daemon in python and twisted framework. And I'm wondering if I have to delete every variable create in my functions to save memory in the long run when multiple users are connected, or are those variable automatically clear?. Here's a strip down version of my code to illustrate my point:</p> <...
11
2009-09-25T15:26:38Z
1,478,002
<p>Python uses garbage collection. This means you don't have to care about memory as it's freed automatically when it's not used anymore.</p>
4
2009-09-25T15:28:37Z
[ "python", "class", "function", "twisted" ]
Python chat : delete variables to clean memory in functions?
1,477,980
<p>I'm creating a chat daemon in python and twisted framework. And I'm wondering if I have to delete every variable create in my functions to save memory in the long run when multiple users are connected, or are those variable automatically clear?. Here's a strip down version of my code to illustrate my point:</p> <...
11
2009-09-25T15:26:38Z
1,478,088
<p>Python objects are never explicitly deleted. The only way to truly reclaim memory from unreferenced Python objects is via the garbage collector. The <code>del</code> keyword simply unbinds a name from an object, but the object still needs to be garbage collected. </p> <p>If you really think you have to, you can fo...
7
2009-09-25T15:44:19Z
[ "python", "class", "function", "twisted" ]
Python chat : delete variables to clean memory in functions?
1,477,980
<p>I'm creating a chat daemon in python and twisted framework. And I'm wondering if I have to delete every variable create in my functions to save memory in the long run when multiple users are connected, or are those variable automatically clear?. Here's a strip down version of my code to illustrate my point:</p> <...
11
2009-09-25T15:26:38Z
1,478,134
<p>C Python (the reference implementation) uses reference counting and garbage collection. When count of references to object decrease to 0, it is automatically reclaimed. The garbage collection normally reclaims only those objects that refer to each other (or other objects from them) and thus cannot be reclaimed by re...
16
2009-09-25T15:52:17Z
[ "python", "class", "function", "twisted" ]
Python type inference for autocompletion
1,478,044
<p>Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python?</p> <p>The idea is to propose autocompletion for example in the following cases:</p> <pre><code>class A: def m1(self): pass def m2(self): pass a = A() a. &lt;--- suggest here 'm1' and 'm2'...
10
2009-09-25T15:34:35Z
1,478,067
<p>Excellent discussion, with many pointers, <a href="http://lambda-the-ultimate.org/node/1519" rel="nofollow">here</a> (a bit dated). I don't believe any "production" editors aggressively try type-inferencing for autocomplete purposes (but I haven't used e.g. wingware's in a while, so maybe they do now).</p>
9
2009-09-25T15:40:04Z
[ "python", "algorithm", "ide", "haskell", "ocaml" ]
Python type inference for autocompletion
1,478,044
<p>Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python?</p> <p>The idea is to propose autocompletion for example in the following cases:</p> <pre><code>class A: def m1(self): pass def m2(self): pass a = A() a. &lt;--- suggest here 'm1' and 'm2'...
10
2009-09-25T15:34:35Z
1,478,564
<p>A proper treatment would require a type system for Python, which would be (is?) an interesting research problem.</p>
0
2009-09-25T17:20:11Z
[ "python", "algorithm", "ide", "haskell", "ocaml" ]
Python type inference for autocompletion
1,478,044
<p>Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python?</p> <p>The idea is to propose autocompletion for example in the following cases:</p> <pre><code>class A: def m1(self): pass def m2(self): pass a = A() a. &lt;--- suggest here 'm1' and 'm2'...
10
2009-09-25T15:34:35Z
1,495,346
<p>You could have a look at ECompletion and OCompletion in <a href="http://www.pharo-project.org" rel="nofollow">Pharo Smalltalk</a>. The compromises will probably different for python, but educated guesses with some conservative type inference work in practice. It also depends if you want completion to replace browsin...
2
2009-09-29T22:45:08Z
[ "python", "algorithm", "ide", "haskell", "ocaml" ]
Python type inference for autocompletion
1,478,044
<p>Is it possible to use Ocaml/Haskell algorithm of type inference to suggest better autocompletions for Python?</p> <p>The idea is to propose autocompletion for example in the following cases:</p> <pre><code>class A: def m1(self): pass def m2(self): pass a = A() a. &lt;--- suggest here 'm1' and 'm2'...
10
2009-09-25T15:34:35Z
5,396,816
<p>The first case is "easy", and I bet <a href="http://www.jetbrains.com/pycharm/" rel="nofollow">JetBrains' PyCharm</a> can do that. I've not used PyCharm, but I do use IDEA, also by JetBrains, for Groovy-based development (a dynamic language), and it has very good, very aggressive auto-completion. The second case wou...
1
2011-03-22T19:38:00Z
[ "python", "algorithm", "ide", "haskell", "ocaml" ]
Django autoreload for development on every request?
1,478,061
<p>Can a Django app be reloaded on <strong>every</strong> request ?</p> <p>This is very useful for development. <a href="http://en.wikipedia.org/wiki/Ruby%5Fon%5FRails" rel="nofollow">Ruby on Rails</a> does just this.</p> <ol> <li>runserver reloads, but it reloads slow, and still sometime one has to stop and start ...
0
2009-09-25T15:38:47Z
1,480,405
<p>Of course it is going to be slow to reload, it has to load all the application code again and not just the one file. Django is not PHP, so don't expect it to work the same.</p> <p>If you really want Django to reload on every request regardless, then use CGI and a CGI/WSGI bridge. It is still going to be slow though...
2
2009-09-26T03:58:32Z
[ "python", "django" ]
To do RegEx, what are the advantages/disadvantages to use UTF-8 string instead of unicode?
1,478,178
<p>Usually, the best practice in python, when using international languages, is to use unicode and to convert early any input to unicode and to convert late to a string encoding (UTF-8 most of the times).</p> <p>But when I need to do RegEx on unicode I don't find the process really friendly. For example, if I need to ...
0
2009-09-25T16:00:02Z
1,478,269
<p>If you're using utf-8 in your python source, you can just write:</p> <pre><code>u'Élisa' </code></pre> <p>and that would be a unicode string, equivalent to writing:</p> <pre><code>u'\xc9lisa' </code></pre> <p>So the 'u' prefix makes the decode thing unneeded. If you leave out the 'u' and write:</p> <pre><code>...
3
2009-09-25T16:24:47Z
[ "python", "regex", "unicode", "utf-8" ]
To do RegEx, what are the advantages/disadvantages to use UTF-8 string instead of unicode?
1,478,178
<p>Usually, the best practice in python, when using international languages, is to use unicode and to convert early any input to unicode and to convert late to a string encoding (UTF-8 most of the times).</p> <p>But when I need to do RegEx on unicode I don't find the process really friendly. For example, if I need to ...
0
2009-09-25T16:00:02Z
1,478,328
<p>You're using Python 2.x? If so, it's generally considered rather bad form to leave your non-ASCII characters in byte strings. Just use Unicode strings the whole way through:</p> <pre><code>re.match(u'Élisa™\\s+', unicodestring) </code></pre> <p>It may look a bit funny writing ‘u’ at the start of your string...
3
2009-09-25T16:37:22Z
[ "python", "regex", "unicode", "utf-8" ]
What's the life-time of a thread-local value in Python?
1,478,248
<pre><code>import threading mydata = threading.local() def run(): # When will the garbage collector be able to destroy the object created # here? After the thread exits from ``run()``? After ``join()`` is called? # Or will it survive the thread in which it was created, and live until # ``mydata`` is g...
6
2009-09-25T16:16:08Z
1,478,266
<p>By making a couple simple changes to your program and forcing a garbage collection after each step of the threading, it seems that <code>foo</code> cannot be collected until the program is finished - in other words, <em>after</em> the thread goes out of scope.</p> <pre><code>import threading import gc mydata = thr...
0
2009-09-25T16:23:48Z
[ "python", "multithreading" ]
What's the life-time of a thread-local value in Python?
1,478,248
<pre><code>import threading mydata = threading.local() def run(): # When will the garbage collector be able to destroy the object created # here? After the thread exits from ``run()``? After ``join()`` is called? # Or will it survive the thread in which it was created, and live until # ``mydata`` is g...
6
2009-09-25T16:16:08Z
1,478,643
<p>Mark had it almost right -- essentially "mydata" will hold references to all the TL variables in it, whatever thread they were created from. To wit...:</p> <pre><code>import threading import gc mydata = threading.local() class x: def __del__(self): print "x got deleted!" def run(): mydata.foo = x...
3
2009-09-25T17:37:03Z
[ "python", "multithreading" ]
What's the life-time of a thread-local value in Python?
1,478,248
<pre><code>import threading mydata = threading.local() def run(): # When will the garbage collector be able to destroy the object created # here? After the thread exits from ``run()``? After ``join()`` is called? # Or will it survive the thread in which it was created, and live until # ``mydata`` is g...
6
2009-09-25T16:16:08Z
1,478,755
<p>Thanks! It seems that Mark's program behaves differently under CPython 2.5 and 2.6:</p> <pre><code>import threading import gc import platform print "Python %s (%s)" % (platform.python_version(), " ".join(platform.python_build())) mydata = threading.local() class x: def __del__(self): print "x got del...
1
2009-09-25T18:00:16Z
[ "python", "multithreading" ]
What's the life-time of a thread-local value in Python?
1,478,248
<pre><code>import threading mydata = threading.local() def run(): # When will the garbage collector be able to destroy the object created # here? After the thread exits from ``run()``? After ``join()`` is called? # Or will it survive the thread in which it was created, and live until # ``mydata`` is g...
6
2009-09-25T16:16:08Z
2,878,927
<p>Here is my answer, since I am failing to see the conclusion in the previous answers.</p> <p>I started wondering the same thing and tried a test program that is similar to the ones in other answers and my conclusion was that they do get GCed sooner than the end of the program, which means, these references can be de...
3
2010-05-21T00:57:08Z
[ "python", "multithreading" ]
Build failure during install py25-gtk on Mac OS X 10.6 using MacPorts 1.8
1,478,263
<p>When I do this command :</p> <pre><code>sudo port clean py25-gtk sudo port install py25-gtk </code></pre> <p>I get this error :</p> <pre><code>---&gt; Computing dependencies for py25-gtk ---&gt; Building getopt Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_loc...
1
2009-09-25T16:21:53Z
1,479,307
<p>The solution is to reinstall all ports because I upgraded to a new OS version (10.5 -> 10.6).</p> <p>To reinstall your ports, save the list of your installed ports:</p> <pre><code>port installed &gt; myports.txt </code></pre> <p>Clean any partially completed builds, and uninstall all installed ports:</p> <pre><c...
1
2009-09-25T20:03:03Z
[ "python", "osx", "gtk", "macports", "getopt" ]
How to import bookmarks from users web browsers using python?
1,478,375
<p>I am working on an RSS Reader type program and I would like it to be able to automatically import RSS feeds from the users browser bookmarks. I assume different browsers use different methods to store bookmarks. Is there any library out there just for this purpose? </p> <p>I only need it to work on Linux so I don'...
1
2009-09-25T16:45:08Z
1,478,419
<p>Take a look at: <a href="http://pyxml.sourceforge.net/topics/xbel/" rel="nofollow">XBEL</a></p>
1
2009-09-25T16:54:08Z
[ "python", "linux", "browser", "xbel" ]
Why my python does not see pysqlite?
1,478,479
<p>I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem...
1
2009-09-25T17:04:40Z
1,478,563
<p>On <strong>Windows</strong>, <code>_sqlite3.pyd</code> resides in <code>C:\Python26\DLLs</code>. On <strong>*nix</strong>, it should be under a path similar to <code>/usr/lib/python2.6/lib-dynload/_sqlite3.so</code>. Chances are that either you are missing that shared library or your <code>PYTHONPATH</code> is set...
1
2009-09-25T17:20:04Z
[ "python", "sqlite", "sqlite3", "pysqlite" ]
Why my python does not see pysqlite?
1,478,479
<p>I would like to have an interface between Python and sqlite. Both are installed on the machine. I had an old version of Python (2.4.3). So, pysqlite was not included by default. First, I tried to solve this problem by installing pysqlite but I did not succeed in this direction. My second attempt to solve the problem...
1
2009-09-25T17:04:40Z
1,478,689
<p>You have a "slite3.py" (actually its equivalent for a package, <code>sqlite3/__init__.py</code>, so <code>import sqlite3</code> per se is fine, BUT that module in turns tries to <code>import _sqlite3</code> and fails, so it's not finding <code>_sqlite3.so</code>. It should be in <code>python2.6/lib-dynload</code> un...
1
2009-09-25T17:45:47Z
[ "python", "sqlite", "sqlite3", "pysqlite" ]
for line in open(filename)
1,478,697
<p>I frequently see python code similar to </p> <pre><code>for line in open(filename): do_something(line) </code></pre> <p>When does filename get closed with this code?</p> <p>Would it be better to write</p> <pre><code>with open(filename) as f: for line in f.readlines(): do_something(line) </code></...
17
2009-09-25T17:47:07Z
1,478,711
<p>The <code>with</code> part is better because it close the file afterwards. You don't even have to use <code>readlines()</code>. <code>for line in file</code> is enough.</p> <p>I don't think the first one closes it.</p>
8
2009-09-25T17:50:45Z
[ "python", "file", "garbage-collection" ]
for line in open(filename)
1,478,697
<p>I frequently see python code similar to </p> <pre><code>for line in open(filename): do_something(line) </code></pre> <p>When does filename get closed with this code?</p> <p>Would it be better to write</p> <pre><code>with open(filename) as f: for line in f.readlines(): do_something(line) </code></...
17
2009-09-25T17:47:07Z
1,478,712
<p><code>filename</code> would be closed when it falls out of scope. That normally would be the end of the method.</p> <p>Yes, it's better to use <code>with</code>.</p> <blockquote> <p>Once you have a file object, you perform all file I/O by calling methods of this object. [...] When you are done with the file, you...
27
2009-09-25T17:50:52Z
[ "python", "file", "garbage-collection" ]
for line in open(filename)
1,478,697
<p>I frequently see python code similar to </p> <pre><code>for line in open(filename): do_something(line) </code></pre> <p>When does filename get closed with this code?</p> <p>Would it be better to write</p> <pre><code>with open(filename) as f: for line in f.readlines(): do_something(line) </code></...
17
2009-09-25T17:47:07Z
1,478,736
<p>python is garbage-collected - cpython has reference counting and a backup cycle detecting garbage collector.</p> <p>File objects close their file handle when the are deleted/finalized. </p> <p>Thus the file will be eventually closed, and in cpython will closed as soon as the for loop finishes. </p>
2
2009-09-25T17:56:47Z
[ "python", "file", "garbage-collection" ]
for line in open(filename)
1,478,697
<p>I frequently see python code similar to </p> <pre><code>for line in open(filename): do_something(line) </code></pre> <p>When does filename get closed with this code?</p> <p>Would it be better to write</p> <pre><code>with open(filename) as f: for line in f.readlines(): do_something(line) </code></...
17
2009-09-25T17:47:07Z
1,479,095
<p>Drop <code>.readlines()</code>. It is redundant and undesirable for large files (due to memory consumption). The variant with <code>'with'</code> block always closes file. </p> <pre><code>with open(filename) as file_: for line in file_: do_something(line) </code></pre> <p>When file will be closed in th...
3
2009-09-25T19:18:21Z
[ "python", "file", "garbage-collection" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,478,912
<pre><code>&gt;&gt;&gt; for s in ["", "0", "-0-0", "1-2-3-4"]: ... print(map(int, filter(None, s.split('-')))) ... [] [0] [0, 0] [1, 2, 3, 4] </code></pre>
8
2009-09-25T18:34:29Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,478,917
<p>You can use a list comprehension to make it shorter. Use the <code>if</code> to account for the empty string.</p> <pre><code>the_string = '1-2-3-4' [int(x) for x in the_string.split('-') if x != ''] </code></pre>
12
2009-09-25T18:35:17Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,478,918
<p>Convert the higher-order functions to a more readable list-comprehension</p> <pre><code>[ int(n) for n in "1-2-3-4".split('-') if n != '' ] </code></pre> <p>The rest is fine.</p>
4
2009-09-25T18:35:18Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,478,952
<p>From the format of your example, you want int's in the list. If so, then you will need to convert the string numbers to int's. If not, then you are done after the string split.</p> <pre><code>text="1-2-3-4" numlist=[int(ith) for ith in text.split('-')] print numlist [1, 2, 3, 4] textlist=text.split('-') print t...
2
2009-09-25T18:42:07Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,479,086
<pre><code>def convert(s): if s: return map(int, s.split("-")) else: return [] </code></pre>
0
2009-09-25T19:16:54Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,479,131
<p>you don't need the lambda, and split won't give you empty elements:</p> <pre><code>map(int, filter(None,x.split("-"))) </code></pre>
0
2009-09-25T19:24:28Z
[ "python" ]
Python: "1-2-3-4" to [1, 2, 3, 4]
1,478,908
<p>What is the best way to convert a string on the format <code>"1-2-3-4"</code> to a list <code>[1, 2, 3, 4]</code>? The string may also be empty, in which case the conversion should return an empty list <code>[]</code>.</p> <p>This is what I have:</p> <pre><code>map(lambda x: int(x), filter(lambda x: x != '', ...
2
2009-09-25T18:33:09Z
1,479,249
<p>I'd go with this:</p> <pre><code>&gt;&gt;&gt; the_string = '1-2-3-4- -5- 6-' &gt;&gt;&gt; &gt;&gt;&gt; [int(x.strip()) for x in the_string.split('-') if len(x)] [1, 2, 3, 4, 5, 6] </code></pre>
1
2009-09-25T19:49:03Z
[ "python" ]
Python: undo write to file
1,479,035
<p>What is the best way to undo the writing to a file? If I'm going through a loop and writing one line at a time, and I want to undo the previous write and replace it with something else, how do I go about doing that? Any ideas?</p> <p>Thanks in advance!</p>
2
2009-09-25T19:02:55Z
1,479,049
<p>Try to write to your files <em>lazily</em>: Don't write until you are finally certain you need to do it.</p>
4
2009-09-25T19:06:57Z
[ "python", "file", "undo" ]
Python: undo write to file
1,479,035
<p>What is the best way to undo the writing to a file? If I'm going through a loop and writing one line at a time, and I want to undo the previous write and replace it with something else, how do I go about doing that? Any ideas?</p> <p>Thanks in advance!</p>
2
2009-09-25T19:02:55Z
1,479,058
<p>If you keep track of the line numbers you can use something like this:</p> <pre><code>from itertools import islice def seek_to_line(f, n): for ignored_line in islice(f, n - 1): pass # skip n-1 lines f = open('foo') seek_to_line(f, 9000) # seek to line 9000 # print lines 9000 and later fo...
0
2009-09-25T19:09:20Z
[ "python", "file", "undo" ]
Python: undo write to file
1,479,035
<p>What is the best way to undo the writing to a file? If I'm going through a loop and writing one line at a time, and I want to undo the previous write and replace it with something else, how do I go about doing that? Any ideas?</p> <p>Thanks in advance!</p>
2
2009-09-25T19:02:55Z
1,479,070
<p>Perhaps a better thing to do would be to modify your program so that it will only write a line if you are sure that you want to write it. To do that your code would look something like:</p> <pre><code>to_write = "" for item in alist: #Check to make sure that I want to write f.write(to_write) to_write = "" ...
0
2009-09-25T19:11:08Z
[ "python", "file", "undo" ]
Python: undo write to file
1,479,035
<p>What is the best way to undo the writing to a file? If I'm going through a loop and writing one line at a time, and I want to undo the previous write and replace it with something else, how do I go about doing that? Any ideas?</p> <p>Thanks in advance!</p>
2
2009-09-25T19:02:55Z
1,479,220
<p>as others have noted, this doesn't make much sense, it's <em>far</em> better not to write until you have to. in your case, you can keep the 'writing pointer' one line behind your processing.</p> <p>pseudocode:</p> <pre><code>previousItem = INVALID for each item I: is I same as previousItem? then update prev...
5
2009-09-25T19:41:36Z
[ "python", "file", "undo" ]
Python: undo write to file
1,479,035
<p>What is the best way to undo the writing to a file? If I'm going through a loop and writing one line at a time, and I want to undo the previous write and replace it with something else, how do I go about doing that? Any ideas?</p> <p>Thanks in advance!</p>
2
2009-09-25T19:02:55Z
1,479,321
<p>As mentioned, you're best off not trying to undo writes. If you really want to do it, though, it's easy enough:</p> <pre><code>import os f = open("test.txt", "w+") f.write("testing 1\n") f.write("testing 2\n") pos = f.tell() f.write("testing 3\n") f.seek(pos, os.SEEK_SET) f.truncate(pos) f.write("foo\n") </code><...
3
2009-09-25T20:05:55Z
[ "python", "file", "undo" ]
How to build from the source?
1,479,265
<p>I cannot use sqlite3 (build python package). The reason of the is missing _sqlite3.so. I found that peoples had the same problem and they resolved it <a href="http://www.linuxquestions.org/questions/slackware-14/no-sqlite3.so-in-usrlibpython2.5lib-dynload-599027/" rel="nofollow">here</a>.</p> <p>The solutions is gi...
1
2009-09-25T19:51:32Z
1,479,335
<p>Download the SQLite source here: <a href="http://www.sqlite.org/download.html" rel="nofollow">SQLite Download Page</a></p> <p>Extract the tarball somewhere on your machine.</p> <p>Navigate to the expanded directory.</p> <p>Run:</p> <pre><code>./configure make make install (sudo make install if you have permissio...
3
2009-09-25T20:09:14Z
[ "python", "build", "sqlite3" ]
How to build from the source?
1,479,265
<p>I cannot use sqlite3 (build python package). The reason of the is missing _sqlite3.so. I found that peoples had the same problem and they resolved it <a href="http://www.linuxquestions.org/questions/slackware-14/no-sqlite3.so-in-usrlibpython2.5lib-dynload-599027/" rel="nofollow">here</a>.</p> <p>The solutions is gi...
1
2009-09-25T19:51:32Z
1,479,352
<p>In this posting, the poster was building Python from source, as the Slackware package he tried to install was apparently broken (Slackware being a Linux distribution).</p> <p>Unless you also use the same Slackware release, I would claim that your problem is <em>not</em> the same.</p>
0
2009-09-25T20:12:14Z
[ "python", "build", "sqlite3" ]
IronPython - How to prevent CLR (and other modules) from being imported
1,479,454
<p>I'm setting up a web application to use IronPython for scripting various user actions and I'll be exposing various business objects ready for accessing by the script. I want to make it impossible for the user to import the CLR or other assemblies in order to keep the script's capabilities simple and restricted to th...
3
2009-09-25T20:33:16Z
1,479,480
<p>You'll have to search the script for the imports you don't want them to use, and reject the script in toto if it contains any of them.</p> <p>Basically, just reject the script if it contains Assembly.Load, import or AddReference.</p>
1
2009-09-25T20:40:37Z
[ "python", "ironpython" ]
IronPython - How to prevent CLR (and other modules) from being imported
1,479,454
<p>I'm setting up a web application to use IronPython for scripting various user actions and I'll be exposing various business objects ready for accessing by the script. I want to make it impossible for the user to import the CLR or other assemblies in order to keep the script's capabilities simple and restricted to th...
3
2009-09-25T20:33:16Z
1,480,581
<p>If you'd like to disable certain built-in modules I'd suggest filing a feature request over at ironpython.codeplex.com. This should be an easy enough thing to implement.</p> <p>Otherwise you could simply look at either Importer.cs and disallow the import there or you could simply delete ClrModule.cs from IronPytho...
0
2009-09-26T06:30:24Z
[ "python", "ironpython" ]
IronPython - How to prevent CLR (and other modules) from being imported
1,479,454
<p>I'm setting up a web application to use IronPython for scripting various user actions and I'll be exposing various business objects ready for accessing by the script. I want to make it impossible for the user to import the CLR or other assemblies in order to keep the script's capabilities simple and restricted to th...
3
2009-09-25T20:33:16Z
1,481,615
<p>You might want to implement the protection using <a href="http://msdn.microsoft.com/en-us/library/930b76w0%28VS.80%29.aspx" rel="nofollow">Microsoft's Code Access Security</a>. I myself am not fully aware of its workings (or how to make it work with IPy), but its something which I feel you should consider. </p> <p>...
1
2009-09-26T16:46:15Z
[ "python", "ironpython" ]
IronPython - How to prevent CLR (and other modules) from being imported
1,479,454
<p>I'm setting up a web application to use IronPython for scripting various user actions and I'll be exposing various business objects ready for accessing by the script. I want to make it impossible for the user to import the CLR or other assemblies in order to keep the script's capabilities simple and restricted to th...
3
2009-09-25T20:33:16Z
8,402,221
<p>This would prevent imports of both python modules and .Net objects so may not be what you want. (I'm relatively new to Python so I might be missing some things as well):</p> <p>Setup the environment. Import anything you need the user to have access to. Either prepend to their script or execute:</p> <pre><code>__b...
2
2011-12-06T15:18:23Z
[ "python", "ironpython" ]
Making a string the name of an instance/object
1,479,490
<p>I've been struggling for a couple of days now with the following... </p> <p>I'm trying to find a way to instantiate a number of objects which I can name via a raw_input call, and then, when I need to, look at its attributes via the 'print VARIABLE NAME' command in conjunction with the <strong>str</strong>() method....
0
2009-09-25T20:45:18Z
1,479,538
<p>This should help:</p> <p><a href="http://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class-in-python/554812#554812">http://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class-in-python/554812#554812</a></p>
0
2009-09-25T20:56:46Z
[ "python", "string", "variables", "object" ]