import pandas as pd
import numpy as np
import os
from .utils import find_files
from .utils import encode
from .utils import last_hour_interpolation

def weather_data_preprocessing(base_path):
    # 导入天气数据
    weather_original_path = base_path + 'data/weather_original/'
    weather_file = weather_original_path + 'nanjing_weather.csv'
    weather_df = pd.read_csv(weather_file)
    weather_df.drop(['站点', '经度', '纬度', '省份', '市', '区（县）'], axis=1, inplace=True)

    # 使用Last Hour Interpolation对weatherDF插值，时间间隔从1h转变为15min
    weather_features = weather_df.columns.values
    post_interpolation = []

    for feature in weather_features[4:]:
        feature_value = weather_df[feature].values
        temp_list = [float(feature_value[0])]
        for i in range(1, len(feature_value)):
            last_hour_value = temp_list[-1]
            for j in range(1, 5):
                temp_list.append(last_hour_interpolation(last_hour_value, feature_value[i], j))
        post_interpolation.append(temp_list)

    for i in range(0, len(post_interpolation)):
        post_interpolation[i].pop()

    weather_df = pd.DataFrame()
    i = 0
    for feature in weather_features[4:]:
        weather_df[feature] = post_interpolation[i][:]
        i = i + 1

    # 由于缺少2017-01-01 00:00:00 的数据，用newWeatherDF尾行填充
    for i in range(4):
        weather_df = weather_df.append(weather_df.iloc[-1:, :], ignore_index=True)

    # 修改中文特征为英文特征
    col_name_dict = {
        '气压': 'barometer',
        '风速': 'windspeed',
        '平均温度': 'temp',
        '相对湿度': 'humidity',
        '降水': 'precipitation',
    }
    weather_df.rename(columns=col_name_dict, inplace=True)

    # 保存newWeatherDF
    print('Saving weather data...')
    weather_path = base_path + 'data/weather/'
    if not os.path.exists(weather_path):
        os.makedirs(weather_path)
    weather_df.to_csv(weather_path + 'new_nanjing_weather.csv', index=False)

    return weather_df

def extract_missing_date(df):
    # 提取user缺失的日期
    cnt = 0
    missing_date_list = []
    date = np.datetime64('2015-01-01T00:00:00.00')
    for i in range(731):
        if (len(df[(df['data_date'] - date < np.timedelta64(1, 'D')) &
                       (df['data_date'] - date >= np.timedelta64(0, 'D'))]) != 96):
            # print(date)
            missing_date_list.append(date)
            cnt = cnt + 1
        date = date + np.timedelta64(1, 'D')
    # TODO: 只提取缺少x天的
    if cnt <= 5:
        pass

    return missing_date_list

def fill_missing_date(df, missing_date_list):
    # 缺失的数据用下周的数据填充
    index = df.index
    print('Number of missing date:', len(missing_date_list))
    for missing_date in missing_date_list:
        date = missing_date + np.timedelta64(7, 'D')
        next_week_day_df = df[(df['data_date']- date < np.timedelta64(1, 'D')) &
                      (df['data_date'] - date >= np.timedelta64(0, 'D'))].copy()
        # print(len(next_week_day_df))
        data_date_list = []
        for m in range(96):
            data_date_list.append(missing_date + np.timedelta64(m * 15, 'm'))
        next_week_day_df['data_date'] = data_date_list
        next_week_day_df['day_of_week'] = next_week_day_df['data_date'].dt.dayofweek
        above_date = missing_date - np.timedelta64(15, 'm')
        # print(above_date)
        above_idx = index[df['data_date'] == above_date][-1]
        above = df.iloc[:above_idx + 1, :]
        below = df.iloc[above_idx + 1:, :]
        # print(len(above), len(below), len(above) + len(below))
        df = pd.concat([above, next_week_day_df, below], ignore_index=True)
        index = df.index

    return df

def get_date_list():
    # 获取2015和2016节假日
    base_date_list = [np.datetime64('2015-01-01T00:00:00.000'),
                      np.datetime64('2015-02-18T00:00:00.000'),
                      np.datetime64('2015-04-04T00:00:00.000'),
                      np.datetime64('2015-05-01T00:00:00.000'),
                      np.datetime64('2015-06-20T00:00:00.000'),
                      np.datetime64('2015-09-27T00:00:00.000'),
                      np.datetime64('2015-10-01T00:00:00.000'),
                      np.datetime64('2016-01-01T00:00:00.000'),
                      np.datetime64('2016-02-07T00:00:00.000'),
                      np.datetime64('2016-04-02T00:00:00.000'),
                      np.datetime64('2016-04-30T00:00:00.000'),
                      np.datetime64('2016-06-09T00:00:00.000'),
                      np.datetime64('2016-09-15T00:00:00.000'),
                      np.datetime64('2016-10-01T00:00:00.000'),
                      ]
    holidays_range_list = [3, 7, 3, 3, 3, 2, 7,
                           3, 7, 3, 3, 3, 3, 7,
                           ]

    holidays_date_list = []
    for i in range(len(base_date_list)):
        temp_list = [base_date_list[i] +  np.timedelta64(j,'D') for j in range(holidays_range_list[i])]
        holidays_date_list.extend(temp_list)

    # 获取2015和2016节假日前后的工作日
    special_workday_date_list = [np.datetime64('2015-01-04T00:00:00.000'),
                                 np.datetime64('2015-02-15T00:00:00.000'),
                                 np.datetime64('2015-02-28T00:00:00.000'),
                                 np.datetime64('2015-10-10T00:00:00.000'),
                                 np.datetime64('2016-02-06T00:00:00.000'),
                                 np.datetime64('2016-02-14T00:00:00.000'),
                                 np.datetime64('2016-06-12T00:00:00.000'),
                                 np.datetime64('2016-09-18T00:00:00.000'),
                                 np.datetime64('2016-10-08T00:00:00.000'),
                                 np.datetime64('2016-10-09T00:00:00.000'),
                                 ]

    return holidays_date_list, special_workday_date_list

def generate_features(load_df, weather_df, holidays_date_list, special_workday_date_list):
    # 添加workday和holiday特征
    cols = ['data_date', 'year_2015', 'year_2016', 'workday_0', 'workday_1', 'holiday_0', 'holiday_1',
            'day_of_week_0', 'day_of_week_1', 'day_of_week_2', 'day_of_week_3', 'day_of_week_4',
            'day_of_week_5', 'day_of_week_6', 'month_sin', 'month_cos', 'day_sin', 'day_cos',
            'hour_sin', 'hour_cos', 'minute_sin', 'minute_cos', 'temp', 'precipitation',
            'humidity', 'barometer', 'windspeed', 'load',
            ]
    day_of_week_list = load_df['day_of_week'].values.tolist()
    workday_list = [0 if (x == 5) | (x == 6) else 1 for x in day_of_week_list]
    index = load_df.index

    for date in special_workday_date_list:
        idx = index[load_df['data_date'] == date][0]
        for i in range(96):
            workday_list[idx + i] = 1
    load_df['workday'] = workday_list
    holiday_list = [0] * 70176

    for date in holidays_date_list:
        idx = index[load_df['data_date'] == date][0]
        for i in range(96):
            holiday_list[idx + i] = 1

    load_df['holiday'] = holiday_list
    load_df['year'] = load_df['data_date'].dt.year
    load_df['month'] = load_df['data_date'].dt.month
    load_df['day'] = load_df['data_date'].dt.day
    load_df['hour'] = load_df['data_date'].dt.hour
    load_df['minute'] = load_df['data_date'].dt.minute

    # 将month，day, hour, minute特征转变为sin，cos分量
    load_df = encode(load_df, 'month', 12)
    load_df = encode(load_df, 'day', 365)
    load_df = encode(load_df, 'hour', 24)
    load_df = encode(load_df, 'minute', 60)

    # 合并load和weather
    df = pd.concat([load_df, weather_df], axis=1, join='inner')

    # One-hot
    df = pd.get_dummies(df, prefix=['day_of_week'], columns=['day_of_week'])
    df = pd.get_dummies(df, prefix=['year'], columns=['year'])
    df = pd.get_dummies(df, prefix=['workday'], columns=['workday'])
    df = pd.get_dummies(df, prefix=['holiday'], columns=['holiday'])
    df = df[cols]

    return df

def load_data_proprecessing(type_path, weather_df):
    # 获取工作日和节假日信息
    holidays_date_list, special_workday_date_list = get_date_list()

    user_id_df_dict = {}
    user_id_co_name_dict = {}
    file_names_list = find_files(type_path)

    for file_name in file_names_list:
        # 提取文件名中的coName和userID
        co_name, user_id = file_name.split('_')
        print('-------' + co_name + '--------')
        print('-------' + user_id + '--------')
        user_id_co_name_dict[user_id] = co_name

        # 导入行业负荷数据
        df = pd.read_csv(type_path + file_name + '.csv')

        # 修改数据的格式
        df['DATA_DATE'] = pd.to_datetime(df['DATA_DATE'])
        df['day_of_week'] = df['DATA_DATE'].dt.dayofweek
        df.drop(['CONS_NO'], axis=1, inplace=True)
        len_df = len(df)
        load_list = list(df.iloc[:, 1: 97].values.reshape((96 * len_df, )))
        data_date_list = list(df['DATA_DATE'].values.repeat(96))
        day_of_week_list = list(df['day_of_week'].values.repeat(96))
        for k in range(len(data_date_list)):
            data_date_list[k] = data_date_list[k] + np.timedelta64(k % 96 * 15, 'm')
        new_df_dict = {'data_date': data_date_list, 'day_of_week': day_of_week_list, 'load': load_list}
        df = pd.DataFrame(new_df_dict)

        # 提取user缺失的日期
        missing_date_list = extract_missing_date(df)

        # 缺失的数据用下周的数据填充
        df = fill_missing_date(df, missing_date_list)
        df = generate_features(df, weather_df, holidays_date_list, special_workday_date_list)
        user_id_df_dict[user_id] = df.copy()

    return user_id_df_dict, user_id_co_name_dict

def feature_engineering(base_path, type_num, sum_flag=False, sum_user_id_list=[]):
    type_path = base_path + 'data/industry_load_by_type/%s/' % type_num
    type_num_original_path = base_path + 'data/type_%s/original/' % type_num

    if sum_flag:
        print('Sum of load files...')
        sum_load_df = pd.DataFrame()
        file_names_list = find_files(type_path)

        for file_name in file_names_list:
            co_name, user_id = file_name.split('_')
            if len(sum_user_id_list) > 0:
                if user_id in sum_user_id_list:
                    df = pd.read_csv(type_num_original_path + file_name + '.csv')
                    if sum_load_df.empty:
                        sum_load_df = df['load']
                    else:
                        sum_load_df = sum_load_df + df['load']
            else:
                df = pd.read_csv(type_num_original_path + file_name + '.csv')
                if sum_load_df.empty:
                    sum_load_df = df['load']
                else:
                    sum_load_df = sum_load_df + df['load']
        sum_df = df.copy()
        sum_df['load'] = sum_load_df
        print('Saving original sum csv file...')
        sum_df.to_csv(type_num_original_path + 'type%s_%s.csv' % (type_num, type_num), index=False)

    else:
        # weather预处理
        weather_df = weather_data_preprocessing(base_path)

        # load预处理
        user_id_df_dict, user_id_co_name_dict = load_data_proprecessing(type_path, weather_df)

        # 保存csv文件
        print('Saving original csv files...')

        if not os.path.exists(type_num_original_path):
            os.makedirs(type_num_original_path)

        for user_id in user_id_df_dict.keys():
            df = user_id_df_dict[user_id]
            co_name = user_id_co_name_dict[user_id]
            print('Saving %s - %s csv file...' % (co_name, user_id))
            df.to_csv(type_num_original_path + '%s_%s.csv' % (co_name, user_id), index=False)


