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 |
|---|---|---|---|---|---|---|---|---|---|
How to find out if a Python object is a string? | 1,303,243 | <p>How can I check if a Python object is a string (either regular or Unicode)?</p>
| 195 | 2009-08-19T23:50:58Z | 37,808,689 | <p>I found this ans more pythonic:</p>
<pre><code>if type(aObject) is str:
#do your stuff here
pass
</code></pre>
<p>since type objects are singleton, <strong>is</strong> can be used to do the compare</p>
| 1 | 2016-06-14T09:53:58Z | [
"python",
"string",
"types",
"compatibility"
] |
How to find out if a Python object is a string? | 1,303,243 | <p>How can I check if a Python object is a string (either regular or Unicode)?</p>
| 195 | 2009-08-19T23:50:58Z | 38,124,526 | <p>In order to check if your variable is something you could go like:</p>
<pre><code>s='Hello World'
if isinstance(s,str):
#do something here,
</code></pre>
<p>The output of isistance will give you a boolean True or False value so you can adjust accordingly.
You can check the expected acronym of your value by initial... | 1 | 2016-06-30T13:26:15Z | [
"python",
"string",
"types",
"compatibility"
] |
How to find out if a Python object is a string? | 1,303,243 | <p>How can I check if a Python object is a string (either regular or Unicode)?</p>
| 195 | 2009-08-19T23:50:58Z | 38,636,757 | <p>If one wants to stay away from explicit type-checking (and there <em>are</em> good reasons to stay away from it), probably the safest part of the string protocol to check is:</p>
<pre><code>str(maybe_string) == maybe_string
</code></pre>
<p>It won't iterate through an iterable or iterator, it won't call a list-of-... | 1 | 2016-07-28T12:44:46Z | [
"python",
"string",
"types",
"compatibility"
] |
FFT for Spectrograms in Python | 1,303,307 | <p>How would I go about using Python to read the frequency peaks from a WAV PCM file and then be able to generate an image of it, for spectogram analysis?</p>
<p>I'm trying to make a program that allows you to read any audio file, converting it to WAV PCM, and then finding the peaks and frequency cutoffs.</p>
| 24 | 2009-08-20T00:14:03Z | 1,303,325 | <p><a href="http://docs.python.org/library/wave.html">Python's wave library</a> will let you import the audio. After that, you can <a href="http://docs.scipy.org/doc/numpy/reference/routines.fft.html">use numpy to take an FFT</a> of the audio.</p>
<p>Then, <a href="http://matplotlib.sourceforge.net/index.html">matplot... | 23 | 2009-08-20T00:19:31Z | [
"python",
"image",
"graphics",
"audio",
"fft"
] |
FFT for Spectrograms in Python | 1,303,307 | <p>How would I go about using Python to read the frequency peaks from a WAV PCM file and then be able to generate an image of it, for spectogram analysis?</p>
<p>I'm trying to make a program that allows you to read any audio file, converting it to WAV PCM, and then finding the peaks and frequency cutoffs.</p>
| 24 | 2009-08-20T00:14:03Z | 1,368,688 | <p>Loading WAV files is easy using <a href="http://cournape.github.com/audiolab/">audiolab</a>:</p>
<pre><code>from audiolab import wavread
signal, fs, enc = wavread('test.wav')
</code></pre>
<p>or for reading any general audio format and converting to WAV:</p>
<pre><code>from audiolab import Sndfile
sound_file = Sn... | 13 | 2009-09-02T16:15:24Z | [
"python",
"image",
"graphics",
"audio",
"fft"
] |
FFT for Spectrograms in Python | 1,303,307 | <p>How would I go about using Python to read the frequency peaks from a WAV PCM file and then be able to generate an image of it, for spectogram analysis?</p>
<p>I'm trying to make a program that allows you to read any audio file, converting it to WAV PCM, and then finding the peaks and frequency cutoffs.</p>
| 24 | 2009-08-20T00:14:03Z | 8,996,824 | <pre><code>from pylab import *
specgram(signal)
</code></pre>
<p>is the easiest. Also quite handy in this context:</p>
<pre><code>subplot
</code></pre>
<p>But be warned: Matplotlib is very slow but it creates beautiful images. You should not use it for demanding animation, even less when you are dealing with 3D</p>
| 3 | 2012-01-25T01:28:40Z | [
"python",
"image",
"graphics",
"audio",
"fft"
] |
FFT for Spectrograms in Python | 1,303,307 | <p>How would I go about using Python to read the frequency peaks from a WAV PCM file and then be able to generate an image of it, for spectogram analysis?</p>
<p>I'm trying to make a program that allows you to read any audio file, converting it to WAV PCM, and then finding the peaks and frequency cutoffs.</p>
| 24 | 2009-08-20T00:14:03Z | 9,714,655 | <p>If you need to convert from PCM format to integers, you'll want to use struct.unpack.</p>
| 1 | 2012-03-15T05:54:00Z | [
"python",
"image",
"graphics",
"audio",
"fft"
] |
Getting a map() to return a list in Python 3.x | 1,303,347 | <p>I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:</p>
<p><strong>A:</strong> Python 2.6:</p>
<pre><code>>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
</code></pre>
<p>However, on Python 3.1, the above returns a map object.</p>
<p><strong>B:</strong... | 155 | 2009-08-20T00:27:26Z | 1,303,353 | <p>Why aren't you doing this:</p>
<pre><code>[chr(x) for x in [66,53,0,94]]
</code></pre>
<p>It's called a list comprehension. You can find plenty of information on Google, but <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">here's the link to the Python (2.6) documentation on list ... | 50 | 2009-08-20T00:28:19Z | [
"python",
"list",
"python-3.x",
"map-function"
] |
Getting a map() to return a list in Python 3.x | 1,303,347 | <p>I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:</p>
<p><strong>A:</strong> Python 2.6:</p>
<pre><code>>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
</code></pre>
<p>However, on Python 3.1, the above returns a map object.</p>
<p><strong>B:</strong... | 155 | 2009-08-20T00:27:26Z | 1,303,354 | <p>Do this:</p>
<pre><code>list(map(chr,[66,53,0,94]))
</code></pre>
<p>In Python 3+, many processes that iterate over iterables return iterators themselves. In most cases, this ends up saving memory, and should make things go faster. </p>
<p>If all you're going to do is iterate over this list eventually, there's... | 256 | 2009-08-20T00:28:40Z | [
"python",
"list",
"python-3.x",
"map-function"
] |
Getting a map() to return a list in Python 3.x | 1,303,347 | <p>I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:</p>
<p><strong>A:</strong> Python 2.6:</p>
<pre><code>>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
</code></pre>
<p>However, on Python 3.1, the above returns a map object.</p>
<p><strong>B:</strong... | 155 | 2009-08-20T00:27:26Z | 1,303,355 | <p>I'm not familiar with Python 3.1, but will this work?</p>
<pre><code>[chr(x) for x in [66, 53, 0, 94]]
</code></pre>
| 9 | 2009-08-20T00:28:44Z | [
"python",
"list",
"python-3.x",
"map-function"
] |
Getting a map() to return a list in Python 3.x | 1,303,347 | <p>I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:</p>
<p><strong>A:</strong> Python 2.6:</p>
<pre><code>>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
</code></pre>
<p>However, on Python 3.1, the above returns a map object.</p>
<p><strong>B:</strong... | 155 | 2009-08-20T00:27:26Z | 24,507,069 | <p>List-returning map function has the advantage of saving typing, especially during interactive sessions. You can define <code>lmap</code> function (on the analogy of python2's <code>imap</code>) that returns list:</p>
<pre><code>lmap = lambda func, *iterable: list(map(func, *iterable)
</code></pre>
<p>Then calling ... | 13 | 2014-07-01T09:41:21Z | [
"python",
"list",
"python-3.x",
"map-function"
] |
Getting a map() to return a list in Python 3.x | 1,303,347 | <p>I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy:</p>
<p><strong>A:</strong> Python 2.6:</p>
<pre><code>>>> map(chr, [66, 53, 0, 94])
['B', '5', '\x00', '^']
</code></pre>
<p>However, on Python 3.1, the above returns a map object.</p>
<p><strong>B:</strong... | 155 | 2009-08-20T00:27:26Z | 38,702,484 | <p>New and neat in Python 3.5:</p>
<pre><code>[*map(chr, [66, 53, 0, 94])]
</code></pre>
<p>Thanks to <a href="https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-448" rel="nofollow">Additional Unpacking Generalizations</a></p>
| 3 | 2016-08-01T15:18:41Z | [
"python",
"list",
"python-3.x",
"map-function"
] |
python shutil.copytree - ignore permissions | 1,303,413 | <p>Python's <code>shutil.copytree</code> is not very flexible; what is the simplest way to add support for ignoring permissions while copying in <code>copytree</code> (without having to re-write its implementation)?</p>
<p>Otherwise, <code>copytree</code> fails like this:</p>
<pre><code>(â¦)â[Errno 45] Operation n... | 4 | 2009-08-20T00:51:17Z | 1,303,434 | <p>You have <code>shutil.py</code> in your standard Python distribution (on Ubuntu, mine is under <code>/usr/lib/python2.6</code> for instance; Windows might be <code>C:\Python26\lib</code>?). The copytree function is only 38 lines long (34 if you don't count comments), and the end of the docstring explicitly states:... | 3 | 2009-08-20T00:58:06Z | [
"python",
"file",
"shutil"
] |
python shutil.copytree - ignore permissions | 1,303,413 | <p>Python's <code>shutil.copytree</code> is not very flexible; what is the simplest way to add support for ignoring permissions while copying in <code>copytree</code> (without having to re-write its implementation)?</p>
<p>Otherwise, <code>copytree</code> fails like this:</p>
<pre><code>(â¦)â[Errno 45] Operation n... | 4 | 2009-08-20T00:51:17Z | 17,022,146 | <p>Not thread-safe (or advisable in general) but OK for a throwaway script:</p>
<pre>
import shutil
_orig_copystat = shutil.copystat
shutil.copystat = lambda x, y: x
shutil.copytree(src, dst)
shutil.copystat = _orig_copystat
</pre>
| 1 | 2013-06-10T10:50:47Z | [
"python",
"file",
"shutil"
] |
Threaded Django task doesn't automatically handle transactions or db connections? | 1,303,654 | <p>I've got Django set up to run some recurring tasks in their own threads, and I noticed that they were always leaving behind unfinished database connection processes (pgsql "Idle In Transaction").</p>
<p>I looked through the Postgres logs and found that the transactions weren't being completed (no ROLLBACK). I tried... | 50 | 2009-08-20T02:26:48Z | 1,346,401 | <p>After weeks of testing and reading the Django source code, I've found the answer to my own question:</p>
<p><strong>Transactions</strong></p>
<p>Django's default autocommit behavior still holds true for my threaded function. However, it states in the Django docs:</p>
<blockquote>
<p>As soon as you perform an ac... | 85 | 2009-08-28T11:22:05Z | [
"python",
"database",
"django",
"multithreading",
"transactions"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 1,303,687 | <p>It may save you a lot of time to reinstall python using fink or macports, then get the berkley package from either.</p>
| 0 | 2009-08-20T02:45:30Z | [
"python",
"osx"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 1,304,700 | <p>The standard Python 2.6 installer for OS X from <a href="http://www.python.org/download/releases/2.6.2/" rel="nofollow">python.org</a> includes bsddb. Why build your own? </p>
| 2 | 2009-08-20T08:15:42Z | [
"python",
"osx"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 1,767,639 | <p>Try:</p>
<p>easy_install bsddb3</p>
| 0 | 2009-11-20T00:44:20Z | [
"python",
"osx"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 1,898,019 | <p>Here's what I did for python2.5 easy_install didn't work for me</p>
<pre><code># get berkeley db
sudo port install db44
# download and untar bsddb3-4.8.1 and cd to the directory
sudo python setup.py --berkeley-db-incdir=/opt/local/include/db44 --berkeley-db-libdir=/opt/local/lib/db44 build
# test program test.py ... | 0 | 2009-12-13T22:25:16Z | [
"python",
"osx"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 6,570,711 | <p>I had a similar problem installing <code>lxml</code>. Try this for berkeleydb:</p>
<pre><code>sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install bsddb3
</code></pre>
| 0 | 2011-07-04T11:13:40Z | [
"python",
"osx"
] |
Unable to install python berkeleydb access on osx | 1,303,659 | <p>I installed python 2.6 on my mac (which ships with 2.5, and I am going crazy in working with 2.6) Everything has been installed on <code>/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/</code>. Now I want to install the python BerkeleyDB module, but it goes syntax error during the build:</p>
<pr... | 1 | 2009-08-20T02:28:02Z | 27,678,763 | <p>For Mavericks/Yosemite (can use newer versions like db53 too):</p>
<pre><code>sudo port install db51
sudo ln -sf /opt/local/include/db51/ /opt/local/lib/db51/include
sudo ln -sf /opt/local/lib/db51/ /opt/local/lib/db51/lib
sudo BERKELEYDB_DIR=/opt/local/lib/db51 ARCHFLAGS=-Wno-error=unused-command-line-argument-har... | 0 | 2014-12-28T17:21:13Z | [
"python",
"osx"
] |
setting option in config file using SafeConfigParser | 1,303,697 | <p>I'm trying to set an option (xdebug.profiler_enable) in my php.ini file using python's ConfigParser object. here is the code:
section in php.ini file im trying to modify</p>
<pre><code>[xdebug]
;XDEBUG SETTINGS
;turn on the profiler?
xdebug.profiler_enable=0
xdebug.profiler_append=1
xdebug.profiler_enable_trigg... | 1 | 2009-08-20T02:48:11Z | 1,303,724 | <pre><code>phpIni.write(open(phpIniLocation, 'w'))
</code></pre>
<p><a href="http://docs.python.org/library/configparser.html#ConfigParser.RawConfigParser.write" rel="nofollow">docs</a>.</p>
| 2 | 2009-08-20T02:59:42Z | [
"python",
"linux",
"file-io",
"ubuntu"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 1,304,050 | <p>Maybe:</p>
<pre><code>path = dict['path']
str = "path: %(path)s curr: %(curr)s prev: %(prev)s" % locals()
</code></pre>
<p>I mean it works:</p>
<pre><code>>>> dict = {"path": "/var/blah"}
>>> curr = "1.1"
>>> prev = "1.0"
>>> path = dict['path']
>>> str = "path: %(path... | 10 | 2009-08-20T05:03:33Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 1,304,088 | <p>Why not:</p>
<pre><code>mystr = "path: %s curr: %s prev: %s" % (mydict[path], curr, prev)
</code></pre>
<p>BTW, I've changed a couple names you were using that trample upon builtin names -- don't do that, it's never needed and once in a while will waste a lot of your time tracking down a misbehavior it causes (whe... | 10 | 2009-08-20T05:16:38Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 1,304,098 | <p>You can try this:</p>
<pre><code>data = {"path": "/var/blah",
"curr": "1.1",
"prev": "1.0"}
s = "path: %(path)s curr: %(curr)s prev: %(prev)s" % data
</code></pre>
| 38 | 2009-08-20T05:20:28Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 1,304,483 | <p>And of course you could use the newer (from 2.6) <a href="http://docs.python.org/library/string.html#format-string-syntax"><strong>.format</strong></a> string method:</p>
<pre><code>>>> mydict = {"path": "/var/blah"}
>>> curr = "1.1"
>>> prev = "1.0"
>>>
>>> s = "path: {... | 17 | 2009-08-20T07:17:00Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 16,095,156 | <p>You can do the following if you place your data inside a dictionary:</p>
<pre><code>data = {"path": "/var/blah","curr": "1.1","prev": "1.0"}
"{0}: {path}, {1}: {curr}, {2}: {prev}".format(*data, **data)
</code></pre>
| 1 | 2013-04-19T00:12:17Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 34,095,216 | <p>You can also (soon) use <a href="https://www.python.org/dev/peps/pep-0498/" rel="nofollow">f-strings</a> in Python 3.6, which is probably the shortest way to format a string:</p>
<pre><code>print f'path: {path} curr: {curr} prev: {prev}'
</code></pre>
<p>And even put all your data inside a dict:</p>
<pre><code>d ... | 1 | 2015-12-04T18:37:34Z | [
"python",
"string",
"interpolation"
] |
Python string interpolation using dictionary and strings | 1,304,025 | <p>Given:</p>
<pre><code>dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
</code></pre>
<p>What's the best/shortest way to interpolate the string to generate the following:</p>
<blockquote>
<p>path: /var/blah curr: 1.1 prev: 1.0</p>
</blockquote>
<p>I know this works:</p>
<pre><code>str = "path: %(path)s c... | 17 | 2009-08-20T04:54:16Z | 37,547,526 | <p>If you don't want to add the unchanging variables to your dictionary each time, you can reference both the variables and the dictionary keys using format:</p>
<p><code>str = "path {path} curr: {curr} prev: {prev}".format(curr=curr, prev=prev, **dict)</code></p>
<p>It might be bad form logically, but it makes thing... | 1 | 2016-05-31T13:44:50Z | [
"python",
"string",
"interpolation"
] |
Sage CRM web services example in Python | 1,304,106 | <p>I am trying to write a Python consumer for Sage CRM using their Web Services interface. I am using SOAPpy as Python SOAP library (not married to it, was easy to install on Ubuntu so went with it). </p>
<p>Managed to fetch the WSDL using the Proxy and executed the logon method exposed by Sage CRM. </p>
<pre><code>
... | 3 | 2009-08-20T05:23:27Z | 1,305,179 | <p>First, just to comment on SOAPpy vs. other soap libraries... SOAPpy traditionally has been the easy-to-use library that didn't support complex data structures. So if it works for your case then you are better off. For more complex data structures in the WSDL you'd need to move ZSI.</p>
<p>Anyway, the documentatio... | 0 | 2009-08-20T10:12:32Z | [
"python",
"soap",
"crm",
"sage-crm"
] |
Sage CRM web services example in Python | 1,304,106 | <p>I am trying to write a Python consumer for Sage CRM using their Web Services interface. I am using SOAPpy as Python SOAP library (not married to it, was easy to install on Ubuntu so went with it). </p>
<p>Managed to fetch the WSDL using the Proxy and executed the logon method exposed by Sage CRM. </p>
<pre><code>
... | 3 | 2009-08-20T05:23:27Z | 34,109,040 | <p>Am just figuring this out too. I have the core of this done though, so this ought to speed things up for anyone else who needs this! I am assuming other Sage subsystems work the same way (but I don't yet know), so I tried to account for that possibility.</p>
<p>First, you need this module I wrote called pySage.py.... | 0 | 2015-12-05T18:08:24Z | [
"python",
"soap",
"crm",
"sage-crm"
] |
Python egg: where is it installed? | 1,304,122 | <p>I'm trying to install py-appscript on the mac using 'sudo easy_install appscript'.
The command runs and I get a message saying "Installed /Library/Python/..../appscript=0.20.0-py2.5-maxosx-10.5-i386.egg".</p>
<p>However, when I run a tool that required this (osaglue) I get an error that py-appscript isn't installed... | 3 | 2009-08-20T05:29:42Z | 1,304,172 | <p>What does <code>which python</code> return?</p>
<p>Update: Ok, so go into /usr/bin and do a <code>ls -l | grep python</code>. Does <code>/usr/bin/python</code> link to the OSX installation?</p>
<p>If it does not, I had the same problem a while back. <code>easy_install</code> is installing the egg for the python pa... | 0 | 2009-08-20T05:45:52Z | [
"python",
"osx"
] |
Python egg: where is it installed? | 1,304,122 | <p>I'm trying to install py-appscript on the mac using 'sudo easy_install appscript'.
The command runs and I get a message saying "Installed /Library/Python/..../appscript=0.20.0-py2.5-maxosx-10.5-i386.egg".</p>
<p>However, when I run a tool that required this (osaglue) I get an error that py-appscript isn't installed... | 3 | 2009-08-20T05:29:42Z | 1,304,519 | <p>You can found all your avaiable packages in the sys.path. Start the pythonshell and type in this code:</p>
<pre><code>import sys
print sys.path
</code></pre>
| 2 | 2009-08-20T07:26:50Z | [
"python",
"osx"
] |
Python egg: where is it installed? | 1,304,122 | <p>I'm trying to install py-appscript on the mac using 'sudo easy_install appscript'.
The command runs and I get a message saying "Installed /Library/Python/..../appscript=0.20.0-py2.5-maxosx-10.5-i386.egg".</p>
<p>However, when I run a tool that required this (osaglue) I get an error that py-appscript isn't installed... | 3 | 2009-08-20T05:29:42Z | 1,308,781 | <p>Do you have more than one version of Python installed on your machine? (Apple install their own copy of Python in /System, with third-party modules at /Library/Python, by default.) If so, chances are, easy_install is installing appscript for one Python, while osaglue is using another.</p>
<p>That said, if you're lo... | 0 | 2009-08-20T21:01:29Z | [
"python",
"osx"
] |
relevent query to how to fetch public key from public key server | 1,304,130 | <pre><code>import urllib
response = urllib.urlopen('http://pool.sks-keyservers.net/')
print 'RESPONSE:', response
print 'URL :', response.geturl()
headers = response.info()
print 'DATE :', headers['date']
print 'HEADERS :'
print '---------'
print headers
data = response.read()
print 'LENGTH :', len(data)
pri... | 2 | 2009-08-20T05:32:19Z | 1,304,618 | <p><a href="http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00" rel="nofollow">This spec</a> should help you along, and stop you asking the same question repeatedly :). Basically you need to perform an HTTP GET request to the key server. Most key servers work on TCP port 11371 so you need to ensure that your firewall... | 4 | 2009-08-20T07:52:51Z | [
"python",
"urllib"
] |
relevent query to how to fetch public key from public key server | 1,304,130 | <pre><code>import urllib
response = urllib.urlopen('http://pool.sks-keyservers.net/')
print 'RESPONSE:', response
print 'URL :', response.geturl()
headers = response.info()
print 'DATE :', headers['date']
print 'HEADERS :'
print '---------'
print headers
data = response.read()
print 'LENGTH :', len(data)
pri... | 2 | 2009-08-20T05:32:19Z | 8,388,169 | <p>Use <a href="http://pypi.python.org/pypi/python-hkp" rel="nofollow">http://pypi.python.org/pypi/python-hkp</a></p>
<p>Python HPK client.</p>
<p>Usage example:</p>
<pre><code>>>> from hkp import KeyServer
>>> serv = KeyServer('http://pool.sks-keyservers.net')
>>> serv.search('Dmitry Glad... | 1 | 2011-12-05T16:05:49Z | [
"python",
"urllib"
] |
Python logging SMTPHandler - handling offline SMTP server | 1,304,593 | <p>I have setup the logging module for my new python script. I have two handlers, one sending stuff to a file, and one for email alerts. The SMTPHandler is setup to mail anything at the ERROR level or above.</p>
<p>Everything works great, unless the SMTP connection fails. If the SMTP server does not respond or auth... | 1 | 2009-08-20T07:45:57Z | 1,304,622 | <p>You probably need to do both. To figure this out, I suggest to install a local mail server and use that. This way, you can shut it down while your script runs and note down the error message.</p>
<p>To keep the code maintainable, you should extends SMTPHandler in such a way that you can handle the exceptions in a s... | 0 | 2009-08-20T07:55:43Z | [
"python",
"logging",
"handler"
] |
Python logging SMTPHandler - handling offline SMTP server | 1,304,593 | <p>I have setup the logging module for my new python script. I have two handlers, one sending stuff to a file, and one for email alerts. The SMTPHandler is setup to mail anything at the ERROR level or above.</p>
<p>Everything works great, unless the SMTP connection fails. If the SMTP server does not respond or auth... | 1 | 2009-08-20T07:45:57Z | 1,308,024 | <p>Exceptions which occur during logging should not stop your script, though they may cause a traceback to be printed to <code>sys.stderr</code>. In order to prevent this printout, do the following:</p>
<pre><code>logging.raiseExceptions = 0
</code></pre>
<p>This is not the default (because in development you typical... | 4 | 2009-08-20T18:32:40Z | [
"python",
"logging",
"handler"
] |
User-specific model in Django | 1,304,608 | <p>I have a model containing items, which has many different fields. There is another model which assigns a set of this field to each user using a m2m-relation.</p>
<p>I want to achieve, that in the end, every user has access to a defined set of fields of the item model, and he only sees these field in views, he can o... | 0 | 2009-08-20T07:50:34Z | 1,304,634 | <p>One way to do this would be to break the <code>Item</code> model up into the parts that are individually assignable to a user. If you have fixed user types (admin, customer, team etc.) who can always see the same set of fields, these parts would be whole groups of fields. If it's very dynamic and you want to be able... | 0 | 2009-08-20T07:58:22Z | [
"python",
"django",
"django-models"
] |
User-specific model in Django | 1,304,608 | <p>I have a model containing items, which has many different fields. There is another model which assigns a set of this field to each user using a m2m-relation.</p>
<p>I want to achieve, that in the end, every user has access to a defined set of fields of the item model, and he only sees these field in views, he can o... | 0 | 2009-08-20T07:50:34Z | 1,304,676 | <p>A second approach would be to not include the filtering in the model layer. I. e., you leave the mapping on the model layer as it is and retrieve the full set of item fields for each user. Then you pass the items through a filter that implements the rules.</p>
<p>Which approach is better for you depends on how you ... | 0 | 2009-08-20T08:09:16Z | [
"python",
"django",
"django-models"
] |
how to install new packages with python 3.1.1? | 1,304,638 | <p>I've tried to install pip on windows, but it's not working:</p>
<p>giving me ImportError: No module named pkg_resources</p>
<p>easy_install doesn't have version 3.1 or so, just 2.5, and should be replaced by pim.</p>
<p>is there easy way to install it on windows? </p>
| 1 | 2009-08-20T07:59:36Z | 1,304,651 | <p>setuptools doesn't quite work on Python 3.1 yet. Try installing packages with regular distutils, or use binary packages (.exe, .msi) provided by the package author.</p>
| 0 | 2009-08-20T08:03:25Z | [
"python"
] |
Difference between accessing an instance attribute and a class attribute | 1,304,868 | <p>I have a Python class</p>
<pre><code>class pytest:
i = 34
def func(self):
return "hello world"
</code></pre>
<p>When I access <code>pytest.i</code>, I get 34. I can also do this another way:</p>
<pre><code>a = pytest()
a.i
</code></pre>
<p>This gives 34 as well.</p>
<p>If I try to access the (no... | 2 | 2009-08-20T08:48:48Z | 1,304,923 | <p>No, these are two different things.</p>
<p>In Python, everything is an object. Classes are objects, functions are objects and instances are objects. Since everything is an object, everything behaves in a similar way. In your case, you create a class instance (== an object with the type "Class") with the name "pytes... | 6 | 2009-08-20T09:01:01Z | [
"python",
"class",
"instance"
] |
Difference between accessing an instance attribute and a class attribute | 1,304,868 | <p>I have a Python class</p>
<pre><code>class pytest:
i = 34
def func(self):
return "hello world"
</code></pre>
<p>When I access <code>pytest.i</code>, I get 34. I can also do this another way:</p>
<pre><code>a = pytest()
a.i
</code></pre>
<p>This gives 34 as well.</p>
<p>If I try to access the (no... | 2 | 2009-08-20T08:48:48Z | 1,306,560 | <p>To summarize, there are two types of variables associated with classes and objects: class variables and instance variables. Class variables are associated with classes, but instance variables are associated with objects. Here's an example:</p>
<pre><code>class TestClass:
classVar = 0
def __init__(self):
... | 0 | 2009-08-20T14:25:21Z | [
"python",
"class",
"instance"
] |
Python japanese module is not found | 1,305,042 | <p>I run following python script.</p>
<p><a href="http://www.halb-katze.jp/pygt/src/pygame2exe.py" rel="nofollow">pygame2exe.py</a></p>
<pre><code>ImportError: No module named japanese
</code></pre>
<p>What's wrong?</p>
<p>Do not you know solutions? </p>
| 0 | 2009-08-20T09:35:11Z | 1,305,197 | <p>The script makes use of japanese encoding</p>
<pre><code># -*- coding: sjis -*-
[...]
args.append('japanese,encodings');
</code></pre>
<p>It's a shame cause it could use UTF-8 that works out of the box. </p>
<p>You can't run this script unless you install the japanese module. I can't find any reference of it on... | 1 | 2009-08-20T10:17:29Z | [
"python",
"pygame"
] |
Python japanese module is not found | 1,305,042 | <p>I run following python script.</p>
<p><a href="http://www.halb-katze.jp/pygt/src/pygame2exe.py" rel="nofollow">pygame2exe.py</a></p>
<pre><code>ImportError: No module named japanese
</code></pre>
<p>What's wrong?</p>
<p>Do not you know solutions? </p>
| 0 | 2009-08-20T09:35:11Z | 1,305,707 | <p>To add to e-satis' explanation, the "japanese" module is <a href="http://www.python.jp/Zope/download/JapaneseCodecs" rel="nofollow">provided by the Japan PUG</a>, but I don't think you've actually needed it since around Python 2.2. I believe that all the Japanese codecs are included in a standard Python install thes... | 0 | 2009-08-20T12:04:14Z | [
"python",
"pygame"
] |
Python socket error occured | 1,305,050 | <p>I wrote this code.</p>
<pre><code>import socket
host = 'localhost'
port = 3794
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))
while 1:
print 'Type message you want to s... | 4 | 2009-08-20T09:36:41Z | 1,305,381 | <p>This means that you already have a socket bound to 3794 port.</p>
<p>It may be another application or it means that port didn't got released yet after the previous run of your own script (it happens, if script terminated improperly).</p>
<p>Simply try to use another port number - I believe everything will work fin... | 8 | 2009-08-20T10:54:41Z | [
"python",
"sockets"
] |
Python socket error occured | 1,305,050 | <p>I wrote this code.</p>
<pre><code>import socket
host = 'localhost'
port = 3794
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))
while 1:
print 'Type message you want to s... | 4 | 2009-08-20T09:36:41Z | 6,222,123 | <p>I had this same problem and it was caused by trying to listen on the wrong host. When I changed it to an IP that was actually associated with the machine the code was running on (localhost), the problem went away.</p>
| 3 | 2011-06-03T01:31:19Z | [
"python",
"sockets"
] |
Python socket error occured | 1,305,050 | <p>I wrote this code.</p>
<pre><code>import socket
host = 'localhost'
port = 3794
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))
while 1:
print 'Type message you want to s... | 4 | 2009-08-20T09:36:41Z | 15,639,675 | <p>This error comes up mostly due to the the port being already used by another application/service .
Choose a port number above the range of registered ports , i.e 49151</p>
| 0 | 2013-03-26T14:32:43Z | [
"python",
"sockets"
] |
Django - Edit data in db | 1,305,182 | <p>I have a question regarding editing/saving data in database using Django.</p>
<p>I have template that show data from database. And after each record i have link that link you to edit page.
But now is the question how to edit data in db without using admin panel?</p>
<p>I run thought tutorial in djangobook but i d... | 0 | 2009-08-20T10:13:54Z | 1,305,199 | <p>Have a look at the <a href="http://docs.djangoproject.com/en/dev/topics/forms/" rel="nofollow">"Working with forms" section</a> in the Django documentation.</p>
| 1 | 2009-08-20T10:18:06Z | [
"python",
"django"
] |
Django - Edit data in db | 1,305,182 | <p>I have a question regarding editing/saving data in database using Django.</p>
<p>I have template that show data from database. And after each record i have link that link you to edit page.
But now is the question how to edit data in db without using admin panel?</p>
<p>I run thought tutorial in djangobook but i d... | 0 | 2009-08-20T10:13:54Z | 37,114,696 | <p>You can use Django authenticaion system to create users and giving them permissions to modify the data.</p>
| 0 | 2016-05-09T11:31:38Z | [
"python",
"django"
] |
python 3.1 with pydev | 1,305,218 | <p>I am now moving to eclipse for my python development. I have pydev installed but it is showing grammar support up to python version 3.0. My question is can I use python 3.1 with 3.0 grammar? Has the grammar changed from version 3.0 to 3.1?</p>
<p>I am using eclipse 3.4.2 and pydev 1.4.7</p>
| 5 | 2009-08-20T10:22:37Z | 1,305,253 | <p>grammar hasn't changed, some <a href="http://docs.python.org/3.1/whatsnew/3.1.html" rel="nofollow">modules have</a>.</p>
| 10 | 2009-08-20T10:31:19Z | [
"python",
"eclipse",
"python-3.x",
"pydev"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,305,314 | <p>A closing paren is missing at <code>row[2] == day</code>.</p>
| 13 | 2009-08-20T10:43:52Z | [
"python",
"syntax-error"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,305,323 | <p>You're missing a closing parenthesis:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
^^^
</code></pre>
<p>Also, beware indentation.</p>
| 11 | 2009-08-20T10:44:41Z | [
"python",
"syntax-error"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,305,369 | <p>Try:</p>
<pre><code>if row[0:5] == [year, month, day, hour, minute]:
</code></pre>
<p>That fixes your error and makes the whole thing much more readable.</p>
| 1 | 2009-08-20T10:52:58Z | [
"python",
"syntax-error"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,305,377 | <p>If "row" is a list, you can do this instead (for clarity):</p>
<pre><code>if row[:5] == [year, month, day, hour, minute]:
</code></pre>
<p>..or if row is a tuple:</p>
<pre><code>if row[:5] == (year, month, day, hour, minute):
</code></pre>
| 16 | 2009-08-20T10:53:38Z | [
"python",
"syntax-error"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,305,543 | <p>Add a colon after "hour)" along with the aforementioned ")"</p>
| 0 | 2009-08-20T11:30:52Z | [
"python",
"syntax-error"
] |
Python - If statement | 1,305,302 | <p>I keep getting invalid syntax with the below:</p>
<pre><code>if (row[0] == year) and (row[1] == month) and (row[2] == day and (row[3] == hour) and (row[4] == minute):
print "hello"
else:
print "hello2"
</code></pre>
<p>Any ideas?</p>
| -1 | 2009-08-20T10:42:13Z | 1,306,343 | <p>You have 5 superfluous left parentheses but only 4 superfluous right parentheses. Consider losing ALL the parentheses!</p>
| 2 | 2009-08-20T13:51:16Z | [
"python",
"syntax-error"
] |
How to delete entries in a dictionary with a given flag in python? | 1,305,437 | <p>I have a dictionary, lets call it <code>myDict</code>, in Python that contains a set of similar dictionaries which all have the entry "<code>turned_on : True</code>" or "<code>turned_on : False</code>". I want to remove all the entries in <code>myDict</code> that are off, e.g. where "<code>turned_on : False</code>".... | 1 | 2009-08-20T11:07:59Z | 1,305,445 | <p>Straight-forward way:</p>
<pre><code>def delete_if_not(predicate_key, some_dict):
for key, subdict in some_dict.items():
if not subdict.get(predicate_key, True):
del some_dict[key]
</code></pre>
<p>Testing:</p>
<pre><code>mydict = {
'test1': {
'turned_on': True,
... | 5 | 2009-08-20T11:11:07Z | [
"python",
"ruby",
"dictionary"
] |
How to delete entries in a dictionary with a given flag in python? | 1,305,437 | <p>I have a dictionary, lets call it <code>myDict</code>, in Python that contains a set of similar dictionaries which all have the entry "<code>turned_on : True</code>" or "<code>turned_on : False</code>". I want to remove all the entries in <code>myDict</code> that are off, e.g. where "<code>turned_on : False</code>".... | 1 | 2009-08-20T11:07:59Z | 1,305,446 | <p>It's not clear what you want, but my guess is:</p>
<pre><code>myDict = {i: j for i, j in myDict.items() if j['turned_on']}
</code></pre>
<p>or for older version of python:</p>
<pre><code>myDict = dict((i, j) for i, j in myDict.iteritems() if j['turned_on'])
</code></pre>
| 3 | 2009-08-20T11:11:10Z | [
"python",
"ruby",
"dictionary"
] |
How to delete entries in a dictionary with a given flag in python? | 1,305,437 | <p>I have a dictionary, lets call it <code>myDict</code>, in Python that contains a set of similar dictionaries which all have the entry "<code>turned_on : True</code>" or "<code>turned_on : False</code>". I want to remove all the entries in <code>myDict</code> that are off, e.g. where "<code>turned_on : False</code>".... | 1 | 2009-08-20T11:07:59Z | 1,305,462 | <p>You mean like this?</p>
<pre><code>myDict = {"id1" : {"turned_on": True}, "id2" : {"turned_on": False}}
result = dict((a, b) for a, b in myDict.items() if b["turned_on"])
</code></pre>
<p>output:</p>
<pre><code>{'id1': {'turned_on': True}}
</code></pre>
| 7 | 2009-08-20T11:14:11Z | [
"python",
"ruby",
"dictionary"
] |
How to delete entries in a dictionary with a given flag in python? | 1,305,437 | <p>I have a dictionary, lets call it <code>myDict</code>, in Python that contains a set of similar dictionaries which all have the entry "<code>turned_on : True</code>" or "<code>turned_on : False</code>". I want to remove all the entries in <code>myDict</code> that are off, e.g. where "<code>turned_on : False</code>".... | 1 | 2009-08-20T11:07:59Z | 1,305,476 | <pre><code>d = { 'id1':{'turned_on':True}, 'id2':{'turned_on':False}}
dict((i,j) for i, j in d.items() if not j['turned_on'])
</code></pre>
| 1 | 2009-08-20T11:17:08Z | [
"python",
"ruby",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,547 | <pre><code>x = type('new_dict', (object,), d)
</code></pre>
<p>then add recursion to this and you're done.</p>
<p><strong>edit</strong> this is how I'd implement it:</p>
<pre><code>>>> d
{'a': 1, 'b': {'c': 2}, 'd': ['hi', {'foo': 'bar'}]}
>>> def obj_dic(d):
top = type('new', (object,), d)
... | 47 | 2009-08-20T11:31:34Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,548 | <p><code>x.__dict__.update(d)</code> should do fine.</p>
| 7 | 2009-08-20T11:31:59Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,561 | <p>This should get your started:</p>
<pre><code>class dict2obj(object):
def __init__(self, d):
self.__dict__['d'] = d
def __getattr__(self, key):
value = self.__dict__['d'][key]
if type(value) == type({}):
return dict2obj(value)
return value
d = {'a': 1, 'b': {'c'... | 6 | 2009-08-20T11:34:42Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,643 | <pre><code>>>> def dict2obj(d):
if isinstance(d, list):
d = [dict2obj(x) for x in d]
if not isinstance(d, dict):
return d
class C(object):
pass
o = C()
for k in d:
o.__dict__[k] = dict2obj(d[k])
return o
>>> d ... | 10 | 2009-08-20T11:50:56Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,651 | <p>Let me explain a solution I <strong>almost</strong> used some time ago. But first, the reason I did not is illustrated by the fact that the following code:</p>
<pre><code>d = {'from': 1}
x = dict2obj(d)
print x.from
</code></pre>
<p>gives this error:</p>
<pre><code> File "test.py", line 20
print x.from == 1... | 2 | 2009-08-20T11:52:34Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,663 | <p><strong>Update:</strong> In Python 2.6 and onwards, consider whether the <a href="https://docs.python.org/2/library/collections.html#collections.namedtuple"><code>namedtuple</code></a> data structure suits your needs:</p>
<pre><code>>>> from collections import namedtuple
>>> MyStruct = namedtuple(... | 495 | 2009-08-20T11:55:39Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,305,682 | <pre><code>class obj(object):
def __init__(self, d):
for a, b in d.items():
if isinstance(b, (list, tuple)):
setattr(self, a, [obj(x) if isinstance(x, dict) else x for x in b])
else:
setattr(self, a, obj(b) if isinstance(b, dict) else b)
>>> d ... | 67 | 2009-08-20T11:58:36Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,306,221 | <p>Here's another implementation:</p>
<pre><code>class DictObj(object):
def __init__(self, d):
self.__dict__ = d
def dict_to_obj(d):
if isinstance(d, (list, tuple)): return map(dict_to_obj, d)
elif not isinstance(d, dict): return d
return DictObj(dict((k, dict_to_obj(v)) for (k,v) in d.iterite... | 2 | 2009-08-20T13:33:26Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,306,518 | <p>Here is another way to implement SilentGhost's original suggestion:</p>
<pre><code>def dict2obj(d):
if isinstance(d, dict):
n = {}
for item in d:
if isinstance(d[item], dict):
n[item] = dict2obj(d[item])
elif isinstance(d[item], (list, tuple)):
n[item] = [dict2obj(elem) for ele... | 2 | 2009-08-20T14:18:43Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 1,334,816 | <p>Building off my answer to "<a href="http://stackoverflow.com/questions/1325673/python-how-to-add-property-to-a-class-dynamically/1333275#1333275" title="python-how-to-add-property-to-a-class-dynamically">python: How to add property to a class dynamically?</a>":</p>
<pre><code>class data(object):
def __init__(se... | 1 | 2009-08-26T13:48:07Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 5,771,818 | <p>I stumbled upon the case I needed to recursively convert a list of dicts to list of objects, so based on Roberto's snippet here what did the work for me:</p>
<pre><code>def dict2obj(d):
if isinstance(d, dict):
n = {}
for item in d:
if isinstance(d[item], dict):
n[item... | 3 | 2011-04-24T16:41:54Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 6,573,827 | <p>Taking what I feel are the best aspects of the previous examples, here's what I came up with:</p>
<pre><code>class Struct:
'''The recursive class for building and representing objects with.'''
def __init__(self, obj):
for k, v in obj.iteritems():
if isinstance(v, dict):
setattr(self, k, Struct... | 27 | 2011-07-04T16:12:02Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 6,993,694 | <pre><code>class Struct(object):
"""Comment removed"""
def __init__(self, data):
for name, value in data.iteritems():
setattr(self, name, self._wrap(value))
def _wrap(self, value):
if isinstance(value, (tuple, list, set, frozenset)):
return type(value)([self._wrap(v... | 17 | 2011-08-09T08:58:27Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 9,413,295 | <p>For anyone who happens to stumble upon this question nowadays. In Python 2.6+ there's a
collection helper called <a href="http://docs.python.org/library/collections.html?highlight=collections#namedtuple-factory-function-for-tuples-with-named-fields"><code>namedtuple</code></a>, that can do this for you: </p>
<pre>... | 33 | 2012-02-23T12:43:16Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 11,007,429 | <p>Old Q&A, but I get something more to talk. Seems no one talk about recursive dict. This is my code:</p>
<pre><code>#!/usr/bin/env python
class Object( dict ):
def __init__( self, data = None ):
super( Object, self ).__init__()
if data:
self.__update( data, {} )
def __update... | 4 | 2012-06-13T02:23:07Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 11,091,977 | <pre><code>class Struct(dict):
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name, value):
self[name] = value
def copy(self):
return Struct(dict.copy(self))
</code></pre>
<p>Usa... | 1 | 2012-06-18T22:26:05Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 11,961,837 | <p>Wanted to upload my version of this little paradigm.</p>
<pre><code>class Struct(dict):
def __init__(self,data):
for key, value in data.items():
if isinstance(value, dict):
setattr(self, key, Struct(value))
else:
setattr(self, key, type(value).__init__(value))
dict.__init... | 3 | 2012-08-14T22:38:06Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 12,075,692 | <p>I had some problems with <code>__getattr__</code> not being called so I constructed a new style class version:</p>
<pre><code>class Struct(object):
'''The recursive class for building and representing objects with.'''
class NoneStruct(object):
def __getattribute__(*args):
return Struct.N... | 0 | 2012-08-22T14:44:29Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 14,208,842 | <p>My dictionary is of this format:</p>
<pre><code>addr_bk = {
'person': [
{'name': 'Andrew', 'id': 123, 'email': 'andrew@mailserver.com',
'phone': [{'type': 2, 'number': '633311122'},
{'type': 0, 'number': '97788665'}]
},
{'name': 'Tom', 'id': 456,
'pho... | 0 | 2013-01-08T05:40:02Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 15,589,291 | <p>If you want to access dict keys as an object (or as a dict for difficult keys), do it recursively, and also be able to update the original dict, you could do:</p>
<pre><code>class Dictate(object):
"""Object view of a dict, updating the passed in dict when values are set
or deleted. "Dictate" the contents of... | 10 | 2013-03-23T16:41:48Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 15,882,327 | <p>If your dict is coming from <code>json.loads()</code>, you can turn it into an object instead (rather than a dict) in one line:</p>
<pre><code>import json
from collections import namedtuple
json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
</code></pre>
<p>See also <a href="http://sta... | 15 | 2013-04-08T14:53:46Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 16,372,505 | <p>How about this:</p>
<pre><code>from functools import partial
d2o=partial(type, "d2o", ())
</code></pre>
<p>This can then be used like this:</p>
<pre><code>>>> o=d2o({"a" : 5, "b" : 3})
>>> print o.a
5
>>> print o.b
3
</code></pre>
| 1 | 2013-05-04T08:58:39Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 18,357,162 | <pre><code>from mock import Mock
d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
my_data = Mock(**d)
# We got
# my_data.a == 1
</code></pre>
| 2 | 2013-08-21T12:15:22Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 19,218,587 | <p>I think a dict consists of number, string and dict is enough most time.
So I ignore the situation that tuples, lists and other types not appearing in the final dimension of a dict. </p>
<p>Considering inheritance, combined with recursion, it solves the print problem conveniently and also provides two ways to query ... | 2 | 2013-10-07T06:47:22Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 19,511,263 | <p>This is another, alternative, way to convert a list of dictionaries to an object:</p>
<pre><code>def dict2object(in_dict):
class Struct(object):
def __init__(self, in_dict):
for key, value in in_dict.items():
if isinstance(value, (list, tuple)):
setattr(
... | 0 | 2013-10-22T07:06:47Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 24,852,544 | <p>Surprisingly no one has mentioned <a href="https://github.com/dsc/bunch">Bunch</a>. This library is exclusively meant to provide attribute style access to dict objects and does exactly what the OP wants. A demonstration:</p>
<pre><code>>>> from bunch import bunchify
>>> d = {'a': 1, 'b': {'c': 2},... | 37 | 2014-07-20T16:30:31Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 26,805,003 | <p>This little class never gives me any problem, just extend it and use the copy() method:</p>
<pre><code> import simplejson as json
class BlindCopy(object):
def copy(self, json_str):
dic = json.loads(json_str)
for k, v in dic.iteritems():
if hasattr(self, k):
setat... | 0 | 2014-11-07T15:51:26Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 31,569,634 | <p>I ended up trying BOTH the <a href="https://github.com/bcj/AttrDict" rel="nofollow">AttrDict</a> and the <a href="https://github.com/dsc/bunch" rel="nofollow">Bunch</a> libraries and found them to be way to slow for my uses. After a friend and I looked into it, we found that the main method for writing these librar... | 3 | 2015-07-22T17:06:05Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 32,210,017 | <p>Here is a nested-ready version with namedtuple:</p>
<pre><code>from collections import namedtuple
class Struct(object):
def __new__(cls, data):
if isinstance(data, dict):
return namedtuple(
'Struct', data.iterkeys()
)(
*(Struct(val) for val in dat... | 0 | 2015-08-25T17:06:23Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 34,997,118 | <p>You can leverage the <strong><code>json</code> module</strong> of the standard library with a <strong>custom object hook</strong>:</p>
<pre><code>import json
class obj(object):
def __init__(self, dict_):
self.__dict__.update(dict_)
def dict2obj(d):
return json.loads(json.dumps(d), object_hook=obj)... | 0 | 2016-01-25T16:11:19Z | [
"python",
"object",
"dictionary"
] |
Convert Python dict to object? | 1,305,532 | <p>I'm searching for an elegant way to convert a normal Python dict with some nested dicts to an object.</p>
<p>For example:</p>
<pre><code>>>> d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}
</code></pre>
<p>Should be accessible in this way:</p>
<pre><code>>>> x = dict2obj(d)
>>>... | 322 | 2009-08-20T11:28:18Z | 37,231,623 | <p>What about just assigning your <code>dict</code> to the <code>__dict__</code> of an empty object?</p>
<pre><code>class Object:
"""If your dict is "flat", this is a simple way to create an object from a dict
>>> obj = Object()
>>> obj.__dict__ = d
>>> d.a
1
"""
... | 0 | 2016-05-14T20:40:46Z | [
"python",
"object",
"dictionary"
] |
How to do Obj-C Categories in Python? | 1,305,632 | <p>Obj-C (which I have not used for a long time) has something called <a href="http://macdevelopertips.com/objective-c/objective-c-categories.html">categories</a> to extend classes. Declaring a category with new methods and compiling it into your program, all instances of the class suddenly have the new methods.</p>
<... | 8 | 2009-08-20T11:48:56Z | 1,305,868 | <p>Why not just add methods dynamically ?</p>
<pre><code>>>> class Foo(object):
>>> pass
>>> def newmethod(instance):
>>> print 'Called:', instance
...
>>> Foo.newmethod = newmethod
>>> f = Foo()
>>> f.newmethod()
Called: <__main__.Foo object at... | 8 | 2009-08-20T12:31:41Z | [
"python"
] |
How to do Obj-C Categories in Python? | 1,305,632 | <p>Obj-C (which I have not used for a long time) has something called <a href="http://macdevelopertips.com/objective-c/objective-c-categories.html">categories</a> to extend classes. Declaring a category with new methods and compiling it into your program, all instances of the class suddenly have the new methods.</p>
<... | 8 | 2009-08-20T11:48:56Z | 1,306,154 | <p>I came up with this implementation of a class decorator. I'm using python2.5 so I haven't actually tested it with decorator syntax (which would be nice), and I'm not sure what it does is really correct. But it looks like this:</p>
<p>pycategories.py</p>
<pre><code>"""
This module implements Obj-C-style categories ... | 2 | 2009-08-20T13:23:00Z | [
"python"
] |
How to do Obj-C Categories in Python? | 1,305,632 | <p>Obj-C (which I have not used for a long time) has something called <a href="http://macdevelopertips.com/objective-c/objective-c-categories.html">categories</a> to extend classes. Declaring a category with new methods and compiling it into your program, all instances of the class suddenly have the new methods.</p>
<... | 8 | 2009-08-20T11:48:56Z | 13,568,709 | <p>Python's <code>setattr</code> function makes this easy.</p>
<pre><code># categories.py
class category(object):
def __init__(self, mainModule, override = True):
self.mainModule = mainModule
self.override = override
def __call__(self, function):
if self.override or function.__name__ ... | 2 | 2012-11-26T16:14:37Z | [
"python"
] |
CheckedListBox used from Python(pywin32) | 1,305,703 | <p>Does anyone know how to get the list of items and check/uncheck items in a CheckedListBox from python?
I've found <a href="http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html" rel="nofollow">this</a> to help me partly on the way. I think I've found the handle for the CheckedListBox(listed as a SysTr... | 0 | 2009-08-20T12:03:27Z | 1,378,449 | <p>By using <a href="http://sourceforge.net/projects/pywinauto/" rel="nofollow">pywinauto</a> I've managed to check items in a checkedlistbox by selecting them twice.</p>
<pre><code>from pywinauto import application
app = application.Application()
app.Form1.CheckedListBox1.Select('item1')
app.Form1.CheckedListBox1.Sel... | 1 | 2009-09-04T10:33:57Z | [
"python",
"windows",
"message",
"pywin32",
"checkedlistbox"
] |
Hook into and log "everything" in the windows message queue | 1,305,847 | <p>Has anyone got a working code example of how to connect to the windows message queue(post/sendMessage) and log all messages there? Preferably in Python.
I'm interrested in this to easier be able to create test-scripts that emulates user input.</p>
| 3 | 2009-08-20T12:26:22Z | 1,305,991 | <p>Usually this is done with <a href="http://msdn.microsoft.com/en-us/library/ms644990%28VS.85%29.aspx" rel="nofollow">SetWindowsHookEx Function</a>.
<br><br>
In Python you probably must use 3rd party libraries, like <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">Python for Windows extensions</a>.
<a... | 1 | 2009-08-20T12:50:47Z | [
"python",
"windows",
"message-queue"
] |
Hook into and log "everything" in the windows message queue | 1,305,847 | <p>Has anyone got a working code example of how to connect to the windows message queue(post/sendMessage) and log all messages there? Preferably in Python.
I'm interrested in this to easier be able to create test-scripts that emulates user input.</p>
| 3 | 2009-08-20T12:26:22Z | 1,844,722 | <p>There's actually a package that wraps the SetWindowsHookEx function, called <a href="http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main%5FPage" rel="nofollow">pyHook</a>. I've used it before to write a primitive key-logger (as an experiment in monitoring myself to assist with reporting work hours) an... | 2 | 2009-12-04T03:51:39Z | [
"python",
"windows",
"message-queue"
] |
Hook into and log "everything" in the windows message queue | 1,305,847 | <p>Has anyone got a working code example of how to connect to the windows message queue(post/sendMessage) and log all messages there? Preferably in Python.
I'm interrested in this to easier be able to create test-scripts that emulates user input.</p>
| 3 | 2009-08-20T12:26:22Z | 1,869,913 | <p>You can use <a href="http://msdn.microsoft.com/en-us/library/dd373640%28VS.85%29.aspx" rel="nofollow">SetWinEventHook</a> to catch most system windowing activity. The advantage from traditional hooks is that you can do it from your process, that is, you don't need to write a hooking DLL. Also, when the thread that c... | 1 | 2009-12-08T21:18:26Z | [
"python",
"windows",
"message-queue"
] |
Extracting the To: header from an attachment of an email | 1,306,026 | <p>I am using python to open an email on the server (POP3). Each email has an attachment which is a forwarded email itself. </p>
<p>I need to get the "To:" address out of the attachment. </p>
<p>I am using python to try and help me learn the language and I'm not that good yet ! </p>
<p>The code I have already is thi... | 3 | 2009-08-20T12:56:40Z | 1,307,262 | <p>Without having an email in my inbox that is representative, it's difficult to work this one through (I've never used poplib). Having said that, some things that might help from my little bit of investigation:</p>
<p>First of all, make lots of use of the command line interface to python and the <code>dir()</code> a... | 1 | 2009-08-20T16:11:51Z | [
"python",
"email",
"mime",
"pop3"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.