title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Python -- Regex -- How to find a string between two sets of strings | 849,912 | <p>Consider the following:</p>
<pre><code><div id=hotlinklist>
<a href="foo1.com">Foo1</a>
<div id=hotlink>
<a href="/">Home</a>
</div>
<div id=hotlink>
<a href="/extract">Extract</a>
</div>
<div id=hotlink>
<a href="/si... | 5 | 2009-05-11T20:23:49Z | 850,003 | <p>Use <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> or <a href="http://www.codespeak.net/lxml/" rel="nofollow">lxml</a> if you need to parse HTML.</p>
<p>Also, what is it that you really need to do? Find the last link? Find the third link? Find the link that points to /sitem... | 1 | 2009-05-11T20:43:23Z | [
"python",
"regex",
"string",
"tags"
] |
Python -- Regex -- How to find a string between two sets of strings | 849,912 | <p>Consider the following:</p>
<pre><code><div id=hotlinklist>
<a href="foo1.com">Foo1</a>
<div id=hotlink>
<a href="/">Home</a>
</div>
<div id=hotlink>
<a href="/extract">Extract</a>
</div>
<div id=hotlink>
<a href="/si... | 5 | 2009-05-11T20:23:49Z | 851,592 | <p>In order to extract the contents of the tagline:</p>
<pre><code> <a href="/sitemap">Sitemap</a>
</code></pre>
<p>... I would use:</p>
<pre><code> >>> import re
>>> s = '''
<div id=hotlinklist>
<a href="foo1.com">Foo1</a>
<div id=hotlink>... | 4 | 2009-05-12T07:37:33Z | [
"python",
"regex",
"string",
"tags"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,125 | <p>what format do you receive? if it is a file, you can do some sort of bulk load: <a href="http://www.classes.cs.uchicago.edu/archive/2005/fall/23500-1/mysql-load.html" rel="nofollow">http://www.classes.cs.uchicago.edu/archive/2005/fall/23500-1/mysql-load.html</a></p>
| 1 | 2009-05-11T21:16:10Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,127 | <p>For MySQL specifically, the fastest way to load data is using <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html" rel="nofollow">LOAD DATA INFILE</a>, so if you could convert the data into the format that expects, it'll probably be the fastest way to get it into the table.</p>
| 11 | 2009-05-11T21:16:21Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,129 | <p>You can write the rows to a file in the format
"field1", "field2", .. and then use LOAD DATA to load them</p>
<pre><code>data = '\n'.join(','.join('"%s"' % field for field in row) for row in data)
f= open('data.txt', 'w')
f.write(data)
f.close()
</code></pre>
<p>Then execute this:</p>
<pre><code>LOAD DATA INFILE ... | 10 | 2009-05-11T21:16:29Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,273 | <p>If you don't <code>LOAD DATA INFILE</code> as some of the other suggestions mention, two things you can do to speed up your inserts are :</p>
<ol>
<li>Use prepared statements - this cuts out the overhead of parsing the SQL for every insert</li>
<li>Do all of your inserts in a single transaction - this would require... | 4 | 2009-05-11T21:51:33Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,723 | <p>If you can do a hand-rolled <code>INSERT</code> statement, then that's the way I'd go. A single <code>INSERT</code> statement with multiple value clauses is much much faster than lots of individual <code>INSERT</code> statements.</p>
| 4 | 2009-05-12T01:08:52Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 850,886 | <p>This is unrelated to the actual load of data into the DB, but...</p>
<p>If providing a "The data is loading... The load will be done shortly" type of message to the user is an option, then you can run the INSERTs or LOAD DATA asynchronously in a different thread.</p>
<p>Just something else to consider.</p>
| 1 | 2009-05-12T02:23:39Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 851,443 | <p>Regardless of the insert method, you will want to use the InnoDB engine for maximum read/write concurrency. MyISAM will lock the entire table for the duration of the insert whereas InnoDB (under most circumstances) will only lock the affected rows, allowing SELECT statements to proceed.</p>
| 2 | 2009-05-12T06:38:43Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django) | 850,117 | <p>I have a database table with a unique string field and a couple of integer fields. The string field is usually 10-100 characters long.</p>
<p>Once every minute or so I have the following scenario: I receive a list of 2-10 thousand tuples corresponding to the table's record structure, e.g. </p>
<pre><code>[("hello"... | 11 | 2009-05-11T21:13:26Z | 851,898 | <p>I donot know the exact details, but u can use json style data representation and use it as fixtures or something. I saw something similar on Django Video Workshop by Douglas Napoleone. See the videos at <a href="http://www.linux-magazine.com/online/news/django%5Fvideo%5Fworkshop" rel="nofollow">http://www.linux-maga... | 1 | 2009-05-12T09:14:03Z | [
"python",
"sql",
"mysql",
"django",
"insert"
] |
Python multiprocessing on Python 2.6 Win32 (xp) | 850,424 | <p>I tried to copy this example from this Multiprocessing lecture by jesse noller (as recommended in another SO post)[<a href="http://pycon.blip.tv/file/1947354?filename=Pycon-IntroductionToMultiprocessingInPython630.mp4" rel="nofollow">http://pycon.blip.tv/file/1947354?filename=Pycon-IntroductionToMultiprocessingInPyt... | 2 | 2009-05-11T22:46:37Z | 850,440 | <p>Seems from the traceback that you are running the code directly into the python interpreter (REPL).</p>
<p>Don't do that. Save the code in a file and run it from the file instead, with the command:</p>
<pre><code>python myfile.py
</code></pre>
<p>That will solve your issue.</p>
<p><hr /></p>
<p>As an unrelated ... | 4 | 2009-05-11T22:50:49Z | [
"python",
"winapi",
"multiprocessing",
"python-2.6"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,573 | <p>You should import it separately. However, if you really need to forward some functionality, you can return a module from a function. Just:</p>
<pre><code>import os
def x:
return os
</code></pre>
<p>But it seems like a plugin functionality - objects + inheritance would solve that case a bit better.</p>
| 0 | 2009-05-11T23:52:56Z | [
"python"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,583 | <p>Sounds like you are wanting to use <a href="http://docs.python.org/tutorial/modules.html#packages" rel="nofollow">python packages</a>. Look into those.</p>
| 0 | 2009-05-11T23:56:47Z | [
"python"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,595 | <p>Just import the modules again.</p>
<p>Importing a module in python is a very lightweight operation. The first time you import a module, python will load the module and execute the code in it. On any subsequent imports, you will just get a reference to the already-imported module.</p>
<p>You can verify this yours... | 1 | 2009-05-12T00:01:39Z | [
"python"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,601 | <p>Yep. Once you import a module, that module becomes a property of the current module.</p>
<pre><code># a.py
class A(object):
...
# b.py
import a
class B(a.A):
...
</code></pre>
<p>In Django, for example, many of the packages simply import the contents of other modules. Classes and functions are defined in ... | 0 | 2009-05-12T00:05:07Z | [
"python"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,611 | <p>Yes you can use the imports from the other file by going a.os.</p>
<p>However, the pythonic way is to just import the exact modules you need without making a chain out of it (which can lead to circular references).</p>
<p>When you import a module, the code is compiled and inserted into a dictionary of names -> mod... | 13 | 2009-05-12T00:11:58Z | [
"python"
] |
Can a Python module use the imports from another file? | 850,566 | <p>I have something like this:</p>
<pre><code> # a.py
import os
class A:
...
# b.py
import a
class B(A):
...
</code></pre>
<p>In class B (b.py) I'd like to be able to use the modules imported in a.py (os in this case). Is it possible to achieve this behavior in Python or should I import the modules in b... | 3 | 2009-05-11T23:48:29Z | 850,680 | <p>First you can shorten it to:</p>
<pre><code>from django.utils import simplejson
from google.appengine.ext import webapp, db
from webapp import template
</code></pre>
<p>Secondly suppose you have those ^ imports in my_module.py</p>
<p>In my_module2.py you can do:</p>
<p>from my_module2.py import webapp, db, tempa... | 0 | 2009-05-12T00:45:11Z | [
"python"
] |
Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System? | 850,630 | <p>To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a... | 10 | 2009-05-12T00:18:59Z | 850,642 | <p>Number 3 should and will work. You do not need the .pyo or .py files in order to use the compiled python code.</p>
| 3 | 2009-05-12T00:25:38Z | [
"python",
"embedded"
] |
Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System? | 850,630 | <p>To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a... | 10 | 2009-05-12T00:18:59Z | 850,646 | <p><a href="http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html">http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html</a></p>
<blockquote>
<p>When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in â.pyoâ files. The optimizer currently doe... | 13 | 2009-05-12T00:27:43Z | [
"python",
"embedded"
] |
Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System? | 850,630 | <p>To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a... | 10 | 2009-05-12T00:18:59Z | 850,690 | <p>What it ultimately boils down to is that you really only need one of the three options, but your best bet is to go with .pys and either .pyos or .pycs.</p>
<p>Here's how I see each of your options:</p>
<ol>
<li>If you put the .pys in a zip file, you won't see pycs or pyos built. It should also be pointed out that... | 1 | 2009-05-12T00:50:04Z | [
"python",
"embedded"
] |
Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System? | 850,630 | <p>To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a... | 10 | 2009-05-12T00:18:59Z | 851,194 | <p>I would recommend keeping only .py files. The difference in startup time isn't that great, and having the source around is a plus, as it will run under different python versions without any issues.</p>
<p>As of python 2.6, setting sys.dont_write_bytecode to True will suppress compilation of .pyc and .pyo files alto... | 2 | 2009-05-12T04:54:30Z | [
"python",
"embedded"
] |
Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System? | 850,630 | <p>To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a... | 10 | 2009-05-12T00:18:59Z | 25,644,162 | <p>Here's how I minimize disk requirements for mainline Python 2.7 at the day job:</p>
<p>1) Remove packages from the standard library which you won't need. The following is a conservative list:</p>
<pre><code>bsddb/test ctypes/test distutils/tests email/test idlelib lib-tk
lib2to3 pydoc.py tabnanny.py test unittest
... | 1 | 2014-09-03T12:20:01Z | [
"python",
"embedded"
] |
Scope of Python Recursive Generators | 850,725 | <p>Hey all, I was working on a recursive generator to create the fixed integer partitions of a number and I was confused by a scoping issue.</p>
<p>The code is similar to this snippet.</p>
<pre><code>def testGen(a,n):
if n <= 1:
print('yield', a)
yield a
else:
for i in range(2):
... | 2 | 2009-05-12T01:09:32Z | 850,729 | <p>I would guess you are mutating the array, so when you print it has a particular value, then the next time you print it has actually updated the value, and so on. At the end, you have 5 references to the same array, so of course you have the same value 5 times.</p>
| 2 | 2009-05-12T01:13:36Z | [
"python",
"recursion",
"scope",
"generator"
] |
Scope of Python Recursive Generators | 850,725 | <p>Hey all, I was working on a recursive generator to create the fixed integer partitions of a number and I was confused by a scoping issue.</p>
<p>The code is similar to this snippet.</p>
<pre><code>def testGen(a,n):
if n <= 1:
print('yield', a)
yield a
else:
for i in range(2):
... | 2 | 2009-05-12T01:09:32Z | 850,733 | <p>The print statement displays the list at that particular point in time. Your code changes the list as you run it, so by the time you examine the list at the end, you see its value then.</p>
<p>You can observe this by stepping through:</p>
<pre><code>>>> g = testGen([1,2],4)
>>> g.next()
('yield'... | 2 | 2009-05-12T01:17:26Z | [
"python",
"recursion",
"scope",
"generator"
] |
Scope of Python Recursive Generators | 850,725 | <p>Hey all, I was working on a recursive generator to create the fixed integer partitions of a number and I was confused by a scoping issue.</p>
<p>The code is similar to this snippet.</p>
<pre><code>def testGen(a,n):
if n <= 1:
print('yield', a)
yield a
else:
for i in range(2):
... | 2 | 2009-05-12T01:09:32Z | 850,739 | <p>The print and yield statements are different because you only have one print statement while you have 2 yields. Try this:</p>
<pre><code>def testGen(a,n):
if n <= 1:
print('yield', a)
yield a
else:
for i in range(2):
a[i] += n
for j in testGen(a,n-i-1):
... | 0 | 2009-05-12T01:19:19Z | [
"python",
"recursion",
"scope",
"generator"
] |
Scope of Python Recursive Generators | 850,725 | <p>Hey all, I was working on a recursive generator to create the fixed integer partitions of a number and I was confused by a scoping issue.</p>
<p>The code is similar to this snippet.</p>
<pre><code>def testGen(a,n):
if n <= 1:
print('yield', a)
yield a
else:
for i in range(2):
... | 2 | 2009-05-12T01:09:32Z | 850,742 | <p>Lists are mutable objects, if you pass in a list, and the generator performs in-place operations on that list, then finally all references to the list will point to the same list.</p>
| 2 | 2009-05-12T01:20:26Z | [
"python",
"recursion",
"scope",
"generator"
] |
Check whether a path exists on a remote host using paramiko | 850,749 | <p>Paramiko's <a href="http://www.metasnark.com/paramiko/docs/paramiko.SFTP-class.html">SFTPClient</a> apparently does not have an <code>exists</code> method. This is my current implementation:</p>
<pre><code>def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(pa... | 9 | 2009-05-12T01:24:59Z | 850,865 | <p>There is no "exists" method defined for SFTP (not just paramiko), so your method is fine.</p>
<p>I think checking the errno is a little cleaner:</p>
<pre><code>def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(path)
except IOError, e:
if e[0] ==... | 5 | 2009-05-12T02:16:12Z | [
"python",
"ssh",
"scp",
"paramiko"
] |
Check whether a path exists on a remote host using paramiko | 850,749 | <p>Paramiko's <a href="http://www.metasnark.com/paramiko/docs/paramiko.SFTP-class.html">SFTPClient</a> apparently does not have an <code>exists</code> method. This is my current implementation:</p>
<pre><code>def rexists(sftp, path):
"""os.path.exists for paramiko's SCP object
"""
try:
sftp.stat(pa... | 9 | 2009-05-12T01:24:59Z | 1,391,409 | <p>See the <a href="http://docs.python.org/library/errno.html#errno.ENOENT"><code>errno</code> module</a> for constants defining all those error codes. Also, it's a bit clearer to use the <code>errno</code> attribute of the exception than the expansion of the <code>__init__</code> args, so I'd do this:</p>
<pre><code... | 15 | 2009-09-08T00:29:06Z | [
"python",
"ssh",
"scp",
"paramiko"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 850,831 | <p>Clearing a list in place will affect all other references of the same list.</p>
<p>For example, this method doesn't affect other references:</p>
<pre><code>>>> a = [1, 2, 3]
>>> b = a
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3]
</code></pre>
<p>But this one does:... | 229 | 2009-05-12T02:03:16Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 850,832 | <p><del>It appears to me that del will give you the memory back, while assigning a new list will make the old one be deleted only when the gc runs.matter.</p>
<p>This may be useful for large lists, but for small list it should be negligible.</del></p>
<p><strong>Edit</strong>: As Algorias, it doesn't matter.</p>
<... | 10 | 2009-05-12T02:03:32Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 851,533 | <p>There are two cases in which you might want to clear a list:</p>
<ol>
<li>You want to use the name <code>old_list</code> further in your code;</li>
<li>You want the old list to be garbage collected as soon as possible to free some memory;</li>
</ol>
<p>In case 1 you just go on with the assigment:</p>
<pre><code> ... | 4 | 2009-05-12T07:13:48Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 12,970,184 | <p>There is a very simple way to delete a python list. Use <strong>del list_name[:]</strong>.</p>
<p>For example:
<pre><code><code>a = [1, 2, 3]</code>
b = a
del a[:]
print b
</pre></code></p>
| 22 | 2012-10-19T08:23:54Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 30,431,696 | <pre><code>del list[:]
</code></pre>
<p>Will delete the values of that list variable </p>
<pre><code>del list
</code></pre>
<p>Will delete the variable itself from memory </p>
| 2 | 2015-05-25T05:22:54Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 31,270,256 | <p>If you're clearing the list, you, obviously, don't need the list anymore. If so, you can just delete the entire list by simple del method.</p>
<pre><code>a = [1, 3, 5, 6]
del a # This will entirely delete a(the list).
</code></pre>
<p>But in case, you need it again, you can reinitialize it. Or just simply clear it... | 1 | 2015-07-07T13:41:34Z | [
"python"
] |
Clearing Python lists | 850,795 | <p>Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python:</p>
<pre><code>old_list = []
old_list = list()
</code></pre>
<p>The reason I ask is that I just saw this in some running code:</p>
<pre><code>del old_list[ 0:len(old_list) ]
</code></pre>
| 119 | 2009-05-12T01:46:55Z | 35,139,695 | <p>another solution that works fine is to create empty list as a reference empty list.</p>
<pre><code>empt_list = []
</code></pre>
<p>for example you have a list as <code>a_list = [1,2,3]</code>. To clear it just make the following:</p>
<pre><code>a_list = list(empt_list)
</code></pre>
<p>this will make <code>a_l... | -1 | 2016-02-01T20:28:09Z | [
"python"
] |
How to filter a file using pattern to another file using Python? | 850,799 | <p>I have a dictionary. I want to take only the words containing a simple word pattern (i.e. "cow") and write them to another file. Each line starts with a word and then the definition. I'm still extremely new at python so I don't have a good grasp of the syntax, but the pseudocode in my head looks something like:</p>
... | 0 | 2009-05-12T01:49:08Z | 850,829 | <pre><code>import re
infile = open('C:/infile.txt')
outfile = open('C:/outfile.txt', 'w')
pattern = re.compile('^(cow\w*)')
for line in infile:
found = pattern.match(line)
if found:
text = "%s\n" % (found.group(0))
outfile.write(text)
outfile.close()
infile.close()
</code></pre>
| 2 | 2009-05-12T02:03:02Z | [
"python",
"file-io"
] |
Faster way to sum a list of numbers than with a for-loop? | 850,877 | <p>Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?</p>
<p>Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.</p>
| 8 | 2009-05-12T02:20:32Z | 850,882 | <p>You can use sum() to sum the values of an array.</p>
<pre><code>a = [1,9,12]
print sum(a)
</code></pre>
| 32 | 2009-05-12T02:22:50Z | [
"python",
"algorithm",
"for-loop"
] |
Faster way to sum a list of numbers than with a for-loop? | 850,877 | <p>Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?</p>
<p>Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.</p>
| 8 | 2009-05-12T02:20:32Z | 850,890 | <p>If each term in the list simply increments by 1, or if you can find a pattern in the series, you could find a formula for summing n terms. For example, the sum of the series {1,2,3,...,n} = n(n+1)/2</p>
<p>Read more <a href="http://en.wikipedia.org/wiki/Summation" rel="nofollow">here</a></p>
| 0 | 2009-05-12T02:24:49Z | [
"python",
"algorithm",
"for-loop"
] |
Faster way to sum a list of numbers than with a for-loop? | 850,877 | <p>Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?</p>
<p>Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.</p>
| 8 | 2009-05-12T02:20:32Z | 850,898 | <p>Well, I don't know if it is faster but you could try a little calculus to make it one operation. (N*(N+1))/2 gives you the sum of every number from 1 to N, and there are other formulas for solving more complex sums.</p>
| 0 | 2009-05-12T02:26:32Z | [
"python",
"algorithm",
"for-loop"
] |
Faster way to sum a list of numbers than with a for-loop? | 850,877 | <p>Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?</p>
<p>Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.</p>
| 8 | 2009-05-12T02:20:32Z | 850,910 | <p>For a general list, you have to at least go over every member at least once to get the sum, which is exactly what a for loop does. Using library APIs (like sum) is more convenient, but I doubt it would actually be faster.</p>
| 0 | 2009-05-12T02:31:29Z | [
"python",
"algorithm",
"for-loop"
] |
Faster way to sum a list of numbers than with a for-loop? | 850,877 | <p>Is there a way to sum up a list of numbers faster than with a for-loop, perhaps in the Python library? Or is that something really only multi-threading / vector processing can do efficiently?</p>
<p>Edit: Just to clarify, it could be a list of any numbers, unsorted, just input from the user.</p>
| 8 | 2009-05-12T02:20:32Z | 851,489 | <p>Yet another way to sum up a list with the loop time:</p>
<pre><code> s = reduce(lambda x, y: x + y, l)
</code></pre>
| 4 | 2009-05-12T06:56:34Z | [
"python",
"algorithm",
"for-loop"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 850,987 | <p>If you're going to automate Photoshop, you pretty much have to use Photoshop's own scripting systems. I don't think there's a way around that.</p>
<p>Looking at the problem a different way, can you export from Photoshop to some other format which supports layers, like PNG, which is editable by ImageMagick?</p>
| 1 | 2009-05-12T03:01:50Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 851,244 | <p>The only way I can think of to automate the changing of text inside of a PSD would be to use a regex based substitution. </p>
<ol>
<li>Create a very simple picture in Photoshop, perhaps a white background and a text layer, with the text being a known length.</li>
<li>Search the file for your text, and with a hex ed... | 3 | 2009-05-12T05:18:51Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 851,513 | <p>If you don't like to use the officially supported AppleScript, JavaScript, or VBScript, then there is also the possibility to do it in Python. This is explained in the article <a href="http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html">Photoshop scripting with Python</a>, which relies on P... | 5 | 2009-05-12T07:07:47Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 851,685 | <p>Have you considered opening and editing the image in The GIMP? It has very good PSD support, and can be scripted in several languages.</p>
<p>Which one you use depends in part on your platform, the Perl interface didn't work on Windows the last I knew. I believe Scheme is supported in all ports.</p>
| 3 | 2009-05-12T08:12:17Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 852,143 | <p>You can use Photoshop itself to do this with OLE. You will need to install Photoshop, of course. Win32::OLE in Perl or similar module in Python. See <a href="http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf" rel="nofollow">http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf</... | 2 | 2009-05-12T10:35:01Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Editing Photoshop PSD text layers programmatically | 850,899 | <p>I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:</p>
<ol>
<li>load the PSD</li>
<li>edit the text in said layer</li>
<li>flatten all layers in the image</li>
<li>save as a web-friendly format like PNG... | 7 | 2009-05-12T02:27:02Z | 35,058,547 | <p>You can also try this using Node.js. I made a <a href="https://www.npmjs.com/package/psd-cli" rel="nofollow">PSD command-line tool</a></p>
<p>One-line command install (needs NodeJS/NPM installed)</p>
<p><code>npm install -g psd-cli</code></p>
<p>You can then use it by typing in your terminal</p>
<p><code>psd myf... | 1 | 2016-01-28T10:14:20Z | [
"python",
"perl",
"image",
"photoshop",
"psd"
] |
Can you pass a class (not an object) as a parameter to a method in python? | 850,921 | <p>I want to do something like the following</p>
<pre><code>class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
</code></pre>
<p>I want this to be equivalent to <code>A.static_method()</code>. Is this possible?</p>
| 5 | 2009-05-12T02:35:23Z | 850,929 | <p>Sure. Classes are first-class objects in Python.</p>
<p>Although, in your example, you should use the <code>@classmethod</code> (class object as initial argument) or <code>@staticmethod</code> (no initial argument) decorator for your method.</p>
| 7 | 2009-05-12T02:37:44Z | [
"python",
"static",
"parameters"
] |
Can you pass a class (not an object) as a parameter to a method in python? | 850,921 | <p>I want to do something like the following</p>
<pre><code>class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
</code></pre>
<p>I want this to be equivalent to <code>A.static_method()</code>. Is this possible?</p>
| 5 | 2009-05-12T02:35:23Z | 850,932 | <p>Sure why not? Don't forget to add @staticmethod to static methods.</p>
<pre><code>class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
</code></pre>
| 0 | 2009-05-12T02:38:29Z | [
"python",
"static",
"parameters"
] |
Can you pass a class (not an object) as a parameter to a method in python? | 850,921 | <p>I want to do something like the following</p>
<pre><code>class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
</code></pre>
<p>I want this to be equivalent to <code>A.static_method()</code>. Is this possible?</p>
| 5 | 2009-05-12T02:35:23Z | 850,937 | <p>You should be able to do the following (note the <code>@staticmethod</code> decorator):</p>
<pre><code>class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
</code></pre>
| 5 | 2009-05-12T02:39:51Z | [
"python",
"static",
"parameters"
] |
How do I check if a disk is in a drive using python? | 851,010 | <p>Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)</p>
<p>The drive letter <em>exists</em>, so os.exists() will always return True in this case. Also, at this point in the process I don't y... | 4 | 2009-05-12T03:20:00Z | 851,038 | <p>Not sure about your platform, but <a href="http://pysnmp.sourceforge.net/" rel="nofollow">SNMP</a> might be the answer for you.</p>
| 0 | 2009-05-12T03:36:26Z | [
"python",
"windows"
] |
How do I check if a disk is in a drive using python? | 851,010 | <p>Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)</p>
<p>The drive letter <em>exists</em>, so os.exists() will always return True in this case. Also, at this point in the process I don't y... | 4 | 2009-05-12T03:20:00Z | 851,039 | <p>You can compare <code>len(os.listdir("path"))</code> to zero to see if there are any files in the directory.</p>
| 4 | 2009-05-12T03:37:44Z | [
"python",
"windows"
] |
How do I check if a disk is in a drive using python? | 851,010 | <p>Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)</p>
<p>The drive letter <em>exists</em>, so os.exists() will always return True in this case. Also, at this point in the process I don't y... | 4 | 2009-05-12T03:20:00Z | 851,043 | <p>If you have pythonwin, does any of the information in <a href="http://www.microsoft.com/technet/scriptcenter/scripts/python/storage/disks/drives/stdvpy05.mspx?mfr=true" rel="nofollow">this recipe</a> help?</p>
<p>At a guess, "Availability" and "Status" may be worth looking at. Or you could test volume name, which ... | 1 | 2009-05-12T03:40:20Z | [
"python",
"windows"
] |
How do I check if a disk is in a drive using python? | 851,010 | <p>Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)</p>
<p>The drive letter <em>exists</em>, so os.exists() will always return True in this case. Also, at this point in the process I don't y... | 4 | 2009-05-12T03:20:00Z | 852,348 | <p>You can use win32 functions via the excellent pywin32 (<a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">http://sourceforge.net/projects/pywin32/</a>) for this purpose. </p>
<p>I suggest looking at the <code>GetDiskFreeSpace</code> function. You can check the free space on the target drive and conti... | 1 | 2009-05-12T11:51:42Z | [
"python",
"windows"
] |
How do I check if a disk is in a drive using python? | 851,010 | <p>Say I want to manipulate some files on a floppy drive or a USB card reader. How do I check to see if the drive in question is ready? (That is, has a disk physically inserted.)</p>
<p>The drive letter <em>exists</em>, so os.exists() will always return True in this case. Also, at this point in the process I don't y... | 4 | 2009-05-12T03:20:00Z | 1,020,363 | <p>And the answer, as with so many things, turns out to be in <a href="http://bcbjournal.org/articles/vol2/9806/Detecting%5Fdisk%5Ferrors.htm">an article about C++/Win32 programming from a decade ago</a>.</p>
<p>The issue, in a nutshell, is that Windows handles floppy disk errors slightly differently than other kinds ... | 6 | 2009-06-19T22:47:36Z | [
"python",
"windows"
] |
how are exceptions compared in an except clause | 851,012 | <p>In the following code segment:</p>
<pre><code>try:
raise Bob()
except Fred:
print "blah"
</code></pre>
<p>How is the comparison of Bob and Fred implemented?</p>
<p>From playing around it seems to be calling isinstance underneath, is this correct?</p>
<p>I'm asking because I am attempting to subvert the p... | -2 | 2009-05-12T03:20:08Z | 851,027 | <blockquote>
<p>I want to be able to construct a Bob
such that it gets caught by execpt
Fred even though it isn't actually an
instance of Fred or any of its
subclasses.</p>
</blockquote>
<p>Well, you can just catch 'Exception' - but this is not very pythonic. You should attempt to catch the correct exception... | 1 | 2009-05-12T03:28:51Z | [
"python",
"exception"
] |
how are exceptions compared in an except clause | 851,012 | <p>In the following code segment:</p>
<pre><code>try:
raise Bob()
except Fred:
print "blah"
</code></pre>
<p>How is the comparison of Bob and Fred implemented?</p>
<p>From playing around it seems to be calling isinstance underneath, is this correct?</p>
<p>I'm asking because I am attempting to subvert the p... | -2 | 2009-05-12T03:20:08Z | 851,037 | <p>I believe that your guess is correct in how the comparison works, and the only way to intercept that is to add Fred as a base class to Bob. For example:</p>
<pre><code># Assume both Bob and Fred are derived from Exception
>>> class Bob(Bob, Fred):
... pass
...
>>> try:
... raise Bob()
...... | 6 | 2009-05-12T03:35:50Z | [
"python",
"exception"
] |
how are exceptions compared in an except clause | 851,012 | <p>In the following code segment:</p>
<pre><code>try:
raise Bob()
except Fred:
print "blah"
</code></pre>
<p>How is the comparison of Bob and Fred implemented?</p>
<p>From playing around it seems to be calling isinstance underneath, is this correct?</p>
<p>I'm asking because I am attempting to subvert the p... | -2 | 2009-05-12T03:20:08Z | 851,482 | <pre><code>>>> class Fred(Exception):
pass
>>> class Bob(Fred):
pass
>>> issubclass(Bob, Fred)
True
>>> issubclass(Fred, Bob)
False
>>> try:
raise Bob()
except Fred:
print("blah")
blah
</code></pre>
<p>So basically the exception is caught because, Bob is s... | 1 | 2009-05-12T06:53:52Z | [
"python",
"exception"
] |
how are exceptions compared in an except clause | 851,012 | <p>In the following code segment:</p>
<pre><code>try:
raise Bob()
except Fred:
print "blah"
</code></pre>
<p>How is the comparison of Bob and Fred implemented?</p>
<p>From playing around it seems to be calling isinstance underneath, is this correct?</p>
<p>I'm asking because I am attempting to subvert the p... | -2 | 2009-05-12T03:20:08Z | 855,053 | <p>In CPython at least, it looks like there's a COMPARE_OP operation with type 10 (exception match). There's unlikely anything you can do to hack around that calculation.</p>
<pre>
>>> import dis
>>> def foo():
... try:
... raise OSError()
... except Exception, e:
... pass
...
>>> dis... | 0 | 2009-05-12T21:50:50Z | [
"python",
"exception"
] |
how are exceptions compared in an except clause | 851,012 | <p>In the following code segment:</p>
<pre><code>try:
raise Bob()
except Fred:
print "blah"
</code></pre>
<p>How is the comparison of Bob and Fred implemented?</p>
<p>From playing around it seems to be calling isinstance underneath, is this correct?</p>
<p>I'm asking because I am attempting to subvert the p... | -2 | 2009-05-12T03:20:08Z | 856,409 | <p>I'm not clear how hiding your exception in socket.timeout adhears to the "seamless" philosophy? What's wrong with catching the expected exception as it's defined?</p>
<pre><code>try:
print "received:", s.recv(2048)
except socket.timeout:
print "timeout"
except our_proxy_helper_class:
print 'crap!'
</co... | 1 | 2009-05-13T06:37:42Z | [
"python",
"exception"
] |
How does Python import modules from .egg files? | 851,420 | <p>How can I open <code>__init__.pyc</code> here?</p>
<pre><code> >>> import stompservice
<module 'stompservice' from 'C:\Python25\lib\site-packages\stompservice-0.1.0-py2.5.egg\stompservice\__init__.pyc'>
</code></pre>
<p>All I see in <code>C:\Python25\lib\site-packages\</code> is the .egg file... | 12 | 2009-05-12T06:31:41Z | 851,430 | <p><a href="http://peak.telecommunity.com/DevCenter/PythonEggs">http://peak.telecommunity.com/DevCenter/PythonEggs</a></p>
<p>.egg files are simply renamed zip files.</p>
<p>Open the egg with your zip program, or just rename the extension to .zip, and extract.</p>
| 18 | 2009-05-12T06:35:16Z | [
"python",
"module",
"packages"
] |
How does Python import modules from .egg files? | 851,420 | <p>How can I open <code>__init__.pyc</code> here?</p>
<pre><code> >>> import stompservice
<module 'stompservice' from 'C:\Python25\lib\site-packages\stompservice-0.1.0-py2.5.egg\stompservice\__init__.pyc'>
</code></pre>
<p>All I see in <code>C:\Python25\lib\site-packages\</code> is the .egg file... | 12 | 2009-05-12T06:31:41Z | 15,497,487 | <p>For example, if you want to import the suds module which is available as .egg file:</p>
<p>In your python script:</p>
<pre><code>egg_path='/home/shahid/suds_2.4.egg'
sys.path.append(egg_path)
import suds
#... rest of code
</code></pre>
| 10 | 2013-03-19T10:54:35Z | [
"python",
"module",
"packages"
] |
Using ctypes.c_void_p as an input to glTexImage2D? | 851,587 | <p>I'm using a 3rd party DLL to load in some raw image data, and I want to use this raw image data as a texture in openGL. However, the c function returns a void*, and I need to somehow convert this so it will work as the "pixels" parameter to glTexImage2D. Right now my code looks like something this:</p>
<pre><code... | 1 | 2009-05-12T07:35:45Z | 851,659 | <p>An <a href="http://stackoverflow.com/questions/318067/python-converting-strings-for-use-with-ctypes-cvoidp/318140#318140">answer</a> to a <a href="http://stackoverflow.com/questions/318067/python-converting-strings-for-use-with-ctypes-cvoidp">similar question</a> suggested to use <code>ctypes.cast()</code>.</p>
| 1 | 2009-05-12T08:00:35Z | [
"python",
"opengl",
"ctypes",
"pyopengl"
] |
Using ctypes.c_void_p as an input to glTexImage2D? | 851,587 | <p>I'm using a 3rd party DLL to load in some raw image data, and I want to use this raw image data as a texture in openGL. However, the c function returns a void*, and I need to somehow convert this so it will work as the "pixels" parameter to glTexImage2D. Right now my code looks like something this:</p>
<pre><code... | 1 | 2009-05-12T07:35:45Z | 13,619,176 | <p>This may or may not help. There is a similar issue on use of c_void_p as a return type. I instead have to return c_longlong AND do some hokey tricks:</p>
<p>if I am returning a pointer c_types obejct, I have to cast its reference to a POINTER to c_longlong to get the integer value to return. (NOTE that CFUNCTYPE... | 0 | 2012-11-29T05:04:26Z | [
"python",
"opengl",
"ctypes",
"pyopengl"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 851,863 | <p>Can Eclipse resolve attributes created runtime via <code>__metaclass__</code>es?</p>
<p>Notice that you never define a <code>DoesNotExist</code> on any of your models and it is not defined on <code>django.db.models.base.Model</code> either.</p>
| 2 | 2009-05-12T09:03:12Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 851,886 | <blockquote>
<p>Eclipse complains that User.DoesNotExist is undefined.</p>
</blockquote>
<p>What do you mean by that? Do you have python error and stack trace? This code have to work (as in <a href="http://docs.djangoproject.com/en/1.0/topics/auth/#writing-an-authentication-backend" rel="nofollow">documentation</a>)... | 0 | 2009-05-12T09:10:16Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 851,888 | <p>The problem is really with PyDev, not your code. What you have done is absolutely correct, but IDEs will always have difficulty resolving attributes in a dynamic language like Python. In the case of the DoesNotExist exception, it is added via a <code>__metaclass__</code> rather than through normal object inheritance... | 16 | 2009-05-12T09:11:26Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 851,937 | <p>Pydev has a workaround for such cases (when the members are defined at runtime).
Just add #@UndefinedVariable at the end of the string which cause the warning (or ctrl+1 on keyboard when the cursor is at "DoesNotExist"), and it won't complain.</p>
| 7 | 2009-05-12T09:29:50Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 1,391,255 | <p>I just discovered Pydev actually has a nice workaround for this.</p>
<p>Go to <strong>Window</strong> > <strong>Preferences</strong>, then <strong>Pydev</strong> > <strong>Editor</strong> > <strong>Code Analysis</strong>.</p>
<p>Click the <strong>Undefined</strong> tab and add "DoesNotExist" to the text box titled... | 19 | 2009-09-07T23:00:34Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 2,591,650 | <p>You can also solve it in a different way: just go to the User class, and add @DynamicAttrs in the docstring.<br>
This will tell PyDev that attributes of the class are added dynamically, and will make it not complain any longer for "issues" like DoesNotExist.</p>
| 1 | 2010-04-07T10:34:25Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
Django -- User.DoesNotExist does not exist? | 851,628 | <p>I'm trying to get hold of Django. I use Pydev on Eclipse. I have written a simple signup page that I can't get to work. Eclipse complains that User.DoesNotExist is undefined. Most likely, I am missing something trivial. Here's the relevant portion of the code:</p>
<pre><code>from django.contrib.auth.models import U... | 26 | 2009-05-12T07:49:16Z | 3,069,447 | <p>I have same problem on Ubuntu in a VirtualEnv to solve problem I have used this snippets.</p>
<blockquote>
<p><a href="http://djangosnippets.org/snippets/191/#c3091" rel="nofollow">http://djangosnippets.org/snippets/191/#c3091</a></p>
</blockquote>
<p>In parituclar he make custom User Fields with code: </p>
<pr... | 1 | 2010-06-18T12:13:58Z | [
"python",
"django",
"authentication",
"django-models",
"pydev"
] |
How to read path from a txt file and copy those file to a new directory? | 851,689 | <blockquote>
<pre><code>from shutil import copy
f = open(r'C:\temp.txt', 'r')
for i in f.readlines():
print i
copy(i,r"C:\opencascade")
f.close()
</code></pre>
</blockquote>
<p>I am reading path from temp.txt file which has 500 lines each line is a path for specific file to be copied to location "C:\open... | 2 | 2009-05-12T08:12:54Z | 851,799 | <p>You have a <code>\n</code> at the and of the filename. </p>
<p>Try:</p>
<pre><code>copy(i.strip(), r"C:\opencascade")
</code></pre>
| 8 | 2009-05-12T08:46:03Z | [
"python",
"file-io",
"copy"
] |
How to read path from a txt file and copy those file to a new directory? | 851,689 | <blockquote>
<pre><code>from shutil import copy
f = open(r'C:\temp.txt', 'r')
for i in f.readlines():
print i
copy(i,r"C:\opencascade")
f.close()
</code></pre>
</blockquote>
<p>I am reading path from temp.txt file which has 500 lines each line is a path for specific file to be copied to location "C:\open... | 2 | 2009-05-12T08:12:54Z | 11,491,159 | <p>Error is single "\" rather than double "\" in the path.</p>
| 0 | 2012-07-15T10:28:07Z | [
"python",
"file-io",
"copy"
] |
Django ImageField issue | 851,830 | <p>I have a similar model</p>
<pre><code>Class Student(models.Model):
"""A simple class which holds the basic info
of a student."""
name = models.CharField(max_length=50)
age = models.PositiveIntegerField()
photo = models.ImageField(upload_to='foobar', blank=True, null=True)
</code></pre>
<p>As we can see photo fiel... | 10 | 2009-05-12T08:55:06Z | 852,022 | <p>It doesn't work because <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#field-lookups">field lookups</a> only work on other models. Here, <code>name</code> is an attribute on the return value of your <code>photo</code> field.</p>
<p>Try this instead:</p>
<pre><code>Student.objects.exclude(photo__i... | 12 | 2009-05-12T09:57:01Z | [
"python",
"django",
"django-models"
] |
Build a GQL query (for Google App Engine) that has a condition on ReferenceProperty | 852,055 | <p>Say I have the following model:</p>
<pre><code>class Schedule(db.Model):
tripCode = db.StringProperty(required=True)
station = db.ReferenceProperty(Station, required=True)
arrivalTime = db.TimeProperty(required=True)
departureTime = db.TimeProperty(required=True)
</code></pre>
<p>And let's say ... | 5 | 2009-05-12T10:06:18Z | 854,910 | <p>You shouldn't be inserting user data into a GQL string using string substitution. GQL supports parameter substitution, so you can do this:</p>
<pre><code>db.GqlQuery("SELECT * FROM Schedule WHERE station = $1", foo.key())
</code></pre>
<p>or, using the Query interface:</p>
<pre><code>Schedule.all().filter("statio... | 10 | 2009-05-12T21:17:11Z | [
"python",
"google-app-engine",
"gql"
] |
Build a GQL query (for Google App Engine) that has a condition on ReferenceProperty | 852,055 | <p>Say I have the following model:</p>
<pre><code>class Schedule(db.Model):
tripCode = db.StringProperty(required=True)
station = db.ReferenceProperty(Station, required=True)
arrivalTime = db.TimeProperty(required=True)
departureTime = db.TimeProperty(required=True)
</code></pre>
<p>And let's say ... | 5 | 2009-05-12T10:06:18Z | 1,788,018 | <p>An even easier thing to do is to change the model definition by adding the 'collection_name' field to the ReferenceProperty: </p>
<blockquote>
<p>station = db.ReferenceProperty(Station, required=True, collection_name="schedules")</p>
</blockquote>
<p>Then you can just do: </p>
<blockquote>
<p>foo.schedules </... | 7 | 2009-11-24T05:35:40Z | [
"python",
"google-app-engine",
"gql"
] |
Anyone successfully adopted JaikuEngine? | 852,268 | <p>Are there real world adaptations of JaikuEngine on Google App Engine?
(Question from my boss which wants to use it instead writing our own system)</p>
| 6 | 2009-05-12T11:26:33Z | 1,037,339 | <p>Whilst I love the App-Engine. Does your solution need to be hosted on the AppEngine? If not I would check out <a href="http://identi.ca" rel="nofollow">identi.ca</a> which is based on the Open Source <a href="http://laconi.ca/trac/" rel="nofollow">Laconi.ca</a></p>
| 1 | 2009-06-24T09:32:47Z | [
"python",
"google-app-engine"
] |
Anyone successfully adopted JaikuEngine? | 852,268 | <p>Are there real world adaptations of JaikuEngine on Google App Engine?
(Question from my boss which wants to use it instead writing our own system)</p>
| 6 | 2009-05-12T11:26:33Z | 1,044,077 | <p>Does <a href="http://www.jaiku.com/" rel="nofollow">http://www.jaiku.com/</a> count?</p>
| 0 | 2009-06-25T13:54:31Z | [
"python",
"google-app-engine"
] |
Anyone successfully adopted JaikuEngine? | 852,268 | <p>Are there real world adaptations of JaikuEngine on Google App Engine?
(Question from my boss which wants to use it instead writing our own system)</p>
| 6 | 2009-05-12T11:26:33Z | 1,052,183 | <p>This is jaikuengine running on AppEngine - https://jaiku.appspot.com/
If you wish to have your own version of jaiku, its pretty straightforward. Check this out- <a href="http://code.google.com/p/jaikuengine/" rel="nofollow">http://code.google.com/p/jaikuengine/</a></p>
| 2 | 2009-06-27T05:42:16Z | [
"python",
"google-app-engine"
] |
How the method resolution and invocation works internally in Python? | 852,308 | <p>How the methods invocation works in Python?
I mean, how the python virtual machine interpret it.</p>
<p>It's true that the python method resolution could be slower in Python that in Java.
What is late binding?</p>
<p>What are the differences on the reflection mechanism in these two languages?
Where to find good re... | 5 | 2009-05-12T11:39:13Z | 852,335 | <p>Names (methods, functions, variables) are all resolved by looking at the namespace. Namespaces are implemented in CPython as <code>dict</code>s (hash maps).</p>
<p>When a name is not found in the instance namespace (<code>dict</code>), python goes for the class, and then for the base classes, following the method r... | 4 | 2009-05-12T11:48:17Z | [
"java",
"python"
] |
How the method resolution and invocation works internally in Python? | 852,308 | <p>How the methods invocation works in Python?
I mean, how the python virtual machine interpret it.</p>
<p>It's true that the python method resolution could be slower in Python that in Java.
What is late binding?</p>
<p>What are the differences on the reflection mechanism in these two languages?
Where to find good re... | 5 | 2009-05-12T11:39:13Z | 852,344 | <blockquote>
<p>It's true that the python method
resolution could be slower in Python
that in Java. What is late binding?</p>
</blockquote>
<p>Late binding describes a strategy of how an interpreter or compiler of a particular language decides how to map an identifier to a piece of code. For example, consider wr... | 1 | 2009-05-12T11:50:34Z | [
"java",
"python"
] |
How the method resolution and invocation works internally in Python? | 852,308 | <p>How the methods invocation works in Python?
I mean, how the python virtual machine interpret it.</p>
<p>It's true that the python method resolution could be slower in Python that in Java.
What is late binding?</p>
<p>What are the differences on the reflection mechanism in these two languages?
Where to find good re... | 5 | 2009-05-12T11:39:13Z | 870,650 | <p>Method invocation in Python consists of two distinct separable steps. First an attribute lookup is done, then the result of that lookup is invoked. This means that the following two snippets have the same semantics:</p>
<pre><code>foo.bar()
method = foo.bar
method()
</code></pre>
<p>Attribute lookup in Python is... | 7 | 2009-05-15T20:10:24Z | [
"java",
"python"
] |
M2Crypto Encrypt/Decrypt using AES256 | 852,332 | <p>Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python</p>
| 6 | 2009-05-12T11:47:20Z | 853,525 | <p>When it comes to security nothing beats reading the documentation.</p>
<p><a href="http://chandlerproject.org/bin/view/Projects/MeTooCrypto" rel="nofollow">http://chandlerproject.org/bin/view/Projects/MeTooCrypto</a></p>
<p>Even if I took the time to understand and make the perfect code for you to copy and paste, ... | -1 | 2009-05-12T15:56:19Z | [
"python",
"aes",
"m2crypto"
] |
M2Crypto Encrypt/Decrypt using AES256 | 852,332 | <p>Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python</p>
| 6 | 2009-05-12T11:47:20Z | 854,417 | <p>M2Crypto's documentation is terrible. Sometimes the OpenSSL documentation (m2crypto wraps OpenSSL) can help. Your best bet is to look at the M2Crypto unit tests -- <a href="http://svn.osafoundation.org/m2crypto/trunk/tests/test_evp.py">http://svn.osafoundation.org/m2crypto/trunk/tests/test_evp.py</a> -- look for the... | 12 | 2009-05-12T19:24:30Z | [
"python",
"aes",
"m2crypto"
] |
M2Crypto Encrypt/Decrypt using AES256 | 852,332 | <p>Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python</p>
| 6 | 2009-05-12T11:47:20Z | 855,045 | <p>Take a look at <a href="http://pypi.python.org/pypi/m2secret/" rel="nofollow">m2secret</a>:</p>
<blockquote>
<p>Small utility and module for
encrypting and decrypting data using
symmetric-key algorithms. By default
uses 256-bit AES (Rijndael) using CBC,
but some options are configurable.
PBKDF2 algorith... | 2 | 2009-05-12T21:48:36Z | [
"python",
"aes",
"m2crypto"
] |
M2Crypto Encrypt/Decrypt using AES256 | 852,332 | <p>Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python</p>
| 6 | 2009-05-12T11:47:20Z | 19,002,390 | <pre><code>def encrypt_file(key, in_filename, out_filename,iv):
cipher=M2Crypto.EVP.Cipher('aes_256_cfb',key,iv, op=1)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(b)
while True:
buf = infile.read(1024)
if not... | 1 | 2013-09-25T10:26:01Z | [
"python",
"aes",
"m2crypto"
] |
M2Crypto Encrypt/Decrypt using AES256 | 852,332 | <p>Can someone provide me code to encrypt / decrypt using m2crypto aes256 CBC using Python</p>
| 6 | 2009-05-12T11:47:20Z | 37,945,148 | <p>I use following wrapper around M2Crypto (borrowed from <a href="https://cryptography.io/en/latest/fernet/" rel="nofollow">cryptography.io</a>):</p>
<pre><code>import os
import base64
import M2Crypto
class SymmetricEncryption(object):
@staticmethod
def generate_key():
return base64.b64encode(os.ur... | 1 | 2016-06-21T12:58:51Z | [
"python",
"aes",
"m2crypto"
] |
Python: getting a reference to a function from inside itself | 852,401 | <p>If I define a function:</p>
<pre><code>def f(x):
return x+3
</code></pre>
<p>I can later store objects as attributes of the function, like so:</p>
<pre><code>f.thing="hello!"
</code></pre>
<p>I would like to do this from inside the code of the function itself. Problem is, how do I get a reference to the func... | 18 | 2009-05-12T12:06:30Z | 852,455 | <p>The same way, just use its name.</p>
<pre><code>>>> def g(x):
... g.r = 4
...
>>> g
<function g at 0x0100AD68>
>>> g(3)
>>> g.r
4
</code></pre>
| 19 | 2009-05-12T12:16:43Z | [
"python",
"function",
"self-reference"
] |
Python: getting a reference to a function from inside itself | 852,401 | <p>If I define a function:</p>
<pre><code>def f(x):
return x+3
</code></pre>
<p>I can later store objects as attributes of the function, like so:</p>
<pre><code>f.thing="hello!"
</code></pre>
<p>I would like to do this from inside the code of the function itself. Problem is, how do I get a reference to the func... | 18 | 2009-05-12T12:06:30Z | 852,456 | <p>If you are trying to do memoization, you can use a dictionary as a default parameter:</p>
<pre><code>def f(x, memo={}):
if x not in memo:
memo[x] = x + 3
return memo[x]
</code></pre>
| 3 | 2009-05-12T12:17:10Z | [
"python",
"function",
"self-reference"
] |
Python: getting a reference to a function from inside itself | 852,401 | <p>If I define a function:</p>
<pre><code>def f(x):
return x+3
</code></pre>
<p>I can later store objects as attributes of the function, like so:</p>
<pre><code>f.thing="hello!"
</code></pre>
<p>I would like to do this from inside the code of the function itself. Problem is, how do I get a reference to the func... | 18 | 2009-05-12T12:06:30Z | 852,971 | <p>Or use a closure:</p>
<pre><code>def gen_f():
memo = dict()
def f(x):
try:
return memo[x]
except KeyError:
memo[x] = x + 3
return f
f = gen_f()
f(123)
</code></pre>
<p>Somewhat nicer IMHO</p>
| 2 | 2009-05-12T14:07:34Z | [
"python",
"function",
"self-reference"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 852,423 | <p>See the <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#in-bulk-id-list">docs</a>:</p>
<pre><code>>>> Blog.objects.in_bulk([1])
{1: <Blog: Beatles Blog>}
>>> Blog.objects.in_bulk([1, 2])
{1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>}
>>> Blog.obje... | 7 | 2009-05-12T12:11:18Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 852,427 | <p>You can use the |= operator to programmatically update a query using Q objects.</p>
| 4 | 2009-05-12T12:12:30Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 852,428 | <p>Maybe it's better to use sql IN statement.</p>
<pre><code>Article.objects.filter(id__in=[1, 2, 3])
</code></pre>
<p>See <a href="http://docs.djangoproject.com/en/1.0/ref/models/querysets/#in">queryset api reference</a>.</p>
<p>If you really need to make queries with dynamic logic, you can do something like this (... | 15 | 2009-05-12T12:12:40Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 852,481 | <p>You could chain your queries as follows:</p>
<pre><code>values = [1,2,3]
# Turn list of values into list of Q objects
queries = [Q(pk=value) for value in values]
# Take one Q object from the list
query = queries.pop()
# Or the Q object with the ones remaining in the list
for item in queries:
query |= item
#... | 73 | 2009-05-12T12:21:28Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 897,884 | <p>A shorter way of writing Dave Webb's answer using <a href="http://docs.python.org/library/functions.html#reduce">python's reduce function</a>:</p>
<pre><code>values = [1,2,3]
# Turn list of values into one big Q objects
query = reduce(lambda q,value: q|Q(pk=value), values, Q())
# Query the model
Article.obj... | 30 | 2009-05-22T13:36:12Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 898,170 | <pre><code>values = [1, 2, 3]
query = reduce(operator.or_, (Q(pk=x) for x in values))
</code></pre>
| 19 | 2009-05-22T14:34:19Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 12,568,718 | <p>In case we want to programmatically set what db field we want to query: </p>
<pre><code>import operator
questions = [('question__contains', 'test'), ('question__gt', 23 )]
q_list = [Q(x) for x in questions]
Poll.objects.filter(reduce(operator.or_, q_list))
</code></pre>
| 3 | 2012-09-24T16:01:10Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 25,094,050 | <p>This one is for dynamic pk list:</p>
<pre><code>pk_list = qs.values_list('pk', flat=True) # i.e [] or [1, 2, 3]
if len(pk_list) == 0:
Article.objects.none()
else:
q = None
for pk in pk_list:
if q is None:
q = Q(pk=pk)
else:
q = q | Q(pk=pk)
Article.objects... | 1 | 2014-08-02T10:30:20Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 29,149,972 | <p>To build more complex queries there is also the option to use built in Q() object's constants Q.OR and Q.AND together with the add() method like so:</p>
<pre><code>list = [1, 2, 3]
# it gets a bit more complicated if we want to dynamically build
# OR queries with dynamic/unknown db field keys, let's say with a list... | 22 | 2015-03-19T16:26:07Z | [
"python",
"django",
"django-q"
] |
How to dynamically compose an OR query filter in Django? | 852,414 | <p>From an example you can see a multiple OR query filter:</p>
<pre><code>Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
</code></pre>
<p>For example, this results in:</p>
<pre><code>[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
</code></pre>
<p>However, I want to creat... | 45 | 2009-05-12T12:08:51Z | 34,359,299 | <p>easy..<br>
from django.db.models import Q
import you model
args = (Q(visibility=1)|(Q(visibility=0)&Q(user=self.user))) #Tuple
parameters={} #dic
order = 'create_at'
limit = 10</p>
<pre><code>Models.objects.filter(*args,**parameters).order_by(order)[:limit]
</code></pre>
| 0 | 2015-12-18T15:50:12Z | [
"python",
"django",
"django-q"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.