Skip to content

Commit 5aa5559

Browse files
committed
only name attribute for most objects
1 parent e246434 commit 5aa5559

File tree

11 files changed

+86
-60
lines changed

11 files changed

+86
-60
lines changed

src/compas/data/data.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ class Data(object):
7272

7373
DATASCHEMA = {}
7474

75-
def __init__(self, name=None, **kwargs):
75+
def __init__(self, name=None):
7676
self._guid = None
77-
self.attributes = kwargs or {}
77+
self._name = None
7878
if name:
7979
self.name = name
8080

@@ -95,15 +95,14 @@ def __jsondump__(self, minimal=False):
9595
"dtype": self.dtype,
9696
"data": self.data,
9797
}
98-
if self.attributes:
99-
state["attrs"] = self.attributes
10098
if minimal:
10199
return state
100+
state["name"] = self.name
102101
state["guid"] = str(self.guid)
103102
return state
104103

105104
@classmethod
106-
def __jsonload__(cls, data, guid=None, attrs=None):
105+
def __jsonload__(cls, data, guid=None, name=None):
107106
"""Construct an object of this type from the provided data to support COMPAS JSON serialization.
108107
109108
Parameters
@@ -112,8 +111,8 @@ def __jsonload__(cls, data, guid=None, attrs=None):
112111
The raw Python data representing the object.
113112
guid : str, optional
114113
The GUID of the object.
115-
attrs : dict, optional
116-
The additional attributes of the object.
114+
name : str, optional
115+
The name of the object.
117116
118117
Returns
119118
-------
@@ -123,8 +122,8 @@ def __jsonload__(cls, data, guid=None, attrs=None):
123122
obj = cls.from_data(data)
124123
if guid is not None:
125124
obj._guid = UUID(guid)
126-
if attrs is not None:
127-
obj.attributes.update(attrs)
125+
if name is not None:
126+
obj.name = name
128127
return obj
129128

130129
def __getstate__(self):
@@ -136,9 +135,8 @@ def __setstate__(self, state):
136135
self.__dict__.update(state["__dict__"])
137136
if "guid" in state:
138137
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"])
138+
if "name" in state:
139+
self.name = state["name"]
142140

143141
@property
144142
def dtype(self):
@@ -170,11 +168,11 @@ def guid(self):
170168

171169
@property
172170
def name(self):
173-
return self.attributes.get("name") or self.__class__.__name__
171+
return self._name or self.__class__.__name__
174172

175173
@name.setter
176174
def name(self, name):
177-
self.attributes["name"] = name
175+
self._name = name
178176

179177
@classmethod
180178
def from_data(cls, data): # type: (dict) -> Data
@@ -303,7 +301,7 @@ def copy(self, cls=None): # type: (...) -> D
303301
if not cls:
304302
cls = type(self)
305303
obj = cls.from_data(deepcopy(self.data))
306-
obj.attributes = deepcopy(self.attributes)
304+
obj.name = self.name
307305
return obj # type: ignore
308306

309307
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, name)
241241

242242
return obj

src/compas/datastructures/cell_network/cell_network.py

Lines changed: 33 additions & 19 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
@@ -91,6 +85,7 @@ class CellNetwork(Datastructure):
9185
DATASCHEMA = {
9286
"type": "object",
9387
"properties": {
88+
"attributes": {"type": "object"},
9489
"dva": {"type": "object"},
9590
"dea": {"type": "object"},
9691
"dfa": {"type": "object"},
@@ -100,6 +95,28 @@ class CellNetwork(Datastructure):
10095
"patternProperties": {"^[0-9]+$": {"type": "object"}},
10196
"additionalProperties": False,
10297
},
98+
"edge": {
99+
"type": "object",
100+
"patternProperties": {
101+
"^[0-9]+$": {
102+
"type": "object",
103+
"patternProperties": {"^[0-9]+$": {"type": "object"}},
104+
"additionalProperties": False,
105+
}
106+
},
107+
"additionalProperties": False,
108+
},
109+
"face": {
110+
"type": "object",
111+
"patternProperties": {
112+
"^[0-9]+$": {
113+
"type": "array",
114+
"items": {"type": "integer", "minimum": 0},
115+
"minItems": 3,
116+
}
117+
},
118+
"additionalProperties": False,
119+
},
103120
"cell": {
104121
"type": "object",
105122
"patternProperties": {
@@ -115,11 +132,6 @@ class CellNetwork(Datastructure):
115132
},
116133
"additionalProperties": False,
117134
},
118-
"edge_data": {
119-
"type": "object",
120-
"patternProperties": {"^\\([0-9]+, [0-9]+\\)$": {"type": "object"}},
121-
"additionalProperties": False,
122-
},
123135
"face_data": {
124136
"type": "object",
125137
"patternProperties": {"^\\([0-9]+(, [0-9]+){3, }\\)$": {"type": "object"}},
@@ -140,8 +152,9 @@ class CellNetwork(Datastructure):
140152
"dfa",
141153
"dca",
142154
"vertex",
155+
"edge",
156+
"face",
143157
"cell",
144-
"edge_data",
145158
"face_data",
146159
"cell_data",
147160
"max_vertex",
@@ -158,7 +171,7 @@ def __init__(
158171
default_cell_attributes=None,
159172
**kwargs
160173
):
161-
super(Datastructure, self).__init__(**kwargs)
174+
super(CellNetwork, self).__init__(**kwargs)
162175
self._max_vertex = -1
163176
self._max_face = -1
164177
self._max_cell = -1
@@ -243,19 +256,20 @@ def from_data(cls, data):
243256
dfa = data.get("dfa") or {}
244257
dca = data.get("dca") or {}
245258

259+
attributes = data.get("attributes") or {}
260+
246261
cell_network = cls(
247262
default_vertex_attributes=dva,
248263
default_edge_attributes=dea,
249264
default_face_attributes=dfa,
250265
default_cell_attributes=dca,
266+
**attributes
251267
)
252268

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 {}
269+
vertex = data["vertex"] or {}
270+
edge = data["edge"] or {}
271+
face = data["face"] or {}
272+
cell = data["cell"] or {}
259273

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

src/compas/datastructures/datastructure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
class Datastructure(Data):
99
"""Base class for all data structures."""
1010

11+
def __init__(self, **kwargs):
12+
super(Datastructure, self).__init__(**kwargs)
13+
self.attributes = kwargs
14+
1115
def transform(self, transformation):
1216
"""Transforms the data structure.
1317

