Package gmisclib :: Module g_implements
[frames] | no frames]

Module g_implements

source code

This module tells you if an object's signature matches a class. It lets you know if one class can substitute for another, even if they are unrelated in an OO hierarchy.

This allows you to begin to check that a foreign object will have the necessary properties to be used in your code. It is more of a functional check than isimplements().

For instance, if your code needs a write() method, you can check for it like this:

       class some_template(object):
               def write(self, somearg):
                       pass
       #
       g_implements.check(x, some_template)
       #... at this point, we can be assured that a
       # call to x.write(3) is possible.
       # (Note that we don't know what the call will *do*,
       # merely that the proper method of x exists and that
       # it can take an argument.

Semi-kluge: if you have an instance, i that needs to convince g_implements that it can actually behave as class C, then set i.__g_implements__ = ['C']. Generally, this can be a list or set of class names. This trick is useful for C-implementations of python classes (where introspection cannot get all the necessary information) or for fancy python classes that use __getattribute__ and similar.

Classes
  GITypeError
Exception raised by check to indicate failure.
Functions
 
Vartype(mem, com) source code
 
Strict(mem, com) source code
 
why(instance, classobj, ignore=None)
Explains why instance doesn't implement the classobj.
source code
 
impl(instance, classobj)
Tells you if an instance of an object implements a class.
source code
 
make_optional(x)
This is a decorator for a function.
source code
 
make_varargs(x)
This is a decorator for a function.
source code
 
make_vartype(x)
This is a decorator for a function.
source code
 
make_strict(x)
This is a decorator for a function.
source code
 
check(instance, classobj, ignore=None)
Require that instance has the attributes defined by classobj.
source code
 
test() source code
Variables
  MAXCACHE = 100
  Optional = 'optional'
  Varargs = 'varargs'
  Ignore = set(['__class__', '__delattr__', '__dict__', '__doc__...
  __package__ = 'gmisclib'

Imports: inspect


Function Details

why(instance, classobj, ignore=None)

source code 

Explains why instance doesn't implement the classobj.

Parameters:
  • instance - an instance of a class
  • classobj - a class object
Returns:
None (if it does) or a string explanation of why it doesn't @rtype None or str

impl(instance, classobj)

source code 

Tells you if an instance of an object implements a class. By implements, I mean that the instance supplies every member that the class supplies, and every member has the same type. The instance may have *more* members, of course. Functions require that the argument names must match, too at least as far as the required arguments in the classobj's function.

The match may be made looser by adding a g_implements attribute to various class members. Possibilities for the value are Optional, Strict, Vartype, Varargs, or you can give a two-argument function, and that function will be called to decide whether the match is acceptable or not.

make_optional(x)

source code 

This is a decorator for a function. make_optional implies make_varargs.

check(instance, classobj, ignore=None)

source code 

Require that instance has the attributes defined by classobj. This function either quietly succeeds or it raises a GITypeError exception.

Parameters:
  • instance (any instance of a class (i.e. anything with a __class__ attribute).) - the thing to check
  • classobj (any class object (i.e. not an instance, typically).) - a class that provides a pattern of attributes.
Raises:
  • GITypeError - instance doesn't provide all the features of classobj.

Variables Details

Ignore

Value:
set(['__class__',
     '__delattr__',
     '__dict__',
     '__doc__',
     '__getattribute__',
     '__hash__',
     '__module__',
     '__new__',
...