Last active
March 4, 2017 17:04
-
-
Save webghostx/f2a7fc80e41b2f18f133213f6c55583d to your computer and use it in GitHub Desktop.
Revisions
-
webghostx renamed this gist
Mar 4, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
webghostx revised this gist
Mar 4, 2017 . No changes.There are no files selected for viewing
-
webghostx revised this gist
Mar 4, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ class AppConfig: load and save readable json configuration files http://usysto.net/formatierte-json-konfiguration-python_846 ''' def __init__(self, configFile = False): -
webghostx revised this gist
Mar 4, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,8 @@ class AppConfig: ''' sample config class load and save readable json configuration files http://usysto.net ''' -
webghostx revised this gist
Mar 4, 2017 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,14 @@ import json class AppConfig: ''' sample config class load and save readable json configuration files http://usysto.net ''' def __init__(self, configFile = False): self.configFile = False self.get = False @@ -58,4 +66,6 @@ def configSave(self, obj = False): AppConfig.get['object']['int'] = 4 if AppConfig.configSave(): print 'New value has been saved' #end -
webghostx created this gist
Mar 4, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ from collections import OrderedDict # required for maintaining order data import json class AppConfig: def __init__(self, configFile = False): self.configFile = False self.get = False if configFile: self.configFile = configFile self.configLoad() def configLoad(self): try: with open(self.configFile) as data: obj = json.load( data, object_pairs_hook = OrderedDict # maintaining order data ) self.get = obj return True except Exception: return False def configSave(self, obj = False): obj = obj or self.get try: with open(self.configFile, "w") as outfile: json.dump( obj, outfile, ensure_ascii = False, # Keep special characters indent = 4 ) return True except Exception: return False ''' content of *.json { "string": "ok", "bool": true, "object": { "int": 3, "float": 3.3 } } ''' ''' Application example ''' AppConfig = AppConfig() AppConfig.configFile = '/home/user/App/config.json' # absolute path to *.json AppConfig.configLoad() print AppConfig.get['string'] AppConfig.get['object']['int'] = 4 if AppConfig.configSave(): print 'New value has been saved'