Re: Save and load initialized class
Yes, all cool. But, json.dump(Opts.__dict__, open("mybuffer", "w")) still doesn't work with python3. Returns "is not JSON serializable" On Sun, Dec 10, 2017 at 4:00 AM, Steve D'Aprano wrote: > On Sun, 10 Dec 2017 04:52 am, MRAB wrote: > > > Try updating __dict__: > > > > Opts.__dict__.update(json.load(open("mybuffer"))) > > __dict__ is implementation, vars() is the public interface: > > vars(Opts).update(json.load(open("mybuffer"))) > > > Looks nicer too :-) > > > > > -- > Steve > “Cheer up,” they said, “things could be worse.” So I cheered up, and sure > enough, things got worse. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Listen to my FREE CD at http://www.mellowood.ca/music/cedars Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: b...@mellowood.ca WWW: http://www.mellowood.ca -- https://mail.python.org/mailman/listinfo/python-list
Re: Save and load initialized class
On Sun, 10 Dec 2017 04:52 am, MRAB wrote: > Try updating __dict__: > > Opts.__dict__.update(json.load(open("mybuffer"))) __dict__ is implementation, vars() is the public interface: vars(Opts).update(json.load(open("mybuffer"))) Looks nicer too :-) -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Save and load initialized class
On 2017-12-08 18:36, Bob van der Poel wrote: I'm trying to something simple (yeah, right). Mainly I want to have a bunch of variables which my program needs and uses to be in a saveable/loadable block. Currently I have then all as a bunch of globals which works, but trying to keep track of them and the correct spellings, etc becomes a bit of maintenance nightmare. So, demonstrating all my cleverness I came up with: class Opts(): var1 = 123 var2 = "hello world" Notationally, it looks much like the variables are stored in a separate module. I can call these options from within a function with the simple notation: if Opts.var1 == 99 ... And, if I need to change a value I can: global Opts.var1 = 0 Further, and importantly, I can save the lot with: json.dump(Opts.__dict__, open("mybuffer", "w")) But, I can't figure out how to load the saved data back into my program. Doing z=json.load (open("mybuffer", "r")) loads a dictionary ... which makes sense since that is what I saved. So, can I now reset the values in Opts from a saved dictionary? Try updating __dict__: Opts.__dict__.update(json.load(open("mybuffer"))) -- https://mail.python.org/mailman/listinfo/python-list
Re: Save and load initialized class
Bob van der Poel writes: > I'm trying to something simple (yeah, right). Mainly I want to have a bunch > of variables which my program needs and uses to be in a saveable/loadable > block. Currently I have then all as a bunch of globals which works, but > trying to keep track of them and the correct spellings, etc becomes a bit > of maintenance nightmare. So, demonstrating all my cleverness I came up > with: > > class Opts(): > var1 = 123 > var2 = "hello world" > > Further, and importantly, I can save the lot with: > > json.dump(Opts.__dict__, open("mybuffer", "w")) > > But, I can't figure out how to load the saved data back into my program. > Doing > > z=json.load (open("mybuffer", "r")) > > loads a dictionary ... which makes sense since that is what I saved. So, > can I now reset the values in Opts from a saved dictionary? You could use: for k, v in z.iteritems: setattr(Opts, k, v) Of course, this means that you already have a class "Opts" (maybe without or with different parameter values). This poses the question why you want to dump/load the class in the first place. Usually, you would want to have the values in code, not stored/loaded. If you really want make parameter values persistent, you could use the following approach: class ParameterCollection(object): def __init__(self, **kwargs): for k, v in kwargs.iteritems(): setattr(self, k, v) my_parameters = ParameterCollection(a=1, b=2, ) from pickle import load, dump dump(my_parameters, open(..., "wb"), -1) ... my_parameters_reloaded = load(open(..., "rb")) -- https://mail.python.org/mailman/listinfo/python-list
Save and load initialized class
I'm trying to something simple (yeah, right). Mainly I want to have a bunch of variables which my program needs and uses to be in a saveable/loadable block. Currently I have then all as a bunch of globals which works, but trying to keep track of them and the correct spellings, etc becomes a bit of maintenance nightmare. So, demonstrating all my cleverness I came up with: class Opts(): var1 = 123 var2 = "hello world" Notationally, it looks much like the variables are stored in a separate module. I can call these options from within a function with the simple notation: if Opts.var1 == 99 ... And, if I need to change a value I can: global Opts.var1 = 0 Further, and importantly, I can save the lot with: json.dump(Opts.__dict__, open("mybuffer", "w")) But, I can't figure out how to load the saved data back into my program. Doing z=json.load (open("mybuffer", "r")) loads a dictionary ... which makes sense since that is what I saved. So, can I now reset the values in Opts from a saved dictionary? Best, -- Listen to my FREE CD at http://www.mellowood.ca/music/cedars Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: b...@mellowood.ca WWW: http://www.mellowood.ca -- https://mail.python.org/mailman/listinfo/python-list