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 |
|---|---|---|---|---|---|---|---|---|---|
Windows live api for python | 2,166,232 | <p>I have read documentation about Windows Live API: <a href="http://msdn.microsoft.com/en-us/library/bb463989.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb463989.aspx</a></p>
<p>But how can I retrieve contacts from hotmail with python ?</p>
<p>Is there any example ?</p>
| 0 | 2010-01-30T00:27:16Z | 4,291,695 | <p>For those who are searching for the download link for the library
<a href="http://download.microsoft.com/download/6/2/a/62adfe67-6fee-487f-9c3e-911ce5d0bc9d/webauth-python-1.2.tar.gz" rel="nofollow">http://download.microsoft.com/download/6/2/a/62adfe67-6fee-487f-9c3e-911ce5d0bc9d/webauth-python-1.2.tar.gz</a></p>
| 0 | 2010-11-27T12:42:07Z | [
"python",
"hotmail"
] |
Casting from list of lists of strings to list of lists of ints in python | 2,166,577 | <p>I'm reading some numbers from a data source that represent xy coordinates that I'll be using for a TSP-esque problem. I'm new to python, so I'm trying to make the most of lists. After reading and parsing through the data, I'm left with a list of string lists that looks like this:</p>
<blockquote>
<p>[['565.0', ... | 4 | 2010-01-30T02:26:39Z | 2,166,583 | <pre><code>x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
x = [[int(float(j)) for j in i] for i in x]
</code></pre>
| 8 | 2010-01-30T02:30:20Z | [
"python"
] |
How would you further lock down a Google App Engine app executing untrusted code? | 2,166,650 | <p>We are currently using Google App Engine to evaluate solutions to Python problems submitted by students. We have moved all of the untrusted code execution off to a separate GAE application that doesn't use the datastore. Everything seems to be working fine for the 50+ problems we have uploaded, but I'm curious what ... | 2 | 2010-01-30T02:53:32Z | 2,166,666 | <p>Look at <a href="http://google-app-engine-samples.googlecode.com/files/shell_20091112.tar.gz" rel="nofollow">shell.appspot.com's source</a> -- it actually even uses the datastore (for session persistence). At the core it's basically just doing a simple <code>exec</code> just like you are -- there are other refineme... | 3 | 2010-01-30T03:04:21Z | [
"python",
"google-app-engine"
] |
In Python, how do you programmatically execute unit tests stored in a string? | 2,166,761 | <p>The following code is used to execute doctests in a Google App Engine app. How would you do this for tests written as unit test asserts rather than as doctests? </p>
<pre><code>#The solution and tests are untrusted code passed in to the GAE app.
solution = 'b=5'
unittest = 'assertEqual(b, 5)'
#Here is the doctest... | 4 | 2010-01-30T03:56:04Z | 2,166,819 | <p>Methods of a class, such as <code>unittest.TestCase.assertEqual</code>, don't execute outside of the context provided by an instance of that class. So, a string like your <code>'assertEqual(b, 5)'</code> is really a very, VERY bad case -- note that the string as written will <strong>never</strong> execute properly ... | 3 | 2010-01-30T04:12:47Z | [
"python",
"testing"
] |
Python: how to check if an object is an instance of a namedtuple? | 2,166,818 | <p>How do I check if an object is an instance of a <a href="http://docs.python.org/dev/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields">Named tuple</a>?</p>
| 9 | 2010-01-30T04:12:43Z | 2,166,841 | <p>Calling the <em>function</em> <code>collections.namedtuple</code> gives you a new type that's a subclass of <code>tuple</code> (and no other classes) with a member named <code>_fields</code> that's a tuple whose items are all strings. So you could check for each and every one of these things:</p>
<pre><code>def is... | 14 | 2010-01-30T04:21:18Z | [
"python",
"introspection",
"namedtuple"
] |
Python: how to check if an object is an instance of a namedtuple? | 2,166,818 | <p>How do I check if an object is an instance of a <a href="http://docs.python.org/dev/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields">Named tuple</a>?</p>
| 9 | 2010-01-30T04:12:43Z | 2,166,847 | <p>If you need to check before calling namedtuple specific functions on it, then just call them and catch the exception instead. That's the preferred way to do it in python.</p>
| 3 | 2010-01-30T04:22:15Z | [
"python",
"introspection",
"namedtuple"
] |
Python: how to check if an object is an instance of a namedtuple? | 2,166,818 | <p>How do I check if an object is an instance of a <a href="http://docs.python.org/dev/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields">Named tuple</a>?</p>
| 9 | 2010-01-30T04:12:43Z | 24,331,427 | <p>I use</p>
<pre><code>isinstance(x, tuple) and isinstance(x.__dict__, collections.abc.Mapping)
</code></pre>
<p>which to me appears to best reflect the dictionary aspect of the nature of named tuples.
It appears robust against some conceivable future changes too and might also work with many third-party namedtuple-... | -1 | 2014-06-20T16:05:52Z | [
"python",
"introspection",
"namedtuple"
] |
Using httplib2 in python 3 properly? (Timeout problems) | 2,167,127 | <p>Hey, first time post, I'm really stuck on httplib2. I've been reading up on it from diveintopython3.org, but it mentions nothing about a timeout function. I look up the documentation, but the only thing I see is an ability to put a timeout int but there are no units specified (seconds? milliseconds? What's the defau... | 4 | 2010-01-30T06:52:54Z | 2,167,314 | <p>Set the timeout to 1, and you'll pretty quickly know if it means one millisecond or one second.</p>
<p>I don't know what your try/except should solve, if it hangs on h.request(url) in one case it should hang in the other.</p>
<p>If you run out of memory in that code, then httplib2 doesn't get garbage collected pro... | 1 | 2010-01-30T08:46:52Z | [
"python",
"timeout",
"python-3.x",
"httplib2"
] |
Using httplib2 in python 3 properly? (Timeout problems) | 2,167,127 | <p>Hey, first time post, I'm really stuck on httplib2. I've been reading up on it from diveintopython3.org, but it mentions nothing about a timeout function. I look up the documentation, but the only thing I see is an ability to put a timeout int but there are no units specified (seconds? milliseconds? What's the defau... | 4 | 2010-01-30T06:52:54Z | 11,635,002 | <p><a href="http://code.google.com/p/httplib2/issues/detail?id=124" rel="nofollow">Due to bug</a> httplib2 measured the timeout in seconds <strong>multiplied by 2</strong> <a href="https://code.google.com/p/httplib2/source/browse/python2/httplib2/__init__.py?name=0.7.5" rel="nofollow">until version 0.7.5</a> (2012-08-2... | 4 | 2012-07-24T16:11:37Z | [
"python",
"timeout",
"python-3.x",
"httplib2"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 2,167,874 | <pre><code>while running:
for elem,next_elem in zip(li, li[1:]+[li[0]]):
...
</code></pre>
| 5 | 2010-01-30T12:35:59Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 2,167,877 | <p>You can use a pairwise cyclic iterator:</p>
<pre><code>from itertools import izip, cycle, tee
def pairwise(seq):
a, b = tee(seq)
next(b)
return izip(a, b)
for elem, next_elem in pairwise(cycle(li)):
...
</code></pre>
| 6 | 2010-01-30T12:37:28Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 2,167,962 | <p>After thinking this through carefully, I think this is the best way. It lets you step off in the middle easily without using <code>break</code>, which I think is important, and it requires minimal computation, so I think it's the fastest. It also doesn't require that <code>li</code> be a list or tuple. It could b... | 34 | 2010-01-30T13:17:33Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 2,167,979 | <pre><code>while running:
lenli = len(li)
for i, elem in enumerate(li):
thiselem = elem
nextelem = li[(i+1)%lenli]
</code></pre>
| 2 | 2010-01-30T13:22:19Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 24,752,357 | <p>A rather different way to solve this: </p>
<pre><code> li = [0,1,2,3]
for i in range(len(li)):
if i < len(li)-1:
# until end is reached
print 'this', li[i]
print 'next', li[i+1]
else:
# end
print 'this', li[i]
</code></pre>
| 0 | 2014-07-15T07:30:33Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 30,206,848 | <p>Use the zip method in Python. This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables</p>
<pre><code> while running:
for thiselem,nextelem in zip(li, li[1 : ] + li[ : 1]):
#Do whatever you want with thiselem and n... | 1 | 2015-05-13T06:04:19Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 38,229,487 | <pre><code>c = [ 1, 2, 3, 4 ]
i = int(raw_input(">"))
if i < 4:
print i + 1
else:
print -1
</code></pre>
| -3 | 2016-07-06T16:53:41Z | [
"python",
"list",
"iteration",
"next"
] |
Getting next element while cycling through a list | 2,167,868 | <pre><code>li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
</code></pre>
<p>When this reaches the last element, an <code>IndexError</code> is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actua... | 21 | 2010-01-30T12:33:37Z | 39,347,338 | <pre><code> li = [0, 1, 2, 3]
for elem in li:
if (li.index(elem))+1 != len(li):
thiselem = elem
nextelem = li[li.index(elem)+1]
print 'thiselem',thiselem
print 'nextel',nextelem
else:
print 'thiselem',... | 0 | 2016-09-06T10:57:03Z | [
"python",
"list",
"iteration",
"next"
] |
How can I pickle suds results? | 2,167,894 | <p>To avoid repeatedly accessing a SOAP server during development, I'm trying to cache the results so I can run the rest of my code without querying the server each time.</p>
<p>With the code below I get a <code>PicklingError: Can't pickle <class suds.sudsobject.AdvertiserSearchResponse at 0x03424060>: it's not ... | 5 | 2010-01-30T12:46:34Z | 2,167,948 | <p>You are pickling the class object itself, and not instance objects of the class. This won't work if the class object is recreated. However, pickling instances of the class will work as long as the class object exists.</p>
| 1 | 2010-01-30T13:11:44Z | [
"python",
"soap",
"pickle",
"suds"
] |
How can I pickle suds results? | 2,167,894 | <p>To avoid repeatedly accessing a SOAP server during development, I'm trying to cache the results so I can run the rest of my code without querying the server each time.</p>
<p>With the code below I get a <code>PicklingError: Can't pickle <class suds.sudsobject.AdvertiserSearchResponse at 0x03424060>: it's not ... | 5 | 2010-01-30T12:46:34Z | 2,168,777 | <p>As the error message you're currently getting is trying to tell you, you're trying to pickle instances that are <strong>not</strong> picklable (in the ancient legacy pickle protocol you're now using) because their class defines <code>__slots__</code> but not a <code>__getstate__</code> method.</p>
<p>However, even ... | 6 | 2010-01-30T17:45:01Z | [
"python",
"soap",
"pickle",
"suds"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,129 | <p>To go from "0,1,2" to ['0','1','2'] its just <code>"0,1,2".split(",")</code></p>
<p>So if you have it in a variable <code>users</code>, then <code>users.split(",")</code> will give you the list.</p>
<p>If you need them as ints instead of strings, it would be <code>[int(x) for x in users.split(',')]</code>.</p>
| 2 | 2010-01-30T14:15:04Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,133 | <p>To convert the string to a list, use <code>split</code>.</p>
<p>To convert the list of strings to a list of integers, use a list comprehension with <code>int</code>.</p>
<p>So putting it all together, it looks something like this:</p>
<pre><code>s = '0,1,2'
l = [int(x) for x in s.split(',')]
</code></pre>
<p>Res... | 2 | 2010-01-30T14:15:47Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,137 | <p>Use the <code>split</code> method. Example:</p>
<pre><code>>>> "0,1,2".split(",")
['0', '1', '2']
</code></pre>
<p>Or even,</p>
<pre><code>>>> [int(x) for x in "0,1,2".split(",")]
[0, 1, 2]
</code></pre>
| 29 | 2010-01-30T14:16:10Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,215 | <pre><code>import ast
x=ast.literal_eval('0,1,2')
print(x)
# (0, 1, 2)
</code></pre>
<p><code>ast.literal_eval</code> is like <code>eval</code>, but completely safe since it restricts the string to literals such as strings, numbers, tuples, lists, dicts, booleans and <code>None</code>.</p>
<p>Another alternative, not... | 3 | 2010-01-30T14:39:14Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,468 | <p>You can use following code:</p>
<pre><code>s = 'user.com/?users=0,1,2'
s.rpartition('?users=')[2].split(',')
</code></pre>
| 0 | 2010-01-30T16:04:32Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 2,168,933 | <p>This question was originally tagged Django, so I'll proceed with that in mind.</p>
<p>Inside your view function, the <code>request</code> object has a GET attribute that is an instance of a <a href="http://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects">QueryDict</a>. If you always know that ... | 6 | 2010-01-30T18:33:49Z | [
"python",
"django",
"string"
] |
Converting a String to List in Python | 2,168,123 | <p>I'm trying to create a list from arguments I receive in a url.</p>
<p>e.g I have:</p>
<pre><code> user.com/?users=0,1,2
</code></pre>
<p>Now when I receive it in the request it comes as a string. I want to make a list out of "0,1,2" [0,1,2]</p>
| 8 | 2010-01-30T14:13:31Z | 12,596,582 | <p>You have eval to convert string to "real code":</p>
<p>Example:</p>
<pre><code>>>> l = u"[('0','None'),('2','Taxable Goods'),('4','Shipping')]"
>>> type(l)
<type 'unicode'>
>>> t = eval(l)
>>> type(t)
<type 'list'>
</code></pre>
| -1 | 2012-09-26T07:21:44Z | [
"python",
"django",
"string"
] |
Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX | 2,168,409 | <p>Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX using the GoogleAppEngineLauncher.</p>
<p>I'm trying to setup facebook development site (using a dyndns.org hostname pointing at my firewall which redirects the call to my mac book).</p>
<p>It seems like GoogleAppEngine... | 10 | 2010-01-30T15:47:05Z | 2,168,494 | <p>This is possible by passing the <code>-a</code> argument to <code>dev_appserver.py</code>, i.e. <code>dev_appserver.py -a <your-ip> <your_app></code>. See also <a href="http://www.ventanazul.com/webzine/articles/public-ip-google-app-engine-development-server" rel="nofollow">this article on using public I... | 4 | 2010-01-30T16:11:10Z | [
"python",
"google-app-engine",
"facebook",
"osx"
] |
Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX | 2,168,409 | <p>Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX using the GoogleAppEngineLauncher.</p>
<p>I'm trying to setup facebook development site (using a dyndns.org hostname pointing at my firewall which redirects the call to my mac book).</p>
<p>It seems like GoogleAppEngine... | 10 | 2010-01-30T15:47:05Z | 2,168,639 | <p>Per the docs, it's technically possible:</p>
<blockquote>
<p>--address=...</p>
<p>The host address to use for the
server. You may need to set this to be
able to access the development server
from another computer on your network.
An address of 0.0.0.0 allows both
localhost access and hostname acces... | 2 | 2010-01-30T17:04:50Z | [
"python",
"google-app-engine",
"facebook",
"osx"
] |
Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX | 2,168,409 | <p>Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX using the GoogleAppEngineLauncher.</p>
<p>I'm trying to setup facebook development site (using a dyndns.org hostname pointing at my firewall which redirects the call to my mac book).</p>
<p>It seems like GoogleAppEngine... | 10 | 2010-01-30T15:47:05Z | 4,706,303 | <p>For the eclipse (PyDev) users, Right-click on your project <strong><code>Run As > Run Configurations...</code></strong></p>
<p>In the <strong>Arguments Tab</strong>, add the -a and -p arguments:</p>
<pre><code>-a 0.0.0.0 -p 80 "${workspace_loc:project}"
</code></pre>
| 5 | 2011-01-16T15:47:57Z | [
"python",
"google-app-engine",
"facebook",
"osx"
] |
Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX | 2,168,409 | <p>Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX using the GoogleAppEngineLauncher.</p>
<p>I'm trying to setup facebook development site (using a dyndns.org hostname pointing at my firewall which redirects the call to my mac book).</p>
<p>It seems like GoogleAppEngine... | 10 | 2010-01-30T15:47:05Z | 21,776,775 | <p>As per the latest <a href="https://developers.google.com/appengine/docs/python/tools/devserver?csw=1#Python_Command-line_arguments">documentation</a> <code>-a</code> wont work anymore.</p>
<p>This is possible by passing <code>--host</code> argument with <code>dev_appserver.py</code> command</p>
<p><code>dev_appser... | 8 | 2014-02-14T10:26:46Z | [
"python",
"google-app-engine",
"facebook",
"osx"
] |
Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX | 2,168,409 | <p>Can access AppEngine SDK sites via local ip-address when localhost works just fine and a MacOSX using the GoogleAppEngineLauncher.</p>
<p>I'm trying to setup facebook development site (using a dyndns.org hostname pointing at my firewall which redirects the call to my mac book).</p>
<p>It seems like GoogleAppEngine... | 10 | 2010-01-30T15:47:05Z | 29,235,036 | <p>In Android Studio with Google App Engine plugin.
Just add <code>httpAddress = '0.0.0.0'</code> to app cfg in build.grade file.</p>
| 0 | 2015-03-24T14:19:40Z | [
"python",
"google-app-engine",
"facebook",
"osx"
] |
pubDate RSS parsing weirdness with Beautifulsoup/Python | 2,168,516 | <p>I'm trying to parse an RSS/Podcast feed using Beautifulsoup and everything is working nicely except I can't seem to parse the 'pubDate' field.</p>
<pre><code>data = urllib2.urlopen("http://www.democracynow.org/podcast.xml")
dom = BeautifulStoneSoup(data, fromEncoding='utf-8')
items = dom.findAll('item');
for item ... | 3 | 2010-01-30T16:19:24Z | 2,168,541 | <p>It works with <code>item.find('pubdate').string.strip()</code>.
Why don't you use <a href="http://pythonhosted.org/feedparser/" rel="nofollow">feedparser</a> ?</p>
| 3 | 2010-01-30T16:26:12Z | [
"python",
"beautifulsoup"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,534 | <p>I am certain that someone is going to come up with a "Pythonic" solution, but what ever happened to just accepting it as a list and checking it afterward?</p>
<pre><code>command_data = raw_input('>').split(" ", 1)
if len(command_data) > 1:
do some brilliant stuff next
</code></pre>
<p>Sorry, I think the C+... | 2 | 2010-01-30T16:24:46Z | [
"python"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,538 | <pre><code>line = raw_input('>').split(" ", 1)
command = line[0]
data = line[1] if len(line) > 1 else ""
</code></pre>
<p>Make sure you use <code>raw_input</code> if you don't want your data being evaluated as a Python expression.</p>
| 1 | 2010-01-30T16:25:39Z | [
"python"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,544 | <p>Unless you're using Python 3 you shouldn't be using the <code>input</code>-function for user input. Use the <code>raw_input</code>-function instead.</p>
<p>As for the actual question; retrieve the result of the split as a list.</p>
<p>The first item in the list will be the command, and the optional following items... | 4 | 2010-01-30T16:27:19Z | [
"python"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,547 | <p>The <em>best</em> way is to build a parser, but if you just want something to work quickly you could just have a list of the commands you want to allow, such as:</p>
<pre><code>commands = ['foo', 'bar', 'exit', 'hello world', 'add number']
</code></pre>
<p>Then for each command check if your input satisfies <code>... | 2 | 2010-01-30T16:27:57Z | [
"python"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,553 | <pre><code>command, data = (input('>') + ' ').split(" ", 1)
</code></pre>
| 1 | 2010-01-30T16:29:39Z | [
"python"
] |
python string split | 2,168,522 | <p>I am trying to recognise user typed strings such as "exit" or "add number" using this:</p>
<pre><code> command, data = input('>').split(" ", 1)
</code></pre>
<p>It works for two word input, but not one word of input ("need more than 1 value to unpack").</p>
<p>What is the best way of accepting both one/two wor... | 2 | 2010-01-30T16:21:51Z | 2,168,556 | <p>This is what <code>partition</code> is for:</p>
<pre><code>command, _, data = raw_input('>').partition(" ")
</code></pre>
<p>If only one word was specified, <code>data</code> will be assigned an empty string.</p>
| 3 | 2010-01-30T16:30:02Z | [
"python"
] |
random.sample() returning the same random sequence every time? | 2,168,648 | <p>I'm using python's random.sample(population, k) function to generate a set of random values from a list to create new permutations of that list. The issue is that each time it runs through a loop, it's generating the <em>exact same</em> random sequence. Why is this? I even used random.seed(i) so that the i variab... | 7 | 2010-01-30T17:07:38Z | 2,168,651 | <p>Do not change the seed for every sample. That breaks the random number generator and absolutely assures that it's not random.</p>
<p>Set the seed exactly once. And never change that value again while your application is running.</p>
<p>The Random Number generator starts with a known, constant seed. Each time yo... | 5 | 2010-01-30T17:08:38Z | [
"python",
"random"
] |
random.sample() returning the same random sequence every time? | 2,168,648 | <p>I'm using python's random.sample(population, k) function to generate a set of random values from a list to create new permutations of that list. The issue is that each time it runs through a loop, it's generating the <em>exact same</em> random sequence. Why is this? I even used random.seed(i) so that the i variab... | 7 | 2010-01-30T17:07:38Z | 2,168,682 | <p>I'm not sure what you mean by "generating the <em>exact same</em> random sequence". Since you only give us a snippet that can't be run on its own, it's quite possible that there are bugs in other parts of your code that you choose not to show us -- I've tried adding the absolutely minimal amount of code needed to m... | 11 | 2010-01-30T17:22:00Z | [
"python",
"random"
] |
Parsing forwarded emails | 2,168,719 | <p>I'm writing some code to <strong>parse</strong> forwarded emails. What I'm not sure is if maybe there is some Python library, some RFC I could stick to or some other resource that would allow me to automate the task. </p>
<p>To be precise, I don't know if the "layout" of forwarded emails is covered by some standard... | 7 | 2010-01-30T17:29:56Z | 2,168,729 | <p>Standard for a reply/forward is > prepending each line the number of times the mail is nested including who sent the initial e-mail is up to the client to sort out. So what you need to do in python is simply add > to the start of each line.</p>
<pre><code>imap Test <imap@gazler.com> Wrote:
>
>twice
&g... | 1 | 2010-01-30T17:32:37Z | [
"python",
"rfc"
] |
Parsing forwarded emails | 2,168,719 | <p>I'm writing some code to <strong>parse</strong> forwarded emails. What I'm not sure is if maybe there is some Python library, some RFC I could stick to or some other resource that would allow me to automate the task. </p>
<p>To be precise, I don't know if the "layout" of forwarded emails is covered by some standard... | 7 | 2010-01-30T17:29:56Z | 2,168,922 | <p>In my experience just about ever email client forwards/replies differently. Typically you'll have a plain text version and a html encoded version in the mime at the bottom of the mail pack. Mail headers do have a RFC (<a href="http://www.faqs.org/rfcs/rfc2822.html" rel="nofollow">http://www.faqs.org/rfcs/rfc2822.htm... | 2 | 2010-01-30T18:31:27Z | [
"python",
"rfc"
] |
Parsing forwarded emails | 2,168,719 | <p>I'm writing some code to <strong>parse</strong> forwarded emails. What I'm not sure is if maybe there is some Python library, some RFC I could stick to or some other resource that would allow me to automate the task. </p>
<p>To be precise, I don't know if the "layout" of forwarded emails is covered by some standard... | 7 | 2010-01-30T17:29:56Z | 2,168,987 | <p>As the other answers already indicate: there is no standard, and your program is not going to be flawless.</p>
<p>You could have a look at the headers, in particular the <code>User-Agent</code> header, to see what kind of client was used, and code specifically for the most common clients.</p>
<p>To find out what c... | 2 | 2010-01-30T18:47:42Z | [
"python",
"rfc"
] |
Parsing forwarded emails | 2,168,719 | <p>I'm writing some code to <strong>parse</strong> forwarded emails. What I'm not sure is if maybe there is some Python library, some RFC I could stick to or some other resource that would allow me to automate the task. </p>
<p>To be precise, I don't know if the "layout" of forwarded emails is covered by some standard... | 7 | 2010-01-30T17:29:56Z | 2,172,262 | <p>Unlike what many other people said, there <strong>is</strong> a standard on forwarded emails, <a href="http://www.ietf.org/rfc/rfc2046.txt" rel="nofollow">RFC 2046</a>, "Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types", more than ten years old. See specially its section 5.2, "Message Media Type".<... | 5 | 2010-01-31T15:46:27Z | [
"python",
"rfc"
] |
Python: creating class instance without calling initializer | 2,168,964 | <p>Is there any way to avoid calling <code>__init__</code> on a class while initializing it, such as from a class method?</p>
<p>I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling <code>__i... | 15 | 2010-01-30T18:41:37Z | 2,169,007 | <p>Pass another argument to the constructor, like so:</p>
<pre><code>def __init__(self, string, simple = None):
if simple is None:
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
else:
self.__string = string
self.__simple = simple
</code></pre>
<p>Y... | 0 | 2010-01-30T18:53:21Z | [
"python",
"class",
"constructor",
"initialization"
] |
Python: creating class instance without calling initializer | 2,168,964 | <p>Is there any way to avoid calling <code>__init__</code> on a class while initializing it, such as from a class method?</p>
<p>I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling <code>__i... | 15 | 2010-01-30T18:41:37Z | 2,169,191 | <p>When feasible, letting <code>__init__</code> get called (and make the call innocuous by suitable arguments) is preferable. However, should that require too much of a contortion, you do have an alternative, as long as you avoid the disastrous choice of using old-style classes (there is <strong>no</strong> good reaso... | 18 | 2010-01-30T19:46:52Z | [
"python",
"class",
"constructor",
"initialization"
] |
Python: creating class instance without calling initializer | 2,168,964 | <p>Is there any way to avoid calling <code>__init__</code> on a class while initializing it, such as from a class method?</p>
<p>I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling <code>__i... | 15 | 2010-01-30T18:41:37Z | 14,719,469 | <p>A trick the standard pickle and copy modules use is to create an empty class, instantiate the object using that, and then assign that instance's <code>__class__</code> to the "real" class. e.g.</p>
<pre><code>>>> class MyClass(object):
... init = False
... def __init__(self):
... print 'ini... | 4 | 2013-02-06T00:22:17Z | [
"python",
"class",
"constructor",
"initialization"
] |
Python: creating class instance without calling initializer | 2,168,964 | <p>Is there any way to avoid calling <code>__init__</code> on a class while initializing it, such as from a class method?</p>
<p>I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling <code>__i... | 15 | 2010-01-30T18:41:37Z | 14,738,204 | <p>Using a metaclass provides a nice solution in this example. The metaclass has limited use but works fine.</p>
<pre><code>>>> class MetaInit(type):
def __call__(cls, *args, **kwargs):
if args or kwargs:
return super().__call__(*args, **kwargs)
return cls.__new__(cls)
>&g... | 0 | 2013-02-06T20:30:17Z | [
"python",
"class",
"constructor",
"initialization"
] |
How to track state when iterating in Python's Mako Templates | 2,169,065 | <p>I want to loop over a list and print the elements seperated by ',', with no trailing comma. I can't just <code>', '.join(headings)</code> because of the formating and escaping. But the following obviously leaves me with a trailing comma.</p>
<pre><code>% for x in headings:
<a href='#${x|u}'>${x}</a>, ... | 3 | 2010-01-30T19:11:45Z | 2,169,151 | <p>I do stuff like this:</p>
<pre><code><%def name="format( item )"><a href="#${item|u}">${item|u}</a>
</%def>
${', '.join( format(item) for item in l)}
</code></pre>
| 4 | 2010-01-30T19:35:58Z | [
"python",
"templates",
"mako"
] |
How to track state when iterating in Python's Mako Templates | 2,169,065 | <p>I want to loop over a list and print the elements seperated by ',', with no trailing comma. I can't just <code>', '.join(headings)</code> because of the formating and escaping. But the following obviously leaves me with a trailing comma.</p>
<pre><code>% for x in headings:
<a href='#${x|u}'>${x}</a>, ... | 3 | 2010-01-30T19:11:45Z | 2,169,156 | <p>To keep track of the first or last leg through the loop, in Mako like in plain Python, use:</p>
<pre><code>% for i, x in enumerate(headings):
</code></pre>
<p>so <code>i</code> is 0 on the first leg and <code>len(headings) - 1</code> on the last leg.</p>
| 3 | 2010-01-30T19:38:32Z | [
"python",
"templates",
"mako"
] |
How to track state when iterating in Python's Mako Templates | 2,169,065 | <p>I want to loop over a list and print the elements seperated by ',', with no trailing comma. I can't just <code>', '.join(headings)</code> because of the formating and escaping. But the following obviously leaves me with a trailing comma.</p>
<pre><code>% for x in headings:
<a href='#${x|u}'>${x}</a>, ... | 3 | 2010-01-30T19:11:45Z | 18,674,577 | <p>Extending on @AlexMartelli's answer, I like to couple the <code>enumerate</code> thing with a nice trick to keep the instruction small:</p>
<pre><code>% for i, x in enumerate(xs):
${','*bool(i)} ${x}
% endfor
</code></pre>
| 1 | 2013-09-07T14:57:17Z | [
"python",
"templates",
"mako"
] |
Ignoring case, punctuation, and whitespace in Strings | 2,169,170 | <p>What is the most efficient way of ignoring case, punctuation, and whitespace in strings? These strings should be divided into words instead of characters should ignore the aforementioned details on comparisons, and slices of these word-strings should be as efficient as possible with speed in mind.</p>
<p>I was goin... | 0 | 2010-01-30T19:41:36Z | 2,169,214 | <p>If you want iteration on a String instance to iterate on its <code>self.__string</code>, as your <code>__iter__</code> method indicates, the only sensible choice for length is also to return the length of <code>__string</code> -- it would be <em>truly</em> peculiar if <code>len(x)</code> and <code>sum(1 for _ in x)<... | 2 | 2010-01-30T19:51:47Z | [
"python",
"filter",
"state",
"slice"
] |
Ignoring case, punctuation, and whitespace in Strings | 2,169,170 | <p>What is the most efficient way of ignoring case, punctuation, and whitespace in strings? These strings should be divided into words instead of characters should ignore the aforementioned details on comparisons, and slices of these word-strings should be as efficient as possible with speed in mind.</p>
<p>I was goin... | 0 | 2010-01-30T19:41:36Z | 2,169,328 | <p><strong>"What is the best way to go about fixing this problem?"</strong></p>
<p>The best -- and only -- way is to define what this object "means" and what the length of this object "means".</p>
<p>The object appears to be a list of words. Nothing more. That seems to be the value in <code>_string</code>.</p>
<p>... | 2 | 2010-01-30T20:32:01Z | [
"python",
"filter",
"state",
"slice"
] |
How to add cookie to existing cookielib CookieJar instance in Python? | 2,169,281 | <p>I have a CookieJar that's being used with mechanize that I want to add a cookie to. How can I go about doing this? make_cookie() and set_cookie() weren't clear enough for me.</p>
<pre><code>br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
</code></pre>
| 14 | 2010-01-30T20:12:26Z | 2,169,298 | <p>Managed to figure this out</p>
<pre><code>import mechanize
import cookielib
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
ck = cookielib.Cookie(version=0, name='Name', value='1', port=None, port_specified=False, domain='www.example.com', domain_specified=False, domain_initial_dot=False... | 34 | 2010-01-30T20:20:52Z | [
"python",
"cookies",
"mechanize",
"cookiejar",
"cookielib"
] |
PyQt4 QDialog connections not being made | 2,169,325 | <p>I am working on an application using PyQt4 and the designer it provides. I have a main window application that works fine, but I wanted to create custom message dialogs. I designed a dialog and set up some custom signal/slot connections in the <code>__init__</code> method and wrote an <code>if __name__=='__main__'... | 1 | 2010-01-30T20:31:16Z | 2,222,480 | <p>It feels like the signal is just not getting passed from the parent down to your child QDIalog.</p>
<p>Try these suggestions:</p>
<ol>
<li>Use the new method for connecting signals</li>
<li>Instead of extending the classes pyuic created, extend the actual QT classes and call the ones generated by pyuic</li>
</ol>
... | 0 | 2010-02-08T15:09:16Z | [
"python",
"pyqt4",
"signals-slots",
"qdialog"
] |
how to write a file correctly after editing | 2,169,445 | <p>say i encrypt a .doc (or any other type) file and i decrypt it later. however, i cant open it because during the decryption process, [null]s and [DC1] and other highlighted chars were not put back into the file since they are not part of the ASCII characters. how are they written in other programs that compress/encr... | -1 | 2010-01-30T21:11:41Z | 2,169,503 | <p>What encryption are you using?<br>
Proper encryption will also encrypt non ascii characters so they can be decrypted properly later</p>
| 1 | 2010-01-30T21:27:09Z | [
"python",
"encryption"
] |
how to write a file correctly after editing | 2,169,445 | <p>say i encrypt a .doc (or any other type) file and i decrypt it later. however, i cant open it because during the decryption process, [null]s and [DC1] and other highlighted chars were not put back into the file since they are not part of the ASCII characters. how are they written in other programs that compress/encr... | -1 | 2010-01-30T21:11:41Z | 2,169,512 | <p>Nulls and DC1's and so on are <strong>definitely</strong> part of the ASCII character set, so I don't know what you're talking about. So, for example, consider...:</p>
<pre><code>>>> import pyDes
>>> f = open('afile', 'w')
>>> f.write('Nel mezzo del cammin di nostra vita\n')
>>> ... | 4 | 2010-01-30T21:28:50Z | [
"python",
"encryption"
] |
Importing _mysql in MySQLdb | 2,169,449 | <p>Why is _mysql in the MySQLdb module a C file? When the module tries to import it, I get an import error. What should I do?</p>
| 1 | 2010-01-30T21:12:27Z | 2,169,464 | <p>It's the adaptor that sits between the Python <code>MySQLdb</code> module and the C <code>libmysqlclient</code> library. One of the most common reasons for it not loading is that the appropriate libmysqlclient library is not in place.</p>
| 0 | 2010-01-30T21:16:22Z | [
"python",
"mysql",
"c"
] |
Importing _mysql in MySQLdb | 2,169,449 | <p>Why is _mysql in the MySQLdb module a C file? When the module tries to import it, I get an import error. What should I do?</p>
| 1 | 2010-01-30T21:12:27Z | 2,169,471 | <p><strong>Edit</strong>: <a href="http://stackoverflow.com/questions/2171078/no-module-names-mysql-where-is-mysql">This</a> might be the answer to your question.</p>
<p>When I try to import <code>_mysql</code>, I get no error:</p>
<pre><code>import _mysql
print(_mysql)
# <module '_mysql' from '/usr/lib/pymodules/... | 0 | 2010-01-30T21:18:09Z | [
"python",
"mysql",
"c"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 2,169,501 | <p>Can't you use hstack and vstack? See <a href="http://www.scipy.org/Tentative_NumPy_Tutorial#head-2dbfc93288ea06a02f270f2f9e6930ffbdd78b87" rel="nofollow">here</a>.
Like this:</p>
<pre><code>>>> import numpy as np
>>> b = np.array([0]*4)
>>> b.shape = (2,2)
>>> w = b + 0xAA
>&... | 2 | 2010-01-30T21:26:02Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 2,169,547 | <p>this ought to do it--any size checkerboard you want (just pass in width and height, as w, h); also i have hard-coded cell height/width to 1, though of course this could also be parameterized so that an arbitrary value is passed in:</p>
<p>import numpy as NP</p>
<pre><code>def build_checkerboard(w, h) :
re = NP... | 6 | 2010-01-30T21:37:50Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 2,169,787 | <p>I'm not sure if this is better than what I had:</p>
<pre><code>c = numpy.fromfunction(lambda x,y: ((x//sq) + (y//sq)) % 2, (w,h))
self.chex = numpy.array((w,h,3))
self.chex[c == 0] = (0xAA, 0xAA, 0xAA)
self.chex[c == 1] = (0x99, 0x99, 0x99)
</code></pre>
| 2 | 2010-01-30T22:50:21Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 2,171,883 | <p>Here's another way to do it using <code>ogrid</code> which is a bit faster:</p>
<pre><code>import numpy as np
import Image
w, h = 600, 800
sq = 15
color1 = (0xFF, 0x80, 0x00)
color2 = (0x80, 0xFF, 0x00)
def use_ogrid():
coords = np.ogrid[0:w, 0:h]
idx = (coords[0] // sq + coords[1] // sq) % 2
vals = n... | 5 | 2010-01-31T13:52:58Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 6,179,649 | <p>Late, but for posterity:</p>
<pre><code>def check(w, h, c0, c1, blocksize):
tile = np.array([[c0,c1],[c1,c0]]).repeat(blocksize, axis=0).repeat(blocksize, axis=1)
grid = np.tile(tile, ( h/(2*blocksize)+1, w/(2*blocksize)+1, 1))
return grid[:h,:w]
</code></pre>
| 2 | 2011-05-30T18:35:41Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 17,066,623 | <p>I recently want the same function and i modified doug's answer a little bit as follows:</p>
<pre><code>def gen_checkerboard(grid_num, grid_size):
row_even = grid_num/2 * [0,1]
row_odd = grid_num/2 * [1,0]
checkerboard = numpy.row_stack(grid_num/2*(row_even, row_odd))
return checkerboard.repeat(grid_... | 0 | 2013-06-12T13:19:16Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 20,124,948 | <p>I modified hass's answer as follows.</p>
<pre><code>import math
import numpy as np
def checkerboard(w, h, c0, c1, blocksize):
tile = np.array([[c0,c1],[c1,c0]]).repeat(blocksize, axis=0).repeat(blocksize, axis=1)
grid = np.tile(tile,(int(math.ceil((h+0.0)/(2*blocksize))),int(math.ceil((w+0.0)/(2*bl... | 0 | 2013-11-21T15:25:42Z | [
"python",
"numpy"
] |
How to make a checkerboard in numpy? | 2,169,478 | <p>I'm using numpy to initialize a pixel array to a gray checkerboard (the classic representation for "no pixels", or transparent). It seems like there ought to be a whizzy way to do it with numpy's amazing array assignment/slicing/dicing operations, but this is the best I've come up with:</p>
<pre><code>w, h = 600, ... | 13 | 2010-01-30T21:19:53Z | 37,440,123 | <p>I'd use the <a href="http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.kron.html" rel="nofollow">Kronecker product</a> <code>kron</code>:</p>
<pre><code>np.kron([[1, 0] * 4, [0, 1] * 4] * 4, np.ones((10, 10)))
</code></pre>
<p>The checkerboard in this example has 2*4=8 fields of size 10x10 in each d... | 2 | 2016-05-25T14:27:27Z | [
"python",
"numpy"
] |
Use numpy to mask an image with a pattern? | 2,169,605 | <p>I'm using numpy to build pixel arrays. An 800x600 image is an 3-dimensional array of uint8, 800x600x3. I also have a similar array with a fixed pattern (a checkerboard, see <a href="http://stackoverflow.com/questions/2169478/how-to-make-a-checkerboard-in-numpy">here</a>). I have another array, 800x600 of mask valu... | 12 | 2010-01-30T21:57:22Z | 2,169,684 | <p>Try:</p>
<pre><code>image[mask[:] == 0,...] = chex[mask[:] == 0,...]
</code></pre>
| 0 | 2010-01-30T22:21:44Z | [
"python",
"image-processing",
"numpy"
] |
Use numpy to mask an image with a pattern? | 2,169,605 | <p>I'm using numpy to build pixel arrays. An 800x600 image is an 3-dimensional array of uint8, 800x600x3. I also have a similar array with a fixed pattern (a checkerboard, see <a href="http://stackoverflow.com/questions/2169478/how-to-make-a-checkerboard-in-numpy">here</a>). I have another array, 800x600 of mask valu... | 12 | 2010-01-30T21:57:22Z | 2,170,057 | <pre><code>idx=(mask==0)
image[idx]=chex[idx]
</code></pre>
<p>Note that <code>image</code> has shape (800,600,3), while <code>idx</code> has shape (800,600). <a href="http://docs.scipy.org/numpy/docs/numpy-docs/reference/arrays.indexing.rst/#arrays-indexing">The rules</a> for indexing state </p>
<blockquote>
<p>if... | 12 | 2010-01-31T00:17:15Z | [
"python",
"image-processing",
"numpy"
] |
Use numpy to mask an image with a pattern? | 2,169,605 | <p>I'm using numpy to build pixel arrays. An 800x600 image is an 3-dimensional array of uint8, 800x600x3. I also have a similar array with a fixed pattern (a checkerboard, see <a href="http://stackoverflow.com/questions/2169478/how-to-make-a-checkerboard-in-numpy">here</a>). I have another array, 800x600 of mask valu... | 12 | 2010-01-30T21:57:22Z | 2,170,373 | <p>I used arrays of 8x6x3, 8x6x3, and 8x6 to represent your image array, checker array, and mask array respectively.</p>
<pre><code># first create mini-versions of your arrays:
mask = NP.random.random_integers(0, 1, 48).reshape(8, 6)
img = NP.random.random_integers(3, 9, 8*6*3).reshape(8, 6, 3)
chk = NP.ones((8, 6, 3)... | 0 | 2010-01-31T02:48:01Z | [
"python",
"image-processing",
"numpy"
] |
Using list comprehension in Python to do something similar to zip()? | 2,169,623 | <p>I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.</p>
<pre><code>cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']
print zip(cities,airports)
[('Chicago', 'ORD'), ('D... | 5 | 2010-01-30T22:03:43Z | 2,169,626 | <p>Something like this:</p>
<pre><code>[[c, a] for c, a in zip(cities, airports)]
</code></pre>
<p>Alternately, the <code>list</code> constructor can convert tuples to lists:</p>
<pre><code>[list(x) for x in zip(cities, airports)]
</code></pre>
<p>Or, the <code>map</code> function is slightly less verbose in this c... | 24 | 2010-01-30T22:05:44Z | [
"python",
"list",
"list-comprehension"
] |
Using list comprehension in Python to do something similar to zip()? | 2,169,623 | <p>I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.</p>
<pre><code>cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']
print zip(cities,airports)
[('Chicago', 'ORD'), ('D... | 5 | 2010-01-30T22:03:43Z | 2,169,640 | <p>This takes <code>zip</code>'s output and converts all tuples to lists:</p>
<pre><code>map(list, zip(cities, airports))
</code></pre>
<p>As for the performance of each:</p>
<pre><code>$ python -m timeit -c '[ [a, b] for a, b in zip(xrange(100), xrange(100)) ]'
10000 loops, best of 3: 68.3 usec per loop
$ python -... | 1 | 2010-01-30T22:08:41Z | [
"python",
"list",
"list-comprehension"
] |
Using list comprehension in Python to do something similar to zip()? | 2,169,623 | <p>I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.</p>
<pre><code>cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']
print zip(cities,airports)
[('Chicago', 'ORD'), ('D... | 5 | 2010-01-30T22:03:43Z | 2,169,644 | <p>A list comprehension, without some help from <code>zip</code>, <code>map</code>, or <code>itertools</code>, cannot institute a "parallel loop" on multiple sequences -- only simple loops on one sequence, or "nested" loops on multiple ones.</p>
| 4 | 2010-01-30T22:10:20Z | [
"python",
"list",
"list-comprehension"
] |
Using list comprehension in Python to do something similar to zip()? | 2,169,623 | <p>I'm a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it's a pretty powerful feature that's worth learning.</p>
<pre><code>cities = ['Chicago', 'Detroit', 'Atlanta']
airports = ['ORD', 'DTW', 'ATL']
print zip(cities,airports)
[('Chicago', 'ORD'), ('D... | 5 | 2010-01-30T22:03:43Z | 2,169,937 | <p>If you wanted to do it without using zip at all, you would have to do something like this:</p>
<pre><code>[ [cities[i],airports[i]] for i in xrange(min(len(cities), len(airports))) ]
</code></pre>
<p>but there is no reason to do that other than an intellectual exercise. </p>
<p>Using <code>map(list, zip(cities, ... | 4 | 2010-01-30T23:33:23Z | [
"python",
"list",
"list-comprehension"
] |
Python: Optimizing a tree evaluator | 2,169,682 | <p>I know tree is a well studied structure. </p>
<p>I'm writing a program that randomly generates many expression trees and then sorts and selects by a fitness attribute.</p>
<p>I have a class MakeTreeInOrder() that turns the tree into a string that 'eval' can evaluate.</p>
<p>but it gets called many many times, an... | 2 | 2010-01-30T22:21:06Z | 2,170,073 | <p>Your example imports numpy and random but never uses them. It also has a "for i in range(2))" with no body. This is clearly not valid Python code.</p>
<p>You don't define what 'cargo' and the nodes are supposed to contain. It appears that 'cargo' is a number, since it comes from itertools.count().next(). But that m... | 1 | 2010-01-31T00:25:33Z | [
"python",
"optimization"
] |
Python: Optimizing a tree evaluator | 2,169,682 | <p>I know tree is a well studied structure. </p>
<p>I'm writing a program that randomly generates many expression trees and then sorts and selects by a fitness attribute.</p>
<p>I have a class MakeTreeInOrder() that turns the tree into a string that 'eval' can evaluate.</p>
<p>but it gets called many many times, an... | 2 | 2010-01-30T22:21:06Z | 2,170,188 | <p>Adding a touch of object orientation here makes things simpler. Have subclasses of Node for each thing in your tree, and use an 'eval' method to evaluate them.</p>
<pre><code>import random
class ArithmeticOperatorNode(object):
def __init__(self, operator, *args):
self.operator = operator
self.c... | 3 | 2010-01-31T01:26:10Z | [
"python",
"optimization"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,850 | <p>Take a look at the <code>itertools</code> module, which provides a <code>product</code> member.</p>
<pre><code>L =[1,2,3]
import itertools
res = list(itertools.product(L,L))
print(res)
</code></pre>
<p>Gives:</p>
<pre><code>[(1,1),(1,2),(1,3),(2,1), .... and so on]
</code></pre>
| 7 | 2010-01-30T23:07:01Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,853 | <p>The <a href="http://docs.python.org/library/itertools.html" rel="nofollow"><code>itertools</code></a> module contains a number of helpful functions for this sort of thing. It looks like you may be looking for <a href="http://docs.python.org/library/itertools.html#itertools.product" rel="nofollow"><code>product</code... | 13 | 2010-01-30T23:07:27Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,854 | <p>You can do it with a list comprehension:</p>
<pre><code>[ (x,y) for x in L for y in L]
</code></pre>
<p><strong>edit</strong></p>
<p>You can also use itertools.product as others have suggested, but only if you are using 2.6 onwards. The list comprehension will work will all versions of Python from 2.0. If you d... | 22 | 2010-01-30T23:07:31Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,856 | <p>x = [a,b,c]
y = []
for item in x:
for item2 in x:
y.append((item, item2))</p>
<p>Maybe not the Pythonic way but working</p>
| -1 | 2010-01-30T23:07:59Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,863 | <p>Ok I tried :</p>
<p>L2 = [(x,y) for x in L for x in L] and this got L square.</p>
<p>Is this the best pythonic way to do this? I would expect L * L to work in python.</p>
| -1 | 2010-01-30T23:08:59Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,169,872 | <p>Two main alternatives:</p>
<pre><code>>>> L = ['a', 'b', 'c']
>>> import itertools
>>> list(itertools.product(L, L))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
>>> [(one, two) for one in L for two in L]
[('a', 'a')... | 3 | 2010-01-30T23:10:54Z | [
"python",
"list",
"cartesian-product"
] |
List multiplication | 2,169,838 | <p>I have a list L = [a, b, c] and I want to generate a list of tuples :</p>
<pre><code>[(a,a), (a,b), (a,c), (b,a), (b,b), (b,c)...]
</code></pre>
<p>I tried doing L * L but it didn't work. Can someone tell me how to get this in python.</p>
| 5 | 2010-01-30T23:03:37Z | 2,170,070 | <p>The most old fashioned way to do it would be:</p>
<pre><code>def perm(L):
result = []
for i in L:
for j in L:
result.append((i,j))
return result
</code></pre>
<p>This has a runtime of O(n^2) and is therefore quite slow, but you could consider it to be "vintage" style code.</p>
| 0 | 2010-01-31T00:23:31Z | [
"python",
"list",
"cartesian-product"
] |
OpenGL frame buffer slow and spontaneously stalls. Can even cause a system crash when used extensively | 2,169,868 | <p>Apparently frame buffers are fast and the best way to render offscreen to textures or to simply pre-create things.</p>
<p>My game however is not liking them at all. In the current code frame buffers are used often, sometimes each frame, several times. When used the game begins to slow down but not instantly. It see... | 1 | 2010-01-30T23:10:19Z | 2,170,039 | <p>I haven't worked with OpenGL for about 14 years so I'm not much help with that. I'm just looking at the Python code. There's a few things you can do to clean the code up, like use ".width" instead of ".surface_size[0]". You do have a get_width(), but descriptors are your friends. You also have checks for "if self.da... | 0 | 2010-01-31T00:13:37Z | [
"python",
"performance",
"opengl",
"framebuffer",
"fbo"
] |
Polymorphism - adding to existing methods while overwriting them | 2,169,947 | <p>I want to be able to subclass a class, and define <code>__init__</code> but still run the old <code>__init__</code> as well.</p>
<p>To illustrate, say I have the following classes:</p>
<pre><code>class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = ... | 2 | 2010-01-30T23:37:16Z | 2,169,955 | <p>Either call <code>a.__init__(self)</code> or derive <code>a</code> from <code>object</code> and use <a href="http://docs.python.org/library/functions.html#super" rel="nofollow"><code>super()</code></a>.</p>
| 0 | 2010-01-30T23:39:49Z | [
"python",
"methods",
"polymorphism"
] |
Polymorphism - adding to existing methods while overwriting them | 2,169,947 | <p>I want to be able to subclass a class, and define <code>__init__</code> but still run the old <code>__init__</code> as well.</p>
<p>To illustrate, say I have the following classes:</p>
<pre><code>class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = ... | 2 | 2010-01-30T23:37:16Z | 2,169,956 | <pre><code>class a:
def __init__(self):
self.var1 = 1
class b(a):
def __init__(self)
self.var2 = 2
a.__init__(self)
</code></pre>
<p>You can even write <code>super().__init__()</code> if you are using python 3.<br>
See <a href="http://stackoverflow.com/questions/576169/python-super">th... | 0 | 2010-01-30T23:40:09Z | [
"python",
"methods",
"polymorphism"
] |
Polymorphism - adding to existing methods while overwriting them | 2,169,947 | <p>I want to be able to subclass a class, and define <code>__init__</code> but still run the old <code>__init__</code> as well.</p>
<p>To illustrate, say I have the following classes:</p>
<pre><code>class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = ... | 2 | 2010-01-30T23:37:16Z | 2,169,962 | <p>Call your father's c'tor from within your c'tor: <code>a.__init__(self)</code>. Note that you need to pass <code>self</code> as first parameter. If the parent c'tor takes more parameters, pass them after <code>self</code>.</p>
| 0 | 2010-01-30T23:41:33Z | [
"python",
"methods",
"polymorphism"
] |
Polymorphism - adding to existing methods while overwriting them | 2,169,947 | <p>I want to be able to subclass a class, and define <code>__init__</code> but still run the old <code>__init__</code> as well.</p>
<p>To illustrate, say I have the following classes:</p>
<pre><code>class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = ... | 2 | 2010-01-30T23:37:16Z | 2,169,963 | <p>replace </p>
<pre><code>doInitForA()
</code></pre>
<p>with</p>
<pre><code>super(b, self).__init__()
</code></pre>
| 6 | 2010-01-30T23:42:19Z | [
"python",
"methods",
"polymorphism"
] |
Polymorphism - adding to existing methods while overwriting them | 2,169,947 | <p>I want to be able to subclass a class, and define <code>__init__</code> but still run the old <code>__init__</code> as well.</p>
<p>To illustrate, say I have the following classes:</p>
<pre><code>class A(object):
def __init__(self):
self.var1 = 1
class B(A):
def __init__(self)
self.var2 = ... | 2 | 2010-01-30T23:37:16Z | 2,169,964 | <p>You might want to look at this question: <a href="http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python">http://stackoverflow.com/questions/904036/chain-calling-parent-constructors-in-python</a>, specifically use the <code>super(b, self).__init__()</code> method.</p>
| 3 | 2010-01-30T23:42:21Z | [
"python",
"methods",
"polymorphism"
] |
Using code generated by Py++ as a Python extension | 2,169,948 | <p>I have a need to wrap an existing C++ library for use in Python. After reading through <a href="http://stackoverflow.com/questions/456884/extending-python-to-swig-or-not-to-swig/456949#456949">this answer on choosing an appropriate method to wrap C++ for use in Python</a>, I decided to go with Py++.</p>
<p>I walked... | 5 | 2010-01-30T23:37:52Z | 2,172,689 | <p>Py++ generates you syntax you use along with boost::python to generate python entry points in your app.
Assuming everything went well with Py++ you need to download the Boost framework, and add the boost include directory and the boost::python lib to your project then compile with the Py++ generated cpp.</p>
<p>You... | 6 | 2010-01-31T18:01:26Z | [
"c++",
"python",
"wrapping",
"py++"
] |
Using code generated by Py++ as a Python extension | 2,169,948 | <p>I have a need to wrap an existing C++ library for use in Python. After reading through <a href="http://stackoverflow.com/questions/456884/extending-python-to-swig-or-not-to-swig/456949#456949">this answer on choosing an appropriate method to wrap C++ for use in Python</a>, I decided to go with Py++.</p>
<p>I walked... | 5 | 2010-01-30T23:37:52Z | 2,174,394 | <p>The following answer was provided to me by <a href="http://mail.python.org/pipermail/cplusplus-sig/2010-January/015190.html" rel="nofollow">Roman Yakovenko on the Python C++-sig mailing list</a>; I'm posting it here, with minor edits, for the benefit of the Stack Overflow community.</p>
<p>I don't fully comprehend ... | 3 | 2010-02-01T02:55:45Z | [
"c++",
"python",
"wrapping",
"py++"
] |
Using code generated by Py++ as a Python extension | 2,169,948 | <p>I have a need to wrap an existing C++ library for use in Python. After reading through <a href="http://stackoverflow.com/questions/456884/extending-python-to-swig-or-not-to-swig/456949#456949">this answer on choosing an appropriate method to wrap C++ for use in Python</a>, I decided to go with Py++.</p>
<p>I walked... | 5 | 2010-01-30T23:37:52Z | 8,684,665 | <p>I wrote a small makefile with the following:</p>
<p>GNUmakefile:</p>
<pre><code>PYTHON_INC=$(shell python-config --includes)
PYTHON_LIBS=$(shell python-config --libs)
BOOST_LIBS=-lboost_python
all:
g++ -W -Wall $(PYTHON_INC) $(PYTHON_LIBS) $(BOOST_LIBS) -fPIC -shared generated.cpp -o hw.so
</code></pre>
<p>a... | 1 | 2011-12-30T22:26:06Z | [
"c++",
"python",
"wrapping",
"py++"
] |
Python2.6 and Snow Leopard. Problem installing appscript (and MANY other packages) | 2,169,987 | <p>I've been having nothing but trouble with python2.6 and Snow Leopard. One major problem is 32 vs 64-bit libraries.</p>
<p>The other manifests itself like this:</p>
<pre><code>tppllc-mbp15$ sudo easy_install-2.6 appscript
Searching for appscript
Reading http://pypi.python.org/simple/appscript/
Reading http://appscr... | 0 | 2010-01-30T23:53:03Z | 2,170,036 | <p>It appears you have likely installed a python 2.6 from python.org or some other 3rd-party installer. The python.org python's are currently built only as 32-bit (i386 and ppc) and are compatible with OS X 10.3 through 10.6. To do that, they are built with the 10.4u SDK which is available via the 10.6 Xcode installe... | 1 | 2010-01-31T00:12:37Z | [
"python",
"osx",
"osx-snow-leopard"
] |
Getting Django comments to be able to use bold and italic style | 2,170,111 | <p>So I'm trying to get rich text to work for the cheeserater program where I added a commenting system using the tinyMCE editor in the textarea. This is what it looks like:</p>
<pre><code><table><td align=left>
{% get_comment_list for package as comment_list %}
{% for comment in comment_list %}
{{ com... | 0 | 2010-01-31T00:48:38Z | 2,170,124 | <p>Those tags work fine here. Check that you don't have a stylesheet causing those tags to use an unadorned style.</p>
| 1 | 2010-01-31T00:53:57Z | [
"python",
"html",
"django",
"tinymce"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,170,243 | <p><code>model._meta.get_all_field_names()</code> will give you all the model's field names, then you can use <code>model._meta.get_field()</code> to work your way to the verbose name, and <code>getattr(model_instance, 'field_name')</code> to get the value from the model.</p>
<p>NOTE: <code>model._meta.get_all_field_n... | 115 | 2010-01-31T01:54:53Z | [
"python",
"django",
"django-templates"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,171,275 | <p>Yeah it's not pretty, you'll have to make your own wrapper. Take a look at builtin <a href="http://code.djangoproject.com/browser/django/trunk/django/contrib/databrowse/datastructures.py" rel="nofollow">databrowse</a> app, which has all the functionality you need really.</p>
| 3 | 2010-01-31T10:12:28Z | [
"python",
"django",
"django-templates"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,174,843 | <p>I've come up with the following method, which works for me because in every case the model will have a ModelForm associated with it.</p>
<pre><code>def GetModelData(form, fields):
"""
Extract data from the bound form model instance and return a
dictionary that is easily usable in templates with the actu... | 4 | 2010-02-01T05:32:45Z | [
"python",
"django",
"django-templates"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,226,150 | <p>Here's another approach using a model method. This version resolves picklist/choice fields, skips empty fields, and lets you exclude specific fields. </p>
<pre><code>def get_all_fields(self):
"""Returns a list of all field names on the instance."""
fields = []
for f in self._meta.fields:
fname ... | 11 | 2010-02-09T01:29:03Z | [
"python",
"django",
"django-templates"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,256,668 | <p>This may be considered a hack but I've done this before using modelform_factory to turn a model instance into a form.</p>
<p>The Form class has a lot more information inside that's super easy to iterate over and it will serve the same purpose at the expense of slightly more overhead. If your set sizes are relativel... | 3 | 2010-02-13T06:09:14Z | [
"python",
"django",
"django-templates"
] |
Iterate over model instance field names and values in template | 2,170,228 | <p>I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if specified on the field) in the first column and the value of that fie... | 123 | 2010-01-31T01:48:11Z | 2,311,015 | <p>You can use Django's <strong>to-python</strong> queryset serializer.</p>
<p>Just put the following code in your view:</p>
<pre><code>from django.core import serializers
data = serializers.serialize( "python", SomeModel.objects.all() )
</code></pre>
<p>And then in the template:</p>
<pre><code>{% for instance in d... | 52 | 2010-02-22T13:11:04Z | [
"python",
"django",
"django-templates"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.