Python-Ref > Object oriented programming > Introspection > Serialization using pickling
 
 

<-^^

Serialization using pickling

Expand/Shrink
  1   import xml.dom.minidom as dom
  2   
  3   class Shape:
  4   
  5     def __init__( self, color=None):
  6       self.color = color
  7   
  8     def get_area( self):
  9       return 0
 10   
 11   
 12   class Rectangle( Shape):
 13   
 14     def __init__( self, color=None, width=None, height=None):
 15       Shape.__init__( self, color=color)
 16       self.width = width
 17       self.height = height
 18   
 19     def get_area( self):
 20       return self.width * self.height
 21   
 22   
 23   class Circle( Shape):
 24   
 25     def __init__( self, color=None, radius=None):
 26       Shape.__init__( self, color=color)
 27       self.radius = radius
 28   
 29     def get_area( self):
 30       return 3.1415 * self.radius**2
 31   
 32   
 33   class Text:
 34   
 35     def __init__( self, color=None, text=None):
 36       self.color = color
 37       self.text = text
 38   
 39   # --------------------------------------------------
 40   
 41   o1 = Rectangle( "black", 2, 3)
 42   o2 = Rectangle( "yellow", 3, 5)
 43   o3 = Circle( "white", 4)
 44   o4 = Text( "green", "Hello world!")
 45   
 46   import pickle
 47   
 48   f = file( "dump.bin", "w")
 49   pickle.dump( [o1,o2,o3,o4], f)
 50   f.close()
 51   
 52   f = file( "dump.bin", "r")
 53   objs = pickle.load( f)
 54   f.close()
 55   
 56   for o in objs:
 57     print o, o.__dict__
stdout:
<__main__.Rectangle instance at 0x2af5be679440> {'color': 'black', 'width': 2, 'height': 3}
<__main__.Rectangle instance at 0x2af5be67ab00> {'color': 'yellow', 'width': 3, 'height': 5}
<__main__.Circle instance at 0x2af5be67a680> {'color': 'white', 'radius': 4}
<__main__.Text instance at 0x2af5be67a098> {'color': 'green', 'text': 'Hello world!'}
(lp0
(i__main__
Rectangle
p1
(dp2
S'color'
p3
S'black'
p4
sS'width'
p5
I2
sS'height'
p6
I3
sba(i__main__
Rectangle
p7
(dp8
g3
S'yellow'
p9
sg5
I3
sg6
I5
sba(i__main__
Circle
p10
(dp11
g3
S'white'
p12
sS'radius'
p13
I4
sba(i__main__
Text
p14
(dp15
g3
S'green'
p16
sS'text'
p17
S'Hello world!'
p18
sba.
Doba běhu: 52.1 ms
Expand/Shrink
  1   import xml.dom.minidom as dom
  2   
  3   class Shape:
  4   
  5     def __init__( self, color=None):
  6       self.color = color
  7   
  8     def get_area( self):
  9       return 0
 10   
 11   
 12   class Rectangle( Shape):
 13   
 14     def __init__( self, color=None, width=None, height=None):
 15       Shape.__init__( self, color=color)
 16       self.width = width
 17       self.height = height
 18   
 19     def get_area( self):
 20       return self.width * self.height
 21   
 22   
 23   class Circle( Shape):
 24   
 25     def __init__( self, color=None, radius=None):
 26       Shape.__init__( self, color=color)
 27       self.radius = radius
 28   
 29     def get_area( self):
 30       return 3.1415 * self.radius**2
 31   
 32   
 33   class Text:
 34   
 35     def __init__( self, color=None, text=None):
 36       self.color = color
 37       self.text = text
 38   
 39   # --------------------------------------------------
 40   
 41   o1 = Rectangle( "black", 2, 3)
 42   o2 = Rectangle( "yellow", 3, 5)
 43   o3 = Circle( "white", 4)
 44   o4 = Text( "green", "Hello world!")
 45   
 46   import pickle
 47   
 48   serials = []
 49   for o in [o1,o2,o3,o4]:
 50     serials.append( pickle.dumps( o))
 51   
 52   for serial in serials:
 53     o = pickle.loads( serial)
 54     print o, o.__dict__
stdout:
<__main__.Rectangle instance at 0x2accea08e440> {'color': 'black', 'width': 2, 'height': 3}
<__main__.Rectangle instance at 0x2accea08fb00> {'color': 'yellow', 'width': 3, 'height': 5}
<__main__.Circle instance at 0x2accea08d098> {'color': 'white', 'radius': 4}
<__main__.Text instance at 0x2accea08fb00> {'color': 'green', 'text': 'Hello world!'}
Doba běhu: 51.3 ms