Skip to content

Commit 18c1b46

Browse files
authored
Merge pull request #1260 from compas-dev/rogue-customization
No more rogue customization
2 parents e246434 + 372df17 commit 18c1b46

File tree

15 files changed

+111
-3336
lines changed

15 files changed

+111
-3336
lines changed

CHANGELOG.md

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

1212
* Added `compas.dtastructures.Network` as alias of `compas.datastructures.Graph`.
13+
* Added `compas.data.Data.name` and included it in serialisation in case `compas.data.Data._name is not None`.
1314

1415
### Changed
1516

@@ -22,6 +23,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2223
* Removed `compas.datastructures.Network`.
2324
* Removed `compas.datastructures.Halfedge`.
2425
* Removed `compas.datastructures.Halfface`.
26+
* Removed `compas.data.Data.attributes`.
27+
* Removed `compas.data.Datastructure.attributes`.
28+
* Removed `attributes` from `compas.datastructures.Assembly.data`.
29+
* Removed `attributes` from `compas.datastructures.CellNetwork.data`.
30+
* Removed `attributes` from `compas.datastructures.Graph.data`.
31+
* Removed `attributes` from `compas.datastructures.Mesh.data`.
32+
* Removed `attributes` from `compas.datastructures.Tree.data`.
33+
* Removed `attributes` from `compas.datastructures.VolMesh.data`.
2534

2635
## [2.0.0-beta.2] 2024-01-12
2736

src/compas/data/data.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ class Data(object):
3939
4040
Attributes
4141
----------
42-
dtype : str, read-only
43-
The type of the object in the form of a fully qualified module name and a class name, separated by a forward slash ("/").
44-
For example: ``"compas.datastructures/Mesh"``.
45-
data : dict
46-
The representation of the object as a dictionary containing only built-in Python data types.
47-
The structure of the dict is described by the data schema.
4842
guid : str, read-only
4943
The globally unique identifier of the object.
5044
The guid is generated with ``uuid.uuid4()``.
@@ -72,9 +66,9 @@ class Data(object):
7266

7367
DATASCHEMA = {}
7468

75-
def __init__(self, name=None, **kwargs):
69+
def __init__(self, name=None):
7670
self._guid = None
77-
self.attributes = kwargs or {}
71+
self._name = None
7872
if name:
7973
self.name = name
8074

@@ -95,15 +89,15 @@ def __jsondump__(self, minimal=False):
9589
"dtype": self.dtype,
9690
"data": self.data,
9791
}
98-
if self.attributes:
99-
state["attrs"] = self.attributes
10092
if minimal:
10193
return state
94+
if self._name is not None:
95+
state["name"] = self._name
10296
state["guid"] = str(self.guid)
10397
return state
10498

