Skip to content

Commit

Permalink
first draft of guide for creating new data type
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Oct 2, 2024
1 parent 1888213 commit 0eec165
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions docs/devguide/dtypes.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
Implementing a New Data Type
============================

COMPAS data types are classes that are based on :class:`compas.data.Data`.

Data types can be serialised to JSON with

* :func:`compas.json_dump`
* :func:`compas.json_dumps`
* :func:`compas.json_dumpz`

and deserialised with the corresponding "load" functions

* :func:`compas.json_load`
* :func:`compas.json_loads`
* :func:`compas.json_loadz`

All geometry objects and data structures,
and also, for example, the visualisation scene,
are data types.


Creating a new data type
========================

A new data type can be created in a few simple steps.

.. code-block:: python
class CustomData(Data):
def __init__(self, a=None, b=None, name=None)
super().__init__(name=name)
self.a = a
self.b = b
@property
def __data__(self):
data = super().__data__
data['a'] = self.a
data['b'] = self.b
return data
>>> data = CustomData(a=1, b=2)
>>> compas.json_dump(data, "custom.json")
>>> result = compas.json_load("custom.json")
>>> isinstance(result, CustomData)
True
>>> result.a
1
>>> result.b
2


Attribute types
===============

More info coming soon...

.. note::

Note that this process can be further simplified
by leveraging the ``__annotations__`` infrastructure of Python 3.
This will be introduced in the next major COMPAS release.


Extending an existing data type
===============================

More info coming soon...


Special Cases
=============

More info coming soon...

0 comments on commit 0eec165

Please sign in to comment.