[Tutor] sensing EOF in Python 3.1

2011-11-25 Thread Cranky Frankie
From: Steven D'Aprano  wrote:

<>

Right, thanks.

<>

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

<>

Still though, that is important.

< 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.

<>

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.

<>

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.

<>

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Steven D'Aprano

Cranky Frankie wrote:

From: Peter Otten <__pete...@web.de> wrote:
snip
<>

Thanks Peter, that was it. I was treating pickle like standard file
i/o when it's not that at all.


I don't understand what you mean by this.

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.




The reason why I'm "pickling" is I'm trying to include information on
Python data structures in the presentaton I'm preparing.


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.


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.


py> import pickle
py> d = {"key": "value", "another key": 42}
py> pickle.dumps(d)
"(dp0\nS'another key'\np1\nI42\nsS'key'\np2\nS'value'\np3\ns."

I don't know what you expect to learn about dicts from that.



Here are the two programs that now work correctly together:

import pickle
pickle_file = open("d:/Work/pickle_file", "wb")

Qb_dict = { ... contents of dict skipped ... }
pickle.dump(Qb_dict, pickle_file)
pickle_file.close()


Take note that you are pickling a dict. This is important later on.



#
# pickle_in.py
# program to read in a pickled file
#
# Frank L. Palmeri
#

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? 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.


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.



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.




print(read_list)  # print the input row from the pickled file
pickle_file.close()   # close the pickled file


Every comment in your second file is either unnecessary, or wrong. This 
is a good example of the need to "comment smart" rather than "comment 
hard".


# Bad:
i += 1  # Add one to i.

# Good:
i += 1  # Adjust i for one-based indexing.


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Steven D'Aprano

Cranky Frankie wrote:

I'm reading in a pickled file. The program works but I'm having
trouble sensing end of file. Here's the program:

[...]

Traceback (most recent call last):
  File "D:\MyDocs\Python\pickle_in.py", line 21, in 
read_file = pickle.load(pickle_file)# read the next record
in the input file
  File "D:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError


Seems to me that you have successfully found the end of file.

I'm not be facetious here. "Easier to ask forgiveness afterwards than 
permission before hand" is generally (but not necessarily always) the 
preferred way of coding things in Python. So instead of trying to 
predict the end of file ahead of time:


while some_hard_to_calculate_condition():
do_stuff_with_pickle()

you can catch the error instead:

try:
while True:  # Loop forever, until interrupted
do_stuff_with_pickle()
except EOFError:
# no more pickles, so we must be done
pass



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Wayne Werner
On Wed, Nov 23, 2011 at 12:03 PM, Cranky Frankie
wrote:

> [...]
> The reason why I'm "pickling" is I'm trying to include information on
> Python data structures in the presentaton I'm preparing. [...]
>

In your context why not just use modules?


 # data.py
