示例#1
0
    def test_group_by_flat(self):
        l = [i % 3 for i in range(12)]
        ez = EZDict()
        for item in l:
            ez.appender(item, item)

        self.assertEqual(ez, Query("$group_by").single(l))
示例#2
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')))
示例#3
0
    def test_group_by_number(self):
        data = [[i, i] for i in range(10)]
        ez = EZDict()
        for item in data:
            ez.appender(item[0], item)

        self.assertEqual(
            ez,
            Query("$group_by(0)").single(data)
        )
示例#4
0
    def test_append(self):
        genders = EZDict()
        manual = {}
        for item in small_data:
            genders.appender(EZDict(item).gender, item)

            if item["gender"] in manual:
                manual[item["gender"]].append(item)
            else:
                manual[item["gender"]] = [item]

        self.assertEqual(manual, genders)
示例#5
0
    def test_increment(self):
        genders = EZDict()
        manual = {}
        for item in small_data:
            genders.incrementer(EZDict(item).gender)

            if item["gender"] in manual:
                manual[item["gender"]] += 1
            else:
                manual[item["gender"]] = 1

        self.assertEqual(manual, genders)
示例#6
0
 def test_arith_basic(self):
     data = EZDict({"a": 4, "b": -4, "c": 2.5, "d": [3, 4], "e": 0, "pi": 3.1415926})
     self.assertEqual(data.a + data.b + data.c, Query("a.$arith('+', @b + @c)").single(data))
     self.assertEqual(data.a + data.b - data.c, Query("a.$arith('+', @b - @c)").single(data))
     self.assertEqual(data.a + data.b * data.c, Query("a.$arith('+', @b * @c)").single(data))
     self.assertEqual(data.a + data.b / data.c, Query("a.$arith('+', @b / @c)").single(data))
     self.assertEqual(data.a + data.b ** data.c, Query("a.$arith('+', @b ** @c)").single(data))
     self.assertEqual(data.a + data.b // data.c, Query("a.$arith('+', @b // @c)").single(data))
     self.assertEqual(data.a + data.b % data.c, Query("a.$arith('+', @b % @c)").single(data))
示例#7
0
 def test_math_args(self):
     data = EZDict({"a": 4, "b": -4, "c": 2.5, "d": [3, 4], "e": 0, "pi": 3.1415926})
     self.assertEqual(
         data.a / (data.b + data.c) - data.pi,
         Query("$inject(@a / (@b + @c) - @pi)").single(data)
     )
     self.assertEqual(
         data.a + data.b * data.c ** data.e,
         Query("$inject(@a + @b * @c ** @e)").single(data)
     )
     self.assertEqual(
         (data.a + data.b) * data.c ** data.e,
         Query("$inject((@a + @b) * @c ** @e)").single(data)
     )
示例#8
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': {
示例#9
0
 def test_sort_nested_reverse(self):
     self.assertEqual(
         sorted(small_data, key=lambda x: EZDict(x).favoriteFruit.banana * -0.5, reverse=True),
         Query("$sort('favoriteFruit.banana.$multiply(-0.5)', true)").single(small_data)
     )
示例#10
0
    def test_group_by_nested_count(self):
        ez = EZDict()
        for item in small_data:
            ez.incrementer(round(EZDict(item).favoriteFruit.apple, 1))

        self.assertEqual(ez, Query("$group_by('favoriteFruit.apple.$round(1)', true)").single(small_data))
示例#11
0
    def test_group_by_nested(self):
        ez = EZDict()
        for item in small_data:
            ez.appender(round(EZDict(item).favoriteFruit.apple, 1), item)

        self.assertEqual(ez, Query("$group_by('favoriteFruit.apple.$round(1)')").single(small_data))
示例#12
0
 def test_empty(self):
     self.assertEqual({}, EZDict())
示例#13
0
 def test_from_dict(self):
     data = {str(i): i for i in range(10)}
     self.assertEqual(dict(data), EZDict(data))
示例#14
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
示例#15
0
 def test_double_nested(self):
     self.assertEqual(small_data[0]["favoriteFruit"]["apple"],
                      EZDict(small_data[0]).favoriteFruit.apple)
示例#16
0
 def test_single_nested(self):
     self.assertEqual(small_data[0]["friends"],
                      EZDict(small_data[0]).friends)
示例#17
0
 def test_from_nested(self):
     self.assertEqual(dict(small_data[0]), EZDict(small_data[0]))
示例#18
0
 def test_from_seq(self):
     seq = [(str(i), i) for i in range(10)]
     self.assertEqual(dict(seq), EZDict(seq))