title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
Programmatic mail-merge style data injection into existing Excel spreadsheets? | 376,221 | <p>I'd like to automate data entry into Excel spreadsheets. User data will exist on a web site, and when the user requests it, that data will need to be injected into an Excel spreadsheet. The complication is that the format of the Excel spreadsheet can vary significantly between users - it'll be user defined.</p>
<p>... | 3 | 2008-12-17T22:12:22Z | 376,252 | <p>You might want to look into a third party library called <a href="http://www.aspose.com/categories/file-format-components/aspose.cells-for-.net-and-java/default.aspx" rel="nofollow">Aspose.Cells</a>. It is available for Java and .Net and allows very granular control of Excel documents. The great thing about this l... | 0 | 2008-12-17T22:21:24Z | [
"java",
"python",
"excel"
] |
Programmatic mail-merge style data injection into existing Excel spreadsheets? | 376,221 | <p>I'd like to automate data entry into Excel spreadsheets. User data will exist on a web site, and when the user requests it, that data will need to be injected into an Excel spreadsheet. The complication is that the format of the Excel spreadsheet can vary significantly between users - it'll be user defined.</p>
<p>... | 3 | 2008-12-17T22:12:22Z | 376,396 | <p>WinHttpRequest (<a href="http://msdn.microsoft.com/en-us/library/aa384045(VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa384045(VS.85).aspx</a>) may suit, you can use the document and so forth. Here is a snippet from the link. </p>
<pre><code>Dim HttpReq As Object
' Create the WinHTTPRequest... | 1 | 2008-12-17T23:08:58Z | [
"java",
"python",
"excel"
] |
Programmatic mail-merge style data injection into existing Excel spreadsheets? | 376,221 | <p>I'd like to automate data entry into Excel spreadsheets. User data will exist on a web site, and when the user requests it, that data will need to be injected into an Excel spreadsheet. The complication is that the format of the Excel spreadsheet can vary significantly between users - it'll be user defined.</p>
<p>... | 3 | 2008-12-17T22:12:22Z | 376,772 | <p>jXLS is maybe an option. You define an XLS file as a template and then you merge your data. </p>
<p>Quick overview <a href="http://www.rgagnon.com/javadetails/java-0516.html" rel="nofollow">here</a></p>
<p><a href="http://jxls.sourceforge.net/" rel="nofollow">http://jxls.sourceforge.net/</a></p>
| 2 | 2008-12-18T02:52:03Z | [
"java",
"python",
"excel"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,490 | <p>I use substitution wherever I can. I only use concatenation if I'm building a string up in say a for-loop.</p>
| 0 | 2008-12-17T23:56:12Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,508 | <p>Concatenation is (significantly) faster according to my machine. But stylistically, I'm willing to pay the price of substitution if performance is not critical. Well, and if I need formatting, there's no need to even ask the question... there's no option but to use interpolation/templating.</p>
<pre><code>>>&... | 42 | 2008-12-18T00:07:44Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,512 | <p>What you want to concatenate/interpolate and how you want to format the result should drive your decision. </p>
<ul>
<li><p>String interpolation allows you to easily add formatting. In fact, your string interpolation version doesn't do the same thing as your concatenation version; it actually adds an extra forward... | 7 | 2008-12-18T00:10:09Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,523 | <p>"As the string concatenation has seen large boosts in performance..."</p>
<p>If performance matters, this is good to know.</p>
<p>However, performance problems I've seen have never come down to string operations. I've generally gotten in trouble with I/O, sorting and O(<em>n</em><sup>2</sup>) operations being th... | 11 | 2008-12-18T00:18:13Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,530 | <p>Don't forget about named substitution:</p>
<pre><code>def so_question_uri_namedsub(q_num):
return "%(domain)s%(questions)s/%(q_num)d" % locals()
</code></pre>
| 27 | 2008-12-18T00:22:59Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,593 | <p>Remember, stylistic decisions <em>are</em> practical decisions, if you ever plan on maintaining or debugging your code :-) There's a famous quote from Knuth (possibly quoting Hoare?): "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."</p>
<p>As l... | 3 | 2008-12-18T01:04:35Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 376,924 | <p><strong>Be wary of concatenating strings in a loop!</strong> The cost of string concatenation is proportional to the length of the result. Looping leads you straight to the land of N-squared. Some languages will optimize concatenation to the most recently allocated string, but it's risky to count on the compiler ... | 10 | 2008-12-18T04:40:07Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 7,129,548 | <p>Actually the correct thing to do, in this case (building paths) is to use <code>os.path.join</code>. Not string concatenation or interpolation</p>
| 0 | 2011-08-20T03:56:37Z | [
"python",
"string",
"string-concatenation"
] |
String concatenation vs. string substitution in Python | 376,461 | <p>In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one?</p>
<p>For a concrete example, how should one handle construction of flexible ... | 81 | 2008-12-17T23:39:39Z | 16,514,440 | <p>I was just testing the speed of different string concatenation/substitution methods out of curiosity. A google search on the subject brought me here. I thought I would post my test results in the hope that it might help someone decide.</p>
<pre><code> import timeit
def percent_():
return "test %s... | 5 | 2013-05-13T03:27:35Z | [
"python",
"string",
"string-concatenation"
] |
IntelliJ Python plug-in | 376,765 | <p>I've been using IntelliJ IDEA at the day job for Java development for a few weeks now. I'm really impressed with it and I'm looking to extend it for other programming languages that I tinker with, starting with Python. I found this plug-in, <a href="https://pythonid.dev.java.net/" rel="nofollow">pythonid</a>. I fig... | 3 | 2008-12-18T02:48:26Z | 376,935 | <p>I've tried Pythonid before and found it very limited. There's a <a href="http://plugins.intellij.net/plugin/?id=631" rel="nofollow">new Python plugin</a> from JetBrains, the people that make IDEA, which looks pretty nice, though it's <a href="http://www.jetbrains.net/confluence/display/PYH/Release+Notes" rel="nofoll... | 4 | 2008-12-18T04:45:44Z | [
"java",
"python",
"ide"
] |
IntelliJ Python plug-in | 376,765 | <p>I've been using IntelliJ IDEA at the day job for Java development for a few weeks now. I'm really impressed with it and I'm looking to extend it for other programming languages that I tinker with, starting with Python. I found this plug-in, <a href="https://pythonid.dev.java.net/" rel="nofollow">pythonid</a>. I fig... | 3 | 2008-12-18T02:48:26Z | 2,292,537 | <p>There is currently an EAP for the Intellij Python IDE (PyCharm): <a href="http://confluence.jetbrains.net/display/PYH/JetBrains+PyCharm+Preview">here</a></p>
| 5 | 2010-02-18T22:00:23Z | [
"java",
"python",
"ide"
] |
need help-variable creation in Python (continuation) | 377,000 | <p><a href="http://stackoverflow.com/questions/374572/need-help-variable-creation-in-python#374604">That</a> was helpful kgiannakakis.
I'm facing a problem as below:</p>
<pre><code>a = ['zbc','2.3']
for i in range(0,5):
exec('E%d=%s' %(i,a[i]))
</code></pre>
<p>This results in:</p>
<pre>
Traceback (most recent c... | -3 | 2008-12-18T05:43:39Z | 377,015 | <p>Okay. this code is very weird.</p>
<p>As a one liner like this, it's not syntactically correct, but I suspect you're missing line breaks for some reason. But then it becomes</p>
<pre><code>a = ['zbc','2.3']
for i in range(0,5):
exec('E%d=%s' %(i,a[i]))
</code></pre>
<p>But that will result in an index error... | 0 | 2008-12-18T05:54:23Z | [
"python",
"variables"
] |
need help-variable creation in Python (continuation) | 377,000 | <p><a href="http://stackoverflow.com/questions/374572/need-help-variable-creation-in-python#374604">That</a> was helpful kgiannakakis.
I'm facing a problem as below:</p>
<pre><code>a = ['zbc','2.3']
for i in range(0,5):
exec('E%d=%s' %(i,a[i]))
</code></pre>
<p>This results in:</p>
<pre>
Traceback (most recent c... | -3 | 2008-12-18T05:43:39Z | 377,079 | <p>It looks like the code you're generating expands to:</p>
<pre><code>E0=zbc
E1=2.3
</code></pre>
<p>At the next iteration through the loop, you'll get an IndexError exception because <code>a</code> is only two elements long.</p>
<p>So given the above, you are trying to assign the value of <code>zbc</code> to <code... | 2 | 2008-12-18T06:55:56Z | [
"python",
"variables"
] |
need help-variable creation in Python (continuation) | 377,000 | <p><a href="http://stackoverflow.com/questions/374572/need-help-variable-creation-in-python#374604">That</a> was helpful kgiannakakis.
I'm facing a problem as below:</p>
<pre><code>a = ['zbc','2.3']
for i in range(0,5):
exec('E%d=%s' %(i,a[i]))
</code></pre>
<p>This results in:</p>
<pre>
Traceback (most recent c... | -3 | 2008-12-18T05:43:39Z | 378,973 | <p>It seems you are trying to use the solution marked in <a href="http://stackoverflow.com/questions/374572/need-help-variable-creation-in-python">this</a> question.</p>
<p>If your goal is access values in a loop, you should just use a list. This weird concept of variable names with numbers in them is not one that s... | 0 | 2008-12-18T19:42:31Z | [
"python",
"variables"
] |
need help-variable creation in Python (continuation) | 377,000 | <p><a href="http://stackoverflow.com/questions/374572/need-help-variable-creation-in-python#374604">That</a> was helpful kgiannakakis.
I'm facing a problem as below:</p>
<pre><code>a = ['zbc','2.3']
for i in range(0,5):
exec('E%d=%s' %(i,a[i]))
</code></pre>
<p>This results in:</p>
<pre>
Traceback (most recent c... | -3 | 2008-12-18T05:43:39Z | 381,712 | <p>Just keep in mind that 'exec' executes whatever string you pass in to it as if you typed it in your .py file or the interpreter.</p>
<p>When debugging exec() related code, it's helpful to log whatever you're about to 'exec' when you run into trouble, if you did that you'd easily have noticed that E0 wasn't being as... | 0 | 2008-12-19T18:03:18Z | [
"python",
"variables"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 377,027 | <p>you can tell if a file exists with the os module. an executable in particular seems quite unportable considering lots of things are executable on nix that aren't on windows and vice versa.</p>
| 0 | 2008-12-18T06:04:51Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 377,028 | <p>Easiest way I can think of: </p>
<pre><code>def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.env... | 233 | 2008-12-18T06:05:29Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 377,029 | <p>It would seem the <em>obvious</em> choice is "which", parsing the results via popen, but you could simulate it otherwise using the os class. In pseudopython, it would look like this:</p>
<pre><code>for each element r in path:
for each file f in directory p:
if f is executable:
return True
<... | 0 | 2008-12-18T06:06:55Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 377,032 | <p>See <a href="http://docs.python.org/library/os.path.html#module-os.path" rel="nofollow">os.path</a> module for some useful functions on pathnames. To check if an existing file is executable, use <a href="http://docs.python.org/library/os.html#os.access" rel="nofollow">os.access(path, mode)</a>, with the os.X_OK mode... | 7 | 2008-12-18T06:08:00Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 377,590 | <p>So basically you want to find a file in mounted filesystem (not necessarily in PATH directories only) and check if it is executable. This translates to following plan: </p>
<ul>
<li>enumerate all files in locally mounted filesystems</li>
<li>match results with name pattern</li>
<li>for each file found check if it i... | 0 | 2008-12-18T11:34:02Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 379,535 | <p>Just remember to specify the file extension on windows. Otherwise, you have to write a much complicated <code>is_exe</code> for windows using <code>PATHEXT</code> environment variable. You may just want to use <a href="http://www.raboof.com/projects/findpath/">FindPath</a>.</p>
<p>OTOH, why are you even bothering t... | 10 | 2008-12-18T22:25:15Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 380,151 | <p>There is a <a href="http://svn.python.org/view/python/trunk/Tools/scripts/which.py?view=markup" rel="nofollow">which.py</a> script in a standard Python distribution (e.g. on Windows <code>'\PythonXX\Tools\Scripts\which.py'</code>). </p>
<p>EDIT: <code>which.py</code> depends on <code>ls</code> therefore it is not c... | 0 | 2008-12-19T05:11:05Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 797,187 | <p>If you have <code>bash</code> and a function <code>sh</code> (<code>subprocess.Popen( ... ).communicate()</code> ),<br />
use the <code>bash</code> builtin <code>type</code>:</p>
<pre><code>type -p ls => /bin/ls
type -p nonesuch => ""
</code></pre>
| 1 | 2009-04-28T10:13:32Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 1,166,486 | <p>None of previous examples do work on all platforms. Usually they fail to work on Windows because you can execute without the file extension <em>and</em> that you can register new extension. For example on Windows if python is well installed it's enough to execute 'file.py' and it will work.</p>
<p>The only valid an... | 0 | 2009-07-22T16:25:00Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 1,328,190 | <p>An important question is "<strong>Why</strong> do you need to test if executable exist?" Maybe you don't? ;-) </p>
<p>Recently I needed this functionality to launch viewer for PNG file. I wanted to iterate over some predefined viewers and run the first that exists. Fortunately, I came across <code>os.startfile</cod... | 1 | 2009-08-25T13:10:52Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 7,740,009 | <p>I found something in StackOverflow that solved the problem for me. This works provided the executable has an option (like --help or --version) that outputs something and returns an exit status of zero. See <a href="http://stackoverflow.com/questions/699325/suppress-output-in-python-calls-to-executables/699374#699374... | 2 | 2011-10-12T12:23:21Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 11,069,822 | <h2>For *nix platforms (Linux and OS X)</h2>
<p>This seems to be working for me:</p>
<p><strong>Edited</strong> to work on Linux, thanks to <a href="http://stackoverflow.com/users/624066/mestrelion">Mestreion</a></p>
<pre><code>def cmd_exists(cmd):
return subprocess.call("type " + cmd, shell=True,
stdou... | 10 | 2012-06-17T08:01:46Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 11,664,950 | <p>On the basis that it is <a href="http://docs.python.org/glossary.html#term-eafp" rel="nofollow">easier to ask forgiveness than permission</a> I would just try to use it and catch the error (OSError in this case - I checked for file does not exist and file is not executable and they both give OSError).</p>
<p>It hel... | 4 | 2012-07-26T08:06:30Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 12,611,523 | <p>I know this is an ancient question, but you can use <code>distutils.spawn.find_executable</code>. This has been <a href="http://docs.python.org/release/2.4/dist/module-distutils.spawn.html">documented since python 2.4</a> and has existed since python 1.6. Also, Python 3.3 now offers <a href="http://docs.python.or... | 180 | 2012-09-26T22:40:30Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 13,936,916 | <p>Python 3.3 now offers <a href="http://docs.python.org/dev/library/shutil.html#shutil.which">shutil.which()</a>.</p>
| 76 | 2012-12-18T16:05:19Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 18,547,150 | <p>I know that I'm being a bit of a necromancer here, but I stumbled across this question and the accepted solution didn't work for me for all cases Thought it might be useful to submit anyway. In particular, the "executable" mode detection, and the requirement of supplying the file extension. Furthermore, both python3... | 1 | 2013-08-31T10:30:02Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 19,351,174 | <p>This seems simple enough and works both in python 2 and 3</p>
<pre><code>try: subprocess.check_output('which executable',shell=True)
except: sys.exit('ERROR: executable not found')
</code></pre>
| 1 | 2013-10-13T22:43:51Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 26,786,351 | <p>You can try the external lib called "sh" (<a href="http://amoffat.github.io/sh/" rel="nofollow">http://amoffat.github.io/sh/</a>).</p>
<pre><code>import sh
print sh.which('ls') # prints '/bin/ls' depending on your setup
print sh.which('xxx') # prints None
</code></pre>
| 0 | 2014-11-06T18:05:55Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 26,901,254 | <p>The best example should be the python bulit-in module shutil.which() in Python 3. The link is <a href="https://hg.python.org/cpython/file/default/Lib/shutil.py" rel="nofollow">https://hg.python.org/cpython/file/default/Lib/shutil.py</a> </p>
| 3 | 2014-11-13T04:17:29Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 28,909,933 | <h3>For python 3.2 and earlier:</h3>
<pre><code>my_command = 'ls'
any(os.access(os.path.join(path, my_command), os.X_OK) for path in os.environ["PATH"].split(os.pathsep))
</code></pre>
<p>This is a one-liner of <a href="http://stackoverflow.com/a/377028/1695680">Jay's Answer</a>, Also here as a lambda func:</p>
<pre... | 6 | 2015-03-07T00:28:11Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 29,690,078 | <p>Added windows support</p>
<pre><code>def which(program):
path_ext = [""];
ext_list = None
if sys.platform == "win32":
ext_list = [ext.lower() for ext in os.environ["PATHEXT"].split(";")]
def is_exe(fpath):
exe = os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# search f... | 1 | 2015-04-17T04:06:02Z | [
"python",
"path"
] |
Test if executable exists in Python? | 377,017 | <p>In Python, is there a portable and simple way to test if an executable program exists?</p>
<p>By simple I mean something like the <code>which</code> command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with <code>Popen</code> & al and see if it fa... | 181 | 2008-12-18T05:55:36Z | 34,075,990 | <p>Using the python fabric library:</p>
<pre><code>from fabric.api import *
def test_cli_exists():
"""
Make sure executable exists on the system path.
"""
with settings(warn_only=True):
which = local('which command', capture=True)
if not which:
print "command does not exist"
... | 0 | 2015-12-03T20:58:33Z | [
"python",
"path"
] |
How to set and preserve minimal width? | 377,204 | <p>I use some wx.ListCtrl classes in wx.LC_REPORT mode, augmented with ListCtrlAutoWidthMixin.</p>
<p>The problem is: When user double clicks the column divider (to auto resize column), column width is set to match the width of contents. This is done by the wx library and resizes column to just few pixels when the con... | 4 | 2008-12-18T08:28:49Z | 380,378 | <p>Ok, after some struggle I got working workaround for that. It is ugly from design point of view, but works well enough for me.</p>
<p>That's how it works:</p>
<ol>
<li><p>Store the initial width of column.</p>
<pre><code>self.SetColumnWidth(colNum, wx.LIST_AUTOSIZE_USEHEADER)
self.__columnWidth[colNum] = self.Ge... | 1 | 2008-12-19T08:38:07Z | [
"python",
"wxpython",
"wxwidgets",
"listctrl"
] |
How to set and preserve minimal width? | 377,204 | <p>I use some wx.ListCtrl classes in wx.LC_REPORT mode, augmented with ListCtrlAutoWidthMixin.</p>
<p>The problem is: When user double clicks the column divider (to auto resize column), column width is set to match the width of contents. This is done by the wx library and resizes column to just few pixels when the con... | 4 | 2008-12-18T08:28:49Z | 380,873 | <p>Honestly, I've stopped using the native wx.ListCtrl in favor of using <a href="http://objectlistview.sourceforge.net/python/" rel="nofollow">ObjectListView</a>. There is a little bit of a learning curve, but there are lots of examples. <a href="http://objectlistview.sourceforge.net/python/recipes.html#recipe-colum... | 3 | 2008-12-19T12:44:05Z | [
"python",
"wxpython",
"wxwidgets",
"listctrl"
] |
Stackless python and multicores? | 377,254 | <p>So, I'm toying around with <a href="http://www.stackless.com/">Stackless Python</a> and a question popped up in my head, maybe this is "assumed" or "common" knowledge, but I couldn't find it actually written anywhere on the <a href="http://www.stackless.com/">stackless site</a>.</p>
<p>Does <a href="http://www.stac... | 23 | 2008-12-18T08:51:22Z | 377,366 | <p>Stackless python does <em>not</em> make use of any kind of multi-core environment it runs on.<br>
This is a common misconception about Stackless, as it allows the programmer to take advantage of thread-based programming. For many people these two are closely intertwined, but are, in fact two separate things.</p>
<p... | 38 | 2008-12-18T09:43:07Z | [
"python",
"multithreading",
"concurrency",
"multicore",
"python-stackless"
] |
OOP and MVC programming style | 377,337 | <p>I'm writing some data analysis software and decided to use such approach:<br /><br /></p>
<p>epn:<br />
model/data.py <- Model definition<br />
model/reader.py <- How to read data into model<br />
view/gui.py <- main gui frame (wx)<br />
view/dialogs.py <- different dialogs (wx)<br />
epn.py <- contr... | 1 | 2008-12-18T09:32:57Z | 377,567 | <p>Example 1: "better (shorter, encapsulated, more error-proof)"</p>
<p>Not really. </p>
<ul>
<li><p>The example 1 function call is no shorter than example 2; you have to set the instance variables before calling the function instead of passing the values as arguments. It's the same code.</p></li>
<li><p>The exampl... | 3 | 2008-12-18T11:26:33Z | [
"python",
"model-view-controller",
"oop",
"coding-style"
] |
Code not waiting for class initialization! | 377,362 | <p>I have a block of code that basically intializes several classes, but they are placed in a sequential order, as later ones reference early ones.</p>
<p>For some reason the last one initializes before the first one...it seems to me there is some sort of threading going on. What I need to know is how can I stop it fr... | 1 | 2008-12-18T09:41:52Z | 377,382 | <p>Python upto 3.0 has a global lock, so everything is running in a single thread and in sequence.</p>
<p>My guess is that some side effect initializes the last class from a different place than you expect. Throw an exception in <code>__init__</code> of that last class to see where it gets called.</p>
| 0 | 2008-12-18T09:47:51Z | [
"python"
] |
Code not waiting for class initialization! | 377,362 | <p>I have a block of code that basically intializes several classes, but they are placed in a sequential order, as later ones reference early ones.</p>
<p>For some reason the last one initializes before the first one...it seems to me there is some sort of threading going on. What I need to know is how can I stop it fr... | 1 | 2008-12-18T09:41:52Z | 377,418 | <p>Spaces vs. Tabs issue...ugh. >.></p>
<p>Well, atleast it works now. I admit that I kind of miss the braces from C instead of forced-indentation. It's quite handy as a prototyping language though. Maybe I'll grow to love it more when I get a better grasp of it.</p>
| 0 | 2008-12-18T10:03:17Z | [
"python"
] |
How do I get my python program to sleep for 50 milliseconds? | 377,454 | <p>How do I get my python program to sleep for 50 milliseconds?</p>
| 231 | 2008-12-18T10:20:24Z | 377,460 | <pre><code>import time
time.sleep (50.0 / 1000.0);
</code></pre>
| 28 | 2008-12-18T10:23:03Z | [
"python",
"timer",
"sleep"
] |
How do I get my python program to sleep for 50 milliseconds? | 377,454 | <p>How do I get my python program to sleep for 50 milliseconds?</p>
| 231 | 2008-12-18T10:20:24Z | 377,461 | <p>A quick look at google tells me: <a href="http://docs.python.org/library/time.html" rel="nofollow">time.sleep</a> - </p>
<p><a href="http://docs.python.org/library/time.html" rel="nofollow">http://docs.python.org/library/time.html</a></p>
| 4 | 2008-12-18T10:23:19Z | [
"python",
"timer",
"sleep"
] |
How do I get my python program to sleep for 50 milliseconds? | 377,454 | <p>How do I get my python program to sleep for 50 milliseconds?</p>
| 231 | 2008-12-18T10:20:24Z | 377,463 | <pre><code>Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import sleep
>>> sleep(0.05)
>>>
</code></pre>
| 327 | 2008-12-18T10:23:50Z | [
"python",
"timer",
"sleep"
] |
How do I get my python program to sleep for 50 milliseconds? | 377,454 | <p>How do I get my python program to sleep for 50 milliseconds?</p>
| 231 | 2008-12-18T10:20:24Z | 377,546 | <p>Note that if you rely on sleep taking <em>exactly</em> 50ms, you won't get that. It will just be about it.</p>
| 36 | 2008-12-18T11:16:30Z | [
"python",
"timer",
"sleep"
] |
Python, Regular Expression Postcode search | 378,157 | <p>I am trying to use regular expressions to find a UK postcode within a string.</p>
<p>I have got the regular expression working inside RegexBuddy, see below:</p>
<pre><code>\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b
</code></pre>
<p>I have a bunch of addresses and want to grab the postcode from them, ex... | 2 | 2008-12-18T15:19:34Z | 378,178 | <p>I'd suggest looking at this <a href="http://en.wikipedia.org/wiki/UK_postcodes#Validation" rel="nofollow">UK postcode wikipedia</a> article.</p>
| 1 | 2008-12-18T15:26:07Z | [
"python",
"regex",
"postal-code"
] |
Python, Regular Expression Postcode search | 378,157 | <p>I am trying to use regular expressions to find a UK postcode within a string.</p>
<p>I have got the regular expression working inside RegexBuddy, see below:</p>
<pre><code>\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b
</code></pre>
<p>I have a bunch of addresses and want to grab the postcode from them, ex... | 2 | 2008-12-18T15:19:34Z | 378,222 | <p>Try</p>
<pre><code>import re
re.findall("[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}", x)
</code></pre>
<p>You don't need the \b. </p>
| 0 | 2008-12-18T15:40:05Z | [
"python",
"regex",
"postal-code"
] |
Python, Regular Expression Postcode search | 378,157 | <p>I am trying to use regular expressions to find a UK postcode within a string.</p>
<p>I have got the regular expression working inside RegexBuddy, see below:</p>
<pre><code>\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b
</code></pre>
<p>I have a bunch of addresses and want to grab the postcode from them, ex... | 2 | 2008-12-18T15:19:34Z | 378,228 | <p>repeating your address 3 times with postcode PA23 6NH, PA2 6NH and PA2Q 6NH as test for you pattern and using the regex from wikipedia against yours, the code is..</p>
<pre><code>import re
s="123 Some Road Name\nTown, City\nCounty\nPA23 6NH\n123 Some Road Name\nTown, City"\
"County\nPA2 6NH\n123 Some Road Name... | 3 | 2008-12-18T15:42:50Z | [
"python",
"regex",
"postal-code"
] |
Python, Regular Expression Postcode search | 378,157 | <p>I am trying to use regular expressions to find a UK postcode within a string.</p>
<p>I have got the regular expression working inside RegexBuddy, see below:</p>
<pre><code>\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b
</code></pre>
<p>I have a bunch of addresses and want to grab the postcode from them, ex... | 2 | 2008-12-18T15:19:34Z | 378,246 | <pre><code>#!/usr/bin/env python
import re
ADDRESS="""123 Some Road Name
Town, City
County
PA23 6NH"""
reobj = re.compile(r'(\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\b)')
matchobj = reobj.search(ADDRESS)
if matchobj:
print matchobj.group(1)
</code></pre>
<p>Example output: </p>
<pre><code>[user@host... | 0 | 2008-12-18T15:45:41Z | [
"python",
"regex",
"postal-code"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 378,837 | <p>This would be the easiest </p>
<pre><code>from django.contrib.auth import models
group = models.Group.objects.get(name='blogger')
users = group.user_set.all()
</code></pre>
| 33 | 2008-12-18T19:03:38Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 378,867 | <p>I think for group permissions, permissions are stored against groups, and then users have groups linked to them. So you can just resolve the user - groups relation.</p>
<p>e.g. </p>
<pre><code>518$ python manage.py shell
(InteractiveConsole)
>>> from django.contrib.auth.models import User, Group
>>... | 14 | 2008-12-18T19:11:08Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 386,375 | <p>Groups are many-to-many with Users (you see, nothing unusual, just Django models...), so the answer by cms is right. Plus this works both ways: having a group, you can list all users in it by inspecting <code>user_set</code> attribute.</p>
| 1 | 2008-12-22T14:34:18Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 457,089 | <p>Try this:</p>
<pre><code>User.objects.filter(groups__permissions = Permission.objects.get(codename='blogger'))
</code></pre>
| 0 | 2009-01-19T10:02:34Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 992,329 | <p>If you want to get list of users by permission, look at this variant:</p>
<pre><code>from django.contrib.auth.models import User, Permission
from django.db.models import Q
perm = Permission.objects.get(codename='blogger')
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinc... | 48 | 2009-06-14T07:02:33Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 18,638,652 | <p>Based on @Glader's answer, this function wraps it up in a single query, and has been modified to algo get the superusers (as by definition, they have all perms):</p>
<pre><code>from django.contrib.auth.models import User
from django.db.models import Q
def users_with_perm(perm_name):
return User.objects.filter(... | 5 | 2013-09-05T14:11:06Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 34,778,196 | <p>Based on @Augusto's answer, I did the following with a model manager and using the authtools library. This is in <code>querysets.py</code>:</p>
<pre><code>from django.db.models import Q
from authtools.models import UserManager as AuthUserManager
class UserManager(AuthUserManager):
def get_users_with_perm(self,... | 0 | 2016-01-13T22:21:50Z | [
"python",
"django",
"dictionary",
"permissions"
] |
How to get a list of all users with a specific permission group in Django | 378,303 | <p>I want to get a list of all Django auth user with a specific permission group, something like this:</p>
<pre><code>user_dict = {
'queryset': User.objects.filter(permisson='blogger')
}
</code></pre>
<p>I cannot find out how to do this. How are the permissions groups saved in the user model?</p>
| 36 | 2008-12-18T16:00:49Z | 37,722,571 | <p>Do not forget that specifying permission codename is not enough because different apps may reuse the same codename. One needs to get permission object to query Users correctly:</p>
<pre><code>def get_permission_object(permission_str):
app_label, codename = permission_str.split('.')
return Permission.objects... | 0 | 2016-06-09T09:53:24Z | [
"python",
"django",
"dictionary",
"permissions"
] |
Problem sub-classing BaseException in Python | 378,493 | <p>I wanted to create my own Python exception class, like this:</p>
<pre><code>class MyException(BaseException):
def __init__(self, errno, address):
if errno == 10048:
mess = str(address) + ' is already in use'
else:
mess = 'Unable to open ' + str(address)
BaseExcept... | 5 | 2008-12-18T17:00:56Z | 378,514 | <p>You have to call the method of the base class with the instance as the first argument:</p>
<pre><code>BaseException.__init__(self, mess)
</code></pre>
<p>To quote from the <a href="http://docs.python.org/tutorial/classes.html#inheritance">tutorial</a>:</p>
<blockquote>
<p>An overriding method in a derived class... | 8 | 2008-12-18T17:11:05Z | [
"python",
"exception",
"inheritance"
] |
Problem sub-classing BaseException in Python | 378,493 | <p>I wanted to create my own Python exception class, like this:</p>
<pre><code>class MyException(BaseException):
def __init__(self, errno, address):
if errno == 10048:
mess = str(address) + ' is already in use'
else:
mess = 'Unable to open ' + str(address)
BaseExcept... | 5 | 2008-12-18T17:00:56Z | 378,518 | <p>hop has it right.</p>
<p>As a side note, you really should not subclass BaseException, you should be subclassing Exception instead. (Unless you really really know what you're doing)</p>
| 6 | 2008-12-18T17:12:40Z | [
"python",
"exception",
"inheritance"
] |
Idiomatic asynchronous design | 378,564 | <p>Are there any sorts of useful idioms I can make use of when writing an API that is asynchronous? I would like to standardize on something as I seem to be using a few different styles throughout. It seems hard to make asynchronous code simple; I suppose this is because asynchronous operations are anything but.</p>
<... | 8 | 2008-12-18T17:27:35Z | 378,613 | <p>You may want to look at <a href="http://twistedmatrix.com/trac/" rel="nofollow">Python Twisted</a>. It is a nice <a href="http://en.wikipedia.org/wiki/Reactor_pattern" rel="nofollow">Reactor</a> based API that supports <a href="http://twistedmatrix.com/projects/core/documentation/howto/async.html" rel="nofollow">asy... | 4 | 2008-12-18T17:43:31Z | [
"python",
"design",
"asynchronous"
] |
Idiomatic asynchronous design | 378,564 | <p>Are there any sorts of useful idioms I can make use of when writing an API that is asynchronous? I would like to standardize on something as I seem to be using a few different styles throughout. It seems hard to make asynchronous code simple; I suppose this is because asynchronous operations are anything but.</p>
<... | 8 | 2008-12-18T17:27:35Z | 378,836 | <p>Also have a look at the Asynchronous Completion Token and ActiveObject patterns.</p>
| 2 | 2008-12-18T19:02:46Z | [
"python",
"design",
"asynchronous"
] |
Idiomatic asynchronous design | 378,564 | <p>Are there any sorts of useful idioms I can make use of when writing an API that is asynchronous? I would like to standardize on something as I seem to be using a few different styles throughout. It seems hard to make asynchronous code simple; I suppose this is because asynchronous operations are anything but.</p>
<... | 8 | 2008-12-18T17:27:35Z | 379,525 | <p>This sounds like the <strong>Observer</strong> design pattern. <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">link</a>.</p>
<p>Your client object is an <strong>Observer</strong>. Your API belongs to an object that's <strong>Observable</strong>.</p>
<p>Each client (in Java parlance) implem... | 2 | 2008-12-18T22:22:09Z | [
"python",
"design",
"asynchronous"
] |
How do I create a Python class in C? | 378,773 | <p>I have a legacy C library that creates a tree of objects. I would like to convert the tree into a pre-existing Python class. How do I create the PyObject for that class?</p>
| 5 | 2008-12-18T18:39:59Z | 378,795 | <p>Take a look at generating your Python bindings by using a tool such as <a href="https://launchpad.net/pybindgen/" rel="nofollow">pybindgen</a>. These guys are trying to make a superior binding generator, they talk about the shortcomings of other tools (e.g. SWIG) on their front page.</p>
| 2 | 2008-12-18T18:45:45Z | [
"python",
"c"
] |
How do I create a Python class in C? | 378,773 | <p>I have a legacy C library that creates a tree of objects. I would like to convert the tree into a pre-existing Python class. How do I create the PyObject for that class?</p>
| 5 | 2008-12-18T18:39:59Z | 378,894 | <p>I've had success using <a href="http://robin.python-hosting.com/" rel="nofollow">Robin</a> in these scenarios.</p>
| 1 | 2008-12-18T19:19:36Z | [
"python",
"c"
] |
How do I create a Python class in C? | 378,773 | <p>I have a legacy C library that creates a tree of objects. I would like to convert the tree into a pre-existing Python class. How do I create the PyObject for that class?</p>
| 5 | 2008-12-18T18:39:59Z | 393,486 | <p><a href="http://www.cython.org/" rel="nofollow">Cython</a> is capable of doing this. It's a semi-fork of Pyrex, and it can wrap existing data structures and expose them to Python. In fact, this is one of the <a href="http://docs.cython.org/docs/external_C_code.html" rel="nofollow">sections in the user guide</a>. ... | 4 | 2008-12-26T04:46:59Z | [
"python",
"c"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 378,822 | <p>Two things spring immediately to mind. </p>
<ol>
<li>Make sure you are outputting the <code>Content-Type: text/html</code> header</li>
<li>Make sure you are adding two newlines ("\n") after the headers before you output "Hello, world" or whatever. </li>
</ol>
| 5 | 2008-12-18T18:59:06Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 378,826 | <p>do you have something like this at the top before you print anything else?</p>
<pre><code>print "Content-type: text/html\n"
</code></pre>
<p>If you already have this, then post your code.</p>
| 6 | 2008-12-18T19:00:01Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 378,890 | <p>You may also get a better error message by adding this line at the top of your Python script:</p>
<p><code>import cgitb; cgitb.enable()</code></p>
<p>Also, the header should be capitalized <code>Content-Type</code>, not <code>Content-type</code>, although I doubt that that is breaking anything.</p>
| 1 | 2008-12-18T19:18:33Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 378,974 | <p>This is the exact behavior you would get if your Python script does not have the executable permission set.</p>
<p>Try:</p>
<pre><code>chmod a+x foo.py
</code></pre>
<p>(where foo.py is your script name).</p>
<p>See the <a href="http://httpd.apache.org/docs/1.3/howto/cgi.html#filepermissions">Apache tutorial</a>... | 10 | 2008-12-18T19:42:48Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 378,975 | <p>OK last guess:</p>
<p>Trying changing that shebang line to:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>or </p>
<pre><code>#!/usr/bin/local/env python
</code></pre>
<p>It would also be helpful to know your platform / hosting provider.</p>
| 1 | 2008-12-18T19:43:11Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 479,519 | <p>I had a similiar problem running my own local server, but got it solved following this guide step-by-step: <a href="http://docs.python.org/howto/webservers.html" rel="nofollow">HOWTO Use Python in the web</a></p>
| 0 | 2009-01-26T11:46:41Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 486,183 | <p>Sounds to me like you're using a script written in Windows on a Unix machine, without first converting the line-endings from 0d0a to 0a. It should be easy to convert it. One way is with your ftp program; transfer the file in ASCII mode. The way I use with Metapad is to use File->FileFormat before saving.</p>
| 0 | 2009-01-28T01:48:58Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 9,333,556 | <p>I tried many approaches to get Python working with Apache properly and finally settled with using <strong>Apache + mod_WSGI + <a href="http://webpy.org/cookbook/mod_wsgi-apache" rel="nofollow">web.py</a></strong> . It sounds like a lot, but it is much simpler than using the complicated frameworks like Django out the... | 0 | 2012-02-17T18:35:42Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 14,104,160 | <p>You can also get some of this same foolishness if you have DOS style end of lines on a linux web server. (That just chewed up about two hours of my morning today.) Off to update my vim.rc on this windows box that I need to use.</p>
| 0 | 2012-12-31T17:01:39Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 17,910,193 | <p>I had a similar problem, the problem is that you need to have two lines breaks after printing the content type. The following worked for me : </p>
<pre><code>#!/usr/local/bin/python2.6
print('Content-type: text/html\r\n')
print('\r\n')
print('Hello, World!')
</code></pre>
| 0 | 2013-07-28T16:11:49Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 22,080,953 | <p>One common error is the wrong path. I my case it was usr/bin/python. The other common error is not transferring the file in ASCII mode. I am using WinSCP where you can set it easily: Go to Options->Preferences->Transfers->click Edit and change the mode to Text.</p>
<p>This code should work:</p>
<pre><code>#!/usr/b... | 0 | 2014-02-27T21:34:48Z | [
"python"
] |
Getting python to work, Internal Server Error | 378,811 | <p>I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error: </p>
<pre><code>500 Internal Server Error
</code></pre>
<p>When I check my error logs I see the message </p>
<pre><code>Premature end of script headers
</code></pre>
<p>The only documentation of this error online says... | 6 | 2008-12-18T18:53:14Z | 34,962,796 | <p>If you have configured Apaches httpd file correctly then you might be getting this error for following two reasons.Make sure these are correct.</p>
<ol>
<li>Include '#!/usr/bin/python' or '#!C:/Python27/python' or accordingly in your script as first line.</li>
<li>Make sure there is space after print "Content-type:... | 2 | 2016-01-23T11:22:15Z | [
"python"
] |
What does the []-esque decorator syntax in Python mean? | 379,291 | <p>Here's a snippet of code from within TurboGears 1.0.6:</p>
<pre><code>[dispatch.generic(MultiorderGenericFunction)]
def run_with_transaction(func, *args, **kw):
pass
</code></pre>
<p>I can't figure out how putting a list before a function definition can possibly affect it.</p>
<p>In dispatch.generic's docstri... | 8 | 2008-12-18T21:14:04Z | 379,333 | <p>Nothing mysterious, it's just how syntax was before.</p>
<p>The parser has changed, probably because the Python Zen claims that "In the face of ambiguity, refuse the temptation to guess.". </p>
<p>[] should be for list only, and there is it.</p>
| -2 | 2008-12-18T21:22:48Z | [
"python",
"syntax",
"decorator"
] |
What does the []-esque decorator syntax in Python mean? | 379,291 | <p>Here's a snippet of code from within TurboGears 1.0.6:</p>
<pre><code>[dispatch.generic(MultiorderGenericFunction)]
def run_with_transaction(func, *args, **kw):
pass
</code></pre>
<p>I can't figure out how putting a list before a function definition can possibly affect it.</p>
<p>In dispatch.generic's docstri... | 8 | 2008-12-18T21:14:04Z | 379,409 | <p>The decorator syntax is provided by PyProtocols.</p>
<p>"""
Finally, it's important to note that these "magic" decorators use a very sneaky hack: they abuse the sys.settrace() debugger hook to track whether assignments are taking place. Guido takes a very dim view of this, but the hook's existing functionality isn'... | 11 | 2008-12-18T21:44:07Z | [
"python",
"syntax",
"decorator"
] |
How to integrate the StringTemplate engine into the CherryPy web server | 379,338 | <p>I love the StringTemplate engine, and I love the CherryPy web server, and I know that they can be integrated.</p>
<p>Who has done it? How?</p>
<p>EDIT: The TurboGears framework takes the CherryPy web server and bundles other related components such as a template engine, data access tools, JavaScript kit, etc. I... | 2 | 2008-12-18T21:23:50Z | 463,042 | <p>Rob,</p>
<p>There's reason behind people's selection of tools. StringTemplate is not terribly popular for Python, there are templating engines that are much better supported and with a much wider audience. If you don't like Kid, there's also Django's templating, Jinja, Cheetah and others. Perhaps you can find in on... | 0 | 2009-01-20T20:49:25Z | [
"python",
"cherrypy",
"stringtemplate"
] |
How to integrate the StringTemplate engine into the CherryPy web server | 379,338 | <p>I love the StringTemplate engine, and I love the CherryPy web server, and I know that they can be integrated.</p>
<p>Who has done it? How?</p>
<p>EDIT: The TurboGears framework takes the CherryPy web server and bundles other related components such as a template engine, data access tools, JavaScript kit, etc. I... | 2 | 2008-12-18T21:23:50Z | 467,736 | <p>Based on the tutorials for both, it looks pretty straightforward:</p>
<pre>
import stringtemplate
import cherrypy
class HelloWorld(object):
def index(self):
hello = stringtemplate.StringTemplate("Hello, $name$")
hello["name"] = "World"
return str(hello)
index.exposed = True
cherryp... | 4 | 2009-01-22T01:09:42Z | [
"python",
"cherrypy",
"stringtemplate"
] |
What are these tags @ivar @param and @type in python docstring? | 379,346 | <p>The ampoule project uses some tags in docstring, like the javadoc ones. </p>
<p>For example from <a href="http://bazaar.launchpad.net/~dialtone/ampoule/main/annotate/26?file_id=pool.py-20080501191749-jqawtxogk4i0quu3-12">pool.py</a> line 86:</p>
<pre><code>def start(self, ampChild=None):
"""
Starts the Pro... | 13 | 2008-12-18T21:26:39Z | 379,415 | <p>Markup for a documentation tool, probably <a href="http://epydoc.sourceforge.net/">epydoc</a>.</p>
| 12 | 2008-12-18T21:45:41Z | [
"python",
"documentation",
"javadoc"
] |
What are these tags @ivar @param and @type in python docstring? | 379,346 | <p>The ampoule project uses some tags in docstring, like the javadoc ones. </p>
<p>For example from <a href="http://bazaar.launchpad.net/~dialtone/ampoule/main/annotate/26?file_id=pool.py-20080501191749-jqawtxogk4i0quu3-12">pool.py</a> line 86:</p>
<pre><code>def start(self, ampChild=None):
"""
Starts the Pro... | 13 | 2008-12-18T21:26:39Z | 380,737 | <p>Just for fun I'll note that the Python standard library is using Sphinx/reStructuredText, whose <a href="http://sphinx-doc.org/domains.html" rel="nofollow">info field lists</a> are similar.</p>
<pre><code>def start(self, ampChild=None):
"""Starts the ProcessPool with a given child protocol.
:param ampChild... | 11 | 2008-12-19T11:34:22Z | [
"python",
"documentation",
"javadoc"
] |
How much slower is a wxWidget written in Python versus C++? | 379,442 | <p>I'm looking into writing a wxWidget that displays a graphical node network, and therefore does a lot of drawing operations. I know that using Python to do it is going to be slower, but I'd rather get it working and port it later when its functional. Ideally, if the performance hit isn't too great, I'd prefer to ke... | 6 | 2008-12-18T21:52:22Z | 379,589 | <p>IMHO, main bottleneck will be the data structures you are going to use for representing the network graph. I have coded a similar application for tracing dependencies between various component versions in a system and graphics was the last thing I had to worry about and I was certainly drawing more than 500 objects ... | 1 | 2008-12-18T22:41:00Z | [
"c++",
"python",
"performance",
"drawing",
"wxpython"
] |
How much slower is a wxWidget written in Python versus C++? | 379,442 | <p>I'm looking into writing a wxWidget that displays a graphical node network, and therefore does a lot of drawing operations. I know that using Python to do it is going to be slower, but I'd rather get it working and port it later when its functional. Ideally, if the performance hit isn't too great, I'd prefer to ke... | 6 | 2008-12-18T21:52:22Z | 379,790 | <p>In my experience, doing things the naive way (drawing each object to the screen) will bog down in Python quicker than C++. However, with Python it's going to be a lot quicker and less painful to code it the clever way (see for example <a href="http://www.wxpython.org/docs/api/wx.PseudoDC-class.html" rel="nofollow">P... | 1 | 2008-12-19T00:18:02Z | [
"c++",
"python",
"performance",
"drawing",
"wxpython"
] |
How much slower is a wxWidget written in Python versus C++? | 379,442 | <p>I'm looking into writing a wxWidget that displays a graphical node network, and therefore does a lot of drawing operations. I know that using Python to do it is going to be slower, but I'd rather get it working and port it later when its functional. Ideally, if the performance hit isn't too great, I'd prefer to ke... | 6 | 2008-12-18T21:52:22Z | 380,558 | <p>For drawing, people have suggested PyGame. I like PyGame, its easy to work with and works well. Other choices would be Pyglet, or using PyOpenGL (you can most likely draw to a wx widget too, though I've never done it).</p>
<p>Personally, I'd do it in Python using whatever library I'm most familiar with (in my case,... | 1 | 2008-12-19T10:00:42Z | [
"c++",
"python",
"performance",
"drawing",
"wxpython"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,909 | <p><code>float("545.2222")</code> and <code>int(float("545.2222"))</code></p>
| 8 | 2008-12-19T01:54:11Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,910 | <pre><code>>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
</code></pre>
| 1,372 | 2008-12-19T01:54:51Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,932 | <p>Users <em>codelogic</em> and <em>harley</em> are correct, but keep in mind if you know the string is an integer (for example, 545) you can call int("545") without first casting to float.</p>
<p>If your strings are in a list, you could use the map function as well. </p>
<pre><code>>>> x = ["545.0", "545.6"... | 13 | 2008-12-19T02:09:03Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,963 | <p>Here's another interpretation of your question (hint: it's vague). It's possible you're looking for something like this:</p>
<pre><code>def parseIntOrFloat( aString ):
return eval( aString )
</code></pre>
<p>It works like this...</p>
<pre><code>>>> parseIntOrFloat("545.2222")
545.22220000000004
>&... | -12 | 2008-12-19T02:29:34Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,966 | <pre><code>def num(s):
try:
return int(s)
except ValueError:
return float(s)
</code></pre>
| 340 | 2008-12-19T02:31:23Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 379,968 | <pre><code>float(x) if '.' in x else int(x)
</code></pre>
| 67 | 2008-12-19T02:32:06Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 3,988,355 | <p>You need to take into account rounding to do this properly.</p>
<p>I.e. int(5.1) => 5
int(5.6) => 5 -- wrong, should be 6 so we do int(5.6 + 0.5) => 6</p>
<pre><code>def convert(n):
try:
return int(n)
except ValueError:
return float(n + 0.5)
</code></pre>
| 4 | 2010-10-21T14:10:01Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 7,588,720 | <p>The question seems a little bit old. But let me suggest a function, parseStr, which makes something similar, that is, returns integer or float and if a given ASCII string cannot be converted to none of them it returns it untouched. The code of course might be adjusted to do only what you want:</p>
<pre><code> >... | 11 | 2011-09-28T19:45:18Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 9,510,585 | <p>This is another method which deserves to be mentioned here, <a href="http://docs.python.org/library/ast.html#ast.literal_eval">ast.literal_eval</a>:</p>
<blockquote>
<p>This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.... | 75 | 2012-03-01T04:23:15Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.