Have node_setattr delete keys when the value to set is None.

Note that this looks at the value directly; when using {'key': None} as
value, it is not None and thus will be set. This thus is NOT a way to
be 100% sure there are no None values.
This commit is contained in:
2016-10-04 12:00:52 +02:00
parent 88c20bf341
commit 97fde17d93
2 changed files with 14 additions and 9 deletions

View File

@@ -135,14 +135,18 @@ class ShotManager(object):
def node_setattr(node, key, value):
"""Sets a node property by dotted key.
Modifies the node in-place.
Modifies the node in-place. Deletes None values.
"""
set_on = node
while key and '.' in key:
head, key = key.split('.', 1)
set_on = node[head]
set_on[key] = value
set_on = set_on[head]
if value is None:
set_on.pop(key, None)
else:
set_on[key] = value
@register_patch_handler(node_type_shot['name'])

View File

@@ -157,7 +157,7 @@ class NodeSetattrTest(unittest.TestCase):
node = {}
node_setattr(node, 'a', None)
node_setattr(node, None, 'b')
self.assertEqual({'a': None, None: 'b'}, node)
self.assertEqual({None: 'b'}, node)
def test_none_dotted(self):
from attract.shots import node_setattr
@@ -167,15 +167,16 @@ class NodeSetattrTest(unittest.TestCase):
node = {'b': {}}
node_setattr(node, 'b.simple', None)
self.assertEqual({'b': {'simple': None}}, node)
self.assertEqual({'b': {}}, node)
node_setattr(node, 'b.complex', {'yes': None})
self.assertEqual({'b': {'simple': None,
'complex': {'yes': None}}}, node)
self.assertEqual({'b': {'complex': {'yes': None}}}, node)
node_setattr(node, 'b.complex.yes', None)
self.assertEqual({'b': {'complex': {}}}, node)
node_setattr(node, 'b.complex', {None: 5})
self.assertEqual({'b': {'simple': None,
'complex': {None: 5}}}, node)
self.assertEqual({'b': {'complex': {None: 5}}}, node)
class PatchShotTest(AbstractShotTest):