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 |
|---|---|---|---|---|---|---|---|---|---|
Django MultiWidget Phone Number Field | 1,777,435 | <p>I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the fields in my form during initial rendering (it happens when the for... | 6 | 2009-11-22T01:17:29Z | 1,779,509 | <p>I took hughdbrown's advise and modified USPhoneNumberField to do what I need. The reason I didn't use it initially was that it stores phone numbers as XXX-XXX-XXXX in the DB, I store them as XXXXXXXXXX. So I over-rode the clean method:</p>
<pre><code>class PhoneNumberField(USPhoneNumberField):
def clean(self,... | 1 | 2009-11-22T18:26:08Z | [
"python",
"django-templates",
"django-forms",
"django-widget",
"django-models"
] |
Django MultiWidget Phone Number Field | 1,777,435 | <p>I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the fields in my form during initial rendering (it happens when the for... | 6 | 2009-11-22T01:17:29Z | 1,912,686 | <p>This uses <code>widget.value_from_datadict()</code> to format the data so no need to subclass a field, just use the existing <code>USPhoneNumberField</code>. Data is stored in db like XXX-XXX-XXXX.</p>
<pre><code>from django import forms
class USPhoneNumberMultiWidget(forms.MultiWidget):
"""
A Widget that ... | 5 | 2009-12-16T06:21:16Z | [
"python",
"django-templates",
"django-forms",
"django-widget",
"django-models"
] |
Django MultiWidget Phone Number Field | 1,777,435 | <p>I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the fields in my form during initial rendering (it happens when the for... | 6 | 2009-11-22T01:17:29Z | 4,014,410 | <p>Sometimes it is useful to fix the original problem rather than redoing everything. The error you got, "Caught an exception while rendering: 'NoneType' object is unsubscriptable" has a clue. There is a value returned as None(unsubscriptable) when a subscriptable value is expected. The decompress function in PhoneNumb... | 0 | 2010-10-25T12:14:34Z | [
"python",
"django-templates",
"django-forms",
"django-widget",
"django-models"
] |
Django MultiWidget Phone Number Field | 1,777,435 | <p>I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the fields in my form during initial rendering (it happens when the for... | 6 | 2009-11-22T01:17:29Z | 4,025,461 | <p>I think the value_from_datadict() code can be simplified to:</p>
<pre><code>
class USPhoneNumberMultiWidget(forms.MultiWidget):
"""
A Widget that splits US Phone number input into three boxes.
"""
def __init__(self,attrs=None):
widgets = (
forms.TextInput(attrs={'size':'3','maxl... | 3 | 2010-10-26T15:55:22Z | [
"python",
"django-templates",
"django-forms",
"django-widget",
"django-models"
] |
Why do I get this error in this Python code? | 1,777,592 | <p>I am trying to extract the first words in a file by Python.</p>
<p><strong>My code</strong></p>
<pre><code>import re
con1 = pg.DB('tk', 'localhost', 5432, None, None, 'masi', '123')
f1="/home/masi/fy.txt"
print re.findall(r"\w+", f1.read())
</code></pre>
<p>I get the error</p>
<pre><code>Traceb... | 0 | 2009-11-22T02:15:56Z | 1,777,593 | <p><code>f1.read()</code> should be <code>open(f1).read()</code></p>
| 5 | 2009-11-22T02:17:31Z | [
"python"
] |
Why do I get this error in this Python code? | 1,777,592 | <p>I am trying to extract the first words in a file by Python.</p>
<p><strong>My code</strong></p>
<pre><code>import re
con1 = pg.DB('tk', 'localhost', 5432, None, None, 'masi', '123')
f1="/home/masi/fy.txt"
print re.findall(r"\w+", f1.read())
</code></pre>
<p>I get the error</p>
<pre><code>Traceb... | 0 | 2009-11-22T02:15:56Z | 1,777,595 | <p>I don't know Python but it looks like you need to open the file which is </p>
<pre><code>f=open('/tmp/workfile', 'r')
</code></pre>
<p>According to <a href="http://www.python.org/doc/2.5.2/tut/node9.html#SECTION009210000000000000000" rel="nofollow">this site</a></p>
| 4 | 2009-11-22T02:18:02Z | [
"python"
] |
Why do I get this error in this Python code? | 1,777,592 | <p>I am trying to extract the first words in a file by Python.</p>
<p><strong>My code</strong></p>
<pre><code>import re
con1 = pg.DB('tk', 'localhost', 5432, None, None, 'masi', '123')
f1="/home/masi/fy.txt"
print re.findall(r"\w+", f1.read())
</code></pre>
<p>I get the error</p>
<pre><code>Traceb... | 0 | 2009-11-22T02:15:56Z | 1,777,611 | <p>When you assign f1 to the filepath, you are actually saying the f1 is the string referring to the filepath. Instead, if you were to assign it to the return value of the 'open' method called on that filepath (as Jonathan suggests), then f1 would be an open file.</p>
| 0 | 2009-11-22T02:24:41Z | [
"python"
] |
Python importing modules differently when run interactively/non-interactively | 1,777,671 | <p>One of my Python scripts runs in interactive mode but fails when run from the command line. The difference is that when run from the command line, it imports modules from a bad .egg file, and when run interactively it uses my fixed (unzipped) version in the current directory.</p>
<p>My question is two-fold: a) why ... | 1 | 2009-11-22T02:51:52Z | 1,777,700 | <p>On UNIX systems and Mac OS-X:</p>
<ul>
<li>Do you have a <code>~/.python-eggs</code> directory?</li>
</ul>
<p>OS independent:</p>
<ul>
<li><p>Are you sure that you use the same Python instance in both cases?</p></li>
<li><p>Can you print <code>sys.path</code> in each cases and see which package directory comes fi... | 2 | 2009-11-22T03:05:42Z | [
"python",
"interactive",
"egg",
"non-interactive"
] |
Python importing modules differently when run interactively/non-interactively | 1,777,671 | <p>One of my Python scripts runs in interactive mode but fails when run from the command line. The difference is that when run from the command line, it imports modules from a bad .egg file, and when run interactively it uses my fixed (unzipped) version in the current directory.</p>
<p>My question is two-fold: a) why ... | 1 | 2009-11-22T02:51:52Z | 1,777,968 | <p>I don't understand what do you mean by running script in interactive mode, so I can't say exactly. But the first place to look for modules (<code>sys.path[0]</code>) in interactive mode is current directory (even calling <code>os.chdir()</code> will affect imports), while for script it's directory where the script i... | 1 | 2009-11-22T06:08:45Z | [
"python",
"interactive",
"egg",
"non-interactive"
] |
Python importing modules differently when run interactively/non-interactively | 1,777,671 | <p>One of my Python scripts runs in interactive mode but fails when run from the command line. The difference is that when run from the command line, it imports modules from a bad .egg file, and when run interactively it uses my fixed (unzipped) version in the current directory.</p>
<p>My question is two-fold: a) why ... | 1 | 2009-11-22T02:51:52Z | 1,778,097 | <p>a) why does Python load modules differently when run from these locations
b) what are my options to work around it?</p>
<p>Check your environment variable PYTHONPATH. When python imports module, it searches those directories. One way to get around your problem is to add your local folder "the (unzipped) version in... | 1 | 2009-11-22T07:25:26Z | [
"python",
"interactive",
"egg",
"non-interactive"
] |
Python importing & using cdll (with a linux .so file) | 1,777,752 | <p>After one of my last questions about python&c++ integration i was told to use dlls at windows.
<a href="http://stackoverflow.com/questions/1659159/python-executing-existent-big-c-code">(Previous question)</a></p>
<p>That worked ok doing:</p>
<p><code>cl /LD A.cpp B.cpp C.pp</code></p>
<p>in windows enviroment... | 1 | 2009-11-22T03:50:29Z | 1,777,817 | <p>C++ compiler mangles names of functions. To do what you are trying to do you must have the declaration prototype inside </p>
<pre><code>extern "C" {...}
</code></pre>
<p>it's hard to tell from your samples what exactly you have in a source file.
As someone already mentioned, use nm utility to see what objects tha... | 2 | 2009-11-22T04:33:37Z | [
"c++",
"python",
"linux",
"file",
"shared-libraries"
] |
Python importing & using cdll (with a linux .so file) | 1,777,752 | <p>After one of my last questions about python&c++ integration i was told to use dlls at windows.
<a href="http://stackoverflow.com/questions/1659159/python-executing-existent-big-c-code">(Previous question)</a></p>
<p>That worked ok doing:</p>
<p><code>cl /LD A.cpp B.cpp C.pp</code></p>
<p>in windows enviroment... | 1 | 2009-11-22T03:50:29Z | 1,777,821 | <p>just to doublecheck something since you using boost.</p>
<pre><code>#include <string>
#include <boost/python.hpp>
using namespace std;
string hello(string s){
return "Hello World!";
}
BOOST_PYTHON_MODULE(pyhello){
using namespace boost::python;
def("hello", hello);
}
</code></pre>
<p>in ... | 1 | 2009-11-22T04:36:35Z | [
"c++",
"python",
"linux",
"file",
"shared-libraries"
] |
How to get sqlalchemy length of a string column | 1,777,814 |
<p>Consider this simple table definition (using SQLAlchemy-0.5.6)</p>
<pre class="lang-py prettyprint-override"><code>from sqlalchemy import *
db = create_engine('sqlite:///tutorial.db')
db.echo = False # Try changing this to True and see what happens
metadata = MetaData(db)
user = Table('user', metadata,
C... | 12 | 2009-11-22T04:32:17Z | 1,777,870 | <p>This should work (tested on my machine) :</p>
<pre><code>print user.columns.name.type.length
</code></pre>
| 3 | 2009-11-22T05:06:37Z | [
"python",
"sqlalchemy"
] |
How to get sqlalchemy length of a string column | 1,777,814 |
<p>Consider this simple table definition (using SQLAlchemy-0.5.6)</p>
<pre class="lang-py prettyprint-override"><code>from sqlalchemy import *
db = create_engine('sqlite:///tutorial.db')
db.echo = False # Try changing this to True and see what happens
metadata = MetaData(db)
user = Table('user', metadata,
C... | 12 | 2009-11-22T04:32:17Z | 1,778,993 | <pre><code>User.name.property.columns[0].type.length
</code></pre>
<p>Note, that SQLAlchemy supports composite properties, that's why <code>columns</code> is a list. It has single item for simple column properties.</p>
| 14 | 2009-11-22T15:17:33Z | [
"python",
"sqlalchemy"
] |
Module import path | 1,777,838 | <p>I'm unable to test-run a cssparser that I'd like to use.</p>
<p><strong>test.py</strong>:</p>
<pre><code>from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p... | 1 | 2009-11-22T04:45:40Z | 1,777,844 | <p>Do you have <code>__init__.py</code> files in the <code>cssparser</code> and <code>css</code> directories, to turn the directories into packages? Is there a <code>css/parse.py</code> file with some function or class named <code>parse</code> in it? What's on your <code>sys.path</code>, and what's the current direct... | 2 | 2009-11-22T04:49:59Z | [
"python",
"import",
"path",
"module"
] |
Module import path | 1,777,838 | <p>I'm unable to test-run a cssparser that I'd like to use.</p>
<p><strong>test.py</strong>:</p>
<pre><code>from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p... | 1 | 2009-11-22T04:45:40Z | 1,777,846 | <p>Does your <code>css/</code> have an <code>__init__.py</code>?</p>
| 0 | 2009-11-22T04:51:06Z | [
"python",
"import",
"path",
"module"
] |
Module import path | 1,777,838 | <p>I'm unable to test-run a cssparser that I'd like to use.</p>
<p><strong>test.py</strong>:</p>
<pre><code>from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p... | 1 | 2009-11-22T04:45:40Z | 1,777,876 | <p>Here is few steps I just tested.</p>
<ul>
<li><p><a href="http://css-py.googlecode.com/svn/trunk/README" rel="nofollow">readme</a> says its python 2.5, so you need python 2.x series</p></li>
<li><p>I have created a folder <code>C:/TEST/</code></p></li>
<li><p>I have downloaded all files from <a href="http://css-py.... | 4 | 2009-11-22T05:11:07Z | [
"python",
"import",
"path",
"module"
] |
Module import path | 1,777,838 | <p>I'm unable to test-run a cssparser that I'd like to use.</p>
<p><strong>test.py</strong>:</p>
<pre><code>from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p... | 1 | 2009-11-22T04:45:40Z | 1,777,973 | <p>python 3 version of css-py</p>
<p><a href="http://ifile.it/v32n70s/css.zip" rel="nofollow">http://ifile.it/v32n70s/css.zip</a></p>
| 2 | 2009-11-22T06:09:31Z | [
"python",
"import",
"path",
"module"
] |
Module import path | 1,777,838 | <p>I'm unable to test-run a cssparser that I'd like to use.</p>
<p><strong>test.py</strong>:</p>
<pre><code>from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p... | 1 | 2009-11-22T04:45:40Z | 1,778,166 | <p>Don't call your own file <code>css.py</code>. Name it something else, like <code>css_test.py</code>. Delete the <code>css.pyc</code> file also.</p>
| 0 | 2009-11-22T08:08:08Z | [
"python",
"import",
"path",
"module"
] |
Redirecting Command-line Arguments for Bootstrapping | 1,777,862 | <p>I am trying to rewrite the following program in C instead of C# (which is less portable). It is obvious that "int system ( const char * command )" will be necessary to complete the program. Starting it with "int main ( int argc, char * argv[] )" will allow getting the command-line arguments, but there is still a pro... | 1 | 2009-11-22T05:02:21Z | 1,777,882 | <p>Windows is all messed up. Every program has its own rules.</p>
| 2 | 2009-11-22T05:14:06Z | [
"c#",
"python",
"c",
"arguments",
"screensaver"
] |
Redirecting Command-line Arguments for Bootstrapping | 1,777,862 | <p>I am trying to rewrite the following program in C instead of C# (which is less portable). It is obvious that "int system ( const char * command )" will be necessary to complete the program. Starting it with "int main ( int argc, char * argv[] )" will allow getting the command-line arguments, but there is still a pro... | 1 | 2009-11-22T05:02:21Z | 1,778,541 | <p>The correct way to do this under windows is to use <a href="http://msdn.microsoft.com/en-us/library/7zt1y878%28VS.80%29.aspx" rel="nofollow">_spawnv</a></p>
<p>Its equivalent under unix like OSes is <code>fork()</code> followed by <code>execv</code>.</p>
| 1 | 2009-11-22T12:14:57Z | [
"c#",
"python",
"c",
"arguments",
"screensaver"
] |
Where can I find a (current) example of code to bulk upload data to Google AppEngine? (For localhost, too.) | 1,777,949 | <p>I've tried to do it like they describe in the <a href="http://code.google.com/appengine/docs/python/tools/uploadingdata.html#Downloading%5Fand%5FUploading%5FAll%5FData" rel="nofollow">AppEngine docs</a> but I can't get it to accept my upload. (I haven't tried to download.) I want to get it working in the development... | 0 | 2009-11-22T05:59:38Z | 1,778,427 | <p>The docs have <a href="http://code.google.com/appengine/docs/python/tools/uploadingdata.html" rel="nofollow">a section</a> on uploading and downloading data, with examples. You should be using appcfg.py unless you need one of the features of bulkloader.py that are not yet integrated, such as --dump/--restore functio... | 1 | 2009-11-22T11:01:30Z | [
"python",
"google-app-engine"
] |
expand users table with django | 1,778,025 | <p>I'm using the authentication that ships with django, and as such, it comes with its own SQL table. I have a few more attributes I'd like to use with the User model that are custom to my app such as a user photo or a random user blob where users can type in notes.</p>
<p>what's the best way of extending the existing... | 1 | 2009-11-22T06:45:17Z | 1,778,096 | <p><a href="http://stackoverflow.com/questions/576345/django-project-structure-recommended-structure-to-share-an-extended-auth-user/576362#576362">Please see my (and others) answer to this question:</a> </p>
| 2 | 2009-11-22T07:24:20Z | [
"python",
"django"
] |
expand users table with django | 1,778,025 | <p>I'm using the authentication that ships with django, and as such, it comes with its own SQL table. I have a few more attributes I'd like to use with the User model that are custom to my app such as a user photo or a random user blob where users can type in notes.</p>
<p>what's the best way of extending the existing... | 1 | 2009-11-22T06:45:17Z | 1,779,218 | <p>The other way to handle this is to do table inheritance on the User model and develop your own derived EnhancedUser model. Typically a custom wrapper backend is also written to make sure the auth subsystem returns the EnhancedUser model as well.</p>
<p>There's a very good <a href="http://scottbarnham.com/blog/2008... | 0 | 2009-11-22T16:44:08Z | [
"python",
"django"
] |
Is there precision in this number that Python isn't showing me? | 1,778,179 | <p>I've run into something odd, here, and I'm at a loss -- I have a feeling this has something to do with floating precision, but I'm surprised Python would not display the approximation error, if so.</p>
<p>I'm working on <a href="http://projecteuler.net/index.php?section=problems&id=62" rel="nofollow">Project Eu... | 2 | 2009-11-22T08:19:19Z | 1,778,182 | <p>This is using <code>c.__str__()</code> (aka. <code>str(c)</code>) :</p>
<pre><code>print "c is", c
</code></pre>
<p>This is using <code>c.__repr__()</code> (aka. <code>repr(c)</code>) :</p>
<pre><code>>>> c # In the Python shell
</code></pre>
<p>IIRC, <code>__str__</code> truncates to 10 decimals, where... | 4 | 2009-11-22T08:21:46Z | [
"python"
] |
Is there precision in this number that Python isn't showing me? | 1,778,179 | <p>I've run into something odd, here, and I'm at a loss -- I have a feeling this has something to do with floating precision, but I'm surprised Python would not display the approximation error, if so.</p>
<p>I'm working on <a href="http://projecteuler.net/index.php?section=problems&id=62" rel="nofollow">Project Eu... | 2 | 2009-11-22T08:19:19Z | 1,778,217 | <pre><code> print "c is", int(c)
</code></pre>
<p>The integer conversion will simply drop anything after the decimal point so even if you have 344.99999999999989 it will always round down.
you could use old school print % </p>
<pre><code> print'c is %5.0f' % c
</code></pre>
<p>or you could use round if you want ... | 0 | 2009-11-22T08:54:25Z | [
"python"
] |
Python float - str - float weirdness | 1,778,368 | <pre><code>>>> float(str(0.65000000000000002))
0.65000000000000002
>>> float(str(0.47000000000000003))
0.46999999999999997 ???
</code></pre>
<p>What is going on here?
How do I convert <code>0.47000000000000003</code> to string and the resultant value back to float?</p>
<p>I am using Python 2.... | 6 | 2009-11-22T10:28:18Z | 1,778,378 | <p>float (and double) do not have infinite precision. Naturally, rounding errors occur when you operate on them.</p>
| 3 | 2009-11-22T10:34:45Z | [
"python",
"string",
"floating-point",
"floating-accuracy"
] |
Python float - str - float weirdness | 1,778,368 | <pre><code>>>> float(str(0.65000000000000002))
0.65000000000000002
>>> float(str(0.47000000000000003))
0.46999999999999997 ???
</code></pre>
<p>What is going on here?
How do I convert <code>0.47000000000000003</code> to string and the resultant value back to float?</p>
<p>I am using Python 2.... | 6 | 2009-11-22T10:28:18Z | 1,778,381 | <p><code>str(0.47000000000000003)</code> give <code>'0.47'</code> and <code>float('0.47')</code> can be <code>0.46999999999999997</code>.
This is due to the way floating point number are represented (see this <a href="http://en.wikipedia.org/wiki/IEEE%5F754">wikipedia</a> article)</p>
<p>Note: <code>float(repr(0.4700... | 14 | 2009-11-22T10:35:48Z | [
"python",
"string",
"floating-point",
"floating-accuracy"
] |
Python float - str - float weirdness | 1,778,368 | <pre><code>>>> float(str(0.65000000000000002))
0.65000000000000002
>>> float(str(0.47000000000000003))
0.46999999999999997 ???
</code></pre>
<p>What is going on here?
How do I convert <code>0.47000000000000003</code> to string and the resultant value back to float?</p>
<p>I am using Python 2.... | 6 | 2009-11-22T10:28:18Z | 1,778,486 | <p>This is a <a href="http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate" rel="nofollow">Python FAQ</a></p>
<p>The same question comes up <a href="http://groups.google.co.uk/group/comp.lang.python/search?hl=en&group=comp.lang.python&q=floating+point+faq" rel="nofollow">qui... | 2 | 2009-11-22T11:41:21Z | [
"python",
"string",
"floating-point",
"floating-accuracy"
] |
Python float - str - float weirdness | 1,778,368 | <pre><code>>>> float(str(0.65000000000000002))
0.65000000000000002
>>> float(str(0.47000000000000003))
0.46999999999999997 ???
</code></pre>
<p>What is going on here?
How do I convert <code>0.47000000000000003</code> to string and the resultant value back to float?</p>
<p>I am using Python 2.... | 6 | 2009-11-22T10:28:18Z | 29,963,592 | <p>by this example
I think this is an error in Python when you devide</p>
<pre><code> >>> print(int(((48/5.0)-9)*5))
2
</code></pre>
<p>the easy way, I solve this problem by this</p>
<pre><code> >>> print(int(round(((48/5.0)-9)*5,2)))
3
</code></pre>
| 0 | 2015-04-30T09:10:02Z | [
"python",
"string",
"floating-point",
"floating-accuracy"
] |
What is wrong with the pearson algorithm from âProgramming Collective Intelligenceâ? | 1,778,411 | <p>This function is from the book "Programming Collective Intelligenceâ, and is supposed to calculate the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1.</p>
<p>If two critics rate items very similarly the function should return 1, or close to 1.</p>
<p>With real us... | 5 | 2009-11-22T10:52:58Z | 1,778,429 | <p>There is nothing wrong in your result. You are trying to plot a line through 3 points. In second case you have all three points with the same coordinates, i.e. effectively one point. You can't say do these points correlate or anti-correlate, because you can draw infinite number of lines through one point (<code>den<... | 11 | 2009-11-22T11:01:58Z | [
"python",
"algorithm",
"pearson"
] |
What is wrong with the pearson algorithm from âProgramming Collective Intelligenceâ? | 1,778,411 | <p>This function is from the book "Programming Collective Intelligenceâ, and is supposed to calculate the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1.</p>
<p>If two critics rate items very similarly the function should return 1, or close to 1.</p>
<p>With real us... | 5 | 2009-11-22T10:52:58Z | 1,778,448 | <p>The algorithm gives the correct result. 0 means that there is no correlation between them (or at least you can't tell from what you know).</p>
<p>Generally (depending on what domain you apply this algorithm) you can consider everything between -0.9 < x < 0.09 as "No correlation observable".</p>
| 0 | 2009-11-22T11:17:03Z | [
"python",
"algorithm",
"pearson"
] |
What is wrong with the pearson algorithm from âProgramming Collective Intelligenceâ? | 1,778,411 | <p>This function is from the book "Programming Collective Intelligenceâ, and is supposed to calculate the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1.</p>
<p>If two critics rate items very similarly the function should return 1, or close to 1.</p>
<p>With real us... | 5 | 2009-11-22T10:52:58Z | 1,778,824 | <p>Correlation does not imply causation. Had to say it. You need to develop an understanding of correlation statistics. Correlation can be between -1 and 1 and a value of 0 falls in this range and is a perfectly reasonable result. A correlation of 0 implies that there is not statistically significant relationship betwe... | 0 | 2009-11-22T14:23:11Z | [
"python",
"algorithm",
"pearson"
] |
What is wrong with the pearson algorithm from âProgramming Collective Intelligenceâ? | 1,778,411 | <p>This function is from the book "Programming Collective Intelligenceâ, and is supposed to calculate the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1.</p>
<p>If two critics rate items very similarly the function should return 1, or close to 1.</p>
<p>With real us... | 5 | 2009-11-22T10:52:58Z | 1,780,042 | <p>If you look up <a href="http://en.wikipedia.org/wiki/Pearson%5Fcorrelation" rel="nofollow">Pearson correlation on wikipedia</a>, you'll see that the formula uses the difference between each item in a series and the mean of the series. When all the items in the series are the same, you get division by zero, so your c... | 2 | 2009-11-22T21:20:04Z | [
"python",
"algorithm",
"pearson"
] |
Python subclass with C++ baseclass | 1,778,487 | <p>I have some C++ I have exposed to <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a> through <a href="http://en.wikipedia.org/wiki/SWIG" rel="nofollow">SWIG</a>. In there is a base class with a single pure virtual function.</p>
<p>In Python, I import my module and... | 2 | 2009-11-22T11:41:22Z | 1,778,742 | <p>Not really an answer, but you might have better luck with <a href="http://wiki.python.org/moin/boost.python/Inheritance" rel="nofollow">boost.python</a>. Don't know how much work it would be for you to switch, but you could probably get a simple inheritence test case for your problem going pretty quickly.</p>
| 0 | 2009-11-22T13:49:04Z | [
"c++",
"python",
"swig"
] |
Python subclass with C++ baseclass | 1,778,487 | <p>I have some C++ I have exposed to <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Python</a> through <a href="http://en.wikipedia.org/wiki/SWIG" rel="nofollow">SWIG</a>. In there is a base class with a single pure virtual function.</p>
<p>In Python, I import my module and... | 2 | 2009-11-22T11:41:22Z | 1,779,076 | <p>My guess is that <code>Foo</code> is not derived from <code>mybase</code> in the eyes of the C++ environment. I'm not sure if SWIG can pull this off since it requires a bidirectional understanding of inheritance - Python class uses C++ class as base and C++ code recognizes the inheritance relationship. I would tak... | 2 | 2009-11-22T15:42:31Z | [
"c++",
"python",
"swig"
] |
Why does Python allow comparison of a callable and a number? | 1,778,521 | <p>I used python to write an assignment last week, here is a code snippet</p>
<pre><code>def departTime():
'''
Calculate the time to depart a packet.
'''
if(random.random < 0.8):
t = random.expovariate(1.0 / 2.5)
else:
t = random.expovariate(1.0 / 10.5)
return t
</code></pre>... | 2 | 2009-11-22T12:07:06Z | 1,778,555 | <p>Because in Python that is a perfectly valid comparison. Python can't know if you really want to make that comparison or if you've just made a mistake. It's your job to supply Python with the right objects to compare.</p>
<p>Because of the dynamic nature of Python you can compare and sort almost everything with almo... | 3 | 2009-11-22T12:19:52Z | [
"python"
] |
Why does Python allow comparison of a callable and a number? | 1,778,521 | <p>I used python to write an assignment last week, here is a code snippet</p>
<pre><code>def departTime():
'''
Calculate the time to depart a packet.
'''
if(random.random < 0.8):
t = random.expovariate(1.0 / 2.5)
else:
t = random.expovariate(1.0 / 10.5)
return t
</code></pre>... | 2 | 2009-11-22T12:07:06Z | 1,778,563 | <p>I think python allows this because the random.random object could be overriding the <code>></code> operator by including a <code>__gt__</code> method in the object which might be accepting or even expecting a number. So, python thinks you know what you are doing... and does not report it.</p>
<p>If you try chec... | 1 | 2009-11-22T12:22:20Z | [
"python"
] |
Why does Python allow comparison of a callable and a number? | 1,778,521 | <p>I used python to write an assignment last week, here is a code snippet</p>
<pre><code>def departTime():
'''
Calculate the time to depart a packet.
'''
if(random.random < 0.8):
t = random.expovariate(1.0 / 2.5)
else:
t = random.expovariate(1.0 / 10.5)
return t
</code></pre>... | 2 | 2009-11-22T12:07:06Z | 1,778,611 | <p><strong>This isn't always a mistake</strong></p>
<p>Firstly, just to make things clear, this isn't <strong>always</strong> a mistake.</p>
<p>In this particular case, it's pretty clear the comparison is an error.</p>
<p>However, because of the dynamic nature of Python, consider the following (perfectly valid, if t... | 9 | 2009-11-22T12:46:18Z | [
"python"
] |
Why does Python allow comparison of a callable and a number? | 1,778,521 | <p>I used python to write an assignment last week, here is a code snippet</p>
<pre><code>def departTime():
'''
Calculate the time to depart a packet.
'''
if(random.random < 0.8):
t = random.expovariate(1.0 / 2.5)
else:
t = random.expovariate(1.0 / 10.5)
return t
</code></pre>... | 2 | 2009-11-22T12:07:06Z | 1,778,613 | <p>This is âfixedâ in Python 3 <a href="http://docs.python.org/3.1/whatsnew/3.0.html#ordering-comparisons" rel="nofollow">http://docs.python.org/3.1/whatsnew/3.0.html#ordering-comparisons</a>.</p>
| 7 | 2009-11-22T12:47:08Z | [
"python"
] |
How to get image details from firefox webdriver? | 1,778,528 | <p>I've got an image on a page rendered by Firefox via Webdriver, I can get its object (<code>wd.find_element_by_xpath("id('main')/form/p[5]/img")</code>), but how can I get its body either base64-encoded or just a location on my hard drive?</p>
<p>PS: please don't suggest getting the <code>src</code> and fetching it ... | 4 | 2009-11-22T12:09:31Z | 1,781,075 | <p>Cached images can be extracted from Firefox's cache by navigating to an URL like this one:</p>
<pre><code>about:cache-entry?client=HTTP&sb=1&key=http://your.server/image.png
</code></pre>
<p>The resulting page will contain a line with the "file on disk" label, like this one:</p>
<pre><code>file on disk: /... | 4 | 2009-11-23T04:22:41Z | [
"python",
"firefox",
"webdriver"
] |
How to get image details from firefox webdriver? | 1,778,528 | <p>I've got an image on a page rendered by Firefox via Webdriver, I can get its object (<code>wd.find_element_by_xpath("id('main')/form/p[5]/img")</code>), but how can I get its body either base64-encoded or just a location on my hard drive?</p>
<p>PS: please don't suggest getting the <code>src</code> and fetching it ... | 4 | 2009-11-22T12:09:31Z | 33,189,448 | <p>I've created a little script for extracting data from browser cache. You can extract cache entries using it. Check it out at this <a href="https://gist.github.com/zxcmehran/b4cab69b70166187f5cb" rel="nofollow">gist</a>. Check <a href="https://medium.com/@zxcmehran/recover-the-past-4b721d73badd" rel="nofollow">this p... | 0 | 2015-10-17T17:23:55Z | [
"python",
"firefox",
"webdriver"
] |
Data visualization in python - after connecting to a database | 1,778,663 | <p>Can you help me to connect to my postgresql database with python? I need to create graphic interface with python which will visualize shapefile data from my database (i have about 50 polygons in shapefile format in that database). Can you help me with creating such application? I am begginer in python.</p>
| 1 | 2009-11-22T13:14:32Z | 1,778,696 | <p>For communicating with the database, use <a href="http://www.devx.com/opensource/Article/29071" rel="nofollow">psycopg2</a>. It's quick, easy and efficient if you are familiar with basic DB concepts.</p>
<p>You have several options from here. You can use <a href="http://indiemaps.com/blog/2008/03/easy-shapefile-loa... | 6 | 2009-11-22T13:29:35Z | [
"python",
"database",
"postgresql",
"visualization"
] |
Data visualization in python - after connecting to a database | 1,778,663 | <p>Can you help me to connect to my postgresql database with python? I need to create graphic interface with python which will visualize shapefile data from my database (i have about 50 polygons in shapefile format in that database). Can you help me with creating such application? I am begginer in python.</p>
| 1 | 2009-11-22T13:14:32Z | 1,778,714 | <p>I would approach this by breaking it up into smaller problems and solving each of them</p>
<p>a) How do I connect to a postgresql database with python?</p>
<p><a href="http://stackoverflow.com/search?q=postgresql+database+python">http://stackoverflow.com/search?q=postgresql+database+python</a> - Looks like psycopg... | 0 | 2009-11-22T13:34:03Z | [
"python",
"database",
"postgresql",
"visualization"
] |
Data visualization in python - after connecting to a database | 1,778,663 | <p>Can you help me to connect to my postgresql database with python? I need to create graphic interface with python which will visualize shapefile data from my database (i have about 50 polygons in shapefile format in that database). Can you help me with creating such application? I am begginer in python.</p>
| 1 | 2009-11-22T13:14:32Z | 1,778,716 | <p><a href="http://mapnik.org/" rel="nofollow">Mapnik</a> is great for drawing maps. It can handle various formats and shapefiles, too. As far as I know it also supports PostgreSQL (at least PostGIS). </p>
<p>And least but not last: it comes with a Python interface (see <a href="http://trac.mapnik.org/wiki/GettingStar... | 0 | 2009-11-22T13:34:55Z | [
"python",
"database",
"postgresql",
"visualization"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,778,682 | <p>It's a matter of weighing pros and cons.</p>
<p>I suppose the real answer to this question is how many features there are in 2.5/2.6 that would really improve your library. It seems as though 2.4 becomes less and less worth supporting as time goes by. </p>
<p>On the other hand, there <em>are</em> still some peop... | 2 | 2009-11-22T13:23:32Z | [
"python"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,778,686 | <p>I'd say it depends on your target audience. For enterprise stuff I think RedHat (certainly CentOS 5) are still on 2.4 - so if you want typical RedHat/CentOS using people to able to install without resorting to third party python installations then I think you need to keep 2.4 for a while. If most of your users are m... | 7 | 2009-11-22T13:24:11Z | [
"python"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,778,723 | <p>You don't <em>have</em> to drop support for 2.4 in order to add support for 3.x, as I'm sure you know. I've made coverage.py run on 2.3 through 3.1 with the same code. It's not always pretty, but it's possible: <a href="http://nedbatchelder.com/blog/200910/running%5Fthe%5Fsame%5Fcode%5Fon%5Fpython%5F2x%5Fand%5F3x.... | 3 | 2009-11-22T13:38:34Z | [
"python"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,778,773 | <p>Any answer here is going to be subjective. I suggest you make a feature and user list. There are 2 things to consider here.</p>
<p>1: How will your program benefit - what features are better nicer/faster/less buggy in newer versions of Python ? What extra dependent libraries can your program utilize by sticking to ... | 0 | 2009-11-22T13:57:58Z | [
"python"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,778,792 | <p>You don't have to drop anything, what works on 2.4, works on 2.5 and 2.6. You can easily avoid incompatibilities skipping "with", the ternary operation, et "import <strong>future</strong>".</p>
<p>Now, once you have a very stable and full featured version of your code and need to make a big achitectural change, sta... | 1 | 2009-11-22T14:04:18Z | [
"python"
] |
When should I drop support for python2.4 on my public python library? | 1,778,670 | <p>I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support.</p>
<p>I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run t... | 8 | 2009-11-22T13:17:31Z | 1,779,015 | <p>Track downloads of each version of your project. Graph the daily traffic (or weekly if there is too much variation day to day) for each version separately. Keep an eye on the trends and at some point you will see a distinctly downward trend for 2.4 compared to the rest. When that downward trend is well established, ... | 1 | 2009-11-22T15:25:41Z | [
"python"
] |
SQL syntax error using Python and psycopg | 1,778,778 | <p><strong>How can you fix this SQL-code?</strong></p>
<p>My Python code:</p>
<pre><code>import os, pg, sys, re, psycopg2
conn = psycopg2.connect("dbname=tk user=masi password=123")
cur = conn.cursor()
cur.execute("""INSERT INTO courses ('course_nro')
VALUES ( `:1` ... | 0 | 2009-11-22T13:59:24Z | 1,778,786 | <p>Remove the quotes around the fieldname:</p>
<pre><code>INSERT INTO courses (course_nro)
</code></pre>
| 1 | 2009-11-22T14:02:08Z | [
"python",
"sql",
"sql-injection"
] |
SQL syntax error using Python and psycopg | 1,778,778 | <p><strong>How can you fix this SQL-code?</strong></p>
<p>My Python code:</p>
<pre><code>import os, pg, sys, re, psycopg2
conn = psycopg2.connect("dbname=tk user=masi password=123")
cur = conn.cursor()
cur.execute("""INSERT INTO courses ('course_nro')
VALUES ( `:1` ... | 0 | 2009-11-22T13:59:24Z | 1,817,264 | <p>You made 3 different errors in the same query:</p>
<ol>
<li>Field names should not be quoted.</li>
<li>psycopg2 uses tuples, not lists for arguments.</li>
<li>Positional arguments like ":1" are not supported.</li>
</ol>
<p>Change your query into:</p>
<p><code>cur.execute("""INSERT INTO courses (course_nro)
... | 2 | 2009-11-30T00:17:33Z | [
"python",
"sql",
"sql-injection"
] |
How to access Firefox cache from webdriver? | 1,778,816 | <p>I'm able to access pages like <code>about:cache-entry?client=HTTP&sb=1&key=(some URL)</code> directly in Firefox, but when it renders the page, it certainly gets the data from some storage. How can I access the latter from Python Firefox Webdriver?</p>
| 4 | 2009-11-22T14:17:11Z | 1,781,082 | <p>The page returned by such an <code>about:cache-entry?...</code> URL contains a line like this one:</p>
<pre><code>file on disk: /home/fviktor/.mozilla/firefox/7jx6k3hx.default/Cache/CF7379D8d01
</code></pre>
<p>It is the full pathname of the cache file if any. I think you'll be able to read that file from Python a... | 2 | 2009-11-23T04:26:24Z | [
"python",
"firefox",
"caching",
"webdriver"
] |
Populating values in module namespace | 1,778,846 | <p>I have a python module.</p>
<p>I want to populate some values to it at runtime, how do I do it.</p>
<p>Eg. I have a list,</p>
<p>['A', 'B', 'C']</p>
<p>I am creating there classes with these names, and want them to available as if I created them normally</p>
<pre><code>for el in ['A', 'B', 'C']:
type(el, (o... | 1 | 2009-11-22T14:30:08Z | 1,778,861 | <p>To create your classes dynamically and make them available in the module <code>mymodule</code>:</p>
<pre><code>import mymodule
for el in ['A', 'B', 'C']:
setattr(mymodule, el, type(el, (object,), {}))
</code></pre>
<p>To create the classes in the current module, use the approach in <a href="http://stackoverflo... | 2 | 2009-11-22T14:35:38Z | [
"python",
"metaprogramming",
"module"
] |
Populating values in module namespace | 1,778,846 | <p>I have a python module.</p>
<p>I want to populate some values to it at runtime, how do I do it.</p>
<p>Eg. I have a list,</p>
<p>['A', 'B', 'C']</p>
<p>I am creating there classes with these names, and want them to available as if I created them normally</p>
<pre><code>for el in ['A', 'B', 'C']:
type(el, (o... | 1 | 2009-11-22T14:30:08Z | 1,778,875 | <p>I can think of a couple ways to do this... In order of (what I believe to be) best to worst we have:</p>
<p>First, setting attributes on the current module</p>
<pre><code># The first way I had of grabbing the module:
mod = __import__(__name__, fromlist=['nonempty'])
# From Roger's suggestion:
import sys
mod = sys... | 5 | 2009-11-22T14:40:37Z | [
"python",
"metaprogramming",
"module"
] |
Boolean fields in MySQL Django Models? | 1,778,948 | <p>At Django, <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" rel="nofollow">a boolean field in MySQL is stored as a TINYINT</a>. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour?</p>
| 3 | 2009-11-22T15:02:36Z | 1,778,966 | <pre><code>>>> u=User.objects.get(pk=1)
>>> u.is_active
1
>>> u.is_active==1
True
>>>
</code></pre>
<p>The reasons why boolean columns return 1 or 0 are on the link in your question.</p>
| 0 | 2009-11-22T15:07:54Z | [
"python",
"mysql",
"django",
"boolean"
] |
Boolean fields in MySQL Django Models? | 1,778,948 | <p>At Django, <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" rel="nofollow">a boolean field in MySQL is stored as a TINYINT</a>. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour?</p>
| 3 | 2009-11-22T15:02:36Z | 1,798,951 | <p>You could create your own method for your model that evaluates this for you:</p>
<pre class="lang-python prettyprint-override"><code>class User(models.Model):
active_status = models.BooleanField(default=1)
def is_active(self):
return bool(self.active_status)
</code></pre>
<p>Then any tests you per... | 5 | 2009-11-25T18:19:43Z | [
"python",
"mysql",
"django",
"boolean"
] |
Boolean fields in MySQL Django Models? | 1,778,948 | <p>At Django, <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" rel="nofollow">a boolean field in MySQL is stored as a TINYINT</a>. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour?</p>
| 3 | 2009-11-22T15:02:36Z | 5,899,741 | <p>Here is the above method adapted for <code>NullBooleanField</code>:</p>
<pre><code>result = models.NullBooleanField()
def get_result(self):
if self.result is None:
return None
return bool(self.result)
</code></pre>
| 1 | 2011-05-05T14:49:25Z | [
"python",
"mysql",
"django",
"boolean"
] |
Boolean fields in MySQL Django Models? | 1,778,948 | <p>At Django, <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/" rel="nofollow">a boolean field in MySQL is stored as a TINYINT</a>. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour?</p>
| 3 | 2009-11-22T15:02:36Z | 6,922,097 | <p>Is there a situation you anticipate that this will cause different behaviour just based on the types?</p>
<pre><code>>>> 1 == True
True
>>> 0 == False
True
>>> int(True)
1
>>> int(False)
0
</code></pre>
| 1 | 2011-08-03T05:35:52Z | [
"python",
"mysql",
"django",
"boolean"
] |
To insert to Pg by Psycopg | 1,779,009 | <p><strong>How can you fix the SQL-statement in Python?</strong></p>
<p>The db connection works. However, <code>cur.execute</code> returns <em>none</em> which is false.</p>
<p><strong>My code</strong></p>
<pre><code>import os, pg, sys, re, psycopg2
try:
conn = psycopg2.connect("dbname='tk' host='localhost' por... | 1 | 2009-11-22T15:23:04Z | 1,779,039 | <p>You have to call one of the <code>fetch</code> methods on <code>cur</code> (fetchone, fetchmany, fetchall) to actually get the results of the query.</p>
<p>You should probably have a read through the a <a href="http://www.amk.ca/python/writing/DB-API.html" rel="nofollow">tutorial</a> for <a href="http://www.python.... | 2 | 2009-11-22T15:30:43Z | [
"python",
"postgresql",
"psycopg"
] |
To insert to Pg by Psycopg | 1,779,009 | <p><strong>How can you fix the SQL-statement in Python?</strong></p>
<p>The db connection works. However, <code>cur.execute</code> returns <em>none</em> which is false.</p>
<p><strong>My code</strong></p>
<pre><code>import os, pg, sys, re, psycopg2
try:
conn = psycopg2.connect("dbname='tk' host='localhost' por... | 1 | 2009-11-22T15:23:04Z | 1,779,042 | <p>You have to call <code>cur.fetchall()</code> method (or one of other <code>fetch*()</code> methods) to get results from query.</p>
| 2 | 2009-11-22T15:31:04Z | [
"python",
"postgresql",
"psycopg"
] |
Problem with logic in Django template | 1,779,055 | <p>Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:</p>
<pre><code>{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if f... | 0 | 2009-11-22T15:34:45Z | 1,779,170 | <p>Just <em>adjust</em> your <strong><code>if</code>s</strong></p>
<pre><code>{% for r in regs %}
{% if forloop.first %}
<input type="radio" value="{{ forloop.counter }}" checked="checked"/>
{% else %}
{% if not r.editable %}
<input type="radio" value="{{ forloop.counter }}... | 1 | 2009-11-22T16:21:54Z | [
"python",
"django",
"templates",
"django-templates"
] |
Problem with logic in Django template | 1,779,055 | <p>Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:</p>
<pre><code>{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if f... | 0 | 2009-11-22T15:34:45Z | 1,779,190 | <p>Djangos templating language doesn't give you a lot in the way of logic in the template (I've heard positive things about <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a> if you want to change that). There's also the <a href="http://www.djangosnippets.org/snippets/1350/" rel="nofollow">"Smart" {% if %}<... | 2 | 2009-11-22T16:32:05Z | [
"python",
"django",
"templates",
"django-templates"
] |
Problem with logic in Django template | 1,779,055 | <p>Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:</p>
<pre><code>{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if f... | 0 | 2009-11-22T15:34:45Z | 1,779,230 | <p>The <em>real</em> answer to this question is as follows:</p>
<p>Such logic has no place in the template. You can preprocess the context before passing it to the template, thus eleminating the need to do this using the (intentionally) crippled template engine logic.</p>
<p>In my opinion, what you are doing is wrong... | 1 | 2009-11-22T16:49:31Z | [
"python",
"django",
"templates",
"django-templates"
] |
Problem with logic in Django template | 1,779,055 | <p>Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:</p>
<pre><code>{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if f... | 0 | 2009-11-22T15:34:45Z | 1,779,231 | <p>Similar to T. Stone's answer of doing this logic in the view, you could just add a new template variable that indicated the first checked radio:</p>
<pre><code>def my_view(request, *args, **kwargs):
regs = Regs.objects.all()
checked_index = None
for i, reg in enumerate(regs):
if reg.enabled:
checked... | 0 | 2009-11-22T16:49:59Z | [
"python",
"django",
"templates",
"django-templates"
] |
Problem with logic in Django template | 1,779,055 | <p>Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField.
I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled:</p>
<pre><code>{% for r in regs %}
<input type="radio" value="{{ forloop.counter }}"
{% if f... | 0 | 2009-11-22T15:34:45Z | 1,779,768 | <p>Similar to becomingGuru but solving your problems:</p>
<pre><code>{% for r in regs %}
{% if not r.editable %}
<input type="radio" value="{{ forloop.counter }}" disabled="disabled"/>
{% else %}
{% if forloop.first %}
<input type="radio" value="{{ forloop.counter }}" ... | 0 | 2009-11-22T19:52:34Z | [
"python",
"django",
"templates",
"django-templates"
] |
There is no spawnl function in python 2.6? | 1,779,081 | <p>I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.</p>
<pre><code>>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv... | 3 | 2009-11-22T15:44:17Z | 1,779,088 | <p>I think its recommended to use the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module these days rather than the <code>os.spawn*</code> functions. (I can't reproduce your problem, but I'm not on windows). </p>
| 3 | 2009-11-22T15:47:18Z | [
"python",
"python-2.6"
] |
There is no spawnl function in python 2.6? | 1,779,081 | <p>I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.</p>
<pre><code>>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv... | 3 | 2009-11-22T15:44:17Z | 1,779,103 | <p>thrope is right about <code>subprocess</code> being preferred. But the spawn* stuff is still there in <a href="http://docs.python.org/library/os.html?highlight=spawn#os.spawnl" rel="nofollow">2.6</a>. In fact, you can see that in your error message. Your first arg seems to be valid. I'd check the second arg, whi... | 5 | 2009-11-22T15:54:29Z | [
"python",
"python-2.6"
] |
There is no spawnl function in python 2.6? | 1,779,081 | <p>I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.</p>
<pre><code>>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv... | 3 | 2009-11-22T15:44:17Z | 1,779,182 | <p><code>os.spawnl()</code> requires full path to executable, while <code>os.spawnlp()</code> uses PATH environment variable to find it. </p>
<p><strong>Update</strong>: Also it's common error to use unescaped backslashes in the path literal (try printing it to see whether it's interpreted right). </p>
| 2 | 2009-11-22T16:27:38Z | [
"python",
"python-2.6"
] |
There is no spawnl function in python 2.6? | 1,779,081 | <p>I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.</p>
<pre><code>>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv... | 3 | 2009-11-22T15:44:17Z | 1,779,227 | <p>A Google search brings up <a href="http://www.openemm.org/forums/ftopic1012.html" rel="nofollow">this page</a> about the same problem happening when there is a space in the Python installation path. I couldn't reproduce it here, but maybe it's the problem?</p>
<p>In any case, <a href="http://msdn.microsoft.com/en-u... | 2 | 2009-11-22T16:48:59Z | [
"python",
"python-2.6"
] |
There is no spawnl function in python 2.6? | 1,779,081 | <p>I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw.</p>
<pre><code>>>> os.spawnl(os.P_NOWAIT,"setup.exe")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python26\lib\os.py", line 612, in spawnl
return spawnv... | 3 | 2009-11-22T15:44:17Z | 1,779,277 | <p>I got it work by adding DUMMY parameter finally, a bit funky though</p>
<p>This is not working</p>
<pre><code>os.spawnl(os.P_NOWAIT,"Setup.exe")
</code></pre>
<p>This is also not working</p>
<pre><code>os.spawnl(os.P_NOWAIT,"Setup.exe","")
</code></pre>
<p>But this is working</p>
<pre><code>os.spawnl(os.P_NOWA... | 5 | 2009-11-22T17:08:36Z | [
"python",
"python-2.6"
] |
Converting SQL commands to Python's ORM | 1,779,239 | <p><strong>How would you convert the following codes to Python's ORM such as by SQLalchemy?</strong></p>
<h1>#1 Putting data to Pg</h1>
<pre><code>import os, pg, sys, re, psycopg2
#conn = psycopg2.connect("dbname='tkk' host='localhost' port='5432' user='noa' pas... | 1 | 2009-11-22T16:52:19Z | 1,779,309 | <p>After the initial declarations, you can do something like this:</p>
<pre><code>o = Course(course_nro='abcd')
session.add(o)
session.commit()
</code></pre>
<p>and</p>
<pre><code>print session.query(Course).all()
</code></pre>
<p>The declarations could look something like this:</p>
<pre><code>from sqlalchemy impo... | 1 | 2009-11-22T17:13:55Z | [
"python",
"postgresql",
"orm",
"sqlalchemy"
] |
Converting SQL commands to Python's ORM | 1,779,239 | <p><strong>How would you convert the following codes to Python's ORM such as by SQLalchemy?</strong></p>
<h1>#1 Putting data to Pg</h1>
<pre><code>import os, pg, sys, re, psycopg2
#conn = psycopg2.connect("dbname='tkk' host='localhost' port='5432' user='noa' pas... | 1 | 2009-11-22T16:52:19Z | 16,301,607 | <p>Even though this is old, more examples can't hurt, right? I thought I'd demonstrate how to do this with <a href="http://pyormish.nullism.com" rel="nofollow">PyORMish</a>. </p>
<pre><code>from pyormish import Model
class Course(Model):
_TABLE_NAME = 'courses'
_PRIMARY_FIELD = 'id' # or whatever your primary... | 0 | 2013-04-30T13:51:03Z | [
"python",
"postgresql",
"orm",
"sqlalchemy"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,779,303 | <p>This isn't pythonic, but it is language neutral. Often <code>val = 1 - val</code> is simplest.</p>
| 59 | 2009-11-22T17:13:07Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,779,305 | <p>Since <code>True == 1</code> and <code>False == 0</code> in python,</p>
<p>you could just use <code>var = not var</code></p>
<p>It will just swap it.</p>
| 16 | 2009-11-22T17:13:20Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,779,322 | <p>If you want to be short: </p>
<pre><code>f = lambda val: 0 if val else 1
</code></pre>
<p>Then:</p>
<pre><code>>>> f(0)
1
>>> f(1)
0
</code></pre>
| 1 | 2009-11-22T17:19:51Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,779,448 | <p>In your case I recommend the ternary:</p>
<pre><code>val = 0 if val else 1
</code></pre>
<p>If you had 2 variables to swap you could say:</p>
<pre><code>(a, b) = (b, a)
</code></pre>
| 5 | 2009-11-22T18:05:37Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,779,501 | <p>Just another possibility:</p>
<pre><code>i = (1,0)[i]
</code></pre>
<p>This works well as long as i is positive, as <a href="http://stackoverflow.com/users/745/">dbr</a> pointed out in the comments it doesn't work fail for i < 0.</p>
<p>Are you sure you don't want to use <code>False</code> and <code>True</code... | 9 | 2009-11-22T18:23:02Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 1,871,430 | <p>The shortest approach is using the bitwise operator XOR.</p>
<p>If you want <code>val</code> to be reassigned:</p>
<pre><code>val ^= 1
</code></pre>
<p>If you do not want <code>val</code> to be reassigned:</p>
<pre><code>val ^ 1
</code></pre>
| 10 | 2009-12-09T03:35:04Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 22,618,329 | <p>Function with mutable argument. Calling the <code>swaper()</code> will return different value every time.</p>
<pre class="lang-py prettyprint-override"><code>def swaper(x=[1]):
x[0] ^= 1
return x[0]
</code></pre>
| 0 | 2014-03-24T19:03:09Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 30,074,682 | <p>Just another way:</p>
<pre><code>val = ~val + 2
</code></pre>
| 0 | 2015-05-06T10:53:57Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 31,843,997 | <p>(0,1)[not val]
flips the val from 0 to 1 and vice versa.</p>
| 1 | 2015-08-05T22:39:26Z | [
"python"
] |
Swapping 1 with 0 and 0 with 1 in a pythonic way? | 1,779,286 | <p>In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.</p>
<p>How do you do it in a Pythonic way?</p>
<pre><code>if val == 1:
val = 0
elif val == 0:
val = 1
</code></pre>
<p>it's too long!</p>
<p>I did:</p>
<pre><code>swap ... | 12 | 2009-11-22T17:10:35Z | 38,361,873 | <p>To expand upon the <a href="http://stackoverflow.com/a/1779305/832230">answer by "YOU"</a>,</p>
<pre><code>int(not(val))
>>> val = 0
>>> int(not(val))
1
>>> val = 1
>>> int(not(val))
0
</code></pre>
<p>And if you wanted to strictly maintain the <code>type</code> of <code>val</c... | 0 | 2016-07-13T21:19:08Z | [
"python"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,779,350 | <p><a href="http://hsqldb.org" rel="nofollow">HSQLDB</a> does this, but unfortunately it's Java rather than Python.</p>
<p><a href="http://www.firebirdsql.org/" rel="nofollow">Firebird SQL</a> might be closer to what you want, since it does seem to have a Python interface.</p>
| 0 | 2009-11-22T17:30:25Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,779,502 | <p>If you don't need an SQL database, what's wrong with CouchDB? You can spawn a local process to serve the DB, and you could easily write a server wrapper to allow only access from your app. I'm not sure about the access story, but I believe the latest Ubuntu uses CouchDB for synchronizeable user-level data.</p>
| 1 | 2009-11-22T18:23:11Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,779,554 | <p>Seems like the perfect job for CouchDB: 2 way sync is incredibly easy, schema-less JSON documents are the native format. If you're using python, <a href="http://code.google.com/p/couchdb-python/" rel="nofollow">couchdb-python</a> is a great way to work with CouchDB.</p>
| 1 | 2009-11-22T18:41:48Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,779,601 | <p>Do you need clients to work offline and then resync when they reconnect to the network? I don't know if <a href="http://mongodb.org" rel="nofollow">MongoDB</a> can handle the offline client scenario, but if the client is online all the time, MongoDB might be a good solution too. It has pretty goode python <a href="h... | 1 | 2009-11-22T18:54:07Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,779,673 | <p>From what you describe, it sounds like you could get by using <code>pickle</code> and FTP.</p>
| 2 | 2009-11-22T19:18:56Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
Small "embeddable" database that can also be synced over the network? | 1,779,287 | <p>I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> or <a href="http://www.equi4.com/metakit/" rel="nofollow">Metakit</a>. I don't need an SQL database, in fact storing fre... | 4 | 2009-11-22T17:10:42Z | 1,790,790 | <p>BerkeleyDB might be another option to check out, and it's lightweight enough. easy_install bsddb3 if you need a Python interface.</p>
| 1 | 2009-11-24T15:26:55Z | [
"python",
"database",
"sqlite",
"couchdb"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 1,779,324 | <pre><code>import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
locale.atoi('1,000,000')
# 1000000
locale.atof('1,000,000.53')
# 1000000.53
</code></pre>
| 58 | 2009-11-22T17:20:11Z | [
"python"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 1,781,958 | <p>There are several ways to parse numbers with thousands separators. And I doubt that the way described by ~ubuntu is the best in all cases. That's why I list other ways too.</p>
<ol>
<li><p>The proper place to call <code>setlocale()</code> is in <code>__main__</code> module. It's global setting and will affect the w... | 23 | 2009-11-23T09:18:32Z | [
"python"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 12,746,073 | <p>I got locale error from accepted error, so here is what works here in Finland in Windows XP:</p>
<pre><code> import locale
locale.setlocale( locale.LC_ALL, 'english_USA' )
print locale.atoi('1,000,000')
# 1000000
print locale.atof('1,000,000.53')
# 1000000.53
</code></pre>
| 3 | 2012-10-05T12:15:19Z | [
"python"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 15,431,682 | <p>This works:</p>
<p>(A dirty but fast way)</p>
<pre><code>>>> a='-1,234,567,89.0123'
>>> "".join(a.split(","))
'-123456789.0123'
</code></pre>
| 2 | 2013-03-15T11:49:38Z | [
"python"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 16,091,812 | <p>Replace the commas with empty strings, and turn the resulting string into an <code>int</code> or a <code>float</code>.</p>
<pre><code>>>> a = '1,000,000'
>>> int(a.replace(',' , ''))
1000000
>>> float(a.replace(',' , ''))
1000000.0
</code></pre>
| 6 | 2013-04-18T19:48:43Z | [
"python"
] |
How do I use Python to convert a string to a number if it has commas in it as thousands separators? | 1,779,288 | <p>I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python?</p>
<pre><code>>>> int("1,000,000")
</code></pre>
<p>Generates a <code>ValueError</code>.</p>
<p>I could replace the commas with empty strings before I try to convert it, but t... | 43 | 2009-11-22T17:10:48Z | 31,074,271 | <pre><code>>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.UTF-8'
>>> print locale.atoi('1,000,000')
1000000
>>> print locale.atof('1,000,000.53')
1000000.53
</code></pre>
<p>this is done on Linux in US.
-Suresh</p>
| 0 | 2015-06-26T13:20:15Z | [
"python"
] |
What does abstract mean in this context? | 1,779,365 | <p>I need some help in understanding a python concept.</p>
<pre><code>class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """
def __init__(self, N, blanks, initial, goal):
""" Initialize """
search.Problem.__init__(self, initial, goal)
... | 2 | 2009-11-22T17:36:53Z | 1,779,369 | <p>Abstract means the class must be inherited. Abstract or "base" classes are there to provide a base type which you can extend through inheritance. You cannot instantiate an abstract class, only the classes that inherit it.</p>
<p>See <a href="http://en.wikipedia.org/wiki/Class%5F%28computer%5Fscience%29#Abstract%5... | 0 | 2009-11-22T17:39:18Z | [
"python",
"abstract"
] |
What does abstract mean in this context? | 1,779,365 | <p>I need some help in understanding a python concept.</p>
<pre><code>class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """
def __init__(self, N, blanks, initial, goal):
""" Initialize """
search.Problem.__init__(self, initial, goal)
... | 2 | 2009-11-22T17:36:53Z | 1,779,375 | <p>An abstract function is a function with no implementation. It's a placeholder, just there to fill out the class contract so that you know what methods subclass should provide. What you need to do here is create a descendant of <code>TilePuzzleProblem</code> and fill in your own implementation of <code>h</code>. I... | 1 | 2009-11-22T17:41:00Z | [
"python",
"abstract"
] |
What does abstract mean in this context? | 1,779,365 | <p>I need some help in understanding a python concept.</p>
<pre><code>class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """
def __init__(self, N, blanks, initial, goal):
""" Initialize """
search.Problem.__init__(self, initial, goal)
... | 2 | 2009-11-22T17:36:53Z | 1,779,387 | <p>An abstract method is one which a class doesn't implement, making it an abstract class; subclasses must override all abstract methods (i.e., provide concrete implementations) to be concrete classes, i.e., ones for which you can make instances. The normal way in Python to express "this method is abstract" is to have... | 5 | 2009-11-22T17:44:03Z | [
"python",
"abstract"
] |
What does abstract mean in this context? | 1,779,365 | <p>I need some help in understanding a python concept.</p>
<pre><code>class TilePuzzleProblem(search.Problem):
""" This class is the class for the NxN - blanks tile puzzle problem """
def __init__(self, N, blanks, initial, goal):
""" Initialize """
search.Problem.__init__(self, initial, goal)
... | 2 | 2009-11-22T17:36:53Z | 1,779,393 | <p>This is a trick described <a href="http://norvig.com/python-iaq.html">here</a>. There's not keyword <code>abstract</code> in Python, so, if you won't override this method in some subclass, it'll cause <code>NotImplementedError</code>. </p>
| 8 | 2009-11-22T17:46:03Z | [
"python",
"abstract"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.