# 2477 참외밭

N = int(input())
d_list = []
direction = {
    '1': 0, '2': 0, '3': 0, '4': 0,
}
area1 = area2 = 1
min_list = []

for _ in range(0, 6):
    direct, meter = map(int, input().split())
    d_list.append([direct, meter])
    direction[str(direct)] += 1

for i in range(len(d_list)):
    for key in direction.keys():
        if d_list[i][0] == int(key):
            if int(direction[key]) == 1:
                min_list.append(int(key))
                area1 *= d_list[i][1]

for i in range(len(d_list)):
    if i == (len(d_list) - 1):
        if not(d_list[0][0] in min_list or d_list[i - 1][0] in min_list):
            area2 *= d_list[i][1]
    elif not(d_list[i + 1][0] in min_list or d_list[i - 1][0] in min_list):
        area2 *= d_list[i][1]

print(N * (area1 - area2))


import os
import subprocess
from distutils.version import LooseVersion
from os.path import join

import setuptools.command.build_py
import setuptools.command.develop
from setuptools import Extension, find_packages, setup

version = "0.1.2"

# Adapted from https://github.com/pytorch/pytorch
cwd = os.path.dirname(os.path.abspath(__file__))
if os.getenv("NNMNKWII_BUILD_VERSION"):
    version = os.getenv("NNMNKWII_BUILD_VERSION")
else:
    try:
        sha = (
            subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=cwd)
            .decode("ascii")
            .strip()
        )
        version += "+" + sha[:7]
    except subprocess.CalledProcessError:
        pass
    except IOError:  # FileNotFoundError for python 3
        pass

class build_py(setuptools.command.build_py.build_py):
    def run(self):
        self.create_version_file()
        setuptools.command.build_py.build_py.run(self)

    @staticmethod
    def create_version_file():
        global version, cwd
        print("-- Building version " + version)
        version_path = os.path.join(cwd, "nnmnkwii", "version.py")
        with open(version_path, "w") as f:
            f.write("__version__ = '{}'\n".format(version))

class develop(setuptools.command.develop.develop):
    def run(self):
        build_py.create_version_file()
        setuptools.command.develop.develop.run(self)

cmdclass = {"build_py": build_py, "develop": develop}

min_cython_ver = "0.28.0"
try:
    import Cython

    ver = Cython.__version__
    _CYTHON_INSTALLED = ver >= LooseVersion(min_cython_ver)
except ImportError:
    _CYTHON_INSTALLED = False

try:
    if not _CYTHON_INSTALLED:
        raise ImportError("No supported version of Cython installed.")
    from Cython.Distutils import build_ext

    cython = True
except ImportError:
    from setuptools.command.build_ext import build_ext

    cython = False

include_dirs = []
cmdclass["build_ext"] = build_ext
if cython:
    ext = ".pyx"
    import numpy as np

    include_dirs.insert(0, np.get_include())
else:
    ext = ".c"
    print("Building extentions from pre-generated C source")
    if not os.path.exists(join("nnmnkwii", "paramgen", "mlpg_helper" + ext)):
        raise RuntimeError("Cython is required to generate C code.")

ext_modules = [
    Extension(
        name="nnmnkwii.util._linalg",
        sources=[join("nnmnkwii", "util", "_linalg" + ext)],
        include_dirs=include_dirs,
        language="c",
        extra_compile_args=["-std=c99"],
    ),
    Extension(
        name="nnmnkwii.paramgen.mlpg_helper",
        sources=[join("nnmnkwii", "paramgen", "mlpg_helper" + ext)],
        include_dirs=include_dirs,
        language="c",
        extra_compile_args=["-std=c99"],
    ),
]

# [start] bandmat related code
# https://github.com/MattShannon/bandmat/issues/10
bandmat_upstream_fixed = False
if not bandmat_upstream_fixed:
    cython_locs = [
        ("nnmnkwii", "paramgen", "_bandmat", "full"),
        ("nnmnkwii", "paramgen", "_bandmat", "core"),
        ("nnmnkwii", "paramgen", "_bandmat", "tensor"),
        ("nnmnkwii", "paramgen", "_bandmat", "linalg"),
        ("nnmnkwii", "paramgen", "_bandmat", "misc"),
        ("nnmnkwii", "paramgen", "_bandmat", "overlap"),
    ]
    ext_modules.extend(
        Extension(
            ".".join(loc),
            [join(*loc) + ".pyx"],
            extra_compile_args=["-O3"],
            include_dirs=include_dirs,
        )
        for loc in cython_locs
    )
# [end] bandmat related code

def package_files(directory):
    # https://stackoverflow.com/questions/27664504/
    paths = []
    for (path, _, filenames) in os.walk(directory):
        for filename in filenames:
            paths.append(os.path.join("..", path, filename))
    return paths

package_data = package_files("./nnmnkwii/util/_example_data")

install_requires = [
    "scipy",
    "cython >= " + min_cython_ver,
    "fastdtw",
    "scikit-learn",
    "pysptk >= 0.1.17",
    "tqdm",
]

setup(
    name="nnmnkwii",
    version=version,
    description="Library to build speech synthesis systems for fast prototyping",
    long_description=open("README.md", "rb").read().decode("utf-8"),
    long_description_content_type="text/markdown",
    author="Ryuichi Yamamoto",
    author_email="zryuichi@gmail.com",
    url="https://github.com/r9y9/nnmnkwii",
    license="MIT",
    packages=find_packages(exclude=["tests", "perf"]),
    package_data={"": package_data},
    ext_modules=ext_modules,
    cmdclass=cmdclass,
    setup_requires=["numpy >= 1.20.0"],
    install_requires=install_requires,
    tests_require=["nose", "coverage"],
    extras_require={
        "docs": ["numpydoc", "sphinx_rtd_theme"],
        "test": ["nose", "pyworld", "librosa"],
        "lint": [
            "pysen",
            "types-setuptools",
            "mypy<=0.910",
            "black>=19.19b0,<=20.8",
            "click<8.1.0",
            "flake8>=3.7,<4",
            "flake8-bugbear",
            "isort>=4.3,<5.2.0",
            "importlib-metadata<5.0",
        ],
    },
    classifiers=[
        "Operating System :: Microsoft :: Windows",
        "Operating System :: POSIX",
        "Operating System :: Unix",
        "Operating System :: MacOS",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "License :: OSI Approved :: MIT License",
        "Topic :: Scientific/Engineering",
        "Topic :: Software Development",
        "Intended Audience :: Science/Research",
        "Intended Audience :: Developers",
    ],
    keywords=["Research"],
)


import os
import pickle
import argparse

import mlflow
import xgboost as xgb
from prefect import flow, task
from hyperopt import STATUS_OK, Trials, hp, tpe, fmin
from hyperopt.pyll import scope
from sklearn.metrics import (
    roc_auc_score,
    accuracy_score,
    classification_report
)
from prefect.artifacts import create_markdown_artifact

mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("news-classification-experiment")

mlflow.xgboost.autolog()

@task(log_prints=True)
def load_pickle(filename):
    with open(filename, "rb") as f_in:
        return pickle.load(f_in)

@task(log_prints=True)
def objective(params):
    with mlflow.start_run():
        mlflow.set_tag("model", "xgboost")
        mlflow.log_params(params)

        model = xgb.XGBClassifier(**params)
        model = model.fit(X_train, y_train)

        # Predicting values for training and validation data, and getting prediction probabilities
        y_pred = model.predict(X_val)
        accuracy = accuracy_score(y_val, y_pred)

        print("SCORE:", accuracy)

        mlflow.log_metric("accuracy", accuracy)

    return {'loss': accuracy, 'status': STATUS_OK}

@flow(log_prints=True)
def run_optimization(data_path: str, num_trials: int = 1):
    global X_train, y_train, X_val, y_val

    X_train, y_train = load_pickle(os.path.join(data_path, "train.pkl"))
    X_val, y_val = load_pickle(os.path.join(data_path, "val.pkl"))

    search_space = {
        'max_depth': scope.int(hp.quniform('max_depth', 4, 100, 1)),
        'eval_metric': 'auc',
        'learning_rate': hp.loguniform('learning_rate', -3, 0),
        'reg_alpha': hp.loguniform('reg_alpha', -5, -1),
        'reg_lambda': hp.loguniform('reg_lambda', -6, -1),
        'min_child_weight': hp.loguniform('min_child_weight', -1, 3),
        'objective': 'binary:logistic',
        'seed': 42,
    }

    best_result = fmin(
        fn=objective,
        space=search_space,
        algo=tpe.suggest,
        max_evals=num_trials,
        trials=Trials(),
    )
    # Querying mlflow api instead of using web UI.
    # Sorting by validation aucroc and then getting top run for best run.
    EXPERIMENT_ID = mlflow.get_experiment_by_name(
        'news-classification-experiment'
    ).experiment_id
    runs_df = mlflow.search_runs(
        experiment_ids=EXPERIMENT_ID, order_by=['metrics.accuracy DESC']
    )
    best_run = runs_df.iloc[0]
    best_run_id = best_run['run_id']

    # Loading model from best run
    best_model = mlflow.xgboost.load_model(f'runs:/{best_run_id}/model')
    y_predict_model = best_model.predict(X_val)

    # Predicting and evaluating best model on holdout set'

    model_report = classification_report(y_val, y_predict_model)
    print(model_report)

    # Plotting the area under the curve to visualize the performance of the model

    pred_prob = best_model.predict_proba(X_val)[:, 1]

    auc_score = roc_auc_score(y_val, pred_prob)
    print("AUC_SCORE:", auc_score)

    markdown__accuracy_report = f""" News Classification Model Report

		{model_report}

		AUC_SCORE: {auc_score}
		"""

    create_markdown_artifact(
        key="news-classifier-report", markdown=markdown__accuracy_report
    )

    mlflow.xgboost.log_model(best_model, artifact_path="artifacts\model")
    mlflow.register_model(
        f'runs:/{best_run_id}/artifacts/model', 'NewsClassification-XGBHP'
    )

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Training Pipeline')
    parser.add_argument(
        '--data_path', required=True, help='Destination of processed data'
    )
    args = parser.parse_args()
    run_optimization(args.data_path)


"""
Provides the implementation of a variety of enemies to be used in the game.
"""

from enemy import AbstractEnemy, SimpleEnemy
from utilities import rectangles_intersect, get_delta_through_centre
class WoodEnemy(SimpleEnemy):
    """An enemy that moves slowly but is immune to slows and has a lot of health"""
    colour = 'peru'
    points = 40

    def __init__(self, grid_size=(.2, .2), grid_speed=2/60, health=1500):
        super().__init__(grid_size, grid_speed, health)

    def damage(self, damage, type_):
        self.health -= damage
        if self.health < 0:
            self.health = 0
class FireEnemy(SimpleEnemy):
    """An enemy that moves very quickly, killed by ice towers"""
    name = "Fire Enemy"
    colour = 'orange red'
    points = 2
    def __init__(self, grid_size=(.2, .2), grid_speed=10/60, health=350):
        super().__init__(grid_size, grid_speed, health)
        self.slowed = False

    def damage(self, damage, type_):
        """Enemy never takes damage

        Parameters:
            damage (int): The amount of damage to inflict
            type_ (str): The type of damage to do i.e. projectile, explosive
        """
        self.health -= damage
        if self.health < 0:
            self.health = 0

