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 |
|---|---|---|---|---|---|---|---|---|---|
Run a Python project in Eclipse as root | 981,411 | <p>I use Eclipse as my IDE, and when I run my application I wish the application itself to run as root. My program currently checks if it is root, and if not it restarts itself with <code>gksudo</code>. The output, however, isn't written to the console. I can't use <code>sudo</code>, since it doesn't give me a graphica... | 8 | 2009-06-11T14:24:30Z | 33,567,564 | <p>You might need to create a runner with external tools pointing to '/sur/bin/sudo' and pointing to variable ${resource_loc}
Run -> External tools -> External Tools Configuration
The below link will guide you on creating a external runner,</p>
<p><a href="http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.pl... | 0 | 2015-11-06T13:08:57Z | [
"python",
"eclipse",
"root",
"sudo",
"gksudo"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,705 | <p><code>python myscript.py</code></p>
| 3 | 2009-06-11T15:08:56Z | [
"python",
"windows",
"command-line"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,707 | <p>See <a href="http://www.voidspace.org.uk/python/articles/command%5Fline.shtml#path" rel="nofollow">Basic Hints for Windows Command Line Programming</a>.</p>
<p>If your python installation directory is included in <code>%PATH%</code> -</p>
<pre><code>C:\> python myscript.py
</code></pre>
<p>If you know the inst... | 3 | 2009-06-11T15:09:33Z | [
"python",
"windows",
"command-line"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,711 | <p>If your script is foo.py, you can simply do</p>
<pre><code>C:\Python25\python.exe foo.py
</code></pre>
<p>Assuming you have python 2.5 installed in the default location. Alternatively, you can add C:\Python25 to your %PATH%, so that:</p>
<pre><code>python foo.py
</code></pre>
<p>will work. But be aware that chan... | 2 | 2009-06-11T15:10:17Z | [
"python",
"windows",
"command-line"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,712 | <p>I do it this way:</p>
<pre><code>C:\path\to\folder> yourscript.py
</code></pre>
| 4 | 2009-06-11T15:10:18Z | [
"python",
"windows",
"command-line"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,725 | <ol>
<li>do you have python installed? if not
install it from python.org</li>
<li><p>on command line use </p>
<p>python "path to
script.py"</p></li>
<li><p>if python is not in PATH
list you can add it to PATH in
environment variables or directly
use path to python.exe e.g. </p>
<pre><code> c:\pytho... | 0 | 2009-06-11T15:13:32Z | [
"python",
"windows",
"command-line"
] |
windows command line and Python | 981,691 | <p>I have a python script that i want to run from the command line but unsure how to run it. Thanks :)</p>
| 1 | 2009-06-11T15:07:05Z | 981,747 | <p>You might find it useful to include a .bat file which calls the .py script. Then all you need to do is to type the name of your script to run it.</p>
<p>Try something like:
python %~dp0\%~n0.py %*</p>
<p>(From <a href="http://wiki.tcl.tk/2455" rel="nofollow">http://wiki.tcl.tk/2455</a>)</p>
| 0 | 2009-06-11T15:17:15Z | [
"python",
"windows",
"command-line"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 981,816 | <p>I do not fully understand the need because you can collect all the modules and iterate thru test case classes</p>
<p>but if you do want to get all names in current scope
use something like</p>
<pre><code>execfile("mod.py")
</code></pre>
<p>it will get all classed defined in mod.py into scope</p>
| 0 | 2009-06-11T15:29:04Z | [
"python"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 981,980 | <p>I'm not sure I exactly understand your question, but you might try <a href="http://somethingaboutorange.com/mrl/projects/nose/" rel="nofollow">nose</a> to <a href="http://www.somethingaboutorange.com/mrl/projects/nose/0.11.1/finding%5Ftests.html" rel="nofollow">discover</a> your test suites.</p>
| 1 | 2009-06-11T15:56:16Z | [
"python"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 981,997 | <p>To get classes from some module, there can be better way but here is generic way to get you started</p>
<pre><code>mod = __import__("mod")
for klass in vars(mod):
o = getattr(mod, klass)
if type(o) == type:
print o
</code></pre>
| 3 | 2009-06-11T15:59:05Z | [
"python"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 983,427 | <p>you could use the exec on a formatted string for each module name in your scanned directory:</p>
<pre><code>exec "from %s import *" % moduleName
</code></pre>
| 0 | 2009-06-11T20:20:15Z | [
"python"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 15,397,425 | <p>Place this code in __init__.py of the tests package and it will import all tests in the current package and subpackages.</p>
<pre><code>import pkgutil
import unittest
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
for na... | 0 | 2013-03-13T22:07:09Z | [
"python"
] |
python automatic (or dynamic) import classes in packages | 981,753 | <p>I'm writing some kind of automated test suite and I want to make sure the classes contained in the package 'tests' get imported automatically upon runtime in the main namespace, without adding them to the <code>__init__</code> file of the package.</p>
<p>So here is the script I have so far:</p>
<pre><code>import o... | 2 | 2009-06-11T15:17:55Z | 32,368,953 | <p>Instead of using <code>__import__</code> and assign it to a variable like this</p>
<pre><code>modulename = __import__(module)
</code></pre>
<p>Use <code>importlib.import_module</code> instead, and don't assign it:</p>
<pre><code>import importlib
importlib.import_module(module)
</code></pre>
<p>At least on Python... | 0 | 2015-09-03T07:00:04Z | [
"python"
] |
How to tractably solve the assignment optimisation task | 982,127 | <p>I'm working on a script that takes the elements from <code>companies</code> and pairs them up with the elements of <code>people</code>. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary <code>ctrPa... | 3 | 2009-06-11T16:22:48Z | 982,219 | <p>This line:</p>
<p>remainingCompanies = companies[1:len(companies)]</p>
<p>Can be replaced with this line:</p>
<pre><code>remainingCompanies = companies[1:]
</code></pre>
<p>For a very slight speed increase. That's the only improvement I see.</p>
| 0 | 2009-06-11T16:39:58Z | [
"python",
"algorithm",
"optimization",
"dynamic-programming"
] |
How to tractably solve the assignment optimisation task | 982,127 | <p>I'm working on a script that takes the elements from <code>companies</code> and pairs them up with the elements of <code>people</code>. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary <code>ctrPa... | 3 | 2009-06-11T16:22:48Z | 982,300 | <p>If you want to get a copy of a tuple as a list you can do
mylist = list(mytuple)</p>
| 0 | 2009-06-11T16:54:22Z | [
"python",
"algorithm",
"optimization",
"dynamic-programming"
] |
How to tractably solve the assignment optimisation task | 982,127 | <p>I'm working on a script that takes the elements from <code>companies</code> and pairs them up with the elements of <code>people</code>. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary <code>ctrPa... | 3 | 2009-06-11T16:22:48Z | 982,306 | <p>To all those who wonder about the use of learning theory, this question is a good illustration. The right question is not about a "fast way to bounce between lists and tuples in python" â the reason for the slowness is something deeper.</p>
<p>What you're trying to solve here is known as the <a href="http://en.w... | 19 | 2009-06-11T16:55:18Z | [
"python",
"algorithm",
"optimization",
"dynamic-programming"
] |
How to tractably solve the assignment optimisation task | 982,127 | <p>I'm working on a script that takes the elements from <code>companies</code> and pairs them up with the elements of <code>people</code>. The goal is to optimize the pairings such that the sum of all pair values is maximized (the value of each individual pairing is precomputed and stored in the dictionary <code>ctrPa... | 3 | 2009-06-11T16:22:48Z | 982,331 | <p>i see two issues here:</p>
<ol>
<li><p>efficiency: you're recreating the same <code>remainingPeople</code> sublists for each company. it would be better to create all the <code>remainingPeople</code> and all the <code>remainingCompanies</code> once and then do all the combinations.</p></li>
<li><p>memoization: you... | 1 | 2009-06-11T16:59:58Z | [
"python",
"algorithm",
"optimization",
"dynamic-programming"
] |
Trac Using Database Authentication | 982,226 | <p>Is it possible to use a database for authentication with Trac?
.htpasswd auth is not desired in this install.</p>
<p>Using Trac .11 and MySQL as the database. Trac is currently using the database, but provides no authentication.</p>
| 5 | 2009-06-11T16:40:52Z | 982,345 | <p>Out of the box, <a href="http://trac.edgewall.org/wiki/TracAuthenticationIntroduction" rel="nofollow">Trac doesn't actually do its own authentication</a>, it leaves it up to the web server. So, you've got a wealth of Apache-related options available to you. You could maybe look at something like <a href="http://sour... | 5 | 2009-06-11T17:03:07Z | [
"python",
"mysql",
"apache",
"trac"
] |
Trac Using Database Authentication | 982,226 | <p>Is it possible to use a database for authentication with Trac?
.htpasswd auth is not desired in this install.</p>
<p>Using Trac .11 and MySQL as the database. Trac is currently using the database, but provides no authentication.</p>
| 5 | 2009-06-11T16:40:52Z | 982,372 | <p>You can use <a href="http://trac-hacks.org/wiki/AccountManagerPlugin#SessionStore" rel="nofollow">Account Manager Plugin with SessionStore</a></p>
<p>The AccountManagerPlugin offers several features for managing user accounts: </p>
<ul>
<li>allow users to register new accounts </li>
<li>login via an HTML form inst... | 1 | 2009-06-11T17:08:50Z | [
"python",
"mysql",
"apache",
"trac"
] |
Trac Using Database Authentication | 982,226 | <p>Is it possible to use a database for authentication with Trac?
.htpasswd auth is not desired in this install.</p>
<p>Using Trac .11 and MySQL as the database. Trac is currently using the database, but provides no authentication.</p>
| 5 | 2009-06-11T16:40:52Z | 3,609,831 | <p>Please refer to <a href="http://trac-hacks.org/wiki/AccountManagerPlugin" rel="nofollow">http://trac-hacks.org/wiki/AccountManagerPlugin</a></p>
<p>Do the following on your trac.ini
[components]
; be sure to enable the component
acct_mgr.svnserve.* = enabled
acct_mgr.svnserve.svnservepasswordstore = enabled
; choos... | 0 | 2010-08-31T14:10:04Z | [
"python",
"mysql",
"apache",
"trac"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 982,287 | <p>Many password-accepting protocols and facilities have a way to specify a keyfile rather than a password. That strategy will probably work here; rather than hard-coding a password, hard-code a filename (or better yet, make it a parameter!). You could even go as far as SSH and refuse to load keyfiles that aren't (1)... | 0 | 2009-06-11T16:52:00Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 982,288 | <p>You should avoid storing the password in clear in the first place. That's the only "true" solution.</p>
<p>Now, you can encrypt the password easily with the hash module (md5 and similar modules in python 2.5 and below).</p>
<pre><code>import hashlib
mypass = "yo"
a = hashlib.sha256(mypass).digest()
</code></pre>
| 1 | 2009-06-11T16:52:01Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 982,310 | <p>If you just want to prevent casually glancing at a password, you may want to consider encoding/decoding the password to/from <a href="http://docs.python.org/library/base64.html">base64</a>. It's not secure in the least, but the password won't be casually human/robot readable.</p>
<pre><code>import base64
# Encode p... | 20 | 2009-06-11T16:57:06Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 982,431 | <p>It depends alot on why you're keeping the password around, and where you feel there is a security issue.</p>
<p>If you're storing or matching this password to a database in the future:</p>
<p>This shouldn't really be a problem, but if you're concerned, use your database's password encryption early and store that (... | 0 | 2009-06-11T17:18:51Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 982,750 | <p>Obviously your best option is to delegate this to a third party. If you can authenticate with whatever you're connecting to using some other credential (eg. the user account your process is running as), you can leave the permission levels up to the OS layer. Alternatively, if sufficiently important / possible you ... | 2 | 2009-06-11T18:13:23Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Obfuscate strings in Python | 982,260 | <p>I have a password string that must be passed to a method. Everything works fine but I don't feel comfortable storing the password in clear text. Is there a way to obfuscate the string or to truly encrypt it? I'm aware that obfuscation can be reverse engineered, but I think I should at least try to cover up the passw... | 7 | 2009-06-11T16:47:30Z | 35,443,610 | <p>The base64 answer is good for obfuscating the password and will work with no user intervention at the cost of being insecure. If the user can be logged in with access to a system keyring service, take a look at the <a href="https://pypi.python.org/pypi/keyring" rel="nofollow">keyring</a> package. I used it to store... | 0 | 2016-02-16T21:44:35Z | [
"python",
"linux",
"encryption",
"passwords",
"obfuscation"
] |
Finding PI digits using Monte Carlo | 982,381 | <p>I have tried many algorithms for finding Ï using Monte Carlo.
One of the solutions (in Python) is this:</p>
<pre><code>def calc_PI():
n_points = 1000000
hits = 0
for i in range(1, n_points):
x, y = uniform(0.0, 1.0), uniform(0.0, 1.0)
if (x**2 + y**2) <= 1.0:
hits += 1
... | 4 | 2009-06-11T17:10:29Z | 982,423 | <p>Your fractional error goes by <code>sqrt(N)/N = 1/sqrt(N)</code>, So this is a very inefficient way to get a precise estimate. This limit is set by the statistical nature of the measurement and can't be beaten.</p>
<p>You should be able to get about <code>floor(log_10(N))/2-1</code> digits of good precision for <co... | 8 | 2009-06-11T17:18:03Z | [
"python",
"statistics",
"montecarlo",
"pi"
] |
Finding PI digits using Monte Carlo | 982,381 | <p>I have tried many algorithms for finding Ï using Monte Carlo.
One of the solutions (in Python) is this:</p>
<pre><code>def calc_PI():
n_points = 1000000
hits = 0
for i in range(1, n_points):
x, y = uniform(0.0, 1.0), uniform(0.0, 1.0)
if (x**2 + y**2) <= 1.0:
hits += 1
... | 4 | 2009-06-11T17:10:29Z | 982,475 | <p>This is a classic example of Monte Carlo. But if you're trying to break the calculation of pi into parallel parts, why not just use an infinite series and let each core take a range, then sum the results as you go?</p>
<p><a href="http://mathworld.wolfram.com/PiFormulas.html">http://mathworld.wolfram.com/PiFormulas... | 14 | 2009-06-11T17:26:25Z | [
"python",
"statistics",
"montecarlo",
"pi"
] |
Finding PI digits using Monte Carlo | 982,381 | <p>I have tried many algorithms for finding Ï using Monte Carlo.
One of the solutions (in Python) is this:</p>
<pre><code>def calc_PI():
n_points = 1000000
hits = 0
for i in range(1, n_points):
x, y = uniform(0.0, 1.0), uniform(0.0, 1.0)
if (x**2 + y**2) <= 1.0:
hits += 1
... | 4 | 2009-06-11T17:10:29Z | 986,367 | <p>Use a quasi random number generator (<a href="http://www.nag.co.uk/IndustryArticles/introduction_to_quasi_random_numbers.pdf" rel="nofollow">http://www.nag.co.uk/IndustryArticles/introduction_to_quasi_random_numbers.pdf</a>) instead of a standard pseudo RNG. Quasi random numbers cover the integration area (what you'... | 4 | 2009-06-12T12:25:04Z | [
"python",
"statistics",
"montecarlo",
"pi"
] |
How can I search and replace in XML with Python? | 982,414 | <p>I am in the middle of making a script for doing translation of xml documents. It's actually pretty cool, the idea is (and it is working) to take an xml file (or a folder of xml files) and open it, parse the xml, get whatever is in between some tags and using the google translate api's translate it and replace the co... | 3 | 2009-06-11T17:16:23Z | 982,493 | <p>Don't try to parse XML using regular expressions! <a href="http://welbog.homeip.net/glue/53/XML-is-not-regular" rel="nofollow">XML is not regular</a> and therefore regular expressions are not suited to doing this kind of task.</p>
<p>Use an actual XML parser. Many of these are readily available for Python. A quick ... | 7 | 2009-06-11T17:29:18Z | [
"python",
"xml",
"regex"
] |
How can I search and replace in XML with Python? | 982,414 | <p>I am in the middle of making a script for doing translation of xml documents. It's actually pretty cool, the idea is (and it is working) to take an xml file (or a folder of xml files) and open it, parse the xml, get whatever is in between some tags and using the google translate api's translate it and replace the co... | 3 | 2009-06-11T17:16:23Z | 982,674 | <p><a href="http://effbot.org/zone/element.htm">ElementTree</a> would be a good choice for this kind of parsing. It is easy to use and lightweight and supports outputting XML after you do operations on it (as simple as calling write()). It comes packaged with Python standard libraries in the latest versions (I believe ... | 5 | 2009-06-11T18:04:38Z | [
"python",
"xml",
"regex"
] |
Mark data as sensitive in python | 982,682 | <p>I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger?</p>
| 17 | 2009-06-11T18:05:47Z | 982,735 | <p>No way to "mark as sensitive", but you could encrypt the data in memory and decrypt it again when you need to use it -- not a perfect solution but the best I can think of.</p>
| 2 | 2009-06-11T18:12:02Z | [
"python",
"security",
"passwords",
"coredump"
] |
Mark data as sensitive in python | 982,682 | <p>I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger?</p>
| 17 | 2009-06-11T18:05:47Z | 982,748 | <ul>
<li>XOR with a one-time pad stored separately</li>
<li>always store salted hash rather than password itself</li>
</ul>
<p>or, if you're very paranoid about dumps, store unique random key in some other place, e.g. i a different thread, in a registry, on your server, etc.</p>
| 2 | 2009-06-11T18:13:14Z | [
"python",
"security",
"passwords",
"coredump"
] |
Mark data as sensitive in python | 982,682 | <p>I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger?</p>
| 17 | 2009-06-11T18:05:47Z | 983,525 | <p><strong>Edit</strong></p>
<p>I have made a solution that uses ctypes (which in turn uses C) to zero memory.</p>
<pre><code>import sys
import ctypes
def zerome(string):
location = id(string) + 20
size = sys.getsizeof(string) - 20
memset = ctypes.cdll.msvcrt.memset
# For Linux, use the followi... | 27 | 2009-06-11T20:39:42Z | [
"python",
"security",
"passwords",
"coredump"
] |
Mark data as sensitive in python | 982,682 | <p>I need to store a user's password for a short period of time in memory. How can I do so yet not have such information accidentally disclosed in coredumps or tracebacks? Is there a way to mark a value as "sensitive", so it's not saved anywhere by a debugger?</p>
| 17 | 2009-06-11T18:05:47Z | 14,667,881 | <blockquote>
<p>... <strong>The only solution to this is to use mutable data structures.</strong> That
is, you must only use data structures that allow you to dynamically
replace elements. For example, in Python you can use lists to store an
array of characters. However, <em>every time you add or remove an elem... | 3 | 2013-02-02T23:53:57Z | [
"python",
"security",
"passwords",
"coredump"
] |
Weird program behaviour in Python | 982,970 | <p>When running the following code, which is an easy problem, the Python interpreter works weirdly:</p>
<pre><code>n = input()
for i in range(n):
testcase = raw_input()
#print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
</code></pre>
<p>The problem consists in taking n strings... | 0 | 2009-06-11T18:59:44Z | 983,082 | <p>Are you sure that the problem is the print i statement? The code works as
expected when I uncomment that statement and run it. However, if I forget to
enter a value for the first input() call, and just enter "4 PYTHON" right off
the bat, then I get:</p>
<pre><code>"SyntaxError: unexpected EOF while parsing"
</cod... | 4 | 2009-06-11T19:18:25Z | [
"python"
] |
Weird program behaviour in Python | 982,970 | <p>When running the following code, which is an easy problem, the Python interpreter works weirdly:</p>
<pre><code>n = input()
for i in range(n):
testcase = raw_input()
#print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
</code></pre>
<p>The problem consists in taking n strings... | 0 | 2009-06-11T18:59:44Z | 983,102 | <p>This worked for me too, give it a try...</p>
<pre><code>n = raw_input()
n = int(n)
for i in range(n):
testcase = raw_input()
print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
</code></pre>
<p>Note the <code>n = int(n)</code></p>
<p>PS: You can continue to use <code>n = input()</... | 1 | 2009-06-11T19:22:14Z | [
"python"
] |
Weird program behaviour in Python | 982,970 | <p>When running the following code, which is an easy problem, the Python interpreter works weirdly:</p>
<pre><code>n = input()
for i in range(n):
testcase = raw_input()
#print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
</code></pre>
<p>The problem consists in taking n strings... | 0 | 2009-06-11T18:59:44Z | 989,853 | <p>I am yet another who has no trouble with or without the commented print statement. The input function on the first line is no problem as long as I give it something Python can evaluate. So the most likely explanation is that when you are getting that error, you have typed something that isn't a valid Python expres... | 1 | 2009-06-13T03:28:35Z | [
"python"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 983,210 | <p>As you suspected, this is a perfect situation for threading. <a href="http://www.wellho.net/solutions/python-python-threads-a-first-example.html" rel="nofollow">Here</a> is a short guide I found immensely helpful when doing my own first bit of threading in python.</p>
| 2 | 2009-06-11T19:44:41Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 983,214 | <p>As you rightly indicated, you could create a number of threads, each of which is responsible for performing urlretrieve operations. This allows the main thread to continue uninterrupted. </p>
<p>Here is a tutorial on threading in python:
<a href="http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf" rel="nof... | 2 | 2009-06-11T19:45:06Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 983,300 | <p>Here's an example of how to use threading.Thread. Just replace the class name with your own and the run function with your own. Note that threading is great for IO restricted applications like your's and can really speed it up. Using pythong threading strictly for computation in standard python doesn't help becau... | 2 | 2009-06-11T20:01:03Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 983,971 | <p>You'll probably benefit from <code>threading</code> or <a href="http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers" rel="nofollow"><code>multiprocessing</code></a> modules. You don't actually need to create all those <code>Thread</code>-based classes by yourself, there is a simpler method us... | 2 | 2009-06-11T21:58:55Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 984,119 | <p>You either need to use threads, or an asynchronous networking library such as <a href="http://twistedmatrix.com" rel="nofollow">Twisted</a>. I suspect that using threads might be simpler in your particular use case.</p>
| 0 | 2009-06-11T22:34:35Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
How to do a non-blocking URL fetch in Python | 983,144 | <p>I am writing a GUI app in <a href="http://www.pyglet.org/" rel="nofollow">Pyglet</a> that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">urllib.urlretrieve</a> to grab them, but this blocks ... | 1 | 2009-06-11T19:31:02Z | 984,197 | <p>You have these choices:</p>
<ul>
<li>Threads: easiest but doesn't scale well</li>
<li>Twisted: medium difficulty, scales well but shares CPU due to GIL and being single threaded.</li>
<li>Multiprocessing: hardest. Scales well if you know how to write your own event loop.</li>
</ul>
<p>I recommend just using thread... | 1 | 2009-06-11T22:55:42Z | [
"python",
"multithreading",
"blocking",
"pyglet"
] |
python and sys.argv | 983,201 | <pre><code>if len(sys.argv) < 2:
sys.stderr.write('Usage: sys.argv[0] ')
sys.exit(1)
if not os.path.exists(sys.argv[1]):
sys.stderr.write('ERROR: Database sys.argv[1] was not found!')
sys.exit(1)
</code></pre>
<p>This is a portion of code I'm working on. The first part I'm trying to say if the use... | 51 | 2009-06-11T19:42:30Z | 983,229 | <p>In Python, you can't just embed arbitrary Python expressions into literal strings and have it substitute the value of the string. You need to either:</p>
<pre><code>sys.stderr.write("Usage: " + sys.argv[0])
</code></pre>
<p>or</p>
<pre><code>sys.stderr.write("Usage: %s" % sys.argv[0])
</code></pre>
<p>Also, you ... | 32 | 2009-06-11T19:47:14Z | [
"python"
] |
python and sys.argv | 983,201 | <pre><code>if len(sys.argv) < 2:
sys.stderr.write('Usage: sys.argv[0] ')
sys.exit(1)
if not os.path.exists(sys.argv[1]):
sys.stderr.write('ERROR: Database sys.argv[1] was not found!')
sys.exit(1)
</code></pre>
<p>This is a portion of code I'm working on. The first part I'm trying to say if the use... | 51 | 2009-06-11T19:42:30Z | 983,355 | <p>I would do it this way:</p>
<pre><code>import sys
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <database>" % (argv[0],))
return 1
if not os.path.exists(argv[1]):
sys.stderr.write("ERROR: Database %r was not found!" % (argv[1],))
return 1
if __name... | 29 | 2009-06-11T20:09:26Z | [
"python"
] |
python and sys.argv | 983,201 | <pre><code>if len(sys.argv) < 2:
sys.stderr.write('Usage: sys.argv[0] ')
sys.exit(1)
if not os.path.exists(sys.argv[1]):
sys.stderr.write('ERROR: Database sys.argv[1] was not found!')
sys.exit(1)
</code></pre>
<p>This is a portion of code I'm working on. The first part I'm trying to say if the use... | 51 | 2009-06-11T19:42:30Z | 984,145 | <p>BTW you can pass the error message directly to sys.exit:</p>
<pre><code>if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 84 | 2009-06-11T22:41:16Z | [
"python"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 983,382 | <p>One way to do this in Python 2, is to use <code>raw_input()</code>:</p>
<pre><code>raw_input("Press Enter to continue...")
</code></pre>
<p>In python3 it's just <code>input()</code></p>
| 205 | 2009-06-11T20:11:59Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 1,135,035 | <p>In Python 3, no <code>raw_input()</code> exists. So, just use:</p>
<pre><code>input("Press Enter to continue...")
</code></pre>
<p>This only waits for a user to press enter though, so you might want to use <strong>msvcrt</strong> ((Windows/DOS only) The msvcrt module gives you access to a number of functions in th... | 176 | 2009-07-16T01:48:20Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 5,004,022 | <p>The python <a href="http://docs.python.org/faq/library#how-do-i-get-a-single-keypress-at-a-time">manual</a> provides the following:</p>
<pre><code>import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON &am... | 13 | 2011-02-15T13:09:08Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 6,037,500 | <p>I don't know of a platform independent way of doing it, but under Windows, if you use the msvcrt module, you can use its getch function:</p>
<pre><code>import msvcrt
c = msvcrt.getch()
print 'you entered', c
</code></pre>
<p>mscvcrt also includes the non-blocking kbhit() function to see if a key was pressed withou... | 10 | 2011-05-17T21:42:22Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 6,599,441 | <p>On my linux box, I use the following code. This is similar to the <a href="http://docs.python.org/faq/library#how-do-i-get-a-single-keypress-at-a-time">manual</a> entry mentioned elsewhere but that code spins in a tight loop where this code doesn't and there are lots of odd corner cases that code doesn't account for... | 27 | 2011-07-06T15:58:39Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 11,094,086 | <p>If you want to see if they pressed a exact key (like say 'b') Do this:</p>
<pre><code>while True:
choice = raw_input("> ")
if choice == 'b' :
print "You win"
input("yay")
break
</code></pre>
| 2 | 2012-06-19T03:42:22Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 16,933,120 | <p>If you are ok with depending on system commands you can use the following:</p>
<p>Linux:</p>
<pre><code>os.system('read -s -n 1 -p "Press any key to continue..."')
print
</code></pre>
<p>Windows:</p>
<pre><code>os.system("pause")
</code></pre>
| 15 | 2013-06-05T06:36:48Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 20,651,266 | <p>os.system seems to always invoke sh, which does not recognize the s and n options for read. However the read command can be passed to bash: </p>
<pre><code> os.system("""bash -c 'read -s -n 1 -p "Press any key to continue..."'""")
</code></pre>
| 0 | 2013-12-18T06:39:33Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 22,151,521 | <p>Simply using</p>
<pre><code>input("Press Enter to continue...")
</code></pre>
<p>will cause a SyntaxError: expected EOF while parsing.</p>
<p>Simple fix use:</p>
<pre><code>try:
input("Press enter to continue")
except SyntaxError:
pass
</code></pre>
| 11 | 2014-03-03T16:06:30Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 26,338,755 | <p>or you could do</p>
<pre><code>print("This is a good joke")
print("what happened when the chicken crossed the road")
gap = input("")
if gap == (""):
print("")
else:
print("")
print("it died")
</code></pre>
| -1 | 2014-10-13T11:18:53Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 34,956,791 | <p><strong>Cross Platform, Python 2/3 code:</strong></p>
<pre><code># import sys, os
def wait_key():
''' Wait for a key press on the console and return it. '''
result = None
if os.name == 'nt':
import msvcrt
result = msvcrt.getch()
else:
import termios
fd = sys.stdin.fi... | 2 | 2016-01-22T22:10:39Z | [
"python",
"wait",
"keyboard-input"
] |
How do I make python to wait for a pressed key | 983,354 | <p>I want my script to wait until the user presses any key.</p>
<p>How do I do that?</p>
| 229 | 2009-06-11T20:09:19Z | 38,647,292 | <p>I am new to python and I was already thinking I am too stupid to reproduce the simplest suggestions made here.
It turns out, there's a pitfall one should know:</p>
<p>When a python-script is executed from IDLE, some IO-commands seem to behave completely different (as there is actually no terminal window).</p>
<p>E... | 0 | 2016-07-28T21:47:58Z | [
"python",
"wait",
"keyboard-input"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 984,379 | <p>Go For <strong>PyQt</strong>:</p>
<p><a href="http://www.riverbankcomputing.co.uk/software/pyqt/download" rel="nofollow">http://www.riverbankcomputing.co.uk/software/pyqt/download</a></p>
<p>Download The <strong>Qt Developers tool</strong> to generate the gui automatically</p>
<p>www.qtsoftware.com/products/devel... | 3 | 2009-06-11T23:50:19Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 984,789 | <p>I'm not exactly sure if I can be completely of help to you @Adrian, but I've been researching something very close to what you are discussing...</p>
<p>Using something like Linq to XML may assist you in the validation of the data being entered, as there are already methods that will validate the data for you.</p>
... | 0 | 2009-06-12T02:57:29Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 985,246 | <p>One solution could be to write an XSL transformation that converts the XML file into a XAML file.</p>
| 1 | 2009-06-12T06:21:05Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 1,015,238 | <p>If the GUI will be simple and you don't bother about the geometry of the components(widgets) in the dialogs, Qt will be a good option. Actually I'm working on a similar task for my project, and my goal was to validate the form data by using an XML file.</p>
<p>Using Qt, it is possible to access any widget on the di... | 1 | 2009-06-18T21:20:01Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 2,118,609 | <p>It's worth taking a look at <a href="http://www.jaxfront.org/" rel="nofollow">Jaxfront</a> which is able to consume an XSD and generate a form (including HTML). You can also supply an instance XML document for it to load into the GUI, and save out instance XML documents too.</p>
<p>I tried it with one of our fairl... | 1 | 2010-01-22T16:13:06Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 4,294,774 | <p>There is an experimental tool, which does that, automatically create a desktop UI for handling data stored in XML. It's called <a href="http://www.lst.de/~cs/kode/kxforms.html" rel="nofollow">KXForms</a>. It comes as part of a suite of tools for handling XML data in various ways, like creating C++ code from an XML s... | 0 | 2010-11-28T00:58:13Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Create a GUI from a XML schema automatically | 983,399 | <p>I have to write a desktop application to edit data stored in a XML file. The format is defined by a XML schema file (.xsd). The format is quite complex. </p>
<p>Are there tools which can generate a basic GUI automatically? It's not yet decided which language to use. I have experience in Python and C++ using wxWidge... | 14 | 2009-06-11T20:14:44Z | 5,012,070 | <p>"<a href="http://www.felixgolubov.com/XMLEditor/" rel="nofollow">XAmple XML Editor</a> project introduces a java Swing based XML editor that analyzes a given schema and then generates a document-specific graphical user interface. Unlike other XML editors, the XAmple XML editor GUI exposes not just a tree representat... | 0 | 2011-02-16T02:59:58Z | [
"c#",
"c++",
"python",
"xml",
"schema"
] |
Call flatpage from with a view | 983,722 | <p>Can I call a Flatpage from with a view. Say I have some code like:</p>
<pre><code>def myview(request):
if request.subdomain != "www":
return HttpResponseRedirect("http://"+request.subdomain+".mydomain/login/")
else:
call the flatpage here...
</code></pre>
| 2 | 2009-06-11T21:13:30Z | 984,276 | <p>You sure can. Just make sure you have the <code>flatpage</code> function included in your view code:</p>
<pre><code>from django.contrib.flatpages.views import flatpage
</code></pre>
<p>And stick the following in your <code>else:</code></p>
<pre><code>return flatpage(request, '/path/to/your/flatpage/')
</code></pr... | 4 | 2009-06-11T23:16:14Z | [
"python",
"django"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,877 | <p>The data you are encoding is a keyless array, so JSON encodes it with [] brackets. See www.json.org for more information about that. The curly braces are used for lists with key/value pairs.</p>
<p>From www.json.org:</p>
<blockquote>
<p>JSON is built on two structures:</p>
<p>A collection of name/value pair... | 4 | 2009-06-11T21:41:11Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,879 | <p>Python <code>lists</code> translate to JSON <code>arrays</code>. What it is giving you is a perfectly valid JSON string that could be used in a Javascript application. To get what you expected, you would need to use a <code>dict</code>:</p>
<pre><code>>>> json.dumps({'apple': 'cat', 'banana':'dog', 'pear':... | 51 | 2009-06-11T21:41:16Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,884 | <p>In <code>simplejson</code> (or the library <code>json</code> in Python 2.6 and later), <code>loads</code> takes a JSON string and returns a Python data structure, <code>dumps</code> takes a Python data structure and returns a JSON string. JSON string can encode Javascript arrays, not just objects, and a Python list ... | 16 | 2009-06-11T21:41:48Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,907 | <p>JSON uses square brackets for lists ( <code>[ "one", "two", "three" ]</code> ) and curly brackets for key/value dictionaries (also called objects in JavaScript, <code>{"one":1, "two":"b"}</code>).</p>
<p>The dump is quite correct, you get a list of three elements, each one is a list of two strings.</p>
<p>if you w... | 3 | 2009-06-11T21:45:40Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,913 | <p>So, simplejson.loads takes a json string and returns a data structure, which is why you are getting that type error there.</p>
<p>simplejson.dumps(data) comes back with </p>
<pre><code>'[["apple", "cat"], ["banana", "dog"], ["pear", "fish"]]'
</code></pre>
<p>Which is a json array, which is what you want, since y... | 3 | 2009-06-11T21:46:21Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 983,915 | <p>Try:</p>
<pre><code>import simplejson
data = {'apple': 'cat', 'banana':'dog', 'pear':'fish'}
data_json = "{'apple': 'cat', 'banana':'dog', 'pear':'fish'}"
simplejson.loads(data_json) # outputs data
simplejson.dumps(data) # outputs data_joon
</code></pre>
<p>NB: Based on Paolo's answer.</p>
| 3 | 2009-06-11T21:46:39Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Python JSON encoding | 983,855 | <p>I'm trying to encode data to JSON in Python and I been having a quite a bit of trouble. I believe the problem is simply a misunderstanding. </p>
<p>I'm relatively new to Python and never really got familiar with the various Python data types, so that's most likely what's messing me up. </p>
<p>Currently I am decla... | 39 | 2009-06-11T21:37:09Z | 984,243 | <p>I think you are simply exchanging <em>dumps</em> and <em>loads</em>. </p>
<pre><code>>>> import json
>>> data = [['apple', 'cat'], ['banana', 'dog'], ['pear', 'fish']]
</code></pre>
<p>The first returns as a (JSON encoded) string its data argument:</p>
<pre><code>>>> encoded_str = json.... | 16 | 2009-06-11T23:07:36Z | [
"python",
"json",
"encoding",
"types",
"simplejson"
] |
Facebook, Django, and Google App Engine | 984,071 | <p>I'm experimenting with <a href="http://code.google.com/p/app-engine-patch/">app-engine-patch</a> (Django for GAE) on Google App Engine. And I would like to write a Facebook application. Is it possible to use PyFacebook and its middleware? Or is there some other solution?</p>
| 14 | 2009-06-11T22:20:36Z | 984,099 | <p>According to <a href="http://forum.developers.facebook.com/viewtopic.php?id=13396" rel="nofollow">this post</a>, you need a slightly modified PyFacebook that you can download from a URL given in said post (I haven't tried it myself, though).</p>
<p>Edit: that link is wrong -- better link and more discussion on <a h... | 0 | 2009-06-11T22:27:59Z | [
"python",
"django",
"google-app-engine",
"facebook"
] |
Facebook, Django, and Google App Engine | 984,071 | <p>I'm experimenting with <a href="http://code.google.com/p/app-engine-patch/">app-engine-patch</a> (Django for GAE) on Google App Engine. And I would like to write a Facebook application. Is it possible to use PyFacebook and its middleware? Or is there some other solution?</p>
| 14 | 2009-06-11T22:20:36Z | 984,140 | <p>I run a system on for social networks and facebook on GAE with back-end in Python, front end in Javascript and Flash. I use mostly client side js libraries to pass data back to the server side datastore. This library for facebook to be exact: <a href="http://code.google.com/p/facebookjsapi/">http://code.google.com/p... | 7 | 2009-06-11T22:40:35Z | [
"python",
"django",
"google-app-engine",
"facebook"
] |
Facebook, Django, and Google App Engine | 984,071 | <p>I'm experimenting with <a href="http://code.google.com/p/app-engine-patch/">app-engine-patch</a> (Django for GAE) on Google App Engine. And I would like to write a Facebook application. Is it possible to use PyFacebook and its middleware? Or is there some other solution?</p>
| 14 | 2009-06-11T22:20:36Z | 984,903 | <p>Adding the Facebook directory from the PyFacebook install directory to the app-engine-patch application allows you to add 'facebook.djangofb.FacebookMiddleware', to the MIDDLEWARE_CLASSES in settings.py. Then your view can use 'import facebook.djangofb as facebook' and '@facebook.require_login().'</p>
<p>I haven't ... | 5 | 2009-06-12T04:00:57Z | [
"python",
"django",
"google-app-engine",
"facebook"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,220 | <p>Firstly, </p>
<p>you have forgotten to copy the temp file back onto the original.</p>
<p>Secondly:</p>
<p>use sed -i or perl -i instead of python.</p>
<p>For instance:</p>
<pre><code>perl -i -pe 's/mrddb2/shpdb2/;s/MRDDB2/SHPDB2/' *
</code></pre>
| 0 | 2009-06-11T23:03:28Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,296 | <p>I don't have the exact answer for you, but what might help is to stick some <code>print</code> lines in there in strategic places, like print each line before it was modified, then again after it was modified. Then place another one after the line was modified just before it is written to the file. Then just befor... | 0 | 2009-06-11T23:20:50Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,297 | <p>Your code (correctly indented, though I don't think there's a way to indent it so it runs but doesn't work right) actually seems right. Keep in mind, temp.out will be the replaced contents of only the last source file. Could it be that file is just blank?</p>
| 2 | 2009-06-11T23:21:39Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,311 | <p>Just glancing at the thing, it appears that your problem is with the 'w'.</p>
<p>It looks like you keep <strong>overwriting</strong>, not <strong>appending</strong>.</p>
<p>So you're basically looping through the file(s), <br />
and by the end you've only copied the last file to your temp file.</p>
<p>You'll may ... | 5 | 2009-06-11T23:26:46Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,377 | <p>if your code is actually indented as showed in the post, the write is working fine. But if it is failing, the write call may be outside the inner for loop. </p>
| 0 | 2009-06-11T23:49:54Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 984,389 | <p>Just to make sure I wasn't really missing something, I tested the code and it worked fine for me. Maybe you could try continue for everything but one specific filename and then check the contents of temp.out after that.</p>
<pre><code>import os
for filename in os.listdir("."):
if filename != 'findme.txt': con... | 0 | 2009-06-11T23:53:30Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 1,112,526 | <p>I noticed sometimes it will not print to file if you don't have a file.close after file.write.</p>
<p>For example, this program never actually saves to file, it just makes a blank file (unless you add outfile.close() right after the outfile.write.)</p>
<pre><code>outfile=open("ok.txt","w")
fc="filecontents"
outf... | 0 | 2009-07-11T00:08:18Z | [
"python",
"file-io",
"for-loop"
] |
file won't write in python | 984,216 | <p>I'm trying to replace a string in all the files within the current directory. for some reason, my temp file ends up blank. It seems my .write isn't working because the secondfile was declared outside its scope maybe? I'm new to python, so still climbing the learning curve...thanks!</p>
<p>edit: I'm aware my tempfi... | 2 | 2009-06-11T23:02:09Z | 1,112,547 | <p>@OP, you might also want to try fileinput module ( this way, you don't have to use your own temp file)</p>
<pre><code>import fileinput
for filename in os.listdir("."):
for line in fileinput.FileInput(filename,inplace=1):
line = line.strip().replace('mrddb2.','shpdb2.')
line = line.strip().replac... | 0 | 2009-07-11T00:17:16Z | [
"python",
"file-io",
"for-loop"
] |
How to recreate this PHP code in Python? | 984,244 | <p>I've found a PHP script that lets me do what I asked in <a href="http://stackoverflow.com/questions/922148/how-to-make-mechanize-not-fail-with-forms-on-this-page">this</a> SO question. I can use this just fine, but out of curiosity I'd like to recreate the following code in Python. </p>
<p>I can of course use url... | 0 | 2009-06-11T23:08:00Z | 984,378 | <p>I'm not familiar with PHP, but this may get you started.
I'm installing the opener here which will apply it to the urlopen method. If you don't want to 'install' the opener(s) you can use the opener object directly. (opener.open(url, data)).</p>
<p>Refer to:
<a href="http://docs.python.org/library/urllib2.html?hi... | 5 | 2009-06-11T23:50:06Z | [
"php",
"python",
"http",
"forms",
"automation"
] |
How to recreate this PHP code in Python? | 984,244 | <p>I've found a PHP script that lets me do what I asked in <a href="http://stackoverflow.com/questions/922148/how-to-make-mechanize-not-fail-with-forms-on-this-page">this</a> SO question. I can use this just fine, but out of curiosity I'd like to recreate the following code in Python. </p>
<p>I can of course use url... | 0 | 2009-06-11T23:08:00Z | 984,409 | <p>Similar to monkut's answer, but a little more concise.</p>
<pre><code>import urllib, urllib2
def steam_login(username,password):
data = urllib.urlencode({
'action': 'doLogin',
'goto': '',
'steamAccountName': username,
'steamPassword': password,
})
request = urllib2.Request('http... | 6 | 2009-06-12T00:02:28Z | [
"php",
"python",
"http",
"forms",
"automation"
] |
Does Pythoncard have an on change event? | 984,263 | <p>I want to do some validation whenever the value of a textfield changes. I don't see an on change event mentioned in the documentation though.</p>
| 0 | 2009-06-11T23:12:31Z | 984,511 | <p>Pythoncard is built on wxPython, and wxPython has a text change event. I know nothing about Pythoncard, but in wxPython one would use:</p>
<pre><code> t1 = wx.TextCtrl(self, -1, "some text", size=(125, -1)) # to make the text control
self.Bind(wx.EVT_TEXT, self.OnText, t1) # your OnText method handles the... | 1 | 2009-06-12T00:40:10Z | [
"python",
"events",
"event-handling",
"pythoncard",
"pycard"
] |
Does Pythoncard have an on change event? | 984,263 | <p>I want to do some validation whenever the value of a textfield changes. I don't see an on change event mentioned in the documentation though.</p>
| 0 | 2009-06-11T23:12:31Z | 2,066,008 | <p>I think the textUpdate event is what your looking for.</p>
<p><a href="http://pythoncard.sourceforge.net/framework/components/TextField.html" rel="nofollow">http://pythoncard.sourceforge.net/framework/components/TextField.html</a></p>
| 1 | 2010-01-14T17:11:52Z | [
"python",
"events",
"event-handling",
"pythoncard",
"pycard"
] |
Dynamic Language Features and Meta-Programming Used in Django | 984,272 | <p>Any good summary articles of the dynamic language and meta-programming features of Python that get utilized by Django? Or can we build that out here? Setting this up as a wiki-style entry.</p>
| 2 | 2009-06-11T23:14:48Z | 985,561 | <p>Marty Alchin (Gulopine) has a few articles on various bits of Django internals - including metaclasses and descriptors - on his blog:
<a href="http://martyalchin.com/categories/django/" rel="nofollow">http://martyalchin.com/categories/django/</a></p>
| 2 | 2009-06-12T08:12:20Z | [
"python",
"django",
"metaprogramming",
"dynamic-languages"
] |
Formatting date times provided as strings in Django | 984,412 | <p>In my Django application I get times from a webservice, provided as a string, that I use in my templates:</p>
<p>{{date.string}}</p>
<p>This provides me with a date such as:</p>
<p>2009-06-11 17:02:09+0000</p>
<p>These are obviously a bit ugly, and I'd like to present them in a nice format to my users. Django ha... | 7 | 2009-06-12T00:03:53Z | 984,446 | <p>This doesn't deal with the Django tag, but the strptime code is:</p>
<pre><code>d = strptime("2009-06-11 17:02:09+0000", "%Y-%m-%d %H:%M:%S+0000")
</code></pre>
<p>Note that you're dropping the time zone info.</p>
| 0 | 2009-06-12T00:14:08Z | [
"python",
"django",
"date",
"time",
"strptime"
] |
Formatting date times provided as strings in Django | 984,412 | <p>In my Django application I get times from a webservice, provided as a string, that I use in my templates:</p>
<p>{{date.string}}</p>
<p>This provides me with a date such as:</p>
<p>2009-06-11 17:02:09+0000</p>
<p>These are obviously a bit ugly, and I'd like to present them in a nice format to my users. Django ha... | 7 | 2009-06-12T00:03:53Z | 984,447 | <p>You're probably better off parsing the string received from the webservice in your view code, and then passing the datetime.date (or string) to the template for display. The spirit of Django templates is that very little coding work should be done there; they are for presentation only, and that's why they go out of ... | 10 | 2009-06-12T00:14:16Z | [
"python",
"django",
"date",
"time",
"strptime"
] |
Django : Timestamp string custom field | 984,460 | <p>I'm trying to create a custom timestamp field.</p>
<pre><code>class TimestampKey(models.CharField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
import time
kwargs['unique'] = True
kwargs['max_length'] = 20
kwargs['auto_created'] = True
kwargs['editable... | 0 | 2009-06-12T00:17:49Z | 984,661 | <p>The <code>get_db_prep_value</code> method only prepares a value for the database, but doesn't send the prepared value back to the Python object in any way. For that you would need the <code>pre_save</code> method, I think.</p>
<p>Fortunately, there's already an "auto_now" option on DateField and DateTimeField that... | 1 | 2009-06-12T01:42:26Z | [
"python",
"django",
"django-models"
] |
Django : Timestamp string custom field | 984,460 | <p>I'm trying to create a custom timestamp field.</p>
<pre><code>class TimestampKey(models.CharField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
import time
kwargs['unique'] = True
kwargs['max_length'] = 20
kwargs['auto_created'] = True
kwargs['editable... | 0 | 2009-06-12T00:17:49Z | 987,336 | <p>Is it wise to use a timestamp as your primary key? If your database uses ISO 8601 or really any time format in which second is the smallest time interval... Well, anyway, my point is that you have no guarantee, especially if this is going to be a web-facing application that two entries are going to resolve within ... | 3 | 2009-06-12T15:33:41Z | [
"python",
"django",
"django-models"
] |
PythonCard - Can I launch a CustomDialog Stand Alone? | 984,506 | <p>I have a CustomDialog I made to let the user configure settings. Normally I want this to be launched from a menu item within the main application which works fine.</p>
<p>But during the install, I want to launch just the dialog to let the user configure the settings. Is there a way I can have both?</p>
| 0 | 2009-06-12T00:38:13Z | 984,686 | <p>With a little supporting code you can -- see e.g. <a href="http://www.freenet.org.nz/python/PythonCard/wizard.py" rel="nofollow">wizard.py</a> which launches an almost-stand-alone custom dialog subclass "wizard".</p>
| 1 | 2009-06-12T01:54:42Z | [
"python",
"user-interface",
"wxpython",
"pythoncard",
"pycard"
] |
Correct way of handling exceptions in Python? | 984,526 | <p>I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.</p>
<p>I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am ... | 25 | 2009-06-12T00:45:51Z | 984,552 | <p>Why not one big try: block? This way, if any exception is caught, you'll go all the way to the except. And as long as all the exceptions for the different steps are different, you can always tell which part it was that fired the exception.</p>
| 0 | 2009-06-12T00:57:05Z | [
"python",
"exception"
] |
Correct way of handling exceptions in Python? | 984,526 | <p>I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.</p>
<p>I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am ... | 25 | 2009-06-12T00:45:51Z | 984,575 | <p>In general, you want to use as few try blocks as possible, distinguishing failure conditions by the kinds of exceptions they throw. For instance, here's my refactoring of the code you posted:</p>
<pre><code>try:
server = smtplib.SMTP(host)
server.login(username, password) # Only runs if the previous line d... | 12 | 2009-06-12T01:09:53Z | [
"python",
"exception"
] |
Correct way of handling exceptions in Python? | 984,526 | <p>I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.</p>
<p>I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am ... | 25 | 2009-06-12T00:45:51Z | 984,589 | <p>I like David's answer but if you are stuck on the server exceptions you can also check for server if is None or states. I flattened out the method a bit bit it is still a but unpythonic looking but more readable in the logic at the bottom.</p>
<pre><code>server = None
def server_obtained(host):
try:
... | 0 | 2009-06-12T01:16:43Z | [
"python",
"exception"
] |
Correct way of handling exceptions in Python? | 984,526 | <p>I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.</p>
<p>I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am ... | 25 | 2009-06-12T00:45:51Z | 984,652 | <p>Just using one try-block is the way to go. This is exactly what they
are designed for: only execute the next statement if the previous
statement did not throw an exception. As for the resource clean-ups,
maybe you can check the resource if it needs to be cleaned up
(e.g. myfile.is_open(), ...) This does add some ext... | 1 | 2009-06-12T01:39:05Z | [
"python",
"exception"
] |
Correct way of handling exceptions in Python? | 984,526 | <p>I have searched for other posts, as I felt this is a rather common problem, but all other Python exception questions I have found didn't reflect my problem.</p>
<p>I will try to be as specific here as I can, so I will give a direct example. And pleeeeease do not post any workarounds for this specific problem. I am ... | 25 | 2009-06-12T00:45:51Z | 984,682 | <p>Instead of using the try/except's else block, you could simply return when it errors:</p>
<pre><code>def send_message(addr, to, msg):
## Connect to host
try:
server = smtplib.SMTP(host) #can throw an exception
except smtplib.socket.gaierror:
return False
## Login
try:
se... | 23 | 2009-06-12T01:53:55Z | [
"python",
"exception"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.