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
Using ECF shared editing with Python
2,313,743
<p>I can use the shared editing feature of <a href="http://www.eclipse.org/ecf/" rel="nofollow">ECF</a> with Java fine perfectly fine. When I try to do it with Python files it also works, but there is no syntax highlighting.</p> <p>I installed PyDev to get syntax highlighting, but then the context menu does not have t...
3
2010-02-22T20:00:24Z
2,668,322
<h2>You have to add the context menu into the PyDev editor manually.</h2> <p>You can do this by setting up a plugin with the following in<code>plugin.xml</code>:</p> <pre><code>&lt;plugin&gt; &lt;extension point="org.eclipse.ui.menus"&gt; &lt;menuContribution locationURI="popup:org.python.pydev.editor.P...
2
2010-04-19T14:42:18Z
[ "python", "eclipse", "eclipse-ecf" ]
Using ECF shared editing with Python
2,313,743
<p>I can use the shared editing feature of <a href="http://www.eclipse.org/ecf/" rel="nofollow">ECF</a> with Java fine perfectly fine. When I try to do it with Python files it also works, but there is no syntax highlighting.</p> <p>I installed PyDev to get syntax highlighting, but then the context menu does not have t...
3
2010-02-22T20:00:24Z
13,278,720
<p>There's a Github project called <a href="https://github.com/gilberterik/ECF4PyDev" rel="nofollow">ECF4PyDev</a> which provides a module to perform map the ECF menu commands into PyDev. The project also includes the necessary files in order for direct installation, which makes it easy to use with the Eclipse softwar...
1
2012-11-07T21:39:41Z
[ "python", "eclipse", "eclipse-ecf" ]
Getting INVALID response from PayPal's Sandbox IPN
2,313,946
<p>I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.</p> <p>I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried...
1
2010-02-22T20:32:07Z
2,314,021
<p>It is hard to judge from what you've given me. Here are a couple of guesses/suggestions:</p> <ol> <li>You may need to encode in utf-8 the key as well as the val.</li> <li>If it is picky about the order of the keys, you should log the keys as you put them into newparams. Since you are using a dict for f, the order m...
0
2010-02-22T20:45:07Z
[ "python", "paypal", "urllib2", "paypal-ipn" ]
Getting INVALID response from PayPal's Sandbox IPN
2,313,946
<p>I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.</p> <p>I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried...
1
2010-02-22T20:32:07Z
2,316,914
<p>The order is <em>critical</em>. You <em>must</em> verify in the same order that Paypal specify. The simplest way to achieve this is to use the exact order they were provided in:</p> <pre><code>def paypal_verify(): """ Returns false if the current request cannot be verified by paypal """ # Create verify para...
2
2010-02-23T09:15:18Z
[ "python", "paypal", "urllib2", "paypal-ipn" ]
Getting INVALID response from PayPal's Sandbox IPN
2,313,946
<p>I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.</p> <p>I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried...
1
2010-02-22T20:32:07Z
2,576,671
<p>Make sure you are posting to the sandbox and not the live..</p> <pre><code>$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); </code></pre>
-1
2010-04-05T02:35:51Z
[ "python", "paypal", "urllib2", "paypal-ipn" ]
Getting INVALID response from PayPal's Sandbox IPN
2,313,946
<p>I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.</p> <p>I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried...
1
2010-02-22T20:32:07Z
8,791,737
<p>in django</p> <pre><code>import httplib2 import urllib h = httplib2.Http() params = urllib.urlencode(request.POST, True) response, content = h.request("http://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate&amp;%s" % params) </code></pre>
1
2012-01-09T16:30:51Z
[ "python", "paypal", "urllib2", "paypal-ipn" ]
Python & sql server
2,314,178
<p>What is the best way to access sql server from python is it DB-API ? Also could someone provide a such code using the DB-API how to connect to sql server from python and excute query ?</p>
25
2010-02-22T21:04:50Z
2,314,222
<p>See <a href="http://pymssql.org/" rel="nofollow">pymssql</a> It is a DB-API module for MS Sql Server and would be the most pythonic way. The documentation includes examples.</p> <p>If on a Windows OS you could also use <a href="http://msdn.microsoft.com/en-us/library/ms722784(VS.85).aspx" rel="nofollow">OLEDB</a> t...
5
2010-02-22T21:11:09Z
[ "python", "sql", "sql-server" ]
Python & sql server
2,314,178
<p>What is the best way to access sql server from python is it DB-API ? Also could someone provide a such code using the DB-API how to connect to sql server from python and excute query ?</p>
25
2010-02-22T21:04:50Z
2,314,269
<p>See <a href="http://code.google.com/p/pyodbc/">pyodbc</a> - <a href="http://www.python.org/dev/peps/pep-0249/">Python Database API Specification v2.0</a> implementation. Works great. </p> <p>Additionally you can use it with other databases. If using with SQL Server 2008, make sure you use Native Driver if you need ...
13
2010-02-22T21:16:53Z
[ "python", "sql", "sql-server" ]
Python & sql server
2,314,178
<p>What is the best way to access sql server from python is it DB-API ? Also could someone provide a such code using the DB-API how to connect to sql server from python and excute query ?</p>
25
2010-02-22T21:04:50Z
2,314,282
<p>ODBC + freetds + a python wrapper library for ODBC.</p>
1
2010-02-22T21:18:33Z
[ "python", "sql", "sql-server" ]
Python & sql server
2,314,178
<p>What is the best way to access sql server from python is it DB-API ? Also could someone provide a such code using the DB-API how to connect to sql server from python and excute query ?</p>
25
2010-02-22T21:04:50Z
15,400,163
<pre><code>Also could someone provide a such code using the DB-API how to connect to sql server from python and excute query ? </code></pre> <p><a href="https://code.google.com/p/pypyodbc/wiki/A_HelloWorld_sample_to_access_mssql_with_python" rel="nofollow">This hello world snippet</a> pretty much shows you the common...
4
2013-03-14T02:33:56Z
[ "python", "sql", "sql-server" ]
Python: Why does right shift >> round down and where should it be used?
2,314,181
<p>I've never used the <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators, not because I've never needed them, but because I don't know if I could have used them, or where I should have.</p> <p><code>100 &gt;&gt; 3</code> outputs <code>12</code> instead of <code>12.5</code>. Why is this. Perhaps learning where ...
3
2010-02-22T21:05:20Z
2,314,203
<p><code>&gt;&gt;</code> and <code>&lt;&lt;</code> are the right and left <a href="http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts" rel="nofollow">bit shift</a> operators, respectively. You should look at the binary representation of the numbers.</p> <pre><code>&gt;&gt;&gt; bin(100) '0b1100100' &gt;&gt;&gt; ...
4
2010-02-22T21:09:11Z
[ "python", "operators" ]
Python: Why does right shift >> round down and where should it be used?
2,314,181
<p>I've never used the <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators, not because I've never needed them, but because I don't know if I could have used them, or where I should have.</p> <p><code>100 &gt;&gt; 3</code> outputs <code>12</code> instead of <code>12.5</code>. Why is this. Perhaps learning where ...
3
2010-02-22T21:05:20Z
2,314,209
<p><strong>Right shift is <em>not</em> division</strong></p> <p>Let's look at what <a href="http://en.wikipedia.org/wiki/Logical_shift" rel="nofollow">right-shift</a> <em>actually</em> does, and it will become clear.</p> <p>First, recall that a number is stored in memory as a collection of binary digits. If we have 8...
13
2010-02-22T21:09:51Z
[ "python", "operators" ]
Python: Why does right shift >> round down and where should it be used?
2,314,181
<p>I've never used the <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators, not because I've never needed them, but because I don't know if I could have used them, or where I should have.</p> <p><code>100 &gt;&gt; 3</code> outputs <code>12</code> instead of <code>12.5</code>. Why is this. Perhaps learning where ...
3
2010-02-22T21:05:20Z
2,314,211
<p>Bit shifting an integer gives another integer. For instance, the number 12 is written in binary as 0b1100. If we bit shift by 1 to the right, we get 0b110 = 6. If we bit shift by 2, we get 0b11 = 3. And lastly, if we bitshift by 3, we get 0b1 = 1 rather than 1.5. This is because the bits that are shifted beyond the ...
0
2010-02-22T21:10:00Z
[ "python", "operators" ]
Python: Why does right shift >> round down and where should it be used?
2,314,181
<p>I've never used the <code>&gt;&gt;</code> and <code>&lt;&lt;</code> operators, not because I've never needed them, but because I don't know if I could have used them, or where I should have.</p> <p><code>100 &gt;&gt; 3</code> outputs <code>12</code> instead of <code>12.5</code>. Why is this. Perhaps learning where ...
3
2010-02-22T21:05:20Z
2,314,326
<p>The other answers explain the idea of bitshifting, but here's specifically what happens for 100>>3</p> <pre><code>100 128 64 32 16 8 4 2 1 0 1 1 0 0 1 0 0 = 100 100 &gt;&gt; 1 128 64 32 16 8 4 2 1 0 0 1 1 0 0 1 0 = 50 100 &gt;&gt; 2 128 64 32 16 8 4 2 1 0 0 0 1 1 0 0 1 = 25 100 &gt;&gt; 3 128 64 32...
2
2010-02-22T21:25:00Z
[ "python", "operators" ]
Using python to run other programs
2,314,206
<p>I have a command that works great on the command line. It has lots of arguments like <code>cmd --thing foo --stuff bar -a b input output</code></p> <p>I want to run this from python and block waiting for it to complete. As the script prints things to <code>stdout</code> and <code>stderr</code> I want it to be immed...
6
2010-02-22T21:09:20Z
2,314,225
<p>You have to quote each field separately, ie. split the options from their arguments.</p> <pre><code>import subprocess output = subprocess.call(["cmd", "--thing", "foo", "--stuff", "bar", "-a", "b", "input", "output"]) </code></pre> <p>otherwise you are effectively running cmd like this</p> <pre><code>$ cmd --thin...
7
2010-02-22T21:11:38Z
[ "python", "unix", "subprocess" ]
Using python to run other programs
2,314,206
<p>I have a command that works great on the command line. It has lots of arguments like <code>cmd --thing foo --stuff bar -a b input output</code></p> <p>I want to run this from python and block waiting for it to complete. As the script prints things to <code>stdout</code> and <code>stderr</code> I want it to be immed...
6
2010-02-22T21:09:20Z
2,314,242
<p>Wouldn't commands.getstatusoutput() work? It'll return your status right away pretty sure.</p>
2
2010-02-22T21:13:35Z
[ "python", "unix", "subprocess" ]
Using python to run other programs
2,314,206
<p>I have a command that works great on the command line. It has lots of arguments like <code>cmd --thing foo --stuff bar -a b input output</code></p> <p>I want to run this from python and block waiting for it to complete. As the script prints things to <code>stdout</code> and <code>stderr</code> I want it to be immed...
6
2010-02-22T21:09:20Z
2,314,245
<p>A coworker just showed me this:</p> <pre><code>import os import sys for line in os.popen("cmd --thing foo --stuff bar -a b input output", "r"): print line sys.stdout.flush() </code></pre> <p>and it is working :)</p>
1
2010-02-22T21:14:14Z
[ "python", "unix", "subprocess" ]
Using python to run other programs
2,314,206
<p>I have a command that works great on the command line. It has lots of arguments like <code>cmd --thing foo --stuff bar -a b input output</code></p> <p>I want to run this from python and block waiting for it to complete. As the script prints things to <code>stdout</code> and <code>stderr</code> I want it to be immed...
6
2010-02-22T21:09:20Z
2,315,501
<p>If you don't need to process the output in your code, only to show it to the user as it happens (it's not clear from your Q, and it seems that way from your own self-answer), simplest is:</p> <pre><code>rc = subprocess.call( ["cmd", "--thing", "foo", "--stuff", "bar", "-a", "b", "input", "output"]) print ...
3
2010-02-23T01:14:26Z
[ "python", "unix", "subprocess" ]
In model save() how to get all field starting with 'foo'
2,314,292
<p>I've this django model:</p> <pre><code>from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en = model.CharField(max_length=100) def save(self): print_all_field_starting_with('foo_') super(MyModel, self).save() </code></pre> <p>So I wa...
1
2010-02-22T21:19:53Z
2,314,356
<p>You can do:</p> <pre><code>for field in dir(self): if field.startswith('foo_'): # getting with getattr(self, field) # setting with setattr(self, field, value) </code></pre> <p>If you want to get the list of fields you can also so this:</p> <pre><code>foo_fields = [field for field in dir(self) if f...
2
2010-02-22T21:29:47Z
[ "python", "django", "django-models" ]
In model save() how to get all field starting with 'foo'
2,314,292
<p>I've this django model:</p> <pre><code>from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en = model.CharField(max_length=100) def save(self): print_all_field_starting_with('foo_') super(MyModel, self).save() </code></pre> <p>So I wa...
1
2010-02-22T21:19:53Z
2,314,358
<p>This will do the trick, though you need to also pass in the object whose fields you want to print:</p> <pre><code>import inspect def print_all_field_starting_with(prefix, object): for name, value in inspect.getmembers(object): if name.startswith(prefix): print name # or do something else </c...
2
2010-02-22T21:30:15Z
[ "python", "django", "django-models" ]
In model save() how to get all field starting with 'foo'
2,314,292
<p>I've this django model:</p> <pre><code>from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en = model.CharField(max_length=100) def save(self): print_all_field_starting_with('foo_') super(MyModel, self).save() </code></pre> <p>So I wa...
1
2010-02-22T21:19:53Z
2,314,465
<p>There is a <code>get_all_field_names()</code> method that is built into the <code>Meta</code> subclass for all models, and can be found in <code>foo._meta.get_all_field_names()</code>:</p> <pre><code>&gt;&gt;&gt; from foo.models import Foo &gt;&gt;&gt; f = Foo.objects.get(pk=1) &gt;&gt;&gt; f._meta.get_all_field_na...
2
2010-02-22T21:44:52Z
[ "python", "django", "django-models" ]
python logging to database
2,314,307
<p>I'm seeking a way to let the python logger module to log to database and falls back to file system when the db is down.</p> <p>So basically 2 things: How to let the logger log to database and how to make it fall to file logging when the db is down.</p>
15
2010-02-22T21:22:10Z
2,314,325
<p>Write yourself a <strong><a href="http://docs.python.org/library/logging.html#handlers">handler</a></strong> that directs the logs to the database in question. When it fails, you can remove it from the handler list of the logger. There are many ways to deal with the failure-modes.</p>
11
2010-02-22T21:24:45Z
[ "python", "database", "logging" ]
python logging to database
2,314,307
<p>I'm seeking a way to let the python logger module to log to database and falls back to file system when the db is down.</p> <p>So basically 2 things: How to let the logger log to database and how to make it fall to file logging when the db is down.</p>
15
2010-02-22T21:22:10Z
37,599,957
<p>Have you seen log4mongo library? <a href="https://pypi.python.org/pypi/log4mongo/" rel="nofollow">https://pypi.python.org/pypi/log4mongo/</a> It provides a handle for Mongo. The fallback is not there but I suppose it would be relatively easy to add.</p>
0
2016-06-02T18:51:50Z
[ "python", "database", "logging" ]
How to plot the lines first and points last in matplotlib
2,314,379
<p>I have a simple plot with several sets of points and lines connecting each set. I want the points to be plotted on top of the lines (so that the line doesn't show inside the point). Regardless of order of the <code>plot</code> and <code>scatter</code> calls, this plot comes out the same, and not as I'd like. Is t...
23
2010-02-22T21:34:48Z
2,314,461
<p>You need to set the Z-order.</p> <pre><code>plt.plot(R,P,color='0.2',lw=1.5, zorder=1) plt.scatter(R,P,s=150,color=c, zorder=2) </code></pre> <p>Check out this example. <a href="http://matplotlib.sourceforge.net/examples/pylab_examples/zorder_demo.html">http://matplotlib.sourceforge.net/examples/pylab_examples/zor...
35
2010-02-22T21:44:19Z
[ "python", "matplotlib" ]
Would using a WSDL to generate REST clients be the wrong direction?
2,314,399
<p>I'm out to create a fairly simple web service for intranet use. The service will ultimately be the interface to a DB that will allow me to keep track of what the various internal tools within the company are doing. I'm thinking I want a web service so that various tools (and thus different languages) within the or...
3
2010-02-22T21:37:06Z
2,314,477
<p>I don't get the code generation issue here.</p> <p>REST rarely requires any code generation of any kind. It's just HTTP requests with simple JSON (or XML) payloads. </p> <p>You use the existing HTTP libraries (e.g. Apache Commons, or Python urrlib2). You use existing JSON (or XML) libraries. (the Jersey projec...
5
2010-02-22T21:46:56Z
[ "java", "python", "perl", "rest", "soap" ]
Would using a WSDL to generate REST clients be the wrong direction?
2,314,399
<p>I'm out to create a fairly simple web service for intranet use. The service will ultimately be the interface to a DB that will allow me to keep track of what the various internal tools within the company are doing. I'm thinking I want a web service so that various tools (and thus different languages) within the or...
3
2010-02-22T21:37:06Z
2,314,731
<p>Fossill,</p> <p>I recommend that you do not bother to learn SOAP for this. Ws-* has a very high learning curve and the (unnecessary) complexity will likely eat you alive.</p> <p>Looking at your skill set (Java, Perl, Python, C++) you should be very satisfied with a REST (or at least HTTP-based) approach. And: you'...
1
2010-02-22T22:22:14Z
[ "java", "python", "perl", "rest", "soap" ]
Would using a WSDL to generate REST clients be the wrong direction?
2,314,399
<p>I'm out to create a fairly simple web service for intranet use. The service will ultimately be the interface to a DB that will allow me to keep track of what the various internal tools within the company are doing. I'm thinking I want a web service so that various tools (and thus different languages) within the or...
3
2010-02-22T21:37:06Z
2,314,869
<p>The code-generation aspect that you value is a maintenance item, but I question its real worth.</p> <p>Be it thru a WSDL document or your own grammar documentation for a REST-style implementations, clients have to comply to the published interface. The WS/SOAP (code-generation) development model might have more too...
1
2010-02-22T22:46:37Z
[ "java", "python", "perl", "rest", "soap" ]
Would using a WSDL to generate REST clients be the wrong direction?
2,314,399
<p>I'm out to create a fairly simple web service for intranet use. The service will ultimately be the interface to a DB that will allow me to keep track of what the various internal tools within the company are doing. I'm thinking I want a web service so that various tools (and thus different languages) within the or...
3
2010-02-22T21:37:06Z
2,315,911
<p>FYI - REST does have a WSDL-like auto-generation schema definition called <a href="http://en.wikipedia.org/wiki/Web_Application_Description_Language" rel="nofollow">WADL</a>. But almost no one uses it. </p>
1
2010-02-23T03:26:52Z
[ "java", "python", "perl", "rest", "soap" ]
Would using a WSDL to generate REST clients be the wrong direction?
2,314,399
<p>I'm out to create a fairly simple web service for intranet use. The service will ultimately be the interface to a DB that will allow me to keep track of what the various internal tools within the company are doing. I'm thinking I want a web service so that various tools (and thus different languages) within the or...
3
2010-02-22T21:37:06Z
4,664,274
<p><a href="http://cxf.apache.org" rel="nofollow">Apache CXF</a> has Java support for REST clients that gives you the same sorts of 'code generation' advantages, in many cases, as full SOAP.</p>
1
2011-01-12T00:34:31Z
[ "java", "python", "perl", "rest", "soap" ]
How can I use the Whirlpool hash with Django authentication?
2,314,405
<p>We have a system written in PHP where account passwords are stored as the first 128 chars of a whirlpool hash of the password.</p> <p>I'd like to transition to handling the logins with Django without changing the database or asking users to change their passwords. Also, I'd prefer to stick with whirlpool vs. the le...
2
2010-02-22T21:38:12Z
2,314,427
<p>Basically you want to write your own authentication back-end. Fortunately, this can be done very easily.</p> <p>It's as easy as:</p> <pre><code>class MyBackend: def authenticate(self, username=None, password=None): # Check the username/password and return a User. </code></pre> <p>Then all you need to ...
2
2010-02-22T21:40:40Z
[ "python", "django", "sha1", "authentication" ]
Efficiently reading a csv file with windows newline on linux in Python
2,314,501
<p>The following is working under windows for reading csv files line by line.</p> <pre><code>f = open(filename, 'r') for line in f: </code></pre> <p>Though when copying the csv file to a linux server, it fails.</p> <p>It should be mentioned that performance is an issue as the csv files are huge. I am therefore conc...
2
2010-02-22T21:50:25Z
2,314,520
<p>The dos2unix utility will do this very efficiently. If the files are that large I would run that command as part of the copy.</p>
1
2010-02-22T21:52:58Z
[ "python", "csv", "python-3.x" ]
Efficiently reading a csv file with windows newline on linux in Python
2,314,501
<p>The following is working under windows for reading csv files line by line.</p> <pre><code>f = open(filename, 'r') for line in f: </code></pre> <p>Though when copying the csv file to a linux server, it fails.</p> <p>It should be mentioned that performance is an issue as the csv files are huge. I am therefore conc...
2
2010-02-22T21:50:25Z
2,314,565
<p>Python has builtin support for Windows, Linux and Mac line endings:</p> <pre><code>f = open(filename, 'rtU') for line in f: ... </code></pre> <p>If you really want don't want slow string operations, you should strip the files before processing them. You can either use dos2unix (can be found in the Debian pack...
6
2010-02-22T21:58:49Z
[ "python", "csv", "python-3.x" ]
Efficiently reading a csv file with windows newline on linux in Python
2,314,501
<p>The following is working under windows for reading csv files line by line.</p> <pre><code>f = open(filename, 'r') for line in f: </code></pre> <p>Though when copying the csv file to a linux server, it fails.</p> <p>It should be mentioned that performance is an issue as the csv files are huge. I am therefore conc...
2
2010-02-22T21:50:25Z
2,314,579
<p>If performance is important, why are you not using <a href="http://docs.python.org/library/csv.html" rel="nofollow"><code>csv.reader</code></a>?</p>
6
2010-02-22T22:00:44Z
[ "python", "csv", "python-3.x" ]
Efficiently reading a csv file with windows newline on linux in Python
2,314,501
<p>The following is working under windows for reading csv files line by line.</p> <pre><code>f = open(filename, 'r') for line in f: </code></pre> <p>Though when copying the csv file to a linux server, it fails.</p> <p>It should be mentioned that performance is an issue as the csv files are huge. I am therefore conc...
2
2010-02-22T21:50:25Z
2,314,587
<p>Ummm .... You have csv files, you are using Python, why not read the files using the Python csv module?</p>
4
2010-02-22T22:02:10Z
[ "python", "csv", "python-3.x" ]
Efficiently reading a csv file with windows newline on linux in Python
2,314,501
<p>The following is working under windows for reading csv files line by line.</p> <pre><code>f = open(filename, 'r') for line in f: </code></pre> <p>Though when copying the csv file to a linux server, it fails.</p> <p>It should be mentioned that performance is an issue as the csv files are huge. I am therefore conc...
2
2010-02-22T21:50:25Z
2,314,884
<p>Actually, the most <em>efficient</em> way to read any file is in one big I/O. There isn't always enough RAM to do that, but the less I/Os the better.</p>
0
2010-02-22T22:49:53Z
[ "python", "csv", "python-3.x" ]
Python daemon to watch a folder and update a database
2,314,892
<p>This is specifically geared towards managing MP3 files, but it should easily work for any directory structure with a lot of files.</p> <p>I want to find or write a daemon (preferably in Python) that will watch a folder with many subfolders that should all contain X number of MP3 files. Any time a file is added, upd...
4
2010-02-22T22:51:56Z
2,314,930
<p>If you use Linux, you can use <a href="http://trac.dbzteam.org/pyinotify" rel="nofollow">PyInotify</a>.</p> <p><a href="http://en.wikipedia.org/wiki/Inotify" rel="nofollow"><code>inotify</code></a> can notify you about filesystem events when your program is running.</p>
3
2010-02-22T22:58:26Z
[ "python", "database", "filesystems", "daemon" ]
Python daemon to watch a folder and update a database
2,314,892
<p>This is specifically geared towards managing MP3 files, but it should easily work for any directory structure with a lot of files.</p> <p>I want to find or write a daemon (preferably in Python) that will watch a folder with many subfolders that should all contain X number of MP3 files. Any time a file is added, upd...
4
2010-02-22T22:51:56Z
2,314,950
<p>IMO, the best media player that has these features is <a href="http://en.wikipedia.org/wiki/Winamp" rel="nofollow">Winamp</a>. It rescans the music folders every X minutes, which is enough for music (but of course a little less efficient than letting the operating system watch for changes).</p> <p>But as you were a...
0
2010-02-22T23:01:52Z
[ "python", "database", "filesystems", "daemon" ]
Python daemon to watch a folder and update a database
2,314,892
<p>This is specifically geared towards managing MP3 files, but it should easily work for any directory structure with a lot of files.</p> <p>I want to find or write a daemon (preferably in Python) that will watch a folder with many subfolders that should all contain X number of MP3 files. Any time a file is added, upd...
4
2010-02-22T22:51:56Z
2,315,452
<p>Another answer already suggested <a href="http://trac.dbzteam.org/pyinotify">pyinotify</a> for Linux, let me add <a href="http://timgolden.me.uk/python/downloads/watch_directory.py">watch_directory</a> for Windows (a good discussion of the possibilities in Windows is <a href="http://timgolden.me.uk/python/win32_how_...
8
2010-02-23T00:59:23Z
[ "python", "database", "filesystems", "daemon" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,049
<p>Sort the list of dates and iterate over it, remembering the previous entry. If the difference between the previous and current entry is more than one day, you have missing days.</p> <p>Here's one way to implement it:</p> <pre><code>from datetime import date, timedelta from itertools import tee, izip def pairwise(...
2
2010-02-22T23:22:27Z
[ "python" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,052
<p>Put the dates in a <code>set</code> and then iterate from the first date to the last using <code>datetime.timedelta()</code>, checking for containment in the set each time.</p>
1
2010-02-22T23:23:01Z
[ "python" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,106
<pre><code>&gt;&gt;&gt; from datetime import datetime, timedelta &gt;&gt;&gt; date_list = [datetime(2010, 2, 23),datetime(2010, 2, 24),datetime(2010, 2, 25),datetime(2010, 2, 26),datetime(2010, 3, 1),datetime(2010, 3, 2)] &gt;&gt;&gt; &gt;&gt;&gt; date_set=set(date_list) # for faster membership tests than list...
2
2010-02-22T23:33:45Z
[ "python" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,195
<pre><code>import datetime DAY = datetime.timedelta(days=1) # missing dates: a list of [start_date, end) missing = [(d1+DAY, d2) for d1, d2 in zip(dates, dates[1:]) if (d2 - d1) &gt; DAY] def date_range(start_date, end, step=DAY): d = start_date while d &lt; end: yield d d += step missing_dat...
0
2010-02-22T23:52:48Z
[ "python" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,230
<p>Using a list comprehension</p> <pre><code>&gt;&gt;&gt; from datetime import date, timedelta &gt;&gt;&gt; d = [date(2010, 2, 23),date(2010, 2, 24),date(2010, 2, 25),date(2010, 2, 26),date(2010, 3, 1),date(2010, 3, 2)] &gt;&gt;&gt; date_set=set(d) &gt;&gt;&gt; missing = [x for x in (d[0]+timedelta(x) for x in range((...
0
2010-02-23T00:01:25Z
[ "python" ]
How do I find missing dates in a list of sorted dates?
2,315,032
<p>In Python how do I find all the missing days in a sorted list of dates?</p>
8
2010-02-22T23:18:29Z
2,315,279
<p>using sets</p> <pre><code>&gt;&gt;&gt; from datetime import date, timedelta &gt;&gt;&gt; d = [date(2010, 2, 23), date(2010, 2, 24), date(2010, 2, 25), date(2010, 2, 26), date(2010, 3, 1), date(2010, 3, 2)] &gt;&gt;&gt; date_set = set(d[0] + timedelta(x) for x in range((d[-1] - d[0]).days)) &gt;&gt;&gt; mis...
13
2010-02-23T00:11:40Z
[ "python" ]
How to generate a module object from a code object in Python
2,315,044
<p>Given that I have the code object for a module, how do I get the corresponding module object?</p> <p>It looks like <code>moduleNames = {}; exec code in moduleNames</code> does something very close to what I want. It returns the globals declared in the module into a dictionary. But if I want the actual module object...
11
2010-02-22T23:21:25Z
2,315,369
<p>As a comment already indicates, in today's Python the preferred way to instantiate types that don't have built-in names is to call the type obtained via the <a href="http://docs.python.org/library/types.html?highlight=types#module-types">types</a> module from the standard library:</p> <pre><code>&gt;&gt;&gt; import...
15
2010-02-23T00:34:56Z
[ "python", "bytecode" ]
How can I determine if instance of class from Django model is subclass of another model?
2,315,047
<p>I have a class called <code>BankAccount</code> as base class. I also have <code>CheckingAccount</code> and <code>SavingsAccount</code> classes that inherit from <code>BankAccount</code>.</p> <p>BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.</p> <p>Then, I e...
8
2010-02-22T23:21:58Z
2,315,059
<p>Try to use the <code>checkingaccount</code> and <code>savingsaccount</code> attributes. The one it is will not blow up.</p>
8
2010-02-22T23:24:05Z
[ "python", "django", "inheritance", "django-models", "subclass" ]
How can I determine if instance of class from Django model is subclass of another model?
2,315,047
<p>I have a class called <code>BankAccount</code> as base class. I also have <code>CheckingAccount</code> and <code>SavingsAccount</code> classes that inherit from <code>BankAccount</code>.</p> <p>BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.</p> <p>Then, I e...
8
2010-02-22T23:21:58Z
2,315,071
<p>A little janky, but this would work:</p> <pre><code>&gt;&gt;&gt; class BankAccount(object): pass ... &gt;&gt;&gt; class SavingsAccount(BankAccount): pass ... &gt;&gt;&gt; class CheckingAccount(BankAccount): pass ... &gt;&gt;&gt; x = SavingsAccount() &gt;&gt;&gt; type(x) == type(SavingsAccount()) True &gt;&gt;&gt; t...
-2
2010-02-22T23:27:00Z
[ "python", "django", "inheritance", "django-models", "subclass" ]
How can I determine if instance of class from Django model is subclass of another model?
2,315,047
<p>I have a class called <code>BankAccount</code> as base class. I also have <code>CheckingAccount</code> and <code>SavingsAccount</code> classes that inherit from <code>BankAccount</code>.</p> <p>BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.</p> <p>Then, I e...
8
2010-02-22T23:21:58Z
2,315,254
<p>You could use <code><a href="http://www.python.org/doc/2.3/lib/built-in-funcs.html" rel="nofollow">isinstance</a>(account, SavingsAccount)</code>, but is <a href="http://www.canonical.org/~kragen/isinstance/" rel="nofollow">generally preferred to avoid it</a> and use <a href="http://www.objectcommando.com/blog/?p=55...
2
2010-02-23T00:06:45Z
[ "python", "django", "inheritance", "django-models", "subclass" ]
How can I determine if instance of class from Django model is subclass of another model?
2,315,047
<p>I have a class called <code>BankAccount</code> as base class. I also have <code>CheckingAccount</code> and <code>SavingsAccount</code> classes that inherit from <code>BankAccount</code>.</p> <p>BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.</p> <p>Then, I e...
8
2010-02-22T23:21:58Z
2,315,318
<p>Add a GetAccountType() method to your Checking and Savings accounts, when you get the object back from BankAccount.objects.get() then call that, if everything that derives from BankAccount has that method then you'll be fine.</p>
0
2010-02-23T00:24:28Z
[ "python", "django", "inheritance", "django-models", "subclass" ]
How can I determine if instance of class from Django model is subclass of another model?
2,315,047
<p>I have a class called <code>BankAccount</code> as base class. I also have <code>CheckingAccount</code> and <code>SavingsAccount</code> classes that inherit from <code>BankAccount</code>.</p> <p>BankAccount is not an abstract class but I do not create an object from it, only the inheriting classes.</p> <p>Then, I e...
8
2010-02-22T23:21:58Z
18,807,130
<p>After some more searching I found solutions akin to this: <a href="http://stackoverflow.com/questions/12411232/django-multi-table-inheritance-how-to-know-which-is-the-child-class-of-a-model">Django multi-table inheritance, how to know which is the child class of a model?</a></p> <p>Basically, there's no elegant sol...
0
2013-09-14T22:42:12Z
[ "python", "django", "inheritance", "django-models", "subclass" ]
Why does Django's ModelForm validation think that this is invalid?
2,315,086
<p>I have what looks to me to be a pretty simple setup of model, modelform and a view playing with the two. The only hitch is that the model has a user property that can't be POSTed to the form, rather it should be populated by <code>request.user</code> so I have this:</p> <pre><code># models.py class Update(models.M...
2
2010-02-22T23:30:33Z
2,315,758
<p>You should try excluding the user field in the form</p> <pre><code>class UpdateForm(ModelForm): name = forms.CharField( max_length=140, required=False, widget=forms.TextInput(attrs={"class": "blankable"}) ) class Meta: model = Update exclude = ("user",) </code></pre> <p><a href="http://d...
1
2010-02-23T02:25:17Z
[ "python", "django" ]
Python: Asynchronous http requests sent in order with automatic handling of cookies?
2,315,151
<p>I am coding a python (2.6) interface to a web service. I need to communicate via http so that :</p> <ul> <li><em>Cookies</em> are handled automatically,</li> <li>The requests are <em>asynchronous</em>,</li> <li>The <em>order</em> in which the requests are sent is respected (the order in which the responses to these...
1
2010-02-22T23:43:52Z
2,317,612
<blockquote> <p>Using httplib and urllib2, the requests are synchronous unless I use thread, in which case the order is not guaranteed to be respected</p> </blockquote> <p>How would you know that the order has been respected unless you get your response back from the first connection before you send the respon...
2
2010-02-23T11:22:10Z
[ "python", "http", "asynchronous", "cookies" ]
Allowing users to delete their own comments in Django
2,315,187
<p>I am using the delete() function from django.contrib.comments.views.moderation module. The staff-member is allowed to delete ANY comment posts, which is completely fine. However, I would also like to give registered non-staff members the privilege to delete their OWN comment posts, and their OWN only. How can I acco...
6
2010-02-22T23:51:30Z
2,315,316
<p>If you want to mark the comment as deleted, just as <code>django.contrib.comments.views.moderation.delete()</code> does:</p> <pre><code>from django.contrib.auth.decorators import login_required from django.contrib.comments.models import Comment from django.shortcuts import get_object_or_404 from django.conf import ...
4
2010-02-23T00:24:19Z
[ "python", "django", "django-comments" ]
Allowing users to delete their own comments in Django
2,315,187
<p>I am using the delete() function from django.contrib.comments.views.moderation module. The staff-member is allowed to delete ANY comment posts, which is completely fine. However, I would also like to give registered non-staff members the privilege to delete their OWN comment posts, and their OWN only. How can I acco...
6
2010-02-22T23:51:30Z
2,482,375
<p>While this is a little late can't you do the same thing similarly in the template?</p> <pre><code>{% if user == comment.user %} &lt;a href="{% url comments-delete comment.id %}"&gt;delete comment&lt;/a&gt; {% endif %} </code></pre> <p>This uses django's comments URL: </p> <pre><code>url(r'^delete/(\d+)/$',...
1
2010-03-20T08:03:35Z
[ "python", "django", "django-comments" ]
Allowing users to delete their own comments in Django
2,315,187
<p>I am using the delete() function from django.contrib.comments.views.moderation module. The staff-member is allowed to delete ANY comment posts, which is completely fine. However, I would also like to give registered non-staff members the privilege to delete their OWN comment posts, and their OWN only. How can I acco...
6
2010-02-22T23:51:30Z
3,269,190
<p>I just ran into this problem.</p> <p>Just re-implementing the logic in comments app's delete view would couple your implementation to that specific version of the comments app. For example the comment app actual also handles signals when you mark something as deleted and the provided version doesn't do that.</p> ...
3
2010-07-16T22:07:06Z
[ "python", "django", "django-comments" ]
UTF-8 and upper()
2,315,451
<p>I want to transform UTF-8 strings using built-in functions such as upper() and capitalize().</p> <p>For example:</p> <pre><code>&gt;&gt;&gt; mystring = "işğüı" &gt;&gt;&gt; print mystring.upper() Işğüı # should be İŞĞÜI instead. </code></pre> <p>How can I fix this?</p>
6
2010-02-23T00:59:09Z
2,315,457
<p>Do not perform actions on encoded strings; decode to <code>unicode</code> first.</p> <pre><code>&gt;&gt;&gt; mystring = "işğüı" &gt;&gt;&gt; print mystring.decode('utf-8').upper() IŞĞÜI </code></pre>
12
2010-02-23T01:00:23Z
[ "python", "case-sensitive" ]
UTF-8 and upper()
2,315,451
<p>I want to transform UTF-8 strings using built-in functions such as upper() and capitalize().</p> <p>For example:</p> <pre><code>&gt;&gt;&gt; mystring = "işğüı" &gt;&gt;&gt; print mystring.upper() Işğüı # should be İŞĞÜI instead. </code></pre> <p>How can I fix this?</p>
6
2010-02-23T00:59:09Z
2,315,479
<p>It's actually best, as a general strategy, to always keep your text as Unicode once it's in memory: decode it at the moment it's input, and encode it exactly at the moment you need to output it, if there are specific encoding requirements at input and/or input times.</p> <p>Even if you don't choose to adopt this ge...
6
2010-02-23T01:05:43Z
[ "python", "case-sensitive" ]
In Django, why does this error say that my column can't be null?
2,315,506
<pre><code>picture_url = models.CharField(max_length=2000) </code></pre>
1
2010-02-23T01:16:00Z
2,315,523
<p>By default fields have <code>null</code> set to <code>False</code> try this</p> <pre><code>picture_url = models.CharField(max_length=2000, blank=True) </code></pre> <p><a href="http://docs.djangoproject.com/en/1.1/ref/models/fields/#null" rel="nofollow">http://docs.djangoproject.com/en/1.1/ref/models/fields/#null<...
0
2010-02-23T01:20:28Z
[ "python", "mysql", "database", "django" ]
In Django, why does this error say that my column can't be null?
2,315,506
<pre><code>picture_url = models.CharField(max_length=2000) </code></pre>
1
2010-02-23T01:16:00Z
2,315,531
<p>You'll need to add <code>null=True</code> if the column can be null:</p> <pre><code>picture_url = models.CharField(max_length=2000, null=True) </code></pre>
3
2010-02-23T01:21:45Z
[ "python", "mysql", "database", "django" ]
In Python, how do I loop through the dictionary and change the value if it equals something?
2,315,520
<p>If the value is None, I'd like to change it to "" (empty string).</p> <p>I start off like this, but I forget:</p> <pre><code>for k, v in mydict.items(): if v is None: ... right? </code></pre>
23
2010-02-23T01:19:42Z
2,315,529
<pre><code>for k, v in mydict.iteritems(): if v is None: mydict[k] = '' </code></pre> <p>In a more general case, e.g. if you were adding or removing keys, it might not be safe to change the structure of the container you're looping on -- so using <code>items</code> to loop on an independent list copy there...
71
2010-02-23T01:21:31Z
[ "python", "dictionary" ]
In Python, how do I loop through the dictionary and change the value if it equals something?
2,315,520
<p>If the value is None, I'd like to change it to "" (empty string).</p> <p>I start off like this, but I forget:</p> <pre><code>for k, v in mydict.items(): if v is None: ... right? </code></pre>
23
2010-02-23T01:19:42Z
2,315,545
<p>Comprehensions are usually faster, and this has the advantage of not editing <code>mydict</code> during the iteration:</p> <pre><code>mydict = dict((k, v if v else '') for k, v in mydict.items()) </code></pre>
5
2010-02-23T01:27:43Z
[ "python", "dictionary" ]
In Python, how do I loop through the dictionary and change the value if it equals something?
2,315,520
<p>If the value is None, I'd like to change it to "" (empty string).</p> <p>I start off like this, but I forget:</p> <pre><code>for k, v in mydict.items(): if v is None: ... right? </code></pre>
23
2010-02-23T01:19:42Z
2,315,715
<p>You could create a dict comprehension of just the elements whose values are None, and then update back into the original:</p> <pre><code>tmp = dict((k,"") for k,v in mydict.iteritems() if v is None) mydict.update(tmp) </code></pre> <p><em><strong>Update</em></strong> - did some performance tests</p> <p>Well, afte...
7
2010-02-23T02:16:01Z
[ "python", "dictionary" ]
pyplot.ginput() causes axes to change?
2,315,656
<p>I am encountering some strange behavior with using the matplotlib.pyplot ginput() function to store clicked points. On the first click, the ranges of the axes of the clicked image change to add 200 on each side. The image remains with this border of whitespace until something new is plotted.</p> <p>Example code:</p...
2
2010-02-23T01:56:26Z
2,315,755
<p>Try </p> <pre><code>plt.imshow(im1) plt.axis('image') x = plt.ginput(4) </code></pre> <p>I learned this <a href="http://old.nabble.com/using-ginput-with-images----axes-are-resized--td20270943.html" rel="nofollow">here</a>.</p>
3
2010-02-23T02:24:17Z
[ "python", "matplotlib" ]
How to sort a collection of objects by an variable they all hold?
2,315,659
<p>I have a class named Individual, which has a variable, self.fitness. I have a collection of these Individual instances and I'd like to sort them by their fitness. How is this done in python?</p>
1
2010-02-23T01:57:39Z
2,315,662
<pre><code>from operator import attrgetter sorted(item_list, key=attrgetter('fitness')) </code></pre> <p>item_list can be any iterable. Here is an example</p> <pre><code>&gt;&gt;&gt; class C(object): ... def __init__(self, fitness): ... self.fitness=fitness ... def __repr__(self): ... return "...
6
2010-02-23T01:58:18Z
[ "python", "sorting" ]
Unescape a string inside a string
2,315,889
<p>I am working with <code>urllib2</code>, and trying to extract the headers in a printable form from a <code>Response</code> object.</p> <p>Presently I am printing <code>str(response.info())</code>, however what is printed, is itself a Python string (at least to my understanding).</p> <pre><code>(Pdb) p str(response...
1
2010-02-23T03:18:31Z
2,315,907
<p><code>response.info()</code> returns a <code>httplib.HTTPMessage</code>, which behaves like a mapping:</p> <pre><code>info = response.info() for k, v in info.items(): print '%s: %s' % (k, v) </code></pre> <p>So in short, you're doing it wrong.</p>
0
2010-02-23T03:26:11Z
[ "python", "string", "http" ]
Unescape a string inside a string
2,315,889
<p>I am working with <code>urllib2</code>, and trying to extract the headers in a printable form from a <code>Response</code> object.</p> <p>Presently I am printing <code>str(response.info())</code>, however what is printed, is itself a Python string (at least to my understanding).</p> <pre><code>(Pdb) p str(response...
1
2010-02-23T03:18:31Z
2,315,910
<p>From pdb, this should work:</p> <pre><code>print str(response.info()) </code></pre> <p>Not sure if that answers your question, though.</p>
1
2010-02-23T03:26:51Z
[ "python", "string", "http" ]
Unescape a string inside a string
2,315,889
<p>I am working with <code>urllib2</code>, and trying to extract the headers in a printable form from a <code>Response</code> object.</p> <p>Presently I am printing <code>str(response.info())</code>, however what is printed, is itself a Python string (at least to my understanding).</p> <pre><code>(Pdb) p str(response...
1
2010-02-23T03:18:31Z
2,315,914
<p><code>str(info())</code> <em>does</em> give a normal string:</p> <pre><code>&gt;&gt;&gt; import urllib2 &gt;&gt;&gt; f = urllib2.urlopen('http://tejp.de') &gt;&gt;&gt; print str(f.info()) Connection: close Vary: Accept-Encoding Content-Type: text/html Accept-Ranges: bytes ETag: "-807357257" Last-Modified: Wed, 01 J...
2
2010-02-23T03:27:22Z
[ "python", "string", "http" ]
Get number of modified rows after sqlite3 execute
2,316,003
<p>When performing SQL statements such as <code>UPDATE</code>, and <code>INSERT</code>, the usual <code>.fetch*()</code> methods on the <code>Cursor</code> instance obviously don't apply to the number of <strong>rows modified</strong>.</p> <p>In the event of executing one of the aforementioned statements, what is the ...
5
2010-02-23T03:55:19Z
2,316,070
<p>After calling your <code>Cursor.execute*()</code> methods with your UPDATE or INSERT statements you can use <code>Cursor.rowcount</code> to see the # of rows affected by the execute call.</p> <p>If I had to guess I would say the python lib is calling <a href="http://www.sqlite.org/c3ref/changes.html"><code>int sqli...
12
2010-02-23T04:16:09Z
[ "python", "c", "sqlite3" ]
How to print line breaks in Python Django template
2,316,028
<pre><code>{'quotes': u'Live before you die.\n\n"Dream as if you\'ll live forever, live as if you\'ll die today"\n\n"Love one person, take care of them until you die. You know, raise kids. Have a good life. Be a good friend. Try to be completely who you are, figure out what you personally love and go after it with ever...
8
2010-02-23T04:03:34Z
2,316,038
<p>Use the <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#linebreaks"><code>linebreaks</code></a> filter.</p>
26
2010-02-23T04:07:33Z
[ "python", "html", "django", "templates" ]
python egg development environment setup
2,316,052
<p>I inherited a python project, which has been packaged as egg. Upon check out through SVN, I am seeing package content as:</p> <pre><code>__init__.py scripts/ ptools/ setup.py ... </code></pre> <p>Here, ptools/ hold the source of various modules. scripts/ is bunch of end-user tools that make use of modules provided...
1
2010-02-23T04:10:06Z
2,316,073
<p>You can use the PYTHONPATH environment variable to customize the locations Python searches for modules.</p>
0
2010-02-23T04:16:31Z
[ "python", "setuptools", "egg" ]
python egg development environment setup
2,316,052
<p>I inherited a python project, which has been packaged as egg. Upon check out through SVN, I am seeing package content as:</p> <pre><code>__init__.py scripts/ ptools/ setup.py ... </code></pre> <p>Here, ptools/ hold the source of various modules. scripts/ is bunch of end-user tools that make use of modules provided...
1
2010-02-23T04:10:06Z
2,320,763
<p>I think I have found the solution to my problem, and this has been answered in the following post. "setup.py develop" seems to be the perfect solution</p> <p><a href="http://stackoverflow.com/questions/1893598/pythonpath-vs-sys-path/1893622#1893622">http://stackoverflow.com/questions/1893598/pythonpath-vs-sys-path/...
1
2010-02-23T18:36:39Z
[ "python", "setuptools", "egg" ]
How to create a dictionary of integer pairs from a file in python
2,316,150
<p>If I have a file of pairs of integer IDs, followed by a value, I'd like to create this into a dictionary. Each separate term is separated by a newline. I want to make sure these are all held as ints. How can I do this?</p> <p>edit: as requested, a sample.</p> <pre><code>9 120 10 12 11 4 12 1 13 515 14 32 </code...
2
2010-02-23T04:43:01Z
2,316,175
<pre><code>d={} f=open("file") for line in f: a,b=map( int, line.split() ) d[a]=b f.close() print d </code></pre> <p>output</p> <pre><code>$ cat file 9 120 10 12 11 4 12 1 13 515 14 32 $ ./python.py {9: 120, 10: 12, 11: 4, 12: 1, 13: 515, 14: 32} </code></pre>
3
2010-02-23T04:50:12Z
[ "python" ]
GAE User registry without Google Accounts, want to restric to specific domain
2,316,255
<p>I am planning on creating an application for the students of my school, and I want to restrict user registration to emails of the form person@myschool.edu. I would prefer to not manually create the user table and do the password hashing and such. Are there any libraries you can recommend for this?</p> <p>Thanks for...
0
2010-02-23T05:12:02Z
4,908,493
<p>Since you said you don't know how the email are registered, that you don't want to manage a login/password database and you just need a regexp or somethings (I quote here!), I assume you could keep it very simple. Something like.</p> <pre><code>user = users.get_current_user() if user: emailDomain = user.email...
0
2011-02-05T17:49:33Z
[ "python", "google-app-engine", "web-applications" ]
GAE User registry without Google Accounts, want to restric to specific domain
2,316,255
<p>I am planning on creating an application for the students of my school, and I want to restrict user registration to emails of the form person@myschool.edu. I would prefer to not manually create the user table and do the password hashing and such. Are there any libraries you can recommend for this?</p> <p>Thanks for...
0
2010-02-23T05:12:02Z
5,254,880
<p>Sometimes, if you just send the user to a login screen you will end in a redirect loop if the user is already logged with a Google Account. What i have found to be a good answer to this problem is to redirect the user to a log out page so he can later login with the domain you want.</p> <p>I have used this for my c...
2
2011-03-10T02:44:58Z
[ "python", "google-app-engine", "web-applications" ]
How to get the original TCP connection hostname in Python Twisted?
2,316,365
<p>With Twisted's TCP mechanisms, when a protocol is created, the only information about the peer is its IP address and port. How can I retrieve the original hostname that I tried to connect with?</p> <pre><code>reactor.connectTCP('somehost.com', 80, MyFactory) </code></pre> <p>How can I ever get <code>'somehost.com'...
1
2010-02-23T05:40:35Z
2,317,632
<p>The simple answer is, "Record it yourself".</p> <p>Updating your example:</p> <pre><code>myfactory = MyFactory(connecthost='somehost.com') reactor.connectTCP(myfactory.connecthost, 80, myfactory) </code></pre> <p>If it's an important piece of information, you should be explicitly passing it around explicitly, in ...
2
2010-02-23T11:25:24Z
[ "python", "tcp", "twisted" ]
How to get the original TCP connection hostname in Python Twisted?
2,316,365
<p>With Twisted's TCP mechanisms, when a protocol is created, the only information about the peer is its IP address and port. How can I retrieve the original hostname that I tried to connect with?</p> <pre><code>reactor.connectTCP('somehost.com', 80, MyFactory) </code></pre> <p>How can I ever get <code>'somehost.com'...
1
2010-02-23T05:40:35Z
2,321,053
<p>Jerub's answer makes sense semantically. After digging through Twisted code, there is a more expedient and direct way of doing specifically what I'm trying to achieve.</p> <p>In protocol:</p> <pre><code>def connectionMade(self): # This is the original connector that connectTCP returned connector = self.tra...
3
2010-02-23T19:28:21Z
[ "python", "tcp", "twisted" ]
How do I print to the OS's default printer in Python 3 (cross platform)?
2,316,368
<p>I have a Python 3 script that is going to be doing some regex substitution on some Rich Text Files (rtf) and I would like to be able to print out a whole directory's files on Windows, Linux, and Mac. I have done quite a bit of searching to no avail.</p> <p>Thanks in advance.</p>
9
2010-02-23T05:41:42Z
2,316,420
<p>There is no cross-platform way. On Linux and OS X you can invoke <a href="http://developer.apple.com/Mac/library/documentation/Darwin/Reference/ManPages/man1/lpr.1.html"><code>lpr(1)</code></a> via <a href="http://docs.python.org/library/subprocess.html"><code>subprocess</code></a> and CUPS will handle the document ...
5
2010-02-23T05:57:08Z
[ "python", "printing", "python-3.x" ]
trouble writing to file in "for line in" iterator in Python script
2,316,452
<p>This seems to be an embarrassingly simple question, but, after a day of reading over How-To's and manuals, it must be asked.</p> <p>I'm writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a si...
0
2010-02-23T07:39:44Z
2,316,503
<p>It appears that the for loop is only executing once.</p> <p>I'm not sure why it would do that - perhaps there is a problem with the line endings so the whole file is being read in as a single line.</p> <p>For example, if the lines of the input script file has <code>\r</code> line ending and python is expecting <co...
1
2010-02-23T07:53:13Z
[ "python", "file-io" ]
trouble writing to file in "for line in" iterator in Python script
2,316,452
<p>This seems to be an embarrassingly simple question, but, after a day of reading over How-To's and manuals, it must be asked.</p> <p>I'm writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a si...
0
2010-02-23T07:39:44Z
2,316,525
<p>my guess is that when you iterate over the lines in the input files (in the inner loops), the whole file is read as a single line, therefore there is only a single iteration.</p> <p>Not sure, but a possible cause might be mixing Windows/Linux files and python interpreters ("\r\n" vs "\n" <a href="http://en.wikipedi...
0
2010-02-23T07:58:38Z
[ "python", "file-io" ]
trouble writing to file in "for line in" iterator in Python script
2,316,452
<p>This seems to be an embarrassingly simple question, but, after a day of reading over How-To's and manuals, it must be asked.</p> <p>I'm writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a si...
0
2010-02-23T07:39:44Z
2,316,533
<p>Maybe you mixed up tabs and spaces in your file and therefore the indentation isn't like it appears to be. Try python's <code>-t</code> flag to check for inconsistent usage of tabs:</p> <pre><code>python -t script.py </code></pre>
0
2010-02-23T07:59:28Z
[ "python", "file-io" ]
trouble writing to file in "for line in" iterator in Python script
2,316,452
<p>This seems to be an embarrassingly simple question, but, after a day of reading over How-To's and manuals, it must be asked.</p> <p>I'm writing many lines to a few files using a few nested loops, inserting some static strings and copying lines over from other files over and over again. The output appears to be a si...
0
2010-02-23T07:39:44Z
2,316,578
<p>Using the sample you gave as an input file and a made up cities+lvl10 variable, the script works as expected: For each of the "attack" lines in the input, it prints multiple "ifgosub" lines first and then the "attack" line.</p> <p>The most likely cause of your problem is that iteration over a file doesn't give you...
1
2010-02-23T08:07:55Z
[ "python", "file-io" ]
Setting up relations/mappings for a SQLAlchemy many-to-many database
2,316,537
<p>I'm new to SQLAlchemy and relational databases, and I'm trying to set up a model for an annotated lexicon. I want to support an arbitrary number of key-value annotations for the words which can be added or removed at runtime. Since there will be a lot of repetition in the names of the keys, I don't want to use <a ...
2
2010-02-23T07:59:50Z
2,319,988
<p>There is <a href="http://stackoverflow.com/questions/2089661/sqlalchemy-column-to-row-transformation-and-vice-versa-is-it-possible/2093103#2093103">very similar question</a> with slight interface difference. But it's easy to fix it by defining <code>__getitem__</code>, <code>__setitem__</code> and <code>__delitem__<...
1
2010-02-23T16:47:07Z
[ "python", "orm", "sqlalchemy" ]
Setting up relations/mappings for a SQLAlchemy many-to-many database
2,316,537
<p>I'm new to SQLAlchemy and relational databases, and I'm trying to set up a model for an annotated lexicon. I want to support an arbitrary number of key-value annotations for the words which can be added or removed at runtime. Since there will be a lot of repetition in the names of the keys, I don't want to use <a ...
2
2010-02-23T07:59:50Z
2,320,089
<p>Simply use <a href="http://www.sqlalchemy.org/docs/mappers.html#dictionary-based-collections" rel="nofollow">Dictionary-Based Collections mapping</a> mapping - out of the box solution to your question. Extract from the link:</p> <pre><code>from sqlalchemy.orm.collections import column_mapped_collection, attribute_m...
1
2010-02-23T17:01:14Z
[ "python", "orm", "sqlalchemy" ]
Setting up relations/mappings for a SQLAlchemy many-to-many database
2,316,537
<p>I'm new to SQLAlchemy and relational databases, and I'm trying to set up a model for an annotated lexicon. I want to support an arbitrary number of key-value annotations for the words which can be added or removed at runtime. Since there will be a lot of repetition in the names of the keys, I don't want to use <a ...
2
2010-02-23T07:59:50Z
2,322,449
<p>I ended up combining Denis and van's posts together to form the solution:</p> <pre><code>from sqlalchemy import Column, Integer, String, Table, create_engine from sqlalchemy import MetaData, ForeignKey from sqlalchemy.orm import relation, mapper, sessionmaker from sqlalchemy.orm.collections import attribute_mapped_...
0
2010-02-23T23:03:13Z
[ "python", "orm", "sqlalchemy" ]
Setting up relations/mappings for a SQLAlchemy many-to-many database
2,316,537
<p>I'm new to SQLAlchemy and relational databases, and I'm trying to set up a model for an annotated lexicon. I want to support an arbitrary number of key-value annotations for the words which can be added or removed at runtime. Since there will be a lot of repetition in the names of the keys, I don't want to use <a ...
2
2010-02-23T07:59:50Z
2,851,466
<p>Comment for Brent, above:</p> <p>You can use <code>session.flush()</code> instead of <code>commit()</code> to get an <code>id</code> on your model instances. <code>flush()</code> will execute the necessary SQL, but will not commit, so you can rollback later if needed.</p>
1
2010-05-17T17:57:41Z
[ "python", "orm", "sqlalchemy" ]
Pipe output of a command to an interactive python session?
2,316,730
<p>What I'd like to do is something like</p> <pre><code> $echo $PATH | python --remain-interactive "x = raw_input().split(':')" &gt;&gt;&gt; &gt;&gt;&gt; print x ['/usr/local/bin', '/usr/bin', '/bin'] </code></pre> <p>I suppose ipython solution would be best. If this isn't achievable, what would be your solution f...
1
2010-02-23T08:37:03Z
2,316,798
<p>Due to the Python axiom of "There should be one - and preferably only one - obvious way to do it" I'm reasonably sure that there won't be a better way to interact with other processes than <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">the <code>subprocess</code> module</a>.</p> <p>It might...
0
2010-02-23T08:50:03Z
[ "python", "redirect", "pipe", "interactive", "ipython" ]
Pipe output of a command to an interactive python session?
2,316,730
<p>What I'd like to do is something like</p> <pre><code> $echo $PATH | python --remain-interactive "x = raw_input().split(':')" &gt;&gt;&gt; &gt;&gt;&gt; print x ['/usr/local/bin', '/usr/bin', '/bin'] </code></pre> <p>I suppose ipython solution would be best. If this isn't achievable, what would be your solution f...
1
2010-02-23T08:37:03Z
2,316,808
<p>In IPython you can do this</p> <pre><code>x = !echo $$$$PATH </code></pre> <p>The double escape of <code>$</code> is a pain though</p> <p>You could do this I guess</p> <pre><code>PATH="$PATH" x = !echo $PATH x[0].split(":") </code></pre>
2
2010-02-23T08:51:43Z
[ "python", "redirect", "pipe", "interactive", "ipython" ]
Pipe output of a command to an interactive python session?
2,316,730
<p>What I'd like to do is something like</p> <pre><code> $echo $PATH | python --remain-interactive "x = raw_input().split(':')" &gt;&gt;&gt; &gt;&gt;&gt; print x ['/usr/local/bin', '/usr/bin', '/bin'] </code></pre> <p>I suppose ipython solution would be best. If this isn't achievable, what would be your solution f...
1
2010-02-23T08:37:03Z
2,316,827
<p>The <code>--remain-interactive</code> switch you are looking for is <code>-i</code>. You also can use the <code>-c</code> switch to specify the command to execute, such as <code>__import__("sys").stdin.read().split(":")</code>. So what you would try is: (do not forget about escaping strings!)</p> <pre><code>echo $P...
0
2010-02-23T08:56:02Z
[ "python", "redirect", "pipe", "interactive", "ipython" ]
Is it just me...or is "Facebook Mobile Web" only for PHP?
2,316,802
<p><a href="http://wiki.developers.facebook.com/index.php/Mobile" rel="nofollow">http://wiki.developers.facebook.com/index.php/Mobile</a></p> <p>I use Django/Python as my mobile website.</p> <p>Am I missing something?</p>
0
2010-02-23T08:51:09Z
2,316,833
<p>"Facebook Connect for Mobile Web is a PHP library..."</p>
1
2010-02-23T08:56:44Z
[ "php", "python", "django", "facebook", "mobile" ]
Is it just me...or is "Facebook Mobile Web" only for PHP?
2,316,802
<p><a href="http://wiki.developers.facebook.com/index.php/Mobile" rel="nofollow">http://wiki.developers.facebook.com/index.php/Mobile</a></p> <p>I use Django/Python as my mobile website.</p> <p>Am I missing something?</p>
0
2010-02-23T08:51:09Z
2,320,966
<p>The library may be "officially' for PHP, <strong>But</strong> that doesnt stop you from making your own.</p> <p>I would suggest looking at the API. You may be able to port the calls to python using python's <code>httplib</code>.</p> <p>All the PHP library does is make curl POST and GET calls to Facebook's REST ser...
3
2010-02-23T19:14:02Z
[ "php", "python", "django", "facebook", "mobile" ]
Converting a string to a formatted date-time string using Python
2,316,987
<p>I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)"</p> <pre><code>&gt;&gt;&gt;import time &gt;&gt;&gt;s = time.strptime("20091229050936", "%Y%m%d%H%M%S") &gt;&gt;&gt;print s.strftime('%H:%M %d %B %Y (UTC)') </code></pre> <p>gives <code>AttributeError: 'time.struct_time' object has ...
17
2010-02-23T09:29:24Z
2,316,990
<p><code>time.strptime</code> returns a <code>time_struct</code>; <code>time.strftime</code> accepts a <code>time_struct</code> as an optional parameter:</p> <pre><code>&gt;&gt;&gt;s = time.strptime(page.editTime(), "%Y%m%d%H%M%S") &gt;&gt;&gt;print time.strftime('%H:%M %d %B %Y (UTC)', s) </code></pre> <p>gives <cod...
8
2010-02-23T09:29:44Z
[ "python", "datetime", "time", "strftime", "strptime" ]
Converting a string to a formatted date-time string using Python
2,316,987
<p>I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)"</p> <pre><code>&gt;&gt;&gt;import time &gt;&gt;&gt;s = time.strptime("20091229050936", "%Y%m%d%H%M%S") &gt;&gt;&gt;print s.strftime('%H:%M %d %B %Y (UTC)') </code></pre> <p>gives <code>AttributeError: 'time.struct_time' object has ...
17
2010-02-23T09:29:24Z
2,317,046
<p>For <code>datetime</code> objects, <code>strptime</code> is a <a href="http://docs.python.org/library/datetime.html#datetime.datetime.strftime">static method</a> of the <code>datetime</code> class, not a free function in the <code>datetime</code> module:</p> <pre><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; s = ...
34
2010-02-23T09:39:08Z
[ "python", "datetime", "time", "strftime", "strptime" ]
Converting a string to a formatted date-time string using Python
2,316,987
<p>I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)"</p> <pre><code>&gt;&gt;&gt;import time &gt;&gt;&gt;s = time.strptime("20091229050936", "%Y%m%d%H%M%S") &gt;&gt;&gt;print s.strftime('%H:%M %d %B %Y (UTC)') </code></pre> <p>gives <code>AttributeError: 'time.struct_time' object has ...
17
2010-02-23T09:29:24Z
6,811,152
<p>For me this is the best and it works on Google App Engine as well</p> <p>Example showing UTC-4</p> <pre><code>import datetime UTC_OFFSET = 4 local_datetime = datetime.datetime.now() print (local_datetime - datetime.timedelta(hours=UTC_OFFSET)).strftime("%Y-%m-%d %H:%M:%S") </code></pre>
1
2011-07-25T01:59:22Z
[ "python", "datetime", "time", "strftime", "strptime" ]
Converting a string to a formatted date-time string using Python
2,316,987
<p>I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)"</p> <pre><code>&gt;&gt;&gt;import time &gt;&gt;&gt;s = time.strptime("20091229050936", "%Y%m%d%H%M%S") &gt;&gt;&gt;print s.strftime('%H:%M %d %B %Y (UTC)') </code></pre> <p>gives <code>AttributeError: 'time.struct_time' object has ...
17
2010-02-23T09:29:24Z
22,842,798
<pre><code>from datetime import datetime s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S") print("{:%H:%M %d %B %Y (UTC)}".format(s)) </code></pre>
1
2014-04-03T15:52:22Z
[ "python", "datetime", "time", "strftime", "strptime" ]
Converting a string to a formatted date-time string using Python
2,316,987
<p>I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)"</p> <pre><code>&gt;&gt;&gt;import time &gt;&gt;&gt;s = time.strptime("20091229050936", "%Y%m%d%H%M%S") &gt;&gt;&gt;print s.strftime('%H:%M %d %B %Y (UTC)') </code></pre> <p>gives <code>AttributeError: 'time.struct_time' object has ...
17
2010-02-23T09:29:24Z
30,148,135
<p>You can use <a href="https://github.com/ralphavalon/easy_date" rel="nofollow">easy_date</a> to make it easy:</p> <pre><code>import date_converter my_datetime = date_converter.string_to_string("20091229050936", "%Y%m%d%H%M%S", "%H:%M %d %B %Y (UTC)") </code></pre>
1
2015-05-10T05:42:09Z
[ "python", "datetime", "time", "strftime", "strptime" ]
Python+parsing custom config file
2,317,070
<p>I have a quite big custom made config file I need to extract data from once a week. This is an "in house" config file which doesn't comply to any know standard like INI or such.</p> <p>My quick and dirty approach was to use re to search for the section header I want and then extract the one or 2 lines of informatio...
1
2010-02-23T09:42:28Z
2,317,104
<p>Regexp have no state, so you can't use them to parse a complex input. But you can load the file into a string, use a regexp to find a substring and then cut the string at that place.</p> <p>In your case, search for <code>r'unique-name I search for using re"\s*=\s*'</code>, then cut after the match. Then search for ...
0
2010-02-23T09:49:09Z
[ "python", "file", "parsing", "config" ]
Python+parsing custom config file
2,317,070
<p>I have a quite big custom made config file I need to extract data from once a week. This is an "in house" config file which doesn't comply to any know standard like INI or such.</p> <p>My quick and dirty approach was to use re to search for the section header I want and then extract the one or 2 lines of informatio...
1
2010-02-23T09:42:28Z
2,317,173
<p>If you only look for special content, using regexp is fine; if you need to read everything, you should rather build yourself a parser.</p> <pre><code>&gt;&gt; s = ''' ... ''' # as above &gt;&gt; t = re.search( 'Registry "unique-name" =(.*?)\n;', s, re.S ).group( 1 ) &gt;&gt; u = re.findall( '^\s*(\w+) "?(.*?)"? = "...
1
2010-02-23T10:02:55Z
[ "python", "file", "parsing", "config" ]
Python+parsing custom config file
2,317,070
<p>I have a quite big custom made config file I need to extract data from once a week. This is an "in house" config file which doesn't comply to any know standard like INI or such.</p> <p>My quick and dirty approach was to use re to search for the section header I want and then extract the one or 2 lines of informatio...
1
2010-02-23T09:42:28Z
2,317,231
<p>I think you should create simple parser which create dictionaries of sections with dictionaries of keys. Something like:</p> <pre><code>#!/usr/bin/python import re re_section = re.compile('Registry (.*)=', re.IGNORECASE) re_value = re.compile('\s+String\s+(\S+)\s*=\s*(.*);') txt = ''' Registry com.name.version =...
0
2010-02-23T10:12:52Z
[ "python", "file", "parsing", "config" ]
Python+parsing custom config file
2,317,070
<p>I have a quite big custom made config file I need to extract data from once a week. This is an "in house" config file which doesn't comply to any know standard like INI or such.</p> <p>My quick and dirty approach was to use re to search for the section header I want and then extract the one or 2 lines of informatio...
1
2010-02-23T09:42:28Z
2,319,635
<p>A simple parser using pyparsing can give you something close to a deserializer, that would let you access fields by key name (like in a dict), or as attributes. Here is the parser:</p> <pre><code>from pyparsing import (Suppress,quotedString,removeQuotes,Word,alphas, alphanums, printables,delimitedList,Grou...
1
2010-02-23T16:05:36Z
[ "python", "file", "parsing", "config" ]
SQLAlchemy - maximum column length
2,317,081
<p>is it possible in SQLAlchemy to enforce maximum string length of value assigned to mapped column? All I want is to raise an exception if an assigned string value is longer then the length of the corresponding table column of type STRING.</p> <p>Thank you</p>
6
2010-02-23T09:45:01Z
2,317,843
<p>It's easiest to just rename the mapped column and proxy it through a property:</p> <pre><code>class Something(Base): ... _foo = Column('foo', String(123)) @property def foo(self): return self._foo @foo.setter def foo(self, value): if len(value) &gt; _foo.type.length: ...
5
2010-02-23T11:55:16Z
[ "python", "sqlalchemy" ]