title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
wxPython gauge problem (skipping)
1,235,884
<p>Pastebin link: <a href="http://pastebin.com/f40ae1bcf" rel="nofollow">http://pastebin.com/f40ae1bcf</a></p> <p>The problem: I made a wx.Gauge, with the range of 50. Then a function that updates Gauge's value when the program is idle. When the gauge is filled by around 50% it empties and doesn't show anything for a ...
1
2009-08-05T21:33:06Z
1,236,293
<p>Are you sure that the code you posted is the code that is giving you the problems? This code looks very similar to the demo code for wxPython, and works on my machine without problems.</p> <p>If you did actually set your gauge's value to 50, that could be your issue. You have to be sure not to exceed that range of ...
0
2009-08-05T23:36:26Z
[ "python", "wxpython" ]
wxPython gauge problem (skipping)
1,235,884
<p>Pastebin link: <a href="http://pastebin.com/f40ae1bcf" rel="nofollow">http://pastebin.com/f40ae1bcf</a></p> <p>The problem: I made a wx.Gauge, with the range of 50. Then a function that updates Gauge's value when the program is idle. When the gauge is filled by around 50% it empties and doesn't show anything for a ...
1
2009-08-05T21:33:06Z
1,236,327
<p>A few things.</p> <ul> <li>I can't reproduce this on my iMac, it goes all the way to full. Python 2.5.4, wxPython 2.8.9.2</li> <li>Idle events can come at strange times. Try adding <code>print event</code> to your idle handler to see exactly when those events are coming. A timer would be best. Is the gauge movi...
1
2009-08-05T23:51:15Z
[ "python", "wxpython" ]
wxPython gauge problem (skipping)
1,235,884
<p>Pastebin link: <a href="http://pastebin.com/f40ae1bcf" rel="nofollow">http://pastebin.com/f40ae1bcf</a></p> <p>The problem: I made a wx.Gauge, with the range of 50. Then a function that updates Gauge's value when the program is idle. When the gauge is filled by around 50% it empties and doesn't show anything for a ...
1
2009-08-05T21:33:06Z
1,237,498
<p>After more tests I discovered that I must override the range of 2 units to display the gauge when completely full. On windows vista it seems not to cause problems. Does it cause problems on linux or mac?</p>
0
2009-08-06T07:56:48Z
[ "python", "wxpython" ]
Compiling python modules whith DEBUG defined on MSVC
1,236,060
<p>Python rather stupidly has a pragma directive in its include files that forces a link against <code>python26_d.lib</code> when the <code>DEBUG</code> preprocessor variable is defined. This is a problem because the python installer doesn't come with <code>python26_d.lib</code>! So I can't build applications in msvc i...
17
2009-08-05T22:16:28Z
1,236,116
<p>From <a href="http://mail.python.org/pipermail/python-list/2009-March/1198717.html">python list</a></p> <blockquote> <p>As a workaround to the situation, try to copy the file python26.dll to python26_d.dll. (I'm not sure this will work; you say you are building a SWIG library in debug mode, and it's po...
19
2009-08-05T22:33:08Z
[ "python", "debugging", "visual-c++" ]
Compiling python modules whith DEBUG defined on MSVC
1,236,060
<p>Python rather stupidly has a pragma directive in its include files that forces a link against <code>python26_d.lib</code> when the <code>DEBUG</code> preprocessor variable is defined. This is a problem because the python installer doesn't come with <code>python26_d.lib</code>! So I can't build applications in msvc i...
17
2009-08-05T22:16:28Z
2,190,931
<p>This works also when linking with static libraries. I made a copy of python26.lib, and renamed it python26_d.lib. I commented out the line #define PY_DEBUG in pyconfig.h. Also changed the pragma to "pragma comment(lib,"python26.lib")" on line 332. Voila! It worked.</p>
2
2010-02-03T09:33:35Z
[ "python", "debugging", "visual-c++" ]
Compiling python modules whith DEBUG defined on MSVC
1,236,060
<p>Python rather stupidly has a pragma directive in its include files that forces a link against <code>python26_d.lib</code> when the <code>DEBUG</code> preprocessor variable is defined. This is a problem because the python installer doesn't come with <code>python26_d.lib</code>! So I can't build applications in msvc i...
17
2009-08-05T22:16:28Z
2,454,541
<p>You can also go the other way: switch to «Release» and then debug it. you need to enable generation of debugging symbols info in project properties in compiler and linker prefs; MSDN <a href="http://msdn.microsoft.com/en-us/library/fsk896zz%28VS.71%29.aspx" rel="nofollow">here</a> will tell you exactly what option...
4
2010-03-16T13:15:09Z
[ "python", "debugging", "visual-c++" ]
Compiling python modules whith DEBUG defined on MSVC
1,236,060
<p>Python rather stupidly has a pragma directive in its include files that forces a link against <code>python26_d.lib</code> when the <code>DEBUG</code> preprocessor variable is defined. This is a problem because the python installer doesn't come with <code>python26_d.lib</code>! So I can't build applications in msvc i...
17
2009-08-05T22:16:28Z
3,548,850
<p>After you comment out "#define Py_DEBUG" on line 332 and modify</p> <pre><code># ifdef _DEBUG # pragma comment(lib,"python26_d.lib") # else </code></pre> <p>to</p> <pre><code># ifdef _DEBUG # pragma comment(lib,"python26.lib") # else </code></pre> <p>you do not need to python26_d.lib anymore.</p>
7
2010-08-23T15:06:34Z
[ "python", "debugging", "visual-c++" ]
Compiling python modules whith DEBUG defined on MSVC
1,236,060
<p>Python rather stupidly has a pragma directive in its include files that forces a link against <code>python26_d.lib</code> when the <code>DEBUG</code> preprocessor variable is defined. This is a problem because the python installer doesn't come with <code>python26_d.lib</code>! So I can't build applications in msvc i...
17
2009-08-05T22:16:28Z
26,108,616
<p>Based on all answers I successfully disabled <code>_DEBUG</code> temporary:</p> <pre><code>#if _DEBUG #define _DEBUG_IS_ENABLED #undef _DEBUG #endif #include "pyconfig.h" #if defined(_DEBUG_IS_ENABLED) #define _DEBUG #endif </code></pre>
4
2014-09-29T20:48:31Z
[ "python", "debugging", "visual-c++" ]
py2app error: "can't copy '%s': doesn't exist or not a regular file"
1,236,104
<p>I'm trying to pack my Python app with py2app. I'm running the <code>setup.py</code> I created, and I get this error:</p> <pre><code> File "C:\Python26\lib\distutils\file_util.py", line 119, in copy_file "can't copy '%s': doesn't exist or not a regular file" % src DistutilsFileError: can't copy '--dist-dir': do...
0
2009-08-05T22:30:04Z
1,236,111
<p>It looks like, for some reason or other, it's trying to interpret the command-line switch <code>--dist-dir</code> as a filename. Perhaps the actual switch is named something else and you typo'd it? Or perhaps it needs to be specified in a different order?</p>
1
2009-08-05T22:31:49Z
[ "python", "osx", "py2app" ]
py2app error: "'module' object has no attribute 'symlink'"
1,236,172
<p>I'm trying to pack my Python app with py2app. I'm running the setup.py I created, and I get this error:</p> <pre><code>Traceback (most recent call last): File "C:\Python26\lib\site-packages\py2app-0.3.6-py2.6.egg\py2app\build_app.py ", line 548, in _run self.run_normal() File "C:\Python26\lib\site-packages\...
0
2009-08-05T22:49:20Z
1,236,175
<p><code>os.symlink</code> is only available on Unix and Unix-like operating systems (including the Mac), not Windows.</p> <p>py2app is for the Mac - are you deliberately running it on Windows? Did you mean to use <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a>?</p>
1
2009-08-05T22:51:38Z
[ "python", "osx", "py2app" ]
How do I see stdout when running Django tests?
1,236,285
<p>When I run tests with <code>./manage.py test</code>, whatever I send to the standard output through <code>print</code> doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass).</p>
24
2009-08-05T23:34:08Z
1,236,326
<p>You probably have some intermediate test runner, such as Nose, intercepting and storing stdout. Try either running the Django tests directly, or write to stderr instead.</p>
4
2009-08-05T23:51:09Z
[ "python", "django", "debugging", "testing", "stdout" ]
How do I see stdout when running Django tests?
1,236,285
<p>When I run tests with <code>./manage.py test</code>, whatever I send to the standard output through <code>print</code> doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass).</p>
24
2009-08-05T23:34:08Z
1,239,545
<p>Checked <code>TEST_RUNNER</code> in <code>settings.py</code>, it's using a project-specific runner that calls out to Nose. <em>Nose has the <code>-s</code> option to stop it from capturing <code>stdout</code></em>, but if I run:</p> <p><code>./manage.py test -s</code></p> <p><code>manage.py</code> captures it firs...
28
2009-08-06T15:09:16Z
[ "python", "django", "debugging", "testing", "stdout" ]
How do I see stdout when running Django tests?
1,236,285
<p>When I run tests with <code>./manage.py test</code>, whatever I send to the standard output through <code>print</code> doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass).</p>
24
2009-08-05T23:34:08Z
22,777,773
<p>Yeah, this issue is cause by <code>NoseTestSuiteRunner</code>. Add <code>-- -s</code> is a tricky but not the best solutions. Try to add follow lines in the <code>settings.py</code>:</p> <pre><code>NOSE_ARGS = ['--nocapture', '--nologcapture',] </code></pre> <p>Which solved my problems.</p>
22
2014-04-01T05:48:07Z
[ "python", "django", "debugging", "testing", "stdout" ]
Creating multiple Python modules in different directories that share a portion of the package structure
1,236,443
<p>I'm working on a Django project that contains a single application. The application will be released under the GPL so I want to develop it separately from the project - a personal site using the app. I'm attempting to use a package structure based on my domain name for both the project and the app, and that's wher...
2
2009-08-06T00:41:28Z
1,236,518
<p>Your analysis seems about right. The python path is used to locate modules to import; python imports the first one it finds. I'm not sure what you could do to work around this other than name your modules different things or put them in the same location in the search path.</p>
1
2009-08-06T01:18:44Z
[ "python", "django" ]
Creating multiple Python modules in different directories that share a portion of the package structure
1,236,443
<p>I'm working on a Django project that contains a single application. The application will be released under the GPL so I want to develop it separately from the project - a personal site using the app. I'm attempting to use a package structure based on my domain name for both the project and the app, and that's wher...
2
2009-08-06T00:41:28Z
1,236,581
<p>You basically have two modules named the same thing (mydomain). Why not set up your PYTHONPATH like so?</p> <pre><code>$HOME/django-sites:$HOME/django-apps/mydomain </code></pre> <p>It would avoid your import problems.</p>
1
2009-08-06T01:45:47Z
[ "python", "django" ]
Creating multiple Python modules in different directories that share a portion of the package structure
1,236,443
<p>I'm working on a Django project that contains a single application. The application will be released under the GPL so I want to develop it separately from the project - a personal site using the app. I'm attempting to use a package structure based on my domain name for both the project and the app, and that's wher...
2
2009-08-06T00:41:28Z
1,236,696
<p>Here's how <code>__path__</code> in a package's <code>__init__.py</code> is intended to be used:</p> <pre><code>$ export PYTHONPATH=$HOME/django-sites $ ls -d $HOME/django* django-apps/ django-sites/ $ cat /tmp/django-sites/mydomain/__init__.py import os _components = __path__[0].split(os.path.sep) if _component...
4
2009-08-06T02:31:53Z
[ "python", "django" ]
How do I use PIL with Tkinter?
1,236,540
<p>I'm missing something at a very basic level when it comes to loading an image using PIL and displaying it in a window created by Tkinter. The simplest form of what I'm trying to do is:</p> <pre><code>import Tkinter as TK from PIL import Image, ImageTk im = Image.open("C:\\tinycat.jpg") tkIm = ImageTk.PhotoImage(im...
3
2009-08-06T01:25:27Z
1,236,579
<p>Tkinter has to be instantiated before you call ImageTk.PhotoImage():</p> <pre><code>TK.Tk() </code></pre>
6
2009-08-06T01:44:20Z
[ "python", "python-imaging-library", "tkinter" ]
How do I use PIL with Tkinter?
1,236,540
<p>I'm missing something at a very basic level when it comes to loading an image using PIL and displaying it in a window created by Tkinter. The simplest form of what I'm trying to do is:</p> <pre><code>import Tkinter as TK from PIL import Image, ImageTk im = Image.open("C:\\tinycat.jpg") tkIm = ImageTk.PhotoImage(im...
3
2009-08-06T01:25:27Z
14,996,618
<p>It's very true what Meredith said you need to add that line for sure!</p> <p>I would like to show you my image formatting and then compare it to yours and see if there any different, my code for a image is</p> <pre><code>master.image = PhotoImage(file="Banditlogo.gif") w = Label(master, image=master.image) w.photo...
-1
2013-02-21T07:19:07Z
[ "python", "python-imaging-library", "tkinter" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
1,236,619
<p>First choice: use the existing join template tag.</p> <p><a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#join">http://docs.djangoproject.com/en/dev/ref/templates/builtins/#join</a></p> <p>Here's their example</p> <pre><code>{{ value|join:" // " }} </code></pre> <p>Second choice: do it in th...
89
2009-08-06T02:02:37Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
1,236,716
<p>I would suggest a custom django templating <em>filter</em> rather than a custom <em>tag</em> -- filter is handier and simpler (where appropriate, like here). <code>{{ fruits | joinby:", " }}</code> looks like what I'd want to have for the purpose... with a custom <code>joinby</code> filter:</p> <pre><code>def joinb...
26
2009-08-06T02:41:01Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
1,242,107
<p>Here's the filter I wrote to solve my problem:</p> <pre><code>def join_with_commas(obj_list): """Takes a list of objects and returns their unicode representations, seperated by commas and with 'and' between the penultimate and final items For example, for a list of fruit objects: [&lt;Fruit: apples&...
5
2009-08-06T23:37:27Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
3,649,002
<p>Here's a super simple solution. Put this code into comma.html:</p> <pre><code>{% if not forloop.last %}{% ifequal forloop.revcounter 2 %} and {% else %}, {% endifequal %}{% else %}{% endif %} </code></pre> <p>And now wherever you'd put the comma, include "comma.html" instead:</p> <pre><code>{% for cat in cats %}...
47
2010-09-06T04:28:13Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
14,798,747
<p>If you want a '.' on the end of Michael Matthew Toomim's answer, then use:</p> <pre><code>{% if not forloop.last %}{% ifequal forloop.revcounter 2 %} and {% else %}, {% endifequal %}{% else %}{% endif %}{% if forloop.last %}.{% endif %} </code></pre>
4
2013-02-10T14:28:14Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
16,820,985
<p>Django doesn't have support for this out-of-the-box. You can define a custom filter for this:</p> <pre><code>from django import template register = template.Library() @register.filter def join_and(value): """Given a list of strings, format them with commas and spaces, but with 'and' at the end. &gt...
1
2013-05-29T18:09:03Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
35,526,258
<p>If you like one-liners:</p> <pre><code>@register.filter def lineup(ls): return ', '.join(ls[:-1])+' and '+ls[-1] if len(ls)&gt;1 else ls[0] </code></pre> <p>and then in the template:</p> <pre><code>{{ fruits|lineup }} </code></pre>
0
2016-02-20T17:00:32Z
[ "python", "django", "list", "django-templates" ]
Comma separated lists in django templates
1,236,593
<p>If <code>fruits</code> is the list <code>['apples', 'oranges', 'pears']</code>,</p> <p>is there a quick way using django template tags to produce "apples, oranges, and pears"?</p> <p>I know it's not difficult to do this using a loop and <code>{% if counter.last %}</code> statements, but because I'm going to use th...
41
2009-08-06T01:50:18Z
37,598,156
<p>On the Django template this all you need to do for establishing a comma after each fruit. The comma will stop once its reached the last fruit.</p> <pre><code>{% if not forloop.last %}, {% endif %} </code></pre>
3
2016-06-02T17:11:26Z
[ "python", "django", "list", "django-templates" ]
Is 'for x in array' always result in sorted x? [Python/NumPy]
1,236,695
<p>For arrays and lists in Python and Numpy are the following lines equivalent:</p> <pre><code>itemlist = [] for j in range(len(myarray)): item = myarray[j] itemlist.append(item) </code></pre> <p>and:</p> <pre><code>itemlist = [] for item in myarray: itemlist.append(item) </code></pre> <p>I'm interested...
4
2009-08-06T02:31:46Z
1,236,701
<p>Yes, it's entirely guaranteed. <code>for item in myarray</code> (where <code>myarray</code> is a <em>sequence</em>, which includes numpy's arrays, builtin lists, Python's array.arrays, etc etc), is in fact equivalent in Python to:</p> <pre><code>_aux = 0 while _aux &lt; len(myarray): item = myarray[_aux] ...et...
9
2009-08-06T02:34:00Z
[ "python", "arrays", "list", "numpy" ]
Is 'for x in array' always result in sorted x? [Python/NumPy]
1,236,695
<p>For arrays and lists in Python and Numpy are the following lines equivalent:</p> <pre><code>itemlist = [] for j in range(len(myarray)): item = myarray[j] itemlist.append(item) </code></pre> <p>and:</p> <pre><code>itemlist = [] for item in myarray: itemlist.append(item) </code></pre> <p>I'm interested...
4
2009-08-06T02:31:46Z
1,236,709
<p>Yes, the <a href="http://docs.python.org/reference/compound%5Fstmts.html#for" rel="nofollow">Python Language Reference</a> guarantees this (emphasis is mine):</p> <pre><code> for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] </code></pre> <blockquote> <p>"The suite ...
6
2009-08-06T02:39:29Z
[ "python", "arrays", "list", "numpy" ]
Is 'for x in array' always result in sorted x? [Python/NumPy]
1,236,695
<p>For arrays and lists in Python and Numpy are the following lines equivalent:</p> <pre><code>itemlist = [] for j in range(len(myarray)): item = myarray[j] itemlist.append(item) </code></pre> <p>and:</p> <pre><code>itemlist = [] for item in myarray: itemlist.append(item) </code></pre> <p>I'm interested...
4
2009-08-06T02:31:46Z
1,236,808
<p>It is guaranteed for lists. I think the more relevant Python parallel to your C# example would be to iterate over the keys in a dictionary, which is NOT guaranteed to be in any order.</p> <pre><code># Always prints 0-9 in order a_list = [0,1,2,3,4,5,6,7,8,9] for x in a_list: print x # May or may not print 0-9...
7
2009-08-06T03:17:50Z
[ "python", "arrays", "list", "numpy" ]
Help with MySQL LOAD DATA INFILE
1,236,971
<p>I want to load a CSV file that looks like this:</p> <pre><code>Acct. No.,1-15 Days,16-30 Days,31-60 Days,61-90 Days,91-120 Days,Beyond 120 Days 2314134101,898.89,8372.16,5584.23,7744.41,9846.54,2896.25 2414134128,5457.61,7488.26,9594.02,6234.78,273.7,2356.13 2513918869,2059.59,7578.59,9395.51,7159.15,5827.48,3041.6...
3
2009-08-06T04:27:01Z
1,237,030
<p>You have basically 3 issues here. In reverse order</p> <ol> <li>Are you doing your Python inserts in individual statements? You probably want to surround them all with a begin transaction/commit. 20,000 commits could easily take hours. </li> <li>Your import statement defines 6 fields, but the CSV has 7 fields. Th...
2
2009-08-06T04:50:37Z
[ "python", "mysql", "load", "load-data-infile" ]
Help with MySQL LOAD DATA INFILE
1,236,971
<p>I want to load a CSV file that looks like this:</p> <pre><code>Acct. No.,1-15 Days,16-30 Days,31-60 Days,61-90 Days,91-120 Days,Beyond 120 Days 2314134101,898.89,8372.16,5584.23,7744.41,9846.54,2896.25 2414134128,5457.61,7488.26,9594.02,6234.78,273.7,2356.13 2513918869,2059.59,7578.59,9395.51,7159.15,5827.48,3041.6...
3
2009-08-06T04:27:01Z
1,326,393
<p>If you use REPLACE keyword in LOAD DATA, then number after "Deleted: " shows how many rows were actually replaced</p>
0
2009-08-25T06:33:03Z
[ "python", "mysql", "load", "load-data-infile" ]
math.sin incorrect result
1,237,085
<pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; math.sin(68) -0.897927680689 </code></pre> <p>But</p> <pre><code>sin(68) = 0.927 (3 decimal places) </code></pre> <p>Any ideas about why I am getting this result?<br> Thanks.</p>
4
2009-08-06T05:16:09Z
1,237,087
<pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; print math.sin.__doc__ sin(x) Return the sine of x (measured in radians). </code></pre> <p>math.sin expects its argument to be in radians, not degrees, so:</p> <pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; print math.sin(math.radians(68)) 0.927183854567 </code></pr...
22
2009-08-06T05:18:56Z
[ "python", "math", "trigonometry", "sin" ]
math.sin incorrect result
1,237,085
<pre><code>&gt;&gt;&gt; import math &gt;&gt;&gt; math.sin(68) -0.897927680689 </code></pre> <p>But</p> <pre><code>sin(68) = 0.927 (3 decimal places) </code></pre> <p>Any ideas about why I am getting this result?<br> Thanks.</p>
4
2009-08-06T05:16:09Z
33,218,237
<h3>Quote from <a href="http://stackoverflow.com/a/8691808/3787376" title="The answer to a Stack Overflow question">a Stack Overflow answer</a> for the question<br> <a href="http://stackoverflow.com/questions/8691800/unusual-math-with-incorrect-results" title="The Stack Overflow question">http://stackoverflow.com/quest...
1
2015-10-19T15:25:12Z
[ "python", "math", "trigonometry", "sin" ]
Cron job python Google App Engine
1,237,126
<p>I want to add a scheduled task to fetch a URL via cron job using google app engine. I am continuously getting a failure. I am just fetching www.google.com. Why is the url fetch failing? Am I missing something?</p>
0
2009-08-06T05:32:45Z
1,237,224
<p>"fetch" your OWN url (on appspot.com probably, but, who cares -- use a relative url anywau1-), not <code>google.com</code>, the homepage of the search engine -- what's that got to do w/your app anyway?!-)...</p>
0
2009-08-06T06:16:38Z
[ "python", "google-app-engine", "cron", "scheduled-tasks" ]
How do I set sys.excepthook to invoke pdb globally in python?
1,237,379
<p>From Python docs:</p> <blockquote> <p><code>sys.excepthook(type, value, traceback)</code></p> <p>This function prints out a given traceback and exception to <code>sys.stderr</code>.</p> <p>When an exception is raised and uncaught, the interpreter calls <code>sys.excepthook</code> with three arguments, t...
6
2009-08-06T07:15:46Z
1,237,407
<p>Here's what you need</p> <p><a href="http://ynniv.com/blog/2007/11/debugging-python.html">http://ynniv.com/blog/2007/11/debugging-python.html</a></p> <p>Three ways, the first is simple but crude (<a href="http://mail.python.org/pipermail/python-list/2001-April/713230.html">Thomas Heller</a>) - add the following to...
17
2009-08-06T07:27:07Z
[ "python", "debugging", "configuration", "pdb" ]
How do I set sys.excepthook to invoke pdb globally in python?
1,237,379
<p>From Python docs:</p> <blockquote> <p><code>sys.excepthook(type, value, traceback)</code></p> <p>This function prints out a given traceback and exception to <code>sys.stderr</code>.</p> <p>When an exception is raised and uncaught, the interpreter calls <code>sys.excepthook</code> with three arguments, t...
6
2009-08-06T07:15:46Z
1,237,422
<p>Try:</p> <pre><code>import pdb import sys def excepthook(type, value, traceback): pdb.post_mortem(traceback) excepthook.old = sys.excepthook sys.excepthook = excepthook def raise_exception(): raise_exception() raise_exception() </code></pre>
0
2009-08-06T07:31:43Z
[ "python", "debugging", "configuration", "pdb" ]
How do I set sys.excepthook to invoke pdb globally in python?
1,237,379
<p>From Python docs:</p> <blockquote> <p><code>sys.excepthook(type, value, traceback)</code></p> <p>This function prints out a given traceback and exception to <code>sys.stderr</code>.</p> <p>When an exception is raised and uncaught, the interpreter calls <code>sys.excepthook</code> with three arguments, t...
6
2009-08-06T07:15:46Z
1,336,425
<p>Another option is to use ipython, which I consider a must-have tool for any python developer anyway. Instead of running your script from the shell, run it from ipython with %run. When an exception occurs, you can type %debug to debug it. (There's also an option to automatically debug any exception that occurs, but I...
1
2009-08-26T17:48:08Z
[ "python", "debugging", "configuration", "pdb" ]
Choose the filename of an uploaded file with Django
1,237,602
<p>I'm uploading images (represented by a FileField) and I need to rename those files when they are uploaded.</p> <p>I want them to be formated like that:</p> <p><code>"%d-%d-%s.%s" % (width, height, md5hash, original_extension)</code></p> <p>I've read the documentation but I don't know if I need to write my own Fil...
6
2009-08-06T08:26:14Z
1,237,741
<p>You don't need to write your own FileStorage class or anything that complicated.</p> <p>The 'upload_to' parameter on File/ImageFields can take a function that returns the path/file to use.</p> <p>How to do this has already been answered <a href="http://stackoverflow.com/questions/1190697/django-filefield-with-uplo...
18
2009-08-06T08:59:38Z
[ "python", "django", "django-models" ]
Choose the filename of an uploaded file with Django
1,237,602
<p>I'm uploading images (represented by a FileField) and I need to rename those files when they are uploaded.</p> <p>I want them to be formated like that:</p> <p><code>"%d-%d-%s.%s" % (width, height, md5hash, original_extension)</code></p> <p>I've read the documentation but I don't know if I need to write my own Fil...
6
2009-08-06T08:26:14Z
1,237,762
<p>My initial instinct when reading this was that you need to overload the save method on the model, and use the os.rename() method, but that causes a lot of overhead, and is just generally a hassle from start to finish. If you simply want to rename the file, but don't want to make any physical changes to it (resizing,...
-1
2009-08-06T09:04:15Z
[ "python", "django", "django-models" ]
How to implement a state-space tree?
1,237,634
<p>I'm trying to solve a knapsack like problem from <a href="http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2007/Assignments/index.htm" rel="nofollow">MIT OCW</a>. Its problem set 5.</p> <p>I need use branch and bound algorithm to find the optimal states. So I need implement a state-spa...
2
2009-08-06T08:34:18Z
1,246,858
<p>I hope I understood correctly the problem, if not please direct me :)<br /> (sorry for the confusion arising from the two different meanings of "state")</p> <p>You can of course add the attribute in the node (it's part of the state!), since it's a very tiny amount of data. Mind that it is not mandatory to save it t...
2
2009-08-07T20:29:54Z
[ "python", "algorithm" ]
Python: binary/hex string conversion?
1,238,002
<p>I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex.</p> <p>The string is as below:</p> <pre><code>&lt;81&gt;^Q&lt;81&gt;"^Q^@^[)^G ^Q^A^S^A^V^@&lt;83&gt;^Cd&lt;80&gt;&lt;99&gt;}^@N^@^@^A^@^@^@^@^@^@^@j </code></pre> <p>How do I go about convertin...
7
2009-08-06T10:03:27Z
1,238,129
<p>You can use ord and hex like this :</p> <pre><code>&gt;&gt;&gt; s = 'some string' &gt;&gt;&gt; hex_chars = map(hex,map(ord,s)) &gt;&gt;&gt; print hex_chars ['0x73', '0x6f', '0x6d', '0x65', '0x20', '0x73', '0x74', '0x72', '0x69', '0x6e', '0x67'] &gt;&gt;&gt; hex_string = "".join(c[2:4] for c in hex_chars) &gt;&gt;&g...
20
2009-08-06T10:36:46Z
[ "python", "binary", "hex" ]
Python: binary/hex string conversion?
1,238,002
<p>I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex.</p> <p>The string is as below:</p> <pre><code>&lt;81&gt;^Q&lt;81&gt;"^Q^@^[)^G ^Q^A^S^A^V^@&lt;83&gt;^Cd&lt;80&gt;&lt;99&gt;}^@N^@^@^A^@^@^@^@^@^@^@j </code></pre> <p>How do I go about convertin...
7
2009-08-06T10:03:27Z
7,565,322
<p>Faster solution see:</p> <pre><code>from timeit import Timer import os import binascii def testSpeed(statement, setup = 'pass'): print '%s' % statement print '%s' % Timer(statement, setup).timeit() setup = """ import os value = os.urandom(32) """ # winner statement = """ import binascii binascii.hexlify(v...
1
2011-09-27T07:00:29Z
[ "python", "binary", "hex" ]
Python: binary/hex string conversion?
1,238,002
<p>I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex.</p> <p>The string is as below:</p> <pre><code>&lt;81&gt;^Q&lt;81&gt;"^Q^@^[)^G ^Q^A^S^A^V^@&lt;83&gt;^Cd&lt;80&gt;&lt;99&gt;}^@N^@^@^A^@^@^@^@^@^@^@j </code></pre> <p>How do I go about convertin...
7
2009-08-06T10:03:27Z
7,813,649
<pre><code>&gt;&gt;&gt; import binascii &gt;&gt;&gt; s = '2F' &gt;&gt;&gt; hex_str = binascii.b2a_hex(s) &gt;&gt;&gt; hex_str &gt;&gt;&gt; '3246' </code></pre> <p>OR</p> <pre><code>&gt;&gt;&gt;import binascii &gt;&gt;&gt; hex_str = binascii.hexlify(s) &gt;&gt;&gt; hex_str &gt;&gt;&gt; '3246' &gt;&gt;&gt; </code...
5
2011-10-18T20:58:57Z
[ "python", "binary", "hex" ]
What does % do to strings in Python?
1,238,306
<p>I have failed to find documentation for the operator % as it is used on strings in Python. Does someone know where that documentation is?</p>
11
2009-08-06T11:29:00Z
1,238,316
<p>It's the string formatting operator. Read up on <a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">string formatting in Python</a>.</p> <pre><code>format % values </code></pre> <p>Creates a string where <code>format</code> specifies a format and <code>values</code> are the values t...
20
2009-08-06T11:30:46Z
[ "python", "string", "documentation", "operators" ]
What does % do to strings in Python?
1,238,306
<p>I have failed to find documentation for the operator % as it is used on strings in Python. Does someone know where that documentation is?</p>
11
2009-08-06T11:29:00Z
1,238,321
<p>The '%' operator is used for string interpolation. Since Python 2.6 the String method "format" is used insted. For details see <a href="http://www.python.org/dev/peps/pep-3101/" rel="nofollow">http://www.python.org/dev/peps/pep-3101/</a></p>
6
2009-08-06T11:31:57Z
[ "python", "string", "documentation", "operators" ]
What does % do to strings in Python?
1,238,306
<p>I have failed to find documentation for the operator % as it is used on strings in Python. Does someone know where that documentation is?</p>
11
2009-08-06T11:29:00Z
1,238,353
<p>It applies <a href="http://en.wikipedia.org/wiki/Printf">printf-like formatting</a> to a string, so that you can substitute certain parts of a string with values of variables. Example</p> <pre><code># assuming numFiles is an int variable print "Found %d files" % (numFiles, ) </code></pre> <p>See the link provided ...
6
2009-08-06T11:38:41Z
[ "python", "string", "documentation", "operators" ]
What does % do to strings in Python?
1,238,306
<p>I have failed to find documentation for the operator % as it is used on strings in Python. Does someone know where that documentation is?</p>
11
2009-08-06T11:29:00Z
1,238,611
<p>Note that starting from Python 2.6, it's recommended to use the new <a href="http://docs.python.org/library/stdtypes.html#str.format" rel="nofollow"><code>str.format()</code></a> method:</p> <pre><code>&gt;&gt;&gt; "The sum of 1 + 2 is {0}".format(1+2) 'The sum of 1 + 2 is 3' </code></pre> <p>If you are using 2.6,...
2
2009-08-06T12:40:28Z
[ "python", "string", "documentation", "operators" ]
Python Multiprocessing exit error
1,238,349
<p>Hey everyone I am seeing this when I press Ctrl-C to exit my app</p> <pre><code>Error in atexit._run_exitfuncs: Traceback (most recent call last): File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs func(*targs, **kargs) File "/usr/lib/python2.6/multiprocessing/util.py", line 269, in _exit_funct...
6
2009-08-06T11:37:57Z
1,244,229
<p>This is related to interactions OS system calls, signals and how it's handled in the multiprocessing module. I'm not really sure if it's a bug or a feature, but it's in somewhat tricky territory as it's where python meets the os.</p> <p>The problem is that multiprocessing is blocking on waitpid until the child it's...
5
2009-08-07T11:20:21Z
[ "python", "exception", "multiprocessing" ]
Python Multiprocessing exit error
1,238,349
<p>Hey everyone I am seeing this when I press Ctrl-C to exit my app</p> <pre><code>Error in atexit._run_exitfuncs: Traceback (most recent call last): File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs func(*targs, **kargs) File "/usr/lib/python2.6/multiprocessing/util.py", line 269, in _exit_funct...
6
2009-08-06T11:37:57Z
1,435,075
<p>I was seeing this, but it went away when I overrode signal handlers with my own. Use reactor.run(installSignalHandlers=False) and define your own functions for SIGINT, SIGTERM, etc.</p>
0
2009-09-16T19:46:34Z
[ "python", "exception", "multiprocessing" ]
Is it necessary or useful to inherit from python's object in Python 3.x?
1,238,606
<p>In older python version when you create a class in python, it can inherit from <em>object</em> which is as far I understand a special built-in python element that allow your object to be a new-style object.</p> <p>What about newer version (> 3.0 and 2.6)? I googled about the class object but I get so much result (f...
32
2009-08-06T12:39:53Z
1,238,632
<p>You don't need to inherit from <code>object</code> to have new style in python 3. All classes are new-style.</p>
27
2009-08-06T12:43:53Z
[ "python", "python-3.x" ]
surprising time shift for python call
1,238,648
<p>I'm using the following code in Python 2.5.1 to generate a UTC timestamp from a string representation of a date:</p> <pre><code>time.mktime(time.strptime("2009-06-16", "%Y-%m-%d")) </code></pre> <p>The general result is: 1245103200 (16.6.2009 0:00 UTC or 15.6.09 22:00:00, if you're in my time zone). </p> <p>But n...
0
2009-08-06T12:45:36Z
1,238,813
<p>What version of Python are you running? There are two bug descriptions here which sound a lot like what you are experiencing:</p> <ul> <li><a href="http://bugs.python.org/issue5582" rel="nofollow">http://bugs.python.org/issue5582</a></li> <li><a href="http://mercurial.selenic.com/bts/issue1364" rel="nofollow">http:...
0
2009-08-06T13:16:32Z
[ "python", "windows-xp", "timestamp" ]
surprising time shift for python call
1,238,648
<p>I'm using the following code in Python 2.5.1 to generate a UTC timestamp from a string representation of a date:</p> <pre><code>time.mktime(time.strptime("2009-06-16", "%Y-%m-%d")) </code></pre> <p>The general result is: 1245103200 (16.6.2009 0:00 UTC or 15.6.09 22:00:00, if you're in my time zone). </p> <p>But n...
0
2009-08-06T12:45:36Z
1,259,669
<p>Found the answer myself:</p> <p>When executing the following on the python command line interface, I get the result:</p> <blockquote> <p>time.strptime("2009-06-16", "%Y-%m-%d")<br /> time.struct_time(tm_year=2009, tm_mon=6, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=167, tm_isdst=-1) </p> <...
1
2009-08-11T11:00:44Z
[ "python", "windows-xp", "timestamp" ]
Does 'a+' mode allow random access to files, on all systems?
1,238,922
<p>According to the <a href="http://docs.python.org/library/functions.html?highlight=open#open" rel="nofollow">documentation</a> of open function 'a' means appending, which on some Unix systems means that all writes append to the end of the file regardless of the current seek position.</p> <p>Will 'a+' allow random wr...
2
2009-08-06T13:33:33Z
1,238,996
<p>On my linux system with Python 2.5.2 writes to a file opened with 'a+' appear to always append to the end, regardless of the current seek position.</p> <p>Here is an example:</p> <pre><code>import os if __name__ == "__main__": f = open("test", "w") f.write("Hello") f.close() f = open("test", "a+...
2
2009-08-06T13:48:45Z
[ "python" ]
Can all language constructs be first-class in languages with offside-rules?
1,238,975
<p>In LISP-like languages all language constructs are first-class citizens.</p> <p>Consider the following example in Dylan:</p> <pre><code>let x = if (c) foo(); else bar(); end; </code></pre> <p>and in LISP:</p> <pre><code>(setf x (if c (foo) (bar))) </code></pre> <p>In Python y...
2
2009-08-06T13:44:22Z
1,239,015
<p>I don't see the relation with first-classness here - you're not passing the <code>if</code> <strong>statement</strong> to the function, but the object it returns, which is as fully first class in python as in lisp. However as far as having a statement/expression dichotomy, clearly it is possible: Haskell for instan...
4
2009-08-06T13:52:42Z
[ "python", "lisp", "expression", "indentation", "if-statement" ]
Can all language constructs be first-class in languages with offside-rules?
1,238,975
<p>In LISP-like languages all language constructs are first-class citizens.</p> <p>Consider the following example in Dylan:</p> <pre><code>let x = if (c) foo(); else bar(); end; </code></pre> <p>and in LISP:</p> <pre><code>(setf x (if c (foo) (bar))) </code></pre> <p>In Python y...
2
2009-08-06T13:44:22Z
1,239,032
<p>Python has the following syntax that performs the same thing:</p> <pre><code>x = foo() if c else bar() </code></pre>
9
2009-08-06T13:54:55Z
[ "python", "lisp", "expression", "indentation", "if-statement" ]
Changing the default indentation of etree.tostring in lxml
1,238,988
<p>I have an XML document which I'm pretty-printing using <code>lxml.etree.tostring</code></p> <pre><code>print etree.tostring(doc, pretty_print=True) </code></pre> <p>The default level of indentation is 2 spaces, and I'd like to change this to 4 spaces. There isn't any argument for this in the <code>tostring</code>...
5
2009-08-06T13:47:37Z
1,239,193
<p>As said in <a href="http://codespeak.net/pipermail/lxml-dev/2009-February/004350.html" rel="nofollow">this thread</a>, there is no real way to change the indent of the <code>lxml.etree.tostring</code> pretty-print.</p> <p>But, you can:</p> <ul> <li>add a XSLT transform to change the indent</li> <li>add whitespace ...
3
2009-08-06T14:20:28Z
[ "python", "lxml", "pretty-print" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
1,239,059
<p>It's not in the language core, but a very mature library that does what you want is <a href="http://twistedmatrix.com/">Twisted</a>. It introduces the Deferred object, which you can attach callbacks or error handlers ("errbacks") to. A Deferred is basically a "promise" that a function will have a result eventually.<...
25
2009-08-06T13:59:22Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
1,239,108
<p>What about something like</p> <pre><code>import threading thr = threading.Thread(target=foo, args=(), kwargs={}) thr.start() # will run "foo" .... thr.is_alive() # will return whether foo is running currently .... thr.join() # will wait till "foo" is done </code></pre> <p>See the docs at <a href="https://docs.pyt...
108
2009-08-06T14:05:52Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
1,239,139
<p>Is there any reason not to use threads? You can use the <code>threading</code> class. Instead of <code>finished()</code> function use the <code>isAlive()</code>. The <code>result()</code> function could <code>join()</code> the thread and retrieve the result. And, if you can, override the <code>run()</code> and <code...
2
2009-08-06T14:11:16Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
1,239,252
<p>You can use the <a href="http://docs.python.org/library/multiprocessing.html#module-multiprocessing">multiprocessing module</a> added in Python 2.6. You can use pools of processes and then get results asynchronously with: </p> <pre><code>apply_async(func[, args[, kwds[, callback]]]) </code></pre> <p>E.g.:</p> <pr...
82
2009-08-06T14:28:14Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
5,239,576
<p>My solution is:</p> <pre><code>import threading class TimeoutError(RuntimeError): pass class AsyncCall(object): def __init__(self, fnc, callback = None): self.Callable = fnc self.Callback = callback def __call__(self, *args, **kwargs): self.Thread = threading.Thread(target = s...
5
2011-03-08T23:22:14Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
5,485,574
<p>You can implement a decorator to make your functions asynchronous, though that's a bit tricky. The <code>multiprocessing</code> module is full of little quirks and seemingly arbitrary restrictions – all the more reason to encapsulate it behind a friendly interface, though.</p> <pre><code>from inspect import getmo...
16
2011-03-30T11:16:36Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
12,780,318
<p>Just</p> <pre><code>import threading, time def f(): print "f started" time.sleep(3) print "f finished" threading.Thread(target=f).start() </code></pre>
7
2012-10-08T10:57:53Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
21,007,695
<p>You could use eventlet. It lets you write what appears to be synchronous code, but have it operate asynchronously over the network.</p> <p>Here's an example of a super minimal crawler:</p> <pre><code>urls = ["http://www.google.com/intl/en_ALL/images/logo.gif", "https://wiki.secondlife.com/w/images/secondlife....
6
2014-01-08T22:12:46Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
22,138,142
<p>Something like this works for me, you can then call the function, and it will dispatch itself onto a new thread.</p> <pre><code>from thread import start_new_thread def dowork(asynchronous=True): if asynchronous: args = (False) start_new_thread(dowork,args) #Call itself on a new thread. else...
2
2014-03-03T03:26:23Z
[ "python", "asynchronous" ]
Asynchronous method call in Python?
1,239,035
<p>I was wondering if there's any library for asynchronous method calls in <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>. It would be great if you could do something like</p> <pre><code>@async def longComputation(): &lt;code&gt; token = longComputation() token.registerCa...
92
2009-08-06T13:55:35Z
36,272,211
<p>As of Python 3.5, you can use enhanced generators for async functions.</p> <pre><code>import asyncio import datetime </code></pre> <p><strong>Enhanced generator syntax:</strong></p> <pre><code>@asyncio.coroutine def display_date(loop): end_time = loop.time() + 5.0 while True: print(datetime.dateti...
12
2016-03-28T22:21:40Z
[ "python", "asynchronous" ]
How to co host django app with php5 on apache2 with mod_python?
1,239,246
<p>I have django+python+apache2+mod_python installed hosted and working on ubuntu server/ linode VPS. php5 is installed and configured. We don't have a domain name as in example.com. Just IP address. So my apache .conf file looks like this</p> <p> ServerAdmin webmaster@localhost DocumentRoot /var/www</...
0
2009-08-06T14:27:04Z
1,242,332
<p>I need to test it, but this should get your Django project running at /mysite/:</p> <pre><code>&lt;VirtualHost *:80&gt; DocumentRoot /var/www/vtigercrm/ ErrorLog /var/log/apache2/vtiger.error_log CustomLog /var/log/apache2/vtiger.access_log combined &lt;Directory /var/www/vtigercrm&gt; Optio...
1
2009-08-07T00:51:06Z
[ "php", "python", "django", "apache", "mod-python" ]
Limit choices to
1,239,433
<p>I have a model named Project which has a m2m field users. I have a task model with a FK project. And it has a field assigned_to. How can i limit the choices of assigned_to to only the users of the current project?</p>
3
2009-08-06T14:53:42Z
1,239,568
<p>You need to create a <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin" rel="nofollow">custom form for the admin</a>.</p> <p>Your form should contain a <a href="http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField" rel="nofollow">M...
0
2009-08-06T15:12:04Z
[ "python", "django-models" ]
Limit choices to
1,239,433
<p>I have a model named Project which has a m2m field users. I have a task model with a FK project. And it has a field assigned_to. How can i limit the choices of assigned_to to only the users of the current project?</p>
3
2009-08-06T14:53:42Z
1,371,169
<p>You could do this another way, using this nifty form factory trick.</p> <pre><code>def make_task_form(project): class _TaskForm(forms.Form): assigned_to = forms.ModelChoiceField( queryset=User.objects.filter(user__project=project)) class Meta: model = Task return _...
1
2009-09-03T02:37:19Z
[ "python", "django-models" ]
comparing and sorting array
1,239,509
<p>From two unequal arrays, i need to compare &amp; delete based on the last value of an array.</p> <p>Example:</p> <p><code>m[0]</code> and <code>n[0]</code> are read form a text file &amp; saved as a array, <code>[0]</code> - their column number in text file.</p> <pre><code>m[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2....
1
2009-08-06T15:04:54Z
1,239,574
<p>I haven't been able to test this but here you go...</p> <pre><code>p = [] for item in m[0]: if (item &lt; n[0][-1]): p.append(item) else: break </code></pre>
0
2009-08-06T15:12:48Z
[ "python", "list" ]
comparing and sorting array
1,239,509
<p>From two unequal arrays, i need to compare &amp; delete based on the last value of an array.</p> <p>Example:</p> <p><code>m[0]</code> and <code>n[0]</code> are read form a text file &amp; saved as a array, <code>[0]</code> - their column number in text file.</p> <pre><code>m[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2....
1
2009-08-06T15:04:54Z
1,239,590
<p>Some details are a little unclear, but this should do what you want:</p> <pre><code>p[0] = [x for x in m[0] if x &lt; n[0][-1]] </code></pre>
6
2009-08-06T15:14:10Z
[ "python", "list" ]
comparing and sorting array
1,239,509
<p>From two unequal arrays, i need to compare &amp; delete based on the last value of an array.</p> <p>Example:</p> <p><code>m[0]</code> and <code>n[0]</code> are read form a text file &amp; saved as a array, <code>[0]</code> - their column number in text file.</p> <pre><code>m[0] = [0.00, 1.15, 1.24, 1.35, 1.54, 2....
1
2009-08-06T15:04:54Z
1,239,697
<p>if both lists are ordered, you can do:</p> <pre><code>import bisect m[0][:bisect.bisect(m[0],n[0][-1])] </code></pre>
1
2009-08-06T15:30:34Z
[ "python", "list" ]
Python SOAP document handling
1,239,538
<p>I've been trying to use <a href="https://fedorahosted.org/suds/wiki" rel="nofollow">suds</a> for Python to call a SOAP WSDL. I just need to call the service programmatically and write the output XML document. However suds automatically parses this data into it's own pythonic data format. I've been looking through <a...
1
2009-08-06T15:08:22Z
1,239,558
<p>You could take a look at a library such as <a href="http://wiki.github.com/jkp/soaplib" rel="nofollow">soaplib</a>: its a really nice way to consume (and serve) SOAP webservices in Python. The latest version has some code to dynamically generate Python bindings either dynamically (at runtime) or statically (run a s...
0
2009-08-06T15:10:48Z
[ "python", "xml", "soap", "wsdl", "suds" ]
Python SOAP document handling
1,239,538
<p>I've been trying to use <a href="https://fedorahosted.org/suds/wiki" rel="nofollow">suds</a> for Python to call a SOAP WSDL. I just need to call the service programmatically and write the output XML document. However suds automatically parses this data into it's own pythonic data format. I've been looking through <a...
1
2009-08-06T15:08:22Z
1,240,791
<p>At this early stage in suds development, the easiest way to get to the raw XML content is not what one would expect.</p> <p>The examples on the site show us with something like this:</p> <pre><code>client = Client(url) result = client.service.Invoke(subm) </code></pre> <p>however, the result is a pre-parsed objec...
3
2009-08-06T19:13:57Z
[ "python", "xml", "soap", "wsdl", "suds" ]
Qt HTTP authentication with QNetworkAccessManager
1,239,669
<p>I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the </p> <pre><code>authenticationRequired (QNetworkReply*, QAuthenticator*) </code></pre> <p>signal when (obviously),authentication is required. When I fill in ...
2
2009-08-06T15:26:22Z
1,239,722
<p>Yeh, it's odd. What I've done previously is check if I've already authenticated with those details and if I have then call <code>QNetworkReply.abort()</code></p>
2
2009-08-06T15:33:17Z
[ "python", "qt", "networking", "qt4", "pyqt" ]
Qt HTTP authentication with QNetworkAccessManager
1,239,669
<p>I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the </p> <pre><code>authenticationRequired (QNetworkReply*, QAuthenticator*) </code></pre> <p>signal when (obviously),authentication is required. When I fill in ...
2
2009-08-06T15:26:22Z
1,239,750
<p>What about trying:</p> <pre><code>QNetworkReply::abort() </code></pre> <p>or</p> <pre><code>QNetworkReply::close() </code></pre> <p>?</p>
1
2009-08-06T15:38:27Z
[ "python", "qt", "networking", "qt4", "pyqt" ]
Qt HTTP authentication with QNetworkAccessManager
1,239,669
<p>I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the </p> <pre><code>authenticationRequired (QNetworkReply*, QAuthenticator*) </code></pre> <p>signal when (obviously),authentication is required. When I fill in ...
2
2009-08-06T15:26:22Z
27,430,047
<p>Calling <code>QNetworkReply::abort()</code> will cause the request to fail with the error 'Operation Aborted' instead of the original 401 error. The correct thing to do now seems to be not to call <code>QAuthenticator::setUser()</code> or <code>QAuthenticator::setPassword()</code> inside your <code>authenticationReq...
0
2014-12-11T18:37:01Z
[ "python", "qt", "networking", "qt4", "pyqt" ]
Qt HTTP authentication with QNetworkAccessManager
1,239,669
<p>I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the </p> <pre><code>authenticationRequired (QNetworkReply*, QAuthenticator*) </code></pre> <p>signal when (obviously),authentication is required. When I fill in ...
2
2009-08-06T15:26:22Z
32,416,102
<p>If you return from a slot connected to &amp;QNetworkAccessManager::proxyAuthenticationRequired without calling any QAuthenticator method, the request will be aborted with real HTTP code (e.g. 401 or 407).</p> <p>For instance you can call QAuthenticator::setUser and setPassword when the user clics OK and call nothin...
0
2015-09-05T17:57:41Z
[ "python", "qt", "networking", "qt4", "pyqt" ]
Pythonic Comparison Functions
1,239,751
<p>For the sake of simplicity, let's say I have a Person class in Python. This class has fields for firstname, lastname, and dob.</p> <pre><code>class Person: def __init__(self, firstname, lastname, dob): self.firstname = firstname; self.lastname = lastname; self.dob = dob; </code></pre> <p>In some sit...
1
2009-08-06T15:39:06Z
1,239,780
<p>Here's one way (maybe not the fastest):</p> <pre><code>def compare_people_flexibly(p1, p2, attrs): """Compare `p1` and `p2` based on the attributes in `attrs`.""" v1 = [getattr(p1, a) for a in attrs] v2 = [getattr(p2, a) for a in attrs] return cmp(v1, v2) def compare_people_firstname(p1, p2): r...
4
2009-08-06T15:45:13Z
[ "comparison", "metaprogramming", "python" ]
Pythonic Comparison Functions
1,239,751
<p>For the sake of simplicity, let's say I have a Person class in Python. This class has fields for firstname, lastname, and dob.</p> <pre><code>class Person: def __init__(self, firstname, lastname, dob): self.firstname = firstname; self.lastname = lastname; self.dob = dob; </code></pre> <p>In some sit...
1
2009-08-06T15:39:06Z
1,239,782
<p>If you really want a comparison function, you can use</p> <pre><code>def comparepeople(p1, p2): o1 = p1.lastname, p1.firstname, p1.dob o2 = p2.lastname, p2.firstname, p2.dob return cmp(o1,o2) </code></pre> <p>This relies on tuple comparison. If you want to sort a list, you shouldn't write a comparison ...
10
2009-08-06T15:45:27Z
[ "comparison", "metaprogramming", "python" ]
Pythonic Comparison Functions
1,239,751
<p>For the sake of simplicity, let's say I have a Person class in Python. This class has fields for firstname, lastname, and dob.</p> <pre><code>class Person: def __init__(self, firstname, lastname, dob): self.firstname = firstname; self.lastname = lastname; self.dob = dob; </code></pre> <p>In some sit...
1
2009-08-06T15:39:06Z
1,239,861
<p>Can you not use the comparison methods on the class, see <a href="http://docs.python.org/reference/datamodel.html" rel="nofollow">__cmp__</a> and the other rich comparison methods...</p>
0
2009-08-06T15:55:57Z
[ "comparison", "metaprogramming", "python" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,236
<p>No, unless you want to confuse every other programmer that looks at your code after you write it. <code>self</code> is not a keyword because it is an identifier. It <em>could</em> have been a keyword and the fact that it isn't one was a design decision.</p>
9
2009-08-06T17:12:11Z
[ "python", "naming-conventions" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,239
<p>I think that the main reason <code>self</code> is used by convention rather than being a Python keyword is because it's simpler to have all methods/functions take arguments the same way rather than having to put together different argument forms for functions, class methods, instance methods, etc.</p> <p>Note that ...
1
2009-08-06T17:13:02Z
[ "python", "naming-conventions" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,265
<p>Because it is a convention, not language syntax. There is a <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">Python style guide</a> that people who program in Python follow. This way libraries have a familiar look and feel. Python places a lot of emphasis on readability, and consistency is an import...
1
2009-08-06T17:18:35Z
[ "python", "naming-conventions" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,277
<p>The only case of this I've seen is when you define a function outside of a class definition, and then assign it to the class, e.g.:</p> <pre><code>class Foo(object): def bar(self): # Do something with 'self' def baz(inst): return inst.bar() Foo.baz = baz </code></pre> <p>In this case, <code>self<...
4
2009-08-06T17:20:54Z
[ "python", "naming-conventions" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,307
<p>As a side observation, note that Pilgrim is committing a common misuse of terms here: a <em>class method</em> is quite a different thing from an <em>instance method</em>, which is what he's talking about here. As <a href="http://en.wikipedia.org/wiki/Method%5F%28computer%5Fscience%29" rel="nofollow">wikipedia</a> pu...
5
2009-08-06T17:27:06Z
[ "python", "naming-conventions" ]
is it ever useful to define a class method with a reference to self not called 'self' in Python?
1,240,229
<p>I'm teaching myself Python and I see the following in <a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> <a href="http://www.diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">section 5.3</a>:</p> <blockquote> <p>By convention, the first argument of any Py...
3
2009-08-06T17:10:36Z
1,240,371
<p>I once had some code like (and I apologize for lack of creativity in the example):</p> <pre><code>class Animal: def __init__(self, volume=1): self.volume = volume self.description = "Animal" def Sound(self): pass def GetADog(self, newvolume): class Dog(Animal): ...
2
2009-08-06T17:40:03Z
[ "python", "naming-conventions" ]
GTK: Modify bg color of a CheckButton
1,240,764
<p>I tried the following, yet the button still has a white background:</p> <pre><code> self.button = gtk.CheckButton() self.button.modify_fg(gtk.STATE_NORMAL, gtk.gdk.Color(65535,0,0)) self.button.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(65535,0,0)) self.button.modify_fg(gtk.STATE_ACTIVE, gtk.gdk.Color...
1
2009-08-06T19:07:19Z
1,320,563
<p>So you want the part with the check mark on it to be a different color? Then use this <code>button.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("red"))</code>.</p> <p><img src="http://www.ubuntu-pics.de/bild/22793/screenshot%5F009%5FMCxjbu.png" alt="alt text" /></p>
3
2009-08-24T05:04:35Z
[ "python", "events", "gtk", "colors", "pygtk" ]
GTK: create a colored regular button
1,241,020
<p>How do I do it? A lot of sites say I can just call .modify_bg() on the button, but that doesn't do anything. I'm able to add an EventBox to the button, and add a label to that, and then change its colors, but it looks horrendous - there is a ton of gray space between the edge of the button that doesn't change. I jus...
5
2009-08-06T19:55:17Z
1,241,609
<p>Here's a little example:</p> <pre><code>import gtk win = gtk.Window() win.connect("destroy", gtk.main_quit) btn = gtk.Button("test") #make a gdk.color for red map = btn.get_colormap() color = map.alloc_color("red") #copy the current style and replace the background style = btn.get_style().copy() style.bg[gtk.S...
14
2009-08-06T21:30:14Z
[ "python", "button", "gtk", "colors", "pygtk" ]
How to filter a dictionary by value?
1,241,029
<p>Newbie question here, so please bear with me.</p> <p>Let's say I have a dictionary looking like this:</p> <pre><code>a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324"...
4
2009-08-06T19:56:16Z
1,241,064
<p>Iterating over a dictionary is no different from iterating over a list in python:</p> <pre><code>for key in dic: print("dic[%s] = %s" % (key, dic[key])) </code></pre> <p>This will print all of the keys and values of your dictionary.</p>
1
2009-08-06T20:01:37Z
[ "python", "dictionary" ]
How to filter a dictionary by value?
1,241,029
<p>Newbie question here, so please bear with me.</p> <p>Let's say I have a dictionary looking like this:</p> <pre><code>a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324"...
4
2009-08-06T19:56:16Z
1,241,307
<p>I assume that your unique id will be the key.<br /> Probably not very beautiful, but returns a dict with your unique values: </p> <pre><code>&gt;&gt;&gt; dict_ = {'1': ['first/dir', 'hello.txt'], '3': ['first/dir', 'foo.txt'], '2': ['second/dir', 'foo.txt'], '4': ['second/dir', 'foo.txt']} &gt;&gt;&gt; dict((v...
1
2009-08-06T20:39:14Z
[ "python", "dictionary" ]
How to filter a dictionary by value?
1,241,029
<p>Newbie question here, so please bear with me.</p> <p>Let's say I have a dictionary looking like this:</p> <pre><code>a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324"...
4
2009-08-06T19:56:16Z
1,241,354
<p>The code below will result in two variables, <code>matches</code> and <code>remainders</code>. <code>matches</code> is an array of dictionaries, in which matching items from the original dictionary will have a corresponding element. <code>remainder</code> will contain, as in your example, a dictionary containing a...
9
2009-08-06T20:49:46Z
[ "python", "dictionary" ]
How to filter a dictionary by value?
1,241,029
<p>Newbie question here, so please bear with me.</p> <p>Let's say I have a dictionary looking like this:</p> <pre><code>a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324"...
4
2009-08-06T19:56:16Z
1,241,369
<p>if you know what value you want to filter out:</p> <pre><code>known_tuple = 'first/dir','hello.txt' b = {k:v for k, v in a.items() if v == known_tuple} </code></pre> <p>then <code>a</code> would become:</p> <pre><code>a = dict(a.items() - b.items()) </code></pre> <p>this is py3k notation, but I'm sure something ...
0
2009-08-06T20:52:47Z
[ "python", "dictionary" ]
How to filter a dictionary by value?
1,241,029
<p>Newbie question here, so please bear with me.</p> <p>Let's say I have a dictionary looking like this:</p> <pre><code>a = {"2323232838": ("first/dir", "hello.txt"), "2323221383": ("second/dir", "foo.txt"), "3434221": ("first/dir", "hello.txt"), "32232334": ("first/dir", "hello.txt"), "324234324"...
4
2009-08-06T19:56:16Z
1,241,803
<p>What you're asking for is called an "Inverted Index" -- the distinct items are recorded just once with a list of keys.</p> <pre><code>&gt;&gt;&gt; from collections import defaultdict &gt;&gt;&gt; a = {"2323232838": ("first/dir", "hello.txt"), ... "2323221383": ("second/dir", "foo.txt"), ... "3434221": ("f...
4
2009-08-06T22:08:42Z
[ "python", "dictionary" ]
Copy constructor in python
1,241,148
<p>Is there a copy constructor in python ? If not what would I do to achieve something similar ?</p> <p>The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.</p>
50
2009-08-06T20:16:48Z
1,241,170
<p>I think you want the <a href="http://docs.python.org/library/copy.html">copy module</a></p> <pre><code>import copy x = copy.copy(y) # make a shallow copy of y x = copy.deepcopy(y) # make a deep copy of y </code></pre> <p>you can control copying in much the same way as you control <a href="http://docs.py...
40
2009-08-06T20:21:34Z
[ "python", "constructor", "copy" ]
Copy constructor in python
1,241,148
<p>Is there a copy constructor in python ? If not what would I do to achieve something similar ?</p> <p>The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.</p>
50
2009-08-06T20:16:48Z
1,241,241
<p>For your situation, I would suggest writing a class method (or it could be a static method or a separate function) that takes as an argument an instance of the library's class and returns an instance of your class with all applicable attributes copied over.</p>
10
2009-08-06T20:31:14Z
[ "python", "constructor", "copy" ]
Copy constructor in python
1,241,148
<p>Is there a copy constructor in python ? If not what would I do to achieve something similar ?</p> <p>The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.</p>
50
2009-08-06T20:16:48Z
16,046,733
<p>In python the copy constructor can be defined using default arguments. Lets say you want the normal constructor to run the function non_copy_constructor(self) and the copy constructor should run copy_constructor(self,orig). Then you can do the following:</p> <pre><code>class Foo: def __init__(self,orig=None):...
11
2013-04-16T20:43:33Z
[ "python", "constructor", "copy" ]
Copy constructor in python
1,241,148
<p>Is there a copy constructor in python ? If not what would I do to achieve something similar ?</p> <p>The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.</p>
50
2009-08-06T20:16:48Z
27,441,776
<p>A simple example of my usual implementation of a copy constructor:</p> <pre><code>import copy class Foo: def __init__(self, data): self._data = data @classmethod def from_foo(cls, class_instance): data = copy.deepcopy(class_instance._data) # if deepcopy is necessary return cls(data) </code></pr...
4
2014-12-12T10:37:17Z
[ "python", "constructor", "copy" ]
Copy constructor in python
1,241,148
<p>Is there a copy constructor in python ? If not what would I do to achieve something similar ?</p> <p>The situation is that I am using a library and I have extended one of the classes there with extra functionality and I want to be able to convert the objects I get from the library to instances of my own class.</p>
50
2009-08-06T20:16:48Z
35,440,728
<p>Building on @Godsmith's <a href="http://stackoverflow.com/a/27441776/623735">train of thought</a> and addressing @Zitrax's need (I think) to do the data copy for all attributes within the constructor:</p> <pre><code>class ConfusionMatrix(pd.DataFrame): def __init__(self, df, *args, **kwargs): try: ...
1
2016-02-16T18:50:48Z
[ "python", "constructor", "copy" ]
ValueError: invalid literal for int() with base 10
1,241,395
<p>I received the following error when trying to retrieve data using <a href="http://en.wikipedia.org/wiki/Google%5FApp%5FEngine" rel="nofollow">Google App Engine</a> from a single entry to a single page e.g. foobar.com/page/1 would show all the data from id 1:</p> <pre><code>ValueError: invalid literal for int() with...
3
2009-08-06T20:56:44Z
1,241,471
<p>Change <code>self.request.get("id")</code> to simply <code>id</code>, which is already being passed to your <code>get</code> handler.</p> <p>The code, as you have it, would only work for URLs like <code>/browse/1/?id=1</code></p>
2
2009-08-06T21:08:02Z
[ "python", "google-app-engine" ]