Skip to content

Commit ff7344e

Browse files
committed
add tutorial
1 parent aa7a8f1 commit ff7344e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

docs/userguide/advanced.serialisation.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,50 @@ which will help with IntelliSense and code completion.
149149
boxes: List[Box] = session['boxes']
150150
151151
152+
Inheritance
153+
===========
154+
155+
When working with custom classes that inherit from COMPAS datastructures, COMPAS will encode the inheritance chain in the serialized data. This allows the object to be reconstructed using the closest available superclass if the custom class is not available in the environment where the data is loaded.
156+
For example, a user can create a custom mesh class and serialize it to JSON:
157+
158+
.. code-block:: python
159+
160+
from compas.datastructures import Mesh
161+
from compas import json_dump
162+
163+
class CustomMesh(Mesh):
164+
def __init__(self, *args, **kwargs):
165+
super(CustomMesh, self).__init__(*args, **kwargs)
166+
self.custom_mesh_attr = "custom_mesh"
167+
168+
@property
169+
def __data__(self):
170+
data = super(CustomMesh, self).__data__
171+
data["custom_mesh_attr"] = self.custom_mesh_attr
172+
return data
173+
174+
@classmethod
175+
def __from_data__(cls, data):
176+
obj = super(CustomMesh, cls).__from_data__(data)
177+
obj.custom_mesh_attr = data.get("custom_mesh_attr", "")
178+
return obj
179+
180+
# Create and serialize a custom mesh
181+
custom_mesh = CustomMesh(name="test")
182+
json_dump(custom_mesh, 'custom_mesh.json')
183+
184+
If another user loads "custom_mesh.json" in an environment where the CustomMesh class is not available, COMPAS will reconstruct the object as an instance of its closest available superclass, which in this case is the regular Mesh class:
185+
186+
.. code-block:: python
187+
188+
from compas.datastructures import Mesh
189+
from compas import json_load
190+
191+
mesh = json_load('custom_mesh.json')
192+
assert isinstance(mesh, Mesh) # This will be True
193+
194+
195+
152196
Validation
153197
==========
154198

tests/compas/datastructures/test_datastructure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __from_data__(cls, data):
108108
return CustomMesh(name="test")
109109

110110

111-
def test_mro_fallback(level2):
111+
def test_inheritance_fallback(level2):
112112
if compas.IPY:
113113
# IronPython is not able to deserialize a class that is defined in a local scope like Level1.
114114
# We skip this tests for IronPython.
@@ -132,7 +132,7 @@ def test_mro_fallback(level2):
132132
assert not hasattr(loaded, "level2_attr")
133133

134134

135-
def test_mro_fallback_multi_level(level3):
135+
def test_inheritance_fallback_multi_level(level3):
136136
if compas.IPY:
137137
# IronPython is not able to deserialize a class that is defined in a local scope like Level1.
138138
# We skip this tests for IronPython.

0 commit comments

Comments
 (0)