Skip to content

Instantly share code, notes, and snippets.

@kannan4k
Last active August 29, 2015 14:27
Show Gist options
  • Save kannan4k/ba78c06f6113722b7c96 to your computer and use it in GitHub Desktop.
Save kannan4k/ba78c06f6113722b7c96 to your computer and use it in GitHub Desktop.
class DBInterfaceMeta(type):
# we use __init__ rather than __new__ here because we want
# to modify attributes of the class *after* they have been
# created
def __init__(cls, name, bases, dct):
if not hasattr(cls, 'registry'):
# this is the base class. Create an empty registry
print "Inside if"+str(cls)
print name
print bases
print dct
cls.registry = {}
else:
# this is a derived class. Add cls to the registry
print "inside else, cls:"+str(cls)
print name
print bases
print dct
interface_id = name.lower()
cls.registry[interface_id] = cls
print "cls.registry :"
print cls.registry
super(DBInterfaceMeta, cls).__init__(name, bases, dct)
class DBInterface(object):
__metaclass__ = DBInterfaceMeta
model = None
@classmethod
def add_field(cls, name):
cls.model = name
print(DBInterface.registry)
class FirstInterface(DBInterface):
FirstInterface.add_field("User")
class SecondInterface(DBInterface):
pass
class SecondInterfaceModified(SecondInterface):
pass
print(DBInterface.registry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment