Package gavo :: Package utils :: Module codetricks
[frames] | no frames]

Module codetricks

source code

Functions dealing with compilation and introspection of python and external code.

Classes
  CachedGetter
A cache for a callable.
  CachedResource
is like CachedGetter but with a built-in getter.
  DeferredImport
A trivial deferred module loader.
  IdManagerMixin
A mixin for objects requiring unique IDs.
  NullObject
A Null object, i.e.
  Infimum
is a *class* smaller than anything.
  Supremum
is a *class* larger than anything.
  AllEncompassingSet
a set that contains everything.
  memoizedMethod
a trivial memoizing decorator for instance methods.
  EqualingRE
A value that compares equal based on RE matches.
  bytelist
  intlist
  floatlist
  complexlist
Functions
 
document(origFun)
is a decorator that adds a "buildDocsForThis" attribute to its argument.
source code
 
iterDerivedClasses(baseClass, objects)
iterates over all subclasses of baseClass in the sequence objects.
source code
 
iterDerivedObjects(baseClass, objects)
iterates over all instances of baseClass in the sequence objects.
source code
 
buildClassResolver(baseClass, objects, instances=False, key=<__builtin__.function object>, default=None)
returns a function resolving classes deriving from baseClass in the sequence objects by their names.
source code
 
formatDocs(docItems, underliner)
returns RST-formatted docs for docItems.
source code
 
makeClassDocs(baseClass, objects)
prints hopefully RST-formatted docs for all subclasses of baseClass in objects.
source code
 
silence(*args, **kwds)
a context manager to temporarily redirect stdout to /dev/null.
source code
 
in_dir(*args, **kwds)
executes the controlled block within destDir and then returns to the previous directory.
source code
 
sandbox(*args, **kwds)
sets up and tears down a sandbox directory within tmpdir.
source code
 
runInSandbox(setUp, func, tearDown, *args, **kwargs)
runs func in a temporary ("sandbox") directory.
source code
 
ensureExpression(expr, errName='unknown')
raises a LiteralParserError if expr is not a parseable python expression.
source code
 
importModule(modName)
imports a module from the module path.
source code
 
loadPythonModule(fqName, relativeTo=None)
imports fqName and returns the module with a module description.
source code
 
loadInternalObject(relativeName, objectName)
gets a name from an internal module.
source code
 
memoized(origFun)
a trivial memoizing decorator.
source code
 
iterConsecutivePairs(sequence)
yields pairs of consecutive items from sequence.
source code
 
iterRanges(separators)
yields (left, right) pairs for a sequence of separators.
source code
 
getKeyNoCase(dict, key)
returns a key of dict matching key case-insensitively.
source code
 
identity(x)
returns a stringified version of aVal, except an empty string is returned when aVal is None.
source code
 
