Functions
wirecell.util.wires.persist Namespace Reference

Functions

def todict (obj)
 
def fromdict (obj)
 
def dumps (obj, indent=2)
 
def loads (text)
 
def dump (filename, obj, indent=2)
 
def load (filename)
 

Detailed Description

Functions to assist in persisting schema objects.

Function Documentation

def wirecell.util.wires.persist.dump (   filename,
  obj,
  indent = 2 
)
Save a response object (typically response.schema.FieldResponse)
to a file of the given name.

Definition at line 66 of file persist.py.

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 
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
def wirecell.util.wires.persist.dumps (   obj,
  indent = 2 
)
Dump object to JSON text.

Definition at line 52 of file persist.py.

52 def dumps(obj, indent=2):
53  '''
54  Dump object to JSON text.
55  '''
56  return json.dumps(todict(obj), indent=indent)
57 
58 
def dumps(obj, indent=2)
Definition: persist.py:52
def wirecell.util.wires.persist.fromdict (   obj)
Undo `todict()`.

Definition at line 35 of file persist.py.

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 
def wirecell.util.wires.persist.load (   filename)
Return response.schema object representation of the data in the
file of the given name.

Definition at line 86 of file persist.py.

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)
103 
int open(const char *, int)
Opens a file descriptor.
int read(int, char *, size_t)
Read bytes from a file descriptor.
def load(filename)
Definition: persist.py:86
def wirecell.util.wires.persist.loads (   text)
Load object from JSON text.

Definition at line 59 of file persist.py.

59 def loads(text):
60  '''
61  Load object from JSON text.
62  '''
63  return fromdict(json.loads(text))
64 
65 
def wirecell.util.wires.persist.todict (   obj)
Return a dictionary for the object which is marked up for type.

Definition at line 17 of file persist.py.

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