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 |
|---|---|---|---|---|---|---|---|---|---|
No module named MySQLdb | 454,854 | <p>I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. Can anyone help me to solve this issue? I am using it in Vista.</p>
| 211 | 2009-01-18T09:13:38Z | 36,183,193 | <p>In CMD </p>
<pre><code>pip install wheel
pip install pymysql
</code></pre>
<p>in settings.py</p>
<pre><code>import pymysql
pymysql.install_as_MySQLdb()
</code></pre>
<p>It worked with me </p>
| 6 | 2016-03-23T16:08:57Z | [
"python",
"django"
] |
No module named MySQLdb | 454,854 | <p>I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. Can anyone help me to solve this issue? I am using it in Vista.</p>
| 211 | 2009-01-18T09:13:38Z | 36,697,579 | <p>I have tried methods above, but still no module named 'MySQLdb', finally, I succeed with </p>
<pre><code>easy_install mysql-python
</code></pre>
<p>my env is unbuntu 14.04</p>
| 1 | 2016-04-18T14:57:53Z | [
"python",
"django"
] |
No module named MySQLdb | 454,854 | <p>I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. Can anyone help me to solve this issue? I am using it in Vista.</p>
| 211 | 2009-01-18T09:13:38Z | 38,310,817 | <p>if your python version is <code>3.5</code>, do a <code>pip install mysqlclient</code>, other things didn't work for me</p>
| 2 | 2016-07-11T15:31:02Z | [
"python",
"django"
] |
What's the most efficient way to share large amounts of data between Python and C++ | 454,931 | <p>I'm writing a system that allows python scripts to be executed within a C++ application. </p>
<p>The python scripts are used to modify values within arrays of data (typically 2048x2048x4 arrays of floats)</p>
<p>I'm currently using numpy arrays created using the array API and registered with Python. </p>
<p>In th... | 2 | 2009-01-18T10:30:46Z | 454,983 | <p>You should probably see if you can set several values in the array in one step rather than in four (or more) steps.</p>
<p>I believe the ellipsis syntax may be useful here:</p>
<p><a href="http://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python">http://stackoverflow.com/quest... | 0 | 2009-01-18T11:29:14Z | [
"c++",
"python",
"optimization"
] |
What's the most efficient way to share large amounts of data between Python and C++ | 454,931 | <p>I'm writing a system that allows python scripts to be executed within a C++ application. </p>
<p>The python scripts are used to modify values within arrays of data (typically 2048x2048x4 arrays of floats)</p>
<p>I'm currently using numpy arrays created using the array API and registered with Python. </p>
<p>In th... | 2 | 2009-01-18T10:30:46Z | 455,006 | <p>You might want to have a look at <a href="http://www.boost.org/doc/libs/1_37_0/libs/python/doc/index.html" rel="nofollow">Boost.Python</a>. It focuses on making C++ code available in Python, but it also provides <a href="http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/exec.html" rel="nofollow">exec and eval ... | 1 | 2009-01-18T11:43:28Z | [
"c++",
"python",
"optimization"
] |
What's the most efficient way to share large amounts of data between Python and C++ | 454,931 | <p>I'm writing a system that allows python scripts to be executed within a C++ application. </p>
<p>The python scripts are used to modify values within arrays of data (typically 2048x2048x4 arrays of floats)</p>
<p>I'm currently using numpy arrays created using the array API and registered with Python. </p>
<p>In th... | 2 | 2009-01-18T10:30:46Z | 455,145 | <p>I thought I would suggest numpy, but you're already using it. I'm afraid that leaves domain-specific changes to do less work. As John Montgomery mentions, you'll need to figure out what is taking the time in the Python code, and determine if you can avoid some of it.</p>
<p>Are there patterns to the work being don... | 1 | 2009-01-18T13:42:24Z | [
"c++",
"python",
"optimization"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 454,996 | <p>If you already have MySQL installed, you could create a table to use as a "todo list" of sorts.</p>
<p>Threads synchronously add jobs to the table, and a batched task removes jobs as they're completed.</p>
<p>That way, there's no need to install and learn more software, and it should work fine as a persistent job ... | 0 | 2009-01-18T11:34:32Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 455,024 | <p>So far I have found no "nice" solution for this. I have some more strict soft realtime requirements (taking a picture from a cardboard box being labeled) so probably one of the approaches is fast enough for you. I assume emails can wait for a few minutes.</p>
<ul>
<li>A "todo list" in the database processed by a cr... | 13 | 2009-01-18T11:54:03Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 456,024 | <p>You can also use Twisted for this. But it won't play with django in all cases, it's very dependant on deployment scenarios. The most important thing is every request has to be served by one python process, so you need apache compiled in threaded mode.</p>
| -2 | 2009-01-18T22:21:53Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 456,389 | <p>Just add the emails to a database, and then write another script ran by some task scheduler utility (cron comes to mind) to send the emails.</p>
| 1 | 2009-01-19T02:45:50Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 456,593 | <p>In your specific case, where it's just an email queue, I wold take the easy way out and use <a href="http://code.google.com/p/django-mailer/" rel="nofollow">django-mailer</a>. As a nice side bonues there are other pluggable projects that are smart enough to take advantage of django-mailer when they see it in the st... | 23 | 2009-01-19T05:16:07Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 714,637 | <p>Stompserver is a good option. It's lightweight, easy to install and easy to use from Django/python.</p>
<p>We have a system using stompserver in production for sending out emails and processing other jobs asynchronously.</p>
<p>Django saves the emails to the database, a model.post_save handler in Django sends an e... | 6 | 2009-04-03T16:15:16Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 2,661,381 | <p>You might want to have a look at <a href="http://code.google.com/p/pymq/" rel="nofollow">pymq</a>. It's written in python, talks HTTP with it's clients and allows a host of monitoring and management options for queues.</p>
| 1 | 2010-04-18T06:48:39Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 3,696,792 | <p>Is there anything wrong is solving this using the mail infrastructure? Like, every app server running their own mail daemons which will queue any locally submitted mail, which forward to a centralized mail server which can do the mail heavy lifting?</p>
| 1 | 2010-09-12T21:46:40Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 6,068,702 | <p><a href="http://www.snakemq.net/" rel="nofollow">http://www.snakemq.net/</a> might work as well</p>
| 1 | 2011-05-20T07:21:04Z | [
"python",
"django",
"message-queue"
] |
Advice on Python/Django and message queues | 454,944 | <p>I have an application in Django, that needs to send a large number of emails to users in various use cases. I don't want to handle this synchronously within the application for obvious reasons.</p>
<p>Has anyone any recommendations for a message queuing server which integrates well with Python, or they have used on... | 40 | 2009-01-18T10:42:53Z | 9,856,825 | <p>Here's a lazy but correct and adequate solution. Use the following database table as a queue. </p>
<pre><code>drop table if exists mailqueue;
create table mailqueue (
id bigint primary key,
subject text not null,
body mediumtext not null,
from varchar(255) not null,
to varchar(255) not null
);
<... | 0 | 2012-03-25T00:08:09Z | [
"python",
"django",
"message-queue"
] |
Using an ordered dict as object dictionary in python | 455,059 | <p>I don't know why this doesn't work:</p>
<p>I'm using the <a href="http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py">odict</a> class from <a href="http://www.python.org/dev/peps/pep-0372/">PEP 372</a>, but I want to use it as a <code>__dict__</code> member, i.e.:</p>
<pre><code>class Bag(object):
def __ini... | 8 | 2009-01-18T12:33:03Z | 455,087 | <p>The closest answer to your question that I can find is at <a href="http://mail.python.org/pipermail/python-bugs-list/2006-April/033155.html">http://mail.python.org/pipermail/python-bugs-list/2006-April/033155.html</a>.</p>
<p>Basically, if <code>__dict__</code> is not an actual <code>dict()</code>, then it is ignor... | 8 | 2009-01-18T12:59:34Z | [
"python",
"ordereddictionary"
] |
Using an ordered dict as object dictionary in python | 455,059 | <p>I don't know why this doesn't work:</p>
<p>I'm using the <a href="http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py">odict</a> class from <a href="http://www.python.org/dev/peps/pep-0372/">PEP 372</a>, but I want to use it as a <code>__dict__</code> member, i.e.:</p>
<pre><code>class Bag(object):
def __ini... | 8 | 2009-01-18T12:33:03Z | 14,048,352 | <p>Everything in sykora's answer is correct. Here's an updated solution with the following improvements:</p>
<ol>
<li>works even in the special case of accessing <code>a.__dict__</code> directly</li>
<li>supports copy.copy()</li>
<li>supports the <code>==</code> and <code>!=</code> operators</li>
<li>uses <a href="ht... | 3 | 2012-12-27T01:30:16Z | [
"python",
"ordereddictionary"
] |
Using an ordered dict as object dictionary in python | 455,059 | <p>I don't know why this doesn't work:</p>
<p>I'm using the <a href="http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py">odict</a> class from <a href="http://www.python.org/dev/peps/pep-0372/">PEP 372</a>, but I want to use it as a <code>__dict__</code> member, i.e.:</p>
<pre><code>class Bag(object):
def __ini... | 8 | 2009-01-18T12:33:03Z | 32,377,941 | <p>If you're looking for a library with attribute access to OrderedDict, the <a href="https://pypi.python.org/pypi/orderedattrdict" rel="nofollow">orderedattrdict</a> package provides this.</p>
<pre><code>>>> from orderedattrdict import AttrDict
>>> conf = AttrDict()
>>> conf['z'] = 1
>&g... | 1 | 2015-09-03T14:07:06Z | [
"python",
"ordereddictionary"
] |
Break on exception in pydev | 455,552 | <p>Is it possible to get the pydev debugger to break on exception?</p>
| 40 | 2009-01-18T17:39:09Z | 455,556 | <p><del>On <strong>any</strong> exception?</p>
<p>If my memory serves me right, in PyDev (in Eclipse) this is possible.</del></p>
<p><hr>
<strong>EDIT:</strong> went through it again, checked <a href="http://docs.python.org/library/pdb.html">pdb documentation</a>, can't find a way to set an exception breakpoint.</p>
... | 16 | 2009-01-18T17:43:29Z | [
"python",
"eclipse",
"debugging",
"exception",
"pydev"
] |
Break on exception in pydev | 455,552 | <p>Is it possible to get the pydev debugger to break on exception?</p>
| 40 | 2009-01-18T17:39:09Z | 3,089,496 | <p>Long since over, but feature requests go <a href="http://sourceforge.net/tracker/?group_id=85796&atid=577332" rel="nofollow">on sourceforge</a>.</p>
| 2 | 2010-06-22T00:52:41Z | [
"python",
"eclipse",
"debugging",
"exception",
"pydev"
] |
Break on exception in pydev | 455,552 | <p>Is it possible to get the pydev debugger to break on exception?</p>
| 40 | 2009-01-18T17:39:09Z | 4,545,851 | <p>I've tried the big try-except trick but it didn't work as expected, you got the stack where it breaks, that is in the except: block, you can't get to the stack where the exception was raised from there, that's pointless.</p>
<p>update: pydev does have break-on-exception facility since 1.6.0, but no UI yet, need som... | 4 | 2010-12-28T11:57:27Z | [
"python",
"eclipse",
"debugging",
"exception",
"pydev"
] |
Break on exception in pydev | 455,552 | <p>Is it possible to get the pydev debugger to break on exception?</p>
| 40 | 2009-01-18T17:39:09Z | 6,655,894 | <p>This was added by the PyDev author, under Run > Manage Python Exception Breakpoints</p>
| 35 | 2011-07-11T20:09:05Z | [
"python",
"eclipse",
"debugging",
"exception",
"pydev"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 455,863 | <p>If you're certain that only Javascript will be consuming the JSON, I prefer to pass Javascript <code>Date</code> objects directly.</p>
<p>The <code>ctime()</code> method on <code>datetime</code> objects will return a string that the Javascript Date object can understand.</p>
<pre><code>import datetime
date = datet... | 21 | 2009-01-18T20:51:42Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 456,032 | <p>For cross language projects I found out that strings containing <a href="http://www.ietf.org/rfc/rfc3339.txt">RfC 3339</a> dates are the best way to go. A RfC 3339 date looks like this:</p>
<pre><code> 1985-04-12T23:20:50.52Z
</code></pre>
<p>I think most of the format is obvious. The only somewhat unusual thing ... | 66 | 2009-01-18T22:26:56Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 2,680,060 | <p>You can add the 'default' parameter to json.dumps to handle this:</p>
<pre><code>date_handler = lambda obj: (
obj.isoformat()
if isinstance(obj, datetime.datetime)
or isinstance(obj, datetime.date)
else None
)
json.dumps(datetime.datetime.now(), default=date_handler)
'"2010-04-20T20:08:21.634121"'
<... | 347 | 2010-04-21T03:09:43Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 3,049,307 | <p>Using <code>json</code>, you can subclass JSONEncoder and override the default() method to provide your own custom serializers:</p>
<pre><code>import json
import datetime
class DateTimeJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.i... | 46 | 2010-06-15T21:45:25Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 3,235,787 | <p>Here's a fairly complete solution for recursively encoding and decoding datetime.datetime and datetime.date objects using the standard library <code>json</code> module. This needs Python >= 2.6 since the <code>%f</code> format code in the datetime.datetime.strptime() format string is only supported in since then. Fo... | 29 | 2010-07-13T09:26:57Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 6,130,825 | <p>I've worked it out.</p>
<p>Let's say you have a Python datetime object, <em>d</em>, created with datetime.now(). Its value is:</p>
<pre><code>datetime.datetime(2011, 5, 25, 13, 34, 5, 787000)
</code></pre>
<p>You can serialize it to JSON as an ISO 8601 datetime string:</p>
<pre><code>import json
json.dumps(d... | 52 | 2011-05-25T20:55:37Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 20,602,937 | <p>On python side:</p>
<pre><code>import time, json
from datetime import datetime as dt
your_date = dt.now()
data = json.dumps(time.mktime(your_date.timetuple())*1000)
return data # data send to javascript
</code></pre>
<p>On javascript side:</p>
<pre><code>var your_date = new Date(data)
</code></pre>
<p>where data... | 4 | 2013-12-16T03:22:24Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 29,744,771 | <p>My advice is to use a library. There are several available at pypi.org.</p>
<p>I use this one, it it works good: <a href="https://pypi.python.org/pypi/asjson" rel="nofollow">https://pypi.python.org/pypi/asjson</a></p>
| 3 | 2015-04-20T10:01:34Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 32,224,522 | <p>Late in the game... :) </p>
<p>A very simple solution is to patch the json module default.
For example:</p>
<pre><code>import json
import datetime
json.JSONEncoder.default = lambda self,obj: (obj.isoformat() if isinstance(obj, datetime.datetime) else None)
</code></pre>
<p>Now, you can use <strong>json.dumps()<... | 7 | 2015-08-26T10:45:12Z | [
"javascript",
"python",
"json"
] |
JSON datetime between Python and JavaScript | 455,580 | <p>I want to send a datetime.datetime object in serialized form from Python using <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> and de-serialize in JavaScript using JSON. What is the best way to do this?</p>
| 331 | 2009-01-18T17:51:11Z | 34,685,105 | <p>Not much to add to the community wiki answer, except for <strong>timestamp</strong>!</p>
<p>Javascript uses the following format:</p>
<pre><code>new Date().toJSON() // "2016-01-08T19:00:00.123Z"
</code></pre>
<p>Python side (for the <code>json.dumps</code> handler, see the other answers):</p>
<pre><code>>>... | 2 | 2016-01-08T19:58:55Z | [
"javascript",
"python",
"json"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 455,634 | <p>You are running into the old problem with floating point numbers that all numbers cannot be represented. The command line is just showing you the full floating point form from memory. In floating point your rounded version is the same number. Since computers are binary they store floating point numbers as an integer... | 687 | 2009-01-18T18:23:53Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 455,658 | <p>What you can do is modify the output format:</p>
<pre><code>>>> a = 13.95
>>> a
13.949999999999999
>>> print "%.2f" % a
13.95
</code></pre>
| 36 | 2009-01-18T18:31:50Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 455,662 | <p>It's doing exactly what you told it to do, and working correctly. Read more about <a href="http://www.lahey.com/float.htm">floating point confusion</a> and maybe try <a href="http://docs.python.org/library/decimal.html">Decimal</a> objects instead.</p>
| 8 | 2009-01-18T18:33:45Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 455,678 | <p>Most numbers cannot be exactly represented in floats. If you want to round the number because that's what your mathematical formula or algorithm requires, then you want to use round. If you just want to restrict the display to a certain precision, then don't even use round and just format it as that string. (If you ... | 71 | 2009-01-18T18:40:03Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 456,343 | <p>The python tutorial has an appendix called: <a href="http://docs.python.org/tutorial/floatingpoint.html">Floating Point Arithmetic: Issues and Limitations</a>. Read it. It explains what is happening and why python is doing its best. It has even an example that matches yours. Let me quote a bit:</p>
<blockquote>
<pr... | 13 | 2009-01-19T02:05:53Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 6,539,677 | <p>There are new format specifications, here:</p>
<p><a href="http://docs.python.org/library/string.html#format-specification-mini-language">http://docs.python.org/library/string.html#format-specification-mini-language</a></p>
<p>You can do the same as:</p>
<pre><code>"{0:.2f}".format(13.949999999999999)
</code></pr... | 249 | 2011-06-30T18:53:13Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 18,438,167 | <p>Try codes below:</p>
<pre><code>>>> a = 0.99334
>>> a = int((a * 100) + 0.5) / 100.0 # Adding 0.5 rounds it up
>>> print a
0.99
</code></pre>
| 62 | 2013-08-26T06:46:25Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 20,512,207 | <p>With python < 3 (e.g. 2.6 or 2.7), there are two ways to do so.</p>
<pre><code># Option one
older_method_string = "%.9f" % numvar
# Option two (note ':' before the '.9f')
newer_method_string = "{:.9f}".format(numvar)
</code></pre>
<p>But note that for python versions above 3 (e.g. 3.2 or 3.3), option two is <... | 22 | 2013-12-11T06:37:46Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 22,821,930 | <p>for fix the floating point in type dynamic languages such as Python and Javascript I use this technique</p>
<pre><code># for example:
a=70000
b=0.14
c=a*b
print c # prints 980.0000000002
#try to fix
c=int(c * 10000)/100000
print c # prints 980
</code></pre>
| 3 | 2014-04-02T20:08:01Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 28,142,318 | <p>I feel that the simplest approach is to use the <code>format()</code> function.</p>
<p>For example:</p>
<pre><code>a = 13.949999999999999
format(a, '.2f')
13.95
</code></pre>
<p>This produces a float number as a string rounded to two decimal points.</p>
| 50 | 2015-01-25T22:26:58Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 30,571,233 | <pre><code>>>> int(0.999991*100)/100.0
>>> 0.99
</code></pre>
| -4 | 2015-06-01T10:32:30Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 32,222,599 | <p>To round a number to a resolution, the best way is the following one, which can work with any resolution (0.01 for 2 decimals or even other steps)</p>
<pre><code>>>> import numpy as np
>>> value = 13.949999999999999
>>> resolution = 0.01
>>> newValue = int(np.round(value/resoluti... | 2 | 2015-08-26T09:17:52Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 34,543,213 | <p>The method I use is that of string slicing. It's relatively quick and simple.</p>
<p>First, convert the float to a string, the choose the length you would like it to be.</p>
<pre><code>float = str(float)[:5]
</code></pre>
<p>In the single line above, we've converted the value to a string, then kept the string onl... | -6 | 2015-12-31T08:05:56Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 35,117,668 | <h2><strong>tldr;)</strong></h2>
<p>The rounding problem at input / output has been <strong>solved by Python 2.7.0</strong> definitively.</p>
<pre><code> import random
for x in iter(random.random, None): # verify FOREVER fixed :-)
assert float(repr(x)) == x # Reversible repr() conversion
... | 8 | 2016-01-31T18:33:53Z | [
"python",
"floating-point",
"precision"
] |
Limiting floats to two decimal points | 455,612 | <p>I want <code>a</code> to be rounded to <em>13.95</em>.</p>
<pre><code>>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
</code></pre>
<p>The <a href="https://docs.python.org/2/library/functions.html#round"><code>round</code></a> function does not work the way I expected.</p>
| 610 | 2009-01-18T18:16:41Z | 36,455,037 | <pre><code>def limit_float(num,len):
return float(str(num)[:len-1])
</code></pre>
| -9 | 2016-04-06T15:03:42Z | [
"python",
"floating-point",
"precision"
] |
Python 3 development and distribution challenges | 455,717 | <p>Suppose I've developed a general-purpose end user utility written in Python. Previously, I had just one version available which was suitable for Python later than version 2.3 or so. It was sufficient to say, "download Python if you need to, then run this script". There was just one version of the script in source co... | 8 | 2009-01-18T19:01:08Z | 455,725 | <p>For developement, option 3 is too cumbersome. Maintaining two branches is the easiest way although the way to do that will vary between VCSes. Many DVCS will be happier with separate repos (with a common ancestry to help merging) and centralized VCS will probably easier to work with with two branches. Option 1 is... | 2 | 2009-01-18T19:06:36Z | [
"python",
"version-control",
"python-3.x"
] |
Python 3 development and distribution challenges | 455,717 | <p>Suppose I've developed a general-purpose end user utility written in Python. Previously, I had just one version available which was suitable for Python later than version 2.3 or so. It was sufficient to say, "download Python if you need to, then run this script". There was just one version of the script in source co... | 8 | 2009-01-18T19:01:08Z | 455,763 | <p>Whichever option for development is chosen, most potential issues could be alleviated with thorough unit testing to ensure that the two versions produce matching output. That said, option 2 seems most natural to me: applying changes from one source tree to another source tree is a task (most) version control system... | 0 | 2009-01-18T19:37:11Z | [
"python",
"version-control",
"python-3.x"
] |
Python 3 development and distribution challenges | 455,717 | <p>Suppose I've developed a general-purpose end user utility written in Python. Previously, I had just one version available which was suitable for Python later than version 2.3 or so. It was sufficient to say, "download Python if you need to, then run this script". There was just one version of the script in source co... | 8 | 2009-01-18T19:01:08Z | 455,770 | <p>I would start by migrating to 2.6, which is very close to python 3.0. You might even want to wait for 2.7, which will be even closer to python 3.0.</p>
<p>And then, once you have migrated to 2.6 (or 2.7), I suggest you simply keep just one version of the script, with things like "if PY3K:... else:..." in the rare ... | 1 | 2009-01-18T19:44:26Z | [
"python",
"version-control",
"python-3.x"
] |
Python 3 development and distribution challenges | 455,717 | <p>Suppose I've developed a general-purpose end user utility written in Python. Previously, I had just one version available which was suitable for Python later than version 2.3 or so. It was sufficient to say, "download Python if you need to, then run this script". There was just one version of the script in source co... | 8 | 2009-01-18T19:01:08Z | 455,831 | <p>I don't think I'd take this path at all. It's painful whichever way you look at it. Really, unless there's strong commercial interest in keeping both versions simultaneously, this is more headache than gain. </p>
<p>I think it makes more sense to just keep developing for 2.x for now, at least for a few months, up t... | 2 | 2009-01-18T20:31:32Z | [
"python",
"version-control",
"python-3.x"
] |
Python 3 development and distribution challenges | 455,717 | <p>Suppose I've developed a general-purpose end user utility written in Python. Previously, I had just one version available which was suitable for Python later than version 2.3 or so. It was sufficient to say, "download Python if you need to, then run this script". There was just one version of the script in source co... | 8 | 2009-01-18T19:01:08Z | 455,840 | <p><strong>Edit:</strong> my original answer was based on the state of 2009, with Python 2.6 and 3.0 as the current versions. Now, with Python 2.7 and 3.3, there are other options. In particular, it is now quite feasible to use a single code base for Python 2 and Python 3.</p>
<p>See <a href="http://docs.python.org/3/... | 9 | 2009-01-18T20:38:22Z | [
"python",
"version-control",
"python-3.x"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,008 | <p>There are none. This is what modules are for: grouping related functions. Using a class full of static methods makes me cringe from Javaitis. The only time I would use a static function is if the function is an integral part of the class. (In fact, I'd probably want to use a class method anyway.)</p>
| 29 | 2009-01-18T22:12:04Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,013 | <p>I agree with Benjamin. Rather than having a bunch of static methods, you should probably have a bunch of functions. And if you want to organize them, you should think about using modules rather than classes. However, if you want to refactor your code to be OO, that's another matter.</p>
| 0 | 2009-01-18T22:15:47Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,016 | <p>Not only are there no advantages, but it makes things slower than using a module full of methods. There's much less need for static methods in python than there is for them in java or c#, they are used in very special cases.</p>
| 0 | 2009-01-18T22:16:26Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,018 | <p>No. It would be better to make them functions and if they are related, place them into their own module. For instance, if you have a class like this:</p>
<pre><code>class Something(object):
@staticmethod
def foo(x):
return x + 5
@staticmethod
def bar(x, y):
return y + 5 * x
</code>... | 11 | 2009-01-18T22:17:46Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,081 | <p>Classes are only useful when you have a set of functionality than interacts with a set of data (instance properties) that needs to be persisted between function calls and referenced in a discrete fashion.</p>
<p>If your class contains nothing other than static methods, then your class is just syntactic cruft, and s... | 1 | 2009-01-18T23:02:28Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,194 | <p>If your functions are dependent on each other or global state, consider also the third approach:</p>
<pre><code>class Something(object):
def foo(self, x):
return x + 5
def bar(self, x, y):
return y + 5 * self.foo(x)
something = Something()
</code></pre>
<p>Using this solution you can test... | 5 | 2009-01-19T00:16:32Z | [
"python",
"class",
"static-methods"
] |
Is there any advantage in using a Python class? | 456,001 | <p>I have a Python class full of static methods. What are the advantages and disadvantages of packaging these in a class rather than raw functions?</p>
| 15 | 2009-01-18T22:09:07Z | 456,222 | <p>Depends on the nature of the functions. If they're not strongly unrelated (minimal amount of calls between them) and they don't have any state then yes I'd say dump them into a module. However, you could be shooting yourself in the foot if you ever need to modify the behavior as you're throwing inheritance out the... | 0 | 2009-01-19T00:31:36Z | [
"python",
"class",
"static-methods"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,089 | <p>How about this:</p>
<pre><code>A = '1, 2,,3,4 '
B = [int(x) for x in A.split(',') if x.strip()]
</code></pre>
<p>x.strip() trims whitespace from the string, which will make it empty if the string is all whitespace. An empty string is "false" in a boolean context, so it's filtered by the if part of the list compre... | 26 | 2009-01-18T23:09:34Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,095 | <p>I'd guess regular expressions are the way to go: <a href="http://docs.python.org/library/re.html" rel="nofollow">http://docs.python.org/library/re.html</a></p>
| -1 | 2009-01-18T23:12:18Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,138 | <p>This will work, and never raise an exception, if all the numbers are ints. The <code>isdigit()</code> call is false if there's a decimal point in the string.</p>
<pre><code>>>> nums = ['1,,2,3,\n,4\n', '1,2,3,4', ',1,2,3,4,\t\n', '\n\t,1,2,3,,4\n']
>>> for n in nums:
... [ int(i.strip()) for i... | 0 | 2009-01-18T23:41:52Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,319 | <p>How about this?</p>
<pre><code>>>> a = "1,2,,3,4,"
>>> map(int,filter(None,a.split(",")))
[1, 2, 3, 4]
</code></pre>
<p>filter will remove all false values (i.e. empty strings), which are then mapped to int.</p>
<p>EDIT: Just tested this against the above posted versions, and it seems to be sign... | 1 | 2009-01-19T01:49:23Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,330 | <p>Generally, I try to avoid regular expressions, but if you want to split on a bunch of different things, they work. Try this:</p>
<pre><code>import re
result = [int(x) for x in filter(None, re.split('[,\n,\t]', A))]
</code></pre>
| 3 | 2009-01-19T01:54:24Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,401 | <p>Why not just wrap in a try except block which catches anything not an integer?</p>
| 0 | 2009-01-19T02:50:52Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 456,408 | <p>Mmm, functional goodness (with a bit of generator expression thrown in):</p>
<pre><code>a = "1,2,,3,4,"
print map(int, filter(None, (i.strip() for i in a.split(','))))
</code></pre>
<p>For full functional joy:</p>
<pre><code>import string
a = "1,2,,3,4,"
print map(int, filter(None, map(string.strip, a.split(','))... | 4 | 2009-01-19T02:54:49Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 458,259 | <p>Why accept inferior substitutes that cannot segfault your interpreter? With ctypes you can just call the real thing! :-)</p>
<pre><code># strtok in Python
from ctypes import c_char_p, cdll
try: libc = cdll.LoadLibrary('libc.so.6')
except WindowsError:
libc = cdll.LoadLibrary('msvcrt.dll')
libc.strtok.restype... | 0 | 2009-01-19T16:43:40Z | [
"python"
] |
How do I do what strtok() does in C, in Python? | 456,084 | <p>I am learning Python and trying to figure out an efficient way to tokenize a string of numbers separated by commas into a list. Well formed cases work as I expect, but less well formed cases not so much.</p>
<p>If I have this:</p>
<pre><code>A = '1,2,3,4'
B = [int(x) for x in A.split(',')]
B results in [1, 2, 3, ... | 7 | 2009-01-18T23:03:45Z | 40,071,673 | <p>For the sake of completeness, I will answer this seven year old question:
The C program that uses strtok:</p>
<pre><code>int main()
{
char myLine[]="This is;a-line,with pieces";
char *p;
for(p=strtok(myLine, " ;-,"); p != NULL; p=strtok(NULL, " ;-,"))
{
printf("piece=%s\n", p);
}
}
</cod... | 0 | 2016-10-16T14:55:10Z | [
"python"
] |
SCons problem - dont understand Variables class | 456,100 | <p>I'm working on an SConstruct build file for a project and I'm trying to update from Options to Variables, since Options is being deprecated. I don't understand how to use Variables though. I have 0 python experience which is probably contributing to this.</p>
<p>For example, I have this:</p>
<pre><code>opts = Vari... | 3 | 2009-01-18T23:13:16Z | 476,395 | <p>That specific error tells you that class <code>Variables</code> hasn't implemented python's <code>__getitem__</code> <a href="http://docs.python.org/reference/datamodel.html#object.__getitem__" rel="nofollow">interface</a> which would allow you to use <code>[ ...]</code> on <code>opts</code>. If all you want to do... | 1 | 2009-01-24T17:47:47Z | [
"python",
"variables",
"scons"
] |
SCons problem - dont understand Variables class | 456,100 | <p>I'm working on an SConstruct build file for a project and I'm trying to update from Options to Variables, since Options is being deprecated. I don't understand how to use Variables though. I have 0 python experience which is probably contributing to this.</p>
<p>For example, I have this:</p>
<pre><code>opts = Vari... | 3 | 2009-01-18T23:13:16Z | 542,096 | <p>Typically you would store the variables in your environment for later testing.</p>
<pre><code>opts = Variables()
opts.Add('fcgi',0)
env = Environment(variables=opts, ...)
</code></pre>
<p>Then later you can test:</p>
<pre><code>if env['fcgi'] == 0:
# do something
</code></pre>
| 5 | 2009-02-12T16:18:08Z | [
"python",
"variables",
"scons"
] |
How to use cvxopt with DSDP? | 456,224 | <p>I'm trying to use DSDP (semidefinite programming package) with cvxopt. I have both of them installed (matlab version for DSDP). I have Python 2.5.2.</p>
<p>When trying to use </p>
<pre><code>dsp(..., solver='dsdp')
</code></pre>
<p>I get an errors mentioning solvers.dsdp is not installed.</p>
<p>How to I make th... | 0 | 2009-01-19T00:33:12Z | 456,627 | <p>I would suggest asking this question on the <a href="http://groups.google.com/group/cvxopt" rel="nofollow">cvxopt</a> group.</p>
| 0 | 2009-01-19T05:39:24Z | [
"python",
"convex-optimization"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 456,491 | <p>I believe you need to create a file called <code>__init__.py</code> in the Models directory so that python treats it as a module.</p>
<p>Then you can do:</p>
<pre><code>from Models.user import User
</code></pre>
<p>You can include code in the <code>__init__.py</code> (for instance initialization code that a few d... | 64 | 2009-01-19T04:00:15Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 456,494 | <p>You have to create <code>__init__.py</code> on the <code>Models</code> subfolder. The file may be empty. It defines a package.</p>
<p>Then you can do:</p>
<pre><code>from Models.user import User
</code></pre>
<p>Read all about it in python tutorial, <a href="http://docs.python.org/tutorial/modules.html#packages">... | 18 | 2009-01-19T04:02:22Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 456,495 | <p>You're missing __init__.py. From the Python tutorial:</p>
<blockquote>
<p>The __init__.py files are required to
make Python treat the directories as
containing packages; this is done to
prevent directories with a common
name, such as string, from
unintentionally hiding valid modules
that occur later o... | 7 | 2009-01-19T04:02:31Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 457,630 | <blockquote>
<p>import user</p>
<p>u=user.User() #error on this line</p>
</blockquote>
<p>Because of the lack of __init__ mentioned above, you would expect an ImportError which would make the problem clearer.</p>
<p>You don't get one because 'user' is also an existing module in the standard library. Your impor... | 9 | 2009-01-19T13:51:51Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 9,643,062 | <p>The right way to import a module located on a parent folder, when you don't have a standard package structure, is:</p>
<pre><code>import os, sys
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(CURRENT_DIR))
</code></pre>
<p>(you can merge the last two lines but this way is ... | 5 | 2012-03-10T01:15:35Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 18,153,930 | <p>The solution by glarrain works the best. I ran into the issue of Python being unable to recognize my python modules and gave me 'module not found' error. For me , even after adding <code>__init__.py</code> files and importing the modules appropriately, I still got the same error.
I resolved the issue by following ... | 1 | 2013-08-09T19:08:59Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 22,525,533 | <p>how do you write out the parameters <code>os.path.dirname</code>.... command?</p>
<pre><code>import os, sys
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(CURRENT_DIR))
</code></pre>
| 1 | 2014-03-20T06:51:17Z | [
"python"
] |
Can't get Python to import from a different folder | 456,481 | <p>I can't seem to get Python to import a module in a subfolder. I get the error when I try to create an instance of the class from the imported module, but the import itself succeeds. Here is my directory structure:</p>
<pre><code>Server
-server.py
-Models
--user.py
</code></pre>
<p>Here's the conten... | 40 | 2009-01-19T03:53:35Z | 39,152,647 | <p>My preferred way is to have __init__.py on every directory that contains modules that get used by other modules, and in the entry point, override sys.path as below:</p>
<pre><code>def get_path(ss):
return os.path.join(os.path.dirname(__file__), ss)
sys.path += [
get_path('Server'),
get_path('Models')
]
</cod... | 0 | 2016-08-25T18:45:55Z | [
"python"
] |
Throttling with urllib2 | 456,649 | <p>is it possible to easily cap the kbps when using <code>urllib2</code>?
If it is, any code examples or resources you could direct me to would be greatly appreciated.</p>
| 8 | 2009-01-19T05:53:58Z | 456,668 | <p>There is the <code>urlretrieve(url, filename=None, reporthook=None, data=None)</code> function in the <code>urllib</code> module.
If you implement the <code>reporthook</code>-function/object as either a <a href="http://en.wikipedia.org/wiki/Token_bucket">token bucket</a>, or a leaky bucket, you have your global rate... | 18 | 2009-01-19T06:06:17Z | [
"python",
"urllib2",
"bandwidth-throttling"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 456,731 | <p>Assuming you need separate classes for different registrars (though it's not obvious in your example) your solution looks okay, though <strong>RegistrarA</strong> and <strong>RegistrarB</strong> probably share functionality and could be derived from an <a href="http://docs.python.org/2/library/abc.html" rel="nofollo... | 15 | 2009-01-19T06:39:00Z | [
"python",
"factory"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 456,747 | <p>I think using a function is fine.</p>
<p>The more interesting question is how do you determine which registrar to load? One option is to have an abstract base Registrar class which concrete implementations subclass, then iterate over its <code>__subclasses__()</code> calling an <code>is_registrar_for()</code> class... | 62 | 2009-01-19T06:49:03Z | [
"python",
"factory"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 545,383 | <p>In Python you can change the actual class directly:</p>
<pre><code>class Domain(object):
def __init__(self, domain):
self.domain = domain
if ...:
self.__class__ = RegistrarA
else:
self.__class__ = RegistrarB
</code></pre>
<p>And then following will work.</p>
<pre><code>com = Domain('test... | 10 | 2009-02-13T09:51:01Z | [
"python",
"factory"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 16,945,996 | <p>how about something like </p>
<pre><code>class Domain(object):
registrars = []
@classmethod
def add_registrar( cls, reg ):
registrars.append( reg )
def __init__( self, domain ):
self.domain = domain
for reg in self.__class__.registrars:
if reg.is_registrar_for( domain ):
self.... | 1 | 2013-06-05T17:13:53Z | [
"python",
"factory"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 16,946,959 | <p>I have this problem all the time. If you have the classes embedded in your application (and its modules) then you can use a function; but if you load plugins dynamically, you need something more dynamic -- registering the classes with a factory via metaclasses automatically.</p>
<p>Here is a pattern I'm sure I lif... | 1 | 2013-06-05T18:10:20Z | [
"python",
"factory"
] |
Class factory in Python | 456,672 | <p>I'm new to Python and need some advice implementing the scenario below.</p>
<p>I have two classes for managing domains at two different registrars. Both have the same interface, e.g.</p>
<pre><code>class RegistrarA(Object):
def __init__(self, domain):
self.domain = domain
def lookup(self):
... | 46 | 2009-01-19T06:08:16Z | 37,476,396 | <p>You can create a 'wrapper' class and overload its <code>__new__()</code> method to return instances of the specialized sub-classes, e.g.:</p>
<pre><code>class Registrar(object):
def __new__(self, domain):
if ...:
return RegistrarA(domain)
elif ...:
return RegistrarB(domai... | 1 | 2016-05-27T06:34:03Z | [
"python",
"factory"
] |
How to handle the error that occurs on giving wrong number of parameters in a function call in Python? | 456,673 | <p>When i give wrong number of parameters in a function , i get errors.
How do I handle it?</p>
<p>I gave </p>
<pre><code>def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
</code></pre>
<p>It is not working.</p>
<p>Help please.</p>
| 0 | 2009-01-19T06:09:47Z | 456,679 | <p>If you remove the <code>try...catch</code> parts it should show you what kind of exception it is throwing.</p>
| -2 | 2009-01-19T06:13:01Z | [
"python",
"function-call"
] |
How to handle the error that occurs on giving wrong number of parameters in a function call in Python? | 456,673 | <p>When i give wrong number of parameters in a function , i get errors.
How do I handle it?</p>
<p>I gave </p>
<pre><code>def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
</code></pre>
<p>It is not working.</p>
<p>Help please.</p>
| 0 | 2009-01-19T06:09:47Z | 456,681 | <p>The caller triggers this exception, not the receiver.</p>
<p>If you want the receiving function to explicitly check argument count you'll need to use varargs:</p>
<pre><code>def fun_name(*args):
if len(args) != 2:
raise TypeError('Two arguments required')
</code></pre>
| 4 | 2009-01-19T06:13:08Z | [
"python",
"function-call"
] |
How to handle the error that occurs on giving wrong number of parameters in a function call in Python? | 456,673 | <p>When i give wrong number of parameters in a function , i get errors.
How do I handle it?</p>
<p>I gave </p>
<pre><code>def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
</code></pre>
<p>It is not working.</p>
<p>Help please.</p>
| 0 | 2009-01-19T06:09:47Z | 456,683 | <p>You need to handle it where you call the function.</p>
<pre><code>try:
fun_name(...)
except TypeError:
print "error!"
</code></pre>
| 4 | 2009-01-19T06:13:24Z | [
"python",
"function-call"
] |
How to handle the error that occurs on giving wrong number of parameters in a function call in Python? | 456,673 | <p>When i give wrong number of parameters in a function , i get errors.
How do I handle it?</p>
<p>I gave </p>
<pre><code>def fun_name(...):
try:
...
except TypeError:
print 'Wrong no of arg'
</code></pre>
<p>It is not working.</p>
<p>Help please.</p>
| 0 | 2009-01-19T06:09:47Z | 457,026 | <p>If you call a function with the wrong number of parameters then there are two possibilities:</p>
<ul>
<li>Either you design your function to handle an arbitrary number of arguments. Then you should know what to do with the extra arguments. The answer of Alec Thomas shows you how to handle this case.</li>
<li>Or you... | 0 | 2009-01-19T09:38:55Z | [
"python",
"function-call"
] |
Evaluate my Python server structure | 456,753 | <p>I'm building a game server in Python and I just wanted to get some input on the architecture of the server that I was thinking up.</p>
<p>So, as we all know, Python cannot scale across cores with a single process. Therefore, on a server with 4 cores, I would need to spawn 4 processes. </p>
<p>Here is the steps tak... | 3 | 2009-01-19T06:51:37Z | 456,824 | <p>Sounds like you'll want to look at <a href="http://pyprocessing.berlios.de/">PyProcessing</a>, now included in Python 2.6 and beyond as <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing</a>. It takes care of a lot of the machinery of dealing with multiple processes.</p>
<p>An alternative... | 5 | 2009-01-19T07:50:14Z | [
"python"
] |
AttributeError: 'module' object has no attribute 'model' | 456,867 | <p>Can anyone help me please to solve this..</p>
<pre><code>from django.db import models
# Create your models here.
class Poll(models.model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice... | 14 | 2009-01-19T08:19:25Z | 456,880 | <p>As the error message says in the last line: the module <code>models</code> in the file <code>c:\projects\mysite..\mysite\polls\models.py</code> contains no class <code>model</code>. This error occurs in the definition of the <code>Poll</code> class:</p>
<pre><code>class Poll(models.model):
</code></pre>
<p>Either ... | 2 | 2009-01-19T08:29:13Z | [
"python",
"django"
] |
AttributeError: 'module' object has no attribute 'model' | 456,867 | <p>Can anyone help me please to solve this..</p>
<pre><code>from django.db import models
# Create your models here.
class Poll(models.model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice... | 14 | 2009-01-19T08:19:25Z | 456,886 | <p>It's called models.Model and not models.model (case sensitive). Fix your Poll model like this -</p>
<pre><code>class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
</code></pre>
<p>Hope that helps...</p>
| 58 | 2009-01-19T08:32:41Z | [
"python",
"django"
] |
AttributeError: 'module' object has no attribute 'model' | 456,867 | <p>Can anyone help me please to solve this..</p>
<pre><code>from django.db import models
# Create your models here.
class Poll(models.model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice... | 14 | 2009-01-19T08:19:25Z | 10,863,390 | <p>I realized that by looking at the stack trace it was trying to load my own script in place of another module called the same way,i.e., my script was called random.py and when a module i used was trying to import the "random" package, it was loading my script causing a circular reference and so i renamed it and delet... | 1 | 2012-06-02T15:04:32Z | [
"python",
"django"
] |
AttributeError: 'module' object has no attribute 'model' | 456,867 | <p>Can anyone help me please to solve this..</p>
<pre><code>from django.db import models
# Create your models here.
class Poll(models.model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice... | 14 | 2009-01-19T08:19:25Z | 20,197,595 | <p>I also got the same error but I noticed that I had typed in Foreign*<em>k</em>*ey and not Foreign*<em>K</em>*ey,(capital K) if there is a newbie out there, check out spelling and caps. </p>
| 3 | 2013-11-25T16:04:54Z | [
"python",
"django"
] |
Extending python - to swig, not to swig or Cython | 456,884 | <p>I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance.</p>
<p>With the help of swig you almost don't need to care about arguments etc. Everything works fine.</p>
<p>Now my question: swig creates a quite large py-file which does a lot of 'ch... | 55 | 2009-01-19T08:32:06Z | 456,894 | <p>If its not a big extension, boost::python might also be an option, it executes faster than swig, because you control what's happening, but it'll take longer to dev.</p>
<p>Anyways swig's overhead is acceptable if the amount of work within a single call is large enough. For example if you issue is that you have some... | 3 | 2009-01-19T08:34:30Z | [
"python",
"c++",
"c",
"swig",
"cython"
] |
Extending python - to swig, not to swig or Cython | 456,884 | <p>I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance.</p>
<p>With the help of swig you almost don't need to care about arguments etc. Everything works fine.</p>
<p>Now my question: swig creates a quite large py-file which does a lot of 'ch... | 55 | 2009-01-19T08:32:06Z | 456,949 | <p>You should consider Boost.Python if you are not planning to generate bindings for other languages as well with swig. </p>
<p>If you have a lot of functions and classes to bind, <a href="http://sourceforge.net/projects/pygccxml/">Py++</a> is a great tool that automatically generates the needed code to make the bindi... | 53 | 2009-01-19T09:00:41Z | [
"python",
"c++",
"c",
"swig",
"cython"
] |
Extending python - to swig, not to swig or Cython | 456,884 | <p>I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance.</p>
<p>With the help of swig you almost don't need to care about arguments etc. Everything works fine.</p>
<p>Now my question: swig creates a quite large py-file which does a lot of 'ch... | 55 | 2009-01-19T08:32:06Z | 456,953 | <p>Before giving up on your python code, have a look at <a href="http://code.google.com/p/shedskin/" rel="nofollow">ShedSkin</a>. They claim better performance than Psyco on some code (and also state that it is still experimental).</p>
<p>Else, there are several choices for binding C/C++ code to python.</p>
<p>Boost ... | 3 | 2009-01-19T09:02:57Z | [
"python",
"c++",
"c",
"swig",
"cython"
] |
Extending python - to swig, not to swig or Cython | 456,884 | <p>I found the bottleneck in my python code, played around with psycho etc. Then decided to write a c/c++ extension for performance.</p>
<p>With the help of swig you almost don't need to care about arguments etc. Everything works fine.</p>
<p>Now my question: swig creates a quite large py-file which does a lot of 'ch... | 55 | 2009-01-19T08:32:06Z | 456,995 | <p>For sure you will always have a performance gain doing this by hand, but the gain will be very small compared to the effort required to do this. I don't have any figure to give you but I don't recommend this, because you will need to maintain the interface by hand, and this is not an option if your module is large!<... | 23 | 2009-01-19T09:20:19Z | [
"python",
"c++",
"c",
"swig",
"cython"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.