import copy
import random
from typing import List, Dict
from genetic_algorithm.DNA import DNA

def costs_mapping(score: int) -> int:
    """returns the costs of the given ability score
    is needed for the point by method
    """
    if score == 8:
        return 0
    elif score == 9:
        return 1
    elif score == 10:
        return 2
    elif score == 11:
        return 3
    elif score == 12:
        return 4
    elif score == 13:
        return 5
    elif score == 14:
        return 7
    elif score == 15:
        return 9

def get_available_scores(points: int) -> int:
    """returns the remaining pointsto by scores for the other abilities
        is needed for the point by method
        """
    if points == 0:
        return 8
    elif points <= 1:
        return 9
    elif points <= 2:
        return 10
    elif points <= 3:
        return 11
    elif points <= 4:
        return 12
    elif points <= 5:
        return 13
    elif points >= 7:
        return 14
    elif points >= 9:
        return 15
    else:
        return 15

def p(costs: int, points: int) -> int:
    points = costs + points
    if points >= 9:
        return 15
    elif points >= 7:
        return 14
    elif points >= 5:
        return 13
    elif points >= 4:
        return 12
    elif points >= 3:
        return 11
    elif points >= 2:
        return 10
    elif points >= 1:
        return 9
    else:
        return 8

def get_costs(value_old: int, value_new: int) -> int:
    """returns the costs difference between an old value and a new value
    is needed for the point by method
    """
    costs_old = costs_mapping(value_old)
    costs_new = costs_mapping(value_new)
    costs = costs_old - costs_new
    # if the new value is bigger the the old invert the costs
    if value_new > value_old:
        costs *= -1
    return costs

def get_random_index() -> int:
    """get a random ability index from 0 to 5"""
    return random.randint(0, 5)

def get_random_ability_score(old_score: int, max_score: int) -> int:
    """get a random ability score between the given range"""
    if old_score < max_score:
        return random.randint(old_score, max_score)
    else:
        return old_score

def set_ability_scores_point_by(ability: Dict[int, List[int]] = None, index: int = None) -> List[int]:
    """set the ability scores random with the point by method
    27 point at the beginning or the given scores if this method is used for the mutation
    """
    if not ability:
        ability = {27: [8, 8, 8, 8, 8, 8]}
    # make a deep copy of the ability dict and get its values
    init = copy.deepcopy(ability)
    key_list = list(init.keys())
    points = copy.deepcopy(key_list[0])
    key = points
    scores: List[int] = copy.deepcopy(init[key])
    # print(points, scores)
    # do so long the points != 0 to provide solutions with remaining points
    while points != 0:
        scores = copy.deepcopy(init[key])
        points = key_list[0]
        # list of all ability index
        abilities: List[int] = [0, 1, 2, 3, 4, 5]
        if index:
            abilities.remove(index)
        # do while not all ability index has a value
        while len(abilities) > 0:
            index = random.choice(abilities)
            old_value = scores[index]
            costs_old = costs_mapping(old_value)
            max_value = p(costs_old, points)
            new_value = get_random_ability_score(old_value, max_value)
            costs = get_costs(old_value, new_value)
            points -= costs
            scores[index] = new_value
            abilities.remove(index)
            if points == 0:
                continue
    # pi = 0
    # for i in scores:
    #     pi += costs_mapping(i)
    # print(pi)
    # scores = [10, 15, 15, 8, 14, 8]
    return scores

