def test_map_add_one_to_keys(self): dictionary = { 0: { 1: {}, 2: {} }, 1: { 2: {}, 3: {}, 5: {} }, } expected = { 1: { 2: {}, 3: {} }, 2: { 3: {}, 4: {}, 6: {} }, } actual = dicttools.map(lambda x, y: (x + 1, y), dictionary) self.assertEquals(expected, actual, msg="%s != %s" % (expected, actual))
def test_map_performance(self): total = 1000000 items = [ (x, float(x)) for x in range(0, total) ] dictionary = dict(items) start = time.time() dicttools.map(lambda x, y: (x, y < total/2), dictionary) dictionary_elapsed = time.time() - start start = time.time() map(lambda (x, y): (x, y < total/2), items) list_elapsed = time.time() - start print('\n dict map is %2.2fx slower than list map' % \ (dictionary_elapsed/list_elapsed)) start = time.time() dicttools.map(lambda x, y: (x, y < total/2), dictionary, recursive=False) dictionary_elapsed = time.time() - start print(' dict map(recursive=False) is %2.2fx slower than list map' % \ (dictionary_elapsed/list_elapsed))
def test_map_create_new_nodes(self): dictionary = { 0: { 1: {}, 2: {} }, 1: { 2: {}, 3: {}, 5: {} }, } expected = { 0: { 1: { 0: {}}, 2: { 0: {}} }, 1: { 2: { 0: {}}, 3: { 0: {}}, 5: { 0: {}} }, } def replace_empty_dict(key, value): if value == {}: return (key, { 0: {}}) else: return (key, value) actual = dicttools.map(replace_empty_dict, dictionary) self.assertEquals(expected, actual, msg="%s != %s" % (expected, actual))
def test_map_replace_empty_dict_with_None(self): dictionary = { 0: { 1: {}, 2: {} }, 1: { 2: {}, 3: {}, 5: {} }, } expected = { 0: { 1: None, 2: None }, 1: { 2: None, 3: None, 5: None }, } def replace_empty_dict(key, value): if value == {}: return (key, None) else: return (key, value) actual = dicttools.map(replace_empty_dict, dictionary) self.assertEquals(expected, actual, msg="%s != %s" % (expected, actual))