def encode(self, pythonExpr):
     """Encode most common Python types to their Wolfram Language counterpart."""
     if isinstance(pythonExpr, six.binary_type):
         yield wxfexpr.WXFExprBinaryString(pythonExpr)
     elif isinstance(pythonExpr, six.string_types):
         yield wxfexpr.WXFExprString(pythonExpr)
     elif isinstance(pythonExpr, WLSymbol):
         yield wxfexpr.WXFExprSymbol(pythonExpr.name)
     elif isinstance(pythonExpr, WLFunction):
         yield wxfexpr.WXFExprFunction(len(pythonExpr))
         for sub in self.encode(pythonExpr.head):
             yield sub
         for arg in pythonExpr.args:
             for sub in self.encode(arg):
                 yield sub
     elif isinstance(pythonExpr, six.integer_types):
         yield wxfexpr.WXFExprInteger(pythonExpr)
     elif isinstance(pythonExpr, list):
         yield wxfexpr.WXFExprFunction(len(pythonExpr))
         yield wxfexpr.WXFExprSymbol("List")
         for pyArg in iter(pythonExpr):
             for wxf_expr in self.serialize(pyArg):
                 yield wxf_expr
     elif isinstance(pythonExpr, dict):
         yield wxfexpr.WXFExprAssociation(len(pythonExpr))
         for key, value in pythonExpr.items():
             yield wxfexpr.WXFExprRule()
             for wxf_expr in self.serialize(key):
                 yield wxf_expr
             for wxf_expr in self.serialize(value):
                 yield wxf_expr
     elif pythonExpr is True:
         yield wxfexpr.WXFExprSymbol("True")
     elif pythonExpr is False:
         yield wxfexpr.WXFExprSymbol("False")
     elif pythonExpr is None:
         yield wxfexpr.WXFExprSymbol("None")
     elif isinstance(pythonExpr, float):
         yield wxfexpr.WXFExprReal(pythonExpr)
     elif isinstance(pythonExpr, complex):
         yield wxfexpr.WXFExprFunction(2)
         yield wxfexpr.WXFExprSymbol("Complex")
         yield wxfexpr.WXFExprReal(pythonExpr.real)
         yield wxfexpr.WXFExprReal(pythonExpr.imag)
     elif isinstance(pythonExpr, wxfexpr.WXFExpr):
         yield pythonExpr
     elif isinstance(pythonExpr, decimal.Decimal):
         yield wxfexpr.WXFExprBigReal(py_encode_decimal(pythonExpr))
Exemplo n.º 2
0
 def serialize_decimal(self, number):
     yield py_encode_decimal(number)
Exemplo n.º 3
0
 def serialize_decimal(self, number):
     number = py_encode_decimal(number)
     yield WXF_CONSTANTS.BigReal
     yield varint_bytes(len(number))
     yield number