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 |
|---|---|---|---|---|---|---|---|---|---|
Why does this Python code print nothing? | 2,048,442 | <pre><code>class a(str):
def b(self,*x,**y):
print str.decode(self,*x,**y)
b=a()
b.b('utf-8','aaa') # This prints nothing, why?
</code></pre>
| 0 | 2010-01-12T11:16:05Z | 2,048,456 | <p>because you initialize b (as an object of a) with nothing as str.</p>
| 4 | 2010-01-12T11:18:53Z | [
"python"
] |
Why does this Python code print nothing? | 2,048,442 | <pre><code>class a(str):
def b(self,*x,**y):
print str.decode(self,*x,**y)
b=a()
b.b('utf-8','aaa') # This prints nothing, why?
</code></pre>
| 0 | 2010-01-12T11:16:05Z | 2,048,461 | <p>Try initialize your string first, with some value:</p>
<pre><code># classes should have capitalized names ...
class a(str):
def b(self,*x,**y):
print 'debugging: ', self, x, y
print str.decode(self, *x,**y)
if __name__ == '__main__':
b=a('aaa')
b.b('utf-8')
b=a()
b.b('utf-8')
... | 8 | 2010-01-12T11:19:23Z | [
"python"
] |
Why does this Python code print nothing? | 2,048,442 | <pre><code>class a(str):
def b(self,*x,**y):
print str.decode(self,*x,**y)
b=a()
b.b('utf-8','aaa') # This prints nothing, why?
</code></pre>
| 0 | 2010-01-12T11:16:05Z | 2,048,469 | <p>Try printing (self,x,y). You will see</p>
<pre><code>('', ('utf-8', 'aaa'), {})
</code></pre>
<p>Therefore, in <code>str.decode(self,*x,**y)</code>, <code>self</code> is acting as the empty string.</p>
| 4 | 2010-01-12T11:22:09Z | [
"python"
] |
Why does this Python code print nothing? | 2,048,442 | <pre><code>class a(str):
def b(self,*x,**y):
print str.decode(self,*x,**y)
b=a()
b.b('utf-8','aaa') # This prints nothing, why?
</code></pre>
| 0 | 2010-01-12T11:16:05Z | 2,048,906 | <p>When you initiate <code>b1 = a()</code>, it's almost the same as <code>b2 = str()</code> except that <code>b2</code> doesn't have the <code>bound method</code> <code>b()</code> of class <code>a</code>. Hence, when you invoke <code>b1.b(...)</code>, it's the same as calling <code>print str.decode(b1,...)</code> or <c... | 2 | 2010-01-12T12:35:10Z | [
"python"
] |
Displaying a cvMatrix containing complex numbers (CV_64FC2) | 2,048,577 | <p>I'm new to OpenCV, and I would like to compare the results of a python program with my calculations in OpenCV. My matrix contains complex numbers since its the result of a cvDFT. Python handles complex numbers well and displays it with scientific notation. My C++ program is not effective when trying to use std::cout... | 4 | 2010-01-12T11:41:03Z | 2,118,663 | <p>Have you tried the python bindings for OpenCV?
<a href="http://www.exothermia.net/monkeys_and_robots/2009/12/11/working-opencv-python-bindings/" rel="nofollow">http://www.exothermia.net/monkeys_and_robots/2009/12/11/working-opencv-python-bindings/</a></p>
<p>With the bindings you can call OpenCV functions from pyth... | 0 | 2010-01-22T16:19:45Z | [
"c++",
"python",
"opencv",
"numpy"
] |
Displaying a cvMatrix containing complex numbers (CV_64FC2) | 2,048,577 | <p>I'm new to OpenCV, and I would like to compare the results of a python program with my calculations in OpenCV. My matrix contains complex numbers since its the result of a cvDFT. Python handles complex numbers well and displays it with scientific notation. My C++ program is not effective when trying to use std::cout... | 4 | 2010-01-12T11:41:03Z | 2,262,178 | <p>There is a dft example in OpenCV 2.0 code, which I am also studying right now. Here is a copy paste for you that might give you an idea. As you can see, it uses cvSplit to spilit to real and imaginary components. Hope that helps:</p>
<pre><code>im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !im )
re... | 4 | 2010-02-14T18:13:58Z | [
"c++",
"python",
"opencv",
"numpy"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,048,962 | <p>Try something like this in python:</p>
<pre><code>min_values = []
def add_to_min(file_name, one, seven):
# checks to see if 7th column is a lower value than exiting values
if len(min_values) == 0 or seven < max(min_values)[0]:
# let's remove the biggest value
min_values.sort()
if... | 1 | 2010-01-12T12:47:31Z | [
"python",
"bash"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,048,964 | <p>I would:</p>
<ul>
<li>take first 10 items, </li>
<li>sort them and then </li>
<li>for every line read from files insert the element into those top10:
<ul>
<li>in case its value is lower than highest one from current top10, </li>
<li>(keeping the sorting for performance)</li>
</ul></li>
</ul>
<p>I wouldn't post the... | 2 | 2010-01-12T12:48:18Z | [
"python",
"bash"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,049,029 | <p>A small improvement of your shell solution:</p>
<pre><code>$ cat in.txt
in_s1.txt
in_s2.txt
...
$ cat in.txt | while read i
do
cat $i | sed -e "s/^/$i /" # add filename as first column
done |
sort -n -k8 | head -10 | cut -d" " -f1,2,8
</code></pre>
| 0 | 2010-01-12T12:56:59Z | [
"python",
"bash"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,049,085 | <p>If your files are million lines, you might want to consider using "buffering". the below script goes through those million lines, each time comparing field 7 with those in the buffer. If a value is smaller than those in the buffer, one of them in buffer is replaced by the new lower value. </p>
<pre><code> for file... | 0 | 2010-01-12T13:06:57Z | [
"python",
"bash"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,049,098 | <p>This might be close to what you're looking for:</p>
<pre><code>for file in *; do sort -k6n "$file" | head -n 10 | cut -f1,7 -d " " | sed "s/^/$file /" > "${file}.out"; done
cat *.out | sort -k3n | head -n 10 > final_result.out
</code></pre>
| 0 | 2010-01-12T13:08:23Z | [
"python",
"bash"
] |
Search and sort data from several files | 2,048,779 | <p>I have a set of 1000 text files with names <code>in_s1.txt</code>, <code>in_s2.txt</code> and so. Each file contains millions of rows and each row has 7 columns like:</p>
<pre><code>ccc245 1 4 5 5 3 -12.3
</code></pre>
<p>For me the most important is the values from the first and seventh columns; the pairs <code>c... | 2 | 2010-01-12T12:15:17Z | 2,049,228 | <p>In python, use the <a href="http://docs.python.org/library/heapq.html#heapq.nsmallest" rel="nofollow">nsmallest function in the heapq module</a> -- it's designed for exactly this kind of task.</p>
<p>Example (tested) for Python 2.5 and 2.6:</p>
<pre><code>import heapq, glob
def my_iterable():
for fname in glo... | 3 | 2010-01-12T13:31:24Z | [
"python",
"bash"
] |
Writing copyright information in python code | 2,048,874 | <p>What is the standard way of writing "copyright information" in python code? Should it be inside docstring or in block comments? I could not find it in PEPs.</p>
| 13 | 2010-01-12T12:30:18Z | 2,048,889 | <pre><code># Comment in the beginning of the file
</code></pre>
<p>At least python built-in modules do this. (found out by doing <code>grep 'Copyright' /usr/lib64/python2.4/*.py</code>)</p>
| 9 | 2010-01-12T12:33:15Z | [
"python",
"coding-style",
"copyright-display"
] |
Writing copyright information in python code | 2,048,874 | <p>What is the standard way of writing "copyright information" in python code? Should it be inside docstring or in block comments? I could not find it in PEPs.</p>
| 13 | 2010-01-12T12:30:18Z | 2,049,113 | <p>We follow the recommendations found (somewhere) on the Software Freedom Law Center's <a href="http://www.softwarefreedom.org/resources/" rel="nofollow">site</a>. <a href="https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/virtaal/setup.py" rel="nofollow">Here</a> is an example of a simple GPL'ed file.... | 2 | 2010-01-12T13:11:29Z | [
"python",
"coding-style",
"copyright-display"
] |
Writing copyright information in python code | 2,048,874 | <p>What is the standard way of writing "copyright information" in python code? Should it be inside docstring or in block comments? I could not find it in PEPs.</p>
| 13 | 2010-01-12T12:30:18Z | 2,049,123 | <p>As I know, there is currently no standard way. Each company/organization will have their own template to doc the copyright information. If this is your personal project, then just feel free to doc it in the way you feel most comforable. Adding a <code>LICENSE</code> file is a very common way for projects with many s... | 0 | 2010-01-12T13:12:50Z | [
"python",
"coding-style",
"copyright-display"
] |
Writing copyright information in python code | 2,048,874 | <p>What is the standard way of writing "copyright information" in python code? Should it be inside docstring or in block comments? I could not find it in PEPs.</p>
| 13 | 2010-01-12T12:30:18Z | 2,049,597 | <p>Some projects use module variables like <code>__license__</code>, as in:</p>
<pre><code>__author__ = "Software Authors Name"
__copyright__ = "Copyright (C) 2004 Author Name"
__license__ = "Public Domain"
__version__ = "1.0"
</code></pre>
<p>Seems like a pretty clean solution to me (unless you overdo it and dump ep... | 14 | 2010-01-12T14:27:32Z | [
"python",
"coding-style",
"copyright-display"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,282 | <p>AFAIK no. </p>
<p>And the reason is that for such an atomic operation to be possible, there has to be OS support in the form of a transactional file system. And none of the mainstream operating system offer a transactional file system.</p>
<p><strong>EDIT</strong> - I'm wrong for POSIX-compliant systems at least... | 6 | 2010-01-12T13:42:50Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,304 | <p>Try <a href="http://java.sun.com/javase/6/docs/api/java/nio/channels/FileLock.html" rel="nofollow">Java FileLock API</a></p>
| 1 | 2010-01-12T13:46:14Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,333 | <p>You could try and use an extra file to act as a lock, but I'm not sure if that will work out ok. (It would force you to create lock-checking and retry logic at both sides, java and python)</p>
<p>Another solution might be to not create files at all, maybe you could make your java process listen on a port and serve ... | 1 | 2010-01-12T13:51:19Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,334 | <p>At least on POSIX platforms, leave out step 3 (delete old file). In POSIX, rename within a filesystem is guaranteed to be atomic, and renaming on top of an existing file replaces it atomically.</p>
| 4 | 2010-01-12T13:51:24Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,386 | <p>Have the python scripts request permission from the service. While the service is writing it would place a lock on the file. If the lock exists, the service would reject the python request.</p>
| 1 | 2010-01-12T13:56:57Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 2,049,395 | <p>It's a classic producer/consumer problem. You should be able to solve this by using file renaming, which is atomic on POSIX systems.</p>
| 2 | 2010-01-12T13:58:17Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
Atomic file write operations (cross platform) | 2,049,247 | <p>How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.<br>
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes. </p>
<p><em>Right now my approach is:</em></p>
... | 17 | 2010-01-12T13:36:54Z | 4,406,823 | <p>In Linux, Solaris, Unix this is easy. Just use <code>rename</code>() from your program or <code>mv</code>. The files need to be on the same filesystem.</p>
<p>On Windows, this is possible if you can control both programs. <code>LockFileEx</code>. For reads, open a <code>shared lock</code> on the lockfile. For write... | 2 | 2010-12-10T08:07:45Z | [
"java",
"python",
"design",
"file",
"file-io"
] |
IPC between python app and injected DLL | 2,049,467 | <p>Hello stack overflow: Sometimes reader, first time poster. </p>
<p><strong>Background:</strong></p>
<p>Windows box running XP SP3, soon to be upgraded to Windows Seven (MSDNAA <3)</p>
<p>I have an injected DLL which gets cycles by hooking a function that is called thousands of times a second. </p>
<p>I would ... | 5 | 2010-01-12T14:08:38Z | 4,621,980 | <p>If you don't care about realtime, then you can use the file system for communication: a log file for the DLL's output, and a config file that is read every now and then to change the DLLs behavior.</p>
| 1 | 2011-01-07T02:21:13Z | [
"python",
"dll",
"ipc",
"code-injection"
] |
IPC between python app and injected DLL | 2,049,467 | <p>Hello stack overflow: Sometimes reader, first time poster. </p>
<p><strong>Background:</strong></p>
<p>Windows box running XP SP3, soon to be upgraded to Windows Seven (MSDNAA <3)</p>
<p>I have an injected DLL which gets cycles by hooking a function that is called thousands of times a second. </p>
<p>I would ... | 5 | 2010-01-12T14:08:38Z | 4,749,467 | <p>Try sockets. Your demands are essentially a requirement of asynchronous operating; Python has <a href="http://docs.python.org/library/asyncore.html" rel="nofollow">asyncore</a> module for asynchronous IO on sockets. At the same time, it doesn't look like Python's stdlib can asynchronously handle other IPC things, so... | 2 | 2011-01-20T16:05:29Z | [
"python",
"dll",
"ipc",
"code-injection"
] |
python CGI and JQUERY problem | 2,049,506 | <p>I have a simple python CGI script where I query a MySQL database and then prints the result to the screen/webpage.
My problem is that the "cursor.execute()" function returns a list of tuples. I use a simple for loop to iterate through this list and extract each tuple. This was working great until.....I got the brigh... | -1 | 2010-01-12T14:14:09Z | 2,050,410 | <p>It looks like you have a syntax error. In Python and Javascript, the code:</p>
<pre><code>{referer: http://test/index-test.html}
</code></pre>
<p>is invalid. In Javascript, you'd have to write it like:</p>
<pre><code>{referer: "http://test/index-test.html"}
</code></pre>
<p>and in Python (assuming "referer" is a... | 0 | 2010-01-12T16:21:36Z | [
"jquery",
"python",
"cgi"
] |
How to get a function to execute | 2,049,605 | <p>I have a function and I want it to execute. Does anyone know how to do this?</p>
<pre><code>def a():
a = 'print'
print a
</code></pre>
| 1 | 2010-01-12T14:28:26Z | 2,049,623 | <p>The name of your function is <code>a</code>. It takes no arguments. So call it using <code>a()</code>:</p>
<pre><code>>>> def a():
... a = 'print'
... print a
...
>>> a()
print
</code></pre>
<p>Note that you <em>shadow</em> the definition of <code>a</code> as a function within <code>a</c... | 14 | 2010-01-12T14:29:44Z | [
"python"
] |
How to get a function to execute | 2,049,605 | <p>I have a function and I want it to execute. Does anyone know how to do this?</p>
<pre><code>def a():
a = 'print'
print a
</code></pre>
| 1 | 2010-01-12T14:28:26Z | 2,049,626 | <p>just write a() !</p>
| 1 | 2010-01-12T14:29:58Z | [
"python"
] |
How to get a function to execute | 2,049,605 | <p>I have a function and I want it to execute. Does anyone know how to do this?</p>
<pre><code>def a():
a = 'print'
print a
</code></pre>
| 1 | 2010-01-12T14:28:26Z | 2,049,633 | <p>I'm not sure I understood your question, but a simple function call goes as in</p>
<pre><code>a()
</code></pre>
<p>of course, after the definition, so it would be </p>
<pre><code>def a():
a = 'print'
print a
a()
</code></pre>
| 1 | 2010-01-12T14:30:45Z | [
"python"
] |
How to get a function to execute | 2,049,605 | <p>I have a function and I want it to execute. Does anyone know how to do this?</p>
<pre><code>def a():
a = 'print'
print a
</code></pre>
| 1 | 2010-01-12T14:28:26Z | 2,049,648 | <p>To do this you just use.</p>
<pre><code>a()
</code></pre>
<p>If you want this for use inside the current file use:</p>
<pre><code>if __name__ == '__main__': a()
</code></pre>
<p>And if you want to pass the variable a on for use in other functions use:</p>
<pre><code>if __name__ == '__main__': b = a()
</code></p... | 3 | 2010-01-12T14:33:16Z | [
"python"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,738 | <p>1</p>
<ul>
<li><p>Use <code>bool(p.match('aa'))</code> to check if the regexp matches or not</p></li>
<li><p><code>p = re.compile('b*(abb*)*a?$')</code></p></li>
<li><p><code>\b</code> matches border of string; place between <code>\w</code> and <code>\W</code> (word characters and non-word characters)</p></li>
</ul... | 3 | 2010-01-12T14:44:51Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,775 | <p>I'm not exactly sure how match works in python, but I think you might need to add ^....$ to your RE. RegExp matching usually matches sub-strings, and it finds the largest match, in the case of p.match('aa') that's "a" (probably the first one). ^...$ makes sure that you're matching the ENTIRE string, which is I belie... | 3 | 2010-01-12T14:52:08Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,818 | <p>You're matching because your regex matches any zero-width segment of any specimen text. You need to <strong>anchor</strong> your regex. Here's one way of doing it, using a zero-width lookahead assertion:</p>
<pre><code>re.compile(r'^(a(?!a)|b)*$')
</code></pre>
| 1 | 2010-01-12T14:57:04Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,901 | <p>Actually, the example works just fine ... to a small details. I would write:</p>
<pre><code>>>> p = re.compile('b*(abb*)*a?')
>>> m = p.match('aa')
>>> print m.group(0)
'a'
>>> m = p.match('abbabbabababbabbbbbaaaaa')
>>> print m.group(0)
abbabbabababbabbbbba
</code></pre... | 5 | 2010-01-12T15:08:42Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,965 | <p>Your second re should be an appropriate replacement for epsilon, as best as I understand it, though I've never seen epsilon in a regex before. </p>
<p>For what it's worth, your pattern is matching 'a'. That is to say, it is matching:</p>
<ul>
<li>zero or more "<code>b</code>"s (choosing zero)</li>
<li>zero or mo... | 1 | 2010-01-12T15:17:22Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,049,973 | <p>the problem with your expression is that it matches the empty string, meaning that if you do:</p>
<pre><code>>>> p = re.compile('b*(abb*)*(a|)')
>>> p.match('c').group(0)
''
</code></pre>
<p>and since re.match attempts to match the start of the string, you have to tell it to match it until the en... | 1 | 2010-01-12T15:17:59Z | [
"python",
"regex",
"theory"
] |
Standard Regex vs python regex discrepancy | 2,049,685 | <p>I am reading a book and they provide an example of how to match a given string with regular expressions.
Here is their example:</p>
<pre><code>b*(abb*)*(a|â) - Strings of a's and b's with no consecutive a's.
</code></pre>
<p>Now I've tried converting it to python like so:</p>
<pre><code>>> p = re.compile(... | 5 | 2010-01-12T14:38:19Z | 2,050,913 | <p>Thanks for the answers. I feel each answer had part of the answer. Here is what I was looking for.</p>
<ol>
<li><p><strong>?</strong> symbol is just a shorthand for <strong>(something|ε)</strong>. Thus <strong>(a|ε)</strong> can be rewritten as <strong>a?</strong>. So the example becomes:</p>
<pre>... | 5 | 2010-01-12T17:24:04Z | [
"python",
"regex",
"theory"
] |
Why can't I pickle this object? | 2,049,849 | <p>I have a class (below):</p>
<pre><code>class InstrumentChange(object):
'''This class acts as the DTO object to send instrument change information from the
client to the server. See InstrumentChangeTransport below
'''
def __init__(self, **kwargs):
self.kwargs = kwargs
self._change... | 11 | 2010-01-12T15:01:04Z | 2,049,936 | <pre><code> file = open('testpickle.dat', 'w')
file = Mock()
</code></pre>
<p>You are losing here reference to the opened file. Might that be a problem?</p>
| 2 | 2010-01-12T15:13:26Z | [
"python"
] |
Why can't I pickle this object? | 2,049,849 | <p>I have a class (below):</p>
<pre><code>class InstrumentChange(object):
'''This class acts as the DTO object to send instrument change information from the
client to the server. See InstrumentChangeTransport below
'''
def __init__(self, **kwargs):
self.kwargs = kwargs
self._change... | 11 | 2010-01-12T15:01:04Z | 2,049,977 | <p>It is failing because it can't find <code>__getstate__()</code> for your object. Pickle needs these to determine how to pickle/unpickle the object. You just need the <code>__getstate__()</code> and <code>__setstate__()</code> methods.</p>
<p>See the TextReader example in the docs: <a href="http://docs.python.org/... | 5 | 2010-01-12T15:18:39Z | [
"python"
] |
Why can't I pickle this object? | 2,049,849 | <p>I have a class (below):</p>
<pre><code>class InstrumentChange(object):
'''This class acts as the DTO object to send instrument change information from the
client to the server. See InstrumentChangeTransport below
'''
def __init__(self, **kwargs):
self.kwargs = kwargs
self._change... | 11 | 2010-01-12T15:01:04Z | 2,050,357 | <p>Your code has several minor "side" issues: the sudden appearance of a 'Transport' in the class name used in the test (it's not the class name that you're defining), the dubious trampling over built-in identifier <code>file</code> as a local variable (don't do that -- it doesn't hurt here, but the habit of trampling ... | 25 | 2010-01-12T16:12:40Z | [
"python"
] |
Profile Python CPU Usage By Thread | 2,050,063 | <p>I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage.</p>
<p>I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the application. I understand that os.times() will get CPU usage overal... | 7 | 2010-01-12T15:30:53Z | 2,050,371 | <p>Other than the profiler, I don't think Python has much built-in for monitoring individual thread CPU usage. Also, <a href="http://code.activestate.com/recipes/465831/" rel="nofollow">this post</a> provides an example of a simple implementation of a thread profiler. It appears mainly geared towards Unix-based systems... | 1 | 2010-01-12T16:15:20Z | [
"python"
] |
Profile Python CPU Usage By Thread | 2,050,063 | <p>I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage.</p>
<p>I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the application. I understand that os.times() will get CPU usage overal... | 7 | 2010-01-12T15:30:53Z | 2,051,647 | <p>If you're on Windows, you could take a look at the Process Explorer ("procexp.exe" shipped with <a href="http://www.sysinternals.com/" rel="nofollow">Microsoft's Sysinternals Tools</a>). There's a tab "Threads" in the properties of each process which lists all threads and their CPU time.</p>
<p>If that doesn't give... | 0 | 2010-01-12T19:15:43Z | [
"python"
] |
Profile Python CPU Usage By Thread | 2,050,063 | <p>I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage.</p>
<p>I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the application. I understand that os.times() will get CPU usage overal... | 7 | 2010-01-12T15:30:53Z | 20,997,730 | <p>Or you can simply use yappi. (<a href="https://code.google.com/p/yappi/" rel="nofollow">https://code.google.com/p/yappi/</a>) It transparently uses GetThreadTimes() if CPU clock type is selected for profiling. It will show you CPU time of a running thread.</p>
<p>See here also for an example: <a href="https://code.... | 2 | 2014-01-08T13:56:55Z | [
"python"
] |
AppEngine fetch through a free proxy | 2,050,256 | <p>My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed.</p>
<p>Can I get around this by using a free proxy of some sort?</p>
<... | 4 | 2010-01-12T15:57:50Z | 2,050,288 | <p>Probably the correct approach is to request permission from the owners of the site you are scraping. </p>
<p>Even if you use a proxy, there is still a big chance that requests coming through the proxy will end up blocked as well.</p>
| 2 | 2010-01-12T16:05:01Z | [
"python",
"google-app-engine",
"proxy"
] |
AppEngine fetch through a free proxy | 2,050,256 | <p>My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed.</p>
<p>Can I get around this by using a free proxy of some sort?</p>
<... | 4 | 2010-01-12T15:57:50Z | 2,050,308 | <p>Have you considered changing the user-agent?</p>
<pre><code>result = urlfetch.fetch(u,headers = {'User-Agent': "Mozilla/5.0"},allow_truncated=True)
</code></pre>
<p>The API will always append "AppEngine-Google;" to the user-agent, but this might work if the restriction is not based on a IP address range.</p>
| 1 | 2010-01-12T16:07:14Z | [
"python",
"google-app-engine",
"proxy"
] |
AppEngine fetch through a free proxy | 2,050,256 | <p>My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed.</p>
<p>Can I get around this by using a free proxy of some sort?</p>
<... | 4 | 2010-01-12T15:57:50Z | 2,218,463 | <p>I'm currently having the same problem and i was thinking about this solution (not yet tried) :</p>
<p>-> develop an app that fetch what you want
-> run it locally
-> fetch your local server from your initial </p>
<p>so the proxy is your computer which you know as not blocked </p>
<p>Let me know if it's works !</... | 0 | 2010-02-07T21:35:11Z | [
"python",
"google-app-engine",
"proxy"
] |
AppEngine fetch through a free proxy | 2,050,256 | <p>My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed.</p>
<p>Can I get around this by using a free proxy of some sort?</p>
<... | 4 | 2010-01-12T15:57:50Z | 3,731,700 | <p>Well to be fair, if they don't want you doing that then you probably shouldn't. It's not nice to be mean.</p>
<p>But if you really want to do it, the best approach would be creating a simple proxy script and running it on a VPS or some computer with a decent enough connection.</p>
<p>Basically you expose a REST AP... | 0 | 2010-09-16T23:45:04Z | [
"python",
"google-app-engine",
"proxy"
] |
AppEngine fetch through a free proxy | 2,050,256 | <p>My (Python) AppEngine program fetches a web page from another site to scrape data from it -- but it seems like the 3rd party site is blocking requests from Google App Engine! -- I can fetch the page from development mode, but not when deployed.</p>
<p>Can I get around this by using a free proxy of some sort?</p>
<... | 4 | 2010-01-12T15:57:50Z | 4,030,689 | <p>What you are talking about is a valid bug in app engine sdk. Have a look at <a href="http://code.google.com/p/googleappengine/issues/detail?id=544" rel="nofollow">http://code.google.com/p/googleappengine/issues/detail?id=544</a> for bug updates, and workarounds for java and python.</p>
| 1 | 2010-10-27T06:59:47Z | [
"python",
"google-app-engine",
"proxy"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,050,331 | <p>Have you looked at <a href="http://www.swig.org/index.php" rel="nofollow">Swig</a>?</p>
| 2 | 2010-01-12T16:10:31Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,050,448 | <p>One my friend for this tasks done C-parser which he use with cog.</p>
| 0 | 2010-01-12T16:25:42Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,050,467 | <p>Have a look at <a href="http://www.swig.org/Doc1.3/Python.html" rel="nofollow">Swig</a> or <a href="http://www.riverbankcomputing.com/software/sip/intro" rel="nofollow">SIP</a> that would generate interface code for you or use <a href="http://python.net/crew/theller/ctypes/" rel="nofollow">ctypes</a>.</p>
| 3 | 2010-01-12T16:28:30Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,050,883 | <p>If you compile your C code with debugging (<code>-g</code>), <a href="http://fedorapeople.org/~acme/dwarves/" rel="nofollow">pahole</a> (<a href="https://github.com/acmel/dwarves" rel="nofollow">git</a>) can give you the exact structure layouts being used.</p>
<pre>
$ pahole /bin/dd
…
struct option {
... | 9 | 2010-01-12T17:20:45Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,051,114 | <p><i>Regular expressions would work great about 90% of the time and then cause endless headaches for the remaining 10%.</i></p>
<p>The headaches happen in the cases where the C code contains syntax that you didn't think of when writing your regular expressions. Then you go back and realise that C can't really be pars... | 5 | 2010-01-12T17:53:57Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 2,053,343 | <p>I have quite successfully used <a href="http://www.gccxml.org/HTML/Index.html" rel="nofollow">GCCXML</a> on fairly large projects. You get an XML representation of the C code (including structures) which you can post-process with some simple Python.</p>
| 1 | 2010-01-12T23:45:48Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Extract the fields of a C struct | 2,050,318 | <p>I often have to write code in other languages that interact with C structs. Most typically this involves writing Python code with the <a href="http://docs.python.org/library/struct.html">struct</a> or <a href="http://docs.python.org/library/ctypes.html">ctypes</a> modules.</p>
<p>So I'll have a .h file full of str... | 12 | 2010-01-12T16:08:31Z | 9,594,157 | <p><a href="https://github.com/cournape/cython-codegen" rel="nofollow">ctypes-codegen</a> or <a href="http://pypi.python.org/pypi/ctypeslib/" rel="nofollow">ctypeslib</a> (same thing, I think) will generate ctypes <code>Structure</code> definitions (also other things, I believe, but I only tried structs) by parsing hea... | 1 | 2012-03-07T00:38:43Z | [
"python",
"c",
"language-agnostic",
"struct"
] |
Django: Unexpectedly persistent module variables | 2,050,320 | <p>I noticed a strange behaviour today: It seems that, in the following example, the config.CLIENT variable stays persistent accross requests â even if the view gets passed an entirely different client_key, the query that gets the client is only executed once (per many requests), and then the config.CLIENT variable s... | 0 | 2010-01-12T16:08:33Z | 2,050,388 | <p>Multiple requests are processed by the same process and global variables like your CLIENT live as long, as process does. You shouldn't rely on global variables, when processing requests - use either local ones, when you need to keep a variable for the time of building response or put data into the database, when som... | 6 | 2010-01-12T16:17:52Z | [
"python",
"django"
] |
Django: Unexpectedly persistent module variables | 2,050,320 | <p>I noticed a strange behaviour today: It seems that, in the following example, the config.CLIENT variable stays persistent accross requests â even if the view gets passed an entirely different client_key, the query that gets the client is only executed once (per many requests), and then the config.CLIENT variable s... | 0 | 2010-01-12T16:08:33Z | 2,055,503 | <p>OK, just to make it slightly clearer (and in response to the comment by Felix), I'm posting the code that does what I needed. The whole problem arose from a fundamental misunderstanding on my part and I'm sorry for any confusion I might have caused.</p>
<pre><code>import config
# This will be called once per reque... | 0 | 2010-01-13T09:07:50Z | [
"python",
"django"
] |
python to Java checksum calculation | 2,050,325 | <p>I received this python script that generates a file checksum:</p>
<pre><code>import sys,os
if __name__=="__main__":
#filename=os.path.abspath(sys.argv[1])
#filename=r"H:\Javier Ortiz\559-7 From Pump.bin"
cksum=0
offset=0
pfi=open(filename,'rb')
while 1:
icks=0
chunk=pfi.read(256)
if not chunk: break #if... | 0 | 2010-01-12T16:09:05Z | 2,050,365 | <p>Your Python version prints the checksum in hex, while your Java version prints it in decimal. You should make your Java version print in hex, too. 0x1c0 == 448.</p>
<p>To use the <code>cksum=0x%4.4x</code> format string as you had in your Python version, use this:</p>
<pre><code>System.out.printf("cksum=0x%4.4x%n"... | 5 | 2010-01-12T16:14:34Z | [
"java",
"python"
] |
python to Java checksum calculation | 2,050,325 | <p>I received this python script that generates a file checksum:</p>
<pre><code>import sys,os
if __name__=="__main__":
#filename=os.path.abspath(sys.argv[1])
#filename=r"H:\Javier Ortiz\559-7 From Pump.bin"
cksum=0
offset=0
pfi=open(filename,'rb')
while 1:
icks=0
chunk=pfi.read(256)
if not chunk: break #if... | 0 | 2010-01-12T16:09:05Z | 2,050,395 | <p>1C0<sub>16</sub> = 448<sub>10</sub></p>
<p>I think that's your problem.</p>
| 3 | 2010-01-12T16:18:51Z | [
"java",
"python"
] |
python to Java checksum calculation | 2,050,325 | <p>I received this python script that generates a file checksum:</p>
<pre><code>import sys,os
if __name__=="__main__":
#filename=os.path.abspath(sys.argv[1])
#filename=r"H:\Javier Ortiz\559-7 From Pump.bin"
cksum=0
offset=0
pfi=open(filename,'rb')
while 1:
icks=0
chunk=pfi.read(256)
if not chunk: break #if... | 0 | 2010-01-12T16:09:05Z | 2,050,415 | <ol>
<li><p><code>dis.read(buffer)</code> returns the number of bytes that was actually read. For the last chunk, it will probably be less than 256. So the <code>for</code> loop shouldn't always be performed 256 times - it should be performed as many times as the actual byte count that was read from the stream.</p></li... | 1 | 2010-01-12T16:22:01Z | [
"java",
"python"
] |
Bash or Python for extracting blocks from text files | 2,050,411 | <p>I have a huge text file, which is structured as:</p>
<pre><code>SEPARATOR
STRING1
(arbitrary number of lines)
SEPARATOR
...
SEPARATOR
STRING2
(arbitrary number of lines)
SEPARATOR
SEPARATOR
STRING3
(arbitrary number of lines)
SEPARATOR
....
</code></pre>
<p>What only changes between the different "blocks" of the f... | 1 | 2010-01-12T16:21:47Z | 2,050,453 | <p>I would use Python and write something similar to this:</p>
<pre><code>import sys
file = open("file", "r")
counter = 0
count = False
for line in file:
if count:
counter += 1
if count and SEPARATOR == line:
break
if not count and sys.argv[1] == line:
count = True
print SEPARATOR, sys.argv[1], coun... | 0 | 2010-01-12T16:26:24Z | [
"python",
"bash"
] |
Bash or Python for extracting blocks from text files | 2,050,411 | <p>I have a huge text file, which is structured as:</p>
<pre><code>SEPARATOR
STRING1
(arbitrary number of lines)
SEPARATOR
...
SEPARATOR
STRING2
(arbitrary number of lines)
SEPARATOR
SEPARATOR
STRING3
(arbitrary number of lines)
SEPARATOR
....
</code></pre>
<p>What only changes between the different "blocks" of the f... | 1 | 2010-01-12T16:21:47Z | 2,050,542 | <p>In Python 2.6 or better:</p>
<pre><code>def doit(inf, ouf, thestring, separator='SEPARATOR\n'):
thestring += '\n'
for line in inf:
# here we're always at the start-of-block separator
assert line == separator
blockid = next(inf)
if blockid == thestring:
# found block of interest, use enumer... | 3 | 2010-01-12T16:36:04Z | [
"python",
"bash"
] |
Bash or Python for extracting blocks from text files | 2,050,411 | <p>I have a huge text file, which is structured as:</p>
<pre><code>SEPARATOR
STRING1
(arbitrary number of lines)
SEPARATOR
...
SEPARATOR
STRING2
(arbitrary number of lines)
SEPARATOR
SEPARATOR
STRING3
(arbitrary number of lines)
SEPARATOR
....
</code></pre>
<p>What only changes between the different "blocks" of the f... | 1 | 2010-01-12T16:21:47Z | 2,050,632 | <p>If you want this to be fast, you need to avoid reading the entire file to find the block of data you need.</p>
<ol>
<li>read over the file once and store an index of a) byte offset for the start of each STRING_I and b) length (bytes) of block - distance to the next SEPARATOR in bytes. You can store this index in a... | 0 | 2010-01-12T16:48:57Z | [
"python",
"bash"
] |
Bash or Python for extracting blocks from text files | 2,050,411 | <p>I have a huge text file, which is structured as:</p>
<pre><code>SEPARATOR
STRING1
(arbitrary number of lines)
SEPARATOR
...
SEPARATOR
STRING2
(arbitrary number of lines)
SEPARATOR
SEPARATOR
STRING3
(arbitrary number of lines)
SEPARATOR
....
</code></pre>
<p>What only changes between the different "blocks" of the f... | 1 | 2010-01-12T16:21:47Z | 2,053,427 | <p>you can use (g)awk, which is a relatively fast tool to process files.</p>
<pre><code>read -p "Enter input: " input
awk -vinput="$input" -vRS="SEPARATOR" '$0~input{ printf RT; print $0; printf RT }' file
</code></pre>
| 0 | 2010-01-13T00:03:16Z | [
"python",
"bash"
] |
Django/Python - Try/except problem | 2,050,568 | <p>i have code like this:</p>
<pre><code>try:
var = request.POST['var']
except NameError:
var = ''
</code></pre>
<p>Why always code after "except" is executing? Even if <code>request.POST['var']</code> exist.</p>
| 0 | 2010-01-12T16:40:38Z | 2,050,578 | <p>How do you know it is executing? Perhaps <code>request.POST['var']</code> is also <code>''</code> so you couldn't tell the difference.</p>
<p>Also, the only way that </p>
<pre><code>var = request.POST['var']
</code></pre>
<p>could raise a <code>NameError</code> is if <code>request</code> doesn't exist.</p>
<p>I... | 8 | 2010-01-12T16:42:17Z | [
"python",
"django",
"variables"
] |
Django/Python - Try/except problem | 2,050,568 | <p>i have code like this:</p>
<pre><code>try:
var = request.POST['var']
except NameError:
var = ''
</code></pre>
<p>Why always code after "except" is executing? Even if <code>request.POST['var']</code> exist.</p>
| 0 | 2010-01-12T16:40:38Z | 2,050,674 | <p>A better way to do what you seem to be trying to do might be</p>
<pre><code>var = request.POST.get('var', 'some default value')
</code></pre>
<p>where the second argument to the POST dict's <code>get</code> method is the value to return if the key (<code>'var'</code> in this case) doesn't exist. Translating your ... | 1 | 2010-01-12T16:53:48Z | [
"python",
"django",
"variables"
] |
Django/Python - Try/except problem | 2,050,568 | <p>i have code like this:</p>
<pre><code>try:
var = request.POST['var']
except NameError:
var = ''
</code></pre>
<p>Why always code after "except" is executing? Even if <code>request.POST['var']</code> exist.</p>
| 0 | 2010-01-12T16:40:38Z | 2,050,695 | <p>Eliminate the guesswork and replace <code>NameError</code> with something like <code>KeyboardInterrupt</code>, look at the traceback and you'll know exactly what the problem is.</p>
| 2 | 2010-01-12T16:56:30Z | [
"python",
"django",
"variables"
] |
Django/Python - Try/except problem | 2,050,568 | <p>i have code like this:</p>
<pre><code>try:
var = request.POST['var']
except NameError:
var = ''
</code></pre>
<p>Why always code after "except" is executing? Even if <code>request.POST['var']</code> exist.</p>
| 0 | 2010-01-12T16:40:38Z | 2,050,696 | <p>what's the result of the following in your case?</p>
<pre><code>except NameError, e:
print e
</code></pre>
| 1 | 2010-01-12T16:56:43Z | [
"python",
"django",
"variables"
] |
Django/Python - Try/except problem | 2,050,568 | <p>i have code like this:</p>
<pre><code>try:
var = request.POST['var']
except NameError:
var = ''
</code></pre>
<p>Why always code after "except" is executing? Even if <code>request.POST['var']</code> exist.</p>
| 0 | 2010-01-12T16:40:38Z | 2,050,710 | <p>try</p>
<pre>
try:
if request.method == 'POST':
var = request.POST['var']
except NameError:
var = ''
</pre>
| 0 | 2010-01-12T16:58:01Z | [
"python",
"django",
"variables"
] |
SQL queries through PYODBC fail silently on one machine, works on another | 2,050,613 | <p>I am working on a program to automate parsing data from XML files and storing it into several databases. (Specifically the USGS realtime water quality service, if anyone's interested, at <a href="http://waterservices.usgs.gov/rest/WaterML-Interim-REST-Service.html" rel="nofollow">http://waterservices.usgs.gov/rest/... | 1 | 2010-01-12T16:46:55Z | 4,803,506 | <blockquote>
<p>I have no idea how to check whether
we have the same version of the
Microsoft ODBC drivers.</p>
</blockquote>
<p>I think you're looking for Control Panel | Administrative Tools | Data Sources (ODBC). Click the "Drivers" tab.</p>
<p>I think either Access 2000 or Office 2000 shipped with a desktop... | 0 | 2011-01-26T10:31:14Z | [
"python",
"sql",
"database",
"pyodbc"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 2,050,649 | <p>The simplest way to do this is with a list comprehension:</p>
<pre><code>[s + mystring for s in mylist]
</code></pre>
<p>Notice that I avoided using builtin names like <code>list</code> because that shadows or hides the builtin names, which is very much not good.</p>
<p>Also, if you do not actually need a list, b... | 76 | 2010-01-12T16:51:21Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 2,050,670 | <pre><code>list2 = ['%sbar' % (x,) for x in list]
</code></pre>
<p>And don't use <code>list</code> as a name; it shadows the built-in type.</p>
| 0 | 2010-01-12T16:53:11Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 2,050,709 | <pre><code>new_list = [word_in_list + end_string for word_in_list in old_list]
</code></pre>
<p>Using names such as "list" for your variable names is bad since it will overwrite/override the builtins.</p>
| 0 | 2010-01-12T16:57:36Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 2,050,721 | <pre><code>my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
my_new_list = [x + string for x in my_list]
print my_new_list
</code></pre>
<p>This will print:</p>
<pre><code>['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
| 8 | 2010-01-12T16:59:06Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 2,051,913 | <p>I havent found a way to comment on answers till now. So here it is.
I support Ignacio Vazquez-Abrams's answer of <code>list2 = ['%sbar' % x for x in list]</code>.</p>
<p>Others answers with <code>[string + "bar" for string in list]</code> would work for most of times, but if you accept a more general solution for t... | 2 | 2010-01-12T19:57:43Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 19,230,697 | <p><code>map</code> seems like the right tool for the job to me.</p>
<pre><code>my_list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
list2 = map(lambda orig_string: orig_string + string, my_list)
</code></pre>
<p>See <a href="http://docs.python.org/2/tutorial/datastructures.html#functional-programming-tools" rel="n... | 1 | 2013-10-07T17:08:16Z | [
"python",
"list"
] |
Appending the same string to a list of strings in Python | 2,050,637 | <p>I am trying to take one string, and append it to every string contained in a list, and then have a new list with the completed strings. Example:</p>
<pre><code>list = ['foo', 'fob', 'faz', 'funk']
string = 'bar'
*magic*
list2 = ['foobar', 'fobbar', 'fazbar', 'funkbar']
</code></pre>
<p>I tried for loops, and an ... | 45 | 2010-01-12T16:50:17Z | 37,863,146 | <p>you can use lambda inside map in python. wrote a gray codes generator.
<a href="https://github.com/rdm750/rdm750.github.io/blob/master/python/gray_code_generator.py" rel="nofollow">https://github.com/rdm750/rdm750.github.io/blob/master/python/gray_code_generator.py</a>
# your code goes here
'''
... | 0 | 2016-06-16T15:15:10Z | [
"python",
"list"
] |
Change dynamically the contents of a matplotlib plot | 2,050,728 | <p>I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.</p>
<p>Now I find myself with the same problem, but now I have a lot of pair of curve... | 2 | 2010-01-12T17:00:29Z | 2,051,151 | <p>Well I managed to do it with an event handler for mouse clicks. I will change it for something more useful, but I post my solution anyway.</p>
<pre><code>import matplotlib.pyplot as plt
figure = plt.figure()
# plotting
plt.plot([1,2,3],[10,20,30],'bo-')
plt.grid()
plt.legend()
def on_press(event):
print 'you ... | 7 | 2010-01-12T17:59:23Z | [
"python",
"matplotlib",
"plot"
] |
Change dynamically the contents of a matplotlib plot | 2,050,728 | <p>I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.</p>
<p>Now I find myself with the same problem, but now I have a lot of pair of curve... | 2 | 2010-01-12T17:00:29Z | 2,051,163 | <p>Sounds like you want to embed matplotlib in an application. There are some resources available for that:</p>
<ul>
<li><a href="http://matplotlib.sourceforge.net/examples/user_interfaces/index.html" rel="nofollow">user interface examples</a></li>
<li><a href="http://www.scipy.org/Cookbook/Matplotlib/EmbeddingInWx" r... | 2 | 2010-01-12T18:02:41Z | [
"python",
"matplotlib",
"plot"
] |
Change dynamically the contents of a matplotlib plot | 2,050,728 | <p>I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.</p>
<p>Now I find myself with the same problem, but now I have a lot of pair of curve... | 2 | 2010-01-12T17:00:29Z | 2,244,345 | <p>I really like using traits. If you follow the tutorial <a href="http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html" rel="nofollow">Writing a graphical application for scientific programming </a>, you should be able to do what you want. The tutorial shows how to interact with ... | 2 | 2010-02-11T12:26:57Z | [
"python",
"matplotlib",
"plot"
] |
Sending stdout as response from CGI spawned program | 2,051,030 | <p>I'm trying to compose a .zip file in a CGI program and send that as the content response. I'm getting stuck in that whenever I spawn a program that prints to stdout, that somehow doesn't get accepted by Apache. It seems to be something to do with spawning a program that writes to stdout. The snippet below reprod... | 0 | 2010-01-12T17:40:08Z | 2,051,044 | <p>Try <code>sys.stdout.flush()</code> before calling external programs.</p>
| 1 | 2010-01-12T17:41:50Z | [
"python",
"apache",
"cgi"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,045 | <p><code>i</code> starts at <code>0</code>, so the <code>while</code> condition stays always true; see the <a href="http://docs.python.org/library/functions.html#range">range docs</a> for details.</p>
| 36 | 2010-01-12T17:42:13Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,049 | <p>Because the initial value of <code>i</code> is 0.</p>
| 7 | 2010-01-12T17:42:43Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,051 | <p>The first value in <code>i</code> will be 0. 0 times anything is 0.</p>
| 4 | 2010-01-12T17:42:49Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,052 | <p><code>i</code> starts at zero, so the condition for the inner loop is always <code>0*j < n</code>, which will always be true.</p>
| 12 | 2010-01-12T17:43:05Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,053 | <p>On the first time through the outer loop, the inner loop becomes an infinite loop.
It doesn't matter what happens after that. There's no "after infinity".</p>
| 1 | 2010-01-12T17:43:06Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,055 | <p>because i is 0!! and i*j=0
| 2 | 2010-01-12T17:43:16Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,056 | <p><code>range(n)</code> starts at <code>0</code>, not <code>1</code>. <code>0 * j</code> will always be less than <code>n</code>.</p>
| 2 | 2010-01-12T17:43:18Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,344 | <p>i is 0 rewrite you loop like</p>
<pre><code>for i in range(1,n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>using this version of the range function will create a range that starts at 1 instead of 0</p>
| 0 | 2010-01-12T18:25:33Z | [
"python",
"loops",
"infinite-loop"
] |
why is this an infinite loop in python? | 2,051,034 | <p>I can't seem to figure out why this is an infinite loop in python??</p>
<pre><code>for i in range(n):
j=1
while((i*j)<n):
j+=1
</code></pre>
<p>shouldn't the outer loop go n times. incrementing j until its equal to n div i each time?</p>
| 7 | 2010-01-12T17:40:27Z | 2,051,355 | <p>You can create a "trace" showing the state changes of the variables.</p>
<ol>
<li>n= 5; i= 0</li>
<li>n= 5; i= 0; j= 1</li>
<li>i*j < n -> 0 < 5: n= 5; i= 0; j= 2</li>
<li>i*j < n -> 0 < 5: n= 5; i= 0; j= 3</li>
<li>i*j < n -> 0 < 5: n= 5; i= 0; j= 4</li>
<li>i*j < n -> 0 < 5: n= 5; i= 0; j=... | 16 | 2010-01-12T18:27:06Z | [
"python",
"loops",
"infinite-loop"
] |
Using Python struct.unpack with 1-byte variables | 2,051,070 | <p>How can I use <code>struct.unpack()</code> or some other function available in Python to easily convert <em>one</em> byte variable to a Python integer? Right now, it is done in a rather lame way:</p>
<pre><code>file = open("telemetry.dat", "rb").read()
magic = file[0]
int(binascii.hexlify(magic), 16)
</code></pre>
... | 1 | 2010-01-12T17:45:19Z | 2,051,076 | <p>how about <code>ord(my_byte)</code> ? </p>
<p>Or if the variable content is like <code>my_byte == "0xff"</code> or <code>ff</code>
you can simply do <code>int(my_byte, 16)</code></p>
<p>If you have a streamof hex digits, you can do:</p>
<pre><code>int_bytes = (int(my_bytes[i:i+2], 16) for i in xrange(0, len(my_by... | 5 | 2010-01-12T17:46:33Z | [
"python",
"binary",
"parsing"
] |
Using Python struct.unpack with 1-byte variables | 2,051,070 | <p>How can I use <code>struct.unpack()</code> or some other function available in Python to easily convert <em>one</em> byte variable to a Python integer? Right now, it is done in a rather lame way:</p>
<pre><code>file = open("telemetry.dat", "rb").read()
magic = file[0]
int(binascii.hexlify(magic), 16)
</code></pre>
... | 1 | 2010-01-12T17:45:19Z | 2,051,125 | <p>Are you asking about <code>struct.unpack( 'b', someByte )</code>?</p>
<p>A "byte" is a 1-character string. </p>
<p>The value <code>0xff</code> is an integer, and already unpacked.</p>
<p>If you're finding the 4-character value <code>0xff</code> in an input file, you're best served by <code>eval()</code> since yo... | -7 | 2010-01-12T17:55:42Z | [
"python",
"binary",
"parsing"
] |
Using Python struct.unpack with 1-byte variables | 2,051,070 | <p>How can I use <code>struct.unpack()</code> or some other function available in Python to easily convert <em>one</em> byte variable to a Python integer? Right now, it is done in a rather lame way:</p>
<pre><code>file = open("telemetry.dat", "rb").read()
magic = file[0]
int(binascii.hexlify(magic), 16)
</code></pre>
... | 1 | 2010-01-12T17:45:19Z | 16,881,320 | <p>Yes, you can use <a href="http://docs.python.org/2/library/struct.html#struct.unpack" rel="nofollow">struct.unpack()</a> with 1-Byte variables; see the example below:</p>
<pre><code>import struct
my_byte = b'\x07';
my_int = struct.unpack('>H', b'\x00' + my_byte)[0]
print type(my_int)
print my_int
</code></pre>
... | 0 | 2013-06-02T09:07:18Z | [
"python",
"binary",
"parsing"
] |
Using Python struct.unpack with 1-byte variables | 2,051,070 | <p>How can I use <code>struct.unpack()</code> or some other function available in Python to easily convert <em>one</em> byte variable to a Python integer? Right now, it is done in a rather lame way:</p>
<pre><code>file = open("telemetry.dat", "rb").read()
magic = file[0]
int(binascii.hexlify(magic), 16)
</code></pre>
... | 1 | 2010-01-12T17:45:19Z | 16,882,232 | <p>An efficient way to interpret each byte from a file as an integer is to use <code>array</code> module:</p>
<pre><code>import os
from array import array
a = array("B") # interpret each byte as unsigned integer [0, 255]
with open("telemetry.dat", "rb") as file:
a.fromfile(file, os.path.getsize(file.name))
</code... | 0 | 2013-06-02T11:19:40Z | [
"python",
"binary",
"parsing"
] |
What is a Python egg? | 2,051,192 | <p>I'm new to Python and am just trying to understand how its packages work. Presumably "eggs" are some sort of packaging mechanism, but what would be a quick overview of what role they play and maybe some information on why they're useful and how to create them?</p>
| 280 | 2010-01-12T18:05:29Z | 2,051,195 | <p>Same concept as a <code>.jar</code> file in Java, it is a <code>.zip</code> file with some metadata files renamed <code>.egg</code>, for distributing code as bundles.</p>
<p><a href="http://svn.python.org/projects/sandbox/trunk/setuptools/doc/formats.txt">Specifically: The Internal Structure of Python Eggs</a></p>
... | 362 | 2010-01-12T18:06:42Z | [
"python",
"egg"
] |
What is a Python egg? | 2,051,192 | <p>I'm new to Python and am just trying to understand how its packages work. Presumably "eggs" are some sort of packaging mechanism, but what would be a quick overview of what role they play and maybe some information on why they're useful and how to create them?</p>
| 280 | 2010-01-12T18:05:29Z | 37,800,297 | <p>The .egg file is a distribution format for Python packages. Itâs just an alternative to a source code distribution or Windows exe. But note that for pure <code>Python</code>, the egg file is completely cross-platform.</p>
<p>The .egg file itself is essentially a .zip file. If you change the extension to â<code>... | 5 | 2016-06-13T22:43:38Z | [
"python",
"egg"
] |
sqlite3 'database is locked' won't go away with retries | 2,051,243 | <p>I have a sqlite3 database that is accessed by a few threads (3-4). I am aware of the general limitations of sqlite3 with regards to concurrency as stated <a href="http://www.sqlite.org/faq.html#q6" rel="nofollow">http://www.sqlite.org/faq.html#q6</a> , but I am convinced that is not the problem.</p>
<p>All of the t... | 3 | 2010-01-12T18:12:39Z | 2,051,330 | <p>Sqlite locks the entire database every time you try to write to the database. Is there any chance one of your threads is constantly writing? Is only one thread hitting the Database lock or all buy one of them?</p>
| 0 | 2010-01-12T18:23:16Z | [
"python",
"sqlite3"
] |
sqlite3 'database is locked' won't go away with retries | 2,051,243 | <p>I have a sqlite3 database that is accessed by a few threads (3-4). I am aware of the general limitations of sqlite3 with regards to concurrency as stated <a href="http://www.sqlite.org/faq.html#q6" rel="nofollow">http://www.sqlite.org/faq.html#q6</a> , but I am convinced that is not the problem.</p>
<p>All of the t... | 3 | 2010-01-12T18:12:39Z | 2,057,325 | <p>Here is a not-so-elegant temporary fix: using an external exclusive lock around the writes rather than depend on internal sqlite locking. The above block in the question is basically wrapped with a system wide lock that every thread has to acquire before writing. Since sqlite3 locks the entire DB when writing anyway... | 0 | 2010-01-13T14:33:35Z | [
"python",
"sqlite3"
] |
sqlite3 'database is locked' won't go away with retries | 2,051,243 | <p>I have a sqlite3 database that is accessed by a few threads (3-4). I am aware of the general limitations of sqlite3 with regards to concurrency as stated <a href="http://www.sqlite.org/faq.html#q6" rel="nofollow">http://www.sqlite.org/faq.html#q6</a> , but I am convinced that is not the problem.</p>
<p>All of the t... | 3 | 2010-01-12T18:12:39Z | 2,506,156 | <p>I also suffered from this, on a website that had ~200 users per day (which transalted to maybe 1000 page views). Retries just didn't help (and I finally increased their number up to 100, with short sleeps in between). I don't remember which version of SQLite it was, but I learned the lesson that if you want to have ... | 0 | 2010-03-24T08:09:18Z | [
"python",
"sqlite3"
] |
Get HTML links within a specified <table> using minidom | 2,051,270 | <p>I'm looking to use Python and xml.dom.minidom to get a list of links within a particular <code><table></code> specified by the table id. Based on some <a href="http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">excellent advice</a>, I'm trying to ... | 1 | 2010-01-12T18:15:27Z | 2,051,501 | <p>I think that you want to first find the TABLE element then call getElemenetByTagName on it. That should return all <strong>a</strong> elements that are children on the table element. Also, double check that your HTML is XHTML; minidom is meant to parse XML, not HTML.</p>
| 0 | 2010-01-12T18:50:11Z | [
"python",
"minidom"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.