3 Functions to assist in persisting schema objects. 19 Return a dictionary for the object which is marked up for type. 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]
39 if isinstance(obj, dict):
41 for typ
in schema.classes():
44 return typ(**{k:
fromdict(v)
for k, v
in obj[tname].items()})
46 if isinstance(obj, list):
47 return [
fromdict(ele)
for ele
in obj]
54 Dump object to JSON text. 56 return json.dumps(
todict(obj), indent=indent)
61 Load object from JSON text. 66 def dump(filename, obj, indent=2):
68 Save a response object (typically response.schema.FieldResponse) 69 to a file of the given name. 72 if filename.endswith(
".json"):
75 if filename.endswith(
".json.bz2"):
77 bz2.BZ2File(filename,
'w').
write(btext)
79 if filename.endswith(
".json.gz"):
81 gzip.open(filename,
"wb").
write(btext)
83 raise ValueError(
"unknown file format: %s" % filename)
88 Return response.schema object representation of the data in the 89 file of the given name. 91 if filename.endswith(
".json"):
94 if filename.endswith(
".json.bz2"):
96 return loads(bz2.BZ2File(filename,
'r').read()) 98 if filename.endswith(
".json.gz"):
100 return loads(gzip.open(filename,
"rb").
read())
102 raise ValueError(
"unknown file format: %s" % filename)
ps_atom_t encode(std::string const &)
int open(const char *, int)
Opens a file descriptor.
def dump(filename, obj, indent=2)
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
int read(int, char *, size_t)
Read bytes from a file descriptor.