import logging
import re

from telegram.ext import CommandHandler, Dispatcher, CallbackContext
from telegram import Bot, Update

from .services import (
    get_random_quote_by_tag, get_random_quote, store_quote, get_keyword_quote_count,
    get_stream_list_by_game
)

logger = logging.getLogger(__name__)
# for dev env
# logging.basicConfig(level=logging.DEBUG,
#                     format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

def help(bot: Bot, update: Update):
    bot.sendMessage(update.message.chat_id, text="""
                         reply message and add  /keyword or /k after with stop word to create stop word -> quote relation.\n
                         - /random or /r - show random quote \n
                         - /count or /c - show random quote \n
                         - /st or /stream - following game name (/st dota2) to retrieve top streams (NEW) \n
                         """
                    )

def error(bot: Bot, update: Update, error_text: str = 'Command not found .'):
    bot.sendMessage(update.message.chat_id, text=error_text)

def build_menu(buttons,
               n_cols,
               header_buttons=None,
               footer_buttons=None):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, [header_buttons])
    if footer_buttons:
        menu.append([footer_buttons])
    return menu

# def snow_camera(bot: Bot, update: Update):
#     location = re.sub(r'/[camera|cam]+', '', update.message.text).strip().lower()
#
#     try:
#         location_obj = Location.objects.get(Q(title_en__iexact=location) | Q(title_uk__iexact=location))
#     except Location.DoesNotExist:
#         error(bot, update,
#               "Location is not supported yet.")
#         return
#
#     cameras_map = location_obj.cameras.values_list("title_uk", "cam_id")
#
#     button_list = [
#         InlineKeyboardButton(camera[0], callback_data=f"cam={camera[1]}")
#         for camera in cameras_map
#     ]
#
#     reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
#     bot.send_message(chat_id=update.message.chat_id, text="Please select camera", reply_markup=reply_markup)
#

# def snow_camera_gif(bot: Bot, update: Update):
#     location = re.sub(r'/[camera_vid|camv]+', '', update.message.text).strip().lower()
#
#     try:
#         location_obj = Location.objects.get(Q(title_en__iexact=location) | Q(title_uk__iexact=location))
#     except Location.DoesNotExist:
#         error(bot, update,
#               "Location is not supported yet.")
#         return
#
#     cameras_map = location_obj.cameras.values_list("title_uk", "cam_id")
#
#     button_list = [
#         InlineKeyboardButton(camera[0], callback_data=f"camv={camera[1]}")
#         for camera in cameras_map
#     ]
#
#     reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
#     bot.send_message(chat_id=update.message.chat_id, text="Please select camera", reply_markup=reply_markup)
#
#
# def weather(bot: Bot, update: Update):
#     try:
#         city = re.sub(r'/[weather|w]+', '', update.message.text).strip()
#         weather = get_weather(city=city)
#         bot.sendMessage(update.message.chat_id, text=weather)
#     except Exception as e:
#         logger.exception('Failed to get weather. Error', exc_info=e)

def quote(update: Update, context: CallbackContext):
    try:
        text, author = update.message.reply_to_message.text, update.message.reply_to_message.from_user.full_name
        stripped_stop_word = re.sub(r'/[store|s]+', '', update.message.text).strip()
        if stripped_stop_word:
            store_quote(text=text, author=author, tag_text=stripped_stop_word)
            context.bot.sendMessage(update.message.chat_id, text='I stored your quote from {}!'.format(author))
        else:
            context.bot.sendMessage(update.message.chat_id,
                            text='Stop word missing. Please provide stop word text after `/store` command! Thanks!')
    except Exception as e:
        logger.exception('Failed to store quote. Error', exc_info=e)

def random_by_stop_word(update: Update, context: CallbackContext):
    quote = get_random_quote_by_tag(message_text=update.message.text)
    context.bot.send_message(chat_id=update.message.chat_id, text=quote, parse_mode='HTML')

def random(update: Update, context: CallbackContext):
    context.bot.send_message(chat_id=update.message.chat_id, text=get_random_quote(), parse_mode='HTML')

def quote_count_by_keyword(update: Update, context: CallbackContext):
    try:
        keyword = re.sub(r'/[count|c]+', "", update.message.text).strip()
        message = get_keyword_quote_count(keyword=keyword)
        context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='HTML')
    except Exception as e:
        logger.exception('Failed to get count for keyword.', exc_info=e)

def get_streams_by_game(bot: Bot, update: Update):
    try:
        game = re.sub(r'/[stream|st]+', "", update.message.text).strip()

        message = get_stream_list_by_game(game_name=game)
        bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='HTML')
    except Exception as e:
        bot.sendMessage(update.message.chat_id,
                        text='There are no streams or game name was not specific enough.')
        logger.exception('Failed to get count for keyword.', exc_info=e)

def register(dispatcher: Dispatcher):
    """
    this method will be called on start of application
    and register bot callbacks
    """
    # quotes
    dispatcher.add_handler(CommandHandler(["store", "s"], quote))
    dispatcher.add_handler(CommandHandler(["random", "r"], random))
    dispatcher.add_handler(CommandHandler("help", help))
    dispatcher.add_handler(CommandHandler(["keyword", "k"], random_by_stop_word))
    dispatcher.add_handler(CommandHandler(["count", "c"], quote_count_by_keyword))
    # dispatcher.add_handler(CommandHandler(["weather", "w"], weather))
    # dispatcher.add_handler(CallbackQueryHandler(camera_handler, pattern="^cam=\d+$"))
    # dispatcher.add_handler(CallbackQueryHandler(camera_handler, pattern="^camv=\d+$"))

    # twitch
    # not needed for now
    # dispatcher.add_handler(CommandHandler(["stream", "st"], get_streams_by_game))

    # snow cameras
    # dispatcher.add_handler(CommandHandler(["camera", "cam"], snow_camera))
    # dispatcher.add_handler(CommandHandler(["camera_gif", "camv"], snow_camera_gif))

    # TODO: make this react to messages with some sane timeout, e.g. sent msg from bot not more then 20 in day
    # dispatcher.add_handler(MessageHandler(Filters.text, random_by_stop_word), group=1)
    # dispatcher.add_error_handler(error)


def add(num_1):
    print (final)
    print (first + second)
def subtract(num_2):
    print (final)
    print (first - second)
def multiply (num_3):
    print (final)
    print (first * second)
def divide (num_4):
    print (final)
    print (first / second)

while True:

  first = int(input("Enter first number: "))
  second = int(input("Enter the second number: "))
  final = ('Your final answer is ')
  choice = int(input('Enter a number a number 1-4 for the following choices:\n'
  "1 = add\n"
  "2 = subtract\n"
  "3 = multiply\n"
  "4 = divide\n"))
  if choice == 1:
    add(first)
  if choice == 2:
    subtract(first)
  if choice == 3:
    multiply(first)
  if choice == 4:
    divide(first)

  cont = input("Another one? yes/no > ")
  while cont.lower() not in ("yes","no"):
        cont = input("Another one? yes/no > ")
  if cont == "no":
    break


from PySimpleGUI import PySimpleGUI as sg
from Conversor import Read_xml as rx
from CorrigeDataEntrada import corretorData as cd

sg.theme('Light Grey')
sg.popup('Para o correto funcionamento deste programa deverá seguir as seguintes instruções: \n 1°-Deixar salvo o relatório excel enviado pela empresa no mesmo diretório onde estão os XMLs;\n2°-No arquivo excel os cabeçalhos devem ser o mesmo do excel executado no mês anterior\n3°-Colar o diretório onde estão os XMLs no campo próprio neste sistema')
layout = [
    [sg.Text(text = 'Conversor de XML para Leiaute Dominio com Separador',font=('Arial Bold',12),justification='center')],
    [sg.Text('Pasta de arquivos XMLs:'), sg.Input(key='pasta')],
    [sg.Button('Converter')]
]

janela = sg.Window('Conversor', layout,)

while True:
    evento, valores = janela.read()
    if evento == sg.WIN_CLOSED:
        break
    if evento == 'Converter':
        a = cd(valores['pasta'])
        a = a.executaCorrecao()
        x = rx(valores['pasta'])
        x = x.contaArquivos(valores['pasta'])
        sg.Popup(f'Foram convertidos {x} arquivos! \n \n Atividade executada com sucesso!')
        sg.Popup('Lembre-se que deverá ser conferido a pasta "Avisos", pois, caso haja algum erro deverá ser solucionado antes de importar os arquivos para o Domínio.')
janela.close()


def round_up(number):
    return int(number + 1)

def sort_table_by_year_and_title(table):
    sorted_table = []
    while len(table) > 0:
        min_i = 0
        min_row = table[min_i]
        min_game_title = min_row[0]
        min_game_year = int(min_row[2])
        for i in range(1, len(table)):
            row = table[i]
            game_title = row[0]
            game_year = int(row[2])
            newer_game = game_year > min_game_year
            same_year = game_year == min_game_year
            # need lower() because uppercase letters are 'less than'
            # their lowercase counterparts: 'A' < 'a' == True
            earlier_title = game_title.lower() < min_game_title.lower()
            if newer_game or (same_year and earlier_title):
                min_i = i
                min_row = row
                min_game_title = game_title
                min_game_year = game_year
        sorted_table.append(min_row)
        del table[min_i]
    table.extend(sorted_table)

def get_table(file_name):
    table = []
    with open(file_name, 'r') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip('\n')
            parts = line.split('\t')
            table.append(parts)
    return table

def get_most_played(file_name):
    most_played_title = None
    max_units_sold = None
    table = get_table(file_name)
    for row in table:
        game_title = row[0]
        units_sold = float(row[1])
        if max_units_sold is None or max_units_sold < units_sold:
            max_units_sold = units_sold
            most_played_title = game_title
    return most_played_title

def sum_sold(file_name):
    sum_units_sold = 0
    table = get_table(file_name)
    for row in table:
        units_sold = float(row[1])
        sum_units_sold += units_sold
    return sum_units_sold

def get_selling_avg(file_name):
    table = get_table(file_name)
    table_length = len(table)
    if table_length == 0:
        return 0
    return sum_sold(file_name) / table_length

def count_longest_title(file_name):
    longest_title_length = None
    table = get_table(file_name)
    for row in table:
        game_title_length = len(row[0])
        if longest_title_length is None \
                or longest_title_length < game_title_length:
            longest_title_length = game_title_length
    return longest_title_length

def get_date_avg(file_name):
    table = get_table(file_name)
    table_length = len(table)
    if table_length == 0:
        return 0
    sum_year = 0
    for row in table:
        game_year = int(row[2])
        sum_year += game_year
    return round_up(sum_year / table_length)

