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
listlengths
1
5
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,349
<p>You could simply print the difference:</p> <pre><code>print tend - tstart </code></pre>
2
2009-04-19T23:16:09Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,354
<p>I am not a Python programmer, but <a href="http://pleac.sourceforge.net/pleac%5Fpython/datesandtimes.html" rel="nofollow">I do know how to use Google</a> and here's what I found: you use the "-" operator. To complete your code:</p> <pre><code>from datetime import datetime tstart = datetime.now() # code to speed t...
0
2009-04-19T23:17:18Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,356
<p>The following code should display the time detla...</p> <pre><code>from datetime import datetime tstart = datetime.now() # code to speed test tend = datetime.now() print tend - tstart </code></pre>
2
2009-04-19T23:17:51Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,357
<p>time.time() / datetime is good for quick use, but is not always 100% precise. For that reason, I like to use one of the std lib <a href="http://docs.python.org/library/profile.html" rel="nofollow">profilers</a> (especially hotshot) to find out what's what.</p>
2
2009-04-19T23:19:26Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,382
<p><a href="http://docs.python.org/3.2/library/datetime.html#timedelta-objects"><code>datetime.timedelta</code></a> is just the difference between two datetimes ... so it's like a period of time, in days / seconds / microseconds</p> <pre><code>&gt;&gt;&gt; a = datetime.datetime.now() &gt;&gt;&gt; b = datetime.datetime...
94
2009-04-19T23:31:01Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,402
<p>You could also use:</p> <pre><code>import time start = time.clock() do_something() end = time.clock() print "%.2gs" % (end-start) </code></pre> <p>Or you could use the <a href="http://docs.python.org/library/profile.html">python profilers</a>.</p>
18
2009-04-19T23:38:06Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,411
<p>You may want to look into the <a href="http://docs.python.org/library/profile.html" rel="nofollow">profile</a> modules. You'll get a better read out of where your slowdowns are, and much of your work will be full-on automated.</p>
2
2009-04-19T23:41:17Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
766,473
<p>You might want to use <a href="http://docs.python.org/library/timeit.html">the timeit module</a> instead.</p>
31
2009-04-20T00:18:10Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
12,617,362
<p>You could use timeit like this to test a script named module.py</p> <pre><code>$ python -mtimeit -s 'import module' </code></pre>
0
2012-09-27T08:41:52Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
23,700,340
<p>I know this is late, but I actually really like using:</p> <pre><code>start = time.time() ##### your timed code here ... ##### print "Process time: " + (time.time() - start) </code></pre> <p><code>time.time()</code> gives you seconds since the epoch. Because this is a standardized time in seconds, you can simply...
4
2014-05-16T16:32:34Z
[ "python", "datetime", "time", "time-measurement" ]
Python speed testing - Time Difference - milliseconds
766,335
<p>What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.</p> <p>So far I have this code:</p> <pre><code>from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = dat...
63
2009-04-19T23:08:27Z
24,274,452
<p>Since Python 2.7 there's the timedelta.total_seconds() method. So, to get the elapsed milliseconds:</p> <pre><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; a = datetime.datetime.now() &gt;&gt;&gt; b = datetime.datetime.now() &gt;&gt;&gt; delta = b - a &gt;&gt;&gt; print delta 0:00:05.077263 &gt;&gt;&gt; int(delta....
19
2014-06-17T22:38:54Z
[ "python", "datetime", "time", "time-measurement" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
766,377
<p>You seek the all-powerful '*?'</p> <p><a href="http://docs.python.org/2/howto/regex.html#greedy-versus-non-greedy">http://docs.python.org/2/howto/regex.html#greedy-versus-non-greedy</a></p>
59
2009-04-19T23:27:21Z
[ "python", "regex", "regex-greedy" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
766,379
<p>Would not <code>\\(.*?\\)</code> work ? That is the non-greedy syntax. </p>
9
2009-04-19T23:28:04Z
[ "python", "regex", "regex-greedy" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
766,384
<pre><code>&gt;&gt;&gt; x = "a (b) c (d) e" &gt;&gt;&gt; re.search(r"\(.*\)", x).group() '(b) c (d)' &gt;&gt;&gt; re.search(r"\(.*?\)", x).group() '(b)' </code></pre> <p><a href="http://docs.python.org/library/re.html#regular-expression-syntax">According to the docs</a>:</p> <blockquote> <p>The '<code>*</code>', '<...
41
2009-04-19T23:31:33Z
[ "python", "regex", "regex-greedy" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
766,439
<p>Do you want it to match "(b)"? Do as Zitrax and Paolo have suggested. Do you want it to match "b"? Do</p> <pre><code>&gt;&gt;&gt; x = "a (b) c (d) e" &gt;&gt;&gt; re.search(r"\((.*?)\)", x).group(1) 'b' </code></pre>
2
2009-04-19T23:54:55Z
[ "python", "regex", "regex-greedy" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
773,816
<p>Using an ungreedy match is a good start, but I'd also suggest that you reconsider any use of <code>.*</code> -- what about this?</p> <pre><code>groups = re.search(r"\([^)]*\)", x) </code></pre>
2
2009-04-21T18:01:15Z
[ "python", "regex", "regex-greedy" ]
Python non-greedy regexes
766,372
<p>How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"?</p> <p>I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possibl...
54
2009-04-19T23:24:03Z
773,844
<p>As the others have said using the ? modifier on the * quantifier will solve your immediate problem, but be careful, you are starting to stray into areas where regexes stop working and you need a parser instead. For instance, the string "(foo (bar)) baz" will cause you problems.</p>
5
2009-04-21T18:06:23Z
[ "python", "regex", "regex-greedy" ]
Python Collections.DefaultDict Sort + Output Top X Custom Class Object
766,546
<p>Problem: I need to output the TOP X Contributors determined by the amount of messages posted.</p> <p>Data: I have a collection of the messages posted. This is not a Database/SQL question by the sample query below just give an overview of the code.</p> <pre><code>tweetsSQL = db.GqlQuery("SELECT * FROM TweetModel OR...
0
2009-04-20T00:49:15Z
766,606
<p>I think your job would be a lot easier if you change the SQL query to something like: </p> <pre><code>SELECT top 100 userId FROM TweetModel GROUP BY userId ORDER BY count(*) </code></pre> <p>I wouldn't bother with the TweetModel class if you only need the data to solve the stated problem. </p>
0
2009-04-20T02:07:59Z
[ "python" ]
Python Collections.DefaultDict Sort + Output Top X Custom Class Object
766,546
<p>Problem: I need to output the TOP X Contributors determined by the amount of messages posted.</p> <p>Data: I have a collection of the messages posted. This is not a Database/SQL question by the sample query below just give an overview of the code.</p> <pre><code>tweetsSQL = db.GqlQuery("SELECT * FROM TweetModel OR...
0
2009-04-20T00:49:15Z
766,672
<p>Use heapq.nlargest() instead of sorted(), for efficiency; it's what it's for. I don't know the answer about the DB part of your question.</p>
1
2009-04-20T02:35:08Z
[ "python" ]
Python Collections.DefaultDict Sort + Output Top X Custom Class Object
766,546
<p>Problem: I need to output the TOP X Contributors determined by the amount of messages posted.</p> <p>Data: I have a collection of the messages posted. This is not a Database/SQL question by the sample query below just give an overview of the code.</p> <pre><code>tweetsSQL = db.GqlQuery("SELECT * FROM TweetModel OR...
0
2009-04-20T00:49:15Z
766,753
<p>Why not invert the dictionary, once you have constructed it, so that the keys are the message counts and the values are the users? Then you can sort the keys and easily get to the users.</p>
0
2009-04-20T03:16:49Z
[ "python" ]
mod_wsgi force reload modules
766,601
<p>Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load?</p> <p>While working on the code, it's very annoying to restart apache every time something is changed. The only option I've found so far is to put <code>modname = reload(modname)</code> below every import.. but that'...
5
2009-04-20T02:04:53Z
766,703
<p>The mod_wsgi <a href="http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode" rel="nofollow">documentation on code reloading</a> is your best bet for an answer.</p>
4
2009-04-20T02:50:17Z
[ "python", "module", "mod-wsgi", "reload" ]
mod_wsgi force reload modules
766,601
<p>Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load?</p> <p>While working on the code, it's very annoying to restart apache every time something is changed. The only option I've found so far is to put <code>modname = reload(modname)</code> below every import.. but that'...
5
2009-04-20T02:04:53Z
1,037,909
<p>The link:</p> <p><a href="http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode">http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode</a></p> <p>should be emphasised. It also should be emphaised that on UNIX systems daemon mode of mod_wsgi must be used and you must implement the code monitor described in ...
9
2009-06-24T11:59:02Z
[ "python", "module", "mod-wsgi", "reload" ]
mod_wsgi force reload modules
766,601
<p>Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load?</p> <p>While working on the code, it's very annoying to restart apache every time something is changed. The only option I've found so far is to put <code>modname = reload(modname)</code> below every import.. but that'...
5
2009-04-20T02:04:53Z
16,766,069
<p>The following solution is aimed at Linux users only, and has been tested to work under Ubuntu Server 12.04.1</p> <p>To run WSGI under daemon mode, you need to specify <code>WSGIProcessGroup</code> and <code>WSGIDaemonProcess</code> directives in your Apache configuration file, for example</p> <pre><code>WSGIProces...
4
2013-05-27T03:52:03Z
[ "python", "module", "mod-wsgi", "reload" ]
mod_wsgi force reload modules
766,601
<p>Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load?</p> <p>While working on the code, it's very annoying to restart apache every time something is changed. The only option I've found so far is to put <code>modname = reload(modname)</code> below every import.. but that'...
5
2009-04-20T02:04:53Z
39,094,975
<p>I know it's an old thread but this might help someone. To kill your process when any file in a certain directory is written to, you can use something like this:</p> <h1>monitor.py</h1> <pre><code>import os, sys, time, signal, threading, atexit import inotify.adapters def _monitor(path): i = inotify.adapters....
0
2016-08-23T07:31:25Z
[ "python", "module", "mod-wsgi", "reload" ]
Some internals of Django auth middleware
766,733
<p>In the django.contrib.auth middleware I see the code:</p> <pre><code>class AuthenticationMiddleware(object): def process_request(self, request): assert hasattr(request, 'session'), "requires session middleware" request.__class__.user = LazyUser() return None </code></pre> <p>Please avdi...
4
2009-04-20T03:02:36Z
766,744
<p>This is going to affect how <code>request</code>s are created. All such instances will have their <code>user</code> attribute as that particular <code>LazuUser</code> without the need to make that change after each individual <code>request</code> is instantiated.</p>
-1
2009-04-20T03:10:28Z
[ "python", "django" ]
Some internals of Django auth middleware
766,733
<p>In the django.contrib.auth middleware I see the code:</p> <pre><code>class AuthenticationMiddleware(object): def process_request(self, request): assert hasattr(request, 'session'), "requires session middleware" request.__class__.user = LazyUser() return None </code></pre> <p>Please avdi...
4
2009-04-20T03:02:36Z
767,299
<p><code>LazyUser</code> is descriptor-class. According to <a href="http://docs.python.org/reference/datamodel.html#invoking-descriptors" rel="nofollow">documentation</a> it can be only class attribute not instance one:</p> <blockquote> <p>For instance, <code>a.x</code> has a lookup chain starting with <code>a.__dic...
9
2009-04-20T07:52:51Z
[ "python", "django" ]
Programattically taking screenshots in windows without the application noticing
767,212
<p><b>Duplicate of: <a href="http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program">http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program</a></b></p> <p>There are various ways to take screenshots of a running application in Windows. However, I h...
0
2009-04-20T07:11:05Z
767,247
<p>Do you have a particular anti-screenshot program in mind? Ultimately, you're right, running the app in a VM will trump any 'protection' it has, but the method depends on which OS/VM you're using, and it's not worth the overhead until it's needed.</p> <p>I'd just use this: <a href="http://stackoverflow.com/question...
2
2009-04-20T07:31:07Z
[ "python", "windows", "events", "operating-system", "screenshot" ]
Programattically taking screenshots in windows without the application noticing
767,212
<p><b>Duplicate of: <a href="http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program">http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program</a></b></p> <p>There are various ways to take screenshots of a running application in Windows. However, I h...
0
2009-04-20T07:11:05Z
767,339
<p>Find out how the pros do it:</p> <ul> <li><a href="http://browsershots.org/" rel="nofollow">http://browsershots.org/</a></li> <li><a href="https://sourceforge.net/projects/browsershots/" rel="nofollow">https://sourceforge.net/projects/browsershots/</a></li> <li><a href="http://v02.browsershots.org/trac/wiki" rel="n...
1
2009-04-20T08:10:08Z
[ "python", "windows", "events", "operating-system", "screenshot" ]
Programattically taking screenshots in windows without the application noticing
767,212
<p><b>Duplicate of: <a href="http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program">http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program</a></b></p> <p>There are various ways to take screenshots of a running application in Windows. However, I h...
0
2009-04-20T07:11:05Z
767,985
<p><em>> I hear that an application can be tailored such that it can notice when a screenshot is being taken of it</em></p> <p>Complete nonsense. Don't repeat what kids say... Read MSDN about screenshots.</p>
4
2009-04-20T12:06:51Z
[ "python", "windows", "events", "operating-system", "screenshot" ]
Programattically taking screenshots in windows without the application noticing
767,212
<p><b>Duplicate of: <a href="http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program">http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program</a></b></p> <p>There are various ways to take screenshots of a running application in Windows. However, I h...
0
2009-04-20T07:11:05Z
767,992
<p>There will certainly be no protection against a screenshot taken with a digital camera.</p>
5
2009-04-20T12:08:21Z
[ "python", "windows", "events", "operating-system", "screenshot" ]
Programattically taking screenshots in windows without the application noticing
767,212
<p><b>Duplicate of: <a href="http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program">http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program</a></b></p> <p>There are various ways to take screenshots of a running application in Windows. However, I h...
0
2009-04-20T07:11:05Z
775,047
<p>One could use Remote Desktop or <a href="http://www.uvnc.com/features/driver.html" rel="nofollow">the (low-level) VNC Mirror Driver</a> and take the screenshot on an another computer.</p>
0
2009-04-21T23:06:37Z
[ "python", "windows", "events", "operating-system", "screenshot" ]
Attributes not available when overwriting __init__?
767,241
<p>I'm trying to overwrite a <code>__init__</code> method, but when I call the super method the attributes created in that method are not available. I can see that it's not an inheritance problem since <code>class B</code> still has the attributes available.</p> <p>I think the code sample will explain it better :-)</p...
2
2009-04-20T07:28:59Z
767,268
<p>Your call to the superclass needs to use its own type</p> <pre><code>super(D, self).__init__(*args,**kwargs) </code></pre> <p>rather than </p> <pre><code>super(A... </code></pre> <p>I believe calling <code>super(A, self).__init__</code> will call the superclass of <code>A</code>, which is <code>object</code>. Ra...
5
2009-04-20T07:38:44Z
[ "python" ]
Attributes not available when overwriting __init__?
767,241
<p>I'm trying to overwrite a <code>__init__</code> method, but when I call the super method the attributes created in that method are not available. I can see that it's not an inheritance problem since <code>class B</code> still has the attributes available.</p> <p>I think the code sample will explain it better :-)</p...
2
2009-04-20T07:28:59Z
767,272
<p>You're using super() incorrectly. In your "C" class the second line of the <strong>init</strong>() method should pass C as the first argument like so...</p> <pre><code>super(C, self).__init__(*args, **kwargs) </code></pre> <p>And really you shouldn't even need to use super here. You could just call </p> <pre><cod...
3
2009-04-20T07:40:05Z
[ "python" ]
nice html reports for pyunit
767,377
<p>Do you know a tool for creating nice html reports for pyunit?</p>
8
2009-04-20T08:24:39Z
767,442
<p>I suggest the following:</p> <ol> <li>Run your tests using <a href="http://somethingaboutorange.com/mrl/projects/nose/" rel="nofollow">nose</a></li> <li>Create a nose plugin that outputs results as HTML. The nose example code has a simple HTML output plugin (<a href="https://raw.github.com/nose-devs/nose/master/exa...
9
2009-04-20T08:44:49Z
[ "python", "html", "pyunit" ]
nice html reports for pyunit
767,377
<p>Do you know a tool for creating nice html reports for pyunit?</p>
8
2009-04-20T08:24:39Z
9,397,138
<p>there has a simple HTMLTestRunner developed for pyunit, here is <a href="http://tungwaiyip.info/software/HTMLTestRunner.html" rel="nofollow">link</a></p> <p>The shortage of it is there are no output in console, because sys.stdout and sys.stderr was captured by HTMLTestRunner. Except above, others works fine to me....
1
2012-02-22T14:54:18Z
[ "python", "html", "pyunit" ]
Blocks of code in python
767,519
<p>Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?</p> <p>What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs?</p> <p>I have so far understood the <code>lambda</cod...
8
2009-04-20T09:14:23Z
767,529
<p>functions are the first-class members in python</p> <pre><code>def add(x, y): return x + y a = add # bind b = a(34, 1) # call </code></pre> <p>so you can pass functions around all you want. you can do the same with any callable object in python.</p>
9
2009-04-20T09:21:31Z
[ "python", "ruby", "lambda", "codeblocks" ]
Blocks of code in python
767,519
<p>Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?</p> <p>What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs?</p> <p>I have so far understood the <code>lambda</cod...
8
2009-04-20T09:14:23Z
767,576
<p>See previous questions, for example <a href="http://stackoverflow.com/questions/144661/python-vs-ruby-for-metaprogramming">python-vs-ruby-for-metaprogramming</a>, or general <a href="http://en.wikipedia.org/wiki/Comparison%5Fof%5Fprogramming%5Flanguages" rel="nofollow">comparisons of programming languages</a> for li...
1
2009-04-20T09:36:52Z
[ "python", "ruby", "lambda", "codeblocks" ]
Blocks of code in python
767,519
<p>Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?</p> <p>What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs?</p> <p>I have so far understood the <code>lambda</cod...
8
2009-04-20T09:14:23Z
767,582
<p><code>lambda</code> is the closest equivalent to a Ruby block, and the restriction to one line <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=147358" rel="nofollow">is intentional</a>. It is typically argued that multiline anonymous functions (blocks, in Ruby) are <em>usually</em> less readable than defi...
2
2009-04-20T09:39:34Z
[ "python", "ruby", "lambda", "codeblocks" ]
Blocks of code in python
767,519
<p>Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?</p> <p>What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs?</p> <p>I have so far understood the <code>lambda</cod...
8
2009-04-20T09:14:23Z
767,593
<p>There are good discussions on comp.lang.python that compare to other languages:</p> <ul> <li><a href="http://groups.google.com/group/comp.lang.python/browse%5Fthread/thread/05f1dcda93929d82" rel="nofollow">Let's Talk About Lambda Functions!</a></li> <li><a href="http://groups.google.com/group/comp.lang.python/brows...
2
2009-04-20T09:42:19Z
[ "python", "ruby", "lambda", "codeblocks" ]
Blocks of code in python
767,519
<p>Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python?</p> <p>What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs?</p> <p>I have so far understood the <code>lambda</cod...
8
2009-04-20T09:14:23Z
768,265
<p>The <code>def</code> is equivalent of an assignment statement which only binds the function object to the object reference variable.</p> <p>The object reference variable can then be used to call the function object to execute.</p>
0
2009-04-20T13:32:25Z
[ "python", "ruby", "lambda", "codeblocks" ]
Current working directory no longer inherited from calling process from python 2.5 onwards?
767,531
<p>I updated my python version on windows 2003 server from 2.4 to 2.5.</p> <p>In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:</p> <pre><code>import sub1 </code></pre> <p>as long as the calling script main.py that lives in c:\application was started like this:</p> <pre><c...
3
2009-04-20T09:22:13Z
767,537
<p>to import sub.py you need to:</p> <pre><code>import sub # not sub1 </code></pre>
4
2009-04-20T09:24:58Z
[ "python", "import" ]
Current working directory no longer inherited from calling process from python 2.5 onwards?
767,531
<p>I updated my python version on windows 2003 server from 2.4 to 2.5.</p> <p>In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:</p> <pre><code>import sub1 </code></pre> <p>as long as the calling script main.py that lives in c:\application was started like this:</p> <pre><c...
3
2009-04-20T09:22:13Z
767,538
<p>add this directory to your python path</p>
0
2009-04-20T09:25:20Z
[ "python", "import" ]
Current working directory no longer inherited from calling process from python 2.5 onwards?
767,531
<p>I updated my python version on windows 2003 server from 2.4 to 2.5.</p> <p>In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:</p> <pre><code>import sub1 </code></pre> <p>as long as the calling script main.py that lives in c:\application was started like this:</p> <pre><c...
3
2009-04-20T09:22:13Z
767,555
<p>You can check where python searches for modules. A list of locations is contained in variable sys.path.</p> <p>You can create a simple script (or execute it interactively) that shows this:</p> <pre><code>import sys for x in sys.path: print x </code></pre> <p>By default, python will search the directory in whic...
2
2009-04-20T09:29:49Z
[ "python", "import" ]
Current working directory no longer inherited from calling process from python 2.5 onwards?
767,531
<p>I updated my python version on windows 2003 server from 2.4 to 2.5.</p> <p>In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:</p> <pre><code>import sub1 </code></pre> <p>as long as the calling script main.py that lives in c:\application was started like this:</p> <pre><c...
3
2009-04-20T09:22:13Z
767,556
<p>I assume <code>sub1</code> is a typo? In your question you sometimes refer to <code>sub</code>, sometimes to <code>sub1</code>.</p> <p>I would first of all check that the sub.py file exists in <code>c:\application</code>.</p> <ul> <li>Check the permissions of the sub.py file and the application directory. Can the ...
1
2009-04-20T09:30:05Z
[ "python", "import" ]
Current working directory no longer inherited from calling process from python 2.5 onwards?
767,531
<p>I updated my python version on windows 2003 server from 2.4 to 2.5.</p> <p>In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:</p> <pre><code>import sub1 </code></pre> <p>as long as the calling script main.py that lives in c:\application was started like this:</p> <pre><c...
3
2009-04-20T09:22:13Z
767,706
<p>You need to make the following changes:</p> <ul> <li>turn <code>subdir</code> into a package by adding an empty <code>__init__.py</code> file to the directory</li> <li>change the import to: <code>from subdir import sub1</code></li> </ul>
1
2009-04-20T10:32:41Z
[ "python", "import" ]
Estimating zip size/creation time
767,684
<p>I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. </p> <p>Resources to be zipped are often > 1GB and not necessarily compression-friendly.</p> <p>How do I efficiently estimate its creation time / size?</p>
7
2009-04-20T10:23:56Z
767,701
<p>I suggest you measure the average time it takes to produce a zip of a certain size. Then you calculate the estimate from that measure. However I think the estimate will be very rough in any case if you don't know how well the data compresses. If the data you want to compress had a very similar "profile" each time yo...
3
2009-04-20T10:31:13Z
[ "python", "zip", "time-estimation", "size-estimation" ]
Estimating zip size/creation time
767,684
<p>I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. </p> <p>Resources to be zipped are often > 1GB and not necessarily compression-friendly.</p> <p>How do I efficiently estimate its creation time / size?</p>
7
2009-04-20T10:23:56Z
767,704
<p>Extract a bunch of small parts from the big file. Maybe 64 chunks of 64k each. Randomly selected.</p> <p>Concatenate the data, compress it, measure the time and the compression ratio. Since you've randomly selected parts of the file chances are that you have compressed a representative subset of the data.</p> <p>N...
14
2009-04-20T10:32:27Z
[ "python", "zip", "time-estimation", "size-estimation" ]
Estimating zip size/creation time
767,684
<p>I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. </p> <p>Resources to be zipped are often > 1GB and not necessarily compression-friendly.</p> <p>How do I efficiently estimate its creation time / size?</p>
7
2009-04-20T10:23:56Z
767,730
<p>If you're using the <a href="http://docs.python.org/library/zipfile.html" rel="nofollow">ZipFile.write()</a> method to write your files into the archive, you could do the following:</p> <ol> <li>Get a list of the files you want to zip and their relative sizes</li> <li>Write one file to the archive and time how long...
0
2009-04-20T10:39:00Z
[ "python", "zip", "time-estimation", "size-estimation" ]
Estimating zip size/creation time
767,684
<p>I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. </p> <p>Resources to be zipped are often > 1GB and not necessarily compression-friendly.</p> <p>How do I efficiently estimate its creation time / size?</p>
7
2009-04-20T10:23:56Z
767,735
<p>If its possible to get progress callbacks from the python module i would suggest finding out how many bytes are processed pr second ( By simply storing where in the file you where at start of the second, and where you are at the end ). When you have the data on how fast the computer your on you can off course save i...
1
2009-04-20T10:41:08Z
[ "python", "zip", "time-estimation", "size-estimation" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
767,900
<p>You can test this runnning </p> <pre><code>$ time python -c "import math" </code></pre> <p>However, what would this help you? Importing only happens once and will almost never be a bottle neck. Importing the same module over and over will not run significantly slower than importing it once, since Python tracks whi...
4
2009-04-20T11:38:10Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
767,902
<p>Use profiler: <a href="http://docs.python.org/library/profile.html" rel="nofollow">http://docs.python.org/library/profile.html</a></p> <p>Anyway, the imports are cached.</p>
0
2009-04-20T11:38:48Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
767,948
<p>Use cProfile:</p> <pre><code>python -m cProfile yourscript.py </code></pre>
1
2009-04-20T11:57:17Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
768,218
<p>Tested on Windows in Python 2.4 - you can try it yourself.</p> <pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; ## Built-in module &gt;&gt;&gt; def testTime(): now = time.clock() # use time.time() on unix-based systems import math print time.clock() - now &gt;&gt;&gt; testTime() 7.54285810...
0
2009-04-20T13:21:25Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
769,587
<p>To find out how long an import takes, the simplest way is probably using the <a href="http://docs.python.org/library/timeit.html" rel="nofollow">timeit module</a>..</p> <pre><code>&gt;&gt;&gt; import timeit &gt;&gt;&gt; t = timeit.Timer('import urllib') &gt;&gt;&gt; t.timeit(number = 1000000) 0.98621106147766113 </...
3
2009-04-20T18:46:36Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
10,375,806
<p>One way to profile imports is to use the profile_imports module used in bzr <a href="http://wiki.bazaar.canonical.com/Download">source code</a>:</p> <pre><code># put those two lines at the top of your script import profile_imports profile_imports.install() # display the results profile_imports.log_stack_info(sys.s...
6
2012-04-29T20:41:49Z
[ "python", "import" ]
Time taken by an import in Python
767,881
<p>I want to know how much time an import takes for both built-in as well as user defined modules.</p>
7
2009-04-20T11:29:46Z
38,407,288
<p>I ran into this issue profiling a large legacy application with a multi-second startup time. It's relatively simple to replace the builtin importer with something that does some profiling. Below is a hacky way of showing approximately how long each module takes to execute:</p> <pre><code>import os import sys import...
0
2016-07-16T03:01:37Z
[ "python", "import" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
767,984
<p>This is very similar to the <a href="http://en.wikipedia.org/wiki/Knight%27s%5Ftour">Knight's Tour</a> problem which relates moving a knight around a chess board without revisiting the same square. Basically it's the same problem but with different "Traverse Rules".</p> <p>The key optimisation I remember from tack...
15
2009-04-20T12:06:28Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
767,991
<p>This is just an example of the <a href="http://en.wikipedia.org/wiki/Hamiltonian%5Fpath" rel="nofollow">http://en.wikipedia.org/wiki/Hamiltonian_path</a> problem. German wikipedia claims that it is NP-hard.</p>
5
2009-04-20T12:08:15Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
768,497
<p>An optimization can me made to check for islands (i.e. non-visited spaces with no valid neighbors.) and back out of the traverse until the island is eliminated. This would occur near the "cheap" side of a certain tree traverse. I guess the question is if the reduction is worth the expense.</p>
1
2009-04-20T14:25:46Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
769,302
<p>I decided to look at the problem and see if I could break it into 5x5 solutions with the ending of a solution one jump away from the corner of another. </p> <p>First assumption was that 5x5 is solvable. It is and fast.</p> <p>So I ran solve(0,5) and looked at the results. I drew a 10x10 numbered grid in Excel w...
10
2009-04-20T17:33:38Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
770,983
<p>I wanted to see if I could write a program that would come up with all possible solutions.</p> <pre><code>#! /usr/bin/env perl use Modern::Perl; { package Grid; use Scalar::Util qw'reftype'; sub new{ my($class,$width,$height) = @_; $width ||= 10; $height ||= $width; my $self = bless [], $c...
1
2009-04-21T03:55:54Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
772,644
<p>Eventually, I have come up with the modified Python code to overcome the problem. I've tun the code for a couple of hours and it has already found half a million solutions in a couple of hours.<br /> The full set of solutions still require a total exhaustive search, i.e. to let the program run until it finishes with...
8
2009-04-21T13:40:39Z
[ "python", "puzzle" ]
Riddle: The Square Puzzle
767,912
<p>Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: </p> <p><hr /></p> <p>There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect t...
21
2009-04-20T11:41:59Z
1,417,563
<p>You could count the number of solutions exactly with a sweep-line dynamic programming algorithm.</p>
0
2009-09-13T11:46:45Z
[ "python", "puzzle" ]
delete *.pyc continuation
767,920
<p>As a follow-up to <a href="http://stackoverflow.com/questions/755694/delete-pyc">this question</a>, I have a new question:</p> <p>What is happening internally in <code>os.remove(module_name)</code> and <code>del sys.modules["module_name"]</code>?</p>
-5
2009-04-20T11:46:10Z
768,043
<p>The short answer is that <code>os.remove(module_name)</code> will remove a file from the filesystem. <code>del sys.modules["module_name"]</code> will remove a module from the cache of previously loaded modules that the current Python interpreter is maintaining.</p> <p>In Python, when you import a module, the inter...
4
2009-04-20T12:25:28Z
[ "python", "operating-system", "sys" ]
delete *.pyc continuation
767,920
<p>As a follow-up to <a href="http://stackoverflow.com/questions/755694/delete-pyc">this question</a>, I have a new question:</p> <p>What is happening internally in <code>os.remove(module_name)</code> and <code>del sys.modules["module_name"]</code>?</p>
-5
2009-04-20T11:46:10Z
3,860,074
<p>my oneliner for this job:</p> <pre><code>find . -name *.pyc |xargs rm </code></pre> <p>NB: You need Linux (or Unix-like OS)</p>
1
2010-10-05T00:14:46Z
[ "python", "operating-system", "sys" ]
delete *.pyc continuation
767,920
<p>As a follow-up to <a href="http://stackoverflow.com/questions/755694/delete-pyc">this question</a>, I have a new question:</p> <p>What is happening internally in <code>os.remove(module_name)</code> and <code>del sys.modules["module_name"]</code>?</p>
-5
2009-04-20T11:46:10Z
3,968,672
<p>I start a lot of apps like this</p> <pre><code>import os os.system('attrib +H *.pyc /S') </code></pre> <p>On windows this hides any visible compiled files when the app starts - they give me the pips.</p>
2
2010-10-19T13:11:54Z
[ "python", "operating-system", "sys" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,190
<p>Since 2.5:</p> <p>If you want to fall back <strong>only</strong> on None:</p> <pre><code>a = x if x is not None else y </code></pre> <p>If you want to fall back also on empty string, <code>false</code>, <code>0</code> etc.: </p> <pre><code>a = x if x else y </code></pre> <p>or</p> <pre><code>a = x or y </code>...
26
2009-04-20T13:13:49Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,193
<p>There's python's ternary operation:</p> <pre><code>a = x if x is not None else y </code></pre> <p>Available in 2.5 and up.</p>
1
2009-04-20T13:14:24Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,194
<p>If it's an argument to a function you can do this:</p> <pre><code>def MyFunc( a=2 ): print "a is %d"%a &gt;&gt;&gt; MyFunc() ...a is 2 &gt;&gt;&gt; MyFunc(5) ...a is 5 </code></pre> <p><strong>[Edit]</strong> For the downvoters.. the if/else bit is unnecessary for the solution - just added to make the results...
-1
2009-04-20T13:14:30Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,196
<p>I think this would help, since the problem comes down to check whether a variable is defined or not:<br /> <a href="http://stackoverflow.com/questions/750298/easy-way-to-check-that-variable-is-defined-in-python">Easy way to check that variable is defined in python?</a></p>
1
2009-04-20T13:15:27Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,200
<p>first you can do your full-on with a ternary:</p> <pre><code>a = y if x is None else x </code></pre> <p>but it doesn't solve your problem. what you want to do is more closely implemented with:</p> <pre><code>try: a = x except: a = y </code></pre>
5
2009-04-20T13:15:59Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,625
<p>Most of the solutions relying on if statements don't work for the case where x is 0 or negative.</p> <pre><code>&gt;&gt;&gt; x = 0 &gt;&gt;&gt; y = 2 &gt;&gt;&gt; a = x or y &gt;&gt;&gt; a 2 &gt;&gt;&gt; </code></pre> <p>If you knew the name of the variable ahead of time you could look for like so:</p> <pre><code...
0
2009-04-20T14:49:03Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
768,688
<p>I am quite convinced that there is no 'pythonic' way to do this, because this is not a pattern that is pythonic. Control should not reach an undefined variable reference in elegant code. There are similar ideas that are pythonic. Most obvious:</p> <pre><code>def myRange(start, stop=None): start, stop = (0, s...
3
2009-04-20T15:05:27Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
769,022
<p>Just some nitpicking with your Perl example:</p> <pre><code>my $x = undef; </code></pre> <p>This redundant code can be shortened to:</p> <pre><code>my $x; </code></pre> <p>And the following code doesn't do what you say it does:</p> <pre><code>my $a = $x || $y; </code></pre> <p>This actually assigns $y to $a wh...
3
2009-04-20T16:26:17Z
[ "python" ]
What is the most Pythonic way to provide a fall-back value in an assignment?
768,175
<p>In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance:</p> <pre><code>my $x = undef; my $y = 2; my $a = $x || $y; </code></pre> <p>After this,</p> <pre><code>$a == 2 </code></pre> <p>Is there a concise way to achiev...
14
2009-04-20T13:10:00Z
769,561
<p>One way to rewrite...</p> <pre><code>if x is not None a = x else a = y </code></pre> <p>..is:</p> <pre><code>x = myfunction() if x is None: x = y print x </code></pre> <p>Or, using exceptions (possibly more Python'y, depending on the what the code is doing - if it returns None because there was an ...
0
2009-04-20T18:40:33Z
[ "python" ]
Is there any pywin32 odbc connector documentation available?
768,250
<p>What is a good pywin32 odbc connector documentation and tutorial on the web?</p>
1
2009-04-20T13:28:34Z
768,327
<p>Alternatives:</p> <ul> <li>mxODBC by <a href="http://egenix.com" rel="nofollow">egenix.com</a> (if you need ODBC)</li> <li><a href="http://code.google.com/p/pyodbc" rel="nofollow">pyODBC</a></li> <li><a href="http://www.sqlalchemy.org/" rel="nofollow">sqlalchemy</a> and DB-API 2.0 modules (which isn't ODBC) but it'...
3
2009-04-20T13:46:49Z
[ "python", "windows", "odbc", "pyodbc" ]
Is there any pywin32 odbc connector documentation available?
768,250
<p>What is a good pywin32 odbc connector documentation and tutorial on the web?</p>
1
2009-04-20T13:28:34Z
768,352
<p>The answer is: 'there isn't one'. However, here is an example that shows how to open a connection and issue a query, and how to get column metadata from the result set. The DB API 2.0 specification can be found in <a href="http://www.python.org/dev/peps/pep-0249/" rel="nofollow">PEP 249.</a></p> <pre><code>impor...
2
2009-04-20T13:54:05Z
[ "python", "windows", "odbc", "pyodbc" ]
Is there any pywin32 odbc connector documentation available?
768,250
<p>What is a good pywin32 odbc connector documentation and tutorial on the web?</p>
1
2009-04-20T13:28:34Z
1,023,268
<p>The only 'documentation' that I found was a unit test that was installed with the pywin32 package. It seems to give an overview of the general functionality. I found it here:</p> <p>python dir\Lib\site-packages\win32\test\test_odbc.py</p> <p>I should also point out that I believe it is implements the Python Databa...
1
2009-06-21T05:21:01Z
[ "python", "windows", "odbc", "pyodbc" ]
Common ways to connect to odbc from python on windows?
768,312
<p>What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?</p> <p>I'm looking for something well-documented, robust, actively maintained, etc. <a href="http://code.google.com/p/pyodbc/"><code>pyodbc</code></a> looks good -- are there any other...
20
2009-04-20T13:41:50Z
768,330
<p>I use <a href="http://www.sqlalchemy.org">SQLAlchemy</a> for all python database access. I highly recommend SQLAlchemy.</p> <p>SA uses pyodbc under the hood when connecting to SQL server databases. It uses other DBAPI libraries to connect to other database, for instance cx_Oracle.</p> <p>A simplistic example, usin...
14
2009-04-20T13:47:27Z
[ "python", "windows", "odbc", "pywin32", "pyodbc" ]
Common ways to connect to odbc from python on windows?
768,312
<p>What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?</p> <p>I'm looking for something well-documented, robust, actively maintained, etc. <a href="http://code.google.com/p/pyodbc/"><code>pyodbc</code></a> looks good -- are there any other...
20
2009-04-20T13:41:50Z
768,500
<p>You already suggested <strong>pyodbc</strong>, and I am going to agree with you. </p> <p>It has given me the least amount of issues in my experience; I've used <a href="http://pymssql.sourceforge.net/">pymssql</a> and <a href="http://adodbapi.sourceforge.net/">adodbapi</a>, and when those threw exceptions/created ...
24
2009-04-20T14:26:37Z
[ "python", "windows", "odbc", "pywin32", "pyodbc" ]
Common ways to connect to odbc from python on windows?
768,312
<p>What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?</p> <p>I'm looking for something well-documented, robust, actively maintained, etc. <a href="http://code.google.com/p/pyodbc/"><code>pyodbc</code></a> looks good -- are there any other...
20
2009-04-20T13:41:50Z
3,564,576
<p>I use pyodbc at work and it has never failed me (we have varius dbs). It is robust and fast.</p> <p>It is actively maintained and a python 3 version will come soon.</p> <p>If you want "enterprise" software with payed support you can use <a href="http://www.egenix.com/products/python/mxODBC/" rel="nofollow">mxODBC<...
1
2010-08-25T09:43:51Z
[ "python", "windows", "odbc", "pywin32", "pyodbc" ]
Common ways to connect to odbc from python on windows?
768,312
<p>What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?</p> <p>I'm looking for something well-documented, robust, actively maintained, etc. <a href="http://code.google.com/p/pyodbc/"><code>pyodbc</code></a> looks good -- are there any other...
20
2009-04-20T13:41:50Z
8,640,539
<p>Python 3 is now supported by <a href="http://code.google.com/p/pyodbc/" rel="nofollow">pyodbc</a>!</p>
2
2011-12-27T01:59:41Z
[ "python", "windows", "odbc", "pywin32", "pyodbc" ]
Common ways to connect to odbc from python on windows?
768,312
<p>What library should I use to connect to odbc from python on windows? Is there a good alternative for pywin32 when it comes to odbc?</p> <p>I'm looking for something well-documented, robust, actively maintained, etc. <a href="http://code.google.com/p/pyodbc/"><code>pyodbc</code></a> looks good -- are there any other...
20
2009-04-20T13:41:50Z
12,594,493
<p>Another alternative is <a href="http://code.google.com/p/pypyodbc/" rel="nofollow">pypyodbc</a> which was writeen in pure python, it can been seen as a re-implemenation of the pyodbc moudle in pure python, with only around 1800 lines code, which is good for maintenance.</p> <p>Here's a <a href="https://code.google....
6
2012-09-26T04:19:18Z
[ "python", "windows", "odbc", "pywin32", "pyodbc" ]
classmethod for Tkinter-Monitor-Window
768,474
<p>I would like to realise a monitor window that reports the user about ongoing computations. To do so I wrote a little class. But as I would like to use it accross different modules in an easy fashion I thought to implement it with classmethods. This allows to use it in the following way without instances:</p> <pre><...
0
2009-04-20T14:19:59Z
773,949
<p>You don't need lots of classmethods just to make it easy to use an object across multiple modules.</p> <p>Instead consider making an instance at module import time as shown here:</p> <pre><code>import Tkinter class Monitor(object): def __init__(self): self.mw = Tkinter.Tk() self.mw.title("Messages by N...
3
2009-04-21T18:28:20Z
[ "python", "tkinter", "class-method" ]
Test for Python module dependencies being installed
768,504
<p>How could one test whether a set of modules is installed, given the names of the modules. E.g.</p> <pre><code>modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something </code></pre> <p>While it's possible to write out the tests as:</p> <pre><code>try: import sy...
7
2009-04-20T14:27:40Z
768,523
<p>What's wrong with this?</p> <pre><code>modules = set(["sys", "os", "jinja"]) for m in modules: try: __import__(m) except ImportError: # do something </code></pre> <p>The above uses the <a href="http://docs.python.org/library/functions.html#%5F%5Fimport%5F%5F"><code>__import__</code></a> fun...
6
2009-04-20T14:31:51Z
[ "python", "reflection", "import", "module", "python-module" ]
Test for Python module dependencies being installed
768,504
<p>How could one test whether a set of modules is installed, given the names of the modules. E.g.</p> <pre><code>modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something </code></pre> <p>While it's possible to write out the tests as:</p> <pre><code>try: import sy...
7
2009-04-20T14:27:40Z
768,536
<p>I can suggest you to read this link: <a href="http://code.activestate.com/recipes/223972/" rel="nofollow">http://code.activestate.com/recipes/223972/</a></p>
0
2009-04-20T14:33:20Z
[ "python", "reflection", "import", "module", "python-module" ]
Test for Python module dependencies being installed
768,504
<p>How could one test whether a set of modules is installed, given the names of the modules. E.g.</p> <pre><code>modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something </code></pre> <p>While it's possible to write out the tests as:</p> <pre><code>try: import sy...
7
2009-04-20T14:27:40Z
768,597
<p>You can use the <a href="http://docs.python.org/library/functions.html"><code>__import__()</code></a> function like this::</p> <pre><code>for module in modules: try: __import__(module) except ImportError: do_something() </code></pre> <p>You can also use <a href="http://docs.python.org/libra...
8
2009-04-20T14:43:12Z
[ "python", "reflection", "import", "module", "python-module" ]
Test for Python module dependencies being installed
768,504
<p>How could one test whether a set of modules is installed, given the names of the modules. E.g.</p> <pre><code>modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something </code></pre> <p>While it's possible to write out the tests as:</p> <pre><code>try: import sy...
7
2009-04-20T14:27:40Z
768,598
<pre><code>modules = set(["sys", "os", "jinja"]) try: for module in modules: __import__(module) doSomething() except ImportError, e: print e </code></pre>
2
2009-04-20T14:43:22Z
[ "python", "reflection", "import", "module", "python-module" ]
Test for Python module dependencies being installed
768,504
<p>How could one test whether a set of modules is installed, given the names of the modules. E.g.</p> <pre><code>modules = set(["sys", "os", "jinja"]) for module in modules: # if test(module exists): # do something </code></pre> <p>While it's possible to write out the tests as:</p> <pre><code>try: import sy...
7
2009-04-20T14:27:40Z
3,248,137
<p>It's worth understanding the tradeoffs between the find_module approach and the <code>__import__</code> approach:</p> <ul> <li><code>__import__('foo')</code> will trigger any side effects of loading the module, regardless of whether you save a reference to the imported module. That might be OK, or not.</li> <li><co...
5
2010-07-14T16:05:14Z
[ "python", "reflection", "import", "module", "python-module" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
769,113
<p>You might not need to re-generate source code. That's a bit dangerous for me to say, of course, since you have not actually explained why you think you need to generate a .py file full of code; but:</p> <ul> <li><p>If you want to generate a .py file that people will actually use, maybe so that they can fill out a ...
19
2009-04-20T16:44:53Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
769,199
<p><a href="http://pythoscope.org/">Pythoscope</a> does this to the test cases it automatically generates as does the <a href="http://docs.python.org/library/2to3.html">2to3</a> tool for python 2.6 (it converts python 2.x source into python 3.x source). </p> <p>Both these tools uses the <a href="http://svn.python.org/...
48
2009-04-20T17:04:21Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
769,202
<p>The builtin ast module doesn't seem to have a method to convert back to source. However, the <a href="https://pypi.python.org/pypi/codegen/1.0">codegen</a> module here provides a pretty printer for the ast that would enable you do do so. eg.</p> <pre><code>import ast import codegen expr=""" def foo(): print("h...
43
2009-04-20T17:05:41Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
2,406,749
<p>A <a href="http://en.wikipedia.org/wiki/Program_transformation" rel="nofollow">Program Transformation System</a> is a tool that parses source text, builds ASTs, allows you to modify them using source-to-source transformations ("if you see this pattern, replace it by that pattern"). Such tools are ideal for doing m...
-1
2010-03-09T04:51:50Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
18,937,780
<p>I've created recently quite stable (core is really well tested) and extensible piece of code which generates code from <code>ast</code> tree: <a href="https://github.com/paluh/code-formatter" rel="nofollow">https://github.com/paluh/code-formatter</a> .</p> <p>I'm using my project as a base for a small vim plugin (w...
5
2013-09-21T21:22:17Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
39,001,153
<p><a href="http://stackoverflow.com/a/769202">One of the other answers</a> recommends <code>codegen</code>, which seems to have been superceded by <a href="https://github.com/berkerpeksag/astor" rel="nofollow"><code>astor</code></a>. The version of <a href="https://pypi.python.org/pypi/astor" rel="nofollow"><code>asto...
1
2016-08-17T15:50:17Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
Parse a .py file, read the AST, modify it, then write back the modified source code
768,634
<p>I want to programmatically edit python source code. Basically I want to read a <code>.py</code> file, generate the <a href="http://en.wikipedia.org/wiki/Abstract%5Fsyntax%5Ftree">AST</a>, and then write back the modified python source code (i.e. another <code>.py</code> file).</p> <p>There are ways to parse/compile...
100
2009-04-20T14:51:55Z
39,413,170
<p>In a different answer I suggested using the <code>astor</code> package, but I have since found a more up-to-date AST un-parsing package called <a href="https://github.com/simonpercivall/astunparse" rel="nofollow"><code>astunparse</code></a>:</p> <pre><code>&gt;&gt;&gt; import ast &gt;&gt;&gt; import astunparse &gt;...
1
2016-09-09T13:52:09Z
[ "python", "compiler-construction", "abstract-syntax-tree" ]
How do I prevent execution of arbitrary commands from a Django app making system calls?
768,677
<p>I have a Django application I'm developing that must make a system call to an external program on the server. In creating the command for the system call, the application takes values from a form and uses them as parameters for the call. I suppose this means that one can essentially use bogus parameters and write ar...
5
2009-04-20T15:03:41Z
768,696
<p>The answer is, don't let users type in shell commands! There is no excuse for allowing arbitrary shell commands to be executed.</p> <p>Also, if you really must allow users to supply arguments to external commands, don't use a shell. In C, you could use <code>execvp()</code> to supply arguments directly to the comma...
3
2009-04-20T15:08:15Z
[ "python", "django", "security" ]
How do I prevent execution of arbitrary commands from a Django app making system calls?
768,677
<p>I have a Django application I'm developing that must make a system call to an external program on the server. In creating the command for the system call, the application takes values from a form and uses them as parameters for the call. I suppose this means that one can essentially use bogus parameters and write ar...
5
2009-04-20T15:03:41Z
768,698
<p>Depending on the range of your commands, you could customize the form, so that parameters are entered in separate form-fields. Those you can parse for fitting values more easily. Also, beware of backticks and other shell specific stuff.</p>
0
2009-04-20T15:08:44Z
[ "python", "django", "security" ]
How do I prevent execution of arbitrary commands from a Django app making system calls?
768,677
<p>I have a Django application I'm developing that must make a system call to an external program on the server. In creating the command for the system call, the application takes values from a form and uses them as parameters for the call. I suppose this means that one can essentially use bogus parameters and write ar...
5
2009-04-20T15:03:41Z
768,720
<p>Based on my understanding of the question, I'm assuming you aren't letting the users specify commands to run on the shell, but just arguments to those commands. In this case, you can avoid <a href="http://en.wikipedia.org/wiki/Code%5Finjection#Shell%5FInjection">shell injection</a> attacks by using the <a href="htt...
10
2009-04-20T15:14:47Z
[ "python", "django", "security" ]
How do I prevent execution of arbitrary commands from a Django app making system calls?
768,677
<p>I have a Django application I'm developing that must make a system call to an external program on the server. In creating the command for the system call, the application takes values from a form and uses them as parameters for the call. I suppose this means that one can essentially use bogus parameters and write ar...
5
2009-04-20T15:03:41Z
768,728
<p>By never trusting users. Any data coming from the web browser should be considered tainted. And absolutely do not try to validate the data via JS or by limiting what can be entered in the FORM fields. You need to do the tests on the server before passing it to your external application.</p> <p><strong>Update after ...
6
2009-04-20T15:16:21Z
[ "python", "django", "security" ]