Esempio n. 1
0
 def test_tag(self):
     parser = letter.many().concat().tag("word")
     self.assertEqual(parser.sep_by(string(",")).parse("this,is,a,list"),
                      [("word", "this"),
                       ("word", "is"),
                       ("word", "a"),
                       ("word", "list")])
Esempio n. 2
0
    def test_many(self):
        letters = letter.many()
        self.assertEqual(letters.parse('x'), ['x'])
        self.assertEqual(letters.parse('xyz'), ['x', 'y', 'z'])
        self.assertEqual(letters.parse(''), [])

        self.assertRaises(ParseError, letters.parse, '1')
Esempio n. 3
0
 def test_tag_map_dict(self):
     parser = seq(letter.tag("first_letter"),
                  letter.many().concat().tag("remainder")).map(dict)
     self.assertEqual(parser.parse("Hello"), {
         'first_letter': 'H',
         'remainder': 'ello'
     })
Esempio n. 4
0
    def test_many(self):
        letters = letter.many()
        self.assertEqual(letters.parse('x'), ['x'])
        self.assertEqual(letters.parse('xyz'), ['x', 'y', 'z'])
        self.assertEqual(letters.parse(''), [])

        self.assertRaises(ParseError, letters.parse, '1')
Esempio n. 5
0
    def test_mark(self):
        parser = (letter.many().mark() << string("\n")).many()

        lines = parser.parse("asdf\nqwer\n")

        self.assertEqual(len(lines), 2)

        (start, letters, end) = lines[0]
        self.assertEqual(start, (0, 0))
        self.assertEqual(letters, ['a', 's', 'd', 'f'])
        self.assertEqual(end, (0, 4))

        (start, letters, end) = lines[1]
        self.assertEqual(start, (1, 0))
        self.assertEqual(letters, ['q', 'w', 'e', 'r'])
        self.assertEqual(end, (1, 4))
Esempio n. 6
0
    def test_mark(self):
        parser = (letter.many().mark() << string("\n")).many()

        lines = parser.parse("asdf\nqwer\n")

        self.assertEqual(len(lines), 2)

        (start, letters, end) = lines[0]
        self.assertEqual(start, (0, 0))
        self.assertEqual(letters, ['a', 's', 'd', 'f'])
        self.assertEqual(end, (0, 4))

        (start, letters, end) = lines[1]
        self.assertEqual(start, (1, 0))
        self.assertEqual(letters, ['q', 'w', 'e', 'r'])
        self.assertEqual(end, (1, 4))
Esempio n. 7
0
 def test_concat(self):
     parser = letter.many().concat()
     self.assertEqual(parser.parse(''), '')
     self.assertEqual(parser.parse('abc'), 'abc')
Esempio n. 8
0
from parsy import string, letter, seq, decimal_digit

bag_name = letter.many().concat() + string(' ') + letter.many().concat()
child_bag = seq(
    decimal_digit.map(int) << string(' '), bag_name <<
    (string(' bags') | string(' bag'))).map(tuple)
child_bag_separator = string(', ') | string('.')
child_bags = string('no other bags.').result(
    set()) | (child_bag << child_bag_separator).many().map(set)

bag_rule = seq(bag_name << string(' bags contain '), child_bags).map(tuple)

bag_rules = (bag_rule << string('\n')).many().map(dict)


def get_children_only(child_set):
    return {child for amount, child in child_set}


def get_parent_bags(input_children, par_child_dict):
    return {
        parent
        for parent, children in par_child_dict.items()
        if any((child in get_children_only(children)
                for child in input_children))
    }


def get_all_parent_bags(input_children, par_child_dict):
    new_bags = input_children.copy()
    all_bags = input_children.copy()