add "implicit groups" to CO3 registry for dynamic key support
This commit is contained in:
@@ -15,6 +15,11 @@ class Vegetable(CO3):
|
||||
self.name = name
|
||||
self.color = color
|
||||
|
||||
#@abstractmethod
|
||||
@collate
|
||||
def cut(self, method):
|
||||
raise NotImplementedError
|
||||
|
||||
class Tomato(Vegetable):
|
||||
def __init__(self, name, radius):
|
||||
super().__init__(name, 'red')
|
||||
@@ -24,30 +29,40 @@ class Tomato(Vegetable):
|
||||
def attributes(self):
|
||||
return vars(self)
|
||||
|
||||
def collation_attributes(self, action_key, action_group):
|
||||
def collation_attributes(self, key, group):
|
||||
return {
|
||||
'name': self.name,
|
||||
'state': action_key,
|
||||
'state': key,
|
||||
}
|
||||
|
||||
@collate('ripe', action_groups=['aging'])
|
||||
@collate('ripe', groups=['aging'])
|
||||
def ripen(self):
|
||||
return {
|
||||
'age': random.randint(1, 6)
|
||||
}
|
||||
|
||||
@collate('rotten', action_groups=['aging'])
|
||||
@collate('rotten', groups=['aging'])
|
||||
def rot(self):
|
||||
return {
|
||||
'age': random.randint(4, 9)
|
||||
}
|
||||
|
||||
@collate('diced', action_groups=['cooking'])
|
||||
@collate('diced', groups=['cooking'])
|
||||
def dice(self):
|
||||
return {
|
||||
'pieces': random.randint(2, 12)
|
||||
}
|
||||
|
||||
@collate
|
||||
def cut(self, method):
|
||||
if method == 'slice':
|
||||
return {
|
||||
'pieces': random.randint(2, 5)
|
||||
}
|
||||
elif method == 'dice':
|
||||
return self.dice()
|
||||
|
||||
|
||||
type_list = [Vegetable, Tomato]
|
||||
|
||||
'''
|
||||
@@ -114,8 +129,8 @@ vegetable_mapper = ComposableMapper(
|
||||
def attr_name_map(cls):
|
||||
return f'{cls.__name__.lower()}'
|
||||
|
||||
def coll_name_map(cls, action_group):
|
||||
return f'{cls.__name__.lower()}_{action_group}_states'
|
||||
def coll_name_map(cls, group):
|
||||
return f'{cls.__name__.lower()}_{group}_states'
|
||||
|
||||
vegetable_mapper.attach_many(
|
||||
type_list,
|
||||
|
||||
@@ -11,13 +11,18 @@ def test_co3_registry():
|
||||
keys_to_groups = defaultdict(list)
|
||||
|
||||
# collect groups each key is associated
|
||||
for action_group, action_keys in tomato.group_registry.items():
|
||||
for action_key in action_keys:
|
||||
keys_to_groups[action_key].append(action_group)
|
||||
for group, keys in tomato.group_registry.items():
|
||||
for key in keys:
|
||||
keys_to_groups[key].append(group)
|
||||
|
||||
# check against `action_registry`, should map keys to all groups
|
||||
for action_key, (_, action_groups) in tomato.action_registry.items():
|
||||
assert keys_to_groups.get(action_key) == action_groups
|
||||
assert set(tomato.key_registry.get(None,{}).keys()) == set(keys_to_groups.get(None,[]))
|
||||
|
||||
# check against `registry`, should map keys to all groups
|
||||
for key, group_obj in tomato.key_registry.items():
|
||||
if key is None: continue
|
||||
|
||||
_, groups = group_obj
|
||||
assert keys_to_groups.get(key) == groups
|
||||
|
||||
def test_co3_attributes():
|
||||
assert tomato.attributes is not None
|
||||
@@ -26,11 +31,12 @@ def test_co3_components():
|
||||
assert tomato.components is not None
|
||||
|
||||
def test_co3_collation_attributes():
|
||||
for action_group, action_keys in tomato.group_registry.items():
|
||||
for action_key in action_keys:
|
||||
assert tomato.collation_attributes(action_key, action_group) is not None
|
||||
for group, keys in tomato.group_registry.items():
|
||||
for key in keys:
|
||||
assert tomato.collation_attributes(key, group) is not None
|
||||
|
||||
def test_co3_collate():
|
||||
for action_group, action_keys in tomato.group_registry.items():
|
||||
for action_key in action_keys:
|
||||
assert tomato.collate(action_key) is not None
|
||||
for group, keys in tomato.group_registry.items():
|
||||
for key in keys:
|
||||
if key is None: continue
|
||||
assert tomato.collate(key) is not None
|
||||
|
||||
Reference in New Issue
Block a user