def get_game(file_name, title):
    table = get_table(file_name)
    for row in table:
        game_title = row[0]
        if title == game_title:
            return [
                row[0],
                float(row[1]),
                int(row[2]),
                row[3],
                row[4],
            ]

def count_grouped_by_genre(file_name):
    genre_count = {}
    table = get_table(file_name)
    for row in table:
        game_genre = row[3]
        if game_genre in genre_count:
            genre_count[game_genre] += 1
        else:
            genre_count[game_genre] = 1
    return genre_count

def get_date_ordered(file_name):
    table = get_table(file_name)
    sort_table_by_year_and_title(table)
    titles = []
    for row in table:
        game_title = row[0]
        titles.append(game_title)
    return titles


"""Utility functions for nautobot_chatops integrations."""
from collections.abc import Mapping
from importlib import import_module
from pathlib import Path
from types import ModuleType
from typing import Generator

from django.conf import settings

from nautobot_chatops.utils import logger

def _each_integration() -> Generator[str, None, None]:
    """Return all integrations."""
    for path in Path(__file__).parent.iterdir():
        if (path / "worker.py").is_file():
            yield path.name

ALL_INTEGRATIONS = set(_each_integration())

def _each_enabled_integration() -> Generator[str, None, None]:
    """Return all enabled integrations."""
    if not isinstance(settings.PLUGINS_CONFIG, Mapping):
        raise TypeError("Django `settings.PLUGINS_CONFIG` must be a Mapping")

    config = settings.PLUGINS_CONFIG["nautobot_chatops"]
    if not isinstance(config, Mapping):
        raise TypeError("nautobot_chatops config must be a Mapping")

    for name in ALL_INTEGRATIONS:
        if config.get(f"enable_{name}", False):
            yield name

ENABLED_INTEGRATIONS = set(_each_enabled_integration())
DISABLED_INTEGRATIONS = ALL_INTEGRATIONS - ENABLED_INTEGRATIONS

def each_enabled_integration_module(module_name: str) -> Generator[ModuleType, None, None]:
    """For each enabled integration, import the module name."""
    for name in ENABLED_INTEGRATIONS:
        try:
            module = import_module(f"nautobot_chatops.integrations.{name}.{module_name}")
        except ModuleNotFoundError:
            logger.debug("Integration %s does not have a %s module, skipping.", name, module_name)
            continue

        yield module


dnas = []
with open('rosalind_ba2e.txt', 'r') as f:
    inp = f.readline().replace('\n', '').split()
    k = int(inp[0])
    t = int(inp[1])
    for line in f.readlines():
        dnas.append(line.replace('\n', ''))

seq2 = {'A':0, 'C':1, 'G':2, 'T':3}
best = []
for i in range(t):
    best.append(dnas[i][:k])
for i in range(len(dnas[0])+1-k):
    temp = [dnas[0][i:i+k]]
    for j in range(1, t):
        prob = []
        for i2 in range(k):
            seq1 = {'A': 1, 'C': 1, 'G': 1, 'T': 1}
            for m in temp:
                seq1[m[i2]] += 1
            pT = []
            for key in seq1.keys():
                pT.append(seq1[key]/(len(temp)+4))
            prob.append(pT)
        probMax = 0
        for i2 in range(len(dnas[j])+1-k):
            stringT = dnas[j][i2:i2+k]
            probTemp = 1
            for sub in range(len(stringT)):
                probTemp *= prob[sub][seq2[stringT[sub]]]
            if probMax < probTemp:
                probMax = probTemp
                newPattern = stringT
        temp.append(newPattern)
    maxProbString = ''
    for i2 in range(k):
        seq1 = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
        for m in temp:
            seq1[m[i2]] += 1
        maximum = 0
        for key in seq1.keys():
            pT = seq1[key] / (len(temp) + 4)
            if pT > maximum:
                maximum = pT
                newGene = key
        maxProbString = maxProbString + newGene
    distance1 = 0
    distance2 = 0
    for m in temp:
        for j in range(k):
            if m[j] != maxProbString[j]:
                distance1 += 1

    maxProbString = ''
    for i2 in range(k):
        seq1 = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
        for m in best:
            seq1[m[i2]] += 1
        maximum = 0
        for key in seq1.keys():
            pT = seq1[key] / (len(best) + 4)
            if pT > maximum:
                maximum = pT
                newGene = key
        maxProbString = maxProbString + newGene
    for m in best:
        for j in range(k):
            if m[j] != maxProbString[j]:
                distance2 += 1
    if distance1 < distance2:
        best = temp.copy()

for i in range(len(best)):
    print(best[i])

Implement GreedyMotifSearch with Pseudocounts/rosalind_ba2e_869_1_code.py


import turtle
import pandas
from state import State
from scoreboard import Scoreboard

states = pandas.read_csv("50_states.csv").reset_index()

screen = turtle.Screen()
screen.title("U.S. States Game")

image = "blank_states_img.gif"
screen.addshape(image)
turtle.shape(image)

# I we wanted to get the coordinates:
# def get_mouse_click_coordinates(x, y):
#     print(x, y)

# turtle.onscreenclick(get_mouse_click_coordinates)

scoreboard = Scoreboard()

def fill_non_guessed(guessed_states, states):
    for state_name in list(states.state[~states.state.isin(guessed_states)].values):
        new_state = states[states.state == state_name]
        text_on_map = State("red", state_name, new_state.x.item(), new_state.y.item())

correct_states = []
game_is_on = True
answer_state = ""

while game_is_on:
    if answer_state in correct_states:
        next
    elif answer_state in states.state.values:
        new_state = states[states.state == answer_state]
        correct_states.append(answer_state)
        text_on_map = State("black", new_state.state.item(), new_state.x.item(), new_state.y.item())
        scoreboard.increase_score()
    elif answer_state.lower() == "give up":
        game_is_on = False
        fill_non_guessed(correct_states, states)
        break

    if len(correct_states) == 50:
        game_is_on = False

    answer_state = screen.textinput(title = "Guess the state", prompt = "What's the state name? Or 'Give up'").title()

# this is an alternative to screen.exitonclick() to avoid it extiting
turtle.mainloop()


from array import array
from collections import Counter

word = array('B', input('word: ').encode())

def count_subpalindroms(s: str) -> int:

    numof_letters = Counter(s)

    counter = len(s)

    while s:
        first_letter = s[0]
        last_letter = s[-1]

    return counter

print(29876543 % count_subpalindroms(word))


import json
import logging
from asyncio import sleep
from collections.abc import Callable
from dataclasses import dataclass
from pathlib import Path
from typing import TypeGuard

from watchdog.events import (
    EVENT_TYPE_CREATED,
    EVENT_TYPE_DELETED,
    EVENT_TYPE_MODIFIED,
    EVENT_TYPE_MOVED,
    FileSystemEvent,
    FileSystemEventHandler,
    FileSystemMovedEvent,
)
from watchdog.observers import Observer

logger = logging.getLogger(__name__)

@dataclass
class NewLineEvent:
    file_path: Path
    line_content: str

ENCODING = "utf-8"

class LogWatcherHandler(FileSystemEventHandler):
    """Handler of watchdog events"""

    def __init__(self, data_folder: str, handler: Callable[[NewLineEvent], None]):
        self.file_positions_map: dict[str, int] = {}
        self.line_process_func = handler
        if not Path(data_folder).exists():
            raise ValueError(f"Logwatcher data folder is missing: {data_folder}")
        self.state_file_path = Path(data_folder).joinpath("log_watcher_state.json")
        if not self.state_file_path.exists():
            return  # This is ok on first startup
        with open(self.state_file_path) as fh:
            state = json.load(fh)
            self.file_positions_map = state["file_pointers"]

    def _is_moved_event(
        self, event: FileSystemEvent
    ) -> TypeGuard[FileSystemMovedEvent]:
        """TypeGuard to help type checker detect moved event class"""
        return event.event_type == EVENT_TYPE_MOVED

    def process_new_lines(self, file_path: Path):
        """Process file to detect new lines appended"""

        # Reset position if it looks like file has been truncated
        if file_path.stat().st_size < self.file_positions_map[str(file_path)]:
            self.file_positions_map[str(file_path)] = 0

        with open(file_path, encoding=ENCODING) as file:
            file.seek(self.file_positions_map[str(file_path)])
            new_data = file.read()
            if not new_data:
                return

            # Process all lines but the last one
            for line in new_data.splitlines(keepends=True):
                if not line.endswith("\n"):
                    continue
                self.line_process_func(
                    NewLineEvent(file_path=file_path, line_content=line.strip())
                )
                self.file_positions_map[str(file_path)] += len(
                    line.encode(encoding=ENCODING)
                )

    def on_any_event(self, event: FileSystemEvent):
        """Function called by watch dog when event occurs"""
        if event.is_directory or event.event_type not in [
            EVENT_TYPE_CREATED,
            EVENT_TYPE_MODIFIED,
            EVENT_TYPE_MOVED,
            EVENT_TYPE_DELETED,
        ]:
            return

        if event.event_type in [
            EVENT_TYPE_CREATED,
            EVENT_TYPE_MODIFIED,
        ]:
            if event.src_path not in self.file_positions_map:
                self.file_positions_map[event.src_path] = 0
            try:
                self.process_new_lines(Path(event.src_path))
            except FileNotFoundError:  # pragma: no cover
                pass

        elif self._is_moved_event(event):
            if event.src_path in self.file_positions_map:
                self.file_positions_map[event.dest_path] = self.file_positions_map[
                    event.src_path
                ]
                del self.file_positions_map[event.src_path]
            try:
                self.process_new_lines(Path(event.dest_path))
            except FileNotFoundError:  # pragma: no cover
                pass

        elif event.event_type == EVENT_TYPE_DELETED:
            # Cleanup to limit memory footprint + allow file name to be reused
            if event.src_path in self.file_positions_map:
                del self.file_positions_map[event.src_path]

        with open(self.state_file_path, "w") as fh:
            json.dump({"file_pointers": self.file_positions_map}, fh)

