[Tutor] Saving Objects

2008-01-17 Thread Andy Cheesman
Hi people

I've written a complex program in python/numpy/scipy which creates a 
dictionary of objects(which takes a while to create). I am hoping to 
save these objects to disk and then access them using a different 
session, thus bypassing the time to create the objects.

My initial tests using pickle and a simple class system (shown below) 
have failed. The method shown below fails with a AttributeError: 
'FakeModule' object has no attribute 'Spod', so when I create a
an empty class Spod in the new session, it generates an IndexError:(list 
index out of range)

Is there a better way to do this?

Andy

class Spod:
 def __init__(self, name):
 self.name=name

fish = Spod("andy")
file = open("test.pickle","w")
pickle.dump(fish, file)

# New session
file = open("test.pickle","r")
pickle.load(file)





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Saving Objects

2008-01-17 Thread Paul McGuire
>>>
My initial tests using pickle and a simple class system (shown below) have
failed. The method shown below fails with a AttributeError: 
'FakeModule' object has no attribute 'Spod', so when I create a an empty
class Spod in the new session, it generates an IndexError:(list index out of
range)

Is there a better way to do this?
>>>

I'm assuming that "# New session" marks the beginning of a separate Python
file.  The problem is that the Spod class definition is not included in the
pickle file, so you must import that as well, before loading back the
pickled fish object.  Do this;

1. Put Spod in its own module, let's call it spod.py.  (While you are at it,
have Spod derive from object, so that you get a new-style class.)
2. Create test1.py to pickle a Spod.  Have test1.py import spod, and then
have your code that creates spod.Spod("andy") and pickles it to test.pickle.
3. Create test2.py to unpickle a Spod.  Have test2.py *also* import spod,
and then have the rest of your code that follows "# New session".  Modify
the pickle.load statement to save the result to a variable, and then you can
verify that its name is "andy", type is "Spod", etc.

-- Paul


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Saving Objects

2008-01-17 Thread Andy Cheesman
That's great and it works too, thank you!

My only arising question is why should i
"derive from object, so that you get a new-style class."

I've looked online and found this 
(http://www.geocities.com/foetsch/python/new_style_classes.htm) but I 
not sure it tells why I would want to do this this way.

Thanks again for your help

Andy

Paul McGuire wrote:
> My initial tests using pickle and a simple class system (shown below) have
> failed. The method shown below fails with a AttributeError: 
> 'FakeModule' object has no attribute 'Spod', so when I create a an empty
> class Spod in the new session, it generates an IndexError:(list index out of
> range)
>
> Is there a better way to do this?
>   
>
> I'm assuming that "# New session" marks the beginning of a separate Python
> file.  The problem is that the Spod class definition is not included in the
> pickle file, so you must import that as well, before loading back the
> pickled fish object.  Do this;
>
> 1. Put Spod in its own module, let's call it spod.py.  (While you are at it,
> have Spod derive from object, so that you get a new-style class.)
> 2. Create test1.py to pickle a Spod.  Have test1.py import spod, and then
> have your code that creates spod.Spod("andy") and pickles it to test.pickle.
> 3. Create test2.py to unpickle a Spod.  Have test2.py *also* import spod,
> and then have the rest of your code that follows "# New session".  Modify
> the pickle.load statement to save the result to a variable, and then you can
> verify that its name is "andy", type is "Spod", etc.
>
> -- Paul
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Saving Objects

2008-01-17 Thread Marc Tompkins
On Jan 17, 2008 6:15 AM, Andy Cheesman <[EMAIL PROTECTED]> wrote:

> My only arising question is why should i
> "derive from object, so that you get a new-style class."
>

This list discussed this topic a couple of weeks ago.  Recklessly
simplifying, it boils down to this: By using new-style classes, your objects
inherit a number of pre-defined methods and properties which you would
otherwise have to write yourself.  Ceteris paribus, a new-style class takes
up less memory than the equivalent old-style class (there was some wild
surmise about why that should be, but it's not important now.)  Finally, it
appears that Python 3000 will require new-style classes, so you might as
well get used to them now.

For your program, the only difference is that instead of defining a class
like this:

class Thingy():
pass

you would write this:

class Thingy(object):
pass

Not much downside, I'd say.


-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread bill.wu


the tutor list has been strangely silent for a few days.  Anyone know 
what has happened? why? 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread Luke Paireepinart
bill.wu wrote:
> the tutor list has been strangely silent for a few days. Anyone know
> what has happened? why?
I got about 20 e-mails from the list yesterday.
Do you consider this slient?
or do you maybe have a problem receiving messages?
> 
> /*DreamMail*/ - 第一个支持邮件来源跟踪的电子邮件客户端
> www.dreammail.org 
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] the tutor list has been strangely silent for a few days. Anyone know

2008-01-17 Thread Dick Moores
At 06:41 PM 1/17/2008, bill.wu wrote:

>the tutor list has been strangely silent for a few days.  Anyone know
>what has happened? why?

FYI I see 34 messages in my Eudora Tutor mailbox, dated 1/16 Pacific
Time (Eudora converts the datetimes to my time zone, PT).

Here's a screenshot of that mailbox:


If you're missing some, you can read them here:


Dick Moores
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor