Ejemplo n.º 1
0
class Profiler(object):
    def __init__(self, file):
        """
        USE with Profiler("myfile.tab"): TO ENABLE PER-PARSER PROFILING
        :param file:
        """
        self.file = File(file).set_extension("tab")
        self.previous_parse = None

    def __enter__(self):
        timing.clear()
        self.previous_parse = ParserElement._parse
        ParserElement._parse = _profile_parse

    def __exit__(self, exc_type, exc_val, exc_tb):
        ParserElement._parse = self.previous_parse
        profile = jx.sort(
            [{
                "parser": text(parser),
                "cache_hits": cache,
                "matches": match,
                "failures": fail,
                "call_count": match + fail + cache,
                "total_parse": parse,
                "total_overhead": all - parse,
                "per_parse": parse / (match + fail),
                "per_overhead": (all - parse) / (match + fail + cache),
            } for parser, (cache, match, fail, parse, all) in timing.items()],
            {"total_parse": "desc"},
        )
        self.file.add_suffix(Date.now().format("%Y%m%d_%H%M%S")).write(
            convert.list2tab(profile))
Ejemplo n.º 2
0
    def test_suffix(self):
        test1 = File("tools/test.json")
        test2 = test1.add_suffix(".backup")
        test3 = test1.add_suffix("-backup")
        test4 = test1.set_name("other")

        self.assertEqual(test1.filename, "tools/test.json")
        self.assertEqual(test2.filename, "tools/test.backup.json")
        self.assertEqual(test3.filename, "tools/test.-backup.json")
        self.assertEqual(test4.filename, "tools/other.json")