Exemplo n.º 1
0
    def _read(self):
        """
        Called by `data.setter`; reads a file using the configparser and assigns the result to `self._data`.
        """

        JSON = 'json'

        if self.fintype != JSON:

            # Read the config file using the objects config parser that was created during initialisation.
            self.cfgparser.read(self.fpath)

            # Start building the return value of the function.
            result = {}

            for section in self.cfgparser.sections():
                _build(
                    result, section.split(self.sep), {
                        key: _cast(val)
                        for key, val in self.cfgparser[section].items()
                    })

            self._data = EZDict.from_dict(result)

        else:

            self._data = EZDict.from_dict(json.load(open(self.fpath, 'r')))
Exemplo n.º 2
0
fpath = './config.json'
ezcfg = EZConfig(fpath).data
print(f'ezcfg.db.ip = {ezcfg.db.ip}')

# ----------------------------------------------------------------------------------------------------------------------
# EZDict examples
# ----------------------------------------------------------------------------------------------------------------------
print('\n-- EZDict examples\n')

# -- A dictionary
d = {'first': 'John',
     'last': 'Doe'}

# -- Create an easy dict
ezd = EZDict.from_dict(d)

# Check the contents of the easy dictionary.
pprint(ezd)

# -- Easily access values
print(f'ezd.first = {ezd.first}')

# -- Easily set values
ezd.first = 'Mary'
print(f'ezd.first = {ezd.first}')

# -- A dictionary with a nested dictionary
dd = {'first': 'John',
      'last': 'Doe',
      'phones': {
Exemplo n.º 3
0
from ezdict import EZDict
from pprint import pprint

d = {'one': 1, 'has space': 2, '__repr__': 'muahaha', 'with': 'love'}

print(f'\n-- d')
pprint(d)

ezd = EZDict.from_dict(d)

print(f'\n-- ezd')
pprint(ezd)

# -- CASE #1
print()
print(f"ezd['one'] = {ezd['one']}")
print(f'ezd.one = {ezd.one}')

# -- CASE #2
print()
print(f"ezd['has space'] = {ezd['has space']}")
# print(f"ezd.has space = {ezd.has space}")  # error
# print(f"ezd.'has space' = {ezd.'has space'}")  # error

# -- CASE #3
print()
print(f"ezd['__repr__'] = {ezd['__repr__']}")
print(f"ezd.__repr__ = {ezd.__repr__}")
print(f"ezd.__repr__() = {ezd.__repr__()}")

# -- CASE #4