how do you use pickle?

J

John Salerno

Here's what I have:

import pickle

data = open(r'C:\pickle_data.txt', 'w')
image = open(r'C:\peakhell.jpg')
pickle.Pickler(data)
data.dump(image)
data.close()
image.close()

First off, I'm not sure the second line is the way to do that. But my
main problem is I don't know how to define the steps when pickling an
object. How do you first create the object? Does it return a value? I tried:

pickler = pickle.Pickler(data)
pickle.Pickler(data)

Then I tried:

pickler.dump(image)
data.dump(image)

Basically, I'm just confused about all the syntax involved. Which
objects do I operate on, and when? I can't find these answers in the
documentation or in the interactive help, because I don't see any examples.

Furthermore, I get this error every time:

Traceback (most recent call last):
File "C:\Documents and Settings\jsaler01.TUFTS\My
Documents\Python24\myscripts\challenges\pickle.py", line 3, in -toplevel-
import pickle
File "C:\Documents and Settings\jsaler01.TUFTS\My
Documents\Python24\myscripts\challenges\pickle.py", line 7, in -toplevel-
pickle.Pickler(data)
AttributeError: 'module' object has no attribute 'Pickler'

So that makes me really confused, because I must have the syntax totally
wrong if it can't even get past the initial creation of a pickler object.
 
F

Fredrik Lundh

John said:
Here's what I have:

import pickle

data = open(r'C:\pickle_data.txt', 'w')
image = open(r'C:\peakhell.jpg')
pickle.Pickler(data)
data.dump(image)
data.close()
image.close()

First off, I'm not sure the second line is the way to do that. But my
main problem is I don't know how to define the steps when pickling an
object. How do you first create the object? Does it return a value? I tried:

pickler = pickle.Pickler(data)
pickle.Pickler(data)

Then I tried:

pickler.dump(image)
data.dump(image)

Basically, I'm just confused about all the syntax involved. Which
objects do I operate on

any object that supports pickling.

file handles does not belong to that group (what did you expect
pickle to do with a file handle ?)

or did "object" refer to the Pickler instance? you don't really need to
bother with that; just use the dump *function* instead:

pickle.dump(object, file)

but if you insist on using your own Pickler instance, you have to save
the pickler instance in a variable, and call the dump method on that in-
stance:

myfile = open(somefilename, "wb")
mypickler = pickle.Pickler(myfile)
mypickler.dump(object)

</F>
 
J

John Salerno

Fredrik said:
file handles does not belong to that group (what did you expect
pickle to do with a file handle ?)

or did "object" refer to the Pickler instance? you don't really need to
bother with that; just use the dump *function* instead:

pickle.dump(object, file)

but if you insist on using your own Pickler instance, you have to save
the pickler instance in a variable, and call the dump method on that in-
stance:

myfile = open(somefilename, "wb")
mypickler = pickle.Pickler(myfile)
mypickler.dump(object)

I'm sorry, but I'm terribly confused. Nothing seems to be working for
me. I *think* what I need to pickle is an image file, but so far I think
all I'm doing is passing the file handle in as the 'object', which
probably isn't correct. How would I get the actual image object to pass
in? But I also tried pickling a string and that didn't work either.

I tried it both ways you suggest, but I can't get past the errors. I
keep getting either

AttributeError: 'module' object has no attribute 'dump' (when I use the
function)

or

AttributeError: 'module' object has no attribute 'dump' (when I create
an instance)

Is there something special I need to do to use the pickle module? My
program seems to recognize the import, but it keeps saying that nothing
is an attribute of it.
 
J

John Salerno

John said:
AttributeError: 'module' object has no attribute 'dump' (when I create
an instance)

That should be:

AttributeError: 'module' object has no attribute 'Pickler' (when I
create an instance)
 
F

Fredrik Lundh

John said:
I'm sorry, but I'm terribly confused. Nothing seems to be working for
me. I *think* what I need to pickle is an image file, but so far I think
all I'm doing is passing the file handle in as the 'object'

define "image file". the data in the file, the file name, or some other
representation?
I tried it both ways you suggest, but I can't get past the errors. I
keep getting either

AttributeError: 'module' object has no attribute 'dump' (when I use the
function)

