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
'sys.excepthook' and threading
1,643,327
<p>I am using Python 2.5 and trying to use a self-defined <code>excepthook</code> in my program. In the main thread it works perfectly fine. But in a thread started with the threading module the usual <code>excepthook</code> is called.</p> <p>Here is an example showing the problem. Uncommenting the comment shows the d...
7
2009-10-29T12:20:24Z
31,622,038
<p>It looks like this bug is still present in (at least) 3.4, and one of the workarounds in the discussion Nadia Alramli linked seems to work in Python 3.4 too.</p> <p>For convenience and documentation sake, I'll post the code for (in my opinion) the best workaround here. I updated the coding style and comments slight...
4
2015-07-25T01:49:34Z
[ "python", "multithreading", "exception", "exception-handling" ]
Passing a pre_delete() or post_delete() signal arguments?
1,643,332
<p>I am using signals to perform an action after an object has been deleted; however, sometimes I want to perform a <em>different</em> action (not the default one) depending on an arugment. </p> <p>Is there a way to pass an argument to my signal catcher? Or will I have to abandon the signal and instead hard code wha...
3
2009-10-29T12:21:32Z
1,643,682
<p>I don't think you need to hardcode your actions in the model - you can still use signals. But you will need to override delete() to at the very least accept the send_email parameter and - since I don't think you can pass extra parameters into post_delete() - trigger your own custom signal.</p> <p>Something like thi...
4
2009-10-29T13:21:11Z
[ "python", "django" ]
Killing Python webservers
1,643,362
<p>I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable.</p>
1
2009-10-29T12:26:51Z
1,643,387
<p>Raise exeption and handle it in main or use sys.exit</p>
1
2009-10-29T12:31:07Z
[ "python" ]
Killing Python webservers
1,643,362
<p>I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable.</p>
1
2009-10-29T12:26:51Z
1,643,390
<p>Try putting </p> <pre><code>import sys </code></pre> <p>at the top and the command</p> <pre><code>sys.exit(0) </code></pre> <p>In the code that handles the "kill request". </p>
1
2009-10-29T12:31:20Z
[ "python" ]
Killing Python webservers
1,643,362
<p>I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable.</p>
1
2009-10-29T12:26:51Z
1,643,709
<p>We use this.</p> <pre><code>import os os._exit(3) </code></pre> <p>To crash in a 'controlled' way.</p>
2
2009-10-29T13:23:47Z
[ "python" ]
Killing Python webservers
1,643,362
<p>I am looking for a simple Python webserver that is easy to kill from within code. Right now, I'm playing with Bottle, but I can't find any way at all to kill it in code. If you know how to kill Bottle (in code, no Ctrl+C) that would be super, but I'll take anything that's Python, simple, and killable.</p>
1
2009-10-29T12:26:51Z
1,643,773
<p>If you want to kill a process from Python, on a Unix-like platform, you can send signals equivalent to Ctrl-C at the console using Pythons os module e.g. </p> <pre><code># Get this processes PID pid_of_process = os.getpid() # Send the interrupt signal to this process os.kill(pid_of_process, signal.SIGINT) </code></...
2
2009-10-29T13:33:47Z
[ "python" ]
Grouping data on year
1,643,377
<pre><code>mydata = [{'date': datetime.datetime(2009, 1, 31, 0, 0), 'value': 14, 'year': u'2009'}, {'date': datetime.datetime(2009, 2, 28, 0, 0), 'value': 84, 'year': u'2009'}, {'date': datetime.datetime(2009, 3, 31, 0, 0), 'value': 77, 'year': u'2009'}, {'date': datetime.datetime(2009,...
1
2009-10-29T12:29:08Z
1,643,525
<pre><code>import datetime from itertools import groupby from pprint import pprint required_data={} for k,g in groupby(mydata,key=lambda x: x.get('year')): partial={} for datum in g: partial[datum.get('date').month]=datum required_data[k]=[partial.get(m,{}) for m in range(1,13)] pprint(required...
6
2009-10-29T12:55:49Z
[ "python", "code-review" ]
Customising Install location for Django (or any Python module)
1,643,540
<p>I'd like to to install Django into a custom location, I've read the <a href="http://docs.python.org/install/index.html#alternate-installation" rel="nofollow">distutils documentation</a> and it suggests that I should be able to do something like the following to install under my home directory (when run from an unpac...
0
2009-10-29T12:59:13Z
1,643,697
<p>As a quick check, I'd suggest replacing</p> <p><code>~/code/packages/install</code></p> <p>with</p> <p><code>/full_path_to_your_user/code/packages/install</code></p>
0
2009-10-29T13:22:36Z
[ "python", "django", "distutils" ]
Customising Install location for Django (or any Python module)
1,643,540
<p>I'd like to to install Django into a custom location, I've read the <a href="http://docs.python.org/install/index.html#alternate-installation" rel="nofollow">distutils documentation</a> and it suggests that I should be able to do something like the following to install under my home directory (when run from an unpac...
0
2009-10-29T12:59:13Z
1,643,827
<p>If you just want it in your home directory, there's no need to install it at all. Just make sure that the container directory is on your pythonpath somewhere, and move the scripts in django/bin into somewhere on your main PATH (or add that dir to your path).</p>
0
2009-10-29T13:42:56Z
[ "python", "django", "distutils" ]
Customising Install location for Django (or any Python module)
1,643,540
<p>I'd like to to install Django into a custom location, I've read the <a href="http://docs.python.org/install/index.html#alternate-installation" rel="nofollow">distutils documentation</a> and it suggests that I should be able to do something like the following to install under my home directory (when run from an unpac...
0
2009-10-29T12:59:13Z
1,645,039
<p>I would strongly suggest that you look at <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">Virtualenv</a> and <a href="http://pypi.python.org/pypi/pip" rel="nofollow">Pip</a> for creating basically silos of python packages.</p> <p>The <a href="http://pinaxproject.com/" rel="nofollow">Pinax project</a...
2
2009-10-29T16:44:14Z
[ "python", "django", "distutils" ]
Customising Install location for Django (or any Python module)
1,643,540
<p>I'd like to to install Django into a custom location, I've read the <a href="http://docs.python.org/install/index.html#alternate-installation" rel="nofollow">distutils documentation</a> and it suggests that I should be able to do something like the following to install under my home directory (when run from an unpac...
0
2009-10-29T12:59:13Z
1,645,101
<p>Ok, so I've been looking at the distutils source code to see what is going on - <code>distutils.command.install</code> does all of the pathname manipulation. </p> <p>It turns out that the documentation is actually incorrect. Whenever an --install-xxxx style option is provided it completely overrides any value that ...
1
2009-10-29T16:54:04Z
[ "python", "django", "distutils" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,643,661
<p>You can iterate over <code>sys.argv[1:]</code>, e.g. via something like:</p> <pre><code>for grp in sys.argv[1:]: for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == grp: hlo.append(sh.cell(i, 8).value) </code></pre>
5
2009-10-29T13:19:16Z
[ "python", "excel", "loops" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,643,689
<p>I would recommend taking a look at Python's <a href="http://docs.python.org/library/optparse.html" rel="nofollow">optparse</a> module. It's a nice helper to parse <code>sys.argv</code>.</p>
2
2009-10-29T13:21:44Z
[ "python", "excel", "loops" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,643,716
<p>I believe this would work, and would avoid iterating over sys.argv:</p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value in sys.argv[1:]: hlo.append(sh.cell(i, 8).value) </code></pre>
0
2009-10-29T13:24:33Z
[ "python", "excel", "loops" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,643,882
<pre><code>outputList = [x for x in values if x in sys.argv[1:]] </code></pre> <p>Substitute the bits that are relevant for your (spreadsheet?) situation. This is a <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>. You can also investigate the <a...
3
2009-10-29T13:48:22Z
[ "python", "excel", "loops" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,644,346
<p><a href="http://docs.python.org/dev/library/argparse.html" rel="nofollow">argparse</a> is another powerful, easy to use module that parses sys.argv for you. Very useful for creating command line scripts.</p>
1
2009-10-29T15:01:26Z
[ "python", "excel", "loops" ]
How to test for multiple command line arguments (sys.argv
1,643,643
<p>I want to test againts multiple command line arguments in a loop</p> <pre><code>&gt; python Read_xls_files.py group1 group2 group3 </code></pre> <p>No this code tests only for the first one (group1). </p> <pre><code>hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value == sys.argv[1]: hl...
3
2009-10-29T13:15:37Z
1,649,594
<pre><code># First thing is to get a set of your query strings. queries = set(argv[1:]) # If using optparse or argparse, queries = set(something_else) hlo = [] for i in range(len(sh.col_values(8))): if sh.cell(i, 1).value in queries: hlo.append(sh.cell(i, 8).value) </code></pre> <p>=== end of answer to que...
0
2009-10-30T12:41:09Z
[ "python", "excel", "loops" ]
Forward slash in a Python regex
1,643,772
<p>I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that <code>[\w\d\s+-/*]*</code> would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals tha...
7
2009-10-29T13:33:43Z
1,643,789
<p>You are saying it to replace <em>zero</em> or more slashes with <code>'a'</code>. So it does replace each "no character" with <code>'a'</code>. :)</p> <p>You probably meant <code>[/]+</code>, i.e. one or more slashes.</p> <p><strong>EDIT:</strong> Read <a href="http://stackoverflow.com/questions/1643772/forward-sl...
6
2009-10-29T13:36:44Z
[ "python", "regex" ]
Forward slash in a Python regex
1,643,772
<p>I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that <code>[\w\d\s+-/*]*</code> would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals tha...
7
2009-10-29T13:33:43Z
1,643,800
<p>r'[/]*' means "Match 0 or more forward-slashes". There are exactly 0 forward-slashes between 'b' &amp; 'c' and between 'c' &amp; 'd'. Hence, those matches are replaced with 'a'.</p>
1
2009-10-29T13:38:19Z
[ "python", "regex" ]
Forward slash in a Python regex
1,643,772
<p>I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that <code>[\w\d\s+-/*]*</code> would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals tha...
7
2009-10-29T13:33:43Z
1,643,809
<p>The <code>*</code> matches its argument zero or more times, and thus matches the empty string. The empty string is (logically) between any two consecutive characters. Hence</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub(r'x*', 'a', 'bcd') 'abacada' </code></pre> <p>As for the forward slash, it receives ...
1
2009-10-29T13:40:23Z
[ "python", "regex" ]
Forward slash in a Python regex
1,643,772
<p>I'm trying to use a Python regex to find a mathematical expression in a string. The problem is that the forward slash seems to do something unexpected. I'd have thought that <code>[\w\d\s+-/*]*</code> would work for finding math expressions, but it finds commas too for some reason. A bit of experimenting reveals tha...
7
2009-10-29T13:33:43Z
1,643,823
<p>Look <a href="http://docs.python.org/library/re.html#module-re">here for documentation</a> on Python's re module.</p> <p>I think it is not the "/", but rather the "-" in your first character class: <code>[+-/]</code> matches '+', '/' and any ASCII value between , which happen to include the comma.</p> <p>Maybe thi...
16
2009-10-29T13:42:38Z
[ "python", "regex" ]
Django tagging migration to GAE
1,643,838
<p>I have a Django app that use a django-tagging. I need to port this application to GAE. So, the main problem is to migrate tagging part. It has a complicated model, that should be rewritten to work with Google store. I think tagging is very popular django app and someone has the same problem before. Has someone a rew...
1
2009-10-29T13:44:08Z
1,644,167
<p>Check Nick's post about <a href="http://blog.notdot.net/2009/10/Blogging-on-App-Engine-part-5-Tagging" rel="nofollow">tagging blog posts</a>. It covers all main tagging issues.</p>
2
2009-10-29T14:33:24Z
[ "python", "django", "google-app-engine", "tagging" ]
Django tagging migration to GAE
1,643,838
<p>I have a Django app that use a django-tagging. I need to port this application to GAE. So, the main problem is to migrate tagging part. It has a complicated model, that should be rewritten to work with Google store. I think tagging is very popular django app and someone has the same problem before. Has someone a rew...
1
2009-10-29T13:44:08Z
10,692,077
<p>There is a <a href="https://bitbucket.org/legutierr/django-tagging-multidb/overview" rel="nofollow">modified django-tagging</a>, probably it might work.</p>
0
2012-05-21T20:23:42Z
[ "python", "django", "google-app-engine", "tagging" ]
Is there any way to use a strftime-like function for dates before 1900 in Python?
1,643,967
<p>I didn't realize this, but apparently Python's <code>strftime</code> function doesn't support dates before 1900:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; d = datetime(1899, 1, 1) &gt;&gt;&gt; d.strftime('%Y-%m-%d') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &...
18
2009-10-29T14:03:24Z
1,644,003
<p>This is the "feature" of the ctime library (UTF). Also You may have problem above 2038.</p>
1
2009-10-29T14:10:09Z
[ "python", "oracle", "datetime", "sql-loader", "strftime" ]
Is there any way to use a strftime-like function for dates before 1900 in Python?
1,643,967
<p>I didn't realize this, but apparently Python's <code>strftime</code> function doesn't support dates before 1900:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; d = datetime(1899, 1, 1) &gt;&gt;&gt; d.strftime('%Y-%m-%d') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &...
18
2009-10-29T14:03:24Z
1,644,026
<p>The <a href="http://docs.python.org/library/datetime.html#strftime-behavior">documentation seems pretty clear about this:</a></p> <blockquote> <p>The exact range of years for which <code>strftime()</code> works also varies across platforms. Regardless of platform, years before 1900 cannot be used.</p> </blockquot...
11
2009-10-29T14:12:37Z
[ "python", "oracle", "datetime", "sql-loader", "strftime" ]
Is there any way to use a strftime-like function for dates before 1900 in Python?
1,643,967
<p>I didn't realize this, but apparently Python's <code>strftime</code> function doesn't support dates before 1900:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; d = datetime(1899, 1, 1) &gt;&gt;&gt; d.strftime('%Y-%m-%d') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &...
18
2009-10-29T14:03:24Z
1,644,028
<p><a href="http://www.egenix.com/products/python/mxBase/mxDateTime/" rel="nofollow"><code>mxDateTime</code></a> can handle arbitrary dates. Python's <code>time</code> and <code>datetime</code> modules use UNIX timestamps internally, that's why they have limited range.</p> <pre><code>In [5]: mx.DateTime.DateTime(1899)...
3
2009-10-29T14:12:42Z
[ "python", "oracle", "datetime", "sql-loader", "strftime" ]
Is there any way to use a strftime-like function for dates before 1900 in Python?
1,643,967
<p>I didn't realize this, but apparently Python's <code>strftime</code> function doesn't support dates before 1900:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; d = datetime(1899, 1, 1) &gt;&gt;&gt; d.strftime('%Y-%m-%d') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &...
18
2009-10-29T14:03:24Z
1,644,114
<p>This is from the <a href="http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/lib/matplotlib/dates.py?view=markup" rel="nofollow">matplotlib source</a>. Could provide a good starting point for rolling your own.</p> <pre><code>def strftime(self, dt, fmt): fmt = self.illegal_s.sub(r"\1", fmt...
3
2009-10-29T14:25:46Z
[ "python", "oracle", "datetime", "sql-loader", "strftime" ]
Is there any way to use a strftime-like function for dates before 1900 in Python?
1,643,967
<p>I didn't realize this, but apparently Python's <code>strftime</code> function doesn't support dates before 1900:</p> <pre><code>&gt;&gt;&gt; from datetime import datetime &gt;&gt;&gt; d = datetime(1899, 1, 1) &gt;&gt;&gt; d.strftime('%Y-%m-%d') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &...
18
2009-10-29T14:03:24Z
1,644,392
<p><a href="http://docs.python.org/library/datetime.html?highlight=isoformat#datetime.datetime.isoformat">isoformat</a> works on <code>datetime</code> instances w/o limitation of range:</p> <pre><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; x=datetime.datetime(1865, 7, 2, 9, 30, 21) &gt;&gt;&gt; x.isoformat() '1865-...
13
2009-10-29T15:07:26Z
[ "python", "oracle", "datetime", "sql-loader", "strftime" ]
best way to parse a line in python to a dictionary
1,644,362
<p>I have a file with lines like</p> <pre><code>account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that" </code></pre> <p>There is no special delimiter and each key has a value that is surrounded by double quotes if its a string but not if it is a number. There is no key without a value th...
1
2009-10-29T15:04:29Z
1,644,488
<p>We're going to need a regex for this.</p> <pre><code>import re, decimal r= re.compile('([^ =]+) *= *("[^"]*"|[^ ]*)') d= {} for k, v in r.findall(line): if v[:1]=='"': d[k]= v[1:-1] else: d[k]= decimal.Decimal(v) &gt;&gt;&gt; d {'account': 'TEST1', 'subject': 'some value', 'values': '3=thi...
10
2009-10-29T15:20:08Z
[ "python", "parsing", "delimiter" ]
best way to parse a line in python to a dictionary
1,644,362
<p>I have a file with lines like</p> <pre><code>account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that" </code></pre> <p>There is no special delimiter and each key has a value that is surrounded by double quotes if its a string but not if it is a number. There is no key without a value th...
1
2009-10-29T15:04:29Z
1,644,662
<p>A recursive variation of bobince's parses values with embedded equals as dictionaries:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; import pprint &gt;&gt;&gt; &gt;&gt;&gt; def parse_line(line): ... d = {} ... a = re.compile(r'\s*(\w+)\s*=\s*("[^"]*"|[^ ,]*),?') ... float_re = re.compile(r'^\d.+$')...
0
2009-10-29T15:44:51Z
[ "python", "parsing", "delimiter" ]
best way to parse a line in python to a dictionary
1,644,362
<p>I have a file with lines like</p> <pre><code>account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that" </code></pre> <p>There is no special delimiter and each key has a value that is surrounded by double quotes if its a string but not if it is a number. There is no key without a value th...
1
2009-10-29T15:04:29Z
1,645,193
<p>If you don't want to use a regex, another option is just to read the string a character at a time:</p> <pre><code>string = 'account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that"' inside_quotes = False key = None value = "" dict = {} for c in string: if c == '"': inside_q...
0
2009-10-29T17:05:58Z
[ "python", "parsing", "delimiter" ]
best way to parse a line in python to a dictionary
1,644,362
<p>I have a file with lines like</p> <pre><code>account = "TEST1" Qty=100 price = 20.11 subject="some value" values="3=this, 4=that" </code></pre> <p>There is no special delimiter and each key has a value that is surrounded by double quotes if its a string but not if it is a number. There is no key without a value th...
1
2009-10-29T15:04:29Z
1,650,758
<p>Maybe a bit simpler to follow is the <a href="http://pyparsing.wikispaces.com" rel="nofollow">pyparsing</a> rendition:</p> <pre><code>from pyparsing import * # define basic elements - use re's for numerics, faster than easier than # composing from pyparsing objects integer = Regex(r'[+-]?\d+') real = Regex(r'[+-]...
4
2009-10-30T16:09:33Z
[ "python", "parsing", "delimiter" ]
Unable to find solution for a practice problem in codechef
1,644,456
<p>Here is the problem from code chef : </p> <p>A set of N dignitaries have arrived in close succession at Delhi and are awaiting transportation to Roorkee to participate in the inaugural ceremony of Cognizance. Being big sponsors of Cognizance, it has been deemed unsuitable by the organizing team to arrange for more ...
0
2009-10-29T15:15:48Z
1,644,608
<p>One first error (I doubt it's blocking you):</p> <pre><code>if int(test[0]) is not len(test[1:]): </code></pre> <p>It's never correct to use <code>is</code> / <code>is not</code> to test immutables (such as numbers): use <code>=</code> or <code>!=</code> instead. The code you've written may accidentally work as a...
2
2009-10-29T15:38:42Z
[ "python", "algorithm" ]
Unable to find solution for a practice problem in codechef
1,644,456
<p>Here is the problem from code chef : </p> <p>A set of N dignitaries have arrived in close succession at Delhi and are awaiting transportation to Roorkee to participate in the inaugural ceremony of Cognizance. Being big sponsors of Cognizance, it has been deemed unsuitable by the organizing team to arrange for more ...
0
2009-10-29T15:15:48Z
1,644,659
<p>Consider the test case:</p> <pre><code>2 A B 1 2 2 1 2 1 1 </code></pre> <p>The answer is yes because person A can use car 2, and person B will use car 1. </p> <p>I believe your solution will put person A in car 1, then be unable to place person 2.</p> <p>If you want a hint, this problem reduces to whether there...
1
2009-10-29T15:44:36Z
[ "python", "algorithm" ]
How do I raise an http error/exception from a python CGI script?
1,644,482
<p>How do I raise an http error/exception from a python CGI script?</p> <p>Is all that is necessary is to print the appropriate header:</p> <pre><code>print '''Status: 501 Not Implemented Content-type: text/html ''' </code></pre> <p>That doesn't seem to work right.</p> <p>I have a very basic setup, namely IIS7 rou...
3
2009-10-29T15:19:01Z
1,645,141
<p>It seems like that's the way to do it. As long as you're correctly following the header format.</p> <p>Here's someone else who asked the same question, more or less.</p> <p><a href="http://stackoverflow.com/questions/833715/returning-http-status-codes-in-python-cgi">http://stackoverflow.com/questions/833715/retur...
2
2009-10-29T16:58:49Z
[ "python", "web-applications", "cgi" ]
How do I raise an http error/exception from a python CGI script?
1,644,482
<p>How do I raise an http error/exception from a python CGI script?</p> <p>Is all that is necessary is to print the appropriate header:</p> <pre><code>print '''Status: 501 Not Implemented Content-type: text/html ''' </code></pre> <p>That doesn't seem to work right.</p> <p>I have a very basic setup, namely IIS7 rou...
3
2009-10-29T15:19:01Z
1,646,647
<p><a href="http://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403">http://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403</a></p> <p>One answer suggested there:</p> <blockquote> <p>sys.stdout('Status: 403 Forbidden\r\n\r\n')</p> ...
0
2009-10-29T21:25:00Z
[ "python", "web-applications", "cgi" ]
How do I raise an http error/exception from a python CGI script?
1,644,482
<p>How do I raise an http error/exception from a python CGI script?</p> <p>Is all that is necessary is to print the appropriate header:</p> <pre><code>print '''Status: 501 Not Implemented Content-type: text/html ''' </code></pre> <p>That doesn't seem to work right.</p> <p>I have a very basic setup, namely IIS7 rou...
3
2009-10-29T15:19:01Z
10,793,939
<p>Start your script with: <code>cgitb.enable()</code> Then to output a correct error code during an exception use:</p> <pre><code>try: blah blah failed code except: print "Status: 500" cgitb.handler() </code></pre>
1
2012-05-29T06:13:30Z
[ "python", "web-applications", "cgi" ]
CPython is bytecode interpreter?
1,644,619
<p>I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture?</p> <p>Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they ar...
7
2009-10-29T15:39:57Z
1,644,643
<p>CPython is both the bytecode compiler, and interpreter (virtual machine).</p> <p>It automatically detects if no existing pre-compiler file (.pyc) exists, compiles the code, and saves it out.</p>
7
2009-10-29T15:42:45Z
[ "c++", "python", "interpreter", "bytecode", "cpython" ]
CPython is bytecode interpreter?
1,644,619
<p>I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture?</p> <p>Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they ar...
7
2009-10-29T15:39:57Z
1,644,742
<p>CPython is the implementation of Python in C. It's the first implementation, and still the main one that people mean when they talk about Python. It compiles .py files to .pyc files. .pyc files contain bytecodes. The CPython implementation also interprets those bytecodes. CPython is not written in C++, it is C.<...
10
2009-10-29T15:57:51Z
[ "c++", "python", "interpreter", "bytecode", "cpython" ]
CPython is bytecode interpreter?
1,644,619
<p>I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture?</p> <p>Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they ar...
7
2009-10-29T15:39:57Z
6,830,193
<p>First: The fact that CPython is a bytecode interpreter should not matter to you as a user of python. Go ahead and write code, and don't worry about how it's turned into action.</p> <p>So, to answer your question and satisfy your curiosity, here is what happens: CPython reads python source code, and compiles it into...
8
2011-07-26T12:43:49Z
[ "c++", "python", "interpreter", "bytecode", "cpython" ]
How to handle Unicode (non-ASCII) characters in Python?
1,644,640
<p>I'm programming in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> and I'm obtaining information from a web page through the <a href="http://docs.python.org/library/urllib2.html"><code>urllib2</code></a> library. The problem is that that page can provide me with non-ASCII characte...
8
2009-10-29T15:42:32Z
1,644,663
<p>You want to use unicode for all your work if you can. </p> <p>You probably will find this question/answer useful:</p> <p><a href="http://stackoverflow.com/questions/1020892/python-urllib2-read-to-unicode">http://stackoverflow.com/questions/1020892/python-urllib2-read-to-unicode</a></p>
6
2009-10-29T15:45:13Z
[ "python", "unicode", "character-encoding" ]
How to handle Unicode (non-ASCII) characters in Python?
1,644,640
<p>I'm programming in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> and I'm obtaining information from a web page through the <a href="http://docs.python.org/library/urllib2.html"><code>urllib2</code></a> library. The problem is that that page can provide me with non-ASCII characte...
8
2009-10-29T15:42:32Z
1,644,816
<p>You might want to look into using an actual parsing library to find this information. <a href="http://codespeak.net/lxml/parsing.html#python-unicode-strings" rel="nofollow"><code>lxml</code></a>, for instance, already addresses Unicode encode/decode using the declared character set.</p>
0
2009-10-29T16:08:22Z
[ "python", "unicode", "character-encoding" ]
How to handle Unicode (non-ASCII) characters in Python?
1,644,640
<p>I'm programming in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> and I'm obtaining information from a web page through the <a href="http://docs.python.org/library/urllib2.html"><code>urllib2</code></a> library. The problem is that that page can provide me with non-ASCII characte...
8
2009-10-29T15:42:32Z
1,645,140
<p>You just read a set of bytes from the socket. If you want a string you have to decode it:</p> <pre><code>yourstring = receivedbytes.decode("utf-8") </code></pre> <p>(substituting whatever encoding you're using for <code>utf-8</code>)</p> <p>Then you have to do the reverse to send it back out:</p> <pre><code>ou...
9
2009-10-29T16:58:42Z
[ "python", "unicode", "character-encoding" ]
Close all opened xml tags
1,644,994
<p>I have a file, which change it content in a short time. But I'd like to read it before it is ready. The problem is, that it is an xml-file (log). So when you read it, it could be, that not all tags are closed.</p> <p>I would like to know if there is a possibility to close all opened tags correctly, that there are n...
4
2009-10-29T16:36:38Z
1,645,047
<p>You can use any SAX parser by feeding data available so far to it. Use SAX handler that just reconstructs source XML, keep the stack of tags opened and close them in reverse order at the end.</p>
0
2009-10-29T16:45:07Z
[ "python", "xml" ]
Close all opened xml tags
1,644,994
<p>I have a file, which change it content in a short time. But I'd like to read it before it is ready. The problem is, that it is an xml-file (log). So when you read it, it could be, that not all tags are closed.</p> <p>I would like to know if there is a possibility to close all opened tags correctly, that there are n...
4
2009-10-29T16:36:38Z
1,645,099
<p>Some XML parsers allow incremental parsing of XML documents that is the parser can start working on the document without needing it to be fully loaded. The XMLTreeBuilder from the xml.etree.ElementTree module in the Python standard library is one such parser: <a href="http://docs.python.org/library/xml.etree.element...
5
2009-10-29T16:53:52Z
[ "python", "xml" ]
Close all opened xml tags
1,644,994
<p>I have a file, which change it content in a short time. But I'd like to read it before it is ready. The problem is, that it is an xml-file (log). So when you read it, it could be, that not all tags are closed.</p> <p>I would like to know if there is a possibility to close all opened tags correctly, that there are n...
4
2009-10-29T16:36:38Z
1,645,135
<p>If I am understanding your question correctly, you have a log file that is always being appended to so you get something like:</p> <pre><code>&lt;root&gt; &lt;entry&gt; ... &lt;/entry&gt; &lt;entry&gt; ... &lt;/entry&gt; ... &lt;entry&gt; ... &lt;/entry &lt;!-- no closing root --&gt; </code></pre> <p>In this case ...
1
2009-10-29T16:58:21Z
[ "python", "xml" ]
Close all opened xml tags
1,644,994
<p>I have a file, which change it content in a short time. But I'd like to read it before it is ready. The problem is, that it is an xml-file (log). So when you read it, it could be, that not all tags are closed.</p> <p>I would like to know if there is a possibility to close all opened tags correctly, that there are n...
4
2009-10-29T16:36:38Z
1,652,871
<p>You could use BeautifulStoneSoup (XML part of BeautifulSoup).</p> <p>www.crummy.com/software/BeautifulSoup</p> <p>It's not ideal, but it would circumvent the problem if you cannot fix the file's output...</p> <p>It's basically a previously implemented version of what Denis said.</p> <p>You can just join whatever...
0
2009-10-30T23:57:36Z
[ "python", "xml" ]
Trace Table for Python Programs
1,645,028
<p>Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I'm a teacher trying to flawlessly verify the answers to the tracing problems that we use on our tests.</p> <p>So, for example, assuming I have a Python program named <code>problem1.py</code>...
7
2009-10-29T16:41:29Z
1,645,159
<p>This isn't a use case that the current Python tracing tool support, but it should be possible to build. I don't know how you decide what columns to output. In your sample, a and b are the only local variables, but larger programs would have more interesting ecosystems of data.</p> <p>Updated: here's a simple proo...
10
2009-10-29T17:00:41Z
[ "python" ]
Trace Table for Python Programs
1,645,028
<p>Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I'm a teacher trying to flawlessly verify the answers to the tracing problems that we use on our tests.</p> <p>So, for example, assuming I have a Python program named <code>problem1.py</code>...
7
2009-10-29T16:41:29Z
1,645,272
<p>You could use the <a href="http://docs.python.org/library/pdb.html" rel="nofollow">Python debugger</a>, though I do not know how to have it step through on it's own, but it's likely do-able, then you could just parse the output.</p> <p>Here's a really crude example:</p> <p><strong>adding.py</strong></p> <pre><cod...
0
2009-10-29T17:19:26Z
[ "python" ]
Trace Table for Python Programs
1,645,028
<p>Is there a way to get the trace table for a Python program? Or for a program to run another program and get its trace table? I'm a teacher trying to flawlessly verify the answers to the tracing problems that we use on our tests.</p> <p>So, for example, assuming I have a Python program named <code>problem1.py</code>...
7
2009-10-29T16:41:29Z
32,908,370
<p>Based on what ned-batchelder proposed, as a teacher, I've made a <code>Tracer</code> class that help on creating <code>LaTeX</code> outputed <code>longtable</code> showing the trace of a program with selective variables, bypassing <code>input()</code> for automating process (especially while called by a <code>\bash<...
0
2015-10-02T13:33:29Z
[ "python" ]
Concurrency control in Django model
1,645,269
<p>How do I handle concurrency in a Django model? I dont want the changes to the record being overwritten by another user who reads the same record</p>
15
2009-10-29T17:19:12Z
1,656,572
<p>The short answer, this really isn't a Django question as presented. </p> <p>Concurrency control is often presented as a technical question, but is in many ways a question of functional requirements. How do you want/need your application to work? Until we know that, it will be difficult to give any Django-specifi...
17
2009-11-01T07:00:54Z
[ "python", "django", "concurrency" ]
Concurrency control in Django model
1,645,269
<p>How do I handle concurrency in a Django model? I dont want the changes to the record being overwritten by another user who reads the same record</p>
15
2009-10-29T17:19:12Z
4,679,345
<p>I don't think that 'keeping a version number or timestamp' works. When self.version == self.read_current_version() is true, there is still a chance that the version number got modified by other sessions just before you call super().save.</p>
8
2011-01-13T11:04:58Z
[ "python", "django", "concurrency" ]
Concurrency control in Django model
1,645,269
<p>How do I handle concurrency in a Django model? I dont want the changes to the record being overwritten by another user who reads the same record</p>
15
2009-10-29T17:19:12Z
24,099,569
<p>I agree with the introductory explanation from Joe Holloway.</p> <p>I want to contribute with a working snippet relative to the very last part of his answer ("In terms of Django, optimistic concurrency control can be implemented by overriding the save method on your model class...")</p> <p>You can use the followin...
1
2014-06-07T16:59:36Z
[ "python", "django", "concurrency" ]
Django db reset without loading fixtures
1,645,310
<p>Is there an easy way to reset a django database (i.e. drop all data/tables, create new tables and create indexes) without loading fixture data afterwords? What I want to have is just an empty database because all data is loaded from another source (a kind of a post-processed backup).</p> <p>I know that this could ...
1
2009-10-29T17:26:17Z
1,645,504
<p>shouldn't you be able do do this with manage.py's <a href="http://docs.djangoproject.com/en/1.0/ref/django-admin/#reset-appname-appname" rel="nofollow">reset</a> option?</p>
2
2009-10-29T17:58:50Z
[ "python", "database", "django", "fixtures" ]
Django db reset without loading fixtures
1,645,310
<p>Is there an easy way to reset a django database (i.e. drop all data/tables, create new tables and create indexes) without loading fixture data afterwords? What I want to have is just an empty database because all data is loaded from another source (a kind of a post-processed backup).</p> <p>I know that this could ...
1
2009-10-29T17:26:17Z
1,645,519
<p>As far as I know, the fixtures (in initial_data file) are automatically loaded after <code>manage.py syndcb</code> and not after reset. So, if you do a <code>manage.py reset yourapp</code> it should not load the fixtures. Hmm?</p>
2
2009-10-29T18:01:24Z
[ "python", "database", "django", "fixtures" ]
insert string in the middle of a file given a file object
1,645,384
<p>I am working on a problem and got stuck at a wall</p> <p>I have a (potentially large) set of text files, and I need to apply a sequence of filters and transformations to it and export it to some other places.</p> <p>so I roughly have</p> <pre><code>def apply_filter_transformer(basepath = None, newpath = None, fts...
0
2009-10-29T17:40:09Z
1,645,491
<p>There is handy python module for modifing or reading a group of files: <a href="http://docs.python.org/library/fileinput.html" rel="nofollow">fileinput</a></p> <p>I'm not sure what is causing this error. But you are reading the whole file into memory which is a bad idea in your case because the files are potentiall...
1
2009-10-29T17:57:05Z
[ "python", "string", "file" ]
insert string in the middle of a file given a file object
1,645,384
<p>I am working on a problem and got stuck at a wall</p> <p>I have a (potentially large) set of text files, and I need to apply a sequence of filters and transformations to it and export it to some other places.</p> <p>so I roughly have</p> <pre><code>def apply_filter_transformer(basepath = None, newpath = None, fts...
0
2009-10-29T17:40:09Z
1,645,766
<p>It's not really possible to tell what's causing the error from the code you posted. The problem may be in the protocol you've adopted for your transformation functions. </p> <p>I'll simplify the code a bit:</p> <pre><code>fileobj = file.open(path, mode) fileobj = fcn(fileobj) fileobj.writelines(fileobj.readlines...
1
2009-10-29T18:45:08Z
[ "python", "string", "file" ]
How this keyword is provided for object instances in C#?
1,645,550
<p>When you have an object instance in C#, you can use the <code>this</code> keyword inside the instance scope. How does the compiler handles it? Is there any assistance for this at runtime?</p> <p>I am mainly wondering how C# does it vs in python you have to provide self for every function manually.</p>
2
2009-10-29T18:07:48Z
1,645,603
<p>This is supported at the CLR level. The argument variable at slot 0 represents the "this" pointer. C# essentially generates calls to this as ldarg.0</p>
3
2009-10-29T18:15:38Z
[ "c#", ".net", "python", "clr" ]
How this keyword is provided for object instances in C#?
1,645,550
<p>When you have an object instance in C#, you can use the <code>this</code> keyword inside the instance scope. How does the compiler handles it? Is there any assistance for this at runtime?</p> <p>I am mainly wondering how C# does it vs in python you have to provide self for every function manually.</p>
2
2009-10-29T18:07:48Z
1,645,621
<p>The compiler always creates IL that sets the field using the class name, in any case - whether you specify this or not. The <code>this.</code> is optional unless there is another variable in your scope with the same name as an instance variable, in which case the compiler knows to set the instance variable.</p> <p...
2
2009-10-29T18:18:40Z
[ "c#", ".net", "python", "clr" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,711
<p>What's wrong with just accessing the correct index and incrementing? <code><pre>for (int i=0;i&lt;myList.Length;i++) { draw_thing(myList[i],myList[++i]); }</pre></code></p> <p>Oops - sorry, in C# mode. I'm sure you get the idea.</p>
-3
2009-10-29T18:34:45Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,725
<p>You can do:</p> <pre><code>l2 = [] for y in range(0, len(l1), 2): l2.append((l1[y], l1[y+1])) </code></pre> <p>I'm not doing any checks to make sure l1 has an even number of entries and such-like.</p>
0
2009-10-29T18:37:35Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,726
<p>Not the most elegant solution</p> <pre><code>l2 = [(l1[i], l1[i+1]) for i in xrange(0,len(l1),2)] </code></pre>
0
2009-10-29T18:37:37Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,728
<p>One <em>good</em> way is:</p> <pre><code>from itertools import izip it = iter([1, 2, 3, 4]) for x, y in izip(it, it): print x, y </code></pre> <p>Output:</p> <pre><code>1 2 3 4 &gt;&gt;&gt; </code></pre>
10
2009-10-29T18:37:55Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,736
<p>No need to construct a new list. You can just iterate over the list by steps of 2 instead of 1. I use <code>len(L) - 1</code> as the upper-bound so you ensure that you don't try to access past the end of the list.</p> <pre><code>for i in range(0, len(L) - 1, 2): draw_thing(L[i], L[i + 1]) </code></pre>
0
2009-10-29T18:39:40Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,837
<p>Building on Nick D's answer:</p> <pre><code>&gt;&gt;&gt; from itertools import izip &gt;&gt;&gt; t = [1,2,3,4,5,6,7,8,9,10,11,12] &gt;&gt;&gt; for a, b in izip(*[iter(t)]*2): ... print a, b ... 1 2 3 4 5 6 7 8 9 10 11 12 &gt;&gt;&gt; for a, b, c in izip(*[iter(t)]*3): ... print a, b, c ... 1 2 3 4 5 6 7 8 9...
7
2009-10-29T18:57:38Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,844
<p>Take a look at grouper function from <a href="http://docs.python.org/library/itertools.html" rel="nofollow">itertools docs</a>.</p> <pre><code>from itertools import izip_longest def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --&gt; ABC DEF Gxx" args = [iter(iterable)] * n return i...
2
2009-10-29T18:59:07Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,864
<p>Kind of easy with python's slicing operator:</p> <pre><code>l2 = zip(l1[0::2], l1[1::2]) </code></pre>
5
2009-10-29T19:02:22Z
[ "python" ]
how convert list of int to list of tuples
1,645,673
<p>I want to convert a list like this</p> <pre><code>l1 = [1,2,3,4,5,6,7,8] </code></pre> <p>to</p> <pre><code>l2 = [(1,2),(3,4),(5,6),(7,8)] </code></pre> <p>because want to loop </p> <pre><code>for x,y in l2: draw_thing(x,y) </code></pre>
4
2009-10-29T18:29:40Z
1,645,990
<pre><code> list = [1,2,3,4,5,6] it = iter(list) newlist = [(x, y) for x, y in zip(it, it)] </code></pre>
0
2009-10-29T19:25:18Z
[ "python" ]
Django: Extending Querysets / Connect multiple filters with OR
1,645,778
<p>I have to work with a queryset, that is already filtered, eg. qs = queryset.filter(language='de') but in some further operation i need to undo some of the already applied filtering, eg not to take only the rows with language='de' but entries in all languages. Is there a way to apply filter again and have the new par...
3
2009-10-29T18:46:34Z
1,645,947
<p>I don't believe it is possible to do what you are asking.</p> <p>The way you do ORs in django is like this:</p> <pre><code>Model.objects.filter(Q(question__startswith='Who') | Q(question__startswith='What')) </code></pre> <p>so if you actually wanted to do this:</p> <pre><code>Model.objects.filter(Q(language='de...
2
2009-10-29T19:19:06Z
[ "python", "django", "filter", "django-queryset" ]
Django Year/Month based posts archive
1,645,962
<p>i'm new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this <a href="http://www.flickr.com/photos/ionutgabriel/3990015411/" rel="nofollow">http://www.flickr.com/photos/ionutgabriel/3990015411/</a>. </p> <p>...
6
2009-10-29T19:21:11Z
1,646,000
<p>You might want to consider starting with a <a href="https://docs.djangoproject.com/en/1.4/ref/generic-views/#date-based-generic-views" rel="nofollow">generic view</a> and building off that.</p>
2
2009-10-29T19:26:56Z
[ "python", "django" ]
Django Year/Month based posts archive
1,645,962
<p>i'm new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this <a href="http://www.flickr.com/photos/ionutgabriel/3990015411/" rel="nofollow">http://www.flickr.com/photos/ionutgabriel/3990015411/</a>. </p> <p>...
6
2009-10-29T19:21:11Z
1,646,414
<p>Firstly, the datetime format strings are given in the <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now">django docs</a>. I think you want capital instead of lowercase 'M'.</p> <p>Since you want to display all 12 months of a year, even if only some have posts, we'll create an <code>archives<...
12
2009-10-29T20:40:43Z
[ "python", "django" ]
Django Year/Month based posts archive
1,645,962
<p>i'm new to Django and started an application, i did the models, views, templates, but i want to add some kind of archive to the bottom of the page, something like this <a href="http://www.flickr.com/photos/ionutgabriel/3990015411/" rel="nofollow">http://www.flickr.com/photos/ionutgabriel/3990015411/</a>. </p> <p>...
6
2009-10-29T19:21:11Z
1,647,285
<p>Ok... so the final code that works for me is:</p> <p>in view:</p> <pre><code> rom_months = ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun', 'Iul', 'Aug', 'Sept', 'Oct', 'Noi', 'Dec'] def arhiva(request): arch = Post.objects.dates('data', 'month', order='DESC') archives = {} for i in arch: year = ...
2
2009-10-30T00:07:53Z
[ "python", "django" ]
Setting mod_python's interperter
1,646,017
<p>I have mod_python installed on a debian box with python 2.4 and 2.6 installed. I want mod_python to use 2.6 but it is finding 2.4. How can set it to use the other version. </p>
1
2009-10-29T19:29:51Z
1,646,473
<p>The version of Python used is set when mod_python is compiled. If you need to use a version other than the default, you'll need to recompile it, or you may be able to find a different package from the repository.</p>
1
2009-10-29T20:50:51Z
[ "python", "apache", "mod-python" ]
Not able to access google spreadsheet from app engine: "Moved Temporarily"?
1,646,073
<p>As of a couple of days ago I was able to access my google spreadsheets from an app (in app engine), but today it is broken.</p> <p>Namely, I could</p> <pre><code>name= "name of my spreadsheet" self.client = gdata.spreadsheet.text_db.DatabaseClient() self.client.GetDatabases(name=name) </code></pre> <p>and it work...
2
2009-10-29T19:40:23Z
1,646,440
<p>Docs is redirecting you to the login page. You need to authenticate before attempting to fetch the spreadsheet. I'm not sure why it's not requiring you to authenticate on the dev server, though.</p>
3
2009-10-29T20:45:17Z
[ "python", "google-app-engine" ]
Not able to access google spreadsheet from app engine: "Moved Temporarily"?
1,646,073
<p>As of a couple of days ago I was able to access my google spreadsheets from an app (in app engine), but today it is broken.</p> <p>Namely, I could</p> <pre><code>name= "name of my spreadsheet" self.client = gdata.spreadsheet.text_db.DatabaseClient() self.client.GetDatabases(name=name) </code></pre> <p>and it work...
2
2009-10-29T19:40:23Z
1,646,455
<p>You probably have to unrestricted access to the spreadsheet. You don't have it shared so everyone can view it.<br /> Can read about sharing <a href="http://docs.google.com/support/bin/static.py?page=guide.cs&amp;guide=20322&amp;topic=20431" rel="nofollow">here</a>.</p> <p><strong>Ninja Edit:</strong><br /> I didn'...
1
2009-10-29T20:47:46Z
[ "python", "google-app-engine" ]
Not able to access google spreadsheet from app engine: "Moved Temporarily"?
1,646,073
<p>As of a couple of days ago I was able to access my google spreadsheets from an app (in app engine), but today it is broken.</p> <p>Namely, I could</p> <pre><code>name= "name of my spreadsheet" self.client = gdata.spreadsheet.text_db.DatabaseClient() self.client.GetDatabases(name=name) </code></pre> <p>and it work...
2
2009-10-29T19:40:23Z
1,649,433
<p>Ok, it now works. The Google has healed itself.</p> <p>To be clear, I did not change anything in my code and the problem went away. It must have been a transient error at Google.</p> <p>I'm hoping these type of errors will be really rare, as they bring my whole web app down.</p>
0
2009-10-30T12:08:23Z
[ "python", "google-app-engine" ]
Not able to access google spreadsheet from app engine: "Moved Temporarily"?
1,646,073
<p>As of a couple of days ago I was able to access my google spreadsheets from an app (in app engine), but today it is broken.</p> <p>Namely, I could</p> <pre><code>name= "name of my spreadsheet" self.client = gdata.spreadsheet.text_db.DatabaseClient() self.client.GetDatabases(name=name) </code></pre> <p>and it work...
2
2009-10-29T19:40:23Z
6,696,952
<p>Check my answer on <a href="http://blog.forret.com/2011/07/google-docs-infamous-moved-temporarily-error-fixed/" rel="nofollow">http://blog.forret.com/2011/07/google-docs-infamous-moved-temporarily-error-fixed/</a></p> <p>If you add "&amp;ndplr=1" to the link, Google will not try to redirect for authentication.</p>
1
2011-07-14T16:47:22Z
[ "python", "google-app-engine" ]
How do I import a COM object namespace/enumeration in Python?
1,646,230
<p>I'm relatively new to programming/python, so I'd appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the code:</p> <pre><code>import win32com.client as win32 def excel(): app = 'Excel' x1 = win32.gencache.EnsureDispatch('%s.Application' % ap...
7
2009-10-29T20:08:48Z
1,647,147
<p>When I used COM to access quickbooks, I could reach the constants defined under a <em>constants</em> member of the object. The code looked something like this (you'll be intersted in the third line):</p> <pre><code>self._session_manager.OpenConnection2("", application_name, ...
2
2009-10-29T23:21:01Z
[ "python", "excel", "com", "namespaces", "enumeration" ]
How do I import a COM object namespace/enumeration in Python?
1,646,230
<p>I'm relatively new to programming/python, so I'd appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the code:</p> <pre><code>import win32com.client as win32 def excel(): app = 'Excel' x1 = win32.gencache.EnsureDispatch('%s.Application' % ap...
7
2009-10-29T20:08:48Z
1,647,233
<p>All of the file format constants are documented <a href="http://msdn.microsoft.com/en-us/library/bb241279.aspx" rel="nofollow">here</a></p>
1
2009-10-29T23:49:30Z
[ "python", "excel", "com", "namespaces", "enumeration" ]
How do I import a COM object namespace/enumeration in Python?
1,646,230
<p>I'm relatively new to programming/python, so I'd appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the code:</p> <pre><code>import win32com.client as win32 def excel(): app = 'Excel' x1 = win32.gencache.EnsureDispatch('%s.Application' % ap...
7
2009-10-29T20:08:48Z
1,692,413
<p>As a general rule I find it really useful to pre-write any code in the vba ide in Excel. This way you can find out all the values of constants etc that you need to use within your python code. You can also make sure stuff will work from within a more controlled environment.</p>
0
2009-11-07T08:21:05Z
[ "python", "excel", "com", "namespaces", "enumeration" ]
How do I import a COM object namespace/enumeration in Python?
1,646,230
<p>I'm relatively new to programming/python, so I'd appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the code:</p> <pre><code>import win32com.client as win32 def excel(): app = 'Excel' x1 = win32.gencache.EnsureDispatch('%s.Application' % ap...
7
2009-10-29T20:08:48Z
5,413,586
<p>This question is a bit stale, but for those reaching this page from Google (as I did) my solution was accessing the constants via the <code>win32com.client.constants</code> object instead of on the application object itself <a href="http://stackoverflow.com/questions/1646230/how-do-i-import-a-com-object-namespace-en...
8
2011-03-24T01:10:52Z
[ "python", "excel", "com", "namespaces", "enumeration" ]
psyco complains about unsupported opcode 54, what is it?
1,646,260
<p>The Psyco log output look like this:</p> <pre><code>21:08:47.56 Logging started, 10/29/09 %%%%%%%%%%%%%%%%%%%% 21:08:47.56 unsupported opcode 54 at create_l0:124 % % 21:08:47.56 unsupported opcode 54 at create_lx:228 % % </code></pre> <p>the lines in qu...
0
2009-10-29T20:13:49Z
1,646,352
<p>Did you compile with another Psyco version than you are running the script with?</p>
0
2009-10-29T20:31:35Z
[ "python", "psyco" ]
psyco complains about unsupported opcode 54, what is it?
1,646,260
<p>The Psyco log output look like this:</p> <pre><code>21:08:47.56 Logging started, 10/29/09 %%%%%%%%%%%%%%%%%%%% 21:08:47.56 unsupported opcode 54 at create_l0:124 % % 21:08:47.56 unsupported opcode 54 at create_lx:228 % % </code></pre> <p>the lines in qu...
0
2009-10-29T20:13:49Z
1,646,379
<p>Finding the name of the opcode using the number is actually pretty easy (below uses Python 2.6.2 on Ubuntu, you may get different results):</p> <pre><code>&gt;&gt;&gt; import dis &gt;&gt;&gt; dis.opname[54] 'STORE_MAP' </code></pre> <p>Of course, finding out what exactly this means is another question entirely. :...
2
2009-10-29T20:35:50Z
[ "python", "psyco" ]
Python on AIX: What are my options?
1,646,293
<p>I need to make some Python applications for a work project. The target platform is AIX 5.3.</p> <p>My question is: What version of Python should I be using?</p> <p>My requirements are:</p> <ul> <li><p>The Python version must be easy to install on the target machines. Others will do that according to instructions ...
5
2009-10-29T20:21:26Z
1,646,337
<p>I'd compile it from source myself and tell them where to download it from in the instructions</p>
2
2009-10-29T20:29:38Z
[ "python", "aix", "ncurses", "curses" ]
Python on AIX: What are my options?
1,646,293
<p>I need to make some Python applications for a work project. The target platform is AIX 5.3.</p> <p>My question is: What version of Python should I be using?</p> <p>My requirements are:</p> <ul> <li><p>The Python version must be easy to install on the target machines. Others will do that according to instructions ...
5
2009-10-29T20:21:26Z
1,646,368
<p>Use the AS Package of Python 2.6.3.7 from Activestate. They have a binary package for AIX on <a href="https://www.activestate.com/activepython/downloads/">their download site</a>.</p> <p>If you don't have an AIX machine to test it on, the install works the same way on Solaris or Linux, so you could write your docum...
6
2009-10-29T20:33:59Z
[ "python", "aix", "ncurses", "curses" ]
Python on AIX: What are my options?
1,646,293
<p>I need to make some Python applications for a work project. The target platform is AIX 5.3.</p> <p>My question is: What version of Python should I be using?</p> <p>My requirements are:</p> <ul> <li><p>The Python version must be easy to install on the target machines. Others will do that according to instructions ...
5
2009-10-29T20:21:26Z
1,646,569
<p>We've used ActiveState's Python as well as <a href="http://pware.hvcc.edu/" rel="nofollow">Pware's</a> compiled version. Both have worked well. For AS, we've used 2.5 and 2.6. For Pware, just 2.6. Both 2.5 and 2.6 from AS support curses on our machine.</p> <p>I've compiled from source but usually wind up having tro...
1
2009-10-29T21:10:35Z
[ "python", "aix", "ncurses", "curses" ]
How to deploy Python to Windows users?
1,646,326
<p>I'm soon to launch a <a href="http://www.bestsellerapp.com/">beta app</a> and this have the option to create custom integration scripts on <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>.</p> <p>The app will target <a href="http://en.wikipedia.org/wiki/Mac%5FOS%5FX">Mac OS X<...
14
2009-10-29T20:28:34Z
1,646,344
<p>You might try using <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a>. It creates a .exe file with Python already included!</p>
11
2009-10-29T20:30:41Z
[ "python", "windows", "delphi", "deployment", "lazarus" ]
How to deploy Python to Windows users?
1,646,326
<p>I'm soon to launch a <a href="http://www.bestsellerapp.com/">beta app</a> and this have the option to create custom integration scripts on <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>.</p> <p>The app will target <a href="http://en.wikipedia.org/wiki/Mac%5FOS%5FX">Mac OS X<...
14
2009-10-29T20:28:34Z
1,647,131
<p>Copy a <a href="http://www.portablepython.com/">Portable Python</a> folder out of your installer, into the same folder as your Delphi/Lazarus app. Set all paths appropriately for that.</p>
10
2009-10-29T23:16:58Z
[ "python", "windows", "delphi", "deployment", "lazarus" ]
How to deploy Python to Windows users?
1,646,326
<p>I'm soon to launch a <a href="http://www.bestsellerapp.com/">beta app</a> and this have the option to create custom integration scripts on <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>.</p> <p>The app will target <a href="http://en.wikipedia.org/wiki/Mac%5FOS%5FX">Mac OS X<...
14
2009-10-29T20:28:34Z
1,647,150
<p>Integrate the python interpreter into your Delphi app with <a href="http://python4delphi.googlecode.com/svn/trunk/" rel="nofollow">P4D</a>. These components actually work, and in both directions too (Delphi classes exposed to Python as binary extensions, and Python interpreter inside Delphi). I also saw a patch fo...
4
2009-10-29T23:21:13Z
[ "python", "windows", "delphi", "deployment", "lazarus" ]
How to deploy Python to Windows users?
1,646,326
<p>I'm soon to launch a <a href="http://www.bestsellerapp.com/">beta app</a> and this have the option to create custom integration scripts on <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29">Python</a>.</p> <p>The app will target <a href="http://en.wikipedia.org/wiki/Mac%5FOS%5FX">Mac OS X<...
14
2009-10-29T20:28:34Z
1,648,155
<p>I think there's no problem combining .EXE packaging with a tool like PyInstaller or py2exe and Python-written plugins. The created .EXE can easily detect where it's installed and the code inside can then simply <code>import</code> files from some pre-determined plugin directory. Don't forget that once you package a ...
1
2009-10-30T05:27:32Z
[ "python", "windows", "delphi", "deployment", "lazarus" ]
How to run django unit-tests on production database?
1,646,468
<p>i'm starting the TDD development attitude and am writting unit-tests for my django application. I'm aware of fixtures and know that's the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i'd like to handle...
15
2009-10-29T20:50:16Z
1,646,530
<p>First, if you're running it on the production database, it isn't much of a "unit" test.</p> <p>It's a first-class batch job and needs to be treated like a first-class production batch job.</p> <p>You cam't to use the Django <code>test</code> command for looking at production data. It always creates an empty datab...
4
2009-10-29T21:00:32Z
[ "python", "django", "unit-testing" ]
How to run django unit-tests on production database?
1,646,468
<p>i'm starting the TDD development attitude and am writting unit-tests for my django application. I'm aware of fixtures and know that's the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i'd like to handle...
15
2009-10-29T20:50:16Z
1,648,881
<p>In case someone googles here searching for the solution for a given problem, here is the skeleton on how to perform unit tests on django production database. Check the django docs section <a href="http://docs.djangoproject.com/en/dev/howto/custom-management-commands/#writing-custom-django-admin-commands">here</a>, f...
45
2009-10-30T09:47:11Z
[ "python", "django", "unit-testing" ]
How to run django unit-tests on production database?
1,646,468
<p>i'm starting the TDD development attitude and am writting unit-tests for my django application. I'm aware of fixtures and know that's the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i'd like to handle...
15
2009-10-29T20:50:16Z
1,651,882
<p>A unittest is meant to test without any sideeffects. Though your test would nothing that is known as unittest. If you want to do it anyway, you can use a custom test runner that is setting up the database (or in your case using the existing db).</p> <p>You can set the TEST_RUNNER setting in your settings.py file. T...
2
2009-10-30T19:39:05Z
[ "python", "django", "unit-testing" ]
How to run django unit-tests on production database?
1,646,468
<p>i'm starting the TDD development attitude and am writting unit-tests for my django application. I'm aware of fixtures and know that's the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i'd like to handle...
15
2009-10-29T20:50:16Z
24,076,857
<p>This TEST_RUNNER works on Django 1.3</p> <pre><code>from django.test.simple import DjangoTestSuiteRunner as TestRunner class DjangoTestSuiteRunner(TestRunner): def setup_databases(self, **kwargs): pass def teardown_databases(self, old_config, **kwargs): pass </code></pre>
2
2014-06-06T07:53:20Z
[ "python", "django", "unit-testing" ]
How to configure format of Python 2.3 logging messages?
1,646,470
<p>In Python 2.4 and later, configuring the logging module to have a more basic formatting is easy:</p> <p>logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")</p> <p>but for applications which need to support Python 2.3 it seems more difficult, because the logging API was overhauled in Py2.4. In particular...
3
2009-10-29T20:50:22Z
1,649,050
<p><code>except:</code> without exception class[es] is a good way to get in trouble. I believe <code>logging</code> module in Python 2.3 has <code>basicConfig()</code> function, but with less options. Since it accepts <code>**kwargs</code> it may fail at any moment after doing some job. I think it already installed a h...
3
2009-10-30T10:35:21Z
[ "python", "logging", "formatting", "legacy" ]
Is there a way to know if a list of elements is on a larger list without using 'in' keyword?
1,646,641
<p>I want to do this. I have two python lists, one larger than the other and I want to know is there is a way to check if the elements of the smaller list are in the big list in the exact same order for example:</p> <pre><code>small_list = [4,2,5] big_list = [1,2,5,7,2,4,2,5,67,8,5,13,45] </code></pre> <p>I tried usi...
4
2009-10-29T21:23:55Z
1,646,664
<p>There's no built in operator doing that particular comparison. I suggest a list comprehension or a quick for loop.</p>
0
2009-10-29T21:27:31Z
[ "python" ]
Is there a way to know if a list of elements is on a larger list without using 'in' keyword?
1,646,641
<p>I want to do this. I have two python lists, one larger than the other and I want to know is there is a way to check if the elements of the smaller list are in the big list in the exact same order for example:</p> <pre><code>small_list = [4,2,5] big_list = [1,2,5,7,2,4,2,5,67,8,5,13,45] </code></pre> <p>I tried usi...
4
2009-10-29T21:23:55Z
1,646,670
<p>Rather non-optimized, demonstrates the general strategy simply:</p> <pre><code>tuple(small_list) in zip(big_list[:], big_list[1:], big_list[2:]) </code></pre> <p>The funky zip thing does this:</p> <pre><code>&gt;&gt;&gt; zip(big_list[:], big_list[1:], big_list[2:]) [(1, 2, 5), (2, 5, 7), (5, 7, 2), (7, 2, 4), (2,...
3
2009-10-29T21:28:10Z
[ "python" ]
Is there a way to know if a list of elements is on a larger list without using 'in' keyword?
1,646,641
<p>I want to do this. I have two python lists, one larger than the other and I want to know is there is a way to check if the elements of the smaller list are in the big list in the exact same order for example:</p> <pre><code>small_list = [4,2,5] big_list = [1,2,5,7,2,4,2,5,67,8,5,13,45] </code></pre> <p>I tried usi...
4
2009-10-29T21:23:55Z
1,646,684
<p>This problem is trickier than it seems. Unless I'm mistaken, it's a special case of the <a href="http://en.wikipedia.org/wiki/Longest%5Fcommon%5Fsubstring%5Fproblem" rel="nofollow">longest common substring problem</a>.</p> <p>For the general case (arbitrarily large lists), I would use some kind of <a href="http://...
2
2009-10-29T21:30:19Z
[ "python" ]
Is there a way to know if a list of elements is on a larger list without using 'in' keyword?
1,646,641
<p>I want to do this. I have two python lists, one larger than the other and I want to know is there is a way to check if the elements of the smaller list are in the big list in the exact same order for example:</p> <pre><code>small_list = [4,2,5] big_list = [1,2,5,7,2,4,2,5,67,8,5,13,45] </code></pre> <p>I tried usi...
4
2009-10-29T21:23:55Z
1,646,687
<p>That's because <code>small_list in big_list</code> checks whether an element in big_list is equal to small_list. What you want to do instead is see if a slice of big_list is the same as small_list.</p> <pre><code>def isSubList(slice, L): n = len(slice) for i in range(0, len(L) - n): if slice == L[i...
2
2009-10-29T21:30:48Z
[ "python" ]
Is there a way to know if a list of elements is on a larger list without using 'in' keyword?
1,646,641
<p>I want to do this. I have two python lists, one larger than the other and I want to know is there is a way to check if the elements of the smaller list are in the big list in the exact same order for example:</p> <pre><code>small_list = [4,2,5] big_list = [1,2,5,7,2,4,2,5,67,8,5,13,45] </code></pre> <p>I tried usi...
4
2009-10-29T21:23:55Z
1,646,692
<pre><code>def in_list(small, big): l_sml = len(small) l_big = len(big) return any((big[i:i+l_sml]==small for i in xrange(l_big-l_sml+1))) print in_list([4,2,1], [1,2,3,4,2,1,0,5]) # True print in_list([1,2,3], [1,2,4]) # False </code></pre>
7
2009-10-29T21:32:04Z
[ "python" ]