from rest_framework.views import exception_handler

def exceptionHandler(exc, context):
    handlers = {
        'ValidationError': handleGenericError,
        'Http404': handleGenericError,
        'PermissionDenied': handleGenericError,
        'NotAuthenticated': handleAuthenticationError,
    }

    response = exception_handler(exc, context)

    if response is not None:
        response.data['status_code'] = response.status_code

    exceptionClass = exc.__class__.__name__

    if exceptionClass in handlers:
        return handlers[exceptionClass](exc, context, response)

    return response

def handleAuthenticationError(exc, context, response):
    response.data = {
        'error': 'login to procced',
        'code': response.status_code
    }

    return response

def handleGenericError(exc, context, response):
    print("JAJAJA")
    return response


class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n <= 1:
            return n
        f = d = 1
        for i in xrange(1, n):
            if nums[i] > nums[i-1]:
                f = d + 1
            elif nums[i] < nums[i-1]:
                d = f + 1
        return max(d, f)


''' @file main3.py
    @brief Main file, which encoder, motor, and user task are being run.
    @details File imports the classes for each task and calls out the period at which they will be run.
            The tasks are created as objects and share objects containing information shared to operate motor.
            Additionally a motor driver is created to be called as a parameter in motor task.
            A while loop is created to constantly run through tasks at their frequency specified by period variables.
            The user can press cntrl+c as a keyboard interupt to stop the program

    @author Christian Clephan
    @author John Bennett
    @date October 16, 2021
'''
import motor
import task_motor
import task_encoder_v2
import task_user_v3
import shares

##  @brief Encoder task period (1.5 milliseconds)
T_encoder = 1.5
##  @brief Encoder task period (2 milliseconds)
T_motor = 2
##  @brief User task period (50 milliseconds)
T_user = 40

if __name__ =='__main__':

    ## @brief Motor1Share contains all data shared for motor 1
    # @details Shared initial values in order include Encoder number (int), Current Position (ticks), Current delta (ticks/period), duty(int), Zero (boolean), Fault(boolean)
    # [Encoder number (int), Current Position (ticks), Current delta (ticks/period), duty(int), Zero (boolean), Fault(boolean)]
    Motor1Share = shares.ShareMotorInfo([1,0,0,0,False,False])

    ## @brief Motor2Share contains all data shared for motor 2
    # @details Shared initial values in order include Encoder number (int), Current Position (ticks), Current delta (ticks/period), duty(int), Zero (boolean), Fault(boolean)
    Motor2Share = shares.ShareMotorInfo([2,0,0,0,False,False])

    ## @brief Creates motor driver object
    motor_drv = motor.DRV8847(3)

    ##  @brief Creating a variable for the encoder task in the Task_Encoder Class at period T_encoder
    encoderTask1 = task_encoder_v2.Task_Encoder(T_encoder,Motor1Share)
    ##  @brief Creating a variable for the encoder task in the Task_Encoder Class at period T_encoder
    encoderTask2 = task_encoder_v2.Task_Encoder(T_encoder,Motor2Share)
    ##  @brief Creating a variable for the motor task in the Task_Motor Class at period T_motor
    motorTask1 = task_motor.Task_Motor(T_motor,Motor1Share,motor_drv)
    ##  @brief Creating a variable for the motor task in the Task_Motor Class at period T_motor
    motorTask2 = task_motor.Task_Motor(T_motor,Motor2Share,motor_drv)
    ##  @brief Creating a variable for the user task in the Task_User Class at period T_user
    userTask = task_user_v3.Task_User(T_user,Motor1Share,Motor2Share)

    while (True):
        #Attempt to run FSM unless Ctrl+c is hit
        try:

            ##  @brief Tuple containing encoder position and delta from the encoder task to user task
            encoderTask1.run()
            encoderTask2.run()
            userTask.run()
            motorTask1.run()
            motorTask2.run()

        #If there is an interuption break
        except KeyboardInterrupt:
            break
    motor_drv.disable()
    print('Program Terminating')


import torch
import torch.nn as nn
import torch.nn.functional as F

from .projection_block import ProjectionBlock

class SimpleMultimodalEncoder(nn.Module):
    def __init__(
        self,
        text_embedding_sz,
        image_embedding_sz,
        joint_embedding_sz,
        dropout=0.1,
        l2_norm=True,
    ):
        """
        :param text_embedding_sz (int): Text embedding size
        :param image_embedding_sz (int): Image embedding size
        :param joint_embedding_sz (int): Joint embeddings size (Text + Image)
        :param dropout (float): dropout rate
        """
        super().__init__()

        self.l2_norm = l2_norm

        # initialize a projection block to fuse image and text embeddings
        self.projection = ProjectionBlock(
            text_embedding_sz + image_embedding_sz, joint_embedding_sz, dropout
        )

    def forward(self, text_embedding, img_embedding):
        """
        :param text_embedding (tensor): Text embeddings
        :param img_embedding (tensor): Image embeddings
        :return output (tensor): Multimodal embeddings
        """

        # concat text and image modalities
        output = torch.cat((text_embedding, img_embedding), 1)

        # fuse both modalities thought a projection block
        output = self.projection(output)

        # l2 normalization
        if self.l2_norm:
            output = F.normalize(output, p=2, dim=1)
        return output


from model.Parser import Parser, Database

if __name__ == "__main__":
    # Cau 1: Xây dựng bộ phân tích cú pháp --> model/Parser.py
    db = Database()
    db.init()
    db.close()
    fp = open("./input/query.csv", "r", encoding="utf-8")
    lines = fp.readlines()
    # Cau 2: Phân tích cú pháp và xuất ra các quan hệ của các thành phần của từng câu truy vấn.
    for index in range(len(lines)):
        parser = Parser(lines[index])
        # print(parser.text)
        parser.do_MaltParser()
        parser.AnalysisGrammarRelationTree(f"./output/output{index+1}.txt")


import gdal
import numpy

def  get_count_equel(tif_path,chislo,ker):
    raster=gdal.Open(tif_path)
    xsize=raster.RasterXSize
    ysize=raster.RasterYSize
    s=0
    #print(xsize*ysize)
    for i in range(0,xsize,ker):
        for j in range(0,ysize,ker):
            if i+ker>=xsize:
                ker_realx=ker-((i+ker)-xsize)
                #print(xsize,i+ker,ker_realx)
            else:
                ker_realx=ker
            if j+ker>=ysize:
                ker_realy=ker-((j+ker)-ysize)
            else:
                ker_realy=ker
            band=raster.GetRasterBand(1).ReadAsArray(i,j,ker_realx,ker_realy).astype(numpy.float32)
            #print(band.shape)
            s=s+numpy.sum(band==chislo)

    s=float(s)*900.0/1000000.0
    return s
print("Declining productivity [km squared]:")
print("Forest to grassland")
print(get_count_equel('/tmp/unzipped/main_degrad.tif',12,1000))
print("Forest to cropland")
print(get_count_equel('/tmp/unzipped/main_degrad.tif',13,1000))
print("Forest to other")
print(get_count_equel('/tmp/unzipped/main_degrad.tif',16,1000))

print("Moderate decline in productivity [km squared]:")
print("Forest to grassland")
print(get_count_equel('/tmp/unzipped/moderate_degrad.tif',12,1000))
print("Forest to cropland")
print(get_count_equel('/tmp/unzipped/moderate_degrad.tif',13,1000))
print("Forest to other")
print(get_count_equel('/tmp/unzipped/moderate_degrad.tif',16,1000))


from rest_framework import viewsets
from realestate.listing.models import Listing
from realestate.api.serializers import ListingSerializer

class PaginationMixin(object):
    paginate_by = 15
    paginate_by_param = 'limit'
    max_paginate_by = 100

class ListingViewSet(PaginationMixin, viewsets.ReadOnlyModelViewSet):
    model = Listing
    serializer_class = ListingSerializer
    queryset = Listing.objects.none()

    def get_queryset(self):
        queryset = Listing.objects.active()

        last_modified = self.request.query_params.get('modified_from')
        if last_modified is not None:
            queryset = queryset.filter(last_modified__gt=last_modified)

        return queryset


def possibilities(step, room):
    res = []
    if rooms[step + 1][room - 1] <= rooms[step][room]:
        res.append(-1)
    res.append(0)
    if rooms[step + 1][room + 1] <= rooms[step][room]:
        res.append(1)
    print(rooms[step + 1][room + 1], rooms[step + 1][room - 1])
    return(res)

rooms = [[10, 10, 10, 1, 10, 10, 10], [10, 10, 1, 2, 1, 10, 10], [10, 1, 1, 3, 2, 0, 10]]
# ееее, вырезаем контент, [10, 1, 2, 3, 2, -1, 10]]
room = 3
secret = False
for step in range(2):
    if rooms[step][room] == 0:
        secret = True
        print("Вы нашли комнату, оставленную разробтчиками")
    pos = possibilities(step, room)
    move = -2
    while move == -2:
        print("Выберите направление" + ('"влево"' if -1 in pos else "") + '"вперёд"' + ('" вправо"' if 1 in pos else "") + "?")
        a = input()
        move = -1 if a == "влево" else (0 if a == "вперёд" else (1 if a == "вправо" else -2))
    room += move
nn = " но вы не нашли секрет, попробуйте ещё раз и найдите его"
if secret:
    nn = ". А так же вы нашли секрет на своём пути"
sts = [0, "вы умерли, поздравляем"]
sts.append("в следущей комнате не было дверей, а дверь через которую вы вошли оказалась заперта. Поздравляем!!!")
sts.append("Ваша прямолинейность привела вас к смерти, поздравляем")
sts.append("Кто бы мог подумать, что открывание случайных дверей убьют вас. Но так произошло. Удивительно")
sts.append("Вы сделали что? Вы выжили? Интересно. Надо будеть закрыть эту дверь")
print(sts[room] + nn)


from fastapi import FastAPI
from server.routes import router as NoteRouter

app = FastAPI()

@app.get("/", tags=["Root"])
async def read_root():
    return {
        "message": "Hello world!"
    }

app.include_router(NoteRouter, prefix="/note")


from typing import Any
import jax
import jax.numpy as jnp
import numpy as np
from operator import itemgetter
import random

Array = Any

def make_equation(x, y, z):
  return (str(x), 'o', str(y), '=', str(z))

def all_operations_in_p(operation: str, prime: int) -> tuple[str, str]:
  all_arithmetic_operations = {
      "x+y": lambda x, y, prime: make_equation(x, y, (x + y) % prime),
      "x-y": lambda x, y, prime: make_equation(x, y, (x - y) % prime),
      # to account for 0 division, we set y to 1 if 0.
      "x/y": lambda x, y, prime: make_equation(x, y, (x / max(y, 1)) % prime),
      "x^2+y^2": lambda x, y, prime: make_equation(x, y, (x**2 + y**2) % prime),
  }

  xs, ys = sorted(set(range(prime))), sorted(set(range(prime)))
  xs, ys = [i.reshape(-1) for i in np.meshgrid(xs, ys)]
  if operation not in all_arithmetic_operations:
    raise ValueError(f"Operation {operation} not supported")
  ops = [all_arithmetic_operations[operation](x, y, prime) for x, y in
         zip(xs, ys)]
  random.seed(42)
  random.shuffle(ops)
  return ops

