protocols/pyon: fix formatting, support OrderedDict

This commit is contained in:
Sebastien Bourdeauducq 2015-11-30 11:40:34 +08:00
parent 23a84500a3
commit 2904b31e7e
1 changed files with 12 additions and 6 deletions

View File

@ -19,6 +19,7 @@ function call syntax to mark special data types.
import base64
from fractions import Fraction
from collections import OrderedDict
import os
import tempfile
@ -36,6 +37,7 @@ _encode_map = {
list: "list",
dict: "dict",
Fraction: "fraction",
OrderedDict: "ordereddict",
numpy.ndarray: "nparray"
}
@ -113,21 +115,24 @@ class _Encoder:
return r
def encode_fraction(self, x):
return "Fraction({}, {})".format(encode(x.numerator),
encode(x.denominator))
return "Fraction({}, {})".format(self.encode(x.numerator),
self.encode(x.denominator))
def encode_ordereddict(self, x):
return "OrderedDict("+ self.encode(list(x.items())) +")"
def encode_nparray(self, x):
r = "nparray("
r += encode(x.shape) + ", "
r += encode(str(x.dtype)) + ", "
r += encode(base64.b64encode(x).decode())
r += self.encode(x.shape) + ", "
r += self.encode(str(x.dtype)) + ", "
r += self.encode(base64.b64encode(x).decode())
r += ")"
return r
def encode_npscalar(self, x):
r = "npscalar("
r += "\"" + type(x).__name__ + "\", "
r += encode(base64.b64encode(x).decode())
r += self.encode(base64.b64encode(x).decode())
r += ")"
return r
@ -164,6 +169,7 @@ _eval_dict = {
"true": True,
"Fraction": Fraction,
"OrderedDict": OrderedDict,
"nparray": _nparray,
"npscalar": _npscalar
}