# Path: automlk/store.py
# def exists_key_store(key):
#     """
#     checks if a key exists
# 
#     :param key: key of the data
#     :return: True if the key exists else False
#     """
#     if get_use_redis():
#         return rds.exists(str(key))
#     else:
#         return os.path.exists(store_folder + '/' + __clean_key(key) + '.json')
# 
# def get_key_store(key):
#     """
#     retieves value from key in store
# 
#     :param key: key of the data
#     :return: value of the data
#     """
#     if get_use_redis():
#         return json.loads(rds.get(str(key)))
#     else:
#         try:
#             if exists_key_store(key):
#                 return json.load(open(store_folder + '/' + __clean_key(key) + '.json', 'r'))
#             else:
#                 return None
#         except:
#             return None
# 
# def set_key_store(key, value):
#     """
#     sets value to key in store
# 
#     :param key: key of the data
#     :param value: value of the data to store
#     """
#     if get_use_redis():
#         rds.set(str(key), json.dumps(value))
#     else:
#         with open(store_folder + '/' + __clean_key(key) + '.json', 'w') as f:
#             json.dump(value, f)
# 
# def del_key_store(key):
#     """
#     deletes key in store
# 
#     :param key: key of the data
#     :return: None
#     """
#     if get_use_redis():
#         rds.delete(str(key))
#     else:
#         if exists_key_store(key):
#             os.remove(store_folder + '/' + __clean_key(key) + '.json')
# 
# Path: automlk/dataset.py
# def get_dataset_ids():
# def get_dataset_list(include_results=False):
# def get_dataset_status(dataset_id):
# def get_dataset(dataset_id, include_results=False):
# def make_dataset(d):
# def create_dataset_json(dataset_id):
# def create_dataset(name, folder_id, description, source, mode, filename_train, filename_test='', filename_cols='',
#                    filename_submit='', url=''):
# def update_dataset(dataset_id, name, folder_id, description, source, url):
# def update_feature_dataset(dataset_id, name, description, to_keep, col_type, text_ref):
# def update_problem_dataset(dataset_id, problem_type, y_col, metric, other_metrics, val_col, cv_folds, val_col_shuffle,
#                            scan, holdout_ratio, col_submit):
# def reset_dataset(dataset_id):
# def delete_dataset(dataset_id):
# def get_dataset_sample(dataset_id):
#     def __init__(self, dataset_id, name, folder_id, description, source, mode, filename_train,
#                  filename_test, filename_cols, filename_submit, url, creation_date):
#     def initialize_data(self):
#     def update_stats(self, df_train, df_test, df_submit):
#     def update_calc(self):
#     def update_problem(self, problem_type, y_col, metric, other_metrics, val_col, cv_folds, val_col_shuffle, scan,
#                        holdout_ratio, col_submit, best_is_min=True):
#     def update_y_data(self):
#     def save(self, dataset_id):
#     def load(self, load_data, features):
#     def load_calc(self, calc_data):
#     def finalize_creation(self, df_train, df_test, df_submit):
#     def get_data(self, part='train'):
#     def create_folders(self):
#     def __folder(self):
#     def __check_data(self, filename, part='train'):
#     def __read_data(self, filename):
#     def __initialize_features(self, df, df_cols):
#     def update_features(self, df):
#     def __import_data(self, filename, part):
#     def __init__(self, name, raw_type, n_missing, n_unique_values, first_unique_values, description, to_keep,
#                  col_type, text_ref='', n_unique_ratio=0.):
# def get_y_eval(dataset_id):
# class DataSet(object):
# class Feature(object):


import os
import pandas as pd
from .store import exists_key_store, get_key_store, set_key_store, del_key_store
from .dataset import get_dataset_folder, get_dataset
        return None


def exec_feature_engineering(content):
    """
    executes the feature engineering definition from content

    :param content: content of the function
    :return:
    """
    global specific_feature_engineering
    # prepare specific function definition
    if content == '':
        return
    s_indent = " " * 4
    f = 'def specific_feature_engineering(X):\n' + content + '\nreturn X'
    f = f.replace('\n', '\n' + s_indent).replace('\t', s_indent)
    # execute function
    exec(f, globals())


def update_specific_metrics(dataset_id, name, best_is_min, content):
    """
    update the specific metrics content of a dataset

    :param dataset_id: id of the dataset
    :param name: name of the metrics
    :param content: text of the python function (without def)
    :return:
    """

