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
How can I create a Word document using Python?
1,035,183
<p>I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I <a href="http://www.xhtml2pdf.com/">programatically convert</a> to a PDF file. However, my client is now requesting that...
32
2009-06-23T20:59:08Z
1,035,392
<p>A couple ways you can create Word documents using Python:</p> <ul> <li>Use COM automation to create a document using the MS Word object model (using <code>pywin32</code>). <a href="http://python.net/crew/pirx/spam7/" rel="nofollow">http://python.net/crew/pirx/spam7/</a></li> <li>Automate OpenOffice using Python: <a...
33
2009-06-23T21:36:24Z
[ "python", "xml", "xslt", "ms-word" ]
How can I create a Word document using Python?
1,035,183
<p>I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I <a href="http://www.xhtml2pdf.com/">programatically convert</a> to a PDF file. However, my client is now requesting that...
32
2009-06-23T20:59:08Z
1,035,433
<p>1) If you want to just stick another step on the end of your current pipeline, there are several options out there now for converting PDF files to Word files. I haven't tried <a href="http://download.cnet.com/123PDFConverter-PDF-to-Word-Converter/3000-10743%5F4-10544297.html" rel="nofollow">123PDFConverter</a>, but ...
2
2009-06-23T21:45:05Z
[ "python", "xml", "xslt", "ms-word" ]
How can I create a Word document using Python?
1,035,183
<p>I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I <a href="http://www.xhtml2pdf.com/">programatically convert</a> to a PDF file. However, my client is now requesting that...
32
2009-06-23T20:59:08Z
1,135,357
<p>I have had to do something similar with python as well. It is <em>far</em> more manual work than I want, but documents created with pyRTF were causing Word and OpenOffice to crash and I didn't have the motivation to try to figure it out.</p> <p>I have found it simplest (but not ideal) to create a Word document temp...
1
2009-07-16T04:14:37Z
[ "python", "xml", "xslt", "ms-word" ]
How can I create a Word document using Python?
1,035,183
<p>I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I <a href="http://www.xhtml2pdf.com/">programatically convert</a> to a PDF file. However, my client is now requesting that...
32
2009-06-23T20:59:08Z
8,052,336
<p>I tried <a href="https://github.com/mikemaccana/python-docx">python-docx</a> with succes, it enables you to make and edit docx within Python</p>
19
2011-11-08T14:54:21Z
[ "python", "xml", "xslt", "ms-word" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
1,035,360
<pre><code>f = open("myfile", "rb") try: byte = f.read(1) while byte != "": # Do stuff with byte. byte = f.read(1) finally: f.close() </code></pre> <p>By suggestion of chrispy:</p> <pre><code>with open("myfile", "rb") as f: byte = f.read(1) while byte != "": # Do stuff with...
196
2009-06-23T21:31:28Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
1,035,419
<p>If the file is not too big that holding it in memory is a problem:</p> <pre><code>bytes_read = open("filename", "rb").read() for b in bytes_read: process_byte(b) </code></pre> <p>where process_byte represents some operation you want to perform on the passed-in byte.</p> <p>If you want to process a chunk at a ...
37
2009-06-23T21:43:03Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
1,035,456
<p>This generator yields bytes from a file, reading the file in chunks:</p> <pre><code>def bytes_from_file(filename, chunksize=8192): with open(filename, "rb") as f: while True: chunk = f.read(chunksize) if chunk: for b in chunk: yield b ...
109
2009-06-23T21:50:55Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
18,652,697
<p>To sum up all the brilliant points of chrispy, Skurmedel, Ben Hoyt and Peter Hansen, this would be the optimal solution for processing a binary file one byte at a time:</p> <pre><code>with open("myfile", "rb") as f: while True: byte = f.read(1) if not byte: break do_stuff_wit...
13
2013-09-06T07:55:55Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
20,014,805
<p>To read a file — one byte at a time (ignoring the buffering) — you could use the <a href="http://docs.python.org/2/library/functions.html#iter">two-argument <code>iter(callable, sentinel)</code> built-in function</a>:</p> <pre><code>from functools import partial with open(filename, 'rb') as file: for byte...
16
2013-11-16T04:47:57Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
31,159,949
<p>If you have a lot of binary data to read, you might want to consider the <a href="https://docs.python.org/3/library/struct.html" rel="nofollow">struct module</a>. It is documented as converting "between C and Python types", but of course, bytes are bytes, and whether those were created as C types does not matter. ...
1
2015-07-01T11:24:30Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
32,720,771
<p>my solution actually returns a string:</p> <pre><code>&gt;&gt;&gt; from StringIO import StringIO &gt;&gt;&gt; from functools import partial # credit to J.F. Sebastian &gt;&gt;&gt; fl = StringIO('string\x00string2\x00') # simulated "file" object &gt;&gt;&gt; bytearray(iter(partial(fl.read, 1), b'\x00')).__str__() # ...
0
2015-09-22T15:20:23Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
37,222,446
<blockquote> <h1>Reading binary file in Python and looping over each byte</h1> </blockquote> <p>Let's make a file:</p> <pre><code>&gt;&gt;&gt; with open('foobarbaz', 'w') as f: ... f.write('foo\nbar\nbaz') </code></pre> <p>Now let's iterate over it, using the <code>rb</code> flag (read mode, bytes mode). </p> ...
0
2016-05-14T04:16:06Z
[ "python", "file-io", "binary" ]
Reading binary file in Python and looping over each byte
1,035,340
<p>In Python, how do I read in a binary file and loop over each byte of that file?</p>
198
2009-06-23T21:26:52Z
39,683,695
<p>Python 3, read all of the file at once:</p> <pre><code>with open("filename", "rb") as binary_file: # Read the whole file at once data = binary_file.read() print(data) </code></pre> <p>You can iterate whatever you want using <code>data</code> variable.</p>
0
2016-09-25T05:26:37Z
[ "python", "file-io", "binary" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
1,035,512
<p>You haven't provided enough information - this depends on the specifics of the object you are creating and what else you're doing with it in the loop. If the object does not create circular references, it should be deallocated on the next iteration. For example, the code</p> <pre><code>for x in range(100000): obj...
16
2009-06-23T22:07:41Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
1,035,526
<p>This is an old error that was corrected for some types in python 2.5. What was happening was that python was not so good at collecting things like empty lists/dictionaries/tupes/floats/ints. In python 2.5 this was fixed...mostly. However floats and ints are singletons for comparisons so once one of those is created ...
12
2009-06-23T22:11:24Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
1,035,528
<p>Here's one thing you can do at the REPL to force a dereferencing of a variable:</p> <pre><code>&gt;&gt;&gt; x = 5 &gt;&gt;&gt; x 5 &gt;&gt;&gt; del x &gt;&gt;&gt; x Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'x' is not defined </code></pre>
1
2009-06-23T22:11:59Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
1,036,054
<p>If you're creating circular references, your objects won't be deallocated immediately, but have to wait for a GC cycle to run.</p> <p>You could use the <a href="https://docs.python.org/3/library/weakref.html" rel="nofollow">weakref</a> module to address this problem, or explicitly del your objects after use.</p>
3
2009-06-24T01:28:56Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
2,037,626
<p>I found that in my case (with Python 2.5.1), with circular references involving classes that have <code>__del__()</code> methods, not only was garbage collection not happening in a timely manner, the <code>__del__()</code> methods of my objects were never getting called, even when the script exited. So I used <a hre...
3
2010-01-10T16:30:33Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Python garbage collection
1,035,489
<p>I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?</p>
23
2009-06-23T21:59:01Z
4,060,791
<p>I think this is circular reference (though the question isn't explicit about this information.)</p> <p>One way to solve this problem is to manually invoke garbage collection. When you manually run garbage collector, it will sweep circular referenced objects too.</p> <pre><code>import gc for i in xrange(10000): ...
12
2010-10-30T21:37:29Z
[ "python", "optimization", "memory-management", "garbage-collection" ]
Google App Engine cannot find gdata module
1,035,554
<p>I can run a simple "Hello World" Google App Engine application on localhost with no problems. However, when I add the line "import gdata.auth" to my Python script I get "ImportError: No module named gdata.auth".</p> <p>I have installed the gdata module and added the following line to my .bashrc:</p> <pre>export P...
4
2009-06-23T22:19:58Z
1,035,670
<p>try adding this to your script:</p> <pre><code>import sys sys.path.append('&lt;directory where gdata.auth module is saved&gt;') import gdata.auth </code></pre>
0
2009-06-23T22:48:49Z
[ "python", "google-app-engine", "osx", "gdata", "google-data-api" ]
Google App Engine cannot find gdata module
1,035,554
<p>I can run a simple "Hello World" Google App Engine application on localhost with no problems. However, when I add the line "import gdata.auth" to my Python script I get "ImportError: No module named gdata.auth".</p> <p>I have installed the gdata module and added the following line to my .bashrc:</p> <pre>export P...
4
2009-06-23T22:19:58Z
1,036,152
<p>Your .bashrc is not known to Google App Engine. Make sure the <code>gdata</code> directory (with all its proper contents) is under your application's main directory!</p> <p>See <a href="http://code.google.com/appengine/articles/gdata.html">this article</a>, particularly (and I quote):</p> <blockquote> <p>To use ...
9
2009-06-24T02:27:38Z
[ "python", "google-app-engine", "osx", "gdata", "google-data-api" ]
Google App Engine cannot find gdata module
1,035,554
<p>I can run a simple "Hello World" Google App Engine application on localhost with no problems. However, when I add the line "import gdata.auth" to my Python script I get "ImportError: No module named gdata.auth".</p> <p>I have installed the gdata module and added the following line to my .bashrc:</p> <pre>export P...
4
2009-06-23T22:19:58Z
4,379,347
<p>The gdata client library installation script installs the modules in the wrong directory for ubuntu python installation.</p> <pre><code>sudo mv /usr/local/lib/python2.6/dist-packages/* /usr/lib/python2.6/dist-packages </code></pre>
1
2010-12-07T16:58:42Z
[ "python", "google-app-engine", "osx", "gdata", "google-data-api" ]
problem running scons
1,035,581
<p>I am trying to get started with <a href="http://www.scons.org/" rel="nofollow">scons</a>. I have Python 3.0.1 and downloaded Scons 1.2.0; when I try to run scons I get the following error. Am I doing something wrong here?</p> <pre><code>C:\tmp\scons&gt;c:\appl\python\3.0.1\Scripts\scons Traceback (most recent call ...
1
2009-06-23T22:25:00Z
1,035,633
<p>That's Python 2 syntax. I assume scons doesn't run on Python 3. You need to run it using Python 2. </p>
15
2009-06-23T22:37:20Z
[ "python", "scons" ]
Sensible python source line wrapping for printout
1,035,721
<p>I am working on a latex document that will require typesetting significant amounts of python source code. I'm using <a href="http://pygments.org/" rel="nofollow">pygments</a> (the python module, not the online demo) to encapsulate this python in latex, which works well except in the case of long individual lines - w...
2
2009-06-23T23:07:54Z
1,035,749
<p>I'd check a reformat tool in an editor like NetBeans.</p> <p>When you reformat java it properly fixes the lengths of lines both inside and outside of comments, if the same algorithm were applied to Python, it would work.</p> <p>For Java it allows you to set any wrapping width and a bunch of other parameters. I'd ...
1
2009-06-23T23:15:58Z
[ "python", "latex", "syntax-highlighting", "code-formatting", "pygments" ]
Sensible python source line wrapping for printout
1,035,721
<p>I am working on a latex document that will require typesetting significant amounts of python source code. I'm using <a href="http://pygments.org/" rel="nofollow">pygments</a> (the python module, not the online demo) to encapsulate this python in latex, which works well except in the case of long individual lines - w...
2
2009-06-23T23:07:54Z
1,035,922
<p>You might want to extend your current approach a bit, but using the <a href="http://docs.python.org/library/tokenize.html" rel="nofollow">tokenize</a> module from the standard library to determine where to put your line breaks. That way you can see the actual tokens (COMMENT, STRING, etc.) of your source code rathe...
3
2009-06-24T00:15:28Z
[ "python", "latex", "syntax-highlighting", "code-formatting", "pygments" ]
Sensible python source line wrapping for printout
1,035,721
<p>I am working on a latex document that will require typesetting significant amounts of python source code. I'm using <a href="http://pygments.org/" rel="nofollow">pygments</a> (the python module, not the online demo) to encapsulate this python in latex, which works well except in the case of long individual lines - w...
2
2009-06-23T23:07:54Z
1,188,601
<p>I use the <code>listings</code> package in LaTeX to insert source code; it does syntax highlight, linebreaks et al.</p> <p>Put the following in your preamble:</p> <pre><code>\usepackage{listings} %\lstloadlanguages{Python} # Load only these languages \newcommand{\MyHookSign}{\hbox{\ensuremath\hookleftarrow}} \lst...
2
2009-07-27T14:47:04Z
[ "python", "latex", "syntax-highlighting", "code-formatting", "pygments" ]
Setting the flags field of the IP header
1,035,799
<p>I have a simple Python script that uses the socket module to send a UDP packet. The script works fine on my Windows box, but on my Ubuntu Linux PC the packet it sends is slightly different. On Windows the flags field in the IP header is zero, but using the same code on Linux created a packet with the flags field set...
5
2009-06-23T23:33:21Z
1,035,871
<p><a href="http://construct.wikispaces.com/intro" rel="nofollow">construct</a> might do the job? </p>
1
2009-06-23T23:56:22Z
[ "python", "sockets" ]
Setting the flags field of the IP header
1,035,799
<p>I have a simple Python script that uses the socket module to send a UDP packet. The script works fine on my Windows box, but on my Ubuntu Linux PC the packet it sends is slightly different. On Windows the flags field in the IP header is zero, but using the same code on Linux created a packet with the flags field set...
5
2009-06-23T23:33:21Z
1,035,878
<p>I'm guessing that the flags field is actually set to 2 = b010 instead of 4 - flags equal to 4 is an invalid IP packet. Remember that flags is a 3 bit value in the <a href="http://en.wikipedia.org/wiki/IPv4#Packet%5Fstructure" rel="nofollow">IP Header</a>. I would expect to see UDP datagrams with a flags value of 2 w...
2
2009-06-23T23:58:16Z
[ "python", "sockets" ]
Setting the flags field of the IP header
1,035,799
<p>I have a simple Python script that uses the socket module to send a UDP packet. The script works fine on my Windows box, but on my Ubuntu Linux PC the packet it sends is slightly different. On Windows the flags field in the IP header is zero, but using the same code on Linux created a packet with the flags field set...
5
2009-06-23T23:33:21Z
1,044,918
<p>Here's the route I ended up taking. I followed the link posted by SashaN in the comments of D.Shwley's answer and learned a little bit about why the "don't fragment" bit is set in Linux's UDP packets. Turns out it has something to do with PMTU discovery. Long story short, you can clear the don't fragment bit from yo...
6
2009-06-25T16:34:12Z
[ "python", "sockets" ]
Any Python Script to Save Websites Like Firefox?
1,035,825
<p>I am tired of clicking "File" and then "Save Page As" in Firefox when I want to save some websites.</p> <p>Is there any script to do this in Python? I would like to save the pictures and css files so that when I read it offline, it looks normal.</p>
2
2009-06-23T23:40:19Z
1,035,842
<p>probably a tool like <a href="http://www.gnu.org/software/wget/" rel="nofollow">wget</a> is more appropriate for this type of thing.</p>
1
2009-06-23T23:45:21Z
[ "python" ]
Any Python Script to Save Websites Like Firefox?
1,035,825
<p>I am tired of clicking "File" and then "Save Page As" in Firefox when I want to save some websites.</p> <p>Is there any script to do this in Python? I would like to save the pictures and css files so that when I read it offline, it looks normal.</p>
2
2009-06-23T23:40:19Z
1,035,850
<p>This is a non-Python answer and I'm not sure what your machine is running, but have you consider using a <a href="http://stackoverflow.com/questions/522290/suggestions-for-a-site-ripper">site ripper</a> such as wget? </p> <pre><code>import os cmd = 'wget &lt;parameters&gt;' os.system(cmd) </code></pre>
0
2009-06-23T23:47:38Z
[ "python" ]
Any Python Script to Save Websites Like Firefox?
1,035,825
<p>I am tired of clicking "File" and then "Save Page As" in Firefox when I want to save some websites.</p> <p>Is there any script to do this in Python? I would like to save the pictures and css files so that when I read it offline, it looks normal.</p>
2
2009-06-23T23:40:19Z
1,035,851
<p>You could use wget</p> <p>wget -m -k -E [url]</p> <pre><code>-E, --html-extension save HTML documents with `.html' extension. -m, --mirror shortcut for -N -r -l inf --no-remove-listing. -k, --convert-links make links in downloaded HTML point to local files. </code></pre>
8
2009-06-23T23:47:44Z
[ "python" ]
Any Python Script to Save Websites Like Firefox?
1,035,825
<p>I am tired of clicking "File" and then "Save Page As" in Firefox when I want to save some websites.</p> <p>Is there any script to do this in Python? I would like to save the pictures and css files so that when I read it offline, it looks normal.</p>
2
2009-06-23T23:40:19Z
1,035,855
<p>Like Cobbal stated, this is largely what wget is designed to do. I believe there's some flags/arguments that you can set to make it download the entire page, CSS + all. I suggest just alias-ing into something more convenient to type, or tossing it into a quick script.</p>
1
2009-06-23T23:48:29Z
[ "python" ]
Any Python Script to Save Websites Like Firefox?
1,035,825
<p>I am tired of clicking "File" and then "Save Page As" in Firefox when I want to save some websites.</p> <p>Is there any script to do this in Python? I would like to save the pictures and css files so that when I read it offline, it looks normal.</p>
2
2009-06-23T23:40:19Z
1,036,063
<p>Have you looked at <a href="http://www.httrack.com/" rel="nofollow">HTTrack</a>?</p>
0
2009-06-24T01:34:49Z
[ "python" ]
Recursively convert python object graph to dictionary
1,036,409
<p>I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. </p> <p>I found <a href="http://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields">this question about cre...
16
2009-06-24T04:30:36Z
1,036,435
<p>I don't know what is the purpose of checking for basestring or object is? also <strong>dict</strong> will not contain any callables unless you have attributes pointing to such callables, but in that case isn't that part of object?</p> <p>so instead of checking for various types and values, let todict convert the ob...
4
2009-06-24T04:41:47Z
[ "python", "python-2.6" ]
Recursively convert python object graph to dictionary
1,036,409
<p>I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. </p> <p>I found <a href="http://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields">this question about cre...
16
2009-06-24T04:30:36Z
1,036,575
<p>In Python there are many ways of making objects behave slightly differently, like metaclasses and whatnot, and it can override <strong>getattr</strong> and thereby have "magical" attributes you can't see through <strong>dict</strong>, etc. In short, it's unlikely that you are going to get a 100% complete picture in ...
2
2009-06-24T05:46:05Z
[ "python", "python-2.6" ]
Recursively convert python object graph to dictionary
1,036,409
<p>I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. </p> <p>I found <a href="http://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields">this question about cre...
16
2009-06-24T04:30:36Z
1,118,038
<p>An amalgamation of my own attempt and clues derived from Anurag Uniyal and Lennart Regebro's answers works best for me:</p> <pre><code>def todict(obj, classkey=None): if isinstance(obj, dict): data = {} for (k, v) in obj.items(): data[k] = todict(v, classkey) return data ...
24
2009-07-13T07:06:27Z
[ "python", "python-2.6" ]
Recursively convert python object graph to dictionary
1,036,409
<p>I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. </p> <p>I found <a href="http://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields">this question about cre...
16
2009-06-24T04:30:36Z
16,993,744
<p>A slow but easy way to do this is to use <code>jsonpickle</code> to convert the object to a JSON string and then <code>json.loads</code> to convert it back to a python dictionary:</p> <p><code>dict = json.loads(jsonpickle.encode( obj, unpicklable=False ))</code></p>
1
2013-06-07T22:12:37Z
[ "python", "python-2.6" ]
Recursively convert python object graph to dictionary
1,036,409
<p>I'm trying to convert the data from a simple object graph into a dictionary. I don't need type information or methods and I don't need to be able to convert it back to an object again. </p> <p>I found <a href="http://stackoverflow.com/questions/61517/python-dictionary-from-an-objects-fields">this question about cre...
16
2009-06-24T04:30:36Z
22,679,824
<p>I realize that this answer is a few years too late, but I thought it might be worth sharing since it's a Python 3.3+ compatible modification to the original solution by @Shabbyrobe that has generally worked well for me:</p> <pre><code>import collections try: # Python 2.7+ basestring except NameError: # Python...
0
2014-03-27T06:24:19Z
[ "python", "python-2.6" ]
Django: Model name clash
1,036,506
<p>I am trying to use different open source apps in my project. Problem is that there is a same Model name used by two different apps with their own model definition. </p> <p>I tried using:</p> <pre><code> class Meta: db_table = "db_name" </code></pre> <p>but it didn't work. I am still getting field name ...
3
2009-06-24T05:19:37Z
1,045,135
<p>The problem is that both Satchmo and Pinax have a Contact model with a ForeignKey to User. Django tries to add a "contact_set" reverse relationship attribute to User for each of those ForeignKeys, so there is a clash.</p> <p>The solution is to add something like related_name="pinax_contact_set" as an argument to t...
5
2009-06-25T17:16:28Z
[ "python", "django", "django-models" ]
Python's asyncore to periodically send data using a variable timeout. Is there a better way?
1,036,646
<p>I wanted to write a server that a client could connect to and receive periodic updates without having to poll. The problem I have experienced with asyncore is that if you do not return true when dispatcher.writable() is called, you have to wait until after the asyncore.loop has timed out (default is 30s).</p> <p>T...
8
2009-06-24T06:11:04Z
1,036,817
<p>The "select law" doesn't apply to your case, as you have not only client-triggered (pure server) activities, but also time-triggered activities - this is precisely what the select timeout is for. What the law should really say is "if you specify a timeout, make sure you actually have to do something useful when the ...
4
2009-06-24T07:10:18Z
[ "python", "sockets", "asynchronous" ]
Python's asyncore to periodically send data using a variable timeout. Is there a better way?
1,036,646
<p>I wanted to write a server that a client could connect to and receive periodic updates without having to poll. The problem I have experienced with asyncore is that if you do not return true when dispatcher.writable() is called, you have to wait until after the asyncore.loop has timed out (default is 30s).</p> <p>T...
8
2009-06-24T06:11:04Z
1,036,918
<p>I would use Twisted, long time since I used asyncore but I think this should be the twisted equivalent (not tested, written from memory):</p> <pre><code>from twisted.internet import reactor, protocol import time UPDATE_PERIOD = 4.0 class MyClient(protocol.Protocol): def connectionMade(self): self.upd...
1
2009-06-24T07:38:11Z
[ "python", "sockets", "asynchronous" ]
Python's asyncore to periodically send data using a variable timeout. Is there a better way?
1,036,646
<p>I wanted to write a server that a client could connect to and receive periodic updates without having to poll. The problem I have experienced with asyncore is that if you do not return true when dispatcher.writable() is called, you have to wait until after the asyncore.loop has timed out (default is 30s).</p> <p>T...
8
2009-06-24T06:11:04Z
2,703,669
<p>Maybe you can do this with <code>sched.scheduler</code>, like this (n.b. not tested):</p> <pre><code>import sched, asyncore, time # Create a scheduler with a delay function that calls asyncore.loop scheduler = sched.scheduler(time.time, lambda t: _poll_loop(t, time.time()) ) # Add the update timeouts with schedul...
3
2010-04-24T07:56:52Z
[ "python", "sockets", "asynchronous" ]
Python's asyncore to periodically send data using a variable timeout. Is there a better way?
1,036,646
<p>I wanted to write a server that a client could connect to and receive periodic updates without having to poll. The problem I have experienced with asyncore is that if you do not return true when dispatcher.writable() is called, you have to wait until after the asyncore.loop has timed out (default is 30s).</p> <p>T...
8
2009-06-24T06:11:04Z
4,956,882
<p>This is basically demiurgus' solution with the rough edges made round. It retains his basic idea, but prevents RuntimeErrors and busy loops and is tested. [Edit: resolved issues with modifying the scheduler during _delay]</p> <pre><code>class asynschedcore(sched.scheduler): """Combine sched.scheduler and asynco...
2
2011-02-10T12:08:57Z
[ "python", "sockets", "asynchronous" ]
Python's asyncore to periodically send data using a variable timeout. Is there a better way?
1,036,646
<p>I wanted to write a server that a client could connect to and receive periodic updates without having to poll. The problem I have experienced with asyncore is that if you do not return true when dispatcher.writable() is called, you have to wait until after the asyncore.loop has timed out (default is 30s).</p> <p>T...
8
2009-06-24T06:11:04Z
6,640,413
<p>Maybe I don't understand what the OP was trying to accomplish, but I just solved this issue using 1 thread that gets a weakref of each Channel(asyncore.dispatcher) object. This thread determines its own timing and will send the Channel an update periodically using a Queue in that channel. It gets the Queue from th...
0
2011-07-10T09:55:48Z
[ "python", "sockets", "asynchronous" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
1,036,757
<p>The only tool in Python for Javascript, that I am aware of is <a href="http://code.google.com/p/python-spidermonkey/" rel="nofollow">python-spidermonkey</a>. I have never used it though.</p> <p>With Jython you could (ab-)use <a href="http://httpunit.sourceforge.net/index.html" rel="nofollow">HttpUnit</a>. </p> <p>...
5
2009-06-24T06:49:31Z
[ "python", "html", "bots" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
1,036,758
<p>Well obviously python won't interpret the JS for you (though there may be modules out there that can). I suppose you need to convert the JS instructions to equivalent transformations in Python.</p> <p>I suppose ElementTree or BeautifulSoup would be good starting points to interpret the HTML structure.</p>
0
2009-06-24T06:49:46Z
[ "python", "html", "bots" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
1,036,761
<p>To execute JavaScript, you need to do much of what a full web browser does, except for the rendering. In particular, you need a JavaScript interpreter, in addition to the Python interpreter.</p> <p>One starting point might be <a href="http://pypi.python.org/pypi/python-spidermonkey" rel="nofollow">python-spidermonk...
0
2009-06-24T06:50:07Z
[ "python", "html", "bots" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
1,036,804
<p>You can try to leverage <a href="http://code.google.com/apis/v8/intro.html" rel="nofollow">V8</a>,</p> <blockquote> <p>V8 is Google's open source, high performance JavaScript engine. It is written in C++ and is used in Google Chrome, Google's open source browser.</p> </blockquote> <p>Calling it from <code>Python...
0
2009-06-24T07:05:08Z
[ "python", "html", "bots" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
1,037,143
<p>For the browser part of this you might want to look into Mechanize, which basically is a webbrowser implemented as a Python library. <a href="http://pypi.python.org/pypi/mechanize/0.1.11" rel="nofollow">http://pypi.python.org/pypi/mechanize/0.1.11</a> But as mentioned, the text n onClick is Javascript, and you'll ne...
0
2009-06-24T08:44:28Z
[ "python", "html", "bots" ]
Python Web-based Bot
1,036,660
<p>I am trying to write a Python-based Web Bot that can read and interpret an HTML page, then execute an onClick function and receive the resulting new HTML page. I can already read the HTML page and I can determine the functions to be called by the onClick command, but I have no idea how to execute those functions or ...
3
2009-06-24T06:15:59Z
5,873,989
<p>Why don't you just sniff what gets sent after the onclick event and replicate that with your bot?</p>
0
2011-05-03T18:29:05Z
[ "python", "html", "bots" ]
Simple Image Metrics with PIL
1,037,090
<p>I want to process uploaded photos with PIL and determine some "soft" image metrics like:</p> <ul> <li>is the image contrastful or dull?</li> <li>colorful or monochrome?</li> <li>bright or dark?</li> <li>is the image warm or cold (regarding light temperature)?</li> <li>is there a dominant hue?</li> </ul> <p>the met...
4
2009-06-24T08:30:47Z
1,037,217
<p>I don't think there are methods that give you a metric exactly for what you want, but the methods that it has, like RMS, takes you a long way there. To do things with color, you can split the image into one layer per color, and get the RMS on each layer, which tells you some of the things you want to know. You can a...
1
2009-06-24T09:01:52Z
[ "python", "python-imaging-library" ]
How to automatically create postgis database for Django testing?
1,037,388
<p>I'm trying to test my Django apps which run on a PostGIS database, by following the info in the <a href="http://docs.djangoproject.com/en/dev/topics/testing/" rel="nofollow">Django testing docs</a>.</p> <p>Normally I create a new database by copying a template:</p> <p>(as user postgres)</p> <pre><code>createdb -T...
4
2009-06-24T09:45:22Z
1,037,580
<p>It may be that your <code>DATABASE_USER</code> doesn't have permissions to create a new database/schema. </p> <p><hr /></p> <p><strong>Edit</strong></p> <p>If you read the source for the Django <code>test</code> command, you'll see that it always creates a test database. Further, it modifies your settings to r...
4
2009-06-24T10:36:48Z
[ "python", "django", "unit-testing" ]
How to automatically create postgis database for Django testing?
1,037,388
<p>I'm trying to test my Django apps which run on a PostGIS database, by following the info in the <a href="http://docs.djangoproject.com/en/dev/topics/testing/" rel="nofollow">Django testing docs</a>.</p> <p>Normally I create a new database by copying a template:</p> <p>(as user postgres)</p> <pre><code>createdb -T...
4
2009-06-24T09:45:22Z
1,041,848
<p>As S.Lott mentioned, use the standard <em>test</em> command.</p> <p>Using geodjango with postgis you'll need to add the following to your settings for the spatial templates to be created properly.</p> <p><strong>settings.py</strong></p> <pre><code>POSTGIS_SQL_PATH = 'C:\\Program Files\\PostgreSQL\\8.3\\share\\con...
2
2009-06-25T02:03:06Z
[ "python", "django", "unit-testing" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
1,037,423
<p>Use the <a href="http://urlgrabber.baseurl.org/" rel="nofollow">urlgrabber</a> library. This includes an HTTP handler for urllib2 that supports HTTP 1.1 and keepalive:</p> <pre><code>&gt;&gt;&gt; import urllib2 &gt;&gt;&gt; from urlgrabber.keepalive import HTTPHandler &gt;&gt;&gt; keepalive_handler = HTTPHandler() ...
28
2009-06-24T09:56:44Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
2,024,633
<p>Note that urlgrabber does not entirely work with python 2.6. I fixed the issues (I think) by making the following modifications in keepalive.py.</p> <p>In keepalive.HTTPHandler.do_open() remove this</p> <pre><code> if r.status == 200 or not HANDLE_ERRORS: return r </code></pre> <p>And insert this</p>...
4
2010-01-08T00:05:38Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
4,611,327
<p>Unfortunately keepalive.py was removed from urlgrabber on 25 Sep 2009 by the following change after urlgrabber was changed to depend on pycurl (which supports keep-alive):</p> <p><a href="http://yum.baseurl.org/gitweb?p=urlgrabber.git;a=commit;h=f964aa8bdc52b29a2c137a917c72eecd4c4dda94" rel="nofollow">http://yum.ba...
4
2011-01-06T02:51:07Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
6,163,023
<p>Or check out <a href="http://docs.python.org/library/httplib.html">httplib</a>'s HTTPConnection.</p>
7
2011-05-28T16:54:26Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
8,086,976
<p>Try <a href="https://github.com/shazow/urllib3">urllib3</a> which has the following features:</p> <ul> <li>Re-use the same socket connection for multiple requests (HTTPConnectionPool and HTTPSConnectionPool) (with optional client-side certificate verification).</li> <li>File posting (encode_multipart_formdata).</li...
11
2011-11-10T22:00:14Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
14,282,639
<p>Please avoid collective pain and use <a href="http://docs.python-requests.org/en/latest/" rel="nofollow">Requests</a> instead. It will do the right thing by default and use keep-alive if applicable.</p>
5
2013-01-11T16:39:05Z
[ "python", "http", "urllib2", "keep-alive" ]
Python urllib2 with keep alive
1,037,406
<p>How can I make a "keep alive" HTTP request using Python's urllib2?</p>
38
2009-06-24T09:50:43Z
27,090,369
<p>Here's a somewhat similar urlopen() that does keep-alive, though it's not threadsafe.</p> <pre><code>try: from http.client import HTTPConnection, HTTPSConnection except ImportError: from httplib import HTTPConnection, HTTPSConnection import select connections = {} def request(method, url, body=None, heade...
0
2014-11-23T15:06:01Z
[ "python", "http", "urllib2", "keep-alive" ]
Are there stack based variables in Python?
1,037,533
<p>If I do this:</p> <pre><code>def foo(): a = SomeObject() </code></pre> <p>Is 'a' destroyed immediately after leaving foo? Or does it wait for some GC to happen?</p>
5
2009-06-24T10:24:18Z
1,037,542
<p>Yes and no. The object will get destroyed after you leave foo (as long as nothing else has a reference to it), but whether it is immediate or not is an implementation detail, and will vary.</p> <p>In CPython (the standard python implementation), refcounting is used, so the item will immediately be destroyed. Ther...
18
2009-06-24T10:26:42Z
[ "python" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,037,705
<p>I think it's about not inventig the wheel. It would work, but why should you do that? All the GUI libraries you mentioned are stable and more or less bullet proofen.</p> <p>I could imagine that there are some experiments implementing a pure python library. But I never saw one. Everything about GUIs is hard work and...
2
2009-06-24T11:08:56Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,037,722
<p>The path of least effort and best results would be to learn what it takes to deploy an app using those existing GUI libraries.</p>
9
2009-06-24T11:14:16Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,037,740
<p>For one thing, all those libraries use different abstractions, so anything that worked with all of them is likely to wind up supporting a least-common-denominator set of functionality, <em>or</em> doing a LOT of work to use each to its fullest.</p>
3
2009-06-24T11:18:35Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,037,810
<p>Not really sure what you mean by "heavyweight."</p> <p>wx uses native controls on each platform, and is about as easy to use in Python as I can imagine; after all, GUI APIs are complex because GUIs can get complex.</p> <p>I think wx, for the effort required to build a window and the quality of what shows up on scr...
4
2009-06-24T11:36:05Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,037,851
<p>Tkinter is part of the python standard distribution and is installed by default. Expect to find this on all python installs where there is a graphical display in the first place. </p>
8
2009-06-24T11:45:45Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,038,265
<p>Notion of "pure python gui library" is wrong because ultimately you will be using system level calls and widgets, may be thru ctypes but that doesn't change the fact that if you start implementing your idea you will eventually become wxPython</p>
4
2009-06-24T13:15:55Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,038,303
<p>Principally what's wrong is that it's reinventing wheels that have already been done by the makers of GTK, Tk, Wx, QT and their ilk. While a pure python GUI is technically feasible, and projects such as <a href="http://anygui.sourceforge.net/">anygui</a> did attempt something similar, there is relatively little to ...
5
2009-06-24T13:24:50Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
Pure python gui library?
1,037,673
<p>Python has a lot of GUI libraries: tkinter, wxWidgets, pyGTK etc. But all this GUI needs to be installed and quite heavyweight, so it's a bit complex to deploy end-user GUI python apps that relay on mentioned GUI libraries.</p> <p>Recently, I have thought about python built-in ctypes. Theoretically, it's possible t...
5
2009-06-24T10:58:26Z
1,038,494
<p>starting in Python 2.7 and 3.1, Tk will look a lot better.</p> <p><a href="http://docs.python.org/dev/whatsnew/2.7.html#ttk-themed-widgets-for-tk">http://docs.python.org/dev/whatsnew/2.7.html#ttk-themed-widgets-for-tk</a></p> <p>"Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk widgets but ha...
9
2009-06-24T13:48:15Z
[ "python", "tkinter", "pyqt", "pygtk", "pyobjc" ]
gnuplot syntax error when using python
1,037,919
<p>I am just about to find out how python and gnuplot work together. On </p> <p><a href="http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module" rel="nofollow">http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module</a></p> <p>I found an introduction and I wanted to execute it on my Ubuntu machine....
1
2009-06-24T12:00:34Z
1,038,003
<p>Well the python interpreter thinks that while parsing there was an syntax error. Check again your quotes, make sure that for convenience sake you only use either double or single quotes in your entire script (except of course when you need to put in a literal quote like "'" or '"').</p> <p>If you are unsure what g...
0
2009-06-24T12:16:21Z
[ "python", "gnuplot" ]
gnuplot syntax error when using python
1,037,919
<p>I am just about to find out how python and gnuplot work together. On </p> <p><a href="http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module" rel="nofollow">http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module</a></p> <p>I found an introduction and I wanted to execute it on my Ubuntu machine....
1
2009-06-24T12:00:34Z
1,038,015
<p>I've been using pylab instead. <a href="http://www.scipy.org/PyLab" rel="nofollow">Pylab homepage</a></p> <p>From debian repos:</p> <pre><code>python-matplotlib - Python based plotting system in a style similar to Matlab. </code></pre>
-1
2009-06-24T12:19:09Z
[ "python", "gnuplot" ]
gnuplot syntax error when using python
1,037,919
<p>I am just about to find out how python and gnuplot work together. On </p> <p><a href="http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module" rel="nofollow">http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module</a></p> <p>I found an introduction and I wanted to execute it on my Ubuntu machine....
1
2009-06-24T12:00:34Z
1,038,173
<p>Your <code>demo.py</code> file is somehow corrupted - make sure that the open-parenthesis character is really that. Grub the installer from the <a href="http://gnuplot-py.sourceforge.net/" rel="nofollow">project page</a> to make sure.</p> <p>You can access the <a href="http://gnuplot-py.svn.sourceforge.net/viewvc/g...
0
2009-06-24T12:52:00Z
[ "python", "gnuplot" ]
gnuplot syntax error when using python
1,037,919
<p>I am just about to find out how python and gnuplot work together. On </p> <p><a href="http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module" rel="nofollow">http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module</a></p> <p>I found an introduction and I wanted to execute it on my Ubuntu machine....
1
2009-06-24T12:00:34Z
1,038,179
<p>If I copy and paste your code into Emacs I get this:</p> <pre><code>gp = Gnuplot.Gnuplot(persist = 1) gp('set data style lines') data1 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]] # The first data set (a quadratic) data2 = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] # The second data set (a straight line) plot1 = G...
0
2009-06-24T12:53:04Z
[ "python", "gnuplot" ]
gnuplot syntax error when using python
1,037,919
<p>I am just about to find out how python and gnuplot work together. On </p> <p><a href="http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module" rel="nofollow">http://wiki.aims.ac.za/mediawiki/index.php/Python:Gnuplot_module</a></p> <p>I found an introduction and I wanted to execute it on my Ubuntu machine....
1
2009-06-24T12:00:34Z
3,901,680
<p>Did you put the bangline in the first line? i.e:</p> <pre><code>#!/usr/bin/python </code></pre> <p>Looks like it is not the Python interpreter who is executing the file.</p>
2
2010-10-10T18:48:00Z
[ "python", "gnuplot" ]
Auto-incrementing attribute with custom logic in SQLAlchemy
1,038,126
<p><br /> I have a simple "Invoices" class with a "Number" attribute that has to be assigned by the application when the user saves an invoice. There are some constraints:</p> <p>1) the application is a (thin) client-server one, so whatever assigns the number must look out for collisions<br /> 2) Invoices has a "versi...
1
2009-06-24T12:42:21Z
1,038,154
<p>Is there any particular reason you don't just use a <code>default=</code> parameter in your column definition? (This can be an arbitrary Python callable).</p> <pre><code>def generate_invoice_number(): # special logic to generate a unique invoice number class Invoice(DeclarativeBase): __tablename__ = 'invo...
3
2009-06-24T12:47:25Z
[ "python", "sqlalchemy", "auto-increment" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
1,038,195
<p>I personally would use the list of row lists. Because the data for each row is always in the same order, you can easily sort by any of the columns by simply accessing that element in each of the lists. You can also easily count based on a particular column in each list, and make searches as well. It's basically as c...
10
2009-06-24T12:58:05Z
[ "python", "data-structures" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
1,038,203
<p>Having a "table" in memory that needs lookups, sorting, and arbitrary aggregation really does call out for SQL. You said you tried SQLite, but did you realize that SQLite can use an in-memory-only database?</p> <pre><code>connection = sqlite3.connect(':memory:') </code></pre> <p>Then you can create/drop/query/upd...
48
2009-06-24T13:00:00Z
[ "python", "data-structures" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
1,038,204
<p>First, given that you have a complex data retrieval scenario, are you sure even SQLite is overkill?</p> <p>You'll end up having an ad hoc, informally-specified, bug-ridden, slow implementation of half of SQLite, paraphrasing <a href="http://en.wikipedia.org/wiki/Greenspun%27s%5FTenth%5FRule" rel="nofollow">Greenspu...
4
2009-06-24T13:00:06Z
[ "python", "data-structures" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
1,038,249
<p>Have a Table class whose rows is a list of dict or better row objects</p> <p>In table do not directly add rows but have a method which update few lookup maps e.g. for name if you are not adding rows in order or id are not consecutive you can have idMap too e.g.</p> <pre><code>class Table(object): def __init__(...
6
2009-06-24T13:12:43Z
[ "python", "data-structures" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
23,554,199
<p>A very old question I know but...</p> <p>A pandas DataFrame seems to be the ideal option here.</p> <p><a href="http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.html">http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.html</a></p> <p>From the blurb</p> <bl...
16
2014-05-08T23:18:39Z
[ "python", "data-structures" ]
Data structure for maintaining tabular data in memory?
1,038,160
<p>My scenario is as follows: I have a table of data (handful of fields, less than a hundred rows) that I use extensively in my program. I also need this data to be persistent, so I save it as a CSV and load it on start-up. I choose not to use a database because every option (even SQLite) is an overkill for my humble r...
42
2009-06-24T12:48:51Z
24,111,934
<h1>I personally wrote a lib for pretty much that quite recently, it is called BD_XML</h1> <p>as its most fundamental reason of existence is to serve as a way to send data back and forth between XML files and SQL databases.</p> <p>It is written in Spanish (if that matters in a programming language) but it is very sim...
2
2014-06-08T23:30:20Z
[ "python", "data-structures" ]
Custom Managers and "through"
1,038,542
<p>I have a many-to-many relationship in my django application where I use the "add" method of the manager pretty heavily (ie album.photos.add() ).</p> <p>I find myself needing to store some data about the many-to-many relationship now, but I don't want to lose the add method. Can I just set a default value for all th...
1
2009-06-24T13:55:42Z
1,045,085
<p>Simplest way is to just add a method to Album (i.e. album.add_photo()) which handles the metadata and manually creates a properly-linked Photo instance.</p> <p>If you want to get all funky, you can write a custom manager for Photos, make it the default (i.e. first assigned manager), set <a href="http://docs.djangop...
2
2009-06-25T17:07:40Z
[ "python", "django", "many-to-many", "models", "manager" ]
In Python, how do I easily generate an image file from some source data?
1,038,550
<p>I have some some data that I would like to visualize. Each byte of the source data roughly corresponds to a pixel value of the image.</p> <p>What is the easiest way to generate an image file (bitmap?) using Python?</p>
13
2009-06-24T13:57:05Z
1,038,562
<p>Have a look at <a href="http://www.pythonware.com/products/pil/" rel="nofollow">PIL</a> and <a href="http://www.pygame.org/news.html" rel="nofollow">pyGame</a>. Both of them allow you to draw on a canvas and then save it to a file.</p>
3
2009-06-24T14:00:02Z
[ "python", "image", "data-visualization" ]
In Python, how do I easily generate an image file from some source data?
1,038,550
<p>I have some some data that I would like to visualize. Each byte of the source data roughly corresponds to a pixel value of the image.</p> <p>What is the easiest way to generate an image file (bitmap?) using Python?</p>
13
2009-06-24T13:57:05Z
1,038,569
<p>You can create images with a list of pixel values using <a href="http://python-pillow.github.io/">Pillow</a>:</p> <pre><code>from PIL import Image img = Image.new('RGB', (width, height)) img.putdata(my_list) img.save('image.png') </code></pre>
17
2009-06-24T14:00:37Z
[ "python", "image", "data-visualization" ]
How should I setup the Wing IDE for use with IronPython
1,038,695
<p>Here is a screen where I should point the Wing IDE to my python files. I am using IronPython.</p> <p><img src="http://i42.tinypic.com/23k6vzn.jpg" alt="alt text" /></p> <p>Am I assuming correctly that textbox one gets filled with ipy.exe ? (proper path provided)</p> <p>What should be in the rest of the boxes ?</p...
0
2009-06-24T14:24:23Z
1,040,700
<p>I do not know about your question in particular; however few weeks ago, Michael Foord published a guide for using WingIde with IronPython.<br /> You can find it here: <a href="http://www.voidspace.org.uk/ironpython/wing-how-to.shtml" rel="nofollow">http://www.voidspace.org.uk/ironpython/wing-how-to.shtml</a></p>
2
2009-06-24T20:14:05Z
[ "python", "ironpython", "wing-ide" ]
How should I setup the Wing IDE for use with IronPython
1,038,695
<p>Here is a screen where I should point the Wing IDE to my python files. I am using IronPython.</p> <p><img src="http://i42.tinypic.com/23k6vzn.jpg" alt="alt text" /></p> <p>Am I assuming correctly that textbox one gets filled with ipy.exe ? (proper path provided)</p> <p>What should be in the rest of the boxes ?</p...
0
2009-06-24T14:24:23Z
1,070,239
<ul> <li>Wing IDE at the moment doesn't allow the debug mode with IronPython. You need to link the IDE to the CPython install. (Michael Foord words in the article <a href="http://www.voidspace.org.uk/ironpython/wing-how-to.shtml" rel="nofollow">http://www.voidspace.org.uk/ironpython/wing-how-to.shtml</a>][1]).</li> <li...
0
2009-07-01T16:57:29Z
[ "python", "ironpython", "wing-ide" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,845
<p>You could do this:</p> <pre><code>url = 'abcdc.com' if url.endswith('.com'): url = url[:-4] </code></pre> <p>Or using regular expressions:</p> <pre><code>import re url = 'abcdc.com' url = re.sub('\.com$', '', url) </code></pre>
222
2009-06-24T14:47:41Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,847
<p>How about <code>url[:-4]</code>?</p>
5
2009-06-24T14:48:21Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,848
<p><strong>strip</strong> strips the characters given from both ends of the string, in your case it strips ".", "c", "o" and "m".</p>
15
2009-06-24T14:48:49Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,873
<p>This is a perfect use for regular expressions:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.match(r"(.*)\.com", "hello.com").group(1) 'hello' </code></pre>
0
2009-06-24T14:53:03Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,898
<p>If you know it's an extension, then</p> <pre> url = 'abcdc.com' ... url.split('.')[0] </pre> <p>This works equally well with abcdc.com as abcdc.[anything] and is more extensible.</p>
4
2009-06-24T14:57:24Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,902
<p>I don't see anything wrong with the way you're doing it with rsplit, it does exactly what you want. It all depends on how generic you want the solution to be. Do you always want to remove .com, or will it sometimes be .org? If that is the case, use one of the other solutions, otherwise, stick with rsplit()</p> <p>T...
0
2009-06-24T14:58:14Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,909
<p>Depends on what you know about your url and exactly what you're tryinh to do. If you know that it will always end in '.com' (or '.net' or '.org') then </p> <pre><code> url=url[:-4] </code></pre> <p>is the quickest solution. If it's a more general URLs then you're probably better of looking into the urlparse libra...
12
2009-06-24T14:59:35Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
1,038,999
<pre><code>def strip_end(text, suffix): if not text.endswith(suffix): return text return text[:len(text)-len(suffix)] </code></pre>
24
2009-06-24T15:13:09Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
2,393,022
<p>If you are sure that the string only appears at the end, then the simplest way would be to use 'replace':</p> <pre><code>url = 'abcdc.com' print url.replace('.com','') </code></pre>
34
2010-03-06T15:41:45Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
13,112,628
<p>In one line:</p> <pre><code>text if not text.endswith(suffix) or len(suffix) == 0 else text[:-len(suffix)] </code></pre>
5
2012-10-28T20:17:44Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
13,759,827
<p>Or you can use split:</p> <pre><code>a = 'abccomputer.com' res = a.split('.com',1)[0] </code></pre>
0
2012-12-07T08:57:35Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
16,411,412
<p>For urls (as it seems to be a part of the topic by the given example), one can do something like this:</p> <pre><code>import os url = 'http://www.stackoverflow.com' name,ext = os.path.splitext(url) print (name, ext) #Or: ext = '.'+url.split('.')[-1] name = url[:-len(ext)] print (name, ext) </code></pre> <p>Both w...
3
2013-05-07T04:49:17Z
[ "python", "string" ]
How do I remove a substring from the end of a string in Python?
1,038,824
<p>I have the following code:</p> <pre><code>url = 'abcdc.com' print(url.strip('.com')) </code></pre> <p>I expected: <code>abcdc</code></p> <p>I got: <code>abcd</code></p> <p>Now I do </p> <pre><code>url.rsplit('.com', 1) </code></pre> <p>Is there a better way?</p>
151
2009-06-24T14:44:01Z
24,794,474
<pre><code>def remove_file_type(infile): import re return(re.sub('\.[^.]*$','',infile)) remove_file_type('abc.efg')'abc' </code></pre>
0
2014-07-17T03:32:56Z
[ "python", "string" ]