Skip to content

Commit b6a70a2

Browse files
committed
compas.data
1 parent 0cafe38 commit b6a70a2

7 files changed

Lines changed: 48 additions & 127 deletions

File tree

src/compas/data/__init__.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
This package defines the core infrastructure for data serialisation in the COMPAS framework.
33
It provides a base class for data objects, a JSON encoder and decoder, serialisers and deserialisers, and schema validation.
44
"""
5-
6-
from __future__ import absolute_import
5+
# ruff: noqa: F401
76

87
from .exceptions import DecoderError
98
from .encoders import DataEncoder
@@ -12,20 +11,3 @@
1211
from .json import json_load, json_loads, json_loadz, json_dump, json_dumps, json_dumpz
1312
from .schema import dataclass_dataschema, dataclass_typeschema, dataclass_jsonschema
1413
from .schema import compas_dataclasses
15-
16-
__all__ = [
17-
"Data",
18-
"DataEncoder",
19-
"DataDecoder",
20-
"DecoderError",
21-
"json_load",
22-
"json_loads",
23-
"json_loadz",
24-
"json_dump",
25-
"json_dumps",
26-
"json_dumpz",
27-
"dataclass_dataschema",
28-
"dataclass_typeschema",
29-
"dataclass_jsonschema",
30-
"compas_dataclasses",
31-
]

src/compas/data/coercion.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
51
from .validators import is_item_iterable
62

73

src/compas/data/data.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
5-
try:
6-
from typing import TypeVar # noqa: F401
7-
8-
D = TypeVar("D", bound="Data")
9-
except ImportError:
10-
pass
11-
121
import hashlib
132
from copy import deepcopy
3+
from typing import TypeVar
144
from uuid import UUID
155
from uuid import uuid4
166

177
import compas
188

19-
# ==============================================================================
20-
# If you ever feel tempted to use ABCMeta in your code: don't, just DON'T.
21-
# Assigning __metaclass__ = ABCMeta to a class causes a severe memory leak/performance
22-
# degradation on IronPython 2.7.
23-
24-
# See these issues for more details:
25-
# - https://github.com/compas-dev/compas/issues/562
26-
# - https://github.com/compas-dev/compas/issues/649
27-
28-
# ==============================================================================
9+
D = TypeVar("D", bound="Data")
2910

3011

31-
class Data(object):
12+
class Data:
3213
"""Abstract base class for all COMPAS data objects.
3314
3415
Parameters
@@ -145,7 +126,7 @@ def __setstate__(self, state):
145126
self.name = state["name"]
146127

147128
@classmethod
148-
def __from_data__(cls, data): # type: (dict) -> Data
129+
def __from_data__(cls, data) -> "Data":
149130
"""Construct an object of this type from the provided data.
150131
151132
Parameters
@@ -190,7 +171,7 @@ def name(self, name):
190171
self._name = name
191172

192173
@classmethod
193-
def from_json(cls, filepath): # type: (...) -> Data
174+
def from_json(cls, filepath) -> "Data":
194175
"""Construct an object of this type from a JSON file.
195176
196177
Parameters
@@ -232,7 +213,7 @@ def to_json(self, filepath, pretty=False, compact=False, minimal=False):
232213
compas.json_dump(self, filepath, pretty=pretty, compact=compact, minimal=minimal)
233214

234215
@classmethod
235-
def from_jsonstring(cls, string): # type: (...) -> Data
216+
def from_jsonstring(cls, string) -> "Data":
236217
"""Construct an object of this type from a JSON string.
237218
238219
Parameters
@@ -276,7 +257,7 @@ def to_jsonstring(self, pretty=False, compact=False, minimal=False):
276257
"""
277258
return compas.json_dumps(self, pretty=pretty, compact=compact, minimal=minimal)
278259

279-
def copy(self, cls=None, copy_guid=False): # type: (...) -> D
260+
def copy(self, cls=None, copy_guid=False) -> D: # type: ignore
280261
"""Make an independent copy of the data object.
281262
282263
Parameters

src/compas/data/encoders.py

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
5-
try:
6-
from typing import Type # noqa: F401
7-
except ImportError:
8-
pass
9-
101
import json
112
import platform
3+
from typing import Type
124

13-
from .data import Data # noqa: F401
5+
import numpy as np
6+
7+
from .data import Data
148
from .exceptions import DecoderError
159

1610
IDictionary = None
@@ -27,20 +21,8 @@
2721
except: # noqa: E722
2822
pass
2923

30-
try:
31-
import numpy as np
32-
33-
try:
34-
np_float = np.float_
35-
except AttributeError:
36-
np_float = np.float64
37-
38-
numpy_support = True
39-
except (ImportError, SyntaxError):
40-
numpy_support = False
4124

42-
43-
def cls_from_dtype(dtype, inheritance=None): # type: (...) -> Type[Data]
25+
def cls_from_dtype(dtype, inheritance=None) -> Type[Data]:
4426
"""Get the class object corresponding to a COMPAS data type specification.
4527
4628
Parameters
@@ -144,32 +126,31 @@ def default(self, o):
144126
if hasattr(o, "__next__"):
145127
return list(o)
146128

