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 |
|---|---|---|---|---|---|---|---|---|---|
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,440 | <p>Python (like Java, C, C++, .NET) uses string pooling / interning. The interpreter realises that "hello" is the same as "hello", so it optimizes and uses the same location in memory.</p>
<p>Another goodie: "hell" + "o" is "hello" ==> True</p>
| 76 | 2009-09-08T07:15:09Z | [
"python",
"identity"
] |
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,446 | <p>The Python interpreter/compiler parses the string literals, i.e. the quoted list of characters. When it does this, it can detect "I've seen this string before", and use the same representation as last time. It can do this since it knows that strings defined in this way cannot be changed.</p>
| 1 | 2009-09-08T07:16:24Z | [
"python",
"identity"
] |
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,449 | <p>Literal strings are probably grouped based on their hash or something similar. Two of the same literal strings will be stored in the same memory, and any references both refer to that.</p>
<pre><code> Memory Code
-------
| myLine = "hello"
| /
|hello <
| \
| myLine = "hel... | 12 | 2009-09-08T07:16:34Z | [
"python",
"identity"
] |
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,451 | <p>The <code>is</code> operator returns true if both arguments are the same object. Your result is a consequence of this, and the quoted bit. </p>
<p>In the case of string literals, these are interned, meaning they are compared to known strings. If an identical string is already known, the literal takes that value,... | 6 | 2009-09-08T07:16:54Z | [
"python",
"identity"
] |
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,927 | <blockquote>
<p>So there is one and only one place in memory for every Python string?</p>
</blockquote>
<p>No, only ones the interpreter has decided to optimise, which is a decision based on a policy that isn't part of the language specification and which may change in different CPython versions.</p>
<p>eg. on my i... | 55 | 2009-09-08T09:19:59Z | [
"python",
"identity"
] |
Python: Why is ("hello" is "hello")? | 1,392,433 | <p>Why is <code>"hello" is "hello" == True</code> in Python?</p>
<p>I read the following <a href="http://zetcode.com/tutorials/pythontutorial/keywords/" rel="nofollow">here</a>:</p>
<blockquote>
<p>If two string literals are equal, they have been put to same
memory location. A string is an immutable entity. No h... | 48 | 2009-09-08T07:13:19Z | 1,392,928 | <p>I think if any two variables (not just strings) contain the same value, the value will be stored only once not twice and both the variables will point to the same location. This saves memory.</p>
| 0 | 2009-09-08T09:20:17Z | [
"python",
"identity"
] |
What python data structure and parser should I use with Apple's system_profiler? | 1,392,604 | <p>My problem is one like a simulated problem from
<a href="http://my.safaribooksonline.com/0596007973/pythoncook2-CHP-10-SECT-17" rel="nofollow">http://my.safaribooksonline.com/0596007973/pythoncook2-CHP-10-SECT-17</a>
which eventually made its way into Python Cookbook, 2nd Edition using an outdated xpath method from... | 0 | 2009-09-08T08:01:26Z | 1,392,719 | <p>Use the -xml option to <code>system_profiler</code> to format the output in a standard OS X plist format, then use Python's built-in <a href="http://docs.python.org/library/plistlib.html">plistlib</a> to parse into an appropriate data structure you can introspect. A simple example:</p>
<pre><code>>>> from... | 5 | 2009-09-08T08:30:58Z | [
"python",
"osx",
"parsing",
"profiler",
"system-profiler"
] |
Django template, how to make a dropdown box with the predefined value selected? | 1,392,706 | <p>I am trying to create a drop down list box with the selected value equal to a value passed from the template values, but with no success. Can anyone take a look and show me what I am doing wrong.</p>
<pre><code> <select name="movie">
{% for movie in movies %}
{% ifequal movie.id selected_movie.... | 4 | 2009-09-08T08:27:48Z | 1,392,747 | <p>Your code works for me with django 1.0.2 and firefox 3.5.</p>
<p>You can use {% else %} instead of {% ifnotequal %} and set selected="selected". Hope it helps.</p>
<pre><code><select name="movie">
{% for movie in movies %}
{% ifequal movie.id selected_movie.id %}
<option value="{{m... | 9 | 2009-09-08T08:40:05Z | [
"python",
"django",
"django-templates"
] |
Problem with subprocess.call | 1,392,757 | <p>In my current working directory I have the dir ROOT/ with some files inside.</p>
<p>I know I can exec <code>cp -r ROOT/* /dst</code> and I have no problems.</p>
<p>But if I open my Python console and I write this:</p>
<pre><code>import subprocess
subprocess.call(['cp', '-r', 'ROOT/*', '/dst'])
</code></pre>
<p>I... | 11 | 2009-09-08T08:42:10Z | 1,392,785 | <p>The <code>*</code> will not be expanded to filenames. This is a function of the shell. Here you actually want to copy a file named *. Use <code>subprocess.call()</code> with the parameter <code>shell=True</code>.</p>
| 4 | 2009-09-08T08:49:23Z | [
"python",
"subprocess"
] |
Problem with subprocess.call | 1,392,757 | <p>In my current working directory I have the dir ROOT/ with some files inside.</p>
<p>I know I can exec <code>cp -r ROOT/* /dst</code> and I have no problems.</p>
<p>But if I open my Python console and I write this:</p>
<pre><code>import subprocess
subprocess.call(['cp', '-r', 'ROOT/*', '/dst'])
</code></pre>
<p>I... | 11 | 2009-09-08T08:42:10Z | 1,392,905 | <p>Try</p>
<pre><code>subprocess.call('cp -r ROOT/* /dst', shell=True)
</code></pre>
<p>Note the use of a single string rather than an array here.</p>
<p>Or build up your own implementation with <a href="http://docs.python.org/library/os.html#os.listdir">listdir</a> and <a href="http://docs.python.org/library/shutil... | 7 | 2009-09-08T09:16:02Z | [
"python",
"subprocess"
] |
Problem with subprocess.call | 1,392,757 | <p>In my current working directory I have the dir ROOT/ with some files inside.</p>
<p>I know I can exec <code>cp -r ROOT/* /dst</code> and I have no problems.</p>
<p>But if I open my Python console and I write this:</p>
<pre><code>import subprocess
subprocess.call(['cp', '-r', 'ROOT/*', '/dst'])
</code></pre>
<p>I... | 11 | 2009-09-08T08:42:10Z | 5,040,567 | <p>Just came across this while trying to do something similar.</p>
<blockquote>
<p>The * will not be expanded to filenames</p>
</blockquote>
<p>Exactly. If you look at the man page of <code>cp</code> you can call it with any number of source arguments and you can easily change the order of the arguments with the <c... | 9 | 2011-02-18T11:10:52Z | [
"python",
"subprocess"
] |
Problem with subprocess.call | 1,392,757 | <p>In my current working directory I have the dir ROOT/ with some files inside.</p>
<p>I know I can exec <code>cp -r ROOT/* /dst</code> and I have no problems.</p>
<p>But if I open my Python console and I write this:</p>
<pre><code>import subprocess
subprocess.call(['cp', '-r', 'ROOT/*', '/dst'])
</code></pre>
<p>I... | 11 | 2009-09-08T08:42:10Z | 8,036,844 | <p>Provide the command as list instead of the string + list.</p>
<p>The following two commands are same:-</p>
<pre><code>First Command:-
test=subprocess.Popen(['rm','aa','bb'])
Second command:-
list1=['rm','aa','bb']
test=subprocess.Popen(list1)
</code></pre>
<p>So to copy multiple files, one need to get the list o... | 0 | 2011-11-07T13:00:58Z | [
"python",
"subprocess"
] |
using RSPython in MacOSX | 1,392,868 | <p>I am trying to install the
<a href="http://www.omegahat.org/RSPython/" rel="nofollow">R/SPlus - Python Interface (RSPython)</a> on my Mac OS X 10.4.11 with R version 2.7.2 (2008-08-25) and python 2.6.2 from fink.</p>
<p>The routine:</p>
<pre><code>sudo R CMD INSTALL -c RSPython_0.7-1.tar.gz
</code></pre>
<p>produ... | 1 | 2009-09-08T09:09:10Z | 1,393,669 | <p>Try running R CMD CHECK RSPython_0.7-1.tar.gz
That should produce at least produce bunch of logs in a RSPython.Rcheck folder</p>
<p>You might get some clues in there. </p>
<p>Update ---
If you can get one of the other packages to work I'd recommend it. On my system (R 2.9.1 using system python (2.6) in /usr/bin/... | 1 | 2009-09-08T12:15:15Z | [
"python",
"install"
] |
using RSPython in MacOSX | 1,392,868 | <p>I am trying to install the
<a href="http://www.omegahat.org/RSPython/" rel="nofollow">R/SPlus - Python Interface (RSPython)</a> on my Mac OS X 10.4.11 with R version 2.7.2 (2008-08-25) and python 2.6.2 from fink.</p>
<p>The routine:</p>
<pre><code>sudo R CMD INSTALL -c RSPython_0.7-1.tar.gz
</code></pre>
<p>produ... | 1 | 2009-09-08T09:09:10Z | 1,393,789 | <p>I found the <a href="http://rpy.sourceforge.net/" rel="nofollow">rpy</a> and <a href="http://rpy.sourceforge.net/" rel="nofollow">rpy2</a> packages and going to try those as an alternative to the older RSPython.</p>
<p>rpy2 is includes in fink's unstable distribution ... well, and it just works fine.</p>
| 0 | 2009-09-08T12:40:38Z | [
"python",
"install"
] |
using RSPython in MacOSX | 1,392,868 | <p>I am trying to install the
<a href="http://www.omegahat.org/RSPython/" rel="nofollow">R/SPlus - Python Interface (RSPython)</a> on my Mac OS X 10.4.11 with R version 2.7.2 (2008-08-25) and python 2.6.2 from fink.</p>
<p>The routine:</p>
<pre><code>sudo R CMD INSTALL -c RSPython_0.7-1.tar.gz
</code></pre>
<p>produ... | 1 | 2009-09-08T09:09:10Z | 1,393,805 | <p>To debug this, simply unpack the tar file yourself (<code>tar xzvf RSPython_0.7-1.tar.gz</code>) and run <code>./configure</code> in the directory created. You should get a config.log file that you can examine.</p>
| 1 | 2009-09-08T12:44:32Z | [
"python",
"install"
] |
GTK Twitter Client | 1,392,872 | <p>I am learning Python and PyGTK.
I'm trying to write a Twitter client. Which widget is best suited for displaying the Tweets (Timeline). I can do it easily with textview but it doesn't support sub widgets to display users image. </p>
<p>Tried using TreeView but it seems to be an overkill and is too complex.</p>
<p>... | 1 | 2009-09-08T09:10:12Z | 1,392,924 | <p>I would suggest <a href="http://library.gnome.org/devel/gtk/stable/GtkTreeView.html" rel="nofollow">GtkTreeView</a>, too. I agree it's kind of complicated (especially if you had time with GTK+ 1.2.x to get used to <a href="http://library.gnome.org/devel/gtk/stable/GtkCList.html" rel="nofollow">GtkCList</a>, which is... | 3 | 2009-09-08T09:19:27Z | [
"python",
"gtk",
"pygtk"
] |
GTK Twitter Client | 1,392,872 | <p>I am learning Python and PyGTK.
I'm trying to write a Twitter client. Which widget is best suited for displaying the Tweets (Timeline). I can do it easily with textview but it doesn't support sub widgets to display users image. </p>
<p>Tried using TreeView but it seems to be an overkill and is too complex.</p>
<p>... | 1 | 2009-09-08T09:10:12Z | 1,392,943 | <p>You could try Webkit (the browser rendering engine) using <a href="http://code.google.com/p/pywebkitgtk/" rel="nofollow">pywebkitgtk</a>. It let's you develop in web technologies (HTML, CSS, JS) on the desktop. I think Gwibber, the microblogging client, uses it.</p>
<p>The widget you'd have to use is <em>webkit.Web... | 5 | 2009-09-08T09:24:27Z | [
"python",
"gtk",
"pygtk"
] |
GTK Twitter Client | 1,392,872 | <p>I am learning Python and PyGTK.
I'm trying to write a Twitter client. Which widget is best suited for displaying the Tweets (Timeline). I can do it easily with textview but it doesn't support sub widgets to display users image. </p>
<p>Tried using TreeView but it seems to be an overkill and is too complex.</p>
<p>... | 1 | 2009-09-08T09:10:12Z | 1,484,907 | <p>Actually the TreeView does support embedding images and widgets within the text. Checkout the PyGTK Tutorial section on TextView specifically: <a href="http://pygtk.org/pygtk2tutorial/sec-TextBuffers.html#id2855808" rel="nofollow">Inserting Images and Widgets</a></p>
| 1 | 2009-09-28T00:29:15Z | [
"python",
"gtk",
"pygtk"
] |
reloading .mo files for all processes/threads in django without a restart | 1,392,913 | <p>We are working on a .po file editor for translators. And the translators need to see the changes they are doing on the live website.</p>
<p>We managed to reload the .mo files for the current process/thread. but not for every process/thread. </p>
<p>Is there a possibility to accomplish this without bigger performan... | 3 | 2009-09-08T09:17:23Z | 1,401,004 | <p>I checked <a href="http://code.google.com/p/django-rosetta/" rel="nofollow">django-rosetta</a> and, as I suspected, they rely on the <a href="http://code.google.com/p/modwsgi/" rel="nofollow">mod_wsgi</a> AutoReload mechanism. This is what I would have suggested. For more details read <a href="http://code.google.c... | 2 | 2009-09-09T17:32:18Z | [
"python",
"django"
] |
reloading .mo files for all processes/threads in django without a restart | 1,392,913 | <p>We are working on a .po file editor for translators. And the translators need to see the changes they are doing on the live website.</p>
<p>We managed to reload the .mo files for the current process/thread. but not for every process/thread. </p>
<p>Is there a possibility to accomplish this without bigger performan... | 3 | 2009-09-08T09:17:23Z | 19,293,936 | <p>We had the same problem. Users must write translations direct on website. I've found middleware for django 1.1, that clear translation cache and try to use it in view with django 1.4.
order:</p>
<ol>
<li>user submit from with translation</li>
<li>methods parse form data and change *.po</li>
<li>-subrocess.Popen(["p... | 1 | 2013-10-10T10:58:54Z | [
"python",
"django"
] |
Java modified UTF-8 strings in Python | 1,393,004 | <p>I am interfacing with a Java application via Python. I need to be able to construct byte sequences which contain utf-8 strings. Java uses a modified utf-8 encoding in DataInputStream.readUTF() which is not supported by python (<a href="http://bugs.python.org/issue2857" rel="nofollow">yet at least</a>)</p>
<p>Can an... | 3 | 2009-09-08T09:40:02Z | 1,393,063 | <p>Okay, if you need to read the format of <code>DataInput.readUTF</code>, I suspect you'll just have to convert the (well-documented) format into Python.</p>
<p>It doesn't look like it would be particularly hard to do. After reading the length and then the binary data itself, I suggest you use a first pass to work ou... | 1 | 2009-09-08T09:54:37Z | [
"java",
"python",
"utf-8"
] |
Java modified UTF-8 strings in Python | 1,393,004 | <p>I am interfacing with a Java application via Python. I need to be able to construct byte sequences which contain utf-8 strings. Java uses a modified utf-8 encoding in DataInputStream.readUTF() which is not supported by python (<a href="http://bugs.python.org/issue2857" rel="nofollow">yet at least</a>)</p>
<p>Can an... | 3 | 2009-09-08T09:40:02Z | 1,393,079 | <p>Maybe this can help you, although it looks like it's the reverse of what you're doing:</p>
<p><a href="http://www.phaedro.com/javapythonutf8/" rel="nofollow">Connecting a Java applet to a python SocketServer</a></p>
| 0 | 2009-09-08T09:58:45Z | [
"java",
"python",
"utf-8"
] |
Java modified UTF-8 strings in Python | 1,393,004 | <p>I am interfacing with a Java application via Python. I need to be able to construct byte sequences which contain utf-8 strings. Java uses a modified utf-8 encoding in DataInputStream.readUTF() which is not supported by python (<a href="http://bugs.python.org/issue2857" rel="nofollow">yet at least</a>)</p>
<p>Can an... | 3 | 2009-09-08T09:40:02Z | 1,393,579 | <p>You can ignore <strong>Modified UTF-8 Encoding</strong> (MUTF-8) and just treat it as UTF-8. On the Python side, you can just handle it like this,</p>
<ol>
<li>Convert the string into normal UTF-8 and stores bytes in a buffer.</li>
<li>Write the 2-byte buffer length (not the string length) as binary in big-endian.... | 5 | 2009-09-08T11:55:41Z | [
"java",
"python",
"utf-8"
] |
Pythonic way to "flatten" object hierarchy to nested dicts? | 1,393,010 | <p>I need to "flatten" objects into nested dicts of the object's properties. The objects I want to do this with are generally just containers for basic types or other objects which act in a similar way. For example:</p>
<pre><code>class foo(object):
bar = None
baz = None
class spam(object):
eggs = []
x =... | 0 | 2009-09-08T09:41:50Z | 1,393,167 | <p>No, there is nothing in the standardlib. Yes, you would have to somehow test that the types are basic types like str, unicode, bool, int, float, long...</p>
<p>You could probably make a registry of methods to "serialize" different types, but that would only be useful if you have some types that should not have all ... | 0 | 2009-09-08T10:21:28Z | [
"python"
] |
Pythonic way to "flatten" object hierarchy to nested dicts? | 1,393,010 | <p>I need to "flatten" objects into nested dicts of the object's properties. The objects I want to do this with are generally just containers for basic types or other objects which act in a similar way. For example:</p>
<pre><code>class foo(object):
bar = None
baz = None
class spam(object):
eggs = []
x =... | 0 | 2009-09-08T09:41:50Z | 1,393,555 | <p>Almost every object has a dictionary (called <code>__dict__</code>), with all its methods and members.<br />
With some type checking, you can then write a function that filters out only the members you are interested in.</p>
<p>It is not a big task, but as chrispy said, it could worth to try looking at your problem... | 0 | 2009-09-08T11:50:53Z | [
"python"
] |
Pythonic way to "flatten" object hierarchy to nested dicts? | 1,393,010 | <p>I need to "flatten" objects into nested dicts of the object's properties. The objects I want to do this with are generally just containers for basic types or other objects which act in a similar way. For example:</p>
<pre><code>class foo(object):
bar = None
baz = None
class spam(object):
eggs = []
x =... | 0 | 2009-09-08T09:41:50Z | 1,393,959 | <p><strong>Code:</strong> You may need to handle other iterable types though:</p>
<pre><code>def flatten(obj):
if obj is None:
return None
elif hasattr(obj, '__dict__') and obj.__dict__:
return dict([(k, flatten(v)) for (k, v) in obj.__dict__.items()])
elif isinstance(obj, (dict,)):
... | 2 | 2009-09-08T13:14:45Z | [
"python"
] |
Pythonic way to "flatten" object hierarchy to nested dicts? | 1,393,010 | <p>I need to "flatten" objects into nested dicts of the object's properties. The objects I want to do this with are generally just containers for basic types or other objects which act in a similar way. For example:</p>
<pre><code>class foo(object):
bar = None
baz = None
class spam(object):
eggs = []
x =... | 0 | 2009-09-08T09:41:50Z | 1,401,621 | <p>Well, I'm not very proud of this, but is possible to do the following:</p>
<ol>
<li>Create a super class that has the serialization method and loop through its properties.</li>
<li>At runtime extend your classes using <strong>bases</strong> at runtime</li>
<li>Execute the class from the new super class. It should b... | 0 | 2009-09-09T19:34:04Z | [
"python"
] |
Vim syntax highlighting 'else:' for Python | 1,393,075 | <p>I'm getting annoyed with the default python syntax highlighting in Vim.</p>
<p>It does not highlight the <code>else:</code> statement correctly.
Vim only highlights the else statement if I have some white space between the <code>else</code> and the colon <code>:</code>, so <code>else :</code> works, but <code>else:... | 5 | 2009-09-08T09:58:00Z | 1,393,207 | <p>It should work by default.</p>
<p>Look for a file that is something like this:</p>
<pre><code>/usr/share/vim/vim72/syntax/python.vim
</code></pre>
<p>There should be a line there that looks like this:</p>
<pre><code>syn keyword pythonConditional if elif else
</code></pre>
<p>If not, investigate further. Does ... | 3 | 2009-09-08T10:30:29Z | [
"python",
"vim",
"syntax-highlighting",
"vim-syntax-highlighting"
] |
Vim syntax highlighting 'else:' for Python | 1,393,075 | <p>I'm getting annoyed with the default python syntax highlighting in Vim.</p>
<p>It does not highlight the <code>else:</code> statement correctly.
Vim only highlights the else statement if I have some white space between the <code>else</code> and the colon <code>:</code>, so <code>else :</code> works, but <code>else:... | 5 | 2009-09-08T09:58:00Z | 1,393,230 | <p>Enable the filetype stuff so things are setup automatically.
I've python auto indenting and syntax highlighting setup for vim here:
<a href="http://www.pixelbeat.org/settings/.vimrc" rel="nofollow">http://www.pixelbeat.org/settings/.vimrc</a></p>
| 1 | 2009-09-08T10:35:12Z | [
"python",
"vim",
"syntax-highlighting",
"vim-syntax-highlighting"
] |
Vim syntax highlighting 'else:' for Python | 1,393,075 | <p>I'm getting annoyed with the default python syntax highlighting in Vim.</p>
<p>It does not highlight the <code>else:</code> statement correctly.
Vim only highlights the else statement if I have some white space between the <code>else</code> and the colon <code>:</code>, so <code>else :</code> works, but <code>else:... | 5 | 2009-09-08T09:58:00Z | 1,410,674 | <p>I had below statement in my vimrc file. This made else: be tokenized as one word. After removing that else: syntax highlighting works just fine. </p>
<p><code>set iskeyword+=:</code></p>
| 5 | 2009-09-11T12:51:30Z | [
"python",
"vim",
"syntax-highlighting",
"vim-syntax-highlighting"
] |
Vim syntax highlighting 'else:' for Python | 1,393,075 | <p>I'm getting annoyed with the default python syntax highlighting in Vim.</p>
<p>It does not highlight the <code>else:</code> statement correctly.
Vim only highlights the else statement if I have some white space between the <code>else</code> and the colon <code>:</code>, so <code>else :</code> works, but <code>else:... | 5 | 2009-09-08T09:58:00Z | 4,041,958 | <p>Note that the file in</p>
<pre><code>/usr/share/vim/vim72/syntax/python.vim
</code></pre>
<p>is not up to date for some reason.</p>
<p>Get the most recent version of the syntax highlight file from <a href="http://www.vim.org/scripts/script.php?script_id=790" rel="nofollow">http://www.vim.org/scripts/script.php?sc... | 3 | 2010-10-28T10:31:23Z | [
"python",
"vim",
"syntax-highlighting",
"vim-syntax-highlighting"
] |
Vim syntax highlighting 'else:' for Python | 1,393,075 | <p>I'm getting annoyed with the default python syntax highlighting in Vim.</p>
<p>It does not highlight the <code>else:</code> statement correctly.
Vim only highlights the else statement if I have some white space between the <code>else</code> and the colon <code>:</code>, so <code>else :</code> works, but <code>else:... | 5 | 2009-09-08T09:58:00Z | 23,918,908 | <p>This periodically pops up, and I've taken to putting this in my vimrc:</p>
<pre><code>" No seriously, else: is the same as else
autocmd BufEnter,BufRead,BufNewFile *.py set iskeyword-=:
</code></pre>
| 2 | 2014-05-28T18:11:36Z | [
"python",
"vim",
"syntax-highlighting",
"vim-syntax-highlighting"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 1,393,231 | <p><code>ImageField</code> is just a string, a path relative to your <code>MEDIA_ROOT</code> setting. Just save the file (you might want to use PIL to check it is an image) and populate the field with its filename.</p>
<p>So it differs from your code in that you need to save the output of your <code>urllib.urlopen</co... | 4 | 2009-09-08T10:35:58Z | [
"python",
"django",
"urllib",
"django-models"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 1,393,316 | <pre>
<code>
from myapp.models import Photo
import urllib
from urlparse import urlparse
from django.core.files import File
img_url = 'http://www.site.com/image.jpg'
photo = Photo() # set any other fields, but don't commit to DB (ie. don't save())
name = urlparse(img_url).path.split('/')[-1]
content = urllib.urlret... | 22 | 2009-09-08T10:54:40Z | [
"python",
"django",
"urllib",
"django-models"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 2,141,823 | <p>I just created <a href="http://www.djangosnippets.org/snippets/1890/">http://www.djangosnippets.org/snippets/1890/</a> for this same problem. The code is similar to pithyless' answer above except it uses urllib2.urlopen because urllib.urlretrieve doesn't perform any error handling by default so it's easy to get the ... | 80 | 2010-01-26T18:59:14Z | [
"python",
"django",
"urllib",
"django-models"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 13,106,218 | <p>this is the right and working way</p>
<pre><code>class Product(models.Model):
upload_path = 'media/product'
image = models.ImageField(upload_to=upload_path, null=True, blank=True)
image_url = models.URLField(null=True, blank=True)
def save(self, *args, **kwargs):
if self.image_url:
... | 1 | 2012-10-28T03:49:13Z | [
"python",
"django",
"urllib",
"django-models"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 26,331,472 | <p>I do it this way on Python 3, which should work with simple adaptations on Python 2. This is based on my knowledge that the files Iâm retrieving are small. If yours arenât, Iâd probably recommend writing the response out to a file instead of buffering in memory.</p>
<p>BytesIO is needed because Django calls s... | 2 | 2014-10-13T00:40:01Z | [
"python",
"django",
"urllib",
"django-models"
] |
Django: add image in an ImageField from image url | 1,393,202 | <p>please excuse me for my ugly english ;-)</p>
<p>Imagine this very simple model :</p>
<pre><code>class Photo(models.Model):
image = models.ImageField('Label', upload_to='path/')
</code></pre>
<p>I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).</p>
<p>I think that ... | 65 | 2009-09-08T10:29:43Z | 30,943,626 | <p>If you are using file uploading feature in your Django project, you must install Pillow first:</p>
<pre><code>pip install pillow==2.6.1
</code></pre>
<p>Remember to set the url for media files in settings.py:</p>
<pre><code>MEDIA_ROOT = 'media/'
</code></pre>
<p>Then, in your models.py, add the following image f... | -1 | 2015-06-19T17:08:30Z | [
"python",
"django",
"urllib",
"django-models"
] |
Call a function from a running process | 1,393,242 | <p>my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?</p>
<p>Alendit</p>
| 0 | 2009-09-08T10:38:49Z | 1,393,268 | <p>You can use the <a href="http://docs.python.org/library/signal.html" rel="nofollow">signal module</a> from the Python standard library to register a signal handler. The subprocess would then use normal signal sending mechanisms.</p>
| 1 | 2009-09-08T10:44:05Z | [
"python",
"ipc",
"subprocess"
] |
Call a function from a running process | 1,393,242 | <p>my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?</p>
<p>Alendit</p>
| 0 | 2009-09-08T10:38:49Z | 1,393,346 | <p>Parent code:
</p>
<pre><code>import signal
def my_callback(signal, frame):
print "Signal %d received" % (signal,)
signal.signal(signal.SIGUSR1, my_callback)
# start child
</code></pre>
<p>Child code:
</p>
<pre><code>import os
import signal
signal.kill(os.getppid(), signal.SIGUSR1)
</code></pre>
<p>Be care... | 2 | 2009-09-08T11:00:27Z | [
"python",
"ipc",
"subprocess"
] |
Call a function from a running process | 1,393,242 | <p>my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?</p>
<p>Alendit</p>
| 0 | 2009-09-08T10:38:49Z | 1,393,543 | <p>If you are using Python 2.6, you can use the <code>multiprocessing</code> module from the standard library, in particular pipes and queues. Simple example from the docs:</p>
<pre><code>from multiprocessing import Process, Pipe
def f(conn): #This code will be spawned as a new child process
conn.send([42, None, ... | 4 | 2009-09-08T11:49:08Z | [
"python",
"ipc",
"subprocess"
] |
In Python, given a URL to a text file, what is the simplest way to read the contents of the text file? | 1,393,324 | <p>In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file? </p>
<pre><code>TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#pr... | 44 | 2009-09-08T10:56:08Z | 1,393,338 | <pre><code>import urllib2
f = urllib2.urlopen(target_url)
for l in f.readlines():
print l
</code></pre>
| 5 | 2009-09-08T10:59:27Z | [
"python"
] |
In Python, given a URL to a text file, what is the simplest way to read the contents of the text file? | 1,393,324 | <p>In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file? </p>
<pre><code>TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#pr... | 44 | 2009-09-08T10:56:08Z | 1,393,355 | <pre><code>import urllib2
for line in urllib2.urlopen("http://www.myhost.com/SomeFile.txt"):
print line
</code></pre>
| 10 | 2009-09-08T11:02:09Z | [
"python"
] |
In Python, given a URL to a text file, what is the simplest way to read the contents of the text file? | 1,393,324 | <p>In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file? </p>
<pre><code>TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#pr... | 44 | 2009-09-08T10:56:08Z | 1,393,367 | <p>Actually the simplest way is :</p>
<pre><code>import urllib2 # the lib that handles the url stuff
data = urllib2.urlopen(target_url) # it's a file like object and works just like a file
for line in data: # files are iterable
print line
</code></pre>
<p>You don't even need "readlines", as Will suggested. You ... | 60 | 2009-09-08T11:04:28Z | [
"python"
] |
In Python, given a URL to a text file, what is the simplest way to read the contents of the text file? | 1,393,324 | <p>In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file? </p>
<pre><code>TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#pr... | 44 | 2009-09-08T10:56:08Z | 1,396,288 | <p>There's really no need to read line-by-line. You can get the whole thing like this:</p>
<pre><code>import urllib
txt = urllib.urlopen(target_url).read()
</code></pre>
| 16 | 2009-09-08T20:55:15Z | [
"python"
] |
Terracotta for Python world? | 1,393,689 | <p>Would you know if something similar to <a href="http://www.terracotta.org/" rel="nofollow">Terracotta</a> (in Java world) exists for Python world? <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a> ? Or something else.</p>
| 1 | 2009-09-08T12:20:23Z | 1,393,848 | <p>I think Twisted is the best alternative you can find.
Let me warn you that it will give you some headaches, as it forces you to code in a completely different way. But once you understand it, it's not that hard....</p>
<p><a href="http://twistedmatrix.com/projects/core/documentation/howto/index.html" rel="nofollow"... | 1 | 2009-09-08T12:54:32Z | [
"java",
"python",
"terracotta"
] |
Terracotta for Python world? | 1,393,689 | <p>Would you know if something similar to <a href="http://www.terracotta.org/" rel="nofollow">Terracotta</a> (in Java world) exists for Python world? <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a> ? Or something else.</p>
| 1 | 2009-09-08T12:20:23Z | 1,393,879 | <p>If you want to use something that gives you a <a href="http://en.wikipedia.org/wiki/Distributed%5Fhash%5Ftable" rel="nofollow">Distributed Hashtable</a>/<a href="http://en.wikipedia.org/wiki/Tuple%5Fspace" rel="nofollow">Tuple Space</a> type of implementation, <a href="http://entangled.sourceforge.net/" rel="nofollo... | 0 | 2009-09-08T12:59:56Z | [
"java",
"python",
"terracotta"
] |
Terracotta for Python world? | 1,393,689 | <p>Would you know if something similar to <a href="http://www.terracotta.org/" rel="nofollow">Terracotta</a> (in Java world) exists for Python world? <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a> ? Or something else.</p>
| 1 | 2009-09-08T12:20:23Z | 1,849,570 | <p>try to run your code with JPython and use Terracotta ;)</p>
| 0 | 2009-12-04T20:48:18Z | [
"java",
"python",
"terracotta"
] |
Terracotta for Python world? | 1,393,689 | <p>Would you know if something similar to <a href="http://www.terracotta.org/" rel="nofollow">Terracotta</a> (in Java world) exists for Python world? <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a> ? Or something else.</p>
| 1 | 2009-09-08T12:20:23Z | 2,201,172 | <p><a href="http://pyro.sourceforge.net/" rel="nofollow">Pyro</a> can be used similarly in python as terracotta in java</p>
| 1 | 2010-02-04T16:11:53Z | [
"java",
"python",
"terracotta"
] |
configuration filename convention | 1,393,731 | <p>Is there a general naming conventions for configuration files for a simple python program?</p>
<p>Thanks,</p>
<p>Udi</p>
| 2 | 2009-09-08T12:28:28Z | 1,393,736 | <p>A convention? Mine would be, if my program was called <code>"Bob"</code>, simply <code>"bob.cfg"</code>.</p>
<p>I have to admit, I didn't really suffer any angst in coming up with that convention. Maybe I've been here too long :-)</p>
<p>Of course, if your configuration information is of a specific format (e.g., X... | 4 | 2009-09-08T12:29:47Z | [
"python",
"configuration",
"naming-conventions"
] |
configuration filename convention | 1,393,731 | <p>Is there a general naming conventions for configuration files for a simple python program?</p>
<p>Thanks,</p>
<p>Udi</p>
| 2 | 2009-09-08T12:28:28Z | 1,393,751 | <p>I am not sure if you mean the file basename, of if your question also includes where to put the configuration file. Anyway, on location:</p>
<p>If it is a linux application, you should follow the <a href="http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html" rel="nofollow">XDG Base Directory specific... | 4 | 2009-09-08T12:34:00Z | [
"python",
"configuration",
"naming-conventions"
] |
configuration filename convention | 1,393,731 | <p>Is there a general naming conventions for configuration files for a simple python program?</p>
<p>Thanks,</p>
<p>Udi</p>
| 2 | 2009-09-08T12:28:28Z | 1,393,752 | <p>I don't think there's any convention for this. You may just use file with extension like: .ini, .cfg, .conf, .config, .pref or anything you like.</p>
| 0 | 2009-09-08T12:34:05Z | [
"python",
"configuration",
"naming-conventions"
] |
configuration filename convention | 1,393,731 | <p>Is there a general naming conventions for configuration files for a simple python program?</p>
<p>Thanks,</p>
<p>Udi</p>
| 2 | 2009-09-08T12:28:28Z | 1,393,754 | <p>.conf is a nice extension too. But since mostly configuration files are application specific, it does not really matter what extension they have as long as they are consistent (i.e do not use .conf with .cfg in the same application).</p>
| 2 | 2009-09-08T12:34:08Z | [
"python",
"configuration",
"naming-conventions"
] |
configuration filename convention | 1,393,731 | <p>Is there a general naming conventions for configuration files for a simple python program?</p>
<p>Thanks,</p>
<p>Udi</p>
| 2 | 2009-09-08T12:28:28Z | 1,394,828 | <p>Django uses <code>settings.py</code> I like that a lot. It's very clear.</p>
| 1 | 2009-09-08T16:03:19Z | [
"python",
"configuration",
"naming-conventions"
] |
Need advice how to represent a certain datastructure in Python | 1,393,849 | <p>I'm not sure how to represent a certain datastructure in Python. It consists of groups and users where each user must be a member of exactly one group and groups should be in turn contained in a container, groups and users will only be used within this container. Furthermore I need random access to groups and users.... | 1 | 2009-09-08T12:54:38Z | 1,393,899 | <p>It's generally good practice to <em>not</em> have objects know about what contains them. Pass the user in to the group, not the group into the user. Just because your current application only has "users" used once, one per group, one group per account, doesn't mean you should hardcode all your classes with that know... | 2 | 2009-09-08T13:02:16Z | [
"python"
] |
Need advice how to represent a certain datastructure in Python | 1,393,849 | <p>I'm not sure how to represent a certain datastructure in Python. It consists of groups and users where each user must be a member of exactly one group and groups should be in turn contained in a container, groups and users will only be used within this container. Furthermore I need random access to groups and users.... | 1 | 2009-09-08T12:54:38Z | 1,393,912 | <p>I would feel comfortable using dicts. But I'd put the content in lists as a list instead of a dict will keep it clean and less redundant:</p>
<pre><code>[
{
"name": "groupa",
"description": "bla",
"members": [{"name": "usera", "age": 38},
{"name": "userb","age": 20}]
},
{
"name": "groupb"... | 1 | 2009-09-08T13:06:05Z | [
"python"
] |
Need advice how to represent a certain datastructure in Python | 1,393,849 | <p>I'm not sure how to represent a certain datastructure in Python. It consists of groups and users where each user must be a member of exactly one group and groups should be in turn contained in a container, groups and users will only be used within this container. Furthermore I need random access to groups and users.... | 1 | 2009-09-08T12:54:38Z | 1,394,404 | <p>I think an abundance of behavior-less classes, in a multi-paradigm language (one like C++ or Python, that while supporting classes doesn't constrain you to use them when simpler structures will do), is a "design smell" -- the design equivalent of a <a href="http://en.wikipedia.org/wiki/Code%5Fsmell" rel="nofollow">"... | 5 | 2009-09-08T14:41:33Z | [
"python"
] |
Need advice how to represent a certain datastructure in Python | 1,393,849 | <p>I'm not sure how to represent a certain datastructure in Python. It consists of groups and users where each user must be a member of exactly one group and groups should be in turn contained in a container, groups and users will only be used within this container. Furthermore I need random access to groups and users.... | 1 | 2009-09-08T12:54:38Z | 1,402,287 | <p>To echo Alex's answer.... these nested classes reek of code smell to me. </p>
<p>Simpler maybe:</p>
<pre><code>def Group(name=None,description=None,members=None):
if name is None:
name = "UNK!" # some reasonable default
if members is None:
members = dict()
return dict(name = ...., me... | 0 | 2009-09-09T21:44:55Z | [
"python"
] |
Change Vim command to work in MS-Windows? Use make to check python syntax | 1,394,058 | <p><code>:make</code> provides a list of errors which can be navigated through in order to fix.</p>
<p>The problem is that <a href="http://vim.wikia.com/wiki/Python%5F-%5Fcheck%5Fsyntax%5Fand%5Frun%5Fscript" rel="nofollow">this script only works in Unix based OSes.</a></p>
<pre><code>autocmd BufRead *.py set makeprg=... | 2 | 2009-09-08T13:31:54Z | 1,394,158 | <p>For the first part to work, you must first add python to your PATH variable.</p>
<p><a href="http://vlaurie.com/computers2/Articles/environment.htm" rel="nofollow">http://vlaurie.com/computers2/Articles/environment.htm</a></p>
<p>python.exe should be placed in:</p>
<pre><code>c:\PythonXX\bin
</code></pre>
<p>But... | 1 | 2009-09-08T13:52:09Z | [
"python",
"windows",
"vim",
"windows-xp"
] |
In Python, what is the best way to execute a local Linux command stored in a string? | 1,394,198 | <p>In Python, what is the simplest way to execute a local Linux command stored in a string while catching any potential exceptions that are thrown and logging the output of the Linux command and any caught errors to a common log file? </p>
<pre><code>String logfile = â/dev/logâ
String cmd = âlsâ
#try
#execut... | 6 | 2009-09-08T14:00:48Z | 1,394,233 | <p>subprocess is the best module for this.</p>
<p>You have different ways to run you scripts, in separate threads, or in the same waiting for each command to finish. Check the whole docs that are more than useful:</p>
<p><a href="http://docs.python.org/library/subprocess.html" rel="nofollow">http://docs.python.org/li... | 0 | 2009-09-08T14:06:05Z | [
"python"
] |
In Python, what is the best way to execute a local Linux command stored in a string? | 1,394,198 | <p>In Python, what is the simplest way to execute a local Linux command stored in a string while catching any potential exceptions that are thrown and logging the output of the Linux command and any caught errors to a common log file? </p>
<pre><code>String logfile = â/dev/logâ
String cmd = âlsâ
#try
#execut... | 6 | 2009-09-08T14:00:48Z | 1,394,253 | <p>Using the <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen">subprocess</a> module is the correct way to do it:</p>
<pre><code>import subprocess
logfile = open("/dev/log", "w")
output, error = subprocess.Popen(
["ls"], stdout=subprocess.PIPE,
stderr=sub... | 13 | 2009-09-08T14:10:02Z | [
"python"
] |
In Python, what is the best way to execute a local Linux command stored in a string? | 1,394,198 | <p>In Python, what is the simplest way to execute a local Linux command stored in a string while catching any potential exceptions that are thrown and logging the output of the Linux command and any caught errors to a common log file? </p>
<pre><code>String logfile = â/dev/logâ
String cmd = âlsâ
#try
#execut... | 6 | 2009-09-08T14:00:48Z | 1,394,343 | <p>Check out <code>commands</code> module.</p>
<pre><code> import commands
f = open('logfile.log', 'w')
try:
exe = 'ls'
content = commands.getoutput(exe)
f.write(content)
except Exception, text:
f.write(text)
f.close()
</code></pre>
<p>Specifying <code>Exception</cod... | -2 | 2009-09-08T14:29:04Z | [
"python"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,394,512 | <pre><code>def lower_getter(field):
def _getter(obj):
return obj[field].lower()
return _getter
list_of_dicts.sort(key=lower_getter(key_field))
</code></pre>
| 2 | 2009-09-08T15:02:38Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,394,530 | <p>How about this:</p>
<pre><code>list_of_dicts.sort(key=lambda a: a['name'].lower())
</code></pre>
| 11 | 2009-09-08T15:06:47Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,394,609 | <p>In the general case, you'll want to write your key-extraction function for sorting purposes; only in special (though important) cases it happens that you can just reuse an existing callable to extract the keys for you, or just conjoin a couple of existing ones (in a "quick and dirty" way using <code>lambda</code>, s... | 9 | 2009-09-08T15:19:54Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,394,862 | <p>You probably should go with a lambda for the sake of readability. But as an interesting study into higher order functions, here's the extended version of q-combinator in Python (also known as the queer bird combinator). This allows you to create a new function by composing two functions</p>
<pre><code> def compose(... | 4 | 2009-09-08T16:11:40Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,395,099 | <p>This solution will use your system locale, and as a bonus, it will sort eventual other characters according to the current locale as well (Will put "ü" after "u" in a german locale etc).</p>
<pre><code>from locale import setlocale, strxfrm, LC_ALL
import operator
# call setlocale to init current locale
setlocale(... | 4 | 2009-09-08T17:03:17Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 1,396,477 | <p>Personally, I wish there were two functions in the Python standard library (probably in functools):</p>
<pre><code>def compose(*funcs):
"""
Compose any number of unary functions into a single unary
function.
>>> import textwrap
>>> str.strip(textwrap.dedent(compose.__doc__)) == compose(str.s... | 4 | 2009-09-08T21:41:22Z | [
"python",
"function",
"sorting",
"key"
] |
python: combine sort-key-functions itemgetter and str.lower | 1,394,475 | <p>I want to sort a list of dictionaries by dictionary key, where I don't want to distinguish between upper and lower case characters.</p>
<pre><code>dict1 = {'name':'peter','phone':'12355'}
dict2 = {'name':'Paul','phone':'545435'}
dict3 = {'name':'klaus','phone':'55345'}
dict4 = {'name':'Krishna','phone':'12345'}
dic... | 5 | 2009-09-08T14:56:24Z | 7,810,937 | <pre><code>from functools import partial
def nested_funcs(*funcs):
return partial(reduce, lambda arg, func: func(arg), funcs)
sorted(list_of_dicts, key=nested_funcs(itemgetter('name'), str.strip, str.lower))
</code></pre>
| 3 | 2011-10-18T16:58:26Z | [
"python",
"function",
"sorting",
"key"
] |
How do I propagate C++ exceptions to Python in a SWIG wrapper library? | 1,394,484 | <p>I'm writing a SWIG wrapper around a custom C++ library which defines its own C++ exception types. The library's exception types are richer and more specific than standard exceptions. (For example, one class represents parse errors and has a collection of line numbers.) How do I propagate those exceptions back to Pyt... | 20 | 2009-09-08T14:57:44Z | 1,394,531 | <p>From the <a href="http://www.swig.org/Doc1.1/HTML/Exceptions.html" rel="nofollow">swig documentation</a> </p>
<pre><code>%except(python) {
try {
$function
}
catch (RangeError) {
PyErr_SetString(PyExc_IndexError,"index out-of-bounds");
return NULL;
}
}
</code></pre>
| 0 | 2009-09-08T15:06:53Z | [
"python",
"exception",
"swig"
] |
How do I propagate C++ exceptions to Python in a SWIG wrapper library? | 1,394,484 | <p>I'm writing a SWIG wrapper around a custom C++ library which defines its own C++ exception types. The library's exception types are richer and more specific than standard exceptions. (For example, one class represents parse errors and has a collection of line numbers.) How do I propagate those exceptions back to Pyt... | 20 | 2009-09-08T14:57:44Z | 1,394,539 | <p>Is the <a href="http://www.swig.org/Doc1.1/HTML/Exceptions.html" rel="nofollow">swig exception documentation</a> any help? It mentions <a href="http://www.swig.org/Doc1.1/HTML/Exceptions.html#n5" rel="nofollow">defining different exception handlers</a>..</p>
| 0 | 2009-09-08T15:08:15Z | [
"python",
"exception",
"swig"
] |
How do I propagate C++ exceptions to Python in a SWIG wrapper library? | 1,394,484 | <p>I'm writing a SWIG wrapper around a custom C++ library which defines its own C++ exception types. The library's exception types are richer and more specific than standard exceptions. (For example, one class represents parse errors and has a collection of line numbers.) How do I propagate those exceptions back to Pyt... | 20 | 2009-09-08T14:57:44Z | 1,513,274 | <p>I know this question is a few weeks old but I just found it as I was researching a solution for myself. So I'll take a stab at an answer, but I'll warn in advance it may not be an attractive solution since swig interface files can be more complicated than hand coding the wrapper. Also, as far as I can tell, the swig... | 15 | 2009-10-03T09:10:42Z | [
"python",
"exception",
"swig"
] |
How do I propagate C++ exceptions to Python in a SWIG wrapper library? | 1,394,484 | <p>I'm writing a SWIG wrapper around a custom C++ library which defines its own C++ exception types. The library's exception types are richer and more specific than standard exceptions. (For example, one class represents parse errors and has a collection of line numbers.) How do I propagate those exceptions back to Pyt... | 20 | 2009-09-08T14:57:44Z | 14,073,400 | <p>I'll add a bit here, since the example given here now says that "%except(python)" is
deprecated...</p>
<p>You can now (as of swig 1.3.40, anyhow) do totally generic, script-language-independent translation. My example would be:</p>
<pre><code>%exception {
try {
$action
} catch (myException &e... | 5 | 2012-12-28T17:34:49Z | [
"python",
"exception",
"swig"
] |
How do I propagate C++ exceptions to Python in a SWIG wrapper library? | 1,394,484 | <p>I'm writing a SWIG wrapper around a custom C++ library which defines its own C++ exception types. The library's exception types are richer and more specific than standard exceptions. (For example, one class represents parse errors and has a collection of line numbers.) How do I propagate those exceptions back to Pyt... | 20 | 2009-09-08T14:57:44Z | 30,078,393 | <p>U can also use:</p>
<p>catches: <a href="http://www.swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_catches" rel="nofollow">http://www.swig.org/Doc3.0/SWIGPlus.html#SWIGPlus_catches</a></p>
<p>Example:</p>
<pre class="lang-cpp prettyprint-override"><code>%catches(std::exception, std::string, int, ...);
</code></pre>
<p>w... | -1 | 2015-05-06T13:34:38Z | [
"python",
"exception",
"swig"
] |
How do I copy a remote image in python? | 1,394,721 | <p>I need to copy a remote image (for example <code>http://example.com/image.jpg</code>) to my server. Is this possible?</p>
<p>How do you verify that this is indeed an image?</p>
| 14 | 2009-09-08T15:40:05Z | 1,394,732 | <p>Downloading stuff</p>
<pre><code>import urllib
url = "http://example.com/image.jpg"
fname = "image.jpg"
urllib.urlretrieve( url, fname )
</code></pre>
<p>Verifying that it is a image can be done in many ways. The hardest check is opening the file with the Python Image Library and see if it throws an error. </p>
<... | 5 | 2009-09-08T15:43:14Z | [
"python",
"download",
"file-copying"
] |
How do I copy a remote image in python? | 1,394,721 | <p>I need to copy a remote image (for example <code>http://example.com/image.jpg</code>) to my server. Is this possible?</p>
<p>How do you verify that this is indeed an image?</p>
| 14 | 2009-09-08T15:40:05Z | 1,394,744 | <p>To download:</p>
<pre><code>import urllib2
img = urllib2.urlopen("http://example.com/image.jpg").read()
</code></pre>
<p>To verify can use <a href="http://www.pythonware.com/products/pil/">PIL</a></p>
<pre><code>import StringIO
from PIL import Image
try:
im = Image.open(StringIO.StringIO(img))
im.verify()... | 28 | 2009-09-08T15:45:41Z | [
"python",
"download",
"file-copying"
] |
How do I copy a remote image in python? | 1,394,721 | <p>I need to copy a remote image (for example <code>http://example.com/image.jpg</code>) to my server. Is this possible?</p>
<p>How do you verify that this is indeed an image?</p>
| 14 | 2009-09-08T15:40:05Z | 1,645,960 | <p>Same thing using httplib2...</p>
<pre><code>from PIL import Image
from StringIO import StringIO
from httplib2 import Http
# retrieve image
http = Http()
request, content = http.request('http://www.server.com/path/to/image.jpg')
im = Image.open(StringIO(content))
# is it valid?
try:
im.verify()
except Exceptio... | 1 | 2009-10-29T19:21:04Z | [
"python",
"download",
"file-copying"
] |
How do I copy a remote image in python? | 1,394,721 | <p>I need to copy a remote image (for example <code>http://example.com/image.jpg</code>) to my server. Is this possible?</p>
<p>How do you verify that this is indeed an image?</p>
| 14 | 2009-09-08T15:40:05Z | 28,306,886 | <p>For the portion of the question with respect to <strong>copying</strong> a remote image, here's an answer inspired by <a href="http://stackoverflow.com/a/8221125/1497596">this answer</a>:</p>
<pre><code>import urllib2
import shutil
url = 'http://dummyimage.com/100' # returns a dynamically generated PNG
local_file_... | 0 | 2015-02-03T19:13:43Z | [
"python",
"download",
"file-copying"
] |
Converting vb.net code to python for educational purposes. Output of a numeric value not occuring | 1,394,737 | <p>My day job is mainly coding in vb.net, so I am very familiar with it. While doing my first few dozen project euler problems, I used vb.net just to get the hang of the problem styles. Now I'd like to use project euler to help me learn a new language and have been running a couple in python. However. I've hit a sna... | 4 | 2009-09-08T15:43:48Z | 1,394,817 | <p>Previous solutions generate wrong answer.</p>
<ol>
<li>VB.net code operates on integers, and your Python code operates on floats, and this apparently fails somewhere.</li>
<li>As mentioned before, keyword capitalization (True/False).</li>
<li>You can use foo % bar == 0 with no problem.</li>
<li>You missed one level... | 2 | 2009-09-08T16:01:22Z | [
"python",
"vb.net"
] |
How to do "hit any key" in python? | 1,394,956 | <p>How would I do a "hit any key" (or grab a menu option) in Python?</p>
<ul>
<li>raw_input requires you hit return.</li>
<li>Windows msvcrt has getch() and getche().</li>
</ul>
<p>Is there a portable way to do this using the standard libs?</p>
| 24 | 2009-09-08T16:32:27Z | 1,394,975 | <p>From the <a href="http://www.python.org/doc/faq/library/#how-do-i-get-a-single-keypress-at-a-time" rel="nofollow">python docs</a>:</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 &... | 4 | 2009-09-08T16:37:11Z | [
"python"
] |
How to do "hit any key" in python? | 1,394,956 | <p>How would I do a "hit any key" (or grab a menu option) in Python?</p>
<ul>
<li>raw_input requires you hit return.</li>
<li>Windows msvcrt has getch() and getche().</li>
</ul>
<p>Is there a portable way to do this using the standard libs?</p>
| 24 | 2009-09-08T16:32:27Z | 1,394,994 | <pre><code>try:
# Win32
from msvcrt import getch
except ImportError:
# UNIX
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1)
finally:
termios.... | 30 | 2009-09-08T16:41:04Z | [
"python"
] |
How to do "hit any key" in python? | 1,394,956 | <p>How would I do a "hit any key" (or grab a menu option) in Python?</p>
<ul>
<li>raw_input requires you hit return.</li>
<li>Windows msvcrt has getch() and getche().</li>
</ul>
<p>Is there a portable way to do this using the standard libs?</p>
| 24 | 2009-09-08T16:32:27Z | 1,395,006 | <pre><code>try:
os.system('pause') #windows, doesn't require enter
except whatever_it_is:
os.system('read -p "Press any key to continue"') #linux
</code></pre>
| 9 | 2009-09-08T16:43:11Z | [
"python"
] |
How to do "hit any key" in python? | 1,394,956 | <p>How would I do a "hit any key" (or grab a menu option) in Python?</p>
<ul>
<li>raw_input requires you hit return.</li>
<li>Windows msvcrt has getch() and getche().</li>
</ul>
<p>Is there a portable way to do this using the standard libs?</p>
| 24 | 2009-09-08T16:32:27Z | 11,932,011 | <p>on linux platform, I use <code>os.system</code> to call <code>/sbin/getkey</code> command, e.g.</p>
<pre><code>continue_ = os.system('/sbin/getkey -m "Please any key within %d seconds to continue..." -c 10')
if continue_:
...
else:
...
</code></pre>
<p>The benefit is it will show an countdown seconds to use... | 0 | 2012-08-13T09:59:58Z | [
"python"
] |
How to do "hit any key" in python? | 1,394,956 | <p>How would I do a "hit any key" (or grab a menu option) in Python?</p>
<ul>
<li>raw_input requires you hit return.</li>
<li>Windows msvcrt has getch() and getche().</li>
</ul>
<p>Is there a portable way to do this using the standard libs?</p>
| 24 | 2009-09-08T16:32:27Z | 33,966,614 | <p>A couple years ago I wrote a small library to do this in a cross-platform way (inspired directly by John Millikin's answer above). In addition to <code>getch</code>, it comes with a <code>pause</code> function that prints <code>'Press any key to continue . . .'</code>:</p>
<pre><code>pause()
</code></pre>
<p>You c... | 2 | 2015-11-28T01:06:57Z | [
"python"
] |
Parsing SQL with Python | 1,394,998 | <p>I want to create a SQL interface on top of a non-relational data store. Non-relational data store, but it makes sense to access the data in a relational manner.</p>
<p>I am looking into using <a href="http://www.antlr.org/">ANTLR</a> to produce an AST that represents the SQL as a relational algebra expression. Then... | 29 | 2009-09-08T16:42:03Z | 1,395,013 | <p><a href="http://www.reddit.com/r/Python/comments/99i6u/help%5Fpythonreddit%5Fseeking%5Fa%5Fsql%5Fparser/">This reddit post</a> suggests <a href="http://code.google.com/p/python-sqlparse/">Python-sqlparse</a> as an existing implementation, among a couple other links.</p>
| 5 | 2009-09-08T16:44:31Z | [
"python",
"sql",
"parsing",
"pyparsing"
] |
Parsing SQL with Python | 1,394,998 | <p>I want to create a SQL interface on top of a non-relational data store. Non-relational data store, but it makes sense to access the data in a relational manner.</p>
<p>I am looking into using <a href="http://www.antlr.org/">ANTLR</a> to produce an AST that represents the SQL as a relational algebra expression. Then... | 29 | 2009-09-08T16:42:03Z | 1,395,854 | <p>I have looked into this issue quite extensively. Python-sqlparse is a non validating parser which is not really what you need. The examples in antlr need a lot of work to convert to a nice ast in python. The sql standard grammers are <a href="http://savage.net.au/SQL/">here</a>, but it would be a full time job to... | 25 | 2009-09-08T19:25:54Z | [
"python",
"sql",
"parsing",
"pyparsing"
] |
Parsing SQL with Python | 1,394,998 | <p>I want to create a SQL interface on top of a non-relational data store. Non-relational data store, but it makes sense to access the data in a relational manner.</p>
<p>I am looking into using <a href="http://www.antlr.org/">ANTLR</a> to produce an AST that represents the SQL as a relational algebra expression. Then... | 29 | 2009-09-08T16:42:03Z | 4,866,734 | <p>Of course, it may be best to leverage <a href="http://code.google.com/p/python-sqlparse" rel="nofollow">python-sqlparse on Google Code</a></p>
<p>UPDATE: Now I see that this has been suggested - I concur that this is worthwhile:</p>
| 0 | 2011-02-01T18:55:08Z | [
"python",
"sql",
"parsing",
"pyparsing"
] |
How can I make `bin(30)` return `00011110` instead of `0b11110`? | 1,395,356 | <p>What does the "b" stand for in the output of bin(30): "0b11110"? Is there any way I can get rid of this "b"? How can I get the output of bin() to always return a standard 8 digit output?</p>
| 15 | 2009-09-08T17:53:38Z | 1,395,376 | <p>0b is like 0x - it indicates the number is formatted in binary (0x indicates the number is in hex).</p>
<p>See <a href="http://stackoverflow.com/questions/1476/how-do-you-express-binary-literals-in-python">How do you express binary literals in python?</a></p>
<p>See <a href="http://docs.python.org/dev/whatsnew/2.6... | 21 | 2009-09-08T17:56:26Z | [
"python"
] |
How can I make `bin(30)` return `00011110` instead of `0b11110`? | 1,395,356 | <p>What does the "b" stand for in the output of bin(30): "0b11110"? Is there any way I can get rid of this "b"? How can I get the output of bin() to always return a standard 8 digit output?</p>
| 15 | 2009-09-08T17:53:38Z | 1,395,408 | <p>Using <a href="http://docs.python.org/library/stdtypes.html#str.zfill">zfill()</a>:</p>
<blockquote>
<p>Return the numeric string left filled with zeros in a string of length width. A sign prefix is handled correctly. The original string is returned if width is less than len(s).</p>
</blockquote>
<pre><code>>... | 29 | 2009-09-08T18:02:31Z | [
"python"
] |
How can I make `bin(30)` return `00011110` instead of `0b11110`? | 1,395,356 | <p>What does the "b" stand for in the output of bin(30): "0b11110"? Is there any way I can get rid of this "b"? How can I get the output of bin() to always return a standard 8 digit output?</p>
| 15 | 2009-09-08T17:53:38Z | 22,863,621 | <pre><code>>>> print format(30, 'b')
11110
>>> print format(30, 'b').zfill(8)
00011110
</code></pre>
<p>Should do. Here <code>'b'</code> stands for binary just like <code>'x'</code>, <code>'o'</code> & <code>'d'</code> for hexadecimal, octal and decimal respectively. </p>
| 8 | 2014-04-04T13:02:10Z | [
"python"
] |
Managing resources in a Python project | 1,395,593 | <p>I have a Python project in which I am using many non-code files. Currently these are all images, but I might use other kinds of files in the future. What would be a good scheme for storing and referencing these files?</p>
<p>I considered just making a folder "resources" in the main directory, but there is a problem... | 15 | 2009-09-08T18:37:39Z | 1,395,673 | <p>You can always have a separate "resources" folder in each subpackage which needs it, and use <code>os.path</code> functions to get to these from the <code>__file__</code> values of your subpackages. To illustrate what I mean, I created the following <code>__init__.py</code> file in three locations:</p>
<pre>
c:\tem... | 3 | 2009-09-08T18:50:26Z | [
"python",
"resources",
"setuptools",
"distutils",
"decoupling"
] |
Managing resources in a Python project | 1,395,593 | <p>I have a Python project in which I am using many non-code files. Currently these are all images, but I might use other kinds of files in the future. What would be a good scheme for storing and referencing these files?</p>
<p>I considered just making a folder "resources" in the main directory, but there is a problem... | 15 | 2009-09-08T18:37:39Z | 1,396,657 | <p>You may want to use <code>pkg_resources</code> library that comes with <code>setuptools</code>.</p>
<p>For example, I've made up a quick little package <code>"proj"</code> to illustrate the resource organization scheme I'd use:</p>
<pre>proj/setup.py
proj/proj/__init__.py
proj/proj/code.py
proj/proj/resources/__in... | 17 | 2009-09-08T22:22:05Z | [
"python",
"resources",
"setuptools",
"distutils",
"decoupling"
] |
Managing resources in a Python project | 1,395,593 | <p>I have a Python project in which I am using many non-code files. Currently these are all images, but I might use other kinds of files in the future. What would be a good scheme for storing and referencing these files?</p>
<p>I considered just making a folder "resources" in the main directory, but there is a problem... | 15 | 2009-09-08T18:37:39Z | 1,472,725 | <p>@ pycon2009, there was a presentation on distutils and setuptools. You can find all of the videos here</p>
<p><a href="http://pycon.blip.tv/file/2061520/" rel="nofollow">Eggs and Buildout Deployment in Python - Part 1</a></p>
<p><a href="http://pycon.blip.tv/file/2061678/" rel="nofollow">Eggs and Buildout Deployme... | 1 | 2009-09-24T16:16:28Z | [
"python",
"resources",
"setuptools",
"distutils",
"decoupling"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,395,613 | <p><code>raw_input</code> is stored as a string, not an integer.</p>
<p>Try using <code>a = int(a)</code> before performing comparisons. </p>
| 2 | 2009-09-08T18:41:28Z | [
"python"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,395,615 | <pre><code>raw_input
</code></pre>
<p>returns a string so you need to convert <code>a</code> which is a string to an integer first: <code>a = int(a)</code></p>
| 5 | 2009-09-08T18:41:36Z | [
"python"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,395,617 | <p>That's because <code>a</code> is a string as inputted. Use <code>int()</code> to convert it to an integer before doing numeric comparisons.</p>
<pre><code>a = int(raw_input('Enter a number: '))
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>Alte... | 7 | 2009-09-08T18:42:13Z | [
"python"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,395,623 | <p>raw input will return a string, not an integer. To convert it, try adding this line immediately after your raw_input statement:</p>
<p>a = int(a)</p>
<p>This will convert the string to an integer. You can crash it by giving it non-numeric data, though, so be careful.</p>
| 1 | 2009-09-08T18:42:36Z | [
"python"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,395,635 | <p>Because you are using <code>raw_input</code> you are getting the value as a String, which is always considered greater than 0 (even if the String is '-10')</p>
<p>Instead, try using input('Enter a number: ') and python will do the type conversion for you.</p>
<p>The final code would look like this:</p>
<pre><code... | 7 | 2009-09-08T18:43:50Z | [
"python"
] |
Trouble with simple Python Code | 1,395,603 | <p>I'm learning Python, and I'm having trouble with this simple piece of code:</p>
<pre><code>a = raw_input('Enter a number: ')
if a > 0:
print 'Positive'
elif a == 0:
print 'Null'
elif a < 0:
print 'Negative'
</code></pre>
<p>It works great, apart from the fact that it always prints 'Positive', no... | 4 | 2009-09-08T18:39:48Z | 1,396,057 | <p>Expanding on my comment on the <a href="http://stackoverflow.com/questions/1395603/trouble-with-simple-python-code/1395635#1395635">accepted answer</a>, here's how I would do it.</p>
<pre><code>value = None
getting_input = True
while getting_input:
try:
value = int(raw_input('Gimme a number: '))
... | 7 | 2009-09-08T20:07:12Z | [
"python"
] |
Pervasive 8 and Python | 1,395,786 | <p>Is it possible to access a Pervasive 8 (not Pervasive SQL) database from python?</p>
| 1 | 2009-09-08T19:10:59Z | 1,397,698 | <p>I'm not familiar with Pervasive, but from a quick web search it looks like people are using some kind of ODBC driver to access Pervasive 8.</p>
<p>ODBC databases can be used from python in Windows using PyODBC: <a href="http://code.google.com/p/pyodbc/" rel="nofollow">http://code.google.com/p/pyodbc/</a></p>
| 2 | 2009-09-09T05:30:53Z | [
"python",
"database",
"pervasive"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.