title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
Python urllib2 Response header | 1,653,591 | <p>I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns:</p>
<pre><code>Content-Type text/html
</code></pre>
<p>However when I use the python code:</p>
<pre><code>urllib2.urlopen(URL).info()
</code></pre>
<p>the resulting output r... | 20 | 2009-10-31T06:17:18Z | 1,654,360 | <p>Keep in mind that a web server can return different results for the same URL based on differences in the request. For example, content-type negotiation: the requestor can specify a list of content-types it will accept, and the server can return different results to try to accomodate different needs.</p>
<p>Also, y... | 1 | 2009-10-31T13:16:55Z | [
"python",
"url",
"header",
"response"
] |
Python urllib2 Response header | 1,653,591 | <p>I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns:</p>
<pre><code>Content-Type text/html
</code></pre>
<p>However when I use the python code:</p>
<pre><code>urllib2.urlopen(URL).info()
</code></pre>
<p>the resulting output r... | 20 | 2009-10-31T06:17:18Z | 2,523,793 | <p>Try to request as Firefox does. You can see the request headers in Firebug, so add them to your request object:</p>
<pre><code>import urllib2
request = urllib2.Request('http://your.tld/...')
request.add_header('User-Agent', 'some fake agent string')
request.add_header('Referer', 'fake referrer')
...
response = url... | 32 | 2010-03-26T14:04:00Z | [
"python",
"url",
"header",
"response"
] |
Python urllib2 Response header | 1,653,591 | <p>I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns:</p>
<pre><code>Content-Type text/html
</code></pre>
<p>However when I use the python code:</p>
<pre><code>urllib2.urlopen(URL).info()
</code></pre>
<p>the resulting output r... | 20 | 2009-10-31T06:17:18Z | 12,818,852 | <p>according to <a href="http://docs.python.org/library/urllib2.html" rel="nofollow">http://docs.python.org/library/urllib2.html</a> there is only <code>get_header()</code> method and nothing about <code>getheader</code> .</p>
<p>Asking because Your code works fine for </p>
<pre><code>response.info().getheader('Set... | 0 | 2012-10-10T12:02:55Z | [
"python",
"url",
"header",
"response"
] |
want to know how to get the syllables in telugu script | 1,653,614 | <p>i am working on telugu script whose unicode range is 0C01-0C7F
can u explain how to find out the syllables in that</p>
<p>(Update copying from <a href="http://stackoverflow.com/questions/1654680">that</a> question )</p>
<p>I am able to categorize chars but in telugu script one syllable may contain one or more char... | 1 | 2009-10-31T06:33:37Z | 1,653,641 | <p>I don't speak telugu (sorry!), but I do understand that it's a syllabic-based language where syllables are composed of vowels (âachchuâs) and consonants (âhalluâs). So, as <a href="http://en.wikipedia.org/wiki/Telugu%5Flanguage" rel="nofollow">wikipedia</a> explains, you end up with "sixty symbols, of which ... | 2 | 2009-10-31T06:54:52Z | [
"python"
] |
want to know how to get the syllables in telugu script | 1,653,614 | <p>i am working on telugu script whose unicode range is 0C01-0C7F
can u explain how to find out the syllables in that</p>
<p>(Update copying from <a href="http://stackoverflow.com/questions/1654680">that</a> question )</p>
<p>I am able to categorize chars but in telugu script one syllable may contain one or more char... | 1 | 2009-10-31T06:33:37Z | 12,946,891 | <p>In Telugu, the syllables are formed in several ways </p>
<ul>
<li>V (at the beginning of the word in the independent form)</li>
<li>C + V (consonant + vowel in the dependent form)</li>
<li>C + C + V (consonant + consonant + vowel)</li>
<li>C + C + C + V consonant + consonant + vowel)</li>
</ul>
<p>If you are usin... | 2 | 2012-10-18T03:59:51Z | [
"python"
] |
Python deprecated functions | 1,653,734 | <p>I have a Django app written in Python 2.5 and I plan to upgrade it to be compatible with Python 2.6. It contains hundreds of .py files. Is there a simple way to find all deprecated functions in those files?</p>
| 3 | 2009-10-31T07:55:37Z | 1,653,756 | <p>I think Python 2.5 apps should work fine in 2.6 without updates. 2.6 might complain about some deprecated functionality but those are only removed in 3.0 and still work in 2.6.</p>
| 0 | 2009-10-31T08:17:23Z | [
"python",
"python-2.6"
] |
Python deprecated functions | 1,653,734 | <p>I have a Django app written in Python 2.5 and I plan to upgrade it to be compatible with Python 2.6. It contains hundreds of .py files. Is there a simple way to find all deprecated functions in those files?</p>
| 3 | 2009-10-31T07:55:37Z | 1,653,810 | <p>Between point releasses of Python (like between 2.5 and 2.6) anything that might break is a warning for at least one release. Most deprecation warnings are emitted by default but you have fine grained control over the emitted warnings with the -W interpreter invocation option.</p>
<p>This is a relatively small issu... | 5 | 2009-10-31T08:49:11Z | [
"python",
"python-2.6"
] |
Python deprecated functions | 1,653,734 | <p>I have a Django app written in Python 2.5 and I plan to upgrade it to be compatible with Python 2.6. It contains hundreds of .py files. Is there a simple way to find all deprecated functions in those files?</p>
| 3 | 2009-10-31T07:55:37Z | 1,654,311 | <p>I'm not sure what you mean by "deprecated functions" when upgrading from Python 2.5 to Python 2.6. </p>
<p>The release notes are very specific. <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">http://docs.python.org/whatsnew/2.6.html</a></p>
<p>There are no deprecated functions. A few whole mo... | 4 | 2009-10-31T12:56:43Z | [
"python",
"python-2.6"
] |
Python deprecated functions | 1,653,734 | <p>I have a Django app written in Python 2.5 and I plan to upgrade it to be compatible with Python 2.6. It contains hundreds of .py files. Is there a simple way to find all deprecated functions in those files?</p>
| 3 | 2009-10-31T07:55:37Z | 1,654,811 | <p>The real deal is to migrate from 2.6 to 3.0. If you want to know all about it, there is a very good speech for that :</p>
<p><a href="http://blip.tv/file/1949281" rel="nofollow">http://blip.tv/file/1949281</a></p>
<p>Includes :</p>
<ul>
<li>incompatibilities;</li>
<li>work around;</li>
<li>to-dos;</li>
<li>automa... | 0 | 2009-10-31T16:09:05Z | [
"python",
"python-2.6"
] |
Python deprecated functions | 1,653,734 | <p>I have a Django app written in Python 2.5 and I plan to upgrade it to be compatible with Python 2.6. It contains hundreds of .py files. Is there a simple way to find all deprecated functions in those files?</p>
| 3 | 2009-10-31T07:55:37Z | 8,189,947 | <p>More and more I am persuaded that the right answer is "Just run your testsuite to find out." You have your testsuite covering your program reasonably, right? If not, this is a great opportunity to create one (you can persuade your point-headed boss that it is "migration" ;)).</p>
| 0 | 2011-11-18T22:27:35Z | [
"python",
"python-2.6"
] |
How to prepopulate ID in Django | 1,653,828 | <p>I have simple question model :</p>
<pre><code>class Question(Polymorph):
text = models.CharField(max_length=256)
poll = models.ForeignKey(Poll)
index = models.IntegerField()
</code></pre>
<p>And I would like to prepopulate ( when saving ) index field with ID value. Of course before save I dont have ID ... | 0 | 2009-10-31T09:00:03Z | 1,653,859 | <p>However you do it, you'll have to call save twice. The ID is generated directly by the database server (except for sqlite, I believe) when the new row is INSERTed, so you'll need to do that in any case.</p>
<p>I would ask if you really need to have the ID value in your <code>index</code> field, though. It's always ... | 5 | 2009-10-31T09:17:09Z | [
"python",
"django",
"django-models"
] |
How to prepopulate ID in Django | 1,653,828 | <p>I have simple question model :</p>
<pre><code>class Question(Polymorph):
text = models.CharField(max_length=256)
poll = models.ForeignKey(Poll)
index = models.IntegerField()
</code></pre>
<p>And I would like to prepopulate ( when saving ) index field with ID value. Of course before save I dont have ID ... | 0 | 2009-10-31T09:00:03Z | 1,669,180 | <p>Yes, trying to figure the next ID out from somewhere wouldn't be portable. Even simpler than using signals is putting something like this in your save method:</p>
<pre><code>def save(self, *args, **kwargs):
"""Save method for Question model"""
if not self.id:
super(Question, self).save(*args, **kwar... | 0 | 2009-11-03T18:17:18Z | [
"python",
"django",
"django-models"
] |
How to prepopulate ID in Django | 1,653,828 | <p>I have simple question model :</p>
<pre><code>class Question(Polymorph):
text = models.CharField(max_length=256)
poll = models.ForeignKey(Poll)
index = models.IntegerField()
</code></pre>
<p>And I would like to prepopulate ( when saving ) index field with ID value. Of course before save I dont have ID ... | 0 | 2009-10-31T09:00:03Z | 1,671,071 | <p>One way to circumvent the double commit is to use something besides an autoincrementing primary key. Django doesn't really care what the primary key is so long as there is one, just one, and it is either a string or an integer. You could use something like a guid, which you can generate on the fly in your models a... | 0 | 2009-11-04T00:42:35Z | [
"python",
"django",
"django-models"
] |
If pickling was interrupted, will unpickling necessarily always fail? - Python | 1,653,897 | <p>Suppose my attempt to write a pickle object out to disk is incomplete due to a crash. Will an attempt to unpickle the object <strong>always</strong> lead to an exception or is it possible that the fragment that was written out may be interpreted as valid pickle and the error go unnoticed?</p>
| 4 | 2009-10-31T09:38:19Z | 1,654,321 | <p>I doubt you could make a claim that it will <em>always</em> lead to an exception. Pickles are actually programs written in a specialized stack language. The internal details of pickles change from version to version, and new pickle protocols are added occasionally. The state of the pickle after a crash, and the r... | 1 | 2009-10-31T12:59:44Z | [
"python"
] |
If pickling was interrupted, will unpickling necessarily always fail? - Python | 1,653,897 | <p>Suppose my attempt to write a pickle object out to disk is incomplete due to a crash. Will an attempt to unpickle the object <strong>always</strong> lead to an exception or is it possible that the fragment that was written out may be interpreted as valid pickle and the error go unnoticed?</p>
| 4 | 2009-10-31T09:38:19Z | 1,654,329 | <p>Pickling an object returns an str object, or writes an str object to a file ... it doesn't modify the original object. If a "crash" (exception) happens inside a pickling call, the result won't be returned to the caller, so you don't have anything that you could try to unpickle. Besides, why would you want to unpickl... | 2 | 2009-10-31T13:02:43Z | [
"python"
] |
If pickling was interrupted, will unpickling necessarily always fail? - Python | 1,653,897 | <p>Suppose my attempt to write a pickle object out to disk is incomplete due to a crash. Will an attempt to unpickle the object <strong>always</strong> lead to an exception or is it possible that the fragment that was written out may be interpreted as valid pickle and the error go unnoticed?</p>
| 4 | 2009-10-31T09:38:19Z | 1,654,390 | <p>Contra the other answers offered, I believe that we <em>can</em> make a strong argument about the recoverability of a pickle. That answer is: "Yes, an incomplete pickle <em>always</em> leads to an exception."</p>
<p>Why are we able to do this? Because the "pickle" format is in fact a small stack-based language. In ... | 7 | 2009-10-31T13:27:39Z | [
"python"
] |
If pickling was interrupted, will unpickling necessarily always fail? - Python | 1,653,897 | <p>Suppose my attempt to write a pickle object out to disk is incomplete due to a crash. Will an attempt to unpickle the object <strong>always</strong> lead to an exception or is it possible that the fragment that was written out may be interpreted as valid pickle and the error go unnoticed?</p>
| 4 | 2009-10-31T09:38:19Z | 1,654,503 | <p>To be sure that you have a "complete" pickle file, you need to pickle three things.</p>
<ol>
<li><p>Pickle a header of some kind that claims how many objects and what the end-of-file flag will look like. A tuple of an integer and the EOF string, for example.</p></li>
<li><p>Pickle the objects you actually care abo... | 1 | 2009-10-31T14:18:07Z | [
"python"
] |
If pickling was interrupted, will unpickling necessarily always fail? - Python | 1,653,897 | <p>Suppose my attempt to write a pickle object out to disk is incomplete due to a crash. Will an attempt to unpickle the object <strong>always</strong> lead to an exception or is it possible that the fragment that was written out may be interpreted as valid pickle and the error go unnoticed?</p>
| 4 | 2009-10-31T09:38:19Z | 1,654,863 | <p>This is a development of S. Lott's answer, with my suggestion: Append a hash or checksum to your data, that you check before unpickling again.</p>
<p>Here is a (simple) implementation of safepickle/safeunpickle to show how you can pad the pickled data with a hash (cryptographically strong hash in this case):</p>
<... | 2 | 2009-10-31T16:31:56Z | [
"python"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 1,653,974 | <p>There is an <a href="http://code.activestate.com/recipes/576694/" rel="nofollow">ordered set</a> recipe for this which is referred to from the <a href="https://docs.python.org/2/library/collections.html" rel="nofollow">Python 2 Documentation</a>. This runs on Py2.6 or later and 3.0 or later without any modifications... | 133 | 2009-10-31T10:15:06Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 1,653,978 | <h2>An ordered set is functionally a special case of an ordered dictionary.</h2>
<p>The keys of a dictionary are unique. Thus, if one disregards the values in an ordered dictionary (e.g. by assigning them <code>None</code>), then one has essentially an ordered set.</p>
<p><a href="http://docs.python.org/3.1/whatsnew/... | 84 | 2009-10-31T10:17:39Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 10,178,672 | <p>There are four kinds of ordering one might want, I believe:</p>
<ol>
<li>Ordered by key</li>
<li>Ordered by value (I've not heard of anyone ask for this one though)</li>
<li>Ordered by modification time</li>
<li>Ordered by addition time</li>
</ol>
<p>I believe collections.OrderedDict gets you #4. Or you could rem... | -4 | 2012-04-16T17:20:47Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 14,991,471 | <p>For many purposes simply calling sorted will suffice. For example</p>
<pre><code>>>> s = set([0, 1, 2, 99, 4, 40, 3, 20, 24, 100, 60])
>>> sorted(s)
[0, 1, 2, 3, 4, 20, 24, 40, 60, 99, 100]
</code></pre>
<p>If you are going to use this repeatedly, there will be overhead incurred by calling the s... | -3 | 2013-02-20T22:52:44Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 22,766,863 | <pre><code>>>> a = {3, 4, 2, 6, 1, 7}
>>> type(a)
<class 'set'>
>>> sorted(a, reverse=True)
[7, 6, 4, 3, 2, 1]
>>> sorted(a)
[1, 2, 3, 4, 6, 7]
</code></pre>
| -6 | 2014-03-31T16:20:29Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 23,225,031 | <h1>Implementations on PyPI</h1>
<p>While others have pointed out that there is no built-in implementation of an insertion-order preserving set in Python (yet), I am feeling that this question is missing an answer which states what there is to be found on <a href="https://pypi.python.org" rel="nofollow">PyPI</a>.</p>
... | 20 | 2014-04-22T16:22:53Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 25,988,702 | <p>If you're using the ordered set to maintain a sorted order, consider using a sorted set implementation from PyPI. The <a href="http://www.grantjenks.com/docs/sortedcontainers/">sortedcontainers</a> module provides a <a href="http://www.grantjenks.com/docs/sortedcontainers/sortedset.html">SortedSet</a> for just this ... | 13 | 2014-09-23T06:52:26Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 26,977,777 | <p>You can use <code>reduce()</code> to get a list of unique values in one line:</p>
<pre><code>>>> mylist = [4, 1, 2, 1, 3, 2, 4, 1, 3, 2, 3, 1, 3, 2, 4]
>>> reduce(lambda a, b: b[0] in a and a or a + b, [[i] for i in mylist])
[4, 1, 2, 3]
</code></pre>
| -6 | 2014-11-17T16:56:12Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 28,052,907 | <p>A little late to the game, but I've written a class <code>setlist</code> as part of <code>collections-extended</code> that fully implements both <code>Sequence</code> and <code>Set</code></p>
<pre><code>>>> from collections_extended import setlist
>>> sl = setlist('abracadabra')
>>> sl
se... | 3 | 2015-01-20T18:46:11Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 32,784,166 | <p>In case you're already using pandas in your code, its <code>Index</code> object behaves pretty like an ordered set, as shown in <a href="https://www.oreilly.com/learning/introducing-pandas-objects" rel="nofollow">this article</a>.</p>
| 2 | 2015-09-25T14:13:08Z | [
"python",
"set"
] |
Does Python have an ordered set? | 1,653,970 | <p>Python has an <a href="http://www.python.org/dev/peps/pep-0372/">ordered dictionary</a>, what about an ordered set?</p>
| 228 | 2009-10-31T10:12:07Z | 35,258,869 | <p>I can do you one better than an OrderedSet: boltons has <a href="http://boltons.readthedocs.org/en/latest/setutils.html">a pure-Python, 2/3-compatible <code>IndexedSet</code> type</a> that is not only an ordered set, but also supports indexing (as with lists).</p>
<p>Simply <code>pip install boltons</code> (or copy... | 6 | 2016-02-07T20:41:45Z | [
"python",
"set"
] |
Trapping signals in Python | 1,654,215 | <p>According to the <a href="http://docs.python.org/library/signal.html" rel="nofollow">documentation</a>:</p>
<blockquote>
<p>There is no way to âblockâ signals temporarily from critical sections (since this is not supported by all Unix flavors).</p>
</blockquote>
<p>What stops me using <code>signal.signal(sig... | 3 | 2009-10-31T12:15:49Z | 1,654,279 | <p>What stops you is that, if the signal actually arrives while SIG_IGN is in place, then it will be ignored and thrown away. When you add the signal back later, it's too late because it's gone and you'll never get to learn that it happened.</p>
<p>Thus, you will have "ignored" (= thrown away) the signal rather than "... | 11 | 2009-10-31T12:45:09Z | [
"python",
"signals"
] |
Testing twisted application - Load client | 1,654,566 | <p>I've written a Twisted based server and I'd like to test it using twisted as well.</p>
<p>But I'd like to write a load test starting a bunch of request at the same time.</p>
<p>But I believe that I didn't get the concepts of Twisted, mainly client side, because I'm stucked with this problem:</p>
<pre><code> fr... | 3 | 2009-10-31T14:37:05Z | 1,657,092 | <p>The direct cause for your failure is that you attemp to call run() on the reactor multiple times. You are supposed to ever only call run() once. I think you are expecting to have multiple reactors, each in its own thread, but actually you only have one. The bad thing is that having multiple reactors is difficult or ... | 8 | 2009-11-01T12:47:18Z | [
"python",
"multithreading",
"twisted"
] |
Run command pipes with subprocess.Popen | 1,654,600 | <p>How can I run the following command using <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen" rel="nofollow"><code>subprocess.Popen</code></a>?</p>
<pre><code>mysqldump database_name table_name | bzip2 > filename
</code></pre>
<p>I know <a href="http://docs.python.org/library/os.html#os.sy... | 4 | 2009-10-31T14:48:45Z | 1,654,624 | <p>You want the <code>shell=True</code> option to make it execute shell commands:</p>
<pre><code>import subprocess
subprocess.Popen("sleep 4s && echo right thar, right thar",shell=True);
print 'i like it when you put it'
</code></pre>
<p>which yields:</p>
<pre><code> I like it when you put it
[4 seconds lat... | 6 | 2009-10-31T14:58:06Z | [
"python",
"command",
"pipe"
] |
Python Syntax Highlighting in Microsoft Word | 1,654,635 | <p>Is there any way I can get my Python code syntax highlighted in Microsoft Word?</p>
| 4 | 2009-10-31T15:01:11Z | 1,654,656 | <p>Use a <a href="https://gist.github.com/" rel="nofollow">pastebin</a> that does syntax highlighting, for example:</p>
<p><a href="https://gist.github.com/anonymous/82cb7f691673e0147edf" rel="nofollow">https://gist.github.com/anonymous/82cb7f691673e0147edf</a></p>
<p>Then copy and paste the syntax highlighted code i... | 10 | 2009-10-31T15:07:57Z | [
"python",
"ms-word"
] |
Avoid C style comments while reading a file | 1,654,649 | <p>I am parsing a C file for LOC in a function using python.
I am starting from first line of function definition and skipping all lines till i met first "{".</p>
<p>The issue is that "{" can also come as a part of comment. I just want to skip all "{" present inside comments.</p>
<p>e.g</p>
<pre><code>100: int func(... | 1 | 2009-10-31T15:05:42Z | 1,654,662 | <p>You're going to find that getting this right is very difficult without a real lexer and parser.</p>
<p>This will find the opening brace you're looking for:</p>
<pre><code>f = open("myfile.c")
for l in f.readlines():
l = l.split('//')[0]
if '{' in l:
break
</code></pre>
<p>But for example, you coul... | 3 | 2009-10-31T15:09:48Z | [
"python"
] |
Avoid C style comments while reading a file | 1,654,649 | <p>I am parsing a C file for LOC in a function using python.
I am starting from first line of function definition and skipping all lines till i met first "{".</p>
<p>The issue is that "{" can also come as a part of comment. I just want to skip all "{" present inside comments.</p>
<p>e.g</p>
<pre><code>100: int func(... | 1 | 2009-10-31T15:05:42Z | 1,654,696 | <p>If you have gcc, you can use <code>gcc -E input_file</code> as a preprocessor which will strip-off comments (but also expand macros - might change LOC). For your example the output would be:</p>
<pre>
# 1 "_.c"
# 1 ""
# 1 ""
# 1 "_.c"
int func(
int i,
float f,
char c
)
{
</pr... | 0 | 2009-10-31T15:22:35Z | [
"python"
] |
Avoid C style comments while reading a file | 1,654,649 | <p>I am parsing a C file for LOC in a function using python.
I am starting from first line of function definition and skipping all lines till i met first "{".</p>
<p>The issue is that "{" can also come as a part of comment. I just want to skip all "{" present inside comments.</p>
<p>e.g</p>
<pre><code>100: int func(... | 1 | 2009-10-31T15:05:42Z | 1,656,009 | <p>Here is a comment stripper that should also comprehend comment introducers within quoted strings:</p>
<pre><code>from pyparsing import cppStyleComment,dblQuotedString
cppStyleComment.ignore(dblQuotedString)
src = cppStyleComment.suppress().transformString(src)
print src
</code></pre>
<p>With your original snippe... | 6 | 2009-11-01T00:18:13Z | [
"python"
] |
Find a path in Windows relative to another | 1,654,659 | <p>This problem should be a no-brainer, but I haven't yet been able to nail it.</p>
<p>I need a function that takes two parameters, each a file path, relative or absolute, and returns a filepath which is the first path (target) resolved relative to the second path (start). The resolved path may be relative to the curr... | 2 | 2009-10-31T15:09:04Z | 1,654,764 | <p>I agree with you: this seems like a deficiency in os.path.join. Looks like you have to deal with the drives separately. This code passes all your tests:</p>
<pre><code>def findpath(target, start=os.path.curdir):
sdrive, start = os.path.splitdrive(start)
tdrive, target = os.path.splitdrive(target)
rdri... | 2 | 2009-10-31T15:50:39Z | [
"python",
"windows",
"filesystems",
"relative-path"
] |
Python MultiThreading With Urllib2 Issue | 1,654,721 | <p>I can download multiple files quite fast with many threads at once but the problem is that after a few minutes it tends to slow down gradually to almost a full stop, I have no idea why. There's nothing wrong with my code that I can see and my RAM/CPU is fine.. The only thing I can think of is that urllib2 isn't hand... | 3 | 2009-10-31T15:31:36Z | 1,654,854 | <p>Can you confirm that doing the same number of simultaneous downloads without python continues to download fast? Perhaps the issue is not with your code, but with your connection getting throttled or with the site serving the files.</p>
<p>If that's not the issue you could try the <a href="http://pyprocessing.berlio... | 3 | 2009-10-31T16:27:51Z | [
"python",
"multithreading",
"sockets",
"urllib"
] |
Python MultiThreading With Urllib2 Issue | 1,654,721 | <p>I can download multiple files quite fast with many threads at once but the problem is that after a few minutes it tends to slow down gradually to almost a full stop, I have no idea why. There's nothing wrong with my code that I can see and my RAM/CPU is fine.. The only thing I can think of is that urllib2 isn't hand... | 3 | 2009-10-31T15:31:36Z | 1,655,064 | <p>Like another answer suggested, the problem might be with your connection or the site that is serving the files. If you can run your code against a test server locally then you will be able to eliminate this.</p>
<p>If the problem goes away when using the test server then the problem lies with your connection or the... | 1 | 2009-10-31T17:43:31Z | [
"python",
"multithreading",
"sockets",
"urllib"
] |
erlang on google app engine? | 1,654,759 | <p>I know python can be run on GAE</p>
<ol>
<li>what is different erlang and python in lay man term?</li>
<li>can erlang run on google app engine ?</li>
</ol>
| 2 | 2009-10-31T15:48:47Z | 1,654,786 | <p>Erlang and Python are programming languages, and each language has one or more "runtimes" that allow you to run programs written in those languages. GAE supplies a Python runtime.</p>
<p>GAE has no support for Erlang programs.</p>
| 11 | 2009-10-31T15:57:51Z | [
"python",
"google-app-engine",
"erlang"
] |
Run an OS X universal binary in 32-bit mode | 1,654,837 | <p>I have a third-party library (the interface to Xerox's Finite State tools) which come as universal binaries with two variants internally: a PPC and an i386 variant. I also have a Python interface to the library (which uses ctypes).</p>
<p>But when I try to run the example code provided with the Python interface I g... | 7 | 2009-10-31T16:18:26Z | 1,654,855 | <p>If you are using the apple system python (on snow leopard) you can execute it with</p>
<pre><code>arch -i386 python
</code></pre>
<p>eg:</p>
<pre><code>robin-mbp:~ $ arch -i386 /usr/bin/python2.6 -c "import sys; print sys.maxint"
2147483647
</code></pre>
<p>to start the interpreter in 32 bit mode. There is also ... | 16 | 2009-10-31T16:28:24Z | [
"python",
"osx",
"universal-binary"
] |
Run an OS X universal binary in 32-bit mode | 1,654,837 | <p>I have a third-party library (the interface to Xerox's Finite State tools) which come as universal binaries with two variants internally: a PPC and an i386 variant. I also have a Python interface to the library (which uses ctypes).</p>
<p>But when I try to run the example code provided with the Python interface I g... | 7 | 2009-10-31T16:18:26Z | 7,773,058 | <p>If you are not using the system python, you can go ahead and add a line to your <code>~/.bash_profile</code>:</p>
<pre><code>export VERSIONER_PYTHON_PREFER_32_BIT=yes
</code></pre>
| 3 | 2011-10-14T20:13:11Z | [
"python",
"osx",
"universal-binary"
] |
Nested transactions with SQLAlchemy and sqlite | 1,654,857 | <p>I'm writing an application in Python using SQLAlchemy (and Elixir) with SQLite as the database backend. I start a new transaction using the code <code>session.begin_transaction()</code>, but when I call <code>session.rollback()</code> I get the following error:</p>
<pre><code>sqlalchemy.exceptions.OperationalError:... | 3 | 2009-10-31T16:28:40Z | 1,654,940 | <p>SQLAlchemy uses pysqlite to interact with an SQLite database, if I'm not mistaken pysqlite will by default wrap up any query you send it in a transaction.</p>
<p>The answer might lie in correctly setting the isolation level when you connect.</p>
<p><a href="http://groups.google.com/group/sqlalchemy-devel/browse%5F... | 0 | 2009-10-31T17:01:33Z | [
"python",
"sqlite",
"sqlalchemy",
"python-elixir"
] |
Nested transactions with SQLAlchemy and sqlite | 1,654,857 | <p>I'm writing an application in Python using SQLAlchemy (and Elixir) with SQLite as the database backend. I start a new transaction using the code <code>session.begin_transaction()</code>, but when I call <code>session.rollback()</code> I get the following error:</p>
<pre><code>sqlalchemy.exceptions.OperationalError:... | 3 | 2009-10-31T16:28:40Z | 1,867,245 | <p>Although sqlite does appear to support nested transactions via SAVEPOINT, it's only as of <a href="http://www.sqlite.org/changes.html" rel="nofollow">version 3.6.8, released 2009 Jan 12</a>. Python, at least up to v2.6, uses earlier versions:</p>
<pre><code>c:\svn\core\apps\general>python
Python 2.6.2 (r262:716... | 2 | 2009-12-08T14:17:27Z | [
"python",
"sqlite",
"sqlalchemy",
"python-elixir"
] |
Nested transactions with SQLAlchemy and sqlite | 1,654,857 | <p>I'm writing an application in Python using SQLAlchemy (and Elixir) with SQLite as the database backend. I start a new transaction using the code <code>session.begin_transaction()</code>, but when I call <code>session.rollback()</code> I get the following error:</p>
<pre><code>sqlalchemy.exceptions.OperationalError:... | 3 | 2009-10-31T16:28:40Z | 36,457,404 | <p>I've run into this issue using nested transactions, using Python 3 on Windows. I'm using SQLite version 3.8.11, so <code>SAVEPOINT</code> should be supported. Apparently installing pysqlite isn't an option for me as it doesn't support Python 3.</p>
<p>After hours of banging my head against the desk I came across th... | 1 | 2016-04-06T16:48:08Z | [
"python",
"sqlite",
"sqlalchemy",
"python-elixir"
] |
Ouput file redirection in Python | 1,654,875 | <p>I'm writing a backup script I intend to execute in a cronjob every night. </p>
<p>The script sets sys.stdout and sys.stderr to an output file to keep a log of what happens.</p>
<p>To do the backup I use the following code</p>
<pre><code>cmd = 'rsync -av --del --stats --filter "- .thumbnails/" ' + \
'--filter ... | 6 | 2009-10-31T16:34:59Z | 1,654,895 | <p>Have you tried putting the flush calls outside the try block?</p>
| 0 | 2009-10-31T16:42:00Z | [
"python",
"file"
] |
Ouput file redirection in Python | 1,654,875 | <p>I'm writing a backup script I intend to execute in a cronjob every night. </p>
<p>The script sets sys.stdout and sys.stderr to an output file to keep a log of what happens.</p>
<p>To do the backup I use the following code</p>
<pre><code>cmd = 'rsync -av --del --stats --filter "- .thumbnails/" ' + \
'--filter ... | 6 | 2009-10-31T16:34:59Z | 1,654,897 | <p>Why are you printing to stderr? If the subprocess is writing to stdout while you are writing to stderr, that could explain the odd interleaving.</p>
| 0 | 2009-10-31T16:44:15Z | [
"python",
"file"
] |
Ouput file redirection in Python | 1,654,875 | <p>I'm writing a backup script I intend to execute in a cronjob every night. </p>
<p>The script sets sys.stdout and sys.stderr to an output file to keep a log of what happens.</p>
<p>To do the backup I use the following code</p>
<pre><code>cmd = 'rsync -av --del --stats --filter "- .thumbnails/" ' + \
'--filter ... | 6 | 2009-10-31T16:34:59Z | 1,655,213 | <p>I just found the solution <a href="http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print">here</a> in a Stackoverflow answer.</p>
<p>Replace </p>
<pre><code>sys.stderr = sys.stdout = logFile = open( tmpLogFileName, 'a' )
</code></pre>
<p>with </p>
<pre><code>sys.stderr = sys.stdout = logF... | 3 | 2009-10-31T18:37:59Z | [
"python",
"file"
] |
Python: Monitoring and killing/throttling spawned processes based on load, time, etc | 1,654,922 | <p>I have a queue of workers that spawn external third party apps using subprocess. I'd like to control how much of the overall resources of my server these process consume. Some of these external apps also tend to hang for unknown reasons, fixed with a restart.</p>
<p>What's a good way to:</p>
<ul>
<li>Monitor the o... | 4 | 2009-10-31T16:53:48Z | 1,655,617 | <p>As for governing CPU, you'll want to use <a href="http://www.gnu.org/software/coreutils/manual/html%5Fnode/nice-invocation.html" rel="nofollow">nice</a> to launch your processes.</p>
<p>For monitoring system load and other stats related to currently running processes, you might look into the <a href="http://www.lin... | 1 | 2009-10-31T20:57:05Z | [
"python"
] |
Python: Monitoring and killing/throttling spawned processes based on load, time, etc | 1,654,922 | <p>I have a queue of workers that spawn external third party apps using subprocess. I'd like to control how much of the overall resources of my server these process consume. Some of these external apps also tend to hang for unknown reasons, fixed with a restart.</p>
<p>What's a good way to:</p>
<ul>
<li>Monitor the o... | 4 | 2009-10-31T16:53:48Z | 1,659,483 | <p>Functions to get load average and kill process are available in standard python library (<code>os.getloadavg()</code>, <code>os.kill()</code>, <code>subprocess.Popen.kill()</code>). There is a <a href="http://code.google.com/p/psutil/" rel="nofollow"><code>psutil</code></a> package for the rest (<code>psutil.Process... | 4 | 2009-11-02T05:06:52Z | [
"python"
] |
Python: Monitoring and killing/throttling spawned processes based on load, time, etc | 1,654,922 | <p>I have a queue of workers that spawn external third party apps using subprocess. I'd like to control how much of the overall resources of my server these process consume. Some of these external apps also tend to hang for unknown reasons, fixed with a restart.</p>
<p>What's a good way to:</p>
<ul>
<li>Monitor the o... | 4 | 2009-10-31T16:53:48Z | 2,246,627 | <p>Monitor the overall server load (say, load average or equivalent of vmstat) in python?</p>
<pre>
>>> import psutil, subprocess
>>> subp = subprocess.Popen('python', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> proc = psutil.Process(subp.pid)
>>> rss, vms = proc.get_memory_info()
>>> print "Resident memory: ... | 1 | 2010-02-11T18:21:33Z | [
"python"
] |
List implemented using an inorder binary tree | 1,654,947 | <p>For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. </p>
<p>The idea is having a binary tree that has its nodes accessible via indexes, e.g. </p>
<pre><code>t = ListTree()
t.insert(2,0) # 1st argument is the v... | 2 | 2009-10-31T17:03:59Z | 1,654,970 | <p>You really wouldn't want to store it on the node itself, because then the index would have to be updated on inserts for all nodes with index less than insert index. I think the real question is how to do an in-order traversal. Try having your recursive function return the number of nodes to its left. </p>
| 1 | 2009-10-31T17:12:12Z | [
"python",
"algorithm",
"binary-tree"
] |
List implemented using an inorder binary tree | 1,654,947 | <p>For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. </p>
<p>The idea is having a binary tree that has its nodes accessible via indexes, e.g. </p>
<pre><code>t = ListTree()
t.insert(2,0) # 1st argument is the v... | 2 | 2009-10-31T17:03:59Z | 1,654,988 | <p>Well, a node in a binary tree cannot have a value and an index. They can have multiple pieces of data but the tree can only be keyed/built on one.</p>
<p>Maybe your assignment wants you to use the index value as the key to the tree and attach the value to the node for quick retrieval of the value given an index.</p... | 0 | 2009-10-31T17:19:12Z | [
"python",
"algorithm",
"binary-tree"
] |
List implemented using an inorder binary tree | 1,654,947 | <p>For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. </p>
<p>The idea is having a binary tree that has its nodes accessible via indexes, e.g. </p>
<pre><code>t = ListTree()
t.insert(2,0) # 1st argument is the v... | 2 | 2009-10-31T17:03:59Z | 1,655,024 | <p>Does the tree have to be balanced? Does the algorithm need to be efficient?</p>
<p>If not, then the simplest thing to do is make a tree in which all the left children are null, i.e., a tree that devolves to a linked list.</p>
<p>To insert, recursively look go to the right child, and then update the size of the nod... | 0 | 2009-10-31T17:29:21Z | [
"python",
"algorithm",
"binary-tree"
] |
List implemented using an inorder binary tree | 1,654,947 | <p>For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. </p>
<p>The idea is having a binary tree that has its nodes accessible via indexes, e.g. </p>
<pre><code>t = ListTree()
t.insert(2,0) # 1st argument is the v... | 2 | 2009-10-31T17:03:59Z | 1,655,230 | <p>I don't think you want to store the index, rather just the size of each subtree. For insance, if you wanted to look up the 10th element in the list, and the left and right subrees had 7 elements each, you would know that the root is the eight element (since it's in-order binary), and the first element of the right ... | 1 | 2009-10-31T18:43:18Z | [
"python",
"algorithm",
"binary-tree"
] |
List implemented using an inorder binary tree | 1,654,947 | <p>For the new computer science assignment we are to implement a list/array using an inorder binary tree. I would just like a suggestion rather than a solution. </p>
<p>The idea is having a binary tree that has its nodes accessible via indexes, e.g. </p>
<pre><code>t = ListTree()
t.insert(2,0) # 1st argument is the v... | 2 | 2009-10-31T17:03:59Z | 1,765,029 | <p>A very easy solution is to do GetFirst() to get the first node of the tree (this is simply finding the leftmost node of the tree). If your index N is 0, return the first node. Otherwise, call GetNodeNext() N times to get the appropriate node. This isn't super efficient though since accessing a node by index takes... | 0 | 2009-11-19T17:20:23Z | [
"python",
"algorithm",
"binary-tree"
] |
Python Scoping/Static Misunderstanding | 1,654,967 | <p>I'm really stuck on why the following code block 1 result in output 1 instead of output 2? </p>
<p><strong>Code block 1:</strong></p>
<pre><code>class FruitContainer:
def __init__(self,arr=[]):
self.array = arr
def addTo(self,something):
self.array.append(something)
def _... | 6 | 2009-10-31T17:11:27Z | 1,654,975 | <p>You should never use a mutable value (like []) for a default argument to a method. The value is computed once, and then used for every invocation. When you use an empty list as a default value, that same list is used every time the method is invoked without the argument, even as the value is modified by previous f... | 8 | 2009-10-31T17:14:37Z | [
"python",
"class",
"scope",
"iteration",
"static-members"
] |
Python Scoping/Static Misunderstanding | 1,654,967 | <p>I'm really stuck on why the following code block 1 result in output 1 instead of output 2? </p>
<p><strong>Code block 1:</strong></p>
<pre><code>class FruitContainer:
def __init__(self,arr=[]):
self.array = arr
def addTo(self,something):
self.array.append(something)
def _... | 6 | 2009-10-31T17:11:27Z | 1,654,986 | <p>As Ned says, the problem is you are using a list as a default argument. There is more detail <a href="http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">here</a>. The solution is to change <code>__init__</code> function as below: </p>
<pre><code> def __init__... | 1 | 2009-10-31T17:18:26Z | [
"python",
"class",
"scope",
"iteration",
"static-members"
] |
Python Scoping/Static Misunderstanding | 1,654,967 | <p>I'm really stuck on why the following code block 1 result in output 1 instead of output 2? </p>
<p><strong>Code block 1:</strong></p>
<pre><code>class FruitContainer:
def __init__(self,arr=[]):
self.array = arr
def addTo(self,something):
self.array.append(something)
def _... | 6 | 2009-10-31T17:11:27Z | 1,654,989 | <p>Your code has a default argument to initialize the class. The value of the default argument is evaluated once, at compile time, so every instance is initialized with the same list. Change it like so:</p>
<pre><code>def __init__(self, arr=None):
if arr is None:
self.array = []
else:
self.ar... | 2 | 2009-10-31T17:19:28Z | [
"python",
"class",
"scope",
"iteration",
"static-members"
] |
Python Scoping/Static Misunderstanding | 1,654,967 | <p>I'm really stuck on why the following code block 1 result in output 1 instead of output 2? </p>
<p><strong>Code block 1:</strong></p>
<pre><code>class FruitContainer:
def __init__(self,arr=[]):
self.array = arr
def addTo(self,something):
self.array.append(something)
def _... | 6 | 2009-10-31T17:11:27Z | 1,656,920 | <p>A better solution than passing in None â in this particular instance, rather than in general â is to treat the arr parameter to __init__ as an enumerable set of items to pre-initialize the FruitContainer with, rather than an array to use for internal storage:</p>
<pre><code>class FruitContainer:
def __init__(... | 0 | 2009-11-01T11:20:50Z | [
"python",
"class",
"scope",
"iteration",
"static-members"
] |
Explanation of PyAPI_DATA() macro? | 1,655,271 | <p>I've searched all over the web and <a href="http://www.google.com/search?hl=en&q=site%3Adocs.python.org%2Fc-api+PyAPI%5FDATA&aq=f&oq=&aqi=" rel="nofollow">can't seem to find</a> documentation or even a simple explanation of what PyAPI_DATA() does (even though it is <a href="http://svn.python.org/proj... | 1 | 2009-10-31T19:00:24Z | 1,655,314 | <p>It's used to mark public API variables (as Python's core is usually a dynamic library), e.g. on Windows, it's expanded to <code>extern __declspec(dllexport) RTYPE</code> when core is compiled and to <code>extern __declspec(dllimport) RTYPE</code> when e.g. modules are compiled. It's defined in <code>Include/pyport.h... | 3 | 2009-10-31T19:16:45Z | [
"python",
"c",
"api",
"python-c-api"
] |
Subclassing python's dict, override of __setitem__ doesn't retain new value | 1,655,422 | <p>I'm subclassing dict, but ran into a problem with <strong>setitem</strong> where one assignment works, but another assignment does not. I've boiled it down to the following basic problem:</p>
<pre><code>class CustomDict(dict):
def __setitem__(self, key, value):
super(CustomDict, self).__setitem__(key, value)
</c... | 2 | 2009-10-31T19:49:39Z | 1,655,427 | <p>You are constructing new instances of CustomDict on each line. <code>CustomDict(data)</code> makes a new instance, which copies <code>data</code>.</p>
<p>Try this:</p>
<pre><code>cd = CustomData({"message":"foo"})
cd["message"] = "bar"
print cd # prints "{'message': 'bar'}".
</code></pre>
| 10 | 2009-10-31T19:51:29Z | [
"python",
"subclass",
"dictionary"
] |
Can I access the __dict__ object for the local scope? | 1,655,509 | <p>Here is my situation...</p>
<p>I am trying to dynamically generate a bunch of stuff in my settings.py file on a django site.</p>
<p>I am setting up several sites, (via sites framework) and I want to have some values I plug in to a function that will generate a portion of the settings file for each site.</p>
<p>fo... | 0 | 2009-10-31T20:20:09Z | 1,655,571 | <p>Dict returned by <code>locals()</code> (or <code>globals()</code>) is mutable, so you could do:</p>
<pre><code>def get_dynamic_settings(context_dict):
context_dict['DEFAULT_FROM_EMAIL'] = '%s <noreply@otakupride.com>' % context_dict['SITE_NAME']
context_dict['ROOT_URLCONF'] = 'mysite.urls.%s' % contex... | 3 | 2009-10-31T20:43:10Z | [
"python",
"django",
"scope",
"settings"
] |
Can I access the __dict__ object for the local scope? | 1,655,509 | <p>Here is my situation...</p>
<p>I am trying to dynamically generate a bunch of stuff in my settings.py file on a django site.</p>
<p>I am setting up several sites, (via sites framework) and I want to have some values I plug in to a function that will generate a portion of the settings file for each site.</p>
<p>fo... | 0 | 2009-10-31T20:20:09Z | 1,655,573 | <p>You might want to look into the various schemes people have used to configure many django sites without duplication: <a href="http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-django">http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-djang... | 3 | 2009-10-31T20:44:01Z | [
"python",
"django",
"scope",
"settings"
] |
Converting Python code to PHP | 1,655,556 | <p>What is the following Python code in PHP?</p>
<pre><code>import sys
li = range(1,777);
def countFigure(li, n):
m = str(n);
return str(li).count(m);
# counting figures
for substr in range(1,10):
print substr, " ", countFigure(li, substr);
</code></pre>
<p>Wanted output for 777</p>
<pre><... | 0 | 2009-10-31T20:37:59Z | 1,655,580 | <p>It's been a while since I did any Python but I think this should do it.
If you could clarify what <code>str(li)</code> looks like it would help.</p>
<pre><code><?php
$li = implode('', range(1, 776));
function countFigure($li, $n)
{
return substr_count($li, $n);
}
// counting figures
foreach (range(1, 9) ... | 1 | 2009-10-31T20:45:52Z | [
"php",
"python"
] |
How can I generate random numbers in Python? | 1,655,559 | <p>Are there any built-in libraries in Python or Numpy to generate random numbers based on various common distributions, such as:</p>
<ul>
<li>Normal</li>
<li>Poisson</li>
<li>Exponential</li>
<li>Bernoulli</li>
</ul>
<p>And various others?</p>
<p>Are there any such libraries with multi-variate distributions?</p>
| 13 | 2009-10-31T20:38:36Z | 1,655,565 | <p>The <a href="http://docs.python.org/library/random.html" rel="nofollow"><code>random</code></a> module has tons of functions for generating random numbers in lots of way. Not sure it has multi-variate.</p>
<p><a href="http://docs.scipy.org/doc/numpy/reference/routines.random.html" rel="nofollow">Numpy.random</a> w... | 5 | 2009-10-31T20:41:10Z | [
"python",
"random"
] |
How can I generate random numbers in Python? | 1,655,559 | <p>Are there any built-in libraries in Python or Numpy to generate random numbers based on various common distributions, such as:</p>
<ul>
<li>Normal</li>
<li>Poisson</li>
<li>Exponential</li>
<li>Bernoulli</li>
</ul>
<p>And various others?</p>
<p>Are there any such libraries with multi-variate distributions?</p>
| 13 | 2009-10-31T20:38:36Z | 1,655,619 | <pre><code>#!/usr/bin/env python
from scipy.stats import bernoulli,poisson,norm,expon
</code></pre>
<p>bernoulli, poisson, norm, expon and many others are documented <a href="http://www.scipy.org/doc/api_docs/SciPy.stats.distributions.html" rel="nofollow">here</a> </p>
<pre><code>print(norm.rvs(size=30))
print(bernou... | 27 | 2009-10-31T20:58:54Z | [
"python",
"random"
] |
How do you flush Python sockets? | 1,655,560 | <p>I've written a server in Python that is meant to send data to the client in the form "Header:Message"</p>
<p>I would like to be able to have each message sent individually so that the client will need to perform minimal work in order to read the "header" and the "message"</p>
<p>Unfortunately, I can't figure out ... | 9 | 2009-10-31T20:38:59Z | 1,655,585 | <p>I guess you are talking over a TCP connection.</p>
<p>Your approach is flawed. A TCP stream is defined as a stream of bytes. You always have to use some sort of separator and may not rely on the network stack to separate your messages.</p>
<p>If you really need datagram based services switch to UDP. You'll need to... | 17 | 2009-10-31T20:47:32Z | [
"python",
"sockets",
"flush"
] |
How do you flush Python sockets? | 1,655,560 | <p>I've written a server in Python that is meant to send data to the client in the form "Header:Message"</p>
<p>I would like to be able to have each message sent individually so that the client will need to perform minimal work in order to read the "header" and the "message"</p>
<p>Unfortunately, I can't figure out ... | 9 | 2009-10-31T20:38:59Z | 1,656,960 | <p>What you are trying to do, is split your data into "batches".</p>
<p>For example, you are operating on "batches" whenever you read "lines" off a file. What defines a "line"? It's a sequence of bytes terminated by '\n'. Another example is: you read 64KiB "chunks" off a file. What defines a "chunk"? You do, since you... | 0 | 2009-11-01T11:42:56Z | [
"python",
"sockets",
"flush"
] |
How do you flush Python sockets? | 1,655,560 | <p>I've written a server in Python that is meant to send data to the client in the form "Header:Message"</p>
<p>I would like to be able to have each message sent individually so that the client will need to perform minimal work in order to read the "header" and the "message"</p>
<p>Unfortunately, I can't figure out ... | 9 | 2009-10-31T20:38:59Z | 4,894,495 | <p>just append \n after every message...
like </p>
<p>socket.send ("header1:message1\n") </p>
<p>socket.send ("header2:message2\n") </p>
<p>socket.send ("header3:message3\n") </p>
| 4 | 2011-02-04T04:13:25Z | [
"python",
"sockets",
"flush"
] |
Help with a AppEngine Handler Regex? | 1,655,745 | <p>I've been trying to design a Google AppEngine Python handler regex and haven't been too successful in getting it to work.</p>
<p>I'm trying to handle API calls similar to <a href="http://wiki.openstreetmap.org/wiki/API_v0.6" rel="nofollow">OpenStreetMap's</a>.</p>
<p>My current regex looks like this:</p>
<pre><co... | 1 | 2009-10-31T22:06:00Z | 1,655,764 | <p>Try this:</p>
<pre><code>^/api/0.6/([^/]+)/([^/]+)/?([^/]*)$
</code></pre>
<p>My Python tests:</p>
<pre><code>>>> regex = re.compile(r"^/api/0.6/([^/]+)/([^/]+)/?([^/]*)$")
>>> regex.match("/api/0.6/changeset") is None
True
>>> regex.match("/api/0.6/changeset/723").groups()
('changeset'... | 3 | 2009-10-31T22:14:31Z | [
"python",
"regex",
"google-app-engine"
] |
Querying the connecting device for usb devices in OS X | 1,655,927 | <p>Ok, so here's the setup. In OS X (>= 10.5), is it possible, given a mounted usb device with a known location, say /Volumes/FLASHDRIVE, to find out whether this device is connecting through another usb device (a card reader for example) and if so, which one. </p>
<p>Ideally, this could all be done in python, but if ... | 0 | 2009-10-31T23:34:37Z | 1,655,945 | <p>You're confusing the term <em>device</em> with the term <em>volume</em>--in this example (and in most real world situations) there would only be one device involved. </p>
<p>The state of most hardware falls under the purview of IOKit, and the only way you can possibly get to this information from Python is through ... | 0 | 2009-10-31T23:47:29Z | [
"python",
"osx",
"usb"
] |
PYTHONPATH environment variable...how do I make every subdirectory afterwards? | 1,655,932 | <p>I currently do this:</p>
<pre><code>PYTHONPATH=/home/$USER:/home/$USER/respository:/home/$USER/repository/python-stuff
</code></pre>
<p>How can I make it so that the PYTHONPATH can include everything subdirectory?</p>
<pre><code>PYTHONPATH = /home/$USER/....and-all-subdirectories
</code></pre>
| 1 | 2009-10-31T23:38:25Z | 1,655,961 | <p>It's generally a bad idea to have on <code>sys.path</code> two paths one of which is the parent of the other -- assuming the sub-directory one contains an <code>__init__.py</code> file to mark it as a package, of course. If only the parent directory is in the path (and <code>$PYTHONPATH</code> is part of what <code... | 8 | 2009-10-31T23:55:05Z | [
"python",
"linux",
"unix",
"environment-variables"
] |
PYTHONPATH environment variable...how do I make every subdirectory afterwards? | 1,655,932 | <p>I currently do this:</p>
<pre><code>PYTHONPATH=/home/$USER:/home/$USER/respository:/home/$USER/repository/python-stuff
</code></pre>
<p>How can I make it so that the PYTHONPATH can include everything subdirectory?</p>
<pre><code>PYTHONPATH = /home/$USER/....and-all-subdirectories
</code></pre>
| 1 | 2009-10-31T23:38:25Z | 24,766,945 | <p>I did it like this:</p>
<pre><code>import os
import sys
all_modules = {} #keeps track of the module names
def discoverModules():
''' Populates a dictionary holding every module name in the directory tree that this file is run '''
global all_modules
for dirname, dirnames, filenames in os.walk('.')... | 1 | 2014-07-15T19:49:45Z | [
"python",
"linux",
"unix",
"environment-variables"
] |
Python: Lazy String Decoding | 1,656,048 | <p>I'm writing a parser, and there is LOTS of text to decode but most of my users will only care about a few fields from all the data. So I only want to do the decoding when a user actually uses some of the data. Is this a good way to do it?</p>
<pre><code>class LazyString(str):
def __init__(self, v) :
sel... | 3 | 2009-11-01T00:49:49Z | 1,656,065 | <p>I don't see any kind on lazy evaluation in your code. The fact that you use <code>xrange</code> only means that the list of integers from <code>0</code> to <code>len(s)</code> will be generated on demand. The whole string <code>r</code> will be decoded during string conversion anyway.</p>
<p>The best way to impleme... | 1 | 2009-11-01T00:58:49Z | [
"python",
"lazy-evaluation"
] |
Python: Lazy String Decoding | 1,656,048 | <p>I'm writing a parser, and there is LOTS of text to decode but most of my users will only care about a few fields from all the data. So I only want to do the decoding when a user actually uses some of the data. Is this a good way to do it?</p>
<pre><code>class LazyString(str):
def __init__(self, v) :
sel... | 3 | 2009-11-01T00:49:49Z | 1,656,123 | <p>What you're doing is built in already:</p>
<pre><code>s = "i am a string!".encode('hex')
# what you do
r = ""
for i in xrange(0, len(s), 2) :
r += chr(int(s[i:i+2], 16))
# but decoding is builtin
print r==s.decode('hex') # => True
</code></pre>
<p>As you can see your whole decoding is <code>s.decode('hex')... | 0 | 2009-11-01T01:40:44Z | [
"python",
"lazy-evaluation"
] |
Python: Lazy String Decoding | 1,656,048 | <p>I'm writing a parser, and there is LOTS of text to decode but most of my users will only care about a few fields from all the data. So I only want to do the decoding when a user actually uses some of the data. Is this a good way to do it?</p>
<pre><code>class LazyString(str):
def __init__(self, v) :
sel... | 3 | 2009-11-01T00:49:49Z | 1,656,889 | <p>The methods you need to override really depend on how are planning to use you new string type. </p>
<p>However you str based type looks a little suspicious to me, have you looked into the implementation of str to check that it has the <code>value</code> attribute that you are setting in your <code>__init__()</code>... | 0 | 2009-11-01T11:06:46Z | [
"python",
"lazy-evaluation"
] |
Python: Lazy String Decoding | 1,656,048 | <p>I'm writing a parser, and there is LOTS of text to decode but most of my users will only care about a few fields from all the data. So I only want to do the decoding when a user actually uses some of the data. Is this a good way to do it?</p>
<pre><code>class LazyString(str):
def __init__(self, v) :
sel... | 3 | 2009-11-01T00:49:49Z | 1,658,699 | <p>I'm not sure how implementing a string subclass is of much benefit here. It seems to me that if you're processing a stream containing petabytes of data, whenever you've created an object that you don't need to you've already lost the game. Your first priority should be to ignore as much input as you possibly can.<... | 1 | 2009-11-01T23:03:46Z | [
"python",
"lazy-evaluation"
] |
Python: Lazy String Decoding | 1,656,048 | <p>I'm writing a parser, and there is LOTS of text to decode but most of my users will only care about a few fields from all the data. So I only want to do the decoding when a user actually uses some of the data. Is this a good way to do it?</p>
<pre><code>class LazyString(str):
def __init__(self, v) :
sel... | 3 | 2009-11-01T00:49:49Z | 1,658,875 | <p>The question is incomplete, in that the answer will depend on details of the encoding you use.</p>
<p>Say, if you encode a list of strings as pascal strings (i.e. prefixed with string length encoded as a fixed-size integer), and say you want to read the 100th string from the list, you may seek() forward for each of... | 0 | 2009-11-02T00:12:39Z | [
"python",
"lazy-evaluation"
] |
appending successfully to a python list | 1,656,059 | <p>This may seem like the worlds simplest python question... But I'm going to give it a go of explaining it.</p>
<p>Basically I have to loop through pages of json results from a query.</p>
<p>the standard result is this</p>
<pre><code>{'result': [{result 1}, {result 2}], 'next_page': '2'}
</code></pre>
<p>I need th... | 0 | 2009-11-01T00:56:12Z | 1,656,073 | <pre><code>new_result = result.append(json_loop['results'])
</code></pre>
<p>The list is appended as a side-effect of the method call.
<code>append()</code> actually returns <code>None</code>, so <code>new_result</code> is now a reference to <code>None</code>.</p>
| 2 | 2009-11-01T01:04:25Z | [
"python",
"list",
"append",
"dictionary"
] |
appending successfully to a python list | 1,656,059 | <p>This may seem like the worlds simplest python question... But I'm going to give it a go of explaining it.</p>
<p>Basically I have to loop through pages of json results from a query.</p>
<p>the standard result is this</p>
<pre><code>{'result': [{result 1}, {result 2}], 'next_page': '2'}
</code></pre>
<p>I need th... | 0 | 2009-11-01T00:56:12Z | 1,656,092 | <p>You want to use </p>
<pre><code>result.append(json_loop['results']) # this grabs the result and "should" put it into the list
new_result = result
</code></pre>
<p>if you insist on doing it that way. As Bastien said, <code>result.append(whatever) == None</code></p>
| 1 | 2009-11-01T01:17:14Z | [
"python",
"list",
"append",
"dictionary"
] |
appending successfully to a python list | 1,656,059 | <p>This may seem like the worlds simplest python question... But I'm going to give it a go of explaining it.</p>
<p>Basically I have to loop through pages of json results from a query.</p>
<p>the standard result is this</p>
<pre><code>{'result': [{result 1}, {result 2}], 'next_page': '2'}
</code></pre>
<p>I need th... | 0 | 2009-11-01T00:56:12Z | 1,656,095 | <p>AFAICS, you don't need the variable new_result at all.</p>
<pre><code>result_count = len(result)
</code></pre>
<p>will give you the answer you need.</p>
| 0 | 2009-11-01T01:18:41Z | [
"python",
"list",
"append",
"dictionary"
] |
appending successfully to a python list | 1,656,059 | <p>This may seem like the worlds simplest python question... But I'm going to give it a go of explaining it.</p>
<p>Basically I have to loop through pages of json results from a query.</p>
<p>the standard result is this</p>
<pre><code>{'result': [{result 1}, {result 2}], 'next_page': '2'}
</code></pre>
<p>I need th... | 0 | 2009-11-01T00:56:12Z | 1,656,101 | <p>Alternate (cleaner) approach, making one big list:</p>
<pre><code>results = []
res = { "next_page": "magic_token_to_get_first_page" }
while "next_page" in res:
fp = urllib2.urlopen("http://search.twitter.com/search.json" + res["next_page"])
res = simplejson.load(fp)
fp.close()
results.extend(res["re... | 4 | 2009-11-01T01:24:15Z | [
"python",
"list",
"append",
"dictionary"
] |
appending successfully to a python list | 1,656,059 | <p>This may seem like the worlds simplest python question... But I'm going to give it a go of explaining it.</p>
<p>Basically I have to loop through pages of json results from a query.</p>
<p>the standard result is this</p>
<pre><code>{'result': [{result 1}, {result 2}], 'next_page': '2'}
</code></pre>
<p>I need th... | 0 | 2009-11-01T00:56:12Z | 1,656,335 | <p>you cannot append into a dict..you can append into your list inside your dict,you should do like this</p>
<pre><code>result['result'].append(json_loop['results'])
</code></pre>
<p>if you want to check if there is no next page value in your result dict,and you want to delete the key from the dict,just do like this<... | 0 | 2009-11-01T03:50:06Z | [
"python",
"list",
"append",
"dictionary"
] |
Shortest total path among set of Latitude/Longitudes | 1,656,112 | <p>I have a set of 52 or so latitude/longitude pairs. I simply need to find the shortest path through all of them; it doesn't matter where staring point or ending point is. </p>
<p>I've implemented Dijkstra's algorithm by hand multiple times before and don't really have the time to do it again. I've found a couple thi... | 2 | 2009-11-01T01:32:55Z | 1,656,171 | <p>Isn't this the <a href="http://en.wikipedia.org/wiki/Traveling%5Fsalesman%5Fproblem" rel="nofollow">Traveling Salesman Problem</a>, and therefore there is no efficient way to solve it?</p>
| 0 | 2009-11-01T02:08:19Z | [
"python",
"clojure",
"mapping",
"graph-theory"
] |
Shortest total path among set of Latitude/Longitudes | 1,656,112 | <p>I have a set of 52 or so latitude/longitude pairs. I simply need to find the shortest path through all of them; it doesn't matter where staring point or ending point is. </p>
<p>I've implemented Dijkstra's algorithm by hand multiple times before and don't really have the time to do it again. I've found a couple thi... | 2 | 2009-11-01T01:32:55Z | 1,656,186 | <p>If this is a closed path, it is the Traveling Salesman Problem, and a sub-optimal but quite effective way to resolve it is to use <a href="http://en.wikipedia.org/wiki/Simulated%5Fannealing" rel="nofollow">Simulated Annealing</a></p>
| 3 | 2009-11-01T02:12:35Z | [
"python",
"clojure",
"mapping",
"graph-theory"
] |
Shortest total path among set of Latitude/Longitudes | 1,656,112 | <p>I have a set of 52 or so latitude/longitude pairs. I simply need to find the shortest path through all of them; it doesn't matter where staring point or ending point is. </p>
<p>I've implemented Dijkstra's algorithm by hand multiple times before and don't really have the time to do it again. I've found a couple thi... | 2 | 2009-11-01T01:32:55Z | 1,656,618 | <p>In python, the best graph handling library I was able to put my hands on is <a href="http://networkx.lanl.gov/" rel="nofollow">networkx</a>. It supports a broad range of different algos for <a href="http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar" rel="nofollow... | 2 | 2009-11-01T07:43:09Z | [
"python",
"clojure",
"mapping",
"graph-theory"
] |
Grab some ofx data with python | 1,656,611 | <p>I was trying to use <a href="http://www.jongsma.org/gc/scripts/ofx-ba.py" rel="nofollow">http://www.jongsma.org/gc/scripts/ofx-ba.py</a> to grab my bank account information from wachovia. Having no luck, I decided that I would just try to manually construct some request data using <a href="http://www.ofx.net/OFXExa... | 3 | 2009-11-01T07:36:26Z | 1,656,742 | <p>The problem was that you were previously passing in the data from your file directly as the data parameter to the <code>Request</code>. The file you were reading in contains both the headers and the data that you should be sending. You needed to supply the headers and the data separately as you have now done.</p>
<... | 2 | 2009-11-01T09:36:52Z | [
"python",
"xml",
"urllib2",
"sgml",
"ofx"
] |
Grab some ofx data with python | 1,656,611 | <p>I was trying to use <a href="http://www.jongsma.org/gc/scripts/ofx-ba.py" rel="nofollow">http://www.jongsma.org/gc/scripts/ofx-ba.py</a> to grab my bank account information from wachovia. Having no luck, I decided that I would just try to manually construct some request data using <a href="http://www.ofx.net/OFXExa... | 3 | 2009-11-01T07:36:26Z | 1,656,823 | <p>could just be authentication? (or lack therof?)</p>
| 0 | 2009-11-01T10:25:44Z | [
"python",
"xml",
"urllib2",
"sgml",
"ofx"
] |
How can a recursive regexp be implemented in python? | 1,656,859 | <p>I'm interested how can be implemented recursive regexp matching in Python (I've not found any examples :( ). For example how would one write expression which matches "bracket balanced" string like "foo(bar(bar(foo)))(foo1)bar1"</p>
| 14 | 2009-11-01T10:48:38Z | 1,656,940 | <p>Unfortunately I don't think Python's regexps support recursive patterns.</p>
<p>You can probably parse it with something like pyparsing: <a href="http://pyparsing.wikispaces.com/" rel="nofollow">http://pyparsing.wikispaces.com/</a></p>
| 3 | 2009-11-01T11:34:10Z | [
"python",
"regex",
"recursion"
] |
How can a recursive regexp be implemented in python? | 1,656,859 | <p>I'm interested how can be implemented recursive regexp matching in Python (I've not found any examples :( ). For example how would one write expression which matches "bracket balanced" string like "foo(bar(bar(foo)))(foo1)bar1"</p>
| 14 | 2009-11-01T10:48:38Z | 1,656,944 | <p>You can't do it with a regexp. Python doesn't support recursive regexp</p>
| 4 | 2009-11-01T11:35:03Z | [
"python",
"regex",
"recursion"
] |
How can a recursive regexp be implemented in python? | 1,656,859 | <p>I'm interested how can be implemented recursive regexp matching in Python (I've not found any examples :( ). For example how would one write expression which matches "bracket balanced" string like "foo(bar(bar(foo)))(foo1)bar1"</p>
| 14 | 2009-11-01T10:48:38Z | 1,657,068 | <p>You could use <a href="http://pyparsing.wikispaces.com/">pyparsing</a> </p>
<pre><code>#!/usr/bin/env python
from pyparsing import nestedExpr
import sys
astring=sys.argv[1]
if not astring.startswith('('):
astring='('+astring+')'
expr = nestedExpr('(', ')')
result=expr.parseString(astring).asList()[0]
print(res... | 15 | 2009-11-01T12:37:59Z | [
"python",
"regex",
"recursion"
] |
How can a recursive regexp be implemented in python? | 1,656,859 | <p>I'm interested how can be implemented recursive regexp matching in Python (I've not found any examples :( ). For example how would one write expression which matches "bracket balanced" string like "foo(bar(bar(foo)))(foo1)bar1"</p>
| 14 | 2009-11-01T10:48:38Z | 17,407,483 | <p>This is an old question, but for the people who come here through searches:</p>
<p>There's an atlernative regex module for python that does support recursive patterns:
<a href="https://pypi.python.org/pypi/regex">https://pypi.python.org/pypi/regex</a></p>
<p>And it has a lot of more nice improvements on <code>re</... | 10 | 2013-07-01T14:55:44Z | [
"python",
"regex",
"recursion"
] |
Can Django be used with py2exe? | 1,657,063 | <p>We'd like to create a Django Intranet application for mass market. We only need to support Windows users, and it needs to be very easy for a Windows admin (or "technical user") to deploy (baring in mind that most Windows admins/users have little experience with Python, etc).</p>
<p>Is Django + py2exe the answer? Do... | 10 | 2009-11-01T12:36:23Z | 1,657,074 | <p>Yes, you require:</p>
<ul>
<li>Python 2.5.2</li>
<li>Django (svn version at
least 2008-06-05)</li>
<li>CherryPy (web
server)</li>
<li>Py2Exe</li>
</ul>
<p><a href="http://misunderstandings.wordpress.com/2008/06/26/django-desktop-app/">Tutorial</a></p>
| 11 | 2009-11-01T12:40:01Z | [
"python",
"django",
"py2exe"
] |
Can Django be used with py2exe? | 1,657,063 | <p>We'd like to create a Django Intranet application for mass market. We only need to support Windows users, and it needs to be very easy for a Windows admin (or "technical user") to deploy (baring in mind that most Windows admins/users have little experience with Python, etc).</p>
<p>Is Django + py2exe the answer? Do... | 10 | 2009-11-01T12:36:23Z | 1,918,326 | <p>PyInstaller has <a href="https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django" rel="nofollow">builtin support</a> for Django applications. It will do most of the magic by itself.</p>
| 3 | 2009-12-16T22:38:47Z | [
"python",
"django",
"py2exe"
] |
Can Django be used with py2exe? | 1,657,063 | <p>We'd like to create a Django Intranet application for mass market. We only need to support Windows users, and it needs to be very easy for a Windows admin (or "technical user") to deploy (baring in mind that most Windows admins/users have little experience with Python, etc).</p>
<p>Is Django + py2exe the answer? Do... | 10 | 2009-11-01T12:36:23Z | 17,727,602 | <p>I recently did this with Django 1.5 and python 2.7. I didn't embed a webserver, because I really just wanted the ORM.</p>
<p>Here are the options I used in setup.py:</p>
<pre><code>options = {
"py2exe":{
"includes": ["Cookie","htmlentitydefs"],
"packages":["django","sqlite3","email"],
... | 0 | 2013-07-18T15:23:00Z | [
"python",
"django",
"py2exe"
] |
how to open a URL with non utf-8 arguments | 1,657,201 | <p>Using Python I need to transfer non utf-8 encoded data (specifically shift-jis) to a URL via the query string.
How should I transfer the data? Quote it? Encode in utf-8?</p>
<p>Thanks</p>
| 0 | 2009-11-01T13:49:07Z | 1,657,227 | <p>I don't know what unicode has to do with this, since the query string is a string of bytes. You can use the quoting functions in <a href="http://docs.python.org/library/urllib.html" rel="nofollow">urllib</a> to quote plain strings so that they can be passed within query strings.</p>
| 1 | 2009-11-01T13:59:33Z | [
"python",
"unicode",
"urllib",
"quotes",
"shift-jis"
] |
how to open a URL with non utf-8 arguments | 1,657,201 | <p>Using Python I need to transfer non utf-8 encoded data (specifically shift-jis) to a URL via the query string.
How should I transfer the data? Quote it? Encode in utf-8?</p>
<p>Thanks</p>
| 0 | 2009-11-01T13:49:07Z | 1,657,234 | <p>By the »query string« you mean HTTP GET like in <code>http:/{URL}?data=XYZ</code>?</p>
<p>You have encoding what ever data you have via <code>base64.b64encode</code> using <code>-_</code> as alternative character to be URL safe as an option. See <a href="http://docs.python.org/library/base64.html" rel="nofollow">... | 0 | 2009-11-01T14:00:57Z | [
"python",
"unicode",
"urllib",
"quotes",
"shift-jis"
] |
how to open a URL with non utf-8 arguments | 1,657,201 | <p>Using Python I need to transfer non utf-8 encoded data (specifically shift-jis) to a URL via the query string.
How should I transfer the data? Quote it? Encode in utf-8?</p>
<p>Thanks</p>
| 0 | 2009-11-01T13:49:07Z | 1,657,500 | <p>Query string parameters are byte-based. Whilst IRI-to-URI and typed non-ASCII characters will typically use UTF-8, there is nothing forcing you to send or receive your own parameters in that encoding.</p>
<p>So for Shift-JIS (actually typically cp932, the Windows extension of that encoding):</p>
<pre><code>foo= u'... | 4 | 2009-11-01T15:58:35Z | [
"python",
"unicode",
"urllib",
"quotes",
"shift-jis"
] |
Help sorting: first by this, and then by that | 1,657,242 | <p>I have a list of tuples I am trying to sort and could use some help. </p>
<p>The field I want to sort by in the tuples looks like "XXX_YYY". First, I want to group the XXX values in reverse order, and then, within those groups, I want to place the YYY values in normal sort order. (NOTE: I am just as happy, actua... | 5 | 2009-11-01T14:04:59Z | 1,657,283 | <pre><code>def my_cmp(x, y):
x1, x2 = x[0].split('_')
y1, y2 = y[0].split('_')
return -cmp(x1, y1) or cmp(x2, y2)
my_list = [
(u'community_news', u'Community: News & Information'),
(u'kf_video', u'KF: Video'),
(u'community_video', u'Community: Video'),
(u'kf_news', u'KF: News & Informa... | 10 | 2009-11-01T14:21:44Z | [
"python",
"sorting"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.