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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Improve a IRC Client in Python | 1,121,002 | <p>How i can make some improvement in my IRC client made in Python. The improvement is: How i can put something that the user can type the <em>HOST, PORT, NICK, INDENT and REALNAME</em> strings and the message? And here is the code of the program:</p>
<blockquote>
<p>simplebot.py</p>
<pre><code>import sys
import so... | 0 | 2009-07-13T17:47:09Z | 1,121,180 | <p>If you're trying to carry out actions in response to user input, maybe the <code>cmd</code> module will help you out:</p>
<ul>
<li><a href="http://docs.python.org/library/cmd.html" rel="nofollow">cmd â Support for line-oriented command interpreters</a></li>
<li><a href="http://blog.doughellmann.com/2008/05/pymotw... | 1 | 2009-07-13T18:19:30Z | [
"python",
"sockets",
"client",
"python-3.x",
"irc"
] |
Eliminating certain Django Session Calls | 1,121,299 | <p>I was wondering if I could eliminate django session calls for specific views. For example, if I have a password reset form I don't want a call to the DB to check for a session or not. Thanks!</p>
| 0 | 2009-07-13T18:40:29Z | 1,121,363 | <p>Sessions are lazily loaded: if you don't use the session during a request, Django won't load it.</p>
<p>This includes request.user: if you access it, it accesses the session to find the user. (It loads lazily, too--if you don't access request.user, it won't access the session, either.)</p>
<p>So, figure out what'... | 1 | 2009-07-13T18:50:12Z | [
"python",
"django",
"session"
] |
How to model one way one-to-one relationship in Django | 1,121,488 | <p>I want to model an article with revisions in Django:</p>
<p>I have following in my article's models.py:</p>
<pre><code>class Article(models.Model):
title = models.CharField(blank=False, max_length=80)
slug = models.SlugField(max_length=80)
def __unicode__(self):
return self.title
class Articl... | 3 | 2009-07-13T19:11:54Z | 1,121,514 | <p>What is wrong with the link going both ways? I would think that the <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield" rel="nofollow"><code>OneToOneField</code></a> would be the perfect choice here. Is there a specific reason why this will be a detriment to your application? If you d... | 4 | 2009-07-13T19:17:13Z | [
"python",
"django",
"django-models"
] |
How to model one way one-to-one relationship in Django | 1,121,488 | <p>I want to model an article with revisions in Django:</p>
<p>I have following in my article's models.py:</p>
<pre><code>class Article(models.Model):
title = models.CharField(blank=False, max_length=80)
slug = models.SlugField(max_length=80)
def __unicode__(self):
return self.title
class Articl... | 3 | 2009-07-13T19:11:54Z | 1,122,236 | <p>The back-references that Django produces are programatic, and do not affect the underlying Database schema. In other words, if you have a one-to-one or foreign key field on your Article pointing to your Revision, a column will be added to the Article table in the database, but not to the Revision table.</p>
<p>Thus... | 2 | 2009-07-13T21:40:31Z | [
"python",
"django",
"django-models"
] |
Python to drive Emacs; pymacs doesn't work | 1,121,759 | <p>I've got a python script that loops indefinitely waiting for input, and then
does something when the input happens. My problem is then making python
tell emacs to do something. I just need some way to send emacs input
and make emacs evaluate that input.</p>
<p>Here's some code to illustrate my problem...</p>
<pre... | 1 | 2009-07-13T20:08:56Z | 1,121,792 | <p>You can use <a href="http://www.emacswiki.org/emacs/GnuClient" rel="nofollow">gnuclient</a> (shipped with Emacs 22) (or <a href="http://www.emacswiki.org/emacs/EmacsClient" rel="nofollow">emacsclient</a> for earlier Emacsen), to evaluate code from external programs and connect to a running Emacs.</p>
<p>Getting Ema... | 4 | 2009-07-13T20:16:10Z | [
"python",
"emacs",
"pymacs"
] |
What is the maximum simultaneous HTTP connections allowed on one machine (windows server 2008) using python | 1,121,951 | <p>To be more specific, I'm using python and making a pool of HTTPConnection (httplib) and was wondering if there is an limit on the number of concurrent HTTP connections on a windows server.</p>
| 3 | 2009-07-13T20:48:19Z | 1,122,107 | <p>AFAIK, the numbers of internet sockets (necessary to make TCP/IP connections) is naturally limited on every machine, but it's pretty high. 1000 simulatneous connections shouldn't be a problem for the client machine, as each socket uses only little memory. If you start receiving data through all these channels, this ... | 2 | 2009-07-13T21:15:06Z | [
"python"
] |
What is the maximum simultaneous HTTP connections allowed on one machine (windows server 2008) using python | 1,121,951 | <p>To be more specific, I'm using python and making a pool of HTTPConnection (httplib) and was wondering if there is an limit on the number of concurrent HTTP connections on a windows server.</p>
| 3 | 2009-07-13T20:48:19Z | 1,122,135 | <p>Per the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html" rel="nofollow">HTTP RFC</a>, a client should not maintain more than 2 simultaneous connections to a webserver or proxy. However, most browsers don't honor that - firefox 3.5 allows 6 per server and 8 per proxy.</p>
<p>In short, you should not b... | 3 | 2009-07-13T21:20:19Z | [
"python"
] |
Write unit tests for restish in Python | 1,122,192 | <p>I'm writing a RESTful API in Python using the <a href="http://ish.io/projects/show/restish" rel="nofollow">restish</a> framework. I would like to write some unit tests (using the unittest package, for example), that will make different requests to my application and validate the results. The unit tests should be a... | 1 | 2009-07-13T21:30:39Z | 1,122,458 | <p>I test everything using <a href="http://pythonpaste.org/webtest/" rel="nofollow" title="WebTest">WebTest</a> and <a href="http://code.google.com/p/python-nose/" rel="nofollow" title="Nose">NoseTests</a> and I can strongly recommend it. It's fast, flexible and easy to set up. Just pass it your wsgi function and you'r... | 1 | 2009-07-13T22:30:32Z | [
"python",
"unit-testing"
] |
Write unit tests for restish in Python | 1,122,192 | <p>I'm writing a RESTful API in Python using the <a href="http://ish.io/projects/show/restish" rel="nofollow">restish</a> framework. I would like to write some unit tests (using the unittest package, for example), that will make different requests to my application and validate the results. The unit tests should be a... | 1 | 2009-07-13T21:30:39Z | 1,123,296 | <p>Since restish is a WSGI framework, you can take advantage of any one of a number of WSGI testing tools:</p>
<ul>
<li><a href="http://wsgi.org/wsgi/Testing" rel="nofollow">http://wsgi.org/wsgi/Testing</a></li>
</ul>
<p>At least a few of those tools, such as Twill, should be able to test your application without sta... | 1 | 2009-07-14T03:19:46Z | [
"python",
"unit-testing"
] |
Write unit tests for restish in Python | 1,122,192 | <p>I'm writing a RESTful API in Python using the <a href="http://ish.io/projects/show/restish" rel="nofollow">restish</a> framework. I would like to write some unit tests (using the unittest package, for example), that will make different requests to my application and validate the results. The unit tests should be a... | 1 | 2009-07-13T21:30:39Z | 1,132,247 | <p>Restish has a built in TestApp class that can be used to test restish apps.
Assuming you have a "test" dir in your root restish project callte "restest" created with <a href="http://pythonpaste.org/script/" rel="nofollow">paster</a>.</p>
<pre><code>import os
import unittest
from paste.fixture import TestApp
class ... | 1 | 2009-07-15T15:46:43Z | [
"python",
"unit-testing"
] |
Which revision of html5lib is stable? | 1,122,494 | <p><a href="http://code.google.com/p/html5lib/" rel="nofollow">html5lib</a> notes that it's latest release (0.11) is somewhat old. Using the Python portion, I have recursion problems as noted in <a href="http://code.google.com/p/html5lib/issues/detail?id=70" rel="nofollow">Issue 70</a> and <a href="http://code.google.c... | 2 | 2009-07-13T22:42:51Z | 4,365,917 | <p>As of January 2010, it looks like version 0.90 is what you want:</p>
<p><a href="http://code.google.com/p/html5lib/downloads/list" rel="nofollow">http://code.google.com/p/html5lib/downloads/list</a></p>
| 1 | 2010-12-06T11:30:53Z | [
"python",
"html5lib"
] |
What can I attach to pylons.request in Pylons? | 1,122,537 | <p>I want keep track of a unique identifier for each browser that connects to my web application (that is written in Pylons.) I keep a cookie on the client to keep track of this, but if the cookie isn't present, then I want to generate a new unique identifier that will be sent back to the client with the response, but... | 2 | 2009-07-13T22:53:56Z | 1,126,737 | <p><em>Why</em> do you want a unique identifier? Basically every visitor already gets a unique identifier, his Session. <a href="http://pypi.python.org/pypi/Beaker/" rel="nofollow">Beaker</a>, Pylons session and caching middleware, does all the work and tracks visitors, usually with a Session cookie. So don't care abou... | 3 | 2009-07-14T17:07:34Z | [
"python",
"thread-safety",
"pylons"
] |
What can I attach to pylons.request in Pylons? | 1,122,537 | <p>I want keep track of a unique identifier for each browser that connects to my web application (that is written in Pylons.) I keep a cookie on the client to keep track of this, but if the cookie isn't present, then I want to generate a new unique identifier that will be sent back to the client with the response, but... | 2 | 2009-07-13T22:53:56Z | 1,128,979 | <p>Whatever you were to set on the request will only survive for the duration of the request. the problem you are describing is more appropriately handled with a Session as TCH4k has said. It's already enabled in the middleware, so go ahead.</p>
| 0 | 2009-07-15T01:43:11Z | [
"python",
"thread-safety",
"pylons"
] |
Django ManyToMany Template rendering and performance issues | 1,122,605 | <p>I've got a django model that contains a manytomany relationship, of the type,</p>
<pre><code>class MyModel(models.Model):
name = ..
refby = models.ManyToManyField(MyModel2)
..
class MyModel2(..):
name = ..
date = ..
</code></pre>
<p>I need to render it in my template such that I am able to render all th... | 3 | 2009-07-13T23:09:48Z | 1,122,693 | <blockquote>
<p>Should I do the sorting and
computation in the view, and then pass
it?</p>
</blockquote>
<p>Yes, definitely.</p>
<p>It is not really a matter of performance (as Django's querysets are lazily evaluated, I suspect the final performance could be very similar in both cases) but of code organization.... | 0 | 2009-07-13T23:34:23Z | [
"python",
"django",
"performance",
"django-templates",
"many-to-many"
] |
Django ManyToMany Template rendering and performance issues | 1,122,605 | <p>I've got a django model that contains a manytomany relationship, of the type,</p>
<pre><code>class MyModel(models.Model):
name = ..
refby = models.ManyToManyField(MyModel2)
..
class MyModel2(..):
name = ..
date = ..
</code></pre>
<p>I need to render it in my template such that I am able to render all th... | 3 | 2009-07-13T23:09:48Z | 1,123,397 | <p>I assume <code>mymodel_obj_list</code> is a QuerySet. You're accessing a foreign key field inside the loop, which means, by default, Django will look up each object's refby one at a time, when you access it. If you're displaying a lot of rows, this is extremely slow.</p>
<p>Call select_related on the QuerySet, to... | 4 | 2009-07-14T04:14:28Z | [
"python",
"django",
"performance",
"django-templates",
"many-to-many"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,122,652 | <p>1</p>
<p>I have discovered a truly marvelous proof of this, which this margin is too narrow to contain.</p>
<p>In all seriousness though, I don't believe you can do this with Python list comprehensions. They have basically the same power as map and filter, so you can't break out or look at previous values without ... | 0 | 2009-07-13T23:21:04Z | [
"python",
"lisp",
"list-comprehension"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,122,653 | <p>I believe you are writing the hailstone sequence, although I could be wrong since I am not fluent in Lisp.</p>
<p>As far as I know, you can't do this in only a list comprehension, since each element depends on the last.</p>
<p>How I would do it would be this</p>
<pre><code>def hailstone(n):
yield n
while ... | 9 | 2009-07-13T23:21:24Z | [
"python",
"lisp",
"list-comprehension"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,122,663 | <p>A list comprehension is used to take an existing sequence and perform some function and/or filter to it, resulting in a new list. So, in this case a list comprehension is not appropriate since you don't have a starting sequence. An example with a while loop:</p>
<pre><code>numbers = []
x=input()
while x != 1:
num... | 10 | 2009-07-13T23:24:13Z | [
"python",
"lisp",
"list-comprehension"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,122,731 | <p>As Kiv said, a list comprehension requires a known sequence to iterate over.</p>
<p>Having said that, if you had a sequence and were fixated on using a list comprehension, your solution would probably include something like this:</p>
<pre><code>[not (x % 2) and (x / 2) or (3 * x + 1) for x in sequence]
</code></pr... | 3 | 2009-07-13T23:45:36Z | [
"python",
"lisp",
"list-comprehension"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,123,651 | <p>Python doesn't have this kind of control structure built in, but you can generalize this into a function like this:</p>
<pre><code>def unfold(evolve, initial, until):
state = initial
yield state
while not until(state):
state = evolve(state)
yield state
</code></pre>
<p>After this your e... | 4 | 2009-07-14T05:51:10Z | [
"python",
"lisp",
"list-comprehension"
] |
List comprehension python | 1,122,612 | <p>What is the equivalent list comprehension in python of the following Common Lisp code:</p>
<pre><code>(loop for x = input then (if (evenp x)
(/ x 2)
(+1 (* 3 x)))
collect x
until (= x 1))
</code></pre>
| 2 | 2009-07-13T23:11:11Z | 1,126,359 | <p>The hackery referred to by Laurence:</p>
<p>You can do it in one list comprehension, it just ends up being AWFUL python. Unreadable python. Terrible python. I only present the following as a curiosity, not as an actual answer. Don't do this in code you actually want to use, only if you fancy having a play with the ... | 4 | 2009-07-14T15:55:53Z | [
"python",
"lisp",
"list-comprehension"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,705 | <p>I would find this acceptable, if a bit lazy, as long as what is being done isn't too performance critical. You could always go back and optimize it if you need more speed.</p>
| -2 | 2009-07-13T23:37:37Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,710 | <p>Code is usually read many times, and it is written only once.<br />
Saving writing time at the expense of readability is not usually a good choice, unless you are doing some throw-away code.</p>
<p>The second version is less explicit, and you need some time to understand what the code is doing. And we are simply ta... | 32 | 2009-07-13T23:38:17Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,719 | <p>In general, I think this is a bad idea. The first example isn't SO bad (it's sort of a substitute for python's lack of qw), but the second is much more difficult to understand. In particular, I think this sort of thing is very unpythonic, and certainly not appropriate when writing Python code, at any rate. Code read... | 5 | 2009-07-13T23:40:52Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,721 | <p>How often do you really need to type <code>["January", "February"... etc]</code>?</p>
<p>Granted your approach might save you time but why add complexity to your code for no good reason other than you are a bit lazy?</p>
<p>If you really do have to type it that often... Copy-Paste!</p>
| 3 | 2009-07-13T23:41:01Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,727 | <blockquote>
<p><em>...I often do something like this to save me having to type quotation marks all over the place...</em></p>
</blockquote>
<p>I think such a thing should be done only once per program. If you do the same "all over the place" then it doesn't matter which one do you use, you're creating a monster. </... | 1 | 2009-07-13T23:42:18Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,728 | <p>It's a reasonable thing to do for data that you don't expect to change, such as months or days of the weeks. It's also reasonable to do this while mocking up or stubbing out interactions with files or databases, because it isolates your code for testing. However, this isn't a good long-term solution for production c... | 1 | 2009-07-13T23:43:17Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,733 | <p>In production code, do it the right way. In test code, as long as this idiom shows up more than once or twice, I think this is acceptable. If, in test code, this situation shows up once or twice, do it the right way. </p>
| 3 | 2009-07-13T23:46:54Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,737 | <p>A good text editor can make these things a non-issue. For example, I can type the following line in my code:</p>
<pre><code>print `"January February March April May June July August September October November December".split()`
</code></pre>
<p>And then using the key sequence <code>V:!python<ENTER></code> I ... | 19 | 2009-07-13T23:50:07Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,739 | <p>I don't think that type of thing should be in the source.</p>
<p>If I were you, I'd have Python evaluate the respective second versions and then paste the results into my source code.</p>
| 0 | 2009-07-13T23:50:29Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,122,788 | <p>In a reasonably smart editor you could:</p>
<ol>
<li>Select the lines of interest, </li>
<li>insert the replacement (<code><space></code> by "<code><space></code>" for the 1st ex.), </li>
<li>check selected lines checkbox, </li>
<li>click replace all,</li>
<li>bam.. your done.</li>
</ol>
<p>Readable <e... | 6 | 2009-07-14T00:05:54Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,124,082 | <p>I think putting statements like</p>
<p>"January February March April May June July August ...".split()</p>
<p>at module global level is fine. This way it is only executed once during import. I even sometimes use it in non-performance critical functions because of the reduced line noise. </p>
<p>On a sidenote i t... | 0 | 2009-07-14T08:00:08Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 1,139,963 | <p>I all but swear by <a href="http://www.powells.com/partner/24934/biblio/9780735619678" rel="nofollow">Steve McConnell's <em>Code Complete</em></a>: one of the core insights is that bad programmers write code in order to get computers to do something, period, while good programmers write first to write code that peop... | 1 | 2009-07-16T20:06:04Z | [
"python",
"coding-style"
] |
Is it acceptable to use tricks to save programmer when putting data in your code? | 1,122,691 | <p>Example: It's really annoying to type a list of strings in python:</p>
<pre><code>["January", "February", "March", "April", ...]
</code></pre>
<p>I often do something like this to save me having to type quotation marks all over the place:</p>
<pre><code>"January February March April May June July August ...".spli... | 9 | 2009-07-13T23:34:04Z | 2,122,735 | <p>your quick and clever methods while nice, take more time to read which is not good. Readability comes first always. </p>
| 1 | 2010-01-23T09:45:26Z | [
"python",
"coding-style"
] |
bash/cygwin/$PATH: Do I really have to reboot to alter $PATH? | 1,122,924 | <p>I wanted to use the Python installed under cygwin rather than one installed under WinXP directly, so I edited ~/.bashrc and sourced it. Nothing changed. I tried other things, but nothing I did changed $PATH in any way. So I rebooted. Aha; now $PATH has changed to what I wanted.</p>
<p>But, can anyone explain WH... | 5 | 2009-07-14T00:45:44Z | 1,122,953 | <p>You may need to re-initialize bash's hashes after modifying the path variable:</p>
<pre><code>hash -r
</code></pre>
| 0 | 2009-07-14T00:57:32Z | [
"python",
"bash",
"path",
"cygwin",
"reboot"
] |
bash/cygwin/$PATH: Do I really have to reboot to alter $PATH? | 1,122,924 | <p>I wanted to use the Python installed under cygwin rather than one installed under WinXP directly, so I edited ~/.bashrc and sourced it. Nothing changed. I tried other things, but nothing I did changed $PATH in any way. So I rebooted. Aha; now $PATH has changed to what I wanted.</p>
<p>But, can anyone explain WH... | 5 | 2009-07-14T00:45:44Z | 1,122,991 | <p>Try:</p>
<pre><code>PATH="${PATH}:${PYTHON}"; export PATH
</code></pre>
<p>Or:</p>
<pre><code>export PATH="${PATH}:${PYTHON}"
</code></pre>
<p>the quotes preserve the spaces and newlines that you <strong><em>don't</em></strong> have in your directory names. I repeat <strong><em>"don't"</em></strong>.</p>
<p>If ... | 3 | 2009-07-14T01:15:56Z | [
"python",
"bash",
"path",
"cygwin",
"reboot"
] |
bash/cygwin/$PATH: Do I really have to reboot to alter $PATH? | 1,122,924 | <p>I wanted to use the Python installed under cygwin rather than one installed under WinXP directly, so I edited ~/.bashrc and sourced it. Nothing changed. I tried other things, but nothing I did changed $PATH in any way. So I rebooted. Aha; now $PATH has changed to what I wanted.</p>
<p>But, can anyone explain WH... | 5 | 2009-07-14T00:45:44Z | 1,123,086 | <p>If you want your changes to be permanent, you should modify the proper file (.bashrc in this case) and perform ONE of the following actions:</p>
<ul>
<li>Restart the cygwin window</li>
<li>source .bashrc (This should work, even if is not working for you)</li>
<li>. .bashrc (that is dot <space> <filename>... | 2 | 2009-07-14T01:55:17Z | [
"python",
"bash",
"path",
"cygwin",
"reboot"
] |
bash/cygwin/$PATH: Do I really have to reboot to alter $PATH? | 1,122,924 | <p>I wanted to use the Python installed under cygwin rather than one installed under WinXP directly, so I edited ~/.bashrc and sourced it. Nothing changed. I tried other things, but nothing I did changed $PATH in any way. So I rebooted. Aha; now $PATH has changed to what I wanted.</p>
<p>But, can anyone explain WH... | 5 | 2009-07-14T00:45:44Z | 1,123,139 | <p>A couple of things to try and rule out at least:</p>
<ol>
<li><p>Do you get the same behavior as the following from the shell? (Pasted from my cygwin, which works as expected.)</p>
<pre>
$ echo $PATH
/usr/local/bin:/usr/bin:/bin
$ export PATH=$PATH:/cygdrive/c/python/bin
$ echo $PATH
/usr/local/bin:/usr/bin:/b... | 1 | 2009-07-14T02:16:47Z | [
"python",
"bash",
"path",
"cygwin",
"reboot"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 1,123,026 | <p>The pythonic way would be to use a <code>dict</code>:</p>
<pre><code>>>> foo = dict(x=1, y=2)
>>> bar = dict(y=2, x=1)
>>> foo == bar
True
</code></pre>
<p>Meets all your requirements except that you still have to do <code>foo['x']</code> instead of <code>foo.x</code>. </p>
<p>If that's... | 37 | 2009-07-14T01:32:06Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 1,123,040 | <p>I don't remember offhand if there's a built-in but writing it yourself is shorter than typing your question. :-)</p>
<pre><code>class record(object):
def __init__(self, **kwargs): self.__dict__ = kwargs
def __eq__(self, r2): return self.__dict__ == r2.__dict__
def __ne__(self, r2): return self.__dict__ != r2... | 6 | 2009-07-14T01:36:11Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 1,123,050 | <p>Quoted from <a href="http://norvig.com/python-iaq.html" rel="nofollow">this page</a>:</p>
<pre><code> class Struct:
def __init__(self, **entries): self.__dict__.update(entries)
def __eq__(self, other): return self.__dict__ == other.__dict__
def __neq__(self, other): return self.__dict__ != other.__di... | 3 | 2009-07-14T01:40:01Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 1,123,054 | <p>1) See <a href="http://uszla.me.uk/space/blog/2008/11/06">http://uszla.me.uk/space/blog/2008/11/06</a>. You can create an anonymous object with slightly ugly syntax by using the <code>type</code> built-in function:</p>
<pre><code> anon_object_2 = type("", (), {})()
</code></pre>
<p>where the 3rd parameter is the ... | 19 | 2009-07-14T01:40:29Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 1,123,114 | <p>The type(...) form will fail the structural comparison requirement (without getting really ugly). The dict(...) form doesn't meet the attribute accessor requirement.</p>
<p>The <a href="http://code.activestate.com/recipes/361668/">attrdict</a> seems to fall in the middle somewhere:</p>
<pre><code>class attrdict(d... | 6 | 2009-07-14T02:07:20Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 13,828,824 | <p>Looks like Python 3.3 has added exactly this thing in the form of <a href="http://docs.python.org/3/library/types.html#types.SimpleNamespace"><code>types.SimpleNamespace</code></a> class.</p>
| 12 | 2012-12-11T21:29:13Z | [
"python"
] |
Does Python have anonymous classes? | 1,123,000 | <p>I'm wondering if Python has anything like the C# anonymous classes feature. To clarify, here's a sample C# snippet:</p>
<pre><code>var foo = new { x = 1, y = 2 };
var bar = new { y = 2, x = 1 };
foo.Equals(bar); // "true"
</code></pre>
<p>In Python, I would imagine something like this:</p>
<pre><code>foo = record... | 51 | 2009-07-14T01:22:55Z | 30,543,074 | <p>If you want the instance to be anonymous as well (using the object directly in an expression), then you're bound to use the type-expression. However in many cases the instance will not be anonymous, but assigned to a variable. This case can be handled in a reasonable way in python by using metaclasses or decorators.... | 1 | 2015-05-30T06:28:17Z | [
"python"
] |
Python Function Decorators in Google App Engine | 1,123,117 | <p>I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar with decorators, but they seem useful in web programming when you might want to force a user to login before executing certain functions. </p>
<p>Anyway, I was following along with a flickr login example <a href="http... | 2 | 2009-07-14T02:08:38Z | 1,123,126 | <p>Decorators were added in python 2.4 (I think), maybe googleapp is using an older version?
You can also do:</p>
<pre><code>def content():
print "Release sensitive data!"
content = require_auth(content)
</code></pre>
<p>it will do the same thing as the decorator, it is just a little more work.</p>
| 0 | 2009-07-14T02:11:27Z | [
"python",
"google-app-engine",
"decorator"
] |
Python Function Decorators in Google App Engine | 1,123,117 | <p>I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar with decorators, but they seem useful in web programming when you might want to force a user to login before executing certain functions. </p>
<p>Anyway, I was following along with a flickr login example <a href="http... | 2 | 2009-07-14T02:08:38Z | 1,123,162 | <p>@Owen, the "takes at least 1 argument" error suggests you're defining content within a class (i.e. as a method) and not as a bare function as you show -- indeed, how exactly are you trying to execute that code in GAE? I.e. what's your app.yaml &c? If you put your code exactly as you gave it in <code>silly.py</c... | 1 | 2009-07-14T02:24:34Z | [
"python",
"google-app-engine",
"decorator"
] |
Python Function Decorators in Google App Engine | 1,123,117 | <p>I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar with decorators, but they seem useful in web programming when you might want to force a user to login before executing certain functions. </p>
<p>Anyway, I was following along with a flickr login example <a href="http... | 2 | 2009-07-14T02:08:38Z | 1,123,174 | <p>Why</p>
<p><code>return func(*args, **kwargs)</code></p>
<p>wouldn't that execute func and then return the result?</p>
<p>If so, you shouldn't give it any arguments, since it doesn't take any. If you edited content() and added the <code>(*args, **kwargs)</code> arguments onto it, will it work?</p>
| 0 | 2009-07-14T02:28:38Z | [
"python",
"google-app-engine",
"decorator"
] |
Python Function Decorators in Google App Engine | 1,123,117 | <p>I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar with decorators, but they seem useful in web programming when you might want to force a user to login before executing certain functions. </p>
<p>Anyway, I was following along with a flickr login example <a href="http... | 2 | 2009-07-14T02:08:38Z | 1,123,309 | <p>You're calling <code>content()</code> without any arguments, but the decorated version <code>protected_view</code> requires the <code>request</code> argument. Either add the argument to <code>content</code> or remove it from <code>protected_view</code>.</p>
<p>If you're getting that error with your simple version t... | 3 | 2009-07-14T03:26:23Z | [
"python",
"google-app-engine",
"decorator"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 1,123,781 | <p>You could use simple file locking as a mutual exclusion mechanism, see my recipe <a href="http://code.activestate.com/recipes/519626/" rel="nofollow">here</a>. It won't suit all scenarios, but then you haven't said much about why you want to use this type of locking.</p>
| 4 | 2009-07-14T06:35:47Z | [
"python",
"django"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 1,151,172 | <p>I ended up going with a solution I made myself involving file locking. If anyone here ends up using it remember that advisory locks and NFS don't mix well, so keep it local. Also, this is a blocking lock, if you want to mess around with loops and constantly checking back then there is instructions in the code.</p>
... | 2 | 2009-07-19T23:29:59Z | [
"python",
"django"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 1,151,797 | <p>You need a distributed lock manager at the point where your app suddenly needs to run on more than one service. I wrote <a href="http://github.com/dustin/elock">elock</a> for this purpose. There are <a href="http://hadoop.apache.org/zookeeper/">bigger ones</a> and others have chosen to ignore every suggestion and ... | 5 | 2009-07-20T05:10:29Z | [
"python",
"django"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 4,317,995 | <p>If you use RDBMS, you can use its "LOCK" mechanism.
For example, while one "SELECT FOR UPDATE" transaction locks a row, the other "SELECT FOR UPDATE" transactions with the row must wait.</p>
<pre><code># You can use any Python DB API.
[SQL] BEGIN;
[SQL] SELECT col_name FROM table_name where id = 1 FOR UPDATE;
[Pr... | 13 | 2010-11-30T20:04:39Z | [
"python",
"django"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 5,110,089 | <p>I did not write this article, but I found it supremely helpful faced with this same problem:</p>
<p><a href="http://chris-lamb.co.uk/2010/06/07/distributing-locking-python-and-redis/" rel="nofollow">http://chris-lamb.co.uk/2010/06/07/distributing-locking-python-and-redis/</a></p>
<p>Basically you use a Redis serve... | 2 | 2011-02-24T20:33:15Z | [
"python",
"django"
] |
How to lock a critical section in Django? | 1,123,200 | <p>I can't find a good clean way to lock a critical section in Django. I could use a lock or semaphore but the python implementation is for threads only, so if the production server forks then those will not be respected. Does anyone know of a way (I am thinking posix semaphores right now) to guarantee a lock across ... | 13 | 2009-07-14T02:44:26Z | 33,480,015 | <p>Use the Django builtin select_for_update function.<br>
<a href="https://docs.djangoproject.com/en/1.8/ref/models/querysets/#select-for-update">https://docs.djangoproject.com/en/1.8/ref/models/querysets/#select-for-update</a><br>
From the docs:<br>
Returns a queryset that will lock rows until the end of the transacti... | 5 | 2015-11-02T14:33:54Z | [
"python",
"django"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,123,350 | <p>You are looking for the <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields">Values member of QuerySet</a> which allows you to get a list of dictionaries from your query</p>
<blockquote>
<p>Returns a ValuesQuerySet -- a QuerySet
that evaluates to a list of
dictionaries instead of... | 25 | 2009-07-14T03:42:17Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,123,354 | <p>use </p>
<pre><code>dict(((m.key, m.value) for m in DictModel.objects.all())
</code></pre>
<p>As suggested by Tom Leys, we do not need to get whole object, we can get only those values we need e.g.</p>
<pre><code>dict(((m['key'], m['value']) for m in DictModel.objects.values('key', 'value')))
</code></pre>
<p>an... | 5 | 2009-07-14T03:44:00Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,123,603 | <p>Does this need to create an actual dict? could you get by with only something that looked like a dict?</p>
<pre><code>class DictModelAdaptor():
def __init__(self, model):
self.model = model
def __getitem__(self, key):
return self.model.objects.get(key=key)
def __setitem__(self, key, it... | 15 | 2009-07-14T05:35:57Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,124,531 | <p>You can use the <code>python</code> <a href="http://docs.djangoproject.com/en/dev/topics/serialization">serializer</a>:</p>
<pre><code>from django.core import serializers
data = serializers.serialize('python', DictModel.objects.all())
</code></pre>
| 13 | 2009-07-14T10:19:23Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,124,610 | <p>You want the <code>in_bulk</code> queryset method which, according to the docs:</p>
<blockquote>
<p>Takes a list of primary-key values and
returns a dictionary mapping each
primary-key value to an instance of
the object with the given ID.</p>
</blockquote>
<p>It takes a list of IDs, so you'll need to get t... | 16 | 2009-07-14T10:39:13Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 1,677,068 | <p>You can also rely on django code already written ;).</p>
<pre><code>from django.forms.models import model_to_dict
model_to_dict(instance, fields=[], exclude=[])
</code></pre>
| 208 | 2009-11-04T22:18:09Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 2,450,865 | <p>Or were you trying to do something like:</p>
<pre><code>def someview(req):
models = MyModel.objects.all()
toTuple = lambda field: (getattr(field, 'someatt'), getattr(field, 'someotheratt'))
data = dict(map(toTuple,models))
return render_to_response(template, data)
</code></pre>
| 1 | 2010-03-15T22:18:55Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 3,428,560 | <p><code>dict((x.name, getattr(o, x.name)) for x in o._meta.fields)</code></p>
| 6 | 2010-08-06T23:49:40Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 7,327,390 | <p>Perhaps I'm missing something, but Django objects have a <code>__dict__</code> attribute which seems be what you want.</p>
| 4 | 2011-09-06T23:42:40Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 10,861,516 | <p>To get a map of all of the instances in a queryset into a single dictionary with a specified model field as the key, try doing this</p>
<pre><code>from django.forms.models import model_to_dict
from myApp.models import myModel
allObjs = myModel.objects.all()
f = {} # initialise the output
key = 'key' # o... | 2 | 2012-06-02T10:19:48Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 18,745,855 | <p>To get values of a models into dictionary, add below code at the place where u need that dictionary</p>
<pre><code>from models import DictModel
activity_map = dict(Plan.objects.values_list('key', 'value'))
</code></pre>
<p>activity_map is a dictionary which contains required information .</p>
| 2 | 2013-09-11T15:51:07Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Django: Converting an entire set of a Model's objects into a single dictionary | 1,123,337 | <p><em>If you came here from Google looking for model to dict, skip my question, and just jump down to the first answer. My question will only confuse you.</em></p>
<p>Is there a good way in Django to entire set of a Model's objects into a single dictionary? I mean, like this:</p>
<pre><code>class DictModel(models.M... | 57 | 2009-07-14T03:35:53Z | 27,721,631 | <pre><code>user = mymodel.objects.all()
user.values()
</code></pre>
<p>You can also try</p>
<pre><code>user.values_list() # getting as list
</code></pre>
| 2 | 2014-12-31T13:10:27Z | [
"python",
"django",
"django-models",
"dictionary"
] |
Setting a timeout function in django | 1,123,367 | <p>So I'm creating a django app that allows a user to add a new line of text to an existing group of text lines. However I don't want multiple users adding lines to the same group of text lines concurrently. So I created a BoolField isBeingEdited that is set to True once a user decides to append a specific group. Once ... | 1 | 2009-07-14T03:50:41Z | 1,123,375 | <p>Change the boolean to a "lock time"</p>
<ol>
<li>To lock the model, set the Lock time to the current time. </li>
<li>To unlock the model, set the lock time to None</li>
<li>Add an "is_locked" method. That method returns "not locked" if the current time is more than 10 minutes after the lock time. </li>
</ol>
<p>Th... | 4 | 2009-07-14T03:56:40Z | [
"python",
"django"
] |
clipping text in python/tkinter | 1,123,463 | <p>I want to draw text on a tkinter canvas, within a previously drawn rectangle. I want to clip the text to be drawn entirely within the rectangle, hopefully by just specifying a maximum allowed width. Is there a straightforward way to do this in tkinter? If not, could I be using something else that would make it easie... | 2 | 2009-07-14T04:45:46Z | 6,692,378 | <p>something along the lines of:</p>
<pre><code>from Tkinter import *
root = Tk()
c = Canvas(root, width=200, height=200)
c.pack()
c.create_rectangle(50,50,91,67, outline='blue')
t = Label(c, text="Hello John, Michael, Eric, ...", anchor='w')
c.create_window(51, 51, width=40, height=15, window=t, anchor='nw')
root.mai... | 1 | 2011-07-14T11:21:21Z | [
"python",
"tkinter"
] |
clipping text in python/tkinter | 1,123,463 | <p>I want to draw text on a tkinter canvas, within a previously drawn rectangle. I want to clip the text to be drawn entirely within the rectangle, hopefully by just specifying a maximum allowed width. Is there a straightforward way to do this in tkinter? If not, could I be using something else that would make it easie... | 2 | 2009-07-14T04:45:46Z | 6,929,134 | <p>Small patch over <a href="http://stackoverflow.com/questions/1123463/clipping-text-in-python-tkinter/6692378#6692378">noob oddy answer</a> (use a slider to illustrate the clipping actually works).</p>
<pre><code>from Tkinter import *
root = Tk()
c = Canvas(root, width=300, height=100)
c.pack()
r = c.create_rectang... | 0 | 2011-08-03T15:35:14Z | [
"python",
"tkinter"
] |
How do I grab an instance of a dynamic php script output? | 1,123,622 | <p>The following link outputs a different image every time you visit it:</p>
<p><a href="http://www.biglickmedia.com/art/random/index.php" rel="nofollow">http://www.biglickmedia.com/art/random/index.php</a></p>
<p>From a web browser, you can obviously right click it and save what you see. But if I were to visit this ... | 0 | 2009-07-14T05:41:16Z | 1,123,637 | <p>you might need something that creates a socket to the server and then issues a http GET request for "art/random/index.php". save the payload from the HTTP response, and then you have your data</p>
<p>what you would be creating is a simple HTTP client</p>
<p>the unix command wget does this:</p>
<pre><code>$ wget h... | 4 | 2009-07-14T05:45:44Z | [
"php",
"python",
"image",
"download",
"screen-scraping"
] |
How do I grab an instance of a dynamic php script output? | 1,123,622 | <p>The following link outputs a different image every time you visit it:</p>
<p><a href="http://www.biglickmedia.com/art/random/index.php" rel="nofollow">http://www.biglickmedia.com/art/random/index.php</a></p>
<p>From a web browser, you can obviously right click it and save what you see. But if I were to visit this ... | 0 | 2009-07-14T05:41:16Z | 1,123,688 | <pre><code><?php
file_put_contents('C:\\random.gif', file_get_contents('http://www.biglickmedia.com/art/random/index.php'));
?>
</code></pre>
| 1 | 2009-07-14T06:04:51Z | [
"php",
"python",
"image",
"download",
"screen-scraping"
] |
How do I grab an instance of a dynamic php script output? | 1,123,622 | <p>The following link outputs a different image every time you visit it:</p>
<p><a href="http://www.biglickmedia.com/art/random/index.php" rel="nofollow">http://www.biglickmedia.com/art/random/index.php</a></p>
<p>From a web browser, you can obviously right click it and save what you see. But if I were to visit this ... | 0 | 2009-07-14T05:41:16Z | 1,123,740 | <p>With Python and mechanize:</p>
<pre><code>import mechanize
b = mechanize.Browser()
t = b.open(url)
image_type = t.info().typeheader # mime-type of image
data = t.read() #bytes of image
open(filename, 'wb').write(data)
</code></pre>
| 1 | 2009-07-14T06:23:26Z | [
"php",
"python",
"image",
"download",
"screen-scraping"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 1,123,878 | <p>You could use something like the <a href="http://en.wikipedia.org/wiki/Observer%5Fpattern" rel="nofollow">Observer</a> pattern, wherein Queue subscribers are notified of state changes.</p>
<p>In this case, you could have your worker thread designated as a listener on each queue, and whenever it receives a ready sig... | 1 | 2009-07-14T07:05:52Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 1,123,952 | <p>It doesn't look like there's an official way to handle this yet. Or at least, not based on this:</p>
<ul>
<li><a href="http://bugs.python.org/issue3831">http://bugs.python.org/issue3831</a></li>
</ul>
<p>You could try something like what this post is doing -- accessing the underlying pipe filehandles:</p>
<ul>
<... | 13 | 2009-07-14T07:27:44Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 1,124,145 | <p>Seems like using threads which forward incoming items to a single Queue which you then wait on is a practical choice when using multiprocessing in a platform independent manner.</p>
<p>Avoiding the threads requires either handling low-level pipes/FDs which is both platform specific and not easy to handle consistent... | 2 | 2009-07-14T08:24:14Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 2,162,188 | <p>Actually you can use multiprocessing.Queue objects in select.select. i.e. </p>
<pre><code>que = multiprocessing.Queue()
(input,[],[]) = select.select([que._reader],[],[])
</code></pre>
<p>would select que only if it is ready to be read from. </p>
<p>No documentation about it though. I was reading the source code ... | 17 | 2010-01-29T13:29:15Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 5,789,428 | <p>Not sure how well the select on a multiprocessing queue works on windows. As select on windows listens for sockets and not file handles, I suspect there could be problems.</p>
<p>My answer is to make a thread to listen to each queue in a blocking fashion, and to put the results all into a single queue listened to ... | 1 | 2011-04-26T11:10:32Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 5,789,473 | <p>New version of above code...</p>
<p>Not sure how well the select on a multiprocessing queue works on windows. As select on windows listens for sockets and not file handles, I suspect there could be problems.</p>
<p>My answer is to make a thread to listen to each queue in a blocking fashion, and to put the results ... | 1 | 2011-04-26T11:14:22Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
"select" on multiple Python multiprocessing Queues? | 1,123,855 | <p>What's the best way to wait (without spinning) until something is available in either one of two (multiprocessing) <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.Queue">Queues</a>, where both reside on the same system?</p>
| 16 | 2009-07-14T06:58:17Z | 21,195,494 | <p>Don't do it.</p>
<p>Put a header on the messages and send them to a common queue. This simplifies the code and will be cleaner overall. </p>
| -2 | 2014-01-17T20:42:50Z | [
"python",
"events",
"select",
"synchronization",
"multiprocessing"
] |
Socket? python -m SimpleHTTPServer | 1,124,420 | <p><strong>Problem:</strong> to get the command working <a href="http://www.commandlinefu.com/commands/view/71/serve-current-directory-tree-at-httphostname8000" rel="nofollow">here.</a> My domain is <a href="http://cs.edu.com/user/share_dir" rel="nofollow">http://cs.edu.com/user/share_dir</a>, but I cannot get the comm... | 3 | 2009-07-14T09:43:22Z | 1,124,467 | <p>Your URL is incorrect. The port number should be specified after the domain name:</p>
<p><a href="http://cs.edu.com:8000/">http://cs.edu.com:8000/</a></p>
<p>Some other things you should keep in mind:</p>
<ol>
<li>If this is a shared host, port 8000 might already be in use by someone else</li>
<li>The host might ... | 5 | 2009-07-14T09:58:08Z | [
"python",
"sockets"
] |
What is the best practice in deploying application on Windows? | 1,124,667 | <p>I have an application that consists of several .dlls, .libs, .pyd (python library), .exe, .class-es.</p>
<p>What is the best practice in the deployment process?</p>
<p>I plan to put .dlls - managed into GAC and unmanaged into WinSxS folder.</p>
<p>What should I do with .libs, .exe, .class and .pyd?</p>
<p>Is it ... | 3 | 2009-07-14T10:54:06Z | 1,124,703 | <p>The current convention seems to be </p>
<p>"/ProgramFiles/YourCompany/YourApplication/..." </p>
<p>As for how to structure things under that folder, it is really dependent on what your application is doing, and how it's structured. Do make sure to store per-user information in <a href="http://msdn.microsoft.com/... | 2 | 2009-07-14T11:04:34Z | [
"python",
"windows",
"deployment"
] |
What is the best practice in deploying application on Windows? | 1,124,667 | <p>I have an application that consists of several .dlls, .libs, .pyd (python library), .exe, .class-es.</p>
<p>What is the best practice in the deployment process?</p>
<p>I plan to put .dlls - managed into GAC and unmanaged into WinSxS folder.</p>
<p>What should I do with .libs, .exe, .class and .pyd?</p>
<p>Is it ... | 3 | 2009-07-14T10:54:06Z | 1,124,939 | <p>I agree that /ProgramFiles/CompanyName/AppName is the convention. But you might also have to look at who will install the application. More and more users are no longer given admin rights on their Windows box at work, so they can't install under ProgramFiles. So depending on your target users and how you envisage th... | 1 | 2009-07-14T12:02:18Z | [
"python",
"windows",
"deployment"
] |
How can I find path to given file? | 1,124,810 | <p>I have a file, for example "something.exe" and I want to find path to this file<br>
How can I do this in python?</p>
| 2 | 2009-07-14T11:30:36Z | 1,124,825 | <p>Perhaps <code>os.path.abspath()</code> would do it:</p>
<pre><code>import os
print os.path.abspath("something.exe")
</code></pre>
<p>If your <code>something.exe</code> is not in the current directory, you can pass any relative path and <code>abspath()</code> will resolve it.</p>
| 9 | 2009-07-14T11:33:23Z | [
"python"
] |
How can I find path to given file? | 1,124,810 | <p>I have a file, for example "something.exe" and I want to find path to this file<br>
How can I do this in python?</p>
| 2 | 2009-07-14T11:30:36Z | 1,124,841 | <p>use <a href="http://docs.python.org/library/os.path.html#os.path.abspath">os.path.abspath</a> to get a normalized absolutized version of the pathname<br />
use <a href="http://docs.python.org/library/os.html#os.walk">os.walk</a> to get it's location</p>
<pre><code>import os
exe = 'something.exe'
#if the exe just in... | 7 | 2009-07-14T11:36:46Z | [
"python"
] |
How can I find path to given file? | 1,124,810 | <p>I have a file, for example "something.exe" and I want to find path to this file<br>
How can I do this in python?</p>
| 2 | 2009-07-14T11:30:36Z | 1,124,851 | <p>if you absolutely do not know where it is, the only way is to find it starting from root c:\</p>
<pre><code>import os
for r,d,f in os.walk("c:\\"):
for files in f:
if files == "something.exe":
print os.path.join(r,files)
</code></pre>
<p>else, if you know that there are only few places y... | 2 | 2009-07-14T11:38:23Z | [
"python"
] |
How can I find path to given file? | 1,124,810 | <p>I have a file, for example "something.exe" and I want to find path to this file<br>
How can I do this in python?</p>
| 2 | 2009-07-14T11:30:36Z | 1,124,862 | <p>Uh... This question is a bit unclear.</p>
<p>What do you mean "have"? Do you have the name of the file? Have you opened it? Is it a file object? Is it a file descriptor? What???</p>
<p>If it's a name, what do you mean with "find"? Do you want to search for the file in a bunch of directories? Or do you know which d... | 1 | 2009-07-14T11:42:19Z | [
"python"
] |
Python error when running script - "IndentationError: unindent does not match any outer indentation" | 1,124,823 | <p>I'm getting an error when I try to run my script </p>
<pre><code>Error:"IndentationError: unindent does not match any outer indentation"
</code></pre>
<p>Code snipet that throws the error:</p>
<pre><code>def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
prin... | -4 | 2009-07-14T11:33:13Z | 1,124,830 | <p>You have an empty try block with nothing underneath it. This is causing the error.</p>
<p>BTW, your <code>sys.exit(1)</code> is off-indent as well. In python, Indentation is important because this is how Python interpreter determines code blocks. So, you have to indent your code properly to get your code running.</... | 0 | 2009-07-14T11:35:06Z | [
"python",
"syntax",
"indentation"
] |
Python error when running script - "IndentationError: unindent does not match any outer indentation" | 1,124,823 | <p>I'm getting an error when I try to run my script </p>
<pre><code>Error:"IndentationError: unindent does not match any outer indentation"
</code></pre>
<p>Code snipet that throws the error:</p>
<pre><code>def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
prin... | -4 | 2009-07-14T11:33:13Z | 1,124,837 | <p>Put a space before <code>sys.exit(1)</code> or remove space before <code>print "[-] Error: Check your phpvuln.txt path and permissions"</code> and <code>print "[-] Update Failed\n"</code>.</p>
| 6 | 2009-07-14T11:35:58Z | [
"python",
"syntax",
"indentation"
] |
Python error when running script - "IndentationError: unindent does not match any outer indentation" | 1,124,823 | <p>I'm getting an error when I try to run my script </p>
<pre><code>Error:"IndentationError: unindent does not match any outer indentation"
</code></pre>
<p>Code snipet that throws the error:</p>
<pre><code>def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
prin... | -4 | 2009-07-14T11:33:13Z | 1,124,875 | <p>The indentation error happens where you have indented incorrectly. Which is clearly visible by the fact that you have different indentation in the line starting with "sys".</p>
| 0 | 2009-07-14T11:44:00Z | [
"python",
"syntax",
"indentation"
] |
Python error when running script - "IndentationError: unindent does not match any outer indentation" | 1,124,823 | <p>I'm getting an error when I try to run my script </p>
<pre><code>Error:"IndentationError: unindent does not match any outer indentation"
</code></pre>
<p>Code snipet that throws the error:</p>
<pre><code>def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
prin... | -4 | 2009-07-14T11:33:13Z | 1,124,878 | <p>As others have mentioned, you need to make sure that each code block has the exact same indentation.</p>
<p>What they haven't mentioned is that the <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">widely adopted convention</a> is to always use exactly 4 spaces per indentation. In your code, the <co... | 3 | 2009-07-14T11:44:45Z | [
"python",
"syntax",
"indentation"
] |
Python error when running script - "IndentationError: unindent does not match any outer indentation" | 1,124,823 | <p>I'm getting an error when I try to run my script </p>
<pre><code>Error:"IndentationError: unindent does not match any outer indentation"
</code></pre>
<p>Code snipet that throws the error:</p>
<pre><code>def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
prin... | -4 | 2009-07-14T11:33:13Z | 3,866,990 | <p>A good way to maintain a standard for your indentations is to use the tab key instead of spacebar.</p>
| 1 | 2010-10-05T19:28:38Z | [
"python",
"syntax",
"indentation"
] |
Interact with a Windows console application via Python | 1,124,884 | <p>I am using python 2.5 on Windows. I wish to interact with a console process via Popen. I currently have this small snippet of code:</p>
<pre><code>p = Popen( ["console_app.exe"], stdin=PIPE, stdout=PIPE )
# issue command 1...
p.stdin.write( 'command1\n' )
result1 = p.stdout.read() # <---- we never return here
# ... | 7 | 2009-07-14T11:46:03Z | 1,124,911 | <p>I think you might want to try to use readline() instead?</p>
<p>Edit: sorry, misunderstoud.</p>
<p>Maybe <a href="http://stackoverflow.com/questions/375427/non-blocking-read-on-a-stream-in-python">this</a> question can help you?</p>
| 0 | 2009-07-14T11:53:33Z | [
"python",
"windows"
] |
Interact with a Windows console application via Python | 1,124,884 | <p>I am using python 2.5 on Windows. I wish to interact with a console process via Popen. I currently have this small snippet of code:</p>
<pre><code>p = Popen( ["console_app.exe"], stdin=PIPE, stdout=PIPE )
# issue command 1...
p.stdin.write( 'command1\n' )
result1 = p.stdout.read() # <---- we never return here
# ... | 7 | 2009-07-14T11:46:03Z | 1,125,001 | <p>Is it possible that the console app is buffering its output in some way so that it is only being sent to stdout when the pipe is closed? If you have access to the code for the console app, maybe sticking a flush after a batch of output data might help?</p>
<p>Alternatively, is it actually writing to stderr and inst... | 0 | 2009-07-14T12:16:06Z | [
"python",
"windows"
] |
Interact with a Windows console application via Python | 1,124,884 | <p>I am using python 2.5 on Windows. I wish to interact with a console process via Popen. I currently have this small snippet of code:</p>
<pre><code>p = Popen( ["console_app.exe"], stdin=PIPE, stdout=PIPE )
# issue command 1...
p.stdin.write( 'command1\n' )
result1 = p.stdout.read() # <---- we never return here
# ... | 7 | 2009-07-14T11:46:03Z | 1,125,684 | <p>Had the exact same problem here. I dug into DrPython source code and stole <strong>wx.Execute()</strong> solution, which is working fine, especially if your script is already using wx. I never found correct solution on windows platform though...</p>
| 0 | 2009-07-14T14:17:23Z | [
"python",
"windows"
] |
Interact with a Windows console application via Python | 1,124,884 | <p>I am using python 2.5 on Windows. I wish to interact with a console process via Popen. I currently have this small snippet of code:</p>
<pre><code>p = Popen( ["console_app.exe"], stdin=PIPE, stdout=PIPE )
# issue command 1...
p.stdin.write( 'command1\n' )
result1 = p.stdout.read() # <---- we never return here
# ... | 7 | 2009-07-14T11:46:03Z | 1,127,074 | <p>Your problem here is that you are trying to control an interactive application.</p>
<p><code>stdout.read()</code> will continue reading until it has reached the end of the stream, file or pipe. Unfortunately, in case of an interactive program, the pipe is only closed then whe program exits; which is never, if the c... | 5 | 2009-07-14T18:16:02Z | [
"python",
"windows"
] |
Interact with a Windows console application via Python | 1,124,884 | <p>I am using python 2.5 on Windows. I wish to interact with a console process via Popen. I currently have this small snippet of code:</p>
<pre><code>p = Popen( ["console_app.exe"], stdin=PIPE, stdout=PIPE )
# issue command 1...
p.stdin.write( 'command1\n' )
result1 = p.stdout.read() # <---- we never return here
# ... | 7 | 2009-07-14T11:46:03Z | 1,132,933 | <p>Have you tried to force windows end lines?
i.e.</p>
<pre><code>p.stdin.write( 'command1 \r\n' )
p.stdout.readline()
</code></pre>
<p>UPDATE:</p>
<p>I've just checked the solution on windows cmd.exe and it works with readline(). But it has one problem Popen's stdout.readline <strong>blocks</strong>. So if the app... | 2 | 2009-07-15T17:59:19Z | [
"python",
"windows"
] |
Generic object "ownership" in Django | 1,125,006 | <p>Suppose I have the following models:</p>
<pre><code>class User(models.Model):
pass
class A(models.Model):
user = models.ForeignKey(User)
class B(models.Model):
a = models.ForeignKey(A)
</code></pre>
<p>That is, each user owns some objects of type A, and also some of type B. Now, I'm writing a generi... | 1 | 2009-07-14T12:18:08Z | 1,125,038 | <p>The way I would do it is to simply go through the object 'a' on class B. So in the view, I would do:</p>
<pre><code>objects = B.objects.get(user=a.user)
objects += A.objects.get(user=user)
</code></pre>
<p>The reason I would do it this way is because these are essentially two database queries, one to retrieve a bu... | 0 | 2009-07-14T12:24:15Z | [
"python",
"django"
] |
Question about paths in Python | 1,125,399 | <p>let's say i have directory paths looking like this:</p>
<pre><code>this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b
</code></pre>
<p>In Python, how can i split these paths up so they will look like this instead:</p>
<pre><code>a/include
b/... | 1 | 2009-07-14T13:30:55Z | 1,125,426 | <p>what about <a href="http://docs.python.org/library/stdtypes.html?highlight=partition#str.partition" rel="nofollow">partition</a>?<br />
It Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the sep... | 1 | 2009-07-14T13:34:56Z | [
"python",
"operating-system"
] |
Question about paths in Python | 1,125,399 | <p>let's say i have directory paths looking like this:</p>
<pre><code>this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b
</code></pre>
<p>In Python, how can i split these paths up so they will look like this instead:</p>
<pre><code>a/include
b/... | 1 | 2009-07-14T13:30:55Z | 1,125,436 | <p>Perhaps something like this, depends on how hardcoded your prefix is:</p>
<pre><code>def removePrefix(path, prefix):
plist = path.split(os.sep)
pflist = prefix.split(os.sep)
rest = plist[len(pflist):]
return os.path.join(*rest)
</code></pre>
<p>Usage:</p>
<pre><code>print removePrefix("this/is/the... | 3 | 2009-07-14T13:36:03Z | [
"python",
"operating-system"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.