title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
583,452
<p>In Virtaal's <a href="https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/virtaal/devsupport/profiling.py">source</a> there's a very useful class and decorator that can make it profiling (even for specific methods/functions) very easy. The output can then be viewed very comfortably in KCacheGrind.</p>
10
2009-02-24T20:31:10Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
1,922,945
<p>It's worth pointing out that using the profiler only works (by default) on the main thread, and you won't get any information from other threads if you use them. This can be a bit of a gotcha as it is completely unmentioned in the <a href="http://docs.python.org/library/profile.html">profiler documentation</a>.</p>...
140
2009-12-17T16:30:34Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
7,693,928
<p>The python wiki is a great page for profiling resources: <a href="http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Profiling_Code">http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Profiling_Code</a></p> <p>as is the python docs: <a href="http://docs.python.org/library/profile.html">http://docs.python....
103
2011-10-08T00:04:12Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
7,838,845
<p>A nice profiling module is the line_profiler (called using the script kernprof.py). It can be downloaded <a href="http://packages.python.org/line_profiler/">here</a>.</p> <p>My understanding is that cProfile only gives information about total time spent in each function. So individual lines of code are not timed....
19
2011-10-20T16:05:34Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
8,065,384
<p>Following Joe Shaw's answer about multi-threaded code not to work as expected, I figured that the <code>runcall</code> method in cProfile is merely doing <code>self.enable()</code> and <code>self.disable()</code> calls around the profiled function call, so you can simply do that yourself and have whatever code you w...
8
2011-11-09T12:59:04Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
11,822,995
<p>A while ago I made <a href="http://pycallgraph.slowchop.com/"><code>pycallgraph</code></a> which generates a visualisation from your Python code. <strong>Edit:</strong> I've updated the example to work with the latest release.</p> <p>After a <code>pip install pycallgraph</code> and installing <a href="http://www.gr...
275
2012-08-06T05:37:07Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
12,874,474
<blockquote> <p>Ever want to know what the hell that python script is doing? Enter the Inspect Shell. Inspect Shell lets you print/alter globals and run functions without interrupting the running script. Now with auto-complete and command history (only on linux).</p> <p>Inspect Shell is not a pdb-style deb...
3
2012-10-13T15:21:22Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
13,830,132
<p>@Maxy's comment on <a href="http://stackoverflow.com/a/7693928/25616">this answer</a> helped me out enough that I think it deserves its own answer: I already had cProfile-generated .pstats files and I didn't want to re-run things with pycallgraph, so I used <a href="http://code.google.com/p/jrfonseca/wiki/Gprof2Dot"...
83
2012-12-11T23:16:39Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
21,885,286
<p>My way is to use yappi (<a href="https://code.google.com/p/yappi/">https://code.google.com/p/yappi/</a>). It's especially useful combined with an RPC server where (even just for debugging) you register method to start, stop and print profiling information, e.g. in this way: </p> <pre><code>@staticmethod def startPr...
6
2014-02-19T15:38:58Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
28,660,109
<p>Also worth mentioning is the GUI cProfile dump viewer <a href="http://www.vrplumber.com/programming/runsnakerun/">RunSnakeRun</a>. It allows you to sort and select, thereby zooming in on the relevant parts of the program. The sizes of the rectangles in the picture is proportional to the time taken. If you mouse o...
18
2015-02-22T16:18:05Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
28,808,860
<h1>pprofile</h1> <p><code>line_profiler</code> (already presented here) also inspired <a href="https://github.com/vpelletier/pprofile" rel="nofollow"><code>pprofile</code></a>, which is described as:</p> <blockquote> <p>Line-granularity, thread-aware deterministic and statistic pure-python profiler</p> </blockq...
11
2015-03-02T11:36:47Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
29,183,471
<p>To add on to <a href="http://stackoverflow.com/a/582337/1070617">http://stackoverflow.com/a/582337/1070617</a>,</p> <p>I wrote this module that allows you to use cProfile and view its output easily. More here: <a href="https://github.com/ymichael/cprofilev" rel="nofollow">https://github.com/ymichael/cprofilev</a></...
3
2015-03-21T13:50:12Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
29,344,687
<p>cProfile is great for quick profiling but most of the time it was ending for me with the errors. Function runctx solves this problem by initializing correctly the environment and variables, hope it can be useful for someone:</p> <pre><code>import cProfile cProfile.runctx('foo()', None, locals()) </code></pre>
7
2015-03-30T11:11:13Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
29,931,325
<p>A new tool to handle profiling in Python is PyVmMonitor: <a href="http://www.pyvmmonitor.com/" rel="nofollow">http://www.pyvmmonitor.com/</a></p> <p>It has some unique features such as</p> <ul> <li>Attach profiler to a running (CPython) program</li> <li>On demand profiling with Yappi integration</li> <li>Profile o...
2
2015-04-28T22:50:08Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
32,139,774
<p>There's a lot of great answers but they either use command line or some external program for profiling and/or sorting the results.</p> <p>I really missed some way I could use in my IDE (eclipse-PyDev) without touching the command line or installing anything. So here it is.</p> <h1>Profiling without command line</h...
7
2015-08-21T11:59:43Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
35,351,674
<p>There's also a statistical profiler called <a href="https://pypi.python.org/pypi/statprof/" rel="nofollow"><code>statprof</code></a>. It's a sampling profiler, so it adds minimal overhead to your code and gives line-based (not just function-based) timings. It's more suited to soft real-time applications like games, ...
1
2016-02-11T22:50:49Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
37,157,132
<p>I think that <a href="https://docs.python.org/2/library/profile.html" rel="nofollow"><code>cProfile</code></a> is great for profiling, while <a href="https://kcachegrind.github.io/html/Home.html" rel="nofollow"><code>kcachegrind</code></a> is great for visualizing the results. The <a href="https://pypi.python.org/py...
4
2016-05-11T08:32:50Z
[ "python", "performance", "profiling", "time-complexity" ]
How can you profile a Python script?
582,336
<p>Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to <code>__main__</code>.</p> <p>What is a good way to profile how long a python program takes t...
702
2009-02-24T16:01:26Z
37,431,235
<p>I ran into a handy tool called <a href="https://jiffyclub.github.io/snakeviz/" rel="nofollow">SnakeViz</a> when researching this topic. SnakeViz is a web-based profiling visualization tool. It is very easy to install and use. The usual way I use it is to generate a stat file with <code>%prun</code> and then do analy...
3
2016-05-25T08:06:13Z
[ "python", "performance", "profiling", "time-complexity" ]
py2exe + sqlalchemy + sqlite problem
582,449
<p>I am playing around with getting some basic stuff to work in Python before i go into full speed dev mode. Here are the specifics:</p> <pre><code>Python 2.5.4 PyQt4 4.4.3 SqlAlchemy 0.5.2 py2exe 0.6.9 setuptools 0.6c9 pysqlite 2.5.1 </code></pre> <p>setup.py:</p> <pre><code>from distutils.core import setup import ...
19
2009-02-24T16:28:11Z
582,520
<p>you need to include the sqlalchemy.databases.sqlite package</p> <pre><code>setup( windows=[{"script" : "main.py"}], options={"py2exe" : { "includes": ["sip", "PyQt4.QtSql"], "packages": ["sqlalchemy.databases.sqlite"] }}) </code></pre>
29
2009-02-24T16:49:55Z
[ "python", "sqlite", "sqlalchemy", "py2exe" ]
py2exe + sqlalchemy + sqlite problem
582,449
<p>I am playing around with getting some basic stuff to work in Python before i go into full speed dev mode. Here are the specifics:</p> <pre><code>Python 2.5.4 PyQt4 4.4.3 SqlAlchemy 0.5.2 py2exe 0.6.9 setuptools 0.6c9 pysqlite 2.5.1 </code></pre> <p>setup.py:</p> <pre><code>from distutils.core import setup import ...
19
2009-02-24T16:28:11Z
5,103,508
<p>you need change to sqlalchemy.dialects.sqlite package</p> <p>setup( windows=[{"script" : "main.py"}], options={"py2exe" : { "includes": ["sip", "PyQt4.QtSql"], "packages": ["sqlalchemy.dialects.sqlite"] }})</p>
5
2011-02-24T10:43:19Z
[ "python", "sqlite", "sqlalchemy", "py2exe" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
582,752
<p>Edit, since i misunderstood the question:</p> <p>Just put the <code>Helper</code> class in <code>__init__.py</code>. Thats perfectly pythonic. It just feels strange coming from languages like Java.</p>
4
2009-02-24T17:42:24Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
582,753
<p>You just put them in __init__.py.</p> <p>So with test/classes.py being:</p> <pre><code>class A(object): pass class B(object): pass </code></pre> <p>... and test/__init__.py being:</p> <pre><code>from classes import * class Helper(object): pass </code></pre> <p>You can import test and have access to A, B and He...
3
2009-02-24T17:42:59Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
582,777
<p>Yes, it is possible. You might also want to define <code>__all__</code> in <code>__init__.py</code> files. It's a list of modules that will be imported when you do </p> <pre><code>from lib import * </code></pre>
1
2009-02-24T17:49:28Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
583,065
<ol> <li><p>'<code>lib/</code>'s parent directory must be in <code>sys.path</code>. </p></li> <li><p>Your '<code>lib/__init__.py</code>' might look like this:</p> <pre><code>from . import settings # or just 'import settings' on old Python versions class Helper(object): pass </code></pre></li> </ol> <p>Then the ...
36
2009-02-24T18:52:35Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
4,595,071
<p>Maybe this could work:</p> <pre><code>import __init__ as lib </code></pre>
-2
2011-01-04T15:09:22Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
24,011,827
<p>Add something like this to <code>lib/__init__.py</code></p> <p><code>from .helperclass import Helper</code></p> <p>now you can import it directly:</p> <p><code>from lib import Helper</code></p>
0
2014-06-03T09:39:20Z
[ "python", "packages" ]
How to import classes defined in __init__.py
582,723
<p>I am trying to organize some modules for my own use. I have something like this:</p> <pre><code>lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py </code></pre> <p>In <code>lib/__init__.py</code>, I want to define some classes to be used if I impor...
46
2009-02-24T17:35:55Z
31,141,079
<p>If <code>lib/__init__.py</code> defines the Helper class then in settings.py you can use:</p> <pre><code>from . import Helper </code></pre> <p>This works because . is the current directory, and acts as a synonym for the lib package from the point of view of the settings module. Note that it is not necessary to exp...
0
2015-06-30T14:27:43Z
[ "python", "packages" ]
Run a linux system command as a superuser, using a python script
583,216
<p>I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:<pre><code>sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile</cod...
15
2009-02-24T19:32:21Z
583,236
<pre><code>import os os.popen("sudo -S /etc/init.d/postifx reload", 'w').write("yourpassword") </code></pre> <p>This of course is almost always not a good idea as the password is in plain text.</p>
1
2009-02-24T19:38:21Z
[ "python", "linux", "sysadmin", "sudo", "root" ]
Run a linux system command as a superuser, using a python script
583,216
<p>I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:<pre><code>sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile</cod...
15
2009-02-24T19:32:21Z
583,239
<p>You can either run your python script as root itself - then you won't need to add privilege to reload postfix.</p> <p>Or you can configure sudo to not need a password for <code>/etc/init.d/postfix</code>.</p> <p>sudo configuration (via visudo) allows NOPASSWD: to allow the command without a password. See <a href="...
18
2009-02-24T19:39:15Z
[ "python", "linux", "sysadmin", "sudo", "root" ]
Run a linux system command as a superuser, using a python script
583,216
<p>I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:<pre><code>sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile</cod...
15
2009-02-24T19:32:21Z
583,257
<pre><code>#include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; // gcc -o reload_postfix reload_postfix.c // chown root reload_postfix // chmod +s reload_postfix int main( int argc, char **argv ) { setuid( geteuid() ); system("/etc/init.d/postifx reload"); } </code></pre> <p>Wrap your command in setuid-ed pro...
4
2009-02-24T19:43:44Z
[ "python", "linux", "sysadmin", "sudo", "root" ]
Run a linux system command as a superuser, using a python script
583,216
<p>I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:<pre><code>sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile</cod...
15
2009-02-24T19:32:21Z
618,831
<p>To answer the error:"sudo: sorry, you must have a tty to run sudo", we have a setting called "Defaults requiretty" in sudoers file. I tried commenting it out and it worked :D.</p>
3
2009-03-06T13:20:55Z
[ "python", "linux", "sysadmin", "sudo", "root" ]
Run a linux system command as a superuser, using a python script
583,216
<p>I have got postfix installed on my machine and I am updating virtual_alias on the fly programmatically(using python)(on some action). Once I update the entry in the /etc/postfix/virtual_alias, I am running the command:<pre><code>sudo /usr/sbin/postmap /etc/postfix/virtual_alias 2>>/work/postfix_valias_errorfile</cod...
15
2009-02-24T19:32:21Z
14,925,876
<p>if you're gonna do this in python you should just do the following:</p> <p>write this command before the line that you call the shell command</p> <pre><code>os.setuid(os.geteuid()) </code></pre> <p>then, you call the shell command without "sudo" prefix</p>
0
2013-02-17T20:46:23Z
[ "python", "linux", "sysadmin", "sudo", "root" ]
What is the best way to print a table with delimiters in Python
583,557
<p>I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done:</p> <pre><code>&gt;&gt;&gt; tab = [['a', 1], ['b', 2]] &gt;&gt;&gt; for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip...
4
2009-02-24T20:56:53Z
583,607
<p>I don't think it's going to get much better than your second code snippet... maybe, if you really want,</p> <pre><code>print "\n".join("\t".join(str(col) for col in row) for row in tab) </code></pre>
4
2009-02-24T21:05:20Z
[ "python", "coding-style" ]
What is the best way to print a table with delimiters in Python
583,557
<p>I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done:</p> <pre><code>&gt;&gt;&gt; tab = [['a', 1], ['b', 2]] &gt;&gt;&gt; for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip...
4
2009-02-24T20:56:53Z
583,757
<p>Your shorter solution would work well as something quick and dirty. But if you need to handle large amounts of data, it'd be better to use <code>csv</code> module:</p> <pre><code>import sys, csv writer = csv.writer(sys.stdout, delimiter="\t") writer.writerows(data) </code></pre> <p>The benefit of this solution is...
17
2009-02-24T21:47:41Z
[ "python", "coding-style" ]
What is the best way to print a table with delimiters in Python
583,557
<p>I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done:</p> <pre><code>&gt;&gt;&gt; tab = [['a', 1], ['b', 2]] &gt;&gt;&gt; for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip...
4
2009-02-24T20:56:53Z
583,764
<pre><code>import sys import csv writer = csv.writer(sys.stdout, dialect=csv.excel_tab) tab = [['a', 1], ['b', 2]] writer.writerows(tab) </code></pre>
2
2009-02-24T21:49:38Z
[ "python", "coding-style" ]
What is the best way to print a table with delimiters in Python
583,557
<p>I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done:</p> <pre><code>&gt;&gt;&gt; tab = [['a', 1], ['b', 2]] &gt;&gt;&gt; for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip...
4
2009-02-24T20:56:53Z
583,880
<p>Please do not use concatanation because it creates a new string every time. cStringIO.StringIO will do this kind of job much more efficiently.</p>
0
2009-02-24T22:20:30Z
[ "python", "coding-style" ]
What is the best way to print a table with delimiters in Python
583,557
<p>I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done:</p> <pre><code>&gt;&gt;&gt; tab = [['a', 1], ['b', 2]] &gt;&gt;&gt; for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip...
4
2009-02-24T20:56:53Z
584,602
<p>It depends on <em>why</em> you want to output like that, but if you just want to visually reference the data you might want to try the <a href="http://docs.python.org/library/pprint.html" rel="nofollow">pprint</a> module.</p> <pre><code>&gt;&gt;&gt; import pprint &gt;&gt;&gt; for item in tab: ... pprint.pprint(...
0
2009-02-25T03:30:35Z
[ "python", "coding-style" ]
Django development server shutdown error
583,740
<p>Hey all, Whenever i shut down my development server (./manage.py runserver) with CTRL+c i get following message:</p> <pre><code>[24/Feb/2009 22:05:23] "GET /home/ HTTP/1.1" 200 1571 [24/Feb/2009 22:05:24] "GET /contact HTTP/1.1" 301 0 [24/Feb/2009 22:05:24] "GET /contact/ HTTP/1.1" 200 2377 ^C Error in atexit._run_...
3
2009-02-24T21:44:19Z
586,081
<p>It appears that you are using the Mac's default python install. I know this has been reputed to have odd issues from time to time. I would recommend install MacPython and installing Django into that python instance. </p>
2
2009-02-25T13:59:20Z
[ "python", "django" ]
Is it possible to generate and return a ZIP file with App Engine?
583,791
<p>I have a small project that would be perfect for Google App Engine. Implementing it hinges on the ability to generate a ZIP file and return it.</p> <p>Due to the distributed nature of App Engine, from what I can tell, the ZIP file couldn't be created "in-memory" in the traditional sense. It would basically have t...
19
2009-02-24T21:56:09Z
583,806
<p>From <a href="http://code.google.com/appengine/docs/whatisgoogleappengine.html" rel="nofollow">What is Google App Engine</a>:</p> <blockquote> <p>You can upload other third-party libraries with your application, as long as they are implemented in pure Python and do not require any unsupported standard lib...
2
2009-02-24T22:02:14Z
[ "python", "google-app-engine", "zip", "in-memory" ]
Is it possible to generate and return a ZIP file with App Engine?
583,791
<p>I have a small project that would be perfect for Google App Engine. Implementing it hinges on the ability to generate a ZIP file and return it.</p> <p>Due to the distributed nature of App Engine, from what I can tell, the ZIP file couldn't be created "in-memory" in the traditional sense. It would basically have t...
19
2009-02-24T21:56:09Z
583,819
<p><a href="http://docs.python.org/library/zipfile.html">zipfile</a> is available at appengine and reworked <a href="http://www.tareandshare.com/2008/09/28/Zip-Google-App-Engine-GAE/">example</a> follows:</p> <pre><code>from contextlib import closing from zipfile import ZipFile, ZIP_DEFLATED from google.appengine.ext...
32
2009-02-24T22:06:10Z
[ "python", "google-app-engine", "zip", "in-memory" ]
Is it possible to generate and return a ZIP file with App Engine?
583,791
<p>I have a small project that would be perfect for Google App Engine. Implementing it hinges on the ability to generate a ZIP file and return it.</p> <p>Due to the distributed nature of App Engine, from what I can tell, the ZIP file couldn't be created "in-memory" in the traditional sense. It would basically have t...
19
2009-02-24T21:56:09Z
2,386,804
<pre><code>import zipfile import StringIO text = u"ABCDEFGHIJKLMNOPQRSTUVWXYVabcdefghijklmnopqqstuvweyxáéöüï东 廣 広 广 國 国 国 界" zipstream=StringIO.StringIO() file = zipfile.ZipFile(file=zipstream,compression=zipfile.ZIP_DEFLATED,mode="w") file.writestr("data.txt.zip",text.encode("utf-8")) file.close...
9
2010-03-05T12:54:24Z
[ "python", "google-app-engine", "zip", "in-memory" ]
How does one enable authentication across a Django site, and transparently preserving any POST or GET data?
583,857
<p>Suppose someone is editing a HTML form, and their session times out, how can one have Django re-authenticate that individual without losing the content the user had entered into the form?</p> <p>The snippet <a href="http://www.djangosnippets.org/snippets/136/" rel="nofollow">Django Snippets: Require login across en...
4
2009-02-24T22:14:28Z
583,923
<p>Add <code>onsubmit</code> handler to all your forms that would check session via JS and prompt use to login before proceeding. This way form submit would not really happen before user is logged in again.</p> <p>And make sure you verify that logged in user stays the same across sessions.</p>
2
2009-02-24T22:33:18Z
[ "python", "django", "authentication", "wsgi", "middleware" ]
How does one enable authentication across a Django site, and transparently preserving any POST or GET data?
583,857
<p>Suppose someone is editing a HTML form, and their session times out, how can one have Django re-authenticate that individual without losing the content the user had entered into the form?</p> <p>The snippet <a href="http://www.djangosnippets.org/snippets/136/" rel="nofollow">Django Snippets: Require login across en...
4
2009-02-24T22:14:28Z
584,135
<p>It is not exactly Django-specific but HTTP (The Stateless) specific... In the case system ends in issuing Redirect while handling POST (switching to GET from original POST) and risking loosing data one should store the data somewhere (db, memcached, etc.) and make the key under which they are stored be carried throu...
2
2009-02-24T23:35:57Z
[ "python", "django", "authentication", "wsgi", "middleware" ]
How does one enable authentication across a Django site, and transparently preserving any POST or GET data?
583,857
<p>Suppose someone is editing a HTML form, and their session times out, how can one have Django re-authenticate that individual without losing the content the user had entered into the form?</p> <p>The snippet <a href="http://www.djangosnippets.org/snippets/136/" rel="nofollow">Django Snippets: Require login across en...
4
2009-02-24T22:14:28Z
584,499
<p>I have two suggestions.</p> <h2>Redirect/Middleware</h2> <p>Since you're already using middleware to handle the login requirement, you could modify this middleware. Or possibly, create another middleware class that is called after the login middleware. These ideas are intertwined so it may make more sense to mod...
3
2009-02-25T02:23:22Z
[ "python", "django", "authentication", "wsgi", "middleware" ]
How does one enable authentication across a Django site, and transparently preserving any POST or GET data?
583,857
<p>Suppose someone is editing a HTML form, and their session times out, how can one have Django re-authenticate that individual without losing the content the user had entered into the form?</p> <p>The snippet <a href="http://www.djangosnippets.org/snippets/136/" rel="nofollow">Django Snippets: Require login across en...
4
2009-02-24T22:14:28Z
585,515
<p>I don't like sessions in general, though I suppose with an authenticated site you are already using them so maybe the answers above fit into your approach.</p> <p>Without sessions, I'd do something similar to Daniels answer, i.e. catch the original POST/GET in the middleware, but I'd change the redirect to include ...
0
2009-02-25T10:41:24Z
[ "python", "django", "authentication", "wsgi", "middleware" ]
PyGTK widget opacity
583,906
<p>Is there any way to set a widget's opacity in PyGTK?<br /> I know there's a function for windows:</p> <pre><code>gtk.Window.set_opacity(0.85) </code></pre> <p>but there seems to be no equivalent for arbitrary widgets.<br /> Anyone have any ideas?</p> <p>Thanks in advance for your help.</p>
0
2009-02-24T22:29:50Z
583,965
<p><a href="http://www.pygtk.org/docs/pygtk/class-gdkwindow.html#method-gdkwindow--set-opacity" rel="nofollow">From pygtk reference</a>:</p> <blockquote> <p>For setting up per-pixel alpha, see <code>gtk.gdk.Screen.get_rgba_colormap()</code>. For making non-toplevel windows translucent, see <code>gtk.gdk.Window.set_c...
3
2009-02-24T22:44:52Z
[ "python", "user-interface", "gtk", "pygtk" ]
PyGTK widget opacity
583,906
<p>Is there any way to set a widget's opacity in PyGTK?<br /> I know there's a function for windows:</p> <pre><code>gtk.Window.set_opacity(0.85) </code></pre> <p>but there seems to be no equivalent for arbitrary widgets.<br /> Anyone have any ideas?</p> <p>Thanks in advance for your help.</p>
0
2009-02-24T22:29:50Z
583,984
<p>It might also be worth looking into <a href="http://www.k-3d.org/gtkglext/Main%5FPage" rel="nofollow">pygtkglext</a> for fancier widget stuff.</p>
0
2009-02-24T22:52:24Z
[ "python", "user-interface", "gtk", "pygtk" ]
How would I make the output of this for loop into a string, into a variable?
583,986
<p>In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.</p> <p>How would I take the output of the for loop and make it a string so that I can load it into a variable?</p> <pre><code>x = ([]) while True: ...
0
2009-02-24T22:52:33Z
584,002
<p>In python, strings support a <code>join</code> method (conceptually the opposite of <code>split</code>) that allows you to join elements of a list (technically, of an iterable) together using the string. One very common use case is <code>', '.join(&lt;list&gt;)</code> to copy the elements of the list into a comma s...
2
2009-02-24T22:57:15Z
[ "python" ]
How would I make the output of this for loop into a string, into a variable?
583,986
<p>In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.</p> <p>How would I take the output of the for loop and make it a string so that I can load it into a variable?</p> <pre><code>x = ([]) while True: ...
0
2009-02-24T22:52:33Z
584,010
<pre><code>s = "\n".join(['&lt;li&gt;' + i + '&lt;/li&gt;' for i in x]) </code></pre>
2
2009-02-24T22:58:37Z
[ "python" ]
How would I make the output of this for loop into a string, into a variable?
583,986
<p>In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.</p> <p>How would I take the output of the for loop and make it a string so that I can load it into a variable?</p> <pre><code>x = ([]) while True: ...
0
2009-02-24T22:52:33Z
584,016
<p>Replace your for loop at the bottom with the following:</p> <pre><code>list_output="" for aLine in x: list_output += '&lt;li&gt;'+aLine+'&lt;/li&gt;\n' </code></pre> <p>Note also that since x is a list, Python lets you iterate through the elements of the list instead of having to iterate on an index variable t...
0
2009-02-24T22:59:51Z
[ "python" ]
How would I make the output of this for loop into a string, into a variable?
583,986
<p>In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.</p> <p>How would I take the output of the for loop and make it a string so that I can load it into a variable?</p> <pre><code>x = ([]) while True: ...
0
2009-02-24T22:52:33Z
584,037
<pre><code>list_output = "&lt;li&gt;%s&lt;/li&gt;\n" * len(x) % tuple(x) </code></pre>
1
2009-02-24T23:04:17Z
[ "python" ]
How would I make the output of this for loop into a string, into a variable?
583,986
<p>In this loop, I'm trying to take user input and continually put it in a list till they write "stop". When the loop is broken, the for loop prints out all of the li's.</p> <p>How would I take the output of the for loop and make it a string so that I can load it into a variable?</p> <pre><code>x = ([]) while True: ...
0
2009-02-24T22:52:33Z
584,070
<p>I hate to be the person to answer a different question, but hand-coded HTML generation makes me feel ill. Even if you're doing nothing more than this super-simple list generation, I'd strongly recommend looking at a templating language like <a href="http://genshi.edgewall.org/" rel="nofollow">Genshi</a>.</p> <p>A G...
2
2009-02-24T23:12:51Z
[ "python" ]
tkinter - set geometry without showing window
584,127
<p>I'm trying to line up some label and canvas widgets. To do so I need to know how wide my label boxes are. I'd like my widget to auto-adjust if the user changes the system font size, so I don't want to hard code 12 pixels per character. If I measure the label widget it's always 1 pixel wide. Until I call .update(...
2
2009-02-24T23:32:56Z
585,800
<p>Withdraw the window before calling update. The command you want is wm_withdraw</p> <pre><code>root = Tk() root.wm_withdraw() &lt;your code here&gt; root.wm_deiconify() </code></pre> <p>However, if your real problem is lining up widgets you usually don't need to know the size of widgets. Use the grid geometry manag...
1
2009-02-25T12:32:15Z
[ "python", "tkinter" ]
Problems on select module on Python 2.5
584,575
<p>I have an application in Python 2.5 that listens to a beanstalk queue. It works fine on all machines I tested so far, except from my newly acquired MacBook Pro. </p> <p>On that computer, when I try to run it I get this error:</p> <pre><code>Traceback (most recent call last): File "jobs.py", line 181, in &lt;modu...
3
2009-02-25T03:12:49Z
584,585
<p><a href="http://docs.python.org/library/select.html?highlight=select#select.poll" rel="nofollow"><strong>select.poll()</strong></a></p> <p>(<em>Not supported by all operating systems.</em>) Returns a polling object, which supports registering and unregistering file descriptors, and then polling them for I/O events;...
0
2009-02-25T03:16:17Z
[ "python", "osx", "beanstalkd" ]
Problems on select module on Python 2.5
584,575
<p>I have an application in Python 2.5 that listens to a beanstalk queue. It works fine on all machines I tested so far, except from my newly acquired MacBook Pro. </p> <p>On that computer, when I try to run it I get this error:</p> <pre><code>Traceback (most recent call last): File "jobs.py", line 181, in &lt;modu...
3
2009-02-25T03:12:49Z
584,684
<p>I think your answer is here</p> <p><a href="http://atomized.org/2008/12/python-on-os-x-leopard-lacks-selectpoll/" rel="nofollow">http://atomized.org/2008/12/python-on-os-x-leopard-lacks-selectpoll/</a></p>
2
2009-02-25T04:07:01Z
[ "python", "osx", "beanstalkd" ]
Problems on select module on Python 2.5
584,575
<p>I have an application in Python 2.5 that listens to a beanstalk queue. It works fine on all machines I tested so far, except from my newly acquired MacBook Pro. </p> <p>On that computer, when I try to run it I get this error:</p> <pre><code>Traceback (most recent call last): File "jobs.py", line 181, in &lt;modu...
3
2009-02-25T03:12:49Z
613,591
<p>use the <a href="http://www.macports.org/" rel="nofollow">MacPorts</a> version of <code>python</code> on your MBP. </p> <p>Mac OS X supports this. Apple stock Leopard <code>python 2.5.1</code> does not.</p> <p>you will want to download and install MacPorts if you have not already. FYI, I find <a href="http://por...
0
2009-03-05T04:41:29Z
[ "python", "osx", "beanstalkd" ]
Problems on select module on Python 2.5
584,575
<p>I have an application in Python 2.5 that listens to a beanstalk queue. It works fine on all machines I tested so far, except from my newly acquired MacBook Pro. </p> <p>On that computer, when I try to run it I get this error:</p> <pre><code>Traceback (most recent call last): File "jobs.py", line 181, in &lt;modu...
3
2009-02-25T03:12:49Z
5,124,744
<p>According to <a href="http://trac.macports.org/ticket/18376">this macports ticket</a> Apple's implementation of poll() is straight up broken. Apple worked around this by disabling poll() in Python and macports now disables poll in their Pythons as well. I think this means you need to look into Python's select.keve...
6
2011-02-26T02:56:02Z
[ "python", "osx", "beanstalkd" ]
Determine if a listing is a directory or file in Python over FTP
584,865
<p>Python has a standard library module <code>ftplib</code> to run FTP communications. It has two means of getting a listing of directory contents. One, <code>FTP.nlst()</code>, will return a list of the contents of a directory given a directory name as an argument. (It will return the name of a file if given a file na...
5
2009-02-25T05:54:36Z
585,232
<p>Unfortunately FTP doesn't have a command to list just folders so parsing the results you get from ftp.dir() would be 'best'.</p> <p>A simple app assuming a standard result from ls (not a windows ftp)</p> <pre><code>from ftplib import FTP ftp = FTP(host, user, passwd) for r in ftp.dir(): if r.upper().startswit...
10
2009-02-25T09:04:16Z
[ "python", "ftp" ]
Determine if a listing is a directory or file in Python over FTP
584,865
<p>Python has a standard library module <code>ftplib</code> to run FTP communications. It has two means of getting a listing of directory contents. One, <code>FTP.nlst()</code>, will return a list of the contents of a directory given a directory name as an argument. (It will return the name of a file if given a file na...
5
2009-02-25T05:54:36Z
3,114,604
<p>If the FTP server supports the <code>MLSD</code> command, then please check <a href="http://stackoverflow.com/questions/2867217/how-to-delete-files-with-a-python-script-from-a-ftp-server-which-are-older-than-7/3114477#3114477">that</a> answer for a couple of useful classes (<code>FTPDirectory</code> and <code>FTPTre...
1
2010-06-24T23:19:03Z
[ "python", "ftp" ]
Determine if a listing is a directory or file in Python over FTP
584,865
<p>Python has a standard library module <code>ftplib</code> to run FTP communications. It has two means of getting a listing of directory contents. One, <code>FTP.nlst()</code>, will return a list of the contents of a directory given a directory name as an argument. (It will return the name of a file if given a file na...
5
2009-02-25T05:54:36Z
14,222,775
<p>Another way is to assume everything is a directory and try and change into it. If this succeeds it is a directory, but if this throws an ftplib.error_perm it is probably a file. You can catch then catch the exception. Sure, this isn't really the safest, but neither is parsing the crazy string for leading 'd's.</p...
1
2013-01-08T19:41:06Z
[ "python", "ftp" ]
Database design of survey query system
585,006
<p>I am working on a so-called Behavioral Risk Factor Surveillance System (BRFSS), a web query system dealing with questionnaires coming every year. </p> <p>I had hard time in coming up with a suitable database design for it. Here is the problem: Each questionnaire contains about 80 questions, with demographic info, e...
0
2009-02-25T07:04:33Z
585,009
<p>Have you checked <a href="http://www.databaseanswers.org/data%5Fmodels/index.htm" rel="nofollow">DatabaseAnswers</a> to see if there is a schema you could use as a starting point?</p>
6
2009-02-25T07:06:41Z
[ "python", "database", "django", "design", "sqlite" ]
Database design of survey query system
585,006
<p>I am working on a so-called Behavioral Risk Factor Surveillance System (BRFSS), a web query system dealing with questionnaires coming every year. </p> <p>I had hard time in coming up with a suitable database design for it. Here is the problem: Each questionnaire contains about 80 questions, with demographic info, e...
0
2009-02-25T07:04:33Z
585,050
<p>Sounds like a case for a star schema.</p> <p>You would have a (huge) fact table like this:</p> <p>question_id, survey_id, age_group_id, health_classifier_id, is_smoking ... , answer_value</p> <p>and denormalised dimension tables:</p> <p>age_group: group_name, min_age, max_age, age_group_id</p> <p>1.4 million ro...
1
2009-02-25T07:29:34Z
[ "python", "database", "django", "design", "sqlite" ]
Database design of survey query system
585,006
<p>I am working on a so-called Behavioral Risk Factor Surveillance System (BRFSS), a web query system dealing with questionnaires coming every year. </p> <p>I had hard time in coming up with a suitable database design for it. Here is the problem: Each questionnaire contains about 80 questions, with demographic info, e...
0
2009-02-25T07:04:33Z
585,051
<p>you need at least 3 tables:</p> <p>1) <code>Questions</code> which contains the text for each question, with autoincrement id key</p> <p>eg: (123, "What is the colour of your hair?")</p> <p>2) <code>Questionaires</code>, which map Q#'s onto questions.</p> <p>eg) question #10 on questionaire #3 maps on to quest...
1
2009-02-25T07:29:39Z
[ "python", "database", "django", "design", "sqlite" ]
Database design of survey query system
585,006
<p>I am working on a so-called Behavioral Risk Factor Surveillance System (BRFSS), a web query system dealing with questionnaires coming every year. </p> <p>I had hard time in coming up with a suitable database design for it. Here is the problem: Each questionnaire contains about 80 questions, with demographic info, e...
0
2009-02-25T07:04:33Z
586,449
<p>I've been also thinking after my post on stackoverflow. Here is how I can use the denormalized wide table (80+ columns) to support question changing every year and also aggregate cross tabulation. Please comment on. Thanks</p> <ol> <li><p>Create a new table for each year with the questions placed on columns e.g. </...
0
2009-02-25T15:26:02Z
[ "python", "database", "django", "design", "sqlite" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
585,565
<p>If you can do this in Python, I'd suggest starting by looking at the <a href="http://docs.python.org/library/ast.html#module-ast" rel="nofollow">ast</a> (Abstract Syntax Tree) module, and going from there.</p>
3
2009-02-25T10:54:54Z
[ "python" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
585,663
<p>Are you asking about the I18N utilities in Python?</p> <p><a href="http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules" rel="nofollow">http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules</a></p> <p>There's a utility called po-utils (formerly...
2
2009-02-25T11:41:28Z
[ "python" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
585,710
<p><a href="http://docs.python.org/library/gettext.html" rel="nofollow">Gettext</a> might help you. Put your strings in <code>_(</code><strong>...</strong><code>)</code> structures:</p> <pre><code>a = _('Test') b = a c = _('Another text') </code></pre> <p>Then run in the shell prompt:</p> <pre><code>pygettext test.p...
0
2009-02-25T12:00:29Z
[ "python" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
585,780
<p>You may also consider to parse your code with <a href="http://pygments.org/" rel="nofollow">pygments.</a></p> <p>I don't know the other solution, but it sure is very simple to use.</p>
2
2009-02-25T12:24:21Z
[ "python" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
585,884
<p>unwind's suggestion of using the ast module in 2.6 is a good one. (There's also the undocumented _ast module in 2.5.) Here's example code for that</p> <pre><code>code = """a = 'blah' b = '''multi line string''' c = u"spam" """ import ast root = ast.parse(code) class ShowStrings(ast.NodeVisitor): def visit_Str(s...
11
2009-02-25T12:54:41Z
[ "python" ]
Find all strings in python code files
585,529
<p>I would like to list all strings within my large python project.</p> <p>Imagine the different possibilities to create a string in python:</p> <pre><code>mystring = "hello world" mystring = ("hello " "world") mystring = "hello " \ "world" </code></pre> <p>I need a tool that outputs "filena...
6
2009-02-25T10:44:02Z
603,736
<p>Python's included <code>tokenize</code> module will also do the trick.</p> <pre><code>from __future__ import with_statement import sys import tokenize for filename in sys.argv[1:]: with open(filename) as f: for toktype, tokstr, (lineno, _), _, _ in tokenize.generate_tokens(f.readline): if t...
5
2009-03-02T19:58:12Z
[ "python" ]
About the optional argument in Canvas in PyS60
585,957
<p>In <a href="http://wiki.opensource.nokia.com/projects/PyS60" rel="nofollow">Python for Symbian60</a> blit() is defined as:</p> <p>blit(image [,target=(0,0), source=((0,0),image.size), mask=None, scale=0 ])</p> <p>In the optional parameter source what is the significance of image.size?</p>
0
2009-02-25T13:17:59Z
585,970
<p>My guess is that blit() will automatically use the result of <code>image.size</code> when you don't specify anything else (and thus blitting the whole image from (0,0) to (width,height)).</p> <p>If you want only a smaller part of the image copied, you can use the source parameter to define a different rectangle to ...
0
2009-02-25T13:22:22Z
[ "python", "nokia", "pys60" ]
About the optional argument in Canvas in PyS60
585,957
<p>In <a href="http://wiki.opensource.nokia.com/projects/PyS60" rel="nofollow">Python for Symbian60</a> blit() is defined as:</p> <p>blit(image [,target=(0,0), source=((0,0),image.size), mask=None, scale=0 ])</p> <p>In the optional parameter source what is the significance of image.size?</p>
0
2009-02-25T13:17:59Z
915,184
<p>Think that source=((0,0)) is the top left corner and image.size is the bottom right corner. You blit whatever is between those two points.</p> <p>Similar for target, btw.</p>
0
2009-05-27T11:18:58Z
[ "python", "nokia", "pys60" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
586,579
<p>I would handle the random column header mapping in your script once it's uploaded. It's hard to make a "catch all" that would handle whatever the users might enter. I would have it evolve as you go and slowly build a list of one-one relations based on what your user uploads.</p> <p>Or!</p> <p>Check the column head...
1
2009-02-25T15:50:13Z
[ "jquery", "python", "django", "django-models", "csv" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
586,582
<p>I'd check out Python's built-in csv module. Frankly a .replace() on your first row should cover your synonyms issue, and if you're using <a href="http://docs.python.org/library/csv.html#csv.DictReader" rel="nofollow">csv.DictReader</a> you should be able to deal with missing columns very easily:</p> <pre><code>my_d...
4
2009-02-25T15:50:32Z
[ "jquery", "python", "django", "django-models", "csv" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
586,601
<p>You should force the first row to be the headers, make the user match up their headers to your field names on the next page, and remember that mapping for their future dumps.</p> <p>Whenever I do CSV imports the data really came from an Excel spreadsheet. I've been able to save time by using <a href="http://sourcef...
3
2009-02-25T15:54:06Z
[ "jquery", "python", "django", "django-models", "csv" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
586,622
<p>Take a look at this project: <a href="http://code.google.com/p/django-batchimport/" rel="nofollow">django-batchimport</a></p> <p>It might be overkill for you, but it can still give you some good ideas on improving your own code.</p> <p>Edit: also, ignore that it is only using xlrd for importing Excel. The base co...
1
2009-02-25T15:59:24Z
[ "jquery", "python", "django", "django-models", "csv" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
586,694
<p>If you'll copy excel table into clipboard and then paste results into notepad, you'll notice that it's tab separated. I once used it to make bulk import from most of table editors by copy-pasting data from the editor into textarea on html page.</p> <p>You can use a background for textarea as a hint for number of co...
1
2009-02-25T16:15:08Z
[ "jquery", "python", "django", "django-models", "csv" ]
What are good ways to upload bulk .csv data into a webapp using Django/Python?
586,517
<p>I have a very basic CSV file upload module working to bulk upload my user's data into my site. I process the CSV file in the backend with a python script that runs on crontab and then email the user the results of the bulk upload. This process works ok operationally, but my issue is with the format of the csv file...
2
2009-02-25T15:39:46Z
588,212
<p>Look at <code>csv</code> module from stdlib. It contains presets for popualr CSV dialects like one produced by Excel.</p> <p>Reader class support field mapping and if file contains column header it coes not depend on column order. For more complex logic, like looking up several alternative names for a field, you'...
1
2009-02-25T22:29:30Z
[ "jquery", "python", "django", "django-models", "csv" ]
Python regular expression matching a multiline block of text
587,345
<p>I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)</p> <pre><code>some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines ...
42
2009-02-25T19:00:49Z
587,389
<p>find:</p> <pre><code>^&gt;([^\n\r]+)[\n\r]([A-Z\n\r]+) </code></pre> <p>\1 = some_varying_text </p> <p>\2 = lines of all CAPS</p> <p><strong>Edit (proof that this works):</strong></p> <pre><code>text = """&gt; some_Varying_TEXT DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF GATACAACATAGGATACA GGGGGAAAAAAAATTTTTTTTT CCCCA...
1
2009-02-25T19:11:53Z
[ "python", "regex", "multiline" ]
Python regular expression matching a multiline block of text
587,345
<p>I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)</p> <pre><code>some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines ...
42
2009-02-25T19:00:49Z
587,518
<p>This will work:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; rx_sequence=re.compile(r"^(.+?)\n\n((?:[A-Z]+\n)+)",re.MULTILINE) &gt;&gt;&gt; rx_blanks=re.compile(r"\W+") # to remove blanks and newlines &gt;&gt;&gt; text="""Some varying text1 ... ... AAABBBBBBCCCCCCDDDDDDD ... EEEEEEEFFFFFFFFGGGGGGG ... HHHHHHI...
10
2009-02-25T19:47:22Z
[ "python", "regex", "multiline" ]
Python regular expression matching a multiline block of text
587,345
<p>I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)</p> <pre><code>some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines ...
42
2009-02-25T19:00:49Z
587,620
<p>Try this:</p> <pre><code>re.compile(r"^(.+)\n((?:\n.+)+)", re.MULTILINE) </code></pre> <p>I think your biggest problem is that you're expecting the <code>^</code> and <code>$</code> anchors to match linefeeds, but they don't. In multiline mode, <code>^</code> matches the position immediately <em>following</em> a ...
56
2009-02-25T20:06:01Z
[ "python", "regex", "multiline" ]
Python regular expression matching a multiline block of text
587,345
<p>I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)</p> <pre><code>some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines ...
42
2009-02-25T19:00:49Z
587,819
<p>My preference.</p> <pre><code>lineIter= iter(aFile) for line in lineIter: if line.startswith( "&gt;" ): someVaryingText= line break assert len( lineIter.next().strip() ) == 0 acids= [] for line in lineIter: if len(line.strip()) == 0: break acids.append( line ) </code></pre> <p...
1
2009-02-25T20:58:28Z
[ "python", "regex", "multiline" ]
Python regular expression matching a multiline block of text
587,345
<p>I'm having a bit of trouble getting a Python regex to work when matching against text that spans multiple lines. The example text is ('\n' is a newline)</p> <pre><code>some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines ...
42
2009-02-25T19:00:49Z
587,825
<p>If each file only has one sequence of aminoacids, I wouldn't use regular expressions at all. Just something like this:</p> <pre><code>def read_amino_acid_sequence(path): with open(path) as sequence_file: title = sequence_file.readline() # read 1st line aminoacid_sequence = sequence_file.read() ...
3
2009-02-25T20:59:59Z
[ "python", "regex", "multiline" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,655
<p>Determine the length, convert it to an integer, increment it, then convert it back to a string with leading zeros so that it has the same length as before.</p>
1
2009-02-25T20:15:29Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,656
<pre><code>int('00000001') + 1 </code></pre> <p>if you want the leading zeroes back:</p> <pre><code>"%08g" % (int('000000001') + 1) </code></pre>
8
2009-02-25T20:15:43Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,689
<p>Presumably, you specifically mean an integer represented as a string with leading zeros?</p> <p>If that's the case, I'd do it thusly:</p> <pre><code>&gt;&gt;&gt; a '00000000000000099' &gt;&gt;&gt; l = len(a) &gt;&gt;&gt; b = int(a)+1 &gt;&gt;&gt; b 100 &gt;&gt;&gt; ("%0"+"%dd" % l) % b '00000000000000100' </code><...
2
2009-02-25T20:23:10Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,690
<p>"%%0%ii" % len(x) % (int(x)+1)</p> <p>-- MarkusQ</p> <p>P.S. For x = "0000034" it unfolds like so:</p> <pre><code>"%%0%ii" % len("0000034") % (int("0000034")+1) "%%0%ii" % 7 % (34+1) "%07i" % 35 "0000035" </code></pre>
8
2009-02-25T20:23:18Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,696
<p>Store your number as an integer. When you want to print it, add the leading zeros. This way you can easily do math without conversions, and it simplifies the thought process.</p>
1
2009-02-25T20:26:57Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
587,791
<p>Use the much overlooked str.zfill():</p> <pre><code>str(int(x) + 1).zfill(len(x)) </code></pre>
15
2009-02-25T20:50:12Z
[ "python" ]
How to increment a value with leading zeroes?
587,647
<p>What would be the best way to increment a value that contains leading zeroes? For example, I'd like to increment "00000001". However, it should be noted that the number of leading zeroes will not exceed 30. So there may be cases like "0000012", "00000000000000099", or "000000000000045".</p> <p>I can think of a c...
8
2009-02-25T20:13:22Z
21,325,089
<p>"my code is in c" <code>int a[6]={0,0,0,0,0,0},i=5,k,p; while(a[0]!=10) { do { for(p=0;p&lt;=i;p++) printf("%d",a[p]); printf("\n"); delay(100); a[i]++; }while(a[i]!=10); for(k=0;k&lt;=i;k++) if(a[i-k]==10) { a[i-(k+1)]++; ...
-1
2014-01-24T05:27:22Z
[ "python" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,067
<p>Generally, you can't. Three variables, three loops.</p> <p>But this is a special case, as <a href="http://stackoverflow.com/questions/588052/a-question-on-for-loops-in-python/588087#588087">nobody</a> pointed out. You can solve this problem with two loops.</p> <p>Also, there's no point in checking y, z and z, y.</...
8
2009-02-25T21:52:06Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,084
<p>Besides what has already been posted, people would expect three loops for three collections. Anything else may get very confusing and provide no added benefit.</p>
0
2009-02-25T21:55:15Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,087
<p>You would only need two loops - just check to see if <code>math.sqrt(x*x+y*y)</code> is an integer. If it is, you've discovered a pythagorean triple.</p> <p>I'm new to Python, so I don't know what <code>range(10000, 1000)</code> does - where does it start and stop? I ask because you can halve your runtime by having...
4
2009-02-25T21:55:26Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,101
<p>You can arrange your code in a single main loop like this:</p> <pre><code>MIN = 10000 MAX = 10010 a = [MIN, MIN, MIN] while True: print a for i in range(len(a)): a[i] = a[i] + 1 if a[i] &lt; MAX: break a[i] = MIN i += 1 else: break </code></pre> <p>In...
4
2009-02-25T21:58:16Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,154
<p>Not the most efficient (Python will build an array with a billion tuples), but this is a single loop:</p> <pre><code>for x, y, z in [(x, y, z) for x in range(10000, 11000) for y in range(10000, 11000) for z in range(10000, 11000)]: if x*x == y*y + z*z: print y, z, x print '-'*50 </code></pre> <...
1
2009-02-25T22:13:33Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,245
<p>Using <a href="http://docs.python.org/library/functions.html#xrange" rel="nofollow">xrange</a> instead of <code>range</code> should use less memory, especially if you want to try large ranges.</p>
2
2009-02-25T22:39:31Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,303
<p>This is the same as Can Berk Guder's answer, but done as a generator, just for fun. It's not really useful with the nested loops here, but it can often be a cleaner solution. Your function produces results; you worry later about how many to retrieve.</p> <pre><code>import math def triplets(limit): for x in r...
1
2009-02-25T22:58:04Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,336
<p>If you want to count to infinity ..</p> <p>Create a generator function that counts from zero and never stops, and use the for loop on it</p> <pre><code>def inf(): i = 0 while True: yield i i = i + 1 for i in inf(): print i # or do whatever you want! </code></pre> <p>I don't know if there's a...
0
2009-02-25T23:08:12Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,359
<p>Using the same algorithm (see the other answers for better approaches), you can use itertools.count to get a loop that runs forever.</p> <pre><code>import itertools for x in itertools.count(1): for y in xrange(1, x): for z in xrange(1, y): if x*x == y*y + z*z: print x, ...
1
2009-02-25T23:13:41Z
[ "python", "for-loop" ]
a question on for loops in python
588,052
<p>I want to calaculate pythagorean triplets(code below) and I want to calculate infinitly how do I do it without using the three for loops? Could I use a for loop in some way? thanks.</p> <pre><code>import math def main(): for x in range (10000, 1000): for y in range (10000, 1000): for z in range(1000...
3
2009-02-25T21:47:41Z
588,679
<p>Here's an efficient version, using iterators, that generates all such triples, in order. The trick here is to iterate up through the sets of (x,y) pairs that sum to N, for all N.</p> <pre> import math import itertools def all_int_pairs(): "generate all pairs of positive integers" for n in itertools.count...
2
2009-02-26T01:04:36Z
[ "python", "for-loop" ]