import numpy as np
from unsupervised_learning.MDS.multiple_dimensional_scaling import MultipleDimensionalScaling

class Isomap:
    """ Isomap，流形学习，非线性降维.
    Attributes:
        d: 降维后的维数/特征数
        k: 计算测地距离矩阵时的邻域样本个数
    """
    def __init__(self, d, k):
        self.d = d
        self.k = k

    def _floyd(self, euclidean_distance):
        """ 计算测地距离矩阵 """
        n_samples = euclidean_distance.shape[0]
        geodesic_distance = np.full_like(euclidean_distance, np.max(euclidean_distance) * 1e8)
        k_index = np.argsort(euclidean_distance, axis=1)
        # 构建迭代用的初始距离矩阵
        for i in range(n_samples):
            geodesic_distance[i, k_index[i, :self.k+1]] = euclidean_distance[i, k_index[i, :self.k+1]]
        # 使用Floyd算法进行迭代计算
        for step in range(n_samples):
            for i in range(n_samples):
                for j in range(n_samples):
                    if geodesic_distance[i, j] > geodesic_distance[i, step] + geodesic_distance[step, j]:
                        geodesic_distance[i, j] = geodesic_distance[i, step] + geodesic_distance[step, j]
        return geodesic_distance

    def dimension_reduction(self, X):
        """ 非线性降维 """
        n_samples = X.shape[0]
        # 以下是MDS算法（只不过用测地距离代替了欧氏距离）
        # -------------------------------------
        # 1) 计算距离的平方矩阵
        euclidean_distance = np.sqrt(MultipleDimensionalScaling.calculate_distance_matrix(X))
        distance_squared_matrix = np.power(self._floyd(euclidean_distance), 2)
        # 2) 计算降维后的内积矩阵
        dist_i_squared = np.expand_dims(distance_squared_matrix.sum(axis=1), axis=1) / n_samples
        dist_j_squared = distance_squared_matrix.sum(axis=0) / n_samples
        dist_squared = distance_squared_matrix.sum() / n_samples ** 2
        inner_product_matrix = -0.5 * (distance_squared_matrix - dist_i_squared -
                                       dist_j_squared + dist_squared)
        # 3) 对内积矩阵进行特征值分解以获得降维后的坐标
        eigenvalues, eigenvectors = np.linalg.eig(inner_product_matrix)
        picked_index = eigenvalues.argsort()[::-1][:self.d]
        picked_eigenvalues = np.sqrt(np.diag(eigenvalues[picked_index])).real
        picked_eigenvectors = eigenvectors.T[picked_index].real
        return np.dot(picked_eigenvectors.T, picked_eigenvalues)

if __name__ == '__main__':
    def main():
        # from sklearn.datasets import load_iris
        # import matplotlib.pyplot as plt
        # iris_data = load_iris()
        # X, Y = iris_data.data, iris_data.target

        from sklearn.datasets import make_s_curve, make_swiss_roll
        import matplotlib.pyplot as plt
        # X, Y = make_s_curve(n_samples=500, noise=0.15, random_state=40)
        X, Y = make_swiss_roll(n_samples=500, noise=0.15, random_state=100)

        isomap = Isomap(d=2, k=20)
        y = isomap.dimension_reduction(X)
        plt.scatter(y[:, 0], y[:, 1], c=Y)
        plt.show()
    main()


from __future__ import annotations

import asyncio
import atexit
import threading
from multiprocessing.util import _exit_function  # type: ignore[attr-defined]
from typing import TYPE_CHECKING, Any

import uvloop

from app.lib import settings

from .base import Queue, Worker

if TYPE_CHECKING:
    from collections.abc import Awaitable, Callable, Collection

    import saq

__all__ = ["create_worker_instance", "run_worker"]

def create_worker_instance(
    queue: Queue,
    tasks: Collection[Callable[..., Any] | tuple[str, Callable]],
    scheduled_tasks: Collection[saq.CronJob] | None = None,
    startup: Callable[[dict[str, Any]], Awaitable[Any]] | None = None,
    shutdown: Callable[[dict[str, Any]], Awaitable[Any]] | None = None,
    before_process: Callable[[dict[str, Any]], Awaitable[Any]] | None = None,
    after_process: Callable[[dict[str, Any]], Awaitable[Any]] | None = None,
    concurrency: int | None = None,
) -> Worker:
    """Create worker instance.

    Args:
        queue: Queue: The queue instance to use for the worker
        tasks: Collection[Callable[..., Any] | tuple[str, Callable]]: Functions to be called via the async workers
        scheduled_tasks (Collection[saq.CronJob] | None, optional): Scheduled functions to be called via the async workers. Defaults to None.
        startup (Callable[[dict[str, Any]], Awaitable[Any]] | None, optional): Async function called on startup. Defaults to None.
        shutdown (Callable[[dict[str, Any]], Awaitable[Any]] | None, optional): Async functions called on startup. Defaults to None.
        before_process (Callable[[dict[str, Any]], Awaitable[Any]] | None, optional): Async function called before a job processes.. Defaults to None.
        after_process (Callable[[dict[str, Any]], Awaitable[Any]] | None, optional): _Async function called after a job processes. Defaults to None.
        concurrency (int | None, optional): _description_. Defaults to None.

    Returns:
        Worker: The worker instance
    """
    if concurrency is None:
        concurrency = settings.worker.CONCURRENCY
    return Worker(
        queue,
        functions=tasks,
        cron_jobs=scheduled_tasks,
        startup=startup,
        shutdown=shutdown,
        before_process=before_process,
        after_process=after_process,
        concurrency=concurrency,
    )

def run_worker() -> None:
    """Run a worker."""
    from app.domain import scheduled_tasks, tasks
    from app.lib import log
    from app.lib.log.worker import after_process as after_logging_process
    from app.lib.log.worker import before_process as before_logging_process
    from app.lib.log.worker import on_shutdown as shutdown_logging_process
    from app.lib.log.worker import on_startup as startup_logging_process
    from app.lib.worker import queues

    if threading.current_thread() is not threading.main_thread():
        atexit.unregister(_exit_function)
    logger = log.get_logger()
    logger.info("Starting working pool")
    loop = _create_event_loop()
    worker_instances: list[Worker] = [
        create_worker_instance(
            queue=queue,
            tasks=tasks.get(queue, []),
            scheduled_tasks=scheduled_tasks.get(queue, []),
            startup=startup_logging_process,
            shutdown=shutdown_logging_process,
            after_process=after_logging_process,
            before_process=before_logging_process,
        )
        for queue in queues.values()
    ]
    try:
        for i, worker_instance in enumerate(worker_instances):
            if i < len(worker_instances) - 1:
                loop.create_task(worker_instance.start())
            else:
                loop.run_until_complete(worker_instance.start())
    except KeyboardInterrupt:
        for worker_instance in worker_instances:
            loop.run_until_complete(worker_instance.stop())

def _create_event_loop() -> asyncio.AbstractEventLoop:
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    return asyncio.new_event_loop()


from django.shortcuts import render
from Usuarios.models import Usuario
from django.http import HttpResponse
from Usuarios.serializers import UsuarioSerializer
from rest_framework.renderers import JSONRenderer

class JSONResponse(HttpResponse):

    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

def verUsuario(request, fbid):
    try:
        usuario = Usuario.objects.get(fbId=fbid)
    except Usuario.DoesNotExist:
        usuario = Usuario(fbId=fbid)
        usuario.baixarInformacoes()

    serializer = UsuarioSerializer(usuario, many=False)
    return JSONResponse(serializer.data)

def usuario(request):
    fbid = request.GET.get("fbid")
    try:
        usuario = Usuario.objects.get(fbId=fbid)
    except Usuario.DoesNotExist:
        return HttpResponse("0")
    serializer = UsuarioSerializer(usuario, many=False)
    return JSONResponse(serializer.data)


# -*- coding: utf-8 -*-
#!/usr/bin/env python3
'''
Created on Wed Jan 16 17:50:59 CST 2019
@Mail: minnglee@163.com
@Author: Ming Li
'''

import sys,os,logging,click

logging.basicConfig(filename=os.path.basename(__file__).replace('.py','.log'),
                    format='%(asctime)s: %(name)s: %(levelname)s: %(message)s',level=logging.DEBUG,filemode='w')
logging.info(f"The command line is:\n\tpython3 {' '.join(sys.argv)}")

def Rename(name):
    return f'{name.split(":")[0]}#{name.split(":")[1].replace("-","#")}'
def LoadFasta(File):
    Dict = {}
    seq = ''
    for line in File:
        line = line.strip()
        if line[0] == '>':
            if len(seq) > 0:
                Dict[name] = seq
            name = Rename(line.split()[0][1:])
            seq = ''
        else: seq += line
    Dict[name] = seq
    return Dict
@click.command()
@click.option('-i','--input',type=click.File('r'),help='The input file',required=True)
@click.option('-l','--length',type=int,help='the min length of seqence',default=500)
@click.option('-o','--output',type=click.File('w'),help='The output file',required=True)
def main(input,length,output):
    FaDict = LoadFasta(input)
    for key,value in FaDict.items():
        Gap = value.upper().count('N')
        if len(value) - Gap < length : continue
        while value[0].upper() == "N": value = value[1:]
        while value[-1].upper() == "N": value = value[:-1]
        output.write(f'>{key}\n{value}\n')
if __name__ == '__main__':
    main()


# 正規方程式
import numpy as np
import matplotlib.pyplot as plt

