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 |
|---|---|---|---|---|---|---|---|---|---|
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,551 | <p>Well, don't know if such library exists, but <code>date</code> from GNU coreutils has something like that:</p>
<pre><code>$ date -d "last monday"
Mon Mar 2 00:00:00 RST 2009
</code></pre>
| 1 | 2009-03-05T12:21:06Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,552 | <p>I think you can get pretty far using just Python's standard <a href="http://docs.python.org/library/calendar.html#module-calendar" rel="nofollow">calendar</a> module, maybe by adding some "human-friendly" methods on top of those methods. The module gives you iterators for dates, while taking weeks into consideration... | 3 | 2009-03-05T12:21:15Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,595 | <p>Use <strong>mxDateTime</strong>. <a href="http://www.egenix.com/products/python/mxBase/mxDateTime/" rel="nofollow">http://www.egenix.com/products/python/mxBase/mxDateTime/</a></p>
| 1 | 2009-03-05T12:32:41Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,623 | <p>If this is for a web app take a look at <a href="http://www.datejs.com/" rel="nofollow">Datejs</a>, it may be easier to use that in your form then just pass it's date object's value to Python.</p>
<p>If you end up writing one yourself the source may be helpful too.</p>
| -1 | 2009-03-05T12:46:27Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,662 | <p>Neither mxDateTime nor Datejs nor that webservice support "last thursday of the month". The OP wants to know all of the last thursdays of the month for, say, a full year.</p>
<p>mxDateTime supports the operations, but the question must be posed in Python code, not as a string.</p>
<p>The best I could figure is <a ... | 7 | 2009-03-05T13:03:01Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 614,667 | <p>The algorithms aren't hard. They're provided in the following book, which is worth every penny.</p>
<p><a href="http://emr.cs.iit.edu/home/reingold/calendar-book/third-edition/" rel="nofollow">http://emr.cs.iit.edu/home/reingold/calendar-book/third-edition/</a></p>
<p>The general approach is to find first days of... | 3 | 2009-03-05T13:03:52Z | [
"python",
"datetime",
"date"
] |
Tool/library for calculating intervals like "last thursday of the month" | 614,518 | <p>I'm looking for a command line tool or some sort of python library (that I can then wrap), so that I can calculate dates that are specified like "last thursday of the month".</p>
<p>i.e. I want to let people enter human friendly text like that above and it should be able to calculate all the dates for any month/yea... | 2 | 2009-03-05T12:11:07Z | 615,731 | <p>I'm not entirely sure, but you might look at the Python <a href="http://labix.org/python-dateutil" rel="nofollow">DateUtil</a> module.</p>
| 2 | 2009-03-05T17:20:41Z | [
"python",
"datetime",
"date"
] |
Big images with cairo | 614,949 | <p>I have to render a very big image (>50.000² pixel) with cairo. To do this without running out of memory I render parts of the image (<1.000² pixel) one after another and merge them together later.</p>
<ol>
<li>Create 1000x1000 Surface</li>
<li>Translate to position of the current part</li>
<li>Draw image (call... | 5 | 2009-03-05T14:19:46Z | 615,003 | <p>First of all, using C or Vala instead of Python will probably speed things up.</p>
<p>As for memory usage, I would try writing to my own stream, rather than a file (see <a href="http://cairographics.org/manual/cairo-png-functions.html#cairo-surface-write-to-png-stream" rel="nofollow">write_to_png_stream</a>). This ... | 2 | 2009-03-05T14:30:03Z | [
"python",
"cairo"
] |
How to detect errors from compileall.compile_dir? | 615,632 | <p>How do I detect an error when compiling a directory of python files using compile_dir?</p>
<p>Currently I get something on stderr, but no way to detect it in my app.
py_compile.compile() takes a 'doraise' argument, but nothing here.</p>
<p>Or is there a better way to do this from a python script?</p>
<p>Edit:</p>... | 0 | 2009-03-05T16:48:51Z | 615,682 | <p>works fine for me. Could it be that you're not setting <code>doraise</code> to <code>True</code> somehow?</p>
| 0 | 2009-03-05T17:02:45Z | [
"python"
] |
How to detect errors from compileall.compile_dir? | 615,632 | <p>How do I detect an error when compiling a directory of python files using compile_dir?</p>
<p>Currently I get something on stderr, but no way to detect it in my app.
py_compile.compile() takes a 'doraise' argument, but nothing here.</p>
<p>Or is there a better way to do this from a python script?</p>
<p>Edit:</p>... | 0 | 2009-03-05T16:48:51Z | 616,423 | <p>I don't see a better way. The code is designed to support the command-line program, and the API doesn't seem fully meant to be used as a library.</p>
<p>If you really had to use the compileall then you could fake it out with this hack, which notices that "quiet" is tested for boolean-ness while in the caught except... | 2 | 2009-03-05T20:10:23Z | [
"python"
] |
Django labels and translations - Model Design | 616,187 | <p>Lets say I have the following Django model:</p>
<pre><code>class StandardLabel(models.Model):
id = models.AutoField(primary_key=True)
label = models.CharField(max_length=255)
abbreviation = models.CharField(max_length=255)
</code></pre>
<p>Each label has an ID number, the label text, and an abbreviatio... | 2 | 2009-03-05T19:09:49Z | 616,317 | <p>I would keep things as simple as possible. The lookup will be faster and the code cleaner with something like this:</p>
<pre><code>class StandardLabel(models.Model):
abbreviation = models.CharField(max_length=255)
label = models.CharField(max_length=255)
language = models.CharField(max_length=2)
# ... | 2 | 2009-03-05T19:38:25Z | [
"python",
"django",
"django-models",
"localization"
] |
Django labels and translations - Model Design | 616,187 | <p>Lets say I have the following Django model:</p>
<pre><code>class StandardLabel(models.Model):
id = models.AutoField(primary_key=True)
label = models.CharField(max_length=255)
abbreviation = models.CharField(max_length=255)
</code></pre>
<p>Each label has an ID number, the label text, and an abbreviatio... | 2 | 2009-03-05T19:09:49Z | 616,361 | <p>Another option you might consider, depending on your application design of course, is to make use of Django's internationalization features. The approach they use is quite common to the approach found in desktop software. </p>
<p>I see the question was edited to add a reference to Django internationalization, so yo... | 3 | 2009-03-05T19:51:20Z | [
"python",
"django",
"django-models",
"localization"
] |
Django labels and translations - Model Design | 616,187 | <p>Lets say I have the following Django model:</p>
<pre><code>class StandardLabel(models.Model):
id = models.AutoField(primary_key=True)
label = models.CharField(max_length=255)
abbreviation = models.CharField(max_length=255)
</code></pre>
<p>Each label has an ID number, the label text, and an abbreviatio... | 2 | 2009-03-05T19:09:49Z | 616,702 | <p>Although I would go with <a href="http://stackoverflow.com/questions/616187/django-labels-and-translations-model-design/616317#616317">Daniel's solution</a>, here is an alternative from what I've understood from your comments:</p>
<p>You can use an <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#xm... | 0 | 2009-03-05T21:30:00Z | [
"python",
"django",
"django-models",
"localization"
] |
Django labels and translations - Model Design | 616,187 | <p>Lets say I have the following Django model:</p>
<pre><code>class StandardLabel(models.Model):
id = models.AutoField(primary_key=True)
label = models.CharField(max_length=255)
abbreviation = models.CharField(max_length=255)
</code></pre>
<p>Each label has an ID number, the label text, and an abbreviatio... | 2 | 2009-03-05T19:09:49Z | 620,095 | <p>I'd much prefer to add a field per language than a new model instance per language. It does require schema alteration when you add a new language, but that isn't hard, and how often do you expect to add languages? In the meantime, it'll give you better database performance (no added joins or indexes) and you don't... | 1 | 2009-03-06T19:26:19Z | [
"python",
"django",
"django-models",
"localization"
] |
Is it possible to remove recursion from this function? | 616,416 | <p>I have been playing with this a while, and just cannot see an obvious solution. I want to remove the recursion from the XinY_Go function.</p>
<pre><code>def XinY_Go(x,y,index,slots):
if (y - index) == 1:
slots[index] = x
print slots
slots[index] = 0
return
for i in range(x+1):
sl... | 3 | 2009-03-05T20:09:17Z | 616,432 | <p>Everything we think of as recursion can also be thought of as a stack-based problem, where the recursive function just uses the program's call stack rather than creating a separate stack. That means any recursive function can be re-written using a stack instead. </p>
<p>I don't know python well enough to give you... | 21 | 2009-03-05T20:12:19Z | [
"python",
"recursion"
] |
Is it possible to remove recursion from this function? | 616,416 | <p>I have been playing with this a while, and just cannot see an obvious solution. I want to remove the recursion from the XinY_Go function.</p>
<pre><code>def XinY_Go(x,y,index,slots):
if (y - index) == 1:
slots[index] = x
print slots
slots[index] = 0
return
for i in range(x+1):
sl... | 3 | 2009-03-05T20:09:17Z | 616,455 | <p>Look at this code for creating all permutations, I guess I'd be relatively simple to implement something similar for your problem.</p>
<p><a href="http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python">How to generate all permutations of a list in python?</a></p>
| 2 | 2009-03-05T20:20:19Z | [
"python",
"recursion"
] |
Is it possible to remove recursion from this function? | 616,416 | <p>I have been playing with this a while, and just cannot see an obvious solution. I want to remove the recursion from the XinY_Go function.</p>
<pre><code>def XinY_Go(x,y,index,slots):
if (y - index) == 1:
slots[index] = x
print slots
slots[index] = 0
return
for i in range(x+1):
sl... | 3 | 2009-03-05T20:09:17Z | 616,666 | <p>A naive implementation of <a href="http://stackoverflow.com/questions/616416/is-it-possible-to-remove-recursion-from-this-function/616432#616432">@Joel Coehoorn's suggestion</a> follows:</p>
<pre><code>def XinY_Stack(x, y):
stack = [(x, 0, [0]*y)]
while stack:
x, index, slots = stack.pop()
i... | 14 | 2009-03-05T21:19:22Z | [
"python",
"recursion"
] |
Python in tcsh | 616,440 | <p>I don't have much experience with tcsh, but I'm interested in learning. I've been having issues getting Python to see PYTHONPATH. I can echo $PYTHONPATH, and it is correct, but when I start up Python, my paths do not show up in sys.path. Any ideas?</p>
<p>EDIT:</p>
<pre><code>[dmcdonal@tg-steele ~]$ echo $PYTHONPA... | 5 | 2009-03-05T20:16:30Z | 616,476 | <p>Make sure that you're not starting python with the <code>-E</code> option (which is: ignore environment variables). If you start python via a shell script or some other app, just double check it doesn't add anything.</p>
<p>Since the list of sys.path is long, it can be hard to miss your paths. PYTHONPATH stuff norm... | 1 | 2009-03-05T20:26:27Z | [
"python",
"tcsh"
] |
Python in tcsh | 616,440 | <p>I don't have much experience with tcsh, but I'm interested in learning. I've been having issues getting Python to see PYTHONPATH. I can echo $PYTHONPATH, and it is correct, but when I start up Python, my paths do not show up in sys.path. Any ideas?</p>
<p>EDIT:</p>
<pre><code>[dmcdonal@tg-steele ~]$ echo $PYTHONPA... | 5 | 2009-03-05T20:16:30Z | 616,530 | <p>Check:</p>
<ol>
<li>PYTHONPATH is in os.environ,</li>
<li>and set to the correct value of a colon separated list of paths.</li>
</ol>
<p>If it is, and you can confirm that your paths are not in sys.path, you have found a bug.</p>
<p>If it is not in os.environ, your environment is not passing through to Python (pr... | 0 | 2009-03-05T20:43:58Z | [
"python",
"tcsh"
] |
Python in tcsh | 616,440 | <p>I don't have much experience with tcsh, but I'm interested in learning. I've been having issues getting Python to see PYTHONPATH. I can echo $PYTHONPATH, and it is correct, but when I start up Python, my paths do not show up in sys.path. Any ideas?</p>
<p>EDIT:</p>
<pre><code>[dmcdonal@tg-steele ~]$ echo $PYTHONPA... | 5 | 2009-03-05T20:16:30Z | 616,558 | <p>How are you setting PYTHONPATH? You might be confusing tcsh's set vs. setenv. Use "set" to set what tcsh calls <em>shell variables</em> and use "setenv" to set <em>environment variables</em>. So, you need to use setenv in order for Python to see it. For example:</p>
<pre><code>$ set FOO='bar'
$ echo $FOO
bar
$ ... | 9 | 2009-03-05T20:54:26Z | [
"python",
"tcsh"
] |
Python in tcsh | 616,440 | <p>I don't have much experience with tcsh, but I'm interested in learning. I've been having issues getting Python to see PYTHONPATH. I can echo $PYTHONPATH, and it is correct, but when I start up Python, my paths do not show up in sys.path. Any ideas?</p>
<p>EDIT:</p>
<pre><code>[dmcdonal@tg-steele ~]$ echo $PYTHONPA... | 5 | 2009-03-05T20:16:30Z | 4,744,590 | <p>I also have the same issue even I set environment <code>PYTHONPATH</code> correctly in <code>.cshrc</code> when I used Python <code>2.5.1</code>, when I switch the to Python <code>2.6.2</code>, It works fine. Looks like it is a python bug.</p>
| 0 | 2011-01-20T07:45:11Z | [
"python",
"tcsh"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 616,672 | <p><strong>The <code>print</code> statement will call the <code>write()</code> method of any object you assign to sys.stdout.</strong> </p>
<p>I would spin up a small class to write to two places at once...</p>
<pre><code>import sys
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
... | 57 | 2009-03-05T21:21:21Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 616,686 | <p>I had this same issue before and found this snippet very useful:</p>
<pre><code>class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
... | 102 | 2009-03-05T21:24:48Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 616,709 | <p>What you really want is <code>logging</code> module from standard library. Create a logger and attach two handlers, one would be writing to a file and the other to stdout or stderr.</p>
<p>See <a href="http://docs.python.org/library/logging.html#logging-to-multiple-destinations">Logging to multiple destinations</... | 52 | 2009-03-05T21:31:47Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 648,322 | <p>(Ah, just re-read your question and see that this doesn't quite apply.)</p>
<p>Here is a sample program that makes uses the <a href="http://docs.python.org/library/logging.html">python logging module</a>. This logging module has been in all versions since 2.3. In this sample the logging is configurable by command... | 8 | 2009-03-15T18:49:54Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 651,718 | <p>Since you're comfortable spawning external processes from your code, you could use <code>tee</code> itself. I don't know of any Unix system calls that do exactly what <code>tee</code> does.</p>
<pre><code>import subprocess, os, sys
# Unbuffer output
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
tee = subpr... | 30 | 2009-03-16T19:00:09Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 2,216,517 | <p>As described elsewhere, perhaps the best solution is to use the logging module directly:</p>
<pre><code>import logging
logging.basicConfig(level=logging.DEBUG, filename='mylog.log')
logging.info('this should to write to the log file')
</code></pre>
<p>However, there are some (rare) occasions where you <em>really ... | 6 | 2010-02-07T09:20:32Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 2,841,525 | <p>another solution using logging module:</p>
<pre><code>import logging
import sys
log = logging.getLogger('stdxxx')
class StreamLogger(object):
def __init__(self, stream, prefix=''):
self.stream = stream
self.prefix = prefix
self.data = ''
def write(self, data):
self.stream... | 2 | 2010-05-15T19:46:49Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 3,423,392 | <p>I wrote a <code>tee()</code> implementation in Python that should work for most cases, and it works on Windows also.</p>
<p><a href="https://github.com/pycontribs/tendo" rel="nofollow">https://github.com/pycontribs/tendo</a></p>
<p>Also, you can use it in combination with <code>logging</code> module from Python if... | 9 | 2010-08-06T11:39:55Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 3,936,968 | <p>I'm writing a script to run cmd-line scripts. ( Because in some cases, there just is no viable substitute for a Linux command -- such as the case of rsync. )</p>
<p>What I really wanted was to use the default python logging mechanism in every case where it was possible to do so, but to still capture any error when... | 0 | 2010-10-14T19:51:00Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 7,206,730 | <p>None of the answers above really seems to answer the problem posed. I know this is an old thread, but I think this problem is a lot simpler than everyone is making it:</p>
<pre><code>class tee_err(object):
def __init__(self):
self.errout = sys.stderr
sys.stderr = self
self.log = 'logfile.log'
l... | 1 | 2011-08-26T15:04:46Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 16,551,730 | <p>Here is another solution, which is more general than the others -- it supports splitting output (written to <code>sys.stdout</code>) to any number of file-like objects. There's no requirement that <code>__stdout__</code> itself is included.</p>
<pre><code>import sys
class multifile(object):
def __init__(self, ... | 6 | 2013-05-14T19:52:43Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 17,942,748 | <p>To complete John T answer: <a href="http://stackoverflow.com/a/616686/395687">http://stackoverflow.com/a/616686/395687</a></p>
<p>I added an <strong>enter</strong> and <strong>exit</strong> method to use it as a context manager with the 'with' keyword, which gives this code</p>
<pre><code>class Tee(object):
de... | 4 | 2013-07-30T09:07:29Z | [
"python",
"tee"
] |
How do I duplicate sys.stdout to a log file in python? | 616,645 | <p>Edit: Since it appears that there's either no solution, or I'm doing something so non-standard that nobody knows - I'll revise my question to also ask: What is the best way to accomplish logging when a python app is making a lot of system calls?</p>
<p>My app has two modes. In interactive mode, I want all output t... | 105 | 2009-03-05T21:12:13Z | 24,583,265 | <p>I know this question has been answered repeatedly, but for this I've taken the main answer from <a href="http://stackoverflow.com/a/616686">John T's</a> answer and modified it so it contains the suggested flush and followed its linked revised version. I've also added the enter and exit as mentioned in <a href="http:... | 2 | 2014-07-05T05:24:52Z | [
"python",
"tee"
] |
Iterative version of Python's deepcopy | 617,097 | <p>Is there an existing implementation of an iterative version of deepcopy for Python 2.5.2? The deepcopy method available from the copy module is recursive and fails on large trees. I am not in a position where we can safely increase the stack limit at runtime.</p>
<p>EDIT</p>
<p>I did track this down:</p>
<p><a hr... | 4 | 2009-03-05T23:07:10Z | 617,113 | <p>I'm not sure this would make sense. Isn't the whole point of <code>deepcopy</code> that each object will ask its children to copy themselves?</p>
<p>Presumably you know exactly what kind of object you're copying and what its children look like as well, so if I were you, I'd just write my own function to copy it. Sh... | 1 | 2009-03-05T23:11:45Z | [
"python",
"recursion",
"iteration"
] |
Iterative version of Python's deepcopy | 617,097 | <p>Is there an existing implementation of an iterative version of deepcopy for Python 2.5.2? The deepcopy method available from the copy module is recursive and fails on large trees. I am not in a position where we can safely increase the stack limit at runtime.</p>
<p>EDIT</p>
<p>I did track this down:</p>
<p><a hr... | 4 | 2009-03-05T23:07:10Z | 617,394 | <p>Maybe it would work as such with <a href="http://en.wikipedia.org/wiki/Stackless%5FPython" rel="nofollow">Stackless Python</a></p>
| 0 | 2009-03-06T01:08:35Z | [
"python",
"recursion",
"iteration"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 617,215 | <p><a href="http://www.askdavetaylor.com/detect%5Fapple%5Fiphone%5Fuser%5Fweb%5Fsite%5Fserver.html" rel="nofollow">This article</a> outlines a few ways of detecting an iPhone through by checking the HTTP_USER_AGENT agent variable. Depending on where you want to do the check at (HTML level, Javascript, CSS, etc.), I'm s... | 2 | 2009-03-05T23:53:04Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 617,216 | <p>Check the user agent. It will be</p>
<blockquote>
<p>Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 </p>
</blockquote>
<p>I'm not sure how to do this with appengine, but the equivalent PHP code can be found here: <a href="http://www.matt... | 0 | 2009-03-05T23:53:29Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 618,041 | <p>Here's how to do implement it as middleware in Django, assuming that's what you're using on appengine. </p>
<pre><code>class DetectiPhone(object):
def process_request(self, request):
if 'HTTP_USER_AGENT' in request.META and request.META['HTTP_USER_AGENT'].find('(iPhone') >= 0:
request.MET... | 1 | 2009-03-06T08:17:57Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 618,051 | <p>if you're using the standard webapp framework the user agent will be in the request instance. This should be good enough:</p>
<pre><code> if "iPhone" in request.headers["User-Agent"]:
# do iPhone logic
</code></pre>
| 1 | 2009-03-06T08:25:39Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 618,062 | <p>The <a href="http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/OptimizingforSafarioniPhone/chapter%5F3%5Fsection%5F3.html" rel="nofollow">Using the Safari on iPhone User Agent String</a> article on the apple website indicate the different user agents for iPhone and ... | 2 | 2009-03-06T08:31:02Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 721,243 | <pre><code>import os
class MainPage(webapp.RequestHandler):
@login_required
def get(self):
userAgent = os.environ['HTTP_USER_AGENT']
if userAgent.find('iPhone') > 0:
self.response.out.write('iPhone support is coming soon...')
else:
self.response.out.write('Hey... you are not from iPhone.... | 0 | 2009-04-06T12:39:39Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
How do I detect if my appengine app is being accessed by an iphone/ipod touch? | 617,202 | <p>I need to render the page differently if it's acessed by an iphone/ipod touch. I suppose the information is in the request object, but what would be the syntax?</p>
| 11 | 2009-03-05T23:46:26Z | 738,766 | <p>This is the syntax I was looking for, works with iphone and ipod touch:</p>
<pre><code>uastring = self.request.headers.get('user_agent')
if "Mobile" in uastring and "Safari" in uastring:
# do iphone / ipod stuff
</code></pre>
| 7 | 2009-04-10T20:27:02Z | [
"iphone",
"python",
"google-app-engine",
"web-applications"
] |
Python Popen, closing streams and multiple processes | 617,308 | <p>I have some data that I would like to gzip, uuencode and then print to standard out. What I basically have is:</p>
<pre><code>compressor = Popen("gzip", stdin = subprocess.PIPE, stdout = subprocess.PIPE)
encoder = Popen(["uuencode", "dummy"], stdin = compressor.stdout)
</code></pre>
<p>The way I feed data to t... | 2 | 2009-03-06T00:30:08Z | 617,316 | <p>if you just want to compress and don't need the file wrappers consider using the zlib module</p>
<pre><code>import zlib
compressed = zlib.compress("text")
</code></pre>
<p>any reason why the shell=True and unix pipes suggestions won't work?</p>
<pre><code>from subprocess import *
pipes = Popen("gzip | uuencode d... | 1 | 2009-03-06T00:34:00Z | [
"python",
"stream",
"popen",
"eof"
] |
Python Popen, closing streams and multiple processes | 617,308 | <p>I have some data that I would like to gzip, uuencode and then print to standard out. What I basically have is:</p>
<pre><code>compressor = Popen("gzip", stdin = subprocess.PIPE, stdout = subprocess.PIPE)
encoder = Popen(["uuencode", "dummy"], stdin = compressor.stdout)
</code></pre>
<p>The way I feed data to t... | 2 | 2009-03-06T00:30:08Z | 617,346 | <p>This is not the sort of thing you should be doing directly in python, there are eccentricities regarding the how thing work that make it a much better idea to do this with a shell. If you can just use subprocess.Popen("foo | bar", shell=True), then all the better.</p>
<p>What might be happening is that gzip has not... | 3 | 2009-03-06T00:49:17Z | [
"python",
"stream",
"popen",
"eof"
] |
Python Popen, closing streams and multiple processes | 617,308 | <p>I have some data that I would like to gzip, uuencode and then print to standard out. What I basically have is:</p>
<pre><code>compressor = Popen("gzip", stdin = subprocess.PIPE, stdout = subprocess.PIPE)
encoder = Popen(["uuencode", "dummy"], stdin = compressor.stdout)
</code></pre>
<p>The way I feed data to t... | 2 | 2009-03-06T00:30:08Z | 617,451 | <p>I suspect the issue is with the order in which you open the pipes. UUEncode is funny is that it will whine when you launch it if there's no incoming pipe in just the right way (try launching the darn thing on it's own in a Popen call to see the explosion with just PIPE as the stdin and stdout)</p>
<p>Try this:</p>
... | 4 | 2009-03-06T01:37:43Z | [
"python",
"stream",
"popen",
"eof"
] |
Poplib not working correctly? | 617,892 | <p>I want to get all the messages from my gmail inbox, but I am facing 2 problems.</p>
<ol>
<li>It does not get all the emails, (as per the count in stat function)</li>
<li>The order of emails it get is random.</li>
</ol>
<p>I am unsure if its the problem with poplib or gmail pop server.</p>
<p>What am I missing her... | 0 | 2009-03-06T06:55:18Z | 618,688 | <p>Why don't you try to use <a href="http://libgmail.sourceforge.net/" rel="nofollow">libgmail</a>?</p>
| 0 | 2009-03-06T12:28:55Z | [
"python",
"python-2.5",
"poplib"
] |
Poplib not working correctly? | 617,892 | <p>I want to get all the messages from my gmail inbox, but I am facing 2 problems.</p>
<ol>
<li>It does not get all the emails, (as per the count in stat function)</li>
<li>The order of emails it get is random.</li>
</ol>
<p>I am unsure if its the problem with poplib or gmail pop server.</p>
<p>What am I missing her... | 0 | 2009-03-06T06:55:18Z | 628,089 | <p>What does your code look like? Using poplib, you're able to decide on the order and number of the messages downloaded. The code from the <a href="http://docs.python.org/library/poplib.html#id2" rel="nofollow">poplib documentation</a> should work:</p>
<pre><code>import getpass, poplib
M = poplib.POP3('localhost')
M... | 1 | 2009-03-09T21:19:39Z | [
"python",
"python-2.5",
"poplib"
] |
Poplib not working correctly? | 617,892 | <p>I want to get all the messages from my gmail inbox, but I am facing 2 problems.</p>
<ol>
<li>It does not get all the emails, (as per the count in stat function)</li>
<li>The order of emails it get is random.</li>
</ol>
<p>I am unsure if its the problem with poplib or gmail pop server.</p>
<p>What am I missing her... | 0 | 2009-03-06T06:55:18Z | 628,130 | <p>You can also try <code>imaplib</code> module since GMail also provides access to email via IMAP protocol.</p>
| 1 | 2009-03-09T21:39:14Z | [
"python",
"python-2.5",
"poplib"
] |
Poplib not working correctly? | 617,892 | <p>I want to get all the messages from my gmail inbox, but I am facing 2 problems.</p>
<ol>
<li>It does not get all the emails, (as per the count in stat function)</li>
<li>The order of emails it get is random.</li>
</ol>
<p>I am unsure if its the problem with poplib or gmail pop server.</p>
<p>What am I missing her... | 0 | 2009-03-06T06:55:18Z | 6,231,079 | <p>It's the problem of gmail: <a href="https://mail.google.com/support/bin/answer.py?answer=13291" rel="nofollow">https://mail.google.com/support/bin/answer.py?answer=13291</a></p>
<p>Try to use recent:username@gmail.com as your email address. At least you'll have all your last month mail in correct order.</p>
| 0 | 2011-06-03T18:20:53Z | [
"python",
"python-2.5",
"poplib"
] |
How do I make this python command line an alias in bash? | 617,952 | <p>I want a quick an easy way to check my ip address after reading a recent question that had been answered on SO. For future reference, is there a way to make the following alias work?</p>
<pre><code>alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'
</code>... | 1 | 2009-03-06T07:27:36Z | 617,966 | <p>Quote the inside double-quotes:</p>
<pre><code>alias myip='python -c "from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]"'
</code></pre>
| 6 | 2009-03-06T07:34:21Z | [
"python",
"bash",
"alias"
] |
How do I make this python command line an alias in bash? | 617,952 | <p>I want a quick an easy way to check my ip address after reading a recent question that had been answered on SO. For future reference, is there a way to make the following alias work?</p>
<pre><code>alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'
</code>... | 1 | 2009-03-06T07:27:36Z | 617,968 | <pre><code>alias myip="python -c 'from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]'"
</code></pre>
<p>You need to use single quotes inside the alias to stop bash trying to interpret parts of your code inside them. The escapes on the double quotes get stripped out while processing... | 7 | 2009-03-06T07:34:49Z | [
"python",
"bash",
"alias"
] |
How do I make this python command line an alias in bash? | 617,952 | <p>I want a quick an easy way to check my ip address after reading a recent question that had been answered on SO. For future reference, is there a way to make the following alias work?</p>
<pre><code>alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'
</code>... | 1 | 2009-03-06T07:27:36Z | 618,043 | <p>could also be done with curl:</p>
<pre><code>alias myip='curl "http://whatismyip.appjet.net"'
</code></pre>
<p>or using wget:</p>
<pre><code>alias myip='wget -O - "http://whatismyip.appjet.net" 2>/dev/null'
</code></pre>
| 5 | 2009-03-06T08:19:46Z | [
"python",
"bash",
"alias"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 618,099 | <pre><code>print 'yes' if 0 < x < 0.5 else 'no'
</code></pre>
<p><a href="http://docs.python.org/library/functions.html#range"><code>range()</code></a> is for generating arrays of consecutive integers</p>
| 23 | 2009-03-06T08:51:47Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 618,100 | <p>No, you can't do that. <code>range()</code> expects integer arguments. If you want to know if <code>x</code> is inside this range try some form of this:</p>
<pre><code>print 0.0 <= x <= 0.5
</code></pre>
<p>Be careful with your upper limit. If you use <code>range()</code> it is excluded (<code>range(0, 5)</c... | 59 | 2009-03-06T08:53:15Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 618,106 | <pre><code>>>> s = 1.1
>>> 0<= s <=0.2
False
>>> 0<= s <=1.2
True
</code></pre>
| 3 | 2009-03-06T08:54:56Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 618,147 | <p>To check whether some number n is in the inclusive range denoted by the two number a and b you do either </p>
<pre><code>if a <= n <= b:
print "yes"
else:
print "no"
</code></pre>
<p>use the replace <code>>=</code> and <code><=</code> with <code>></code> and <code><</code> to check whet... | 2 | 2009-03-06T09:12:17Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 618,560 | <p>Old faithful:</p>
<pre><code>if n >= a and n <= b:
</code></pre>
<p>And it doesn't look like Perl (joke)</p>
| 3 | 2009-03-06T11:40:55Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 1,278,055 | <p>I would use the numpy library, which would allow you to do this for a list of numbers as well:</p>
<pre><code>from numpy import array
a = array([1, 2, 3, 4, 5, 6,])
a[a < 2]
</code></pre>
| 3 | 2009-08-14T14:06:52Z | [
"python",
"range"
] |
How to find whether a number belongs to a particular range in Python? | 618,093 | <p>Suppose I want to check if <code>x</code> belongs to range 0 to 0.5. How can I do it?</p>
| 30 | 2009-03-06T08:49:08Z | 28,989,561 | <pre class="lang-python prettyprint-override"><code>if num in range(min, max):
"""do stuff..."""
else:
"""do other stuff..."""
</code></pre>
| 0 | 2015-03-11T14:39:42Z | [
"python",
"range"
] |
Prevent wxPython from showing 'Unhandled exception' dialog | 618,429 | <p>I have complex GUI application written in Python and wxPython.</p>
<p>I want it to be certified for Windows Vista, so it has to crash in a way that causes Windows Error Reporting dialog (The one that asks "<em>Do you want to send report to Microsoft?</em>") to appear. This is relevant to test case no 32 from "<em>C... | 1 | 2009-03-06T10:51:27Z | 618,469 | <p>Try <a href="http://wiki.python.org/moin/CrashingPython" rel="nofollow">http://wiki.python.org/moin/CrashingPython</a></p>
| 0 | 2009-03-06T11:11:12Z | [
"python",
"windows",
"wxpython",
"error-reporting"
] |
Prevent wxPython from showing 'Unhandled exception' dialog | 618,429 | <p>I have complex GUI application written in Python and wxPython.</p>
<p>I want it to be certified for Windows Vista, so it has to crash in a way that causes Windows Error Reporting dialog (The one that asks "<em>Do you want to send report to Microsoft?</em>") to appear. This is relevant to test case no 32 from "<em>C... | 1 | 2009-03-06T10:51:27Z | 618,500 | <p>If I remember correctly, this is a catch(...) at top level (wxApp) in wxWidgets. You can either use a vectored Exception Handler or _set_se_translator() to get a first shot at the Structured Exception, and exit to WER, i.e. ReportFault() from there.</p>
| 1 | 2009-03-06T11:19:15Z | [
"python",
"windows",
"wxpython",
"error-reporting"
] |
Prevent wxPython from showing 'Unhandled exception' dialog | 618,429 | <p>I have complex GUI application written in Python and wxPython.</p>
<p>I want it to be certified for Windows Vista, so it has to crash in a way that causes Windows Error Reporting dialog (The one that asks "<em>Do you want to send report to Microsoft?</em>") to appear. This is relevant to test case no 32 from "<em>C... | 1 | 2009-03-06T10:51:27Z | 624,644 | <p>Is it possible for you to just handle everything? You would have to, I guess, put a try:except: block around every method bound to a widget. You could write a decorator:</p>
<pre><code>def catch_exception(f):
def safe(*args, **kw):
try:
f(*args, **kw)
except Exception, e:
... | 1 | 2009-03-09T00:48:15Z | [
"python",
"windows",
"wxpython",
"error-reporting"
] |
Prevent wxPython from showing 'Unhandled exception' dialog | 618,429 | <p>I have complex GUI application written in Python and wxPython.</p>
<p>I want it to be certified for Windows Vista, so it has to crash in a way that causes Windows Error Reporting dialog (The one that asks "<em>Do you want to send report to Microsoft?</em>") to appear. This is relevant to test case no 32 from "<em>C... | 1 | 2009-03-06T10:51:27Z | 2,537,311 | <p>It all ended up with compiling my own wxWidgets and wxPython, with just one compilation flag changed: <strong>wxUSE_EXCEPTIONS</strong> should be set to 0.</p>
<p>Robin Dunn wrote that he will try to patch wxPython, so this behaviour could be modified without recompiling of the whole library.</p>
| 2 | 2010-03-29T10:53:30Z | [
"python",
"windows",
"wxpython",
"error-reporting"
] |
Python regex split a string by one of two delimiters | 618,551 | <p>I wanted to cut up a string of email addresses which may be separated by any combination of commas and white-space.</p>
<p>And I thought it would be pretty straight-forward :</p>
<pre><code>sep = re.compile('(\s*,*)+')
print sep.split("""a@b.com, c@d.com
e@f.com,,g@h.com""")
</code></pre>
<p>But it isn't. I c... | 7 | 2009-03-06T11:36:13Z | 618,562 | <p>Doh!</p>
<p>It's just this.</p>
<pre><code>sep = re.compile('[\s,]+')
</code></pre>
| 13 | 2009-03-06T11:42:11Z | [
"python",
"regex"
] |
Python regex split a string by one of two delimiters | 618,551 | <p>I wanted to cut up a string of email addresses which may be separated by any combination of commas and white-space.</p>
<p>And I thought it would be pretty straight-forward :</p>
<pre><code>sep = re.compile('(\s*,*)+')
print sep.split("""a@b.com, c@d.com
e@f.com,,g@h.com""")
</code></pre>
<p>But it isn't. I c... | 7 | 2009-03-06T11:36:13Z | 618,564 | <p>I like the following...</p>
<pre><code>>>> sep= re.compile( r',*\s*' )
>>> sep.split("""a@b.com, c@d.com
e@f.com,,g@h.com""")
['a@b.com', 'c@d.com', 'e@f.com', 'g@h.com']
</code></pre>
<p>Which also seems to work on your test data.</p>
| 2 | 2009-03-06T11:44:11Z | [
"python",
"regex"
] |
Python regex split a string by one of two delimiters | 618,551 | <p>I wanted to cut up a string of email addresses which may be separated by any combination of commas and white-space.</p>
<p>And I thought it would be pretty straight-forward :</p>
<pre><code>sep = re.compile('(\s*,*)+')
print sep.split("""a@b.com, c@d.com
e@f.com,,g@h.com""")
</code></pre>
<p>But it isn't. I c... | 7 | 2009-03-06T11:36:13Z | 618,669 | <p>without re</p>
<pre><code>line = 'e@d , f@g, 7@g'
addresses = line.split(',')
addresses = [ address.strip() for address in addresses ]
</code></pre>
| 3 | 2009-03-06T12:23:44Z | [
"python",
"regex"
] |
Django: using <select multiple> and POST | 618,557 | <p>I'm using something like this in my template</p>
<pre><code><select multiple="multiple" name="services" id="services" size="5">
{% for service in services %}
<option value="{{service.id}}">{{service}}</option>
{% endfor %}
</select>
</code></pre>
<p>When I view the POST data i... | 34 | 2009-03-06T11:39:14Z | 618,626 | <pre><code>request.POST.getlist('services')
</code></pre>
| 86 | 2009-03-06T12:06:52Z | [
"python",
"django",
"http"
] |
Django: using <select multiple> and POST | 618,557 | <p>I'm using something like this in my template</p>
<pre><code><select multiple="multiple" name="services" id="services" size="5">
{% for service in services %}
<option value="{{service.id}}">{{service}}</option>
{% endfor %}
</select>
</code></pre>
<p>When I view the POST data i... | 34 | 2009-03-06T11:39:14Z | 6,587,210 | <p>Just FYI, I had to use:</p>
<p>list = <strong>request.POST.getlist('items[ ]')</strong></p>
<p>because omitting the [] caused a blank list to be returned instead of the correct values. I'm using jQuery to fetch the values of a multiple select element, and jQuery appears to be adding the [] </p>
| 6 | 2011-07-05T18:31:45Z | [
"python",
"django",
"http"
] |
Django: using <select multiple> and POST | 618,557 | <p>I'm using something like this in my template</p>
<pre><code><select multiple="multiple" name="services" id="services" size="5">
{% for service in services %}
<option value="{{service.id}}">{{service}}</option>
{% endfor %}
</select>
</code></pre>
<p>When I view the POST data i... | 34 | 2009-03-06T11:39:14Z | 13,742,103 | <p>Watch out! getlist method from QueryDict returns an empty list if the key doesn't exist. It does not throw an exception. <a href="http://bit.ly/MdgrUH" rel="nofollow">http://bit.ly/MdgrUH</a></p>
| 1 | 2012-12-06T10:56:04Z | [
"python",
"django",
"http"
] |
Optimizing Jinja2 Environment creation | 618,827 | <p>My application is running on Google App Engine and most of requests constantly gets yellow flag due to high CPU usage. Using profiler I tracked the issue down to the routine of creating <code>jinja2.Environment</code> instance.</p>
<p>I'm creating the instance at module level:</p>
<pre><code>from jinja2 import Env... | 8 | 2009-03-06T13:19:58Z | 624,222 | <p>OK, people, this is what I got today on #pocoo:</p>
<p>[20:59] zgoda: hello, i'd like to know if i could optimize my jinja2 environment creation process, the problem -> <a href="http://stackoverflow.com/questions/618827/optimizing-jinja2-environment-creation">http://stackoverflow.com/questions/618827/optimizing-jin... | 4 | 2009-03-08T20:20:49Z | [
"python",
"google-app-engine",
"jinja2"
] |
Optimizing Jinja2 Environment creation | 618,827 | <p>My application is running on Google App Engine and most of requests constantly gets yellow flag due to high CPU usage. Using profiler I tracked the issue down to the routine of creating <code>jinja2.Environment</code> instance.</p>
<p>I'm creating the instance at module level:</p>
<pre><code>from jinja2 import Env... | 8 | 2009-03-06T13:19:58Z | 806,902 | <p>According to this <a href="http://appengine-cookbook.appspot.com/recipe/better-performance-with-jinja2/?id=ahJhcHBlbmdpbmUtY29va2Jvb2tyqwELEgtSZWNpcGVJbmRleCJGYWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUhnc1NDRU5oZEdWbmIzSjVJaEJYWldKaGNIQWdSbkpoYldWM2IzSnJEQQwLEgZSZWNpcGUiSGFoSmhjSEJsYm1kcGJtVXRZMjl2YTJKdmIydHlIZ3NTQ0VOaG... | 2 | 2009-04-30T13:29:51Z | [
"python",
"google-app-engine",
"jinja2"
] |
Optimizing Jinja2 Environment creation | 618,827 | <p>My application is running on Google App Engine and most of requests constantly gets yellow flag due to high CPU usage. Using profiler I tracked the issue down to the routine of creating <code>jinja2.Environment</code> instance.</p>
<p>I'm creating the instance at module level:</p>
<pre><code>from jinja2 import Env... | 8 | 2009-03-06T13:19:58Z | 1,020,013 | <p>Armin suggested to pre-compile Jinja2 templates to python code, and use the compiled templates in production. So I've made a compiler/loader for that, and it now renders some complex templates 13 times faster, throwing away <strong>all</strong> the parsing overhead. The related discussion with link to the repository... | 8 | 2009-06-19T21:02:24Z | [
"python",
"google-app-engine",
"jinja2"
] |
dateutil.rrule.rrule.between() gives only dates after now | 618,910 | <p>From the IPython console:</p>
<pre><code>In [16]: b
Out[16]: datetime.datetime(2008, 3, 1, 0, 0)
In [17]: e
Out[17]: datetime.datetime(2010, 5, 2, 0, 0)
In [18]: rrule(MONTHLY).between(b, e, inc=True)
Out[18]:
[datetime.datetime(2009, 3, 6, 14, 42, 1),
datetime.datetime(2009, 4, 6, 14, 42, 1),
datetime.datetime... | 4 | 2009-03-06T13:48:08Z | 618,939 | <p>You need to pass <code>b</code> into rrule, like this:</p>
<pre><code>rrule(MONTHLY, dtstart = b).between(b, e, inc=True)
</code></pre>
<p>From these docs (<a href="http://labix.org/python-dateutil">http://labix.org/python-dateutil</a>), it looks like calling rrule without specifying dtstart will use datetime.date... | 10 | 2009-03-06T13:58:08Z | [
"python",
"python-dateutil",
"rrule"
] |
Python build/release system | 618,958 | <p>I started using Pyant recenently to do various build/release tasks but have recently discovered that development for this project has ended.</p>
<p>I did some research and can't seem to find any other Python build scripts that are comparable. Just wondering if anyone can recommend one? I basically need it to do wha... | 4 | 2009-03-06T14:04:56Z | 619,012 | <p>what about maven? (<a href="http://maven.apache.org/" rel="nofollow">http://maven.apache.org/</a>) With the right plugins it can do much more then ant, it can even use ant for building if you configure it so.</p>
<p>It's very flexible and supports the full product life cycle. I really recommend you take a look at i... | 0 | 2009-03-06T14:21:34Z | [
"python",
"ant",
"build-automation"
] |
Python build/release system | 618,958 | <p>I started using Pyant recenently to do various build/release tasks but have recently discovered that development for this project has ended.</p>
<p>I did some research and can't seem to find any other Python build scripts that are comparable. Just wondering if anyone can recommend one? I basically need it to do wha... | 4 | 2009-03-06T14:04:56Z | 619,035 | <p>Probably the best answer is to use Ant as-is... that is, use the Java version. My second suggestion would be to use <a href="http://www.scons.org/" rel="nofollow">scons</a>. It won't take much time using scons before you're asking, "Who ever thought of using XML to script a build?"</p>
| 5 | 2009-03-06T14:31:09Z | [
"python",
"ant",
"build-automation"
] |
Python build/release system | 618,958 | <p>I started using Pyant recenently to do various build/release tasks but have recently discovered that development for this project has ended.</p>
<p>I did some research and can't seem to find any other Python build scripts that are comparable. Just wondering if anyone can recommend one? I basically need it to do wha... | 4 | 2009-03-06T14:04:56Z | 619,060 | <p>Its not completely comparable but I tend to use <a href="http://www.nongnu.org/fab/" rel="nofollow">fabric</a>. Its more geared towards deployment with support for ssh to production host and runing things as root there etc.</p>
| 2 | 2009-03-06T14:36:23Z | [
"python",
"ant",
"build-automation"
] |
Python build/release system | 618,958 | <p>I started using Pyant recenently to do various build/release tasks but have recently discovered that development for this project has ended.</p>
<p>I did some research and can't seem to find any other Python build scripts that are comparable. Just wondering if anyone can recommend one? I basically need it to do wha... | 4 | 2009-03-06T14:04:56Z | 619,209 | <p>Some people use <a href="http://www.blueskyonmars.com/projects/paver/" rel="nofollow">Paver</a> for build/deployment of Python packages. While I know it works, it does not appeal to me that much.</p>
| 2 | 2009-03-06T15:20:33Z | [
"python",
"ant",
"build-automation"
] |
Python metaclasses | 618,960 | <p>I've been hacking classes in Python like this:</p>
<pre><code>def hack(f,aClass) :
class MyClass(aClass) :
def f(self) :
f()
return MyClass
A = hack(afunc,A)
</code></pre>
<p>Which looks pretty clean to me. It takes a class, A, creates a new class derived from it that has an extra method, calling ... | 6 | 2009-03-06T14:06:27Z | 619,025 | <p>The definition of a class in Python is an instance of type (or an instance of a subclass of type). In other words, the class definition itself is an object. With metaclasses, you have the ability to control the type instance that becomes the class definition.</p>
<p>When a metaclass is invoked, you have the ability... | 5 | 2009-03-06T14:27:36Z | [
"python",
"metaclass"
] |
Python metaclasses | 618,960 | <p>I've been hacking classes in Python like this:</p>
<pre><code>def hack(f,aClass) :
class MyClass(aClass) :
def f(self) :
f()
return MyClass
A = hack(afunc,A)
</code></pre>
<p>Which looks pretty clean to me. It takes a class, A, creates a new class derived from it that has an extra method, calling ... | 6 | 2009-03-06T14:06:27Z | 815,963 | <p>A metaclass is the class of a class. IMO, the bloke here covered it quite serviceably, including some use-cases. See Stack Overflow question <em><a href="http://stackoverflow.com/questions/395982/metaclass-new-cls-and-super-can-someone-explain-the-mechanism-exactly/396109#396109">"MetaClass", "<strong>new</strong>",... | 1 | 2009-05-03T00:09:30Z | [
"python",
"metaclass"
] |
Python metaclasses | 618,960 | <p>I've been hacking classes in Python like this:</p>
<pre><code>def hack(f,aClass) :
class MyClass(aClass) :
def f(self) :
f()
return MyClass
A = hack(afunc,A)
</code></pre>
<p>Which looks pretty clean to me. It takes a class, A, creates a new class derived from it that has an extra method, calling ... | 6 | 2009-03-06T14:06:27Z | 914,283 | <p>You can use the <code>type</code> callable as well.</p>
<pre><code>def hack(f, aClass):
newfunc = lambda self: f()
return type('MyClass', (aClass,), {'f': newfunc})
</code></pre>
<p>I find using <code>type</code> the easiest way to get into the metaclass world.</p>
| 1 | 2009-05-27T06:40:19Z | [
"python",
"metaclass"
] |
Set the size of wx.GridBagSizer dynamically | 619,163 | <p>I'm creating an app where I drag button widgets into a panel. I would like to have a visible grid in the panel where i drop the widgets so the widgets will be aligned to the grid.</p>
<p>I guess it isn't hard making a grid where the squares are 15x15 pixels using a GridBagSizer(since the widgets will span between m... | 1 | 2009-03-06T15:05:34Z | 619,375 | <p>Don't use a sizer at all for this. Just position the buttons yourself, with whatever co-ordinate rounding you like. (using <a href="http://docs.wxwidgets.org/2.8/wx%5Fwxwindow.html#wxwindowsetsize" rel="nofollow">wxWindow::SetSize()</a>).</p>
<p>(The point of a <a href="http://docs.wxwidgets.org/2.8/wx%5Fwxsizer.... | 1 | 2009-03-06T16:07:55Z | [
"python",
"wxpython",
"wxwidgets"
] |
Tools to ease executing raw SQL with Django ORM | 619,384 | <p>I often need to execute custom sql queries in django, and manually converting query results into objects every time is kinda painful. I wonder how fellow Slackers deal with this. Maybe someone had written some kind of a library to help dealing with custom SQL in Django?</p>
| 2 | 2009-03-06T16:11:46Z | 619,691 | <p>Not exactly sure what you're looking for, but you can always add a method onto a model to execute custom SQL per <a href="http://docs.djangoproject.com/en/dev/topics/db/sql/#topics-db-sql" rel="nofollow" title="the docs">the docs</a>:</p>
<pre><code>def my_custom_sql(self):
from django.db import connection
curs... | 4 | 2009-03-06T17:26:39Z | [
"python",
"django",
"orm"
] |
Tools to ease executing raw SQL with Django ORM | 619,384 | <p>I often need to execute custom sql queries in django, and manually converting query results into objects every time is kinda painful. I wonder how fellow Slackers deal with this. Maybe someone had written some kind of a library to help dealing with custom SQL in Django?</p>
| 2 | 2009-03-06T16:11:46Z | 620,117 | <p>Since the issue is "manually converting query results into objects," the simplest solution is often to see if your custom SQL can fit into an ORM .extra() call rather than being a pure-SQL query. Often it can, and then you let the ORM do all the work of building up objects as usual.</p>
| 3 | 2009-03-06T19:33:52Z | [
"python",
"django",
"orm"
] |
Tools to ease executing raw SQL with Django ORM | 619,384 | <p>I often need to execute custom sql queries in django, and manually converting query results into objects every time is kinda painful. I wonder how fellow Slackers deal with this. Maybe someone had written some kind of a library to help dealing with custom SQL in Django?</p>
| 2 | 2009-03-06T16:11:46Z | 2,673,733 | <p>The newest development version (future 1.2) has .raw() method to help you with that:</p>
<pre><code>Person.objects.raw('SELECT * FROM myapp_person')
</code></pre>
<p>More information can be found under <a href="http://docs.djangoproject.com/en/dev/topics/db/sql/" rel="nofollow">http://docs.djangoproject.com/en/dev... | 4 | 2010-04-20T08:46:11Z | [
"python",
"django",
"orm"
] |
What does PyPy have to offer over CPython, Jython, and IronPython? | 619,437 | <p>From what I have seen and read on blogs, PyPy is a very ambitious project. What are some advantages it will bring to the table over its siblings (CPython, Jython, and IronPython)? Is it speed, cross-platform compatibility (including mobile platforms), the ability to use c-extensions without the GIL, or is this more ... | 28 | 2009-03-06T16:25:49Z | 619,463 | <p>The most important feature is of course the JIT compiler. In CPython files are compiled to bytecode (<code>.pyc</code>) or optimized bytecode (<code>.pyo</code>) and then interpreted. With PyPy they will be compiled to native code. PyPy also includes <a href="http://www.stackless.com/" rel="nofollow">Stackless Pytho... | 4 | 2009-03-06T16:29:52Z | [
"python",
"interpreter",
"pypy"
] |
What does PyPy have to offer over CPython, Jython, and IronPython? | 619,437 | <p>From what I have seen and read on blogs, PyPy is a very ambitious project. What are some advantages it will bring to the table over its siblings (CPython, Jython, and IronPython)? Is it speed, cross-platform compatibility (including mobile platforms), the ability to use c-extensions without the GIL, or is this more ... | 28 | 2009-03-06T16:25:49Z | 619,466 | <p>In case that Python gets a real <a href="http://en.wikipedia.org/wiki/Just-in-time%5Fcompilation" rel="nofollow">JIT</a> I think it's going to be as fast as any other implementation.</p>
<p>The advantage is that it's much easier to implement new features. One can see this today by observing the library. Often modul... | 0 | 2009-03-06T16:30:06Z | [
"python",
"interpreter",
"pypy"
] |
What does PyPy have to offer over CPython, Jython, and IronPython? | 619,437 | <p>From what I have seen and read on blogs, PyPy is a very ambitious project. What are some advantages it will bring to the table over its siblings (CPython, Jython, and IronPython)? Is it speed, cross-platform compatibility (including mobile platforms), the ability to use c-extensions without the GIL, or is this more ... | 28 | 2009-03-06T16:25:49Z | 619,480 | <blockquote>
<p><em>cross-platform compatibility</em></p>
</blockquote>
<p>Yes</p>
| 0 | 2009-03-06T16:33:03Z | [
"python",
"interpreter",
"pypy"
] |
What does PyPy have to offer over CPython, Jython, and IronPython? | 619,437 | <p>From what I have seen and read on blogs, PyPy is a very ambitious project. What are some advantages it will bring to the table over its siblings (CPython, Jython, and IronPython)? Is it speed, cross-platform compatibility (including mobile platforms), the ability to use c-extensions without the GIL, or is this more ... | 28 | 2009-03-06T16:25:49Z | 619,544 | <p>PyPy is really two projects:</p>
<ul>
<li>An interpreter compiler toolchain allowing you to write interpreters in RPython (a static subset of Python) and have cross-platform interpreters compiled standalone, for the JVM, for .NET (etc)</li>
<li>An implementation of Python in RPython</li>
</ul>
<p>These two project... | 38 | 2009-03-06T16:46:37Z | [
"python",
"interpreter",
"pypy"
] |
What does PyPy have to offer over CPython, Jython, and IronPython? | 619,437 | <p>From what I have seen and read on blogs, PyPy is a very ambitious project. What are some advantages it will bring to the table over its siblings (CPython, Jython, and IronPython)? Is it speed, cross-platform compatibility (including mobile platforms), the ability to use c-extensions without the GIL, or is this more ... | 28 | 2009-03-06T16:25:49Z | 723,987 | <p>This is a <a href="http://blip.tv/file/1957282">link</a> to presentation one of the PyPy authors gave at PyCon 2009. It is very informative.</p>
| 5 | 2009-04-07T02:39:24Z | [
"python",
"interpreter",
"pypy"
] |
Will python provide enough performance for a proxy? | 619,575 | <p>I want to start writing a http proxy that will modify responses according to some rules/filters I will configure. However, before I start coding it, I want to make sure I'm making the right choice in going with Python. Later, this tool would have to be able to process a lot of requests, so, I would like to know I ca... | 2 | 2009-03-06T16:52:36Z | 619,587 | <p>As long as the bulk of the processing uses Python's built-in modules it should be fine as far as performance. The biggest strength of Python is its clear syntax and ease of testing/maintainability. If you find that one section of your code is slowing down the process, you can rewrite that section and use it as a C m... | 4 | 2009-03-06T16:55:56Z | [
"python",
"performance",
"proxy"
] |
Will python provide enough performance for a proxy? | 619,575 | <p>I want to start writing a http proxy that will modify responses according to some rules/filters I will configure. However, before I start coding it, I want to make sure I'm making the right choice in going with Python. Later, this tool would have to be able to process a lot of requests, so, I would like to know I ca... | 2 | 2009-03-06T16:52:36Z | 619,605 | <p>This will depend on the library you use more than the language itself. The <a href="http://twistedmatrix.com/trac/" rel="nofollow">twisted</a> framework is known to scale well. </p>
<p>Here's a <a href="http://wiki.python.org/moin/Twisted-Examples" rel="nofollow">proxy server example in python/twisted</a> to get ... | 2 | 2009-03-06T17:00:36Z | [
"python",
"performance",
"proxy"
] |
Will python provide enough performance for a proxy? | 619,575 | <p>I want to start writing a http proxy that will modify responses according to some rules/filters I will configure. However, before I start coding it, I want to make sure I'm making the right choice in going with Python. Later, this tool would have to be able to process a lot of requests, so, I would like to know I ca... | 2 | 2009-03-06T16:52:36Z | 619,606 | <p>Yes, I think you will find Python to be perfectly adequate for your needs. There's a huge number of web frameworks, WSGI libraries, etc. to choose from, or learn from when building your own.</p>
<p>There's an interesting <a href="http://python-history.blogspot.com/2009/01/microsoft-ships-python-code-in-1996.html" ... | 2 | 2009-03-06T17:02:41Z | [
"python",
"performance",
"proxy"
] |
Will python provide enough performance for a proxy? | 619,575 | <p>I want to start writing a http proxy that will modify responses according to some rules/filters I will configure. However, before I start coding it, I want to make sure I'm making the right choice in going with Python. Later, this tool would have to be able to process a lot of requests, so, I would like to know I ca... | 2 | 2009-03-06T16:52:36Z | 619,640 | <p>Python performs pretty well for most tasks, but you'll need to change the way you program if you're used to other languages. See <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="nofollow">Python is not Java</a> for more info.</p>
<p>If plain old CPython doesn't give the performance you need, yo... | 2 | 2009-03-06T17:11:58Z | [
"python",
"performance",
"proxy"
] |
What is the best way to represent a schedule in a database, via Python/Django? | 619,592 | <p>I am writing a backup system in Python, with a Django front-end. I have decided to implement the scheduling in a slightly strange way - the client will poll the server (every 10 minutes or so), for a list of backups that need doing. The server will only respond when the time to backup is reached. This is to keep the... | 0 | 2009-03-06T16:56:50Z | 619,734 | <p>Take a look at <a href="http://code.google.com/p/django-chronograph/" rel="nofollow">django-chronograph</a>. It has a pretty nice interface for scheduling jobs at all sorts of intervals. You might be able to borrow some ideas from that. It relies on <a href="http://labix.org/python-dateutil" rel="nofollow">python-da... | 3 | 2009-03-06T17:39:35Z | [
"python",
"django",
"django-models",
"backup",
"django-forms"
] |
What is the best way to represent a schedule in a database, via Python/Django? | 619,592 | <p>I am writing a backup system in Python, with a Django front-end. I have decided to implement the scheduling in a slightly strange way - the client will poll the server (every 10 minutes or so), for a list of backups that need doing. The server will only respond when the time to backup is reached. This is to keep the... | 0 | 2009-03-06T16:56:50Z | 37,279,723 | <p>Your question is a bit ambiguousâdo you mean: <em>"Back up every Sunday, Monday and Friday at time X."</em>? </p>
<p>If so, use a <a href="https://en.wikipedia.org/wiki/Mask_(computing)" rel="nofollow">Bitmask</a> to store the recurring schedule as an integer:</p>
<p>Let's say that you want a backup as mentioned... | 0 | 2016-05-17T14:59:39Z | [
"python",
"django",
"django-models",
"backup",
"django-forms"
] |
Using pixel fonts in PIL | 619,618 | <p>I am creating images using PIL that contain numerous exactly placed text strings. My first attempt was to convert pixel fonts into the pil-compatible format as described <a href="http://llbb.wordpress.com/2006/11/24/making-pil-font-for-python-image-library/" rel="nofollow">here</a>. For example, I download the Sil... | 6 | 2009-03-06T17:05:11Z | 619,919 | <p>Eureka!</p>
<p>Just needed to specify a resolution of 72 dpi (default is 100) for otf2bdf:</p>
<pre><code>otf2bdf -p 8 -r 72 -o fonts/slkscr.bdf fonts/slkscr.ttf
</code></pre>
<p>Now, <img src="http://i.stack.imgur.com/QReUU.png" alt="alt text"> looks great!</p>
| 3 | 2009-03-06T18:31:15Z | [
"python",
"imaging"
] |
Do Django custom authentication backends need to take a password? | 619,620 | <p>Here's how my university handles authentication: we redirect the user to a website, they enter in their username and password, then they get redirected back to us with the username and a login key passed in the query string. When we get the user back, we call a stored procedure in the university's database that ta... | 2 | 2009-03-06T17:05:24Z | 619,700 | <p>The <a href="http://docs.djangoproject.com/en/dev/topics/auth/">Django docs</a> say this:</p>
<blockquote>
<p>Either way, authenticate should check
the credentials it gets, and it should
return a User object that matches
those credentials, if the credentials
are valid. If they're not valid, it
should re... | 6 | 2009-03-06T17:29:05Z | [
"python",
"django",
"authentication",
"django-authentication",
"custom-backend"
] |
Supplying password to wrapped-up MySQL | 619,804 | <p>Greetings.</p>
<p>I have written a little python script that calls MySQL in a subprocess. [Yes, I know that the right approach is to use MySQLdb, but compiling it under OS X Leopard is a pain, and likely more painful if I wanted to use the script on computers of different architectures.] The subprocess technique ... | 2 | 2009-03-06T17:58:49Z | 619,871 | <p>This may be a windows / SQL Server feature, but could you use a Trusted Connection (i.e. use your OS login/password to access the DB)? There may be an OS X equivalent for MySQL.</p>
<p>Or you may just need to set up your DB to use the OS login and password so that you don't need to keep it in your code.</p>
<p>An... | 2 | 2009-03-06T18:15:44Z | [
"python",
"mysql",
"subprocess"
] |
Supplying password to wrapped-up MySQL | 619,804 | <p>Greetings.</p>
<p>I have written a little python script that calls MySQL in a subprocess. [Yes, I know that the right approach is to use MySQLdb, but compiling it under OS X Leopard is a pain, and likely more painful if I wanted to use the script on computers of different architectures.] The subprocess technique ... | 2 | 2009-03-06T17:58:49Z | 619,899 | <p>You could simply build a my.cnf file and point to that on the mysql command. Obviously you'll want to protect that file with permissions/acls. But it shouldn't be really an more/less secure then having the password in your python script, or the config for your python script.</p>
<p>So you would do something like ... | 6 | 2009-03-06T18:22:20Z | [
"python",
"mysql",
"subprocess"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.