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
Help sorting: first by this, and then by that
1,657,242
<p>I have a list of tuples I am trying to sort and could use some help. </p> <p>The field I want to sort by in the tuples looks like "XXX_YYY". First, I want to group the XXX values in reverse order, and then, within those groups, I want to place the YYY values in normal sort order. (NOTE: I am just as happy, actua...
5
2009-11-01T14:04:59Z
1,657,285
<pre><code>&gt;&gt;&gt; def my_cmp(tuple_1, tuple_2): xxx_1, yyy_1 = tuple_1[0].split('_') xxx_2, yyy_2 = tuple_2[0].split('_') if xxx_1 &gt; xxx_2: return -1 elif xxx_1 &lt; xxx_2: return 1 else: return cmp(yyy_1, yyy_2) &gt;&gt;&gt; import pprint &gt;&gt;&gt; pprint.pprint(sorted(...
2
2009-11-01T14:22:20Z
[ "python", "sorting" ]
Help sorting: first by this, and then by that
1,657,242
<p>I have a list of tuples I am trying to sort and could use some help. </p> <p>The field I want to sort by in the tuples looks like "XXX_YYY". First, I want to group the XXX values in reverse order, and then, within those groups, I want to place the YYY values in normal sort order. (NOTE: I am just as happy, actua...
5
2009-11-01T14:04:59Z
1,657,512
<p>Custom <em>comparison</em> functions for sorting, as suggested in existing answers, do make it easy to sort in a mix of ascending and descending orders -- but they have serious performance issues and have been removed in Python 3, leaving only the preferred customization approach -- custom <strong>key-extraction</st...
8
2009-11-01T16:01:42Z
[ "python", "sorting" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,318
<p>Similar question <a href="http://stackoverflow.com/questions/826493/python-mixing-files-and-loops">here</a>. You can't mix iteration and readline so you need to use one or the other.</p> <pre><code>while True: line1 = f.readline() line2 = f.readline() if not line2: break # EOF ... </code></pre>
33
2009-11-01T14:35:56Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,337
<p>use line.next(), eg</p> <pre><code>f=open("file") for line in f: print line nextline=f.next() print "next line", nextline .... f.close() </code></pre>
18
2009-11-01T14:41:06Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,340
<p>Works for even and odd-length files. It just ignores the unmatched last line.</p> <pre><code>f=file("file") lines = f.readlines() for even, odd in zip(lines[0::2], lines[1::2]): print "even : ", even print "odd : ", odd print "end cycle" f.close() </code></pre> <p>If you have large files, this is not ...
4
2009-11-01T14:43:50Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,348
<pre> file_name = 'your_file_name' file_open = open(file_name, 'r') def handler(line_one, line_two): print(line_one, line_two) while file_open: try: one = file_open.next() two = file_open.next() handler(one, two) except(StopIteration): file_open.close() break </pre...
2
2009-11-01T14:45:06Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,350
<p>My idea is to create a generator that reads two lines from the file at a time, and returns this as a 2-tuple, This means you can then iterate over the results.</p> <pre><code>from cStringIO import StringIO def read_2_lines(src): while True: line1 = src.readline() if not line1: break ...
0
2009-11-01T14:46:01Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,365
<p>This Python code will print the first two lines:</p> <pre><code>import linecache filename = "ooxx.txt" print(linecache.getline(filename,2)) </code></pre>
-2
2009-11-01T14:56:21Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,385
<pre><code>import itertools with open('a') as f: for line1,line2 in itertools.izip_longest(*[f]*2): print(line1,line2) </code></pre> <p>izip_longest returns an iterator, so it should work well even if the file is very large.</p> <p>If there are an odd number of lines, then line2 gets the value None on the...
28
2009-11-01T15:12:54Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,443
<pre><code>def readnumlines(file, num=2): f = iter(file) while True: lines = [None] * num for i in range(num): try: lines[i] = f.next() except StopIteration: # EOF or not enough lines available return yield lines # use like this f ...
2
2009-11-01T15:33:38Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,503
<p>I would proceed in a similar way as <a href="http://stackoverflow.com/questions/1657299/how-do-i-read-two-lines-from-a-file-at-a-time-using-python/1657337#1657337">ghostdog74</a>, only with the try outside and a few modifications:</p> <pre><code>try: with open(filename) as f: for line1 in f: ...
10
2009-11-01T16:00:30Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
1,657,919
<p>I have worked on a similar problem last month. I tried a while loop with f.readline() as well as f.readlines(). My data file is not huge, so I finally chose f.readlines(), which gives me more control of the index, otherwise I have to use f.seek() to move back and forth the file pointer.</p> <p>My case is more compl...
0
2009-11-01T18:36:35Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
5,148,640
<p>Simple little reader. It will pull lines in pairs of two and return them as a tuple as you iterate over the object. You can close it manually or it will close itself when it falls out of scope.</p> <pre><code>class doublereader: def __init__(self,filename): self.f = open(filename, 'r') def __iter__(...
0
2011-02-28T23:06:12Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
10,740,697
<p>how about this one, anybody seeing a problem with it</p> <pre><code>f=open('file_name') for line,line2 in zip(f,f): print line,line2 </code></pre>
2
2012-05-24T15:35:07Z
[ "python" ]
How do I read two lines from a file at a time using python
1,657,299
<p>I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?</p> <p>I would like to some something like:</p> <pre><code>f = open(filename, "r") f...
61
2009-11-01T14:27:27Z
16,818,794
<pre><code>f = open(filename, "r") for line in f: line1 = line f.next() f.close </code></pre> <p>Right now, you can read file every two line. If you like you can also check the f status before <code>f.next()</code></p>
1
2013-05-29T16:03:59Z
[ "python" ]
Is it possible to run a single doctest in Django using "manage.py test"
1,657,442
<p>In my Django app, I have a mix of doctests (defined in models.py and views.py) and unit tests (defined in tests.py. I can invoke an individual unit test by doing: </p> <pre><code>manage.py test app.TestCase </code></pre> <p>However, this doesn't seem to work for the doctests. Is there some way to run a single doc...
3
2009-11-01T15:33:09Z
1,657,762
<p>I think the way python doctests work is to simply run all doctests in a module in the same interpreter instance.</p> <p>I doubt that manage.py can alter that basic behavior of python doctests.</p>
3
2009-11-01T17:39:10Z
[ "python", "django", "doctest" ]
Is it possible to run a single doctest in Django using "manage.py test"
1,657,442
<p>In my Django app, I have a mix of doctests (defined in models.py and views.py) and unit tests (defined in tests.py. I can invoke an individual unit test by doing: </p> <pre><code>manage.py test app.TestCase </code></pre> <p>However, this doesn't seem to work for the doctests. Is there some way to run a single doc...
3
2009-11-01T15:33:09Z
5,252,777
<p>I posted a small script that lets you run doctests in a specific file or module in your project. Running doctests from a specific file or module: <a href="http://dodrum.blogspot.com/2011/01/running-doctests-in-django-project.html" rel="nofollow">LINK</a>.</p> <p>It makes sure that the correct testing environment is...
1
2011-03-09T22:03:51Z
[ "python", "django", "doctest" ]
Is it possible to run a single doctest in Django using "manage.py test"
1,657,442
<p>In my Django app, I have a mix of doctests (defined in models.py and views.py) and unit tests (defined in tests.py. I can invoke an individual unit test by doing: </p> <pre><code>manage.py test app.TestCase </code></pre> <p>However, this doesn't seem to work for the doctests. Is there some way to run a single doc...
3
2009-11-01T15:33:09Z
38,318,707
<p>Try it this way:</p> <pre><code>python -m doctest views.py </code></pre>
0
2016-07-12T01:36:51Z
[ "python", "django", "doctest" ]
web2py - display a SQL query in a form
1,657,480
<p>i have a SQL query</p> <pre><code>family_members = db(\ db.member.parent_membership_id==parent_id.membership_id\ ).select(\ db.member.first_name, db.member.parent_membership_id) </code></pre> <p>I want to display "family_members" as a table in my form.</p> <p>How can i do this?</p>
2
2009-11-01T15:50:50Z
1,657,617
<p>You can follow the example I've shown you <a href="http://stackoverflow.com/questions/1550368/web2py-how-to-inject-html/1550413#1550413">in a previous question</a>.</p> <p>Make sure to also check the documentation on the <a href="http://www.web2py.com/examples/default/docs" rel="nofollow">web2py website</a>, seeing...
2
2009-11-01T16:37:58Z
[ "python", "web2py" ]
web2py - display a SQL query in a form
1,657,480
<p>i have a SQL query</p> <pre><code>family_members = db(\ db.member.parent_membership_id==parent_id.membership_id\ ).select(\ db.member.first_name, db.member.parent_membership_id) </code></pre> <p>I want to display "family_members" as a table in my form.</p> <p>How can i do this?</p>
2
2009-11-01T15:50:50Z
1,658,955
<p>In view:</p> <pre><code>{{=family_members}} </code></pre>
3
2009-11-02T00:51:57Z
[ "python", "web2py" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
1,657,597
<p>There's a simple example <a href="http://www.ajaxlines.com/ajax/stuff/article/%5Fgoogle%5Fajax%5Fsearch%5Fapi%5Fexample%5Fpython%5Fcode.php">here</a> (peculiarly missing some quotes;-). Most of what you'll see on the web is Python interfaces to the old, discontinued SOAP API -- the example I'm pointing to uses the ...
71
2009-11-01T16:30:16Z
[ "python", "api", "google-search-api" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
1,658,018
<p>Here is Alex's answer ported to Python3</p> <pre><code>#!/usr/bin/python3 import json import urllib.request, urllib.parse def showsome(searchfor): query = urllib.parse.urlencode({'q': searchfor}) url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&amp;%s' % query search_response = urllib.request...
16
2009-11-01T19:09:14Z
[ "python", "api", "google-search-api" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
8,802,156
<p>Here's my approach to this: <a href="http://breakingcode.wordpress.com/2010/06/29/google-search-python/">http://breakingcode.wordpress.com/2010/06/29/google-search-python/</a></p> <p>A couple code examples:</p> <pre><code> # Get the first 20 hits for: "Breaking Code" WordPress blog from google import search...
11
2012-01-10T10:57:45Z
[ "python", "api", "google-search-api" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
14,744,162
<p>I am new in python and I was investigating how to do this. None of the provided examples are working properly for me. Some are blocked by google if you make many (few) requests, some are outdated. Parsing the google search html (adding the header in the request) will work until google changes the html structure aga...
6
2013-02-07T05:23:07Z
[ "python", "api", "google-search-api" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
30,449,983
<p>A note is that this will only return the top 4 results from Google... I am still searching for a way to get more results, but the top 4 will work for me for now</p>
0
2015-05-26T05:15:25Z
[ "python", "api", "google-search-api" ]
Google Search from a Python App
1,657,570
<p>I'm trying to run a google search query from a python app. Is there any python interface out there that would let me do this? If there isn't does anyone know which Google API will enable me to do this. Thanks.</p>
49
2009-11-01T16:21:32Z
31,229,695
<p>You might also be interested in this Python module <a href="https://github.com/rnikhil275/pygoogle" rel="nofollow">GitHub-pygoogle</a></p>
1
2015-07-05T11:02:52Z
[ "python", "api", "google-search-api" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
1,657,626
<p>I use rsync to back up all of my clients' web sites. A script is triggered by cron and it uses Makefiles for each client because of their different needs.</p> <p>Rather than do something that enters the password, use ssh-keygen to create a public/private key pair and put your public key on the remote machine. This ...
4
2009-11-01T16:42:13Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
1,657,639
<p>If you need to programatically control a sub-process, you should look into using <a href="http://sourceforge.net/projects/pexpect/" rel="nofollow">pexpect</a>. </p>
1
2009-11-01T16:47:14Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
1,657,700
<p>If you'd like to interact with a subprocess in general, you can use <a href="http://www.noah.org/wiki/Pexpect">pexpect</a> as mentioned elsewhere. But for your particular case, assuming your rsync is running over ssh (the default), then you may want to consider setting up a <a href="http://www.debian-administration...
20
2009-11-01T17:15:00Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
1,658,516
<p>I don't think it supports rsync out of the box, but <a href="http://www.lag.net/paramiko/" rel="nofollow">paramiko</a> might have some components you could recycle?</p>
1
2009-11-01T22:00:48Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
3,001,511
<p>If you just need to enter the password, you can try populating the <code>RSYNC_PASSWORD</code> environmental variable or using the <code>--password-file</code> option.</p>
0
2010-06-08T21:39:03Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
4,514,596
<p>There's a great Python module written by Colin Stewart called RSyncBackup that's little known and little documented, but very useful.</p> <p>By default, it doesn't contain any methods for including a password in your rsync commands, so I modified the module and talked about it in this blog post: <a href="http://tec...
7
2010-12-22T23:17:44Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
18,924,157
<p>I feel bad, for answering this late, but I feel like everyone else's answer was wrong. They did KINDA answer your question, but not directly as they could have.</p> <p>More to the point, you had asked how to grab the password interactively. To do so I would suggest the built-in getpass. In short, you are not int...
2
2013-09-20T19:10:00Z
[ "python", "bash", "automation", "rsync" ]
Controlling rsync with Python?
1,657,614
<p>I've been wanting to write a python script that would run several instances of rsync in sequence for backing up data to a different computer.</p> <p>At the moment I just have this text file with the commands I use and I've just been copy-pasting them into the terminal, and it seems kinda silly.</p> <p>I want to be...
17
2009-11-01T16:36:51Z
33,334,282
<p>I've made a package called paralle_rsync that does rsync commands in parallel. You can use it within fabric to perform the operation on multiple hosts at the same time.</p>
0
2015-10-25T19:46:43Z
[ "python", "bash", "automation", "rsync" ]
Subversion python bindings could not be loaded
1,657,918
<p>This is a but of a part 2 in trying to convert an SVN repository to a Mercurial one</p> <p>command is:</p> <pre><code>hg convert file://c:/svnrepository </code></pre> <p>but, the output I get is:</p> <pre><code>assuming destination svnrepository-hg initializing destination svnrepository-hg repository file://c:/s...
20
2009-11-01T18:36:09Z
1,657,981
<p>The problem's explained <a href="http://mercurial.selenic.com/wiki/ConvertExtension" rel="nofollow">here</a> at heading "Converting from Subversion":</p> <blockquote> <p>Subversion's Python bindings are a prerequisite. The bindings (generated with SWIG) are installed separately on Windows, and can be found ...
14
2009-11-01T18:55:32Z
[ "python", "windows", "svn", "mercurial" ]
Subversion python bindings could not be loaded
1,657,918
<p>This is a but of a part 2 in trying to convert an SVN repository to a Mercurial one</p> <p>command is:</p> <pre><code>hg convert file://c:/svnrepository </code></pre> <p>but, the output I get is:</p> <pre><code>assuming destination svnrepository-hg initializing destination svnrepository-hg repository file://c:/s...
20
2009-11-01T18:36:09Z
1,947,039
<pre><code>sudo apt-get install python-subversion </code></pre> <p>did the trik for me on ubuntu..</p>
5
2009-12-22T15:23:32Z
[ "python", "windows", "svn", "mercurial" ]
Subversion python bindings could not be loaded
1,657,918
<p>This is a but of a part 2 in trying to convert an SVN repository to a Mercurial one</p> <p>command is:</p> <pre><code>hg convert file://c:/svnrepository </code></pre> <p>but, the output I get is:</p> <pre><code>assuming destination svnrepository-hg initializing destination svnrepository-hg repository file://c:/s...
20
2009-11-01T18:36:09Z
3,410,421
<p>I just wanted to bring the actual solution out of the comments to Alex Martelli's answer:</p> <blockquote> <p>According to: selenic.com/pipermail/mercurial/2009-May/… the subversion bindings are included in tortoisehg. So you just need to enable the convert extension in tortoisehg. – tonfa</p> <p>Ah ha!...
21
2010-08-04T22:37:32Z
[ "python", "windows", "svn", "mercurial" ]
Does Django's Unit Testing Raise Warnings to Exceptions?
1,658,265
<p>I am using Django's unit testing apparatus (manage.py test), which is throwing an error and halting when the code generates a warning. This same code when tested with the standard Python unittest module, generates warnings but continues code execution through them.</p> <p>A little research shows that Python can be...
4
2009-11-01T20:47:49Z
1,666,101
<p>Given the updated info, I'm inclined to say that this is the right thing for Django to be doing; MySQL's warnings can indicate any number of things up to and including loss of data (e.g., MySQL will warn and silently truncate if you try to insert a value larger than a column can hold), and that's the sort of thing y...
2
2009-11-03T09:02:30Z
[ "python", "django", "unit-testing" ]
Why is there a need to explicitly delete the sys.exc_info() traceback in Python?
1,658,293
<p>I've seen in different code bases and just read on PyMOTW (see the first Note <a href="http://www.doughellmann.com/PyMOTW/sys/exceptions.html" rel="nofollow">here</a>).</p> <p>The explanation says that a cycle will be created in case the traceback is assigned to a variable from <code>sys.exc_info()[2]</code>, but w...
9
2009-11-01T20:57:14Z
1,658,335
<p>The traceback contains references to all the active frames, which in turn contain references to all the local variables in those various frames -- those references are a big part of the very job of traceback and frame objects, so that's hardly surprising. So, if you add a reference back to the traceback (or fail to...
9
2009-11-01T21:11:56Z
[ "python" ]
Why is there a need to explicitly delete the sys.exc_info() traceback in Python?
1,658,293
<p>I've seen in different code bases and just read on PyMOTW (see the first Note <a href="http://www.doughellmann.com/PyMOTW/sys/exceptions.html" rel="nofollow">here</a>).</p> <p>The explanation says that a cycle will be created in case the traceback is assigned to a variable from <code>sys.exc_info()[2]</code>, but w...
9
2009-11-01T20:57:14Z
1,658,336
<p>The Python garbage collector will, eventually, find and delete circular references like the one created by referring to a traceback stack from inside one of the stack frames themselves, so don't go back and rewrite your code. But, going forward, you could follow the advice of</p> <p><a href="http://docs.python.org...
18
2009-11-01T21:12:03Z
[ "python" ]
searching within nested list in python
1,658,505
<p>I have a list:</p> <pre><code>l = [['en', 60, 'command'],['sq', 34, 'komand']] </code></pre> <p>I want to search for <code>komand</code> or <code>sq</code> and get <code>l[1]</code> returned.</p> <p>Can I somehow define my own matching function for list searches?</p>
4
2009-11-01T21:57:23Z
1,658,524
<p>You can do it this way:</p> <pre><code>def find(value, seq): for index, item in enumerate(seq): if value in item: return index, item In [10]: find('sq', [['en', 60, 'command'],['sq', 34, 'komand']]) Out[10]: (1, ['sq', 34, 'komand']) </code></pre> <p>Or if you want a general solution:</p>...
1
2009-11-01T22:05:59Z
[ "python", "list", "nested", "search" ]
searching within nested list in python
1,658,505
<p>I have a list:</p> <pre><code>l = [['en', 60, 'command'],['sq', 34, 'komand']] </code></pre> <p>I want to search for <code>komand</code> or <code>sq</code> and get <code>l[1]</code> returned.</p> <p>Can I somehow define my own matching function for list searches?</p>
4
2009-11-01T21:57:23Z
1,658,529
<p>If all you're trying to do is return the first list that contains a match for any of the values then this will work.</p> <pre><code>def search(inlist, matches): for li in inlist: for m in matches: if m in li: return li return None &gt;&gt;&gt; l = [['en', 60, 'command'],...
0
2009-11-01T22:06:40Z
[ "python", "list", "nested", "search" ]
searching within nested list in python
1,658,505
<p>I have a list:</p> <pre><code>l = [['en', 60, 'command'],['sq', 34, 'komand']] </code></pre> <p>I want to search for <code>komand</code> or <code>sq</code> and get <code>l[1]</code> returned.</p> <p>Can I somehow define my own matching function for list searches?</p>
4
2009-11-01T21:57:23Z
1,658,585
<p>Yes:</p> <pre><code>has_oneof = lambda *patterns: lambda: values any(p in values for p in patterns) result = itertools.ifilter(has_oneof('komand', 'sq'), l).next() print result # prints ['sq', 34, 'komand'] </code></pre>
0
2009-11-01T22:24:06Z
[ "python", "list", "nested", "search" ]
searching within nested list in python
1,658,505
<p>I have a list:</p> <pre><code>l = [['en', 60, 'command'],['sq', 34, 'komand']] </code></pre> <p>I want to search for <code>komand</code> or <code>sq</code> and get <code>l[1]</code> returned.</p> <p>Can I somehow define my own matching function for list searches?</p>
4
2009-11-01T21:57:23Z
1,658,785
<p>An expression like:</p> <pre><code>next(subl for subl in l if 'sq' in subl) </code></pre> <p>will give you exactly the sublist you're searching for (or raise <code>StopIteration</code> if there is no such sublist; if the latter behavior is not what you want, pass <code>next</code> a second argument [[e.g, <code>[]...
11
2009-11-01T23:30:51Z
[ "python", "list", "nested", "search" ]
Issues with mechanize follow_link() and back()
1,658,509
<p>I've run into a problem with mechanize following links. Here's a snippet of what I'm aiming to do:</p> <pre><code>for link in mech.links(url_regex='/test/'): mech.follow_link(link) // Do some processing on that link mech.back() </code></pre> <p>According to mechanize examples, this should work just f...
4
2009-11-01T21:58:41Z
1,691,596
<p>Pirate, I agree, this shouldn't be happening, you're doing pretty much exactly what the documentation page at wwwsearch.sourceforge.net/mechanize/ says; I tried code similar to yours and got the same result where it stopped after the first iteration.</p> <p>I did, however, find a work-around, namely to save the lin...
2
2009-11-07T01:34:11Z
[ "python", "mechanize" ]
Issues with mechanize follow_link() and back()
1,658,509
<p>I've run into a problem with mechanize following links. Here's a snippet of what I'm aiming to do:</p> <pre><code>for link in mech.links(url_regex='/test/'): mech.follow_link(link) // Do some processing on that link mech.back() </code></pre> <p>According to mechanize examples, this should work just f...
4
2009-11-01T21:58:41Z
12,436,575
<p>A much more straightforward workaround than saving the link list is to simply get a second Browser object. This can be considered equivalent to opening a second tab in a "real" browser. If you also need authentication, you will need to share a cookie jar between browser instances:</p> <pre><code>import mechanize im...
1
2012-09-15T10:26:23Z
[ "python", "mechanize" ]
Issues with mechanize follow_link() and back()
1,658,509
<p>I've run into a problem with mechanize following links. Here's a snippet of what I'm aiming to do:</p> <pre><code>for link in mech.links(url_regex='/test/'): mech.follow_link(link) // Do some processing on that link mech.back() </code></pre> <p>According to mechanize examples, this should work just f...
4
2009-11-01T21:58:41Z
28,164,445
<p>every page visit resets the links() iterator to the links on that new page. you therefore need to save it to a separate variable, e.g.: <code>links = mech.links()</code>, or as Chirael indicated, <code>links = list(mech.links())</code>, which has the advantage of being able to be counted with <code>print &gt;&gt;sys...
0
2015-01-27T06:24:20Z
[ "python", "mechanize" ]
Range of valid numpy values
1,658,714
<p>I'm interested in finding for a particular Numpy type (e.g. np.int64, np.uint32, np.float32, etc.) what the range of all possible valid values is (e.g. np.int32 can store numbers up to 2**31-1). Of course, I guess one can theoretically figure this out for each type, but is there a way to do this at run time to ensur...
14
2009-11-01T23:07:07Z
1,658,752
<p>Quoting from a numpy dicussion list:</p> <pre><code>That kind of information is available via numpy.finfo() and numpy.iinfo(): In [12]: finfo('d').max Out[12]: 1.7976931348623157e+308 In [13]: iinfo('i').max Out[13]: 2147483647 In [14]: iinfo(uint8).max Out[14]: 255 </code></pre> <p>The link is here: <a href="h...
26
2009-11-01T23:19:26Z
[ "python", "numpy" ]
Range of valid numpy values
1,658,714
<p>I'm interested in finding for a particular Numpy type (e.g. np.int64, np.uint32, np.float32, etc.) what the range of all possible valid values is (e.g. np.int32 can store numbers up to 2**31-1). Of course, I guess one can theoretically figure this out for each type, but is there a way to do this at run time to ensur...
14
2009-11-01T23:07:07Z
1,658,755
<p>You can use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.iinfo.html"><code>numpy.iinfo(arg).max</code></a> to find the max value for integer types of <code>arg</code>, and <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.finfo.html"><code>numpy.finfo(arg).max</code></a> to fin...
10
2009-11-01T23:20:55Z
[ "python", "numpy" ]
Python: how to make a function visible throughout a program
1,658,722
<p>I have two functions like the following:</p> <pre><code>def fitnesscompare(x, y): if x.fitness&gt;y.fitness: return 1 elif x.fitness==y.fitness: return 0 else: #x.fitness&lt;y.fitness return -1 </code></pre> <p>that are used with 'sort' to sort on different attributes of cl...
0
2009-11-01T23:10:07Z
1,658,727
<p>Defining a function with <code>def</code> makes that function available within whatever <em>scope</em> you've defined it in. At module level, using <code>def</code> will make that function available to any other function inside that module.</p> <p>Can you perhaps post an example of what is <em>not</em> working for ...
1
2009-11-01T23:12:00Z
[ "python" ]
Python: how to make a function visible throughout a program
1,658,722
<p>I have two functions like the following:</p> <pre><code>def fitnesscompare(x, y): if x.fitness&gt;y.fitness: return 1 elif x.fitness==y.fitness: return 0 else: #x.fitness&lt;y.fitness return -1 </code></pre> <p>that are used with 'sort' to sort on different attributes of cl...
0
2009-11-01T23:10:07Z
1,658,762
<p>The best approach (to get the visibility you ask about) is to put this <code>def</code> statement in a module (say fit.py), <code>import fit</code> from any other module that needs access to items defined in this one, and use <code>fit.fitnesscompare</code> in any of those modules as needed.</p> <p>What you ask, an...
6
2009-11-01T23:22:45Z
[ "python" ]
Random int64 and float64 numbers
1,658,808
<p>I'm trying to generate random 64-bit integer values for integers and floats using Numpy, <strong>within the entire range of valid values for that type</strong>. To generate random 32-bit floats, I can use:</p> <pre><code>In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10) O...
8
2009-11-01T23:42:29Z
1,658,826
<p>The issue seems to be that the <code>random_numbers</code> method expects only 32-bit integers.</p> <p>According to <a href="http://projects.scipy.org/numpy/ticket/555" rel="nofollow">ticket #555</a> random seeds can now be 64-bit as of version 1.1.0 I suggest downloading and installing the latest version of NumPy...
0
2009-11-01T23:52:19Z
[ "python", "random", "numpy" ]
Random int64 and float64 numbers
1,658,808
<p>I'm trying to generate random 64-bit integer values for integers and floats using Numpy, <strong>within the entire range of valid values for that type</strong>. To generate random 32-bit floats, I can use:</p> <pre><code>In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10) O...
8
2009-11-01T23:42:29Z
1,658,874
<p>For integers you could generate 2 32 bit random numbers and combine them:</p> <pre><code>a + (b &lt;&lt; 32) </code></pre>
7
2009-11-02T00:12:37Z
[ "python", "random", "numpy" ]
Random int64 and float64 numbers
1,658,808
<p>I'm trying to generate random 64-bit integer values for integers and floats using Numpy, <strong>within the entire range of valid values for that type</strong>. To generate random 32-bit floats, I can use:</p> <pre><code>In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10) O...
8
2009-11-01T23:42:29Z
1,658,939
<p>It would appear that the code for <code>numpy.random.uniform()</code> does high-low calculation at some point, and the Inf stems from there.</p> <p>Uniformly distributed integers are easy to generate as was shown. Uniformly distributed floating point numbers would require rather more careful thought.</p> <p>As for...
2
2009-11-02T00:43:32Z
[ "python", "random", "numpy" ]
Random int64 and float64 numbers
1,658,808
<p>I'm trying to generate random 64-bit integer values for integers and floats using Numpy, <strong>within the entire range of valid values for that type</strong>. To generate random 32-bit floats, I can use:</p> <pre><code>In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10) O...
8
2009-11-01T23:42:29Z
7,943,485
<p>I don't believe it refers to the random seed call. The simplest code I've got that falls into "Python int too large to convert to C long" is: </p> <pre><code>x = numpy.random.random_integers(2**64,size=(SIZE,)).astype(numpy.uint64) </code></pre> <p>numpy.<strong>version</strong>=1.5.0 here</p>
0
2011-10-30T05:31:01Z
[ "python", "random", "numpy" ]
module to abstract limitations of GQL
1,658,829
<p>I am after a Python module for Google App Engine that abstracts away limitations of the GQL.</p> <p>Specifically I want to store big files (> 1MB) and retrieve all records for a model (> 1000). I have my own code that handles this at present but would prefer to build on existing work, if available.</p> <p>Thanks</...
0
2009-11-01T23:54:12Z
1,660,404
<p>I'm not aware of any libraries that do that. You may want to reconsider what you're doing, at least in terms of retrieving more than 1000 results - those operations are not available because they're expensive, and needing to evade them is usually (though not always) a sign that you need to rearchitect your app to do...
1
2009-11-02T10:11:14Z
[ "python", "google-app-engine", "gql" ]
Django unique_together and flagging objects as "deleted"
1,658,945
<p>I'm implementing the first option discussed in <a href="http://jtauber.com/blog/2008/12/19/marking%5Ffor%5Fdeletion%5Fin%5Fdjango/" rel="nofollow">"Marking for deletion in Django"</a>, that is, when an object is no longer active, I set a boolean to mark it as inactive.</p> <p>The specific reason I'm using this meth...
1
2009-11-02T00:49:22Z
1,659,035
<p>You could have a unique constraint:</p> <pre><code>class Meta: unique_together = ( ('name', 'active'),) </code></pre> <p>However, that means you can only have one active and one inactive object with the same name.</p> <p>If you make active a NullBooleanField, then you can have NULL for active, and have (IIRC...
3
2009-11-02T01:30:38Z
[ "python", "django", "database-design", "django-models" ]
Django unique_together and flagging objects as "deleted"
1,658,945
<p>I'm implementing the first option discussed in <a href="http://jtauber.com/blog/2008/12/19/marking%5Ffor%5Fdeletion%5Fin%5Fdjango/" rel="nofollow">"Marking for deletion in Django"</a>, that is, when an object is no longer active, I set a boolean to mark it as inactive.</p> <p>The specific reason I'm using this meth...
1
2009-11-02T00:49:22Z
1,659,076
<p>I think I understand where you're coming from when you say that <code>unique together</code> should remain on the database level. In the theoretical best of situations, the best design would be one which does not rely on understanding the underlying DB enforcement of <code>unique together</code> and instead acts as...
1
2009-11-02T01:46:01Z
[ "python", "django", "database-design", "django-models" ]
Django unique_together and flagging objects as "deleted"
1,658,945
<p>I'm implementing the first option discussed in <a href="http://jtauber.com/blog/2008/12/19/marking%5Ffor%5Fdeletion%5Fin%5Fdjango/" rel="nofollow">"Marking for deletion in Django"</a>, that is, when an object is no longer active, I set a boolean to mark it as inactive.</p> <p>The specific reason I'm using this meth...
1
2009-11-02T00:49:22Z
11,976,467
<p>Set up a structure like this:</p> <pre><code>class Item(models.Model): unique_field = models.CharField(unique=True, null=True) class DeletedItem(models.Model): original = models.OneToOneField(Item) original_unique_field = models.CharField() </code></pre> <p>To delete of an Item, created a DeletedItem ...
0
2012-08-15T20:08:10Z
[ "python", "django", "database-design", "django-models" ]
How to run code on Pylons startup
1,658,986
<p>I have a Python 2.6 web app built on Pylons 0.9.7. The code in my controller only runs the first time a client requests it, which is fair enough, but is there any way I can run some code as soon as the server starts and is ready to accept requests, without waiting until a request is actually received?</p>
2
2009-11-02T01:06:53Z
1,659,170
<p>It's an environment setting, if that's what you're asking.</p> <p>Specifically: lib/app_globals, modify _ _ init _ _ (). (Ignore the spaces there, silly emboldening function!)</p> <p>See: <a href="http://pylonshq.com/docs/en/0.9.7/configuration/#environment" rel="nofollow">http://pylonshq.com/docs/en/0.9.7/config...
2
2009-11-02T02:27:34Z
[ "python", "pylons" ]
A case of outwardly equal lists of sets behaving differently under Python 2.5 (I think ...)
1,659,039
<p>Four years ago I wrote a Sudoku puzzle solver, and now I'm trying to understand how it works so that I can reuse parts of it for a KenKen puzzle solver. I thought I'd better compactify loops into list comprehensions and pick more self-explanatory names for variables. </p> <p>There's a class Puz which contains the...
0
2009-11-02T01:31:50Z
1,659,049
<p>self.W3 as you've coded it contains many references to the same set object -- as soon as you call any mutating method on one of those references, you've changed all the others. You need to ensure <code>W3_fill_func</code> returns <strong><em>independent copies</em></strong> of the set of interest, just like all oth...
2
2009-11-02T01:37:03Z
[ "python", "set", "python-2.5" ]
Pygtk StatusIcon not loading?
1,659,085
<p>I'm currently working on a small script that needs to use <code>gtk.StatusIcon()</code>. For some reason, I'm getting some weird behavior with it. If I go into the python interactive shell and type:</p> <pre><code>&gt;&gt; import gtk &gt;&gt; statusIcon = gtk.status_icon_new_from_file("img/lin_idle.png") </code></p...
0
2009-11-02T01:50:19Z
1,677,259
<p>You made two mistakes. One is important one is not.</p> <p>At first if you want to use stock icon use .set_from_stock( stock_id ) method. If you want to use your own icon then the .set_from_file(/path/to/img.png) is ok.</p> <p>The other think witch is the probably the main problem is that when you write gtk applic...
1
2009-11-04T22:56:04Z
[ "python", "gtk", "pygtk" ]
Pygtk StatusIcon not loading?
1,659,085
<p>I'm currently working on a small script that needs to use <code>gtk.StatusIcon()</code>. For some reason, I'm getting some weird behavior with it. If I go into the python interactive shell and type:</p> <pre><code>&gt;&gt; import gtk &gt;&gt; statusIcon = gtk.status_icon_new_from_file("img/lin_idle.png") </code></p...
0
2009-11-02T01:50:19Z
1,677,393
<p>You need to call the <code>gtk.main</code> function like qba said, however the correct way to call a function every N milliseconds is to use the <code>gobject.timeout_add</code> function. In most cases you would want to have anything that could tie up the gui in a separate thread, however in your case where you just...
4
2009-11-04T23:28:10Z
[ "python", "gtk", "pygtk" ]
python executing existent (&big) c++ code
1,659,159
<p>I have a program in C++ that uses the <code>cryptopp</code> library to decrypt/encrypt messages.</p> <p>It offers two interface methods <code>encrypt</code> &amp; <code>decrypt</code> that receive a string and operate on it through <code>cryptopp</code> methods.</p> <p>Is there some way to use both methods in Pyth...
1
2009-11-02T02:23:21Z
1,659,176
<p>If you can make a DLL from that C++ code, exposing those two methods (ideally as "extern C", that makes all interfacing tasks so much simpler), <a href="http://docs.python.org/library/ctypes.html?highlight=ctypes#module-ctypes" rel="nofollow">ctypes</a> can be the answer, not requiring any third party tool or extens...
6
2009-11-02T02:29:42Z
[ "c++", "python" ]
python executing existent (&big) c++ code
1,659,159
<p>I have a program in C++ that uses the <code>cryptopp</code> library to decrypt/encrypt messages.</p> <p>It offers two interface methods <code>encrypt</code> &amp; <code>decrypt</code> that receive a string and operate on it through <code>cryptopp</code> methods.</p> <p>Is there some way to use both methods in Pyth...
1
2009-11-02T02:23:21Z
1,659,457
<p>As Alex suggested you can make a dll, export the function you want to access from python and use ctypes(<a href="http://docs.python.org/library/ctypes.html" rel="nofollow">http://docs.python.org/library/ctypes.html</a>) module to access e.g.</p> <pre><code>&gt;&gt;&gt; libc = cdll.LoadLibrary("libc.so.6") &gt;&gt;&...
4
2009-11-02T04:51:40Z
[ "c++", "python" ]
sqlalchemy: AttributeError: 'tuple' object has no attribute 'insert'
1,659,160
<p>I was playing around making a simple haiku site using sqlalchemy and pylons. It basically takes a haiku, writes it to a database, and displays the haiku. The problem appears when I get the data from the form and try and write it to a database, Pylons give me this error: AttributeError: 'tuple' object has no attribut...
0
2009-11-02T02:23:30Z
1,659,179
<p>Well, it looks like you're creating <code>haiku_table</code> and not doing anything else to it before trying to use the .insert function which obviously is not part of a tuple</p> <p>Looks like when you create a table with SQLAlchemy, you want the format:</p> <pre><code>haiku_table = Table('haiku', metadata, ...
1
2009-11-02T02:30:28Z
[ "python", "sqlalchemy", "pylons" ]
MacPython: programmatically finding all serial ports
1,659,283
<p>I am looking for a solution to programmatically return all available serial ports with python.</p> <p>At the moment I am entering <code>ls /dev/tty.*</code> or <code>ls /dev/cu.*</code> into the terminal to list ports and hardcoding them into the pyserial class.</p>
3
2009-11-02T03:16:12Z
1,659,288
<p>You could do something like this:</p> <pre><code>import glob def scan(): return glob.glob('/dev/tty*') + glob.glob('/dev/cu*') for port in scan(): # do something to check this port is open. </code></pre> <p>Then, take a look at <a href="http://pyserial.sourceforge.net/">pyserial</a> for some good utility f...
5
2009-11-02T03:18:47Z
[ "python", "osx", "serial-port" ]
MacPython: programmatically finding all serial ports
1,659,283
<p>I am looking for a solution to programmatically return all available serial ports with python.</p> <p>At the moment I am entering <code>ls /dev/tty.*</code> or <code>ls /dev/cu.*</code> into the terminal to list ports and hardcoding them into the pyserial class.</p>
3
2009-11-02T03:16:12Z
1,659,294
<p>What about just doing the <code>os.listdir</code> / <code>glob</code> equivalent of <code>ls</code> to perform the equivalent of that <code>ls</code>? Of course it's not going to be the case that some usable device is connected to each such special file (but, that holds for <code>ls</code> as well;-), but for "find...
1
2009-11-02T03:22:24Z
[ "python", "osx", "serial-port" ]
Inheriting directly from a built-in type versus its wrapper class in Python
1,659,337
<p>I'm currently reading <a href="http://diveintopython.net/index.html" rel="nofollow">Dive Into Python</a> by Mark Pilgrim, and have gotten to the section on inheritance. In <a href="http://diveintopython.net/object_oriented_framework/userdict.html#fileinfo.userdict.fromdict" rel="nofollow">section 5.5</a>, Pilgrim m...
1
2009-11-02T03:45:50Z
1,659,346
<p>You're right:</p> <blockquote> <p>The need for this class has been largely supplanted by the ability to subclass directly from dict (a feature that became available starting with Python version 2.2). Prior to the introduction of dict, the UserDict class was used to create dictionary-like sub-classes...
3
2009-11-02T03:51:39Z
[ "python", "inheritance", "types", "wrapper", "builtin" ]
Inheriting directly from a built-in type versus its wrapper class in Python
1,659,337
<p>I'm currently reading <a href="http://diveintopython.net/index.html" rel="nofollow">Dive Into Python</a> by Mark Pilgrim, and have gotten to the section on inheritance. In <a href="http://diveintopython.net/object_oriented_framework/userdict.html#fileinfo.userdict.fromdict" rel="nofollow">section 5.5</a>, Pilgrim m...
1
2009-11-02T03:45:50Z
1,659,354
<p>The wrapper classes have been removed from Python 3, as they haven't been all that useful for a while now. The <em>mixin</em> class, <code>UserDict.DictMixin</code>, is a completely different story -- its useful features are now found all over the "abstract base classes" in the <code>collections</code> module (Pyth...
3
2009-11-02T03:54:55Z
[ "python", "inheritance", "types", "wrapper", "builtin" ]
Inheriting directly from a built-in type versus its wrapper class in Python
1,659,337
<p>I'm currently reading <a href="http://diveintopython.net/index.html" rel="nofollow">Dive Into Python</a> by Mark Pilgrim, and have gotten to the section on inheritance. In <a href="http://diveintopython.net/object_oriented_framework/userdict.html#fileinfo.userdict.fromdict" rel="nofollow">section 5.5</a>, Pilgrim m...
1
2009-11-02T03:45:50Z
1,659,358
<p>I found, on the page you linked to, a hint as to the answer:</p> <blockquote> <p>In versions of Python prior to 2.2, you could not directly subclass built-in datatypes like strings, lists, and dictionaries. To compensate for this, Python comes with wrapper classes that mimic the behavior of these buil...
1
2009-11-02T03:56:20Z
[ "python", "inheritance", "types", "wrapper", "builtin" ]
How do I fetch an XML document and parse it with Python twisted?
1,659,380
<p>I want a fast way to grab a URL and parse it while streaming. Ideally this should be super fast. My language of choice is Python. I have an intuition that twisted can do this but I'm at a loss to find an example. </p>
3
2009-11-02T04:06:11Z
1,682,249
<p>You only need to parse a single URL? Then don't worry. Use urllib2 to open the connection and pass the file handle into ElementTree.</p> <p>Variations you can try would be to use ElementTree's incremental parser or to use iterparse, but that depends on what your real requirements are. There's "super fast" but there...
0
2009-11-05T17:33:47Z
[ "python", "xml", "twisted" ]
How do I fetch an XML document and parse it with Python twisted?
1,659,380
<p>I want a fast way to grab a URL and parse it while streaming. Ideally this should be super fast. My language of choice is Python. I have an intuition that twisted can do this but I'm at a loss to find an example. </p>
3
2009-11-02T04:06:11Z
1,690,504
<p>If you need to handle HTTP responses in a streaming fashion, there are a few options.</p> <p>You can do it via <code>downloadPage</code>:</p> <pre><code>from xml.sax import make_parser from twisted.web.client import downloadPage class StreamingXMLParser: def __init__(self): self._parser = make_parser(...
6
2009-11-06T21:16:18Z
[ "python", "xml", "twisted" ]
**kwargs search mechanism in an object (python)
1,659,418
<p>Want to be able to provide a search interface for a collection of objects to be used by passing a list of keyword arguments like so:</p> <pre><code>playerID = players.search(nameFirst='ichiro', nameLast='suzuki') </code></pre> <p>Where players.search is defined like so:</p> <pre><code>def search(self, **args): r...
0
2009-11-02T04:30:04Z
1,659,424
<p>You should be able to change it to a list comprehension with the <a href="http://docs.python.org/library/functions.html#all" rel="nofollow"><code>all</code></a> builtin, which returns True iff all the elements in its argument are true (or if the iterable is empty). Something like this should do the trick:</p> <pre...
1
2009-11-02T04:33:21Z
[ "python" ]
**kwargs search mechanism in an object (python)
1,659,418
<p>Want to be able to provide a search interface for a collection of objects to be used by passing a list of keyword arguments like so:</p> <pre><code>playerID = players.search(nameFirst='ichiro', nameLast='suzuki') </code></pre> <p>Where players.search is defined like so:</p> <pre><code>def search(self, **args): r...
0
2009-11-02T04:30:04Z
1,659,451
<blockquote> <p>I want to, to borrow some SQL idioms, to work like where player.key == value and player.keyN = valueN, and so on for N number of kwargs passed.</p> </blockquote> <p>So you're currently implementing an OR and want to implement an AND instead -- is that it?</p> <p>If so, then the <code>all</code...
1
2009-11-02T04:48:26Z
[ "python" ]
Python socket client to a Java socket server
1,659,584
<p>I have a Java socket server that is expecting exactly <code>n</code> bytes from some port. I want to write a Python clients that just sends bytes on some port to the Java server.</p> <p>Since Python does not have primitives, I'm not sure to send exactly <code>n</code> bytes. Any suggestions?</p> <p>More details:</...
0
2009-11-02T05:42:40Z
1,659,590
<p><code>somesocket.send</code> takes a byte-string argument <code>s</code> -- just ensure that <code>len(s) == n</code>, and you <strong>will</strong> be sending exacty <code>n</code> bytes. What do "primitives" have to do with it?!</p> <p>To turn arbitrary bunches of data into byte strings (and back), see the <a hr...
1
2009-11-02T05:44:46Z
[ "java", "python", "sockets", "client" ]
Python socket client to a Java socket server
1,659,584
<p>I have a Java socket server that is expecting exactly <code>n</code> bytes from some port. I want to write a Python clients that just sends bytes on some port to the Java server.</p> <p>Since Python does not have primitives, I'm not sure to send exactly <code>n</code> bytes. Any suggestions?</p> <p>More details:</...
0
2009-11-02T05:42:40Z
1,659,635
<p>If you are using a <strong>datagram</strong> socket (e. g. the UDP protocol over IP), the Socket API guarantees that if your <code>n</code> is less than the maximum payload size, then your data will be sent in a single packet. So just calling <code>socket.send</code> would be sufficient.</p> <p>The easiest way to s...
1
2009-11-02T06:01:29Z
[ "java", "python", "sockets", "client" ]
Python socket client to a Java socket server
1,659,584
<p>I have a Java socket server that is expecting exactly <code>n</code> bytes from some port. I want to write a Python clients that just sends bytes on some port to the Java server.</p> <p>Since Python does not have primitives, I'm not sure to send exactly <code>n</code> bytes. Any suggestions?</p> <p>More details:</...
0
2009-11-02T05:42:40Z
1,659,836
<p>Thanks to your answers, I figured out what I was looking for. What I wanted was struct.unpack and struct.pack to allow me to pack the python float 1.2345 to a string representation of a C double.</p> <p>That is:</p> <pre><code>&gt;&gt;&gt; struct.pack('d', 1.2345) '\x8d\x97n\x12\x83\xc0\xf3?' &gt;&gt;&gt; struct.u...
0
2009-11-02T07:13:30Z
[ "java", "python", "sockets", "client" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,626
<p>Perhaps that's because it's a <a href="http://wiki.blender.org/index.php/Extensions%3APy/Scripts" rel="nofollow">scripting language for Blender</a>?</p>
6
2009-11-02T05:58:27Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,637
<p>My guess is that it is the tool for the job because it is easy to prototype extra features. </p>
1
2009-11-02T06:01:55Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,654
<p>Because Python is what Basic should have been ;)</p> <p>Its a language designed from the beginning to be used by non-programmers, but with the power to be truly used as a general purpose programming language.</p>
3
2009-11-02T06:06:09Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,703
<p>Aside from the fact that it's already in use, the main advantage is that it's quick to use. Java, C, and friends almost all require tedious coding that merely restates what you already know. Python is designed to be quick to write, quick to modify, and as general as possible.</p> <p>As an example, functions in java...
2
2009-11-02T06:24:50Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,716
<p>I work in this industry, and here's what I've observed:</p> <ul> <li><p>It's a nice, tidy language that's not hard to pick up. You don't have to be a language guru to use it.</p></li> <li><p>It embeds nicely in C/C++ applications.</p></li> <li><p>It has data types, so numeric operations can be done without type sh...
6
2009-11-02T06:27:48Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,774
<p>A few other points I've not seen in the existing answers:</p> <ul> <li>it's free</li> <li>it's fast [enough]</li> <li>it runs on every platform I know of (AIX, HPUX, Linux, Mac OS X, Windows..)</li> <li>quick to learn</li> <li>large, powerful libraries <ul> <li>numeric</li> <li>graphical</li> <li>etc</li> </ul></l...
4
2009-11-02T06:48:43Z
[ "python", "animation" ]
Why is Python a favourite among people working in animation industry?
1,659,620
<p>What is that needs to be coded in Python instead of C/C++ etc? I know its advantages etc. I want to know why exactly makes Python <strong><em>The language</em></strong> for people in this industry?</p>
1
2009-11-02T05:55:48Z
1,659,832
<p>I used python for molecular animation using <a href="http://pymol.sourceforge.net/" rel="nofollow">PyMol</a>. Many molecular visualization programs have their own scripting languages. PyMol's scripting language is python, a real programming language. So, if your task requires calculations of any kind, text parsing o...
1
2009-11-02T07:11:04Z
[ "python", "animation" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,659,705
<p>You can make this program more memory efficient by not reading all 15MB from the TCP connection, but instead processing each line as it is read. This will make the remote servers wait for you, of course, but that's okay. </p> <p>Python is just not very memory efficient. It wasn't built for that.</p>
2
2009-11-02T06:25:40Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,659,854
<p>The last line should surely be <code>f.close()</code>? Those trailing parens are kinda important. </p>
5
2009-11-02T07:21:50Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,659,909
<p>You could do more of your work in compiled C code if you convert this to a list comprehension:</p> <pre><code>data = [] items = GetData(url) for item in items: data.append(';'.join(item)) </code></pre> <p>to:</p> <pre><code>data = [';'.join(items) for items in GetData(url)] </code></pre> <p>This is actually ...
2
2009-11-02T07:43:23Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,659,939
<p>Consider using xrange() instead of range(), I believe that xrange is a generator whereas range() expands the whole list.</p> <p>I'd say either don't read the whole file into memory, or don't keep the whole unpacked structure in memory.</p> <p>Currently you keep both in memory, at the same time, this is going to be...
6
2009-11-02T07:56:08Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,660,031
<p>The major culprit here is as mentioned above the range() call. It will create a list with 15 million members, and that will eat up 200 MB of your memory, and with 15 processes, that's 3GB.</p> <p>But also don't read in the whole 15MB file into data(), read bit by bit from the response. Sticking those 15MB into a va...
7
2009-11-02T08:31:35Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,660,163
<p>There are 2 obvious places where you keep large data objects in memory (<code>data</code> variable in <code>GetData()</code> and <code>data</code> in <code>MyThread.run()</code> - these two will take about 500Mb) and probably there are other places in the skipped code. There are both easy to make memory efficient. U...
2
2009-11-02T09:13:58Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,660,199
<p>Make sure you delete the threads after they are stopped. (using <code>del</code>)</p>
1
2009-11-02T09:22:14Z
[ "python", "memory", "memory-management" ]
How to write a memory efficient Python program?
1,659,659
<p>It's said that Python automatically manages memory. I'm confused because I have a Python program consistently uses more than 2GB of memory.</p> <p>It's a simple multi-thread binary data downloader and unpacker.</p> <pre><code>def GetData(url): req = urllib2.Request(url) response = urllib2.urlopen(req) ...
7
2009-11-02T06:08:08Z
1,660,263
<p>Like others have said, you need at least the following two changes:</p> <ol> <li><p>Do not create a huge list of integers with <code>range</code></p> <pre><code># use xrange for i in xrange(0, count): # UNPACK FIXED LENGTH OF BINARY DATA HERE yield (field1, field2, field3) </code></pre></li> <li><p>do not ...
8
2009-11-02T09:40:03Z
[ "python", "memory", "memory-management" ]
qt - pyqt QTableView not populating when changing databases
1,659,756
<p>I'm trying to allow my users to pick which database to open. Each database will have the same schema. For some reason though I can't get my QTableView to populate after I open the database.</p> <p>I'm paraphrasing the example code but this should give you an idea of what I'm trying to do.</p> <p>works:</p> <pre><...
1
2009-11-02T06:43:11Z
1,668,861
<p>I can't remember today exactly where I found it but as I was researching something else I found some forum posting that said the connection must be made before making the model. I suspect there must be some code in the model construct that's touching the db. I changed my on_actionOpen_DB_triggered to create the mode...
2
2009-11-03T17:22:05Z
[ "python", "qt", "pyqt" ]
Python name grabber
1,659,759
<p>if I have a string in the format of</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) na...
0
2009-11-02T06:43:20Z
1,659,813
<p>Expecting this string:</p> <p><code>Foo NameA Bar MessageA Baz</code></p> <p>this regex will match:</p> <p><code>Foo\s+(\w+)\s+Bar\s+(\w+)\s+Baz</code></p> <p>Group 1 will be the name, group 2 will be the message. FooBarBaz are the static parts.</p> <p>Here it is using the repl of Python:</p> <pre><code>Python...
0
2009-11-02T07:03:34Z
[ "python", "regex", "parsing" ]
Python name grabber
1,659,759
<p>if I have a string in the format of</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) na...
0
2009-11-02T06:43:20Z
1,659,926
<pre><code>&gt;&gt;&gt; s="(static string) name (different static string ) message (last static string)" &gt;&gt;&gt; _,_,s=s.partition("(static string)") &gt;&gt;&gt; name,_,s=s.partition("(different static string )") &gt;&gt;&gt; message,_,s=s.partition("(last static string)") &gt;&gt;&gt; name ' name ' &gt;&gt;&gt; ...
3
2009-11-02T07:50:47Z
[ "python", "regex", "parsing" ]
Python name grabber
1,659,759
<p>if I have a string in the format of</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) na...
0
2009-11-02T06:43:20Z
1,664,525
<p>Here's a full answer showing how to do it using <code>replace()</code>. </p> <pre><code>strings = ['(static string) name (different static string ) message (last static string)', '(static string) name (different static string ) message (last static string)', '(static string) name (different st...
0
2009-11-03T00:16:44Z
[ "python", "regex", "parsing" ]
Python name grabber
1,659,759
<p>if I have a string in the format of</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) name (different static string ) message (last static string)</p> <p>(static string) na...
0
2009-11-02T06:43:20Z
1,664,614
<pre><code>for line in open("file"): line=line.split(")") for item in line: try: print item[:item.index("(")] except:pass </code></pre> <p>output</p> <pre><code>$ more file (static string) name (different static string ) message (last static string) (static string) name (different ...
0
2009-11-03T00:51:39Z
[ "python", "regex", "parsing" ]
what is the difference between BaseHTTPServer and SimpleHTTPServer? when and where to use it?
1,660,045
<p>What is the difference between BaseHTTPServer and SimpleHTTPServer? When and where should i use these?</p>
7
2009-11-02T08:37:06Z
1,660,073
<p><code>BaseHTTPServer</code> is a HTTP server library. It understands the HTTP protocol and let's your code to handle requests. It doesn't have any "logic" on it's own. <code>SimpleHTTPServer</code> is built on top of <code>BaseHTTPServer</code> and handles requests in a similar way normal HTTP servers do, i.e. serve...
6
2009-11-02T08:45:13Z
[ "python", "http", "simplehttpserver", "basehttpserver" ]
Unittesting Corba in Python
1,660,049
<p>I am interested in your opinions on unittesting code that uses Corba to communicate with a server.</p> <p>Would you mock the Corba objects? In Python that's sort of a pain in the ass because all the methods of Corba objects are loaded dynamically. So you're basically stuck with "mock anything".</p> <p>Thanks!</p> ...
2
2009-11-02T08:38:34Z
1,660,185
<p>Don't try to unittest Corba. Assume that Corba works. Unittest <em>your own</em> code. This means:</p> <ol> <li><p>Create a unit test which checks that you correctly set up Corba and that you can invoke a single method and read a property. If that works, all other methods and properties will work, too.</p></li> <li...
3
2009-11-02T09:19:52Z
[ "python", "unit-testing", "mocking", "corba" ]