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 does the Python's range function work?
1,738,109
<p>all,I am not really understand for loop in the following,can anyone give some tips?thanks in advance</p> <pre><code>for i in range(5): print i </code></pre> <p>then it gives 0,1,2,3,4 is that python assign 0,1,2,3,4 to i at the same time? however if i wrote:</p> <pre><code>for i in range(5): a=i+1 </c...
8
2009-11-15T17:06:38Z
1,738,132
<p><code>range(x)</code> returns a list of numbers from 0 to <em>x</em> - 1.</p> <pre><code>&gt;&gt;&gt; range(1) [0] &gt;&gt;&gt; range(2) [0, 1] &gt;&gt;&gt; range(3) [0, 1, 2] &gt;&gt;&gt; range(4) [0, 1, 2, 3] </code></pre> <p><code>for i in range(x):</code> executes the body (which is <code>print i</code> in you...
3
2009-11-15T17:16:50Z
[ "python", "for-loop", "range" ]
How does the Python's range function work?
1,738,109
<p>all,I am not really understand for loop in the following,can anyone give some tips?thanks in advance</p> <pre><code>for i in range(5): print i </code></pre> <p>then it gives 0,1,2,3,4 is that python assign 0,1,2,3,4 to i at the same time? however if i wrote:</p> <pre><code>for i in range(5): a=i+1 </c...
8
2009-11-15T17:06:38Z
1,738,840
<p>When I'm teaching someone programming (just about any language) I introduce <code>for</code> loops with terminology similar to this code example:</p> <pre><code>for eachItem in someList: doSomething(eachItem) </code></pre> <p>... which, conveniently enough, is syntactically valid Python code.</p> <p>The Pytho...
9
2009-11-15T21:08:07Z
[ "python", "for-loop", "range" ]
httplib2, how to set more than one cookie?
1,738,227
<p>As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:</p> <pre><code>response, content = http.request(url, 'GET', headers=headers) headers = {'Cookie': response['set-cookie']} url = 'http://www.e...
6
2009-11-15T17:42:55Z
1,738,240
<p>Cookies are contained in a single HTTP header, separated by semicolons. Example:</p> <pre><code>cookie1=value1;cookie2=value2 </code></pre> <p>So you'll need to build a string from the cookies sent by the server, and then set that as the <code>Cookie</code> header.</p> <p><strong>Edit</strong>: Actually, playing...
5
2009-11-15T17:47:05Z
[ "python", "cookies", "httplib2", "cookiejar" ]
httplib2, how to set more than one cookie?
1,738,227
<p>As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:</p> <pre><code>response, content = http.request(url, 'GET', headers=headers) headers = {'Cookie': response['set-cookie']} url = 'http://www.e...
6
2009-11-15T17:42:55Z
1,738,320
<p>Yes, I just found out elsewhere about the Cookie header when making the request, but the server may send several Set-Cookie headers, with a cookie(and expiration,domain,etc attributes) per header. But with the dictionary system used in httplib2, I can't really get all the possible Set-Cookie headers sent by the serv...
3
2009-11-15T18:14:50Z
[ "python", "cookies", "httplib2", "cookiejar" ]
httplib2, how to set more than one cookie?
1,738,227
<p>As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:</p> <pre><code>response, content = http.request(url, 'GET', headers=headers) headers = {'Cookie': response['set-cookie']} url = 'http://www.e...
6
2009-11-15T17:42:55Z
1,738,450
<p>Doing some extra testing, with a dummy setcookie() PHP page, I generated in 3 test, the following set of headers:</p> <pre><code>Set-Cookie: chocolate=chips Set-Cookie: milk=shape Set-Cookie: chocolate=chips; expires=Sun, 15-Nov-2009 18:47:08 GMT; path=/; domain=thaorius.net; secure; httponly Set-Cookie: milk=sha...
2
2009-11-15T18:57:48Z
[ "python", "cookies", "httplib2", "cookiejar" ]
httplib2, how to set more than one cookie?
1,738,227
<p>As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this:</p> <pre><code>response, content = http.request(url, 'GET', headers=headers) headers = {'Cookie': response['set-cookie']} url = 'http://www.e...
6
2009-11-15T17:42:55Z
2,882,912
<p>Try this:</p> <pre><code>pp = re.compile('(Sun|Mon|Tue|Wed|Thu|Fri|Sat),') pp.sub('','alpha Sun, beta') 'alpha beta' </code></pre>
-2
2010-05-21T14:25:45Z
[ "python", "cookies", "httplib2", "cookiejar" ]
Pylons and Memcached
1,738,250
<p>Anyone happen to use this combination in their web application? I'm having a bit of trouble finding some sort of tutorial or guideline for configuring this. Also seeing as how I started using Pylons recently I'm not familiar so please keep the advice very newbie friendly ( I haven't even used modules like Beaker all...
3
2009-11-15T17:51:13Z
1,738,519
<p><code>memcached</code> is nice and framework-agnostic, and you just have to write a bit of code to interact with it. The general idea of memcached is:</p> <pre><code>object = try_memcached() if not object: object = real_query() put_in_memcached(object) </code></pre> <p>That will likely be done in your SQL...
7
2009-11-15T19:29:57Z
[ "python", "memcached", "pylons" ]
Pylons and Memcached
1,738,250
<p>Anyone happen to use this combination in their web application? I'm having a bit of trouble finding some sort of tutorial or guideline for configuring this. Also seeing as how I started using Pylons recently I'm not familiar so please keep the advice very newbie friendly ( I haven't even used modules like Beaker all...
3
2009-11-15T17:51:13Z
4,011,568
<p>Pylons recommends Beaker for caching and it has a Memcache backend. See <a href="http://pylonshq.com/docs/en/0.9.7/thirdparty/beaker/" rel="nofollow">here</a></p>
1
2010-10-25T02:39:06Z
[ "python", "memcached", "pylons" ]
help!for function
1,738,362
<p>I have post the similar question before,but this time the problem is different,I got stuck with the following code..can anyone help with it?thanks in advance</p> <p>I have fixed mu code as suggested,thanks</p> <pre><code>from numpy import * #vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] ...
0
2009-11-15T18:27:25Z
1,738,373
<p>Put your inizialization <strong>outside</strong> the loop! Right now, you're resetting the arrays to all zeros <strong>each time through the loop</strong>, over and over, which is absurd. You also appear to have a typo -- you set vs twice and rs never -- so I've tried to guess what you meant.</p> <pre><code>from ...
2
2009-11-15T18:30:49Z
[ "python" ]
help!for function
1,738,362
<p>I have post the similar question before,but this time the problem is different,I got stuck with the following code..can anyone help with it?thanks in advance</p> <p>I have fixed mu code as suggested,thanks</p> <pre><code>from numpy import * #vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] ...
0
2009-11-15T18:27:25Z
1,738,398
<p>You do not start looping where the comment indicates it, but at the <code>for i in range(5):</code> line. Everything in the indented block (the "body of the for-loop") is done repeatedly for each <code>i</code>. So setting <code>vs</code> and <code>fs</code> to zero is done repeatedly, each time deleting what was ca...
0
2009-11-15T18:42:10Z
[ "python" ]
help!for function
1,738,362
<p>I have post the similar question before,but this time the problem is different,I got stuck with the following code..can anyone help with it?thanks in advance</p> <p>I have fixed mu code as suggested,thanks</p> <pre><code>from numpy import * #vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] ...
0
2009-11-15T18:27:25Z
1,738,474
<p>I don't know exactly what you want to print. If you want to print every matrix every time it is updated, then you're fine. But if you want to print the matrices after all the updates have been completed, then you should bring that print statement out of the for loop.</p> <p>This and what Alex and sth have said shou...
0
2009-11-15T19:07:07Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,406
<p>Maybe something like:</p> <pre><code>d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} result = [] for key, values in d.items(): result.append(([key]*len(values), values)) </code></pre>
2
2009-11-15T18:44:58Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,409
<pre><code>d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} res = [([x]*len(y), y) for x, y in d.iteritems()] </code></pre> <p>res will be a list of tuples, where the first element in the tuple is your list of x-values and second element in the tuple is your list f y-values</p>
3
2009-11-15T18:45:29Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,410
<p>the map function in python will allow this</p> <pre><code>x = [1,2,4] y = [1,24,2] c = zip(x,y) print c d = map(None,x,y) print d </code></pre> <p>check it out. This will give you</p> <pre><code>[(1, 1), (2, 24), (4, 2)] </code></pre> <p>In the case of zip(), if one of the lists are smaller then the others, valu...
-1
2009-11-15T18:45:32Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,411
<pre><code>for k, v in dictionary.iteritems(): x = [k] * len(v) y = v pyplot.plot(x, y) </code></pre>
13
2009-11-15T18:45:43Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,417
<p>Use this list comprehension:</p> <pre><code>[([k]*len(v), v) for k, v in D.iteritems()] </code></pre> <p>Here's an example of it being used:</p> <pre><code>&gt;&gt;&gt; from pprint import pprint &gt;&gt;&gt; D = {1: [1.2, 2.3, 4.9, 2.0], 2: [4.1, 5.1, 6.3], 3: [4.9, 6.8, 9.5, 1.1, 7.1]} &gt;&gt;&gt; LL = [([k]*le...
2
2009-11-15T18:47:47Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,429
<p>I guess that a wizard will put something nicer, but I would do something like:</p> <pre><code>map(lambda x: ([x]*len(a[x]),a[x]),a) </code></pre> <p>for a tuple, or </p> <pre><code>map(lambda x: [[x]*len(a[x]),a[x]],a) </code></pre> <p>for a list.</p> <p>btw: <code>a</code> is the dictionary, of course!</p> <p...
0
2009-11-15T18:51:47Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,446
<pre><code>&gt;&gt;&gt; d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} &gt;&gt;&gt; result = [ ([k] * len(d[k]), d[k]) for k in d.keys() ] &gt;&gt;&gt; print result [([1, 1, 1, 1], [1.2, 2.2999999999999998, 4.9000000000000004, 2.0]), ([2, 2, 2], [4.0999999999999996, 5.099999999999999...
1
2009-11-15T18:56:56Z
[ "python" ]
Python: how to make two lists from a dictionary
1,738,392
<p>I have a dictionary.</p> <pre><code>{1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} </code></pre> <p>I want to pass each key:value pair to an instance of <code>matplotlib.pyplot</code> as two lists: x values and y values.</p> <p>Each key is an x value associated with each item in ...
4
2009-11-15T18:37:37Z
1,738,459
<p>As a list comprehension:</p> <pre><code>r = [([k]*len(v), v) for k,v in d.items()] </code></pre> <p>If your dictionary is very large, you'd want to use a generator expression:</p> <pre><code>from itertools import repeat r = ((repeat(k, len(v)), v) for k,v in d.iteritems()) </code></pre> <p>...though note that us...
2
2009-11-15T19:01:36Z
[ "python" ]
Count the number of occurences of letters in a Python string
1,738,452
<p>So I got a DNA sequence.</p> <pre><code>ACCAGAGCGGCACAGCAGCGACATCAGCACTAGCACTAGCATCAGCATCAGCATCAGC CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT ACACCCCCCCCGGTGTGTGTGGGGGGTTAAAAATGATGAGTGATGAGTGAGTTGTGTG CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT TTCTATCATCATTCGGCGGGGGGATATATTATAGCGCGCG...
0
2009-11-15T18:58:15Z
1,738,456
<pre><code>for base in 'ACGT': print base, thesequence.count(base) + thesequence.count(base.lower()) </code></pre>
7
2009-11-15T19:00:05Z
[ "python", "string" ]
How do I import the render_to_response method from Django 1.1 inside of Google App Engine?
1,738,466
<p>I'm a Python newbie, so I'm sure this is easy. Here's the code in my main.py:</p> <pre><code>import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from google.appengine.dist import use_library use_library('django', '1.1') # Use django form library. from django import forms from django.shortcuts import rend...
2
2009-11-15T19:04:37Z
1,738,571
<p>Well, <code>render_to_response</code> <a href="http://docs.djangoproject.com/en/dev/intro/tutorial03/#write-views-that-actually-do-something" rel="nofollow">is a shortcut for this</a>, so give this a try:</p> <pre><code>from django.template import Context, loader from django.http import HttpResponse def render_to_...
2
2009-11-15T19:42:36Z
[ "python", "django", "google-app-engine" ]
How do I import the render_to_response method from Django 1.1 inside of Google App Engine?
1,738,466
<p>I'm a Python newbie, so I'm sure this is easy. Here's the code in my main.py:</p> <pre><code>import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from google.appengine.dist import use_library use_library('django', '1.1') # Use django form library. from django import forms from django.shortcuts import rend...
2
2009-11-15T19:04:37Z
1,857,592
<p>In this case the problem ended up being that Google App Engine uses version 0.96 of Django and it actually couldn't find the 'redirect' method, because that's only in Django 1.1. If you use the GAE utility method 'use_library' you can specify what version of the Django framework you want to use and this problem goe...
0
2009-12-07T03:07:03Z
[ "python", "django", "google-app-engine" ]
How to make logging.debug work on Appengine?
1,738,494
<p>I'm having a tough time getting the logging on Appengine working. the statement</p> <pre><code>import logging </code></pre> <p>is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an Eclipse issue, so I tried calling </p> <pre><code>logging.debug("My debug statement")...
2
2009-11-15T19:18:41Z
1,738,500
<p>I have experience only with the Java App Engine, but there they set things up to only log WARN and ERROR. Try using one of those and see if you start getting output!</p> <p>I'm sure there are ways to loosen the filter, but I wouldn't know how to do it in Python.</p>
1
2009-11-15T19:22:23Z
[ "python", "google-app-engine", "logging" ]
How to make logging.debug work on Appengine?
1,738,494
<p>I'm having a tough time getting the logging on Appengine working. the statement</p> <pre><code>import logging </code></pre> <p>is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an Eclipse issue, so I tried calling </p> <pre><code>logging.debug("My debug statement")...
2
2009-11-15T19:18:41Z
1,738,514
<p>The problem is most likely with the setup of the dev_appserver. Try running it with the -d flag to turn on debugging.</p>
3
2009-11-15T19:27:57Z
[ "python", "google-app-engine", "logging" ]
How to make logging.debug work on Appengine?
1,738,494
<p>I'm having a tough time getting the logging on Appengine working. the statement</p> <pre><code>import logging </code></pre> <p>is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an Eclipse issue, so I tried calling </p> <pre><code>logging.debug("My debug statement")...
2
2009-11-15T19:18:41Z
26,037,164
<p>Use the command option <code>--log_level</code></p> <pre><code>$ dev_appserver.py --host 127.0.0.1 --log_level=debug . </code></pre>
0
2014-09-25T11:12:48Z
[ "python", "google-app-engine", "logging" ]
How to read this file using Python?
1,738,524
<p>I have a DNA file in the following format:</p> <pre><code>&gt;gi|5524211|gb|AAD44166.1| cytochrome ACCAGAGCGGCACAGCAGCGACATCAGCACTAGCACTAGCATCAGCATCAGCATCAGC CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT ACACCCCCCCCGGTGTGTGTGGGGGGTTAAAAATGATGAGTGATGAGTGAGTTGTGTG CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCA...
1
2009-11-15T19:32:44Z
1,738,538
<p>If there's always only one line of header :</p> <pre><code>dnalines = text.split('\n')[1:] dna = ''.join(dnalines) </code></pre> <p>With text = the contents of your file (for example, <code>text = open('yourfile').read()</code>)</p>
8
2009-11-15T19:34:42Z
[ "python", "regex" ]
How to read this file using Python?
1,738,524
<p>I have a DNA file in the following format:</p> <pre><code>&gt;gi|5524211|gb|AAD44166.1| cytochrome ACCAGAGCGGCACAGCAGCGACATCAGCACTAGCACTAGCATCAGCATCAGCATCAGC CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT ACACCCCCCCCGGTGTGTGTGGGGGGTTAAAAATGATGAGTGATGAGTGAGTTGTGTG CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCA...
1
2009-11-15T19:32:44Z
1,738,604
<p>I did some tests, and it appears that the following is more efficient than <a href="http://stackoverflow.com/questions/1738524/how-to-read-this-file-using-python/1738538#1738538">delroth's answer</a>:</p> <pre><code>text.split('\n', 1)[1].replace('\n', '') </code></pre> <p><strong>Edit:</strong> wait, it's not so ...
3
2009-11-15T19:52:48Z
[ "python", "regex" ]
More pythonic way to find a complementary DNA strand
1,738,633
<p>Here's what I have, but it seems kind of redundant. Maybe someone more experienced in Python knows of a way to clean this up? Should be pretty self explanatory what it does.</p> <pre><code>def complementary_strand(self, strand): ''' Takes a DNA strand string and finds its opposite base pair match. ''' ...
2
2009-11-15T20:02:05Z
1,738,644
<p>Even better would be to craft a generator, instead:</p> <pre><code>TRANS = { "T": "A", "A": "T", "G": "C", "C": "G" } def complementary_strand(self, strand): for base in strand.upper(): yield TRANS[base] </code></pre> <p>Then you can use it any way you want, and an iterator is more efficient:</p> <pr...
6
2009-11-15T20:06:16Z
[ "python" ]
More pythonic way to find a complementary DNA strand
1,738,633
<p>Here's what I have, but it seems kind of redundant. Maybe someone more experienced in Python knows of a way to clean this up? Should be pretty self explanatory what it does.</p> <pre><code>def complementary_strand(self, strand): ''' Takes a DNA strand string and finds its opposite base pair match. ''' ...
2
2009-11-15T20:02:05Z
1,738,648
<p>Something like</p> <pre><code>def complementary_strand(self, strand): return strand.upper().translate(maketrans("TAGC", "ATCG")) </code></pre>
3
2009-11-15T20:07:10Z
[ "python" ]
More pythonic way to find a complementary DNA strand
1,738,633
<p>Here's what I have, but it seems kind of redundant. Maybe someone more experienced in Python knows of a way to clean this up? Should be pretty self explanatory what it does.</p> <pre><code>def complementary_strand(self, strand): ''' Takes a DNA strand string and finds its opposite base pair match. ''' ...
2
2009-11-15T20:02:05Z
1,738,653
<p>Probably the most efficient way to do it, if the string is long enough:</p> <pre><code>import string def complementary_strand(self, strand): return strand.translate(string.maketrans('TAGCtagc', 'ATCGATCG')) </code></pre> <p>This is making use of the <a href="http://docs.python.org/library/string.html#string.t...
12
2009-11-15T20:09:05Z
[ "python" ]
More pythonic way to find a complementary DNA strand
1,738,633
<p>Here's what I have, but it seems kind of redundant. Maybe someone more experienced in Python knows of a way to clean this up? Should be pretty self explanatory what it does.</p> <pre><code>def complementary_strand(self, strand): ''' Takes a DNA strand string and finds its opposite base pair match. ''' ...
2
2009-11-15T20:02:05Z
1,738,664
<pre><code>def __init__(self, *args): # ... original __init__ method, and: self.trans = { "T": "A", "A": "T", "G": "C", "C": "G" } def complementary_strand(self, strand): '''Takes a DNA strand string and returns its opposite base pair match.''' return ''.join([self.trans[base] for base in strand.upper(...
2
2009-11-15T20:12:19Z
[ "python" ]
Python: Convert string into function name; getattr or equal?
1,738,687
<p>I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file:</p> <pre><code>... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STR...
4
2009-11-15T20:19:14Z
1,738,700
<p>Usually you just use a dict and store <code>(func_name, function)</code> pairs:</p> <pre><code>unpack_options = { 'unpack_pdb_line' : unpack_pdb_line, 'some_other' : some_other_function } unpack_function = unpack_options[options.unpack_method] </code></pre>
8
2009-11-15T20:24:01Z
[ "python", "function", "getattr" ]
Python: Convert string into function name; getattr or equal?
1,738,687
<p>I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file:</p> <pre><code>... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STR...
4
2009-11-15T20:19:14Z
1,738,769
<p>If you are taking input from a user, for the sake of security it is probably best to use a hand-made dict which will accept only a well-defined set of admissible user inputs:</p> <pre><code>unpack_options = { 'unpack_pdb_line' : unpack_pdb_line, 'unpack_pdb_line2' : unpack_pdb_line2, } </code></pre> <p>Ign...
1
2009-11-15T20:48:37Z
[ "python", "function", "getattr" ]
Python: Convert string into function name; getattr or equal?
1,738,687
<p>I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file:</p> <pre><code>... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STR...
4
2009-11-15T20:19:14Z
1,738,777
<p>If you want to exploit the dictionaries (&amp;c) that Python's already keeping on your behalf, I'd suggest:</p> <pre><code>def str2fun(astr): module, _, function = astr.rpartition('.') if module: __import__(module) mod = sys.modules[module] else: mod = sys.modules['__main__'] # or whatever's the ...
4
2009-11-15T20:50:31Z
[ "python", "function", "getattr" ]
Python: Convert string into function name; getattr or equal?
1,738,687
<p>I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file:</p> <pre><code>... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STR...
4
2009-11-15T20:19:14Z
1,739,054
<pre><code>function = eval_dottedname(name if '.' in name else "%s.%s" % (__name__, name)) </code></pre> <p>Where <code>eval_dottedname()</code>:</p> <pre><code>def eval_dottedname(dottedname): """ &gt;&gt;&gt; eval_dottedname("os.path.join") #doctest: +ELLIPSIS &lt;function join at 0x...&gt; &gt;&gt;...
1
2009-11-15T22:14:53Z
[ "python", "function", "getattr" ]
Python: Convert string into function name; getattr or equal?
1,738,687
<p>I am editing PROSS.py to work with .cif files for protein structures. Inside the existing PROSS.py, there is the following functions (I believe that's the correct name if it's not associated with any class?), just existing within the .py file:</p> <pre><code>... def unpack_pdb_line(line, ATOF=_atof, ATOI=_atoi, STR...
4
2009-11-15T20:19:14Z
1,761,164
<p><code>vars()["unpack_pdb_line"]()</code> will work too.</p> <p>or </p> <p>globals() or locals() will also work similar way.</p> <pre><code>&gt;&gt;&gt; def a():return 1 &gt;&gt;&gt; &gt;&gt;&gt; vars()["a"] &lt;function a at 0x009D1230&gt; &gt;&gt;&gt; &gt;&gt;&gt; vars()["a"]() 1 &gt;&gt;&gt; locals()["a"]() 1 &...
2
2009-11-19T05:51:37Z
[ "python", "function", "getattr" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,738,798
<p>Chinese doesn't usually have whitespace between words, and the symbols can have different meanings depending on context. You will have to understand the text in order to split it at a word boundary. In other words, what you are trying to do is not easy in general.</p>
7
2009-11-15T20:57:22Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,738,833
<p>For word segmentation in Chinese, and other advanced tasks in processing natural language, consider <a href="http://nltk.googlecode.com/svn/trunk/doc/book/ch03.html#word-segmentation">NLTK</a> as a good starting point if not a complete solution -- it's a rich Python-based toolkit, particularly good for learning abou...
5
2009-11-15T21:05:37Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,738,898
<p>This punts the word-breaking decision to the re module, but it may work well enough for you.</p> <pre><code>import re def shorten(tweet, footer="", limit=140): """Break tweet into two pieces at roughly the last word break before limit. """ lower_break_limit = limit / 2 # limit under which to as...
0
2009-11-15T21:27:15Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,745,300
<p>After speaking with some native Cantonese, Mandarin, and Japanese speakers it seems that the correct thing to do is hard, but my current algorithm still makes sense to them in the context of internet posts. </p> <p>Meaning, they are used to the "split on space and add … at the end" treatment.</p> <p>So I'm going...
1
2009-11-16T22:33:44Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,745,334
<p>the <a href="http://docs.python.org/library/re.html#re.U" rel="nofollow"><code>re.U</code> flag</a> will treat <code>\s</code> according to the Unicode character properties database.</p> <p>The given string, however, doesn't apparently contain any white space characters according to python's unicode database:</p> ...
3
2009-11-16T22:43:39Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
1,745,367
<p>Save two characters and use an elipsis (<code>…</code>, <a href="http://www.fileformat.info/info/unicode/char/2026/index.htm" rel="nofollow">0x2026</a>) instead of three dots!</p>
-1
2009-11-16T22:49:44Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
2,106,679
<p>I tried out the solution with PyAPNS for push notifications and just wanted to share what worked for me. The issue I had is that truncating at 256 bytes in UTF-8 would result in the notification getting dropped. I had to make sure the notification was encoded as "unicode_escape" to get it to work. I'm assuming th...
2
2010-01-21T03:19:52Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python: Split unicode string on word boundaries
1,738,788
<p>I need to take a string, and shorten it to 140 characters.</p> <p>Currently I am doing:</p> <pre><code>if len(tweet) &gt; 140: tweet = re.sub(r"\s+", " ", tweet) #normalize space footer = "… " + utils.shorten_urls(post['url']) avail = 140 - len(footer) words = tweet.split() result = "" fo...
10
2009-11-15T20:53:40Z
9,124,527
<p>Basically, in CJK (Except Korean with spaces), you need dictionary look-ups to segment words properly. Depending on your exact definition of "word", Japanese can be more difficult than that, since not all inflected variants of a word (i.e. "行こう" vs. "行った") will appear in the dictionary. Whether it's wo...
1
2012-02-03T06:24:07Z
[ "python", "unicode", "internationalization", "character-properties" ]
Python and iPhone development on Mac - minimum/recommended hardware?
1,738,845
<p>What is the minimum configuration to do some Python and iPhone development on Mac ?</p> <ul> <li>Platform wise: Mac Mini, Mac Pro, Mac Book, Mac Book Pro ?</li> <li>Memory requirement</li> <li>CPU speed</li> </ul> <p>Thanks for your advice.</p> <p>Laurent</p>
0
2009-11-15T21:12:24Z
1,738,867
<p>Any of those platforms are going to be more than adequate for iPhone development, but since Apple is not allowing anything that requires a VM or an interpreted environment, there is no way to do iPhone development using Python at this time.</p> <p>EDIT: Looks like I misread that as doing Python development on an iP...
4
2009-11-15T21:19:58Z
[ "iphone", "python", "osx" ]
Python and iPhone development on Mac - minimum/recommended hardware?
1,738,845
<p>What is the minimum configuration to do some Python and iPhone development on Mac ?</p> <ul> <li>Platform wise: Mac Mini, Mac Pro, Mac Book, Mac Book Pro ?</li> <li>Memory requirement</li> <li>CPU speed</li> </ul> <p>Thanks for your advice.</p> <p>Laurent</p>
0
2009-11-15T21:12:24Z
1,738,881
<p>The minimum requirement is an intel mac. <strong>Any</strong> intel mac will work. iPhone development is unsupported on PPC.</p> <p>Python can be done on any mac that runs os x.</p> <p>The minimum requirement, and what's pleasant are different things. Everything you've listed will work pretty great. You might want...
4
2009-11-15T21:22:33Z
[ "iphone", "python", "osx" ]
Python and iPhone development on Mac - minimum/recommended hardware?
1,738,845
<p>What is the minimum configuration to do some Python and iPhone development on Mac ?</p> <ul> <li>Platform wise: Mac Mini, Mac Pro, Mac Book, Mac Book Pro ?</li> <li>Memory requirement</li> <li>CPU speed</li> </ul> <p>Thanks for your advice.</p> <p>Laurent</p>
0
2009-11-15T21:12:24Z
1,738,884
<p>Rather ephemeral in our requirements, aren't we. 'Some' python/iPhone....</p> <p>You will be well served by a mini, starting @ $600, they're a steal. Upgrade the processor (2.53ghz), add RAM to 4GB (after purchase, if you're comfortable) throw a generic keyboard, mouse and Monitor on it and you've got a heck of a...
1
2009-11-15T21:22:59Z
[ "iphone", "python", "osx" ]
Python and iPhone development on Mac - minimum/recommended hardware?
1,738,845
<p>What is the minimum configuration to do some Python and iPhone development on Mac ?</p> <ul> <li>Platform wise: Mac Mini, Mac Pro, Mac Book, Mac Book Pro ?</li> <li>Memory requirement</li> <li>CPU speed</li> </ul> <p>Thanks for your advice.</p> <p>Laurent</p>
0
2009-11-15T21:12:24Z
1,738,885
<p>Any system that Apple sells is sufficient to do Python and iPhone development on the Mac. I generally consider 2 GB to be the minimum amount of RAM that I would want to use, and 4 GB if I'm going to be doing any significant amount of work in a VM (for instance, VMware Fusion or Parallels for running Windows within M...
1
2009-11-15T21:23:03Z
[ "iphone", "python", "osx" ]
What is a good option for a Non-Blocking "Database" (Using a TCP Socket Server)
1,739,100
<p>In short I'm creating a socket server so I can add multiplayer support to my Flash game (Using Actionscript 3.0 Binary Socket on the Client-End).</p> <p>I decided to go with Python since I'm the sole developer of the game/server and this will be my first non-blocking socket server. I was going to use Twisted but I...
4
2009-11-15T22:32:21Z
1,739,187
<p>I suggest you use any DB you like (<a href="http://docs.python.org/library/sqlite3.html">sqlite</a> comes with Python and will probably serve you well!): just have a dedicated thread that accesses said DB, taking requests from a <a href="http://docs.python.org/library/queue.html?highlight=queue#Queue.Queue">Queue</a...
6
2009-11-15T22:59:59Z
[ "python", "database", "sockets" ]
What is a good option for a Non-Blocking "Database" (Using a TCP Socket Server)
1,739,100
<p>In short I'm creating a socket server so I can add multiplayer support to my Flash game (Using Actionscript 3.0 Binary Socket on the Client-End).</p> <p>I decided to go with Python since I'm the sole developer of the game/server and this will be my first non-blocking socket server. I was going to use Twisted but I...
4
2009-11-15T22:32:21Z
1,739,287
<p>Blocking (in traditional database terms, it's "locking") is inevitable if you want any form of consistency in your data.</p> <p>Consistency as in "it is impossible for two gamers with distinct scores to both be declared as the winner".</p> <p>Reducing the locking to a minimum is precisely one of those things in da...
1
2009-11-15T23:31:37Z
[ "python", "database", "sockets" ]
Python: Architecture for url polling and posting
1,739,185
<p>I have a simple problem. I have to fetch a url (about once a minute), check if there is any new content, and if there is, post it to another url.</p> <p>I have a working system with a cronjob every minute that basically:</p> <pre><code>for link in models.Link.objects.filter(enabled=True).select_related(): # do...
0
2009-11-15T22:58:56Z
1,739,214
<p>Simplest: use a long-running process with <a href="http://docs.python.org/library/sched.html?highlight=sched#module-sched" rel="nofollow">sched</a> (on its own thread) to handle the scheduling -- by posting requests to a <a href="http://docs.python.org/library/queue.html?highlight=queue#Queue.Queue" rel="nofollow">Q...
2
2009-11-15T23:08:06Z
[ "python", "architecture", "concurrency" ]
Allow / in django url
1,739,431
<p>I want to make a wiki, and i must assign for each url a view. Each url can contain letters (<code>A-Z, a-z</code>), digits and punctuation (<code>'.', ',', '/', '-', '_'</code>). How can I make the expression ?</p> <p>I want something like this :</p> <pre><code>(r'^(?P&lt;wiki_page&gt;\w+)/$', 'www.wiki.views.page...
2
2009-11-16T00:19:49Z
1,739,447
<p>You could replace \w in the regex with a regex to match what you're looking for. Perhaps</p> <pre><code>(r'^(?P&lt;wiki_page&gt;[A-Za-z_/,\.-]+)$, 'www.wiki.views.page') </code></pre> <p>or similar.</p>
1
2009-11-16T00:25:22Z
[ "python", "regex", "django", "url" ]
Allow / in django url
1,739,431
<p>I want to make a wiki, and i must assign for each url a view. Each url can contain letters (<code>A-Z, a-z</code>), digits and punctuation (<code>'.', ',', '/', '-', '_'</code>). How can I make the expression ?</p> <p>I want something like this :</p> <pre><code>(r'^(?P&lt;wiki_page&gt;\w+)/$', 'www.wiki.views.page...
2
2009-11-16T00:19:49Z
1,739,452
<p>Try this regexp:</p> <pre><code>r'^(?P&lt;wiki_page&gt;[\w.,/_\-]+)/$' </code></pre>
4
2009-11-16T00:26:16Z
[ "python", "regex", "django", "url" ]
Allow / in django url
1,739,431
<p>I want to make a wiki, and i must assign for each url a view. Each url can contain letters (<code>A-Z, a-z</code>), digits and punctuation (<code>'.', ',', '/', '-', '_'</code>). How can I make the expression ?</p> <p>I want something like this :</p> <pre><code>(r'^(?P&lt;wiki_page&gt;\w+)/$', 'www.wiki.views.page...
2
2009-11-16T00:19:49Z
1,739,453
<p>I think it's more of a regex question</p> <pre><code>r'^(?P&lt;wiki_page&gt;[\w\.,_/\-]+)/$' </code></pre> <p>you can build character classes on top of existing ones</p> <p>I think you have to escape the dash, or put it last, because dash defines range of characters and you may get a very unexpected side-effect o...
0
2009-11-16T00:26:20Z
[ "python", "regex", "django", "url" ]
Compress data before storage on Google App Engine
1,739,543
<p>I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I would like to compress the file before upload and decompress the file every time it is requested. How would you sugge...
1
2009-11-16T00:55:28Z
1,739,587
<p>As Aneto mentions in a comment, you will not be able to compress MP3 data with a standard compression library like gzip or zlib. However, you could reencode the MP3 at a <strong>MUCH</strong> lower bitrate, possible with <a href="http://lame.sourceforge.net/" rel="nofollow">LAME</a>.</p>
0
2009-11-16T01:20:39Z
[ "python", "google-app-engine", "compression", "gzip", "zlib" ]
Compress data before storage on Google App Engine
1,739,543
<p>I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I would like to compress the file before upload and decompress the file every time it is requested. How would you sugge...
1
2009-11-16T00:55:28Z
1,739,598
<p>"Compressing <strong>before</strong> upload" implies doing it in the user's browser -- but no text in your question addresses that! It seems to be about compression <em>in your GAE app</em>, where of course the data will only be <strong>after</strong> the upload. You could do it with a Firefox extension (or other ...
2
2009-11-16T01:23:50Z
[ "python", "google-app-engine", "compression", "gzip", "zlib" ]
Compress data before storage on Google App Engine
1,739,543
<p>I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I would like to compress the file before upload and decompress the file every time it is requested. How would you sugge...
1
2009-11-16T00:55:28Z
1,739,968
<p>While the technical limitations (mentioned in other answers) of compressing MP3 files via standard compression or reencoding at a lower bitrate are correct, your aim is to store <strong>30 seconds</strong> of MP3 encoded data. Assuming that you can enforce that on your users, you should be alright without applying a...
2
2009-11-16T03:55:14Z
[ "python", "google-app-engine", "compression", "gzip", "zlib" ]
Compress data before storage on Google App Engine
1,739,543
<p>I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I would like to compress the file before upload and decompress the file every time it is requested. How would you sugge...
1
2009-11-16T00:55:28Z
1,996,744
<p>You could try the new Blobstore API allowing the storage and serving of files up to 50MB</p> <p><a href="http://www.cloudave.com/link/the-new-google-app-engine-blobstore-api-first-thoughts" rel="nofollow">http://www.cloudave.com/link/the-new-google-app-engine-blobstore-api-first-thoughts</a></p> <p><a href="http:/...
2
2010-01-03T23:20:23Z
[ "python", "google-app-engine", "compression", "gzip", "zlib" ]
Compress data before storage on Google App Engine
1,739,543
<p>I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I would like to compress the file before upload and decompress the file every time it is requested. How would you sugge...
1
2009-11-16T00:55:28Z
2,125,539
<p>You can store up to 10Mb with a list of Blobs. Search for <code>google file service</code>. It's much more versatile than BlobStore in my opinion, since I just started using BlobStore Api yesterday and I'm still figuring out if it is possible to access the data bytewise.. as in changing doc to pdf, jpeg to gif.. </p...
0
2010-01-24T01:37:48Z
[ "python", "google-app-engine", "compression", "gzip", "zlib" ]
How to translate python tuple unpacking to Matlab?
1,739,566
<p>I am translating some python code to Matlab, and want to figure out what the best way to translate the python tuple unpacking to Matlab is.</p> <p>For the purposes of this example, a <code>Body</code> is a class whose constructor takes as input two functionals.</p> <p>I have the following python code:</p> <pre><c...
3
2009-11-16T01:08:33Z
1,739,681
<p>Without knowing what <code>Body</code> is, this is my solution:</p> <pre><code>bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords); </code></pre> <p>or, if the output has to encapsulated in a cell array:</p> <pre><code>bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords, 'UniformOutput',false); </code>...
2
2009-11-16T02:06:21Z
[ "python", "matlab", "iterable-unpacking" ]
How to translate python tuple unpacking to Matlab?
1,739,566
<p>I am translating some python code to Matlab, and want to figure out what the best way to translate the python tuple unpacking to Matlab is.</p> <p>For the purposes of this example, a <code>Body</code> is a class whose constructor takes as input two functionals.</p> <p>I have the following python code:</p> <pre><c...
3
2009-11-16T01:08:33Z
1,739,960
<p>It appears that <a href="http://stackoverflow.com/questions/1739566/how-to-translate-python-tuple-unpacking-to-matlab/1739681#1739681">Amro's answer</a> will work for you. However, if you don't really need or want to create a new <code>Body</code> class, there's a straight-forward way to construct an array of struct...
0
2009-11-16T03:52:08Z
[ "python", "matlab", "iterable-unpacking" ]
What is the max length of a python string?
1,739,913
<p>If it is environment-independent, what is the theoretical maximum number of characters in a python string?</p> <p>Also if it differs with version number I'd like to know it for python 2.5.2</p>
39
2009-11-16T03:37:28Z
1,739,928
<p>With a 64-bit Python installation, and (say) 64 GB of memory, a Python 2 string of around 63 GB should be quite feasible (if not maximally fast). If you can upgrade your memory much beyond that (which will cost you an arm and a leg, of course), your maximum feasible strings should get proportionally longer. (I don...
54
2009-11-16T03:42:33Z
[ "python", "string" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
1,739,931
<p>If Y is a module (and X a package) <code>reload(Y)</code> will be fine -- otherwise, you'll see why good Python style guides (such as my employer's) say to <strong>never</strong> import anything <strong>except</strong> a module (this is one out of many great reasons -- yet people still keep importing functions and c...
24
2009-11-16T03:44:48Z
[ "python", "python-import" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
1,740,326
<ol> <li><code>reload()</code> module <code>X</code>,</li> <li><code>reload()</code> module importing <code>Y</code> from <code>X</code>.</li> </ol> <p>Note that reloading won't change already created objects bound in other namespaces (even if you follow style guide from Alex).</p>
1
2009-11-16T05:58:28Z
[ "python", "python-import" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
1,740,401
<p>First off, you shouldn't be using reload at all, if you can avoid it. But let's assume you have your reasons (i.e. debugging inside IDLE).</p> <p>Reloading the library won't get the names back into the module's namespace. To do this, just reassign the variables:</p> <pre><code>f = open('zoo.py', 'w') f.write("snak...
7
2009-11-16T06:21:51Z
[ "python", "python-import" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
11,724,154
<h1>Answer</h1> <p>From my tests. The marked answer, which suggests a simple <code>reload(X)</code>, does not work.</p> <p>From what I can tell the correct answer is:</p> <pre><code>import X reload( X ) from X import Y </code></pre> <hr> <h1>Test</h1> <p>My test was the following (Python 2.6.5 + bpython 0.9.5.2)<...
29
2012-07-30T15:04:18Z
[ "python", "python-import" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
22,073,792
<pre><code>from modulename import func import sys reload(sys.modules['modulename']) </code></pre>
2
2014-02-27T16:00:57Z
[ "python", "python-import" ]
Python: reload component Y imported with 'from X import Y'?
1,739,924
<p>In Python, once I have imported a module X in an interpreter session using <code>import X</code>, and the module changes on the outside, I can reload the module with <code>reload(X)</code>. The changes then become available in my interpreter session.</p> <p>I am wondering if this also possible when I import a compo...
39
2009-11-16T03:41:28Z
38,998,365
<p>If you want to do this:</p> <pre><code>from mymodule import myobject </code></pre> <p>Do this instead:</p> <pre><code>import mymodule myobject=mymodule.myobject </code></pre> <p>You can now use myobject in the same way as you were planning (without the tiresome unreadable mymodule references everywhere).</p> <p...
0
2016-08-17T13:41:52Z
[ "python", "python-import" ]
Google Eclipse Plugin - Work with Python
1,739,969
<p>Does the Google Eclipse Plugin work at all with Pydev or is it only useful if I am developing in Java?</p>
3
2009-11-16T03:56:04Z
1,740,109
<p>You can, indeed, use Eclipse (and PyDev) for Python development for App Engine -- see <a href="http://code.google.com/appengine/articles/eclipse.html" rel="nofollow">here</a> for more, for example!</p>
1
2009-11-16T04:48:35Z
[ "python", "eclipse", "google-app-engine" ]
Google Eclipse Plugin - Work with Python
1,739,969
<p>Does the Google Eclipse Plugin work at all with Pydev or is it only useful if I am developing in Java?</p>
3
2009-11-16T03:56:04Z
1,741,343
<p>The App Engine eclipse plugin is for Java development. As Casebash points out, though Pydev now has <a href="http://pydev.blogspot.com/2009/05/testing-on-pydev-146-google-app-engine.html" rel="nofollow">App Engine integration</a>.</p>
3
2009-11-16T10:37:12Z
[ "python", "eclipse", "google-app-engine" ]
Elixir not creating my tables with default values
1,740,040
<pre><code>class MyObject(Entity): name = Field(Unicode(256), default=u'default name', nullable=False) using_options(shortnames=True) using_mapper_options(save_on_init=False) def __init__(self): self.name = None </code></pre> <p>I am using MySQL in this case, but have also checked against S...
1
2009-11-16T04:27:44Z
1,740,190
<p><code>default</code> keyword argument in SQLAlchemy is for Python runtime default value, it's used for INSERT statements. Use <code>PassiveDefault()</code> object as positional argument when you really need database level default. </p>
2
2009-11-16T05:13:59Z
[ "python", "sqlalchemy", "python-elixir" ]
Convert flat data into a hierarchical python list
1,740,107
<p>I have a data model from my database. This is a flat python list sorted by left values.</p> <pre><code>&gt; id name left right &gt; 1 Beginning 1 6 &gt; 2 FOO 2 5 &gt; 3 BAR 3 4 &gt; 4 Programming 6 13 &gt; 5 Python 7 8 &gt; 7 C# 9 12 &gt; 8 XNA 10 11 &gt; 6 About 14 15...
2
2009-11-16T04:48:00Z
1,744,019
<p>This is a modified pre-order tree traversal.</p> <p><a href="http://www.sitepoint.com/print/hierarchical-data-database/" rel="nofollow">http://www.sitepoint.com/print/hierarchical-data-database/</a></p> <p>So the input looks like this, a list of dictionaries.</p> <pre><code>dbrows = [ {'title': 'Food', 'lft': ...
0
2009-11-16T18:37:58Z
[ "python", "hierarchical" ]
Documents and examples of PythonMagick
1,740,158
<p>Where can I find the document and examples of <a href="http://wiki.python.org/moin/PythonMagick">PythonMagick</a>?</p> <p>I did a search on Google but no much information was found.</p>
18
2009-11-16T05:04:08Z
1,741,209
<p>It is a bindings to the <a href="http://www.imagemagick.org/script/magick-wand.php">MagickWand API</a> : <a href="http://www.assembla.com/wiki/show/pythonmagickwand">http://www.assembla.com/wiki/show/pythonmagickwand</a></p> <p>So you can use all of the MagickWand API functions.</p> <pre><code> #!/usr/bin/python ...
-6
2009-11-16T10:04:41Z
[ "python", "imagemagick" ]
Documents and examples of PythonMagick
1,740,158
<p>Where can I find the document and examples of <a href="http://wiki.python.org/moin/PythonMagick">PythonMagick</a>?</p> <p>I did a search on Google but no much information was found.</p>
18
2009-11-16T05:04:08Z
5,188,661
<p>I could not find them anywhere either but this is how I went about using it anyway.</p> <p>Example</p> <pre><code>import PythonMagick image = PythonMagick.Image("sample_image.jpg") print image.fileName() print image.magick() print image.size().width() print image.size().height() </code></pre> <p>With output like ...
24
2011-03-04T01:24:21Z
[ "python", "imagemagick" ]
Documents and examples of PythonMagick
1,740,158
<p>Where can I find the document and examples of <a href="http://wiki.python.org/moin/PythonMagick">PythonMagick</a>?</p> <p>I did a search on Google but no much information was found.</p>
18
2009-11-16T05:04:08Z
5,532,685
<p>To find out the methods type in Python:</p> <pre><code>import PythonMagick dir(PythonMagick.Image()) </code></pre> <p>Then you get an output like this:</p> <blockquote> <p><code>['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__ini...
14
2011-04-03T21:33:37Z
[ "python", "imagemagick" ]
Documents and examples of PythonMagick
1,740,158
<p>Where can I find the document and examples of <a href="http://wiki.python.org/moin/PythonMagick">PythonMagick</a>?</p> <p>I did a search on Google but no much information was found.</p>
18
2009-11-16T05:04:08Z
11,317,673
<p>From what I can tell, PythonMagick wrapps the <a href="http://www.imagemagick.org/Magick++/Documentation.html">Magick++ library</a>. I have been able to copy and paste C++ code using this library into python and it works as expected. Plus the names of the classes and member functions match (where as MagickWand seems...
14
2012-07-03T19:17:49Z
[ "python", "imagemagick" ]
Installing Trac with Subversion 1.6
1,740,165
<p>I'm trying to set up Trac on my server and have successfully installed it, compiled the bytecode and run the tracd server. The only problem is that it's not reading my SVN repository.</p> <p>The error I'm receiving is:</p> <blockquote> <p>Warning: Can't synchronize with the repository (Couldn't open Subversion r...
1
2009-11-16T05:05:59Z
1,740,182
<p>Is your python svn library updated? Sounds like it's stale.</p>
3
2009-11-16T05:12:04Z
[ "python", "svn", "installation", "trac" ]
Installing Trac with Subversion 1.6
1,740,165
<p>I'm trying to set up Trac on my server and have successfully installed it, compiled the bytecode and run the tracd server. The only problem is that it's not reading my SVN repository.</p> <p>The error I'm receiving is:</p> <blockquote> <p>Warning: Can't synchronize with the repository (Couldn't open Subversion r...
1
2009-11-16T05:05:59Z
1,740,188
<p>you should make sure the python binding match your SVN version. </p> <p>to get the binding you can use the SVN source and compile the wrapper, the <a href="http://svn.collab.net/repos/svn/trunk/subversion/bindings/swig/INSTALL" rel="nofollow">install</a> give an overview of the process how to build that binding. </...
3
2009-11-16T05:13:44Z
[ "python", "svn", "installation", "trac" ]
Installing Trac with Subversion 1.6
1,740,165
<p>I'm trying to set up Trac on my server and have successfully installed it, compiled the bytecode and run the tracd server. The only problem is that it's not reading my SVN repository.</p> <p>The error I'm receiving is:</p> <blockquote> <p>Warning: Can't synchronize with the repository (Couldn't open Subversion r...
1
2009-11-16T05:05:59Z
1,740,192
<p>It may be that the Subversion Python bindings are compiled against a too-old version of Subversion, but given FS format 2 is <a href="http://svn.collab.net/repos/svn/trunk/subversion/libsvn%5Frepos/repos.h" rel="nofollow">pre-1.0,</a> it's possible something else is wrong. You can get the Subversion client library ...
2
2009-11-16T05:15:10Z
[ "python", "svn", "installation", "trac" ]
Installing Trac with Subversion 1.6
1,740,165
<p>I'm trying to set up Trac on my server and have successfully installed it, compiled the bytecode and run the tracd server. The only problem is that it's not reading my SVN repository.</p> <p>The error I'm receiving is:</p> <blockquote> <p>Warning: Can't synchronize with the repository (Couldn't open Subversion r...
1
2009-11-16T05:05:59Z
1,761,339
<p>Looks like these guys answered your question pretty thoroughly but FYI: setup the log file so you can read it during your next problem. In your trac.ini somewhere:</p> <pre><code>[logging] log_file = trac.log # log_format = &lt;inherited&gt; log_level = DEBUG log_type = file </code></pre> <p>After that, try to de...
0
2009-11-19T06:46:58Z
[ "python", "svn", "installation", "trac" ]
Integrating a simple web server into a custom main loop in python?
1,740,235
<p>I have an application in python with a custom main loop (I don't believe the details are important). I'd like to integrate a simple non-blocking web server into the application which can introspect the application objects and possibly provide an interface to manipulate them. What's the best way to do this?</p> <p>I...
4
2009-11-16T05:24:49Z
1,740,378
<p><a href="http://docs.python.org/reference/compound%5Fstmts.html" rel="nofollow">Twisted</a> is designed to make stuff like that fairly simple</p> <pre><code>import time from twisted.web import server, resource from twisted.internet import reactor class Simple(resource.Resource): isLeaf = True def render_G...
7
2009-11-16T06:11:21Z
[ "python", "loops" ]
Integrating a simple web server into a custom main loop in python?
1,740,235
<p>I have an application in python with a custom main loop (I don't believe the details are important). I'd like to integrate a simple non-blocking web server into the application which can introspect the application objects and possibly provide an interface to manipulate them. What's the best way to do this?</p> <p>I...
4
2009-11-16T05:24:49Z
1,740,507
<p>I'd suggest creating a new thread and running a web server (such as Python's built-in SimpleHTTPServer or <a href="http://docs.python.org/library/basehttpserver.html" rel="nofollow">BaseHTTPServer</a>). Threads really aren't that scary when it comes down to it.</p> <pre><code>from threading import Event, Thread im...
-1
2009-11-16T06:55:31Z
[ "python", "loops" ]
Can i integrate Blender with Phanthom Omni
1,740,299
<p>I have some project about game programming, which have to use blender with phanthom ,Don't know possible to do it</p>
0
2009-11-16T05:48:18Z
1,740,306
<p>If you mean Phantom Omni, the haptic feedback device, and you mean Blender, the rendering program, then I don't see any reason why you can't use the API for both of them in a program, together to make a game.</p> <p>In short, if your questions means what I think it does, of course. </p>
0
2009-11-16T05:51:35Z
[ "python", "blender" ]
Python: Behavior of the garbage collector
1,740,394
<p>I have a Django application that exhibits some strange garbage collection behavior. There is one view in particular that will just keep growing the VM size significantly every time it is called - up to a certain limit, at which point usage drops back again. The problem is that it's taking considerable time until tha...
9
2009-11-16T06:20:25Z
1,740,929
<p>I think your analysis looks sound. I'm not an expert on the <code>gc</code>, so whenever I have a problem like this I just add a call to <code>gc.collect()</code> in an appropriate, non time critical place, and forget about it.</p> <p>I'd suggest you call <code>gc.collect()</code> in your view(s) and see what effe...
2
2009-11-16T09:01:06Z
[ "python", "django", "memory", "garbage-collection" ]
Python: Behavior of the garbage collector
1,740,394
<p>I have a Django application that exhibits some strange garbage collection behavior. There is one view in particular that will just keep growing the VM size significantly every time it is called - up to a certain limit, at which point usage drops back again. The problem is that it's taking considerable time until tha...
9
2009-11-16T06:20:25Z
1,741,165
<blockquote> <p>Does the above even make sense, or do I have to look for the problem elsewhere? Is it just an unfortunate accident that significant data is kept around for so long in this particular use case?</p> </blockquote> <p>Yes, it does make sense. And yes, there are other issues worth to consider. Django uses...
3
2009-11-16T09:55:18Z
[ "python", "django", "memory", "garbage-collection" ]
Python threading strange behaviour
1,740,485
<pre><code>from threading import * from time import * class MyThread(Thread): def __init__(self,x): self.x = x Thread.__init__(self) def run(self): sleep(2) print(self.x) if __name__=='__main__': threads = [] for i in range(5): threads.append(MyThread('Hello')) for i ...
0
2009-11-16T06:47:56Z
1,740,545
<p>I think you've found a bug in Python 3 -- I can reproduce your results running under the latest released Py 3.1, but with 2.6 the results are as expected (only five lines emitted either way, though the newlines can get bunched up surprisingly wrt the contents that's within the specs of threaded behavior). Please op...
0
2009-11-16T07:04:17Z
[ "python", "multithreading" ]
Python threading strange behaviour
1,740,485
<pre><code>from threading import * from time import * class MyThread(Thread): def __init__(self,x): self.x = x Thread.__init__(self) def run(self): sleep(2) print(self.x) if __name__=='__main__': threads = [] for i in range(5): threads.append(MyThread('Hello')) for i ...
0
2009-11-16T06:47:56Z
1,740,695
<p>It looks like you've run into the problem documented in Python bug tracker <a href="http://bugs.python.org/issue6750" rel="nofollow">issue 6750</a>. Fixes for the problem are checked in and will appear in the next maintenance release of Python 3.1, if there is one, or in Python 3.2.</p> <pre><code>$ python3.1 test...
2
2009-11-16T07:50:10Z
[ "python", "multithreading" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
1,740,514
<p>You would have to take the last row there is in the triangle and create the next one like this:</p> <ol> <li>Put a 1 at the start of the new row</li> <li>For every number in the last row except the last, calculate the sum of the number and its right neighbor and put it into the new row</li> <li>Put another 1 at the...
3
2009-11-16T06:57:24Z
[ "python" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
1,740,526
<p>Here's <strong><a href="http://ptri1.tripod.com/" rel="nofollow">the correct way</a></strong> to make pascal's triangle.</p> <p><strong><a href="http://ptri1.tripod.com/" rel="nofollow">http://ptri1.tripod.com/</a></strong></p> <p><strong><a href="http://en.wikipedia.org/wiki/Pascal%27s%5Ftriangle" rel="nofollow">...
3
2009-11-16T06:59:31Z
[ "python" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
11,004,439
<p>Actually, the next row is crossed axis sum of last row. For example, if the last row is [1, 1], the next row would be:</p> <pre><code> [1, 1] + [1, 1] ----------- = [1, 2, 1] [1, 2, 1] + [1, 2, 1] -------------- = [1, 3, 3, 1] </code></pre> <p>So, the loop body can be like this:</p> <pre><code>tri.appen...
2
2012-06-12T20:34:04Z
[ "python" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
11,916,798
<p>here is my solution to generate a pascal triangle</p> <pre><code>def factorial(x): return 1 if x == 0 else x * factorial(x - 1) def triangle(n): return [[factorial(i) / (factorial(j) * factorial(i - j)) for j in range(i + 1)] for i in range(n)] </code></pre>
0
2012-08-11T18:55:46Z
[ "python" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
14,124,260
<p>try the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.pascal.html#scipy.linalg.pascal" rel="nofollow">scipy pascal</a> module:</p> <pre><code>from scipy.linalg import pascal pascal(6, kind='lower') </code></pre> <p>output:</p> <pre><code>array([[ 1, 0, 0, 0, 0, 0], [ 1, 1, 0,...
1
2013-01-02T15:05:16Z
[ "python" ]
Pascal's triangle
1,740,499
<p>I am looking for a Pascals triangle using python script</p> <p>I have done till here and have no idea how to add on</p> <pre><code>numstr= raw_input("please enter the height:") height = int( ) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) &lt; height: </code></pre>
1
2009-11-16T06:51:23Z
18,429,441
<pre><code> // C++ code for pascal triangle #include&lt;stdio.h&gt; #include&lt;ctype.h&gt; #include&lt;conio.h&gt; #include&lt;time.h&gt; #include&lt;stdlib.h&gt; long unsigned int Factorial(long unsigned int Number) { long unsigned int Fact=0; if (Number==0) return (long unsigned int) 1; else { Fact=...
0
2013-08-25T13:23:28Z
[ "python" ]
Approaches to resize an uploaded image of unknown format with Python
1,740,682
<p>PIL is great for resizing an image 99% of time. But as there are some formats which PIL cannot handle, e.g. interlaced PNG images, I wonder is there any other libraries to work with as a supplement to PIL when we encounter a unsupported image.</p> <p>Besides interlaced PNG images, what other formats are not current...
1
2009-11-16T07:46:06Z
1,741,200
<p>You can use the binding python of imagemagick maybe : <a href="http://www.assembla.com/wiki/show/pythonmagickwand" rel="nofollow">MagickWand</a> You might also be interested in <a href="http://www.procoders.net/?p=39" rel="nofollow">PythonMagickWand</a></p>
1
2009-11-16T10:03:32Z
[ "python", "image", "python-imaging-library" ]
bytes vs bytearray in Python 2.6 and 3
1,740,696
<p>I'm experimenting with <code>bytes</code> vs <code>bytearray</code> in Python 2.6. I don't understand the reason for some differences.</p> <p>A <code>bytes</code> iterator returns strings:</p> <pre><code>for i in bytes(b"hi"): print(type(i)) </code></pre> <p>Gives:</p> <pre><code>&lt;type 'str'&gt; &lt;type ...
11
2009-11-16T07:50:25Z
1,740,758
<p>I am not sure since which version, but <code>bytes</code> is actually a <code>str</code>, which you can see if you do <code>type(bytes(b"hi"))</code> -> <code>&lt;type 'str'&gt;</code>.</p> <p><code>bytearray</code> is a mutable array of bytes, one constructor of which takes a string.</p>
3
2009-11-16T08:09:03Z
[ "python", "python-3.x", "byte", "bytearray", "python-2.x" ]
bytes vs bytearray in Python 2.6 and 3
1,740,696
<p>I'm experimenting with <code>bytes</code> vs <code>bytearray</code> in Python 2.6. I don't understand the reason for some differences.</p> <p>A <code>bytes</code> iterator returns strings:</p> <pre><code>for i in bytes(b"hi"): print(type(i)) </code></pre> <p>Gives:</p> <pre><code>&lt;type 'str'&gt; &lt;type ...
11
2009-11-16T07:50:25Z
1,740,762
<p><strong>In Python 2.6 bytes is merely an alias for str</strong>.<br> This "pseudo type" was introduced to [partially] prepare programs [and programmers!] to be converted/compatible with Python 3.0 where there is a strict distinction of semantics and use for str (which are systematically unicode) and bytes (which are...
22
2009-11-16T08:09:55Z
[ "python", "python-3.x", "byte", "bytearray", "python-2.x" ]
bytes vs bytearray in Python 2.6 and 3
1,740,696
<p>I'm experimenting with <code>bytes</code> vs <code>bytearray</code> in Python 2.6. I don't understand the reason for some differences.</p> <p>A <code>bytes</code> iterator returns strings:</p> <pre><code>for i in bytes(b"hi"): print(type(i)) </code></pre> <p>Gives:</p> <pre><code>&lt;type 'str'&gt; &lt;type ...
11
2009-11-16T07:50:25Z
1,760,553
<p>I tried it on Python 3.0.</p> <p>In Python 3.0, a <code>bytes</code> iterator returns <code>int</code>s, not strings as Python 2.6 did:</p> <pre><code>for i in bytes(b"hi"): print(type(i)) </code></pre> <p>Gives:</p> <pre><code>&lt;class 'int'&gt; &lt;class 'int'&gt; </code></pre> <p>A <code>bytearray</code...
2
2009-11-19T02:37:54Z
[ "python", "python-3.x", "byte", "bytearray", "python-2.x" ]
Python:: Turn string into operator
1,740,726
<p>How can I turn a string such as "+" into the operator plus? Thanks!</p>
12
2009-11-16T08:00:19Z
1,740,742
<p>You can use <code>eval</code> like so:</p> <pre><code>eval("+") </code></pre>
-9
2009-11-16T08:04:21Z
[ "python" ]
Python:: Turn string into operator
1,740,726
<p>How can I turn a string such as "+" into the operator plus? Thanks!</p>
12
2009-11-16T08:00:19Z
1,740,759
<p>Use a lookup table:</p> <pre><code>import operator ops = { "+": operator.add, "-": operator.sub } # etc. print ops["+"](1,1) # prints 2 </code></pre>
44
2009-11-16T08:09:15Z
[ "python" ]
Python:: Turn string into operator
1,740,726
<p>How can I turn a string such as "+" into the operator plus? Thanks!</p>
12
2009-11-16T08:00:19Z
1,740,760
<p>You can try using eval(), but it's dangerous if the strings are not coming from you. Else you might consider creating a dictionary:</p> <pre><code>ops = {"+": (lambda x,y: x+y), "-": (lambda x,y: x-y)}</code></pre> <p>etc... and then calling <pre><code>ops['+'] (1,2)</code></pre> or, for user input:<br></p> <pre>...
3
2009-11-16T08:09:16Z
[ "python" ]