Data

class compas.data.Data(name=None)[source]

Bases: object

Abstract base class for all COMPAS data objects.

Parameters

name (str, optional) – The name of the object.

Attributes
  • dtype (str, read-only) – The type of the object in the form of a fully qualified module name and a class name, separated by a forward slash (“/”). For example: "compas.datastructures/Mesh".

  • data (dict) – The representation of the object as a dictionary containing only built-in Python data types. The structure of the dict is described by the data schema.

  • jsonstring (str, read-only) – The object’s data dict in JSON string format.

  • guid (str, read-only) – The globally unique identifier of the object. The guid is generated with uuid.uuid4().

  • name (str) – The name of the object. This name is not necessarily unique and can be set by the user. The default value is the object’s class name: self.__class__.__name__.

Notes

Objects created from classes that implement this data class can be serialized to JSON and unserialized without loss of information using:

To implement this data class, it is sufficient for the deriving class to define the “getter” and “setter” of the data property: compas.data.Data.data.

Examples

>>> from compas.data import Data
>>> class Point(Data):
...     def __init__(self, x, y, z):
...         super().__init__()
...         self.x = x
...         self.y = y
...         self.z = z
...     @property
...     def data(self):
...         return {'x': self.x, 'y': self.y, 'z': self.z}
...     @data.setter
...     def data(self, data):
...         self.x = data['x']
...         self.y = data['y']
...         self.z = data['z']
...
>>> a = Point(1.0, 0.0, 0.0)
>>> a.guid                 
UUID('1ddad2fe-6716-4e30-a5ae-8ed7cad892c4')
>>> a.name
'Point'
>>> a.data
{'x': 1.0, 'y': 0.0, 'z': 0.0}
>>> from compas.data import json_dumps, json_loads
>>> s = json_dumps(a)
>>> b = json_loads(s)                                     
>>> a is b                                                
False
>>> a == b                                                
True

Methods

copy

Make an independent copy of the data object.

from_data

Construct an object of this type from the provided data.

from_json

Construct an object from serialized data contained in a JSON file.

from_jsonstring

Construct an object from serialized data contained in a JSON string.

sha256

Compute a hash of the data for comparison during version control using the sha256 algorithm.

to_data

Convert an object to its native data representation.

to_json

Serialize the data representation of an object to a JSON file.

to_jsonstring

Serialize the data representation of an object to a JSON string.

validate_data

Validate the object's data against its data schema.

validate_json

Validate the object's data against its json schema.