Custom Search

Monday, December 13, 2010

Django Model and Frame class working technique

#Django Model and Frame class working technique.
#Technique of, Django Model classes declaring table columns as class variables.
#Technique of, Django Frame classes declaring fields as class variables.

class MyMetaClass(type):
def __new__(meta, classname, bases, classDict):
print "\n--------MyMetaClass-------__new__-----"
print '\n----meta:---------', meta
print '\n----Class Name:---------', classname
print "\n----Bases:(base classes of class 'Test')---------", bases
print "\n----Class Attributes:(class variables and methods (not member datas)
from class 'Test' )---------", classDict
return type.__new__(meta, classname, bases, classDict)

def __init__(cls, *arg):
print "\n--------MyMetaClass-------__init__-----", arg
print "\n--------MyMetaClass-------cls---------", cls
print "\n--------MyMetaClass-------dir(cls)---------", dir(cls)


class BaseCls(object):
def __init__(self, *arg):
print "\n--------BaseCls-------__init__-----", arg

def base_cls_method(self):
print "\n--------BaseCls-------basecls_method-----"


class Test(BaseCls):

my_class_var1 = 1000
my_class_var2 = 2000
#all class variables and methods of this class are passed as dictionary in to metaclass
'MyMetaClass'.
#In metaclass these variables and methods are can access in __new__ class special method
from last(3rd) argument.
#Django framework using this technique in Model and Frame classes for create fields.

__metaclass__ = MyMetaClass
#methods in this class are passed into the metaclass 'MyMetaClass' as functions.

def __init__(self, *arg):
self.number = arg
#member datas in this class are not passed into the metaclass.
print "\n--------Test-------__init__-----", arg
print "\n--------Test-------self---------", self
print "\n--------Test-------dir(self)---------", dir(self)

def test_method(self):
#This method is passed into the metaclass 'MyMetaClass' as functions.
print "\n--------Test-------test_method-----"


print "\n-------Test()------",Test(5000)



OUTPUT
======

# python aa.py

--------MyMetaClass-------__new__-----

----meta:---------

----Class Name:--------- Test

----Bases:(base classes of class 'Test')--------- (,)

----Class Attributes:(class variables and methods (not member datas) from class 'Test' )
--------- {'__module__': '__main__', '__metaclass__': , 'test_method': , 'my_class_var2': 2000,
'my_class_var1': 1000, '__init__': }

--------MyMetaClass-------__init__----- ('Test', (,), {'__module__': '__main__',
'__metaclass__': , 'test_method': , 'my_class_var2': 2000, 'my_class_var1': 1000,
'__init__': })

--------MyMetaClass-------cls---------

--------MyMetaClass-------dir(cls)--------- ['__class__', '__delattr__', '__dict__',
'__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'base_cls_method',
'my_class_var1', 'my_class_var2', 'test_method']

-------Test()------
--------Test-------__init__----- (5000,)

--------Test-------self--------- <__main__.test>

--------Test-------dir(self)--------- ['__class__', '__delattr__', '__dict__', '__doc__',
'__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__weakref__', 'base_cls_method', 'my_class_var1', 'my_class_var2',
'number', 'test_method']
<__main__.test>

--------------------------------------------------------------

No comments:

Post a Comment