title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
728,171
<p>EDIT: removed the bad advice...</p> <p>You can also use arrays like the java example if you like, but just overwriting it should be enough.</p> <p><a href="http://docs.python.org/library/array.html" rel="nofollow">http://docs.python.org/library/array.html</a></p>
-2
2009-04-08T01:17:11Z
[ "python", "security", "passwords" ]
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
728,176
<p>Store the password in a list, and if you just set the list to null, the memory of the array stored in the list is automatically freed.</p>
-2
2009-04-08T01:20:09Z
[ "python", "security", "passwords" ]
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
728,237
<p>Python doesn't have that low of a level of control over memory. Accept it, and move on. The <strong>best</strong> you can do is to <code>del password</code> after calling <code>mail.login</code> so that no references to the password string object remain. Any solution that purports to be able to do more than that ...
29
2009-04-08T02:01:43Z
[ "python", "security", "passwords" ]
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
728,288
<p>If you don't need the mail object to persist once you are done with it, I think your best bet is to perform the mailing work in a subprocess (see the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module.) That way, when the subprocess dies, so goes your password.</p>
5
2009-04-08T02:26:42Z
[ "python", "security", "passwords" ]
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
1,753,654
<p>There actually -is- a way to securely erase strings in Python; use the memset C function, as per <a href="http://stackoverflow.com/questions/982682/mark-data-as-sensitive-in-python/983525#983525">http://stackoverflow.com/questions/982682/mark-data-as-sensitive-in-python/983525#983525</a></p>
12
2009-11-18T04:32:22Z
[ "python", "security", "passwords" ]
Securely Erasing Password in Memory (Python)
728,164
<p>How do you store a password entered by the user in memory and erase it securely after it is no longer need?</p> <p>To elaborate, currently we have the following code:</p> <pre><code>username = raw_input('User name: ') password = getpass.getpass() mail = imaplib.IMAP4(MAIL_HOST) mail.login(username, password) </cod...
23
2009-04-08T01:13:20Z
31,590,819
<p>This could be done using numpy chararray:</p> <pre><code>import numpy as np username = raw_input('User name: ') mail = imaplib.IMAP4(MAIL_HOST) x = np.chararray((20,)) x[:] = list("{:&lt;20}".format(raw_input('Password: '))) mail.login(username, x.tobytes().strip()) x[:] = '' </code></pre> <p>You would have to de...
0
2015-07-23T14:47:47Z
[ "python", "security", "passwords" ]
Python/urllib suddenly stops working properly
728,193
<p>I'm writing a little tool to monitor class openings at my school.</p> <p>I wrote a python script that will fetch the current availablity of classes from each department every few minutes.</p> <p>The script was functioning properly until the uni's site started returning this:</p> <pre><code>SIS Server is not avail...
1
2009-04-08T01:31:58Z
728,210
<p>After seeing multiple requests from an odd non-browser User-Agent string, it's possible that they are blocking users not being referred to from the site. For example, PHP has a feature called <code>$_SERVER['HTTP_REFERRER']</code> IIRC, which will check the page which reffered the user to the current one. Since your...
0
2009-04-08T01:42:34Z
[ "python", "urllib" ]
Python/urllib suddenly stops working properly
728,193
<p>I'm writing a little tool to monitor class openings at my school.</p> <p>I wrote a python script that will fetch the current availablity of classes from each department every few minutes.</p> <p>The script was functioning properly until the uni's site started returning this:</p> <pre><code>SIS Server is not avail...
1
2009-04-08T01:31:58Z
728,510
<p>This post doesn't attempt to fix your code, but suggest a debugging tool.</p> <p>Once upon a time I was coding a program to fill out online forms for me. To learn exactly how my browser was handling the POSTs, and cookies, and whatnot, I installed WireShark ( <a href="http://www.wireshark.org/" rel="nofollow">http...
2
2009-04-08T04:20:00Z
[ "python", "urllib" ]
How to convert html entities into symbols?
728,296
<p>I have made some adaptations to <a href="http://stackoverflow.com/questions/6936/using-what-ive-learned-from-stackoverflow-html-scraper/50772#50772">the script from this answer</a>. and I am having problems with unicode. Some of the questions end up being written poorly.</p> <p>Some answers and responses end up loo...
0
2009-04-08T02:32:18Z
728,818
<p>You should be able to convert HTMl/XML entities into Unicode characters. Check out this answer in SO:</p> <p><a href="http://stackoverflow.com/questions/628332/decoding-html-entities-with-python">http://stackoverflow.com/questions/628332/decoding-html-entities-with-python</a></p> <p>Basically you want something li...
1
2009-04-08T06:58:41Z
[ "python", "unicode", "beautifulsoup", "html-entities" ]
How to convert html entities into symbols?
728,296
<p>I have made some adaptations to <a href="http://stackoverflow.com/questions/6936/using-what-ive-learned-from-stackoverflow-html-scraper/50772#50772">the script from this answer</a>. and I am having problems with unicode. Some of the questions end up being written poorly.</p> <p>Some answers and responses end up loo...
0
2009-04-08T02:32:18Z
732,387
<p>Does changing your default encoding in siteconfig.py work?</p> <p>In your site-packages file (on my OS X system it's in <code>/Library/Python/2.5/site-packages/</code>) create a file called <code>siteconfig.py</code>. In this file put:</p> <pre><code>import sys sys.setdefaultencoding('utf-8') </code></pre> <p>The...
0
2009-04-09T00:28:01Z
[ "python", "unicode", "beautifulsoup", "html-entities" ]
Dynamically creating a menu in Tkinter. (lambda expressions?)
728,356
<p>I have a menubutton, which when clicked should display a menu containing a specific sequence of strings. Exactly what strings are in that sequence, we do not know until runtime, so the menu that pops up must be generated at that moment. Here's what I have:</p> <pre><code>class para_frame(Frame): def __init__(se...
4
2009-04-08T02:59:40Z
728,411
<p>That kind of thing is quite a common problem in Tkinter, I think.</p> <p>Try this (at the appropriate point):</p> <pre><code>def new_command(tag=tag): self.add_tag(tag) </code></pre>
1
2009-04-08T03:21:41Z
[ "python", "lambda", "copy", "tkinter" ]
Dynamically creating a menu in Tkinter. (lambda expressions?)
728,356
<p>I have a menubutton, which when clicked should display a menu containing a specific sequence of strings. Exactly what strings are in that sequence, we do not know until runtime, so the menu that pops up must be generated at that moment. Here's what I have:</p> <pre><code>class para_frame(Frame): def __init__(se...
4
2009-04-08T02:59:40Z
728,445
<p>First of all, your problem doesn't have anything to do with Tkinter; it's best if you reduce it down to a simple piece of code demonstrating your problem, so you can experiment with it more easily. Here's a simplified version of what you're doing that I experimented with. I'm substituting a dict in place of the menu...
4
2009-04-08T03:38:40Z
[ "python", "lambda", "copy", "tkinter" ]
Is there a way to overload += in python?
728,361
<p>I know about the <code>__add__</code> method to override plus, but when I use that to override +=, I end up with one of two problems:</p> <p>(1) if <code>__add__</code> mutates self, then </p> <pre><code>z = x + y </code></pre> <p>will mutate x when I don't really want x to be mutated there.</p> <p>(2) if <code>...
12
2009-04-08T03:01:52Z
728,376
<p>Yes. Just override the object's <code>__iadd__</code> method, which takes the same parameters as <code>add</code>. You can find more information <a href="http://docs.python.org/reference/datamodel.html#emulating-numeric-types">here</a>.</p>
36
2009-04-08T03:06:14Z
[ "python", "operator-overloading" ]
Python "round robin"
728,543
<p>Given multiple (x,y) ordered pairs, I want to compare distances between each one of them. So pretend I have a list of ordered pairs:</p> <pre><code>pairs = [a,b,c,d,e,f] </code></pre> <p>I have a function that takes two ordered pairs and find the distance between them:</p> <pre><code>def distance(a,b): from m...
4
2009-04-08T04:38:53Z
728,560
<pre><code>try: from itertools import combinations except ImportError: def combinations(l, n): if n != 2: raise Exception('This placeholder only good for n=2') for i in range(len(l)): for j in range(i+1, len(l)): yield l[i], l[j] coords_list = [(0,0), (3,4), (6,8...
10
2009-04-08T04:47:59Z
[ "python", "iteration", "round-robin" ]
Python "round robin"
728,543
<p>Given multiple (x,y) ordered pairs, I want to compare distances between each one of them. So pretend I have a list of ordered pairs:</p> <pre><code>pairs = [a,b,c,d,e,f] </code></pre> <p>I have a function that takes two ordered pairs and find the distance between them:</p> <pre><code>def distance(a,b): from m...
4
2009-04-08T04:38:53Z
728,562
<p>If you don't mind doing distance calculations between two points that are the same twice, the following will find the greatest distance:</p> <pre><code>max( [distance(a, b) for a in pairs for b in pairs] ) </code></pre> <p>In order to have the a and b pair instead, then do the following:</p> <pre><code>import ope...
3
2009-04-08T04:48:20Z
[ "python", "iteration", "round-robin" ]
Python "round robin"
728,543
<p>Given multiple (x,y) ordered pairs, I want to compare distances between each one of them. So pretend I have a list of ordered pairs:</p> <pre><code>pairs = [a,b,c,d,e,f] </code></pre> <p>I have a function that takes two ordered pairs and find the distance between them:</p> <pre><code>def distance(a,b): from m...
4
2009-04-08T04:38:53Z
728,567
<p>in python 2.6, you can use itertools.permutations</p> <pre><code>import itertools perms = itertools.permutations(pairs, 2) distances = (distance(*p) for p in perms) </code></pre> <p>or</p> <pre><code>import itertools combs = itertools.combinations(pairs, 2) distances = (distance(*c) for c in combs) </code></pre>
17
2009-04-08T04:51:52Z
[ "python", "iteration", "round-robin" ]
Python "round robin"
728,543
<p>Given multiple (x,y) ordered pairs, I want to compare distances between each one of them. So pretend I have a list of ordered pairs:</p> <pre><code>pairs = [a,b,c,d,e,f] </code></pre> <p>I have a function that takes two ordered pairs and find the distance between them:</p> <pre><code>def distance(a,b): from m...
4
2009-04-08T04:38:53Z
728,570
<p>Try:</p> <pre><code>max(distance(a, b) for (i, a) in enumerate(pairs) for b in pairs[i+1:]) </code></pre> <p>This avoid identity-comparisons (e.g. <code>distance(x, x)</code>, <code>distance(y, y)</code>, etc.). It also avoids doing symmetric comparisons, since <code>distance(x, y) == distance(y, x)</code>.</p> <...
6
2009-04-08T04:54:37Z
[ "python", "iteration", "round-robin" ]
Python "round robin"
728,543
<p>Given multiple (x,y) ordered pairs, I want to compare distances between each one of them. So pretend I have a list of ordered pairs:</p> <pre><code>pairs = [a,b,c,d,e,f] </code></pre> <p>I have a function that takes two ordered pairs and find the distance between them:</p> <pre><code>def distance(a,b): from m...
4
2009-04-08T04:38:53Z
730,987
<p>slightly related, you don't have to compute the euclidean distance yourself, there's math.hypot:</p> <pre><code>In [1]: a = (1, 2) In [2]: b = (4, 5) In [3]: hypot(a[0]-b[0], a[1]-b[1]) Out[3]: 4.2426406871192848 </code></pre>
3
2009-04-08T17:26:06Z
[ "python", "iteration", "round-robin" ]
Example of how to use msilib to create a .msi file from a python module
728,589
<p>Can anyone give me an example of how to use python's <a href="http://docs.python.org/library/msilib.html" rel="nofollow">msilib</a> standard library module to create a msi file from a custom python module?</p> <p>For example, let's say I have a custom module called cool.py with the following code</p> <pre><code>cl...
2
2009-04-08T05:09:12Z
728,647
<p>I think there is a misunderstanding: think of MS CAB Files as archives like <code>.zip</code>-Files. Now it is possible to put anything in such an archive, like your <code>cool.py</code>. But i think you mentioned that python source, since you want it executed, otherwise just use an archiver like zip, no need to use...
0
2009-04-08T05:42:45Z
[ "python", "windows", "windows-installer" ]
Example of how to use msilib to create a .msi file from a python module
728,589
<p>Can anyone give me an example of how to use python's <a href="http://docs.python.org/library/msilib.html" rel="nofollow">msilib</a> standard library module to create a msi file from a custom python module?</p> <p>For example, let's say I have a custom module called cool.py with the following code</p> <pre><code>cl...
2
2009-04-08T05:09:12Z
728,719
<p>You need to write a distutils setup script for your module, then you can do</p> <pre><code>python setup.py bdist_msi </code></pre> <p>and an msi-installer will be created for your module.</p> <p>See also <a href="http://docs.python.org/distutils/apiref.html#module-distutils.command.bdist%5Fmsi">http://docs.python...
5
2009-04-08T06:12:10Z
[ "python", "windows", "windows-installer" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
728,916
<p>I suspect it is similar to Ruby - either method is okay.</p> <p>This is largely because different text editors use different methods (ie, these two) of marking encoding.</p> <p>With Ruby, as long as the first, or second if there is a shebang line contains a string that matches:</p> <pre><code>coding: encoding-nam...
0
2009-04-08T07:43:34Z
[ "python", "encoding" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
728,926
<p>If I'm not mistaken, the original proposal for source file encodings was to use a regular expression for the first couple of lines, which would allow both.</p> <p>I think the regex was something along the lines of <code>coding:</code> followed by something.</p> <p>I found this: <a href="http://www.python.org/dev/p...
2
2009-04-08T07:46:39Z
[ "python", "encoding" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
729,016
<p>Check the docs <a href="http://docs.python.org/reference/lexical_analysis.html#encoding-declarations">here</a>: </p> <p>"If a comment in the first or second line of the Python script matches the regular expression <code>coding[=:]\s*([-\w.]+)</code>, this comment is processed as an encoding declaration"</p> <p>"Th...
83
2009-04-08T08:20:41Z
[ "python", "encoding" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
729,017
<p>PEP 263:</p> <blockquote> <p>the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)"</p> </blockquote> <p>Obviously "en<b>coding: UTF-8</b>" matches. </p> <p>PEP provides some examples:</p> <pre><code> #!/usr/bin/python # vim: set fileencoding=&lt;encoding name&gt...
19
2009-04-08T08:21:12Z
[ "python", "encoding" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
36,163,461
<p>Just copy paste below statement on the top of your program.It will solve character encoding problems</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- </code></pre>
9
2016-03-22T19:28:47Z
[ "python", "encoding" ]
Correct way to define Python source code encoding
728,891
<p><a href="http://www.python.org/dev/peps/pep-0263/">PEP 263</a> defines how to define Python source code encoding.</p> <p>Normally, the first 2 lines of a Python file should start with:</p> <pre><code>#!/usr/bin/python # -*- coding: &lt;encoding name&gt; -*- </code></pre> <p>But I have seen a lot of files starting...
66
2009-04-08T07:35:05Z
38,979,793
<p>I just had same problem and solved it by adding this to top of my python script : </p> <pre><code># coding=utf-8 </code></pre>
-1
2016-08-16T16:14:58Z
[ "python", "encoding" ]
Python script - SCP on windows
729,130
<p>How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to ...
0
2009-04-08T08:56:38Z
729,144
<p>How do you expect to provide the authentication data? The easiest way is to create a key, and make sure it is in the server's list of accepted hosts. That way scp will authenticate using the private/public key pair automatically, and "just work".</p> <p><a href="http://www.lesbell.com.au/Home.nsf/b8ec57204f60dfcb4a...
0
2009-04-08T09:00:39Z
[ "python", "windows", "copy" ]
Python script - SCP on windows
729,130
<p>How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to ...
0
2009-04-08T08:56:38Z
729,190
<p><a href="http://pypi.python.org/pypi/ssh4py" rel="nofollow">http://pypi.python.org/pypi/ssh4py</a></p> <p>SCP example: <a href="http://blog.keyphrene.com/keyphrene/index.php/2008/09/18/13-scp" rel="nofollow">http://blog.keyphrene.com/keyphrene/index.php/2008/09/18/13-scp</a></p>
1
2009-04-08T09:13:12Z
[ "python", "windows", "copy" ]
Python script - SCP on windows
729,130
<p>How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to ...
0
2009-04-08T08:56:38Z
729,871
<p><a href="http://www.lag.net/paramiko/" rel="nofollow">paramiko</a> is pretty slick. See <a href="http://stackoverflow.com/questions/250283/how-to-scp-in-python">this question</a> for some more details.</p>
2
2009-04-08T13:00:19Z
[ "python", "windows", "copy" ]
Python script - SCP on windows
729,130
<p>How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to ...
0
2009-04-08T08:56:38Z
730,060
<p><a href="http://twistedmatrix.com/trac/wiki/TwistedConch" rel="nofollow">Twisted Conch</a> supports ssh and sftp.</p>
1
2009-04-08T13:39:42Z
[ "python", "windows", "copy" ]
Python script - SCP on windows
729,130
<p>How is it possible to do secure copy using python (windows native install - ActivePython). Unfortunately pexpect module is for unix only and we don't want cygwin locally. I wrote a script that based on pscp.exe win tool - but always stops at first execution becuse of fingerprint host id. and haven't found option to ...
0
2009-04-08T08:56:38Z
731,122
<p>I strongly recommend that you use keys rather than passwords. If you use ssh keys properly, you do not need to use expect, as the scp command won't ask for any user input. If you have command line ssh installed, you can make a key like this: </p> <pre><code>ssh-keygen -t dsa </code></pre> <p>Then simply follow t...
2
2009-04-08T18:02:33Z
[ "python", "windows", "copy" ]
Python's eval() and globals()
729,248
<p>I'm trying to execute a number of functions using eval(), and I need to create some kind of environment for them to run. It is said in documentation that you can pass globals as a second parameter to eval().</p> <p>But it seems to not work in my case. Here's the simpified example (I tried two approaches, declaring ...
6
2009-04-08T09:30:31Z
729,251
<p>*test_variable* should be global in test.py. You're getting a name error because you're trying to declare a variable global that doesn't yet exist.</p> <p>So your my_test.py file should be like this:</p> <pre><code>test_variable = None def my_func(): print test_variable </code></pre> <p>And running this fro...
9
2009-04-08T09:32:51Z
[ "python", "eval" ]
Python's eval() and globals()
729,248
<p>I'm trying to execute a number of functions using eval(), and I need to create some kind of environment for them to run. It is said in documentation that you can pass globals as a second parameter to eval().</p> <p>But it seems to not work in my case. Here's the simpified example (I tried two approaches, declaring ...
6
2009-04-08T09:30:31Z
729,420
<p><em>Please correct me Python experts if I am wrong. I am also learning Python. The following is my current understanding of why the <code>NameError</code> exception was thrown.</em></p> <p>In Python, you cannot create a variable that can be access across modules without specifying the module name (i.e. to access th...
4
2009-04-08T10:44:36Z
[ "python", "eval" ]
Python's eval() and globals()
729,248
<p>I'm trying to execute a number of functions using eval(), and I need to create some kind of environment for them to run. It is said in documentation that you can pass globals as a second parameter to eval().</p> <p>But it seems to not work in my case. Here's the simpified example (I tried two approaches, declaring ...
6
2009-04-08T09:30:31Z
6,784,820
<p>PyMathParser does what you want. It's a free download. You can look at the source code to learn how it is done.</p>
0
2011-07-22T01:37:44Z
[ "python", "eval" ]
Identifying a map in groovy
729,354
<p>While porting over a code fragment from python I've stumbled over a trivial problem:</p> <pre><code>if isinstance(v['content'], dict): </code></pre> <p>What would be the most elegant way to port this over to groovy?</p>
3
2009-04-08T10:16:07Z
729,586
<p>You can use <code>instanceof</code> (see map-specific example <a href="http://groovy.codehaus.org/Collections" rel="nofollow">here</a>), like this:</p> <pre><code>if (v['content'] instanceof java.util.map) </code></pre>
5
2009-04-08T11:30:36Z
[ "python", "groovy" ]
Getting file path of imported module
729,583
<p>How can I get the file path of a module imported in python. I am using Linux (if it matters). </p> <p>Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.</p>
28
2009-04-08T11:30:21Z
729,602
<p>This will give you the directory the module is in:</p> <pre><code>import foo os.path.dirname(foo.__file__) </code></pre>
5
2009-04-08T11:37:31Z
[ "python" ]
Getting file path of imported module
729,583
<p>How can I get the file path of a module imported in python. I am using Linux (if it matters). </p> <p>Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.</p>
28
2009-04-08T11:30:21Z
729,605
<p>Modules and packages have a <code>__file__</code> attribute that has its path information. If the module was imported relative to current working directory, you'll probably want to get its absolute path.</p> <pre><code>import os.path import my_module print os.path.abspath(my_module.__file__) </code></pre>
33
2009-04-08T11:38:35Z
[ "python" ]
Getting file path of imported module
729,583
<p>How can I get the file path of a module imported in python. I am using Linux (if it matters). </p> <p>Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.</p>
28
2009-04-08T11:30:21Z
729,607
<p>I've been using this:</p> <pre><code>import inspect import os class DummyClass: pass print os.path.dirname(os.path.abspath(inspect.getsourcefile(DummyClass)) </code></pre> <p>(Edit: This is a "where am I" function - it returns the directory containing the current module. I'm not quite sure if that's what you want)...
8
2009-04-08T11:40:21Z
[ "python" ]
Getting file path of imported module
729,583
<p>How can I get the file path of a module imported in python. I am using Linux (if it matters). </p> <p>Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.</p>
28
2009-04-08T11:30:21Z
1,205,775
<p>To find the load path of modules already loaded: </p> <pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; sys.modules['os'] &lt;module 'os' from 'c:\Python26\lib\os.pyc'&gt; </code></pre>
4
2009-07-30T10:36:07Z
[ "python" ]
Getting file path of imported module
729,583
<p>How can I get the file path of a module imported in python. I am using Linux (if it matters). </p> <p>Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.</p>
28
2009-04-08T11:30:21Z
36,689,047
<p>I have been using this method, which applies to both non-built-in and built-in modules:</p> <pre><code>def showModulePath(module): if (hasattr(module, '__name__') is False): print 'Error: ' + str(module) + ' is not a module object.' return None moduleName = module.__name__ ...
0
2016-04-18T08:35:18Z
[ "python" ]
How to I get scons to invoke an external script?
729,759
<p>I'm trying to use scons to build a latex document. In particular, I want to get scons to invoke a python program that generates a file containing a table that is \input{} into the main document. I've looked over the scons documentation but it is not immediately clear to me what I need to do.</p> <p>What I wish to a...
16
2009-04-08T12:36:53Z
729,940
<p>In this simple case, the easiest way is to just use the subprocess module</p> <pre><code>from subprocess import call call("python table_generator.py") call("pdflatex document.tex") </code></pre> <p>Regardless of where in your SConstruct file these lines are placed, they will happen before any of the compiling and ...
3
2009-04-08T13:14:31Z
[ "python", "latex", "scons", "tex" ]
How to I get scons to invoke an external script?
729,759
<p>I'm trying to use scons to build a latex document. In particular, I want to get scons to invoke a python program that generates a file containing a table that is \input{} into the main document. I've looked over the scons documentation but it is not immediately clear to me what I need to do.</p> <p>What I wish to a...
16
2009-04-08T12:36:53Z
730,303
<p>Something along these lines should do -</p> <pre><code>env.Command ('document.tex', '', 'python table_generator.py') env.PDF ('document.pdf', 'document.tex') </code></pre> <p>It declares that 'document.tex' is generated by calling the Python script, and requests a PDF document to be created from this generatd 'doc...
15
2009-04-08T14:33:08Z
[ "python", "latex", "scons", "tex" ]
Seting up Python on IIS 5.1
730,105
<p>I have this test python file</p> <pre><code>import os print 'Content-type: text/html' print print '&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Python Sample CGI&lt;/TITLE&gt;&lt;/HEAD&gt;' print '&lt;BODY&gt;' print "&lt;H1&gt;This is A Sample Python CGI Script&lt;/H1&gt;" print '&lt;br&gt;' if os.environ.has_key('REM...
1
2009-04-08T13:50:10Z
730,432
<pre><code>C:\Python30\python.exe -u "%" "%" </code></pre> <p>Close, but it should be "%s". I use:</p> <pre><code>"C:\Python30\python.exe" -u "%s" </code></pre> <p>(The second %s is for command-line &lt;isindex> queries, which will never happen in this century.)</p>
2
2009-04-08T15:01:21Z
[ "python", "iis", "cgi", "iis-5" ]
Django Model set foreign key to a field of another Model
730,207
<p>Is there any way to set a foreign key in django to a <em>field</em> of another model?</p> <p>For example, imagine I have a ValidationRule object. And I want the rule to define what field in another model is to be validated (as well as some other information, such as whether it can be null, a data-type, range, etc....
13
2009-04-08T14:12:43Z
730,235
<p>Yes and no. The FK relationship is described at the class level, and mirrors the FK association in the database, so you can't add extra information directly in the FK parameter.</p> <p>Instead, I'd recommend having a string that holds the field name on the other table:</p> <pre><code>class ValidationRule(models.Mo...
12
2009-04-08T14:18:41Z
[ "python", "django", "django-models" ]
Django Model set foreign key to a field of another Model
730,207
<p>Is there any way to set a foreign key in django to a <em>field</em> of another model?</p> <p>For example, imagine I have a ValidationRule object. And I want the rule to define what field in another model is to be validated (as well as some other information, such as whether it can be null, a data-type, range, etc....
13
2009-04-08T14:12:43Z
2,000,016
<p>I haven't tried this, but it seems that since Django 1.0 you can do something like:</p> <pre><code>class Foo(models.Model): foo = models.ForeignKey(Bar, to_field='bar') </code></pre> <p>Documentation for this is <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.to_fie...
41
2010-01-04T14:56:09Z
[ "python", "django", "django-models" ]
Replace html entities with the corresponding utf-8 characters in Python 2.6
730,299
<p>I have a html text like this:</p> <pre><code>&amp;lt;xml ... &amp;gt; </code></pre> <p>and I want to convert it to something readable:</p> <pre><code>&lt;xml ...&gt; </code></pre> <p>Any easy (and fast) way to do it in Python?</p>
13
2009-04-08T14:32:42Z
730,330
<p><a href="http://docs.python.org/library/htmlparser.html">http://docs.python.org/library/htmlparser.html</a></p> <pre><code>&gt;&gt;&gt; import HTMLParser &gt;&gt;&gt; pars = HTMLParser.HTMLParser() &gt;&gt;&gt; pars.unescape('&amp;copy; &amp;euro;') u'\xa9 \u20ac' &gt;&gt;&gt; print _ © € </code></pre>
21
2009-04-08T14:36:29Z
[ "python", "html-entities", "python-2.6" ]
Replace html entities with the corresponding utf-8 characters in Python 2.6
730,299
<p>I have a html text like this:</p> <pre><code>&amp;lt;xml ... &amp;gt; </code></pre> <p>and I want to convert it to something readable:</p> <pre><code>&lt;xml ...&gt; </code></pre> <p>Any easy (and fast) way to do it in Python?</p>
13
2009-04-08T14:32:42Z
731,150
<p>There is a function <a href="http://effbot.org/zone/re-sub.htm#unescape-html" rel="nofollow">here</a> that does it, as linked from the post Fred pointed out. Copied here to make things easier. </p> <p>Credit to Fred Larson for linking to the other question on SO. Credit to dF for posting the link. </p>
1
2009-04-08T18:09:06Z
[ "python", "html-entities", "python-2.6" ]
mercurial + OSX == fail? hg log abort: Is a directory
730,319
<pre><code>$ mkdir foo $ cd foo $ hg init . $ hg log abort: Is a directory $ hg history abort: Is a directory </code></pre> <p>Darwin Host.local 9.6.1 Darwin Kernel Version 9.6.1: Wed Dec 10 10:38:33 PST 2008; root:xnu-1228.9.75~3/RELEASE_I386 i386</p> <pre><code>$ hg --version Mercurial Distributed SCM (version 1.2....
2
2009-04-08T14:35:13Z
731,397
<p>Not a direct answer to your question, but I've successfully been using the Mercurial pre-packaged binaries from <a href="http://mercurial.berkwood.com/" rel="nofollow">here</a> with the standard Python 2.5.1 install on OSX 10.5 without issue.</p> <pre><code>$ mkdir foo $ cd foo $ hg init . $ hg log $ hg history $ ...
1
2009-04-08T19:05:04Z
[ "python", "osx", "mercurial" ]
mercurial + OSX == fail? hg log abort: Is a directory
730,319
<pre><code>$ mkdir foo $ cd foo $ hg init . $ hg log abort: Is a directory $ hg history abort: Is a directory </code></pre> <p>Darwin Host.local 9.6.1 Darwin Kernel Version 9.6.1: Wed Dec 10 10:38:33 PST 2008; root:xnu-1228.9.75~3/RELEASE_I386 i386</p> <pre><code>$ hg --version Mercurial Distributed SCM (version 1.2....
2
2009-04-08T14:35:13Z
736,079
<p>From <a href="http://www.selenic.com/mercurial/bts/issue233" rel="nofollow">http://www.selenic.com/mercurial/bts/issue233</a>, there is an interesting traceback:</p> <pre><code>hg --traceback qpop Traceback (most recent call last): [...] File "/export/home/bos/lib/python/mercurial/util.py", line 747, in o r...
1
2009-04-09T22:11:37Z
[ "python", "osx", "mercurial" ]
mercurial + OSX == fail? hg log abort: Is a directory
730,319
<pre><code>$ mkdir foo $ cd foo $ hg init . $ hg log abort: Is a directory $ hg history abort: Is a directory </code></pre> <p>Darwin Host.local 9.6.1 Darwin Kernel Version 9.6.1: Wed Dec 10 10:38:33 PST 2008; root:xnu-1228.9.75~3/RELEASE_I386 i386</p> <pre><code>$ hg --version Mercurial Distributed SCM (version 1.2....
2
2009-04-08T14:35:13Z
1,025,678
<p>I found the problem: </p> <p>If you symlink your .hgrc somewhere it causes this. Definitely a bug in mercurial (and a stupid error message at that).</p> <pre><code>$ rm -f ~/.hgrc $ hg init foo $ cd foo $ hg log </code></pre> <p>(works)</p> <pre><code>$ ls -l ~/Dropbox/.hgrc -rw-r--r--@ 1 schubert staff 83 Jan...
0
2009-06-22T06:12:37Z
[ "python", "osx", "mercurial" ]
mercurial + OSX == fail? hg log abort: Is a directory
730,319
<pre><code>$ mkdir foo $ cd foo $ hg init . $ hg log abort: Is a directory $ hg history abort: Is a directory </code></pre> <p>Darwin Host.local 9.6.1 Darwin Kernel Version 9.6.1: Wed Dec 10 10:38:33 PST 2008; root:xnu-1228.9.75~3/RELEASE_I386 i386</p> <pre><code>$ hg --version Mercurial Distributed SCM (version 1.2....
2
2009-04-08T14:35:13Z
1,025,710
<p>I took another look and it is actually caused by the .hgrc line:</p> <p>style = gitweb</p> <p>Why that is I'm not sure but the error message sure sucks still.</p>
0
2009-06-22T06:26:16Z
[ "python", "osx", "mercurial" ]
wxPython: Making a fixed-height panel
730,394
<p>I have a wx.Frame, in which I have a vertical BoxSizer with two items, a TextCtrl and a custom widget. I want the custom widget to have a fixed pixel height, while the TextCtrl will expand normally to fill the window. What should I do?</p>
2
2009-04-08T14:53:31Z
730,452
<p>Got it.</p> <p>When creating the widget, use a size of <code>(-1,100)</code>, where "<code>100</code>" is the height you want. Apparently the "<code>-1</code>" is a sort of "None" in this context.</p> <p>When adding the widget to the sizer, use a proportion of 0, like this: <code>self.sizer.Add(self.timeline,0,wx....
6
2009-04-08T15:06:48Z
[ "python", "layout", "wxpython", "widget" ]
django to send AND receive email?
730,573
<p>I have gotten quite familiar with django's email sending abilities, but I havn't seen anything about it receiving and processing emails from users. Is this functionality available? </p> <p>A few google searches have not turned up very promising results. Though I did find this: <a href="http://stackoverflow.com/ques...
21
2009-04-08T15:38:53Z
730,769
<p>Django is really intended as a web server (well, as a framework that fits into a web server), not as an email server. I suppose you could put some code into a Django web application that starts up an email server, using the kind of code shown in that question you linked to, but I really wouldn't recommend it; it's a...
2
2009-04-08T16:23:38Z
[ "python", "django", "email", "pop3", "django-email" ]
django to send AND receive email?
730,573
<p>I have gotten quite familiar with django's email sending abilities, but I havn't seen anything about it receiving and processing emails from users. Is this functionality available? </p> <p>A few google searches have not turned up very promising results. Though I did find this: <a href="http://stackoverflow.com/ques...
21
2009-04-08T15:38:53Z
731,515
<p>There's an app called <a href="http://code.google.com/p/jutda-helpdesk/">jutda-helpdesk</a> that uses Python's <code>poplib</code> and <code>imaplib</code> to process incoming emails. You just have to have an account somewhere with POP3 or IMAP access.</p> <p>This is adapted from their <a href="http://code.google....
17
2009-04-08T19:43:37Z
[ "python", "django", "email", "pop3", "django-email" ]
django to send AND receive email?
730,573
<p>I have gotten quite familiar with django's email sending abilities, but I havn't seen anything about it receiving and processing emails from users. Is this functionality available? </p> <p>A few google searches have not turned up very promising results. Though I did find this: <a href="http://stackoverflow.com/ques...
21
2009-04-08T15:38:53Z
5,328,135
<p>I know this question is pretty old now but just thought I'd add for future reference that you might want to give <a href="http://cloudmailin.com">http://cloudmailin.com</a> a go. We have quite a few django users using the system and it should be a little simpler than the proposed solution.</p>
5
2011-03-16T16:07:24Z
[ "python", "django", "email", "pop3", "django-email" ]
Python/wxPython: Doing work continuously in the background
730,645
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
12
2009-04-08T15:55:06Z
730,663
<p>Launch a new process to render in background and periodically check to see if it has returned.</p> <p>You can find the documentation for the subprocess module <a href="http://docs.python.org/library/subprocess.html#module-subprocess" rel="nofollow">here</a> and the multiprocess module <a href="http://docs.python.or...
4
2009-04-08T15:58:29Z
[ "python", "multithreading", "wxpython", "background" ]
Python/wxPython: Doing work continuously in the background
730,645
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
12
2009-04-08T15:55:06Z
746,144
<p>I would use a <code>threading.Thread</code> to run the code in the background and <code>wx.CallAfter</code> to post updates to my window thread to render them to the user.</p> <pre><code>thread = threading.Thread(target=self.do_work) thread.setDaemon(True) thread.start() ... def do_work(self): # processing co...
7
2009-04-14T02:36:37Z
[ "python", "multithreading", "wxpython", "background" ]
Python/wxPython: Doing work continuously in the background
730,645
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
12
2009-04-08T15:55:06Z
746,156
<p>If you don't mind using a very slightly different approach, you can have a look at <a href="http://www.stackless.com/" rel="nofollow">stackless</a> python and create a tasklet for your rendering process. I find it very easy to use personally.</p>
0
2009-04-14T02:42:19Z
[ "python", "multithreading", "wxpython", "background" ]
Python/wxPython: Doing work continuously in the background
730,645
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
12
2009-04-08T15:55:06Z
755,846
<p>There's a fair bit of info on the <a href="http://wiki.wxpython.org/LongRunningTasks">wxPython wiki about long running tasks</a> that might be useful. They basically make use a thread and <code>wx.PostEvent</code> to handle communication between the thread and the main wx event loop.</p>
7
2009-04-16T12:10:02Z
[ "python", "multithreading", "wxpython", "background" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
730,778
<pre><code>try: doSomething() except: pass </code></pre> <p>or</p> <pre><code>try: doSomething() except Exception: pass </code></pre> <p>The difference is, that the first one will also catch <code>KeyboardInterrupt</code>, <code>SystemExit</code> and stuff like that, which are derived directly from <code>e...
561
2009-04-08T16:25:10Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
730,806
<blockquote> <p>When you just want to do a try catch without handling the exception, how do you do it in Python?</p> </blockquote> <p>It depends on what you mean by "handling."</p> <p>If you mean to catch it without taking any action, the code you posted will work.</p> <p>If you mean that you want to take action o...
93
2009-04-08T16:32:32Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
736,029
<p>It's generally considered best-practice to only catch the errors you are interested in, in the case of <code>shutil.rmtree</code> it's probably <code>OSError</code>:</p> <pre><code>&gt;&gt;&gt; shutil.rmtree("/fake/dir") Traceback (most recent call last): [...] OSError: [Errno 2] No such file or directory: '/fa...
78
2009-04-09T21:48:05Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
1,767,077
<pre><code>try: doSomething() except Exception: pass else: stuffDoneIf() TryClauseSucceeds() </code></pre> <p>FYI the else clause can go after all exceptions and will only be run if the code in the try doesn't cause an exception. </p>
4
2009-11-19T22:35:58Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
2,390,566
<p>For completeness:</p> <pre><code>&gt;&gt;&gt; def divide(x, y): ... try: ... result = x / y ... except ZeroDivisionError: ... print "division by zero!" ... else: ... print "result is", result ... finally: ... print "executing finally clause" </code></pre> <p>...from ...
39
2010-03-05T23:02:39Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
4,213,786
<p>in python, we handle exceptions similar to other language but the difference is some syntex difference, for example-</p> <pre><code>try: #Your Code in which exception can occur except &lt;here we can put particular exception name&gt;: #we can call that exception here also, like ZeroDivisionError() #now ...
2
2010-11-18T10:27:56Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
7,735,712
<p>@When you just want to do a try catch without handling the exception, how do you do it in Python?</p> <p>This will help you to print what exception is:( i.e. try catch without handling the exception and print the exception.)</p> <pre> import sys .... try: doSomething() except: print "Unexpected error:", sy...
6
2011-10-12T05:36:30Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
18,677,839
<p>First I quote the answer of Jack o'Connor from <a href="http://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed">this thread</a>. The referenced thread got closed so I write here:</p> <p>"There's a new way to do this coming in Python 3.4:</p> <pre><code>from contextlib import suppre...
34
2013-09-07T20:55:51Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
28,081,414
<blockquote> <h1>How to properly ignore Exceptions?</h1> </blockquote> <p>There are now several ways of doing this.</p> <h2>New in Python 3.4:</h2> <p>You can import the <code>suppress</code> context manager:</p> <pre><code>from contextlib import suppress </code></pre> <p>But only suppress the most specific exce...
17
2015-01-22T04:42:53Z
[ "python", "exception", "exception-handling", "try-except" ]
Try/Except in Python: How do you properly ignore Exceptions?
730,764
<p>When you just want to do a try-except without handling the exception, how do you do it in Python?</p> <p>Is the following the right way to do it?</p> <pre><code>try : shutil.rmtree ( path ) except : pass </code></pre>
393
2009-04-08T16:23:00Z
38,735,158
<p><a href="http://www.cpphub.com/2016/08/exceptions-handling-in-python-with.html#more" rel="nofollow">Handling an exception in python:</a> If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. `# your statements ............. except E...
0
2016-08-03T05:33:47Z
[ "python", "exception", "exception-handling", "try-except" ]
draw text with GLUT / OpenGL in Python
730,952
<p>Hey, I m drawing text with OpenGL in Python. It all works ok, however the font is really bad. If I make it thick the letters start to look very occurred (especially the ones which are round like 'o' or 'g'. For the purpose of my program it must be thick. Is there any font I could use which does not look so bad when...
1
2009-04-08T17:19:23Z
733,244
<p>Try a more sophisticated text rendering solution. Perhaps something like <a href="http://code.google.com/p/pyftgl/" rel="nofollow">pyftgl</a> would get you better results, by rendering full-quality TrueType fonts.</p>
1
2009-04-09T07:50:30Z
[ "python", "opengl", "text", "draw" ]
draw text with GLUT / OpenGL in Python
730,952
<p>Hey, I m drawing text with OpenGL in Python. It all works ok, however the font is really bad. If I make it thick the letters start to look very occurred (especially the ones which are round like 'o' or 'g'. For the purpose of my program it must be thick. Is there any font I could use which does not look so bad when...
1
2009-04-08T17:19:23Z
2,010,599
<p><a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=43" rel="nofollow">http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=43</a></p> <p>At the bottom is the python version of this lesson which shows you how to load freetype fonts with the Python Image Library (PIL) <a href="http://www.pythonware.com...
0
2010-01-06T02:24:17Z
[ "python", "opengl", "text", "draw" ]
How can I get useful information from flash swf files?
731,016
<p>I'm doing some crawling with Python, and would like to be able to identify (however imperfectly) the flash I come across - is it a video, an ad, a game, or whatever.</p> <p>I assume I would have to decompile the swf, which seems doable. But what sort of processing would I do with the decompiled Actionscript to figu...
1
2009-04-08T17:34:51Z
731,053
<p>I think your best bet would be to check the context where you see the swf file</p> <p>usually they're embedded within web pages so if that page has 100 occurences of the word "game", then it might be a game, as an example</p> <p>To detect an ad it might be trickier but i think that checking the domainname where th...
4
2009-04-08T17:48:08Z
[ "python", "actionscript", "flash" ]
How can I get useful information from flash swf files?
731,016
<p>I'm doing some crawling with Python, and would like to be able to identify (however imperfectly) the flash I come across - is it a video, an ad, a game, or whatever.</p> <p>I assume I would have to decompile the swf, which seems doable. But what sort of processing would I do with the decompiled Actionscript to figu...
1
2009-04-08T17:34:51Z
731,089
<p>It might help to look at the arguments passed to the Flash movie. If there's reference to an FLV file then there's a good chance the SWF is being used to play a movie.</p> <p>The path to the SWF might help too. If it's under, say an /ads directory then it's probably just a banner ad. Or if it's under /games then...
2
2009-04-08T17:55:12Z
[ "python", "actionscript", "flash" ]
How can I get useful information from flash swf files?
731,016
<p>I'm doing some crawling with Python, and would like to be able to identify (however imperfectly) the flash I come across - is it a video, an ad, a game, or whatever.</p> <p>I assume I would have to decompile the swf, which seems doable. But what sort of processing would I do with the decompiled Actionscript to figu...
1
2009-04-08T17:34:51Z
762,561
<p>Tough one. I guess you should try find a scope for a swf context. As you said, swfs can be: ads,games, video players, they can also contain experimental art. who knows. Once you know what exactly your after, it should be easier to figure out how to look for that kind of data.</p> <p>I think it would be easier to ge...
0
2009-04-17T23:29:30Z
[ "python", "actionscript", "flash" ]
Pygame Invalid Syntax I just can't figure out
731,057
<p>I've been following a tutorial "McGugan - Beginning Game Development with Python and Pygame (Apress, 2007)" and in the code at around chapter five involving object movement I keep getting invalid syntax alerts on '-' being used in the code. It isn't up to date but I would've thought a subtract wouldn't be changed in...
0
2009-04-08T17:49:15Z
731,083
<p>In this line:</p> <pre><code>destination = Vector2(*event.pos) – Vector2(*sprite.get_size())/2. </code></pre> <p>You somehow typed the character "<code>–</code>" (EN DASH) instead of "<code>-</code>" (HYPHEN-MINUS). Use "<code>-</code>" (HYPHEN-MINUS) instead, like this:</p> <pre><code>destination = Vector2(*...
5
2009-04-08T17:54:20Z
[ "python", "syntax", "pygame" ]
Pygame Invalid Syntax I just can't figure out
731,057
<p>I've been following a tutorial "McGugan - Beginning Game Development with Python and Pygame (Apress, 2007)" and in the code at around chapter five involving object movement I keep getting invalid syntax alerts on '-' being used in the code. It isn't up to date but I would've thought a subtract wouldn't be changed in...
0
2009-04-08T17:49:15Z
731,087
<p>I can't be sure without a stack trace, but I have a hunch that it's the wrong - symbol. What editor are you using? Is it possible that your editor is taking the - symbol and turning it into a fancier dash, like an ndash or an mdash? </p>
0
2009-04-08T17:55:01Z
[ "python", "syntax", "pygame" ]
Pygame Invalid Syntax I just can't figure out
731,057
<p>I've been following a tutorial "McGugan - Beginning Game Development with Python and Pygame (Apress, 2007)" and in the code at around chapter five involving object movement I keep getting invalid syntax alerts on '-' being used in the code. It isn't up to date but I would've thought a subtract wouldn't be changed in...
0
2009-04-08T17:49:15Z
731,090
<p>Maybe try changing speed to "speed = 250.0". I don't know if that dangling dot would throw python off.</p> <p>What is going on here, with your error message at least, is the Python parser is stumbling over something before your '-', which screws up its interpretation of '-'. So I recommend looking before the '-' fo...
0
2009-04-08T17:55:13Z
[ "python", "syntax", "pygame" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,476
<p>JavaScript is the primary way to add this sort of interactivity to a website. You can make the back-end Python, but the client will have to use JavaScript AJAX calls to update the page. Python doesn't run in the browser, so you're out of luck if you want to use just Python.</p> <p>(It's also possible to use Flash...
1
2009-04-08T19:33:21Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,477
<p>You need Javascript in one way or another for your 30 second refresh. Alternatively, you could set a meta tag refresh for every 30 seconds to redirect to the current page, but the Javascript route will prevent page flicker.</p>
1
2009-04-08T19:33:24Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,588
<p>You could use <a href="http://en.wikipedia.org/wiki/Comet%5F%28programming%29" rel="nofollow">Comet</a>, but I strongly discourage you from doing so. I'd just write a short Javascript, using <a href="http://jquery.com" rel="nofollow">jQuery</a> this is really straightforward.</p> <p>Another possibility is the use o...
2
2009-04-08T20:02:13Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,618
<p>If you want to do it entirely in python you can use <a href="http://pyjs.org/" rel="nofollow">pyjamas</a>.</p> <p>It generates javascript directly from python code, so you avoid writing javascript yourself completely.</p>
2
2009-04-08T20:11:31Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,629
<p>Is this for a real webapp? Or is this a convenience thing for you to view output in the browser? If it's more so for convenience, you could consider using mod_python.</p> <p>mod_python is an extension for the apache webserver that embeds a python interpreter in the web server (so the script runs server side). It wo...
0
2009-04-08T20:13:48Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,652
<p>This question appears to have two things in it.</p> <ol> <li><p>Presentation on the web. This is easy to do in Python -- use Django or TurboGears or any Python-based web framework.</p></li> <li><p>Refresh of the web page to show new data. This can be done two ways.</p> <ul> <li><p>Some fancy Javascript to refres...
4
2009-04-08T20:20:40Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,702
<p>Write your output to a log file, and load the log file to the browser thru web server. If you need auto refresh, create a template HTML file with tag to refresh every 15 seconds:</p> <pre><code>&lt;META HTTP-EQUIV="refresh" CONTENT="15"&gt; </code></pre> <p>and use <a href="http://en.wikipedia.org/wiki/Server%5FSi...
1
2009-04-08T20:35:43Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,756
<p>If you want a dead simple way to print data from a Python script to a webpage and update automatically, you can just print from the script. For example, using Apache with the below Python CGI script: </p> <pre><code>#!/usr/bin/python import time import sys import random def write(inline=''): sys.stdout.write...
3
2009-04-08T20:46:33Z
[ "javascript", "python" ]
What's easiest way to get Python script output on the web?
731,470
<p>I have a python script that runs continuously. It outputs 2 lines of info every 30 seconds. I'd like to be able to view this output on the web. In particular, I'd like the site to auto-update (add the new output at the top of the page/site every 30 seconds without having to refresh the page).</p> <p>I understand...
5
2009-04-08T19:31:04Z
731,846
<p>Perhaps <a href="http://stackoverflow.com/questions/333664/simple-long-polling-example-code">"long polling"</a> is what you're looking for?</p> <p>Long polling could be described as "HTTP push", basically you have a (Python) script served via a web-server, which only outputs data when available.. Then you try and l...
1
2009-04-08T21:06:22Z
[ "javascript", "python" ]
Is there a way to get all the directories but not files in a directory in Python?
731,534
<p>This <a href="http://linux.die.net/diveintopython/html/file_handling/os_module.html" rel="nofollow">link</a> is using a custom method, but I just wanna see if there is a single method to do it in Python 2.6?</p>
1
2009-04-08T19:47:11Z
731,580
<p>If *a_directory* is the directory you want to inspect, then:</p> <p><code>next(f[1] for f in os.walk(a_directory))</code></p> <p>From the <a href="http://docs.python.org/library/os.html#os.walk" rel="nofollow">os.walk()</a> reference:</p> <blockquote> <p>Generate the file names in a directory tree by walking th...
3
2009-04-08T20:00:13Z
[ "python", "directory" ]
Is there a way to get all the directories but not files in a directory in Python?
731,534
<p>This <a href="http://linux.die.net/diveintopython/html/file_handling/os_module.html" rel="nofollow">link</a> is using a custom method, but I just wanna see if there is a single method to do it in Python 2.6?</p>
1
2009-04-08T19:47:11Z
731,594
<p>I don't believe there is. Since directories are also files, you have to ask for all the files, then ask each one if it is a directory.</p>
1
2009-04-08T20:04:18Z
[ "python", "directory" ]
Is there a way to get all the directories but not files in a directory in Python?
731,534
<p>This <a href="http://linux.die.net/diveintopython/html/file_handling/os_module.html" rel="nofollow">link</a> is using a custom method, but I just wanna see if there is a single method to do it in Python 2.6?</p>
1
2009-04-08T19:47:11Z
731,653
<p>There isn't a built-in function to only list files, but it's easy enough to define in a couple of lines:</p> <pre><code>def listfiles(directory): return [f for f in os.listdir(directory) if os.path.isdir(os.path.join(directory, f))] </code></pre> <p><strong>EDIT</strong>: fixed, thanks Stephan20...
5
2009-04-08T20:20:44Z
[ "python", "directory" ]
Is there a way to get all the directories but not files in a directory in Python?
731,534
<p>This <a href="http://linux.die.net/diveintopython/html/file_handling/os_module.html" rel="nofollow">link</a> is using a custom method, but I just wanna see if there is a single method to do it in Python 2.6?</p>
1
2009-04-08T19:47:11Z
731,714
<pre><code>def listdirs(path): ret = [] for cur_name in os.listdir(path): full_path = os.path.join(path, cur_name) if os.path.isdir(full_path): ret.append(cur_name) return ret onlydirs = listdir("/tmp/") print onlydirs </code></pre> <p>..or as a list-comprehension..</p> <pre><...
0
2009-04-08T20:37:21Z
[ "python", "directory" ]
Showing data in a GUI where the data comes from an outside source
731,759
<p>I'm kind of lost on how to approach this problem, I'd like to write a GUI ideally using Tkinter with python, but I initially started with Qt and found that the problem extends either with all GUI frameworks or my limited understanding.</p> <p>The data in this case is coming from a named pipe, and I'd like to displa...
0
2009-04-08T20:46:50Z
731,919
<p>When I did something like this I used a separate thread listening on the pipe. The thread had a pointer/handle back to the GUI so it could send the data to be displayed.</p> <p>I suppose you could do it in the GUI's update/event loop, but you'd have to make sure it's doing non-blocking reads on the pipe. I did it i...
0
2009-04-08T21:25:20Z
[ "python", "user-interface", "named-pipes" ]
Showing data in a GUI where the data comes from an outside source
731,759
<p>I'm kind of lost on how to approach this problem, I'd like to write a GUI ideally using Tkinter with python, but I initially started with Qt and found that the problem extends either with all GUI frameworks or my limited understanding.</p> <p>The data in this case is coming from a named pipe, and I'd like to displa...
0
2009-04-08T20:46:50Z
731,927
<p>In the past when I've had GUI's reading data off of external things (eg: ethernet sockets), I've had a separate thread that handles servicing the external thing, and a timed callback (generally set to something like half a second) to update the GUI widget that displays the external data.</p>
0
2009-04-08T21:27:51Z
[ "python", "user-interface", "named-pipes" ]
Showing data in a GUI where the data comes from an outside source
731,759
<p>I'm kind of lost on how to approach this problem, I'd like to write a GUI ideally using Tkinter with python, but I initially started with Qt and found that the problem extends either with all GUI frameworks or my limited understanding.</p> <p>The data in this case is coming from a named pipe, and I'd like to displa...
0
2009-04-08T20:46:50Z
732,117
<p>Here is the way I would do it (on windows):</p> <pre><code>import wx, wx.lib.newevent, threading import win32event, win32pipe, win32file, pywintypes, winerror NewMessage, EVT_NEW_MESSAGE = wx.lib.newevent.NewEvent() class MessageNotifier(threading.Thread): pipe_name = r"\\.\pipe\named_pipe_demo" def __in...
0
2009-04-08T22:21:50Z
[ "python", "user-interface", "named-pipes" ]
Resetting the main GUI window
731,887
<p>I just want the equivalent of closing and reopening my main program. I want to invoke it when a "new"-like option from a drop-down menu is clicked on. Something like calling root.destroy() and then re-initiating the mainloop.</p> <p>How can I get this done?</p>
2
2009-04-08T21:14:42Z
732,085
<p>If you are on Unix, restart the entire application with os.execv. Make sure you pass all command line arguments etc.</p>
2
2009-04-08T22:09:52Z
[ "python", "tkinter" ]
Resetting the main GUI window
731,887
<p>I just want the equivalent of closing and reopening my main program. I want to invoke it when a "new"-like option from a drop-down menu is clicked on. Something like calling root.destroy() and then re-initiating the mainloop.</p> <p>How can I get this done?</p>
2
2009-04-08T21:14:42Z
732,131
<p>You could take all your GUI building logic and initial state code out of the mainloop and put it into functions. Call these functions from the mainloop (something like: buildgui() &amp; initstate()) and then, when the user clicks your menu icon, just call initstate() to set it back like it was when the application f...
2
2009-04-08T22:25:23Z
[ "python", "tkinter" ]
Resetting the main GUI window
731,887
<p>I just want the equivalent of closing and reopening my main program. I want to invoke it when a "new"-like option from a drop-down menu is clicked on. Something like calling root.destroy() and then re-initiating the mainloop.</p> <p>How can I get this done?</p>
2
2009-04-08T21:14:42Z
732,529
<p>There are at least three ways you can solve this. </p> <p>Method one: <strong>the head fake</strong>. When you create your app, don't put all the widgets in the root window. Instead, hide the root window and create a new toplevel that represents your application. When you restart it's just a matter of destroying th...
1
2009-04-09T02:08:10Z
[ "python", "tkinter" ]
Multiprocessing or Multithreading?
731,993
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
24
2009-04-08T21:44:44Z
732,026
<p>I always prefer multiple threads for simplicity, but there is a real issue with affinity. There is no way (that I know of) to tell Python's threading implementation to bind to a specific processor. This may not be an issue for you, it doesnt sound like it should be. Unless you have a good reason not to, it sounds li...
2
2009-04-08T21:52:44Z
[ "python", "multithreading", "multiprocessing", "multicore", "python-stackless" ]
Multiprocessing or Multithreading?
731,993
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
24
2009-04-08T21:44:44Z
732,116
<p><strong>"I checked it out and it looks good, but I also heard that processes, unlike threads, can't share a lot of information..."</strong></p> <p>This is only partially true.</p> <p>Threads are part of a process -- threads share memory trivially. Which is as much of a problem as a help -- two threads with casual...
18
2009-04-08T22:21:29Z
[ "python", "multithreading", "multiprocessing", "multicore", "python-stackless" ]
Multiprocessing or Multithreading?
731,993
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
24
2009-04-08T21:44:44Z
732,119
<ul> <li><strong>Stackless</strong>: uses 1 cpu. "Tasklets" must yield voluntarily. The preemption option doesn't work all the time.</li> <li><strong>Threaded</strong>: uses 1 cpu. Native threads share time somewhat randomly after running 20-100 python opcodes.</li> <li><strong>Multiprocessing</strong>: uses multiple c...
14
2009-04-08T22:22:13Z
[ "python", "multithreading", "multiprocessing", "multicore", "python-stackless" ]
Multiprocessing or Multithreading?
731,993
<p>I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.</p> <p>When the user starts a simulation, and defines an initial state, I want the program...
24
2009-04-08T21:44:44Z
732,176
<p>With CPython multiple threads can't execute at the same time because of the GIL: <a href="http://en.wikipedia.org/wiki/Global%5FInterpreter%5FLock" rel="nofollow">link text</a>.</p> <p>I think it's still possible that threads boost your application, e.g. a thread might block on I/O while another one does some work....
5
2009-04-08T22:41:01Z
[ "python", "multithreading", "multiprocessing", "multicore", "python-stackless" ]