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
group by in django
475,552
<p>How can i create simple group by query in trunk version of django?</p> <p>I need something like</p> <pre><code>SELECT name FROM mytable GROUP BY name </code></pre> <p>actually what i want to do is simply get all entries with distinct names. </p>
3
2009-01-24T05:15:15Z
476,156
<p>If you need all the distinct names, just do this:</p> <pre><code>Foo.objects.values('name').distinct() </code></pre> <p>And you'll get a list of dictionaries, each one with a <strong>name</strong> key. If you need other data, just add more attribute names as parameters to the .values() call. Of course, if you ad...
12
2009-01-24T15:37:28Z
[ "python", "django", "django-models" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
475,873
<p><code>A.x</code> is a <em>class variable</em>. <code>B</code>'s <code>self.x</code> is an <em>instance variable</em>.</p> <p>i.e. <code>A</code>'s <code>x</code> is shared between instances.</p> <p>It would be easier to demonstrate the difference with something that can be modified like a list:</p> <pre><code>#!/...
107
2009-01-24T11:17:47Z
[ "python", "oop" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
475,905
<p>A.x is a class variable, and will be shared across all instances of A, unless specifically overridden within an instance. B.x is an instance variable, and each instance of B has its own version of it.</p> <p>I hope the following Python example can clarify:</p> <pre><code> >>> class Foo(): ... i = 3 ...
17
2009-01-24T11:41:32Z
[ "python", "oop" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
475,919
<p>Just as a side note: <code>self</code> is actually just a randomly chosen word, that everyone uses, but you could also use <code>this</code>, <code>foo</code>, or <code>myself</code> or anything else you want, it's just the first parameter of every non static method for a class. This means that the word <code>self</...
42
2009-01-24T11:57:59Z
[ "python", "oop" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
475,924
<p>There have been lately some interesting posts regarding the use of <code>self</code> between Bruce Eckel and GvR. Check it out: <a href="http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html" rel="nofollow">this</a> vs. <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=239003" rel="nofo...
-1
2009-01-24T12:03:13Z
[ "python", "oop" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
11,342,486
<p>I used to explain it with this example</p> <pre><code># By TMOTTM class Machine: # Class Variable counts how many machines have been created. # The value is the same for all objects of this class. counter = 0 def __init__(self): # Notice: no 'self'. Machine.counter += 1 ...
13
2012-07-05T10:30:27Z
[ "python", "oop" ]
Python - why use "self" in a class?
475,871
<p>How do these 2 classes differ?</p> <pre><code>class A(): x=3 class B(): def __init__(self): self.x=3 </code></pre> <p>Is there any significant difference? </p>
68
2009-01-24T11:13:19Z
25,025,527
<p>I've just started learning Python and this confused me as well for some time. Trying to figure out how it all works in general I came up with this very simple piece of code:</p> <pre><code># Create a class with a variable inside and an instance of that class class One: color = 'green' obj2 = One() # Here we ...
2
2014-07-29T21:47:11Z
[ "python", "oop" ]
python IPC (Inter Process Communication) for Vista UAC (User Access Control)
475,928
<p>I am writing a Filemanager in (wx)python - a lot already works. When copying files there is already a progress dialog, overwrite handling etc.</p> <p>Now in Vista when the user wants to copy a file to certain directories (eg %Program Files%) the application/script needs elevation, which cannot be asked for at runti...
4
2009-01-24T12:05:46Z
476,056
<p>How about just communicating with the second process using stdin/stdout?</p> <p>There are some caveats due to input and output buffering, but take a look at <a href="http://code.activestate.com/recipes/440554/" rel="nofollow">this Python Cookbook recipe</a>, and also <a href="http://www.noah.org/wiki/Pexpect" rel="...
2
2009-01-24T14:00:50Z
[ "python", "windows-vista", "wxpython", "ipc", "vista-security" ]
Order a QuerySet by aggregate field value
476,017
<p>Let's say I have the following model:</p> <pre><code>class Contest: title = models.CharField( max_length = 200 ) description = models.TextField() class Image: title = models.CharField( max_length = 200 ) description = models.TextField() contest = models.ForeignKey( Contest ) user = models.F...
24
2009-01-24T13:26:58Z
476,024
<p>You can write your own sort in Python very simply.</p> <pre><code>def getScore( anObject ): return anObject.score() objects= list(Contest.objects.get( pk = id ).image_set) objects.sort( key=getScore ) </code></pre> <p>This works nicely because we sorted the list, which we're going to provide to the template.</...
9
2009-01-24T13:34:36Z
[ "python", "database", "django", "order" ]
Order a QuerySet by aggregate field value
476,017
<p>Let's say I have the following model:</p> <pre><code>class Contest: title = models.CharField( max_length = 200 ) description = models.TextField() class Image: title = models.CharField( max_length = 200 ) description = models.TextField() contest = models.ForeignKey( Contest ) user = models.F...
24
2009-01-24T13:26:58Z
476,025
<p>The db-level <code>order_by</code> cannot sort queryset by model's python method.</p> <p>The solution is to introduce <code>score</code> field to <code>Image</code> model and recalculate it on every <code>Vote</code> update. Some sort of denormalization. When you will can to sort by it.</p>
0
2009-01-24T13:36:40Z
[ "python", "database", "django", "order" ]
Order a QuerySet by aggregate field value
476,017
<p>Let's say I have the following model:</p> <pre><code>class Contest: title = models.CharField( max_length = 200 ) description = models.TextField() class Image: title = models.CharField( max_length = 200 ) description = models.TextField() contest = models.ForeignKey( Contest ) user = models.F...
24
2009-01-24T13:26:58Z
476,033
<p>Oh, of course I forget about new aggregation support in Django and its <code>annotate</code> functionality.</p> <p>So query may look like this:</p> <pre><code>Contest.objects.get(pk=id).image_set.annotate(score=Sum('vote__value')).order_by( 'score' ) </code></pre>
44
2009-01-24T13:44:37Z
[ "python", "database", "django", "order" ]
The OLE way of doing drag&drop in wxPython
476,142
<p>I have wxPython app which is running on MS Windows and I'd like it to support drag&amp;drop between its instances (so the user opens my app 3 times and drags data from one instance to another).</p> <p>The simple drag&amp;drop in wxPython works that way:</p> <ol> <li><strong>User initiates drag</strong>: The source...
2
2009-01-24T15:29:45Z
476,570
<p>Since you can't use one of the <a href="http://www.wxpython.org/docs/api/wx.DataFormat-class.html" rel="nofollow">standard</a> data formats to store references to python objects I would recommend you use a text data format for storing the parameters you need for your method calls rather than making a new data format...
3
2009-01-24T19:59:58Z
[ "python", "windows", "drag-and-drop", "wxpython", "ole" ]
The OLE way of doing drag&drop in wxPython
476,142
<p>I have wxPython app which is running on MS Windows and I'd like it to support drag&amp;drop between its instances (so the user opens my app 3 times and drags data from one instance to another).</p> <p>The simple drag&amp;drop in wxPython works that way:</p> <ol> <li><strong>User initiates drag</strong>: The source...
2
2009-01-24T15:29:45Z
479,987
<p>Ok, it seems that it can't be done the way I wanted it.</p> <p>Possible solutions are: </p> <ol> <li>Pass some parameters in d&amp;d and do some inter-process communication on your own, after user drops data in target processes window. </li> <li>Use <a href="http://www.wxpython.org/docs/api/wx.DataObjectComposit...
0
2009-01-26T14:40:39Z
[ "python", "windows", "drag-and-drop", "wxpython", "ole" ]
Where can i get a wiki formatting widget for my django application?
476,187
<p>Am looking for a wiki formatting widget for my django application, just like the one am using to type this question in stackoverflow?</p>
2
2009-01-24T15:49:59Z
476,415
<p>Stack Overflow uses <a href="http://wmd-editor.com/" rel="nofollow">WMD</a> for their editing. This is an editor for the <strong>Markdown</strong> language, which, while not <em>strictly</em> wiki markup, is quite close.<br /> An un-obfuscated Stack Overflow-edition version of WMD is available <a href="http://github...
2
2009-01-24T18:02:19Z
[ "python", "django", "wiki" ]
Where can i get a wiki formatting widget for my django application?
476,187
<p>Am looking for a wiki formatting widget for my django application, just like the one am using to type this question in stackoverflow?</p>
2
2009-01-24T15:49:59Z
476,546
<p>There are two parts to this question. If you're looking for the client-side Javascript WYSIWYG (or WYSIWYM) editor widget, that's unrelated to Django and WMD is a fine choice (though personally I prefer <a href="http://markitup.jaysalvat.com" rel="nofollow">MarkItUp!</a>).</p> <p>If you're looking for the server-s...
3
2009-01-24T19:42:30Z
[ "python", "django", "wiki" ]
Where can i get a wiki formatting widget for my django application?
476,187
<p>Am looking for a wiki formatting widget for my django application, just like the one am using to type this question in stackoverflow?</p>
2
2009-01-24T15:49:59Z
9,243,900
<p>You can check out my plugin for Django CMS, which adds Trac wiki syntax (with Trac macro support):</p> <p><a href="https://bitbucket.org/mitar/cmsplugin-markup-tracwiki" rel="nofollow">https://bitbucket.org/mitar/cmsplugin-markup-tracwiki</a></p>
0
2012-02-11T20:43:46Z
[ "python", "django", "wiki" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
476,258
<p>I don't think simple checksums will ever work:</p> <ol> <li>ID3 tags will affect the md5</li> <li>Different encoders will encode the same song different ways - so the checksums will be different</li> <li>Different bit-rates will produce different checksums</li> <li>Re-encoding an mp3 to a different bit-rate will pr...
2
2009-01-24T16:26:11Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
476,307
<p>Re-encoding at the same bit rate won't work, in fact it may make things worse as transcoding (that is what re-encoding at different bitrates is called) is going to change the nature of the compression, you are recompressing an already compressed file is going to lead to a significantly different file.</p> <p>This i...
2
2009-01-24T16:47:41Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
476,382
<p>The exact same question that people at the old AudioScrobbler and currently at <a href="http://musicbrainz.org/">MusicBrainz</a> have worked on since long ago. For the time being, the Python project that can aid in your quest, is <a href="http://musicbrainz.org/doc/PicardDownload">Picard</a>, which will tag audio fi...
13
2009-01-24T17:39:41Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
477,814
<p>Like the others said, simple checksums won't detect duplicates with different bitrates or ID3 tags. What you need is an audio fingerprint algorithm. The Python Audioprocessing Suite has such an an algorithm, but I can't say anything about how reliable it is.</p> <p><a href="http://rudd-o.com/new-projects/python-aud...
4
2009-01-25T15:24:26Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
585,671
<p>For tag issues, <a href="http://musicbrainz.org/doc/PicardTagger" rel="nofollow">Picard</a> may indeed be a very good bet. If, having identified two potentially duplicate files, what you want is to extract bitrate information from them, have a look at <a href="http://shibatch.sourceforge.net/" rel="nofollow">mp3gues...
3
2009-02-25T11:43:03Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
2,397,542
<p>I'm looking for something similar and I found this:<br> <a href="http://www.lastfm.es/user/nova77LF/journal/2007/10/12/4kaf_fingerprint_(command_line)_client" rel="nofollow">http://www.lastfm.es/user/nova77LF/journal/2007/10/12/4kaf_fingerprint_(command_line)_client</a></p> <p>Hope it helps.</p>
1
2010-03-07T19:14:15Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
4,750,190
<p>I'd use length as my primary heuristic. That's what iTunes does when it's trying to identify a CD using the <a href="http://en.wikipedia.org/wiki/Gracenote" rel="nofollow">Gracenote database</a>. <a href="http://stackoverflow.com/questions/993971/mp3-length-in-milliseconds">Measure the lengths in milliseconds</a> ra...
1
2011-01-20T17:03:58Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
23,745,588
<p>The Dejavu project is written in Python and does exactly what you are looking for. </p> <p><a href="https://github.com/worldveil/dejavu" rel="nofollow">https://github.com/worldveil/dejavu</a></p> <p>It also supports many common formats (.wav, .mp3, etc) as well as finding the time offset of the clip in the origina...
2
2014-05-19T19:20:45Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Detect duplicate MP3 files with different bitrates and/or different ID3 tags?
476,227
<p>How could I detect (preferably with Python) duplicate MP3 files that can be encoded with different bitrates (but they are the same song) and ID3 tags that can be incorrect?</p> <p>I know I can do an <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> checksum of the files content but that won't work for different bi...
12
2009-01-24T16:14:12Z
27,886,496
<p>You can use the successor for PUID and MusicBrainz, called <strong><a href="https://musicbrainz.org/doc/AcoustID" rel="nofollow">AcoustiD</a></strong>:</p> <blockquote> <p>AcoustID is an open source project that aims to create a free database of audio fingerprints with mapping to the MusicBrainz metadata database...
1
2015-01-11T11:25:44Z
[ "python", "file", "mp3", "duplicates", "id3" ]
Trimming Mako output
476,324
<p>I really like the Mako templating system that's used in Pylons and a couple other Python frameworks, and my only complaint is how much WS leaks through even a simple inheritance scheme.</p> <p>Is there anyway to accomplish below, without creating such huge WS gaps... or packing my code in like I started to do with ...
4
2009-01-24T16:56:22Z
476,428
<p>Found my own answer <a href="http://docs.makotemplates.org/en/latest/filtering.html" rel="nofollow">http://docs.makotemplates.org/en/latest/filtering.html</a></p> <p>It still required some trial and error, but using</p> <pre><code>t = TemplateLookup(directories=['/tmp'], default_filters=['trim']) </code></pre> <p...
2
2009-01-24T18:14:20Z
[ "python", "layout", "template-engine", "mako" ]
Is there a way to install the scipy special module without the rest of scipy?
476,369
<p>I'm writing some Python numerical code and would like to use some functions from the <a href="http://scipy.org/SciPyPackages/Special" rel="nofollow">special module</a>. So far, my code only depends on numpy, which I've found very easy to install in a variety of Python environments. Installing scipy, on the other han...
3
2009-01-24T17:26:24Z
476,376
<p>I'm not familiar with <em>scipy</em> in particular, but in general, modules for software packages depend on the installation of the package itself. So, yes, I'm pretty sure that you need to install <em>scipy</em> to use the special module.</p>
0
2009-01-24T17:35:03Z
[ "python", "numpy", "package", "scipy" ]
Is there a way to install the scipy special module without the rest of scipy?
476,369
<p>I'm writing some Python numerical code and would like to use some functions from the <a href="http://scipy.org/SciPyPackages/Special" rel="nofollow">special module</a>. So far, my code only depends on numpy, which I've found very easy to install in a variety of Python environments. Installing scipy, on the other han...
3
2009-01-24T17:26:24Z
476,655
<p>If you already have the right version of Numpy installed then you could try to just take the source for the special module, stick it in a directory somewhere, add it to your PYTHONPATH, and see if it works. It all depends on its dependencies. If it has dependencies beyond Numpy, then you'll have to install those, ...
0
2009-01-24T21:07:12Z
[ "python", "numpy", "package", "scipy" ]
Is there a way to install the scipy special module without the rest of scipy?
476,369
<p>I'm writing some Python numerical code and would like to use some functions from the <a href="http://scipy.org/SciPyPackages/Special" rel="nofollow">special module</a>. So far, my code only depends on numpy, which I've found very easy to install in a variety of Python environments. Installing scipy, on the other han...
3
2009-01-24T17:26:24Z
476,886
<p>The scipy subpackages can usually be installed individually. Try cd-ing to the "<code>special</code>" directory and running your normal "<code>python setup.py install</code>". The name space for importing should now be <code>special</code> and now <code>scipy.special</code>.</p>
2
2009-01-24T23:35:24Z
[ "python", "numpy", "package", "scipy" ]
Django "Did you mean?" query
476,394
<p>I am writing a fairly simple Django application where users can enter string queries. The application will the search through the database for this string.</p> <pre><code>Entry.objects.filter(headline__contains=query) </code></pre> <p>This query is pretty strait forward but not really helpful to someone who isn't ...
3
2009-01-24T17:47:30Z
476,420
<p>You could try using python's <a href="http://docs.python.org/library/difflib">difflib</a> module.</p> <pre><code>&gt;&gt;&gt; from difflib import get_close_matches &gt;&gt;&gt; get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape'] &gt;&gt;&gt; import keyword &gt;&gt;&gt; get_close_matches(...
9
2009-01-24T18:07:09Z
[ "python", "django", "spell-checking" ]
Django "Did you mean?" query
476,394
<p>I am writing a fairly simple Django application where users can enter string queries. The application will the search through the database for this string.</p> <pre><code>Entry.objects.filter(headline__contains=query) </code></pre> <p>This query is pretty strait forward but not really helpful to someone who isn't ...
3
2009-01-24T17:47:30Z
476,527
<p>djangos orm doesn't have this behavior out-of-box, but there are several projects that integrate django w/ search services like: </p> <ul> <li>sphinx (<a href="http://github.com/dcramer/django-sphinx/tree/master" rel="nofollow">django-sphinx</a>)</li> <li>solr, a lightweight version of lucene (<a href="https://code...
5
2009-01-24T19:31:54Z
[ "python", "django", "spell-checking" ]
Python Regular Expression to add links to urls
476,478
<p>I'm trying to make a regular expression that will correctly capture URLs, including ones that are wrapped in parenthesis as in (<a href="http://example.com" rel="nofollow">http://example.com</a>) and spoken about on coding horror at <a href="http://www.codinghorror.com/blog/archives/001181.html" rel="nofollow">http:...
4
2009-01-24T18:48:33Z
476,493
<p>Problem is, URLs could have parenthesis as part of them... (<a href="http://en.wikipedia.org/wiki/Tropical_Storm_Alberto_(2006)" rel="nofollow">http://en.wikipedia.org/wiki/Tropical_Storm_Alberto_(2006)</a>) . You can't treat that with regexp alone, since it doesn't have state. You need a parser. So your best chance...
4
2009-01-24T18:57:39Z
[ "python", "regex", "url" ]
Resolving a relative url path to its absolute path
476,511
<p>Is there a library in python that works like this?</p> <pre><code>&gt;&gt;&gt; resolvePath("http://www.asite.com/folder/currentpage.html", "anotherpage.html") 'http://www.asite.com/folder/anotherpage.html' &gt;&gt;&gt; resolvePath("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") 'http://w...
43
2009-01-24T19:09:59Z
476,521
<p>Yes, there is <a href="https://docs.python.org/2/library/urlparse.html#urlparse.urljoin"><code>urlparse.urljoin</code></a>, or <a href="https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin"><code>urllib.parse.urljoin</code></a> for Python 3.</p> <pre><code>&gt;&gt;&gt; try: from urlparse import ...
70
2009-01-24T19:20:19Z
[ "python", "url", "path" ]
Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?
476,586
<p>I recently discovered metaclasses in python. </p> <p>Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritan...
13
2009-01-24T20:10:48Z
476,633
<p>To answer your question: no.</p> <p>Feel free to research it further. </p> <p>Note, however, that you've conflated design patterns (which are just ideas) with code (which is an implementation.)</p> <p>Good code often reflects a number of interlocking design patterns. There's no easy way for formalize this. The...
6
2009-01-24T20:49:28Z
[ "python", "metaprogramming", "design-patterns", "factory", "metaclass" ]
Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?
476,586
<p>I recently discovered metaclasses in python. </p> <p>Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritan...
13
2009-01-24T20:10:48Z
476,641
<p>This reminds me of the eternal quest some people seem to be on to make a "generic implementation of a pattern." Like a factory that can create any object (<a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12">including another factory</a>), or a general-purpose dependency injection framework that...
18
2009-01-24T21:00:00Z
[ "python", "metaprogramming", "design-patterns", "factory", "metaclass" ]
Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?
476,586
<p>I recently discovered metaclasses in python. </p> <p>Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritan...
13
2009-01-24T20:10:48Z
476,743
<p>The class system in Smalltalk is an interesting one to study. In Smalltalk, everything is an object and every object has a class. This doesn't imply that the hierarchy goes to infinity. If I remember correctly, it goes something like:</p> <p>5 -> Integer -> Integer class -> Metaclass -> Metaclass class -> Metaclass...
4
2009-01-24T22:08:59Z
[ "python", "metaprogramming", "design-patterns", "factory", "metaclass" ]
Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?
476,586
<p>I recently discovered metaclasses in python. </p> <p>Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritan...
13
2009-01-24T20:10:48Z
2,090,276
<p>During the History of Programming Languages conference in 2007, Simon Peyton Jones commented that Haskell allows meta programming using Type Classes, but that its really turtles all the way down. You can meta-meta-meta-meta etc program in Haskell, but that he's never heard of anyone using more than 3 levels of indir...
5
2010-01-19T00:39:14Z
[ "python", "metaprogramming", "design-patterns", "factory", "metaclass" ]
Cygwin and Python 2.6
476,659
<p>New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff.</p> <p>Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin com...
2
2009-01-24T21:09:26Z
476,666
<p>There are builds of python which don't require cygwin. For instance (from python.org):</p> <p><a href="http://www.python.org/download/releases/" rel="nofollow">link text</a></p> <p>Also, there is the .NET version called Iron Python:</p> <p><a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" re...
2
2009-01-24T21:16:23Z
[ "python", "windows-xp", "cygwin", "python-2.6" ]
Cygwin and Python 2.6
476,659
<p>New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff.</p> <p>Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin com...
2
2009-01-24T21:09:26Z
476,809
<p>cygwin is effectively a Unix subkernel. Setup and installed in its default manner it won't interrupt or change any existing Windows XP functionality. However, you'll have to start the cygwin equivalent of the command prompt before you can use its functionality. </p> <p>With that said, some of the functionality you'...
1
2009-01-24T22:42:08Z
[ "python", "windows-xp", "cygwin", "python-2.6" ]
Cygwin and Python 2.6
476,659
<p>New to python (and programming). What exactly do I need from Cygwin? I'm running python 2.6 on winxp. Can I safely download the complete Cygwin? It just seems like a huge bundle of stuff.</p> <p>Well, I keep running into modules and functionality (i.e. piping output) which suggest downloading various cygwin com...
2
2009-01-24T21:09:26Z
10,878,598
<p>I would say the simplest option is to try a Linux Distro. I know if your new Linux can be intimidating, but when I looked at Ubuntu and started developing there my life was changed. Ubuntu is bloated (for linux) however, it comes with the things that I would expect a Microsoft based OS to come pre-packaged with. Th...
0
2012-06-04T08:47:26Z
[ "python", "windows-xp", "cygwin", "python-2.6" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
476,689
<p>I know that Kent Beck's book (which you mentioned) covers TDD in Python to some pretty good depth. If I remember correctly, the last half of the book takes you through development of a unit test framework in Python. There's nothing specific to web development, though, which is a problem in many TDD resources that ...
2
2009-01-24T21:32:14Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
476,690
<p>I'd recommend "xUnit Test Patterns: Refactoring Test Code" by Gerard Meszaros. It is not Python or Web specific, but it's a good book on TDD in general and the xUnit framework in particular. Since python unittest is actually an xUnit implementation ("a Python version of JUnit", as the docs say), I'd say that the boo...
2
2009-01-24T21:32:16Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
476,858
<p>I wrote a series of blogs on <a href="http://blog.cerris.com/category/django-tdd/">TDD in Django</a> that covers some TDD with the <a href="http://somethingaboutorange.com/mrl/projects/nose/">nose testing framework</a>.</p> <p>There are a lot of free online resources out there for learning about TDD:</p> <ul> <li>...
13
2009-01-24T23:08:13Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
2,856,092
<p>A very good unit test framework is also <a href="http://twistedmatrix.com/trac/wiki/TwistedTrial" rel="nofollow">trial</a> from the twisted project.</p>
0
2010-05-18T09:46:22Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
6,887,352
<p>A little late to the game with this one, but I have been hunting for a Python oriented TDD book, and I just found <a href="http://rads.stackoverflow.com/amzn/click/1847198848" rel="nofollow">Python Testing: Beginner's Guide by Daniel Arbuckle</a>. Haven't had a chance to read it yet, but when I do, I'll try to remem...
0
2011-07-31T03:22:50Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
10,648,767
<p>Can I plug my own tutorial, which covers the materials from the official Django tutorial, but uses full TDD all the way - including "proper" functional/acceptance tests using the Selenium browser-automation tool... <a href="http://tdd-django-tutorial.com" rel="nofollow">http://tdd-django-tutorial.com</a> </p> <p>[...
8
2012-05-18T07:50:46Z
[ "python", "tdd", "testing" ]
Resources for TDD aimed at Python Web Development
476,668
<p>I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or ai...
15
2009-01-24T21:19:07Z
12,446,364
<p><a href="http://blog.fruiapps.com/tag/test-driven-development" rel="nofollow">Here</a> is a great series of article written on test driven development in python. It starts from basic and goes to a point, where you are taught stuffs as designing for maintainability etc. I am sure you would like it.</p>
1
2012-09-16T11:07:36Z
[ "python", "tdd", "testing" ]
Get foreign key without requesting the whole object
476,731
<p>I have a model Foo which have a ForeignKey to the User model.</p> <p>Later, I need to grab all the User's id and put then on a list</p> <pre><code>foos = Foo.objects.filter(...) l = [ f.user.id for f in foos ] </code></pre> <p>But when I do that, django grabs the whole User instance from the DB instead of giving...
0
2009-01-24T22:00:43Z
476,805
<p>Nevermind... I´m not sure why this didn´t work before when I tried, but this is how to do:</p> <pre><code>l = [ f.user_id for f in foos ] </code></pre>
0
2009-01-24T22:41:20Z
[ "python", "django", "django-models" ]
Get foreign key without requesting the whole object
476,731
<p>I have a model Foo which have a ForeignKey to the User model.</p> <p>Later, I need to grab all the User's id and put then on a list</p> <pre><code>foos = Foo.objects.filter(...) l = [ f.user.id for f in foos ] </code></pre> <p>But when I do that, django grabs the whole User instance from the DB instead of giving...
0
2009-01-24T22:00:43Z
476,967
<p>Use <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields" rel="nofollow">queryset's values() function</a>, which will return a list of dictionaries containing name/value pairs for each attribute passed as parameters:</p> <pre><code>&gt;&gt;&gt; Foo.objects.all().values('user__id') [{'us...
5
2009-01-25T00:41:14Z
[ "python", "django", "django-models" ]
Get foreign key without requesting the whole object
476,731
<p>I have a model Foo which have a ForeignKey to the User model.</p> <p>Later, I need to grab all the User's id and put then on a list</p> <pre><code>foos = Foo.objects.filter(...) l = [ f.user.id for f in foos ] </code></pre> <p>But when I do that, django grabs the whole User instance from the DB instead of giving...
0
2009-01-24T22:00:43Z
486,544
<p>Whenever you define a ForeignKey in Django, it automatically adds a FIELD_id field to your model.</p> <p>For instance, if <code>Foo</code> has a FK to <code>User</code> with an attribute named "<code>user</code>", then you also have an attribute named <strong>user_id</strong> which contains the id of the related us...
3
2009-01-28T05:11:17Z
[ "python", "django", "django-models" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
476,787
<p>It is true you should not use '+'. Your example is quite special, try the same code with:</p> <pre><code>s1='*'*100000 s2='+'*100000 </code></pre> <p>Then the second version (str.join) is much faster.</p>
4
2009-01-24T22:32:25Z
[ "python", "performance", "string" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
476,788
<p>Most of the performance issues with string concatenation are ones of asymptotic performance, so the differences become most significant when you are concatenating many long strings. In your sample, you are performing the same concatenation many times. You aren't building up any long string, and it may be that the py...
12
2009-01-24T22:32:32Z
[ "python", "performance", "string" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
476,794
<blockquote> <p>Shouldn't it be exactly the other way round ?</p> </blockquote> <p>Not necessarily. I don't know the internals of Python well enough to comment specifically but some common observations are that your first loop uses a simple operator <code>+</code> which is probly implemented as a primitive by the ru...
5
2009-01-24T22:33:59Z
[ "python", "performance", "string" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
476,893
<p>The advice is about concatenating a lot of strings.</p> <p>To compute s = s1 + s2 + ... + sn,</p> <p>1) using +. A new string s1+s2 is created, then a new string s1+s2+s3 is created,..., etc, so a lot of memory allocation and copy operations is involved. In fact, s1 is copied n-1 times, s2 is copied n-2 time, ...,...
4
2009-01-24T23:43:59Z
[ "python", "performance", "string" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
476,921
<p>String concatenation (<code>+</code>) has an optimized implementation on CPython. But this may not be the case on other architectures like Jython or IronPython. So when you want your code to performe well on these interpreters you should use the <code>.join()</code> method on strings. <code>os.path.join()</code> is ...
1
2009-01-25T00:00:58Z
[ "python", "performance", "string" ]
python string join performance
476,772
<p>There are a lot of articles around the web concerning python performance, the first thing you read: concatenating strings should not be done using '+': avoid s1+s2+s3, instead use str.join</p> <p>I tried the following: concatenating two strings as part of a directory path: three approaches:</p> <ol> <li>'+' which ...
10
2009-01-24T22:26:13Z
848,867
<p>I would like to add a link to the python wiki, where there are notes regarding string concatenation and also that "<em>this section is somewhat wrong with python2.5. Python 2.5 string concatenation is fairly fast</em>".</p> <p>I believe that string concatenation had a big improvement since 2.5, and that although st...
0
2009-05-11T16:18:24Z
[ "python", "performance", "string" ]
Splitting a large XML file in Python
476,949
<p>I'm looking to split a huge XML file into smaller bits. I'd like to scan through the file looking for a specific tag, then grab all info between and , then save that into a file, then continue on through the rest of the file.</p> <p>My issue is trying to find a clean way to note the start and end of the tags, so t...
9
2009-01-25T00:22:01Z
476,958
<p>You might consider using the ElementTree <a href="https://docs.python.org/2/library/xml.etree.elementtree.html" rel="nofollow">iterparse</a> function for this situation.</p>
5
2009-01-25T00:32:07Z
[ "python", "xml" ]
Splitting a large XML file in Python
476,949
<p>I'm looking to split a huge XML file into smaller bits. I'd like to scan through the file looking for a specific tag, then grab all info between and , then save that into a file, then continue on through the rest of the file.</p> <p>My issue is trying to find a clean way to note the start and end of the tags, so t...
9
2009-01-25T00:22:01Z
476,982
<p>There are two common ways to handle XML data.</p> <p>One is called DOM, which stands for Document Object Model. This style of XML parsing is probably what you have seen when looking at documentation, because it reads the entire XML into memory to create the object model.</p> <p>The second is called SAX, which is ...
9
2009-01-25T00:49:08Z
[ "python", "xml" ]
Splitting a large XML file in Python
476,949
<p>I'm looking to split a huge XML file into smaller bits. I'd like to scan through the file looking for a specific tag, then grab all info between and , then save that into a file, then continue on through the rest of the file.</p> <p>My issue is trying to find a clean way to note the start and end of the tags, so t...
9
2009-01-25T00:22:01Z
477,046
<p>How serendipitous! Will Larson just made a good post about <a href="http://lethain.com/entry/2009/jan/22/handling-very-large-csv-and-xml-files-in-python/" rel="nofollow">Handling Very Large CSV and XML File in Python</a>.</p> <p>The main takeaways seem to be to use the <code>xml.sax</code> module, as Van mentioned...
1
2009-01-25T01:53:15Z
[ "python", "xml" ]
Splitting a large XML file in Python
476,949
<p>I'm looking to split a huge XML file into smaller bits. I'd like to scan through the file looking for a specific tag, then grab all info between and , then save that into a file, then continue on through the rest of the file.</p> <p>My issue is trying to find a clean way to note the start and end of the tags, so t...
9
2009-01-25T00:22:01Z
488,947
<p>I have had success with the cElementTree.iterparse method in order to do a similar task.</p> <p>I had a large xml doc with repeated 'entries' with tag 'resFrame' and I wanted to filter out entries for a specific id. Here is the code that I used for it:</p> <p>source document had this structure</p> <pre><code>&lt;...
6
2009-01-28T19:17:39Z
[ "python", "xml" ]
Splitting a large XML file in Python
476,949
<p>I'm looking to split a huge XML file into smaller bits. I'd like to scan through the file looking for a specific tag, then grab all info between and , then save that into a file, then continue on through the rest of the file.</p> <p>My issue is trying to find a clean way to note the start and end of the tags, so t...
9
2009-01-25T00:22:01Z
1,075,441
<p>This is an old, but very good article from Uche Ogbuji's also very good Python &amp; XMl column. It covers your exact question and uses the standard lib's sax module like the other answer has suggested. <a href="http://www.xml.com/pub/a/2004/07/28/py-xml.html" rel="nofollow">Decomposition, Process, Recomposition</a>...
0
2009-07-02T16:42:26Z
[ "python", "xml" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
476,977
<p>Take a look at <a href="http://www.jython.org/">Jython</a>. It's kind of like JNI, but replace C with Python, i.e. you can call Python from Java and vice versa. It's not totally clear what you're trying to do or why your current approach isn't what you want.</p>
8
2009-01-25T00:46:46Z
[ "java", "python", "jython" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
477,533
<p>Wrap your Java-Code in a Container (Servlet / EJB). </p> <p>So you don´t loose time in the vm-startup and you go the way to more service-oriented. </p> <p>For the wraping you can use jython (<em>only make sense if you are familiar with python</em>)</p> <p>Choose a communication-protocoll in which python an...
4
2009-01-25T10:50:46Z
[ "java", "python", "jython" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
511,109
<p>If you really want to embed your Java app within your Python process, have a look at <a href="http://jpype.sourceforge.net/">JPype</a>. It provides access to Java through JNI.</p>
5
2009-02-04T12:18:07Z
[ "java", "python", "jython" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
511,177
<p>How about using swig: <a href="http://www.swig.org/Doc1.3/Java.html" rel="nofollow">http://www.swig.org/Doc1.3/Java.html</a> ?</p>
3
2009-02-04T12:38:23Z
[ "java", "python", "jython" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
511,209
<p>Give JCC a try <a href="http://pypi.python.org/pypi/JCC/2.1" rel="nofollow">http://pypi.python.org/pypi/JCC/2.1</a></p> <p>JCC is a code generator for calling Java directly from CPython. It supports CPython 2.3+, several JREs (Sun JDK 1.4+, Apple JRE 1.4+, and OpenJDK 1.7) on OS X, Linux, Solaris, and Windows. It's...
2
2009-02-04T12:54:28Z
[ "java", "python", "jython" ]
Using a java library from python
476,968
<p>I have a python app and java app. The python app generates input for the java app and invokes it on the command line.</p> <p>I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.</p> <p>Any pointers? (FYI I'm v. new to Python) </p> <p><strong>Clarification</stron...
25
2009-01-25T00:41:22Z
3,793,496
<p>Sorry to ressurect the thread, but there was no accepted answer...</p> <p>You could also use <a href="http://py4j.sourceforge.net/index.html">Py4J</a>. There is an example on the frontpage and lots of documentation, but essentially, you just call Java methods from your python code as if they were python methods:</p...
37
2010-09-25T10:51:57Z
[ "java", "python", "jython" ]
How to read Unicode input and compare Unicode strings in Python?
477,061
<p>I work in Python and would like to read user input (from command line) in Unicode format, ie a Unicode equivalent of <code>raw_input</code>?</p> <p>Also, I would like to test Unicode strings for equality and it looks like a standard <code>==</code> does not work.</p> <p>Thank you for your help !</p>
26
2009-01-25T02:19:21Z
477,084
<p>It should work. <code>raw_input</code> returns a byte string which you must decode using the correct encoding to get your <code>unicode</code> object. For example, the following works for me under Python 2.5 / Terminal.app / OSX:</p> <pre><code>&gt;&gt;&gt; bytes = raw_input() 日本語 Ελληνικά &gt;&gt;&gt...
14
2009-01-25T02:38:34Z
[ "python", "unicode" ]
How to read Unicode input and compare Unicode strings in Python?
477,061
<p>I work in Python and would like to read user input (from command line) in Unicode format, ie a Unicode equivalent of <code>raw_input</code>?</p> <p>Also, I would like to test Unicode strings for equality and it looks like a standard <code>==</code> does not work.</p> <p>Thank you for your help !</p>
26
2009-01-25T02:19:21Z
477,087
<p>I'm not really sure, which format you mean by "Unicode format", there are several. UTF-8? UTF-16? In any case you should be able to read a normal string with <code>raw_input</code> and then decode it using the strings <code>decode</code> method:</p> <pre><code>raw = raw_input("Please input some funny characters: ")...
3
2009-01-25T02:42:49Z
[ "python", "unicode" ]
How to read Unicode input and compare Unicode strings in Python?
477,061
<p>I work in Python and would like to read user input (from command line) in Unicode format, ie a Unicode equivalent of <code>raw_input</code>?</p> <p>Also, I would like to test Unicode strings for equality and it looks like a standard <code>==</code> does not work.</p> <p>Thank you for your help !</p>
26
2009-01-25T02:19:21Z
477,104
<p>In the general case, it's probably not possible to compare unicode strings. The problem is that there are several ways to compose the same characters. A simple example is accented roman characters. Although there are codepoints for basically all of the commonly used accented characters, it is also correct to comp...
1
2009-01-25T03:20:14Z
[ "python", "unicode" ]
How to read Unicode input and compare Unicode strings in Python?
477,061
<p>I work in Python and would like to read user input (from command line) in Unicode format, ie a Unicode equivalent of <code>raw_input</code>?</p> <p>Also, I would like to test Unicode strings for equality and it looks like a standard <code>==</code> does not work.</p> <p>Thank you for your help !</p>
26
2009-01-25T02:19:21Z
477,496
<p><a href="https://docs.python.org/2/library/functions.html#raw_input" rel="nofollow" title="raw_input()"><code>raw_input()</code></a> returns strings as encoded by the OS or UI facilities. The difficulty is knowing which is that decoding. You might attempt the following:</p> <pre><code>import sys, locale text= raw_i...
48
2009-01-25T10:25:54Z
[ "python", "unicode" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,103
<p>A few problems with this approach:</p> <ul> <li>It's not immediately obvious when opening the file which modules it depends on.</li> <li>It will confuse programs that have to analyze dependencies, such as <code>py2exe</code>, <code>py2app</code> etc.</li> <li>What about modules that you use in many functions? You w...
18
2009-01-25T03:19:08Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,107
<p>This does have a few disadvantages.</p> <h1>Testing</h1> <p>On the off chance you want to test your module through runtime modification, it may make it more difficult. Instead of doing</p> <pre><code>import mymodule mymodule.othermodule = module_stub </code></pre> <p>You'll have to do</p> <pre><code>import othe...
48
2009-01-25T03:24:50Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,110
<p>From a performance point of view, you can see this: <a href="http://stackoverflow.com/questions/128478/should-python-import-statements-always-be-at-the-top-of-a-module">Should Python import statements always be at the top of a module? </a></p> <p>In general, I only use local imports in order to break dependency cyc...
2
2009-01-25T03:29:21Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,116
<p>Another useful thing to note is that the <code>from module import *</code> syntax inside of a function has been removed in Python 3.0.</p> <p>There is a brief mention of it under "Removed Syntax" here:</p> <p><a href="http://docs.python.org/3.0/whatsnew/3.0.html" rel="nofollow">http://docs.python.org/3.0/whatsnew/...
8
2009-01-25T03:32:28Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,128
<p>I believe this is a recommended approach in some cases/scenarios. For example in Google App Engine lazy-loading big modules is recommended since it will minimize the warm-up cost of instantiating new Python VMs/interpreters. Have a look at a <a href="https://sites.google.com/site/io/building-scalable-web-application...
2
2009-01-25T03:48:46Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,548
<p>You might want to take a look at Import <a href="http://wiki.python.org/moin/PythonSpeed/PerformanceTips#ImportStatementOverhead" rel="nofollow">statement overhead</a> in the python wiki. In short: if the module has already been loaded (look at <code>sys.modules</code>) your code will run slower. If your module hasn...
1
2009-01-25T11:03:19Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
477,873
<p>I would suggest that you try to avoid <code>from foo import bar</code> imports. I only use them inside packages, where the splitting into modules is an implementation detail and there won't be many of them anyway.</p> <p>In all other places, where you import a package, just use <code>import foo</code> and then refe...
3
2009-01-25T16:10:16Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
479,642
<p>People have explained very well why to avoid inline-imports, but not really alternative workflows to address the reasons you want them in the first place.</p> <blockquote> <p>I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth</p> </blockquote> <p>To...
3
2009-01-26T12:32:50Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
4,789,963
<p>The (previously) <a href="http://stackoverflow.com/questions/477096/python-import-coding-style/477107#477107">top-voted answer</a> to this question is nicely formatted but absolutely wrong about performance. Let me demonstrate</p> <h1>Performance</h1> <h2>Top Import</h2> <pre><code>import random def f(): L =...
63
2011-01-25T04:23:27Z
[ "python", "coding-style" ]
Python import coding style
477,096
<p>I've discovered a new pattern. Is this pattern well known or what is the opinion about it?</p> <p>Basically, I have a hard time scrubbing up and down source files to figure out what module imports are available and so forth, so now, instead of</p> <pre><code>import foo from bar.baz import quux def myFunction(): ...
46
2009-01-25T03:06:09Z
37,095,634
<h1>Security Implementations</h1> <p>Consider an environment where all of your Python code is located within a folder only a privileged user has access to. In order to avoid running your whole program as privileged user, you decide to drop privileges to an unprivileged user during execution. As soon as you make use of...
0
2016-05-08T02:35:17Z
[ "python", "coding-style" ]
How to list the files in a static directory?
477,135
<p>I am playing with Google App Engine and Python and I cannot list the files of a static directory. Below is the code I currently use.</p> <p><strong>app.yaml</strong></p> <pre><code>- url: /data static_dir: data </code></pre> <p><strong>Python code to list the files</strong></p> <pre><code>myFiles = [] for root...
8
2009-01-25T03:59:08Z
477,887
<p><a href="https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Static_file_handlers" rel="nofollow">https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Static_file_handlers</a></p> <p>They're not where you think they are, GAE puts static content into G...
7
2009-01-25T16:23:31Z
[ "python", "google-app-engine" ]
How to list the files in a static directory?
477,135
<p>I am playing with Google App Engine and Python and I cannot list the files of a static directory. Below is the code I currently use.</p> <p><strong>app.yaml</strong></p> <pre><code>- url: /data static_dir: data </code></pre> <p><strong>Python code to list the files</strong></p> <pre><code>myFiles = [] for root...
8
2009-01-25T03:59:08Z
477,956
<p>Here´s a project that let you browse your static files: <a href="http://code.google.com/p/appfilesbrowser/" rel="nofollow">http://code.google.com/p/appfilesbrowser/</a></p> <p>And here is must see list of recipes for appengine: <a href="http://appengine-cookbook.appspot.com/" rel="nofollow">http://appengine-cookbo...
2
2009-01-25T17:33:25Z
[ "python", "google-app-engine" ]
How to list the files in a static directory?
477,135
<p>I am playing with Google App Engine and Python and I cannot list the files of a static directory. Below is the code I currently use.</p> <p><strong>app.yaml</strong></p> <pre><code>- url: /data static_dir: data </code></pre> <p><strong>Python code to list the files</strong></p> <pre><code>myFiles = [] for root...
8
2009-01-25T03:59:08Z
477,972
<p>You can't access files uploaded as static content programmatically - they're not installed on the server along with your app, rather they're served up directly. If you really need to access them, you can remove the static file handler and serve them up yourself.</p>
0
2009-01-25T17:42:51Z
[ "python", "google-app-engine" ]
Generating and submitting a dynamic number of objects in a form with Django
477,183
<p>I want to be able to update a dynamic number of objects within a single form using Django and I'm wondering what the best way to do this would be. An example of a similar situation may help.</p> <p>Model:</p> <pre><code>class Customer(Model.models): name = models.CharField(max_length=100) active = models.B...
2
2009-01-25T05:02:30Z
477,194
<p><a href="http://docs.djangoproject.com/en/dev/topics/forms/formsets/" rel="nofollow">Formsets</a>!</p> <p>Also, the equivalent for forms generated directly models are <a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#id1" rel="nofollow">model formsets</a>.</p>
8
2009-01-25T05:17:59Z
[ "python", "html", "django", "forms", "web-applications" ]
How do I simulate flip of biased coin in python?
477,237
<p>In unbiased coin flip H or T occurs 50% of times.</p> <p>But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.</p> <p>something like this:</p> <pre><code>def flip(p): '''this function return H with probability p''' # do something return result &gt;&gt; [flip(0.8...
20
2009-01-25T06:13:00Z
477,239
<pre><code>import random def flip(p): return (random.random() &lt; p) </code></pre> <p>That returns a boolean which you can then use to choose H or T (or choose between any two values) you want. You could also include the choice in the method:</p> <pre><code>def flip(p): if random.random() &lt; p: ret...
4
2009-01-25T06:14:29Z
[ "python", "random", "coin-flipping" ]
How do I simulate flip of biased coin in python?
477,237
<p>In unbiased coin flip H or T occurs 50% of times.</p> <p>But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.</p> <p>something like this:</p> <pre><code>def flip(p): '''this function return H with probability p''' # do something return result &gt;&gt; [flip(0.8...
20
2009-01-25T06:13:00Z
477,246
<ul> <li><p>Import a random number between 0 - 1 (you can use randrange function)</p></li> <li><p>If the number is above (1-p), return tails.</p></li> <li><p>Else, return heads</p></li> </ul>
0
2009-01-25T06:17:46Z
[ "python", "random", "coin-flipping" ]
How do I simulate flip of biased coin in python?
477,237
<p>In unbiased coin flip H or T occurs 50% of times.</p> <p>But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.</p> <p>something like this:</p> <pre><code>def flip(p): '''this function return H with probability p''' # do something return result &gt;&gt; [flip(0.8...
20
2009-01-25T06:13:00Z
477,248
<p><code>random.random()</code> returns a <em>uniformly distributed</em> pseudo-random floating point number in the range [0, 1). This number is less than a given number <code>p</code> in the range [0,1) with probability <code>p</code>. Thus:</p> <pre><code>def flip(p): return 'H' if random.random() &lt; p else 'T...
37
2009-01-25T06:17:59Z
[ "python", "random", "coin-flipping" ]
How do I simulate flip of biased coin in python?
477,237
<p>In unbiased coin flip H or T occurs 50% of times.</p> <p>But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.</p> <p>something like this:</p> <pre><code>def flip(p): '''this function return H with probability p''' # do something return result &gt;&gt; [flip(0.8...
20
2009-01-25T06:13:00Z
478,513
<p>Do you want the "bias" to be based in symmetric distribuition? Or maybe exponential distribution? Gaussian anyone?</p> <p>Well, here are all the methods, extracted from random documentation itself.</p> <p>First, an example of triangular distribution:</p> <pre><code>print random.triangular(0, 1, 0.7) </code></pre>...
7
2009-01-25T23:24:57Z
[ "python", "random", "coin-flipping" ]
How do I simulate flip of biased coin in python?
477,237
<p>In unbiased coin flip H or T occurs 50% of times.</p> <p>But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.</p> <p>something like this:</p> <pre><code>def flip(p): '''this function return H with probability p''' # do something return result &gt;&gt; [flip(0.8...
20
2009-01-25T06:13:00Z
25,470,152
<p>How about:</p> <pre><code>import numpy as np n, p = 1, .33 # n = coins flipped, p = prob of success s = np.random.binomial(n, p, 100) </code></pre>
2
2014-08-24T09:09:20Z
[ "python", "random", "coin-flipping" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,506
<p>Increase the magnitude of <code>i</code> for the loop and then reduce it when you need it.</p> <pre><code>for i * 100 in range(0, 100, 10): print i / 100.0 </code></pre> <p><strong>EDIT: I honestly cannot remember why I thought that would work syntactically</strong></p> <pre><code>for i in range(0, 11, 1): ...
22
2009-01-25T10:32:01Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,507
<p>The range() built-in function returns a sequence of integer values, I'm afraid, so you can't use it to do a decimal step. </p> <p>I'd say just use a while loop:</p> <pre><code>i = 0.0 while i &lt;= 1.0: print i i += 0.1 </code></pre> <p>If you're curious, Python is converting your 0.1 to 0, which is why ...
7
2009-01-25T10:32:43Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,513
<p>Python's range() can only do integers, not floating point. In your specific case, you can use a list comprehension instead:</p> <pre><code>[x * 0.1 for x in range(0, 10)] </code></pre> <p>(Replace the call to range with that expression.) </p> <p>For the more general case, you may want to write a custom function o...
109
2009-01-25T10:35:25Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,527
<p>And if you do this often, you might want to save the generated list <code>r</code></p> <pre><code>r=map(lambda x: x/10.0,range(0,10)) for i in r: print i </code></pre>
3
2009-01-25T10:48:33Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,610
<p>Building on <a href="http://docs.python.org/library/functions.html#xrange">'xrange([start], stop[, step])'</a>, you can define a generator that accepts and produces any type you choose (stick to types supporting <code>+</code> and <code>&lt;</code>):</p> <pre><code>&gt;&gt;&gt; def drange(start, stop, step): ... ...
132
2009-01-25T11:57:17Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
477,635
<p>You can also use the <a href="http://en.wikipedia.org/wiki/NumPy">NumPy</a> library (which isn't part of standard library but is relatively easy to obtain) which has the <code>arange</code> function:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; np.arange(0,1,0.1) array([ 0. , 0.1, 0.2, 0.3, 0.4, ...
343
2009-01-25T12:26:08Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
3,120,257
<p>You can use this function:</p> <pre><code>def frange(start,end,step): return map(lambda x: x*step, range(int(start*1./step),int(end*1./step))) </code></pre>
2
2010-06-25T17:57:04Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
4,321,838
<p>Add auto-correction for the possibility of an incorrect sign on step:</p> <pre><code>def frange(start,step,stop): step *= 2*((stop&gt;start)^(step&lt;0))-1 return [start+i*step for i in range(int((stop-start)/step))] </code></pre>
1
2010-12-01T06:36:16Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
10,575,982
<pre><code>[x * 0.1 for x in range(0, 10)] </code></pre> <p>in Python 2.7x gives you the result of:</p> <blockquote> <p>[0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9]</p> </blockquote> <p>but if you use:</p> <pre><code>[ round(x * 0.1, 1) for x in range(0, 10)] <...
5
2012-05-13T23:20:58Z
[ "python", "floating-point", "range" ]
Python decimal range() step value
477,486
<p>Is there a way to step between 0 and 1 by 0.1? </p> <p>I thought I could do it like the following, but it failed:</p> <pre><code>for i in range(0, 1, 0.1): print i </code></pre> <p>Instead, it says that the step argument cannot be zero, which I did not expect.</p>
283
2009-01-25T10:20:43Z
10,986,098
<p>Similar to <a href="http://en.wikipedia.org/wiki/R_%28programming_language%29">R's</a> <code>seq</code> function, this one returns a sequence in any order given the correct step value. The last value is equal to the stop value. </p> <pre><code>def seq(start, stop, step=1): n = int(round((stop - start)/float(ste...
12
2012-06-11T19:10:53Z
[ "python", "floating-point", "range" ]