class LogWatcher:
    """Watch log files and call the handler function for every new line appended

    The handler function is called only once the line is completed (i.e. ending with a
    carriage return character).

    Moved, deleted and truncated files are supported.

    Nested files are watched as well if `recursive` is True.

    Some limitations:
    - files must not be truncated and recreated bigger than before within few
    milliseconds
    - watchdog issues applies (e.g. special combinations of host an container OSes or
    special mount types might provide issues)
    """

    def __init__(
        self,
        watched_folder: str,
        data_folder: str,
        handler: Callable[[NewLineEvent], None],
        *,
        recursive: bool = True,
    ) -> None:
        self.watched_folder = Path(watched_folder)
        self.event_handler = LogWatcherHandler(data_folder=data_folder, handler=handler)
        self.recursive = recursive
        self.observer = Observer()

    async def run_async(self):
        """Watch directory"""
        self.process_existing_files()

        self.observer.schedule(  # pyright: ignore[reportUnknownMemberType]
            self.event_handler, self.watched_folder, recursive=self.recursive
        )

        self.observer.start()

        while self.observer.is_alive():
            self.observer.join(1)
            # perform a very small sleep, just to let the coroutine pause
            await sleep(0.001)

        self.observer.join()

    def run_sync(self):
        """Watch directory"""
        self.process_existing_files()

        self.observer.schedule(  # pyright: ignore[reportUnknownMemberType]
            self.event_handler, self.watched_folder, recursive=self.recursive
        )

        self.observer.start()

        while self.observer.is_alive():
            self.observer.join(1)

        self.observer.join()

    def stop(self):
        """Stop watcher"""
        if self.observer.is_alive():
            self.observer.stop()

    def process_existing_files(self):
        """Process files that are already there at watcher startup"""
        for file in self.watched_folder.rglob("*"):
            if not file.is_file():
                continue
            # Let's consider that all existing files have been modified, so that we
            # process any line that might have appeared since our last execution
            event = FileSystemEvent(str(file))
            event.is_directory = False
            event.event_type = EVENT_TYPE_MODIFIED
            self.event_handler.on_any_event(event)


from xml.dom.minidom import parse

# SEED CLASS
# name: display name in game
# rarity: on a scale of 0 beyond (currently 0-2, common uncommon and rare seeds)
# climate: number 0-4 to show which climate the seed grows best in (polar,continental,temperate,dry,tropical)
# point_value: how many points the seed is worth when held at the end of the game
class Seed:
    def __init__(self, name, rarity, climate, point_value):
        self.name = name
        self.rarity = int(rarity)
        self.climate = climate
        self.point_value = point_value

class Contract:
    def __init__(self, seeds, point_value, round_reward, instant_reward, duration):
        self.seeds = seeds
        self.point_value = point_value
        self.round_reward = round_reward
        self.instant_reward = instant_reward
        self.duration = duration

class Deck:
    # list of seeds that are currently in the deck, will be populated by individual seed objects once deck is populated
    seeds = []
    contracts = []

    def __init__(self, filename):
        self.filename = filename

    def populate(self):
        deck_doc = parse(self.filename)
        deck_element = deck_doc.documentElement
        seed_nodes = deck_element.getElementsByTagName("seed")
        for i in range(len(seed_nodes)):
            current_node = seed_nodes[i]
            num_seeds = int(current_node.getAttribute("quantity"))
            # makes a copy of the seed object to add to the deck according to the quantity stated for the deck
            # possibly change so it doesnt call constructor every time, instead it should duplicate original to ^ speed
            for j in range(num_seeds):
                current_seed = Seed(current_node.getAttribute("name"), current_node.getAttribute("rarity"),
                                    current_node.getAttribute("climate"), current_node.getAttribute("point_value"))
                self.seeds.append(current_seed)

class Vault:
    def __init__(self, capacity):
        if type(capacity) == int:
            self.capacity = capacity
        elif type(capacity) == str:
            self.capacity = int(capacity)
        self.seeds = []

class Player:
    def __init__(self, vault_size, coins, number):
        self.vault = Vault(vault_size)
        self.coins = coins
        self.current_contracts = []
        self.completed_contracts = []
        self.number = number

class Game:
    players = []
    market = []
    discard = []

    def __init__(self, num_rounds, num_players, deck_name, seeds_per_market, filter_per_market, starting_coins,
                 starting_vault_size):
        self.current_round = 0
        self.num_rounds = int(num_rounds)

        self.num_players = int(num_players)
        self.deck = Deck(deck_name + ".xml")
        self.deck.populate()

        self.seeds_per_market = int(seeds_per_market)
        self.filter_per_market = int(filter_per_market)

        for i in range(self.num_players):
            self.players.append(Player(int(starting_vault_size), int(starting_coins), i))


import random
board = {1:"1", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9"}
next_move = []
def proverka():
    while True:
        cifra = input()
        try:
            cifra = int(cifra)
            if cifra <= 0:
                print("Вы вели число с меньшим значением, чем надо. Повоторите пожалуйста")
                return proverka()
            elif cifra >= 10:
                print("Вы вели число с больше значением, чем надо. Повоторите пожалуйста")
                return proverka()
            else:
                try:
                    next_move.index(cifra)
                    print("Данная клетка занята. Повторите пожалуйста")
                    return proverka()
                except ValueError:
                    print("Вы вели правильно")
                    next_move.append(cifra)
                    return cifra
        except ValueError:
            print("Вы вели букву. Ведите число")

def playx(a):
    board.update({a:"X"})
    return draw_board()

def playy(b):
    board.update({b:"0"})
    return draw_board()

def draw_board():
    print("-------------")
    print("|", board[1] , "|", board[2] , "|", board[3] , "|")
    print("-------------")
    print("|", board[4] , "|", board[5] , "|", board[6] , "|")
    print("-------------")
    print("|", board[7] , "|", board[8] , "|", board[9] , "|")
    print("-------------")
    if board[1] == board[2] == board[3]:
        print("Вы виграли ")
        raise SystemExit
    elif board[4] == board[5] == board[6]:
        print("Вы виграли ")
        raise SystemExit
    elif board[7] == board[8] == board[9]:
        print("Вы виграли ")
        raise SystemExit
    elif board[1] == board[4] == board[7]:
        print("Вы виграли ")
        raise SystemExit
    elif board[2] == board[5] == board[8]:
        print("Вы виграли ")
        raise SystemExit
    elif board[3] == board[6] == board[9]:
        print("Вы виграли ")
        raise SystemExit
    elif board[1] == board[5] == board[9]:
        print("Вы виграли ")
        raise SystemExit
    elif board[7] == board[5] == board[3]:
        print("Вы виграли ")
        raise SystemExit
    elif len(next_move) == 9:
        print("Ничья")
        raise SystemExit
print("Добро пожаловать в игру Х и О")
draw_board()
print("Для того, чтоб ходить, вам надо вести чилос, куда вы должны поставить X или 0")
while range(5):
        print("Ходить первый игрок X")
        first = proverka()
        playx(first)
        print("Ходтив второй игрок 0")
        second = proverka()
        playy(second)


#! /usr/bin/env python3

"""Predict mirtron with probability using learned latent variables. "yes" means "is a    mirtron"(probability >=0.8) ."no" means "is not a mirtron"(probability <=0.2). "not sure" (0.2 < probability < 0.8)
"""

from sklearn import svm
import sys
sys.path.append("./autoencoder")
import dataProcess
import numpy as np
from keras.models import load_model

x_cast = {"A":[1,0,0,0],"U":[0,1,0,0],\
              "T":[0,1,0,0],"G":[0,0,1,0],\
              "C":[0,0,0,1],"N":[0,0,0,0]}
y_cast = {"TRUE": 1,"FALSE":-1} #TRUE:Mirtrons  FALSE:canonical microRN

def usage():
    print("USAGE: python isMirtron.py -s pre-miRNA sequnece\n")
    print("Example: python isMirtron.py -s CTGGGGAGATGGGGGGAGCTCTGCTGAGGGTGCACAAGGCCCTGGCTCTACACACATCCCTGTCTTACAG")

# padding and trim the sequence to the length of SEQUENCE_LENGTH
SEQUENCE_LENGTH = 164
def seq_process(seq):
    # remove all the spaces
    seq = seq.replace(' ', '')
    # remove \n
    seq = seq.replace("\n", "")
    for base in seq:
        if base not in ("A","U","G","C","T","a","u","g","c","t"):
            print ("Input sequence is wrong: incorrect base!\n")
            usage()
            exit(1)
    seq = seq.upper()
    m_len = len(seq)
    if m_len < SEQUENCE_LENGTH:
        seq += "N" *(SEQUENCE_LENGTH - m_len)
    elif m_len >= SEQUENCE_LENGTH:
        seq = seq[:SEQUENCE_LENGTH]

    return seq

# sequence vectorization
def seq_vectorize(seq):
    vectorized_seq = []
    for char in processed_seq:
        vectorized_seq.append(x_cast[char])
    vectorized_seq = np.array(vectorized_seq)
    vectorized_seq = vectorized_seq.reshape([1,SEQUENCE_LENGTH,4])
    return vectorized_seq

# get the parameters
def get_input_seq():
    import getopt
    input_seq = ""
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hs:",["help","sequence="])
    except getopt.GetoptError:
        print ("Wrong usage!\n")
        usage()
        sys.exit(1)
    if len(opts) < 1:
        usage()
        sys.exit(1)
    # parse the options
    for op, value in opts:
        if op in ("-s","--sequence"):
            input_seq = value
        elif op in ("-h","--help"):
            usage()
            sys.exit()
    return input_seq

# handle the input and get the input sequence
input_seq = get_input_seq()
# check the input sequence
processed_seq = seq_process(input_seq)
# vectorized the input sequence with one-hot encoding
vectorized_seq = seq_vectorize(processed_seq)

FILE_PATH = "./data/miRBase_set.csv"
FILE_PATH_PUTATIVE = "./data/putative_mirtrons_set.csv"

X,y = dataProcess.generate_data(FILE_PATH,FILE_PATH_PUTATIVE)

# convert the value of y for svm model
y = y.tolist()
for i in range(len(y)):
    if y[i] ==[1,0]:
        y[i] = 1
    if y[i] ==[0,1]:
        y[i] = -1
y = np.array(y)
#print(X.shape)
#print(y.shape)
#print("===============")

# load the trained deep autoencoder model
encoder_model = load_model('./autoencoder/trained_encoder.h5')
# encoding the X in the dataset
X_encoded = encoder_model.predict(X)
# encoding the vectorized input sequence
vectorized_seq_encoded = encoder_model.predict(vectorized_seq)

# establish the SVC model
svm_model = svm.SVC(kernel='rbf', C=5,gamma = 0.0001,probability=True)
# train the model
svm_model.fit(X_encoded, y)

# prediction results (probability)
result_probability = svm_model.predict_proba(vectorized_seq_encoded)
# print(result_probability)
result_probability = result_probability[0][1]    # probability of mirtron
# print the prediction result
up_threhold = 0.8
down_threhold = 0.5
print("==================================================================")
print("Prediction result:")
if result_probability >= up_threhold:
    print("Yes,it is a mirtron.")
    print("probability:{}".format(result_probability))
elif result_probability <= down_threhold:
    print("No,it is not a mirtron.")
else:
    print("Not sure.")
print("\n")


import numpy as np
import multiprocessing as mp
from multiprocessing import connection
import gym
from gym.core import Wrapper

def worker_process(remote, env, seed):

    while True:

        cmd, data = remote.recv()
        if cmd == 'step':
            obs, r, done, infos = env.step(data)
            if done:
                obs = env.reset()
            remote.send((obs, r, done, infos))
            # remote.send(seed)

        elif cmd == 'reset':
            s = env.reset()
            remote.send(s)

        elif cmd == 'close':
            env.close()
            remote.close()
            break

        else:
            raise NotImplementedError

class Worker:

    def __init__(self, s, env):

        self.child, parent = mp.Pipe()
        self.ps = mp.Process(target = worker_process, args = (parent, env, s))
        self.ps.start()

class Monitor(Wrapper):

    def __init__(self, env):

        super().__init__(env = env)

        self.episode_reward = 0
        self.episode_length = 0

    def step(self, ac):

        ns, r, done, infos = super().step(ac)
        self.episode_reward += r
        self.episode_length += 1

        if done:
            infos['episode'] = {'r':self.episode_reward, 'l':self.episode_length}
        else:
            infos = {}

        return ns, r, done, infos

    def reset(self):

        self.episode_reward = 0.
        self.episode_length = 0.

        return super().reset()

class VEnv:

    def __init__(self, nb_ps, env_name):

        self.nb_ps = nb_ps
        self.workers = [Worker(i, Monitor(gym.make(env_name))) for i in range(self.nb_ps)]

        sample_env = gym.make(env_name)
        self.action_space = sample_env.action_space
        self.observation_space = sample_env.observation_space

    def reset(self):

        for w in self.workers:
            w.child.send(('reset', None))

        return np.vstack([w.child.recv() for w in self.workers])

    def close(self):

        for w in self.workers:
            w.child.send(('close', None))

    def step(self, action):

        for w,a in zip(self.workers, action):
            w.child.send(('step', a))

        obs, rewards, dones, infos = [],[],[],[]
        for i,w in enumerate(self.workers):
            data = w.child.recv()
            obs.append(data[0])
            rewards.append(data[1])
            dones.append(data[2])
            infos.append(data[-1])

        return np.vstack(obs), np.array(rewards).reshape(-1,1), np.stack(dones), infos

# nb_envs = 10
# envs = VEnv(nb_envs, 'BipedalWalker-v2')

# print(envs)
# print(envs.reset())

# for _ in range(10):
#     # result = envs.step(np.random.randint(0,2, (nb_envs)))
#     result = envs.step(np.random.uniform(-1.,1., (nb_envs, 4)))
#     print(result[1])

#     print('\n')
# envs.close()


from infraMeta import LibraryInfo

# noinspection PyUnreachableCode
if False:
	# noinspection PyUnresolvedReferences
	from _stubs import *

class LibrarySwitcher:
	def __init__(self, ownerComp: 'COMP'):
		self.ownerComp = ownerComp

	@staticmethod
	def buildLibraryTable(
			dat: 'DAT'):
		dat.clear()
		dat.appendRow(['path', 'name'])
		metas = root.findChildren(
			type=baseCOMP,
			path='*/libraryMeta',
		)
		for meta in metas:
			if meta.par.clone.eval() is meta:
				continue
			info = LibraryInfo(meta.par.Hostop)
			if info:
				dat.appendRow([
					info.comp.path,
					info.libraryName,
				])


import plotly.express as px
iris = px.data.iris()
iris["e"]=iris["sepal_width"]/100
fig = px.scatter(
    iris,
    x="sepal_width",
    y="sepal_length",
    color="species",
    error_x="e",
    error_y="e"
)
fig.show()


import surprise as spr
import scipy.sparse as sp
import numpy as np

## Helper functions to use surprise library

def pandas_to_surprise(data, pred=False):
    """Function that converts pandas dataframe into surprise data
    or directly the testset if the submission sample is given and pred set to True"""
    reader = spr.Reader(rating_scale=(1, 5))
    data_spr = spr.Dataset.load_from_df(data[['User', 'Movie', 'Rating']], reader)
    if pred:
        data_spr = data_spr.build_full_trainset().build_testset()
        data_spr = sorted(data_spr, key=lambda x: (x[1], x[0]))
    return data_spr

def get_testset_indices(test_spr):
    """Returns the users, and movies indices and their label of a testset"""
    users_idx, movies_idx, labels = list(zip(*test_spr))
    return users_idx, movies_idx, labels

def testset_to_sparse_matrix(test_spr):
    """Convert a testset to a scipy sparse matrix"""
    matrix = sp.lil_matrix((10000, 1000))
    users_idx, movies_idx, labels = get_testset_indices(test_spr)
    matrix[users_idx, movies_idx] = labels
    return matrix

def spr_estimate_to_vect(predict):
    "Convert the estimations of a surprise prediction to a vector"
    vect = np.zeros(len(predict))
    for i, pred in enumerate(predict):
        vect[i] = pred.est
    return vect

## Function running the suprise's models

def surprise_SVD(trainset, finalset):
    "SVD model"
    algo = spr.SVD(n_factors=40, n_epochs=20, lr_all=0.001)

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)

