Exemple #1
0
 def test_set_data(self):
     """Test that data is propagated to children."""
     var = StructureType("var", value=42, one="1")
     var["one"] = BaseType("one")
     var["two"] = BaseType("two")
     var.data = [10, 20]
     self.assertEqual(var["one"].data, 10)
     self.assertEqual(var["two"].data, 20)
Exemple #2
0
def test_StructureType_set_data():
    """Test that data is propagated to children."""
    var = StructureType("var", value=42, one="1")
    var["one"] = BaseType("one")
    var["two"] = BaseType("two")
    var.data = [10, 20]
    assert (var["one"].data == 10)
    assert (var["two"].data == 20)
Exemple #3
0
def test_StructureType_set_data():
    """Test that data is propagated to children."""
    var = StructureType("var", value=42, one="1")
    var["one"] = BaseType("one")
    var["two"] = BaseType("two")
    var.data = [10, 20]
    assert (var["one"].data == 10)
    assert (var["two"].data == 20)
Exemple #4
0
 def test_set_data(self):
     """Test that data is propagated to children."""
     var = StructureType("var", value=42, one="1")
     var["one"] = BaseType("one")
     var["two"] = BaseType("two")
     var.data = [10, 20]
     self.assertEqual(var["one"].data, 10)
     self.assertEqual(var["two"].data, 20)
Exemple #5
0
def _sequencetype(var):
    # a flat array can be processed one record (or more?) at a time
    if all(isinstance(child, BaseType) for child in var.children()):
        types = []
        position = 0
        for child in var.children():
            if child.dtype.char in 'SU':
                types.append('>I')                  # string length as int
                types.append('|S{%s}' % position)   # string padded to 4n
                position += 1
            else:
                types.append(typemap[child.dtype.char])
        dtype = ','.join(types)
        strings = position > 0

        # array initializations is costy, so we keep a cache here; this will
        # be inneficient if there are many strings of different length only
        cache = {}

        for record in var:
            yield START_OF_SEQUENCE

            if strings:
                out = []
                padded = []
                for value in record:
                    if isinstance(value, string_types):
                        length = len(value) or 1
                        out.append(length)
                        padded.append(length + (-length % 4))
                    out.append(value)
                record = out
                dtype = ','.join(types).format(*padded)

            if dtype not in cache:
                cache[dtype] = np.zeros((1,), dtype=dtype)
            cache[dtype][:] = tuple(record)
            yield cache[dtype].tostring()

        yield END_OF_SEQUENCE

    # nested array, need to process individually
    else:
        # create a template structure
        struct = StructureType(var.name)
        for name in var.keys():
            struct[name] = copy.copy(var[name])

        for record in var:
            yield START_OF_SEQUENCE
            struct.data = record
            for block in dods(struct):
                yield block
        yield END_OF_SEQUENCE
Exemple #6
0
def _sequencetype(var):
    # a flat array can be processed one record (or more?) at a time
    if all(isinstance(child, BaseType) for child in var.children()):
        types = []
        position = 0
        for child in var.children():
            if child.dtype.char in 'SU':
                types.append('>I')  # string length as int
                types.append('|S{%s}' % position)  # string padded to 4n
                position += 1
            else:
                types.append(typemap[child.dtype.char])
        dtype = ','.join(types)
        strings = position > 0

        # array initializations is costy, so we keep a cache here; this will
        # be inneficient if there are many strings of different length only
        cache = {}

        for record in var:
            yield START_OF_SEQUENCE

            if strings:
                out = []
                padded = []
                for value in record:
                    if isinstance(value, string_types):
                        length = len(value) or 1
                        out.append(length)
                        padded.append(length + (-length % 4))
                    out.append(value)
                record = out
                dtype = ','.join(types).format(*padded)

            if dtype not in cache:
                cache[dtype] = np.zeros((1, ), dtype=dtype)
            cache[dtype][:] = tuple(record)
            yield cache[dtype].tostring()

        yield END_OF_SEQUENCE

    # nested array, need to process individually
    else:
        # create a template structure
        struct = StructureType(var.name)
        for name in var.keys():
            struct[name] = copy.copy(var[name])

        for record in var:
            yield START_OF_SEQUENCE
            struct.data = record
            for block in dods(struct):
                yield block
        yield END_OF_SEQUENCE
Exemple #7
0
    def test_copy(self):
        """Test lightweight clone of a structure."""
        original = StructureType("var", value=42, one="1")
        original["one"] = BaseType("one")
        original["two"] = BaseType("two")
        original.data = [10, 20]

        clone = copy.copy(original)

        # note that clones share the same data:
        self.assertIsNot(original, clone)
        self.assertIsNot(original["one"], clone["one"])
        self.assertIs(original["one"].data, clone["one"].data)
        self.assertIsNot(original["two"], clone["two"])
        self.assertIs(original["two"].data, clone["two"].data)

        # test attributes
        self.assertEqual(original.id, clone.id)
        self.assertEqual(original.name, clone.name)
Exemple #8
0
def test_StructureType_copy():
    """Test lightweight clone of a structure."""
    original = StructureType("var", value=42, one="1")
    original["one"] = BaseType("one")
    original["two"] = BaseType("two")
    original.data = [10, 20]

    clone = copy.copy(original)

    # note that clones share the same data:
    assert original is not clone
    assert original["one"] is not clone["one"]
    assert original["one"].data is clone["one"].data
    assert original["two"] is not clone["two"]
    assert original["two"].data is clone["two"].data

    # test attributes
    assert (original.id == clone.id)
    assert (original.name == clone.name)
Exemple #9
0
def test_StructureType_copy():
    """Test lightweight clone of a structure."""
    original = StructureType("var", value=42, one="1")
    original["one"] = BaseType("one")
    original["two"] = BaseType("two")
    original.data = [10, 20]

    clone = copy.copy(original)

    # note that clones share the same data:
    assert original is not clone
    assert original["one"] is not clone["one"]
    assert original["one"].data is clone["one"].data
    assert original["two"] is not clone["two"]
    assert original["two"].data is clone["two"].data

    # test attributes
    assert (original.id == clone.id)
    assert (original.name == clone.name)
Exemple #10
0
    def test_copy(self):
        """Test lightweight clone of a structure."""
        original = StructureType("var", value=42, one="1")
        original["one"] = BaseType("one")
        original["two"] = BaseType("two")
        original.data = [10, 20]

        clone = copy.copy(original)

        # note that clones share the same data:
        self.assertIsNot(original, clone)
        self.assertIsNot(original["one"], clone["one"])
        self.assertIs(original["one"].data, clone["one"].data)
        self.assertIsNot(original["two"], clone["two"])
        self.assertIs(original["two"].data, clone["two"].data)

        # test attributes
        self.assertEqual(original.id, clone.id)
        self.assertEqual(original.name, clone.name)