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 |
|---|---|---|---|---|---|---|---|---|---|
Matplotlib coord. sys origin to top left | 1,349,230 | <p>How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).</p>
<p>I'm looking for the equivalent of the matlab comm... | 8 | 2009-08-28T20:40:31Z | 1,350,154 | <p>For an image or contour plot, you can use the keyword <code>origin = None | 'lower' | 'upper'</code> and for a line plot, you can set the ylimits high to low.</p>
<pre><code>from pylab import *
A = arange(25)/25.
A = A.reshape((5,5))
figure()
imshow(A, interpolation='nearest', origin='lower')
figure()
imshow(A, i... | 8 | 2009-08-29T01:31:45Z | [
"python",
"matplotlib",
"axes"
] |
Matplotlib coord. sys origin to top left | 1,349,230 | <p>How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).</p>
<p>I'm looking for the equivalent of the matlab comm... | 8 | 2009-08-28T20:40:31Z | 11,653,129 | <p>The following is a basic way to achieve this</p>
<pre><code>ax=pylab.gca()
ax.set_ylim(ax.get_ylim()[::-1])
</code></pre>
| 4 | 2012-07-25T15:22:55Z | [
"python",
"matplotlib",
"axes"
] |
Matplotlib coord. sys origin to top left | 1,349,230 | <p>How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).</p>
<p>I'm looking for the equivalent of the matlab comm... | 8 | 2009-08-28T20:40:31Z | 35,917,758 | <p>The esisest way is to use:</p>
<pre><code>plt.gca().invert_yaxis()
</code></pre>
<p>Origin works only for imshow.</p>
| 3 | 2016-03-10T13:21:33Z | [
"python",
"matplotlib",
"axes"
] |
Matplotlib coord. sys origin to top left | 1,349,230 | <p>How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).</p>
<p>I'm looking for the equivalent of the matlab comm... | 8 | 2009-08-28T20:40:31Z | 39,923,007 | <p>This</p>
<pre><code>plt.ylim(max(plt.ylim()), min(plt.ylim()))
</code></pre>
<p>has an advantage over this</p>
<pre><code>plt.gca().invert_yaxis()
</code></pre>
<p>and is that if you are in interactive mode and you repeatedly plot the same plot (maybe with updated data and having a breakpoint after the plot) the... | 2 | 2016-10-07T17:37:27Z | [
"python",
"matplotlib",
"axes"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,358 | <p>I assume x() is slower because you're first building the array and then joining it. So you're not only measuring the time that join takes, but also the time that you take to build the array.</p>
<p>In a scenario where you already have an array and you want to create a string out of its elements, join should be fast... | 4 | 2009-08-28T21:05:30Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,526 | <p>Interesting: I've done some tests where the size of the string changes, and this is what I found:</p>
<pre><code>def x():
x = "a" * 100
s=[]
for i in range(100):
# Other codes here...
s.append(x)
return ''.join(s)
def z():
x = "a" * 100
s=''
for i in xrange(100):
... | 1 | 2009-08-28T21:40:15Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,532 | <p>There's a difference between += and + with strings -- if there's no other references to "x", x+=y can just append to x, rather than having to take a copy of the string to append to -- which is the same benefit you get from using "".join().</p>
<p>The main benefit from "".join() over + or += is that join() should al... | 2 | 2009-08-28T21:41:20Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,533 | <p>String concatenation was a lot slower before Python 2.5, when it still created a new copy for every string concatenation rather than appending to the original, leading to join() becoming a popular workaround.</p>
<p>Here's an old benchmark demonstrating the old problem:
<a href="http://www.skymind.com/~ocrow/python... | 0 | 2009-08-28T21:41:25Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,538 | <p>In addition to what the others said, 100 1-char strings is <em>really small</em>. (I'm kind of surprised you get separation of results at all.) That's the sort of dataset that fits in your processor cache. You're not going to see asymptotic performance on a microbenchmark.</p>
| 1 | 2009-08-28T21:42:26Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,598 | <p>As to why <code>q</code> is a lot slower: when you say </p>
<pre><code>l += "a"
</code></pre>
<p>you are appending the string <code>"a"</code> to the end of <code>l</code>, but when you say</p>
<pre><code>l = l + ["a"]
</code></pre>
<p>you are creating a new list with the contents of <code>l</code> and <code>["... | 4 | 2009-08-28T22:00:21Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,349,629 | <p>You are measuring two distinct operations: the creation of an array of strings, and the concatenation of strings.</p>
<pre><code> import timeit
def x():
s = []
for i in range(100):
s.append("abcdefg"[i%7])
return ''.join(s)
def y():
s = ''
for i in rang... | 2 | 2009-08-28T22:12:17Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,350,148 | <p>This question is really about what things cost. We'll play a bit fast and loose here, subtracting results in similar cases. You can decide for yourself if this is a valid method. Here are some basic test cases:</p>
<pre><code>import timeit
def append_to_list_with_join():
s=[]
for i in xrange(100):
s... | 3 | 2009-08-29T01:27:37Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,350,289 | <p>Some of us Python committers, I believe mostly Rigo and Hettinger, went out of their way (on the way to 2.5 I believe) to optimize some special cases of the alas-far-too-common <code>s += something</code> <strong>blight</strong>, arguing that it was proven that beginners will never be covinced that <code>''.join</co... | 42 | 2009-08-29T02:47:39Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 1,390,428 | <p>I have figured out the answer from the answers posted here by experts. Python string concatenation (and timing measurements) depends on these (as far as I've seen):</p>
<ul>
<li>Number of concatenations</li>
<li>Average length of strings</li>
<li>Number of function callings</li>
</ul>
<p>I have built a new code th... | 2 | 2009-09-07T18:07:11Z | [
"python",
"string",
"performance"
] |
Python string 'join' is faster (?) than '+', but what's wrong here? | 1,349,311 | <p>I asked the most efficient method for mass dynamic string concatenation in an earlier post and I was suggested to use the <strong>join</strong> method, the best, simplest and fastest method to do so (as everyone said that). But while I was playing with string concatenations, I found some weird(?) results. I'm sure s... | 23 | 2009-08-28T20:57:29Z | 21,964,653 | <p>There are many good summaries already here, but just for more proof.</p>
<p>Source: I stared at python source code for an hour and calculated complexities!</p>
<p>My findings.</p>
<p>For 2 strings. (Assume n is the length of both strings)</p>
<pre><code>Concat (+) - O(n)
Join - O(n+k) effectively O(n)
For... | 2 | 2014-02-23T05:36:41Z | [
"python",
"string",
"performance"
] |
Python SOAP clients will not work with this WSDL | 1,349,325 | <p>Thus far I've tried to access this WSDL:</p>
<p><a href="https://login.azoogleads.com/affiliate/tool/soap%5Fapi" rel="nofollow">https://login.azoogleads.com/affiliate/tool/soap%5Fapi</a></p>
<p>from the two common Python SOAP clients that I'm aware of: SOAPpy and ZSI.client.Binding. SOAPpy raises an exception in ... | 2 | 2009-08-28T21:00:06Z | 1,353,802 | <p>your not actually giving it a valid WSDL endpoint try explicilty giving it the WSDL location rather than the directory it is in. Remember computer are exceptually stupid things!</p>
| 2 | 2009-08-30T12:44:32Z | [
"python",
"soap",
"soappy",
"zsi"
] |
Python - Passing a function into another function | 1,349,332 | <p>I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?</p>
<p>Example</p>
<pre><code>def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if ... | 37 | 2009-08-28T21:01:19Z | 1,349,350 | <p>Just pass it in like any other parameter:</p>
<pre><code>def a(x):
return "a(%s)" % (x,)
def b(f,x):
return f(x)
print b(a,10)
</code></pre>
| 72 | 2009-08-28T21:04:04Z | [
"python",
"function",
"first-class-functions"
] |
Python - Passing a function into another function | 1,349,332 | <p>I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?</p>
<p>Example</p>
<pre><code>def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if ... | 37 | 2009-08-28T21:01:19Z | 1,349,359 | <p>Treat function as variable in your program so you can just pass them to other functions easily:</p>
<pre><code>def test ():
print "test was invoked"
def invoker(func):
func()
invoker(test) # prints test was invoked
</code></pre>
| 10 | 2009-08-28T21:06:28Z | [
"python",
"function",
"first-class-functions"
] |
Python - Passing a function into another function | 1,349,332 | <p>I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?</p>
<p>Example</p>
<pre><code>def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if ... | 37 | 2009-08-28T21:01:19Z | 1,349,395 | <p>Just pass it in, like this:</p>
<pre><code>Game(list_a, list_b, Rule1)
</code></pre>
<p>and then your Game function could look something like this (still pseudocode):</p>
<pre><code>def Game(listA, listB, rules=None):
if rules:
# do something useful
# ...
result = rules(variable) # thi... | 6 | 2009-08-28T21:12:57Z | [
"python",
"function",
"first-class-functions"
] |
Python - Passing a function into another function | 1,349,332 | <p>I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?</p>
<p>Example</p>
<pre><code>def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if ... | 37 | 2009-08-28T21:01:19Z | 13,382,678 | <p>A function name can become a variable name (and thus be passed as an argument) by dropping the parentheses. A variable name can become a function name by adding the parentheses.</p>
<p>In your example, equate the variable <code>rules</code> to one of your functions, leaving off the parentheses and the mention of th... | 6 | 2012-11-14T16:15:48Z | [
"python",
"function",
"first-class-functions"
] |
Python - Passing a function into another function | 1,349,332 | <p>I am solving a puzzle using python and depending on which puzzle I am solving I will have to use a special set of rules. How can I pass a function into another function in Python?</p>
<p>Example</p>
<pre><code>def Game(listA, listB, rules):
if rules == True:
do...
else:
do...
def Rule1(v):
if ... | 37 | 2009-08-28T21:01:19Z | 35,467,124 | <p><strong>A generalized approach</strong></p>
<p>For passing both a function, and the parameters to the function (e.g. using the same iteration routine for different functions) consider the following (<code>python2.x</code>) example:</p>
<pre><code>def test(a, b):
'''The function to pass'''
print a+b
def lo... | 2 | 2016-02-17T20:31:58Z | [
"python",
"function",
"first-class-functions"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,506 | <p>If those components will always be there, then a regex will do the trick:</p>
<pre><code>test = '''Authorization: Digest qop="chap", realm="testrealm@host.com", username="Foobear", response="6629fae49393a05397450978507c4ef1", cnonce="5ccc069c403ebaf9f0171e9517f40e41"'''
import re
re_auth = re.compile(r"""
Aut... | 1 | 2009-08-28T21:36:41Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,512 | <p>I would recommend finding a correct library for parsing http headers unfortunately I can't reacall any. :(</p>
<p>For a while check the snippet below (it should mostly work):</p>
<pre><code>input= """
Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foob,ear",
response="6629fae49393a0... | 1 | 2009-08-28T21:38:11Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,517 | <p>If your response comes in a single string that that <strong>never varies and has as many lines as there are expressions to match</strong>, you can split it into an array on the newlines called <code> authentication_array</code> and use regexps:</p>
<pre><code>pattern_array = ['qop', 'realm', 'username', 'response',... | 0 | 2009-08-28T21:38:47Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,528 | <p>A little regex:</p>
<pre><code>import re
reg=re.compile('(\w+)[:=] ?"?(\w+)"?')
>>>dict(reg.findall(headers))
{'username': 'Foobear', 'realm': 'testrealm', 'qop': 'chap', 'cnonce': '5ccc069c403ebaf9f0171e9517f40e41', 'response': '6629fae49393a05397450978507c4ef1', 'Authorization': 'Digest'}
</code></pre>... | 11 | 2009-08-28T21:40:19Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,539 | <p>Your original concept of using PyParsing would be the best approach. What you've implicitly asked for is something that requires a grammar... that is, a regular expression or simple parsing routine is always going to be brittle, and that sounds like it's something you're trying to avoid.</p>
<p>It appears that get... | 0 | 2009-08-28T21:42:40Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,349,626 | <p>You can also use urllib2 as <a href="http://www.google.com/codesearch/p?hl=en&sa=N&cd=9&ct=rc#OQvO9n2mc04/CherryPy-3.0.1/cherrypy/lib/httpauth.py&q=Authorization%20Digest%20http%20lang%3Apython">CheryPy</a> does.</p>
<p>here is the snippet:</p>
<pre><code>input= """
Authorization: Digest qop="chap... | 8 | 2009-08-28T22:11:31Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 1,378,208 | <p>Here's my pyparsing attempt:</p>
<pre><code>text = """Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41" """
from pyparsing import *
AUTH = Keyword("Authorization")
... | 2 | 2009-09-04T09:40:06Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 2,831,097 | <p>The http digest Authorization header field is a bit of an odd beast. Its format is similar to that of <a href="http://tools.ietf.org/html/rfc2616" rel="nofollow">rfc 2616</a>'s Cache-Control and Content-Type header fields, but just different enough to be incompatible. If you're still looking for a library that's a... | 1 | 2010-05-14T00:13:46Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Parse an HTTP request Authorization header with Python | 1,349,367 | <p>I need to take a header like this:</p>
<pre><code> Authorization: Digest qop="chap",
realm="testrealm@host.com",
username="Foobear",
response="6629fae49393a05397450978507c4ef1",
cnonce="5ccc069c403ebaf9f0171e9517f40e41"
</code></pre>
<p>And parse it into this using Python:</p>
<pre><code>{'pro... | 9 | 2009-08-28T21:07:56Z | 7,404,187 | <p>Nadia's regex only matches alphanumeric characters for the value of a parameter. That means it fails to parse at least two fields. Namely, the uri and qop. According to RFC 2617, the uri field is a duplicate of the string in the request line (i.e. the first line of the HTTP request). And qop fails to parse corre... | 1 | 2011-09-13T15:09:58Z | [
"python",
"http",
"google-app-engine",
"parsing",
"http-headers"
] |
Python change screen resolution virtual machine | 1,349,544 | <p>In virtualbox, the screen resolution can be anything - even something strange like 993x451, etc. I tried changing it using pywin32 but I failed::</p>
<pre><code>>>> dm = win32api.EnumDisplaySettings(None, 0)
>>> dm.PelsHeight = 451
>>> dm.PelsWidth = 950
>>> win32api.ChangeDispla... | 3 | 2009-08-28T21:44:14Z | 1,349,559 | <p>Do you have VirtualBox set to automatically set the client window? that could cause some issues.</p>
| 0 | 2009-08-28T21:48:16Z | [
"python",
"winapi",
"virtualization",
"resolution",
"pywin32"
] |
Python change screen resolution virtual machine | 1,349,544 | <p>In virtualbox, the screen resolution can be anything - even something strange like 993x451, etc. I tried changing it using pywin32 but I failed::</p>
<pre><code>>>> dm = win32api.EnumDisplaySettings(None, 0)
>>> dm.PelsHeight = 451
>>> dm.PelsWidth = 950
>>> win32api.ChangeDispla... | 3 | 2009-08-28T21:44:14Z | 1,411,168 | <p>Have you configured the virtual machine to actually advertise this mode to the OS?</p>
<p>edit: VirtualBox automatically sets new resolutions if you change the size of the window. You can set video mode hints from the host OS I believe (look for it in the documentation), but you need guest additions installed. You ... | 1 | 2009-09-11T14:20:04Z | [
"python",
"winapi",
"virtualization",
"resolution",
"pywin32"
] |
Python change screen resolution virtual machine | 1,349,544 | <p>In virtualbox, the screen resolution can be anything - even something strange like 993x451, etc. I tried changing it using pywin32 but I failed::</p>
<pre><code>>>> dm = win32api.EnumDisplaySettings(None, 0)
>>> dm.PelsHeight = 451
>>> dm.PelsWidth = 950
>>> win32api.ChangeDispla... | 3 | 2009-08-28T21:44:14Z | 1,981,856 | <p>The way I found to do this is to enable the automatic client resizing from the Guest OS. Then, in the host OS, programatically resize the VM window. This will cause the resolution to change.</p>
| 0 | 2009-12-30T19:06:43Z | [
"python",
"winapi",
"virtualization",
"resolution",
"pywin32"
] |
Strategy for maintaining complex filter states? | 1,349,840 | <p>I need to maintain a list of filtered and sorted objects, preferably in a generic manner, that can be used in multiple views. This is necessary so I can generate next, prev links, along with some other very useful things for the user.</p>
<p>Examples of filters:</p>
<pre><code>field__isnull=True
field__exact="so"
... | 0 | 2009-08-28T23:19:04Z | 1,350,064 | <p>Depending on what you want to do, you'll want to either <a href="http://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets" rel="nofollow">create a custom manager</a> or add a <a href="http://docs.djangoproject.com/en/dev/topics/db/managers/#adding-extra-manager-methods" rel="nofol... | 1 | 2009-08-29T00:48:42Z | [
"python",
"django",
"http"
] |
Strategy for maintaining complex filter states? | 1,349,840 | <p>I need to maintain a list of filtered and sorted objects, preferably in a generic manner, that can be used in multiple views. This is necessary so I can generate next, prev links, along with some other very useful things for the user.</p>
<p>Examples of filters:</p>
<pre><code>field__isnull=True
field__exact="so"
... | 0 | 2009-08-28T23:19:04Z | 1,351,611 | <p>You've identified the two options for maintaining user-specific state in a web application: store it in cookies/session, or pass it around on URLs. I don't believe there's a third "silver bullet" waiting in the wings to solve your problem.</p>
<p>The URL query-string option has the advantage that a particular view ... | 2 | 2009-08-29T14:46:31Z | [
"python",
"django",
"http"
] |
Novice needs advice for script that gets data and returns it in a usable format | 1,349,932 | <p>I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction. </p>
<p>I want the script to get the width and height of each image, then f... | 0 | 2009-08-28T23:49:50Z | 1,349,982 | <p>Maybe the better solution would be not to create a script that generates a list of all the image elements for you to put in your document but rather generate the image elements on the fly in the desired document. This could be done like this:</p>
<pre><code>if ($handle = opendir('/path/to/images')) {
while (fal... | 1 | 2009-08-29T00:08:07Z | [
"php",
"python",
"scripting",
"image-processing",
"website"
] |
Novice needs advice for script that gets data and returns it in a usable format | 1,349,932 | <p>I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction. </p>
<p>I want the script to get the width and height of each image, then f... | 0 | 2009-08-28T23:49:50Z | 1,349,986 | <p>Check out the <a href="http://www.pythonware.com/products/pil/" rel="nofollow">PIL</a>:</p>
<pre><code>from PIL import Image
im = Image.open("yourfile.jpg")
print im.size
</code></pre>
<p>For looping through files see <a href="http://love-python.blogspot.com/2008/05/list-of-files-directories-in-python.html" rel="n... | 3 | 2009-08-29T00:09:32Z | [
"php",
"python",
"scripting",
"image-processing",
"website"
] |
Novice needs advice for script that gets data and returns it in a usable format | 1,349,932 | <p>I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction. </p>
<p>I want the script to get the width and height of each image, then f... | 0 | 2009-08-28T23:49:50Z | 1,350,015 | <p>You can also use shell <a href="http://www.imagemagick.org/script/identify.php" rel="nofollow">identify</a> command from <a href="http://www.imagemagick.org/script/index.php" rel="nofollow">Imagemagick</a>:</p>
<pre><code>for file in dir/*; do
identify -format "Width: %w, Height: %h" $file
done
</code></pre>
| 0 | 2009-08-29T00:26:34Z | [
"php",
"python",
"scripting",
"image-processing",
"website"
] |
Novice needs advice for script that gets data and returns it in a usable format | 1,349,932 | <p>I have a large number of images that I am putting into web pages. Rather than painstakingly enter all of the image attributes, I thought I could write a script that would do the work for me. I just need a little push in the right direction. </p>
<p>I want the script to get the width and height of each image, then f... | 0 | 2009-08-28T23:49:50Z | 1,350,082 | <p>I think this should do what you want. But it's not necessarily the best way (it's late, I'm tired, etc...):</p>
<pre><code><?php
$directory = "img"; // The path in which your images are located.
if ($directory) {
$files = scandir($directory); // $files becomes an array of files in the relevant directory.
}
fo... | 0 | 2009-08-29T00:57:57Z | [
"php",
"python",
"scripting",
"image-processing",
"website"
] |
Can Selenium RC tests written in Python be integrated into PHPUnit? | 1,350,114 | <p>I'm working on large project in PHP and I'm running phpundercontrol with PHPUnit for my unit tests. I would like to use Selenium RC for running acceptance tests. Unfortunately the only person I have left to write tests only knows Python. Can Selenium tests written in Python be integrated into PHPUnit?</p>
<p>Than... | 1 | 2009-08-29T01:15:11Z | 1,360,647 | <p>The only thing that comes to my mind is running them through the shell.
It would be:</p>
<pre><code><?php
$output = shell_exec('python testScript.py');
echo $output;
?>
</code></pre>
<p>It's not too integrated with phpunit, but once you get the output in a variable ($output), you can then parse the text insi... | 1 | 2009-09-01T05:17:19Z | [
"python",
"selenium",
"phpunit"
] |
sqlalchemy create a foreign key? | 1,350,121 | <p>I have a composite PK in table Strings (integer id, varchar(2) lang)</p>
<p>I want to create a FK to ONLY the id half of the PK from other tables. This means I'd have potentially many rows in Strings table (translations) matching the FK. I just need to store the id, and have referential integrity maintained by the ... | 1 | 2009-08-29T01:17:07Z | 1,351,222 | <p>This is from <a href="http://en.wikipedia.org/wiki/Foreign%5Fkey" rel="nofollow">wiki</a></p>
<blockquote>
<p>The columns in the referencing table
must be the primary key or other
candidate key in the referenced table. The values in one row of the referencing columns must occur in a single row in the referenc... | 3 | 2009-08-29T11:36:52Z | [
"python",
"sql",
"sqlalchemy"
] |
What does matrix**2 mean in python/numpy? | 1,350,174 | <p>I have a python ndarray temp in some code I'm reading that suffers this:</p>
<pre><code>x = temp**2
</code></pre>
<p>Is this the dot square (ie, equivalent to m.*m) or the matrix square (ie m must be a square matrix)? In particular, I'd like to know whether I can get rid of the transpose in this code:</p>
<pre><... | 9 | 2009-08-29T01:40:52Z | 1,350,199 | <p>It's just the square of each element.</p>
<pre><code>from numpy import *
a = arange(4).reshape((2,2))
print a**2
</code></pre>
<p>prints</p>
<pre><code>[[0 1]
[4 9]]
</code></pre>
| 12 | 2009-08-29T01:54:48Z | [
"python",
"numpy"
] |
What does matrix**2 mean in python/numpy? | 1,350,174 | <p>I have a python ndarray temp in some code I'm reading that suffers this:</p>
<pre><code>x = temp**2
</code></pre>
<p>Is this the dot square (ie, equivalent to m.*m) or the matrix square (ie m must be a square matrix)? In particular, I'd like to know whether I can get rid of the transpose in this code:</p>
<pre><... | 9 | 2009-08-29T01:40:52Z | 1,350,212 | <p><code>**</code> is the raise-to-power operator in Python, so <code>x**2</code> means "x squared" in Python -- including numpy. Such operations in numpy always apply element by element, so <code>x**2</code> squares each element of array <code>x</code> (whatever number of dimensions) just like, say, <code>x*2</code> w... | 5 | 2009-08-29T02:04:01Z | [
"python",
"numpy"
] |
What does matrix**2 mean in python/numpy? | 1,350,174 | <p>I have a python ndarray temp in some code I'm reading that suffers this:</p>
<pre><code>x = temp**2
</code></pre>
<p>Is this the dot square (ie, equivalent to m.*m) or the matrix square (ie m must be a square matrix)? In particular, I'd like to know whether I can get rid of the transpose in this code:</p>
<pre><... | 9 | 2009-08-29T01:40:52Z | 1,351,683 | <p>You should read <a href="http://www.scipy.org/NumPy%5Ffor%5FMatlab%5FUsers" rel="nofollow">NumPy for Matlab Users</a>. The elementwise power operation is mentioned there, and you can also see that in numpy, some operators apply differently to <code>array</code> and <code>matrix</code>.</p>
<pre><code>>>> f... | 5 | 2009-08-29T15:17:41Z | [
"python",
"numpy"
] |
Check whether debug is enabled in a Pylons application | 1,350,227 | <p>I'm working on a fairly simple Pylons 0.9.7 application. How do I tell, in code, whether or not debugging is enabled? That is, I'm interested in the value of the <strong>debug</strong> setting under <strong>[app:main]</strong> in my INI file. More generally, how do I access the other values from there in my code?</p... | 1 | 2009-08-29T02:11:19Z | 1,350,240 | <pre><code># tmp.py
print __debug__
$ python tmp.py
True
$ python -O tmp.py
False
</code></pre>
<p>I'm not sure if this holds in Pylons, as I've never used that -- but in "normal" command line Python, debug is enabled if optimizations are <em>not</em> enabled. The <code>-O</code> flag indicates to Python to turn on... | 3 | 2009-08-29T02:18:32Z | [
"python",
"configuration",
"pylons"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,350,443 | <p>If you're only going to be using this on strings, in a pinch you could just write a method that goes through the string and replaces special characters (for whatever definition of "special" you want) with their escape codes. That's what I would do. (I did a quick search and nothing came up on Google, so it might be ... | 0 | 2009-08-29T04:23:06Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,350,529 | <p>This will do it, but it's a bit of a hack, it uses <a href="http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringUtils.html" rel="nofollow">StringUtils</a> and <strong>replaceEach</strong> from Common Lang to achieve a simple replace:</p>
<pre><code>String hello = "hello\n\tworld\n\n\t";
... | 0 | 2009-08-29T05:10:53Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,350,820 | <p>If there were such a method, it would make writing quines in Java really easy, because it would solve the problem of escaping the quotes. Seeing as the simplest quines in Java all require manually inserting the quote character manually with its character code, it is unlikely that such a method exists.</p>
| 0 | 2009-08-29T08:06:50Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,351,230 | <p>don't think there's a specific method -- but this'll solve it without commons lang:</p>
<pre><code>public class test {
public test() throws Exception {
byte[] hello = "hello\n\tworld\n\n\t".getBytes();
System.out.println(new String(hexToByte(stringToHex(hello).replaceAll("0a", "5c6e")
... | 1 | 2009-08-29T11:41:40Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,351,973 | <p>In some projects, I use the following helper function to accomplish something akin to Python's <em>repr</em> for strings:</p>
<pre><code>private static final char CONTROL_LIMIT = ' ';
private static final char PRINTABLE_LIMIT = '\u007e';
private static final char[] HEX_DIGITS = new char[] { '0', '1', '2', '3', '4',... | 8 | 2009-08-29T17:49:30Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 1,352,380 | <p>It appears that Jython already does this. You could, in theory, include the Jython jar, startup an interpreter, and actually run repr(object) on the object in question. Probably more overhead than you want, but does exactly what you describe.</p>
<p>If you want to embed the Jython interpreter in your application, ... | 0 | 2009-08-29T21:08:56Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 18,722,866 | <p>Java has no repr-Function, but <a href="http://repr.sf.net" rel="nofollow">repr</a> has got you covered (Full disclosure: I am the author of repr).</p>
| 2 | 2013-09-10T15:21:57Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 23,957,629 | <p>Use the static method <code>escapeJava</code> from Commons Lang <code>StringEscapeUtils</code> class.</p>
<pre><code>String repr = "\"" + StringEscapeUtils.escapeJava(myString) + "\"";
</code></pre>
| 1 | 2014-05-30T15:00:31Z | [
"java",
"python",
"repr"
] |
Java equivalent of Python repr()? | 1,350,397 | <p>Is there a Java method that works like Python's repr? For example, assuming the function were named repr,</p>
<pre><code>"foo\n\tbar".repr()
</code></pre>
<p>would return</p>
<pre>"foo\n\tbar"</pre>
<p>not</p>
<pre>foo
bar</pre>
<p>as toString does.</p>
| 20 | 2009-08-29T03:52:13Z | 32,284,299 | <p>In case you are using <strong>Groovy</strong>, it provides a similar <a href="http://docs.groovy-lang.org/latest/html/api/groovy/json/StringEscapeUtils.html#escapeJava(java.lang.String)" rel="nofollow"><code>StringEscapeUtils</code> class</a> as Apache Commons Lang:</p>
<pre><code>StringEscapeUtils.escapeJava("foo\... | 0 | 2015-08-29T08:52:44Z | [
"java",
"python",
"repr"
] |
Preventing Python code from importing certain modules? | 1,350,466 | <p>I'm writing an application where users can enter a python script and execute it in a sandbox. I need a way to prevent the exec'ed code from importing certain modules, so malicious code won't be as much of a problem. Is there a way to do this in Python?</p>
| 7 | 2009-08-29T04:39:33Z | 1,350,472 | <p>You can overload the import mechanism. We used this to have a licensing system for plugins, you can easily have a whitelist / blacklist of module names.</p>
| 1 | 2009-08-29T04:43:36Z | [
"python",
"module",
"sandbox"
] |
Preventing Python code from importing certain modules? | 1,350,466 | <p>I'm writing an application where users can enter a python script and execute it in a sandbox. I need a way to prevent the exec'ed code from importing certain modules, so malicious code won't be as much of a problem. Is there a way to do this in Python?</p>
| 7 | 2009-08-29T04:39:33Z | 1,350,473 | <p>Unfortunately, I think that what you're trying to do is fundamentally impossible. If users can execute arbitrary code in your application then they can do whatever they want. Even if you were able to prevent them from importing certain modules there would be nothing stopping them from writing equivalent functionalit... | 0 | 2009-08-29T04:43:49Z | [
"python",
"module",
"sandbox"
] |
Preventing Python code from importing certain modules? | 1,350,466 | <p>I'm writing an application where users can enter a python script and execute it in a sandbox. I need a way to prevent the exec'ed code from importing certain modules, so malicious code won't be as much of a problem. Is there a way to do this in Python?</p>
| 7 | 2009-08-29T04:39:33Z | 1,350,476 | <p>Have you checked the python.org <a href="http://wiki.python.org/moin/SandboxedPython">article on SandboxedPython</a>, and the <a href="http://wiki.python.org/moin/How%20can%20I%20run%20an%20untrusted%20Python%20script%20safely%20%28i.e.%20Sandbox%29">linked article</a>?</p>
<p>Both of those pages have links to othe... | 11 | 2009-08-29T04:44:34Z | [
"python",
"module",
"sandbox"
] |
Preventing Python code from importing certain modules? | 1,350,466 | <p>I'm writing an application where users can enter a python script and execute it in a sandbox. I need a way to prevent the exec'ed code from importing certain modules, so malicious code won't be as much of a problem. Is there a way to do this in Python?</p>
| 7 | 2009-08-29T04:39:33Z | 1,350,505 | <p>Google App Engine's open source <a href="http://code.google.com/appengine/downloads.html">SDK</a> has a detailed and solid implementation of mechanics to stop the importing of unwanted modules (to help detect code trying to import modules that aren't made available in the production instances of App Engine), though ... | 5 | 2009-08-29T04:59:50Z | [
"python",
"module",
"sandbox"
] |
Preventing Python code from importing certain modules? | 1,350,466 | <p>I'm writing an application where users can enter a python script and execute it in a sandbox. I need a way to prevent the exec'ed code from importing certain modules, so malicious code won't be as much of a problem. Is there a way to do this in Python?</p>
| 7 | 2009-08-29T04:39:33Z | 1,350,574 | <p>If you put None in sys.modules for a module name, in won't be importable...</p>
<pre><code>>>> import sys
>>> import os
>>> del os
>>> sys.modules['os']=None
>>> import os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: N... | 13 | 2009-08-29T05:35:31Z | [
"python",
"module",
"sandbox"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 1,350,692 | <p>I don't think you can do this in Python 2.x, but something similar to this functionality is part of Python 3. From <a href="http://www.python.org/dev/peps/pep-3134/" rel="nofollow">PEP 3134</a>:</p>
<blockquote>
<p>In today's Python implementation, exceptions are composed of three
parts: the type, the value... | 4 | 2009-08-29T06:49:47Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 1,350,885 | <p>Maybe you could grab the relevant information and pass it up? I'm thinking something like:</p>
<pre><code>import traceback
import sys
import StringIO
class ApplicationError:
def __init__(self, value, e):
s = StringIO.StringIO()
traceback.print_exc(file=s)
self.value = (value, s.getvalu... | 3 | 2009-08-29T08:51:09Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 1,350,972 | <p>In <strong>Python 3.x</strong>:</p>
<pre><code>raise Exception('Failed to process file ' + filePath).with_traceback(e.__traceback__)
</code></pre>
<p><strong>or</strong> simply </p>
<pre><code>except Exception:
raise MyException()
</code></pre>
<p>which will propagate <code>MyException</code> but print <stro... | 6 | 2009-08-29T09:36:19Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 1,350,981 | <p>It's simple; pass the traceback as the third argument to raise.</p>
<pre><code>import sys
class MyException(Exception): pass
try:
raise TypeError("test")
except TypeError, e:
raise MyException(), None, sys.exc_info()[2]
</code></pre>
<p>Always do this when catching one exception and re-raising another.</p... | 97 | 2009-08-29T09:41:33Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 6,246,394 | <p>In python 3 you can do the following:</p>
<pre><code>try:
raise MyExceptionToBeWrapped("I have twisted my ankle")
except MyExceptionToBeWrapped as e:
raise MyWrapperException("I'm not in a good shape") from e
</code></pre>
<p>This will produce something like this:</p>
<pre><code>Traceback (most recent c... | 105 | 2011-06-05T22:42:06Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 11,669,125 | <p>Here is an answer straight from the docs:
<a href="http://docs.python.org/tutorial/errors.html" rel="nofollow">http://docs.python.org/tutorial/errors.html</a></p>
<p>Look at the the second code sample in the Raising Exceptions section(copied below):</p>
<pre><code>try:
raise NameError('HiThere')
except NameErr... | 0 | 2012-07-26T12:17:54Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 12,279,514 | <p>You could use my <a href="http://code.activestate.com/recipes/578252-python-exception-chains-or-trees/?in=user-4182236" rel="nofollow">CausedException class</a> to chain exceptions in Python 2.x (and even in Python 3 it can be useful in case you want to give more than one caught exception as cause to a newly raised ... | 3 | 2012-09-05T10:35:23Z | [
"python",
"exception",
"error-handling"
] |
"Inner exception" (with traceback) in Python? | 1,350,671 | <p>My background is in C# and I've just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It's quite easy in C#, but how do I do it in Python?</p>
<p>Eg. in C# I would do something l... | 86 | 2009-08-29T06:35:17Z | 13,018,468 | <p>Python 3 has the <a href="https://docs.python.org/3/reference/simple_stmts.html#raise" rel="nofollow"><code>raise</code> ... <code>from</code> clause</a> to chain exceptions. <a href="http://stackoverflow.com/a/1350981/4794">Glenn's answer</a> is great for Python 2.7, but it only uses the original exception's traceb... | 8 | 2012-10-22T19:33:05Z | [
"python",
"exception",
"error-handling"
] |
Tool for analysing and stepping through code? | 1,350,864 | <p>Recently I came across a tool which could analyse running python code and produced a visual representation similar to a code editor to allow one to step through the different parts of the code, seeing how many times each part was called, execution time, etc.</p>
<p>I can't find the reference to it again. Would anyo... | 2 | 2009-08-29T08:37:59Z | 1,350,869 | <p><a href="http://www.netbeans.org/features/python/index.html" rel="nofollow">NetBeans</a> with python plug-in?</p>
| 0 | 2009-08-29T08:42:34Z | [
"python",
"code-analysis",
"profiling"
] |
Tool for analysing and stepping through code? | 1,350,864 | <p>Recently I came across a tool which could analyse running python code and produced a visual representation similar to a code editor to allow one to step through the different parts of the code, seeing how many times each part was called, execution time, etc.</p>
<p>I can't find the reference to it again. Would anyo... | 2 | 2009-08-29T08:37:59Z | 1,350,872 | <p><a href="http://pydoc.org/2.5.1/cProfile.html" rel="nofollow">cProfile</a> or <a href="http://docs.python.org/library/hotshot.html" rel="nofollow">Hotshot</a>.</p>
| 2 | 2009-08-29T08:44:08Z | [
"python",
"code-analysis",
"profiling"
] |
Tool for analysing and stepping through code? | 1,350,864 | <p>Recently I came across a tool which could analyse running python code and produced a visual representation similar to a code editor to allow one to step through the different parts of the code, seeing how many times each part was called, execution time, etc.</p>
<p>I can't find the reference to it again. Would anyo... | 2 | 2009-08-29T08:37:59Z | 1,350,889 | <p><a href="http://www.vrplumber.com/programming/runsnakerun/" rel="nofollow"><strong>RunSnakeRun</strong></a> is user interface for cProfile/Hotshot (see <a href="http://stackoverflow.com/questions/1350864/tool-for-analysing-and-stepping-through-code/1350872#1350872">James' answer</a>), which also provides a visualiza... | 1 | 2009-08-29T08:53:20Z | [
"python",
"code-analysis",
"profiling"
] |
Tool for analysing and stepping through code? | 1,350,864 | <p>Recently I came across a tool which could analyse running python code and produced a visual representation similar to a code editor to allow one to step through the different parts of the code, seeing how many times each part was called, execution time, etc.</p>
<p>I can't find the reference to it again. Would anyo... | 2 | 2009-08-29T08:37:59Z | 1,351,373 | <p>Maybe <a href="http://pycallgraph.slowchop.com/" rel="nofollow">Python Call Graph</a>?</p>
| 0 | 2009-08-29T12:55:05Z | [
"python",
"code-analysis",
"profiling"
] |
Tool for analysing and stepping through code? | 1,350,864 | <p>Recently I came across a tool which could analyse running python code and produced a visual representation similar to a code editor to allow one to step through the different parts of the code, seeing how many times each part was called, execution time, etc.</p>
<p>I can't find the reference to it again. Would anyo... | 2 | 2009-08-29T08:37:59Z | 1,352,647 | <p>Found what I was looking for: <a href="http://codeinvestigator.googlepages.com/main" rel="nofollow">Code Investigator</a></p>
<blockquote>
<p>CodeInvestigator is a tracing tool for Python programs. All run time information is recorded. Read your code together with its run time details in a Firefox browser. See wh... | 1 | 2009-08-29T23:37:54Z | [
"python",
"code-analysis",
"profiling"
] |
Python: Is there a place when I can put default imports for all my modules? | 1,350,887 | <p>Is there a place when I can put default imports for all my modules?</p>
| 1 | 2009-08-29T08:52:21Z | 1,350,906 | <p>Yes, just create a separate module and import it into yours.</p>
<p>Example:</p>
<pre><code># my_imports.py
'''Here go all of my imports'''
import sys
import functools
from contextlib import contextmanager # This is a long name, no chance to confuse it.
....
# something1.py
'''One of my project files.'''
from m... | 3 | 2009-08-29T09:03:05Z | [
"python"
] |
Python: Is there a place when I can put default imports for all my modules? | 1,350,887 | <p>Is there a place when I can put default imports for all my modules?</p>
| 1 | 2009-08-29T08:52:21Z | 33,278,074 | <p>If you want default imports when using the python shell you can also set the <code>PYTHONSTARTUP</code> environmental variable to point to a python file that will be executed whenever you start the shell. Put all your default imports in this file.</p>
| 2 | 2015-10-22T09:47:25Z | [
"python"
] |
How to import XSD schema with Python Suds (version 0.3.6) SOAP library : TypeNotFound exception? | 1,351,125 | <p>I'm trying to use SABRE travel web services with Python Suds, but one XSD seems not well-formed (maybe namespace is missing in this schema).</p>
<pre><code>
from suds.client import Client
wsdl = 'http://webservices.sabre.com/wsdl/sabreXML1.0.00/tpf/OTA_AirPriceLLSRQ.wsdl'
client = Client(wsdl, cache=None)
</code></... | 9 | 2009-08-29T10:47:41Z | 3,282,657 | <p>This looks like <a href="https://fedorahosted.org/suds/ticket/239" rel="nofollow">https://fedorahosted.org/suds/ticket/239</a> . I added your example to the ticket.</p>
| 1 | 2010-07-19T15:48:17Z | [
"python",
"soap",
"wsdl",
"suds"
] |
How to import XSD schema with Python Suds (version 0.3.6) SOAP library : TypeNotFound exception? | 1,351,125 | <p>I'm trying to use SABRE travel web services with Python Suds, but one XSD seems not well-formed (maybe namespace is missing in this schema).</p>
<pre><code>
from suds.client import Client
wsdl = 'http://webservices.sabre.com/wsdl/sabreXML1.0.00/tpf/OTA_AirPriceLLSRQ.wsdl'
client = Client(wsdl, cache=None)
</code></... | 9 | 2009-08-29T10:47:41Z | 4,395,481 | <p>I just posted a workaround to that Suds ticket. You may want to check it out: <a href="https://fedorahosted.org/suds/ticket/239#comment:19" rel="nofollow">https://fedorahosted.org/suds/ticket/239#comment:19</a></p>
<p>Briefly, here's the workaround code:</p>
<pre><code>from suds.client import Client
import sys
sys... | 3 | 2010-12-09T06:26:35Z | [
"python",
"soap",
"wsdl",
"suds"
] |
Compiling a py2app working build for both Leopard and Snow Leopard? | 1,351,209 | <p>I currently am making my PyObjC application work for Snow Leopard and I successfully compiled a standalone app. My question would be, how do I make the build to be also Leopard-compatible, given these errors?</p>
<pre><code>dyld: lazy symbol binding failed: Symbol not found: _fopen$UNIX2003
Referenced from: /Appl... | 6 | 2009-08-29T11:31:25Z | 1,541,364 | <p>Since both are on distinct architecture (respectively 32 bits and 64 bits) I think you have to create 2 distinct compilations.</p>
| 0 | 2009-10-09T01:08:17Z | [
"python",
"osx-snow-leopard",
"osx-leopard",
"py2app"
] |
Compiling a py2app working build for both Leopard and Snow Leopard? | 1,351,209 | <p>I currently am making my PyObjC application work for Snow Leopard and I successfully compiled a standalone app. My question would be, how do I make the build to be also Leopard-compatible, given these errors?</p>
<pre><code>dyld: lazy symbol binding failed: Symbol not found: _fopen$UNIX2003
Referenced from: /Appl... | 6 | 2009-08-29T11:31:25Z | 1,758,020 | <p>Try the following:</p>
<p><a href="http://groups.google.com/group/wxpython-users/browse%5Fthread/thread/916fa0569bfa3efd/9d16f540a89cc4c3?lnk=gst&q=py2app#9d16f540a89cc4c3" rel="nofollow">http://groups.google.com/group/wxpython-users/browse%5Fthread/thread/916fa0569bfa3efd/9d16f540a89cc4c3?lnk=gst&q=py2app#... | 0 | 2009-11-18T18:18:59Z | [
"python",
"osx-snow-leopard",
"osx-leopard",
"py2app"
] |
Compiling a py2app working build for both Leopard and Snow Leopard? | 1,351,209 | <p>I currently am making my PyObjC application work for Snow Leopard and I successfully compiled a standalone app. My question would be, how do I make the build to be also Leopard-compatible, given these errors?</p>
<pre><code>dyld: lazy symbol binding failed: Symbol not found: _fopen$UNIX2003
Referenced from: /Appl... | 6 | 2009-08-29T11:31:25Z | 2,043,657 | <p>I've done this recently and the trick was to build a <strong>standalone</strong> version on a <strong>Leopard</strong> installation.</p>
<p>By default, unless you have an open source version of Python installed, py2app creates a <strong>semi-standalone</strong> application that has symlinks to the OS files.</p>
<p... | 3 | 2010-01-11T17:42:56Z | [
"python",
"osx-snow-leopard",
"osx-leopard",
"py2app"
] |
What are some of the core conceptual differences between C# and Python? | 1,351,227 | <p>I'm new to Python, coming from a C# background and I'm trying to get up to speed. <strike>I understand that Python is dynamically typed, whereas C# is strongly-typed.</strike> -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exi... | 3 | 2009-08-29T11:39:35Z | 1,351,287 | <p>There are a lot of differences between C# and Python; rather than dwell on the individual differences, it's probably better just to look at how Python works using a guide such as <a href="http://www.diveintopython.org/" rel="nofollow">Dive Into Python</a>. And remember, while Python allows you to do OOP very well, i... | 3 | 2009-08-29T12:06:08Z | [
"c#",
"asp.net",
"python",
"django",
"programming-languages"
] |
What are some of the core conceptual differences between C# and Python? | 1,351,227 | <p>I'm new to Python, coming from a C# background and I'm trying to get up to speed. <strike>I understand that Python is dynamically typed, whereas C# is strongly-typed.</strike> -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exi... | 3 | 2009-08-29T11:39:35Z | 1,351,334 | <p>The conceptual differences are important, but mostly in how they result in different attitudes. </p>
<p>Most important of those are "duck typing". Ie, forget what type things are, you don't need to care. You only need to care about what attributes and methods objects have. "If it looks like a duck and walks like a ... | 1 | 2009-08-29T12:33:40Z | [
"c#",
"asp.net",
"python",
"django",
"programming-languages"
] |
What are some of the core conceptual differences between C# and Python? | 1,351,227 | <p>I'm new to Python, coming from a C# background and I'm trying to get up to speed. <strike>I understand that Python is dynamically typed, whereas C# is strongly-typed.</strike> -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exi... | 3 | 2009-08-29T11:39:35Z | 1,351,338 | <h2>duck typing</h2>
<p>I think the main thing that sets c#/java from python is that there is often no need for interfaces. This is because python has <a href="http://en.wikipedia.org/wiki/Duck%5Ftyping" rel="nofollow">ducktyping</a>. </p>
<pre><code>class Duck(object):
def quack(self):
print "quack"
cla... | 4 | 2009-08-29T12:34:31Z | [
"c#",
"asp.net",
"python",
"django",
"programming-languages"
] |
What are some of the core conceptual differences between C# and Python? | 1,351,227 | <p>I'm new to Python, coming from a C# background and I'm trying to get up to speed. <strike>I understand that Python is dynamically typed, whereas C# is strongly-typed.</strike> -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exi... | 3 | 2009-08-29T11:39:35Z | 1,351,664 | <p>You said that Python is dynamically typed and C# is strongly typed but this isn't true. Strong vs. weak typing and static vs. dynamic typing are orthagonal. Strong typing means str + int doesn't coerce one of the opperands, so in this regard both Python and C# are strongly typed (whereas PHP or C is weakly typed).... | 2 | 2009-08-29T15:10:48Z | [
"c#",
"asp.net",
"python",
"django",
"programming-languages"
] |
What are some of the core conceptual differences between C# and Python? | 1,351,227 | <p>I'm new to Python, coming from a C# background and I'm trying to get up to speed. <strike>I understand that Python is dynamically typed, whereas C# is strongly-typed.</strike> -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exi... | 3 | 2009-08-29T11:39:35Z | 1,351,670 | <p>" I understand that Python is dynamically typed, whereas C# is strongly-typed. "</p>
<p>This is weirdly wrong.</p>
<ol>
<li><p>Python is strongly typed. A list or integer or dictionary is always of the given type. The object's type cannot be changed.</p></li>
<li><p>Python variables are not strongly typed. Inde... | 9 | 2009-08-29T15:11:48Z | [
"c#",
"asp.net",
"python",
"django",
"programming-languages"
] |
Object store for objects in Django between requests | 1,351,323 | <p>I had the following idea: Say we have a webapp written using django which models some kind of bulletin board. This board has many threads but a few of them get the most posts/views per hour.
The thread pages look a little different for each user, so you can't cache the rendered page as whole and caching only some pa... | 5 | 2009-08-29T12:24:56Z | 1,351,459 | <p>An in memory storage is not persistent, so no. </p>
<p>I think you mean that you only want to write to the database ever X new posts of objects. I guess this is for speedup reasons. But since you need to serialize them sooner or later anyway, you don't actually save any time that way. However, you will save time by... | 0 | 2009-08-29T13:37:27Z | [
"python",
"django"
] |
Object store for objects in Django between requests | 1,351,323 | <p>I had the following idea: Say we have a webapp written using django which models some kind of bulletin board. This board has many threads but a few of them get the most posts/views per hour.
The thread pages look a little different for each user, so you can't cache the rendered page as whole and caching only some pa... | 5 | 2009-08-29T12:24:56Z | 1,352,415 | <p>In a production WSGI environment, you would probably have multiple worker processes serving requests at the same time. These worker processes would be recycled from time to time, meaning local memory objects would be lost.</p>
<p>But if you really need this (and make sure you do), I suggest you look into Django's <... | 5 | 2009-08-29T21:24:11Z | [
"python",
"django"
] |
How to make custom buttons in wx? | 1,351,448 | <p>I'd like to make a custom button in wxPython. Where should I start, how should I do it?</p>
| 4 | 2009-08-29T13:31:52Z | 1,351,475 | <p>You can extend the default button class, like this for example:</p>
<pre><code>class RedButton(wx.Button):
def __init__(self, *a, **k):
wx.Button.__init__(self, *a, **k)
self.SetBackgroundColour('RED')
# more customization here
</code></pre>
<p>Every time you put a <code>RedButton</code... | 3 | 2009-08-29T13:43:46Z | [
"python",
"button",
"wxpython"
] |
How to make custom buttons in wx? | 1,351,448 | <p>I'd like to make a custom button in wxPython. Where should I start, how should I do it?</p>
| 4 | 2009-08-29T13:31:52Z | 1,351,690 | <p>Try using a <a href="http://www.wxpython.org/docs/api/wx.lib.buttons-module.html" rel="nofollow">Generic Button</a> or a <a href="http://www.wxpython.org/docs/api/wx.BitmapButton-class.html" rel="nofollow">Bitmap Button</a>.</p>
| 2 | 2009-08-29T15:19:49Z | [
"python",
"button",
"wxpython"
] |
How to make custom buttons in wx? | 1,351,448 | <p>I'd like to make a custom button in wxPython. Where should I start, how should I do it?</p>
| 4 | 2009-08-29T13:31:52Z | 1,351,727 | <p>When I wanted to learn how to make custom widgets (buttons included) I referenced <a href="http://wiki.wxpython.org/CreatingCustomControls">Andrea Gavana's page</a> (full working example there) on the wxPyWiki and Cody Precord's platebutton (the source is in wx.lib.platebtn, also <a href="http://svn.wxwidgets.org/vi... | 5 | 2009-08-29T15:36:54Z | [
"python",
"button",
"wxpython"
] |
How to make custom buttons in wx? | 1,351,448 | <p>I'd like to make a custom button in wxPython. Where should I start, how should I do it?</p>
| 4 | 2009-08-29T13:31:52Z | 1,361,336 | <p>Here is a skeleton which you can use to draw totally custom button, its up to your imagination how it looks or behaves</p>
<pre><code>class MyButton(wx.PyControl):
def __init__(self, parent, id, bmp, text, **kwargs):
wx.PyControl.__init__(self,parent, id, **kwargs)
self.Bind(wx.EVT_LEFT_DOWN, ... | 7 | 2009-09-01T08:55:55Z | [
"python",
"button",
"wxpython"
] |
IBOutlet in Python | 1,351,480 | <p>Is there a way to use a Cocoa IBOutlet in Python? Or do I need to do this in ObjC? Thanks in advance.</p>
| 0 | 2009-08-29T13:45:08Z | 1,351,594 | <p><a href="http://lethain.com/entry/2008/aug/22/an-epic-introduction-to-pyobjc-and-cocoa/" rel="nofollow">This article</a> (found by searching Google for <a href="http://www.google.com/search?ie=utf-8&oe=utf-8&num=20&q=pyobjc+iboutlet" rel="nofollow">âpyobjc iboutletâ</a>) has an example. Basically, yo... | 1 | 2009-08-29T14:35:27Z | [
"python",
"objective-c",
"cocoa"
] |
How do you make a PDF searchable with text in the sidebar? | 1,351,510 | <p>I'm looking to create some PDF's from Python.</p>
<p>I've noticed that some pdf's have sidebar text that allows you to see the context of occurrences of search terms.</p>
<p>e.g. search for "dictionary"</p>
<p>View in Sidebar:</p>
<p>Page 10 Assigning a value to an existing <strong>dictionary</strong> key simply... | 2 | 2009-08-29T13:59:52Z | 1,352,775 | <p>I believe what you're referring to are bookmarks. The <a href="http://www.ehow.com/how%5F10082%5Fcreate-bookmarks-pdf.html" rel="nofollow">first hit on Google</a> indicates that you can put them in by hand with Acrobat Pro.</p>
<p>The <a href="http://docbook.sourceforge.net/" rel="nofollow">DocBook XSL</a> template... | 0 | 2009-08-30T01:07:33Z | [
"python",
"pdf"
] |
How do you make a PDF searchable with text in the sidebar? | 1,351,510 | <p>I'm looking to create some PDF's from Python.</p>
<p>I've noticed that some pdf's have sidebar text that allows you to see the context of occurrences of search terms.</p>
<p>e.g. search for "dictionary"</p>
<p>View in Sidebar:</p>
<p>Page 10 Assigning a value to an existing <strong>dictionary</strong> key simply... | 2 | 2009-08-29T13:59:52Z | 1,405,464 | <p>If you use <a href="http://www.reportlab.org/" rel="nofollow">Reportlab</a> to generate your pdfs, then there are facilities in the library to bookmark as you want. Checkout the <code>bookmarkPage</code> method on page 54 of the documentation.</p>
| 1 | 2009-09-10T14:00:40Z | [
"python",
"pdf"
] |
How do you make a PDF searchable with text in the sidebar? | 1,351,510 | <p>I'm looking to create some PDF's from Python.</p>
<p>I've noticed that some pdf's have sidebar text that allows you to see the context of occurrences of search terms.</p>
<p>e.g. search for "dictionary"</p>
<p>View in Sidebar:</p>
<p>Page 10 Assigning a value to an existing <strong>dictionary</strong> key simply... | 2 | 2009-08-29T13:59:52Z | 1,428,147 | <p>The <a href="http://www.riverbankcomputing.com" rel="nofollow">PyQt gui toolkit</a> has support for creating PDF's. See for example: <a href="http://doc.trolltech.com/4.5/richtext-common-tasks.html" rel="nofollow">Printing Rich Text with Qt</a></p>
| 0 | 2009-09-15T16:14:50Z | [
"python",
"pdf"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,351,688 | <p>You're going to need to move the definition of <code>a.lut</code> outside of the definition of <code>a</code>. </p>
<pre><code>class a():
def spam():pass
a.lut = [1,2,3,a.spam]
</code></pre>
<p>If you think about it, this makes perfect sense. Using self wouldn't work because <code>self</code> is actually on... | -1 | 2009-08-29T15:18:48Z | [
"python",
"class",
"reference"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,351,693 | <p>In the clas body, you're creating the class; there is no <code>self</code>, so you obviously cannot yet refer to <code>self.anything</code>. But also within that body there is as yet no <code>a</code>: name <code>a</code> gets bound AFTER the class body is done. So, although that's a tad less obvious, in the body of... | 4 | 2009-08-29T15:20:34Z | [
"python",
"class",
"reference"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,351,694 | <p>While you are in the scope of the class, you can just write</p>
<pre><code>class A:
def spam(self):
pass
lut = [1, 2, 3, spam]
a = A()
print a.lut
</code></pre>
<p>gives</p>
<pre><code>[1, 2, 3, <function spam at 0xb7bb764c>]
</code></pre>
<p>Don't forget that this is a <em>function</em> ... | 2 | 2009-08-29T15:20:35Z | [
"python",
"class",
"reference"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,351,716 | <p>I'm not sure I understand the question. Is this what you mean?</p>
<pre><code>class a:
lut = [1,
3,
17,
[12,34],
5]
def __init__(self):
self.lut.append(self.spam)
def spam(self, a):
print "this is %s" % a
b = a()
b.lut[-1](4)
</code></pre>
... | 0 | 2009-08-29T15:31:35Z | [
"python",
"class",
"reference"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,352,027 | <p>Trivial:</p>
<pre><code>class a:
@classmethod
def spam(cls):
# not really pass, but you get the idea
pass
lut = [1,
3,
17,
[12,34],
5,
spam]
assert a().lut[-1] == a.spam
assert a.spam() is None
</code></pre>
| 0 | 2009-08-29T18:14:54Z | [
"python",
"class",
"reference"
] |
referencing class methods in class lists in Python | 1,351,669 | <p>I am writing a class in Python 2.6.2 that contains a lookup table. Most cases are simple enough that the table contains data. Some of the cases are more complex and I want to be able call a function. However, I'm running into some trouble referencing the function.</p>
<p>Here's some sample code:</p>
<pre><code... | 1 | 2009-08-29T15:11:45Z | 1,352,190 | <p>You want the function to be bound to the class. None of the answers seem to address this.</p>
<p>This won't be done automatically; when you do this:</p>
<pre><code>class a:
def spam(self): print self
lut = [1, spam]
</code></pre>
<p><code>lut[1]</code> is spam itself, not a bound method to an object, so ... | 0 | 2009-08-29T19:27:34Z | [
"python",
"class",
"reference"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.