[Tutor] sensing EOF in Python 3.1

Cranky Frankie cranky.frankie at gmail.com
Fri Nov 25 13:59:34 CET 2011


From: Steven D'Aprano <steve at pearwood.info> wrote:

<<Pickle does standard file I/O in the same way that opening a JPEG in
an image view does standard file I/O: both programs read data from a
file the standard, ordinary way, but expect data of a certain format.
If you provide it too little data, you will get an EOF error. If you
provide too much data, or messed up data, then you will some other
error. But the file I/O is exactly the same. It's just that pickle, or
your image viewer, handle it for you.>>

Right, thanks.

<<Pickling won't tell you anything about Python data structures.
Pickling takes Python data structures, bashes them with a hammer until
they stop wiggling, then runs them through a serialiser turning them
into a stream of text or binary codes.>>

I want to include this info in my presentation because it shows how
data structures can be externalized.

<<Pickles tell you only about pickles. You won't learn anything about
(say) dicts by looking at a pickled dict except the bare fact that
dicts can be pickled.>>

Still though, that is important.

<<import pickle                            # import the pickle module

Really? Wow! I thought "import pickle" meant sort the database!!! *wink*

> Sarcasm aside, what else could "import pickle" mean other than import
> the pickle module?>>

I was trying to put a comment on every line because the audience who
will be looking at this will have never seen any Python before.

<<The comment adds absolutely nothing to the code. At best it is
superfluous. At worst it is harmful, because code and comments have a
regrettable tendency to get out of sync.>>

I know what you mean. The commens could be meaningful at first, then
the code gets edited over the years and the comments don't get
updated. I've seen that many times.

<<Every comment should carry its weight. If the code is particularly
convoluted, you might write comments explaining *how* you do
something, but generally the code speaks for itself regarding the how,
so comments should explain *why* you do something. If a comment
doesn't tell you something that the code doesn't, that you need to
know (or at least should know). Otherwise it should be throw out into
the back alley for the stray cats to eat.>>

Again, in this specific instance, this simple example code is intended
to be looked at not by Python programmers, or even programmers, but
rather by database professionals who I am attempting to expose Python
to for the first time.

<<pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled file
read_list = pickle.load(pickle_file)            # read the first pickled row

And this is what I'm talking about. It does NOT read the first pickled
row. Pickles don't have rows. In this case, you are reading the first
and only pickled object, which happens to be a dict.>>

You are of course, correct, thanks. I'll be fixing that.


Steven thanks for your comments about comments, I'll be doing them
over for sure.


-- 
Frank L. "Cranky Frankie" Palmeri


More information about the Tutor mailing list