title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
Why am I getting the error "cannot import name Scanner" when I try to use the mwclient module for Python?
2,307,805
<p>I'm using Python 2.5.2 (because mwclient still only works for 2.x). I've copied the mwclient folder into the /usr/lib/python2.5/site-packages/mwclient folder, and when I run a program that imports mwclient I get this:</p> <pre><code> Traceback (most recent call last): File "get_wiki.py", line 2, in &lt;module&gt...
0
2010-02-21T22:57:57Z
2,321,310
<p>Remove the simplejson subdirectory in mwclient if you already have simplejson installed. Or download the latest version from SVN; it has been fixed.</p>
2
2010-02-23T20:06:01Z
[ "python", "import", "module", "mediawiki", "wikipedia" ]
GAE Task Queue oddness
2,308,050
<p>I have been testing the taskqueue with mixed success. Currently I am using the default queue, in default settings etc etc....</p> <p>I have a test URL setup which inserts about 8 tasks into the queue. With short order, all 8 are completed properly. So far so good.</p> <p>The problem comes up when I re-load that UR...
2
2010-02-22T00:05:36Z
2,402,964
<p>When a task-queue ends in error : I believe it stays in your queue .. Check that</p>
0
2010-03-08T16:48:26Z
[ "python", "google-app-engine", "task-queue" ]
How to limit the heap size?
2,308,091
<p>I sometimes write Python programs which are very difficult to determine how much memory it will use before execution. As such, I sometimes invoke a Python program that tries to allocate massive amounts of RAM causing the kernel to heavily swap and degrade the performance of other running processes.</p> <p>Because o...
35
2010-02-22T00:21:12Z
2,308,104
<p>Have a look at <a href="http://ss64.com/bash/ulimit.html" rel="nofollow">ulimit</a>. It allows resource quotas to be set. May need appropriate kernel settings as well.</p>
0
2010-02-22T00:27:24Z
[ "python", "linux", "memory" ]
How to limit the heap size?
2,308,091
<p>I sometimes write Python programs which are very difficult to determine how much memory it will use before execution. As such, I sometimes invoke a Python program that tries to allocate massive amounts of RAM causing the kernel to heavily swap and degrade the performance of other running processes.</p> <p>Because o...
35
2010-02-22T00:21:12Z
2,308,635
<p>Check out <a href="http://docs.python.org/library/resource.html#resource.setrlimit">resource.setrlimit()</a>. It only works on Unix systems but it seems like it might be what you're looking for, as you can choose a maximum heap size for your process and your process's children with the resource.RLIMIT_DATA paramete...
36
2010-02-22T03:33:53Z
[ "python", "linux", "memory" ]
Find maximum signed short integer in python
2,308,247
<p><strong>How do I get the maximum signed short integer in Python</strong> (i.e. SHRT_MAX in C's limits.h)<strong>?</strong></p> <p>I want to normalize samples from a single channel of a <code>*.wav</code> file, so instead of a bunch of 16-bit signed integers, I want a bunch of floats between 1 and -1. Here's what I...
4
2010-02-22T01:12:08Z
2,308,257
<p>in module sys, sys.maxint. Though I'm not sure that is the correct way to solve your problem.</p>
1
2010-02-22T01:16:24Z
[ "python", "struct" ]
Find maximum signed short integer in python
2,308,247
<p><strong>How do I get the maximum signed short integer in Python</strong> (i.e. SHRT_MAX in C's limits.h)<strong>?</strong></p> <p>I want to normalize samples from a single channel of a <code>*.wav</code> file, so instead of a bunch of 16-bit signed integers, I want a bunch of floats between 1 and -1. Here's what I...
4
2010-02-22T01:12:08Z
2,308,305
<p>GregS is right, this is not the right way to solve the problem. If your samples are known 8 or 16 bit, you don't want to be dividing them by a number that varies by platform.</p> <p>You may be running into trouble because a signed 16-bit int actually ranges from -32768 to 32767. Dividing by 32767 is going to give...
2
2010-02-22T01:33:15Z
[ "python", "struct" ]
Find maximum signed short integer in python
2,308,247
<p><strong>How do I get the maximum signed short integer in Python</strong> (i.e. SHRT_MAX in C's limits.h)<strong>?</strong></p> <p>I want to normalize samples from a single channel of a <code>*.wav</code> file, so instead of a bunch of 16-bit signed integers, I want a bunch of floats between 1 and -1. Here's what I...
4
2010-02-22T01:12:08Z
2,308,327
<p>Here is a way using <strong>cython</strong></p> <p><strong>getlimit.py</strong></p> <pre><code>import pyximport; pyximport.install() import limits print limits.shrt_max </code></pre> <p><strong>limits.pyx</strong></p> <pre><code>import cython cdef extern from "limits.h": cdef int SHRT_MAX shrt_max = SHRT_M...
1
2010-02-22T01:41:00Z
[ "python", "struct" ]
Find maximum signed short integer in python
2,308,247
<p><strong>How do I get the maximum signed short integer in Python</strong> (i.e. SHRT_MAX in C's limits.h)<strong>?</strong></p> <p>I want to normalize samples from a single channel of a <code>*.wav</code> file, so instead of a bunch of 16-bit signed integers, I want a bunch of floats between 1 and -1. Here's what I...
4
2010-02-22T01:12:08Z
2,308,332
<p>I can't imagine circumstances on a modern computer (i.e. one that uses 2's complement integers) where this would fail:</p> <pre><code>assert -32768 &lt;= signed_16_bit_integer &lt;= 32767 </code></pre> <p>To do exactly what you asked for:</p> <pre><code>if signed_16_bit_integer &gt;= 0: afloat = signed_16_bit...
0
2010-02-22T01:42:19Z
[ "python", "struct" ]
How to convert a numeric string with place-value commas into an integer?
2,308,443
<p>In Python, what is a clean and elegant way to convert strings like "1,374" or "21,000,000" to int values like 1374 or 21000000?</p>
7
2010-02-22T02:20:57Z
2,308,449
<p><a href="http://docs.python.org/library/locale.html#locale.atoi" rel="nofollow"><code>locale.atoi()</code></a>, after setting an appropriate locale.</p>
4
2010-02-22T02:22:24Z
[ "python" ]
How to convert a numeric string with place-value commas into an integer?
2,308,443
<p>In Python, what is a clean and elegant way to convert strings like "1,374" or "21,000,000" to int values like 1374 or 21000000?</p>
7
2010-02-22T02:20:57Z
2,308,452
<p>It really depends where you get your number from.</p> <p>If the number you are trying to convert comes from user input, use <code>locale.atoi()</code>. That way, the number will be parsed in a way that is consistent with the user's settings and thus expectations.</p> <p>If on the other hand you read it, let's say,...
9
2010-02-22T02:22:30Z
[ "python" ]
How to convert a numeric string with place-value commas into an integer?
2,308,443
<p>In Python, what is a clean and elegant way to convert strings like "1,374" or "21,000,000" to int values like 1374 or 21000000?</p>
7
2010-02-22T02:20:57Z
2,308,455
<pre><code>int("1,374".replace(",","")) </code></pre>
2
2010-02-22T02:23:12Z
[ "python" ]
How to convert a numeric string with place-value commas into an integer?
2,308,443
<p>In Python, what is a clean and elegant way to convert strings like "1,374" or "21,000,000" to int values like 1374 or 21000000?</p>
7
2010-02-22T02:20:57Z
2,308,488
<pre><code>&gt;&gt;&gt; s="1,374" &gt;&gt;&gt; import locale &gt;&gt;&gt; locale.setlocale(locale.LC_NUMERIC, '') 'en_US.UTF-8' &gt;&gt;&gt; locale.atoi(s) 1374 </code></pre>
2
2010-02-22T02:34:07Z
[ "python" ]
How can I have an encrypted input in Python?
2,308,536
<p>I am writing a Python program that requires a user to input their gmail usernames and passwords. When the user types in their password, I want the characters to be displayed as asterisks. Is this possible for a command-line program?</p>
1
2010-02-22T02:54:30Z
2,308,541
<p><a href="http://docs.python.org/library/getpass.html#getpass.getpass" rel="nofollow"><code>getpass.getpass()</code></a> doesn't show asterisks but instead suppresses all output, which is expected behavior on some systems.</p>
4
2010-02-22T02:56:20Z
[ "python" ]
How to create comparison tables in Python
2,308,561
<p>I want to generate comparison tables <a href="http://img714.imageshack.us/img714/5677/22862352.png" rel="nofollow">like this</a> by using Python. </p> <p><img src="http://img714.imageshack.us/img714/5677/22862352.png" alt="alt text"></p> <p>Can you suggest any library to do this?</p>
0
2010-02-22T03:03:03Z
2,308,573
<p>That's surely the output of TeX. It would take all of 60 minutes to learn enough TeX (<a href="http://andy-roberts.net/misc/latex/latextutorial4.html" rel="nofollow">or, better yet, LaTeX</a>) to spit out tables like that.</p>
1
2010-02-22T03:08:16Z
[ "python", "table" ]
How to create comparison tables in Python
2,308,561
<p>I want to generate comparison tables <a href="http://img714.imageshack.us/img714/5677/22862352.png" rel="nofollow">like this</a> by using Python. </p> <p><img src="http://img714.imageshack.us/img714/5677/22862352.png" alt="alt text"></p> <p>Can you suggest any library to do this?</p>
0
2010-02-22T03:03:03Z
7,155,321
<p>Reportlab has some great tools for creating and formatting tables. The only drawback from your perspective is that it outputs to PDF, so you would need an intermediate step to convert the output to PNG. Take a look at the <a href="http://www.reportlab.com/docs/reportlab-userguide.pdf" rel="nofollow">Reportlab User G...
0
2011-08-23T01:14:46Z
[ "python", "table" ]
cx_Oracle, generators, and threads in Python
2,308,698
<p>What is the behavior of cx_Oracle cursors when the connection object is used by different threads? How would generators affect this behavior? Specifically...</p> <p><strong>Edit</strong>: The original example function was incorrect; a generator was being returned by a sub function, <code>yield</code> wasn't used ...
4
2010-02-22T03:54:02Z
2,309,264
<p>See <a href="http://cx-oracle.sourceforge.net/html/module.html#cx_Oracle.threadsafety" rel="nofollow">the docs</a>: <code>threadsafety</code> is, and I quote,</p> <blockquote> <p>Currently 2, which means that threads may share the module and connections, but not cursors.</p> </blockquote> <p>So your "pool of...
2
2010-02-22T06:47:50Z
[ "python", "multithreading", "yield", "generator", "cx-oracle" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,308,887
<pre><code>print '\n'.join('\t'.join(x) for x in L) </code></pre>
8
2010-02-22T04:56:02Z
[ "python", "list", "csv", "export", "tuples" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,308,890
<p>Try this</p> <pre><code>"\n".join(map("\t".join,l)) </code></pre> <p>Test</p> <pre><code>&gt;&gt;&gt; l = [ ('one', 'two', 'three'), ('four', 'five', 'six')] &gt;&gt;&gt; print "\n".join(map("\t".join,l)) one two three four five six &gt;&gt;&gt; </code></pre>
2
2010-02-22T04:56:31Z
[ "python", "list", "csv", "export", "tuples" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,308,893
<pre><code>open("data.txt", "w").write("\n".join(("\t".join(item)) for item in list)) </code></pre>
2
2010-02-22T04:56:58Z
[ "python", "list", "csv", "export", "tuples" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,308,899
<p>The most idiomatic way, IMHO, is to use a list comprehension and a join:</p> <pre><code>print '\n'.join('\t'.join(i) for i in l) </code></pre>
1
2010-02-22T04:59:26Z
[ "python", "list", "csv", "export", "tuples" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,308,915
<p>You can solve it, as other answers suggest by just joining lines but better way would be to just use python csv module, so that later on you can easily change delimter or add header etc and read it back too, looks like you want tab delimited file</p> <pre><code>import sys import csv csv_writer = csv.writer(sys.std...
9
2010-02-22T05:03:50Z
[ "python", "list", "csv", "export", "tuples" ]
Export list as .txt (Python)
2,308,883
<p>My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so:</p> <pre><code>list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] </code></pre> <p>How do I print the list so each tuple item is separated by a tab and each tuple is sep...
0
2010-02-22T04:54:06Z
2,481,039
<p>You don't have to join the list in advance:</p> <pre><code>with open("output.txt", "w") as fp: fp.writelines('%s\n' % '\t'.join(items) for items in a_list) </code></pre>
0
2010-03-19T22:52:04Z
[ "python", "list", "csv", "export", "tuples" ]
Multiple value checks using 'in' operator (Python)
2,308,944
<pre><code>if 'string1' in line: ... </code></pre> <p>... works as expected but what if I need to check multiple strings like so:</p> <pre><code>if 'string1' or 'string2' or 'string3' in line: ... </code></pre> <p>... doesn't seem to work.</p>
4
2010-02-22T05:12:01Z
2,308,950
<pre><code>if any(s in line for s in ('string1', 'string2', ...)): </code></pre>
15
2010-02-22T05:13:21Z
[ "python" ]
Multiple value checks using 'in' operator (Python)
2,308,944
<pre><code>if 'string1' in line: ... </code></pre> <p>... works as expected but what if I need to check multiple strings like so:</p> <pre><code>if 'string1' or 'string2' or 'string3' in line: ... </code></pre> <p>... doesn't seem to work.</p>
4
2010-02-22T05:12:01Z
2,308,962
<pre><code>if 'string1' in line or 'string2' in line or 'string3' in line: </code></pre> <p>Would that be fine for what you need to do?</p>
1
2010-02-22T05:17:09Z
[ "python" ]
Multiple value checks using 'in' operator (Python)
2,308,944
<pre><code>if 'string1' in line: ... </code></pre> <p>... works as expected but what if I need to check multiple strings like so:</p> <pre><code>if 'string1' or 'string2' or 'string3' in line: ... </code></pre> <p>... doesn't seem to work.</p>
4
2010-02-22T05:12:01Z
2,308,965
<p><code>or</code> does not behave that way. <code>'string1' or 'string2' or 'string3' in line</code> is equivalent to <code>('string1') or ('string2') or ('string3' in line)</code>, which will always return true (actually, <code>'string1'</code>). </p> <p>To get the behavior you want, you can say <code>if any(s in l...
1
2010-02-22T05:17:45Z
[ "python" ]
Multiple value checks using 'in' operator (Python)
2,308,944
<pre><code>if 'string1' in line: ... </code></pre> <p>... works as expected but what if I need to check multiple strings like so:</p> <pre><code>if 'string1' or 'string2' or 'string3' in line: ... </code></pre> <p>... doesn't seem to work.</p>
4
2010-02-22T05:12:01Z
2,308,977
<p>If you read the expression like this</p> <pre><code>if ('string1') or ('string2') or ('string3' in line): </code></pre> <p>The problem becomes obvious. What will happen is that 'string1' evaluates to True so the rest of the expression is shortcircuited.</p> <p>The long hand way to write it is this</p> <pre><code...
2
2010-02-22T05:21:12Z
[ "python" ]
why class creation throws error
2,308,945
<p>Just playin around class</p> <pre><code>def func(*args, **kwargs): print args, kwargs class Klass(func): pass </code></pre> <p>it throws error</p> <blockquote> <p>TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str</p> </blockquote> <p>What does it mean, i...
2
2010-02-22T05:12:08Z
2,309,055
<p>see here for the reason for the cryptic msg</p> <p><a href="http://bugs.python.org/issue6829" rel="nofollow">http://bugs.python.org/issue6829</a></p> <p>questions <a href="http://stackoverflow.com/questions/2231427/error-when-calling-the-metaclass-bases-function-argument-1-must-be-code-not-s/2231478">http://stacko...
2
2010-02-22T05:45:01Z
[ "python", "class" ]
IMEI number of a mobile phone using python
2,309,108
<p>How can I get the IMEI number of a mobile phone using Python?</p> <p>Thanks in advance. Nimmy</p>
1
2010-02-22T06:02:23Z
2,309,202
<p>Sending <code>AT+CGSN</code> through the appropriate serial device will have it return the IMEI.</p>
0
2010-02-22T06:29:44Z
[ "python", "mobile-phones", "imei" ]
Get class in Python decorator
2,309,124
<p>In this code:</p> <pre><code>def online_only(func, self): def f(*args, **kwargs): if self.running: return func(*args, **kwargs) else: return False return f class VM(object): @property def running(self): return True @property @online_only def disk...
3
2010-02-22T06:07:11Z
2,309,176
<ol> <li>You can not have two arguments in <code>def online_only(func, self)</code> ? it will raise TypeError, so change it to <code>def online_only(func)</code></li> <li>The first argument to wrapped function would be self, you can just use that e.g.</li> </ol> <hr> <pre><code>def online_only(func): def f(self)...
1
2010-02-22T06:22:38Z
[ "python" ]
Get class in Python decorator
2,309,124
<p>In this code:</p> <pre><code>def online_only(func, self): def f(*args, **kwargs): if self.running: return func(*args, **kwargs) else: return False return f class VM(object): @property def running(self): return True @property @online_only def disk...
3
2010-02-22T06:07:11Z
2,309,187
<p><code>self</code> is passed as the first parameter to the wrapping function, so just treat the first parameter specially in <code>f</code>:</p> <pre><code>def online_only(func): def f(self, *args, **kwargs): if self.running: return func(self, *args, **kwargs) else: return...
2
2010-02-22T06:25:32Z
[ "python" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
2,309,334
<p>You can cast it to a list, insert the item, then cast it back to a tuple.</p> <pre><code>a = ('Product', '500.00', '1200.00') a = list(a) a.insert(3, 'foobar') a = tuple(a) print a &gt;&gt; ('Product', '500.00', '1200.00', 'foobar') </code></pre>
26
2010-02-22T07:05:46Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
2,309,338
<p>Since tuples are immutable, this will result in a new tuple. Just place it back where you got the old one.</p> <pre><code>sometuple + (someitem,) </code></pre>
29
2010-02-22T07:07:03Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
2,309,343
<p>one way is to convert it to list</p> <pre><code>&gt;&gt;&gt; b=list(mytuple) &gt;&gt;&gt; b.append("something") &gt;&gt;&gt; a=tuple(b) </code></pre>
2
2010-02-22T07:07:23Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
2,309,350
<p>You absolutely need to make a new tuple -- then you can rebind the name (or whatever reference[s]) from the old tuple to the new one. The <code>+=</code> operator can help (if there was only one reference to the old tuple), e.g.:</p> <pre><code>thetup += ('1200.00',) </code></pre> <p>does the appending and rebind...
13
2010-02-22T07:09:40Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
2,309,353
<p>For the case where you are not adding to the end of the tuple</p> <pre><code>&gt;&gt;&gt; a=(1,2,3,5,6) &gt;&gt;&gt; a=a[:3]+(4,)+a[3:] &gt;&gt;&gt; a (1, 2, 3, 4, 5, 6) &gt;&gt;&gt; </code></pre>
5
2010-02-22T07:10:24Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
31,571,003
<pre><code>def tuple_insert(tup,pos,ele): tup = tup[:pos]+(ele,)+tup[pos:] return tup tuple_insert(tup,pos,9999) </code></pre> <p><strong>tup: tuple<br/> pos: Position to insert<br/> ele: Element to insert</strong></p>
8
2015-07-22T18:18:47Z
[ "python", "insert", "tuples" ]
Inserting an item in a Tuple
2,309,329
<p>Yes, I understand tuples are immutable but the situation is such that I need to insert an extra value into each tuple. So one of the items is the amount, I need to add a new item next to it in a different currency, like so:</p> <pre><code>('Product', '500.00', '1200.00') </code></pre> <p>Possible?</p> <p>Thanks!<...
18
2010-02-22T07:03:52Z
39,382,532
<p>You can code simply like this as well:</p> <pre><code>T += (new_element,) </code></pre>
0
2016-09-08T04:19:26Z
[ "python", "insert", "tuples" ]
Python equivalent of std::set and std::multimap
2,309,419
<p>I'm porting a C++ program to Python. There are some places where it uses <code>std::set</code> to store objects that define their own comparison operators. Since the Python standard library has no equivalent of <code>std::set</code> (a sorted key-value mapping data structure) I tried using a normal dictionary and th...
10
2010-02-22T07:28:26Z
2,309,456
<p>Python does not have built-in data-structures for this, though the <code>bisect</code> module provides functionality for keeping a sorted list with appropriately efficient algorithms.</p> <p>If you have a list of sorted keys, you can couple it with a <code>collections.defaultdict(list)</code> to provide multimap-li...
2
2010-02-22T07:37:11Z
[ "c++", "python", "performance", "data-structures", "dictionary" ]
Python equivalent of std::set and std::multimap
2,309,419
<p>I'm porting a C++ program to Python. There are some places where it uses <code>std::set</code> to store objects that define their own comparison operators. Since the Python standard library has no equivalent of <code>std::set</code> (a sorted key-value mapping data structure) I tried using a normal dictionary and th...
10
2010-02-22T07:28:26Z
2,309,574
<p>In his book "<a href="http://www.qtrac.eu/py3book.html" rel="nofollow">Programming in Python 3</a>", Mark Summerfield introduces a sorted dictionary class. The source code is available in <a href="http://www.qtrac.eu/py3book30.zip" rel="nofollow">this zip archive</a> - look for SortedDict.py. The SortedDict class is...
0
2010-02-22T08:10:39Z
[ "c++", "python", "performance", "data-structures", "dictionary" ]
Python equivalent of std::set and std::multimap
2,309,419
<p>I'm porting a C++ program to Python. There are some places where it uses <code>std::set</code> to store objects that define their own comparison operators. Since the Python standard library has no equivalent of <code>std::set</code> (a sorted key-value mapping data structure) I tried using a normal dictionary and th...
10
2010-02-22T07:28:26Z
2,309,605
<p>You should use <code>sort(key=...)</code>.<br> The key function you use will be related to the cmp you are already using. The advantage is that the key function is called n times whereas the cmp is called nlog n times, and typically key does half the work that cmp does</p> <p>If you can include your <code>__cmp__()...
5
2010-02-22T08:18:07Z
[ "c++", "python", "performance", "data-structures", "dictionary" ]
Python equivalent of std::set and std::multimap
2,309,419
<p>I'm porting a C++ program to Python. There are some places where it uses <code>std::set</code> to store objects that define their own comparison operators. Since the Python standard library has no equivalent of <code>std::set</code> (a sorted key-value mapping data structure) I tried using a normal dictionary and th...
10
2010-02-22T07:28:26Z
2,309,635
<p>For the sorted dictionary, you can (ab)use the stable nature of python's timsort: basically, keep the items partially sorted, append items at the end when needed, switching a "dirty" flag, and sort the remaining before iterating. See this entry for details and implementation (A Martelli's answer): <a href="http://st...
4
2010-02-22T08:23:48Z
[ "c++", "python", "performance", "data-structures", "dictionary" ]
How do I print out which arguments a Python function requires/allows?
2,309,645
<p>Suppose I have a function and I want to print out the arguments it accepts. How can I do this?</p>
3
2010-02-22T08:26:53Z
2,309,660
<p>Use <a href="http://docs.python.org/library/inspect.html#inspect.getargspec"><code>inspect.getargspec()</code></a> to find out.</p>
6
2010-02-22T08:29:34Z
[ "python", "function", "arguments", "argument-passing" ]
How do I print out which arguments a Python function requires/allows?
2,309,645
<p>Suppose I have a function and I want to print out the arguments it accepts. How can I do this?</p>
3
2010-02-22T08:26:53Z
2,309,739
<p>I see that someone has already offered the answer i had in mind, so i'll suggest a purely practical one. <strong>IDLE</strong> will give you a function's parameters as a 'tooltip'. </p> <p>This should be enabled by default; the tooltip will appear just after you type the function name and the left parenthesis. </p>...
2
2010-02-22T08:48:50Z
[ "python", "function", "arguments", "argument-passing" ]
How do I print out which arguments a Python function requires/allows?
2,309,645
<p>Suppose I have a function and I want to print out the arguments it accepts. How can I do this?</p>
3
2010-02-22T08:26:53Z
2,309,864
<p>if you use IPython (as you absolutely should), use</p> <pre><code>foo? </code></pre> <p>to see the documentation, including what the function expects, and:</p> <pre><code>foo?? </code></pre> <p>to see the above documentation plus the source code (if available)</p>
-1
2010-02-22T09:16:10Z
[ "python", "function", "arguments", "argument-passing" ]
How do I print out which arguments a Python function requires/allows?
2,309,645
<p>Suppose I have a function and I want to print out the arguments it accepts. How can I do this?</p>
3
2010-02-22T08:26:53Z
2,310,447
<p>The <code>help</code> function does this.</p> <p>All you have to do is put in docstrings for your functions.</p>
0
2010-02-22T11:11:28Z
[ "python", "function", "arguments", "argument-passing" ]
How to Include xml file as .py source file
2,309,783
<p>I am having a xml file and i am reading the data from that xml file. But the problem is that i am making an exe file from all the available .py files (using py2exe).</p> <p>i dont want to distribute my xml file along with my exe ( because of securiy reasons) as standalone xml file.I want my xml file should be part ...
0
2010-02-22T09:02:11Z
2,309,802
<p>One thing you can do is include it as a string in a module:</p> <pre><code>bigxml = '''&lt;?xml ... .... .... .... &lt;/topelement&gt;''' </code></pre> <p>Then just import it:</p> <pre><code>from xmldoc import bigxml </code></pre>
2
2010-02-22T09:05:07Z
[ "python" ]
Get rid of leading zeros for date strings in Python?
2,309,828
<p>Is there a nimble way to get rid of leading zeros for date strings in Python?</p> <p>In the example below I'd like to get 12/1/2009 in return instead of 12/01/2009. I guess I could use regular expressions. But to me that seems like overkill. Is there a better solution?</p> <pre><code>&gt;&gt;&gt; time.strftime('%m...
14
2010-02-22T09:10:29Z
2,309,884
<p>I'd suggest a very simple regular expression. It's not like this is performace-critical, is it? </p> <p>Search for <code>\b0</code> and replace with nothing.</p> <p>I. e.:</p> <pre><code>import re newstring = re.sub(r"\b0","",time.strftime('%m/%d/%Y',time.strptime('12/1/2009', '%m/%d/%Y'))) </code></pre>
3
2010-02-22T09:19:40Z
[ "python", "date", "leading-zero" ]
Get rid of leading zeros for date strings in Python?
2,309,828
<p>Is there a nimble way to get rid of leading zeros for date strings in Python?</p> <p>In the example below I'd like to get 12/1/2009 in return instead of 12/01/2009. I guess I could use regular expressions. But to me that seems like overkill. Is there a better solution?</p> <pre><code>&gt;&gt;&gt; time.strftime('%m...
14
2010-02-22T09:10:29Z
2,309,910
<p>@OP, it doesn't take much to do a bit of string manipulation.</p> <pre><code>&gt;&gt;&gt; t=time.strftime('%m/%d/%Y',time.strptime('12/1/2009', '%m/%d/%Y')) &gt;&gt;&gt; '/'.join( map( str, map(int,t.split("/")) ) ) '12/1/2009' </code></pre>
7
2010-02-22T09:24:15Z
[ "python", "date", "leading-zero" ]
Get rid of leading zeros for date strings in Python?
2,309,828
<p>Is there a nimble way to get rid of leading zeros for date strings in Python?</p> <p>In the example below I'd like to get 12/1/2009 in return instead of 12/01/2009. I guess I could use regular expressions. But to me that seems like overkill. Is there a better solution?</p> <pre><code>&gt;&gt;&gt; time.strftime('%m...
14
2010-02-22T09:10:29Z
2,309,945
<p>A simpler and readable solution is to format it yourself:</p> <pre><code>&gt;&gt;&gt; d = datetime.datetime.now() &gt;&gt;&gt; "%d/%d/%d"%(d.month, d.day, d.year) 4/8/2012 </code></pre>
21
2010-02-22T09:33:15Z
[ "python", "date", "leading-zero" ]
Get rid of leading zeros for date strings in Python?
2,309,828
<p>Is there a nimble way to get rid of leading zeros for date strings in Python?</p> <p>In the example below I'd like to get 12/1/2009 in return instead of 12/01/2009. I guess I could use regular expressions. But to me that seems like overkill. Is there a better solution?</p> <pre><code>&gt;&gt;&gt; time.strftime('%m...
14
2010-02-22T09:10:29Z
2,309,961
<pre><code>&gt;&gt;&gt; time.strftime('%-m/%-d/%Y',time.strptime('8/1/2009', '%m/%d/%Y')) '8/1/2009' </code></pre> <p>However, I suspect this is dependent on the system's <code>strftime()</code> implementation and might not be fully portable to all platforms, if that matters to you.</p>
2
2010-02-22T09:35:22Z
[ "python", "date", "leading-zero" ]
Downloading Image
2,309,829
<p>I used urllib2.build_opener() to download an image from a corresponding url.But for a particular url I am getting an error. When I checked that url, I saw that there is no image. How can I check whether there is an image or not? This is my code:</p> <pre><code>opener1 = urllib2.build_opener() page1=opener1.open(...
2
2010-02-22T09:10:29Z
2,309,857
<p>I don't understand. Why just not catch the error with the try and except keywords?</p>
1
2010-02-22T09:14:54Z
[ "python" ]
Downloading Image
2,309,829
<p>I used urllib2.build_opener() to download an image from a corresponding url.But for a particular url I am getting an error. When I checked that url, I saw that there is no image. How can I check whether there is an image or not? This is my code:</p> <pre><code>opener1 = urllib2.build_opener() page1=opener1.open(...
2
2010-02-22T09:10:29Z
2,309,859
<p>By checking the <code>code</code> attribute of the <a href="http://docs.python.org/library/urllib2.html#urllib2.HTTPError" rel="nofollow">exception</a>.</p>
0
2010-02-22T09:15:05Z
[ "python" ]
Downloading Image
2,309,829
<p>I used urllib2.build_opener() to download an image from a corresponding url.But for a particular url I am getting an error. When I checked that url, I saw that there is no image. How can I check whether there is an image or not? This is my code:</p> <pre><code>opener1 = urllib2.build_opener() page1=opener1.open(...
2
2010-02-22T09:10:29Z
2,309,920
<p>as others have suggested catch the exception and check for code e.g.</p> <pre><code>import urllib2 opener1 = urllib2.build_opener() try: page1=opener1.open("http://www.google.com/nosuchimage") my_picture=page1.read() except urllib2.HTTPError,e: if e.code == 404: print "no such image" else: ...
1
2010-02-22T09:29:08Z
[ "python" ]
Downloading Image
2,309,829
<p>I used urllib2.build_opener() to download an image from a corresponding url.But for a particular url I am getting an error. When I checked that url, I saw that there is no image. How can I check whether there is an image or not? This is my code:</p> <pre><code>opener1 = urllib2.build_opener() page1=opener1.open(...
2
2010-02-22T09:10:29Z
2,309,923
<pre><code>try: page1=opener1.open(orginal) except HTTPError, e: if e.code == 404: # Only one of the many possible errors... print "Resource does not exist" raise my_picture=page1.read() </code></pre> <p>see also <a href="http://www.voidspace.org.uk/python/articles/urllib2.shtml#httperror" rel="n...
0
2010-02-22T09:29:34Z
[ "python" ]
Modifying the `clear` attribute of an image with TinyMCE
2,309,894
<p>I'm using TinyMCE with the <code>tinymce-django</code> app in my Django website. I am using it in the admin interface to edit HTML fields. (Something like a flatpage.)</p> <p>When adding images with TinyMCE, how can I change their <code>clear</code> style attribute?</p>
0
2010-02-22T09:21:52Z
2,331,610
<p>You need to have clear included in the <a href="http://wiki.moxiecode.com/index.php/TinyMCE%3aConfiguration/valid_elements" rel="nofollow">valid elements</a> rules (or the <a href="http://wiki.moxiecode.com/index.php/TinyMCE%3aConfiguration/extended_valid_elements" rel="nofollow">extended valid elements</a>), or Tin...
0
2010-02-25T04:21:15Z
[ "python", "django", "django-tinymce" ]
Modifying the `clear` attribute of an image with TinyMCE
2,309,894
<p>I'm using TinyMCE with the <code>tinymce-django</code> app in my Django website. I am using it in the admin interface to edit HTML fields. (Something like a flatpage.)</p> <p>When adding images with TinyMCE, how can I change their <code>clear</code> style attribute?</p>
0
2010-02-22T09:21:52Z
2,362,056
<p>First go to the <strong>image popup</strong>. After selecting image, navigate to <strong>Appearance</strong> tab.</p> <p>If you don't see this popup, make sure that the editor mode is <em>advanced</em> and the plugin <em>advimage</em> is included.</p> <p>Now, you have two options to do so:</p> <ol> <li><strong>CS...
2
2010-03-02T09:27:09Z
[ "python", "django", "django-tinymce" ]
Redirect stderr to stdout on exec-ed process from python?
2,309,911
<p>In a bash script, I can write:</p> <pre><code>exec 2&gt;&amp;1 exec someprog </code></pre> <p>And the stderr output of <code>someprog</code> would be redirected to stdout.</p> <p>Is there any way to do a similar thing using python's <a href="http://docs.python.org/library/os.html#os.execl" rel="nofollow"><code>os...
3
2010-02-22T09:25:01Z
2,309,927
<p><a href="http://docs.python.org/library/os.html#os.dup2" rel="nofollow"><code>os.dup2(1, 2)</code></a></p> <h3>Illuminating examples</h3> <p>Let's execute <code>/bin/ls</code> with a bogus argument so that it complains to stderr.</p> <pre> $ python -c "import os; os.execl('/bin/ls', '', 'ffweew')" 1>/dev/null : f...
4
2010-02-22T09:30:08Z
[ "python", "linux", "redirect", "file-descriptor" ]
Integer comparison in python slows everything down to a crawl
2,309,996
<p>The following code is causing me enormous headaches:</p> <pre><code>def extract_by_letters(letters, dictionary): for word in dictionary: for letter in letters: if word.count(letter) != letters.count(letter): if word in dictionary: #I cant leave this line out ...
3
2010-02-22T09:44:20Z
2,310,034
<p>The reason you get the exception is that if the letter count matches for more than one letter, you are trying to remove the word more than once</p> <p>The reason it is so slow is that you have loops inside loops inside loops.</p> <p>If you would write a sentence or two about what the function is supposed to do, it...
4
2010-02-22T09:52:42Z
[ "python", "loops", "integer" ]
Integer comparison in python slows everything down to a crawl
2,309,996
<p>The following code is causing me enormous headaches:</p> <pre><code>def extract_by_letters(letters, dictionary): for word in dictionary: for letter in letters: if word.count(letter) != letters.count(letter): if word in dictionary: #I cant leave this line out ...
3
2010-02-22T09:44:20Z
2,310,407
<p>Try building the output instead of deleting from it:</p> <pre><code>def extract_by_letters(letters, dictionary): d = [] for word in dictionary: for letter in letters: if word.count(letter)&gt;0: d.append(word) break return d </code></pre> <p>Or, you could...
2
2010-02-22T11:03:39Z
[ "python", "loops", "integer" ]
Integer comparison in python slows everything down to a crawl
2,309,996
<p>The following code is causing me enormous headaches:</p> <pre><code>def extract_by_letters(letters, dictionary): for word in dictionary: for letter in letters: if word.count(letter) != letters.count(letter): if word in dictionary: #I cant leave this line out ...
3
2010-02-22T09:44:20Z
2,312,452
<p>Here's a function that should offer a major speed-up:</p> <pre><code>def extract_by_letters(letters,dictionary): letdict = zip(set(letters),[letters.count(let) for let in set(letters)]) outarr = [] for word in dictionary: goodword = True for letter in letdict: if word.count(l...
2
2010-02-22T16:43:24Z
[ "python", "loops", "integer" ]
Integer comparison in python slows everything down to a crawl
2,309,996
<p>The following code is causing me enormous headaches:</p> <pre><code>def extract_by_letters(letters, dictionary): for word in dictionary: for letter in letters: if word.count(letter) != letters.count(letter): if word in dictionary: #I cant leave this line out ...
3
2010-02-22T09:44:20Z
2,313,824
<p>You're trying to find all anagrams of 'letters'?</p> <pre><code>def anagrams(letters, words): letters = sorted(letters) result = [] for word in words: if sorted(word.strip()) == letters: result.append(word) return result </code></pre>
0
2010-02-22T20:12:33Z
[ "python", "loops", "integer" ]
Integer comparison in python slows everything down to a crawl
2,309,996
<p>The following code is causing me enormous headaches:</p> <pre><code>def extract_by_letters(letters, dictionary): for word in dictionary: for letter in letters: if word.count(letter) != letters.count(letter): if word in dictionary: #I cant leave this line out ...
3
2010-02-22T09:44:20Z
2,315,238
<pre><code>import pprint from collections import defaultdict # This is a best approximation to what Bryan is trying to do. # However the results are meaningless because the list is being # mutated during iteration over it. So I haven't shown the output. def extract_by_letters_0(letters, input_list): dictionary = ...
1
2010-02-23T00:02:43Z
[ "python", "loops", "integer" ]
file manipulation and find a word and tricky replace
2,310,014
<p>I have file something like this </p> <pre><code>hostname ser1-xyz myuser name passwd secret group 1234 hostname ser2-xyz myuser name passwd secret group 2345 </code></pre> <p>I need to find the line first appearance of host named "ser1-xyz" and modify it as "ser1" and increment it's the group value...
1
2010-02-22T09:49:32Z
2,310,047
<p>one way</p> <pre><code>import fileinput f=0 for line in fileinput.input("file",inplace=0): if "hostname" in line and "ser1-xyz" in line: line=line.replace("ser1-xyz","ser1") f=1 if f and "group" in line: a=line.rstrip().split(" ") a[-1]=str(int(a[-1])+1) line=' '.join(a) ...
2
2010-02-22T09:55:50Z
[ "python", "file", "replace" ]
Reading file from concatinated ( tar ) file directly without untarring the tar file
2,310,100
<p>Hi i am having a one xml file and some image files, i am making my one concatenate file (ie as like tar file) from all these file (i am having my own scripts for tarring and untarring). </p> <p>before i describe what exactly i want you have to look the current situation.</p> <p>As of now i have to untar the all fi...
0
2010-02-22T10:06:50Z
2,310,131
<p>The <a href="http://docs.python.org/library/tarfile.html" rel="nofollow"><code>tarfile</code></a> module gives you access to tarballs. It won't be random access, but you can read out any files you need and put them in a temporary directory, or just store them in strings.</p>
2
2010-02-22T10:11:33Z
[ "python" ]
urlfetch.fetch got missed content within script tag of html in app engine
2,310,134
<p>in my app, when I use urlfetch.fetch function to fetch a specified url, I found the content I got is empty for some special html codes.</p> <p>for example, the url: <a href="http://www.club.cn.sodexo.com/node/5071" rel="nofollow">http://www.club.cn.sodexo.com/node/5071</a> in real html source, the codes including: ...
2
2010-02-22T10:12:04Z
5,495,133
<p>Perhaps the server inserts different javascript based on the user-agent (ie different code for Firefox verses Internet Explorer), and for <code>urlfetch</code>'s user-agent the server gets confused and doesn't insert any javascript at all.</p>
0
2011-03-31T03:49:34Z
[ "python", "google-app-engine", "fetch", "urlfetch" ]
When I use httplib for my OAUTH in Python, I always get "CannotSendRequest" and then "
2,310,143
<pre><code>Traceback: File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response 92. response = callback(request, *callback_args, **callback_kwargs) File "/home/ea/ea/hell/life/views.py" in linkedin_auth 274. token = oauth_linkedin.get_unauthorised_request_token()...
1
2010-02-22T10:13:14Z
3,336,604
<p>Apparently (from <a href="http://stackoverflow.com/questions/1925639/httplib-cannotsendrequest-error-in-wsgi">here</a>) this happens if you try to reuse an <code>httplib.HTTP</code> object which had not been fully used. Maybe a connection pool in the library you're using, and an exception getting thrown during the ...
0
2010-07-26T16:00:20Z
[ "python", "django", "connection", "oauth", "httplib" ]
running a .py by double-clicking is not working
2,310,189
<p>I'm using Windows XP.</p> <p>When I double click the Launch_PyDemos.pyw from the book Programming Python, nothing happens. When I try to run Launch_PyDemos.pyw from command-line, I get the error message:</p> <pre><code>Traceback (most recent call last): File "PyDemos2.pyw", line 41, in &lt;module&gt; from PP...
1
2010-02-22T10:19:11Z
2,310,217
<p>You're missing libraries from the book. Quoting <a href="http://bytes.com/topic/python/answers/854927-pp3e-error" rel="nofollow">a bytes thread</a>:</p> <blockquote> <p>Please follow the instructions on the book, or read the README-PP3E.txt file; below I copy the most relevant parts:</p> <p>"""Copy the...
3
2010-02-22T10:23:33Z
[ "python" ]
What encryption algorithm would be best for data transfer between Python and Php?
2,310,203
<p>I am writing a client / server based program, Server side is with Php and Client is with Python, and I need to make sure the transfer data is safe both way.</p> <p>So, My question is </p> <p><strong>What encryption algorithm would be best for data transfer between Python and Php?</strong></p> <ul> <li>I couldn't ...
-1
2010-02-22T10:21:12Z
2,310,277
<p>Do not attempt to invent an encryption scheme yourself. This is extremely difficult to get right (even professionals can't do this correctly on a regular basis). The SSL suite of security protocols embodies literally decades of research and implementation experience that you will not be able to reinvent.</p> <p>For...
4
2010-02-22T10:36:15Z
[ "php", "python", "algorithm", "security", "encryption" ]
What encryption algorithm would be best for data transfer between Python and Php?
2,310,203
<p>I am writing a client / server based program, Server side is with Php and Client is with Python, and I need to make sure the transfer data is safe both way.</p> <p>So, My question is </p> <p><strong>What encryption algorithm would be best for data transfer between Python and Php?</strong></p> <ul> <li>I couldn't ...
-1
2010-02-22T10:21:12Z
2,310,279
<p>The answer is usually "it depends". If all you're looking for is symmetric encryption of sufficient quantities of data you'll probably want something like AES. There are however many ways in which you could use encryption that can turn out to be insecure in the end, which is why using https is a good idea since it's...
1
2010-02-22T10:36:22Z
[ "php", "python", "algorithm", "security", "encryption" ]
What encryption algorithm would be best for data transfer between Python and Php?
2,310,203
<p>I am writing a client / server based program, Server side is with Php and Client is with Python, and I need to make sure the transfer data is safe both way.</p> <p>So, My question is </p> <p><strong>What encryption algorithm would be best for data transfer between Python and Php?</strong></p> <ul> <li>I couldn't ...
-1
2010-02-22T10:21:12Z
33,936,561
<p>Greg Hewgill is correct and has the best advice. I tried to invent my own token system, while somewhat successful, it is not what it could be. So i believe SSL is the best way to go.</p> <p>PHP has openssl built into it and such you can easily use that, which is what i am currently using. If you phone your hosting ...
1
2015-11-26T10:44:06Z
[ "php", "python", "algorithm", "security", "encryption" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,459
<p>I don't really understand what you're trying to do exactly, but:</p> <pre><code> 10: CityBean( "name10" , "coord10" , "hero10" )}[self.n]() </code></pre> <p>if calling something (the () at the end make it a function call). </p> <p>What you would want is I think temp = { ... 10: CityBean( "name10" , "c...
1
2010-02-22T11:13:58Z
[ "python", "class", "instance" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,465
<p>Putting parens after a class name in a normal expression instantiates it immediately. If you want to delay instantiation then you'll need to use <a href="http://docs.python.org/tutorial/controlflow.html#lambda-forms" rel="nofollow"><code>lambda</code></a> or <a href="http://docs.python.org/library/functools.html#fun...
0
2010-02-22T11:14:35Z
[ "python", "class", "instance" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,467
<p>Why did your write this?</p> <pre><code>temp = { 1: CityBean( "name21" , "coord21" , "hero21" ), ... blah blah blah 10: CityBean( "name30" , "coord30" , "hero30" ) }[self.n]() </code></pre> <p>What do you think <code>temp= {...}[something]()</code> will do?</p> <ol> <li><p>It ...
3
2010-02-22T11:15:07Z
[ "python", "class", "instance" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,475
<p>your are doing </p> <pre><code>{ 1: CityBean( "name1" , "coord1" , "hero1"), ... blah blah blah 10: CityBean( "name10" , "coord10" , "hero10" )}[self.n]() </code></pre> <p>which is basically from a dict get value based on key, values on your dict are CityBean instances, so in short you are doing this</p> <pre><...
2
2010-02-22T11:16:11Z
[ "python", "class", "instance" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,485
<p>It's really hard to guess what you want from what you provided, since, instead of saying what you want to do, you provided your newbie code, so one has a hard guess time.</p> <p>I'd think something like this would do:</p> <pre><code>data = { (1, 'user1'): ("name1", "coord1", "hero1"), (2, 'user1'):...
6
2010-02-22T11:17:37Z
[ "python", "class", "instance" ]
Python instance has no __call__ method
2,310,432
<p>I've been learning how to use Python for the better part of today after giving up on an ugly bash script. </p> <p>I'm trying to use 2 classes to define a few arrays of objects in which to store some unique strings and integers (1-10). The objects will consist of the following:</p> <pre><code>object[i].user ...
0
2010-02-22T11:08:24Z
2,310,497
<p>Notice, that in python, when you write</p> <pre><code>CityBean( "name1" , "coord1" , "hero1") </code></pre> <p>You initialize the object <code>CityBean</code>. There is no need for an extra <code>()</code>.</p>
1
2010-02-22T11:20:03Z
[ "python", "class", "instance" ]
Python Virtualbox API
2,310,563
<p><a href="http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html" rel="nofollow">http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html</a></p> <p>there is a parameter **kw in <strong>init</strong>(self, **kw) what is it?</p>
0
2010-02-22T11:35:29Z
2,310,581
<p>A dictionary of arbitrary keyword arguments. See <a href="http://docs.python.org/tutorial/controlflow.html#keyword-arguments" rel="nofollow">http://docs.python.org/tutorial/controlflow.html#keyword-arguments</a></p> <p>If it really is a question about that concrete API, then please excuse me, I was typing faster th...
3
2010-02-22T11:38:43Z
[ "python" ]
Python Virtualbox API
2,310,563
<p><a href="http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html" rel="nofollow">http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html</a></p> <p>there is a parameter **kw in <strong>init</strong>(self, **kw) what is it?</p>
0
2010-02-22T11:35:29Z
2,310,586
<p>**kw is short for **kwargs. Additional named aguments.</p>
0
2010-02-22T11:39:15Z
[ "python" ]
Python Virtualbox API
2,310,563
<p><a href="http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html" rel="nofollow">http://enomalism.com/api/pyvb/pyvb.vm.vbVM-class.html</a></p> <p>there is a parameter **kw in <strong>init</strong>(self, **kw) what is it?</p>
0
2010-02-22T11:35:29Z
2,310,589
<p>Looks like it's a dictionary of options from <a href="http://enomalism.com/api/pyvb/pyvb.vm-pysrc.html#vbVM.__init__" rel="nofollow">the source</a>?</p>
0
2010-02-22T11:39:45Z
[ "python" ]
Django Signal via Decorator on Model Method?
2,310,676
<p>I'm trying to do something like <a href="http://code.djangoproject.com/ticket/9015">these proposed signal decorators</a>. In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods.</...
6
2010-02-22T11:58:45Z
2,376,694
<p>It's not clear from your example code, so I'd be asking if the signal listener actually has to be a <code>@classmethod</code>? I.e. Will a regular method do (and then use <code>self.__class__</code> if you still need to access the class itself)? Does it need to be a method at all (can you just use a function)?</p> ...
2
2010-03-04T03:30:32Z
[ "python", "django", "django-models", "decorator", "django-signals" ]
Django Signal via Decorator on Model Method?
2,310,676
<p>I'm trying to do something like <a href="http://code.djangoproject.com/ticket/9015">these proposed signal decorators</a>. In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods.</...
6
2010-02-22T11:58:45Z
3,259,139
<p>Could you make it a @staticmethod instead? That way, you can just swap the order of the decorators.</p> <pre><code>class ModelA(Model): @staticmethod @connect.post_save(ModelB) def observe_model_b_saved(sender, instance, created, **kwargs): # do some stuff pass </code></pre> <p>You'd ...
1
2010-07-15T19:18:20Z
[ "python", "django", "django-models", "decorator", "django-signals" ]
Django Signal via Decorator on Model Method?
2,310,676
<p>I'm trying to do something like <a href="http://code.djangoproject.com/ticket/9015">these proposed signal decorators</a>. In addition to having a decorator that connects the decorated method to a signal (with the signal's sender as an argument to the decorator), I would like to use the decorator on class methods.</...
6
2010-02-22T11:58:45Z
39,039,576
<p>Based off of Matt's answer, the @staticmethod trick worked for me. You can use a string to reference a model non-concretely.</p> <pre><code>class Foo(Model): @staticmethod @receiver(models.signals.post_save, sender='someappname.Foo') def post_save(sender, instance, created, **kwargs): print...
0
2016-08-19T12:53:39Z
[ "python", "django", "django-models", "decorator", "django-signals" ]
Pygame and threading: locked when accessing to globals?
2,310,728
<p>I am programming a game using pygame. I intend to control one of the characters using OpenSoundControl (OSC), a udp-based protocol for realtime communication. Basically I am using <a href="http://www.ixi-audio.net/content/body_backyard_python.html" rel="nofollow">simpleOSC</a> module to biund some OSC commands to fu...
1
2010-02-22T12:09:58Z
2,316,963
<p>I can't verify whether you are in fact having a thread/concurrency issue, although it seems likely. I can suggest a solution that may resolve it.</p> <p>The python multiprocessing module demonstrates how to spawn a new process (not a thread) with a queue. If you create the new process and then init OSC in there, an...
3
2010-02-23T09:24:04Z
[ "python", "multithreading", "pygame", "osc" ]
django-tinymce: Using different options for different instances
2,310,835
<p>I have a model with an <code>HTMLField</code> which can be edited with a TinyMCE control in the admin. However, I would like to be able to give different options to TinyMCE depending on which instance of the model is being edited. How can I do this?</p> <p>(For example, if the user is editing the <code>SimplePage</...
3
2010-02-22T12:37:38Z
2,337,571
<p>I guess you have a <code>Media</code> class in your ModelAdmin with additional JavaScript and CSS for the admin (<a href="http://code.djangoproject.com/wiki/AddWYSIWYGEditor" rel="nofollow">like here</a>). Your JavaScript doesn't know the slug of the current object, let's change that.</p> <p>First create one of the...
1
2010-02-25T20:57:37Z
[ "python", "django", "tinymce", "django-tinymce" ]
plotting lines without blocking execution
2,310,851
<p>I am using matplotlib to draw charts and graphs.</p> <p>When I plot the chart using the command <code>show()</code> my code blocks at this command.</p> <p>I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with...
16
2010-02-22T12:40:56Z
2,310,894
<p>This works by invoking Ipython with the <code>-wthread</code> (or the <code>-pylab</code>) option. It will not block on <code>show</code> anymore.</p>
-1
2010-02-22T12:47:22Z
[ "python", "matplotlib" ]
plotting lines without blocking execution
2,310,851
<p>I am using matplotlib to draw charts and graphs.</p> <p>When I plot the chart using the command <code>show()</code> my code blocks at this command.</p> <p>I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with...
16
2010-02-22T12:40:56Z
2,311,326
<p>If you are not using the IPython shell but instead running a program, you probably want to do:</p> <pre><code>pyplot.draw() </code></pre> <p>after a <code>plot()</code>, possibly followed by</p> <pre><code>raw_input("Press enter when done...") </code></pre> <p>so as to wait for the user before plotting something...
3
2010-02-22T14:05:39Z
[ "python", "matplotlib" ]
plotting lines without blocking execution
2,310,851
<p>I am using matplotlib to draw charts and graphs.</p> <p>When I plot the chart using the command <code>show()</code> my code blocks at this command.</p> <p>I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with...
16
2010-02-22T12:40:56Z
2,312,184
<p>In IPython started with <code>-pylab</code> it should not block.</p> <p>Otherwise: With <code>ion()</code> you turn the interactive mode on. <code>show()</code> does not block your system anymore. Every <code>draw()</code> or <code>plot(x, y)</code> updated your plot.</p> <p><code>ioff()</code> turns interactive m...
12
2010-02-22T16:08:10Z
[ "python", "matplotlib" ]
plotting lines without blocking execution
2,310,851
<p>I am using matplotlib to draw charts and graphs.</p> <p>When I plot the chart using the command <code>show()</code> my code blocks at this command.</p> <p>I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with...
16
2010-02-22T12:40:56Z
36,923,807
<p>On MacOS X i had the problem that unblocking only produced a white screen. In the end @tyleha's suggestion using %pylab directly in the note book helped. In fact it's suggested when using the deprecated the -pylab flag:</p> <pre><code>bash:~/Projects/plyground $ python -m IPython notebook -pylab WARNING: `-pylab` f...
0
2016-04-28T19:27:47Z
[ "python", "matplotlib" ]
Printing tuple data in normal text in Python
2,310,919
<p>I have variable <code>r=(u'East london,London,England', u'Mr.Baker in East london (at 2010-02-21 15:25:27.0)')</code> in this format from webservice as a output from small program. How can I print these tuple data as normal string like: </p> <pre><code>East london,London,England Mr.Baker in East london (at 2010-0...
0
2010-02-22T12:51:27Z
2,310,928
<p>just join on the separator, if it's space it would be:</p> <pre><code>' '.join(r) </code></pre> <p><strong>edit</strong>: re your update code. Your table contains primary key, as can be seen from the table definition, that primary key is an integer. That's why you're getting that <code>TypeError</code>. The questi...
3
2010-02-22T12:54:24Z
[ "python", "tuples" ]
Printing tuple data in normal text in Python
2,310,919
<p>I have variable <code>r=(u'East london,London,England', u'Mr.Baker in East london (at 2010-02-21 15:25:27.0)')</code> in this format from webservice as a output from small program. How can I print these tuple data as normal string like: </p> <pre><code>East london,London,England Mr.Baker in East london (at 2010-0...
0
2010-02-22T12:51:27Z
2,311,095
<p>If the sequence <code>x</code> contains other types than string, convert those first:</p> <pre><code>' '.join( map( str, x ) ) </code></pre>
0
2010-02-22T13:27:04Z
[ "python", "tuples" ]
Determine if string in input is a single word in Python
2,311,044
<p>a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory.</p> <p>So, given that the input can be a full or relative path, or a single word (in...
0
2010-02-22T13:15:41Z
2,311,081
<pre><code>&gt;&gt;&gt; r="Foo bar".split() &gt;&gt;&gt; if len(r) != 1: print "not ok" ... not ok &gt;&gt;&gt; if "/" in "/home/path/to/something": ... print "ok" ... ok </code></pre>
3
2010-02-22T13:24:42Z
[ "python", "string" ]
Determine if string in input is a single word in Python
2,311,044
<p>a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory.</p> <p>So, given that the input can be a full or relative path, or a single word (in...
0
2010-02-22T13:15:41Z
2,311,086
<p>You could tweak your regex to match the whole input string so that you don't have to count the matches. I.e.</p> <pre><code>if re.match(r'\A[\w-]+\Z', word): print "Single word" </code></pre> <p>(compile the regex if you feel so inclined)</p> <p><code>\A</code> and <code>\Z</code> match the beginning and end of...
3
2010-02-22T13:25:04Z
[ "python", "string" ]
Determine if string in input is a single word in Python
2,311,044
<p>a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory.</p> <p>So, given that the input can be a full or relative path, or a single word (in...
0
2010-02-22T13:15:41Z
2,311,097
<p>Just check the string and see if it contains a <code>' '</code> (space) or an <code>'\'</code> (path separator). If it does, then it's not a "single word".</p>
0
2010-02-22T13:27:23Z
[ "python", "string" ]
Determine if string in input is a single word in Python
2,311,044
<p>a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory.</p> <p>So, given that the input can be a full or relative path, or a single word (in...
0
2010-02-22T13:15:41Z
2,311,112
<p>Check the os.path module. You can use <a href="http://docs.python.org/library/os.path.html#os.path.abspath" rel="nofollow">os.path.abspath()</a> to convert a given path into full path (if it is on the same system). </p> <p>You can check with the following snippet if the string has any whitespace characters.</p> <p...
0
2010-02-22T13:30:28Z
[ "python", "string" ]
How can I get python in the command prompt on Windows?
2,311,074
<p>I have just installed Python on my Windows 7. I thought that after that I will be able to run python on the command prompt but it is not the case. After the installation I also found out that I can run the python command shell. This is nice. But what should I do if I want to save my program in a file and then I want...
1
2010-02-22T13:22:44Z
2,311,082
<p>Is python.exe in your windows path? Try to look at the PATH environment variable and see if the installation folder of python is listed there.</p>
0
2010-02-22T13:24:48Z
[ "python", "windows", "command-line", "command-prompt" ]
How can I get python in the command prompt on Windows?
2,311,074
<p>I have just installed Python on my Windows 7. I thought that after that I will be able to run python on the command prompt but it is not the case. After the installation I also found out that I can run the python command shell. This is nice. But what should I do if I want to save my program in a file and then I want...
1
2010-02-22T13:22:44Z
2,311,090
<p>You need to add the python bin directory to your path. Follow the instructions <a href="http://banagale.com/changing-your-system-path-in-windows-vista.htm" rel="nofollow">here</a> and add <code>c:\python26\bin</code> to the path (unless you installed python in a non-default location).</p>
1
2010-02-22T13:26:18Z
[ "python", "windows", "command-line", "command-prompt" ]
How can I get python in the command prompt on Windows?
2,311,074
<p>I have just installed Python on my Windows 7. I thought that after that I will be able to run python on the command prompt but it is not the case. After the installation I also found out that I can run the python command shell. This is nice. But what should I do if I want to save my program in a file and then I want...
1
2010-02-22T13:22:44Z
2,311,098
<p>You need to update your environment variables to include the path to the Python executable. </p> <p>On XP you can do this by right clicking on "My Computer" -> Properties and then going to the "Advanced" tab. </p>
0
2010-02-22T13:27:27Z
[ "python", "windows", "command-line", "command-prompt" ]
How to handle user mangement in Django for groups that has same access but different rules?
2,311,094
<h3>Background information:</h3> <p>I have created an internal site for a company. Most of the work has gone into making calculation tools that their sale persons can use to make offers for clients. Create pdf offers and contracts that can be downloaded, compare prices etc. All of this is working fine.</p> <p>Now the...
2
2010-02-22T13:27:00Z
2,311,419
<p>you could use <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#id8" rel="nofollow">proxy models</a> on the <code>Group</code> and <code>User</code> models that come packed with django.</p> <p>then write your authorization and calculation methods inside the proxy model. if a new group is added later, ...
1
2010-02-22T14:24:35Z
[ "python", "django", "user-management" ]
How to handle user mangement in Django for groups that has same access but different rules?
2,311,094
<h3>Background information:</h3> <p>I have created an internal site for a company. Most of the work has gone into making calculation tools that their sale persons can use to make offers for clients. Create pdf offers and contracts that can be downloaded, compare prices etc. All of this is working fine.</p> <p>Now the...
2
2010-02-22T13:27:00Z
2,311,740
<p>If I'm understanding you correctly, it seems like you want to have two different groups have access to all the same views, but they will see different numbers. You can achieve this effect by making separate templates for the different groups, and then loading the appropriate template for each view depending on the g...
0
2010-02-22T15:07:58Z
[ "python", "django", "user-management" ]
Python ConfigParser: how to work out options set in a specific section (rather than defaults)
2,311,102
<p>I have a config file that I read using the RawConfigParser in the standard ConfigParser library. My config file has a [DEFAULT] section followed by a [specific] section. When I loop through the options in the [specific] section, it includes the ones under [DEFAULT], which is what is meant to happen. </p> <p>However...
3
2010-02-22T13:28:26Z
2,311,146
<p>Doesn't <code>RawConfigParser.has_option(section, option)</code> do the job?</p>
0
2010-02-22T13:36:03Z
[ "python", "configparser" ]