|
1 | 1 | Implementing a New Data Type |
2 | 2 | ============================ |
| 3 | + |
| 4 | +COMPAS data types are classes that are based on :class:`compas.data.Data`. |
| 5 | + |
| 6 | +Data types can be serialized to JSON with |
| 7 | + |
| 8 | +* :func:`compas.json_dump` |
| 9 | +* :func:`compas.json_dumps` |
| 10 | +* :func:`compas.json_dumpz` |
| 11 | + |
| 12 | +and deserialized with the corresponding "load" functions |
| 13 | + |
| 14 | +* :func:`compas.json_load` |
| 15 | +* :func:`compas.json_loads` |
| 16 | +* :func:`compas.json_loadz` |
| 17 | + |
| 18 | +All geometry objects and data structures, |
| 19 | +and also, for example, the visualization scene, |
| 20 | +are serializable data types. |
| 21 | + |
| 22 | + |
| 23 | +Creating a new data type |
| 24 | +======================== |
| 25 | + |
| 26 | +In most cases, it is sufficient to implement the ``__data__`` property when creating your custom `Data` class. |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + class SomeThing(Data): |
| 31 | +
|
| 32 | + def __init__(self, a, b) |
| 33 | + super().__init__() |
| 34 | + # note that if the code needs to be compatible with IronPython |
| 35 | + # you should write the following: |
| 36 | + # super(SomeThing, self).__init__() |
| 37 | + self.a = a |
| 38 | + self.b = b |
| 39 | +
|
| 40 | + @property |
| 41 | + def __data__(self): |
| 42 | + return { |
| 43 | + "a": self.a, |
| 44 | + "b": self.b, |
| 45 | + } |
| 46 | +
|
| 47 | +
|
| 48 | +>>> custom = SomeThing(a=1, b=2) |
| 49 | +>>> compas.json_dump(custom, "custom.json") |
| 50 | +>>> result = compas.json_load("custom.json") |
| 51 | +>>> isinstance(result, SomeThing) |
| 52 | +True |
| 53 | +>>> result.a |
| 54 | +1 |
| 55 | +>>> result.b |
| 56 | +2 |
| 57 | + |
| 58 | +If the attributes stored in the data dictionary defined by the ``__data__`` property |
| 59 | +are different from the initialization parameters of the class, |
| 60 | +you must also customize the ``__from_data__`` class method to compensate for the difference. |
| 61 | + |
| 62 | +.. code-block:: python |
| 63 | +
|
| 64 | + class SomeThing(Data): |
| 65 | +
|
| 66 | + def __init__(self) |
| 67 | + super().__init__() |
| 68 | + # note that if the code needs to be compatible with IronPython |
| 69 | + # you should write the following: |
| 70 | + # super(SomeThing, self).__init__() |
| 71 | + self.items = [] |
| 72 | +
|
| 73 | + @property |
| 74 | + def __data__(self): |
| 75 | + return { |
| 76 | + "items": self.items, |
| 77 | + } |
| 78 | +
|
| 79 | + @classmethod |
| 80 | + def __from_data__(cls, data): |
| 81 | + custom = cls() |
| 82 | + for item in data['items']: |
| 83 | + custom.add(item) |
| 84 | + return custom |
| 85 | +
|
| 86 | + def add(self, item): |
| 87 | + self.items.append(item) |
| 88 | +
|
| 89 | +
|
| 90 | +>>> custom = SomeThing() |
| 91 | +>>> custom.add(1) |
| 92 | +>>> custom.add(2) |
| 93 | +>>> compas.json_dump(custom, "custom.json") |
| 94 | +>>> result = compas.json_load("custom.json") |
| 95 | +>>> isinstance(result, SomeThing) |
| 96 | +True |
| 97 | +>>> result.items |
| 98 | +[1, 2] |
| 99 | + |
| 100 | + |
| 101 | +Attribute types |
| 102 | +=============== |
| 103 | + |
| 104 | +Any attribute that is an instance of a Python base type or a serializable COMPAS data object |
| 105 | +can be included in the data dict created by the ``__data__`` property without further processing. |
| 106 | +The serialization process will recursively serialize all these attributes. |
| 107 | + |
| 108 | +.. code-block:: python |
| 109 | +
|
| 110 | + class SomeThing(Data): |
| 111 | +
|
| 112 | + def __init__(self, point, frame, mesh): |
| 113 | + super().__init__() |
| 114 | + # note that if the code needs to be compatible with IronPython |
| 115 | + # you should write the following: |
| 116 | + # super(SomeThing, self).__init__() |
| 117 | + self.point = point |
| 118 | + self.frame = frame |
| 119 | + self.mesh = mesh |
| 120 | +
|
| 121 | + @property |
| 122 | + def __data__(self): |
| 123 | + return { |
| 124 | + "point": self.point, |
| 125 | + "frame": self.frame, |
| 126 | + "mesh": self.mesh, |
| 127 | + } |
| 128 | +
|
| 129 | +
|
| 130 | +>>> import compas |
| 131 | +>>> from compas.geometry import Point, Frame |
| 132 | +>>> from compas.datastructures import Mesh |
| 133 | +>>> point = Point(1, 2, 3) |
| 134 | +>>> frame = Frame() |
| 135 | +>>> mesh = Mesh.from_meshgrid(10, 10) |
| 136 | +>>> custom = SomeThing(point, frame, mesh) |
| 137 | +>>> compas.json_dump(custom, "custom.json") |
| 138 | +>>> result = compas.json_load("custom.json") |
| 139 | +>>> isinstance(result.point, Point) |
| 140 | +True |
| 141 | +>>> isinstance(result.frame, Frame) |
| 142 | +True |
| 143 | +>>> isinstance(result.mesh, Mesh) |
| 144 | +True |
| 145 | +>>> result.point == point |
| 146 | +True |
| 147 | +>>> result.point is point |
| 148 | +False |
| 149 | + |
| 150 | + |
| 151 | +Note that the the automatic serialization process will incur overhead information |
| 152 | +that increases the size of the resulting JSON file. |
| 153 | +The performance impact may be significant when many of these instances are serialized. |
| 154 | + |
| 155 | +To avoid this, anticipated conversions can be included explicitly in `__data__` and `__from_data__`. |
| 156 | + |
| 157 | +.. code-block:: python |
| 158 | +
|
| 159 | + class SomeThing(Data): |
| 160 | +
|
| 161 | + def __init__(self, point, frame, mesh): |
| 162 | + super().__init__() |
| 163 | + # note that if the code needs to be compatible with IronPython |
| 164 | + # you should write the following: |
| 165 | + # super(SomeThing, self).__init__() |
| 166 | + self.point = point |
| 167 | + self.frame = frame |
| 168 | + self.mesh = mesh |
| 169 | +
|
| 170 | + @property |
| 171 | + def __data__(self): |
| 172 | + return { |
| 173 | + "point": self.point.__data__, |
| 174 | + "frame": self.frame.__data__, |
| 175 | + "mesh": self.mesh.__data__, |
| 176 | + } |
| 177 | +
|
| 178 | + @classmethod |
| 179 | + def __from_data__(cls, data): |
| 180 | + return cls( |
| 181 | + Point.__from_data__(data['point']), |
| 182 | + Frame.__from_data__(data['frame']), |
| 183 | + Mesh.__from_data__(data['mesh']), |
| 184 | + ) |
0 commit comments