class GroundEnemy(AbstractEnemy):
    """Basic type of enemy"""
    name = "Ground Enemy"
    colour = 'black'

    points = 5

    def __init__(self, grid_size=(.2, .2), grid_speed=5/60, health=100):
        super().__init__(grid_size, grid_speed, health)

    def damage(self, damage, type_):
        """Inflict damage on the enemy

        Parameters:
            damage (int): The amount of damage to inflict
            type_ (str): The type of damage to do i.e. projectile, explosive
        """
        if type_ == 'energy':
            self.health -= damage
            if self.health < 0:
                self.health = 0

    def step(self, data):
        """Move the enemy forward a single time-step

        Parameters:
            grid (GridCoordinateTranslator): Grid the enemy is currently on
            path (Path): The path the enemy is following

        Returns:
            bool: True iff the new location of the enemy is within the grid
        """
        grid = data.grid
        path = data.path

        # Repeatedly move toward next cell centre as much as possible
        movement = self.grid_speed
        while movement > 0:
            cell_offset = grid.pixel_to_cell_offset(self.position)

            # Assuming cell_offset is along an axis!
            offset_length = abs(cell_offset[0] + cell_offset[1])

            if offset_length == 0:
                partial_movement = movement
            else:
                partial_movement = min(offset_length, movement)

            cell_position = grid.pixel_to_cell(self.position)
            delta = path.get_best_delta(cell_position)

            # Ensures enemy will move to the centre before moving toward delta
            dx, dy = get_delta_through_centre(cell_offset, delta)

            speed = partial_movement * self.cell_size
            self.move_by((speed * dx, speed * dy))
            self.position = tuple(int(i) for i in self.position)

            movement -= partial_movement

        intersects = rectangles_intersect(*self.get_bounding_box(), (0, 0), grid.pixels)
        return intersects or grid.pixel_to_cell(self.position) in path.deltas


from django.shortcuts import render, HttpResponse
from django.views.generic import ListView
import requests

def index(request):
    print('x')
    return render(request, 'core/index.html')

def species(request):
    species = []

    if request.method == 'POST':

        species_url = 'https://ghibliapi.herokuapp.com/species/'

        search_params = {
            'species' : 'name',
            'species' : 'classification',
            'species' : 'eye_colors',
            'species' : 'hair_colors',
            'q' : request.POST['search']

            }

        r = requests.get(species_url, params=search_params)
        results = r.json()

        if len(results):
            for result in results:
                species_data = {
                'Name' : result['name'],
                'Classification': result['classification'],
                'Eye_Colors' : result['eye_colors'],
                'Hair_Colors' : result['hair_colors']
            }
                species.append(species_data)

        else:
            message = print("No results found")

        print(species)

    context = {
    'species' : species
    }

    return render(request,'core/species.html', context)

def films(request):
    movies = []

    if request.method == 'POST':

        film_url = 'https://ghibliapi.herokuapp.com/films/'

        search_params = {
            'films' : 'title',
            'films' : 'description',
            'films' : 'director',
            'films' : 'release_date',
            'q' : request.POST['search']

            }

        r = requests.get(film_url, params=search_params)
        results = r.json()

        if len(results):
            for result in results:
                movie_data = {
                'Title' : result['title'],
                'Release_date': result['release_date'],
                'Director' : result['director'],
                'Producer' : result['producer'],
                'Description' : result['description']

            }
                movies.append(movie_data)

        else:
            message = print('No results found')

    context = {
    'movies' : movies
    }

    return render(request,'core/films.html', context)

def people(request):
    people = []

    if request.method == 'POST':

        people_url = 'https://ghibliapi.herokuapp.com/people/'

        search_params = {
            'people' : 'name',
            'people' : 'age',
            'people' : 'gender',
            'people' : 'eye_color',
            'people' : 'hair_color',
            'q' : request.POST['search']

            }

        r = requests.get(people_url, params=search_params)
        results = r.json()

        if len(results):
            for result in results:
                people_data = {
                'name' : result['name'],
                'age': result['age'],
                'gender': result['gender'],
                'eye_Color' : result['eye_color'],
                'hair_Color' : result['hair_color']
            }

                people.append(people_data)

        else:
            message = print("No results found")

        print(people)

    context = {
    'people' : people
    }

    return render(request,'core/people.html', context)


def solution(name, yearning, photo):
    answer = []
    year_dict = {n:y for n,y in zip(name,yearning)}
    ans = 0

    for pho in photo:
        for p in pho:
            if p in year_dict.keys():
                ans += year_dict[p]
        answer.append(ans)
        ans = 0

    return answer


import turtle as t
import random as rd

def draw_shape(num_sides, tim_turtle):
    for _ in range(num_sides):
        tim_turtle.forward(100)
        tim_turtle.right(360 / num_sides)

def random_move(tim_turtle):
    angles = [0, 90, 180, 270]
    tim_turtle.forward(30)
    tim_turtle.setheading(rd.choice(angles))

def random_color():
    r = rd.randint(0, 255)
    g = rd.randint(0, 255)
    b = rd.randint(0, 255)
    return (r, g, b)

def draw_spirograph(tim_turtle, step):
    for pos in range(0, 360, step):
        tim_turtle.pencolor(random_color())
        tim_turtle.circle(100)
        tim.setheading(pos)

if __name__ == "__main__":

    tim = t.Turtle()
    t.colormode(255)
    tim.pensize(2)
    tim.speed("fastest")

    draw_spirograph(tim, 5)

    screen = t.Screen()
    screen.exitonclick()


# Cours hippique
# Version très basique, sans mutex sur l'écran, sans arbitre, sans annoncer le gagant, ... ...

# Programmeur : Carisey Corentin
# Date de DEBUT 25 Mai 2023
# Dernière modif
#A Faire :
# -ADAPTER arbitre au nouvel affichage
#

# Quelques codes d'échappement (tous ne sont pas utilisés)
CLEARSCR="\x1B[2J\x1B[;H"          #  Clear SCReen
CLEAREOS = "\x1B[J"                #  Clear End Of Screen
CLEARELN = "\x1B[2K"               #  Clear Entire LiNe
CLEARCUP = "\x1B[1J"               #  Clear Curseur UP
GOTOYX   = "\x1B[%.2d;%.2dH"       #  ('H' ou 'f') : Goto at (y,x), voir le code

DELAFCURSOR = "\x1B[K"             #  effacer après la position du curseur
CRLF  = "\r\n"                     #  Retour à la ligne

# Nov 2021
# Course Hippique (version élèves)
# VT100 : Actions sur le curseur
CURSON   = "\x1B[?25h"             #  Curseur visible
CURSOFF  = "\x1B[?25l"             #  Curseur invisible

# VT100 : Actions sur les caractères affichables
NORMAL = "\x1B[0m"                  #  Normal
BOLD = "\x1B[1m"                    #  Gras
UNDERLINE = "\x1B[4m"               #  Souligné

# VT100 : Couleurs : "22" pour normal intensity
CL_BLACK="\033[22;30m"                  #  Noir. NE PAS UTILISER. On verra rien !!
CL_RED="\033[22;31m"                    #  Rouge
CL_GREEN="\033[22;32m"                  #  Vert
CL_BROWN = "\033[22;33m"                #  Brun
CL_BLUE="\033[22;34m"                   #  Bleu
CL_MAGENTA="\033[22;35m"                #  Magenta
CL_CYAN="\033[22;36m"                   #  Cyan
CL_GRAY="\033[22;37m"                   #  Gris

# "01" pour quoi ? (bold ?)
CL_DARKGRAY="\033[01;30m"               #  Gris foncé
CL_LIGHTRED="\033[01;31m"               #  Rouge clair
CL_LIGHTGREEN="\033[01;32m"             #  Vert clair
CL_YELLOW="\033[01;33m"                 #  Jaune
CL_LIGHTBLU= "\033[01;34m"              #  Bleu clair
CL_LIGHTMAGENTA="\033[01;35m"           #  Magenta clair
CL_LIGHTCYAN="\033[01;36m"              #  Cyan clair
CL_WHITE="\033[01;37m"                  #  Blanc

#-------------------------------------------------------

#-------------------------------------------------------
import multiprocessing as mp

import os, time,math, random, sys, ctypes, array

# Une liste de couleurs à affecter aléatoirement aux chevaux
lyst_colors=[CL_WHITE, CL_RED, CL_GREEN, CL_BROWN , CL_BLUE, CL_MAGENTA, CL_CYAN, CL_GRAY,
             CL_DARKGRAY, CL_LIGHTRED, CL_LIGHTGREEN,  CL_LIGHTBLU, CL_YELLOW, CL_LIGHTMAGENTA, CL_LIGHTCYAN]

def effacer_ecran() : print(CLEARSCR,end='')
def erase_line_from_beg_to_curs() : print("\033[1K",end='')
def curseur_invisible() : print(CURSOFF,end='')
def curseur_visible() : print(CURSON,end='')
def move_to(lig, col) : print("\033[" + str(lig) + ";" + str(col) + "f",end='')

def en_couleur(Coul) : print(Coul,end='')
def en_rouge() : print(CL_RED,end='') # Un exemple !

# La tache d'un cheval
def un_cheval(repere_cheval : int, keep_running) : # repere_cheval commence à 0
    col=1
    while col < LONGEUR_COURSE and keep_running.value :
        for i in range(4) :
            move_to(repere_cheval*4+i,col)         # pour effacer toute ma ligne
            erase_line_from_beg_to_curs()
            # Section critique
            mutex.acquire()
            if i == 0 : print("_______\/")
            if i == 1 : print( " /---- _.\ " )
            if i == 2 : print("/|__"+chr(ord('A')+repere_cheval)+"___/")
            if i == 3 : print("/\ /\ ")
            mutex.release()
        en_couleur(lyst_colors[repere_cheval%len(lyst_colors)])

        tableau[repere_cheval] = col         #Chaque cheval ajoute sa position au tableau
        col+=1
        time.sleep(0.1 * random.randint(1,5))

#------------------------------------------------
# La partie principale :

def arbitre():
    k=0
    while keep_running.value :
        lst =(tableau[:]) #Tableau des positions en temps réel

        #Pari sur le gagnant        => NE fonctione pas
        #mutex.acquire()
        #move_to(Nb_process*4+1,0)         # pour effacer toute ma ligne
        #erase_line_from_beg_to_curs()
        #pari = input("Pariez sur le gagnant!")
        #time.sleep(1)
        #mutex.release()

        premier_cheval = chr(lst.index(max(lst))+65)            # Recuperation du cheval a partir de sa position dans la liste
        dernier_cheval = chr(lst.index(min(lst))+65)            # Chr traduit un entier en une lettre

        #Section critique
        mutex.acquire()   #Affichage 1er et Dernier
        move_to(Nb_process*4+2,0)         # pour effacer toute ma ligne
        erase_line_from_beg_to_curs()
        print("Premier cheval : ",premier_cheval)
        print("Dernier cheval :",dernier_cheval)
        mutex.release()     #Protection d'affichage

        #Detection de l'arrivée du premier cheval
        # On considère que la course est terminé lorsque le permier cheval franchit la ligne.
        for i in range(len(lst)):
            if lst[i] >= LONGEUR_COURSE-1: #Detection fin de course
                keep_running.value = False
                for i in range(Nb_process):
                    mes_process[i].terminate() #Fermeture des process
                # Recherche des exaequo
                lst.sort()
                j=1
                while j<len(lst) and lst[j] == lst[0] :
                    mutex.acquire()
                    move_to(Nb_process*4+2,0)
                    print("Chevaux exaequo",chr(lst.index(lst[j])+65))
                    mutex.release()

        time.sleep(0.3)