def surprise_basicKNN(trainset, finalset):
    "Basic K Nearest Neighbours model"
    algo = spr.KNNBasic()

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)

def surprise_baselineKNN(trainset, finalset):
    "Item based baseline K-Nearest neighbours model"
    options = {'name': 'pearson_baseline',
               'user_based': False}

    algo = spr.KNNBaseline(k=40, sim_options=options)

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)

def surprise_slopeOne(trainset, finalset):
    "Slope One model"
    algo = spr.SlopeOne()

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)

def surprise_SVDpp(trainset, finalset):
    "SVD++ model"
    algo = spr.SVDpp(n_factors=40, n_epochs=20, lr_all=0.001)

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)

def surprise_baseline(trainset, finalset):
    "baseline model"

    algo = spr.BaselineOnly()

    algo.fit(trainset)
    predictions_final = algo.test(finalset)

    return spr_estimate_to_vect(predictions_final)


# _*_ coding: utf-8 _*_
import pandas as pd

df = pd.read_csv("customer_churn.csv", index_col=0, header=0)
df = df.iloc[:, 3:]
# print(df.columns)

# 数据清洗，将含有‘yes'或者'no'的数据转换为one_hot编码

cat_vars = ['international_plan', 'voice_mail_plan', 'churn']
for var in cat_vars:
    df[var] = df[var].map(lambda e: 1 if e == 'yes' else 0)
# 检查数据是否全部转化为数值型数据
print(df.info())

y = df.iloc[:, -1]
X = df.iloc[:, :-1]
# print(x.shape)
# print(y.shape)

# 区分训练与测试数据集

import sklearn.model_selection
#  random_state=123,相当于random类中的seed,设置可复现随机数据
(x_train, x_test, y_train, y_test) = sklearn.model_selection.train_test_split(X, y, test_size=0.33, random_state=123)
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

# 数据归一化
import sklearn.preprocessing
sc = sklearn.preprocessing.StandardScaler()

x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)
#print(x_test)

import keras

# model = keras.Sequential()
# model.add(keras.layers.Dense(units=8,kernel_initializer='uniform',activation='relu',input_shape=(16,)))
# model.add(keras.layers.Dense(units = 1,kernel_initializer='uniform',activation='sigmoid'))
#
# model.compile(
#     #因为此处输出为二元，0为留下，1为流失，所以使用binary_crossentropy作为代价函数
#     loss='binary_crossentropy'
#    ,optimizer='sgd'
#     ,metrics=['accuracy']
# )
#
# history = model.fit(x_train,y_train
#                     ,batch_size=10
#                     ,epochs=200
#                     ,verbose=True
#                     ,validation_data=(x_test,y_test))

# model.save('m2.h5')
model = keras.models.load_model('m2.h5')

y_pred = model.predict(x_test)
#大于0.5，客户会流失，小于0.5，客户会留下来
#flatten   2维数组拉平，变成1维的数组
#astype   使用0或1而不是用true或false 作为输出结果
y_pred = (y_pred>0.5).flatten().astype(int)

#预测测试数据的准确率
print(sum(y_pred == y_test)/len(y_test))
print('------------------------------')
print(sklearn.metrics.accuracy_score(y_test, y_pred))
print('------------------------------')
cm = sklearn.metrics.confusion_matrix(y_test,y_pred)
print(cm)

print('==============================')
print(sklearn.metrics.classification_report(y_test, y_pred))

2.7 使用类神经网络建立客户流失预测模型/2.7 使用类神经网络建立客户流失预测模型.py

2.7 使用类神经网络建立客户流失预测模型.py


from typing import Dict

import pytorch_lightning as pl
import torch
import torch.nn as nn
from omegaconf import DictConfig
from torch import Tensor
from torch.optim import ASGD, SGD, Adadelta, Adagrad, Adam, Adamax, AdamW

from openspeech.criterion import CRITERION_REGISTRY
from openspeech.metrics import CharacterErrorRate, WordErrorRate
from openspeech.optim import AdamP, Novograd, RAdam
from openspeech.optim.scheduler import SCHEDULER_REGISTRY
from openspeech.tokenizers.tokenizer import Tokenizer

