Пример #1
0
 def setUp(self):
     self.prog = lightgrep.Program(0)
     self.addCleanup(self.prog.close)
     self.fsm = lightgrep.Fsm(0)
     self.addCleanup(self.fsm.close)
     self.pat = lightgrep.Pattern()
     self.addCleanup(self.pat.close)
Пример #2
0
    def test_size(self):
        with lightgrep.Program(0) as prog:
            with lightgrep.Pattern() as pat:
                with lightgrep.Fsm(0) as fsm:
                    pat.parse("a+b", lightgrep.KeyOpts())
                    fsm.add_pattern(prog, pat, 'UTF-8', 42)
                    prog.compile(fsm, lightgrep.ProgOpts())

            self.assertTrue(prog.size() > 0)
Пример #3
0
    def setUp(self):
        self.prog = lightgrep.Program(0)
        self.addCleanup(self.prog.close)

        with lightgrep.Pattern() as pat:
            with lightgrep.Fsm(0) as fsm:
                pat.parse("a+b", lightgrep.KeyOpts())
                fsm.add_pattern(self.prog, pat, 'UTF-8', 42)
                self.prog.compile(fsm, lightgrep.ProgOpts())

        self.ctx = lightgrep.Context(self.prog, lightgrep.CtxOpts())
        self.addCleanup(self.ctx.close)
Пример #4
0
    def test_write_read(self):
        with lightgrep.Program(0) as prog1:
            with lightgrep.Pattern() as pat:
                with lightgrep.Fsm(0) as fsm:
                    pat.parse("a+b", lightgrep.KeyOpts())
                    fsm.add_pattern(prog1, pat, 'UTF-8', 42)
                    prog1.compile(fsm, lightgrep.ProgOpts())

            buf = prog1.write()

            with lightgrep.Program(buf) as prog2:
                self.assertEqual(prog2.count(), prog1.count())
                self.assertEqual(prog2.size(), prog1.size())
Пример #5
0
    print(f"{withHitCount} hits found")
    for h in withHits.Hits:
        print(
            f"hit at [{h['start']},{h['end']}) on keyindex {h['keywordIndex']}, pattern is '{h['pattern']}' with encoding chain '{h['encChain']}'"
        )
        # hBytes = searchData[h['start']:h['end']]
        # hText = hBytes.decode("utf-8")
        # print(f"    hit text: '{hText}'")
    withHits.reset()

# Bad input results in exceptions
print("---------------------------")
print("Exception due to a malformed pattern")

with lg.Pattern() as pat:
    try:
        pat.parse('+++', lg.KeyOpts())
    except RuntimeError as e:
        print(e)

print("---------------------------")
print("Results creating program and pattern map separately from context")
# Creating the program and pattern map separately from the context
with lg.Program(0) as prog:
    with lg.Pattern() as pat:
        with lg.Fsm(0) as fsm:
            fsm.add_patterns(prog, pat, keys)
            prog.compile(fsm, lg.ProgOpts())

    myHits = lg.HitAccumulator()
Пример #6
0
 def setUp(self):
     self.pat = lightgrep.Pattern()
     self.addCleanup(self.pat.close)
Пример #7
0
 def test_close_unused(self):
     # test that closing an unused Pattern doesn't throw
     pat = lightgrep.Pattern()
     pat.close()