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 |
|---|---|---|---|---|---|---|---|---|---|
Will everything in the standard library treat strings as unicode in Python 3.0? | 91,205 | <p>I'm a little confused about how the standard library will behave now that Python (from 3.0) is unicode-based. Will modules such as CGI and urllib use unicode strings or will they use the new 'bytes' type and just provide encoded data?</p>
| 10 | 2008-09-18T09:29:23Z | 91,301 | <p>Logically a lot of things like MIME-encoded mail messages, URLs, XML documents, and so on should be returned as <code>bytes</code> not strings. This could cause some consternation as the libraries start to be nailed down for Python 3 and people discover that they have to be more aware of the <code>bytes</code>/<code... | 10 | 2008-09-18T09:52:48Z | [
"python",
"unicode",
"string",
"cgi",
"python-3.x"
] |
Will everything in the standard library treat strings as unicode in Python 3.0? | 91,205 | <p>I'm a little confused about how the standard library will behave now that Python (from 3.0) is unicode-based. Will modules such as CGI and urllib use unicode strings or will they use the new 'bytes' type and just provide encoded data?</p>
| 10 | 2008-09-18T09:29:23Z | 91,325 | <p>One of the great things about this question (and Python in general) is that you can just mess around in the interpreter! <a href="http://www.python.org/download/releases/3.0/">Python 3.0 rc1 is currently available for download</a>.</p>
<pre><code>>>> import urllib.request
>>> fh = urllib.request.u... | 6 | 2008-09-18T09:58:19Z | [
"python",
"unicode",
"string",
"cgi",
"python-3.x"
] |
Will everything in the standard library treat strings as unicode in Python 3.0? | 91,205 | <p>I'm a little confused about how the standard library will behave now that Python (from 3.0) is unicode-based. Will modules such as CGI and urllib use unicode strings or will they use the new 'bytes' type and just provide encoded data?</p>
| 10 | 2008-09-18T09:29:23Z | 91,508 | <p>There will be a two-step dance here. See <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=227041" rel="nofollow">Python 3000 and You</a>.</p>
<p>Step 1 is to get running under 3.0.</p>
<p>Step 2 is to rethink your API's to, perhaps, do something more sensible.</p>
<p>The most likely course is that the ... | 1 | 2008-09-18T10:35:07Z | [
"python",
"unicode",
"string",
"cgi",
"python-3.x"
] |
NI CVI with Python | 91,666 | <p>I'd like to integrate a Python IDLE-esque command prompt interface into an existing NI-CVI (LabWindows) application. I've tried to follow the Python.org discussions but seem to get lost in the details. Is there a resource out there for dummies like me?</p>
| 2 | 2008-09-18T11:09:50Z | 91,735 | <p>Here is a <a href="http://decibel.ni.com/content/docs/DOC-1494" rel="nofollow">python sample code calling a CVI</a>.</p>
<p>There are <a href="http://www.scipy.org/Cookbook/Data_Acquisition_with_PyUL" rel="nofollow">DaqMx python bindings</a> too.</p>
| 1 | 2008-09-18T11:22:06Z | [
"python",
"labview",
"labwindows",
"cvi"
] |
Is there a pretty printer for python data? | 91,810 | <p>Working with python interactively, it's sometimes necessary to display a result which is some arbitrarily complex data structure (like lists with embedded lists, etc.)
The default way to display them is just one massive linear dump which just wraps over and over and you have to parse carefully to read it.</p>
<p>Is... | 16 | 2008-09-18T11:43:45Z | 91,818 | <pre><code>from pprint import pprint
a = [0, 1, ['a', 'b', 'c'], 2, 3, 4]
pprint(a)
</code></pre>
<p>Note that for a short list like my example, pprint will in fact print it all on one line. However, for more complex structures it does a pretty good job of pretty printing data.</p>
| 24 | 2008-09-18T11:44:54Z | [
"python",
"prettify"
] |
Is there a pretty printer for python data? | 91,810 | <p>Working with python interactively, it's sometimes necessary to display a result which is some arbitrarily complex data structure (like lists with embedded lists, etc.)
The default way to display them is just one massive linear dump which just wraps over and over and you have to parse carefully to read it.</p>
<p>Is... | 16 | 2008-09-18T11:43:45Z | 91,972 | <p>Another good option is to use <a href="http://ipython.scipy.org/moin/" rel="nofollow">IPython</a>, which is an interactive environment with a lot of extra features, including automatic pretty printing, tab-completion of methods, easy shell access, and a lot more. It's also very easy to install. </p>
<p><a href="h... | 3 | 2008-09-18T12:15:04Z | [
"python",
"prettify"
] |
Is there a pretty printer for python data? | 91,810 | <p>Working with python interactively, it's sometimes necessary to display a result which is some arbitrarily complex data structure (like lists with embedded lists, etc.)
The default way to display them is just one massive linear dump which just wraps over and over and you have to parse carefully to read it.</p>
<p>Is... | 16 | 2008-09-18T11:43:45Z | 92,260 | <p>Somtimes <a href="http://pyyaml.org/">YAML</a> can be good for this.</p>
<pre><code>import yaml
a = [0, 1, ['a', 'b', 'c'], 2, 3, 4]
print yaml.dump(a)
</code></pre>
<p>Produces:</p>
<pre><code>- 0
- 1
- [a, b, c]
- 2
- 3
- 4
</code></pre>
| 10 | 2008-09-18T12:55:39Z | [
"python",
"prettify"
] |
Is there a pretty printer for python data? | 91,810 | <p>Working with python interactively, it's sometimes necessary to display a result which is some arbitrarily complex data structure (like lists with embedded lists, etc.)
The default way to display them is just one massive linear dump which just wraps over and over and you have to parse carefully to read it.</p>
<p>Is... | 16 | 2008-09-18T11:43:45Z | 93,312 | <p>In addition to <code>pprint.pprint</code>, <code>pprint.pformat</code> is really useful for making readable <code>__repr__</code>s. My complex <code>__repr__</code>s usually look like so:</p>
<pre><code>def __repr__(self):
from pprint import pformat
return "<ClassName %s>" % pformat({"attrs":self.att... | 8 | 2008-09-18T14:56:26Z | [
"python",
"prettify"
] |
Google App Engine: how can I programmatically access the properties of my Model class? | 91,821 | <p>I have a model class:</p>
<pre><code>class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
</code></pre>
<p>I have an instance of this class in <code>p</code>, and string <code>s</code> contains the value <code>'first_name'</code>. I would like to do... | 8 | 2008-09-18T11:45:16Z | 91,859 | <pre><code>getattr(p, s)
setattr(p, s, new_value)
</code></pre>
| 1 | 2008-09-18T11:54:13Z | [
"python",
"string",
"google-app-engine"
] |
Google App Engine: how can I programmatically access the properties of my Model class? | 91,821 | <p>I have a model class:</p>
<pre><code>class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
</code></pre>
<p>I have an instance of this class in <code>p</code>, and string <code>s</code> contains the value <code>'first_name'</code>. I would like to do... | 8 | 2008-09-18T11:45:16Z | 91,911 | <p>Try:</p>
<pre><code>p.model_properties()[s].get_value_for_datastore(p)
</code></pre>
<p>See <a href="http://code.google.com/appengine/docs/datastore/propertyclass.html#Property_get_value_for_datastore" rel="nofollow">the documentation</a>.</p>
| 1 | 2008-09-18T12:06:25Z | [
"python",
"string",
"google-app-engine"
] |
Google App Engine: how can I programmatically access the properties of my Model class? | 91,821 | <p>I have a model class:</p>
<pre><code>class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
</code></pre>
<p>I have an instance of this class in <code>p</code>, and string <code>s</code> contains the value <code>'first_name'</code>. I would like to do... | 8 | 2008-09-18T11:45:16Z | 91,970 | <p>If the model class is sufficiently intelligent, it should recognize the standard Python ways of doing this.</p>
<p>Try:</p>
<pre><code>getattr(p, s)
setattr(p, s, new_value)
</code></pre>
<p>There is also hasattr available.</p>
| 7 | 2008-09-18T12:14:37Z | [
"python",
"string",
"google-app-engine"
] |
Google App Engine: how can I programmatically access the properties of my Model class? | 91,821 | <p>I have a model class:</p>
<pre><code>class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
</code></pre>
<p>I have an instance of this class in <code>p</code>, and string <code>s</code> contains the value <code>'first_name'</code>. I would like to do... | 8 | 2008-09-18T11:45:16Z | 97,760 | <p>With much thanks to Jim, the exact solution I was looking for is:</p>
<pre><code>p.properties()[s].get_value_for_datastore(p)
</code></pre>
<p>To all the other respondents, thank you for your help. I also would have expected the Model class to implement the python standard way of doing this, but for whatever reaso... | 3 | 2008-09-18T22:46:08Z | [
"python",
"string",
"google-app-engine"
] |
Google App Engine: how can I programmatically access the properties of my Model class? | 91,821 | <p>I have a model class:</p>
<pre><code>class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
</code></pre>
<p>I have an instance of this class in <code>p</code>, and string <code>s</code> contains the value <code>'first_name'</code>. I would like to do... | 8 | 2008-09-18T11:45:16Z | 169,899 | <p>p.first_name = "New first name"
p.put()</p>
<p>or p = Person(first_name = "Firsty",
last_name = "Lasty" )
p.put()</p>
| -1 | 2008-10-04T07:10:55Z | [
"python",
"string",
"google-app-engine"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,254 | <p>I'd suggest writing a non-trivial webapp using either Django or Pylons, something that does some number crunching.
No better way to learn a new language than commiting yourself to a problem and learning as you go!</p>
| 1 | 2008-09-18T12:54:38Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,269 | <p>Something great to play around with, though not a project, is <a href="http://pythonchallenge.com" rel="nofollow">The Python Challenge</a>. I've found it quite useful in improving my python skills, and it gives your brain a good workout at the same time.</p>
| 3 | 2008-09-18T12:56:33Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,288 | <p>I honestly loved the book <a href="http://rads.stackoverflow.com/amzn/click/0596009259" rel="nofollow">Programming Python.</a> It has a large assortment of small projects, most of which can be completed in an evening at a leisurely pace. They get you acquainted with most of the standard library and will likely hold... | 2 | 2008-09-18T12:57:32Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,296 | <p>Depending on exactly what you mean by "gotten to grips with the basics", I'd suggest reading through <a href="http://www.diveintopython.net/" rel="nofollow">Dive Into Python</a> and typing/executing all the chapter code, then get something like <a href="http://www.amazon.co.uk/Programming-Collective-Intelligence-Bui... | 5 | 2008-09-18T12:58:50Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,318 | <p>Well, there are great ressources for advanced Python programming :</p>
<ul>
<li>Dive Into Python (<a href="http://www.diveintopython.net/">read it for free</a>)</li>
<li>Online python cookbooks (e.g. <a href="http://code.activestate.com/recipes/langs/python/">here</a> and <a href="http://the.taoofmac.com/space/Pyth... | 14 | 2008-09-18T13:01:53Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,381 | <p>Write a web app, likely in <a href="http://www.djangoproject.com/" rel="nofollow">Django</a> - the <a href="http://docs.djangoproject.com/" rel="nofollow">docs</a> will teach you a lot of good Python style.</p>
<p>Use some of the popular libraries like <a href="http://pygments.org/" rel="nofollow">Pygments</a> or t... | 1 | 2008-09-18T13:10:07Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,461 | <p>People tend to say something along the lines of "The best way to learn is by doing" but I've always found that unless you're specifically learning a language to contribute to some project it's difficult to actually find little problems to tackle to keep yourself going.</p>
<p>A good solution to this is <a href="htt... | 1 | 2008-09-18T13:20:21Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,491 | <p>I'll plug <a href="http://homepage.mac.com/s_lott/books/python.html" rel="nofollow">Building Skills in Python</a>. Plus, if you want something more challenging, <a href="http://homepage.mac.com/s_lott/books/oodesign.html" rel="nofollow">Building Skills in OO Design</a> is a rather large and complex series of exerc... | 2 | 2008-09-18T13:23:41Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 92,691 | <p><em>The Python Cookbook</em> is absolutely essential if you want to master idiomatic Python. Besides, that's the book that made me fall in love with the language.</p>
| 2 | 2008-09-18T13:49:59Z | [
"python"
] |
Python, beyond the basics | 92,230 | <p>I've gotten to grips with the basics of Python and I've got a small holiday which I want to use some of to learn a little more Python. The problem is that I have no idea what to learn or where to start. I'm primarily web development but in this case I don't know how much difference it will make.</p>
| 16 | 2008-09-18T12:51:07Z | 181,078 | <p>Search "Alex Martelli", "Alex Martelli patterns" and "Thomas Wouters" on Google video. There's plenty of interesting talks on andvanced Python, design patterns in Python, and so on.</p>
| 1 | 2008-10-08T01:31:46Z | [
"python"
] |
Django + FCGID on Fedora Core 9 -- what am I missing? | 92,373 | <p>Fedora Core 9 seems to have <a href="http://fastcgi.coremail.cn/" rel="nofollow">FCGID</a> instead of <a href="http://www.fastcgi.com/drupal/node/2" rel="nofollow">FastCGI</a> as a pre-built, YUM-managed module. [<em>I'd rather not have to maintain a module outside of YUM; so no manual builds for me or my sysadmins... | 0 | 2008-09-18T13:09:21Z | 92,891 | <p>Why don't you try <a href="http://code.google.com/p/modwsgi/" rel="nofollow">modwsgi</a>? It sounds as the preffered way these days for WSGI applications such as Django.</p>
<p>If you don't wan't to compile stuff for Fedora Core, that might be trickier.</p>
<p>Regarding 1) <a href="http://fitri.manzanisimo.net/200... | 1 | 2008-09-18T14:13:09Z | [
"python",
"django",
"apache2",
"fastcgi",
"fcgid"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 92,441 | <p>The best I've come up with now is (thanks to the python-izers above) </p>
<pre><code>def filter_non_printable(str):
return ''.join([c for c in str if ord(c) > 31 or ord(c) == 9])
</code></pre>
<p>This is the only way I've found out that works with Unicode characters/strings</p>
<p>Any better options?</p>
| 0 | 2008-09-18T13:17:35Z | [
"python",
"string",
"non-printable"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 92,488 | <p>As far as I know, the most pythonic/efficient method would be:</p>
<pre><code>import string
filtered_string = filter(lambda x: x in string.printable, myStr)
</code></pre>
| 38 | 2008-09-18T13:23:14Z | [
"python",
"string",
"non-printable"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 92,509 | <p>This function uses list comprehensions and str.join, so it runs in linear time instead of O(n^2):</p>
<pre><code>from curses.ascii import isprint
def printable(input):
return ''.join(char for char in input if isprint(char))
</code></pre>
| 4 | 2008-09-18T13:26:00Z | [
"python",
"string",
"non-printable"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 93,029 | <p>Iterating over strings is unfortunately rather slow in Python. Regular expressions are over an order of magnitude faster for this kind of thing. You just have to build the character class yourself. The <em>unicodedata</em> module is quite helpful for this, especially the <em>unicodedata.category()</em> function. See... | 52 | 2008-09-18T14:28:04Z | [
"python",
"string",
"non-printable"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 93,557 | <p>You could try setting up a filter using the <code>unicodedata.category()</code> function:</p>
<pre><code>printable = Set('Lu', 'Ll', ...)
def filter_non_printable(str):
return ''.join(c for c in str if unicodedata.category(c) in printable)
</code></pre>
<p>See the <a href="http://www.unicode.org/versions/Unicode... | 8 | 2008-09-18T15:25:37Z | [
"python",
"string",
"non-printable"
] |
Stripping non printable characters from a string in python | 92,438 | <p>I use to run</p>
<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>
<p>on Perl to get rid of non printable characters. </p>
<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>
<p>What wou... | 59 | 2008-09-18T13:17:06Z | 25,829,509 | <p>In Python 3,</p>
<pre><code>def filter_nonprintable(text):
import string
# Get the difference of all ASCII characters from the set of printable characters
nonprintable = set([chr(i) for i in range(128)]).difference(string.printable)
# Use translate to remove all non-printable characters
return t... | 3 | 2014-09-14T02:20:39Z | [
"python",
"string",
"non-printable"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 92,548 | <p>I found struct.unpack to be a godsend for unpacking binary data formats after I learned of it!</p>
| 2 | 2008-09-18T13:32:23Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 93,498 | <p>I've found <a href="http://docs.python.org/lib/module-sched.html" rel="nofollow">sched module</a> to be helpful in cron-like activities. It simplifies things a lot. Unfortunately I found it too late. </p>
| 1 | 2008-09-18T15:17:34Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 93,865 | <p>Oft overlooked modules, uses and tricks:</p>
<p>collections.defaultdict(): for when you want missing keys in a dict to have a default value.</p>
<p>functools.wraps(): for writing decorators that play nicely with introspection.</p>
<p>posixpath: the os.path module for POSIX systems. You can use it for manipulating... | 4 | 2008-09-18T15:56:57Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 94,551 | <p><a href="http://docs.python.org/lib/module-getpass.html" rel="nofollow">getpass</a> is useful for determining the login name of the current user.</p>
<p><a href="http://docs.python.org/lib/module-grp.html" rel="nofollow">grp</a> allows you to lookup Unix group IDs by name, and vice versa.</p>
<p><a href="http://do... | 2 | 2008-09-18T17:11:53Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 94,991 | <p>Most of the other examples are merely overlooked, not unexpected uses for module.</p>
<p>fnmatch, like shlex, can be applied in unexpected ways. fnmatch is a kind of poor-person's RE, and can be used for more than matching files, it can compare strings with the simplified wild-card patterns.</p>
| 1 | 2008-09-18T17:58:06Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 95,159 | <p>I use itertools (especially cycle, repeat, chain) to make python behave more like R and in other functional / vector applications. Often this lets me avoid the overhead and complication of Numpy. </p>
<pre><code># in R, shorter iterables are automatically cycled
# and all functions "apply" in a "map"-like way ove... | 3 | 2008-09-18T18:13:28Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 95,825 | <p>I was quite surprised to learn that you could use the bisect module to do a very fast binary search in a sequence. It's documentation doesn't say anything about it:</p>
<blockquote>
<p>This module provides support for maintaining a list in sorted order without having to sort the list after each insertion.</p>
</b... | 6 | 2008-09-18T19:08:34Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 97,103 | <p>One function I've come to appreciate is string.translate. Its very fast at what it does, and useful anywhere you want to alter or remove characters in a string. I've just used it in a seemingly inapplicable <a href="http://stackoverflow.com/questions/89909/in-python-how-to-i-verify-that-a-string-only-contains-lette... | 1 | 2008-09-18T21:20:29Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 99,969 | <p>The <a href="http://docs.python.org/lib/module-pickle.html" rel="nofollow">pickle</a> module is pretty awesome</p>
| 1 | 2008-09-19T05:54:22Z | [
"python"
] |
Python module functions used in unexpected ways | 92,533 | <p>Based on <a href="http://stackoverflow.com/questions/6209/split-a-string-ignoring-quoted-sections#6243">"Split a string by spaces in Python"</a>, which uses <em>shlex.split</em> to split a string with quotes smartly, I would be interested in hearing about other common tasks solved by non-obvious standard library fun... | 4 | 2008-09-18T13:29:56Z | 101,353 | <p>complex numbers. (The complexobject.c defines a class, so technically it's not a module). Great for 2d coordinates, with easy translation/rotations etc</p>
<p>eg.</p>
<pre><code>TURN_LEFT_90= 1j
TURN_RIGHT_90= -1j
coord= 5+4j # x=5 y=4
print coord*TURN_LEFT_90
</code></pre>
| 1 | 2008-09-19T12:10:20Z | [
"python"
] |
How can I highlight text in Scintilla? | 92,565 | <p>I am writing an editor using <a href="http://www.scintilla.org/" rel="nofollow">Scintilla</a>.</p>
<p>I am already using a lexer to do automatic syntax highlighting but now I would like to mark search results. If I want to mark only one hit I can set the selection there, however, I would like to mark (e.g. with yel... | 6 | 2008-09-18T13:34:54Z | 92,682 | <p>The "sample" editor scite uses the bookmark feature to bookmark all the lines that match the search result.</p>
| 2 | 2008-09-18T13:48:41Z | [
"c#",
"python",
"perl",
"ide",
"scintilla"
] |
How can I highlight text in Scintilla? | 92,565 | <p>I am writing an editor using <a href="http://www.scintilla.org/" rel="nofollow">Scintilla</a>.</p>
<p>I am already using a lexer to do automatic syntax highlighting but now I would like to mark search results. If I want to mark only one hit I can set the selection there, however, I would like to mark (e.g. with yel... | 6 | 2008-09-18T13:34:54Z | 92,778 | <p>Have you read the <a href="http://scintilla.sourceforge.net/ScintillaDoc.html#Markers">Markers reference in Scintilla doc</a>?
This reference can be a bit obscure, so I advise to take a look at the source code of SciTE as well. This text editor was originally a testbed for Scintilla. It grown to a full fledged edito... | 9 | 2008-09-18T13:59:19Z | [
"c#",
"python",
"perl",
"ide",
"scintilla"
] |
How can I highlight text in Scintilla? | 92,565 | <p>I am writing an editor using <a href="http://www.scintilla.org/" rel="nofollow">Scintilla</a>.</p>
<p>I am already using a lexer to do automatic syntax highlighting but now I would like to mark search results. If I want to mark only one hit I can set the selection there, however, I would like to mark (e.g. with yel... | 6 | 2008-09-18T13:34:54Z | 4,854,367 | <p>I used <a href="http://www.scintilla.org/ScintillaDoc.html#Indicators" rel="nofollow">Indicators</a> to highlight search results.</p>
| 1 | 2011-01-31T18:05:43Z | [
"c#",
"python",
"perl",
"ide",
"scintilla"
] |
Python sockets suddenly timing out? | 92,620 | <p>I came back today to an old script I had for logging into Gmail via SSL. The script worked fine last time I ran it (several months ago) but now it dies immediately with:</p>
<pre><code><urlopen error The read operation timed out>
</code></pre>
<p>If I set the timeout (no matter how long), it dies even more i... | 13 | 2008-09-18T13:41:22Z | 93,401 | <p>www.google.com is not accessible by HTTPS. It redirects to insecure HTTP. To get to mail, you should be going go https://mail.google.com</p>
| 0 | 2008-09-18T15:06:11Z | [
"python",
"api",
"sockets",
"ssl",
"gmail"
] |
Python sockets suddenly timing out? | 92,620 | <p>I came back today to an old script I had for logging into Gmail via SSL. The script worked fine last time I ran it (several months ago) but now it dies immediately with:</p>
<pre><code><urlopen error The read operation timed out>
</code></pre>
<p>If I set the timeout (no matter how long), it dies even more i... | 13 | 2008-09-18T13:41:22Z | 93,404 | <pre><code>import socket
socket.setdefaulttimeout(30000)
sock = socket.socket()
sock.connect(('www.google.com', 443))
ssl = socket.ssl(sock)
ssl.server()
--> '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com'
</code></pre>
<p>It works just fine. I can't reproduce your error.</p>
| 1 | 2008-09-18T15:06:36Z | [
"python",
"api",
"sockets",
"ssl",
"gmail"
] |
Python sockets suddenly timing out? | 92,620 | <p>I came back today to an old script I had for logging into Gmail via SSL. The script worked fine last time I ran it (several months ago) but now it dies immediately with:</p>
<pre><code><urlopen error The read operation timed out>
</code></pre>
<p>If I set the timeout (no matter how long), it dies even more i... | 13 | 2008-09-18T13:41:22Z | 96,565 | <p>The first thing I would check is whether you need to connect via an HTTP proxy (in which case direct connections bypassing the proxy will likely time out). Run Wireshark and see what happens.</p>
| 0 | 2008-09-18T20:29:34Z | [
"python",
"api",
"sockets",
"ssl",
"gmail"
] |
time.sleep -- sleeps thread or process? | 92,928 | <p>In Python for the *nix, does <code>time.sleep()</code> block the thread or the process?</p>
| 227 | 2008-09-18T14:16:45Z | 92,953 | <p>Just the thread.</p>
| 15 | 2008-09-18T14:18:51Z | [
"python",
"multithreading",
"time",
"sleep",
"python-internals"
] |
time.sleep -- sleeps thread or process? | 92,928 | <p>In Python for the *nix, does <code>time.sleep()</code> block the thread or the process?</p>
| 227 | 2008-09-18T14:16:45Z | 92,986 | <p>It will just sleep the thread except in the case where your application has only a single thread, in which case it will sleep the thread and effectively the process as well.</p>
<p>The python documentation on sleep doesn't specify this however, so I can certainly understand the confusion!</p>
<p><a href="http://do... | 34 | 2008-09-18T14:22:52Z | [
"python",
"multithreading",
"time",
"sleep",
"python-internals"
] |
time.sleep -- sleeps thread or process? | 92,928 | <p>In Python for the *nix, does <code>time.sleep()</code> block the thread or the process?</p>
| 227 | 2008-09-18T14:16:45Z | 93,069 | <p>The thread will block, but the process is still alive.</p>
<p>In a single threaded application, this means everything is blocked while you sleep. In a multithreaded application, only the thread you explicitly 'sleep' will block and the other threads still run within the process.</p>
| 6 | 2008-09-18T14:32:45Z | [
"python",
"multithreading",
"time",
"sleep",
"python-internals"
] |
time.sleep -- sleeps thread or process? | 92,928 | <p>In Python for the *nix, does <code>time.sleep()</code> block the thread or the process?</p>
| 227 | 2008-09-18T14:16:45Z | 93,179 | <p>It blocks the thread. If you look in Modules/timemodule.c in the Python source, you'll see that in the call to <code>floatsleep()</code>, the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current... | 219 | 2008-09-18T14:42:23Z | [
"python",
"multithreading",
"time",
"sleep",
"python-internals"
] |
time.sleep -- sleeps thread or process? | 92,928 | <p>In Python for the *nix, does <code>time.sleep()</code> block the thread or the process?</p>
| 227 | 2008-09-18T14:16:45Z | 32,216,136 | <p>Only the thread unless your process has a single thread.</p>
| 1 | 2015-08-26T00:21:35Z | [
"python",
"multithreading",
"time",
"sleep",
"python-internals"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 93,831 | <p>I think I have your poison :</p>
<p><a href="http://sourceforge.net/projects/xmlite" rel="nofollow">http://sourceforge.net/projects/xmlite</a></p>
<p>Cheers</p>
| 1 | 2008-09-18T15:54:07Z | [
"python",
"xml",
"streaming"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 93,850 | <p><strong>xml.etree.cElementTree</strong>, included in the default distribution of CPython since 2.5. Lightning fast for both reading and writing XML.</p>
| -4 | 2008-09-18T15:55:24Z | [
"python",
"xml",
"streaming"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 93,871 | <p>I've always had good results with <a href="http://codespeak.net/lxml/" rel="nofollow">lxml</a>. It's a pain to install, as it's mostly a wrapper around <a href="http://xmlsoft.org/" rel="nofollow">libxml2</a>, but <code>lxml.etree</code> tree objects have a <code>.write()</code> method that takes a file-like object... | -1 | 2008-09-18T15:57:23Z | [
"python",
"xml",
"streaming"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 93,984 | <p>Some years ago I used <code>MarkupWriter</code> from <a href="http://4suite.org/index.xhtml" rel="nofollow">4suite</a></p>
<blockquote>
<pre><code>General-purpose utility class for generating XML (may eventually be
expanded to produce more output types)
Sample usage:
from Ft.Xml import MarkupWriter
writer = Marku... | 0 | 2008-09-18T16:08:21Z | [
"python",
"xml",
"streaming"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 94,114 | <p>I think you'll find XMLGenerator from xml.sax.saxutils is the closest thing to what you want.</p>
<pre>
import time
from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesNSImpl
LOG_LEVELS = ['DEBUG', 'WARNING', 'ERROR']
class xml_logger:
def __init__(self, output, encoding):
... | 13 | 2008-09-18T16:22:28Z | [
"python",
"xml",
"streaming"
] |
What's the easiest non-memory intensive way to output XML from Python? | 93,710 | <p>Basically, something similar to System.Xml.XmlWriter - A streaming XML Writer that doesn't incur much of a memory overhead. So that rules out xml.dom and xml.dom.minidom. Suggestions?</p>
| 12 | 2008-09-18T15:42:19Z | 97,581 | <p>Second vote for ElementTree (cElementTree is a C implementation that is a little faster, like cPickle vs pickle). There's some short example code here that you can look at to give you an idea of how it works: <a href="http://effbot.org/zone/element-index.htm" rel="nofollow">http://effbot.org/zone/element-index.htm<... | -2 | 2008-09-18T22:17:07Z | [
"python",
"xml",
"streaming"
] |
How do I persist to disk a temporary file using Python? | 94,153 | <p>I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError:</p>
<pre><code>>>> import tempfile, shutil
>>> f = tempfile... | 13 | 2008-09-18T16:27:26Z | 94,206 | <p>You could always use <em>shutil.copyfileobj</em>, in your example:</p>
<pre><code>new_file = open('bar.txt', 'rw')
shutil.copyfileobj(f, new_file)
</code></pre>
| 4 | 2008-09-18T16:33:46Z | [
"python",
"temporary-files"
] |
How do I persist to disk a temporary file using Python? | 94,153 | <p>I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError:</p>
<pre><code>>>> import tempfile, shutil
>>> f = tempfile... | 13 | 2008-09-18T16:27:26Z | 94,339 | <p>The file you create with <code>TemporaryFile</code> or <code>NamedTemporaryFile</code> is automatically removed when it's closed, which is why you get an error. If you don't want this, you can use <code>mkstemp</code> instead (see the docs for <a href="http://docs.python.org/lib/module-tempfile.html">tempfile</a>).<... | 16 | 2008-09-18T16:45:37Z | [
"python",
"temporary-files"
] |
How do I persist to disk a temporary file using Python? | 94,153 | <p>I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError:</p>
<pre><code>>>> import tempfile, shutil
>>> f = tempfile... | 13 | 2008-09-18T16:27:26Z | 109,591 | <p>Starting from python 2.6 you can also use <code>NamedTemporaryFile</code> with the <code>delete=</code> option set to False. This way the temporary file will be accessible, even after you close it.</p>
<p>Note that on Windows (NT and later) you cannot access the file a second time while it is still open. You have t... | 11 | 2008-09-20T22:15:33Z | [
"python",
"temporary-files"
] |
How do I persist to disk a temporary file using Python? | 94,153 | <p>I am attempting to use the 'tempfile' module for manipulating and creating text files. Once the file is ready I want to save it to disk. I thought it would be as simple as using 'shutil.copy'. However, I get a 'permission denied' IOError:</p>
<pre><code>>>> import tempfile, shutil
>>> f = tempfile... | 13 | 2008-09-18T16:27:26Z | 9,155,528 | <p>hop is right, and dF. is incorrect on why the error occurs.</p>
<p>Since you haven't called <code>f.close()</code> yet, the file is <strong>not</strong> removed. </p>
<p>The <a href="http://docs.python.org/library/tempfile.html#tempfile.NamedTemporaryFile">doc</a> for <code>NamedTemporaryFile</code> says: </p>
<b... | 28 | 2012-02-06T04:19:42Z | [
"python",
"temporary-files"
] |
Distributed python | 94,334 | <p>What is the best python framework to create distributed applications? For example to build a P2P app.</p>
| 6 | 2008-09-18T16:45:07Z | 94,385 | <p>You could download the source of BitTorrent for starters and see how they did it.</p>
<p><a href="http://download.bittorrent.com/dl/" rel="nofollow">http://download.bittorrent.com/dl/</a></p>
| 1 | 2008-09-18T16:50:48Z | [
"python",
"distributed"
] |
Distributed python | 94,334 | <p>What is the best python framework to create distributed applications? For example to build a P2P app.</p>
| 6 | 2008-09-18T16:45:07Z | 94,434 | <p>If it's something where you're going to need tons of threads and need better concurrent performance, check out <a href="http://www.stackless.com/" rel="nofollow">Stackless Python</a>. Otherwise you could just use the <a href="http://en.wikipedia.org/wiki/SOAP" rel="nofollow">SOAP</a> or <a href="http://www.xmlrpc.co... | 1 | 2008-09-18T16:55:34Z | [
"python",
"distributed"
] |
Distributed python | 94,334 | <p>What is the best python framework to create distributed applications? For example to build a P2P app.</p>
| 6 | 2008-09-18T16:45:07Z | 94,510 | <p>You could checkout <a href="http://pyprocessing.berlios.de/" rel="nofollow">pyprocessing</a> which will be included in the standard library as of 2.6. It allows you to run tasks on multiple processes using an API similar to threading.</p>
| 2 | 2008-09-18T17:07:00Z | [
"python",
"distributed"
] |
Distributed python | 94,334 | <p>What is the best python framework to create distributed applications? For example to build a P2P app.</p>
| 6 | 2008-09-18T16:45:07Z | 94,597 | <p>I think you mean "Networked Apps"? Distributed means an app that can split its workload among multiple worker clients over the network. </p>
<p>You probably want.
<a href="http://twistedmatrix.com/trac/">Twisted</a></p>
| 9 | 2008-09-18T17:18:53Z | [
"python",
"distributed"
] |
Distributed python | 94,334 | <p>What is the best python framework to create distributed applications? For example to build a P2P app.</p>
| 6 | 2008-09-18T16:45:07Z | 111,300 | <p>You probably want <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a>. There is a P2P framework for Twisted called "<a href="http://divmod.org/trac/wiki/DivmodVertex" rel="nofollow">Vertex</a>". While not actively maintained, it does allow you to tunnel through NATs and make connections directly be... | 3 | 2008-09-21T16:03:08Z | [
"python",
"distributed"
] |
How do I read selected files from a remote Zip archive over HTTP using Python? | 94,490 | <p>I need to read selected files, matching on the file name, from a remote zip archive using Python. I don't want to save the full zip to a temporary file (it's not that large, so I can handle everything in memory).</p>
<p>I've already written the code and it works, and I'm answering this myself so I can search for it... | 9 | 2008-09-18T17:03:36Z | 94,491 | <p>Here's how I did it (grabbing all files ending in ".ranks"):</p>
<pre><code>import urllib2, cStringIO, zipfile
try:
remotezip = urllib2.urlopen(url)
zipinmemory = cStringIO.StringIO(remotezip.read())
zip = zipfile.ZipFile(zipinmemory)
for fn in zip.namelist():
if fn.endswith(".ranks"):
... | 8 | 2008-09-18T17:03:42Z | [
"python",
"http",
"zip"
] |
How do I read selected files from a remote Zip archive over HTTP using Python? | 94,490 | <p>I need to read selected files, matching on the file name, from a remote zip archive using Python. I don't want to save the full zip to a temporary file (it's not that large, so I can handle everything in memory).</p>
<p>I've already written the code and it works, and I'm answering this myself so I can search for it... | 9 | 2008-09-18T17:03:36Z | 94,516 | <p>Bear in mind that merely decompressing a ZIP file may result in <a href="http://en.wikipedia.org/wiki/Decompression_bomb" rel="nofollow">a security vulnerability</a>.</p>
| 1 | 2008-09-18T17:07:38Z | [
"python",
"http",
"zip"
] |
How do I read selected files from a remote Zip archive over HTTP using Python? | 94,490 | <p>I need to read selected files, matching on the file name, from a remote zip archive using Python. I don't want to save the full zip to a temporary file (it's not that large, so I can handle everything in memory).</p>
<p>I've already written the code and it works, and I'm answering this myself so I can search for it... | 9 | 2008-09-18T17:03:36Z | 952,834 | <p>Thanks Marcel for your question and answer (I had the same problem in a different context and encountered the same difficulty with file-like objects not really being file-like)! Just as an update: For Python 3.0, your code needs to be modified slightly:</p>
<pre><code>import urllib.request, io, zipfile
try:
re... | 2 | 2009-06-04T20:13:44Z | [
"python",
"http",
"zip"
] |
How do I read selected files from a remote Zip archive over HTTP using Python? | 94,490 | <p>I need to read selected files, matching on the file name, from a remote zip archive using Python. I don't want to save the full zip to a temporary file (it's not that large, so I can handle everything in memory).</p>
<p>I've already written the code and it works, and I'm answering this myself so I can search for it... | 9 | 2008-09-18T17:03:36Z | 14,461,148 | <p>This will do the job without downloading the entire zip file!</p>
<p><a href="http://pypi.python.org/pypi/pyremotezip" rel="nofollow">http://pypi.python.org/pypi/pyremotezip</a></p>
| 2 | 2013-01-22T14:43:27Z | [
"python",
"http",
"zip"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,953 | <p>See this <a href="http://avinashv.net/2008/05/pythons-range-and-xrange/" rel="nofollow">post</a> to find difference between range and xrange:</p>
<p>To quote:</p>
<blockquote>
<p><code>range</code> returns exactly what you think: a list of consecutive
integers, of a defined length beginning with 0. <code>xrang... | 0 | 2008-09-18T17:54:40Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,957 | <p>xrange returns an iterator and only keeps one number in memory at a time. range keeps the entire list of numbers in memory.</p>
| 25 | 2008-09-18T17:55:00Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,962 | <p>range creates a list, so if you do <code>range(1, 10000000)</code> it creates a list in memory with <code>9999999</code> elements.</p>
<p><code>xrange</code> is a sequence object that evaluates lazily. </p>
| 460 | 2008-09-18T17:55:13Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,965 | <p>range generates the entire list and returns it. xrange does not -- it generates the numbers in the list on demand.</p>
| 2 | 2008-09-18T17:55:27Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,966 | <p>xrange uses an iterator (generates values on the fly), range returns a list.</p>
| 2 | 2008-09-18T17:55:30Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 94,971 | <p>Do spend some time with the <a href="http://docs.python.org/lib/typesseq-xrange.html">Library Reference</a>. The more familiar you are with it, the faster you can find answers to questions like this. Especially important are the first few chapters about builtin objects and types.</p>
<blockquote>
<p>The advantage... | 18 | 2008-09-18T17:55:59Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 95,010 | <p>It is for optimization reasons.</p>
<p>range() will create a list of values from start to end (0 .. 20 in your example). This will become an expensive operation on very large ranges.</p>
<p>xrange() on the other hand is much more optimised. it will only compute the next value when needed (via an xrange sequence ob... | 8 | 2008-09-18T17:59:46Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 95,100 | <blockquote>
<p>range creates a list, so if you do <code>range(1, 10000000)</code> it creates a list in memory with <code>10000000</code> elements.</p>
<p><code>xrange</code> <s>is a generator, so it</s> is a sequence object <s>is a</s> that evaluates lazily. </p>
</blockquote>
<p>This is true, but in Python ... | 148 | 2008-09-18T18:08:19Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 95,168 | <p><code>xrange</code> only stores the range params and generates the numbers on demand. However the C implementation of Python currently restricts its args to C longs:</p>
<pre><code>xrange(2**32-1, 2**32+1) # When long is 32 bits, OverflowError: Python int too large to convert to C long
range(2**32-1, 2**32+1) # ... | 51 | 2008-09-18T18:13:44Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 95,549 | <blockquote>
<p>range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000 elements.
xrange is a generator, so it evaluates lazily.</p>
</blockquote>
<p>This brings you two advantages:</p>
<ol>
<li>You can iterate longer lists without getting a <code>MemoryError</code>.</li>
<... | 10 | 2008-09-18T18:44:38Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 97,530 | <p>Remember, use the timeit module to test which of small snipps of code is faster!</p>
<pre><code>$ python -m timeit 'for i in range(1000000):' ' pass'
10 loops, best of 3: 90.5 msec per loop
$ python -m timeit 'for i in xrange(1000000):' ' pass'
10 loops, best of 3: 51.1 msec per loop
</code></pre>
<p>Personally, I... | 77 | 2008-09-18T22:11:15Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 5,351,725 | <p>When testing range against xrange in a loop (I know I should use <a href="http://docs.python.org/library/timeit.html" rel="nofollow">timeit</a>, but this was swiftly hacked up from memory using a simple list comprehension example) I found the following:</p>
<pre><code>import time
for x in range(1, 10):
t = ti... | 2 | 2011-03-18T12:04:59Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 21,137,807 | <p>On a requirement for scanning/printing of 0-N items , range and xrange works as follows.</p>
<p>range() - creates a new list in the memory and takes the whole 0 to N items(totally N+1) and prints them.
xrange() - creates a iterator instance that scans through the items and keeps only the current encountered item in... | 1 | 2014-01-15T12:45:29Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 22,905,006 | <p>I am shocked nobody read <a href="https://docs.python.org/2/library/functions.html#xrange" rel="nofollow">doc</a></p>
<p>This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually ... | 4 | 2014-04-07T06:25:25Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 27,144,195 | <p>What?<br>
<code>range</code> returns a static list at runtime.<br>
<code>xrange</code> returns an <code>object</code> (which acts like a generator, although it's certainly not one) from which values are generated as and when required.</p>
<p>When to use which? </p>
<ul>
<li>Use <code>xrange</code> if you want to ... | 2 | 2014-11-26T08:18:28Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 27,752,378 | <p><strong>Range</strong> returns a <strong>list</strong> while <strong>xrange</strong> returns an <strong>xrange</strong> object which takes the same memory irrespective of the range size,as in this case,only one element is generated and available per iteration whereas in case of using range, all the elements are gene... | 0 | 2015-01-03T06:31:21Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 30,088,340 | <p>Some of the other answers mention that Python 3 eliminated 2.x's <code>range</code> and renamed 2.x's <code>xrange</code> to <code>range</code>. However, unless you're using 3.0 or 3.1 (which nobody should be), it's actually a somewhat different type.</p>
<p>As <a href="https://docs.python.org/3.1/library/stdtypes.... | 3 | 2015-05-06T21:57:10Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 30,545,536 | <p>The difference decreases for smaller arguments to <code>range(..)</code> / <code>xrange(..)</code>:</p>
<pre><code>$ python -m timeit "for i in xrange(10111):" " for k in range(100):" " pass"
10 loops, best of 3: 59.4 msec per loop
$ python -m timeit "for i in xrange(10111):" " for k in xrange(100):" " pass"
10 ... | 1 | 2015-05-30T11:23:20Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 30,997,385 | <p><strong>range():</strong> range(1, 10) returns a list from 1 to 10 numbers & hold whole list in memory.</p>
<p><strong>xrange():</strong> Like range(), but instead of returning a list, returns an object that generates the numbers in the range on demand. For looping, this is lightly faster than range() and more ... | 6 | 2015-06-23T08:16:59Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 31,761,387 | <p>Read the following post for the comparison between range and xrange with graphical analysis.</p>
<p><a href="http://justindailey.blogspot.in/2011/09/python-range-vs-xrange.html" rel="nofollow">Python range Vs xrange</a></p>
| 2 | 2015-08-01T11:37:56Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 34,877,258 | <p>xrange() and range() in python works similarly as for the user , but the difference comes when we are talking about how the memory is allocated in using both the function.</p>
<p>When we are using range() we allocate memory for all the variables it is generating, so it is not recommended to use with larger no. of v... | 4 | 2016-01-19T12:48:45Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 35,680,931 | <p>In python 2.x</p>
<p><strong>range(x)</strong> returns a list, that is created in memory with x elements.</p>
<pre><code>>>> a = range(5)
>>> a
[0, 1, 2, 3, 4]
</code></pre>
<p><strong>xrange(x)</strong> returns an xrange object which is a generator obj which generates the numbers on demand. the... | 3 | 2016-02-28T09:42:31Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
What is the difference between range and xrange functions in Python 2.X? | 94,935 | <p>Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about</p>
<pre><code>for i in range(0, 20):
for i in xrange(0, 20):
</code></pre>
| 378 | 2008-09-18T17:52:51Z | 38,318,039 | <p><code>range(x,y)</code> returns a list of each number in between x and y if you use a <code>for</code> loop, then <code>range</code> is slower. In fact, <code>range</code> has a bigger Index range. <code>range(x.y)</code> will print out a list of all the numbers in between x and y</p>
<p><code>xrange(x,y)</code> re... | 4 | 2016-07-12T00:05:27Z | [
"python",
"loops",
"range",
"python-2.x",
"xrange"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 95,014 | <p>An option you may consider, if an LL parser is ok to you, is to give <a href="http://www.antlr.org/wiki/display/ANTLR3/Antlr3PythonTarget" rel="nofollow">ANTLR</a> a try, it can generate python too (actually it is LL(*) as they name it, * stands for the quantity of lookahead it can cope with).</p>
| 3 | 2008-09-18T18:00:05Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 95,035 | <p>There's ANTLR, which is LL(*), there's PyParsing, which is more object friendly and is sort of like a DSL, and then there's <a href="http://www.canonware.com/Parsing/" rel="nofollow">Parsing</a> which is like OCaml's Menhir.</p>
| 1 | 2008-09-18T18:02:07Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 95,102 | <p><a href="http://www.antlr.org/" rel="nofollow">ANTLR</a> is great and has the added benefit of working across multiple languages.</p>
| 0 | 2008-09-18T18:08:28Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 95,417 | <p>PySec is another monadic parser, I don't know much about it, but it's worth looking at <a href="http://www.valuedlessons.com/2008/02/pysec-monadic-combinatoric-parsing-in.html" rel="nofollow">here</a></p>
| 4 | 2008-09-18T18:34:52Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 95,707 | <p>I believe that <a href="http://pyparsing.wikispaces.com/">pyparsing</a> is based on the same principles as parsec.</p>
| 7 | 2008-09-18T18:58:12Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Python implementation of Parsec? | 94,952 | <p>I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the st... | 10 | 2008-09-18T17:54:39Z | 104,547 | <p>Nothing prevents you for diverting your parser from the "context free" path using PLY. You can pass information to the lexer during parsing, and in this way achieve full flexibility. I'm pretty sure that you can parse anything you want with PLY this way.</p>
<p>For a hands-on example, <a href="http://code.google.co... | 1 | 2008-09-19T18:58:07Z | [
"python",
"parsing",
"parsec",
"combinators"
] |
Does an application-wide exception handler make sense? | 95,642 | <p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p>
<p>If my application crashes, I want to ensure these system resources are properly released.</p>
<... | 10 | 2008-09-18T18:52:30Z | 95,659 | <p>Application wide handler is fine. They are great for logging. Just make sure that the application wide one is durable and is unlikely to crash itself.</p>
| 2 | 2008-09-18T18:54:18Z | [
"python",
"exception-handling"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.