class OpenspeechModel(pl.LightningModule):
    r"""
    Super class of openspeech models.

    Note:
        Do not use this class directly, use one of the sub classes.

    Args:
        configs (DictConfig): configuration set.
        tokenizer (Tokenizer): tokenizer is in charge of preparing the inputs for a model.

    Inputs:
        inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``.
        input_lengths (torch.LongTensor): The length of input tensor. ``(batch)``

    Returns:
        outputs (dict): Result of model predictions.
    """

    def __init__(self, configs: DictConfig, tokenizer: Tokenizer) -> None:
        super(OpenspeechModel, self).__init__()
        self.configs = configs
        self.num_classes = len(tokenizer)
        self.tokenizer = tokenizer
        self.current_val_loss = 100.0
        self.wer_metric = WordErrorRate(tokenizer)
        self.cer_metric = CharacterErrorRate(tokenizer)
        if hasattr(configs, "trainer"):
            self.gradient_clip_val = configs.trainer.gradient_clip_val
        if hasattr(configs, "criterion"):
            self.criterion = self.configure_criterion(configs.criterion.criterion_name)

    def set_beam_decoder(self, beam_size: int = 3):
        raise NotImplementedError

    def info(self, dictionary: dict) -> None:
        r"""
        Logging information from dictionary.

        Args:
            dictionary (dict): dictionary contains information.
        """
        for key, value in dictionary.items():
            self.log(key, value, prog_bar=True)

    def forward(self, inputs: torch.FloatTensor, input_lengths: torch.LongTensor) -> Dict[str, Tensor]:
        r"""
        Forward propagate a `inputs` and `targets` pair for inference.

        Inputs:
            inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``.
            input_lengths (torch.LongTensor): The length of input tensor. ``(batch)``

        Returns:
            outputs (dict): Result of model predictions.
        """
        raise NotImplementedError

    def training_step(self, batch: tuple, batch_idx: int):
        r"""
        Forward propagate a `inputs` and `targets` pair for training.

        Inputs:
            batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
            batch_idx (int): The index of batch

        Returns:
            loss (torch.Tensor): loss for training
        """
        raise NotImplementedError

    def validation_step(self, batch: tuple, batch_idx: int):
        r"""
        Forward propagate a `inputs` and `targets` pair for validation.

        Inputs:
            batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
            batch_idx (int): The index of batch

        Returns:
            loss (torch.Tensor): loss for training
        """
        raise NotImplementedError

    def test_step(self, batch: tuple, batch_idx: int):
        r"""
        Forward propagate a `inputs` and `targets` pair for test.

        Inputs:
            batch (tuple): A train batch contains `inputs`, `targets`, `input_lengths`, `target_lengths`
            batch_idx (int): The index of batch

        Returns:
            loss (torch.Tensor): loss for training
        """
        raise NotImplementedError

    def configure_optimizers(self):
        r"""
        Choose what optimizers and learning-rate schedulers to use in your optimization.

        Returns:
            - **Dictionary** - The first item has multiple optimizers, and the second has multiple LR schedulers (or multiple ``lr_dict``).
        """
        SUPPORTED_OPTIMIZERS = {
            "adam": Adam,
            "adamp": AdamP,
            "radam": RAdam,
            "adagrad": Adagrad,
            "adadelta": Adadelta,
            "adamax": Adamax,
            "adamw": AdamW,
            "sgd": SGD,
            "asgd": ASGD,
            "novograd": Novograd,
        }

        assert self.configs.model.optimizer in SUPPORTED_OPTIMIZERS.keys(), (
            f"Unsupported Optimizer: {self.configs.model.optimizer}\n"
            f"Supported Optimizers: {SUPPORTED_OPTIMIZERS.keys()}"
        )

        self.optimizer = SUPPORTED_OPTIMIZERS[self.configs.model.optimizer](
            self.parameters(),
            lr=self.configs.lr_scheduler.lr,
        )
        scheduler = SCHEDULER_REGISTRY[self.configs.lr_scheduler.scheduler_name](self.optimizer, self.configs)

        if self.configs.lr_scheduler.scheduler_name == "reduce_lr_on_plateau":
            lr_scheduler = {
                "scheduler": scheduler,
                "monitor": "val_loss",
                "interval": "epoch",
            }
        elif self.configs.lr_scheduler.scheduler_name == "warmup_reduce_lr_on_plateau":
            lr_scheduler = {
                "scheduler": scheduler,
                "monitor": "val_loss",
                "interval": "step",
            }
        else:
            lr_scheduler = {
                "scheduler": scheduler,
                "interval": "step",
            }

        return [self.optimizer], [lr_scheduler]

    def configure_criterion(self, criterion_name: str) -> nn.Module:
        r"""
        Configure criterion for training.

        Args:
            criterion_name (str): name of criterion

        Returns:
            criterion (nn.Module): criterion for training
        """
        if criterion_name in ("joint_ctc_cross_entropy", "label_smoothed_cross_entropy"):
            return CRITERION_REGISTRY[criterion_name](
                configs=self.configs,
                num_classes=self.num_classes,
                tokenizer=self.tokenizer,
            )
        else:
            return CRITERION_REGISTRY[criterion_name](
                configs=self.configs,
                tokenizer=self.tokenizer,
            )

    def get_lr(self):
        for g in self.optimizer.param_groups:
            return g["lr"]

    def set_lr(self, lr):
        for g in self.optimizer.param_groups:
            g["lr"] = lr


# MNIST is a hand-written digits database. As for softmax, that will what we will do in multi-logistic regression to be able to sum the prob to be 1.

### Softmax Regression

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import sys
import sys
sys.path.append("/data")
import numpy as np

x = tf.placeholder("float", [None, 784])

W = tf.Variable(tf.zeros([784, 10]))
# each column will be weights for each output units. 784*1
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)
# we use a trick here, since x is a 2D tensor with multiple inputs.

