0% found this document useful (0 votes)
83 views4 pages

10.7 Serialization

This document discusses serialization and pickling in Python. Serialization allows non-textual data like lists and class instances to be converted into a byte format that can be saved to file or transmitted over a network. This process is called pickling. The document provides an example of pickling a list to a file and then loading it back. It notes that cPickle provides a pre-compiled version of pickling for improved performance compared to the regular pickle module. Shelving is also discussed as similar to pickling but storing objects in a dictionary-like database accessible by key.

Uploaded by

manas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

10.7 Serialization

This document discusses serialization and pickling in Python. Serialization allows non-textual data like lists and class instances to be converted into a byte format that can be saved to file or transmitted over a network. This process is called pickling. The document provides an example of pickling a list to a file and then loading it back. It notes that cPickle provides a pre-compiled version of pickling for improved performance compared to the regular pickle module. Shelving is also discussed as similar to pickling but storing objects in a dictionary-like database accessible by key.

Uploaded by

manas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 10.

FILES 76

the pointer is actually in the location you think it is) or as a returned


value for a function.

10.7 Serialization
Serialization (pickling) allows you to save non-textual information to
memory or transmit it over a network. Pickling essentially takes any
data object, such as dictionaries, lists, or even class instances (which
we’ll cover later), and converts it into a byte set that can be used to
“reconstitute” the original data.

Listing 10.7: Pickling data


>>>import c P i c k l e #i m p o rt c P i c k l e l i b r a r y
>>>a _ l i s t = [ " one " , "two" , " b u c k l e " , "my" , " s h o e " ]
>>>s a v e _ f i l e = open ( " p i c k l e d _ l i s t " , "w" )
>>>c P i c k l e . dump( a _ l i s t , s a v e _ f i l e ) #s e r i a l i z e
l i s t to f i l e
>>> f i l e . c l o s e ( )
>>>o p e n _ f i l e = open ( " p i c k l e d _ l i s t " , " r " )
>>>b _ l i s t = c P i c k l e . l o a d ( o p e n _ f i l e )
There are two different pickle libraries for Python: cPickle and
pickle. In the above example, I used the cPickle library rather than
the pickle library. The reason is related to the information discussed in
Chapter 2. Since Python is interpreted, it runs a bit slower compared
to compiled languages, like C. Because of this, Python has a pre-
compiled version of pickle that was written in C; hence cPickle. Using
cPickle makes your program run faster.
Of course, with processor speeds getting faster all the time, you
probably won’t see a significant difference. However, it is there and
the use is the same as the normal pickle library, so you might as well
use it. (As an aside, anytime you need to increase the speed of your
program, you can write the bottleneck code in C and bind it into
Python. I won’t cover that in this book but you can learn more in the
official Python documentation.)
Shelves are similar to pickles except that they pickle objects to an
access-by-key database, much like dictionaries. Shelves allow you to
CHAPTER 10. FILES 77

simulate a random-access file or a database. It’s not a true database


but it often works well enough for development and testing purposes.

Listing 10.8: Shelving data


>>>import s h e l v e #i m p o rt s h e l v e l i b r a r y
>>>a _ l i s t = [ " one " , "two" , " b u c k l e " , "my" , " s h o e " ]
>>>dbase = s h e l v e . open ( " f i l e n a m e " )
>>>dbase [ "rhyme" ] = a _ l i s t #s a v e l i s t under key name
>>>b _ l i s t = dbase [ "rhyme" ] #r e t r i e v e l i s t
Chapter 11

Statements

Now that we know how Python uses it’s fundamental data types, let’s
talk about how to use them. Python is nominally a procedure-based
language but as we’ll see later, it also functions as an object-oriented
language. As a matter of fact, it’s similar to C++ in this aspect; you
can use it as either a procedural or OO language or combine them as
necessary.
The following is a listing of many Python statements. It’s not
all-inclusive but it gives you an idea of some of the features Python
has.

78
CHAPTER 11. STATEMENTS 79

Statement Role Examples


Assignment Creating references new_car = “Audi”
Calls Running functions stdout.write("eggs, ham,
toast\n")
Print Printing objects print “The Killer”, joke
Print() Python 3.x print function print(“Have you seen my
baseball?”)
If/elif/else Selecting actions if "python" in text: print
“yes”
For/else Sequence iteration for X in mylist: print X
While/else General loops while 1: print ’hello’
Pass Empty placeholder while 1: pass
Break, Continue Loop jumps while 1: if not line: break
Try/except/finally Catching exceptions try: action() except:
print ’action error’
Raise Trigger exception raise locationError
Import, From Module access import sys; from wx
import wizard
Def, Return Building functions def f(a, b, c=1, *d):
return a+b+c+d[0]
Class Building objects class subclass: staticData
= []

11.1 Assignment
I’ve already talked about assignment before. To reiterate, assignment
is basically putting the target name on the left of an equals sign and
the object you’re assigning to it on the right. There’s only a few things
you need to remember:
• Assignment creates object references.

– Assignment acts like pointers in C since it doesn’t copy ob-


jects, just refers to an object. Hence, you can have multiple
assignments of the same object, i.e. several different names
referring to one object.

• Names are created when first assigned

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy