Skip to content

Commit

Permalink
add support for dataclass serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
jhidding committed Jul 16, 2019
1 parent 2c319fa commit 370c904
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
14 changes: 12 additions & 2 deletions noodles/draw_workflow/draw_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ def _format_arg_list(a, v):

s = "({0}{1})"
for i in a[:-1]:
if isinstance(i, float):
istr = "{:.6}".format(i)
else:
istr = str(i)

s = s.format(
_sugar(str(i))
_sugar(istr)
if i is not Parameter.empty
else "\u2014", ", {0}{1}")

if v:
return s.format("\u2026", "")

if isinstance(a[-1], float):
istr = "{:.6}".format(a[-1])
else:
istr = str(a[-1])

return s.format(
_sugar(str(a[-1]))
_sugar(istr)
if a[-1] is not Parameter.empty
else "\u2014", "")

Expand Down
9 changes: 7 additions & 2 deletions noodles/serial/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# from .numpy import registry as numpy
try:
from .numpy import registry as numpy
except ImportError:
numpy = None

from .pickle import registry as pickle
from .base import registry as base
from .as_dict import AsDict
from .namedtuple import registry as namedtuple
from .registry import (Registry, Serialiser, RefObject)
from .reasonable import (Reasonable)
from .path import SerPath

__all__ = ['pickle', 'base', 'Registry', 'Serialiser', 'SerPath',
'RefObject', 'AsDict', 'namedtuple', 'Reasonable']
'RefObject', 'AsDict', 'namedtuple', 'Reasonable', 'numpy']
16 changes: 15 additions & 1 deletion noodles/serial/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
from pathlib import Path
import base64

try:
import dataclasses
from .dataclass import SerDataClass
except ImportError:
use_dataclasses = False
SerDataClass = None
else:
use_dataclasses = True

from ..interface import (Fail, PromisedObject, Quote)
from ..lib import (object_name, look_up, importable, unwrap, is_unwrapped)
from ..workflow import (Workflow, NodeData, FunctionNode, ArgumentAddress,
Expand Down Expand Up @@ -208,6 +217,10 @@ def _noodles_hook(obj):
if '__member_of__' in dir(obj) and obj.__member_of__:
return '<method>'

if use_dataclasses and \
dataclasses.is_dataclass(obj) and not isinstance(obj, type):
return '<dataclass>'

if importable(obj):
return '<importable>'

Expand Down Expand Up @@ -259,7 +272,8 @@ def registry():
'<boundmethod>': SerBoundMethod(),
'<importable>': SerImportable(),
'<automagic>': SerAuto(),
'<unwrapped>': SerUnwrapped()
'<unwrapped>': SerUnwrapped(),
'<dataclass>': SerDataClass()
},
hook_fn=_noodles_hook,
default=SerUnknown(),
Expand Down
13 changes: 13 additions & 0 deletions noodles/serial/dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import dataclasses
from .registry import Serialiser


class SerDataClass(Serialiser):
def __init__(self):
super(SerDataClass, self).__init__('<dataclass>')

def encode(self, obj, make_rec):
return make_rec(obj.__dict__)

def decode(self, cls, data):
return cls(**data)

0 comments on commit 370c904

Please sign in to comment.