title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Manually giving the twisted (web) network stack a packet to process?
1,546,745
<p>I am running an HTTP server using the twisted framework. Is there any way I can "manually" ask it to process some payload? For example, if I've constructed some Ethernet frame can I ask twisted's reactor to handle it just as if it had just arrived on my network card?</p>
1
2009-10-10T01:09:29Z
8,650,185
<p>You can do something like this:</p> <pre><code>from twisted.web import server from twisted.web.resource import Resource from twisted.internet import reactor from twisted.internet.protocol import Protocol, ClientFactory class SomeWebThing(Resource): def render_GET(self, request): return "hello\n" class...
2
2011-12-27T23:07:21Z
[ "python", "twisted" ]
How to create a specific if condition templatetag with Django?
1,546,816
<p>My problem is a if condition.</p> <p>I would like somethings like that but cannot figure out how to do it.</p> <pre><code>{% if restaurant.is_favorite_of(user) %} &lt;img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" /&gt; {% else %} &lt;img src="...
3
2009-10-10T01:53:39Z
1,546,838
<p>Maybe I could use <a href="http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags" rel="nofollow">the inclusion tag</a>.</p> <p>Create a tag like that :</p> <pre><code>{% show_favorite_img user restaurant %} </code></pre> <p>templatetags/user_extra.py :</p> <pre><code>@register.inclusion...
2
2009-10-10T02:09:08Z
[ "python", "django", "django-templates", "favorites" ]
How to create a specific if condition templatetag with Django?
1,546,816
<p>My problem is a if condition.</p> <p>I would like somethings like that but cannot figure out how to do it.</p> <pre><code>{% if restaurant.is_favorite_of(user) %} &lt;img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" /&gt; {% else %} &lt;img src="...
3
2009-10-10T01:53:39Z
1,547,306
<p>Easiest way is to create a filter.</p> <pre><code>@register.filter def is_favourite_of(object, user): return Favourite.objects.is_favourite(user, object) </code></pre> <p>and in the template:</p> <pre><code>{% if restaurant|is_favourite_of:user %} </code></pre>
11
2009-10-10T07:35:06Z
[ "python", "django", "django-templates", "favorites" ]
How to create a specific if condition templatetag with Django?
1,546,816
<p>My problem is a if condition.</p> <p>I would like somethings like that but cannot figure out how to do it.</p> <pre><code>{% if restaurant.is_favorite_of(user) %} &lt;img src="{{MEDIA_URL}}images/favorite_on.png" alt="This restaurant is one of your favorite (Click to undo)" /&gt; {% else %} &lt;img src="...
3
2009-10-10T01:53:39Z
1,549,535
<p>When all else fails you can use the {% expr whatever %} tag to compute a value and stick it in a variable that you can use in your template. I don't let designers know about it, but sometimes it's the only thing that works short of standing on your head and ... well, you know.</p> <p>See <a href="http://www.djangos...
0
2009-10-11T01:01:17Z
[ "python", "django", "django-templates", "favorites" ]
Str in Python's map and sum
1,546,846
<p><strong>Why do you need to use the function 'str' in the following code?</strong></p> <p>I am trying to count the sum of digits in a number.</p> <p><strong>My code</strong></p> <pre><code>for i in number: sum(map(int, str(i)) </code></pre> <p>where <em>number</em> is the following array</p> <pre><code>[7,79...
1
2009-10-10T02:13:21Z
1,546,853
<p>What happens when you try that code without using <code>str()</code>?</p> <p>The <code>str()</code> is used to convert the integer into a sequence of characters, so that <code>map()</code> can iterate over the sequence. The key point here is that a "string" can be treated as a "sequence of characters".</p>
2
2009-10-10T02:19:02Z
[ "python", "string" ]
Str in Python's map and sum
1,546,846
<p><strong>Why do you need to use the function 'str' in the following code?</strong></p> <p>I am trying to count the sum of digits in a number.</p> <p><strong>My code</strong></p> <pre><code>for i in number: sum(map(int, str(i)) </code></pre> <p>where <em>number</em> is the following array</p> <pre><code>[7,79...
1
2009-10-10T02:13:21Z
1,546,862
<p>Given <code>79</code> you need to get <code>[7, 9]</code> in order to sum up this list.</p> <p>What does it mean to split a number into digits? It means to represent the number in a numerical system with some base (base <code>10</code> in this case). E. g. <code>79</code> is <code>7 * 10**1 + 9 * 10**0</code>.</p> ...
8
2009-10-10T02:24:11Z
[ "python", "string" ]
Str in Python's map and sum
1,546,846
<p><strong>Why do you need to use the function 'str' in the following code?</strong></p> <p>I am trying to count the sum of digits in a number.</p> <p><strong>My code</strong></p> <pre><code>for i in number: sum(map(int, str(i)) </code></pre> <p>where <em>number</em> is the following array</p> <pre><code>[7,79...
1
2009-10-10T02:13:21Z
1,547,166
<p><strong>Why do you need to use the function 'str' in the following code?</strong></p> <p>Because map takes an iterable, like a list or a tuple or a string.</p> <p>The code in question adds upp all the numbers in an integer. And it does it by a little clever hack. It converts the number into a sequence of numbers b...
2
2009-10-10T05:47:06Z
[ "python", "string" ]
Workflow for maintaining different versions of codebase for different versions of Python
1,546,917
<p>I'm developing an open source application called <a href="http://garlicsim.org" rel="nofollow">GarlicSim</a>.</p> <p>Up to now I've been developing it only for Python 2.6. It seems not to work on any other version.</p> <p>I decided it's important to produce versions of it that will support other versions of Python...
4
2009-10-10T03:10:20Z
1,546,944
<p>I would try to maintain one branch to cover all the python 2.4-2.6</p> <p>The differences are not so great, after all if you have to write a bunch of extra code for 2.4 to do something that is easy in 2.6, it will be less work for you in the long run to use the 2.4 version for 2.5 and 2.6.</p> <p>Python 3 should h...
2
2009-10-10T03:28:21Z
[ "python", "git", "merge", "version", "organization" ]
Workflow for maintaining different versions of codebase for different versions of Python
1,546,917
<p>I'm developing an open source application called <a href="http://garlicsim.org" rel="nofollow">GarlicSim</a>.</p> <p>Up to now I've been developing it only for Python 2.6. It seems not to work on any other version.</p> <p>I decided it's important to produce versions of it that will support other versions of Python...
4
2009-10-10T03:10:20Z
1,547,094
<p>If your code is not overly dependent on the run-time performance in exception handlers, you might even get away without having a separate branch for Py3. I've managed to keep one version of pyparsing for all of my Py2.x versions, although I've had to stick with a "lowest common denominator" approach, meaning that I...
1
2009-10-10T04:55:29Z
[ "python", "git", "merge", "version", "organization" ]
Workflow for maintaining different versions of codebase for different versions of Python
1,546,917
<p>I'm developing an open source application called <a href="http://garlicsim.org" rel="nofollow">GarlicSim</a>.</p> <p>Up to now I've been developing it only for Python 2.6. It seems not to work on any other version.</p> <p>I decided it's important to produce versions of it that will support other versions of Python...
4
2009-10-10T03:10:20Z
1,547,148
<p>You need separate branches for separate versions only in the rarest of cases. You mention context managers, and they are great, and it would suck not to use them, and you are right. But for Python 2.4 you will have to not use them. So that will suck. So therefore, if you want to support Python 2.4 you'll have to wri...
3
2009-10-10T05:32:09Z
[ "python", "git", "merge", "version", "organization" ]
Workflow for maintaining different versions of codebase for different versions of Python
1,546,917
<p>I'm developing an open source application called <a href="http://garlicsim.org" rel="nofollow">GarlicSim</a>.</p> <p>Up to now I've been developing it only for Python 2.6. It seems not to work on any other version.</p> <p>I decided it's important to produce versions of it that will support other versions of Python...
4
2009-10-10T03:10:20Z
1,548,784
<p>I eventually decided to have 4 different forks for my project, for 2.4, 2.5, 2.6 and 3.1. My main priority is 2.6 and I don't want to compromise the elegance of that code for the sake of 2.4. So the ugly compatibility hacks will be on the lower versions, not the higher versions.</p>
0
2009-10-10T18:57:39Z
[ "python", "git", "merge", "version", "organization" ]
Defining private module functions in python
1,547,145
<p>According to <a href="http://www.faqs.org/docs/diveintopython/fileinfo%5Fprivate.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a>:</p> <blockquote> <p>Like most languages, Python has the concept of private elements: </p> <ul> <li>Private functions, which can't be called from <s...
106
2009-10-10T05:30:20Z
1,547,159
<p>Python allows for private <em>class</em> members with the double underscore prefix. This technique doesn't work at a module level so I am thinking this is a mistake in Dive Into Python.</p> <p>Here is an example of private class functions:</p> <pre><code>class foo(): def bar(self): pass def __bar(self): p...
21
2009-10-10T05:39:17Z
[ "python", "function", "module", "private" ]
Defining private module functions in python
1,547,145
<p>According to <a href="http://www.faqs.org/docs/diveintopython/fileinfo%5Fprivate.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a>:</p> <blockquote> <p>Like most languages, Python has the concept of private elements: </p> <ul> <li>Private functions, which can't be called from <s...
106
2009-10-10T05:30:20Z
1,547,160
<p>There may be confusion between <strong><em>class privates</em></strong> and <strong><em>module privates</em></strong>.</p> <p>A <em>module private</em> starts with <strong>one underscore</strong><br> Such a element is not copied along when using the <code>from &lt;module_name&gt; import *</code> form of the import ...
141
2009-10-10T05:40:54Z
[ "python", "function", "module", "private" ]
Defining private module functions in python
1,547,145
<p>According to <a href="http://www.faqs.org/docs/diveintopython/fileinfo%5Fprivate.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a>:</p> <blockquote> <p>Like most languages, Python has the concept of private elements: </p> <ul> <li>Private functions, which can't be called from <s...
106
2009-10-10T05:30:20Z
1,547,163
<p>In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't <em>force</em> it (any more than you can in real life;-). A single leading underscore means you're not <strong>supposed</strong> to access it "from the outside" -- <strong>two</strong> leading underscores (w/o trailing underscores)...
145
2009-10-10T05:43:03Z
[ "python", "function", "module", "private" ]
Defining private module functions in python
1,547,145
<p>According to <a href="http://www.faqs.org/docs/diveintopython/fileinfo%5Fprivate.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a>:</p> <blockquote> <p>Like most languages, Python has the concept of private elements: </p> <ul> <li>Private functions, which can't be called from <s...
106
2009-10-10T05:30:20Z
13,618,522
<p>This question was not fully answered, since module privacy is not purely conventional, and since using <strong>import</strong> may or may not recognize module privacy, depending on how it is used.</p> <p>If you define private names in a module, those names <strong>will</strong> be imported into any script that uses...
45
2012-11-29T03:50:32Z
[ "python", "function", "module", "private" ]
Defining private module functions in python
1,547,145
<p>According to <a href="http://www.faqs.org/docs/diveintopython/fileinfo%5Fprivate.html">http://www.faqs.org/docs/diveintopython/fileinfo_private.html</a>:</p> <blockquote> <p>Like most languages, Python has the concept of private elements: </p> <ul> <li>Private functions, which can't be called from <s...
106
2009-10-10T05:30:20Z
37,894,996
<p>Python has three modes via., private, public and protected .While importing a module only public mode is accessible .So private and protected modules cannot be called from outside of the module i.e., when it is imported .</p>
0
2016-06-18T08:40:48Z
[ "python", "function", "module", "private" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
1,547,201
<p><strong>EDIT</strong> I modified it, tried it out, and it works. You just need this piece of code</p> <p>As soon as ans = 4, ans * ans is no longer smaller than x. Try while ans*ans &lt;= x: instead of just &lt;</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt;= x: ...
0
2009-10-10T06:18:43Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
1,547,203
<p>Your <code>while</code> loop only executes once. No matter which branch the <code>if</code> statement inside it takes, the whole function will return immediately.</p>
1
2009-10-10T06:20:58Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
1,547,206
<p>Change your code so it displays the value of <code>ans</code> as well as <code>x</code>, so you can tell how many times the loop is executed.</p>
0
2009-10-10T06:23:12Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
1,547,210
<p>Indent your code correctly to let the <code>while</code> statement execute until <code>ans*ans &lt; x</code>:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: # this if statement was nested inside the while print...
7
2009-10-10T06:30:43Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
1,547,224
<p>If your code sample is actually correctly indentet the first round of the while will return on it's first round - always. So any positive value of x>1 will fullfil the ans*ans=1*1=1!=x, giving "x is not a perfect square". </p> <p>You basically needs to get your indentation right - like you do in your other example....
0
2009-10-10T06:37:47Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
5,750,159
<p>If the goal is to determine whether a number is a perfect square, I would think it would be simpler (and perhaps more efficient) to use math builtins, e.g.:</p> <pre><code>def is_perfect_square(n): if not ( ( isinstance(n, int) or isinstance(n, long) ) and ( n &gt;= 0 ) ): return False else: return mat...
-1
2011-04-21T21:50:41Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
5,767,169
<p>Just thought I'd contribute a simpler solution:</p> <pre><code>def is_square(n): return sqrt(n).is_integer() </code></pre> <p><strong>This is valid for <code>n &lt; 2**52 + 2**27 = 4503599761588224</code>.</strong></p> <p>Examples:</p> <pre><code>&gt;&gt;&gt; is_square(4) True &gt;&gt;&gt; is_square(123) Fal...
10
2011-04-23T21:49:19Z
[ "python" ]
Finding perfect square
1,547,196
<p>I have this python code:</p> <pre><code>def sqrt(x): ans = 0 if x &gt;= 0: while ans*ans &lt; x: ans = ans + 1 if ans*ans != x: print x, 'is not a perfect square.' return None else: print x, ' is a perfect square.' return ans else: print x, ' is no...
1
2009-10-10T06:15:36Z
24,680,548
<pre><code>def isPerfectSquare(number): return len(str(math.sqrt(number)).split('.')[1]) == 1 </code></pre>
0
2014-07-10T15:29:23Z
[ "python" ]
Prevent decorator from being used twice on the same function in python
1,547,222
<p>I have a decorator:</p> <pre><code>from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper </code></pre> <p>And I want to prevent it from decorating the same function twice, e.g prevent things such as:</p>...
1
2009-10-10T06:35:59Z
1,547,233
<p>Look at <code>f.func_code</code>, it can tell you if f is a function or a wrapper.</p>
-1
2009-10-10T06:45:07Z
[ "python", "decorator" ]
Prevent decorator from being used twice on the same function in python
1,547,222
<p>I have a decorator:</p> <pre><code>from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper </code></pre> <p>And I want to prevent it from decorating the same function twice, e.g prevent things such as:</p>...
1
2009-10-10T06:35:59Z
1,547,259
<p>Noam, The property of <code>func_code</code> to use is <code>co_name</code>. See below, all that is changed is two lines at top of d()'s def</p> <pre><code>def d(f): if f.func_code.co_name == 'wrapper': return f #ignore it (or can throw exception instead...) @wraps(f) def wrapper(*args, **kwargs...
0
2009-10-10T07:01:49Z
[ "python", "decorator" ]
Prevent decorator from being used twice on the same function in python
1,547,222
<p>I have a decorator:</p> <pre><code>from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper </code></pre> <p>And I want to prevent it from decorating the same function twice, e.g prevent things such as:</p>...
1
2009-10-10T06:35:59Z
1,547,261
<p>I'd store the information in the function itself. There is a risk of a conflict if multiple decorators decide to use the same variable, but if it's only your own code, you should be able to avoid it.</p> <pre><code>def d(f): if getattr(f, '_decorated_with_d', False): raise SomeException('Already decorat...
2
2009-10-10T07:02:22Z
[ "python", "decorator" ]
Prevent decorator from being used twice on the same function in python
1,547,222
<p>I have a decorator:</p> <pre><code>from functools import wraps def d(f): @wraps(f) def wrapper(*args,**kwargs): print 'Calling func' return f(*args,**kwargs) return wrapper </code></pre> <p>And I want to prevent it from decorating the same function twice, e.g prevent things such as:</p>...
1
2009-10-10T06:35:59Z
1,547,305
<p>I'll also propose my solution:</p> <p>first, create another decorator:</p> <pre><code>class DecorateOnce(object): def __init__(self,f): self.__f=f self.__called={} #save all functions that have been decorated def __call__(self,toDecorate): #get the distinct func name funcNa...
2
2009-10-10T07:33:50Z
[ "python", "decorator" ]
Python 3.1.1 with --enable-shared : will not build any extensions
1,547,310
<p>Summary: Building Python 3.1 on RHEL 5.3 64 bit with <code>--enable-shared</code> fails to compile all extensions. Building "normal" works fine without any problems.</p> <p><strong>Please note</strong> that this question may seem to blur the line between programming and system administration. However, I believe t...
6
2009-10-10T07:37:29Z
1,547,352
<p>Something is wrong with your build environment. It is picking up a libpython3.1.a from <code>/usr/local/lib</code>; this confuses the error messages. It tries linking with that library, which fails - however, it shouldn't have tried that in the first place, since it should have used the libpython that it just built....
5
2009-10-10T08:01:32Z
[ "python", "compilation", "python-3.x", "mod-wsgi" ]
Python 3.1.1 with --enable-shared : will not build any extensions
1,547,310
<p>Summary: Building Python 3.1 on RHEL 5.3 64 bit with <code>--enable-shared</code> fails to compile all extensions. Building "normal" works fine without any problems.</p> <p><strong>Please note</strong> that this question may seem to blur the line between programming and system administration. However, I believe t...
6
2009-10-10T07:37:29Z
1,549,832
<p>/usr/local/lib has been added to the library include path at compile time:</p> <p>-L/usr/local/lib -L.</p> <p>Its common for compile time to look in multiple 'common' paths for libraries (/usr/lib, /usr/local/lib, ./, etc) but also, it's possibly picking up /usr/local/lib from the environment variable LD_LIBRARY_P...
0
2009-10-11T04:10:17Z
[ "python", "compilation", "python-3.x", "mod-wsgi" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
1,547,369
<p>I did a <a href="http://www.dcl.hpi.uni-potsdam.de/home/loewis/psycopg%5F3k.diff" rel="nofollow">port</a> of psycopg2 to Python 3.</p>
1
2009-10-10T08:11:53Z
[ "python", "database", "python-3.x" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
1,547,384
<p>You could create a new sqlite object in each thread, each using the same database file. For such a small number of users you might not come across the problems with concurrency, unless they are all writing to it very heavily.</p>
0
2009-10-10T08:26:13Z
[ "python", "database", "python-3.x" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
1,547,572
<p>First note that <a href="http://www.sqlite.org/faq.html#q6">sqlite is thread safe</a></p> <pre><code>$ python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import sqlite &gt;&gt;&gt; ...
7
2009-10-10T10:20:12Z
[ "python", "database", "python-3.x" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
1,550,867
<p>I know there is some Python <a href="http://www.firebirdsql.org/index.php?op=devel&amp;sub=python" rel="nofollow">driver</a> for <a href="http://www.firebirdsql.org" rel="nofollow">Firebird</a> but I don't know if some exist for Python 3. May be you can ask in the Firebird-Python <a href="http://www.firebirdsql.org/...
1
2009-10-11T14:49:14Z
[ "python", "database", "python-3.x" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
1,550,870
<p>Surely a pragmatic option is to just use one SQLite connection per thread.</p>
0
2009-10-11T14:50:53Z
[ "python", "database", "python-3.x" ]
A database for python 3?
1,547,365
<p>I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.</p> <p>Since it's a threaded server, SQLite doesn't work. It complains about threads like this:</p> <blockquote> <p>ProgrammingError: SQLite objects created in a thread can ...
3
2009-10-10T08:09:24Z
10,863,434
<p>pymongo works with Python 3 now.</p>
1
2012-06-02T15:09:33Z
[ "python", "database", "python-3.x" ]
Forms in Django--cannot get past "cleaned_data"
1,547,412
<p>I have a form that allows users to upload text AND a file. However, I'd like to make it valid even if the user doesn't upload the file (file is optional). However, in Django, it is not allowing me to get past "clean(self)". I just want it simple--if text box, pass. If no text , return error.</p> <pre><code>class P...
2
2009-10-10T08:44:00Z
1,547,457
<p>You must set <code>required=False</code> for the fields which are optional as noted in <a href="http://docs.djangoproject.com/en/dev/ref/forms/fields/#required" rel="nofollow">the documentation</a></p> <p>In your case, the following line should do the trick:</p> <pre><code> file = forms.FileField(required=False...
4
2009-10-10T09:10:22Z
[ "python", "django" ]
Check if a parameter is a Python module?
1,547,466
<p>How can I (pythonically) check if a parameter is a Python module? There's no type like module or package.</p> <pre><code>&gt;&gt;&gt; os &lt;module 'os' from '/usr/lib/python2.6/os.pyc'&gt; &gt;&gt;&gt; isinstance(os, module) Traceback (most recent call last): File "/usr/lib/gedit-2/plugins/pythonconsole/console...
18
2009-10-10T09:18:38Z
1,547,469
<p>This seems a bit hacky, but:</p> <pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; import os &gt;&gt;&gt; type(os) is type(sys) True </code></pre>
2
2009-10-10T09:20:44Z
[ "python", "types" ]
Check if a parameter is a Python module?
1,547,466
<p>How can I (pythonically) check if a parameter is a Python module? There's no type like module or package.</p> <pre><code>&gt;&gt;&gt; os &lt;module 'os' from '/usr/lib/python2.6/os.pyc'&gt; &gt;&gt;&gt; isinstance(os, module) Traceback (most recent call last): File "/usr/lib/gedit-2/plugins/pythonconsole/console...
18
2009-10-10T09:18:38Z
1,547,475
<pre><code>from types import ModuleType isinstance(obj, ModuleType) </code></pre>
37
2009-10-10T09:25:46Z
[ "python", "types" ]
Check if a parameter is a Python module?
1,547,466
<p>How can I (pythonically) check if a parameter is a Python module? There's no type like module or package.</p> <pre><code>&gt;&gt;&gt; os &lt;module 'os' from '/usr/lib/python2.6/os.pyc'&gt; &gt;&gt;&gt; isinstance(os, module) Traceback (most recent call last): File "/usr/lib/gedit-2/plugins/pythonconsole/console...
18
2009-10-10T09:18:38Z
1,547,567
<pre><code>&gt;&gt;&gt; import inspect, os &gt;&gt;&gt; inspect.ismodule(os) True </code></pre>
20
2009-10-10T10:13:58Z
[ "python", "types" ]
How to do nested Django SELECT?
1,547,494
<pre><code>class Friendship(models.Model): from_friend = models.ForeignKey(User, related_name='friend_set') to_friend = models.ForeignKey(User, related_name='to_friend_set') </code></pre> <p>I'd like to SELECT all to_friends that have from_friend = a certain User.</p> <p>Then, I'd like to pass to_friends to i...
3
2009-10-10T09:35:07Z
1,547,534
<blockquote> <p>I'd like to <code>SELECT</code> all <code>to_friends</code> that have <code>from_friend</code> = a certain User.</p> </blockquote> <p>You could get all the Friendship objects for this step like so:</p> <pre><code>friendships = Friendship.objects.filter(from_friend=some_user) </code></pre> <p>Then y...
3
2009-10-10T09:58:43Z
[ "python", "mysql", "django" ]
How to do nested Django SELECT?
1,547,494
<pre><code>class Friendship(models.Model): from_friend = models.ForeignKey(User, related_name='friend_set') to_friend = models.ForeignKey(User, related_name='to_friend_set') </code></pre> <p>I'd like to SELECT all to_friends that have from_friend = a certain User.</p> <p>Then, I'd like to pass to_friends to i...
3
2009-10-10T09:35:07Z
1,549,654
<p>As pccardune says, you get the relevant users like this:</p> <pre><code>friendships = Friendship.objects.filter(from_friend=some_user) </code></pre> <p>But in fact you can pass this directly into your next query:</p> <pre><code>second_select = Whatever.objects.filter(friend__in=friendships) </code></pre>
2
2009-10-11T02:09:22Z
[ "python", "mysql", "django" ]
How to do nested Django SELECT?
1,547,494
<pre><code>class Friendship(models.Model): from_friend = models.ForeignKey(User, related_name='friend_set') to_friend = models.ForeignKey(User, related_name='to_friend_set') </code></pre> <p>I'd like to SELECT all to_friends that have from_friend = a certain User.</p> <p>Then, I'd like to pass to_friends to i...
3
2009-10-10T09:35:07Z
1,549,673
<p>This appears to return the desired results</p> <pre><code>User.objects.filter(to_friend_set__from_friend=1) </code></pre>
2
2009-10-11T02:24:22Z
[ "python", "mysql", "django" ]
A minimalist, non-enterprisey approach for a SOAP server in Python
1,547,520
<p>I need to implement a small test utility which consumes extremely simple SOAP XML (HTTP POST) messages. This is a protocol which I have to support, and it's not my design decision to use SOAP (just trying to prevent those "why do you use protocol X?" answers) </p> <p>I'd like to use stuff that's already in the basi...
1
2009-10-10T09:49:47Z
1,547,530
<p>You could write a WSGI function (see <a href="http://docs.python.org/library/wsgiref.html" rel="nofollow"><code>wsgiref</code></a>) and parse inside it an HTTP request body using the <a href="http://docs.python.org/library/xml.etree.elementtree.html" rel="nofollow"><code>xml.etree.ElementTree</code></a> module.</p> ...
3
2009-10-10T09:55:24Z
[ "python", "http", "soap" ]
A minimalist, non-enterprisey approach for a SOAP server in Python
1,547,520
<p>I need to implement a small test utility which consumes extremely simple SOAP XML (HTTP POST) messages. This is a protocol which I have to support, and it's not my design decision to use SOAP (just trying to prevent those "why do you use protocol X?" answers) </p> <p>I'd like to use stuff that's already in the basi...
1
2009-10-10T09:49:47Z
1,547,642
<p>I wrote something like this in Boo, using a .Net HTTPListener, because I too had to implement someone else's defined WSDL.</p> <p>The WSDL I was given used document/literal form (you'll need to make some adjustments to this information if your WSDL uses rpc/encoded). I wrapped the HTTPListener in a class that allo...
1
2009-10-10T10:55:40Z
[ "python", "http", "soap" ]
Improvizing a drop-in replacement for the "with" statement for Python 2.4
1,547,526
<p>Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4?</p> <p>It would be a hack, but it would allow me to port my project to Python 2.4 more nicely.</p> <p><strong>EDIT:</strong> Removed irrelevant metaclass sketch</p>
6
2009-10-10T09:53:31Z
1,547,559
<p>You could (ab)use decorators to do this, I think. The following works, eg:</p> <pre><code>def execute_with_context_manager(man): def decorator(f): target = man.__enter__() exc = True try: try: f(target) except: exc = False ...
4
2009-10-10T10:11:16Z
[ "python", "with-statement" ]
Improvizing a drop-in replacement for the "with" statement for Python 2.4
1,547,526
<p>Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4?</p> <p>It would be a hack, but it would allow me to port my project to Python 2.4 more nicely.</p> <p><strong>EDIT:</strong> Removed irrelevant metaclass sketch</p>
6
2009-10-10T09:53:31Z
1,547,565
<p>Since you need to exit the context manager both during errors and not errors, I don't think it's possible to do a generic usecase with metaclasses, or in fact at all. You are going to need try/finally blocks for that.</p> <p>But maybe it's possible to do something else in your case. That depends on what you use the...
1
2009-10-10T10:13:20Z
[ "python", "with-statement" ]
Improvizing a drop-in replacement for the "with" statement for Python 2.4
1,547,526
<p>Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4?</p> <p>It would be a hack, but it would allow me to port my project to Python 2.4 more nicely.</p> <p><strong>EDIT:</strong> Removed irrelevant metaclass sketch</p>
6
2009-10-10T09:53:31Z
1,547,628
<p>Just use try-finally.</p> <p>Really, this may be nice as a mental exercise, but if you actually do it in code you care about you will end up with ugly, hard to maintain code.</p>
6
2009-10-10T10:47:10Z
[ "python", "with-statement" ]
Improvizing a drop-in replacement for the "with" statement for Python 2.4
1,547,526
<p>Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4?</p> <p>It would be a hack, but it would allow me to port my project to Python 2.4 more nicely.</p> <p><strong>EDIT:</strong> Removed irrelevant metaclass sketch</p>
6
2009-10-10T09:53:31Z
1,548,883
<p>How about this?</p> <pre><code>def improvize_context_manager(*args, **kwargs): assert (len(args) + len(kwargs)) == 1 if args: context_manager = args[0] as_ = None else: # It's in kwargs (as_, context_manager) = kwargs.items()[0] def decorator(f): exit_ = context_man...
1
2009-10-10T19:46:39Z
[ "python", "with-statement" ]
Improvizing a drop-in replacement for the "with" statement for Python 2.4
1,547,526
<p>Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4?</p> <p>It would be a hack, but it would allow me to port my project to Python 2.4 more nicely.</p> <p><strong>EDIT:</strong> Removed irrelevant metaclass sketch</p>
6
2009-10-10T09:53:31Z
3,524,603
<p>If you are OK with using def just to get a block, and decorators that immediately execute, you could use the function signature to get something more natural for the named case.</p> <pre> import sys def with(func): def decorated(body = func): contexts = body.func_defaults try: exc = ...
1
2010-08-19T17:43:48Z
[ "python", "with-statement" ]
General printing raster and/or vector images
1,547,621
<p>I'm looking for some API for printing.</p> <p>Basically what I want to achieve is to print set of pixels(monochromatic bitmap which I store in memory) onto the generic paper format (A4,A5..etc.).</p> <p>What I think that would be minimum API is:</p> <ul> <li>printer devices list</li> <li>printer buffer where I co...
2
2009-10-10T10:43:22Z
1,547,649
<p>You might want to <a href="http://wiki.wxpython.org/Printing" rel="nofollow">investigate wx python for printing</a>. Learning the framework might be a bit of an overhead for you though! I've had success with that in the past, both on windows and linux.</p> <p>I've also used <a href="http://www.reportlab.org/downl...
0
2009-10-10T10:58:03Z
[ "c++", "python", "c" ]
General printing raster and/or vector images
1,547,621
<p>I'm looking for some API for printing.</p> <p>Basically what I want to achieve is to print set of pixels(monochromatic bitmap which I store in memory) onto the generic paper format (A4,A5..etc.).</p> <p>What I think that would be minimum API is:</p> <ul> <li>printer devices list</li> <li>printer buffer where I co...
2
2009-10-10T10:43:22Z
1,547,655
<p>I would use <a href="http://www.pythonware.com/products/pil/" rel="nofollow">PIL</a> to create a BMP file, and then just use the standard OS services to print that file. PIL will accept data in either raster or vector form.</p>
0
2009-10-10T10:59:18Z
[ "c++", "python", "c" ]
General printing raster and/or vector images
1,547,621
<p>I'm looking for some API for printing.</p> <p>Basically what I want to achieve is to print set of pixels(monochromatic bitmap which I store in memory) onto the generic paper format (A4,A5..etc.).</p> <p>What I think that would be minimum API is:</p> <ul> <li>printer devices list</li> <li>printer buffer where I co...
2
2009-10-10T10:43:22Z
1,547,656
<p>Well you got close, look at <a href="http://doc.trolltech.com/4.5/printing.html" rel="nofollow">Printing in Qt</a>. There is the <a href="http://doc.trolltech.com/4.5/qprinter.html" rel="nofollow">QPrinter</a> class that implements some of what you are looking for. It is implmenetent as a <code>QPaintDevice</code>. ...
0
2009-10-10T10:59:31Z
[ "c++", "python", "c" ]
How to sort this list in Python?
1,547,733
<p>[ {'time':33}, {'time':11}, {'time':66} ]</p> <p>How to sort by the "time" element, DESC.</p>
5
2009-10-10T11:37:47Z
1,547,739
<p>Like this:</p> <pre><code>from operator import itemgetter l = sorted(l, key=itemgetter('time'), reverse=True) </code></pre> <p>Or:</p> <pre><code>l = sorted(l, key=lambda a: a['time'], reverse=True) </code></pre> <p>output: </p> <pre><code>[{'time': 66}, {'time': 33}, {'time': 11}] </code></pre> <p>If you d...
27
2009-10-10T11:41:33Z
[ "python", "list" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
1,547,788
<p>I have limited but positive experience with <a href="http://www.dabeaz.com/ply/">PLY</a> (Python Lex-Yacc). It combines <a href="http://dinosaur.compilertools.net/">Lex and Yacc</a> functionality in a single Python class. You may want to check it out.</p> <p>Fellow Stackoverflow'er <a href="http://stackoverflow.com...
16
2009-10-10T12:12:23Z
[ "python", "dsl" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
1,547,789
<p>In order to be productive, I'd always use a parser generator like <a href="http://pypi.python.org/pypi/CocoPy/" rel="nofollow">CocoPy</a> (<a href="http://www.ssw.uni-linz.ac.at/Coco/Tutorial/" rel="nofollow">Tutorial</a>) to have your grammar transformed into a (correct) parser (unless you want to implement the par...
2
2009-10-10T12:13:22Z
[ "python", "dsl" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
1,547,791
<p>Python is such a wonderfully simple and extensible language that I'd suggest merely creating a comprehensive python module, and coding against that.</p> <p>I see that while I typed up the above, <a href="http://www.dabeaz.com/ply/" rel="nofollow"><code>PLY</code></a> has already been mentioned.</p>
3
2009-10-10T12:14:43Z
[ "python", "dsl" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
1,548,368
<p><a href="http://pyparsing.wikispaces.com/">Pyparsing</a> is handy for writing "little languages". I gave a <a href="http://www.ptmcg.com/geo/python/confs/pyCon2006%5Fpres2.html">presentation at PyCon'06</a> on writing a simple adventure game engine, in which the language being parsed and interpreted was the game co...
29
2009-10-10T16:20:32Z
[ "python", "dsl" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
1,548,393
<p>I would recommend <a href="http://code.google.com/p/funcparserlib/"><code>funcparserlib</code></a>. It was written especially for parsing little languages and DSLs and it is faster and smaller than <code>pyparsing</code> (see stats on its homepage). Minimalists and functional programmers should like <code>funcparser...
14
2009-10-10T16:29:48Z
[ "python", "dsl" ]
Mini-languages in Python
1,547,782
<p>I'm after creating a simple mini-language parser in Python, programming close to the problem domain and all that.</p> <p>Anyway, I was wondering how the people on here would go around doing that - what are the preferred ways of doing this kind of thing in Python? </p> <p>I'm not going to give specific details of w...
34
2009-10-10T12:06:18Z
39,859,385
<p>If you ask me this now, I would try the textx library for python. You can very easily create a dsl in that with python! Advantages are that it creates an AST for you, and lexing and parsing is combined.</p> <p><a href="http://igordejanovic.net/textX/" rel="nofollow">http://igordejanovic.net/textX/</a></p>
1
2016-10-04T18:32:21Z
[ "python", "dsl" ]
Crazy python behaviour
1,547,856
<p>I have a little piece of python code in the server script for my website which looks a little bit like this:</p> <pre><code>console.append([str(x) for x in data]) console.append(str(max(data))) </code></pre> <p>quite simple, you might think, however the result it's outputting is this:</p> <pre><code>['3', '12', '...
0
2009-10-10T12:45:38Z
1,547,861
<p>I know very little Python, but you are taking the max of strings, which means that <code>'3..'</code> is greater than <code>'1..'</code>.</p>
1
2009-10-10T12:49:46Z
[ "python", "max" ]
Crazy python behaviour
1,547,856
<p>I have a little piece of python code in the server script for my website which looks a little bit like this:</p> <pre><code>console.append([str(x) for x in data]) console.append(str(max(data))) </code></pre> <p>quite simple, you might think, however the result it's outputting is this:</p> <pre><code>['3', '12', '...
0
2009-10-10T12:45:38Z
1,547,868
<p>Because the character <code>'3'</code> is higher in the ASCII table than <code>'1'</code>. You are comparing strings, not numbers. If you want to compare the numerically, you need to convert them to numbers. One way is <code>max(data, key=int)</code>, but you might want to actually store numbers in the list.</p>
8
2009-10-10T12:51:04Z
[ "python", "max" ]
How do I parse indents and dedents with pyparsing?
1,547,944
<p>Here is a subset of the Python grammar:</p> <pre><code>single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE stmt: simple_stmt | compound_stmt simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE small_stmt: pass_stmt pass_stmt: 'pass' compound_stmt: if_stmt if_stmt: 'if' test ':' suite ('elif' test ':'...
7
2009-10-10T13:27:37Z
1,548,219
<p>There are a couple of examples on the pyparsing wiki <a href="http://pyparsing.wikispaces.com/Examples">Examples page</a> that could give you some insights:</p> <ul> <li><a href="http://pyparsing.wikispaces.com/file/view/pythonGrammarParser.py">pythonGrammarParser.py</a></li> <li><a href="http://pyparsing.wikispace...
9
2009-10-10T15:24:35Z
[ "python", "indentation", "parser-generator", "pyparsing" ]
Traffic shaping under Linux
1,548,086
<p>Where can I learn about controlling/interrogating the network interface under Linux? I'd like to get specific application upload/download speeds, and enforce a speed limit for a specific application. </p> <p>I'd particularly like information that can help me write a traffic shaping application using Python.</p>
4
2009-10-10T14:24:27Z
1,548,211
<p>You want the iproute2 suite, in which you use the <a href="http://www.linuxfoundation.org/en/Net:Iproute2_examples" rel="nofollow">tc</a> command. tc commands look like</p> <pre><code>tc class add dev eth2 parent 1: classid 1:1 htb rate 100Mbit ceil 100Mbit quantum 1600 </code></pre> <p>Here's <a href="http://free...
5
2009-10-10T15:21:46Z
[ "python", "linux", "ubuntu", "trafficshaping" ]
Traffic shaping under Linux
1,548,086
<p>Where can I learn about controlling/interrogating the network interface under Linux? I'd like to get specific application upload/download speeds, and enforce a speed limit for a specific application. </p> <p>I'd particularly like information that can help me write a traffic shaping application using Python.</p>
4
2009-10-10T14:24:27Z
1,548,244
<p>It is actually quite hard shaping per application using the linux kernel tools, unless the application uses specific ip addresses and/or ports you can match on.</p> <p>Assuming that is the case then you'll need to read up on <code>iptables</code> and in particular fwmarks. You'll also need to read up on <code>tc</...
6
2009-10-10T15:34:28Z
[ "python", "linux", "ubuntu", "trafficshaping" ]
Traffic shaping under Linux
1,548,086
<p>Where can I learn about controlling/interrogating the network interface under Linux? I'd like to get specific application upload/download speeds, and enforce a speed limit for a specific application. </p> <p>I'd particularly like information that can help me write a traffic shaping application using Python.</p>
4
2009-10-10T14:24:27Z
1,548,761
<p>Is there any reason you wish to use python? As mentioned, it will likely only hand-off to already developed tools for this purpose. However, if you look around, you can find things such as <code>Click! modular router</code>, <code>XORP</code>, and others that provide a drop-in for things you want to do - not to ment...
0
2009-10-10T18:49:29Z
[ "python", "linux", "ubuntu", "trafficshaping" ]
Python library for syntax highlighting
1,548,276
<p>Which Python library for syntax highlighting is the best one? I'm interested in things like supported languages, ease of use, pythonic design, dependencies, development status, etc.</p>
2
2009-10-10T15:48:40Z
1,548,292
<p>I've always heard <a href="http://pygments.org/" rel="nofollow">pygments</a> as the module for syntax highlighting. I have never used it myself, tho.</p>
2
2009-10-10T15:53:58Z
[ "python", "syntax-highlighting", "pygments" ]
Python library for syntax highlighting
1,548,276
<p>Which Python library for syntax highlighting is the best one? I'm interested in things like supported languages, ease of use, pythonic design, dependencies, development status, etc.</p>
2
2009-10-10T15:48:40Z
1,548,295
<p>I think <a href="http://pygments.org/">pygments</a> is the greatest choice. It supports a large number of languages and it's very mature.</p>
8
2009-10-10T15:54:19Z
[ "python", "syntax-highlighting", "pygments" ]
Backporting float("inf") to Python 2.4 and 2.5
1,548,279
<p>I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used <code>float("inf")</code>, and now I find it is unavailable on Python 2.5. Is there a backport of it?</p>
2
2009-10-10T15:49:11Z
1,548,287
<p>Spelling it either the long way or the short way works fine for me:</p> <pre><code>$ python2.4 -c "print float('inf')+200" inf $ python2.5 -c "print float('inf')+200" inf $ python2.5 -c "print float('infinity')+200" inf $ python2.4 -c "print float('infinity')+200" inf </code></pre> <p>The <code>-c</code> flag mean...
4
2009-10-10T15:52:12Z
[ "python", "infinity", "backport" ]
Backporting float("inf") to Python 2.4 and 2.5
1,548,279
<p>I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used <code>float("inf")</code>, and now I find it is unavailable on Python 2.5. Is there a backport of it?</p>
2
2009-10-10T15:49:11Z
1,548,409
<p>You should be able to fake it up by giving Python a sufficiently large floating point constant instead. For instance:</p> <pre><code>&gt;&gt;&gt; 1e100000 inf &gt;&gt;&gt; float('inf') == 1e1000000 True &gt;&gt;&gt; float('inf') == 2e1000000 True </code></pre>
2
2009-10-10T16:36:17Z
[ "python", "infinity", "backport" ]
Backporting float("inf") to Python 2.4 and 2.5
1,548,279
<p>I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used <code>float("inf")</code>, and now I find it is unavailable on Python 2.5. Is there a backport of it?</p>
2
2009-10-10T15:49:11Z
5,131,423
<p>The <a href="http://docs.python.org/library/decimal.html" rel="nofollow">decimal</a> module is available since Python 2.4 and supports positive or negative infinite decimals. It does not always behave like a float (eg. adding a decimal and a float is not supported) but does the right thing for comparison, which was ...
1
2011-02-27T04:20:46Z
[ "python", "infinity", "backport" ]
Backporting float("inf") to Python 2.4 and 2.5
1,548,279
<p>I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used <code>float("inf")</code>, and now I find it is unavailable on Python 2.5. Is there a backport of it?</p>
2
2009-10-10T15:49:11Z
5,141,374
<p>I created a backport, tested on Python 2.5+, can probably be easily made to work on Python 2.4:</p> <p><a href="https://github.com/cool-RR/GarlicSim/blob/master/garlicsim/garlicsim/general_misc/infinity.py" rel="nofollow">https://github.com/cool-RR/GarlicSim/blob/master/garlicsim/garlicsim/general_misc/infinity.py<...
0
2011-02-28T11:30:30Z
[ "python", "infinity", "backport" ]
Backporting float("inf") to Python 2.4 and 2.5
1,548,279
<p>I'm backporting my project from Python 2.6 to Python 2.4 and 2.5. In my project I used <code>float("inf")</code>, and now I find it is unavailable on Python 2.5. Is there a backport of it?</p>
2
2009-10-10T15:49:11Z
9,093,004
<p>Borrowing NumPy for a few lines:</p> <pre><code>import numpy as np inf = float(np.inf) </code></pre> <p>Here, <code>inf</code> is a <a href="http://docs.python.org/release/2.5/lib/typesnumeric.html" rel="nofollow">regular python <code>float</code></a>, so you don't need to bother with NumPy any further.</p>
0
2012-02-01T08:46:56Z
[ "python", "infinity", "backport" ]
Python way to do crc32b
1,548,366
<p>As i posted as title, there is a way to use the crc32b hash on python natively or through a library (i.e. chilkat)?</p> <p>My intention is to "translate" a program from php to python, so output should be same as in php:</p> <p>$hashedData= hash('crc32b',$data);</p> <p>-> Edit: in a win32 system</p> <p>Thanks to ...
0
2009-10-10T16:20:06Z
1,548,390
<p><a href="http://labix.org/python-mhash" rel="nofollow">python-mhash</a> supplies many hashing functions including crc32b.</p>
1
2009-10-10T16:29:32Z
[ "python", "hash", "crc" ]
Self contained classes with Qt
1,548,370
<p>I've been trying to make my classes completely self contained, but I'm having some problems, which are probably coming from my missing something that everybody else knew first off...</p> <p>Anyway, take this example:</p> <pre><code>class Main_Window (QtGui.QMainWindow): def __init__ (self, parent=None): ...
0
2009-10-10T16:21:02Z
1,548,447
<p>I don't know Python so I can't give a good example here, but what I would probably do with Qt in C++ is define a signal of "bookingAdded" to your window object, and have one of your external objects (probably whichever invokes the UI) connect a slot to this signal, and then in your clickSubmitRecord you fire this si...
1
2009-10-10T16:48:08Z
[ "python", "oop" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
1,548,501
<p>HTML Generation is usually done with one of the infinite amounts of HTML templating languages available for Python. Personally I like <a href="http://debris.demon.nl/templess.txt" rel="nofollow">Templess</a>, but <a href="http://genshi.edgewall.org/" rel="nofollow">Genshi</a> is probably the most popular. There are ...
5
2009-10-10T17:13:40Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
1,548,514
<p>If you want programmatic generation rather than templating, Karrigell's <a href="http://karrigell.sourceforge.net/en/htmltags.html" rel="nofollow">HTMLTags</a> module is one possibility; it can include e.g. the <code>class</code> attribute (which would be a reserved word in Python) by the trick of uppercasing its in...
6
2009-10-10T17:20:25Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
1,548,597
<p>There's the venerable HTMLGen by Robin Friedrich, which is hard to find but still available <a href="http://www.python.org/ftp/python/contrib-09-Dec-1999/Network/HTMLgen.tar.gz">here</a> (dated 2001, but HTML hasn't changed much since then ;-). There's also <a href="http://www.livinglogic.de/Python/xist/">xist</a>. ...
5
2009-10-10T17:48:38Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
1,633,568
<p>Actually you can add any attribute such as id and class to objects in HTML.py (<a href="http://www.decalage.info/python/html" rel="nofollow">http://www.decalage.info/python/html</a>). </p> <p>attribs is an optional parameter of Table, TableRow and TableCell classes. It is a dictionary of additional attributes you w...
2
2009-10-27T20:49:03Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
2,612,236
<p>I have made a simple module for doing this. It works and has the same idea as HTML.py. However its a bit different and maybe more customizable? It is very lightweight. The project is called HTMLpy (pronounced HTML pie). You can see more information at <a href="http://patx.me/htmlpy" rel="nofollow">http://patx.me/htm...
0
2010-04-10T04:18:16Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
2,612,631
<p>You might be interested in some of the Python HAML implementations. HAML is like HTML shorthand and only takes a few minutes to learn. There's a CSS version called SASS too.</p> <p><a href="http://haml.hamptoncatlin.com/" rel="nofollow">http://haml.hamptoncatlin.com/</a></p> <p>"<a href="http://stackoverflow.com/q...
0
2010-04-10T07:35:22Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
6,715,824
<p>This is one ultra-simple HTML generator I have written. I use it build-time to generate html. If one is generating html pages run-time then there are better options available</p> <p>Here is the link</p> <p><a href="http://pypi.python.org/pypi/sphc" rel="nofollow">http://pypi.python.org/pypi/sphc</a></p> <p>And a ...
3
2011-07-16T06:38:49Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
25,540,947
<p><a href="https://github.com/Knio/dominate" rel="nofollow">Dominate</a> is an HTML generation library that lets you easily create tags. In dominate, python reserved words are prefixed with an underscore, so it would look like this:</p> <pre><code>from dominate.tags import * t = div(table(_id="the_table"), _class="tb...
1
2014-08-28T04:51:45Z
[ "python", "html", "generator" ]
python html generator
1,548,474
<p>I am looking for an easily implemented html generator for python. I found this one </p> <pre><code>http://www.decalage.info/python/html </code></pre> <p>but there is no way to add css elements (id, class) for table.</p> <p>thx</p>
15
2009-10-10T16:56:50Z
37,395,547
<p>Like the majority of the other posters - I too have written my own HTML generator. Mine is not a module though, it is a single function:</p> <pre><code>self_closing = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", ...
0
2016-05-23T15:58:48Z
[ "python", "html", "generator" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,631
<p><a href="http://www.diveintopython.net/" rel="nofollow">Dive into Python</a> is a good start. I wouldn't recommend to someone with no programming experience, but if you have coded in another language before, it will help you learn python idioms quickly.</p>
5
2009-10-10T17:59:16Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,641
<p>I started Python over a year ago too, also C++ background. </p> <p>I've learned that everything is simpler in Python, you don't need to worry so much if you're doing it right, you probably are. Most of the things came natural.</p> <p>I can't say I've read a book or anything, I usually pested the guys in #python o...
0
2009-10-10T18:03:05Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,642
<p>I second <a href="http://www.diveintopython.net/" rel="nofollow">Dive in to Python</a> as a resource. As for the main function, there isn't one. The "main" function is what you write in the script you run. </p> <p>So a helloworld.py looks like this:</p> <pre><code>print "Hello World" </code></pre> <p>and you run ...
0
2009-10-10T18:03:10Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,644
<p>When you run a script through the Python interpreter (or import that script from another script), <strong>it actually executes all the code from beginning to end</strong> -- in <em>that</em> sense, <strong>there is no "entry point" to a Python script</strong>.</p> <p>So to work around this, Python automatically cre...
11
2009-10-10T18:04:08Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,654
<p>If you are quite familiar with several languages like C++ and Java, you may find it easy to follow the official <a href="http://docs.python.org/tutorial/" rel="nofollow">Python Tutorial</a>. It is written in a classical language description bottom-up style from the lexical structure and syntax to more advanced conce...
1
2009-10-10T18:09:01Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,801
<p>The pithiest comment I guess is that the entry point is the 1st line of your script that is not a function or a class. You don't necessarily need to use the if hack unless you want to and your script is meant to be imported.</p>
0
2009-10-10T19:05:08Z
[ "python" ]
Python for C++ or Java Programmer
1,548,620
<p>I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can sh...
4
2009-10-10T17:56:09Z
1,548,871
<p>Excellent answer, but none points out what I think is one key insight for programmers coming to Python with background in other languages such as Java or C++: <code>import</code>, <code>def</code> and <code>class</code> are not "instructions to the compiler", "declarations", or other kind of magical incantations: th...
6
2009-10-10T19:37:01Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
1,548,720
<p>Try something like this:</p> <pre><code>import os, re def purge(dir, pattern): for f in os.listdir(dir): if re.search(pattern, f): os.remove(os.path.join(dir, f)) </code></pre> <p>Then you would pass the directory containing the files and the pattern you wish to match.</p>
29
2009-10-10T18:38:01Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
1,548,788
<p>If you need recursion into several subdirectories, you can use this method:</p> <pre><code>import os, re, os.path pattern = "^(?P&lt;photo_id&gt;\d+)[^\d].*jpg$" mypath = "Photos" for root, dirs, files in os.walk(mypath): for file in filter(lambda x: re.match(pattern, x), files): os.remove(os.path.join(...
7
2009-10-10T18:59:31Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
1,548,837
<p>It's not clear to me that you actually want to do any named-group matching -- in the use you describe, the photoid is an <em>input</em> to the deletion function, and named groups' purpose is "output", i.e., extracting certain substrings from the matched string (and accessing them by name in the match object). So, I...
2
2009-10-10T19:22:33Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
7,968,999
<p>My recomendation:</p> <pre><code>def purge(dir, pattern, inclusive=True): regexObj = re.compile(pattern) for root, dirs, files in os.walk(dir, topdown=False): for name in files: path = os.path.join(root, name) if bool(regexObj.search(path)) == bool(inclusive): ...
2
2011-11-01T15:45:40Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
21,572,576
<p>I find <code>Popen(["rm " + file_name + "*.ext"], shell=True, stdout=PIPE).communicate()</code> to be a much simpler solution to this problem. Although this is prone to injection attacks, I don't see any issues if your program is using this internally.</p>
0
2014-02-05T08:56:30Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
37,251,070
<p>How about this?</p> <pre><code>import glob, os map(os.remove, glob.glob("P*.jpg")) </code></pre> <p>Mind you this does not do recursion and uses wildcards (not regex).</p>
6
2016-05-16T09:50:35Z
[ "python" ]
Delete multiple files matching a pattern
1,548,704
<p>I have made an online gallery using Python and Django. I've just started to add editing functionality, starting with a rotation. I use sorl.thumbnail to auto-generate thumbnails on demand.</p> <p>When I edit the original file, I need to clean up all the thumbnails so new ones are generated. There are three or four ...
19
2009-10-10T18:32:43Z
38,189,275
<p>A variation on the glob approach, that will work with Python 3:</p> <pre><code>import glob, os for f in glob.glob("P*.jpg"): os.remove(f) </code></pre>
3
2016-07-04T16:45:14Z
[ "python" ]
Efficient storage of and access to web pages with Python
1,548,857
<p>So like many people I want a way to download, index/extract information and store web pages efficiently. My first thought is to use MySQL and simply shove the pages in which would let me use FULLTEXT searches which would let me do ad hoc queries easily (in case I want to see if something exists and extract it/etc.)....
1
2009-10-10T19:29:56Z
1,548,877
<p>It sounds to me like you need a content management system. Check out <a href="http://plone.org/" rel="nofollow">Plone</a>. If that's not what you want maybe a web framework, like Grok, BFG, Django, Turbogears, or anything on <a href="http://wiki.python.org/moin/WebFrameworks" rel="nofollow">this list</a>. If that is...
0
2009-10-10T19:43:04Z
[ "python", "mysql", "database" ]
Efficient storage of and access to web pages with Python
1,548,857
<p>So like many people I want a way to download, index/extract information and store web pages efficiently. My first thought is to use MySQL and simply shove the pages in which would let me use FULLTEXT searches which would let me do ad hoc queries easily (in case I want to see if something exists and extract it/etc.)....
1
2009-10-10T19:29:56Z
1,548,896
<p>You may be trying to achieve too much with the storage of the html (and supporting files). It seems you wish this repository would both</p> <ul> <li>allow to display a particular page as it was in its original site</li> <li>provide indexing for locating pages relevant to a particular search criteria</li> </ul> <p...
1
2009-10-10T19:52:25Z
[ "python", "mysql", "database" ]