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 |
|---|---|---|---|---|---|---|---|---|---|
how to reference python package when filename contains a period | 1,828,127 | <p>I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:</p>
<pre><code>from "models.admin" import *
</code></pre>
<p>however, I get a syntax error for having double quotes. But if I just do </p>
<pre><code>from models.admin import *
</code></pre>
<p>then I get ... | 15 | 2009-12-01T18:53:48Z | 1,828,149 | <p>The file is called <code>models/admin.py</code>. (<a href="http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/models/admin.py?rev=945" rel="nofollow">Source</a>)</p>
<p>That is, it should be called <code>admin.py</code> in a directory called <code>models</code>.</p>
<p>Then you can import usin... | 3 | 2009-12-01T18:58:12Z | [
"python",
"import",
"package"
] |
how to reference python package when filename contains a period | 1,828,127 | <p>I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:</p>
<pre><code>from "models.admin" import *
</code></pre>
<p>however, I get a syntax error for having double quotes. But if I just do </p>
<pre><code>from models.admin import *
</code></pre>
<p>then I get ... | 15 | 2009-12-01T18:53:48Z | 1,828,174 | <p>No, you can't import a python file as a module if its name contains a period (or a question mark, or exclamation mark, etc). A python module's name (not including the .py) must be a valid python name (ie can be used as a variable name).</p>
| 1 | 2009-12-01T19:03:13Z | [
"python",
"import",
"package"
] |
how to reference python package when filename contains a period | 1,828,127 | <p>I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:</p>
<pre><code>from "models.admin" import *
</code></pre>
<p>however, I get a syntax error for having double quotes. But if I just do </p>
<pre><code>from models.admin import *
</code></pre>
<p>then I get ... | 15 | 2009-12-01T18:53:48Z | 1,828,188 | <p>You are not referencing <em>files</em> in the import statement, you are referencing <em>modules</em> and <em>packages</em>.</p>
<p>Please read the <a href="http://docs.python.org/tutorial/modules.html" rel="nofollow">docs</a>, they are very clear on that matter.</p>
<p>Anyway, since you are using django, the usual... | 0 | 2009-12-01T19:05:21Z | [
"python",
"import",
"package"
] |
how to reference python package when filename contains a period | 1,828,127 | <p>I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:</p>
<pre><code>from "models.admin" import *
</code></pre>
<p>however, I get a syntax error for having double quotes. But if I just do </p>
<pre><code>from models.admin import *
</code></pre>
<p>then I get ... | 15 | 2009-12-01T18:53:48Z | 1,828,244 | <p>If you <em>really</em> want to, you can import a module with an unusual filename (e.g., a filename containing a '.' before the '.py') using the imp module:</p>
<pre><code>>>> import imp
>>> a_b = imp.load_source('a.b', 'a.b.py')
>>> a_b.x
"I was defined in a.b.py!"
</code></pre>
<p>Howev... | 8 | 2009-12-01T19:15:56Z | [
"python",
"import",
"package"
] |
how to reference python package when filename contains a period | 1,828,127 | <p>I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:</p>
<pre><code>from "models.admin" import *
</code></pre>
<p>however, I get a syntax error for having double quotes. But if I just do </p>
<pre><code>from models.admin import *
</code></pre>
<p>then I get ... | 15 | 2009-12-01T18:53:48Z | 1,828,249 | <p>Actually, you <em>can</em> import a module with an invalid name. But you'll need to use <a href="http://docs.python.org/library/imp.html"><code>imp</code></a> for that, e.g. assuming file is named <code>models.admin.py</code>, you could do</p>
<pre><code>import imp
with open('models.admin.py', 'rb') as fp:
mode... | 16 | 2009-12-01T19:16:50Z | [
"python",
"import",
"package"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 1,829,073 | <p>If you are running Django in debug mode (have <code>DEBUG = True</code> in your settings) and then type a non-existent URL you will get an error page listing the complete URL configuration.</p>
| -3 | 2009-12-01T21:32:05Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 1,829,362 | <p>Are you looking for the urls evaluated or not evaluated as shown in the DEBUG mode? For evaluated, django.contrib.sitemaps can help you there, otherwise it might involve some reverse engineering with Django's code.</p>
| 0 | 2009-12-01T22:22:18Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 1,829,565 | <p>Django is Python, so introspection is your friend.</p>
<p>In the shell, import <code>urls</code>. By looping through <code>urls.urlpatterns</code>, and drilling down through as many layers of included url configurations as possible, you can build the complete url configuration. </p>
<pre><code>import urls
urls.url... | 42 | 2009-12-01T23:02:21Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 17,414,187 | <p>At the risk of adding a "me too" answer, I am posting a modified version of the above submitted script that gives you a view listing all the URLs in the project, somewhat prettified and sorted alphabetically, and the views that they call. More of a developer tool than a production page. </p>
<pre><code>def all_ur... | 4 | 2013-07-01T21:41:25Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 17,817,886 | <p>The easiest way to get a complete list of registered URLs is to install <a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/admindocs/" rel="nofollow">contrib.admindocs</a> then check the "Views" section. Very easy to set up, and also gives you fully browsable docs on all of your template tags, models, ... | 1 | 2013-07-23T18:13:52Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 19,868,961 | <p>Django extensions provides a utility to do this as a manage.py command.</p>
<pre><code>pip install django-extensions
</code></pre>
<p>Then add <code>django_extensions</code> to your INSTALLED_APPS in <code>settings.py</code>. then from the console just type the following</p>
<pre><code>python manage.py show_urls
... | 44 | 2013-11-08T21:38:43Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 27,774,519 | <p>This question is a bit old, but I ran into the same problem and I thought I would discuss my solution. A given Django project obviously needs a means of knowing about all its URLs and needs to be able to do a couple things:</p>
<ol>
<li>map from a url -> view</li>
<li>map from a named url -> url (then 1 is used to ... | 3 | 2015-01-05T06:36:00Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 30,036,214 | <p>When I tried the other answers here, I got this error:</p>
<blockquote>
<p>django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.</p>
</blockquote>
<p>It looks like the problem comes from using <code>django.contrib.admin.autodiscover()</code> in my <code>urls.py</code>, so I can either comment that ... | 0 | 2015-05-04T17:29:04Z | [
"python",
"django",
"url"
] |
Determine complete Django url configuration | 1,828,187 | <p>Is there a way to get the <strong>complete</strong> django url configuration?</p>
<p>For example Django's debugging 404 page does not show included url configs, so this is not the complete configuration.</p>
<p><hr></p>
<p><strong>Answer</strong>: Thanks to Alasdair, here is an example script:</p>
<pre><code>imp... | 65 | 2009-12-01T19:05:15Z | 35,521,437 | <p>I have submitted a package (django-showurls) that adds this functionality to any Django project, it's a simple new management command that integrates well with manage.py:</p>
<pre><code>$ python manage.py showurls
^admin/
^$
^login/$
^logout/$
.. etc ..
</code></pre>
<p>You can install it through pip:</p... | 2 | 2016-02-20T09:36:36Z | [
"python",
"django",
"url"
] |
Optimized dot product in Python | 1,828,233 | <p>The dot product of two n-dimensional vectors <code>u=[u1,u2,...un]</code> and <code>v=[v1,v2,...,vn]</code> is is given by <code>u1*v1 + u2*v2 + ... + un*vn</code>.</p>
<p>A question <a href="http://stackoverflow.com/questions/1823293/optimized-method-for-calculating-cosine-distance-in-python/1823735#1823735">poste... | 7 | 2009-12-01T19:13:31Z | 1,828,462 | <p>I don't know about faster, but I'd suggest:</p>
<pre><code>sum(i*j for i, j in zip(v1, v2))
</code></pre>
<p>it's much easier to read and doesn't require even standard-library modules.</p>
| 4 | 2009-12-01T19:53:42Z | [
"python",
"algorithm",
"math"
] |
Optimized dot product in Python | 1,828,233 | <p>The dot product of two n-dimensional vectors <code>u=[u1,u2,...un]</code> and <code>v=[v1,v2,...,vn]</code> is is given by <code>u1*v1 + u2*v2 + ... + un*vn</code>.</p>
<p>A question <a href="http://stackoverflow.com/questions/1823293/optimized-method-for-calculating-cosine-distance-in-python/1823735#1823735">poste... | 7 | 2009-12-01T19:13:31Z | 1,828,580 | <p>Just for fun I wrote a "d4" which uses numpy: </p>
<pre><code>from numpy import dot
def d4(v1, v2):
check(v1, v2)
return dot(v1, v2)
</code></pre>
<p>My results (Python 2.5.1, XP Pro sp3, 2GHz Core2 Duo T7200): </p>
<pre><code>d0 elapsed: 12.1977242918
d1 elapsed: 13.885232341
d2 elapsed: 13.792955249... | 6 | 2009-12-01T20:12:59Z | [
"python",
"algorithm",
"math"
] |
Optimized dot product in Python | 1,828,233 | <p>The dot product of two n-dimensional vectors <code>u=[u1,u2,...un]</code> and <code>v=[v1,v2,...,vn]</code> is is given by <code>u1*v1 + u2*v2 + ... + un*vn</code>.</p>
<p>A question <a href="http://stackoverflow.com/questions/1823293/optimized-method-for-calculating-cosine-distance-in-python/1823735#1823735">poste... | 7 | 2009-12-01T19:13:31Z | 1,833,607 | <p>Here is a comparison with numpy. We compare the fast starmap approach with <code>numpy.dot</code></p>
<p>First, iteration over normal Python lists:</p>
<pre><code>$ python -mtimeit "import numpy as np; r = range(100)" "np.dot(r,r)"
10 loops, best of 3: 316 usec per loop
$ python -mtimeit "import operator; r = ran... | 1 | 2009-12-02T15:25:32Z | [
"python",
"algorithm",
"math"
] |
Optimized dot product in Python | 1,828,233 | <p>The dot product of two n-dimensional vectors <code>u=[u1,u2,...un]</code> and <code>v=[v1,v2,...,vn]</code> is is given by <code>u1*v1 + u2*v2 + ... + un*vn</code>.</p>
<p>A question <a href="http://stackoverflow.com/questions/1823293/optimized-method-for-calculating-cosine-distance-in-python/1823735#1823735">poste... | 7 | 2009-12-01T19:13:31Z | 1,953,387 | <p>Please benchmark this "d2a" function, and compare it to your "d3" function.</p>
<pre><code>from itertools import imap, starmap
from operator import mul
def d2a(v1,v2):
"""
d2a uses itertools.imap
"""
check(v1,v2)
return sum(imap(mul, v1, v2))
</code></pre>
<p><code>map</code> (on Python 2.x, w... | 0 | 2009-12-23T15:20:21Z | [
"python",
"algorithm",
"math"
] |
Any way to make nice antialiased round corners for images in python? | 1,828,345 | <p>Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an <code>arc()</code> method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy.</p>
<p>Is there any neat way to make antialiased/smooth... | 13 | 2009-12-01T19:32:44Z | 1,828,426 | <p>What I usually do is use an image as a mask, like this one for example:</p>
<p>border.png</p>
<p><img src="http://nadiana.com/sites/default/files/pil%5Ftutorial/border.png" alt="alt text"></p>
<pre><code>border = Image.open('border.png')
source = border.convert('RGB')
img.paste(source, mask=border)
</code></pre>
... | 16 | 2009-12-01T19:48:33Z | [
"python",
"image-processing",
"python-imaging-library",
"rounded-corners"
] |
Any way to make nice antialiased round corners for images in python? | 1,828,345 | <p>Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an <code>arc()</code> method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy.</p>
<p>Is there any neat way to make antialiased/smooth... | 13 | 2009-12-01T19:32:44Z | 1,828,491 | <p>You'll have to <a href="http://en.wikipedia.org/wiki/Supersampling" rel="nofollow">supersample</a>: draw the arc to a larger image and scale it down.</p>
<p>eg. a white, smoothly semi-transparent bottom-right 16x16 border, with 4x4x square supersampling:</p>
<pre><code>>>> import Image, ImageDraw
>>... | 4 | 2009-12-01T19:59:13Z | [
"python",
"image-processing",
"python-imaging-library",
"rounded-corners"
] |
Any way to make nice antialiased round corners for images in python? | 1,828,345 | <p>Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an <code>arc()</code> method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy.</p>
<p>Is there any neat way to make antialiased/smooth... | 13 | 2009-12-01T19:32:44Z | 1,839,511 | <p>So, okay, i found the solution.</p>
<p>I used the supersampling method <em>bobince</em> proposed before, but i found that the images become blurry. So, I would not provide the full code, but i'll explain my solution.</p>
<p>It may seem kind of idiotistic, but i haven't found any other way to do it without involvin... | 2 | 2009-12-03T12:04:57Z | [
"python",
"image-processing",
"python-imaging-library",
"rounded-corners"
] |
Any way to make nice antialiased round corners for images in python? | 1,828,345 | <p>Is there any way to make nice round corners with python? Currently PIL and GD2 are used in my project. Both of them have an <code>arc()</code> method, that allows you to draw a quater-circle, but the quater-circle is not antialiased, so the image looks crispy.</p>
<p>Is there any neat way to make antialiased/smooth... | 13 | 2009-12-01T19:32:44Z | 12,225,833 | <p>Use Xiaolin Wu's circle algorithm as described in Graphics Gems II. A concise summary and example (in php) han be found here: <a href="http://create.stephan-brumme.com/antialiased-circle/" rel="nofollow">http://create.stephan-brumme.com/antialiased-circle/</a></p>
<p>As for drawing an arc (partial circle), you mus... | 0 | 2012-09-01T07:30:10Z | [
"python",
"image-processing",
"python-imaging-library",
"rounded-corners"
] |
Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? | 1,828,379 | <p>When using the generated Python code from our protobuf classes, we get this error:</p>
<pre><code>cannot import name descriptor_pb2
</code></pre>
<p>The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.</p>
<p>This error occurs when I try... | 2 | 2009-12-01T19:38:39Z | 1,828,490 | <p>I believe you have to generate <code>descriptor_pb2.py</code> with <code>protoc</code> yourself:</p>
<pre><code>protoc descriptor.proto --python_out=gen/
</code></pre>
<p><code>gen/</code> is a folder with generated python classes.</p>
<p>After that, the following works just fine:</p>
<pre><code>sys.path.append(... | 6 | 2009-12-01T19:59:07Z | [
"python",
"protocol-buffers"
] |
Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? | 1,828,379 | <p>When using the generated Python code from our protobuf classes, we get this error:</p>
<pre><code>cannot import name descriptor_pb2
</code></pre>
<p>The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.</p>
<p>This error occurs when I try... | 2 | 2009-12-01T19:38:39Z | 1,829,103 | <p>Please make sure to install the protobuf runtime library as directed in the readme file. You cannot simply use the source directly out of the package, since descriptor_pb2.py needs to be generated by protoc (the protobuf compiler) as part of the installation process.</p>
| 2 | 2009-12-01T21:36:48Z | [
"python",
"protocol-buffers"
] |
Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? | 1,828,379 | <p>When using the generated Python code from our protobuf classes, we get this error:</p>
<pre><code>cannot import name descriptor_pb2
</code></pre>
<p>The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.</p>
<p>This error occurs when I try... | 2 | 2009-12-01T19:38:39Z | 20,403,944 | <p>In my case, not finding descriptor_pb2 occurred because protobuf wasn't correctly installed.
In the python subdirectory of protobuf, be sure to run</p>
<pre><code>python setup.py build
python setup.py test
python setup.py install (as root)
</code></pre>
| 4 | 2013-12-05T15:29:28Z | [
"python",
"protocol-buffers"
] |
Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? | 1,828,379 | <p>When using the generated Python code from our protobuf classes, we get this error:</p>
<pre><code>cannot import name descriptor_pb2
</code></pre>
<p>The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.</p>
<p>This error occurs when I try... | 2 | 2009-12-01T19:38:39Z | 31,883,244 | <p>python setup.py build</p>
<p>This step is mandatory as it generated some of the source files.</p>
<blockquote>
<p>Generating google/protobuf/descriptor_pb2.py... Generating
google/protobuf/compiler/plugin_pb2.py... Generating
google/protobuf/unittest_pb2.py... Generating
google/protobuf/unittest_custom_opt... | 0 | 2015-08-07T17:14:56Z | [
"python",
"protocol-buffers"
] |
Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers? | 1,828,379 | <p>When using the generated Python code from our protobuf classes, we get this error:</p>
<pre><code>cannot import name descriptor_pb2
</code></pre>
<p>The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.</p>
<p>This error occurs when I try... | 2 | 2009-12-01T19:38:39Z | 36,478,802 | <p>I use python 2.7 on windows 10.</p>
<p>In my case, I have downloaded protoc-3.0.0-beta-2-win32 from <a href="https://github.com/google/protobuf/releases" rel="nofollow">https://github.com/google/protobuf/releases</a> and copied the binary protoc file to src folder. </p>
<p>after that I have run the command <strong... | 1 | 2016-04-07T14:11:47Z | [
"python",
"protocol-buffers"
] |
PyQt subclassing | 1,828,567 | <p>The usual way to use Qt widgets from Python seems to be to subclass them.</p>
<p>Qt widget classes have a great many methods, so inevitably I'm going to end up adding a method to the subclass, with the same name as one inherited from the Qt widget. In Python, all methods are virtual, so what I'm concerned about is ... | 4 | 2009-12-01T20:10:09Z | 1,828,607 | <p>If the underlaying C++ methods are virtual, your Python methods that override them will be called any time C++ code calls them. If they are just regular methods, any C++ code will call the original C++ methods by default (Python code will call the Python methods though, because it sees the Python object and all meth... | 3 | 2009-12-01T20:16:28Z | [
"python",
"qt",
"qt4",
"pyqt",
"pyqt4"
] |
how to create windows 7 jump lists via python/pyqt? | 1,828,927 | <p>i have a pyqt project which i'm interested in using to play around with the new windows 7 jump list feature. after a bunch of searching, i have not found any specific examples of anyone creating jumplists via python.</p>
<p>has anyone here found an easy way to hook into this? does mark hammond's pywin32 module ha... | 3 | 2009-12-01T21:05:36Z | 1,829,011 | <p>I don't think Qt supports jump lists, you can find a bit more info <a href="http://stackoverflow.com/questions/1518620/qt-what-is-the-current-status-of-windows-7-taskbar-extensions-support">here</a></p>
<p>Qt 4.6 added support for windows 7 and it was released today but I don't think they added this specific featur... | 1 | 2009-12-01T21:21:26Z | [
"python",
"windows",
"windows-7",
"pyqt",
"jump-list"
] |
how to create windows 7 jump lists via python/pyqt? | 1,828,927 | <p>i have a pyqt project which i'm interested in using to play around with the new windows 7 jump list feature. after a bunch of searching, i have not found any specific examples of anyone creating jumplists via python.</p>
<p>has anyone here found an easy way to hook into this? does mark hammond's pywin32 module ha... | 3 | 2009-12-01T21:05:36Z | 2,827,562 | <p>There is a Qt add-on that implements all the Windows 7 taskbar extensions. It is called <a href="http://www.strixcode.com/q7goodies/" rel="nofollow">Q7Goodies</a>. Although, it is a C++ library, I suggest contacting the authors, maybe they provide a PyQt bindings too.</p>
| 0 | 2010-05-13T14:25:52Z | [
"python",
"windows",
"windows-7",
"pyqt",
"jump-list"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,829,171 | <p>What you're trying to do here (store a shared state in a Python module over separate python interpreters) won't work.</p>
<p>A value in a module can be updated by one module and then read by another module, but this must be within the same Python interpreter. What you seem to be doing here is actually a sort of int... | 5 | 2009-12-01T21:48:48Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,829,178 | <p>You need to store the variable in some sort of persistent file. There are several modules to do this, depending on your exact need. </p>
<p>The pickle and cPickle module can save and load most python objects to file.</p>
<p>The shelve module can store python objects in a dictionary-like structure (using pickle b... | 2 | 2009-12-01T21:50:11Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,829,186 | <p>Use text files or environnement variables. Since the two run separatly, you can't really do what you are trying to do.</p>
| 1 | 2009-12-01T21:51:02Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,829,270 | <p>You're not going to be able to do what you want without storing the information somewhere external to the two instances of the interpreter.<br>
If it's just simple variables you want, you can easily dump a python dict to a file with the pickle module in script one and then re-load it in script two.
Example:</p>
<p>... | 13 | 2009-12-01T22:05:37Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,829,803 | <p>In your example, the first script runs to completion, and then the second script runs. That means you need some sort of persistent state. Other answers have suggested using text files or Python's <code>pickle</code> module. Personally I am lazy, and I wouldn't use a text file when I could use <code>pickle</code>;... | 0 | 2009-12-01T23:59:10Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 1,830,574 | <p>I'd advise that you use the <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">multiprocessing</a> module. You can't run two scripts from the commandline, but you can have two separate processes easily speak to each other.</p>
<p>From the doc's examples:</p>
<pre><code>from multiprocessi... | 2 | 2009-12-02T04:01:06Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 3,898,232 | <p>sudo apt-get install memcached python-memcache</p>
<p>one.py</p>
<pre><code>import memcache
shared = memcache.Client(['127.0.0.1:11211'], debug=0)
shared.set('Value', 'Hello')
</code></pre>
<p>two.py</p>
<pre><code>import memcache
shared = memcache.Client(['127.0.0.1:11211'], debug=0)
print shared.get('Value... | 7 | 2010-10-09T22:54:13Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 14,700,365 | <p>Hope it's OK to jot down my notes about this issue here. </p>
<p>First of all, I appreciate the example in the OP a lot, because that is where I started as well - although it made me think <code>shared</code> is some built-in Python module, until I found a complete example at <a href="http://mail.python.org/piperma... | 12 | 2013-02-05T04:45:07Z | [
"python",
"variables",
"share"
] |
How to share variables across scripts in python? | 1,829,116 | <p>The following does not work</p>
<p>one.py</p>
<pre><code>import shared
shared.value = 'Hello'
raw_input('A cheap way to keep process alive..')
</code></pre>
<p>two.py</p>
<pre><code>import shared
print shared.value
</code></pre>
<p>run on two command lines as:</p>
<pre><code>>>python one.py
>>pytho... | 8 | 2009-12-01T21:38:54Z | 28,167,809 | <p>you can use the relative simple mmap file.
you can use the shared.py to store the common constants. The following code will work across different python interpreters \ scripts \processes</p>
<p>shared.py:</p>
<pre><code>MMAP_SIZE = 16*1024
MMAP_NAME = 'Global\\SHARED_MMAP_NAME'
</code></pre>
<p>* The "Global" is... | 2 | 2015-01-27T10:08:00Z | [
"python",
"variables",
"share"
] |
python modify __metaclass__ for whole program | 1,829,205 | <p><strong>EDIT:</strong> Note that this is a REALLY BAD idea to do in production code. This was just an interesting thing for me. Don't do this at home!</p>
<p>Is it possible to modify __metaclass__ variable for whole program (interpreter) in Python? </p>
<p>This simple example is working:</p>
<pre><code>class Chat... | 2 | 2009-12-01T21:55:05Z | 1,829,505 | <p>Nope. (This is a feature!)</p>
| 1 | 2009-12-01T22:51:42Z | [
"python",
"metaprogramming",
"metaclass",
"python-2.x"
] |
python modify __metaclass__ for whole program | 1,829,205 | <p><strong>EDIT:</strong> Note that this is a REALLY BAD idea to do in production code. This was just an interesting thing for me. Don't do this at home!</p>
<p>Is it possible to modify __metaclass__ variable for whole program (interpreter) in Python? </p>
<p>This simple example is working:</p>
<pre><code>class Chat... | 2 | 2009-12-01T21:55:05Z | 1,830,067 | <p>The "global <code>__metaclass__</code>" feature of Python 2 is designed to work per-module, only (just think what havoc it would wreak, otherwise, by forcing your own metaclass on all library and third-party modules that you imported from that point onwards -- shudder!). If it's very important to you to "secretly" ... | 7 | 2009-12-02T01:10:02Z | [
"python",
"metaprogramming",
"metaclass",
"python-2.x"
] |
python modify __metaclass__ for whole program | 1,829,205 | <p><strong>EDIT:</strong> Note that this is a REALLY BAD idea to do in production code. This was just an interesting thing for me. Don't do this at home!</p>
<p>Is it possible to modify __metaclass__ variable for whole program (interpreter) in Python? </p>
<p>This simple example is working:</p>
<pre><code>class Chat... | 2 | 2009-12-01T21:55:05Z | 1,830,201 | <p>Okay; IMO this is gross, hairy, dark magic. You shouldn't use it, perhaps ever, but especially not in production code. It is kind of interesting just for curiosity's sake, however.</p>
<p>You can write a custom importer using the mechanisms described in <a href="http://www.python.org/dev/peps/pep-0302/" rel="nofo... | 4 | 2009-12-02T01:57:00Z | [
"python",
"metaprogramming",
"metaclass",
"python-2.x"
] |
How can I programmatically obtain the max_length of a Django model field? | 1,829,216 | <p>Say I have a Django class something like this:</p>
<pre><code>class Person(models.Model):
name = models.CharField(max_length=50)
# ...
</code></pre>
<p>How can I programatically obtain the <code>max_length</code> value for the <code>name</code> field?</p>
| 44 | 2009-12-01T21:56:14Z | 1,829,286 | <p><code>Person._meta.get_field('name').max_length</code> will give you this value. <s>But having to use <code>_meta</code> suggests this is something you shouldn't do in normal usage.</s></p>
<p>Edit: as Carl pointed out, this naming is misleading and it does seem quite acceptable to use it: <a href="http://www.b-lis... | 58 | 2009-12-01T22:08:19Z | [
"python",
"django",
"django-models",
"oop"
] |
How can I programmatically obtain the max_length of a Django model field? | 1,829,216 | <p>Say I have a Django class something like this:</p>
<pre><code>class Person(models.Model):
name = models.CharField(max_length=50)
# ...
</code></pre>
<p>How can I programatically obtain the <code>max_length</code> value for the <code>name</code> field?</p>
| 44 | 2009-12-01T21:56:14Z | 31,211,736 | <p>Ben James answer is a good answer, but I think this one is easier and more clear:<br>
1. In a template:
<br>
<code>{{form.name.field.max_length}}</code>
<br><br>
2. In python code (e.g. in the view)
<br>
<code>form.name.field.max_length</code></p>
| 4 | 2015-07-03T17:21:03Z | [
"python",
"django",
"django-models",
"oop"
] |
Python 3: Best string compression method to minimize the size of a sqlite3 db | 1,829,256 | <p>I recently created a script that parses several web proxy logs into a tidy sqlite3 db file that is working great for me... with one snag. the file size. I have been pressed to use this format (a sqlite3 db) and python handles it natively like a champ, so my question is this... what is the best form of string comp... | 0 | 2009-12-01T22:02:52Z | 1,829,367 | <p>Here is a <a href="http://lserinol.googlepages.com/sqlitecompress" rel="nofollow">page</a> with an SQLite extension to provide compression.</p>
<p>This extension provides a function that can be called on individual fields.</p>
<p>Here is some of the example text from the page </p>
<blockquote>
<p>create a test ... | 1 | 2009-12-01T22:23:46Z | [
"python",
"sqlite",
"compression"
] |
Python 3: Best string compression method to minimize the size of a sqlite3 db | 1,829,256 | <p>I recently created a script that parses several web proxy logs into a tidy sqlite3 db file that is working great for me... with one snag. the file size. I have been pressed to use this format (a sqlite3 db) and python handles it natively like a champ, so my question is this... what is the best form of string comp... | 0 | 2009-12-01T22:02:52Z | 1,829,601 | <p>what sort of parsing do you do before you put it in the database? I get the impression that it is fairly simple with a single table holding each entry - if not then my apologies. </p>
<p>Compression is all about removing duplication, and in a log file most of the duplication is between entries rather than within e... | 0 | 2009-12-01T23:09:32Z | [
"python",
"sqlite",
"compression"
] |
Python 3: Best string compression method to minimize the size of a sqlite3 db | 1,829,256 | <p>I recently created a script that parses several web proxy logs into a tidy sqlite3 db file that is working great for me... with one snag. the file size. I have been pressed to use this format (a sqlite3 db) and python handles it natively like a champ, so my question is this... what is the best form of string comp... | 0 | 2009-12-01T22:02:52Z | 1,832,688 | <p>Instead of inserting compression/decompression code into your program, you could store the table itself on a compressed drive.</p>
| 0 | 2009-12-02T12:45:34Z | [
"python",
"sqlite",
"compression"
] |
Solving a cubic equation | 1,829,330 | <p>As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):</p>
<pre><code>a*x**3 + b*x**2 + c*x + d = 0.
</code></pre>
<p>I'm trying to use the equations from <a href="http://en.wikipedia.org/wiki/Cubic_function#Root-finding_formula" rel="nofollow">here<... | 8 | 2009-12-01T22:16:45Z | 1,829,437 | <p>I've looked at the Wikipedia article and your program.</p>
<p>I also solved the equation using <a href="http://www.wolframalpha.com" rel="nofollow">Wolfram Alpha</a> and the results there don't match what you get.</p>
<p>I'd just go through your program at each step, use a lot of print statements, and get each int... | 3 | 2009-12-01T22:37:40Z | [
"python",
"algorithm",
"math",
"cubic"
] |
Solving a cubic equation | 1,829,330 | <p>As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):</p>
<pre><code>a*x**3 + b*x**2 + c*x + d = 0.
</code></pre>
<p>I'm trying to use the equations from <a href="http://en.wikipedia.org/wiki/Cubic_function#Root-finding_formula" rel="nofollow">here<... | 8 | 2009-12-01T22:16:45Z | 1,829,469 | <p>Wikipedia's notation <code>(rho^(1/3), theta/3)</code> does not mean that <code>rho^(1/3)</code> is the real part and <code>theta/3</code> is the imaginary part. Rather, this is in polar coordinates. Thus, if you want the real part, you would take <code>rho^(1/3) * cos(theta/3)</code>.</p>
<p>I made these changes... | 22 | 2009-12-01T22:44:54Z | [
"python",
"algorithm",
"math",
"cubic"
] |
Solving a cubic equation | 1,829,330 | <p>As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):</p>
<pre><code>a*x**3 + b*x**2 + c*x + d = 0.
</code></pre>
<p>I'm trying to use the equations from <a href="http://en.wikipedia.org/wiki/Cubic_function#Root-finding_formula" rel="nofollow">here<... | 8 | 2009-12-01T22:16:45Z | 16,867,984 | <p>Here's A. Rex's solution in JavaScript:</p>
<pre><code>a = 1.0;
b = 0.0;
c = 0.2 - 1.0;
d = -0.7 * 0.2;
q = (3*a*c - Math.pow(b, 2)) / (9 * Math.pow(a, 2));
r = (9*a*b*c - 27*Math.pow(a, 2)*d - 2*Math.pow(b, 3)) / (54*Math.pow(a, 3));
console.log("q = "+q);
console.log("r = "+r);
delta = Math.pow(q, 3) + Math.... | 0 | 2013-06-01T00:55:45Z | [
"python",
"algorithm",
"math",
"cubic"
] |
Solving a cubic equation | 1,829,330 | <p>As part of a program I'm writing, I need to solve a cubic equation exactly (rather than using a numerical root finder):</p>
<pre><code>a*x**3 + b*x**2 + c*x + d = 0.
</code></pre>
<p>I'm trying to use the equations from <a href="http://en.wikipedia.org/wiki/Cubic_function#Root-finding_formula" rel="nofollow">here<... | 8 | 2009-12-01T22:16:45Z | 22,297,736 | <p>The program below perfectly solves the Cubic Equation of the form Ax^3 + Bx^2 + Cx + D.</p>
<p>The flawless code is written in .Net C#.</p>
<p>The real and imaginary roots are separated by black and red colors respectively.</p>
<p>Simply update the value of variables A, B, C and D to see the answers i.e. the solu... | -1 | 2014-03-10T10:31:21Z | [
"python",
"algorithm",
"math",
"cubic"
] |
pythonic way to aggregate arrays (numpy or not) | 1,829,340 | <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
<p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and y... | 5 | 2009-12-01T22:17:54Z | 1,829,500 | <p><a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#dictionary-get-method" rel="nofollow">http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#dictionary-get-method</a></p>
<p>should help to make it a little prettier, more pythonic, more efficient possibly. I'll come bac... | 0 | 2009-12-01T22:51:01Z | [
"python",
"arrays",
"numpy",
"aggregate"
] |
pythonic way to aggregate arrays (numpy or not) | 1,829,340 | <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
<p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and y... | 5 | 2009-12-01T22:17:54Z | 1,829,504 | <p>Your <code>if k not in data_per_key.keys()</code> could be rewritten as <code>if k not in data_per_key</code>, but you can do even better with <a href="http://docs.python.org/library/collections.html#defaultdict-objects"><code>defaultdict</code></a>. Here's a version that uses <code>defaultdict</code> to get rid of ... | 5 | 2009-12-01T22:51:37Z | [
"python",
"arrays",
"numpy",
"aggregate"
] |
pythonic way to aggregate arrays (numpy or not) | 1,829,340 | <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
<p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and y... | 5 | 2009-12-01T22:17:54Z | 1,829,848 | <p>Perhaps the function you are seeking is <a href="http://matplotlib.sourceforge.net/api/mlab_api.html#matplotlib.mlab.rec_groupby" rel="nofollow">matplotlib.mlab.rec_groupby</a>:</p>
<pre><code>import matplotlib.mlab
data=np.array(
[('Aaron','Digger',1),
('Bill','Planter',2),
('Carl','Waterer',3),
... | 5 | 2009-12-02T00:09:47Z | [
"python",
"arrays",
"numpy",
"aggregate"
] |
pythonic way to aggregate arrays (numpy or not) | 1,829,340 | <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
<p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and y... | 5 | 2009-12-01T22:17:54Z | 14,292,283 | <p><a href="http://www.scipy.org/Cookbook/AccumarrayLike" rel="nofollow">Here</a> is a recipe which emulates the functionality of matlabs accumarray quite well. It uses pythons iterators quite nicely, nevertheless, performancewise it sucks compared to the matlab implementation. As I had the same problem, I had written ... | 2 | 2013-01-12T10:07:35Z | [
"python",
"arrays",
"numpy",
"aggregate"
] |
pythonic way to aggregate arrays (numpy or not) | 1,829,340 | <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
<p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and y... | 5 | 2009-12-01T22:17:54Z | 24,937,168 | <p>Best flexibility and readability is get using <a href="http://pandas.sourceforge.net/" rel="nofollow">pandas</a>:</p>
<pre><code>import pandas
data=np.array(
[('Aaron','Digger',1),
('Bill','Planter',2),
('Carl','Waterer',3),
('Darlene','Planter',3),
('Earl','Digger',7)],
dtype=[('name',... | 2 | 2014-07-24T14:55:01Z | [
"python",
"arrays",
"numpy",
"aggregate"
] |
Delaunay tessellation in Python? | 1,829,365 | <p>I need to find the Delaunay tessellation of a polygon in Python, and the only libraries I could find (Delny, scikits) triangulate point clouds, not polygons. Any suggestions?</p>
| 3 | 2009-12-01T22:23:06Z | 1,829,486 | <p>According to Wikipedia's <a href="http://en.wikipedia.org/wiki/Delaunay%5Ftriangulation" rel="nofollow">article</a>, the Delaunay triangulation is defined for a set of points, not for a polygon. Could you just pass the set of the polygon's points into one of those libraries?</p>
| 1 | 2009-12-01T22:48:09Z | [
"python",
"graphics",
"delaunay"
] |
Delaunay tessellation in Python? | 1,829,365 | <p>I need to find the Delaunay tessellation of a polygon in Python, and the only libraries I could find (Delny, scikits) triangulate point clouds, not polygons. Any suggestions?</p>
| 3 | 2009-12-01T22:23:06Z | 1,829,520 | <p>Apparently <a href="http://www.cs.cmu.edu/~quake/triangle.html" rel="nofollow">Triangle</a> has a Python binding. I'll try to get it working</p>
| 2 | 2009-12-01T22:54:16Z | [
"python",
"graphics",
"delaunay"
] |
Delaunay tessellation in Python? | 1,829,365 | <p>I need to find the Delaunay tessellation of a polygon in Python, and the only libraries I could find (Delny, scikits) triangulate point clouds, not polygons. Any suggestions?</p>
| 3 | 2009-12-01T22:23:06Z | 1,829,672 | <p>Have you tried matplotlib.delaunay.interpolate <a href="http://matplotlib.sourceforge.net/" rel="nofollow">link text</a> or Delny <a href="http://flub.stuffwillmade.org/delny/" rel="nofollow">link text</a>? </p>
| 0 | 2009-12-01T23:27:13Z | [
"python",
"graphics",
"delaunay"
] |
Ranking Elements of multiple Lists by their count in Python | 1,829,470 | <p>I want to rank multiple lists according to their elements how often they appear in each list. Example:</p>
<p>list1 = 1,2,3,4<br>
list2 = 4,5,6,7<br>
list3 = 4,1,8,9<br></p>
<p>result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once)</p>
<p>I've tried the following but i need something... | 1 | 2009-12-01T22:45:00Z | 1,829,527 | <pre><code>import collections
data = [
[1, 2, 3, 4, 5],
[1, 9, 3, 4, 5],
[1, 10, 8, 4, 5],
[1, 12, 13, 7, 5],
[1, 14, 13, 13, 6],
]
def sorted_by_count(lists):
counts = collections.defaultdict(int)
for L in lists:
for n in L:
counts[n] += 1
return [num for num, count in
sorted(cou... | 6 | 2009-12-01T22:54:43Z | [
"python",
"list",
"set",
"ranking",
"rank"
] |
Ranking Elements of multiple Lists by their count in Python | 1,829,470 | <p>I want to rank multiple lists according to their elements how often they appear in each list. Example:</p>
<p>list1 = 1,2,3,4<br>
list2 = 4,5,6,7<br>
list3 = 4,1,8,9<br></p>
<p>result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once)</p>
<p>I've tried the following but i need something... | 1 | 2009-12-01T22:45:00Z | 1,829,558 | <pre><code>def items_ordered_by_frequency(*lists):
# get a flat list with all the values
biglist = []
for x in lists:
biglist += x
# sort it in reverse order by frequency
return sorted(set(biglist),
key=lambda x: biglist.count(x),
reverse=True)
</code>... | 1 | 2009-12-01T23:01:38Z | [
"python",
"list",
"set",
"ranking",
"rank"
] |
Ranking Elements of multiple Lists by their count in Python | 1,829,470 | <p>I want to rank multiple lists according to their elements how often they appear in each list. Example:</p>
<p>list1 = 1,2,3,4<br>
list2 = 4,5,6,7<br>
list3 = 4,1,8,9<br></p>
<p>result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once)</p>
<p>I've tried the following but i need something... | 1 | 2009-12-01T22:45:00Z | 1,829,617 | <p>Try this one:</p>
<pre><code>def rank(*lists):
d = dict()
for lst in lists:
for e in lst:
if e in d: d[e] += 1
else: d[e] = 1
return [j[1] for j in sorted([(d[i],i) for i in d], reverse=True)]
</code></pre>
<p>Usage example:</p>
<pre><code>a = [1,2,3,4]
b = [4,5,6,7]
c ... | 0 | 2009-12-01T23:12:48Z | [
"python",
"list",
"set",
"ranking",
"rank"
] |
Ranking Elements of multiple Lists by their count in Python | 1,829,470 | <p>I want to rank multiple lists according to their elements how often they appear in each list. Example:</p>
<p>list1 = 1,2,3,4<br>
list2 = 4,5,6,7<br>
list3 = 4,1,8,9<br></p>
<p>result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once)</p>
<p>I've tried the following but i need something... | 1 | 2009-12-01T22:45:00Z | 1,829,683 | <p>Combining a couple of ideas already posted:</p>
<pre><code>from itertools import chain
from collections import defaultdict
def frequency(*lists):
counter = defaultdict(int)
for x in chain(*lists):
counter[x] += 1
return [key for (key, value) in
sorted(counter.items(), key=lambda kv: (k... | 2 | 2009-12-01T23:29:44Z | [
"python",
"list",
"set",
"ranking",
"rank"
] |
Ranking Elements of multiple Lists by their count in Python | 1,829,470 | <p>I want to rank multiple lists according to their elements how often they appear in each list. Example:</p>
<p>list1 = 1,2,3,4<br>
list2 = 4,5,6,7<br>
list3 = 4,1,8,9<br></p>
<p>result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once)</p>
<p>I've tried the following but i need something... | 1 | 2009-12-01T22:45:00Z | 1,829,721 | <p>You can count the number of appearances of each element (a histogram), then sort by it:</p>
<pre><code>def histogram(enumerable):
result = {}
for x in enumerable:
result.setdefault(x, 0)
result[x] += 1
return result
lists = [ [1,2,3,4], [4,5,6,7], ... ]
from itertools import chain
h = histogram(cha... | 0 | 2009-12-01T23:37:55Z | [
"python",
"list",
"set",
"ranking",
"rank"
] |
Review my Django Model - Need lots of suggestions | 1,829,489 | <p>I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy... | 1 | 2009-12-01T22:49:01Z | 1,829,632 | <p>It seems to me that many of your ManyToMany relations should be replaced by properties of the target model.</p>
<p>For example, you've got <code>city</code> and <code>country</code> as separate relations. That means that you might end up with a user having <code>city = ['Paris', 'London', 'New York']</code> and <co... | 4 | 2009-12-01T23:16:48Z | [
"django",
"django-models",
"coding-style",
"python"
] |
Review my Django Model - Need lots of suggestions | 1,829,489 | <p>I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy... | 1 | 2009-12-01T22:49:01Z | 1,829,633 | <p>I'd make subordinate field and remove supervisor.</p>
<pre><code> subordinates = models.ManyToManyField('self', related_name='supervisors', through=Position)
</code></pre>
<p>This way supervisors will be an automatic reverse lookup field and there will be a record of Position that connects supervisor with the subo... | 2 | 2009-12-01T23:17:36Z | [
"django",
"django-models",
"coding-style",
"python"
] |
Review my Django Model - Need lots of suggestions | 1,829,489 | <p>I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy... | 1 | 2009-12-01T22:49:01Z | 1,829,642 | <p>Here's a few thoughts.</p>
<p>I think you should create separate models for <code>department</code> and <code>job_classification</code> and so on. For example</p>
<pre><code>class department(models.Model):
name = Models.CharField(max_length=100)
number = Models.IntegerField
</code></pre>
<p>and then have ... | 2 | 2009-12-01T23:20:00Z | [
"django",
"django-models",
"coding-style",
"python"
] |
Review my Django Model - Need lots of suggestions | 1,829,489 | <p>I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy... | 1 | 2009-12-01T22:49:01Z | 1,829,710 | <p><code>__unicode__</code> should return unicode</p>
<pre><code>def __unicode__(self):
return u'%s' %self.name
</code></pre>
<p>Django provides a emailfield for complete adresses:</p>
<pre><code>mail = models.EmailField()
</code></pre>
<p>I think, a address model might be sense-full. A person could have sev... | 5 | 2009-12-01T23:36:10Z | [
"django",
"django-models",
"coding-style",
"python"
] |
Review my Django Model - Need lots of suggestions | 1,829,489 | <p>I am pulling a variety of information sources to build up a profile of a person. Once I do this I want to the flexibility to look at a person in a different ways. I don't have a lot of expierience in django so I would like a critique (be gentle) of my model. Admittedly even as I coded this I'm thinking redundancy... | 1 | 2009-12-01T22:49:01Z | 1,831,407 | <p>I would also suggest that you use a separate model for employment. Use a foreign key back to your Person object. You can add as many records to that table as necessary. </p>
<p>Same suggestion goes for the mail_* fields as well.</p>
| 1 | 2009-12-02T08:21:51Z | [
"django",
"django-models",
"coding-style",
"python"
] |
How to run installed python script? | 1,829,524 | <p>I used distutils to install my python package, with this setup.py :</p>
<pre><code>import distutils.core
args = {
'name' : 'plugh',
'version' : '1.0',
'scripts' : [ "scripts/plugh" ],
'packages': [ "plugh" ],
}
d = distutils.core.setup(
**args
)
</code></pre>... | 6 | 2009-12-01T22:54:38Z | 1,829,638 | <p>windows uses the extension of the file to determine how it will run.</p>
<p>Name your file <code>plugh.py</code> and use <code>plugh.py</code> on the prompt to call it.</p>
| 5 | 2009-12-01T23:19:26Z | [
"python",
"windows",
"packaging",
"distutils"
] |
How to run installed python script? | 1,829,524 | <p>I used distutils to install my python package, with this setup.py :</p>
<pre><code>import distutils.core
args = {
'name' : 'plugh',
'version' : '1.0',
'scripts' : [ "scripts/plugh" ],
'packages': [ "plugh" ],
}
d = distutils.core.setup(
**args
)
</code></pre>... | 6 | 2009-12-01T22:54:38Z | 1,936,850 | <ol>
<li><p>If you use <a href="http://www.activestate.com/activepython/">ActivePython</a>, it will already add the <code>C:\PythonXY\Scripts</code> directory to your <code>%PATH%</code> (ActivePython 2.6 additionally adds <a href="http://www.python.org/dev/peps/pep-0370/">PEP 370</a>'s <code>%APPDATA%\Python\Scripts</... | 5 | 2009-12-20T20:10:47Z | [
"python",
"windows",
"packaging",
"distutils"
] |
media.set_xx ValueError | 1,829,810 | <p>New guy here. I asked a while back about a sprite recolouring program that I was having difficulty with and got some great responses. Basically, I tried to write a program that would recolour pixels of all the pictures in a given folder from one given colour to another.</p>
<p>I believe I have it down, but, now the... | 1 | 2009-12-02T00:00:35Z | 1,869,679 | <p>Slow going; but no worries I got it. Didn't convert the values to integers, was trying to use strings as arguments...</p>
| 0 | 2009-12-08T20:38:46Z | [
"python",
"media"
] |
How to read datetime back from sqlite as a datetime instead of string in Python? | 1,829,872 | <p>I'm using the sqlite3 module in Python 2.6.4 to store a datetime in a SQLite database. Inserting it is very easy, because sqlite automatically converts the date to a string. The problem is, when reading it it comes back as a string, but I need to reconstruct the original datetime object. How do I do this?</p>
| 41 | 2009-12-02T00:15:06Z | 1,830,011 | <p>It turns out that sqlite3 can do this and it's even <a href="http://docs.python.org/library/sqlite3.html#module-functions-and-constants">documented</a>, kind of - but it's pretty easy to miss or misunderstand.</p>
<p>What I had to do is:</p>
<ul>
<li>Pass the <strong>sqlite3.PARSE_COLNAMES</strong> option in the .... | 12 | 2009-12-02T00:51:38Z | [
"python",
"datetime",
"sqlite",
"sqlite3"
] |
How to read datetime back from sqlite as a datetime instead of string in Python? | 1,829,872 | <p>I'm using the sqlite3 module in Python 2.6.4 to store a datetime in a SQLite database. Inserting it is very easy, because sqlite automatically converts the date to a string. The problem is, when reading it it comes back as a string, but I need to reconstruct the original datetime object. How do I do this?</p>
| 41 | 2009-12-02T00:15:06Z | 1,830,499 | <p>If you declare your column with a type of timestamp, you're in clover:</p>
<pre><code>>>> db = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
>>> c = db.cursor()
>>> c.execute('create table foo (bar integer, baz timestamp)')
<sqlite3.Cursor object at 0x40fc50>
>... | 68 | 2009-12-02T03:33:05Z | [
"python",
"datetime",
"sqlite",
"sqlite3"
] |
reusable application for django site wide announcements that displays a message only once per user | 1,829,926 | <p>I want to show various messages to registered users only once in my django application. I found <a href="http://github.com/pinax/django-announcements/" rel="nofollow">django-announcements</a> which seemed to do what I want - but I found in testing it marks messages as read by using a session variable, which disappe... | 0 | 2009-12-02T00:26:50Z | 1,829,956 | <p>Have a look at <a href="http://github.com/jtauber/django-notification/" rel="nofollow">django-notification</a>. It is used by pinax, there it seems to work like what you are searching for. At least it saves the status in the db.</p>
<h2>edit</h2>
<p>Response to the comment</p>
<p>from the <a href="http://github.c... | 3 | 2009-12-02T00:32:52Z | [
"python",
"django",
"code-reuse",
"pinax"
] |
reusable application for django site wide announcements that displays a message only once per user | 1,829,926 | <p>I want to show various messages to registered users only once in my django application. I found <a href="http://github.com/pinax/django-announcements/" rel="nofollow">django-announcements</a> which seemed to do what I want - but I found in testing it marks messages as read by using a session variable, which disappe... | 0 | 2009-12-02T00:26:50Z | 6,818,850 | <p>Have you looked at the <a href="https://docs.djangoproject.com/en/1.3/ref/contrib/messages/" rel="nofollow">Messages Framework</a> in Django 1.3? In Django <=1.2 it was a simple model so you could do:</p>
<pre><code>for user in User.objects.all():
user.message_set.create(message="some text")
</code></pre>
<... | 0 | 2011-07-25T15:53:10Z | [
"python",
"django",
"code-reuse",
"pinax"
] |
Good way to pass variables for common elements to Mako templates? | 1,830,042 | <p>I'm using Mako's inheritance features to factor out common page elements, like a header and footer, into a "base.mako" template. Page-specific controllers use their own templates, which inherit base.mako. </p>
<p>base.mako needs a set of variables -- for example, the name of the logged-on user goes in the header f... | 1 | 2009-12-02T01:03:59Z | 1,830,276 | <p>You have two OO design choices for your page controllers.</p>
<p>Common features can be handled two ways.</p>
<ul>
<li><p>Inheritance. All page controllers are subclasses of a common class that provides the common attributes.</p></li>
<li><p>Delegation. All page controllers are part of a pipeline where some comm... | 0 | 2009-12-02T02:21:18Z | [
"python",
"mako"
] |
TypeError: ListControl, must set a sequence (python error) | 1,830,262 | <p>I am using Python Mechanize to open a website, fill out a form, and submit that form.
It's actually pretty simple.
It works until I come across radio buttons and "select" input boxes.</p>
<pre><code>br.open(url)
br.select_form(name="postmsg")
br.form['subject'] = "Is this good for the holidays? "
br.form['message']... | 5 | 2009-12-02T02:17:06Z | 1,830,294 | <p>Radio buttons and Check-boxes can have different behavior then other elements. It depends on their name and id.</p>
<p>If the items have the same name, try doing this:</p>
<pre><code>br.find_control(name="E").value = ["0"]
</code></pre>
<p>Another option is:</p>
<pre><code>form.find_control(name="E", kind="list"... | 8 | 2009-12-02T02:27:56Z | [
"python",
"http",
"url",
"mechanize"
] |
How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder? | 1,830,304 | <p>I'm working with <a href="http://www.pyinstaller.org">PyInstaller</a> under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.</p>
<p>The problem is that the manifest embedding support relies on the <a hr... | 46 | 2009-12-02T02:31:03Z | 1,831,370 | <p>OK, well since I had to find a way forward I improvised. I've internally created a git repository with a hacked-together version of pywin32 that will install within a virtualenv using the standard <code>setup.py</code> script. It took a lot of fiddling to make it work right but I managed to get it to load and the ... | 6 | 2009-12-02T08:13:13Z | [
"python",
"winapi",
"virtualenv",
"pyinstaller"
] |
How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder? | 1,830,304 | <p>I'm working with <a href="http://www.pyinstaller.org">PyInstaller</a> under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.</p>
<p>The problem is that the manifest embedding support relies on the <a hr... | 46 | 2009-12-02T02:31:03Z | 2,500,528 | <p>I found <a href="http://old.nabble.com/Windows:-virtualenv-and-pywin32--td27658201.html">http://old.nabble.com/Windows:-virtualenv-and-pywin32--td27658201.html</a> (now a dead link) which offered the following solution:</p>
<ol>
<li>Browse <a href="http://sourceforge.net/projects/pywin32/files/">http://sourceforge.... | 69 | 2010-03-23T14:09:21Z | [
"python",
"winapi",
"virtualenv",
"pyinstaller"
] |
How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder? | 1,830,304 | <p>I'm working with <a href="http://www.pyinstaller.org">PyInstaller</a> under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.</p>
<p>The problem is that the manifest embedding support relies on the <a hr... | 46 | 2009-12-02T02:31:03Z | 33,778,121 | <p>This may have been improved since previous answer, since I've successfully installed pywin32 on sandbox on several machines without any specific "hacks" :</p>
<pre><code>$ virtualenv sandbox
$ sandbox\scripts\activate
(sandbox) $ git clone https://github.com/Travis-Sun/pywin32.git
(sandbox) $ cd pywin32
(sandbox) $... | 0 | 2015-11-18T10:53:44Z | [
"python",
"winapi",
"virtualenv",
"pyinstaller"
] |
How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder? | 1,830,304 | <p>I'm working with <a href="http://www.pyinstaller.org">PyInstaller</a> under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.</p>
<p>The problem is that the manifest embedding support relies on the <a hr... | 46 | 2009-12-02T02:31:03Z | 34,063,195 | <p><strong><em>Edit: Scratch this for now, appears to be some problems with the installation still...</em></strong></p>
<p>I got rather tired of the whole situation, and just created a set of converted wheels ("wheel convert <.exe>"). I'll try and keep them maintained for the most recent build, but do shout if ther... | 0 | 2015-12-03T10:14:01Z | [
"python",
"winapi",
"virtualenv",
"pyinstaller"
] |
How can I use pywin32 with a virtualenv without having to include the host environment's site-packages folder? | 1,830,304 | <p>I'm working with <a href="http://www.pyinstaller.org">PyInstaller</a> under Python 2.6, which is only partially supported due to the mess MS have created with their manifest nonense which now affects Python since it is now MSVC8 compiled.</p>
<p>The problem is that the manifest embedding support relies on the <a hr... | 46 | 2009-12-02T02:31:03Z | 38,121,495 | <p>For Python 2.7 or 3.x use <strong><a href="https://pypi.python.org/pypi/pypiwin32">pypiwin32</a></strong>.</p>
<p><code>pip install pypiwin32</code></p>
| 5 | 2016-06-30T11:14:20Z | [
"python",
"winapi",
"virtualenv",
"pyinstaller"
] |
why Ghost Process appears after kill -9 | 1,830,370 | <p>In my Python script, I first launch a subprocess by subprocess.Popen(). Then later on, I want to kill that subprocess by <code>kill -9 Pid</code>.</p>
<p>What I found is that after the kill is executed, the subprocess is "stopped" because the GUI window of that process disappeared immediately. But when I perform a ... | 1 | 2009-12-02T02:49:27Z | 1,830,395 | <p>I think -9 signal lets the process to try to handle kill and spend some time housekeeping. You can try just kill the process without signal.</p>
<p>Edit: oh, its actually -15 signal, that lets process die gracefully. never mind.</p>
| 0 | 2009-12-02T02:59:01Z | [
"python",
"process",
"kill"
] |
why Ghost Process appears after kill -9 | 1,830,370 | <p>In my Python script, I first launch a subprocess by subprocess.Popen(). Then later on, I want to kill that subprocess by <code>kill -9 Pid</code>.</p>
<p>What I found is that after the kill is executed, the subprocess is "stopped" because the GUI window of that process disappeared immediately. But when I perform a ... | 1 | 2009-12-02T02:49:27Z | 1,830,397 | <p>From the manual page of ps:</p>
<blockquote>
<p>Z Defunct ("zombie") process,
terminated but not reaped by its
parent.</p>
</blockquote>
<p>That means that the parent didn't do a <code>waitpid()</code> for the child that died.</p>
<p>Apart from waitpid(), you can avoid that by using a <a href... | 4 | 2009-12-02T02:59:31Z | [
"python",
"process",
"kill"
] |
why Ghost Process appears after kill -9 | 1,830,370 | <p>In my Python script, I first launch a subprocess by subprocess.Popen(). Then later on, I want to kill that subprocess by <code>kill -9 Pid</code>.</p>
<p>What I found is that after the kill is executed, the subprocess is "stopped" because the GUI window of that process disappeared immediately. But when I perform a ... | 1 | 2009-12-02T02:49:27Z | 1,853,165 | <p>Zombie processes are actually just an entry in the process table. They do not run, they don't consume memory; the entry just stays because the parent hasn't checked their exit code.
You can either do a double fork as Gonzalo suggests, or you can filter out all ps lines with a Z in the S column.</p>
| 0 | 2009-12-05T19:24:41Z | [
"python",
"process",
"kill"
] |
python locale strange error. what's going on here exactly? | 1,830,394 | <p>So today I upgraded to bazaar 2.0.2, and I started receiving this message (I'm on snow leopard, btw):</p>
<pre><code>bzr: warning: unknown locale: UTF-8
Could not determine what text encoding to use.
This error usually means your Python interpreter
doesn't support the locale set by $LANG (en_US.UTF-8)
Conti... | 25 | 2009-12-02T02:57:27Z | 1,830,497 | <p>It's a Mac OS X problem. To see your locale settings, run <code>locale</code> in terminal. <code>locale -a</code> should list all locales that you have defined (that you may use as argument to LC_ALL).</p>
<p>Notice that <code>LC_ALL</code> and other <code>LC_*</code> variables take precedence over <code>LANG</code... | 4 | 2009-12-02T03:31:52Z | [
"python",
"osx",
"osx-snow-leopard",
"locale"
] |
python locale strange error. what's going on here exactly? | 1,830,394 | <p>So today I upgraded to bazaar 2.0.2, and I started receiving this message (I'm on snow leopard, btw):</p>
<pre><code>bzr: warning: unknown locale: UTF-8
Could not determine what text encoding to use.
This error usually means your Python interpreter
doesn't support the locale set by $LANG (en_US.UTF-8)
Conti... | 25 | 2009-12-02T02:57:27Z | 1,830,529 | <p>2016 UPDATE: Turns out that <a href="https://bugs.python.org/issue18378#msg215215" rel="nofollow">this is a Python bug</a> since at least 2013, very probably earlier too, consisting in Python not reacting well to non-GNU locales - like those found in Mac OS X and the BSDs. The bug is still open as of September 2016,... | 13 | 2009-12-02T03:44:31Z | [
"python",
"osx",
"osx-snow-leopard",
"locale"
] |
How to submit a form with more than 1 submit button. Sending a POST to a website. (Python) | 1,830,413 | <p>I am creating a script using <a href="http://wwwsearch.sourceforge.net/mechanize/#docs" rel="nofollow">Python Mechanize</a> that can login to a website and submit a form.
However, this form has 3 submit buttons (Preview, Post, and Cancel). I'm used to only one button...</p>
<p>This is the form:</p>
<pre><code><... | 0 | 2009-12-02T03:06:53Z | 1,830,590 | <p>problem solved.</p>
<p>Do not use python mechanize. It won't let you send other post data.</p>
<p>For some reason, the post data currently in the question is NOT sufficient. There are other stuff that needs to be sent to the server.</p>
<p>Therefore, you must use the regular opener/cookiejar method to send all t... | 0 | 2009-12-02T04:05:36Z | [
"python",
"http",
"url",
"urllib2",
"mechanize"
] |
How to submit a form with more than 1 submit button. Sending a POST to a website. (Python) | 1,830,413 | <p>I am creating a script using <a href="http://wwwsearch.sourceforge.net/mechanize/#docs" rel="nofollow">Python Mechanize</a> that can login to a website and submit a form.
However, this form has 3 submit buttons (Preview, Post, and Cancel). I'm used to only one button...</p>
<p>This is the form:</p>
<pre><code><... | 0 | 2009-12-02T03:06:53Z | 2,052,812 | <p>I had the same problem as you.</p>
<p>A form with two submit buttons, first was preview, second was submit.</p>
<p>At first, mechanize was using only the first button, I could see the server answer using</p>
<pre><code>response = browser.submit()
print response.read()
</code></pre>
<p>I put the submit button nam... | 2 | 2010-01-12T22:06:00Z | [
"python",
"http",
"url",
"urllib2",
"mechanize"
] |
How to cleanly loop over two files in parallel in Python | 1,830,552 | <p>I frequently write code like:</p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
i = 0
for line in lines:
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print words[i].strip(),
i += 1
</code></pre>
<p>Is it possible to avoid use variable i and make ... | 7 | 2009-12-02T03:53:53Z | 1,830,562 | <p>You can try to use enumerate, </p>
<p><a href="http://docs.python.org/tutorial/datastructures.html#looping-techniques">http://docs.python.org/tutorial/datastructures.html#looping-techniques</a></p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
for i,line in enu... | 14 | 2009-12-02T03:56:45Z | [
"python"
] |
How to cleanly loop over two files in parallel in Python | 1,830,552 | <p>I frequently write code like:</p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
i = 0
for line in lines:
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print words[i].strip(),
i += 1
</code></pre>
<p>Is it possible to avoid use variable i and make ... | 7 | 2009-12-02T03:53:53Z | 1,830,572 | <p>Take a look at <a href="http://docs.python.org/library/functions.html#enumerate" rel="nofollow">enumerate</a>:</p>
<pre><code>>>> for i, season in enumerate(['Spring', 'Summer', 'Fall', 'Winter']):
... print i, season
0 Spring
1 Summer
2 Fall
3 Winter
</code></pre>
| 1 | 2009-12-02T03:59:53Z | [
"python"
] |
How to cleanly loop over two files in parallel in Python | 1,830,552 | <p>I frequently write code like:</p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
i = 0
for line in lines:
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print words[i].strip(),
i += 1
</code></pre>
<p>Is it possible to avoid use variable i and make ... | 7 | 2009-12-02T03:53:53Z | 1,830,582 | <p>In general enumerate is a good solution. In this case, you could do something like:</p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
for word, line in zip(words, lines):
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print word.strip... | 5 | 2009-12-02T04:04:02Z | [
"python"
] |
How to cleanly loop over two files in parallel in Python | 1,830,552 | <p>I frequently write code like:</p>
<pre><code>lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
i = 0
for line in lines:
v = [eval(s) for s in line.split()]
if v[0] > v[1]:
print words[i].strip(),
i += 1
</code></pre>
<p>Is it possible to avoid use variable i and make ... | 7 | 2009-12-02T03:53:53Z | 1,830,663 | <p>It looks like you don't care what the value of <code>i</code> is. You just are using it as a way to pair up the <code>lines</code> and the <code>words</code>. Therefore, I recommend you read one line at a time, and at the same time read one word. Then they will match.</p>
<p>Also, when you use <code>.readlines()... | 15 | 2009-12-02T04:32:16Z | [
"python"
] |
how to find the owner of a file or directory in python | 1,830,618 | <p>I need a function or method in Python to find the owner of a file or directory.</p>
<p>The function should be like:</p>
<pre><code>>>> find_owner("/home/somedir/somefile")
owner3
</code></pre>
| 14 | 2009-12-02T04:19:36Z | 1,830,631 | <p>You want to use <a href="http://docs.python.org/library/os.html"><code>os.stat()</code></a>:</p>
<pre><code>os.stat(path)
Perform the equivalent of a stat() system call on the given path.
(This function follows symlinks; to stat a symlink use lstat().)
The return value is an object whose attributes correspond t... | 13 | 2009-12-02T04:25:33Z | [
"python",
"linux",
"file",
"permissions",
"ownership"
] |
how to find the owner of a file or directory in python | 1,830,618 | <p>I need a function or method in Python to find the owner of a file or directory.</p>
<p>The function should be like:</p>
<pre><code>>>> find_owner("/home/somedir/somefile")
owner3
</code></pre>
| 14 | 2009-12-02T04:19:36Z | 1,830,633 | <p>See <a href="http://docs.python.org/library/os.html#os.stat" rel="nofollow">os.stat</a>. It gives you <code>st_uid</code> which is the user ID of the owner. Then you have to convert it to the name. To do that, use <a href="http://docs.python.org/library/pwd.html#pwd.getpwuid" rel="nofollow">pwd.getpwuid</a>.</p>
| 3 | 2009-12-02T04:26:06Z | [
"python",
"linux",
"file",
"permissions",
"ownership"
] |
how to find the owner of a file or directory in python | 1,830,618 | <p>I need a function or method in Python to find the owner of a file or directory.</p>
<p>The function should be like:</p>
<pre><code>>>> find_owner("/home/somedir/somefile")
owner3
</code></pre>
| 14 | 2009-12-02T04:19:36Z | 1,830,635 | <p>I'm not really much of a python guy, but I was able to whip this up:</p>
<pre><code>from os import stat
from pwd import getpwuid
def find_owner(filename):
return getpwuid(stat(filename).st_uid).pw_name
</code></pre>
| 40 | 2009-12-02T04:27:09Z | [
"python",
"linux",
"file",
"permissions",
"ownership"
] |
how to find the owner of a file or directory in python | 1,830,618 | <p>I need a function or method in Python to find the owner of a file or directory.</p>
<p>The function should be like:</p>
<pre><code>>>> find_owner("/home/somedir/somefile")
owner3
</code></pre>
| 14 | 2009-12-02T04:19:36Z | 1,830,644 | <p>Here is some example code, showing how you can find the owner of file:</p>
<pre><code>#!/usr/bin/env python
import os
import pwd
filename = '/etc/passwd'
st = os.stat(filename)
uid = st.st_uid
print(uid)
# output: 0
userinfo = pwd.getpwuid(st.st_uid)
print(userinfo)
# output: pwd.struct_passwd(pw_name='root', pw_pa... | 2 | 2009-12-02T04:29:04Z | [
"python",
"linux",
"file",
"permissions",
"ownership"
] |
how to find the owner of a file or directory in python | 1,830,618 | <p>I need a function or method in Python to find the owner of a file or directory.</p>
<p>The function should be like:</p>
<pre><code>>>> find_owner("/home/somedir/somefile")
owner3
</code></pre>
| 14 | 2009-12-02T04:19:36Z | 38,799,857 | <p>I stumbled across this recently, looking to get owner user <em>and group</em> information, so I thought I'd share what I came up with:</p>
<pre><code>import os
from pwd import getpwuid
from grp import getgrgid
def get_file_ownership(filename):
return (
getpwuid(os.stat(filename).st_uid).pw_name,
... | 1 | 2016-08-06T01:51:35Z | [
"python",
"linux",
"file",
"permissions",
"ownership"
] |
Uncompress Zlib string in using ByteArrays | 1,830,665 | <p>I have a web application developed in Adobe Flex 3 and Python 2.5 (deployed on Google App Engine). A RESTful web service has been created in Python and its results are currently in an XML format which is being read by Flex using the HttpService object.</p>
<p>Now the main objective is to compress the XML so that th... | 4 | 2009-12-02T04:32:42Z | 1,830,709 | <p>What is <code>byteArray.writeUTF( event.result.toString() );</code> supposed to do? The result of zlib.compress() is neither unicode nor "UTF" (meaningless without a number after it!?); it is binary aka raw bytes; you should neither decode it nor encode it nor apply any other transformation to it. The receiver shoul... | 2 | 2009-12-02T04:47:49Z | [
"python",
"flex",
"actionscript-3",
"compression",
"zlib"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.