<commit_before>from typpete.src.stmt_inferrer import *
from typpete.src.import_handler import ImportHandler
import typpete.src.config as config
from z3 import Optimize

import os
import time
import astunparse
import sys


def configure_inference(flags):
    for flag in flags:
        flag_assignment = flag[2:]
        try:
            flag_name, flag_value = flag_assignment.split('=')
        except ValueError:
            print("Invalid flag assignment {}".format(flag_assignment))
            continue
        if flag_name in config.config:
            config.config[flag_name] = flag_value == 'True'
        else:
            print("Invalid flag {}. Ignoring.".format(flag_name))

def run_inference(file_path=None):
    if not file_path:
        try:
            file_path = sys.argv[1]
        except:
            print("Please specify the python file")
            return

    start_time = time.time()
    configure_inference([flag for flag in sys.argv[2:] if flag.startswith("--")])

    base_folder = '/'.join(file_path.split('/')[:-1])
    file_name = file_path.split('/')[-1]
    if file_name.endswith('.py'):
        file_name = file_name[:-3]

    class_type_params = None
    type_params = {'make_object': [1]}

    t = ImportHandler.get_module_ast(file_name, base_folder)

    solver = z3_types.TypesSolver(t, base_folder=base_folder, type_params=type_params,
                                  class_type_params=class_type_params)

    context = Context(t, t.body, solver)
    context.type_params = solver.config.type_params
    context.class_type_params = solver.config.class_type_params
    solver.infer_stubs(context, infer)

    for stmt in t.body:<commit_msg>Take type params from command line<commit_after>from typpete.src.stmt_inferrer import *
from typpete.src.import_handler import ImportHandler
import typpete.src.config as config
from z3 import Optimize

import os
import time
import astunparse
import sys


def configure_inference(flags):
    class_type_params = None
    func_type_params = None
    for flag in flags:
        flag_assignment = flag[2:]
        try:
            flag_name, flag_value = flag_assignment.split('=')
        except ValueError:
            print("Invalid flag assignment {}".format(flag_assignment))
            continue
        # func_type_params=make_object,1,d,2
        if flag_name == 'func_type_params':
            if func_type_params is None:
                func_type_params = {}
            flag_value = flag_value.split(',')
            for i in range(0, len(flag_value), 2):
                func_name = flag_value[i]
                count = int(flag_value[i + 1])
                type_vars = ['{}{}'.format(func_name, i) for i in range(count)]
                func_type_params[func_name] = type_vars
        elif flag_name == 'class_type_params':
            if class_type_params is None:
                class_type_params = {}
            flag_value = flag_value.split(',')
            for i in range(0, len(flag_value), 2):
                cls_name = flag_value[i]
                count = int(flag_value[i + 1])
                type_vars = ['{}{}'.format(cls_name, i) for i in range(count)]
                class_type_params[cls_name] = type_vars
        elif flag_name in config.config:
            config.config[flag_name] = flag_value == 'True'
        else:
            print("Invalid flag {}. Ignoring.".format(flag_name))
    return class_type_params, func_type_params

def run_inference(file_path=None):
    if not file_path:
        try:
            file_path = sys.argv[1]
        except:
            print("Please specify the python file")
            return

    start_time = time.time()
    class_type_params, func_type_params = configure_inference([flag for flag in sys.argv[2:] if flag.startswith("--")])

    base_folder = '/'.join(file_path.split('/')[:-1])
    file_name = file_path.split('/')[-1]
    if file_name.endswith('.py'):
        file_name = file_name[:-3]

    t = ImportHandler.get_module_ast(file_name, base_folder)

    solver = z3_types.TypesSolver(t, base_folder=base_folder, type_params=func_type_params,
                                  class_type_params=class_type_params)

    context = Context(t, t.body, solver)
    context.type_params = solver.config.type_params
    context.class_type_params = solver.config.class_type_params
    solver.infer_stubs(context, infer)

    for stmt in t.body:
{'fraction_non_alphanumeric': 0.10288439700204406, 'fraction_numerical': 0.005677946854417443, 'mean_word_length': 2.574675324675325, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105811', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1334, 'n_words': 293}