Skip to content

Instantly share code, notes, and snippets.

@webghostx
Last active March 4, 2017 17:04
Show Gist options
  • Save webghostx/f2a7fc80e41b2f18f133213f6c55583d to your computer and use it in GitHub Desktop.
Save webghostx/f2a7fc80e41b2f18f133213f6c55583d to your computer and use it in GitHub Desktop.

Revisions

  1. webghostx renamed this gist Mar 4, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. webghostx revised this gist Mar 4, 2017. No changes.
  3. webghostx revised this gist Mar 4, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion config.py
    Original 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
    http://usysto.net/formatierte-json-konfiguration-python_846
    '''

    def __init__(self, configFile = False):
  4. webghostx revised this gist Mar 4, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions config.py
    Original 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
    load and save readable
    json configuration files
    http://usysto.net
    '''
  5. webghostx revised this gist Mar 4, 2017. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion config.py
    Original 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'
    print 'New value has been saved'

    #end
  6. webghostx created this gist Mar 4, 2017.
    61 changes: 61 additions & 0 deletions config.py
    Original 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'