Reputation: 38949
I have a class that I'll be instantiating a lot from lines of JSON. Something like:
class Something:
def __init__(self, json):
#load all self variables from simplejson.loads(json) here
print self.some_variable_loaded_from_json
I'd like this to be as efficient as possible because this class is loaded hundreds of times a second. I know I can do a for loop with key/value pairs in the dictionary generated from simplejson, but if there's a way to have simplejson just load directly to class variables without that O(n) overhead, that would be awesome.
Upvotes: 2
Views: 6490
Reputation: 2557
You're going to have to either load the data once and keep it cached, or loop over the entire JSON blob on every instantiation of Something
. Unfortunately, SimpleJson doesn't maintain an "object cache" or anything like that (nor does any other JSON library in Python). To get the data again from the JSON blob, you'd have to re-parse the blob itself (or the file, whatever it is).
What I might recommend is you add these variables from the JSON to the class rather than each instance, and only when necessary. For instance:
class Something(object):
@classmethod
def _load_from_json(cls, blob):
for k, v in blob.iteritems():
setattr(cls, k, v)
The call to _load_from_json
will be relatively expensive, but you should only have to do it whenever the JSON itself changes, and then all instances of Something
will have attribute-like access to the data.
On a similar note, if you are looking at a file here, I recently published a gist that allows for real-time object-like access of a YAML file (although you can really just swap out yaml
for json
in the code). It might do what you're looking for.
Upvotes: 2
Reputation: 110301
If you will simply load your JSON data into a Python object, just pass your relevant dictionary to the __init__
method - there you can simply override your instance __dict__
with the passed in dictionary:
>>> import json
>>> json_data = '{"a": "my data"}'
>>> data = json.loads(json_data)
>>> class AutoVar(object):
... def __init__(self, data):
... self.__dict__ = data
...
>>> test = AutoVar(data)
>>> test.a
u'my data'
Upvotes: 10