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
Wrapping a C library in Python: C, Cython or ctypes?
1,942,298
<p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning exte...
185
2009-12-21T20:05:51Z
5,686,873
<p>Warning: a Cython core developer's opinion ahead.</p> <p>I almost always recommend Cython over ctypes. The reason is that it has a much smoother upgrade path. If you use ctypes, many things will be simple at first, and it's certainly cool to write your FFI code in plain Python, without compilation, build dependenci...
89
2011-04-16T13:41:45Z
[ "python", "c", "ctypes", "cython" ]
Wrapping a C library in Python: C, Cython or ctypes?
1,942,298
<p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning exte...
185
2009-12-21T20:05:51Z
7,266,364
<p>There's also one possibility to use <a href="http://live.gnome.org/GObjectIntrospection" rel="nofollow">GObject Introspection</a> for libraries that are using <a href="http://developer.gnome.org/glib/" rel="nofollow">GLib</a>.</p>
3
2011-09-01T05:19:39Z
[ "python", "c", "ctypes", "cython" ]
Wrapping a C library in Python: C, Cython or ctypes?
1,942,298
<p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning exte...
185
2009-12-21T20:05:51Z
14,942,060
<p>For calling a C library from a Python application there is also <a href="http://cffi.readthedocs.org/en/latest/"><strong>cffi</strong></a> which is a new alternative for <em>ctypes</em>. It brings a fresh look for FFI: </p> <ul> <li>it handles the problem in a fascinating, clean way (as opposed to <em>ctypes</em>)<...
26
2013-02-18T17:33:30Z
[ "python", "c", "ctypes", "cython" ]
Wrapping a C library in Python: C, Cython or ctypes?
1,942,298
<p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning exte...
185
2009-12-21T20:05:51Z
17,067,839
<p>There is one issue which made me use ctypes and not cython and which is not mentioned in other answers. </p> <p>Using ctypes the result does not depend on compiler you are using at all. You may write a library using more or less any language which may be compiled to native shared library. It does not matter much, ...
4
2013-06-12T14:15:18Z
[ "python", "c", "ctypes", "cython" ]
Wrapping a C library in Python: C, Cython or ctypes?
1,942,298
<p>I want to call a C library from a Python application. I don't want to wrap the whole API, only the functions and datatypes that are relevant to my case. As I see it, I have three choices:</p> <ol> <li>Create an actual extension module in C. Probably overkill, and I'd also like to avoid the overhead of learning exte...
185
2009-12-21T20:05:51Z
21,967,623
<p>If you are targeting Windows and choose to wrap some proprietary C++ libraries, then you may soon discover that different versions of <code>msvcrt***.dll</code> (Visual C++ Runtime) are slightly incompatible. </p> <p>This means that you may not be able to use <a href="http://www.cython.org/" rel="nofollow"><code>Cy...
2
2014-02-23T11:31:10Z
[ "python", "c", "ctypes", "cython" ]
anyone have example python code that sends mail using sendmail and subprocess?
1,942,305
<p>I'm kind of confused about how <code>subprocess.Popen</code> works. If anyone has example code that sends email using the subprocess module and sendmail that'd be great.</p>
0
2009-12-21T20:06:56Z
1,942,420
<p>One of the first gotchas I encountered with <CODE>subprocess</CODE> was the fact that it doesn't take full shell string commands by default. </p> <p>If you want to do the equivalent of a shell command like:</p> <pre><code>os.system("echo hello world") </code></pre> <p>you need to use the <CODE>shell=True</CODE> ...
0
2009-12-21T20:27:50Z
[ "python", "subprocess", "sendmail" ]
anyone have example python code that sends mail using sendmail and subprocess?
1,942,305
<p>I'm kind of confused about how <code>subprocess.Popen</code> works. If anyone has example code that sends email using the subprocess module and sendmail that'd be great.</p>
0
2009-12-21T20:06:56Z
1,942,620
<p>This doesn't directly answer the question, but given your response to a comment by "DNS", it might solve your problem.</p> <p>When sending SMTP mail, you need to understand that the "from" and "to" addresses that you pass to the smtplib.sendmail() routine as arguments are <em>not</em> the same thing as what you see...
2
2009-12-21T21:03:09Z
[ "python", "subprocess", "sendmail" ]
Why does ctypes WriteProcessMemory() fail?
1,942,322
<p>I have been trying to get this function working for some time now with no luck.</p> <pre><code>def write_memory(self, address, data): PROCESS_ALL_ACCESS = 0x001F0FFF count = c_ulong(0) length = len(data) c_data = c_char_p(data[count.value:]) null = c_int(0) windll.kernel32.SetLastError(10000...
0
2009-12-21T20:09:41Z
1,942,415
<p>There's nothing obviously wrong with the call. Most likely there just aren't any pages at 0x00050000 or the pages there aren't writable. </p> <p>Why don't you try doing a <code>VirtualQuery</code> on the bytes you are trying to write to and see if it's actually writable? Most random addresses aren't.</p>
0
2009-12-21T20:26:49Z
[ "python", "ctypes" ]
Add a member variable / method to a Python generator?
1,942,328
<p>Can I add a member variable / method to a Python generator?</p> <p>I want something along the following lines, so that I can "peek" at member variable j:</p> <pre><code>def foo(): for i in range(10): self.j = 10 - i yield i gen = foo() for k in gen: print gen.j print k </code></pre> <...
5
2009-12-21T20:10:17Z
1,942,382
<p>I think it's ugly, but it should do what you want. I'd rather return i AND j every time, though :-)</p> <pre><code>class Foo(object): def foo(self): for i in range(10): self.j = 10 - i yield i genKlass = Foo() gen = genKlass.foo() for k in gen: print genKlass.j print k <...
2
2009-12-21T20:20:51Z
[ "python", "generator", "local" ]
Add a member variable / method to a Python generator?
1,942,328
<p>Can I add a member variable / method to a Python generator?</p> <p>I want something along the following lines, so that I can "peek" at member variable j:</p> <pre><code>def foo(): for i in range(10): self.j = 10 - i yield i gen = foo() for k in gen: print gen.j print k </code></pre> <...
5
2009-12-21T20:10:17Z
1,942,387
<p>You could create an object and manipulate the <a href="http://docs.python.org/library/stdtypes.html#container.%5F%5Fiter%5F%5F">__iter__</a> interface:</p> <pre><code>class Foo(object): def __init__(self): self.j = None def __iter__(self): for i in range(10): self.j = 10 - i ...
8
2009-12-21T20:21:10Z
[ "python", "generator", "local" ]
DOM to use with SpiderMonkey?
1,942,344
<p>I'm trying to use the GoogleMaps JavaScript library from inside of SpiderMonkey using the python wrapper, but I can't because of the lack of a DOM.</p> <p>Is there some way I can integrate a DOM into this so that I can get this to work?</p>
3
2009-12-21T20:13:24Z
1,942,541
<p><a href="http://github.com/thatcher/env-js" rel="nofollow">env.js</a> implements the DOM. I know it at least works in SpiderMonkey and Rhino but SpiderMonkey may have issues with things like XMLHttpRequest.</p>
4
2009-12-21T20:49:14Z
[ "javascript", "python", "dom", "spidermonkey" ]
How properly bundle&install python to windows users
1,942,399
<p>I need to redistribute Python 2.6 for my users.</p> <p>Currently, I execute the silent instalation for the msi installer from <a href="http://www.python.org/download/" rel="nofollow">http://www.python.org/download/</a>, but I have some problems, like if other version of python is installed this not get the default....
3
2009-12-21T20:23:03Z
1,942,539
<p>I haven't used it, beyond some initial research, but ActiveState's <a href="http://www.activestate.com/activepython/" rel="nofollow">ActivePython</a> generally does what you want, if you're looking for a commercial pre-built solution.</p> <p>I have worked on software that included its own bundled Python, and there ...
2
2009-12-21T20:49:10Z
[ "python", "windows", "deployment", "installer" ]
How properly bundle&install python to windows users
1,942,399
<p>I need to redistribute Python 2.6 for my users.</p> <p>Currently, I execute the silent instalation for the msi installer from <a href="http://www.python.org/download/" rel="nofollow">http://www.python.org/download/</a>, but I have some problems, like if other version of python is installed this not get the default....
3
2009-12-21T20:23:03Z
1,942,540
<p>Try looking at this video (<a href="http://blip.tv/file/2080263" rel="nofollow">How I Distribute Python apps on Windows</a>).</p>
0
2009-12-21T20:49:11Z
[ "python", "windows", "deployment", "installer" ]
mod_wsgi on Snow Leopard python version mismatch
1,942,462
<p>I'm trying to run mod_wsgi 3.1 under Apache 2.2.14 using a non-default python installation on Mac OS X 10.6.</p> <p>After downloading the mod_wsgi source I run:</p> <pre><code>sudo apachectl -k stop </code></pre> <p>then</p> <pre><code>./configure --with-python=/usr/local/Cellar/python/2.6.4/bin/python make sudo...
1
2009-12-21T20:35:55Z
1,942,857
<p>Because for some reason some Python framework installs from source code, usually MacPorts, have something wrong with the information embedded in the Python framework and the run time look up path of the executable isn't set correctly. As a result it ends up using the Python framework from /System/Library instead.</p...
0
2009-12-21T21:51:46Z
[ "python", "apache", "makefile", "mod-wsgi" ]
mod_wsgi on Snow Leopard python version mismatch
1,942,462
<p>I'm trying to run mod_wsgi 3.1 under Apache 2.2.14 using a non-default python installation on Mac OS X 10.6.</p> <p>After downloading the mod_wsgi source I run:</p> <pre><code>sudo apachectl -k stop </code></pre> <p>then</p> <pre><code>./configure --with-python=/usr/local/Cellar/python/2.6.4/bin/python make sudo...
1
2009-12-21T20:35:55Z
1,957,496
<p>Graham helped me solve this over on the mod_wsgi mailing list.</p> <p><a href="http://groups.google.com/group/modwsgi/browse%5Fthread/thread/4046eaf290a49b1e/ae14888450de39f5#ae14888450de39f5" rel="nofollow">http://groups.google.com/group/modwsgi/browse%5Fthread/thread/4046eaf290a49b1e/ae14888450de39f5#ae14888450de...
0
2009-12-24T09:05:37Z
[ "python", "apache", "makefile", "mod-wsgi" ]
mod_wsgi on Snow Leopard python version mismatch
1,942,462
<p>I'm trying to run mod_wsgi 3.1 under Apache 2.2.14 using a non-default python installation on Mac OS X 10.6.</p> <p>After downloading the mod_wsgi source I run:</p> <pre><code>sudo apachectl -k stop </code></pre> <p>then</p> <pre><code>./configure --with-python=/usr/local/Cellar/python/2.6.4/bin/python make sudo...
1
2009-12-21T20:35:55Z
7,749,078
<p>This post is old, but still turns up in searches about mac + homebrew + python, so I thought I'd add some useful information. I was having the problem as the OP, just with a different module (uwsgi). I learned that you don't have to abandon homebrew. Homebrew can, in fact, install python as a framework; you just hav...
2
2011-10-13T03:17:24Z
[ "python", "apache", "makefile", "mod-wsgi" ]
Simple code but can't find the error (PyS60 but not specific)
1,942,559
<p>I'm a Python beginner and now it's freakin me out:</p> <pre><code>L = [] file = urllib.urlopen("http://someurl.com/someText.txt") line = file.readline() while line != "" : L.append(line) line = file.readline() appuifw.selection_list(choices=L) </code></pre> <p>and I get this error:</p> <pre><code> line = file...
1
2009-12-21T20:52:34Z
1,942,582
<p>Seems to work fine in my Python interpreter (2.6.1).</p> <p>I take it you did do <code>import urllib</code> first? (Not doing that would cause a <code>NameError</code>, not a <code>SyntaxError</code>.)</p> <p><strong>EDIT:</strong> a quick Google found this: <a href="http://discussion.forum.nokia.com/forum/showthr...
0
2009-12-21T20:57:36Z
[ "python", "pys60" ]
Simple code but can't find the error (PyS60 but not specific)
1,942,559
<p>I'm a Python beginner and now it's freakin me out:</p> <pre><code>L = [] file = urllib.urlopen("http://someurl.com/someText.txt") line = file.readline() while line != "" : L.append(line) line = file.readline() appuifw.selection_list(choices=L) </code></pre> <p>and I get this error:</p> <pre><code> line = file...
1
2009-12-21T20:52:34Z
1,942,597
<p>I actually don't see a problem, unless you're mixing tabs and spaces in that indentation, in which case the error should complaining about indentation levels. But I thought I'd point out that there's a much cleaner way to read all the lines in a file-like object:</p> <pre><code>f = urllib.urlopen("http://someurl.c...
0
2009-12-21T20:59:30Z
[ "python", "pys60" ]
Simple code but can't find the error (PyS60 but not specific)
1,942,559
<p>I'm a Python beginner and now it's freakin me out:</p> <pre><code>L = [] file = urllib.urlopen("http://someurl.com/someText.txt") line = file.readline() while line != "" : L.append(line) line = file.readline() appuifw.selection_list(choices=L) </code></pre> <p>and I get this error:</p> <pre><code> line = file...
1
2009-12-21T20:52:34Z
1,942,602
<p>You are overwriting the built-in function <code>file</code> with you variable of the same name. Maybe that causes the Py60 some grief?</p>
0
2009-12-21T21:00:35Z
[ "python", "pys60" ]
Simple code but can't find the error (PyS60 but not specific)
1,942,559
<p>I'm a Python beginner and now it's freakin me out:</p> <pre><code>L = [] file = urllib.urlopen("http://someurl.com/someText.txt") line = file.readline() while line != "" : L.append(line) line = file.readline() appuifw.selection_list(choices=L) </code></pre> <p>and I get this error:</p> <pre><code> line = file...
1
2009-12-21T20:52:34Z
1,942,668
<p>Rewriting to</p> <pre><code>file = urllib.urlopen("http://blabla.com/bla.txt") lines1 = file.readlines() for li in lines1: L.append(li) index = appuifw.selection_list(choices=L) </code></pre> <p>it seems to work now.<br> (Still problems left but I think it's the URL)</p>
2
2009-12-21T21:14:39Z
[ "python", "pys60" ]
Simple code but can't find the error (PyS60 but not specific)
1,942,559
<p>I'm a Python beginner and now it's freakin me out:</p> <pre><code>L = [] file = urllib.urlopen("http://someurl.com/someText.txt") line = file.readline() while line != "" : L.append(line) line = file.readline() appuifw.selection_list(choices=L) </code></pre> <p>and I get this error:</p> <pre><code> line = file...
1
2009-12-21T20:52:34Z
1,942,712
<p>Show the invisibles. I bet there is an illegal character (null is a favorite) hiding in one of those lines and it's not showing up on your screen. Or maybe the file has the wrong type of line ends.</p> <p>My usual tricks here:</p> <p>1) You might have typed it in right in StackOverflow; try copying this code bac...
1
2009-12-21T21:23:54Z
[ "python", "pys60" ]
PyCURL: TLS Handshake Error
1,942,719
<p>I'm using PyCURL to test a redirection service we're offering- a user hits <a href="http://xyz.com/asdf/" rel="nofollow">http://xyz.com/asdf/</a> and gets redirected to <a href="https://a.com" rel="nofollow">https://a.com</a>, <a href="https://b.com" rel="nofollow">https://b.com</a> and <a href="https://c.com" rel="...
1
2009-12-21T21:25:04Z
1,949,286
<p>pycurl uses libcurl and libcurl can be built to use one of several TLS libraries. GnuTLS and OpenSSL being two of them. You can thus rebuild libcurl to use whichever of those that you prefer.</p>
1
2009-12-22T21:26:42Z
[ "python", "curl", "pycurl" ]
PyCURL: TLS Handshake Error
1,942,719
<p>I'm using PyCURL to test a redirection service we're offering- a user hits <a href="http://xyz.com/asdf/" rel="nofollow">http://xyz.com/asdf/</a> and gets redirected to <a href="https://a.com" rel="nofollow">https://a.com</a>, <a href="https://b.com" rel="nofollow">https://b.com</a> and <a href="https://c.com" rel="...
1
2009-12-21T21:25:04Z
27,213,192
<p>I just resolved the same issue. I'm not 100% sure what was wrong. But I installed pycurl via <code>apt-get install python-pycurl</code> which i think by default uses gnutls, but it seemed to me it should be openssl on my system.</p> <p>I resolved it by doing the following:</p> <p><code>export PYCURL_SSL_LIBRARY=[o...
2
2014-11-30T12:23:07Z
[ "python", "curl", "pycurl" ]
A little misunderstanding timers in python
1,943,182
<p>can somebody tell me how to use this class timers from python in my code more than one time.</p> <pre><code>import MOD class timer: def __init__(self, seconds): self.start(seconds) def start(self, seconds): self.startTime = MOD.secCounter() self.expirationTime = self.startTime + ...
1
2009-12-21T23:00:29Z
1,943,195
<p>Close - the argument to timers.timer is the number of seconds that the timer should time for at first. But every time you call timers.timer(), you'll get a new timer instance.</p> <p>So your code could look more like:</p> <pre><code> timerB = timers.timer(1800) while 1: if timerB.isexpired(): ...
2
2009-12-21T23:02:16Z
[ "python", "timer" ]
Open 3rd party Application with Python
1,943,367
<p>I am on Windows XP and have a 3rd party application that I want to be able to open using a Python script. How do I go about doing it?</p> <pre><code>from win32com.client import Dispatch mySuite = Dispatch("TestSuite.Application") </code></pre> <p>throws an error </p> <pre><code>File "C:\Python26\Lib\site-packages...
0
2009-12-21T23:44:41Z
1,943,917
<p>Looks like the TestSuite.Application COM class has not been registered -- try opening it in, say, VBScript, to verify, and it should also fail. In which case maybe you can fix it with <a href="http://support.microsoft.com/kb/249873" rel="nofollow">regsvr32.exe</a> or similar tools.</p>
1
2009-12-22T02:58:17Z
[ "python" ]
Python regular expression inconsistency
1,943,400
<p>I am getting different results based on whether I precompile a regular expression:</p> <pre><code>&gt;&gt;&gt; re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean') ' Bean' &gt;&gt;&gt; re.sub('mr', '', 'Mr Bean', re.IGNORECASE) 'Mr Bean' </code></pre> <p>The <a href="http://docs.python.org/library/re.html" rel="nofo...
2
2009-12-21T23:54:19Z
1,943,413
<p>the module level sub() call doesn't accept modifiers at the end. thats the "count" argument - the maximum number of pattern occurrences to be replaced.</p>
5
2009-12-21T23:58:58Z
[ "python", "regex", "documentation" ]
Python regular expression inconsistency
1,943,400
<p>I am getting different results based on whether I precompile a regular expression:</p> <pre><code>&gt;&gt;&gt; re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean') ' Bean' &gt;&gt;&gt; re.sub('mr', '', 'Mr Bean', re.IGNORECASE) 'Mr Bean' </code></pre> <p>The <a href="http://docs.python.org/library/re.html" rel="nofo...
2
2009-12-21T23:54:19Z
1,943,414
<p><code>re.sub()</code> can't accept the <code>re.IGNORECASE</code>, it appears.</p> <p>The documentation states:</p> <blockquote> <p><code>sub(pattern, repl, string, count=0)</code> </p> <pre>Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replace...
12
2009-12-21T23:59:01Z
[ "python", "regex", "documentation" ]
Python regular expression inconsistency
1,943,400
<p>I am getting different results based on whether I precompile a regular expression:</p> <pre><code>&gt;&gt;&gt; re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean') ' Bean' &gt;&gt;&gt; re.sub('mr', '', 'Mr Bean', re.IGNORECASE) 'Mr Bean' </code></pre> <p>The <a href="http://docs.python.org/library/re.html" rel="nofo...
2
2009-12-21T23:54:19Z
1,943,424
<pre><code>&gt;&gt;&gt; help(re.sub) 1 Help on function sub in module re: 2 3 sub(pattern, repl, string, count=0) 4 Return the string obtained by replacing the leftmost 5 non-overlapping occurrences of the pattern in string by the 6 replacement repl. repl can be either a string or a callable; ...
4
2009-12-22T00:02:02Z
[ "python", "regex", "documentation" ]
Python regular expression inconsistency
1,943,400
<p>I am getting different results based on whether I precompile a regular expression:</p> <pre><code>&gt;&gt;&gt; re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean') ' Bean' &gt;&gt;&gt; re.sub('mr', '', 'Mr Bean', re.IGNORECASE) 'Mr Bean' </code></pre> <p>The <a href="http://docs.python.org/library/re.html" rel="nofo...
2
2009-12-21T23:54:19Z
1,943,428
<p>From the Python 2.6.4 documentation:</p> <pre><code>re.sub(pattern, repl, string[, count]) </code></pre> <p>re.sub() doesn't take a flag to set the regex mode. If you want re.IGNORECASE, you must use re.compile().sub()</p>
2
2009-12-22T00:02:54Z
[ "python", "regex", "documentation" ]
How to redirect to a newly created object in Django without a Generic View's post_save_redirect argument
1,943,510
<p>I'm trying to redirect a user to a newly created object's <code>object.get_absolute_url()</code> after saving a form. I'm not using a generic view, so I can't use the <code>post_save_redirect</code> argument. The relevant portion of the <code>view</code> is like so:</p> <pre><code>if form.is_valid(): form.save(...
0
2009-12-22T00:30:56Z
1,943,532
<p>A <code>ModelForm</code>'s save method returns the newly created model instance. In your case, this means you should be able to work with the following:</p> <pre><code>if form.is_valid(): story = form.save() return HttpResponseRedirect(reverse('story_detail', args=(story.user, story.id))) </code></pre>
4
2009-12-22T00:36:32Z
[ "python", "django" ]
python fetching multiple pages using post and cookies
1,943,546
<p>Got a test site that I'm fetching. The site uses the POST method, as well as cookies. (Not sure that the cookies are critical, but i'm inclined to think they are..)</p> <p>The app presents a page, with a "next button" to generate the subsequent pages. I've used LiveHttpHeaders/Firefof to determine what the post dat...
0
2009-12-22T00:40:10Z
1,943,596
<p>I'm really not sure what's going wrong. It may not be helpful, but a different way to do it:</p> <p>Open a table-row for each 1st page submit then keep only the rowId in the cookie and add to the row with subsequent accesses.</p> <p>tf.</p>
0
2009-12-22T00:55:20Z
[ "python", "cookies", "post", "urllib2" ]
Grab Focus for Frame after Shortcut Pressed in wxPython on GNOME
1,943,682
<p>I'm building an app that uses global shortcut keys (using <a href="http://kaizer.se/wiki/python-keybinder/" rel="nofollow">python-keybinder</a>), but there's a problem. The frame pops up and raises properly but doesn't have focus. I have to click on frame. </p> <p>After I press my keyboard shortcut my frame appears...
0
2009-12-22T01:27:04Z
1,943,765
<p>How are you showing the frmae initially, with frame.Show()? I'm not sure if you're saying the frame itself doesn't have focus (but/or a child of the frame does), or your application doesn't have focus?</p> <p>Are you calling SetFocus in the Frame that initialises all your widgets? It could be an issue of the focus ...
0
2009-12-22T01:53:07Z
[ "python", "wxpython", "gnome" ]
redirecting standard output to the calling process
1,943,712
<p>hello everyone<br> Im no bash expert so bear with me </p> <p>I have a python script thats starting other processes, which then emit log messages on stdout.<br> what bash command would I use to redirect stdout of those child processes back to the stdout of the parent process (the python script thats starting the pr...
1
2009-12-22T01:36:19Z
1,943,728
<p>If you just want to capture the output of the child processes in your python script, the best way to do it would be to use the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow"><code>subprocess</code></a> module:</p> <pre><code>import subprocess p = subprocess.Popen(['/bin/ls', '-l'], stdout=...
3
2009-12-22T01:41:51Z
[ "python", "bash" ]
redirecting standard output to the calling process
1,943,712
<p>hello everyone<br> Im no bash expert so bear with me </p> <p>I have a python script thats starting other processes, which then emit log messages on stdout.<br> what bash command would I use to redirect stdout of those child processes back to the stdout of the parent process (the python script thats starting the pr...
1
2009-12-22T01:36:19Z
1,943,751
<p>I'm assuming you're using <code>os.system("command")</code> to execute your scripts. This method only executes the command and gives you the program's return value and NOT it's stdout. What you need is the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a> module. For example:</p>...
1
2009-12-22T01:47:18Z
[ "python", "bash" ]
How to call main() of a python script with arguments from mod_python.publisher?
1,943,714
<p>I use a mod_python publisher function which calls the main() function of another python script with a custom built argv list. When I execute the publisher script from shell command line it works. But when I tried it through apache2 with mod_python, I get the error (shown below) that main takes no arguments. </p> <h...
0
2009-12-22T01:36:39Z
28,096,926
<p>The first logging statement says:</p> <pre><code>aba.main([ "aba.py","-i", "-b"]) </code></pre> <p>And you say main is defined as:</p> <pre><code>def main(argv=None): </code></pre> <p>Therefore <code>aba</code> is passed in as the first argument into main() which takes up the <code>argv</code> argument and then ...
0
2015-01-22T19:17:22Z
[ "python", "mod-python" ]
Python logging before you run logging.basicConfig?
1,943,747
<p>It appears that if you invoke logging.info() <strong>BEFORE</strong> you run logging.basicConfig, the logging.basicConfig call doesn't have any effect. In fact, no logging occurs.</p> <p>Where is this behavior documented? I don't really understand.</p>
24
2009-12-22T01:46:53Z
1,943,809
<p>Yes.</p> <p>You've asked to log something. Logging must, therefore, fabricate a default configuration. Once logging is configured... well... it's configured.</p> <blockquote> <p>"With the logger object configured, the following methods create log messages:"</p> </blockquote> <p>Further, you can read about...
9
2009-12-22T02:05:27Z
[ "python", "logging" ]
Python logging before you run logging.basicConfig?
1,943,747
<p>It appears that if you invoke logging.info() <strong>BEFORE</strong> you run logging.basicConfig, the logging.basicConfig call doesn't have any effect. In fact, no logging occurs.</p> <p>Where is this behavior documented? I don't really understand.</p>
24
2009-12-22T01:46:53Z
2,588,054
<p>You can remove the default handlers and reconfigure logging like this:</p> <pre><code># if someone tried to log something before basicConfig is called, Python creates a default handler that # goes to the console and will ignore further basicConfig calls. Remove the handler if there is one. root = logging.getLogger(...
26
2010-04-06T20:28:07Z
[ "python", "logging" ]
Python logging before you run logging.basicConfig?
1,943,747
<p>It appears that if you invoke logging.info() <strong>BEFORE</strong> you run logging.basicConfig, the logging.basicConfig call doesn't have any effect. In fact, no logging occurs.</p> <p>Where is this behavior documented? I don't really understand.</p>
24
2009-12-22T01:46:53Z
12,549,515
<p>Here's the one piece of the puzzle that the above answers didn't mention... and then it will all make sense: the "root" logger -- which is used if you call, say, logging.info() before logging.basicConfig(level=logging.DEBUG) -- <em>has a default logging level of WARNING</em>.</p> <p>That's why logging.info() and l...
0
2012-09-23T03:33:17Z
[ "python", "logging" ]
Python logging before you run logging.basicConfig?
1,943,747
<p>It appears that if you invoke logging.info() <strong>BEFORE</strong> you run logging.basicConfig, the logging.basicConfig call doesn't have any effect. In fact, no logging occurs.</p> <p>Where is this behavior documented? I don't really understand.</p>
24
2009-12-22T01:46:53Z
14,602,233
<p>This answer from Carlos A. Ibarra is in principle right, however that implementation might break since you are iterating over a list that might be changed by calling removeHandler(). This is unsafe. Two alternatives are:</p> <pre><code>while len(logging.root.handlers) &gt; 0: logging.root.removeHandler(logging....
4
2013-01-30T10:51:56Z
[ "python", "logging" ]
How to get value on a certain index, in a python list?
1,943,824
<p>I have a list which looks something like this</p> <pre><code>List = [q1,a1,q2,a2,q3,a3] </code></pre> <p>I need the final code to be something like this </p> <pre><code>dictionary = {q1:a1,q2:a2,q3:a3} </code></pre> <p>if only I can get values at a certain index e.g List[0] I can accomplish this, is there any wa...
6
2009-12-22T02:10:45Z
1,943,831
<p>Python dictionaries can be constructed using the <code>dict</code> class, given an iterable containing tuples. We can use this in conjunction with the <code>range</code> builtin to produce a collection of tuples as in (every-odd-item, every-even-item), and pass it to <code>dict</code>, such that the values organize...
11
2009-12-22T02:13:06Z
[ "python" ]
How to get value on a certain index, in a python list?
1,943,824
<p>I have a list which looks something like this</p> <pre><code>List = [q1,a1,q2,a2,q3,a3] </code></pre> <p>I need the final code to be something like this </p> <pre><code>dictionary = {q1:a1,q2:a2,q3:a3} </code></pre> <p>if only I can get values at a certain index e.g List[0] I can accomplish this, is there any wa...
6
2009-12-22T02:10:45Z
1,943,849
<p>Using extended slice notation:</p> <pre><code>dictionary = dict(zip(List[0::2], List[1::2])) </code></pre>
12
2009-12-22T02:20:21Z
[ "python" ]
How to get value on a certain index, in a python list?
1,943,824
<p>I have a list which looks something like this</p> <pre><code>List = [q1,a1,q2,a2,q3,a3] </code></pre> <p>I need the final code to be something like this </p> <pre><code>dictionary = {q1:a1,q2:a2,q3:a3} </code></pre> <p>if only I can get values at a certain index e.g List[0] I can accomplish this, is there any wa...
6
2009-12-22T02:10:45Z
1,943,865
<p>The range-based answer is simpler, but there's another approach possible using the itertools package:</p> <pre><code>from itertools import izip dictionary = dict(izip(*[iter(List)] * 2)) </code></pre> <p>Breaking this down (<strong>edit:</strong> tested this time):</p> <pre><code># Create instance of iterator wra...
3
2009-12-22T02:30:02Z
[ "python" ]
How to get value on a certain index, in a python list?
1,943,824
<p>I have a list which looks something like this</p> <pre><code>List = [q1,a1,q2,a2,q3,a3] </code></pre> <p>I need the final code to be something like this </p> <pre><code>dictionary = {q1:a1,q2:a2,q3:a3} </code></pre> <p>if only I can get values at a certain index e.g List[0] I can accomplish this, is there any wa...
6
2009-12-22T02:10:45Z
1,943,919
<p>You've mentioned in the comments that you have duplicate entries. We can work with this. Take your favorite method of generating the list of tuples, and expand it into a <code>for</code> loop:</p> <pre><code>from itertools import izip dictionary = {} for k, v in izip(List[::2], List[1::2]): if k not in diction...
0
2009-12-22T02:58:53Z
[ "python" ]
How to get value on a certain index, in a python list?
1,943,824
<p>I have a list which looks something like this</p> <pre><code>List = [q1,a1,q2,a2,q3,a3] </code></pre> <p>I need the final code to be something like this </p> <pre><code>dictionary = {q1:a1,q2:a2,q3:a3} </code></pre> <p>if only I can get values at a certain index e.g List[0] I can accomplish this, is there any wa...
6
2009-12-22T02:10:45Z
1,943,943
<pre><code>d = {} for i in range(0, len(List), 2): d[List[i]] = List[i+1] </code></pre>
2
2009-12-22T03:07:54Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,866
<p>You'll notice that <code>b</code> isn't defined anywhere. While <code>_b</code> is.</p> <p>Unless you're asking why that naming convention is being used, in which case, you should ask the author of that code.</p>
0
2009-12-22T02:31:10Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,867
<p>The underscore is simply used to denote that the variable is private.</p>
0
2009-12-22T02:31:40Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,868
<p>The class object <code>a</code> has an attribute <code>_b</code> which is found when asking for <code>self._b</code>. The same class object <code>a</code> has no attribute <code>b</code>.</p> <p><code>_b</code> and <code>b</code> are as different as <code>beehive</code> and <code>zulu</code>. </p> <p>You will ge...
-1
2009-12-22T02:31:46Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,870
<p>Per pep8 (<a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">http://www.python.org/dev/peps/pep-0008/</a>), a single underscore is intended to denote a "generally private" method or attribute. The interpreter itself has no interaction with the single underscore, its more of a convention. The doubl...
8
2009-12-22T02:31:49Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,872
<p>Prefixing a variable or function name with an underscore is a convention in Python to indicate that the variable is private. From <a href="http://docs.python.org/tutorial/classes.html#private-variables" rel="nofollow">the docs</a>:</p> <blockquote> <p>Private” instance variables that cannot be accessed except f...
10
2009-12-22T02:32:37Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,943,934
<p>Basically, the idea is that in Python convention, there are three levels of encapsulation/hiding.</p> <ol> <li><p>Public attributes and methods: These are meant to be accessed by other classes without restriction.</p></li> <li><p>Private attributes and methods (prefixed with an underscore): These are meant to be hi...
4
2009-12-22T03:03:40Z
[ "python" ]
What's the difference between _b and b in this place
1,943,863
<pre><code>class a: def __init__(self): self._b()#why here use _b,not b,What's the difference self._c='cccc'#why here use _c,not c,What's the difference def _b(): print 'bbbb' </code></pre> <p>a.py</p> <pre><code>class a: def __init__(self): self._b()#why here use _b,not b,...
1
2009-12-22T02:28:53Z
1,945,039
<p>Difference between single and double underscore:</p> <pre><code>class Foo(object): def regular_method(self): print 'ok' def _soft_private(self): print 'ok' def __mangled_private(self): print 'ok' f = Foo() f.regular_method() # prints ok f._soft_private() # print ok f.__mangle...
3
2009-12-22T08:54:39Z
[ "python" ]
Extending numpy with cython
1,944,006
<p>I am trying to wrap a header file which has lots of functions like this </p> <p><strong>test.h</strong></p> <pre><code>void test(int N, int* data_in, int* data_out); </code></pre> <p>so that I can use those from numpy.</p> <p>Right now I have the following cython code:</p> <p><strong>test.pyx</strong></p> <pre...
4
2009-12-22T03:32:20Z
1,967,143
<p>This issue is currently being discussed on the Cython mailing list; apparently it stems from a small bug in one of the Cython libraries:</p> <p><a href="http://codespeak.net/mailman/listinfo/cython-dev" rel="nofollow">http://codespeak.net/mailman/listinfo/cython-dev</a></p> <p>For the moment, one potential workaro...
4
2009-12-27T21:51:44Z
[ "python", "numpy", "wrapping", "cython" ]
Extending numpy with cython
1,944,006
<p>I am trying to wrap a header file which has lots of functions like this </p> <p><strong>test.h</strong></p> <pre><code>void test(int N, int* data_in, int* data_out); </code></pre> <p>so that I can use those from numpy.</p> <p>Right now I have the following cython code:</p> <p><strong>test.pyx</strong></p> <pre...
4
2009-12-22T03:32:20Z
5,053,184
<p>Another workaround that doesn't require you to change things from <code>int</code>s to <code>long</code>s: change the function signature in the <code>cdef extern from '...'</code> block. Cython uses the declarations in a <code>cdef extern</code> block only to check types while generating the <code>.c</code> file, b...
2
2011-02-19T20:01:51Z
[ "python", "numpy", "wrapping", "cython" ]
Why isn't count() working the way I expect in my code?
1,944,064
<pre><code>class FriendshipManager(models.Manager): def are_friends(self, user1, user2): if self.filter(from_user=user1, to_user=user2).count() &gt; 0: return True if self.filter(from_user=user2, to_user=user1).count() &gt; 0: return True re...
0
2009-12-22T03:48:09Z
1,944,080
<p>I think you want to use <code>len(a)</code> rather than <code>a.count()</code> if you want to determine the length/size of the list. <code>a.count()</code> actually requires an argument anyway. It counts the number of occurrences of a value. For example:</p> <pre><code>a = [2, 6, 2, 1, 5, 3, 9, 5] print a.count(...
5
2009-12-22T03:53:34Z
[ "python", "django" ]
Why isn't count() working the way I expect in my code?
1,944,064
<pre><code>class FriendshipManager(models.Manager): def are_friends(self, user1, user2): if self.filter(from_user=user1, to_user=user2).count() &gt; 0: return True if self.filter(from_user=user2, to_user=user1).count() &gt; 0: return True re...
0
2009-12-22T03:48:09Z
1,944,082
<p>len is the correct one for that case.</p> <pre><code>&gt;&gt;&gt; a=[1,2,3,4] &gt;&gt;&gt; print len(a) 4 </code></pre>
3
2009-12-22T03:55:10Z
[ "python", "django" ]
Why isn't count() working the way I expect in my code?
1,944,064
<pre><code>class FriendshipManager(models.Manager): def are_friends(self, user1, user2): if self.filter(from_user=user1, to_user=user2).count() &gt; 0: return True if self.filter(from_user=user2, to_user=user1).count() &gt; 0: return True re...
0
2009-12-22T03:48:09Z
1,944,255
<p>The issue here is that you've mixed up two methods with the same name.</p> <p>On a sequence in Python, <code>count()</code> works exactly has Dustin describes to "count the number of occurrences of the parameter in the sequence."</p> <p>The code you're referencing however, is from a Django model. There, calling <...
8
2009-12-22T05:03:34Z
[ "python", "django" ]
Why isn't count() working the way I expect in my code?
1,944,064
<pre><code>class FriendshipManager(models.Manager): def are_friends(self, user1, user2): if self.filter(from_user=user1, to_user=user2).count() &gt; 0: return True if self.filter(from_user=user2, to_user=user1).count() &gt; 0: return True re...
0
2009-12-22T03:48:09Z
1,944,260
<p>I am not sure if this is a right answer, but, try this instead of the Model Manager code which you have. No big changes as such ofcourse.</p> <pre><code>class FriendshipManager(models.Manager): def are_friends(self, user1, user2): if self.filter(from_user=user1, to_user=user2).count() or self.filter(...
0
2009-12-22T05:05:47Z
[ "python", "django" ]
Sqlalchemy query not commiting
1,944,423
<p>I'm trying to create a simple unique username function for use in a Formencode schema. Here is the function:</p> <pre><code>class UniqueUsername(formencode.FancyValidator): def _to_python(self, value, state): user = DBSession.query(User.user_name).filter(User.username==value) if user is n...
1
2009-12-22T05:59:17Z
1,946,004
<p>It should be:</p> <pre><code>user = DBSession.query(User.user_name).filter(User.username==value).first() </code></pre> <p>also: is it User.user_name or User.username ?</p>
4
2009-12-22T12:11:36Z
[ "python", "sqlalchemy", "formencode" ]
How do I write good/correct package __init__.py files
1,944,569
<p>My package has the following structure:</p> <pre><code>mobilescouter/ __init__.py #1 mapper/ __init__.py #2 lxml/ __init__.py #3 vehiclemapper.py vehiclefeaturemapper.py vehiclefeaturesetmapper.py ... basemapper.py vehicle/ ...
112
2009-12-22T06:48:52Z
1,944,610
<p>My own <code>__init__.py</code> files are empty more often than not. In particular, I never have a <code>from blah import *</code> as part of <code>__init__.py</code> -- if "importing the package" means getting all sort of classes, functions etc defined directly as part of the package, then I would lexically copy t...
78
2009-12-22T06:59:24Z
[ "python", "packages" ]
How do I write good/correct package __init__.py files
1,944,569
<p>My package has the following structure:</p> <pre><code>mobilescouter/ __init__.py #1 mapper/ __init__.py #2 lxml/ __init__.py #3 vehiclemapper.py vehiclefeaturemapper.py vehiclefeaturesetmapper.py ... basemapper.py vehicle/ ...
112
2009-12-22T06:48:52Z
1,947,834
<p><code>__all__</code> is very good - it helps guide import statements without automatically importing modules <a href="http://docs.python.org/tutorial/modules.html#importing-from-a-package">http://docs.python.org/tutorial/modules.html#importing-from-a-package</a></p> <p>using <code>__all__</code> and <code>import *<...
75
2009-12-22T17:16:14Z
[ "python", "packages" ]
What is the relationship between __getattr__ and getattr?
1,944,625
<p>I know this code is right:</p> <pre><code>class A: def __init__(self): self.a = 'a' def method(self): print "method print" a = A() print getattr(a, 'a', 'default') print getattr(a, 'b', 'default') print getattr(a, 'method', 'default') getattr(a, 'method', 'default')() ...
25
2009-12-22T07:04:18Z
1,944,663
<p><code>getattr</code> is a built-in function taking (at least) two arguments: the object from which you're getting the attribute, and the string name of the attribute.</p> <p>If the string name is a constant, say <code>'foo'</code>, <code>getattr(obj, 'foo')</code> is <strong>exactly the same thing</strong> as <code...
31
2009-12-22T07:14:16Z
[ "python", "getattr" ]
What is the relationship between __getattr__ and getattr?
1,944,625
<p>I know this code is right:</p> <pre><code>class A: def __init__(self): self.a = 'a' def method(self): print "method print" a = A() print getattr(a, 'a', 'default') print getattr(a, 'b', 'default') print getattr(a, 'method', 'default') getattr(a, 'method', 'default')() ...
25
2009-12-22T07:04:18Z
1,944,708
<p>Alex's answer was good, but providing you with a sample code since you asked for it :)</p> <pre><code>class foo: def __init__(self): self.a = "a" def __getattr__(self, attribute): return "You asked for %s, but I'm giving you default" % attribute &gt;&gt;&gt; bar = foo() &gt;&gt;&gt; bar.a ...
43
2009-12-22T07:26:49Z
[ "python", "getattr" ]
What is the relationship between __getattr__ and getattr?
1,944,625
<p>I know this code is right:</p> <pre><code>class A: def __init__(self): self.a = 'a' def method(self): print "method print" a = A() print getattr(a, 'a', 'default') print getattr(a, 'b', 'default') print getattr(a, 'method', 'default') getattr(a, 'method', 'default')() ...
25
2009-12-22T07:04:18Z
1,944,714
<p><code>__getattr__()</code> is a special method function that you can define. When a member lookup fails, this function will be called.</p> <p><code>getattr()</code> is a function you can call to attempt a member lookup. If the lookup succeeds, you get the member (perhaps a method function object, or perhaps a dat...
11
2009-12-22T07:28:59Z
[ "python", "getattr" ]
Having trouble with Python's telnetlib module
1,945,084
<p>I have a basic chat server which I can easily connect to with telnet. I simply enter the host and port and, without any further authentication, can begin entering commands that the server can interpret. In an effort to simulate user traffic, I would like to create a script that will open telnet, connect to the serve...
4
2009-12-22T09:06:34Z
1,945,097
<p>In your code, you write to the server, but in order to receive data, you'll eventually have to one of the <a href="http://docs.python.org/library/telnetlib.html#telnetlib.Telnet.read%5Funtil" rel="nofollow">read methods</a> of the library. To see an example implementation of a client with a 'reading thread', see thi...
0
2009-12-22T09:09:30Z
[ "python", "telnet" ]
Having trouble with Python's telnetlib module
1,945,084
<p>I have a basic chat server which I can easily connect to with telnet. I simply enter the host and port and, without any further authentication, can begin entering commands that the server can interpret. In an effort to simulate user traffic, I would like to create a script that will open telnet, connect to the serve...
4
2009-12-22T09:06:34Z
1,945,113
<p>Perhaps adding a newline would help?</p> <pre><code>myTel.write('login\n') </code></pre> <p>I'd also recommend using some network sniffing tool (i.e. Wireshark) to verify that you're really sending what you intended.</p>
3
2009-12-22T09:13:04Z
[ "python", "telnet" ]
Having trouble with Python's telnetlib module
1,945,084
<p>I have a basic chat server which I can easily connect to with telnet. I simply enter the host and port and, without any further authentication, can begin entering commands that the server can interpret. In an effort to simulate user traffic, I would like to create a script that will open telnet, connect to the serve...
4
2009-12-22T09:06:34Z
1,945,140
<p>I would use read_until to check that the server is ready to receive the command here is a sample using telnet to talk to a cisco switch:</p> <pre><code>from telnetlib import Telnet PASSWD = "mypass" t = Telnet(switch) t.read_until("Password: ", 2) t.write(PASSWD + "\r\n") </code></pre> <p>Here I wait for a passwo...
0
2009-12-22T09:19:14Z
[ "python", "telnet" ]
Python Format string "}" fill
1,945,230
<p>Python 2.6 defines <a href="http://docs.python.org/library/stdtypes.html#str.format" rel="nofollow"><code>str.format(…)</code></a>, which, from Python 3.0, is preferred to the old <code>%</code> style of string formatting. Looking at the "<a href="http://docs.python.org/library/string.html#format-specification-min...
3
2009-12-22T09:37:28Z
1,945,687
<p>You could always specify it as a separate parameter like so:</p> <pre><code>&gt;&gt;&gt; "The word is {0:{1}&lt;10}".format("spam", "}") 'The word is spam}}}}}}' </code></pre> <p>See, it gets passed in and used in place of {1}.</p>
7
2009-12-22T11:05:20Z
[ "python", "string-formatting" ]
Why does the following code always output 16?
1,945,270
<pre><code>def makeActions(): acts=[] for i in range(5): print len(acts) acts.append(lambda x: i ** x) print acts[i] return acts acts=makeActions() for i in range(5): print acts[i](2) </code></pre> <p>Output:</p> <pre><code>16 16 16 16 16 </code></pre> <p>Expected output:</p> <pre><co...
4
2009-12-22T09:45:51Z
1,945,321
<p>Because the <code>i</code> in the lambda is probably not what you expect. To verify this, change the code:</p> <pre><code>acts.append(lambda x: (i, i ** x)) </code></pre> <p>Now the <code>print</code> tells you the value of <code>i</code>:</p> <pre><code>(4, 16) (4, 16) (4, 16) (4, 16) (4, 16) </code></pre> <p>T...
23
2009-12-22T09:54:41Z
[ "python" ]
Why does the following code always output 16?
1,945,270
<pre><code>def makeActions(): acts=[] for i in range(5): print len(acts) acts.append(lambda x: i ** x) print acts[i] return acts acts=makeActions() for i in range(5): print acts[i](2) </code></pre> <p>Output:</p> <pre><code>16 16 16 16 16 </code></pre> <p>Expected output:</p> <pre><co...
4
2009-12-22T09:45:51Z
1,945,324
<p>Because all lambda functions you create are bound to i, which becomes 4 at the end of loop, and as we all well know 4*4 = 16</p> <p>to avoid that create your functions using nested function(closure) e.g.</p> <pre><code>def makePowerFunc(base): def powerFunc(x): return base**x return powerFunc def ...
6
2009-12-22T09:54:57Z
[ "python" ]
Why does the following code always output 16?
1,945,270
<pre><code>def makeActions(): acts=[] for i in range(5): print len(acts) acts.append(lambda x: i ** x) print acts[i] return acts acts=makeActions() for i in range(5): print acts[i](2) </code></pre> <p>Output:</p> <pre><code>16 16 16 16 16 </code></pre> <p>Expected output:</p> <pre><co...
4
2009-12-22T09:45:51Z
1,945,326
<p>It's counterintuitive or at least less common syntax. I guess you meant:</p> <pre><code>acts.append(lambda x, i = i: i ** x) </code></pre> <p>which will output:</p> <pre><code>0 1 4 9 16 </code></pre> <p><hr></p> <p>fn. in your version,</p> <pre><code>acts.append(lambda x, i: i ** x) </code></pre> <p>the lamb...
5
2009-12-22T09:55:04Z
[ "python" ]
Why does the following code always output 16?
1,945,270
<pre><code>def makeActions(): acts=[] for i in range(5): print len(acts) acts.append(lambda x: i ** x) print acts[i] return acts acts=makeActions() for i in range(5): print acts[i](2) </code></pre> <p>Output:</p> <pre><code>16 16 16 16 16 </code></pre> <p>Expected output:</p> <pre><co...
4
2009-12-22T09:45:51Z
1,945,368
<p>This phenomena is called lambda binding see <a href="http://stackoverflow.com/questions/160859/what-is-lambda-binding-in-python">http://stackoverflow.com/questions/160859/what-is-lambda-binding-in-python</a></p>
3
2009-12-22T10:03:49Z
[ "python" ]
How can I copy a remote image over HTTP to gtk.gdk.pixbuf by Python?
1,945,331
<p>this is first post here and I am a noob programmer.<br> This may be a stupid question.</p> <p>I'd like to create a personal Twitter notifier on GNOE Desktop. And I've decided to use <code>pynotify</code> and <code>Tweepy</code>.<br> Now I just want to make <code>pynotify</code> show Twitter user's icon, and there s...
1
2009-12-22T09:56:16Z
1,945,489
<p>did you mean something <a href="http://faq.pygtk.org/index.py?req=show&amp;file=faq08.016.htp" rel="nofollow">like this</a>?</p>
2
2009-12-22T10:27:33Z
[ "python", "gtk", "pynotify" ]
python add a new div every 3rd iteration
1,945,379
<p>I have a product list that put 3 products on a row and clears the row and adds another 3, this works fine everywhere but IE6, i know that adding <code>&lt;div&gt;</code> around each group of 3 products will solve this is the template file at the moment</p> <pre><code>{% for product in category.products.all %} ...
13
2009-12-22T10:05:45Z
1,945,399
<p>Use <code>forloop.counter</code> and a modulo operator inside the loop:</p> <pre><code>{% for ... %} {% if forloop.counter|divisibleby:3 %}&lt;div&gt;{% endif %} ... {% if forloop.counter|divisibleby:3 %}&lt;/div&gt;{% endif %} {% endfor %} </code></pre> <p>See <a href="http://docs.djangoproject.com/en/dev/ref/tem...
7
2009-12-22T10:08:56Z
[ "python", "django-templates" ]
python add a new div every 3rd iteration
1,945,379
<p>I have a product list that put 3 products on a row and clears the row and adds another 3, this works fine everywhere but IE6, i know that adding <code>&lt;div&gt;</code> around each group of 3 products will solve this is the template file at the moment</p> <pre><code>{% for product in category.products.all %} ...
13
2009-12-22T10:05:45Z
1,945,620
<p>codeape's solution only works if you are using a very recent SVN checkout of Django trunk. If you're using version 1.1 or below, that syntax is not supported.</p> <p>Instead, you can use the <code>divisibleby</code> filter:</p> <pre><code>{% if forloop.counter|divisibleby:3 %}&lt;div&gt;{% endif %} </code></pre>
29
2009-12-22T10:54:50Z
[ "python", "django-templates" ]
converting a zcml based python script to a standalone script in zope/plone
1,945,812
<p>I have a python class working in zope 3 zcml kind of way, but i want to move the python into a standalone script that a could access via something along the lines of tal:content='context/get_tags'. This is the code as it stands:</p> <pre><code>class TagListView(BrowserView): def getCategories(self): categories...
3
2009-12-22T11:29:09Z
1,946,051
<p>You can access views from page templates with the @@ syntax: context/@@viewname:</p> <pre><code>tal:define="view context/@@get_tags; entries view/entries;" </code></pre>
2
2009-12-22T12:22:16Z
[ "python", "plone", "zope" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,945,930
<p>The latter strings shouldn't start with a slash. If they start with a slash, then they're considered an "absolute path" and everything before them is discarded.</p> <p>Quoting the <a href="http://docs.python.org/library/os.path.html#os.path.join">Python docs for <code>os.path.join</code></a>:</p> <blockquote> <p...
187
2009-12-22T11:55:44Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,945,935
<p>do not use forward slashes at the beginning of path components, except when refering to the root dir:</p> <pre><code>os.path.join('/home/build/test/sandboxes', todaystr, 'new_sandbox') </code></pre> <p>see also: <a href="http://docs.python.org/library/os.path.html#os.path.join">http://docs.python.org/library/os.pa...
9
2009-12-22T11:56:54Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,945,936
<p>Try with <code>new_sandbox</code> only</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, 'new_sandbox') </code></pre>
3
2009-12-22T11:56:56Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,945,939
<p>It's because your <code>'/new_sandbox/'</code> begins with a <code>/</code> and thus is assumed to be relative to the root directory. Remove the leading <code>/</code>.</p>
8
2009-12-22T11:57:06Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,946,192
<p>do it like this, without too the extra slashes</p> <pre><code>root="/home" os.path.join(root,"build","test","sandboxes",todaystr,"new_sandbox") </code></pre>
3
2009-12-22T12:48:33Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
1,948,595
<p>The idea of <code>os.path.join()</code> is to make your program cross-platform (linux/win32/etc).</p> <p>Even one slash ruins it.</p> <p>So it only makes sense when being used with some kind of reference point like <code>os.environ['HOME']</code> or <code>os.path.dirname(__file__)</code>.</p>
99
2009-12-22T19:29:50Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
14,962,135
<p><code>os.path.join()</code> can be used in conjunction with <code>os.path.sep()</code> to create an absolute rather than relative path.</p> <pre><code>os.path.join(os.path.sep, 'home','build','test','sandboxes',todaystr,'new_sandbox') </code></pre>
36
2013-02-19T16:14:54Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
24,296,743
<p>To help understand why this surprising behavior isn't <em>entirely</em> terrible, consider an application which accepts a config file name as an argument:</p> <pre><code>config_root = "/etc/myapp.conf/" file_name = os.path.join(config_root, sys.argv[1]) </code></pre> <p>If the application is executed with:</p> <p...
5
2014-06-18T23:50:21Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
25,559,273
<p>To make your function more portable, use it as such:</p> <pre><code>os.path.join(os.sep, 'home', 'build', 'test', 'sandboxes', todaystr, 'new_sandbox') </code></pre> <p>or</p> <pre><code>os.path.join(os.environ.get("HOME"), 'test', 'sandboxes', todaystr, 'new_sandbox') </code></pre>
5
2014-08-28T22:57:20Z
[ "python", "path" ]
Why doesn't os.path.join() work in this case?
1,945,920
<p>The below code will not join, when debugged the command does not store the whole path but just the last entry.</p> <pre><code>os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') </code></pre> <p>When I test this it only stores the <code>/new_sandbox/</code> part of the code.</p>
135
2009-12-22T11:53:28Z
37,556,617
<p>Note that a similar issue can bite you if you use <code>os.path.join()</code> to include an extension that already includes a dot, which is what happens automatically when you use <code>os.path.splitext()</code>. In this example:</p> <pre><code>components = os.path.splitext(filename) prefix = components[0] extensio...
0
2016-05-31T22:23:32Z
[ "python", "path" ]
Variable referenced instead of copied
1,946,109
<p>When I run the code below it removes <code>deleted_partner</code> from <code>B</code>. But as it removes it from <code>B</code> it also removes it from <code>A</code>. So that when I try to remove it from <code>A</code> the program crashes. What is the problem?</p> <pre><code>for deleted_partner in self.list_of_tra...
1
2009-12-22T12:33:04Z
1,946,150
<p>Since you do not put the real list is hard to do tests, buy you can try with copy.copy or copy.deepcopy</p> <pre><code>import copy for deleted_partner in self.list_of_trading_partners: B = copy.copy(A) print("t", deleted_partner) print(B[self.ID].list_of_trading_partners) B[self.ID].list_of_tradin...
1
2009-12-22T12:41:19Z
[ "python" ]
Variable referenced instead of copied
1,946,109
<p>When I run the code below it removes <code>deleted_partner</code> from <code>B</code>. But as it removes it from <code>B</code> it also removes it from <code>A</code>. So that when I try to remove it from <code>A</code> the program crashes. What is the problem?</p> <pre><code>for deleted_partner in self.list_of_tra...
1
2009-12-22T12:33:04Z
1,946,166
<p>You're not removing from B or A, but from <code>A[some_ID].list_of_trading_partners</code> and <code>B[some_ID].list_of_trading_partners</code>. <code>[:]</code> only makes a "shallow copy" of the list, in that it creates a new, separate list, but the elements contained in that list (one of which <code>list_of_tradi...
2
2009-12-22T12:43:24Z
[ "python" ]
Variable referenced instead of copied
1,946,109
<p>When I run the code below it removes <code>deleted_partner</code> from <code>B</code>. But as it removes it from <code>B</code> it also removes it from <code>A</code>. So that when I try to remove it from <code>A</code> the program crashes. What is the problem?</p> <pre><code>for deleted_partner in self.list_of_tra...
1
2009-12-22T12:33:04Z
1,946,180
<p><code>B=A[:]</code> does copy <strong>only</strong> the list, but <strong>not</strong> it's contents. <code>B[self.ID]</code> and <code>A[self.ID]</code> still reference the same object, only <code>A</code> and <code>B</code> are different. </p> <p>You might way to explicitly copy all the elements of the list too -...
1
2009-12-22T12:46:27Z
[ "python" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
1,946,243
<p>Here is an interessting Thread from Python Forum for you: <a href="http://www.python-forum.org/pythonforum/viewtopic.php?f=2&amp;t=8976" rel="nofollow">Python Forum</a></p> <p>Edit: There was also an interessting question on stackoverflow regarding mouse control...maybe it is a good starting point.. <a href="http:/...
1
2009-12-22T12:58:48Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
1,946,361
<p>for the mouse, I've found <a href="http://code.google.com/p/pymouse/" rel="nofollow">pymouse</a> which seems to work (I haven't fully tried it, a small hack needed for the click, cf the issues)</p> <p>for the keyboard, I'm not sure Xlib can do the job. I'm still looking on how to write something but you can catch k...
1
2009-12-22T13:26:31Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
1,946,625
<p>For console try ncurses or slang. In other situation try PyQt, PyGtk, TkInter.</p> <p>ALL of this solution ARE cross-platform and work almost anywhere. </p>
1
2009-12-22T14:17:34Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
1,946,862
<p>I use dogtail (<a href="https://fedorahosted.org/dogtail/">https://fedorahosted.org/dogtail/</a>) to do such things, using this I have created a automated testing fromework for my linux(ubuntu) app. That framework clicks buttons and types into text fields.</p> <p>see the gedit example, <a href="https://fedorahosted...
6
2009-12-22T14:54:46Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
1,953,460
<blockquote> <p>For linux there is Xlib but does it works for keyboard as well? (found only reference to the mouse)</p> </blockquote> <p>Yes, it work for keyboard also.</p>
1
2009-12-23T15:33:11Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
7,072,927
<p>This totally works... on a Mac at least. This is for a click AND drag, etc.. but can be retrofitted accordingly.</p> <pre><code>#!/usr/bin/python import sys import time from Quartz.CoreGraphics import * # imports all of the top-level symbols in the module def mouseEvent(type, posx, posy): theEvent = CGEventCr...
2
2011-08-16T02:14:21Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
30,063,070
<p>A cross platform solution on linux, windows and mac is autopy. <a href="https://github.com/msanders/autopy/" rel="nofollow">https://github.com/msanders/autopy/</a></p> <p>It allows controlling mouse and keyboard, taking screenshots, and finding small bitmaps on larger bitmaps and should be very convenient if you wa...
0
2015-05-05T20:53:04Z
[ "python", "linux", "awtrobot" ]
How can I control the keyboard and mouse with Python?
1,946,181
<p>How can I control the mouse and keyboard in Python?</p> <p>The idea is to do the same as the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html">Robot() class</a> in Java. Be able to say: move the mouse from here to here, click there, write that whatever is on the screen.</p> <p>For Windows there...
10
2009-12-22T12:46:31Z
33,730,810
<p>I can advise you <a href="https://pypi.python.org/pypi/PyAutoGUI" rel="nofollow">PyAutoGUI</a>, it allows to full control Mouse and Keyboard and get Screenshots and even you can locate images in screen (yes, like: where is the button?), very useful to automate clicks dynamically.</p> <p>Enjoy ;-)</p>
0
2015-11-16T08:07:42Z
[ "python", "linux", "awtrobot" ]
How to create an internationalized Google App Engine application
1,946,251
<p>I would like to provide my Python GAE website in the user's own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext files (.po and .mo files).</p> <p>Has someone successfully combined Python <strong>Google App Engine</strong> and <strong>gettext</strong> files...
11
2009-12-22T13:01:05Z
1,946,398
<p>You can use the Django internationalisation tool, like explained <a href="http://web.archive.org/web/20150308191617/http://blog.yjl.im/2009/02/using-django-i18n-in-google-app-engine.html" rel="nofollow">here</a>.</p> <p>They are also saying that there is no easy way to do this.</p> <p>I hope that helps you :)</p>
4
2009-12-22T13:34:01Z
[ "python", "google-app-engine", "internationalization", "gettext" ]
How to create an internationalized Google App Engine application
1,946,251
<p>I would like to provide my Python GAE website in the user's own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext files (.po and .mo files).</p> <p>Has someone successfully combined Python <strong>Google App Engine</strong> and <strong>gettext</strong> files...
11
2009-12-22T13:01:05Z
1,946,402
<p>As my needs were simple, I used a simple hack instead of (unavailable) gettext. I created a file with string translations, <code>translate.py</code>. Approximately like this:</p> <pre><code>en={} ru={} en['default_site_title']=u"Site title in English" ru['default_site_title']=u"Название сайта по-рÑ...
6
2009-12-22T13:34:35Z
[ "python", "google-app-engine", "internationalization", "gettext" ]
Best way to serialize a range of hours to meaningful data
1,946,265
<p>A part of my web app involves creating 'appointments' (shifts) using a drag-and-drop selector for each day. Currently, this data is serialized to something like this:</p> <pre><code>9,10,11,12,13,14,15,16,76,77,78,298,299,300,301,302,303,304, </code></pre> <p>Where each number represents the nth half-hour of the w...
2
2009-12-22T13:03:56Z
1,946,316
<p>Well, assuming you have the code in place to extract the ranges (i.e. 1,2,3,4 is converted to [1,4]), I don't see why you cannot use standard date/time arithmetic.</p> <pre><code>from datetime import datetime, timedelta def get_shift(start_of_week, start, end): """Returns a 2-tuple of datetimes""" HALF_HOU...
1
2009-12-22T13:14:59Z
[ "javascript", "jquery", "python", "serialization" ]
Best way to serialize a range of hours to meaningful data
1,946,265
<p>A part of my web app involves creating 'appointments' (shifts) using a drag-and-drop selector for each day. Currently, this data is serialized to something like this:</p> <pre><code>9,10,11,12,13,14,15,16,76,77,78,298,299,300,301,302,303,304, </code></pre> <p>Where each number represents the nth half-hour of the w...
2
2009-12-22T13:03:56Z
1,946,334
<p>You're talking about intervals. </p> <p>The best way to have an interval is a start time and a duration.</p> <pre><code>class Shift( object ): def __init__( self, start, length ): self.start= start self.length= length def __repr__( self ): return "Shift(%d,%d)" % ( self.start, s...
2
2009-12-22T13:21:17Z
[ "javascript", "jquery", "python", "serialization" ]