class DataGenerator:
  def __init__(self, elements: Array) -> None:
    self._elements = elements
    self._sequence_length = 5
    self._vocab = sorted(set([i for e in elements for i in e]))
    self._vocab2idx = {c: i for i, c in enumerate(self._vocab)}
    self._idx2vocab = {i: c for c, i in self._vocab2idx.items()}

    self._encoded_elements = self.encode(elements)

  @ property
  def vocab_size(self) -> int:
    return len(self._vocab)

  @ property
  def sequence_length(self) -> int:
    return self._sequence_length

  def encode(self, inputs: Array) -> jax.Array:
    def encode_str(s: str) -> jax.Array:
      tokens = [self._vocab2idx[c] for c in s]
      tokens_length = len(tokens)
      if tokens_length != self._sequence_length:
        raise ValueError(
            f"Input length: is wrong : {tokens_length} expected: {self._sequence_length}")
      return tokens

    return [encode_str(single_input) for single_input in inputs]

  def decode(self, batch_inputs: Array) -> np.ndarray:
    def decode_inputs(inputs):
      decoded = []
      for elem in inputs.tolist():
        decoded.append(self._idx2vocab[elem])
      return decoded

    return list(map(decode_inputs, batch_inputs))

  def get_batch(self, rng, batch_size) -> tuple[jax.Array, jax.Array]:
    perm = jax.random.permutation(
        rng, max(batch_size, len(self._elements))).astype(
        jnp.int32)[:batch_size]
    perm = jax.numpy.mod(perm, len(self._elements))
    batch = jnp.asarray(itemgetter(
      *perm)(self._encoded_elements)).reshape(batch_size, -1)
    return {'x': batch[:, :-1], 'y': batch[:, -1:].reshape(-1)}

def input_pipeline(operation: str, prime: int, train_fraction: float) -> tuple[
        DataGenerator, DataGenerator]:
  elements = all_operations_in_p(operation, prime)
  train_set = elements[: int(len(elements) * train_fraction)]
  eval_set = elements[int(len(elements) * train_fraction):]

  return DataGenerator(train_set), DataGenerator(eval_set)


import numpy
start_text = """Juan 23 4500
María 12 800
Pedro 34 8500"""
payments = []

# Ponemos los datos del enunciado
def start_file():
    file = open('empleado.txt', 'w')
    file.write(start_text)
    file.close()

def split_data():
    with open('empleado.txt', 'r') as file:
        data = [line.rstrip() for line in file]
        for text in data:
            temp = text.split(' ')
            temp.pop(0)
            payments.append([int(payment) for payment in temp])

def update_file():
    with open('empleado.txt', 'w+') as file:
        for i in range(len(payments)):
            if i == 0:
                value = numpy.prod(payments[i])
                file.write(f' Juan 23 4500 ${value}\n')
            if i == 1:
                value = numpy.prod(payments[i])
                file.write(f' María 12 800 ${value}\n')
            if i == 2:
                value = numpy.prod(payments[i])
                file.write(f' Pedro 34 8500 ${value}')

start_file()
split_data()
update_file()
print('Guardado con éxito.')


""" Module for managing routes associated with labels. """
from typing import List

from fastapi import APIRouter, HTTPException, status, Depends
import tortoise

from dependencies.dependency import get_current_user
from label_form_model import ScenarioLabelFormModel
import models

router = APIRouter(
    prefix="/scenario",
    tags=["scenario_label"],
    responses={404: {"description": "Not found"}},
)

@router.post("/label", response_model=models.scen_label_pydantic)
async def create_scen_label(
    body: ScenarioLabelFormModel, user: models.user_pydantic = Depends(get_current_user)
):
    """Add label to a scenario."""

    # Making sure label exists
    await models.Labels.get(
        labelname=body.labelname, user=await models.Users.get(username=user.username)
    )

    # Making sure scenario exists
    await models.ScenarioMetadata.get(
        id=body.scenarioid, user=await models.Users.get(username=user.username)
    )

    try:
        await models.ScenarioLabels.get(
            label=await models.Labels.get(labelname=body.labelname),
            scenario=await models.ScenarioMetadata.get(id=body.scenarioid),
            user=await models.Users.get(username=user.username),
        )
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail="Label already exists for a scenario.",
        )

    except tortoise.exceptions.DoesNotExist:
        scen_label_obj = models.ScenarioLabels(
            label=await models.Labels.get(labelname=body.labelname),
            scenario=await models.ScenarioMetadata.get(id=body.scenarioid),
            user=await models.Users.get(username=user.username),
        )

        await scen_label_obj.save()
        return await models.scen_label_pydantic.from_tortoise_orm(scen_label_obj)

@router.delete("/{id}/label/{labelname}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_scenario_label(
    id: int, labelname: str, user: models.user_pydantic = Depends(get_current_user)
):
    """Delete scenario label by name."""

    try:
        scen_label_data = await models.ScenarioLabels.get(
            label=await models.Labels.get(labelname=labelname),
            scenario=await models.ScenarioMetadata.get(id=id),
            user=await models.Users.get(username=user.username),
        )
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized!"
        ) from e

    await scen_label_data.delete()


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

from enum import Enum
import argparse
from utils.logging import log
import requests
import random
from urllib.parse import urlparse, urlunparse
import json
import os
from pyquery import PyQuery as pq
import time
import threading
import tqdm
import queue

UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.133 Safari/537.39'
DYN_UA_FORMAT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.{:04d}.{:03d} Safari/537.39'

WEBAPI_HOST = 'https://webapi.ctfile.com'
GET_DIR_URL = WEBAPI_HOST + '/getdir.php'
GET_FILE_URL1 = WEBAPI_HOST + '/getfile.php'
GET_FILE_URL2 = WEBAPI_HOST + '/get_file_url.php'
TEMP_DIR = 'temp'
DOWNLOAD_DIR = 'download'

DL_ERROR_FILELINKTIMEOUT = '下载链接已超时，请重新从文件夹获取。'
SPLIT_CNT = 4
DOWNLOAD_CNT = 5

g_sem = threading.Semaphore(DOWNLOAD_CNT)

DL_Thread_status = Enum('DL_Thread_status', ('init', 'finished', 'E404'))

def random_ua(id):
    return DYN_UA_FORMAT.format(random.randrange(9999), id + 1)

def requests_debug(r, prefix=''):
    log.debug('{}requests:{}'.format(prefix, r.request.url))
    log.debug('{}response:{}'.format(prefix, r.text))

class CtFile():
    def __init__(self, url, args, filename='', fid=0, parent_dir=DOWNLOAD_DIR, session=None):
        self.url = url
        self.args = args
        self.fid = fid
        self.parent_dir = parent_dir
        self.filename = filename
        self.s = session if session else requests.session()

        self.urlparsed = urlparse(self.url)

    def dl(self):
        log.info('download {}'.format(self.url))

        # step 1
        headers = {
            'origin': self.urlparsed.netloc,
        }

        # parameters
        params = {
            'f': self.url.split('/')[-1],
            'passcode': '',
            'r': str(random.random()),
            'ref': '',
        }
        r = self.s.get(GET_FILE_URL1, params=params, headers=headers)
        j = json.loads(r.text)
        log.debug('step 1')
        requests_debug(r)

        # link error handler
        if j.get('code') == 404:
            log.error('dl_file error: {}'.format(j.get('message')))
            if j.get('message') == DL_ERROR_FILELINKTIMEOUT:
                log.error('need get dir list again')
            return False, j.get('message')

        if not self.filename:
            self.filename = j['file_name']

        # step 2
        params = {
            'uid': j['userid'],
            'fid': j['file_id'],
            'folder_id': 0,
            'file_chk': j['file_chk'],
            'mb': 0,
            'app': 0,
            'acheck': 1,
            'verifycode': '',
            'rd': str(random.random())
        }
        while True:
            r = self.s.get(GET_FILE_URL2, params=params, headers=headers)
            j = json.loads(r.text)
            log.debug('step 2')
            requests_debug(r)
            if j.get('code') == 503:
                params['rd'] = str(random.random())
            else:
                break

        # create an empty file
        filename = os.path.join(self.parent_dir, self.filename)
        filesize = int(j['file_size'])
        temp_filename = filename + '.ctdown'
        log.debug('create empty file {} size {}'.format(temp_filename, filesize))
        with open(temp_filename, 'wb') as fd:
            fd.truncate(filesize)

        # donwload with thread
        threads = []
        for i in range(self.args.split):
            start = i * filesize // self.args.split
            end = (i + 1) * filesize // self.args.split - 1 if i != self.args.split - 1 else filesize

            t = SplitThread(
                i, j['downurl'].replace(r'\/', r'/'),
                params, headers, filename, start, end)

            log.debug('dl-{:03d} download range start={} end={}'.format(i + 1, start, end))

            threads.append(t)
            t.start()
            # time.sleep(1)

        progressbar = tqdm.tqdm(total=filesize, desc=filename, ascii=' #', unit="B", unit_scale=True, unit_divisor=1024)
        downloaded_bytes = 0
        last_downloaded_bytes = 0
        download_success = True
        while downloaded_bytes < filesize:
            downloaded_bytes = 0
            for t in threads:
                if t._status == DL_Thread_status.E404:
                    log.error('dl-{:03d} download {} Fail'.format(t._index, filename))
                    download_success = False
                    break
                downloaded_bytes += t.downloaded_bytes()

            if not download_success:
                log.error('exit')
                break

            progressbar.update(downloaded_bytes - last_downloaded_bytes)
            last_downloaded_bytes = downloaded_bytes
            log.debug("{} {}".format(downloaded_bytes, filesize))
            time.sleep(1)

        log.debug('quit')
        for i in range(self.args.split):
            threads[i].join()

        os.rename(temp_filename, filename)
        return True, None

class DirThread(threading.Thread):
    def __init__(self, ct_dir):
        super().__init__()
        self.ct_dir = ct_dir
        self.queue = ct_dir.queue
        self.file_threads = []
        self.link_timeout = False
        self.quit = False

    def run(self):
        while not self.quit:
            for t in self.file_threads:
                if not t.is_alive():
                    if t.get_ret():
                        success, error = t.get_ret()
                        if success:
                            # save downloaded status
                            self.ct_dir.status['loc'][t.ct_file.fid] = True
                            self.ct_dir.save_status()
                        else:
                            if error == DL_ERROR_FILELINKTIMEOUT:
                                self.link_timeout = True

                        # remove the thread from array
                        self.file_threads.remove(t)
            time.sleep(1)

    def add(self, thread):
        self.file_threads.append(thread)

class FileThread(threading.Thread):
    def __init__(self, ct_file):
        super().__init__()
        self.ct_file = ct_file
        self.ret = None

    def run(self):
        self.ret = self.ct_file.dl()
        g_sem.release()

    def get_ret(self):
        return self.ret

