4 from schema
import FieldResponse, PlaneResponse, PathResponse
9 Return a dictionary for the object which is marked up for type. 11 for typename
in [
'FieldResponse',
'PlaneResponse',
'PathResponse']:
12 if typename ==
type(obj).__name__:
13 cname = obj.__class__.__name__
14 return {cname: {k:
todict(v)
for k, v
in obj._asdict().items()}}
15 if isinstance(obj, numpy.ndarray):
16 shape = list(obj.shape)
17 elements = obj.flatten().tolist()
18 return dict(array=dict(shape=shape, elements=elements))
19 if isinstance(obj, list):
20 return [
todict(ele)
for ele
in obj]
29 if isinstance(obj, dict):
32 ret = numpy.asarray(obj[
'array'][
'elements'])
33 return ret.reshape(obj[
'array'][
'shape'])
35 for typ
in [FieldResponse, PlaneResponse, PathResponse]:
38 return typ(**{k:
fromdict(v)
for k, v
in obj[tname].items()
if k
not in [
"pitchdir",
"wiredir"]})
40 if isinstance(obj, list):
41 return [
fromdict(ele)
for ele
in obj]
48 Dump object to JSON text. 50 return json.dumps(
todict(obj), indent=2)
55 Load object from JSON text. 62 Save a response object (typically response.schema.FieldResponse) 63 to a file of the given name. 66 if filename.endswith(
".json"):
69 if filename.endswith(
".json.bz2"):
71 bz2.BZ2File(filename,
'w').
write(text)
73 if filename.endswith(
".json.gz"):
75 gzip.open(filename,
"wb").
write(text)
77 raise ValueError(
"unknown file format: %s" % filename)
82 Return response.schema object representation of the data in the 83 file of the given name. 85 if filename.endswith(
".json"):
88 if filename.endswith(
".json.bz2"):
90 return loads(bz2.BZ2File(filename,
'r').read()) 92 if filename.endswith(
".json.gz"):
94 return loads(gzip.open(filename,
"rb").
read())
96 raise ValueError(
"unknown file format: %s" % filename)
int open(const char *, int)
Opens a file descriptor.
int read(int, char *, size_t)
Read bytes from a file descriptor.
def write(rflist, outputfile="wire-cell-garfield-response.json.bz2")