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 to find the mime type of a file in python?
43,580
<p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p> <p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h...
101
2008-09-04T12:07:27Z
39,356,849
<p>In Python 3.x and webapp with url to the file which couldn't have an extension or a fake extension. You should install python-magic, using </p> <pre><code>pip3 install python-magic </code></pre> <p>For Mac OS X, you should also install libmagic using</p> <pre><code>brew install libmagic </code></pre> <p>Code sni...
2
2016-09-06T19:55:29Z
[ "python", "mime" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
43,736
<p>If you've never done any CGI programming before I think it would be worth doing one project - perhaps just a sample play site just for yourself - using the DIY approach. You'll learn a lot more about how all the various parts work than you would by using a framework. This will help in you design and debug and so o...
2
2008-09-04T13:17:38Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
43,753
<p>If you decide to go with a framework that is WSGI-based (for instance <a href="http://www.turbogears.org/2.0" rel="nofollow">TurboGears</a>), I would recommend you go through the excellent article <a href="http://pythonpaste.org/webob/do-it-yourself.html" rel="nofollow">Another Do-It-Yourself Framework</a> by Ian Bi...
7
2008-09-04T13:24:55Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
43,773
<p>CGI is great for low-traffic websites, but it has some performance problems for anything else. This is because every time a request comes in, the server starts the CGI application in its own process. This is bad for two reasons: 1) Starting and stopping a process can take time and 2) you can't cache anything in ...
17
2008-09-04T13:35:45Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
43,835
<p>The simplest web program is a CGI script, which is basically just a program whose standard output is redirected to the web browser making the request. In this approach, every page has its own executable file, which must be loaded and parsed on every request. This makes it really simple to get something up and runn...
12
2008-09-04T14:11:54Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
44,638
<p>OK, rails is actually pretty good, but there is just a little bit too much magic going on in there (from the Ruby world I would much prefer merb to rails). I personally use Pylons, and am pretty darn happy. I'd say (compared to django), that pylons allows you to interchange ints internal parts easier than django doe...
1
2008-09-04T19:55:29Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
87,048
<p>If you want to go big, choose Django and you are set. But if you want just to learn, roll your own framework using already mentioned <a href="http://pythonpaste.org/webob/" rel="nofollow">WebOb</a> - this can be really fun and I am sure you'll learn much more (plus you can use components you like: template system, u...
4
2008-09-17T20:04:12Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
222,159
<p>It's always worth doing something the hard way - once - as a learning exercise. Once you understand how it works, pick a framework that suits your application, and use that. You don't need to reinvent the wheel once you understand angular velocity. :-)</p> <p>It's also worth making sure that you have a fairly robus...
3
2008-10-21T14:49:57Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
1,209,580
<p>If you are using Python you should <em>not</em> start with CGI, instead start with WSGI (and you can use <a href="http://docs.python.org/library/wsgiref.html#wsgiref.handlers.CGIHandler" rel="nofollow">wsgiref.handlers.CGIHandler</a> to run your WSGI script as a CGI script. The result is something that is basically...
2
2009-07-30T21:44:35Z
[ "python", "frameworks", "cgi", "wsgi" ]
Pros and Cons of different approaches to web programming in Python
43,709
<p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p> <p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr...
24
2008-09-04T13:00:13Z
1,873,745
<p>For smaller projects, rolling your own is fairly easy. Especially as you can simply import a templating engine like <a href="http://genshi.edgewall.org/" rel="nofollow">Genshi</a> and get alot happening quite quickly and easily. Sometimes it's just quicker to use a screwdriver than to go looking for the power drill....
1
2009-12-09T13:02:23Z
[ "python", "frameworks", "cgi", "wsgi" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,780
<p>Your Python interpreter is correct. One (stupid) way of calculating a modulus is to subtract or add the modulus until the resulting value is between 0 and (modulus − 1).</p> <p>e.g.: 13 mod 5 = (13 − 5) mod 5 = (13 − 10) mod 5 = 3</p> <p>or in your case: −2 mod 5 = (−2 + 5) mod 5 = 3</p>
9
2008-09-04T13:40:59Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,781
<p>Well, -2 divided by 5 would be 0 with a remainder of 3. I don't believe that should be very platform dependent, but I've seen stranger things.</p>
0
2008-09-04T13:41:04Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,782
<p>It is indeed 3. In <a href="http://en.wikipedia.org/wiki/Modular_arithmetic" rel="nofollow">modular arithmetic</a>, a modulus is simply the remainder of a division, and the remainder of -2 divided by 5 is 3.</p>
0
2008-09-04T13:41:19Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,783
<p>The result of the modulus operation on negatives seems to be programming language dependent and here is a listing <a href="http://en.wikipedia.org/wiki/Modulo_operation">http://en.wikipedia.org/wiki/Modulo_operation</a></p>
11
2008-09-04T13:41:25Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,785
<p>Well, 0 % 5 should be 0, right?</p> <p>-1 % 5 should be 4 because that's the next allowed digit going in the reverse direction (i.e., it can't be 5, since that's out of range).</p> <p>And following along by that logic, -2 must be 3.</p> <p>The easiest way to think of how it will work is that you keep adding or su...
4
2008-09-04T13:41:40Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,794
<p>By the way: most programming languages would disagree with Python and give the result <code>-2</code>. Depending on the interpretation of modulus this is correct. However, the most agreed-upon mathematical definition states that the modulus of <em>a</em> and <em>b</em> is the (strictly positive) rest <em>r</em> of t...
15
2008-09-04T13:46:23Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,799
<p>The result depends on the language. Python returns the sign of the divisor, where for example c# returns the sign of the dividend (ie. -2 % 5 returns -2 in c#).</p>
0
2008-09-04T13:53:07Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,837
<p>One explanation might be that negative numbers are stored using <a href="http://en.wikipedia.org/wiki/Two%27s_complement" rel="nofollow">2's complement</a>. When the python interpreter tries to do the modulo operation it converts to unsigned value. As such instead of doing (-2) % 5 it actually computes 0xFFFF_FFFF_F...
0
2008-09-04T14:12:16Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,863
<p>As explained in other answers, there are many choices for a modulo operation with negative values. In general different languages (and different machine architectures) will give a different result.</p> <p>According to the <a href="http://docs.python.org/ref/binary.html" rel="nofollow">Python reference manual</a>,</...
4
2008-09-04T14:25:05Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
43,916
<p>Be careful not to rely on this mod behavior in C/C++ on all OSes and architectures. If I recall correctly, I tried to rely on C/C++ code like</p> <pre><code>float x2 = x % n; </code></pre> <p>to keep x2 in the range from 0 to n-1 but negative numbers crept in when I would compile on one OS, but things would work f...
0
2008-09-04T14:46:11Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
3,224,825
<p>Like the documentation says in <a href="http://docs.python.org/reference/expressions.html#binary">Binary arithmetic operations</a>, Python assures that:</p> <blockquote> <p>The integer division and modulo operators are connected by the following identity: <code>x == (x/y)*y + (x%y)</code>. Integer division and mo...
5
2010-07-11T22:18:00Z
[ "python", "math", "modulo" ]
Modulus operation with negatives values - weird thing?
43,775
<p>Can you please tell me how much is <code>(-2) % 5</code>? According to my Python interpreter is 3, but do you have a wise explanation for this?</p> <p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
14
2008-09-04T13:36:46Z
5,203,460
<p>There seems to be a common confusion between the terms "modulo" and "remainder".</p> <p>In math, a remainder should <em>always</em> be defined consistent with the quotient, so that if <code>a / b == c rem d</code> then <code>(c * b) + d == a</code>. Depending on how you round your quotient, you get different remain...
0
2011-03-05T11:16:46Z
[ "python", "math", "modulo" ]
How do I document a module in Python?
44,084
<p>That's it. If you want to document a function or a class, you put a string just after the definition. For instance:</p> <pre><code>def foo(): """This function does nothing.""" pass </code></pre> <p>But what about a module? How can I document what a <em>file.py</em> does?</p>
31
2008-09-04T16:06:48Z
44,094
<p>It's easy, you just add a docstring at the top of the module.</p>
5
2008-09-04T16:12:07Z
[ "python", "documentation", "python-module" ]
How do I document a module in Python?
44,084
<p>That's it. If you want to document a function or a class, you put a string just after the definition. For instance:</p> <pre><code>def foo(): """This function does nothing.""" pass </code></pre> <p>But what about a module? How can I document what a <em>file.py</em> does?</p>
31
2008-09-04T16:06:48Z
44,095
<p>For the packages, you can document it in <code>__init__.py</code>. For the modules, you can add a docstring simply in the module file.</p> <p>All the information is here: <a href="http://www.python.org/dev/peps/pep-0257/">http://www.python.org/dev/peps/pep-0257/</a></p>
33
2008-09-04T16:12:23Z
[ "python", "documentation", "python-module" ]
How do I document a module in Python?
44,084
<p>That's it. If you want to document a function or a class, you put a string just after the definition. For instance:</p> <pre><code>def foo(): """This function does nothing.""" pass </code></pre> <p>But what about a module? How can I document what a <em>file.py</em> does?</p>
31
2008-09-04T16:06:48Z
44,098
<p>You do it the exact same way. Put a string in as the first statement in the module.</p>
6
2008-09-04T16:12:52Z
[ "python", "documentation", "python-module" ]
How do I document a module in Python?
44,084
<p>That's it. If you want to document a function or a class, you put a string just after the definition. For instance:</p> <pre><code>def foo(): """This function does nothing.""" pass </code></pre> <p>But what about a module? How can I document what a <em>file.py</em> does?</p>
31
2008-09-04T16:06:48Z
23,450,896
<p>Add your docstring as the <a href="http://legacy.python.org/dev/peps/pep-0257/#what-is-a-docstring">first statement in the module</a>.</p> <p>Since I like seeing an example:</p> <pre><code>""" Your module's verbose yet thorough docstring. """ import foo # ... </code></pre>
14
2014-05-03T23:29:51Z
[ "python", "documentation", "python-module" ]
Iterate over subclasses of a given class in a given module
44,352
<p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>
15
2008-09-04T18:05:23Z
44,381
<p>Here's one way to do it:</p> <pre><code>import inspect def get_subclasses(mod, cls): """Yield the classes in module ``mod`` that inherit from ``cls``""" for name, obj in inspect.getmembers(mod): if hasattr(obj, "__bases__") and cls in obj.__bases__: yield obj </code></pre>
9
2008-09-04T18:20:21Z
[ "python", "oop" ]
Iterate over subclasses of a given class in a given module
44,352
<p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>
15
2008-09-04T18:05:23Z
44,403
<p>Given the module foo.py</p> <pre><code>class foo(object): pass class bar(foo): pass class baz(foo): pass class grar(Exception): pass def find_subclasses(module, clazz): for name in dir(module): o = getattr(module, name) try: if issubclass(o, clazz): yield name, o ...
1
2008-09-04T18:29:59Z
[ "python", "oop" ]
Iterate over subclasses of a given class in a given module
44,352
<p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>
15
2008-09-04T18:05:23Z
47,032
<p>Can I suggest that neither of the answers from Chris AtLee and zacherates fulfill the requirements? I think this modification to zacerates answer is better:</p> <pre><code>def find_subclasses(module, clazz): for name in dir(module): o = getattr(module, name) try: if (o != clazz) and issubclass(o...
4
2008-09-05T22:54:42Z
[ "python", "oop" ]
Iterate over subclasses of a given class in a given module
44,352
<p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>
15
2008-09-04T18:05:23Z
408,465
<p>Although Quamrana's suggestion works fine, there are a couple of possible improvements I'd like to suggest to make it more pythonic. They rely on using the inspect module from the standard library.</p> <ol> <li>You can avoid the getattr call by using <code>inspect.getmembers()</code></li> <li>The try/catch can be a...
19
2009-01-03T01:56:21Z
[ "python", "oop" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
44,781
<pre><code>myList = ['a','b','c','d'] myString = ",".join(myList ) </code></pre> <p>This won't work if the list contains numbers.</p> <hr> <p>As <a href="http://stackoverflow.com/users/3399/ricardo-reyes">Ricardo Reyes</a> suggested, if it contains non-string types (such as integers, floats, bools, None) then do:</p...
333
2008-09-04T21:06:12Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
44,788
<p>Why the map/lambda magic? Doesn't this work?</p> <pre><code>&gt;&gt;&gt;foo = [ 'a', 'b', 'c' ] &gt;&gt;&gt;print ",".join(foo) a,b,c &gt;&gt;&gt;print ",".join([]) &gt;&gt;&gt;print ",".join(['a']) a </code></pre> <p>Edit: @mark-biek points out the case for numbers. Perhaps the list comprehension:</p> <pre><cod...
42
2008-09-04T21:08:29Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
44,791
<p>Don't you just want:</p> <pre><code>",".join(l) </code></pre> <p>Obviously it gets more complicated if you need to quote/escape commas etc in the values. In that case I would suggest looking at the csv module in the standard library:</p> <p><a href="https://docs.python.org/library/csv.html" rel="nofollow">https:/...
10
2008-09-04T21:09:33Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
44,794
<p>Unless I'm missing something, <code>','.join(foo)</code> should do what you're asking for.</p> <pre><code>&gt;&gt;&gt; ','.join(['']) '' &gt;&gt;&gt; ','.join(['s']) 's' &gt;&gt;&gt; ','.join(['a','b','c']) 'a,b,c' </code></pre> <p>(edit: and as jmanning2k points out, </p> <pre><code>','.join([str(x) for x in fo...
2
2008-09-04T21:10:25Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
44,878
<p>@jmanning2k using a list comprehension has the downside of creating a new temporary list. The better solution would be using itertools.imap which returns an iterator</p> <pre><code>from itertools import imap l = [1, "foo", 4 ,"bar"] ",".join(imap(str, l)) </code></pre>
5
2008-09-04T21:57:51Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
46,233
<p>@<a href="#44878">Peter Hoffmann</a></p> <p>Using generator expressions has the benefit of also producing an iterator but saves importing itertools. Furthermore, list comprehensions are generally preferred to map, thus, I'd expect generator expressions to be preferred to imap.</p> <pre><code>&gt;&gt;&gt; l = [1, "...
6
2008-09-05T16:29:25Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
65,255
<pre><code>l=['a', 1, 'b', 2] print str(l)[1:-1] Output: "'a', 1, 'b', 2" </code></pre>
2
2008-09-15T18:08:41Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
156,851
<p>Here is a alternative solution in Python 3.0 which allows non-string list items:</p> <pre><code>&gt;&gt;&gt; alist = ['a', 1, (2, 'b')] </code></pre> <ul> <li><p>a standard way</p> <pre><code>&gt;&gt;&gt; ", ".join(map(str, alist)) "a, 1, (2, 'b')" </code></pre></li> <li><p>the alternative solution</p> <pre><cod...
5
2008-10-01T09:23:00Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
12,297,685
<p>for converting list containing numbers do the following:</p> <pre><code>string = ''.join([str(i) for i in list]) </code></pre>
4
2012-09-06T10:08:13Z
[ "python", "list" ]
How would you make a comma-separated string from a list?
44,778
<p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and...
180
2008-09-04T21:04:04Z
35,319,592
<p><code>",".join(l)</code> will not work for all cases. I'd suggest using the csv module with StringIO</p> <pre><code>import StringIO import csv l = ['list','of','["""crazy"quotes"and\'',123,'other things'] line = StringIO.StringIO() writer = csv.writer(line) writer.writerow(l) csvcontent = line.getvalue() # 'list,...
4
2016-02-10T15:43:06Z
[ "python", "list" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
44,842
<p>It's a list of public objects of that module. It overrides the default of hiding everything that begins with an underscore.</p>
198
2008-09-04T21:30:46Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
44,843
<p>From <a href="http://effbot.org/pyref/__all__.htm">(An Unofficial) Python Reference Wiki</a>:</p> <blockquote> <p>The public names defined by a module are determined by checking the module's namespace for a variable named <code>__all__</code>; if defined, it must be a sequence of strings which are names defined o...
43
2008-09-04T21:31:16Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
64,130
<p>Linked to, but not explicitly mentioned here, is exactly when <code>__all__</code> is used. It is a list of strings defining what symbols in a module will be exported when <code>from &lt;module&gt; import *</code> is used on the module.</p> <p>For example, the following code in a <code>foo.py</code> explicitly expo...
483
2008-09-15T15:49:50Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
2,838,800
<p>It also changes what pydoc will show:</p> <p>module1.py</p> <pre><code>a = "A" b = "B" c = "C" </code></pre> <p>module2.py</p> <pre><code>__all__ = ['a', 'b'] a = "A" b = "B" c = "C" </code></pre> <p>$ pydoc module1</p> <pre> Help on module module1: <b>NAME</b> module1 <b>FILE</b> module1.py <b>DAT...
76
2010-05-15T03:22:29Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
16,595,377
<p>I'm just adding this to be precise:</p> <p>All other answers refer to <em>modules</em>. The original question explicitely mentioned <code>__all__</code> in <code>__init__.py</code> files, so this is about python <em>packages</em>.</p> <p>Generally, <code>__all__</code> only comes into play when the <code>from xxx ...
84
2013-05-16T19:01:48Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
35,710,527
<blockquote> <p><strong>Explain __all__ in Python?</strong></p> <p>I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files.</p> <p>What does this do?</p> </blockquote> <h1>What does <code>__all__</code> do?</h1> <p>It declares the semantically "public" names from a ...
22
2016-02-29T21:58:50Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
36,119,040
<h1><code>__all__</code> customizes <strong>the asterisk</strong> in <a href="https://docs.python.org/2/tutorial/modules.html#importing-from-a-package" rel="nofollow"><code>from &lt;module&gt; import *</code></a></h1> <hr> <pre><code>""" cheese.py """ __all__ = ['swiss', 'cheddar'] swiss = 4.99 cheddar = 3.99 gouda...
4
2016-03-20T20:20:43Z
[ "python", "syntax", "namespaces" ]
Can someone explain __all__ in Python?
44,834
<p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
409
2008-09-04T21:28:18Z
36,853,901
<p><code>__all__</code> is used to document the public API of a Python module. Here is the relevant excerpt from <a href="https://docs.python.org/3/reference/simple_stmts.html#the-import-statement" rel="nofollow">the Python language reference</a>:</p> <blockquote> <p>The public names defined by a module are determin...
1
2016-04-26T01:39:25Z
[ "python", "syntax", "namespaces" ]
Can the HTTP version or headers affect the visual appearance of a web page?
45,013
<p>I know, I would have thought the answer was obviously "no" as well, but I am experiencing a strange situation where when I view my site from our staging server it appears slightly larger than when I view it from my local dev server. I have used Charles to confirm that all of the content -- the HTML, the images, the...
1
2008-09-05T00:10:37Z
45,016
<p>Have you tried View -> Zoom -> Reset on both sites?</p>
8
2008-09-05T00:12:41Z
[ "python", "django", "firefox" ]
Python packages - import by class, not file
45,122
<p>Say I have the following file structure:</p> <pre><code>app/ app.py controllers/ __init__.py project.py plugin.py </code></pre> <p>If app/controllers/project.py defines a class Project, app.py would import it like this:</p> <pre><code>from app.controllers.project import Project </code></pre> <p>I...
35
2008-09-05T02:15:33Z
45,126
<p>You need to put</p> <pre><code>from project import Project </code></pre> <p>in <code>controllers/__init__.py</code>.</p> <p>Note that when <a href="http://www.python.org/dev/peps/pep-0328/" rel="nofollow">Absolute imports</a> become the default (Python 2.7?), you will want to add a dot before the module name (to ...
71
2008-09-05T02:18:21Z
[ "python", "packages" ]
Where can I find the time and space complexity of the built-in sequence types in Python
45,228
<p>I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?</p>
14
2008-09-05T04:27:46Z
45,245
<p>If your asking what I think your asking, you can find them <a href="http://books.google.com/books?id=vpTAq4dnmuAC&amp;pg=RA1-PA476&amp;lpg=RA1-PA476&amp;dq=Big-O+of+Python+data+structures&amp;source=web&amp;ots=AOM5B5D7yA&amp;sig=deLGb6VrftTpnNYN6fILVbiirXM&amp;hl=en&amp;sa=X&amp;oi=book_result&amp;resnum=7&amp;ct=r...
2
2008-09-05T04:52:09Z
[ "python", "performance", "complexity-theory", "big-o", "sequences" ]
Where can I find the time and space complexity of the built-in sequence types in Python
45,228
<p>I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?</p>
14
2008-09-05T04:27:46Z
45,538
<p>Raymond D. Hettinger does <a href="http://www.youtube.com/watch?v=hYUsssClE94" rel="nofollow">an excellent talk</a> (<a href="http://wenku.baidu.com/view/9c6fb20dcc1755270722089d.html" rel="nofollow">slides</a>) about Python's built-in collections called 'Core Python Containers - Under the Hood'. The version I saw f...
12
2008-09-05T11:04:04Z
[ "python", "performance", "complexity-theory", "big-o", "sequences" ]
Where can I find the time and space complexity of the built-in sequence types in Python
45,228
<p>I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?</p>
14
2008-09-05T04:27:46Z
46,201
<p>Checkout the <a href="http://wiki.python.org/moin/TimeComplexity">TimeComplexity</a> page on the py dot org wiki. It covers set/dicts/lists/etc at least as far as time complexity goes.</p>
16
2008-09-05T16:19:03Z
[ "python", "performance", "complexity-theory", "big-o", "sequences" ]
Wacom tablet Python interface
45,500
<p>If possible I want to catch pressure sensitive input from a Wacom tablet in Python. Are there any Python libraries available that can do this?</p>
7
2008-09-05T10:13:16Z
45,564
<p>You could perhaps take a look at the <a href="http://www.alexmac.cc/tablet-apps/tablet-apps-0.3.1.tar.bz2" rel="nofollow">software</a> described <a href="http://www.alexmac.cc/tablet-apps/" rel="nofollow">here</a>. It is a gnome applet, written in Python.</p> <p>From the web site:</p> <p>"The gnome wacom applet is...
3
2008-09-05T11:21:35Z
[ "python", "interface", "wacom" ]
Wacom tablet Python interface
45,500
<p>If possible I want to catch pressure sensitive input from a Wacom tablet in Python. Are there any Python libraries available that can do this?</p>
7
2008-09-05T10:13:16Z
5,088,138
<p>Use PySide (wrapper for QT)'s QTabletEvent: <a href="http://www.pyside.org/docs/pyside/PySide/QtGui/QTabletEvent.html#PySide.QtGui.QTabletEvent" rel="nofollow">http://www.pyside.org/docs/pyside/PySide/QtGui/QTabletEvent.html#PySide.QtGui.QTabletEvent</a></p>
2
2011-02-23T07:36:18Z
[ "python", "interface", "wacom" ]
Wacom tablet Python interface
45,500
<p>If possible I want to catch pressure sensitive input from a Wacom tablet in Python. Are there any Python libraries available that can do this?</p>
7
2008-09-05T10:13:16Z
10,476,121
<p>For Mac OS X:</p> <p><a href="https://bitbucket.org/AnomalousUnderdog/pythonmactabletlib" rel="nofollow">https://bitbucket.org/AnomalousUnderdog/pythonmactabletlib</a></p> <blockquote> <p>A small Python library to allow Python scripts to access pen tablet input data in Mac OS X.</p> <p>The library exists ...
2
2012-05-07T02:47:32Z
[ "python", "interface", "wacom" ]
Wacom tablet Python interface
45,500
<p>If possible I want to catch pressure sensitive input from a Wacom tablet in Python. Are there any Python libraries available that can do this?</p>
7
2008-09-05T10:13:16Z
28,655,874
<p>Pressure data is available in <a href="http://python-gtk-3-tutorial.readthedocs.org/en/latest/" rel="nofollow">PyGObject to access Gtk+ 3</a> on multiple platforms, though "Windows users may still want to keep using PyGTK until more convenient installers are published." [<a href="https://wiki.python.org/moin/PyGtk" ...
1
2015-02-22T08:15:26Z
[ "python", "interface", "wacom" ]
Is there a Python library for generating .ico files?
45,507
<p>I'm looking to create <code>favicon.ico</code> files programatically from Python, but PIL only has support for reading <code>ico</code> files.</p>
11
2008-09-05T10:26:47Z
45,519
<p>Perhaps the following would work:</p> <ul> <li>Generate your icon image using PIL</li> <li>Convert the image to .ico format using the python interface to ImageMagick, <a href="http://www.imagemagick.org/download/python/">PythonMagick</a></li> </ul> <p>I have not tried this approach. The ImageMagick convert command...
6
2008-09-05T10:36:58Z
[ "python", "favicon" ]
Is there a Python library for generating .ico files?
45,507
<p>I'm looking to create <code>favicon.ico</code> files programatically from Python, but PIL only has support for reading <code>ico</code> files.</p>
11
2008-09-05T10:26:47Z
45,520
<p>I don't know if this applies for all cases, but on WinXP an .ico can be a bmp of size 16x16, 32x32 or 64x64. Just change the extension to ico from bmp and you're ready to go.</p>
2
2008-09-05T10:38:00Z
[ "python", "favicon" ]
Is there a Python library for generating .ico files?
45,507
<p>I'm looking to create <code>favicon.ico</code> files programatically from Python, but PIL only has support for reading <code>ico</code> files.</p>
11
2008-09-05T10:26:47Z
45,832
<p>According to <a href="http://en.wikipedia.org/wiki/Favicon">Wikipedia</a> modern browsers can handle favicons in PNG format, so maybe you could just generate that?</p> <p>Alternatively the <a href="http://en.wikipedia.org/wiki/ICO_(icon_image_file_format)">ICO article</a> describes the format...</p>
6
2008-09-05T13:35:32Z
[ "python", "favicon" ]
Is there a Python library for generating .ico files?
45,507
<p>I'm looking to create <code>favicon.ico</code> files programatically from Python, but PIL only has support for reading <code>ico</code> files.</p>
11
2008-09-05T10:26:47Z
36,168,447
<p>You can use <a href="http://pillow.readthedocs.org" rel="nofollow">Pillow</a>:</p> <pre><code>from PIL import Image filename = r'logo.png' img = Image.open(filename) img.save('logo.ico') </code></pre> <p>Optionally, you may specify the icon sizes you want:</p> <pre><code>icon_sizes = [(16,16), (32, 32), (48, 48),...
4
2016-03-23T01:49:37Z
[ "python", "favicon" ]
How to know whether a window with a given title is already open in Tk?
45,540
<p>I’ve writen a little python script that just pops up a message box containing the text passed on the command line. I want to pop it up only when the window —resulting from a previous call— is not open.</p> <pre><code>from Tkinter import * import tkMessageBox root = Tk() root.withdraw() # TODO not if a wind...
5
2008-09-05T11:07:06Z
46,205
<p>I believe you want:</p> <pre><code>if 'normal' != root.state(): tkMessageBox.showinfo("Key you!", " ".join(sys.argv[1:])) </code></pre>
1
2008-09-05T16:19:35Z
[ "python", "tkinter", "tk" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
46,028
<p>I don't know of any, but if all else fails you could use <a href="http://docs.python.org/lib/module-ctypes.html" rel="nofollow">ctypes</a> to directly use libdwarf, libelf or libbfd.</p>
3
2008-09-05T15:23:13Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
510,623
<p>You should give <a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a> a try. It is very useful to parse binary data into python objects. </p> <p>There is even an example for the <a href="http://sebulbasvn.googlecode.com/svn/trunk/construct/formats/executable/elf32.py" rel="nofollow">ELF32</a> file...
4
2009-02-04T09:19:06Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
1,352,742
<p>I've been developing a DWARF parser using <a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a>. Currently fairly rough, and parsing is slow. But I thought I should at least let you know. It may suit your needs, with a bit of work.</p> <p>I've got the code in Mercurial, hosted at bitbucket:</p> <...
3
2009-08-30T00:48:04Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
3,023,558
<p><a href="http://bitbucket.org/haypo/hachoir/wiki/Home" rel="nofollow">hachior</a> is another library for parsing binary data</p>
2
2010-06-11T14:23:05Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
3,647,010
<p>You might be interested in the DWARF library from <a href="http://code.google.com/p/pydevtools/" rel="nofollow">pydevtools</a>:</p> <pre><code>&gt;&gt;&gt; from bintools.dwarf import DWARF &gt;&gt;&gt; dwarf = DWARF('test/test') &gt;&gt;&gt; dwarf.get_loc_by_addr(0x8048475) ('/home/emilmont/Workspace/dbg/test/main....
8
2010-09-05T17:29:19Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Analizing MIPS binaries: is there a Python library for parsing binary data?
45,954
<p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu...
10
2008-09-05T14:44:25Z
8,754,578
<p>Please check <a href="https://github.com/eliben/pyelftools" rel="nofollow">pyelftools</a> - a new pure Python library meant to do this.</p>
4
2012-01-06T07:11:55Z
[ "python", "x86", "mips", "elf", "dwarf" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
47,201
<p>I've not seen a system with less than 2.3 installed for some time. Mostly 2.4+ is installed by default for most OS I use now. 2.3 is just on an older Solaris machine. Linux distros tend to have 2.4+, as does OS X.</p> <p>IIRC, 2.4 has a lot of the features 2.5 does, but usable only with</p> <pre><code>from __fu...
4
2008-09-06T02:27:45Z
[ "python", "compatibility" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
47,208
<p>As python is in kind of an transition phase towards python 3 with breaking backward compatibility I don't think it is a good idea to go python 3 only. Based on the <a href="http://www.python.org/dev/peps/pep-3000/#timeline" rel="nofollow">time line</a> there will be at least one or two following releases of the 2.x ...
6
2008-09-06T02:44:31Z
[ "python", "compatibility" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
47,264
<p>You can use different versions of python on each machine. </p> <p>Coding something new, I would not use anything less than python2.5. You can do <code>apt-get install python2.5</code> on stock debian stable. </p> <p>For windows, don't really worry about it. It's very easy to install the python2.5 msi. </p> <p>If ...
1
2008-09-06T05:25:47Z
[ "python", "compatibility" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
48,175
<p>Python 2.3, or 2.2 if you can live without the many modules that were added (e.g. datetime, csv, logging, optparse, zipimport), aren't using SSL, and are willing to add boilerplate for True/False.</p> <p>2.4 added decorators. generator expressions, reversed(), sorted(), and the subprocess and decimal modules. Alth...
1
2008-09-07T05:21:11Z
[ "python", "compatibility" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
2,036,609
<p>If the project is going to be mainstream and will be run on Linux the <strong>only</strong> sensible choise is 2.4 - just because it is a pain to get anything else installed as default on Enterprise Linuxes.</p> <p>In any case, any <em>modern</em> OS will/can have 2.4 or newer.</p>
0
2010-01-10T09:36:52Z
[ "python", "compatibility" ]
Which Version of Python to Use for Maximum Compatibility
47,198
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
14
2008-09-06T02:22:50Z
7,695,227
<p>You should use <strong>Python 2.7</strong>, the final major version of Python 2.</p> <p>Python 3.x currently has limited 3rd-party library support, and is often not installed by default. So you are looking at the 2.x series.</p> <p>Python 2.7 is essentially fully backwards-compatible with earlier 2.xs. In addition...
0
2011-10-08T06:40:31Z
[ "python", "compatibility" ]
Django: Print url of view without hardcoding the url
47,207
<p>Can i print out a url <code>/admin/manage/products/add</code> of a certain view in a template?</p> <p>Here is the rule i want to create a link for</p> <pre><code>(r'^manage/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}), </code></pre> <p>I would like to have /manage/products/add in ...
7
2008-09-06T02:42:49Z
47,212
<p>If you use <a href="https://docs.djangoproject.com/en/1.2/topics/http/urls/#naming-url-patterns" rel="nofollow">named url patterns</a> you can do the follwing in your template</p> <pre><code>{% url create_object %} </code></pre>
2
2008-09-06T02:49:38Z
[ "python", "django" ]
Django: Print url of view without hardcoding the url
47,207
<p>Can i print out a url <code>/admin/manage/products/add</code> of a certain view in a template?</p> <p>Here is the rule i want to create a link for</p> <pre><code>(r'^manage/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}), </code></pre> <p>I would like to have /manage/products/add in ...
7
2008-09-06T02:42:49Z
47,213
<p>The preferred way of creating the URL is by adding a <code>get_absolute_url</code> method to your model classes. You can hardcode the path there so you at least get closer to following the KISS philosophy.</p> <p>You can go further by utilizing the <code>permalink</code> decorator that figures the path based on the...
0
2008-09-06T02:50:08Z
[ "python", "django" ]
Django: Print url of view without hardcoding the url
47,207
<p>Can i print out a url <code>/admin/manage/products/add</code> of a certain view in a template?</p> <p>Here is the rule i want to create a link for</p> <pre><code>(r'^manage/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}), </code></pre> <p>I would like to have /manage/products/add in ...
7
2008-09-06T02:42:49Z
47,304
<p>You can use <code>get_absolute_url</code>, but that will only work for a particular object. Since your object hasn't been created yet, it won't work in this case.</p> <p>You want to use <a href="https://docs.djangoproject.com/en/1.2/topics/http/urls/#naming-url-patterns" rel="nofollow">named URL patterns</a>. Her...
15
2008-09-06T07:22:52Z
[ "python", "django" ]
How do you set up a python wsgi server under IIS?
47,253
<p>I work in a windows environment and would prefer to deploy code to IIS. At the same time I would like to code in python.</p> <p>Having read that IIS can run fastCGI application, I went to the IIS site where it describes in detail how to get PHP up and running but not much about anything else.</p> <p>Does anyone ha...
22
2008-09-06T04:26:49Z
47,266
<p>There shouldn't be any need to use FastCGI. There exists a <a href="https://github.com/hexdump42/isapi-wsgi">ISAPI extension for WSGI</a>.</p>
20
2008-09-06T05:32:39Z
[ "python", "iis", "deployment", "windows-server", "iis-modules" ]
How do you set up a python wsgi server under IIS?
47,253
<p>I work in a windows environment and would prefer to deploy code to IIS. At the same time I would like to code in python.</p> <p>Having read that IIS can run fastCGI application, I went to the IIS site where it describes in detail how to get PHP up and running but not much about anything else.</p> <p>Does anyone ha...
22
2008-09-06T04:26:49Z
37,523,628
<p>We can use iiswsgi framework to setup WSGI over IIS since it is compatible with IIS web server's FastCGI protocol.It's bundled with distutils for building, distribution and installing packages with the help of Microsoft Web Deploy and Web Platform Installer. </p> <p>For more info refer the following link:</p> <p><...
4
2016-05-30T10:39:30Z
[ "python", "iis", "deployment", "windows-server", "iis-modules" ]
How do you set up a python wsgi server under IIS?
47,253
<p>I work in a windows environment and would prefer to deploy code to IIS. At the same time I would like to code in python.</p> <p>Having read that IIS can run fastCGI application, I went to the IIS site where it describes in detail how to get PHP up and running but not much about anything else.</p> <p>Does anyone ha...
22
2008-09-06T04:26:49Z
37,529,437
<p>Microsoft itself develops <a href="https://pypi.python.org/pypi/wfastcgi" rel="nofollow">wfastcgi</a> (<a href="https://github.com/Microsoft/PTVS/tree/master/Python/Product/WFastCgi" rel="nofollow">source code</a>) to host Python code on IIS.</p>
4
2016-05-30T15:39:37Z
[ "python", "iis", "deployment", "windows-server", "iis-modules" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
47,744
<p>What platform are you attempting this on? Most debuggers allow you to attach to a running process by using the process id. You can either output the process id via logging or using something like Task Manager. Once that is achieved it will be possible to inspect individual threads and their call stacks.</p> <p>EDIT...
0
2008-09-06T19:08:14Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
56,510
<p>Yeah, gdb is good for lower level debugging.</p> <p>You can change threads with the <em>thread</em> command.</p> <p>e.g</p> <pre><code>(gdb) thr 2 [Switching to thread 2 (process 6159 thread 0x3f1b)] (gdb) backtrace .... </code></pre> <p>You could also check out Python specific debuggers like <a href="http://win...
8
2008-09-11T13:19:10Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
59,179
<p>If you mean the pydb, there is no way to do it. There was some effort in that direction: <a href="http://svn.python.org/projects/sandbox/trunk/pdb/mpdb.py" rel="nofollow">see the svn commit</a>, but it was abandoned. Supposedly <a href="http://winpdb.org/docs/launch-time/" rel="nofollow">winpdb supports it</a>.</p>
3
2008-09-12T14:51:51Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
277,800
<p>My experience debugging multi-threaded programs in PyDev (Eclipse on Windows XP) is, threads created using thread.start_new_thread could not be hooked, but thread created using threading.Thread could be hooked. Hope the information is helpful.</p>
3
2008-11-10T12:58:24Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
550,795
<p>You can attach a debugger to a multi-threaded Python process, but you need to do it at the C level. To make sense of what's going on, you need the Python interpreter to be compiled with symbols. If you don't have one, you need to download source from python.org and build it yourself:</p> <pre><code>./configure --...
6
2009-02-15T12:18:34Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
553,633
<p>Use <a href="http://winpdb.org/">Winpdb</a>. It is a <strong>platform independent</strong> graphical GPL Python debugger with support for remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.</p> <p>Features:</p>...
12
2009-02-16T15:18:44Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
29,093,342
<p><a href="https://github.com/dcramer/pdbinject" rel="nofollow">pdbinject</a> allows you to inject pdb into an already running python process.</p> <p>The pdbinject executable only works under python2, but can inject into python3 just fine too. </p>
0
2015-03-17T07:18:57Z
[ "python", "debugging" ]
Is there a way to attach a debugger to a multi-threaded Python process?
47,701
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
24
2008-09-06T18:14:05Z
29,354,135
<p><a href="https://www.jetbrains.com/pycharm/" rel="nofollow">PyCharm IDE</a> allows attaching to a running Python process since version 4.0. </p> <p><a href="https://www.jetbrains.com/pycharm/help/attaching-to-process.html" rel="nofollow">Here</a> is described how to do that.</p>
0
2015-03-30T19:02:50Z
[ "python", "debugging" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
47,792
<p>Use list comprehensions when the result needs to be iterated over multiple times, or where speed is paramount. Use generator expressions where the range is large or infinite.</p>
59
2008-09-06T20:10:59Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
47,793
<p>Iterating over the <em>generator expression</em> or the <em>list comprehension</em> will do the same thing. However, the <em>list comprehension</em> will create the entire list in memory first while the <em>generator expression</em> will create the items on the fly, so you are able to use it for very large (and also...
89
2008-09-06T20:11:17Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
47,826
<p>John's answer is good (that list comprehensions are better when you want to iterate over something multiple times). However, it's also worth noting that you should use a list if you want to use any of the list methods. For example, the following code won't work:</p> <pre><code>def gen(): return (something for...
168
2008-09-06T20:54:08Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
53,255
<p>Sometimes you can get away with the <em>tee</em> function from <a href="https://docs.python.org/3/library/itertools.html" rel="nofollow">itertools</a>, it returns multiple iterators for the same generator that can be used independently.</p>
3
2008-09-10T00:58:03Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
189,840
<p>The benefit of a generator expression is that it uses less memory since it doesn't build the whole list at once. Generator expressions are best used when the list is an intermediary, such as summing the results, or creating a dict out of the results.</p> <p>For example:</p> <pre><code>sum(x*2 for x in xrange(256)...
34
2008-10-10T01:42:30Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
22,858,478
<p>The important point is that the list comprehension creates a new list. The generator creates a an iterable object that will "filter" the source material on-the-fly as you consume the bits.</p> <p>Imagine you have a 2TB log file called "hugefile.txt", and you want the content and length for all the lines that start ...
27
2014-04-04T09:14:57Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
34,599,335
<p>I'm using the Hadoop Mincemeat module. I think this is a great example to take a note of:</p> <pre><code>def mapfn(k,v): for w in v: yield 'sum',w #yield 'count',1 def reducefn(k,v): r1=sum(v) r2=len(v) print r2 m=r1/r2 std=0 for i in range(r2): std+=pow(abs(v[i...
1
2016-01-04T20:31:50Z
[ "python", "list-comprehension", "generator" ]
Generator Expressions vs. List Comprehension
47,789
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
213
2008-09-06T20:07:59Z
35,964,024
<p>When creating a generator from a mutable object (like a list) be aware that the generator will get evaluated on the state of the list at time of using the generator, not at time of the creation of the generator:</p> <pre><code>&gt;&gt;&gt; mylist = ["a", "b", "c"] &gt;&gt;&gt; gen = (elem + "1" for elem in mylist) ...
1
2016-03-12T22:21:00Z
[ "python", "list-comprehension", "generator" ]
User Authentication in Pylons + AuthKit
47,801
<p>I am trying to create a web application using Pylons and the resources on the web point to the <a href="http://pylonsbook.com/alpha1/authentication_and_authorization">PylonsBook</a> page which isn't of much help. I want authentication and authorisation and is there anyway to setup Authkit to work easily with Pylons?...
6
2008-09-06T20:17:45Z
52,136
<p>I don't think AuthKit is actively maintained anymore. It does use the Paste (<a href="http://pythonpaste.org" rel="nofollow">http://pythonpaste.org</a>) libs though for things like HTTP Basic/Digest authentication. I would probably go ahead and take a look at the source for some inspiration and then use the Paste to...
1
2008-09-09T15:18:50Z
[ "python", "authentication", "sqlalchemy", "pylons", "authkit" ]
User Authentication in Pylons + AuthKit
47,801
<p>I am trying to create a web application using Pylons and the resources on the web point to the <a href="http://pylonsbook.com/alpha1/authentication_and_authorization">PylonsBook</a> page which isn't of much help. I want authentication and authorisation and is there anyway to setup Authkit to work easily with Pylons?...
6
2008-09-06T20:17:45Z
61,614
<p>This actually got me interested:<a href="http://groups.google.com/group/pylons-discuss/browse_thread/thread/644deb53612af362?hl=en" rel="nofollow">Check out this mailing on the pylons list</a>. So AuthKit is being developed, and I will follow the book and get back on the results.</p>
1
2008-09-14T20:26:02Z
[ "python", "authentication", "sqlalchemy", "pylons", "authkit" ]
User Authentication in Pylons + AuthKit
47,801
<p>I am trying to create a web application using Pylons and the resources on the web point to the <a href="http://pylonsbook.com/alpha1/authentication_and_authorization">PylonsBook</a> page which isn't of much help. I want authentication and authorisation and is there anyway to setup Authkit to work easily with Pylons?...
6
2008-09-06T20:17:45Z
139,574
<p>Ok, another update on the subject. It seems that the cheeseshop template is broken. I've followed the chapter you linked in the post and it seems that authkit is working fine. There are some caveats:</p> <ol> <li>sqlalchemy has to be in 0.5 version</li> <li>authkit has to be the dev version from svn (easy_install a...
2
2008-09-26T13:42:50Z
[ "python", "authentication", "sqlalchemy", "pylons", "authkit" ]
User Authentication in Pylons + AuthKit
47,801
<p>I am trying to create a web application using Pylons and the resources on the web point to the <a href="http://pylonsbook.com/alpha1/authentication_and_authorization">PylonsBook</a> page which isn't of much help. I want authentication and authorisation and is there anyway to setup Authkit to work easily with Pylons?...
6
2008-09-06T20:17:45Z
255,183
<p>Check out this tutorial:</p> <p><a href="http://wiki.pylonshq.com/display/pylonscookbook/AuthKit+0.4+UsersFromDatabase+with+SQLAlchemy+0.4+and+SQLAlchemyManager" rel="nofollow">http://wiki.pylonshq.com/display/pylonscookbook/AuthKit+0.4+UsersFromDatabase+with+SQLAlchemy+0.4+and+SQLAlchemyManager</a></p>
0
2008-10-31T22:47:23Z
[ "python", "authentication", "sqlalchemy", "pylons", "authkit" ]