Skip to content

Commit 5fc5cb3

Browse files
authored
Merge pull request #690 from compas-dev/attributes-all
Attributes all
2 parents 0c587c7 + 4075b74 commit 5fc5cb3

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
* Fix bug in `compas.datastructures.AttributesView`.
15+
1416
### Removed
1517

1618

src/compas/datastructures/attributes.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
]
1414

1515

16-
class AttributeView(object):
17-
"""Mixin for attribute dict views."""
16+
class AttributeView(MutableMapping):
17+
"""Base class for attribute dict views."""
1818

1919
def __init__(self, defaults, attr, custom_only=False):
2020
super(AttributeView, self).__init__()
@@ -29,7 +29,7 @@ def __str__(self):
2929
return "{" + ", ".join(s) + "}"
3030

3131
def __len__(self):
32-
return len(self.defaults)
32+
return len(set(self.defaults).union(self.attr))
3333

3434
def __getitem__(self, name):
3535
if name not in self.attr:
@@ -48,43 +48,44 @@ def __iter__(self):
4848
for name in self.attr:
4949
yield name
5050
else:
51-
for name in self.defaults:
51+
names = set(self.defaults).union(self.attr)
52+
for name in names:
5253
yield name
5354

5455

55-
class NodeAttributeView(AttributeView, MutableMapping):
56+
class NodeAttributeView(AttributeView):
5657
"""Mutable Mapping that provides a read/write view of the custom attributes of a node
5758
combined with the default attributes of all nodes."""
5859

5960
def __init__(self, defaults, attr, custom_only=False):
6061
super(NodeAttributeView, self).__init__(defaults, attr, custom_only)
6162

6263

63-
class VertexAttributeView(AttributeView, MutableMapping):
64+
class VertexAttributeView(AttributeView):
6465
"""Mutable Mapping that provides a read/write view of the custom attributes of a vertex
6566
combined with the default attributes of all vertices."""
6667

6768
def __init__(self, defaults, attr, custom_only=False):
6869
super(VertexAttributeView, self).__init__(defaults, attr, custom_only)
6970

7071

71-
class EdgeAttributeView(AttributeView, MutableMapping):
72+
class EdgeAttributeView(AttributeView):
7273
"""Mutable Mapping that provides a read/write view of the custom attributes of an edge
7374
combined with the default attributes of all edges."""
7475

7576
def __init__(self, defaults, attr, custom_only=False):
7677
super(EdgeAttributeView, self).__init__(defaults, attr, custom_only)
7778

7879

79-
class FaceAttributeView(AttributeView, MutableMapping):
80+
class FaceAttributeView(AttributeView):
8081
"""Mutable Mapping that provides a read/write view of the custom attributes of a face
8182
combined with the default attributes of all faces."""
8283

8384
def __init__(self, defaults, attr, custom_only=False):
8485
super(FaceAttributeView, self).__init__(defaults, attr, custom_only)
8586

8687

87-
class CellAttributeView(AttributeView, MutableMapping):
88+
class CellAttributeView(AttributeView):
8889
"""Mutable Mapping that provides a read/write view of the custom attributes of a cell
8990
combined with the default attributes of all faces."""
9091

tests/compas/datastructures/test_mesh.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# import pytest
2-
import os
3-
import compas
41
import json
2+
import os
3+
54
import pytest
65

6+
import compas
77
from compas.datastructures import Mesh
88
from compas.datastructures import meshes_join_and_weld
9+
from compas.geometry import Box
910
from compas.geometry import Polygon
1011
from compas.geometry import Translation
1112

@@ -20,6 +21,12 @@ def cube():
2021
return Mesh.from_polyhedron(6)
2122

2223

24+
@pytest.fixture
25+
def box():
26+
box = Box.from_width_height_depth(2, 2, 2)
27+
return Mesh.from_shape(box)
28+
29+
2330
@pytest.fixture
2431
def hexagon():
2532
polygon = Polygon.from_sides_and_radius_xy(6, 1)
@@ -666,3 +673,14 @@ def test_faces_on_boundaries_triangleboundarychain(triangleboundarychain):
666673
# --------------------------------------------------------------------------
667674
# attributes
668675
# --------------------------------------------------------------------------
676+
677+
def test_face_attributes_includes_all_defaults(box):
678+
box.update_default_face_attributes({"attr1": "value1", "attr2": "value2"})
679+
680+
random_fkey = box.get_any_face()
681+
assert sorted(box.face_attributes(random_fkey).keys()) == ['attr1', 'attr2']
682+
683+
box.face_attribute(random_fkey, "attr3", "value3")
684+
assert sorted(box.face_attributes(random_fkey).keys()) == ['attr1', 'attr2', 'attr3']
685+
686+
assert box.face_attribute(random_fkey, "attr3") == 'value3'

0 commit comments

Comments
 (0)