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 |
|---|---|---|---|---|---|---|---|---|---|
Clojure prime numbers lazy sequence | 1,590,716 | <p>What is the Clojure equivalent (for the exact algorithm) of the following Python code?</p>
<pre><code>from itertools import count
from math import sqrt
def prime_gen():
primes = []
for n in count(2):
if all(n%p for p in primes if p <= sqrt(n)):
primes.append(n)
yield n
</code>... | 7 | 2009-10-19T19:46:12Z | 1,591,051 | <p>This is as Pythonish as I can make it:</p>
<pre><code>(def prime-gen
(let [primes (atom [])]
(for [n (iterate inc 2)
:when (not-any? #(zero? (rem n %))
(filter #(<= % (Math/sqrt n))
@primes))]
(do (swap! primes c... | 10 | 2009-10-19T20:46:22Z | [
"python",
"clojure"
] |
Clojure prime numbers lazy sequence | 1,590,716 | <p>What is the Clojure equivalent (for the exact algorithm) of the following Python code?</p>
<pre><code>from itertools import count
from math import sqrt
def prime_gen():
primes = []
for n in count(2):
if all(n%p for p in primes if p <= sqrt(n)):
primes.append(n)
yield n
</code>... | 7 | 2009-10-19T19:46:12Z | 37,084,208 | <p>This version is much faster than @Brian Carper's</p>
<pre><code>(def prime-gen
(let [primes (atom [2N])]
(iterate
#(let [ps @primes]
(loop [n (inc %)]
(if (loop [i 0]
(let [p (nth ps i)]
(cond
(< n (* p p)) true
... | 0 | 2016-05-07T03:31:31Z | [
"python",
"clojure"
] |
appengine, urlfetch, and the content-length header | 1,590,844 | <p>I have a Google Appengine app requesting pages from another server using urllib2 POSTs. I recently enabled gzip compression on the other server running Apache2, and the Appengine page requests started failing on key-error, indicating 'content-length' is not in the headers.</p>
<p>I am not explicitly declaring gzip... | 4 | 2009-10-19T20:14:20Z | 1,613,636 | <p>According to this thread:
<a href="http://groups.google.com/group/google-appengine-java/browse%5Fthread/thread/5c5f2a7e2d2beadc?pli=1" rel="nofollow">http://groups.google.com/group/google-appengine-java/browse%5Fthread/thread/5c5f2a7e2d2beadc?pli=1</a>)
on an Appengine Java newsgroup, Google does generally set the ... | 2 | 2009-10-23T13:57:08Z | [
"python",
"google-app-engine",
"post",
"apache2"
] |
Preserving whitespace with Pygments | 1,591,024 | <p>I'm currently writing an application that uses Pygments to perform syntax highlighting. The problem I'm having is any code I process with Pygments has the leading and trailing whitespace in the file removed, and a single line break added to the end. Is there a way to make Pygments preserve the whitespace?</p>
| 0 | 2009-10-19T20:41:36Z | 1,591,083 | <p>I think you want to make your own lexer, e.g.</p>
<pre><code>lexer = lexers.get_lexer_by_name("python", stripnl=False)
</code></pre>
<p>and explicitly pass it to <code>pygment.highlight</code>. See the lexers' reference <a href="http://pygments.org/docs/lexers/" rel="nofollow">here</a>.</p>
| 1 | 2009-10-19T20:50:51Z | [
"python"
] |
Combinations with replacements | 1,591,087 | <p>I know how to generate combinations of a set and that's a builtin in Python (what I use), anyway. But how to generate combinations with replacements?</p>
<p>Suppose I have a set with, say, two identical elements - for example, <strong>AABCDE</strong>.</p>
<p>Combinations of 3 items could be:</p>
<pre><code>"AAB"
... | 0 | 2009-10-19T20:51:12Z | 1,591,100 | <p>convert it to <code>set</code>, that's the easiest way to get rid of duplicates.</p>
| 2 | 2009-10-19T20:54:24Z | [
"python",
"combinations"
] |
Combinations with replacements | 1,591,087 | <p>I know how to generate combinations of a set and that's a builtin in Python (what I use), anyway. But how to generate combinations with replacements?</p>
<p>Suppose I have a set with, say, two identical elements - for example, <strong>AABCDE</strong>.</p>
<p>Combinations of 3 items could be:</p>
<pre><code>"AAB"
... | 0 | 2009-10-19T20:51:12Z | 1,591,147 | <pre><code>>>> import itertools
>>> ["".join(x) for x in (itertools.combinations(set("AABCDE"),3))]
['ACB', 'ACE', 'ACD', 'ABE', 'ABD', 'AED', 'CBE', 'CBD', 'CED', 'BED']
>>>
</code></pre>
<p>From your other comments, I think I misunderstood what you are asking.</p>
<pre><code>>>> ... | 2 | 2009-10-19T21:03:29Z | [
"python",
"combinations"
] |
Combinations with replacements | 1,591,087 | <p>I know how to generate combinations of a set and that's a builtin in Python (what I use), anyway. But how to generate combinations with replacements?</p>
<p>Suppose I have a set with, say, two identical elements - for example, <strong>AABCDE</strong>.</p>
<p>Combinations of 3 items could be:</p>
<pre><code>"AAB"
... | 0 | 2009-10-19T20:51:12Z | 30,520,169 | <pre><code>def stepper_w_w(l,stop):#stepper_with_while
"""l is a list of any size usually you would input [1,1,1,1...],
stop is the highest number you want to stop at so if you put in stop=5
the sequence would stop at [5,5,5,5...]
This stepper shows the first number that equals the last.
This generates combinations wit... | 0 | 2015-05-29T02:31:17Z | [
"python",
"combinations"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,591,337 | <p>You should only catch exceptions that you will handle.</p>
<p>Catching all exceptions by their concrete types is nonsense. You should catch specific exceptions you <em>can</em> and <em>will</em> handle. For other exceptions, you may write a generic catch that catches "base Exception", logs it (use <code>str()</co... | 23 | 2009-10-19T21:51:39Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,591,363 | <p>normally, you'd need to catch exception only around a few lines of code. You wouldn't want to put your whole <code>main</code> function into the <code>try except</code> clause. for every few line you always should now (or be able easily to check) what kind of exception might be raised.</p>
<p>docs have an exhaustiv... | 1 | 2009-10-19T21:56:17Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,591,368 | <p>The correct tool to solve this problem is unittests. If you are having exceptions raised by real code that the unittests do not raise, then you need more unittests.</p>
<p>Consider this</p>
<pre><code>def f(duck):
try:
duck.quack()
except ??? could be anything
</code></pre>
<p>duck can be any obje... | 8 | 2009-10-19T21:56:46Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,591,432 | <p>I guess a solution could be only imprecise because of lack of static typing rules.</p>
<p>I'm not aware of some tool that checks exceptions, but you could come up with your own tool matching your needs (a good chance to play a little with static analysis).</p>
<p>As a first attempt, you could write a function that... | 14 | 2009-10-19T22:15:17Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,591,860 | <p>I ran into this when using socket, I wanted to find out all the error conditions I would run in to (so rather than trying to create errors and figure out what socket does I just wanted a concise list). Ultimately I ended up grep'ing "/usr/lib64/python2.4/test/test_socket.py" for "raise":</p>
<pre><code>$ grep raise... | 3 | 2009-10-20T00:26:38Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 1,891,657 | <p>Noone explained so far, why you can't have a full, 100% correct list of exceptions, so I thought it's worth commenting on. One of the reasons is a first-class function. Let's say that you have a function like this:</p>
<pre><code>def apl(f,arg):
return f(arg)
</code></pre>
<p>Now <code>apl</code> can raise any ... | 2 | 2009-12-12T00:02:31Z | [
"python",
"exception"
] |
Python: How can I know which exceptions might be thrown from a method call | 1,591,319 | <p>Is there a way knowing (at coding time) which exceptions to expect when executing python code?
I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown(and don't tell me to read the documentation. many times an exception can be propagated from the deep. and ... | 45 | 2009-10-19T21:43:36Z | 22,664,826 | <p>There are two ways that I found informative. The first one, run the instructions in iPython, which will display the exception type.</p>
<pre><code>n = 2
str = 'me '
str + 2
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</code></pre>
<p>In the second way we settle for catching too much and improving... | 0 | 2014-03-26T14:56:09Z | [
"python",
"exception"
] |
Cron job to connect to sql server and run a sproc using python | 1,591,477 | <p>I want to learn python, and my task is the run a sql server 2008 stored procedure via a cron job.</p>
<p>Can someone step through a script for me in python?</p>
| 0 | 2009-10-19T22:25:44Z | 1,591,533 | <p>I am assuming you mean <strong>Microsoft's</strong> SQL server...</p>
<pre><code>#! /usr/bin/python
import pymssql
con = pymssql.connect (host='xxxxx',user='xxxx',
password='xxxxx',database='xxxxx')
cur = con.cursor()
query = "DECLARE @id INT; EXECUTE sp_GetUserID; SELECT @id;"
cur.execute(q... | 1 | 2009-10-19T22:39:19Z | [
"python"
] |
From PHP workers to Python threads | 1,591,555 | <p>Right now I'm running 50 PHP (in CLI mode) individual workers (processes) per machine that are waiting to receive their workload (job). For example, the job of resizing an image. In workload they receive the image (binary data) and the desired size. The worker does it's work and returns the resized image back. Then ... | 3 | 2009-10-19T22:47:00Z | 1,591,593 | <p>If you are on a sane operating system then shared libraries should only be loaded once and shared among all processes using them. Memory for data structures and connection handles will obviously be duplicated, but the overhead of stopping and starting the systems may be greater than keeping things up while idle. I... | 1 | 2009-10-19T22:56:37Z | [
"php",
"python",
"multithreading"
] |
From PHP workers to Python threads | 1,591,555 | <p>Right now I'm running 50 PHP (in CLI mode) individual workers (processes) per machine that are waiting to receive their workload (job). For example, the job of resizing an image. In workload they receive the image (binary data) and the desired size. The worker does it's work and returns the resized image back. Then ... | 3 | 2009-10-19T22:47:00Z | 1,591,603 | <p>Linux has shared libraries, so those 50 php processes use mostly the same libraries.
You don't sound like you even have a problem at all.</p>
<p><em>"this does not sound very effective."</em> is not a problem description, if anything those words are a problem on their own. Writing code needs a <em>real reason</em>... | 4 | 2009-10-19T22:59:44Z | [
"php",
"python",
"multithreading"
] |
From PHP workers to Python threads | 1,591,555 | <p>Right now I'm running 50 PHP (in CLI mode) individual workers (processes) per machine that are waiting to receive their workload (job). For example, the job of resizing an image. In workload they receive the image (binary data) and the desired size. The worker does it's work and returns the resized image back. Then ... | 3 | 2009-10-19T22:47:00Z | 1,591,616 | <p>Most probably - yes. But don't assume you have to do multithreading. Have a look at the multiprocessing module. It already has an implementation of a Pool included, which is what you could use. And it basically solves the GIL problem (multithreading can run only 1 "standard python code" at any time - that's a very s... | 4 | 2009-10-19T23:04:14Z | [
"php",
"python",
"multithreading"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,600 | <p>What you really want to do is use an XML parser and append the new elements with the API provided.</p>
<p>Then simply overwrite the file.</p>
<p>The easiest to use would probably be a DOM parser like the one below:</p>
<p><a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow">http://docs.pyt... | 1 | 2009-10-19T22:58:47Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,604 | <p>You should read the XML file using specific XML modules. That way you can edit the XML document in memory and rewrite your changed XML document into the file.</p>
<p>Here is a quick start: <a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow">http://docs.python.org/library/xml.dom.minidom.htm... | 0 | 2009-10-19T23:00:33Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,617 | <p>While I agree with Tim and Oben Sonne that you should use an XML library, there are ways to still manipulate it as a simple string object. </p>
<p>I likely would not try to use a single file pointer for what you are describing, and instead read the file into memory, edit it, then write it out.:</p>
<pre><code>inF... | 3 | 2009-10-19T23:04:14Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,629 | <p>The quick and easy way, which you definitely <strong>should not do</strong> (see below), is to read the whole file into a list of strings using readlines(). I write this in case the quick and easy solution is what you're looking for.</p>
<p>Just open the file using open(), then call the readlines() method. What you... | 2 | 2009-10-19T23:06:21Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,655 | <p>Useful Python XML parsers:</p>
<ol>
<li><a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow">Minidom</a> - functional but limited</li>
<li><a href="http://effbot.org/zone/element-index.htm" rel="nofollow">ElementTree</a> - decent performance, more functionality</li>
<li><a href="http://lxml.... | 12 | 2009-10-19T23:12:10Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 1,591,732 | <p>To make this process more robust, you could consider using the SAX parser (that way you don't have to hold the whole file in memory), read & write till the end of tree and then start appending.</p>
| 1 | 2009-10-19T23:38:53Z | [
"python",
"xml",
"io"
] |
How to update/modify a XML file in python? | 1,591,579 | <p>I have a xml file that I would like to update after it has already data.</p>
<p>I thought about open the xml file in "a" (append) mode. The problem is that the new data will start to be dumped after the "root" tag has been closed.</p>
<p>What I wanted someone to be kind enough to tell me is how can I delete the la... | 8 | 2009-10-19T22:52:30Z | 27,934,162 | <p>Using <code>ElementTree</code>:</p>
<pre><code>import xml.etree.ElementTree
# Open original file
et = xml.etree.ElementTree.parse('file.xml')
# Append new tag: <a x='1' y='abc'>body text</a>
new_tag = xml.etree.ElementTree.SubElement(et.getroot(), 'a')
new_tag.text = 'body text'
new_tag.attrib['x'] = ... | 16 | 2015-01-14T00:52:58Z | [
"python",
"xml",
"io"
] |
How to make all combinations of the elements in an array? | 1,591,762 | <p>I have a list. It contains x lists, each with y elements.
I want to pair each element with all the other elements, just once, (a,b = b,a)</p>
<p>EDIT: this has been criticized as being too vague.So I'll describe the history.
My function produces random equations and using genetic techniques, mutates and crossbreeds... | 2 | 2009-10-19T23:51:01Z | 1,591,774 | <p><code>itertools.product</code> is your friend.</p>
<p>about removing the duplicates, try with a set of sets.</p>
<p>Now it's a little bit clearer what you want:</p>
<pre><code>import itertools
def recombinate(families):
"families is the list of 8 elements, each one with 12 individuals"
for fi, fj in iter... | 7 | 2009-10-19T23:56:35Z | [
"python"
] |
How to make all combinations of the elements in an array? | 1,591,762 | <p>I have a list. It contains x lists, each with y elements.
I want to pair each element with all the other elements, just once, (a,b = b,a)</p>
<p>EDIT: this has been criticized as being too vague.So I'll describe the history.
My function produces random equations and using genetic techniques, mutates and crossbreeds... | 2 | 2009-10-19T23:51:01Z | 1,591,802 | <p>First of all, please don't refer to this as an "array". You are using a list of lists. In Python, an array is a different type of data structure, provided by the array module. </p>
<p>Also, your application sounds suspiciously like a matrix. If you are really doing matrix manipulations, you should investigate the N... | 0 | 2009-10-20T00:10:18Z | [
"python"
] |
How to make all combinations of the elements in an array? | 1,591,762 | <p>I have a list. It contains x lists, each with y elements.
I want to pair each element with all the other elements, just once, (a,b = b,a)</p>
<p>EDIT: this has been criticized as being too vague.So I'll describe the history.
My function produces random equations and using genetic techniques, mutates and crossbreeds... | 2 | 2009-10-19T23:51:01Z | 1,592,512 | <p>You haven't made it completely clear what you need. It sounds like itertools should have what you need. Perhaps what you wish is an itertools.combinations of the itertools.product of the lists in your big list. </p>
<p>@fortran: you can't have a set of sets. You can have a set of frozensets, but depending on what i... | 1 | 2009-10-20T04:48:39Z | [
"python"
] |
Twisted: tcp server with push producer example? | 1,591,787 | <p>I want to put together simple TCP server using Python and Twisted.</p>
<p>The server starts up and waits for connection - I already have client - non-python application. Once connection is made server starts sending data at some interval (e.g. 1 sec).</p>
<p>The server reads data from a static file (a record at a ... | 6 | 2009-10-20T00:05:26Z | 1,592,434 | <p>What about something simplistic like:</p>
<pre><code>thedata = '''
Questa mattina
mi son svegliato
o bella ciao, bella ciao,
bella ciao, ciao, ciao
questa mattina
mi son svegliato
ho trovato l'invasor!
'''.splitlines(True)
class Push(protocol.Protocol):
"""This is just about the simplest possible protocol"""
... | 1 | 2009-10-20T04:26:40Z | [
"python",
"twisted",
"tcpserver"
] |
Twisted: tcp server with push producer example? | 1,591,787 | <p>I want to put together simple TCP server using Python and Twisted.</p>
<p>The server starts up and waits for connection - I already have client - non-python application. Once connection is made server starts sending data at some interval (e.g. 1 sec).</p>
<p>The server reads data from a static file (a record at a ... | 6 | 2009-10-20T00:05:26Z | 3,088,794 | <p><a href="http://twistedmatrix.com/pipermail/twisted-python/2009-December/021183.html" rel="nofollow">Here</a> is a complete example of a push producer. It's been added to the twisted svn as an example.</p>
| 4 | 2010-06-21T21:57:50Z | [
"python",
"twisted",
"tcpserver"
] |
os.popen subprocess conversion | 1,591,798 | <p>This snippet gets me the dotted quad of my BSD network interface.
I would like to figure out how to use the subprocess module instead.</p>
<pre><code>ifcfg_lines = os.popen("/sbin/ifconfig fxp0").readlines()
x = string.split(ifcfg_lines[3])[1]
</code></pre>
<p>Seems as if I can't use subprocess in exactly the same... | 0 | 2009-10-20T00:08:30Z | 1,591,805 | <p>Why not use something like:</p>
<pre><code>import socket
ipadr = socket.gethostbyname(socket.gethostname())
</code></pre>
<p>as in <a href="http://stackoverflow.com/questions/166506/finding-local-ip-addresses-in-python">Finding Local IP Addresses in Python</a> and remove the subprocess dependency? There are also s... | 0 | 2009-10-20T00:12:04Z | [
"python",
"string",
"indexing",
"subprocess"
] |
os.popen subprocess conversion | 1,591,798 | <p>This snippet gets me the dotted quad of my BSD network interface.
I would like to figure out how to use the subprocess module instead.</p>
<pre><code>ifcfg_lines = os.popen("/sbin/ifconfig fxp0").readlines()
x = string.split(ifcfg_lines[3])[1]
</code></pre>
<p>Seems as if I can't use subprocess in exactly the same... | 0 | 2009-10-20T00:08:30Z | 1,591,815 | <pre><code>from subprocess import Popen, PIPE
ifcfg_lines = Popen("/sbin/ifconfig fxp0",shell=True,stdout=PIPE).stdout.readlines()
x = string.split(ifcfg_lines[3])[1]
</code></pre>
<p>For a little more elegance, hide the details:</p>
<pre><code>def getBSDIP():
from subprocess import Popen, PIPE
import string
... | 1 | 2009-10-20T00:14:42Z | [
"python",
"string",
"indexing",
"subprocess"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,592,273 | <p>I could be misunderstanding, but you appear to be concerned about the presentation and seem to want to get beyond a 2d table with just one axis per variable. As you point out, things get difficult with more variables. A spreadsheet is inherently 2d and the best you'll get with graphing is 3d. I'd bet you have mor... | 4 | 2009-10-20T03:19:07Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,593,240 | <p>A good framework to automate testing is nose: <a href="http://somethingaboutorange.com/mrl/projects/nose/0.11.1/" rel="nofollow">http://somethingaboutorange.com/mrl/projects/nose/0.11.1/</a></p>
<p>It recognizes any function with the word 'tests_' in its name as a function. </p>
| -1 | 2009-10-20T08:45:00Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,629,112 | <p>If the number of variables or the number of possible values for each variable is even moderately large, the set of tests can quickly become unwieldy. In that case you probably want to consider reasonable coverage with a subset of the values. An example tool for generating a candidate set of tests is <a href="http:... | 2 | 2009-10-27T06:56:56Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,636,568 | <p>I think <a href="http://www.pytables.org/moin" rel="nofollow">PyTables</a> will do what you are looking for, and more. It lets you make HDF5 tables/files. Guessing from <a href="http://www.pytables.org/docs/manual/ch03.html#id323222" rel="nofollow">the examples in the documentation</a>, it was originally built for... | 1 | 2009-10-28T11:04:46Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,637,746 | <p>If you are only concerned with display, you could write an Excel file with <a href="http://pypi.python.org/pypi/xlwt" rel="nofollow">xlwt</a>. (<a href="http://panela.blog-city.com/pyexcelerator%5Fxlwt%5Fcheatsheet%5Fcreate%5Fnative%5Fexcel%5Ffrom%5Fpu.htm" rel="nofollow">cheatsheet</a>).</p>
<p>It can write xls f... | 1 | 2009-10-28T14:47:30Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,652,191 | <p>I believe Matlab to be perfect for generating and manipulating such tables. It treats the universe as vectors and matrices, and it's very high-level.<br />
Downsides:<br />
It's very expensive<br />
If you plan to automate the running of tests, integrating Matlab to what your test programs are running at might be ha... | 1 | 2009-10-30T20:40:49Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,653,264 | <p>I'm a little confused about exactly what you want to generate. Are you trying to just format tables, or actually generate the combinations of the values? In that case you'll want to look at design-of-experiment or test matrix generation tools like:</p>
<ul>
<li><a href="http://sourceforge.net/apps/trac/allpairs/" r... | 1 | 2009-10-31T03:02:49Z | [
"java",
"c++",
"python",
"research"
] |
Existence of a table generation framework for experiments | 1,591,864 | <p>I am running experiments on computer programs with a number of variables whose effect I am measuring. For each run, I record the time, the precision and the recall. </p>
<p>Are there any frameworks that exist that can help me with <strong>running tests and generating tables?</strong> I am currently using the Python... | 0 | 2009-10-20T00:27:39Z | 1,654,603 | <p>Some things that I suggest that you look at are <a href="http://pyepl.sourceforge.net/" rel="nofollow">PyEPL</a>, the Python Experiment-Programming Library geared for psychology experiments, and this paper on <a href="http://gael-varoquaux.info/computers/agile%5Fcomputer%5Fcontrol%5Fof%5Fan%5Fexperiment.pdf" rel="no... | 1 | 2009-10-31T14:49:11Z | [
"java",
"c++",
"python",
"research"
] |
How do I go about setting up a TDD development process with Google App Engine? | 1,591,875 | <p>I'm primarily a Ruby guy, but lately I've been working on a lot of Python stuff, in particular, App Engine code. In Ruby, I'd use automated continuous integration (<a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autotest</a>), code coverage tools (<a href="http://github.com/relevance/rcov">rcov</a>), stati... | 15 | 2009-10-20T00:31:05Z | 1,592,003 | <p>You won't always find one to one equivalents of Ruby testing tools in Python, but there are some great testing tools in Python. Some of the tools that I've found useful include:</p>
<ul>
<li><a href="http://docs.python.org/library/unittest.html">unittest</a> - the xUnit tool included in the Python standard library... | 26 | 2009-10-20T01:25:29Z | [
"python",
"unit-testing",
"google-app-engine",
"tdd"
] |
How do I go about setting up a TDD development process with Google App Engine? | 1,591,875 | <p>I'm primarily a Ruby guy, but lately I've been working on a lot of Python stuff, in particular, App Engine code. In Ruby, I'd use automated continuous integration (<a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autotest</a>), code coverage tools (<a href="http://github.com/relevance/rcov">rcov</a>), stati... | 15 | 2009-10-20T00:31:05Z | 1,592,011 | <p>Haven't used App Engine, but my feeling for the most popular python testing tools is</p>
<ul>
<li><a href="http://docs.python.org/library/development.html" rel="nofollow">unittest/doctest</a> are the testing packages from the Python standard
library. unittest is the xUnit for python.</li>
<li><a href="http://somet... | 3 | 2009-10-20T01:27:56Z | [
"python",
"unit-testing",
"google-app-engine",
"tdd"
] |
How do I go about setting up a TDD development process with Google App Engine? | 1,591,875 | <p>I'm primarily a Ruby guy, but lately I've been working on a lot of Python stuff, in particular, App Engine code. In Ruby, I'd use automated continuous integration (<a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autotest</a>), code coverage tools (<a href="http://github.com/relevance/rcov">rcov</a>), stati... | 15 | 2009-10-20T00:31:05Z | 1,592,108 | <p>On my GAE project, I use:</p>
<ul>
<li><a href="http://code.google.com/p/nose-gae/">NoseGAE</a>—This is the critical piece that ties all the rest together</li>
<li>Mock, as in John's excellent answer. I use this largely for AWS and other web services</li>
<li>Fixtures (the package, not the idea)</li>
</ul>
<... | 15 | 2009-10-20T02:07:44Z | [
"python",
"unit-testing",
"google-app-engine",
"tdd"
] |
How do I go about setting up a TDD development process with Google App Engine? | 1,591,875 | <p>I'm primarily a Ruby guy, but lately I've been working on a lot of Python stuff, in particular, App Engine code. In Ruby, I'd use automated continuous integration (<a href="http://www.zenspider.com/ZSS/Products/ZenTest/">autotest</a>), code coverage tools (<a href="http://github.com/relevance/rcov">rcov</a>), stati... | 15 | 2009-10-20T00:31:05Z | 8,238,153 | <p>It looks like another new option is <a href="http://pypi.python.org/pypi/agar" rel="nofollow">Agar</a>, which is based on <a href="http://code.google.com/appengine/docs/python/tools/localunittesting.html" rel="nofollow">google.appengine.ext.testbed</a>, part of the App Engine SDK. See <a href="http://www.recursion.o... | 0 | 2011-11-23T06:32:54Z | [
"python",
"unit-testing",
"google-app-engine",
"tdd"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,591,926 | <p>Take a look at the <a href="http://docs.python.org/library/struct.html?#struct.unpack" rel="nofollow">struct.unpack</a> in the struct module.</p>
| 2 | 2009-10-20T00:54:39Z | [
"python",
"binary-data"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,591,935 | <p>Use pack/unpack functions from "struct" package. More info here <a href="http://docs.python.org/library/struct.html" rel="nofollow">http://docs.python.org/library/struct.html</a></p>
<p>Bye!</p>
| 1 | 2009-10-20T00:57:16Z | [
"python",
"binary-data"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,592,172 | <blockquote>
<pre><code>>>> struct.unpack('ih', response.read(6))
(16777216, 1024)
</code></pre>
</blockquote>
<p>You are unpacking big-endian data on a little-endian machine. Try this instead:</p>
<pre><code>>>> struct.unpack('!IH', response.read(6))
(1L, 4)
</code></pre>
<p>This tells unpack to c... | 5 | 2009-10-20T02:34:44Z | [
"python",
"binary-data"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,592,173 | <p>As it was already mentioned, <code>struct</code> is the module you need to use.</p>
<p>Please read its documentation to learn about byte ordering, etc.</p>
<p>In your example you need to do the following (as your data is big-endian and unsigned):</p>
<pre><code>>>> import struct
>>> x = '\x00\x0... | 0 | 2009-10-20T02:34:52Z | [
"python",
"binary-data"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,592,340 | <pre><code>>>> data
'\x00\x00\x00\x01\x00\x04AMTD\x00\x00\x00\x00\x02A\x9733A\x99\\)A\x90=qA\x91\xd7\nG\x0f\xc6\x14\x00\x00\x01\x16j\xe0h\x80A\x93\xb4\x05A\x97\x1e\xb8A\x90z\xe1A\x96\x8fWF\xe6.\x80\x00\x00\x01\x16zS|\x80\xff\xff'
>>> from struct import unpack, calcsize
>>> scount, slength = u... | 6 | 2009-10-20T03:51:26Z | [
"python",
"binary-data"
] |
Python binary data reading | 1,591,920 | <p>A urllib2 request receives binary response as below:</p>
<pre><code>00 00 00 01 00 04 41 4D 54 44 00 00 00 00 02 41
97 33 33 41 99 5C 29 41 90 3D 71 41 91 D7 0A 47
0F C6 14 00 00 01 16 6A E0 68 80 41 93 B4 05 41
97 1E B8 41 90 7A E1 41 96 8F 57 46 E6 2E 80 00
00 01 16 7A 53 7C 80 FF FF
</code></pre>
<p>Its structu... | 3 | 2009-10-20T00:51:46Z | 1,592,361 | <p>So here's my best shot at interpreting the data you're giving...:</p>
<pre><code>import datetime
import struct
class Printable(object):
specials = ()
def __str__(self):
resultlines = []
for pair in self.__dict__.items():
if pair[0] in self.specials: continue
resultlines.append('%10s %s' % p... | 10 | 2009-10-20T04:00:48Z | [
"python",
"binary-data"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 1,592,189 | <p>You should check out <a href="http://gaelyk.appspot.com/" rel="nofollow">gaelyk</a>. It's a lightweight framework on top of appengine that uses groovy.</p>
| 0 | 2009-10-20T02:43:41Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 1,592,249 | <p>No such framework has been released at this time, to the best of my knowledge (most people appear to be quite happy with Django I guess;-). You could try using Juno with <a href="http://gist.github.com/140565" rel="nofollow">this patch</a> -- it doesn't seem to be quite ready for prime time, but then again, it IS a... | 2 | 2009-10-20T03:08:36Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 1,592,866 | <p>Another framework that I've been meaning to try out is <a href="http://bloog.billkatz.com/" rel="nofollow">Bloog</a>. It is actually a blog engine for GAE but also provides a framework for developing other GAE apps.</p>
| 1 | 2009-10-20T06:50:36Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 1,593,340 | <p>I use <a href="http://webpy.org/" rel="nofollow">web.py</a>. It's really simple and doesn't get in your way.</p>
<p>This is how it looks:</p>
<pre><code>import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
... | 2 | 2009-10-20T09:04:04Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 1,593,411 | <p>There are several frameworks either specifically for App Engine, or well suited to it:</p>
<ul>
<li><a href="http://webpy.org/">web.py</a> - Not specifically for App Engine, but well suited.</li>
<li><a href="http://code.google.com/p/google-app-engine-oil/">Google App Engine Oil</a></li>
<li><a href="http://code.go... | 7 | 2009-10-20T09:21:32Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
A simple framework for Google App Engine (like Sinatra)? | 1,592,088 | <p>Is there a simple 'wrapper' framework for appengine? Something like <a href="http://www.sinatrarb.com/" rel="nofollow">Sinatra</a> or <a href="http://github.com/breily/juno" rel="nofollow">Juno</a>? So that one can write code like the following:</p>
<pre><code>from juno import *
@route('/')
def index(web):
ret... | 5 | 2009-10-20T01:59:57Z | 4,366,792 | <p><a href="http://bottle.paws.de/docs/dev/index.html" rel="nofollow">Bottle</a> is one-single-file framework, so it's very easy to deploy it on GAE.</p>
<p>Bottle is similar with Sinatra, see the "hello world" example below:</p>
<p>Sinatra:</p>
<pre><code>require 'sinatra'
get '/hi' do
"Hello World!"
end
</code><... | 1 | 2010-12-06T13:22:40Z | [
"python",
"google-app-engine",
"web-frameworks"
] |
Subclass lookup | 1,592,089 | <p>(I'm developing in Python 3.1, so if there's some shiny new 3.x feature I should know about for this, please let me know!)</p>
<p>I've got a class (we'll just call it "Packet") that serves as the parent for a bunch of child classes representing each of a few dozen packet types in a legacy client-server protocol ove... | 0 | 2009-10-20T02:00:03Z | 1,592,110 | <p>"I'd like the table built at runtime by examining all of the subclasses of Packet," </p>
<p>This is guaranteed to cause endless problems. This kind of thing puts a strange constraint on your subclasses. You can't use any abstract superclasses to simplify things.</p>
<p>Here's a specific example of what won't wor... | 3 | 2009-10-20T02:08:05Z | [
"python",
"python-3.x"
] |
Subclass lookup | 1,592,089 | <p>(I'm developing in Python 3.1, so if there's some shiny new 3.x feature I should know about for this, please let me know!)</p>
<p>I've got a class (we'll just call it "Packet") that serves as the parent for a bunch of child classes representing each of a few dozen packet types in a legacy client-server protocol ove... | 0 | 2009-10-20T02:00:03Z | 1,592,119 | <p>I guess you can use Python metaclasses (Python ⥠2.2) to share such an information between classes, that would be quite pythonic. Take a look at the implementation of the Google's <a href="http://code.google.com/p/protobuf/" rel="nofollow">Protocol Buffers</a>. Here is <a href="http://code.google.com/apis/protocol... | 1 | 2009-10-20T02:11:51Z | [
"python",
"python-3.x"
] |
Subclass lookup | 1,592,089 | <p>(I'm developing in Python 3.1, so if there's some shiny new 3.x feature I should know about for this, please let me know!)</p>
<p>I've got a class (we'll just call it "Packet") that serves as the parent for a bunch of child classes representing each of a few dozen packet types in a legacy client-server protocol ove... | 0 | 2009-10-20T02:00:03Z | 1,592,235 | <pre><code>
>>> class PacketMeta(type):
... def __init__(self,*args,**kw):
... if self.type_id is not None:
... self.subclasses[self.type_id]=self
... return type.__init__(self,*args,**kw)
...
>>> class Packet(object):
... __metaclass__=PacketMeta
... subclasses={}
... type_... | 2 | 2009-10-20T03:01:33Z | [
"python",
"python-3.x"
] |
Subclass lookup | 1,592,089 | <p>(I'm developing in Python 3.1, so if there's some shiny new 3.x feature I should know about for this, please let me know!)</p>
<p>I've got a class (we'll just call it "Packet") that serves as the parent for a bunch of child classes representing each of a few dozen packet types in a legacy client-server protocol ove... | 0 | 2009-10-20T02:00:03Z | 1,592,240 | <p><code>__subclasses__</code> IS part of the Python language, and implemented by IronPython and Jython as well as CPython (no pypy at hand to test, right now, but I'd be astonished if they have broken that!-). Whatever gave you the impression that <code>__subclasses__</code> was at all problematic?! I see a comment ... | 3 | 2009-10-20T03:02:46Z | [
"python",
"python-3.x"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 1,592,179 | <p>I'm guessing this question relates to <a href="http://stackoverflow.com/questions/1591920/python-binary-data-reading">this one</a> and you are working with 4 bytes rather than 8 hex digits.</p>
<p><code>"\x41\x91\x33\x33"</code> is a 4 byte string even though it looks like 16</p>
<pre><code>>>> len("\x41\... | 5 | 2009-10-20T02:37:28Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 1,592,200 | <p>I recommend using <a href="http://docs.python.org/library/ctypes.html">the ctypes module</a> which basically lets you work with low level data types. In your case you could say</p>
<pre><code>from ctypes import *
def convert(s):
i = int(s, 16) # convert from hex to a Python int
cp = poin... | 10 | 2009-10-20T02:49:58Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 1,592,226 | <p>Slice up the hex strings into 2-character chunks (bytes), make each chunk into the right byte with int formatting, struct.unpack when done. I.e.:</p>
<pre><code>import struct
testcases = {
"41973333": 1.88999996185302734375E1,
"41995C29": 1.91700000762939453125E1,
"470FC614": 3.6806078125E4,
}
def hex2float(s):... | 4 | 2009-10-20T02:58:23Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 1,592,362 | <pre><code>>>> import struct
>>> struct.unpack('!f', '41973333'.decode('hex'))[0]
18.899999618530273
>>> struct.unpack('!f', '41995C29'.decode('hex'))[0]
19.170000076293945
>>> struct.unpack('!f', '470FC614'.decode('hex'))[0]
36806.078125
</code></pre>
<p>Update: see comment on how ... | 36 | 2009-10-20T04:00:51Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 21,617,990 | <p>Gentelmen... Behold:</p>
<pre><code> class fl:
def __init__(this, value=0, byte_size=4):
this.value = value
if this.value: # speedy check (before performing any calculations)
Fe=((byte_size*8)-1)//(byte_size+1)+(byte_size>2)*byte_size//2+(byte_size==3)
... | 0 | 2014-02-07T02:10:26Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Convert hex to float | 1,592,158 | <p>How to convert the following hex string to float (single precision 32-bit) in Python?</p>
<pre><code>"41973333" -> 1.88999996185302734375E1
"41995C29" -> 1.91700000762939453125E1
"470FC614" -> 3.6806078125E4
</code></pre>
| 20 | 2009-10-20T02:32:02Z | 32,684,085 | <p>The ctypes approach does not work when the hex string contains leading zeroes. Don't use it.</p>
| -1 | 2015-09-20T20:39:55Z | [
"python",
"binary",
"floating-point",
"hex",
"decimal"
] |
Rendering different part of templates according to the request values in Django | 1,592,331 | <p>In my view to render my template I receive different parameters through my request.</p>
<p>According to these parameters I need to render different "part" in my templates.</p>
<p>For example let say that if I receive in my request</p>
<pre><code>to_render = ["table", "bar_chart"]
</code></pre>
<p>I want to rende... | 0 | 2009-10-20T03:48:34Z | 1,592,367 | <p>just manage it in your template</p>
| 2 | 2009-10-20T04:02:40Z | [
"python",
"django",
"django-templates",
"django-views"
] |
Rendering different part of templates according to the request values in Django | 1,592,331 | <p>In my view to render my template I receive different parameters through my request.</p>
<p>According to these parameters I need to render different "part" in my templates.</p>
<p>For example let say that if I receive in my request</p>
<pre><code>to_render = ["table", "bar_chart"]
</code></pre>
<p>I want to rende... | 0 | 2009-10-20T03:48:34Z | 1,592,454 | <p>Sure, you can manage this in your view. The <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#ref-templates-api" rel="nofollow">template API</a> shows clearly how to use the templating system in Python.</p>
| 1 | 2009-10-20T04:32:02Z | [
"python",
"django",
"django-templates",
"django-views"
] |
Compilers targeting .pyc files? | 1,592,347 | <p>Out of curiosity, are there many compilers out there which target <code>.pyc</code> files?</p>
<p>After a bit of Googling, the only two I can find are:</p>
<ul>
<li><a href="http://github.com/whymirror/unholy" rel="nofollow">unholy</a>: why_'s Ruby-to-pyc compiler</li>
<li><a href="http://python.org/" rel="nofollo... | 4 | 2009-10-20T03:53:17Z | 1,592,404 | <p>"I want to write a Scheme-to-pyc compiler".</p>
<p>My brain hurts! Why would you want to do that? Python byte code is an intermediate language specifically designed to meet the needs of the Python language and designed to run on Python virtual machines that, again, have been tailored to the needs of Python. Some... | 4 | 2009-10-20T04:18:00Z | [
"python",
"compiler-construction",
"pyc"
] |
Compilers targeting .pyc files? | 1,592,347 | <p>Out of curiosity, are there many compilers out there which target <code>.pyc</code> files?</p>
<p>After a bit of Googling, the only two I can find are:</p>
<ul>
<li><a href="http://github.com/whymirror/unholy" rel="nofollow">unholy</a>: why_'s Ruby-to-pyc compiler</li>
<li><a href="http://python.org/" rel="nofollo... | 4 | 2009-10-20T03:53:17Z | 1,592,531 | <p>I suggest you focus on CPython.</p>
<p><a href="http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html" rel="nofollow">http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html</a></p>
<p>Rather than a Scheme to .pyc translator, I suggest you write a Scheme to Python translator, and then let... | 2 | 2009-10-20T04:55:19Z | [
"python",
"compiler-construction",
"pyc"
] |
Compilers targeting .pyc files? | 1,592,347 | <p>Out of curiosity, are there many compilers out there which target <code>.pyc</code> files?</p>
<p>After a bit of Googling, the only two I can find are:</p>
<ul>
<li><a href="http://github.com/whymirror/unholy" rel="nofollow">unholy</a>: why_'s Ruby-to-pyc compiler</li>
<li><a href="http://python.org/" rel="nofollo... | 4 | 2009-10-20T03:53:17Z | 1,598,066 | <p>I wrote a compiler several years ago which accepted a lisp-like language called "Noodle" and produced Python bytecode. While it never became particularly useful, it was a tremendously good learning experience both for understanding Common Lisp better (I copied several of its features) and for understanding Python be... | 3 | 2009-10-21T00:17:26Z | [
"python",
"compiler-construction",
"pyc"
] |
Compilers targeting .pyc files? | 1,592,347 | <p>Out of curiosity, are there many compilers out there which target <code>.pyc</code> files?</p>
<p>After a bit of Googling, the only two I can find are:</p>
<ul>
<li><a href="http://github.com/whymirror/unholy" rel="nofollow">unholy</a>: why_'s Ruby-to-pyc compiler</li>
<li><a href="http://python.org/" rel="nofollo... | 4 | 2009-10-20T03:53:17Z | 1,814,044 | <p>Just for your interest, I have written a toy compiler from a simple LISP to Python. Practically, this is a LISP to pyc compiler.</p>
<p>Have a look: <a href="http://bernhardkausler.wordpress.com/2009/11/28/sinc-%E2%80%94-the-tiniest-lisp-compiler-to-python/" rel="nofollow">sinC - The tiniest LISP compiler</a></p>
| 2 | 2009-11-28T22:56:37Z | [
"python",
"compiler-construction",
"pyc"
] |
Compilers targeting .pyc files? | 1,592,347 | <p>Out of curiosity, are there many compilers out there which target <code>.pyc</code> files?</p>
<p>After a bit of Googling, the only two I can find are:</p>
<ul>
<li><a href="http://github.com/whymirror/unholy" rel="nofollow">unholy</a>: why_'s Ruby-to-pyc compiler</li>
<li><a href="http://python.org/" rel="nofollo... | 4 | 2009-10-20T03:53:17Z | 13,425,716 | <p>Probably a bit late at the party but if you're still interested the clojure-py project (https://github.com/halgari/clojure-py) is now able to compile a significant subset of clojure to python bytecode -- but some help is always welcome.</p>
<p>Targeting bytecode is not that hard in itself, except for one thing: it ... | 1 | 2012-11-16T23:01:59Z | [
"python",
"compiler-construction",
"pyc"
] |
Decoding Mac OS text in Python | 1,592,925 | <p>I'm writing some code to parse RTF documents, and need to handle the various codepages they can use. Python comes with decoders for all the necessary Windows codepages, but I'm not sure how to handle the Mac ones:</p>
<pre><code># 77: "10000", # Mac Roman
# 78: "10001", # Mac Shift Jis
# 79: "10003", # Mac Hangul
#... | 6 | 2009-10-20T07:05:44Z | 1,592,936 | <p>You can use the python codecs for these that are known by their names 'mac-roman', 'mac-turkish', etc.</p>
<pre><code>>>> 'foo'.decode('mac-turkish')
u'foo'
</code></pre>
<p>You'll have to refer to them by their names, these numbers you've got in your question don't appear in the source files. For more in... | 8 | 2009-10-20T07:09:54Z | [
"python",
"osx"
] |
Decoding Mac OS text in Python | 1,592,925 | <p>I'm writing some code to parse RTF documents, and need to handle the various codepages they can use. Python comes with decoders for all the necessary Windows codepages, but I'm not sure how to handle the Mac ones:</p>
<pre><code># 77: "10000", # Mac Roman
# 78: "10001", # Mac Shift Jis
# 79: "10003", # Mac Hangul
#... | 6 | 2009-10-20T07:05:44Z | 1,592,937 | <p>It seems that at least Mac Roman and Mac Turkish encodings exist in Python stdlib, under names macroman and macturkish. See <a href="http://svn.python.org/projects/python/trunk/Lib/encodings/aliases.py" rel="nofollow">http://svn.python.org/projects/python/trunk/Lib/encodings/aliases.py</a> for a complete list of enc... | 3 | 2009-10-20T07:10:02Z | [
"python",
"osx"
] |
Decoding Mac OS text in Python | 1,592,925 | <p>I'm writing some code to parse RTF documents, and need to handle the various codepages they can use. Python comes with decoders for all the necessary Windows codepages, but I'm not sure how to handle the Mac ones:</p>
<pre><code># 77: "10000", # Mac Roman
# 78: "10001", # Mac Shift Jis
# 79: "10003", # Mac Hangul
#... | 6 | 2009-10-20T07:05:44Z | 1,592,941 | <p>No.</p>
<p>However, <a href="http://unicode.org/Public/MAPPINGS/VENDORS/APPLE/" rel="nofollow">unicode.org</a> provides codec description files that you can use to generate modules that will parse those codecs. Included with python source distributions is a script that will convert these files: <code>Python-x.x/Too... | 1 | 2009-10-20T07:10:50Z | [
"python",
"osx"
] |
Is there any simple way to benchmark python script? | 1,593,019 | <p>Usually I use shell command <code>time</code>. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.</p>
<p>Any tools for linux or just python to do this?</p>
| 39 | 2009-10-20T07:40:54Z | 1,593,034 | <p>Have a look at <a href="http://docs.python.org/library/timeit.html">timeit</a>, <a href="http://docs.python.org/library/profile.html">the python profiler</a> and <a href="https://pycallgraph.readthedocs.org/en/master/">pycallgraph</a>.</p>
<h2>timeit</h2>
<pre><code>def test():
"""Stupid test function"""
l... | 60 | 2009-10-20T07:45:04Z | [
"python",
"unix",
"shell",
"benchmarking"
] |
Is there any simple way to benchmark python script? | 1,593,019 | <p>Usually I use shell command <code>time</code>. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.</p>
<p>Any tools for linux or just python to do this?</p>
| 39 | 2009-10-20T07:40:54Z | 1,593,210 | <p>Have a look at <a href="https://pypi.python.org/pypi/nose/" rel="nofollow">nose</a> and at one of its plugins, <a href="http://somethingaboutorange.com/mrl/projects/nose/0.11.1/plugins/prof.html" rel="nofollow">this one</a> in particular.</p>
<p>Once installed, nose is a script in your path, and that you can call i... | 2 | 2009-10-20T08:38:42Z | [
"python",
"unix",
"shell",
"benchmarking"
] |
Is there any simple way to benchmark python script? | 1,593,019 | <p>Usually I use shell command <code>time</code>. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.</p>
<p>Any tools for linux or just python to do this?</p>
| 39 | 2009-10-20T07:40:54Z | 5,544,739 | <p>I usually do a quick <code>time ./script.py</code> to see how long it takes. That does not show you the memory though, at least not as a default. You can use <code>/usr/bin/time -v ./script.py</code> to get a lot of information, including memory usage.</p>
| 6 | 2011-04-04T21:47:21Z | [
"python",
"unix",
"shell",
"benchmarking"
] |
Is there any simple way to benchmark python script? | 1,593,019 | <p>Usually I use shell command <code>time</code>. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.</p>
<p>Any tools for linux or just python to do this?</p>
| 39 | 2009-10-20T07:40:54Z | 11,151,365 | <p>I use a simple decorator to time the func</p>
<pre><code>def st_time(func):
"""
st decorator to calculate the total time of a func
"""
def st_func(*args, **keyArgs):
t1 = time.time()
r = func(*args, **keyArgs)
t2 = time.time()
print "Function=%s, Time=%s" % (func... | 14 | 2012-06-22T06:46:52Z | [
"python",
"unix",
"shell",
"benchmarking"
] |
Is there any simple way to benchmark python script? | 1,593,019 | <p>Usually I use shell command <code>time</code>. My purpose is to test if data is small, medium, large or very large set, how much time and memory usage will be.</p>
<p>Any tools for linux or just python to do this?</p>
| 39 | 2009-10-20T07:40:54Z | 11,857,869 | <p>The <code>timeit</code> module was slow and weird, so I wrote this:</p>
<pre><code>def timereps(reps, func):
from time import time
start = time()
for i in range(0, reps):
func()
end = time()
return (end - start) / reps
</code></pre>
<p>Example:</p>
<pre><code>import os
listdir_time = t... | 4 | 2012-08-08T04:55:00Z | [
"python",
"unix",
"shell",
"benchmarking"
] |
How do I check if the python debug option is set from within a script | 1,593,274 | <p>If I'm in debug mode, I want to do other stuff than when I'm not.</p>
<pre><code>if DEBUG:
STORED_DATA_FILE = os.path.join(TEMP_DIR, 'store.dat')
LOG_LEVEL = logging.DEBUG
print "debug mode"
else:
STORED_DATA_FILE = os.path.join(SCRIPT_PATH, 'store.dat')
LOG_LEVEL = logging.INFO
print "not d... | 7 | 2009-10-20T08:50:46Z | 1,593,355 | <p>you can use <code>python -O</code> with the <code>__debug__</code> variable</p>
<p>where <code>-O</code> means optimise. so <code>__debug__</code> is false</p>
<p><code>-d</code> turns on debugging for the parser, which is not what you want</p>
| 9 | 2009-10-20T09:08:05Z | [
"python",
"script-debugging"
] |
How do I check if the python debug option is set from within a script | 1,593,274 | <p>If I'm in debug mode, I want to do other stuff than when I'm not.</p>
<pre><code>if DEBUG:
STORED_DATA_FILE = os.path.join(TEMP_DIR, 'store.dat')
LOG_LEVEL = logging.DEBUG
print "debug mode"
else:
STORED_DATA_FILE = os.path.join(SCRIPT_PATH, 'store.dat')
LOG_LEVEL = logging.INFO
print "not d... | 7 | 2009-10-20T08:50:46Z | 1,593,401 | <p>Parser debug mode is enabled with <code>-d</code> commandline option or PYTHONDEBUG environment variable and starting from python 2.6 is reflected in <code>sys.flags.debug</code>. But are you sure this is what you are looking for?</p>
| 5 | 2009-10-20T09:19:37Z | [
"python",
"script-debugging"
] |
python queue get(),task_done() question | 1,593,299 | <p>my consumer side of the queue:</p>
<pre><code>m = queue.get()
queue.task_done()
<rest of the program>
</code></pre>
<p>questions:</p>
<ol>
<li><p>does task_done() effectively pop m off the queue and release whatever locks the consumer has on the queue?</p></li>
<li><p>i need to use m during the rest of the... | 18 | 2009-10-20T08:54:43Z | 1,593,330 | <p>No, <code>queue.get()</code> pops the item off the queue. After you do that, you can do whatever you want with it, as long as the producer works like it should and doesn't touch it anymore. <code>queue.task_done()</code> is called only to notify the queue that you are done with something (it doesn't even know about ... | 34 | 2009-10-20T09:00:46Z | [
"python",
"queue"
] |
How to get all related/parent instances from set of child instances without looping through latter set | 1,593,306 | <p>Please regard the following Django models:</p>
<pre><code>
ParentModel(models.Model):
...
ChildModel(models.Model):
parent = models.ForeignKey(ParentModel, related_name='children')
</code></pre>
<p>Let's assume there is certain subset of all children in the database available as a queryset (call it the 1... | 1 | 2009-10-20T08:56:08Z | 1,593,392 | <p>Assuming you have a queryset called <code>children</code>:</p>
<pre><code>ParentModel.objects.filter(children__in=children)
</code></pre>
| 4 | 2009-10-20T09:17:49Z | [
"python",
"django",
"django-views",
"foreign-key-relationship"
] |
How to read a csv file with python | 1,593,318 | <p>I'm trying to read a csv file but it doesn't work.
I can read my csv file but when I see what I read, there where white space between values.</p>
<p>Here is my code </p>
<pre><code># -*- coding: iso-8859-1 -*-
import sql_db, tmpl_macros, os
import security, form, common
import csv
class windows_dialect(csv.Diale... | 3 | 2009-10-20T08:58:39Z | 1,593,351 | <p>I prefer to use numpy's genfromtxt rather than the standard csv library, because it generates numpy's recarray, which are clean data structures to store data in a table-like object.</p>
<pre><code>>>> from numpy import genfromtxt
>>> data = genfromtxt(csvfile, delimiter=',', dtype=None)
# data is ... | 6 | 2009-10-20T09:07:08Z | [
"python",
"csv"
] |
How to read a csv file with python | 1,593,318 | <p>I'm trying to read a csv file but it doesn't work.
I can read my csv file but when I see what I read, there where white space between values.</p>
<p>Here is my code </p>
<pre><code># -*- coding: iso-8859-1 -*-
import sql_db, tmpl_macros, os
import security, form, common
import csv
class windows_dialect(csv.Diale... | 3 | 2009-10-20T08:58:39Z | 1,593,488 | <p>You don't do anything with the dialect you've defined. Did you mean to do this:</p>
<pre><code>csv.register_dialect('windows_dialect', windows_dialect)
p = csv.reader(contenu, dialect='windows_dialect')
</code></pre>
<p>Also not sure what the <code>reco</code> function is for.</p>
| 0 | 2009-10-20T09:35:02Z | [
"python",
"csv"
] |
How to read a csv file with python | 1,593,318 | <p>I'm trying to read a csv file but it doesn't work.
I can read my csv file but when I see what I read, there where white space between values.</p>
<p>Here is my code </p>
<pre><code># -*- coding: iso-8859-1 -*-
import sql_db, tmpl_macros, os
import security, form, common
import csv
class windows_dialect(csv.Diale... | 3 | 2009-10-20T08:58:39Z | 1,600,227 | <p>If you have control over the data, use tab-delimited instead::</p>
<pre><code>import csv
import string
writer = open('junk.txt', 'wb')
for x in range(10):
writer.write('\t'.join(string.letters[:5]))
writer.write('\r\n')
writer.close()
reader = csv.reader(open('junk.txt', 'r'), dialect='excel-tab')
for line... | 2 | 2009-10-21T11:25:35Z | [
"python",
"csv"
] |
Permissions for a site only | 1,593,423 | <p>I have a multilingual Django project. Every language is a different subdomain.
So we've decided to use the "sites" application and to create one different site for every language.</p>
<p>On that project, I also have a "pages" application, which is quite similar to a CMS. The user can create pages with content and t... | 3 | 2009-10-20T09:24:27Z | 1,593,883 | <p>You could create a Group (<a href="http://docs.djangoproject.com/en/dev/topics/auth/" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/auth/</a>)
per site / language and add the users to the groups accordingly.</p>
<p>Then, you can check if the request.user.groups belongs to the group.
(You can do this wi... | 3 | 2009-10-20T11:14:23Z | [
"python",
"django",
"permissions"
] |
Permissions for a site only | 1,593,423 | <p>I have a multilingual Django project. Every language is a different subdomain.
So we've decided to use the "sites" application and to create one different site for every language.</p>
<p>On that project, I also have a "pages" application, which is quite similar to a CMS. The user can create pages with content and t... | 3 | 2009-10-20T09:24:27Z | 1,593,943 | <p>You can override has_add_permission (and related methods) in your ModelAdmin class.
(With similar code like shown above)</p>
| 1 | 2009-10-20T11:28:10Z | [
"python",
"django",
"permissions"
] |
Permissions for a site only | 1,593,423 | <p>I have a multilingual Django project. Every language is a different subdomain.
So we've decided to use the "sites" application and to create one different site for every language.</p>
<p>On that project, I also have a "pages" application, which is quite similar to a CMS. The user can create pages with content and t... | 3 | 2009-10-20T09:24:27Z | 1,594,188 | <p>If you want to filter the Page objects on the admin index of your page-application,
you can override the method queryset() in ModelAdmin.
This QuerySet returns only those Page objects, that belong to a Site (and therefore Group)
of which the request.user is a member.</p>
<pre><code>Pages.objects.filter(site__name__... | 0 | 2009-10-20T12:15:06Z | [
"python",
"django",
"permissions"
] |
Get the request uri outside of a RequestHandler in Google App Engine (Python) | 1,593,483 | <p>So, within a webapp.RequestHandler subclass I would use <code>self.request.uri</code> to get the request URI. But, I can't access this outside of a RequestHandler and so no go. Any ideas?</p>
<p>I'm running Python and I'm new at it as well as GAE.</p>
| 1 | 2009-10-20T09:33:50Z | 1,593,985 | <p>You should generally be doing everything within some sort of RequestHandler or the equivalent in your non-WebApp framework. However, if you really <strong>insist</strong> on being stuck in the early 1990s and writing plain CGI scripts, the environment variables SERVER_NAME and PATH_INFO may be what you want; see a ... | 2 | 2009-10-20T11:34:22Z | [
"python",
"google-app-engine"
] |
Get the request uri outside of a RequestHandler in Google App Engine (Python) | 1,593,483 | <p>So, within a webapp.RequestHandler subclass I would use <code>self.request.uri</code> to get the request URI. But, I can't access this outside of a RequestHandler and so no go. Any ideas?</p>
<p>I'm running Python and I'm new at it as well as GAE.</p>
| 1 | 2009-10-20T09:33:50Z | 1,594,029 | <p>Since using request outside code handling it is meaningless I assume you'd like to access it from some method called by handler without passing request to it. Your choices are:</p>
<ol>
<li>Refactor code so that request is passed to it.</li>
<li>When the former is not possible use a hack by defining a global <a hre... | 0 | 2009-10-20T11:43:35Z | [
"python",
"google-app-engine"
] |
Python: How to check if a nested list is essentially empty? | 1,593,564 | <p>Is there a Pythonic way to <strong>check</strong> if a <em>list</em> (a <em>nested</em> list with elements & lists) is essentially <strong>empty</strong>? What I mean by empty here is that the list might have elements, but those are also empty lists.</p>
<p>The Pythonic way to check an empty list works only on ... | 6 | 2009-10-20T09:57:24Z | 1,593,589 | <p>I don't think there is an obvious way to do it in Python. My best guess would be to use a recursive function like this one :</p>
<pre><code>def empty(li):
if li == []:
return True
else:
return all((isinstance(sli, list) and empty(sli)) for sli in li)
</code></pre>
<p>Note that <code>all</co... | 4 | 2009-10-20T10:03:33Z | [
"python",
"list"
] |
Python: How to check if a nested list is essentially empty? | 1,593,564 | <p>Is there a Pythonic way to <strong>check</strong> if a <em>list</em> (a <em>nested</em> list with elements & lists) is essentially <strong>empty</strong>? What I mean by empty here is that the list might have elements, but those are also empty lists.</p>
<p>The Pythonic way to check an empty list works only on ... | 6 | 2009-10-20T09:57:24Z | 1,593,612 | <p>A simple recursive check would be enough, and return as early as possible, we assume it input is not a list or contains non-lists it is not empty</p>
<pre><code>def isEmpty(alist):
try:
for a in alist:
if not isEmpty(a):
return False
except:
# we will reach here i... | 1 | 2009-10-20T10:09:26Z | [
"python",
"list"
] |
Python: How to check if a nested list is essentially empty? | 1,593,564 | <p>Is there a Pythonic way to <strong>check</strong> if a <em>list</em> (a <em>nested</em> list with elements & lists) is essentially <strong>empty</strong>? What I mean by empty here is that the list might have elements, but those are also empty lists.</p>
<p>The Pythonic way to check an empty list works only on ... | 6 | 2009-10-20T09:57:24Z | 1,593,759 | <p>Simple code, works for any iterable object, not just lists:</p>
<pre><code>>>> def empty(seq):
... try:
... return all(map(empty, seq))
... except TypeError:
... return False
...
>>> empty([])
True
>>> empty([4])
False
>>> empty([[]])
True
>>> emp... | 7 | 2009-10-20T10:38:54Z | [
"python",
"list"
] |
Python: How to check if a nested list is essentially empty? | 1,593,564 | <p>Is there a Pythonic way to <strong>check</strong> if a <em>list</em> (a <em>nested</em> list with elements & lists) is essentially <strong>empty</strong>? What I mean by empty here is that the list might have elements, but those are also empty lists.</p>
<p>The Pythonic way to check an empty list works only on ... | 6 | 2009-10-20T09:57:24Z | 1,593,797 | <p>If you do not need to iterate through the lists, simpler is better, so something like this will work:</p>
<pre><code>def empty_tree(input_list):
"""Recursively iterate through values in nested lists."""
for item in input_list:
if not isinstance(item, list) or not empty_tree(item):
retur... | 9 | 2009-10-20T10:51:43Z | [
"python",
"list"
] |
Python: How to check if a nested list is essentially empty? | 1,593,564 | <p>Is there a Pythonic way to <strong>check</strong> if a <em>list</em> (a <em>nested</em> list with elements & lists) is essentially <strong>empty</strong>? What I mean by empty here is that the list might have elements, but those are also empty lists.</p>
<p>The Pythonic way to check an empty list works only on ... | 6 | 2009-10-20T09:57:24Z | 1,605,679 | <p>I have combined the use of <code>isinstance()</code> by <strong>Ants Aasma</strong> and <code>all(map())</code> by <strong>Stephan202</strong>, to form the following solution. <code>all([])</code> returns <code>True</code> and the function relies on this behaviour. I think it has the best of both and is better since... | 7 | 2009-10-22T08:11:16Z | [
"python",
"list"
] |
python constructing functions on the fly | 1,593,572 | <p>I have several little functions <code>f1</code>, <code>f2</code>, <code>f3</code> and a function <code>f</code>.</p>
<p>I want <code>f</code> to be a "container" to <code>f1</code>, <code>f2</code>, <code>f3</code>: to do the some of operations <code>f1</code>, <code>f2</code>, <code>f3</code>, depending on the pro... | 5 | 2009-10-20T09:59:00Z | 1,593,586 | <p>Create dictionary</p>
<pre><code>fun_dict = {'f1': f1, 'f2': f2}
</code></pre>
<p>then parse your config and run function from dict:</p>
<pre><code>fun_dict[parsed_name]()
</code></pre>
| 1 | 2009-10-20T10:03:15Z | [
"python"
] |
python constructing functions on the fly | 1,593,572 | <p>I have several little functions <code>f1</code>, <code>f2</code>, <code>f3</code> and a function <code>f</code>.</p>
<p>I want <code>f</code> to be a "container" to <code>f1</code>, <code>f2</code>, <code>f3</code>: to do the some of operations <code>f1</code>, <code>f2</code>, <code>f3</code>, depending on the pro... | 5 | 2009-10-20T09:59:00Z | 1,593,596 | <p>If <code>f1</code>, <code>f2</code> etc. are functions with side effects, than you should use an explicit <code>for</code> loop (no fancy <code>map</code> solution). Perhaps you want something like this?</p>
<pre><code>configurations = {
'config_1': (f1, f2, f3),
'config_2': (f1, f2),
}
def f(config='config_1'... | 3 | 2009-10-20T10:05:18Z | [
"python"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.