Last active
January 12, 2016 11:12
-
-
Save dmitryhd/4118cf1c6e79c48b01ef to your computer and use it in GitHub Desktop.
stored json dict with access as class members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import pprint | |
| class Stored: | |
| def __init__(self, init_dict=None): | |
| if init_dict: | |
| self.__dict__ = init_dict | |
| def save(self, file_name): | |
| with open(file_name, 'w') as fd: | |
| fd.write(self.to_json()) | |
| def to_json(self): | |
| return json.dumps(self.__dict__, sort_keys=True, indent=4) | |
| @classmethod | |
| def from_json(cls, json_str): | |
| return cls(json.loads(json_str)) | |
| @classmethod | |
| def load(cls, file_name): | |
| with open(file_name, 'r') as fd: | |
| json_str = fd.read() | |
| return cls(json.loads(json_str)) | |
| def __repr__(self): | |
| return pprint.pformat(self.__dict__) | |
| class Settings(Stored): | |
| """ Settings object. Must be passed to main classes. """ | |
| def __init__(self, *args, **kwargs): | |
| self.context_name = 'base' | |
| self.mail_timeout = 60 | |
| super().__init__(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment