Popular recipes by Andrey Nikishaev http://code.activestate.com/recipes/users/4176176/2014-03-18T14:11:20-07:00ActiveState Code RecipesDecorator to check if needed modules for method are imported (Python)
2014-03-18T14:11:20-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578852-decorator-to-check-if-needed-modules-for-method-ar/
<p style="color: grey">
Python
recipe 578852
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/import/">import</a>).
</p>
<p>Check if needed modules imported before run method</p>
<pre class="prettyprint"><code>Example::
@require_module(['time'],exception=Exception)
def get_time():
return time.time()
</code></pre>
Decorator to check method param types (Python)
2014-01-14T11:23:40-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578809-decorator-to-check-method-param-types/
<p style="color: grey">
Python
recipe 578809
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/typecheck/">typecheck</a>).
</p>
<p>This solution give possibility to check method param type, raise needed exception type, and also have good readability in the decorator definition.</p>
Script for cache invalidation at Amazon CloudFront (Python)
2011-07-05T14:40:58-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577779-script-for-cache-invalidation-at-amazon-cloudfront/
<p style="color: grey">
Python
recipe 577779
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/amazon/">amazon</a>, <a href="/recipes/tags/cloudfront/">cloudfront</a>).
</p>
<p>This script scans directories that was uploaded to CloudFront and build files index. When you modify some files, script automatically see what files was modified since the last update, and clear cache on CloudFront only for them.</p>
<p>Usage: script.py data_dir [index_file] [dir_prefix] </p>
<p>data_dir - path to directory with uploaded data</p>
<p>index_file - path to files index</p>
<p>dir_prefix - needed if you data_dir path is different from url at CloudFront.For example: Your data_dir is '/data' but url at CloudFront is <a href="http://url.com/social/data/" rel="nofollow">http://url.com/social/data/</a> so dir_prefix will be '/social/data/'</p>
Linux cat command backward (Python)
2011-05-18T12:54:15-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577699-linux-cat-command-backward/
<p style="color: grey">
Python
recipe 577699
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/cat/">cat</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/output/">output</a>).
Revision 2.
</p>
<p>Utility to output file from backward. Useful for logs output.</p>
Simple local cache and cache decorator (Python)
2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/
<p style="color: grey">
Python
recipe 577492
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Simple local cache.
It saves local data in singleton dictionary with convenient interface</p>
<h4 id="examples-of-use">Examples of use:</h4>
<pre class="prettyprint"><code># Initialize
SimpleCache({'data':{'example':'example data'}})
# Getting instance
c = SimpleCache.getInstance()
c.set('re.reg_exp_compiled',re.compile(r'\W*'))
reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*'))
# --------------------------------------------------------------
c = SimpleCache.getInstance()
reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*'))
# --------------------------------------------------------------
@scache
def func1():
return 'OK'
</code></pre>
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>
Observer Design Pattern for python gevent coroutine package (Python)
2010-12-08T08:33:30-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577491-observer-design-pattern-for-python-gevent-coroutin/
<p style="color: grey">
Python
recipe 577491
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/gevent/">gevent</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This is simple implementation of the observer design pattern. Acting as a registration hub, it fires events when requested.
Also i have gevent.Timeout like interface in situations when you need to run event-method in the same greenlet. Example: </p>
<pre class="prettyprint"><code>e = Observer()
ev = e.wait('kill')
try:
gevent.sleep(3)
except FiredEvent:
print 'Fired!'
else:
print 'Not Fired!'
finally:
ev.cancel()
</code></pre>
<p>But rememeber, if you are using subscribe method, event-method will be executed in another greenlet.</p>