A basic Namespace metaclass for PythonSuppose that you want to conveniently abuse classes as namespaces. For me, this means avoiding having to splatter
Clearly the solution to this problem is a metaclass to do all of the work for me. Here's one, which is deliberately somewhat simple: from types import FunctionType
class NSMeta(type):
def __new__(meta, cname, bases, cdict):
ndict = {}
for name, aval in cdict.items():
if type(aval) == FunctionType:
aval = staticmethod(aval)
ndict[name] = aval
return type.__new__(meta, cname, \
bases, ndict)
# Inherit from this class:
class Namespace(object):
__metaclass__ = NSMeta
# An example:
class uint16(Namespace):
def encode(val):
return ...
def decode(data):
return ...
(To fully exhibit my twitch about aesthetics and not repeating myself,
I set up the A really superintelligent metaclass Writing such a superintelligent metaclass is left as an exercise for the reader. Update: code in haste, repent slightly later. I have realized that
this metaclass should at least only apply |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |