Exemplo n.º 1
0
    def test_regexp(self):
        sequence = SequenceType("sequence")
        sequence["name"] = BaseType("name")
        sequence.data = IterData([
            ("John", "Paul", "George", "Ringo"),
        ], sequence)

        filtered = sequence[ConstraintExpression('sequence.name=~"J.*"')]
        self.assertEqual(list(filtered.iterdata()), [("John", )])
Exemplo n.º 2
0
 def setUp(self):
     self.nested_object = IterData(
         ("nested", ("a", "b", "c",
                     ("d", ("e", "f", "g")))), [(1, 1, 1, [(10, 11, 12),
                                                           (21, 22, 23)]),
                                                (2, 4, 4, [(15, 16, 17)]),
                                                (3, 6, 9, []),
                                                (4, 8, 16, [(31, 32, 33),
                                                            (41, 42, 43),
                                                            (51, 52, 53),
                                                            (61, 62, 63)])])
Exemplo n.º 3
0
def simple_object(simple_array):
    # add sequence and children for each column
    name = 'nameless'
    dataset = DatasetType(name)
    seq = dataset['sequence'] = SequenceType('sequence')
    for var in simple_array.dtype.names:
        seq[var] = BaseType(var)

    obj = IterData([(0, 1, 10.), (1, 2, 20.), (2, 3, 30.), (3, 4, 40.),
                    (4, 5, 50.), (5, 6, 60.), (6, 7, 70.), (7, 8, 80.)], seq)
    return obj
Exemplo n.º 4
0
def nested_object(nested_data):
        name = 'nameless'
        dataset = DatasetType(name)
        seq = dataset['nested'] = SequenceType('nested')
        for var in ['a', 'b', 'c']:
            seq[var] = BaseType(var)
        seq['d'] = SequenceType('d')
        for var in ['e', 'f', 'g']:
            seq['d'][var] = BaseType(var)

        nested = IterData(nested_data, seq)
        return nested
Exemplo n.º 5
0
    def setUp(self):
        """Create a flat IterData."""
        template = SequenceType("a")
        template["b"] = BaseType("b")
        template["c"] = BaseType("c")
        template["d"] = BaseType("d")
        self.data = IterData([(1, 2, 3), (4, 5, 6)], template)

        self.array = np.array(
            np.rec.fromrecords([
                (1, 2, 3),
                (4, 5, 6),
            ], names=["b", "c", "d"]))
Exemplo n.º 6
0
def unpack_children(stream, template):
    """Unpack children from a structure, returning their data."""
    cols = list(template.children()) or [template]

    out = []
    for col in cols:
        # sequences and other structures
        if isinstance(col, SequenceType):
            out.append(IterData(list(unpack_sequence(stream, col)), col))
        elif isinstance(col, StructureType):
            out.append(tuple(unpack_children(stream, col)))

        # unpack arrays
        elif col.shape:
            n = np.fromstring(stream.read(4), ">I")[0]
            count = col.dtype.itemsize * n
            if col.dtype.char in "SU":
                data = []
                for _ in range(n):
                    k = np.fromstring(stream.read(4), ">I")[0]
                    data.append(stream.read(k))
                    stream.read(-k % 4)
                out.append(np.array(text_type(data), dtype='S'))

            else:
                stream.read(4)  # read additional length
                out.append(
                    np.fromstring(stream.read(count),
                                  col.dtype).reshape(col.shape))
                if col.dtype.char == "B":
                    stream.read(-n % 4)

        # special types: strings and bytes
        elif col.dtype.char in 'SU':
            n = np.fromstring(stream.read(4), '>I')[0]
            out.append(stream.read(n).decode('ascii'))
            stream.read(-n % 4)
        elif col.dtype.char == 'B':
            data = np.fromstring(stream.read(1), col.dtype)[0]
            stream.read(3)
            out.append(data)

        # usual data
        else:
            out.append(
                np.fromstring(stream.read(col.dtype.itemsize), col.dtype)[0])

    return out
Exemplo n.º 7
0
    def setUp(self):
        self.simple_array = np.array([
            (0, 1, 10.),
            (1, 2, 20.),
            (2, 3, 30.),
            (3, 4, 40.),
            (4, 5, 50.),
            (5, 6, 60.),
            (6, 7, 70.),
            (7, 8, 80.),
        ],
                                     dtype=[('byte', 'b'), ('int', 'i4'),
                                            ('float', 'f4')])

        self.simple_object = IterData(("simple", ("byte", "int", "float")),
                                      [(0, 1, 10.), (1, 2, 20.), (2, 3, 30.),
                                       (3, 4, 40.), (4, 5, 50.), (5, 6, 60.),
                                       (6, 7, 70.), (7, 8, 80.)])
Exemplo n.º 8
0
                                                      ('int', 'i4'),
                                                      ('float', 'f4')])

# A nested sequence.
NestedSequence = DatasetType("NestedSequence")
NestedSequence["location"] = SequenceType("location")
NestedSequence["location"]["lat"] = BaseType("lat")
NestedSequence["location"]["lon"] = BaseType("lon")
NestedSequence["location"]["elev"] = BaseType("elev")
NestedSequence["location"]["time_series"] = SequenceType("time_series")
NestedSequence["location"]["time_series"]["time"] = BaseType("time")
NestedSequence["location"]["time_series"]["slp"] = BaseType("slp")
NestedSequence["location"]["time_series"]["wind"] = BaseType("wind")
NestedSequence["location"].data = IterData([
    (1, 1, 1, [(10, 11, 12), (21, 22, 23)]),
    (2, 4, 4, [(15, 16, 17)]),
    (3, 6, 9, []),
    (4, 8, 16, [(31, 32, 33), (41, 42, 43), (51, 52, 53), (61, 62, 63)]),
], NestedSequence.location)

# A simple array with bytes, strings and shorts. These types require special
# encoding for the DODS response.
SimpleArray = DatasetType("SimpleArray")
SimpleArray["byte"] = BaseType("byte", np.arange(5, dtype="b"))
SimpleArray["string"] = BaseType("string", np.array(["one", "two"]))
SimpleArray["short"] = BaseType("short", np.array(1, dtype="h"))

DODS = os.path.join(os.path.dirname(__file__),
                    'rainfall_time_malaysia.cdp.dods')
DAS = os.path.join(os.path.dirname(__file__), 'rainfall_time_malaysia.cdp.das')
dapper = open_file(DODS, DAS)