# ---------------------------------------------------
# La partie principale :
if __name__ == "__main__" :

    import platform
    if platform.system() == "Darwin" :
        mp.set_start_method('fork') # Nécessaire sous macos, OK pour Linux (voir le fichier des sujets)

    LONGEUR_COURSE = 50 # Tout le monde aura la même copie (donc no need to have a 'value')
    keep_running=mp.Value(ctypes.c_bool, True)

    Nb_process=6
    mes_process = [0 for i in range(Nb_process)]

    #Initialisation du verrou
    mutex = mp.Lock()

    #Tableau des chevaux
    tableau = mp.Array("i",Nb_process)

    effacer_ecran()
    curseur_invisible()

    for i in range(Nb_process):  # Lancer   Nb_process  processus
        mes_process[i] = mp.Process(target=un_cheval, args= (i,keep_running,))
        mes_process[i].start()

    #Process arbitre
    P_arbitre = mp.Process(target=arbitre, args=())
    P_arbitre.start()
    P_arbitre.join()

    for i in range(Nb_process): mes_process[i].join()

    move_to(24, 1)
    curseur_visible()


# main.py
from format import format_wabbit
from interpret import interpret_wabbit
from model import *

program1 = PrintStatement(Integer("42"))

program2 = Statements(
    [
        PrintStatement(BinOp(Op("+"), Integer("2"), Integer("3"))),
        PrintStatement(BinOp(Op("+"), Unary(Op("-"), Integer("2")), Integer("3"))),
        PrintStatement(
            BinOp(
                Op("+"),
                Integer("2"),
                BinOp(Op("*"), Integer("3"), Unary(Op("-"), Integer("4"))),
            )
        ),
        PrintStatement(
            BinOp(
                Op("*"),
                Grouping(BinOp(Op("+"), Integer("2"), Integer("3"))),
                Integer("4"),
            )
        ),
        PrintStatement(
            BinOp(Op("-"), Float("2.0"), BinOp(Op("/"), Float("3.0"), Float("4")))
        ),
    ]
)

program3 = Statements(
    [
        ConstDeclaration(Name("pi"), None, Float("3.14159")),
        ConstDeclaration(Name("tau"), None, BinOp(Op("*"), Float("2.0"), Name("pi"))),
        VarDeclaration(Name("radius"), None, Float("4.0")),
        VarDeclaration(Name("perimeter"), Typename("float"), None),
        Assignment(Name("perimeter"), BinOp(Op("*"), Name("tau"), Name("radius"))),
        PrintStatement(Name("perimeter")),
    ]
)

program4 = Statements(
    [
        PrintStatement(Bool("true")),
        PrintStatement(BinOp(Op("=="), Integer("1"), Integer("1"))),
        PrintStatement(BinOp(Op("<"), Integer("0"), Integer("1"))),
        PrintStatement(BinOp(Op(">"), Integer("1"), Integer("0"))),
        PrintStatement(BinOp(Op("&&"), Bool("true"), Bool("true"))),
        PrintStatement(BinOp(Op("||"), Bool("false"), Bool("true"))),
        PrintStatement(Unary(Op("!"), Bool("false"))),
    ]
)

program5 = Statements(
    [
        VarDeclaration(Name("a"), Typename("int"), Integer("2")),
        VarDeclaration(Name("b"), Typename("int"), Integer("3")),
        VarDeclaration(Name("minval"), Typename("int"), None),
        IfStatement(
            BinOp(Op("<"), Name("a"), Name("b")),
            Statements([Assignment(Name("minval"), Name("a"))]),
            Statements([Assignment(Name("minval"), Name("b"))]),
        ),
        PrintStatement(Name("minval")),
    ]
)

program6 = Statements(
    [
        VarDeclaration(Name("x"), Typename("int"), Integer("1")),
        VarDeclaration(Name("fact"), Typename("int"), Integer("1")),
        WhileStatement(
            BinOp(Op("<"), Name("x"), Integer("11")),
            Statements(
                [
                    Assignment(Name("fact"), BinOp(Op("*"), Name("fact"), Name("x"))),
                    Assignment(Name("x"), BinOp(Op("+"), Name("x"), Integer("1"))),
                    PrintStatement(Name("fact")),
                ]
            ),
        ),
    ]
)

program7 = Statements(
    [
        VarDeclaration(Name("n"), None, Integer("5")),
        WhileStatement(
            Bool("true"),
            Statements(
                [
                    IfStatement(
                        BinOp(Op("=="), Name("n"), Integer("0")),
                        Statements([BreakStatement()]),
                        Statements(
                            [
                                PrintStatement(Name("n")),
                                Assignment(
                                    Name("n"), BinOp(Op("-"), Name("n"), Integer("1"))
                                ),
                                ContinueStatement(),
                            ]
                        ),
                    ),
                    Assignment(Name("n"), BinOp(Op("+"), Name("n"), Integer("1"))),
                ]
            ),
        ),
    ]
)

program8 = Statements(
    [
        FuncDeclaration(
            Name("add"),
            [
                Parameter(Name("x"), Typename("int")),
                Parameter(Name("y"), Typename("int")),
            ],
            Typename("int"),
            Statements([ReturnStatement(BinOp(Op("+"), Name("x"), Name("y")))]),
        ),
        VarDeclaration(
            Name("result"),
            None,
            FunctionCall(Name("add"), [Integer("2"), Integer("3")]),
        ),
        PrintStatement(Name("result")),
    ]
)

def format_programs():
    print("program 1:")
    print(format_wabbit(program1))

    print("program 2:")
    print(format_wabbit(program2))

    print("program 3:")
    print(format_wabbit(program3))

    print("program 4:")
    print(format_wabbit(program4))

    print("program5:")
    print(format_wabbit(program5))

    print("program 6:")
    print(format_wabbit(program6))

    print("program 7:")
    print(format_wabbit(program7))

    print("program 8:")
    print(format_wabbit(program8))

def interpret_programs():
    print("Running program 1:")
    interpret_wabbit(program1)

    print("running program 2:")
    interpret_wabbit(program2)

    print("running program 3:")
    interpret_wabbit(program3)

    print("runing program 4:")
    interpret_wabbit(program4)

    print("running program 5:")
    interpret_wabbit(program5)

    print("running program 6:")
    interpret_wabbit(program6)

    print("running program 7:")
    interpret_wabbit(program7)

    print("running program 8:")
    interpret_wabbit(program8)

if __name__ == "__main__":
    user_input = input(
        "Enter F to run format methods or I to run interpret methods'\n"
    ).upper()

    if user_input == "F":
        format_programs()
    elif user_input == "I":
        interpret_programs()
    else:
        print("Invalid entry please try again")


"""
----------------------------------------------------------------------
ALGORITHM
----------------------------------------------------------------------

- Pick a random cell as the current cell and mark it as visited.
- While there are unvisited cells:
    - Pick a random neighbour.
    - If the chosen neighbour has not been visited:
        - Remove the wall between the current cell and the chosen neighbour.
        - Mark the chosen neighbour as visited.
    - Make the chosen neighbour the current cell.

----------------------------------------------------------------------

"""
import random
def generate(cell_list):
    visited = []

    current_cell = cell_list[0]

    visited.append(current_cell)

    while True:
        # While there are still unvisited cells
        exhausted = True
        for cell in cell_list:
            if cell not in visited:
                exhausted = False
                break

        # Quite loop if all have been visited
        if exhausted:
            break

        neighbors = [current_cell.top_neighbor, current_cell.bottom_neighbor, current_cell.left_neighbor, current_cell.right_neighbor]
        valid_neighbors = []

        # Remove None values
        for cell in neighbors:
            if cell != None:

                valid_neighbors.append(cell)

        if len(valid_neighbors):
            chosen_cell = random.choice(valid_neighbors)

            if chosen_cell not in visited:

                if current_cell.top_neighbor == chosen_cell:
                    current_cell.remove_wall("top")

                elif current_cell.bottom_neighbor == chosen_cell:
                    current_cell.remove_wall("bottom")

                elif current_cell.left_neighbor == chosen_cell:
                    current_cell.remove_wall("left")

                elif current_cell.right_neighbor == chosen_cell:
                    current_cell.remove_wall("right")

                visited.append(chosen_cell)

        current_cell = chosen_cell

    return cell_list, "Aldous-Broder Algorithm"


from flask_testing import TestCase

from app import app, db
from models.author import Author
from models.book import Book
from models.tag import Tag

class BaseTestCase(TestCase):
    """A base test case."""

    def create_app(self):
        app.config["WTF_CSRF_ENABLED"] = False
        app.config["SQLALCHEMY_DATABASE_URI"] = 'postgres://localhost/sqlalchemy_app_test'
        return app

    def setUp(self):
        db.create_all()
        author1 = Author('Matt')
        author2 = Author('Elie')
        tag1 = Tag('Sci Fi')
        tag2 = Tag('Horror')
        tag3 = Tag('Biography')
        db.session.add_all([author1, author2, tag1, tag2, tag3])
        db.session.commit()
        book1 = Book('Night of the Living Decorator', author2.id)
        book1.tags = [tag2]
        book2 = Book("Matt's Scary Sci Fi book", author1.id)
        book2.tags = [tag1, tag2]
        db.session.add_all([book1, book2])
        db.session.commit()

    def tearDown(self):
        db.session.remove()
        db.drop_all()


import heapq
# Huffman encoding for strings to binary
def encodeHuffmanTree(string):
	string = list(string)
	freqTable = {}
	stringArray = []
# 	Creates table of frequencies.
	for i in string:
		if i in freqTable:
			freqTable[i] += 1
		else:
			freqTable[i] = 1

	minHeap = []
	entry = 0
	# Loads each char into min-heap
	for i in freqTable.keys():
		iO = {
			'char': i,
			'left': False,
			'right': False
		}

		charO = [freqTable[i], entry, iO]
		heapq.heappush(minHeap, charO)
		entry += 1
	# Builds huffman tree from heap.
	for i in range(len(freqTable.keys())-1):
		left = heapq.heappop(minHeap)
		right = heapq.heappop(minHeap)
		iO = {
			'left': left,
			'right': right
		}
		node = [int(left[0] + right[0]), entry, iO]
		heapq.heappush(minHeap, node)
		entry += 1
	encodedTree = heapq.heappop(minHeap)
	encoding = []

	return encodedTree

def traverseTree(tree, path=[], encoding={}):
	# traverses huffman tree to find encoding for each char
	if not tree[2]['left'] and not tree[2]['right']:
		print("".join([str(x) for x in path]), tree[2]['char'])
		encoding[tree[2]['char']] = "".join([str(x) for x in path])
		return
	else:
	# 0 for left child, 1 for right child
		traverseTree(tree[2]['left'], path + [0])
		traverseTree(tree[2]['right'], path + [1])
	# Returns dict of encodings for each character
	return encoding
def encodeFunc(text, encoding):
	encodeBinary = ""
	currentFrame = ""
	for i in text:
		currentFrame += i
		if encoding[currentFrame]:
			encodeBinary += encoding[currentFrame]
			currentFrame = ""
	return encodeBinary
def decodeFunc(binary, encodingTable):
	decodingTable = {}
	for i in encodingTable.keys():
		decodingTable[encodingTable[i]] = i
	currentFrame = ""
	decodedString = ""
	print(decodingTable)
	for i in binary:
		currentFrame += i
		if currentFrame in decodingTable:
			decodedString += decodingTable[currentFrame]
			currentFrame = ""
	return decodedString
def huffman(text, encode=True, encodingTable=False):
	if encode:
		encoderTree = encodeHuffmanTree(text)
		encodingTable = traverseTree(encoderTree)
		encoded = encodeFunc(text, encodingTable)
		print(encoded)
		return encoded, encodingTable
	elif not encode and encodingTable:
		decoded = decodeFunc(text, encodingTable)
		print(decoded)
		return decoded

text = 'aaaabbbccd'
encodedString, encodingTable = huffman(text)
huffman(encodedString, False, encodingTable)


# coding: utf-8

import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import signal
from scraper import Scraper
from nouns_counter import count_nouns

