class MyClass:
def __init__(self, name):
self.amount = 100
self.name = name
def test(self):
print "test"
m1 = MyClass('SAJU')
print "\n-------m1---------", m1
print "\n--------dir(m1)----------", dir(m1)
print "\n--------vars(m1)----------", vars(m1)
print "\n---------m1.__dict__-----------------", m1.__dict__
print "\n----------m1.__class__.__name__----------------", m1.__class__.__name__
print "\n===============================================1"
class EmptyClass:
pass
e1 = EmptyClass()
print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================2"
e1.__class__ = m1.__class__
print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================3"
e1.__dict__.update(m1.__dict__)
print "\n-------e1---------", e1
print "\n--------dir(e1)----------", dir(e1)
print "\n--------vars(e1)----------", vars(e1)
print "\n---------e1.__dict__-----------------", e1.__dict__
print "\n----------e1.__class__.__name__----------------", e1.__class__.__name__
print "\n-------isinstance(e1, EmptyClass)--------", isinstance(e1, EmptyClass)
print "\n-------isinstance(e1, MyClass)--------", isinstance(e1, MyClass)
print "\n===============================================4"
OUTPUT
=======
-------m1--------- <__main__.myclass>
--------dir(m1)---------- ['__doc__', '__init__', '__module__', 'amount', 'name', 'test']
--------vars(m1)---------- {'amount': 100, 'name': 'SAJU'}
---------m1.__dict__----------------- {'amount': 100, 'name': 'SAJU'}
----------m1.__class__.__name__---------------- MyClass
===============================================1
-------e1--------- <__main__.emptyclass>
--------dir(e1)---------- ['__doc__', '__module__']
--------vars(e1)---------- {}
---------e1.__dict__----------------- {}
----------e1.__class__.__name__---------------- EmptyClass
-------isinstance(e1, EmptyClass)-------- True
-------isinstance(e1, MyClass)-------- False
===============================================2
-------e1--------- <__main__.myclass>
--------dir(e1)---------- ['__doc__', '__init__', '__module__', 'test']
--------vars(e1)---------- {}
---------e1.__dict__----------------- {}
----------e1.__class__.__name__---------------- MyClass
-------isinstance(e1, EmptyClass)-------- False
-------isinstance(e1, MyClass)-------- True
===============================================3
-------e1--------- <__main__.myclass>
--------dir(e1)---------- ['__doc__', '__init__', '__module__', 'amount', 'name', 'test']
--------vars(e1)---------- {'amount': 100, 'name': 'SAJU'}
---------e1.__dict__----------------- {'amount': 100, 'name': 'SAJU'}
----------e1.__class__.__name__---------------- MyClass
-------isinstance(e1, EmptyClass)-------- False
-------isinstance(e1, MyClass)-------- True
===============================================4
Custom Search
Wednesday, January 26, 2011
python django working of object copy technique
python django working of object copy technique
python django howto class name as string
python django how to get class name of an object as string
>>> l = []
>>>
>>> l.__class__.__name__
'list'
===================
>>> t = ()
>>>
>>> t.__class__.__name__
'tuple'
===================
>>> d = {}
>>>
>>> d.__class__.__name__
'dict'
===================
>>> i = int(4)
>>>
>>> i
4
>>> i.__class__.__name__
'int'
===================
>>> f = float(4)
>>>
>>> f
4.0
>>>
>>> f.__class__.__name__
'float'
-------------------------
>>> class Test:
... pass
...
>>>
>>>
>>> t = Test()
>>>
>>> t.__class__.__name__
'Test'
===================
main uses of python __class__
main uses of python __class__
>>> l = [2,4]
>>> l
[2, 4]
>>> l.__class__
type 'list'>
>>> l2 = l.__class__()
>>> l2
[]
>>> l.__class__.__name__
'list'
=====================
>>> l = [2,4]
>>> l
[2, 4]
* Return class (class object) of object l.
>>> l.__class__
type 'list'>
* Creating object of that class (class object) by putting "()" at the end.
>>> l2 = l.__class__()
>>> l2
[]
* To get class name of a python object
>>> l.__class__.__name__
'list'
===================== following are equal
>>> l.__class__
type 'list'>
>>> type(l)
type 'list'>
===================== following are equal
>>> l2 = l.__class__()
>>> l2
[]
>>> l2 = type(l)()
>>> l2
[]
=====================
* You can get the class (class object) of any object by calling "type" on it.
>>> type(l)
type 'list'>
* we can create object of that class (class object) by putting "()" at the end.
>>> lt = type(l)
>>> lt
type 'list'>
>>> lt() <---------"()" at the end.
[]
=====================
Saturday, January 22, 2011
python defining special methods __eq__ and __le__ on a class
python defining special methods __eq__ and __le__ on a class
class A:
def __init__(self, num):
self.num = num
def __add__(self, second_num):
return self.num + second_num
def __eq__(self, second_num):
if self.num == second_num:
print "\nNumbers are equal"
else:
print "\nNumbers are not equal"
def __le__(self, second_num):
if self.num <= second_num:
print "\n"+str(self.num)+" is less than or equal to "+str(second_num)
else:
print "\n"+str(self.num)+" is grater than "+str(second_num)
a1 = A(10)
print "-------a1 + 10-------", a1 + 40
print "-------a1 == 10-------", a1 == 10
print "-------a1 == 10-------", a1 == 20
print "-------a1 <= 30 -------", a1 <= 30
print "-------a1 <= 5 -------", a1 <= 5
OUTPUT
=======
-------a1 + 10------- 50
-------a1 == 10-------
Numbers are equal
None
-------a1 == 10-------
Numbers are not equal
None
-------a1 <= 30 -------
10 is less than or equal to 30
None
-------a1 <= 5 -------
10 is grater than 5
None
python defining special method __add__ on a class
python defining special method __add__ on a class
======================== 1
class A:
def __init__(self, num):
self.num = num
def __add__(self, second_num):
return self.num + second_num
a1 = A(10)
print "-------a1 + 10-------", a1 + 40
print "-------a1 + a1-------", a1 + a1
OUTPUT
=======
-------a1 + 10------- 50 <--------
-------a1 + a1-------
Traceback (most recent call last):
File "sp.py", line 16, in
print "-------a1 + a1-------", a1 + a1
File "sp.py", line 10, in __add__
return self.num + second_num
TypeError: unsupported operand type(s) for +: 'int' and 'instance'
======================== 2
class A:
def __init__(self, num):
self.num = num
def __add__(self, second_object):
return self.num + second_object.num
a1 = A(10)
print "-------a1 + a1-------", a1 + a1
print "-------a1 + 10-------", a1 + 40
OUTPUT
=======
-------a1 + a1------- 20 <--------
-------a1 + 10-------
Traceback (most recent call last):
File "sp.py", line 16, in
print "-------a1 + 10-------", a1 + 40
File "sp.py", line 10, in __add__
return self.num + second_object.num
AttributeError: 'int' object has no attribute 'num'
======================== 3
class a:
def __init__(self):
pass
a1 = a()
print "-------a1 + a1-------", a1 + a1
OUTPUT
========
test@saju-laptop:~/Desktop$ python sp.py
-------a1 + a1-------
Traceback (most recent call last):
File "sp.py", line 10, in
print "-------a1 + a1-------", a1 + a1
TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
======================== 4
class A:
def __init__(self):
pass
def __add__(self, second_item):
print "\n-------in------__add__-------"
print "\n-------first item-------", self
print "\n-------second item------", second_item
#Note: Here items first and second are same(same object 'a1' of class 'A')
a1 = A()
print "--------a1---------", a1
print "\n\***********\n"
print "-------a1 + a1-------", a1 + a1
print "\n\***********\n"
print "---------a1.__add__(a1)------------", a1.__add__(a1)
#Note: Here "a1 + a1" and "a1.__add__(a1)" are same.
OUTPUT
=======
--------a1--------- <__main__.a>
\***********
-------a1 + a1-------
-------in------__add__-------
-------first item------- <__main__.a>
-------second item------ <__main__.a>
None
\***********
---------a1.__add__(a1)------------
-------in------__add__-------
-------first item------- <__main__.a>
-------second item------ <__main__.a>
None
======================== 5
class A:
def __init__(self):
pass
def __add__(self, second_item):
print "\n-------in------__add__-------"
print "\n-------first item-------", self
print "\n-------second item------", second_item
#Note: Here items first 'a1' and second 'a2' are different
a1 = A()
a2 = A()
print "--------a1---------", a1
print "--------a2---------", a2
print "\n\***********\n"
print "-------a1 + a2-------", a1 + a2
print "\n\***********\n"
print "---------a1.__add__(a2)------------", a1.__add__(a2)
#Here "a1 + a2" and "a1.__add__(a2)" are same.
OUTPUT
=======
--------a1--------- <__main__.a>
--------a2--------- <__main__.a>
\***********
-------a1 + a2-------
-------in------__add__-------
-------first item------- <__main__.a>
-------second item------ <__main__.a>
None
\***********
---------a1.__add__(a2)------------
-------in------__add__-------
-------first item------- <__main__.a>
-------second item------ <__main__.a>
None
========================
Friday, January 21, 2011
python django defining __getitem__ on a class
python django defining __getitem__ on a class
#############################################
# defining __getitem__ on a class make it iterable in python?
# cb[0] is the same as cb.__getitem__(0)
#############################################1
class Test3:
Data = {'name':'saju', 'sex':'male'}
def __init__(self):
print "----in----__init__----"
def __getitem__(self, item1):
return self.Data[item1]
t3 = Test3()
print t3['name']
print t3['sex']
# OUTPUT
# ======
# ----in----__init__----
# saju
# male
#############################################2
class Test1:
def __init__(self):
print "----in----__init__----"
def __getitem__(self, item1):
print "-----in-----__getitem__-----", item1
if item1==5:
raise Exception('End')#For stop iteration (otherwise it will go to a loop)
t1 = Test1()
print "=============="
t1['saju'] #<----------IMP
t1.__getitem__('saju')
print "=============="
for x in t1:
pass
# OUTPUT
# ======
# ----in----__init__----
# ==============
# -----in-----__getitem__----- saju
# -----in-----__getitem__----- saju
# ==============
# -----in-----__getitem__----- 0
# -----in-----__getitem__----- 1
# -----in-----__getitem__----- 2
# -----in-----__getitem__----- 3
# -----in-----__getitem__----- 4
# -----in-----__getitem__----- 5
# Traceback (most recent call last):
# File "getitem.py", line 26, in
# for x in t1:
# File "getitem.py", line 15, in __getitem__
# raise Exception('End')#For stop iteration (otherwise it will go to a loop)
# Exception: End
#############################################3
class Test2:
def __init__(self):
print "----in----__init__----"
def __getitem__(self, item1, item2=0):
print "-----in-----__getitem__-----", item1, item2
if item1==5:
raise Exception('End')#For stop iteration (otherwise it will go to a loop)
t2 = Test2()
print "=============="
t2['saju'] #<----------IMP
t2.__getitem__('saju','1000')
print "=============="
for x in t2:
pass
# OUTPUT
# ======
# ----in----__init__----
# ==============
# -----in-----__getitem__----- saju 0
# -----in-----__getitem__----- saju 1000
# ==============
# -----in-----__getitem__----- 0 0
# -----in-----__getitem__----- 1 0
# -----in-----__getitem__----- 2 0
# -----in-----__getitem__----- 3 0
# -----in-----__getitem__----- 4 0
# -----in-----__getitem__----- 5 0
# Traceback (most recent call last):
# File "getitem.py", line 72, in
# for x in t2:
# File "getitem.py", line 60, in __getitem__
# raise Exception('End')#For stop iteration (otherwise it will go to a loop)
# Exception: End
#############################################
Thursday, January 6, 2011
Python calculate time taken to execute a function
Python calculate time taken to execute(run) a code or function or script.
>>> now = time.time()
>>> content1 = Myclass().content() <---- do some works
>>> time.time() - now
22.43316888809204 <-------time taken to do that works
>>> now = time.time()
>>> content1 = Myclass().content() <---- do some works
>>> time.time() - now
22.43316888809204 <-------time taken to do that works
Sunday, January 2, 2011
python if in operation with string
python "if in" operation with string
>>> name = 'smithjohnannaivan'
>>>
>>> if 'john' in name:
... print '---ok-----'
...
---ok-----
>>>
Subscribe to:
Posts (Atom)