Popular recipes by Oren Tirosh http://code.activestate.com/recipes/users/2033964/2017-01-03T10:31:26-08:00ActiveState Code Recipesctypes CDLL with automatic errno checking (Python) 2017-01-03T10:31:26-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/580741-ctypes-cdll-with-automatic-errno-checking/ <p style="color: grey"> Python recipe 580741 by <a href="/recipes/users/2033964/">Oren Tirosh</a> . </p> <p>This class extends ctypes.CDLL with automatic checking of errno and automatically raises an exception when set by the function.</p> Probably the fastest memoization decorator in the world (Python) 2012-08-02T07:27:12-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/ <p style="color: grey"> Python recipe 578231 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memo/">memo</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>With apologies to a certain European brand of beer, this is probably the fastest memoization decorator in the world. Implemented using the __missing__ method in a dict subclass. </p> Mixin for pickling objects with __slots__ (Python) 2013-01-21T08:55:57-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578433-mixin-for-pickling-objects-with-__slots__/ <p style="color: grey"> Python recipe 578433 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/unpickle/">unpickle</a>, <a href="/recipes/tags/__slots__/">__slots__</a>). </p> <p>This mixin makes it possible to pickle/unpickle objects with __slots__ defined. </p> One-liner to show the caller of the current function. (Python) 2013-02-12T13:13:16-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578422-one-liner-to-show-the-caller-of-the-current-functi/ <p style="color: grey"> Python recipe 578422 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/_getframe/">_getframe</a>). </p> <p>This is a quick one-liner to produce the filename and line number of the caller of the current function.</p> Code to source and back (Python) 2012-12-01T08:59:49-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578353-code-to-source-and-back/ <p style="color: grey"> Python recipe 578353 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/decompile/">decompile</a>, <a href="/recipes/tags/recompile/">recompile</a>, <a href="/recipes/tags/uncompile/">uncompile</a>). </p> <p>Converts a code object to a source code snippet and back: <code>c == recompile(*uncompile(c))</code></p> <p>This is useful, for example, if you want to apply an AST transformation to the code.</p> How to read millions of hexadecimal numbers into a numpy array quickly (Python) 2012-06-27T06:03:40-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578177-how-to-read-millions-of-hexadecimal-numbers-into-a/ <p style="color: grey"> Python recipe 578177 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/numpy/">numpy</a>). </p> <p>The numpy.fromfile() function supports binary formats or decimal text. How do you read millions of hexadecimal numbers quickly?</p> Monotonic local time. And sandwich. (Python) 2013-01-03T08:28:36-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578270-monotonic-local-time-and-sandwich/ <p style="color: grey"> Python recipe 578270 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/daylight/">daylight</a>, <a href="/recipes/tags/dst/">dst</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>A Mutton, Lettuce and Tomato sandwich is generally agreed to be the greatest thing in the world. But a different kind of MLT comes in at a close second: Monotonic Local Time. This is a time value in your local timezone that takes care of that annoying hour that happens once a year where time goes backwards. It works by extending the previous day by a couple of hours (24, 25 etc) until after the switch.</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> Immutable class decorator (Python) 2012-08-05T08:30:41-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578233-immutable-class-decorator/ <p style="color: grey"> Python recipe 578233 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/immutable/">immutable</a>). </p> <p>Apply this decorator to a class with __slots__. Members will be mutable during the execution of __init__ but read-only afterwards.</p> Context manager to atomically replace a file (Python) 2012-06-17T12:09:49-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578166-context-manager-to-atomically-replace-a-file/ <p style="color: grey"> Python recipe 578166 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/replace/">replace</a>). Revision 3. </p> <p>This context manager ensures that a file's content is either replaced atomically with new contents or left unchanged. An exception or other error will not leave it empty or with partial content.</p> Context manager to prevent calling code from catching exceptions (Python) 2012-06-17T09:20:56-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/577863-context-manager-to-prevent-calling-code-from-catch/ <p style="color: grey"> Python recipe 577863 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/exception/">exception</a>). </p> <p>The following context manager causes any exceptions raised inside it to print a stack trace and exit immediately. The calling scope is not given a chance to catch the exception.</p> Teach the hashbang header new tricks using a dual mode shell/python script (Python) 2011-08-21T07:27:05-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/577851-teach-the-hashbang-header-new-tricks-using-a-dual-/ <p style="color: grey"> Python recipe 577851 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/hashbang/">hashbang</a>, <a href="/recipes/tags/interpreter/">interpreter</a>, <a href="/recipes/tags/options/">options</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sh/">sh</a>, <a href="/recipes/tags/shell/">shell</a>). </p> <p>This dual-mode script is both a Posix shell script and a python script. The shell part looks like a triple-quoted string to the Python interpreter. The shell does not reach anything after the exec statement.</p> Pickle the interactive interpreter state (Python) 2008-05-20T10:36:50-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/572213-pickle-the-interactive-interpreter-state/ <p style="color: grey"> Python recipe 572213 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>This code extends the pickle module to enable pickling of functions and classes defined interactively at the command prompt. You can save the interpreter state by pickling the __main__ module and restore it later.</p> Unicode repr (Python) 2007-12-22T04:41:40-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/541082-unicode-repr/ <p style="color: grey"> Python recipe 541082 by <a href="/recipes/users/2033964/">Oren Tirosh</a> . </p> <p>Alternative to the builtin repr() uses the \N{FULL NAME} format instead of \uXXXX for unicode strings.</p> Frozen dictionaries (Python) 2005-05-16T06:20:36-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/414283-frozen-dictionaries/ <p style="color: grey"> Python recipe 414283 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>A frozendict is a dictionary that cannot be modified after being created - but it is hashable and may serve as a member of a set or a key in a dictionary.</p> Finding the last item in a loop (Python) 2005-03-17T08:37:03-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/392015-finding-the-last-item-in-a-loop/ <p style="color: grey"> Python recipe 392015 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>An method to detect the last item in a loop.</p>