scraper = Scraper()

def signal_handler(signum, frame):
    raise Exception("end of time")

def create_corpus(url):
    try:
        signal.signal(signal.SIGALRM, signal_handler)
        signal.alarm(10)
        # テキストそのものを返すように修正
        counter = scraper.get_words(url)
        #counter = count_nouns(texts)
    except Exception as e:
        print(e)
        return {}
    return counter

if __name__ == '__main__':
    url = sys.argv[1]
    print(create_corpus(url))


import jittor as jt
from jittor import init
from jittor import nn
import numpy as np
from utils import gram_matrix, bgr2yuv

jt.flags.use_cuda = 1

# adversarial loss for generator (LSGAN)
def adv_loss_g(generated_logit):
    return jt.mean(jt.sqr(generated_logit-1))

# content loss for generator
def con_loss_g(vgg,real,generated):
    real_feature_map = vgg(real)
    fake_feature_map = vgg(generated)

    content_loss = nn.L1Loss()(fake_feature_map,real_feature_map)
    return content_loss

#  grayscale style loss for generator
def gs_loss_g(vgg,generated,anime_gray):
    generated_feature_map = vgg(generated)
    gray_feature_map = vgg(anime_gray)

    generated_gram = gram_matrix(generated_feature_map)
    gray_gram = gram_matrix(gray_feature_map)

    style_loss = nn.L1Loss()(generated_gram,gray_gram)
    return style_loss

# color loss for generator
def color_loss_g(real,generated):
    real_yuv = bgr2yuv(real)
    generated_yuv = bgr2yuv(generated)

    real_y = real_yuv[:,0,:,:]
    generated_y = generated_yuv[:,0,:,:]

    real_u = real_yuv[:,1,:,:]
    generated_u = generated_yuv[:,1,:,:]

    real_v = real_yuv[:,2,:,:]
    generated_v = generated_yuv[:,2,:,:]

    color_loss = nn.L1Loss()(generated_y,real_y) + nn.smooth_l1_loss(generated_u,real_u) + nn.smooth_l1_loss(generated_v,real_v)
    return color_loss

# smooth loss for generator (not mentioned in the paper), see tensorflow version of source code
def smooth_loss_g(args,generated):
    zero_h = jt.zeros((args.batch_size,3,255,256))
    zero_w = jt.zeros((args.batch_size,3,256,255))
    dh = generated[:,:,:-1,:]-generated[:,:,1:,:]
    dw = generated[:,:,:,:-1]-generated[:,:,:,1:]

    return nn.MSELoss()(dh,zero_h) + nn.MSELoss()(dw,zero_w)

# loss for discriminator
def discriminator_loss(anime_logit, anime_gray_logit, generated_logit, smooth_logit):
    real_loss = jt.mean(jt.sqr(anime_logit-1))
    gray_loss = jt.mean(jt.sqr(anime_gray_logit))
    fake_loss = jt.mean(jt.sqr(generated_logit))
    real_blur_loss = jt.mean(jt.sqr(smooth_logit))

    # for Hayao : 1.2, 1.2, 1.2, 0.8
    # for Paprika : 1.0, 1.0, 1.0, 0.005
    # for Shinkai: 1.7, 1.7, 1.7, 1.0
    loss = real_loss * 1.2 + fake_loss * 1.2 + gray_loss * 1.2 + real_blur_loss * 1.8

    return loss


#!/usr/bin/python
#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
# author:      Luciano Augusto Kruk
# website:     www.kruk.eng.br
#
# description: Package with some mathematical functions, as
#              block-matrixes manipulation.
#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>

import numpy as np
from   numpy           import dot;
#import math  as mt
#from numpy import zeros,sin,cos,empty,sqrt;

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
##WWww=--  calc A (x) B:  --=wwWW##
def fn_elwisemult(A,B):
    """
    Calculates the elementwise product of two matrixes. Each
    element of matrix A is dot-multiplied by all elements in B.
    """

    C = dot(A[0,0],B) # aux result

    # dimensions:
    a = C.shape[0]
    b = C.shape[1]
    r = A.shape[0] * a # C rows
    c = A.shape[1] * b # C columns

    # buffer:
    C = np.zeros((r,c))

    # indexes:
    m = 0 # row in C
    n = 0 # column in C

    for i in range(A.shape[0]):
        for j in range(A.shape[1]):
            #print "m=%d; m+a=%d; n=%d; n+b=%d" % (m,m+a,n,n+b)
            C[m:(m+a), n:(n+b)] = dot(A[i,j], B)
            n = n+b
        n = 0
        m = m+a

    #print C
    return C

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
def fn_skew(vector):
    """
    This function returns a numpy array with the skew symmetric cross
    product matrix for vector.  the skew symmetric cross product matrix is
    defined such that np.cross(a, b) = np.dot(skew(a), b)

    :param vector: An array like vector to create the skew symmetric cross product matrix for
    :return: A numpy array of the skew symmetric cross product vector
    """

    assert(max(vector.shape) == 3);

    return np.array([[0, -vector[2], vector[1]],
        [vector[2], 0, -vector[0]],
        [-vector[1], vector[0], 0]])

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
##WWww=--  block skew:  --=wwWW##
def fn_blockskew(vec):
    """
    Generates a matrix with several skew-matrixes in. Each
    three elements of vec are used to generate one of the
    skew-matrixes.
    """

    if isinstance(vec, np.ndarray):
        n = max(vec.shape)
    else:
        n = len(vec)

    # requires multiple of 3:
    assert(not (n%3));
    n = n//3

    # outut buffer:
    C = np.zeros((3*n,3*n))

    for i in range(n):
        m = 3*i
        n = 3*(i+1)
        C[m:n, m:n] = fn_skew(np.asarray(vec)[m:n])

    return C

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
##WWww=--  separate [3x1] vectors from a
#               concatenated set of vectors:  --=wwWW##
def fn_separate3x1(vec):
    aux = vec.squeeze()
    N   = np.max(aux.shape)
    return [aux[(i*3):(3*(i+1))].reshape((3,1)) for i in range(N/3)]

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>
##WWww=--  norm of a vector  --=wwWW##
def fn_norm(vec):
    return np.sqrt(np.sum(vec*vec))

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>

if (__name__ == "__main__"):
    print

#>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>--<<..>>


#!/usr/local/bin/env python
import json
import os
import sys

import numpy as np

from ... import verify_signature

# Read input data from JSON
fn = "../../../versign-core/src/app/verify_request.json"
fo = open(fn, "r")
payload = json.loads(fo.read())
fo.close()
os.remove(fn)

# Get customer ID
user = payload['customerId']

# Extract all four reference signatures
refSign = payload['questionedSignature']
pixelData = np.array(refSign['pixelData']).astype('uint8')
width = refSign['width']
height = refSign['height']

a = np.reshape(pixelData, (height, width))
print
verify_signature(user, a, "../../../versign-core/")


from code.clients import FinnhubClient
from code.models import Company
from ._base import BaseService

class CompanyUpdateService(BaseService):
    def __init__(self, symbol: str):
        self._symbol = symbol

    async def _fetch_company_profile(self):
        async with FinnhubClient() as client:
            return await client.get_company_profile(self._symbol)

    def _parse_company(self, company: dict):
        return {
            'name': company['name'],
            'logo_url': company['logo'],
            'symbol': self._symbol,
        }

    async def _save_company(self, parsed_company: dict):
        company, _ = await Company.update_or_create(**parsed_company)
        return company

    async def do(self):
        company = await self._fetch_company_profile()
        parsed_company = self._parse_company(company)
        return await self._save_company(parsed_company)


# coding=utf-8
# auther：lucas
# date：2019/7/12 10:50
# tools：PyCharm
# Python：3.7.3
import threading
from selenium import webdriver
import time

caps = {'platform': 'ANY',
        'browserName': None,
        'version': '',
        # "enableVNC": True,
        # 'javascriptEnabled': True,
        'recordVideo': True,
        'build': "build_%s" % time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime()),
        'name': 'test_%s' % time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime()),
        'idleTimeout': 90,
        'screenResolution': '1280x720',
        # 'tz': 'Asia/Shanghai',
        # 'tz': 'America/Montreal',
        'testFileNameTemplate': '{platform}_{browser}_{testStatus}_{timestamp}',
        # 'name': 'my_test_name',
        }

def run_browser(browser="chrome"):
    if browser.lower() == "chrome":
        caps['browserName'] = "chrome"
    if browser.lower() == "firefox":
        caps['browserName'] = "firefox"
    # print(caps)
    dr = webdriver.Remote('http://localhost:4444/wd/hub', desired_capabilities=caps)
    dr.get("https://www.baidu.com")
    dr.add_cookie({"name": "zaleniumMessage", "value": "go to baidu page"})
    dr.add_cookie({"name": "zaleniumMessage", "value": "input keyword"})
    dr.find_element_by_id("kw").send_keys("zalenium")
    dr.add_cookie({"name": "zaleniumMessage", "value": "click search button"})
    dr.find_element_by_id("su").click()
    print(dr.find_element_by_id("su").is_displayed())

threads = []
temp_thrad = threading.Thread(target=run_browser, args=())
temp_thrad1 = threading.Thread(target=run_browser, args=("firefox",))
threads.append(temp_thrad)
threads.append(temp_thrad1)
for t in threads:
    t.setDaemon(True)
    t.start()
for i in threads:
    i.join()


from flask.ext.mongoengine import MongoEngine
from mongoengine import *
from mongoengine.base import BaseList, BaseDocument

engine_db = MongoEngine()

def is_empty(doc):
    if doc is None:
        return True
    if isinstance(doc, BaseList):
        return False if doc else True
    if isinstance(doc, BaseDocument):
        return all(not getattr(doc, field, None) for field in doc._fields.keys())
    else:
        return False if doc else True

# Resume models follow:
class BasicInfo(EmbeddedDocument):
    location = StringField()
    email = EmailField()
    phone_number = StringField()
    website = URLField()
    industry = StringField()

class Skill(EmbeddedDocument):
    name = StringField()
    url = URLField()
    skill_level = IntField(default=3)  # 1 - 10
    years = IntField(default=0)  # 0 or greater
    order_by = IntField(default=9)  # 1-10
    description = StringField()

class Language(EmbeddedDocument):
    name = StringField()
    proficiency_level = StringField()

class Experience(EmbeddedDocument):
    company_title = StringField(required=True)
    position_location = StringField()
    company_url = URLField()
    time_at_position = StringField(required=True)
    position_title_url = URLField()
    position_title = StringField(required=True)
    company_image_url = URLField()
    from_date = StringField()
    to_date = StringField()
    description = StringField()
    sensitive = BooleanField(default=False)

class Certification(EmbeddedDocument):
    title = StringField(required=True)
    license = StringField()
    issuer_url_or_certificate_url = URLField()
    issuer_url = URLField()
    date = StringField()
    issuer_image_url = StringField()
    cert_url = URLField()
    institution = StringField(required=True)

class SummaryInfo(EmbeddedDocument):
    current_position = StringField()
    previous_positions = ListField(StringField())
    previous_position = ListField(StringField())
    summary = StringField()

class School(EmbeddedDocument):
    name = StringField(required=True)
    image = URLField()
    to_date = StringField()
    from_date = StringField()
    degree = StringField(required=True)
    url = URLField()

class Other(EmbeddedDocument):
    opportunities = ListField(StringField())
    organizations = ListField(StringField())
    causes = ListField(StringField())

class Book(EmbeddedDocument):
    goodreads_id = IntField()
    isbn = StringField()
    isbn13 = StringField()
    title = StringField()
    image_url = URLField()
    small_image_url = URLField()
    link = URLField()
    num_pages = IntField()
    publisher = StringField()
    average_rating = StringField()
    description = StringField()
    authors = ListField(StringField())
    rating = StringField()
    started_at = DateTimeField()
    read_at = DateTimeField()
    date_added = DateTimeField()
    date_updated = DateTimeField()
    review_body = StringField()

