Table of contents Index

Module bhoelHelper.CheckArgs

Declared in module bhoelHelper

class ArgError(TypeError)
class CheckArgs
def checkArgs(default, data, kw, name)
def tst()
string __author__ = 'Berthold H\366llmann, bhoel@starship.python.net'
string __file__ = '../bhoelHelper/CheckArgs.pyc'
string __version__ = '1.1'

Description

This module provides a class named CheckArgs and a function named checkArgs.

The class CheckArgs is ment to be used as base class for own classes. After the __init__ method is called a dictionary args is added to the local namespace.

The init mathod has four arguments.

The first is, as usual, the self handler. The second is a list or tuples holding the allowed keyword arguments as first tuple element and their default values as remaining tuple elements.

The third and forth argument are the *data and **kw arguments from the implemented class.

Example:

    >>> from CheckArgs import CheckArgs
    >>> class samp(CheckArgs):
    ...     def __init__(self, *data, **kw):
    ...         default = (('A', A'), ('B, B'), ('C, ('C', 1, 2)))
    ...         CheckArgs.__init__(self, default, data, kw)
    ...         print self.args['A'], self.args['B'], self.args['C']
    ... 
    >>> A = samp(A = 34)
    34 B ('C', 1, 2)
    >>> A = samp(34, "HALLO")
    34 HALLO ('C', 1, 2)
    >>> A = samp(D = 34)
    Traceback (innermost last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 4, in __init__
      File "CheckArgs.py", line 64, in __init__
        self.args = checkArgs(default, data, kw, self.__class__.__name__)
      File "CheckArgs.py", line 79, in checkArgs
        raise ArgError, "%s: unexpected keyword argument: %s" %     CheckArgs.ArgError: samp: unexpected keyword argument: D
    >>> 

The function checkArgs provides similar funtionality for functions. It also takes four arguments.

The first to third argument are simlar to the second to forth element of the class. The forth element of the function is the functions name.

Example:

    >>> from CheckArgs import checkArgs
    >>> def tst(*data, **kw):
    ...     default = (('A', A'), ('B, B'), ('C, ('C', 1, 2)))
    ...     print checkArgs(default, data, kw, 'tst')
    ... 
    >>> tst(A = 34)
    {'B': B, C: ('C', 1, 2), A: 34}
    >>> tst(34)
    {'B': B, C: ('C', 1, 2), A: 34}
    >>> tst(D = 34)
    Traceback (innermost last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 3, in tst
      File "CheckArgs.py", line 100, in checkArgs
        raise ArgError, "%s: unexpected keyword argument: %s" %     CheckArgs.ArgError: tst: unexpected keyword argument: D
    >>> 

Author: Berthold H\366llmann
Version: 1.1