コード例 #1
0
def test_Word():
    parseString = Martel.Word().make_parser().parseString
    for term in ("Andrew", "Dalke", "was_here", "test12", "12df"):
        must_parse("Word", parseString, term)
    for term in ("*", "", "this-that"):
        must_not_parse("not Word", parseString, term)

    has_group(Martel.Word("spam", {"x": "fly"}), "_", "spam", "fly")
    has_group(Martel.Word("eggs", {"x": "boy"}), "9", "eggs", "boy")
    has_no_group(Martel.Word(), "__")
コード例 #2
0
def test1():
    fields = (
        ["Andrew", "Dalke", "12"],
        ["Liz", "Nelson", "22"],
        ["Mandrake", "Moose", "23"],
        ["Lisa", "Marie", "91"],
    )
    text = ""
    for line in fields:
        text = text + string.join(line, " ") + "\n"

    format = Martel.Rep1(
                 Martel.Group("line",
                     Martel.Word("name", {"type": "first"}) + \
                     Martel.Spaces() + \
                     Martel.Word("name", {"type": "last"}) + \
                     Martel.Spaces() + \
                     Martel.Integer("age") + \
                     Martel.AnyEol()
                              ))
    iterator = format.make_iterator("line")
    i = 0
    for record in iterator.iterateString(text, LAX.LAX()):
        assert record["name"] == fields[i][:2], (record["name"], fields[i][:2])
        assert record["age"] == fields[i][2:3], (record["age"], fields[i][2:3])
        i = i + 1

    i = 0
    for record in iterator.iterateString(text, LAX.LAXAttrs()):
        assert [x[0] for x in record["name"]] == fields[i][:2], \
                ([x[0] for x in record["name"]], fields[i][:2])
        assert [x[0] for x in record["age"]] == fields[i][2:3], \
               ([x[0] for x in record["age"]], fields[i][2:3])
        assert record["name"][0][1]["type"] == "first"
        assert record["name"][1][1]["type"] == "last"
        assert record["age"][0][1].keys() == []
        i = i + 1