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
Deleting from dict if found in new list in Python
1,688,863
<p>Say I have a dictionary with whatever number of values. And then I create a list. If any of the values of the list are found in the dictionary, regardless of whether or not it is a key or an index how do I delete the full value?</p> <p>E.g:</p> <pre><code>dictionary = {1:3,4:5} list = [1] ... dictionary = {4:5...
1
2009-11-06T16:49:34Z
29,527,028
<p>A one liner to do this would be :</p> <pre><code>[dictionary.pop(x) for x in list if x in dictionary.keys()] </code></pre>
0
2015-04-08T23:21:44Z
[ "python", "dictionary", "merge", "list" ]
PHPs call_user_func_array in Python
1,688,931
<p>Is there an equivalent in Python for PHP's <a href="http://www.php.net/manual/en/function.call-user-func-array.php" rel="nofollow">call_user_func_array</a>?</p>
5
2009-11-06T17:01:15Z
1,688,948
<p>Call the function with the array preceded by *:</p> <pre><code>function(*array) </code></pre>
9
2009-11-06T17:03:48Z
[ "php", "python" ]
PHPs call_user_func_array in Python
1,688,931
<p>Is there an equivalent in Python for PHP's <a href="http://www.php.net/manual/en/function.call-user-func-array.php" rel="nofollow">call_user_func_array</a>?</p>
5
2009-11-06T17:01:15Z
1,688,988
<p>if your function name is a string, you could do:</p> <pre><code>getattr(obj, 'func')(*arr) # where obj is the namespace that hold func </code></pre>
4
2009-11-06T17:12:33Z
[ "php", "python" ]
PHPs call_user_func_array in Python
1,688,931
<p>Is there an equivalent in Python for PHP's <a href="http://www.php.net/manual/en/function.call-user-func-array.php" rel="nofollow">call_user_func_array</a>?</p>
5
2009-11-06T17:01:15Z
20,127,343
<p>Your can call function and pass arguments by <code>*</code> sign<br> ex.</p> <pre><code>def add(a, b): return a + b arg = (1, 2) add(*arg) </code></pre> <p>You also can use dict to pass argument pairs by double star <code>**</code></p> <p>ex.</p> <pre><code>arg = {a: 1, b: 2} add(**arg) </code></pre>
1
2013-11-21T17:02:17Z
[ "php", "python" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,688,961
<p>A number of easy ways to do it... </p> <p>A simple perl oneliner: </p> <pre><code>perl -pi -e 's/^(.*)$/\"$1\",/g' /path/to/your/file </code></pre> <p>To explain a bit, the regex <code>^(.*)$</code> grabs everything (the <code>(.*)</code>) between the start of the line (<code>^</code>) and the end of the line (...
4
2009-11-06T17:06:57Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,688,986
<p>For the reference, in case someone wants to do the same thing using python. There is a handy module <a href="http://docs.python.org/library/fileinput.html" rel="nofollow">fileinput</a> that could be used like this:</p> <pre><code>import fileinput import sys, os for line in fileinput.input(inplace=True): sys.st...
6
2009-11-06T17:12:27Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,689,002
<p>Be a true unix geek: use sed!</p> <pre><code>sed 's/^/"/; s/$/",/;' &lt; your_text_file </code></pre> <p>If you want to escape existing double quotes with backslashes, use <code>'s/"/\\"/g; s/^/"/; s/$/",/;'</code> as the pattern.</p> <p><a href="http://en.wikipedia.org/wiki/Sed" rel="nofollow">sed</a> is ideally...
6
2009-11-06T17:14:42Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,689,011
<pre><code>sed 's/.*/"&amp;",/' </code></pre>
8
2009-11-06T17:15:35Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,689,102
<p>sh + awk are nice here too...</p> <pre><code>!/bin/sh for FILE in "$@" do awk '{print "\" $0 "\","}' &lt; $FILE &gt; $FILE.tmp mv $FILE.tmp $FILE done </code></pre>
0
2009-11-06T17:31:26Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,689,300
<p>In Bash:</p> <pre><code>while read line do echo "\"${line}\"," done &lt; inputfile </code></pre>
1
2009-11-06T18:04:10Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,691,257
<p>there is no need to construct regular expression(with backreferencing) for this task. Its an expensive operation since you are not going to change something in the line. Easiest way is just to print them out.</p> <pre><code> awk '{print "\042"$0"\042,"}' file </code></pre> <p>Results on operation on a big file:...
5
2009-11-06T23:38:13Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,691,313
<p>In vi:</p> <pre><code>:%s/^\(.*\)$/"\1",/g </code></pre>
0
2009-11-06T23:53:37Z
[ "python", "linux", "bash" ]
python or bash - adding " at beginning of line and ", at end of line
1,688,952
<p>I have text file with something like</p> <pre><code>first line line nr 2 line three </code></pre> <p>etc</p> <p>And i want to generate</p> <pre><code>"first line", "line nr 2", "line three", </code></pre> <p>I wonder how to do this in python or maybe in bash if it's easier/quicker. I know there is different cod...
2
2009-11-06T17:04:24Z
1,693,337
<p>Python</p> <pre><code>for line in open("file"): line=line.strip() print '"%s",' % line </code></pre>
1
2009-11-07T15:06:58Z
[ "python", "linux", "bash" ]
Run Python script without Windows console appearing
1,689,015
<p>Is there any way to run a Python script in Windows XP without a command shell momentarily appearing? I often need to automate WordPerfect (for work) with Python, and even if my script has no output, if I execute it from without WP an empty shell still pops up for a second before disappearing. Is there any way to pre...
36
2009-11-06T17:16:28Z
1,689,024
<p>pythonw.exe will run the script without a command prompt. The problem is that the Python interpreter, Python.exe, is linked against the console subsystem to produce console output (since that's 90% of cases) -- pythonw.exe is instead linked against the GUI subsystem, and Windows will not create a console output win...
60
2009-11-06T17:17:10Z
[ "python", "windows", "shell" ]
Run Python script without Windows console appearing
1,689,015
<p>Is there any way to run a Python script in Windows XP without a command shell momentarily appearing? I often need to automate WordPerfect (for work) with Python, and even if my script has no output, if I execute it from without WP an empty shell still pops up for a second before disappearing. Is there any way to pre...
36
2009-11-06T17:16:28Z
1,689,269
<p>If you name your files with the ".pyw" extension, then windows will execute them with the pythonw.exe interpreter. This will not open the dos console for running your script.</p>
14
2009-11-06T18:00:29Z
[ "python", "windows", "shell" ]
Overhead of a Round-trip to MySql?
1,689,031
<p>So I've been building django applications for a while now, and drinking the cool-aid and all: only using the ORM and never writing custom SQL.</p> <p>The main page of the site (the primary interface where users will spend 80% - 90% of their time) was getting slow once you have a large amount of user specific conten...
2
2009-11-06T17:18:28Z
1,689,143
<p>There is always overhead in database calls, in your case the overhead is not <strong>that</strong> bad because the application and database are on the same machine so there is no network latency but there is still a significant cost.</p> <p>When you make a request to the database it has to prepare to service that r...
1
2009-11-06T17:37:04Z
[ "python", "mysql", "django", "overhead" ]
Overhead of a Round-trip to MySql?
1,689,031
<p>So I've been building django applications for a while now, and drinking the cool-aid and all: only using the ORM and never writing custom SQL.</p> <p>The main page of the site (the primary interface where users will spend 80% - 90% of their time) was getting slow once you have a large amount of user specific conten...
2
2009-11-06T17:18:28Z
1,689,146
<p>The overhead of each queries is only part of the picture. The actual round trip time between your Django and Mysql servers is probably very small since most of your queries are coming back in less than a one millisecond. The bigger problem is that the number of queries issued to your database can quickly overwhelm ...
3
2009-11-06T17:37:31Z
[ "python", "mysql", "django", "overhead" ]
Overhead of a Round-trip to MySql?
1,689,031
<p>So I've been building django applications for a while now, and drinking the cool-aid and all: only using the ORM and never writing custom SQL.</p> <p>The main page of the site (the primary interface where users will spend 80% - 90% of their time) was getting slow once you have a large amount of user specific conten...
2
2009-11-06T17:18:28Z
1,689,330
<p>There are some ways to reduce the query volume.</p> <ol> <li><p>Use <code>.filter()</code> and <code>.all()</code> to get a bunch of things; pick and choose in the view function (or template via <code>{%if%}</code>). Python can process a batch of rows faster than MySQL. </p> <p>"But I could send too much to the ...
2
2009-11-06T18:09:45Z
[ "python", "mysql", "django", "overhead" ]
Overhead of a Round-trip to MySql?
1,689,031
<p>So I've been building django applications for a while now, and drinking the cool-aid and all: only using the ORM and never writing custom SQL.</p> <p>The main page of the site (the primary interface where users will spend 80% - 90% of their time) was getting slow once you have a large amount of user specific conten...
2
2009-11-06T17:18:28Z
1,689,452
<p>Just because you are using an ORM doesn't mean that you shouldn't do performance tuning. </p> <p>I had - like you - a home page of one of my applications that had low performance. I saw that I was doing hundreds of queries to display that page. I went looking at my code and realized that with some careful use of <c...
4
2009-11-06T18:34:27Z
[ "python", "mysql", "django", "overhead" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,109
<p>It is probably an axiom that any code can be made more succinct. Yours looks perfectly pythonic, though.</p> <p>Actually, on reflection perhaps there is no succinctness axiom like that. If <a href="http://www.merriam-webster.com/dictionary/succinct" rel="nofollow">succinct</a> means "marked by compact precise exp...
1
2009-11-06T17:32:48Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,118
<pre><code>&gt;&gt;&gt; def toss(count): result = [] for i in range(count): result.append("HT"[random.randrange(0, 2)]) return ''.join(result) &gt;&gt;&gt; s = toss(200) &gt;&gt;&gt; h_max = max(len(x) for x in s.split("T")) &gt;&gt;&gt; t_max = max(len(x) for x in s.split("H")) &gt...
2
2009-11-06T17:34:25Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,153
<p>This isn't really pythonic so much as tortured, but here's a short version (with meaningless 1-character variable names, no less!)</p> <pre><code>import random x = ''.join([chr(random.randrange(2)) for i in range(200)]) print max([len(s) for s in x.split(chr(0)) + x.split(chr(1))]) </code></pre>
2
2009-11-06T17:38:39Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,162
<p>You can use <code>itertools</code>, which is a much more Pythonic way to do this:</p> <pre><code>def toss(n): rolls = [random.randrange(2) for i in xrange(n)] maximums = [0, 0] for which, grp in itertools.groupby(rolls): maximums[which] = max(len(list(grp)), maximums[which]) print "Longest ...
7
2009-11-06T17:39:51Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,174
<pre><code>def coins(num): lst = [random.randrange(2) for i in range(num)] lst = [(i, len(list(j))) for i, j in itertools.groupby(lst)] tails = max(j for i, j in lst if i) heads = max(j for i, j in lst if not i) return {1: tails, 0: heads} </code></pre>
11
2009-11-06T17:42:00Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,198
<p>Another inefficient solution :-)</p> <pre><code>import random, re s = ''.join(str(random.randrange(2)) for c in range(10)) print s print max(re.findall(r'0+', s)) print max(re.findall(r'1+', s)) &gt;&gt;&gt; 0011100100 00 111 &gt;&gt;&gt; </code></pre>
3
2009-11-06T17:45:53Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,203
<pre><code>import random, itertools def toss(n): faces = (random.randrange(2) for i in range(n)) longest = [0, 0] for face, seq in itertools.groupby(faces): longest[face] = max(longest[face], len(list(seq))) print "longest sequence heads %d, tails %d" % tuple(longest) </code></pre>
1
2009-11-06T17:46:21Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,209
<pre><code>import collections, itertools, random def makesequence(choices=2, length=200): return [random.randrange(choices) for _ in itertools.repeat(None, length)] def runlengths(sequence): runlength_by_item = collections.defaultdict(set) for key, group in itertools.groupby(sequence): runlength_by_item[key...
11
2009-11-06T17:47:11Z
[ "python" ]
Is there a more succinct / pythonic way to do this? (counting longest seq of heads, tails in coin flips)
1,689,032
<p>Count the longest sequence of heads and tails in 200 coin flips.<br> I did this - is there a niftier way to do it in python? (without being too obfuscated)</p> <pre><code>import random def toss(n): count = [0,0] longest = [0,0] for i in xrange(n): coinface = random.randrange(2) count[c...
11
2009-11-06T17:18:33Z
1,689,313
<h2>String scanning algorithm</h2> <p>If you are looking for a fast algorithm, then you can use the algorithm I developed recently for an interview question that asked for the longest string of consecutive letters in a string. <a href="http://www.iwebthereforeiam.com/iwebthereforeiam/2009/11/interview-question-part-ii...
1
2009-11-06T18:06:26Z
[ "python" ]
handling multiple returned objects
1,689,122
<p>I have a contact/address app that allows users to search the database for contact entries. The current view will return an object (Entry()) and display its fields. The code is as follows:</p> <pre><code>def search_page(request): form = SearchForm() entrylinks = [] show_results = True if request....
0
2009-11-06T17:34:46Z
1,689,184
<p>The object returned by <code>Entry.objects.filter</code> (a <code>QuerySet</code>) has a length, meaning you can call <code>len(entrylinks)</code> to get the number of records returned. Thus, you can do something like this:</p> <pre><code>if len(entrylinks) == 1: tpl = "search.html" else: tpl = "select.html...
2
2009-11-06T17:44:26Z
[ "python", "django", "django-views" ]
handling multiple returned objects
1,689,122
<p>I have a contact/address app that allows users to search the database for contact entries. The current view will return an object (Entry()) and display its fields. The code is as follows:</p> <pre><code>def search_page(request): form = SearchForm() entrylinks = [] show_results = True if request....
0
2009-11-06T17:34:46Z
1,689,546
<p>Rather than using len(entrylinks), you should use entrylinks.count(). The built-in count method is much faster and can save you a database query.</p>
1
2009-11-06T18:52:23Z
[ "python", "django", "django-views" ]
What can cause select to block in Python?
1,689,182
<p>Here's a snippet of code I'm using in a loop:</p> <pre><code>while True: print 'loop' rlist, wlist, xlist = select.select(readers, [], [], TIMEOUT) print 'selected' # do stuff </code></pre> <p>At a certain point, select will block and "selected" is never getting printed. What can cause this behavi...
0
2009-11-06T17:44:03Z
1,689,222
<p>Some longshots...</p> <p>If TIMEOUT is getting set to None, then select will never timeout. Also, if readers becomes an empty list, select will always wait for the full timeout value (or hang if TIMEOUT is None)</p>
1
2009-11-06T17:50:48Z
[ "python", "sockets", "select", "asynchronous" ]
What can cause select to block in Python?
1,689,182
<p>Here's a snippet of code I'm using in a loop:</p> <pre><code>while True: print 'loop' rlist, wlist, xlist = select.select(readers, [], [], TIMEOUT) print 'selected' # do stuff </code></pre> <p>At a certain point, select will block and "selected" is never getting printed. What can cause this behavi...
0
2009-11-06T17:44:03Z
1,689,226
<p>Yes, depending on the OS in question, it is indeed possible for a certain file descriptor to block at OS level in a non-interruptible way even though you've explicitly demanded for it to be non-blocking. Depending on your OS, there may be workarounds to these OS-level bugs (or "misfeatures"), but to offer any furth...
2
2009-11-06T17:51:22Z
[ "python", "sockets", "select", "asynchronous" ]
Python: ulimit and nice for subprocess.call / subprocess.Popen?
1,689,505
<p>I need to limit the amount of time and cpu taken by external command line apps I spawn from a python process using subprocess.call , mainly because sometimes the spawned process gets stuck and pins the cpu at 99%. </p> <p>nice and ulimit seem like reasonable ways to do this, but I'm not sure how they'd interact wit...
30
2009-11-06T18:44:01Z
1,689,787
<p>You can set limits for subprocesses with the <code>ulimit</code> and <code>nice</code> shell commands like this:</p> <pre><code>import subprocess subprocess.Popen('ulimit -t 60; nice -n 15 cpuhog', shell=True) </code></pre> <p>This runs <code>cpuhog</code> with a limit of 60 seconds of CPU time and a niceness adju...
10
2009-11-06T19:28:06Z
[ "python", "resources", "nice", "ulimit" ]
Python: ulimit and nice for subprocess.call / subprocess.Popen?
1,689,505
<p>I need to limit the amount of time and cpu taken by external command line apps I spawn from a python process using subprocess.call , mainly because sometimes the spawned process gets stuck and pins the cpu at 99%. </p> <p>nice and ulimit seem like reasonable ways to do this, but I'm not sure how they'd interact wit...
30
2009-11-06T18:44:01Z
1,689,991
<p>Use the preexec_fn parameter to subprocess.Popen, and the resource module. Example:</p> <p><em>parent.py:</em></p> <pre><code>#!/usr/bin/env python import os import sys import resource import subprocess def setlimits(): # Set maximum CPU time to 1 second in child process, after fork() but before exec() p...
71
2009-11-06T19:59:22Z
[ "python", "resources", "nice", "ulimit" ]
Python: ulimit and nice for subprocess.call / subprocess.Popen?
1,689,505
<p>I need to limit the amount of time and cpu taken by external command line apps I spawn from a python process using subprocess.call , mainly because sometimes the spawned process gets stuck and pins the cpu at 99%. </p> <p>nice and ulimit seem like reasonable ways to do this, but I'm not sure how they'd interact wit...
30
2009-11-06T18:44:01Z
22,891,513
<p><strong>Erik</strong> made it easy for me, but he forgot the <code>nice</code> part which <strong>Rich</strong> Pointed out. I find the <code>psutil</code> package nice(pun intended) but unfortunately less portable. Here is my take at the question:</p> <pre><code>import os import psutil import resource import subpr...
3
2014-04-06T08:12:15Z
[ "python", "resources", "nice", "ulimit" ]
Tool like 2to3, except for merges
1,689,548
<p>I maintain a fork of my project for Python 3.1. When I initially made the port from 2.6, I used 2to3, but now I constantly have to merge new code from the 2.6 fork into the 3.1 fork. How can I perform the 2to3 operation on these merges automatically? (I use git, if it matters.)</p>
4
2009-11-06T18:52:30Z
1,689,736
<p>Hmmm, you are in a tough position. Perhaps you could run 2to3 on the 2.6 fork, then merge the results of that into your 3.1 branch?</p> <p>Alternatively, perhaps this pain will make you reconsider your strategy of maintaining two distinct branches for the two Python versions? I've had good luck using a single code...
7
2009-11-06T19:20:29Z
[ "python", "merge", "python-3.x", "py2to3" ]
How do I upload data to Google App Engine periodically?
1,689,570
<p>I'm writing an aggregation application which scrapes data from a couple of web sources and displays that data with a novel interface. The sites from which I'm scraping update every couple of minutes, and I want to make sure the data on my aggregator is up-to-date.</p> <p>What's the best way to periodically submit f...
0
2009-11-06T18:56:11Z
1,689,805
<p>The only way to get data into AppEngine is to call up a Web app of yours (as a Web app) and feed it data through the usual HTTP-ish means, i.e. as parameters to a GET request (for short data) or to a POST (if long or binary).</p> <p>In other words, you'll have to craft your own little dataloader, which you will acc...
0
2009-11-06T19:30:31Z
[ "python", "security", "google-app-engine", "automation" ]
How do I upload data to Google App Engine periodically?
1,689,570
<p>I'm writing an aggregation application which scrapes data from a couple of web sources and displays that data with a novel interface. The sites from which I'm scraping update every couple of minutes, and I want to make sure the data on my aggregator is up-to-date.</p> <p>What's the best way to periodically submit f...
0
2009-11-06T18:56:11Z
1,690,150
<p>Write a Task Queue task or an App Engine cron job to handle this. I'm not sure where you heard that there's a limit of 1 second on any sort of App Engine operations - requests are limited to 30 seconds, and URL fetches have a maximum deadline of 10 seconds.</p>
3
2009-11-06T20:24:43Z
[ "python", "security", "google-app-engine", "automation" ]
How do I upload data to Google App Engine periodically?
1,689,570
<p>I'm writing an aggregation application which scrapes data from a couple of web sources and displays that data with a novel interface. The sites from which I'm scraping update every couple of minutes, and I want to make sure the data on my aggregator is up-to-date.</p> <p>What's the best way to periodically submit f...
0
2009-11-06T18:56:11Z
1,690,155
<p>Can you break up the scraping process into independent chunks that can each finish in the timeframe of an appengine request? (which can run longer than one second btw). Then you can just spawn a bunch of tasks using the task API that when combined, accomplish the full scrape. Then use the cron API to spawn off tho...
0
2009-11-06T20:26:02Z
[ "python", "security", "google-app-engine", "automation" ]
How do I upload data to Google App Engine periodically?
1,689,570
<p>I'm writing an aggregation application which scrapes data from a couple of web sources and displays that data with a novel interface. The sites from which I'm scraping update every couple of minutes, and I want to make sure the data on my aggregator is up-to-date.</p> <p>What's the best way to periodically submit f...
0
2009-11-06T18:56:11Z
1,693,701
<p>I asked around and some friends came up with two solutions:</p> <ul> <li><p>Upload a file with a shared secret token along with the application, but when committing to the codebase, change the token.</p></li> <li><p>Create a small datastore model with one row, a secret token.</p></li> </ul> <p>In both cases the to...
0
2009-11-07T17:02:19Z
[ "python", "security", "google-app-engine", "automation" ]
How do I upload data to Google App Engine periodically?
1,689,570
<p>I'm writing an aggregation application which scrapes data from a couple of web sources and displays that data with a novel interface. The sites from which I'm scraping update every couple of minutes, and I want to make sure the data on my aggregator is up-to-date.</p> <p>What's the best way to periodically submit f...
0
2009-11-06T18:56:11Z
3,183,694
<p>App engine has tools to upload data. Refer to <a href="http://code.google.com/appengine/docs/python/tools/uploadingdata.html" rel="nofollow">http://code.google.com/appengine/docs/python/tools/uploadingdata.html</a></p>
0
2010-07-06T05:26:56Z
[ "python", "security", "google-app-engine", "automation" ]
In Python 2.6.4, why do I get a syntax error for a function call, of which the function is defined and works perfectly on its own?
1,689,594
<p>This happens in IDLE and Windows 7 RC1 (if that helps). Here is the module:</p> <pre><code> from math import * from TurtleWorld import * world = TurtleWorld() bob = Turtle() bob.delay = 0.1 def polyline(turtle, length, n, angle): for i in range(n): fd(turtle, length) rt(turtle, angle) def ...
1
2009-11-06T18:59:29Z
1,689,620
<p>The previous line is missing a closing parenthesis. It should read like this instead:</p> <pre><code>length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)) </code></pre>
2
2009-11-06T19:03:27Z
[ "python", "syntax", "python-idle" ]
In Python 2.6.4, why do I get a syntax error for a function call, of which the function is defined and works perfectly on its own?
1,689,594
<p>This happens in IDLE and Windows 7 RC1 (if that helps). Here is the module:</p> <pre><code> from math import * from TurtleWorld import * world = TurtleWorld() bob = Turtle() bob.delay = 0.1 def polyline(turtle, length, n, angle): for i in range(n): fd(turtle, length) rt(turtle, angle) def ...
1
2009-11-06T18:59:29Z
1,689,621
<p>There is a closing parenthesis missing in the previous line:</p> <pre><code>length_of_spoke = length_of_side/(2*sin(pi/180*angle/2) </code></pre>
2
2009-11-06T19:03:27Z
[ "python", "syntax", "python-idle" ]
In Python 2.6.4, why do I get a syntax error for a function call, of which the function is defined and works perfectly on its own?
1,689,594
<p>This happens in IDLE and Windows 7 RC1 (if that helps). Here is the module:</p> <pre><code> from math import * from TurtleWorld import * world = TurtleWorld() bob = Turtle() bob.delay = 0.1 def polyline(turtle, length, n, angle): for i in range(n): fd(turtle, length) rt(turtle, angle) def ...
1
2009-11-06T18:59:29Z
1,689,626
<p>At quick glance are you missing a closing parenthesis at the end of the line before you call spokes()?</p> <pre><code>length_of_spoke = length_of_side/(2*sin(pi/180*angle/2)) </code></pre> <p>instead of</p> <pre><code>length_of_spoke = length_of_side/(2*sin(pi/180*angle/2) </code></pre>
3
2009-11-06T19:04:36Z
[ "python", "syntax", "python-idle" ]
In Python 2.6.4, why do I get a syntax error for a function call, of which the function is defined and works perfectly on its own?
1,689,594
<p>This happens in IDLE and Windows 7 RC1 (if that helps). Here is the module:</p> <pre><code> from math import * from TurtleWorld import * world = TurtleWorld() bob = Turtle() bob.delay = 0.1 def polyline(turtle, length, n, angle): for i in range(n): fd(turtle, length) rt(turtle, angle) def ...
1
2009-11-06T18:59:29Z
1,689,648
<p>Others have already pointed out the actual syntax error, so I won't say any more about that. One thing I will add though is that if I get a syntax error, the first place I look is on the lines <em>before</em> the function. Usually it's something like a paren missing or a comma in the wrong place.</p>
2
2009-11-06T19:08:03Z
[ "python", "syntax", "python-idle" ]
Sending a retrieved SMS using Python
1,689,638
<p>I am writing a Python script to read a SMS from the SIM memory, buffer it and send the same SMS to another number. I am executing this script on Telit GM862-GPS. The script I have written is :</p> <pre><code>import MDM MDM.send('AT+CMGF=1\r', 10) # Changing to Text mode MDM.send('AT+CMGR=1\r',0...
0
2009-11-06T19:06:56Z
1,690,355
<p>It looks like your program is waiting for a response that never arrives. That sort of thing is typical when a device doesn't think you have sent a complete command yet.</p> <p>I don't know the protocol you're using to communicate with that device, but it looks like a Hayes AT command set. Is it possible the devic...
1
2009-11-06T20:53:06Z
[ "python", "sms" ]
Browser Detection Python / mod_python?
1,689,861
<p>I want to keep some statistics about users and locations in a database. For instance, I would like to store "Mozilla","Firefox","Safari","Chrome","IE", etc... as well as the versions, and possibly the operating system. </p> <p>What I am trying to locate from Python is this string;</p> <p>Mozilla/5.0 (X11; U; Linux...
4
2009-11-06T19:38:41Z
1,689,884
<p><code>HTTP_USER_AGENT</code> contains this information, and will be passed in the environment variables your application uses. In mod_python, this is expressed as:</p> <pre><code>def my_request_handler(req): req.add_common_vars() agent = req.subprocess_env.get("HTTP_USER_AGENT") # `agent` now contains...
3
2009-11-06T19:42:11Z
[ "python", "http-headers", "mod-python", "browser-detection" ]
Browser Detection Python / mod_python?
1,689,861
<p>I want to keep some statistics about users and locations in a database. For instance, I would like to store "Mozilla","Firefox","Safari","Chrome","IE", etc... as well as the versions, and possibly the operating system. </p> <p>What I am trying to locate from Python is this string;</p> <p>Mozilla/5.0 (X11; U; Linux...
4
2009-11-06T19:38:41Z
2,434,109
<p>The method suggested by Jed Smith works, but I was sure there was a simpler way.</p> <p>The <code>req.headers_in</code> variable contains all the header info, and you can easily access the user agent using mod_python by calling:</p> <pre><code>req.headers_in[ 'User-Agent' ] </code></pre> <p>It is not necessary to...
2
2010-03-12T16:25:13Z
[ "python", "http-headers", "mod-python", "browser-detection" ]
Browser Detection Python / mod_python?
1,689,861
<p>I want to keep some statistics about users and locations in a database. For instance, I would like to store "Mozilla","Firefox","Safari","Chrome","IE", etc... as well as the versions, and possibly the operating system. </p> <p>What I am trying to locate from Python is this string;</p> <p>Mozilla/5.0 (X11; U; Linux...
4
2009-11-06T19:38:41Z
11,121,406
<p>If you are using the <strong>Django</strong>-Framework you get the user agent like this</p> <pre><code>request.META['HTTP_USER_AGENT'] </code></pre> <p>The very nice plugin <strong>httpagentparser</strong> extracts every detail and puts it a dictionary. </p> <p>Installation works via pip</p> <pre><code>pip insta...
2
2012-06-20T14:05:53Z
[ "python", "http-headers", "mod-python", "browser-detection" ]
Modulus in Python's slicing
1,689,984
<p>How can you fix the following code?</p> <p>I want to get the slice of elements that are <em>i mod 5 == 1</em>.</p> <pre><code>data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8" arra = map(int,data.split("|")) sums += [sum(arra[i % 5==1:(i + 4) % 5==1]) // Problem here for i in range(0, len(arra), 4)] </c...
0
2009-11-06T19:58:09Z
1,690,010
<pre><code>sums += sum(arra[1::5]) </code></pre> <p>And it's spelled <code>array</code>. ;-)</p>
6
2009-11-06T20:01:22Z
[ "python" ]
Modulus in Python's slicing
1,689,984
<p>How can you fix the following code?</p> <p>I want to get the slice of elements that are <em>i mod 5 == 1</em>.</p> <pre><code>data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8" arra = map(int,data.split("|")) sums += [sum(arra[i % 5==1:(i + 4) % 5==1]) // Problem here for i in range(0, len(arra), 4)] </c...
0
2009-11-06T19:58:09Z
1,691,577
<p>It's</p> <pre><code>sums = sum(arra[1::5]) </code></pre> <p>If you use +=, Python spects that the name sums is alreadey accesible:</p> <p>Traceback (most recent call last): File "", line 1, in sums += sum(arra[1::5]) NameError: name 'sums' is not defined</p>
0
2009-11-07T01:28:45Z
[ "python" ]
What technologies are good for sending encapsulated data, and later converting it, between Python and Objective-C?
1,690,080
<p>I'm attempting to create a client/server web-app. The client software is written in Objective-C (Mac), and the server software is written in Python (Linux). I'd like to encapsulate object data on either side, and send it across the internet to the other side. This will include standard types such as strings, doub...
0
2009-11-06T20:13:09Z
1,690,103
<p>If you're talking about a traditional browser based web app, then I'd probably stick with JSON or XML serialization.</p> <p>For anything else I'd suggest <em>drum roll please...</em></p> <p><a href="http://code.google.com/p/protobuf/" rel="nofollow">Google Protocol Buffers</a></p> <p>Small, fast, and has a decent...
2
2009-11-06T20:15:59Z
[ "python", "objective-c", "encapsulation" ]
What technologies are good for sending encapsulated data, and later converting it, between Python and Objective-C?
1,690,080
<p>I'm attempting to create a client/server web-app. The client software is written in Objective-C (Mac), and the server software is written in Python (Linux). I'd like to encapsulate object data on either side, and send it across the internet to the other side. This will include standard types such as strings, doub...
0
2009-11-06T20:13:09Z
1,690,122
<p>Might check out a piece of technology called Thrift. It is an apache based service built specifically for communicating between different languages.</p> <p>EDIT - <a href="http://incubator.apache.org/thrift/" rel="nofollow">link</a></p>
1
2009-11-06T20:18:55Z
[ "python", "objective-c", "encapsulation" ]
What technologies are good for sending encapsulated data, and later converting it, between Python and Objective-C?
1,690,080
<p>I'm attempting to create a client/server web-app. The client software is written in Objective-C (Mac), and the server software is written in Python (Linux). I'd like to encapsulate object data on either side, and send it across the internet to the other side. This will include standard types such as strings, doub...
0
2009-11-06T20:13:09Z
1,690,190
<p>You could use <a href="http://pyobjc.sourceforge.net/" rel="nofollow">PyObjC</a> on the client side and some kind of python based serialization. There are facilities in PyObjC for embedding in an existing Cocoa application and for communicating with the Python runtime.</p>
1
2009-11-06T20:30:17Z
[ "python", "objective-c", "encapsulation" ]
AND in Python's slicing with modulus
1,690,203
<p>How can you fix the code?</p> <p>I am trying to have <code>i % 3 == 1</code> and <code>i != 16</code> unsuccessfully by</p> <pre><code>data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8" arra = map(int,data.split("|")) arra = sum(arra[1::3 and != 16]) for i in range(0, len(arra), 16)] | ...
0
2009-11-06T20:32:08Z
1,690,218
<p>Slices don't work like that.</p> <p><a href="http://stackoverflow.com/questions/1690203/and-in-pythons-slicing-with-modulus/1690245#1690245">Paul McGuire</a> has the correct code:</p> <pre><code>arra = sum(x for i, x in enumerate(arra) if i % 3 == 1 and i != 16) </code></pre> <p>It's also not clear from your code...
0
2009-11-06T20:33:57Z
[ "python" ]
AND in Python's slicing with modulus
1,690,203
<p>How can you fix the code?</p> <p>I am trying to have <code>i % 3 == 1</code> and <code>i != 16</code> unsuccessfully by</p> <pre><code>data = "8|9|8|9|8|9|8|9|9|8|9|8|9|8|9|8" arra = map(int,data.split("|")) arra = sum(arra[1::3 and != 16]) for i in range(0, len(arra), 16)] | ...
0
2009-11-06T20:32:08Z
1,690,245
<p>Try this:</p> <pre><code>arra = sum(a for i,a in enumerate(arra) if i %3==1 and i != 16) </code></pre> <p>For this kind of complex work, slice notation wont really do. But why do you assign back to arra? You wipe out your original list of values.</p>
6
2009-11-06T20:37:50Z
[ "python" ]
Compile a .pyw file so it can be run like .pyc without console
1,690,331
<p>I'm trying to compile a pyw file into a pyc without console. When I try a straight compile with it, the result is a pywc file, but it seems that pythonw.exe doesn't register that extension as one of it's files like python.exe does for a pyc.</p> <p>The result of course is that it has no double click handler when y...
2
2009-11-06T20:49:55Z
1,690,516
<p>You can use the FTYPE and ASSOC commands to associate .pywc files with pythonw.exe.</p> <p>ASSOC is used to associate an extension with a file type, and FTYPE associates a file type with the command used to open it. The following commands show existing assocations on my machine:</p> <pre><code>C:\Python26&gt;assoc...
3
2009-11-06T21:17:45Z
[ "python", "compilation" ]
Compile a .pyw file so it can be run like .pyc without console
1,690,331
<p>I'm trying to compile a pyw file into a pyc without console. When I try a straight compile with it, the result is a pywc file, but it seems that pythonw.exe doesn't register that extension as one of it's files like python.exe does for a pyc.</p> <p>The result of course is that it has no double click handler when y...
2
2009-11-06T20:49:55Z
1,691,273
<p>(following a little discussion in the OP's post)</p> <p>If what you need is provide clients or users with the compiled application in form of *.pyc or *.pyo, and avoid opening the console at the execution, a good option is to leave a *.pyw wrapper that calls the main application.</p> <p>Let's assume the main appli...
5
2009-11-06T23:43:07Z
[ "python", "compilation" ]
Compile a .pyw file so it can be run like .pyc without console
1,690,331
<p>I'm trying to compile a pyw file into a pyc without console. When I try a straight compile with it, the result is a pywc file, but it seems that pythonw.exe doesn't register that extension as one of it's files like python.exe does for a pyc.</p> <p>The result of course is that it has no double click handler when y...
2
2009-11-06T20:49:55Z
12,307,483
<p>The interpreter that reads <code>.pyw</code> files is also capable of reading a python compiled <code>.pyc</code> formatted file. Simply rename your <code>.pyc</code> file to <code>.pyw</code>. It will run (although it will still be version specific), and you won't get the ugly console.</p>
3
2012-09-06T19:58:35Z
[ "python", "compilation" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
1,690,425
<p>This cannot work, just imagine this: <code>a = b = TheMagicObjet()</code>. Names have no effect on Values, they just point to them.</p>
2
2009-11-06T21:03:23Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
1,690,433
<p>Instances don't have names. By the time the global name <code>ThisObject</code> gets <strong>bound</strong> to the instance created by evaluating the <code>SomeObject</code> constructor, the constructor has finished running.</p> <p>If you want an object to have a name, just pass the name along in the constructor.</...
16
2009-11-06T21:04:13Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
1,690,480
<p>In Python, all data is stored in objects. Additionally, a name can be bound with an object, after which that name can be used to look up that object.</p> <p>It makes no difference to the object what names, if any, it might be bound to. It might be bound to dozens of different names, or none. Also, Python does not...
2
2009-11-06T21:10:57Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
1,690,751
<p>Well, there is almost a way to do it:</p> <pre><code>#!/usr/bin/env python import traceback class SomeObject(): def __init__(self, def_name=None): if def_name == None: (filename,line_number,function_name,text)=traceback.extract_stack()[-2] def_name = text[:text.find('=')].strip()...
13
2009-11-06T21:54:19Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
7,314,788
<p>I think that names matters if they are the pointers to any object.. no matters if:</p> <pre><code>foo = 1 bar = foo </code></pre> <p>I know that foo points to 1 and bar points to the same value 1 into the same memory space. but supose that I want to create a class with a function that adds a object to it.</p> <pr...
0
2011-09-06T04:02:03Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
7,315,253
<p>One horrible, horrible way to accomplish this is to reverse the responsibilities:</p> <pre><code>class SomeObject(): def __init__(self, def_name): self.defined_name = def_name globals()[def_name] = self SomeObject("ThisObject") print ThisObject.defined_name </code></pre> <p>If you wanted to su...
2
2011-09-06T05:19:26Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
17,214,572
<p>You can create a method inside your class that check all variables in the current frame and use <code>hash()</code> to look for the <code>self</code> variable.</p> <p>The solution proposed here will return all the variables pointing to the instance object.</p> <p>In the class below, <code>isinstance()</code> is us...
2
2013-06-20T13:12:02Z
[ "python", "class", "variables", "object", "instance" ]
Getting an instance name inside class __init__()
1,690,400
<p>While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here's the basic pseudo-code I'm trying for:</p> <pre><code>class SomeObject(): defined_name = u"" def __init__(s...
11
2009-11-06T20:58:57Z
39,902,619
<p>If you are creating the instance with 'ThisObject = SomeObject()' from the command line, you can get the object name from the command string in command history:</p> <pre><code>import readline import re class SomeObject(): def __init__(self): cmd = readline.get_history_item(readline.get_current_history_...
0
2016-10-06T17:52:37Z
[ "python", "class", "variables", "object", "instance" ]
How to post a file via HTTP with cookies using python poster lib
1,690,446
<p>Using Chris Atlee's python <a href="http://atlee.ca/software/poster/" rel="nofollow">poster library</a> is there any way to include cookie handling? I have python http login code, which works with cookies:</p> <pre><code>cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(...
2
2009-11-06T21:05:59Z
1,690,487
<p>Have you tried this:</p> <pre><code>cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) register_openers() params = {'file': open("test.txt", "rb"), 'name': 'upload test'} datagen, headers = multipart_encode(params) request = urllib2.Request(upload_url, datagen...
0
2009-11-06T21:12:25Z
[ "python", "http", "cookies", "file-upload" ]
How to post a file via HTTP with cookies using python poster lib
1,690,446
<p>Using Chris Atlee's python <a href="http://atlee.ca/software/poster/" rel="nofollow">poster library</a> is there any way to include cookie handling? I have python http login code, which works with cookies:</p> <pre><code>cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(...
2
2009-11-06T21:05:59Z
1,702,492
<p>I've figured out how to do this. I'm not sure if this is the best way to go about things, but it works, so I'll share it. In order to use the poster lib with cookies one must add <code>urllib2.HTTPCookieProcessor</code> to the opener built in <code>poster.streaminghttp.register_openers()</code>.</p> <p>Essentially,...
0
2009-11-09T17:26:58Z
[ "python", "http", "cookies", "file-upload" ]
How to post a file via HTTP with cookies using python poster lib
1,690,446
<p>Using Chris Atlee's python <a href="http://atlee.ca/software/poster/" rel="nofollow">poster library</a> is there any way to include cookie handling? I have python http login code, which works with cookies:</p> <pre><code>cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(...
2
2009-11-06T21:05:59Z
2,184,189
<p>You don't have to modify the original source code, just install all required openers manually (without calling register_openers()):</p> <pre><code>import urllib2 import cookielib import poster handlers = [poster.streaminghttp.StreamingHTTPHandler(), poster.streaminghttp.StreamingHTTPRedirectHandler(), ...
1
2010-02-02T13:05:38Z
[ "python", "http", "cookies", "file-upload" ]
How to post a file via HTTP with cookies using python poster lib
1,690,446
<p>Using Chris Atlee's python <a href="http://atlee.ca/software/poster/" rel="nofollow">poster library</a> is there any way to include cookie handling? I have python http login code, which works with cookies:</p> <pre><code>cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(...
2
2009-11-06T21:05:59Z
2,356,659
<p>I sent an email to Chris AtLee asking whether we could get a basic authentication example. He was very cool about answering my questions and even ran some example code I sent him. </p> <p>To include cookie handling, you do something like this:</p> <pre><code>opener = poster.streaminghttp.register_openers() opener....
7
2010-03-01T15:02:18Z
[ "python", "http", "cookies", "file-upload" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,690,541
<p>They are semantically identical. </p> <p>The <code>dict( param=value, ... )</code> notation limits your keys to strings which are valid python identifiers.</p> <p>The <code>dict( sequence-of-2-tuples )</code> is effectively the same as <code>{}</code>.</p> <p>The <code>{}</code> notation places no limits on the ...
5
2009-11-06T21:21:22Z
[ "python", "hash", "dictionary" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,690,544
<p>The curly braces are a syntaxtic nicity, so the only benefit is can more clearly express the structure of the dict. </p>
1
2009-11-06T21:21:41Z
[ "python", "hash", "dictionary" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,690,547
<p><code>dict(somekey=somevalue)</code> is exactly the same as <code>{'somekey': somevalue}</code>. (Your two examples, then, aren't quite quivalent—in the first case the key is the string <code>'somekey'</code> as the key and in the second it is whatever somekey is.) It is generally nicer just to use dict literals u...
1
2009-11-06T21:21:53Z
[ "python", "hash", "dictionary" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,690,569
<pre> >>> def foo(): return dict(a=1) ... >>> def bar(): return {'a':1} ... >>> import dis >>> dis.dis(foo) 1 0 LOAD_GLOBAL 0 (dict) 3 LOAD_CONST 1 ('a') 6 LOAD_CONST 2 (1) 9 CALL_FUNCTION 256 12 RETURN_...
15
2009-11-06T21:25:03Z
[ "python", "hash", "dictionary" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,690,574
<p><code>{somekey:somevalue}</code> can only be used as a literal, it doesn't allow any looping inside the brackets.</p> <p><code>dict</code> takes <code>**kwargs</code>, which means you can for example do <code>dict( **locals() )</code>. <code>dict(somekey=somevalue)</code> is just a nice shortcut when you don't want...
2
2009-11-06T21:25:48Z
[ "python", "hash", "dictionary" ]
Is there any benefit to returning a hash constructed with dict rather than just using the curly braces syntax?
1,690,517
<p>In some Python code I've read I keep noticing this code:</p> <pre><code>return dict(somekey=somevalue) </code></pre> <p>Does that have any benefit over:</p> <pre><code>return {somekey:somevalue} </code></pre> <p>I tend to say no, since both objects will belong to the same <code>dict</code> type, but I may be wro...
4
2009-11-06T21:17:45Z
1,691,494
<p>Python is dynamic -- so you can redefine what 'dict' means in any of your scopes, while Python's syntax is absolutely nonprogrammable. Thus the Python parser can conclude, when it sees the curly braces, that it must build a dict. A <code>dict(..)</code> expression must always be evaluated like any other function cal...
2
2009-11-07T00:52:31Z
[ "python", "hash", "dictionary" ]
Location of Sphinx sources for my notes
1,690,757
<p><strong>How can you fix the Sphinx's warning at the bottom?</strong></p> <p>I am trying to have my Python notes in Sphinx. I have my notes in separate files at the same directory level as the <em>index.rst</em>.</p> <p>I get the following warnings after building HTML</p> <p><strong>The warning</strong></p> <pre>...
2
2009-11-06T21:55:31Z
1,691,460
<p>Are you aware of Sphinx's documentation? <a href="http://sphinx.pocoo.org/contents.html">http://sphinx.pocoo.org/contents.html</a></p> <p>Specifically, read about the <code>toctree</code> directive: <a href="http://sphinx.pocoo.org/concepts.html">http://sphinx.pocoo.org/concepts.html</a></p> <p>You can have as ma...
6
2009-11-07T00:41:39Z
[ "python", "python-sphinx" ]
Python internationalization, local setting independent
1,690,857
<p>I need the return of a strftime() call being in a language different at the one set on my local machine/OS. Is that possible to choose the language of the return?</p>
3
2009-11-06T22:17:25Z
1,691,268
<p>For solid i18n/L10N, usable by a server which must serve different localizations within the same run, I keep recommending <a href="http://pyicu.osafoundation.org/" rel="nofollow">PyICU</a>, the Python layer on top of <a href="http://site.icu-project.org/" rel="nofollow">ICU</a>, the International Components for Unic...
1
2009-11-06T23:40:48Z
[ "python", "internationalization" ]
Python internationalization, local setting independent
1,690,857
<p>I need the return of a strftime() call being in a language different at the one set on my local machine/OS. Is that possible to choose the language of the return?</p>
3
2009-11-06T22:17:25Z
1,692,913
<p>Try the babel library: <a href="http://babel.edgewall.org/wiki/Documentation/dates.html" rel="nofollow">http://babel.edgewall.org/wiki/Documentation/dates.html</a></p>
0
2009-11-07T12:41:24Z
[ "python", "internationalization" ]
Zip File with PPMD Compression, Programmatically Unzip
1,690,993
<p>I have a Linux Python script that needs to unzip some zip files. I was using the zipfile module. On a few of these files I'm getting:</p> <pre><code>BadZipfile: Unsupported compression method 98 for file </code></pre> <p>Searching around it seems these files are using <a href="http://www.winzip.com/comp%5Finfo.h...
1
2009-11-06T22:39:56Z
1,691,645
<p>Have you taken a look at the p7zip (binary)? </p> <p>It seems to support the PPMD compression format.</p> <p>Some PPMd switches: -mmem=24b, -mmem=24k, -mmem=24m, -mo=2, and -mo=32</p>
0
2009-11-07T01:47:12Z
[ "python", "linux", "compression", "zip" ]
Activate virtualenv via os.system()
1,691,076
<p>I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.</p> <pre><code>os.system('virtualenv %s --no-site-packages' % project_name...
4
2009-11-06T22:55:18Z
1,691,118
<p>Each <code>os.system</code> call creates a new process. You'll need to ensure that the <code>activate</code> and the <code>easy_install</code> are run in the same <code>os.system</code> or <code>subprocess</code> call.</p>
3
2009-11-06T23:01:22Z
[ "python", "django", "virtualenv", "fabric", "pip" ]
Activate virtualenv via os.system()
1,691,076
<p>I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.</p> <pre><code>os.system('virtualenv %s --no-site-packages' % project_name...
4
2009-11-06T22:55:18Z
1,691,123
<p>Each call to os.system runs the command in a new subshell, which has the same properties as the original python process.</p> <p>Try putting the commands into one string separated by semicolons.</p>
11
2009-11-06T23:02:18Z
[ "python", "django", "virtualenv", "fabric", "pip" ]
Activate virtualenv via os.system()
1,691,076
<p>I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.</p> <pre><code>os.system('virtualenv %s --no-site-packages' % project_name...
4
2009-11-06T22:55:18Z
1,692,040
<p>You could also install virtualenvwrapper, and use the postmkvirtualenv hook. I use it to automatically bring in fresh copies of pip and IPython into virtualenvs I create (as I don't want it using my system IPython). I also use it to copy pythonw into the virtualenv, otherwise wx-based stuff won't work. Looks like th...
3
2009-11-07T04:53:48Z
[ "python", "django", "virtualenv", "fabric", "pip" ]
Activate virtualenv via os.system()
1,691,076
<p>I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip, and fabric. Should be straightforward enough, but it appears that I'm not able to activate and run commands in the virtualenv through the shell script.</p> <pre><code>os.system('virtualenv %s --no-site-packages' % project_name...
4
2009-11-06T22:55:18Z
1,698,958
<p>Just don't use "source activate" at all. It does nothing but alter your shell PATH to put the virtualenv's bin directory first. I presume your script knows the directory of the virtualenv it has just created; all you have to do is call _virtualenv_dir_/bin/easy_install by full path. Or _virtualenv_dir_/bin/python fo...
4
2009-11-09T03:32:02Z
[ "python", "django", "virtualenv", "fabric", "pip" ]
Is TCP Guaranteed to arrive in order?
1,691,179
<p>If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-s...
24
2009-11-06T23:16:50Z
1,691,189
<p>As long as the two messages were sent <em>on the same TCP connection</em>, order will be maintained. If multiple connections are opened between the same pair of processes, you may be in trouble.</p> <p>Regarding Twisted, or any other asynchronous event system: I expect you'll get the <code>dataReceived</code> mess...
38
2009-11-06T23:19:18Z
[ "python", "tcp", "twisted", "protocols" ]
Is TCP Guaranteed to arrive in order?
1,691,179
<p>If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-s...
24
2009-11-06T23:16:50Z
1,691,194
<p>TCP is a stream, UDP is a message. You're mixing up terms. For TCP it is true that the stream will arrive in the same order as it was send. There are no distict messages in TCP, bytes appear as they arrive, interpreting them as messages is up to you.</p>
7
2009-11-06T23:20:34Z
[ "python", "tcp", "twisted", "protocols" ]
Is TCP Guaranteed to arrive in order?
1,691,179
<p>If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-s...
24
2009-11-06T23:16:50Z
1,691,197
<p>TCP is connection-oriented and offers its Clients <strong>in-order</strong> delivery. Of course this applies to the connection level: individual connections are independent.</p> <p>You should note that normally we refer to "TCP streams" and "UDP messages".</p> <p>Whatever Client library you use (e.g. Twisted), the...
16
2009-11-06T23:21:52Z
[ "python", "tcp", "twisted", "protocols" ]
Is TCP Guaranteed to arrive in order?
1,691,179
<p>If I send two TCP messages, do I need to handle the case where the latter arrives before the former? Or is it guaranteed to arrive in the order I send it? I assume that this is not a Twisted-specific example, because it should conform to the TCP standard, but if anyone familiar with Twisted could provide a Twisted-s...
24
2009-11-06T23:16:50Z
1,691,220
<p>TCP "guarantees" that a receiver will receive the reconstituted stream of bytes as it was originally sent by the sender. However, between the TCP send/receive endpoints (i.e., the physical network), the data can be received out of order, it can be fragmented, it can be corrupted, and it can even be lost. TCP accou...
11
2009-11-06T23:26:56Z
[ "python", "tcp", "twisted", "protocols" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,691,215
<p>Probably the closest thing to what you are talking about is an <a href="http://en.wikipedia.org/wiki/Interpreter%5F%28computing%29" rel="nofollow">interpreter</a>:</p> <blockquote> <p>In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in ...
2
2009-11-06T23:25:41Z
[ "java", "python", "ruby", "scripting" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,691,248
<p>You've basically discovered that the distinction between a scripting language and a "non-scripting" language is pretty artificial. Python can be compiled to JVM bytecode (with Jython), and I believe Ruby also can -- then the "engine" running the Python or Ruby code in question will be a JVM, the same "engine" that ...
6
2009-11-06T23:35:09Z
[ "java", "python", "ruby", "scripting" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,691,250
<p>There is no hard and fast line between a "scripting language" and a "programming language".</p> <p>Properties of "scripting languages" tend to include:</p> <ul> <li><p>garbage-collected memory manager, with no need to explicitly allocate and free objects</p></li> <li><p>ability to simply execute commands, without ...
11
2009-11-06T23:35:40Z
[ "java", "python", "ruby", "scripting" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,691,253
<p>"Scripting language" might be called a colloquialism. The term is not well defined, and you will see some disagreement about which languages are scripting languages. It is sometimes useful for conveying a vague idea of the properties of a language (See <a href="http://stackoverflow.com/questions/1691201/what-is-a-...
0
2009-11-06T23:37:38Z
[ "java", "python", "ruby", "scripting" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,691,317
<p>One useful distinction between scripting / interpreted languages and compiled languages is that you can typically embed a scripting language's interpreter in a compiled project, such as a game engine.</p>
0
2009-11-06T23:54:51Z
[ "java", "python", "ruby", "scripting" ]
What is a scripting engine?
1,691,201
<p>I've seen <a href="http://stackoverflow.com/questions/101055/when-is-a-language-considered-a-scripting-language">here</a> that what sets a programming language apart from a scripting language is the scripting engine. But I don't understand how it works, so I don't know the difference.</p> <p>For example, I see code...
9
2009-11-06T23:22:45Z
1,692,561
<p>I know you have accepted an answer, however there is some amgiguity.</p> <p>When referring to a <em>scripting engine</em>, we typically mean a small embedded language that sits within a template and generates textual output or documents. For example Freemarker and Velocity are often referred to as scripting engines...
3
2009-11-07T09:38:18Z
[ "java", "python", "ruby", "scripting" ]
optimizing this django code?
1,691,207
<p>I'm having some performance issues because I'm making a lot of query calls that I'm not sure how to reduce.</p> <p>user_item_rel_set is a m2m relation between user and items showing how much a user paid for a particular item. Each item can have multiple users and buyers, and I'm trying to get the m2m relation for ...
1
2009-11-06T23:23:37Z
1,691,537
<p>Some additional code from your model would help, because it's hard to see what the 'items' queryset contains.</p> <p>I will try to help anyway...</p> <p>Because you've modeled a relationship between users and items, there is no need to iterate over every item in that queryset when you can simply select the subset ...
1
2009-11-07T01:10:55Z
[ "python", "django" ]
Does Google allow other people to use their "Did you mean" API?
1,691,275
<p>I have been searching all over the Internet, but did not find that exact API.</p> <p>I'd like to use their Did You mean feature for my own website.</p>
6
2009-11-06T23:43:19Z
1,691,312
<p>doSpellingSuggestion I believe is the api call. There is a good example <a href="http://www.asp101.com/articles/jeremy/googlespell/default.asp" rel="nofollow">here</a></p> <p><em>Update</em><br> As people have noted, the link is broken. <a href="https://web.archive.org/web/20090319083335/http://www.asp101.com/artic...
6
2009-11-06T23:53:34Z
[ "python", "api", "spell-checking", "google-api" ]
Does Google allow other people to use their "Did you mean" API?
1,691,275
<p>I have been searching all over the Internet, but did not find that exact API.</p> <p>I'd like to use their Did You mean feature for my own website.</p>
6
2009-11-06T23:43:19Z
1,691,335
<p>Pygoogle has an api call for that</p> <p><a href="http://pygoogle.sourceforge.net/dist/doc/public/google-module.html#doSpellingSuggestion">http://pygoogle.sourceforge.net/dist/doc/public/google-module.html#doSpellingSuggestion</a></p> <pre><code>&gt;&gt;&gt; import google &gt;&gt;&gt; google.LICENSE_KEY = '...' &g...
8
2009-11-07T00:00:17Z
[ "python", "api", "spell-checking", "google-api" ]
Does Google allow other people to use their "Did you mean" API?
1,691,275
<p>I have been searching all over the Internet, but did not find that exact API.</p> <p>I'd like to use their Did You mean feature for my own website.</p>
6
2009-11-06T23:43:19Z
1,691,939
<p>There's been a great Python example going around for awhile showing exactly how to implement this (in only 21 lines of code!): <a href="http://norvig.com/spell-correct.html" rel="nofollow">http://norvig.com/spell-correct.html</a> It was shown at the Stackoverflow DevDays in Toronto, and I believe at some of the othe...
1
2009-11-07T03:54:31Z
[ "python", "api", "spell-checking", "google-api" ]
Does Google allow other people to use their "Did you mean" API?
1,691,275
<p>I have been searching all over the Internet, but did not find that exact API.</p> <p>I'd like to use their Did You mean feature for my own website.</p>
6
2009-11-06T23:43:19Z
15,883,602
<p>Here is one in python that get the <a href="https://github.com/bkvirendra/didyoumean" rel="nofollow">didyoumean</a> results</p>
0
2013-04-08T15:51:22Z
[ "python", "api", "spell-checking", "google-api" ]