class SplitThread(threading.Thread):
    def __init__(self, i, url, params, headers, filename, start, end):
        super().__init__()
        self._params = params
        self._index = i
        self._url = url
        self._headers = headers
        self._filename = filename
        self._start = start
        self._end = end
        self._UA = random_ua(self._index)
        self._s = requests.session()
        self._downloaded_bytes = 0
        self._status = DL_Thread_status.init

        self._headers['user-agent'] = self._UA

    def run(self):
        while True:
            while True:
                self._params['rd'] = str(random.random())
                r = self._s.get(GET_FILE_URL2, params=self._params, headers=self._headers)
                j = json.loads(r.text)
                log.debug('dl-{:03d} step 2'.format(self._index + 1))
                requests_debug(r, 'dl-{:03d} '.format(self._index + 1))
                if j['code'] == 503 and j['message'] == 'require for verifycode':
                    log.debug('dl-{:03d} retry'.format(self._index + 1))
                    self._UA = random_ua(self._index)
                    self._headers['user-agent'] = self._UA
                else:
                    break

            with open(self._filename + '.ctdown', 'r+b') as f:
                f.seek(self._start)
                self._headers['Range'] = 'bytes={}-{}'.format(self._start, self._end)
                r = self._s.get(j['downurl'].replace(r'\/', r'/'), headers=self._headers, stream=True)
                log.debug('dl-{:03d} download file request: {}'.format(self._index + 1, r.status_code))
                if r.status_code == 503:
                    log.warning('dl-{:03d} download fail, retry'.format(self._index + 1))
                    self._headers['user-agent'] = random_ua(self._index)
                    time.sleep(1)
                elif r.status_code == 404:
                    log.error('dl-{:03d} download fail, 404'.format(self._index + 1))
                    self._status = DL_Thread_status.E404
                    break
                else:
                    for chunk in r.iter_content(chunk_size=128):
                        f.write(chunk)
                        self._downloaded_bytes += len(chunk)
                    log.debug('dl-{:03d} exit'.format(self._index + 1))
                    break

    def downloaded_bytes(self):
        return self._downloaded_bytes

class CtDir():
    def __init__(self, args, parent_dir=DOWNLOAD_DIR, subdir=''):
        self.args = args
        self.parent_dir = parent_dir

        # create a sub folder url
        if subdir:
            self.url = '{}?{}'.format(args.dir, subdir)
        else:
            self.url = args.dir
        self.urlparsed = urlparse(self.url)
        self.url_host = self.urlparsed.netloc

        # remove the last '/' if exist
        path_split = self.urlparsed.path.split('/')
        self.url_id = path_split[-1] if path_split[-1] else path_split[-2]
        if subdir:
            self.status_file = os.path.join(TEMP_DIR, self.url_id + '.sub-{}'.format(subdir))
        else:
            self.status_file = os.path.join(TEMP_DIR, self.url_id)

        # session
        self.s = requests.session()
        self.s.headers = {'user-agent': UA}

        # init self.status
        if self.status_exist():
            log.debug('read dir status')
            self.load_status()
        else:
            log.debug('init dir status')
            self.init_status()

        # queue between FileThread to DirThread
        self.queue = queue.Queue()

    def save_status(self):
        with open(self.status_file, 'w') as f:
            f.write(json.dumps(self.status))

    def load_status(self):
        with open(self.status_file) as f:
            self.status = json.loads(f.read())

    def status_exist(self):
        return os.path.isfile(self.status_file)

    def init_status(self):
        self.status = {
            'loc': {},
            'web': {},
        }
        with open(self.status_file, 'w') as f:
            f.write(json.dumps(self.status))

    def get_dir_list(self, get_dir_from_web):
        log.info('Get list for {}'.format(self.url))
        if get_dir_from_web or not self.status.get('web'):
            loc = self.status['loc']
            log.info('get list from website')
            headers = {
                'origin': self.urlparsed.netloc
            }
            # parameters
            params = {
                'd': self.urlparsed.path.split('/')[-1],
                'folder_id': self.urlparsed.query,
                'passcode': '',
                'r': str(random.random()),
                'ref': '',
            }
            r = self.s.get(GET_DIR_URL, params=params, headers=headers)
            j = json.loads(r.text.encode().decode('utf-8-sig'))
            requests_debug(r)
            if j.get('code') == 404 or j.get('code') == 503:
                log.error('dl_dir_list error: {}, {}'.format(self.url, j.get('message')))

            loc['name'] = j['folder_name']
            loc['url'] = j['url']  # real url
            log.info('folder name: {}'.format(loc['name']))
            log.info('folder url: {}'.format(loc['url']))

            r = self.s.get(WEBAPI_HOST + loc['url'])
            self.status['web'] = json.loads(r.text)

            self.save_status()

    def dl_dir(self):
        loc = self.status['loc']
        web = self.status['web']

        # mkdir
        dir_fullname = os.path.join(self.parent_dir, loc['name'])
        log.info(dir_fullname)
        if not os.path.exists(dir_fullname):
            os.mkdir(dir_fullname)

        # start a thread to watch the download process
        dir_thread = DirThread(self)
        dir_thread.start()

        for i in web['aaData']:
            p = pq(i[0])
            value = p('input').attr('value')
            dl_type = p('input').attr('name')
            p = pq(i[1])
            name = p('a').text()
            if 'file' in dl_type:
                url = p('a').attr('href')
                if loc.get(value) and loc[value]:
                    log.info('file {} {} downloaded, skip it'.format(value, name))
                else:
                    lst = list(self.urlparsed)
                    lst[3] = lst[4] = lst[5] = ""
                    lst[2] = url
                    ct_file = CtFile(urlunparse(lst), self.args, name, value, dir_fullname, self.s)
                    file_thread = FileThread(ct_file)

                    # get the semaphare before create thread
                    r = False
                    while not r:
                        r = g_sem.acquire(timeout=1)
                        if dir_thread.link_timeout:
                            dir_thread.quit = True
                            return False, DL_ERROR_FILELINKTIMEOUT

                    file_thread.start()
                    dir_thread.add(file_thread)

            elif 'folder' in dl_type:
                log.debug('{} {} is a folder'.format(value, name))
                ct_subdir = CtDir(self.args, dir_fullname, value)
                ct_subdir.get_dir_list(True)
                success, error = ct_subdir.dl_dir()
                if not success:
                    dir_thread.quit = True
                    return success, error

        dir_thread.quit = True
        return True, None

def main():
    global g_sem
    parser = argparse.ArgumentParser(description='Download from CTDisk.')
    parser.add_argument('-d', '--dir', help='download a directory')
    parser.add_argument('-f', '--file', help='download a file')
    parser.add_argument('-s', '--split', type=int, default=SPLIT_CNT, help='split a files to parts')
    parser.add_argument('-c', '--dl_cnt', type=int, default=DOWNLOAD_CNT, help='download files concorrent')
    args = parser.parse_args()

    g_sem = threading.Semaphore(args.dl_cnt)

    if args.dir:
        stop = False
        get_dir_from_web = False
        error = None
        ct_dir = CtDir(args)
        while not stop:
            if error == DL_ERROR_FILELINKTIMEOUT:
                get_dir_from_web = True
            else:
                get_dir_from_web = False
            ct_dir.get_dir_list(get_dir_from_web)
            success, error = ct_dir.dl_dir()
            if success:
                stop = True
                log.info('download finished')
    elif args.file:
        ct_file = CtFile(args.file, args)
        ct_file.dl()
        log.info('download finished')

if __name__ == "__main__":
    main()


from tkinter import *
from tkinter import messagebox as mb
from child_window import ChildWindow
# from PIL import Image as PilImage
# from PIL import ImageTk, ImageOps

class Window:
    def __init__(self, width, height, title="MyWindow", resizable=(False, False), icon=r"resources/feather.ico"):
        self.root = Tk()
        self.root.title(title)
        # self.root.geometry(f"{width}x{height}+200+200")
        self.root.geometry("+600+300")
        # self.root.resizable(resizable[0], resizable[1])
        if icon:
            self.root.iconbitmap(icon)

        self.txt = StringVar(value="Hey")

    def run(self):
        self.draw_widgets()
        self.root.mainloop()

    def draw_widgets(self):
        self.draw_menu()
        Message(self.root, text="My Message", bg="red", fg="white", font=("Verdana", 8, "italic"), justify=CENTER).pack()
        Message(self.root, text="My\nMessage\n1", relief=RAISED, bd=3, width=100, padx=20, pady=30).pack()
        Message(self.root, textvariable=self.txt).pack()

        self.txt.set("Hey123")
        print(self.txt.get())

    def draw_menu(self):
        menu_bar = Menu(self.root)

        file_menu = Menu(menu_bar, tearoff=0)
        file_menu.add_separator()
        file_menu.add_command(label="Выйти", command=self.exit)

        info_menu = Menu(menu_bar, tearoff=0)
        info_menu.add_command(label="О приложении", command=self.show_info)

        menu_bar.add_cascade(label="Файл", menu=file_menu)
        menu_bar.add_cascade(label="Справка", menu=info_menu)
        self.root.configure(menu=menu_bar)

    def show_info(self):
        mb.showinfo("Информация", "Лучшее графическое приложение на свете")

    def exit(self):
        choice = mb.askyesno("Quit", "Do you want to quit?")
        if choice:
            self.root.destroy()

    def create_child(self, width, height, title="Child", resizable=(False, False), icon=None):
        ChildWindow(self.root, width, height, title, resizable, icon)

if __name__ == "__main__":
    window = Window(500, 500, "TKINTER")
    # window.create_child(200, 100)
    window.run()


import logging
from dateutil import parser
import requests
import json
import logging
import pandas as pd
from datetime import datetime, timedelta
import pandas as pd
import PyPDF2
import re
import copy
logger = logging.getLogger(__name__)

class CreditRating:
    def __init__(self, name, line):
        self.name = name
        self.line = line
        self.ratings = []
        self.alfanum = []

    def add_rating(self, rate):
        rate = rate.replace("-", "")
        rate = rate.strip()
        if rate.isnumeric():
            self.ratings.append(rate)
        elif rate == "na":
            self.ratings.append('0')

    def add_alfa_rating(self, rate):
        rate = rate.replace("-", "")
        rate = rate.strip()
        if len(rate) > 0:
            self.alfanum.append(rate)

    def post_processes_ratings(self):
        temp = []
        i = 0
        while i < len(self.ratings):
            if len(self.ratings[i])>=5:
                temp.append(float(self.ratings[i][:3]))
                temp.append(float(self.ratings[i][3:6]))
                i=i+2
            elif len(self.ratings[i])==4:
                temp.append(float(self.ratings[i][:3]))
                i=i+1
            else:
                temp.append(float(self.ratings[i]))
                i=i+1
        self.ratings = []
        temp2=[]
        i = 0
        greatest=0
        while i < len(temp):
            if i==0:
                temp2.append(temp[i])
                greatest=temp[i]
            else:
                if temp[i]<greatest:
                    temp2.append(greatest)
                else:
                    temp2.append(temp[i])
                    greatest = temp[i]
            i=i+1
        i=len(temp2)-1
        greatest = 10000
        while i>=0:
            if temp2[i]==0:
                self.ratings.insert(0,greatest)
            else:
                self.ratings.insert(0, temp2[i])
                greatest=temp2[i]
            i=i-1

    def processes_ratings(self):
        temp = []
        i = 0
        while i < len(self.ratings):
            if self.ratings[i]=="1" or (len(self.ratings[0]) > 1 and len(
                    self.ratings[i]) == 1):  # If first element is very low rating, do not check for split fields
                newval = self.ratings[i] if i + 1 == len(self.ratings) else self.ratings[i] + self.ratings[i + 1]
                temp.append(newval)
                i = i + 2
            elif i>1 and (len(self.ratings[i])==2 and len(self.ratings[i-1])==3):
                newval = self.ratings[i] if i + 1 == len(self.ratings) else self.ratings[i] + self.ratings[i + 1]
                temp.append(newval)
                i = i + 2
            elif i<(len(self.ratings)-1) and (len(self.ratings[i])==2 and len(self.ratings[i+1])==4):
                firstval=self.ratings[i] + self.ratings[i+1][0]
                secval = self.ratings[i + 1][1:]
                temp.append(firstval)
                temp.append(secval)
                i = i + 2
            else:
                temp.append(self.ratings[i])
                i = i + 1
        self.ratings = []
        for newel in temp:
            if newel.isnumeric():
                self.ratings.append((newel))
    def __str__(self):
        return self.name + " rating=" + str(self.ratings[0:7])

