Utilities & Logging

Utilities

sy.util.memoize(f)

Function decorator that caches return values

If the decorated function is called with the same arguments a cached response is returned. This is useful for functions that does system calls, like talking over the network or accessing the filesystem and whose value wont be changed during the lifetime of the process:

@sy.util.memoize
def uname(arg):
    print 'Getting uname', arg
    return sy.cmd.outlines('uname -{}', arg)[0]

uname('r')
Getting uname r

uname('s')
Getting uname s

uname('r') # cached value returned

The decorator creates a pickle of the arguments and uses it as cache key. If the arguments cant be pickled it will throw a pickle.PickleError.

class sy.util.retry(tries, exceptions=None, delay=0)

Decorator for retrying function if exception occurs:

@sy.util.retry
def errorprone():
    raise Exception('fail')

Note

Hides all raised exceptions except the last one.

Parameters:
  • tries – Num tries
  • exceptions – Exceptions to catch
  • delay – Wait between retries
Copied from Peter Hoffmann:
http://peter-hoffmann.com/2010/retry-decorator-python.html

Todo

Not tested

default_exceptions

alias of Exception

Logging

synopsis:Logging management

Todo

Create and document log handling

Table Of Contents

Previous topic

Prompt user for information

Next topic

Networking

This Page