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))
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')))
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) )
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)
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)
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))
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) )
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': {
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) )
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))
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))
def test_empty(self): self.assertEqual({}, EZDict())
def test_from_dict(self): data = {str(i): i for i in range(10)} self.assertEqual(dict(data), EZDict(data))
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
def test_double_nested(self): self.assertEqual(small_data[0]["favoriteFruit"]["apple"], EZDict(small_data[0]).favoriteFruit.apple)
def test_single_nested(self): self.assertEqual(small_data[0]["friends"], EZDict(small_data[0]).friends)
def test_from_nested(self): self.assertEqual(dict(small_data[0]), EZDict(small_data[0]))
def test_from_seq(self): seq = [(str(i), i) for i in range(10)] self.assertEqual(dict(seq), EZDict(seq))