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
list
'import feedparser' works via SSH, but fails when in browser
1,755,043
<p>I installed feedparser via SSH, using</p> <pre><code>$ python setup.py install --home=~/httpdocs/python-libraries/feedparser-4.1/ </code></pre> <p>I did that because I don't seem to have permission to properly run 'python setup.py install'</p> <p>I am running the following python code in 'test.py'. </p> <pre><co...
1
2009-11-18T10:26:48Z
1,759,335
<p>I tried both the <code>sys.path</code> and the <code>$PYTHONPATH</code> solutions, but they didn't seem to work. I didn't try adding PythonPath to <code>httpd.conf</code> or <code>python.conf</code> - that could have worked.</p> <p>I ended up asking a root user to run <code># python setup.py install</code> for me....
0
2009-11-18T21:51:03Z
[ "python", "browser", "feedparser", "import" ]
'import feedparser' works via SSH, but fails when in browser
1,755,043
<p>I installed feedparser via SSH, using</p> <pre><code>$ python setup.py install --home=~/httpdocs/python-libraries/feedparser-4.1/ </code></pre> <p>I did that because I don't seem to have permission to properly run 'python setup.py install'</p> <p>I am running the following python code in 'test.py'. </p> <pre><co...
1
2009-11-18T10:26:48Z
3,648,326
<p>Rather than catching the exception yourself, use the <a href="http://docs.python.org/library/cgitb.html" rel="nofollow">cgitb</a> module just once at the top of your program, and you'll get better information about all errors.</p> <pre><code>import cgitb cgitb.enable() </code></pre>
0
2010-09-05T23:57:59Z
[ "python", "browser", "feedparser", "import" ]
How do I get stdout from tcl into a python string variable when using tkinter?
1,755,415
<p>I have the following python code...</p> <pre><code>import Tkinter root = Tkinter.Tk() root.tk.eval('puts {printed by tcl}') </code></pre> <p>It prints "printed by tcl" to the screen. How can I capture what the tcl interpreter prints to the screen into a python string.</p> <p>This is a simplified example of what...
1
2009-11-18T11:35:07Z
1,755,696
<p>If the code you are running prints to screen and you're calling it with root.tk.eval() you can't capture that. However, You can redefine what "puts" does in the tcl code and have it do whatever you want. This is part of the beauty of Tcl -- there are no reserved words. </p> <p>Simply create a proc named "puts" in t...
4
2009-11-18T12:41:25Z
[ "python", "tcl", "tkinter" ]
Converting RE code from PHP to Python
1,755,706
<p>I have done a simple program in PHP, now need to convert this into Python:</p> <pre><code>$string="Google 1600 Amphitheatre Parkway Mountain View, CA 94043 phone"; preg_match_all('/[0-9]+.{10,25}[^0-9]*[0-9]{5,6}+\s/',$string,$matches); print_r($matches); </code></pre>
-2
2009-11-18T12:43:15Z
1,755,760
<pre><code>import re string = "Input values" match = re.match('/[0-9]+.{10,25}[^0-9]*[0-9]{5,6}\s/', s) print match </code></pre> <p>this should be what you're looking for, if the RegEx is right.</p>
-1
2009-11-18T12:50:36Z
[ "php", "python", "regex" ]
Converting RE code from PHP to Python
1,755,706
<p>I have done a simple program in PHP, now need to convert this into Python:</p> <pre><code>$string="Google 1600 Amphitheatre Parkway Mountain View, CA 94043 phone"; preg_match_all('/[0-9]+.{10,25}[^0-9]*[0-9]{5,6}+\s/',$string,$matches); print_r($matches); </code></pre>
-2
2009-11-18T12:43:15Z
1,755,764
<pre><code>import re for x in re.findall('[0-9]+.{10,25}[^0-9]*[0-9]{5,6}\s',STRING):print x </code></pre> <p>will be ok for you?</p>
1
2009-11-18T12:51:05Z
[ "php", "python", "regex" ]
Converting RE code from PHP to Python
1,755,706
<p>I have done a simple program in PHP, now need to convert this into Python:</p> <pre><code>$string="Google 1600 Amphitheatre Parkway Mountain View, CA 94043 phone"; preg_match_all('/[0-9]+.{10,25}[^0-9]*[0-9]{5,6}+\s/',$string,$matches); print_r($matches); </code></pre>
-2
2009-11-18T12:43:15Z
1,755,811
<p>In python, you must use the "re" module to do that. Unlike in PHP, you don't need to place delimitors, so strip the "/" you have at the begining and the end of the pattern.</p> <p>The Python idiom would then be :</p> <pre><code>import re string="Input values" for match in re.findall('[0-9]+.{10,25}[^0-9]*[0-9]{5,6...
0
2009-11-18T13:02:05Z
[ "php", "python", "regex" ]
Problem with db.get in Google App Engine
1,756,059
<p>When I run the following code:</p> <pre><code> query = datastore.Food_Item.all() results = query.fetch(1) foodA = results[0] foodB = db.get(foodA.key()) </code></pre> <p>I would expect foodA and foodB to be the same type. However, I see that the foodA is of type "model.datastore.Food_Item" and foodB...
0
2009-11-18T13:42:51Z
1,756,578
<p>It seems likely you're importing the same module (model.datastore) by different names in different places - for example, by using a relative import inside the model package. db.get returns whichever name it saw when the module was first imported, while your own code (the query) returns whatever you explicitly specif...
4
2009-11-18T14:58:06Z
[ "python", "google-app-engine" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,152
<p>Generators could be thought of as shorthand for creating an iterator. They behave like a Java Iterator. Example:</p> <pre><code>&gt;&gt;&gt; g = (x for x in range(10)) &gt;&gt;&gt; g &lt;generator object &lt;genexpr&gt; at 0x7fac1c1e6aa0&gt; &gt;&gt;&gt; g.next() 0 &gt;&gt;&gt; g.next() 1 &gt;&gt;&gt; g.next() 2 ...
17
2009-11-18T13:53:35Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,156
<p><strong>Note: this post assumes Python 3.x syntax.</strong><sup>&dagger;</sup></p> <p>A <a href="http://www.python.org/dev/peps/pep-0255/">generator</a> is simply a function which returns an object on which you can call <code>next</code>, such that for every call it returns some value, until it raises a <code>StopI...
175
2009-11-18T13:54:07Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,183
<p>There is no Java equivalent.</p> <p>Here is a bit of a contrived example:</p> <pre><code>#! /usr/bin/python def mygen(n): x = 0 while x &lt; n: x = x + 1 if x % 3 == 0: yield x for a in mygen(100): print a </code></pre> <p>There is a loop in the generator that runs from 0 to n, and i...
6
2009-11-18T13:58:13Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,282
<p>It helps to make a clear distinction between the function foo, and the generator foo(n):</p> <pre><code>def foo(n): yield n yield n+1 </code></pre> <p>foo is a function. foo(6) is a generator object.</p> <p>The typical way to use a generator object is in a loop:</p> <pre><code>for n in foo(6): print...
3
2009-11-18T14:15:10Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,342
<p>A generator is effectively a function that returns (data) before it is finished, but it pauses at that point, and you can resume the function at that point.</p> <pre><code>&gt;&gt;&gt; def myGenerator(): ... yield 'These' ... yield 'words' ... yield 'come' ... yield 'one' ... yield 'at' ... ...
25
2009-11-18T14:24:02Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,430
<p>First of all the term <strong>generator</strong> originally was somewhat ill-defined in Python, leading to lots of confusion. What you probably mean are <strong>iterators</strong> and <strong>iterables</strong> (see <a href="http://docs.python.org/library/stdtypes.html#typeiter">here</a>). Then in Python there are a...
14
2009-11-18T14:35:54Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,756,555
<p>I believe the first appearance of iterators and generators were in the Icon programming language, about 20 years ago.</p> <p>You may enjoy <a href="http://www.cs.arizona.edu/icon/docs/ipd266.htm" rel="nofollow">the Icon overview</a>, which lets you wrap your head around them without concentrating on the syntax (sin...
1
2009-11-18T14:53:32Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,757,863
<p>The only thing I can add to Stephan202's answer is a recommendation that you take a look at David Beazley's PyCon '08 presentation "Generator Tricks for Systems Programmers," which is the best single explanation of the how and why of generators that I've seen anywhere. This is the thing that took me from "Python lo...
5
2009-11-18T17:54:00Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,932,114
<p>This post will use <a href="http://en.wikipedia.org/wiki/Fibonacci%5Fnumber" rel="nofollow">Fibonacci numbers</a> as a tool to build up to explaining the usefulness of <a href="http://www.python.org/dev/peps/pep-0255/" rel="nofollow">Python generators</a>. </p> <p>This post will feature both C++ and Python code. </...
2
2009-12-19T05:58:42Z
[ "python", "generator" ]
Understanding Generators in Python
1,756,096
<p>Reading the Python cookbook at the minute and currently looking at generators. I'm finding it hard to get my head round.</p> <p>As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however when I hear that I think of threading.</p> <p>Can anyone explain w...
92
2009-11-18T13:46:51Z
1,932,598
<p>I like to describe generators, to those with a decent background in programming languages and computing, in terms of stack frames.</p> <p>In many languages, there is a stack on top of which is the current stack "frame". The stack frame includes space allocated for variables local to the function including the argu...
3
2009-12-19T10:50:33Z
[ "python", "generator" ]
Gracefully convert .rar to .zip using Python
1,756,109
<p>I'm currently making system call to "unrar and zip" commands. It interrupts and requires me to enter password while encounter password propected archives.</p> <p>Is it possible to let it run and return a "unsuccessful" value to main program on any error or password prompt?</p> <p>Can we natively use rarfile and zi...
2
2009-11-18T13:48:45Z
1,756,477
<p>Maybe this could help: <a href="http://www.noah.org/wiki/Pexpect#Description%5Fof%5FPexpect" rel="nofollow">http://www.noah.org/wiki/Pexpect#Description%5Fof%5FPexpect</a></p>
0
2009-11-18T14:41:53Z
[ "python", "zip", "compression", "rar", "uncompress" ]
Gracefully convert .rar to .zip using Python
1,756,109
<p>I'm currently making system call to "unrar and zip" commands. It interrupts and requires me to enter password while encounter password propected archives.</p> <p>Is it possible to let it run and return a "unsuccessful" value to main program on any error or password prompt?</p> <p>Can we natively use rarfile and zi...
2
2009-11-18T13:48:45Z
1,757,998
<p>I think it's very difficult to do the task without using temporary files. If you are converting very large files you need to use temporary space in disk.</p> <p>You can use the <a href="http://code.google.com/p/py-unrar2/" rel="nofollow">PyUnRAR2 library</a>, it will let you examine and extract the files of a RAR a...
2
2009-11-18T18:15:08Z
[ "python", "zip", "compression", "rar", "uncompress" ]
lazy load or early load for python?
1,756,276
<p>We've got the following code sample:</p> <pre><code>big_static_data = { "key1" : { "subkey1" : "subvalue1", ... }, "key2" : ... } class StaticDataEarlyLoad: def __init__(self): self.static_data = big_static_data # other init def handle_use_id(self, id): return ...
1
2009-11-18T14:14:15Z
1,756,341
<p>In your example, StaticDataLazyLoad (once the syntax for <strong>init</strong> is correct) wont make a big difference.</p> <p>"big_static_data" is initialized ("loaded") when the module is imported. It will immediately require some memory, no matter whether an instance of your classes is created or not.</p> <p>An ...
3
2009-11-18T14:23:53Z
[ "python", "performance" ]
lazy load or early load for python?
1,756,276
<p>We've got the following code sample:</p> <pre><code>big_static_data = { "key1" : { "subkey1" : "subvalue1", ... }, "key2" : ... } class StaticDataEarlyLoad: def __init__(self): self.static_data = big_static_data # other init def handle_use_id(self, id): return ...
1
2009-11-18T14:14:15Z
1,756,377
<p>big_static_data is created once at the beginning of the file (at least in the code that you show).</p> <p>This consumes memory.</p> <p>When you create an instance of StaticDataEarlyLoad, </p> <p>StaticDataEarlyLoad().static_data is a reference to big_static_data. It consumes a very minor amount of memory. It mere...
3
2009-11-18T14:28:51Z
[ "python", "performance" ]
Pass XML fragments as stylesheet paramters with lxml?
1,756,559
<p>I'm starting to use <code>lxml</code> in Python for processing XML/XSL documents, and in general it seems very straight forward. However, I'm not able to find a way to pass an XML fragment as a stylesheet parameter when doing a translation.</p> <p>For example, in PHP it is possible to pass <code>DOMDocument</code> ...
0
2009-11-18T14:54:38Z
1,757,449
<p>As far as I know, you can only use xpath expressions (or result of the etree.XSLT.strparam() method for strings with quotes) in lxml (at the moment anyway).</p> <p>However, because you can use xpath expressions, that means you could "work around" by using a <a href="http://lxml.de/extensions.html#xpath-extension-fu...
1
2009-11-18T16:56:46Z
[ "python", "xml", "xslt", "lxml" ]
Pass XML fragments as stylesheet paramters with lxml?
1,756,559
<p>I'm starting to use <code>lxml</code> in Python for processing XML/XSL documents, and in general it seems very straight forward. However, I'm not able to find a way to pass an XML fragment as a stylesheet parameter when doing a translation.</p> <p>For example, in PHP it is possible to pass <code>DOMDocument</code> ...
0
2009-11-18T14:54:38Z
1,759,138
<p>I believe what you're looking for can be performed using <a href="http://codespeak.net/lxml/api/lxml.etree.XSLT-class.html" rel="nofollow">lxml.etree.XSLT</a>. Good examples can be found in the <a href="http://codespeak.net/lxml/xpathxslt.html#xslt" rel="nofollow">XSLT section</a> on the page documenting <a href="h...
0
2009-11-18T21:16:09Z
[ "python", "xml", "xslt", "lxml" ]
Pass XML fragments as stylesheet paramters with lxml?
1,756,559
<p>I'm starting to use <code>lxml</code> in Python for processing XML/XSL documents, and in general it seems very straight forward. However, I'm not able to find a way to pass an XML fragment as a stylesheet parameter when doing a translation.</p> <p>For example, in PHP it is possible to pass <code>DOMDocument</code> ...
0
2009-11-18T14:54:38Z
1,993,641
<p>You can use a python extension function to return an etree.XML document to xslt. Here's the bare minimum example. It registers a global namespace (uri:params), associations a function with the "params" method in that namespace. That's used by the stylesheet to reach the extension function. Don't forget the xmlns:pte...
0
2010-01-03T01:51:30Z
[ "python", "xml", "xslt", "lxml" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
1,756,757
<p>On my system (OSX 10.6) that package is at </p> <pre><code>/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py </code></pre> <p>I hope that helps you figure out if it's missing or just not on your path.</p>
3
2009-11-18T15:25:02Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
1,757,269
<p>[UPDATE] TL;DR <code>pkg_resources</code> is provided by either <a href="http://pypi.python.org/pypi/distribute/">Distribute</a> or <a href="http://pypi.python.org/pypi/setuptools/">setuptools</a>. </p> <p>[UPDATE 2] As announced at PyCon 2013, the <code>Distribute</code> and <code>setuptools</code> projects have r...
71
2009-11-18T16:31:32Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
9,883,577
<p>In case of upgrading your python on mac os 10.7 and pkg_resources doesn't work, the simplest way to fix this is just reinstall setuptools as Ned mentioned above.</p> <pre><code>sudo pip install setuptools --upgrade or sudo easy_install install setuptools --upgrade </code></pre>
7
2012-03-27T04:57:37Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
10,538,341
<p>I encountered the same <code>ImportError</code>. Somehow the <code>setuptools</code> package had been deleted in my Python environment.</p> <p>To fix the issue, run the setup script for <code>setuptools</code>:</p> <pre><code>curl https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py | python </code></pr...
76
2012-05-10T16:25:53Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
18,363,536
<p>The reason might be because the IPython module is not in your PYTHONPATH.</p> <p>If you donwload IPython and then do python setup.py install </p> <p>The setup doesn't add the module IPython to your python path. You might want to add it to your PYTHONPATH manually. It should work after you do :</p> <p>export PY...
1
2013-08-21T17:03:28Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
21,417,966
<p>I realize this is not related to OSX, but on an embedded system (Beagle Bone Angstrom) I had the exact same error message. Installing the following ipk packages solved it.</p> <pre><code>opkg install python-setuptools opkg install python-pip </code></pre>
0
2014-01-28T22:07:56Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
24,609,758
<p>I got this error on <strong>Ubuntu</strong>, and the following worked for me:</p> <p>Removed the dropbox binaries and download them again, by running:</p> <pre><code>sudo rm -rf /var/lib/dropbox/.dropbox-dist dropbox start -i </code></pre>
0
2014-07-07T11:41:11Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
26,257,667
<p>I encountered with the same problem when i am working on autobahn related project. </p> <p>1) So I download the setuptools.-0.9.8.tar.gz form <a href="https://pypi.python.org/packages/source/s/setuptools/" rel="nofollow">https://pypi.python.org/packages/source/s/setuptools/</a> and extract it. </p> <p>2 )The...
0
2014-10-08T13:05:55Z
[ "python", "ipython" ]
What is causing ImportError: No module named pkg_resources after upgrade of Python on os X?
1,756,721
<p>I just updated Python to 2.6.4 on my Mac. I installed from the dmg package.</p> <p>The binary did not seem to correctly set my Python path, so I added <code>'/usr/local/lib/python2.6/site-packages'</code> in <code>.bash_profile</code></p> <pre><code>&gt;&gt;&gt; pprint.pprint(sys.path) ['', '/Users/Bryan/work/d...
76
2009-11-18T15:19:25Z
39,211,022
<p>In my case, package <code>python-pygments</code> was missed. You can fix it by command:</p> <p><code>sudo apt-get install python-pygments</code></p> <p>If there is problem with <code>pandoc</code>. You should install <code>pandoc</code> and <code>pandoc-citeproc</code>.</p> <p><code>sudo apt-get install pandoc pa...
0
2016-08-29T16:34:52Z
[ "python", "ipython" ]
How to specify a baseDN when connecting to LDAP via python?
1,756,977
<p>I want to connect to a ldap server with python-ldap using a specific baseDN. </p> <pre><code>import ldap baseDN="ou=unit,o=org.c=xx" # doesn't work #baseDN="" # works host="ldaps://test.org.xx:636" userDN="cn=proxyhlrb,ou=services,o=org,c=xx" passwd="secret" server=ldap.initialize(host+"/"+baseDN) server.bind_s(...
0
2009-11-18T15:54:48Z
1,757,072
<p>I think this resource would be interesting for you. It nicely explains how to combine LDAP with Python.</p> <p><a href="http://www.packtpub.com/article/python-ldap-applications-ldap-opearations" rel="nofollow">http://www.packtpub.com/article/python-ldap-applications-ldap-opearations</a></p> <p><strong>Edit</strong...
0
2009-11-18T16:06:06Z
[ "python", "ldap", "connection" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,039
<p>Dictionaries don't preserve order, so you can't tell which element had been added first. You could combine the dictionary with a list of it's keys to preserve order.</p> <p>Here's an <a href="http://code.activestate.com/recipes/496761/">activestate recipe</a> for an ordered dict that does just this.</p> <p>There's...
7
2009-11-18T16:01:30Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,058
<p>Unless you had some kind of set number of elements, where you know which one is the oldest, then you could simply delete it. Otherwise, you're using the wrong data structure for what you're doing I think.</p> <p><em>EDIT</em>: Though, according to a quick google, I've come across <a href="http://www.python.org/dev/...
3
2009-11-18T16:03:54Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,073
<p>One way to do this would be to store the keys in an array, which will preserve your order for you. Something like:</p> <pre><code>MAXSIZE = 4 dict = {} history = [] def add(key,value): print len(dict) if len(dict) == MAXSIZE: old = history.pop(0) # returns the key to the oldest item del dic...
2
2009-11-18T16:06:07Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,075
<p>Python 3.1 has an ordered dict. use the class <code>collections.OrderedDict</code> to keep the elements in their order of insertions. beware that if you are overwriting an element, it keeps its place in the order, you need to delete and re-insert an element to make it last.</p> <p>if you are using an older release,...
12
2009-11-18T16:06:40Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,104
<p>Without knowing what you're really trying to use this structure for, here's something that may work for you:</p> <pre><code>class DictCache: def __init__(self, maxcount=4): self.data = {} self.lru = [] self.maxcount = maxcount def add(self, key, value): self.data[key] = value...
0
2009-11-18T16:10:19Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,113
<p>Alternatively a list of tuples could be used for this as well.</p> <pre><code>MAXSIZE = 4 stack = [] def add(key, value): stack.append((key, value)) if len(stack) &gt; MAXSIZE: stack.pop(0) print stack add('a','1') add('b','2') add('c','3') add('d','4') add('e','5') </code></pre> <p>results in</p> <pre><c...
1
2009-11-18T16:10:59Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,156
<p>how about like this? put order in array and when its reach limit, pop it.</p> <pre><code>MAXSIZE = 4 dict,order= {},[] def add(key,value): if len(dict) &gt; MAXSIZE: old = order.pop() # returns the key to the oldest item del dict[old] order.insert(0,key) dict[key] = value </code></pre>
0
2009-11-18T16:15:52Z
[ "python", "dictionary" ]
removing the oldest element from a dictionary in python
1,756,992
<p>I would like to know the best way to remove the <strong>oldest</strong> element in a dictionary in order to control the maximum dictionary size.</p> <p><strong>example</strong>:</p> <pre><code>MAXSIZE = 4 dict = {} def add(key,value): if len(dict) == MAXSIZE: old = get_oldest_key() # returns the key to the o...
3
2009-11-18T15:56:50Z
1,757,621
<p>I believe <a href="http://code.activestate.com/recipes/252524/" rel="nofollow">LRU dict-like container</a> will suite your needs the best.</p>
3
2009-11-18T17:18:30Z
[ "python", "dictionary" ]
Checking files retrieved by Twisted's FTPClient.retrieveFile method for completeness
1,757,276
<p>I'm writing a custom ftp client to act as a gatekeeper for incoming multimedia content from subcontractors hired by one of our partners. I chose twisted because it allows me to parse the file contents before writing the files to disk locally, and I've been looking for occasion to explore twisted anyway. I'm using 't...
3
2009-11-18T16:32:36Z
1,757,848
<p>There are a couple unit tests for behavior in this area.</p> <p><code>twisted.test.test_ftp.FTPClientTestCase.test_failedRETR</code> is the most directly relevant one. It covers the case where the control and data connections are lost while a file transfer is in progress.</p> <p>It seems to me that test coverage ...
4
2009-11-18T17:52:09Z
[ "python", "ftp", "client", "twisted" ]
Possible to sandbox Python configuration file?
1,757,381
<p>I'm thinking of implementing a configuration file written in Python syntax, not unlike what Django does.</p> <p>While I've seen <a href="http://stackoverflow.com/questions/1642413/why-do-so-many-apps-frameworks-keep-their-configuration-files-in-an-un-executed-f">one</a> or <a href="http://stackoverflow.com/question...
1
2009-11-18T16:47:15Z
1,757,946
<p>Unfortunately there isn't a lot you can do about this issue with standard Python. When the Python interpreter is running the "configuration code" that code can do whatever it likes including accessing the host program or not returning control. Running the configuration code in a separate process might help but also ...
2
2009-11-18T18:07:53Z
[ "python", "configuration", "sandbox" ]
Possible to sandbox Python configuration file?
1,757,381
<p>I'm thinking of implementing a configuration file written in Python syntax, not unlike what Django does.</p> <p>While I've seen <a href="http://stackoverflow.com/questions/1642413/why-do-so-many-apps-frameworks-keep-their-configuration-files-in-an-un-executed-f">one</a> or <a href="http://stackoverflow.com/question...
1
2009-11-18T16:47:15Z
3,697,169
<p>We do this for some of our internal tools</p> <p>What we do protects us from exception issues and discourages any attempts by the users to get overly creative in the config scripts. However it doesn't protect us from infinite loops or actively malicious third parties.</p> <p>The core of the approach here is to run...
3
2010-09-12T23:48:37Z
[ "python", "configuration", "sandbox" ]
Detach a subprocess started using python multiprocessing module
1,757,388
<p>I would like to create a process using the mutliprocessing module in python but ensure it continues running after the process that created the subprocess exits. </p> <p>I can get the required functionality using the subprocess module and Popen, but I want to run my code as a function, not as a script. The reason I ...
7
2009-11-18T16:48:38Z
1,758,916
<p>What you're trying to do is start a daemon process. Look at <a href="http://www.python.org/dev/peps/pep-3143" rel="nofollow">PEP-3143</a>, and the <a href="http://pypi.python.org/pypi/python-daemon/" rel="nofollow">python-daemon</a> package. </p> <p>Took a look into Pyro, and it seems they include their own daemon...
0
2009-11-18T20:40:01Z
[ "python", "subprocess", "multiprocessing", "detach", "pyro" ]
Detach a subprocess started using python multiprocessing module
1,757,388
<p>I would like to create a process using the mutliprocessing module in python but ensure it continues running after the process that created the subprocess exits. </p> <p>I can get the required functionality using the subprocess module and Popen, but I want to run my code as a function, not as a script. The reason I ...
7
2009-11-18T16:48:38Z
1,761,537
<p>I finally got what I wanted. I appreciate any suggestions to improve the code.</p> <pre><code>def start_server(): pyrodaemon = Pyro.core.Daemon() #setup daemon and nameserver #Don't want to close the pyro socket #Need to remove SIGTERM map so Processing doesn't kill the subprocess #Need to expli...
3
2009-11-19T07:37:46Z
[ "python", "subprocess", "multiprocessing", "detach", "pyro" ]
Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists
1,757,698
<p>I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of <strong>all possible 2-list combinations</strong> in this dataset. Note that elements in each list are unique so they can be stored as sets in python.</p> <p>What is the most efficient way...
2
2009-11-18T17:27:54Z
1,757,750
<p>As you need to produce a (N by N/2) matrix of results, i.e., O(N squared) outputs, no approach can be less than O(N squared) -- in any language, of course. (N is "about 9K" in your question). So, I see nothing intrinsically faster than (a) making the N sets you need, and (b) iterating over them to produce the outp...
2
2009-11-18T17:36:58Z
[ "python", "list", "set", "intersection", "combinations" ]
Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists
1,757,698
<p>I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of <strong>all possible 2-list combinations</strong> in this dataset. Note that elements in each list are unique so they can be stored as sets in python.</p> <p>What is the most efficient way...
2
2009-11-18T17:27:54Z
1,757,895
<p>If your sets are stored in s, for example:</p> <pre><code>s = [set([1, 2]), set([1, 3]), set([1, 2, 3]), set([2, 4])] </code></pre> <p>Then you can use <a href="http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations" rel="nofollow">itertools.combinations</a> to take them two by tw...
2
2009-11-18T17:59:17Z
[ "python", "list", "set", "intersection", "combinations" ]
Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists
1,757,698
<p>I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of <strong>all possible 2-list combinations</strong> in this dataset. Note that elements in each list are unique so they can be stored as sets in python.</p> <p>What is the most efficient way...
2
2009-11-18T17:27:54Z
1,945,769
<p>Try this:</p> <pre><code>_lists = [[1, 2, 3, 7], [1, 3], [1, 2, 3], [1, 3, 4, 7]] _sets = map( set, _lists ) _intersection = reduce( set.intersection, _sets ) </code></pre> <p>And to obtain the indexes:</p> <pre><code>_idxs = [ map(_i.index, _intersection ) for _i in _lists ] </code></pre> <p>Cheers,</p> <p>Jos...
0
2009-12-22T11:19:02Z
[ "python", "list", "set", "intersection", "combinations" ]
How to append a reading to a python list?
1,758,068
<p>i am using GM862 module and i want to write the cordinates as it is in a file "cordinates.txt" but i get some error, this is the code i wrote:</p> <pre><code>import MDM cordlist = [] f = open("cordinates.txt", 'w') def AcquiredPosition(): res = MDM.send('AT$GPSACP\r', 0) res = MDM.receive(30) if(res.find('...
0
2009-11-18T18:28:53Z
1,758,082
<p>Why not open the file in append mode ('a' instead of 'w') and just writelines to that?</p>
2
2009-11-18T18:31:53Z
[ "python" ]
How to append a reading to a python list?
1,758,068
<p>i am using GM862 module and i want to write the cordinates as it is in a file "cordinates.txt" but i get some error, this is the code i wrote:</p> <pre><code>import MDM cordlist = [] f = open("cordinates.txt", 'w') def AcquiredPosition(): res = MDM.send('AT$GPSACP\r', 0) res = MDM.receive(30) if(res.find('...
0
2009-11-18T18:28:53Z
1,758,100
<p>You are appending to your list and then writing the full list to the file each time through the loop.</p> <p>You need to clear down the list in each pass through the loop.</p> <p>Put <code>cordlist = []</code> as the first line under <code>while(1)</code></p>
3
2009-11-18T18:33:43Z
[ "python" ]
How to append a reading to a python list?
1,758,068
<p>i am using GM862 module and i want to write the cordinates as it is in a file "cordinates.txt" but i get some error, this is the code i wrote:</p> <pre><code>import MDM cordlist = [] f = open("cordinates.txt", 'w') def AcquiredPosition(): res = MDM.send('AT$GPSACP\r', 0) res = MDM.receive(30) if(res.find('...
0
2009-11-18T18:28:53Z
1,758,146
<p>Because that's what you've asked it to do. On every iteration, you append an item to the list, then write out all the lines so far. So each time you'll repeat everything you've already done, plus the one new line.</p> <p>Since your function only returns a single line I don't know why you're bothering with a list at...
0
2009-11-18T18:40:33Z
[ "python" ]
How can I run a Python program over telnet?
1,758,276
<p>How can I run a Python program so it outputs its STDOUT and inputs its STDIN to/from a remote telnet client?</p> <p>All the program does is print out text then wait for raw_input(), repeatedly. I want a remote user to use it without needing shell access. It can be single threaded/single user.</p>
2
2009-11-18T19:02:22Z
1,758,297
<p>On a Unix system, you can use <a href="http://en.wikipedia.org/wiki/Inetd">inetd</a> for this. It will take care of opening the network connection for you, so your program will work as-is.</p>
10
2009-11-18T19:05:47Z
[ "python", "telnet" ]
How can I run a Python program over telnet?
1,758,276
<p>How can I run a Python program so it outputs its STDOUT and inputs its STDIN to/from a remote telnet client?</p> <p>All the program does is print out text then wait for raw_input(), repeatedly. I want a remote user to use it without needing shell access. It can be single threaded/single user.</p>
2
2009-11-18T19:02:22Z
1,758,310
<p>Make the Python script into the shell for that user. (Or if that doesn't work, wrap it up in bash script or even a executable).</p> <p>(You might have to put it in /etc/shells (or equiv.))</p>
5
2009-11-18T19:07:45Z
[ "python", "telnet" ]
How can I run a Python program over telnet?
1,758,276
<p>How can I run a Python program so it outputs its STDOUT and inputs its STDIN to/from a remote telnet client?</p> <p>All the program does is print out text then wait for raw_input(), repeatedly. I want a remote user to use it without needing shell access. It can be single threaded/single user.</p>
2
2009-11-18T19:02:22Z
1,758,688
<p>If you don't need full telnet functionality, you can just use a socket.</p> <p>Check out the <a href="http://docs.python.org/library/socketserver.html#module-SocketServer" rel="nofollow">Socket Server</a> module in the standard python library. It'll enable you to easily write a program that'll listen on a port for ...
4
2009-11-18T20:04:09Z
[ "python", "telnet" ]
How can I run a Python program over telnet?
1,758,276
<p>How can I run a Python program so it outputs its STDOUT and inputs its STDIN to/from a remote telnet client?</p> <p>All the program does is print out text then wait for raw_input(), repeatedly. I want a remote user to use it without needing shell access. It can be single threaded/single user.</p>
2
2009-11-18T19:02:22Z
1,760,716
<p>You can just create a new linux user and set their shell to your script.</p> <p>Then when they telnet in and enter the username/password, the program runs instead of bash or whatever the default shell is.</p>
0
2009-11-19T03:32:42Z
[ "python", "telnet" ]
How to produce html unit test output in Python?
1,758,354
<p>I'm looking for any way to display the results of python unit tests in an html summary. There are tools like this for Java and Ruby... haven't yet located any tools that seem to do this for Python. Are there any out there?</p> <p><strong>JUnit html output:</strong></p> <p><img src="http://www.ibm.com/developerwork...
6
2009-11-18T19:15:23Z
1,836,762
<p>Yes, there is something like that written, it's at <a href="http://tungwaiyip.info/software/HTMLTestRunner.html">http://tungwaiyip.info/software/HTMLTestRunner.html</a>. Here is the sample output: <a href="http://tungwaiyip.info/software/sample%5Ftest%5Freport.html">http://tungwaiyip.info/software/sample%5Ftest%5Fre...
7
2009-12-02T23:59:49Z
[ "python", "unit-testing", "nose" ]
python prompt with a bash like interface
1,758,819
<p>I am using the python prompt to practice some regular expressions. I was wondering if there was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks</p>
2
2009-11-18T20:27:29Z
1,758,836
<p>You want <a href="http://ipython.scipy.org/moin/">ipython</a>.</p>
6
2009-11-18T20:29:20Z
[ "python" ]
python prompt with a bash like interface
1,758,819
<p>I am using the python prompt to practice some regular expressions. I was wondering if there was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks</p>
2
2009-11-18T20:27:29Z
1,758,841
<p>Use the <a href="http://docs.python.org/library/rlcompleter.html">rlcompleter</a> module to get both readline and completion.</p> <p>Sample PYTHONSTARTUP code:</p> <pre><code>try: import readline except ImportError: print "Module readline unavailable." else: import rlcompleter readline.parse_and_bind("tab:...
7
2009-11-18T20:30:06Z
[ "python" ]
python prompt with a bash like interface
1,758,819
<p>I am using the python prompt to practice some regular expressions. I was wondering if there was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks</p>
2
2009-11-18T20:27:29Z
1,758,845
<p>If you compile python with readline support, the REPL environment should do this for you.</p>
6
2009-11-18T20:30:41Z
[ "python" ]
python prompt with a bash like interface
1,758,819
<p>I am using the python prompt to practice some regular expressions. I was wondering if there was a way to use the up/down arrows (like bash) to cycle through the old commands typed. I know its possible since it works on python on cygwin/windows. thanks</p>
2
2009-11-18T20:27:29Z
1,760,293
<p>As well as compiling with readline enabled as suggested in another answer, you can also use rlrwrap to add readline at run time, even if it wasn't complied in; like so:</p> <pre><code>rlwrap python </code></pre>
6
2009-11-19T01:10:08Z
[ "python" ]
What is the right way to organize python unittests into suites?
1,759,128
<p>I have some test case classes organized in directories</p> <pre><code>foo_tests foo_tests1.py foo_tests2.py ... bar_tests bar_tests1.py ... </code></pre> <p>The test cases look like:</p> <pre><code>foo_tests1.py: import unittest class FooTestsOne(unittest.TestCase): def test_1(): as...
3
2009-11-18T21:14:33Z
1,759,336
<p>Tests are not supposed to run during import. Maybe you have <code>unittest.main()</code> at the bottom of foo_test1.py?</p> <p>Your script should work, except that </p> <pre><code>result = unittest.TextTestRunner(verbosity=2).run(suite()) </code></pre> <p>should be </p> <pre><code>result = unittest.TextTestRunne...
3
2009-11-18T21:51:04Z
[ "python", "unit-testing" ]
How to read line (from a file) and then append + print in python?
1,759,193
<pre><code>for line in file: print line </code></pre> <p>In the code above when I change it to:</p> <pre><code>for line in file: print line + " just a string" </code></pre> <p>This only appends "just a string" to the last line</p> <p>PS: Python newbie</p>
1
2009-11-18T21:25:46Z
1,759,212
<p>Iterating over a file includes the line endings, so just remove them:</p> <pre><code>for line in file: print line.rstrip("\n"), "something" </code></pre> <p>Note that print will append its own newline, so even without appending "something" you'd want to do this (or use sys.stdout.write instead of print). You ma...
5
2009-11-18T21:29:39Z
[ "python" ]
How to read line (from a file) and then append + print in python?
1,759,193
<pre><code>for line in file: print line </code></pre> <p>In the code above when I change it to:</p> <pre><code>for line in file: print line + " just a string" </code></pre> <p>This only appends "just a string" to the last line</p> <p>PS: Python newbie</p>
1
2009-11-18T21:25:46Z
1,759,213
<p>The line received through the iterator includes the newline character at the end - so if you want "something" to be appended on the same line you will need to cut this off. </p> <pre><code>for line in file: print line[:-1] + " something" </code></pre>
0
2009-11-18T21:29:45Z
[ "python" ]
How to read line (from a file) and then append + print in python?
1,759,193
<pre><code>for line in file: print line </code></pre> <p>In the code above when I change it to:</p> <pre><code>for line in file: print line + " just a string" </code></pre> <p>This only appends "just a string" to the last line</p> <p>PS: Python newbie</p>
1
2009-11-18T21:25:46Z
1,759,268
<p><strong>This is how you can append 'something' at the end of each line:</strong></p> <pre><code>import fileinput for line in fileinput.input("dat.txt"): print line.rstrip(), ' something' </code></pre> <p>If you want to append 'something' to line and then print the line:</p> <pre><code>import fileinput for l...
0
2009-11-18T21:41:26Z
[ "python" ]
Deploying a python CGI app
1,759,205
<p>I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is...
1
2009-11-18T21:27:50Z
1,759,959
<p>This looks like a job for <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">virtualenv</a>. From the site:</p> <blockquote> <p>Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.</p> </blockquote> <p>This looks to be right up your alley...
0
2009-11-18T23:52:58Z
[ "python", "linux", "hosting", "cgi" ]
Deploying a python CGI app
1,759,205
<p>I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is...
1
2009-11-18T21:27:50Z
1,760,779
<p>In your shoes, I'd use <a href="http://www.pyinstaller.org/" rel="nofollow">pyinstaller</a> to bundle Python, my code, and all my dependencies into one installer executable, upload it, and run it. Just be sure to use the SVN trunk of pyinstaller -- the "released" version is WAY obsolete.</p> <p>Be aware that with ...
1
2009-11-19T03:51:32Z
[ "python", "linux", "hosting", "cgi" ]
Deploying a python CGI app
1,759,205
<p>I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is...
1
2009-11-18T21:27:50Z
1,760,822
<p>I am on <a href="http://www.dreamhost.com/" rel="nofollow">Dreamhost</a>'s shared plan. Besides CGI, they also offer <a href="http://wiki.dreamhost.com/Python%5FFastCGI" rel="nofollow">FastCGI</a> which makes things much faster than CGI. You should check if your hosting provider offers that. Or maybe they provide <a...
0
2009-11-19T04:09:55Z
[ "python", "linux", "hosting", "cgi" ]
Deploying a python CGI app
1,759,205
<p>I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is...
1
2009-11-18T21:27:50Z
1,763,643
<blockquote> <p>how do I set up a isolated, complete, standalone python environment in my home directory</p> </blockquote> <ol> <li><code>mkdir /home/me/.local</code> (if it doesn't already exist. You don't have to use <code>.local</code> but it is becoming the normal place to put this)</li> <li><code>mkdir /home/me...
3
2009-11-19T14:25:10Z
[ "python", "linux", "hosting", "cgi" ]
Write a data string to a NumPy character array?
1,759,208
<p>I want to write a data string to a NumPy array. Pseudocode:</p> <pre><code>d = numpy.zeros(10, dtype = numpy.character) d[1:6] = 'hello' </code></pre> <p>Example result:</p> <pre><code>d= array(['', 'h', 'e', 'l', 'l', 'o', '', '', '', ''], dtype='|S1') </code></pre> <p>How can this be done most natura...
5
2009-11-18T21:29:01Z
1,759,232
<p>Just explicitly make your text a <code>list</code> (rather than that it is iterable from Python) and <a href="http://en.wikipedia.org/wiki/NumPy" rel="nofollow">NumPy</a> will understand it automatically:</p> <pre><code>&gt;&gt;&gt; text = 'hello' &gt;&gt;&gt; offset = 1 &gt;&gt;&gt; d[offset:offset+len(text)] = li...
3
2009-11-18T21:33:32Z
[ "python", "numpy" ]
Write a data string to a NumPy character array?
1,759,208
<p>I want to write a data string to a NumPy array. Pseudocode:</p> <pre><code>d = numpy.zeros(10, dtype = numpy.character) d[1:6] = 'hello' </code></pre> <p>Example result:</p> <pre><code>d= array(['', 'h', 'e', 'l', 'l', 'o', '', '', '', ''], dtype='|S1') </code></pre> <p>How can this be done most natura...
5
2009-11-18T21:29:01Z
1,789,872
<p>There's little need to build a list when you have numpy.fromstring and numpy.fromiter.</p>
1
2009-11-24T12:47:40Z
[ "python", "numpy" ]
Write a data string to a NumPy character array?
1,759,208
<p>I want to write a data string to a NumPy array. Pseudocode:</p> <pre><code>d = numpy.zeros(10, dtype = numpy.character) d[1:6] = 'hello' </code></pre> <p>Example result:</p> <pre><code>d= array(['', 'h', 'e', 'l', 'l', 'o', '', '', '', ''], dtype='|S1') </code></pre> <p>How can this be done most natura...
5
2009-11-18T21:29:01Z
1,894,219
<p>You might try Python's <code>array</code> type. It will not have as high an overhead as a <code>list</code>.</p> <pre><code>from array import array as pyarray d[1:6] = pyarray('c', 'hello') </code></pre>
1
2009-12-12T18:00:14Z
[ "python", "numpy" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,360
<p>Follow the presentation slides from David M. Beazley at (if you are certain you want to skip the intro, start at page 18): <a href="http://www.dabeaz.com/generators/" rel="nofollow">http://www.dabeaz.com/generators/</a></p> <p>That is undisputably (in my opinion :) the very best way to solve what you are trying to ...
1
2009-11-18T21:54:31Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,393
<p>You have many issues here.</p> <pre><code> for line in inputfile: line = inputfile_fileHandle.readline() </code></pre> <p><code>inputfile</code> is the name of your file, so this loop is going to be executed once for each character in the name of the file, definitely not what you want. You do this twice (ne...
1
2009-11-18T21:57:50Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,408
<p>Several errors:</p> <pre><code>for line in inputfile: </code></pre> <p><code>inputfile</code> is your filename. So the <code>for</code> loop will iterate over each character of the filename. </p> <p>You need to do</p> <pre><code>for line in inputfile_fileHandle: </code></pre> <p>Then, <code>line</code> already ...
0
2009-11-18T21:59:23Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,422
<pre><code>in_data = False for line in open( 'data.txt' ): if line.startswith( 'Date:' ): in_data = True elif line.startswith( 'End Set' ): in_data = False elif in_data: print line.rstrip() </code></pre> <p>Just put something like that inside a loop over your files (i.e. os.walk) an...
2
2009-11-18T22:00:54Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,520
<p>I'd probably do something much simpler, like this:</p> <pre><code>import glob, os start_search = "Set :" end_search = "End Set" path = '.' for filename in glob.glob(os.path.join(path, '*.txt')): inputfile = open(filename, 'rb', 0) print("Current file being read: " + filename) is_in_set = False while True: ...
2
2009-11-18T22:13:09Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,759,908
<pre><code>f=0 for line in open("file"): if "End Set" in line: f=0 if "Date" in line: f=1 elif f: print line.strip() </code></pre> <p>output</p> <pre><code>$ ./python.py 12 34 56 25 67 90 34 56 89 25 67 89 </code></pre>
0
2009-11-18T23:38:29Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,760,405
<p>The first thing I'd do is build a generator that uses a simple state machine to extract the data from a sequence:</p> <pre><code>def extract_data(seq): state = "Waiting" for item in seq: if state == "Waiting": if item.startswith("Set"): state = "SkippingDateLine" ...
2
2009-11-19T01:47:04Z
[ "python" ]
Python: Issue reading data lines multiple times from a file
1,759,320
<p>I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file -</p> <pre><code>Set : 1 Date: 10212009 12 34 56 25 67 90 End Set +++++++++ Set: 2 Date: 10222009 34 56 89 25 67 89 End Set </code></pre> ...
0
2009-11-18T21:48:27Z
1,760,441
<p>I'd probably do something even easier:</p> <pre><code>grep -E '[0-9][0-9] [0-9][0-9] [0-9][0-9]' *.txt </code></pre> <p><a href="http://gnuwin32.sourceforge.net/packages/grep.htm" rel="nofollow">grep is available on Win32</a></p>
1
2009-11-19T01:59:30Z
[ "python" ]
How to merge duplicates in 2D python arrays
1,759,369
<p>I have a set of data similar to this:</p> <pre><code># Start_Time End_Time Call_Type Info 1 13:14:37.236 13:14:53.700 Ping1 RTT(Avr):160ms 2 13:14:58.955 13:15:29.984 Ping2 RTT(Avr):40ms 3 13:19:12.754 13:19:14.757 Ping3_1 RTT(Avr):620ms 3 13:19:12.754 Ping3_2 RTT...
0
2009-11-18T21:55:07Z
1,759,511
<p>Assuming the duplicates are adjacent you can use a generator like this. I guess you already have some code to average the pings</p> <pre><code>def average_pings(ping1, ping2): pass def merge_pings(seq): prev_key=prev_key=None for item in seq: key = item.split()[:2] if key == prev_key: ...
0
2009-11-18T22:11:47Z
[ "python", "arrays", "merge", "duplicates" ]
How to merge duplicates in 2D python arrays
1,759,369
<p>I have a set of data similar to this:</p> <pre><code># Start_Time End_Time Call_Type Info 1 13:14:37.236 13:14:53.700 Ping1 RTT(Avr):160ms 2 13:14:58.955 13:15:29.984 Ping2 RTT(Avr):40ms 3 13:19:12.754 13:19:14.757 Ping3_1 RTT(Avr):620ms 3 13:19:12.754 Ping3_2 RTT...
0
2009-11-18T21:55:07Z
1,759,516
<p>I'm not sure on how your data is structured, so I'll assume a list of dicts for duck typing purposes.</p> <p>I'm also assuming the real primary key of your dataset is Start.</p> <pre><code>for i in range(len(dataset)-1): #Detect duplicates, assuming they are sorted properly if dataset[i]["Start"] == dataset[i+...
0
2009-11-18T22:12:18Z
[ "python", "arrays", "merge", "duplicates" ]
How to merge duplicates in 2D python arrays
1,759,369
<p>I have a set of data similar to this:</p> <pre><code># Start_Time End_Time Call_Type Info 1 13:14:37.236 13:14:53.700 Ping1 RTT(Avr):160ms 2 13:14:58.955 13:15:29.984 Ping2 RTT(Avr):40ms 3 13:19:12.754 13:19:14.757 Ping3_1 RTT(Avr):620ms 3 13:19:12.754 Ping3_2 RTT...
0
2009-11-18T21:55:07Z
1,760,752
<p>Assuming your duplicates are adjacent (as they're shown on your question), <a href="http://docs.python.org/library/itertools.html?highlight=itertools.groupby#itertools.groupby" rel="nofollow">itertools.groupby</a> is the ideal way to identify them as duplicates (with a little help from <a href="http://docs.python.or...
1
2009-11-19T03:42:51Z
[ "python", "arrays", "merge", "duplicates" ]
How can I account for period (AM/PM) with datetime.strptime?
1,759,455
<p>Specifically I have code that simplifies to this:</p> <pre><code>from datetime import datetime date_string = '2009-11-29 03:17 PM' format = '%Y-%m-%d %H:%M %p' my_date = datetime.strptime(date_string, format) # This prints '2009-11-29 03:17 AM' print my_date.strftime(format) </code></pre> <p>What gives? Does Pyth...
54
2009-11-18T22:04:41Z
1,759,485
<p>The Python <a href="https://docs.python.org/2/library/time.html#time.strftime"><code>time.strftime</code> docs</a> say:</p> <blockquote> <p>When used with the strptime() function, the <strong><code>%p</code></strong> directive only affects the output hour field if the <strong><code>%I</code></strong> directive ...
75
2009-11-18T22:07:33Z
[ "python", "datetime", "strptime" ]
How can I account for period (AM/PM) with datetime.strptime?
1,759,455
<p>Specifically I have code that simplifies to this:</p> <pre><code>from datetime import datetime date_string = '2009-11-29 03:17 PM' format = '%Y-%m-%d %H:%M %p' my_date = datetime.strptime(date_string, format) # This prints '2009-11-29 03:17 AM' print my_date.strftime(format) </code></pre> <p>What gives? Does Pyth...
54
2009-11-18T22:04:41Z
1,759,494
<p>Try replacing %H (Hour on a 24-hour clock) with %I (Hour on a 12-hour clock) ?</p>
7
2009-11-18T22:08:59Z
[ "python", "datetime", "strptime" ]
How can I account for period (AM/PM) with datetime.strptime?
1,759,455
<p>Specifically I have code that simplifies to this:</p> <pre><code>from datetime import datetime date_string = '2009-11-29 03:17 PM' format = '%Y-%m-%d %H:%M %p' my_date = datetime.strptime(date_string, format) # This prints '2009-11-29 03:17 AM' print my_date.strftime(format) </code></pre> <p>What gives? Does Pyth...
54
2009-11-18T22:04:41Z
1,759,497
<p>You used <code>%H</code> (24 hour format) instead of <code>%I</code> (12 hour format).</p>
9
2009-11-18T22:09:19Z
[ "python", "datetime", "strptime" ]
How can I account for period (AM/PM) with datetime.strptime?
1,759,455
<p>Specifically I have code that simplifies to this:</p> <pre><code>from datetime import datetime date_string = '2009-11-29 03:17 PM' format = '%Y-%m-%d %H:%M %p' my_date = datetime.strptime(date_string, format) # This prints '2009-11-29 03:17 AM' print my_date.strftime(format) </code></pre> <p>What gives? Does Pyth...
54
2009-11-18T22:04:41Z
1,759,498
<pre><code>format = '%Y-%m-%d %H:%M %p' </code></pre> <p>The format is using <code>%H</code> instead of <code>%I</code>. Since <code>%H</code> is the "24-hour" format, it's likely just discarding the <code>%p</code> information. It works just fine if you change the <code>%H</code> to <code>%I</code>.</p>
16
2009-11-18T22:09:19Z
[ "python", "datetime", "strptime" ]
How to generate a graphic on the fly with cherrypy
1,759,608
<p>I am developing a small web application using cherrypy and I would like to generate some graphs from the data stored in a database. The web pages with tables are easy and I plan to use matplotlib for the graphs themselves, but how do I set the content-type for the method so they return images instead of plain text?...
3
2009-11-18T22:27:43Z
1,759,690
<p>You can change the content type of the response:</p> <pre><code>cherrypy.response.headers['Content-Type'] = "image/png" </code></pre>
0
2009-11-18T22:45:11Z
[ "python", "dynamic", "graph", "cherrypy" ]
How to generate a graphic on the fly with cherrypy
1,759,608
<p>I am developing a small web application using cherrypy and I would like to generate some graphs from the data stored in a database. The web pages with tables are easy and I plan to use matplotlib for the graphs themselves, but how do I set the content-type for the method so they return images instead of plain text?...
3
2009-11-18T22:27:43Z
1,759,906
<p>You need to set the content-type header of the response manually, either in the app config, using the <code>response.headers</code> tool, or in the handler method.</p> <p>In the handler method, there are two options covered on the <a href="http://tools.cherrypy.org/wiki/MimeDecorator">MimeDecorator</a> page of the ...
6
2009-11-18T23:38:28Z
[ "python", "dynamic", "graph", "cherrypy" ]
Remove &#13; from python string
1,759,619
<p>When you run something through <code>popen</code> in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?</p>
1
2009-11-18T22:29:22Z
1,759,635
<p>You can simply do</p> <pre><code>s = s.replace('\r\n', '\n') </code></pre> <p>to replace all occurrences of CRNL with just NL, which seems to be what you want.</p>
4
2009-11-18T22:31:35Z
[ "python", "popen", "newline" ]
Remove &#13; from python string
1,759,619
<p>When you run something through <code>popen</code> in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?</p>
1
2009-11-18T22:29:22Z
1,759,636
<pre><code>buffer = "&lt;text from your subprocess here&gt;\r\n" no_cr = buffer.replace("\r\n", "\n") </code></pre>
2
2009-11-18T22:31:55Z
[ "python", "popen", "newline" ]
Remove &#13; from python string
1,759,619
<p>When you run something through <code>popen</code> in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?</p>
1
2009-11-18T22:29:22Z
1,759,683
<p>If they are at the end of the string(s), I would suggest to use:</p> <pre><code>buffer = "&lt;text from your subprocess here&gt;\r\n" no_cr = buffer.rstrip("\r\n") </code></pre> <p>You can also use rstrip() without parameters which will remove whitespace as well.</p>
2
2009-11-18T22:41:36Z
[ "python", "popen", "newline" ]
Remove &#13; from python string
1,759,619
<p>When you run something through <code>popen</code> in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?</p>
1
2009-11-18T22:29:22Z
1,761,038
<p>Actually, you can simply do the following:</p> <pre><code>s = s.strip() </code></pre> <p>This will remove any extraneous whitespace, including CR and LFs, leading or trailing the string.</p> <pre><code>s = s.rstrip() </code></pre> <p>Does the same, but only trailing the string.</p> <p>That is:</p> <pre><code>s...
0
2009-11-19T05:17:32Z
[ "python", "popen", "newline" ]
Remove &#13; from python string
1,759,619
<p>When you run something through <code>popen</code> in Python, the results come in from the buffer with the CR-LF decimal value of a carriage return (13) at the end of each line. How do you remove this from a Python string?</p>
1
2009-11-18T22:29:22Z
1,761,077
<p>You can do <code>s = s.replace('\r', '')</code> too.</p>
0
2009-11-19T05:27:23Z
[ "python", "popen", "newline" ]
What Is The Best Python Zip Module To Handle Large Files?
1,759,736
<p>EDIT: Specifically compression and extraction speeds.</p> <p>Any Suggestions?</p> <p>Thanks</p>
6
2009-11-18T22:57:27Z
1,760,715
<p>So I made a random-ish large zipfile:</p> <pre><code>$ ls -l *zip -rw-r--r-- 1 aleax 5000 115749854 Nov 18 19:16 large.zip $ unzip -l large.zip | wc 23396 93633 2254735 </code></pre> <p>i.e., 116 MB with 23.4K files in it, and timed things:</p> <pre><code>$ time unzip -d /tmp large.zip &gt;/dev/null real...
13
2009-11-19T03:32:10Z
[ "python", "performance", "compression", "zip", "extraction" ]
What Is The Best Python Zip Module To Handle Large Files?
1,759,736
<p>EDIT: Specifically compression and extraction speeds.</p> <p>Any Suggestions?</p> <p>Thanks</p>
6
2009-11-18T22:57:27Z
1,763,408
<p>For handling large files without loading them into memory, use the new stream-based methods in Python 2.6's version of <code>zipfile</code>, such as <code>ZipFile.open</code>. <strong>Don't</strong> use <code>extract</code> or <code>extractall</code> unless you have strongly sanitised the filenames in the ZIP.</p> ...
4
2009-11-19T13:47:46Z
[ "python", "performance", "compression", "zip", "extraction" ]
Limit Python VM memory
1,760,025
<p>I'm trying to find a way to limit the memory available for the Python VM, as the option "-Xmx" in the Java VM does. (The idea is to be able to play with the MemoryError exception)</p> <p>I'm not sure this option exist but there may be a solution using a command of the OS to "isolate" a process and its memory.</p> ...
18
2009-11-19T00:05:15Z
1,760,046
<p>On *nix you can play around with the <code>ulimit</code> command, specifically the -m (max memory size) and -v (virtual memory).</p>
11
2009-11-19T00:09:04Z
[ "python", "memory", "jvm" ]
Limit Python VM memory
1,760,025
<p>I'm trying to find a way to limit the memory available for the Python VM, as the option "-Xmx" in the Java VM does. (The idea is to be able to play with the MemoryError exception)</p> <p>I'm not sure this option exist but there may be a solution using a command of the OS to "isolate" a process and its memory.</p> ...
18
2009-11-19T00:05:15Z
1,760,075
<p>Don't waste any time on this.</p> <p>Instead, if you want to play with <code>MemoryError</code> exceptions create a design that isolates object construction so you can test it.</p> <p>Instead of this</p> <pre><code>for i in range(1000000000000000000000): try: y = AnotherClass() except MemoryError:...
3
2009-11-19T00:18:38Z
[ "python", "memory", "jvm" ]
Limit Python VM memory
1,760,025
<p>I'm trying to find a way to limit the memory available for the Python VM, as the option "-Xmx" in the Java VM does. (The idea is to be able to play with the MemoryError exception)</p> <p>I'm not sure this option exist but there may be a solution using a command of the OS to "isolate" a process and its memory.</p> ...
18
2009-11-19T00:05:15Z
5,433,168
<p>On Linux I'm using the <a href="http://docs.python.org/library/resource.html">resource</a> module:</p> <pre><code>import resource resource.setrlimit(resource.RLIMIT_AS, (megs * 1048576L, -1L)) </code></pre>
33
2011-03-25T13:34:36Z
[ "python", "memory", "jvm" ]