def setUp(self): super().setUp() self.flock = FlockDict() self.flock['a'] = 'Original Value' self.flock['b'] = {'i': 'Original Value', 'ii': 42} self.flock['c'] = [1, 2, 3] self.flock['d'] = set('ABC')
def apply_skills(character): character.setdefault('skills', []) for skill in list(character['skills']): pass # character['skills'] = ...... character['points'].setdefault('spent', FlockDict()) character['points'].setdefault('available', FlockDict()) character['points']['spent']['mental'] = lambda: sum( skill.cost for skill in character['skills'] if skill.isMental) character['points']['available']['mental'] = lambda: character['points']['total']['mental'] - \ character['points']['spent']['mental'] character['points']['spent']['physical'] = lambda: sum( skill.cost for skill in character['skills'] if skill.isPhysical) character['points']['available']['physical'] = lambda: character['points']['total']['physical'] - \ character['points']['spent']['physical'] character['points']['spent']['heroic'] = lambda: sum( skill.cost for skill in character['skills'] if skill.isHeroic) character['points']['available']['heroic'] = lambda: character['points']['total']['heroic'] - \ character['points']['spent']['heroic'] character['points']['spent']['universal'] = lambda: -sum( min(0, character['points']['available'][pt_type]) for pt_type in character['points']['available'] if pt_type != 'universal') character['points']['available']['universal'] = lambda: character['points']['total']['universal'] - \ character['points']['spent']['universal']
class ShearTestCase(unittest.TestCase): def setUp(self): super().setUp() self.flock = FlockDict() self.flock['a'] = 'Original Value' self.flock['b'] = {'i': 'Original Value', 'ii': 42} self.flock['c'] = [1, 2, 3] self.flock['d'] = set('ABC') def test_consistent_types(self): for collection in 'bcd': pre_shear_type = type(self.flock[collection]) self.flock.shear() post_shear_type = type(self.flock[collection]) assert pre_shear_type is post_shear_type def test_edit_post_shear(self): self.flock.shear() self.flock['b']['i'] = 'New Value' self.flock['c'].append(4) self.flock['d'].add('D') assert self.flock['c'] == [1, 2, 3, 4] assert self.flock['b']['i'] == 'New Value' assert 'D' in self.flock['d'] self.flock['unreleated'] = 'Something' assert self.flock['b']['i'] == 'New Value' assert self.flock['c'] == [1, 2, 3, 4] assert 'D' in self.flock['d']
class FlockAggregatorTestCase(unittest.TestCase): def setUp(self): super().setUp() self.flock = FlockDict() self.flock['x'] = {x: x for x in range(1, 10)} self.flock['y'] = {x: 2 * x for x in range(1, 10)} def test_shear_list(self): self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: sum(x)) assert not self.flock.check() assert len(self.flock['sum']) == 9 self.assertContentsEqual(self.flock['sum'].keys(), set(range(1, 10))) self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10))) self.assertContentsEqual(self.flock['sum'].items(), ((x,x * 3) for x in range(1, 10))) assert self.flock['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)} assert self.flock['sum']() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)} sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared def test_shear_func(self): self.flock['sum'] = FlockAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum) assert not self.flock.check() assert len(self.flock['sum']) == 9 self.assertContentsEqual(self.flock['sum'].keys(), range(1, 10)) self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10))) self.assertContentsEqual(self.flock['sum'].items(), ((x,x * 3) for x in range(1, 10))) assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)} assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)} sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared def test_check(self): self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: int(x)) check = self.flock.check() assert check assert len(check['sum']) == 9 for value in check['sum'].values(): assert len(value) == 2 def assertContentsEqual(self, param, param1, *args, **kwargs): return self.assertSetEqual(set(param), set(param1), *args, **kwargs)
class FlockAggregatorTestCase(unittest.TestCase): def setUp(self): super().setUp() self.flock = FlockDict() self.flock['x'] = {x: x for x in range(1, 10)} self.flock['y'] = {x: 2 * x for x in range(1, 10)} def test_shear_list(self): self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: sum(x)) assert not self.flock.check() assert len(self.flock['sum']) == 9 self.assertContentsEqual(self.flock['sum'].keys(), set(range(1, 10))) self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10))) self.assertContentsEqual(self.flock['sum'].items(), ((x, x * 3) for x in range(1, 10))) assert self.flock['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)} assert self.flock['sum']() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)} sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared def test_shear_func(self): self.flock['sum'] = FlockAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum) assert not self.flock.check() assert len(self.flock['sum']) == 9 self.assertContentsEqual(self.flock['sum'].keys(), range(1, 10)) self.assertContentsEqual(self.flock['sum'].values(), (x * 3 for x in range(1, 10))) self.assertContentsEqual(self.flock['sum'].items(), ((x, x * 3) for x in range(1, 10))) assert self.flock.promises['sum'].shear() == {x: x * 3 for x in range(1, 10)} assert self.flock.promises['sum']() == {x: x * 3 for x in range(1, 10)} assert self.flock['sum'] == {x: x * 3 for x in range(1, 10)} sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared def test_check(self): self.flock['sum'] = FlockAggregator([self.flock['x'], self.flock['y']], lambda x: int(x)) check = self.flock.check() assert check assert len(check['sum']) == 9 for value in check['sum'].values(): assert len(value) == 2 def assertContentsEqual(self, param, param1, *args, **kwargs): return self.assertSetEqual(set(param), set(param1), *args, **kwargs)
def apply_level_allotments(character): if 'level' not in character: character['level'] = 1 character.setdefault('points', FlockDict()) character['points'].setdefault('total', FlockDict({'universal': 10})) character['points']['total']['mental'] = lambda: character[ 'level'] * character['bonuses']['Mental Skill Points'] character['points']['total']['physical'] = lambda: character[ 'level'] * character['bonuses']['Phy Skill Points'] character['points']['total']['heroic'] = lambda: character['level'] * ( character['level'] + 1 + character['bonuses']['heroics'])
class MetaAggregatorTestCase(unittest.TestCase): def setUp(self): super().setUp() self.flock = FlockDict() self.flock['x'] = {x: x for x in range(1, 10)} self.flock['y'] = {x: 2 * x for x in range(1, 10)} def test_shear(self): self.flock['sum'] = MetaAggregator(lambda: [self.flock[ls] for ls in ['x', 'y']], sum) assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared
def apply_attribute_table(character): if 'Attribute_Bonuses' not in character: character['Attribute_Bonuses'] = FlockDict() for (attribute, bonus), table in get_attribute_table().items(): character['Attribute_Bonuses'][bonus] = lookup(character, attribute, table) character['base_bonuses'] = {'Spell Points Multiple': {'General': 1}} character['bonuses'] = FlockAggregator( [character['Attribute_Bonuses'], character['base_bonuses']], cross_total)
def apply_racial_bonuses(character): if character["Race"] == "Human": preVal = character["Spirit"] character["Spirit"] = lambda: preVal + 2 character.setdefault('Racial Bonuses', FlockDict()) character['Racial Bonuses']['heroics'] = 1 # print("character bonuses {ct_type} sources {sources} bonuses {rb_type}".format( # ct_type=type(character.promises['bonuses']), rb_type=type(character.promises['Racial Bonuses']), # sources=character.promises['bonuses'].sources)) character.promises['bonuses'].sources.append(character['Racial Bonuses'])
def test_split_cache(self): self.flock2 = FlockDict() self.flock2['dest'] = reference(self.flock, 'source') self.flock2['nested_dest'] = {'dest': reference(self.flock, 'nested_source', 'source')} self.flock2['jump_dest'] = {'dest': reference(self.flock['nested_source'], 'source')} assert self.flock2['dest'] == self.flock2['nested_dest']['dest'] == 'Original Value' self.flock['source'] = '1st New Value' assert self.flock2['dest'] == '1st New Value' assert self.flock2['nested_dest']['dest'] == self.flock2['jump_dest']['dest'] == 'Original Value' self.flock['nested_source']['source'] = '2nd New Value' assert self.flock2['dest'] == '1st New Value' assert self.flock2['nested_dest']['dest'] == '2nd New Value' assert self.flock2['jump_dest']['dest'] == '2nd New Value'
class AggregatorTestCase(unittest.TestCase): def setUp(self): super().setUp() self.flock = FlockDict() self.flock['x'] = {x: x for x in range(1, 10)} self.flock['y'] = {x: 2 * x for x in range(1, 10)} def test_shear(self): self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: sum(x)) assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 3 assert isinstance(sheared, dict) assert sheared['sum'] == {x: x * 3 for x in range(1, 10)} assert dict(self.flock()) == sheared def test_check(self): self.flock['sum'] = Aggregator([self.flock['x'], self.flock['y']], lambda x: int(x)) check = self.flock.check() assert check assert len(check['sum']) == 9 for value in check['sum'].values(): assert len(value) == 2
class BasicFlockTestCase(unittest.TestCase): """ Tests of the basic operations of a flock """ def setUp(self): super().setUp() self.flock = FlockDict() def test_simple_values(self): """ Test that simple(non-dict, non-callable) values are stored and retrieved transparently. """ assert len(self.flock) == 0 self.flock[3] = 3 assert self.flock[3] == 3 assert len(self.flock) == 1 self.flock["Shepherd"] = "Mary" assert len(self.flock) == 2 self.assertEqual(self.flock["Shepherd"], "Mary") self.flock["Management"] = ["Mary", "Joshua", "Isaac"] assert len(self.flock) == 3 self.assertEqual(self.flock["Management"], ["Mary", "Joshua", "Isaac"]) self.flock["Shepherd"] = "John" self.assertEqual(self.flock["Shepherd"], "John") assert "Shepherd" in self.flock assert 'missing' not in self.flock del self.flock["Shepherd"] assert "Shepherd" not in self.flock def test_simple_dict(self): """ Test that nested dicts still look like dicts. """ self.flock["Management"] = {"Mary": {"lambs": 1, "size": 'little'}} assert self.flock["Management"]["Mary"]['lambs'] == 1 assert self.flock["Management"]["Mary"]['size'] == 'little' def test_simple_closure(self): """ Test that simple closures are stored without mangling and evaluated on retrieval """ self.flock["lamb"] = lambda: "little" self.assertEqual(self.flock["lamb"], "little") def test_callable_with_arguments(self): """ Test that callables with arguments are passed transparently and never called """ self.flock["func"] = lambda x, y: x + y self.assertEqual(self.flock["func"](1, 2), 3) def test2(x, y): assert False self.flock["test2"] = test2 assert test2 == self.flock["test2"] # Testing that this is not called def test_error(self): self.flock["bad"] = lambda: 1 / 0 assert 'bad' in self.flock with raises(FlockException) as exc_info: self.flock.pop('bad') assert isinstance(exc_info.value.__cause__, ZeroDivisionError) with raises(FlockException) as exc_info: assert self.flock['bad'] != ( lambda: 1 / 0), "This should not be called at all as the exception should be raised" assert isinstance(exc_info.value.__cause__, ZeroDivisionError) with raises(FlockException) as exc_info: self.flock.shear() assert isinstance(exc_info.value.__cause__, ZeroDivisionError) error = self.flock.shear(record_errors=True)['bad'] assert isinstance(error, FlockException) assert isinstance(error.__cause__, ZeroDivisionError) def test_shear(self): """ Test trivial shear operation """ self.flock[3] = 15 assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 1 assert isinstance(sheared, dict) assert sheared[3] == 15 self.flock['cat'] = lambda: 'Abbey' assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 2 assert isinstance(sheared, dict) assert sheared['cat'] == 'Abbey' assert sheared[3] == 15 assert self.flock.dataset() == {'cat':'Abbey',3:15} assert len(self.flock.ruleset()) == 0 assert not self.flock.ruleset() def test_consistent_shear(self): t = toggle() self.flock['toggle'] = t self.flock['toggle2'] = t self.flock.update({x: lambda: self.flock['toggle'] for x in range(5)}) sheared = self.flock.shear() self.assertEqual(sheared['toggle'], not sheared['toggle2']) self.assertEqual([sheared['toggle']] * 5, [sheared[x] for x in range(5)]) assert len(self.flock.dataset()) == 0 assert not self.flock.dataset() assert len(self.flock.ruleset()) == 7 assert all(isinstance(x, FunctionType) for x in self.flock.ruleset().values())
def setUp(self): super().setUp() self.flock = FlockDict() self.flock['x'] = {x: x for x in range(1, 10)} self.flock['y'] = {x: 2 * x for x in range(1, 10)}
def setUp(self): super().setUp() self.flock = FlockDict() self.flock['source'] = 'Original Value' self.flock['nested_source'] = {'source': 'Original Value'}
def setUp(self): super().setUp() self.flock = FlockDict()
def load_character(filename): sheet = pickle.load(open(filename, 'rb')) ret = FlockDict(sheet) return apply_rules(ret)
default=None, help="File to read character from.") parser.add_argument("--outfile", type=argparse.FileType('w'), default=None, help="File to read character from.") return parser if __name__ == "__main__": logging.basicConfig() parser = get_parser() opt = parser.parse_args() # char = load_character("Mondavite2.pkl") char = FlockDict() if not opt.infile: char['base_stats'] = { 'Combat Skill': 13, 'Dexterity': 16, 'Health': 11, 'Intelligence': 18, 'Magic': 17, 'Perception': 20, 'Presence': 11, 'Speed': 13, 'Spirit': 10, 'Strength': 10, 'Luck': 10 }
def get_parser(): parser = argparse.ArgumentParser(description='Mythica Character manager', ) parser.add_argument("--infile", type=argparse.FileType('r'), default=None, help="File to read character from.") parser.add_argument("--outfile", type=argparse.FileType('w'), default=None, help="File to read character from.") return parser if __name__ == "__main__": logging.basicConfig() parser = get_parser() opt = parser.parse_args() # char = load_character("Mondavite2.pkl") char = FlockDict() if not opt.infile: char['base_stats'] = {'Combat Skill': 13, 'Dexterity': 16, 'Health': 11, 'Intelligence': 18, 'Magic': 17, 'Perception': 20, 'Presence': 11, 'Speed': 13, 'Spirit': 10, 'Strength': 10, 'Luck': 10} char['practice_sessions'] = {'Combat Skill': 9, 'Dexterity': 2} char['skills'] = [ Skill('Read Common', MENTAL), Skill('Bargaining', MENTAL), Skill('Begging', MENTAL, 2), Skill('Appraisal', MENTAL), Skill('Local History', MENTAL), Skill('Endow Plants', MENTAL), Skill('Fishing', MENTAL), Skill('Artificer', MENTAL, 2), Skill('Stiletto', WEAPON, 2),
class BasicFlockTestCase(unittest.TestCase): """ Tests of the basic operations of a flock """ def setUp(self): super().setUp() self.flock = FlockDict() def test_simple_values(self): """ Test that simple(non-dict, non-callable) values are stored and retrieved transparently. """ assert len(self.flock) == 0 self.flock[3] = 3 assert self.flock[3] == 3 assert len(self.flock) == 1 self.flock["Shepherd"] = "Mary" assert len(self.flock) == 2 self.assertEqual(self.flock["Shepherd"], "Mary") self.flock["Management"] = ["Mary", "Joshua", "Isaac"] assert len(self.flock) == 3 self.assertEqual(self.flock["Management"], ["Mary", "Joshua", "Isaac"]) self.flock["Shepherd"] = "John" self.assertEqual(self.flock["Shepherd"], "John") def test_simple_dict(self): """ Test that nested dicts still look like dicts. """ self.flock["Management"] = {"Mary": {"lambs": 1, "size": 'little'}} assert self.flock["Management"]["Mary"]['lambs'] == 1 assert self.flock["Management"]["Mary"]['size'] == 'little' def test_simple_closure(self): """ Test that simple closures are stored without mangling and evaluated on retrieval """ self.flock["lamb"] = lambda: "little" self.assertEqual(self.flock["lamb"], "little") def test_shear(self): """ Test trivial shear opperation """ self.flock[3] = 15 assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 1 assert isinstance(sheared, dict) assert sheared[3] == 15 self.flock['cat'] = lambda: 'Abbey' assert not self.flock.check() sheared = self.flock.shear() assert len(sheared) == 2 assert isinstance(sheared, dict) assert sheared['cat'] == 'Abbey' def test_consistent_shear(self): t = toggle() self.flock['toggle'] = t self.flock['toggle2'] = t self.flock.update({x: lambda: self.flock['toggle'] for x in range(5)}) sheared = self.flock.shear() self.assertEqual(sheared['toggle'], not sheared['toggle2']) self.assertEqual([sheared['toggle']] * 5, [sheared[x] for x in range(5)])