User-Defined Persistent Object

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

User-Defined Persistent Object

Nick D.
Hello to all!
I have a simple class that holds some variables that desribe various states of the application and I would like to save it to be restored when restarting the application.
This class is not wx.Window derived ( it is not a GUI class ) it justs holds part of the "logic" of my application.
I have not found so far an example on how to implement a persistent non-window user defined object.
Does anyone have any idea or an example on how to do that?

Thanks
Nick

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Michael Moriarity
On Thu, Jan 12, 2017 at 11:58 AM, Nick D. <[hidden email]> wrote:
Hello to all!
I have a simple class that holds some variables that desribe various states of the application and I would like to save it to be restored when restarting the application.
This class is not wx.Window derived ( it is not a GUI class ) it justs holds part of the "logic" of my application.
I have not found so far an example on how to implement a persistent non-window user defined object.
Does anyone have any idea or an example on how to do that?

Thanks
Nick


You can use pickle.dump() to save the object to a file, and pickle.load() to restore it.

--
Best Regards,
Michael Moriarity

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Rufus V. Smith
In reply to this post by Nick D.

On Jan 12, 2017, at 11:58 AM, Nick D. <[hidden email]> wrote:

Hello to all!
I have a simple class that holds some variables that desribe various states of the application and I would like to save it to be restored when restarting the application.
This class is not wx.Window derived ( it is not a GUI class ) it justs holds part of the "logic" of my application.
I have not found so far an example on how to implement a persistent non-window user defined object.
Does anyone have any idea or an example on how to do that?

Thanks
Nick

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

If it is a POD (plain old data) class, you should be able to covert it to a dictionary and
simply use the pickle module or json module.

You might consider adding a load and dump method to the class itself which would just
take the stored file name and do the required conversions, including creating the default
values when the file is not found (first run, for example)



--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Chris Barker - NOAA Federal

If it is a POD (plain old data) class, you should be able to covert it to a dictionary and
simply use the pickle module or json module.

No need to convert to. Dict for pickle . But pickle has security issues if you can't completely trust the source.

If you want to the JSON route, if it's really a POD class, you maybe be able simply save the instances __dict__ attribute.


-CHB



You might consider adding a load and dump method to the class itself which would just
take the stored file name and do the required conversions, including creating the default
values when the file is not found (first run, for example)



--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Nick D.
In reply to this post by Nick D.
I would like to save it some readable format so pickle i wouldn't like to use.
I am thinking of using YAML or TOML both seem more comprehendable than XML or JSON,
However, I would still wanted to experiment with user-defined persistence objects.
I have already tried it for saving widgets and it is really small, easy, and comprehendable and could save
me a lot of work.

Nick

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Chris Barker - NOAA Federal
On Fri, Jan 13, 2017 at 12:32 AM, Nick D. <[hidden email]> wrote:
I would like to save it some readable format so pickle i wouldn't like to use.
I am thinking of using YAML or TOML both seem more comprehendable than XML or JSON,

I'd probably go with JSON -- it mapps more directly to pyton objects (i.e. dicts), so very little code to write to use for persistence.

Also -- if you don't need to interface with non-python apps -- you could use what I cal PySON -- Python literals.

you can read them with ast.literal_eval, which is pretty secure (i.e. only reads literals, not arbitrary Python code)

Also, there is a JSON-pickle:


Which gives you all (Most) of the easy of use of pickle, but a JSON format. (and the security risks of pickle...)

 
However, I would still wanted to experiment with user-defined persistence objects.

You can write a generic persistence system really easily that supports simple classes quite easily:

Simply write the __dict__ and the __class__ to your file (as JSON, or TOML, or...) and then you can re-construct an instance with the __class__, and re-populate it with the contents of __class__.

"simple" classes in this case means that all attributes are regular build-tin python objects (the ones with literals).

It could, of course be extended to do nested objects of custom types, but then you're getting into more work...

-CHB


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Nick D.
In reply to this post by Nick D.

Thanks for the post and tips Chris.
I have another question as well.
After I create persistent objects with the persistence manager and I happen to have many instances of my application open and I close them simultaneously
I get a lot of .tmp configuration files which are automatically created by the persistence manager.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: User-Defined Persistent Object

Chris Barker - NOAA Federal


I have another question as well.
After I create persistent objects with the persistence manager and I happen to have many instances of my application open and I close them simultaneously
I get a lot of .tmp configuration files which are automatically created by the persistence manager.

I would either:

Not do that -- do you need the tmp files? Or could you do whatever they do in memory instead?

Or

Put the .tmp files in a proper temp dir. python has a standard library for creating temp files in a system specific way, called tempfile, reasonably enough.

CHB


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.