ATLAS Offline Software
Loading...
Searching...
No Matches
serialization.py
Go to the documentation of this file.
1# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
2import logging
3import pickle
4from typing import Any, Set
5
6logger = logging.getLogger(__name__)
7
8
9def save_object(file_path: str, obj: Any) -> None:
10 logger.info(f"Saving object of type {type(obj)} to {file_path}")
11 with open(file_path, "wb") as f:
12 pickle.dump(obj, f)
13
14
15def load_strings_set(file_path: str) -> Set[str]:
16 logger.info(f"Loading {file_path}")
17 with open(file_path, "rb") as f:
18 s: Set[str] = pickle.load(f)
19 if not isinstance(s, set):
20 msg = f"Saved object is not a set. Type: {type(s)}"
21 logger.error(msg)
22 raise TypeError(msg)
23 for element in s:
24 if not isinstance(element, str):
25 msg = f"Set element is not a string. Type: {type(element)}"
26 logger.error(msg)
27 raise TypeError(msg)
28 return s
Set[str] load_strings_set(str file_path)
None save_object(str file_path, Any obj)