class ReadingList(EmbeddedDocument):
    to_read = ListField(EmbeddedDocumentField(Book))
    currently_reading = ListField(EmbeddedDocumentField(Book))
    finished_reading_general = ListField(EmbeddedDocumentField(Book))
    finished_reading = ListField(EmbeddedDocumentField(Book))

# link to code eval github etc
class ServiceLink(EmbeddedDocument):
    name = StringField()
    user_id = StringField()
    key = StringField()
    secret = StringField()
    oauth_key = StringField()
    oauth_secret = StringField()

class SiteLink(EmbeddedDocument):
    site_name = StringField()
    css_name = StringField()
    url = URLField()

class Resume(Document):
    '''
    About me +
    Basic Info +
    Skills
    Testimonials
    Education
    Courses
    Languages -
    Latest Blog Posts -
    Latest Projects -
    Other Projects -
    Work Experience
    My Github
    CodeEval (maybe) -
    Favorite Coding Music -
    Conferences -
    Credits (look into requirements)
    -------
    certifications
    summary_info
    top_skills
    other_skills
    organizations
    locality?
    name
    industry
    connections
    experiences
    headline
    school
    causes
    '''

    is_primary = BooleanField(default=True)
    meta = {'strict': False}

    title = StringField()
    github_username = StringField()

    headline = StringField()
    name = StringField()
    industry = StringField()
    connections = StringField()
    site_links = ListField(EmbeddedDocumentField(SiteLink))
    basic_info = EmbeddedDocumentField(BasicInfo)
    summary_info = EmbeddedDocumentField(SummaryInfo)
    skills = ListField(EmbeddedDocumentField(Skill))
    work_experience_text = StringField()
    work_experiences = ListField(EmbeddedDocumentField(Experience))
    certifications = ListField(EmbeddedDocumentField(Certification))
    schools = ListField(EmbeddedDocumentField(School))
    other = EmbeddedDocumentField(Other)
    languages = ListField(EmbeddedDocumentField(Language))
    reading_list = EmbeddedDocumentField(ReadingList)
    service_links_list = ListField(EmbeddedDocumentField(ServiceLink))

    def __repr__(self):
        return '<Resume: {}>'.format(self.title)

    def __str__(self):
        return '<Resume: {}>'.format(self.title)

class ResumeSettings(Document):
    enable_limited_resume = BooleanField(default=False)
    mail_to = StringField()
    limited_resume_url_specifier = StringField()
    hidden_experiences = ListField(StringField())
    looking_for = StringField()
    hide_basic_info = BooleanField(default=False)
    resume_link = URLField()

    enable_goodreads_booklist_sync = BooleanField(default=False)
    resume_to_sync = ReferenceField(document_type=Resume)


from typing import Union

from django_opensearch_dsl import Document, fields
from django_opensearch_dsl.registries import registry
from tests.django_dummy_app.models import Article, Source, ArticleKeyword, Author

@registry.register_document
class ArticleDocument(Document):
    """Article document."""

    source = fields.ObjectField(properties={
        "domain": fields.TextField(),
        "name": fields.TextField()
    })

    keywords = fields.NestedField(properties={
        "keyword": fields.TextField()
    })
    authors = fields.NestedField(properties={
        "first_name": fields.KeywordField(),
        "last_name": fields.KeywordField()
    })

    class Django:
        model = Article
        fields = [
            "url",
            "title",
            "snippet"
        ]
        related_models = [
            Source,
            ArticleKeyword,
            Author
        ]

    class Index:
        """Elasticsearch Index Data."""

        name = "articles"
        settings = {"number_of_shards": 2, "number_of_replicas": 1}

    def get_instances_from_related(self, related_instance: Union[Source]):
        """Get related instances from related instance."""
        if isinstance(related_instance, Source):
            return related_instance.articles.all(), "source"


import logging

VERBOSITY_LEVELS = {
    0: None,  # Equivalent to no logging.
    1: logging.WARN,  # For simplicity. Includes ERROR, CRITICAL
    2: logging.INFO,
    3: logging.DEBUG,
    4: logging.NOTSET,  # Equivalent to no filtering. Everything is logged.
}
LOGGING_LEVELS = {
    'NONE': None,
    'CRITICAL': logging.CRITICAL,
    'ERROR': logging.ERROR,
    'WARN': logging.WARN,
    'INFO': logging.INFO,
    'DEBUG': logging.DEBUG,
    'NOTSET': logging.NOTSET,
}
VERBOSITY_TO_LEVELS = {
    0: 'NONE',
    1: 'WARN',
    2: 'INFO',
    3: 'DEBUG',
    4: 'NOTSET',
}

def map_log_verbosity(verbosity):
    '''
    Returns a logging level as exported by `logging` module.
    By default returns logging.NOTSET
    '''
    if verbosity > len(VERBOSITY_LEVELS):
        verbosity = len(VERBOSITY_LEVELS) - 1
    return VERBOSITY_LEVELS.get(verbosity, logging.NOTSET)


import selectors
import socket
import time

def accept(sock, mask, sel):
    conn, addr = sock.accept()
    print("accept:", addr)
    #conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, read)

def read(conn, mask, sel):
    try:
        data = conn.recv(128)  # Should be ready
    except ConnectionResetError as e:
        print(e)
        sel.unregister(conn)
        conn.close()
        return

    if data:
        conn.send(b"Got: " + data)
    # data = b"" peer 关闭sock
    else:
        sel.unregister(conn)
        conn.close()
        print("close()")

listen = ('0.0.0.0', 6789)

sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(listen)
print("listen:", listen)
sock.listen(128)
#sock.setblocking(False)

sel = selectors.DefaultSelector()
sel.register(sock, selectors.EVENT_READ, accept)

start = time.time()
count = 0
while True:

    for key, mask in sel.select():
        callback = key.data
        callback(key.fileobj, mask, sel)

        #count += 1
        #end = time.time()

        #if end - start >= 1:
        #    print("count: {}/s".format(count))
        #    start, end = end, time.time()
        #    count = 0

sel.close()


import numpy as np
from PIL import Image
import os
#from os import list

# Define width and height
w, h = 300, 300
cnt= 0;

# Read file using numpy "fromfile()"
for file in os.listdir('./testmode/'):
    if (file == '.DS_Store'):
        continue

    with open('./testmode/'+file, 'rb') as f:
        d = np.fromfile(f,dtype=np.uint8,count=w*h).reshape(h,w)
        # Make into PIL Image and save
        PILimage = Image.fromarray(d)
        PILimage.save('i'+str(cnt)+'.png')
    cnt += 1;


#!/usr/bin/env python3
# default_encoder.py

import json
import myobj

class MyEncoder(json.JSONEncoder):

    def default(self, obj):
        print('default(', repr(obj), ')')
        # Converts objects to a dictionary of their representation
        d = {
            '__class__': obj.__class__.__name__,
            '__module__': obj.__module__,
        }
        d.update(obj.__dict__)
        return d

obj = myobj.MyObj('internal data')
print(obj)
print(MyEncoder().encode(obj))


from logging import exception
import sys
import subprocess
import math
import importlib

pkgs = {'pandas': 'pd', 'tqdm': 'tqdm','numpy':'np','openpyxl':'openpyxl','xlwings':'xlwings'}
def check_packages():
    for p in pkgs:
        s = pkgs[p]
        try:
            print(f'check for {p}')
            s = importlib.import_module(p)

        except ImportError:
            print(f'{p} is not installed and has to be installed')
            subprocess.call([sys.executable, '-m', 'pip', 'install', p])
        finally:
            s = importlib.import_module(p)
            print(f'{p} is properly installed')

    return
try:
    check_packages()
except:
    print('upgrade pip then try again')
    subprocess.check_call([sys.executable,'-m','pip','install','--upgrade','pip'])
    check_packages()

import pandas as pd
import numpy as np
from tqdm import tqdm
import curate_config as config

def clean_column_names(data):
    try:
        columns=[str(c) for c in data.columns]
        data.columns=[c.replace(' ','_').lower() for c in columns]
        data.columns=[c.replace('__','_') for c in data.columns]
        data.columns=[c.replace('sampleid','sample_id').lower() for c in data.columns]
        data.columns=[c.replace('holeid','hole_id').lower() for c in data.columns]
        data.columns=[c.replace('sample_id_description','sample_id').lower() for c in data.columns]

        data.rename(columns={'depth':'depth_ft'},inplace=True)

        data=data.groupby(level=0,axis=1).sum(numeric_only=False)

    except:
        print('error in cleaning the column names')
    return data

def merge_duplicate_columns(df, method="unique", sep=""):
    duplicated =  df.columns[df.columns.duplicated()].unique()
    print('######duplicated_columns#####')
    print(duplicated)
    try:
        if method == "join":
            for d in duplicated:
                df[d] = df.pop(d).fillna("").astype(str).apply(sep.join, axis=1)

        elif method == "unique":
            for d in duplicated:
                df[d] = df.pop(d).fillna("").astype(str).apply(lambda x: sep.join(x.unique()),axis=1)

        elif method == "sum":
            for d in duplicated:
                df[d] = df.pop(d).sum(axis=1)

    except Exception as e:
        print('error merging duplicate columns')
        print(e)

    return df

def reorder_columns(data,verbose=False,col_order=['work_order','sample_id','sample_id_description','hole_id','hole_id_samples','from_ft','to_ft','from_m','to_m','depth_ft','depth_m','depth','depthfrom','depthto']):
    new_order=col_order.copy()
    for c in col_order:
        i=new_order.index(c)
        if '_'in c:
            new=c.replace('_','')
            new_order.insert(i+1,new)
    #look in the data for the columns we want
    #look in the data for the columns we want

    u_cols=set([c for c in new_order if c in data.columns])
    # set alphabetizies them so we need to reorder basded
    first_cols=[c for c in new_order if c in u_cols]
    last_cols= [c for c in data.columns if c not in first_cols]
    col_order=first_cols+last_cols
    col_order=[c for c in col_order if c in data.columns]
    if verbose:
        print('first columns before sort')
        print([c for c in data.columns][:15])
        print('first columns after sort')
        print(col_order[:15])
    try:
        data=data[col_order]
    except Exception as e:
        print('Error re ordering columns')
        print(e)
    return data

def drop_bad_rows(data,na_threshold=2,targets=['ft','hole'],verbose=False):
    target_rows=[]
    for f in targets:
        try:
            trow=data.filter(like=f).columns[0]
            target_rows.append(trow)
        except:
            pass
    for col in target_rows:
        pass
    try:
        drop_index=data[(data.notna().sum(axis=1)<=na_threshold)].index

        if verbose:
            print(f'drop {len(drop_index)} na rows in locations: {drop_index}')
        data=data.drop(drop_index,axis=0)
        if len(drop_index)==len(data):
            if verbose:
                print('dropped all of the data only contained empty cells')

    except Exception as e:
        print('Error dropping bad rows')
        print(e)
    return data

def drop_work_order(data,verbose=False):
    if verbose:
        print('## Drop work_order column ##')
    work=data.filter(like='work_order').columns
    if len(work)>0:
        data = data.drop(work,axis=1)
    return data

def drop_bad_columns(data,verbose=False):
    if verbose:
        print('## Drop no data columns ##')
    dropped_cols=[]
    for col in data.columns:
        try:
            data[col]=data[col].replace({'':np.nan})
        except Exception as e:
            print(e)
        try:
            if data[col].isna().sum()==len(data):
                dropped_cols.append(col)
                data.drop(col,axis=1,inplace=True)
        except Exception as e:
            print('Error dropping bad columns')
            print(e)
    if verbose:
        print(f'Dropped {dropped_cols}: no data')
    return data

