How to use the executing.Source.for_filename function in executing

To help you get started, we’ve selected a few executing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github alexmojaki / executing / tests / samples / tests.py View on Github external
def assert_qualname(self, func, qn, check_actual_qualname=True):
        qualname = Source.for_filename(__file__).code_qualname(func.__code__)
        self.assertEqual(qn, qualname)
        if PY3 and check_actual_qualname:
            self.assertEqual(qn, func.__qualname__)
        self.assertTrue(qn.endswith(func.__name__))
github alexmojaki / executing / tests / samples / tests.py View on Github external
def test_invalid_python(self):
        path = os.path.join(os.path.dirname(__file__), 'not_code.txt', )
        source = Source.for_filename(path)
        self.assertIsNone(source.tree)
github alexmojaki / executing / tests / test_main.py View on Github external
def check_filename(self, filename):
        print(filename)
        source = Source.for_filename(filename)
        nodes = defaultdict(list)
        for node in ast.walk(source.tree):
            if isinstance(node, (
                    ast.UnaryOp,
                    ast.BinOp,
                    ast.Subscript,
                    ast.Call,
                    ast.Compare,
                    ast.Attribute
            )):
                nodes[node] = []

        code = compile(source.tree, source.filename, 'exec')
        result = list(self.check_code(code, nodes))

        if not re.search(r'^\s*if 0(:| and )', source.text, re.MULTILINE):
github alexmojaki / executing / tests / test_main.py View on Github external
def test_invalid_python(self):
        path = os.path.join(os.path.dirname(__file__), 'not_code.txt', )
        source = Source.for_filename(path)
        self.assertIsNone(source.tree)
github alexmojaki / executing / tests / test_main.py View on Github external
def assert_qualname(self, func, qn, check_actual_qualname=True):
        qualname = Source.for_filename(__file__).code_qualname(func.__code__)
        self.assertEqual(qn, qualname)
        if PY3 and check_actual_qualname:
            self.assertEqual(qn, func.__qualname__)
        self.assertTrue(qn.endswith(func.__name__))
github alexmojaki / heartrate / heartrate / core.py View on Github external
def file_table_context():
        filename = request.args['filename']
        source = Source.for_filename(filename)
        queue = queues[filename]

        highlighted = highlight_ranges(source, frames_matching(filename))
        highlighted = highlight_python_and_ranges(highlighted)
        highlighted_lines = list(enumerate(highlighted.splitlines()))
        
        counters = [
            queue_counter(queue, 2 ** i)
            for i in range(levels + 1)
        ]

        ratios = [
            [
                counter[i + 1] / min(2 ** c, len(queue) or 1)
                * (c + 1) / levels
                for c, counter in enumerate(counters)