Skip to content

Commit

Permalink
add UUID codec
Browse files Browse the repository at this point in the history
  • Loading branch information
hmacdope committed Feb 29, 2024
1 parent 544262c commit f8448bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 8 additions & 1 deletion gufe/custom_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
from openff.units import DEFAULT_UNIT_REGISTRY
from uuid import UUID

import gufe
from gufe.custom_json import JSONCodec
Expand Down Expand Up @@ -72,7 +73,6 @@ def is_openff_quantity_dict(dct):
from_dict=lambda dct: datetime.datetime.fromisoformat(dct['isotime']),
)


# Note that this has inconsistent behaviour for some generic types
# which end up being handled by the default JSON encoder/decoder.
# The main example of this is np.float64 which will be turned into
Expand Down Expand Up @@ -137,3 +137,10 @@ def is_openff_quantity_dict(dct):
is_my_obj=lambda obj: isinstance(obj, DEFAULT_UNIT_REGISTRY.Unit),
is_my_dict=is_openff_unit_dict,
)


UUID_CODEC = JSONCodec(
cls=UUID,
to_dict=lambda p: {"uuid": str(p)},
from_dict=lambda dct: UUID(dct["uuid"]),
)
18 changes: 17 additions & 1 deletion gufe/tests/test_custom_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from openff.units import unit
import pytest
from numpy import testing as npt

from uuid import uuid4
from gufe.custom_codecs import (
BYTES_CODEC,
NUMPY_CODEC,
Expand All @@ -20,6 +20,7 @@
OPENFF_UNIT_CODEC,
PATH_CODEC,
SETTINGS_CODEC,
UUID_CODEC,
)
from gufe.custom_json import JSONSerializerDeserializer, custom_json_factory
from gufe import tokenization
Expand Down Expand Up @@ -299,3 +300,18 @@ def setup_method(self):
"unit_name": "unified_atomic_mass_unit",
}
]

class TestUUIDCodec(CustomJSONCodingTest):
def setup_method(self):
self.codec = UUID_CODEC
self.objs = [
uuid4()
]
self.dcts = [
{
":is_custom:": True,
"__class__": "UUID",
"__module__": "uuid",
"uuid": f"{str(self.objs[0])}",
}
]
2 changes: 2 additions & 0 deletions gufe/tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
OPENFF_UNIT_CODEC,
PATH_CODEC,
SETTINGS_CODEC,
UUID_CODEC,
)
from gufe.custom_json import JSONSerializerDeserializer

Expand All @@ -35,6 +36,7 @@
SETTINGS_CODEC,
OPENFF_UNIT_CODEC,
OPENFF_QUANTITY_CODEC,
UUID_CODEC,
]
JSON_HANDLER = JSONSerializerDeserializer(_default_json_codecs)

Expand Down

0 comments on commit f8448bf

Please sign in to comment.