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 |
|---|---|---|---|---|---|---|---|---|---|
Gruberâs URL Regular Expression in Python | 1,986,059 | <p>How do I rewrite this <a href="http://alanstorm.com/url_regex_explained" rel="nofollow">new way to recognise</a> addresses to work in Python?</p>
<p><code>\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))</code></p>
| 4 | 2009-12-31T16:38:01Z | 1,986,116 | <p>I don't think python have this expression</p>
<pre><code>[:punct:]
</code></pre>
<p><a href="http://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes" rel="nofollow">Wikipedia</a> says <code>[:punct:]</code> is same to</p>
<pre><code>[-!\"#$%&\'()*+,./:;<=>?@\\[\\\\]^_`{|}~]
</code></pre>... | 5 | 2009-12-31T16:48:20Z | [
"python",
"regex",
"gruber"
] |
Gruberâs URL Regular Expression in Python | 1,986,059 | <p>How do I rewrite this <a href="http://alanstorm.com/url_regex_explained" rel="nofollow">new way to recognise</a> addresses to work in Python?</p>
<p><code>\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))</code></p>
| 4 | 2009-12-31T16:38:01Z | 1,986,134 | <p>Python doesn't have the <a href="http://www.regular-expressions.info/posixbrackets.html" rel="nofollow">POSIX bracket expressions</a>.</p>
<p>The <code>[:punct:]</code> bracket expression is equivalent in ASCII to </p>
<pre><code>[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]
</code></pre>
| 2 | 2009-12-31T16:52:43Z | [
"python",
"regex",
"gruber"
] |
Gruberâs URL Regular Expression in Python | 1,986,059 | <p>How do I rewrite this <a href="http://alanstorm.com/url_regex_explained" rel="nofollow">new way to recognise</a> addresses to work in Python?</p>
<p><code>\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))</code></p>
| 4 | 2009-12-31T16:38:01Z | 1,986,151 | <p>The <a href="http://daringfireball.net/2009/11/liberal_regex_for_matching_urls">original source</a> for that states "This pattern should work in most modern regex implementations" and specifically Perl. Python's regex implementation is modern and <a href="http://docs.python.org/library/re.html#module-re">similar to... | 9 | 2009-12-31T16:55:42Z | [
"python",
"regex",
"gruber"
] |
Where should I place the one-time operation operation in the Django framework? | 1,986,060 | <p>I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django?</p>
<ul>
<li>Put them into the setting.py f... | 3 | 2009-12-31T16:38:20Z | 1,986,150 | <p>For one operation in startserver, you can use <a href="http://docs.djangoproject.com/en/1.1/howto/custom-management-commands/#howto-custom-management-commands" rel="nofollow">customs commands</a> or if you want a periodic task or a queue of taske you can use <a href="http://ask.github.com/celery/introduction.html" r... | 1 | 2009-12-31T16:55:05Z | [
"python",
"django"
] |
Where should I place the one-time operation operation in the Django framework? | 1,986,060 | <p>I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django?</p>
<ul>
<li>Put them into the setting.py f... | 3 | 2009-12-31T16:38:20Z | 1,986,323 | <p>__init__.py will be called every time the app is imported. So if you're using mod_wsgi with Apache for instance with the prefork method, then every new process created is effectively 'starting' the project thus importing __init__.py. It sounds like your best method would be to create a new management command, and th... | 0 | 2009-12-31T17:30:57Z | [
"python",
"django"
] |
Where should I place the one-time operation operation in the Django framework? | 1,986,060 | <p>I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django?</p>
<ul>
<li>Put them into the setting.py f... | 3 | 2009-12-31T16:38:20Z | 1,986,339 | <p>I just create standalone scripts and schedule them with cron. Admittedly it's a bit low-tech, but It Just Works. Just place this at the top of a script in your projects top-level directory and call as needed.</p>
<pre><code>#!/usr/bin/env python
from django.core.management import setup_environ
import settings
setup... | 6 | 2009-12-31T17:35:54Z | [
"python",
"django"
] |
Where should I place the one-time operation operation in the Django framework? | 1,986,060 | <p>I want to perform some one-time operations such as to start a background thread and populate a cache every 30 minutes as initialize action when the Django server is started, so it will not block user from visiting the website. Where should I place all this code in Django?</p>
<ul>
<li>Put them into the setting.py f... | 3 | 2009-12-31T16:38:20Z | 1,986,926 | <p>We put one-time startup scripts in the top-level <code>urls.py</code>. This is often where your admin bindings go -- they're one-time startup, also.</p>
<p>Some folks like to put these things in <code>settings.py</code> but that seems to conflate settings (which don't do much) with the rest of the site's code (whi... | 4 | 2009-12-31T20:07:15Z | [
"python",
"django"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 1,986,519 | <p>The reason "sign" is not included is that if we included every useful one-liner in the list of built-in functions, Python wouldn't be easy and practical to work with anymore.
If you use this function so often then why don't you do factor it out yourself? It's not like it's remotely hard or even tedious to do so.</p>... | -6 | 2009-12-31T18:23:05Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 1,986,718 | <p>"copysign" is defined by IEEE 754, and part of the C99 specification. That's why it's in Python. The function cannot be implemented in full by abs(x) * sign(y) because of how it's supposed to handle NaN values.</p>
<pre><code>>>> import math
>>> math.copysign(1, float("nan"))
1.0
>>> math... | 43 | 2009-12-31T19:12:11Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 1,986,776 | <p><strong>EDIT:</strong></p>
<p>Indeed there was a <a href="http://bugs.python.org/msg58786" rel="nofollow">patch</a> which included <code>sign()</code> in <a href="http://docs.python.org/library/math.html#math.copysign" rel="nofollow">math</a>, but it wasn't accepted, because they didn't agree on <a href="http://bug... | 145 | 2009-12-31T19:25:00Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 16,726,462 | <p>Another one liner for sign()</p>
<pre><code>sign = lambda x: (1, -1)[x<0]
</code></pre>
<p>If you want it to return 0 for x = 0:</p>
<pre><code>sign = lambda x: x and (1, -1)[x<0]
</code></pre>
| 20 | 2013-05-24T01:37:40Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 24,035,421 | <p>Since <code>cmp</code> has been <a href="https://docs.python.org/3.0/whatsnew/3.0.html">removed</a>, you can get the same functionality with</p>
<pre><code>def cmp(a, b):
return (a > b) - (a < b)
def sign(a):
return (a > 0) - (a < 0)
</code></pre>
<p>It works for <code>float</code>, <code>int<... | 13 | 2014-06-04T10:48:09Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 25,517,239 | <p>numpy has a sign function, and gives you a bonus of other functions as well. So: </p>
<pre><code>import numpy as np
x = np.sign(y)
</code></pre>
<p>Just be careful that the result is a numpy.float64: </p>
<pre><code>>>> type(np.sign(1.0))
<type 'numpy.float64'>
</code></pre>
<p>For things like js... | 4 | 2014-08-27T00:41:57Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 26,399,341 | <p>You dont need one, you can just use:</p>
<pre><code>If not number == 0:
sig = number/abs(number)
else:
sig = 0
</code></pre>
| 0 | 2014-10-16T08:13:02Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 28,850,174 | <p>Try running this, where x is any number</p>
<pre><code>int_sign = bool(x > 0) - bool(x < 0)
</code></pre>
| 4 | 2015-03-04T08:55:45Z | [
"python",
"language-design"
] |
Why doesn't Python have a sign function? | 1,986,152 | <p>I can't understand why Python doesn't have a <code>sign</code> function. It has an <code>abs</code> builtin (which I consider <code>sign</code>'s sister), but no <code>sign</code>.</p>
<p>In python 2.6 there is even a <code>copysign</code> function (in <a href="http://docs.python.org/library/math.html#math.copysign... | 115 | 2009-12-31T16:56:40Z | 39,016,428 | <p>Yes a correct <a href="http://en.wikipedia.org/wiki/Sign_function" rel="nofollow"><code>sign()</code></a> function should be at least in the math module - as it is in numpy. Because one frequently needs it for math oriented code.</p>
<p>But <code>math.copysign()</code> is also useful independently.</p>
<p><code>cm... | 1 | 2016-08-18T10:56:30Z | [
"python",
"language-design"
] |
How to query a contact for its PubSub using Python's xmpp? | 1,986,169 | <p>I'm writing a XMPP bot in Python (using <a href="http://xmpppy.sourceforge.net/" rel="nofollow">xmpppy</a>). I want (after user's request) check his PubSub status (mood, tune) and do something with it. How do I do that?</p>
<p>I know how to parse a stanza send by user when he changes status but I don't know how to ... | 0 | 2009-12-31T17:00:05Z | 1,998,266 | <p>Read <a href="http://xmpp.org/extensions/xep-0163.html" rel="nofollow">XEP-163</a>. Essentially, you need to implement the send-side of entity caps (<a href="http://xmpp.org/extensions/xep-0115.html" rel="nofollow">XEP-115</a>), so that you send your presence like this:</p>
<pre><code><presence from='romeo@exam... | 3 | 2010-01-04T08:43:41Z | [
"python",
"xmpp",
"bots"
] |
Holiday Calendars, File Formats, et al | 1,986,207 | <p>I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar.</p>
<p>Specifically, you might say <code>is_holiday (datetime.date, "USA")</code> which would answer whether the given date is a holiday for the calendar named "USA." </p>
<p>I recognize that there's no <em>trivia... | 9 | 2009-12-31T17:08:58Z | 1,986,232 | <p>Mozilla has a set of <a href="http://www.mozilla.org/projects/calendar/holidays.html" rel="nofollow">user-contributed holiday files</a> in .ics format. That may get you started. Unfortunately they go out to different date ranges. <a href="http://icalshare.com/index.php?topic=holidays" rel="nofollow">iCalShare</a> al... | 6 | 2009-12-31T17:14:40Z | [
"python",
"calendar"
] |
Holiday Calendars, File Formats, et al | 1,986,207 | <p>I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar.</p>
<p>Specifically, you might say <code>is_holiday (datetime.date, "USA")</code> which would answer whether the given date is a holiday for the calendar named "USA." </p>
<p>I recognize that there's no <em>trivia... | 9 | 2009-12-31T17:08:58Z | 1,986,290 | <p>The classic <a href="http://www.koders.com/python/fidFD6BA799CFCFD58CF7897C1E7D97CA364208953D.aspx?s=mdef%3A%22Martin%22#L364" rel="nofollow">holiday.py</a> module (part of an old <code>dateutil</code> package -- old enough to rely on module <code>time</code> rather than the relatively recent <code>datetime</code>!)... | 2 | 2009-12-31T17:25:19Z | [
"python",
"calendar"
] |
Holiday Calendars, File Formats, et al | 1,986,207 | <p>I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar.</p>
<p>Specifically, you might say <code>is_holiday (datetime.date, "USA")</code> which would answer whether the given date is a holiday for the calendar named "USA." </p>
<p>I recognize that there's no <em>trivia... | 9 | 2009-12-31T17:08:58Z | 1,986,512 | <p>Read Calendrical Calculations: <a href="http://emr.cs.uiuc.edu/home/reingold/calendar-book/index.shtml" rel="nofollow">http://emr.cs.uiuc.edu/home/reingold/calendar-book/index.shtml</a></p>
<p>The various rules for US holidays are provided in details, with algorithms.</p>
<p>The "Celebrate" business (moving the Ch... | 0 | 2009-12-31T18:21:09Z | [
"python",
"calendar"
] |
Holiday Calendars, File Formats, et al | 1,986,207 | <p>I'm looking for a way to figure out whether a given date is a "holiday," given some holiday calendar.</p>
<p>Specifically, you might say <code>is_holiday (datetime.date, "USA")</code> which would answer whether the given date is a holiday for the calendar named "USA." </p>
<p>I recognize that there's no <em>trivia... | 9 | 2009-12-31T17:08:58Z | 8,475,550 | <p>Another good, comprehensive source of holidays data is KDE's kholidays:</p>
<p>See more on <a href="http://community.kde.org/KDE_PIM/KHolidays" rel="nofollow">http://community.kde.org/KDE_PIM/KHolidays</a></p>
| 1 | 2011-12-12T14:08:35Z | [
"python",
"calendar"
] |
Finding all classes that derive from a given base class in python | 1,986,335 | <p>I'm looking for a way to get a list of all classes that derive from a particular base class in Python. </p>
<p>More specifically I am using Django and I have a abstract base Model and then several Models that derive from that base class...</p>
<pre><code>class Asset(models.Model):
name = models.CharField(max_l... | 0 | 2009-12-31T17:34:13Z | 1,986,390 | <p><code>Asset.__subclasses__()</code> gives the <em>immediate</em> subclasses of <code>Asset</code>, but whether that's sufficient depends on whether that <em>immediate</em> part is a problem for you -- if you want all descendants to whatever number of levels, you'll need recursive expansion, e.g.:</p>
<pre><code>def... | 9 | 2009-12-31T17:49:47Z | [
"python",
"django"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,986,739 | <p>For some simple data structures (eg. a stack), you can just use the builtin list to get your job done. With more complex structures (eg. a bloom filter), you'll have to implement them yourself using the primitives the language supports. </p>
<p>You should use the builtins if they serve your purpose really since the... | 14 | 2009-12-31T19:16:30Z | [
"python",
"data-structures"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,986,749 | <p>C/C++ data structure books are only attempting to teach you the underlying principles behind the various structures - they are generally not advising you to actually go out and re-invent the wheel by building your own library of stacks and lists.</p>
<p>Whether you're using Python, C++, C#, Java, whatever, you shou... | 8 | 2009-12-31T19:18:24Z | [
"python",
"data-structures"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,986,757 | <p>How Python handles objects at a low level isn't too strange anyway. <a href="http://docs.python.org/c-api/" rel="nofollow">This document</a> should disambiguate it a tad; it's basically all the pointer logic you're already familiar with.</p>
| 3 | 2009-12-31T19:20:19Z | [
"python",
"data-structures"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,986,761 | <p>It's not possible to implement something like a C++ vector in Python, since you don't have array primitives the way C/C++ do. However, anything more complicated can be implemented (efficiently) on top of it, including, but not limited to: linked lists, hash tables, multisets, bloom filters, etc.</p>
| 0 | 2009-12-31T19:20:59Z | [
"python",
"data-structures"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,987,019 | <p>Python gives you some powerful, highly optimized data structures, both as built-ins and as part of a few modules in the standard library (<code>list</code>s and <code>dict</code>s, of course, but also <code>tuple</code>s, <code>set</code>s, <code>array</code>s in module <a href="http://docs.python.org/library/array.... | 16 | 2009-12-31T20:37:01Z | [
"python",
"data-structures"
] |
Data Structures in Python | 1,986,712 | <p>All the books I've read on data structures so far seem to use C/C++, and make heavy use of the "manual" pointer control that they offer. Since Python hides that sort of memory management and garbage collection from the user is it even possible to implement efficient data structures in this language, and is there any... | 11 | 2009-12-31T19:10:12Z | 1,987,211 | <p>With Python you have access to a vast assortment of library modules written and debugged by other people. Odds are very good that somewhere out there, there is a module that does at least part of what you want, and odds are even good that it might be implemented in C for performance.</p>
<p>For example, if you nee... | 2 | 2009-12-31T21:38:52Z | [
"python",
"data-structures"
] |
How to use ipython's IPShellEmbed from within a running doctest | 1,986,805 | <p>Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since ctrl-C won't work. </p>
<pre><code>def some_function():
"""
>>> some_function()
... | 3 | 2009-12-31T19:30:47Z | 2,011,630 | <p>I emailed the ipython user group and got some help. There is now a <a href="https://bugs.launchpad.net/ipython/+bug/503698" rel="nofollow">support ticket</a> to get this feature fixed in future versions of ipython. Here is a code snippet with a workaround:</p>
<pre><code>import sys
from IPython.Shell import IPShel... | 0 | 2010-01-06T08:12:53Z | [
"python",
"unit-testing",
"ipython",
"doctest"
] |
Create a text file in python using values from a sqlite database | 1,986,978 | <p>I am trying to build a text file which is a combination of predefined strings and variable values which I would like to take from a pre-existing sqlite database. The general format of each line of the text file is as such:</p>
<p>constraint n: value i < value j</p>
<p>Where n is an integer which will increase... | 1 | 2009-12-31T20:24:30Z | 1,987,049 | <p>The simplest, unoptimized approach would be something like:</p>
<pre><code>import sqlite3
conn = sqlite3.connect('whatever.file')
c = conn.cursor
out = open('results.txt', 'w')
n = 1
for x in range(1, 30):
for y in range(1, 30):
c.execute('select value from i where row=? and column=?', (x, y))
i = c.fet... | 3 | 2009-12-31T20:46:09Z | [
"python",
"sqlite"
] |
Create a text file in python using values from a sqlite database | 1,986,978 | <p>I am trying to build a text file which is a combination of predefined strings and variable values which I would like to take from a pre-existing sqlite database. The general format of each line of the text file is as such:</p>
<p>constraint n: value i < value j</p>
<p>Where n is an integer which will increase... | 1 | 2009-12-31T20:24:30Z | 1,987,176 | <p>I believe you will be much happier if you use an ORM to access the data, rather than direct SQL as shown in Alex Martelli's answer. It seems like your database is very simple and an ORM will work well.</p>
<p>I suggest you try SQLAlchemy or Autumn. SQLAlchemy is powerful and flexible, and has been around for a lo... | 0 | 2009-12-31T21:31:00Z | [
"python",
"sqlite"
] |
cherrypy: accessing the URI/route parameters inside a tool hook? | 1,987,042 | <p>I have a tool hook setup for 'before_finalize' like so:</p>
<pre><code>def before_finalize():
d = cherrypy.response.body
location = '%s/views' % cherrypy.request.app.config['/']['application_path']
cherrypy.response.body = Template(file='%s/index.tmpl' % location).respond()
</code></pre>
<p>What I need... | 0 | 2009-12-31T20:44:57Z | 1,987,473 | <p>cherrypy.url will get you the complete URI, but I suspect that's not quite what you're looking for...why do you need it? If you're trying to form your 'location' variable based on the URI, you probably want path_info instead of the complete URI:</p>
<pre><code>location = '%s/views' % request.app.config['/']['applic... | 0 | 2009-12-31T23:27:14Z | [
"python",
"cherrypy"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 1,987,254 | <p>You don't need to use a recursive algorithm if you use os.walk. <a href="http://stackoverflow.com/questions/335078/whats-the-best-way-to-get-the-size-of-a-folder-and-all-the-files-inside-from-pyt">Please check this question</a>.</p>
<p>You should time both approaches, but this is supposed to be much faster:</p>
<p... | 1 | 2009-12-31T21:54:28Z | [
"python",
"windows",
"size",
"folder"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 1,987,455 | <p>I don't have a Windows box to test on at the moment, but the documentation states that
<code>win32file.FindFilesIterator</code> is "similar to <code>win32file.FindFiles</code>, but avoid the creation of the list for huge directories". Does that help?</p>
| 1 | 2009-12-31T23:18:17Z | [
"python",
"windows",
"size",
"folder"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 1,987,463 | <p>It's a whopper of a directory tree. As others have said, I'm not sure you can speed it up... not like that, cold w/o data. And that means...</p>
<p>If you can <em>cache</em> data, somehow (not sure what the actual implication is), then you could speed things up (I think... as always, measure, measure, measure).</... | 1 | 2009-12-31T23:21:18Z | [
"python",
"windows",
"size",
"folder"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 1,987,511 | <p>This jumps out at me:</p>
<pre><code>try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
</code></pre>
<p>Exception handling can add significant time to your algorithm. If you can specify the path differently, in a way that you always know is safe, and thus prevent the need to capt... | 1 | 2009-12-31T23:46:43Z | [
"python",
"windows",
"size",
"folder"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 1,987,732 | <p>A quick profiling of your code suggests that over 90% of the time is consumed in the <code>FindFilesW()</code> call alone. This means any improvements by tweaking the Python code would be minor.</p>
<p>Tiny tweaks (if you were to stick with FindFilesW) could include ensuring DIR_EXCLUDES is a set instead of a list... | 5 | 2010-01-01T02:28:20Z | [
"python",
"windows",
"size",
"folder"
] |
fast folder size calculation in Python on Windows | 1,987,119 | <p>I am looking for a fast way to calculate the size of a folder in Python on Windows. This is what I have so far:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add ... | 5 | 2009-12-31T21:08:49Z | 4,874,876 | <pre><code># Size of File Folder/Directory in MBytes
import os
# pick a folder you have ...
folder = 'D:\\zz1'
folder_size = 0
for (path, dirs, files) in os.walk(folder):
for file in files:
filename = os.path.join(path, file)
folder_size += os.path.getsize(filename)
print "Folder = %0.1f MB" % (folder_size... | 0 | 2011-02-02T13:15:18Z | [
"python",
"windows",
"size",
"folder"
] |
Determining if root logger is set to DEBUG level in Python? | 1,987,468 | <p>If I set the logging module to DEBUG with a command line parameter like this:</p>
<pre><code>if (opt["log"] == "debug"):
logging.basicConfig(level=logging.DEBUG)
</code></pre>
<p>How can I later tell if the logger was set to DEBUG? I'm writing a decorator that
will time a function if True flag is passed to it, ... | 32 | 2009-12-31T23:24:06Z | 1,987,479 | <p><a href="http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel" rel="nofollow">http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel</a></p>
| 3 | 2009-12-31T23:29:27Z | [
"python",
"logging",
"decorator"
] |
Determining if root logger is set to DEBUG level in Python? | 1,987,468 | <p>If I set the logging module to DEBUG with a command line parameter like this:</p>
<pre><code>if (opt["log"] == "debug"):
logging.basicConfig(level=logging.DEBUG)
</code></pre>
<p>How can I later tell if the logger was set to DEBUG? I'm writing a decorator that
will time a function if True flag is passed to it, ... | 32 | 2009-12-31T23:24:06Z | 1,987,484 | <pre><code>logging.getLogger().getEffectiveLevel()
</code></pre>
<p><code>logging.getLogger()</code> without arguments gets the root level logger.</p>
<p><a href="http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel">http://docs.python.org/library/logging.html#logging.Logger.getEffectiveLevel<... | 46 | 2009-12-31T23:31:11Z | [
"python",
"logging",
"decorator"
] |
Determining if root logger is set to DEBUG level in Python? | 1,987,468 | <p>If I set the logging module to DEBUG with a command line parameter like this:</p>
<pre><code>if (opt["log"] == "debug"):
logging.basicConfig(level=logging.DEBUG)
</code></pre>
<p>How can I later tell if the logger was set to DEBUG? I'm writing a decorator that
will time a function if True flag is passed to it, ... | 32 | 2009-12-31T23:24:06Z | 27,849,836 | <p>Actually, there's one better: use the code <code>logging.getLogger().isEnabledFor(logging.DEBUG)</code>. I found it while trying to understand what to do with the result of <code>getEffectiveLevel()</code>. :-)</p>
<p>(Below is an image of the code that the logging module itself uses.)</p>
<p><img src="http://i.st... | 20 | 2015-01-08T21:29:29Z | [
"python",
"logging",
"decorator"
] |
How to get Permissions to inherit from User's Groups? | 1,987,496 | <p>I'm trying to figure out Django Groups and the documentation is pretty bare on the site.</p>
<p>For example, you can use the decorator <a href="http://docs.djangoproject.com/en/dev/topics/auth/#the-permission-required-decorator" rel="nofollow"><code>permission_required()</code></a> to check the permissions, however... | 1 | 2009-12-31T23:39:22Z | 1,987,526 | <p>Django does automatically inherit permissions from groups. There may be some problem in your installation or database (such as using a custom permission without having done a syncdb), or you might be passing the wrong argument to the decorator.</p>
<p>If you have a model named <code>post</code> in an app named <co... | 4 | 2009-12-31T23:58:03Z | [
"python",
"django",
"django-authentication",
"django-permissions"
] |
What does this code mean: "print >> sys.stderr" | 1,987,626 | <pre><code>print >> sys.stderr, "Error in atexit._run_exitfuncs:"
</code></pre>
<p>Why print '>>' in front of <code>sys.stderr</code>?</p>
<p>Thanks.</p>
| 16 | 2010-01-01T01:00:23Z | 1,987,629 | <p>This syntax means writes to a file object (<code>sys.stderr</code> in this case) instead of standard output. <a href="http://docs.python.org/reference/simple_stmts.html#print">[Link]</a></p>
<p>In Python 3.0, <code>print</code> becomes a function instead of a statement: <a href="http://docs.python.org/library/funct... | 21 | 2010-01-01T01:02:29Z | [
"python",
"syntax"
] |
What does this code mean: "print >> sys.stderr" | 1,987,626 | <pre><code>print >> sys.stderr, "Error in atexit._run_exitfuncs:"
</code></pre>
<p>Why print '>>' in front of <code>sys.stderr</code>?</p>
<p>Thanks.</p>
| 16 | 2010-01-01T01:00:23Z | 1,987,631 | <p>From the <a href="http://docs.python.org/reference/simple_stmts.html">Python documentation</a>:</p>
<blockquote>
<p>print also has an extended form,
defined by the second portion of the
syntax described above. This form is
sometimes referred to as âprint
chevron.â In this form, the first
expression ... | 5 | 2010-01-01T01:03:10Z | [
"python",
"syntax"
] |
Sphinx 0.6.3: The languages module cannot be found | 1,987,658 | <p>I receive the following error message when I try to use the sphinx-quickstart generated make.bat command:</p>
<p><em>make html</em></p>
<blockquote>
<p>Error: The languages module cannot be found. Did you install Sphinx and its dependencies correctly?</p>
</blockquote>
<p>I tried running the sphinx-build comman... | 1 | 2010-01-01T01:24:43Z | 1,988,686 | <p>Grepping through the sphinx package for "languages", the only relevant import is:</p>
<pre><code>/usr/lib/pymodules/python2.5/sphinx/environment.py:from docutils.parsers.rst.languages import en as english
</code></pre>
<p>So most likely there's something wrong with your docutils installation. Admittedly, the error... | 0 | 2010-01-01T14:08:19Z | [
"python",
"python-sphinx"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 1,987,705 | <p>This sounds like you're using numpy.</p>
<p>If that's the case, you can add:</p>
<pre><code>set_printoptions(threshold=nan)
</code></pre>
<p>That will disable the corner printing. For more information, see this <a href="http://www.scipy.org/Tentative_NumPy_Tutorial#head-a476da892560e3bf994ec9b363e169b6467cd5a4">... | 34 | 2010-01-01T02:02:41Z | [
"python",
"arrays",
"numpy"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 1,988,024 | <p>To clarify on Reed's reply</p>
<pre><code>import numpy
numpy.set_printoptions(threshold=numpy.nan)
</code></pre>
<p>Note that the reply as given above works with an initial 'from numpy import *', which is not advisable.
This also works for me</p>
<pre><code>numpy.set_printoptions(threshold='nan')
</code></pre>
... | 155 | 2010-01-01T06:26:05Z | [
"python",
"arrays",
"numpy"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 24,542,498 | <p>Here is a one-off way to do this, which is useful if you don't want to change your default settings:</p>
<pre><code>def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold='nan')
pprint(*args, **kwargs)
numpy.set_printoptions... | 21 | 2014-07-02T23:08:50Z | [
"python",
"arrays",
"numpy"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 26,018,905 | <pre><code>import numpy as np
np.set_printoptions(threshold=np.inf)
</code></pre>
<p>I suggest using <code>np.inf</code> instead of <code>np.nan</code> which is suggested by others. They both work for your purpose, but by setting the threshold to "infinity" it is obvious to everybody reading your code what you mean. H... | 38 | 2014-09-24T14:01:51Z | [
"python",
"arrays",
"numpy"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 29,825,258 | <p>The previous answers are the correct ones, but as a weeker alternative you can transform into a list:</p>
<pre><code>>>> numpy.arange(100).reshape(25,4).tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21,
22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, ... | 10 | 2015-04-23T13:40:50Z | [
"python",
"arrays",
"numpy"
] |
Print the full numpy array | 1,987,694 | <p>When I print a numpy array, I get a truncated representation, but I want the full array. </p>
<p>Is there any way to do this?</p>
<p><strong>Examples:</strong></p>
<pre><code>>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[... | 146 | 2010-01-01T01:51:46Z | 39,405,221 | <p>Using a context manager as <a href="http://stackoverflow.com/a/24542498/5584077">Paul Price</a> sugggested</p>
<pre><code>import numpy as np
class fullprint:
'''context manager for printing full numpy arrays'''
def __enter__(self):
self.opt = np.get_printoptions()
np.set_printoptions(thre... | 2 | 2016-09-09T06:33:55Z | [
"python",
"arrays",
"numpy"
] |
Gracefully handling "MySQL has gone away" | 1,987,701 | <p>I'm writing a small database adapter in Python, mostly for fun. I'm trying to get the code to gracefully recover from a situation where the MySQL connection "goes away," aka <code>wait_timeout</code> is exceeded. I've set <code>wait_timeout</code> at <code>10</code> so I can try this.</p>
<p>Here's my code:</p>
... | 17 | 2010-01-01T01:56:24Z | 1,987,756 | <p>Can't see from the code, but my guess would be that the <code>db._get_cxn()</code> method is doing some kind of connection pooling and returning the existing connection object instead of making a new one. Is there not a call you can make on <code>db</code> to flush the existing useless connection? (And should you re... | 12 | 2010-01-01T02:46:49Z | [
"python",
"mysql"
] |
Any difference between 'b' and 'c'? | 1,987,703 | <pre><code>class a(object):
b = 'bbbb'
def __init__(self):
self.c = 'cccc'
</code></pre>
<p>I think they are the same; is there any difference?</p>
| 1 | 2010-01-01T02:01:42Z | 1,987,713 | <p>Yes, there is a difference.</p>
<p><code>b</code> is a class variable... one that is shared by all instances of <code>a</code>, while <code>c</code> is an instance variable which will exist independantly for each instance.</p>
| 9 | 2010-01-01T02:08:56Z | [
"python",
"class"
] |
Any difference between 'b' and 'c'? | 1,987,703 | <pre><code>class a(object):
b = 'bbbb'
def __init__(self):
self.c = 'cccc'
</code></pre>
<p>I think they are the same; is there any difference?</p>
| 1 | 2010-01-01T02:01:42Z | 1,987,714 | <p>'b' is a class attribute, set directly on the class object 'a'. 'c' is an instance attribute, set directly on self. While <code>self.b</code> will find <code>a.b</code> due to how lookup works, you cannot use <code>a.c</code> (as it doesn't exist).</p>
| 2 | 2010-01-01T02:09:28Z | [
"python",
"class"
] |
Any difference between 'b' and 'c'? | 1,987,703 | <pre><code>class a(object):
b = 'bbbb'
def __init__(self):
self.c = 'cccc'
</code></pre>
<p>I think they are the same; is there any difference?</p>
| 1 | 2010-01-01T02:01:42Z | 1,987,723 | <p><code>b</code> is a class variable, while <code>c</code> is a instance variable.</p>
<pre><code>>>> a.b
'bbbb'
>>> a.c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'a' has no attribute 'c'
>>> a().b
'bbbb'
>>> a(... | 5 | 2010-01-01T02:19:39Z | [
"python",
"class"
] |
Any difference between 'b' and 'c'? | 1,987,703 | <pre><code>class a(object):
b = 'bbbb'
def __init__(self):
self.c = 'cccc'
</code></pre>
<p>I think they are the same; is there any difference?</p>
| 1 | 2010-01-01T02:01:42Z | 1,987,725 | <pre><code>>>> class a(object):
... b = 'bbbb'
... def __init__(self):
... self.c = 'cccc'
...
>>> a1=a()
>>> a2=a()
>>> a1.b
'bbbb'
>>> a2.b
'bbbb'
>>> a1.c='dddd'
>>> a1.c
'dddd'
>>> a2.c
'cccc'
>>> a.b= 'common'
>>... | 2 | 2010-01-01T02:21:01Z | [
"python",
"class"
] |
Convert an image to RGBA mode with python | 1,987,848 | <p>I tried the code listed at <a href="http://stackoverflow.com/questions/765736/using-pil-to-make-all-white-pixels-transparent">Using PIL to make all white pixels transparent</a> to convert some .ico files to .png images with transparent background.</p>
<p>It doesn't work on all .ico images, some time it just outputs... | 0 | 2010-01-01T04:11:33Z | 1,988,238 | <p>i tried that script on all icons you put there . and it worked fine. some times it represents transparency with a black background . to make sure that the background is fully transparent . open it in photoshop.
<strong>Edit</strong> : make sure that your icon has an alpha channel.</p>
| 0 | 2010-01-01T09:20:55Z | [
"python",
"image",
"python-imaging-library",
"transparent"
] |
Why can @decorator not decorate a staticmethod or a classmethod? | 1,987,919 | <p>Why can <code>decorator</code> not decorate a staticmethod or a classmethod?</p>
<pre><code>from decorator import decorator
@decorator
def print_function_name(function, *args):
print '%s was called.' % function.func_name
return function(*args)
class My_class(object):
@print_function_name
@classmet... | 16 | 2010-01-01T05:06:03Z | 1,987,983 | <p>Is this what you wanted?</p>
<pre><code>def print_function_name(function):
def wrapper(*args):
print('%s was called.' % function.__name__)
return function(*args)
return wrapper
class My_class(object):
@classmethod
@print_function_name
def get_dir(cls):
return dir(cls)
... | 3 | 2010-01-01T05:48:15Z | [
"python",
"decorator"
] |
Why can @decorator not decorate a staticmethod or a classmethod? | 1,987,919 | <p>Why can <code>decorator</code> not decorate a staticmethod or a classmethod?</p>
<pre><code>from decorator import decorator
@decorator
def print_function_name(function, *args):
print '%s was called.' % function.func_name
return function(*args)
class My_class(object):
@print_function_name
@classmet... | 16 | 2010-01-01T05:06:03Z | 1,989,025 | <p>It works when <code>@classmathod</code> and <code>@staticmethod</code> are the top-most decorators:</p>
<pre><code>from decorator import decorator
@decorator
def print_function_name(function, *args):
print '%s was called.' % function.func_name
return function(*args)
class My_class(object):
@classmetho... | 17 | 2010-01-01T16:53:56Z | [
"python",
"decorator"
] |
Why can @decorator not decorate a staticmethod or a classmethod? | 1,987,919 | <p>Why can <code>decorator</code> not decorate a staticmethod or a classmethod?</p>
<pre><code>from decorator import decorator
@decorator
def print_function_name(function, *args):
print '%s was called.' % function.func_name
return function(*args)
class My_class(object):
@print_function_name
@classmet... | 16 | 2010-01-01T05:06:03Z | 6,208,458 | <p><code>classmethod</code> and <code>staticmethod</code> return <a href="https://docs.python.org/2/howto/descriptor.html">descriptor objects</a>, not functions. Most decorators are not designed to accept descriptors.</p>
<p>Normally, then, you must apply <code>classmethod</code> and <code>staticmethod</code> last wh... | 14 | 2011-06-01T22:23:45Z | [
"python",
"decorator"
] |
Poor numpy.cross() performance | 1,988,091 | <p>I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance.</p>
<p>However, the result from the following performance tests were quite s... | 3 | 2010-01-01T07:12:17Z | 1,988,145 | <p>Try this with larger arrays. I think that just the cost of calling the methods of <code>numpy</code> here overruns the simple several list accesses required by the Python version. If you deal with larger arrays, I think you'll see large wins for <code>numpy</code>.</p>
| 6 | 2010-01-01T07:48:49Z | [
"python",
"performance",
"numpy"
] |
Poor numpy.cross() performance | 1,988,091 | <p>I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance.</p>
<p>However, the result from the following performance tests were quite s... | 3 | 2010-01-01T07:12:17Z | 1,988,312 | <p>You can see the source code yourself here: <a href="http://www.google.com/codesearch/p?hl=en#5mAq98l-MUw/trunk/dnumpy/numpy/core/numeric.py&q=cross%20package:numpy&sa=N&cd=1&ct=rc">http://www.google.com/codesearch/p?hl=en#5mAq98l-MUw/trunk/dnumpy/numpy/core/numeric.py&q=cross%20package:numpy&... | 5 | 2010-01-01T10:37:24Z | [
"python",
"performance",
"numpy"
] |
Poor numpy.cross() performance | 1,988,091 | <p>I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance.</p>
<p>However, the result from the following performance tests were quite s... | 3 | 2010-01-01T07:12:17Z | 1,990,689 | <p>To reduce the numpy calling overhead, you might try using cython as an intermediate to call into the numpy functions.</p>
<p>See <a href="http://conference.scipy.org/proceedings/SciPy2009/paper_2/" rel="nofollow">Fast numerical computations with Cython (SciPy 2009)</a> for details.</p>
| 1 | 2010-01-02T05:29:52Z | [
"python",
"performance",
"numpy"
] |
Poor numpy.cross() performance | 1,988,091 | <p>I've been doing some performance testing in order to improve the performance of a pet project I'm writing. It's a very number-crunching intensive application, so I've been playing with Numpy as a way of improving computational performance.</p>
<p>However, the result from the following performance tests were quite s... | 3 | 2010-01-01T07:12:17Z | 24,623,124 | <p>Excellent post! I think that the comparison is not actually fair. Batch Cross Product gives an array containing the cross products of all vectors while Python Cross Product gives one vector at a time. If you need to compute all cross products at once of course Batch is better, but if you need to compute every cross ... | 0 | 2014-07-08T03:13:29Z | [
"python",
"performance",
"numpy"
] |
Managing Setup code with TimeIt | 1,988,127 | <p>As part of a pet project of mine, I need to test the performance of various different implementations of my code in Python. I anticipate this to be something I do alot of, and I want to try to make the code I write to serve this aim as easy to update and modify as possible.</p>
<p>It's still in its infancy at the m... | 1 | 2010-01-01T07:39:08Z | 1,988,131 | <p>Use triple quotes """</p>
<pre><code>setup_code = """
from PerformanceTests.Vectors import NaiveVector
left = NaiveVector([1,0,0])
right = NaiveVector([0,1,0])
"""
</code></pre>
<p>Another interesting method is provided in the docs of <code>timeit</code>:</p>
<pre><code>def test():
"Stupid test function... | 3 | 2010-01-01T07:42:38Z | [
"performance",
"python",
"timeit"
] |
Managing Setup code with TimeIt | 1,988,127 | <p>As part of a pet project of mine, I need to test the performance of various different implementations of my code in Python. I anticipate this to be something I do alot of, and I want to try to make the code I write to serve this aim as easy to update and modify as possible.</p>
<p>It's still in its infancy at the m... | 1 | 2010-01-01T07:39:08Z | 1,989,006 | <p>Timing code is fine, but it will still leave you guessing what's going on.</p>
<p>To find out what's actually going on, manually pause it a few random times in the debugger, and examine the call stack.</p>
<p>For example, in the code that is 30x slower in one implementation than in another, each sample of the stac... | 0 | 2010-01-01T16:45:12Z | [
"performance",
"python",
"timeit"
] |
Why django contains a lot of '__init__.py'? | 1,988,149 | <p>Many directories in a django project contain a <code>__init__.py</code> and I think it will be used as initialization for something. Where is this <code>__init__.py</code> used?</p>
| 3 | 2010-01-01T07:53:08Z | 1,988,152 | <p>Your question is not clear. What exactly are you asking? </p>
<p>The file <code>__init__.py</code> is there so your folder can be defined as <a href="http://www.network-theory.co.uk/docs/pytut/Packages.html">a package</a>, which lets you do things like:</p>
<pre><code>from myapp.models import Something
</code></pr... | 7 | 2010-01-01T07:57:11Z | [
"python",
"django",
"module"
] |
Why django contains a lot of '__init__.py'? | 1,988,149 | <p>Many directories in a django project contain a <code>__init__.py</code> and I think it will be used as initialization for something. Where is this <code>__init__.py</code> used?</p>
| 3 | 2010-01-01T07:53:08Z | 1,989,096 | <p>Python doesn't take every subdirectory of every directory in <code>sys.path</code> to necessarily be a <em>package</em>: only those with a file called <code>__init__.py</code>. Consider the following shell session:</p>
<pre><code>$ mkdir adir
$ echo 'print "hello world"' > adir/helo.py
$ python -c 'import adir.h... | 10 | 2010-01-01T17:24:06Z | [
"python",
"django",
"module"
] |
Qt Python - report in toolbox: QTextDocument and QPainter | 1,988,150 | <p>I want to build multiple documents report using toolbox. Two pages is an option get a start. Formatting is ok, and can be worked latter.</p>
<p>I tried using QTextDocument in Html, and alternatively QPainter.</p>
<p>Of course, to make a test and keep things simple, I just ask in Qt to show the report title display... | 0 | 2010-01-01T07:55:32Z | 1,988,961 | <p>For page1, the document needs to be displayed by a widget. Add the following to that function</p>
<pre><code> textEdit = QtGui.QTextEdit(self.page1)
textEdit.setDocument(document)
layout = QtGui.QVBoxLayout(self.page1)
layout.addWidget(textEdit)
</code></pre>
<p>For page2, painting on a widget must ... | 1 | 2010-01-01T16:18:05Z | [
"python",
"html",
"qt",
"reportviewer",
"qpainter"
] |
why my 'time' class has not 'tzset' attribute | 1,988,182 | <p>my code:</p>
<pre><code>import time
print hasattr(time.tzset)#error
</code></pre>
<p>and why someone do this like next:</p>
<pre><code>if hasattr(time, 'tzset'):
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
... | 2 | 2010-01-01T08:28:07Z | 1,988,185 | <p>Either you have a local module that is shadowing the stock <code>time</code> module, you're using a version of Python older than 2.3, or you're running Python on Windows.</p>
| 1 | 2010-01-01T08:31:01Z | [
"python",
"syntax"
] |
why my 'time' class has not 'tzset' attribute | 1,988,182 | <p>my code:</p>
<pre><code>import time
print hasattr(time.tzset)#error
</code></pre>
<p>and why someone do this like next:</p>
<pre><code>if hasattr(time, 'tzset'):
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
... | 2 | 2010-01-01T08:28:07Z | 1,988,724 | <p>Your use of <code>hasattr</code> is wrong. The correct syntax is shown in your second snippet.</p>
<p><code>hasattr</code> takes two arguments - an object, and a string represent the attribute you want to check for. The way you've done it, Python will try to evaluate <code>time.tzset</code> first, before passing it... | 3 | 2010-01-01T14:25:39Z | [
"python",
"syntax"
] |
why my 'time' class has not 'tzset' attribute | 1,988,182 | <p>my code:</p>
<pre><code>import time
print hasattr(time.tzset)#error
</code></pre>
<p>and why someone do this like next:</p>
<pre><code>if hasattr(time, 'tzset'):
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
... | 2 | 2010-01-01T08:28:07Z | 1,989,104 | <p>See <a href="http://docs.python.org/library/time.html?highlight=tzset#time.tzset" rel="nofollow">the docs</a> for <code>tzset</code>: they clearly say</p>
<blockquote>
<p>Availability: Unix.</p>
</blockquote>
<p>so you would have it in, say, MacOSX, Solaris, or Linux, but not on Windows.</p>
<p>Also: there is n... | 4 | 2010-01-01T17:29:22Z | [
"python",
"syntax"
] |
Asyncore not working properly with Tkinter GUI | 1,988,286 | <p>At this point I'm still a noob when it comes to GUI and network programming so I'm hoping this will be a very simple fix. I've got a very basic understanding of the tkinter and asyncore modules having built a handful of programs in each of them, however I'm having trouble using both of them together in a program. I ... | 0 | 2010-01-01T10:14:08Z | 1,988,443 | <p>There are several problems here, and I'm not sure which one it is.</p>
<p>asyncore.loop is a function that never returns, when things are working properly. root.mainloop is probably a function that never returns until you close the window. So things are likely to go wrong because at some point one loop will be star... | 2 | 2010-01-01T12:19:25Z | [
"python",
"tkinter",
"asyncore"
] |
Asyncore not working properly with Tkinter GUI | 1,988,286 | <p>At this point I'm still a noob when it comes to GUI and network programming so I'm hoping this will be a very simple fix. I've got a very basic understanding of the tkinter and asyncore modules having built a handful of programs in each of them, however I'm having trouble using both of them together in a program. I ... | 0 | 2010-01-01T10:14:08Z | 1,989,131 | <p>See <a href="http://code.activestate.com/recipes/82965/" rel="nofollow">this recipe</a> by Jacob Hallén showing how to use asyncore and Tkinter together (basically by means of a threading trick). (It's also expanded as recipe 9.6 in the Python Cookbook's first printed edition, and as 11.4 in the second edition).</... | 0 | 2010-01-01T17:40:00Z | [
"python",
"tkinter",
"asyncore"
] |
exception not getting caught when not in the right package in python | 1,988,475 | <p>EDIT:
OK, I managed to isolate the bug and the exact, complete code to to reproduce it. But it appears either something that's by design, or a bug in python.</p>
<p>Create two sibling packages: <code>admin</code> & <code>General</code>, each with it's own <code>__init__.py</code>, of course.
In the package <cod... | 4 | 2010-01-01T12:33:52Z | 1,988,556 | <p>I can only see two reasons</p>
<ol>
<li><p>You have two different Exception classes with same name<br>
<strong>Edit</strong>: I think culprit is this part because you import Exception class two ways</p>
<ul>
<li><strong>from RunStoppedException import RunStoppedException</strong> </li>
<li><strong>from admin.RunSt... | 0 | 2010-01-01T13:09:51Z | [
"python",
"exception",
"package"
] |
exception not getting caught when not in the right package in python | 1,988,475 | <p>EDIT:
OK, I managed to isolate the bug and the exact, complete code to to reproduce it. But it appears either something that's by design, or a bug in python.</p>
<p>Create two sibling packages: <code>admin</code> & <code>General</code>, each with it's own <code>__init__.py</code>, of course.
In the package <cod... | 4 | 2010-01-01T12:33:52Z | 1,988,628 | <p>Works fine for me:</p>
<pre><code>[/tmp] ls admin/
RunStoppedException.py __init__.py test.py
RunStoppedException.pyc __init__.pyc
[/tmp] ls General/
__init__.py __init__.pyc test02.py test02.pyc
[/tmp] python -m admin.test
right
[/tmp]
</code></pre>
<p>Running on:</p>
<pre><code>Python 2.6.4 Stackless ... | 0 | 2010-01-01T13:45:11Z | [
"python",
"exception",
"package"
] |
exception not getting caught when not in the right package in python | 1,988,475 | <p>EDIT:
OK, I managed to isolate the bug and the exact, complete code to to reproduce it. But it appears either something that's by design, or a bug in python.</p>
<p>Create two sibling packages: <code>admin</code> & <code>General</code>, each with it's own <code>__init__.py</code>, of course.
In the package <cod... | 4 | 2010-01-01T12:33:52Z | 1,988,648 | <pre><code>import admin.RunStoppedException
</code></pre>
<p>This is an ambiguous relative import. Do you mean <code>RunStoppedException</code> from the <code>admin</code> top-level module? Or from <code>mypackage.admin</code> when you're in a package? If your current working directory (which is added to the module se... | 7 | 2010-01-01T13:53:32Z | [
"python",
"exception",
"package"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,988,522 | <p>If I were you I would take the time to learn how to properly use the various data structures available in Python instead of looking for things that are similar to what you know from C++. </p>
<p>It's not like you're looking for something fancy, just working with some data structures. In that case I would refer you... | 13 | 2010-01-01T12:55:43Z | [
"c++",
"python",
"stl"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,988,584 | <p>For linked-list-like operations people usually use <a href="http://docs.python.org/dev/library/collections.html#deque-objects" rel="nofollow"><code>collections.deque</code></a>.</p>
<p>What operations do you need to perform fast? Bisection? Insertion?</p>
| 2 | 2010-01-01T13:26:42Z | [
"c++",
"python",
"stl"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,988,594 | <p>Python STL (in hope of leveraging my years of STL experience) - Start with the collections ABC's to learn what Python has. <a href="http://docs.python.org/library/collections.html" rel="nofollow">http://docs.python.org/library/collections.html</a></p>
<p>Python linked lists. Python lists have all the features yo... | 1 | 2010-01-01T13:31:50Z | [
"c++",
"python",
"stl"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,988,889 | <p>The design of Python is quite intentionally "you can use just a few data structures (arrays and hash tables) for whatever you want to do, and if that isn't fast enough there's always C".</p>
<p>Python's standard library doesn't have a sorted-list data structure like <code>std::set</code>. You can <a href="http://py... | 1 | 2010-01-01T15:42:16Z | [
"c++",
"python",
"stl"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,989,221 | <p>Python's "lists" are not <em>linked</em> lists -- they're like Java <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html">ArrayList</a>s or C++'s <code>std::vector</code>s, i.e., in lower-level terms, a resizable compact array of pointers.</p>
<p>A good "advanced tutorial" on such subjects is H... | 12 | 2010-01-01T18:21:37Z | [
"c++",
"python",
"stl"
] |
Python lists with STL like interface | 1,988,484 | <p>I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:</p>
<ol>
<li>Python STL (... | 1 | 2010-01-01T12:39:46Z | 1,990,227 | <p>I would say that your issues go beyond just STL porting. Since the list, dict, and set data structures, which are bolted on to C++ via the STL, are native to core Python, then their usage is incorporated into common Python code idioms. If you want to give Google another shot, try looking for references for "Python... | 2 | 2010-01-02T01:06:07Z | [
"c++",
"python",
"stl"
] |
Variables and function | 1,988,533 | <p>Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want.
For example:</p>
<pre><code>def main():
# Inputing the x-value for the first start point of the line
start_point_x_1()
# Inputing the x-... | 0 | 2010-01-01T12:59:34Z | 1,988,543 | <p>Google around for 'memoization' which is the term for what you are trying to do. I don't know whether or how Python supports it, but since it's homework I guess you might want to look further.</p>
| 0 | 2010-01-01T13:02:02Z | [
"python"
] |
Variables and function | 1,988,533 | <p>Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want.
For example:</p>
<pre><code>def main():
# Inputing the x-value for the first start point of the line
start_point_x_1()
# Inputing the x-... | 0 | 2010-01-01T12:59:34Z | 1,988,548 | <p>Change</p>
<pre><code>start_point_x_1()
</code></pre>
<p>to</p>
<pre><code>x0 = start_point_x_1()
</code></pre>
<p>Similarly, do</p>
<pre><code>x2 = end_point_x_2()
</code></pre>
<p>and finally:</p>
<pre><code>first_calculated_point()
</code></pre>
<p>becomes</p>
<pre><code>first_calculated_point(x0, x2)
</... | 3 | 2010-01-01T13:06:40Z | [
"python"
] |
Variables and function | 1,988,533 | <p>Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want.
For example:</p>
<pre><code>def main():
# Inputing the x-value for the first start point of the line
start_point_x_1()
# Inputing the x-... | 0 | 2010-01-01T12:59:34Z | 1,988,563 | <p>Why are you calling <code>start_point_x_1</code> and <code>end_point_x_2</code> from both <code>main</code> and <code>first_calculated_point</code>?</p>
<p>You could change the definitions of <code>main</code></p>
<pre><code>def main():
first_calculated_point()
</code></pre>
<p>and <code>first_calculated_point<... | 0 | 2010-01-01T13:12:45Z | [
"python"
] |
Variables and function | 1,988,533 | <p>Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want.
For example:</p>
<pre><code>def main():
# Inputing the x-value for the first start point of the line
start_point_x_1()
# Inputing the x-... | 0 | 2010-01-01T12:59:34Z | 1,988,569 | <p>You can use 'memoization' that is cache the result of functions based on function arguments, for that you can write a decorator so that you can decorate whatever functions you think need that behaviour but if you problem is as simple as your code is why not assign it a variable an d used that assigned values?</p>
<... | 0 | 2010-01-01T13:17:12Z | [
"python"
] |
How do I get datetime from date object python? | 1,988,599 | <p>How do I get datetime from date object python?</p>
<p>I think of</p>
<pre><code>import datetime as dt
today = dt.date.today()
date_time = dt.datetime(today.year, today.month, today.day)
</code></pre>
<p>Any easier solution?</p>
| 3 | 2010-01-01T13:34:40Z | 1,988,608 | <p>There are a few ways to do this:</p>
<pre><code>mydatetime = datetime.datetime(d.year, d.month, d.day)
</code></pre>
<p>or</p>
<pre><code>mydatetime = datetime.combine(d, datetime.time())
</code></pre>
<p>or </p>
<pre><code>mydatetime = datetime.datetime.fromordinal(d.toordinal())
</code></pre>
<p>I think the ... | 9 | 2010-01-01T13:38:14Z | [
"python",
"datetime"
] |
How do I get datetime from date object python? | 1,988,599 | <p>How do I get datetime from date object python?</p>
<p>I think of</p>
<pre><code>import datetime as dt
today = dt.date.today()
date_time = dt.datetime(today.year, today.month, today.day)
</code></pre>
<p>Any easier solution?</p>
| 3 | 2010-01-01T13:34:40Z | 1,988,611 | <p>Try this:</p>
<pre><code>import datetime
print 'Now :', datetime.datetime.now()
print 'Today :', datetime.datetime.today()
print 'UTC Now:', datetime.datetime.utcnow()
d = datetime.datetime.now()
for attr in [ 'year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond']:
print attr, ':', getattr(d, a... | 1 | 2010-01-01T13:39:39Z | [
"python",
"datetime"
] |
Reading values with raw_input in Python | 1,988,733 | <p>I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason?</p>
<p>E.g.:</p>
<pre><code>def main():
eq_of_form()
value_of_a()
value_of_b()
value_of_c()
value_of_m()
value_of_n()
value_of_x()
area_under_graph()
def eq_of_form():
... | 0 | 2010-01-01T14:29:06Z | 1,988,739 | <p>Your code at the start is wrong. You need to assign your variables after you read the user input:</p>
<pre><code>value_of_a()
</code></pre>
<p>should be:</p>
<pre><code>a = value_of_a()
</code></pre>
<p>It is also unnecessary to write a separate function for inputting each variable. You could instead have a func... | 5 | 2010-01-01T14:31:10Z | [
"python"
] |
Reading values with raw_input in Python | 1,988,733 | <p>I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason?</p>
<p>E.g.:</p>
<pre><code>def main():
eq_of_form()
value_of_a()
value_of_b()
value_of_c()
value_of_m()
value_of_n()
value_of_x()
area_under_graph()
def eq_of_form():
... | 0 | 2010-01-01T14:29:06Z | 1,988,752 | <p><code>/</code> does Integer division in Python2, this means <code>a/b</code> is the biggest integer <code>c</code> with <code>c*b <=a</code>, so <code>7/3</code> is indeed <code>2</code>. You want floats, so you need to use them .. replace all the <code>int</code> with <code>float</code> in your code.</p>
<p>You... | 3 | 2010-01-01T14:35:57Z | [
"python"
] |
Reading values with raw_input in Python | 1,988,733 | <p>I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason?</p>
<p>E.g.:</p>
<pre><code>def main():
eq_of_form()
value_of_a()
value_of_b()
value_of_c()
value_of_m()
value_of_n()
value_of_x()
area_under_graph()
def eq_of_form():
... | 0 | 2010-01-01T14:29:06Z | 1,988,755 | <p>In Python 2.x, dividing an integer by another integer results in an integer. Either use <code>from __future__ import division</code>, or turn one of the integers into a float by passing it to <code>float()</code>.</p>
| 0 | 2010-01-01T14:36:42Z | [
"python"
] |
Reading values with raw_input in Python | 1,988,733 | <p>I am trying to do integration in Python but whenever I key in a value my outputs always results in 0. What the reason?</p>
<p>E.g.:</p>
<pre><code>def main():
eq_of_form()
value_of_a()
value_of_b()
value_of_c()
value_of_m()
value_of_n()
value_of_x()
area_under_graph()
def eq_of_form():
... | 0 | 2010-01-01T14:29:06Z | 1,988,757 | <p>The issue is that you're using integer arithmetic - see all those <code>int</code> calls you've got everywhere. Integer arithmetic (in Python 2.x) will only ever return integers, so you'll never get 2.33, only 2. </p>
<p>Use <code>float()</code> instead of <code>int()</code> and things should work.</p>
| 0 | 2010-01-01T14:38:25Z | [
"python"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 1,988,826 | <p>Memoization effectively refers to remembering ("memoization" -> "memorandum" -> to be remembered) results of method calls based on the method inputs and then returning the remembered result rather than computing the result again. You can think of it as a cache for method results. For further details, see page 387 fo... | 214 | 2010-01-01T15:05:05Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 1,988,828 | <p>Memoization is keeping the results of expensive calculations and returning the cached result rather than continuously recalculating it. </p>
<p>Here's an example:</p>
<pre><code>def doSomeExpensiveCalculation(self, input):
if input not in self.cache:
<do expensive calculation>
self.cache[... | 14 | 2010-01-01T15:05:56Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 1,991,818 | <p>The other answers cover what it is quite well. I'm not repeating that. Just some points that might be useful to you. </p>
<p>Usually, memoisation is an operation you can apply on any function that computes something (expensive) and returns a value. Because of this, it's often implemented as a <a href="http://www.py... | 51 | 2010-01-02T14:55:06Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 7,004,393 | <p>Memoization is the conversion of functions into data structures. Usually one wants the conversion to occur incrementally and lazily (on demand of a given domain element--or "key"). In lazy functional languages, this lazy conversion can happen automatically, and thus memoization can be implemented without (explicit) ... | 4 | 2011-08-09T23:49:51Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 13,865,482 | <p>Memoization is basically saving the results of past operations done with recursive algorithms in order to reduce the need to traverse the recursion tree if the same calculation is required at a later stage.</p>
<p>see <a href="http://scriptbucket.wordpress.com/2012/12/11/introduction-to-memoization/" rel="nofollow"... | 6 | 2012-12-13T17:45:55Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 14,183,354 | <pre><code>cache = {}
def fib(n):
if n <= 1:
return n
else:
if n not in cache:
cache[n] = fib(n-1) + fib(n-2)
return cache[n]
</code></pre>
| 2 | 2013-01-06T15:02:23Z | [
"python",
"memoization"
] |
What is memoization and how can I use it in Python? | 1,988,804 | <p>I just started Python and I've got no idea what <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a> is and how to use it. Also, may I have a simplified example?</p>
| 215 | 2010-01-01T14:55:48Z | 14,731,729 | <p>New to Python 3.2 is <a href="http://docs.python.org/3/library/functools.html"><code>functools.lru_cache</code></a>. By default, it only caches the 128 most recently used calls, but you can set the <code>maxsize</code> to <code>None</code> to indicate that the cache should never expire:</p>
<pre class="lang-python ... | 98 | 2013-02-06T14:43:14Z | [
"python",
"memoization"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.