10599
@classmethod
106-
def __jsonload__(cls, data, guid=None, attrs=None):
100+
def __jsonload__(cls, data, guid=None, name=None):
107101
"""Construct an object of this type from the provided data to support COMPAS JSON serialization.
108102
109103
Parameters
@@ -112,8 +106,8 @@ def __jsonload__(cls, data, guid=None, attrs=None):
112106
The raw Python data representing the object.
113107
guid : str, optional
114108
The GUID of the object.
115-
attrs : dict, optional
116-
The additional attributes of the object.
109+
name : str, optional
110+
The name of the object.
117111
118112
Returns
119113
-------
@@ -123,8 +117,8 @@ def __jsonload__(cls, data, guid=None, attrs=None):
123117
obj = cls.from_data(data)
124118
if guid is not None:
125119
obj._guid = UUID(guid)
126-
if attrs is not None:
127-
obj.attributes.update(attrs)
120+
if name is not None:
121+
obj.name = name
128122
return obj
129123

130124
def __getstate__(self):
@@ -136,9 +130,8 @@ def __setstate__(self, state):
136130
self.__dict__.update(state["__dict__"])
137131
if "guid" in state:
138132
self._guid = UUID(state["guid"])
139-
# could be that this is already taken care of by the first line
140-
if "attrs" in state:
141-
self.attributes.update(state["attrs"])
133+
if "name" in state:
134+
self.name = state["name"]
142135

143136
@property
144137
def dtype(self):
@@ -170,11 +163,11 @@ def guid(self):
170163

171164
@property
172165
def name(self):
173-
return self.attributes.get("name") or self.__class__.__name__
166+
return self._name or self.__class__.__name__
174167

175168
@name.setter
176169
def name(self, name):
177-
self.attributes["name"] = name
170+
self._name = name
178171

179172
@classmethod
180173
def from_data(cls, data): # type: (dict) -> Data
@@ -303,7 +296,7 @@ def copy(self, cls=None): # type: (...) -> D
303296
if not cls:
304297
cls = type(self)
305298
obj = cls.from_data(deepcopy(self.data))
306-
obj.attributes = deepcopy(self.attributes)
299+
obj.name = self.name
307300
return obj # type: ignore
308301

309302
def sha256(self, as_string=False):

src/compas/data/encoders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ def object_hook(self, o):
231231

232232
data = o["data"]
233233
guid = o.get("guid")
234-
attrs = o.get("attrs")
234+
name = o.get("name")
235235

236236
# Kick-off from_data from a rebuilt Python dictionary instead of the C# data type
237237
if IDictionary and isinstance(o, IDictionary[str, object]):
238238
data = {key: data[key] for key in data.Keys}
239239

240-
obj = cls.__jsonload__(data, guid, attrs)
240+
obj = cls.__jsonload__(data, guid=guid, name=name)
241241

242242
return obj

src/compas/datastructures/assembly/assembly.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class Assembly(Datastructure):
1717
1818
Attributes
1919
----------
20-
attributes : dict[str, Any]
21-
General attributes of the data structure that will be included in the data dict and serialization.
2220
graph : :class:`compas.datastructures.Graph`
2321
The graph that is used under the hood to store the parts and their connections.
2422
@@ -33,16 +31,13 @@ class Assembly(Datastructure):
3331
DATASCHEMA = {
3432
"type": "object",
3533
"properties": {
36-
"attributes": {"type": "object"},
3734
"graph": Graph.DATASCHEMA,
3835
},
3936
"required": ["graph"],
4037
}
4138

42-
def __init__(self, name=None, **kwargs):
43-
super(Assembly, self).__init__()
44-
self.attributes = {"name": name or "Assembly"}
45-
self.attributes.update(kwargs)
39+
def __init__(self, name=None):
40+
super(Assembly, self).__init__(name=name)
4641
self.graph = Graph()
4742
self._parts = {}
4843

@@ -57,30 +52,16 @@ def __str__(self):
5752
@property
5853
def data(self):
5954
return {
60-
"attributes": self.attributes,
6155
"graph": self.graph.data,
6256
}
6357

6458
@classmethod
6559
def from_data(cls, data):
6660
assembly = cls()
67-
assembly.attributes.update(data["attributes"] or {})
6861
assembly.graph = Graph.from_data(data["graph"])
6962
assembly._parts = {part.guid: part.key for part in assembly.parts()} # type: ignore
7063
return assembly
7164

72-
# ==========================================================================
73-
# Properties
74-
# ==========================================================================
75-
76-
@property
77-
def name(self):
78-
return self.attributes.get("name") or self.__class__.__name__
79-
80-
@name.setter
81-
def name(self, value):
82-
self.attributes["name"] = value
83-
8465
# ==========================================================================
8566
# Constructors
8667
# ==========================================================================

src/compas/datastructures/cell_network/cell_network.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import division
33
from __future__ import print_function
44

5-
# from itertools import product
65
from random import sample
76

87
from compas.datastructures import Mesh
@@ -28,18 +27,13 @@
2827
from compas.geometry import normal_polygon
2928
from compas.geometry import normalize_vector
3029
from compas.geometry import volume_polyhedron
31-
32-
# from compas.topology import face_adjacency
3330
from compas.geometry import add_vectors
3431
from compas.geometry import bestfit_plane
3532
from compas.geometry import project_point_plane
3633
from compas.geometry import scale_vector
3734
from compas.geometry import subtract_vectors
3835
from compas.geometry import bounding_box
3936

40-
# from compas.geometry import transform_points
41-
42-
# from compas.utilities import linspace
4337
from compas.utilities import pairwise
4438

4539
from compas.tolerance import TOL
@@ -58,13 +52,11 @@ class CellNetwork(Datastructure):
5852
Default values for face attributes.
5953
default_cell_attributes: dict, optional
6054
Default values for cell attributes.
61-
**kwargs : dict, optional
62-
Additional attributes for the data structure itself.
55+
name : str, optional
56+
The name of the cell network.
6357
6458
Attributes
6559
----------
66-
attributes : dict[str, Any]
67-
General attributes of the data structure which will be included in the data representation.
6860
default_vertex_attributes : dict[str, Any]
6961
Default attributes of the vertices.
7062
default_edge_attributes: dict[str, Any]
@@ -100,6 +92,28 @@ class CellNetwork(Datastructure):
10092
"patternProperties": {"^[0-9]+$": {"type": "object"}},
10193
"additionalProperties": False,
10294
},
95+
"edge": {
96+
"type": "object",
97+
"patternProperties": {
98+
"^[0-9]+$": {
99+
"type": "object",
100+
"patternProperties": {"^[0-9]+$": {"type": "object"}},
101+
"additionalProperties": False,
102+
}
103+
},
104+
"additionalProperties": False,
105+
},
106+
"face": {
107+
"type": "object",
108+
"patternProperties": {
109+
"^[0-9]+$": {
110+
"type": "array",
111+
"items": {"type": "integer", "minimum": 0},
112+
"minItems": 3,
113+
}
114+
},
115+
"additionalProperties": False,
116+
},
103117
"cell": {
104118
"type": "object",
105119
"patternProperties": {
@@ -115,11 +129,6 @@ class CellNetwork(Datastructure):
115129
},
116130
"additionalProperties": False,
117131
},
118-
"edge_data": {
119-
"type": "object",
120-
"patternProperties": {"^\\([0-9]+, [0-9]+\\)$": {"type": "object"}},
121-
"additionalProperties": False,
122-
},
123132
"face_data": {
124133
"type": "object",
125134
"patternProperties": {"^\\([0-9]+(, [0-9]+){3, }\\)$": {"type": "object"}},
@@ -140,8 +149,9 @@ class CellNetwork(Datastructure):
140149
"dfa",
141150
"dca",
142151
"vertex",
152+
"edge",
153+
"face",
143154
"cell",
144-
"edge_data",
145155
"face_data",
146156
"cell_data",
147157
"max_vertex",
@@ -156,9 +166,9 @@ def __init__(
156166
default_edge_attributes=None,
157167
default_face_attributes=None,
158168
default_cell_attributes=None,
159-
**kwargs
169+
name=None,
160170
):
161-
super(Datastructure, self).__init__(**kwargs)
171+
super(CellNetwork, self).__init__(name=name)
162172
self._max_vertex = -1
163173
self._max_face = -1
164174
self._max_cell = -1
@@ -220,7 +230,6 @@ def data(self):
220230
cell[c] = sorted(list(faces))
221231

222232
return {
223-
"attributes": self.attributes,
224233
"dva": self.default_vertex_attributes,
225234
"dea": self.default_edge_attributes,
226235
"dfa": self.default_face_attributes,
@@ -250,12 +259,10 @@ def from_data(cls, data):
250259
default_cell_attributes=dca,
251260
)
252261

253-
cell_network.attributes.update(data.get("attributes") or {})
254-
255-
vertex = data.get("vertex") or {}
256-
edge = data.get("edge") or {}
257-
face = data.get("face") or {}
258-
cell = data.get("cell") or {}
262+
vertex = data["vertex"] or {}
263+
edge = data["edge"] or {}
264+
face = data["face"] or {}
265+
cell = data["cell"] or {}
259266

260267
for key, attr in iter(vertex.items()):
261268
cell_network.add_vertex(key=key, attr_dict=attr)

0 commit comments

Comments
 (0)