persist.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 Functions to assist in persisting schema objects.
4 
5 '''
6 
7 # fixme: except for this next line, this module is generic. See also similar
8 # code for the detector response schema.
9 from . import schema
10 
11 ###########################
12 
13 import json
14 import numpy
15 
16 
17 def todict(obj):
18  '''
19  Return a dictionary for the object which is marked up for type.
20  '''
21  for typename in [c.__name__ for c in schema.classes()]:
22  if typename == type(obj).__name__:
23  cname = obj.__class__.__name__
24  return {cname: {k: todict(v) for k, v in obj._asdict().items()}}
25  if isinstance(obj, numpy.ndarray):
26  shape = list(obj.shape)
27  elements = obj.flatten().tolist()
28  return dict(array=dict(shape=shape, elements=elements))
29  if isinstance(obj, list):
30  return [todict(ele) for ele in obj]
31 
32  return obj
33 
34 
35 def fromdict(obj):
36  '''
37  Undo `todict()`.
38  '''
39  if isinstance(obj, dict):
40 
41  for typ in schema.classes():
42  tname = typ.__name__
43  if tname in obj:
44  return typ(**{k: fromdict(v) for k, v in obj[tname].items()})
45 
46  if isinstance(obj, list):
47  return [fromdict(ele) for ele in obj]
48 
49  return obj
50 
51 
52 def dumps(obj, indent=2):
53  '''
54  Dump object to JSON text.
55  '''
56  return json.dumps(todict(obj), indent=indent)
57 
58 
59 def loads(text):
60  '''
61  Load object from JSON text.
62  '''
63  return fromdict(json.loads(text))
64 
65 
66 def dump(filename, obj, indent=2):
67  '''
68  Save a response object (typically response.schema.FieldResponse)
69  to a file of the given name.
70  '''
71  btext = dumps(obj, indent=indent).encode()
72  if filename.endswith(".json"):
73  open(filename, 'wb').write(btext)
74  return
75  if filename.endswith(".json.bz2"):
76  import bz2
77  bz2.BZ2File(filename, 'w').write(btext)
78  return
79  if filename.endswith(".json.gz"):
80  import gzip
81  gzip.open(filename, "wb").write(btext)
82  return
83  raise ValueError("unknown file format: %s" % filename)
84 
85 
86 def load(filename):
87  '''
88  Return response.schema object representation of the data in the
89  file of the given name.
90  '''
91  if filename.endswith(".json"):
92  return loads(open(filename, 'r').read())
93 
94  if filename.endswith(".json.bz2"):
95  import bz2
96  return loads(bz2.BZ2File(filename, 'r').read())
97 
98  if filename.endswith(".json.gz"):
99  import gzip
100  return loads(gzip.open(filename, "rb").read())
101 
102  raise ValueError("unknown file format: %s" % filename)
ps_atom_t encode(std::string const &)
Definition: coding.cc:87
int open(const char *, int)
Opens a file descriptor.
def dump(filename, obj, indent=2)
Definition: persist.py:66
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
def dumps(obj, indent=2)
Definition: persist.py:52
int read(int, char *, size_t)
Read bytes from a file descriptor.
def load(filename)
Definition: persist.py:86