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 variable scope error
370,357
<p>I've been programming for many years, and recently started learning Python. The following code works as expected in both python 2.5 and 3.0 (on OS X if that matters):</p> <pre><code>a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) print(b) print(c) # (A) #c+=1 # (B) test() </code><...
136
2008-12-16T03:06:52Z
24,035,261
<p>The best example that makes it clear is:</p> <pre><code>bar = 42 def foo(): print bar if False: bar = 0 </code></pre> <p>when calling <code>foo()</code> , this also <strong>raises</strong> <code>UnboundLocalError</code> although we will never reach to line <code>bar=0</code>, so logically local var...
4
2014-06-04T10:39:21Z
[ "python", "variables", "scope" ]
Python variable scope error
370,357
<p>I've been programming for many years, and recently started learning Python. The following code works as expected in both python 2.5 and 3.0 (on OS X if that matters):</p> <pre><code>a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) print(b) print(c) # (A) #c+=1 # (B) test() </code><...
136
2008-12-16T03:06:52Z
34,153,129
<p>The best way to reach class variable is directly accesing by class name</p> <pre><code>class Employee: counter=0 def __init__(self): Employee.counter+=1 </code></pre>
0
2015-12-08T10:09:47Z
[ "python", "variables", "scope" ]
Differences between Python game libraries Pygame and Pyglet?
370,680
<p>I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days.</p> <p>How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use?</p> <p>Finally, would you say that one is more Pythonic than the other?</p>
33
2008-12-16T07:55:22Z
371,249
<p><strong>Pygame: LGPL license</strong></p> <p><em>Pyglet: BSD license</em></p> <p><strong>Pygame relies on SDL libraries heavily</strong></p> <p><em>Pyglet is a pure python library with fewer dependencies, I think it requires better understanding of OpenGL</em></p> <p><strong>Pygame is around here for a long time...
25
2008-12-16T13:08:52Z
[ "python", "pygame", "pyglet" ]
Differences between Python game libraries Pygame and Pyglet?
370,680
<p>I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days.</p> <p>How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use?</p> <p>Finally, would you say that one is more Pythonic than the other?</p>
33
2008-12-16T07:55:22Z
397,025
<p>I was considering both Pygame and Pyglet for a small 2D shooter, and after looking at source code and some tutorials went with Pyglet. I was very happy with the results.</p> <p>Pyglet worked immediately and was enjoyable to work with, and conceptually very clean. It certainly had a Pythonic feel to me: you could ge...
21
2008-12-29T02:40:06Z
[ "python", "pygame", "pyglet" ]
Differences between Python game libraries Pygame and Pyglet?
370,680
<p>I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days.</p> <p>How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use?</p> <p>Finally, would you say that one is more Pythonic than the other?</p>
33
2008-12-16T07:55:22Z
414,112
<p>I would like to add that there is a fast sprite library <a href="http://arcticpaint.com/projects/rabbyt/" rel="nofollow">Rabbyt</a> which may be a good complement for Pyglet.</p>
5
2009-01-05T18:59:37Z
[ "python", "pygame", "pyglet" ]
Differences between Python game libraries Pygame and Pyglet?
370,680
<p>I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days.</p> <p>How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use?</p> <p>Finally, would you say that one is more Pythonic than the other?</p>
33
2008-12-16T07:55:22Z
2,477,829
<p>Having looked at both pygame and pyglet I found pyglet easier to pick up and was able to write a simple breakout style game within a few days.</p>
2
2010-03-19T14:00:39Z
[ "python", "pygame", "pyglet" ]
Differences between Python game libraries Pygame and Pyglet?
370,680
<p>I've had some experience with Pygame, but there seems to be a lot of buzz around Pyglet these days.</p> <p>How do these two libraries compare? What would be the advantage of using one over the other, both in features and ease of use?</p> <p>Finally, would you say that one is more Pythonic than the other?</p>
33
2008-12-16T07:55:22Z
4,520,448
<p>Pyglet is good (for 2D games) if you never intend to draw any vector graphics or primitives within the game itself, and just stick to loading images from disk. It's also much cleaner because there's no need to write you're own game loop and have to worry about speed and timing and responsiveness.</p> <p>However, if...
9
2010-12-23T16:08:56Z
[ "python", "pygame", "pyglet" ]
Trac documentation?
370,733
<p>I'm trying to write my first little plugin for <a href="http://trac.edgewall.org">Trac</a> and am kind of lost as to what the API exactly is. For example, exactly which fields are offered for "ticket" objects, among many other things.</p> <p>Does anyone know of a good place to look for Trac API documentation? Can't...
12
2008-12-16T08:50:10Z
370,743
<p><a href="http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture" rel="nofollow">http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture</a> is a start.</p>
4
2008-12-16T08:55:55Z
[ "python", "documentation", "trac" ]
Trac documentation?
370,733
<p>I'm trying to write my first little plugin for <a href="http://trac.edgewall.org">Trac</a> and am kind of lost as to what the API exactly is. For example, exactly which fields are offered for "ticket" objects, among many other things.</p> <p>Does anyone know of a good place to look for Trac API documentation? Can't...
12
2008-12-16T08:50:10Z
370,755
<p>The component architecture is important, but the real starting page for development is: <a href="http://trac.edgewall.org/wiki/TracDev">http://trac.edgewall.org/wiki/TracDev</a></p> <p>Have also a look at the trac-hacks web site <a href="http://trac-hacks.org/">http://trac-hacks.org/</a> This is really a good sourc...
11
2008-12-16T08:58:33Z
[ "python", "documentation", "trac" ]
Trac documentation?
370,733
<p>I'm trying to write my first little plugin for <a href="http://trac.edgewall.org">Trac</a> and am kind of lost as to what the API exactly is. For example, exactly which fields are offered for "ticket" objects, among many other things.</p> <p>Does anyone know of a good place to look for Trac API documentation? Can't...
12
2008-12-16T08:50:10Z
370,759
<p>It's all in Trac's Trac!</p> <p>The pages on <a href="http://trac.edgewall.org/wiki/TracDev/PluginDevelopment">plugin development</a> and the <a href="http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture">component architecture</a> give a good overview. Unfortunately, I can't find any API documentation. Your...
5
2008-12-16T09:03:41Z
[ "python", "documentation", "trac" ]
Trac documentation?
370,733
<p>I'm trying to write my first little plugin for <a href="http://trac.edgewall.org">Trac</a> and am kind of lost as to what the API exactly is. For example, exactly which fields are offered for "ticket" objects, among many other things.</p> <p>Does anyone know of a good place to look for Trac API documentation? Can't...
12
2008-12-16T08:50:10Z
377,004
<p>Each component of Trac has an api.py that's loaded with docstrings on all the interfaces you can implement. I've found them to be extremely valuable when implementing my own plugins.</p> <p>For example:</p> <p><a href="http://trac.edgewall.org/browser/trunk/trac/ticket/api.py" rel="nofollow">http://trac.edgewall.o...
4
2008-12-18T05:47:30Z
[ "python", "documentation", "trac" ]
Trac documentation?
370,733
<p>I'm trying to write my first little plugin for <a href="http://trac.edgewall.org">Trac</a> and am kind of lost as to what the API exactly is. For example, exactly which fields are offered for "ticket" objects, among many other things.</p> <p>Does anyone know of a good place to look for Trac API documentation? Can't...
12
2008-12-16T08:50:10Z
4,889,232
<p>I know this is way late and you probably found your answer by now, but for anyone else who ends up here looking for the same thing, the API is online on the Trac website at <a href="http://www.edgewall.org/docs/tags-trac-0.12/epydoc/" rel="nofollow">http://www.edgewall.org/docs/tags-trac-0.12/epydoc/</a> (for 0.12)....
3
2011-02-03T17:06:50Z
[ "python", "documentation", "trac" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
371,314
<p>Compared to pydev, I found it very, very slow, and it crashed (once) when I created a project from existing sources. It's still beta, though.</p> <p>Integration with SCMs will be as good as netbeans is already (I only tried subversion, which worked fine).</p> <p>Feature-wise it was about the same : refactor, debug...
2
2008-12-16T13:32:23Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
371,438
<p>Sun use Mercurial internally now, so expect that their IDE support for it will be top notch.</p>
2
2008-12-16T14:20:28Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
371,600
<p>I started using it a little while back and I like it. I usually develop in a simple editor (SciTE), NetBeans is nice to organize larger projects.</p> <p><a href="http://coreygoldberg.blogspot.com/2008/12/python-trying-out-netbeans-ide.html" rel="nofollow">wrote about it briefly here</a></p>
1
2008-12-16T15:08:08Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
376,922
<p>BraveSirFoobar, it would be nice to know more about what problems you found -- the very, very slow part, as well as the crash. The first time you run the IDE it will index information about your Python platform and project and libraries - such that it can do quick code completion, go to declaration etc. later - but ...
4
2008-12-18T04:39:53Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
416,219
<p>I will share some of the feelings from using it for quite a while now. Things that are roughly the same quality as in Eclipse+Pydev+mercurial:</p> <ol> <li>editor, code-completion</li> <li>debugger features</li> </ol> <p>Things that are better:</p> <ol> <li>autoimport</li> <li>color schemes (Norway today rocks)</...
5
2009-01-06T11:43:39Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
478,975
<p>After looking at this, I decided to go ahead with PyDev than NetBeans.</p> <p>However best wishes to NetBeans team for a faster and better Python support. Cant wait for that :)</p>
0
2009-01-26T06:22:50Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
971,730
<p>How does it compare with PyDev Extensions? I've recently installed it and, to be honest, couldn't imagine myself going back to PyDev.</p> <p>NetBeans seems interesting though, if only I wasn't already hooked onto a couple of other Eclipse plug-ins as well.</p>
0
2009-06-09T18:28:32Z
[ "python", "ide", "netbeans" ]
Has anyone tried NetBeans 6.5 Python IDE?
371,037
<p>Has anyone tried the <a href="http://en.wikipedia.org/wiki/NetBeans#Other%5FNetBeans%5FIDE%5FBundles" rel="nofollow">NetBeans 6.5 Python IDE</a>?</p> <p>What are your opinions? Is it better/worse than <a href="http://en.wikipedia.org/wiki/PyDev" rel="nofollow">PyDev</a>? Do you like it? How does it integrate with s...
14
2008-12-16T11:23:32Z
1,068,597
<p>Having worked with PyDev and PyDev extension for Eclipse for the past few months, the move to NetBeans has been a very pleasurable one.</p> <p>Without having to hunt all the different plug-ins for PyDev and Eclipse, NetBeans had everything I needed out of the box: auto completion, super fast index search, style con...
2
2009-07-01T11:26:38Z
[ "python", "ide", "netbeans" ]
String conversion in Python
371,155
<p>I'm using Python 2.5. The DLL I imported is created using the CLR. The DLL function is returning a string. I'm trying to apply "partition" attribute to it. I'm not able to do it. Even the partition is not working. I think "all strings returned from CLR are returned as Unicode".</p>
-3
2008-12-16T12:27:53Z
371,200
<p>Could you post your error message? Could you post what type of object you have (<code>type(yourvar)</code>)?</p> <p>Please check if you have a <code>partition(sep)</code> method for this object (<code>dir(yourvar)</code>).</p> <p>Applying <code>partition</code> method should look like:</p> <pre><code>&gt;&gt;&gt;...
2
2008-12-16T12:46:24Z
[ "python", "clr" ]
String conversion in Python
371,155
<p>I'm using Python 2.5. The DLL I imported is created using the CLR. The DLL function is returning a string. I'm trying to apply "partition" attribute to it. I'm not able to do it. Even the partition is not working. I think "all strings returned from CLR are returned as Unicode".</p>
-3
2008-12-16T12:27:53Z
371,201
<p>If by <em>CLR</em> you mean <a href="http://msdn.microsoft.com/en-us/library/ddk909ch.aspx" rel="nofollow">.NET CLR</a>, try using <a href="http://www.codeplex.com/IronPython" rel="nofollow">IronPython</a> :</p> <blockquote> <p>IronPython is a new implementation of the Python programming language running on .NET....
1
2008-12-16T12:46:26Z
[ "python", "clr" ]
What's the most pythonic way of testing that inputs are well-formed numbers
371,419
<p>I have a function that expects real numbers (either integers or floats) as its input, and I'm trying to validate this input before doing mathematical operations on it.</p> <p>My first instinct is to cast inputs as floats from within a try-except block.</p> <pre><code>try: myinput = float(input) except: raise...
2
2008-12-16T14:15:10Z
371,436
<p>Maybe you can use a combination of <code>assert</code> and <code>isinstance</code> statements. Something like the following is I think a more pythonic way, as you throw an exception whenever your inputs don't follow your requirements. Unfortunately I don't see any better definition of what is a valid number than you...
2
2008-12-16T14:19:43Z
[ "python", "idioms" ]
What's the most pythonic way of testing that inputs are well-formed numbers
371,419
<p>I have a function that expects real numbers (either integers or floats) as its input, and I'm trying to validate this input before doing mathematical operations on it.</p> <p>My first instinct is to cast inputs as floats from within a try-except block.</p> <pre><code>try: myinput = float(input) except: raise...
2
2008-12-16T14:15:10Z
371,461
<p>To quote myself from <a href="http://stackoverflow.com/questions/367560/how-much-input-validation-should-i-be-doing-on-my-python-functionsmethods#368072">How much input validation should I be doing on my python functions/methods?</a>:</p> <blockquote> <p>For calculations like sum, factorial etc, pythons built-in ...
12
2008-12-16T14:26:45Z
[ "python", "idioms" ]
What's the most pythonic way of testing that inputs are well-formed numbers
371,419
<p>I have a function that expects real numbers (either integers or floats) as its input, and I'm trying to validate this input before doing mathematical operations on it.</p> <p>My first instinct is to cast inputs as floats from within a try-except block.</p> <pre><code>try: myinput = float(input) except: raise...
2
2008-12-16T14:15:10Z
371,573
<p>In Python 2.6 and 3.0, a type hierarchy of numeric abstract data types has been <a href="http://www.python.org/dev/peps/pep-3141/" rel="nofollow">added</a>, so you could perform your check as:</p> <pre><code>&gt;&gt;&gt; import numbers &gt;&gt;&gt; isValid = isinstance(myinput , numbers.Real) </code></pre> <p>numb...
5
2008-12-16T14:58:08Z
[ "python", "idioms" ]
What's the most pythonic way of testing that inputs are well-formed numbers
371,419
<p>I have a function that expects real numbers (either integers or floats) as its input, and I'm trying to validate this input before doing mathematical operations on it.</p> <p>My first instinct is to cast inputs as floats from within a try-except block.</p> <pre><code>try: myinput = float(input) except: raise...
2
2008-12-16T14:15:10Z
372,346
<p>I don't get the question.</p> <p>There are two things with wildly different semantics tossed around as "alternatives".</p> <p>A type conversion is one thing. It works with any object that supports <code>__float__</code>, which can be quite a variety of objects, few of which are actually numeric.</p> <pre><code>t...
1
2008-12-16T19:15:47Z
[ "python", "idioms" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
371,833
<p>You get a recursion error because you call the same function, your <code>__getattribute__</code>. If you use <code>object</code>'s <code>__getattribute__</code> instead, it works:</p> <pre><code>class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): ...
73
2008-12-16T16:26:37Z
[ "python", "oop", "class", "recursion" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
371,844
<p>Are you sure you want to use <code>__getattribute__</code>? What are you actually trying to achieve?</p> <p>The easiest way to do what you ask is:</p> <pre><code>class D(object): def __init__(self): self.test = 20 self.test2 = 21 test = 0 </code></pre> <p>or:</p> <pre><code>class D(obje...
13
2008-12-16T16:32:36Z
[ "python", "oop", "class", "recursion" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
371,864
<p><a href="http://docs.python.org/reference/datamodel.html#object.__getattribute__" rel="nofollow">Python language reference:</a></p> <blockquote> <p>In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes i...
15
2008-12-16T16:39:23Z
[ "python", "oop", "class", "recursion" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
372,919
<p>Actually, I believe you want to use the <code>__getattr__</code> special method instead.</p> <p>Quote from the Python docs:</p> <blockquote> <p><code>__getattr__( self, name)</code> </p> <p>Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute ...
19
2008-12-16T22:01:38Z
[ "python", "oop", "class", "recursion" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
16,811,277
<p>Here is a more reliable version:</p> <pre><code>class D(object): def __init__(self): self.test = 20 self.test2 = 21 def __getattribute__(self, name): if name == 'test': return 0. else: return super(D, self).__getattribute__(name) </code></pre> <p>It c...
5
2013-05-29T10:18:19Z
[ "python", "oop", "class", "recursion" ]
Python - Using __getattribute__ method
371,753
<p>I want to override access to one variable in a class, but return all others normally. How do I accomplish this with <code>__getattribute__</code>?</p> <p>I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:</p> <pre><code>class D(object): def __init__(self): ...
50
2008-12-16T16:04:38Z
28,356,216
<p><strong>Best Practice</strong></p> <p>The proper way to programmatically control access to a single attribute is with <a href="https://docs.python.org/library/functions.html#property" rel="nofollow"><code>property</code></a>. Class <code>D</code> should be written as follows (with the setter and deleter optionally ...
1
2015-02-05T23:48:48Z
[ "python", "oop", "class", "recursion" ]
When is white space not important in Python?
372,007
<p>When is white space not important in Python?</p> <p>It seems to be ignored inside a list, for example:</p> <pre><code>for x in range(5): list += [x, 1 ,2,3, 4,5] </code></pre>
3
2008-12-16T17:21:05Z
372,025
<p>White space is only important for indentation of statements. You have a single statement across several lines, and only the indentation of the beginning of the statement on the first line is significant. See <em><a href="http://www.secnetix.de/~olli/Python/block_indentation.hawk" rel="nofollow">Python: Myths about I...
14
2008-12-16T17:26:39Z
[ "python", "whitespace" ]
When is white space not important in Python?
372,007
<p>When is white space not important in Python?</p> <p>It seems to be ignored inside a list, for example:</p> <pre><code>for x in range(5): list += [x, 1 ,2,3, 4,5] </code></pre>
3
2008-12-16T17:21:05Z
372,108
<p>Your question is really about when Python implicitly joins lines of code.</p> <p>Python will implicitly join lines that are contained within (parentheses), {braces}, and [brackets], as in your example code. You can also explicitly join lines with a backslash (\) at the end of a line.</p> <p>More here on <a href="h...
7
2008-12-16T17:52:23Z
[ "python", "whitespace" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
372,066
<p>Python doesn't really have either concept.</p> <p>It uses duck typing, which removed the need for interfaces (at least for the computer :-))</p> <p>Python &lt;= 2.5: Base classes obviously exist, but there is no explicit way to mark a method as 'pure virtual', so the class isn't really abstract.</p> <p>Python >= ...
29
2008-12-16T17:38:22Z
[ "python", "interface", "abstract-class" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
372,107
<p>Python >= 2.6 has <a href="http://docs.python.org/library/abc.html">Abstract Base Classes</a>.</p> <blockquote> <p>Abstract Base Classes (abbreviated ABCs) complement duck-typing by providing a way to define interfaces when other techniques like hasattr() would be clumsy. Python comes with many builtin ...
85
2008-12-16T17:51:35Z
[ "python", "interface", "abstract-class" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
372,121
<p>What you'll see sometimes is the following:</p> <pre><code>class Abstract1( object ): """Some description that tells you it's abstract, often listing the methods you're expected to supply.""" def aMethod( self ): raise NotImplementedError( "Should have implemented this" ) </code></pre> <p>Becau...
355
2008-12-16T17:59:23Z
[ "python", "interface", "abstract-class" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
372,188
<p>In general, interfaces are used only in languages that use the single-inheritance class model. In these single-inheritance languages, interfaces are typically used if any class could use a particular method or set of methods. Also in these single-inheritance languages, abstract classes are used to either have define...
12
2008-12-16T18:19:55Z
[ "python", "interface", "abstract-class" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
16,447,106
<p>In a more basic way to explain: An interface is sort of like an empty muffin pan. It's a class file with a set of method definitions that have no code.</p> <p>An abstract class is the same thing, but not all functions need to be empty. Some can have code. It's not strictly empty.</p> <p>Why differentiate: There's ...
21
2013-05-08T17:51:11Z
[ "python", "interface", "abstract-class" ]
Difference between abstract class and interface in Python
372,042
<p>What is the difference between abstract class and interface in Python?</p>
302
2008-12-16T17:32:27Z
31,439,126
<blockquote> <h1>What is the difference between abstract class and interface in Python?</h1> </blockquote> <p>In Python, there is none! An abstract class defines an interface.</p> <h2>Using an Abstract Base Class</h2> <p>For example, say we want to use one of the abstract base classes from the <code>collections</c...
37
2015-07-15T19:15:20Z
[ "python", "interface", "abstract-class" ]
UTF in Python Regex
372,102
<p>I'm aware that Python 3 fixes a lot of UTF issues, I am not however able to use Python 3, I am using 2.5.1</p> <p>I'm trying to regex a document but the document has UTF hyphens in it – rather than -. Python can't match these and if I put them in the regex it throws a wobbly.</p> <p>How can I force Python to use...
7
2008-12-16T17:49:12Z
372,128
<p>You have to escape the character in question (–) and put a u in front of the string literal to make it a unicode string. </p> <p>So, for example, this:</p> <pre><code>re.compile("–") </code></pre> <p>becomes this:</p> <pre><code>re.compile(u"\u2013") </code></pre>
7
2008-12-16T18:01:19Z
[ "python", "regex" ]
UTF in Python Regex
372,102
<p>I'm aware that Python 3 fixes a lot of UTF issues, I am not however able to use Python 3, I am using 2.5.1</p> <p>I'm trying to regex a document but the document has UTF hyphens in it – rather than -. Python can't match these and if I put them in the regex it throws a wobbly.</p> <p>How can I force Python to use...
7
2008-12-16T17:49:12Z
372,193
<p>After a quick test and visit to <a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow">PEP 0264: Defining Python Source Code Encodings</a>, I see you may need to tell Python the whole file is UTF-8 encoded by adding adding a comment like this to the first line.</p> <pre><code># encoding: utf-8 </code></p...
4
2008-12-16T18:20:34Z
[ "python", "regex" ]
UTF in Python Regex
372,102
<p>I'm aware that Python 3 fixes a lot of UTF issues, I am not however able to use Python 3, I am using 2.5.1</p> <p>I'm trying to regex a document but the document has UTF hyphens in it – rather than -. Python can't match these and if I put them in the regex it throws a wobbly.</p> <p>How can I force Python to use...
7
2008-12-16T17:49:12Z
372,210
<p>Don't use UTF-8 in a regular expression. UTF-8 is a multibyte encoding where some unicode code points are encoded by 2 or more bytes. You may match parts of your string that you didn't plan to match. Instead use unicode strings as suggested.</p>
3
2008-12-16T18:27:38Z
[ "python", "regex" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
372,442
<p>I have looked at several ways to solve this issue and by far the most elegant is described here: <a href="https://seattle.cs.washington.edu/browser/seattle/trunk/demokit/timeout_xmlrpclib.py?rev=692" rel="nofollow">https://seattle.cs.washington.edu/browser/seattle/trunk/demokit/timeout_xmlrpclib.py?rev=692</a></p> ...
5
2008-12-16T19:45:15Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
375,000
<p>Here is a verbatim copy from <a href="http://code.activestate.com/recipes/473878/" rel="nofollow">http://code.activestate.com/recipes/473878/</a></p> <pre><code>def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None): import threading class InterruptableThread(threading.Thread): def ...
1
2008-12-17T15:53:56Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
1,766,187
<p>An more straightforward solution is at: <a href="http://www.devpicayune.com/entry/200609191448">http://www.devpicayune.com/entry/200609191448</a></p> <pre><code>import xmlrpclib import socket x = xmlrpclib.ServerProxy('http:1.2.3.4') socket.setdefaulttimeout(10) #set the timeout to 10 seconds x.func_nam...
16
2009-11-19T20:09:22Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
2,116,786
<p>clean non global version.</p> <pre><code>import xmlrpclib import httplib class TimeoutHTTPConnection(httplib.HTTPConnection): def connect(self): httplib.HTTPConnection.connect(self) self.sock.settimeout(self.timeout) class TimeoutHTTP(httplib.HTTP): _connection_class = TimeoutHTTPConnect...
14
2010-01-22T11:10:29Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
2,138,709
<p>Based on the one from antonylesuisse, a working version (on python >= 2.6).</p> <pre><code># -*- coding: utf8 -*- import xmlrpclib import httplib import socket class TimeoutHTTP(httplib.HTTP): def __init__(self, host='', port=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): if...
1
2010-01-26T10:15:16Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
7,915,395
<p>Here is code that works on Python 2.7 (probably for other 2.x versions of Python) without raising <em>AttributeError, instance has no attribute 'getresponse'</em>.</p> <pre><code> class TimeoutHTTPConnection(httplib.HTTPConnection): def connect(self): httplib.HTTPConnection.connect(self) self.so...
2
2011-10-27T11:46:37Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
14,397,619
<p>Here another smart and very <strong>pythonic</strong> solution using Python's <code>with</code> statement:</p> <pre><code>import socket import xmlrpc.client class MyServerProxy: def __init__(self, url, timeout=None): self.__url = url self.__timeout = timeout self.__prevDefaultTimeout = ...
0
2013-01-18T11:05:39Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
15,969,144
<p>The following example works with Python 2.7.4.</p> <pre><code>import xmlrpclib from xmlrpclib import * import httplib def Server(url, *args, **kwargs): t = TimeoutTransport(kwargs.get('timeout', 20)) if 'timeout' in kwargs: del kwargs['timeout'] kwargs['transport'] = t server = xmlrpclib.Ser...
0
2013-04-12T10:36:02Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
26,774,377
<p>Based on the one from antonylesuisse, but works on Python 2.7.5, resolving the problem:<code>AttributeError: TimeoutHTTP instance has no attribute 'getresponse'</code></p> <pre><code>class TimeoutHTTP(httplib.HTTP): def __init__(self, host='', port=None, strict=None, timeout=socket._GLOBAL_DEFAU...
0
2014-11-06T07:58:16Z
[ "python", "xml-rpc" ]
Set timeout for xmlrpclib.ServerProxy
372,365
<p>I am using xmlrpclib.ServerProxy to make RPC calls to a remote server. If there is not a network connection to the server it takes the default 10 seconds to return a socket.gaierror to my program. </p> <p>This is annoying when doing development without a network connection, or if the remote server is down. Is there...
14
2008-12-16T19:22:27Z
33,944,151
<p>I wanted a small, clean, but also explicit version, so based on all other answers here, this is what I came up with:</p> <pre><code>import xmlrpclib class TimeoutTransport(xmlrpclib.Transport): def __init__(self, timeout, use_datetime=0): self.timeout = timeout # xmlrpclib uses old-style clas...
1
2015-11-26T17:20:22Z
[ "python", "xml-rpc" ]
Protecting online static content
372,465
<p>How would I only allow users authenticated via Python code to access certain files on the server?</p> <p>For instance, say I have <code>/static/book.txt</code> which I want to protect. When a user accesses <code>/some/path/that/validates/him</code>, a Python script deems him worthy of accessing <code>/static/book.t...
0
2008-12-16T19:50:23Z
372,482
<p>Lighttpd has <a href="http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModSecDownload" rel="nofollow">mod_secdownload</a> for this. Basically, it won't serve the static content directly unless you generate a short-lived static URL for it.</p> <p>Note that you can do similar things on S3 for static content. It's a q...
3
2008-12-16T19:54:29Z
[ "python", "security", "apache", "download", "lighttpd" ]
Protecting online static content
372,465
<p>How would I only allow users authenticated via Python code to access certain files on the server?</p> <p>For instance, say I have <code>/static/book.txt</code> which I want to protect. When a user accesses <code>/some/path/that/validates/him</code>, a Python script deems him worthy of accessing <code>/static/book.t...
0
2008-12-16T19:50:23Z
372,488
<p>You might want to just have your Python script open the file and dump the contents as its output if the user is properly authenticated. Put the files you want to protect in a folder that is outside of the webserver root.</p>
3
2008-12-16T19:55:15Z
[ "python", "security", "apache", "download", "lighttpd" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,517
<p>Impossible to answer without knowing the shape of the data and the structure that you've designed to contain it.</p>
0
2008-12-16T20:06:48Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,524
<p>I wouldn't consider it fair to blame any shortcomings of your design to OOP. Just like any other programming platform out there OO can be used for both good and less than optimal design. Rarely will this be the fault of the programming model itself. </p> <p>But to try to answer your question: Allocating 250000 new ...
2
2008-12-16T20:10:28Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,566
<p>You'd have similar issues with procedural/functional programming languages. How do you store that much data in memory? A struct or array wouldn't work either. </p> <p>You need to take special steps to manage this scale of data.</p> <p>BTW: I wouldn't use this as a reason to pick either an OO language or not. </p>
3
2008-12-16T20:24:58Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,595
<p>The "overhead" depends largely on the platform and the implementation you chose.</p> <p>Now if you have a memory problem reading millions of data from a multiple Gb file, you're having an algorithm problem where the memory consumption of objects is definitely not the biggest concern, the concern yould be more about...
0
2008-12-16T20:34:36Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,641
<p>Like the other posters have stated. I do not believe Objects are going to lend a significant amount of overhead to your process. It will need to store a pointer to the object but the rest of the 'doubles' will be taking 99% of your program's memory.</p> <p>Can you partition this data into much smaller subsets? What...
0
2008-12-16T20:43:57Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,656
<p>See <a href="http://code.activestate.com/recipes/546530/" rel="nofollow">http://code.activestate.com/recipes/546530/</a></p> <p>This is the approximate size of Python objects.</p> <p>The OO size "penalty" is often offset by the ability to (a) simplify processing and (b) keep less stuff in memory in the first place...
2
2008-12-16T20:48:12Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,658
<p>I don't think the question is overhead coming from OO.</p> <p>If we accept C++ as an OO language and remember that the C++ compiler is a preprocessor to C (at least it used to be, when I used C++), anything done in C++ is really done in C. C has very little overhead. So it would depend on the libraries.</p> <p>I t...
0
2008-12-16T20:48:41Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,706
<p>compared to the size of your data set, the overhead of 250K objects is negligible</p> <p>i think you're on the wrong path; don't blame objects for that ;-)</p>
0
2008-12-16T21:03:58Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,724
<p>Please define "manipulate". If you really want to manipulate 4 gigs of data why do you want to manipulate it by pulling it ALL into memory right away?</p> <p>I mean, who needs 4 gig of RAM anyway? :)</p>
0
2008-12-16T21:09:31Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,748
<p>Slightly OT: the <a href="http://en.wikipedia.org/wiki/Flyweight_pattern" rel="nofollow">flyweight design pattern</a> can be useful for minimising overheads when you're manipulating large datasets. Without knowing the details of your problem I'm not sure how applicable it is, but it's worth a look...</p>
3
2008-12-16T21:15:02Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,779
<p>Actual C++ OO memory overhead is one pointer (4-8 bytes, depending) per object with virtual methods. However, as mentioned in other answers, the default memory allocation overhead from dynamic allocation is likely to be significantly greater than this.</p> <p>If you're doing things halfway reasonably, neither over...
1
2008-12-16T21:24:38Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,798
<p>If you have to manipulate data sets this big on a regular basis, could you just get a <a href="http://h10010.www1.hp.com/wwpc/us/en/sm/WF05a/12454-12454-296719-307907-296721-3211286.html" rel="nofollow">64-bit machine</a> with bucket-loads of RAM? For various reasons, I've found myself working with fairly resource ...
0
2008-12-16T21:28:39Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,801
<p>A friend of mine was a professor at MIT and a student asked him why his image analysis program was running so slow. How was it built? Every pixel was an object, and would send messages to its neighbors!</p> <p>If I were you I'd try it in a throw-away program. My suspicion is, unless your classes are very carefully ...
0
2008-12-16T21:29:24Z
[ "python", "oop", "data-analysis" ]
What is the object oriented programming computing overhead cost?
372,511
<p>I have a large set of data (a data cube of 250,000 X 1,000 doubles, about a 4 gig file) and I want to manipulate it using a previous set of OOP classes I have written in Python. Currently the data set is already so large that to read into my machine memory I have to at least split it in half so computing overhead i...
2
2008-12-16T20:04:06Z
372,848
<p>Since you can split the data in half and operate on it, I'm assuming that you're working on each record individually? It sounds to me like you need to change your deserialiser to read one record at a time, manipulate it, and then store out the results.</p> <p>Basically you need a string parser class that does a Pee...
0
2008-12-16T21:37:35Z
[ "python", "oop", "data-analysis" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
372,896
<p>Here's one way to do it.</p> <pre><code>#!/usr/bin/python import MySQLdb # connect db = MySQLdb.connect(host="localhost", user="appuser", passwd="", db="onco") cursor = db.cursor() # execute SQL select statement cursor.execute("SELECT * FROM LOCATION") # commit your changes db.commit() # get the number of rows...
118
2008-12-16T21:51:56Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
372,899
<p>Try using MySQLdb</p> <p>There is a how to page here: <a href="http://www.kitebird.com/articles/pydbapi.html">http://www.kitebird.com/articles/pydbapi.html</a></p> <p><hr /></p> <p>From the page:</p> <pre><code># server_version.py - retrieve and display database server version import MySQLdb conn = MySQLdb.con...
15
2008-12-16T21:52:49Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
373,011
<p><a href="http://pypi.python.org/pypi/MySQL-python/">MySQLdb</a> is the straightforward way. You get to execute SQL queries over a connection. Period.</p> <p>My preferred way, which is also pythonic, is to use the mighty <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> instead. Here is a <a href="http://www.sqlal...
5
2008-12-16T22:29:06Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
622,308
<h2>Connecting to MYSQL with Python in 3 steps</h2> <p><strong>1 - Setting</strong></p> <p>You must install a MySQL driver before doing anything. Unlike PHP, only the SQLite driver is installed by default with Python. The most used package to do so is <a href="http://pypi.python.org/pypi/MySQL-python/">MySQLdb</a> ...
837
2009-03-07T18:57:34Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
12,904,258
<p>As a db driver, there is also <a href="http://packages.python.org/oursql/index.html">oursql</a>. Some of the reasons listed on that link, which say why oursql is better:</p> <blockquote> <ul> <li>oursql has real parameterization, sending the SQL and data to MySQL completely separately.</li> <li>oursql allows ...
16
2012-10-15T21:25:05Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
16,796,108
<p>Oracle (MySQL) now supports a pure Python connector. That means no binaries to install: it's just a Python library. It's called "Connector/Python". </p> <p><a href="http://dev.mysql.com/downloads/connector/python/">http://dev.mysql.com/downloads/connector/python/</a></p>
70
2013-05-28T15:39:22Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
18,738,239
<p>For python 3.3</p> <p>CyMySQL <a href="https://github.com/nakagami/CyMySQL" rel="nofollow">https://github.com/nakagami/CyMySQL</a></p> <p>I have pip installed on my windows 7, just pip install cymysql</p> <p>(you don't need cython) quick and painless</p>
0
2013-09-11T10:04:37Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
20,959,654
<p>If you do not need MySQLdb, but would accept any library, I would very, very much recommend MySQL Connector/Python from MySQL: <a href="http://dev.mysql.com/downloads/connector/python/">http://dev.mysql.com/downloads/connector/python/</a>.</p> <p>It is one package (around 110k), pure Python, so it is system indepen...
64
2014-01-06T21:32:15Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
30,619,930
<p>Also take a look at <a href="https://storm.canonical.com" rel="nofollow">Storm</a>. It is a simple SQL mapping tool which allows you to easily edit and create SQL entries without writing the queries.</p> <p>Here is a simple example:</p> <pre><code>from storm.locals import * # User will be the mapped object; you h...
0
2015-06-03T12:20:16Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
32,687,395
<p>Just a modification in above answer. Simply run this command to install mysql for python</p> <pre><code>sudo yum install MySQL-python sudo apt-get install MySQL-python </code></pre> <p>remember! It is case sensitive.</p>
2
2015-09-21T04:31:40Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
34,503,728
<blockquote> <p>Stop Using MySQLDb if you want to avoid installing mysql headers just to access mysql from python.</p> </blockquote> <p>Use <a href="https://github.com/PyMySQL/PyMySQL">pymysql</a> it does all of what MySQLDb does but it was implemented purely in python with <strong><em>NO External Dependencies</em><...
18
2015-12-29T02:52:13Z
[ "python", "mysql" ]
How do I connect to a MySQL Database in Python?
372,885
<p>How do I connect to a MySQL database using a python program?</p>
674
2008-12-16T21:49:09Z
37,970,797
<p>Despite all answers above, in case you do not want to connect to a specific database upfront, for example, if you want to create the database still (!), you can use <code>connection.select_db(database)</code>, as demonstrated in the following.</p> <pre><code>import pymysql.cursors connection = pymysql.connect(host=...
2
2016-06-22T14:18:47Z
[ "python", "mysql" ]
Finding the Current Active Window in Mac OS X using Python
373,020
<p>Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?</p>
12
2008-12-16T22:33:02Z
373,210
<p>First off, do you want the window or the application name? This isn't Windows—an application process on Mac OS X can have multiple windows. (Furthermore, this has also been true of Windows for a few years now, although I have no idea what the API looks like for that.)</p> <p>Second, Carbon or Cocoa?</p> <p>To ge...
6
2008-12-16T23:52:06Z
[ "python", "objective-c", "cocoa", "osx" ]
Finding the Current Active Window in Mac OS X using Python
373,020
<p>Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?</p>
12
2008-12-16T22:33:02Z
373,310
<p>This should work:</p> <pre><code>#!/usr/bin/python from AppKit import NSWorkspace activeAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName'] print activeAppName </code></pre> <p>Only works on Leopard, or on Tiger if you have PyObjC installed and happen to point at the right python bina...
18
2008-12-17T00:44:22Z
[ "python", "objective-c", "cocoa", "osx" ]
Finding the Current Active Window in Mac OS X using Python
373,020
<p>Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?</p>
12
2008-12-16T22:33:02Z
8,717,955
<p>I post this as an answer because it is too long for a comment. I needed the current frontmost application in a python script that arranges the windows nicely on my screen[1].</p> <p>[1] <a href="https://github.com/SirVer/move_window" rel="nofollow">https://github.com/SirVer/move_window</a></p> <p>Of course, the co...
1
2012-01-03T20:04:07Z
[ "python", "objective-c", "cocoa", "osx" ]
Finding the Current Active Window in Mac OS X using Python
373,020
<p>Is there a way to find the application name of the current active window at a given time on Mac OS X using Python?</p>
12
2008-12-16T22:33:02Z
25,214,024
<p>The method in the accepted answer was deprecated in OS X 10.7+. The current recommended version would be the following:</p> <pre><code>from AppKit import NSWorkspace active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName() print active_app_name </code></pre>
8
2014-08-09T00:20:52Z
[ "python", "objective-c", "cocoa", "osx" ]
List all the classes that currently exist
373,067
<p>I'm creating a simple API that creates typed classes based on JSON data that has a mandatory 'type' field defined in it. It uses this string to define a new type, add the fields in the JSON object, instantiate it, and then populate the fields on the instance.</p> <p>What I want to be able to do is allow for these ...
2
2008-12-16T22:53:42Z
373,076
<p>You can use <code>dir()</code>:</p> <pre><code>Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; dir() ['__builtins__', '__doc__', '__name__'] &gt;&gt;&gt; class Foo: ... pass ... &gt;&gt;&gt; dir() ['Foo', ...
0
2008-12-16T22:57:15Z
[ "python" ]
List all the classes that currently exist
373,067
<p>I'm creating a simple API that creates typed classes based on JSON data that has a mandatory 'type' field defined in it. It uses this string to define a new type, add the fields in the JSON object, instantiate it, and then populate the fields on the instance.</p> <p>What I want to be able to do is allow for these ...
2
2008-12-16T22:53:42Z
373,092
<p>If you want to reuse types that you created earlier, it's best to cache them yourself:</p> <pre><code>json_types = {} def get_json_type(name): try: return json_types[name] except KeyError: json_types[name] = t = type(json_object['type'], (object,), {}) # any further initialization of t here retu...
3
2008-12-16T23:06:13Z
[ "python" ]
List all the classes that currently exist
373,067
<p>I'm creating a simple API that creates typed classes based on JSON data that has a mandatory 'type' field defined in it. It uses this string to define a new type, add the fields in the JSON object, instantiate it, and then populate the fields on the instance.</p> <p>What I want to be able to do is allow for these ...
2
2008-12-16T22:53:42Z
373,104
<p>You can use <code>dir()</code> to get a list of all the names of all objects in the current environment, and you can use <code>globals()</code> to a get a dictionary mapping those names to their values. Thus, to get just the list of objects which are classes, you can do:</p> <pre><code>import types listOfClasses =...
1
2008-12-16T23:12:58Z
[ "python" ]
XPath in XmlStream.addObserver doesn't work the way it should
373,189
<p>What I want to do is to react only on specified root elements. For example, if user sends XmlStream that looks like:</p> <pre><code>&lt;auth&gt; &lt;login&gt;user&lt;/login&gt; &lt;pass&gt;dupa.8&lt;/pass&gt; &lt;/auth&gt; </code></pre> <p>My method ._auth should be executed. I've done it with addObserver ...
1
2008-12-16T23:42:35Z
374,548
<p>Ok, finally I got the answer. It's because of XmlStream itself. Connection is active as long as main root element is not closed (for example: <code>&lt;stream/&gt;</code>). Everything inside it is root element for XPath, that's why "/auth" means <code>&lt;stream&gt;&lt;auth&gt;&lt;/auth&gt;&lt;/stream&gt;</code>.</p...
1
2008-12-17T13:40:33Z
[ "python", "twisted" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
373,206
<p>How about "([a-fA-F\d]{32})" which requires it to be 32 characters long?</p>
2
2008-12-16T23:51:00Z
[ "python", "regex", "md5" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
373,207
<p>Well, since md5 is just a string of 32 hex digits, about all you could add to your expression is a check for "32 digits", perhaps something like this?</p> <pre><code>re.findall(r"([a-fA-F\d]{32})", data) </code></pre>
40
2008-12-16T23:51:02Z
[ "python", "regex", "md5" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
373,286
<p>When using regular expressions in Python, you should almost always use the raw string syntax <code>r"..."</code>:</p> <pre><code>re.findall(r"([a-fA-F\d]{32})", data) </code></pre> <p>This will ensure that the backslash in the string is not interpreted by the normal Python escaping, but is instead passed through t...
11
2008-12-17T00:31:29Z
[ "python", "regex", "md5" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
376,889
<p>Here's a better way to do it than some of the other solutions:</p> <pre><code>re.findall(r'(?i)(?&lt;![a-z0-9])[a-f0-9]{32}(?![a-z0-9])', data) </code></pre> <p>This ensures that the match must be a string of 32 hexadecimal digit characters, <em>but which is not contained within a larger string of other alphanumer...
7
2008-12-18T04:13:35Z
[ "python", "regex", "md5" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
15,303,876
<h2>MD5 Python Regex With Examples</h2> <p>Since an MD5 is composed of exactly 32 Hexadecimal Characters, and sometimes the hash is presented using lowercase letters, one should account for them as well. </p> <hr> <p>The below example was tested against four different strings:</p> <ul> <li>A valid lowecase MD5 has...
3
2013-03-08T21:46:02Z
[ "python", "regex", "md5" ]
Python regex for MD5 hash
373,194
<p>I've come up with:</p> <pre><code>re.findall("([a-fA-F\d]*)", data) </code></pre> <p>but it's not very fool proof, is there a better way to grab all MD5-hash codes?</p>
15
2008-12-16T23:45:02Z
25,231,922
<p>Here's an extremely pedantic expression:</p> <pre><code>re.findall(r"\b([a-f\d]{32}|[A-F\d]{32})\b", data) </code></pre> <ul> <li>string must be exactly 32 characters long,</li> <li>string must be between a word boundary (newline, space, etc),</li> <li>alpha must all be lowercase a-f <strong><em>OR</em></strong> a...
1
2014-08-10T18:52:35Z
[ "python", "regex", "md5" ]
Close an easygui Python script with the standard 'close' button
373,212
<p>I've created a <em>very</em> simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input.</p> <p>I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. t...
3
2008-12-16T23:52:47Z
373,267
<p>I don't know right now, but have you tried something like this?:</p> <pre><code>root.protocol('WM_DELETE_WINDOW', self.quit) </code></pre> <p>or </p> <pre><code>root.protocol('WM_DELETE_WINDOW', self.destroy) </code></pre> <p>I haven't tried, but google something like <code>"Tkinter protocol WM_DELETE_WINDOW"<...
0
2008-12-17T00:24:29Z
[ "python", "user-interface", "tkinter", "easygui" ]
Close an easygui Python script with the standard 'close' button
373,212
<p>I've created a <em>very</em> simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input.</p> <p>I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. t...
3
2008-12-16T23:52:47Z
473,695
<p>It would require altering the easygui module, yes. I will get it modified!</p> <p>** I have sent in a e-mail to the EasyGUI creator explaning this [12:12 PM, January 23/09]</p> <p>** I just want to say that the possibility of this change happening - if at all, which I doubt - is very tiny. You see, EasyGUI is int...
5
2009-01-23T17:04:00Z
[ "python", "user-interface", "tkinter", "easygui" ]
Close an easygui Python script with the standard 'close' button
373,212
<p>I've created a <em>very</em> simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input.</p> <p>I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. t...
3
2008-12-16T23:52:47Z
34,888,002
<p>I found a solution, the answer is below the choice box, when he defines some functions (you can just type to find denyWindowManagerClose) and go to where he defines it. Just erase it and put this code in its place.</p> <pre><code> def denyWindowManagerClose(): #---------------------------------------------------...
0
2016-01-19T22:16:44Z
[ "python", "user-interface", "tkinter", "easygui" ]
How do I get a Cron like scheduler in Python?
373,335
<p>I'm looking for a library in Python which will provide <code>at</code> and <code>cron</code> like functionality.</p> <p>I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron.</p> <p>For those unfamiliar with <code>cron</code>: you can...
137
2008-12-17T00:56:31Z
373,348
<p>I don't know if something like that already exists. It would be easy to write your own with time, datetime and/or calendar modules, see <a href="http://docs.python.org/library/time.html" rel="nofollow">http://docs.python.org/library/time.html</a></p> <p>The only concern for a python solution is that your job needs ...
-1
2008-12-17T01:01:33Z
[ "python", "cron" ]
How do I get a Cron like scheduler in Python?
373,335
<p>I'm looking for a library in Python which will provide <code>at</code> and <code>cron</code> like functionality.</p> <p>I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron.</p> <p>For those unfamiliar with <code>cron</code>: you can...
137
2008-12-17T00:56:31Z
373,410
<p>One thing that in my searches I've seen is python's <a href="http://docs.python.org/library/sched.html"><code>sched</code></a> module which might be the kind of thing you're looking for.</p>
10
2008-12-17T01:45:17Z
[ "python", "cron" ]