Popular recipes by pavel http://code.activestate.com/recipes/users/4171837/2012-07-04T21:03:32-07:00ActiveState Code RecipesDistributed lock manager for Python (Python)
2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/
<p style="color: grey">
Python
recipe 578194
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>).
Revision 2.
</p>
<p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p>
<p>First start server process:</p>
<pre class="prettyprint"><code>$ chmod +x dlm.py
$ ./dlm.py
</code></pre>
<p>Usage:</p>
<pre class="prettyprint"><code>from dlm import LockClient
client = LockClient('localhost', 27272, 'client_name')
lock = client.mkLock('lock_name')
lock.acquire()
# critical section here...
lock.release()
# using context manager
with lock:
# critical section here...
</code></pre>
Dynamic function parameters using annotations (Python)
2012-02-22T22:31:02-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578049-dynamic-function-parameters-using-annotations/
<p style="color: grey">
Python
recipe 578049
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/decorator/">decorator</a>).
</p>
<p>Python default values for keyword arguments are evaluated only once and not on every function call. For example following function will not work as user may expect:</p>
<pre class="prettyprint"><code>def printNow(l=[], now=datetime.now()):
l.append(len(l))
print('List:', l, ' id:', id(l))
print('Now:', now)
for i in range(3):
printNow()
print()
</code></pre>
<p>The "dynamic" decorator solves problem by evaluating callables, that are assigned to parameters using annotations syntax (see PEP 3107).</p>
JSON instead of pickle for memcached (Python)
2012-01-10T22:31:36-08:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578011-json-instead-of-pickle-for-memcached/
<p style="color: grey">
Python
recipe 578011
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/memcache/">memcache</a>, <a href="/recipes/tags/memcached/">memcached</a>, <a href="/recipes/tags/pickle/">pickle</a>).
</p>
<p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) does'n know pickle, but json yes.</p>
Pickle to/from socket (Python)
2011-04-19T22:22:53-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/577667-pickle-tofrom-socket/
<p style="color: grey">
Python
recipe 577667
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/socket/">socket</a>).
Revision 2.
</p>
<p>It's useful for transfering objects through socket, when doing communication between processes or networking.</p>
Functional selection sort (Python)
2009-09-29T13:34:35-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/576917-functional-selection-sort/
<p style="color: grey">
Python
recipe 576917
by <a href="/recipes/users/4171837/">pavel</a>
(<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/reduce/">reduce</a>, <a href="/recipes/tags/selection/">selection</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>).
Revision 3.
</p>
<p>This is a variant of selection sort without using for-statements. Do you like it? :-)</p>