from pathlib import Path

def parse_credit_rating(filepath):
    if isinstance(filepath, str):
        columns = Path(filepath).name.split("_")
    else:
        columns = filepath.name.split("_")
    dt = columns[0]
    dtiso = "20" + dt[0:2] + "-" +dt[2:4] + "-" + dt[4:6]
    try:
        pdfReader = PyPDF2.PdfReader(filepath)
    except Exception as e:
        print(e)
        return None, None
    companies = []
    def parse_page(pagenr):
        pageObj = pdfReader.pages[pagenr]
        lines = pageObj.extract_text().split("\n")
        idx = 0
        while idx < len(lines):
            line = lines[idx]
            try:
                company = re.match("((.*?)( AS| AB| ASA| A/S))+", line).group()
                # print(company)
                ratingcompany = CreditRating(company, line)
                companies.append(ratingcompany)
                ratingline = line[len(company):]  # The rest of the line after name
                columns = ratingline.split('/')
                if len(columns[-1:]) > 0:
                    firstel = columns[-1:][0]  # There should only be one element after  the / character
                    firstel = firstel.replace("-", "")
                    firstel = firstel.strip()  # Get rid of white space
                    ratinglist = firstel.split( " ")
                    for r in ratinglist:
                        ratingcompany.add_rating(r)
                for r in columns[:-1]:
                    ratingcompany.add_alfa_rating(r)
            except:
                pass
            idx = idx + 1

    for i in range(20):
        parse_page(i)

    for c in companies:
        splits = re.split(r'\s+and\s+|,(?!\s*AB|\s*AS)\s*', c.name)
        if len(splits) > 0:
            c.name = splits[0]
            for s in splits[1:]:
                cc = copy.deepcopy(c)
                cc.name = s
                companies.append(cc)
    dicts = []
    for c in companies:
        if len(c.ratings) > 0:
            c.processes_ratings()
            c.processes_ratings()
            c.processes_ratings()
            c.post_processes_ratings()
            # print(c)
        if len(c.ratings) < 8:
            continue
        dicts.append(
            {
                "company": c.name,
                "3M": c.ratings[0],
                "1Y": c.ratings[1],
                "2Y": c.ratings[2],
                "3Y": c.ratings[3],
                "5Y": c.ratings[4],
                "7Y": c.ratings[5],
                "10Y": c.ratings[6],
            }
        )
    df = pd.DataFrame.from_dict(dicts)
    return dtiso, df


import streamlit as st
import pandas as pd
import numpy as np
import pydeck as pdk

import streamlit as st
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from numpy import asarray

st.set_page_config()

#st.title("Global Greening")