src/compas/datastructures/graph/graph.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ class Graph(Datastructure):
5858
default_edge_attributes: dict, optional
5959
Default values for edge attributes.
6060
**kwargs : dict, optional
61-
Additional attributes to add to the graph.
61+
Additional attributes.
6262
6363
Attributes
6464
----------
65+
attributes : dict[str, Any]
66+
General attributes of the data structure which will be included in the data representation.
6567
default_node_attributes : dict[str, Any]
6668
dictionary containing default values for the attributes of nodes.
6769
It is recommended to add a default to this dictionary using :meth:`update_default_node_attributes`
@@ -76,6 +78,7 @@ class Graph(Datastructure):
7678
DATASCHEMA = {
7779
"type": "object",
7880
"properties": {
81+
"attributes": {"type": "object"},
7982
"dna": {"type": "object"},
8083
"dea": {"type": "object"},
8184
"node": {
@@ -138,6 +141,7 @@ def __str__(self):
138141
@property
139142
def data(self):
140143
data = {
144+
"attributes": self.attributes,
141145
"dna": self.default_node_attributes,
142146
"dea": self.default_edge_attributes,
143147
"node": {},
@@ -158,10 +162,12 @@ def data(self):
158162
def from_data(cls, data):
159163
dna = data.get("dna") or {}
160164
dea = data.get("dea") or {}
161-
node = data.get("node") or {}
162-
edge = data.get("edge") or {}
165+
attributes = data.get("attributes") or {}
163166

164-
graph = cls(default_node_attributes=dna, default_edge_attributes=dea)
167+
node = data["node"] or {}
168+
edge = data["edge"] or {}
169+
170+
graph = cls(default_node_attributes=dna, default_edge_attributes=dea, **attributes)
165171

166172
for node, attr in iter(node.items()):
167173
node = literal_eval(node)
@@ -227,6 +233,7 @@ def from_networkx(cls, graph):
227233
228234
"""
229235
g = cls()
236+
g.name = graph.graph.get("name")
230237
g.attributes.update(graph.graph)
231238

232239
for node in graph.nodes():
@@ -473,6 +480,7 @@ def to_networkx(self):
473480
import networkx as nx
474481

475482
G = nx.DiGraph()
483+
G.graph["name"] = self.name # type: ignore
476484
G.graph.update(self.attributes) # type: ignore
477485

478486
for node, attr in self.nodes(data=True):

src/compas/datastructures/mesh/mesh.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ class Mesh(Datastructure):
9191
default_face_attributes: dict[str, Any], optional
9292
Default values for face attributes.
9393
**kwargs : dict, optional
94-
Additional attributes to add to the mesh object.
94+
Additional attributes to add to the mesh.
9595
9696
Attributes
9797
----------
9898
attributes : dict[str, Any]
99-
General attributes of the data structure that are included in the data representation and serialization.
99+
General attributes of the data structure which will be included in the data representation.
100100
default_vertex_attributes : dict[str, Any]
101101
Dictionary containing default values for the attributes of vertices.
102102
It is recommended to add a default to this dictionary using :meth:`update_default_vertex_attributes`
@@ -125,6 +125,7 @@ class Mesh(Datastructure):
125125
DATASCHEMA = {
126126
"type": "object",
127127
"properties": {
128+
"attributes": {"type": "object"},
128129
"dva": {"type": "object"},
129130
"dea": {"type": "object"},
130131
"dfa": {"type": "object"},
@@ -218,6 +219,7 @@ def __str__(self):
218219
@property
219220
def data(self):
220221
return {
222+
"attributes": self.attributes,
221223
"dva": self.default_vertex_attributes,
222224
"dea": self.default_edge_attributes,
223225
"dfa": self.default_face_attributes,
@@ -234,11 +236,13 @@ def from_data(cls, data):
234236
dva = data.get("dva") or {}
235237
dfa = data.get("dfa") or {}
236238
dea = data.get("dea") or {}
239+
attributes = data.get("attributes") or {}
237240

238-
halfedge = cls(default_vertex_attributes=dva, default_face_attributes=dfa, default_edge_attributes=dea)
241+
halfedge = cls(default_vertex_attributes=dva, default_face_attributes=dfa, default_edge_attributes=dea, **attributes)
242+
243+
vertex = data["vertex"] or {}
244+
face = data["face"] or {}
239245

240-
vertex = data.get("vertex") or {}
241-
face = data.get("face") or {}
242246
facedata = data.get("facedata") or {}
243247
edgedata = data.get("edgedata") or {}
244248

src/compas/datastructures/tree/tree.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TreeNode(Data):
4949

5050
def __init__(self, **kwargs):
5151
super(TreeNode, self).__init__(**kwargs)
52+
self.attributes = kwargs
5253
self._parent = None
5354
self._children = []
5455
self._tree = None
@@ -86,11 +87,6 @@ def tree(self):
8687
@property
8788
def data(self):
8889
return {
89-
# this duplicates the behaviour of the Data class
90-
# but it is necessary to make the tree serializable
91-
# however, it only duplicates the attributes if the node is serialized independently
92-
# perhaps this should not be possible
93-
# in this sense, the node is a prototype of an independen graph node, or mesh vertex
9490
"attributes": self.attributes,
9591
"children": [child.data for child in self.children],
9692
}

0 commit comments

Comments
 (0)