Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataclass #73

Merged
merged 8 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bigraph_schema/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

92 changes: 92 additions & 0 deletions bigraph_schema/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import numpy as np

from pprint import pformat as pf
from typing import Any, Tuple, Union, Optional, Mapping
from dataclasses import field, make_dataclass

from bigraph_schema.parse import parse_expression
from bigraph_schema.protocols import local_lookup_module, function_module
import bigraph_schema.data


NONE_SYMBOL = '!nil'
Expand Down Expand Up @@ -525,6 +528,34 @@ def fold_union(schema, state, method, values, core):
return result


def type_parameters_for(schema):
parameters = []
for key in schema['_type_parameters']:
subschema = schema.get(f'_{key}', 'any')
parameters.append(subschema)

return parameters


def dataclass_union(schema, path, core):
parameters = type_parameters_for(schema)
subtypes = []
for parameter in parameters:
dataclass = core.dataclass(
parameter,
path)

if isinstance(dataclass, str):
subtypes.append(dataclass)
elif isinstance(dataclass, type):
subtypes.append(dataclass.__name__)
else:
subtypes.append(str(dataclass))

parameter_block = ', '.join(subtypes)
return eval(f'Union[{parameter_block}]')


def divide_any(schema, state, values, core):
divisions = values.get('divisions', 2)

Expand All @@ -551,6 +582,64 @@ def divide_any(schema, state, values, core):
for _ in range(divisions)]


def dataclass_any(schema, path, core):
parts = path
if not parts:
parts = ['top']
dataclass_name = '_'.join(parts)

if isinstance(schema, dict):
type_name = schema.get('_type', 'any')

branches = {}
for key, subschema in schema.items():
if not key.startswith('_'):
branch = core.dataclass(
subschema,
path + [key])

def default(subschema=subschema):
return core.default(subschema)

branches[key] = (
key,
branch,
field(default_factory=default))

dataclass = make_dataclass(
dataclass_name,
branches.values(),
namespace={
'__module__': 'bigraph_schema.data'})

setattr(
bigraph_schema.data,
dataclass_name,
dataclass)

else:
schema = core.access(schema)
dataclass = core.dataclass(schema, path)

return dataclass


def dataclass_tuple(schema, path, core):
parameters = type_parameters_for(schema)
subtypes = []

for index, key in enumerate(schema['type_parameters']):
subschema = schema.get(key, 'any')
subtype = core.dataclass(
subschema,
path + [index])

subtypes.append(subtype)

parameter_block = ', '.join(subtypes)
return eval(f'tuple[{parameter_block}]')


def divide_tuple(schema, state, values, core):
divisions = values.get('divisions', 2)

Expand Down Expand Up @@ -954,6 +1043,7 @@ def deserialize_union(schema, encoded, core):
'_check': check_any,
'_serialize': serialize_any,
'_deserialize': deserialize_any,
'_dataclass': dataclass_any,
'_fold': fold_any,
'_merge': merge_any,
'_bind': bind_any,
Expand All @@ -967,6 +1057,7 @@ def deserialize_union(schema, encoded, core):
'_slice': slice_tuple,
'_serialize': serialize_tuple,
'_deserialize': deserialize_tuple,
'_dataclass': dataclass_tuple,
'_fold': fold_tuple,
'_divide': divide_tuple,
'_bind': bind_tuple,
Expand All @@ -980,6 +1071,7 @@ def deserialize_union(schema, encoded, core):
'_slice': slice_union,
'_serialize': serialize_union,
'_deserialize': deserialize_union,
'_dataclass': dataclass_union,
'_fold': fold_union,
'_description': 'union of a set of possible types'}}

Expand Down
Loading
Loading