Popular recipes tagged "meta:loc=63"http://code.activestate.com/recipes/tags/meta:loc=63/2017-04-26T17:26:29-07:00ActiveState Code RecipesAuto assign self attributes in __init__ using PEP 484 (Python)
2017-04-26T17:26:29-07:00Ryan Gonzalezhttp://code.activestate.com/recipes/users/4187447/http://code.activestate.com/recipes/580790-auto-assign-self-attributes-in-__init__-using-pep-/
<p style="color: grey">
Python
recipe 580790
by <a href="/recipes/users/4187447/">Ryan Gonzalez</a>
(<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/self/">self</a>, <a href="/recipes/tags/type/">type</a>).
Revision 2.
</p>
<p>TL;DR: Short way of doing stuff like <code>def __init__(self, a, b): self.a = a; self.b = b</code>, using type annotations from PEP 484, inspired by Dart's <code>MyConstructor(this.a, this.b)</code> and Ruby's/Crystal's/(Moon|Coffee)Script's <code>constructor/initialize(@a, @b)</code>.</p>
Python method chaining examples (Python)
2016-02-25T19:40:33-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580616-python-method-chaining-examples/
<p style="color: grey">
Python
recipe 580616
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/chaining/">chaining</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This recipe shows a few examples of doing method chaining in Python.</p>
Computing simple list functions recursively (Python)
2016-03-01T20:41:13-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580617-computing-simple-list-functions-recursively/
<p style="color: grey">
Python
recipe 580617
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/recursive/">recursive</a>).
</p>
<p>This recipe shows how to compute some simple list functions (functions that operate on lists) using simple recursive algorithms. Simple non-recursive algorithms exist for these tasks; this recipe is just for illustrative purposes. However, the principles described can be used for more complex recursive algorithms, for which non-recursive algorithms might be more complex than the recursive ones.</p>
Retry loop (Python)
2013-05-23T20:45:01-07:00Ryan Nowakowskihttp://code.activestate.com/recipes/users/4186624/http://code.activestate.com/recipes/578527-retry-loop/
<p style="color: grey">
Python
recipe 578527
by <a href="/recipes/users/4186624/">Ryan Nowakowski</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/timeout/">timeout</a>).
</p>
<p>Encapsulates the logic of a retry loop using a generator function.</p>
parse png image (Python)
2013-05-27T22:02:07-07:00judyhttp://code.activestate.com/recipes/users/4186659/http://code.activestate.com/recipes/578534-parse-png-image/
<p style="color: grey">
Python
recipe 578534
by <a href="/recipes/users/4186659/">judy</a>
(<a href="/recipes/tags/height/">height</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/width/">width</a>).
</p>
<p>parse png image find all chunks width height</p>
zero curve bootstrapping and forward curve generation (Python)
2012-09-10T08:07:56-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578257-zero-curve-bootstrapping-and-forward-curve-generat/
<p style="color: grey">
Python
recipe 578257
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/sympy/">sympy</a>).
</p>
<p>example of a bootstrapping and forward curve generation
this can be used to build a set of curves for different currencies</p>
Retry loop (Python)
2013-05-24T05:19:50-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578163-retry-loop/
<p style="color: grey">
Python
recipe 578163
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/timeout/">timeout</a>).
Revision 4.
</p>
<p>Encapsulates the logic of a retry loop using a generator function.</p>
Mersenne twister (Python)
2012-02-29T17:04:40-08:00Yaşar Arabacıhttp://code.activestate.com/recipes/users/4178739/http://code.activestate.com/recipes/578056-mersenne-twister/
<p style="color: grey">
Python
recipe 578056
by <a href="/recipes/users/4178739/">Yaşar Arabacı</a>
.
</p>
<p>Mersenne twister algorithm implemented in Python. Based on the pseudocode in here: <a href="https://en.wikipedia.org/wiki/Mersenne_Twister" rel="nofollow">https://en.wikipedia.org/wiki/Mersenne_Twister</a></p>
XML as Dictionary (Python)
2012-01-17T18:10:29-08:00Vikram Baghelhttp://code.activestate.com/recipes/users/4180561/http://code.activestate.com/recipes/578017-xml-as-dictionary/
<p style="color: grey">
Python
recipe 578017
by <a href="/recipes/users/4180561/">Vikram Baghel</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>I use this for configuration. I hadn't intended to put it up anywhere, but there have been a couple discussions lately about converting XML to python dicts, so I feel obligated to share another approach, one that is based on Fredrik Lundh's ElementTree.</p>
Printing current, next, or previous day (Python)
2011-09-29T21:39:22-07:00Andy Barbourhttp://code.activestate.com/recipes/users/4174351/http://code.activestate.com/recipes/577883-printing-current-next-or-previous-day/
<p style="color: grey">
Python
recipe 577883
by <a href="/recipes/users/4174351/">Andy Barbour</a>
(<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>).
Revision 3.
</p>
<p>simply prints the previous, current, or next day. Inputs may be <em>year month month-day</em> or <em>year julian-day</em> with the output in the same format.</p>
Mixin and Overlay (Python)
2011-06-01T05:16:47-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577730-mixin-and-overlay/
<p style="color: grey">
Python
recipe 577730
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/mixins/">mixins</a>).
</p>
<h4 id="the-two-sides-of-classes">The Two Sides of Classes</h4>
<p>Class inheritance either adds new attributes to a class, or changes existing attributes. However, this distinction is not made in Python's class system. This recipe is a stab at providing that separation. It takes advantage of metaclasses and class decorators to do it.</p>
<p>For this recipe the two sides of class inheritance are called mixins and overlays. Mixins add new attributes to a class. Overlays change existing attributes, which in the case of methods means changing the behavior of the methods. However, overlays do not add any attributes.</p>
<h4 id="separation-of-concerns">Separation of Concerns</h4>
<p>Another issue with class inheritance is that abstraction rarely breaks down into perfect trees with a clear separation of concerns. Python allows multiple inheritance, which can help, but requires cooperation between classes in the diamond hierarchy. Mixins and overlays help with this problem. Hopefully, that will be evidenced by the recipe and subsequent examples.</p>
<p>With this approach, the main single inheritance line can focus on the core abstraction and mixins/overlays can be used to extend the classes in other directions. This does not solve all the problems regarding separation of concerns, but it solves some. I hope to address the rest in another recipe that centers around delegation through a component architecture on instances.</p>
<h4 id="interfaces">Interfaces</h4>
<p>Since Python 2.6 we have had Abstract Base Classes providing a mechanism for promising what interfaces an object provides. I'll show in one of the examples how an ABC can be split into a interface portion and a mixin portion. The recipe takes advantage of the ABC functionality in the Python type system. I expect that mixins and overlays would be a good fit with one of my other recipes (<a href="http://code.activestate.com/recipes/577711/">Recipe 577711</a>).</p>
<h4 id="the-recipe-classes">The Recipe Classes</h4>
<p>This recipe provides a standard approach to applying mixin classes without using inheritance. This is done through a metaclass. The metaclass builds a __mixins__ attribute on the class and provides a mixes_in class decorator. Applying that decorator to the class will add the attributes in __mixins__ to the decorated class. However, if any of those names are already bound on the name then it will fail. The decorator returns the modified class.</p>
<p><strong>Note</strong>: Traditional mixins are typically done through multiple inheritance, as opposed to class decorators.</p>
<p>This recipe also provides a companion to mixin classes, called overlays. This is done through a metaclass in exactly the same way as the mixins, but provides an __overlays__ attribute and a class decorator called overlays. In contrast to mixins, if an overlay attribute is missing on the decorated class, it will fail. This is because the decorator will return a new class that inherits from the decorated class, and overrides the attributes in __overlays__.</p>
<p><strong>Note</strong>: Metaclasses are used here because we need to pull from the class namespace there. A class decorator does not afford us the same functionality without more complexity.</p>
<p><strong>Note</strong>: This recipe should work fine in 2.7 with a switch to the __metaclass__ syntax.</p>
Retrieving the median of a set in constant time (Python)
2011-04-19T15:00:57-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577661-retrieving-the-median-of-a-set-in-constant-time/
<p style="color: grey">
Python
recipe 577661
by <a href="/recipes/users/4177690/">jimmy2times</a>
(<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/retrieving/">retrieving</a>, <a href="/recipes/tags/structure/">structure</a>).
Revision 2.
</p>
<p>Provides a data structure for a queue of integers whose get() method returns the median element. The interface is similar to the standard Queue module, with an added method top() to retrieve the median without removing it.</p>
Website Text Search (Python)
2010-09-11T17:32:01-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577388-website-text-search/
<p style="color: grey">
Python
recipe 577388
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/url/">url</a>, <a href="/recipes/tags/urllib2/">urllib2</a>, <a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>Searches a website recursively for the given text string and prints all URLs containing it.</p>
Launch virtualbox with emulated USB microphones (Python)
2010-12-14T14:53:07-08:00Joseph Reaglehttp://code.activestate.com/recipes/users/4171494/http://code.activestate.com/recipes/577467-launch-virtualbox-with-emulated-usb-microphones/
<p style="color: grey">
Python
recipe 577467
by <a href="/recipes/users/4171494/">Joseph Reagle</a>
(<a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/virtualbox/">virtualbox</a>).
Revision 4.
</p>
<p>A small Python script that sets sound emulation variables for a particular USB microphone that you want to use, and sets the microphone volume level, since that is often set at zero in the Linux context.</p>
Blocking, Polling, Pipes, Queues in multiprocessing and how they are affected by the OS's time-slicing schedule (Python)
2010-05-08T18:18:01-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577223-blocking-polling-pipes-queues-in-multiprocessing-a/
<p style="color: grey">
Python
recipe 577223
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/multitasking/">multitasking</a>, <a href="/recipes/tags/polling/">polling</a>, <a href="/recipes/tags/timeslice/">timeslice</a>).
</p>
<p>This short code demonstrates how blocking calls to a Queue, while consuming less CPU, are limited in their response time by the minimum time slice the OS is willing to allocate (typically 10ms for Mac OS X and Linux). Non-blocking calls to Pipe, using poll() to check if there is data, on the other hand, give us millisecond or less response times, though they consume more CPU. In this respect doing a blocking call to a CPU is no different than adding sleep(.01) statements to a polling loop. In a way, if you execute a sleep(.01) only when you have no events in your poll you will be more efficient than if you had a blocking call pull events off your Queue one by one - because each call to Queue.get() consumes a time-slice, whereas the sleep(.01) only occurs once. </p>
3d Surface fitting to N random points (Python)
2010-03-16T20:55:31-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577119-3d-surface-fitting-to-n-random-points/
<p style="color: grey">
Python
recipe 577119
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>3d Surface fitting to N random points.
It draws the surface by converting z values to RGB colors. </p>
Scrambled word solving game (Python)
2009-12-03T19:48:56-08:00me mehttp://code.activestate.com/recipes/users/4172371/http://code.activestate.com/recipes/576963-scrambled-word-solving-game/
<p style="color: grey">
Python
recipe 576963
by <a href="/recipes/users/4172371/">me me</a>
(<a href="/recipes/tags/anagram/">anagram</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/word/">word</a>).
Revision 4.
</p>
<p>Solve scrambled words. This game is made by a newbie to python so...</p>
Multi-Regex: Single pass replace of multiple regexes (Python)
2009-04-03T13:38:39-07:00Michael Palmerhttp://code.activestate.com/recipes/users/1827292/http://code.activestate.com/recipes/576710-multi-regex-single-pass-replace-of-multiple-regexe/
<p style="color: grey">
Python
recipe 576710
by <a href="/recipes/users/1827292/">Michael Palmer</a>
(<a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/string_substitution/">string_substitution</a>).
Revision 5.
</p>
<p>Not really - all regexes first get combined into a single big disjunction. Then, for each match, the matching sub-regex is determined from a group name and the match object dispatched to a corresponding method, or simply replaced by a string. </p>
SuperFly: Separating class heirarchy from class definition. (Python)
2009-02-15T20:47:51-08:00Ted Skolnickhttp://code.activestate.com/recipes/users/4169200/http://code.activestate.com/recipes/576652-superfly-separating-class-heirarchy-from-class-def/
<p style="color: grey">
Python
recipe 576652
by <a href="/recipes/users/4169200/">Ted Skolnick</a>
(<a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/polymorphism/">polymorphism</a>).
Revision 2.
</p>
<p>This is a recipe for defining class hierarchies outside of class definitions. This way you could at, runtime, decide what class should act as the parent of a given class. A class could sometimes have one parent, sometimes another.</p>
Frequency measurement convenience class (Python)
2009-01-05T07:01:11-08:00Willi Richerthttp://code.activestate.com/recipes/users/4003859/http://code.activestate.com/recipes/576607-frequency-measurement-convenience-class/
<p style="color: grey">
Python
recipe 576607
by <a href="/recipes/users/4003859/">Willi Richert</a>
(<a href="/recipes/tags/frequency/">frequency</a>, <a href="/recipes/tags/measurement/">measurement</a>).
</p>
<p>From time to time one needs to calculate the average frequency of concurrently running processes/threads. This class just does this.</p>