forked from M-Labs/artiq
1
0
Fork 0

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