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
Qt winId() forcing 32bit values
2,053,949
<p>Hi all Im trying to embed a display from an alien application (python OCC) into (Py)Qt using the winId of the widget. But when i pass it to OCC i get an overflow error. Inspecting the winId qt returns its 4318283408 which is more than a 32bit number. Im running 64bits (osx) and both libraries are compiled for 64bit...
0
2010-01-13T02:16:49Z
2,060,398
<p>Looking in Qt's source code, in the file <code>src/gui/kernel/qwindowdefs.h</code>, you'll find that <code>WId</code> is typedef'd to <code>long</code> for 64-bits OSX (it's <code>int</code> for 32-bits OSX). A <code>long</code> on 64-bits OSX is 8 bytes long (or 64 bits), and therefore 4318283408 is a valid value.<...
0
2010-01-13T21:26:30Z
[ "python", "qt", "pyqt4" ]
How to average certain sized subsections of a list in python?
2,054,013
<p>I would like to take bites out of a list (or array) of a certain size, return the average for that bite, and then move on to the next bite and do that all over again. Is there some way to do this without writing a for loop?</p> <pre><code>In [1]: import numpy as np In [2]: x = range(10) In [3]: np.average(x[:4]) Ou...
0
2010-01-13T02:39:21Z
2,054,018
<p>The <code>grouper</code> <a href="http://docs.python.org/library/itertools.html#recipes" rel="nofollow"><code>itertools</code> recipe</a> can help.</p>
1
2010-01-13T02:41:49Z
[ "python", "list", "average" ]
How to average certain sized subsections of a list in python?
2,054,013
<p>I would like to take bites out of a list (or array) of a certain size, return the average for that bite, and then move on to the next bite and do that all over again. Is there some way to do this without writing a for loop?</p> <pre><code>In [1]: import numpy as np In [2]: x = range(10) In [3]: np.average(x[:4]) Ou...
0
2010-01-13T02:39:21Z
2,054,023
<pre>[np.average(x[i:i+4]) for i in xrange(0, len(x), 4) ]</pre>
5
2010-01-13T02:43:04Z
[ "python", "list", "average" ]
How to average certain sized subsections of a list in python?
2,054,013
<p>I would like to take bites out of a list (or array) of a certain size, return the average for that bite, and then move on to the next bite and do that all over again. Is there some way to do this without writing a for loop?</p> <pre><code>In [1]: import numpy as np In [2]: x = range(10) In [3]: np.average(x[:4]) Ou...
0
2010-01-13T02:39:21Z
2,054,156
<p>Using numpy, you can use <code>np.average</code> with the <code>axis</code> keyword:</p> <pre><code>import numpy as np x=np.arange(12) y=x.reshape(3,4) print(y) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] print(np.average(y,axis=1)) # [ 1.5 5.5 9.5] </code></pre> <p>Note that to reshape <code>x</code>, I...
1
2010-01-13T03:21:18Z
[ "python", "list", "average" ]
Why the following python code works?
2,054,124
<pre><code>class Square: def __init__(self,start,stop): self.value = start - 1 self.stop = stop def __iter__(self): return self def next(self): if self.value == self.stop: raise StopIteration ...
0
2010-01-13T03:12:56Z
2,054,137
<p>why wouldn't it? It looks like a normal iterator to me...</p> <p>the <code>next()</code> method is a 'known' method in python that along with the <code>__iter__()</code> method signals a generator.</p> <p><a href="http://docs.python.org/tutorial/classes.html#iterators" rel="nofollow">Here is the python docs on it...
1
2010-01-13T03:15:45Z
[ "python", "syntax" ]
Why the following python code works?
2,054,124
<pre><code>class Square: def __init__(self,start,stop): self.value = start - 1 self.stop = stop def __iter__(self): return self def next(self): if self.value == self.stop: raise StopIteration ...
0
2010-01-13T03:12:56Z
2,054,140
<p>This is a Python iterator: every time through the loop the next() method is called</p>
1
2010-01-13T03:16:19Z
[ "python", "syntax" ]
Why the following python code works?
2,054,124
<pre><code>class Square: def __init__(self,start,stop): self.value = start - 1 self.stop = stop def __iter__(self): return self def next(self): if self.value == self.stop: raise StopIteration ...
0
2010-01-13T03:12:56Z
2,054,141
<p>The typical Python iteration protocol: <code>for y in x...</code> is as follows:</p> <pre><code>iter = x.__iter__() # get iterator try: while 1: y = iter.next() # get each item ... # process y except StopIteration: pass # iterator exhausted </code></pre> <p>...
0
2010-01-13T03:16:47Z
[ "python", "syntax" ]
Why the following python code works?
2,054,124
<pre><code>class Square: def __init__(self,start,stop): self.value = start - 1 self.stop = stop def __iter__(self): return self def next(self): if self.value == self.stop: raise StopIteration ...
0
2010-01-13T03:12:56Z
2,080,813
<p>It's an iterator.</p> <p>Normally though, you would write it using yield.</p> <pre><code>def Square(start, stop): for value in xrange(start, stop + 1): yield value ** 2 for i in Square(1, 4): print i, </code></pre>
1
2010-01-17T11:31:23Z
[ "python", "syntax" ]
Python: Why does my function not display what's being returned in the interpreter?
2,054,228
<h1>In the Python interactive interpreter:</h1> <p>I am importing a module that contains a class. These are the methods of that class (some of them):</p> <pre><code> def do_api_call(self, params): return self.__apicall(params) def __apicall(self, params): return urllib2.urlopen(self.endpoint, ...
1
2010-01-13T03:42:09Z
2,054,300
<p>Your first version only returns the value that you would like to see as the output. The second version actually prints this value.</p> <p>If I were you, I would consider storing the return value of the call to the first version into a variable and printing that variable. That should solve your issue</p>
1
2010-01-13T04:00:57Z
[ "python" ]
What is a good thesaurus and taxonomy library for Python?
2,054,230
<p>Can you recommend a good Python library to get thesaurus and taxonomy of a given word?</p> <p>Synonym:</p> <pre><code>&gt;&gt;&gt; print get_synonym('image') ['picture', 'photo'] </code></pre> <p>Taxonomy:</p> <pre><code>&gt;&gt;&gt; print get_taxonomy('baseball') ['sports'] </code></pre>
2
2010-01-13T03:42:30Z
2,054,238
<p><a href="http://osteele.com/projects/pywordnet/" rel="nofollow">pywordnet</a>, now part of <a href="http://www.nltk.org/" rel="nofollow">NLTK</a></p>
3
2010-01-13T03:44:56Z
[ "python", "taxonomy", "thesaurus" ]
What is a good thesaurus and taxonomy library for Python?
2,054,230
<p>Can you recommend a good Python library to get thesaurus and taxonomy of a given word?</p> <p>Synonym:</p> <pre><code>&gt;&gt;&gt; print get_synonym('image') ['picture', 'photo'] </code></pre> <p>Taxonomy:</p> <pre><code>&gt;&gt;&gt; print get_taxonomy('baseball') ['sports'] </code></pre>
2
2010-01-13T03:42:30Z
28,763,463
<p><a href="http://www.nltk.org/" rel="nofollow">NLTK</a></p> <h2>Installation</h2> <p>You might need superuser rights for that:</p> <pre><code>$ pip install nltk </code></pre> <h2>Usage</h2> <pre><code>&gt;&gt;&gt; import nltk &gt;&gt;&gt; from nltk.corpus import wordnet as wn &gt;&gt;&gt; wn.synsets('dog') [Syn...
0
2015-02-27T10:59:42Z
[ "python", "taxonomy", "thesaurus" ]
creating a numpy vector using 3 components
2,054,360
<p>I want to create a vector in numpy using 3 components Vx, Vy, Vz as sown below. Can anyone help? Thank you</p> <pre><code>from numpy import cos, sin Vx = cos(alpha)* cos(beta) Vy = sin(alpha)*cos(beta) Vz = sin(beta) </code></pre>
1
2010-01-13T04:16:25Z
2,054,383
<p>With NumPy, vectors (and matrices for that matter) are just Python arrays. As in</p> <pre><code>from numpy import array myVector = array([Vx, Vy, Vz]) myMatrix = array([[Vx, Vy, Vz], [1,2,3]]) </code></pre>
5
2010-01-13T04:22:13Z
[ "python", "numpy" ]
Getting the first elements per row in an array in Python?
2,054,416
<p>Let's say i have an array of Tuples, s, in the form of:</p> <pre><code>s = ((1, 23, 34),(2, 34, 44), (3, 444, 234)) </code></pre> <p>and i want to return another Tuple, t, consisting of the first element per row:</p> <pre><code>t = (1, 2, 3) </code></pre> <p>Which would be the most efficient method to do this? ...
11
2010-01-13T04:31:02Z
2,054,423
<p>No.</p> <pre><code>t = tuple(x[0] for x in s) </code></pre>
21
2010-01-13T04:32:18Z
[ "python", "tuples" ]
Getting the first elements per row in an array in Python?
2,054,416
<p>Let's say i have an array of Tuples, s, in the form of:</p> <pre><code>s = ((1, 23, 34),(2, 34, 44), (3, 444, 234)) </code></pre> <p>and i want to return another Tuple, t, consisting of the first element per row:</p> <pre><code>t = (1, 2, 3) </code></pre> <p>Which would be the most efficient method to do this? ...
11
2010-01-13T04:31:02Z
2,054,449
<p>The list comprehension method given by Ignacio is the cleanest.</p> <p>Just for kicks, you could also do:</p> <pre><code>zip(*s)[0] </code></pre> <p><code>*s</code> expands <code>s</code> into a list of arguments. So it is equivalent to </p> <pre><code>zip( (1, 23, 34),(2, 34, 44), (3, 444, 234)) </code></pre> ...
5
2010-01-13T04:39:59Z
[ "python", "tuples" ]
Getting the first elements per row in an array in Python?
2,054,416
<p>Let's say i have an array of Tuples, s, in the form of:</p> <pre><code>s = ((1, 23, 34),(2, 34, 44), (3, 444, 234)) </code></pre> <p>and i want to return another Tuple, t, consisting of the first element per row:</p> <pre><code>t = (1, 2, 3) </code></pre> <p>Which would be the most efficient method to do this? ...
11
2010-01-13T04:31:02Z
2,054,479
<pre><code>import itertools s = ((1, 23, 34),(2, 34, 44), (3, 444, 234)) print(next(itertools.izip(*s))) </code></pre> <p><code>itertools.izip</code> returns an iterator. The <code>next</code> function returns the next (and in this case, first) element from the iterator. </p> <p>In Python 2.x, <code>zip</code> return...
1
2010-01-13T04:48:44Z
[ "python", "tuples" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,054,457
<p>I would use <code>([^\d]+)?(\d{5})?</code> as my regular expression, and use <code>match</code> so it only matches the beginning of the string. This way it won't fail on bad input and will make its best guess as to what was intended. Then you can split the first capture group on ",".</p>
1
2010-01-13T04:42:28Z
[ "python", "regex", "string" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,054,463
<p>Just ask for their zip only, then give a (short) list of applicable cities by using a geocode database. That way you get nice clean 5-digit input, they save time, and you all go home happy.</p> <p>If you already have the data, look just for the zip, find a list of possible cities (there will only be one applicable ...
3
2010-01-13T04:44:14Z
[ "python", "regex", "string" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,054,481
<p>This code seems to do what you want:</p> <pre><code>text = """ Boulder, Co 80303 Boulder, Colorado Boulder, Co 80303 """ lines = text.splitlines() ABBREV = dict(co="Colorado", ca="California") STATES = ABBREV.values() def parse_addr(line): addr = {} # normalize commas parts = line.replace(",", " ").s...
0
2010-01-13T04:49:20Z
[ "python", "regex", "string" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,054,706
<p>You could use a geocoding web service or something similar. For example, on the <a href="http://developer.yahoo.com/maps/rest/V1/geocode.html" rel="nofollow">Yahoo geocoding API</a> page, it shows how you can specify the address in a number of ways:</p> <pre><code>This free field lets users enter any of the follow...
3
2010-01-13T05:46:02Z
[ "python", "regex", "string" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,718,896
<ol> <li><p><code>easy_install ngram</code></p></li> <li><p>build file with all the city and state names one per line, place in citystate.data Redwood City, CA Redwood, VA etc</p></li> <li><p>Experiment ( the .2 threshold is a little lax )</p></li> </ol> <hr> <pre><code>import string import ngram cityStateParser = ng...
1
2010-04-27T05:19:49Z
[ "python", "regex", "string" ]
get city, state or zip from a string in python
2,054,422
<p>I'd like to be able to parse out the city, state or zip from a string in python. So, if I entered</p> <p>Boulder, Co<br> 80303<br> Boulder, Colorado<br> Boulder, Co 80303<br> ... </p> <p>any variation of these it would return the city, state or zip. </p> <p>This is all going to be user inputted data and inputte...
2
2010-01-13T04:32:15Z
2,982,015
<p>Expect that some users might type a ZIP+4.</p> <p>The pitfalls of asking for city/state/ZIP without an address are described at <a href="http://semaphorecorp.com/cgi/zip5.html" rel="nofollow">http://semaphorecorp.com/cgi/zip5.html</a></p>
0
2010-06-05T20:39:23Z
[ "python", "regex", "string" ]
Suggestions on manipulating an SVG map
2,054,438
<p>I'm working on a map of the native languages of California for Wikipedia. The map contains areas that each correspond to a language. The original looks like this (click it to see the <a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" rel="nofollow">SVG</a>):</p> <p><a href="http://en.wikipedia.org/wiki...
6
2010-01-13T04:37:26Z
2,055,540
<p>Here's an <a href="http://xn--dahlstrm-t4a.net/svg/examples/MapWithMiniView.svg" rel="nofollow">example</a> using your map. You can click any element to get the boundingbox, this can be used for the miniview viewBox (with some tweaking). As you see it's not adding much code to the map, just a couple of elements. Whe...
3
2010-01-13T09:15:29Z
[ "python", "svg", "cartography" ]
Suggestions on manipulating an SVG map
2,054,438
<p>I'm working on a map of the native languages of California for Wikipedia. The map contains areas that each correspond to a language. The original looks like this (click it to see the <a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" rel="nofollow">SVG</a>):</p> <p><a href="http://en.wikipedia.org/wiki...
6
2010-01-13T04:37:26Z
2,160,018
<p>I would recommend using Python and specifically creating extensions for Inkscape. I don't think you really need 60 SVG unless you really want to because the source map will have everything you need.</p> <p>What I would do is use Inkscape to rename the various regions to the same language code you will be using. For...
5
2010-01-29T05:13:20Z
[ "python", "svg", "cartography" ]
find corresponding key,values and return?
2,054,490
<pre><code>I have a dictionary cities = {1:'Kompong Som', 2: 'Kompong Thom', 3: 'Phnom Penh'} tags = {1: 'school', 2: 'public', 3: 'private'} kwargs = {'city': '2', 'tag': '3'}#should be improve </code></pre> <p>I want to get output like this :</p> <pre><code>kwargs = {'city': 'Kompong Thom', 'tag': 'private'} </...
-1
2010-01-13T04:50:30Z
2,054,505
<pre><code>def translate(cities, tags, kwargs): return {'city': cities[int(kwargs['city'])], 'tag': tags[int(kwargs['tag'])]} </code></pre> <p>There's no clear way (from your question) to automate the keyname-to-auxiliary dictionary choice, so I've just hardcoded the keys and aux dict to use for each; if t...
3
2010-01-13T04:54:12Z
[ "python" ]
find corresponding key,values and return?
2,054,490
<pre><code>I have a dictionary cities = {1:'Kompong Som', 2: 'Kompong Thom', 3: 'Phnom Penh'} tags = {1: 'school', 2: 'public', 3: 'private'} kwargs = {'city': '2', 'tag': '3'}#should be improve </code></pre> <p>I want to get output like this :</p> <pre><code>kwargs = {'city': 'Kompong Thom', 'tag': 'private'} </...
-1
2010-01-13T04:50:30Z
2,054,516
<p>You could put <code>cities</code> and <code>tags</code> in a helper dictionary to make it more easy to select the correct values using the <code>kwargs</code> keys:</p> <pre><code>choices = { 'city': cities, 'tag': tags } result = {} for k, v in kwargs: result[k] = choices[k][int(v)] </code></pre>
1
2010-01-13T04:55:45Z
[ "python" ]
How to disable shell interception of control characters?
2,054,626
<p>I'm writing a curses application in Python under UNIX. I want to enable the user to use C-Y to yank from a kill ring a la Emacs.</p> <p>The trouble is, of course, that C-Y is caught by my shell which then sends SIGTSTP to my process. In addition, C-Z also results in SIGTSTP being sent, so catching the signal means ...
3
2010-01-13T05:21:25Z
2,054,642
<p>For basic functionality, use <a href="http://docs.python.org/library/tty.html" rel="nofollow"><code>tty</code></a>. For example, calling <code>tty.setraw(sys.stdin)</code> will put standard input's terminal into raw mode.</p> <p>For the more general case, Python comes with a <a href="http://docs.python.org/library/...
1
2010-01-13T05:26:30Z
[ "python", "unix", "signals", "curses" ]
How to disable shell interception of control characters?
2,054,626
<p>I'm writing a curses application in Python under UNIX. I want to enable the user to use C-Y to yank from a kill ring a la Emacs.</p> <p>The trouble is, of course, that C-Y is caught by my shell which then sends SIGTSTP to my process. In addition, C-Z also results in SIGTSTP being sent, so catching the signal means ...
3
2010-01-13T05:21:25Z
2,054,648
<p>See the <code>termios</code> module, and the <code>termios(3)</code> man page.</p>
2
2010-01-13T05:27:45Z
[ "python", "unix", "signals", "curses" ]
How to search and replace utf-8 special characters in Python?
2,054,746
<p>I'm a Python beginner, and I have a utf-8 problem.</p> <p>I have a utf-8 string and I would like to replace all german umlauts with ASCII replacements (in German, u-umlaut 'ü' may be rewritten as 'ue'). </p> <p>u-umlaut has unicode code point 252, so I tried this:</p> <pre><code>&gt;&gt;&gt; str = unichr(252) + ...
1
2010-01-13T05:55:53Z
2,054,783
<p><a href="http://docs.python.org/library/functions.html#repr" rel="nofollow"><code>repr(str)</code></a> returns a quoted version of <code>str</code>, that when printed out, will be something you could type back in as Python to get the string back. So, it's a string that literally contains <code>\xfcber</code>, instea...
5
2010-01-13T06:03:03Z
[ "python", "string", "utf-8", "replace" ]
How to search and replace utf-8 special characters in Python?
2,054,746
<p>I'm a Python beginner, and I have a utf-8 problem.</p> <p>I have a utf-8 string and I would like to replace all german umlauts with ASCII replacements (in German, u-umlaut 'ü' may be rewritten as 'ue'). </p> <p>u-umlaut has unicode code point 252, so I tried this:</p> <pre><code>&gt;&gt;&gt; str = unichr(252) + ...
1
2010-01-13T05:55:53Z
2,054,935
<p>I think it's easiest and clearer to do it on a more straightforward way, using directly the unicode representation os 'ü' better than unichr(252).</p> <pre><code>&gt;&gt;&gt; s = u'über' &gt;&gt;&gt; s.replace(u'ü', 'ue') u'ueber' </code></pre> <p>There's no need to use repr, as this will print the 'Python repr...
5
2010-01-13T06:46:52Z
[ "python", "string", "utf-8", "replace" ]
How to search and replace utf-8 special characters in Python?
2,054,746
<p>I'm a Python beginner, and I have a utf-8 problem.</p> <p>I have a utf-8 string and I would like to replace all german umlauts with ASCII replacements (in German, u-umlaut 'ü' may be rewritten as 'ue'). </p> <p>u-umlaut has unicode code point 252, so I tried this:</p> <pre><code>&gt;&gt;&gt; str = unichr(252) + ...
1
2010-01-13T05:55:53Z
2,056,807
<p>You can avoid all that sourcefile encoding stuff and its problems. Use the Unicode names, then its screamingly obvious what you are doing and the code can be read and modified anywhere.</p> <p>I don't know of any language where the only accented Latin letter is lower-case-u-with-umlaut-aka-diaeresis, so I've added ...
4
2010-01-13T13:13:36Z
[ "python", "string", "utf-8", "replace" ]
How do I send email with django-registration?
2,054,785
<p>How do I send email with django-registration?</p>
-1
2010-01-13T06:03:30Z
2,055,149
<p><a href="http://bitbucket.org/ubernostrum/django-registration/" rel="nofollow">django-registration</a> sends an email to the user, e.g. when he or she registers. The process is as follows (if this was your question ...)*:</p> <ul> <li><p>The user has filled out and submitted the registration form ...</p></li> <li><...
6
2010-01-13T07:39:48Z
[ "python", "django" ]
How to provide a default value for base class attribute from subclass in Django?
2,054,831
<p>Scenario:</p> <pre><code>class BaseClass(models.Model): base_field = models.CharField(max_length=15, default=None) class SubClass(BaseClass): # TODO set default value if base_field's value is None ... </code></pre> <p>ie. I need to be able to load a fixture into the database, providing a default value...
3
2010-01-13T06:19:10Z
2,055,141
<p>You could override the <code>save</code> method, check if base_field is set, otherwise set it to the default value.</p>
2
2010-01-13T07:37:59Z
[ "python", "django", "django-models", "django-inheritance" ]
How to import models in other projects in Django
2,054,898
<p>Is it possible to import models from apps in different Django projects?</p> <p>I hope to move some common models in a base projects from which every child projects can share the same data in these common models.</p> <p><strong>Edit</strong></p> <p>I have to place</p> <pre><code>from baseproject.appname.models im...
2
2010-01-13T06:35:05Z
2,054,924
<p>Yes. You can turn a project-specific app into a standard Python package by moving it to <code>site-packages</code> (or wherever your Python install expects its modules) and breaking any links from it to other apps in the project. You can then import it as you would any Python module, in any project.</p>
2
2010-01-13T06:42:55Z
[ "python", "django", "model" ]
How to stop "warnings" when using vim omni-completion with python?
2,054,942
<p>I am trying vim's python <a href="http://www.vim.org/scripts/script.php?script_id=1542" rel="nofollow">omni-completion script</a>, it works, but I got problem.</p> <p>After I start vim, the 1st time I press to ask vim to complete python code, many warning shown up. That's because some python libs in my project us...
1
2010-01-13T06:48:10Z
2,055,009
<p>If you have a top level routine which imports the 3rd party library, you can insert this</p> <pre><code>import warnings warnings.simplefilter("ignore",DeprecationWarning) </code></pre> <p>to ignore all deprecation warnings (which is what the <code>md5</code> warning is).</p> <p>Check the <code>warnings</code> mod...
1
2010-01-13T07:07:45Z
[ "python", "vim" ]
Why can't Python handle true/false values as I expect?
2,055,029
<p>As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:</p> <pre><code>print True # outputs true True = False; print True # outputs false True = True; print True # outputs false True = not True; print True # outputs true </code...
31
2010-01-13T07:12:47Z
2,055,036
<p>In 2.x, True and False are not keywords so it's possible to shadow the built-ins in this manner.</p>
17
2010-01-13T07:14:20Z
[ "python", "python-2.x" ]
Why can't Python handle true/false values as I expect?
2,055,029
<p>As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:</p> <pre><code>print True # outputs true True = False; print True # outputs false True = True; print True # outputs false True = not True; print True # outputs true </code...
31
2010-01-13T07:12:47Z
2,055,060
<p>Imagine this instead:</p> <pre><code>A = True B = False print A # true A = B; print A # false A = A; print A # false, because A is still false from before A = not A; print A # true, because A was false, so not A is true </code></pre> <p>The exact same thing is going on, but in your version it's co...
41
2010-01-13T07:21:37Z
[ "python", "python-2.x" ]
Why can't Python handle true/false values as I expect?
2,055,029
<p>As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:</p> <pre><code>print True # outputs true True = False; print True # outputs false True = True; print True # outputs false True = not True; print True # outputs true </code...
31
2010-01-13T07:12:47Z
2,055,081
<p>You can check whether True/False is a keyword:</p> <pre><code>&gt;&gt;&gt; import keyword &gt;&gt;&gt; keyword.iskeyword('True') False </code></pre> <p>Since it's not (in my version), assigning True=False just means "True" is another "variable" name.</p>
10
2010-01-13T07:25:44Z
[ "python", "python-2.x" ]
Why can't Python handle true/false values as I expect?
2,055,029
<p>As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:</p> <pre><code>print True # outputs true True = False; print True # outputs false True = True; print True # outputs false True = not True; print True # outputs true </code...
31
2010-01-13T07:12:47Z
2,055,144
<p>Python has these two (among others) builtin objects. They are just objects; in the beginning, they don't have any names yet, but to know what we refer to, let's call them <code>0x600D</code> and <code>0xBAD</code>.</p> <p>Before starting to execute a Python (2.x) script, the name <code>True</code> gets bound to the...
72
2010-01-13T07:38:40Z
[ "python", "python-2.x" ]
Why can't Python handle true/false values as I expect?
2,055,029
<p>As part of answering another question, I wrote the following code whose behaviour seems bizarre at first glance:</p> <pre><code>print True # outputs true True = False; print True # outputs false True = True; print True # outputs false True = not True; print True # outputs true </code...
31
2010-01-13T07:12:47Z
36,762,418
<p>You could easily restore the original values using simple Boolean comparisons:</p> <pre><code>True = 1==1 False = 1==0 </code></pre> <p>Or by converting integer literals to bools:</p> <pre><code>True = bool(1) # actually every number except 0 works False = bool(0) </code></pre>
0
2016-04-21T07:18:40Z
[ "python", "python-2.x" ]
python memory del list[:] vs list = []
2,055,107
<p>In python I have noticed that if you do</p> <pre><code>mylist = [] for i in range(0,100000000): mylist.append('something here to take memory') mylist = [] </code></pre> <p>it would seem the second call </p> <pre><code>mylist = [] </code></pre> <p>would remove the reference and it would get collected but, a...
3
2010-01-13T07:31:33Z
2,055,987
<p>How do you measure that?</p> <p>I've made a small test that does not confirm your results. Here is the source:</p> <pre><code>import meminfo, gc, commands page_size = int(commands.getoutput("getconf PAGESIZE")) def stat(message): s = meminfo.proc_stat() print "My memory usage %s: RSS: %dkb, VSIZE: %dkb" ...
9
2010-01-13T10:37:41Z
[ "python", "memory" ]
Merging two event loops (Cherrypy and Wxpython)
2,055,193
<p>Okay, I have an application written with cherrypy, and I want to build a wxpython gui for it. The problem is that both modules use a close loop for event handling, which (I assume) means while one is running the other will be locked.</p> <p>I asked for some advice and it was suggested that I merge the two event loo...
1
2010-01-13T07:53:13Z
2,055,290
<p>In the case of cherrypy, you have the source. Look in the code what <code>quickloop()</code> does and then try to merge this code with the <code>MainLoop()</code> of WX.</p> <p>Both loops will probably look like this:</p> <pre><code>while (true) { if (pendingEvents()) processEvents (); else waitForEvents (...
0
2010-01-13T08:14:26Z
[ "python", "wxpython", "cherrypy" ]
Merging two event loops (Cherrypy and Wxpython)
2,055,193
<p>Okay, I have an application written with cherrypy, and I want to build a wxpython gui for it. The problem is that both modules use a close loop for event handling, which (I assume) means while one is running the other will be locked.</p> <p>I asked for some advice and it was suggested that I merge the two event loo...
1
2010-01-13T07:53:13Z
2,058,131
<p>You already asked the same question here: <a href="http://stackoverflow.com/questions/2022376/cherrypy-and-wxpython">http://stackoverflow.com/questions/2022376/cherrypy-and-wxpython</a>, and I gave you the best response you're going to find anywhere there, which was voted up and you approved, apparently. Why are you...
6
2010-01-13T16:03:49Z
[ "python", "wxpython", "cherrypy" ]
Generating SQL "IN" clauses: how to safely handle input + empty value lists?
2,055,258
<p>In my Python code I often find myself doing the following (using DB-API):</p> <pre><code>yValues = pickInterestingValuesOfY() sql = "..." # includes a clause stating that "y must be in yValues" c.execute(sql, yValues) </code></pre> <p>In the end, the SQL being executed could be something as simple as</p> <pre><...
2
2010-01-13T08:07:04Z
2,055,278
<p>Nope. There's no pretty way to do it. DB-API does not have any specification for handling sequences in this manner.</p>
3
2010-01-13T08:11:05Z
[ "python", "sql", "mysql", "sql-injection" ]
Generating SQL "IN" clauses: how to safely handle input + empty value lists?
2,055,258
<p>In my Python code I often find myself doing the following (using DB-API):</p> <pre><code>yValues = pickInterestingValuesOfY() sql = "..." # includes a clause stating that "y must be in yValues" c.execute(sql, yValues) </code></pre> <p>In the end, the SQL being executed could be something as simple as</p> <pre><...
2
2010-01-13T08:07:04Z
2,055,287
<p>For the in clause, just always include a value that won't be in the list (e.g. a negative integer). </p>
5
2010-01-13T08:12:58Z
[ "python", "sql", "mysql", "sql-injection" ]
Generating SQL "IN" clauses: how to safely handle input + empty value lists?
2,055,258
<p>In my Python code I often find myself doing the following (using DB-API):</p> <pre><code>yValues = pickInterestingValuesOfY() sql = "..." # includes a clause stating that "y must be in yValues" c.execute(sql, yValues) </code></pre> <p>In the end, the SQL being executed could be something as simple as</p> <pre><...
2
2010-01-13T08:07:04Z
2,056,247
<p>Don't generate your own SQL.</p> <p>Use SQLAlchemy. It does this for you.</p>
-1
2010-01-13T11:34:18Z
[ "python", "sql", "mysql", "sql-injection" ]
Generating SQL "IN" clauses: how to safely handle input + empty value lists?
2,055,258
<p>In my Python code I often find myself doing the following (using DB-API):</p> <pre><code>yValues = pickInterestingValuesOfY() sql = "..." # includes a clause stating that "y must be in yValues" c.execute(sql, yValues) </code></pre> <p>In the end, the SQL being executed could be something as simple as</p> <pre><...
2
2010-01-13T08:07:04Z
24,689,764
<p>For MySQL, you can do this:</p> <pre><code>SELECT x FROM table1 WHERE y IN (SELECT NULL FROM DUAL WHERE 0); </code></pre> <p><code>DUAL</code> is a virtual table which is born for this purpose. It's suitable for selecting data that doesn't require a table. And the <code>WHERE 0</code> clause ensures that no record...
0
2014-07-11T03:43:57Z
[ "python", "sql", "mysql", "sql-injection" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
2,055,374
<p>This should not be a problem, if you create a stand alone program using <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a> you only get the .pyc files. </p> <p>Normally you don't need to tell python to look for .pyc files, it does so anyway. Only if there is a newer .py source file this is used.</p> <p>How...
2
2010-01-13T08:37:10Z
[ "python", "c", "compilation", "bytecode" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
2,055,381
<p>I did it by creating .py library and simple .py program that uses that library. Then I compiled library to .pyc and distributed: program as .py source and library as compiled .pyc.</p>
4
2010-01-13T08:38:53Z
[ "python", "c", "compilation", "bytecode" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
2,055,395
<p>Use the <a href="http://wiki.python.org/moin/Freeze" rel="nofollow">freeze</a> tool, which is included in the Python source tree as <a href="http://svn.python.org/view/python/trunk/Tools/freeze/" rel="nofollow">Tools/freeze</a>. It converts Python byte code to C arrays; with a C compiler you can embed all your modul...
11
2010-01-13T08:44:41Z
[ "python", "c", "compilation", "bytecode" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
2,055,402
<p>In the interactive interpreter, that's automatic - if there is no .py, the .pyc will still be used:</p> <pre><code>$ echo 'print "hello"' &gt; test.py $ python -m compileall . $ rm test.py $ python -m test hello $ </code></pre> <p>Could you just try if that works the same way with the API?</p> <p>Edited to add: I...
2
2010-01-13T08:46:06Z
[ "python", "c", "compilation", "bytecode" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
2,056,647
<p>Since you are writing your main program in C++, you can do whatever you want to protect your Python files. You could encrypt them for distribution, then decrypt them just in time to import them into the Python interpreter, for example.</p> <p>Since you are using PyImport_Import, you can write your own <code>__impo...
5
2010-01-13T12:44:11Z
[ "python", "c", "compilation", "bytecode" ]
How to protect Python source code?
2,055,355
<p>Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?</p>
8
2010-01-13T08:30:09Z
37,890,918
<p>The best way would be to Encrypt the code on disk and decrypt it in memory by using a custom module loader. However as everything that isn't 100 % unbreakable. If you don't want to implement this yourself check out <a href="https://www.philipp-mayr.de/python-protect-2/" rel="nofollow">https://www.philipp-mayr.de/pyt...
0
2016-06-17T21:54:46Z
[ "python", "c", "compilation", "bytecode" ]
is there a way to have a python application run invisibly?
2,055,458
<p>I have a cherrypy application that I want to control over http with a simple gui. The problem is I don't want both the cherrypy window and the gui running at the same time. Is there a way I can make the cherrypy applications window visible? </p> <p>Its being written for windows, which probably makes a difference</p...
0
2010-01-13T08:58:07Z
2,055,465
<p>Use <code>pythonw.exe</code> rather than <code>python.exe</code> It will start without a console window.</p> <p>If you're using py2exe, you need to change your setup file to not use a console</p> <pre><code>from distutils.core import setup import py2exe setup(windows=[{"script":"hello.py"}]) </code></pre>
2
2010-01-13T08:59:55Z
[ "python", "cherrypy" ]
python optimized mode
2,055,557
<p>Python can run script in optimized mode (-O) that turns off debugs like assert and if I remember also remove docstrings. I have no seen it used really and maybe it is just artifact of the past times. Is it being used? What for? </p> <p>Why isn't this useless thing being removed in Python 3?</p>
18
2010-01-13T09:21:00Z
2,055,594
<p>Removing assertions means a <em>small</em> performance benefit, so you could use this for "release" code. Anyway nobody uses it because many Python libraries are open sourced and thus the <code>help()</code> function should work.</p> <p>So, as long as there isn't any <em>real</em> optimization in this mode, you can...
2
2010-01-13T09:27:03Z
[ "python" ]
python optimized mode
2,055,557
<p>Python can run script in optimized mode (-O) that turns off debugs like assert and if I remember also remove docstrings. I have no seen it used really and maybe it is just artifact of the past times. Is it being used? What for? </p> <p>Why isn't this useless thing being removed in Python 3?</p>
18
2010-01-13T09:21:00Z
2,055,877
<p>Prepacked software in different Linux distributions often comes byte-compiled with -O. For example, this if from <a href="https://fedoraproject.org/wiki/Packaging/Python" rel="nofollow">Fedora packaging guidelines</a> for python applications:</p> <blockquote> <p>In the past it was common practice to %ghost .pyo f...
4
2010-01-13T10:15:27Z
[ "python" ]
python optimized mode
2,055,557
<p>Python can run script in optimized mode (-O) that turns off debugs like assert and if I remember also remove docstrings. I have no seen it used really and maybe it is just artifact of the past times. Is it being used? What for? </p> <p>Why isn't this useless thing being removed in Python 3?</p>
18
2010-01-13T09:21:00Z
2,083,834
<p>It saves a small amount of memory, and a small amount of disk space if you distribute any archive form containing only the <code>.pyo</code> files. (If you use <code>assert</code> a lot, and perhaps with complicated conditions, the savings can be not trivial and can extend to running time too).</p> <p>So, it's def...
22
2010-01-18T04:03:46Z
[ "python" ]
python optimized mode
2,055,557
<p>Python can run script in optimized mode (-O) that turns off debugs like assert and if I remember also remove docstrings. I have no seen it used really and maybe it is just artifact of the past times. Is it being used? What for? </p> <p>Why isn't this useless thing being removed in Python 3?</p>
18
2010-01-13T09:21:00Z
2,167,639
<p>python -O does the following currently:</p> <ul> <li>completely ignores asserts</li> <li>sets the special builtin name <code>__debug__</code> to False (which by default is True)</li> </ul> <p>and when called as python -OO</p> <ul> <li>removes docstrings from the code</li> </ul> <p>I don't know why everyone forge...
30
2010-01-30T11:01:05Z
[ "python" ]
is there any difference between 'a.b.a' and 'b.b.a'
2,055,604
<pre><code>class a(object): class b: a='aaa' print a.b.a#print 'aaa' b=a() print b.b.a#print 'aaa' </code></pre>
-3
2010-01-13T09:29:23Z
2,055,622
<p>Either ways, you are accessing: "outerclass/object.innerclass/object.member".</p>
0
2010-01-13T09:32:21Z
[ "python" ]
is there any difference between 'a.b.a' and 'b.b.a'
2,055,604
<pre><code>class a(object): class b: a='aaa' print a.b.a#print 'aaa' b=a() print b.b.a#print 'aaa' </code></pre>
-3
2010-01-13T09:29:23Z
2,055,624
<p>No.</p> <p>To create instance variables, you need to explicitly prefix them with <code>self.</code>, in the constructor method <code>__init__(self)</code>.</p> <p>In your code, you're simply assigning in the class scope, and those variables can be reached both ways.</p>
5
2010-01-13T09:32:43Z
[ "python" ]
is there any difference between 'a.b.a' and 'b.b.a'
2,055,604
<pre><code>class a(object): class b: a='aaa' print a.b.a#print 'aaa' b=a() print b.b.a#print 'aaa' </code></pre>
-3
2010-01-13T09:29:23Z
2,055,711
<p>Running your code and then <code>a.b.a is b.b.a</code> gives the result of <code>True</code>, which indicates that they are, indeed, referring to the same object - the class variable <code>a</code> of inner class <code>b</code>.</p>
4
2010-01-13T09:47:57Z
[ "python" ]
Python function local variable scope during exceptions
2,055,611
<p>Background: I'm doing COM programming of National Instruments' TestStand in Python. TestStand complains if objects aren't "released" properly (it pops up an "objects not released properly" debug dialog box). The way to release the TestStand COM objects in Python is to ensure all variables no longer contain the objec...
3
2010-01-13T09:30:22Z
2,055,621
<p>A function's scope is for the entire function. Handle this in <code>finally</code>.</p>
1
2010-01-13T09:32:04Z
[ "python", "exception", "exception-handling", "scope" ]
Python function local variable scope during exceptions
2,055,611
<p>Background: I'm doing COM programming of National Instruments' TestStand in Python. TestStand complains if objects aren't "released" properly (it pops up an "objects not released properly" debug dialog box). The way to release the TestStand COM objects in Python is to ensure all variables no longer contain the objec...
3
2010-01-13T09:30:22Z
2,057,995
<p>Your exception-handling is probably creating reference loops by keeping references to frames. As <a href="http://docs.python.org/library/inspect.html?highlight=frame#the-interpreter-stack" rel="nofollow">the docs</a> put it:</p> <blockquote> <p><strong>Note</strong> Keeping references to frame objects, as found...
2
2010-01-13T15:49:28Z
[ "python", "exception", "exception-handling", "scope" ]
Python function local variable scope during exceptions
2,055,611
<p>Background: I'm doing COM programming of National Instruments' TestStand in Python. TestStand complains if objects aren't "released" properly (it pops up an "objects not released properly" debug dialog box). The way to release the TestStand COM objects in Python is to ensure all variables no longer contain the objec...
3
2010-01-13T09:30:22Z
14,720,085
<p>According to <a href="http://stackoverflow.com/a/14719709/60075">this answer for another question</a>, it is possible to <a href="http://docs.python.org/2/library/inspect.html" rel="nofollow">inspect local variables on the frame in an exception traceback</a> via <code>tb_frame.f_locals</code>. So it does look as tho...
0
2013-02-06T01:27:57Z
[ "python", "exception", "exception-handling", "scope" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,055,750
<ol> <li>Open the file</li> <li>Read each line and parse the line via <code>line.split( )</code></li> </ol>
1
2010-01-13T09:53:39Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,055,769
<p>Not sure if it's the best way, but here's one way:</p> <pre><code>lines = open('file.txt') for line in lines: as_list = line.split() result = [as_list[0], as_list[1], as_list[2:]] print result </code></pre> <p>Result will contain</p> <pre><code>['index', 'command', ['param1', 'param2', 'param3']] </code>...
9
2010-01-13T09:56:55Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,055,775
<p>i typically write: </p> <pre><code>lines = open('a.txt').readlines() for line in lines: para = lines.split() index = int(para[0]) command = para[1] para1 = float(para[2]) ... </code></pre>
3
2010-01-13T09:57:52Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,055,809
<pre><code>def add_command(index, command, *params): index = int(index) #do what you need to with index, command and params here with open('commands.txt') as f: for line in f: add_command(*line.split()) </code></pre>
5
2010-01-13T10:03:21Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,055,872
<pre><code>&gt;&gt;&gt; for line in open("file"): ... line=line.rstrip().split(" ",2) ... line[0]=int(line[0]) ... line[2]=line[2].split() ... print line ... [1, 'command', ['param1', 'param2', 'param3']] </code></pre>
1
2010-01-13T10:14:23Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,056,334
<p>The Answer provided by <strong>cb160</strong> is correct and smart way, But, I did it in this way. In <strong>cb160</strong>'s code, Only thing is index should be in Integer format, as you have mentioned.</p> <p>In my below code, I added exceptions for empty lines in input file if there are any.</p> <pre><code>#Ex...
0
2010-01-13T11:48:58Z
[ "python", "string" ]
Help with Python strings
2,055,738
<p>I have a program which reads commands from a text file</p> <p>for example, the command syntax will be as follows and is a string</p> <p><code>'index command param1 param2 param3'</code></p> <p>The number of parameters is variable from 0 up to 3 index is an integer command is a string all the params are integers</...
1
2010-01-13T09:51:19Z
2,056,416
<p>If you use <strong>Python 3+</strong>, then following should be enough as indicated in <a href="http://www.python.org/dev/peps/pep-3132/" rel="nofollow">PEP 3132: Extended Iterable Unpacking</a>:</p> <pre><code>(index,command,*parameters) = line.split() </code></pre> <hr> <p>Otherwise, I like solution from James ...
1
2010-01-13T12:02:57Z
[ "python", "string" ]
What is the best approach to change primary keys in an existing Django app?
2,055,784
<p>I have an application which is in BETA mode. The model of this app has some classes with an explicit primary_key. As a consequence Django use the fields and doesn't create an id automatically.</p> <pre><code>class Something(models.Model): name = models.CharField(max_length=64, primary_key=True) </code></pre> <...
25
2010-01-13T10:00:16Z
2,055,923
<p>Currently you are failing because you are adding a pk column that breaks the NOT NULL and UNIQUE requirements.</p> <p>You should split the migration into <a href="http://south.readthedocs.org/en/latest/tutorial/part3.html#data-migrations" rel="nofollow">several steps</a>, separating schema migrations and data migra...
6
2010-01-13T10:25:20Z
[ "python", "django", "data-migration", "django-south" ]
What is the best approach to change primary keys in an existing Django app?
2,055,784
<p>I have an application which is in BETA mode. The model of this app has some classes with an explicit primary_key. As a consequence Django use the fields and doesn't create an id automatically.</p> <pre><code>class Something(models.Model): name = models.CharField(max_length=64, primary_key=True) </code></pre> <...
25
2010-01-13T10:00:16Z
2,056,129
<p>Agreed, your model is probably wrong.</p> <p>The formal primary key should always be a surrogate key. Never anything else. [Strong words. Been database designer since the 1980's. Important lessoned learned is this: everything is changeable, even when the users swear on their mothers' graves that the value canno...
46
2010-01-13T11:11:43Z
[ "python", "django", "data-migration", "django-south" ]
What is the best approach to change primary keys in an existing Django app?
2,055,784
<p>I have an application which is in BETA mode. The model of this app has some classes with an explicit primary_key. As a consequence Django use the fields and doesn't create an id automatically.</p> <pre><code>class Something(models.Model): name = models.CharField(max_length=64, primary_key=True) </code></pre> <...
25
2010-01-13T10:00:16Z
10,250,962
<p>To change primary key with south you can use south.db.create_primary_key command in datamigration. To change your custom CharField pk to standard AutoField you should do:</p> <p>1) create new field in your model</p> <pre><code>class MyModel(Model): id = models.AutoField(null=True) </code></pre> <p>1.1) if you...
9
2012-04-20T17:42:38Z
[ "python", "django", "data-migration", "django-south" ]
What is the best approach to change primary keys in an existing Django app?
2,055,784
<p>I have an application which is in BETA mode. The model of this app has some classes with an explicit primary_key. As a consequence Django use the fields and doesn't create an id automatically.</p> <pre><code>class Something(models.Model): name = models.CharField(max_length=64, primary_key=True) </code></pre> <...
25
2010-01-13T10:00:16Z
12,247,601
<p>I had the same problem to day and came to a solution inspired by the answers above.</p> <p>My model has a "Location" table. It has a CharField called "unique_id" and I foolishly made it a primary key, last year. Of course they didn't turn out to be as unique as expected at the time. There is also a "ScheduledMeasur...
5
2012-09-03T12:08:00Z
[ "python", "django", "data-migration", "django-south" ]
All members package/module defines?
2,055,961
<p>How can I know which members module/package defines? By defining I mean:</p> <p><strong>somemodule.py</strong></p> <pre><code>import os # &lt;-- Not defined in this module from os.path import sep # &lt;-- Not defined in this module I_AM_ATTRIBUTE = None # &lt;-- Is defined in this module class SomeClass(object):...
1
2010-01-13T10:33:38Z
2,056,030
<p>There is no way to do this. This is because simple attributes (like <code>I_AM_ATTRIBUTE</code> in your example) are simply values stored in the module's dictionary. When they are copied to another module, they are placed in that module's dictionary as well, and there is no way to tell which was the original locatio...
1
2010-01-13T10:46:53Z
[ "python" ]
All members package/module defines?
2,055,961
<p>How can I know which members module/package defines? By defining I mean:</p> <p><strong>somemodule.py</strong></p> <pre><code>import os # &lt;-- Not defined in this module from os.path import sep # &lt;-- Not defined in this module I_AM_ATTRIBUTE = None # &lt;-- Is defined in this module class SomeClass(object):...
1
2010-01-13T10:33:38Z
2,056,189
<p>What does "define" mean? For me, <em>defined</em> means that some symbol is part of the module's public interface. This matches up with how it seems you're trying to use the term, including in your example, but if you have some purpose which doesn't align with this definition, you'll have to clarify the question o...
1
2010-01-13T11:24:50Z
[ "python" ]
What are the major differences in object models of dynamic languages like Smalltalk, Ruby and Python
2,055,966
<p>I dived into understanding the Ruby object model in the last weeks, and although so far was only a user of the fruits of ruby's and python's object in the past, I became curious how these things might differ in other languages.</p> <p>Years ago I touched smalltalk's squeak. Smalltalk is often figuring as a referent...
3
2010-01-13T10:34:23Z
2,057,309
<p>The main difference between Python and Smalltalk that I remember is the way attribute privacy is handled. In Smalltalk I defined attributes and had to generate all the accessors instantly (fortunately Dolphin Smalltalk did this) and the use them. On the other hand in Python everything can be accessed, even attribute...
2
2010-01-13T14:31:24Z
[ "python", "ruby", "comparison", "oop", "smalltalk" ]
What are the major differences in object models of dynamic languages like Smalltalk, Ruby and Python
2,055,966
<p>I dived into understanding the Ruby object model in the last weeks, and although so far was only a user of the fruits of ruby's and python's object in the past, I became curious how these things might differ in other languages.</p> <p>Years ago I touched smalltalk's squeak. Smalltalk is often figuring as a referent...
3
2010-01-13T10:34:23Z
2,058,158
<p>In Python, each object has <em>one</em> namespace -- the object's "attributes", accessible in that namespace, can be methods, simple references to other objects (callable or not), or be synthesized on the fly by <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="nofollow">descriptors</a> (plain attri...
2
2010-01-13T16:06:35Z
[ "python", "ruby", "comparison", "oop", "smalltalk" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,357
<p>Easy, you can try this</p> <pre><code>del a[0][0] </code></pre>
5
2010-01-13T11:53:15Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,358
<pre><code>a[0].remove(3) </code></pre> <p>(had to add more text so it was long enough)</p>
2
2010-01-13T11:53:18Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,365
<p>Using this:</p> <pre><code>del a[0][0] </code></pre> <p>For a better understanding of lists, dictionaries, etc., I suggest you should read <a href="http://diveintopython3.ep.io/" rel="nofollow">Dive Into Python</a> You'll find Chapter 3 very useful.</p>
2
2010-01-13T11:55:50Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,367
<p>If I understand your question you have list in a list and you want to delete first element from first list. So use:</p> <pre><code>del a[0][0] </code></pre>
0
2010-01-13T11:56:36Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,387
<p>if you don't know where "3" is, </p> <pre><code>&gt;&gt;&gt; for n,i in enumerate(list): ... if 3 in i: list[n].remove(3) ... &gt;&gt;&gt; list [[4], [5], [6, 7, 8]] &gt;&gt;&gt; </code></pre>
2
2010-01-13T11:59:37Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,388
<p>First of all, be careful because you are shadowing the built in name "list". This </p> <pre><code>a_list = [[3,4],[5],[6,7,3, 3, 8]] def clear_list_from_item(a_list, item): try: while True: a_list.remove(item) except ValueError: return a_list a_list = [clear_list_from_item(x, 3) for x in a_lis...
0
2010-01-13T11:59:44Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,390
<p>lots of possible ways</p> <pre><code>&gt;&gt;&gt; mylist = [[3,4],[5],[6,7,8]] &gt;&gt;&gt; mylist[0] = [4] &gt;&gt;&gt; mylist [[4], [5], [6, 7, 8]] &gt;&gt;&gt; mylist = [[3,4],[5],[6,7,8]] &gt;&gt;&gt; del mylist[0][0] &gt;&gt;&gt; mylist [[4], [5], [6, 7, 8]] &gt;&gt;&gt; mylist = [[3,4],[5],[6,7,8]] &gt;&gt;&g...
11
2010-01-13T11:59:53Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,056,395
<p>Assuming, you want to delete all 3s from a list of lists:</p> <pre><code>&gt;&gt;&gt; lst = [[3,4],[5],[6,7,8]] &gt;&gt;&gt; [[i for i in el if i != 3] for el in lst] [[4], [5], [6, 7, 8]] </code></pre>
1
2010-01-13T12:00:38Z
[ "python" ]
How to delete element from list in Python?
2,056,341
<p>I am new to python. I have created a list</p> <pre><code>a = [[3,4],[5],[6,7,8]] </code></pre> <p>I want to delete 3 from this list. What is the command for this?</p>
1
2010-01-13T11:50:41Z
2,057,396
<p>two easy ways to remove from a list, if you know the element of concern is at position 0: </p> <pre><code>a[0].pop (0) del a[0][0] </code></pre>
0
2010-01-13T14:43:12Z
[ "python" ]
Django custom auth backend not recognized on Apache
2,056,384
<p>I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on:</p> <pre><code>mypro...
2
2010-01-13T11:59:15Z
2,056,436
<p>The problem is that python cannot find the module <code>twitteroauth</code>. What is the name of the file <code>TwitterBackend</code> is in? Also make sure that there is a <code>__init__.py</code> file in <code>backends</code> to mark it as a package.</p> <p><strong>edit:</strong></p> <p>What happens if you run th...
2
2010-01-13T12:07:13Z
[ "python", "django", "mod-python" ]
Django custom auth backend not recognized on Apache
2,056,384
<p>I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on:</p> <pre><code>mypro...
2
2010-01-13T11:59:15Z
2,057,232
<p>Make sure that backends is on the python path and has a <strong>init</strong>.py file in the folder.</p>
1
2010-01-13T14:19:44Z
[ "python", "django", "mod-python" ]
Django custom auth backend not recognized on Apache
2,056,384
<p>I'm trying to deploy my Django application to an Apache2 based server with mod_python. I've set the handlers right and made the configuration to make mod_python work with my project. My project implements a custom auth backend to connect my users to twitter, and my backend implementation is on:</p> <pre><code>mypro...
2
2010-01-13T11:59:15Z
2,058,324
<p>Removing database solved my problem. As far as I can guess, if a user is logged, his corresponding login backend is kept as a session variable on the database. My settings.py file was</p> <pre><code>AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'backends.twitteroauth.TwitterBackend'...
2
2010-01-13T16:23:00Z
[ "python", "django", "mod-python" ]
Recover process with subprocess.Popen?
2,056,594
<p>I have a python program that uses subprocess.Popen to launch another process (python process or whatever), and after launching it I save the child's PID to a file. Let's suppose that suddenly the parent process dies (because of an exception or whatever). Is there any way to access again to the object returned by Pop...
1
2010-01-13T12:33:59Z
2,056,605
<p>Looks like you're trying to write a daemon, and it needs pidfile support. You can't go wrong with <a href="http://pypi.python.org/pypi/python-daemon/" rel="nofollow">python-daemon</a>.</p> <p>For example:</p> <pre><code>import daemon import lockfile import os with daemon.DaemonContext(pidfile=lockfile.FileLock('/...
0
2010-01-13T12:36:46Z
[ "python", "popen" ]
Recover process with subprocess.Popen?
2,056,594
<p>I have a python program that uses subprocess.Popen to launch another process (python process or whatever), and after launching it I save the child's PID to a file. Let's suppose that suddenly the parent process dies (because of an exception or whatever). Is there any way to access again to the object returned by Pop...
1
2010-01-13T12:33:59Z
2,056,745
<p>The Popen object is effectively just a wrapper for the child processes PID, stdin, stdout, and stderr, plus some convenience functions for using those.</p> <p>So the question is why do you need access to the Popen object? Do you want to communicate with the child, terminate it, or check whether it's still running?<...
2
2010-01-13T13:02:51Z
[ "python", "popen" ]
Recover process with subprocess.Popen?
2,056,594
<p>I have a python program that uses subprocess.Popen to launch another process (python process or whatever), and after launching it I save the child's PID to a file. Let's suppose that suddenly the parent process dies (because of an exception or whatever). Is there any way to access again to the object returned by Pop...
1
2010-01-13T12:33:59Z
2,056,808
<p>If a process dies, all its open file handles are closed. This includes any unnamed pipes created by <code>popen()</code>. So, no, there's no way to recover a <code>Popen</code> object from just a PID. The OS won't even consider your new process the parent, so you won't even get <code>SIGCHLD</code> signals (thoug...
1
2010-01-13T13:13:38Z
[ "python", "popen" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,056,762
<p>Do you want to create new fields/getters/setters in the class? If you want to do this in runtime, you have to create completely new class with your fields and methods, and load it into the JVM. To create new class you can use library like ASM or CGLib, but if you're new to Java, this isn't something you want to star...
0
2010-01-13T13:06:23Z
[ "java", "python", "properties" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,056,781
<p>There's no such facility built into Java language. You have to write all the getters and setters explicitly by yourself. IDEs like Eclipse can generate this boilerplate code for you though.</p> <p>For example :</p> <pre><code>class Point{ private int x, y; public Point(int x, int y){ this.x = x; this....
7
2010-01-13T13:08:30Z
[ "java", "python", "properties" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,056,814
<p>They don't really exist. In Java it's common practice to declare members as <code>private</code> or <code>protected</code> and only allow access to them via methods. Often this leads to lots of small <code>getFoo()</code> and <code>setFoo(newFoo)</code> methods. Python doesn't really have <code>private</code> and <c...
2
2010-01-13T13:14:46Z
[ "java", "python", "properties" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,056,928
<p>As others have noted, java has getters and setters, but there is no strict analogon. There is a third party library called <a href="http://projectlombok.org/" rel="nofollow">Project Lombok</a> tha uses annotation to generate the getters and setters in the .class files at comile time. This could be used to make thing...
1
2010-01-13T13:33:35Z
[ "java", "python", "properties" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,057,025
<p>Actually you may simulate this behavior in Java.</p> <p><strong>WARNING: ugly solution below</strong></p> <p>You can write a method in an utility class like the code below:</p> <pre><code>public Object getProperty(String property, Object obj) { if (obj != null &amp;&amp; property != null) { Field field =...
-1
2010-01-13T13:48:50Z
[ "java", "python", "properties" ]
What is the Java Equivalent of Python's property()?
2,056,752
<p>I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thin...
7
2010-01-13T13:03:49Z
2,057,125
<p>the built-in property() function does exactly the opposite of what's described here in the answers. it's not about generating getters and setters for member variables. it just allows you to call a method by accessing a property (so, although you just access a variable in te Python class, a function will be called). ...
3
2010-01-13T14:03:27Z
[ "java", "python", "properties" ]
python - os.makedirs don't understand ~ in my path?
2,057,045
<p>I have a little problem with ~ in my paths.</p> <p>This code example creates some dirs called "~/some_dir", and do not understand that I wanted to create some_dir in my home dir.</p> <pre><code>my_dir = "~/some_dir" if not os.path.exists(my_dir): os.makedirs(my_dir) </code></pre> <p>Note this is on a linux b...
37
2010-01-13T13:51:09Z
2,057,072
<p>you need to expand the tilde manually:</p> <pre><code>my_dir = os.path.expanduser('~/some_dir') </code></pre>
83
2010-01-13T13:55:50Z
[ "python", "path" ]