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 |
|---|---|---|---|---|---|---|---|---|---|
How can I monitor mouse events with Python Xlib instead of capture them? | 2,585,647 | <p>I need to monitor and filter mouse events with Xlib in Python.</p>
<p>So far I have found out that this code receives events, but does not pass them on, so I can't actually do anything with the mouse anymore.</p>
<pre><code>from Xlib.display import Display
from Xlib import X
display = Display(':0')
root = display... | 1 | 2010-04-06T14:37:19Z | 3,799,399 | <p>The link was broken. I think this is the latest one: <a href="http://github.com/pepijndevos/PyMouse/blob/master/pymouse/unix.py" rel="nofollow">http://github.com/pepijndevos/PyMouse/blob/master/pymouse/unix.py</a> Line 58</p>
| 0 | 2010-09-26T19:19:02Z | [
"python",
"event-handling",
"xlib"
] |
python: can't terminate a thread hung in socket.recvfrom() call | 2,585,680 | <p>I cannot get a way to terminate a thread that is hung in a socket.recvfrom() call. For example, ctrl+c that should trigger KeyboardInterrupt exception can't be caught. Here is a script I've used for testing:</p>
<pre><code>from socket import *
from threading import Thread
from sys import exit
class TestThread(Thre... | 0 | 2010-04-06T14:40:41Z | 2,585,743 | <p>You should open a pipe from the main thread to the network thread and 'select' on both the socket and the pipe. When you want to terminate the network thread, just send a byte through the pipe from the main thread and act accordingly in the network thread.</p>
<p>Just my 2 cents.</p>
| 1 | 2010-04-06T14:46:13Z | [
"python",
"multithreading"
] |
python: can't terminate a thread hung in socket.recvfrom() call | 2,585,680 | <p>I cannot get a way to terminate a thread that is hung in a socket.recvfrom() call. For example, ctrl+c that should trigger KeyboardInterrupt exception can't be caught. Here is a script I've used for testing:</p>
<pre><code>from socket import *
from threading import Thread
from sys import exit
class TestThread(Thre... | 0 | 2010-04-06T14:40:41Z | 2,585,845 | <p>Keyboard interrupts are always caught on the main thread -- never on "child" threads. To avoid <code>server_thread</code> keeping the process alive when the main thread exits, do</p>
<pre><code>server_thread.daemon = True
</code></pre>
<p>before you call <code>server_thread.start()</code>.</p>
<p>BTW, your <code... | 4 | 2010-04-06T14:56:49Z | [
"python",
"multithreading"
] |
QFileDialog and german umlaute within a path | 2,585,694 | <p>i am working on a project, which i am developing with Python and PyQT4. I have stumbled upon a somewhat odd behaviour of the QFileDialog, that is not occuring when running the project within in my IDE (Eclipse).</p>
<p>The problem is that QFileDialog in ExistingFiles-mode does fail to return the list of selected fi... | 3 | 2010-04-06T14:41:46Z | 2,608,921 | <ol>
<li><p>Try to use lambda x: x.toUtf8(), or toLocal8Bit() or set TextCodec to any codepage you want, it should help. These methods return properly encoded python strings. Avoid using str() on QString, it is unaware of charmap you want.</p></li>
<li><p>What is getSelectedFiles()? There is no such method in Qt 4.5 or... | 0 | 2010-04-09T15:59:51Z | [
"python",
"path",
"pyqt",
"pyqt4",
"diacritics"
] |
QFileDialog and german umlaute within a path | 2,585,694 | <p>i am working on a project, which i am developing with Python and PyQT4. I have stumbled upon a somewhat odd behaviour of the QFileDialog, that is not occuring when running the project within in my IDE (Eclipse).</p>
<p>The problem is that QFileDialog in ExistingFiles-mode does fail to return the list of selected fi... | 3 | 2010-04-06T14:41:46Z | 2,846,142 | <p>You should use unicode() (not str()) to convert QString into Python unicode strings.</p>
| 0 | 2010-05-17T00:48:38Z | [
"python",
"path",
"pyqt",
"pyqt4",
"diacritics"
] |
Python Memory leak - Solved, but still puzzled | 2,585,712 | <p>Dear everyone,
I have successfully debugged my own memory leak problems. However, I have noticed some very strange occurence.</p>
<pre><code> for fid, fv in freqDic.iteritems():
outf.write(fid+"\t") #ID
for i, term in enumerate(domain): #Vector
tfidf = self.tf(term, f... | 2 | 2010-04-06T14:43:49Z | 2,585,834 | <p>Iterating over <code>freqDict</code> does not generate new values, but passes references to the values already held by the dict. This means you add new values to the fv which is held by <code>freqDict</code> even after the loop.</p>
<p>Another solution would be to clear freqDict after looping over it.</p>
<p>In ge... | 2 | 2010-04-06T14:56:04Z | [
"python",
"memory-leaks"
] |
Python Memory leak - Solved, but still puzzled | 2,585,712 | <p>Dear everyone,
I have successfully debugged my own memory leak problems. However, I have noticed some very strange occurence.</p>
<pre><code> for fid, fv in freqDic.iteritems():
outf.write(fid+"\t") #ID
for i, term in enumerate(domain): #Vector
tfidf = self.tf(term, f... | 2 | 2010-04-06T14:43:49Z | 2,585,869 | <p>It is not a memory leak, as memory is not leaking, it is being taken by your default dict e.g.</p>
<pre><code>from collections import defaultdict
d = defaultdict(int)
for i in xrange(10**7):
a = d[i]
</code></pre>
<p>Do you think it is a memory leak? you are assigning values to a dict and memory usage should ... | 0 | 2010-04-06T14:59:16Z | [
"python",
"memory-leaks"
] |
Python Memory leak - Solved, but still puzzled | 2,585,712 | <p>Dear everyone,
I have successfully debugged my own memory leak problems. However, I have noticed some very strange occurence.</p>
<pre><code> for fid, fv in freqDic.iteritems():
outf.write(fid+"\t") #ID
for i, term in enumerate(domain): #Vector
tfidf = self.tf(term, f... | 2 | 2010-04-06T14:43:49Z | 2,586,026 | <p>I suspect that the memory usage of Python might be increasing because floating point numbers are also objects in Python, and the interpreter maintains a freelist of floats which is unbounded and immortal. Therefore, whenever a float calculation results in a new float that did not occur before, Python allocates a new... | 0 | 2010-04-06T15:21:04Z | [
"python",
"memory-leaks"
] |
Bounced email on Google App Engine | 2,585,779 | <p>I'm developing application for google app engine (python), witch needs not only to send emails, but also know which ones bounce back.</p>
<p>I created special account for my domain noreply@example.com, added it as an app admin and sending messages from it.</p>
<p>The problem is (and it was described here <a href="... | 10 | 2010-04-06T14:49:45Z | 2,653,819 | <p>It looks like someone bypassed this problem by switching to <a href="http://developer.yahoo.com/mail/" rel="nofollow">Yahoo's Mail API</a>, which uses OAuth and can be used over HTTP. Until google fixes your problem, this looks like a viable solution.</p>
| 3 | 2010-04-16T14:46:56Z | [
"python",
"google-app-engine"
] |
Bounced email on Google App Engine | 2,585,779 | <p>I'm developing application for google app engine (python), witch needs not only to send emails, but also know which ones bounce back.</p>
<p>I created special account for my domain noreply@example.com, added it as an app admin and sending messages from it.</p>
<p>The problem is (and it was described here <a href="... | 10 | 2010-04-06T14:49:45Z | 2,726,660 | <p>You could use use a third party "email marketing" API like <a href="http://www.campaignmonitor.com/api" rel="nofollow">CampaignMonitor</a> that keeps track of the bounced addresses:
<a href="http://www.campaignmonitor.com/api/method/subscribers-getbounced/" rel="nofollow">http://www.campaignmonitor.com/api/method/su... | 1 | 2010-04-28T03:37:06Z | [
"python",
"google-app-engine"
] |
Bounced email on Google App Engine | 2,585,779 | <p>I'm developing application for google app engine (python), witch needs not only to send emails, but also know which ones bounce back.</p>
<p>I created special account for my domain noreply@example.com, added it as an app admin and sending messages from it.</p>
<p>The problem is (and it was described here <a href="... | 10 | 2010-04-06T14:49:45Z | 2,734,309 | <p>Until the issue is resolved, workaround for my project is using <a href="http://code.google.com/p/typhoonae/" rel="nofollow">typhoonae</a>, bind mail service to google app's gmail as SMTP (to send messages from noreply@example.com). When sending this way, noreply@example.com receives bounced messages. </p>
| 2 | 2010-04-29T01:51:41Z | [
"python",
"google-app-engine"
] |
Bounced email on Google App Engine | 2,585,779 | <p>I'm developing application for google app engine (python), witch needs not only to send emails, but also know which ones bounce back.</p>
<p>I created special account for my domain noreply@example.com, added it as an app admin and sending messages from it.</p>
<p>The problem is (and it was described here <a href="... | 10 | 2010-04-06T14:49:45Z | 15,623,225 | <p>Google has actually since added <a href="https://developers.google.com/appengine/docs/python/mail/bounce" rel="nofollow">a method for receiving bounced messages</a> via an HTTP Request. It requires adding to your app.yaml:</p>
<pre><code>inbound_services:
- mail_bounce
</code></pre>
<p>Which will cause a request t... | 1 | 2013-03-25T19:36:57Z | [
"python",
"google-app-engine"
] |
How to process python generated error messages my own way? | 2,585,803 | <p>For some code as follows,
<PRE>
opts, args = getopt.getopt(sys.argv[1:], "c:", ...
for o,v in opts:
...
elif o in ("-c", "--%s" % checkString):
kCheckOnly = True
clientTemp = v
</PRE></p>
<p>If I don't give the parameter after the -c, I get the error messages as follows.</p>... | 2 | 2010-04-06T14:52:51Z | 2,585,839 | <p>You can catch getopt.GetoptError and check the 'opt' and 'msg' attributes yourself:</p>
<pre>
try:
opts, args = getopt.getopt(sys.argv[1:], "c:", ...
except getopt.GetoptError, e:
if e.opt == 'c' and 'requires argument' in e.msg:
print >>sys.stderr, 'ERROR: You forgot to give the file name after -c ... | 3 | 2010-04-06T14:56:22Z | [
"python",
"getopt"
] |
How to process python generated error messages my own way? | 2,585,803 | <p>For some code as follows,
<PRE>
opts, args = getopt.getopt(sys.argv[1:], "c:", ...
for o,v in opts:
...
elif o in ("-c", "--%s" % checkString):
kCheckOnly = True
clientTemp = v
</PRE></p>
<p>If I don't give the parameter after the -c, I get the error messages as follows.</p>... | 2 | 2010-04-06T14:52:51Z | 2,585,858 | <p>the correct answer is to use the OptionParser module instead of trying to "roll your own".</p>
| 3 | 2010-04-06T14:58:24Z | [
"python",
"getopt"
] |
Good way to edit the previous defined class in ipython | 2,586,218 | <p>I am wondering a good way to follow if i would like to redefine the members of a previous defined class in ipython.
say :
I have defined a class intro like below, and later i want to redefine part of the function definition _print_api. Any way to do that without retyping it .</p>
<pre><code>class intro(object):
... | 14 | 2010-04-06T15:48:52Z | 2,586,414 | <p>There isn't really a "good" way to do it. The best you can do is something like this:</p>
<pre><code># class defined as normally above, but now you want to change a funciton
def new_print_api(self, obj):
# redefine the function (have to rewrite it)
...
# now set that function as the _print_api function in t... | 0 | 2010-04-06T16:16:16Z | [
"python",
"ipython"
] |
Good way to edit the previous defined class in ipython | 2,586,218 | <p>I am wondering a good way to follow if i would like to redefine the members of a previous defined class in ipython.
say :
I have defined a class intro like below, and later i want to redefine part of the function definition _print_api. Any way to do that without retyping it .</p>
<pre><code>class intro(object):
... | 14 | 2010-04-06T15:48:52Z | 2,586,442 | <p>When you get to the point where you've got that much code, it's easiest just to put it into a file and <code>import</code> that particular module. Then when you need to update, you can edit the file and re-run the <code>import</code> statement and you'll get the updated code, though previously defined objects will ... | 0 | 2010-04-06T16:19:24Z | [
"python",
"ipython"
] |
Good way to edit the previous defined class in ipython | 2,586,218 | <p>I am wondering a good way to follow if i would like to redefine the members of a previous defined class in ipython.
say :
I have defined a class intro like below, and later i want to redefine part of the function definition _print_api. Any way to do that without retyping it .</p>
<pre><code>class intro(object):
... | 14 | 2010-04-06T15:48:52Z | 2,586,577 | <p>If you use the IPython %edit features, you can use something like <a href="http://ipython.org/ipython-doc/stable/interactive/tips.html#lightweight-version-control">this</a></p>
| 5 | 2010-04-06T16:39:48Z | [
"python",
"ipython"
] |
Good way to edit the previous defined class in ipython | 2,586,218 | <p>I am wondering a good way to follow if i would like to redefine the members of a previous defined class in ipython.
say :
I have defined a class intro like below, and later i want to redefine part of the function definition _print_api. Any way to do that without retyping it .</p>
<pre><code>class intro(object):
... | 14 | 2010-04-06T15:48:52Z | 2,586,580 | <p>Use the %edit command, or its alias %ed. Assuming that the intro class already exists in the ipython namespace, typing <code>%ed intro</code> will open an external editor on the source code for the class. When you save and exit the editor the code will be executed by ipython, effectively redefining the class. </p... | 9 | 2010-04-06T16:40:16Z | [
"python",
"ipython"
] |
Is there an easy way to "append()" two dictionaries together in Python? | 2,586,273 | <p>If I have two dictionaries I'd like to combine in Python, i.e.</p>
<pre><code>a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
</code></pre>
<p>If I run update on them it reorders the list:</p>
<pre><code>a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
</code></pre>
<p>when what I really want is attach "b" to the end of "... | 2 | 2010-04-06T15:57:59Z | 2,586,285 | <p>Dictionaries are unordered: they do not have a beginning or an end. Whether you use <code>update</code> or the <code>for</code> loop, the end result will still be unordered.</p>
<p>If you need ordering, use a list of tuples instead. Python 3.1 has an <code>OrderedDict</code> type which will also be in Python 2.7.</... | 2 | 2010-04-06T15:59:45Z | [
"python",
"dictionary"
] |
Is there an easy way to "append()" two dictionaries together in Python? | 2,586,273 | <p>If I have two dictionaries I'd like to combine in Python, i.e.</p>
<pre><code>a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
</code></pre>
<p>If I run update on them it reorders the list:</p>
<pre><code>a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
</code></pre>
<p>when what I really want is attach "b" to the end of "... | 2 | 2010-04-06T15:57:59Z | 2,586,288 | <p>A python dictionary has no ordering -- if in practice items appear in a particular order, that's purely a side-effect of the particular implementation and shouldn't be relied on.</p>
| 11 | 2010-04-06T16:00:13Z | [
"python",
"dictionary"
] |
Is there an easy way to "append()" two dictionaries together in Python? | 2,586,273 | <p>If I have two dictionaries I'd like to combine in Python, i.e.</p>
<pre><code>a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
</code></pre>
<p>If I run update on them it reorders the list:</p>
<pre><code>a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
</code></pre>
<p>when what I really want is attach "b" to the end of "... | 2 | 2010-04-06T15:57:59Z | 2,586,291 | <p>Dictionaries are not ordered. If you need ordering, you might try something like a list.</p>
| 2 | 2010-04-06T16:00:31Z | [
"python",
"dictionary"
] |
Is there an easy way to "append()" two dictionaries together in Python? | 2,586,273 | <p>If I have two dictionaries I'd like to combine in Python, i.e.</p>
<pre><code>a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
</code></pre>
<p>If I run update on them it reorders the list:</p>
<pre><code>a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
</code></pre>
<p>when what I really want is attach "b" to the end of "... | 2 | 2010-04-06T15:57:59Z | 2,586,300 | <p>I think you're misunderstanding the use of dictionaries. You do not access them sequence or location. You access them from the keys, which in your case also happen to be numbers. They are hash tables and are supposed to work like this. You're doing it correctly.</p>
| 0 | 2010-04-06T16:02:14Z | [
"python",
"dictionary"
] |
Is there an easy way to "append()" two dictionaries together in Python? | 2,586,273 | <p>If I have two dictionaries I'd like to combine in Python, i.e.</p>
<pre><code>a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
</code></pre>
<p>If I run update on them it reorders the list:</p>
<pre><code>a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
</code></pre>
<p>when what I really want is attach "b" to the end of "... | 2 | 2010-04-06T15:57:59Z | 2,586,353 | <p>There is an ordered dictionary in the standard library <a href="http://www.python.org/download/releases/3.1.1/" rel="nofollow">as of Python 3.1.1</a>, but the use-cases for it are limited. Typically if you are using a dictionary, you are simply interested in the mapping of keys to values, and not order. There are ... | 1 | 2010-04-06T16:08:38Z | [
"python",
"dictionary"
] |
Return list of words from a list of lines with regexp | 2,586,326 | <p>I'm running the following code on a list of strings to return a list of its words:</p>
<pre><code>words = [re.split('\\s+', line) for line in lines]
</code></pre>
<p>However, I end up getting something like:</p>
<pre><code>[['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...]
</code></pre>
... | 0 | 2010-04-06T16:05:05Z | 2,586,399 | <p>You can always do this:</p>
<pre><code>words = []
for line in lines:
words.extend(re.split('\\s+',line))
</code></pre>
<p>It's not nearly as elegant as a one-liner list comprehension, but it gets the job done.</p>
| 0 | 2010-04-06T16:14:00Z | [
"python",
"regex",
"python-3.x",
"list-comprehension"
] |
Return list of words from a list of lines with regexp | 2,586,326 | <p>I'm running the following code on a list of strings to return a list of its words:</p>
<pre><code>words = [re.split('\\s+', line) for line in lines]
</code></pre>
<p>However, I end up getting something like:</p>
<pre><code>[['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...]
</code></pre>
... | 0 | 2010-04-06T16:05:05Z | 2,586,443 | <p>The reason why you get a list of lists is because re.split() returns a list which then in 'appended' to the list comprehension output.</p>
<p>It's unclear why you are using that (or probably just a bad example) but if you can get the full content (all lines) as a string you can just do</p>
<pre><code>words = re.sp... | 1 | 2010-04-06T16:19:27Z | [
"python",
"regex",
"python-3.x",
"list-comprehension"
] |
Return list of words from a list of lines with regexp | 2,586,326 | <p>I'm running the following code on a list of strings to return a list of its words:</p>
<pre><code>words = [re.split('\\s+', line) for line in lines]
</code></pre>
<p>However, I end up getting something like:</p>
<pre><code>[['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...]
</code></pre>
... | 0 | 2010-04-06T16:05:05Z | 2,586,509 | <pre><code>>>> import re
>>> from itertools import chain
>>> lines = ["hello world", "second line", "third line"]
>>> words = chain(*[re.split(r'\s+', line) for line in lines])
</code></pre>
<p>This will give you an iterator that can be used for looping through all words:</p>
<pre>... | 4 | 2010-04-06T16:26:39Z | [
"python",
"regex",
"python-3.x",
"list-comprehension"
] |
Return list of words from a list of lines with regexp | 2,586,326 | <p>I'm running the following code on a list of strings to return a list of its words:</p>
<pre><code>words = [re.split('\\s+', line) for line in lines]
</code></pre>
<p>However, I end up getting something like:</p>
<pre><code>[['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...]
</code></pre>
... | 0 | 2010-04-06T16:05:05Z | 6,988,895 | <p>Just stumbled across this old question, and I think I have a better solution. Normally if you want to nest a list comprehension ("append" each list), you think backwards (un-for-loop-like). This is not what you want:</p>
<pre><code>>>> import re
>>> lines = ["hello world", "second line", "third li... | 0 | 2011-08-08T21:33:00Z | [
"python",
"regex",
"python-3.x",
"list-comprehension"
] |
What is the most platform- and Python-version-independent way to make a fast loop for use in Python? | 2,586,749 | <p>I'm writing a scientific application in Python with a very processor-intensive loop at its core. I would like to optimise this as far as possible, at minimum inconvenience to end users, who will probably use it as an uncompiled collection of Python scripts, and will be using Windows, Mac, and (mainly Ubuntu) Linux.<... | 6 | 2010-04-06T17:05:13Z | 2,586,808 | <p>Python isn't intended for high-performance computation. Write the core loop in C and call it from Python.</p>
| 1 | 2010-04-06T17:12:32Z | [
"python",
"optimization"
] |
What is the most platform- and Python-version-independent way to make a fast loop for use in Python? | 2,586,749 | <p>I'm writing a scientific application in Python with a very processor-intensive loop at its core. I would like to optimise this as far as possible, at minimum inconvenience to end users, who will probably use it as an uncompiled collection of Python scripts, and will be using Windows, Mac, and (mainly Ubuntu) Linux.<... | 6 | 2010-04-06T17:05:13Z | 2,586,848 | <p>Numpy does use some native optimization for array processing. You can use Numpy array with <a href="http://docs.cython.org/src/tutorial/numpy.html" rel="nofollow">Cython</a> to gain some speed-ups.</p>
| 4 | 2010-04-06T17:19:40Z | [
"python",
"optimization"
] |
What is the most platform- and Python-version-independent way to make a fast loop for use in Python? | 2,586,749 | <p>I'm writing a scientific application in Python with a very processor-intensive loop at its core. I would like to optimise this as far as possible, at minimum inconvenience to end users, who will probably use it as an uncompiled collection of Python scripts, and will be using Windows, Mac, and (mainly Ubuntu) Linux.<... | 6 | 2010-04-06T17:05:13Z | 2,587,039 | <p>Your python code could probably be speeded up a bit by replacing your loop with a generator expression and removing all the lookups of mom_i[i], relative[i] and r_unit[i] by iterating through all three sequences in parallel using itertools.izip.</p>
<p>i.e. replace</p>
<pre><code>B = zeros(3,float)
for i in range... | 3 | 2010-04-06T17:46:52Z | [
"python",
"optimization"
] |
What is the most platform- and Python-version-independent way to make a fast loop for use in Python? | 2,586,749 | <p>I'm writing a scientific application in Python with a very processor-intensive loop at its core. I would like to optimise this as far as possible, at minimum inconvenience to end users, who will probably use it as an uncompiled collection of Python scripts, and will be using Windows, Mac, and (mainly Ubuntu) Linux.<... | 6 | 2010-04-06T17:05:13Z | 2,587,886 | <p>One simple, but significant speed-up is to take the multiplication by A outside of your sum. You can just times the B with it as you return it:</p>
<pre><code>for i in range(len(relative)):
#work out the dipole field and add it to the estimate so far
B += (3*dot(mom_i[i],r_unit[i])*r_unit[i] - mom_i[i]) / s... | 2 | 2010-04-06T20:04:47Z | [
"python",
"optimization"
] |
What is the most platform- and Python-version-independent way to make a fast loop for use in Python? | 2,586,749 | <p>I'm writing a scientific application in Python with a very processor-intensive loop at its core. I would like to optimise this as far as possible, at minimum inconvenience to end users, who will probably use it as an uncompiled collection of Python scripts, and will be using Windows, Mac, and (mainly Ubuntu) Linux.<... | 6 | 2010-04-06T17:05:13Z | 2,589,031 | <p>You can get this to run much, much faster if you eliminate the loop and use Numpy's vectorized operations. Put your data in numpy arrays of shape (3,N) and try the following:</p>
<pre><code>import numpy as np
N = 20000
mu = np.random.random((3,1))
r_i = np.random.random((3,N))
mom_i = np.random.random((3,N))
def ... | 10 | 2010-04-06T23:25:53Z | [
"python",
"optimization"
] |
How to match a variable list of items separated by commas | 2,586,849 | <p>I want to turn something like this</p>
<pre><code>CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only
</code></pre>
<p>into</p>
<pre><code>('CS 240', 'CS 246', 'ECE 222', 'ECE 220')
</code></pre>
<p>in Python, code that matches a single course looks like</p>
<pre><code>>>> r... | 0 | 2010-04-06T17:19:50Z | 2,586,909 | <pre><code>>>> a="CS 240, CS 246, ECE 222"
>>> b=tuple(a.strip() for a in a.split(','))
>>> b
('CS 240', 'CS 246', 'ECE 222')
>>>
</code></pre>
| 5 | 2010-04-06T17:26:22Z | [
"python",
"regex",
"pattern-matching"
] |
How to match a variable list of items separated by commas | 2,586,849 | <p>I want to turn something like this</p>
<pre><code>CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only
</code></pre>
<p>into</p>
<pre><code>('CS 240', 'CS 246', 'ECE 222', 'ECE 220')
</code></pre>
<p>in Python, code that matches a single course looks like</p>
<pre><code>>>> r... | 0 | 2010-04-06T17:19:50Z | 2,587,021 | <p>This method uses regular expressions and matches your input:</p>
<pre><code>>>> import re
>>> re.findall("\w+\s\d+", "CS 240, CS 246, ECE 222")
['CS 240', 'CS 246', 'ECE 222']
</code></pre>
<p>It does not look for the comma. Instead it looks for anything <em>but</em> the comma: it first matches m... | 0 | 2010-04-06T17:44:08Z | [
"python",
"regex",
"pattern-matching"
] |
How to match a variable list of items separated by commas | 2,586,849 | <p>I want to turn something like this</p>
<pre><code>CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only
</code></pre>
<p>into</p>
<pre><code>('CS 240', 'CS 246', 'ECE 222', 'ECE 220')
</code></pre>
<p>in Python, code that matches a single course looks like</p>
<pre><code>>>> r... | 0 | 2010-04-06T17:19:50Z | 2,587,143 | <p>Isn't the <code>csv</code> standard library module ( <a href="http://docs.python.org/library/csv.html" rel="nofollow">http://docs.python.org/library/csv.html</a> ) what you are looking for?</p>
| 3 | 2010-04-06T18:02:12Z | [
"python",
"regex",
"pattern-matching"
] |
Python analyze method calls from other classes/modules | 2,586,959 | <p>I've got a Codebase of around 5,3k LOC with around 30 different classe. The code is already very well formatted and I want to improve it further by prefixing methods that are only called in the module that were defined in with a "_", in order to indicate that. Yes it would have been a good idea to do that from the b... | 1 | 2010-04-06T17:34:06Z | 2,586,977 | <p>For me, this sounds like special case of coverage.</p>
<p>Thus I'd take a look at coverage.py or figleaf and modify it to ignore inter-module calls. </p>
| 0 | 2010-04-06T17:38:33Z | [
"python",
"methods",
"code-analysis"
] |
float change from python 3.0.1 to 3.1.2 | 2,587,010 | <p>I'm trying to learn python. I am using 3.1.2 and the o'reilly book is using 3.0.1
here is my code:</p>
<pre><code>import urllib.request
price = (99.99)
while price > 4.74:
page = urllib.request.urlopen ("http://www.beans-r-us.biz/prices-loyalty.html")
text = page.read().decode("utf8")
where = text... | 0 | 2010-04-06T17:42:18Z | 2,587,029 | <p>The problem is that you have extra characters at the end of your float, probably because the content of the page changed since the code was written (the number appears to change every fifteen minute). You could try changing the following line to make the code slightly more robust:</p>
<pre><code>end_of_price = text... | 5 | 2010-04-06T17:45:53Z | [
"python"
] |
float change from python 3.0.1 to 3.1.2 | 2,587,010 | <p>I'm trying to learn python. I am using 3.1.2 and the o'reilly book is using 3.0.1
here is my code:</p>
<pre><code>import urllib.request
price = (99.99)
while price > 4.74:
page = urllib.request.urlopen ("http://www.beans-r-us.biz/prices-loyalty.html")
text = page.read().decode("utf8")
where = text... | 0 | 2010-04-06T17:42:18Z | 2,587,047 | <p>You are calculating <code>end_of_price</code> as <code>start_of_price + 6</code>. Actually your price value seems to be only 4 characters long, so that you also include the two characters following in the string you want to convert to a <code>float</code>. Python then complains that <code>4.59</</code> is not a n... | 1 | 2010-04-06T17:47:56Z | [
"python"
] |
Exit to command line in Python | 2,587,083 | <p>I have a script that I want to exit early under some condition:</p>
<pre><code>if not "id" in dir():
print "id not set, cannot continue"
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
[ more code ]
</code></pre>
<p>I run this script using <code>execfile("foo.py")</cod... | 2 | 2010-04-06T17:51:29Z | 2,587,107 | <p>Put your code block in a method and return from that method, like such:</p>
<pre><code>def do_the_thing():
if not "id" in dir():
print "id not set, cannot continue"
return
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
# [ more code ]
... | 3 | 2010-04-06T17:56:12Z | [
"python",
"command-line",
"scripting",
"exit",
"execfile"
] |
Exit to command line in Python | 2,587,083 | <p>I have a script that I want to exit early under some condition:</p>
<pre><code>if not "id" in dir():
print "id not set, cannot continue"
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
[ more code ]
</code></pre>
<p>I run this script using <code>execfile("foo.py")</cod... | 2 | 2010-04-06T17:51:29Z | 2,587,114 | <p>In the interactive interpreter; catch <code>SystemExit</code> raised by <code>sys.exit</code> and ignore it:</p>
<pre><code>try:
execfile("mymodule.py")
except SystemExit:
pass
</code></pre>
| 7 | 2010-04-06T17:56:59Z | [
"python",
"command-line",
"scripting",
"exit",
"execfile"
] |
Exit to command line in Python | 2,587,083 | <p>I have a script that I want to exit early under some condition:</p>
<pre><code>if not "id" in dir():
print "id not set, cannot continue"
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
[ more code ]
</code></pre>
<p>I run this script using <code>execfile("foo.py")</cod... | 2 | 2010-04-06T17:51:29Z | 2,587,862 | <p>Instead of using execfile, you should make the script importable (<strong>name</strong>=='<strong>main</strong> protection, seperated into functions, etc.), and then call the functions from the interpreter. </p>
| 0 | 2010-04-06T20:00:44Z | [
"python",
"command-line",
"scripting",
"exit",
"execfile"
] |
Exit to command line in Python | 2,587,083 | <p>I have a script that I want to exit early under some condition:</p>
<pre><code>if not "id" in dir():
print "id not set, cannot continue"
# exit here!
# otherwise continue with the rest of the script...
print "alright..."
[ more code ]
</code></pre>
<p>I run this script using <code>execfile("foo.py")</cod... | 2 | 2010-04-06T17:51:29Z | 2,594,508 | <p>I'm a little obsessed with ipython for interactive work but check out the tutorial on <a href="http://ipython.scipy.org/doc/rel-0.10/html/interactive/reference.html#embedding-ipython" rel="nofollow">shell embedding</a> for a more robust solution than <a href="http://stackoverflow.com/a/2587114">this one</a> (which ... | 2 | 2010-04-07T17:19:34Z | [
"python",
"command-line",
"scripting",
"exit",
"execfile"
] |
Serialize the @property methods in a Python class | 2,587,119 | <p>Is there a way to have any @property definitions passed through to a json serializer when serializing a Django model class?</p>
<p>example:</p>
<pre><code>class FooBar(object.Model)
name = models.CharField(...)
@property
def foo(self):
return "My name is %s" %self.name
</code></pre>
<p>Want ... | 15 | 2010-04-06T17:57:45Z | 2,587,616 | <p>You can get all of the properties of a class using some black magic:</p>
<pre><code>def list_class_properties(cls):
return [k for k,v in cls.__dict__.iteritems() if type(v) is property]
</code></pre>
<p>For example:</p>
<pre><code>>>> class Foo:
@property
def bar(self):
retur... | -1 | 2010-04-06T19:20:48Z | [
"python",
"django",
"serialization"
] |
Serialize the @property methods in a Python class | 2,587,119 | <p>Is there a way to have any @property definitions passed through to a json serializer when serializing a Django model class?</p>
<p>example:</p>
<pre><code>class FooBar(object.Model)
name = models.CharField(...)
@property
def foo(self):
return "My name is %s" %self.name
</code></pre>
<p>Want ... | 15 | 2010-04-06T17:57:45Z | 2,607,242 | <p>You can extend Django's serializers without /too/ much work. Here's a custom serializer that takes a queryset and a list of attributes (fields or not), and returns JSON.</p>
<pre><code>from StringIO import StringIO
from django.core.serializers.json import Serializer
class MySerializer(Serializer):
def seriali... | 11 | 2010-04-09T12:14:10Z | [
"python",
"django",
"serialization"
] |
Serialize the @property methods in a Python class | 2,587,119 | <p>Is there a way to have any @property definitions passed through to a json serializer when serializing a Django model class?</p>
<p>example:</p>
<pre><code>class FooBar(object.Model)
name = models.CharField(...)
@property
def foo(self):
return "My name is %s" %self.name
</code></pre>
<p>Want ... | 15 | 2010-04-06T17:57:45Z | 30,215,745 | <p>Things have changed a bit since 2010, so the answer of @user85461 seems to no longer be working with Django 1.8 and Python 3.4. This is an updated answer with what seems to work for me.</p>
<pre><code>from django.core.serializers.base import Serializer as BaseSerializer
from django.core.serializers.python import Se... | 5 | 2015-05-13T13:06:28Z | [
"python",
"django",
"serialization"
] |
Serialize the @property methods in a Python class | 2,587,119 | <p>Is there a way to have any @property definitions passed through to a json serializer when serializing a Django model class?</p>
<p>example:</p>
<pre><code>class FooBar(object.Model)
name = models.CharField(...)
@property
def foo(self):
return "My name is %s" %self.name
</code></pre>
<p>Want ... | 15 | 2010-04-06T17:57:45Z | 38,253,327 | <p>The solution worked well that is proposed by M. Rafay Aleem and Wtower, but it's duplicated lot of code. Here is an improvment:</p>
<pre><code>from django.core.serializers.base import Serializer as BaseSerializer
from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.... | 0 | 2016-07-07T18:59:03Z | [
"python",
"django",
"serialization"
] |
Configuration problems with django and mod_wsgi | 2,587,251 | <p>I've got problems on getting django to work on apache 2.2 with mod_wsgi.
Django is installed and mod_wsgi too.
I can even see a 404 page when accessing the path and I can login to django admin.
But if I want to install the tagging module I get the following error:</p>
<pre><code> Traceback (most recent call last):
... | 5 | 2010-04-06T18:18:00Z | 2,587,494 | <p>Have you read Graham Dumpleton's <a href="http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html" rel="nofollow">blog post on Django and WSGI</a>? It does a pretty good job of describing some common configuration problems, and specifically touches on <code>mysite.settings</code> vs. <code>settings</... | 4 | 2010-04-06T18:59:58Z | [
"python",
"django",
"apache",
"mod-wsgi"
] |
Configuration problems with django and mod_wsgi | 2,587,251 | <p>I've got problems on getting django to work on apache 2.2 with mod_wsgi.
Django is installed and mod_wsgi too.
I can even see a 404 page when accessing the path and I can login to django admin.
But if I want to install the tagging module I get the following error:</p>
<pre><code> Traceback (most recent call last):
... | 5 | 2010-04-06T18:18:00Z | 2,590,818 | <p>First,</p>
<ul>
<li>Since your admin works, the setting with wsgi is good. Don't bother changing/editing it.</li>
</ul>
<p>To ensure that it is not a Apache/mod-wsgi setting problem, you can run the development server from the production machine</p>
<pre><code>python manage.py runserver 0:8080
</code></pre>
<p>T... | 4 | 2010-04-07T07:58:06Z | [
"python",
"django",
"apache",
"mod-wsgi"
] |
Python: Picking an element without replacement | 2,587,387 | <p>I would like to slice random letters from a string. </p>
<p>Given
s="howdy"</p>
<p>I would like to pick elements from 's' without replacement but keep the index number.</p>
<p>For example</p>
<pre><code>>>> random.sample(s,len(s))
['w', 'h', 'o', 'd', 'y']
</code></pre>
<p>is close to what I want, but ... | 6 | 2010-04-06T18:42:21Z | 2,587,414 | <pre><code>>>> random.sample(list(enumerate(a)), 5)
[(1, 'o'), (0, 'h'), (3, 'd'), (2, 'w'), (4, 'y')]
</code></pre>
| 15 | 2010-04-06T18:46:13Z | [
"python"
] |
Python: Picking an element without replacement | 2,587,387 | <p>I would like to slice random letters from a string. </p>
<p>Given
s="howdy"</p>
<p>I would like to pick elements from 's' without replacement but keep the index number.</p>
<p>For example</p>
<pre><code>>>> random.sample(s,len(s))
['w', 'h', 'o', 'd', 'y']
</code></pre>
<p>is close to what I want, but ... | 6 | 2010-04-06T18:42:21Z | 2,587,415 | <p>You could just enumerate the list before sampling:</p>
<pre><code>>>> random.sample(list(enumerate(l)), 5)
[(1, 'o'), (2, 'w'), (0, 'h'), (3, 'd'), (4, 'y')]
</code></pre>
| 7 | 2010-04-06T18:46:20Z | [
"python"
] |
Python: Picking an element without replacement | 2,587,387 | <p>I would like to slice random letters from a string. </p>
<p>Given
s="howdy"</p>
<p>I would like to pick elements from 's' without replacement but keep the index number.</p>
<p>For example</p>
<pre><code>>>> random.sample(s,len(s))
['w', 'h', 'o', 'd', 'y']
</code></pre>
<p>is close to what I want, but ... | 6 | 2010-04-06T18:42:21Z | 2,587,420 | <p>It's probably easier to do something like this:</p>
<pre><code>def sample_with_indices(s):
indices = range(len(s))
random.shuffle(indices)
return [(s[i], i) for i in indices]
</code></pre>
<p>This will basically shuffle all the indices for a string and then just return the character at that index. Goi... | 2 | 2010-04-06T18:47:56Z | [
"python"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 2,587,419 | <p>When you pass a <code>lambda</code> to <code>sort</code>, you need to return an integer, not a boolean. So your code should instead read as follows:</p>
<pre><code>xs.sort(lambda x,y: cmp(len(x), len(y)))
</code></pre>
<p>Note that <a href="http://docs.python.org/library/functions.html#cmp">cmp</a> is a builtin f... | 83 | 2010-04-06T18:47:43Z | [
"python",
"list",
"sorting"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 2,587,513 | <p>The same as in Eli's answer - just using a shorter form, because you can skip a <code>lambda</code> part here. </p>
<p>Creating new list:</p>
<pre><code>>>> xs = ['dddd','a','bb','ccc']
>>> sorted(xs, key=len)
['a', 'bb', 'ccc', 'dddd']
</code></pre>
<p>In-place sorting:</p>
<pre><code>>>... | 43 | 2010-04-06T19:03:29Z | [
"python",
"list",
"sorting"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 20,162,501 | <p>I Would like to add how the pythonic key function works while sorting :</p>
<p><strong>Decorate-Sort-Undecorate Design Pattern :</strong></p>
<p>Pythonâs support for a key function when sorting is implemented using what is known as the
decorate-sort-undecorate design pattern. </p>
<p><strong>It proceeds in 3 s... | 2 | 2013-11-23T12:50:10Z | [
"python",
"list",
"sorting"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 31,800,325 | <p>Write a function lensort to sort a list of strings based on length. </p>
<pre><code>def lensort(a):
n = len(a)
for i in range(n):
for j in range(i+1,n):
if len(a[i]) > len(a[j]):
temp = a[i]
a[i] = a[j]
a[j] = temp
return a
print len... | 0 | 2015-08-04T03:46:18Z | [
"python",
"list",
"sorting"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 32,422,087 | <pre><code>def lensort(list_1):
list_2=[];list_3=[]
for i in list_1:
list_2.append([i,len(i)])
list_2.sort(key = lambda x : x[1])
for i in list_2:
list_3.append(i[0])
return list_3
</code></pre>
<p>This works for me!</p>
| 0 | 2015-09-06T09:39:17Z | [
"python",
"list",
"sorting"
] |
Sorting Python list based on the length of the string | 2,587,402 | <p>I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.</p>
<pre><code>xs = ['dddd','a','bb','ccc']
print xs
xs.sort(lambda x,y: len(x) < len(y))
print xs
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
</code></pre>
<p>W... | 40 | 2010-04-06T18:45:00Z | 36,335,468 | <p><strong>The easiest way to do this is:</strong></p>
<blockquote>
<p>list.sort(key = lambda x:len(x))</p>
</blockquote>
| 0 | 2016-03-31T13:53:22Z | [
"python",
"list",
"sorting"
] |
Emulating a web browser | 2,587,423 | <p>we are tasked with basically emulating a browser to fetch webpages, looking to automate tests on different web pages. This will be used for (ideally) console-ish applications that run in the background and generate reports.</p>
<p>We tried going with .NET and the WatiN library, but it was built on a Marshalled IE,... | 5 | 2010-04-06T18:49:07Z | 2,587,472 | <p>I know you mentioned you don't like Ruby syntax (neither do I), but I just have to chime in and say that <a href="http://watir.com/" rel="nofollow">Watir</a> is probably the best thing out there for what you are trying to do.</p>
<p><strong>EDIT:</strong> There appears to be a Java counter-part called <a href="htt... | 1 | 2010-04-06T18:56:47Z | [
"c#",
".net",
"c++",
"python",
"qt"
] |
Emulating a web browser | 2,587,423 | <p>we are tasked with basically emulating a browser to fetch webpages, looking to automate tests on different web pages. This will be used for (ideally) console-ish applications that run in the background and generate reports.</p>
<p>We tried going with .NET and the WatiN library, but it was built on a Marshalled IE,... | 5 | 2010-04-06T18:49:07Z | 2,587,720 | <p>You might try one of these:</p>
<p><a href="http://code.google.com/p/spynner/" rel="nofollow">http://code.google.com/p/spynner/</a></p>
<p><a href="http://code.google.com/p/pywebkitgtk/" rel="nofollow">http://code.google.com/p/pywebkitgtk/</a></p>
| 3 | 2010-04-06T19:37:39Z | [
"c#",
".net",
"c++",
"python",
"qt"
] |
Emulating a web browser | 2,587,423 | <p>we are tasked with basically emulating a browser to fetch webpages, looking to automate tests on different web pages. This will be used for (ideally) console-ish applications that run in the background and generate reports.</p>
<p>We tried going with .NET and the WatiN library, but it was built on a Marshalled IE,... | 5 | 2010-04-06T18:49:07Z | 2,587,769 | <p>I've only been digging into this recently myself, so I couldn't say that this does everything you've listed, but check out <a href="http://geckofx.org/" rel="nofollow">GeckoFx</a>. </p>
<p>From the site: <strong>GeckoFX is an open-source component</strong> which makes it easy to embed Mozilla Gecko (Firefox) into ... | 1 | 2010-04-06T19:45:05Z | [
"c#",
".net",
"c++",
"python",
"qt"
] |
Working with bytes and binary data in Python | 2,587,438 | <p>Four consecutive bytes in a byte string together specify some value. However, only 7 bits in each byte are used; the most significant bit is always zero and therefore its ignored (that makes 28 bits altogether). So...</p>
<pre><code>b"\x00\x00\x02\x01"
</code></pre>
<p>would be <code>000 0000</code> <code>000 0000... | 3 | 2010-04-06T18:51:00Z | 2,587,469 | <p>Use bitshifting and addition:</p>
<pre><code>bytes = b"\x00\x00\x02\x01"
i = 0
for b in bytes:
i <<= 7
i += b # Or use (b & 0x7f) if the last bit might not be zero.
print(i)
</code></pre>
<p>Result:</p>
<pre><code>257
</code></pre>
| 8 | 2010-04-06T18:55:55Z | [
"python",
"binary",
"byte"
] |
Working with bytes and binary data in Python | 2,587,438 | <p>Four consecutive bytes in a byte string together specify some value. However, only 7 bits in each byte are used; the most significant bit is always zero and therefore its ignored (that makes 28 bits altogether). So...</p>
<pre><code>b"\x00\x00\x02\x01"
</code></pre>
<p>would be <code>000 0000</code> <code>000 0000... | 3 | 2010-04-06T18:51:00Z | 12,865,857 | <p>Using the <a href="https://pypi.python.org/pypi/bitarray" rel="nofollow">bitarray module</a>, you can do it a lot quicker for big numbers:</p>
<p>Benchmarks (factor 2.4x speedup!):</p>
<pre><code>janus@Zeus /tmp % python3 -m timeit -s "import tst" "tst.tst(10000)"
10 loops, best of 3: 251 msec per loop
janus@Zeus... | 1 | 2012-10-12T19:32:03Z | [
"python",
"binary",
"byte"
] |
How to write outline data into .otf files? | 2,587,530 | <p>I need to edit or completely replace outline data (bezier curves) of OpenType fonts. the input data is an EPS file that i have to write it into one specified glyph of an otf file with a certain scaling. (The glyph is specified by PostScript name OR Unicode value.)</p>
<p>I need something like an encoder (or just a ... | 0 | 2010-04-06T19:05:59Z | 2,587,627 | <p>Try the <a href="http://fontforge.sourceforge.net/python.html" rel="nofollow">FontForge</a> python extensions.</p>
| 2 | 2010-04-06T19:22:13Z | [
"python",
"postscript",
"bezier",
"opentype"
] |
Python Ephem / Datetime calculation | 2,587,640 | <p>the output should process the first date as "day" and second as "night". I've been playing with this for a few hours now and can't figure out what I'm doing wrong. Any ideas?</p>
<p><strong>Edit</strong>
I assume that the problem is due to my date comparison implementation </p>
<p><strong>Output:</strong></p>
<pr... | 3 | 2010-04-06T19:23:36Z | 2,587,794 | <p>o.date will always be between o.previous_settings and o.next_rising ;), so you can check it this way:</p>
<pre><code>if o.previous_rising(ephem.Sun()) > o.previous_setting(ephem.Sun()):
return "day"
elif:
return "night"
</code></pre>
| 7 | 2010-04-06T19:49:14Z | [
"python",
"datetime",
"astronomy"
] |
Automatically execute commands on launching python shell | 2,587,709 | <p>I was wondering if there is a way to automatically run commands on entering the python shell as you would with the .bash_profile or .profile scripts with bash. I would like to automatically import some modules so I don't have to type the whole shebang everytime I hop into the shell.</p>
<p>Thanks, </p>
| 7 | 2010-04-06T19:36:09Z | 2,587,733 | <p>Yup you can use the <code>PYTHONSTARTUP</code> environment variable to do this as outlined <a href="http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file" rel="nofollow">here</a></p>
| 12 | 2010-04-06T19:39:01Z | [
"python",
"shell"
] |
Automatically execute commands on launching python shell | 2,587,709 | <p>I was wondering if there is a way to automatically run commands on entering the python shell as you would with the .bash_profile or .profile scripts with bash. I would like to automatically import some modules so I don't have to type the whole shebang everytime I hop into the shell.</p>
<p>Thanks, </p>
| 7 | 2010-04-06T19:36:09Z | 2,594,456 | <p>Also consider using <a href="http://ipython.scipy.org/" rel="nofollow">ipython</a> if you're doing a lot of interactive work. Your options for this kind of automation expand significantly.</p>
| 1 | 2010-04-07T17:09:33Z | [
"python",
"shell"
] |
How to validate a bunch of proxies against a URL? | 2,588,093 | <p>I have a list of 100 proxies. The URL I am interested in is abc.com. I want to check the number of proxies which can successfully fetch this URL and the time taken for the same. I am hoping I made sense. I am a Python noob. I am looking for a code snippet. A helping hand is really appreciated :)</p>
<pre><code>Prox... | 0 | 2010-04-06T20:34:12Z | 2,588,283 | <p>You can fetch URLs using <a href="http://docs.python.org/library/urllib2.html" rel="nofollow">urllib2</a>. To get the amount of time taken, you can use the time module. Here's a simple example that does what you seem to want:</p>
<pre><code>import urllib2
import time
def testProxies(url, proxies):
# prepare t... | 4 | 2010-04-06T20:59:47Z | [
"python",
"proxy",
"curl"
] |
A Combinations of Items in Given List | 2,588,247 | <p>I'm currently in Python land. This is what I need to do. I have already looked into the itertools library but it seems to only do permutations.</p>
<p>I want to take an input list, like ['yahoo', 'wikipedia', 'freebase'] and generate every unique combination of one item with zero or more other items... </p>
<pre><... | 2 | 2010-04-06T20:55:40Z | 2,588,281 | <p>That is called a power set. Just follow this <a href="http://en.wikipedia.org/wiki/Power_set#Algorithms" rel="nofollow">algorithm</a>. Here's a simple implementation:</p>
<pre><code>def powerset(seq):
if len(seq):
head = powerset(seq[:-1])
return head + [item + [seq[-1]] for item in head]
else:
retu... | 0 | 2010-04-06T20:59:33Z | [
"python",
"permutation",
"combinations"
] |
A Combinations of Items in Given List | 2,588,247 | <p>I'm currently in Python land. This is what I need to do. I have already looked into the itertools library but it seems to only do permutations.</p>
<p>I want to take an input list, like ['yahoo', 'wikipedia', 'freebase'] and generate every unique combination of one item with zero or more other items... </p>
<pre><... | 2 | 2010-04-06T20:55:40Z | 2,588,294 | <pre><code>>>> l = ['yahoo', 'wikipedia', 'freebase']
>>> import itertools
>>> for i in range(1, len(l) +1):
print(list(itertools.combinations(l, r=i)))
[('yahoo',), ('wikipedia',), ('freebase',)]
[('yahoo', 'wikipedia'), ('yahoo', 'freebase'), ('wikipedia', 'freebase')]
[('yahoo', 'wik... | 3 | 2010-04-06T21:00:40Z | [
"python",
"permutation",
"combinations"
] |
A Combinations of Items in Given List | 2,588,247 | <p>I'm currently in Python land. This is what I need to do. I have already looked into the itertools library but it seems to only do permutations.</p>
<p>I want to take an input list, like ['yahoo', 'wikipedia', 'freebase'] and generate every unique combination of one item with zero or more other items... </p>
<pre><... | 2 | 2010-04-06T20:55:40Z | 2,588,296 | <p>You're basically counting from 1 to 2<sup>n</sup>-1 <em>in binary</em>:</p>
<pre><code>0 0 1 ['freebase']
0 1 0 ['wikipedia']
0 1 1 ['wikipedia', 'freebase']
1 0 0 ['yahoo']
1 0 1 ['yahoo', 'freebase']
1 1 0 ['yahoo', 'wikipedia']
1 1 1 ['yahoo', 'wikipedia', 'freebase']
</code></pre>
| 0 | 2010-04-06T21:00:43Z | [
"python",
"permutation",
"combinations"
] |
A Combinations of Items in Given List | 2,588,247 | <p>I'm currently in Python land. This is what I need to do. I have already looked into the itertools library but it seems to only do permutations.</p>
<p>I want to take an input list, like ['yahoo', 'wikipedia', 'freebase'] and generate every unique combination of one item with zero or more other items... </p>
<pre><... | 2 | 2010-04-06T20:55:40Z | 2,588,307 | <p>It's called a powerset. This is an implementation from the <a href="http://docs.python.org/library/itertools.html" rel="nofollow">itertools docs</a>:</p>
<pre><code>def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterabl... | 3 | 2010-04-06T21:02:11Z | [
"python",
"permutation",
"combinations"
] |
Problem with literal arguments in the PATTERN string for a python 2to3 fixer | 2,588,286 | <p>I'm writing a fixer for the 2to3 tool in python.</p>
<p>In my pattern string, I have a section where I'd like to match an empty string as an argument, or an empty unicode string. The relevant chunk of my pattern looks like:</p>
<pre><code>(args='""' | args='u""')
</code></pre>
<p>My issue is the second option nev... | 0 | 2010-04-06T20:59:52Z | 2,609,322 | <p>Because 2to3 pattern matching is designed to match tokens not literals, there is no way to do this directly.</p>
<p>Instead you could match (args=STRING) and then determine the value of the string argument inside the transformation function and handle it appropriately.</p>
| 1 | 2010-04-09T16:55:28Z | [
"python",
"unicode",
"pattern-matching",
"special-characters",
"2to3"
] |
Passing a non-iterable to list.extend () | 2,588,312 | <p>I am creating a public method to allow callers to write values to a device, call it write_vals() for example.</p>
<p>Since these values will be typed live, I would like to simplify the user's life by allowing them type in either a list or a single value, depending on how many values they need to write. For example... | 3 | 2010-04-06T21:03:12Z | 2,588,362 | <p>You can check if the passed parameter is a list with the <a href="http://docs.python.org/library/functions.html?highlight=isinstance#isinstance"><code>isinstance()</code></a> function.</p>
<p>An even better solution could be to support a variable number of arguments:</p>
<pre><code>def write_to_device(*args):
#... | 8 | 2010-04-06T21:10:03Z | [
"python",
"list"
] |
Passing a non-iterable to list.extend () | 2,588,312 | <p>I am creating a public method to allow callers to write values to a device, call it write_vals() for example.</p>
<p>Since these values will be typed live, I would like to simplify the user's life by allowing them type in either a list or a single value, depending on how many values they need to write. For example... | 3 | 2010-04-06T21:03:12Z | 2,588,363 | <p>You can use <code>try...except...</code>:</p>
<pre><code>try:
input_list = list(input_val)
except TypeError:
input_list = list((input_val,))
</code></pre>
<p>This is pythonic.</p>
<p>P.S.: Variable parameters, as <a href="http://stackoverflow.com/questions/2588312/passing-a-non-iterable-to-list-extend/258... | 3 | 2010-04-06T21:10:23Z | [
"python",
"list"
] |
Passing a non-iterable to list.extend () | 2,588,312 | <p>I am creating a public method to allow callers to write values to a device, call it write_vals() for example.</p>
<p>Since these values will be typed live, I would like to simplify the user's life by allowing them type in either a list or a single value, depending on how many values they need to write. For example... | 3 | 2010-04-06T21:03:12Z | 2,588,367 | <p>Instead of checking the type, you could use try, except. Although, I'm curious to see how people better at python than I am would do this.</p>
<pre><code>input_list = []
try:
input_list.extend(input_val)
except TypeError:
input_list.append(input_val)
</code></pre>
<p>This seems incomplete to me from an er... | 3 | 2010-04-06T21:10:50Z | [
"python",
"list"
] |
Passing a non-iterable to list.extend () | 2,588,312 | <p>I am creating a public method to allow callers to write values to a device, call it write_vals() for example.</p>
<p>Since these values will be typed live, I would like to simplify the user's life by allowing them type in either a list or a single value, depending on how many values they need to write. For example... | 3 | 2010-04-06T21:03:12Z | 2,588,541 | <p>There are a few ways to do this, and which you choose depends on your needs.</p>
<p>You could try and convert the argument to a list, and assume if it doesn't work it should be interpreted as a single argument:</p>
<pre><code>def write_to_device(args):
try:
args = list(args)
except TypeError:
... | 2 | 2010-04-06T21:39:24Z | [
"python",
"list"
] |
My python auto-login script is broken | 2,588,345 | <p>A long time ago, I wrote a little python script to automatically log me on to the wireless network at my office. </p>
<p>Here is the code:</p>
<pre><code>#!/opt/local/bin/python
from urllib2 import urlopen
from ClientForm import ParseResponse
try:
if "Logged on as" in urlopen("https://MYWIRELESS.com/logon").read(... | 0 | 2010-04-06T21:07:17Z | 2,588,419 | <p>Also check that the package you installed is in python path:</p>
<pre><code>>>> import sys
>>> sys.path
</code></pre>
| 1 | 2010-04-06T21:19:34Z | [
"python",
"login",
"clientform"
] |
My python auto-login script is broken | 2,588,345 | <p>A long time ago, I wrote a little python script to automatically log me on to the wireless network at my office. </p>
<p>Here is the code:</p>
<pre><code>#!/opt/local/bin/python
from urllib2 import urlopen
from ClientForm import ParseResponse
try:
if "Logged on as" in urlopen("https://MYWIRELESS.com/logon").read(... | 0 | 2010-04-06T21:07:17Z | 10,715,639 | <p>This worked for something similar:</p>
<p>(Was able to get site to send data using wireshark. Also "user" might be something else e.g. "username" same with "password". Once again wireshark would help with this. Also could look at source of login page. Good luck!!!)</p>
<pre><code>from urllib import urlencode
from ... | 0 | 2012-05-23T07:50:00Z | [
"python",
"login",
"clientform"
] |
Python TEA implementation | 2,588,364 | <p>Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one I've found here: <a href="http://sysadminco.com/code/python-tea/" rel="nofollow">http://sysadminco.com/code/python-tea/</a> - but it does not seem to work properly.</p>
<p>It returns different results than other implement... | 1 | 2010-04-06T21:10:29Z | 2,588,508 | <p>Since <a href="http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm" rel="nofollow">TEA</a> is a block cipher and your <code>v</code> is a very small block, I'd guess there may be block padding differences, or as Wikipedia notes:
<a href="http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm" rel="nofollow">http:... | 1 | 2010-04-06T21:33:10Z | [
"python",
"c",
"encryption"
] |
Python TEA implementation | 2,588,364 | <p>Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one I've found here: <a href="http://sysadminco.com/code/python-tea/" rel="nofollow">http://sysadminco.com/code/python-tea/</a> - but it does not seem to work properly.</p>
<p>It returns different results than other implement... | 1 | 2010-04-06T21:10:29Z | 2,588,512 | <p>Tea is broken, do not use it. </p>
<p>XXTEA which is secure does not define endianess and stuff and you should should reinvent whell when you can use AES. </p>
<p>There is no point in using unsecure cryptography.</p>
<p>I strongy advice you to apply AES, it can be implemented in 8bit microcontolers whit just few ... | 0 | 2010-04-06T21:34:05Z | [
"python",
"c",
"encryption"
] |
Python TEA implementation | 2,588,364 | <p>Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one I've found here: <a href="http://sysadminco.com/code/python-tea/" rel="nofollow">http://sysadminco.com/code/python-tea/</a> - but it does not seem to work properly.</p>
<p>It returns different results than other implement... | 1 | 2010-04-06T21:10:29Z | 2,588,957 | <p>I fixed it. Here is working TEA implementation in python:</p>
<pre><code>from ctypes import *
def encipher(v, k):
y=c_uint32(v[0]);
z=c_uint32(v[1]);
sum=c_uint32(0);
delta=0x9E3779B9;
n=32
w=[0,0]
while(n>0):
sum.value += delta
y.value += ( z.value << 4 ) + k[... | 9 | 2010-04-06T23:10:33Z | [
"python",
"c",
"encryption"
] |
Deploying a PyQt application on Windows Vista x64 | 2,588,424 | <p>I'm working on an application for a client/friend using PyQt. I've been working on Linux and testing on Vista, but the target computer is Vista x64. Now, Python comes with compiled binaries of Python 2.6 for 64 bit Windows, but Riverbank don't provide 64 bit binaries for PyQt.</p>
<p>I don't have much access to the... | 1 | 2010-04-06T21:21:18Z | 2,588,460 | <p>You should be able to compile to the 32-bit (x86) and include 32-bit PyQt binaries and all will be well. 64-bit Windoze will run the project in a WOW64 process, and there shouldn't be compatibility issues.</p>
| 1 | 2010-04-06T21:26:00Z | [
"python",
"64bit",
"pyqt"
] |
Deploying a PyQt application on Windows Vista x64 | 2,588,424 | <p>I'm working on an application for a client/friend using PyQt. I've been working on Linux and testing on Vista, but the target computer is Vista x64. Now, Python comes with compiled binaries of Python 2.6 for 64 bit Windows, but Riverbank don't provide 64 bit binaries for PyQt.</p>
<p>I don't have much access to the... | 1 | 2010-04-06T21:21:18Z | 2,588,870 | <p>From the same link you posted, the guy made a binary for Python 2.6 </p>
<p><a href="http://www.ozgurfx.com/downloads/PyQt-Py2.6-gpl-4.5.4-1_amd64.exe" rel="nofollow">http://www.ozgurfx.com/downloads/PyQt-Py2.6-gpl-4.5.4-1_amd64.exe</a></p>
<p>Here's my source:
<a href="http://www.mail-archive.com/python_inside_ma... | 1 | 2010-04-06T22:46:23Z | [
"python",
"64bit",
"pyqt"
] |
PDF Form Field Manipulation | 2,588,622 | <p>I'm making a web interface to autofill pdf forms with user data from a database. The admin needs to be able to upload a pdf (right now targeted at IRS pdf forms) and then associate the fields in the pdf with data fields in the database.</p>
<p>I need a way to help the admin associate the field names (stuff like "t... | 8 | 2010-04-06T21:53:09Z | 2,590,346 | <p>There may be an easier solution than this, but you could definitely get the job done with ReportLab.</p>
<p>If you can save the current tax forms as an image, you could determine where each of the items need to be written and develop your code so that it automatically layers the appropriate values from the database... | 1 | 2010-04-07T06:09:35Z | [
"python",
"django",
"pdf"
] |
PDF Form Field Manipulation | 2,588,622 | <p>I'm making a web interface to autofill pdf forms with user data from a database. The admin needs to be able to upload a pdf (right now targeted at IRS pdf forms) and then associate the fields in the pdf with data fields in the database.</p>
<p>I need a way to help the admin associate the field names (stuff like "t... | 8 | 2010-04-06T21:53:09Z | 3,260,328 | <p>A good response using ReportLab and pyPDF can be found here: <a href="http://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python">http://stackoverflow.com/questions/1180115/add-text-to-existing-pdf-using-python</a></p>
| 0 | 2010-07-15T21:43:59Z | [
"python",
"django",
"pdf"
] |
PDF Form Field Manipulation | 2,588,622 | <p>I'm making a web interface to autofill pdf forms with user data from a database. The admin needs to be able to upload a pdf (right now targeted at IRS pdf forms) and then associate the fields in the pdf with data fields in the database.</p>
<p>I need a way to help the admin associate the field names (stuff like "t... | 8 | 2010-04-06T21:53:09Z | 10,624,674 | <p>I may be interpreting the question wrong but I have a lot of experience in pdf generation with python/django because of the site that I worked on for 5 months. I would suggest using texlive. Basically what I did was built a generic tex template for a document and then used django templating to insert the fields. I r... | 0 | 2012-05-16T18:42:49Z | [
"python",
"django",
"pdf"
] |
PDF Form Field Manipulation | 2,588,622 | <p>I'm making a web interface to autofill pdf forms with user data from a database. The admin needs to be able to upload a pdf (right now targeted at IRS pdf forms) and then associate the fields in the pdf with data fields in the database.</p>
<p>I need a way to help the admin associate the field names (stuff like "t... | 8 | 2010-04-06T21:53:09Z | 10,713,642 | <p>A postscript parser lives here: <a href="https://github.com/haxwithaxe/py-ps-parser" rel="nofollow">https://github.com/haxwithaxe/py-ps-parser</a></p>
<p>I've been interested in playing with it, but haven't yet. </p>
| 0 | 2012-05-23T04:53:43Z | [
"python",
"django",
"pdf"
] |
PDF Form Field Manipulation | 2,588,622 | <p>I'm making a web interface to autofill pdf forms with user data from a database. The admin needs to be able to upload a pdf (right now targeted at IRS pdf forms) and then associate the fields in the pdf with data fields in the database.</p>
<p>I need a way to help the admin associate the field names (stuff like "t... | 8 | 2010-04-06T21:53:09Z | 10,953,726 | <p>This may be way off your intended track; but, it might be worth a think. I've been working on parsing scanned structured documents into Django model instances. Using tesseract and unpaper to do the pre-processing and OCR, I get over 99% accuracy. That lets me parse the OCR output text with the <code>Levenshtein</cod... | 1 | 2012-06-08T17:54:54Z | [
"python",
"django",
"pdf"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,662 | <p>Right, but it marks the class as a <a href="http://www.python.org/doc/newstyle/" rel="nofollow">new-style</a> class. Newly developed classes should use the <code>object</code> base because it costs little and future-proofs your code.</p>
| 2 | 2010-04-06T22:00:31Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,667 | <p>In short, it sets free magical ponies.</p>
<p>In long, Python 2.2 and earlier used "old style classes". They were a particular implementation of classes, and they had a few limitations (for example, you couldn't subclass builtin types). The fix for this was to create a new style of class. But, doing this would invo... | 41 | 2010-04-06T22:01:39Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,669 | <p>It has to do with the "new-style" of classes. You can read more about it here: <a href="http://docs.python.org/tutorial/classes.html#multiple-inheritance" rel="nofollow">http://docs.python.org/tutorial/classes.html#multiple-inheritance</a> and also here: <a href="http://docs.python.org/reference/datamodel.html#new-... | 4 | 2010-04-06T22:02:32Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,672 | <p>The short version is that classic classes, which didn't need a superclass, had limitations that couldn't be worked around without breaking a lot of old code. So they created the concept of new-style classes which subclass from <code>object</code>, and now you can do cool things like define properties, and subclassi... | 0 | 2010-04-06T22:03:29Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,674 | <p><a href="http://docs.python.org/dev/whatsnew/2.2.html#old-and-new-classes" rel="nofollow">Python 2.2 introduced "new style classes"</a> which had a number of additional features relative to the old style classes which did not subclass object. Subclasses object was the chosen way to indicate that your class should b... | 0 | 2010-04-06T22:04:02Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
What is the purpose of subclassing the class "object" in Python? | 2,588,628 | <p>All the Python built-ins are subclasses of <code>object</code> and I come across many user-defined classes which are too. Why? What is the purpose of the class <code>object</code>? It's just an empty class, right?</p>
| 35 | 2010-04-06T21:54:50Z | 2,588,677 | <p>Short answer: subclassing <code>object</code> effectively makes it a <em>new-style</em> class (note that this is unnecessary since automatic in Python 3.x)</p>
<p>For the difference between <code>new style</code> classes and <code>old style</code> classes: see <a href="http://stackoverflow.com/questions/54867/old-s... | 2 | 2010-04-06T22:04:07Z | [
"python",
"object",
"deprecated",
"future-proof",
"new-style-class"
] |
Using Complex datatype with python SUDS client | 2,588,768 | <p>I am trying to call webservice from python client using SUDS. When I call a function with a complex data type as input parameter, it is not passed correctly, but complex data type is getting returned correctly froma webservice call.</p>
<pre><code>Webservice Type:
Soap Binding 1.1
Document/Literal
Webserve... | 3 | 2010-04-06T22:23:29Z | 2,589,632 | <p>From the looks of it the server-side operation <code>printParamBean</code> is only printing what you're passing it, but is not returning anything. Compared to <code>modifyParamBean</code> which is both printing and returning value. Could this be your problem? </p>
<p>I surmise that renaming (strictly for the sake... | 0 | 2010-04-07T02:28:41Z | [
"python",
"web-services",
"suds"
] |
Trouble using eval() with cython | 2,589,121 | <p>I was trying to speed up some code, and then I tried compiling a class and a function using cython </p>
<p>and WOW! I havn't measured it yet but it looks at least 10x faster.</p>
<p>I first looked at cython just two days ago, I'm very impressed!</p>
<p>However, I can't get eval() to work.</p>
<pre><code>def thef... | 4 | 2010-04-06T23:51:10Z | 2,589,158 | <p>This is because <code>eval</code> has no way of examining the environment to find <code>a</code>. Use the <code>locals</code> function to pass it the environment.</p>
<pre><code>def thefirst(a):
return eval('1+2+a', locals())
</code></pre>
| 6 | 2010-04-07T00:00:28Z | [
"python",
"cython"
] |
Trouble using eval() with cython | 2,589,121 | <p>I was trying to speed up some code, and then I tried compiling a class and a function using cython </p>
<p>and WOW! I havn't measured it yet but it looks at least 10x faster.</p>
<p>I first looked at cython just two days ago, I'm very impressed!</p>
<p>However, I can't get eval() to work.</p>
<pre><code>def thef... | 4 | 2010-04-06T23:51:10Z | 2,589,169 | <p>Hmm, I'd think eval would be fairly bad for performance in any case. What is your actual use case?</p>
| -1 | 2010-04-07T00:03:06Z | [
"python",
"cython"
] |
Trouble using eval() with cython | 2,589,121 | <p>I was trying to speed up some code, and then I tried compiling a class and a function using cython </p>
<p>and WOW! I havn't measured it yet but it looks at least 10x faster.</p>
<p>I first looked at cython just two days ago, I'm very impressed!</p>
<p>However, I can't get eval() to work.</p>
<pre><code>def thef... | 4 | 2010-04-06T23:51:10Z | 5,687,039 | <p>You may get away with cython.inline:</p>
<p><a href="http://wiki.cython.org/enhancements/inline" rel="nofollow">http://wiki.cython.org/enhancements/inline</a></p>
<p>However, keep an eye on your Python runtime's memory usage in this case. Each distinct expression that gets compiled and loaded takes up some memory.... | 3 | 2011-04-16T14:09:39Z | [
"python",
"cython"
] |
How can I remove all words that end in ":" from a string in Python? | 2,589,200 | <p>I'm wondering how to remove a dynamic word from a string within Python.</p>
<p>It will always have a ":" at the end of the word, and sometimes there's more than one within the string. I'd like to remove all occurrences of "word:".</p>
<p>Thanks! :-)</p>
| 6 | 2010-04-07T00:13:59Z | 2,589,213 | <p>Use regular expressions.</p>
<pre><code>import re
blah = "word word: monty py: thon"
answer = re.sub(r'\w+:\s?','',blah)
print answer
</code></pre>
<p>This will also pull out a single optional space after the colon.</p>
| 10 | 2010-04-07T00:18:16Z | [
"python",
"regex"
] |
How can I remove all words that end in ":" from a string in Python? | 2,589,200 | <p>I'm wondering how to remove a dynamic word from a string within Python.</p>
<p>It will always have a ":" at the end of the word, and sometimes there's more than one within the string. I'd like to remove all occurrences of "word:".</p>
<p>Thanks! :-)</p>
| 6 | 2010-04-07T00:13:59Z | 2,589,224 | <p>This removes all words which end with a ":":</p>
<pre><code>def RemoveDynamicWords(s):
L = []
for word in s.split():
if not word.endswith(':'):
L.append(word)
return ' '.join(L)
print RemoveDynamicWords('word: blah')
</code></pre>
<p>or use a generator expression:</p>
<pre><code>pr... | 0 | 2010-04-07T00:20:16Z | [
"python",
"regex"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.