y_ = tf.placeholder("float", [None, 10])
#MUTATION#
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
obj_var = tf.reduce_min(tf.abs(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# Tensorflow here return us a single operation which when runned, will do a step of gradient descent training.

init = tf.initialize_all_variables()

sess = tf.Session()
tf.train.write_graph(sess.graph_def, '/data/scripts/study_case/pbtxt_files', 'tensorflow_in_ml.pbtxt')
sess.run(init)

batch_size = 100
'''inserted code'''
from scripts.utils.tf_utils import TensorFlowScheduler
scheduler = TensorFlowScheduler(name="tensorflow_in_ml")
'''inserted code'''

while True:
    batch_xs, batch_ys = mnist.train.next_batch(batch_size)
    _, loss, obj_var_val = sess.run([train_step, cross_entropy, obj_var],
                                    feed_dict={x: batch_xs, y_: batch_ys})

    '''inserted code'''
    scheduler.loss_checker(loss)
    '''inserted code'''

    '''inserted code'''
    scheduler.check_time()
    '''inserted code'''


import copy
import torch
from torch import nn

from models.stance_prediction_module import StancePredictionModule
from models.mult_modules.transformer import TransformerEncoder

def freeze_model(model):
    for param in model.parameters():
        param.requires_grad = False

class MultimodalModel(StancePredictionModule):
    def __init__(
            self,
            text_model,
            audio_model,
            dropout_values = (0.3),
            freeze_text = False,
            freeze_audio = False,
        ):
        """
            Creates a model accepting two inputs of different type: a text sequence, which is passed to the text_model component, and
            a raw audio signal, which is fed as input to the audio_model. Once we obtain two separate embeddings for the inputs, they are
            concatenated and passed through a linear layer for the classification step.

            Parameters
            ----------
            text_model: nn.Module
                The desired TextModel instance; it will be used to process the text portion of the input.
            audio_model: nn.Module
                The desired AudioModel instance; it will be used to process the audio portion of the input.
            dropout_values: tuple of floats
                The value to be used for dropout after concatenating the embeddings of the two modlities. Default to (0.3).
            freeze_text: bool
                Whether to freeze all the parameters in the text model. Default to False.
            freeze_audio: bool
                Whether to freeze all the parameters in the audio model. Default to False.

        """
        super(MultimodalModel, self).__init__()
        self.text_model = text_model
        self.audio_model = audio_model

        if freeze_text: freeze_model(self.text_model)
        if freeze_audio: freeze_model(self.audio_model)

        self.dropout = nn.Dropout(p=dropout_values[0])
        self.classifier = nn.Linear(self.text_model.bert_out_dim + self.audio_model.wav2vec2_out_dim, 1)

    def forward(self, text_input, audio_input):
        x = self.text_model(**text_input)
        y = self.audio_model(audio_input)
        x = torch.cat([x, y], dim=1)
        x = self.dropout(x)
        x = self.classifier(x)
        return x

class MulT(StancePredictionModule):
    def __init__(
            self,
            text_model,
            audio_model,
            dropout_values = (0.3),
            freeze_text = False,
            freeze_audio = False,
            crossmodal_type = 'audio2text', # ['audio2text', 'text2audio', 'both']
            pool_operation = 'avg', # ['avg', 'first', 'last']
        ):
        """
            Creates a MulT-inspired model accepting two inputs of different type: a text sequence, which is passed to the text_model component, and
            a raw audio signal, which is fed as input to the audio_model. Once we obtain two separate embeddings for the inputs, they can be passed through:
                - A stack of Multimodal Transformer layers where the audio acts like the query and the text acts like the key/value
                - A stack of Multimodal Transformer layers where the text acts like the query and the audio acts like the key/value
                - Both of the above options
            After the results go through a specified pooling operation and, in the case of crossmodal_type='both', also get contatenated, they are then fed into
            a linear head for the classification

            Parameters
            ----------
            text_model: nn.Module
                The desired TextModel instance; it will be used to process the text portion of the input.
            audio_model: nn.Module
                The desired AudioModel instance; it will be used to process the audio portion of the input.
            dropout_values: tuple of floats
                The value to be used for dropout after obtaining the embeddings of the two modlities. Default to (0.3).
            freeze_text: bool
                Whether to freeze all the parameters in the text model. Default to False.
            freeze_audio: bool
                Whether to freeze all the parameters in the audio model. Default to False.
            crossmodal_type: str
                Whether the text, the audio or both will act as the query in the Multimodal Transformer stack. Default to 'audio2text'.
            pool_operation: str
                Determines which pooling operation will be applied to the output of the Multimodal Transformer. Default to 'avg'.
        """
        super(MulT, self).__init__()
        self.text_model = text_model
        self.audio_model = audio_model

        if freeze_text: freeze_model(self.text_model)
        if freeze_audio: freeze_model(self.audio_model)

        self.crossmodal_type = crossmodal_type
        self.crossmodal = nn.ModuleList([TransformerEncoder(embed_dim=768,
                                                num_heads=8,
                                                layers=4,
                                                attn_dropout=0.1,
                                                relu_dropout=0.1,
                                                res_dropout=0.1,
                                                embed_dropout=0.25,
                                                attn_mask=True)])

        if crossmodal_type == 'both':
            self.crossmodal.append(copy.deepcopy(self.crossmodal[0]))

        self.relu = nn.ReLU()
        self.classifier = nn.Linear(1536 if crossmodal_type == 'both' else 768, 1)
        self.dropout = nn.Dropout(p=dropout_values[0])
        if pool_operation == 'avg':
            self.pool_operation = lambda x, dim: torch.mean(x, dim=dim)
        elif pool_operation == 'last':
            self.pool_operation = lambda x, _ : x[:,-1,:]
        elif pool_operation == 'first':
            self.pool_operation = lambda x, _ : x[:, 0,:]

    def forward(self, text_input, audio_input):
        text_sequences = self.text_model(**text_input)
        audio_sequences = self.audio_model(audio_input)
        text_sequences = text_sequences.permute(1, 0, 2)
        audio_sequences = audio_sequences.permute(1, 0, 2)

        audio_sequences = self.dropout(audio_sequences)
        text_sequences = self.dropout(text_sequences)

        if self.crossmodal_type == 'audio2text' or 'both':
            x = self.crossmodal[0](audio_sequences, text_sequences, text_sequences)
            x = x.permute(1, 0, 2)
            x = self.pool_operation(x, 1)
            #x = torch.mean(x, dim=1)
        elif self.crossmodal_type == 'text2audio':
            x = self.crossmodal[0](text_sequences, audio_sequences, audio_sequences)
            x = x.permute(1, 0, 2)
            x = self.pool_operation(x, 1)

        if len(self.crossmodal) > 1:
            y = self.crossmodal[1](text_sequences, audio_sequences, audio_sequences)
            y = y.permute(1, 0, 2)
            y = self.pool_operation(y, 1)
            x = torch.cat([x, y], dim=1)
        x = self.relu(x)
        x = self.classifier(x)
        return x


import torch
import torch.nn as nn

def smart_sort(x, permutation):
    d1, d2 = x.size()
    ret = x[
        torch.arange(d1).unsqueeze(1).repeat((1, d2)).flatten(),
        permutation.flatten()
    ].view(d1, d2)
    return ret

class NormActive(nn.Module):
    def __init__(self, nactive, featsize, scale=1.):
        super(NormActive, self).__init__()
        self.nactive = nactive
        self.proportion = (featsize - self.nactive)/featsize
        self.featsize = featsize
        self.scale = (scale / (1. - self.proportion))

    def forward(self, feat):

        sortind = torch.argsort(feat, descending=False).to("cuda:0")
        m = torch.Tensor(self.featsize * torch.Tensor((range(feat.size()[0])))).repeat((self.featsize, 1)).t().long().to("cuda:0")
        m2 = sortind + m
        m2 = m2[:, range(int(feat.size()[1] - self.nactive))]

        batchsize = feat.size()[0]
        feat = feat.view(feat.size()[0] * self.featsize)
        feat[m2] = 0
        feat *= self.scale
        feat = feat.view(batchsize, self.featsize)

        return feat

class NormActiveAdaptDropWithLoss(nn.Module):
    def __init__(self, featsize):
        super(NormActiveAdaptDropWithLoss, self).__init__()

        self.featsize = featsize
        self.scale = nn.Parameter(torch.Tensor(1))
        nn.init.constant_(self.scale, 1.)

    def forward(self, feat, prop):

        scale = self.scale/prop
        toKeep = torch.round(self.featsize * prop)
        sortind = torch.argsort(feat, descending=False, dim=1).to("cuda:0")

        mask = torch.zeros_like(feat)

        for i in range(feat.size()[0]):
            mask[i, sortind[i, range(int(toKeep[i]))]] = 1.

        scale = scale.view(-1, 1).repeat(1, self.featsize).to("cuda:0")
        feat *= (mask*scale)

        return feat


from DataProcessing.preprocessing import Preprocessing
import os
import sklearn
import numpy as np
import pandas as pd
from torch.utils.data import DataLoader,TensorDataset
from torch.utils.data import Subset
import torch

def load_dataset(data_path,configs):
    # get data
    if configs['preprocess']==True:
        # npy_dict=get_data(data_path,configs)
        preprocessing=Preprocessing(data_path,configs)
        npy_dict=preprocessing.run()
    if 'sj' in configs['mode']:
        train_data=np.load(os.path.join(data_path,'sj_train_data.npy'))
        train_targets=np.load(os.path.join(data_path,'sj_train_targets.npy'))
        valid_data=np.load(os.path.join(data_path,'sj_valid_data.npy'))
        valid_targets=np.load(os.path.join(data_path,'sj_valid_targets.npy'))

        train_dataset=TensorDataset(train_data,train_targets)
        valid_dataset=TensorDataset(valid_data,valid_targets)
        return train_dataset,valid_dataset
    else:
        npy_dict={
            'train_crime_data':np.load(os.path.join(data_path,'train_crime_data.npy')),
            'train_priority_data':np.load(os.path.join(data_path,'train_priority_data.npy')),
            'crime_targets':np.load(os.path.join(data_path,'crime_targets.npy')), # y_1 (0,1)
            'priority_targets':np.load(os.path.join(data_path,'priority_targets.npy')), #y_2 (0,1,2)
            'train_indices':np.load(os.path.join(data_path,'train_indices.npy')),
            'valid_indices':np.load(os.path.join(data_path,'valid_indices.npy')),
            'test_crime_data':np.load(os.path.join(data_path,'test_crime_data.npy')),
            'test_priority_data':np.load(os.path.join(data_path,'test_priority_data.npy')),
        }

    if configs['mode'] in ['record','tsne_mixed','tsne_priority','tsne_crime']:
        return npy_dict
    #type casting
    if 'xgboost' not in configs['mode']:
        for key in npy_dict.keys():
            npy_dict[key]=torch.from_numpy(npy_dict[key])
            if npy_dict[key].dtype in [torch.float64,torch.double]:
                npy_dict[key]=npy_dict[key].float()

    #data separation
        if 'crime' in configs['mode']:
            crime_dataset=TensorDataset(npy_dict['train_crime_data'],npy_dict['crime_targets'])
            #crime
            train_dataset=Subset(crime_dataset,npy_dict['train_indices'])
            valid_dataset=Subset(crime_dataset,npy_dict['valid_indices'])

        elif 'priority' in configs['mode']:
            #priority
            train_data=npy_dict['train_priority_data'][npy_dict['train_indices'].long()]
            valid_data=npy_dict['train_priority_data'][npy_dict['valid_indices'].long()]

            train_target=npy_dict['priority_targets'][npy_dict['train_indices'].long()]
            valid_target=npy_dict['priority_targets'][npy_dict['valid_indices'].long()]

            # select 1 and 2
            train_data=train_data[torch.logical_or(train_target==1,train_target==2)]
            valid_data=valid_data[torch.logical_or(valid_target==1,valid_target==2)]
            train_target=train_target[torch.logical_or(train_target==1,train_target==2)]
            valid_target=valid_target[torch.logical_or(valid_target==1,valid_target==2)]

            # change target 1 to 0, 2 to 1
            train_target[train_target==1]=0
            train_target[train_target==2]=1
            valid_target[valid_target==1]=0
            valid_target[valid_target==2]=1
            train_dataset=TensorDataset(train_data, train_target)
            valid_dataset=TensorDataset(valid_data, valid_target)

        elif configs['mode']=='train_mixed':
            #mixed
            mixed_dataset=TensorDataset(npy_dict['train_crime_data'],npy_dict['train_priority_data'],npy_dict['crime_targets'],npy_dict['priority_targets'])
            train_dataset=Subset(mixed_dataset,npy_dict['train_indices'])
            valid_dataset=Subset(mixed_dataset,npy_dict['valid_indices'])

        else:
            print('No dataset')
            raise NotImplementedError
        return train_dataset, valid_dataset

    else: #XGBOOST
        if 'crime' in configs['mode']:
            train_data=npy_dict['train_crime_data'][npy_dict['train_indices']]
            valid_data=npy_dict['train_crime_data'][npy_dict['valid_indices']]

            train_target=npy_dict['crime_targets'][npy_dict['train_indices']]
            valid_target=npy_dict['crime_targets'][npy_dict['valid_indices']]

        elif 'priority' in configs['mode']:
            train_data=npy_dict['train_priority_data'][npy_dict['train_indices']]
            valid_data=npy_dict['train_priority_data'][npy_dict['valid_indices']]

            train_target=npy_dict['priority_targets'][npy_dict['train_indices']]
            valid_target=npy_dict['priority_targets'][npy_dict['valid_indices']]

        return train_data, train_target, valid_data, valid_target

def load_dataloader(data_path,configs):

    # npy_dict={
    #         # 'table_data':np.load(os.path.join(data_path,'train_crime_data.npy')),
    #         # 'crime_targets':np.load(os.path.join(data_path,'crime_targets.npy')), # y_1 (0,1)
    #         # 'priority_targets':np.load(os.path.join(data_path,'priority_targets.npy')), #y_2 (0,1,2)
    #         # 'train_indices':np.load(os.path.join(data_path,'train_indices.npy')),
    #         # 'valid_indices':np.load(os.path.join(data_path,'valid_indices.npy')),
    #         #'test_data':np.load(os.path.join(data_path,'test_data.npy')),
    #     }
    # train_target=npy_dict['crime_targets'][npy_dict['train_indices']]

    if 'xgboost' not in configs['mode']:
        print('-------------load_dataloader--------------')
        sampler=None
        #class_counts = [66344, 18794]  # [36377, 3755]
        #class_counts = [np.where(train_target == 0)[0].shape[0], np.where(train_target == 1)[0].shape[0], np.where(train_target == 2)[0].shape[0]]
        # class_counts = [np.where(train_target == 0)[0].shape[0], np.where(train_target == 1)[0].shape[0]]
        # num_samples = sum(class_counts)  # 40132
        # train_labels = train_target.tolist()
        # #print(np.where(train_target == 0)[0].shape, np.where(train_target == 1)[0].shape, np.where(train_target == 2)[0].shape)

        # class_weights = [num_samples/class_counts[i] for i in range(len(class_counts))]
        # weights = [class_weights[train_labels[i]] for i in range(int(num_samples))]
        # sampler = torch.utils.data.sampler.WeightedRandomSampler(torch.DoubleTensor(weights), int(num_samples))

        #주석만 삭제해도 돌아가게
        if sampler is None:
            shuffle=True
        else:
            shuffle=False

        train_dataset,valid_dataset=load_dataset(data_path,configs)
        train_dataloader=DataLoader(train_dataset,batch_size=configs['batch_size'],num_workers=configs['num_workers'], sampler = sampler,shuffle=shuffle)
        valid_dataloader=DataLoader(valid_dataset,batch_size=configs['batch_size'],num_workers=configs['num_workers'], shuffle = False)
        return train_dataloader,valid_dataloader
    else: #xgboost
        train_data,train_target,valid_data,valid_target = load_dataset(data_path,configs)
        return train_data,train_target,valid_data,valid_target


import os
import cv2
import numpy as np

from flask import Flask, render_template, request, jsonify, redirect, url_for
from werkzeug.utils import secure_filename

from descriptor import DescribeTexture
from ranker import Ranker

UPLOAD_FOLDER = 'static/queries'

# create flask instance
app = Flask(__name__)
app.config['UPLOAD_FOLDER']= UPLOAD_FOLDER

INDEX = os.path.join(os.path.dirname(__file__), 'texture.csv')

# main route
@app.route('/')
def index():
    return render_template('index.html', preview="static/init-preview.png")

#rout upload file
@app.route('/upload',methods= ['GET','POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        filename = secure_filename(f.filename)
        f.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
        return redirect("/")

# image database url list route
@app.route('/list', methods=['POST'])
def image_list():

    if request.method == "POST":

        try:

            imgList = [img for img in list(os.listdir(os.path.join(os.path.dirname(__file__), 'static/queries/'))) if img[-4:] in ('.png', '.jpg', '.gif')]

            return jsonify(imgList=imgList)

        except Exception as e:
            return jsonify({"sorry": "Sorry, no results! Please try again."}), 500

# search route
@app.route('/search', methods=['POST'])
def search():

    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')
        print(image_url)

        try:

            # initialize the image descriptor
            tx = DescribeTexture()

            # load the query image and describe it
            from skimage import io
            import cv2

            query = cv2.imread(os.path.join(os.path.dirname(__file__), 'static/queries/'+image_url))
            features = tx.describe_texture(query)

            # perform the search
            searcher = Ranker(INDEX)
            results = searcher.rank(features)
            slice1 = slice(47)
            print(results)
            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append(
                    {"image": str(resultID[48:]), "score": str(score)})
                print(RESULTS_ARRAY)
            return jsonify(results=(RESULTS_ARRAY[:101]), preview="images/"+image_url)

        except Exception as e:
            print(str(e))
            # return error
            return jsonify({"sorry": "Sorry, no results! Please try again."}), 500

# run!
if __name__ == '__main__':
    app.run('0.0.0.0', debug=True)


import torch

def make_optimizer(cfg, model):
    params = []
    for key, value in model.named_parameters():
        if not value.requires_grad:
            continue

        lr = cfg.BASE_LR
        weight_decay = cfg.WEIGHT_DECAY

        if "bias" in key:
            lr = cfg.BASE_LR * cfg.BIAS_LR_FACTOR
            weight_decay = cfg.WEIGHT_DECAY_BIAS

        if "base.patch_embed.proj" in key:
            params +=[{"params": [value], "lr": lr * cfg.LR_PRETRAIN, "weight_decay": weight_decay}]
            continue

        if "base.blocks." in key:
            params +=[{"params": [value], "lr": lr * cfg.LR_PRETRAIN, "weight_decay": weight_decay}]
            continue

        params += [{"params": [value], "lr": lr, "weight_decay": weight_decay}]

    if cfg.OPTIMIZER_NAME == 'SGD':
        optimizer = getattr(torch.optim, cfg.OPTIMIZER_NAME)(params, momentum=cfg.MOMENTUM)

    elif cfg.OPTIMIZER_NAME == 'AdamW':
        optimizer = torch.optim.AdamW(params, lr=cfg.BASE_LR, weight_decay=cfg.WEIGHT_DECAY)

    else:
        optimizer = getattr(torch.optim, cfg.OPTIMIZER_NAME)(params)

    return optimizer


import cv2 as cv
import numpy as np

#vdosrc= "C:\\Users\\Abhishek\\Desktop\\ML\\data\\road_car_view.mp4"
video = cv.VideoCapture("C:\\Users\\Abhishek\\Desktop\\ML\\data\\road_car_view.mp4")

while True:
    ret, frame = video.read()
    if not ret:
        video = cv.VideoCapture("C:\\Users\\Abhishek\\Desktop\\ML\\data\\road_car_view.mp4")
        continue
    hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
    #cv.imshow("HSV", hsv)
    hsv[0:500,0:1280] = [0,0,0]
    blurimg = cv.blur(hsv, (2, 2),0)

    low_yellow = np.array([18,94,140])
    high_yellow= np.array([48,255,255])

    mask = cv.inRange(blurimg,low_yellow,high_yellow)
    #cv.imshow("Mask", mask)
    edge = cv.Canny(mask,75,150)
    #cv.imshow("Edge", edge)
    #print(edge.shape)

    lines = cv.HoughLinesP(edge,1,np.pi/180,50,maxLineGap=50)
    if lines is not None:
       for line in lines:
            x1,y1,x2,y2 = line[0]
            cv.line(frame,(x1,y1),(x2,y2),(0,255,0),3)

    cv.imshow("Frame",frame)
    #cv.imshow("Cropped",cropped)
    key = cv.waitKey(1)
    if key == 27:
        break
video.release()
cv.destroyAllWindows()


#Simple music player using SenseHat
# Path (volume serial of USB stick) is hardcoded
# and the code is not portable
#

from sense_hat import SenseHat
from random import randint
from pygame import mixer
import glob,os,random

#initialize sound mixer
mixer.init()

#initialize SenseHat
sense = SenseHat()
sense.clear()

#change dir if you want to reuse the code
os.chdir("/media/pi/1EB8-AEB7/muzyka")
songs = glob.glob("*.mp3")

#infinite loop
while True:
	#read joystick
    for event in sense.stick.get_events():
		#next song
        if event.action == 'pressed' and event.direction == 'right':
            mixer.music.load(random.choice(songs))
            mixer.music.play()
		#stop
        if event.action == 'pressed' and event.direction == 'middle':
            mixer.music.stop()
		#volume up
        if event.action == 'pressed' and event.direction == 'up':
            mixer.music.set_volume(mixer.music.get_volume()+0.1)
		#volume down
        if event.action == 'pressed' and event.direction == 'down':
            mixer.music.set_volume(mixer.music.get_volume()-0.1)
	#get volume as int between 0,7
    volume=(int)(mixer.music.get_volume()*7)

	#random sparkling leds
    sense.set_pixel(randint(0,6),randint(0,7),randint(0,255),randint(0,255),randint(0,255))

	#show volume indicator
    for i in range (0,7):
        if i < volume:
            sense.set_pixel (7,i,(255,0,0))
        else:
            sense.set_pixel (7,i,(0,0,0))


from osgeo import gdal
import numpy as np
import sys
from scipy import stats

"""
This script is to make a summary of a tiff image, especially to find the peculiar(s).

usage:
python NoDataCount.py <tiff file path>
"""

def largest_indices(ary, n):
    """Returns the n largest indices from a numpy array. (src: Internet.)"""
    flat = ary.flatten()
    indices = np.argpartition(flat, -n)[-n:]
    indices = indices[np.argsort(-flat[indices])]
    return np.unravel_index(indices, ary.shape)

def RasterSummary(filename):
    np.set_printoptions(threshold=np.inf)
    dataset = gdal.Open(filename)
    cols=dataset.RasterXSize
    rows=dataset.RasterYSize

    band = dataset.GetRasterBand(1)

    arr=band.ReadAsArray(0, 0, cols, rows)
    print(filename)
    print(rows,'x',cols)
    print('avg:',np.mean(arr))
    mode=stats.mode(arr.reshape(-1))
    print('mode:',mode.mode[0],'[*',mode.count[0],'times]')
    print('median:',np.median(arr))
    for i in range(1,5):
        print(str(i*20)+'% percentile',np.percentile(arr, i*20))
    print('99.999% percentile',np.percentile(arr, 99.999))
    k=10
    index=largest_indices(arr,k)
    for i in range(k):
        x=index[0][i]
        y=index[1][i]
        print('%dth largest at (%d,%d)-> %f' % (i+1,x,y,arr[x][y]))

    del dataset

if __name__=='__main__':
    RasterSummary(sys.argv[1])


from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include

from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),

    # Urls for app netwokr
    path('', include('network.urls')),

    # Urls for app users
    path('users/', include('users.urls')),
    path('api/', include('api.urls')),

    path('chat/', include('chat.urls')),
    path('actions/', include('log_actions.urls')),

    path('admin/', admin.site.urls),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

admin.site.site_header = "Куб administration"
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


# Multiple Inheritance

class Mahasiswa1(): #Class Mahasiswa1
    def __init__(self,nama,nim):
        self.nim = nim #Atribut public
        self.nama = nama #Atribut public

class Mahasiswa2():
    def __init__(self,alamat,jurusan):
        self.alamat = alamat #Atribut public
        self.jurusan = jurusan #Atribut public

class Siswa(Mahasiswa1,Mahasiswa2):
    def __init__(self,nim,nama,alamat,jurusan):
        #Fungction dengan metod __init_ menyimpan parameter self,alamat,jurusan,nim,nama
        Mahasiswa1.__init__(self,nama,nim)
        Mahasiswa2.__init__(self,alamat,jurusan)
print()
#Membuat objek
s = Siswa("Mikasa", 135105,"Wall rose","Informatika")
print("Nim : ",s.nim, "Nama : ",s.nama, "Alamat : ",s.alamat,"Jurusan : ",s.jurusan) #Cetak objek atau data


import wsgiref.simple_server
import http.cookies

def application(environ, start_response):
    headers = [('Content-Type', 'text/plain; charset=utf-8'),
               ('Set-Cookie', 'favoriteColor=green'),
               ('Set-Cookie', 'favoriteNumber=79'),
               ('Set-Cookie', 'name=Malachi')]
    start_response('200 OK', headers)
    print(environ)
    if 'HTTP_COOKIE' in environ:
        cookies = http.cookies.SimpleCookie()
        cookies.load(environ['HTTP_COOKIE'])
        text = ''
        for key in cookies:
            text += (key + ': ' + cookies[key].value)
        return[text.encode()]
    else:
        start_response('404 Not Found', headers)
        return ['Status 404: Resource not found'.encode()]

httpd = wsgiref.simple_server.make_server('', 8000, application)

print("Serving on port 8000...")

httpd.serve_forever()


#-*- coding:utf-8 -*-

import os
import json
import telegram
from flask import Flask, request, render_template
from flask_cors import CORS
import time

app = Flask(__name__)
CORS(app)
coinSym = {}
coinName =[]

@app.route('/',methods=['GET', 'OPTIONS'])
def welcome():
    return render_template('index.html')

@app.route('/webhook',methods=['POST'])
def whatever():

    readData = json.loads(request.data)
    print('test OK',readData);
    chk = False;
    chkk = False;
    chkkk = False;
    chkkkk = False;
    global coinName
    for keyread in readData.keys():
        if keyread == 'arbitrage':
            chk = True
        if keyread == 'exchange':
            chkk = True
        if keyread == 'moving_exchange':
            chk = False
            chkk = False
            chkkk = True
        if keyread == 'maxVolume':
            chk = False
            chkk = False
            chkkk = False
            chkkkk = True

    if chk == True:
        coinName = readData['arbitrage'].split(',')
        coinName.pop();
        print('Read OK',coinName);

    elif chkk == True:
        global coinSym
        if coinSym:
            check = False;
            coinSymRead = readData['exchange'];
            for coin in coinSym.keys():
                if coin == coinSymRead:
                    check = True;
                    break;

            if check == True:
                if coinSym[coinSymRead] >= 2:
                    coinSym[coinSymRead] = 0

                    totalString = readData['name']+' | 코인 : ' + readData['exchange'] + ' 거래량 : ' + str(readData['volume']) + ' 금액 : ' + str(readData['price']) + ' 시간 : ' + str(readData['time'])

                    checkarrow = ""
                    for keyread in coinName:
                        if keyread in readData['exchange']:
                            checkarrow  = '✅ ' #'✅ '+ readData['name']+' | 코인 : ' + readData['exchange'] + ' 거래량 : ' + str(readData['volume']) + ' 금액 : ' + str(readData['price']) + ' 시간 : ' + str(readData['time'])

                    #time.sleep(5)

                    bot = telegram.Bot(token='2105654811:AAEpHpQLLeE-e2qQ6s-kJ7MDeQV54iZJbo8')
                    chat_id = '@cryptotradingviewconnect'
                    bot.sendMessage(chat_id=chat_id, text=totalString)
                    chat_id = '1000903796'
                    bot.sendMessage(chat_id=chat_id, text=(checkarrow + totalString))

                    print('send',totalString)
                elif coinSym[coinSymRead] < 2:
                    coinSym[coinSymRead] = coinSym[coinSymRead] + 1
                    print('sum', coinSym[coinSymRead],coinSymRead)
            else:
                coinSym.update({coinSymRead : 1})
                print('add', coinSym[coinSymRead], coinSymRead)
        else:
            coinSym = {readData['exchange'] : 1}
            print('add', coinSym[readData['exchange']],readData['exchange'])

    elif chkkk == True:
        chkkk = False;
        totalString = '🔔 코인 : ' + readData['moving_exchange'] + ' 거래량 : ' + str(readData['volume']) + ' 금액 : ' + str(readData['price']) + ' 시간 : ' + str(readData['time'])
        print('MA send : ',totalString)

        bot = telegram.Bot(token='2105654811:AAEpHpQLLeE-e2qQ6s-kJ7MDeQV54iZJbo8')
        chat_id = '-1001678871735'
        bot.sendMessage(chat_id=chat_id, text=(totalString))

    elif chkkkk == True:
        chkkkk = False;
        totalString = '📈 거래량 Up 코인 : ' + readData['maxVolume_exchange'] + ' 거래량 : ' + str(readData['volume']) + ' 금액 : ' + str(readData['price']) + ' 시간 : ' + str(readData['time'])
        print('volume send : ',totalString)

        bot = telegram.Bot(token='2105654811:AAEpHpQLLeE-e2qQ6s-kJ7MDeQV54iZJbo8')
        chat_id = '-1001678871735'
        bot.sendMessage(chat_id=chat_id, text=(totalString))

    return {
        "code": "succss",
        "message": readData
    }

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host="0.0.0.0", port=port)


# Set up and load data
# Includes
import sys
import os

# Setup paths containing utility
curr_folder = os.getcwd()
sys.path.insert(0, os.path.join(curr_folder,'../app'))

# Import utils
from utils_EDA import p_list_qas
from utils import load_SQuAD_train
from utils import load_SQuAD_dev

# Load the training data
arts_train = load_SQuAD_train()

# Load the testing data
arts_dev = load_SQuAD_dev()

# All articles
Ntrain = len(arts_train)
Ndev = len(arts_dev)
print ("Narticles in train = " +  str(len(arts_train)))
print ("Narticles in dev = " +  str(len(arts_dev)))

# # TRAINING DATASET # #

# # Pick out a subset of articles
art = arts_train[:]
# art = arts_train[14:15]

from utils_SQuAD import classify_blanks_from_answers

maxWords_per_FITB = 2
art3 = classify_blanks_from_answers(art,maxWords_per_FITB=2,return_full=False)

# Do a test print
print(art3[0]['title'])
print(art3[0]['paragraphs'][0]['context_blanked'])

# # Save the file

from utils import get_foldername, save_data
foldername = get_foldername('sq_pp_training')
save_data(art3,'train.json',foldername);

# # DEV DATASET # #

# # Pick out a subset of articles
art = arts_dev[:]

from utils_SQuAD import classify_blanks_from_answers

maxWords_per_FITB = 2
arts3dev = classify_blanks_from_answers(art,maxWords_per_FITB=2,return_full=False)

# Do a test print
print(arts3dev[0]['title'])
print(arts3dev[0]['paragraphs'][0]['context_blanked'])

# # Save the file
from utils import get_foldername, save_data
foldername = get_foldername('sq_pp_training')
save_data(arts3dev,'dev.json',foldername);


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

# helper conv function
def conv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True):
    """Creates a convolutional layer, with optional batch normalization.
    """
    layers = []
    conv_layer = nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
                           kernel_size=kernel_size, stride=stride, padding=padding, bias=False)

    layers.append(conv_layer)

    if batch_norm:
        layers.append(nn.BatchNorm2d(out_channels))
    return nn.Sequential(*layers)

# helper conv function
def conv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True):
    """Creates a convolutional layer, with optional batch normalization.
    """
    layers = []
    conv_layer = nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
                           kernel_size=kernel_size, stride=stride, padding=padding, bias=False)

    layers.append(conv_layer)

    if batch_norm:
        layers.append(nn.BatchNorm2d(out_channels))
    return nn.Sequential(*layers)

class Discriminator(nn.Module):

    def __init__(self, conv_dim=64):
        super(Discriminator, self).__init__()

        # Define all convolutional layers
        # Should accept an RGB image as input and output a single value

        #128x128x3 --> (128-4+2)/2+1 = 64 --> 64x64x64
        self.conv1 = conv(in_channels = 3, out_channels = conv_dim, kernel_size = 4,
                          batch_norm = False)

        #64x64x64 --> (64-4+2)/2+1 = 32 --> 32x32x128
        self.conv2 = conv(in_channels = conv_dim, out_channels = conv_dim * 2, kernel_size = 4,
                          batch_norm = True)

        #32x32x128 --> (32-4+2)/2+1 = 16 --> 16x16x256
        self.conv3 = conv(in_channels = conv_dim * 2, out_channels = conv_dim * 4, kernel_size = 4,
                          batch_norm = True)

        #16x16x256 --> (16-4+2)/2+1 = 8 --> 8x8x512
        self.conv4 = conv(in_channels = conv_dim * 4, out_channels = conv_dim * 8, kernel_size = 4,
                          batch_norm = True)

        #8x8x512 --> (8-8+0)/1+1 = 1 --> 1x1x1

        self.conv5 = conv(in_channels = conv_dim * 8, out_channels = 1, kernel_size = 4, stride = 1,
                          batch_norm = False)

    def forward(self, x):
        # define feedforward behavior
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))
        x = self.conv5(x)

        return x