# Path: automlk/store.py
# def exists_key_store(key):
#     """
#     checks if a key exists
# 
#     :param key: key of the data
#     :return: True if the key exists else False
#     """
#     if get_use_redis():
#         return rds.exists(str(key))
#     else:
#         return os.path.exists(store_folder + '/' + __clean_key(key) + '.json')
# 
# def get_key_store(key):
#     """
#     retieves value from key in store
# 
#     :param key: key of the data
#     :return: value of the data
#     """
#     if get_use_redis():
#         return json.loads(rds.get(str(key)))
#     else:
#         try:
#             if exists_key_store(key):
#                 return json.load(open(store_folder + '/' + __clean_key(key) + '.json', 'r'))
#             else:
#                 return None
#         except:
#             return None
# 
# def set_key_store(key, value):
#     """
#     sets value to key in store
# 
#     :param key: key of the data
#     :param value: value of the data to store
#     """
#     if get_use_redis():
#         rds.set(str(key), json.dumps(value))
#     else:
#         with open(store_folder + '/' + __clean_key(key) + '.json', 'w') as f:
#             json.dump(value, f)
# 
# def del_key_store(key):
#     """
#     deletes key in store
# 
#     :param key: key of the data
#     :return: None
#     """
#     if get_use_redis():
#         rds.delete(str(key))
#     else:
#         if exists_key_store(key):
#             os.remove(store_folder + '/' + __clean_key(key) + '.json')
# 
# Path: automlk/dataset.py
# def get_dataset_ids():
# def get_dataset_list(include_results=False):
# def get_dataset_status(dataset_id):
# def get_dataset(dataset_id, include_results=False):
# def make_dataset(d):
# def create_dataset_json(dataset_id):
# def create_dataset(name, folder_id, description, source, mode, filename_train, filename_test='', filename_cols='',
#                    filename_submit='', url=''):
# def update_dataset(dataset_id, name, folder_id, description, source, url):
# def update_feature_dataset(dataset_id, name, description, to_keep, col_type, text_ref):
# def update_problem_dataset(dataset_id, problem_type, y_col, metric, other_metrics, val_col, cv_folds, val_col_shuffle,
#                            scan, holdout_ratio, col_submit):
# def reset_dataset(dataset_id):
# def delete_dataset(dataset_id):
# def get_dataset_sample(dataset_id):
#     def __init__(self, dataset_id, name, folder_id, description, source, mode, filename_train,
#                  filename_test, filename_cols, filename_submit, url, creation_date):
#     def initialize_data(self):
#     def update_stats(self, df_train, df_test, df_submit):
#     def update_calc(self):
#     def update_problem(self, problem_type, y_col, metric, other_metrics, val_col, cv_folds, val_col_shuffle, scan,
#                        holdout_ratio, col_submit, best_is_min=True):
#     def update_y_data(self):
#     def save(self, dataset_id):
#     def load(self, load_data, features):
#     def load_calc(self, calc_data):
#     def finalize_creation(self, df_train, df_test, df_submit):
#     def get_data(self, part='train'):
#     def create_folders(self):
#     def __folder(self):
#     def __check_data(self, filename, part='train'):
#     def __read_data(self, filename):
#     def __initialize_features(self, df, df_cols):
#     def update_features(self, df):
#     def __import_data(self, filename, part):
#     def __init__(self, name, raw_type, n_missing, n_unique_values, first_unique_values, description, to_keep,
#                  col_type, text_ref='', n_unique_ratio=0.):
# def get_y_eval(dataset_id):
# class DataSet(object):
# class Feature(object):


# Path: automlk/specific.py
import os
import pandas as pd
from .store import exists_key_store, get_key_store, set_key_store, del_key_store
from .dataset import get_dataset_folder, get_dataset

        return None


def exec_feature_engineering(content):
    """
    executes the feature engineering definition from content

    :param content: content of the function
    :return:
    """
    global specific_feature_engineering
    # prepare specific function definition
    if content == '':
        return
    s_indent = " " * 4
    f = 'def specific_feature_engineering(X):\n' + content + '\nreturn X'
    f = f.replace('\n', '\n' + s_indent).replace('\t', s_indent)
    # execute function
    exec(f, globals())


def update_specific_metrics(dataset_id, name, best_is_min, content):
    """
    update the specific metrics content of a dataset

    :param dataset_id: id of the dataset
    :param name: name of the metrics
    :param content: text of the python function (without def)
    :return:
    """

    set_key_store('dataset:%s:metrics' % dataset_id, {'name': name, 'best_is_mean': best_is_min})