User-Defined Persistent Object

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.

···

On Thu, Jan 12, 2017 at 11:58 AM, Nick D. <nikos7am@gmail.com> 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

--
Best Regards,
Michael Moriarity

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)

···

On Jan 12, 2017, at 11:58 AM, Nick D. nikos7am@gmail.com 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 wxpython-users+unsubscribe@googlegroups.com.

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.

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

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

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:

https://jsonpickle.github.io/

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

···

On Fri, Jan 13, 2017 at 12:32 AM, Nick D. <nikos7am@gmail.com> wrote:

--

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

Chris.Barker@noaa.gov

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.

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.