fix dependency ordering bug in basic properties containing template strings

This commit is contained in:
evilchili 2023-12-31 09:55:09 -08:00
parent 07884a0fbe
commit 2dda47f9c4
2 changed files with 28 additions and 14 deletions

View File

@ -30,7 +30,7 @@ class AttributeMap(Mapping):
AttributeMap is a data class that is also a mapping, converting a dict AttributeMap is a data class that is also a mapping, converting a dict
into an object with attributes. Example: into an object with attributes. Example:
>>> amap = AttributeDict(attributes={'foo': True, 'bar': False}) >>> amap = AttributeMap(attributes={'foo': True, 'bar': False})
>>> amap.foo >>> amap.foo
True True
>>> amap.bar >>> amap.bar
@ -40,7 +40,7 @@ class AttributeMap(Mapping):
recursively transform dictionary members sinto AttributeMaps: recursively transform dictionary members sinto AttributeMaps:
>>> nested_dict = {'foo': {'bar': {'baz': True}, 'boz': False}} >>> nested_dict = {'foo': {'bar': {'baz': True}, 'boz': False}}
>>> amap = AttributeDict.from_dict(nested_dict) >>> amap = AttributeMap.from_dict(nested_dict)
>>> amap.foo.bar.baz >>> amap.foo.bar.baz
True True
>>> amap.foo.boz >>> amap.foo.boz
@ -48,14 +48,14 @@ class AttributeMap(Mapping):
The dictionary can be accessed directly via 'attributes': The dictionary can be accessed directly via 'attributes':
>>> amap = AttributeDict(attributes={'foo': True, 'bar': False}) >>> amap = AttributeMap(attributes={'foo': True, 'bar': False})
>>> list(amap.attributes.keys()): >>> list(amap.attributes.keys()):
>>>['foo', 'bar'] >>>['foo', 'bar']
Because AttributeMap is a mapping, you can use it anywhere you would use Because AttributeMap is a mapping, you can use it anywhere you would use
a regular mapping, like a dict: a regular mapping, like a dict:
>>> amap = AttributeDict(attributes={'foo': True, 'bar': False}) >>> amap = AttributeMap(attributes={'foo': True, 'bar': False})
>>> 'foo' in amap >>> 'foo' in amap
True True
>>> "{foo}, {bar}".format(**amap) >>> "{foo}, {bar}".format(**amap)
@ -97,7 +97,7 @@ class Item(AttributeMap):
""" """
Item is the base class for items, weapons, and spells, and is intended to Item is the base class for items, weapons, and spells, and is intended to
be subclassed to define those things. Item extends AttributeMap to provide be subclassed to define those things. Item extends AttributeMap to provide
some helper methods, including the name and description properties.o some helper methods, including the name and description properties.
Creating Items Creating Items
@ -234,7 +234,7 @@ class Item(AttributeMap):
return obj return obj
# step through the supplied attributes and format each member. # step through the supplied attributes and format each member.
for k, v in attrs.items(): for k, v in sorted(attrs.items(), key=lambda i: '{' in f"{i[0]}{i[1]}"):
if type(v) is dict: if type(v) is dict:
attributes[k] = AttributeMap.from_dict(_format(v)) attributes[k] = AttributeMap.from_dict(_format(v))
else: else:

View File

@ -1,11 +1,25 @@
from dnd_item import types from dnd_item import types
def test_item_attributes(): def test_Item_attributes():
item = types.Item.from_dict( assert types.Item.from_dict(dict(
foo='bar', name='{length}ft. Pole',
baz={ weight='7lbs.',
'qaz': True value=5,
} length=10
)).name == '10ft. Pole'
def test_item_nested_attributes():
assert types.Item.from_dict(dict(
name='{length}ft. Pole',
length=10,
properties=dict(
engraved=dict(
description='"Property of {info.owner}!"'
),
),
info=dict(
owner='Jules Ultardottir',
) )
assert item.baz.qaz is True )).description == 'Engraved. "Property of Jules Ultardottir!"'