Qb_dict = {"Montana": ["Joe", "Montana", "415-123-4567",
"joe.mont...@gmail.com","Candlestick Park"],
"Tarkington": ["Fran", "651-321-7657", "frank.tarking...@gmail.com",
"Metropolitan Stadidum"],
"Namath": ["Joe", "212-222-", "joe.nam...@gmail.com", "Shea Stadium"],
"Elway": ["John", "303-9876-333", "john.el...@gmai.com", "Mile High
Stadium"],
"Elway": ["Ed", "303-9876-333", "john.el...@gmai.com", "Mile High
Stadium"],
"Manning": ["Archie","504-888-1234", "archie.mann...@gmail.com",
"Louisiana Superdome"],
"Staubach": ["Roger","214-765-8989", "roger.staub...@gmail.com",
"Cowboy Stadium"]}

# program.py
from data import Qb_dict
print(Qb_dict)

Then you can put whatever comments or data you want. Just make sure that
you don't name your data.py file the same as something built-in. It's
pretty horrible when you try to 'import random' and it doesn't randint() or
anything else that you're looking for.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Cranky Frankie
From: Peter Otten <__pete...@web.de> wrote:
snip
<>

Thanks Peter, that was it. I was treating pickle like standard file
i/o when it's not that at all.

The reason why I'm "pickling" is I'm trying to include information on
Python data structures in the presentaton I'm preparing.

Here are the two programs that now work correctly together:

import pickle
pickle_file = open("d:/Work/pickle_file", "wb")

Qb_dict = {"Montana": ["Joe", "Montana", "415-123-4567",
"joe.mont...@gmail.com","Candlestick Park"],
"Tarkington": ["Fran", "651-321-7657", "frank.tarking...@gmail.com",
"Metropolitan Stadidum"],
"Namath": ["Joe", "212-222-", "joe.nam...@gmail.com", "Shea Stadium"],
"Elway": ["John", "303-9876-333", "john.el...@gmai.com", "Mile High Stadium"],
"Elway": ["Ed", "303-9876-333", "john.el...@gmai.com", "Mile High
Stadium"],
"Manning": ["Archie","504-888-1234", "archie.mann...@gmail.com",
"Louisiana Superdome"],
"Staubach": ["Roger","214-765-8989", "roger.staub...@gmail.com",
"Cowboy Stadium"]}

pickle.dump(Qb_dict, pickle_file)

pickle_file.close()



#
# pickle_in.py
# program to read in a pickled file
#
# Frank L. Palmeri
#

import pickle   # import the pickle
module
pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled file

read_list = pickle.load(pickle_file)# read the first pickled row

print(read_list)# print the input row from
the pickled file

pickle_file.close() # close the pickled file




-- 
Frank L. "Cranky Frankie" Palmeri
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Peter Otten
Cranky Frankie wrote:

> I'm reading in a pickled file. The program works but I'm having
> trouble sensing end of file. Here's the program:
> 
> #
> # pickle_in.py
> # program to read in a pickled file
> #
> # Frank L. Palmeri
> #
> 
> import pickle   # import the pickle module
> pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled input
> file
> 
> read_file = pickle.load(pickle_file)# read the first input
> record
> 
> new_list=[] # create a new empty list
> 
> while pickle_file:  # loop until end of file
> for i in range(0, 4):   # iterate for each field
> new_list.append(read_file[i])   # append each field to
> the new list
> i = i + 1   # increment loop counter
> print(new_list) # print the input
> record from the new list
> new_list=[] # initialize the new
> list for the next record
> read_file = pickle.load(pickle_file)# read the next record
> in the input file
> 
> 
> here's the error:
> 
> Traceback (most recent call last):
>   File "D:\MyDocs\Python\pickle_in.py", line 21, in 
> read_file = pickle.load(pickle_file)# read the next record
> in the input file
>   File "D:\Python31\lib\pickle.py", line 1365, in load
> encoding=encoding, errors=errors).load()
> EOFError

How did you write the data into the pickle file? The normal approach is to 
write all your data in one step, e. g. (all code snippets untested)

# write list
with open(filename, "wb") as out:
pickle.dump(list_of_records, out)

You can then read back all records with

# read list
with open(filename, "rb") as instream:
list_of_records = pickle.load(instream)

A second approach is also possible, but not appropriate for a newbie:

# write list entries
with open(filename, "wb") as out:
for record in list_of_records:
pickle.dump(record, out)

# read list entries to rebuild the list
list_of_records = []
with open(filename, "rb") as instream:
while True:
try: 
record = pickle.load(instream)
except EOFError:
break
else:
list_of_records.append(record)

By the way

> new_list=[]
> for i in range(0, 4):
> new_list.append(read_file[i])
> i = i + 1

- Here the for-loop is taking care of the value of i, you don't have to 
increment it manually. 

- A simpler way to achieve the same is

new_list = read_file[:4]


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sensing EOF in Python 3.1

2011-11-23 Thread Cranky Frankie
I'm reading in a pickled file. The program works but I'm having
trouble sensing end of file. Here's the program:

#
# pickle_in.py
# program to read in a pickled file
#
# Frank L. Palmeri
#

import pickle   # import the pickle module
pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled input file

read_file = pickle.load(pickle_file)# read the first input record

new_list=[] # create a new empty list

while pickle_file:  # loop until end of file
for i in range(0, 4):   # iterate for each field
new_list.append(read_file[i])   # append each field to
the new list
i = i + 1   # increment loop counter
print(new_list) # print the input
record from the new list
new_list=[] # initialize the new
list for the next record
read_file = pickle.load(pickle_file)# read the next record
in the input file


here's the error:

Traceback (most recent call last):
  File "D:\MyDocs\Python\pickle_in.py", line 21, in 
read_file = pickle.load(pickle_file)# read the next record
in the input file
  File "D:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError







-- 
Frank L. "Cranky Frankie" Palmeri
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor