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
In python is there a way to check if a function is a "generator function" before calling it?
1,871,685
<p>Lets say I have two functions:</p> <pre><code>def foo(): return 'foo' def bar(): yield 'bar' </code></pre> <p>The first one is a normal function, and the second is a generator function. Now I want to write something like this:</p> <pre><code>def run(func): if is_generator_function(func): gen = func() ...
37
2009-12-09T05:05:10Z
1,871,759
<p>Actually, I'm wondering just how useful such a hypothetical <code>is_generator_function()</code> would be really. Consider:</p> <pre><code>def foo(): return 'foo' def bar(): yield 'bar' def baz(): return bar() def quux(b): if b: return foo() else: return bar() </code></pre> <p>W...
13
2009-12-09T05:24:28Z
[ "python", "function", "generator", "coroutine" ]
In python is there a way to check if a function is a "generator function" before calling it?
1,871,685
<p>Lets say I have two functions:</p> <pre><code>def foo(): return 'foo' def bar(): yield 'bar' </code></pre> <p>The first one is a normal function, and the second is a generator function. Now I want to write something like this:</p> <pre><code>def run(func): if is_generator_function(func): gen = func() ...
37
2009-12-09T05:05:10Z
6,416,855
<pre><code>&gt;&gt;&gt; import inspect &gt;&gt;&gt; &gt;&gt;&gt; def foo(): ... return 'foo' ... &gt;&gt;&gt; def bar(): ... yield 'bar' ... &gt;&gt;&gt; print inspect.isgeneratorfunction(foo) False &gt;&gt;&gt; print inspect.isgeneratorfunction(bar) True </code></pre> <ul> <li>New in Python version 2.6</li> </...
42
2011-06-20T20:09:14Z
[ "python", "function", "generator", "coroutine" ]
In python is there a way to check if a function is a "generator function" before calling it?
1,871,685
<p>Lets say I have two functions:</p> <pre><code>def foo(): return 'foo' def bar(): yield 'bar' </code></pre> <p>The first one is a normal function, and the second is a generator function. Now I want to write something like this:</p> <pre><code>def run(func): if is_generator_function(func): gen = func() ...
37
2009-12-09T05:05:10Z
24,263,677
<p>I've implemented a decorator that hooks on the decorated function returned/yielded value. Its basic goes:</p> <pre><code>import types def output(notifier): def decorator(f): def wrapped(*args, **kwargs): r = f(*args, **kwargs) if type(r) is types.GeneratorType: fo...
0
2014-06-17T12:21:00Z
[ "python", "function", "generator", "coroutine" ]
Not possible to do (a, b) += (1, 2) in python?
1,871,786
<p>The following line doesn't seem to work:</p> <pre><code>(count, total) += self._GetNumberOfNonZeroActions((state[0] + x, state[1] - ring, state[2])) </code></pre> <p>I guess it is not possible to use the += operator in this case. I wonder why?</p> <p>edit: Actually what I want is to add to variables count and tot...
3
2009-12-09T05:32:10Z
1,871,809
<p>You are mixing two concepts together. Python supports <a href="http://www.python.org/doc/1.5.1p1/tut/tuples.html" rel="nofollow">tuple unpacking</a> which allows you to <em>assign</em> more that one variable in a single line.</p> <p>The <code>+=</code> operator gets expanded by the interpreter since it is only a s...
5
2009-12-09T05:37:57Z
[ "python" ]
Not possible to do (a, b) += (1, 2) in python?
1,871,786
<p>The following line doesn't seem to work:</p> <pre><code>(count, total) += self._GetNumberOfNonZeroActions((state[0] + x, state[1] - ring, state[2])) </code></pre> <p>I guess it is not possible to use the += operator in this case. I wonder why?</p> <p>edit: Actually what I want is to add to variables count and tot...
3
2009-12-09T05:32:10Z
1,871,814
<p>Your observation is right: <code>a += b</code> for any a and b means the same as <code>a = a + b</code> (except that it may save one evaluation of a). So if <code>a</code> is a tuple, the only thing that can be <code>+=</code>'d to it is another tuple; if <code>a</code> is a temporary unnamed tuple, that <code>+=</...
10
2009-12-09T05:39:16Z
[ "python" ]
Not possible to do (a, b) += (1, 2) in python?
1,871,786
<p>The following line doesn't seem to work:</p> <pre><code>(count, total) += self._GetNumberOfNonZeroActions((state[0] + x, state[1] - ring, state[2])) </code></pre> <p>I guess it is not possible to use the += operator in this case. I wonder why?</p> <p>edit: Actually what I want is to add to variables count and tot...
3
2009-12-09T05:32:10Z
1,872,835
<p>If you want to work on numeric arrays, I recommend using numpy <a href="http://numpy.scipy.org/" rel="nofollow">http://numpy.scipy.org/</a>.</p> <p>It allows you to do this:</p> <pre><code>&gt;&gt;&gt; from numpy import * &gt;&gt;&gt; count_total = array((0,0)) &gt;&gt;&gt; count_total += (1,2) &gt;&gt;&gt; count_...
1
2009-12-09T10:00:49Z
[ "python" ]
Project Ideas for medium level Java/Python developer
1,871,839
<p>I am a Python and Java developer with about 1.5 years of coding experience and I am looking for a fun but challenging project to engage myself in.</p> <p>What are some fun and interesting project ideas that are challenging enough to be note-worthy and useful enough to put into a portfolio?</p> <p>Thanks</p>
2
2009-12-09T05:49:24Z
1,871,876
<p>There are a number of important Python libraries (with extensions coded in C, or Cython, Pyrex, etc) that don't yet run on Jython, the generally excellent Python implementation for JVM. As somebody with reasonably good experience in both Python and Java, why don't you help further "bridge" the two by investigating ...
6
2009-12-09T06:04:15Z
[ "java", "python", "projects" ]
Project Ideas for medium level Java/Python developer
1,871,839
<p>I am a Python and Java developer with about 1.5 years of coding experience and I am looking for a fun but challenging project to engage myself in.</p> <p>What are some fun and interesting project ideas that are challenging enough to be note-worthy and useful enough to put into a portfolio?</p> <p>Thanks</p>
2
2009-12-09T05:49:24Z
1,871,880
<p>Do anything that keeps your interest, show cases your skills, and demonstrates that you can learn on your own. Not knowing your interests or skills I can't tell you "Write such and such application using these Java and Python technologies."</p> <p>Better yet get involved in an exist open-source project on source f...
4
2009-12-09T06:04:51Z
[ "java", "python", "projects" ]
Project Ideas for medium level Java/Python developer
1,871,839
<p>I am a Python and Java developer with about 1.5 years of coding experience and I am looking for a fun but challenging project to engage myself in.</p> <p>What are some fun and interesting project ideas that are challenging enough to be note-worthy and useful enough to put into a portfolio?</p> <p>Thanks</p>
2
2009-12-09T05:49:24Z
1,871,884
<p>Make a ray tracer.</p> <p>I did this for a graduate course in Java and it turned out to be an awesome choice - the result of your work is <b>actually visible</b> and there are a lot of features you can add to increase the complexity of your project. For eg. you can add shaders, forward raytracing, color effects etc...
1
2009-12-09T06:05:40Z
[ "java", "python", "projects" ]
Project Ideas for medium level Java/Python developer
1,871,839
<p>I am a Python and Java developer with about 1.5 years of coding experience and I am looking for a fun but challenging project to engage myself in.</p> <p>What are some fun and interesting project ideas that are challenging enough to be note-worthy and useful enough to put into a portfolio?</p> <p>Thanks</p>
2
2009-12-09T05:49:24Z
1,873,394
<p>I usually do this when mastering a new language.</p> <p><a href="http://homepage.mac.com/s_lott/books/oodesign.html" rel="nofollow">http://homepage.mac.com/s_lott/books/oodesign.html</a></p> <p>Many programming exercises.</p>
1
2009-12-09T11:48:31Z
[ "java", "python", "projects" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,067
<p>Here's another way to do it that is likely faster than Alex's original code:</p> <pre><code>item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: for filename in search_list: if filename.partition(".")[0] == item: matches.append((item,fil...
0
2009-12-09T06:52:28Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,078
<p>You could combine all the items into one regexp like this which will be more efficient</p> <pre><code>import re item_list = ['item1', 'item2'] regex = re.compile("^("+"|".join(item_list)+")\.") search_list = ['item1.exe', 'item2.pdf'] matches = [] for file in search_list: match = regex.match(file) if match:...
2
2009-12-09T06:55:28Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,095
<p>Use splitext to get the filename without the extension:</p> <pre><code>import os.path for item in item_list: for filename in search_list: if item == os.path.splitext(filename)[0]: matches.append((item, file)) </code></pre> <p>It's more correct, but it's also easier to understand what your ...
2
2009-12-09T07:01:28Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,125
<p>I think you should use <code>.rsplit(".",1)</code> for that purpose, regex aren't overkill?</p> <pre><code>&gt;&gt;&gt; item_list = ['item1', 'item2','item3'] &gt;&gt;&gt; search_list = ['item1.exe', 'item2.pdf','item9999.txt'] &gt;&gt;&gt; &gt;&gt;&gt; [(x.rsplit(".",1)[0],x) for x in search_list if x.rsplit(".",1...
0
2009-12-09T07:08:14Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,154
<p>You do not need to use a regex for this since you are doing exact string matches (no wildcards, groups etc) - you can use <strong>str.startswith(..)</strong> instead. This is equivalent to your code:</p> <pre><code>for item in item_list: match = item + "." for file in search_list: if file.startswit...
1
2009-12-09T07:19:06Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,872,164
<pre><code>&gt;&gt;&gt; for file in search_list: ... tomatch=file.split(".")[0] ... if tomatch in item_list: ... found=item_list.index(tomatch) ... matches.append( ( file, item_list[found] ) ) ... &gt;&gt;&gt; print matches [('item1.exe', 'item1'), ('item2.pdf', 'item2')] &gt;&gt;&gt; </code></pre> <p>No nee...
0
2009-12-09T07:21:11Z
[ "python", "regex" ]
How to use re to search for items in one list inside another list in Python
1,872,016
<p>I am reading a list of strings, each of which relate to a file name. However, each string is minus the extension. I have come up with the following code:</p> <pre><code>import re item_list = ['item1', 'item2'] search_list = ['item1.exe', 'item2.pdf'] matches = [] for item in item_list: # Match item in search_li...
0
2009-12-09T06:38:55Z
1,874,237
<p>Avoid <code>re</code> unless you really need it. For simple string matching, you don't really need it.</p> <p>Mark Byers's answer duplicates the original behaviour of keeping <code>matches</code> in <code>item_list</code>-order. If you don't need that, you could do it even more simply/quickly:</p> <pre><code>for f...
1
2009-12-09T14:24:19Z
[ "python", "regex" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,291
<p>This is a lame <a href="http://en.wikipedia.org/wiki/Primality%5Ftest" rel="nofollow">primality test</a>.</p> <p>% is the <a href="http://en.wikipedia.org/wiki/Modulo%5Foperation" rel="nofollow">mod operator</a>. It performs division and returns the remainder rather than the result of the division. For example, 5 /...
4
2009-12-09T07:54:59Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,292
<p>the logic is:</p> <p>if y modulo x is 0, it means that x is a dividor of y, so y has a factor. print that, and break out of the loop.</p> <p>if not, decrease x by 1, and try again.</p> <p>but some things are broken in this code:</p> <ol> <li>the else statement position</li> <li>the fact the 'print y is prime' is...
1
2009-12-09T07:55:07Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,296
<p>I think the program tries to find the biggest prime factors of y. If y is a prime factor it prints this as well.</p>
0
2009-12-09T07:55:19Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,305
<p><code>x = y // 2</code> is for testing the numbers in the range x: <code>2..y/2</code>.<br> A better approach would be to test only the numbers x: <code>2..sqrt(y)</code></p>
0
2009-12-09T07:57:32Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,310
<p>The program prints at least one factor of an integer y, or if it has no factors (other than itself and 1), prints that y is prime.</p> <p>It uses the variable x to try all possible factors greater than one. It starts at the floor of y divided by 2, because no number larger than half of y could be a factor. Using ...
1
2009-12-09T07:59:36Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,312
<p>The code simply checks if the square root of x has been reached. Note that you can check the primality of a number by checking if the integers from 2 up to the square root of x divides x perfectly (without a remainder).</p>
1
2009-12-09T07:59:44Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,332
<p>the % denotes a modulus which gives you the remainder of division...</p> <p>and this code checks for prime Y and also checks if Y is a multiplier of x...</p> <p>x = y // 2 #x=the division or modulus of y , 2</p> <p>while x > 1: #you want to check if this is a division result or a modulus</p> <p>if y % x == ...
0
2009-12-09T08:04:36Z
[ "python", "while-loop", "prime-factoring" ]
While loop example
1,872,261
<pre><code>x = y // 2 # For some y &gt; 1 while x &gt; 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 else: # Normal exit print(y, 'is prime') </code></pre> <p>This is an example for understanding while loop in a book I'm reading, I don't quite understand why...
0
2009-12-09T07:46:36Z
1,872,354
<p>For any number (x) which is not prime, there would be a factor greater than 1 and less than (x/2). 9 = 3*3 The logic is to iterate through all the numbers &lt;= x/2 and check if the number divides.</p>
1
2009-12-09T08:09:43Z
[ "python", "while-loop", "prime-factoring" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
1,872,342
<p>Use a list to hold the key order</p>
3
2009-12-09T08:07:08Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
1,872,345
<p>Dictionaries in Python are implemented as hash tables, which is why the order appears random. You could implement your own variation of a dict that sorts, but you'd lose out on the convenient syntax. Instead, keep track of the order of the keys, too.</p> <p>Initialization:</p> <pre><code>keys = [] myDict = {} </...
1
2009-12-09T08:08:30Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
1,872,350
<p>Try python 2.7 and above, probably 3.1, there is OrderedDict</p> <p><a href="http://www.python.org/">http://www.python.org/</a></p> <p><a href="http://python.org/download/releases/2.7/">http://python.org/download/releases/2.7/</a></p> <pre><code>&gt;&gt;&gt; from collections import OrderedDict &gt;&gt;&gt; d = Or...
25
2009-12-09T08:09:14Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
1,873,179
<p>Implementations of order-preserving dictionaries certainly do exist.</p> <p>There is <a href="http://code.djangoproject.com/browser/django/trunk/django/utils/datastructures.py" rel="nofollow">this one in Django</a>, confusingly called <code>SortedDict</code>, that will work in Python >= 2.3 iirc.</p>
3
2009-12-09T11:10:18Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
19,976,650
<p>There is a very short answer to that.. do this--</p> <p><strong>dictCopy=yourdictname.copy()</strong></p> <p>then use the dictCopy , it will be in the same order.</p>
-1
2013-11-14T11:37:29Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Storing Python dictionary entries in the order they are pushed
1,872,329
<p>Fellows:</p> <p>A Python dictionary is stored in no particular order (mappings have no order), e.g.,</p> <pre><code>&gt;&gt;&gt; myDict = {'first':'uno','second':'dos','third':'tres'} myDict = {'first':'uno','second':'dos','third':'tres'} &gt;&gt;&gt; myDict myDict {'second': 'dos', 'third': 'tres', 'first': 'uno'...
16
2009-12-09T08:04:00Z
28,231,253
<p>Rather Than Explaining The Theoretical Part I'll Give A Simple Example.</p> <pre><code>&gt;&gt;&gt; from collections import OrderedDict &gt;&gt;&gt; my_dictionary=OrderedDict() &gt;&gt;&gt; my_dictionary['foo']=3 &gt;&gt;&gt; my_dictionar['aol']=1 &gt;&gt;&gt; my_dictionary OrderedDict([('foo', 3), ('aol', 1)]) </...
1
2015-01-30T07:32:46Z
[ "python", "sorting", "dictionary", "mapping", "order" ]
Django and multiple databases
1,872,456
<p>My current Django setup uses <a href="http://en.wikipedia.org/wiki/MySQL" rel="nofollow">MySQL</a> as the main database to store models. Now for my project I need to connect to a remote <a href="http://en.wikipedia.org/wiki/PostgreSQL" rel="nofollow">PostgreSQL</a> database and retrieve data from it. Is it possible ...
1
2009-12-09T08:41:25Z
1,872,625
<p>Django Project is working on <a href="http://lazypython.blogspot.com/2009/11/state-of-multidb-in-django.html" rel="nofollow">Multiple Database Support</a>. There is also a recent (Nov 10 2009) blog post about <a href="http://lazypython.blogspot.com/2009/11/state-of-multidb-in-django.html" rel="nofollow">"The state o...
4
2009-12-09T09:26:14Z
[ "python", "django", "postgresql", "multiple-databases" ]
Use Python to extract ListView items from another application
1,872,480
<p>I have an application with a ListView ('SysListView32') control, from which I would like to extract data. The control has 4 columns, only textual data.</p> <p>I have been playing around the following lines (found online somewhere):</p> <pre><code>VALUE_LENGTH = 256 bufferlength_int=struct.pack('i', VALUE_LENGTH) c...
3
2009-12-09T08:50:31Z
1,872,497
<p>If the control is in the same process as your code, it should work. If it's in a different process (as "another application" suggests), then this doesn't work (or at least it shouldn't). Check the error codes, you should get something along the lines of "permission denied": Applications can't see into each others me...
1
2009-12-09T08:56:01Z
[ "python", "listview", "pywin32", "syslistview32" ]
Use Python to extract ListView items from another application
1,872,480
<p>I have an application with a ListView ('SysListView32') control, from which I would like to extract data. The control has 4 columns, only textual data.</p> <p>I have been playing around the following lines (found online somewhere):</p> <pre><code>VALUE_LENGTH = 256 bufferlength_int=struct.pack('i', VALUE_LENGTH) c...
3
2009-12-09T08:50:31Z
1,893,696
<p>Well, it turns out I was wrong on several points there. However it is possible to do by allocating memory inside the target process, constructing the required struct (LVITEM) there, sending the message and reading back the result from the buffer allocated in said process.</p> <p>For the sake of completeness, I atta...
4
2009-12-12T15:06:24Z
[ "python", "listview", "pywin32", "syslistview32" ]
Overriding __getattribute__ for the Model class in Django
1,872,820
<p>In Django, I need to create a model in which an instance of that model can inherit values from another instance of that model in what amounts to a superclass-subclass type relationship. I created a field in this model called parent, which is an optional self-referencing foreign key. I then want to override the __g...
2
2009-12-09T09:57:50Z
1,873,088
<p>Do you need that parent's attributes shadow own ones? If not, you'd better overwrite <code>__getattr__()</code> instead of <code>__getattribute__()</code> which is not called when instance itself has property with this name:</p> <pre><code>def __getattr__(self, name): return getattr(self.parent, name) </code></...
1
2009-12-09T10:51:33Z
[ "python", "django" ]
Overriding __getattribute__ for the Model class in Django
1,872,820
<p>In Django, I need to create a model in which an instance of that model can inherit values from another instance of that model in what amounts to a superclass-subclass type relationship. I created a field in this model called parent, which is an optional self-referencing foreign key. I then want to override the __g...
2
2009-12-09T09:57:50Z
1,873,267
<p>This is fraught with difficulty when done this way.</p> <p>Please reconsider overriding <code>__getattribute__</code> and use a plain old properties for this.</p> <p>I suspect that a simple "getter" with a property names will do eveything you want without bumping into any magic.</p> <p>Yes, you may have several s...
2
2009-12-09T11:25:13Z
[ "python", "django" ]
Overriding __getattribute__ for the Model class in Django
1,872,820
<p>In Django, I need to create a model in which an instance of that model can inherit values from another instance of that model in what amounts to a superclass-subclass type relationship. I created a field in this model called parent, which is an optional self-referencing foreign key. I then want to override the __g...
2
2009-12-09T09:57:50Z
19,278,644
<p>I suppose that overriding <code>__getattribute__</code> or <code>__getattr__</code> may lead to performance cost. Probably there is a better way to solve this problem:</p> <ol> <li>On model instance initialization, if an inheritable field is empty and corresponding parent's field is not, do <code>setattr(self, fiel...
0
2013-10-09T17:20:59Z
[ "python", "django" ]
Extract resulted list data to a xml file in python
1,872,861
<p>How can I extract my resulted list data to an xml file?</p> <p>My resulted list is given below:</p> <p>week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]</p>
0
2009-12-09T10:05:01Z
1,872,914
<p>You can trivially adjust <a href="http://www.postneo.com/projects/pyxml/" rel="nofollow">this</a> to your needs.</p>
1
2009-12-09T10:13:52Z
[ "python", "xml" ]
Extract resulted list data to a xml file in python
1,872,861
<p>How can I extract my resulted list data to an xml file?</p> <p>My resulted list is given below:</p> <p>week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]</p>
0
2009-12-09T10:05:01Z
1,872,919
<p>Since you don't provide any information on how you want to format your XML, i just invented my own notation.</p> <pre><code>week=[{'item': 'Electrelane', 'weight': 140}, {'item': 'Kraftwerk', 'weight': 117},{'item': 'The Flaming Lips', 'weight': 113}] print "&lt;?xml version='1.0' ?&gt;" print "&lt;week&gt;" for d...
1
2009-12-09T10:14:28Z
[ "python", "xml" ]
Extract resulted list data to a xml file in python
1,872,861
<p>How can I extract my resulted list data to an xml file?</p> <p>My resulted list is given below:</p> <p>week=[{'item': Electrelane, 'weight': 140}, {'item': Kraftwerk, 'weight': 117},{'item': The Flaming Lips, 'weight': 113}]</p>
0
2009-12-09T10:05:01Z
1,874,124
<p>Here's some code that uses <a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow"><code>xml.dom.minidom</code></a> to build up the XML document.</p> <pre><code>week=[{'item': 'Electrelane', 'weight': 140}, {'item': 'Kraftwerk', 'weight': 117},{'item': 'The Flaming Lips', 'weight': 113}] from ...
1
2009-12-09T14:04:43Z
[ "python", "xml" ]
Python to Javascript
1,873,135
<p>are there any tools for Windows to convert python to javascript?</p> <p>regards</p> <p>Alberto</p>
6
2009-12-09T11:02:13Z
1,873,148
<p>Check out <a href="http://pyjs.org/">Pyjamas</a></p>
8
2009-12-09T11:04:27Z
[ "javascript", "python" ]
Python to Javascript
1,873,135
<p>are there any tools for Windows to convert python to javascript?</p> <p>regards</p> <p>Alberto</p>
6
2009-12-09T11:02:13Z
1,873,149
<p>Pyjs (formerly Pyjamas)? <a href="http://pyjs.org/">http://pyjs.org/</a></p> <p>There are a few howto pages for Pyjs with django, webpy, etc.: <a href="https://github.com/pyjs/pyjs/wiki">https://github.com/pyjs/pyjs/wiki</a></p>
9
2009-12-09T11:04:31Z
[ "javascript", "python" ]
Python to Javascript
1,873,135
<p>are there any tools for Windows to convert python to javascript?</p> <p>regards</p> <p>Alberto</p>
6
2009-12-09T11:02:13Z
1,873,151
<p>I've found a <a href="http://code.google.com/p/pyjamas/" rel="nofollow">Python-To-Javascript</a> compiler (Pyjamas), but I'm not 100% that's what you're looking for.</p> <p>To be brutally honest, they're extremely different languages with a hugely different syntax and different methods. I doubt you'll find a conver...
2
2009-12-09T11:04:43Z
[ "javascript", "python" ]
Python to Javascript
1,873,135
<p>are there any tools for Windows to convert python to javascript?</p> <p>regards</p> <p>Alberto</p>
6
2009-12-09T11:02:13Z
1,873,165
<p>Another one to look at is <a href="http://www.skulpt.org/">Skulpt</a>.</p>
5
2009-12-09T11:07:06Z
[ "javascript", "python" ]
Django Admin doesn't show entries
1,873,177
<p>If I create a new entry for one particular model it doesn't show up in the django admin.</p> <p>The Agency Model is causing the trouble.</p> <pre><code># catalog.models class Content(models.Model): class Meta: abstract = True BUNDESLAND_CHOICES = ( ('bw', 'Baden-Württemberg'), (...
2
2009-12-09T11:09:43Z
1,873,447
<p>The first manager listed in the model class definition is the one that is used for the admin site and a number of other operations.</p> <p>There have been a number of bugs in Django related to using a manager that does not return all instances as the default manager. IMHO, you are best to use a standard manager as ...
3
2009-12-09T11:57:24Z
[ "python", "django-models", "django-admin" ]
Easiest "daytime" service client in Python?
1,873,214
<p>What's the easiest way to write a daytime client in Python?</p> <p>And if there's more data of unknown size but still plain text - how do I read until the server closes the connection?</p>
1
2009-12-09T11:15:36Z
1,973,685
<p>Use Twisted - it will take some time to get the concept, but it rocks!</p> <p>Start with tutorials <a href="http://twistedmatrix.com/documents/current/core/howto/index.html" rel="nofollow">http://twistedmatrix.com/documents/current/core/howto/index.html</a> - first two should be enough.</p>
0
2009-12-29T09:17:19Z
[ "python", "networking", "tcp" ]
Easiest "daytime" service client in Python?
1,873,214
<p>What's the easiest way to write a daytime client in Python?</p> <p>And if there's more data of unknown size but still plain text - how do I read until the server closes the connection?</p>
1
2009-12-09T11:15:36Z
5,436,764
<p>This works:</p> <pre><code>#!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "time.nist.gov" port = 13 s.connect((host,port)) while True: data = s.recv(10000) if data: print data else: break s.close() </code></pre>
3
2011-03-25T18:45:03Z
[ "python", "networking", "tcp" ]
Easiest "daytime" service client in Python?
1,873,214
<p>What's the easiest way to write a daytime client in Python?</p> <p>And if there's more data of unknown size but still plain text - how do I read until the server closes the connection?</p>
1
2009-12-09T11:15:36Z
5,438,027
<pre><code>#!/usr/bin/env python import socket from contextlib import closing as C address = "time.nist.gov", socket.getservbyname('daytime') with C(socket.create_connection(address, timeout=2)) as conn: with C(conn.makefile()) as f: print f.read(), </code></pre> <p>The solution:</p> <ul> <li>cleanly f...
1
2011-03-25T20:53:12Z
[ "python", "networking", "tcp" ]
Send raw ethernet packet with data field length in type field
1,873,427
<p>I'm trying to send a raw ethernet frame with the length of my data written in the type field. This should be a valid ethernet frame. My code for this looks like this:</p> <pre><code>ethData = "foobar" proto =len(ethData) if proto &lt; 46: proto = 46 soc = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, proto)...
1
2009-12-09T11:55:07Z
1,874,155
<p>Sorry, my fault. I just parsed the wrong portion of the packet and didn't get any output. My bad. The package gets there just like it is supposed to.</p>
0
2009-12-09T14:11:15Z
[ "python", "sockets", "ethernet" ]
How could I get a Frame with a scrollbar in Tkinter?
1,873,575
<p>I'd like to have a <code>Frame</code>, where the user could add as many textfields as needed by the application. </p> <p>The application starts with a textfield, and a button below that textfield. When the user presses the button, a new text entry will be added below the first one ( this may be repeated countless t...
7
2009-12-09T12:26:04Z
1,877,862
<p>If you can use Tix, there is the ScrolledWindow widget which has a <code>window</code> Frame and one or two Scrollbar widgets:</p> <pre><code>import Tix as tk r= tk.Tk() r.title("test scrolled window") sw= tk.ScrolledWindow(r, scrollbar=tk.Y) # just the vertical scrollbar sw.pack(fill=tk.BOTH, expand=1) for i in x...
7
2009-12-10T00:14:59Z
[ "python", "scrollbar", "tkinter", "frame" ]
How could I get a Frame with a scrollbar in Tkinter?
1,873,575
<p>I'd like to have a <code>Frame</code>, where the user could add as many textfields as needed by the application. </p> <p>The application starts with a textfield, and a button below that textfield. When the user presses the button, a new text entry will be added below the first one ( this may be repeated countless t...
7
2009-12-09T12:26:04Z
1,884,142
<p>The following is an example of <em>auto hiding scrollbars</em> that only works if you just use the <em><strong>grid</strong> geometry manager</em>, taken from the <a href="http://effbot.org/zone/tkinter-autoscrollbar.htm" rel="nofollow"><code>effbot.org</code></a> documentation:</p> <pre><code>from tkinter import *...
5
2009-12-10T21:25:24Z
[ "python", "scrollbar", "tkinter", "frame" ]
I need a clean approach for range-checking floats in Python
1,873,625
<p>I'm looking for a simple way of range checking floats in Python where the minimum and maximum bounds may be null.</p> <p>The code in question is: </p> <pre><code> tval = float(-b - discriminant) / float (2*a) if tval &gt;= tmin and tval &lt;= tmax: return tval tval = float(-b + discriminant) ...
3
2009-12-09T12:35:51Z
1,873,750
<p>First, some setup: we'll need a float infinity constant.</p> <pre><code>INF = float(1e3000) </code></pre> <p>or</p> <pre><code>INF = float('inf') # Python 2.6+ </code></pre> <p>First option can be considered portable for practical purposes; just use some really huge value which is guaranteed to be outside the r...
4
2009-12-09T13:03:58Z
[ "python" ]
I need a clean approach for range-checking floats in Python
1,873,625
<p>I'm looking for a simple way of range checking floats in Python where the minimum and maximum bounds may be null.</p> <p>The code in question is: </p> <pre><code> tval = float(-b - discriminant) / float (2*a) if tval &gt;= tmin and tval &lt;= tmax: return tval tval = float(-b + discriminant) ...
3
2009-12-09T12:35:51Z
1,873,784
<p>Maybe I'd define a checker function first, for readability:</p> <pre><code>def inrange(x, min, max): return (min is None or min &lt;= x) and (max is None or max &gt;= x) tval = float(-b - discriminant) / float (2*a) if inrange(tval, tmin, tmax): return tval tval = float(-b + discriminant) / float (2*a) i...
4
2009-12-09T13:12:26Z
[ "python" ]
I need a clean approach for range-checking floats in Python
1,873,625
<p>I'm looking for a simple way of range checking floats in Python where the minimum and maximum bounds may be null.</p> <p>The code in question is: </p> <pre><code> tval = float(-b - discriminant) / float (2*a) if tval &gt;= tmin and tval &lt;= tmax: return tval tval = float(-b + discriminant) ...
3
2009-12-09T12:35:51Z
1,873,913
<p>Using INF from <a href="#1873750" rel="nofollow">atzz's answer</a> (in a way that won't fail when <code>0.0</code> is used):</p> <pre><code>def coalesce(*values): for v in values: if v is not None: return v if coalesce(tmin, -INF) &lt;= tval &lt;= coalesce(tmax, INF): return tval </code></pre> <p>Ho...
3
2009-12-09T13:30:39Z
[ "python" ]
I need a clean approach for range-checking floats in Python
1,873,625
<p>I'm looking for a simple way of range checking floats in Python where the minimum and maximum bounds may be null.</p> <p>The code in question is: </p> <pre><code> tval = float(-b - discriminant) / float (2*a) if tval &gt;= tmin and tval &lt;= tmax: return tval tval = float(-b + discriminant) ...
3
2009-12-09T12:35:51Z
1,874,844
<p>Using INF from atzz's answer</p> <pre><code>if tmin is None: tmin = -INF if tmax is None: tmax = +INF tval = float(-b - discriminant) / float (2*a) if tmin &lt;= tval &lt;= tmax: return tval tval = float(-b + discriminant) / float (2*a) if tmin &lt;= tval &lt;= tmax: return tval # Neither solution was w...
2
2009-12-09T15:55:26Z
[ "python" ]
How to detect an invalid C escaped string using a regular expression?
1,873,652
<p>I would like to find a regular expression (regex) that does detect if you have some invalid escapes in a C double quoted escaped string (where you can find double quotes only escaped).</p> <p>I consider valid <code>\\</code> <code>\n</code> <code>\r</code> <code>\"</code> (the test string is using ")</p> <p>A part...
0
2009-12-09T12:41:16Z
1,873,715
<p>Try this regular expression:</p> <pre><code>^(?:[^\\]+|\\[\\rn"])*(\\(?:[^\\rn"]|$)) </code></pre> <p>If you have a match, you have an invalid escape sequence.</p>
0
2009-12-09T12:56:22Z
[ "python", "c", "regex", "escaping" ]
How to detect an invalid C escaped string using a regular expression?
1,873,652
<p>I would like to find a regular expression (regex) that does detect if you have some invalid escapes in a C double quoted escaped string (where you can find double quotes only escaped).</p> <p>I consider valid <code>\\</code> <code>\n</code> <code>\r</code> <code>\"</code> (the test string is using ")</p> <p>A part...
0
2009-12-09T12:41:16Z
1,873,721
<pre><code>(?:^|[^\\])(?:\\\\)*((?:\"|\\(?:[^\"\\nr]|$))) </code></pre> <p>That's the start of a string, or something that's not a backslash. Then some (possibly zero) properly escaped backslashes, then either an unescaped <code>"</code> or another backslash; if it's another backslash, it must be followed by something...
3
2009-12-09T12:57:49Z
[ "python", "c", "regex", "escaping" ]
String Conversion
1,873,659
<pre><code>s='This is sample' </code></pre> <p>i need to convert like this </p> <pre><code>s='"This is sample"' </code></pre> <p>output=<code>"This is sample"</code></p> <p>how to do this in dynamic<br> Thanks in advance</p>
0
2009-12-09T12:44:18Z
1,873,683
<pre><code>orig = 'This is sample' converted = '"%s"' % orig </code></pre>
5
2009-12-09T12:50:05Z
[ "python", "string" ]
String Conversion
1,873,659
<pre><code>s='This is sample' </code></pre> <p>i need to convert like this </p> <pre><code>s='"This is sample"' </code></pre> <p>output=<code>"This is sample"</code></p> <p>how to do this in dynamic<br> Thanks in advance</p>
0
2009-12-09T12:44:18Z
1,873,692
<pre><code>&gt;&gt;&gt; s= 'This is a sample' &gt;&gt;&gt; s = '"' + s + '"' # or s = '"%s"' % s &gt;&gt;&gt; s '"This is a sample"' &gt;&gt;&gt; print(s) "This is a sample" &gt;&gt;&gt; </code></pre>
1
2009-12-09T12:52:00Z
[ "python", "string" ]
gae python optimization: django filter for language support
1,873,704
<p>I have a filter that I use for lang support in my webapp. But when I publish it to gae it keeps telling me that it the usage of CPU is to high. </p> <p>I think I located the problem to my filters I use for support. I use this in my templates:</p> <pre><code>&lt;h1&gt;{{ "collection.header"|translate:lang }}&lt;/h1...
3
2009-12-09T12:54:04Z
2,109,263
<p>Have you considered switching to standard gettext methods? Gettext is a widely spread approach for internationalization and very well embedded in the Python (and the Django) world.</p> <p>Some links:</p> <ul> <li><p><a href="http://docs.python.org/library/gettext.html" rel="nofollow">Python's gettext module</a></p...
2
2010-01-21T12:51:38Z
[ "python", "django", "google-app-engine", "filter" ]
gae python optimization: django filter for language support
1,873,704
<p>I have a filter that I use for lang support in my webapp. But when I publish it to gae it keeps telling me that it the usage of CPU is to high. </p> <p>I think I located the problem to my filters I use for support. I use this in my templates:</p> <pre><code>&lt;h1&gt;{{ "collection.header"|translate:lang }}&lt;/h1...
3
2009-12-09T12:54:04Z
2,126,340
<p>Your initial question is about high cpu usage, the answer i think is simple, with GAE and databases like BigTable (non-relational) the code with <code>entries.count()</code> is expensive and the <code>for entry in entrie</code> too if you have a lot of data.</p> <p>I think you must have to do a couple of things:</p...
3
2010-01-24T07:47:32Z
[ "python", "django", "google-app-engine", "filter" ]
Display the result on the webpage as soon as the data is available at server
1,873,735
<p>I am writing a cgi page in Python. Let's say a client sends request to my cgi page. My cgi page does the calculation and as soon as it has the first output, it sends back that output to the client, but it will <strong>CONTINUE</strong> to do the calculation and send other responses <strong>AFTER</strong> the first r...
4
2009-12-09T13:00:59Z
1,873,819
<p>Yes thats possible and you don't have do much, as you print data out, server will send it, just to be sure keep flushing stdout</p>
1
2009-12-09T13:17:50Z
[ "python", "cgi" ]
Display the result on the webpage as soon as the data is available at server
1,873,735
<p>I am writing a cgi page in Python. Let's say a client sends request to my cgi page. My cgi page does the calculation and as soon as it has the first output, it sends back that output to the client, but it will <strong>CONTINUE</strong> to do the calculation and send other responses <strong>AFTER</strong> the first r...
4
2009-12-09T13:00:59Z
1,873,936
<p>There are a few techniques.</p> <p>The old-fashioned way is to continue to stream data, and have the browser continue to render it using progressive rendering. So as an old-fashioned CGI, just do <code>sys.stdout.flush()</code>. This shows a partial page that you can keep adding to, but it looks clumsy in the brows...
1
2009-12-09T13:35:03Z
[ "python", "cgi" ]
Display the result on the webpage as soon as the data is available at server
1,873,735
<p>I am writing a cgi page in Python. Let's say a client sends request to my cgi page. My cgi page does the calculation and as soon as it has the first output, it sends back that output to the client, but it will <strong>CONTINUE</strong> to do the calculation and send other responses <strong>AFTER</strong> the first r...
4
2009-12-09T13:00:59Z
1,874,121
<p>The trick in old-fashioned <code>CGI</code> programs is using the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1" rel="nofollow"><code>Transfer-Encoding: chunked</code></a> HTTP header:</p> <blockquote> <p><strong>3.6.1 Chunked Transfer Coding</strong></p> <p>The chunked encoding mod...
1
2009-12-09T14:04:29Z
[ "python", "cgi" ]
Display the result on the webpage as soon as the data is available at server
1,873,735
<p>I am writing a cgi page in Python. Let's say a client sends request to my cgi page. My cgi page does the calculation and as soon as it has the first output, it sends back that output to the client, but it will <strong>CONTINUE</strong> to do the calculation and send other responses <strong>AFTER</strong> the first r...
4
2009-12-09T13:00:59Z
1,874,123
<p>Sure.</p> <p>There's traditional server-driven approach, where the script runs just once, but takes a long time to complete, spitting out bits of page as it goes:</p> <pre><code>import sys, time sys.stdout.write('Content-Type: text/html;charset=utf-8\r\n\r\n') print '&lt;html&gt;&lt;body&gt;' for i in range(10):...
6
2009-12-09T14:04:42Z
[ "python", "cgi" ]
How do I decode garbled text from the Library of Congress?
1,873,754
<p>I am making a z39.50 search in python, but have a problem with decoding search results.</p> <p>The first search result for "harry potter" is apparantly a hebrew version of the book.</p> <p>How can I make this into unicode?</p> <p>This is the minimal code I use to get a post:</p> <pre><code>#!/usr/bin/env python ...
0
2009-12-09T13:05:08Z
1,873,770
<pre><code>r.data.decode('windows-1255').encode('utf-8') </code></pre> <p>you'll have to figure the correct encoding they used, and put that instead of 'windows-1255' (which might work, if you're right about the hebrew guess).</p>
1
2009-12-09T13:10:22Z
[ "python", "encoding", "z39.50" ]
How do I decode garbled text from the Library of Congress?
1,873,754
<p>I am making a z39.50 search in python, but have a problem with decoding search results.</p> <p>The first search result for "harry potter" is apparantly a hebrew version of the book.</p> <p>How can I make this into unicode?</p> <p>This is the minimal code I use to get a post:</p> <pre><code>#!/usr/bin/env python ...
0
2009-12-09T13:05:08Z
1,874,117
<p>I'm trying to reproduce your problem, but am getting into the Python equivalent of "DLL Hell". Please specify what version of each of (Python, PyZ3950, and PLY) that you are using.</p> <p>You will note from the error message that there are 788 bytes of ASCII before you get a non-ASCII byte. Doesn't sound like Hebre...
1
2009-12-09T14:04:04Z
[ "python", "encoding", "z39.50" ]
How do I decode garbled text from the Library of Congress?
1,873,754
<p>I am making a z39.50 search in python, but have a problem with decoding search results.</p> <p>The first search result for "harry potter" is apparantly a hebrew version of the book.</p> <p>How can I make this into unicode?</p> <p>This is the minimal code I use to get a post:</p> <pre><code>#!/usr/bin/env python ...
0
2009-12-09T13:05:08Z
35,427,954
<p>U need to convert Marc data for this: U can use code below:</p> <pre><code>from pymarc import MARCReader temp_list = [] for i in range(0, 2):# You can take len(res) here for all results temp_list.append(res[i].data) for i in range(0, 2):# You can take len(res) here for all results reader = MARCReader(temp_l...
0
2016-02-16T08:58:29Z
[ "python", "encoding", "z39.50" ]
How to allow users to change their own passwords in Django?
1,873,806
<p>Can any one point me to code where users can change their own passwords in Django?</p>
55
2009-12-09T13:15:50Z
1,873,855
<blockquote> <p>Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This document explains how things work.</p> </blockquote> <p><a href="http://docs.djangoproject.com/en/dev/topics/auth/">How to change Django passwords</a> </p> <p>Se...
112
2009-12-09T13:21:32Z
[ "python", "django" ]
How to allow users to change their own passwords in Django?
1,873,806
<p>Can any one point me to code where users can change their own passwords in Django?</p>
55
2009-12-09T13:15:50Z
1,873,933
<p>You can also just use the <code>django.contrib.auth.views.password_change</code> view in your URLconf. It uses a default form and template; supplying your own is optional.</p>
21
2009-12-09T13:34:19Z
[ "python", "django" ]
How to allow users to change their own passwords in Django?
1,873,806
<p>Can any one point me to code where users can change their own passwords in Django?</p>
55
2009-12-09T13:15:50Z
37,197,380
<p><code>urls.py</code>:</p> <pre><code>urlpatterns = [ url(r'^accounts/', include('django.contrib.auth.urls')), </code></pre> <p>Template:</p> <pre><code>&lt;a href="{% url 'password_change' %}"&gt;{% trans "Change password" %}&lt;/a&gt; </code></pre> <p>Documented at: <a href="https://docs.djangoproject.com/e...
0
2016-05-12T21:05:36Z
[ "python", "django" ]
How to allow users to change their own passwords in Django?
1,873,806
<p>Can any one point me to code where users can change their own passwords in Django?</p>
55
2009-12-09T13:15:50Z
38,215,981
<p>Very similar to @Ciro's answer, but more specific to the original question (without adding all the authentication views):</p> <p>just add to <code>urlpatterns</code> in <code>urls.py</code>:</p> <pre><code>url('^change-password/$', auth_views.password_change, {'post_change_redirect': 'next_page'}, name='password_c...
0
2016-07-06T03:35:46Z
[ "python", "django" ]
How to implement objects modification trace
1,873,966
<p>With django admin, we have an history of who altered an object and when. I would like to add an "old value", "new value" to this to be able to roll back if needed.</p> <p>Plus I would like every modification made to my objects (also outside of admin) to be recorded as well.</p> <p>The final objective is to be able...
0
2009-12-09T13:40:19Z
1,874,215
<p>I haven't personally tried it but it sounds like you should check out <a href="http://code.google.com/p/django-reversion/" rel="nofollow">django-reversion</a>.</p>
3
2009-12-09T14:22:02Z
[ "python", "django", "django-models", "django-admin", "trace" ]
Overridden attribute access does not work (as expected)
1,874,043
<p>The main objective of the following module, is to provide a kind of "constant" semantics for some names.</p> <pre><code>class ConstantError(Exception): def __init__(self, msg): self._msg = msg class Constant(object): def __init__(self, name): self._name = name def __get__(se...
1
2009-12-09T13:52:35Z
1,874,071
<p>You are missing a <strong>str</strong>() or <strong>unicode</strong>() method in Constants.</p> <p>Add:</p> <pre><code>def __unicode__(self): return self._name </code></pre>
0
2009-12-09T13:56:46Z
[ "python", "attributes", "setattr" ]
Overridden attribute access does not work (as expected)
1,874,043
<p>The main objective of the following module, is to provide a kind of "constant" semantics for some names.</p> <pre><code>class ConstantError(Exception): def __init__(self, msg): self._msg = msg class Constant(object): def __init__(self, name): self._name = name def __get__(se...
1
2009-12-09T13:52:35Z
1,874,105
<p>The descriptor protocol only works on <strong>attributes of the class</strong>, not on attributes of instances of a class. See the <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="nofollow">How-To Guide for Descriptors</a></p>
3
2009-12-09T14:00:58Z
[ "python", "attributes", "setattr" ]
Overridden attribute access does not work (as expected)
1,874,043
<p>The main objective of the following module, is to provide a kind of "constant" semantics for some names.</p> <pre><code>class ConstantError(Exception): def __init__(self, msg): self._msg = msg class Constant(object): def __init__(self, name): self._name = name def __get__(se...
1
2009-12-09T13:52:35Z
6,742,139
<p>I think python prevents classes from accessing the descriptor machinery so that they can be manipulated. Otherwise manipulating the descriptor could get very tricky without some kind of 'magical' function, and if you've noticed python tries to keep a lot of the language machinery accessible. To get around this I'v...
0
2011-07-19T03:57:33Z
[ "python", "attributes", "setattr" ]
Checking if a postgresql table exists under python (and probably Psycopg2)
1,874,113
<p>How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.</p>
26
2009-12-09T14:02:56Z
1,874,135
<pre><code>select exists(select relname from pg_class where relname = 'mytablename' and relkind='r'); </code></pre>
2
2009-12-09T14:08:06Z
[ "python", "postgresql", "psycopg2" ]
Checking if a postgresql table exists under python (and probably Psycopg2)
1,874,113
<p>How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.</p>
26
2009-12-09T14:02:56Z
1,874,147
<p>I don't know the psycopg2 lib specifically, but the following query can be used to check for existence of a table:</p> <pre><code>SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_catalog='DB_NAME' AND table_schema='public' AND table_name='TAB...
18
2009-12-09T14:09:52Z
[ "python", "postgresql", "psycopg2" ]
Checking if a postgresql table exists under python (and probably Psycopg2)
1,874,113
<p>How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.</p>
26
2009-12-09T14:02:56Z
1,874,268
<p>How about:</p> <pre><code>&gt;&gt;&gt; import psycopg2 &gt;&gt;&gt; conn = psycopg2.connect("dbname='mydb' user='username' host='localhost' password='foobar'") &gt;&gt;&gt; cur = conn.cursor() &gt;&gt;&gt; cur.execute("select * from information_schema.tables where table_name=%s", ('mytable',)) &gt;&gt;&gt; bool(cur...
42
2009-12-09T14:28:57Z
[ "python", "postgresql", "psycopg2" ]
Checking if a postgresql table exists under python (and probably Psycopg2)
1,874,113
<p>How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.</p>
26
2009-12-09T14:02:56Z
21,485,584
<p>The first answer did not work for me. I found success checking for the relation in pg_class:</p> <p>def table_exists(con, table_str):</p> <pre><code>exists = False try: cur = con.cursor() cur.execute("select exists(select relname from pg_class where relname='" + table_str + "')") exists = cur.fetchone(...
1
2014-01-31T16:52:55Z
[ "python", "postgresql", "psycopg2" ]
Checking if a postgresql table exists under python (and probably Psycopg2)
1,874,113
<p>How can I determine if a table exists using the Psycopg2 Python library? I want a true or false boolean.</p>
26
2009-12-09T14:02:56Z
30,906,154
<pre><code>#!/usr/bin/python # -*- coding: utf-8 -*- import psycopg2 import sys con = None try: con = psycopg2.connect(database='testdb', user='janbodnar') cur = con.cursor() cur.execute('SELECT 1 from mytable') ver = cur.fetchone() print ver //здесь наш код при ус...
0
2015-06-18T03:50:25Z
[ "python", "postgresql", "psycopg2" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
1,874,292
<p>Yes, you need to write for Python 2.4 syntax to support all of 2.4 - 2.7 in the same codebase.</p> <p>Some changes in Python 2.6 and 2.7 aim to make it a bit easier to write compatible code with 3.x, but you have to drop support for 2.5 and below to do that.</p>
1
2009-12-09T14:32:12Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
1,874,415
<p>If the differences between versions are not extreme, you can try isolating them into a separate package or module in which you write version-specific code to act as an adaptation layer.</p> <p>In a trivial fashion, this can be done without the separate module in simple cases, such as when a new version of Python ma...
0
2009-12-09T14:54:35Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
1,874,431
<p>First of call you need to keep in mind that Python 2.x shares mostly the same syntax which is backward compatible, new features &amp; additions aside. There are other things to consider that aren't necessarily errors, such as DeprecationWarning messages that while not detrimental, are ugly and can cause confusion. ...
0
2009-12-09T14:58:10Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
1,874,435
<p>There seem be different answers to your problem.</p> <p>First, if you want to offer <em>all</em> functions for <em>all</em> python versions then yes, you're probably stuck with using the smallest possible functionality subset - hence writing your code for Python 2.4. Or you could backport features from newer interp...
1
2009-12-09T14:58:50Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
1,876,225
<p>You could try <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">virtualenv</a> and distribute your application using a single Python version. This may or may not be practical in your case though.</p>
0
2009-12-09T19:28:53Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Supporting different versions of Python
1,874,154
<p>This subject has been disturbing me for some time.</p> <p>For <a href="http://garlicsim.org">my Python project</a> I wanted to be able to support Python versions 2.4 to 3.1. I thought a bit about how to do this, and eventually decided to have four separate forks of the source code for four different versions of Pyt...
8
2009-12-09T14:11:11Z
9,842,269
<p>We have related problem, a large system that supports both jython and cpython back to 2.4. Basically you need to isolate code that needs to be written differently into a hopefully small set of modules, and have things get imported conditionally.</p> <pre><code># module svn.py import sys if sys.platform.startswith(...
0
2012-03-23T15:48:09Z
[ "python", "python-3.x", "compatibility", "multiple-versions" ]
Pythonic way to get the largest item in a list
1,874,194
<p>Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.</p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; &gt;&gt;...
17
2009-12-09T14:19:45Z
1,874,210
<pre><code>max(a_list, key=operator.itemgetter(1)) </code></pre>
45
2009-12-09T14:21:16Z
[ "python" ]
Pythonic way to get the largest item in a list
1,874,194
<p>Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.</p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; &gt;&gt;...
17
2009-12-09T14:19:45Z
1,874,225
<p>You could use the <code>max</code> function.</p> <blockquote> <p>Help on built-in function max in module __builtin__:</p> <p>max(...)</p> <p>max(iterable[, key=func]) -> value</p> <p>max(a, b, c, ...[, key=func]) -> value</p> <p>With a single iterable argument, return its largest item. Wit...
7
2009-12-09T14:23:06Z
[ "python" ]
Pythonic way to get the largest item in a list
1,874,194
<p>Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.</p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; &gt;&gt;...
17
2009-12-09T14:19:45Z
1,874,232
<p>Use the <a href="http://docs.python.org/library/functions.html#max" rel="nofollow"><code>max()</code></a> function or do it FP style:</p> <pre><code>reduce(lambda max, c: max if c &lt;= max else c, [1, 6, 9, 2, 4, 0, 8, 1, 3]) </code></pre>
2
2009-12-09T14:23:36Z
[ "python" ]
Pythonic way to get the largest item in a list
1,874,194
<p>Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.</p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; &gt;&gt;...
17
2009-12-09T14:19:45Z
20,164,055
<p>The key can also be a lambda, for example:</p> <pre><code>people = [("Tom", 33), ("Dick", 55), ("Harry", 44)] oldest = max(people, key=lambda p: p[1]) </code></pre> <p>For some reason, using a lambda makes it feel more like "my code" is doing the work, compared with <code>itemgetter</code>. I think this feels part...
5
2013-11-23T15:20:06Z
[ "python" ]
Pythonic way to get the largest item in a list
1,874,194
<p>Is there a better way of doing this? I don't really need the list to be sorted, just scanning through to get the item with the greatest specified attribute. I care most about readability but sorting a whole list to get one item seems a bit wasteful.</p> <pre><code>&gt;&gt;&gt; import operator &gt;&gt;&gt; &gt;&gt;...
17
2009-12-09T14:19:45Z
35,045,978
<p>Some people mentioned the following solution:</p> <pre><code>max(nameOfList, key=len) </code></pre> <p>However, this solution only returns the first sequential element of largest size. So, for example, in the case of list ["ABC","DCE"], only the first item of the list is returned.</p> <p>To remedy this, I found t...
0
2016-01-27T19:10:20Z
[ "python" ]
Python port forwarding/multiplexing server
1,874,331
<p>I would like to make server that listen on UDP port 162 (SNMP trap) and then forwards this traffic to multiple clients. Also important is that the source port &amp; address stays same (address spoofing).</p> <p>I guess that best tool for this would be <a href="http://en.wikipedia.org/wiki/Twisted_%28software%29" re...
7
2009-12-09T14:37:30Z
1,898,106
<p>I am not comfortable with twisted or scapy, but it's quite straightforward to do this with vanilla python sockets. An extra advantage of that is that it will be even more portable. This code works in my limited tests:</p> <pre><code>#!/usr/bin/python from socket import * bufsize = 1024 # Modify to suit your needs...
5
2009-12-13T22:56:54Z
[ "python", "networking", "iptables", "packet-mangling" ]
Python port forwarding/multiplexing server
1,874,331
<p>I would like to make server that listen on UDP port 162 (SNMP trap) and then forwards this traffic to multiple clients. Also important is that the source port &amp; address stays same (address spoofing).</p> <p>I guess that best tool for this would be <a href="http://en.wikipedia.org/wiki/Twisted_%28software%29" re...
7
2009-12-09T14:37:30Z
12,490,387
<p>A different but sort of related solution to forwarding ports, not multiplexing (not answering the specific question but hopefully matching related ones - this is what I was looking for at least):</p> <p><a href="http://www.linux-support.com/cms/forward-network-connections-with-python/" rel="nofollow">http://www.lin...
0
2012-09-19T07:38:31Z
[ "python", "networking", "iptables", "packet-mangling" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,620
<p>Backslash:</p> <pre><code>s = "this is my really, really, really, really, really, really" + \ "really long string that I'd like to shorten." </code></pre> <p>or wrap in parens:</p> <pre><code>s = ("this is my really, really, really, really, really, really" + "really long string that I'd like to shorten....
4
2009-12-09T15:24:01Z
[ "python", "string", "pep8" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,622
<p>With an \ you can expand statments to multiple lines</p> <pre><code>s = "this is my really, really, really, really, really, really" + \ "really long string that I'd like to shorten." </code></pre> <p>should work.</p>
1
2009-12-09T15:24:08Z
[ "python", "string", "pep8" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,623
<p>You lost a space, and you probably need a line continuation character, ie. a <code>\</code>.</p> <pre><code>s = "this is my really, really, really, really, really, really" + \ " really long string that I'd like to shorten." </code></pre> <p>or even:</p> <pre><code>s = "this is my really, really, really, real...
10
2009-12-09T15:24:16Z
[ "python", "string", "pep8" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,628
<p>I think the most important word in your question was "suggests". </p> <p>Coding standards are funny things. Often the guidance they provide has a really good basis when it was written (e.g. most terminals being unable to show > 80 characters on a line), but over time they become functionally obsolete, but still r...
8
2009-12-09T15:24:55Z
[ "python", "string", "pep8" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,635
<p>Implicit concatenation might be the cleanest solution:</p> <pre><code>s = "this is my really, really, really, really, really, really," \ " really long string that I'd like to shorten." </code></pre> <p><strong>Edit</strong> Todd's answer below (using brackets rather than line continuation) is better for all th...
48
2009-12-09T15:25:52Z
[ "python", "string", "pep8" ]
PEP8 - 80 Characters - Big Strings
1,874,592
<p>Due to the sheer annoyance of figuring out what to Google, I've decided to risk what any reputation I had to ask this rather simple question.</p> <p>As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.</p> <pre><code>s = "this is my really, real...
80
2009-12-09T15:20:54Z
1,874,679
<p>Also, because neighboring string constants are automatically concatenated, you can code it like this too:</p> <pre><code>s = ("this is my really, really, really, really, really, really, " "really long string that I'd like to shorten.") </code></pre> <p>Note no plus sign, and I added the extra comma and spac...
112
2009-12-09T15:31:49Z
[ "python", "string", "pep8" ]