def _basetype(var, printname=True): if printname: yield var.id yield '\n' if not getattr(var, "shape", ()): yield encode(var.data) else: for indexes, value in zip(np.ndindex(var.shape), var.data.flat): yield "{indexes} {value}\n".format( indexes="[" + "][".join([str(idx) for idx in indexes]) + "]", value=encode(value))
def _(var, printname=True): if printname: yield var.id yield '\n' if not getattr(var, "shape", ()): yield encode(var.data) else: for indexes, value in zip(np.ndindex(var.shape), var.data.flat): yield "{indexes} {value}\n".format( indexes="[" + "][".join([str(idx) for idx in indexes]) + "]", value=encode(value))
def build_attributes(attr, values, level=0): """ Recursive function to build the DAS. """ # check for metadata if isinstance(values, dict): yield '{indent}{attr} {{\n'.format(indent=(level+1)*INDENT, attr=attr) for k, v in values.items(): for line in build_attributes(k, v, level+1): yield line yield '{indent}}}\n'.format(indent=(level+1)*INDENT) else: # get type type = get_type(values) # encode values if isinstance(values, basestring) or not isinstance(values, Iterable): values = [encode(values)] else: values = map(encode, values) yield '{indent}{type} {attr} {values};\n'.format( indent=(level+1)*INDENT, type=type, attr=quote(attr), values=', '.join(values))
def build_attributes(attr, values, level=0): """Recursive function to build the DAS.""" # check for metadata if isinstance(values, dict): yield '{indent}{attr} {{\n'.format(indent=(level) * INDENT, attr=attr) for k, v in values.items(): for line in build_attributes(k, v, level + 1): yield line yield '{indent}}}\n'.format(indent=(level) * INDENT) else: # get type type = get_type(values) # encode values if (isinstance(values, string_types) or not isinstance(values, Iterable) or getattr(values, 'shape', None) == ()): values = [encode(values)] else: values = map(encode, values) yield '{indent}{type} {attr} {values};\n'.format( indent=(level) * INDENT, type=type, attr=quote(attr), values=', '.join(values))
def __call__(self, *args): params = [] for arg in args: if isinstance(arg, (DapType, ServerFunctionResult)): params.append(arg.id) else: params.append(encode(arg)) id_ = self.name + '(' + ','.join(params) + ')' return ServerFunctionResult(self.baseurl, id_)
def __call__(self, *args): params = [] for arg in args: if isinstance(arg, (DapType, ServerFunctionResult)): params.append(arg.id) else: params.append(encode(arg)) id_ = self.name + '(' + ','.join(params) + ')' return ServerFunctionResult(self.baseurl, id_, self.application, self.session)
def __init__(self, info): # get exception message buf = StringIO() print_exception(*info, file=buf) message = encode(buf.getvalue()) # build error message code = getattr(info[0], 'code', -1) self.body = r'''Error {{ code = {}; message = {}; }}'''.format(code, message)
def __gt__(self, other): return ConstraintExpression('%s>%s' % (self.id, encode(other))) def __lt__(self, other): return ConstraintExpression('%s<%s' % (self.id, encode(other)))
def __eq__(self, other): return ConstraintExpression('%s=%s' % (self.id, encode(other))) def __ne__(self, other): return ConstraintExpression('%s!=%s' % (self.id, encode(other)))
def __ge__(self, other): return ConstraintExpression('%s>=%s' % (self.id, encode(other))) def __le__(self, other): return ConstraintExpression('%s<=%s' % (self.id, encode(other)))
def test_integer(self): """Test integer encoding.""" self.assertEqual(encode(1), "1")
def __lt__(self, other): return ConstraintExpression("%s<%s" % (self.id, encode(other)))
def __lt__(self, other): return ConstraintExpression('%s<%s' % (self.id, encode(other)))
def test_unicode(self): """Unicode objects are encoded just like strings.""" self.assertEqual(encode(u"test"), '"test"')
def __lt__(self, other): if isinstance(other, self.__class__): right = other.template.id else: right = encode(other) return ConstraintExpression('%s<%s' % (self.template.id, right))
def test_obj(self): """Other objects are encoded according to their ``repr``.""" self.assertEqual(encode({}), '"{}"')
def test_string_with_quotation(self): """Test encoding a string with a quotation mark.""" self.assertEqual(encode('this is a "test"'), '"this is a \"test\""')
def test_string(self): """Test string encoding.""" self.assertEqual(encode("test"), '"test"')
def test_float(self): """Test floating encoding.""" self.assertEqual(encode(np.pi), "3.14159")
def __lt__(self, other): return ConstraintExpression('%s<%s' % (self.id, encode(other))) def build_filter(selection, cols):
def __lt__(self, other): return ConstraintExpression('%s<%s' % (self.id, encode(other))) class StreamReader(object):
def test_numpy_string(self): self.assertEqual(encode(np.array('1', dtype='<U1')), '"1"')