def sort_data(data,sorters=['hole','ft','sample'],verbose=False):
    sort_by=[]
    for c in sorters:
        try:
            all_columns=data.filter(like=c,axis=1).columns
            sort_by.append(all_columns[0])
        except Exception as e:
            print('Error in sorting')
            print(c,'not in the columns')
            print(e)
    if verbose:
        print(f'using {sort_by} for sorting the data')
    try:
        data=data.sort_values(sort_by,axis=0,ascending=True).reset_index(drop=True)
    except Exception as e:
        print('Error in sorting')
        print(e)
    return data

def get_base_path(path,start_point='_AZ_Kay'):

    path_list=path.split('/')
    b=path_list.index(start_point)
    base_path='/'.join(path_list[:b+1])
    return base_path

def round_depths(data,targets=['_m','_ft','recovery','depth'],verbose=False):
    if verbose:
        print('###rounding depths#####')
    for t in targets:
        cols=list(data.filter(like= t).columns)
        if len(cols)>0:
            print("rounding:",cols)
            for col in cols:
                try:
                    ser=pd.to_numeric(data[col],errors='coerce',)
                    ser=ser.astype(float)

                    ser=ser.round(1)
                    ser=ser.map('{:.1f}'.format)
                    data.loc[ser[ser.notna()==True].index,col]=ser[ser.notna()==True]
                except:
                    pass

    return data

def drop_hash(data, verbose=True):
    print('####DROP HASH #########')
    try:
        print('Dtype',data['depth_m'].dtype)
        data['depth_m']=pd.to_numeric(data['depth_m'],errors='coerce')
        data['depth_m']=data['depth_m'].replace(0.0,np.nan)
        drop=(data[data['depth_m'].isna()==True]).index
        print('Dropping ',drop)
        data=data.drop(drop,axis=0)
        data['depth_m']=data['depth_m'].replace(0.0,np.nan)
        data=data.drop(drop,axis=0)
        print('Dropping ',drop)
        drop=(data[data['depth_m']==0]).index
        data=data.drop(drop,axis=0)
    except Exception as e :
        if verbose:
            print (e)
        pass

    try:
        drop=(data[(data['recovery_%']=='#DIV/0!')]).index
        data['recovery_%']=pd.to_numeric(data['recovery_%'],errors='coerce')
        data['recovery_%']=data['recovery_%'].replace(0.0,np.nan)
        drop=(data[data['recovery_%'].isna()==True]).index
        print('Dropping ',drop)

        data=data.drop(drop,axis=0)

    except Exception as e :
        if verbose:
            print (e)
        pass

    return data

def fix_depths(data,like=['_m','_ft'],verbose=True):
    depths=[d for d in data.columns if d.lower().startswith('depth')]
    print(depths)
    depth_map={d:'_'.join(d.split('_')[:-1]) for d in depths if len(d.split('_'))>2 }
    print (depth_map)

    data=data.rename(columns=depth_map)

    for l in like:
        depths=data.filter(like=l,)
        cols=[c for c in depths.columns if c.endswith(l)]
        cols=list(set(cols))

        if verbose:
            print('Filling', cols)
        data[cols]=data[cols].replace(0,np.nan)
        depths=data[cols].copy()

        depths=depths.apply(lambda x: x.fillna(x.mean(skipna=True)),axis=1)
        data[cols]=depths
        # data.drop(data.columns.isduplicated(),axis=1)
    return data


from os import rename, walk
from colors import *
import sys

def walk_dirs(dirname):
    for (dirpath, dirnames, filenames) in walk(dirname):
        for file_ in filenames:
            new_ = file_.lower()
            rename(dirpath+"/"+file_, dirpath+"/"+new_)

        for dir_ in dirnames:
            new_ = dir_.lower()
            rename(dirpath+"/"+dir_, dirpath+"/"+new_)

if __name__ == "__main__":
    try:
        if len(sys.argv) < 2:
            print(f"{OPENRED}FAILURE{CLOSECOLOR}")
            print(
                f"Expected: {OPENBLUE}filepath{CLOSECOLOR}")
            print(
                f"Got:\t  {' '.join(sys.argv[1:])}")
        else:
            filepath = sys.argv[1]
            walk_dirs(filepath)
            print(f"{OPENGREEN}SUCCESS{CLOSECOLOR}")
    except:
        print(f"{OPENRED}FAILURE{CLOSECOLOR}")
        raise


import os
import io
from jinja2 import Environment, BaseLoader, select_autoescape

try:
    import yaml
except ImportError as e:
    print (str(e) + " To install it, type:")
    print ("pip install PyYAML")
    exit(1)

class TemplateLoader(BaseLoader):
    """
    Template loader class.

    An instance of this class is passed to the template engine. It is
    responsible for reading the template file
    """
    def __init__(self):
        pass

    def get_source(self, environment, template):
        """
        This function reads the template files.
        For detailed documentation see:
        http://jinja.pocoo.org/docs/2.10/api/#jinja2.BaseLoader.get_source

        Please note that this function always return 'false' as 'uptodate'
        value, so the output file will always be generated.
        """
        if not os.path.isfile(template):
            raise TemplateNotFound(template)
        with open(template) as f:
            source = f.read()
        return source, template, False

def load_manifest_list(file):
    """
    Load the substitution data from the manifests.

    Parameters
    ----------
    file : file
        A yaml file containing the manifest list

    Returns
    -------
    list
        The list of the contents of the manifest files, as generated by the yaml
        parser.
        """
    db = []
    manifest_list = yaml.load(file)
    for item in manifest_list["manifest_list"]:
        manifest_path = item['manifest']
        try:
            file = open(manifest_path)
            manifest = yaml.load(file)
            db.append({"manifest": manifest, "attr": item})
        except IOError:
            raise Exception ("Manifest for "+item['name']+" cannot be opened at path "+item['manifest'])

    return db

def main():
    """
    The entry point of the script.

    Generates the output files based on the templates and the manifests.
    """
    donotedit_warning = \
                    "/*********** " + \
                    "WARNING: This is an auto-generated file. Do not edit!" + \
                    " ***********/"

    env = Environment(
        loader = TemplateLoader(),
        autoescape = select_autoescape(['html', 'xml']),
        lstrip_blocks = True,
        trim_blocks = True,
        keep_trailing_newline = True
    )

    with open(os.path.join('tools', 'tfm_manifest_list.yaml')) \
                                                    as manifest_list_yaml_file:
        # Read manifest list file, build database
        db = load_manifest_list(manifest_list_yaml_file)

        utilities = {}
        utilities['donotedit_warning']=donotedit_warning

        context = {}
        context['manifests'] = db
        context['utilities'] = utilities

    with open(os.path.join('tools', 'tfm_generated_file_list.yaml')) \
                                                    as file_list_yaml_file:
        # read list of files that need to be generated from templates using db
        file_list_yaml = yaml.load(file_list_yaml_file)
        file_list = file_list_yaml["file_list"]
        for file in file_list:
            outfile_name = file["output"]
            templatefile_name = outfile_name + '.template'

            print ("Generating " + outfile_name)

            template = env.get_template(templatefile_name)

            outfile = io.open(outfile_name, "w", newline='\n')
            outfile.write(template.render(context))
            outfile.close()
    print ("Generation of files done")

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
import torch
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence

IGNORE_INDEX = -100
PAD = "[PAD]"
CLS = "[CLS]"
SEP = "[SEP]"
ACTION = "[A]"
TOPIC = "[T]"

class BERTDataset(Dataset):
    def __init__(self, data, tokenizer, max_seq_len=512, batch_first=True, reward_labels=True):
        self.data = data
        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len
        self.tokenizer.pad_token = PAD
        self.tokenizer.cls_token = CLS
        self.tokenizer.sep_token = SEP
        self.pad = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
        self.bos = tokenizer.convert_tokens_to_ids(tokenizer.cls_token)
        self.eos = tokenizer.convert_tokens_to_ids(tokenizer.sep_token)
        self.batch_first = batch_first
        self.reward_labels = reward_labels

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        if self.reward_labels:
            history = self.data[index][0]
            next_act = self.data[index][1]
            label = self.data[index][2]
        else:
            history = self.data[index][0]
            next_act = self.data[index][1]
            label = IGNORE_INDEX
        return self._process(history, next_act, label)

    def _process(self, history, next_act, label):

        # truncate previous tokens if dialogue history is too long
        ## including the sep between the input and the next action, topic.

        if len(history) + len(next_act) > self.max_seq_len - 2:
            tmp = len(history) - (self.max_seq_len - 2 - (len(next_act)))
            history = history[tmp:]

        seg_ids = [0] * (len(history)) + [1] * len(next_act)
        input_ids = self.tokenizer.convert_tokens_to_ids([CLS]) + history + next_act + self.tokenizer.convert_tokens_to_ids([SEP])
        seg_ids = [1] + seg_ids + [1]
        pos_ids = list(range(len(input_ids)))

        assert len(input_ids) == len(seg_ids) == len(pos_ids)

        instance = {}
        instance["input_ids"] = input_ids
        instance["seg_ids"] = seg_ids
        instance["pos_ids"] = pos_ids
        instance["label"] = label

        return instance

    def collate(self, batch):
        input_ids = pad_sequence(
            [torch.tensor(instance["input_ids"], dtype=torch.long) for instance in batch],
            batch_first=self.batch_first, padding_value=self.pad)

        seg_ids = pad_sequence(
            [torch.tensor(instance["seg_ids"], dtype=torch.long) for instance in batch],
            batch_first=self.batch_first, padding_value=self.pad)

        pos_ids = pad_sequence(
            [torch.tensor(instance["pos_ids"], dtype=torch.long) for instance in batch],
            batch_first=self.batch_first, padding_value=self.pad)

        labels = torch.tensor([instance["label"] for instance in batch], dtype=torch.long)

        return input_ids, seg_ids, pos_ids, labels

    def rl_collate(self, batch):

        input_ids = pad_sequence([torch.tensor(x, dtype=torch.long) for x in batch],  batch_first=self.batch_first, padding_value=self.pad)
        return input_ids


# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : ImageEnhancement.py
# Time       ：2023/9/12 11:10
# Author     ：yujia
# version    ：python 3.6
# Description：
"""
import numbers
import cv2
import numpy as np
from PIL import Image

from utils.utils_aug import CenterCrop, ImageNetPolicy, RandomResizedCrop, Resize

class ImageEnhancement(object):
    def __init__(self, input_shape):
        self.resize_crop = RandomResizedCrop(input_shape)
        self.policy = ImageNetPolicy()

        self.resize = Resize(input_shape[0] if input_shape[0] == input_shape[1] else input_shape)
        self.center_crop = CenterCrop(input_shape)

    def rand(self, a=0, b=1):
        return np.random.rand() * (b - a) + a

    def get_random_data(self, image, input_shape, jitter=.3, hue=.1, sat=0.7, val=0.3, random=True):
        # ------------------------------#
        #   获得图像的高宽与目标高宽
        # ------------------------------#
        iw, ih = image.size
        h, w = input_shape

        if not random:
            scale = min(w / iw, h / ih)
            nw = int(iw * scale)
            nh = int(ih * scale)
            dx = (w - nw) // 2
            dy = (h - nh) // 2

            # ---------------------------------#
            #   将图像多余的部分加上灰条
            # ---------------------------------#
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image, np.float32)

            return image_data

        # ------------------------------------------#
        #   对图像进行缩放并且进行长和宽的扭曲
        # ------------------------------------------#
        new_ar = iw / ih * self.rand(1 - jitter, 1 + jitter) / self.rand(1 - jitter, 1 + jitter)
        scale = self.rand(0.75, 1.5)
        if new_ar < 1:
            nh = int(scale * h)
            nw = int(nh * new_ar)
        else:
            nw = int(scale * w)
            nh = int(nw / new_ar)
        image = image.resize((nw, nh), Image.BICUBIC)

        # ------------------------------------------#
        #   将图像多余的部分加上灰条
        # ------------------------------------------#
        dx = int(self.rand(0, w - nw))
        dy = int(self.rand(0, h - nh))
        new_image = Image.new('RGB', (w, h), (128, 128, 128))
        new_image.paste(image, (dx, dy))
        image = new_image

        # ------------------------------------------#
        #   翻转图像
        # ------------------------------------------#
        flip = self.rand() < .5
        if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

        rotate = self.rand() < .5
        if rotate:
            angle = np.random.randint(-15, 15)
            a, b = w / 2, h / 2
            M = cv2.getRotationMatrix2D((a, b), angle, 1)
            image = cv2.warpAffine(np.array(image), M, (w, h), borderValue=[128, 128, 128])

        image_data = np.array(image, np.uint8)
        # ---------------------------------#
        #   对图像进行色域变换
        #   计算色域变换的参数
        # ---------------------------------#
        r = np.random.uniform(-1, 1, 3) * [hue, sat, val] + 1
        # ---------------------------------#
        #   将图像转到HSV上
        # ---------------------------------#
        hue, sat, val = cv2.split(cv2.cvtColor(image_data, cv2.COLOR_RGB2HSV))
        dtype = image_data.dtype
        # ---------------------------------#
        #   应用变换
        # ---------------------------------#
        x = np.arange(0, 256, dtype=r.dtype)
        lut_hue = ((x * r[0]) % 180).astype(dtype)
        lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
        lut_val = np.clip(x * r[2], 0, 255).astype(dtype)

        image_data = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
        image_data = cv2.cvtColor(image_data, cv2.COLOR_HSV2RGB)
        return image_data

    def AutoAugment(self, image, random=True):
        if not random:
            image = self.resize(image)
            image = self.center_crop(image)
            return image

        # ------------------------------------------#
        #   resize并且随即裁剪
        # ------------------------------------------#
        image = self.resize_crop(image)

        # ------------------------------------------#
        #   翻转图像
        # ------------------------------------------#
        flip = self.rand() < .5
        if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

        # ------------------------------------------#
        #   随机增强
        # ------------------------------------------#
        image = self.policy(image)
        return image

def crop(img, i, j, h, w):
    """Crop the given PIL Image.

    Args:
        img (PIL Image): Image to be cropped.
        i (int): i in (i,j) i.e coordinates of the upper left corner.
        j (int): j in (i,j) i.e coordinates of the upper left corner.
        h (int): Height of the cropped image.
        w (int): Width of the cropped image.

    Returns:
        PIL Image: Cropped image.
    """
    return img.crop((j, i, j + w, i + h))

def center_crop(img, output_size):
    if isinstance(output_size, numbers.Number):
        output_size = (int(output_size), int(output_size))
    w, h = img.size
    th, tw = output_size
    i = int(round((h - th) / 2.))
    j = int(round((w - tw) / 2.))
    return crop(img, i, j, th, tw)

def resize(img, size, interpolation=Image.BILINEAR):
    r"""Resize the input PIL Image to the given size.

    Args:
        img (PIL Image): Image to be resized.
        size (sequence or int): Desired output size. If size is a sequence like
            (h, w), the output size will be matched to this. If size is an int,
            the smaller edge of the image will be matched to this number maintaing
            the aspect ratio. i.e, if height > width, then image will be rescaled to
            :math:`\left(\text{size} \times \frac{\text{height}}{\text{width}}, \text{size}\right)`
        interpolation (int, optional): Desired interpolation. Default is
            ``PIL.Image.BILINEAR``

    Returns:
        PIL Image: Resized image.
    """
    if isinstance(size, int):
        w, h = img.size
        if (w <= h and w == size) or (h <= w and h == size):
            return img
        if w < h:
            ow = size
            oh = int(size * h / w)
            return img.resize((ow, oh), interpolation)
        else:
            oh = size
            ow = int(size * w / h)
            return img.resize((ow, oh), interpolation)
    else:
        return img.resize(size[::-1], interpolation)

def letterbox_image(image, size, letterbox_image=False):
    w, h = size
    iw, ih = image.size
    if letterbox_image:
        '''resize image with unchanged aspect ratio using padding'''
        scale = min(w/iw, h/ih)
        nw = int(iw*scale)
        nh = int(ih*scale)

        image = image.resize((nw,nh), Image.BICUBIC)
        new_image = Image.new('RGB', size, (128,128,128))
        new_image.paste(image, ((w-nw)//2, (h-nh)//2))
    else:
        if h == w:
            new_image = resize(image, h)
        else:
            new_image = resize(image, [h ,w])
        new_image = center_crop(new_image, [h ,w])
    return new_image


def interval( X ):

    X.sort()

    if len(X) == 0: return 0

    S = []
    S.append( [ X[0], X[0]+1 ] )

    for x in X:
        if x > S[-1][1]:
            S.append( [ x, x+1 ] )

    return len(S), S

X = [ 0.25, 0.5, 1.6, 2.6, 2.7 ]
print(interval(X))

All-Unorganised/Exercises/Cw7/zad1 - pokrycie przedzialami jedn.py


#!python3
import os, sys, re, csv, sqlite3, string
from datetime import datetime, timedelta
from time import strptime, strftime
from itertools import chain

maxlinecount = 16000000
linecount = 0
filecount = 0

# Sample LIne
# 192.168.117.1 - - [20/Jun/2013:01:38:40 -0400] "GET /Dkrh8Ycb.htm HTTP/1.0" 404 210 "-" "Mozilla/5.00"

header = ["DATE_TIME","SRC_FILE","SRC_IP","REQ_TYPE","URI","HTTP_STATUS_CODE","BYTES","REFFER","USER_AGENT_STRING","RAW_LINE"]

def parsefile(fname,ofile):
	# REGEX Patterns
	http_bytes_useragentstring_pattern = re.compile("\"([A-Z]{1,10}|[a-z]{1,10}) (.{1,10000})\" (\d{1,1000}) (\d{1,1000}|-) \"(-|.{1,500})\" \"(.{1,500})\"")
	ip_address_pattern = re.compile('(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
	datetime_pattern = re.compile('\[([0-9]{2}\/[A-Z][a-z]{2}\/[0-9]{4}\:[0-9]{2}\:[0-9]{2}:[0-9]{2}) ([\+,\-][0-9]{4})\]')

	tmp_lcount = 0
	for line in open(fname,'r'):
		if line[0] != "#":
			output_line = []
			# Prefill the value of each field before starting loop
			finaldate = "-"
			srcip = "-"
			URL = "-"
			HTTPSTATUSCODE = "-"
			BYTES1 = "-"
			BYTES2 = "-"
			REFFER = "-"
			port = "-"
			useragentstring = "-"

			# # Check for NULL line
			# if line == " ":
			# 	continue

			# DATE
			rawdate = datetime_pattern.search(line)
			if rawdate:
				datetime2 = rawdate.group(1)
				offset = rawdate.group(2)
				finaldate = normalize_datetime(datetime2,offset)
			output_line.append(str(finaldate))

			#FILENAME
			try:
				output_line.append(fname)
			except:
				print("ERROR: Parsing FILENAME error on line")
				#print(line)
				error_file.write(line)
				continue

			#SRCIP and SRC_PORT
			srcip_atmp = ip_address_pattern.search(line)
			#srcinfo = status_type_url_port_pattern.search(line)

			if srcip_atmp:
				srcip = srcip_atmp.group().split(":")[0]
			output_line.append(srcip)

			#HTTPSTATUSCODE and BYTES
			bytesinfo = http_bytes_useragentstring_pattern.search(line)
			if bytesinfo:
				REQ_TYPE = bytesinfo.group(1)
				URL = bytesinfo.group(2)
				BYTES1 = bytesinfo.group(4)
				HTTPSTATUSCODE = bytesinfo.group(3)
				REFFER = bytesinfo.group(5)
				useragentstring = bytesinfo.group(6)

			output_line.append(REQ_TYPE)
			output_line.append(URL)
			output_line.append(HTTPSTATUSCODE)
			output_line.append(BYTES1)
			output_line.append(REFFER)
			output_line.append(useragentstring)

			# RAW_LINE
			output_line.append(line.strip())

			ofile.write('\t'.join(output_line)+"\n")
			tmp_lcount += 1
			#print(tmp_lcount)
	return tmp_lcount

def normalize_datetime(rawdate,offset):
	# RAW Date: 28/Feb/2015:00:05:03
	# RAW Offset: -0800
	tmpdate = datetime.strptime(rawdate,'%d/%b/%Y:%H:%M:%S')
	if offset[0:1] == "-":
		finaldate = tmpdate + timedelta(hours=int(offset[1:3]),minutes=int(offset[3:5]))
	if offset[0:1] == "+":
		finaldate = tmpdate - timedelta(hours=int(offset[1:3]),minutes=int(offset[3:5]))
	return finaldate

def getfilenames(fext):
	listoffiles = []
	for root, dirs, files in os.walk(os.curdir):
		for file in files:
			if file.endswith(fext):
				listoffiles.append(os.path.join(root, file))
	return listoffiles

def new_outfile(filecount):
	ofilename = "all_output_file_final_"+ str(filecount) +".log"
	print(ofilename)
	return open(ofilename,'w')

# Start the error file
error_file = open("unparsed_lines.log",'w')

# Start the first file and write the header
mofile = new_outfile(filecount)
mofile.write('\t'.join(header)+"\n")

# Get the list of filenames from the directory ending with the string provided to the function
lof = getfilenames(sys.argv[1])

# Cycle through list of filenames and parse the data
for fname in lof:
	linecount += parsefile(fname, mofile)
	if linecount > maxlinecount:
		mofile.close()
		filecount += 1
		mofile = new_outfile(filecount)
		mofile.write('\t'.join(header)+"\n")
		print('\t'.join(header))
		linecount = 0

# Close the final file
mofile.close()

# Close the error file
error_file.close()


#!/usr/bin/env python
# coding=utf-8

from setuptools import setup

package_name = 'spage'
filename = f'spage/__init__.py'

def get_version():
    import ast

    with open(filename) as input_file:
        for line in input_file:
            if line.startswith('__version__'):
                return ast.parse(line).body[0].value.s

def get_long_description():
    try:
        with open('README.md', 'r') as f:
            return f.read()
    except IOError:
        return ''

setup(
    name=package_name,
    version=get_version(),
    author='tuxbert',
    author_email='elliotpatonsimpson@gmail.com',
    description='SPAGE',
    url='https://github.com/tuxbert/spage',
    long_description=get_long_description(),
    packages=[package_name],
    entry_points={
        'console_scripts': [
            'spage = spage:spage'
        ]
    },
    license='License :: OSI Approved :: MIT License',
)


from django.shortcuts import render, redirect

from apps.library.models import Book

def index(request):
    context = {'books':  Book.objects.filter(user=request.user)}

    return render(request, 'library.html', context)

def book(request, book_id):
    """
    Individual book page displaying the progress of its exercises if they exists.

    :param book_id:
    :param request:
    :return:
    """
    context = {
        'book': Book.objects.get(user=request.user, id=book_id)
    }

    return render(request, 'book.html', context)

def submit(request):
    record = Book(
        user=request.user,
        title=request.POST['title'],
        subtitle=request.POST['subtitle'],
        author=request.POST['author'],
        edition=request.POST['edition'],
        read=True if request.POST['read'] == 'on' else False
    )
    record.save()

    return redirect('library')