def test_xml(self): xml_text = """<?xml version="1.0"?> <employee id="0034404"> <name first="john" last="doe"></name> <project lead="Foo"> <name>GAaaS</name> </project> <project name="Data Algebra"> <lead>Bar</lead> </project> </employee> """ xml_set = import_xml(io.StringIO(xml_text)) json_text = """ { "employee": { "id": "0034404", "name": { "first": "john", "last": "doe" }, "project": [ { "lead": "Foo", "name": "GAaaS" }, { "name": "Data Algebra", "lead": "Bar" } ] } } """ json_set = import_json(io.StringIO(json_text)) self.assertEqual(json_set, xml_set) from algebraixlib.algebras.relations import compose c = next(iter(xml_set)) self.assertEqual(Atom('employee'), c.left) self.assertEqual(4, len(c.right)) cid = compose(c.right, Set(Couplet('id', 'id'))) self.assertEqual(cid, Set(Couplet('id', '0034404'))) name = next(iter(compose(c.right, Set(Couplet('name', 'name'))))) self.assertEqual(name.right, Set([Couplet('first', 'john'), Couplet('last', 'doe')])) projects = compose(c.right, Set(Couplet('project', 'project'))) self.assertEqual(2, len(projects)) ep = Set([Couplet('project', Set([Couplet('name', x['name']), Couplet('lead', x['lead'])])) for x in [{'name': 'GAaaS', 'lead': 'Foo'}, {'lead': 'Bar', 'name': 'Data Algebra'}]]) self.assertEqual(projects, ep)
# Similarly, a binary operation like couplets.composition can be extended by applying to every element # of the cross product of two relations. Notice that couplets.composition is a partial binary # operation (given two legitimate Couplets, it may be undefined). When couplets.compose(a, b) is # not defined, it simply isn't included in the membership of the resulting relation. By extending, # we have turned composition into a full binary operation in the power set algebra. second_relation = Set(Couplet('one', 'a'), Couplet('won', 'a'), Couplet('four', 'd')) composed_relation = extension.binary_extend(first_relation, second_relation, couplets.compose) empty_relation = extension.binary_extend(second_relation, first_relation, couplets.compose) # empty relation; still not commutative print("composed_relation:", composed_relation) print("empty_relation:", empty_relation) # These extended operations are defined as functions in the relations module. transpose_is_same = transposed_relation == relations.transpose(first_relation) compose_is_same = composed_relation == relations.compose(first_relation, second_relation) print("transpose_is_same:", transpose_is_same) print("compose_is_same:", compose_is_same) # The following docstring specifies a CSV table of words in various languages, with their meaning # normalized to English. vocab_csv = """word,language,meaning hello,English,salutation what's up,English,salutation hola,Spanish,salutation world,English,earth mundo,Spanish,earth gallo,Spanish,rooster Duniy?,Hindi,earth Kon'nichiwa,Japanese,salutation hallo,German,salutation
def test_xml(self): xml_text = """<?xml version="1.0"?> <employee id="0034404"> <name first="john" last="doe"></name> <project lead="Foo"> <name>GAaaS</name> </project> <project name="Data Algebra"> <lead>Bar</lead> </project> </employee> """ xml_set = import_xml(io.StringIO(xml_text)) json_text = """ { "employee": { "id": "0034404", "name": { "first": "john", "last": "doe" }, "project": [ { "lead": "Foo", "name": "GAaaS" }, { "name": "Data Algebra", "lead": "Bar" } ] } } """ json_set = import_json(io.StringIO(json_text)) self.assertEqual(json_set, xml_set) from algebraixlib.algebras.relations import compose c = next(iter(xml_set)) self.assertEqual(Atom('employee'), c.left) self.assertEqual(4, len(c.right)) cid = compose(c.right, Set(Couplet('id', 'id'))) self.assertEqual(cid, Set(Couplet('id', '0034404'))) name = next(iter(compose(c.right, Set(Couplet('name', 'name'))))) self.assertEqual( name.right, Set([Couplet('first', 'john'), Couplet('last', 'doe')])) projects = compose(c.right, Set(Couplet('project', 'project'))) self.assertEqual(2, len(projects)) ep = Set([ Couplet( 'project', Set([Couplet('name', x['name']), Couplet('lead', x['lead'])])) for x in [{ 'name': 'GAaaS', 'lead': 'Foo' }, { 'lead': 'Bar', 'name': 'Data Algebra' }] ]) self.assertEqual(projects, ep)
# binary operation (given two legitimate Couplets, it may be undefined). When couplets.compose(a, # b) is not defined, it simply isn't included in the membership of the resulting relation. By # extending, we have turned composition into a full binary operation in the power set algebra. second_relation = Set(Couplet('one', 'a'), Couplet('won', 'a'), Couplet('four', 'd')) composed_relation = extension.binary_extend(first_relation, second_relation, couplets.compose) empty_relation = extension.binary_extend( second_relation, first_relation, couplets.compose) # empty relation; still not commutative print("composed_relation:", composed_relation) print("empty_relation:", empty_relation) # These extended operations are defined as functions in the relations module. transpose_is_same = transposed_relation == relations.transpose(first_relation) compose_is_same = composed_relation == relations.compose( first_relation, second_relation) print("transpose_is_same:", transpose_is_same) print("compose_is_same:", compose_is_same) # The following docstring specifies a CSV table of words in various languages, with their meaning # normalized to English. vocab_csv = """word,language,meaning hello,English,salutation what's up,English,salutation hola,Spanish,salutation world,English,earth mundo,Spanish,earth gallo,Spanish,rooster Duniy?,Hindi,earth Kon'nichiwa,Japanese,salutation hallo,German,salutation