Package wtf :: Module util
[hide private]
[frames] | no frames]

Module util

source code

Certain utilities to make the life more easy.


Author: André Malo

Classes [hide private]
  ImportWarning
A package import failed, but is configured to not be fatal
  BaseDecorator
Base decorator class
  PooledInterface
Interface for pooled objects
  BasePool
Abstract pool of arbitrary objects
  Version
Represents the package version
Functions [hide private]
callable
decorating(decorated, extra=None, skip=0)
Create decorator for designating decorators.
source code
callable
make_setarg(arg, func)
Find argument position and create arg setter:
source code
any
load_dotted(name)
Load a dotted name
source code
tuple
make_dotted(name)
Generate a dotted module
source code
iterable
walk_package(package, errors='ignore')
Collect all modules and subpackages of package recursively
source code
tuple or str
parse_socket_spec(spec, default_port=None, any=False, _hpre=hpre)
Parse a socket specification
source code
property
Property(func)
Property with improved docs handling
source code
list
find_public(space)
Determine all public names in space
source code
int
hash32(s)
Replacement for str.__hash__
source code
Variables [hide private]
  __package__ = 'wtf'
Function Details [hide private]

decorating(decorated, extra=None, skip=0)

source code 
Create decorator for designating decorators.
Parameters:
  • decorated (function) - Function to decorate
  • extra (dict) - Dict of consumed keyword parameters (not existing in the originally decorated function), mapping to their defaults. If omitted or None, no extra keyword parameters are consumed. The arguments must be consumed by the actual decorator function.
  • skip (int) - Skip positional parameters at the beginning
Returns: callable
Decorator

make_setarg(arg, func)

source code 

Find argument position and create arg setter:

The signature of the setter function is:

def setarg(args, kwargs, value_func):
    '''
    Set argument

    The argument is set only if the `value_func` function says so::

        def value_func(oldval):
            '''
            Determine argument value

            :Parameters:
              `oldval` : any
                Value passed in

            :Return: A tuple containing a boolean if the value is
                     new and should be set (vs. to leave the
                     passed-in value) and the final value
            :Rtype: ``tuple``
            '''

    :Parameters:
      `args` : sequence
        Positional arguments

      `kwargs` : ``dict``
        Keyword arguments

      `value_func` : ``callable``
        Value function

    :Return: A tuple containing `value_func`'s return value, the
             new args and the new kwargs.
    :Rtype: ``tuple``
    '''
Parameters:
  • arg (str) - Argument name
  • func (callable) - Function to call
Returns: callable
arg setter function(args, kwargs, value_func)

load_dotted(name)

source code 

Load a dotted name

The dotted name can be anything, which is passively resolvable (i.e. without the invocation of a class to get their attributes or the like). For example, name could be 'wtf.util.load_dotted' and would return this very function. It's assumed that the first part of the name is always is a module.

Parameters:
  • name (str) - The dotted name to load
Returns: any
The loaded object
Raises:
  • ImportError - A module in the path could not be loaded

make_dotted(name)

source code 
Generate a dotted module
Parameters:
  • name (str) - Fully qualified module name (like wtf.services)
Returns: tuple
The module object of the last part and the information whether the last part was newly added ((module, bool))
Raises:
  • ImportError - The module name was horribly invalid

walk_package(package, errors='ignore')

source code 
Collect all modules and subpackages of package recursively
Parameters:
  • package (module or str) - The package to inspect, if it's a string the string is interpreted as a python package name and imported first. A failed import of this package cannot be suppressed.
  • errors (str) - What should happen on ImportError``s during the crawling process? The following values are recognized: ``ignore, warn, error
Returns: iterable
Iterator over the modules/packages (including the root package)
Raises:
  • ImportError - some import failed
  • ValueError - The errors value could nto be recognized
  • OSError - Something bad happened while accessing the file system

parse_socket_spec(spec, default_port=None, any=False, _hpre=hpre)

source code 

Parse a socket specification

This is either u'host:port' or u'/foo/bar'. The latter (spec containing a slash) specifies a UNIX domain socket and will be transformed to a string according to the inherited locale setting. It may be a string initially, too.

For internet sockets, the port is optional (will be default_port then). If any is true, the host may point to ANY. That is: the host is u'*' or the port stands completely alone (e.g. u'80'). Hostnames will be IDNA encoded.

Parameters:
  • spec (basestring) - The socket spec
  • default_port (int) - The default port to apply
  • any (bool) - Allow host resolve to ANY?
Returns: tuple or str
The determined spec. It may be a string (for UNIX sockets) or a tuple of host and port for internet sockets. (('host', port))
Raises:
  • ValueError - Unparsable spec

Property(func)

source code 
Property with improved docs handling
Parameters:
  • func (callable) - The function providing the property parameters. It takes no arguments as returns a dict containing the keyword arguments to be defined for property. The documentation is taken out the function by default, but can be overridden in the returned dict.
Returns: property
The requested property

find_public(space)

source code 
Determine all public names in space
Parameters:
  • space (dict) - Name space to inspect
Returns: list
List of public names

hash32(s)

source code 

Replacement for str.__hash__

The function is supposed to give identical results on 32 and 64 bit systems.

Parameters:
  • s (str) - The string to hash
Returns: int
The hash value