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 |
|---|---|---|---|---|---|---|---|---|---|
Python Lambda behaviour | 2,294,134 | <p>I'm trying to get my head around lambda expressions, closures and scoping in Python. Why does the program not crash on the first line here?</p>
<pre><code>>>> foo = lambda x: x + a
>>> foo(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>... | 4 | 2010-02-19T04:52:12Z | 2,294,155 | <p>on the first line you create expression, which is different from evaluating it. When you try to evaluate it, this is then it cannot find symbol a .</p>
| 0 | 2010-02-19T05:00:06Z | [
"python",
"lambda"
] |
Python Lambda behaviour | 2,294,134 | <p>I'm trying to get my head around lambda expressions, closures and scoping in Python. Why does the program not crash on the first line here?</p>
<pre><code>>>> foo = lambda x: x + a
>>> foo(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>... | 4 | 2010-02-19T04:52:12Z | 2,294,165 | <p>Because that's just not how Python functions work; it's not special to lambdas:</p>
<pre><code>>>> def foo(x):
... return x + a
>>> foo
<function foo at 0xb7dde454>
>>> foo(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>... | 6 | 2010-02-19T05:01:38Z | [
"python",
"lambda"
] |
Python Lambda behaviour | 2,294,134 | <p>I'm trying to get my head around lambda expressions, closures and scoping in Python. Why does the program not crash on the first line here?</p>
<pre><code>>>> foo = lambda x: x + a
>>> foo(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>... | 4 | 2010-02-19T04:52:12Z | 2,294,217 | <p>Variables in Python may be used before they are set. This will generate a runtime error, not a syntax error. Here's an example using local variables:</p>
<pre><code>>>> def f():
... return a
... a = 3
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <... | 2 | 2010-02-19T05:16:20Z | [
"python",
"lambda"
] |
Issue with python class hierarchy | 2,294,200 | <p>I have a class hierarchy:</p>
<pre><code>class ParentClass:
def do_something(self):
pass # child classes have their own implementation of this
class ChildClass1(ParentClass):
def do_something(self):
<implementation here>
class ChildClass2(ParentClass):
def do_something(self, a... | 2 | 2010-02-19T05:10:04Z | 2,294,244 | <p>Abstract questions like this are very difficult to answer. It would be much easier to answer if we knew what problem you were trying to solve. I can tell you that usually it is a bad sign to see something like:</p>
<pre><code>if isinstance(obj, cls1):
...
elif isinstance(obj, cls2):
...
</code></pre>
<p>... | 2 | 2010-02-19T05:23:52Z | [
"python",
"oop"
] |
Issue with python class hierarchy | 2,294,200 | <p>I have a class hierarchy:</p>
<pre><code>class ParentClass:
def do_something(self):
pass # child classes have their own implementation of this
class ChildClass1(ParentClass):
def do_something(self):
<implementation here>
class ChildClass2(ParentClass):
def do_something(self, a... | 2 | 2010-02-19T05:10:04Z | 2,294,349 | <p>You could do this, to make the signatures the same:</p>
<pre><code>class ParentClass:
pass
class ChildClass1(ParentClass):
def do_something(self, **kwargs):
<implementation here>
class ChildClass2(ParentClass):
def do_something(self, **kwargs):
argument_x = kwargs[argument_x]
... | -1 | 2010-02-19T05:51:28Z | [
"python",
"oop"
] |
Issue with python class hierarchy | 2,294,200 | <p>I have a class hierarchy:</p>
<pre><code>class ParentClass:
def do_something(self):
pass # child classes have their own implementation of this
class ChildClass1(ParentClass):
def do_something(self):
<implementation here>
class ChildClass2(ParentClass):
def do_something(self, a... | 2 | 2010-02-19T05:10:04Z | 2,294,420 | <p>If things are to be used interchangeably, they should have the the same interface. For a method, this means the same number of arguments with the same meanings and the same names in the same order. If they do not behave exactly the same, simply give them different names and make it look like they are interchangable.... | 1 | 2010-02-19T06:08:29Z | [
"python",
"oop"
] |
Issue with python class hierarchy | 2,294,200 | <p>I have a class hierarchy:</p>
<pre><code>class ParentClass:
def do_something(self):
pass # child classes have their own implementation of this
class ChildClass1(ParentClass):
def do_something(self):
<implementation here>
class ChildClass2(ParentClass):
def do_something(self, a... | 2 | 2010-02-19T05:10:04Z | 2,295,784 | <p>Methods with the same name and different arguments are a code smell.</p>
<p><strong>"method do_something() has different interfaces in subclasses: it accepts an argument in child classes 2 and 3, but has no argument in child class 1"</strong></p>
<p>You don't say why. There are two good reasons why </p>
<ul>
<li>... | 5 | 2010-02-19T11:05:50Z | [
"python",
"oop"
] |
Deferred evaluation in python | 2,294,236 | <p>I have heard of deferred evaluation in python (for example <a href="http://wiki.python.org/moin/AlternateLambdaSyntax">here</a>), is it just referring to how lambdas are evaluated by the interpreter only when they are used? Or is this the proper term for describing how, due to python's dynamic design, it will not ca... | 8 | 2010-02-19T05:18:34Z | 2,294,285 | <p>Deferred evaluation is when an expression isn't evaluated until it is needed. In most languages, you use something like <code>lambda</code> to make this work. Here's a contrived example that shows part of the concept:</p>
<pre><code>def list_files():
for fn in os.listdir('.'):
yield fn, lambda: open(f... | 13 | 2010-02-19T05:36:02Z | [
"python",
"lambda",
"interpreter",
"evaluation"
] |
Deferred evaluation in python | 2,294,236 | <p>I have heard of deferred evaluation in python (for example <a href="http://wiki.python.org/moin/AlternateLambdaSyntax">here</a>), is it just referring to how lambdas are evaluated by the interpreter only when they are used? Or is this the proper term for describing how, due to python's dynamic design, it will not ca... | 8 | 2010-02-19T05:18:34Z | 2,294,489 | <p>Dietrich's answer is a good one, but I just want to add that the simplest form of deferred evaluation is the <code>if</code> statement:</p>
<pre><code>if True:
x = 5
else:
x = y # huh? what is y?
</code></pre>
<p>This code parses and runs correctly, although the <code>else</code> clause makes no sense - <co... | 7 | 2010-02-19T06:32:01Z | [
"python",
"lambda",
"interpreter",
"evaluation"
] |
How to get the position of a character in Python? | 2,294,493 | <p>How can I get the position of a character inside a string in python?</p>
| 199 | 2010-02-19T06:32:57Z | 2,294,502 | <p>There are two string methods for this, <code>find</code> and <code>index</code>.</p>
<p>Example:</p>
<pre><code>>>> myString = "Position of a character"
>>> myString.index('s')
2
</code></pre>
<p>The difference is that <code>find</code> returns <code>-1</code> when what you're looking for isn't ... | 244 | 2010-02-19T06:35:27Z | [
"python",
"string"
] |
How to get the position of a character in Python? | 2,294,493 | <p>How can I get the position of a character inside a string in python?</p>
| 199 | 2010-02-19T06:32:57Z | 2,294,510 | <pre><code>>>> s="mystring"
>>> s.index("r")
4
>>> s.find("r")
4
</code></pre>
<p>"Long winded" way</p>
<pre><code>>>> for i,c in enumerate(s):
... if "r"==c: print i
...
4
</code></pre>
<p>to get substring, </p>
<pre><code>>>> s="mystring"
>>> s[4:10]
'ring'
... | 26 | 2010-02-19T06:36:47Z | [
"python",
"string"
] |
How to get the position of a character in Python? | 2,294,493 | <p>How can I get the position of a character inside a string in python?</p>
| 199 | 2010-02-19T06:32:57Z | 2,294,513 | <pre><code>string.find(character)
string.index(character)
</code></pre>
<p>Perhaps you'd like to have a look at <a href="http://docs.python.org/library/stdtypes.html#string-methods" rel="nofollow">the documentation</a> to find out what the difference between the two is.</p>
| 3 | 2010-02-19T06:37:36Z | [
"python",
"string"
] |
How to get the position of a character in Python? | 2,294,493 | <p>How can I get the position of a character inside a string in python?</p>
| 199 | 2010-02-19T06:32:57Z | 31,161,614 | <p>but what happens when the string contains a duplicate character?
from my experience with index() I saw that for duplicate you get back the same index
for example:</p>
<pre><code>s = 'abccde'
for c in s:
print '%s,%d' %(c , s.index(c))
</code></pre>
<p>would return:</p>
<pre><code>a,0
b,1
c,2
c,2
d,4
</code></... | 6 | 2015-07-01T12:40:29Z | [
"python",
"string"
] |
How to get the position of a character in Python? | 2,294,493 | <p>How can I get the position of a character inside a string in python?</p>
| 199 | 2010-02-19T06:32:57Z | 32,794,963 | <p>Just for a sake of completeness, if you need to find all positions of a substring in a string, you can do the following:</p>
<pre><code>s = 'shak#spea#e'
c = '#'
print [pos for pos, char in enumerate(s) if char == c]
</code></pre>
<p>which will return <code>[4, 9]</code></p>
| 17 | 2015-09-26T07:59:52Z | [
"python",
"string"
] |
'ASCII' to Unicode error in python when attempting to read a latin-1 encoded string | 2,294,509 | <p>I'm having a problem when trying to apply a regular expression to some strings encoded in latin-1 (ISO-8859-1). </p>
<p>What I'm trying to do is send some data via HTTP POST from a page encoded in ISO-8859-1 to my python application and do some parsing on the data using regular expressions in my python script.</p>
... | 1 | 2010-02-19T06:36:29Z | 2,294,538 | <p>Try this instead:</p>
<pre><code>print(repr(unicode(data, 'iso-8859-1')))
</code></pre>
<p>by printing a unicode object you're implicitly trying to convert it to the default encoding, which is ASCII. Using <code>repr</code> will escape it into an ASCII-safe form, plus it'll be easier for you to figure out what's g... | 2 | 2010-02-19T06:43:02Z | [
"python",
"encoding"
] |
'ASCII' to Unicode error in python when attempting to read a latin-1 encoded string | 2,294,509 | <p>I'm having a problem when trying to apply a regular expression to some strings encoded in latin-1 (ISO-8859-1). </p>
<p>What I'm trying to do is send some data via HTTP POST from a page encoded in ISO-8859-1 to my python application and do some parsing on the data using regular expressions in my python script.</p>
... | 1 | 2010-02-19T06:36:29Z | 2,294,548 | <p>Are you using Python 3.X or 2.X? It makes a difference. Actually looks like 2.X but you confused me by using print(blahblah) :-)</p>
<p>Answer to your last question: Yes, ASCII by default when you do <code>print()</code>. On 3.X: Use <code>print(ascii(foo))</code> for debugging, not <code>print(foo)</code>. On 2.X ... | 1 | 2010-02-19T06:46:02Z | [
"python",
"encoding"
] |
Python : How to plot 3d graphs using Python? | 2,294,588 | <p>I am using matplotlib for doing this</p>
<pre><code>from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
</code></p... | 19 | 2010-02-19T06:57:13Z | 2,294,670 | <p>According to the documentation you want to use the <code>ax.plot_surface(x,y,z)</code> method. More information and chart types <a href="http://www.scipy.org/Cookbook/Matplotlib/mplot3D" rel="nofollow">here</a>.</p>
<p>The following should work:</p>
<pre><code>x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)
... | 3 | 2010-02-19T07:21:53Z | [
"python",
"matplotlib"
] |
Python : How to plot 3d graphs using Python? | 2,294,588 | <p>I am using matplotlib for doing this</p>
<pre><code>from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
</code></p... | 19 | 2010-02-19T06:57:13Z | 2,294,994 | <p>bp's answer might work fine, but there's a much simpler way.</p>
<p>Your current graph is 'flat' on the z-axis, which is why it's horizontal. You want it to be vertical, which means that you want it to be 'flat' on the y-axis. This involves the tiniest modification to your code:</p>
<pre><code>from mpl_toolkits.mp... | 11 | 2010-02-19T08:51:33Z | [
"python",
"matplotlib"
] |
Python : How to plot 3d graphs using Python? | 2,294,588 | <p>I am using matplotlib for doing this</p>
<pre><code>from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
</code></p... | 19 | 2010-02-19T06:57:13Z | 4,042,582 | <p>Mayavi, in particular the <a href="http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html">mlab</a> module, provides powerful 3D plotting that will work on large and or complex data, and should be easy to use on numpy arrays.</p>
| 7 | 2010-10-28T11:49:53Z | [
"python",
"matplotlib"
] |
Python : How to plot 3d graphs using Python? | 2,294,588 | <p>I am using matplotlib for doing this</p>
<pre><code>from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
</code></p... | 19 | 2010-02-19T06:57:13Z | 12,330,848 | <p>You can set the view angle of the 3d plot with the view_init() function. The example below is for version 1.1 of matplotlib.</p>
<pre><code>from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x ... | 7 | 2012-09-08T12:48:51Z | [
"python",
"matplotlib"
] |
how do i get the username who login use openid ,i use django,pinax | 2,294,597 | <p>1.</p>
<p><img src="http://omploader.org/vM2xubg" alt="alt text"></p>
<p>2</p>
<p><img src="http://omploader.org/vM2xubw" alt="alt text"></p>
<p>3.</p>
<p><img src="http://omploader.org/vM2xucA" alt="alt text"></p>
<p>in this example,my username is zjm1126 ,and my email is zjm1126@yahoo.com,</p>
<p>i want to ... | -1 | 2010-02-19T07:00:27Z | 2,294,711 | <p>Some open id providers provide the info about the user, which they send to you as a post, to the callback url. Some others don't.</p>
<p>If you want to obtain this extra info, as long as people are allowed to choose any open id, you should prompt for this information. Thats precisely what stackoverflow does.</p>
<... | 0 | 2010-02-19T07:32:18Z | [
"python",
"django",
"openid",
"pinax"
] |
how to write integer number in particular no of bytes in python ( file writing) | 2,294,608 | <p>assume i have to store few integer numbers like 1024 or 512 or 10240 or 900000 in a file, but the condition is that i can consume only 4 bytes (not less nor max).but while writing a python file using write method it stored as "1024" or "512" or "10240" ie they written as ascii value but i want to store directly thei... | 1 | 2010-02-19T07:03:53Z | 2,294,631 | <p>use the <a href="http://docs.python.org/library/struct.html" rel="nofollow"><strong>struct</strong></a> module</p>
<pre><code>>>> import struct
>>> struct.pack("i",1024)
'\x00\x04\x00\x00'
>>> struct.pack("i",10240)
'\x00(\x00\x00'
>>> struct.pack("i",900000)
'\xa0\xbb\r\x00'
</c... | 9 | 2010-02-19T07:11:37Z | [
"python"
] |
how to write integer number in particular no of bytes in python ( file writing) | 2,294,608 | <p>assume i have to store few integer numbers like 1024 or 512 or 10240 or 900000 in a file, but the condition is that i can consume only 4 bytes (not less nor max).but while writing a python file using write method it stored as "1024" or "512" or "10240" ie they written as ascii value but i want to store directly thei... | 1 | 2010-02-19T07:03:53Z | 2,294,649 | <p><a href="http://docs.python.org/library/struct.html" rel="nofollow">The struct module</a> will do</p>
<pre><code>>>> import struct
>>> f = open('binary.bin','wb')
>>> f.write(struct.pack("l",1024))
>>> f.close()
vinko@parrot:~$ xxd -b binary.bin
0000000: 00000000 00000100 000000... | 4 | 2010-02-19T07:17:13Z | [
"python"
] |
assign multiple instances of a class to variables | 2,294,823 | <p>Python. I need to assign multiple class instances to number of variables. First i tried this:</p>
<pre><code>a = b = c = []
</code></pre>
<p>but they all refer to the same object, which is not what I need. This works better:</p>
<pre><code>(a, b, c) = [[] for i in range(3)]
</code></pre>
<p>but it seems a bit to... | 2 | 2010-02-19T08:02:03Z | 2,294,836 | <p>simply:</p>
<pre><code>a = []
b = []
c = []
</code></pre>
<p>(python is not perl, there is no need for one-liners)</p>
| 8 | 2010-02-19T08:06:16Z | [
"python"
] |
assign multiple instances of a class to variables | 2,294,823 | <p>Python. I need to assign multiple class instances to number of variables. First i tried this:</p>
<pre><code>a = b = c = []
</code></pre>
<p>but they all refer to the same object, which is not what I need. This works better:</p>
<pre><code>(a, b, c) = [[] for i in range(3)]
</code></pre>
<p>but it seems a bit to... | 2 | 2010-02-19T08:02:03Z | 2,294,847 | <pre><code>a, b, c = [], [], []
</code></pre>
| 9 | 2010-02-19T08:08:56Z | [
"python"
] |
assign multiple instances of a class to variables | 2,294,823 | <p>Python. I need to assign multiple class instances to number of variables. First i tried this:</p>
<pre><code>a = b = c = []
</code></pre>
<p>but they all refer to the same object, which is not what I need. This works better:</p>
<pre><code>(a, b, c) = [[] for i in range(3)]
</code></pre>
<p>but it seems a bit to... | 2 | 2010-02-19T08:02:03Z | 2,295,796 | <pre><code>a = []
b = a[:]
import copy
c = copy.copy(b)
</code></pre>
<p>You could use these if you want them all initialized to something other than []</p>
| 0 | 2010-02-19T11:08:04Z | [
"python"
] |
Replace a whole line in a file | 2,294,848 | <p>I have a .txt file containing data like this:</p>
<pre><code>0,Rent1,Expense,16/02/2010,1,4000,4000
0,Car Loan1,Expense,16/02/2010,2,4500,9000
0,Flat Loan1,Expense,16/02/2010,2,4000,8000
0,Rent2,Expense,16/02/2010,1,4000,4000
0,Car Loan2,Expense,16/02/2010,2,4500,9000
0,Flat Loan2,Expense,16/02/2010,2,400... | 0 | 2010-02-19T08:09:05Z | 2,294,852 | <p><code>sys.stdout.write()</code> does not add a newline. Either append a <code>\n</code>, use <code>print</code> after, or use <code>print</code>'s file syntax:</p>
<pre><code>print >>sys.stdout, result
</code></pre>
<p>... Or since <code>sys.stdout</code> is the default:</p>
<pre><code>print result
</code><... | 0 | 2010-02-19T08:11:06Z | [
"python",
"file",
"replace"
] |
Replace a whole line in a file | 2,294,848 | <p>I have a .txt file containing data like this:</p>
<pre><code>0,Rent1,Expense,16/02/2010,1,4000,4000
0,Car Loan1,Expense,16/02/2010,2,4500,9000
0,Flat Loan1,Expense,16/02/2010,2,4000,8000
0,Rent2,Expense,16/02/2010,1,4000,4000
0,Car Loan2,Expense,16/02/2010,2,4500,9000
0,Flat Loan2,Expense,16/02/2010,2,400... | 0 | 2010-02-19T08:09:05Z | 2,294,857 | <p>Replace:</p>
<pre><code>result = newline #you lose the newline here
</code></pre>
<p>with:</p>
<pre><code>result = newline + '\n' #or '\r\n' depending on your newline format.
</code></pre>
| 0 | 2010-02-19T08:12:35Z | [
"python",
"file",
"replace"
] |
Replace a whole line in a file | 2,294,848 | <p>I have a .txt file containing data like this:</p>
<pre><code>0,Rent1,Expense,16/02/2010,1,4000,4000
0,Car Loan1,Expense,16/02/2010,2,4500,9000
0,Flat Loan1,Expense,16/02/2010,2,4000,8000
0,Rent2,Expense,16/02/2010,1,4000,4000
0,Car Loan2,Expense,16/02/2010,2,4500,9000
0,Flat Loan2,Expense,16/02/2010,2,400... | 0 | 2010-02-19T08:09:05Z | 2,294,863 | <p>Put a new line </p>
<pre><code>newline='0,Loan,Expense,19/02/2010,2,5000,10000'
lnum=1
for line in fileinput.FileInput("sample.txt",inplace=1):
if lnum==4:
result = newline+"\n"
else:
result=line
lnum=lnum+1
sys.stdout.write(result)
</code></pre>
<p>Or you can just declare <code... | 2 | 2010-02-19T08:14:11Z | [
"python",
"file",
"replace"
] |
Replace a whole line in a file | 2,294,848 | <p>I have a .txt file containing data like this:</p>
<pre><code>0,Rent1,Expense,16/02/2010,1,4000,4000
0,Car Loan1,Expense,16/02/2010,2,4500,9000
0,Flat Loan1,Expense,16/02/2010,2,4000,8000
0,Rent2,Expense,16/02/2010,1,4000,4000
0,Car Loan2,Expense,16/02/2010,2,4500,9000
0,Flat Loan2,Expense,16/02/2010,2,400... | 0 | 2010-02-19T08:09:05Z | 2,297,284 | <p>Use <strong>enumerate</strong>: <em>less code less error posibilities</em></p>
<pre><code>newline = '0,Loan,Expense,19/02/2010,2,5000,10000\n'
for lnum, line in enumerate(fileinput.FileInput("sample.txt",inplace=1)):
if lnum == 4:
line = newline
sys.stdout.write(line)
</code></pre>
| 1 | 2010-02-19T15:11:27Z | [
"python",
"file",
"replace"
] |
Maintaining session in an Eventlet page scraper? | 2,294,869 | <p>I'm trying to do some scraping of a site that requires authentication (not http auth). The script I'm using is based on <a href="http://www.eventlet.net/doc/examples.html#web-crawler" rel="nofollow">this eventlet example</a>. Basically,</p>
<pre><code>urls = ["https://mysecuresite.com/data.aspx?itemid=blah1",
... | 3 | 2010-02-19T08:16:13Z | 2,294,892 | <p>You can use <a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">the <code>mechanize</code> library</a> to make the session establishing easier, then use one of the different threading/multiprocessing techniques like this <a href="http://code.activestate.com/recipes/203871/" rel="nofollow">threading ... | 0 | 2010-02-19T08:24:44Z | [
"python",
"parallel-processing",
"screen-scraping"
] |
Maintaining session in an Eventlet page scraper? | 2,294,869 | <p>I'm trying to do some scraping of a site that requires authentication (not http auth). The script I'm using is based on <a href="http://www.eventlet.net/doc/examples.html#web-crawler" rel="nofollow">this eventlet example</a>. Basically,</p>
<pre><code>urls = ["https://mysecuresite.com/data.aspx?itemid=blah1",
... | 3 | 2010-02-19T08:16:13Z | 2,300,896 | <p>I'm not an expert on this by any means, but it looks like the standard way to maintain session state with urllib2 is to create a custom opener instance for each session. That looks like this:</p>
<pre><code>opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
</code></pre>
<p>Then you use that opener to d... | 4 | 2010-02-20T03:17:37Z | [
"python",
"parallel-processing",
"screen-scraping"
] |
Maintaining session in an Eventlet page scraper? | 2,294,869 | <p>I'm trying to do some scraping of a site that requires authentication (not http auth). The script I'm using is based on <a href="http://www.eventlet.net/doc/examples.html#web-crawler" rel="nofollow">this eventlet example</a>. Basically,</p>
<pre><code>urls = ["https://mysecuresite.com/data.aspx?itemid=blah1",
... | 3 | 2010-02-19T08:16:13Z | 2,300,995 | <p>Like suggested below, use <a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">mechanize</a>. It'll take care of the low-level details, like cookie management for you. </p>
<p>However, to make a 3rd party library work with eventlet you need to replace socket and ssl objects from stdlib with somethin... | 1 | 2010-02-20T04:07:12Z | [
"python",
"parallel-processing",
"screen-scraping"
] |
How to open a Pyqt 3.3 ui file with QtDesigner 4? | 2,294,956 | <p>I've a PyQt 4 installed (I can't find PyQt 3 for Windows) and I would like to open a QtDesigner ui file which has been created with QtDesigner 3.3.</p>
<p>When I open this file I've the following message:</p>
<p><code>Please use uic3 -convert to convert to Qt4</code></p>
<p>Unfortunately, I don't see the uic3 too... | 0 | 2010-02-19T08:41:25Z | 2,296,460 | <p>Try finding <code>pyuic3</code> or rather <code>pyuic4</code>. Since you are using PyQt all your qt tools have py in front (like <code>pyrcc4</code> or <code>pyuicc4</code>).</p>
<p>I am not sure, that there are pyqt3 binaries for windows. Are you sure that Phil Thompson did PyQt3 also for windows at the time? If s... | 1 | 2010-02-19T13:12:45Z | [
"python",
"qt",
"pyqt",
"qt-designer"
] |
How to open a Pyqt 3.3 ui file with QtDesigner 4? | 2,294,956 | <p>I've a PyQt 4 installed (I can't find PyQt 3 for Windows) and I would like to open a QtDesigner ui file which has been created with QtDesigner 3.3.</p>
<p>When I open this file I've the following message:</p>
<p><code>Please use uic3 -convert to convert to Qt4</code></p>
<p>Unfortunately, I don't see the uic3 too... | 0 | 2010-02-19T08:41:25Z | 2,296,509 | <p>Qt3 and Qt4 aren't fully compatible so you may need to make some manual tweaks after uic3 converts.</p>
<p>That said, the uic3 tool was installed on my system when I installed the Qt SDK, in C:\Qt\2010.01\qt\bin</p>
<p>Qt SDK: <a href="http://qt.nokia.com/downloads" rel="nofollow">http://qt.nokia.com/downloads</a>... | 2 | 2010-02-19T13:20:55Z | [
"python",
"qt",
"pyqt",
"qt-designer"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 2,295,354 | <p>In answer to your second question, the most elegant way to do this would be to use a function that takes two parameters instead of an array:</p>
<pre><code>add = lambda a, b: a + b
add(1, 3)
</code></pre>
<p>However, using lambda here is a bit silly. Python gives us the <code>operator</code> module, which provides... | 2 | 2010-02-19T09:55:40Z | [
"python",
"lambda",
"closures"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 2,295,368 | <p>Your second question has been answered, but as for your first:</p>
<blockquote>
<p>what does the closure capture exactly?</p>
</blockquote>
<p>Scoping in Python is <strike>dynamic and</strike> lexical. A closure will always remember the name and scope of the variable, not the object it's pointing to. Since all t... | 87 | 2010-02-19T09:58:52Z | [
"python",
"lambda",
"closures"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 2,295,372 | <p>you may force the capture of a variable using an argument with a default value:</p>
<pre><code>>>> for i in [0,1,2,3]:
... adders[i]=lambda a,i=i: i+a # note the dummy parameter with a default value
...
>>> print( adders[1](3) )
4
</code></pre>
<p>the idea is to declare a parameter (cleverly ... | 94 | 2010-02-19T09:59:22Z | [
"python",
"lambda",
"closures"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 2,295,659 | <p>Consider the following code:</p>
<pre><code>x = "foo"
def print_x():
print x
x = "bar"
print_x() # Outputs "bar"
</code></pre>
<p>I think most people won't find this confusing at all. It is the expected behaviour.</p>
<p>So, why do people think it would be different when it is done in a loop? I know I did ... | 9 | 2010-02-19T10:45:54Z | [
"python",
"lambda",
"closures"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 10,101,476 | <p>For completeness another answer to your second question: You could use <a href="http://docs.python.org/library/functools.html#functools.partial">partial</a> in the <a href="http://docs.python.org/library/functools.html">functools</a> module.</p>
<p>With importing add from operator as Chris Lutz proposed the example... | 8 | 2012-04-11T07:11:57Z | [
"python",
"lambda",
"closures"
] |
What do (lambda) function closures capture in Python? | 2,295,290 | <p>Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code:</p>
<pre><code>adders= [0,1,2,3]
for i in [0,1,2,3]:
adders[i]=lambda a: i+a
print adders[1](3)
</code></pre>
<p>It builds a simple array of functions that take a single inp... | 130 | 2010-02-19T09:46:31Z | 23,557,126 | <p>Here's a new example that highlights the data structure and contents of a closure, to help clarify when the enclosing context is "saved."</p>
<pre><code>def make_funcs():
i = 42
my_str = "hi"
f_one = lambda: i
i += 1
f_two = lambda: i+1
f_three = lambda: my_str
return f_one, f_two, f_... | 1 | 2014-05-09T05:19:18Z | [
"python",
"lambda",
"closures"
] |
List minimum in Python with None? | 2,295,461 | <p>Is there any clever in-built function or something that will return <code>1</code> for the <code>min()</code> example below? (I bet there is a solid reason for it not to return anything, but in my particular case I need it to disregard <code>None</code> values really bad!)</p>
<pre><code>>>> max([None, 1,2... | 13 | 2010-02-19T10:14:40Z | 2,295,480 | <p><code>None</code> is being returned</p>
<pre><code>>>> print min([None, 1,2])
None
>>> None < 1
True
</code></pre>
<p>If you want to return <code>1</code> you have to filter the <code>None</code> away:</p>
<pre><code>>>> L = [None, 1, 2]
>>> min(x for x in L if x is not None... | 28 | 2010-02-19T10:16:54Z | [
"python",
"list",
"max",
"minimum"
] |
List minimum in Python with None? | 2,295,461 | <p>Is there any clever in-built function or something that will return <code>1</code> for the <code>min()</code> example below? (I bet there is a solid reason for it not to return anything, but in my particular case I need it to disregard <code>None</code> values really bad!)</p>
<pre><code>>>> max([None, 1,2... | 13 | 2010-02-19T10:14:40Z | 2,295,493 | <p>using a generator expression:</p>
<pre><code>>>> min(value for value in [None,1,2] if value is not None)
1
</code></pre>
<p>eventually, you may use filter:</p>
<pre><code>>>> min(filter(lambda x: x is not None, [None,1,2]))
1
</code></pre>
| 3 | 2010-02-19T10:18:50Z | [
"python",
"list",
"max",
"minimum"
] |
python csv - export data and format color, text format, etc | 2,295,727 | <p>I'm exporting data from python with the csv library which works pretty good. After a web-search I can not find any information about how to format the exported data with python.</p>
<p>For example. I export a csv row within python like this.</p>
<pre><code>for hour_record in object_list:
row = list()
... | 2 | 2010-02-19T10:58:27Z | 2,295,770 | <p>CSV is used only for plain text. If you want formatting information to be contained then you must either embed HTML fragments, or you must add the attributes as separate fields. Either option will require a consumer that understands said formatting mechanism.</p>
| 2 | 2010-02-19T11:04:02Z | [
"python",
"csv"
] |
python csv - export data and format color, text format, etc | 2,295,727 | <p>I'm exporting data from python with the csv library which works pretty good. After a web-search I can not find any information about how to format the exported data with python.</p>
<p>For example. I export a csv row within python like this.</p>
<pre><code>for hour_record in object_list:
row = list()
... | 2 | 2010-02-19T10:58:27Z | 2,296,426 | <p>Instead of csv, you should use numpy.genfromtxt and numpy.savetxt:</p>
<pre><code>>>> data = numpy.genfromtxt('mydata.txt', dtype=None)
>>> <-> work with the data recarray object
>>> numpy.savetxt('results.txt', data)
</code></pre>
<p>If you look at help(numpy.savetxt) you can see ... | 0 | 2010-02-19T13:05:51Z | [
"python",
"csv"
] |
Generating recurring dates using python? | 2,295,765 | <p>How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).</p>
| 15 | 2010-02-19T11:03:17Z | 2,295,806 | <p>You can give <a href="http://labix.org/python-dateutil" rel="nofollow">dateutil</a> a try - especially its relativedelta and rrule fetures.</p>
| 0 | 2010-02-19T11:10:22Z | [
"python",
"date"
] |
Generating recurring dates using python? | 2,295,765 | <p>How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).</p>
| 15 | 2010-02-19T11:03:17Z | 2,296,384 | <pre><code>import dateutil.rrule as dr
import dateutil.parser as dp
import dateutil.relativedelta as drel
start=dp.parse("19/02/2010") # Third Friday in Feb 2010
</code></pre>
<p>This generates the third Friday of every month</p>
<pre><code>rr = dr.rrule(dr.MONTHLY,byweekday=drel.FR(3),dtstart=start, count=10)
</c... | 23 | 2010-02-19T12:59:26Z | [
"python",
"date"
] |
Generating recurring dates using python? | 2,295,765 | <p>How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).</p>
| 15 | 2010-02-19T11:03:17Z | 2,296,599 | <p>you may try to write this yourself. you will first need an iterator which generates dates separated by a given interval: </p>
<pre><code>import datetime
def dateiter(start, resolution):
date = start
while True:
yield date
date += resolution
</code></pre>
<p>now, you can generate da... | 0 | 2010-02-19T13:36:24Z | [
"python",
"date"
] |
Is the python mechanize cache feature the same as normal browser cache feature? | 2,295,812 | <p>In its document, it says "This caching behaviour can be avoided by using <code>Mechanize.OpenerDirector</code>". Does this cache influence next request of the same page?</p>
| 2 | 2010-02-19T11:11:31Z | 5,495,113 | <p>It looks to me like the "caching" the documentation refers to is talking about seekable responses, not a browser-like cache feature. For example:</p>
<pre><code>total_nr_bytes = len(response.get_data())
assert len(response.read()) == total_nr_bytes
assert len(response.read()) == 0 # we've already read the data
re... | 1 | 2011-03-31T03:45:27Z | [
"python",
"mechanize"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 2,295,954 | <p><strong>There is no such thing in the Python standard library.</strong> It's because Python is a general purpose language while PHP started as a Web oriented language.</p>
<p><strong>Nevertheless, you have 3 solutions:</strong></p>
<ul>
<li>You are in a hurry: just make your own. <code>re.sub(r'<[^>]*?>',... | 30 | 2010-02-19T11:40:07Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 2,295,958 | <p>Python doesn't have one built-in, but there are an <a href="http://www.google.com/search?q=python+strip+tags" rel="nofollow">ungodly number of implementations</a>.</p>
| 1 | 2010-02-19T11:40:58Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 2,295,964 | <p>Using <a href="http://www.crummy.com/software/BeautifulSoup/"><strong>BeautifulSoup</strong></a></p>
<pre><code>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(htmltext)
''.join([e for e in soup.recursiveChildGenerator() if isinstance(e,unicode)])
</code></pre>
| 15 | 2010-02-19T11:41:48Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 2,295,973 | <p>You won't find many builtin Python equivalents for builtin PHP HTML functions since Python is more of a general-purpose scripting language than a web development language. For HTML processing, <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> is generally recommended. </p>
| 2 | 2010-02-19T11:44:28Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 8,131,619 | <p>I built one for Python 3 using the HTMLParser class. It is more verbose than PHP's. I called it the HTMLCleaner class, and you can find the source <a href="https://github.com/cwallenpoole/PyFram-Tutorial/blob/master/wk8/fri/view/html.py" rel="nofollow">here</a> and you can find examples <a href="http://www.allen-poo... | 1 | 2011-11-15T04:46:09Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 12,362,508 | <p>There is an active state recipe for this,</p>
<p><a href="http://code.activestate.com/recipes/52281/" rel="nofollow">http://code.activestate.com/recipes/52281/</a></p>
<p>It's old code so you have to change sgml parser to HTMLparser as mentioned in the comments</p>
<p>Here is the modified code,</p>
<pre><code>im... | 1 | 2012-09-11T03:51:57Z | [
"php",
"python",
"strip"
] |
Python's equivalent to PHP's strip_tags? | 2,295,942 | <p>Python's equivalent to PHP's strip_tags?</p>
<p><a href="http://php.net/manual/en/function.strip-tags.php">http://php.net/manual/en/function.strip-tags.php</a></p>
| 16 | 2010-02-19T11:38:14Z | 19,323,337 | <pre><code>from bleach import clean
print clean("<strong>My Text</strong>", tags=[], strip=True, strip_comments=True)
</code></pre>
| 2 | 2013-10-11T16:47:46Z | [
"php",
"python",
"strip"
] |
Garbage collection of object after exception | 2,295,978 | <p>I have observed that after an exception I have an object for which constructor is not called, which causes a lock to be held. What is the best way to improve the situation? Would calling del in an except block be the solution?</p>
<pre><code>b=BigHash(DB_DIR, url)
meta = bdecode(b.get())
return meta
</code></pre>
... | 1 | 2010-02-19T11:45:14Z | 2,296,005 | <p>You need to do something like this to make sure b in unlocked</p>
<pre><code>b=BigHash(DB_DIR, url)
try:
meta = bdecode(b.get())
return meta
finally:
#unlock b here
</code></pre>
<p>A cleaner way would be if BigHash can work as a context, so you can write</p>
<pre><code>with b as BigHash(DB_DIR, url):... | 1 | 2010-02-19T11:49:39Z | [
"python",
"exception-handling",
"destructor"
] |
Garbage collection of object after exception | 2,295,978 | <p>I have observed that after an exception I have an object for which constructor is not called, which causes a lock to be held. What is the best way to improve the situation? Would calling del in an except block be the solution?</p>
<pre><code>b=BigHash(DB_DIR, url)
meta = bdecode(b.get())
return meta
</code></pre>
... | 1 | 2010-02-19T11:45:14Z | 2,296,024 | <p>No matter what, you want the lock to be released - whether or not an exception is thrown. In that case, it's probably best to release the lock/delete b in a <code>finally:</code> clause:</p>
<pre><code>b=BigHash(DB_DIR, url)
try:
meta = bdecode(b.get())
finally:
del b # or whatever you need to do to release... | 3 | 2010-02-19T11:52:23Z | [
"python",
"exception-handling",
"destructor"
] |
Garbage collection of object after exception | 2,295,978 | <p>I have observed that after an exception I have an object for which constructor is not called, which causes a lock to be held. What is the best way to improve the situation? Would calling del in an except block be the solution?</p>
<pre><code>b=BigHash(DB_DIR, url)
meta = bdecode(b.get())
return meta
</code></pre>
... | 1 | 2010-02-19T11:45:14Z | 2,298,521 | <p>Calling <code>del</code> on a name is something you prettymuch never should do. <strong>Calling <code>del</code> does not guarantee anything useful about what will happen to the underlying object.</strong> You should never depend on a <code>__del__</code> method for something you need to happen.</p>
<p><code>del</c... | 0 | 2010-02-19T18:00:27Z | [
"python",
"exception-handling",
"destructor"
] |
Weak reference callback is not called because of circular references | 2,295,993 | <p>I'm trying to write a finalizer for Python classes that have circular references. I found out that weak reference callbacks are the <a href="http://stackoverflow.com/questions/1935153/del-method-being-called-in-python-when-it-is-not-expected">way to go</a>. Unfortunately, it seems the lambda I use as a callback is n... | 3 | 2010-02-19T11:47:57Z | 2,296,066 | <p>Circular references are cleaned up automatically. There are a few exceptions, such as classes that define a <code>__del__</code> method. </p>
<p>Usually you do not need to define a <code>__del__</code> method</p>
| 0 | 2010-02-19T11:58:46Z | [
"python",
"lambda",
"weak-references"
] |
Weak reference callback is not called because of circular references | 2,295,993 | <p>I'm trying to write a finalizer for Python classes that have circular references. I found out that weak reference callbacks are the <a href="http://stackoverflow.com/questions/1935153/del-method-being-called-in-python-when-it-is-not-expected">way to go</a>. Unfortunately, it seems the lambda I use as a callback is n... | 3 | 2010-02-19T11:47:57Z | 2,296,263 | <p>When you use </p>
<pre><code> self._wr = weakref.ref(self, lambda wr, n = self.name: del_A(n))
</code></pre>
<p>the callback will only be called when <code>self</code> is about to be finalized. </p>
<p>The reason why the callback is not getting called is because</p>
<pre><code>a = A('a1')
b = B()
a.other = b ... | 2 | 2010-02-19T12:40:29Z | [
"python",
"lambda",
"weak-references"
] |
Weak reference callback is not called because of circular references | 2,295,993 | <p>I'm trying to write a finalizer for Python classes that have circular references. I found out that weak reference callbacks are the <a href="http://stackoverflow.com/questions/1935153/del-method-being-called-in-python-when-it-is-not-expected">way to go</a>. Unfortunately, it seems the lambda I use as a callback is n... | 3 | 2010-02-19T11:47:57Z | 2,397,020 | <p>I think I finally found the reason why the callback is not called in the presence of a weak reference:</p>
<p>Weak reference callbacks are not called if the <a href="http://mail.python.org/pipermail/python-dev/2003-November/040289.html" rel="nofollow">"weakref object dies before the object it
references"</a></p>
<... | 2 | 2010-03-07T16:48:30Z | [
"python",
"lambda",
"weak-references"
] |
Looking for example GUI applications written in Python for the iPhone | 2,296,068 | <p>Hey!
I have a little script I wrote in python and it actually works on the iPhone via the terminal. I am looking for code snippets or documentation for the GUI writing for the iPhone -
Actually what I need is to implement an input and some output.
nothing fancy - for now.
I have found this page: <a href="http://w... | 1 | 2010-02-19T11:59:17Z | 2,296,414 | <p>There is a GUI sample in that link. The whole app is written in Python with ObjC runtime (but no ObjC code involved). </p>
<p>Still, the ObjC "feel" cannot be avoided because UIKit is designed for and uses ObjC. Note that the GUI programmed in Python is extremely slow on the iPhoneOS. Python can be used as testing,... | 0 | 2010-02-19T13:03:38Z | [
"iphone",
"python",
"objective-c",
"user-interface"
] |
Looking for example GUI applications written in Python for the iPhone | 2,296,068 | <p>Hey!
I have a little script I wrote in python and it actually works on the iPhone via the terminal. I am looking for code snippets or documentation for the GUI writing for the iPhone -
Actually what I need is to implement an input and some output.
nothing fancy - for now.
I have found this page: <a href="http://w... | 1 | 2010-02-19T11:59:17Z | 2,298,045 | <p>You cannot write an iPhone app in Python that will run on non-jailbroken phones. Apple's SDK license prohibits interpreted code on the iPhone, which definitely excludes Python. Although you can write OS X apps in Python using PyObjC, you still need to understand the Objective-C language both for documentation and fo... | 1 | 2010-02-19T16:49:26Z | [
"iphone",
"python",
"objective-c",
"user-interface"
] |
How to run a piece of code in every view in django? | 2,296,117 | <p>I need to check user authorization in every view of one of my Django apps (I don't use Django's built in auth system) and redirect user to a "login please" page, if authorization has failed.</p>
<p>Code looks like this:</p>
<pre><code>try:
admin_from_session = request.session['admin'];
admin = Administrato... | 8 | 2010-02-19T12:09:47Z | 2,296,149 | <p>Function decorators comes to mind</p>
| 1 | 2010-02-19T12:16:20Z | [
"python",
"django"
] |
How to run a piece of code in every view in django? | 2,296,117 | <p>I need to check user authorization in every view of one of my Django apps (I don't use Django's built in auth system) and redirect user to a "login please" page, if authorization has failed.</p>
<p>Code looks like this:</p>
<pre><code>try:
admin_from_session = request.session['admin'];
admin = Administrato... | 8 | 2010-02-19T12:09:47Z | 2,296,164 | <p><a href="http://docs.djangoproject.com/en/dev/topics/http/middleware/#topics-http-middleware">Middleware</a>.</p>
| 6 | 2010-02-19T12:17:42Z | [
"python",
"django"
] |
How to run a piece of code in every view in django? | 2,296,117 | <p>I need to check user authorization in every view of one of my Django apps (I don't use Django's built in auth system) and redirect user to a "login please" page, if authorization has failed.</p>
<p>Code looks like this:</p>
<pre><code>try:
admin_from_session = request.session['admin'];
admin = Administrato... | 8 | 2010-02-19T12:09:47Z | 2,296,196 | <p>Look at the <a href="http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/decorators.py">source code for django.contrib.auth decorators</a>. They do exactly what you want, but for the built-in Django authentication system (<a href="http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.au... | 5 | 2010-02-19T12:23:56Z | [
"python",
"django"
] |
How to run a piece of code in every view in django? | 2,296,117 | <p>I need to check user authorization in every view of one of my Django apps (I don't use Django's built in auth system) and redirect user to a "login please" page, if authorization has failed.</p>
<p>Code looks like this:</p>
<pre><code>try:
admin_from_session = request.session['admin'];
admin = Administrato... | 8 | 2010-02-19T12:09:47Z | 2,296,292 | <p>Take a look at the User authentication page here <a href="http://docs.djangoproject.com/en/dev/topics/auth/" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/auth/</a></p>
<p>Read on to "The login_required decorator".</p>
<pre><code>from django.contrib.auth.decorators import login_required
@login_requir... | 0 | 2010-02-19T12:45:10Z | [
"python",
"django"
] |
How to run a piece of code in every view in django? | 2,296,117 | <p>I need to check user authorization in every view of one of my Django apps (I don't use Django's built in auth system) and redirect user to a "login please" page, if authorization has failed.</p>
<p>Code looks like this:</p>
<pre><code>try:
admin_from_session = request.session['admin'];
admin = Administrato... | 8 | 2010-02-19T12:09:47Z | 2,324,896 | <p>I found the answer I was looking for. Function decorators allow to run a peace of code at the beginning of a function.</p>
<p>You must define a decorator function</p>
<pre><code>def login_please_decorator(view_func):
"""
Redirect if admin was not logged in
"""
def _decorated(request, *args, **k... | 2 | 2010-02-24T09:31:14Z | [
"python",
"django"
] |
join lists based on common head or tail | 2,296,406 | <p>What is the fastest way to solve the following
I will to join several lists based on common head or tail</p>
<pre><code>input = ([5,6,7], [1,2,3], [3,4,5], [8, 9])
output = [1, 2, 3, 4, 5, 6, 7]
</code></pre>
| 1 | 2010-02-19T13:02:30Z | 2,296,506 | <pre><code>>>> def chain(inp):
d = {}
for i in inp:
d[i[0]] = i[:], i[-1]
l, n = d.pop(min(d))
while True:
lt, n = d.pop(n, [None, None])
if n is None:
if len(d) == len(inp) - 1:
l, n = d.pop(min(d))
continue
break
... | 1 | 2010-02-19T13:20:31Z | [
"python",
"list",
"join",
"tail",
"head"
] |
Updating a de-normalized attribute automatically with an AttributeExtension | 2,296,502 | <p>I am having some troubles with the <a href="http://www.sqlalchemy.org/docs/reference/orm/interfaces.html#sqlalchemy.orm.interfaces.AttributeExtension" rel="nofollow">AttributeExtension</a> of SQLAlchemy.</p>
<p>Actually I am storing a de-normalized sum attribute in the Partent table, because I need it quite often f... | 3 | 2010-02-19T13:20:14Z | 2,298,123 | <p>As far as I can see, you are looking for events on <code>child</code>, but change <code>child.value</code>. Something like this should do the trick:</p>
<pre><code>class ValueAttributeExtension(AttributeExtension):
...
class Child(Base):
...
value = ColumnProperty(Column(Integer, nullable=False, default=0), ... | 3 | 2010-02-19T16:59:04Z | [
"python",
"sqlalchemy"
] |
How to see possible combinations of a given number in python | 2,296,505 | <p>in Python I wanting see all the possible combination's of a number, but limiting to 0's and 1's...</p>
<p>So for example the result of some loop would be:</p>
<pre><code>0000
0001
0011
0111
1111
1000
and so on.
</code></pre>
<p>What python algorithm would best suite this?</p>
| 1 | 2010-02-19T13:20:26Z | 2,296,536 | <p>Example is in the <a href="http://docs.python.org/library/itertools.html#itertools.product" rel="nofollow"><code>itertools</code> docs</a>:</p>
<pre><code>>>> import itertools
>>> for i in itertools.product(range(2), repeat=4):
print(i)
</code></pre>
| 7 | 2010-02-19T13:25:48Z | [
"python",
"algorithm",
"binary"
] |
How to see possible combinations of a given number in python | 2,296,505 | <p>in Python I wanting see all the possible combination's of a number, but limiting to 0's and 1's...</p>
<p>So for example the result of some loop would be:</p>
<pre><code>0000
0001
0011
0111
1111
1000
and so on.
</code></pre>
<p>What python algorithm would best suite this?</p>
| 1 | 2010-02-19T13:20:26Z | 2,296,544 | <p>See the <a href="http://docs.python.org/library/itertools.html" rel="nofollow">product</a> generator.</p>
<blockquote>
<p>This module implements a number of iterator building blocks inspired
by constructs from APL, Haskell, and SML. Each has been recast in a
form suitable for Python.</p>
<p>The module st... | 0 | 2010-02-19T13:27:06Z | [
"python",
"algorithm",
"binary"
] |
How to see possible combinations of a given number in python | 2,296,505 | <p>in Python I wanting see all the possible combination's of a number, but limiting to 0's and 1's...</p>
<p>So for example the result of some loop would be:</p>
<pre><code>0000
0001
0011
0111
1111
1000
and so on.
</code></pre>
<p>What python algorithm would best suite this?</p>
| 1 | 2010-02-19T13:20:26Z | 2,296,589 | <pre><code>def print_all_combinations(max_value):
width = len('{0:0b}'.format(max_value))
format_string = '{0:0%db}' % width
for i in xrange(max_value):
print format_string.format(i)</code></pre>
| 0 | 2010-02-19T13:34:42Z | [
"python",
"algorithm",
"binary"
] |
How to see possible combinations of a given number in python | 2,296,505 | <p>in Python I wanting see all the possible combination's of a number, but limiting to 0's and 1's...</p>
<p>So for example the result of some loop would be:</p>
<pre><code>0000
0001
0011
0111
1111
1000
and so on.
</code></pre>
<p>What python algorithm would best suite this?</p>
| 1 | 2010-02-19T13:20:26Z | 2,296,644 | <pre><code>def f(n):
if n==1:
return ['0', '1']
tmp = f(n-1)
return ['0'+v for v in tmp] + ['1'+v for v in tmp]
>>> f(4)
['0000',
'0001',
'0010',
'0011',
'0100',
'0101',
'0110',
'0111',
'1000',
'1001',
'1010',
'1011',
'1100',
'1101',
'1110',
'1111']
</code></pre>
| 1 | 2010-02-19T13:42:25Z | [
"python",
"algorithm",
"binary"
] |
How to see possible combinations of a given number in python | 2,296,505 | <p>in Python I wanting see all the possible combination's of a number, but limiting to 0's and 1's...</p>
<p>So for example the result of some loop would be:</p>
<pre><code>0000
0001
0011
0111
1111
1000
and so on.
</code></pre>
<p>What python algorithm would best suite this?</p>
| 1 | 2010-02-19T13:20:26Z | 2,296,925 | <p>You're looking for k-combinations. Check <a href="http://code.activestate.com/recipes/190465/" rel="nofollow">this</a> out.</p>
<p>The function you want to look at is xcombinations:</p>
<pre><code>def xcombinations(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for cc i... | 1 | 2010-02-19T14:23:02Z | [
"python",
"algorithm",
"binary"
] |
how to encode/decode escape sequence characters in python | 2,296,525 | <p>how to encode/decode escape sequence character '\x13' in python into a character that is valid in a RSS or XML. </p>
<p>use case is, I am getting data from arbitrary sources and making a RSS feed for that data. The data source sometimes have escape sequence character which is breaking my RSS feed.</p>
<p>So how c... | 3 | 2010-02-19T13:24:17Z | 2,297,429 | <p><code>\x13</code> (ASCII 19, âDC3â) can't be escaped; it is invalid in XML 1.0, period. You <em>can</em> include one, encoded as <code>&#19;</code> or <code>&#x13;</code> in XML 1.1, but then you have to include the <code><?xml version="1.1"?></code> declaration and many tools won't like it.</p>
<... | 2 | 2010-02-19T15:33:10Z | [
"python",
"encoding",
"character-encoding"
] |
Production-ready PayPal, 2CO and Authorize.Net libraries for Python/Django? | 2,296,542 | <p>Seems that Python lacks e-commerce solutions compared to PHP and C#. Any production-ready PayPal, 2CO and Authorize.Net libraries for Python/Django?</p>
<h2>EDIT:</h2>
<ol>
<li><a href="http://github.com/johnboxall/django-paypal" rel="nofollow">http://github.com/johnboxall/django-paypal</a></li>
<li><a href="http:... | 2 | 2010-02-19T13:26:41Z | 2,297,930 | <p>You might look into <a href="http://www.satchmoproject.com/" rel="nofollow">Satchmo</a>'s <a href="http://bitbucket.org/chris1610/satchmo/src/" rel="nofollow">source code</a>. Satchmo is an open source e-commerce app for Django, and I'm pretty sure that it has support for a variety of payment gateways.</p>
<p>Thei... | 4 | 2010-02-19T16:35:26Z | [
"python",
"django",
"frameworks",
"e-commerce"
] |
how to have global variables among different modules in Python | 2,296,550 | <p>I investigated that scope of global variables in python is limited to the module. But I need the scope to be global among different modules. Is there such a thing? I played around <code>__builtin__</code> but no luck.</p>
<p>thanks in advance!</p>
| 2 | 2010-02-19T13:28:51Z | 2,296,561 | <p>You can access global variables from other modules by importing them explicitly.</p>
<p>In module <code>foo</code>:</p>
<pre><code> joe = 5
</code></pre>
<p>In module <code>bar</code>:</p>
<pre><code> from foo import joe
print joe
</code></pre>
<p>Note that this isn't recommended, though. It's much better t... | 6 | 2010-02-19T13:30:44Z | [
"python",
"scope",
"global-variables"
] |
how to have global variables among different modules in Python | 2,296,550 | <p>I investigated that scope of global variables in python is limited to the module. But I need the scope to be global among different modules. Is there such a thing? I played around <code>__builtin__</code> but no luck.</p>
<p>thanks in advance!</p>
| 2 | 2010-02-19T13:28:51Z | 2,296,580 | <p>Scopes beyond the local must be written to via a reference to the scope, or after a <code>global</code> or <code>nonlocal</code> (3.x+) directive.</p>
| 0 | 2010-02-19T13:33:19Z | [
"python",
"scope",
"global-variables"
] |
how to have global variables among different modules in Python | 2,296,550 | <p>I investigated that scope of global variables in python is limited to the module. But I need the scope to be global among different modules. Is there such a thing? I played around <code>__builtin__</code> but no luck.</p>
<p>thanks in advance!</p>
| 2 | 2010-02-19T13:28:51Z | 2,298,388 | <p>Python does not support globals shared between several modules: <em>this is a feature</em>. Code that implicitly modifies variables used far away is confusing and unmaintainable. The <em>real</em> solution is to encapsulate all state within a class and pass its instance to anything that has to modify it. This can ma... | 1 | 2010-02-19T17:36:48Z | [
"python",
"scope",
"global-variables"
] |
Python Regex Help (httplib2 cookies) | 2,296,654 | <p>Having the same problem as the poster of this question:
<a href="http://stackoverflow.com/questions/1738227/httplib2-how-to-set-more-than-one-cookie">http://stackoverflow.com/questions/1738227/httplib2-how-to-set-more-than-one-cookie</a></p>
<p>The cookie looks like this..</p>
<blockquote>
<p><code>PHPSESSID=852... | 0 | 2010-02-19T13:44:32Z | 2,296,798 | <p>Have you tried <a href="http://docs.python.org/library/cookielib.html" rel="nofollow">cookielib</a> / http.cookiejar?</p>
<hr>
<p>If you interpret the cookie as this</p>
<pre><code>PHPSESSID=8527b5532b6018aec4159d81f69765bd;
path=/;
expires=Fri, 19-Feb-2010 13:52:51 GMT, id=1578;
expires=Mon, 22-Feb-2010 13:37:51... | 1 | 2010-02-19T14:03:18Z | [
"python",
"regex",
"cookies",
"httplib2"
] |
Python Regex Help (httplib2 cookies) | 2,296,654 | <p>Having the same problem as the poster of this question:
<a href="http://stackoverflow.com/questions/1738227/httplib2-how-to-set-more-than-one-cookie">http://stackoverflow.com/questions/1738227/httplib2-how-to-set-more-than-one-cookie</a></p>
<p>The cookie looks like this..</p>
<blockquote>
<p><code>PHPSESSID=852... | 0 | 2010-02-19T13:44:32Z | 2,296,920 | <blockquote>
<p>Note how it uses commas as well as semi-colons to separate cookies, but commas are also used in the cookie itself.</p>
</blockquote>
<p>As quoted, the ambiguous commas make the string unparseable with regex or any other tool. Where is that string coming from?</p>
<p>As a <code>Set-Cookie:</code> hea... | 1 | 2010-02-19T14:22:40Z | [
"python",
"regex",
"cookies",
"httplib2"
] |
Change tab-label on gtk.noteBook | 2,296,722 | <p>I have a gtk.Notebook with some tabs on it. How can i change label on current tab when button clicked?
I make:</p>
<pre><code>self.set_tab_label(self.scrolled_window,label)
</code></pre>
<p>But label change on last tab, but not in current :(</p>
<p>Thank you.</p>
| 0 | 2010-02-19T13:53:28Z | 2,296,813 | <p>I find solution! it can someone useful:</p>
<pre><code>self.set_tab_label_text(self.get_nth_page(self.get_current_page()),"LABEL_TEXT")
</code></pre>
| 0 | 2010-02-19T14:04:53Z | [
"python",
"gtk",
"pygtk"
] |
python basic while loop | 2,296,803 | <p>I have another newbie Python question. I have the following piece of code that I have a feeling is not written as pythonic as it should be:</p>
<pre><code> rowindex = 0
while params.getfirst('myfield'+rowindex):
myid = params.getfirst('myfield'+rowindex)
# do stuff with myid
rowindex... | 1 | 2010-02-19T14:03:29Z | 2,296,827 | <p>you can do this. I consider this more "flexible" as I can give any conditions i want, as many as i want, inside the while loop. But then again, some will say its personal taste. </p>
<pre><code>rowindex = 0
while 1:
myid = params.getfirst('myfield'+rowindex)
if not myid: #or check for length of 0 , etc, th... | 1 | 2010-02-19T14:07:39Z | [
"python",
"while-loop"
] |
python basic while loop | 2,296,803 | <p>I have another newbie Python question. I have the following piece of code that I have a feeling is not written as pythonic as it should be:</p>
<pre><code> rowindex = 0
while params.getfirst('myfield'+rowindex):
myid = params.getfirst('myfield'+rowindex)
# do stuff with myid
rowindex... | 1 | 2010-02-19T14:03:29Z | 2,296,858 | <p>The "unbounded" nature of a simple counter has some appeal. However, it's also an opportunity for someone to attempt a Denial of Service attack by spoofing a form with billions of fields. Simply counting through the fields could stall your web server as you attempt to process those billions of fields. </p>
<p>Si... | 5 | 2010-02-19T14:11:43Z | [
"python",
"while-loop"
] |
python basic while loop | 2,296,803 | <p>I have another newbie Python question. I have the following piece of code that I have a feeling is not written as pythonic as it should be:</p>
<pre><code> rowindex = 0
while params.getfirst('myfield'+rowindex):
myid = params.getfirst('myfield'+rowindex)
# do stuff with myid
rowindex... | 1 | 2010-02-19T14:03:29Z | 2,296,949 | <p>I think I'd make a little generator to fully encapsulate the looping logic:</p>
<pre><code>import itertools
def genit(params):
for rowindex in itertools.count():
theid = params.getfirst('myfield%s' % rowindex)
if not theid: break
yield theid
</code></pre>
<p>so that application ("business") logic ca... | 2 | 2010-02-19T14:26:13Z | [
"python",
"while-loop"
] |
python basic while loop | 2,296,803 | <p>I have another newbie Python question. I have the following piece of code that I have a feeling is not written as pythonic as it should be:</p>
<pre><code> rowindex = 0
while params.getfirst('myfield'+rowindex):
myid = params.getfirst('myfield'+rowindex)
# do stuff with myid
rowindex... | 1 | 2010-02-19T14:03:29Z | 2,297,966 | <p>Try Doing this</p>
<pre><code>While 1:
codehere
</code></pre>
| -1 | 2010-02-19T16:39:30Z | [
"python",
"while-loop"
] |
Assignment into Python 3.x Buffers with itemsize > 1 | 2,297,026 | <p>I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so:</p>
<pre><code>int Image_get_buffer(PyObject* self, Py_buffer* view, int flags)
{
int img_len;
void* img_bytes;
/... | 2 | 2010-02-19T14:36:50Z | 2,297,655 | <p>I found this in the python code (in memoryobject.c in Objects) in the function memory_ass_sub:</p>
<pre><code>/* XXX should we allow assignment of different item sizes
as long as the byte length is the same?
(e.g. assign 2 shorts to a 4-byte slice) */
if (srcview.itemsize != view->itemsize) {
PyErr_For... | 1 | 2010-02-19T16:02:02Z | [
"python",
"python-3.x",
"python-c-api",
"pybuffer",
"pep3118"
] |
Django(postgresql) + lighttpd. Any issues with threading and python's postgresql driver? | 2,297,030 | <p>I'd like to deploy my Django app (which uses postgresql as database) on lighttpd using FastCGI.<br>
For postgresql i see that Django has 2 backends available 'postgresql_psycopg2' and 'postgresql'.<br>
My question is that lighttpd being a threaded server are there any issues with any of this backends? Are they threa... | 1 | 2010-02-19T14:37:33Z | 2,298,298 | <p><a href="http://initd.org/psycopg/docs/usage.html#thread-safety" rel="nofollow">http://initd.org/psycopg/docs/usage.html#thread-safety</a></p>
| 2 | 2010-02-19T17:24:56Z | [
"python",
"django",
"postgresql",
"thread-safety",
"lighttpd"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 2,297,577 | <p>Maybe you could use the <a href="http://www.doughellmann.com/PyMOTW/cgitb/" rel="nofollow">cgitb</a> module (short official doc <a href="http://docs.python.org/library/cgitb.html" rel="nofollow">here</a>) as a starting point (it creates nice HTML tracebacks). It should be relatively simple to make the modifications ... | 1 | 2010-02-19T15:52:34Z | [
"python",
"debugging",
"bash"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 2,298,264 | <p>As a starting point for colorizing (and otherwise formatting) text, you'll probably want to look at the <a href="http://docs.python.org/library/curses.html" rel="nofollow">curses library</a>. Also see <a href="http://www.amk.ca/python/howto/curses/" rel="nofollow">this how-to</a>, which looks useful.</p>
<p>As for... | 0 | 2010-02-19T17:19:59Z | [
"python",
"debugging",
"bash"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 2,298,271 | <p>Take a look at <a href="http://supercat.nosredna.net/index.html">Supercat</a> (spc). It does both ANSI and HTML highlighting and can be configured for your particular output. It comes with some configuration files for source code files in C and Python, for example and log files, Changelogs, diffs and others.</p>
<p... | 6 | 2010-02-19T17:20:31Z | [
"python",
"debugging",
"bash"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 2,298,425 | <p>Load the text into vim:</p>
<pre><code>nosetests | vim -
</code></pre>
<p>Set vim to highlight all lines that match a search</p>
<pre><code>:set hlsearch
</code></pre>
<p>Search for lines with "your" path</p>
<pre><code>/.*/path/to/my/code.*
</code></pre>
<p>Voila - all the lines with your path will be highlig... | -1 | 2010-02-19T17:43:35Z | [
"python",
"debugging",
"bash"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 2,329,010 | <p>Actually, there is a great Python syntax highlighting library called <a href="http://pygments.org/">Pygments</a>, which is also able to highlight tracebacks. Here is an <a href="http://paste.pocoo.org/show/182168/">example</a> (the pastebin here uses Pygments).</p>
<p>So, all you have to do is:</p>
<pre><code>$ e... | 15 | 2010-02-24T19:54:15Z | [
"python",
"debugging",
"bash"
] |
Highlighting python stack traces | 2,297,044 | <p>I'm working on quite complex project and time after time I have to narrow down problems looking at stack traces. They happen to be very long and involve âmyâ code, standard library code and 3rd party libraries code at same time. Most of time the real problem is in âmyâ code and locating it instantly in a sta... | 18 | 2010-02-19T14:40:23Z | 9,720,112 | <p>Also you can open traceback file with vim, using <code>:cfile</code> command. Then you can open highlighted ant interactive list of files in traceback using <code>:copen</code> command and jump between these files using set of related vim commands.</p>
| 0 | 2012-03-15T12:53:59Z | [
"python",
"debugging",
"bash"
] |
tkinter: Specifying arguments for a function that's called when you press a button | 2,297,336 | <pre><code>button1 = tkinter.Button(frame, text="Say hi", command=print)
button2 = tkinter.Button(frame, text="foo", command=print)
button3 = tkinter.Button(frame, text="bar", command=print)
</code></pre>
<p>You've probably spotted the hole in my program: <code>print</code> can't specify arguments. This renders the wh... | 2 | 2010-02-19T15:18:06Z | 2,297,423 | <p>If you have at least python 2.6 (which I'm guessing you are since you use print in a function position) you can use <code>functools.partial</code>. It takes a function and any arguments to supply and returns a callable that will call the underlying function and add on any arguments passed to the final call. For exam... | 5 | 2010-02-19T15:32:36Z | [
"python",
"arguments",
"tkinter"
] |
tkinter: Specifying arguments for a function that's called when you press a button | 2,297,336 | <pre><code>button1 = tkinter.Button(frame, text="Say hi", command=print)
button2 = tkinter.Button(frame, text="foo", command=print)
button3 = tkinter.Button(frame, text="bar", command=print)
</code></pre>
<p>You've probably spotted the hole in my program: <code>print</code> can't specify arguments. This renders the wh... | 2 | 2010-02-19T15:18:06Z | 2,297,521 | <p>A simple solution is to use lambda, which lets you create anonymous functions.</p>
<pre><code>button1 = tkinter.Button(frame, text="Say hi", command=lambda: print("Say hi")
button2 = tkinter.Button(frame, text="foo", command=lambda: print("foo"))
button3 = tkinter.Button(frame, text="bar", command=lambda: print("ba... | 2 | 2010-02-19T15:45:19Z | [
"python",
"arguments",
"tkinter"
] |
Do simple things with a Google Wave robot | 2,297,420 | <p>I wanted to add 3 features to the robot from the tutorial here:
<a href="http://code.google.com/apis/wave/extensions/robots/python-tutorial.html" rel="nofollow">http://code.google.com/apis/wave/extensions/robots/python-tutorial.html</a></p>
<p>Before adding all these features, my robot is working as intended.
Now t... | 13 | 2010-02-19T15:32:19Z | 6,834,318 | <p><code>events.BLIP_SUMBITTED</code> should be <code>events.BLIP_SUBMITTED</code></p>
| 1 | 2011-07-26T17:53:57Z | [
"python",
"google-wave"
] |
Searching a website | 2,297,787 | <pre><code>import urllib
import re
import os
search = (raw_input('[!]Search: '))
site = "http://www.exploit-db.com/list.php?description="+search+"&author=&platform=&type=&port=&osvdb=&cve="
print site
source = urllib.urlopen(site).read()
founds = re.findall("href='/exploits/\d+",source)
print... | 0 | 2010-02-19T16:18:48Z | 2,297,847 | <p>Easy to check by just visiting the site and looking at the URLs as you manually page: just put right after the <code>?</code> in the URL <code>page=1&</code> to look at the second page of results, or <code>page=2&</code> to look at the third page, and so forth.</p>
<p>How is this a Python question? It's a ... | 0 | 2010-02-19T16:27:09Z | [
"python",
"urllib"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.