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 |
|---|---|---|---|---|---|---|---|---|---|
Python: Lock directory access under windows | 394,439 | <p>I'd like to be able to lock directory access under windows.
The following code work greatly with file or directory under POSIX system:</p>
<pre><code>def flock(fd, blocking=False, exclusive=False):
if exclusive:
flags = fcntl.LOCK_EX
else:
flags = fcntl.LOCK_SH
if not blocking:
... | 3 | 2008-12-26T23:16:23Z | 395,212 | <p>I don't believe it's possible to use flock() on directories in windows. <a href="http://us2.php.net/flock" rel="nofollow">PHPs docs</a> on flock() indicate that it won't even work on FAT32 filesystems.</p>
<p>On the other hand, Windows already tends to not allow you to delete files/directories if any files are sti... | 1 | 2008-12-27T17:53:45Z | [
"python",
"windows",
"directory",
"locking"
] |
Python POST data using mod_wsgi | 394,465 | <p>This must be a very simple question, but I don't seem to be able to figure out.</p>
<p>I'm using apache + mod_wsgi to host my python application, and I'd like to get the post content submitted in one of the forms -however, neither the environment values, nor sys.stdin contains any of this data. Mind giving me a qui... | 14 | 2008-12-26T23:35:59Z | 394,543 | <p><a href="http://www.python.org/dev/peps/pep-0333/">PEP 333</a> says <a href="http://www.python.org/dev/peps/pep-0333/#input-and-error-streams">you must read environ['wsgi.input']</a>.</p>
<p>I just saved the following code and made apache's mod_wsgi run it. It works.</p>
<p>You must be doing something wrong.</p>
... | 21 | 2008-12-27T01:22:33Z | [
"python",
"mod-wsgi"
] |
Python POST data using mod_wsgi | 394,465 | <p>This must be a very simple question, but I don't seem to be able to figure out.</p>
<p>I'm using apache + mod_wsgi to host my python application, and I'd like to get the post content submitted in one of the forms -however, neither the environment values, nor sys.stdin contains any of this data. Mind giving me a qui... | 14 | 2008-12-26T23:35:59Z | 1,038,071 | <p>Be aware that technically speaking calling read() or read(-1) on wsgi.input is a violation of the WSGI specification even though Apache/mod_wsgi allows it. This is because the WSGI specification requires that a valid length argument be supplied. The WSGI specification also says you shouldn't read more data than is s... | 13 | 2009-06-24T12:31:53Z | [
"python",
"mod-wsgi"
] |
Is there a simple way in Python to create a file which can be written to in one thread and read in a different one? | 394,500 | <p>In the python program I'm writing, I've got a thread which iterates over a large structure in memory and writes it incrementally into a file-like object. I've got another thread which takes a file-like object and writes it to disk. Is there an easy way to connect the two, such that any data input from the first th... | 2 | 2008-12-27T00:25:16Z | 394,503 | <p>You should use the <a href="http://docs.python.org/library/queue.html" rel="nofollow">Queue</a> module for sharing sequential data across threads. You would have to make a file-like Queue subclass, where .read and .write mutually block each other, with a buffer in-between.</p>
<p>OTOH, I wonder why the first thread... | 4 | 2008-12-27T00:32:30Z | [
"python"
] |
Is there a simple way in Python to create a file which can be written to in one thread and read in a different one? | 394,500 | <p>In the python program I'm writing, I've got a thread which iterates over a large structure in memory and writes it incrementally into a file-like object. I've got another thread which takes a file-like object and writes it to disk. Is there an easy way to connect the two, such that any data input from the first th... | 2 | 2008-12-27T00:25:16Z | 394,508 | <p>I'm not clear what you're trying to do ehre. This sounds like a job for a regular old pipe, which is a file-like object. I'm guessing, however, that you mean you're got a stream of some other sort.</p>
<p>It also sounds a lot like what you want is a python <a href="http://docs.python.org/library/queue.html" rel="... | 0 | 2008-12-27T00:36:28Z | [
"python"
] |
Is there a simple way in Python to create a file which can be written to in one thread and read in a different one? | 394,500 | <p>In the python program I'm writing, I've got a thread which iterates over a large structure in memory and writes it incrementally into a file-like object. I've got another thread which takes a file-like object and writes it to disk. Is there an easy way to connect the two, such that any data input from the first th... | 2 | 2008-12-27T00:25:16Z | 394,548 | <p>I think there is something wrong in the design if you already have a file-like object if you want your data to end up in the subprocess. You should then arrange that they get written into the subprocess in the first place, rather than having them written into something else file-like first. Whoever is writing the da... | 1 | 2008-12-27T01:32:40Z | [
"python"
] |
Is there a simple way in Python to create a file which can be written to in one thread and read in a different one? | 394,500 | <p>In the python program I'm writing, I've got a thread which iterates over a large structure in memory and writes it incrementally into a file-like object. I've got another thread which takes a file-like object and writes it to disk. Is there an easy way to connect the two, such that any data input from the first th... | 2 | 2008-12-27T00:25:16Z | 394,549 | <p>Use <a href="http://docs.python.org/library/shutil.html#shutil.copyfileobj" rel="nofollow"><code>shutil</code>'s <code>copyfileobj()</code></a> function:</p>
<pre><code>import shutil
import subprocess
proc = subprocess.Popen([...], stdin=subprocess.PIPE)
my_input = get_filelike_object('from a place not given in t... | 2 | 2008-12-27T01:33:04Z | [
"python"
] |
SDL or PyGame international input | 394,618 | <p>So basically, how is non-western input handled in SDL or OpenGL games or applications? Googling for it reveals <a href="http://sdl-im.csie.net/" rel="nofollow">http://sdl-im.csie.net/</a> but that doesn't seem to be maintained or available anymore. Just to view the page I had to use the <a href="http://74.125.95.1... | 5 | 2008-12-27T03:23:33Z | 400,158 | <p>Usually everybody just ends up using unicode for the text to internationalize their apps.</p>
<p>I don't remember SDL or neither OpenGL implemented anything that'd prevent you from implementing international input/output, except they are neither helping at that.</p>
<p>There's utilities over OpenGL you can use to ... | 1 | 2008-12-30T12:32:25Z | [
"python",
"internationalization",
"sdl"
] |
SDL or PyGame international input | 394,618 | <p>So basically, how is non-western input handled in SDL or OpenGL games or applications? Googling for it reveals <a href="http://sdl-im.csie.net/" rel="nofollow">http://sdl-im.csie.net/</a> but that doesn't seem to be maintained or available anymore. Just to view the page I had to use the <a href="http://74.125.95.1... | 5 | 2008-12-27T03:23:33Z | 685,280 | <p>You are interested in <a href="http://www.libsdl.org/docs/html/sdlenableunicode.html" rel="nofollow"><code>SDL_EnableUNICODE()</code></a>. When you enable unicode translation, you can use the <code>unicode</code> field of <code>SDL_keysym</code> structure to get the unicode character based on the key user typed.</p>... | 3 | 2009-03-26T10:56:18Z | [
"python",
"internationalization",
"sdl"
] |
SDL or PyGame international input | 394,618 | <p>So basically, how is non-western input handled in SDL or OpenGL games or applications? Googling for it reveals <a href="http://sdl-im.csie.net/" rel="nofollow">http://sdl-im.csie.net/</a> but that doesn't seem to be maintained or available anymore. Just to view the page I had to use the <a href="http://74.125.95.1... | 5 | 2008-12-27T03:23:33Z | 930,794 | <p>It appears there is now a Google summer of code project on this topic, for both <a href="http://ocghop.appspot.com/student%5Fproject/show/google/gsoc2009/sdl/t124024854219" rel="nofollow">X11</a> and for <a href="http://socghop.appspot.com/student%5Fproject/show/google/gsoc2009/sdl/t124024853848" rel="nofollow">MacO... | 0 | 2009-05-30T22:40:57Z | [
"python",
"internationalization",
"sdl"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 394,779 | <p>Yes, it's possible:</p>
<pre><code>class Dog:
def bark(self):
print "Woof"
def new_bark(self):
print "Woof Woof"
foo = Dog()
funcType = type(Dog.bark)
# "Woof"
foo.bark()
# replace bark with new_bark for this object only
foo.bark = funcType(new_bark, foo, Dog)
foo.bark()
# "Woof Woof"
</code><... | 80 | 2008-12-27T07:35:02Z | [
"python"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 394,788 | <pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
def new_bark():
print "WoOoOoF!!"
boby.bark = new_bark
boby.bark() # WoOoOoF!!
</code></pre>
<p>You can use the <code>boby</code> variable inside the function if you need. Since you are overriding th... | 20 | 2008-12-27T07:46:45Z | [
"python"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 394,939 | <p>Since functions are first class objects in Python you can pass them while initializing your class object or override it anytime for a given class instance:</p>
<pre><code>class Dog:
def __init__(self, barkmethod=None):
self.bark=self.barkp
if barkmethod:
self.bark=barkmethod
def ... | 0 | 2008-12-27T12:06:04Z | [
"python"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 395,006 | <p>Please do not do this as shown. You code becomes unreadable when you monkeypatch an instance to be different from the class.</p>
<p>You cannot debug monkeypatched code.</p>
<p>When you find a bug in <code>boby</code> and <code>print type(boby)</code>, you'll see that (a) it's a Dog, but (b) for some obscure reas... | 23 | 2008-12-27T13:44:45Z | [
"python"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 395,031 | <p>Though I liked the inheritance idea from S. Lott and agree with the 'type(a)' thing, but since functions too have accessible attributes, I think the it can be managed this way:</p>
<pre><code>class Dog:
def __init__(self, barkmethod=None):
self.bark=self.barkp
if barkmethod:
self.bark... | -2 | 2008-12-27T14:10:24Z | [
"python"
] |
Override a method at instance level | 394,770 | <p>Is there a way in Python to override a class method at instance level?
For example:</p>
<pre><code>class Dog:
def bark(self):
print "WOOF"
boby = Dog()
boby.bark() # WOOF
# METHOD OVERRIDE
boby.bark() # WoOoOoF!!
</code></pre>
| 33 | 2008-12-27T07:12:07Z | 16,977,672 | <p>Dear this is not overriding you are just calling the same function twice with the object. Basically overriding is related to more than one class. when same signature method exist in different classes then which function your are calling this decide the object who calls this. Overriding is possible in python when you... | -2 | 2013-06-07T06:30:39Z | [
"python"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 394,814 | <p>Yes, it was <a href="https://mail.python.org/pipermail/python-dev/2005-September/056846.html">added</a> in version 2.5.<br>
The syntax is:</p>
<pre><code>a if condition else b
</code></pre>
<p>First <code>condition</code> is evaluated, then either <code>a</code> or <code>b</code> is returned based on the <a href="... | 3,110 | 2008-12-27T08:44:19Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 394,815 | <p>From <a href="https://docs.python.org/3/reference/expressions.html#conditional-expressions">the documentation</a>:</p>
<blockquote>
<p>Conditional expressions (sometimes called a âternary operatorâ) have the lowest priority of all Python operations.</p>
<p>The expression <code>x if C else y</code> first ... | 70 | 2008-12-27T08:44:23Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 394,887 | <p>For versions prior to 2.5, there's the trick:</p>
<pre><code>[expression] and [on_true] or [on_false]
</code></pre>
<p>It can give wrong results when <code>on_true</code>
has a false boolean value.<sup>1</sup><br>
Although it does have the benefit of evaluating expressions left to right, which is clearer in my o... | 152 | 2008-12-27T10:48:27Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 470,376 | <p>You can index into a tuple:</p>
<pre><code>(falseValue, trueValue)[test]
</code></pre>
<p><code>test</code> needs to return <em>True</em> or <em>False</em>.<br>
It might be safer to always implement it as:</p>
<pre><code>(falseValue, trueValue)[test == True]
</code></pre>
<p>or you can use the built-in <a href="... | 359 | 2009-01-22T18:58:09Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 1,855,173 | <p>@up:</p>
<p>Unfortunately, the</p>
<pre><code>(falseValue, trueValue)[test]
</code></pre>
<p>solution doesn't have short-circuit behaviour; thus both falseValue and trueValue are evaluated regardless of the condition. This could be suboptimal or even buggy (i.e. both trueValue and falseValue could be methods and ... | 40 | 2009-12-06T11:51:50Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 2,919,360 | <p><em>expression1</em> if <em>condition</em> else <em>expression2</em></p>
<pre><code>>>> a = 1
>>> b = 2
>>> 1 if a > b else -1
-1
>>> 1 if a > b else -1 if a < b else 0
-1
</code></pre>
| 79 | 2010-05-27T07:56:06Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 10,314,837 | <p>For Python 2.5 and newer there is a specific syntax:</p>
<pre><code>[on_true] if [cond] else [on_false]
</code></pre>
<p>In older Pythons a ternary operator is not implemented but it's possible to simulate it.</p>
<pre><code>cond and on_true or on_false
</code></pre>
<p>Though, there is a potential problem, whic... | 30 | 2012-04-25T11:40:11Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 14,321,907 | <p>You might often find</p>
<pre><code>cond and on_true or on_false
</code></pre>
<p>but this lead to problem when on_true == 0</p>
<pre><code>>>> x = 0
>>> print x == 0 and 0 or 1
1
>>> x = 1
>>> print x == 0 and 0 or 1
1
</code></pre>
<p>where you would expect for a normal t... | 19 | 2013-01-14T15:56:09Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 20,093,702 | <p>Simulating the python ternary operator.</p>
<p>For example</p>
<pre><code>a, b, x, y = 1, 2, 'a greather than b', 'b greater than a'
result = (lambda:y, lambda:x)[a > b]()
</code></pre>
<p>output:</p>
<pre><code>'b greater than a'
</code></pre>
| 10 | 2013-11-20T10:44:12Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 23,518,594 | <pre><code>In [1]: a = 1 if False else 0
In [2]: a
Out[2]: 0
In [3]: b = 1 if True else 0
In [4]: b
Out[4]: 1
</code></pre>
| 6 | 2014-05-07T13:02:06Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 30,052,371 | <p>An operator for a conditional expression in Python was added in 2006 as part of <a href="https://www.python.org/dev/peps/pep-0308/">Python Enhancement Proposal 308</a>. Its form differ from common <code>?:</code> operator and it's:</p>
<pre><code><expression1> if <condition> else <expression2>
</c... | 33 | 2015-05-05T12:00:09Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 31,429,254 | <p>Absolutely, and it is incredibly easy to understand. </p>
<pre><code>general syntax : first_expression if bool_expression== true else second_expression
Example: x= 3 if 3 > 2 else 4
# assigns 3 to x if the boolean expression evaluates to true or 4 if it is false
</code></pre>
| 11 | 2015-07-15T11:33:41Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 33,765,206 | <blockquote>
<h1>Does Python have a ternary conditional operator?</h1>
</blockquote>
<p>Yes. From the <a href="https://docs.python.org/reference/grammar.html" rel="nofollow">grammar file</a>:</p>
<pre><code>test: or_test ['if' or_test 'else' test] | lambdef
</code></pre>
<p>The part of interest is:</p>
<pre><code... | 12 | 2015-11-17T19:14:33Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 36,041,689 | <p>Yes.</p>
<pre><code>>>> b = (True if 5 > 4 else False)
>>> print b
True
</code></pre>
| 2 | 2016-03-16T16:36:22Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 37,155,553 | <p>More a tip than an answer (don't need to repeat the obvious for the hundreth time), but I sometimes use it as a oneliner shortcut in such constructs:</p>
<pre><code>if conditionX:
print('yes')
else:
print('nah')
</code></pre>
<p>, becomes:</p>
<pre><code>print('yes') if conditionX else print('nah')
</code... | 5 | 2016-05-11T07:13:31Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Does Python have a ternary conditional operator? | 394,809 | <p>If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?</p>
| 2,655 | 2008-12-27T08:32:18Z | 39,067,220 | <h2><strong>Ternary Operator in different programming Languages</strong></h2>
<p>Here I just try to show some important difference in <code>ternary operator</code> between a couple of programming languages.</p>
<blockquote>
<p><em>Ternary Operator in Javascript</em></p>
</blockquote>
<pre><code>var a = true ? 1 : ... | 3 | 2016-08-21T17:56:26Z | [
"python",
"operators",
"ternary-operator",
"conditional-operator",
"python-2.5"
] |
Python threads stack_size and segfaults | 394,895 | <p>A web crawler script that spawns at most 500 threads and each thread basically requests for certain data served from the remote server, which each server's reply is different in content and size from others.</p>
<p>i'm setting stack_size as 756K's for threads</p>
<pre><code>threading.stack_size(756*1024)
</code></... | 2 | 2008-12-27T11:03:00Z | 394,916 | <p><strong>Why</strong> on earth are you spawning 500 threads? That seems like a terrible idea! </p>
<p>Remove threading completely, use an event loop to do the crawling. Your program will be faster, simpler, and easier to maintain.</p>
<p>Lots of threads waiting for network won't make your program <em>wait faster</e... | 9 | 2008-12-27T11:28:29Z | [
"python",
"multithreading",
"segmentation-fault",
"stack-size"
] |
Download from EXPLOSM.net Comics Script [Python] | 394,978 | <p>So I wrote this short script (correct word?) to download the comic images from explosm.net comics because I somewhat-recently found out about it and I want to...put it on my iPhone...3G.</p>
<p>It works fine and all. urllib2 for getting webpage html and urllib for image.retrieve()</p>
<p><strong><em>Why I posted t... | 1 | 2008-12-27T13:09:10Z | 395,014 | <p>I suggest using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> to do the parsing, it would simplifly your code a lot.</p>
<p>But since you already got it working this way maybe you won't want to touch it until it breaks (page format changes).</p>
| 0 | 2008-12-27T13:54:56Z | [
"python",
"scripting",
"download",
"urllib"
] |
Download from EXPLOSM.net Comics Script [Python] | 394,978 | <p>So I wrote this short script (correct word?) to download the comic images from explosm.net comics because I somewhat-recently found out about it and I want to...put it on my iPhone...3G.</p>
<p>It works fine and all. urllib2 for getting webpage html and urllib for image.retrieve()</p>
<p><strong><em>Why I posted t... | 1 | 2008-12-27T13:09:10Z | 395,026 | <p>I would suggest using <a href="http://scrapy.org/" rel="nofollow">Scrapy</a> for your page fetching and <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a> for the parsing. This would make your code a lot simpler.</p>
<p>Whether you want to change your existing code that works ... | 7 | 2008-12-27T14:04:48Z | [
"python",
"scripting",
"download",
"urllib"
] |
Download from EXPLOSM.net Comics Script [Python] | 394,978 | <p>So I wrote this short script (correct word?) to download the comic images from explosm.net comics because I somewhat-recently found out about it and I want to...put it on my iPhone...3G.</p>
<p>It works fine and all. urllib2 for getting webpage html and urllib for image.retrieve()</p>
<p><strong><em>Why I posted t... | 1 | 2008-12-27T13:09:10Z | 395,066 | <p><a href="http://refactormycode.com/" rel="nofollow">refactormycode</a> may be a more appropriate web site for these "let's improve this code" type of discussions.</p>
| 3 | 2008-12-27T14:53:42Z | [
"python",
"scripting",
"download",
"urllib"
] |
Download from EXPLOSM.net Comics Script [Python] | 394,978 | <p>So I wrote this short script (correct word?) to download the comic images from explosm.net comics because I somewhat-recently found out about it and I want to...put it on my iPhone...3G.</p>
<p>It works fine and all. urllib2 for getting webpage html and urllib for image.retrieve()</p>
<p><strong><em>Why I posted t... | 1 | 2008-12-27T13:09:10Z | 395,469 | <p>urllib2 uses blocking calls, and that's the main reason for performance.
You should use a non-blocking library (like scrapy) or use multiple threads for the retrieval. I have never used scrapy (so I can't tell on that option), but threading in python is really easy and straightforward.</p>
| 0 | 2008-12-27T21:56:24Z | [
"python",
"scripting",
"download",
"urllib"
] |
templatetags don't refresh | 395,053 | <p>I have two templatetags in my app which contain forms which show entries in db. When I alter data or add new entry to db, the forms show the old data. While in admin panel everything is correct (updated). When I restart the server (I mean <code>manage.py runserver</code>) forms show updated db entries. How to make ... | 0 | 2008-12-27T14:43:10Z | 395,087 | <p>Are you using some kind of cache system? It could be that.</p>
| 0 | 2008-12-27T15:24:04Z | [
"python",
"django",
"django-templates",
"templatetags"
] |
templatetags don't refresh | 395,053 | <p>I have two templatetags in my app which contain forms which show entries in db. When I alter data or add new entry to db, the forms show the old data. While in admin panel everything is correct (updated). When I restart the server (I mean <code>manage.py runserver</code>) forms show updated db entries. How to make ... | 0 | 2008-12-27T14:43:10Z | 397,219 | <p>Look for "CACHE_BACKEND= ????" in your settings.py file. The value will change as a function of which caching mechanism you are using. Comment this out and restart the server. If your values are now showing correctly, then it was a caching problem.</p>
| 0 | 2008-12-29T06:10:09Z | [
"python",
"django",
"django-templates",
"templatetags"
] |
templatetags don't refresh | 395,053 | <p>I have two templatetags in my app which contain forms which show entries in db. When I alter data or add new entry to db, the forms show the old data. While in admin panel everything is correct (updated). When I restart the server (I mean <code>manage.py runserver</code>) forms show updated db entries. How to make ... | 0 | 2008-12-27T14:43:10Z | 30,299,070 | <p>I have too low rep to comment, but <code>takes_context</code> defaults to False, making your assignment redundant. Also, but now I am guessing, but it might be related to your problem.</p>
| 0 | 2015-05-18T09:06:38Z | [
"python",
"django",
"django-templates",
"templatetags"
] |
Can I use a ForeignKey in __unicode__ return? | 395,340 | <p>I have the following classes: Ingredients, Recipe and RecipeContent...</p>
<pre><code>class Ingredient(models.Model):
name = models.CharField(max_length=30, primary_key=True)
qty_on_stock = models.IntegerField()
def __unicode__(self):
return self.name
class Recipe(models.Model):
name = mod... | 11 | 2008-12-27T19:58:22Z | 395,347 | <pre><code>class RecipeContent(models.Model):
...
def __unicode__(self):
# You can access ForeignKey properties through the field name!
return self.recipe.name
</code></pre>
| 24 | 2008-12-27T20:02:38Z | [
"python",
"django",
"django-models"
] |
Can I use a ForeignKey in __unicode__ return? | 395,340 | <p>I have the following classes: Ingredients, Recipe and RecipeContent...</p>
<pre><code>class Ingredient(models.Model):
name = models.CharField(max_length=30, primary_key=True)
qty_on_stock = models.IntegerField()
def __unicode__(self):
return self.name
class Recipe(models.Model):
name = mod... | 11 | 2008-12-27T19:58:22Z | 403,724 | <p>Yes, you can (as bishanty points), but be prepared for situation when <code>__unicode__()</code> is called but FK is not set yet. I came into this few times.</p>
| 0 | 2008-12-31T18:31:53Z | [
"python",
"django",
"django-models"
] |
Can I use a ForeignKey in __unicode__ return? | 395,340 | <p>I have the following classes: Ingredients, Recipe and RecipeContent...</p>
<pre><code>class Ingredient(models.Model):
name = models.CharField(max_length=30, primary_key=True)
qty_on_stock = models.IntegerField()
def __unicode__(self):
return self.name
class Recipe(models.Model):
name = mod... | 11 | 2008-12-27T19:58:22Z | 6,714,575 | <p>If you only care about the name part of the Recipe, you can do:</p>
<pre><code>class Recipe(models.Model):
name = models.CharField(max_length=30, primary_key=True)
comments = models.TextField(blank=True)
...
def __unicode__(self):
return self.name
class RecipeContent(models.Model):
rec... | 1 | 2011-07-16T01:01:29Z | [
"python",
"django",
"django-models"
] |
How to download a file over http with authorization in python 3.0, working around bugs? | 395,451 | <p>I have a script that I'd like to continue using, but it looks like I either have to find some workaround for a bug in Python 3, or downgrade back to 2.6, and thus having to downgrade other scripts as well...</p>
<p>Hopefully someone here have already managed to find a workaround.</p>
<p>The problem is that due to ... | 6 | 2008-12-27T21:45:47Z | 395,481 | <p>Direct from the Py3k docs: <a href="http://docs.python.org/dev/py3k/library/urllib.request.html#examples">http://docs.python.org/dev/py3k/library/urllib.request.html#examples</a></p>
<pre><code>import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.reque... | 19 | 2008-12-27T22:04:53Z | [
"python",
"python-3.x",
"urllib"
] |
How to download a file over http with authorization in python 3.0, working around bugs? | 395,451 | <p>I have a script that I'd like to continue using, but it looks like I either have to find some workaround for a bug in Python 3, or downgrade back to 2.6, and thus having to downgrade other scripts as well...</p>
<p>Hopefully someone here have already managed to find a workaround.</p>
<p>The problem is that due to ... | 6 | 2008-12-27T21:45:47Z | 395,665 | <p>My advice would be to maintain your 2.* branch as your production branch until you can get the 3.0 stuff sorted.</p>
<p>I am going to wait a while before moving over to Python 3.0. There seems a lot of people in a rush, but I just want everything sorted out, and a decent selection of third-party libraries. This may... | 0 | 2008-12-28T01:21:20Z | [
"python",
"python-3.x",
"urllib"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,546 | <p>You can use the BSD-alike(?) <a href="http://macports.org" rel="nofollow">http://macports.org</a>, which provides gentoo-like build-it-yourself installations of a wide swath of software you'd expect to find in a Linux distro.</p>
<p>Alternatively you could just run Ubuntu in a virtual machine as your test stage.</p... | 2 | 2008-12-27T22:53:35Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,547 | <p>The <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29" rel="nofollow">Python</a> mysqldb packge <a href="http://pdb.finkproject.org/pdb/package.php/mysql-python-py25" rel="nofollow">is in fink</a>, but it's in the unstable tree. Just <a href="http://www.finkproject.org/faq/usage-fink.php#unstab... | 0 | 2008-12-27T22:54:23Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,568 | <p>Did the MySQL and MySQL-dev installations go smoothly? Can you run MySQL, connect to it and so on? Does <code>/usr/local/mysql/include</code> contain lots of header files? (I've got 46 header files there, for reference).</p>
<p>If so, MySQL should be good to go. There are still a few manual steps required to compil... | 12 | 2008-12-27T23:09:42Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,574 | <p>Assuming this is just a development environment, you could try using <a href="http://en.wikipedia.org/wiki/SQLite" rel="nofollow">SQLite</a> 3 as your database. Mileage may vary depending on how low-level you need to get with the database for your specific application.</p>
| 2 | 2008-12-27T23:15:34Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,669 | <p>I put a step-by-step guide in a blog post that might help: <em><a href="http://www.djangrrl.com/view/installing-django-with-mysql-on-mac-os-x/" rel="nofollow">Installing Django with MySQL on Mac OS X</a></em>.</p>
| 0 | 2008-12-28T01:30:00Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 395,680 | <p>If you are a Mac user, install <a href="http://www.macports.org/" rel="nofollow">MacPorts</a>. Almost any Linux/Unix application is available through "port". It is simple and easy to manage.</p>
| 0 | 2008-12-28T01:48:04Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
How do I install MySQL and the Python MySQL package on OS X Leopard? Or how do I learn about being a web developer using OS X? | 395,509 | <p>I'm new to the Mac OS X, and I'm just about ready to throw my brand new <a href="http://en.wikipedia.org/wiki/MacBook_Pro" rel="nofollow">MacBook Pro</a> out the window. Every tutorial on setting up a Django development environment on <a href="http://en.wikipedia.org/wiki/Mac_OS_X_Leopard" rel="nofollow">Mac OS... | 6 | 2008-12-27T22:23:48Z | 396,736 | <p>Install <a href="http://en.wikipedia.org/wiki/MacPorts" rel="nofollow">MacPorts</a>. Run <code>port install mysql</code>. It worked for me!</p>
| 0 | 2008-12-28T21:54:09Z | [
"python",
"mysql",
"django",
"osx",
"sysadmin"
] |
Threads in Python | 395,704 | <p>General tutorial or good resource on how to use threads in Python?</p>
<p>When to use threads, how they are effective, and some general background on threads [specific to Python]?</p>
| 6 | 2008-12-28T02:22:38Z | 395,716 | <p>Threads should be used when you want two things to run at once, or want something to run in the background without slowing down the main process.<br />
My recommendation is to only use threads if you have to. They generally add complexity to a program.<br />
The main documentation for threading is here: <a href="htt... | 13 | 2008-12-28T02:40:43Z | [
"python",
"multithreading"
] |
Threads in Python | 395,704 | <p>General tutorial or good resource on how to use threads in Python?</p>
<p>When to use threads, how they are effective, and some general background on threads [specific to Python]?</p>
| 6 | 2008-12-28T02:22:38Z | 395,726 | <p>There are several tutorials <a href="http://python.objectis.net/index_html#thread" rel="nofollow">here</a>. </p>
| 1 | 2008-12-28T02:54:25Z | [
"python",
"multithreading"
] |
Threads in Python | 395,704 | <p>General tutorial or good resource on how to use threads in Python?</p>
<p>When to use threads, how they are effective, and some general background on threads [specific to Python]?</p>
| 6 | 2008-12-28T02:22:38Z | 395,728 | <p>There is a fantastic pdf, <a href="http://heather.cs.ucdavis.edu/~matloff/158/PLN/ParProcBook.pdf" rel="nofollow">Tutorial on Threads Programming with Python</a> by
Norman Matloff and Francis Hsu, of University of California, Davis.</p>
<p>Threads should be avoided whenever possible. They add much in complexity, ... | 3 | 2008-12-28T02:54:56Z | [
"python",
"multithreading"
] |
Threads in Python | 395,704 | <p>General tutorial or good resource on how to use threads in Python?</p>
<p>When to use threads, how they are effective, and some general background on threads [specific to Python]?</p>
| 6 | 2008-12-28T02:22:38Z | 396,055 | <p>One thing to remember before spending time and effort in writing a multi-threaded Python application is that there is a <a href="http://en.wikipedia.org/wiki/Global_Interpreter_Lock">Global Interpreter Lock</a> (GIL), so you won't actually be running more than one thread at a time.</p>
<p>This makes threading unsui... | 8 | 2008-12-28T10:22:05Z | [
"python",
"multithreading"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 395,741 | <pre><code>>>> class X(object):
... pass
...
>>> type(X)
<type 'type'>
>>> isinstance(X,type)
True
</code></pre>
| 17 | 2008-12-28T03:11:54Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 395,782 | <p>Even better: use the <a href="https://docs.python.org/library/inspect.html#inspect.isclass"><code>inspect.isclass</code></a> function.</p>
<pre><code>>>> import inspect
>>> class X(object):
... pass
...
>>> inspect.isclass(X)
True
>>> x = X()
>>> isinstance(x, X)
T... | 178 | 2008-12-28T03:47:00Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 395,784 | <p>class Foo: is called old style class and class X(object): is called new style class. </p>
<p>Check this <a href="http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python">http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python</a> . New style is recommended. Read... | 1 | 2008-12-28T03:48:43Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 10,045,680 | <pre><code>isinstance(X, type)
</code></pre>
<p>Return <code>True</code> if <code>X</code> is class and <code>False</code> if not.</p>
| 4 | 2012-04-06T15:34:35Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 10,123,520 | <p>The inspect.isclass is probably the best solution, and it's really easy to see how it's actually implemented</p>
<pre><code>def isclass(object):
"""Return true if the object is a class.
Class objects provide these attributes:
__doc__ documentation string
__module__ name of modu... | 29 | 2012-04-12T12:24:08Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 11,179,404 | <p>There are some working solutions here already, but here's another one:</p>
<pre><code>>>> import types
>>> class Dummy: pass
>>> type(Dummy) is types.ClassType
True
</code></pre>
| 0 | 2012-06-24T17:00:17Z | [
"python",
"reflection"
] |
How to check whether a variable is a class or not? | 395,735 | <p>I was wondering how to check whether a variable is a class (not an instance!) or not.</p>
<p>I've tried to use the function <code>isinstance(object, class_or_type_or_tuple)</code> to do this, but I don't know what type would a class will have.</p>
<p>For example, in the following code</p>
<pre><code>class Foo: pa... | 119 | 2008-12-28T03:08:05Z | 34,406,582 | <p>That's a little hackish, but an option if you don't want to avoid imports:</p>
<pre><code>def isclass(obj):
try:
issubclass(obj, object)
except TypeError:
return False
else:
return True
</code></pre>
| 0 | 2015-12-22T00:18:59Z | [
"python",
"reflection"
] |
How do I send an ARP packet through python on windows without needing winpcap? | 395,846 | <p>Is there any way to send ARP packet on Windows without the use of another library such as winpcap?</p>
<p>I have heard that Windows XP SP2 blocks raw ethernet sockets, but I have also heard that raw sockets are only blocked for administrators. Any clarification here?</p>
| 3 | 2008-12-28T04:52:54Z | 395,921 | <p>There is no way to do that in the general case without the use of an external library.</p>
<p>If there are no requirements on what the packet should contain (i.e., if any ARP packet will do) then you <em>can</em> obviously send an ARP request if you're on an Ethernet network simply by trying to send something to an... | 3 | 2008-12-28T06:36:50Z | [
"python",
"sockets",
"ethernet",
"arp"
] |
How do I send an ARP packet through python on windows without needing winpcap? | 395,846 | <p>Is there any way to send ARP packet on Windows without the use of another library such as winpcap?</p>
<p>I have heard that Windows XP SP2 blocks raw ethernet sockets, but I have also heard that raw sockets are only blocked for administrators. Any clarification here?</p>
| 3 | 2008-12-28T04:52:54Z | 503,144 | <p>You could use the OpenVPN tap to send arbitrary packets as if you where using raw sockets.</p>
| 0 | 2009-02-02T13:10:46Z | [
"python",
"sockets",
"ethernet",
"arp"
] |
"MetaClass", "__new__", "cls" and "super" - what is the mechanism exactly? | 395,982 | <p>I have read posts like these:</p>
<ol>
<li><a href="http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python?</a> </li>
<li><a href="http://stackoverflow.com/questions/392160/what-are-your-concrete-use-cases-for-metaclasses-in-python">What are your (concrete) use-cases ... | 23 | 2008-12-28T08:41:45Z | 396,109 | <p>OK, you've thrown quite a few concepts into the mix here! I'm going to pull out a few of the specific questions you have.</p>
<p>In general, understanding super, the MRO and metclasses is made much more complicated because there have been lots of changes in this tricky area over the last few versions of Python.</p>... | 14 | 2008-12-28T11:36:08Z | [
"python",
"types",
"metaclass",
"super"
] |
"MetaClass", "__new__", "cls" and "super" - what is the mechanism exactly? | 395,982 | <p>I have read posts like these:</p>
<ol>
<li><a href="http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python?</a> </li>
<li><a href="http://stackoverflow.com/questions/392160/what-are-your-concrete-use-cases-for-metaclasses-in-python">What are your (concrete) use-cases ... | 23 | 2008-12-28T08:41:45Z | 396,192 | <p>Here's the more pragmatic answer.</p>
<p><strong>It rarely matters</strong></p>
<ol>
<li><p>"<a href="http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python</a>". Bottom line, <code>type</code> is the metaclass of all classes. You have almost no practical use for t... | 9 | 2008-12-28T14:11:03Z | [
"python",
"types",
"metaclass",
"super"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,430 | <p>I think the first one is the "obvious" way. It is shorter, clearer, and likely to be faster in many cases because Python's built-in sort is highly optimized.</p>
| 7 | 2008-12-28T17:33:11Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,436 | <p>Your second example won't actually work:</p>
<pre><code>all(a.count(char) == b.count(char) for char in a)
</code></pre>
<p>will only work if b does not contain extra characters not in a. It also does duplicate work if the characters in string a repeat.</p>
<p>If you want to know whether two strings are permutatio... | 5 | 2008-12-28T17:39:08Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,438 | <p>Here is a way which is O(n), asymptotically better than the two ways you suggest. </p>
<pre><code>import collections
def same_permutation(a, b):
d = collections.defaultdict(int)
for x in a:
d[x] += 1
for x in b:
d[x] -= 1
return not any(d.itervalues())
## same_permutation([1,2,3],[... | 14 | 2008-12-28T17:40:03Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,445 | <p>Go with the first one - it's much more straightforward and easier to understand. If you're actually dealing with incredibly large strings and performance is a real issue, then don't use Python, use something like C.</p>
<p>As far as the Zen of Python is concerned, that there should only be one obvious way to do th... | 1 | 2008-12-28T17:43:14Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,460 | <p>Here are some timed executions on very small strings, using two different methods:<br>
1. sorting<br>
2. counting (specifically the original method by @namin).</p>
<pre><code>a, b, c = 'confused', 'unfocused', 'foncused'
sort_method = lambda x,y: sorted(x) == sorted(y)
def count_method(a, b):
d = {}
for x... | 2 | 2008-12-28T17:54:00Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,523 | <p>"but the first one is slower when (for example) the first char of a is nowhere in b".</p>
<p>This kind of degenerate-case performance analysis is not a good idea. It's a rat-hole of lost time thinking up all kinds of obscure special cases. </p>
<p>Only do the <strong>O</strong>-style "overall" analysis.</p>
<p>... | 14 | 2008-12-28T18:55:35Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 396,570 | <p>heuristically you're probably better to split them off based on string size.</p>
<p>Pseudocode:</p>
<pre><code>returnvalue = false
if len(a) == len(b)
if len(a) < threshold
returnvalue = (sorted(a) == sorted(b))
else
returnvalue = naminsmethod(a, b)
return returnvalue
</code></pre>
<p>If per... | 7 | 2008-12-28T19:36:38Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 397,055 | <p>This is a PHP function I wrote about a week ago which checks if two words are anagrams. How would this compare (if implemented the same in python) to the other methods suggested? Comments?</p>
<pre><code>public function is_anagram($word1, $word2) {
$letters1 = str_split($word1);
$letters2 = str_split($word2... | 0 | 2008-12-29T03:02:50Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 397,334 | <p>This version is faster than any examples presented so far except it is 20% slower than <code>sorted(x) == sorted(y)</code> for short strings. It depends on use cases but generally 20% performance gain is insufficient to justify a complication of the code by using different version for short and long strings (as in @... | 0 | 2008-12-29T08:34:26Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 408,151 | <p>Sorry that my code is not in Python, I have never used it, but I am sure this can be easily translated into python. I believe this is faster than all the other examples already posted. It is also O(n), but stops as soon as possible:</p>
<pre><code>public boolean isPermutation(String a, String b) {
if (a.length(... | 1 | 2009-01-02T22:11:40Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 408,162 | <p>Here's martinus code in python. It only works for ascii strings:</p>
<pre><code>def is_permutation(a, b):
if len(a) != len(b):
return False
char_count = [0] * 256
for c in a:
char_count[ord(c)] += 1
for c in b:
char_count[ord(c)] -= 1
if char_count[ord(c)] < 0:
... | 1 | 2009-01-02T22:17:38Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 426,629 | <p>I did a pretty thorough comparison in Java with all words in a book I had. The counting method beats the sorting method in every way. The results:</p>
<pre><code>Testing against 9227 words.
Permutation testing by sorting ... done. 18.582 s
Permutation testing by counting ... done. 14.949 s
</code></pre>
<p... | 1 | 2009-01-09T00:26:04Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 2,021,817 | <p>In Python 3.1/2.7 you can just use <code>collections.Counter(a) == collections.Counter(b)</code>.</p>
<p>But <code>sorted(a) == sorted(b)</code> is still the most obvious IMHO. You are talking about permutations - changing order - so sorting is the obvious operation to erase that difference.</p>
| 1 | 2010-01-07T16:23:02Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 19,896,549 | <p>This is derived from <a href="http://stackoverflow.com/a/396570/72321">@patros' answer</a>.</p>
<pre><code>from collections import Counter
def is_anagram(a, b, threshold=1000000):
"""Returns true if one sequence is a permutation of the other.
Ignores whitespace and character case.
Compares sorted sequ... | 0 | 2013-11-10T23:24:08Z | [
"algorithm",
"python"
] |
Checking if two strings are permutations of each other in Python | 396,421 | <p>I'm checking if two strings <code>a</code> and <code>b</code> are permutations of each other, and I'm wondering what the ideal way to do this is in Python. From the Zen of Python, "There should be one -- and preferably only one -- obvious way to do it," but I see there are at least two ways:</p>
<pre><code>sorted(a... | 15 | 2008-12-28T17:25:17Z | 35,800,655 | <p>In Swift (or another languages implementation), you could look at the encoded values ( in this case Unicode) and see if they match. </p>
<p>Something like:</p>
<pre><code>let string1EncodedValues = "Hello".unicodeScalars.map() {
//each encoded value
$0
//Now add the values
}.reduce(0){ total, value in
total + ... | 0 | 2016-03-04T16:07:49Z | [
"algorithm",
"python"
] |
Python-PostgreSQL psycopg2 interface --> executemany | 396,455 | <p>I am currently analyzing a wikipedia dump file; I am extracting a bunch of data from it using python and persisting it into a PostgreSQL db. I am always trying to make things go faster for this file is huge (18GB). In order to interface with PostgreSQL, I am using psycopg2, but this module seems to mimic many other ... | 6 | 2008-12-28T17:51:48Z | 396,824 | <p>"When my script detects such a non-UNIQUE INSERT, it connection.rollback() (which makes ups to 1000 rows everytime, and kind of makes the executemany worthless) and then INSERTs all values one by one."</p>
<p>The question doesn't really make a lot of sense.</p>
<p>Does EVERY block of 1,000 rows fail due to non-uni... | 0 | 2008-12-28T23:06:15Z | [
"python",
"postgresql",
"database",
"psycopg"
] |
Python-PostgreSQL psycopg2 interface --> executemany | 396,455 | <p>I am currently analyzing a wikipedia dump file; I am extracting a bunch of data from it using python and persisting it into a PostgreSQL db. I am always trying to make things go faster for this file is huge (18GB). In order to interface with PostgreSQL, I am using psycopg2, but this module seems to mimic many other ... | 6 | 2008-12-28T17:51:48Z | 550,849 | <p>just copy all the data into a scratch table with the psql \copy command, or use the psycopg cursor.copy_in() method. Then:</p>
<pre><code>insert into mytable
select * from (
select distinct *
from scratch
) uniq
where not exists (
select 1
from mytable
where mytable.mykey = uniq.mykey
);
</co... | 8 | 2009-02-15T13:13:40Z | [
"python",
"postgresql",
"database",
"psycopg"
] |
Python-PostgreSQL psycopg2 interface --> executemany | 396,455 | <p>I am currently analyzing a wikipedia dump file; I am extracting a bunch of data from it using python and persisting it into a PostgreSQL db. I am always trying to make things go faster for this file is huge (18GB). In order to interface with PostgreSQL, I am using psycopg2, but this module seems to mimic many other ... | 6 | 2008-12-28T17:51:48Z | 675,865 | <p>using a MERGE statement instead of an INSERT one would solve your problem.</p>
| -1 | 2009-03-24T01:32:07Z | [
"python",
"postgresql",
"database",
"psycopg"
] |
Python-PostgreSQL psycopg2 interface --> executemany | 396,455 | <p>I am currently analyzing a wikipedia dump file; I am extracting a bunch of data from it using python and persisting it into a PostgreSQL db. I am always trying to make things go faster for this file is huge (18GB). In order to interface with PostgreSQL, I am using psycopg2, but this module seems to mimic many other ... | 6 | 2008-12-28T17:51:48Z | 11,059,350 | <p>I had the same problem and searched here for many days to collect a lot of hints to form a complete solution. Even if the question outdated, I hope this will be useful to others.</p>
<p>1) Forget things about removing indexes/constraints & recreating them later, benefits are marginal or worse.</p>
<p>2) execut... | 3 | 2012-06-15T23:24:35Z | [
"python",
"postgresql",
"database",
"psycopg"
] |
Desktop graphics - or "skinned" windows | 396,791 | <p>I'm looking for a way to draw animations right on the desktop. No window frames and with transparent background.</p>
<p>I'm using Python in windows XP for it, but it doesn't have to be cross platform, although it'd be a nice bonus.</p>
<p>Does anyone know about a python library that can do this?</p>
| 3 | 2008-12-28T22:41:47Z | 396,900 | <p>If you want a frameless window, there are several options. For example, pygame can be initialized with the following flag:</p>
<pre><code>pygame.init()
screen = pygame.display.set_mode(size=(640,480), pygame.NOFRAME)
</code></pre>
<p>Your question doesn't make it clear if you're looking for a transparent surface, ... | 2 | 2008-12-29T00:25:56Z | [
"python",
"graphics",
"desktop",
"skinning",
"shaped-window"
] |
Desktop graphics - or "skinned" windows | 396,791 | <p>I'm looking for a way to draw animations right on the desktop. No window frames and with transparent background.</p>
<p>I'm using Python in windows XP for it, but it doesn't have to be cross platform, although it'd be a nice bonus.</p>
<p>Does anyone know about a python library that can do this?</p>
| 3 | 2008-12-28T22:41:47Z | 455,878 | <p>I found that wxPython was able to do this after all. You always find things when you've stopped looking for some reason. I stumbled upon the shaped window setting when I was looking through the demo file for wxPython.</p>
<p>I haven't been able to find any good tutorials for it on the net, but the code in the demo ... | 1 | 2009-01-18T20:57:58Z | [
"python",
"graphics",
"desktop",
"skinning",
"shaped-window"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 396,871 | <p>You can instantiate the class by declaring a variable and calling the class as if it were a function:</p>
<pre><code>x = mystuff()
print x.average(9,18,27)
</code></pre>
<p>However, this won't work with the code you gave us. When you call a class method on a given object (x), it always passes a pointer to the obj... | 46 | 2008-12-28T23:48:05Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 396,875 | <p>In python member function of a class need explicit <code>self</code> argument. Same as implicit <code>this</code> pointer in C++. For more details please check out <a href="http://diveintopython.net/object_oriented_framework/defining_classes.html" rel="nofollow">this</a> page.</p>
| 1 | 2008-12-28T23:50:42Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 396,876 | <p>From your example, it seems to me you want to use a static method.</p>
<pre><code>class mystuff:
@staticmethod
def average(a,b,c): #get the average of three numbers
result=a+b+c
result=result/3
return result
print mystuff.average(9,18,27)
</code></pre>
<p>Please note that an heavy usage of static ... | 30 | 2008-12-28T23:51:21Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 396,884 | <p>You never created an instance.</p>
<p>You've defined average as an instance method, thus, in order to use average you need to create an instance first.</p>
| -1 | 2008-12-29T00:03:20Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 396,968 | <p>You need to spend a little more time on some fundamentals of object-oriented programming.</p>
<p>This sounds harsh, but it's important.</p>
<ul>
<li><p>Your class definition is incorrect -- although the syntax happens to be acceptable. The definition is simply wrong. </p></li>
<li><p>Your use of the class to crea... | 1 | 2008-12-29T01:46:17Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 397,076 | <p>Every function inside a class, and every class variable must take the <em>self</em> argument as pointed.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
result=result/3
return result
def sum(self,a,b):
return a+... | 4 | 2008-12-29T03:23:37Z | [
"python",
"python-3.x"
] |
How to instantiate a class in python | 396,856 | <p>So, I'm trying to learn Python. It seems pretty easy, but evidently, I don't understand how classes are used. The following code gives me an error when I try to use the class.</p>
<pre><code>class mystuff:
def average(a,b,c): #get the average of three numbers
result=a+b+c
r... | 41 | 2008-12-28T23:36:46Z | 23,840,853 | <p>To minimally modify your example, you could amend the code to:</p>
<pre><code>class myclass(object):
def __init__(self): # this method creates the class object.
pass
def average(self,a,b,c): #get the average of three numbers
result=a+b+c
result=result... | 7 | 2014-05-24T02:51:04Z | [
"python",
"python-3.x"
] |
In Python, how do I find the date of the first Monday of a given week? | 396,913 | <p>If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?</p>
<p>Many thanks</p>
| 26 | 2008-12-29T00:42:56Z | 396,926 | <pre><code>>>> import time
>>> time.asctime(time.strptime('2008 50 1', '%Y %W %w'))
'Mon Dec 15 00:00:00 2008'
</code></pre>
<p>Assuming the first day of your week is Monday, use <code>%U</code> instead of <code>%W</code> if the first day of your week is Sunday. See the documentation for <a href="ht... | 33 | 2008-12-29T01:05:22Z | [
"python",
"datetime"
] |
In Python, how do I find the date of the first Monday of a given week? | 396,913 | <p>If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?</p>
<p>Many thanks</p>
| 26 | 2008-12-29T00:42:56Z | 396,927 | <p>This seems to work, assuming week one can have a Monday falling on a day in the last year. </p>
<pre><code>from datetime import date, timedelta
def get_first_dow(year, week):
d = date(year, 1, 1)
d = d - timedelta(d.weekday())
dlt = timedelta(days = (week - 1) * 7)
return d + dlt
</code></pre>
| 8 | 2008-12-29T01:05:54Z | [
"python",
"datetime"
] |
In Python, how do I find the date of the first Monday of a given week? | 396,913 | <p>If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?</p>
<p>Many thanks</p>
| 26 | 2008-12-29T00:42:56Z | 396,928 | <p>Use the string formatting found in the time module. <a href="http://www.python.org/doc/current/library/time.html#time.strftime" rel="nofollow">Detailed explanation of the formats used</a></p>
<pre><code>>>> import time
>>> time.strptime("51 08 1","%U %y %w")
(2008, 12, 22, 0, 0, 0, 0, 357, -1)
</c... | 0 | 2008-12-29T01:06:16Z | [
"python",
"datetime"
] |
In Python, how do I find the date of the first Monday of a given week? | 396,913 | <p>If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?</p>
<p>Many thanks</p>
| 26 | 2008-12-29T00:42:56Z | 1,287,862 | <p>PEZ's and Gerald Kaszuba's solutions work under assumption that January 1st will always be in the first week of a given year. This assumption is not correct for ISO calendar, see <a href="http://docs.python.org/library/datetime.html#datetime.date.isocalendar">Python's docs</a> for reference. For example, in ISO cale... | 28 | 2009-08-17T13:07:00Z | [
"python",
"datetime"
] |
In Python, how do I find the date of the first Monday of a given week? | 396,913 | <p>If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?</p>
<p>Many thanks</p>
| 26 | 2008-12-29T00:42:56Z | 1,983,884 | <p>I have slightly modified the script of Vaidas K. in a way that it will return the beginning of the week and the end day of the week.</p>
<pre><code>from datetime import datetime, date, timedelta
def weekbegend(year, week):
"""
Calcul du premier et du dernier jour de la semaine ISO
"""
d = date(year... | 4 | 2009-12-31T04:10:29Z | [
"python",
"datetime"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.