# 架空のデータセットの用意
# データは正規分布に従って生成
N = 100
mean1 = [1, 3]  # クラス1の平均
mean2 = [3, 1]  # クラス2の平均
cov = [[2.0,0.0], [0.0, 0.1]]  # 共分散行列（2クラス共通)
class_1 = np.random.multivariate_normal(mean1, cov, N//2)
class_2 = np.random.multivariate_normal(mean2, cov, N//2)
bias_x = np.ones((N, 1))
t_1 = np.ones(N//2)
t_2 = np.ones(N//2) * (-1)

# はじめてのパターン認識(P75)通りに変数を指定, 正規方程式からパラメータwを計算
X = np.concatenate((class_1, class_2))
X = np.concatenate((bias_x, X), axis=1)
t = np.concatenate((t_1, t_2)).reshape(N, 1)
w = np.dot(np.dot(np.linalg.inv(np.dot(X.T, X)), X.T), t)

# 求めたパラメータから直線を引く
w0, w1, w2 = w[0], w[1], w[2]
x = np.arange(start=-4, stop=10)
y = (((-1) * w1)/w2)*x + ((-1) * w0)/w2

plt.scatter(class_1[:, 0], class_1[:, 1], marker='o', color='red')
plt.scatter(class_2[:, 0], class_2[:, 1], marker='o', color='blue')
plt.plot(x, y)
plt.show()


import os
import json
import logging
import typing
from logging import Logger
from datetime import datetime

class LogBase:
    def __init__(self, log_name:str, file_share:str, identity:str = None):
        self.file_share = file_share
        self.log_name = log_name
        self.identity = identity

    def get_logger(self) -> Logger:
        return LoggingUtils.get_logger(self.file_share, self.log_name, self.identity)

class ActivityLog:
    ACTIVITY_BASE = "activity"

    def __init__(self, file_share:str, activity_name:str, identity:str = None):
        # Set up base logger

        self.file_share = file_share
        self.activity_name = activity_name

        if identity is not None:
            if identity is not None:
                self.activity_file = "{}-{}.log".format(self.activity_name,identity)
        else:
            timestamp = datetime.now().strftime('%m%d%y')
            self.activity_file = "{}-{}.log".format(self.activity_name,timestamp)

        self.log_path = os.path.join(self.file_share, ActivityLog.ACTIVITY_BASE)
        self.activity_log_path = os.path.join(self.log_path, self.activity_file)

        self._buffer:typing.List[str] = []

    def add_activity(self, *args):
        now = datetime.utcnow()
        stamp = now.strftime("%m-%d-%Y, %H:%M:%S,%f : ")
        for arg in args:
            content = arg
            if isinstance(arg, dict) or isinstance(arg, list):
                content = json.dumps(arg, indent=4)

            self._buffer.append("{} {}".format(stamp, content))

    def dump(self):
        if not os.path.exists(self.log_path):
            os.makedirs(self.log_path)

        with open(self.activity_log_path, "w") as log:
            for activity in self._buffer:
                log.writelines("{}\n".format(activity))

class LoggingUtils:
    LOG_UTILS:typing.Dict[str, Logger] = {}
    ACTIVE_LOG_FILE = None
    LOG_BASE = "output"

    @staticmethod
    def get_logger(file_share:str, log_name:str, identity:str = None) -> Logger:

        if log_name in LoggingUtils.LOG_UTILS:
            return LoggingUtils.LOG_UTILS[log_name]

        # Set up base logger
        timestamp = datetime.now().strftime('%m%d%y')
        LOG_FILE_NAME = "dataloader-{}.log".format(timestamp)
        if identity is not None:
            LOG_FILE_NAME = "dataloader-{}.log".format(identity)

        working_directory = file_share
        if not working_directory:
            working_directory = os.getcwd()

        LOG_FILE_PATH = os.path.join(
            working_directory,
            LoggingUtils.LOG_BASE,
            LOG_FILE_NAME
        )
        LoggingUtils.ACTIVE_LOG_FILE = LOG_FILE_PATH

        os.makedirs(os.path.dirname(LOG_FILE_PATH), exist_ok=True)

        handler = logging.FileHandler(LOG_FILE_PATH)
        handler.setFormatter(logging.Formatter(
            "%(asctime)s [%(name)-14.14s] [%(levelname)-7.7s]  %(message)s"))

        logger = logging.getLogger(log_name)

        try:
            level = logging.getLevelName(os.environ.get('LOG_LEVEL', 'info').upper())
            logger.setLevel(level)
        except ValueError:
            print('Valid Log Levels are DEBUG, INFO, WARN and ERROR')
            exit(1)

        logger.addHandler(handler)

        LoggingUtils.LOG_UTILS[log_name] = logger

        return LoggingUtils.LOG_UTILS[log_name]


import torch

import numpy
import pprint
import matplotlib.pyplot as plt

if __name__ == "__main__":
    checkpoint = torch.load("loss_log_original_3x.pt")

    x=checkpoint.numpy()

    epochs=[]
    loss=[]
    rows=x.shape[0]
    columns=x.shape[1]
    print(x.shape)
    for i in range(rows):
        for j in range(columns):

            epochs.insert(i,i)
            loss.insert(i,x[i][j])

    checkpoint1 = torch.load("loss_log_2_blocks_customized_3x.pt")
    print("enter")
    x1=checkpoint1.numpy()

    epochs1=[]
    loss1=[]
    rows1=x1.shape[0]
    columns1=x1.shape[1]
    for l in range(rows):
        for m in range(columns):

            epochs1.insert(l,l)
            loss1.insert(l,x1[l][m])

    plt.plot(epochs,loss,label = "original 3x",linestyle="-")
    plt.plot(epochs1,loss1,label = "model 7 3x " ,linestyle="-.")
    plt.title('Loss curves')
    plt.xlabel('epochs')
    plt.ylabel('loss')
    plt.legend()
    plt.show()


import re
import os
import pytesseract
from PIL import Image
from tqdm import tqdm

pytesseract.pytesseract.tesseract_cmd = (
    r"/usr/local/Cellar/tesseract/5.1.0/bin/tesseract"
)
directory = ""

def preprocess_text(text):
    text = text.lower().strip()
    text = re.sub(re.escape("\n"), " ", text)
    return text

def main():
    print("Loading images...")
    file_strings = dict()
    images_paths = [os.path.join(directory, file) for file in os.listdir(directory) if os.fsdecode(file).endswith(".png")]
    for file_path in tqdm(images_paths):
            file_string = pytesseract.image_to_string(file_path)
            file_string = preprocess_text(file_string)
            file_strings[file_path] = file_string
    print("Images loaded.")
    print()

    while True:
        print("Introduce phrase:")
        text = input()
        print("")
        print("searching...")
        text = preprocess_text(text)
        files = [
            file_path
            for file_path, image_text in file_strings.items()
            if text in image_text
        ]
        for file in files:
            img = Image.open(file)
            img.show()
        print("DONE")
        print("")

if __name__ == "__main__":
    main()


from typing import Optional
from fastapi import APIRouter, Depends, Request, Response
from sqlalchemy.ext.asyncio import AsyncSession

from .dao import LandmarkDAO
from .service import DatabaseManager
from .schemas import LandmarkCreate, LandmarkBase, Landmark, LandmarkId
from ..database import get_async_session

router = APIRouter()

@router.post("/create_landmark/", response_model=Landmark)
async def create_landmark(
    request: Request,
    landmark_data: LandmarkCreate,
    token: str,
    db: AsyncSession = Depends(get_async_session),
) -> Landmark:
    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    if token:
        return await landmark_crud.create_landmark(token=token, request=request, landmark=landmark_data)

@router.post("/get_landmark/", response_model=Landmark)
async def get_landmark(
    landmark_data: LandmarkId,
    db: AsyncSession = Depends(get_async_session),
) -> Landmark:
    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    return await landmark_crud.get_existing_landmark(id=landmark_data.id)

# Получение списка всех пользователей
@router.get("/read_all_landmarks")
async def get_all_landmarks(
    offset: int = 0,
    limit: int = 10,
    db: AsyncSession = Depends(get_async_session),
):
    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    return await landmark_crud.get_all_landmarks(offset=offset, limit=limit)

@router.delete("/delete_landmark")
async def delete_landmark(
    id: str = None,
    title: str = None,
    address: str = None,
    db: AsyncSession = Depends(get_async_session),
):

    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    await landmark_crud.delete_landmark(id=id, title=title, address=address)

    return {"message": "Deleting successful"}

@router.post("/add_to_favorite")
async def add_to_favorite(
    request: Request,
    landmark_id: LandmarkId,
    token: str,
    db: AsyncSession = Depends(get_async_session),
):

    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    return await landmark_crud.add_to_favorite(token=token, request=request, landmark_data=landmark_id.id)

@router.post("/remove_from_favorite")
async def remove_from_favorite(
    request: Request,
    landmark_id: LandmarkId,
    token: str,
    db: AsyncSession = Depends(get_async_session),
):

    db_manager = DatabaseManager(db)
    landmark_crud = db_manager.landmark_crud

    return await landmark_crud.remove_from_favorite(token=token, request=request, landmark_data=landmark_id.id)


#!/usr/bin/env python3

'''Wait for Cozmo to see a face, and then turn on his backpack light.

This is a script to show off faces, and how they are easy to use.
It waits for a face, and then will light up his backpack when that face is visible.
'''

import asyncio
import time

import cozmo

def light_when_face(robot: cozmo.robot.Robot):
    '''The core of the light_when_face program'''

    # Move lift down and tilt the head up
    robot.move_lift(-3)
    robot.set_head_angle(cozmo.robot.MAX_HEAD_ANGLE).wait_for_completed()

    face = None

    print("Press CTRL-C to quit")
    while True:
        if face and face.is_visible:
            robot.set_all_backpack_lights(cozmo.lights.blue_light)
        else:
            robot.set_backpack_lights_off()

            # Wait until we we can see another face
            try:
                face = robot.world.wait_for_observed_face(timeout=30)
            except asyncio.TimeoutError:
                print("Didn't find a face.")
                return

        time.sleep(.1)

cozmo.run_program(light_when_face, use_viewer=True, force_viewer_on_top=True)


import json
import os
from typing import Dict
from typing import List
from typing import Optional

from git import Repo

# Global Configuration
GIT_REPO_BASE_PATH = "./repo"
AUTHOR_LICENSE_FILE = "./config/author-licenses.txt"
PSEUDONYMS_FILE = "./config/pseudonyms.txt"
EXCLUDE_DIRS = [".git"]

def _load_pseudonyms(path: str):
    cache = {}
    if os.path.exists(path):
        with open(path) as f:
            for line in f:
                module, author, email, license = line.strip().split("|")
                cache[module] = {
                    "author": author,
                    "email": email,
                    "license": license,
                }
    return cache

def _load_author_licenses(path: str):
    cache = {}
    if os.path.exists(path):
        with open(path) as f:
            for line in f:
                author, license = line.strip().split("|")
                cache[author] = license
    return cache

def _get_git_repo_path(base_path: str):
    _root, dirs, _files = next(os.walk(base_path))
    return os.path.join(base_path, dirs[0])

# Internal Configuration
_GIT_REPO_PATH = _get_git_repo_path(GIT_REPO_BASE_PATH)
_BUILT_PATH = "./build"
_BUILT_BLAME_FILE = f"{_BUILT_PATH}/blame.json"
_BUILT_HTML_FILE = f"{_BUILT_PATH}/authorship.html"
_PSEUDONYMS_CACHE = _load_pseudonyms(PSEUDONYMS_FILE)
_AUTHOR_LICENSE_CACHE = _load_author_licenses(AUTHOR_LICENSE_FILE)

repo = Repo(_GIT_REPO_PATH)

def file_blame(path: str):
    blame = repo.blame("HEAD", path, rev_opts=["-M", "-C", "-C", "-C"])
    return (
        [
            [(commit.author.name, commit.author.email), len(lines)]
            for commit, lines in blame
        ]
        if blame
        else []
    )

def folder_blame(path: str) -> Dict:
    print(f"Blaming {path}")
    root, dirs, files = next(os.walk(path))
    return {
        "files": {
            file_name: file_blame(
                os.path.join(root, file_name)[len(_GIT_REPO_PATH) + 1 :]
            )
            for file_name in files
        },
        "dirs": {
            dir_name: folder_blame(os.path.join(root, dir_name))
            for dir_name in dirs
            if dir_name not in EXCLUDE_DIRS
        },
    }

def repo_blame():
    from pathlib import Path

    if os.path.exists(_BUILT_BLAME_FILE):
        with open(_BUILT_BLAME_FILE) as f:
            return json.load(f)
    else:
        blame = folder_blame(_GIT_REPO_PATH)
        Path(_BUILT_PATH).mkdir(parents=True, exist_ok=True)
        with open(_BUILT_BLAME_FILE, "w") as f:
            json.dump(blame, f)
    return blame

#####################################

class ModuleAnalyzer:
    name: str
    parent: str
    blame: List
    submodules: List["ModuleAnalyzer"]

    def __init__(
        self, name: str, parent: str, submodules: List["ModuleAnalyzer"] = None
    ):
        self.name = name
        self.parent = parent
        self.submodules = submodules or []

    def with_blame(self, blame) -> "ModuleAnalyzer":
        self.blame = blame
        return self

    def authorship(self):
        authors = {}
        for submodule in self.submodules:
            for author, lines in submodule.authorship().items():
                authors[author] = authors.get(author, 0) + lines
        return authors

    def licensing(self):
        licenses = {}
        for submodule in self.submodules:
            for license, lines in submodule.licensing().items():
                licenses[license] = licenses.get(license, 0) + lines
        return licenses

    def flatten(self):
        flat = [self]
        for submodule in self.submodules:
            flat += submodule.flatten()
        return flat

class FileModuleAnalyzer(ModuleAnalyzer):
    def authorship(self):
        NAME = 0  # list idx
        authors = {}
        for author, lines in self.blame:
            name = self.__author_override() or author[NAME]
            authors[name] = authors.get(name, 0) + lines
        return authors

    def __author_override(self) -> Optional[str]:
        for override_path, override in _PSEUDONYMS_CACHE.items():
            if override_path in self.name:
                return override["author"]
        return None

    def licensing(self):
        NAME = 0  # list idx
        licenses = {}
        for author, lines in self.blame:
            license = self.__license_override() or _AUTHOR_LICENSE_CACHE.get(
                author[NAME], "???"
            )
            licenses[license] = licenses.get(license, 0) + lines
        return licenses

    def __license_override(self) -> Optional[str]:
        # Note, uses the first matching entry.
        # Consider using the longest matching entry instead.
        for override_path, override in _PSEUDONYMS_CACHE.items():
            if override_path in self.name:
                return override["license"]
        return None

def raw_blame_to_module_analyzer(
    module_name: str, parent_name: str, raw_blame: Dict[str, Dict]
) -> ModuleAnalyzer:
    return ModuleAnalyzer(
        module_name,
        parent_name,
        [
            *[
                FileModuleAnalyzer(
                    f"{module_name}/{file_name}", module_name
                ).with_blame(blame)
                for file_name, blame in raw_blame["files"].items()
            ],
            *[
                raw_blame_to_module_analyzer(
                    f"{module_name}/{dirname}", module_name, dirblame
                )
                for dirname, dirblame in raw_blame["dirs"].items()
            ],
        ],
    )

#####################################

blame = repo_blame()
analyzer = raw_blame_to_module_analyzer(_GIT_REPO_PATH, "", blame)
stats = analyzer.authorship()
print(stats)

#####################################

import plotly.graph_objects as go

modules = analyzer.flatten()
ids = [module.name for module in modules]
labels = [i.split("/")[-1] for i in ids]
parents = [module.parent for module in modules]
values = [sum(module.authorship().values()) for module in modules]

def authorship_str(module: ModuleAnalyzer) -> str:
    authors = module.authorship()
    return "
Authors:
 - " + "
 - ".join(
        f"{author}: {lines}" for author, lines in authors.items()
    )

def licensing_str(module: ModuleAnalyzer) -> str:
    licenses = module.licensing()
    return "
Licenses:
 - " + "
 - ".join(
        f"{license}: {lines}" for license, lines in licenses.items()
    )

def info_str(module: ModuleAnalyzer) -> str:
    return f"{authorship_str(module)}
{licensing_str(module)}"

fig = go.Figure(
    go.Treemap(
        ids=ids,
        labels=labels,
        parents=parents,
        values=values,
        maxdepth=3,
        branchvalues="total",
        text=[info_str(module) for module in modules],
        hovertemplate="%{label}

%{value} lines
%{text}",
        root_color="lightgrey",
    )
)

fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.write_html(_BUILT_HTML_FILE)


from six.moves.urllib.parse import urljoin, urlsplit, urlunsplit

from pywb.rewrite.wburl import WbUrl
from pywb.rewrite.cookie_rewriter import get_cookie_rewriter

#=================================================================
class UrlRewriter(object):
    """
    Main pywb UrlRewriter which rewrites absolute and relative urls
    to be relative to the current page, as specified via a WbUrl
    instance and an optional full path prefix
    """

    NO_REWRITE_URI_PREFIX = ('#', 'javascript:', 'data:',
                             'mailto:', 'about:', 'file:', '{')

    PROTOCOLS = ('http:', 'https:', 'ftp:', 'mms:', 'rtsp:', 'wais:')

    REL_SCHEME = ('//', r'\/\/', r'\\/\\/')

    PARENT_PATH = '../'
    REL_PATH = '/'

    def __init__(self, wburl, prefix='', full_prefix=None, rel_prefix=None,
                 root_path=None, cookie_scope=None, rewrite_opts=None, pywb_static_prefix=None):
        self.wburl = wburl if isinstance(wburl, WbUrl) else WbUrl(wburl)
        self.prefix = prefix
        self.full_prefix = full_prefix or prefix
        self.rel_prefix = rel_prefix or prefix
        self.root_path = root_path or '/'
        if self.full_prefix and self.full_prefix.startswith(self.PROTOCOLS):
            self.prefix_scheme = self.full_prefix.split(':')[0]
        else:
            self.prefix_scheme = None
        self.prefix_abs = self.prefix and self.prefix.startswith(self.PROTOCOLS)
        self.cookie_scope = cookie_scope
        self.rewrite_opts = rewrite_opts or {}
        self._pywb_static_prefix = pywb_static_prefix

        if self.rewrite_opts.get('punycode_links'):
            self.wburl._do_percent_encode = False

    @property
    def pywb_static_prefix(self):
        """Returns the static path URL
        :rtype: str
        """
        if self._pywb_static_prefix is None:
            return ''
        if self._pywb_static_prefix.startswith(self.PROTOCOLS):
            return self._pywb_static_prefix
        return self.urljoin(self.full_prefix, self._pywb_static_prefix)

    def rewrite(self, url, mod=None, force_abs=False):
        # if special protocol, no rewriting at all
        if url.startswith(self.NO_REWRITE_URI_PREFIX):
            return url

        if (self.prefix and
             self.prefix != '/' and
             url.startswith(self.prefix)):
            return url

        if (self.full_prefix and
             self.full_prefix != self.prefix and
             url.startswith(self.full_prefix)):
            return url

        wburl = self.wburl

        is_abs = url.startswith(self.PROTOCOLS)

        scheme_rel = False
        if url.startswith(self.REL_SCHEME):
            is_abs = True
            scheme_rel = True
        elif (not force_abs and not is_abs and
              not url.startswith(self.REL_PATH) and
              self.PARENT_PATH not in url):
            return url

            # if prefix starts with a scheme
            #if self.prefix_scheme:
            #    url = self.prefix_scheme + ':' + url
            #url = 'http:' + url

        # optimize: join if not absolute url, otherwise just use as is
        if not is_abs:
            new_url = self.urljoin(wburl.url, url)
        else:
            new_url = url

        if mod is None:
            mod = wburl.mod

        final_url = self.prefix + wburl.to_str(mod=mod, url=new_url)

        if not is_abs and self.prefix_abs and not self.rewrite_opts.get('no_match_rel'):
            parts = final_url.split('/', 3)
            final_url = '/'
            if len(parts) == 4:
                final_url += parts[3]

        # experiment for setting scheme rel url
        elif scheme_rel and self.prefix_abs:
            final_url = final_url.split(':', 1)[1]

        return final_url

    def get_new_url(self, **kwargs):
        return self.prefix + self.wburl.to_str(**kwargs)

    def rebase_rewriter(self, base_url):
        if not base_url.startswith(self.PROTOCOLS):
            base_url = self.urljoin(self.wburl.url, base_url)

        new_wburl_str = self.wburl.to_str(url=base_url)
        new_wburl = WbUrl(new_wburl_str)

        return self._create_rebased_rewriter(new_wburl, self.prefix)

    def _create_rebased_rewriter(self, new_wburl, prefix):
        return UrlRewriter(new_wburl, prefix)

    def get_cookie_rewriter(self, scope=None):
        # collection scope overrides rule scope?
        if self.cookie_scope:
            scope = self.cookie_scope

        cls = get_cookie_rewriter(scope)
        return cls(self)

    def deprefix_url(self):
        return self.wburl.deprefix_url(self.full_prefix)

    def __repr__(self):
        return "UrlRewriter('{0}', '{1}')".format(self.wburl, self.prefix)

    @staticmethod
    def urljoin(orig_url, url):  # pragma: no cover
        new_url = urljoin(orig_url, url)
        if '../' not in new_url:
            return new_url

        # only needed in py2 as py3 urljoin resolves '../'
        parts = urlsplit(new_url)
        scheme, netloc, path, query, frag = parts

        path_parts = path.split('/')
        i = 0
        n = len(path_parts) - 1
        while i < n:
            if path_parts[i] == '..':
                del path_parts[i]
                n -= 1
                if i > 0:
                    del path_parts[i - 1]
                    n -= 1
                    i -= 1
            else:
                i += 1

        if path_parts == ['']:
            path = '/'
        else:
            path = '/'.join(path_parts)

        parts = (scheme, netloc, path, query, frag)

        new_url = urlunsplit(parts)
        return new_url

#=================================================================
class IdentityUrlRewriter(UrlRewriter):
    """
    No rewriting performed, return original url
    """
    def rewrite(self, url, mod=None, force_abs=False):
        return url

    def get_new_url(self, **kwargs):
        return kwargs.get('url', self.wburl.url)

    def rebase_rewriter(self, new_url):
        return self

    def get_cookie_rewriter(self, scope=None):
        return None

    def deprefix_url(self):
        return self.wburl.url

#=================================================================
class SchemeOnlyUrlRewriter(IdentityUrlRewriter):
    """
    A url rewriter which ensures that any urls have the same
    scheme (http or https) as the base url.
    Other urls/input is unchanged.
    """

    def __init__(self, *args, **kwargs):
        super(SchemeOnlyUrlRewriter, self).__init__(*args, **kwargs)
        self.url_scheme = self.wburl.url.split(':')[0]
        if self.url_scheme == 'https':
            self.opposite_scheme = 'http'
        else:
            self.opposite_scheme = 'https'

    def rewrite(self, url, mod=None, force_abs=False):
        if url.startswith(self.opposite_scheme + '://'):
            url = self.url_scheme + url[len(self.opposite_scheme):]

        return url


"""Resources related with fractals.

Refs:
 - https://realpython.com/mandelbrot-set-python/
"""

from dataclasses import dataclass
import math
from typing import Union

import numpy as np
import matplotlib.pyplot as plt

def mandelbrot_sequence(c):
    """Returns generator of the Mandelbrot sequence given C"""
    z = 0
    while True:
        yield z
        z = z**2 + c

def julia_sequence(c, z=0):
    """Returns generator of a Julia sequence given C and Z!=0

    Note: if Z=0 then Julia_seq==Mandelbrot_seq
    """
    while True:
        yield z
        z = z**2 + c

def mandelbrot_sequence_index(n, c):
    """Returns the n-th value of the Mandelbrot sequence given C"""
    if n == 0:
        return 0
    else:
        return mandelbrot_sequence_index(n - 1, c) ** 2 + c

def pixel_matrix(pixel_density, xmin=-2, xmax=0.5, ymin=-1.5, ymax=1.5):
    """Generate a matrix for plotting Mandelbrot/Julia sets.

    Args:
     - pixel_density: Number of pixels per unit
    """
    re = np.linspace(xmin, xmax, int((xmax - xmin) * pixel_density))
    im = np.linspace(ymin, ymax, int((ymax - ymin) * pixel_density))
    return re[np.newaxis, :] + im[:, np.newaxis] * 1j

def is_mandelbrot_elem(c, num_iterations):
    """Returns if a value is stable (relatively small) and is element of Mandelbrot set"""
    z = 0
    for _ in range(num_iterations):
        z = z**2 + c
    return abs(z) <= 2

def mask_pixel_matrix(c, num_iterations):
    """Return pixel_matrix with bool elements indicating if pixel is Mandelbrot elem"""
    mask = is_mandelbrot_elem(c, num_iterations)
    return c[mask]

def plot_mandelbrot_ori(pixel_density=21):
    """Plot replica of first ever visualization of Mandelbrot set"""

    c = pixel_matrix(pixel_density=pixel_density)
    members = mask_pixel_matrix(c, num_iterations=20)

    plt.scatter(members.real, members.imag, color="black", marker=",", s=1)
    plt.gca().set_aspect("equal")
    plt.axis("off")
    plt.tight_layout()
    plt.show()

def plot_mandelbrot(pixel_density=512):
    """Plot Mandelbrot set"""
    c = pixel_matrix(pixel_density=pixel_density)

    plt.imshow(is_mandelbrot_elem(c, num_iterations=20), cmap="binary")
    plt.gca().set_aspect("equal")
    plt.axis("off")
    plt.tight_layout()
    plt.show()

@dataclass
class MandelbrotSet:
    """Encapsulates the logic to compute if a value is element of Mandelbrot set.

    Optionally return a value between 0 and 1 if the approximation is not enough.
    """

    max_iterations: int
    escape_radius: float = 2.0

    def __contains__(self, c: complex) -> bool:
        return self.stability(c) == 1

    def stability(self, c: complex, smooth=False, clamp=True) -> float:
        value = self.escape_count(c, smooth) / self.max_iterations
        return max(0.0, min(value, 1.0)) if clamp else value

    def escape_count(self, c: complex, smooth=False) -> Union[int, float]:
        z = 0
        for iteration in range(self.max_iterations):
            z = z**2 + c
            if abs(z) > self.escape_radius:
                if smooth:
                    return iteration + 1 - math.log(math.log(abs(z))) / math.log(2)
                return iteration
        return self.max_iterations


from gensim import corpora, models
from glob import glob
from operator import itemgetter
from scipy.sparse.csgraph import connected_components
from statistics import mean
import fasttext
import hdbscan
import collections
import umap
import os
import sys
import csv
import string
import codecs
import re
import time
import MeCab
import pandas as pd
import numpy as np

#MeCab tokenizer
def mecab_tokenizer(tx, sw):
    token_list = []
    tagger = MeCab.Tagger('/usr/local/lib/mecab/dic/mecab-ipadic-neologd')

    tagger.parse('')
    node = tagger.parseToNode(tx)
    while node:
        if not node.surface in sw:
            pos = node.feature.split(",")
            if not pos[0] in ["記号"]:
                token_list.append(node.surface)
        node = node.next
    return list(token_list)

#Feature-word extractor
def fword_extractor(num, docs):
    fword = docs[0][0]
    for c in range(num+1):
        if not c == 0:
            fword = fword + '|' + docs[c][0]
    return fword

#FastText vectorizer
def ft_vectorizer(tx, bsn):
    model_path = "02_models/cc.ja.300.bin"
    if not os.path.exists(model_path):
        model_txt_path = "02_models/" + bsn + "_ft_corpus" + ".txt"
        model_alt_path = "02_models/" + bsn + "_ft_corpus" + ".bin"
        with open(model_txt_path, 'w', encoding="utf_8") as f:
            text_corpus = ""
            for t in tx:
                text_corpus = " ".join(map(str, t))
                f.write(text_corpus+"\n")
        print("New FastText model was created at " + model_alt_path)
        model = fasttext.train_unsupervised(model_txt_path, model='cbow', dim=300)
        #model.save_model(model_alt_path)
    else:
        print("Pre-trained model " + model_path + " is used.")
        model = fasttext.load_model(model_path)

    ft_vec = np.zeros((df.shape[0], 300))
    #Prepairing an empty list for the model coverage rate
    coverage = []
    #Caluculating the average value of the vector of words by a document
    for i,doc in enumerate(tx):
        feature_vec = np.zeros(300) #Initializing the 300-dimension vector data to 0
        num_words = 0
        no_count = 0
        for word in doc:
            try:
                feature_vec += model[str(word)]
                num_words += 1
            except:
                no_count += 1
        feature_vec = feature_vec / num_words
        ft_vec[i] = feature_vec
        #Caluculating the word coverage rate of the model by each document
        cover_rate = num_words / (num_words + no_count)
        coverage.append(cover_rate)
    #Print overall word coverage rate of the model
    mean_coverage = round(mean(coverage)*100, 2)
    print("Word cover-rate: " + str(mean_coverage) + "%")
    return ft_vec

os.makedirs("01_dataset", exist_ok=True)
os.makedirs("02_models", exist_ok=True)
os.makedirs("03_processing", exist_ok=True)

#Open the input-data and store it into DataFrame
docs_file = input('Input a document-list file (.csv) in 01_dataset >> ')
docs_path = "01_dataset/" + str(docs_file)
if os.path.exists(docs_path):
    with codecs.open(docs_path, 'r', 'utf-8', 'ignore') as f:
        #df = pd.read_csv(f, delimiter='\t')
        df = pd.read_csv(f)
    base_name = os.path.splitext(os.path.basename(docs_file))[0] #The file name without the extension
else:
    print('Processing was cancelled due to an invalid input. Please enter a correct file name')
    sys.exit()

#Open stopword list
stops_file = input('Input a stopword-list file (.txt) in 02_models >> ')
stops_path = "02_models/" + str(stops_file)
if os.path.exists(stops_path):
    with codecs.open(stops_path, 'r', 'utf-8', 'ignore') as f:
        stopwords = f.read().splitlines()
else:
    print('The stopword file is not found. This process continues without stopwords.')
    stopwords = []

#Target column
df['text'] = df['description'].astype(str)
#Tokenizer
text_tokenizer = mecab_tokenizer

print("Input data size : " + str(len(df)))

#Remove https-links
df['text_clean'] = df.text.map(lambda x: re.sub(r'https?://[\w/:%#\$&\?\(\)~\.=\+\-]+', "", x))
#Remove numerals
df['text_clean'] = df.text_clean.map(lambda x: re.sub(r'\d+', '', x))
#Remove symbols
df['text_clean'] = df.text_clean.map(lambda x: re.sub(r'[「」。、,（）%#\$&\?\(\)~\.=\+\-\[\]\{\}\|\*]+', '', x))
#Remove specific noises (optional)
df['text_clean'] = df.text_clean.map(lambda x: re.sub(r'○.{0,20}　', ' ', x))
#Creating DataFrame for Token-list
df['text_tokens'] = df.text_clean.map(lambda x: text_tokenizer(x, stopwords))

#Creating dictionary and corpus
texts = df['text_tokens'].values
dictionary = corpora.Dictionary(texts)
dictionary.filter_extremes(no_below=3, no_above=0.4)
corpus = [dictionary.doc2bow(text) for text in texts]

#Feature words extraction by tf-idf (option: min_df, max_df)
tfidf_model = models.TfidfModel(corpus, id2word=dictionary.token2id, normalize=False)
tfidf_corpus = list(tfidf_model[corpus])

method_name = "FT" #For the output file name
doc_vec = ft_vectorizer(df['text_tokens'], base_name)

tfidf_texts = []
for doc in tfidf_corpus:
    tfidf_text = []
    for word in doc:
        tfidf_text.append([dictionary[word[0]], word[1]])
        tfidf_text_sort = sorted(tfidf_text, reverse=True, key=itemgetter(1))
    tfidf_texts.append(tfidf_text_sort)

num_fword = 4 #Set the number of displayed feature words
fword_list = []
for i in range(len(tfidf_texts)):
    if len(tfidf_texts[i]) > num_fword-1:
        fword = fword_extractor(num_fword-1, tfidf_texts[i])
    else:
        fword = fword_extractor(len(tfidf_texts[i])-1, tfidf_texts[i])
    fword_list.append(fword)

#Storing in DataFrame
embedding_u = umap.UMAP(min_dist=0.1, n_neighbors=50, metric='euclidean', spread=1.0, init='spectral').fit_transform(doc_vec)
embedding = pd.DataFrame(embedding_u, columns=['x', 'y'])
embedding['uid'] = df['ID']
embedding['keywords'] = fword_list
embedding['size'] = df['size']
embedding['title'] = df['title']
embedding['date'] = df['date']
embedding['author'] = df['author']
embedding['affiliation'] = df['affiliation']
embedding['description'] = df['description']

x_coord = embedding_u[:, 0]
y_coord = embedding_u[:, 1]

#Clustering
clst_input = input('Input the number for min-size of a cluster (default=100) >> ')
try:
    clusters = int(clst_input)
except:
    clusters = 100 #set default value
    print("The number of cluster was set to 100, due to an invalid input")

type_input = input('Input type of clustering - eom:1 or leaf:2 >> ')
if type_input == "1":
    type_c = 'eom'
elif type_input == "2":
    type_c = 'leaf'
else:
    type_c = 'leaf'
    print("Type of the clustering is set to leaf due to an invalid input.")

clustering = hdbscan.HDBSCAN(cluster_selection_method=type_c, min_cluster_size=clusters, min_samples=10) #The lower the value, the less noise you’ll get
log_method = "HDBSCAN (minimum size of clusters: " + str(clusters) + " )"

log_list = []
label_list = []
log_list.append(log_method)

#Extract only XY coordinates from DataFrame for the clustering calculation
df_xy = embedding.loc[:, ['x', 'y']]

#Convert XY coordicates into numpy array
X = df_xy.to_numpy()
log_doc = "Documents : " + str(X.shape[0])
log_list.append(log_doc)
print(log_doc)

#Execution of clustering (using scikit-learn based modules)
clustering.fit(X)

#Store the label data
labels = clustering.labels_
num_labels = len(set(labels)) - (1 if -1 in labels else 0)
outliers = list(labels).count(-1)
log_label = "labels : " + str(num_labels) + ", outliers : " + str(outliers)
log_list.append(log_label)
print(log_label)

#Create DataFrame of ID and Label No
embedding_L1 = pd.DataFrame()
embedding_L1["id"] = df['ID']
embedding_L1["keywords"] = fword_list
embedding_L1["X"] = x_coord
embedding_L1["Y"] = y_coord
embedding_L1["label"] = labels

#Feature words extraction
num = 0
log_head = "area_id" + '\t' + "documents" + '\t' + "keywords" + '\t' + "X" + '\t' + "Y" + '\t' + "label"
log_list.append(log_head)
label_list.append(log_head)
while num < num_labels:
    f_list = []
    embedding_L2 = embedding_L1.query("label == @num")
    fwords = np.array(embedding_L2['keywords'])
    s = fwords.T

    for i in s:
        try:
            l = [x.strip() for x in i.split('|')]
            f_list.extend(l)
        except:
            pass

    c = collections.Counter(f_list)
    top_c = c.most_common(40)[0:39]
    doc_n = len(embedding_L2)
    x_mean = embedding_L2["X"].mean()
    y_mean = embedding_L2["Y"].mean()

    try:
        l_key1 = top_c[0][0] + "/" + top_c[1][0] + "/" + top_c[2][0]
        l_key2 = [k for k, v in top_c[3:] if v / doc_n > 0.4 and v / doc_n < 0.65]
        if len(l_key2) > 0:
            l_keys = l_key1 + "/" + l_key2[0]
        else:
            l_keys = l_key1
    except:
        l_keys = "-"

    log_fwords = str(num) + "\t" + str(doc_n) + "\t" + str(top_c) + "\t" + str(round(x_mean, 4)) + "\t" + str(round(y_mean, 4)) + "\t" + str(l_keys) #log the feature words of areas
    log_list.append(log_fwords)
    label_list.append(log_fwords)
    print(log_fwords)
    num += 1

#export the result
embedding.to_csv("03_processing/" + base_name + "_by" + method_name + ".csv", encoding="utf_8", index=False)

log_file = "log_" + str(num_labels) + ".txt"
with open("03_processing/" + base_name + "_by" + method_name + "_" + log_file, 'w', encoding="utf_8") as t:
    t.write("\n".join(log_list))

label_name = "label_" + str(num_labels) + ".tsv"
with open("03_processing/" + base_name + "_by" + method_name + "_" + label_name, 'w', encoding="utf_8") as t:
    t.write("\n".join(label_list))

print("Completed.")


from itertools import permutations

def is_prime(s, primes):
    if not s or int(s) in primes or int(s) <= 1:
        return False
    n = int(s)
    if n == 2:
        return True
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

def dfs(s, numbers, idx, primes):
    global ans
    if is_prime(s, primes):
        primes.add(int(s))
    if idx >= len(numbers):
        return

    dfs(s+numbers[idx], numbers, idx+1, primes)
    dfs(s, numbers, idx+1, primes)

def solution(numbers):
    primes = set()
    nums = [n for n in numbers]
    nums_perm = list(map("".join, permutations(nums)))
    for num in nums_perm:
        dfs("", num, 0, primes)

    answer = len(primes)

    return answer


from Gwe_service.api.controller.CouponListObj import CouponListObj as TemplateController
from common.func import *

class CouponList(TemplateController):
    """根据优惠券名称和类型分页获取优惠券列表"""
    def __init__(self, status=0, message='操作成功', **kwargs):
        super(CouponList, self).__init__()
        self.status = status  # 期望返回结果
        self.message = message
        self.update_default_body(**kwargs)

    def update_default_body(self, **kwargs):
        self.body.update_value(**kwargs)

    def check(self):
        """
        1. 检查状态self.status
        2. 检查resp
        3. 检查数据落库
        2. 检查其他内容：redis, mq ,
        :return:
        """

        assert str(self.status) == str(self.resp.code), "该接口实际返回结果中的code{act_code}码不符合期望结果{exp_code}".format(act_code=self.resp.code,
                                                                                exp_code=self.status)
        assert str(self.message) == str(self.resp.message)

        from Gwe_service.data.status import ConsStatusCode
        if self.status == ConsStatusCode.OK:
            # 检查数据库
            exp_obj = TemplateController.Resp.CommonPageSmsCoupon()
            from Gwe_service.check.GwePWUtil import db
            coupon_detail = db.sms_coupon(id=3)
            if coupon_detail:
                fill_in_obj_from_obj(exp_obj, coupon_detail[0])
                # f = "%Y-%m-%dT%H:%M:%S.000+0000"
                # exp_obj.startTime = exp_obj.startTime.strftime(format=f)
                # exp_obj.endTime = exp_obj.endTime.strftime(format=f)
                # exp_obj.enableTime = exp_obj.enableTime.strftime(format=f)

                from common.ObjAssert import ObjAssert
                ObjAssert().is_equal(exp_obj=exp_obj.list[0], act_obj=self.resp.data.list[0], is_toggle=False, ex=[])
            else:
                assert self.resp.data.list[0] is None


import boto3

from django.conf import settings

SENDER_ID = 'CovidBeacon'
client = boto3.client('sns',
                      aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                      aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
                      region_name='eu-west-1')

def send_sms(phone, msg):
    client.publish(PhoneNumber=phone,
                   Message=msg,
                   MessageAttributes={
                       'AWS.SNS.SMS.SenderID': {
                           'DataType': 'String',
                           'StringValue': SENDER_ID,
                        },
                       'AWS.SNS.SMS.SMSType': {
                           'DataType': 'String',
                           'StringValue': 'Transactional',
                        }
                    })


import ctypes, pygame

class Resize:
    def __init__(self, W, H):
        self.w = W
        self.h = H
        self.Log = [(self.w, self.h), (self.w, self.h)]
        self.user = ctypes.windll.user32
        self.relW = self.user.GetSystemMetrics(0)
        self.relH = int(self.user.GetSystemMetrics(1) - self.user.GetSystemMetrics(1) // 20.5 + (self.user.GetSystemMetrics(1)*1.017518248175182 - self.user.GetSystemMetrics(1)))
        self.wh_Ratio = self.relW / self.relH
        self.hw_Ratio = self.relH / self.relW
        self.screenW = W
        self.screenH = H

    def resizeScreen(self):
        newScreen = pygame.display.set_mode((self.screenW, self.screenH), pygame.RESIZABLE)
        return newScreen

    def resizeWidthHeight(self, eventW, eventH):
        if self.screenW != eventW:
            self.screenW = int(eventW)
            self.screenH = int(self.screenW // self.wh_Ratio)
        else:
            self.screenH = int(eventH)
            self.screenW = int(self.screenH // self.hw_Ratio)

        self.Log.append((self.screenW, self.screenH))
        return self.screenW, self.screenH

    def resizeIMG(self, IMGPath, imgSize):

        ratioW = self.Log[-2][0] / imgSize[0]
        ratioH = self.Log[-2][1] / imgSize[1]

        W = self.screenW / ratioW
        H = self.screenH / ratioH

        if type(IMGPath) == str:
            newIMG = pygame.transform.scale(pygame.image.load(IMGPath).convert_alpha(), (int(W), int(H)))
        else:
            newIMG = pygame.transform.scale(IMGPath.convert_alpha(), (int(W), int(H)))

        return newIMG

    def resizeFont(self, normalPNT, ttf):
        font = pygame.font.Font(f'{ttf}', int(normalPNT * self.screenH / self.h))
        return font


import os
import subprocess as sp
from io import open

from setuptools import Extension, find_packages, setup

import versioneer
from compile_externals import compile_all

CONFIG_NAME = "gimmemotifs.cfg"
DESCRIPTION = "GimmeMotifs is a motif prediction pipeline."

with open("README.md", encoding="utf-8") as f:
    long_description = f.read().strip("\n")

# are we in the conda build environment?
conda_build = os.environ.get("CONDA_BUILD")

module1 = Extension("gimmemotifs.c_metrics", sources=["gimmemotifs/c_metrics.c"])

MOTIF_BINS = {
    "AMD": ["src/AMD/AMD.bin"],
    "BioProspector": ["src/BioProspector/BioProspector"],
    "MDmodule": ["src/MDmodule/MDmodule"],
    "Posmo": ["src/posmo/posmo", "src/posmo/clusterwd"],
}

cmdclass = versioneer.get_cmdclass()
my_build_py = cmdclass["build_py"]

class build_tools(my_build_py):
    user_options = []

    def initialize_options(self):
        my_build_py.initialize_options(self)
        self.build_base = None
        self.build_lib = None

    def finalize_options(self):
        my_build_py.finalize_options(self)
        self.set_undefined_options("build", ("build_base", "build_base"))
        self.set_undefined_options("build", ("build_lib", "build_lib"))

    def run(self):
        if not self.dry_run:
            src_dir = os.path.join(self.build_base, "src")
            target_dir = os.path.join(self.build_lib, "gimmemotifs/included_tools")
            # package_data={'gimmemotifs.data':['data/cfg/*']},

            self.copy_tree("src/", src_dir)
            # mkpath is a distutils helper to create directories
            self.mkpath(target_dir)

            compile_all(src_dir)

            for exes in MOTIF_BINS.values():
                for exe in exes:
                    if os.path.exists(exe):
                        self.copy_file(exe, target_dir)
                    exe = os.path.join(self.build_base, exe)
                    if os.path.exists(exe):
                        self.copy_file(exe, target_dir)

            self.copy_tree(
                os.path.join(src_dir, "ChIPMunk"), os.path.join(target_dir, "ChIPMunk")
            )
            self.copy_tree(
                os.path.join(src_dir, "HMS"), os.path.join(target_dir, "HMS")
            )

            arch = "x86_64"
            try:
                sp.check_call(f"src/MotifSampler/MotifSampler_{arch}")
            except (sp.CalledProcessError, OSError) as e:
                if "died" in str(e):
                    arch = "i386"

            self.copy_file(
                os.path.join(src_dir, f"MotifSampler/MotifSampler_{arch}"),
                os.path.join(target_dir, "MotifSampler"),
            )
            self.copy_file(
                os.path.join(src_dir, f"MotifSampler/CreateBackgroundModel_{arch}"),
                os.path.join(target_dir, "CreateBackgroundModel"),
            )
            self.copy_file(
                os.path.join(src_dir, "Improbizer/ameme_x86_64"),
                os.path.join(target_dir, "ameme"),
            )

        my_build_py.run(self)

cmdclass["build_py"] = build_tools
version = versioneer.get_version()

setup(
    name="gimmemotifs",
    version=version,
    long_description=long_description,
    long_description_content_type="text/markdown",
    description=DESCRIPTION,
    author="Simon van Heeringen",
    author_email="simon.vanheeringen@gmail.com",
    url="https://github.com/vanheeringen-lab/gimmemotifs/",
    download_url="https://github.com/vanheeringen-lab/gimmemotifs/tarball/" + version,
    license="MIT",
    packages=find_packages(),
    scripts=["scripts/gimme", "scripts/combine_peaks", "scripts/coverage_table"],
    package_data={"gimmemotifs.data": ["data/"]},
    include_package_data=True,
    ext_modules=[module1],
    cmdclass=cmdclass,
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: MIT License",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python :: 3",
        "Topic :: Scientific/Engineering :: Bio-Informatics",
    ],
    install_requires=[
        "setuptools >=0.7",
        # copied from the requirements.yaml
        "biofluff >=3.0.4",
        "configparser",
        "diskcache",
        "feather-format",
        "genomepy >=0.13.0",
        # "ipywidgets",
        "iteround",
        "jinja2",
        "logomaker",
        "loguru",
        "matplotlib >=3.3",  # matplotlib-base
        "numpy >=1.18",
        "pandas >=1.3.0",
        "pybedtools >=0.9.0",
        "pysam >=0.16",
        # "python",
        "xxhash",  # "python-xxhash"
        "qnorm  >=0.8.1",
        "scikit-learn >=0.23.2",
        "scipy >=1.5",
        "seaborn >=0.10.1",
        "statsmodels",
        "tqdm >=4.46.1",
        "xdg",
        # "xgboost >=1.0.2",
    ],
    extras_require={
        "ete3": ["ete3"],
        "notebook": ["ipywidgets"],
        "xgboost": ["xgboost >=1.0.2"],
    },
)


import pygame
from ball import Ball
from random import randint

pygame.mixer.pre_init(44100, -16, 1, 512) # важно прописать до pygame.init()
pygame.init()
pygame.time.set_timer(pygame.USEREVENT, 2000)

pygame.mixer.music.load('sounds/bird.mp3')
pygame.mixer.music.play(-1)

s_catch = pygame.mixer.Sound('sounds/catch.ogg')

BLACK = (0, 0, 0)
W, H = 1000, 570

ground = H - 70           # уровень земли на которой находится герой
jump_force = 20         # сила прыжка
move = jump_force + 1

sc = pygame.display.set_mode((W, H))

clock = pygame.time.Clock()
FPS = 60

score = pygame.image.load('images/score_fon.png').convert_alpha()
f = pygame.font.SysFont('arial', 30)

telega = pygame.image.load('images/kozak_min-removebg-preview.png').convert_alpha()
kozak_left = pygame.image.load('images/kozak_min-removebg-preview_left.png').convert_alpha()
t_rect = telega.get_rect(centerx=W//2, bottom=H-5)
pygame.display.set_caption("ukranian kozak")

balls_data = ({'path': 'ball_bear.png', 'score': 100},
              {'path': 'ball_fox.png', 'score': 150},
              {'path': 'ball_puppy.png', 'score': 200},
              {'path': 'ball_pantera.png', 'score': 350},
              {'path': 'ball_tiger.png', 'score': 400})

balls_surf = [pygame.image.load('images/'+data['path']).convert_alpha() for data in balls_data]

def createBall(group):
    indx = randint(0, len(balls_surf)-1)
    x = randint(20, W-20)
    speed = randint(1, 4)

    return Ball(x, speed, balls_surf[indx], balls_data[indx]['score'], group)

game_score = 0

def collideBalls():
    global game_score
    for ball in balls:
        if t_rect.collidepoint(ball.rect.center):
            s_catch.play()
            game_score += ball.score
            ball.kill()

balls = pygame.sprite.Group()

bg = pygame.image.load('images/back1.jpg').convert()

kozak = telega
speed = 10
createBall(balls)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.USEREVENT:
            createBall(balls)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        kozak = kozak_left
        t_rect.x -= speed
        if t_rect.x < 0:
            t_rect.x = 0
    elif keys[pygame.K_RIGHT]:
        kozak = telega
        t_rect.x += speed
        if t_rect.x > W-t_rect.width:
            t_rect.x = W-t_rect.width

    collideBalls()

    sc.blit(bg, (0, 0))
    sc.blit(score, (0, 0))
    sc_text = f.render(str(game_score), 1, (94, 138, 14))
    sc.blit(sc_text, (20, 10))

    balls.draw(sc)
    sc.blit(kozak, t_rect)
    pygame.display.update()

    clock.tick(FPS)

    balls.update(H)


import json
import ast
from amadeus import Client, ResponseError, Location
from django.shortcuts import render
from django.contrib import messages
from .flight import Flight
from .booking import Booking
from django.http import HttpResponse

amadeus = Client()

def demo(request):
    # Retrieve data from the UI form
    origin = request.POST.get("Origin")
    destination = request.POST.get("Destination")
    departure_date = request.POST.get("Departuredate")
    return_date = request.POST.get("Returndate")

    # Prepare url parameters for search
    kwargs = {
        "originLocationCode": origin,
        "destinationLocationCode": destination,
        "departureDate": departure_date,
        "adults": 1,
    }

    # For a round trip, we use AI Trip Purpose Prediction
    # to predict if it is a leisure or business trip
    tripPurpose = ""
    if return_date:
        # Adds the parameter returnDate for the Flight Offers Search API call
        kwargs["returnDate"] = return_date
        kwargs_trip_purpose = {
            "originLocationCode": origin,
            "destinationLocationCode": destination,
            "departureDate": departure_date,
            "returnDate": return_date,
        }
        try:
            # Calls Trip Purpose Prediction API
            trip_purpose_response = amadeus.travel.predictions.trip_purpose.get(
                **kwargs_trip_purpose
            ).data
            tripPurpose = trip_purpose_response["result"]
        except ResponseError as error:
            messages.add_message(
                request, messages.ERROR, error.response.result["errors"][0]["detail"]
            )
            return render(request, "demo/home.html", {})

    # Perform flight search based on previous inputs
    if origin and destination and departure_date:
        try:
            search_flights = amadeus.shopping.flight_offers_search.get(**kwargs)
        except ResponseError as error:
            messages.add_message(
                request, messages.ERROR, error.response.result["errors"][0]["detail"]
            )
            return render(request, "demo/home.html", {})
        try:
            country = search_flights.result['dictionaries'].get('locations').get(destination).get('countryCode')
            # Get the travel restrictions for the destination
            travel_restrictions = amadeus.duty_of_care.diseases.covid19_report.get(countryCode=country)
            documents = travel_restrictions.data['areaAccessRestriction']['declarationDocuments']['text']
            covid_tests = ''
            if 'text' in travel_restrictions.data['areaAccessRestriction']['travelTest']:
                covid_tests = travel_restrictions.data['areaAccessRestriction']['travelTest']['text']
            else:
                covid_tests = travel_restrictions.data['areaAccessRestriction']['travelTest']['travelTestConditionsAndRules'][0]['scenarios'][0]['condition']['textualScenario']
        except (ResponseError, KeyError, AttributeError) as error:
            messages.add_message(
                request, messages.ERROR, 'No results found'
            )
            return render(request, "demo/home.html", {})
        search_flights_returned = []
        response = ""
        for flight in search_flights.data:
            offer = Flight(flight).construct_flights()
            search_flights_returned.append(offer)
            response = zip(search_flights_returned, search_flights.data)

        return render(
            request,
            "demo/results.html",
            {
                "response": response,
                "origin": origin,
                "destination": destination,
                "departureDate": departure_date,
                "returnDate": return_date,
                "tripPurpose": tripPurpose,
                "country": country,
                "documents": documents,
                "covid_tests": covid_tests
            },
        )
    return render(request, "demo/home.html", {})

def book_flight(request, flight):
    # Create a fake traveler profile for booking
    traveler = {
        "id": "1",
        "dateOfBirth": "1982-01-16",
        "name": {"firstName": "JORGE", "lastName": "GONZALES"},
        "gender": "MALE",
        "contact": {
            "emailAddress": "jorge.gonzales833@telefonica.es",
            "phones": [
                {
                    "deviceType": "MOBILE",
                    "countryCallingCode": "34",
                    "number": "480080076",
                }
            ],
        },
        "documents": [
            {
                "documentType": "PASSPORT",
                "birthPlace": "Madrid",
                "issuanceLocation": "Madrid",
                "issuanceDate": "2015-04-14",
                "number": "00000000",
                "expiryDate": "2025-04-14",
                "issuanceCountry": "ES",
                "validityCountry": "ES",
                "nationality": "ES",
                "holder": True,
            }
        ],
    }
    # Use Flight Offers Price to confirm price and availability
    try:
        flight_price_confirmed = amadeus.shopping.flight_offers.pricing.post(
            ast.literal_eval(flight)
        ).data["flightOffers"]
    except (ResponseError, KeyError, AttributeError) as error:
        messages.add_message(request, messages.ERROR, error.response.body)
        return render(request, "demo/book_flight.html", {})

    # Use Flight Create Orders to perform the booking
    try:
        order = amadeus.booking.flight_orders.post(
            flight_price_confirmed, traveler
        ).data
    except (ResponseError, KeyError, AttributeError) as error:
        messages.add_message(
            request, messages.ERROR, error.response.result["errors"][0]["detail"]
        )
        return render(request, "demo/book_flight.html", {})

    passenger_name_record = []
    booking = Booking(order).construct_booking()
    passenger_name_record.append(booking)

    return render(request, "demo/book_flight.html", {"response": passenger_name_record})

def origin_airport_search(request):
    if request.is_ajax():
        try:
            data = amadeus.reference_data.locations.get(
                keyword=request.GET.get("term", None), subType=Location.ANY
            ).data
        except (ResponseError, KeyError, AttributeError) as error:
            messages.add_message(
                request, messages.ERROR, error.response.result["errors"][0]["detail"]
            )
    return HttpResponse(get_city_airport_list(data), "application/json")

def destination_airport_search(request):
    if request.is_ajax():
        try:
            data = amadeus.reference_data.locations.get(
                keyword=request.GET.get("term", None), subType=Location.ANY
            ).data
        except (ResponseError, KeyError, AttributeError) as error:
            messages.add_message(
                request, messages.ERROR, error.response.result["errors"][0]["detail"]
            )
    return HttpResponse(get_city_airport_list(data), "application/json")

def get_city_airport_list(data):
    result = []
    for i, val in enumerate(data):
        result.append(data[i]["iataCode"] + ", " + data[i]["name"])
    result = list(dict.fromkeys(result))
    return json.dumps(result)


# For data wrangling
import pandas as pd
import numpy as np

# For visualization
import matplotlib.pyplot as plt
import seaborn as sns

# For data acquisition
from nba_api.stats.endpoints import playercareerstats, shotchartdetail
from nba_api.stats.static import players

# For plotting the court
from matplotlib import cm
from matplotlib.patches import Circle, Rectangle, Arc, ConnectionPatch
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import LinearSegmentedColormap, ListedColormap, BoundaryNorm
from matplotlib.path import Path
from matplotlib.patches import PathPatch

# Setting the workspace
sns.set_style('white')
sns.set_color_codes()
pd.options.display.max_columns = None

def draw_court(ax=None, color="blue", lw=1, shotzone=False, outer_lines=False):
    """Returns an axes with a basketball court drawn onto to it.
    This function draws a court based on the x and y-axis values that the NBA
    stats API provides for the shot chart data.  For example the center of the
    hoop is located at the (0,0) coordinate.  Twenty-two feet from the left of
    the center of the hoop in is represented by the (-220,0) coordinates.
    So one foot equals +/-10 units on the x and y-axis.
    Parameters
    ----------
    ax : Axes, optional
        The Axes object to plot the court onto.
    color : matplotlib color, optional
        The color of the court lines.
    lw : float, optional
        The linewidth the of the court lines.
    outer_lines : boolean, optional
        If `True` it draws the out of bound lines in same style as the rest of
        the court.
    Returns
    -------
    ax : Axes
        The Axes object with the court on it.
    """
    if ax is None:
        ax = plt.gca()

    # Create the various parts of an NBA basketball court

    # Create the basketball hoop
    hoop = Circle((0, 0), radius=7.5, linewidth=lw, color=color, fill=False)

    # Create backboard
    backboard = Rectangle((-30, -12.5), 60, 0, linewidth=lw, color=color)

    # The paint
    # Create the outer box 0f the paint, width=16ft, height=19ft
    outer_box = Rectangle((-80, -47.5), 160, 190, linewidth=lw, color=color,
                          fill=False)
    # Create the inner box of the paint, widt=12ft, height=19ft
    inner_box = Rectangle((-60, -47.5), 120, 190, linewidth=lw, color=color,
                          fill=False)

    # Create free throw top arc
    top_free_throw = Arc((0, 142.5), 120, 120, theta1=0, theta2=180,
                         linewidth=lw, color=color, fill=False)
    # Create free throw bottom arc
    bottom_free_throw = Arc((0, 142.5), 120, 120, theta1=180, theta2=0,
                            linewidth=lw, color=color, linestyle='dashed')
    # Restricted Zone, it is an arc with 4ft radius from center of the hoop
    restricted = Arc((0, 0), 80, 80, theta1=0, theta2=180, linewidth=lw,
                     color=color)

    # Three point line
    # Create the right side 3pt lines, it's 14ft long before it arcs
    corner_three_a = Rectangle((-220, -47.5), 0, 140, linewidth=lw,
                               color=color)
    # Create the right side 3pt lines, it's 14ft long before it arcs
    corner_three_b = Rectangle((220, -47.5), 0, 140, linewidth=lw, color=color)
    # 3pt arc - center of arc will be the hoop, arc is 23'9" away from hoop
    three_arc = Arc((0, 0), 475, 475, theta1=22, theta2=158, linewidth=lw,
                    color=color)

    # Center Court
    center_outer_arc = Arc((0, 422.5), 120, 120, theta1=180, theta2=0,
                           linewidth=lw, color=color)
    center_inner_arc = Arc((0, 422.5), 40, 40, theta1=180, theta2=0,
                           linewidth=lw, color=color)

    # Draw shotzone Lines
    # Based on Advanced Zone Mode
    if (shotzone == True):
        inner_circle = Circle((0, 0), radius=80, linewidth=lw, color='black', fill=False)
        outer_circle = Circle((0, 0), radius=160, linewidth=lw, color='black', fill=False)
        corner_three_a_x =  Rectangle((-250, 92.5), 30, 0, linewidth=lw, color=color)
        corner_three_b_x = Rectangle((220, 92.5), 30, 0, linewidth=lw, color=color)

        # 60 degrees
        inner_line_1 = Rectangle((40, 69.28), 80, 0, 60, linewidth=lw, color=color)
        # 120 degrees
        inner_line_2 = Rectangle((-40, 69.28), 80, 0, 120, linewidth=lw, color=color)

        # Assume x distance is also 40 for the endpoint
        inner_line_3 = Rectangle((53.20, 150.89), 290, 0, 70.53, linewidth=lw, color=color)
        inner_line_4 = Rectangle((-53.20, 150.89), 290, 0, 109.47, linewidth=lw, color=color)

        # Assume y distance is also 92.5 for the endpoint
        inner_line_5 = Rectangle((130.54, 92.5), 80, 0, 35.32, linewidth=lw, color=color)
        inner_line_6 = Rectangle((-130.54, 92.5), 80, 0, 144.68, linewidth=lw, color=color)

        # List of the court elements to be plotted onto the axes
        court_elements = [hoop, backboard, outer_box, inner_box, top_free_throw,
                          bottom_free_throw, restricted, corner_three_a,
                          corner_three_b, three_arc, center_outer_arc,
                          center_inner_arc, inner_circle, outer_circle,
                          corner_three_a_x, corner_three_b_x,
                          inner_line_1, inner_line_2, inner_line_3, inner_line_4, inner_line_5, inner_line_6]
    else:
        # List of the court elements to be plotted onto the axes
        court_elements = [hoop, backboard, outer_box, inner_box, top_free_throw,
                          bottom_free_throw, restricted, corner_three_a,
                          corner_three_b, three_arc, center_outer_arc,
                          center_inner_arc]

    if outer_lines:
        # Draw the half court line, baseline and side out bound lines
        outer_lines = Rectangle((-250, -47.5), 500, 470, linewidth=lw,
                                color=color, fill=False)
        court_elements.append(outer_lines)

    # Add the court elements onto the axes
    for element in court_elements:
        ax.add_patch(element)

    return ax

def get_player_shotchartdetail(player_name, season_id):
    """
    Parameters
    ----------
    player_name: name of the player with Capital
    season_id: ex. 2012-13
    """

    # player dictionary
    nba_players = players.get_players()
    player_dict = [player for player in nba_players if player['full_name'] == player_name][0]

    # career df
    career = playercareerstats.PlayerCareerStats(player_id=player_dict['id'])
    career_df = career.get_data_frames()[0]

    # team id during the season
    team_id = career_df[career_df['SEASON_ID'] == season_id]['TEAM_ID']

    # shotchardtdetail endpoint
    shotchartlist = shotchartdetail.ShotChartDetail(team_id=int(team_id),
                                                   player_id=int(player_dict['id']),
                                                   season_type_all_star='Regular Season',
                                                   season_nullable=season_id,
                                                   context_measure_simple="FGA").get_data_frames()

    return shotchartlist[0], shotchartlist[1]

def shot_chart(data, title="", color="b",
               xlim=(-250, 250), ylim=(422.5, -47.5), line_color="black",
               court_color="white", court_lw=2, outer_lines=False,
               flip_court=False, gridsize=None,
               ax=None, despine=False, **kwargs):

    if ax is None:
        ax = plt.gca()

    if not flip_court:
        ax.set_xlim(xlim)
        ax.set_ylim(ylim)
    else:
        ax.set_xlim(xlim[::-1])
        ax.set_ylim(ylim[::-1])

    ax.tick_params(labelbottom="off", labelleft="off")
    ax.set_title(title, fontsize=18)

    # draws the court
    draw_court(ax, color=line_color, lw=court_lw, outer_lines=outer_lines)

    # separate color by make or miss
    x_missed = data[data['EVENT_TYPE'] == 'Missed Shot']['LOC_X']
    y_missed = data[data['EVENT_TYPE'] == 'Missed Shot']['LOC_Y']

    x_made = data[data['EVENT_TYPE'] == 'Made Shot']['LOC_X']
    y_made = data[data['EVENT_TYPE'] == 'Made Shot']['LOC_Y']

    # plot missed shots
    ax.scatter(x_missed, y_missed, c='r', s=80, **kwargs, alpha=0.8)
    # plot made shots
    ax.scatter(x_made, y_made, facecolors='none', c='g', s=80, **kwargs, alpha=0.8)

    # Set the spines to match the rest of court lines, makes outer_lines
    # somewhate unnecessary
    for spine in ax.spines:
        ax.spines[spine].set_lw(court_lw)
        ax.spines[spine].set_color(line_color)

    if despine:
        ax.spines["top"].set_visible(False)
        ax.spines["bottom"].set_visible(False)
        ax.spines["right"].set_visible(False)
        ax.spines["left"].set_visible(False)

    return ax


import os
import json

class _JSONFile:
    def __init__(self, file):
        self.file = file

    def open(self):
        try:
            with open(self.file, mode='r', encoding='utf-8') as f:
                return json.load(f)
        except FileNotFoundError as e:
            print("Creating file...")
            os.mknod(self.file)
            return {}
        except json.decoder.JSONDecodeError as e:
            print("No key exists")
            return {}
        except Exception as e:
            return e

class JSONParser(_JSONFile):
    def insert(self, context=None):
        if not context:
            return None

        file = self.open()

        for k, v in context.items():
            file[k] = v

        with open(self.file, mode='w') as f:
            json.dump(file, f, indent=4)

        print("Item/s inserted")
        return context

    def delete(self, context=None):
        if not context:
            return None

        file = self.open()

        for k, v in context.items():
            try:
                del file[k]
            except KeyError:
                print("Key doesn't exists")
                return {k: v}

        with open(self.file, mode='w') as f:
            json.dump(file, f, indent=4)

        print("Item/s deleted")
        return context

class JSONReader(_JSONFile):
    def read_all(self):
        return self.open()

    def read_line(self, key):
        try:
            file = self.open()
            return file[str(key)]
        except KeyError as e:
            print("Key does not exists")
            return


#%%
import os
import shutil
import tqdm
import argparse

parser = argparse.ArgumentParser(description='Copy files with progress bar')
parser.add_argument('source', type=str, help='Source folder')
parser.add_argument('destination', type=str, help='Destination folder')
args = parser.parse_args()

source_dir = args.source
destination_dir = args.destination
files_to_copy = ['test', 'filter.csv', 'sc_items.csv', 'test.csv', 'test_raw.csv', 'train.csv']

total_files = len(files_to_copy)

# 創建一個主進度條，顯示複製了多少個文件或文件夾
main_pbar = tqdm.tqdm(files_to_copy, desc="Files", total=total_files)

for item in main_pbar:
    src_path = os.path.join(source_dir, item)
    dst_path = os.path.join(destination_dir, item)
    if os.path.isfile(src_path):
        # 創建一個子進度條，顯示單個文件的複製情況
        file_size = os.path.getsize(src_path)
        sub_pbar = tqdm.tqdm(total=file_size, desc=item, leave=False)
        # 使用shutil.copy函數來複製文件，並在每次寫入後更新子進度條的值
        with open(src_path, "rb") as src_file:
            with open(dst_path, "wb") as dst_file:
                while True:
                    data = src_file.read(1024) # 每次讀取1024字節
                    if not data: # 如果沒有數據了，跳出循環
                        break
                    dst_file.write(data) # 將數據寫入目標文件
                    sub_pbar.update(len(data)) # 更新子進度條的值

        sub_pbar.close()
    elif os.path.isdir(src_path):
        # 複製整個文件夾，不顯示子進度條
        shutil.copytree(src_path, dst_path)

main_pbar.close()


import argparse
import os
import sys
import time

import github
import requests
from requests.packages import urllib3
import yaml

# Turn of warnings about bad SSL config.
# https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
urllib3.disable_warnings()

DESC_SUFFIX = ' Mirror of code maintained at opendev.org.'
ARCHIVE_ORG = 'openstack-archive'
DEFAULT_TEAM_ID = 73197
PR_CLOSING_TEXT = (
    "Thank you for your contribution!\n\n"
    "This GitHub repository is just a mirror of %(url)s, where development "
    "really happens. Pull requests proposed on GitHub are automatically "
    "closed.\n\n"
    "If you are interested in pushing this code upstream, please note "
    "that OpenStack development uses Gerrit for change proposal and code "
    "review.\n\n"
    "If you have never contributed to OpenStack before, please see:\n"
    "https://docs.openstack.org/contributors/code-and-documentation/"
    "quick-start.html"
    "\n\nFeel free to reach out to the First Contact SIG by sending an "
    "email to the openstack-discuss list with the tag '[First Contact]' "
    "in the subject line. To email the mailing list, you must first "
    "subscribe which can be done here:\n"
    "http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-discuss"
)

def homepage(repo):
    return 'https://opendev.org/' + repo

def reponame(org, fullname):
    owner, name = fullname.split('/')
    if org.login != owner:
        raise ValueError(f'{fullname} does not match target org ({org.login})')
    return name

def load_from_project_config(project_config, org='openstack'):
    print('Loading project config...')
    pc = {}
    proj_yaml_filename = os.path.join(project_config, 'gerrit/projects.yaml')
    with open(proj_yaml_filename, 'r') as proj_yaml:
        for project in yaml.safe_load(proj_yaml):
            if project['project'].startswith(org + '/'):
                desc = project.get('description', '')
                if desc and desc[-1:] != '.':
                    desc += '.'
                desc += DESC_SUFFIX
                pc[project['project']] = desc
    print(f'\rLoaded {len(pc)} project descriptions from configuration.')
    return pc

def list_repos_in_zuul(project_config, tenant='openstack', org='openstack'):
    print('Loading Zuul repos...')
    repos = set()
    main_yaml_filename = os.path.join(project_config, 'zuul/main.yaml')
    with open(main_yaml_filename, 'r') as main_yaml:
        for t in yaml.safe_load(main_yaml):
            if 'tenant' in t and t['tenant']['name'] == tenant:
                for ps, pt in t['tenant']['source']['gerrit'].items():
                    for elem in pt:
                        if type(elem) is dict:
                            for k, v in elem.items():
                                if k.startswith(org + '/'):
                                    repos.add(k)
                        else:
                            if elem.startswith(org + '/'):
                                repos.add(elem)
    print(f'Loaded {len(repos)} repositories from Gerrit.')
    return repos

def list_repos_in_governance(governance, org='openstack'):
    print('Loading governance repos...')
    repos = set()

    proj_yaml_filename = os.path.join(governance, 'reference/projects.yaml')
    with open(proj_yaml_filename, 'r') as proj_yaml:
        for pname, project in yaml.safe_load(proj_yaml).items():
            for dname, deliv in project.get('deliverables', dict()).items():
                for r in deliv['repos']:
                    if r.startswith(org + '/'):
                        repos.add(r)

    extrafiles = ['sigs-repos.yaml',
                  'technical-committee-repos.yaml',
                  'user-committee-repos.yaml']
    for extrafile in extrafiles:
        yaml_filename = os.path.join(governance, 'reference', extrafile)
        if not os.path.exists(yaml_filename):
            print(f'Skipping {extrafile} as it no longer exists')
            continue
        with open(yaml_filename, 'r') as extra_yaml:
            for pname, project in yaml.safe_load(extra_yaml).items():
                for r in project:
                    if r['repo'].startswith(org + '/'):
                        repos.add(r['repo'])
    print(f'Loaded {len(repos)} repositories from governance.')
    return repos

def load_github_repositories(org):
    print('Loading GitHub repository list from GitHub...')
    gh_repos = {}
    for repo in org.get_repos():
        gh_repos[repo.full_name] = repo
    print(f'Loaded {len(gh_repos)} repositories from GitHub.     ')
    return gh_repos

def transfer_repository(repofullname, newowner=ARCHIVE_ORG):
    # Transfer repository is not yet supported by PyGitHub, so call directly
    token = os.environ['GITHUB_TOKEN']
    url = f'https://api.github.com/repos/{repofullname}/transfer'
    res = requests.post(url,
                        headers={'Authorization': f'token {token}'},
                        json={'new_owner': newowner})
    if res.status_code != 202:
        raise github.GithubException(res.status_code, res.text)

def archive_repository(archive_org, shortname):
    repository = archive_org.get_repo(shortname)
    repository.edit(archived=True)

def create_repository(org, repofullname, desc, homepage):
    name = reponame(org, repofullname)
    org.create_repo(name=name,
                    description=desc,
                    homepage=homepage,
                    has_issues=False,
                    has_projects=False,
                    has_wiki=False,
                    team_id=DEFAULT_TEAM_ID)

def update_repository(org, repofullname, desc, homepage):
    name = reponame(org, repofullname)
    repository = org.get_repo(name)
    repository.edit(description=desc, homepage=homepage)

def close_pull_request(org, repofullname, github_pr):
    github_pr.create_issue_comment(
        PR_CLOSING_TEXT % {'url': homepage(repofullname)})
    github_pr.edit(state="closed")

def main(args=sys.argv[1:]):
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'project_config',
        help='directory containing the project-config repository')
    parser.add_argument(
        'governance',
        help='directory containing the governance repository')
    parser.add_argument(
        '--dryrun',
        default=False,
        help='do not actually do anything',
        action='store_true')
    args = parser.parse_args(args)

    try:
        gh = github.Github(os.environ['GITHUB_TOKEN'])
        org = gh.get_organization('openstack')
        archive_org = gh.get_organization(ARCHIVE_ORG)
    except KeyError:
        print('Aborting: missing GITHUB_TOKEN environment variable')
        sys.exit(1)

    if args.dryrun:
        print('Running in dry run mode, no action will be actually taken')

    # Load data from Gerrit and GitHub
    gerrit_descriptions = load_from_project_config(args.project_config)
    in_governance = list_repos_in_governance(args.governance)
    in_zuul = list_repos_in_zuul(args.project_config)

    if in_governance != in_zuul:
        print("\nWarning, projects defined in Zuul do not match governance:")

        print("\nIn Governance but not in Zuul (should be cleaned up):")
        for repo in in_governance.difference(in_zuul):
            print(repo)

        print("\nIn Zuul but not in Governance (retirement in progress?):")
        for repo in in_zuul.difference(in_governance):
            print(repo)
        print()

    github_repos = load_github_repositories(org)
    in_github = set(github_repos.keys())

    print("\nUpdating repository descriptions:")
    for repo in in_github.intersection(in_zuul):
        if ((github_repos[repo].description != gerrit_descriptions[repo])
           or (github_repos[repo].homepage != homepage(repo))):
            print(repo, end=' - ', flush=True)
            if args.dryrun:
                print('nothing done (--dryrun)')
            else:
                update_repository(org, repo,
                                  gerrit_descriptions[repo],
                                  homepage(repo))
                print('description updated')
                time.sleep(1)

    print("\nArchiving repositories that are in GitHub but not in Zuul:")
    for repo in in_github.difference(in_zuul):
        print(repo, end=' - ', flush=True)
        if args.dryrun:
            print('nothing done (--dryrun)')
        else:
            transfer_repository(repo)
            print(f'moved to {ARCHIVE_ORG}', end=' - ', flush=True)
            time.sleep(10)
            archive_repository(archive_org, reponame(org, repo))
            print('archived')
            time.sleep(1)

    print("\nCreating repos that are in Zuul & governance but not in Github:")
    for repo in (in_zuul.intersection(in_governance)).difference(in_github):
        print(repo, end=' - ', flush=True)
        if args.dryrun:
            print('nothing done (--dryrun)')
        else:
            create_repository(org, repo,
                              gerrit_descriptions[repo],
                              homepage(repo))
            print('created')
            time.sleep(1)

    print("\nIterating through all Github repositories to close PRs:")
    for repo, gh_repository in github_repos.items():
        for req in gh_repository.get_pulls("open"):
            print(f'Closing PR#{req.number} in {repo}', end=' - ', flush=True)
            if args.dryrun:
                print('nothing done (--dryrun)')
            else:
                close_pull_request(org, repo, req)
                print('closed')
                time.sleep(1)
    print("Done.")

if __name__ == '__main__':
    main()


import pandas as pd
import sklearn.feature_extraction.text as sk
import retrieve_data
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

def model(trainData,testData):
    vectorizer = sk.TfidfVectorizer(max_features = 500, use_idf=True, stop_words='english')
    X = vectorizer.fit_transform(trainData["content"]).toarray()
    Y = list(trainData["type"])
    clf_svm = svm.SVC(C=1.0, kernel='rbf', class_weight='balanced', gamma=0.1, probability=True)
    clf_kNeighbors = KNeighborsClassifier(n_neighbors = 5, weights='distance')
    clf_MultiNB = MultinomialNB(alpha=0.1)
    ensemble = VotingClassifier(estimators=[('SVM', clf_svm), ('KNeighBors', clf_kNeighbors), ('MultiNB', clf_MultiNB)],weights=[1,1,1], voting='soft', n_jobs=-1)
    ensemble.fit(X,Y)
    #c_array= [0.01, 0.1, 1]

    # Best linear C=0.1
    # Best rbf C=1.0
    '''
    for c in c_array:
        clf_svm = svm.SVC(C=c,kernel='rbf', class_weight='balanced', gamma=0.1, probability=True)
        clf_svm.fit(X,Y)
        Y1 = clf_svm.predict(test_content)
        print("Number of test samples: {}. C-value={}".format(len(real_types),c))
        print("SVM Accuracy: {}".format(accuracy_score(real_types, Y1)))
        print("SVM classification report \n {}".format(classification_report(real_types,Y1)))
    '''
    test_content = vectorizer.transform(testData["content"]).toarray()
    real_types = list(testData["type"])

    Y7 = ensemble.predict(test_content)

    print("Ensemble Accuracy: {}".format(accuracy_score(real_types, Y7)))
    print("Ensemble classification report \n {}".format(classification_report(real_types,Y7)))

    kaggle_data = pd.read_csv('test_data.csv')
    content_data = vectorizer.transform(kaggle_data["article"].values.astype('U')).toarray()
    labels = ensemble.predict(content_data)

    real_counter = 0
    fake_counter = 0
    for i, y in enumerate(labels):
        if y == "reliable":
            labels[i] = "REAL"
            real_counter += 1
        if y == "NULL":
            labels[i] = "FAKE"
            fake_counter += 1
        if y == "fake":
            labels[i] = "FAKE"
            fake_counter += 1
    print("Reals in Kaggle pred: {}".format(real_counter))
    print("Fakes in Kaggle pred: {}".format(fake_counter))
    kaggle_data["label"] = labels
    id_with_predictions = kaggle_data.drop("article", axis = 1)
    id_with_predictions.to_csv("predictions.csv", index=False)

    liar_data = pd.read_csv('liar_test.tsv', delimiter='\t')
    liar_content = vectorizer.transform(liar_data['content'].values.astype('U')).toarray()
    true_labels = liar_data['type']
    for i, y in enumerate(true_labels):
        if y == "true":
            true_labels[i] = "reliable"
        if y == "mostly-true":
            true_labels[i] = "reliable"
        else:
            true_labels[i] = "fake"

    pred_labels = ensemble.predict(liar_content)

    print("Ensemble Accuracy (Liar-Liar): {}".format(accuracy_score(true_labels, pred_labels)))
    print("Ensemble classification report (Liar-Liar) \n {}".format(classification_report(true_labels,pred_labels)))


from tkinter import *

from PIL import ImageTk, Image

root = Tk()
root.title('Learn to Code at Codem.com')
# png file 16 * 16, 32 * 32, 64 * 64

#root.iconbitmap('c:/gui/codemy.ico')

my_img = ImageTk.PhotoImage(Image.open("gui/tree.jpg"))
my_label = Label(image=my_img)
my_label.pack()

button_quit = Button(root, text="Exit Program", command=root.quit)
button_quit.pack()

root.mainloop()


# utils.py

""" Proporciona funciones auxiliares para procesar el texto durante la generación de la grilla. """

from typing import List, Tuple
import math
import random
import re

from sachagrilla.utils.separasilabas import silabizer

def clean_text(text: str) -> str:
    """Toma un texto y lo devuelve en minúscula y sin signos de puntuación ni acentuación."""
    patterns = [u'[àáâãäå]', u'[èéêë]', u'[ìíîï]', u'[òóôõö]', u'[ùúûü]', u'[,.:;]']
    vowels = ['a', 'e', 'i', 'o', 'u', '']
    for idx, pattern in enumerate(patterns):
        text = re.sub(pattern, vowels[idx], text)
    return text.lower()

def cut_in_half(text: str) -> Tuple[str, str]:
    """Toma un texto, devuelve dos mitades sin espacios. Si la longitud es impar la primera mitad será la más larga."""
    clean_quote = clean_text(text).replace(' ', '').replace(' ', '')
    size = len(clean_quote)
    cut_point = size//2 if size % 2 == 0 else math.ceil(size/2)
    return clean_quote[:cut_point], clean_quote[cut_point:]

def get_random_word(words, pos1, pos2, letter1, letter2):
    """Devuelve una palabra aleatoria cuyas letras en las posiciones señaladas coincidan con las indicadas."""
    if letter2:
        eligible_words = [(idx, word) for idx, word in words.items()
                          if clean_text(word[pos1]) == letter1 and clean_text(word[pos2]) == letter2]
    else:
        eligible_words = [(idx, word) for idx, word in words.items() if clean_text(word[pos1]) == letter1]
    try:
        chosen_word = random.choice(eligible_words)
        return chosen_word
    except IndexError:
        return None, None

def get_all_syllables(words: List[str]) -> str:
    """Toma lista de palabras y devuelve un str con las sílabas de todas las palabras ord. alf. y separadas por coma."""
    syllabator = silabizer()
    syllables = []
    for word in words:
        syllables.extend(syllabator(word))
    syllables = sorted([str(sil) for sil in syllables])
    syllables_str = ', '.join(syllables)
    return syllables_str

if __name__ == '__main__':
    one = [3,3,3]
    two = [9,9, 9]
    for uno, dos in zip(one, two):
        print(uno, dos)
    # print(cut_in_half('seis treso'))


"""
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"
Note:

The length of both num1 and num2 is < 110.
Both num1 and num2 contain only digits 0-9.
Both num1 and num2 do not contain any leading zero, except the number 0 itself.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

 123
 456
"""
import collections
class Solution:
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        carry = 0
        ans_stack = collections.deque()
        rev1 = num1[::-1]
        rev2 = num2[::-1]
        # if len(num1) >= len(num2):
        #     rev1 = num1[::-1]
        #     rev2 = num2[::-1]
        # else:
        #     rev1 = num2[::-1]
        #     rev2 = num1[::-1]

        n1_multiplier = 1
        n1_ans = 0
        for i, n1 in enumerate(rev2):
            n2_multiplier = 1
            n2_ans = 0
            for j, n2 in enumerate(rev1):
                p = int(n1) * int(n2) + carry
                if p >= 10:
                    carry = int(p/10)
                else:
                    carry = 0
                n2_ans += (p%10) * n2_multiplier
                n2_multiplier *= 10
            n2_ans += carry * n2_multiplier
            carry = 0
            ans_stack.append(n2_ans)

        m = 1
        s = 0

        while ans_stack:
            s += ans_stack.popleft() * m
            m *= 10
        return str(s)

if __name__ == "__main__":
    s = Solution()
    print(s.multiply("99","9"))


"""
Manipulate a time series
========================
"""
# %%
# The objective here is to create and manipulate a time series.
# A time series is a particular field where the mesh :math:`\mathcal{M}` 1-d and regular, eg a time grid :math:`(t_0, \dots, t_{N-1})`.
#
# It is possible to draw a time series, using interpolation between the values: see the use case on the Field.
#
# A time series can be obtained as a realization of a multivariate stochastic process
# :math:`X: \Omega \times [0,T] \rightarrow \mathbb{R}^d` of dimension :math:`d` where :math:`[0,T]`
# is discretized according to the regular grid :math:`(t_0, \dots, t_{N-1})`.
# The  values :math:`(\underline{x}_0, \dots, \underline{x}_{N-1})` of the  time series are defined by:
#
# .. math::
#    \forall i \in [0, N-1],\quad \underline{x}_i= X(\omega)(t_i)
#
#
# A time series is stored in the **TimeSeries** object that stores the regular time grid and the associated values.

# %%
import openturns as ot
import openturns.viewer as viewer
from matplotlib import pylab as plt

ot.Log.Show(ot.Log.NONE)

# %%
# Create the RegularGrid
tMin = 0.0
timeStep = 0.1
N = 100
myTimeGrid = ot.RegularGrid(tMin, timeStep, N)

# %%
# Case 1: Create a time series from a time grid and values
# Care! The number of steps of the time grid must correspond to the size of the values
myValues = ot.Normal(3).getSample(myTimeGrid.getVertices().getSize())
myTimeSeries = ot.TimeSeries(myTimeGrid, myValues)
myTimeSeries

# %%
# Case 2: Get a time series from a Process
myProcess = ot.WhiteNoise(ot.Normal(3), myTimeGrid)
myTimeSeries2 = myProcess.getRealization()
myTimeSeries2

# %%
# Get the number of values of the time series
myTimeSeries.getSize()

# %%
# Get the dimension of the values observed at each time
myTimeSeries.getMesh().getDimension()

# %%
# Get the value Xi at index i
i = 37
print("Xi = ", myTimeSeries.getValueAtIndex(i))

# %%
# Get the time series at index i : Xi
i = 37
print("Xi = ", myTimeSeries[i])

# %%
# Get a the marginal value at index i of the time series
i = 37
# get the time stamp:
print("ti = ", myTimeSeries.getTimeGrid().getValue(i))
# get the first component of the corresponding value :
print("Xi1 = ", myTimeSeries[i, 0])

# %%
# Get all the values (X1, .., Xn) of the time series
myTimeSeries.getValues()

# %%
# Compute the temporal Mean
# It corresponds to the mean of the values of the time series
myTimeSeries.getInputMean()

# %%
# Draw the marginal i of the time series using linear interpolation
graph = myTimeSeries.drawMarginal(0)
view = viewer.View(graph)

# %%
# with no interpolation
graph = myTimeSeries.drawMarginal(0, False)
view = viewer.View(graph)
plt.show()


from webcam import Webcam
import cv2
from datetime import datetime

webcam = Webcam()
webcam.start()
i = 0
while True:

    # get image from webcam
    image = webcam.get_current_frame()

    # display image
    cv2.imshow('grid', image)
    cv2.waitKey(3000)

    # save image to file, if pattern found
    ret, corners = cv2.findChessboardCorners(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), (7, 6), None)
    print (ret)

    if ret == True:
        i += 1
        print (i)
        if i == 10:
            break
        filename = datetime.now().strftime('%Y%m%d_%Hh%Mm%Ss%f') + '.jpg'
        cv2.imwrite("sample_images/" + filename, image)