Skip to content

Commit bce3fbb

Browse files
authored
Merge pull request #878 from compas-dev/ds-datakey-generation-fix
Use str representation of sorted tuples to be compatible with literal eval
2 parents f864b24 + f2b9d8c commit bce3fbb

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
* Fixed data schema of `compas.geometry.Polyline`, `compas.geometry.Polygon`, `compas.geometry.Pointcloud`.
2424
* Fixed `Configuration.from_data` to be backward-compatible with JSON data generated before `compas 1.3.0`.
2525
* Changed `compas_rhino.drawing.draw_breps` to assume provided polygon is closed and automatically add missing corner to polycurve constructor.
26+
* Changed conversion of edges and faces to uniques keys for the data dicts to use the string representation of a sorted tuple of identifiers.
2627
* Added `dtype` to JSON decoding error message.
2728

2829
### Removed

src/compas/datastructures/mesh/core/halfedge.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ def edge_attribute(self, edge, name, value=None):
12781278
u, v = edge
12791279
if u not in self.halfedge or v not in self.halfedge[u]:
12801280
raise KeyError(edge)
1281-
key = "-".join(map(str, sorted(edge)))
1281+
key = str(tuple(sorted(edge)))
12821282
if value is not None:
12831283
if key not in self.edgedata:
12841284
self.edgedata[key] = {}
@@ -1312,7 +1312,7 @@ def unset_edge_attribute(self, edge, name):
13121312
u, v = edge
13131313
if u not in self.halfedge or v not in self.halfedge[u]:
13141314
raise KeyError(edge)
1315-
key = "-".join(map(str, sorted(edge)))
1315+
key = str(tuple(sorted(edge)))
13161316
if key in self.edgedata and name in self.edgedata[key]:
13171317
del self.edgedata[key][name]
13181318

@@ -1352,7 +1352,7 @@ def edge_attributes(self, edge, names=None, values=None):
13521352
return
13531353
# use it as a getter
13541354
if not names:
1355-
key = "-".join(map(str, sorted(edge)))
1355+
key = str(tuple(sorted(edge)))
13561356
# get the entire attribute dict
13571357
return EdgeAttributeView(self.default_edge_attributes, self.edgedata.setdefault(key, {}))
13581358
# get only the values of the named attributes

src/compas/datastructures/volmesh/core/halfface.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ def edge_attribute(self, edge, name, value=None):
900900
u, v = edge
901901
if u not in self._plane or v not in self._plane[u]:
902902
raise KeyError(edge)
903-
key = "-".join(map(str, sorted(edge)))
903+
key = str(tuple(sorted(edge)))
904904
if value is not None:
905905
if key not in self._edge_data:
906906
self._edge_data[key] = {}
@@ -934,7 +934,7 @@ def unset_edge_attribute(self, edge, name):
934934
u, v = edge
935935
if u not in self._plane or v not in self._plane[u]:
936936
raise KeyError(edge)
937-
key = "-".join(map(str, sorted(edge)))
937+
key = str(tuple(sorted(edge)))
938938
if key in self._edge_data and name in self._edge_data[key]:
939939
del self._edge_data[key][name]
940940

@@ -967,7 +967,7 @@ def edge_attributes(self, edge, names=None, values=None):
967967
u, v = edge
968968
if u not in self._plane or v not in self._plane[u]:
969969
raise KeyError(edge)
970-
key = "-".join(map(str, sorted(edge)))
970+
key = str(tuple(sorted(edge)))
971971
if values:
972972
for name, value in zip(names, values):
973973
if key not in self._edge_data:
@@ -1097,7 +1097,7 @@ def face_attribute(self, face, name, value=None):
10971097
"""
10981098
if face not in self._halfface:
10991099
raise KeyError(face)
1100-
key = "-".join(map(str, sorted(self.halfface_vertices(face))))
1100+
key = str(tuple(sorted(self.halfface_vertices(face))))
11011101
if value is not None:
11021102
if key not in self._face_data:
11031103
self._face_data[key] = {}
@@ -1130,7 +1130,7 @@ def unset_face_attribute(self, face, name):
11301130
"""
11311131
if face not in self._halfface:
11321132
raise KeyError(face)
1133-
key = "-".join(map(str, sorted(self.halfface_vertices(face))))
1133+
key = str(tuple(sorted(self.halfface_vertices(face))))
11341134
if key in self._face_data and name in self._face_data[key]:
11351135
del self._face_data[key][name]
11361136

@@ -1162,7 +1162,7 @@ def face_attributes(self, face, names=None, values=None):
11621162
"""
11631163
if face not in self._halfface:
11641164
raise KeyError(face)
1165-
key = "-".join(map(str, sorted(self.halfface_vertices(face))))
1165+
key = str(tuple(sorted(self.halfface_vertices(face))))
11661166
if values:
11671167
for name, value in zip(names, values):
11681168
if key not in self._face_data:

tests/compas/datastructures/test_graph.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ def graph():
2323
# ==============================================================================
2424

2525

26+
# def test_edgedata_directionality(graph):
27+
# graph.update_default_edge_attributes({'index': 0})
28+
# for index, (u, v) in enumerate(graph.edges()):
29+
# graph.edge_attribute((u, v), 'index', index)
30+
# assert all(graph.edge_attribute((u, v), 'index') != graph.edge_attribute((v, u), 'index') for u, v in graph.edges())
31+
32+
33+
def test_edgedata_io(graph):
34+
graph.update_default_edge_attributes({'index': 0})
35+
for index, (u, v) in enumerate(graph.edges()):
36+
graph.edge_attribute((u, v), 'index', index)
37+
other = Graph.from_data(graph.data)
38+
assert all(other.edge_attribute(edge, 'index') == index for index, edge in enumerate(other.edges()))
39+
40+
2641
def test_data_schema(graph):
2742
if compas.IPY:
2843
return

tests/compas/datastructures/test_halfedge.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ def edge_key():
4040
# ==============================================================================
4141

4242

43+
def test_edgedata_nondirectionality(mesh):
44+
mesh.update_default_edge_attributes({'index': 0})
45+
for index, (u, v) in enumerate(mesh.edges()):
46+
mesh.edge_attribute((u, v), 'index', index)
47+
assert all(mesh.edge_attribute((u, v), 'index') == mesh.edge_attribute((v, u), 'index') for u, v in mesh.edges())
48+
49+
50+
def test_edgedata_io(mesh):
51+
mesh.update_default_edge_attributes({'index': 0})
52+
for index, (u, v) in enumerate(mesh.edges()):
53+
mesh.edge_attribute((u, v), 'index', index)
54+
other = HalfEdge.from_data(mesh.data)
55+
assert all(other.edge_attribute(edge, 'index') == index for index, edge in enumerate(other.edges()))
56+
57+
4358
def test_data_schema(mesh):
4459
if not compas.IPY:
4560
mesh.validate_data()

0 commit comments

Comments
 (0)