Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > xray module

Thread Tools

xray module

Robert Brewer
Posts: n/a
I finally cleaned up the code I've been using for a while now to grab
objects out of modules via dotted-package names (all this work to avoid
using exec! Sheesh!). Feel free to use it, abuse it, or comment on it. I
realize many of you already do this on your own--this is just sugar for
the newbies. I'm releasing/announcing it for three reasons:

1. The ego rush of authorship,
2. I continually hear people asking for similar (if not exact)
functionality, especially those coming from a Java/Tomcat background,
3. The name is cool. I spent a lot of time on it.

Here 'tis:

"""Load modules, classes, functions, and attributes by dotted package


desiredClass = xray.classes("myapp.strtools.alwayslowercase", str)
newInstance = desiredClass()


for f in startup_function_names:
func_object = xray.functions(f)


I try to create systems that are configurable by deployers; mostly
becase I
hate it when an application forces me to use the database, filesystem,
communication protocol, algorithm, or operating system du jour.
these basic components of an application is its own study (look up the
of Four "Strategy" pattern for a start); this is often accomplished with
classes which share signatures, often via subclassing. However, the
then needs to specify the class they wish to use. This module gives them
the ability to specify that with a single string (for example, in a text
configuration file).


import sys

def modules(modulePath):
"""Load a module and retrieve a reference to that module."""
aMod = sys.modules[modulePath]
if aMod is None:
raise KeyError
except KeyError:
# The last [''] is important.
aMod = __import__(modulePath, globals(), locals(), [''])
sys.modules[modulePath] = aMod
return aMod

def attributes(fullAttributeName):
"""Load a module and retrieve an attribute of that module."""

# Parse out the path, module, and attribute
lastDot = fullAttributeName.rfind(u".")
attrName = fullAttributeName[lastDot + 1:]
modPath = fullAttributeName[:lastDot]

aMod = modules(modPath)
# Let an AttributeError propagate outward.
anAttr = getattr(aMod, attrName)

# Return a reference to the attribute.
return anAttr

def functions(fullFuncName):
"""Load a module and retrieve a function object."""

aFunc = attributes(fullFuncName)

# Assert that the function is a *callable* attribute.
if not callable(aFunc):
raise TypeError(u"'%s' is not callable." % fullFuncName)

# Return a reference to the function itself,
# not the results of the function.
return aFunc

def classes(fullClassName, parentClass=None):
"""Load a module and retrieve a class (NOT an instance).

If the parentClass is supplied, className must be of parentClass
or a subclass of parentClass (or None is returned).
aClass = functions(fullClassName)

# Assert that the class is a subclass of parentClass.
if parentClass is not None:
if not issubclass(aClass, parentClass):
raise TypeError(u"'%s' is not a subclass of %s" %
(fullClassName, parentClass.__name__))

# Return a reference to the class itself, not an instantiated
return aClass

# end

Robert Brewer
Amor Ministries Removed)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: XRAY BEEMS INTO YER CAR! hwf Computer Support 0 10-06-2010 03:05 PM
display xray images displayname ASP General 9 12-12-2009 10:17 PM
Photoshop CS3 and DCM files (xray images) Rudy Benner Digital Photography 10 03-02-2007 04:54 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
DO airport xray machines harm digicams? Ken Scharf Digital Photography 7 08-30-2004 04:08 PM