Popular recipes tagged "meta:loc=56"http://code.activestate.com/recipes/tags/meta:loc=56/2017-01-30T14:09:47-08:00ActiveState Code RecipesFind the arity of a Python function (Python)
2017-01-30T14:09:47-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580753-find-the-arity-of-a-python-function/
<p style="color: grey">
Python
recipe 580753
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/reflection/">reflection</a>).
</p>
<p>This recipe shows how to find the arity of a given Python function. The arity of a function is the number of arguments the function takes. The recipe uses the inspect module of Python.</p>
<p>More details and sample output (including some limitations) here:</p>
<p><a href="https://jugad2.blogspot.in/2017/01/finding-arity-of-python-function.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/finding-arity-of-python-function.html</a></p>
Simple command-line alarm clock in Python (Python)
2015-10-25T18:27:27-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579117-simple-command-line-alarm-clock-in-python/
<p style="color: grey">
Python
recipe 579117
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple alarm clock in Python, that can be run from the command line in a terminal. It lets you specify the alarm time in minutes as a command line argument, and prints a wake-up message and beeps a few times, after that time arrives. You can use 0 for the minutes to test it immediately, including to adjust the volume using your speaker controls.</p>
PyQt4 pressed modifier keys names as method argument by a decorator (Python)
2013-09-29T14:13:04-07:00TNThttp://code.activestate.com/recipes/users/4187961/http://code.activestate.com/recipes/578675-pyqt4-pressed-modifier-keys-names-as-method-argume/
<p style="color: grey">
Python
recipe 578675
by <a href="/recipes/users/4187961/">TNT</a>
(<a href="/recipes/tags/keys/">keys</a>, <a href="/recipes/tags/modifier/">modifier</a>, <a href="/recipes/tags/pyqt/">pyqt</a>).
Revision 2.
</p>
<p>This is a definition of a decorator function that checks which modifier keys are being pressed and adds a keyword argument to a method. This argument is a tuple of names (strings) of the modifier keys that have been pressed when the method was called (or triggered).</p>
Riemann Zeta Function Graph (Python)
2013-07-21T08:03:39-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578616-riemann-zeta-function-graph/
<p style="color: grey">
Python
recipe 578616
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>Riemann Zeta Function Graph</p>
Validate XML with schemaLocation (Python)
2013-03-28T10:24:55-07:00Mathias Loeschhttp://code.activestate.com/recipes/users/4185910/http://code.activestate.com/recipes/578503-validate-xml-with-schemalocation/
<p style="color: grey">
Python
recipe 578503
by <a href="/recipes/users/4185910/">Mathias Loesch</a>
(<a href="/recipes/tags/schema/">schema</a>, <a href="/recipes/tags/schemalocation/">schemalocation</a>, <a href="/recipes/tags/validation/">validation</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 3.
</p>
<p>Addresses problems described at:</p>
<p><a href="http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml" rel="nofollow">http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml</a>
<a href="https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html" rel="nofollow">https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html</a></p>
Save and restore SHA-512 internal state (Python)
2013-03-03T18:05:42-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578479-save-and-restore-sha-512-internal-state/
<p style="color: grey">
Python
recipe 578479
by <a href="/recipes/users/4068698/">Dima Tisnek</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/inner/">inner</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/restore/">restore</a>, <a href="/recipes/tags/sha512/">sha512</a>, <a href="/recipes/tags/state/">state</a>).
</p>
<p>If you have a very long input to hash, you may want to save your progress.</p>
<p>CPython doesn't normally let you, but it's easy to hack around via ctypes</p>
The Iota language in Python (Python)
2013-04-02T01:19:28-07:00Simon Formanhttp://code.activestate.com/recipes/users/2702520/http://code.activestate.com/recipes/578505-the-iota-language-in-python/
<p style="color: grey">
Python
recipe 578505
by <a href="/recipes/users/2702520/">Simon Forman</a>
.
</p>
<p>Forgive me if you've heard this one before. </p>
Generate Password and Bit Rank (Python)
2013-01-02T14:33:42-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578397-generate-password-and-bit-rank/
<p style="color: grey">
Python
recipe 578397
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/passwords/">passwords</a>, <a href="/recipes/tags/password_generator/">password_generator</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>The code generates passwords using different techniques and then checks them according to bit strength. Was written in context of <a href="http://thelivingpearl.com/2013/01/02/generating-and-checking-passwords-in-python/">Generating And Checking Passwords In Python</a>.</p>
Using ChainMap for embedded namespaces (Python)
2012-10-03T18:12:50-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578279-using-chainmap-for-embedded-namespaces/
<p style="color: grey">
Python
recipe 578279
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/chainmap/">chainmap</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/namespace/">namespace</a>).
</p>
<p>The Zen of Python tells us:</p>
<p><em>Namespaces are one honking great idea -- let's do more of those!</em></p>
<p>Python already has an excellent namespace type, the module, but the problem with modules is that they have to live in a separate file, and sometimes you want the convenience of a single file while still encapsulating your code into namespaces. That's where classes are the usual solution, but classes need to be instantiated and methods need to be defined with a <code>self</code> parameter.</p>
<p>C++ has "namespaces" for encapsulating related objects and dividing the global scope into sub-scopes. Can we do the same in Python?</p>
<p>With a bit of metaclass trickery and the new ChainMap type from Python 3.3, we can!</p>
Automatic Python PyObject ref-count management in C++ using a smart ptr (C++)
2011-12-19T15:24:03-08:00samhhttp://code.activestate.com/recipes/users/4180289/http://code.activestate.com/recipes/577985-automatic-python-pyobject-ref-count-management-in-/
<p style="color: grey">
C++
recipe 577985
by <a href="/recipes/users/4180289/">samh</a>
(<a href="/recipes/tags/extending/">extending</a>).
</p>
<p>Managing ref-counting is a complex and error prone business. If you choose C++ to extend or embed Python, you can simply use a modification on <code>std::auto_ptr</code>. Instead of calling delete on the managed pointer, it will decref it.</p>
<p>So now you can do:</p>
<pre class="prettyprint"><code>auto_pyptr pyHelloStr(PyStr_FromString("Hello World!"));
</code></pre>
<p>and forget about having to decref it altogether! Just like <code>auto_ptr</code> you can get the <code>PyObject *</code> with <code>get()</code>, release it from managed control with <code>release()</code>, and rebind it with <code>reset(new_ptr)</code>. You can also incref it by calling <code>inc()</code>, but be cautious as you can easily create a leak by increfing once to much.</p>
Multi-threaded Mandelbrot Fractal (Python)
2011-05-01T17:33:20-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577680-multi-threaded-mandelbrot-fractal/
<p style="color: grey">
Python
recipe 577680
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</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/mathematics/">mathematics</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>Multi-threaded Mandelbrot Fractal.</p>
Infix operators (Python)
2011-06-06T12:07:00-07:00Animesh Kumarhttp://code.activestate.com/recipes/users/4178212/http://code.activestate.com/recipes/577738-infix-operators/
<p style="color: grey">
Python
recipe 577738
by <a href="/recipes/users/4178212/">Animesh Kumar</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p>
Mandelbrot trajectories (Python)
2011-04-06T18:18:48-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/577642-mandelbrot-trajectories/
<p style="color: grey">
Python
recipe 577642
by <a href="/recipes/users/4166965/">Kaushik Ghose</a>
(<a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/interactive/">interactive</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/plotting/">plotting</a>, <a href="/recipes/tags/widget/">widget</a>).
</p>
<p>An interactive graph to plot the trajectory of points on and off the mandelbrot
set. Illustrates the use of sliders in matplotlib</p>
Infix operators (Python)
2005-02-17T11:12:04-08:00Ferdinand Jamitzkyhttp://code.activestate.com/recipes/users/98863/http://code.activestate.com/recipes/384122-infix-operators/
<p style="color: grey">
Python
recipe 384122
by <a href="/recipes/users/98863/">Ferdinand Jamitzky</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p>
Implementing Vertical Editing in Komodo 6 (JavaScript)
2010-09-29T19:09:35-07:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577406-implementing-vertical-editing-in-komodo-6/
<p style="color: grey">
JavaScript
recipe 577406
by <a href="/recipes/users/4166930/">Eric Promislow</a>
(<a href="/recipes/tags/columns/">columns</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/verticalediting/">verticalediting</a>).
Revision 4.
</p>
<p>When editing a group of similar lines, it's very convenient to be able to press down-arrow
and move to the same starting column position on the next line. (See an example at
<a href="http://community.activestate.com/forum/vertical-editing" rel="nofollow">http://community.activestate.com/forum/vertical-editing</a> ).</p>
<p>This code is for a Komodo 6 macro -- possibly the only tweak necessary to make it work on Komodo 5 is by commenting out the "macro.save()" code, but I haven't tested it. The discussion below will talk about how to use it. </p>
Approximately Equal (Python)
2010-03-17T17:05:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577124-approximately-equal/
<p style="color: grey">
Python
recipe 577124
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/comparisons/">comparisons</a>, <a href="/recipes/tags/distance/">distance</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/numeric/">numeric</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Generic "approximately equal" function for any object type, with customisable error tolerance.</p>
<p>When called with float arguments, approx_equal(x, y[, tol[, rel]) compares x and y numerically, and returns True if y is within either absolute error tol or relative error rel of x, otherwise return False. The function defaults to sensible default values for tol and rel.</p>
<p>For any other pair of objects, approx_equal() looks for a method __approx_equal__ and, if found, calls it with arbitrary optional arguments. This allows types to define their own concept of "close enough".</p>
True-color Plasma Fractal (Python)
2016-01-22T22:44:33-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577161-true-color-plasma-fractal/
<p style="color: grey">
Python
recipe 577161
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>).
Revision 4.
</p>
<p>True-color plasma fractal using non-recursive algorithm.</p>
Binary Matrix Fractals using recursion (Python)
2010-03-27T15:15:49-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577157-binary-matrix-fractals-using-recursion/
<p style="color: grey">
Python
recipe 577157
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Binary Matrix Fractals using recursion.
It can convert any given binary matrix pattern into a fractal.</p>
Convert Byte Size to String Representation (Python)
2012-04-13T19:55:07-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576924-convert-byte-size-to-string-representation/
<p style="color: grey">
Python
recipe 576924
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/representation/">representation</a>).
Revision 4.
</p>
<p>In anticipation of creating large data structure, it appeared to be helpful if the users could get an idea of how much memory (RAM in particular) would be used while attempting to create a large, multidimensional array. In order to convert the calculated size into a precise, human-readable format, the follow code was devised. In order to convert a number into an equivalent representation of bytes, just call the "convert" function while providing the number as its argument. The other functions are currently public in case anyone else finds them useful.</p>
Quantitative Concentration Ratios Calculation (Python)
2009-05-08T01:41:29-07:00donyo Ganchevhttp://code.activestate.com/recipes/users/4167704/http://code.activestate.com/recipes/576744-quantitative-concentration-ratios-calculation/
<p style="color: grey">
Python
recipe 576744
by <a href="/recipes/users/4167704/">donyo Ganchev</a>
.
</p>
<p>Quantitative Concentration Ratios Calculation Script</p>