Custom Search

Sunday, March 27, 2011

introduction python pickle load dump

introduction python pickle load dump

* for serializing and de-serializing a Python object structure.
* Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” [1] or “flattening”.
* The pickle module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The module shelve provides a simple interface to pickle and unpickle objects on DBM-style database files.
* non-Python programs may not be able to reconstruct pickled Python objects.
* http://docs.python.org/library/pickle.html


*******Dumping*******

class A:
g1=100

def __init__(self):
self.name='saju'

def f1(self):
print "----f1--"

import pickle
a1 = A()
pickle.dump(a1, file('pickle_file_1','w'))
print "\n------vars(A)------", vars(A)
print "\n------vars(a1)------", vars(a1)

OUTPUT
======
------vars(A)------ {'f1': ,
'__module__': '__main__',
'__init__': ,
'g1': 100,
'__doc__': None}

------vars(a1)------ {'name': 'saju'}

*******Loading-1*******

class A:
g1=100

def __init__(self):
self.name='saju'

def f1(self):
print "----in f1--"

import pickle
c1 = pickle.load(file('pickle_file_1'))
print "\n------vars(A)------", vars(A)
print "\n------vars(c1)------", vars(c1)
print "\n------c1.name------", c1.name
print "\n------ c1.g1------", c1.g1
print "\n------c1.f1------", c1.f1()

OUTPUT
======
------vars(A)------ {'f1': ,
'__module__': '__main__',
'__init__': ,
'g1': 100,
'__doc__': None}

------vars(c1)------ {'name': 'saju'}

------c1.name------ saju

------ c1.g1------ 100

------c1.f1------ ----in f1--

*******Loading-2*******

#Made some changes in class 'A'.
#Added class variable 'g2' , method 'f2()' and instance variable 'age'.
#We can access new class variable 'g2' , method 'f2()' using loaded object.
#But we can not access instance variable 'age' using loaded object.
# that means pickle only dump __dict__ attribute of object "c1".

class A:
g1=100
g2=200
def __init__(self):
self.age=23
self.name='saju'

def f1(self):
print "----in f1--"

def f2(self):
print "----in f2--"

import pickle
c1 = pickle.load(file('pickle_file_1'))
print "\n------vars(A)------", vars(A)
print "\n------vars(c1)------", vars(c1)
print "\n------c1.name------", c1.name
print "\n------ c1.g1------", c1.g1
print "\n------c1.f1------", c1.f1()
print "\n------ c1.g2------", c1.g2
print "\n------c1.f2------", c1.f2()
print "\n------c1.age------", c1.age

OUTPUT
=====
------vars(A)------ {'f1': ,
'__module__': '__main__',
'g2': 200,
'g1': 100,
'f2': ,
'__doc__': None,
'__init__': }

------vars(c1)------ {'name': 'saju'}

------c1.name------ saju

------ c1.g1------ 100

------c1.f1------ ----in f1--

------ c1.g2------ 200

------c1.f2------ ----in f2--

------c1.age------
Traceback (most recent call last):
File "pickle1.py", line 39, in
print "\n------c1.age------", c1.age
AttributeError: A instance has no attribute 'age'


=========================
python , __dict__ attribute of class and object

More
More

No comments:

Post a Comment