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 I check for Python version in a program that uses new language features?
446,052
<p>If I have a Python script that requires at least a particular version of Python, what is the correct way to fail gracefully when an earlier version of Python is used to launch the script?</p> <p>How do I get control early enough to issue an error message and exit?</p> <p>For example, I have a program that uses the...
194
2009-01-15T08:45:28Z
10,519,951
<p>I just found this question after a quick search whilst trying to solve the problem myself and I've come up with a hybrid based on a few of the suggestions above.</p> <p>I like DevPlayer's idea of using a wrapper script, but the downside is that you end up maintaining multiple wrappers for different OSes, so I decid...
1
2012-05-09T16:11:57Z
[ "python", "version" ]
How can I check for Python version in a program that uses new language features?
446,052
<p>If I have a Python script that requires at least a particular version of Python, what is the correct way to fail gracefully when an earlier version of Python is used to launch the script?</p> <p>How do I get control early enough to issue an error message and exit?</p> <p>For example, I have a program that uses the...
194
2009-01-15T08:45:28Z
22,543,960
<p>Here's a quick and simple way to ensure that a python script will exit cleanly if you don't meet version requirements in order to run the script</p> <pre><code># Check python version import sys if sys.version_info &lt; ( 3, 2): # python too old, kill the script sys.exit("This script requires Python 3.2 or ...
-1
2014-03-20T20:07:23Z
[ "python", "version" ]
How can I check for Python version in a program that uses new language features?
446,052
<p>If I have a Python script that requires at least a particular version of Python, what is the correct way to fail gracefully when an earlier version of Python is used to launch the script?</p> <p>How do I get control early enough to issue an error message and exit?</p> <p>For example, I have a program that uses the...
194
2009-01-15T08:45:28Z
31,052,037
<pre><code>import sys # prints whether python is version 3 or not python_version = sys.version_info.major if python_version == 3: print("is python 3") else: print("not python 3") </code></pre>
9
2015-06-25T13:39:04Z
[ "python", "version" ]
How can I check for Python version in a program that uses new language features?
446,052
<p>If I have a Python script that requires at least a particular version of Python, what is the correct way to fail gracefully when an earlier version of Python is used to launch the script?</p> <p>How do I get control early enough to issue an error message and exit?</p> <p>For example, I have a program that uses the...
194
2009-01-15T08:45:28Z
33,301,084
<p>Put the following at the very top of your file:</p> <pre><code>import sys if float(sys.version.split()[0][:3]) &lt; 2.7: print "Python 2.7 or higher required to run this code, " + sys.version.split()[0] + " detected, exiting." exit(1) </code></pre> <p>Then continue on with the normal Python code:</p> <pr...
2
2015-10-23T11:19:29Z
[ "python", "version" ]
Possible values from sys.platform?
446,209
<p>What are the possible return values from the following command?</p> <pre><code>import sys print sys.platform </code></pre> <p>I know there is a lot of possibilities, so I'm mainly interested in the "main" ones (Windows, Linux, Mac OS)</p>
34
2009-01-15T10:02:59Z
446,210
<p>Mac OS X (10.4, 10.5, 10.7, 10.8):</p> <pre><code>darwin </code></pre> <p>Linux (2.6 kernel):</p> <pre><code>linux2 </code></pre> <p>Windows XP 32 bit:</p> <pre><code>win32 </code></pre> <p>Versions in brackets have been checked - other/newer versions are likely to be the same.</p>
22
2009-01-15T10:03:16Z
[ "python", "cross-platform" ]
Possible values from sys.platform?
446,209
<p>What are the possible return values from the following command?</p> <pre><code>import sys print sys.platform </code></pre> <p>I know there is a lot of possibilities, so I'm mainly interested in the "main" ones (Windows, Linux, Mac OS)</p>
34
2009-01-15T10:02:59Z
446,215
<blockquote> <p>As others have indicated, sys.platform is derived from the name that the system vendor gives their system. However, Python also adds plat- to sys.path, so you can look at all the plat-* directories in the Python distribution.</p> <p>This gives you the list</p> <p>aix3 aix4 athe...
19
2009-01-15T10:04:52Z
[ "python", "cross-platform" ]
Possible values from sys.platform?
446,209
<p>What are the possible return values from the following command?</p> <pre><code>import sys print sys.platform </code></pre> <p>I know there is a lot of possibilities, so I'm mainly interested in the "main" ones (Windows, Linux, Mac OS)</p>
34
2009-01-15T10:02:59Z
446,216
<p>FreeBSD 7.0: <code>freebsd7</code>. FreeBSD8 but build performed on previous version, same answer.</p> <p>So be aware you get the platform used for the build, not necessarely the one you're running on.</p>
6
2009-01-15T10:06:02Z
[ "python", "cross-platform" ]
Possible values from sys.platform?
446,209
<p>What are the possible return values from the following command?</p> <pre><code>import sys print sys.platform </code></pre> <p>I know there is a lot of possibilities, so I'm mainly interested in the "main" ones (Windows, Linux, Mac OS)</p>
34
2009-01-15T10:02:59Z
13,874,620
<pre><code>.---------------------.----------. | System | Value | |---------------------|----------| | Linux (2.x and 3.x) | linux2 | | Windows | win32 | | Windows/Cygwin | cygwin | | Mac OS X | darwin | | OS/2 | os2 | | OS/2 EMX | os2em...
28
2012-12-14T07:49:05Z
[ "python", "cross-platform" ]
Possible values from sys.platform?
446,209
<p>What are the possible return values from the following command?</p> <pre><code>import sys print sys.platform </code></pre> <p>I know there is a lot of possibilities, so I'm mainly interested in the "main" ones (Windows, Linux, Mac OS)</p>
34
2009-01-15T10:02:59Z
20,827,181
<p>As of Dec 29 2013, OS X 10.9.1 Mavericks is still labeled Darwin.</p>
1
2013-12-29T16:13:55Z
[ "python", "cross-platform" ]
How do I reverse Unicode decomposition using Python?
446,222
<p>Using Python 2.5, I have some text in stored in a unicode object:</p> <blockquote> <p>Dinis e Isabel, uma difı´cil relac¸a˜o conjugal e polı´tica</p> </blockquote> <p>This appears to be <a href="http://www.unicode.org/reports/tr15/#Decomposition">decomposed Unicode</a>. Is there a generic way in Python t...
5
2009-01-15T10:08:25Z
446,255
<p>I can't really give you a definitive answer to your question because I never tried that. But there is a <a href="http://docs.python.org/library/unicodedata.html" rel="nofollow">unicodedata module</a> in the standard library. It has two functions <code>decomposition()</code> and <code>normalize()</code> that might he...
1
2009-01-15T10:18:38Z
[ "python", "unicode" ]
How do I reverse Unicode decomposition using Python?
446,222
<p>Using Python 2.5, I have some text in stored in a unicode object:</p> <blockquote> <p>Dinis e Isabel, uma difı´cil relac¸a˜o conjugal e polı´tica</p> </blockquote> <p>This appears to be <a href="http://www.unicode.org/reports/tr15/#Decomposition">decomposed Unicode</a>. Is there a generic way in Python t...
5
2009-01-15T10:08:25Z
446,291
<p>I think you are looking for this:</p> <pre><code>&gt;&gt;&gt; import unicodedata &gt;&gt;&gt; print unicodedata.normalize("NFC",u"c\u0327") ç </code></pre>
7
2009-01-15T10:33:47Z
[ "python", "unicode" ]
How do I reverse Unicode decomposition using Python?
446,222
<p>Using Python 2.5, I have some text in stored in a unicode object:</p> <blockquote> <p>Dinis e Isabel, uma difı´cil relac¸a˜o conjugal e polı´tica</p> </blockquote> <p>This appears to be <a href="http://www.unicode.org/reports/tr15/#Decomposition">decomposed Unicode</a>. Is there a generic way in Python t...
5
2009-01-15T10:08:25Z
447,047
<blockquote> <p>Unfortunately it seems I actually have (for example) \u00B8 (cedilla) instead of \u0327 (combining cedilla) in my text.</p> </blockquote> <p>Eurgh, nasty! You can still do it automatically, though the process wouldn't be entirely lossless as it involves a compatibility decomposition (NFKD).</p> <p>N...
5
2009-01-15T14:55:26Z
[ "python", "unicode" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
446,712
<p>You can use <a href="http://search.cpan.org/perldoc?PAR" rel="nofollow">Perl Archive Toolkit</a> to bring a minimal perl core + needed modules + your Perl program with you.</p> <p>And you can even convert it using <a href="http://search.cpan.org/perldoc?pp" rel="nofollow">PAR Packer</a> to a windows exe file that w...
23
2009-01-15T13:28:05Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
446,716
<p>You could convert the script to an executable. In Python and Windows you can easily do that with <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a>. There are similar solutions for Perl and Ruby, but I believe py2exe is both free and reliable.</p>
3
2009-01-15T13:29:53Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
446,741
<p>You can get Windows executables in all three languages. </p> <ul> <li>As usual with Perl, there's more than one way to do it: <ul> <li><a href="http://search.cpan.org/perldoc?pp">PAR Packer</a> (free/open-source)</li> <li><a href="http://www.indigostar.com/perl2exe.htm">perl2exe</a> (shareware)</li> <li><a href="h...
30
2009-01-15T13:37:12Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
446,865
<p>A non-technical audience? You'll also want some sort of basic user interface, probably of the `graphical' type. You might try <a href="http://www.wxpython.org" rel="nofollow">wxPython</a>, which can be packaged into a Windows executable with <a href="http://wiki.wxpython.org/index.cgi/DistributingYourApplication" re...
2
2009-01-15T14:11:45Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
447,176
<p>"installing Perl on Windows would not be worth it for them" Really? It's that complex?</p> <p>Python has a simple .MSI that neatly installs itself with no muss or fuss.</p> <p>A simple application program is just a few .py files, so, I don't see a big pain factor there.</p> <p>You know your customers best.</p> ...
0
2009-01-15T15:32:55Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
447,259
<p><a href="http://hackety.org/2008/06/19/stampingExesAndDmgs.html" rel="nofollow">Shoes</a> can make exe's, and binaries for other platforms as well, and you get an integrated GUI.</p>
1
2009-01-15T15:46:01Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
447,388
<p>Are you sure there is a headache? <a href="http://www.activestate.com/" rel="nofollow">ActivePerl</a> and <a href="http://www.strawberryperl.com" rel="nofollow">Strawberry Perl</a> are dead easy to install on Windows with just a couple of mouse clicks. Python is just as easy to install from what I hear. </p> <p>Hav...
6
2009-01-15T16:12:22Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
449,470
<p>Using <a href="http://search.cpan.org/perldoc?PAR">PAR, the Perl Aachiver</a> has already been mentioned in other answers, and is an excellent solution. There's a short tutorial on <a href="http://perltraining.com.au/tips/2008-05-23.html">building executables using PAR</a> that was published as a <a href="http://pe...
8
2009-01-16T03:58:59Z
[ "python", "ruby", "perl" ]
How can I deploy a Perl/Python/Ruby script without installing an interpreter?
446,685
<p>I want to write a piece of software which is essentially a regex data scrubber. I am going to take a contact list in CSV and remove all non-word characters and such from the person's name.</p> <p>This project has Perl written all over it but my client base is largely non-technical and installing Perl on Windows wou...
21
2009-01-15T13:21:14Z
2,483,939
<p>Ruby has the <a href="http://ocra.rubyforge.org/" rel="nofollow">OCRA</a> program that packages everything up into a nice neat .EXE file. The RubyScript2EXE program is no longer maintained, so this would be the better solution.</p>
1
2010-03-20T17:12:11Z
[ "python", "ruby", "perl" ]
Getting out of a function in Python
446,782
<p>I want to get out of a function when an exception occurs or so. I want to use other method than 'return'</p>
0
2009-01-15T13:48:51Z
446,805
<p>Can't think of another way to "get out" of a function other than a) return, b) throw an exception, or c) terminate execution of the program.</p>
3
2009-01-15T13:54:31Z
[ "python", "exception", "function" ]
Getting out of a function in Python
446,782
<p>I want to get out of a function when an exception occurs or so. I want to use other method than 'return'</p>
0
2009-01-15T13:48:51Z
446,806
<p>If you catch an exception and then want to rethrow it, <a href="http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html">this pattern</a> is pretty simple:</p> <pre><code>try: do_something_dangerous() except: do_something_to_apologize() raise </code></pre> <p>Of course if you want to ...
13
2009-01-15T13:54:39Z
[ "python", "exception", "function" ]
Getting out of a function in Python
446,782
<p>I want to get out of a function when an exception occurs or so. I want to use other method than 'return'</p>
0
2009-01-15T13:48:51Z
446,813
<p>The exception itself will terminate the function:</p> <pre><code>def f(): a = 1 / 0 # will raise an exception return a try: f() except: print 'no longer in f()' </code></pre>
3
2009-01-15T13:56:27Z
[ "python", "exception", "function" ]
Getting out of a function in Python
446,782
<p>I want to get out of a function when an exception occurs or so. I want to use other method than 'return'</p>
0
2009-01-15T13:48:51Z
446,817
<p>Assuming you want to "stop" execution inside of that method. There's a few things you can do.</p> <ol> <li><strong>Don't catch the exception.</strong> This will return control to the method that called it in the first place. You can then do whatever you want with it. </li> <li><strong>sys.exit(0)</strong> This one ...
2
2009-01-15T13:57:05Z
[ "python", "exception", "function" ]
Getting out of a function in Python
446,782
<p>I want to get out of a function when an exception occurs or so. I want to use other method than 'return'</p>
0
2009-01-15T13:48:51Z
448,771
<p>As others have pointed out, an exception will get you out of the method. You shouldn't be ashamed or embarassed by exceptions; an exception indicates an <strong>error</strong>, but that's not necessarily the same as a <strong>bug</strong>.</p> <p>For example, say I'm writing a factorial function. Factorial isn't ...
1
2009-01-15T22:36:33Z
[ "python", "exception", "function" ]
What's the best way to specify a proxy with username and password for an **https** connection in python?
446,869
<p>I read somewhere that currently urllib2 doesn't support authenticated https connection. My proxy uses a basic authentication only, but how to open an https based webpage through it . Please help me. </p> <p>Thanks.</p>
3
2009-01-15T14:12:21Z
447,096
<p>You can use <a href="http://code.google.com/p/httplib2/" rel="nofollow">httplib2</a>, which solves some of the limitations of urllib2 including this one. There is an example <a href="http://code.google.com/p/httplib2/wiki/Examples" rel="nofollow">here</a> of how to do Basic Authentication on a https connection.</p>
0
2009-01-15T15:12:19Z
[ "python", "authentication", "proxy", "https" ]
What's the best way to specify a proxy with username and password for an **https** connection in python?
446,869
<p>I read somewhere that currently urllib2 doesn't support authenticated https connection. My proxy uses a basic authentication only, but how to open an https based webpage through it . Please help me. </p> <p>Thanks.</p>
3
2009-01-15T14:12:21Z
447,102
<p>"urllib2 doesn't support authenticated https connection" False.</p> <pre><code> # Build Handler to support HTTP Basic Authentication... basic_handler = urllib2.HTTPBasicAuthHandler() basic_handler.add_password(realm, self.urlBase, username, password) # Get cookies, also, to handle login self.cook...
2
2009-01-15T15:13:27Z
[ "python", "authentication", "proxy", "https" ]
python web-services: returning a fault from the server using ZSI
446,912
<p>I'm interested in writing a python client for a web-service, and for testing purposes it would be very interesting also to have a simple stub server. I'm using python 2.3, and ZSI 2.0.</p> <p>My problem is that I do not manage to return an exception from the server.</p> <p>If I raise an exception of the type used ...
2
2009-01-15T14:24:18Z
447,795
<p>apologies for not being able to answer the question.</p> <p>I battled with ZSI for a while.</p> <p>I'm now using SUDS : https://fedorahosted.org/suds/wiki , and everything has become much simpler.</p>
0
2009-01-15T17:58:21Z
[ "python", "web-services", "fault", "zsi" ]
python web-services: returning a fault from the server using ZSI
446,912
<p>I'm interested in writing a python client for a web-service, and for testing purposes it would be very interesting also to have a simple stub server. I'm using python 2.3, and ZSI 2.0.</p> <p>My problem is that I do not manage to return an exception from the server.</p> <p>If I raise an exception of the type used ...
2
2009-01-15T14:24:18Z
456,998
<p>I've found the answer in this <a href="http://pywebsvcs.sourceforge.net/cookbook.pdf" rel="nofollow">ZSI Cookbook</a>, by Chris Hoobs, linked at the bottom of the <a href="http://pywebsvcs.sourceforge.net/" rel="nofollow">ZSI home page</a>: </p> <blockquote> <p>5.4 Exceptions<br /> A thorny question is how to ...
1
2009-01-19T09:22:07Z
[ "python", "web-services", "fault", "zsi" ]
Where to get/How to build Windows binary of mod_wsgi with python 3.0 support?
447,015
<p>I wanted to experiment a little with python 3.0 at home. I got python 3.0 working, I've played around with some scripts, and I thought it would be fun to try to make a small web-project with it.</p> <p>As I was googling, it turned out, that mod_python, for some reasons, will not be able to support python 3.0.</p> ...
5
2009-01-15T14:48:24Z
449,772
<p>I would like to find either 2.6 (preferable) or 3.0 (okay) Windows binaries myself, and have looked into this a bit.</p> <p>There are Windows build steps for mod_wsgi buried deep in the Google Group for it. I think they're out of date; only one person appears to have ever done it and told the world how well it work...
1
2009-01-16T07:41:46Z
[ "python", "apache", "visual-c++", "mod-wsgi" ]
Where to get/How to build Windows binary of mod_wsgi with python 3.0 support?
447,015
<p>I wanted to experiment a little with python 3.0 at home. I got python 3.0 working, I've played around with some scripts, and I thought it would be fun to try to make a small web-project with it.</p> <p>As I was googling, it turned out, that mod_python, for some reasons, will not be able to support python 3.0.</p> ...
5
2009-01-15T14:48:24Z
603,874
<p>For what it is worth, this is still (as of March 2nd 2008) up in the air as far as I can tell. See:</p> <p><a href="http://groups.google.com/group/modwsgi/browse_thread/thread/93e5e56a04fe37ab/5883f8f6a0fcc945" rel="nofollow">http://groups.google.com/group/modwsgi/browse_thread/thread/93e5e56a04fe37ab/5883f8f6a0fcc...
0
2009-03-02T20:28:10Z
[ "python", "apache", "visual-c++", "mod-wsgi" ]
Where to get/How to build Windows binary of mod_wsgi with python 3.0 support?
447,015
<p>I wanted to experiment a little with python 3.0 at home. I got python 3.0 working, I've played around with some scripts, and I thought it would be fun to try to make a small web-project with it.</p> <p>As I was googling, it turned out, that mod_python, for some reasons, will not be able to support python 3.0.</p> ...
5
2009-01-15T14:48:24Z
1,037,956
<p>Binaries for Windows are now being supplied from the mod_wsgi site for Apache 2.2 and Python 2.6 and 3.0. Python 3.0 is only supported for mod_wsgi 3.0 onwards. See:</p> <p><a href="http://code.google.com/p/modwsgi/downloads/list" rel="nofollow">http://code.google.com/p/modwsgi/downloads/list</a></p> <hr> <p>UPDA...
9
2009-06-24T12:06:29Z
[ "python", "apache", "visual-c++", "mod-wsgi" ]
Where to get/How to build Windows binary of mod_wsgi with python 3.0 support?
447,015
<p>I wanted to experiment a little with python 3.0 at home. I got python 3.0 working, I've played around with some scripts, and I thought it would be fun to try to make a small web-project with it.</p> <p>As I was googling, it turned out, that mod_python, for some reasons, will not be able to support python 3.0.</p> ...
5
2009-01-15T14:48:24Z
2,964,594
<p>I was able to build mod_wsgi for python 2.54 (my python is 2.5 therefore i have to use MSVC7). Using xampp Apache 2.2.14 (it is just a dev machine, for testing purposes):</p> <p>Instructions:</p> <ol> <li>download <a href="http://adal.chiriliuc.com/temp/win32.zip" rel="nofollow">http://adal.chiriliuc.com/temp/win3...
0
2010-06-03T09:18:01Z
[ "python", "apache", "visual-c++", "mod-wsgi" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
447,140
<p>You might like <a href="http://code.activestate.com/recipes/456151/" rel="nofollow">this module</a> which implements the wrapper you are looking for.</p>
5
2009-01-15T15:20:54Z
[ "python", "regex" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
448,018
<p>You could write a utility class to do the "save state and return result" operation. I don't think this is that hackish. It's fairly trivial to implement:</p> <pre><code>class Var(object): def __init__(self, val=None): self.val = val def set(self, result): self.val = result return result <...
1
2009-01-15T19:05:47Z
[ "python", "regex" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
448,026
<p>Trying out some ideas...</p> <p>It looks like you would ideally want an expression with side effects. If this were allowed in Python:</p> <pre><code>if m = re.match('foo (\w+) bar (\d+)', line): # do stuff with m.group(1) and m.group(2) elif m = re.match('baz whoo_(\d+)', line): # do stuff with m.group(1) eli...
3
2009-01-15T19:07:53Z
[ "python", "regex" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
448,044
<pre><code>class last(object): def __init__(self, wrapped, initial=None): self.last = initial self.func = wrapped def __call__(self, *args, **kwds): self.last = self.func(*args, **kwds) return self.last def test(): """ &gt;&gt;&gt; test() crude, but effective: (oYo) """ import re m = l...
1
2009-01-15T19:12:13Z
[ "python", "regex" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
449,924
<p>Based on the great answers to this question, I've concocted the following mechanism. It appears like a general way to solve the "no assignment in conditions" limitation of Python. The focus is transparency, implemented by silent delegation:</p> <pre><code>class Var(object): def __init__(self, val=None): ...
1
2009-01-16T09:38:38Z
[ "python", "regex" ]
Python's re module - saving state?
447,086
<p>One of the biggest annoyances I find in Python is the inability of the <code>re</code> module to save its state without explicitly doing it in a match object. Often, one needs to parse lines and if they comply a certain regex take out values from them by the same regex. I would like to write code like this:</p> <pr...
11
2009-01-15T15:09:57Z
8,800,979
<p>Probably the simplest solution is to return early so you can get back to creating variables instead of needing to make an immediate test.</p> <pre><code>def get_results(line): m = re.match('foo (\w+) bar (\d+)', line) if m: # do stuff with .group(1) and .group(2) return result m = re.match('...
0
2012-01-10T09:29:32Z
[ "python", "regex" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
447,138
<p>There are a few encodings that can be used to de-/encode from str to str or from unicode to unicode. For example base64, hex or even rot13. They are listed in the <a href="http://docs.python.org/library/codecs.html" rel="nofollow">codecs module</a>.</p> <p>Edit:</p> <p>The decode message on a unicode string can un...
5
2009-01-15T15:20:41Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
447,401
<p>mybytestring.encode(somecodec) is meaningful for these values of <code>somecodec</code>:</p> <ul> <li>base64 </li> <li>bz2</li> <li>zlib</li> <li>hex</li> <li>quopri</li> <li>rot13</li> <li>string_escape</li> <li>uu</li> </ul> <p>I am not sure what decoding an already decoded unicode text is good for. Trying that ...
11
2009-01-15T16:15:39Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
448,383
<p>To represent a unicode string as a string of bytes is known as <b>encoding</b>. Use <code>u'...'.encode(encoding)</code>.</p> <p>Example:</p> <pre> >>> u'æøå'.encode('utf8') '\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5' >>> u'æøå'.encode('latin1') '\xc3\xa6\xc3\xb8\xc3\xa5' >>> u'æÃ...
45
2009-01-15T20:41:48Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
449,281
<p>The <code>decode</code> method of unicode strings really doesn't have any applications at all (unless you have some non-text data in a unicode string for some reason -- see below). It is mainly there for historical reasons, i think. In Python 3 it is completely gone.</p> <p><code>unicode().decode()</code> will perf...
83
2009-01-16T02:06:33Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
449,878
<p>You should read <a href="http://stackoverflow.com/questions/368805/python-unicodedecodeerror-am-i-misunderstanding-encode#370199">Python UnicodeDecodeError - Am I misunderstanding encode</a>. My understanding of unicode in Python was a lot clearer after reading the accepted answer.</p>
6
2009-01-16T08:47:01Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
What is the difference between encode/decode?
447,107
<p>I've never been sure that I understand the difference between str/unicode decode and encode.</p> <p>I know that <code>str().decode()</code> is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.</p> <p>I know that <code>unico...
122
2009-01-15T15:13:59Z
452,378
<p>anUnicode.<strong>encode</strong>('encoding') results in a <strong>string</strong> object and can be called on a unicode object </p> <p>aString.<strong>decode</strong>('encoding') results in an <strong>unicode</strong> object and can be called on a string, encoded in given encoding.</p> <p><hr /></p> <p>Some mor...
9
2009-01-16T23:11:40Z
[ "python", "string", "unicode", "character-encoding", "python-2.x" ]
Django: Increment blog entry view count by one. Is this efficient?
447,117
<p>I have the following code in my index view.</p> <pre><code>latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry in latest_entry_list: entry.views = entry.views + 1 entry.save() </code></pre> <p>If there are ten (the limit) rows returned from the initial que...
16
2009-01-15T15:15:31Z
447,133
<p><strong>Revised</strong></p> <p>You're updating 10 separate, individual, distinct objects.</p> <p>The 10 separate, individual, distinct updates can't easily be collapsed into one magical update that somehow touches 10 objects.</p>
3
2009-01-15T15:19:08Z
[ "python", "database", "django", "performance" ]
Django: Increment blog entry view count by one. Is this efficient?
447,117
<p>I have the following code in my index view.</p> <pre><code>latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry in latest_entry_list: entry.views = entry.views + 1 entry.save() </code></pre> <p>If there are ten (the limit) rows returned from the initial que...
16
2009-01-15T15:15:31Z
447,433
<p>You could handle the updates in a single transaction, which could improve performance significantly. Use a separate function, decorated with @transaction.commit_manually.</p> <pre><code>@transaction.commit_manually def update_latest_entries(latest_entry_list): for entry in latest_entry_list: entry.view...
12
2009-01-15T16:22:34Z
[ "python", "database", "django", "performance" ]
Django: Increment blog entry view count by one. Is this efficient?
447,117
<p>I have the following code in my index view.</p> <pre><code>latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry in latest_entry_list: entry.views = entry.views + 1 entry.save() </code></pre> <p>If there are ten (the limit) rows returned from the initial que...
16
2009-01-15T15:15:31Z
448,196
<p>If you really need the efficiency, at the moment you'd have to drop down into SQL and run the update yourself. That's not worth the added complexity in this case, though.</p> <p>By Django 1.1 you'll be able to do this in a single SQL call via the ORM using <a href="http://groups.google.com/group/django-developers/...
3
2009-01-15T19:50:01Z
[ "python", "database", "django", "performance" ]
Django: Increment blog entry view count by one. Is this efficient?
447,117
<p>I have the following code in my index view.</p> <pre><code>latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry in latest_entry_list: entry.views = entry.views + 1 entry.save() </code></pre> <p>If there are ten (the limit) rows returned from the initial que...
16
2009-01-15T15:15:31Z
889,463
<p>You can use <code>F()</code> objects for this. </p> <p>Here is how you import <code>F</code>: <code>from django.db.models import F</code></p> <p><strong><a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#updating-multiple-objects-at-once" rel="nofollow">New in Django 1.1</a>.</strong><br> Calls to up...
37
2009-05-20T18:23:57Z
[ "python", "database", "django", "performance" ]
Django: Increment blog entry view count by one. Is this efficient?
447,117
<p>I have the following code in my index view.</p> <pre><code>latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry in latest_entry_list: entry.views = entry.views + 1 entry.save() </code></pre> <p>If there are ten (the limit) rows returned from the initial que...
16
2009-01-15T15:15:31Z
2,044,825
<p>A performance improvement to the previous entry. This results in one database hit, with a subquery.</p> <pre><code>latest_entry_query_set = Entry.objects.filter(is_published=True) .order_by('-date_published')[:10] non_sliced_query_set = Entry.objects.filter(pk__in=latest_entr...
2
2010-01-11T20:50:46Z
[ "python", "database", "django", "performance" ]
urlsafe_b64encode always ends in '=' ?:
447,875
<p>I think this must be a stupid question, but why do the results of urlsafe_b64encode() always end with a '=' for me? '=' isn't url safe?</p> <pre><code>from random import getrandbits from base64 import urlsafe_b64encode from hashlib import sha256 from time import sleep def genKey(): keyLenBits = 64 a = str(ge...
8
2009-01-15T18:27:40Z
447,925
<p><a href="http://en.wikipedia.org/wiki/Base64">Base64</a> uses '=' for padding. Your string bit length isn't divisible by 24, so it's padded with '='. By the way, '=' should be URL safe as it's often used for parameters in URLs.</p> <p>See <a href="http://mail.python.org/pipermail/python-bugs-list/2007-February/0371...
6
2009-01-15T18:38:06Z
[ "python", "hash", "base64" ]
urlsafe_b64encode always ends in '=' ?:
447,875
<p>I think this must be a stupid question, but why do the results of urlsafe_b64encode() always end with a '=' for me? '=' isn't url safe?</p> <pre><code>from random import getrandbits from base64 import urlsafe_b64encode from hashlib import sha256 from time import sleep def genKey(): keyLenBits = 64 a = str(ge...
8
2009-01-15T18:27:40Z
447,994
<p>The '=' is for padding. If you want to pass the output as the value of a URL parameter, you'll want to escape it first, so that the padding doesn't get lost when later reading in the value.</p> <pre><code>import urllib param_value = urllib.quote_plus(b64_data) </code></pre> <p>Python is just following RFC3548 by ...
2
2009-01-15T18:58:48Z
[ "python", "hash", "base64" ]
urlsafe_b64encode always ends in '=' ?:
447,875
<p>I think this must be a stupid question, but why do the results of urlsafe_b64encode() always end with a '=' for me? '=' isn't url safe?</p> <pre><code>from random import getrandbits from base64 import urlsafe_b64encode from hashlib import sha256 from time import sleep def genKey(): keyLenBits = 64 a = str(ge...
8
2009-01-15T18:27:40Z
448,003
<p>I would expect that an URI parser would ignore a "=" in the value part of a parameter.</p> <p>The URI parameters are: "&amp;" , [name], "=", [value], next, so an equals sign in the value part is harmless. An unescaped ampersand has more potential to break the parser.</p>
1
2009-01-15T19:01:08Z
[ "python", "hash", "base64" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,065
<p>Create the lock outside the method.</p> <pre><code>class Agent(Thread): mylock = Lock() def write_result(self): self.mylock.acquire() try: ... finally: self.mylock.release() </code></pre> <p>or if using python >= 2.5:</p> <pre><code>class Agent(Thread): ...
3
2009-01-15T19:16:13Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,070
<p>The lock() method returns a lock object for every call. So every thread ( actually every call to write_result ) will have a different lock object. And there will be no locking. </p>
1
2009-01-15T19:17:25Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,074
<p>The lock that's used needs to be common to all threads, or at least ensure that two locks can't lock the same resource at the same time.</p>
1
2009-01-15T19:18:13Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,078
<p>I'm pretty sure that the lock needs to be the same object for each thread. Try this:</p> <pre><code>import time from threading import Thread, Lock def main(): lock = Lock() for i in range(20): agent = Agent(i, lock) agent.start() class Agent(Thread, Lock): def __init__(self, thread_num...
0
2009-01-15T19:18:48Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,084
<p>The lock instance should be associated with the file instance.</p> <p>In other words, you should create both the lock and file at the same time and pass both to each thread.</p>
1
2009-01-15T19:19:51Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,128
<p>For your use case one approach could be to write a <code>file</code> subclass that locks:</p> <pre><code>class LockedWrite(file): """ Wrapper class to a file object that locks writes """ def __init__(self, *args, **kwds): super(LockedWrite, self).__init__(*args, **kwds) self._lock = Lock() ...
6
2009-01-15T19:30:46Z
[ "python", "multithreading", "locking" ]
Multithreaded Resource Access - Where Do I Put My Locks?
448,034
<p>I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object.</p> <p>My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread?</p> <p>Basically, ...
5
2009-01-15T19:09:13Z
448,209
<p>You can simplify things a bit (at the cost of slightly more overhead) by designating a single thread (probably created exclusively for this purpose) as the sole thread that writes to the file, and have all other threads delegate to the file-writer by placing the string that they want to add to the file into a <code>...
1
2009-01-15T19:52:41Z
[ "python", "multithreading", "locking" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,137
<p>If it were me, I'd consider doing the work by hand. A couple thousand lines of code isn't a lot of code, and by rewriting it yourself (rather than translating it automatically), you'll be in a position to decide how to take advantage of Python idioms appropriately. (FWIW, I worked Java almost exclusively for 9 years...
9
2009-01-15T19:32:22Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,152
<p>Have a look at <a href="http://www.jython.org/Project/" rel="nofollow">Jython</a>. It can fairly seamlessly integrate Python on top of Java, and provide access to Java libraries but still let you act on them dynamically.</p>
5
2009-01-15T19:35:30Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,188
<p>Code is always better the second time you write it anyway.... Plus a few thousand lines of Java can probably be translated into a few hundred of Python.</p>
6
2009-01-15T19:47:28Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,213
<p>Automatic translators (f2c, j2py, whatever) normally emit code you wouldn't want to touch by hand. This is fine when all you need to do is use the output (for example, if you have a C compiler and no Fortran compiler, f2c allows you to compile Fortran programs), but terrible when you need to do anything to the code...
3
2009-01-15T19:53:08Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,245
<p>I would write it again by hand. I don't know of any automated tools that would generate non-disgusting looking Python, and having ported Java code to Python myself, I found the result was both higher quality than the original and considerably shorter.</p> <p>You gain quality because Python is more expressive (for e...
3
2009-01-15T19:59:15Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
448,330
<p>Jython's not what you're looking for in the final solution, but it <strong>will</strong> make the porting go much smoother.</p> <p>My approach would be:</p> <ol> <li>If there are existing tests (unit or otherwise), rewrite them in Jython (using Python's unittest)</li> <li>Write some characterization tests in Jytho...
2
2009-01-15T20:26:36Z
[ "java", "python" ]
Porting library from Java to Python
448,095
<p>I'm about to port a smallish library from Java to Python and wanted some advice (smallish ~ a few thousand lines of code). I've studied the Java code a little, and noticed some design patterns that are common in both languages. However, there were definitely some Java-only idioms (singletons, etc) present that are...
6
2009-01-15T19:23:22Z
2,746,114
<p>I've used Java2Python. It's not too bad, you still need to understand the code as it doesn't do everything correctly, but it does help.</p>
0
2010-04-30T16:32:58Z
[ "java", "python" ]
Filtering models with ReferenceProperties
448,120
<p>I'm using google app engine, and am having trouble writing querys to filter ReferenceProperties.</p> <p>eg.</p> <pre><code>class Group(db.Model): name = db.StringProperty(required=True) creator = db.ReferenceProperty(User) class GroupMember(db.Model): group = db.ReferenceProperty(Group) user = db....
5
2009-01-15T19:28:59Z
448,712
<p>This would require a join, which isn't possible in App Engine. If you want to filter by a property of another model, you need to include that property on the model you're querying against.</p>
1
2009-01-15T22:21:23Z
[ "python", "google-app-engine" ]
Filtering models with ReferenceProperties
448,120
<p>I'm using google app engine, and am having trouble writing querys to filter ReferenceProperties.</p> <p>eg.</p> <pre><code>class Group(db.Model): name = db.StringProperty(required=True) creator = db.ReferenceProperty(User) class GroupMember(db.Model): group = db.ReferenceProperty(Group) user = db....
5
2009-01-15T19:28:59Z
471,815
<p>This would result in two datastore hits but should work. If you use memcache shouldnt be a problem.</p> <pre><code>group = models.Group.all().filter("name =", group_name).get() members = models.GroupMember.all().filter('group =', group) </code></pre>
1
2009-01-23T03:35:41Z
[ "python", "google-app-engine" ]
Filtering models with ReferenceProperties
448,120
<p>I'm using google app engine, and am having trouble writing querys to filter ReferenceProperties.</p> <p>eg.</p> <pre><code>class Group(db.Model): name = db.StringProperty(required=True) creator = db.ReferenceProperty(User) class GroupMember(db.Model): group = db.ReferenceProperty(Group) user = db....
5
2009-01-15T19:28:59Z
526,271
<p>If your groups are uniquely named, then your "group.name" is a unique identifier of a Group entity.</p> <p>That means you can write:</p> <pre><code> members = models.GroupMember.all().filter( "group =",model.Group.gql("WHERE name=:1", group_name).get() ) </code></pre> <p>though you only need to do that i...
5
2009-02-08T20:04:38Z
[ "python", "google-app-engine" ]
Filtering models with ReferenceProperties
448,120
<p>I'm using google app engine, and am having trouble writing querys to filter ReferenceProperties.</p> <p>eg.</p> <pre><code>class Group(db.Model): name = db.StringProperty(required=True) creator = db.ReferenceProperty(User) class GroupMember(db.Model): group = db.ReferenceProperty(Group) user = db....
5
2009-01-15T19:28:59Z
539,147
<p>If what you want is to get the members of a group, ReferenceProperties have that built-in. </p> <pre><code>class GroupMember(db.Model): group = db.ReferenceProperty(Group, collection_name="groupMembers") user = db.ReferenceProperty(User, collection_name="groupMembers") </code></pre> <p>Then you can write:...
4
2009-02-11T22:30:31Z
[ "python", "google-app-engine" ]
Filtering models with ReferenceProperties
448,120
<p>I'm using google app engine, and am having trouble writing querys to filter ReferenceProperties.</p> <p>eg.</p> <pre><code>class Group(db.Model): name = db.StringProperty(required=True) creator = db.ReferenceProperty(User) class GroupMember(db.Model): group = db.ReferenceProperty(Group) user = db....
5
2009-01-15T19:28:59Z
2,134,473
<p>Using the Models that you defined in your question, lets say you want to list all members of a group called "<em>Space monkeys</em>".</p> <pre><code>mygroup = Group.gql("WHERE name = :1",'Space monkeys') for group_member in mygroup.groupmember_set: print 'group members name is: %s' % (group_member.user.name) <...
0
2010-01-25T18:11:30Z
[ "python", "google-app-engine" ]
Python - downloading a file over HTTP with progress bar and basic authentication
448,207
<p>I'm using <code>urllib.urlretrieve</code> to download a file, and implementing a download progress bar using the <code>reporthook</code> parameter. Since <code>urlretrieve</code> doesn't directly support authentication, I came up with </p> <pre><code>import urllib def urlretrieve_with_basic_auth(url, filename=None...
8
2009-01-15T19:52:18Z
448,277
<p><a href="http://linux.duke.edu/projects/urlgrabber/" rel="nofollow">urlgrabber</a> has built-in support for progress bars, authentication, and more.</p>
7
2009-01-15T20:12:47Z
[ "python", "http", "download" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
448,279
<p>It's a part of a package. <a href="http://docs.python.org/tutorial/modules.html#packages">Here's the documentation.</a></p> <blockquote> <p>The <code>__init__.py</code> files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as <...
554
2009-01-15T20:13:24Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
448,292
<p>It facilitates importing other python files. When you placed this file in a directory (say stuff)containing other py files, then you can do something like import stuff.other.</p> <pre><code>root\ stuff\ other.py morestuff\ another.py </code></pre> <p>Without this <code>__init__.py</code>...
15
2009-01-15T20:18:32Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
448,311
<p>The <code>__init__.py</code> file makes Python treat directories containing it as modules. </p> <p>Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.</p>
64
2009-01-15T20:22:58Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
4,116,384
<p>Files named <code>__init__.py</code> are used to mark directories on disk as Python package directories. If you have the files</p> <pre><code>mydir/spam/__init__.py mydir/spam/module.py </code></pre> <p>and <code>mydir</code> is on your path, you can import the code in <code>module.py</code> as</p> <pre><code>imp...
323
2010-11-07T03:31:14Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
18,979,314
<p>In addition to labeling a directory as a Python package and defining <code>__all__</code>, <strong><code>__init__.py</code> allows you to define any variable at the package level.</strong> Doing so is often convenient if a package defines something that will be imported frequently, in an API-like fashion. This patte...
263
2013-09-24T10:38:34Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
21,019,300
<p>In Python the definition of package is very simple. Like Java the hierarchical structure and the directory structure are the same. But you have to have <code>__init__.py</code> in a package. I will explain the <code>__init__.py</code> file with the example below:</p> <pre><code>package_x/ |-- __init__.py |-- su...
25
2014-01-09T11:45:10Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
27,757,676
<p><code>__init__.py</code> will treat the directory it is in as a loadable module.</p> <p>For people who prefer reading code, I put <a href="http://stackoverflow.com/users/2588818/two-bit-alchemist">Two-Bit Alchemist's</a> comment here.</p> <pre><code>$ find /tmp/mydir/ /tmp/mydir/ /tmp/mydir//spam /tmp/mydir//spam/...
25
2015-01-03T17:41:14Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
29,509,611
<ol> <li><p>For convenience: The other users will not need to know your functions' exact location.</p> <pre><code>your_package/ __init__.py file1.py/ file2.py/ ... fileN.py # in __init__.py from file1 import * from file2 import * ... from fileN import * # in file1.py def add(): pass </code></pre> <p...
25
2015-04-08T08:29:37Z
[ "python", "module" ]
What is __init__.py for?
448,271
<p>What is <code>__init__.py</code> for in a Python source directory?</p>
751
2009-01-15T20:09:09Z
39,991,978
<p>From Python 3.3, <code>__init__.py</code> is no longer required to define directories as importable Python packages.</p> <p>Check <a href="https://docs.python.org/3/whatsnew/3.3.html#pep-420-implicit-namespace-packages" rel="nofollow">PEP 420: Implicit Namespace Packages</a>:</p> <blockquote> <p>Native support f...
1
2016-10-12T06:36:59Z
[ "python", "module" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
448,850
<p>All you need is to have it in a directory that is set to execute CGI scripts, then you need to add </p> <pre><code>#!/usr/bin/env python </code></pre> <p>to the top, and it should execute the script as a normal python script if you link to it. (like you would a HTML or PHP file)</p> <p>This, of course, assumes yo...
2
2009-01-15T23:02:23Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
448,860
<p>You should look into <a href="http://www.cherrypy.org/" rel="nofollow">CherryPy</a> as your HTTP request handler which can be configured to run on top of a very light <a href="http://nginx.net/" rel="nofollow">NGINX</a> web server (or Apache if you prefer).</p> <p>You can then either import the other scripts into t...
1
2009-01-15T23:05:05Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
448,894
<p>If you know your way around Apache <a href="http://www.linux.com/feature/136602" rel="nofollow">this</a> should get your started.</p>
0
2009-01-15T23:13:59Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
448,899
<p>In an earlier question I asked for a minimal implementation for <a href="http://stackoverflow.com/questions/336866/how-to-implement-a-minimal-server-for-ajax-in-python">connecting an AJAX website to Python</a>. This might accomplish what you are looking for. It has a button that triggers some Python code and then sh...
0
2009-01-15T23:15:52Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
448,901
<p><a href="http://docs.python.org/library/cgi.html">This page</a> on the python site has a good description and example of what you need to do to run a python CGI script. Start out with the simplest case first. Just make a short script that prints html. </p> <pre><code>#!/usr/bin/python #on windo...
5
2009-01-15T23:16:36Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
449,062
<p>A simple cgi script (or set of scripts) is all you need to get started. The other answers have covered how to do this so I won't repeat it; instead, I will stress that <strong>using plain text will get you a long way</strong>. Just output the header (<code>print("Content-type: text/plain\n")</code> plus print adds i...
1
2009-01-16T00:05:09Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
449,199
<p>When setting this up, <em>please</em> be careful to restrict access to the scripts that take some action on your web server. It is not sufficient to place them in a directory where you just don't publish the URL, because sooner or later somebody will find them.</p> <p>At the very least, put these scripts in a locat...
0
2009-01-16T01:26:21Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
450,194
<p>This simple approach requires nothing except Python standard library. Create this directory structure:</p> <pre><code>. |-- cgi-bin | `-- script.py |-- index.html `-- server.py </code></pre> <p>You put your scripts in "cgi-bin" directory, "index.html" contains links to scripts in "cgi-bin" directory (so you don'...
3
2009-01-16T11:55:58Z
[ "python", "windows", "web-services", "cgi" ]
How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?
448,837
<p>I'm a hobbyist (and fairly new) programmer who has written several useful (to me) scripts in python to handle various system automation tasks that involve copying, renaming, and downloading files amongst other sundry activities.</p> <p>I'd like to create a web page served from one of my systems that would merely pr...
18
2009-01-15T22:58:46Z
38,508,000
<p>If you're now using Python 3, <strong><em>print</em></strong> is a function requiring parenthesis.</p> <pre><code>#!/Python34/python print ("Content-type: text/html\n") print ("&lt;html&gt;&lt;head&gt;") print ("&lt;/head&gt;&lt;body&gt;") print ("Hello World from Python Script.") print ("&lt;/body&gt;&lt;/html&gt;...
0
2016-07-21T15:12:54Z
[ "python", "windows", "web-services", "cgi" ]
Setting up Python on Windows/ Apache?
449,055
<p>I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed <code>mod_python</code> and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to ...
6
2009-01-16T00:02:42Z
449,120
<p>You do not NEED mod_python to run Python code on the web, you could use simple CGI programming to run your python code, with the instructions in the following link: <a href="http://www.imladris.com/Scripts/PythonForWindows.html" rel="nofollow">http://www.imladris.com/Scripts/PythonForWindows.html</a></p> <p>That sh...
3
2009-01-16T00:38:14Z
[ "python", "wamp", "mod-python" ]
Setting up Python on Windows/ Apache?
449,055
<p>I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed <code>mod_python</code> and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to ...
6
2009-01-16T00:02:42Z
449,166
<blockquote> <p>AddHandler mod_python .py</p> </blockquote> <p>Have you set 'PythonHandler'?</p> <p>These days, consider using WSGI instead of native mod-python interfaces for more wide-ranging deployment options. Either through mod-python's WSGI support, or, maybe better, mod-wsgi. (CGI via eg. wsgiref will also w...
0
2009-01-16T01:11:41Z
[ "python", "wamp", "mod-python" ]
Setting up Python on Windows/ Apache?
449,055
<p>I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed <code>mod_python</code> and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to ...
6
2009-01-16T00:02:42Z
450,097
<p>Stay away from <code>mod_python</code>. One common misleading idea is that <code>mod_python</code> is like <code>mod_php</code>, but for python. That is not true. <a href="http://wsgi.org">Wsgi</a> is the standard to run python web applications, defined by <a href="http://www.python.org/dev/peps/pep-0333/">PEP 333</...
22
2009-01-16T11:08:32Z
[ "python", "wamp", "mod-python" ]
Are there any good 3rd party GUI products for Python?
449,168
<p>In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.</p> <p>Edit 01-16-09: For Example:</p> <p><a href="http://www.devexpress.com/Downloads...
5
2009-01-16T01:11:49Z
449,215
<p>Heaps of GUI apis are available. PyQT, PtGTK, Tkinter... </p> <p>-T</p>
0
2009-01-16T01:36:06Z
[ "python", "user-interface" ]
Are there any good 3rd party GUI products for Python?
449,168
<p>In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.</p> <p>Edit 01-16-09: For Example:</p> <p><a href="http://www.devexpress.com/Downloads...
5
2009-01-16T01:11:49Z
449,235
<p>The popular Python GUI toolkits are usually wrappers around external (usually C, C++) libraries. So whatever 3rd party products those external libraries have, Python code can benefit (by minimal manual wrapping even in case the 3rd party solution does not provide Python wrappers by default).</p>
1
2009-01-16T01:41:21Z
[ "python", "user-interface" ]
Are there any good 3rd party GUI products for Python?
449,168
<p>In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.</p> <p>Edit 01-16-09: For Example:</p> <p><a href="http://www.devexpress.com/Downloads...
5
2009-01-16T01:11:49Z
449,252
<p>There are a number of GUI Toolkits available for Python. Obviously, the toolkit you choose will determine your selection of 3rd party widgets. </p> <p><strong>The Contenders</strong> </p> <p>Python comes with <a href="http://wiki.python.org/moin/TkInter" rel="nofollow">Tkinter</a> which is easy to use, but not g...
8
2009-01-16T01:49:55Z
[ "python", "user-interface" ]
Are there any good 3rd party GUI products for Python?
449,168
<p>In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.</p> <p>Edit 01-16-09: For Example:</p> <p><a href="http://www.devexpress.com/Downloads...
5
2009-01-16T01:11:49Z
449,258
<p><a href="http://www.wxpython.org/" rel="nofollow">wxPython</a> is a wrapper around <a href="http://wxwidgets.org/" rel="nofollow">wxWidgets</a>, LGPL (approximately) and very comprehensive<br /> <a href="http://www.riverbankcomputing.co.uk/software/pyqt/intro" rel="nofollow">pyQT</a> is a similair wrapper around <a ...
1
2009-01-16T01:52:12Z
[ "python", "user-interface" ]
Are there any good 3rd party GUI products for Python?
449,168
<p>In .Net you have companies like DevEpxress, and Infragistics that offer a range of GUI widgets. Is there any market like that for Python GUI widgets? I'm thinking specifically about widgets like the DevExpress xtraGrid Suite.</p> <p>Edit 01-16-09: For Example:</p> <p><a href="http://www.devexpress.com/Downloads...
5
2009-01-16T01:11:49Z
449,261
<p>There's also <a href="http://wxpython.org/" rel="nofollow">wxPython</a>, which is a wrapper around <a href="http://www.wxwidgets.org/" rel="nofollow">wxWidgets</a> which is quite popular, works on many platforms and liberally licensed.</p> <p>QT would probably be the other one I would look at, especially now that t...
0
2009-01-16T01:52:42Z
[ "python", "user-interface" ]