how do you use pickle?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    how do you use pickle?

    Here's what I have:

    import pickle

    data = open(r'C:\pickl e_data.txt', 'w')
    image = open(r'C:\peakh ell.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(im age)
    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:\Documen ts and Settings\jsaler 01.TUFTS\My
    Documents\Pytho n24\myscripts\c hallenges\pickl e.py", line 3, in -toplevel-
    import pickle
    File "C:\Documen ts and Settings\jsaler 01.TUFTS\My
    Documents\Pytho n24\myscripts\c hallenges\pickl e.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.
  • Fredrik Lundh

    #2
    Re: how do you use pickle?

    John Salerno wrote:
    [color=blue]
    > Here's what I have:
    >
    > import pickle
    >
    > data = open(r'C:\pickl e_data.txt', 'w')
    > image = open(r'C:\peakh ell.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(im age)
    > data.dump(image )
    >
    > Basically, I'm just confused about all the syntax involved. Which
    > objects do I operate on[/color]

    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(obj ect, 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(somefilena me, "wb")
    mypickler = pickle.Pickler( myfile)
    mypickler.dump( object)

    </F>



    Comment

    • John Salerno

      #3
      Re: how do you use pickle?

      Fredrik Lundh wrote:
      [color=blue]
      > 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(obj ect, 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(somefilena me, "wb")
      > mypickler = pickle.Pickler( myfile)
      > mypickler.dump( object)[/color]

      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.

      Comment

      • John Salerno

        #4
        Re: how do you use pickle?

        John Salerno wrote:
        [color=blue]
        > AttributeError: 'module' object has no attribute 'dump' (when I create
        > an instance)[/color]

        That should be:

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

        Comment

        • Pythor

          #5
          Re: how do you use pickle?


          John Salerno wrote:
          [color=blue]
          > 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,[/color]

          SNIP

          Hint: Read the source for that page more carefully.

          Comment

          • Fredrik Lundh

            #6
            Re: how do you use pickle?

            John Salerno wrote:
            [color=blue][color=green]
            > > 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(somefilena me, "wb")
            > > mypickler = pickle.Pickler( myfile)
            > > mypickler.dump( object)[/color]
            >
            > 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'[/color]

            define "image file". the data in the file, the file name, or some other
            representation?
            [color=blue]
            > 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)[/color]

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

            </F>



            Comment

            • John Salerno

              #7
              Re: how do you use pickle?

              Fredrik Lundh wrote:[color=blue]
              > John Salerno wrote:
              >[color=green][color=darkred]
              >>> 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(somefilena me, "wb")
              >>> mypickler = pickle.Pickler( myfile)
              >>> mypickler.dump( object)[/color]
              >> 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'[/color]
              >
              > define "image file". the data in the file, the file name, or some other
              > representation?
              >[color=green]
              >> 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)[/color]
              >
              > did you, by any chance, name your test script "pickle.py" ?[/color]

              DOH!

              Comment

              • John Salerno

                #8
                Re: how do you use pickle?

                Fredrik Lundh wrote:
                [color=blue]
                > define "image file". the data in the file, the file name, or some other
                > representation?[/color]

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

                That did the trick! Thanks!

                Comment

                • John Salerno

                  #9
                  Re: how do you use pickle?

                  Pythor wrote:[color=blue]
                  > John Salerno wrote:
                  >[color=green]
                  >> 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,[/color]
                  >
                  > SNIP
                  >
                  > Hint: Read the source for that page more carefully.
                  >[/color]

                  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?

                  Comment

                  • Pythor

                    #10
                    Re: how do you use pickle?


                    John Salerno wrote:[color=blue]
                    > Pythor wrote:[color=green]
                    > > John Salerno wrote:
                    > >[color=darkred]
                    > >> 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,[/color]
                    > >
                    > > SNIP
                    > >
                    > > Hint: Read the source for that page more carefully.
                    > >[/color]
                    >
                    > 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?[/color]

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

                    Comment

                    • John Salerno

                      #11
                      Re: how do you use pickle?

                      Pythor wrote:[color=blue]
                      > John Salerno wrote:[color=green]
                      >> Pythor wrote:[color=darkred]
                      >>> John Salerno wrote:
                      >>>
                      >>>> 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.
                      >>>[/color]
                      >> 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?[/color]
                      >
                      > Yes... It mentions a whole different file, which you need to use pickle
                      > on.
                      >[/color]

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

                      Comment

                      • Pythor

                        #12
                        Re: how do you use pickle?


                        John Salerno wrote:[color=blue]
                        > Pythor wrote:[color=green]
                        > > John Salerno wrote:[color=darkred]
                        > >> Pythor wrote:
                        > >>> John Salerno wrote:
                        > >>>
                        > >>>> 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.
                        > >>>
                        > >> 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?[/color]
                        > >
                        > > Yes... It mentions a whole different file, which you need to use pickle
                        > > on.
                        > >[/color]
                        >
                        > Yikes, I even took a second look at that after you said re-read the
                        > source and then I ignored it! Thanks![/color]

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

                        Comment

                        • John Salerno

                          #13
                          Re: how do you use pickle?

                          Pythor wrote:
                          [color=blue]
                          > Whis is why I said carefully ;) I missed it several times myself when
                          > I was working on the challenge.[/color]

                          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.

                          Comment

                          • John Salerno

                            #14
                            Re: how do you use pickle?

                            John Salerno wrote:[color=blue]
                            > Pythor wrote:
                            >[color=green]
                            >> Whis is why I said carefully ;) I missed it several times myself when
                            >> I was working on the challenge.[/color]
                            >
                            > 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.[/color]

                            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.

                            Comment

                            • Pythor

                              #15
                              Re: how do you use pickle?


                              John Salerno wrote:[color=blue]
                              > John Salerno wrote:[color=green]
                              > > Pythor wrote:
                              > >[color=darkred]
                              > >> Whis is why I said carefully ;) I missed it several times myself when
                              > >> I was working on the challenge.[/color]
                              > >
                              > > 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.[/color]
                              >
                              > 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.[/color]

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



                              Not that I mind helping. ;)

                              Comment

                              Working...