Last active
August 29, 2015 14:27
-
-
Save kannan4k/ba78c06f6113722b7c96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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