class DNAGenerator:
    """the DNAGenerator builds the random start DNA and mutate the populations"""
    dna: DNA
    ability_points: int

    def __init__(self):
        """initialize an instance of the class DNAGenerator"""
        self.ability_points = 27
        self.dna = DNA()

    def get_random_character(self):
        """create a random DNA"""
        scores = set_ability_scores_point_by()
        self.dna.set_ability_scores(scores)
        list = self.set_proficiencies()
        self.set_equipment()
        self.set_fighting_style()
        self.set_background(list)
        self.set_race()

    def get_complete_random_character(self):
        for i in range(0, 6):
            a = random.randint(8, 15)
            self.dna.set_dna_value(a, i)
        a = random.randint(0, 16)
        self.dna.set_dna_value(a, 6)
        for i in range(7, 11):
            a = random.randint(0, 17)
            self.dna.set_dna_value(a, i)
        a = random.randint(0, 4)
        self.dna.set_dna_value(a, 11)
        a = random.randint(0, 5)
        self.dna.set_dna_value(a, 12)
        a = random.randint(0, 12)
        self.dna.set_dna_value(a, 13)
        a = random.randint(0, 1)
        self.dna.set_dna_value(a, 14)
        a = random.randint(0, 12)
        self.dna.set_dna_value(a, 15)

    def mutate_random_ability_score(self):
        i = random.randint(0, 5)
        old = self.dna.get_dna_value(i)
        a = random.randint(0, 1)
        if a == 0:
            self.dna.set_dna(old + 1)
        else:
            self.dna.set_dna(old - 1)

    def mutate_random_race(self):
        i = 6
        old = self.dna.get_dna_value(i)
        a = random.randint(0, 1)
        if a == 0:
            self.dna.set_dna(old + 1)
        else:
            self.dna.set_dna(old - 1)

    def get_random_fighting_style(self) -> int:
        """returns a random fightingstyle between 0 and 4"""
        return random.randint(0, 4)

    def set_fighting_style(self):
        """set the fightingstyle to DNA"""
        self.dna.set_fighting_style(self.get_random_fighting_style())

    def reduce_random_ability_score(self, index: int) -> Dict[int, List[int]]:
        """reduce a ability score to get new remaining points to by other values"""
        abilities_init = self.get_ability_scores()
        old_value = abilities_init[index]
        new_value = old_value
        points = 0
        range_value_mod = old_value - 8
        if range_value_mod > 1:
            change_value = 1
            new_value -= change_value
        else:
            new_value = 8
        costs = get_costs(old_value, new_value)
        points += costs
        abilities_init[index] = new_value
        d = {points: abilities_init}
        return d

    def mutate_ability_scores(self):
        """mutate the ability scores with reduce and point by"""
        index = get_random_index()
        abilities = self.reduce_random_ability_score(index)
        scores = set_ability_scores_point_by(abilities, index)
        self.dna.set_ability_scores(scores)

    def set_proficiencies(self) -> List[int]:
        """set random proficiencies to the DNA"""
        new_list: List[int] = []
        proficiencies: List[int] = [0, 1, 3, 5, 6, 7, 11]
        value_1 = random.choice(proficiencies)
        proficiencies.remove(value_1)
        new_list.append(value_1)
        value_2 = random.choice(proficiencies)
        proficiencies.remove(value_2)
        new_list.append(value_2)
        self.dna.set_proficiency_choice(value_1, value_2)
        return new_list

    def set_background(self, list: [int]):
        """set random background to the DNA"""
        proficiencies: List[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
        for skill in list:
            proficiencies.remove(skill)
        value_1 = random.choice(proficiencies)
        proficiencies.remove(value_1)
        value_2 = random.choice(proficiencies)
        proficiencies.remove(value_2)
        self.dna.set_background(value_1, value_2)

    def set_race(self):
        """set random race to the DNA between 0 and 116"""
        races: List[int] = [0, 1, 2, 3, 13, 14, 15, 16]
        race = random.choice(races)
        # race = random.randint(0, 16)
        self.dna.set_race(race)

    def set_ability_scores_standard_array(self):
        """set the ability score with the method standart array"""
        array: List[int] = [15, 14, 13, 12, 10, 8]
        abilities: List[int] = [0, 1, 2, 3, 4, 5]
        while len(abilities) > 0:
            index = random.choice(abilities)
            value = random.choice(array)
            self.dna.set_dna_value(value, index)
            array.remove(value)
            abilities.remove(index)

    def set_equipment(self):
        """set random equipment to the DNA
        Armor, Weapon, Shield, Second Weapon
        """
        weapon_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        second_weapon_list = [0, 8, 9]
        armor_list = [0, 1, 2, 3, 4, 5]
        if self.dna.dna[0] < 13:
            armor_list.remove(3)
        weapon = random.choice(weapon_list)
        armor = random.choice(armor_list)
        shield = 0
        second_weapon = 13
        if weapon == 0 or weapon == 8 or weapon == 9:
            choice = random.randint(0, 2)
            if choice == 0:
                shield = 1
            elif choice == 1:
                second_weapon = random.choice(second_weapon_list)

        elif weapon == 1 or weapon == 7 or weapon == 12:
            choice = random.randint(0, 1)
            if choice == 0:
                shield = 1
        self.dna.set_equipment(armor, weapon, shield, second_weapon)

    def permutate_ability_scores(self):
        """switch two ability score"""
        abilities: List[int] = [0, 1, 2, 3, 4, 5]

        index_1 = random.choice(abilities)
        abilities.remove(index_1)
        value_1 = self.dna.get_dna_value(index_1)

        index_2 = random.choice(abilities)
        abilities.remove(index_2)
        value_2 = self.dna.get_dna_value(index_2)
        self.dna.set_dna_value(value_1, index_2)
        self.dna.set_dna_value(value_2, index_1)

    def get_ability_scores(self) -> List[int]:
        """get the ability scores from DNA"""
        abilities: List[int] = []
        for a in range(6):
            abilities.append(self.dna.get_dna_value(a))
        return abilities

    def mutate_fighting_style(self):
        """mutate the fighting style"""
        styles: List = [0, 1, 2, 3, 4]
        styles.remove(self.dna.get_dna_value(11))

        new_style = random.choice(styles)
        self.dna.set_fighting_style(new_style)

    def mutate_weapon(self):
        """mutate the weapon"""
        equipment = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        armor = self.dna.get_dna_value(12)
        weapon = self.dna.get_dna_value(13)
        shield = self.dna.get_dna_value(14)
        second_weapon = self.dna.get_dna_value(15)
        equipment.remove(weapon)
        weapon = random.choice(equipment)

        # check if the weapon can use a second weapon or shield
        if weapon != 0 or weapon != 8 or weapon != 9:
            second_weapon = 13

        # check if the weapon can use a shield
        if weapon != 1 or weapon != 7 != 12:
            second_weapon = 13
            shield = 0

        self.dna.set_equipment(armor, weapon, shield, second_weapon)

    def mutate_shield(self):
        """mutate the shield"""
        equipment = [0, 1, 7, 8, 9, 12]
        armor = self.dna.get_dna_value(12)
        weapon = self.dna.get_dna_value(13)
        shield = self.dna.get_dna_value(14)
        second_weapon = self.dna.get_dna_value(15)
        if shield != 0:
            shield = 1
            second_weapon = 13

            # check if the weapon ca use a shield
            if weapon != 1 or weapon != 7 or weapon != 0 or weapon != 8 or weapon != 9 or weapon != 12:
                weapon = random.choice(equipment)
        else:
            shield = 0
        self.dna.set_equipment(armor, weapon, shield, second_weapon)

    def mutate_second_weapon(self):
        """mutate the second weapon"""
        second_weapon_list = [13, 0, 8, 9]
        weapon_list = [0, 8, 9]
        armor = self.dna.get_dna_value(12)
        weapon = self.dna.get_dna_value(13)
        shield = self.dna.get_dna_value(14)
        second_weapon = self.dna.get_dna_value(15)
        second_weapon_list.remove(second_weapon)

        # check if not shield and second weapon
        if second_weapon == 13:
            shield = 0
        second_weapon = random.choice(second_weapon_list)

        if second_weapon != 13:

            # check if weapon can use a second weapon
            if weapon != 0 or weapon != 8 or weapon != 9:
                weapon = random.choice(weapon_list)
        self.dna.set_equipment(armor, weapon, shield, second_weapon)

    def mutate_armor(self):
        """mutate the json_files"""
        equipment = [0, 1, 2, 3, 4, 5]
        armor = self.dna.get_dna_value(12)
        equipment.remove(armor)

        # check if strength >= 13 do get heavy json_files
        if armor is not 3 and self.dna.dna[0] < 13:
            equipment.remove(3)
        armor = random.choice(equipment)
        weapon = self.dna.get_dna_value(13)
        shield = self.dna.get_dna_value(14)
        second_weapon = self.dna.get_dna_value(15)
        self.dna.set_equipment(armor, weapon, shield, second_weapon)

    def mutate_race(self):
        """mutate the race"""
        races: List[int] = [0, 1, 2, 3, 13, 14, 15, 16]
        # races: List[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
        race = self.dna.get_dna_value(6)
        races.remove(race)
        new_race = random.choice(races)
        self.dna.set_race(new_race)

    def mutate_proficiencies(self):
        """mutate the proficiencies"""
        proficiencies: List[int] = [0, 1, 3, 5, 6, 7, 11]
        prof: List[int] = []
        for i in range(7, 9):
            value = self.dna.get_dna_value(i)
            prof.append(value)
            if value in proficiencies:
                proficiencies.remove(value)

        index = random.randint(0, 1)
        new_prof = random.choice(proficiencies)

        prof[index] = new_prof

        self.dna.set_proficiency_choice(prof[0], prof[1])

    def mutate_background(self):
        """mutate the background"""
        proficiencies: List[int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
        prof: List[int] = []
        for i in range(9, 11):
            value = self.dna.get_dna_value(i)
            prof.append(value)
            if value in proficiencies:
                proficiencies.remove(value)

        index = random.randint(0, 1)
        new_prof = random.choice(proficiencies)

        prof[index] = new_prof

        self.dna.set_background(prof[0], prof[1])


import argparse
import datetime
import os
import tempfile
import subprocess
import shutil
import distutils.util
import distutils.dir_util
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
import utils._utils as utils

targets = ["bdist_wheel", "sdist", "install"]

parser = argparse.ArgumentParser()
parser.add_argument("--python-dir",
                    default="include",
                    help="Path to the folder containing the python files")
parser.add_argument(
    "--include-dir",
    default="include",
    help="Path to the include folder needed to compile the wheel")
parser.add_argument(
    "--lib-dir",
    default="lib",
    help=
    "Path to the folder containing the libraries needed to compile the wheel")
parser.add_argument(
    "--standalone",
    help=("Colon separated list of folders to add to the lib folder of the "
          "sdist / wheel package"))
parser.add_argument("target",
                    choices=targets,
                    help=f"Which target to build: {targets}")
parser.add_argument("--output-dir",
                    default="dist",
                    help="Where to create the packages")
args = parser.parse_args()

def get_version_from_requirements(package):
    with open(os.path.join(src_dir, 'requirements.txt'), 'r') as f:
        for line in f:
            if package in line and not 'cpu' in line:
                name_and_version = line.split(';')[0].split('=')
                return name_and_version[-1].strip()

    return None

def get_torch_dependency(package, version):
    if "aarch64" in utils.get_arch_type():
        # There is no +cpu variant of Torch on Arm
        return f'{package}=={version}'
    # For sdist packages we don't know ahead of time what the python version
    # will be, and there is no support for --find-links so we just have to
    # use the regular torch wheel instead.
    if args.target != "bdist_wheel":
        return f'{package}=={version}'
    return f"{package} @ https://download.pytorch.org/whl/cpu/{package}-{version}%2Bcpu-{get_abbr_impl()}{get_impl_ver()}-{get_abi_tag()}-{PLATFORM}.whl"

def get_poptorch_version():
    version = utils.PkgInfo.load_from_file(must_exist=False,
                                           path="..").version_long
    if args.standalone is not None:
        # Only 1 "+" symbol allowed per version
        separator = "+" if "+" not in version else "_"
        version += separator + "standalone"
    return version

VERSION = get_poptorch_version()

# https://www.python.org/dev/peps/pep-0425/
# The platform tag is simply distutils.util.get_platform() with all hyphens - and periods . replaced with underscore _.
PLATFORM = distutils.util.get_platform().replace(".", "_").replace("-", "_")

torch_ver = utils.get_required_torch_version()
TORCH_DEPENDENCY = get_torch_dependency('torch', torch_ver)

src_dir = utils.sources_dir()
# torch{audio, vision} are added here to prevent the torch upgrade when other
# packages depend on torch{audio, vision}.
torchaudio_ver = get_version_from_requirements('torchaudio')
TORCHAUDIO_DEPENDENCY = get_torch_dependency('torchaudio', torchaudio_ver)

torchvision_ver = get_version_from_requirements('torchvision')
TORCHVISION_DEPENDENCY = get_torch_dependency('torchvision', torchvision_ver)

# Only keep files of a given extension
class ExtOnly:
    def __init__(self, *ext):
        self.ext = ext

    def _is_ignored(self, file):
        return not any(file.endswith(ext) for ext in self.ext)

    def __call__(self, adir, filenames):
        # Return the files to ignore
        return [f for f in filenames if self._is_ignored(f)]

include_dir = os.path.realpath(args.include_dir)
lib_dirs = [os.path.realpath(args.lib_dir)]
if args.standalone is not None:
    lib_dirs += [os.path.realpath(l) for l in args.standalone.split(":")]
output_dir = os.path.realpath(args.output_dir)
python_dir = os.path.realpath(args.python_dir)

def configure(src_filename, dst_filename):
    with open(dst_filename, "w") as f:
        for line in open(src_filename):
            f.write(
                line.replace("@VERSION@", VERSION) \
                    .replace("@PLATFORM@", PLATFORM) \
                    .replace("@TORCH_DEPENDENCY@", TORCH_DEPENDENCY) \
                    .replace("@TORCHAUDIO_DEPENDENCY@", TORCHAUDIO_DEPENDENCY) \
                    .replace("@TORCHVISION_DEPENDENCY@", TORCHVISION_DEPENDENCY)
            )

# Create a temporary directory and copy the files to package to it.
with tempfile.TemporaryDirectory() as tmp_dir:
    os.chdir(tmp_dir)
    shutil.copytree(os.path.join(src_dir, "python"),
                    "src",
                    ignore=ExtOnly(".cpp"))
    shutil.copytree(python_dir, "poptorch")
    # distutils won't throw an exception if the destination already exists,
    # which will happen if lib_dirs contains more than one element.
    for lib_dir in lib_dirs:
        distutils.dir_util.copy_tree(lib_dir, "poptorch/lib")
    shutil.copytree(include_dir, "include")
    shutil.copy(os.path.join(src_dir, "MANIFEST.in"), ".")
    shutil.copy(os.path.join(src_dir, 'setup.cfg'), '.')
    shutil.copy(os.path.join(src_dir, 'License.txt'), '.')
    shutil.copy(os.path.join(src_dir, 'poptorch_third_party_licenses.txt'),
                '.')

    configure(os.path.join(src_dir, "setup.py"), "setup.py")
    configure(os.path.join(src_dir, "pyproject.toml"), "pyproject.toml")

    # distutils doesn't like spaces in CXX (https://github.com/mapnik/python-mapnik/issues/99#issuecomment-527591113)
    env = {**os.environ}
    cc = env.get("CC", "gcc")
    cxx = env.get("CXX", "g++")
    # Only keep the real compiler: e.g "cmake gcc" -> "gcc"
    cc = cc.split(" ")[-1]
    cxx = cxx.split(" ")[-1]
    env["CXX"] = cxx
    env["CC"] = cc
    start = datetime.datetime.now()
    if args.target == "install":
        subprocess.check_call(
            f"python3 setup.py build_ext -b {output_dir}".split(), env=env)
        dst_dir = f"{output_dir}/poptorch/lib"
        if os.path.isdir(dst_dir):
            shutil.rmtree(dst_dir)
        shutil.copytree("poptorch/lib", dst_dir, ignore=ExtOnly(".so"))
    else:
        extra_opts = ""
        if args.target == "sdist":
            extra_opts = "--formats=zip"
        subprocess.check_call(
            f"python3 setup.py {args.target} -d {output_dir} {extra_opts}".
            split(),
            env=env)
    print(f"Time to generate {args.target} in {output_dir} : "
          f"{datetime.datetime.now()-start}")


#项目的初始化文件
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_wtf import CSRFProtect  #csrf保护
db=SQLAlchemy()
migrate=Migrate()
csrf=CSRFProtect()  #实例化
def createApp(obj):
    # 实例化app
    app = Flask(__name__)
    app.config.from_object(obj)
    db.init_app(app)  #惰性加载
    migrate.init_app(app,db)
    csrf.init_app(app)
    from blueprint.course import course_bl
    from blueprint.user import user_bl
    # 将申明好的蓝图注册进来
    app.register_blueprint(course_bl, url_prefix="/course")
    app.register_blueprint(user_bl, url_prefix="/user")
    #使用装饰器，注册一个要被执行的方法，请求钩子
    @app.before_first_request
    def first_request():
        print("before_first_request")
    @app.before_request
    def req():
        print(" before_request")
    # @app.after_request
    # def afger_req(response):
    #     print("after_request")
    # @app.teardown_request
    # def teardown_req(response):
    #     print("teardown_request")
    #     return response

    return app


import os
import torch
import torch.nn as nn
from VAE import VAE
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.nn import DataParallel
from small_cnn import SmallCNN
import torchvision
import torchvision.utils as vutils
import torchvision.transforms as transforms
import torchvision.models as models
from torchvision.datasets import ImageFolder
import pdb
DATA_PATH = '~/data/'
import argparse
from tqdm import tqdm
from loguru import logger
from utils import get_args

def generate(vae, model, dataloader, device):
    model.eval()
    labels_cnt = [0] * 10
    with torch.no_grad():
        for batch_idx, (inputs, targets) in tqdm(enumerate(dataloader), total=len(dataloader), desc="testing"):
            inputs = inputs.to(device)
            recon, _, _ = vae(inputs.view(-1,28*28).to(device), device)
            recon_img = recon.view(-1, 1, 28 ,28)
            outputs = model(recon_img.to(device))
            _, predicted = outputs.max(1)
            label = int(targets.item())
            predicted = int(predicted.item())
            if label != predicted:
                label_cnt = labels_cnt[label]
                output_dir = f'hardimgs/{label}'
                if not os.path.exists(output_dir):
                   os.makedirs(output_dir)
                vutils.save_image(torch.cat([inputs.squeeze().cpu(), recon_img.squeeze().cpu()],dim=0),f'{output_dir}/{label_cnt}.png')
                labels_cnt[label] += 1

def main():
    args = get_args()

    log_file_name = args.log_file
    logger.add(log_file_name)
    logger.info('args:\n' + args.__repr__())

    batch_size = args.batch_size
    output_dir = args.save_path
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    device = args.device
    best_acc = 0  # best test accuracy

    # Data
    logger.info('==> Preparing data..')
    if args.dataset == 'MNIST':
        transform=transforms.Compose([
            transforms.ToTensor()
        ])
        transform_train = transform
        tramsform_test = transform
    else:
        raise NotImplementedError

    epochs = args.epochs
    GEN_PATH = f'/home/linhw/myproject/Attack-Vae/gen_imgs-{epochs}'
    genset = torchvision.datasets.MNIST(DATA_PATH, train=False, transform=transforms.Compose(
        [
            transforms.ToTensor()
        ]
    ))
    genloader = torch.utils.data.DataLoader(genset, batch_size=1, shuffle=False, num_workers=8)
    # Model
    logger.info('==> Building model..')
    if args.model == 'smallCNN':
        model = SmallCNN()
    model = model.to(device)
    model.load_state_dict(torch.load(args.params, map_location=device)['state_dict'])
    vae = VAE()
    vae = vae.to(device)
    vae.load_state_dict(torch.load('/home/linhw/myproject/Attack-Vae/checkpoint/model/500epoch_model.pt', map_location=device))

    generate(vae, model, genloader, device)
    logger.info("generate finished")

if __name__ == '__main__':
    main()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

class VehicleData:
    def __init__(self, filepath, columns=["index", "id", "x", "y"]) -> None:
        """Initialize the VehicleData instance with data provided in the path.

        Args:
            filepath (str): Path to the data file.
            columns (list): Column names of given data.
        """
        try:
            self.df = pd.DataFrame(
                np.load(filepath),
                columns=columns,
            )
        except Exception as e:
            print(f"An error occurred: {e}")
            self.df = None

    def segment_by_id(self, id):
        """Selects a subset from the data using a specific id.

        Args:
            id (int): To get all the samples associated with the id number.

        Returns:
            Dataframe: Subset of data associated with the id, Empty otherwise.
        """
        if self.df is not None:
            return self.df[self.df["id"] == id]
        return pd.DataFrame()

    def filter(self, fun, segment_id=0):
        """To filter based on input functions.

        Args:
            fun (function): Filter function.
            segment_id (int): To get the trajectory associated with the id number.

        Returns:
            Dataframe: List of latitudinal and longitudinal values of the trajectory, Empty otherwise.
        """
        return fun(self.df, segment_id)

    def plot(self, data):
        """Plots only filtered data segment.

        Args:
            data (Dataframe): Filtered segment data to plot.
        """
        plt.scatter(data["x"], data["y"])
        plt.show()


import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import json
from test import ask_google
import googleSearch
import webbrowser
import time
import signal
import os

listener = sr.Recognizer()
engine = pyttsx3.init()
voice = engine.getProperty('voices')
engine.setProperty('voice', 'com.apple.speech.synthesis.voice.Alex')
engine.setProperty('rate',250)

class TimeoutException(Exception):   # Custom exception class
    pass

def break_after(seconds=2):
    def timeout_handler(signum, frame):   # Custom signal handler
        raise TimeoutException

    def function(function):
        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, timeout_handler)
            signal.alarm(seconds)
            try:
                res = function(*args, **kwargs)
                signal.alarm(0)      # Clear alarm
                return res
            except TimeoutException:
                print('Oops, timeout: %s sec reached.' % seconds, function.__name__, args, kwargs)
            return
        return wrapper
    return function

#@break_after(10)
def talk(text):
    engine.say(text)
    engine.runAndWait()

def takeCommand():
    try:
        with sr.Microphone() as source:
            print("Listening.....")
            voices = listener.listen(source,timeout=5, phrase_time_limit=5)
            command = listener.recognize_google(voices).lower()
    except Exception as e:
        print(e)
    return command

def validate_input(input,commands):
    for cmd in commands:
        if cmd in input:
            return True

def launch(voice_command,launch_dict):
    for key,values in launch_dict.items():
        if key in voice_command:
            return values

def runAlexa():
    while True:
        command = takeCommand().strip().lower()
        print(command)
        quesWords = ['who', 'why']
        primeWords = ['minister', 'president']
        numerals = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth']
        launch_cmd = ['open', 'launch', 'run']
        greeting_cmd = ['hello jarvis','hi jarvis','wake up jarvis']
        grateful_cmd = ['thank you jarvis','thanks jarvis','thank you very much jarvis','thanks','thank you']
        launch_dict = {'app store': '/Volumes/Omega/System/Applications/App\ Store.app', 'pycharm':'/Applications/PyCharm.app',
                       'downloads': '/Users/macedon/Downloads', 'library':'/Library','current directory':'.',
                       'youtube':"https://www.youtube.com",'clion':'/Applications/CLion.app'}

        if validate_input(command,greeting_cmd):
            talk("Hello Sir, how may I help you ?")
            continue
        elif validate_input(command,launch_cmd):
            path = launch(command,launch_dict)
            if os.uname().sysname == 'Darwin':
                cmd = 'open'
            else:
                cmd = 'explorer'
            cmd = cmd+" "+path
            talk("Opening Sir")
            os.system(cmd)
            continue
        elif validate_input(command,grateful_cmd):
            talk('It is my pleasure to serve you sir')
            continue
        elif 'play' in command:
            song = command.replace('play', '')
            talk('Playing ' + song)
            pywhatkit.playonyt(song)
            continue
        elif 'time' in command:
            time = datetime.datetime.now().strftime('%I:%M %p')
            talk("Current time is " + time)
            continue
        elif 'when' in command:
            answer = ask_google(command)
            talk(answer)
            continue
        elif not any(num in command for num in numerals):
            print('1')
            if any(word in command for word in primeWords):
                print('2')
                ind = command.index('the')
                s = command[0:ind + 4] + 'current ' + command[ind + 4:]
                answer = ask_google(s)
                talk(answer)
            elif any(word in command for word in quesWords):
                print('3')
                answer = ask_google(command)
                talk(answer)
                talk("Do you want additional information from web ?")
            else:
                print('4')
                answer = ask_google(command)
                talk(answer)
            continue
        elif any(num in command for num in numerals):
            print('5')
            if any(word in command for word in quesWords):
                print('6')
                if 'india' in command:
                    print('7')
                    answer = ask_google(command)
                    talk(answer)
                else:
                    print('8')
                    url = googleSearch.Search(command)
                    talk("Here is what I found on web ")
                    webbrowser.get().open(url)
            continue
        else:
            print("9")

runAlexa()


import pygame
from src.UI.Button import *

class Menu:

    def __init__(self, container, *btns):
        self.container = container
        self.btnList = []
        self.mainFont = ('freesansbold.ttf', 32)
        self.largeFont = ('freesansbold.ttf',50)

        self.hovered = None

        for btnId in range ( len(btns)): #init buttons
            self.btnList.append(Button(btns[btnId][0], btns[btnId][1]))
            self.placeBtn(btnId)

    def btnDefault(self, btnId): #reset selected button to its default state/font
        self.btnList[btnId].fontUpdate(self.mainFont, "white")
        self.placeBtn(btnId)

    def btnHovered(self, btnId):
        self.btnList[btnId].fontUpdate(self.largeFont, "white")
        self.placeBtn(btnId)

    def placeBtn(self, btnId):
        self.btnList[btnId].rect.center = (self.container.get_width() // 2, self.container.get_height() // 4 * (1 + (btnId * 0.4)))

    def isHovering(self, btnId, pos):
        if(self.btnList[btnId].rect.collidepoint(pos)):
            self.hovered = btnId
            return True

    def btnBlit(self, btnId):
        self.btnList[btnId].blitSelf(self.container)

    def blitAll(self):
        for btnId in range (len(self.btnList)):
            self.btnBlit(btnId)

#This function is called to check if a button is hovered or clicked and apply the different visual effects or return an action to call.
    def eventHandler(self, type, pos): #get event type and pos
        for btnId in range (len(self.btnList)): #iterate all the buttons of the menu
            if self.isHovering(btnId, pos): #if the iterated button is hovered
                if type == pygame.MOUSEBUTTONDOWN: #if the event type = mouse click
                    return self.btnList[btnId].command
                elif type == pygame.MOUSEMOTION: #if the event type = mouse motion
                    self.btnHovered(btnId)
                    return None

        if (self.hovered != None):
            self.btnDefault(self.hovered)
            self.hovered = None
        return None

tictactoe (with class objects)/app/client/src/UI/Menu.py


# coding: utf-8
"""
Set of utility function used throughout the app
"""
from uuid import uuid4
import hashlib
import re
from google.appengine.ext import ndb #pylint: disable=import-error
import config
from pydash import _
import itertools, operator

EMAIL_REGEX = r'^[-!#$%&\'*+\\.\/0-9=?A-Za-z^_`{|}~]+@([-0-9A-Za-z]+\.)+([0-9A-Za-z]){2,4}$'

# see: https://gist.github.com/dperini/729294 (PYTHON PORT)
URL_REGEX = r'^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:/\S*)?$'

def uuid():
    """Generates random UUID used as user token for verification, reseting password etc.

    Returns:
      string: 32 characters long string

    """
    return uuid4().hex

def create_name_from_email(email):
    """Function tries to recreate real name from email address

    Examples:
      >>> create_name_from_email('bobby.tables@email.com')
      Bobby Tables
      >>> create_name_from_email('bobby-tables@email.com')
      Bobby Tables

    Args:
      email (string): Email address

    Returns:
      string: Hopefully user's real name

    """
    return re.sub(r'_+|-+|\.+|\++', ' ', email.split('@')[0]).title()

def password_hash(password):
    """Hashes given plain text password with sha256 encryption
    Hashing is salted with salt configured by admin, stored in >>> model.Config

    Args:
        password (string): Plain text password

    Returns:
        string: hashed password, 64 characters

    """
    sha = hashlib.sha256()
    sha.update(password.encode('utf-8'))
    sha.update(config.CONFIG_DB.salt)
    return sha.hexdigest()

def list_to_dict(input_list):
    """Creates dictionary with keys from list values
    This function is primarily useful for converting passed data from Angular checkboxes,
     since angular ng-model can't return list of checked group of checkboxes, instead
     it returns something like {'a': True, 'b': True} for each checkbox

    Example:
        >>> list_to_dict(['a', 'b'])
        {'a': True, 'b': True}

    Args:
        input_list (list): List of any type

    Returns:
        dict: Dict with 'True' values
    """
    return _.zip_object(input_list, _.map(input_list, _.constant(True)))

def dict_to_list(input_dict):
    """Creates list from dictionary with true booloean values
    This function is primarily useful for converting passed data from Angular checkboxes,
     since angular ng-model can't return list of checked group of checkboxes, instead
     it returns something like {'a': True, 'b': True} for each checkbox

    Example:
        >>> dict_to_list({'a': True, 'b': True, 'c': False})
        ['a', 'b']

    Args:
        input_dict (dict): Dict with boolean values

    Returns:
        list: list of truthful values
    """
    return _.keys(_.pick(input_dict, _.identity))

def constrain_string(string, minlen, maxlen):
    """Validation function constrains minimal and maximal lengths of string.

    Args:
        string (string): String to be checked
        minlen (int): Minimal length
        maxlen (int): Maximal length

    Returns:
        string: Returns given string

    Raises:
        ValueError: If string doesn't fit into min/max constrains

    """

    if len(string) < minlen:
        raise ValueError('Input need to be at least %s characters long' % minlen)
    elif len(string) > maxlen:
        raise ValueError('Input need to be maximum %s characters long' % maxlen)
    return string

def constrain_regex(string, regex):
    """Validation function checks validity of string for given regex.

    Args:
        string (string): String to be checked
        regex (string): Regular expression

    Returns:
        string: Returns given string

    Raises:
        ValueError: If string doesn't match regex

    """
    regex_email = re.compile(regex, re.IGNORECASE)
    if not regex_email.match(string):
        raise ValueError('Incorrect regex format')
    return string

def create_validator(lengths=None, regex='', required=True):
    """This is factory function, which creates validator functions, which
    will then validate passed strings according to lengths or regex set at creation time

    Args:
        lengths (list): list of exact length 2. e.g [3, 7]
            indicates that string should be between 3 and 7 charactes
        regex (string): Regular expression
        required (bool): Wheter empty value '' should be accepted as valid,
            ignoring other constrains

    Returns:
        function: Function, which will be used for validating input
    """

    def validator_function(value, prop):
        """Function validates input against constraints given from closure function
        These functions are primarily used as ndb.Property validators

        Args:
            value (string): input value to be validated
            prop (string): ndb.Property name, which is validated

        Returns:
            string: Returns original string, if valid

        Raises:
            ValueError: If input isn't valid

        """
        # when we compare ndb.Property with equal operator e.g User.name == 'abc' it
        # passes arguments to validator in different order than as when e.g putting data,
        # hence the following parameters switch
        if isinstance(value, ndb.Property):
            value = prop
        if not required and value == '':
            return ''
        if regex:
            return constrain_regex(value, regex)
        return constrain_string(value, lengths[0], lengths[1])

    return validator_function

###############################################################################
# Request Parameters
###############################################################################
def param(name, cast=None):
    return None
    #value = None
    #if flask.request.json:
    #    return flask.request.json.get(name, None)

    #if value is None:
    #    value = flask.request.args.get(name, None)
    #if value is None and flask.request.form:
    #    value = flask.request.form.get(name, None)

    #if cast and value is not None:
    #    if cast is bool:
    #        return value.lower() in ['true', 'yes', 'y', '1', '']
    #    if cast is list:
    #        return value.split(',') if len(value) > 0 else []
    #    if cast is ndb.Key:
    #        return ndb.Key(urlsafe=value)
    #    return cast(value)
    #return value

###############################################################################
# Model manipulations
###############################################################################
def get_dbs(
    query, order=None, limit=None, cursor=None, keys_only=None, **filters
  ):
  limit = limit or config.DEFAULT_DB_LIMIT
  cursor = Cursor.from_websafe_string(cursor) if cursor else None
  model_class = ndb.Model._kind_map[query.kind]

  for prop in filters:
    if filters.get(prop, None) is None:
      continue
    if isinstance(filters[prop], list):
      for value in filters[prop]:
        query = query.filter(model_class._properties[prop] == value)
# new custom wodor app -------------
    elif isinstance(filters[prop], dict):
      if filters[prop]['test'] == '>':
        query = query.filter(model_class._properties[prop] > filters[prop]['value'])
      elif filters[prop]['test'] == '>=':
        query = query.filter(model_class._properties[prop] >= filters[prop]['value'])
      elif filters[prop]['test'] == '<':
        query = query.filter(model_class._properties[prop] < filters[prop]['value'])
      elif filters[prop]['test'] == '<=':
        query = query.filter(model_class._properties[prop] < filters[prop]['value'])
      elif filters[prop]['test'] == '==':
        query = query.filter(model_class._properties[prop] == filters[prop]['value'])
      elif filters[prop]['test'] == '!=':
        query = query.filter(model_class._properties[prop] != filters[prop]['value'])
      elif filters[prop]['test'] == 'IN':
        values = filters[prop]['value']
        if isinstance(values, list):
          values = filters[prop]['value']
        else:
           values = values.split(',')
        query = query.filter(model_class._properties[prop].IN(values))
        query = query.order(model_class._key)
      query = query.order(model_class._properties[prop]) # TODO does it work?
    else:
      query = query.filter(model_class._properties[prop] == filters[prop])
# ----------------------------------

  if order:
    for o in order.split(','):
      if o.startswith('-'):
        query = query.order(-model_class._properties[o[1:]])
      else:
        query = query.order(model_class._properties[o])
  model_dbs, next_cursor, more = query.fetch_page(
      limit, start_cursor=cursor, keys_only=keys_only,
    )
  next_cursor = next_cursor.to_websafe_string() if more else None
  return list(model_dbs), next_cursor

def sort_uniq(sequence,key_sort=None,key_group=None,reverse=False):
    sorted_seq = list(sorted(sequence,key=operator.itemgetter(key_sort), reverse=reverse))
    #return sorted_seq
    uniq_seq = {v[key_group]:v for v in sorted_seq}.values()
    return list(sorted(uniq_seq,key=operator.itemgetter(key_sort), reverse=not reverse))


from django.shortcuts import render, get_object_or_404

# Create your views here.

from django.http import HttpResponse

from .models import Department, DepartmentSubPage

def departmentdetail(request):
  department = Department.objects.filter(url=request.path).first()
  departmentsubpage = DepartmentSubPage.objects.filter(url=request.path).first()
  if department:
    page = department
  elif departmentsubpage:
    page = departmentsubpage
  pageopts = page._meta
  department_subpages = DepartmentSubPage.objects.filter(parent__department__url=request.path).filter(deleted=0).filter(published=1)
  department_children = Department.objects.filter(parent__url=request.path)
  return render(request, 'departments/departmentdetail.html', {'page': page,'pageopts': pageopts,  'department_subpages': department_subpages, 'department_children': department_children})


#jupyter magic
#wrote code below to file.py
# see new file!

from mrjob.job import MRJob

class MRWordFrequencyCount(MRJob):

    def mapper(self, _, line): # FOCUS ON
        #underscore means we will not be using this arg at all
        #go line by line in text
        yield "chars", len(line)
        yield "words", len(line.split()) #take a line and split by whitespace
        yield "lines", 1
        #yield creates a 'generator', we're making generators to not store stuff in memory

    def reducer(self, key, values ): #FOCUS ON
        yield key, sum(values)
                #values is a list of number of words

if __name__ == '__main__':
    MRWordFrequencyCount.run()


from flask import Flask, request, render_template
from flask_cors import cross_origin
import sklearn
import pickle
import pandas as pd

app = Flask(__name__)
model = pickle.load(open("oil_price.pkl","rb"))

@app.route("/")
@cross_origin()
def home():
    return render_template("petrol.html")

@app.route("/predict", methods = ["GET", "POST"])
@cross_origin()
def predict():
    if request.method == "POST":
        da=request.form["date"]
        day=int(pd.to_datetime(da, format="%Y-%m-%d").day)
        month=int(pd.to_datetime(da, format="%Y-%m-%d").month)
        year=int(pd.to_datetime(da, format="%Y-%m-%d").year)

        prediction=model.predict([[day,month,year]])
        output=round(prediction[0],2)
        return render_template('petrol.html',prediction_text="Petrol price is USD($). {}".format(output))
    return render_template("petrol.html")

if __name__ == "__main__":
    app.run(debug=True)


# -*- coding: utf-8 -*-
#harvest theses from TU Munich
#FS: 2019-09-24
#FS: 2022-11-24

import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
import re
import os
import ejlmod3
import time

publisher = 'Munich, Tech. U.'
jnlfilename = 'THESES-TUM-%s' % (ejlmod3.stampoftoday())
rpp = 100
years = 2
skipalreadyharvested = True

boring = ['Fakultät für Sport- und Gesundheitswissenschaften']
boring += ['CHE Chemie', 'BAU 550', 'BAU 650', 'BAU 650', 'UMW 300', 'BAU 651', 'BAU 900', 'BAU 950',
           'BAU Bauingenieurwesen, Vermessungswesen', 'GEO Geowissenschaften',
           'BAU Bauingenieurwesen, Vermessungswesen', 'BIO 110','BIO 250', 'BIO Biowissenschaften',
           'BRA 200', 'BRA Brauwesen', 'ERG Energietechnik, Energiewirtschaft', 'FER 000',
           'FER Fertigungstechnik', 'FOR 110', 'FOR Forstwissenschaften', 'IND 020',
           'LEB Lebensmitteltechnologie', 'MED 040', 'MED 535', 'MED 320', 'MED 410', 'MED 430',
           'MED 320', 'MED 430', 'MED 450', 'MED Medizin', 'VER 630', 'MAS 590',
           'VER Technik der Verkehrsmittel', 'WIR 531', 'WIR 700', 'WIR 800', 'WIR 895',
           'WIR Wirtschaftswissenschaften']
boring += ['ARC 045', 'ARC 165', 'ARC 279', 'ARC 370', 'ARC Architektur', 'BAU 005', 'BAU 050',
           'BAU 150', 'BAU 450', 'BAU 967', 'BIO 120', 'BIO 140', 'BIO 300', 'BIO 780', 'BIO 950',
           'CHE 140', 'CHE 167', 'CHE 513', 'CHE 800', 'CHE 880', 'CIT 001', 'CIT 200', 'CIT 280',
           'CIT 325', 'CIT 335', 'CIT 680', 'CIT 900', 'CIT 960',
           'CIT Chemie-Ingenieurwesen, Technische Chemie, Biotechnologie', 'EDU 640',
           'EDU Erziehungswissenschaften', 'GES Geschichte', 'IND Sonstige Industrien',
           'INF Informationswesen, Bibliotheks-, Dokumentations-, Archiv-, Museumswesen',
           'Ingenieurfakultät Bau Geo Umwelt', 'KUN 850', 'KUN Kunst', 'LAN 120', 'LAN 332',
           'LAN 490', 'LAN 610', 'LAN 640', 'LAN Landbauwissenschaft', 'LEB 020', 'LEB 050',
           'LEB 054', 'MAS 000', 'MAS 520', 'MAS 700', 'MAS Maschinenbau', 'MED 230', 'MED 280',
           'MED 370', 'MED 385', 'MED 403', 'MED 580', 'MED 600', 'MED 670', 'MTA 000', 'MTA 009',
           'MTA 300', 'MTA 309', 'MTA 600', 'MTA 900',
           'MTA Technische Mechanik, Technische Thermodynamik, Technische Akustik', 'OEK 100',
           'OEK 160', 'OEK Ökotrophologie, Ernährungswissenschaft', 'POL 650', 'POL Politologie',
           'PSY Psychologie', 'RPL 000', 'RPL 734', 'RPL 900', 'RPL Raumplanung, Raumordnung',
           'SOZ 700', 'SPO 570', 'SPO 600', 'SPO 610', 'SPO 617', 'SPO 630', 'SPO 650', 'SPO 680',
           'SPO 695', 'SPO 720', 'SPO Sport', 'UMW 028', 'UMW 200', 'UMW 350',
           'UMW Umweltschutz und Gesundheitsingenieurwesen', 'VER 020', 'VER 500', 'VER 505',
           'VER 600', 'VER 700', 'VER 800', 'WEH Wehrtechnik', 'WER 000', 'WER 440', 'WER 490',
           'WER 740', 'WER Werkstoffwissenschaften', 'WIR 006', 'WIR 523', 'WIR 527', 'WIR 780',
           'WIS 600', 'WIS Wissenschaftskunde']
hdr = {'User-Agent' : 'Magic Browser'}

alreadyharvested = []
if skipalreadyharvested:
    alreadyharvested = ejlmod3.getalreadyharvested(jnlfilename)

prerecs = []
links = []
reboring = re.compile('(Bachelorarbeit|Masterarbeit)')
#for (fc, dep, pages) in [('', '603847', 1), ('m', '603845', 1), ('c', '603842', 1), ('o', '603787', 10)]:
for (fc, dep, pages) in [('c', '1691132', 1), ('m', '1691133', 1), ('', '1688930', 5)]:
#                         ('m', '603815', 2), ('', '603821', '')]:
    tocurl = 'https://mediatum.ub.tum.de/' + dep + '?id=' + dep + '&sortfield0=-year&sortfield1=&nodes_per_page=' + str(rpp)
    for page in range(pages):
        ejlmod3.printprogress('=', [[dep], [page+1, pages], [tocurl]])
        req = urllib.request.Request(tocurl, headers=hdr)
        tocpage = BeautifulSoup(urllib.request.urlopen(req), features="lxml")
        for div in tocpage.body.find_all('div', attrs = {'class' : 'preview_list'}):
            for a in div.find_all('a'):
                keepit = True
                rec = {'tc' : 'T', 'keyw' : [], 'jnl' : 'BOOK', 'note' : [], 'supervisor' : []}
                rec['link'] = re.sub('.*=', 'https://mediatum.ub.tum.de/?id=', a['href'])
            for child in div.find_all('div', attrs = {'class' : 'mediatum-stylelist-div'}):
                if not reboring.search(child.text) and ejlmod3.checkinterestingDOI(rec['link']):
                    if not rec['link'] in links:
                        for br in child.find_all('br'):
                            br.replace_with('XXXXXX')
                        for part in re.split(' *XXXXXX *', child.text):
                            if re.search('^[12]\d\d\d$', part):
                                rec['year'] = part
                                if int(part) <= ejlmod3.year(backwards=years):
                                    keepit = False
                        if keepit:
                            prerecs.append(rec)
                            links.append(rec['link'])
        print('  %4i records so far' % (len(prerecs)))
        time.sleep(5)
        nextpage = tocpage.body.find_all('a', attrs = {'class' : 'page-nav-next'})
        if nextpage:
            tocurl = 'https://mediatum.ub.tum.de' + nextpage[0]['href']
        else:
            break

recs = []
for (i, rec) in enumerate(prerecs):
    i += 1
    keepit = True
    ejlmod3.printprogress('-', [[i, len(prerecs)], [rec['link']], [len(recs)]])
    req = urllib.request.Request(rec['link'] + '&change_language=de', headers=hdr)
    artpage = BeautifulSoup(urllib.request.urlopen(req), features="lxml")
    ejlmod3.metatagcheck(rec, artpage, ['citation_author', 'citation_publication_date',
                                        'citation_pdf_url'])
    #Metadata
    for dl in artpage.body.find_all('dl', attrs = {'class' : 'object_meta'}):
        for child in dl.children:
            try:
                childclass = child['class'][0]
            except:
                continue
            if childclass == 'mask_label':
                label = child.text.strip()
            elif childclass == 'mask_value':
                #Document type
                if label == 'Dokumenttyp:':
                    rec['documenttype'] = child.text.strip()
                    rec['note'].append(rec['documenttype'])
                #Author
                elif label == 'Autor:':
                    rec['autaff'] = [[ child.text.strip() ]]
                    for a in child.find_all('a'):
                        if a.has_attr('href') and a['href'][:6] == 'mailto':
                            rec['autaff'][-1].append(re.sub('mailto:', 'EMAIL:', a['href']))
                #title:
                elif label == 'Originaltitel:':
                    rec['tit'] = child.text.strip()
                elif label == 'Übersetzter Titel:':
                    rec['transtit'] = child.text.strip()
                #Abstract
                elif label == 'Kurzfassung:':
                    rec['origabs'] = child.text.strip()
                elif label == 'Übersetzte Kurzfassung:':
                    rec['transabs'] = child.text.strip()
                #classification
                elif label in ['Fachgebiet:', 'TU-Systematik:']:
                    for subject in re.split(' *; *', child.text.strip()):
                        if subject in boring:
                            keepit = False
                            #print('   skip "%s"' % (subject))
                        elif subject in ['DAT Datenverarbeitung, Informatik']:
                            rec['fc'] = 'c'
                        elif subject in ['MAT Mathematik']:
                            rec['fc'] = 'm'
                        elif subject in ['PHY 600']:
                            rec['fc'] = 'f'
                        elif subject in ['PHY 900']:
                            rec['fc'] = 'a'
                        elif subject in ['PHY 410']:
                            rec['fc'] = 'p'
                        elif subject in ['PHY 023', 'PHY 025', 'PHY 035', 'PHY 037']:
                            rec['fc'] = 't'
                        elif subject in ['PHY 040', 'PHY 041', 'PHY 042', 'PHY 043']:
                            rec['fc'] = 'g'
                        elif subject in ['PHY 402', 'PHY 403', 'PHY 404']:
                            rec['fc'] = 'b'
                        else:
                            rec['note'].append(subject)
                #DDC
                elif label == 'DDC:':
                    rec['ddc'] = []
                    for part in re.split('[,;]',  child.text.strip()):
                        ddc = re.sub('\D', '', part)
                        if len(ddc) == 3:
                            rec['ddc'].append(ddc)
                            if ddc[:2] == '00':
                                rec['fc'] = 'c'
                            elif ddc[:2] == '51':
                                rec['fc'] = 'm'
                            elif ddc[:2] == '52':
                                rec['fc'] = 'a'
                            elif not rec['ddc'][:2] in ['50', '53', '60', '62']:
                                keepit = False
                                #print('   skip "%s"' % (div2.text.strip()))
                #Supervisor
                elif label == 'Betreuer:':
                    rec['supervisor'].append([re.sub(' \(.*', '', child.text.strip())])
                #Faculty
                elif label in ['Institution:', 'Fakultät:']:
                    department = child.text.strip()
                    if department in boring:
                        keepit = False
                        #print('   skip "%s"' % (department))
                    elif department == 'Fakultät für Informatik':
                        rec['fc'] = 'm'
                    elif department == 'Fakultät für Informatik':
                        rec['fc'] = 'c'
                    else:
                        rec['note'].append(department)
                #Pages
                elif label == 'Seiten:':
                    if re.search('\d\d', child.text):
                        rec['pages'] = re.sub('.*?(\d\d+).*', r'\1', child.text.strip())
                #URN
                elif label in ['URN:', 'Urn (Zitierfähige URL):']:
                    rec['urn'] = re.sub('.*\?', '', child.text.strip())
                #Keywords
                elif label in ['Stichworte:', 'Übersetzte Stichworte:']:
                    rec['keyw'] += re.split(', ', child.text.strip())
                #language
                elif label == 'Sprache:':
                    rec['language'] = child.text.strip()
                #ISBN
                elif label == 'ISBN:':
                    rec['isbn'] = child.text.strip()
                #Hinweis
                elif label == 'Hinweis:':
                    rec['note'].append(child.text.strip())
                #
                #elif label == '':
                #    rec[''] = child.text.strip()
                elif not label in ['Jahr:', 'Gutachter:', 'Dateigröße:', 'WWW:', 'Eingereicht am:',
                                   'Mündliche Prüfung:', 'Letzte Änderung:',
                                   'Originaluntertitel:', 'Übersetzter Untertitel:']:
                    print('\n  - ', label, ':', child.text.strip() + '\n')
    #abstract
    if 'language' in rec and rec['language'] == 'de' and 'transabs' in rec:
        rec['abs'] = rec['transabs']
    elif 'origabs' in rec:
        rec['abs'] = rec['origabs']
    #title
    if 'language' in rec and rec['language'] == 'en' and 'transtit' in rec:
        del(rec['transtit'])
    #Habilitation
    if 'documenttype' in rec and rec['documenttype'] == 'Habilitation':
        year = re.sub('.*([12]\d\d\d).*', r'\1', rec['date'])
        rec['MARC'] = [('502', [('b', 'habilitation'), ('c', publisher), ('d', year)])]
    #PDF
    for div in artpage.body.find_all('div', attrs = {'class' : 'document_download'}):
        for a in div.find_all('a'):
            rec['hidden'] = 'https://mediatum.ub.tum.de' + a['href']
    if keepit:
        if 'urn' in rec and rec['urn'] in alreadyharvested:
            print('  already in backup')
        else:
            rec['autaff'][-1].append(publisher)
            ejlmod3.printrecsummary(rec)
            recs.append(rec)
    else:
        ejlmod3.adduninterestingDOI(rec['link'])
    time.sleep(5)

ejlmod3.writenewXML(recs, publisher, jnlfilename)


import requests
import vktime
import init
import json

class vk:
	def __init__(self):
		self.time = vktime.vktime()

		self.access_token = ''
		self.expires = 0

		self.response = {}
		self.status = 0

	def checkToken(self):
		timeEmpty, curTime = self.time.timeIsEmpty(self.expires)
		if timeEmpty == True:
			while True:
				error, req = self.getAccessToken()
				if error == False:
					break
			self.access_token = req.get('access_token', '')
			self.expires = req.get('expires_in', 0)
			self.time.setSavedTime(curTime)

	def send(self, url):
		r = requests.get(url=url)
		self.status = r.status_code
		try:
			self.response = r.text
		except:
			print('send %s wrong')
			self.response = {}

	def getResponse(self):
		return self.response

	def getStatus(self):
		return self.status

	def getAccessToken(self):
		authUrl = 'https://oauth.vk.com/authorize?client_id=%s&display=page&redirect_uri=%s&scope=friends&response_type=code&v=%s' % (init.d['client_id'], init.d['redirect_uri'], init.d['v'])
		print('*********************************')
		print(authUrl)
		print('write CODE')
		code = input()
		tokenUrl = 'https://oauth.vk.com/access_token?client_id=%s&client_secret=%s&redirect_uri=%s&code=%s' % (init.d['client_id'], init.d['client_secret'], init.d['redirect_uri'], code)
		r = requests.get(url=tokenUrl)
		try:
			res = json.loads(r.text)
		except:
			print('ERROR request get token...')
			return True, {}

		if 'error' in res:
			print('ERROR... - %s, %s' % (res['error'], res['error_description']))
			return True, {}

		return False, res


#!/usr/bin/env python3

import argparse

def main():
    parser = argparse.ArgumentParser(description='Small script to add an origin to out csv')
    parser.add_argument('--in_file', type=str, help='The input path of csv file to clean')
    parser.add_argument('--out_file', type=str, help='The output path of the script')
    parser.add_argument('--origin', type=str, help='Original repo where the file came from')
    args = parser.parse_args()
    if args.in_file == None:
        print('Please provide a input file path')
    if args.out_file == None:
        print('Please provide a output file path')
    if args.origin == None:
        print('Please provide an origin')
    add_origin(args.in_file, args.out_file, args.origin)

def add_origin(input_file, output_file, origin):
    with open(input_file, 'r') as input:
        with open(output_file, 'w') as output:
            output.writelines(input.readline()[:-1] + ',Origin\n')
            for line in input:
                output.writelines(line[:-1] + ',' + origin + '\n')

if __name__ == '__main__':
    main()


import os
import pickle
from pathlib import Path
import numpy as np

import click
import pandas as pd
from constants import TARGET_COLUMN

from feature_transform import ExperimentalTransformer

def load_object(filepath: str):
    with open(filepath, "rb") as f:
        obj = pickle.load(f)
    return obj

@click.command("make_prediction")
@click.option("--input-datafile")
@click.option("--path-to-model")
@click.option("--output-dir")
def make_prediction(input_datafile: str, path_to_model: str, output_dir: str):
    model = load_object(path_to_model)
    data = pd.read_csv(input_datafile)

    X = data.drop(columns=[TARGET_COLUMN], axis=1)

    model_dir = Path(path_to_model).parent
    transformer = ExperimentalTransformer(model_dir)
    transformer.load_from_file()

    X_transformed = transformer.transform(X)

    y_predict = model.predict(X_transformed)

    os.makedirs(output_dir, exist_ok=True)
    np.savetxt(os.path.join(output_dir, "predictions.csv"), y_predict, delimiter=',')

if __name__ == "__main__":
    make_prediction()


import os
import urllib

import requests

BASE_OBJECT_STORAGE_URL = "https://phish.nyc3.digitaloceanspaces.com/"

class LogoDetection:
    BASE_URL = "https://api.logograb.com/detect"

    def _construct_image_url(self, image_filename):
        return "{}{}".format(BASE_OBJECT_STORAGE_URL, image_filename)

    def get_brand_from_url(self, image_filename):
        payload = "mediaUrl={}".format(
            urllib.parse.quote_plus(self._construct_image_url(image_filename))
        )
        headers = {
            'Content-Type': "application/x-www-form-urlencoded",
            'X-DEVELOPER-KEY': os.environ.get("LOGOGRAB_KEY")
        }

        response = requests.request(
            "POST", self.BASE_URL, data=payload, headers=headers
        )
        return set(
            map(lambda x: x['name'], response.json()['data']['detections'])
        )

    def get_brand_from_screenshot(self, image_file):
        payload = "mediaFile={}".format(
            urllib.parse.quote_plus(image_file)
        )

        headers = {
            'Content-Type': "application/x-www-form-urlencoded",
            'X-DEVELOPER-KEY': os.environ.get("LOGOGRAB_KEY")
        }

        response = requests.request(
            "POST", self.BASE_URL, data=payload, headers=headers
        )
        return set(
            map(lambda x: x['name'], response.json()['data']['detections'])
        )

""" Tests

image_file = "hibshman.net.jpeg"
brands = LogoDetection().get_brand_from_url(
    image_file=image_file
)

print(
    "Set (as in set theory) of brands detected from {}: {}".format(
        image_file, brands
    )
)

image_file = "nyuclubs.atspace.cc.jpeg"
brands = LogoDetection().get_brand_from_url(
    image_file=image_file
)

print(
    "Set (as in set theory) of brands detected from {}: {}".format(
        image_file, brands
    )
)
"""


# import pygame module
import pygame

# Start pygame
pygame.init()

width = 800
height = 600

screen = pygame.display.set_mode((width, height))

cat = pygame.image.load('cat.png')
cat = pygame.transform.scale(cat, (120, 140))

# tracks whether the game is currently running
active = True

# This loop will run as long as active is True
while active:
    # get mouse position
    mouse_loc = pygame.mouse.get_pos()

    # Loop through the event queue. If we reach a QUIT event, set active to False
    # Once active is no longer True, the loop will no longer run
    for event in pygame.event.get():
        # Add a line of code here to print the event
        # print(event)

        if event.type == pygame.MOUSEBUTTONDOWN:
            print("clicked!")
            if event.button == 1:
                print("and it's the left button")

        if event.type == pygame.QUIT:
            active = False

    # display mouse location in terminal
    print(mouse_loc)
    # display cat
    screen.blit(cat, mouse_loc)

    # update the display
    pygame.display.flip()


import dearpygui.dearpygui as dpg
import sys
import glob
import docklaunch as dckl
import psutil
import re
import os
from subprocess import Popen, PIPE

dpg.create_context()
dpg.create_viewport(title="Conan", width=800, height=500)

dpg.show_metrics()

def boutonsave(sender, data):
    # dpg.get_value(Sender)
    print(dpg.get_value("nptsx"))
    print(dpg.get_value("Softdock"))

def delete_second_window(sender):
    dpg.delete_item(sender)

def save_file():
    path = dpg.get_value("pathconfig")
    f = open(path, "w+")
    software = dpg.get_value("Softdock")
    if software == "Autodock-gpu":
        soft2 = "gpu"
    if software == "Autodock-vina":
        soft2 = "vina"
    if software == "Gnina":
        soft2 = "gnina"
    if software == "Smina":
        soft2 = "smina"
    if software == "Qvina":
        soft2 = "qvina"
    if software == "Autodock4":
        soft2 = "AD4"
    f.write("#SOFTWARE# " + soft2 + " \n")
    # f.write("#DEBUG# "+dpg.get_value("debug")+" \n")

    debug = dpg.get_value("debug")
    if debug == "True":
        f.write("#DEBUG# " + "yes" + " \n")
    else:
        f.write("#DEBUG# " + "no" + " \n")

    f.write("#THREADS# " + dpg.get_value("threads") + " \n")
    f.write("#NRUNS# " + dpg.get_value("nruns") + " \n")
    f.write("#PATHDB# " + dpg.get_value("db") + " \n")
    f.write("#SPACING# " + dpg.get_value("spacing") + " \n")
    f.write("#CENTERX# " + dpg.get_value("gridcenterx") + " \n")
    f.write("#CENTERY# " + dpg.get_value("gridcentery") + " \n")
    f.write("#CENTERZ# " + dpg.get_value("gridcenterz") + " \n")
    f.write("#SIZEX# " + dpg.get_value("nptsx") + " \n")
    f.write("#SIZEY# " + dpg.get_value("nptsy") + " \n")
    f.write("#SIZEZ# " + dpg.get_value("nptsz") + " \n")
    f.close()
    return

def load_file(sender, app_data, user_data):
    print(f"user_data = {user_data}")
    with open(user_data) as f:
        lines = f.readlines()
    for x in range(len(lines)):
        if re.findall(r"\S+", lines[x])[0] == "#SOFTWARE#":
            # /home/louis/Téléchargements/Conansuite/Executions/test.txt
            if re.findall(r"\S+", lines[x])[1].strip() == "vina":
                print("test")
                dpg.set_value("Softdock", softwares[1])
                print("found vina")
            if re.findall(r"\S+", lines[x])[1].strip() == "gpu":
                dpg.set_value("Softdock", softwares[0])
            if re.findall(r"\S+", lines[x])[1].strip() == "gnina":
                dpg.set_value("Softdock", softwares[2])
            if re.findall(r"\S+", lines[x])[1].strip() == "smina":
                dpg.set_value("Softdock", softwares[3])
            if re.findall(r"\S+", lines[x])[1].strip() == "qvina":
                dpg.set_value("Softdock", softwares[4])
            if re.findall(r"\S+", lines[x])[1].strip() == "autodock4":
                dpg.set_value("Softdock", softwares[4])

        if re.findall(r"\S+", lines[x])[0] == "#DEBUG#":
            if re.findall(r"\S+", lines[x])[1].strip() == "yes":
                dpg.set_value("debug", "True")
            if re.findall(r"\S+", lines[x])[1].strip() == "no":
                dpg.set_value("debug", "False")

        if re.findall(r"\S+", lines[x])[0] == "#THREADS#":
            dpg.set_value("threads", re.findall(r"\S+", lines[x])[1].strip())

        if re.findall(r"\S+", lines[x])[0] == "#NRUNS#":
            dpg.set_value("nruns", re.findall(r"\S+", lines[x])[1].strip())

        if re.findall(r"\S+", lines[x])[0] == "#PATHDB#":
            dpg.set_value("db", re.findall(r"\S+", lines[x])[1].strip())

        if re.findall(r"\S+", lines[x])[0] == "#SPACING#":
            dpg.set_value("spacing", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#CENTERX#":
            dpg.set_value("gridcenterx", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#CENTERY#":
            dpg.set_value("gridcentery", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#CENTERZ#":
            dpg.set_value("gridcenterz", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#SIZEX#":
            dpg.set_value("nptsx", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#SIZEY#":
            dpg.set_value("nptsy", re.findall(r"\S+", lines[x])[1].strip())
        if re.findall(r"\S+", lines[x])[0] == "#SIZEZ#":
            dpg.set_value("nptsz", re.findall(r"\S+", lines[x])[1].strip())

    print(dpg.get_value("Softdock"))
3

def get_info_db(sender, app_data, user_data):
    import rdkit
    from rdkit.Chem import rdMolDescriptors
    from rdkit import Chem
    from rdkit.Chem import GraphDescriptors
    from rdkit.Chem import Descriptors
    import numpy as np

    descriptor_names = list(rdMolDescriptors.Properties.GetAvailableProperties())
    descriptor_names.remove("NumAtomStereoCenters")
    descriptor_names.remove("NumUnspecifiedAtomStereoCenters")
    print(descriptor_names)
    listofpdbqt = glob.glob(f"{user_data}/**/*.pdbqt", recursive=True)
    print(listofpdbqt)

    def compute_descript(blockdata, descriptors_to_compute):
        # transformation du bloc texte en molecule rdkit
        m = Chem.rdmolfiles.MolFromPDBBlock(blockdata)
        # get_descriptors = rdMolDescriptors.Properties(descriptors_to_compute)
        descriptors = []

        if m:
            descriptors = np.array(descriptors_to_compute.ComputeProperties(m))

        return descriptors

    with dpg.window(
        label="Compute",
        width=400,
        height=400,
        pos=(100, 100),
        tag="computeinformationdb",
        on_close=delete_second_window,
    ):
        descriptor_names = list(dict.fromkeys(descriptor_names))
        print(descriptor_names)

        # Add a callback function to handle check/uncheck all checkboxes
        def check_uncheck_all(sender, app_data, user_data):
            check_all = dpg.get_value("check_all")
            for descriptor in descriptor_names:
                dpg.set_value(descriptor, check_all)

        def check_checked():
            good = []
            print("GOOD")
            for descriptor in descriptor_names:
                if dpg.get_value(descriptor):
                    good.append(descriptor)
            print(good)
            return good

        # Do the calculations on the selected descriptors
        def iteration_pdbqt(sender, data, user_data):
            descriptor_names_entry = check_checked()
            print(f"desc names: {descriptor_names_entry}")
            get_descriptors = rdMolDescriptors.Properties(descriptor_names_entry)

            import pandas as pd

            pandas_df = pd.DataFrame(columns=descriptor_names_entry)
            import time

            print(len(listofpdbqt))
            # time.sleep(10)
            for file in listofpdbqt:
                with open(file, "r+") as pdbqt:  # read a list of lines into data
                    pdbqtcontenu = pdbqt.readlines()
                    # print(pdbqtcontenu)
                blockdata = ""

                # Tiny pdbqt parser
                for x in range(len(pdbqtcontenu)):
                    if pdbqtcontenu[x][:4] == "ATOM" or pdbqtcontenu[x][:6] == "HETATM":
                        templine = pdbqtcontenu[x].split()
                        pdbqtcontenu[x] = pdbqtcontenu[x][:-3]
                        pdbqtcontenu[x] = pdbqtcontenu[x] + templine[2][:1] + "\n"
                        blockdata = blockdata + pdbqtcontenu[x]
                        # print(x)
                    if pdbqtcontenu[x][:6] == "ENDMDL":
                        print("break")
                        break

                print(compute_descript(blockdata, get_descriptors))
                # Compute the descriptors for the current blockdata
                descriptors = compute_descript(blockdata, get_descriptors)

                # Append the computed descriptors to the pandas_df dataframe
                if len(descriptors) > 0:
                    pandas_df = pandas_df.append(
                        pd.Series(descriptors, index=descriptor_names_entry),
                        ignore_index=True,
                    )

            print(pandas_df)
            """
            data = pandas_df['exactmw']
            import matplotlib.pyplot as plt
# Plotting the histogram
            plt.hist(data, bins=10)  # Adjust the number of bins as needed

            # Adding labels and title
            plt.xlabel('Values')
            plt.ylabel('Frequency')
            plt.title('Histogram of First Column')

            # Displaying the histogram
            plt.show()

            """
            with dpg.window(
                label="Descriptors Histogram",
                width=700,
                height=400,
                pos=(100, 100),
                tag="descriptors_histogram",
                on_close=delete_second_window,
            ):
                dpg.add_text("Select the number of bins:")
                dpg.add_input_int(
                    label="Number of bins", tag="num_bins", default_value=15
                )

                # def plot_histograms(sender, app_data, user_data):
                num_bins = dpg.get_value("num_bins")
                datap = pandas_df["exactmw"].values.tolist()
                amin, amax = min(datap), max(datap)
                newdata = []
                for i, val in enumerate(datap):
                    newdata.append((val - amin) / (amax - amin))
                print(newdata)

                # Create a histogram plot
                with dpg.plot(label="Histogram"):
                    dpg.add_plot_axis(1, label="y", tag="y_axis")
                    dpg.add_histogram_series(newdata, bins=10, parent="y_axis")  # A
                """
                with dpg.plot(label="Histograms", width=700, height=400,parent="descriptors_histogram"):

                    dpg.add_plot_axis(dpg.mvXAxis, label="x")
                    dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis")
                    #for descriptor in descriptor_names_entry:
                    print([int(item) for item in list(pandas_df["exactmw"])])
                    hist_series = dpg.add_histogram_series([int(item) for item in list(pandas_df["exactmw"])], bins=int(num_bins), label="exactmw", parent="y_axis")

                    dpg.add_plot_legend(location="upper right")

                    dpg.set_item_color(hist_series, [255, 0, 0, 255])

                    #dpg.add_histogram_series(list(pandas_df['exactmw']), bins=num_bins, label='descriptor', parent="y_axis")
                    #dpg.add_plot_legend(location="upper right")
                    #dpg.add_plot_axis(dpg.mvXAxis, label="x")
                    #dpg.add_plot_axis(dpg.mvYAxis, label="y", tag="y_axis")
                    """

                # series belong to a y axis

                # dpg.add_button(label="Plot Histograms", tag="plot_histograms", callback=plot_histograms)

        dpg.add_text(
            "Which descriptor do you want to compute on the database ? \n Rdkit calculations are used for this."
        )
        with dpg.group(horizontal=True):
            dpg.add_checkbox(
                label="Check/Uncheck All", callback=check_uncheck_all, tag="check_all"
            )

            dpg.add_button(label="Run", tag="loadconf", callback=iteration_pdbqt)

        for descriptor in descriptor_names:
            checkbox = dpg.add_checkbox(label=descriptor, tag=descriptor)

        # Add the check/uncheck all checkbox

        # dpg.add_button(label=descriptor, callback=lambda: dpg.set_value(descriptor, not dpg.get_value(descriptor)), user_data=descriptor, tag=descriptor)

def detect_config_files(path):
    print(path)
    filestoscan = glob.glob(f"{path}/*.txt")
    print(filestoscan)
    config_files = []
    import datetime

    def convert_date(file_date):
        return datetime.datetime.fromtimestamp(file_date).strftime("%Y/%m/%d %H:%M:%S")

    for file in filestoscan:
        with open(file) as f:
            first_line = f.readline()
            if "#SOFTWARE#" in first_line:
                file_name = os.path.basename(file)
                file_path = os.path.abspath(file)
                file_date = os.path.getctime(file)
                file_date = convert_date(file_date)

                config_files.append(
                    (file_name, file_path, first_line.split()[1], file_date)
                )

    print(config_files)
    return config_files

def save_config(sender):
    with dpg.window(
        label="Save",
        width=200,
        height=200,
        pos=(100, 100),
        tag="saveconfig",
        on_close=delete_second_window,
    ):
        dpg.add_input_text(tag="pathconfig", width=150)
        dpg.add_button(label="Save File", tag="saveconf", callback=save_file)
        # dpg.add_text(f"CPU usage:{psutil.cpu_percent()}%, Memory usage: {psutil.virtual_memory().percent}%", tag="usage")

def load_config(sender):
    with dpg.window(
        label="Load",
        width=700,
        height=400,
        pos=(100, 100),
        tag="loadconfig",
        on_close=delete_second_window,
    ):
        dpg.add_input_text(tag="pathconfig2", width=150)
        dpg.add_button(
            label="Load File",
            tag="loadconf",
            callback=load_file,
            user_data=lambda: str(dpg.get_value("pathconfig2")),
        )
        dpg.add_text("Theses are detected files in the current directory:")
        config_files = detect_config_files(os.getcwd())

        print(config_files)
        headers_conf = ["Filename", "Path", "Software", "Date of Creation", "Open"]
        with dpg.table(
            header_row=True,
            resizable=True,
            borders_outerH=True,
            borders_innerV=True,
            borders_innerH=True,
            borders_outerV=True,
        ):
            print(len(config_files))

            for x in range(len(headers_conf)):
                print(headers_conf[x])
                dpg.add_table_column(label=headers_conf[x])

            for i in range(len(config_files)):
                with dpg.table_row():
                    for j in range(len(config_files[i])):
                        dpg.add_text(config_files[i][j])
                    dpg.add_button(
                        label="Load this file",
                        callback=load_file,
                        user_data=config_files[i][1],
                    )

# dckl.dockingtot("VINA","90","75","85","55","-21","-20","0.375","1","100","/home/louis/Téléchargements/dbtest/",True)
def run(sender, data):
    software = dpg.get_value("Softdock")
    if software == "Autodock-gpu":
        soft = "GPU"
    if software == "Autodock-vina":
        soft = "VINA"
    if software == "Gnina":
        soft = "GNINA"
    if software == "Smina":
        soft = "SMINA"
    if software == "Qvina":
        soft = "QVINA"
    if software == "Autodock4":
        soft = "AD4"

    nptsx = dpg.get_value("nptsx")
    nptsy = dpg.get_value("nptsy")
    nptsz = dpg.get_value("nptsz")
    gridcenterx = dpg.get_value("gridcenterx")
    gridcentery = dpg.get_value("gridcentery")
    gridcenterz = dpg.get_value("gridcenterz")
    spacing = dpg.get_value("spacing")
    threads = dpg.get_value("threads")
    nruns = dpg.get_value("nruns")
    pathdb2 = dpg.get_value("db")
    if dpg.get_value("debug") == "True":
        debug = True
    else:
        debug = False
    import asyncio

    async def count_files():
        while True:
            listoffiles = glob.glob("results_VINA/*.pdbqt")
            print(listoffiles)
            dpg.set_value("file_count", f"Number of files found: {len(listoffiles)}")
            await asyncio.sleep(1)

    async def start_count_files():
        asyncio.create_task(count_files())

    with dpg.window(label="Parameters", width=500, height=500):
        dpg.add_text(tag="file_count")
        asyncio.run(start_count_files())

    dckl.dockingtot(
        soft,
        nptsx,
        nptsy,
        nptsz,
        gridcenterx,
        gridcentery,
        gridcenterz,
        spacing,
        threads,
        nruns,
        pathdb2,
        debug,
    )

def windowsettingsopen(path):
    with open(path, "r+") as configfile:
        # read a list of lines into data
        config_data = configfile.readlines()

    with dpg.window(label="Parameters", width=800, height=500):
        block_config = ''.join(config_data)
        dpg.add_input_text(tag="", label="test2", multiline=True, height=500, width=800, default_value=block_config)

def settingseditor(sender, data):
    soft = dpg.get_value("Softfock")
    if soft == "Autodock-vina" or "Smina" or "Qvina":
        windowsettingsopen("/home/louis/Conan/newparametres/DOCKING.dpf")

def view_grid(sender, data):
    nptsx = dpg.get_value("nptsx")
    nptsy = dpg.get_value("nptsy")
    nptsz = dpg.get_value("nptsz")
    gridcenterx = dpg.get_value("gridcenterx")
    gridcentery = dpg.get_value("gridcentery")
    gridcenterz = dpg.get_value("gridcenterz")
    spacing = dpg.get_value("spacing")
    nptsx = float(nptsx) * float(spacing)
    nptsy = float(nptsy) * float(spacing)
    nptsz = float(nptsz) * float(spacing)
    pathrecs = glob.glob("../receptors/*.pdbqt")
    txtrec = ""
    pathfilerec = "../parametres/temp_files/pathreceptors.txt"
    if os.path.isfile(pathfilerec):
        os.remove(pathfilerec)

    for x in pathrecs:
        txtrec = txtrec + x + "\n"
    file = open(pathfilerec, "w")  # append mode
    file.write(txtrec)
    file.close()
    print(
        f"python ../parametres/viewreceptor.py {nptsx} {nptsy} {nptsz} {gridcenterx} {gridcentery} {gridcenterz}"
    )
    process = Popen(
        [
            "python",
            "../parametres/viewreceptor.py",
            str(nptsx),
            str(nptsy),
            str(nptsz),
            str(gridcenterx),
            str(gridcentery),
            str(gridcenterz),
        ],
        stdout=PIPE,
        stderr=PIPE,
    )
    # viewer.viewreceptor(pathrecs,nptsx,nptsy,nptsz,gridcenterx,gridcentery,gridcenterz)

def print_me(sender, data):
    print(f"Menu Item: {sender}{data}")

def setdb(sender, data):
    pathdb = dpg.get_value("db")
    listofpdbqt = glob.glob(f"{pathdb}/**/*.pdbqt", recursive=True)
    print(len(listofpdbqt))
    if len(listofpdbqt) == 0:
        dpg.set_value("textdb", "No pdbqt detected on this path")
    else:
        dpg.set_value("textdb", f"Found {len(listofpdbqt)} ligands on this path")

def close(sender, data):
    sys.exit()

def validation(sender, app_data):
    print("App Data: ", app_data)
    print(app_data["file_path_name"])
    dpg.set_value("db", app_data["file_path_name"])

def cancel_callback(sender, app_data):
    print("Cancel was clicked.")
    print("Sender: ", sender)
    print("App Data: ", app_data)

"""
def on_render(sender, data):
    #dpgc.get_total_time()
    dpg.set_value("usage",f"CPU usage: {psutil.cpu_percent()}%, Memory usage: {psutil.virtual_memory().percent}%", tag="usage")
    print("test")
"""

def update():
    dpg.set_value(
        "usage",
        f"CPU usage:{psutil.cpu_percent()}%, Memory usage: {psutil.virtual_memory().percent}%",
    )

softwares = ["Autodock-gpu", "Autodock-vina", "Autodock4", "Gnina", "Smina", "Qvina"]
with dpg.window(label="Parameters", width=500, height=500):
    # dpg.set_main_window_size(500,500)
    dpg.add_text("Conan")
    dpg.add_button(label="Load Configuration", callback=load_config)
    dpg.add_listbox(tag="Softdock", items=softwares)
    dpg.add_text("Number of points in the grid")
    dpg.add_text("  X  --- Y ---  Z")
    with dpg.group(horizontal=True):
        dpg.add_input_text(tag="nptsx", width=40)
        # dpg.add_same_line()
        dpg.add_input_text(tag="nptsy", width=40)
        # dpg.add_same_line()
        dpg.add_input_text(tag="nptsz", width=40)

    dpg.add_text("Center of the grid")
    dpg.add_text("  X  --- Y ---  Z")
    with dpg.group(horizontal=True):
        dpg.add_input_text(tag="gridcenterx", width=40)
        # dpg.add_same_line()
        dpg.add_input_text(tag="gridcentery", width=40)
        # dpg.add_same_line()
        dpg.add_input_text(tag="gridcenterz", width=40)
    # dpg.add_text("Spacing (Angstrom)")
    dpg.add_input_text(tag="spacing", width=40, label="Spacing (Angstrom)")
    dpg.add_button(label="View grid in Pymol", callback=view_grid)
    # dpg.add_text("Number of Runs             Number of Threads")

    dpg.add_input_text(tag="nruns", width=40, label="Number of Genetic Runs")
    dpg.add_input_text(tag="threads", width=40, label="Number of Threads")
    dpg.add_button(label="Edit all settings", callback=settingseditor)
    dpg.add_text("Path of Database:")
    with dpg.group(horizontal=True):
        dpg.add_input_text(tag="db", width=150)
        dpg.add_file_dialog(
            directory_selector=True,
            show=False,
            callback=validation,
            tag="file_dialog_id",
            cancel_callback=cancel_callback,
        )
        dpg.add_button(
            label="Directory Selector", callback=lambda: dpg.show_item("file_dialog_id")
        )
        dpg.add_button(
            label="Get informations",
            callback=get_info_db,
            user_data="/home/louis/Téléchargements/PROJETISDD/ligands",
        )
    dpg.add_button(label="Set", callback=setdb)
    dpg.add_text("No database selected yet", tag="textdb")
    dpg.add_text("Enable Debugging:")
    dpg.add_radio_button(tag="debug", items=["True", "False"], horizontal=True)
    with dpg.group(horizontal=True):
        dpg.add_button(label="Save", callback=boutonsave)
        dpg.add_button(label="Run", callback=run)
    dpg.add_button(label="Close", callback=close)
    dpg.add_button(label="Save Configuration", callback=save_config)
    # dpg.add_text(f"CPU usage:{psutil.cpu_percent()}%, Memory usage: {psutil.virtual_memory().percent}%", tag="usage")

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()


import os
import sys

import discord
from discord import Activity, ActivityType
from discord.ext.commands import Bot
from dotenv import load_dotenv

load_dotenv()

if not os.path.isfile("config.json"):
    sys.exit("'config.json' not found! Please add it and try again.")
else:
    pass

client = Bot(command_prefix=">",
             help_command=None,
             case_insensitive=True,
             max_messages=100,
             activity=Activity(type=ActivityType.watching,
                               name=f"over Cartel."),
             intents=discord.Intents.all())

@client.event
async def on_ready():
    print("Main system is operational.")

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')

TOKEN = os.getenv("DISCORD_TOKEN")
client.run(TOKEN)


import torch as t
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam

import numpy as np
import cv2
import os

batch_size = 16
img_size = 32
latent_dim = 64
channels = 1
lr = 0.00001 #0.0002
b1, b2 = 0.5, 0.999
n_epochs = 50
n_ch = 64  # Number of channels in initial convolution layers
sample_interval = 200  # Save a generated image every sample_interval number of batches

write_dir = '../images/gan_conv_test/'
data_dir = '../data/bub_single_24new/'
os.makedirs(write_dir, exist_ok=True)

Zeros = t.zeros((batch_size, 1, img_size, img_size))
Ones = t.ones((batch_size, 1, img_size, img_size))

class Data(Dataset):
    def __init__(self):
        self.data = [cv2.imread(data_dir + im, cv2.IMREAD_GRAYSCALE)//255 for im in os.listdir(data_dir) if '-0.png' in im]
        self.data = t.Tensor([im for im in self.data if im.shape == (img_size, img_size)])

        self.len = self.data.shape[0]

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return self.data[index]

dataset = Data()

dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)

img_shape = (channels, img_size, img_size)

class Generator(nn.Module):
    # Takes as input a random tensor of size (batch_size, latent_dim) generated by noise()
    '''def __init__(self):
        super(Generator, self).__init__()

        self.lin = nn.Sequential(nn.Linear(latent_dim, n_ch * (img_size//2)**2))
        # Reshape to (N, n_ch, im_size//2, im_size//2)
        self.conv_block = nn.Sequential(
            nn.BatchNorm2d(n_ch),
            nn.Conv2d(n_ch, n_ch//2, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(n_ch//2),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Upsample(scale_factor=2),
            nn.Conv2d(n_ch//2, n_ch//4, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(n_ch//4),
            nn.LeakyReLU(0.2, inplace=True),
            #nn.Upsample(scale_factor=2),
            nn.Conv2d(n_ch//4, channels, kernel_size=3, stride=1, padding=1),
            nn.Sigmoid()
            )

    def forward(self, im):
        im = self.lin(im)
        im = im.view(im.shape[0], n_ch, img_size//2, img_size//2)
        return self.conv_block(im)

    # (N, latent_dim) ->[linear] (N, n * 16**2) ->[reshape] (N, n, 16, 16) ->[conv] (N, n/2, 16, 16) ->[upsampling] (N, n/2, 32, 32) ->[conv] (N, n/4, 32, 32) ->[conv] (N, 1, 32, 32)'''

    def __init__(self):
        super(Generator, self).__init__()

        self.init_size = img_size//4
        self.linear = nn.Sequential(nn.Linear(latent_dim, n_ch * self.init_size**2))  #Outsize=(8x8xn) with n=128

        self.conv_blocks = nn.Sequential(
            nn.BatchNorm2d(n_ch),
            nn.Upsample(scale_factor=2),
            nn.Conv2d(n_ch, n_ch, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(n_ch, 0.8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Upsample(scale_factor=2),
            nn.Conv2d(n_ch, n_ch//2, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(n_ch//2, 0.8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(n_ch//2, channels, kernel_size=3, stride=1, padding=1),
            nn.Sigmoid()
            )

    def forward(self, noise):
        generated_im = self.linear(noise)
        generated_im = generated_im.view(generated_im.shape[0], n_ch, self.init_size, self.init_size)
        generated_im = self.conv_blocks(generated_im)
        #generated_im = t.where(generated_im > 0.5, Ones, generated_im)
        return generated_im

    # (N, latent_dim) ->[linear] (N, n * 8**2) ->[reshape] (N, n, 8, 8) ->[upsampling] (N, n, 16, 16) ->[conv] (N, n, 16, 16) ->[upsampling] (N, n, 32, 32) ->[conv] (N, n/2, 32, 32) ->[conv] (N, 1, 32, 32)

class Discriminator(nn.Module):
    # Takes as input a data image or image generated by Generator
    def __init__(self):
        super(Discriminator, self).__init__()

        self.model = nn.Sequential(
            nn.Linear(int(np.prod(img_shape)), 8*latent_dim),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(8*latent_dim, 4*latent_dim),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Linear(4*latent_dim, 1),
            nn.Sigmoid(),
        )

    def forward(self, img):
        img_flat = img.view(img.size(0), -1)
        validity = self.model(img_flat)
        return validity

def generate_noise():
    return t.rand((batch_size, latent_dim))

# Loss function
adversarial_loss = nn.BCELoss()

G = Generator()
D = Discriminator()

# Optimizers
optimizer_G = Adam(G.parameters(), lr=lr, betas=(b1, b2))
optimizer_D = Adam(D.parameters(), lr=lr, betas=(b1, b2))

for epoch in range(n_epochs):
    for i, data_imgs in enumerate(dataloader):

        # Configure generator input
        data_shape = data_imgs.shape
        data_imgs = data_imgs.view(data_imgs.shape[0], channels, data_imgs.shape[1], data_imgs.shape[2])

        # Adversarial ground truths
        valid = t.ones((data_shape[0], 1), requires_grad=False)
        fake = t.zeros((data_shape[0], 1), requires_grad=False)

        # ----------------
        # Train Generator
        # ----------------

        optimizer_G.zero_grad()

        # Sample noise as generator input
        noise = generate_noise()

        # Generate a batch of images
        gen_imgs = G(noise)
        gen_imgs = gen_imgs[:data_shape[0]]

        # Loss measures generator's ability to fool the discriminator
        G_loss = adversarial_loss(D(gen_imgs), valid)

        G_loss.backward()
        optimizer_G.step()

        # --------------------
        # Train Discriminator
        # --------------------

        optimizer_D.zero_grad()

        # Measure discriminator's ability to classify real from generated samples
        real_loss = adversarial_loss(D(data_imgs), valid)
        fake_loss = adversarial_loss(D(gen_imgs.detach()), fake)
        D_loss = (real_loss + fake_loss) / 2

        D_loss.backward()
        optimizer_D.step()

        # ----------------
        # Log Progress
        # ----------------

        print(
            "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
            % (epoch, n_epochs, i, len(dataloader), D_loss.item(), G_loss.item())
        )

        batches_done = epoch*len(dataloader) + i
        if batches_done % sample_interval == 0:
            os.makedirs(write_dir + str(batches_done), exist_ok=True)
            for j, gen_im in enumerate(gen_imgs):
                cv2.imwrite('%s%d/%d.png' % (write_dir, batches_done, j), gen_im[0].detach().numpy()*255)