Module t.p.reflect

Part of twisted.python View Source

Standardized versions of various cool and/or strange things that you can do with Python's reflection capabilities.
Line # Kind Name Docs
34 Class Settable A mixin class for syntactic sugar. Lets you assign attributes by
51 Class AccessorType Metaclass that generates properties automatically.
108 Class PropertyAccessor A mixin class for Python 2.2 that uses AccessorType.
145 Class Accessor Extending this class will give you explicit accessor methods; a
199 Class Summer Extend from this class to get the capability to maintain 'related
241 Class QueueMethod I represent a method that doesn't exist yet.
250 Function funcinfo this is more documentation for myself than useful code.
279 Function fullFuncName Undocumented
285 Function qual Return full import path of a class.
289 Function getcurrent Undocumented
297 Function getClass Return the class or type of object 'obj'.
308 Function isinst Undocumented
324 Function namedModule Return a module given its name.
334 Function namedObject Get a fully named module-global object.
343 Function namedAny Get a fully named package, module, module-global object, or attribute.
374 Function _reclass Undocumented
382 Function macro macro(name, source, **identifiers)
420 Function _determineClass Undocumented
426 Function _determineClassName Undocumented
436 Function safe_repr safe_repr(anything) -> string
455 Function safe_str safe_str(anything) -> string
474 Function allYourBase allYourBase(classObj, baseClass=None) -> list of all base
484 Function accumulateBases Undocumented
491 Function prefixedMethodNames A list of method names with a given prefix in a given class.
499 Function addMethodNamesToDict addMethodNamesToDict(classObj, dict, prefix, baseClass=None) -> dict
521 Function prefixedMethods A list of methods with a given prefix on a given instance.
528 Function accumulateMethods accumulateMethods(instance, dict, prefix)
546 Function accumulateClassDict Accumulate all attributes of a given name in a class heirarchy into a single dictionary.
582 Function accumulateClassList Accumulate all attributes of a given name in a class heirarchy into a single list.
593 Function isSame Undocumented
596 Function isLike Undocumented
599 Function modgrep Undocumented
602 Function isOfType Undocumented
608 Function findInstances Undocumented
611 Function objgrep An insanely CPU-intensive process for finding stuff.
658 Function _startswith Undocumented
662 Function filenameToModuleName Convert a name in the filesystem to the name of the Python module it is.
def funcinfo(function):
this is more documentation for myself than useful code.
def fullFuncName(func):
Undocumented
def qual(clazz):
Return full import path of a class.
def getcurrent(clazz):
Undocumented
def getClass(obj):
Return the class or type of object 'obj'. Returns sensible result for oldstyle and newstyle instances and types.
def isinst(inst, clazz):
Undocumented
def namedModule(name):
Return a module given its name.
def namedObject(name):
Get a fully named module-global object.
def namedAny(name):
Get a fully named package, module, module-global object, or attribute.
def _reclass(clazz):
Undocumented
def macro(name, filename, source, **identifiers):

macro(name, source, **identifiers)

This allows you to create macro-like behaviors in python. See twisted.python.hook for an example of its usage.
def _determineClass(x):
Undocumented
def _determineClassName(x):
Undocumented
def safe_repr(o):

safe_repr(anything) -> string

Returns a string representation of an object, or a string containing a traceback, if that object's __repr__ raised an exception.
def safe_str(o):

safe_str(anything) -> string

Returns a string representation of an object, or a string containing a traceback, if that object's __str__ raised an exception.
def allYourBase(classObj, baseClass=None):
allYourBase(classObj, baseClass=None) -> list of all base classes that are subclasses of baseClass, unless it is None, in which case all bases will be added.
def accumulateBases(classObj, l, baseClass=None):
Undocumented
def prefixedMethodNames(classObj, prefix):
A list of method names with a given prefix in a given class.
def addMethodNamesToDict(classObj, dict, prefix, baseClass=None):

addMethodNamesToDict(classObj, dict, prefix, baseClass=None) -> dict this goes through 'classObj' (and its bases) and puts method names starting with 'prefix' in 'dict' with a value of 1. if baseClass isn't None, methods will only be added if classObj is-a baseClass

If the class in question has the methods 'prefix_methodname' and 'prefix_methodname2', the resulting dict should look something like: {"methodname": 1, "methodname2": 1}.
def prefixedMethods(obj, prefix=''):
A list of methods with a given prefix on a given instance.
def accumulateMethods(obj, dict, prefix='', curClass=None):
accumulateMethods(instance, dict, prefix) I recurse through the bases of instance.__class__, and add methods beginning with 'prefix' to 'dict', in the form of {'methodname':*instance*method_object}.
def accumulateClassDict(classObj, attr, adict, baseClass=None):

Accumulate all attributes of a given name in a class heirarchy into a single dictionary.

Assuming all class attributes of this name are dictionaries. If any of the dictionaries being accumulated have the same key, the one highest in the class heirarchy wins. (XXX: If "higest" means "closest to the starting class".)

Ex:

| class Soy: | properties = {"taste": "bland"} | | class Plant: | properties = {"colour": "green"} | | class Seaweed(Plant): | pass | | class Lunch(Soy, Seaweed): | properties = {"vegan": 1 } | | dct = {} | | accumulateClassDict(Lunch, "properties", dct) | | print dct

{"taste": "bland", "colour": "green", "vegan": 1}
def accumulateClassList(classObj, attr, listObj, baseClass=None):

Accumulate all attributes of a given name in a class heirarchy into a single list.

Assuming all class attributes of this name are lists.
def isSame(a, b):
Undocumented
def isLike(a, b):
Undocumented
def modgrep(goal):
Undocumented
def isOfType(start, goal):
Undocumented
def findInstances(start, t):
Undocumented
def objgrep(start, goal, eq=isLike, path='', paths=None, seen=None, showUnknowns=0, maxDepth=None):
An insanely CPU-intensive process for finding stuff.
def _startswith(s, sub):
Undocumented
def filenameToModuleName(fn):

Convert a name in the filesystem to the name of the Python module it is.

This is agressive about getting a module name back from a file; it will always return a string. Agressive means 'sometimes wrong'; it won't look at the Python path or try to do any error checking: don't use this method unless you already know that the filename you're talking about is a Python module.