Package commons :: Module decs
[hide private]
[frames] | no frames]

Module decs

source code

Decorators and decorator utilities.


To Do: Move the actual decorators to modules based on their topic.

Classes [hide private]
  GenericWrapper
Wrap all of an object's methods with before/after calls.
Functions [hide private]
 
recursion_guard(retval, guard_name='____recursion_guard')
Prevents a methods from recursively calling itself.
source code
 
wrap_callable(any_callable, before, after)
Wrap any callable with before/after calls.
source code
 
xmlrpc_safe(func)
Makes a procedure "XMLRPC-safe" by returning 0 whenever the inner function returns None.
source code
 
file_memoized(serializer, deserializer, pathfunc)
The string result of the given function is saved to the given path.
source code
 
file_string_memoized(pathfunc)
Wrapper around file_memoized that expects the decorated function to return strings, so the string is written verbatim.
source code
 
pickle_memoized(pathfunc)
Wrapper around file_memoized that uses pickle.
source code
Variables [hide private]
  HIGHEST_PROTOCOL = 2
  __package__ = 'commons'
  compatible_formats = ['1.0', '1.1', '1.2', '1.3', '2.0']
  format_version = '2.0'
Function Details [hide private]

recursion_guard(retval, guard_name='____recursion_guard')

source code 

Prevents a methods from recursively calling itself. On recursion, it will return retval instead. Note that this only protects methods, not functions. To implement something similar for functions may call for some thread-local state or some stack-walking.

wrap_callable(any_callable, before, after)

source code 

Wrap any callable with before/after calls.

From the Python Cookbook. Modified to support None for before or after.

Parameters:
  • any_callable (function) - The function to decorate.
  • before (function) - The pre-processing procedure. If this is None, then no pre-processing will be done.
  • after (function) - The post-processing procedure. If this is None, then no post-processing will be done.

Copyright: O'Reilly Media

xmlrpc_safe(func)

source code 

Makes a procedure "XMLRPC-safe" by returning 0 whenever the inner function returns None. This is useful because XMLRPC requires return values, and 0 is commonly used when functions don't intend to return anything.

Also, if the procedure returns a boolean, it will be wrapped in xmlrpclib.Boolean.

Parameters:
  • func (function) - The procedure to decorate.

file_memoized(serializer, deserializer, pathfunc)

source code 

The string result of the given function is saved to the given path.

Example:

   @file_memoized(lambda x,f: f.write(x),
                  lambda f: f.read(),
                  lambda: "/tmp/cache")
   def foo(): return "hello"

   @file_memoized(pickle.dump,
                  pickle.load,
                  lambda x,y: "/tmp/cache-%d-%d" % (x,y))
   def foo(x,y): return "hello %d %d" % (x,y)
Parameters:
  • serializer (function) - The function to serialize the return value into a string. This should take the return value object and the file object.
  • deserializer (function) - The function te deserialize the cache file contents into the return value. This should take the file object and return a string.
  • pathfunc (str) - Returns the path where the files should be saved. This should be able to take the same arguments as the original function.