Exemplo n.º 1
0
def hdf5write(h5file, obj, root=''):
    """
    Write a generic object serializable to a Node-like object into a :class:
    `openquake.baselib.hdf5.File`
    """
    dic = node_to_dict(obj_to_node(obj))
    h5file.save(dic, root)
Exemplo n.º 2
0
    def test_dict(self):
        # convertion to and from JSON strings
        input_dict = {
            "attrib": {},
            "nodes": [{
                "attrib": {},
                "tag": "a",
                "text": "A"
            }],
            "tag": "root",
            "text": None
        }
        expected_dict = {
            "nodes": [{
                "tag": "a",
                "text": "A"
            }],
            "tag": "root",
        }
        node = n.node_from_dict(input_dict)
        output_dict = n.node_to_dict(node)
        self.assertEqual(expected_dict, output_dict)

        # test deepcopy
        self.assertEqual(node, copy.deepcopy(node))
Exemplo n.º 3
0
def hdf5write(h5file, obj, root=''):
    """
    Write a generic object serializable to a Node-like object into a :class:
    `openquake.baselib.hdf5.File`
    """
    dic = node_to_dict(obj_to_node(obj))
    h5file.save(dic, root)
Exemplo n.º 4
0
    def test_dict(self):
        # convertion to and from JSON strings
        input_dict = {
            "attrib": {},
            "nodes": [
                {
                    "attrib": {},
                    "tag": "a",
                    "text": "A"
                    }
                ],
            "tag": "root",
            "text": None
            }
        expected_dict = {
            "nodes": [
                {
                    "tag": "a",
                    "text": "A"
                    }
                ],
            "tag": "root",
            }
        node = n.node_from_dict(input_dict)
        output_dict = n.node_to_dict(node)
        self.assertEqual(expected_dict, output_dict)

        # test deepcopy
        self.assertEqual(node, copy.deepcopy(node))
Exemplo n.º 5
0
 def test_dict(self):
     # convertion to and from JSON strings
     d1 = {'root': {'a': 'A'}}
     d2 = {'root': {'a': 'A', 'attrib': {'x': 1, 'y': 2}}}
     d3 = {
         'root': [{
             'a': 'A',
             'attrib': {
                 'x': 1,
                 'y': 2
             }
         }, {
             'a': 'A',
             'attrib': {
                 'x': 3,
                 'y': 4
             }
         }]
     }
     for input_dict in [d1, d2, d3]:
         node = n.node_from_dict(input_dict)
         output_dict = n.node_to_dict(node)
         self.assertEqual(input_dict, output_dict)
         # test deepcopy
         self.assertEqual(node, copy.deepcopy(node))
Exemplo n.º 6
0
def tomldump(obj, fileobj=None):
    """
    Write a generic serializable object in TOML format
    """
    dic = node_to_dict(obj_to_node(obj))
    if fileobj is None:
        return toml.dumps(dic)
    toml.dump(dic, fileobj)
Exemplo n.º 7
0
def convert_xml_hdf5(input_file, output_file):
    with hdf5.File(output_file, 'w') as out:
        inp = nrml.read(input_file)
        if inp['xmlns'].endswith('nrml/0.4'):  # old version
            d = os.path.dirname(input_file) or '.'
            raise ValueError('Please upgrade with `oq upgrade_nrml %s`' % d)
        elif inp['xmlns'].endswith('nrml/0.5'):  # current version
            sm = inp.sourceModel
        else:  # not a NRML
            raise ValueError('Unknown NRML:' % inp['xmlns'])
        out.save(node.node_to_dict(sm))
    return output_file
Exemplo n.º 8
0
def convert_xml_hdf5(input_file, output_file):
    with hdf5.File(output_file, 'w') as out:
        inp = nrml.read(input_file)
        if inp['xmlns'].endswith('nrml/0.4'):  # old version
            d = os.path.dirname(input_file) or '.'
            raise ValueError('Please upgrade with `oq upgrade_nrml %s`' % d)
        elif inp['xmlns'].endswith('nrml/0.5'):  # current version
            sm = inp.sourceModel
        else:  # not a NRML
            raise ValueError('Unknown NRML:' % inp['xmlns'])
        out.save(node.node_to_dict(sm))
    return output_file
Exemplo n.º 9
0
    def test_duplicates_ok(self):
        # sequential duplicate tags
        xmlfile = io.StringIO(u"""\
<nonParametric>
<singlePlaneRupture>1</singlePlaneRupture>
<singlePlaneRupture>3</singlePlaneRupture>
<multiPlanesRupture>2</multiPlanesRupture>
</nonParametric>
""")
        node = n.node_from_xml(xmlfile)
        dic = n.node_to_dict(node)
        self.assertEqual(dic, {
            'nonParametric':
            {'singlePlaneRupture': ['1', '3'], 'multiPlanesRupture': '2'}})
Exemplo n.º 10
0
def convert_xml_hdf5(input_file, output_file):
    with hdf5.File(output_file, 'w') as out:
        inp = nrml.read(input_file)
        if inp['xmlns'].endswith('nrml/0.4'):  # old version
            d = os.path.dirname(input_file) or '.'
            raise ValueError('Please upgrade with `oq upgrade_nrml %s`' % d)
        elif inp['xmlns'].endswith('nrml/0.5'):  # current version
            sm = inp.sourceModel
        else:  # not a NRML
            raise ValueError('Unknown NRML:' % inp['xmlns'])
        for group in sm:
            for src in group:  # make the trt implicit
                del src.attrib['tectonicRegion']
        out.save(node.node_to_dict(sm))
    return output_file
Exemplo n.º 11
0
    def test_duplicates_bad(self):
        # this is the current behavior: the first rupture is lost
        # we may want to raise an error instead
        xmlfile = io.StringIO(u"""\
<nonParametric>
<singlePlaneRupture>1</singlePlaneRupture>
<multiPlanesRupture>2</multiPlanesRupture>
<singlePlaneRupture>3</singlePlaneRupture>
</nonParametric>
""")
        node = n.node_from_xml(xmlfile)
        dic = n.node_to_dict(node)
        self.assertEqual(dic, {
            'nonParametric':
            {'singlePlaneRupture': '3', 'multiPlanesRupture': '2'}})
Exemplo n.º 12
0
 def convert_mfdist(self, node):
     with context(self.fname, node):
         [mfd_node] = [
             subnode for subnode in node if subnode.tag.endswith(KNOWN_MFDS)
         ]
     return str(node_to_dict(mfd_node))