intToFunnyWord(anInt, translation='\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x1...)
returns a sometimes funny (but unique) word from an arbitrary integer.
source code
 
addDefaults(dataDict, defaultDict)
adds key-value pairs from defaultDict to dataDict if the key is missing in dataDict.
source code
 
memoizeOn(onObject, generatingObject, generatingFunction, *args)
memoizes the result of generatingFunction on onObject.
source code
 
forgetMemoized(ob)
clears things memoizeOn-ed on ob or @utils.memoize-ed.
source code
 
stealVar(varName)
returns the first local variable called varName in the frame stack above my caller.
source code
 
printFrames()
prints a compact list of frames.
source code
 
getTracebackAsString() source code
Variables
  __package__ = 'gavo.utils'
  __warningregistry__ = {('The compiler package is deprecated an...
Function Details

document(origFun)

source code 

is a decorator that adds a "buildDocsForThis" attribute to its argument.

This attribute is evaluated by documentation generators.

buildClassResolver(baseClass, objects, instances=False, key=<__builtin__.function object>, default=None)

source code 

returns a function resolving classes deriving from baseClass in the sequence objects by their names.

This is used to build registries of Macros and RowProcessors. The classes in question have to have a name attribute.

objects would usually be something like globals().values()

If instances is True the function will return instances instead of classes.

key is a function taking an object and returning the key under which you will later access it. If this function returns None, the object will not be entered into the registry.

formatDocs(docItems, underliner)

source code 

returns RST-formatted docs for docItems.

docItems is a list of (title, doc) tuples. doc is currently rendered in a preformatted block.

makeClassDocs(baseClass, objects)

source code 

prints hopefully RST-formatted docs for all subclasses of baseClass in objects.

The function returns True if it finds arguments it expects ("docs" and optionally a char to build underlines from) in the command line, False if not (and it doesn't print anything in this case) if not.

Thus, you'll usually use it like this:

       if __name__=="__main__":        
               if not makeClassDocs(Macro, globals().values()):
                       _test()

silence(*args, **kwds)

source code 

a context manager to temporarily redirect stdout to /dev/null.

This is used to shut up some versions of pyparsing and pyfits that insist on spewing stuff to stdout from deep within in relatively normal situations.

Note that this will acquire a lock while things are silenced; this means that silenced things cannot run concurrently.

Decorators:
  • @contextlib.contextmanager

in_dir(*args, **kwds)

source code 

executes the controlled block within destDir and then returns to the previous directory.

Think "within dir". Haha.

Decorators:
  • @contextlib.contextmanager

sandbox(*args, **kwds)

source code 

sets up and tears down a sandbox directory within tmpdir.

This is is a context manager. The object returned is the original path (which allows you to copy stuff from there). The working directory is the sandbox created while in the controlled block.

If tmpdir is None, the *system* default is used (usually /tmp), rather than dachs' tmpdir. So, you will ususally want to call this as sandbox(base.getConfig("tempDir"))

This is obviously not thread-safe -- you'll not usually want to run this in the main server process. Better fork before running this.

You can pass in a function extractfunc(owd) that is executed in the sandbox just before teardown. It receives the original working directory and can, e.g., move files there from the sandbox.

Decorators:
  • @contextlib.contextmanager

runInSandbox(setUp, func, tearDown, *args, **kwargs)

source code 

runs func in a temporary ("sandbox") directory.

func is called with args and kwargs. setUp and tearDown are two functions also called with args and kwargs; in addition, they are passed the path of the tempdir (setUp) or the path of the original directory (teardown) in the first argument.

setUp is called after the directory has been created, but the process is still in the current WD.

tearDown is called before the temp dir is deleted and in this directory. Its return value is the return value of runInSandbox, which is the preferred way of getting data out of the sandbox.

If any of the handlers raise exceptions, the following handlers will not be called. The sandbox will be torn down, though.

This is only present for legacy code. Use the sandbox context manager now.

importModule(modName)

source code 

imports a module from the module path.

Use this to programmatically import "normal" modules, e.g., dc-internal ones. It uses python's standard import mechanism and returns the module object.

We're using exec and python's normal import, so the semantics should be identical to saying import modName except that the caller's namespace is not changed.

The function returns the imported module.

loadPythonModule(fqName, relativeTo=None)

source code 

imports fqName and returns the module with a module description.

The module description is what what find_module returns; you may need this for reloading and similar.

Do not use this function to import DC-internal modules; this may mess up singletons since you could bypass python's mechanisms to prevent multiple imports of the same module.

fqName is a fully qualified path to the module without the .py, unless relativeTo is given, in which case it is interpreted as a relative path. This for letting modules in resdir/res import each other by saying:

       mod, _ = api.loadPythonModule("foo", relativeTo=__file__)

The python path is temporarily amended with the path part of the source module.

If the module is in /var/gavo/inputs/foo/bar/mod.py, Python will know the module as foo_bar_mod (the last two path components are always added). This is to keep Python from using the module when someone writes import mod.

Decorators:
  • @document

loadInternalObject(relativeName, objectName)

source code 

gets a name from an internal module.

relativeName is the python module path (not including "gavo."), objectName the name of something within the module.

This is used for "manual" registries (grammars, cores,...).

memoized(origFun)

source code 

a trivial memoizing decorator.

Use this for plain functions; see memoizedMethod for instance methods. No cache expiry, no non-hashable arguments, nothing.

iterConsecutivePairs(sequence)

source code 

yields pairs of consecutive items from sequence.

If the last item cannot be paired, it is dropped.

>>> list(iterConsecutivePairs(range(6)))
[(0, 1), (2, 3), (4, 5)]
>>> list(iterConsecutivePairs(range(5)))
[(0, 1), (2, 3)]

iterRanges(separators)

source code 

yields (left, right) pairs for a sequence of separators.

>>> list(iterRanges(range(6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

getKeyNoCase(dict, key)

source code 

returns a key of dict matching key case-insensitively.

This is sometimes useful with protocols that stupidly define keys as case-insensitive.

If no matching key exists, a KeyError is raised.

memoizeOn(onObject, generatingObject, generatingFunction, *args)

source code 

memoizes the result of generatingFunction on onObject.

This is for caching things that adapt to onObjects; see procdefs and rowmakers for examples why this is useful.

forgetMemoized(ob)

source code 

clears things memoizeOn-ed on ob or @utils.memoize-ed.

This is sometimes necessary to let the garbage collector free ob, e.g., when closures have been memoized.

stealVar(varName)

source code 

returns the first local variable called varName in the frame stack above my caller.

This is obviously abominable. This is only used within the DC code where the author deemed the specification ugly. Ah. Almost.

printFrames()

source code 

prints a compact list of frames.

This is an aid for printf debugging.


Variables Details

__warningregistry__

Value:
{('The compiler package is deprecated and removed in Python 3.x.',
  <type 'exceptions.DeprecationWarning'>,
  14): True}