<commit_before>import sys, inspect, ast, re
from ast import *
from meta.asttools import print_ast
from funcy import re_find

from .transform import transform_function


__all__ = ('patterns', 'Mismatch')


class Mismatch(Exception):
    pass


def patterns(func):
    empty_argspec = inspect.ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
    assert inspect.getargspec(func) == empty_argspec, 'Pattern function should not have arguments'

    # TODO: make it not as weird and dirty
    func.__globals__['Mismatch'] = Mismatch

    tree = get_ast(func)
    transform_function(tree.body[0])
    return compile_func(func, tree)


def compile_func(func, tree):
    def _compile_func():
        # TODO: this one doesn't work quite well, handle it
        ast.fix_missing_locations(tree)
        code = compile(tree, func_file(func), 'single')
        exec code in func.__globals__, context

    def wrap_func(func_tree, arg_names):
        args = [Name(ctx=Param(), id=name) for name in arg_names]<commit_msg>Added TODO about locals/globals name clash<commit_after>import sys, inspect, ast, re
from ast import *
from meta.asttools import print_ast
from funcy import re_find

from .transform import transform_function


__all__ = ('patterns', 'Mismatch')


class Mismatch(Exception):
    pass


def patterns(func):
    empty_argspec = inspect.ArgSpec(args=[], varargs=None, keywords=None, defaults=None)
    assert inspect.getargspec(func) == empty_argspec, 'Pattern function should not have arguments'

    # TODO: make it not as weird and dirty
    func.__globals__['Mismatch'] = Mismatch

    tree = get_ast(func)
    transform_function(tree.body[0])
    return compile_func(func, tree)


# TODO: wrap referenced globals somehow to prevent name conflicts with global/local vars
def compile_func(func, tree):
    def _compile_func():
        # TODO: this one doesn't work quite well, handle it
        ast.fix_missing_locations(tree)
        code = compile(tree, func_file(func), 'single')
        exec code in func.__globals__, context

    def wrap_func(func_tree, arg_names):
        args = [Name(ctx=Param(), id=name) for name in arg_names]
{'fraction_non_alphanumeric': 0.09656453110492108, 'fraction_numerical': 0.0009285051067780873, 'mean_word_length': 4.094562647754137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1030100', 'n_tokens_mistral': 660, 'n_tokens_neox': 639, 'n_words': 200}