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
How to install Python ssl module on Windows?
2,261,866
<p>The Google App Engine Launcher tells me:</p> <blockquote> <p>WARNING appengine_rpc.py:399 ssl module not found. Without the ssl module, the identity of the remote host cannot be verified, and connections may NOT be secure. To fix this, please install the ssl module from <a href="http://pypi.python.org/pypi/...
19
2010-02-14T16:48:50Z
9,380,177
<p>I've compiled all the instructions at one place to make people's life easy. <a href="http://waqasshabbir.tumblr.com/post/18010535789/ssl-module-for-python-2-5-installation-windows-only" rel="nofollow">http://waqasshabbir.tumblr.com/post/18010535789/ssl-module-for-python-2-5-installation-windows-only</a></p>
1
2012-02-21T15:26:15Z
[ "python", "google-app-engine", "ssl", "installation", "python-2.5" ]
BMP2avi in python
2,261,928
<p>hi I am looking for a python code that takes series of BMP file and merges them into Avi file (and get the parameter frames per second from the user)</p> <p>does anyone has an idea where to begin?</p> <p>Ariel</p>
0
2010-02-14T17:07:38Z
2,261,964
<p>I'd begin by using <A HREF="http://pygstdocs.berlios.de/pygst-tutorial/index.html" rel="nofollow">the GStreamer Python bindings</A>; at a minimum, that'll take the AVI encoding (or <A HREF="http://gstreamer.freedesktop.org/documentation/plugins.html" rel="nofollow">a great many other codecs, if you prefer</A>) off y...
0
2010-02-14T17:18:06Z
[ "python", "bmp", "avi" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,027
<p>I think this is mostly a matter of style. Both should be easily understandable as an infinite loop. </p> <p>However, personally I prefer the second option. That's because it just takes a mental micro-step less to understand, especially for programmers without C background.</p>
2
2010-02-14T17:36:13Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,033
<p>The first one will work also in those early versions where <code>True</code> is not yet defined. </p>
2
2010-02-14T17:37:34Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,045
<p>This is only a matter of style, any programming beginner will understand either option.</p> <p>But the second option will only work if <code>True</code> wasn't assigned to <code>False</code>, which was possible until Python 3:</p> <pre><code>&gt;&gt;&gt; True = False &gt;&gt;&gt; True False </code></pre>
0
2010-02-14T17:40:32Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,063
<p>IMO the second option is more <em>obvious</em>.</p> <p>If you could get rid of the <code>while</code> and write more compact code, that might be more pythonic.<br> For example:</p> <pre><code># Get the even numbers in the range 1..10 # Version 1 l = [] n = 1 while 1: if n % 2 == 0: l.append(n) n += 1 i...
2
2010-02-14T17:45:46Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,064
<p>If you have an algorithm that is suppose to terminate in a finite time, I would recommend this, which is always safer than <code>while True</code>:</p> <pre><code>maxiter = 1000 for i in xrange(maxiter): # your code # on success: break else: # that algorithm has not finished in maxiter steps! do somet...
2
2010-02-14T17:46:17Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,069
<p>The most pythonic way will always be the most readable. Use <code>while True:</code></p>
7
2010-02-14T17:47:32Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,085
<p>I believe the second expression is more explicit, and thus more <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html" rel="nofollow">pythonic</a>. </p>
1
2010-02-14T17:51:16Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,110
<p>Neither.</p> <p>Both of them mean I have to scan the code looking for the <code>break</code>, instead of being able to see the stop condition right where it belongs.</p> <p>I try to avoid this kind of thing wherever possible, and if it's not possible, let the code speak for itself like this:</p> <pre><code>while ...
3
2010-02-14T17:57:32Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,118
<p>The better way is "while True" with a conditional break out of the loop.</p>
0
2010-02-14T17:59:11Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,162
<p>Fundamentally it doesn't matter, such minutiae doesn't really affect whether something is 'pythonic' or not.</p> <p>If you're interested in trivia however, there are some differences.</p> <ol> <li><p>The <a href="http://www.python.org/doc/2.3.5/whatsnew/section-bool.html">builtin boolean type didn't exist till Pyt...
36
2010-02-14T18:08:33Z
[ "python", "while-loop", "infinite-loop" ]
What's the difference between "while 1" and "while True"?
2,261,987
<p>I've seen two ways to create an infinite loop in Python:</p> <ol> <li><pre><code>while 1: do_something() </code></pre></li> <li><pre><code>while True: do_something() </code></pre></li> </ol> <p>Is there any difference between these? Is one more pythonic than the other?</p>
18
2010-02-14T17:28:42Z
2,262,208
<p>It doesn't really matter. Neither is hard to read or understand, though personally I'd always use <code>while True</code>, which is a bit more explicit.</p> <p>More generally, a whole lot of while–break loops people write in Python could be something else. Sometimes I see people write <code>i = 0; while True: i +...
3
2010-02-14T18:22:55Z
[ "python", "while-loop", "infinite-loop" ]
How can I detect what other copy of Python script is already running
2,261,997
<p>I have a script. It uses GTK. And I need to know if another copy of scrip starts. If it starts window will extend.</p> <p>Please, tell me the way I can detect it.</p>
2
2010-02-14T17:30:12Z
2,262,025
<p>The new copy could search for running copies, fire a <code>SIGUSER</code> signal and trigger a callback in your running process that then handles all the magic.</p> <p>See the <a href="http://docs.python.org/library/signal.html" rel="nofollow"><code>signal</code></a> library for details and the list of things that ...
0
2010-02-14T17:36:05Z
[ "python" ]
How can I detect what other copy of Python script is already running
2,261,997
<p>I have a script. It uses GTK. And I need to know if another copy of scrip starts. If it starts window will extend.</p> <p>Please, tell me the way I can detect it.</p>
2
2010-02-14T17:30:12Z
2,262,058
<p>You can use a PID file to determine if the application is already running (just search for "python daemon" on Google to find some working implementations).</p> <p>If you detected that the program is already running, you can communicate with the running instance using <a href="http://docs.python.org/library/os.html#...
1
2010-02-14T17:44:17Z
[ "python" ]
How can I detect what other copy of Python script is already running
2,261,997
<p>I have a script. It uses GTK. And I need to know if another copy of scrip starts. If it starts window will extend.</p> <p>Please, tell me the way I can detect it.</p>
2
2010-02-14T17:30:12Z
2,262,290
<p>You could use a D-Bus service. Your script would start a new service if none is found running in the current session, and otherwise send a D-Bus message to the running instace (that can send "anything", including strings, lists, dicts).</p> <p>The GTK-based library libunique (missing Python bindings?) uses this app...
3
2010-02-14T18:47:24Z
[ "python" ]
How can I detect what other copy of Python script is already running
2,261,997
<p>I have a script. It uses GTK. And I need to know if another copy of scrip starts. If it starts window will extend.</p> <p>Please, tell me the way I can detect it.</p>
2
2010-02-14T17:30:12Z
2,262,383
<p>I've done that using several ways depending upon the scenario</p> <ol> <li>In one case my script had to listen on a TCP port. So I'd just see if the port was available it'd mean it is a new copy. This was sufficient for me but in certain cases, if the port is already in use, it might be because some other kind of a...
0
2010-02-14T19:13:24Z
[ "python" ]
What mutex/locking/waiting mechanism to use when writing a Chat application with Tornado Web Framework
2,262,039
<p>We're implementing a Chat server using Tornado.</p> <p>The premise is simple, a user makes open an HTTP ajax connection to the Tornado server, and the Tornado server answers only when a new message appears in the chat-room. Whenever the connection closes, regardless if a new message came in or an error/timeout occu...
0
2010-02-14T17:39:17Z
2,270,139
<p>I'm looking into the best options for developing a chat application and was looking into tornado as well. This rough cuts <a href="http://my.safaribooksonline.com/9781449382292" rel="nofollow">Building the Realtime User Experience</a> has a chapter on building a chat application with tornado that might be useful to ...
2
2010-02-16T01:41:55Z
[ "python", "asynchronous", "tornado" ]
What mutex/locking/waiting mechanism to use when writing a Chat application with Tornado Web Framework
2,262,039
<p>We're implementing a Chat server using Tornado.</p> <p>The premise is simple, a user makes open an HTTP ajax connection to the Tornado server, and the Tornado server answers only when a new message appears in the chat-room. Whenever the connection closes, regardless if a new message came in or an error/timeout occu...
0
2010-02-14T17:39:17Z
2,795,094
<p>Tornado has a "chat" example which uses long polling. It contains everything you need (or actually, probably more than you need since it includes a 3rd-party login)</p>
0
2010-05-08T18:04:36Z
[ "python", "asynchronous", "tornado" ]
Google App Engine - Is os.environ reset between requests?
2,262,072
<p>Hello I can't think of a way to test this and was hoping someone here knew the answer... I'm storing some request-specific data in os.environ, and was wondering if that data was going to leak to other requests. Does anyone know?</p> <p>Yes I realize that it's normal to use request.environ for this, and usually I d...
0
2010-02-14T17:48:09Z
2,262,077
<p>Ah-ha! I hadn't searched hard enough, this question has already been answered...</p> <p><a href="http://stackoverflow.com/questions/2259727/request-aware-code-in-google-app-engine-os-environ">http://stackoverflow.com/questions/2259727/request-aware-code-in-google-app-engine-os-environ</a></p>
1
2010-02-14T17:49:08Z
[ "python", "google-app-engine" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
2,262,117
<p>From a RGB integer:</p> <pre><code>Blue = RGBint mod 256 Green = RGBint / 256 mod 256 Red = RGBint / 256 / 256 mod 256 </code></pre> <p>This can be pretty simply implemented once you know how to get it. :)</p> <p>Upd: Added python function. Not sure if there's a better way to do it, but this works on Python 3 ...
12
2010-02-14T17:59:08Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
2,262,148
<p>There's probably a shorter way of doing this:</p> <pre><code>dec=10490586 hex="%06x" % dec r=hex[:2] g=hex[2:4] b=hex[4:6] rgb=(r,g,b) </code></pre> <p>EDIT: this is wrong - gives the answer in Hex, OP wanted int. EDIT2: refined to reduce misery and failure - needed '%06x' to ensure hex is always shown as six digi...
0
2010-02-14T18:05:32Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
2,262,152
<p>I'm not a Python expert by all means, but as far as I know it has the same operators as C. </p> <p>If so this should work and it should also be a lot quicker than using modulo and division.</p> <pre><code>Blue = RGBint &amp; 255 Green = (RGBint &gt;&gt; 8) &amp; 255 Red = (RGBint &gt;&gt; 16) &amp; 255 </code><...
32
2010-02-14T18:06:30Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
2,262,181
<p>I assume you have a 32-bit integer containing the RGB values (e.g. ARGB). Then you can unpack the binary data using the <code>struct</code> module:</p> <pre><code># Create an example value (this represents your 32-bit input integer in this example). # The following line results in exampleRgbValue = binary 0x00FF77F...
5
2010-02-14T18:14:45Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
2,373,760
<pre><code>def unpack2rgb(intcol): tmp, blue= divmod(intcol, 256) tmp, green= divmod(tmp, 256) alpha, red= divmod(tmp, 256) return alpha, red, green, blue </code></pre> <p>If only the <code>divmod(value, (divider1, divider2, divider3…))</code> suggestion was accepted, it would have simplified various...
2
2010-03-03T18:08:05Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
4,296,274
<pre><code>&gt;&gt;&gt; import struct &gt;&gt;&gt; str='aabbcc' &gt;&gt;&gt; struct.unpack('BBB',str.decode('hex')) (170, 187, 204) </code></pre> <p>and</p> <pre><code>&gt;&gt;&gt; rgb = (50,100,150) &gt;&gt;&gt; struct.pack('BBB',*rgb).encode('hex') '326496' </code></pre>
8
2010-11-28T10:05:09Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
8,340,936
<pre><code>&gt;&gt;&gt; r, g, b = (111, 121, 131) &gt;&gt;&gt; packed = int('%02x%02x%02x' % (r, g, b), 16) </code></pre> <p>This produces the following integer:</p> <pre><code>&gt;&gt;&gt; packed 7305603 </code></pre> <p>You can then unpack it either the long explicit way:</p> <pre><code>&gt;&gt;&gt; packed % 256 ...
5
2011-12-01T11:53:59Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
18,603,037
<p>Just a note for anyone using Google's Appengine Images Python API. I found I had a situation where I had to supply a method with a 32-bit RGB color value.</p> <p>Specifically, if you're using the API to convert a PNG (or any image with transparent pixels), you'll need to supply the execute_transforms method with an...
3
2013-09-03T23:26:26Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
31,681,928
<p>If you are using NumPy and you have an array of RGBints, you can also just change its dtype to extract the red, green, blue and alpha components:</p> <pre><code>&gt;&gt;&gt; type(rgbints) numpy.ndarray &gt;&gt;&gt; rgbints.shape (1024L, 768L) &gt;&gt;&gt; rgbints.dtype dtype('int32') &gt;&gt;&gt; rgbints.dtype = dt...
0
2015-07-28T16:19:33Z
[ "python", "integer", "rgb" ]
RGB Int to RGB - Python
2,262,100
<p>How can I convert an RGB integer to the corresponding RGB tuple <code>(R,G,B)</code>? Seems simple enough, but I can't find anything on google.</p> <p>I know that for every RGB <code>(r,g,b)</code> you have the integer <code>n = r256^2 + g256 + b</code>, how can I solve the reverse in Python, IE given an <code>n</c...
11
2010-02-14T17:54:30Z
33,040,479
<p>Adding to what is mentioned above. A concise one-liner alternative.</p> <pre><code># 2003199 or #E190FF is Dodger Blue. tuple((2003199 &gt;&gt; Val) &amp; 255 for Val in (16, 8, 0)) # (30, 144, 255) </code></pre> <p>And to avoid any confusion in the future.</p> <pre><code>from collections import namedtuple RGB = ...
0
2015-10-09T14:11:05Z
[ "python", "integer", "rgb" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
2,262,372
<p>int() is the built-in and pythonic way, just like you have there.</p> <p>It's usually easier and more common to use it directly though:</p> <pre><code>def show_square(user_input): """Example of using int().""" try: num = int(user_input, 10) except ValueError: print "Error" # handle not-an-integer cas...
1
2010-02-14T19:10:35Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
2,262,377
<p>No, it is already perfect. The <code>val</code> parameter could be better named default, though.</p> <p>Documented in the official docs simply as <a href="http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex">int(x) -- x converted to integer</a></p>
7
2010-02-14T19:11:00Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
2,262,384
<p>I would go for:</p> <pre><code>def parse_int(s, base=10, val=None): if s.isdigit(): return int(s, base) else: return val </code></pre> <p>But it's more or less the same thing.</p>
5
2010-02-14T19:13:27Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
2,262,387
<p>That's the pythonic way. In python, it's customary to use EAFP style - Easier to Ask Forgiveness than Permission.<br> That means you'd try first, and then clean up the mess if necessary.</p>
19
2010-02-14T19:15:20Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
2,262,424
<p>This is a pretty regular scenario so I've written an "ignore_exception" decorator that works for all kinds of functions which through excpetions instead of failing gracefully:</p> <pre><code>def ignore_exception(IgnoreException=Exception,DefaultVal=None): """ Decorator for ignoring exception from a function ...
30
2010-02-14T19:26:55Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
9,591,248
<pre><code>def intTryParse(value): try: return int(value), True except ValueError: return value, False </code></pre>
10
2012-03-06T20:22:39Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
15,310,339
<pre><code>def parseint(string): result = '0' for x in string: if x.isdigit(): result+=x else: return int(result) return int(result) </code></pre>
0
2013-03-09T11:51:55Z
[ "python", "parsing", "integer" ]
Is there a built-in or more Pythonic way to try to parse a string to an integer
2,262,333
<p>I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?</p> <pre><code>def try_parse_int(s, base=1...
33
2010-02-14T19:01:31Z
40,114,267
<pre><code>myList = ['12', '13', '5', 'hope', 'despair', '69','0', '1.2'] myInts = [int(x) for x in myList if x.isdigit()] </code></pre>
0
2016-10-18T17:12:48Z
[ "python", "parsing", "integer" ]
Python+SWIG+MinGW - setup.py builds the source and the pyd file, python "cannot find the module"
2,262,351
<p>I'm using MinGW to build a PythonC module using Swig. When I tell Python to load the module, it fails and complains that Python cannot find the module.</p> <p>The funny thing is that the module is in the same directory that I'm running Python in <em>and</em> the module is named <code>_mod.pyd</code>. (I also have g...
1
2010-02-14T19:04:44Z
2,287,469
<p>This is a common problem. You probably are using the binary version of Python for Windows which is compiled with MSC not with MinGW. You can see it looking what the interpreter says when you run it from command line. If it is the case you have two choices:</p> <ul> <li>compile the module with MSC or </li> <li>compi...
1
2010-02-18T09:21:39Z
[ "python", "mingw", "swig" ]
Python+SWIG+MinGW - setup.py builds the source and the pyd file, python "cannot find the module"
2,262,351
<p>I'm using MinGW to build a PythonC module using Swig. When I tell Python to load the module, it fails and complains that Python cannot find the module.</p> <p>The funny thing is that the module is in the same directory that I'm running Python in <em>and</em> the module is named <code>_mod.pyd</code>. (I also have g...
1
2010-02-14T19:04:44Z
3,857,862
<p>I ran across this article on the MinGW site under "How do I create Python extensions?"</p> <p><a href="http://www.mingw.org/wiki/FAQ" rel="nofollow">http://www.mingw.org/wiki/FAQ</a></p> <p>I knew there was a trick with pexports but, I've tested this out a couple of times with little/no success (that I can remembe...
0
2010-10-04T18:00:26Z
[ "python", "mingw", "swig" ]
Python+SWIG+MinGW - setup.py builds the source and the pyd file, python "cannot find the module"
2,262,351
<p>I'm using MinGW to build a PythonC module using Swig. When I tell Python to load the module, it fails and complains that Python cannot find the module.</p> <p>The funny thing is that the module is in the same directory that I'm running Python in <em>and</em> the module is named <code>_mod.pyd</code>. (I also have g...
1
2010-02-14T19:04:44Z
3,967,440
<p>Did you try: <br></p> <blockquote> <p>python setup.py build --compiler=mingw32</p> </blockquote> <p>?</p>
0
2010-10-19T10:36:04Z
[ "python", "mingw", "swig" ]
unexpected list appearing in python loop
2,262,509
<p>I am new to python and have the following piece of test code featuring a nested loop and I'm getting some unexpected lists generated:</p> <pre><code>import pybel import math import openbabel search = ["CCC","CCCC"] matches = [] #n = 0 #b = 0 print search for n in search: print "n=",n sma...
2
2010-02-14T19:53:26Z
2,262,528
<p><code>allmol</code> has 2 items and so you're looping twice with <code>matches</code> being an empty list the second time.</p> <p>Notice how the newline is printed after each; changing that <code>"\n"</code> to <code>"&lt;-- matches"</code> may clear things up for you:</p> <pre><code>print matches, "&lt;-- matches...
1
2010-02-14T19:58:59Z
[ "python", "list", "for-loop", "openbabel" ]
unexpected list appearing in python loop
2,262,509
<p>I am new to python and have the following piece of test code featuring a nested loop and I'm getting some unexpected lists generated:</p> <pre><code>import pybel import math import openbabel search = ["CCC","CCCC"] matches = [] #n = 0 #b = 0 print search for n in search: print "n=",n sma...
2
2010-02-14T19:53:26Z
2,262,717
<p>Perhaps it is supposed to end like this</p> <pre><code>for b in allmol: matches.append(smarts.findall(b)) print matches, "\n" </code></pre> <p>otherwise I'm not sure why you'd initialise matches to an empty list</p> <p>If that is the case, you can instead write</p> <pre><code>matches = [smarts.findall(b)...
0
2010-02-14T20:53:45Z
[ "python", "list", "for-loop", "openbabel" ]
How to make my Python unit tests to import the tested modules if they are in sister folders?
2,262,546
<p>I am still getting my head around the <code>import</code> statement. If I have 2 folders in the same level:</p> <ol> <li>src</li> <li>test</li> </ol> <p>How to make the <code>py</code> files in <code>test</code> import the modules in <code>src</code>? Is there a better solution (like put a folder inside another?)<...
10
2010-02-14T20:03:15Z
2,262,586
<p>The code you want is for using src/module_name.py</p> <pre><code>from src import module_name </code></pre> <p>and the root directory is on your PYTHONPATH e.g. you run from the root directory</p> <p>Your directory structure is what I use but woth the model name instead from src. I got this structure from <a href...
7
2010-02-14T20:12:52Z
[ "python", "import", "folders", "code-organization", "pyunit" ]
How to make my Python unit tests to import the tested modules if they are in sister folders?
2,262,546
<p>I am still getting my head around the <code>import</code> statement. If I have 2 folders in the same level:</p> <ol> <li>src</li> <li>test</li> </ol> <p>How to make the <code>py</code> files in <code>test</code> import the modules in <code>src</code>? Is there a better solution (like put a folder inside another?)<...
10
2010-02-14T20:03:15Z
2,262,862
<p>Try this out:</p> <pre><code>import sys import os sys.path.append(os.path.join('..', 'src')) import module_in_src_folder </code></pre> <p><em>edited to support any platform</em></p>
5
2010-02-14T21:29:22Z
[ "python", "import", "folders", "code-organization", "pyunit" ]
How to make my Python unit tests to import the tested modules if they are in sister folders?
2,262,546
<p>I am still getting my head around the <code>import</code> statement. If I have 2 folders in the same level:</p> <ol> <li>src</li> <li>test</li> </ol> <p>How to make the <code>py</code> files in <code>test</code> import the modules in <code>src</code>? Is there a better solution (like put a folder inside another?)<...
10
2010-02-14T20:03:15Z
2,262,888
<p>I have exactly the same situation as the OP with all the python projects I write:</p> <ul> <li>Project Folder <ol> <li>src</li> <li>test</li> </ol></li> </ul> <p>All modules, whether in src, or test, or subfolders of these always use the form of <code>import</code> that Mark shows in his answer:</p> <pre><code>fr...
0
2010-02-14T21:34:37Z
[ "python", "import", "folders", "code-organization", "pyunit" ]
How can I get the text between tags using python SAX parser?
2,262,577
<p>What I need is just get the text of the corresponding tag and persist it into database. Since the xml file is big (4.5GB) I'm using sax. I used the <em>characters</em> method to get the text and put it in a dictionary. However when I'm printing the text at the <em>endElement</em> method I'm getting a new line instea...
7
2010-02-14T20:11:00Z
2,262,609
<p>The text in the tag is chunked by the SAX processor. <code>characters</code> might be called multiple times.</p> <p>You need to do something like:</p> <pre><code>def startElement(self, name, attrs): self.map[name] = '' self.tag = name def characters(self, content): self.map[self.tag] += content def e...
6
2010-02-14T20:18:20Z
[ "python", "xml", "sax" ]
How to check if a page is displaying a specific <img> tag
2,262,724
<p>What is the best way to determine if a page on a website is REALLY displaying a specific img tag like this <code>&lt;img src=http://domain.com/img.jpg&gt;</code>? A simple string comparison is easy to fool using http comments <code>&lt;!-- --&gt;</code>. Even if the html tag exists it could be deleted with JavaSc...
2
2010-02-14T20:55:38Z
2,262,772
<p>The only surefire way I can think of is to render the page and check. It is simple to strip comments etc. But if scripts are involved, it is not possible to have a general solution that will not amount to executing them (I believe this is the first time I ever invoked Church's theorem...).</p>
1
2010-02-14T21:06:57Z
[ "python", "security", "browser", "web-crawler" ]
How to check if a page is displaying a specific <img> tag
2,262,724
<p>What is the best way to determine if a page on a website is REALLY displaying a specific img tag like this <code>&lt;img src=http://domain.com/img.jpg&gt;</code>? A simple string comparison is easy to fool using http comments <code>&lt;!-- --&gt;</code>. Even if the html tag exists it could be deleted with JavaSc...
2
2010-02-14T20:55:38Z
2,262,796
<p>You could place a script anywhere that processes the request, counts the view and delivers the image like this:</p> <p><a href="http://yourhost.com/imageprocess?image=media/foo/bar.jpg" rel="nofollow">http://yourhost.com/imageprocess?image=media/foo/bar.jpg</a></p> <p>Then you can be sure that the image was loaded...
0
2010-02-14T21:11:43Z
[ "python", "security", "browser", "web-crawler" ]
How to check if a page is displaying a specific <img> tag
2,262,724
<p>What is the best way to determine if a page on a website is REALLY displaying a specific img tag like this <code>&lt;img src=http://domain.com/img.jpg&gt;</code>? A simple string comparison is easy to fool using http comments <code>&lt;!-- --&gt;</code>. Even if the html tag exists it could be deleted with JavaSc...
2
2010-02-14T20:55:38Z
2,264,251
<p>I don't think you can ever be sure. First, you're not even sure the program will <a href="http://en.wikipedia.org/wiki/Halting_problem" rel="nofollow">stop</a>.<br> Aside from that, consider the following scenarios. Your <code>&lt;img&gt;</code> can be added, removed or get obscured using JavaScript, CSS and/or serv...
1
2010-02-15T05:56:50Z
[ "python", "security", "browser", "web-crawler" ]
How to get unit test coverage results in Eclipse + Pydev?
2,262,777
<p>I know Eclipse + PyDev has an option <code>Run As</code> => <code>3 Python Coverage</code>. But all it reports is:</p> <blockquote> <p>Ran 6 tests in 0.001s</p> <p>OK </p> </blockquote> <p>And it says nothing about code coverage. How to get a code coverage report in Pydev?</p>
13
2010-02-14T21:07:28Z
2,262,802
<p>See if this helps <a href="http://www.machine-envy.com/blog/2006/07/29/automated-python-testing-with-nose-and-eclipse/" rel="nofollow">http://www.machine-envy.com/blog/2006/07/29/automated-python-testing-with-nose-and-eclipse/</a></p> <p>It uses nosetests with the <code>–with-coverage</code> option.</p>
0
2010-02-14T21:12:59Z
[ "python", "eclipse", "code-coverage", "pydev" ]
How to get unit test coverage results in Eclipse + Pydev?
2,262,777
<p>I know Eclipse + PyDev has an option <code>Run As</code> => <code>3 Python Coverage</code>. But all it reports is:</p> <blockquote> <p>Ran 6 tests in 0.001s</p> <p>OK </p> </blockquote> <p>And it says nothing about code coverage. How to get a code coverage report in Pydev?</p>
13
2010-02-14T21:07:28Z
2,262,839
<ul> <li>Run a file with "Python Coverage"</li> <li>Window > Show View > Code Coverage Results View</li> <li>Select the directory in which the executed file is</li> <li>Double-click on the executed file in the file list</li> <li>Statistics are now at the right, not executed lines are marked red in the code view</li> </...
13
2010-02-14T21:24:19Z
[ "python", "eclipse", "code-coverage", "pydev" ]
How to get unit test coverage results in Eclipse + Pydev?
2,262,777
<p>I know Eclipse + PyDev has an option <code>Run As</code> => <code>3 Python Coverage</code>. But all it reports is:</p> <blockquote> <p>Ran 6 tests in 0.001s</p> <p>OK </p> </blockquote> <p>And it says nothing about code coverage. How to get a code coverage report in Pydev?</p>
13
2010-02-14T21:07:28Z
5,648,086
<p>Note that in pydev 2.0, the coverage support changed, now, you should first open the coverage view and select the 'enable code coverage for new launches'... after that, any launch you do (regular or unit-test) will have coverage information being gathered (and the results inspection also became a bit more intuitive)...
7
2011-04-13T11:01:40Z
[ "python", "eclipse", "code-coverage", "pydev" ]
Detect the location of an image within a larger image
2,262,832
<p>How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other transformations are conducted on the resulting image. Python code ...
12
2010-02-14T21:21:55Z
2,262,873
<p><a href="http://en.wikipedia.org/wiki/Autocorrelation" rel="nofollow">http://en.wikipedia.org/wiki/Autocorrelation</a> is my first instinct.</p>
2
2010-02-14T21:31:10Z
[ "python", "image", "image-processing", "gdlib" ]
Detect the location of an image within a larger image
2,262,832
<p>How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other transformations are conducted on the resulting image. Python code ...
12
2010-02-14T21:21:55Z
2,262,939
<p>You probably want <a href="http://en.wikipedia.org/wiki/Cross-correlation" rel="nofollow">cross-correlation</a>. (Autocorrelation is correlating a signal with itself; cross correlating is correlating two different signals.)</p> <p>What correlation does for you, over simply checking for exact matches, is that it wi...
3
2010-02-14T21:45:42Z
[ "python", "image", "image-processing", "gdlib" ]
Detect the location of an image within a larger image
2,262,832
<p>How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other transformations are conducted on the resulting image. Python code ...
12
2010-02-14T21:21:55Z
2,264,069
<p>There is a quick and dirty solution, and that's simply sliding a window over the target image and computing some measure of similarity at each location, then picking the location with the highest similarity. Then you compare the similarity to a threshold, if the score is above the threshold, you conclude the image i...
6
2010-02-15T04:46:48Z
[ "python", "image", "image-processing", "gdlib" ]
Detect the location of an image within a larger image
2,262,832
<p>How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other transformations are conducted on the resulting image. Python code ...
12
2010-02-14T21:21:55Z
2,264,086
<p>Take a look at <a href="http://en.wikipedia.org/wiki/Scale-invariant_feature_transform" rel="nofollow">Scale-Invariant Feature Transforms</a>; there are many different flavors that may be more or less tailored to the type of images you happen to be working with.</p>
2
2010-02-15T04:56:32Z
[ "python", "image", "image-processing", "gdlib" ]
Detect the location of an image within a larger image
2,262,832
<p>How do you detect the location of an image within a larger image? I have an unmodified copy of the image. This image is then changed to an arbitrary resolution and placed randomly within a much larger image which is of an arbitrary size. No other transformations are conducted on the resulting image. Python code ...
12
2010-02-14T21:21:55Z
2,264,120
<p>Reduce the colors in both images, turn the edges into vectors, then find similar vectors within both images. This will probably allow you to find both location and scale.</p> <p>I have absolutely no idea to code this all up though.</p>
1
2010-02-15T05:08:17Z
[ "python", "image", "image-processing", "gdlib" ]
Verify source of HTTP request
2,262,852
<p>I've got two systems that need to talk. The systems are setup likeso:</p> <p><code>System A</code>, running Django (Python 2.5) on Google App Engine (GAE)</p> <p><code>System B</code>, running Django (Python 2.6) on Ubuntu/Linux over Lighttpd (maybe nginx, later)</p> <p>System A will periodically make requests ('...
4
2010-02-14T21:26:58Z
2,263,043
<p>It sounds like it would suffice to use SSL on the link and include a password in the query string.</p> <p>SSL keeps out sniffers, and you're not going to reveal the queries outside systems under your control, so a shared secret will do (and will be more secure than IP tracking, as other GAE sites will use those add...
2
2010-02-14T22:15:00Z
[ "python", "django", "google-app-engine", "encryption", "cryptography" ]
Verify source of HTTP request
2,262,852
<p>I've got two systems that need to talk. The systems are setup likeso:</p> <p><code>System A</code>, running Django (Python 2.5) on Google App Engine (GAE)</p> <p><code>System B</code>, running Django (Python 2.6) on Ubuntu/Linux over Lighttpd (maybe nginx, later)</p> <p>System A will periodically make requests ('...
4
2010-02-14T21:26:58Z
2,263,057
<p>You are spot on, the first two bullet points are no use.</p> <p>A password as <a href="http://stackoverflow.com/questions/2262852/verify-source-of-http-request/2263043#2263043">Andrew</a> says is good enough, unless you are worried about issues on browser caches. If you are then you should still use SSL, but to aut...
1
2010-02-14T22:21:41Z
[ "python", "django", "google-app-engine", "encryption", "cryptography" ]
Verify source of HTTP request
2,262,852
<p>I've got two systems that need to talk. The systems are setup likeso:</p> <p><code>System A</code>, running Django (Python 2.5) on Google App Engine (GAE)</p> <p><code>System B</code>, running Django (Python 2.6) on Ubuntu/Linux over Lighttpd (maybe nginx, later)</p> <p>System A will periodically make requests ('...
4
2010-02-14T21:26:58Z
2,263,885
<p>System A can send HTTPS requests via <a href="http://code.google.com/appengine/docs/python/urlfetch/fetchfunction.html" rel="nofollow">urlfetch.fetch</a> -- just have the URL parameter start with <code>https://</code>. However this does not authenticate it to system B (it does prevent sniffers and man-in-the-middle...
1
2010-02-15T03:08:34Z
[ "python", "django", "google-app-engine", "encryption", "cryptography" ]
Verify source of HTTP request
2,262,852
<p>I've got two systems that need to talk. The systems are setup likeso:</p> <p><code>System A</code>, running Django (Python 2.5) on Google App Engine (GAE)</p> <p><code>System B</code>, running Django (Python 2.6) on Ubuntu/Linux over Lighttpd (maybe nginx, later)</p> <p>System A will periodically make requests ('...
4
2010-02-14T21:26:58Z
17,011,877
<p>One bullet proof option is to look at FOAF + SSL <a href="http://www.w3.org/wiki/Foaf%2Bssl/FAQ#What_is_FOAF.2BSSL" rel="nofollow">http://www.w3.org/wiki/Foaf%2Bssl/FAQ#What_is_FOAF.2BSSL</a></p>
1
2013-06-09T16:48:44Z
[ "python", "django", "google-app-engine", "encryption", "cryptography" ]
Converting from ascii to utf-8 with Python
2,262,879
<p>I have xmpp bot written in python. One of it's plugins is able to execute OS commands and send output to the user. As far as I know output should be unicode-like to send it over xmpp protocol. So I tried to handle it this way:</p> <pre><code>output = os.popen(cmd).read() if not isinstance(output, unicode): outp...
3
2010-02-14T21:32:08Z
2,262,896
<p>Ascii has no defined character values above 127 0x7F. Perhaps you mean the Cyrillic code page? It's 866 </p> <p>See <a href="http://en.wikipedia.org/wiki/Code_page" rel="nofollow">http://en.wikipedia.org/wiki/Code_page</a></p> <p>edit: since this answer was marked correct presumably 886 worked, but as other answ...
2
2010-02-14T21:35:57Z
[ "python", "utf-8", "ascii", "command-prompt" ]
Converting from ascii to utf-8 with Python
2,262,879
<p>I have xmpp bot written in python. One of it's plugins is able to execute OS commands and send output to the user. As far as I know output should be unicode-like to send it over xmpp protocol. So I tried to handle it this way:</p> <pre><code>output = os.popen(cmd).read() if not isinstance(output, unicode): outp...
3
2010-02-14T21:32:08Z
2,262,905
<p><code>sys.getdefaultencoding()</code> returns <strong>python's</strong> default encoding - which is ASCII unless you have changed it. ASCII doesn't support Russian characters.</p> <p>You need to work out what encoding the actual text is, either manually, or using the <a href="http://docs.python.org/library/locale.h...
3
2010-02-14T21:38:07Z
[ "python", "utf-8", "ascii", "command-prompt" ]
Converting from ascii to utf-8 with Python
2,262,879
<p>I have xmpp bot written in python. One of it's plugins is able to execute OS commands and send output to the user. As far as I know output should be unicode-like to send it over xmpp protocol. So I tried to handle it this way:</p> <pre><code>output = os.popen(cmd).read() if not isinstance(output, unicode): outp...
3
2010-02-14T21:32:08Z
2,263,171
<p>In Python 'cp855', 'cp866', 'cp1251', 'iso8859_5', 'koi8_r' are differing Russian code pages. You'll need to use the right one to decode the output of popen. In the Windows console, the 'chcp' command lists the code page used by console commands. That won't necessarily be the same code page as Windows application...
0
2010-02-14T22:48:51Z
[ "python", "utf-8", "ascii", "command-prompt" ]
Converting from ascii to utf-8 with Python
2,262,879
<p>I have xmpp bot written in python. One of it's plugins is able to execute OS commands and send output to the user. As far as I know output should be unicode-like to send it over xmpp protocol. So I tried to handle it this way:</p> <pre><code>output = os.popen(cmd).read() if not isinstance(output, unicode): outp...
3
2010-02-14T21:32:08Z
2,263,456
<p>You say """sys.getdefaultencoding() says that default command prompt encoding is 'ascii'"""</p> <p>sys.getdefaultencoding says NOTHING about the "command prompt" encoding.</p> <p>On Windows, <code>sys.stdout.encoding</code> should do the job. On my machine, it contains <code>cp850</code> when Python is run in a Co...
1
2010-02-15T00:28:40Z
[ "python", "utf-8", "ascii", "command-prompt" ]
mod_python not detecting files when using open()
2,263,005
<p>I am trying to open a file I have in my /var/www/ directory named cardlist.xml. this is the code I am using.</p> <pre><code>import cgi import os open("./cardlist.xml", "r") def crawlXml(): return 0 </code></pre> <p>My error is</p> <blockquote> <p>MOD_PYTHON ERROR</p> <p>ProcessId: 11361 Interprete...
0
2010-02-14T22:04:05Z
2,263,072
<p>The working directory might not be the directory of the file. Try using an absolute path, or an explicitly relative path:</p> <pre><code>import os.path open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cardlist.xml')) </code></pre>
3
2010-02-14T22:27:16Z
[ "python", "apache2", "mod-python" ]
How to calculate positions of holes in a game board?
2,263,121
<p>I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.</p> <p>I've split the game board generation into several parts.</p> <p><strong>Part one</strong> generates the board h...
2
2010-02-14T22:39:29Z
2,263,262
<p>The bit about accuracy becomes relatively important as soon as we realize these points are going to be converted to pixel coordinates, a.k.a. integers. Accumulate an error of 0.5 and bam! You're <strong>one</strong> pixel off.</p> <p>So, either there is a huge problem with accuracy and rounding errors are climbing ...
0
2010-02-14T23:19:24Z
[ "python", "language-agnostic", "rounding", "precision" ]
How to calculate positions of holes in a game board?
2,263,121
<p>I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.</p> <p>I've split the game board generation into several parts.</p> <p><strong>Part one</strong> generates the board h...
2
2010-02-14T22:39:29Z
2,263,297
<p>If hole positions are stored as integers, I don't doubt rounding error accumulates quickly enough to kill you. If hole positions are stored as floating point, and if you have an error of one unit in the last place (ULP) at each computation, I'm not quite sure how quickly error accumulates&mdash;but if error doubles...
1
2010-02-14T23:28:50Z
[ "python", "language-agnostic", "rounding", "precision" ]
How to calculate positions of holes in a game board?
2,263,121
<p>I'm making a game with Python->PyGame->Albow and ran into a problem with board generation. However I'll try to explain the problem in a language agnostic way. I believe it's not related to python.</p> <p>I've split the game board generation into several parts.</p> <p><strong>Part one</strong> generates the board h...
2
2010-02-14T22:39:29Z
2,263,645
<p>I hate to be the one to suggest this, but, start in the the center. Also, you should look at your code and double check for an unfortunate conversion. The is, if a hole ends up at "138.2, 150.8", you need to keep the fractional parts until you have computed the next hole.</p>
0
2010-02-15T01:54:13Z
[ "python", "language-agnostic", "rounding", "precision" ]
Inter-database communications in PostgreSQL
2,263,132
<p>I am using PostgreSQL 8.4. I really like the new unnest() and array_agg() features; it is about time they realize the dynamic processing potential of their Arrays!</p> <p>Anyway, I am working on web server back ends that uses long Arrays a lot. Their will be two successive processes which will each occur on a diffe...
3
2010-02-14T22:40:54Z
2,263,569
<p>not sure I totally understand, but you've looked at notify/listen? <a href="http://www.postgresql.org/docs/8.1/static/sql-listen.html" rel="nofollow">http://www.postgresql.org/docs/8.1/static/sql-listen.html</a></p>
1
2010-02-15T01:21:37Z
[ "python", "arrays", "postgresql", "database-connection" ]
Inter-database communications in PostgreSQL
2,263,132
<p>I am using PostgreSQL 8.4. I really like the new unnest() and array_agg() features; it is about time they realize the dynamic processing potential of their Arrays!</p> <p>Anyway, I am working on web server back ends that uses long Arrays a lot. Their will be two successive processes which will each occur on a diffe...
3
2010-02-14T22:40:54Z
2,277,362
<p>I am thinking either listen/notify or something with a cache such as memcache. You would send the key to memcache and have the second python app retrieve it from there. You could even do it with listen/notify... e.g; send the key and notify your second app that the key is in memcache waiting to be retrieved.</p>
0
2010-02-17T00:04:13Z
[ "python", "arrays", "postgresql", "database-connection" ]
Inter-database communications in PostgreSQL
2,263,132
<p>I am using PostgreSQL 8.4. I really like the new unnest() and array_agg() features; it is about time they realize the dynamic processing potential of their Arrays!</p> <p>Anyway, I am working on web server back ends that uses long Arrays a lot. Their will be two successive processes which will each occur on a diffe...
3
2010-02-14T22:40:54Z
2,277,627
<p>Sounds like you want <a href="http://www.postgresql.org/docs/8.4/static/contrib-dblink.html" rel="nofollow">dblink</a> from contrib. This allows some inter-db postgres communication. The pg docs are great and should provide the needed examples.</p>
1
2010-02-17T00:59:46Z
[ "python", "arrays", "postgresql", "database-connection" ]
How can I send data to a base template in Django?
2,263,258
<p>Let's say I have a django site, and a base template for all pages with a footer that I want to display a list of the top 5 products on my site. How would I go about sending that list to the base template to render? Does every view need to send that data to the render_to_response? Should I use a template_tag? How wou...
2
2010-02-14T23:17:02Z
2,263,281
<p>You should use a <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors" rel="nofollow">custom context processor</a>. With this you can set a variable e.g. <code>top_products</code> that will be available in all your templates.</p> <p>E.g.</p> <pre><code># in project/a...
4
2010-02-14T23:25:38Z
[ "python", "django", "django-templates" ]
How can I send data to a base template in Django?
2,263,258
<p>Let's say I have a django site, and a base template for all pages with a footer that I want to display a list of the top 5 products on my site. How would I go about sending that list to the base template to render? Does every view need to send that data to the render_to_response? Should I use a template_tag? How wou...
2
2010-02-14T23:17:02Z
35,047,296
<p>Just a heads up for anyone using later versions of Django (i.e. 1.9): the way that Django defines its context processors has changed. Instead of adding to <code>TEMPLATE_CONTEXT_PROCESSORS</code> you can just add a line to Django's list of context processors:</p> <pre><code>TEMPLATES = [ { 'BACKEND': 'django.te...
-1
2016-01-27T20:23:13Z
[ "python", "django", "django-templates" ]
Generating & Merging PDF Files in Python
2,263,263
<p>I want to automatically generate booking confirmation PDF files in Python. Most of the content will be static (i.e. logos, booking terms, phone numbers), with a few dynamic bits (dates, costs, etc).</p> <p>From the user side, the simplest way to do this would be to start with a PDF file with the static content, an...
4
2010-02-14T23:19:38Z
2,263,276
<p>You could generate a document through, for example, TeX, or OpenOffice, or whatever gives you the most comfortable bindings and then print the document with a pdf printer.</p> <p>This allows you not to have to figure out where to put fields precisely or figure out what to do if your content overflows the space allo...
1
2010-02-14T23:24:08Z
[ "python", "pdf", "merge", "reportlab", "pypdf" ]
Generating & Merging PDF Files in Python
2,263,263
<p>I want to automatically generate booking confirmation PDF files in Python. Most of the content will be static (i.e. logos, booking terms, phone numbers), with a few dynamic bits (dates, costs, etc).</p> <p>From the user side, the simplest way to do this would be to start with a PDF file with the static content, an...
4
2010-02-14T23:19:38Z
2,263,296
<p>Look into docutils and reSTructuredText. You could quickly write out your PDF document in reST and then compile the PDF using rst2pdf.py</p> <p>I've used this, it creates very beautiful documents and the markup is extensible! Later you could take the same code and run it into rst2html to create a website out if i...
2
2010-02-14T23:28:04Z
[ "python", "pdf", "merge", "reportlab", "pypdf" ]
Generating & Merging PDF Files in Python
2,263,263
<p>I want to automatically generate booking confirmation PDF files in Python. Most of the content will be static (i.e. logos, booking terms, phone numbers), with a few dynamic bits (dates, costs, etc).</p> <p>From the user side, the simplest way to do this would be to start with a PDF file with the static content, an...
4
2010-02-14T23:19:38Z
2,263,405
<blockquote> <p>From the user side, the simplest way to do this would be to start with a PDF file with the static content, and then using python to just add the dynamic parts. Is this a simple process?</p> </blockquote> <p>Unfortunately no. There are several tools that are good at producing PDFs from scratch (most c...
8
2010-02-15T00:11:23Z
[ "python", "pdf", "merge", "reportlab", "pypdf" ]
Are Python 2.5 .pyc files compatible with Python 2.6 .pyc files?
2,263,356
<p>A while ago I had to upgrade some servers from Python 2.4 to Python 2.5. I found that .pyc files created under Python 2.4 would crash when Python 2.5 tried to run them.</p> <p>Will this happen again when I upgrade from 2.5 to 2.6?</p> <p>EDIT: Here is a bit more detail</p> <p>I have a fileserver that contains the...
14
2010-02-14T23:48:52Z
2,263,375
<p>The Python version that creates the file is stored in the .pyc file itself. Usually this means that the .pyc is replaced by one with the correct Python version</p> <p>some reasons this might not happen<br> - permissions<br> - .py file is not available</p> <p>In the case of permission problem, Python will just use ...
2
2010-02-14T23:54:22Z
[ "python" ]
Are Python 2.5 .pyc files compatible with Python 2.6 .pyc files?
2,263,356
<p>A while ago I had to upgrade some servers from Python 2.4 to Python 2.5. I found that .pyc files created under Python 2.4 would crash when Python 2.5 tried to run them.</p> <p>Will this happen again when I upgrade from 2.5 to 2.6?</p> <p>EDIT: Here is a bit more detail</p> <p>I have a fileserver that contains the...
14
2010-02-14T23:48:52Z
2,263,379
<p>You will certainly need to recompile the bytecode files for them to be of any use. The bytecode magic number has changed over every major version of Python(*).</p> <p>However, having non-version-matching bytecode files shouldn't ever crash Python. It will generally just ignore any bytecode that doesn't have the cor...
0
2010-02-14T23:57:04Z
[ "python" ]
Are Python 2.5 .pyc files compatible with Python 2.6 .pyc files?
2,263,356
<p>A while ago I had to upgrade some servers from Python 2.4 to Python 2.5. I found that .pyc files created under Python 2.4 would crash when Python 2.5 tried to run them.</p> <p>Will this happen again when I upgrade from 2.5 to 2.6?</p> <p>EDIT: Here is a bit more detail</p> <p>I have a fileserver that contains the...
14
2010-02-14T23:48:52Z
2,263,382
<p>In general, <code>.pyc</code> files are specific to one Python version (although portable across different machine architectures, as long as they're running the same version); the files carry the information about the relevant Python version in their headers -- so, if you leave the corresponding <code>.py</code> fil...
10
2010-02-14T23:58:29Z
[ "python" ]
Are Python 2.5 .pyc files compatible with Python 2.6 .pyc files?
2,263,356
<p>A while ago I had to upgrade some servers from Python 2.4 to Python 2.5. I found that .pyc files created under Python 2.4 would crash when Python 2.5 tried to run them.</p> <p>Will this happen again when I upgrade from 2.5 to 2.6?</p> <p>EDIT: Here is a bit more detail</p> <p>I have a fileserver that contains the...
14
2010-02-14T23:48:52Z
2,263,434
<p>If you have the source code then it will recompile it for you. So in general you are okay. </p> <p>But, this could be bad for you if users with difference versions of Python run from a central installation directory. </p> <p>It could also be bad if you just have the pyc files. I just ran a quick test for you. ...
4
2010-02-15T00:19:58Z
[ "python" ]
Are Python 2.5 .pyc files compatible with Python 2.6 .pyc files?
2,263,356
<p>A while ago I had to upgrade some servers from Python 2.4 to Python 2.5. I found that .pyc files created under Python 2.4 would crash when Python 2.5 tried to run them.</p> <p>Will this happen again when I upgrade from 2.5 to 2.6?</p> <p>EDIT: Here is a bit more detail</p> <p>I have a fileserver that contains the...
14
2010-02-14T23:48:52Z
3,453,542
<p>See <a href="http://www.python.org/dev/peps/pep-3149/" rel="nofollow">http://www.python.org/dev/peps/pep-3149/</a> for a proposed fix for this (hopefully in Python 3.2)</p>
1
2010-08-10T21:20:52Z
[ "python" ]
Reading from Python dict if key might not be present
2,263,761
<p>I am very new to Python and parsing data.</p> <p>I can pull an external JSON feed into a Python dictionary and iterate over the dictionary. </p> <pre><code>for r in results: print r['key_name'] </code></pre> <p>As I walk through the results returned, I am getting an error when a key does not have a value (a...
20
2010-02-15T02:31:46Z
2,263,776
<p>use has_key() , and that will return true or false</p>
0
2010-02-15T02:36:11Z
[ "python", "json" ]
Reading from Python dict if key might not be present
2,263,761
<p>I am very new to Python and parsing data.</p> <p>I can pull an external JSON feed into a Python dictionary and iterate over the dictionary. </p> <pre><code>for r in results: print r['key_name'] </code></pre> <p>As I walk through the results returned, I am getting an error when a key does not have a value (a...
20
2010-02-15T02:31:46Z
2,263,826
<p>The preferred way, when applicable:</p> <pre><code>for r in results: print r.get('key_name') </code></pre> <p>this will simply print <code>None</code> if <code>key_name</code> is not a key in the dictionary. You can also have a different default value, just pass it as the second argument:</p> <pre><code>for...
53
2010-02-15T02:48:20Z
[ "python", "json" ]
Reading from Python dict if key might not be present
2,263,761
<p>I am very new to Python and parsing data.</p> <p>I can pull an external JSON feed into a Python dictionary and iterate over the dictionary. </p> <pre><code>for r in results: print r['key_name'] </code></pre> <p>As I walk through the results returned, I am getting an error when a key does not have a value (a...
20
2010-02-15T02:31:46Z
2,263,854
<p>[Updated to remove careless mistake]</p> <p>You could also do something like this:</p> <pre><code>for r in (row for row in results if 'a' in row): print r['a'] </code></pre> <p>This uses a generator expression to pick "rows" out of "results" where "row" includes the key "a".</p> <p>Here's a little test scrip...
0
2010-02-15T02:59:06Z
[ "python", "json" ]
Reading from Python dict if key might not be present
2,263,761
<p>I am very new to Python and parsing data.</p> <p>I can pull an external JSON feed into a Python dictionary and iterate over the dictionary. </p> <pre><code>for r in results: print r['key_name'] </code></pre> <p>As I walk through the results returned, I am getting an error when a key does not have a value (a...
20
2010-02-15T02:31:46Z
2,263,860
<p>If possible, use the simplejson library for managing JSON data.</p>
1
2010-02-15T03:01:16Z
[ "python", "json" ]
How to upload all .html files to a remote server using FTP and preserving file structure?
2,263,782
<p>I have edited about 100 html files locally, and now I want to push them to my live server, which I can only access via ftp.</p> <p>The HTML files are in many different directories, but hte directory structure on the remote machine is the same as on the local machine.</p> <p>How can I recursively descend from my to...
0
2010-02-15T02:37:33Z
2,263,804
<p>umm, maybe by pressing F5 in mc for linux or total commander for windows?</p>
0
2010-02-15T02:44:12Z
[ "python", "networking", "scripting", "ftp" ]
How to upload all .html files to a remote server using FTP and preserving file structure?
2,263,782
<p>I have edited about 100 html files locally, and now I want to push them to my live server, which I can only access via ftp.</p> <p>The HTML files are in many different directories, but hte directory structure on the remote machine is the same as on the local machine.</p> <p>How can I recursively descend from my to...
0
2010-02-15T02:37:33Z
2,263,838
<p>If you want to do it in Python (rather than using other pre-packaged existing tools), you can use <a href="http://docs.python.org/library/os.html?highlight=os.walk#os.walk" rel="nofollow">os.walk</a> to read everything in the local subtree, and <a href="http://docs.python.org/library/ftplib.html?highlight=ftp#module...
1
2010-02-15T02:54:01Z
[ "python", "networking", "scripting", "ftp" ]
How to upload all .html files to a remote server using FTP and preserving file structure?
2,263,782
<p>I have edited about 100 html files locally, and now I want to push them to my live server, which I can only access via ftp.</p> <p>The HTML files are in many different directories, but hte directory structure on the remote machine is the same as on the local machine.</p> <p>How can I recursively descend from my to...
0
2010-02-15T02:37:33Z
2,263,878
<p>After searching PyPI, I found ftptool (http://pypi.python.org/pypi/ftptool/0.4.2). Its <code>mirror_to_remote</code> method could be what you need. I don't have an FTP server handy, though, so I couldn't test it.</p>
0
2010-02-15T03:07:10Z
[ "python", "networking", "scripting", "ftp" ]
How to upload all .html files to a remote server using FTP and preserving file structure?
2,263,782
<p>I have edited about 100 html files locally, and now I want to push them to my live server, which I can only access via ftp.</p> <p>The HTML files are in many different directories, but hte directory structure on the remote machine is the same as on the local machine.</p> <p>How can I recursively descend from my to...
0
2010-02-15T02:37:33Z
2,299,546
<p>if you have a mac, you can try cyberduck. It's good for syncing remote directory structures via ftp.</p>
0
2010-02-19T21:03:12Z
[ "python", "networking", "scripting", "ftp" ]
how to work with strings and integers as bit strings in python?
2,264,130
<p>I'm developing a Genetic Algorithm in python were chromosomes are composed of strings and integers. To apply the genetic operations, I want to convert these groups of integers and strings into bit strings.</p> <p>For example, if one chromosome is:</p> <pre><code>["Hello", 4, "anotherString"] </code></pre> <p>I'd ...
0
2010-02-15T05:12:02Z
2,264,158
<p>Once you describe exactly how the translation from strings to bitstrings should go, the "how" should be fairly easy. If the genetic algorithms should work on a bit-level then obviously a bit level string makes sense, but it is probably way slower than using numbers or character strings.</p>
0
2010-02-15T05:24:57Z
[ "python", "artificial-intelligence", "bit-manipulation", "genetic-algorithm", "data-conversion" ]
how to work with strings and integers as bit strings in python?
2,264,130
<p>I'm developing a Genetic Algorithm in python were chromosomes are composed of strings and integers. To apply the genetic operations, I want to convert these groups of integers and strings into bit strings.</p> <p>For example, if one chromosome is:</p> <pre><code>["Hello", 4, "anotherString"] </code></pre> <p>I'd ...
0
2010-02-15T05:12:02Z
2,264,214
<p>You can turn strings and integers into bytestrings (and back) with the <a href="http://docs.python.org/library/struct.html?highlight=struct#module-struct" rel="nofollow">struct</a> module, and that's exactly 8 bits to a byte. If for some reason you want these binary bytestrings as text strings made up of <code>0</c...
3
2010-02-15T05:43:29Z
[ "python", "artificial-intelligence", "bit-manipulation", "genetic-algorithm", "data-conversion" ]
how to work with strings and integers as bit strings in python?
2,264,130
<p>I'm developing a Genetic Algorithm in python were chromosomes are composed of strings and integers. To apply the genetic operations, I want to convert these groups of integers and strings into bit strings.</p> <p>For example, if one chromosome is:</p> <pre><code>["Hello", 4, "anotherString"] </code></pre> <p>I'd ...
0
2010-02-15T05:12:02Z
2,264,554
<p>Hey.<br> Converting everything into one concatenated string, and than applying genetic operations doesn't seem to be the best idea. Genetic operations can break here many things (especially if you have some constrains on individuals), additionally effectiveness of such solution is probably low. I would suggest diffe...
3
2010-02-15T07:48:07Z
[ "python", "artificial-intelligence", "bit-manipulation", "genetic-algorithm", "data-conversion" ]
twisted.web2 and spawining threads for synchronous code?
2,264,135
<p>So, I'm writing a python web application using the twisted web2 framework. There's a library that I need to use (SQLAlchemy, to be specific) that doesn't have asynchronous code. Would it be bad to spawn a thread to handle the request, fetch any data from the DB, and then return a response? I'm afraid that if there w...
1
2010-02-15T05:14:26Z
2,264,222
<p>See <a href="http://twistedmatrix.com/documents/current/core/howto/threading.html" rel="nofollow">the docs</a>, and specifically the <a href="http://twistedmatrix.com/documents/current/core/howto/threading.html#auto3" rel="nofollow">thread pool</a> which lets you control how many threads are active at most. Spawnin...
0
2010-02-15T05:45:02Z
[ "python", "multithreading", "twisted" ]
Unwind a function call
2,264,137
<p>This is a difficult problem to describe so please let me know if anything is unclear.</p> <p>I am trying to solve a possible deadlock situation in my C++ app and I am having trouble visualizing an appropriate solution. The restrictions placed on me by the two libraries I am trying to connect make my problem very c...
0
2010-02-15T05:14:45Z
2,264,224
<p>If you're destroying the timer handler, I figure you're exiting the program. Before you try to exit and begin killing the timers, can you set a flag to prevent Action 1 and have Thread 1 terminate itself? I hope I'm reading your diagram right, because it doesn't exactly match with the text...</p>
0
2010-02-15T05:45:40Z
[ "c++", "python", "multithreading", "boost-python" ]
Library to render Directed Graphs (similar to graphviz) on Google App Engine
2,264,157
<p>I am looking for a Java or Python library that can render graphs in the Dot language as image file. The problem is that I need a library that I can use on Google App Engine. Basically I am looking for a library that can convert the text description of a directed graph into an image of the graph.</p> <p>For example...
17
2010-02-15T05:23:31Z
2,265,826
<p>I do not think there is such pure python library, the best you can do is use <a href="https://networkx.lanl.gov)" rel="nofollow">NetworkX</a>, it can draw using matplotlib or pygraphviz. Maybe you can modify networkx's matplotlib code to draw on server side, <a href="https://networkx.lanl.gov/trac/browser/networkx/t...
0
2010-02-15T12:22:47Z
[ "java", "python", "google-app-engine", "graph", "graphviz" ]
Library to render Directed Graphs (similar to graphviz) on Google App Engine
2,264,157
<p>I am looking for a Java or Python library that can render graphs in the Dot language as image file. The problem is that I need a library that I can use on Google App Engine. Basically I am looking for a library that can convert the text description of a directed graph into an image of the graph.</p> <p>For example...
17
2010-02-15T05:23:31Z
2,277,282
<p>You could take a look at the flash based <a href="http://flare.prefuse.org/" rel="nofollow">perfuse project</a> if just need to display a graph and not having it embedded as an image is acceptable.</p> <p>They have some example applications of the library such as this <a href="http://flare.prefuse.org/apps/dependen...
0
2010-02-16T23:46:47Z
[ "java", "python", "google-app-engine", "graph", "graphviz" ]
Library to render Directed Graphs (similar to graphviz) on Google App Engine
2,264,157
<p>I am looking for a Java or Python library that can render graphs in the Dot language as image file. The problem is that I need a library that I can use on Google App Engine. Basically I am looking for a library that can convert the text description of a directed graph into an image of the graph.</p> <p>For example...
17
2010-02-15T05:23:31Z
2,295,775
<p><a href="http://code.google.com/p/canviz/">Canviz</a> is what you are looking for: it is a JavaScript library for drawing Graphviz graphs to a web browser canvas. It works with <a href="http://code.google.com/p/canviz/wiki/Browsers">most browsers</a>.</p> <blockquote> <p>Using Canviz has advantages for your web a...
16
2010-02-19T11:04:14Z
[ "java", "python", "google-app-engine", "graph", "graphviz" ]
Library to render Directed Graphs (similar to graphviz) on Google App Engine
2,264,157
<p>I am looking for a Java or Python library that can render graphs in the Dot language as image file. The problem is that I need a library that I can use on Google App Engine. Basically I am looking for a library that can convert the text description of a directed graph into an image of the graph.</p> <p>For example...
17
2010-02-15T05:23:31Z
3,168,462
<p>Google Charts API <a href="http://code.google.com/apis/chart/docs/gallery/graphviz.html" rel="nofollow">now supports GraphViz experimentally</a>. (Note that the entire Image Charts project is officially deprecated.)</p>
12
2010-07-02T19:06:48Z
[ "java", "python", "google-app-engine", "graph", "graphviz" ]