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
How to have a URL like this in Django?
1,788,432
<p>How can I have URLs like <code>example.com/category/catename-operation/</code> in Django?</p> <p>Also in some cases the user enters a space separated category, how can I handle that? E.g if the user enters the category as "my home", then the URL for this category will become <code>example.com/my home/</code> which...
2
2009-11-24T07:25:37Z
1,818,653
<p>You can try an improved version of SlugField called AutoSlugField which is part of <a href="http://code.google.com/p/django-command-extensions/" rel="nofollow">Django Custom Management Command Extensions</a>.</p>
1
2009-11-30T09:05:26Z
[ "python", "django" ]
Reference to Part of List - Python
1,788,608
<p>If I have a list in python, how can I create a reference to part of the list? For example:</p> <pre><code>myList = ["*", "*", "*", "*", "*", "*", "*", "*", "*"] listPart = myList[0:7:3] #This makes a new list, which is not what I want myList[0] = "1" listPart[0] "1" </code></pre> <p>Is this possible and if so...
9
2009-11-24T08:15:26Z
1,788,617
<p>Use a slice object or an islice iterator?</p> <p><a href="http://docs.python.org/library/functions.html#slice" rel="nofollow">http://docs.python.org/library/functions.html#slice</a></p>
3
2009-11-24T08:20:19Z
[ "python", "list" ]
Reference to Part of List - Python
1,788,608
<p>If I have a list in python, how can I create a reference to part of the list? For example:</p> <pre><code>myList = ["*", "*", "*", "*", "*", "*", "*", "*", "*"] listPart = myList[0:7:3] #This makes a new list, which is not what I want myList[0] = "1" listPart[0] "1" </code></pre> <p>Is this possible and if so...
9
2009-11-24T08:15:26Z
1,788,651
<p>I think that it is impossible. It would lead to many possible errors, for example: what when you append the list which is reference to part of a bigger list? Shall next element in big list be replaced, or inserted?</p> <p>As far as I know, silce is internal mechanism for getting elements of the list. They do not cr...
1
2009-11-24T08:27:44Z
[ "python", "list" ]
Reference to Part of List - Python
1,788,608
<p>If I have a list in python, how can I create a reference to part of the list? For example:</p> <pre><code>myList = ["*", "*", "*", "*", "*", "*", "*", "*", "*"] listPart = myList[0:7:3] #This makes a new list, which is not what I want myList[0] = "1" listPart[0] "1" </code></pre> <p>Is this possible and if so...
9
2009-11-24T08:15:26Z
1,788,729
<p>There's nothing in python that really does what you want. Basically you want to write some sort of proxy object.</p>
2
2009-11-24T08:44:04Z
[ "python", "list" ]
Reference to Part of List - Python
1,788,608
<p>If I have a list in python, how can I create a reference to part of the list? For example:</p> <pre><code>myList = ["*", "*", "*", "*", "*", "*", "*", "*", "*"] listPart = myList[0:7:3] #This makes a new list, which is not what I want myList[0] = "1" listPart[0] "1" </code></pre> <p>Is this possible and if so...
9
2009-11-24T08:15:26Z
1,789,539
<p>You can write a <strong>list view</strong> type. Here is something I have written as experiment, it is by no means guaranteed to be complete or bug-free</p> <pre><code>class listview (object): def __init__(self, data, start, end): self.data = data self.start, self.end = start, end def __repr...
3
2009-11-24T11:40:54Z
[ "python", "list" ]
How do I remove something form a list, plus string matching?
1,788,710
<pre><code>[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] </code></pre> <p>Suppose I have this list, with tuples inside. </...
2
2009-11-24T08:39:34Z
1,788,728
<pre><code>the_list = [(a, b) for a, b in the_list if a.isalpha()] </code></pre>
10
2009-11-24T08:43:48Z
[ "python", "regex", "list" ]
How do I remove something form a list, plus string matching?
1,788,710
<pre><code>[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] </code></pre> <p>Suppose I have this list, with tuples inside. </...
2
2009-11-24T08:39:34Z
1,788,732
<p>This uses <a href="http://docs.python.org/3.1/library/string.html#string.ascii%5Fletters" rel="nofollow"><code>string.ascii_letters</code></a>, but <a href="http://stackoverflow.com/questions/1788710/how-do-i-remove-something-form-a-list-plus-string-matching/1788728#1788728">SilentGhost's solution</a> is to be prefe...
0
2009-11-24T08:44:45Z
[ "python", "regex", "list" ]
How do I remove something form a list, plus string matching?
1,788,710
<pre><code>[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] </code></pre> <p>Suppose I have this list, with tuples inside. </...
2
2009-11-24T08:39:34Z
1,788,734
<p>Easiest should be a list comprehension with a regular expression:</p> <pre><code>import re lst = [...] lst = [t for t in lst if re.search(r'\w', t[0])] </code></pre>
3
2009-11-24T08:44:46Z
[ "python", "regex", "list" ]
How do I remove something form a list, plus string matching?
1,788,710
<pre><code>[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] </code></pre> <p>Suppose I have this list, with tuples inside. </...
2
2009-11-24T08:39:34Z
1,788,751
<p>you could use built-in <strong>filter</strong> function too, Its dedicated to that purpose actually.</p> <pre><code>filter(lambda x:x[0].isalpha(),LIST) </code></pre> <p>The result is like this</p> <pre><code>[('news', 15), ('bbc', 8), ('and', 8), ('the', 8), ('music', 5), ('blog', 4), ('world', 4), ('asia...
0
2009-11-24T08:50:29Z
[ "python", "regex", "list" ]
How do I remove something form a list, plus string matching?
1,788,710
<pre><code>[(',', 52), ('news', 15), ('.', 11), ('bbc', 8), ('and', 8), ('the', 8), (':', 6), ('music', 5), ('-', 5), ('blog', 4), ('world', 4), ('asia', 4), ('international', 4), ('on', 4), ('itunes', 4), ('online', 4), ('digital', 3)] </code></pre> <p>Suppose I have this list, with tuples inside. </...
2
2009-11-24T08:39:34Z
1,788,936
<p>@OP, just go through the list items one by one, and check the first element of each item. This is just our simple and basic thought process. No need to think too deeply about being pythonic or not, or using fanciful list comprehensions etc.. keep everything simple.</p> <pre><code>l = [(',', 52), ('news', 15), ('....
1
2009-11-24T09:31:19Z
[ "python", "regex", "list" ]
python inheritance and __init__ functions
1,788,842
<p>I came across the folloqing type of code when looking for some pyQt examples :</p> <pre><code>class DisplayPage(QWizardPage): def __init__(self, *args): apply(QWizardPage.__init__, (self, ) + args) </code></pre> <p>What does *args mean ?<br> What is the purpose of using apply for this type of code ? <...
1
2009-11-24T09:10:18Z
1,788,856
<p><code>*args</code> means that <code>__init__</code> takes any number of positional arguments, all of which will be stored in the list <code>args</code>. For more on that, see <a href="http://stackoverflow.com/questions/287085/what-does-args-and-kwargs-mean"><em>What does *args and **kwargs mean?</em></a></p> <p>Thi...
9
2009-11-24T09:12:50Z
[ "python", "pyqt" ]
python inheritance and __init__ functions
1,788,842
<p>I came across the folloqing type of code when looking for some pyQt examples :</p> <pre><code>class DisplayPage(QWizardPage): def __init__(self, *args): apply(QWizardPage.__init__, (self, ) + args) </code></pre> <p>What does *args mean ?<br> What is the purpose of using apply for this type of code ? <...
1
2009-11-24T09:10:18Z
1,788,858
<p><code>DisplayPage</code> inherits from <code>QWizardPage</code>. Its constructor accepts a variable amount of arguments (which is what *args means), and passes them all to the constructor of its parent, <code>QWizardPage</code></p> <p>It's better to say:</p> <pre><code>super(DisplayPage, self).__init__(*args) </co...
3
2009-11-24T09:13:07Z
[ "python", "pyqt" ]
python inheritance and __init__ functions
1,788,842
<p>I came across the folloqing type of code when looking for some pyQt examples :</p> <pre><code>class DisplayPage(QWizardPage): def __init__(self, *args): apply(QWizardPage.__init__, (self, ) + args) </code></pre> <p>What does *args mean ?<br> What is the purpose of using apply for this type of code ? <...
1
2009-11-24T09:10:18Z
1,788,861
<p>"Variable length argument lists": <a href="http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/" rel="nofollow">http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/</a></p> <p>Basically, it's just saying, take all the arguments that were passed to DisplayPage's __init...
1
2009-11-24T09:14:05Z
[ "python", "pyqt" ]
python inheritance and __init__ functions
1,788,842
<p>I came across the folloqing type of code when looking for some pyQt examples :</p> <pre><code>class DisplayPage(QWizardPage): def __init__(self, *args): apply(QWizardPage.__init__, (self, ) + args) </code></pre> <p>What does *args mean ?<br> What is the purpose of using apply for this type of code ? <...
1
2009-11-24T09:10:18Z
1,789,026
<p>In a parameter list (<strong>definition of</strong> a function) *args is Python's way of representing "variable arguments" (called "varargs" in C and C like languages). In an argument list (a <strong>call to</strong> a function) *args has the complementary meaning ... it "applies" the function to the value of the v...
0
2009-11-24T09:49:03Z
[ "python", "pyqt" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,018
<p>The question mark will not have white space either side or a line break/end-of-string after it, if it is in a url</p>
0
2009-11-24T09:46:50Z
[ "python", "regex", "string", "url" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,024
<p>You can for example check if the question mark is immediately followed by a non-space, non-line break character. But I guess that a more safe way would be to strip any possible URL from the string before searching the question mark on it.</p>
1
2009-11-24T09:48:41Z
[ "python", "regex", "string", "url" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,028
<p>If question mark is always there, you could check like</p> <pre><code>if question.strip().endswith("?") and "://" not in question: # do something ? </code></pre> <p>If you really want to parse real sentence, you may need nltk, I am not sure for that case.</p> <p>p.s this is just an sample if the text is fixed...
2
2009-11-24T09:49:12Z
[ "python", "regex", "string", "url" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,036
<p>This regex finds question marks following a word character, and followed by either whitespace or the end of the string/line. Not perfect, but should catch most cases...</p> <pre><code>\w\?[$\s] </code></pre> <p>Edit (lack of caffeine strikes...):</p> <p>That should have been:</p> <pre><code>\w\?(\s|$) </code></p...
3
2009-11-24T09:50:40Z
[ "python", "regex", "string", "url" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,049
<p>Essentially what others say is correct. There should be no whitespace before the <code>?</code>. If the question is entered by a user, things get more ambiguous however.</p> <p>In that case a proper parser using a context free grammar may yield better results. Even with questions not having a question mark at the e...
2
2009-11-24T09:54:29Z
[ "python", "regex", "string", "url" ]
I am trying to determine if a string is a Question. How can I analyze the "?" symbol (python)
1,789,009
<p>This is a question:</p> <pre><code>"Where is the car?" </code></pre> <p>This is NOT a question:</p> <pre><code>"Check this out: http://domain.com/?q=test" </code></pre> <p>How do I write a function to analyze a string so that we know for sure it is a question and not <strong>part of a URL</strong>? </p>
0
2009-11-24T09:44:19Z
1,789,361
<p>A probably not very robust approach that you might be able to get some traction with would be to look for "question words" in strings that end with question marks. In English, most question sentences or clauses (i.e. following a comma) start with "who", "what", "where", "when", "how", "why", "can", "may", "will", "...
0
2009-11-24T11:06:53Z
[ "python", "regex", "string", "url" ]
Trying to get a terminal to work in Emacs
1,789,064
<p>I've been having a lot of problems with emacs and trying to get the terminal to work with:</p> <pre><code> M-x term </code></pre> <p>I installed cygwin and I fixed up my .emacs to include the paths:</p> <pre><code> (setenv "PATH" (concat "c:/cygwin/bin;" (getenv "PATH"))) (setq exec-path (cons "c:/cygwi...
3
2009-11-24T09:57:42Z
1,789,121
<p>Are you using the default 'Cygwin Bash Shell'? This is the one that launches inside a Windows cmd shell, and you can't drag to resize the screen. This shell is horribly broken, because of the underlying Windows component. Try using something like <strong>rxvt</strong>, or one of the putty forks.</p> <p>If this i...
3
2009-11-24T10:12:40Z
[ "python", "emacs", "ssh", "cygwin" ]
Trying to get a terminal to work in Emacs
1,789,064
<p>I've been having a lot of problems with emacs and trying to get the terminal to work with:</p> <pre><code> M-x term </code></pre> <p>I installed cygwin and I fixed up my .emacs to include the paths:</p> <pre><code> (setenv "PATH" (concat "c:/cygwin/bin;" (getenv "PATH"))) (setq exec-path (cons "c:/cygwi...
3
2009-11-24T09:57:42Z
19,280,145
<blockquote> <p>SSH also gives the error:</p> <p>"Pseudo-terminal will not be allocated because stdin is not a terminal."</p> </blockquote> <p>To solve this problem in NTEmacs (NOT cygwin's emacs), I did the following:</p> <ol> <li>Install cygwin's gcc</li> <li>Use it to compile <a href="https://github.com/S...
1
2013-10-09T18:45:07Z
[ "python", "emacs", "ssh", "cygwin" ]
How to get the next token (int, float or string) from a file in Python?
1,789,114
<p>Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java?</p> <pre><code>File file = new File("something"); Scanner myinput = new Scanner(file); double a = myinput.nextDouble(); String s = myinput.next(); </code></pre> <p>I'd like to ignore whitespaces, tabs...
6
2009-11-24T10:11:15Z
1,789,146
<p>if your file is *.ini alike text files, you could use ConfigParser module</p> <p>There is few examples out there.</p> <p><a href="http://docs.python.org/library/configparser.html" rel="nofollow">http://docs.python.org/library/configparser.html</a></p> <p>and pyparsing will do that for other purpose, I think. I h...
0
2009-11-24T10:18:44Z
[ "python" ]
How to get the next token (int, float or string) from a file in Python?
1,789,114
<p>Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java?</p> <pre><code>File file = new File("something"); Scanner myinput = new Scanner(file); double a = myinput.nextDouble(); String s = myinput.next(); </code></pre> <p>I'd like to ignore whitespaces, tabs...
6
2009-11-24T10:11:15Z
1,789,157
<p>Probably you can take a look at <a href="http://www.dabeaz.com/ply/" rel="nofollow">PLY</a></p>
0
2009-11-24T10:21:14Z
[ "python" ]
How to get the next token (int, float or string) from a file in Python?
1,789,114
<p>Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java?</p> <pre><code>File file = new File("something"); Scanner myinput = new Scanner(file); double a = myinput.nextDouble(); String s = myinput.next(); </code></pre> <p>I'd like to ignore whitespaces, tabs...
6
2009-11-24T10:11:15Z
1,789,203
<p>Check out the shlex-module in the standard library: <a href="http://docs.python.org/library/shlex.html">http://docs.python.org/library/shlex.html</a></p> <pre><code>import shlex import StringIO # use in place of files list(shlex.shlex(StringIO.StringIO('Some tokens. 123, 45.67 "A string with whitespace"'))) </code...
10
2009-11-24T10:29:50Z
[ "python" ]
How to get the next token (int, float or string) from a file in Python?
1,789,114
<p>Is there some way to just get the next token from a file in Python, as for example the Scanner class does in Java?</p> <pre><code>File file = new File("something"); Scanner myinput = new Scanner(file); double a = myinput.nextDouble(); String s = myinput.next(); </code></pre> <p>I'd like to ignore whitespaces, tabs...
6
2009-11-24T10:11:15Z
1,789,229
<p>I don't think there is really something around that sophisticated.</p> <p>But you can take a look at the following options</p> <ul> <li><a href="http://docs.python.org/library/re.html#re.split" rel="nofollow"><code>use re.split(pattern, string)</code></a> and get what you want by providing regex's</li> <li>There i...
1
2009-11-24T10:35:31Z
[ "python" ]
Django, Import tables as models
1,789,191
<p>I would like to know if it's possible to use django over existing db tables that defines the models.</p> <p>Instead of defining models in order to create db tables</p>
1
2009-11-24T10:28:24Z
1,789,215
<p>Did you look in <a href="http://docs.djangoproject.com/en/dev/howto/legacy-databases/">the documentation</a>?</p>
6
2009-11-24T10:32:25Z
[ "python", "database", "django", "django-models" ]
Django, Import tables as models
1,789,191
<p>I would like to know if it's possible to use django over existing db tables that defines the models.</p> <p>Instead of defining models in order to create db tables</p>
1
2009-11-24T10:28:24Z
1,790,087
<p>i think you are looking for inspectdb</p>
3
2009-11-24T13:32:06Z
[ "python", "database", "django", "django-models" ]
Clustering text in Python
1,789,254
<p>I need to cluster some text documents and have been researching various options. It looks like LingPipe can cluster plain text without prior conversion (to vector space etc), but it's the only tool I've seen that explicitly claims to work on strings.</p> <p>Are there any Python tools that can cluster text directly...
22
2009-11-24T10:43:39Z
1,789,423
<p>It seems to be possible by using simple UNIX command line tools to extract the text contents of those documents into text files, then using a pure Python solution for the actual clustering.</p> <p>I found a code snippet for clustering data in general:</p> <p><a href="http://www.daniweb.com/code/snippet216641.html"...
2
2009-11-24T11:18:47Z
[ "python", "cluster-analysis", "nlp" ]
Clustering text in Python
1,789,254
<p>I need to cluster some text documents and have been researching various options. It looks like LingPipe can cluster plain text without prior conversion (to vector space etc), but it's the only tool I've seen that explicitly claims to work on strings.</p> <p>Are there any Python tools that can cluster text directly...
22
2009-11-24T10:43:39Z
1,791,009
<p>The quality of text-clustering depends mainly on two factors:</p> <ol> <li><p>Some notion of similarity between the documents you want to cluster. For example, it's easy to distinguish between newsarticles about sports and politics in vector space via tfidf-cosine-distance. It's a lot harder to cluster product-revi...
43
2009-11-24T15:58:35Z
[ "python", "cluster-analysis", "nlp" ]
Clustering text in Python
1,789,254
<p>I need to cluster some text documents and have been researching various options. It looks like LingPipe can cluster plain text without prior conversion (to vector space etc), but it's the only tool I've seen that explicitly claims to work on strings.</p> <p>Are there any Python tools that can cluster text directly...
22
2009-11-24T10:43:39Z
10,309,649
<p>There is Python library <a href="http://www.nltk.org/" rel="nofollow">NLTK</a> that supports linguistic analysis including clustering text</p>
1
2012-04-25T05:08:53Z
[ "python", "cluster-analysis", "nlp" ]
Django, is possible to run two different versions?
1,789,285
<p>I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in ...
4
2009-11-24T10:50:12Z
1,789,315
<p>Of course it is, but it will require a bit of nestling. It depends mainly on which server you use.</p> <p>The key point is the $PYTHONPATH. This variable stores, where Python looks for modules to embed. If you use</p> <pre><code>import django.conf </code></pre> <p>it really looks through all dirs in $PYTHONPATH a...
1
2009-11-24T10:57:04Z
[ "python", "django", "hosting", "shared-hosting" ]
Django, is possible to run two different versions?
1,789,285
<p>I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in ...
4
2009-11-24T10:50:12Z
1,789,330
<p>Yes, you could. I have blogged about this at length over <a href="http://ayaz.wordpress.com/2009/04/05/a-guide-to-configuring-two-different-django-versions-for-development/" rel="nofollow">here</a>.</p>
5
2009-11-24T10:59:10Z
[ "python", "django", "hosting", "shared-hosting" ]
Django, is possible to run two different versions?
1,789,285
<p>I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in ...
4
2009-11-24T10:50:12Z
1,790,143
<p>It is possible, but in my experience of an environment that used different versions of both Django and Python it tends to end up getting messy, especially if you have more than one developer working on the projects. Each developer then needs to maintain two versions of Django and remember which features they can and...
0
2009-11-24T13:41:07Z
[ "python", "django", "hosting", "shared-hosting" ]
Django, is possible to run two different versions?
1,789,285
<p>I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in ...
4
2009-11-24T10:50:12Z
1,790,443
<p>When you have more than one site on a server, you should consider using something like <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">virtualenv</a>.</p> <p>Using that you can setup different virtual environments and place site specific packages and such in there, instead of messing up your site-pa...
6
2009-11-24T14:32:00Z
[ "python", "django", "hosting", "shared-hosting" ]
Django, is possible to run two different versions?
1,789,285
<p>I have a server on which I have two sites built with Django and Python, one site is major site is build with an older version of django, the other with the newer release, I have upgraded to the new release and major aspects of my other site have broken, is it possible to tell the site to use a different version in ...
4
2009-11-24T10:50:12Z
1,791,248
<p>I use <a href="http://www.wigwam-framework.org/" rel="nofollow">Wigwam</a> for this sort of thing. It's a heavy-handed approach &mdash; there's a separate build of each of Apahce, Python, Django, etc. &mdash; but it works quite well.</p>
0
2009-11-24T16:33:16Z
[ "python", "django", "hosting", "shared-hosting" ]
How to parse *.py file with python?
1,789,301
<p>I'd like to parse Python source in order to try making a basic source code converter from Python to Go.</p> <ul> <li>What module should I use?</li> <li>Should I proceed or not?</li> <li>If I should proceed, how?</li> </ul>
2
2009-11-24T10:54:06Z
1,789,342
<p>Have a look at the <a href="http://docs.python.org/library/language.html" rel="nofollow">language services</a> packages, particularly the <a href="http://docs.python.org/library/ast.html" rel="nofollow">ast</a>.</p> <p>My guess is that if you don't already have a solid grasp of both parsing as well as code generati...
13
2009-11-24T11:01:07Z
[ "python", "go", "code-conversion" ]
How to parse *.py file with python?
1,789,301
<p>I'd like to parse Python source in order to try making a basic source code converter from Python to Go.</p> <ul> <li>What module should I use?</li> <li>Should I proceed or not?</li> <li>If I should proceed, how?</li> </ul>
2
2009-11-24T10:54:06Z
1,789,450
<p><strong>The Boo Solution</strong></p> <p>Are you trying to make a <em>python-like</em> language, that can compiles into Go? This seems most sensible, as you will want to do Go-specific things (to take advantage of Go features). </p> <p>Look at <a href="http://pyparsing.wikispaces.com" rel="nofollow" title="pyparsi...
3
2009-11-24T11:21:55Z
[ "python", "go", "code-conversion" ]
How to parse *.py file with python?
1,789,301
<p>I'd like to parse Python source in order to try making a basic source code converter from Python to Go.</p> <ul> <li>What module should I use?</li> <li>Should I proceed or not?</li> <li>If I should proceed, how?</li> </ul>
2
2009-11-24T10:54:06Z
1,789,456
<p>There's a good list of parsers rounded-up by <a href="http://nedbatchelder.com/text/python-parsers.html" rel="nofollow">Ned Batchelder</a> which might help.</p>
2
2009-11-24T11:24:05Z
[ "python", "go", "code-conversion" ]
How to parse *.py file with python?
1,789,301
<p>I'd like to parse Python source in order to try making a basic source code converter from Python to Go.</p> <ul> <li>What module should I use?</li> <li>Should I proceed or not?</li> <li>If I should proceed, how?</li> </ul>
2
2009-11-24T10:54:06Z
1,789,614
<p>As for the 'should I go ahead or better not' question: why do you want to do this in the first place?</p> <p>If it's a purely learning exercise, then you don't don't need to ask us whether it's worthwhile. You want to learn, so go right ahead.</p> <p>If it's meant to be a practical tool, then my suggestion is to n...
3
2009-11-24T11:58:08Z
[ "python", "go", "code-conversion" ]
How to find number of bytes taken by python variable
1,789,543
<p>Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have</p> <pre><code>int = 12 print (type(int)) </code></pre> <p>it will print</p> <pre><code>&lt;class 'int'&gt; </code></pre> <p>But i wanted to know how many bytes it has taken on memory? is it possible?</p>
11
2009-11-24T11:41:47Z
1,789,567
<p>You can find the functionality you are looking for <a href="http://docs.python.org/library/sys.html#sys.getsizeof">here</a> (in <code>sys.getsizeof</code> - Python 2.6 and up).</p> <p>Also: don't shadow the <code>int</code> builtin!</p> <pre><code>import sys myint = 12 print sys.getsizeof(myint) </code></pre>
23
2009-11-24T11:47:20Z
[ "python" ]
How to find number of bytes taken by python variable
1,789,543
<p>Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have</p> <pre><code>int = 12 print (type(int)) </code></pre> <p>it will print</p> <pre><code>&lt;class 'int'&gt; </code></pre> <p>But i wanted to know how many bytes it has taken on memory? is it possible?</p>
11
2009-11-24T11:41:47Z
1,789,569
<p>In Python >= 2.6 you can use <a href="http://docs.python.org/library/sys.html#sys.getsizeof" rel="nofollow">sys.getsizeof</a>.</p>
4
2009-11-24T11:48:12Z
[ "python" ]
How to find number of bytes taken by python variable
1,789,543
<p>Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have</p> <pre><code>int = 12 print (type(int)) </code></pre> <p>it will print</p> <pre><code>&lt;class 'int'&gt; </code></pre> <p>But i wanted to know how many bytes it has taken on memory? is it possible?</p>
11
2009-11-24T11:41:47Z
1,789,734
<p>You could also take a look at <a href="http://packages.python.org/Pympler/index.html" rel="nofollow">Pympler</a>, especially its <a href="http://packages.python.org/Pympler/asizeof.html#asizeof" rel="nofollow">asizeof</a> module, which unlike <code>sys.getsizeof</code> works with Python >=2.2.</p>
1
2009-11-24T12:19:38Z
[ "python" ]
How to find number of bytes taken by python variable
1,789,543
<p>Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have</p> <pre><code>int = 12 print (type(int)) </code></pre> <p>it will print</p> <pre><code>&lt;class 'int'&gt; </code></pre> <p>But i wanted to know how many bytes it has taken on memory? is it possible?</p>
11
2009-11-24T11:41:47Z
1,789,885
<p>if you want to know size of int, you can use struct</p> <pre><code>&gt;&gt;&gt; import struct &gt;&gt;&gt; struct.calcsize("i") 4 </code></pre> <p>otherwise, as others already pointed out, use getsizeof (2.6). there is also a <a href="http://code.activestate.com/recipes/544288/">recipe</a> you can try.</p>
6
2009-11-24T12:51:18Z
[ "python" ]
How to find number of bytes taken by python variable
1,789,543
<p>Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have</p> <pre><code>int = 12 print (type(int)) </code></pre> <p>it will print</p> <pre><code>&lt;class 'int'&gt; </code></pre> <p>But i wanted to know how many bytes it has taken on memory? is it possible?</p>
11
2009-11-24T11:41:47Z
27,900,735
<p>on python command prompt, you can use size of function</p> <pre><code> $ import python $ import ctypes $ ctypes.sizeof(ctypes.c_int) </code></pre> <p>and read more on it from <a href="https://docs.python.org/2/library/ctypes.html" rel="nofollow">https://docs.python.org/2/library/ctypes.html</a></p>
0
2015-01-12T11:15:52Z
[ "python" ]
How do I make django's markdown filter transform a carriage return to <br />?
1,790,176
<p>How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag?</p>
5
2009-11-24T13:49:00Z
1,792,354
<p>You could write a <a href="http://docs.djangoproject.com/en/dev/howto/custom-template-tags/" rel="nofollow">custom filter</a> that calls markdown, then does <a href="http://docs.python.org/library/stdtypes.html#str.replace" rel="nofollow">replace</a> on its output.</p>
0
2009-11-24T19:24:40Z
[ "python", "django", "markdown" ]
How do I make django's markdown filter transform a carriage return to <br />?
1,790,176
<p>How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag?</p>
5
2009-11-24T13:49:00Z
1,793,709
<h2>I don't think messing around with the newline syntax is a good idea ...</h2> <p>I agree with Henrik's comment. From the <a href="http://daringfireball.net/projects/markdown/syntax" rel="nofollow">markdown docs</a>:</p> <blockquote> <p>When you do want to insert a <code>&lt;br /&gt;</code> break tag using Markdo...
5
2009-11-24T23:33:32Z
[ "python", "django", "markdown" ]
How do I make django's markdown filter transform a carriage return to <br />?
1,790,176
<p>How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag?</p>
5
2009-11-24T13:49:00Z
1,802,906
<p>There appears to be a <code>linebreaks</code> filter that converts <code>\n</code> characters to either <code>&lt;br&gt;</code> or <code>&lt;p&gt;</code>.<br> See <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#linebreaks" rel="nofollow">linebreaks</a> or <a href="http://docs.djangoproject.com/...
0
2009-11-26T10:36:46Z
[ "python", "django", "markdown" ]
How do I make django's markdown filter transform a carriage return to <br />?
1,790,176
<p>How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag?</p>
5
2009-11-24T13:49:00Z
5,183,209
<p><strong>EDIT: As of the end of June, 2011, the extension below is now included with Python Markdown.</strong></p> <p>Here is a Markdown extension that I wrote and am currently testing on my site to do exactly what you want:</p> <pre><code>""" A python-markdown extension to treat newlines as hard breaks; like Stack...
5
2011-03-03T16:00:09Z
[ "python", "django", "markdown" ]
Adding Version Control / Numbering (?) to Python Project
1,790,235
<p>With my Java projects at present, I have full version control by declaring it as a Maven project. However I now have a Python project that I'm about to tag <code>0.2.0</code> which has no version control. Therefore should I come accross this code at a later date, I won't no what version it is. </p> <p>How do I add ...
2
2009-11-24T13:58:35Z
1,790,262
<p>Create a <a href="http://docs.python.org/distutils/index.html#distutils-index" rel="nofollow">distutils</a> setup.py file. This is the Python equivalent to maven pom.xml, it looks something like this:</p> <pre><code>from distutils.core import setup setup(name='foo', version='1.0', py_modules=['foo'], ...
2
2009-11-24T14:02:38Z
[ "python", "version-control" ]
Adding Version Control / Numbering (?) to Python Project
1,790,235
<p>With my Java projects at present, I have full version control by declaring it as a Maven project. However I now have a Python project that I'm about to tag <code>0.2.0</code> which has no version control. Therefore should I come accross this code at a later date, I won't no what version it is. </p> <p>How do I add ...
2
2009-11-24T13:58:35Z
1,790,410
<p>First, maven is a build tool and has nothing to do with version control. You don't need a build tool with Python -- there's nothing to "build". </p> <p>Some folks like to create <code>.egg</code> files for distribution. It's as close to a "build" as you get with Python. This is a simple <code>setup.py</code> fi...
5
2009-11-24T14:26:41Z
[ "python", "version-control" ]
Adding Version Control / Numbering (?) to Python Project
1,790,235
<p>With my Java projects at present, I have full version control by declaring it as a Maven project. However I now have a Python project that I'm about to tag <code>0.2.0</code> which has no version control. Therefore should I come accross this code at a later date, I won't no what version it is. </p> <p>How do I add ...
2
2009-11-24T13:58:35Z
1,790,413
<p>Ants's answer is correct, but I would like to add that your modules can define a <code>__version__</code> variable, according to <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 8</a>, which can be populated manually or via Subversion or CVS, e.g. if you have a module thingy, with a file <code>t...
2
2009-11-24T14:27:15Z
[ "python", "version-control" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
1,790,532
<p>Logical <code>and</code> across all elements in <code>a_list</code>:</p> <pre><code>all(a_list) </code></pre> <p>Logical <code>or</code> across all elements in <code>a_list</code>:</p> <pre><code>any(a_list) </code></pre> <hr> <p>If you feel creative, you can also do:</p> <pre><code>import operator def my_all(...
69
2009-11-24T14:46:24Z
[ "python" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
1,790,558
<p><a href="http://docs.python.org/library/functions.html">Reduce</a> can do this:</p> <pre><code>reduce(lambda a,b: a and b, alist, True) </code></pre> <p>As fortran mentioned, all is the most succinct way to do it. But reduce answers the more general question "How to apply a logical operator to all elements in a py...
9
2009-11-24T14:49:39Z
[ "python" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
1,790,576
<p>ANDing and ORing is easy:</p> <pre><code>&gt;&gt;&gt; some_list = [True] * 100 # OR &gt;&gt;&gt; any(some_list) True #AND &gt;&gt;&gt; all(some_list) True &gt;&gt;&gt; some_list[0] = False &gt;&gt;&gt; any(some_list) True &gt;&gt;&gt; all(some_list) False </code></pre> <p>NOTing is also fairly easy:</p> <pre><cod...
28
2009-11-24T14:52:37Z
[ "python" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
1,790,604
<p>As the other answers show, there are multiple ways to accomplish this task. Here's another solution that uses functions from the standard library:</p> <pre><code>from functools import partial apply_and = all apply_or = any apply_not = partial(map, lambda x: not x) if __name__ == "__main__": ls = [True, True, ...
0
2009-11-24T14:56:06Z
[ "python" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
1,790,608
<p>The idiom for such operations is to use the <code>reduce</code> function (global in Python 2.X, in module <code>functools</code> in Python 3.X) with an appropriate binary operator either taken from the <code>operator</code> module or coded explicitly. In your case, it's <code>operator.and_</code></p> <pre><code>red...
4
2009-11-24T14:56:49Z
[ "python" ]
How to apply a logical operator to all elements in a python list
1,790,520
<p>I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.</p> <pre><code>def apply_and(alist): if len(alist) &gt; 1: return alist[0] and apply_and(alist[1:]) else: return alist[0] </code></pre> <p>Any suggestions on how...
29
2009-11-24T14:44:40Z
6,478,660
<p>Here's another solution:</p> <pre><code>def my_and(a_list): return not (False in a_list) def my_or(a_list): return True in a_list </code></pre> <p>ANDing all elements will return True if all elements are True, hence no False in a list. ORing is similar, but it should return True if at least one True value...
2
2011-06-25T15:18:52Z
[ "python" ]
Running average in Python
1,790,550
<p>Is there a pythonic way to <strong>build up a list that contains a running average</strong> of some function?</p> <p>After reading a fun little piece about <a href="http://www.johndcook.com/Cauchy%5Festimation.html">Martians, black boxes, and the Cauchy distribution</a>, I thought it would be fun to calculate a run...
13
2009-11-24T14:48:41Z
1,790,600
<p>You could write a generator:</p> <pre><code>def running_average(): sum = 0 count = 0 while True: sum += cauchy(3,1) count += 1 yield sum/count </code></pre> <p>Or, given a generator for Cauchy numbers and a utility function for a running sum generator, you can have a neat generator expression:</p...
14
2009-11-24T14:55:39Z
[ "python", "list-comprehension", "moving-average" ]
Running average in Python
1,790,550
<p>Is there a pythonic way to <strong>build up a list that contains a running average</strong> of some function?</p> <p>After reading a fun little piece about <a href="http://www.johndcook.com/Cauchy%5Festimation.html">Martians, black boxes, and the Cauchy distribution</a>, I thought it would be fun to calculate a run...
13
2009-11-24T14:48:41Z
1,790,789
<p>I've got two possible solutions here for you. Both are just generic running average functions that work on any list of numbers. (could be made to work with any iterable)</p> <p><strong>Generator based:</strong></p> <pre><code>nums = [cauchy(3,1) for x in xrange(10)] def running_avg(numbers): for count in xra...
4
2009-11-24T15:26:48Z
[ "python", "list-comprehension", "moving-average" ]
Running average in Python
1,790,550
<p>Is there a pythonic way to <strong>build up a list that contains a running average</strong> of some function?</p> <p>After reading a fun little piece about <a href="http://www.johndcook.com/Cauchy%5Festimation.html">Martians, black boxes, and the Cauchy distribution</a>, I thought it would be fun to calculate a run...
13
2009-11-24T14:48:41Z
1,791,168
<p>You could use coroutines. They are similar to generators, but allows you to send in values. Coroutines was added in Python 2.5, so this won't work in versions before that.</p> <pre><code>def running_average(): sum = 0.0 count = 0 value = yield(float('nan')) while True: sum += value c...
6
2009-11-24T16:23:24Z
[ "python", "list-comprehension", "moving-average" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
1,790,817
<p>Have you tried</p> <pre><code>rfc822.parsedate_tz(date) # ? </code></pre> <p>More on RFC822, <a href="http://docs.python.org/library/rfc822.html" rel="nofollow">http://docs.python.org/library/rfc822.html</a></p> <p>It's deprecated (parsedate_tz is now in <a href="http://docs.python.org/library/email.util.html#ema...
2
2009-11-24T15:32:21Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
1,790,885
<p><code>email.utils</code> has a <code>parsedate()</code> function for the RFC 2822 format, which as far as I know is not deprecated.</p> <pre><code>&gt;&gt;&gt; import email.utils &gt;&gt;&gt; import time &gt;&gt;&gt; import datetime &gt;&gt;&gt; email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0100') (2009, 11, 16...
21
2009-11-24T15:42:23Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
8,339,750
<p>Use <a href="https://docs.python.org/2/library/email.util.html#email.utils.parsedate_tz" rel="nofollow"><code>email.utils.parsedate_tz(date)</code></a>:</p> <pre><code>msg=email.message_from_file(open(file_name)) date=None date_str=msg.get('date') if date_str: date_tuple=email.utils.parsedate_tz(date_str) i...
20
2011-12-01T10:22:20Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
23,117,071
<p>In Python 3.3+, <code>email</code> message can parse the headers for you:</p> <pre><code>import email import email.policy headers = email.message_from_file(file, policy=email.policy.default) print(headers.get('date').datetime) # -&gt; 2009-11-16 13:32:02+01:00 </code></pre> <p>Since Python 3.2+, it works if you r...
1
2014-04-16T18:14:18Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
29,082,754
<h1><code>ValueError: 'z' is a bad directive in format...</code></h1> <p>(note: I have to stick to python 2.7 in my case)</p> <p>I have had a similar problem parsing commit dates from the output of <code>git log --date=iso8601</code> which actually isn't the ISO8601 format (hence the addition of <code>--date=iso8601-...
-1
2015-03-16T17:04:07Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Parsing date with timezone from an email?
1,790,795
<p>I am trying to retrieve date from an email. At first it's easy:</p> <pre><code>message = email.parser.Parser().parse(file) date = message['Date'] print date </code></pre> <p>and I receive:</p> <pre><code>'Mon, 16 Nov 2009 13:32:02 +0100' </code></pre> <p>But I need a nice datetime object, so I use:</p> <pre><co...
21
2009-11-24T15:27:45Z
37,823,519
<pre><code># Parses Nginx' format of "01/Jan/1999:13:59:59 +0400" # Unfortunately, strptime doesn't support %z for the UTC offset (despite what # the docs actually say), hence the need # for this function. def parseDate(dateStr): date = datetime.datetime.strptime(dateStr[:-6], "%d/%b/%Y:%H:%M:%S") offsetDir = d...
0
2016-06-14T23:04:26Z
[ "python", "datetime", "timezone", "format", "rfc5322" ]
Compact Class DSL in python
1,790,856
<p>I want to have compact class based python DSLs in the following form:</p> <pre><code>class MyClass(Static): z = 3 def _init_(cls, x=0): cls._x = x def set_x(cls, x): cls._x = x def print_x_plus_z(cls): print cls._x + cls.z @property def x(cls): return cls._...
-1
2009-11-24T15:38:20Z
1,791,245
<p>To give a <em>class</em> (as opposed to <em>its instances</em>) a property, you need to have that property object as an attribute of the class's metaclass (so you'll probably need to make a custom metaclass to avoid inflicting that property upon other classes with the same metaclass). Similarly for special methods s...
2
2009-11-24T16:33:02Z
[ "python", "class", "singleton", "properties", "dsl" ]
Matching multiple regex groups and removing them
1,791,097
<p>I have been given a file that I would like to extract the useful data from. The format of the file goes something like this:</p> <pre><code>LINE: 1 TOKENKIND: somedata TOKENKIND: somedata LINE: 2 TOKENKIND: somedata LINE: 3 </code></pre> <p>etc...</p> <p>What I would like to do is remove LINE: and the line number...
2
2009-11-24T16:12:15Z
1,791,154
<p>How about replacing <code>(^LINE: \d+$)|(^\w+:)</code> with an empty string <code>""</code>? </p> <p>Use <code>\n</code> instead of <code>^</code> and <code>$</code> to remove unwanted empty lines also.</p>
1
2009-11-24T16:21:08Z
[ "python", "regex", "lexical-analysis" ]
Matching multiple regex groups and removing them
1,791,097
<p>I have been given a file that I would like to extract the useful data from. The format of the file goes something like this:</p> <pre><code>LINE: 1 TOKENKIND: somedata TOKENKIND: somedata LINE: 2 TOKENKIND: somedata LINE: 3 </code></pre> <p>etc...</p> <p>What I would like to do is remove LINE: and the line number...
2
2009-11-24T16:12:15Z
1,791,186
<pre><code>import re x = '''LINE: 1 TOKENKIND: somedata TOKENKIND: somedata LINE: 2 TOKENKIND: somedata LINE: 3''' junkre = re.compile(r'(\s*LINE:\s*\d*\s*)|(\s*TOKENKIND:)', re.DOTALL) print junkre.sub('', x) </code></pre>
4
2009-11-24T16:26:15Z
[ "python", "regex", "lexical-analysis" ]
Matching multiple regex groups and removing them
1,791,097
<p>I have been given a file that I would like to extract the useful data from. The format of the file goes something like this:</p> <pre><code>LINE: 1 TOKENKIND: somedata TOKENKIND: somedata LINE: 2 TOKENKIND: somedata LINE: 3 </code></pre> <p>etc...</p> <p>What I would like to do is remove LINE: and the line number...
2
2009-11-24T16:12:15Z
1,794,011
<p>no need to use regex in Python. Its Python after all, not Perl. Think simple and use its string manipulation capabilities</p> <pre><code>f=open("file") for line in f: if line.startswith("LINE:"): continue if "TOKENKIND" in line: print line.split(" ",1)[-1].strip() f.close() </code></pre>
2
2009-11-25T00:55:30Z
[ "python", "regex", "lexical-analysis" ]
Genshi: Nested for loops
1,791,252
<p>I need to generate a HTML using a Genshi template. The Html is, basicaly a very long html with tables. The data comes in a simple CSV, so, i read it with python, i put it into a list[] and then i call the template and send the variable (the list)</p> <p>Actually i solved it by doing something like this in the templ...
1
2009-11-24T16:34:24Z
1,791,422
<pre><code>&lt;table&gt; &lt;tr py:for="i in t"&gt; &lt;td py:for="e in tp[i]"&gt; ${e}s &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre>
2
2009-11-24T16:58:39Z
[ "python", "csv", "genshi" ]
Python: Invalid Syntax with test data using Pyparser
1,791,269
<p>Using pyparser, I am trying to create a very simple parser for the S-Expression language. I have written a very small grammar. Here is my code:</p> <pre><code>from pyparsing import * alphaword = Word(alphas) integer = Word(nums) sexp = Forward() LPAREN = Suppress("(") RPAREN = Suppress(")") sexp &lt;&lt...
2
2009-11-24T16:36:58Z
1,791,296
<p>parentheses on a previous line are not closed.</p> <pre><code>sexp &lt;&lt; ( alphaword | integer | ( LPAREN + ZeroOrMore(sexp) + RPAREN) </code></pre> <p>Needs more )'s</p>
4
2009-11-24T16:41:06Z
[ "python", "syntax", "syntax-error", "pyparsing" ]
Printing several binary data fields from Google DataStore?
1,791,580
<p>I'm using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I'd like to present this data to a computer requesting it, all fields at the same time. Now, the problem is I don't know how to write it out in a way that the ot...
1
2009-11-24T17:17:50Z
1,792,068
<p>One solution that would leverage your json investment would be to simply convert the binary data to something that json can support. For example, <a href="http://en.wikipedia.org/wiki/Base64" rel="nofollow">Base64</a> encoding might work well for you. You could treat the output of your BAse64 encoder just like you...
1
2009-11-24T18:43:39Z
[ "python", "json", "google-app-engine", "binary" ]
Printing several binary data fields from Google DataStore?
1,791,580
<p>I'm using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I'd like to present this data to a computer requesting it, all fields at the same time. Now, the problem is I don't know how to write it out in a way that the ot...
1
2009-11-24T17:17:50Z
1,793,333
<p>Several easy options:</p> <ul> <li><a href="http://docs.python.org/library/base64.html" rel="nofollow">base64</a> encode your data - meaning you can still use JSON.</li> <li>Use <a href="http://code.google.com/p/protobuf/" rel="nofollow">Protocol Buffers</a>.</li> <li>Prefix each field with its length - either as a...
2
2009-11-24T22:21:35Z
[ "python", "json", "google-app-engine", "binary" ]
Creating container relationship in declarative SQLAlchemy
1,791,713
<p>My Python / SQLAlchemy application manages a set of nodes, all derived from a base class <em>Node</em>. I'm using SQLAlchemy's polymorphism features to manage the nodes in a SQLite3 table. Here's the definition of the base <em>Node</em> class:</p> <pre><code>class Node(db.Base): __tablename__ = 'nodes' id ...
3
2009-11-24T17:41:41Z
1,791,923
<p>You need an additional table for many-to-many relation:</p> <pre><code>nodes_list_nodes = Table( 'nodes_list_nodes', metadata, Column('parent_id', None, ForeignKey('nodes_list.id'), nullable=False), Column('child_id', None, ForeignKey(Node.id), nullable=False), PrimaryKeyConstraint('parent_id', 'chi...
5
2009-11-24T18:15:38Z
[ "python", "sqlalchemy" ]
Stacking numpy recarrays without losing their recarrayness
1,791,791
<p>Suppose I make two recarrays with the same dtype and stack them:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; dt = [('foo', int), ('bar', float)] &gt;&gt;&gt; a = np.empty(2, dtype=dt).view(np.recarray) &gt;&gt;&gt; b = np.empty(3, dtype=dt).view(np.recarray) &gt;&gt;&gt; c = np.hstack((a,b)) </code>...
5
2009-11-24T17:54:18Z
1,793,291
<p>I don't know. Most likely it's a bug/feature that's never been implemented. <code>numpy.hstack</code> is basically a wrapper around a function in <code>numpy.core.fromnumeric</code>. Numeric is one of the two predecessors of numpy. Most functions in numpy have a convention to output the same type as the input by...
6
2009-11-24T22:12:20Z
[ "python", "numpy", "recarray" ]
Stacking numpy recarrays without losing their recarrayness
1,791,791
<p>Suppose I make two recarrays with the same dtype and stack them:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; dt = [('foo', int), ('bar', float)] &gt;&gt;&gt; a = np.empty(2, dtype=dt).view(np.recarray) &gt;&gt;&gt; b = np.empty(3, dtype=dt).view(np.recarray) &gt;&gt;&gt; c = np.hstack((a,b)) </code>...
5
2009-11-24T17:54:18Z
3,323,070
<p>Incidentally, you can also use:</p> <pre><code>c = np.concatenate((a,b)) </code></pre> <p>or </p> <pre><code>c = np.r_[a, b] </code></pre> <p>( Source: <a href="http://www.mail-archive.com/numpy-discussion@scipy.org/msg14599.html" rel="nofollow">this mailing list message</a> )</p>
0
2010-07-23T23:25:46Z
[ "python", "numpy", "recarray" ]
Stacking numpy recarrays without losing their recarrayness
1,791,791
<p>Suppose I make two recarrays with the same dtype and stack them:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; dt = [('foo', int), ('bar', float)] &gt;&gt;&gt; a = np.empty(2, dtype=dt).view(np.recarray) &gt;&gt;&gt; b = np.empty(3, dtype=dt).view(np.recarray) &gt;&gt;&gt; c = np.hstack((a,b)) </code>...
5
2009-11-24T17:54:18Z
14,613,280
<p>Alternatively, there are some helper utilities in <a href="http://pyopengl.sourceforge.net/pydoc/numpy.lib.recfunctions.html" rel="nofollow"><code>numpy.lib.recfunctions</code></a> which I stumbled across <a href="http://www.astropython.org/resource/2011/7/recfunctions" rel="nofollow">here</a>. This module has funct...
3
2013-01-30T20:38:38Z
[ "python", "numpy", "recarray" ]
Losing session data when user logs in
1,791,818
<p>I have been working on a shop that is built in Python on the back of the django framework, everything was working fine until I noticed that when a user proceeds to the checkout and is requested to log in they do so and their basket empties...obvioulsy this is not a great thing for a basket to do, I was wondering wh...
2
2009-11-24T17:58:11Z
1,794,248
<p>This will probably be something to do with session management.</p> <p>A user arrives at your site without first logging in, adds a few things to their basket, and then proceeds to the checkout. Upon first arrival at your site a session is established for this user. This might be done through cookies, a session id t...
1
2009-11-25T02:13:10Z
[ "python", "django", "session", "e-commerce" ]
Losing session data when user logs in
1,791,818
<p>I have been working on a shop that is built in Python on the back of the django framework, everything was working fine until I noticed that when a user proceeds to the checkout and is requested to log in they do so and their basket empties...obvioulsy this is not a great thing for a basket to do, I was wondering wh...
2
2009-11-24T17:58:11Z
1,814,068
<p>Are you running two django instances on the same machine? If so, check that <code>SESSION_COOKIE_NAME</code> is set to something different for each instance.</p> <p>We had the problem that instances using sessions using the same <code>SESSION_COOKIE_NAME</code> had very sporadic (read bizarre) behaviour.</p>
1
2009-11-28T23:07:16Z
[ "python", "django", "session", "e-commerce" ]
Django is_valid() not working with modelformset_factory
1,791,942
<p>I've created a simple contact form using the modelformset_factory to build the form in the view using the DB model. The issue that I am having is that the <strong>is_valid()</strong> check before the save() is not working. When I submit the form with empty fields it still passes the <strong>is_valid()</strong> and...
1
2009-11-24T18:18:38Z
1,792,242
<p>Your issue is that providing 0 items is a valid formset, there is no minimum validation. I'd provide a custom BaseModelFormset subclass that's clean() method just checked for a minimum of one obj.</p>
2
2009-11-24T19:07:58Z
[ "python", "django", "django-models", "django-forms" ]
Django is_valid() not working with modelformset_factory
1,791,942
<p>I've created a simple contact form using the modelformset_factory to build the form in the view using the DB model. The issue that I am having is that the <strong>is_valid()</strong> check before the save() is not working. When I submit the form with empty fields it still passes the <strong>is_valid()</strong> and...
1
2009-11-24T18:18:38Z
1,792,966
<p>Did you really want a <code>formset</code>? I suspect if you have a contacts form with only one instance of the <code>Response</code> in then you want a <code>ModelForm</code>...</p> <pre><code>class ResponseForm(forms.ModelForm): class Meta: model = Response fields=('name','email','phone', ...
1
2009-11-24T21:15:28Z
[ "python", "django", "django-models", "django-forms" ]
Python: dynamic class generation: overwrite members
1,792,104
<p>I have a python class hierarchy, that I want to extend at runtime. Furthermore every class in this hierarchy has a static attribute 'dict', that I want to overwrite in every subclass. Simplyfied it looks like this:</p> <h3>'dict' is a protected (public but with leading underscore) member</h3> <pre><code>class A(ob...
4
2009-11-24T18:48:35Z
1,792,141
<p>The Java or C++ concepts of "protected" and "private" do not apply. The naming convention Python does a little, but not what you're imagining.</p> <p>The <code>__name</code> does some name mangling, making it hard to access because the name is obscured.</p> <p>Your <code>_dict</code> and <code>__dict</code> are s...
1
2009-11-24T18:54:07Z
[ "python", "inheritance", "name-mangling", "dynamic-class-creation" ]
Python: dynamic class generation: overwrite members
1,792,104
<p>I have a python class hierarchy, that I want to extend at runtime. Furthermore every class in this hierarchy has a static attribute 'dict', that I want to overwrite in every subclass. Simplyfied it looks like this:</p> <h3>'dict' is a protected (public but with leading underscore) member</h3> <pre><code>class A(ob...
4
2009-11-24T18:48:35Z
1,792,166
<p>Here is a <a href="http://docs.python.org/tutorial/classes.html#private-variables" rel="nofollow">chapter in documentation about "private" attributes</a>. And I commented you class definition to make it more clear:</p> <pre><code>class C(object): __dict = {} # This creates C.__dict__['_C__dict'] @classmeth...
2
2009-11-24T18:57:21Z
[ "python", "inheritance", "name-mangling", "dynamic-class-creation" ]
Python: dynamic class generation: overwrite members
1,792,104
<p>I have a python class hierarchy, that I want to extend at runtime. Furthermore every class in this hierarchy has a static attribute 'dict', that I want to overwrite in every subclass. Simplyfied it looks like this:</p> <h3>'dict' is a protected (public but with leading underscore) member</h3> <pre><code>class A(ob...
4
2009-11-24T18:48:35Z
1,792,273
<p>phild, as you know, when you prefix an attribute name with double-underscore <code>__</code>, the python interpreter automagically changes (mangles) attribute name from <code>__attribute</code> to <code>_CLS__attribute</code>, where CLS is the class name.</p> <p>However, when you say</p> <p><code>return type(name,...
3
2009-11-24T19:12:33Z
[ "python", "inheritance", "name-mangling", "dynamic-class-creation" ]
Looping through files in a folder
1,792,312
<p>I'm fairly new when it comes to programming, and have started out learning python.</p> <p>What I want to do is to recolour sprites for a game, and I am given the original colours, followed by what they are to be turned into. Each sprite has between 20 and 60 angles, so looping through each one in the folder for eac...
32
2009-11-24T19:19:07Z
1,792,349
<p>The path is wrong because the backslashes need to be doubled up - backslash is an escape for special characters.</p> <p><code>os.listdir</code> does not return open files, it returns filenames. You need to open the file using the filename.</p>
3
2009-11-24T19:24:26Z
[ "python", "file", "loops" ]
Looping through files in a folder
1,792,312
<p>I'm fairly new when it comes to programming, and have started out learning python.</p> <p>What I want to do is to recolour sprites for a game, and I am given the original colours, followed by what they are to be turned into. Each sprite has between 20 and 60 angles, so looping through each one in the folder for eac...
32
2009-11-24T19:19:07Z
1,792,350
<p><a href="http://docs.python.org/library/os.html">os.listdir()</a> returns a list of file names. Thus, <code>filename</code> is a string. You need to open the file before iterating on it, I guess.</p> <p>Also, be careful with backslashes in strings. They are mostly used for special escape sequences, so you need to e...
34
2009-11-24T19:24:28Z
[ "python", "file", "loops" ]
Looping through files in a folder
1,792,312
<p>I'm fairly new when it comes to programming, and have started out learning python.</p> <p>What I want to do is to recolour sprites for a game, and I am given the original colours, followed by what they are to be turned into. Each sprite has between 20 and 60 angles, so looping through each one in the folder for eac...
32
2009-11-24T19:19:07Z
1,792,374
<pre><code>for pix in filename: </code></pre> <p>iterates over the letters of the filename. So that's certainly not what you want. You'll probably want to replace that line by:</p> <pre><code>with open(filename) as current_file: for pix in current_file: </code></pre> <p>(assuming Python 2.6) and indent the rest ...
10
2009-11-24T19:28:48Z
[ "python", "file", "loops" ]
Packet Queue in Python?
1,792,320
<p>is there any way to queue packets to a socket in Python? I've been looking for something like the <code>libipq</code> library, but can't find anything equivalent. </p> <p>Here's what I'm trying to accomplish:</p> <ol> <li>Create tcp socket connection between server and client (both under my control).</li> <li>Try ...
2
2009-11-24T19:20:26Z
1,793,077
<p>I suggest you implement an application protocol, so when the client receives data it acknowledges it to the server, with a serial number for each bit of data.</p> <p>The server then keeps note of which serial number the client needs next and sends it. If the connection breaks then the server re-makes it.</p> <p>T...
0
2009-11-24T21:35:39Z
[ "python", "sockets", "queue", "packet" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,792,381
<p>They're not used for the same kinds of things, in general. Python is very useful for prototyping since it is so simple, but isn't always the best choice when speed is an important factor, since it is an interpreted language. C++ can be heavily optimized, but as you said might be much more difficult to write and un...
4
2009-11-24T19:30:17Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,792,387
<p>if you are trying to find out whether you are still going to be employed some time later using C++ or Python, don't concern yourself with a single language's longevity. Learn to program. Don't learn to program in <code>&lt;insert a language here&gt;.</code></p> <p>Here is an analogy: If your car is running fine (g...
3
2009-11-24T19:31:09Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,792,402
<p>Why don't you ask the converse question? Unlike C++, Python can give you <a href="http://xkcd.com/353/" rel="nofollow">antigravity</a> and summon <a href="http://xkcd.com/413/" rel="nofollow">souls</a> via its <code>import</code> command. On the other hand, C++'s 'equivalent' -- <code>#include</code> -- only allows ...
9
2009-11-24T19:32:56Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,793,767
<p>Some Python limits :</p> <p><strong>- Python is slow.</strong> It can be improved in many ways (see other answers) but the bare bone cPython is 100 times slower that C/C++. </p> <p><em>This problem is getter more and more mitigated. With Numpy, Pypy and asyncio, most performance problems are not covered, and only ...
31
2009-11-24T23:45:23Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,793,871
<p>Learn a statically typed language and a scripting language. <BR> You can do whatever you want in either language. A well-written C++ code base is easier to maintain/debug than a Python code base written with the same level of competency.</p> <p>If your goal is to do web stuff or scripting, Python is for you. Anythi...
0
2009-11-25T00:13:33Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,794,560
<p>Here's why it's worth learning Python:</p> <p>A comparatively small number of problems are constrained by the speed of the algorithm. A comparatively large number of problems are bounded by the speed of the developer.</p>
25
2009-11-25T04:10:50Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,794,856
<p>From what I've been told, 1% of learning C++ is learning C. 1% is learning the extra basic features. 98% is learning to use the features in a safe, maintainable way, and coping with the dark hairy corners of the language.</p> <p>Learning python will teach you to write code that is safe, and maintainable. I think th...
1
2009-11-25T05:31:22Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,795,061
<p>As others have suggested: learn Python to study algorithms and higher level concepts and use it for prototyping and for places where you can. Learn C/C++ and/or Java for the job market and for cases where you must use it.</p> <p>Python's vastly easier syntax and high level libraries allow you to focus on interface...
0
2009-11-25T06:39:14Z
[ "c++", "python" ]
What are the limits of Python?
1,792,360
<p>I spent a few days reading about C++ and Python and I found that Python is so much simpler and easy to learn. </p> <p>So I wonder does it really worth spending time learning it? Or should I invest that time learning C++ instead?</p> <p>What can C++ do and Python can't ?</p>
6
2009-11-24T19:26:02Z
1,795,473
<p>One significant difference not so far mentioned is the difference between a language like C++ that builds to native code, and a language like Python which by default puts a VM between you and the hardware. For doing low-level work, like coding against the OS kernel, the native language will be the preferred option....
0
2009-11-25T08:38:18Z
[ "c++", "python" ]
Extract URLs out of email in Python
1,792,366
<pre><code>Dear Sam Adams Thanks for your submission to ourdirectory.com URL: http://myurlok.us Please click below link to confirm your submission. http://www.ourdirectory.com/confirm.aspx?id=1247778154270076 Once we receive your comfirmation, your site will be included for process! regards, http://www.ourdirectory....
1
2009-11-24T19:27:51Z
1,792,404
<p>Not easy. One suggestion (taken from the <a href="http://www.regexbuddy.com" rel="nofollow">RegexBuddy</a> library):</p> <pre><code>\b(?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&amp;@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&amp;@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&amp;@#/%=~_|$?!:,.]*\)|[A-Z0-9+&amp;@#/%=~_|$]) </code></pre...
0
2009-11-24T19:33:33Z
[ "python", "email", "url" ]