title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Python: Locks from `threading` and `multiprocessing` interchangable? | 739,687 | <p>Are the locks from the <code>threading</code> module interchangeable with those from the <code>multiprocessing</code> module?</p>
| 5 | 2009-04-11T07:37:24Z | 739,705 | <p>Yes, you can use locks from the multiprocessing module as normal in your one-process application, but if you're using multiprocessing, you should use its locks.</p>
| 1 | 2009-04-11T07:48:01Z | [
"python",
"multithreading",
"locking",
"multiprocessing"
] |
Python: Locks from `threading` and `multiprocessing` interchangable? | 739,687 | <p>Are the locks from the <code>threading</code> module interchangeable with those from the <code>multiprocessing</code> module?</p>
| 5 | 2009-04-11T07:37:24Z | 740,197 | <p>You can typically use the two interchangeably, but you need to cognizant of the differences. For example, multiprocessing.Event is backed by a named semaphore, which is sensitive to the platform under the application. </p>
<p>Multiprocessing.Lock is backed by Multiprocessing.SemLock - so it needs named semaphores. ... | 7 | 2009-04-11T14:34:57Z | [
"python",
"multithreading",
"locking",
"multiprocessing"
] |
Iterating over object instances of a given class in Python | 739,882 | <p>Given a class that keeps a registry of its Objects:</p>
<pre><code>class Person(object):
__registry = []
def __init__(self, name):
self.__registry.append(self)
self.name = name
</code></pre>
<p>How would I make the following code work (without using Person.__registry):</p>
<pre><code>for pers... | 12 | 2009-04-11T11:13:09Z | 739,896 | <p>you can do it with:</p>
<pre><code>for item in Person.__registry:
print(item)
</code></pre>
| 2 | 2009-04-11T11:24:13Z | [
"python",
"oop"
] |
Iterating over object instances of a given class in Python | 739,882 | <p>Given a class that keeps a registry of its Objects:</p>
<pre><code>class Person(object):
__registry = []
def __init__(self, name):
self.__registry.append(self)
self.name = name
</code></pre>
<p>How would I make the following code work (without using Person.__registry):</p>
<pre><code>for pers... | 12 | 2009-04-11T11:13:09Z | 739,936 | <p>First, do not use double <code>__</code> names. They're reserved for use by Python. If you want "private" use single <code>_</code>.</p>
<p>Second, keep this kind of thing as simple as possible. Don't waste a lot of time and energy on something complex. This is a simple problem, keep the code as simple as possi... | 7 | 2009-04-11T11:57:21Z | [
"python",
"oop"
] |
Iterating over object instances of a given class in Python | 739,882 | <p>Given a class that keeps a registry of its Objects:</p>
<pre><code>class Person(object):
__registry = []
def __init__(self, name):
self.__registry.append(self)
self.name = name
</code></pre>
<p>How would I make the following code work (without using Person.__registry):</p>
<pre><code>for pers... | 12 | 2009-04-11T11:13:09Z | 739,954 | <p>You can make your class object iterable with a simple metaclass.</p>
<pre><code>class IterRegistry(type):
def __iter__(cls):
return iter(cls._registry)
class Person(object):
__metaclass__ = IterRegistry
_registry = []
def __init__(self, name):
self._registry.append(self)
se... | 18 | 2009-04-11T12:10:20Z | [
"python",
"oop"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 740,000 | <p>From the shell</p>
<pre><code>ls site-packages
</code></pre>
<p>If that's not helpful, you can do this.</p>
<pre><code>import sys
import os
for p in sys.path:
print os.listdir( p )
</code></pre>
<p>And see what that produces.</p>
| -8 | 2009-04-11T12:38:07Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 740,018 | <pre><code>help('modules')
</code></pre>
<p>in a Python shell/prompt.</p>
| 631 | 2009-04-11T12:45:45Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 4,703,670 | <p>Now, these methods I tried myself, and I got exactly what was advertised: All the modules.</p>
<p>Alas, really you don't care much about the stdlib, you know what you get with a python install. </p>
<p>Really, I want the stuff that <em>I</em> installed. </p>
<p>What actually, surprisingly, worked just fine was:... | 173 | 2011-01-16T03:42:15Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 9,194,180 | <ul>
<li><p>In <a href="http://ipython.org/"><code>ipython</code></a> you can type "<code>import</code><kbd>Tab</kbd>".</p></li>
<li><p>In the standard Python interpreter, you can type "<code>help('modules')</code>".</p></li>
<li><p>At the command-line, you can use <a href="http://docs.python.org/library/pydoc.html"><c... | 59 | 2012-02-08T13:24:27Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 10,563,781 | <p>Very simple searching using <a href="http://docs.python.org/library/pkgutil.html#pkgutil.iter_modules">pkgutil.iter_modules</a></p>
<pre><code>from pkgutil import iter_modules
a=iter_modules()
while True:
try: x=a.next()
except: break
if 'searchstr' in x[1]: print x[1]
</code></pre>
| 9 | 2012-05-12T12:34:44Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 11,164,710 | <p>I just use this to see currently used modules:</p>
<pre><code>import sys as s
s.modules.keys()
</code></pre>
<p>which shows all modules running on your python.</p>
<p>For all built-in modules use:</p>
<pre><code>s.modules
</code></pre>
<p>Which is a dict containing all modules and import objects.</p>
| 31 | 2012-06-22T22:02:06Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 12,107,636 | <p>I ran into a custom installed python 2.7 on OS X. It required X11 to list modules installed (both using help and pydoc).</p>
<p>To be able to list all modules without installing X11 I ran pydoc as http-server, i.e.:</p>
<pre><code>pydoc -p 12345
</code></pre>
<p>Then it's possible to direct Safari to <code>http:/... | 10 | 2012-08-24T10:28:05Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 14,529,730 | <p>Aside from using <code>pip freeze</code> I have been installing <a href="http://pypi.python.org/pypi/yolk" rel="nofollow">yolk</a> in my virtual environments.</p>
| 5 | 2013-01-25T20:20:06Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 17,870,160 | <p>Since pip version 1.3, you've got access to:</p>
<pre><code>pip list
</code></pre>
<p>Which seems to be syntactic sugar for "pip freeze". It will list all of the modules particular to your installation or virtualenv, along with their version numbers. Unfortunately it does not display the current version number o... | 38 | 2013-07-25T22:56:32Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 18,399,722 | <p>In normal shell just use</p>
<pre><code>pydoc modules
</code></pre>
| 41 | 2013-08-23T09:45:31Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 20,413,093 | <p>In case you have an <a href="https://store.continuum.io/cshop/anaconda/" rel="nofollow">anaconda python distribution</a> installed, you could also use</p>
<pre><code>$conda list
</code></pre>
<p>in addition to solutions described above.</p>
| 3 | 2013-12-05T23:38:03Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 20,879,819 | <p>If we need to know list of installed packages in python , we can use 'help' command like given below (in python shell)</p>
<pre><code>>>help('modules package')
</code></pre>
| 9 | 2014-01-02T09:26:40Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 21,250,992 | <ol>
<li>to get all available modules, run <code>sys.modules</code></li>
<li>to get all <em>installed</em> modules (read: installed by <code>pip</code>), you may look at <code>pip.get_installed_distributions()</code></li>
</ol>
<p>For the second purpose, example code:</p>
<pre><code>import pip
for package in pip.get_... | 3 | 2014-01-21T06:42:12Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 23,885,252 | <h2>Solution</h2>
<p>My 50 cents for getting a <code>pip freeze</code>-like list from a Python script:</p>
<pre class="lang-python prettyprint-override"><code>import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packa... | 260 | 2014-05-27T09:05:42Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 34,224,653 | <p>on windows, Enter this in cmd</p>
<pre><code>c:\python\libs>python -m pip freeze
</code></pre>
| 1 | 2015-12-11T13:26:27Z | [
"python",
"module",
"pip"
] |
How can I get a list of locally installed Python modules? | 739,993 | <p>I would like to get a list of Python modules, which are in my Python installation (UNIX server).</p>
<p><strong>How can you get a list of Python modules installed in your computer?</strong></p>
| 533 | 2009-04-11T12:34:18Z | 36,767,383 | <p><strong>pip freeze</strong> does it all finding packages however one can simply write the following command to list all paths where python packages are.</p>
<pre><code>>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
</code></pre>
| 2 | 2016-04-21T10:52:02Z | [
"python",
"module",
"pip"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,291 | <p>Think about what the code actually says!</p>
<pre><code>>>> (1 or 2)
1
>>> (2 or 1)
2
</code></pre>
<p>That should probably explain it. :) Python apparently implements "lazy or", which should come as no surprise. It performs it something like this:</p>
<pre><code>def or(x, y):
if x: return... | 15 | 2009-04-11T15:16:29Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,294 | <pre><code>>>> L1 = [2,3,4]
>>> L2 = [1,2]
>>> [i for i in L1 if i in L2]
[2]
>>> S1 = set(L1)
>>> S2 = set(L2)
>>> S1.intersection(S2)
set([2])
</code></pre>
<p>Both empty lists and empty sets are False, so you can use the value directly as a truth value.</p>
| 119 | 2009-04-11T15:22:07Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,297 | <p>Best I could come up with:</p>
<pre><code>any([True for e in (1, 2) if e in a])
</code></pre>
| 3 | 2009-04-11T15:24:02Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,300 | <p>This will do it in one line.</p>
<pre><code>>>> a=[2,3,4]
>>> b=[1,2]
>>> bool(sum(map(lambda x: x in b, a)))
True
</code></pre>
| 1 | 2009-04-11T15:26:36Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,309 | <p>In some cases (e.g. unique list elements), set operations can be used.</p>
<pre><code>>>> a=[2,3,4]
>>> set(a) - set([2,3]) != set(a)
True
>>>
</code></pre>
<p>Or, using <a href="http://docs.python.org/library/stdtypes.html#set.isdisjoint" rel="nofollow">set.isdisjoint()</a>,</p>
<pre><... | 3 | 2009-04-11T15:32:41Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,359 | <p>Maybe a bit more lazy:</p>
<pre><code>a = [1,2,3,4]
b = [2,7]
print any((True for x in a if x in b))
</code></pre>
| 12 | 2009-04-11T16:12:42Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 740,384 | <p>Ah, Tobias you beat me to it. I was thinking of this slight variation on your solution:</p>
<pre><code>print any(x in a for x in b)
</code></pre>
| 103 | 2009-04-11T16:30:38Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 15,710,833 | <pre><code>print (1 in a) or (2 in a)
print (2 in a) or (5 in a)
</code></pre>
<p>This is a very old question, but I wasn't happy with any of the answers, so I had to add this for posterity's sake.</p>
| 0 | 2013-03-29T20:21:33Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 21,248,370 | <p>When you think "check to see if a in b", think hashes (in this case, sets). The fastest way is to hash the list you want to check, and then check each item in there.</p>
<p>This is why Joe Koberg's answer is fast: checking set intersection is very fast.</p>
<p>When you don't have a lot of data though, making sets ... | 1 | 2014-01-21T03:07:07Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 22,682,523 | <pre><code>a = {2,3,4}
if {1,2} & a:
pass
</code></pre>
<p>Code golf version. Consider using a set if it makes sense to do so.
I find this more readable than a list comprehension.</p>
| 7 | 2014-03-27T08:56:02Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 24,399,987 | <p>1 line without list comprehensions.</p>
<pre><code>>>> any(map(lambda each: each in [2,3,4], [1,2]))
True
>>> any(map(lambda each: each in [2,3,4], [1,5]))
False
>>> any(map(lambda each: each in [2,3,4], [2,4]))
True
</code></pre>
| 5 | 2014-06-25T04:02:23Z | [
"python"
] |
How to check if one of the following items is in a list? | 740,287 | <p>I'm trying to find a short way to see if any of the following items is in a list, but my first attempt does not work. Besides writing a function to accomplish this, is the any short way to check if one of multiple items is in a list.</p>
<pre><code>>>> a = [2,3,4]
>>> print (1 or 2) in a
False
>... | 104 | 2009-04-11T15:13:07Z | 30,169,646 | <p>Simple.</p>
<pre><code>_new_list = []
for item in a:
if item in b:
_new_list.append(item)
else:
pass
</code></pre>
| -2 | 2015-05-11T13:58:00Z | [
"python"
] |
SQLAlchemy many-to-many orphan deletion | 740,630 | <p>I'm trying to use SQLAlchemy to implement a basic users-groups model where users can have multiple groups and groups can have multiple users.</p>
<p>When a group becomes empty, I want the group to be deleted, (along with other things associated with the group. Fortunately, SQLAlchemy's cascade works fine with thes... | 8 | 2009-04-11T19:07:17Z | 763,256 | <p>The way I've generally handled this is to have a function on your user or group called leave_group. When you want a user to leave a group, you call that function, and you can add any side effects you want into there. In the long term, this makes it easier to add more and more side effects. (For example when you w... | 3 | 2009-04-18T10:29:31Z | [
"python",
"sqlalchemy"
] |
SQLAlchemy many-to-many orphan deletion | 740,630 | <p>I'm trying to use SQLAlchemy to implement a basic users-groups model where users can have multiple groups and groups can have multiple users.</p>
<p>When a group becomes empty, I want the group to be deleted, (along with other things associated with the group. Fortunately, SQLAlchemy's cascade works fine with thes... | 8 | 2009-04-11T19:07:17Z | 770,287 | <p>I think you want <code>cascade='save, update, merge, expunge, refresh, delete-orphan'</code>. This will prevent the "delete" cascade (which you get from "all") but maintain the "delete-orphan", which is what you're looking for, I think (delete when there are no more parents).</p>
| 3 | 2009-04-20T22:00:12Z | [
"python",
"sqlalchemy"
] |
SQLAlchemy many-to-many orphan deletion | 740,630 | <p>I'm trying to use SQLAlchemy to implement a basic users-groups model where users can have multiple groups and groups can have multiple users.</p>
<p>When a group becomes empty, I want the group to be deleted, (along with other things associated with the group. Fortunately, SQLAlchemy's cascade works fine with thes... | 8 | 2009-04-11T19:07:17Z | 776,246 | <p>Could you post a sample of your table and mapper set up? It might be easier to spot what is going on.</p>
<p>Without seeing the code it is hard to tell, but perhaps there is something wrong with the direction of the relationship?</p>
| 0 | 2009-04-22T08:48:37Z | [
"python",
"sqlalchemy"
] |
SQLAlchemy many-to-many orphan deletion | 740,630 | <p>I'm trying to use SQLAlchemy to implement a basic users-groups model where users can have multiple groups and groups can have multiple users.</p>
<p>When a group becomes empty, I want the group to be deleted, (along with other things associated with the group. Fortunately, SQLAlchemy's cascade works fine with thes... | 8 | 2009-04-11T19:07:17Z | 803,584 | <p>I had the same problem about 3 months ago, i have a Post/Tags relation and wanted to delete unused Tags. I asked on irc and SA's author told me that cascades on many-to-many relations are not supported, which kind of makes sense since there is no "parent" in many-to-many.</p>
<p>But extending SA is easy, you can pr... | 2 | 2009-04-29T18:15:16Z | [
"python",
"sqlalchemy"
] |
Dynamic processes in Python | 740,717 | <p>I have a question concerning Python multiprocessing. I am trying to take a dataset, break into chunks, and pass those chunks to concurrently running processes. I need to transform large tables of data using simple calculations (eg. electrical resistance -> temperature for a thermistor).</p>
<p>The code listed below... | 2 | 2009-04-11T20:15:47Z | 740,759 | <p>You haven't overridden the <code>run</code> method. There are two ways with processes (or threads) to have it execute code:</p>
<ol>
<li>Create a process specifying target</li>
<li>Subclass the process, overriding the <code>run</code> method.</li>
</ol>
<p>Overriding <code>__init__</code> just means your process ... | 1 | 2009-04-11T20:53:34Z | [
"python",
"multithreading",
"multiprocessing"
] |
Dynamic processes in Python | 740,717 | <p>I have a question concerning Python multiprocessing. I am trying to take a dataset, break into chunks, and pass those chunks to concurrently running processes. I need to transform large tables of data using simple calculations (eg. electrical resistance -> temperature for a thermistor).</p>
<p>The code listed below... | 2 | 2009-04-11T20:15:47Z | 743,032 | <p>Ok, so it looks like the list was not thread safe, and I have moved to using a Queue (although it appears to be much slower). This code essentially accomplishes what I was trying to do:</p>
<pre><code>import math, multiprocessing
class Worker(multiprocessing.Process):
def process(self, x):
for i in ra... | 0 | 2009-04-13T04:25:52Z | [
"python",
"multithreading",
"multiprocessing"
] |
Dynamic processes in Python | 740,717 | <p>I have a question concerning Python multiprocessing. I am trying to take a dataset, break into chunks, and pass those chunks to concurrently running processes. I need to transform large tables of data using simple calculations (eg. electrical resistance -> temperature for a thermistor).</p>
<p>The code listed below... | 2 | 2009-04-11T20:15:47Z | 743,104 | <p>No need to send the number of chunks to each process, just use get_nowait() and handle the eventual Queue.Empty exception. Every process will get different amounts of CPU time and this should keep them all busy.</p>
<pre><code>import multiprocessing, Queue
class Worker(multiprocessing.Process):
def process(sel... | 1 | 2009-04-13T05:20:54Z | [
"python",
"multithreading",
"multiprocessing"
] |
python write string directly to tarfile | 740,820 | <p>Is there a way to write a string directly to a tarfile? From <a href="http://docs.python.org/library/tarfile.html">http://docs.python.org/library/tarfile.html</a> it looks like only files already written to the file system can be added.</p>
| 21 | 2009-04-11T21:41:08Z | 740,839 | <p>I would say it's possible, by playing with TarInfo e TarFile.addfile passing a StringIO as a fileobject. </p>
<p>Very rough, but works</p>
<pre><code>import tarfile
import StringIO
tar = tarfile.TarFile("test.tar","w")
string = StringIO.StringIO()
string.write("hello")
string.seek(0)
info = tarfile.TarInfo(name=... | 24 | 2009-04-11T21:48:26Z | [
"python",
"file",
"file-io",
"tar"
] |
python write string directly to tarfile | 740,820 | <p>Is there a way to write a string directly to a tarfile? From <a href="http://docs.python.org/library/tarfile.html">http://docs.python.org/library/tarfile.html</a> it looks like only files already written to the file system can be added.</p>
| 21 | 2009-04-11T21:41:08Z | 740,854 | <p>As Stefano pointed out, you can use <code>TarFile.addfile</code> and <code>StringIO</code>.</p>
<pre><code>import tarfile, StringIO
data = 'hello, world!'
tarinfo = tarfile.TarInfo('test.txt')
tarinfo.size = len(data)
tar = tarfile.open('test.tar', 'a')
tar.addfile(tarinfo, StringIO.StringIO(data))
tar.close()
<... | 9 | 2009-04-11T22:02:46Z | [
"python",
"file",
"file-io",
"tar"
] |
python write string directly to tarfile | 740,820 | <p>Is there a way to write a string directly to a tarfile? From <a href="http://docs.python.org/library/tarfile.html">http://docs.python.org/library/tarfile.html</a> it looks like only files already written to the file system can be added.</p>
| 21 | 2009-04-11T21:41:08Z | 740,856 | <p>You have to use TarInfo objects and the addfile method instead of the usual add method:</p>
<pre><code>from StringIO import StringIO
from tarfile import open, TarInfo
s = "Hello World!"
ti = TarInfo("test.txt")
ti.size = len(s)
tf = open("testtar.tar", "w")
tf.addfile(ti, StringIO(s))
</code></pre>
| 1 | 2009-04-11T22:04:37Z | [
"python",
"file",
"file-io",
"tar"
] |
python write string directly to tarfile | 740,820 | <p>Is there a way to write a string directly to a tarfile? From <a href="http://docs.python.org/library/tarfile.html">http://docs.python.org/library/tarfile.html</a> it looks like only files already written to the file system can be added.</p>
| 21 | 2009-04-11T21:41:08Z | 18,031,786 | <p>In my case I wanted to read from an existing tar file, append some data to the contents, and write it to a new file. Something like:</p>
<pre><code>for ti in tar_in:
buf_in = tar.extractfile(ti)
buf_out = io.BytesIO()
size = buf_out.write(buf_in.read())
size += buf_out.write(other data)
buf_out.... | 2 | 2013-08-03T10:21:11Z | [
"python",
"file",
"file-io",
"tar"
] |
python write string directly to tarfile | 740,820 | <p>Is there a way to write a string directly to a tarfile? From <a href="http://docs.python.org/library/tarfile.html">http://docs.python.org/library/tarfile.html</a> it looks like only files already written to the file system can be added.</p>
| 21 | 2009-04-11T21:41:08Z | 25,157,700 | <p>Just for the record: <br>
StringIO objects have a .len property.<br>
No need to seek(0) and do len(foo.buf)<br>
No need to keep the entire string around to do len() on, or God forbid, do the accounting yourself.</p>
<p>( Maybe it did not at the time the OP was written. )</p>
| 2 | 2014-08-06T10:12:30Z | [
"python",
"file",
"file-io",
"tar"
] |
Python multiprocessing: Pool of custom Processes | 740,844 | <p>I am subclassing the Process class, into a class I call EdgeRenderer. I want to use <code>multiprocessing.Pool</code>, except instead of regular Processes, I want them to be instances of my EdgeRenderer. Possible? How?</p>
| 4 | 2009-04-11T21:53:55Z | 741,123 | <p>I don't see any hook for it in the API. You might be able to get away with replicating your desired functionality by using <code>initializer</code> and <code>initargs</code> argument. Alternately, you can build the functionality into the callable object that you use for mapping:</p>
<pre><code>class EdgeRenderTas... | 2 | 2009-04-12T01:26:06Z | [
"python",
"multiprocessing",
"pool"
] |
Python multiprocessing: Pool of custom Processes | 740,844 | <p>I am subclassing the Process class, into a class I call EdgeRenderer. I want to use <code>multiprocessing.Pool</code>, except instead of regular Processes, I want them to be instances of my EdgeRenderer. Possible? How?</p>
| 4 | 2009-04-11T21:53:55Z | 741,852 | <p>From Jesse Noller:</p>
<blockquote>
<p>It is not currently supported in the
API, but would not be a bad addition.
I'll look at adding it to
python2.7/2.6.3 3.1 this week</p>
</blockquote>
| 2 | 2009-04-12T14:24:46Z | [
"python",
"multiprocessing",
"pool"
] |
Can InstantDjango be Used Rather than the Normal Installation | 740,929 | <p>Is it possible to do development just using Instant Django? Do I need to have the normal version working or can I just use this instant version? Has anyone used it?</p>
| 3 | 2009-04-11T22:48:33Z | 740,950 | <p>It is, of course, possible to use InstantDjango for development. InstantDjango uses SQLite3, which is a perfectly reasonable relational database for embedded or light/sometimes-moderate use. The whole purpose of django is that the ORM layer gives you database portability.</p>
<p>That said, I would not use InstantDj... | 4 | 2009-04-11T23:02:47Z | [
"python",
"django",
"instant"
] |
Django: Adding additional properties to Model Class Object | 741,270 | <p>This is using Google App Engine. I am not sure if this is applicable to just normal Django development or if Google App Engine will play a part. If it does, would you let me know so I can update the description of this problem.</p>
<pre><code>class MessageModel(db.Model):
to_user_id = db.IntegerProperty()
t... | 1 | 2009-04-12T04:36:51Z | 741,444 | <p>You should still be able to send it messagesSQL to the template after you've added elements to it via the for loop. Python allows that sort of thing.</p>
<p>Something else that might make sense in some cases would be to give your MessageModel methods. For instance, if you have a </p>
<pre><code>def since_date_cr... | 5 | 2009-04-12T08:10:08Z | [
"python",
"django",
"google-app-engine"
] |
Django: Adding additional properties to Model Class Object | 741,270 | <p>This is using Google App Engine. I am not sure if this is applicable to just normal Django development or if Google App Engine will play a part. If it does, would you let me know so I can update the description of this problem.</p>
<pre><code>class MessageModel(db.Model):
to_user_id = db.IntegerProperty()
t... | 1 | 2009-04-12T04:36:51Z | 741,448 | <p>You can obtain that by defining methods in the model
like</p>
<pre><code>class MessageModel(db.Model):
# Definition
def since_date_created(self):
# ...
</code></pre>
<p>Now in the template, you can use it like</p>
<pre><code>Time since created {{ message.since_date_created }}
</code></pre>
| 4 | 2009-04-12T08:10:59Z | [
"python",
"django",
"google-app-engine"
] |
How do I tell matplotlib that I am done with a plot? | 741,877 | <p>The following code plots to two <a href="http://en.wikipedia.org/wiki/PostScript">PostScript</a> (.ps) files, but the second one contains both lines.</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10,... | 92 | 2009-04-12T14:40:55Z | 741,884 | <p>You can use <code>figure</code> to create a new plot, for example, or use <code>close</code> after the first plot.</p>
| 81 | 2009-04-12T14:43:47Z | [
"python",
"matplotlib",
"plot"
] |
How do I tell matplotlib that I am done with a plot? | 741,877 | <p>The following code plots to two <a href="http://en.wikipedia.org/wiki/PostScript">PostScript</a> (.ps) files, but the second one contains both lines.</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10,... | 92 | 2009-04-12T14:40:55Z | 742,062 | <p>There is a clear figure command, and it should do it for you:</p>
<pre><code>plt.clf()
</code></pre>
<p>If you have multiple subplots in the same figure</p>
<pre><code>plt.cla()
</code></pre>
<p>clears the current axes.</p>
| 109 | 2009-04-12T17:08:30Z | [
"python",
"matplotlib",
"plot"
] |
How do I tell matplotlib that I am done with a plot? | 741,877 | <p>The following code plots to two <a href="http://en.wikipedia.org/wiki/PostScript">PostScript</a> (.ps) files, but the second one contains both lines.</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10,... | 92 | 2009-04-12T14:40:55Z | 742,489 | <p>As stated from David Cournapeau, use figure().</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.figure()
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")
plt.figure()
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, bas... | 20 | 2009-04-12T21:44:36Z | [
"python",
"matplotlib",
"plot"
] |
How do I tell matplotlib that I am done with a plot? | 741,877 | <p>The following code plots to two <a href="http://en.wikipedia.org/wiki/PostScript">PostScript</a> (.ps) files, but the second one contains both lines.</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10,... | 92 | 2009-04-12T14:40:55Z | 9,820,820 | <p>Just enter <code>plt.hold(False)</code> before the first plt.plot, and you can stick to your original code.</p>
| 11 | 2012-03-22T10:52:09Z | [
"python",
"matplotlib",
"plot"
] |
How do I tell matplotlib that I am done with a plot? | 741,877 | <p>The following code plots to two <a href="http://en.wikipedia.org/wiki/PostScript">PostScript</a> (.ps) files, but the second one contains both lines.</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
plt.subplot(111)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10,... | 92 | 2009-04-12T14:40:55Z | 38,976,379 | <p>If you're using matplotlib interactively for example in a web application (e.g. ipython) you maybe looking for</p>
<pre><code>plt.show()
</code></pre>
<p>Instead of <code>plt.close()</code> or <code>plt.clf()</code></p>
| 1 | 2016-08-16T13:33:22Z | [
"python",
"matplotlib",
"plot"
] |
Programatically determining amount of parameters a function requires - Python | 741,950 | <p>I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I pla... | 30 | 2009-04-12T15:43:34Z | 741,957 | <p><a href="http://docs.python.org/library/inspect.html#inspect.getargspec">inspect.getargspec()</a>:</p>
<blockquote>
<p>Get the names and default values of a functionâs arguments. A tuple of four things is returned: (args, varargs, varkw, defaults). args is a list of the argument names (it may contain nested lis... | 34 | 2009-04-12T15:48:52Z | [
"python",
"parameters",
"function"
] |
Programatically determining amount of parameters a function requires - Python | 741,950 | <p>I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I pla... | 30 | 2009-04-12T15:43:34Z | 741,961 | <p>What you want is in general not possible, because of the use of varargs and kwargs, but <code>inspect.getargspec</code> (Python 2.x) and <code>inspect.getfullargspec</code> (Python 3.x) come close.</p>
<ul>
<li><p>Python 2.x:</p>
<pre><code>>>> import inspect
>>> def add(a, b=0):
... return a... | 13 | 2009-04-12T15:53:40Z | [
"python",
"parameters",
"function"
] |
Programatically determining amount of parameters a function requires - Python | 741,950 | <p>I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I pla... | 30 | 2009-04-12T15:43:34Z | 742,452 | <p>Make each command a class, derived from an abstract base defining the general structure of a command. As much as possible, the definition of command properties should be put into class variables with methods defined in the base class handling that data.</p>
<p>Register each of these subclasses with a factory class.... | 1 | 2009-04-12T21:20:51Z | [
"python",
"parameters",
"function"
] |
Programatically determining amount of parameters a function requires - Python | 741,950 | <p>I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I pla... | 30 | 2009-04-12T15:43:34Z | 12,405,945 | <p>Excellent question. I just had the problem that I wanted to write a function that takes a callback argument. Depending on the number of arguments of that callback, it needs to be called differently.</p>
<p>I started with gimel's answer, then expanded to be able to deal with builtins which don't react well with the ... | 0 | 2012-09-13T12:10:16Z | [
"python",
"parameters",
"function"
] |
Programatically determining amount of parameters a function requires - Python | 741,950 | <p>I was creating a simple command line utility and using a dictionary as a sort of case statement with key words linking to their apropriate function. The functions all have different amount of arguments required so currently to check if the user entered the correct amount of arguments needed for each function I pla... | 30 | 2009-04-12T15:43:34Z | 38,776,881 | <p>This has already been answered but without the inspect module you can also use <code>someMethod.func_code.co_argcount</code></p>
| 0 | 2016-08-04T20:28:28Z | [
"python",
"parameters",
"function"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 742,383 | <p>This is a well-documented behaviour in Python, that you aren't supposed to modify the list being iterated through. Try this instead:</p>
<pre><code>for i in x[:]:
x.remove(i)
</code></pre>
<p>The <code>[:]</code> returns a "slice" of <code>x</code>, which happens to contain all its elements, and is thus effect... | 28 | 2009-04-12T20:36:58Z | [
"python",
"list",
"foreach"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 742,386 | <p>When you delete an element, and the for-loop incs to the next index, you then skip an element.</p>
<p>Do it backwards. Or please state your real problem.</p>
| 10 | 2009-04-12T20:37:18Z | [
"python",
"list",
"foreach"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 742,388 | <p>Why don't you just use:</p>
<pre><code>x = []
</code></pre>
<p>It's probably because you're changing the same array that you're iterating over.</p>
<p>Try Chris-Jester Young's answer if you want to clear the array your way.</p>
| 3 | 2009-04-12T20:38:05Z | [
"python",
"list",
"foreach"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 742,906 | <p>I think, broadly speaking, that when you write:</p>
<pre><code>for x in lst:
# loop body goes here
</code></pre>
<p>under the hood, python is doing something like this:</p>
<pre><code>i = 0
while i < len(lst):
x = lst[i]
# loop body goes here
i += 1
</code></pre>
<p>If you insert <code>lst.rem... | 4 | 2009-04-13T02:43:03Z | [
"python",
"list",
"foreach"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 1,693,733 | <p>I agree with John Fouhy regarding the break condition. Traversing a copy of the list works for the remove() method, as Chris Jester-Young suggested. But if one needs to pop() specific items, then iterating in reverse works, as Erik mentioned, in which case the operation can be done in place. For example:</p>
<pre><... | 1 | 2009-11-07T17:12:18Z | [
"python",
"list",
"foreach"
] |
Python strange behavior in for loop or lists | 742,371 | <p>I'm currently developing a program in python and I just noticed that something was wrong with the foreach loop in the language, or maybe the list structure. I'll just give a generic example of my problem to simplify, since I get the same erroneous behavior on both my program and my generic example:</p>
<pre><code>x... | 7 | 2009-04-12T20:30:22Z | 38,798,324 | <p>I know this is an old post with an accepted answer but for those that may still come along...</p>
<p>A few previous answers have indicated it's a bad idea to change an iterable during iteration. But as a way to highlight what is happening...</p>
<pre><code>>>> x=[1,2,3,4,5]
>>> for i in x:
... ... | 0 | 2016-08-05T22:01:31Z | [
"python",
"list",
"foreach"
] |
Encapsulation vs. inheritance, help making a choice | 742,376 | <p>I need to write handlers for several different case types (in Python). The interface for all this types are the same, but the handling logic is different. </p>
<p>One option would be defining a common class that receives the particular handler type as one of the __init__ parameters:</p>
<pre><code>class Handler:
... | 1 | 2009-04-12T20:32:47Z | 742,405 | <p>I might be missing some subtle intricacy in your question, but given your first example, what precludes you from doing something like this:</p>
<pre><code>class HandlerCase1(object):
def handle_stuff(self, *args, **kwargs):
print "Handling case 1"
class HandlerCase2(object):
def handle_stuff(self,... | 4 | 2009-04-12T20:50:25Z | [
"python",
"design-patterns",
"inheritance",
"abstract-class"
] |
Code works in global scope but not local scope? | 742,496 | <p>This function should be returning 36 but it returns 0. If I run through the logic line by line in interactive mode I get 36.</p>
<p>Code</p>
<pre><code>from math import *
line = ((2, 5), (4, -1))
point = (6, 11)
def cross(line, point):
#reference: http://www.topcoder.com/tc?module=Static&d1=tutorials&... | 0 | 2009-04-12T21:50:15Z | 742,509 | <p>You have ab and ac pointing to the same reference. Change this:</p>
<pre><code>ab = ac = [None, None]
</code></pre>
<p>to this:</p>
<pre><code>ab = [None, None]
ac = [None, None]
</code></pre>
| 5 | 2009-04-12T21:57:27Z | [
"python"
] |
Code works in global scope but not local scope? | 742,496 | <p>This function should be returning 36 but it returns 0. If I run through the logic line by line in interactive mode I get 36.</p>
<p>Code</p>
<pre><code>from math import *
line = ((2, 5), (4, -1))
point = (6, 11)
def cross(line, point):
#reference: http://www.topcoder.com/tc?module=Static&d1=tutorials&... | 0 | 2009-04-12T21:50:15Z | 742,510 | <p>In the line <code>ab = ac = [None, None]</code>, you assign the <strong>same list</strong> to the variables ab and ac. When you change one, you change the other at the same time.</p>
<p>The reason it works interactively, is that you don't init the lists in the same way.</p>
<p>Swap the first line in your function ... | 1 | 2009-04-12T21:58:38Z | [
"python"
] |
Get the amplitude at a given time within a sound file? | 742,546 | <p>I'm working on a project where I need to know the amplitude of sound coming in from a microphone on a computer. </p>
<p>I'm currently using Python with the <a href="http://www.speech.kth.se/snack/" rel="nofollow">Snack Sound Toolkit</a> and I can record audio coming in from the microphone, but I need to know how lo... | 2 | 2009-04-12T22:36:30Z | 742,575 | <p>Looking at the Snack Sound Toolkit examples, there seems to be a dbPowerSpectrum function. </p>
<p>From the reference:</p>
<blockquote>
<p>dBPowerSpectrum ( )</p>
<p>Computes the log FFT power spectrum of the sound (at the sample number given in the start option) and returns a list of dB values. See the sec... | 3 | 2009-04-12T22:56:54Z | [
"python",
"audio",
"input",
"microphone",
"amplitude"
] |
Get the amplitude at a given time within a sound file? | 742,546 | <p>I'm working on a project where I need to know the amplitude of sound coming in from a microphone on a computer. </p>
<p>I'm currently using Python with the <a href="http://www.speech.kth.se/snack/" rel="nofollow">Snack Sound Toolkit</a> and I can record audio coming in from the microphone, but I need to know how lo... | 2 | 2009-04-12T22:36:30Z | 782,014 | <p>I disagree completely with this "answer" from CookieOfFortune.</p>
<p>granted, the question is poorly phrased... but this answer is making things much more complex than necessary. I am assuming that by 'amplitude' you mean perceived loudness. as technically each sample in the (PCM) audio stream represents an ampli... | 1 | 2009-04-23T14:28:38Z | [
"python",
"audio",
"input",
"microphone",
"amplitude"
] |
Get the amplitude at a given time within a sound file? | 742,546 | <p>I'm working on a project where I need to know the amplitude of sound coming in from a microphone on a computer. </p>
<p>I'm currently using Python with the <a href="http://www.speech.kth.se/snack/" rel="nofollow">Snack Sound Toolkit</a> and I can record audio coming in from the microphone, but I need to know how lo... | 2 | 2009-04-12T22:36:30Z | 845,613 | <p>I'm not sure if this will help, but
skimpygimpy
provides facilities for parsing WAVE files into python
sequences and back -- you could potentially use this
to examine the wave form samples directly and do
what you like. You will have to read some source,
these subcomponents are not documented.</p>
| 0 | 2009-05-10T16:30:16Z | [
"python",
"audio",
"input",
"microphone",
"amplitude"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 742,723 | <p>yes, you can.
Quick and dirty: </p>
<pre><code>class foo:
def one(self):
print "here is one"
def two(self):
print "here is two"
def three(self):
print "here is three"
obj = foo()
for entry in dir(obj):
print entry, callable(getattr(obj,entry))
if callable(getattr(obj,en... | 4 | 2009-04-13T00:41:33Z | [
"python",
"reflection",
"oop"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 742,724 | <p>Depends what you mean by "function". Something like this could work, though:</p>
<pre><code>import inspect
def methods(c):
return (m for m in (getattr(c, d) for d in dir(c))
if inspect.ismethoddescriptor(m) or inspect.ismethod(m))
</code></pre>
<p>Then:</p>
<pre><code>class C:
def f(self): p... | 3 | 2009-04-13T00:42:26Z | [
"python",
"reflection",
"oop"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 742,913 | <p>Here is one that uses yield to loop through the functions in the class. </p>
<pre><code>def get_functions(mod):
for entry in dir(mod):
obj=getattr(mod,entry);
if hasattr(obj, '__call__') and hasattr(obj,'__func__') :
yield obj
class foo:
def one(self):
print ("here is ... | 1 | 2009-04-13T02:49:06Z | [
"python",
"reflection",
"oop"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 743,528 | <p>Since you wrote the class, you already <em>know</em> all the functions.</p>
<pre><code>class ThisIsPeculiar( object ):
def aFunction( self, arg1 ):
pass
def anotherFunction( self, thisArg, thatArg ):
pass
functionsToCall = [ aFunction, anotherFunction ]
>>> p= ThisIsPeculiar()
... | 1 | 2009-04-13T10:18:15Z | [
"python",
"reflection",
"oop"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 743,959 | <p>Try using the <a href="http://docs.python.org/library/inspect.html" rel="nofollow">inspect module</a>:</p>
<pre><code>import inspect
class Spam:
def eggs(self):
print "eggs"
def ducks(self):
print "ducks"
value = "value"
spam = Spam()
for name, method in inspect.getmembers(spam, callab... | 1 | 2009-04-13T14:02:09Z | [
"python",
"reflection",
"oop"
] |
For each function in class within python | 742,708 | <p>In python is it possible to run each function inside a class?</p>
<p><strong>EDIT:</strong>
What i am trying to do is call of the functions inside a class, collect their return variables and work with that.</p>
| 5 | 2009-04-13T00:30:07Z | 747,958 | <p>The <a href="http://docs.python.org/library/functions.html#dir" rel="nofollow"><code>dir</code> builtin</a> will list all attributes of an object, for example:</p>
<pre><code>>>> class MyClass:
... def one(self):
... print "one"
... def two(self):
... print "two"
... def three(s... | 3 | 2009-04-14T14:46:40Z | [
"python",
"reflection",
"oop"
] |
Discrete Event Queuing Simulation | 742,776 | <p>I'm stuck trying to implement a single server queue. I've adapted some <a href="http://pastebin.com/m17c230e6" rel="nofollow">pseudocode</a> from Norm Matloff's Simpy tutorial to Python and the code is <a href="http://pastebin.com/m566e1c12" rel="nofollow">here</a>. Now I am struggling to find some way to calculate ... | 2 | 2009-04-13T01:18:54Z | 742,825 | <p>You should know when each customer arrived in the queue. When they arrive at the server you should add one to the number of customers served as well as accumulate the amount of time he waited. At the end of the simulation you simply divide the accumulated time by the number of customers and you have a mean wait ti... | 5 | 2009-04-13T01:47:21Z | [
"python",
"queue"
] |
Immutability and thread safety in Python | 742,882 | <p>I'm cleaning some of the Python code I wrote when I was...not as knowledgeable. Primarily I am killing some of the complexity that stemmed from an incomplete understanding of threading in Python. I need to make a list of items thread-safe, and I'd like to do it via immutable lists, instead of the usual locking appro... | 4 | 2009-04-13T02:25:58Z | 742,899 | <p>First of all, appending to a list is already thread-safe in the CPython reference implementation of the Python programming language. In other words, while the language specification doesn't require that the list class be thread-safe, it is anyway. So unless you're using Jython or IronPython or some other Python im... | 15 | 2009-04-13T02:35:47Z | [
"python",
"multithreading",
"immutability"
] |
Immutability and thread safety in Python | 742,882 | <p>I'm cleaning some of the Python code I wrote when I was...not as knowledgeable. Primarily I am killing some of the complexity that stemmed from an incomplete understanding of threading in Python. I need to make a list of items thread-safe, and I'd like to do it via immutable lists, instead of the usual locking appro... | 4 | 2009-04-13T02:25:58Z | 742,923 | <p>A true immutable list implementation will not allow the underlying list structure to change, like you are here. As @[Eli Courtwright] pointed out, your implementation is not thread safe. That is because it is not really immutable. To make an immutable implementation, any methods that would have changed the list, wou... | 4 | 2009-04-13T02:57:27Z | [
"python",
"multithreading",
"immutability"
] |
Django RSS Feed Wrong Domain | 742,974 | <p>I have an RSS feed that I'm setting up on my new site using Django. Currently I have an RSS feed being served per user, rather than just one big nasty, global RSS feed. The only problem is that the links that are returned by the RSS feed have the completely wrong domain name in the links. The end path is perfectly c... | 3 | 2009-04-13T03:36:50Z | 744,191 | <p>May be it's coming from environment variables? Try:</p>
<pre><code>export | grep your.mistery.domain
</code></pre>
<p>see if that comes up with anything, do that as the same user under which you are running your Django apps.</p>
<p>You know you can always implement your item_link() method which would return the U... | 3 | 2009-04-13T15:08:41Z | [
"python",
"django",
"rss"
] |
Trying to embed python into tinycc, says python symbols are undefined | 743,044 | <p>I've literally spent the past half hour searching for the solution to this, and everything involves GCC. What I do here works absolutely fine with GCC, however I'm using TinyCC, and this is where I'm getting confused. First the code:</p>
<pre><code>#include <Python.h>
#include <stdio.h>
int main(int arg... | 1 | 2009-04-13T04:32:36Z | 743,217 | <p>Did you use <code>tiny_impdef.exe</code> to create a <code>.def</code> file for the Python DLL?</p>
| 2 | 2009-04-13T06:57:54Z | [
"python",
"c",
"compilation"
] |
Trying to embed python into tinycc, says python symbols are undefined | 743,044 | <p>I've literally spent the past half hour searching for the solution to this, and everything involves GCC. What I do here works absolutely fine with GCC, however I'm using TinyCC, and this is where I'm getting confused. First the code:</p>
<pre><code>#include <Python.h>
#include <stdio.h>
int main(int arg... | 1 | 2009-04-13T04:32:36Z | 1,459,983 | <p>Full solution for Windows:</p>
<ol>
<li><p>tiny_impdef as per bk1e's advice</p>
<p><code>tiny_impdef.exe c:\WINDOWS\system32\python25.dll</code></p></li>
<li><p>add python25.def (or python26.def) to compilation list</p>
<p><code>tcc tiny.c python25.def -IC:\Python25\include -LC:\Python25\libs -lpython25</code></p... | 2 | 2009-09-22T13:04:32Z | [
"python",
"c",
"compilation"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 743,174 | <p>Why don't you just do</p>
<pre><code>for s in l :
print s
print "done"
</code></pre>
<p>?</p>
| 3 | 2009-04-13T06:23:44Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 743,186 | <p>I am not sure what you are trying to do. You can implement a do-while loop like this:</p>
<pre><code>while True:
stuff()
if fail_condition:
break
</code></pre>
<p>Or:</p>
<pre><code>stuff()
while not fail_condition:
stuff()
</code></pre>
<p>What are you doing trying to use a do while loop to print the ... | 449 | 2009-04-13T06:28:43Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 743,231 | <p>Exception will break the loop, so you might as well handle it outside the loop.</p>
<pre><code>try:
while True:
if s:
print s
s = i.next()
except StopIteration:
pass
</code></pre>
<p>I guess that the problem with your code is that behaviour of <code>break</code> inside <code>except</code> is n... | 22 | 2009-04-13T07:06:40Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 743,339 | <p>See if this helps :</p>
<p>Set a flag inside the exception handler and check it before working on the s.</p>
<pre><code>flagBreak = false;
while True :
if flagBreak : break
if s :
print s
try :
s = i.next()
except StopIteration :
flagBreak = true
print "done"
</code></pre... | -1 | 2009-04-13T08:17:55Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 743,944 | <pre><code>do {
stuff()
} while (condition())
</code></pre>
<p>-></p>
<pre><code>while True:
stuff()
if not condition():
break
</code></pre>
<p>You can do a function:</p>
<pre><code>def do_while(stuff, condition):
while condition(stuff()):
pass
</code></pre>
<p>But
1) It's ugly.
2) Condition should... | 20 | 2009-04-13T13:57:02Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 1,662,604 | <p>Here is a crazier solution of a different pattern -- using coroutines. The code is still very similar, but with one important difference; there are no exit conditions at all! The coroutine (chain of coroutines really) just stops when you stop feeding it with data.</p>
<pre><code>def coroutine(func):
"""Coroutin... | 13 | 2009-11-02T17:32:02Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 2,440,643 | <p>Here's a very simple way to emulate a do-while loop:</p>
<pre><code>condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
</code></pre>
<p>The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluat... | 152 | 2010-03-14T00:09:54Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 4,309,796 | <pre><code>while condition is True:
stuff()
else:
stuff()
</code></pre>
| 5 | 2010-11-30T01:38:03Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 5,957,047 | <p>for a do - while loop containing try statements</p>
<pre><code>loop = True
while loop:
generic_stuff()
try:
questionable_stuff()
# to break from successful completion
# loop = False
except:
optional_stuff()
# to break from unsuccessful completion -
# the case r... | 8 | 2011-05-10T22:03:06Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 16,136,518 | <p>Quick hack:</p>
<pre><code>def dowhile(func = None, condition = None):
if not func or not condition:
return
else:
func()
while condition():
func()
</code></pre>
<p>Use like so:</p>
<pre><code>>>> x = 10
>>> def f():
... global x
... x = x - 1
&... | 4 | 2013-04-21T21:42:57Z | [
"python",
"while-loop",
"do-while"
] |
Emulate a do-while loop in Python? | 743,164 | <p>I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work:</p>
<pre><code>l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
s = i.next()
except StopIteration :
break
print "done"
</code></pre>
<p>Instead of "1,... | 357 | 2009-04-13T06:18:42Z | 27,095,765 | <p>My code below might be a useful implementation, highlighting the main difference between <a href="/questions/tagged/do-while" class="post-tag" title="show questions tagged 'do-while'" rel="tag">do-while</a> vs <a href="/questions/tagged/while" class="post-tag" title="show questions tagged 'while'" re... | 14 | 2014-11-23T23:37:17Z | [
"python",
"while-loop",
"do-while"
] |
Something wrong with output from list in Python | 743,248 | <p>I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: </p>
<pre><code>A Alfa
B Betta
C Charlie
</code></pre>
<p>I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie... | 2 | 2009-04-13T07:16:48Z | 743,268 | <p>You want something like this:</p>
<pre><code>english2german = open("english2german.txt")
englist = []
gerlist = []
for line in english2german:
(e, g) = line.split()
englist.append(e)
gerlist.append(g)
</code></pre>
<p>The problem with your code before is that <code>englist[i:]</code> is actually a <em... | 6 | 2009-04-13T07:30:34Z | [
"python",
"list",
"text"
] |
Something wrong with output from list in Python | 743,248 | <p>I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: </p>
<pre><code>A Alfa
B Betta
C Charlie
</code></pre>
<p>I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie... | 2 | 2009-04-13T07:16:48Z | 743,274 | <p>Like this you mean:</p>
<pre><code>english2german = open('k.txt', 'r')
englist = []
gerlist = []
for i, line in enumerate(english2german):
englist.append(line.split()[0])
gerlist.append(line.split()[1])
print englist
print gerlist
</code></pre>
<p>which generates:</p>
<p>['A', 'B', 'C']
['Alfa', 'Betta'... | 1 | 2009-04-13T07:32:34Z | [
"python",
"list",
"text"
] |
Something wrong with output from list in Python | 743,248 | <p>I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: </p>
<pre><code>A Alfa
B Betta
C Charlie
</code></pre>
<p>I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie... | 2 | 2009-04-13T07:16:48Z | 743,313 | <p>And even shorter than <a href="http://stackoverflow.com/questions/743248/something-wrong-with-output-from-list-in-python/743274#743274">amo-ej1's answer</a>, and likely faster:</p>
<pre><code>In [1]: english2german = open('english2german.txt')
In [2]: eng, ger = zip(*( line.split() for line in english2german ))
In ... | 6 | 2009-04-13T07:58:57Z | [
"python",
"list",
"text"
] |
Something wrong with output from list in Python | 743,248 | <p>I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: </p>
<pre><code>A Alfa
B Betta
C Charlie
</code></pre>
<p>I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie... | 2 | 2009-04-13T07:16:48Z | 743,378 | <p>The solutions already posted are OK if you have no spaces in any of the words (ie each line has a single space). If I understand correctly, you are trying to build a dictionary, so I would suggest you consider the fact that you can also have definitions of multiple word expressions. In that case, you'd better use so... | 1 | 2009-04-13T08:46:23Z | [
"python",
"list",
"text"
] |
Something wrong with output from list in Python | 743,248 | <p>I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form: </p>
<pre><code>A Alfa
B Betta
C Charlie
</code></pre>
<p>I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie... | 2 | 2009-04-13T07:16:48Z | 743,968 | <p>just an addition: you're working with files.
please close them :) or use the with construct:</p>
<pre><code>with open('english2german.txt') as english2german:
englist, gerlist = zip(*(line.split() for line in english2german))
</code></pre>
| 3 | 2009-04-13T14:04:15Z | [
"python",
"list",
"text"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.