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 |
|---|---|---|---|---|---|---|---|---|---|
What is the simplest way to find the difference between 2 times in python? | 51,010 | <p>I have 2 time values which have the type <code>datetime.time</code>. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type <code>datetime.datetime</code> but not for <code>datetime.time</code>. So what is the best way to do this?</p>
| 9 | 2008-09-09T00:28:58Z | 263,451 | <p>Environment.TickCount seems to work well if you need something quick.</p>
<p>int start = Environment.TickCount</p>
<p>...DoSomething()</p>
<p>int elapsedtime = Environment.TickCount - start</p>
<p>Jon</p>
| -3 | 2008-11-04T21:05:21Z | [
"python",
"datetime",
"time"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 51,214 | <p>If you use the <code>curses</code> package, you have much greater control of the console. It also comes at a higher cost in code complexity and is probably unnecessary unless you are developing a large console-based app.</p>
<p>For a simple solution, you can always put the spinning wheel at the end of the status m... | 4 | 2008-09-09T04:14:37Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 51,218 | <p>You might also try:</p>
<pre><code>sys.stdout.write("\r%2d%%" % percent)
sys.stdout.flush()
</code></pre>
<p>Using a single carriage return at the beginning of your string rather than several backspaces. Your cursor will still blink, but it'll blink after the percent sign rather than under the first digit, and wit... | 13 | 2008-09-09T04:21:09Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 51,239 | <p>There's a text progress bar library for python at <a href="http://pypi.python.org/pypi/progressbar/2.2">http://pypi.python.org/pypi/progressbar/2.2</a> that you might find useful: </p>
<blockquote>
<p>This library provides a text mode progressbar. This is tipically used to display the progress of a long running ... | 16 | 2008-09-09T04:48:28Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 704,865 | <p>For small files you may need to had this lines in order to avoid crazy percentages:</p>
<p>sys.stdout.write("\r%2d%%" % percent)</p>
<p>sys.stdout.flush()</p>
<p>Cheers</p>
| 0 | 2009-04-01T10:04:19Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 12,880,543 | <p>Thats how I did this could help you:
<a href="https://github.com/mouuff/MouDownloader/blob/master/api/download.py" rel="nofollow">https://github.com/mouuff/MouDownloader/blob/master/api/download.py</a></p>
| 0 | 2012-10-14T08:31:23Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 14,123,416 | <p>I used this code:</p>
<pre><code>url = (<file location>)
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while Tr... | 1 | 2013-01-02T14:01:43Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 28,424,236 | <p>Late to the party, as usual. Here's an implementation that supports reporting progress, like the core <code>urlretrieve</code>:</p>
<pre><code>import urllib2
def urlretrieve(urllib2_request, filepath, reporthook=None, chunk_size=4096):
req = urllib2.urlopen(urllib2_request)
if reporthook:
# ensure... | 0 | 2015-02-10T04:38:33Z | [
"python",
"http"
] |
How to write a download progress indicator in Python? | 51,212 | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | 41 | 2008-09-09T04:09:15Z | 36,022,923 | <p>For what it's worth, here's the code I used to get it working:</p>
<pre><code>from urllib import urlretrieve
from progressbar import ProgressBar, Percentage, Bar
url = "http://......."
fileName = "file"
pbar = ProgressBar(widgets=[Percentage(), Bar()])
urlretrieve(url, fileName, reporthook=dlProgress)
def dlProgr... | 2 | 2016-03-15T21:52:32Z | [
"python",
"http"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 51,240 | <p>I'll always use <a href="http://lxml.de/">lxml</a> for such tasks. You could use <a href="http://www.crummy.com/software/BeautifulSoup/">beautifulsoup</a> as well.</p>
<pre><code>import lxml.html
t = lxml.html.parse(url)
print t.find(".//title").text
</code></pre>
| 44 | 2008-09-09T04:49:38Z | [
"python",
"html"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 51,242 | <p>This is probably overkill for such a simple task, but if you plan to do more than that, then it's saner to start from these tools (mechanize, BeautifulSoup) because they are much easier to use than the alternatives (urllib to get content and regexen or some other parser to parse html)</p>
<p>Links:
<a href="http://... | 5 | 2008-09-09T04:51:09Z | [
"python",
"html"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 51,263 | <p>The mechanize Browser object has a title() method. So the code from <a href="http://stackoverflow.com/questions/51233/how-can-i-retrieve-the-page-title-of-a-webpage-using-python#51242" rel="nofollow">this post</a> can be rewritten as:</p>
<pre><code>from mechanize import Browser
br = Browser()
br.open("http://www.g... | 10 | 2008-09-09T05:45:39Z | [
"python",
"html"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 51,550 | <p>Here's a simplified version of <a href="http://stackoverflow.com/a/51242/4279">@Vinko Vrsalovic's answer</a>:</p>
<pre><code>import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("https://www.google.com"))
print soup.title.string
</code></pre>
<p>NOTE:</p>
<ul>
<li><p><em>so... | 54 | 2008-09-09T10:32:54Z | [
"python",
"html"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 17,123,979 | <p><code>soup.title.string</code> actually returns a unicode string.
To convert that into normal string, you need to do
<code>string=string.encode('ascii','ignore')</code></p>
| 1 | 2013-06-15T13:05:59Z | [
"python",
"html"
] |
How can I retrieve the page title of a webpage using Python? | 51,233 | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| 44 | 2008-09-09T04:38:55Z | 36,650,753 | <p>Using <a href="https://docs.python.org/3.4/library/html.parser.html" rel="nofollow">HTMLParser</a>:</p>
<pre><code>from urllib.request import urlopen
from html.parser import HTMLParser
class TitleParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.match = False
self.... | 0 | 2016-04-15T15:07:10Z | [
"python",
"html"
] |
Passing on named variable arguments in python | 51,412 | <p>Say I have the following methods:</p>
<pre><code>def methodA(arg, **kwargs):
pass
def methodB(arg, *args, **kwargs):
pass
</code></pre>
<p>In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define <code>methodA</code> as follows, the second argument will be passed on as ... | 11 | 2008-09-09T08:46:51Z | 51,414 | <p>Some experimentation and I figured this one out:</p>
<p>def methodA(arg, **kwargs):
methodB("argvalue", **kwargs)</p>
<p>Seems obvious now...</p>
| 1 | 2008-09-09T08:49:02Z | [
"python",
"varargs"
] |
Passing on named variable arguments in python | 51,412 | <p>Say I have the following methods:</p>
<pre><code>def methodA(arg, **kwargs):
pass
def methodB(arg, *args, **kwargs):
pass
</code></pre>
<p>In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define <code>methodA</code> as follows, the second argument will be passed on as ... | 11 | 2008-09-09T08:46:51Z | 51,415 | <p>Put the asterisks before the kwargs variable. This makes Python pass the variable (which is assumed to be a dictionary) as keyword arguments.</p>
<pre><code>methodB("argvalue", **kwargs)
</code></pre>
| 32 | 2008-09-09T08:50:34Z | [
"python",
"varargs"
] |
Passing on named variable arguments in python | 51,412 | <p>Say I have the following methods:</p>
<pre><code>def methodA(arg, **kwargs):
pass
def methodB(arg, *args, **kwargs):
pass
</code></pre>
<p>In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define <code>methodA</code> as follows, the second argument will be passed on as ... | 11 | 2008-09-09T08:46:51Z | 63,943 | <p>As an aside: When using functions instead of methods, you could also use functools.partial:</p>
<pre><code>import functools
def foo(arg, **kwargs):
...
bar = functools.partial(foo, "argvalue")
</code></pre>
<p>The last line will define a function "bar" that, when called, will call foo with the first argument... | 2 | 2008-09-15T15:28:30Z | [
"python",
"varargs"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 51,505 | <p>You could try increasing the indent size, but in general I would just say, relax, it will come with time. I don't think trying to make Python look like C is a very good idea.</p>
| 8 | 2008-09-09T10:08:28Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 51,531 | <p>Perhaps the best thing would be to turn on "show whitespace" in your editor. Then you would have a visual indication of how far in each line is tabbed (usually a bunch of dots), and it will be more apparent when that changes.</p>
| 3 | 2008-09-09T10:24:46Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 51,551 | <p>I like to put blank lines around blocks to make control flow more obvious. For example:</p>
<pre><code>if foo:
bar = baz
while bar not biz:
bar = i_am_going_to_find_you_biz_i_swear_on_my_life()
did_i_not_warn_you_biz()
my_father_is_avenged()
</code></pre>
| 15 | 2008-09-09T10:33:05Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 51,570 | <p>Rather than focusing on making your existing structures more readable, you should focus on making more logical structures. Make smaller blocks, try not to nest blocks excessively, make smaller functions, and try to think through your code flow more.</p>
<p>If you come to a point where you can't quickly determine t... | 7 | 2008-09-09T10:43:27Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 52,090 | <p>Part of learning a new programming language is learning to read code in that language. A crutch like this may make it easier to read your own code, but it's going to impede the process of learning how to read anyone else's Python code. I really think you'd be better off getting rid of the end of block comments and... | 23 | 2008-09-09T14:58:52Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 52,111 | <p>I would look in to understanding more details about Python syntax. Often times if a piece of code looks odd, there usually is a better way to write it. For example, in the above example:</p>
<pre><code>bar = foo if baz else None
while bar not biz:
bar = i_am_going_to_find_you_biz_i_swear_on_my_life()
did_i_not... | -1 | 2008-09-09T15:09:04Z | [
"python",
"readability"
] |
Improving Python readability? | 51,502 | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | 2 | 2008-09-09T10:05:36Z | 3,054,853 | <pre><code>from __future__ import braces
</code></pre>
<p>Need I say more? :)</p>
<p>Seriously, <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 8</a>, 'Blank lines', §4 is the official way to do it.</p>
| 0 | 2010-06-16T15:40:49Z | [
"python",
"readability"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 51,523 | <pre><code>>>> import os
>>> os.path.abspath("mydir/myfile.txt")
</code></pre>
| 424 | 2008-09-09T10:21:03Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 51,539 | <pre><code>>>> import os
>>> os.path.abspath('mydir/myfile.txt')
'C:\\example\\cwd\\mydir\\myfile.txt'
>>>
</code></pre>
| 32 | 2008-09-09T10:28:26Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 58,417 | <p>Better still, install the <a href="http://pypi.python.org/pypi/path.py/2.2"><code>path.py</code></a> module, it wraps all the <code>os.path</code> functions and other related functions into methods on an object that can be used wherever strings are used:</p>
<pre><code>>>> from path import path
>>>... | 13 | 2008-09-12T06:53:25Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 15,325,066 | <p>Today you can also use the <code>unipath</code> package which was based on <code>path.py</code>: <a href="http://sluggo.scrapping.cc/python/unipath/">http://sluggo.scrapping.cc/python/unipath/</a></p>
<pre><code>>>> from unipath import Path
>>> absolute_path = Path('mydir/myfile.txt').absolute()
P... | 7 | 2013-03-10T17:11:57Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 26,539,947 | <p>You could use the new Python 3.4 library <code>pathlib</code>. (You can also get it for Python 2.6 or 2.7 using <code>pip install pathlib</code>.) The authors <a href="http://www.python.org/dev/peps/pep-0428/#abstract" rel="nofollow">wrote</a>: "The aim of this library is to provide a simple hierarchy of classes to ... | 29 | 2014-10-24T01:05:02Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
How to get an absolute file path in Python | 51,520 | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| 308 | 2008-09-09T10:19:32Z | 38,813,098 | <p>I prefer to use glob</p>
<p>here is how to list all file types in your current folder:</p>
<pre><code>import glob
for x in glob.glob():
print(x)
</code></pre>
<p>here is how to list all (for example) .txt files in your current folder:</p>
<pre><code>import glob
for x in glob.glob('*.txt'):
print(x)
</cod... | 2 | 2016-08-07T10:14:40Z | [
"python",
"path",
"relative-path",
"absolute-path"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 51,668 | <p>I don't think that your results are all that surprising -- if anything it is that Postgres is so fast.</p>
<p>Does the Postgres query run faster a second time once it has had a chance to cache the data? To be a little fairer your test for Java and Python should cover the cost of acquiring the data in the first pla... | 0 | 2008-09-09T11:43:10Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 51,745 | <p>I would say your test scheme is not really useful. To fulfill the db query, the db server goes through several steps:</p>
<ol>
<li>parse the SQL</li>
<li>work up a query plan, i. e. decide on which indices to use (if any), optimize etc.</li>
<li>if an index is used, search it for the pointers to the actual data, th... | 13 | 2008-09-09T12:31:26Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 51,817 | <p>One other thing that an RDBMS generally does for you is to provide concurrency by protecting you from simultaneous access by another process. This is done by placing locks, and there's some overhead from that.</p>
<p>If you're dealing with entirely static data that never changes, and especially if you're in a basi... | 0 | 2008-09-09T13:04:15Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 51,933 | <p>Those are very detailed answers, but they mostly beg the question, how do I get these benefits without leaving Postgres given that the data easily fits into memory, requires concurrent reads but no writes and is queried with the same query over and over again.</p>
<p>Is it possible to precompile the query and optim... | 3 | 2008-09-09T13:50:18Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 51,976 | <p>You need to increase postgres' caches to the point where the whole working set fits into memory before you can expect to see perfomance comparable to doing it in-memory with a program.</p>
| 0 | 2008-09-09T14:10:06Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 52,006 | <p>Postgres is doing a lot more than it looks like (maintaining data consistency for a start!)</p>
<p>If the values don't have to be 100% spot on, or if the table is updated rarely, but you are running this calculation often, you might want to look into Materialized Views to speed it up.</p>
<p>(Note, I have not used... | 8 | 2008-09-09T14:26:28Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 52,179 | <p>Thanks for the Oracle timings, that's the kind of stuff I'm looking for (disappointing though :-)</p>
<p>Materialized views are probably worth considering as I think I can precompute the most interesting forms of this query for most users.</p>
<p>I don't think query round trip time should be very high as i'm runni... | 0 | 2008-09-09T15:34:19Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 53,303 | <p>I'm a MS-SQL guy myself, and we'd use <a href="http://msdn.microsoft.com/en-us/library/ms178015.aspx" rel="nofollow">DBCC PINTABLE</a> to keep a table cached, and <a href="http://msdn.microsoft.com/en-us/library/ms184361.aspx" rel="nofollow">SET STATISTICS IO</a> to see that it's reading from cache, and not disk. </... | 2 | 2008-09-10T01:47:32Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 53,333 | <p>I retested with MySQL specifying ENGINE = MEMORY and it doesn't change a thing (still 200 ms). Sqlite3 using an in-memory db gives similar timings as well (250 ms).</p>
<p>The math <a href="http://stackoverflow.com/questions/51553/why-are-sql-aggregate-functions-so-much-slower-than-python-and-java-or-poor-man#5330... | 5 | 2008-09-10T02:29:45Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | 51,553 | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | 11 | 2008-09-09T10:33:39Z | 53,713 | <p>Are you using TCP to access the Postgres? In that case Nagle is messing with your timing.</p>
| 1 | 2008-09-10T09:45:20Z | [
"python",
"sql",
"optimization",
"aggregate",
"olap"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 51,663 | <p>The <a href="https://docs.python.org/2.7/library/os.html" rel="nofollow">os.statvfs()</a> function is a better way to get that information for Unix-like platforms (including OS X). The Python documentation says "Availability: Unix" but it's worth checking whether it works on Windows too in your build of Python (ie. ... | 1 | 2008-09-09T11:40:26Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 51,675 | <p>I Don't know of any cross-platform way to achieve this, but maybe a good workaround for you would be to write a wrapper class that checks the operating system and uses the best method for each.</p>
<p>For Windows, there's the <a href="http://aspn.activestate.com/ASPN/docs/ActivePython/2.2/PyWin32/win32api__GetDiskF... | 0 | 2008-09-09T11:47:20Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 53,170 | <p>You can use <a href="http://man.he.net/?section=all&topic=df" rel="nofollow">df</a> as a cross-platform way. It is a part of <a href="http://www.gnu.org/software/coreutils/" rel="nofollow">GNU core utilities</a>. These are the core utilities which are expected to exist on every operating system. However, they ar... | -2 | 2008-09-09T23:48:07Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 1,728,106 | <p>If you dont like to add another dependency you can for windows use ctypes to call the win32 function call directly. </p>
<pre><code>import ctypes
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(u'c:\\'), None, None, ctypes.pointer(free_bytes))
if free_bytes.value ==... | 5 | 2009-11-13T09:22:14Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 2,372,171 | <pre><code>import ctypes
import os
import platform
import sys
def get_free_space_mb(dirname):
"""Return folder/drive free space (in megabytes)."""
if platform.system() == 'Windows':
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, No... | 58 | 2010-03-03T14:45:23Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 6,773,639 | <p>A good cross-platform way is using psutil: <a href="http://pythonhosted.org/psutil/#disks" rel="nofollow">http://pythonhosted.org/psutil/#disks</a>
(Note that you'll need psutil 0.3.0 or above).</p>
| 5 | 2011-07-21T09:02:48Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 19,332,602 | <p>You could use the <a href="https://pypi.python.org/pypi/WMI/">wmi</a> module for windows and os.statvfs for unix</p>
<p>for window</p>
<pre><code>import wmi
c = wmi.WMI ()
for d in c.Win32_LogicalDisk():
print( d.Caption, d.FreeSpace, d.Size, d.DriveType)
</code></pre>
<p>for unix or linux</p>
<pre><code>fr... | 13 | 2013-10-12T09:20:42Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 29,944,093 | <p>Install <a href="http://pythonhosted.org/psutil/">psutil</a> using <code>pip install psutil</code>. Then you can get the amount of free space in bytes using:</p>
<pre class="lang-python prettyprint-override"><code>import psutil
print(psutil.disk_usage(".").free)
</code></pre>
| 7 | 2015-04-29T12:43:17Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 35,860,878 | <p>Below code returns correct value on windows</p>
<pre><code>import win32file
def get_free_space(dirname):
secsPerClus, bytesPerSec, nFreeClus, totClus = win32file.GetDiskFreeSpace(dirname)
return secsPerClus * bytesPerSec * nFreeClus
</code></pre>
| 0 | 2016-03-08T06:53:17Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Cross-platform space remaining on volume using python | 51,658 | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| 56 | 2008-09-09T11:36:39Z | 38,889,608 | <p>From Python 3.3 you can use <a href="https://docs.python.org/3/library/shutil.html#shutil.disk_usage" rel="nofollow">shutil.disk_usage("/").free</a> from standard library for both Windows and UNIX :)</p>
| 2 | 2016-08-11T07:11:52Z | [
"python",
"windows",
"linux",
"osx",
"diskspace"
] |
Large Python Includes | 53,027 | <p>I have a file that I want to include in Python but the included file is fairly long and it'd be much neater to be able to split them into several files but then I have to use several include statements.</p>
<p>Is there some way to group together several files and include them all at once?</p>
| 3 | 2008-09-09T22:15:09Z | 53,036 | <p>Yes, take a look at the "6.4 Packages" section in <a href="http://docs.python.org/tut/node8.html" rel="nofollow">http://docs.python.org/tut/node8.html</a>:</p>
<p>Basically, you can place a bunch of files into a directory and add an __init__.py file to the directory. If the directory is in your PYTHONPATH or sys.p... | 6 | 2008-09-09T22:19:25Z | [
"python"
] |
Large Python Includes | 53,027 | <p>I have a file that I want to include in Python but the included file is fairly long and it'd be much neater to be able to split them into several files but then I have to use several include statements.</p>
<p>Is there some way to group together several files and include them all at once?</p>
| 3 | 2008-09-09T22:15:09Z | 53,037 | <ol>
<li>Put files in one folder. </li>
<li>Add __init__.py file to the folder. Do necessary imports in __init__.py</li>
<li><p>Replace multiple imports by one:</p>
<p>import folder_name </p></li>
</ol>
<p>See <a href="http://docs.python.org/tut/node8.html#SECTION008400000000000000000... | 8 | 2008-09-09T22:21:04Z | [
"python"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 53,173 | <p>Put a <code>\</code> at the end of your line or enclose the statement in parens <code>( .. )</code>. From <a href="http://www.ibm.com/developerworks/opensource/library/os-python5/">IBM</a>:</p>
<pre><code>b = ((i1 < 20) and
(i2 < 30) and
(i3 < 40))
</code></pre>
<p>or</p>
<pre><code>b = (i1 <... | 12 | 2008-09-09T23:48:30Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 53,180 | <p>What is the line? You can just have arguments on the next line without any problems:</p>
<pre><code>a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,
blahblah6, blahblah7)
</code></pre>
<p>Otherwise you can do something like this:</p>
<pre><code>if a == True and \
b == False
</co... | 615 | 2008-09-09T23:52:05Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 53,182 | <blockquote>
<p>From the horse's mouth: <a href="http://docs.python.org/ref/explicit-joining.html">Explicit line
joining </a></p>
<p>Two or more physical lines may be
joined into logical lines using
backslash characters (<code>\</code>), as follows:
when a physical line ends in a
backslash that is not ... | 9 | 2008-09-09T23:53:17Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 53,200 | <p>From <a href="http://www.python.org/dev/peps/pep-0008/">Style Guide for Python Code</a>:</p>
<blockquote>
<p><strong>The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces.</strong> Long lines can be broken over multiple lines by wrapping ex... | 119 | 2008-09-10T00:06:39Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 61,933 | <p>The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was.</p>
<p>See Python Idioms and Anti-Idioms (for <a href="https://docs.python.org/2/howto/doanddont.html#using-backslash-to... | 46 | 2008-09-15T06:28:30Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How can I do a line break (line continuation) in Python? | 53,162 | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| 535 | 2008-09-09T23:45:30Z | 110,882 | <p>You can break lines in between parenthesises and braces. Additionally, you can append the backslash character <code>\</code> to a line to explicitly break it:</p>
<pre><code>x = (tuples_first_value,
second_value)
y = 1 + \
2
</code></pre>
| 16 | 2008-09-21T12:20:08Z | [
"python",
"syntax",
"line-breaks",
"long-lines"
] |
How do you check whether a python method is bound or not? | 53,225 | <p>Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?</p>
| 22 | 2008-09-10T00:31:11Z | 53,237 | <p><a href="http://docs.python.org/ref/types.html" rel="nofollow"><strong>im_self</strong> attribute</a></p>
| 3 | 2008-09-10T00:42:13Z | [
"python",
"python-datamodel"
] |
How do you check whether a python method is bound or not? | 53,225 | <p>Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?</p>
| 22 | 2008-09-10T00:31:11Z | 53,322 | <pre><code>def isbound(method):
return method.im_self is not None
def instance(bounded_method):
return bounded_method.im_self
</code></pre>
<p><a href="http://docs.python.org/ref/types.html">User-defined methods:</a></p>
<blockquote>
<p>When a user-defined method object is
created by retrieving a user-de... | 27 | 2008-09-10T02:19:09Z | [
"python",
"python-datamodel"
] |
How do you check whether a python method is bound or not? | 53,225 | <p>Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?</p>
| 22 | 2008-09-10T00:31:11Z | 18,955,425 | <p>In python 3 the <code>__self__</code> attribute is <em>only</em> set on bound methods. It's not set to <code>None</code> on plain functions (or unbound methods, which are just plain functions in python 3). </p>
<p>Use something like this:</p>
<pre><code>def is_bound(m):
return hasattr(m, '__self__')
</code></p... | 4 | 2013-09-23T09:03:57Z | [
"python",
"python-datamodel"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 53,522 | <pre><code>if not a:
print("List is empty")
</code></pre>
<p>Using the implicit booleanness of the empty list <code>a</code> is quite pythonic.</p>
| 2,388 | 2008-09-10T06:28:05Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 53,523 | <p>I have seen the below as preferred, as it will catch the null list as well:</p>
<pre><code>if not a:
print "The list is empty or null"
</code></pre>
| 35 | 2008-09-10T06:28:39Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 53,525 | <p>An empty list is itself considered false in true value testing (see <a href="https://docs.python.org/2/library/stdtypes.html#truth-value-testing">python documentation</a>):</p>
<pre><code>a = []
if a:
print "not empty"
</code></pre>
<p>@Daren Thomas</p>
<blockquote>
<p>EDIT: Another point against testing
... | 65 | 2008-09-10T06:31:22Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 53,533 | <p>I prefer the following:</p>
<pre><code>if a == []:
print "The list is empty."
</code></pre>
<p>Readable and you don't have to worry about calling a function like <code>len()</code> to iterate through the variable. Although I'm not entirely sure what the BigO notation of something like this is... but Python's so... | 4 | 2008-09-10T06:43:14Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 53,752 | <p>The pythonic way to do it is from the <a href="https://www.python.org/dev/peps/pep-0008">PEP 8 style guide</a>:</p>
<blockquote>
<p>For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
</p>
<pre><code><b>Yes:</b> if not seq:
if seq:
<b>No:</b> if len(seq):
if not le... | 565 | 2008-09-10T10:33:38Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 61,918 | <p><a href="http://books.google.com/books?id=vpTAq4dnmuAC&pg=RA1-PA479&lpg=RA1-PA479&dq=Python+len+big+O&source=web&ots=AOM6A1K9Fy&sig=iQo8mV6Xf9KdzuNSa-Jkr8wDEuw&hl=en&sa=X&oi=book_result&resnum=4&ct=result"><code>len()</code> is an O(1) operation</a> for Python lists, strin... | 28 | 2008-09-15T05:50:48Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 7,302,987 | <p>I prefer it explicitly:</p>
<pre><code>if len(li) == 0:
print 'the list is empty'
</code></pre>
<p>This way it's 100% clear that <code>li</code> is a sequence (list) and we want to test its size. My problem with <code>if not li: ...</code> is that it gives the false impression that <code>li</code> is a boolean... | 267 | 2011-09-05T00:30:30Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 9,381,545 | <h1>Other methods don't work for numpy arrays</h1>
<p>Other people seem to be generalizing your question beyond just <code>list</code>s, so I thought I'd add a caveat for a different type of sequence that a lot of people might use. You need to be careful with numpy arrays, because other methods that work fine for <co... | 95 | 2012-02-21T16:48:02Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 10,835,703 | <p>I had written:</p>
<pre><code>if isinstance(a, (list, some, other, types, i, accept)) and not a:
do_stuff
</code></pre>
<p>which was voted -1. I'm not sure if that's because readers objected to the strategy or thought the answer wasn't helpful as presented. I'll pretend it was the latter, since---whatever coun... | 15 | 2012-05-31T14:35:05Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 10,863,668 | <p>Python is very uniform about the treatment of emptiness. Given the following:</p>
<pre><code>a = []
.
.
.
if a:
print("List is not empty.")
else:
print("List is empty.")
</code></pre>
<p>You simply check list a with an "if" statement to see if it is empty. From what I have read and been taught, this is th... | 16 | 2012-06-02T15:40:24Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 22,674,207 | <p>A Python list is considered False when it is empty and True when it is not empty.
The following will work quite nicely</p>
<pre><code> if seq:print('List has items')
if not seq:print('List does not have items')
</code></pre>
<p>Also </p>
<pre><code> bool(seq) #will return true if the list has items and... | 3 | 2014-03-26T22:16:58Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 26,579,625 | <p>some methods what i use:</p>
<pre><code>if not a:
print "list is empty"
if not bool(a):
print "list is empty"
if len(a) == 0:
print "list is empty"
</code></pre>
| 7 | 2014-10-27T00:35:45Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 27,262,598 | <p><a href="http://stackoverflow.com/a/53522/908494">Patrick's (accepted) answer</a> is right: <code>if not a:</code> is the right way to do it. <a href="http://stackoverflow.com/a/53752/908494">Harley Holcombe's answer</a> is right that this is in the PEP 8 style guide. But what none of the answers explain is why it's... | 39 | 2014-12-03T02:21:29Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 30,272,696 | <p>Try:</p>
<pre><code>if list:
#Not empty
else:
#Empty
</code></pre>
<p><code>if</code> executes the first statement if <code>bool(condition)</code> returns <code>True</code>. <code>bool(condition)</code> returns <code>False</code> if <code>condition</code> is an empty sequence, including a string, 0 or <cod... | 3 | 2015-05-16T06:50:09Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 32,978,062 | <p>No one seems to have addressed questioning your <em>need</em> to test the list in the first place. Because you provided no additional context, I can imagine that you may not need to do this check in the first place, but are unfamiliar with list processing in Python.</p>
<p>I would argue that the <em>most pythonic<... | 16 | 2015-10-06T19:25:34Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 34,558,732 | <p>From <a href="https://docs.python.org/3.5/library/stdtypes.html#truth-value-testing">documentation</a> on truth value testing:</p>
<p>All values other than what is listed here are considered <code>True</code></p>
<ul>
<li><code>None</code></li>
<li><code>False</code></li>
<li>zero of any numeric type, for example,... | 10 | 2016-01-01T18:18:58Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 35,420,903 | <p>There is of course also</p>
<p><code>print a or "List is empty"</code></p>
| 5 | 2016-02-15T23:18:48Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 36,610,301 | <pre><code>def list_test (L):
if L is None : print 'list is None'
elif not L : print 'list is empty'
else: print 'list has %d elements' % len(L)
list_test(None)
list_test([])
list_test([1,2,3])
</code></pre>
<p>It is sometimes good to test for <code>None</code> and for emptiness separately as thos... | 3 | 2016-04-13T21:55:32Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 37,008,449 | <p>You could also do :</p>
<pre><code>if len(a_list):
print "it's not empty"
</code></pre>
| 5 | 2016-05-03T15:35:09Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 37,353,236 | <p>The preferred way to check if any list, dictionary, set, string or tuple is empty in Python is to simply use an if statement to check it.</p>
<pre><code>def is_empty(any_structure):
if any_structure:
print('Structure is not empty.')
return False
else:
print('Structure is empty.')
... | -3 | 2016-05-20T18:11:01Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 38,492,895 | <pre><code>a = []
# First way
if a:
print 'yes'
else:
print 'no'
# Second way
if len(a) != 0:
print 'yes'
else:
print 'no'
</code></pre>
| 0 | 2016-07-21T00:24:05Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 38,592,561 | <p>you can use try and except as it is cheaper than if else construct</p>
<pre><code>a=[]
try:
x=a[0]
except:
print(' list is empty')
</code></pre>
| -2 | 2016-07-26T14:25:29Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 39,469,420 | <p>You can even try using bool() like this</p>
<pre><code> a = [1,2,3];
print bool(a); # it will return True
a = [];
print bool(a); # it will return False
</code></pre>
<p>I love this way for checking list is empty or not. </p>
<p>Very handy and useful.</p>
| 0 | 2016-09-13T11:53:56Z | [
"python",
"list"
] |
Best way to check if a list is empty | 53,513 | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| 1,615 | 2008-09-10T06:20:11Z | 39,598,495 | <p>Demo: </p>
<pre><code>tasks = ['task1', 'task2']
# create new list
if len(tasks) == False:
print("empty")
'''
# There are many ways to check whether a List/Tuples/String is empty!
# method 1:
# if len(tasks) is False:
# method 2:
# if len(tasks) == False:
# method 3:
# if len(tasks) == 0:
# method 4:
# i... | 0 | 2016-09-20T15:45:59Z | [
"python",
"list"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 53,549 | <p>If you <em>do</em> find you need to write unique code for an environment, use pythons </p>
<pre><code>import mymodule_jython as mymodule
import mymodule_cpython as mymodule
</code></pre>
<p>have this stuff in a simple module (''module_importer''?) and write your code like this:</p>
<pre><code>from module_importe... | 14 | 2008-09-10T07:07:59Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 55,312 | <p>@<a href="#53549" rel="nofollow">Daren Thomas</a>: I agree, but you should use the <a href="http://docs.python.org/dev/library/platform.html#platform.python_implementation" rel="nofollow">platform module</a> to determine which interpreter you're running.</p>
| 10 | 2008-09-10T21:00:04Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 199,275 | <p>I write code for CPython and IronPython but tip should work for Jython as well.</p>
<p>Basically, I write all the platform specific code in separate modules/packages and then import the appropriate one based on platform I'm running on. (see cdleary's comment above)</p>
<p>This is especially important when it comes... | 2 | 2008-10-13T22:33:17Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 342,835 | <p>The #1 thing IMO: <strong>Focus on thread safety</strong>. CPython's GIL makes writing threadsafe code easy because only one thread can access the interpreter at a time. IronPython and Jython are a little less hand-holding though.</p>
| 1 | 2008-12-05T03:35:43Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 342,845 | <p>I'm pretty sure you already know this but unfortunately Jython <a href="http://www.jython.org/Project/userfaq.html#is-jython-the-same-language-as-python" rel="nofollow">can't load c extension modules.</a></p>
| 0 | 2008-12-05T03:41:37Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
What are some strategies to write python code that works in CPython, Jython and IronPython | 53,543 | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| 15 | 2008-09-10T07:04:43Z | 637,549 | <p>There are two <em>major</em> issues at play here...</p>
<p>Firstly, to my knowledge, only CPython has RAII - you have to close your own resources in Jython, Ironpython, etc.</p>
<p>And Secondly, as has been mentioned, is thread safety.</p>
| 0 | 2009-03-12T06:11:27Z | [
"python",
"ironpython",
"jython",
"cpython"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 53,791 | <p>Why don't you try <a href="http://grinder.sourceforge.net" rel="nofollow">The Grinder 3</a> to load test your server, it comes with all this and more prebuilt, and it supports python as a scripting language</p>
| 1 | 2008-09-10T11:20:27Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 56,032 | <p>Instead of giving the number of requests as a fixed value, why not use a scaling factor instead? At the moment, you're treating requests as a limited quantity, and randomising the days on which those requests fall. It would seem more reasonable to treat your requests-per-day as independent.</p>
<pre><code>from date... | 0 | 2008-09-11T08:30:23Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 56,102 | <p>I rewrote the code above to be shorter (but maybe it's too obfuscated now?)</p>
<pre><code>timeline = (start_date + timedelta(days=days) for days in count(0))
how_many_days = (end_date - start_date).days
pick_a_day = lambda _:int(how_many_days * weibullvariate(0.5, 2))
days = sorted(imap(pick_a_day, xrange(how_many... | 0 | 2008-09-11T09:11:50Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 56,247 | <p>Slightly longer but probably more readable rework of your last four lines:</p>
<pre><code>samples = [0 for i in xrange(how_many_days + 1)]
for s in xrange(how_many_responses):
samples[min(int(how_many_days * weibullvariate(0.5, 2)), how_many_days)] += 1
histogram = zip(timeline, samples)
print '\n'.join((d.strf... | 1 | 2008-09-11T10:47:06Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 56,548 | <p>This is quick and probably not that accurate, but if you calculate the PDF yourself, then at least you make it easier to lay several smaller/larger ones on a single timeline. <code>dev</code> is the std deviation in the Guassian noise, which controls the roughness. Note that this is <em>not</em> the 'right' way to g... | 1 | 2008-09-11T13:40:31Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | 53,786 | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | 3 | 2008-09-10T11:16:30Z | 89,140 | <p>Another solution is to use <a href="http://rpy.sourceforge.net/" rel="nofollow">Rpy</a>, which puts all of the power of R (including lots of tools for distributions), easily into Python. </p>
| 0 | 2008-09-18T01:16:20Z | [
"python",
"statistics",
"simulation",
"stress-testing"
] |
Any good AJAX framework for Google App Engine apps? | 53,997 | <p>I am trying to implement AJAX in my Google App Engine application, and so I am looking for a good AJAX framework that will help me. Anyone has any idea?</p>
<p>I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?</p>
| 12 | 2008-09-10T13:12:07Z | 54,008 | <p>As Google Web Toolkit is a <a href="http://code.google.com/support/bin/answer.py?answer=54830&topic=10208" rel="nofollow">subset of Java</a> it works best when you Java at the backend too. Since Google App Engine is currently <a href="http://code.google.com/appengine/kb/general.html#language" rel="nofollow">Pyt... | 12 | 2008-09-10T13:14:54Z | [
"python",
"ajax",
"google-app-engine"
] |
Any good AJAX framework for Google App Engine apps? | 53,997 | <p>I am trying to implement AJAX in my Google App Engine application, and so I am looking for a good AJAX framework that will help me. Anyone has any idea?</p>
<p>I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?</p>
| 12 | 2008-09-10T13:12:07Z | 54,015 | <p>I'd recommend looking into a pure javascript framework (probably Jquery) for your client-side code, and write JSON services in python- that seems to be the easiest / bestest way to go.</p>
<p>Google Web Toolkit lets you write the UI in Java and compile it to javascript. As Dave says, it may be a better choice where... | 3 | 2008-09-10T13:19:51Z | [
"python",
"ajax",
"google-app-engine"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.