Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactor for attribute handler factories
  • Loading branch information
dwhswenson committed Dec 1, 2020
commit a67f1644bd1fcceb4cc8185dc50e23c64b41244c
34 changes: 34 additions & 0 deletions openpathsampling/experimental/simstore/attribute_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from .my_types import parse_ndarray_type


class HandlerFactory(object):
def is_my_type(self, type_str):
pass

def serializer(self, type_str):
pass

def deserializer(self, type_str):
pass

class NDArrayHandlerFactory(HandlerFactory):
def is_my_type(self, type_str):
as_ndarray = parse_ndarray_type(type_str)

def serializer(self, type_str):
as_ndarray = self.is_my_type(type_str)
if as_ndarray:
dtype, shape = as_ndarray
handler = lambda data, _: \
np.fromstring(data, dtype=dtype).reshape(shape)
return handler

def deserializer(self, type_str):
as_ndarray = self.is_my_type(type_str)
if as_ndarray:
dtype, shape = as_ndarray
return lambda arr: \
arr.astype(dtype=dtype, copy=False).tostring()



37 changes: 27 additions & 10 deletions openpathsampling/experimental/simstore/serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from .my_types import parse_ndarray_type
from . import serialization_helpers as serialization
from .attribute_handlers import NDArrayHandlerFactory
import json

import logging
Expand Down Expand Up @@ -96,6 +97,9 @@ def make_all_lazies(self, lazies):


class SchemaDeserializer(object):
handler_factories = [
NDArrayHandlerFactory(),
]
default_handlers = {
'lazy': serialization.search_caches,
'uuid': serialization.search_caches,
Expand All @@ -113,9 +117,15 @@ def __init__(self, schema, table, cls):
self.attribute_handlers = self.init_attribute_handlers()

# TODO: move this external
@staticmethod
def make_numpy_handler(dtype, shape):
return lambda data, _: np.fromstring(data, dtype=dtype).reshape(shape)
# @staticmethod
# def make_numpy_handler(dtype, shape):
# return lambda data, _: np.fromstring(data, dtype=dtype).reshape(shape)

def get_handler_from_factories(self, type_name):
for factory in self.handler_factories:
handler = factory.serializer(type_name)
if handler is not None:
return handler

def init_attribute_handlers(self):
attribute_handlers = {}
Expand All @@ -124,10 +134,11 @@ def init_attribute_handlers(self):
if type_name in self.default_handlers:
handler = self.default_handlers[type_name]
else:
as_ndarray = parse_ndarray_type(type_name)
if as_ndarray:
(dtype, shape) = as_ndarray
handler = self.make_numpy_handler(dtype, shape)
handler = self.get_handler_from_factories(type_name)
# as_ndarray = parse_ndarray_type(type_name)
# if as_ndarray:
# (dtype, shape) = as_ndarray
# handler = self.make_numpy_handler(dtype, shape)
if handler:
attribute_handlers[attr] = handler
return attribute_handlers
Expand Down Expand Up @@ -158,9 +169,15 @@ class ToDictSerializer(SchemaDeserializer):

# TODO: move this external; that will allow us to remove this class
# (use it as input to SchemaSerializer or a class factory for that)
@staticmethod
def make_numpy_handler(dtype, shape):
return lambda arr: arr.astype(dtype=dtype, copy=False).tostring()
# @staticmethod
# def make_numpy_handler(dtype, shape):
# return lambda arr: arr.astype(dtype=dtype, copy=False).tostring()

def get_handler_from_factories(self, type_name):
for factory in self.handler_factories:
handler = factory.serializer(type_name)
if handler is not None:
return handler

def __call__(self, obj):
dct = obj.to_dict()
Expand Down