Popular recipes tagged "meta:loc=115"http://code.activestate.com/recipes/tags/meta:loc=115/2015-09-08T01:27:04-07:00ActiveState Code RecipesPython 2 nonlocal (Python)
2015-09-08T01:27:04-07:00Ryan Gonzalezhttp://code.activestate.com/recipes/users/4187447/http://code.activestate.com/recipes/578965-python-2-nonlocal/
<p style="color: grey">
Python
recipe 578965
by <a href="/recipes/users/4187447/">Ryan Gonzalez</a>
(<a href="/recipes/tags/backport/">backport</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/python2/">python2</a>).
Revision 2.
</p>
<p>This implements nonlocal in Python 2...albeit in a slightly ugly way. Tested with CPython 2.7 and PyPy.</p>
modify a xml file for experiment (XML)
2012-07-05T19:39:16-07:00darkrevlishttp://code.activestate.com/recipes/users/4182724/http://code.activestate.com/recipes/578196-modify-a-xml-file-for-experiment/
<p style="color: grey">
XML
recipe 578196
by <a href="/recipes/users/4182724/">darkrevlis</a>
.
</p>
<p>the same</p>
Future decorator in a pretty pythonic way (Python)
2011-10-20T16:51:48-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577915-future-decorator-in-a-pretty-pythonic-way/
<p style="color: grey">
Python
recipe 577915
by <a href="/recipes/users/4174931/">Filippo Squillace</a>
(<a href="/recipes/tags/threading/">threading</a>).
</p>
<p>Without thinking in thread creation the idea is to call several times
a function assigning a thread for each call with related parameters
and returning the list of results in a pretty pythonic way.
For example, if we have already defined the function 'func':
res = func(par)
we want to call that several times and get the values in the future. So:
func(par1)
func(par2)
func(par3).get()
func(par4).get_all()</p>
<p>assigning a thread for each call. The 'get' method returns the first possible results,
and 'get_all' returns all the values in a list. The decorator works fine with kwargs too.</p>
<p>This recipe is based on:
<a href="http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/" rel="nofollow">http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/</a>
that use only one call for the function. The problem in that recipe is that each call blocks the execution.</p>
<p>Vote if you like it!</p>
Using Metaclasses and Class Decorators to Inherit Function Docstrings (Python)
2011-06-11T00:59:35-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577743-using-metaclasses-and-class-decorators-to-inherit-/
<p style="color: grey">
Python
recipe 577743
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>).
Revision 4.
</p>
<p>You'll find three different approaches to copying the method's docstring to the overriding method on a child class.</p>
<p>The function decorator approach is limited by the fact that you have to know the class when you call the decorator, so it can't be used inside a class body. However, <a href="http://code.activestate.com/recipes/577746/">recipe #577746</a> provides a function decorator that does not have this limitation.</p>
edit dictionary values (possibly restrained) with Tkinter (Python)
2011-03-15T21:37:54-07:00s_h_a_i_ohttp://code.activestate.com/recipes/users/4177334/http://code.activestate.com/recipes/577611-edit-dictionary-values-possibly-restrained-with-tk/
<p style="color: grey">
Python
recipe 577611
by <a href="/recipes/users/4177334/">s_h_a_i_o</a>
(<a href="/recipes/tags/edit/">edit</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/in/">in</a>, <a href="/recipes/tags/options/">options</a>).
</p>
<p>The behaviour of applications sometimes depends on various parameters that can be chosen by the user through some GUI. Quite often, these parameters need take value in a predefined set. Parameters include for instance:</p>
<ul>
<li>The number of replications for a simulator.</li>
<li>Dates of beginning/end of sample for time series access/plot.</li>
<li>Log writing</li>
</ul>
<p>This recipe assumes that</p>
<ul>
<li>Such parameters are stored in a dictionary. Values in the dictionnary are used as initial values for the parameters.</li>
<li>Values are "singleton", i.e non iterable (although they may be strings).</li>
<li>When a value is a list instance, its content represent the set of possible values
The recipe offers a simple way to edit the values in a Tkinter Frame, by adding proper widget to it (constrained value are menubutton while unconstrained values are edit widgets).</li>
</ul>
<p>This is done by the function apply(frame,dict,position), which adds to the Tkinter frame the necessary widgets to edit the dictionary dict. Widgets are placed using grid (hence the frame needs use grid() too) and span on two columns. topleft position (x,y) in grid is specified using position= (x,y,0,0)</p>
MongoDB Pool for gevent and pymongo packages (Python)
2011-09-02T05:56:58-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577490-mongodb-pool-for-gevent-and-pymongo-packages/
<p style="color: grey">
Python
recipe 577490
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/gevent/">gevent</a>, <a href="/recipes/tags/mongodb/">mongodb</a>, <a href="/recipes/tags/pymongo/">pymongo</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>Wrote some simple implementation of pool for pymongo package under gevent coroutine library.</p>
<p>Base bug here was with pymongo.connection.Pool because in the original package it is thread-local, so when you spawn new greenlet and trying to get already open connection, it creates new connection because in this greenlet pool is empty. So if you will implement your own pool don’t forget about this.</p>
<h4 id="example-of-use">Example of use:</h4>
<pre class="prettyprint"><code># Create Pool.
db = Mongo('test_db',10)
# Get connection from pool
conn = db.get_conn()
# Get raw connection for GridFS
raw_conn = conn.getDB
#Mongo is a singleton. So if you want to get connection in another part of application just type
db = Mongo()
conn = db.get_conn()
#Connection will get back to pool when context will be closed.
</code></pre>
Binary_Decimal Recursion (Python)
2010-02-23T04:39:41-08:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577034-binary_decimal-recursion/
<p style="color: grey">
Python
recipe 577034
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 5.
</p>
<p>The most commonly used number system is the denary number system, a system of numbers to base 10. However, All the others numbers systems are used in the computing field.
And the binary number system, a system of numbers to base 2 uses only two digits 0 and 1, and any position in a binary number system increases by powers of 2 in contrast with the decimal system, which increases by powers of 10.
<strong>Though, the binary number system was invented by a Chinese poet and philosopher Shao Yong in the 11th century.</strong>
Solving repetitive problems requires a powerful method and yet recursion allows the design of faster recursive algorithms. Recursion method could be used to solve higher-level mathematics problems, such as sequences and it is a branch in computer science study.</p>
High level inotify wrapper (Python)
2008-09-21T05:17:23-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/576377-high-level-inotify-wrapper/
<p style="color: grey">
Python
recipe 576377
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/inotify/">inotify</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/system/">system</a>).
Revision 4.
</p>
<p>This is an exemple to show how to use the inotify module, it could be very usefull unchanged though.</p>
<p>A Watcher instance let you define callbacks for any event that occur on any file or directory and subdirectories.</p>
<p>The inotify module is from <a href="http://code.activestate.com/recipes/576375/">Recipe 576375</a></p>
Getting system information under Windows (Python)
2007-04-22T11:47:15-07:00Eli Golovinskyhttp://code.activestate.com/recipes/users/2925878/http://code.activestate.com/recipes/511491-getting-system-information-under-windows/
<p style="color: grey">
Python
recipe 511491
by <a href="/recipes/users/2925878/">Eli Golovinsky</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>This code uses pywin32 and the _winreg module to get some vital information about the system the script is running on, including OS version, installed browsers and free space on the main HD.</p>
<p>Enjoy</p>
Windows network file transfers (Python)
2005-11-04T16:20:21-08:00Fadly Tabranihttp://code.activestate.com/recipes/users/2143621/http://code.activestate.com/recipes/442521-windows-network-file-transfers/
<p style="color: grey">
Python
recipe 442521
by <a href="/recipes/users/2143621/">Fadly Tabrani</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 3.
</p>
<p>This module enables users on the windows platform to transfer files to remote hosts. Requires pywin32 extensions by Mark Hammond</p>
Paint 1.0 (Python)
2007-03-28T15:21:48-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511434-paint-10/
<p style="color: grey">
Python
recipe 511434
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>This program was adapted from the SAMPLES directory
that comes with Python. It allows two people to draw
on a program with someone else watching and, at the
same time watch what the other person is drawing also.</p>
Watchdog: Decorator for Function Parameter / Result checking (Python)
2006-03-19T14:18:37-08:00Tobias Simonhttp://code.activestate.com/recipes/users/2815331/http://code.activestate.com/recipes/475132-watchdog-decorator-for-function-parameter-result-c/
<p style="color: grey">
Python
recipe 475132
by <a href="/recipes/users/2815331/">Tobias Simon</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 8.
</p>
<p>Watchdog((Param_Type1, Param_Type2, ...), Result_Type) returns a function decorator which can easily be applied to functions in oder to check parameter / result integrity.</p>
Instant GUI compiler for testing purpose (Python)
2006-01-30T15:46:23-08:00Andre Robergehttp://code.activestate.com/recipes/users/2467063/http://code.activestate.com/recipes/473812-instant-gui-compiler-for-testing-purpose/
<p style="color: grey">
Python
recipe 473812
by <a href="/recipes/users/2467063/">Andre Roberge</a>
.
</p>
<p>I find that I often want to test some small code samples, too long to type at the interpreter (and retype, with some small changes), but not worth saving. The following wxPython based mini-app implements the basic for a small Python editor with a side output window. It also handles input() and raw_input().</p>