did you, by any chance, name your test script "pickle.py" ?

</F>
 
P

Pythor

John said:
I'm sorry, but I'm terribly confused. Nothing seems to be working for
me. I *think* what I need to pickle is an image file,

SNIP

Hint: Read the source for that page more carefully.
 
J

John Salerno

Fredrik said:
define "image file". the data in the file, the file name, or some other
representation?


did you, by any chance, name your test script "pickle.py" ?

DOH!
 
J

John Salerno

Fredrik said:
define "image file". the data in the file, the file name, or some other
representation?

I was being dense again. I was just opening the file, but not reading
it! After reading it, it's easy to pickle it.
did you, by any chance, name your test script "pickle.py" ?

That did the trick! Thanks!
 
J

John Salerno

Pythor said:
SNIP

Hint: Read the source for that page more carefully.

Hmmm...the source doesn't say much. Is there more to do with the source
beyond using it to figure out what 'peak hell' means?
 
P

Pythor

John said:
Hmmm...the source doesn't say much. Is there more to do with the source
beyond using it to figure out what 'peak hell' means?

Yes... It mentions a whole different file, which you need to use pickle
on.
 
J

John Salerno

Pythor said:
Yes... It mentions a whole different file, which you need to use pickle
on.

Yikes, I even took a second look at that after you said re-read the
source and then I ignored it! Thanks!
 
P

Pythor

John said:
Yikes, I even took a second look at that after you said re-read the
source and then I ignored it! Thanks!

Whis is why I said carefully ;) I missed it several times myself when
I was working on the challenge.
 
J

John Salerno

Pythor said:
Whis is why I said carefully ;) I missed it several times myself when
I was working on the challenge.

Ok, frustration has set in again. I see the file name in the source
code, but am I meant to actually access a file, or just use the name
itself? I also had to look up what 'banner' meant in Unix, which wasn't
a very fun thing to have to do for a Python puzzle.
 
J

John Salerno

John said:
Ok, frustration has set in again. I see the file name in the source
code, but am I meant to actually access a file, or just use the name
itself? I also had to look up what 'banner' meant in Unix, which wasn't
a very fun thing to have to do for a Python puzzle.

Ah, I should have known we weren't done with URL manipulation yet. Now I
just need to figure out what to do with this source code.
 
J

John Salerno

Pythor said:
Incidentally, there's a forum specifically for posting an receiving
hints on the challenges here:

http://www.pythonchallenge.com/forums/viewforum.php?f=1

Not that I mind helping. ;)

Yeah, I read the whole thread for this level and even posted a question,
but the forums are pretty dead. Not only that, but I'm starting to get
the feeling that these challenges are going to require a little more
programming expertise than I have, because all of the 'hints' are still
too vague for me.

Like I said in my post on the hints forum, I really enjoy figuring out
how to use Python to solve the puzzles, but it's not very fun when I
don't know *what* to do in the first place, and I think knowing what to
do has a lot to do with how experienced you are with certain CS subjects
and how quickly you can identify the problem and a possible solution.
All I see is huge bunch of string characters with no idea what to do
with it to extract any kind of information.
 
P

Pythor

John said:
Yeah, I read the whole thread for this level and even posted a question,
but the forums are pretty dead. Not only that, but I'm starting to get
the feeling that these challenges are going to require a little more
programming expertise than I have, because all of the 'hints' are still
too vague for me.

Like I said in my post on the hints forum, I really enjoy figuring out
how to use Python to solve the puzzles, but it's not very fun when I
don't know *what* to do in the first place, and I think knowing what to
do has a lot to do with how experienced you are with certain CS subjects
and how quickly you can identify the problem and a possible solution.
All I see is huge bunch of string characters with no idea what to do
with it to extract any kind of information.

Only a bunch of string characters? If that's true, you missed
something. I get a bunch of lists of tuples. Each tuple has a meaning
associated with the original filename, and an old unix command. Which
is what confused the heck out of me the first time, not knowing much
Unix. The command is used to make large signs using old-style dot
matrix printers. If you want a more coherent hint than that, email
me at pythor @ pythor dot com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,518
Members
48,225
Latest member
JasminOrta

Latest Threads

Top