st.markdown("
GLOBAL GREENINGðŸŒ¿
", unsafe_allow_html=True)

#original_title = '
GLOBAL GREENING ðŸŒ¿
'

#st.markdown(original_title, unsafe_allow_html=True)

st.sidebar.success("PROJECT: Global Greening ðŸ¤– ")

import streamlit as st

def intro():
    import streamlit as st

    st.write("# Welcome to Streamlit! ðŸ‘‹")
    st.sidebar.success("Select a demo above.")

    st.markdown(
        """
        Streamlit is an open-source app framework built specifically for
        Machine Learning and Data Science projects.

        **ðŸ‘ˆ Select a demo from the dropdown on the left** to see some examples
        of what Streamlit can do!

        ### Want to learn more?

        - Check out [streamlit.io](https://streamlit.io)
        - Jump into our [documentation](https://docs.streamlit.io)
        - Ask a question in our [community
          forums](https://discuss.streamlit.io)

        ### See more complex demos

        - Use a neural net to [analyze the Udacity Self-driving Car Image
          Dataset](https://github.com/streamlit/demo-self-driving)
        - Explore a [New York City rideshare dataset](https://github.com/streamlit/demo-uber-nyc-pickups)
    """
    )

def plotting_demo():

    # Generate random data for each year
    data_1985 = np.random.randn(5000,2) + [39.7302, -104.9903]
    data_1990 = np.random.randn(5000,2) / [5, 5] + [39.7302, -104.9903]
    data_1995 = np.random.randn(5000,2) / [20, 20] + [39.7302, -104.9903]

    # Combine the data for each year into a single DataFrame
    chart_data = pd.DataFrame(
        np.concatenate([data_1985, data_1990, data_1995],axis=0),
        columns=['lat', 'lon']
    )
    #st.write(data_1985[1])
    #st.write(data_1985[2])
    st.write(chart_data)

    MAPBOX_API_KEY = st.secrets["MAPBOX_API_KEY"]

    # AWS Open Data Terrain Tiles
    TERRAIN_IMAGE = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"

    # Define how to parse elevation tiles
    ELEVATION_DECODER = {"rScaler": 256, "gScaler": 1, "bScaler": 1 / 256, "offset": -32768}

    SURFACE_IMAGE = f"https://api.mapbox.com/v4/mapbox.satellite/{{z}}/{{x}}/{{y}}@2x.png?access_token={MAPBOX_API_KEY}"

    ndvi_data=pd.read_csv("/Users/karakaya/code/Alastair908/GlobalGreening/app_demo/csv_largeNDVI.csv")
    # Filter data based on selected year
    #selected_year = st.slider("Choose Year!", min_value=int(ndvi_data['Year'].min()), max_value=int(ndvi_data['Year'].max()), step=5)

    #filtered_data = ndvi_data[ndvi_data['Year'] == str(selected_year)]
    #st.write(Changing NDVI index over time )

    nvdi_positive= ndvi_data.query('NDVI>0')
    nvdi_negative= ndvi_data.query('NDVI<0')
    nvdi_negative['NDVI'] = nvdi_negative['NDVI'].abs()

    initial_view_state=pdk.ViewState(
            latitude=37.42700000000004,
            longitude=-106.39530000000022,
            zoom=10,
            pitch=50,
        )
    column_1=pdk.Layer(
            'ColumnLayer',
            data=nvdi_positive,
            get_position=['longitude', 'latitude'],
            radius=500,
            get_elevation ="NDVI",
            elevation_scale=50,
            get_fill_color=[0, 100, 0, 255],
            pickable=True,
            auto_highlight=True,

)

    column_2=pdk.Layer(
            'ColumnLayer',
            data=nvdi_negative,
            get_position=['longitude', 'latitude'],
            radius=500,
            get_elevation ="NDVI",
            elevation_scale=50,
            get_fill_color=[255, 0, 0, 255],
            pickable=True,
            auto_highlight=True,
    )

    scatter=pdk.Layer(
                'ScatterplotLayer',
                 data=nvdi_positive,
                 get_position=['longitude', 'latitude'],
                 get_color=[0, 42, 115, 55],
                 get_radius=200,
             )
    terrain=pdk.Layer(
     "TerrainLayer", elevation_decoder=ELEVATION_DECODER, texture=SURFACE_IMAGE, elevation_data=TERRAIN_IMAGE)

    r = pdk.Deck(layers=[column_1,column_2,scatter,terrain],

    initial_view_state=initial_view_state)

    st.pydeck_chart(r)

    initial_view_state=pdk.ViewState(
            latitude=37.42700000000004,
            longitude=-106.39530000000022,
            zoom=10,
            pitch=50,
        )
    column=pdk.Layer(
            'ColumnLayer',
            data=nvdi_negative,
            get_position=['longitude', 'latitude'],
            radius=500,
            get_elevation ="NDVI",
            elevation_scale=50,
            get_fill_color=[255, 0, 0, 255],
            pickable=True,
            auto_highlight=True,
)
    scatter=pdk.Layer(
                'ScatterplotLayer',
                 data=nvdi_negative,
                 get_position=['longitude', 'latitude'],
                 get_color=[0, 42, 115, 55],
                 get_radius=200,
             )
    terrain=pdk.Layer(
     "TerrainLayer", elevation_decoder=ELEVATION_DECODER, texture=SURFACE_IMAGE, elevation_data=TERRAIN_IMAGE)

    r = pdk.Deck(layers=[column,scatter,terrain],
    initial_view_state=initial_view_state)

    st.pydeck_chart(r)

    st.write(nvdi_negative['NDVI'])

def charts_demo():
    arr = np.random.normal(1, 1, size=100)
    fig, ax = plt.subplots()
    ax.hist(arr, bins=20)

    st.pyplot(fig)

#def team_members():

    #team_members = [
    #{"name": "", "image": "image1.png"},
    #{"name": "Jane Smith", "image": "image2.png"},
    ##{"name": "Michael Johnson", "image": "image3.png"},
    #{"name": "Emily Davis", "image": "image4.png"}
#]

# Yeniden boyutlandÄ±rÄ±lmÄ±ÅŸ resimlerin boyutu
#image_size = (300, 300)

# Streamlit uygulamasÄ±nÄ± baÅŸlat
#st.title('TakÄ±m Ãœyeleri')

# DÃ¶rt resmi gÃ¶stermek iÃ§in 2x2 bir grid oluÅŸturun
#col1, col2 = st.columns(2)

# TakÄ±m Ã¼yelerini dÃ¶ngÃ¼ye alÄ±n ve resimleri ve isimleri grid iÃ§ine yerleÅŸtirin
#for i, member in enumerate(team_members):
    #with col1 if i < 2 else col2:
        #image = Image.open(member["image"])
        #resized_image = image.resize(image_size)
        #st.image(resized_image, caption=member["name"], use_column_width=True)

#demo_name = st.sidebar.selectbox("Choose a demoðŸŒ¿", page_names_to_funcs.keys())
#page_names_to_funcs[demo_name]()

page_names_to_funcs = {
    "Intro": intro,
    "Plotting Demo": plotting_demo,
    "Charts Demo": charts_demo,
    #'team': team
}

#st.sidebar.success("PROJECT: Global Greening ðŸ¤– ")
demo_name = st.sidebar.selectbox("Choose a demoðŸŒ¿", list(page_names_to_funcs.keys()))

page_func = page_names_to_funcs[demo_name]
page_func()

# Create map
#view_state = pdk.ViewState(latitude=filtered_data['lat'].mean(), longitude=filtered_data['lon'].mean(), zoom=10, pitch=50)
#scatterplot_layer = pdk.Layer('ScatterplotLayer', data=filtered_data, get_position='[lon, lat]', get_color=[0, 255, 0, 160], get_radius=200)
#deck = pdk.Deck(map_style='mapbox://styles/mapbox/satellite-v9', initial_view_state=view_state, layers=[scatterplot_layer])
#st.pydeck_chart(deck)


"""
A custom submission type to evaluate SQL queries
"""
from xblock.core import XBlock
from xblockutils.resources import ResourceLoader
from xblockutils.studio_editable import StudioEditableXBlockMixin

from .mixins.dates import EnforceDueDates
from .mixins.fragment import XBlockFragmentBuilderMixin
from .mixins.grading import Scorable
from .mixins.scenario import XBlockWorkbenchMixin
from .models import XBlockDataMixin

def _(text):
    """
    Mock translation helper
    """
    return text

@XBlock.needs('i18n')
class SqlGrader(
        XBlockDataMixin,
        EnforceDueDates,
        StudioEditableXBlockMixin,
        Scorable,
        XBlockFragmentBuilderMixin,
        XBlockWorkbenchMixin,
        XBlock,
):
    """
    A custom submission type to evaluate SQL queries
    """
    # pylint: disable=too-many-ancestors

    loader = ResourceLoader(__name__)
    public_dir = 'static'
    static_css = [
        'view.css',
        'codemirror/lib/codemirror.css',
        'codemirror/addon/hint/show-hint.css',
    ]
    static_js = [
        'view.js',
        'codemirror/lib/codemirror.js',
        'codemirror/mode/sql.js',
        'codemirror/addon/edit/matchbrackets.js',
        'codemirror/addon/hint/show-hint.js',
        'codemirror/addon/hint/sql-hint.js',
    ]
    static_js_init = 'SqlGrader'

    @XBlock.json_handler
    def submit_query(self, data, suffix=''):
        """
        Handle a user's query submission
        """
        # pylint: disable=unused-argument
        query = data.get('query') or ''
        query = str(query)
        query = query.strip()
        self.raw_response = query
        score, actual, expected, error, comparison = self._calculate_score()
        self.set_score(score)
        return {
            'comparison': comparison,
            'result': actual,
            'expected': expected,
            'verify': self.verify_query,
            'error': error,
        }


from lib.cuckoo.common.abstracts import Signature

class ProcessCreationSuspiciousLocation(Signature):
    name = "process_creation_suspicious_location"
    description = "Created a process from a suspicious location"
    severity = 3
    confidence = 20
    categories = ["execution"]
    authors = ["Kevin Ross"]
    minimum = "1.3"
    evented = True
    ttps = ["T1106"]

    filter_apinames = set(["CreateProcessInternalW"])

    def __init__(self, *args, **kwargs):
        Signature.__init__(self, *args, **kwargs)
        self.ret = False
        self.ignore_proc_list = []
        self.suspicious_paths = [
            "\\AppData\\Roaming\\",
            "\\AppData\\Local\\Temp\\",
        ]

    def on_call(self, call, process):
        pname = process["process_name"].lower()
        if pname not in self.ignore_proc_list:
            appname = self.get_argument(call, "ApplicationName")
            cmdline = self.get_argument(call, "CommandLine")
            for suspiciouspath in self.suspicious_paths:
                if suspiciouspath in appname:
                    self.ret = True
                    self.data.append({"file": appname})
                    self.data.append({"command": cmdline})
                    if self.pid:
                        self.mark_call()

    def on_complete(self):
        return self.ret


import discord
from discord.ext import commands
import requests
from core.Coin import Coin

class Crypto(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self.currency = "usd"
        self.supported_currencies = []
        self.last_used_coin = "bitcoin"  # DEFAULT COIN

    def get_supported_currencies(self):
        url = "https://api.coingecko.com/api/v3/simple/supported_vs_currencies"
        res = requests.get(url).json()
        self.supported_currencies = res

    @commands.command()
    async def price(self, ctx, coin=None):
        if coin is not None:
            self.last_used_coin = coin
            coin = Coin(coin, self.currency)
        else:
            coin = Coin(self.last_used_coin, self.currency)
        coin.get_price()
        title = f"1 {coin.name} = {coin.price} {self.currency.upper()}"
        color = 0xff0000
        embed = discord.Embed(
            title=title, color=color)

        await ctx.send(embed=embed)

    @commands.command()
    async def currency(self, ctx, currency):

        if len(self.supported_currencies) > 0:
            pass
        else:
            self.get_supported_currencies()

        if currency in self.supported_currencies:
            self.currency = currency
            await ctx.send(f"Currency changed to ``{currency.upper()}``")
        else:
            await ctx.send("Currency not supported")

    @commands.command()
    async def chart(self, ctx, coin=None):
        if coin is not None:
            self.last_used_coin = coin
            coin = Coin(coin, self.currency)
        else:
            coin = Coin(self.last_used_coin, self.currency)
        coin.get_historical_data()
        coin.generate_chart()
        await ctx.send(file=discord.File('./src/img/chart.png'))

def setup(bot: commands.Bot):
    bot.add_cog(Crypto(bot))


import random
from traits_flaws import *
import os.path
import pickle

def rng(num):
    return random.randint(1, num)

def selector(prompt, min_num, max_num):
    valid = False
    while not valid:
        try:
            var = int(input(prompt))
        except TypeError as err:
            print(err)
            continue
        except ValueError as err:
            print(err)
            continue
        if min_num <= var <= max_num:
            return var
        else:
            continue

def trait_list():
    for key in traits_dict:
        print("-" * 25)
        print(key + " : " + traits_dict[key])

def unpickle_char():
    path = (os.getcwd() + "/pickles/")
    pickles = []
    for obj in os.listdir(path):
        f = os.path.join(path, obj)
        if os.path.isfile(f):
            pickles.append(f)
    num = 1
    for obj in pickles:
        print(f"{num}. {obj}")
        num += 1
    var = int(input("Please enter the corresponding number to the save file you wish to load\n"))
    with open(pickles[var - 1], 'rb') as inp:
        obj = pickle.load(inp)
    return obj

def str_input(prompt):
    x = ""
    try:
        x = str(input(prompt))
    except ValueError as err:
        print(err)
    return x


import matplotlib.pyplot as plt

# Vector class
class Vector:
    def __init__(self, vector: list):
        self.start_x = 0
        self.start_y = 0
        self.dx = vector[0]
        self.dy = vector[1]

    def move_vector(self, move_x, move_y):
        self.start_x += move_x
        self.start_y += move_y

    def plt_arrow(self, color, l_style='solid'):
        # plt.arrow(x, y, dx, dy)
        return plt.arrow(self.start_x, self.start_y, self.dx, self.dy,
                         length_includes_head=True,
                         head_width=.08, head_length=.08,
                         color=color,
                         linestyle=l_style
                         )

    def value(self):
        value = [self.start_x, self.start_y, self.dx, self.dy]
        return value

def draw_scalar_int_multiply(v: Vector, times: int, color, l_style='solid'):
    draw_v = Vector([v.dx, v.dy])
    draw_v.start_x = v.start_x
    draw_v.start_y = v.start_y

    return_legend = ''
    if times == 0:
        return return_legend
    if times > 0:
        pass
    else:
        draw_v.dx *= -1
        draw_v.dy *= -1

    for _ in range(abs(times)):
        return_legend = draw_v.plt_arrow(color, l_style)
        draw_v.start_x += draw_v.dx
        draw_v.start_y += draw_v.dy
    return return_legend


import mne
import matplotlib.pyplot as plt
from matplotlib import cm
from nilearn import plotting
from nilearn import  datasets
import pandas as pd
import sys
import time
import os
from os import listdir as listdir
from os.path import join
from os.path import isdir
from os.path import isfile
import numpy as np
import holoviews as hv
hv.extension('bokeh')
from holoviews import opts, dim
from matplotlib import patches

def plot_aparc_parcels(matrix, ax, fig, title):
    """
    :param matrix:
        The connectivity matrix (real or simulated) representing aparc parcels

    :param ax:
        axes to plot on
    :return:
    """

    # get labels and coordinates
    labels = mne.read_labels_from_annot('fsaverage_1', parc='aparc', subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS','FS_SUBDIR'))
    labels = labels[0:-1]
    label_names = [label.name for label in labels]
    coords = []
    # TODO: Find a better way to get centre of parcel
    #get coords of centre of mass
    for i in range(len(labels)):
        if 'lh' in label_names[i]:
            hem = 1
        else:
            hem = 0
        coord = mne.vertex_to_mni(labels[i].center_of_mass(subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS','FS_SUBDIR')), subject='fsaverage_1',hemis=hem,subjects_dir=join(MAINDIR,'FS_SUBDIR'))
        coords.append(coord[0])

    plotting.plot_connectome_strength(matrix,
                                      node_coords=coords,
                                      title=title,
                                      figure=fig,
                                      axes=ax,
                                      cmap=plt.cm.YlOrRd)

def hv_chord(contrast, frequency, threshold, stats, re_order_ind, label_names, des, freq_vect):

    """
    Makes a holoview/boken chord diagram
    :param contrast:
        Which contrast in the glm data are we looking at
    :param frequency:
        The index of the frequency in the freq_vect we are looking at
    :param threshold:
        The percentile threshold for plotting (so the plot isn't messy)
    :param stats:
        The stats array we are plotting from
    :param re_order_ind:
        The indices for re-ordering the data into the Y-order of the parcels in the brain
    :param label_names:
        The names for each parcel
    :param des:
        The design matrix from the GLM
    :param freq_vect:
        The frequency vector containing the actual frequencies of each
    :return:
    """
    dtypes = np.dtype([
        ('source', int),
        ('target', int),
        ('value', int),
    ])

    data = np.empty(0, dtype=dtypes)
    links = pd.DataFrame(data)

    square = stats[contrast,:,:,frequency]

    #square = stats[contrast,:,:,:].sum(axis=2)
    thresh_mask = square > np.percentile(square, threshold)
    square[~thresh_mask] = 0

    #reorder
    X_sorted = np.copy(square)
    # Sort along first dim
    X_sorted = X_sorted[re_order_ind,:]
    # Sort along second dim
    X_sorted = X_sorted[:,re_order_ind]

    labels_sorted = np.array(label_names)[re_order_ind]
    # loop through Y axis of matrix
    counter = 0
    for i in range(X_sorted.shape[0]):
        for ii in range(X_sorted.shape[1]):
            links.loc[counter] = [i, ii, int(X_sorted[i,ii])]
            counter +=1

    # make label index
    dtypes = np.dtype([
        ('name', int),
        ('group', int),
    ])

    data = np.empty(0, dtype=dtypes)
    nodes = pd.DataFrame(data)

    for i in range(X_sorted.shape[0]):
        nodes.loc[i] = [labels_sorted[i], 1]

    graph = hv.Chord((links, hv.Dataset(nodes, 'index')))
    graph.opts(
        opts.Chord(cmap='Category20', edge_cmap='Category20', edge_color=dim('source').str(),
                   labels='name', node_color=dim('index').str())
    )
    graph.relabel('Directed Graph').opts(directed=True)
    graph.opts(title=f'{des.contrast_names[contrast]} Partial Directed Coherence @ {int(freq_vect[frequency])}Hz')
    return graph

def get_labels():
    MAINDIR = join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS')
    labels = mne.read_labels_from_annot('fsaverage_1', parc='aparc', subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS','FS_SUBDIR'))
    labels = labels[0:-1]
    label_names = [label.name for label in labels]
    coords = []
    # TODO: Find a better way to get centre of parcel
    #get coords of centre of mass
    for i in range(len(labels)):
        if 'lh' in label_names[i]:
            hem = 1
        else:
            hem = 0
        coord = mne.vertex_to_mni(labels[i].center_of_mass(subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS','FS_SUBDIR')),
                                  subject='fsaverage_1',hemis=hem,subjects_dir=join(MAINDIR,'FS_SUBDIR'))
        coords.append(coord[0])

    # First, we reorder the labels based on their location in the left hemi
    lh_labels = [name for name in label_names if name.endswith('lh')]
    rh_labels = [name for name in label_names if name.endswith('rh')]

    # Get the y-location of the label
    label_ypos = list()
    for name in lh_labels:
        idx = label_names.index(name)
        ypos = np.mean(labels[idx].pos[:, 1])
        label_ypos.append(ypos)

    lh_labels = [label for (yp, label) in sorted(zip(label_ypos, lh_labels))]

    # For the right hemi
    rh_labels = [label[:-2] + 'rh' for label in lh_labels]

    # make a list with circular plot order
    node_order = list()
    node_order.extend(lh_labels[::-1])  # reverse the order
    node_order.extend(rh_labels)

    node_order = node_order[::-1] # reverse the whole thing

    # get a mapping to the original list
    re_order_ind = [label_names.index(x) for x in node_order]

    return labels, label_names, re_order_ind

def plot_roi(names):
    labels = mne.read_labels_from_annot('fsaverage_1', parc='aparc', subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS','FS_SUBDIR'))
    #labels = labels[0:-1]
    label_names = [label.name for label in labels]

    #names = ['lingual-lh', 'parahippocampal-lh', 'parahippocampal-rh']

    ninds = [label_names.index(i) for i in names]

    # nilearn parcellation is a list representing each vertex, do left and right seperatl
    # MNE labels are a list representing the index of each vertex the parcel contains

    parcellation = np.zeros([10242])
    names_cmap_inds = []
    for i in range(len(names)):
        vinds = labels[ninds[i]].get_vertices_used()
        names_cmap_inds.append(i+1)
        for ii in vinds:
            parcellation[ii] = i+1

    fsaverage = datasets.fetch_surf_fsaverage()

    cmap = cm.get_cmap('gist_ncar')
    legend_elements = [patches.Patch(facecolor=cmap(x), edgecolor=cmap(x), label=y) for x,y in zip(names_cmap_inds, names)]

    fig, ax = plt.subplots(2,2, subplot_kw={'projection': '3d'})
    plotting.plot_surf_roi(fsaverage['pial_left'], roi_map=parcellation,
                           hemi='left', view='medial',
                           bg_map=fsaverage['sulc_left'], bg_on_data=True,
                           darkness=.5, axes=ax[0,0], title= 'Left Hemisphere Medial',
                           cmap=cmap)

    plotting.plot_surf_roi(fsaverage['pial_right'], roi_map=parcellation,
                           hemi='right', view='medial',
                           bg_map=fsaverage['sulc_right'], bg_on_data=True,
                           darkness=.5, axes=ax[0,1], title= 'Right Hemisphere Medial',
                           cmap=cmap)

    plotting.plot_surf_roi(fsaverage['pial_left'], roi_map=parcellation,
                           hemi='left', view='lateral',
                           bg_map=fsaverage['sulc_left'], bg_on_data=True,
                           darkness=.5, axes=ax[1,0], title= 'Left Hemisphere Lateral',
                           cmap=cmap)

    plotting.plot_surf_roi(fsaverage['pial_right'], roi_map=parcellation,
                           hemi='right', view='lateral',
                           bg_map=fsaverage['sulc_right'], bg_on_data=True,
                           darkness=.5, axes=ax[1,1], title= 'Right Hemisphere Lateral',
                           cmap=cmap)

    fig.legend()
    plt.subplots_adjust(right=0.85)
    return [fig, ax]

def mne_connec(matrix, this_title, fig, *labels):
    MAINDIR = join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS')
    if labels is None:
        labels = mne.read_labels_from_annot('fsaverage_1', parc='aparc',
                                            subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS', 'FS_SUBDIR'))
    labels = labels[0:-1]
    label_colors = [label.color for label in labels]

    label_names = [label.name for label in labels]
    coords = []
    # TODO: Find a better way to get centre of parcel
    # get coords of centre of mass
    for i in range(len(labels)):
        if 'lh' in label_names[i]:
            hem = 1
        else:
            hem = 0
        coord = mne.vertex_to_mni(
            labels[i].center_of_mass(subjects_dir=join('/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS', 'FS_SUBDIR')),
            subject='fsaverage_1', hemis=hem, subjects_dir=join(MAINDIR, 'FS_SUBDIR'))
        coords.append(coord[0])

    # First, we reorder the labels based on their location in the left hemi
    lh_labels = [name for name in label_names if name.endswith('lh')]
    rh_labels = [name for name in label_names if name.endswith('rh')]

    # Get the y-location of the label
    label_ypos = list()
    for name in lh_labels:
        idx = label_names.index(name)
        ypos = np.mean(labels[idx].pos[:, 1])
        label_ypos.append(ypos)

    lh_labels = [label for (yp, label) in sorted(zip(label_ypos, lh_labels))]

    # For the right hemi
    rh_labels = [label[:-2] + 'rh' for label in lh_labels]

    # make a list with circular plot order
    node_order = list()
    node_order.extend(lh_labels[::-1])  # reverse the order
    node_order.extend(rh_labels)

    node_order = node_order[::-1]  # reverse the whole thing

    # get a mapping to the original list
    re_order_ind = [label_names.index(x) for x in node_order]

    node_angles = mne.viz.circular_layout(label_names, node_order, start_pos=90,
                                  group_boundaries=[0, len(label_names) / 2])

    fig2, ax = mne.viz.plot_connectivity_circle(matrix, label_names, n_lines=300,
                             node_angles=node_angles, node_colors=label_colors,
                             title=this_title, fig=fig,
                                                facecolor='white',
                                                textcolor='black')

    return fig2, ax

def plot_cluster(time_inds, space_inds, T_obs, evok):

    ch_inds = np.unique(space_inds)
    time_inds = np.unique(time_inds)

    # get topography for F stat
    f_map = T_obs[time_inds, ...].mean(axis=0)

    # get signals at the sensors contributing to the cluster
    sig_times = evok.times[time_inds]

    # create spatial mask
    mask = np.zeros((f_map.shape[0], 1), dtype=bool)
    mask[ch_inds, :] = True
    reg_masks.append(mask)
    # initialize figure
    fig, ax_topo = plt.subplots(1, 1, figsize=(10, 3))

    # plot average test statistic and mark significant sensors
    image, _ = mne.viz.plot_topomap(f_map, pos, mask=mask, axes=ax_topo, ch_type=sen,
                                    vmin=np.min, vmax=np.max, sphere=sphere, show=False)

    # create additional axes (for ERF and colorbar)
    divider = make_axes_locatable(ax_topo)

    # add axes for colorbar
    ax_colorbar = divider.append_axes('right', size='5%', pad=0.05)
    plt.colorbar(image, cax=ax_colorbar)
    ax_topo.set_xlabel(
        'Averaged T-map ({:0.3f} - {:0.3f} s)'.format(*sig_times[[0, -1]]))

    # add new axis for time courses and plot time courses
    ax_signals = divider.append_axes('right', size='300%', pad=1.2)
    title = 'Retro-Cue Cluster #{0}, {1} sensor'.format(i_clu + 1, len(ch_inds))
    if len(ch_inds) > 1:
        title += "s (mean)"
    mne.viz.plot_compare_evokeds({'Left': L_E, 'Right': R_E}, title=title, picks=ch_inds, axes=ax_signals,
                                 colors=colors, show=False,
                                 split_legend=True, truncate_yaxis='auto', combine='mean')

    ax_signals.set_title(title, y=1.1)
    # plot temporal cluster extent
    ymin, ymax = ax_signals.get_ylim()
    ax_signals.fill_betweenx((ymin, ymax), sig_times[0], sig_times[-1],
                             color='orange', alpha=0.3)

    xmin, xmax = ax_signals.get_xlim()

    # add labels if needed
    for i, ev in enumerate(event_labels):
        if xmin <= event_onsets[i] <= xmax:
            ax_signals.axvline(event_onsets[i], linestyle='--')
            ax_signals.text(event_onsets[i], ymax, f'{ev} onset')
        if xmin <= event_offsets[i] <= xmax:
            ax_signals.axvline(event_offsets[i], linestyle='--')
            ax_signals.text(event_offsets[i], ymax, f'{ev} offset')

    ax_signals.set_ylabel('Field Strength (fT)')
    # clean up viz
    mne.viz.tight_layout(fig=fig)
    fig.subplots_adjust(bottom=.05)

    labels = [item.get_text() for item in ax_signals.get_xticklabels()]
    labels = [str(float(i) - plot_t_offset) for i in labels]
    ax_signals.set_xticklabels(labels)
    plt.show()


import sys
import pygame

pygame.init()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

playing = True

while playing:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            playing = False

    screen.fill((255, 255, 255))
    pygame.display.flip()


lst1=[1,2,3,4,5]
l=len(lst1)

lst2=[3,4,5,1,2]

def rotate_lst(lst1,lst2):
    shift=int(input("enter how many steps do you want to shift"))
    net_shift=shift%len(lst1)
    #if net_shift==0, for loop imediately ends
    for i in range(net_shift):
        if net_shift>0:
            poped=lst1.pop(len(lst1)-1)
            lst1.insert(0,poped)

        elif net_shift<0:
            poped=lst1.pop(0)
            lst1.append(poped)
    print(lst1)
    #if set(lst2).issubset(lst1):
        #print("list2 is rotation list of list1")
#shift=int(input("enter no of shifts"))

rotate_lst(lst1,lst2)


import requests
from bs4 import BeautifulSoup
import pyautogui

#검색어 변경하여 크롤링
#keyword = pyautogui.prompt('크롤링할 검색어 : ')
keyword = input('크롤링할 검색어 : ')
response = requests.get(
    f'https://search.naver.com/search.naver?where=news&sm=tab_jum&query={keyword}')
html = response.text
soup = BeautifulSoup(html, 'html.parser')
links = soup.select('.news_tit')
for link in links:
    title = link.text
    url = link.attrs['href']
    print(title, url)


# -*- coding: utf-8 -*-
"""
指数日线行情
"""
from stockquant.settings import CQ_Config
from stockquant.util.stringhelper import TaskEnum
from stockquant.odl.models import TS_Index_Basic, TS_Index_Daily
from stockquant.util.database import get_new_session, session_scope, engine
from stockquant.util.models import TaskTable
from datetime import datetime
from stockquant.util import logger
from tqdm import tqdm
import tushare as ts
import time
import pandas as pd

_logger = logger.Logger(__name__).get_log()
task = TaskEnum.TS指数日线行情

def update_task():
    """
    更新任务表
    """
    global task

    TaskTable.del_with_task(task)

    with session_scope() as sm:
        query = sm.query(TS_Index_Basic.ts_code, TS_Index_Basic.list_date)
        query = query.filter(TS_Index_Basic.market != "SW")
        codes = query.all()

        tasklist = []
        for c in codes:
            tasktable = TaskTable(
                task=task.value,
                task_name=task.name,
                ts_code=c.ts_code,
                bs_code="NA",
                begin_date=c.list_date if c.list_date else datetime(1990, 12, 19).date(),
                end_date=datetime.now().date(),
            )
            tasklist.append(tasktable)
        sm.bulk_save_objects(tasklist)
    _logger.info("生成{}条任务记录".format(len(codes)))

def _load_data(content: pd.DataFrame, ts_code):
    """
    做一些简单转换后，加载数据到数据库
    """
    table_name = TS_Index_Daily.__tablename__

    if content.empty:
        return

    try:
        content["trade_date"] = pd.to_datetime(content["trade_date"])
        content.to_sql(table_name, engine, if_exists="append", index=False)
    except Exception as e:
        _logger.error(f"{ts_code}-日线行情保存出错/{repr(e)}")

def get_index_daily():
    """
    更新TS指数日线行情
    """
    global task
    pro = ts.pro_api(CQ_Config.TUSHARE_TOKEN)
    sm = get_new_session()
    try:
        rp = sm.query(TaskTable).filter(TaskTable.task == task.value, TaskTable.finished == False)  # noqa
        if CQ_Config.IDB_DEBUG == "1":  # 如果是测试环境
            rp = rp.limit(1000)

        rp = rp.all()

        for task in tqdm(rp):
            if task.finished:
                continue

            max_try = 8  # 失败重连的最大次数
            for i in range(max_try):
                try:
                    df = pro.index_daily(ts_code=task.ts_code)
                    _load_data(content=df, ts_code=task.ts_code)
                    task.finished = True
                    break
                except Exception as e:
                    if i < (max_try - 1):
                        t = (i + 1) * 2
                        time.sleep(t)
                        _logger.warning(f"[{task.ts_code}]异常重连/{repr(e)}")
                        continue
                    else:
                        _logger.error(f"获取指数日线行情失败/获取[{task.ts_code}]日线行情失败：{repr(e)}")
                        raise
        sm.commit()
    except:  # noqa
        sm.commit()
        raise
    finally:
        sm.close()


from queue import PriorityQueue

class Solution:
    def reorganizeString(self, S):
        """
        Similar to Task Scheduler
        """
        letter_map = {}
        queue = PriorityQueue()

        for c in S:
            letter_map[c] = letter_map.get(c, 0) + 1

        for k, v in letter_map.items():
            queue.put((-v, k))

        result = ""

        while not queue.empty():
            f1, l1 = queue.get()
            freq, letter = f1, l1

            if result and result[-1] == letter:
                if queue.empty():
                    return ""
                else:
                    f2, l2 = queue.get()
                    queue.put((f1, l1))
                    freq, letter = f2, l2

            result += letter
            freq = abs(freq) - 1
            if freq > 0:
                queue.put((-freq, letter))

        return result

"""
Also check out Rearrage string k distance apart.
Compare time complexity.

https://www.programcreek.com/2014/08/leetcode-rearrange-string-k-distance-apart-java/
"""


from __future__ import annotations

import sys
from pathlib import Path
from typing import Callable, List, Optional, Tuple, Union, overload

import thot.alignment as ta

from ...corpora.parallel_text_corpus import ParallelTextCorpus
from ...corpora.parallel_text_row import ParallelTextRow
from ...tokenization.tokenizer import Tokenizer
from ...tokenization.whitespace_tokenizer import WHITESPACE_TOKENIZER
from ...utils.progress_status import ProgressStatus
from ...utils.typeshed import StrPath
from ..trainer import Trainer, TrainStats
from .thot_utils import escape_token, escape_tokens
from .thot_word_alignment_model_type import ThotWordAlignmentModelType
from .thot_word_alignment_parameters import ThotWordAlignmentParameters

class ThotWordAlignmentModelTrainer(Trainer):
    @overload
    def __init__(
        self,
        model_type: ThotWordAlignmentModelType,
        corpus: ParallelTextCorpus,
        prefix_filename: Optional[StrPath],
        parameters: ThotWordAlignmentParameters = ThotWordAlignmentParameters(),
        source_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
        target_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
        max_corpus_count: int = sys.maxsize,
    ) -> None:
        ...

    @overload
    def __init__(
        self,
        model_type: ThotWordAlignmentModelType,
        corpus: Tuple[StrPath, StrPath],
        prefix_filename: Optional[StrPath],
        parameters: ThotWordAlignmentParameters = ThotWordAlignmentParameters(),
        source_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
        target_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
    ) -> None:
        ...

    def __init__(
        self,
        model_type: ThotWordAlignmentModelType,
        corpus: Union[ParallelTextCorpus, Tuple[StrPath, StrPath]],
        prefix_filename: Optional[StrPath],
        parameters: ThotWordAlignmentParameters = ThotWordAlignmentParameters(),
        source_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
        target_tokenizer: Tokenizer[str, int, str] = WHITESPACE_TOKENIZER,
        max_corpus_count: int = sys.maxsize,
    ) -> None:
        if isinstance(corpus, tuple) and max_corpus_count != sys.maxsize:
            raise ValueError("max_corpus_count cannot be set when corpus filenames are provided.")

        self._prefix_filename = None if prefix_filename is None else Path(prefix_filename)
        self._parallel_corpus = corpus
        self._max_corpus_count = max_corpus_count
        self.source_tokenizer = source_tokenizer
        self.target_tokenizer = target_tokenizer
        self._stats = TrainStats()

        self._models: List[Tuple[ta.AlignmentModel, int]] = []
        if model_type is ThotWordAlignmentModelType.FAST_ALIGN:
            fast_align = ta.FastAlignModel()
            fast_align.variational_bayes = parameters.get_variational_bayes(model_type)
            if parameters.fast_align_p0 is not None:
                fast_align.fast_align_p0 = parameters.fast_align_p0
            self._models.append((fast_align, parameters.get_fast_align_iteration_count(model_type)))
        else:
            ibm1 = ta.Ibm1AlignmentModel()
            ibm1.variational_bayes = parameters.get_variational_bayes(model_type)
            self._models.append((ibm1, parameters.get_ibm1_iteration_count(model_type)))

            ibm2_or_hmm: Optional[ta.AlignmentModel] = None
            if model_type >= ThotWordAlignmentModelType.IBM2:
                if parameters.get_hmm_iteration_count(model_type) > 0:
                    ibm2_or_hmm = ta.HmmAlignmentModel(ibm1)
                    if parameters.hmm_p0 is not None:
                        ibm2_or_hmm.hmm_p0 = parameters.hmm_p0
                    if parameters.hmm_lexical_smoothing_factor is not None:
                        ibm2_or_hmm.lexical_smoothing_factor = parameters.hmm_lexical_smoothing_factor
                    if parameters.hmm_alignment_smoothing_factor is not None:
                        ibm2_or_hmm.hmm_alignment_smoothing_factor = parameters.hmm_alignment_smoothing_factor
                    self._models.append((ibm2_or_hmm, parameters.get_hmm_iteration_count(model_type)))
                else:
                    ibm2_or_hmm = ta.Ibm2AlignmentModel(ibm1)
                    self._models.append((ibm2_or_hmm, parameters.get_ibm2_iteration_count(model_type)))

            ibm3: Optional[ta.Ibm3AlignmentModel] = None
            if (
                model_type >= ThotWordAlignmentModelType.IBM3
                and ibm2_or_hmm is not None
                and parameters.get_ibm3_iteration_count(model_type) > 0
            ):
                ibm3 = ta.Ibm3AlignmentModel(ibm2_or_hmm)
                if parameters.ibm3_fertility_smoothing_factor is not None:
                    ibm3.fertility_smoothing_factor = parameters.ibm3_fertility_smoothing_factor
                if parameters.ibm3_count_threshold is not None:
                    ibm3.count_threshold = parameters.ibm3_count_threshold
                self._models.append((ibm3, parameters.get_ibm3_iteration_count(model_type)))

            if model_type >= ThotWordAlignmentModelType.IBM4:
                ibm4: Optional[ta.Ibm4AlignmentModel] = None
                if ibm3 is not None:
                    ibm4 = ta.Ibm4AlignmentModel(ibm3)
                elif isinstance(ibm2_or_hmm, ta.HmmAlignmentModel):
                    ibm4 = ta.Ibm4AlignmentModel(ibm2_or_hmm)
                if ibm4 is not None:
                    if parameters.ibm4_distortion_smoothing_factor is not None:
                        ibm4.distortion_smoothing_factor = parameters.ibm4_distortion_smoothing_factor
                    for word, word_class in parameters.source_word_classes.items():
                        ibm4.map_src_word_to_word_class(escape_token(word), word_class)
                    for word, word_class in parameters.target_word_classes.items():
                        ibm4.map_trg_word_to_word_class(escape_token(word), word_class)
                    self._models.append((ibm4, parameters.get_ibm4_iteration_count(model_type)))

        self._max_segment_length = self._model.max_sentence_length

    @property
    def stats(self) -> TrainStats:
        return self._stats

    @property
    def _model(self) -> ta.AlignmentModel:
        return self._models[-1][0]

    def train(
        self,
        progress: Optional[Callable[[ProgressStatus], None]] = None,
        check_canceled: Optional[Callable[[], None]] = None,
    ) -> None:
        num_steps = sum(iterations + 1 for _, iterations in self._models if iterations > 0) + 1
        cur_step = 0

        if progress is not None:
            progress(ProgressStatus.from_step(cur_step, num_steps))

        if isinstance(self._parallel_corpus, ParallelTextCorpus):
            corpus_count = 0
            index = 0
            with self._parallel_corpus.tokenize(self.source_tokenizer, self.target_tokenizer).get_rows() as rows:
                for row in rows:
                    self._model.add_sentence_pair(
                        list(escape_tokens(row.source_segment)), list(escape_tokens(row.target_segment)), 1
                    )
                    if self._is_segment_valid(row):
                        corpus_count += 1
                    index += 1
                    if corpus_count == self._max_corpus_count:
                        break
        else:
            self._model.read_sentence_pairs(str(self._parallel_corpus[0]), str(self._parallel_corpus[1]))
        cur_step += 1
        if progress is not None:
            progress(ProgressStatus.from_step(cur_step, num_steps))
        if check_canceled is not None:
            check_canceled()

        trained_segment_count = 0
        for model, iteration_count in self._models:
            if iteration_count == 0:
                continue

            trained_segment_count = model.start_training()
            cur_step += 1
            if progress is not None:
                progress(ProgressStatus.from_step(cur_step, num_steps))
            if check_canceled is not None:
                check_canceled()

            for _ in range(iteration_count):
                model.train()
                cur_step += 1
                if progress is not None:
                    progress(ProgressStatus.from_step(cur_step, num_steps))
                if check_canceled is not None:
                    check_canceled()
            model.end_training()
        self._stats.train_corpus_size = trained_segment_count

    def save(self) -> None:
        if self._prefix_filename is not None:
            self._model.print(str(self._prefix_filename))

    def close(self) -> None:
        for model, _ in self._models:
            model.clear()
        self._models.clear()

    def __enter__(self) -> ThotWordAlignmentModelTrainer:
        return self

    def _is_segment_valid(self, row: ParallelTextRow) -> bool:
        return (
            not row.is_empty
            and len(row.source_segment) <= self._max_segment_length
            and len(row.target_segment) <= self._max_segment_length
        )


from utils.cityscape_ds_alpha import load_data
import os
import torch
import cv2

def preprocess_edge(inst_map, filename, subset):
    # utils to get the edge of image
    def get_edges(t):
        # zero tensor, prepare to fill with edge (1) and bg (0)
        edge = torch.ByteTensor(t.size()).zero_().to(t.device)
        edge[:, :, :, 1:] = edge[:, :, :, 1:] | (t[:, :, :, 1:] != t[:, :, :, :-1]).byte()
        edge[:, :, :, :-1] = edge[:, :, :, :-1] | (t[:, :, :, 1:] != t[:, :, :, :-1]).byte()
        edge[:, :, 1:, :] = edge[:, :, 1:, :] | (t[:, :, 1:, :] != t[:, :, :-1, :]).byte()
        edge[:, :, :-1, :] = edge[:, :, :-1, :] | (t[:, :, 1:, :] != t[:, :, :-1, :]).byte()
        return edge.float()

    instance_edge_map = get_edges(inst_map)
    # saving coarse-edge-image
    for inst_msk, fn_w_ext in zip(instance_edge_map, filename):
        fn_lst = fn_w_ext.split('_')
        # DA_Data
        if len(fn_lst) == 1:
            fn_name = fn_lst[0].split('.')[0]
            prefix = f"/data1/dataset/Cityscapes/gtFine/{subset}/DA_Data/"
            path = prefix + f"{fn_name}_crEdgeMaps.png"
        # general subset
        elif len(fn_lst) == 4:
            #city, *id_info = fn_lst
            prefix = f"/data1/dataset/Cityscapes/gtFine/{subset}/{fn_lst[0]}/"
            fn_lst[-1] = 'gtFine_crEdgeMaps.png'
            tmp_path = "_".join(fn_lst)
            path = prefix + tmp_path
        else:
            raise ValueError

        #breakpoint()
        #               ( 1 x H x W -> H x W )
        cv2.imwrite(path, inst_msk.numpy()[0]*255.)

def get_cfg_str():
    return '''
    gen_set = 'train'
    [dataset]
        data_dir = '/data1/dataset/Cityscapes'
        resize_size = 540
        subset_type = 'all'
        fn_qry = '*/*.png'
        random_flip = False@bool
        ret_dataset = False@bool

        [dataset.data_ld_kwargs]
            shuffle = False@bool
            batch_size = 8
            num_workers = 6
    '''

if __name__ == "__main__":
    from easy_configer.Configer import Configer
    cfger = Configer()
    cfger.cfg_from_str( get_cfg_str() )

    data_ld = load_data(**cfger.dataset)

    # train subset
    for idx, batch in enumerate(data_ld[cfger.gen_set], 0):
        preprocess_edge(batch["segmap"]['instance'], batch['filename'], cfger.gen_set)

    print('done!')
    # should have 3975..
    #print(f"total mask num : {len(all_lst)}\n")


from copy import copy
from utils.solution import Solution

def flip(cucumbers: list[str]) -> list[str]:
    return [''.join(line[i] for line in cucumbers) for i in range(len(cucumbers[0]))]

def move(cucumbers: list[str], c: str) -> bool:
    moved = False
    for i in range(len(cucumbers)):
        line = cucumbers[i] + cucumbers[i][0] if cucumbers[i].startswith('.') else cucumbers[i]
        replace = line.replace(c + '.', '.' + c)
        line = replace[-1] + replace[1:-1] if len(line) > len(cucumbers[i]) else replace
        if cucumbers[i] != line:
            cucumbers[i] = line
            moved = True

    return moved

def part1(input: list[str]) -> int:
    cucumbers = copy(input)
    i = 0
    moving = True
    while moving:
        moving = False
        for c in ['>', 'v']:
            if move(cucumbers, c): moving = True
            cucumbers = flip(cucumbers)

        i += 1

    return i

def part2(input: list[str]) -> int:
    return 0

solution = Solution(25, part1, part2, (58, 0))
solution.run()


"""ImageNet workload implemented in Jax.

Forked from the Flax ImageNet Example v0.3.3
https://github.com/google/flax/tree/v0.3.3/examples/imagenet.
"""

import functools
import itertools
import math
from typing import Dict, Iterator, Optional, Tuple

from flax import jax_utils
import jax
from jax import lax
import jax.numpy as jnp
import optax
import tensorflow_datasets as tfds

from algorithmic_efficiency import param_utils
from algorithmic_efficiency import random_utils as prng
from algorithmic_efficiency import spec
from algorithmic_efficiency.workloads.imagenet_resnet import imagenet_v2
from algorithmic_efficiency.workloads.imagenet_resnet.imagenet_jax import \
    input_pipeline
from algorithmic_efficiency.workloads.imagenet_resnet.imagenet_jax import \
    models
from algorithmic_efficiency.workloads.imagenet_resnet.workload import \
    BaseImagenetResNetWorkload

class ImagenetResNetWorkload(BaseImagenetResNetWorkload):

  def _build_dataset(
      self,
      data_rng: spec.RandomState,
      split: str,
      data_dir: str,
      global_batch_size: int,
      cache: Optional[bool] = None,
      repeat_final_dataset: Optional[bool] = None,
      use_mixup: bool = False,
      use_randaug: bool = False) -> Iterator[Dict[str, spec.Tensor]]:
    if split == 'test':
      np_iter = imagenet_v2.get_imagenet_v2_iter(
          data_dir,
          global_batch_size,
          mean_rgb=self.train_mean,
          stddev_rgb=self.train_stddev,
          image_size=self.center_crop_size,
          resize_size=self.resize_size)
      return itertools.cycle(np_iter)

    ds_builder = tfds.builder('imagenet2012:5.1.0', data_dir=data_dir)
    train = split == 'train'
    ds = input_pipeline.create_input_iter(
        split,
        ds_builder,
        data_rng,
        global_batch_size,
        self.train_mean,
        self.train_stddev,
        self.center_crop_size,
        self.resize_size,
        self.aspect_ratio_range,
        self.scale_ratio_range,
        train=train,
        cache=not train if cache is None else cache,
        repeat_final_dataset=repeat_final_dataset,
        use_mixup=use_mixup,
        mixup_alpha=0.2,
        use_randaug=use_randaug)
    return ds

  def sync_batch_stats(
      self, model_state: spec.ModelAuxiliaryState) -> spec.ModelAuxiliaryState:
    """Sync the batch statistics across replicas."""
    # An axis_name is passed to pmap which can then be used by pmean.
    # In this case each device has its own version of the batch statistics and
    # we average them.
    avg_fn = jax.pmap(lambda x: lax.pmean(x, 'x'), 'x')
    new_model_state = model_state.copy(
        {'batch_stats': avg_fn(model_state['batch_stats'])})
    return new_model_state

  def init_model_fn(
      self,
      rng: spec.RandomState,
      dropout_rate: Optional[float] = None,
      aux_dropout_rate: Optional[float] = None) -> spec.ModelInitState:
    """Dropout is unused."""
    del dropout_rate
    del aux_dropout_rate
    model_cls = getattr(models, 'ResNet50')
    model = model_cls(num_classes=self._num_classes, dtype=jnp.float32)
    self._model = model
    input_shape = (1, 224, 224, 3)
    variables = jax.jit(model.init)({'params': rng},
                                    jnp.ones(input_shape, model.dtype))
    model_state, params = variables.pop('params')
    self._param_shapes = param_utils.jax_param_shapes(params)
    self._param_types = param_utils.jax_param_types(self._param_shapes)
    model_state = jax_utils.replicate(model_state)
    params = jax_utils.replicate(params)
    return params, model_state

  def is_output_params(self, param_key: spec.ParameterKey) -> bool:
    return param_key == 'Dense_0'

  @functools.partial(
      jax.pmap,
      axis_name='batch',
      in_axes=(None, 0, 0, 0, 0),
      static_broadcasted_argnums=(0,))
  def _eval_model(self,
                  params: spec.ParameterContainer,
                  batch: Dict[str, spec.Tensor],
                  model_state: spec.ModelAuxiliaryState,
                  rng: spec.RandomState) -> Dict[str, spec.Tensor]:
    logits, _ = self.model_fn(
        params,
        batch,
        model_state,
        spec.ForwardPassMode.EVAL,
        rng=rng,
        update_batch_norm=False)
    weights = batch.get('weights')
    return self._compute_metrics(logits, batch['targets'], weights)

  def model_fn(
      self,
      params: spec.ParameterContainer,
      augmented_and_preprocessed_input_batch: Dict[str, spec.Tensor],
      model_state: spec.ModelAuxiliaryState,
      mode: spec.ForwardPassMode,
      rng: spec.RandomState,
      update_batch_norm: bool) -> Tuple[spec.Tensor, spec.ModelAuxiliaryState]:
    del mode
    del rng
    variables = {'params': params, **model_state}
    if update_batch_norm:
      logits, new_model_state = self._model.apply(
          variables,
          augmented_and_preprocessed_input_batch['inputs'],
          update_batch_norm=update_batch_norm,
          mutable=['batch_stats'])
      return logits, new_model_state
    else:
      logits = self._model.apply(
          variables,
          augmented_and_preprocessed_input_batch['inputs'],
          update_batch_norm=update_batch_norm,
          mutable=False)
      return logits, model_state

  # Does NOT apply regularization, which is left to the submitter to do in
  # `update_params`.
  def loss_fn(
      self,
      label_batch: spec.Tensor,  # Dense or one-hot labels.
      logits_batch: spec.Tensor,
      mask_batch: Optional[spec.Tensor] = None,
      label_smoothing: float = 0.0) -> Dict[str, spec.Tensor]:  # differentiable
    """Evaluate the (masked) loss function at (label_batch, logits_batch).

    Return {'summed': scalar summed loss, 'n_valid_examples': scalar number of
    valid examples in batch, 'per_example': 1-d array of per-example losses}
    (not synced across devices).
    """
    if label_batch.shape[-1] != self._num_classes:
      one_hot_labels = jax.nn.one_hot(
          label_batch, num_classes=self._num_classes)
    else:
      one_hot_labels = label_batch
    smoothed_labels = optax.smooth_labels(one_hot_labels, label_smoothing)
    per_example_losses = -jnp.sum(
        smoothed_labels * jax.nn.log_softmax(logits_batch, axis=-1), axis=-1)
    # mask_batch is assumed to be shape [batch].
    if mask_batch is not None:
      per_example_losses *= mask_batch
      n_valid_examples = mask_batch.sum()
    else:
      n_valid_examples = len(per_example_losses)
    summed_loss = per_example_losses.sum()
    return {
        'summed': summed_loss,
        'n_valid_examples': n_valid_examples,
        'per_example': per_example_losses,
    }

  def _compute_metrics(self,
                       logits: spec.Tensor,
                       labels: spec.Tensor,
                       weights: spec.Tensor) -> Dict[str, spec.Tensor]:
    if weights is None:
      weights = jnp.ones(len(logits))
    summed_loss = self.loss_fn(labels, logits, weights)['summed']
    # not accuracy, but nr. of correct predictions
    accuracy = jnp.sum((jnp.argmax(logits, -1) == labels) * weights)
    metrics = {
        'loss': summed_loss,
        'accuracy': accuracy,
    }
    metrics = lax.psum(metrics, axis_name='batch')
    return metrics

  def _eval_model_on_split(self,
                           split: str,
                           num_examples: int,
                           global_batch_size: int,
                           params: spec.ParameterContainer,
                           model_state: spec.ModelAuxiliaryState,
                           rng: spec.RandomState,
                           data_dir: str,
                           global_step: int = 0) -> Dict[str, float]:
    del global_step
    if model_state is not None:
      # Sync batch statistics across replicas before evaluating.
      model_state = self.sync_batch_stats(model_state)
    num_batches = int(math.ceil(num_examples / global_batch_size))
    data_rng, eval_rng = prng.split(rng, 2)
    # We already repeat the dataset indefinitely in tf.data.
    if split not in self._eval_iters:
      self._eval_iters[split] = self._build_input_queue(
          data_rng,
          split=split,
          global_batch_size=global_batch_size,
          data_dir=data_dir,
          cache=True,
          repeat_final_dataset=True,
          num_batches=num_batches)

    eval_metrics = {}
    for bi in range(num_batches):
      eval_rng = prng.fold_in(eval_rng, bi)
      step_eval_rngs = prng.split(eval_rng, jax.local_device_count())
      batch = next(self._eval_iters[split])
      # We already average these metrics across devices inside _compute_metrics.
      synced_metrics = self._eval_model(params,
                                        batch,
                                        model_state,
                                        step_eval_rngs)
      for metric_name, metric_value in synced_metrics.items():
        if metric_name not in eval_metrics:
          eval_metrics[metric_name] = 0.0
        eval_metrics[metric_name] += metric_value

    eval_metrics = jax.tree_map(lambda x: float(x[0] / num_examples),
                                eval_metrics)
    return eval_metrics