1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-03 02:50:58 +08:00

online serving v10

This commit is contained in:
lzh222333
2021-05-07 09:59:15 +00:00
parent 84c56f13bd
commit 9dfd001f6f
14 changed files with 426 additions and 345 deletions

View File

@@ -732,7 +732,7 @@ def flatten_dict(d, parent_key="", sep="."):
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
new_key = parent_key + sep + str(k) if parent_key else k
if isinstance(v, collections.abc.MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:

View File

@@ -3,6 +3,7 @@
from pathlib import Path
import pickle
import dill
from typing import Union
@@ -14,6 +15,8 @@ class Serializable:
- For examples, a learnable Datahandler just wants to save the parameters without data when dumping to disk
"""
pickle_backend = "pickle" # another optional value is "dill" which can pickle more things of python.
def __init__(self):
self._dump_all = False
self._exclude = []
@@ -74,4 +77,35 @@ class Serializable:
def to_pickle(self, path: Union[Path, str], dump_all: bool = None, exclude: list = None):
self.config(dump_all=dump_all, exclude=exclude)
with Path(path).open("wb") as f:
pickle.dump(self, f)
if self.pickle_backend == "pickle":
pickle.dump(self, f)
elif self.pickle_backend == "dill":
dill.dump(self, f)
else:
raise ValueError("Unknown pickle backend, please use 'pickle' or 'dill'.")
@classmethod
def load(cls, filepath):
"""
load the collector from a file
Args:
filepath (str): the path of file
Raises:
TypeError: the pickled file must be `Collector`
Returns:
Collector: the instance of Collector
"""
with open(filepath, "rb") as f:
if cls.pickle_backend == "pickle":
object = pickle.load(f)
elif cls.pickle_backend == "dill":
object = dill.load(f)
else:
raise ValueError("Unknown pickle backend, please use 'pickle' or 'dill'.")
if isinstance(object, cls):
return object
else:
raise TypeError(f"The instance of {type(object)} is not a valid `{type(cls)}`!")