Popular recipes tagged "meta:loc=104"http://code.activestate.com/recipes/tags/meta:loc=104/2016-04-17T01:22:51-07:00ActiveState Code RecipesLRU dictionary (Python)
2016-04-17T01:22:01-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580644-lru-dictionary/
<p style="color: grey">
Python
recipe 580644
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expiration/">expiration</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
LRU dictionary (Python)
2016-04-17T01:22:51-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580645-lru-dictionary/
<p style="color: grey">
Python
recipe 580645
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expires/">expires</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
where() function for generator expressions (Python 3) (Python)
2014-10-05T04:10:21-07:00Alan Cristhian Ruizhttp://code.activestate.com/recipes/users/4186199/http://code.activestate.com/recipes/578946-where-function-for-generator-expressions-python-3/
<p style="color: grey">
Python
recipe 578946
by <a href="/recipes/users/4186199/">Alan Cristhian Ruiz</a>
(<a href="/recipes/tags/generators/">generators</a>).
Revision 3.
</p>
<p>Function that work like an "where statement" for generator expression. The code below</p>
<pre class="prettyprint"><code>x, y, z = 1, 2, 3
((x, y, z) for _ in range(5))
</code></pre>
<p>Is equivalent to:</p>
<pre class="prettyprint"><code>((x, y, z) for _ in range(5)) < where(x=1, y=2, z=3)
</code></pre>
lru_timestamp - cache entry aging for functools.lru_cache (Python)
2014-02-02T21:28:25-08:00Peter Santorohttp://code.activestate.com/recipes/users/4189027/http://code.activestate.com/recipes/578817-lru_timestamp-cache-entry-aging-for-functoolslru_c/
<p style="color: grey">
Python
recipe 578817
by <a href="/recipes/users/4189027/">Peter Santoro</a>
(<a href="/recipes/tags/age/">age</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>).
</p>
<p>Return a timestamp string for @lru_cache decorated functions.</p>
<p>The returned timestamp is used as the value of an extra parameter
to @lru_cache decorated functions, allowing for more control over
how often cache entries are refreshed. The lru_timestamp function
should be called with the same refresh_interval value for a given
@lru_cache decorated function. The returned timestamp is for the
benefit of the @lru_cache decorator and is normally not used by
the decorated function.</p>
<p>Positional arguments:
refresh_interval -- in minutes (default 60), values less than 1
are coerced to 1, values more than 1440 are
coerced to 1440</p>
Forward iterator (Python)
2015-01-29T17:12:10-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578560-forward-iterator/
<p style="color: grey">
Python
recipe 578560
by <a href="/recipes/users/4186880/">Andrea Corbellini</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 2.
</p>
<p>A proxy for iterators that lets you <em>read ahead</em> items without consuming the iterator.</p>
C-style enumerations (Python)
2013-01-29T16:13:39-08:00Arthur Gardinerhttp://code.activestate.com/recipes/users/4185064/http://code.activestate.com/recipes/578438-c-style-enumerations/
<p style="color: grey">
Python
recipe 578438
by <a href="/recipes/users/4185064/">Arthur Gardiner</a>
(<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/enumeration/">enumeration</a>).
</p>
<p>Fast and dirty C style enumerations specifically made for machine states but easily adapted. Not the most optimized implementation but a hardy read-only structure that I got tired of emailing around.</p>
Sequence Builder (Python)
2012-03-03T17:50:03-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578061-sequence-builder/
<p style="color: grey">
Python
recipe 578061
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/builder/">builder</a>, <a href="/recipes/tags/sequence/">sequence</a>).
Revision 2.
</p>
<p><strong>Why?</strong></p>
<ul>
<li>Thinking about a sequence of odd numbers where numbers divisible by 3 are not part of it I came to a sequence starting with 5, 7, 11, 13, 17, 19, ...</li>
<li>The interesting property of this sequence is that the sequence of differences between the individual elements are 2,4,2,4,2,...</li>
</ul>
<p><strong>How?</strong></p>
<ul>
<li>I'm not a math expert (unfortunately) but I could imagine that there is a quite simple formula to get this.</li>
<li>However I thought that using different combinations of defined functions a script could do the favour for me.</li>
</ul>
<p><strong>Result?</strong></p>
<ul>
<li>Formula: <code>(((-1)**(x-1))+1)+2</code> -> Simplified -> <code>-1**(x-1) + 3</code> (now clear to you?)</li>
<li>You have to look for the sequence [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]</li>
</ul>
<p><strong>Out of scope:</strong></p>
<ul>
<li>Does not check for using lambda functions only and "x" only in the lambda functions.</li>
<li>The Python script does not simplify functions like changing ((x+1)+1) to x+2 - would be an interesting recipe - by the way :)</li>
</ul>
<p><strong>Please note</strong></p>
<ul>
<li>You should not add other functions than lambda.</li>
<li>You should always use "x" in your formular.</li>
<li>Be aware that the actual setup runs a few minutes (about 3 minutes) and you can imagine - surely - that adding further functions will definitely increase the runtime.</li>
</ul>
<p><strong>Side effects?</strong></p>
<ul>
<li>Quite funny to produce a sequence of nines with <code>(((-1)**(2*x))+2)**2</code>.</li>
<li>If you apply the first binomial theorem (a+b)^2 = a^2 + 2ab + b^2 then you see why!</li>
</ul>
<p><strong>What I have learned from this?</strong></p>
<ul>
<li>The biggest nightmare I did have with the Sequence class because of not knowing how to generate unique elements of this in a set (__eq__ and __hash__ are required); and I have to ensure that the hash code is calculated once only.</li>
<li>The 'combineFunctions' was interesting to me. I have never used 'inspect.getsource' before.</li>
<li>A few minutes does not sound much but for developing all times with more than 30 seconds are not comfortable. There are just 325 sequences and to investigate for certain sequences you probably have to have some more formula. Maybe I would have to take another approach for that.</li>
</ul>
A Simple Namespace Class (Python)
2011-10-03T21:12:41-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577887-a-simple-namespace-class/
<p style="color: grey">
Python
recipe 577887
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/namespaces/">namespaces</a>).
Revision 3.
</p>
<p>"Namespaces are one honking great idea -- let's do more of those!" -- <a href="http://www.python.org/dev/peps/pep-0020/">The Zen of Python</a></p>
<p>For when you want a simple, easy namespace, but you don't want it cluttered up with Python's object machinery.</p>
Komodo JS Macro: smart quotes and smart braces (JavaScript)
2013-06-24T13:38:55-07:00Todd Whitemanhttp://code.activestate.com/recipes/users/2666241/http://code.activestate.com/recipes/577790-komodo-js-macro-smart-quotes-and-smart-braces/
<p style="color: grey">
JavaScript
recipe 577790
by <a href="/recipes/users/2666241/">Todd Whiteman</a>
(<a href="/recipes/tags/braces/">braces</a>, <a href="/recipes/tags/brackets/">brackets</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/scintilla/">scintilla</a>, <a href="/recipes/tags/smart/">smart</a>, <a href="/recipes/tags/toddw/">toddw</a>).
Revision 5.
</p>
<p>With this macro, you can make a text/word selection, then press one of ", ', (, [ or { and the matching set of quotes/braces will be added around the selection.</p>
<p>Example (where | denotes the selection):
This is |myselectedword|.
then pressing "{" you'll get:
This is {myselectedword}.</p>
url_spider (Python)
2011-03-14T09:08:28-07:00amir naghavihttp://code.activestate.com/recipes/users/4177294/http://code.activestate.com/recipes/577608-url_spider/
<p style="color: grey">
Python
recipe 577608
by <a href="/recipes/users/4177294/">amir naghavi</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/web/">web</a>).
Revision 3.
</p>
<p>a simple url spider that goes through web pages and collects urls.</p>
Script para transformar videos compatibles con reproductor MP4 (Python)
2010-04-30T18:23:39-07:00jrovegnohttp://code.activestate.com/recipes/users/4170207/http://code.activestate.com/recipes/577210-script-para-transformar-videos-compatibles-con-rep/
<p style="color: grey">
Python
recipe 577210
by <a href="/recipes/users/4170207/">jrovegno</a>
(<a href="/recipes/tags/ffmpge/">ffmpge</a>, <a href="/recipes/tags/mencoder/">mencoder</a>, <a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/video/">video</a>).
Revision 3.
</p>
<p>Script para transformar videos compatibles con reproductor MP4:
MP4 2GB FUJITEL 80MP4TV2 MP4-TV (AVIConverter_320X240_20FPS_EN Setup.exe)
Requiere:
- ffmpeg - mencoder
Extras:
Divide video en partes de 10 min
Agrega subtÃtulos si existe el archivo file_name.srt
Uso:
avi2mp4 file_name.avi</p>
gsl real fft in python3 (Python)
2008-11-01T10:27:39-07:00David Lamberthttp://code.activestate.com/recipes/users/4167420/http://code.activestate.com/recipes/576550-gsl-real-fft-in-python3/
<p style="color: grey">
Python
recipe 576550
by <a href="/recipes/users/4167420/">David Lambert</a>
(<a href="/recipes/tags/fft/">fft</a>, <a href="/recipes/tags/gnu/">gnu</a>, <a href="/recipes/tags/gsl/">gsl</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/scientific/">scientific</a>).
</p>
<p>Real_FFT wraps the gsl_fft_real_transform in a python3 setting. This recipe serves as a complete example for "<a href="http://code.activestate.com/recipes/576549/">Recipe 576549</a>: gsl with python3".</p>
Quadratic (Python)
2011-01-28T12:46:37-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/576407-quadratic/
<p style="color: grey">
Python
recipe 576407
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>).
Revision 19.
</p>
<p>Quadratic can be used by students for solving quadratic equations ax^2+bx+c a!=0,
which are given by the quadratic formula x = -b + SQUARE-ROOT(b^2-4<em>a</em>c)/2<em>a and x = -b - SQUARE-ROOT(b^2-4</em>a<em>c)/2</em>a
However,if b^2-4<em>a</em>c is negative the solutions are not real but complex numbers of the form a+bj.
Though, both real and complex solutions are called using Quadratic</p>
Convenience class for algorithm development (Python)
2007-07-21T17:51:35-07:00wolfgang reschhttp://code.activestate.com/recipes/users/4069886/http://code.activestate.com/recipes/525484-convenience-class-for-algorithm-development/
<p style="color: grey">
Python
recipe 525484
by <a href="/recipes/users/4069886/">wolfgang resch</a>
.
</p>
<p>Two convenience classes for applying different scenarios (data) to different functions and measuring timing as a practical framework for optimizing performance.
NOTE: time.clock() and time.time() resolution depend on system time.clock() may be more appropriate.</p>
A simple time-out class. (Python)
2005-08-25T06:37:01-07:00Johan Geldenhuyshttp://code.activestate.com/recipes/users/2041834/http://code.activestate.com/recipes/440476-a-simple-time-out-class/
<p style="color: grey">
Python
recipe 440476
by <a href="/recipes/users/2041834/">Johan Geldenhuys</a>
.
</p>
<p>Starts a timer that can be stopped or acts upon time-out.</p>
Finding significant digits (Python)
2007-09-05T19:14:04-07:00David Eykhttp://code.activestate.com/recipes/users/2376838/http://code.activestate.com/recipes/392115-finding-significant-digits/
<p style="color: grey">
Python
recipe 392115
by <a href="/recipes/users/2376838/">David Eyk</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 3.
</p>
<p>Method to find significant digits for any given number. Accounts for scientific notation. Returns a numeric string.</p>
<p>A unit test is included.</p>
Matlab-like 'spy' and 'pcolor' functions (Python)
2005-03-02T15:23:38-08:00Rick Mullerhttp://code.activestate.com/recipes/users/1461790/http://code.activestate.com/recipes/390208-matlab-like-spy-and-pcolor-functions/
<p style="color: grey">
Python
recipe 390208
by <a href="/recipes/users/1461790/">Rick Muller</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>I really like the 'spy' and 'pcolor' functions, which are useful in viewing matrices. 'spy' prints colored blocks for values that are above a threshold, and 'pcolor' prints out each element in a continuous range of colors.
The attached is a little Python/PIL script that does these functions for Numpy arrays.</p>
Enums for Python (Python)
2001-08-23T14:57:17-07:00Will Warehttp://code.activestate.com/recipes/users/98156/http://code.activestate.com/recipes/67107-enums-for-python/
<p style="color: grey">
Python
recipe 67107
by <a href="/recipes/users/98156/">Will Ware</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>I once tried to give Python something like C's enums, as described
here: <a href="http://groups.google.com/groups?selm=G6qzLy.6Fo%2540world.std.com" rel="nofollow">http://groups.google.com/groups?selm=G6qzLy.6Fo%40world.std.com</a>
That approach tried to assign to a dictionary returned by the locals()
function, intending that such assignments would become class attributes.
The Tim-bot explained to me the errors of my ways. The quest for the
perfect Python enum goes on.</p>
Scour Microsoft Windows registry for a list of name server addresses (Python)
2001-07-19T18:15:40-07:00Wolfgang Stroblhttp://code.activestate.com/recipes/users/109628/http://code.activestate.com/recipes/66260-scour-microsoft-windows-registry-for-a-list-of-nam/
<p style="color: grey">
Python
recipe 66260
by <a href="/recipes/users/109628/">Wolfgang Strobl</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 3.
</p>
<p>A utility function somewhat similar to what parsing resolv.conf for nameserver entries would do on Unix.</p>