class ResidualBlock(nn.Module):
    """Defines a residual block.
       This adds an input x to a convolutional layer (applied to x) with the same size input and output.
       These blocks allow a model to learn an effective transformation from one domain to another.
    """
    def __init__(self, conv_dim):
        super(ResidualBlock, self).__init__()
        # conv_dim = number of inputs
        self.conv1 = conv(in_channels = conv_dim, out_channels = conv_dim, kernel_size = 3, stride = 1,
                          padding = 1,batch_norm = True)
        self.conv2 = conv(in_channels = conv_dim, out_channels = conv_dim, kernel_size = 3, stride = 1,
                          padding = 1,batch_norm = True)

    def forward(self, x):
        res_x = F.relu(self.conv1(x))
        res_x = self.conv2(res_x)

        return x + res_x

# helper deconv function
def deconv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True):
    """Creates a transpose convolutional layer, with optional batch normalization.
    """
    layers = []
    # append transpose conv layer
    layers.append(nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, bias=False))
    # optional batch norm layer
    if batch_norm:
        layers.append(nn.BatchNorm2d(out_channels))
    return nn.Sequential(*layers)

class CycleGenerator(nn.Module):

    def __init__(self, conv_dim=64, n_res_blocks=6):
        super(CycleGenerator, self).__init__()

        # 1. Define the encoder part of the generator

        #128x128x3 --> (128-4+2)/2+1 = 64 --> 64x64x64
        #64x64x64 --> (64-4+2)/2+1 = 32 --> 32x32x128
        #32x32x128 --> (32-4+2)/2+1 = 16 --> 16x16x256

        self.encoder = nn.Sequential(conv(in_channels = 3, out_channels = conv_dim, kernel_size = 4,
                                     batch_norm = True),
                            nn.ReLU(),
                            conv(in_channels = conv_dim, out_channels = conv_dim * 2, kernel_size = 4,
                                 batch_norm = True),
                            nn.ReLU(),
                            conv(in_channels = conv_dim * 2, out_channels = conv_dim * 4, kernel_size = 4,
                                 batch_norm = True),
                            nn.ReLU()
                            )
        res_layers = []
        for i in range(n_res_blocks):
            res_layers.append(ResidualBlock(conv_dim*4))

        self.resblock = nn.Sequential(*res_layers)

        #(16-1)*2-2+4 = 32x32x128
        #(32-1)*2-2+4 = 64x64x64
        #(32-1)*2-2+4 = 128x128x3

        self.decoder = nn.Sequential(deconv(in_channels = conv_dim*4, out_channels = conv_dim * 2, kernel_size = 4,
                                            batch_norm = True),
                                    nn.ReLU(),
                                    deconv(in_channels = conv_dim*2, out_channels = conv_dim, kernel_size = 4,
                                            batch_norm = True),
                                    nn.ReLU(),
                                    deconv(in_channels = conv_dim, out_channels = 3, kernel_size = 4,
                                           batch_norm = False),
                                    nn.Tanh()
                                    )

    def forward(self, x):
        """Given an image x, returns a transformed image."""
        # define feedforward behavior, applying activations as necessary
        encode_out = self.encoder(x)
        res_out = self.resblock(encode_out)
        decode_out = self.decoder(res_out)

        return decode_out

def create_model(g_conv_dim=64, d_conv_dim=64, n_res_blocks=6):
    """Builds the generators and discriminators."""

    # Instantiate generators
    G_XtoY = CycleGenerator(conv_dim = g_conv_dim, n_res_blocks = n_res_blocks)
    G_YtoX = CycleGenerator(conv_dim = g_conv_dim, n_res_blocks = n_res_blocks)
    # Instantiate discriminators
    D_X = Discriminator(conv_dim = d_conv_dim)
    D_Y = Discriminator(conv_dim = d_conv_dim)

    # move models to GPU, if available
    if torch.cuda.is_available():
        device = torch.device("cuda:0")
        G_XtoY.to(device)
        G_YtoX.to(device)
        D_X.to(device)
        D_Y.to(device)
        print('Models moved to GPU.')
    else:
        print('Only CPU available.')

    return G_XtoY, G_YtoX, D_X, D_Y

def print_models(G_XtoY, G_YtoX, D_X, D_Y):
    """Prints model information for the generators and discriminators.
    """
    print("                     G_XtoY                    ")
    print("-----------------------------------------------")
    print(G_XtoY)
    print()

    print("                     G_YtoX                    ")
    print("-----------------------------------------------")
    print(G_YtoX)
    print()

    print("                      D_X                      ")
    print("-----------------------------------------------")
    print(D_X)
    print()

    print("                      D_Y                      ")
    print("-----------------------------------------------")
    print(D_Y)
    print()