def test_to_str(self): # tests the methods .to_str with expandattrs and expandvals off root = n.Node('root') a = n.Node('a', dict(zz='ZZ'), text="A") b = n.Node('b') x1 = n.Node('x1') x2 = n.Node('x2') root.append(a) root.append(b) root.a.append(x1) root.a.append(x2) self.assertEqual(root.to_str(expandvals=False, expandattrs=False), '''\ root a{zz} x1 x2 b ''') self.assertEqual(root.to_str(expandvals=True, expandattrs=False), '''\ root a{zz} A x1 x2 b ''') self.assertEqual(root.to_str(), '''\ root a{zz='ZZ'} A x1 x2 b ''')
def test_reserved_name(self): # there are four reserved names: tag, attrib, text, nodes # this is an example of what happens for 'tag' node = n.Node('tag') root = n.Node('root', nodes=[node]) self.assertEqual(root.tag, 'root') # not node, use __getattr__ self.assertEqual(root.__getattr__('tag'), node)
def test_getitem(self): # test the __getitem__ method nodes = [n.Node('a', dict(z='Z')), n.Node('b')] root = n.Node('root', nodes=nodes) self.assertEqual(root.a['z'], 'Z') self.assertEqual(root[0], nodes[0]) self.assertEqual(root[1], nodes[1]) self.assertEqual(list(root), nodes)
def serialize(self, data): """ Serialize a collection of ground motion fields to XML. :param data: An iterable of "GMF set" objects. Each "GMF set" object should: * have an `investigation_time` attribute * have an `stochastic_event_set_id` attribute * be iterable, yielding a sequence of "GMF" objects Each "GMF" object should: * have an `imt` attribute * have an `sa_period` attribute (only if `imt` is 'SA') * have an `sa_damping` attribute (only if `imt` is 'SA') * have a `rupture_id` attribute (to indicate which rupture contributed to this gmf) * be iterable, yielding a sequence of "GMF node" objects Each "GMF node" object should have: * a `gmv` attribute (to indicate the ground motion value * `lon` and `lat` attributes (to indicate the geographical location of the ground motion field) """ gmf_set_nodes = [] for gmf_set in data: gmf_set_node = node.Node('gmfSet') if gmf_set.investigation_time: gmf_set_node['investigationTime'] = str( gmf_set.investigation_time) gmf_set_node['stochasticEventSetId'] = str( gmf_set.stochastic_event_set_id) gmf_set_node.nodes = gen_gmfs(gmf_set) gmf_set_nodes.append(gmf_set_node) gmf_container = node.Node('gmfCollection') gmf_container[SM_TREE_PATH] = self.sm_lt_path gmf_container[GSIM_TREE_PATH] = self.gsim_lt_path gmf_container.nodes = gmf_set_nodes with open(self.dest, 'w') as dest: node.node_to_nrml(gmf_container, dest)
def gen_gmfs(gmf_set): """ Generate GMF nodes from a gmf_set :param gmf_set: a sequence of GMF objects with attributes imt, sa_period, sa_damping, rupture_id and containing a list of GMF nodes with attributes gmv and location. The nodes are sorted by lon/lat. """ for gmf in gmf_set: gmf_node = node.Node('gmf') gmf_node['IMT'] = gmf.imt if gmf.imt == 'SA': gmf_node['saPeriod'] = str(gmf.sa_period) gmf_node['saDamping'] = str(gmf.sa_damping) tag = gmf.rupture_id if tag: gmf_node['ruptureId'] = tag sorted_nodes = sorted(gmf) gmf_node.nodes = (node.Node( 'node', dict(gmv=str(n.gmv), lon=str(n.location.x), lat=str(n.location.y))) for n in sorted_nodes) yield gmf_node
def serialize(self, data): """ Serialize a collection of ground motion fields to XML. :param data: An iterable of "GMFScenario" objects. Each "GMFScenario" object should: * have an `imt` attribute * have an `sa_period` attribute (only if `imt` is 'SA') * have an `sa_damping` attribute (only if `imt` is 'SA') * be iterable, yielding a sequence of "GMF node" objects Each "GMF node" object should have: * an `gmv` attribute (to indicate the ground motion value * `lon` and `lat` attributes (to indicate the geographical location of the ground motion field """ gmfset = node.Node('gmfSet', {}, nodes=gen_gmfs(data)) with open(self.dest, 'w') as dest: node.node_to_nrml(gmfset, dest)
def test_setitem(self): root = n.Node('root') root['a'] = 'A' self.assertEqual(root['a'], 'A') self.assertEqual(root.attrib['a'], 'A')
def test_can_pickle(self): node = n.Node('tag') self.assertEqual(cPickle.loads(cPickle.dumps(node)), node)