147-
if numpy_support:
148-
if isinstance(o, np.ndarray):
149-
return o.tolist()
150-
if isinstance(
151-
o,
152-
(
153-
np.int_,
154-
np.intc,
155-
np.intp,
156-
np.int8,
157-
np.int16,
158-
np.int32,
159-
np.int64,
160-
np.uint8,
161-
np.uint16,
162-
np.uint32,
163-
np.uint64,
164-
), # type: ignore
165-
):
166-
return int(o)
167-
if isinstance(o, (np_float, np.float16, np.float32, np.float64)): # type: ignore
168-
return float(o)
169-
if isinstance(o, np.bool_):
170-
return bool(o)
171-
if isinstance(o, np.void):
172-
return None
129+
if isinstance(o, np.ndarray):
130+
return o.tolist()
131+
if isinstance(
132+
o,
133+
(
134+
np.int_,
135+
np.intc,
136+
np.intp,
137+
np.int8,
138+
np.int16,
139+
np.int32,
140+
np.int64,
141+
np.uint8,
142+
np.uint16,
143+
np.uint32,
144+
np.uint64,
145+
), # type: ignore
146+
):
147+
return int(o)
148+
if isinstance(o, (np.float_, np.float16, np.float32, np.float64)): # type: ignore
149+
return float(o)
150+
if isinstance(o, np.bool_):
151+
return bool(o)
152+
if isinstance(o, np.void):
153+
return None
173154

174155
if dotnet_support:
175156
if isinstance(o, (System.Decimal, System.Double, System.Single)):
@@ -217,7 +198,7 @@ class DataDecoder(json.JSONDecoder):
217198
"""
218199

219200
def __init__(self, *args, **kwargs):
220-
super(DataDecoder, self).__init__(object_hook=self.object_hook, *args, **kwargs)
201+
super().__init__(object_hook=self.object_hook, *args, **kwargs)
221202

222203
def object_hook(self, o):
223204
"""Reconstruct a deserialized object.

src/compas/data/json.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
51
import json
62
import zipfile
73

84
from compas import _iotools
9-
from compas.data import Data # noqa: F401
105
from compas.data import DataDecoder
116
from compas.data import DataEncoder
127

@@ -66,7 +61,7 @@ def json_dump(data, fp, pretty=False, compact=False, minimal=False):
6661
return json.dump(data, f, cls=DataEncoder, **kwargs)
6762

6863

69-
def json_dumps(data, pretty=False, compact=False, minimal=False): # type: (...) -> str
64+
def json_dumps(data, pretty=False, compact=False, minimal=False) -> str:
7065
"""Write a collection of COMPAS objects to a JSON string.
7166
7267
Parameters
@@ -173,7 +168,7 @@ def json_loadz(zip_file):
173168
return json_loads(json_str)
174169

175170

176-
def json_load(fp): # type: (...) -> dict
171+
def json_load(fp) -> dict:
177172
"""Read COMPAS object data from a JSON file.
178173
179174
Parameters
@@ -207,7 +202,7 @@ def json_load(fp): # type: (...) -> dict
207202
return json.load(f, cls=DataDecoder)
208203

209204

210-
def json_loads(s): # type: (...) -> dict
205+
def json_loads(s) -> dict:
211206
"""Read COMPAS object data from a JSON string.
212207
213208
Parameters

src/compas/data/schema.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
51
import json
62
import os
73

84

9-
def dataclass_dataschema(cls): # type: (...) -> dict
5+
def dataclass_dataschema(cls) -> dict:
106
"""Generate a JSON schema for a COMPAS object class.
117
128
Parameters
@@ -23,7 +19,7 @@ def dataclass_dataschema(cls): # type: (...) -> dict
2319
return cls.DATASCHEMA
2420

2521

26-
def dataclass_typeschema(cls): # type: (...) -> dict
22+
def dataclass_typeschema(cls) -> dict:
2723
"""Generate a JSON schema for the data type of a COMPAS object class.
2824
2925
Parameters
@@ -43,7 +39,7 @@ def dataclass_typeschema(cls): # type: (...) -> dict
4339
}
4440

4541

46-
def dataclass_jsonschema(cls, filepath=None, draft=None): # type: (...) -> dict
42+
def dataclass_jsonschema(cls, filepath=None, draft=None) -> dict:
4743
"""Generate a JSON schema for a COMPAS object class.
4844
4945
Parameters
@@ -85,7 +81,7 @@ def dataclass_jsonschema(cls, filepath=None, draft=None): # type: (...) -> dict
8581
return schema
8682

8783

88-
def compas_jsonschema(dirname=None): # type: (...) -> list
84+
def compas_jsonschema(dirname=None) -> list:
8985
"""Generate a JSON schema for the COMPAS data model.
9086
9187
Parameters
@@ -110,7 +106,7 @@ def compas_jsonschema(dirname=None): # type: (...) -> list
110106
return schemas
111107

112108

113-
def compas_dataclasses(): # type: (...) -> list
109+
def compas_dataclasses() -> list:
114110
"""Find all classes in the COMPAS data model.
115111
116112
Returns

src/compas/data/validators.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
4-
5-
try:
6-
basestring # type: ignore
7-
except NameError:
8-
basestring = str
9-
10-
111
def is_sequence_of_str(items):
122
"""Verify that the sequence contains only items of type str.
133
@@ -23,7 +13,7 @@ def is_sequence_of_str(items):
2313
False otherwise.
2414
2515
"""
26-
return all(isinstance(item, basestring) for item in items)
16+
return all(isinstance(item, str) for item in items)
2717

2818

2919
def is_sequence_of_int(items):

0 commit comments

Comments
 (0)