#!/usr/bin/env python3

import sys
from datetime import datetime
import select
import threading

from OUCHTrade.OUCHSocketHandler import OUCHSocketHandler
from OUCHTrade.OUCHParser import OUCHParser

class OUCHAppServer:

    def __init__(self, host, port):
        self.ouch_server_sock = OUCHSocketHandler()
        self.host = host
        self.port = port
        self.socket_list = []
        self.clients_dict = {}
        self.heartbeat_frequency = 1.0

    def pad_bytes(self, message_tag):
        if message_tag[3] == "alpha":
            message_length = message_tag[2]
            padded_message = message_tag[4].rjust(message_length)
            return bytes(padded_message, encoding='utf-8')
        elif message_tag[3] == "integer":
            message_length = message_tag[2]
            int_value: int
            int_value = message_tag[4]
            padded_message = int_value.to_bytes(message_length, byteorder='big')
            return padded_message

    def create_login_response(self, client_dict):

        login_list = []

        login_list.append(("packet_type", 2, 1, "alpha", "A"))
        login_list.append(("session", 3, 10, "alpha", client_dict["requested_session"]))
        login_list.append(("sequence_number", 13, 20, "alpha", str(client_dict["current_seq_num"]).rjust(20)))

        login_response = b''
        for login_tag in login_list:
            login_response = login_response + self.pad_bytes(login_tag)

        packet_length = len(login_response)
        login_response = packet_length.to_bytes(2, byteorder='big') + login_response

        client_dict["current_seq_num"] += 1

        return login_response

    def create_heartbeat_message(self, client_dict):
        message_list = []

        message_list.append(("packet_type", 2, 1, "alpha", "H"))

        message = b''
        for message_tag in message_list:
            message = message + self.pad_bytes(message_tag)

        packet_length = len(message)
        message = packet_length.to_bytes(2, byteorder='big') + message

        client_dict["current_seq_num"] += 1

        return message

    def create_new_order_ack(self, client_dict, new_order_dict):
        message_list = []

        message_list.append(("packet_type", 2, 1, "alpha", "S"))
        message_list.append(("message_type", 0, 1, "alpha", "A"))
        message_list.append(("timestamp", 1, 8, "integer", self.getSendingTime()))
        message_list.append(("order_token", 9, 4, "integer", new_order_dict["order_token"]))
        message_list.append(("client_reference", 13, 10, "alpha", new_order_dict["client_reference"]))
        message_list.append(("buy_sell_indicator", 23, 1, "alpha", new_order_dict["buy_sell_indicator"]))
        message_list.append(("quantity", 24, 4, "integer", new_order_dict["quantity"]))
        message_list.append(("orderbook_id", 28, 4, "integer", new_order_dict["orderbook_id"]))
        message_list.append(("group", 32, 4, "alpha", new_order_dict["group"]))
        message_list.append(("price", 36, 4, "integer", new_order_dict["price"]))
        message_list.append(("time_in_force", 40, 4, "integer", new_order_dict["time_in_force"]))
        message_list.append(("firm_id", 44, 4, "integer", new_order_dict["firm_id"]))
        message_list.append(("display", 48, 1, "alpha", new_order_dict["display"]))
        message_list.append(("capacity", 49, 1, "alpha", new_order_dict["capacity"]))
        message_list.append(("order_number", 50, 8, "integer", client_dict["current_seq_num"]))
        message_list.append(("minimum_quantity", 58, 4, "integer", new_order_dict["minimum_quantity"]))
        message_list.append(("order_state", 62, 1, "alpha", "L"))
        message_list.append(("order_classification", 63, 1, "alpha", new_order_dict["order_classification"]))
        message_list.append(("cash_margin_type", 61, 1, "alpha", new_order_dict["cash_margin_type"]))

        message = b''
        for message_tag in message_list:
            message = message + self.pad_bytes(message_tag)

        packet_length = len(message)
        message = packet_length.to_bytes(2, byteorder='big') + message

        client_dict["current_seq_num"] += 1

        return message

    def create_replace_ack(self, client_dict, replace_request_dict):
        message_list = []

        message_list.append(("packet_type", 2, 1, "alpha", "S"))
        message_list.append(("message_type", 0, 1, "alpha", "U"))
        message_list.append(("timestamp", 1, 8, "integer", self.getSendingTime()))
        message_list.append(
            ("replacement_order_token", 9, 4, "integer", replace_request_dict["replacement_order_token"]))
        message_list.append(("buy_sell_indicator", 13, 1, "alpha", " "))
        message_list.append(("quantity", 14, 4, "integer", replace_request_dict["quantity"]))
        message_list.append(("orderbook_id", 18, 4, "integer", 0))
        message_list.append(("group", 22, 4, "alpha", " "))
        message_list.append(("price", 26, 4, "integer", replace_request_dict["price"]))
        message_list.append(("time_in_force", 30, 4, "integer", replace_request_dict["time_in_force"]))
        message_list.append(("display", 34, 1, "alpha", replace_request_dict["display"]))
        message_list.append(("order_number", 35, 8, "integer", client_dict["current_seq_num"]))
        message_list.append(("minimum_quantity", 43, 4, "integer", replace_request_dict["minimum_quantity"]))
        message_list.append(("order_state", 47, 1, "alpha", "L"))
        message_list.append(("previous_order_token", 48, 4, "integer", replace_request_dict["existing_order_token"]))

        message = b''
        for message_tag in message_list:
            message = message + self.pad_bytes(message_tag)

        packet_length = len(message)
        message = packet_length.to_bytes(2, byteorder='big') + message

        client_dict["current_seq_num"] += 1

        return message

    def create_cancel_ack(self, client_dict, cancel_request_dict):
        message_list = []

        message_list.append(("packet_type", 2, 1, "alpha", "S"))
        message_list.append(("message_type", 0, 1, "alpha", "C"))
        message_list.append(("timestamp", 1, 8, "integer", self.getSendingTime()))
        message_list.append(("order_token", 9, 4, "integer", cancel_request_dict["order_token"]))
        message_list.append(("decrement_quantity", 13, 4, "integer", cancel_request_dict["quantity"]))
        message_list.append(("order_canceled_reason", 17, 1, "alpha", "U"))

        message = b''
        for message_tag in message_list:
            message = message + self.pad_bytes(message_tag)

        packet_length = len(message)
        message = packet_length.to_bytes(2, byteorder='big') + message

        client_dict["current_seq_num"] += 1

        return message

    def getSendingTime(self):
        now = datetime.now()
        midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
        nanoseconds = (now - midnight).microseconds * 1000
        return nanoseconds

    def start_sending_heartbeats(self, ouch_client_sock, client_dict):

        heartbeat_thread = threading.Timer(self.heartbeat_frequency, self.start_sending_heartbeats,
                                           [ouch_client_sock, client_dict])
        heartbeat_thread.daemon = True
        heartbeat_thread.start()

        heartbeat = self.create_heartbeat_message(client_dict)
        ouch_client_sock.send(heartbeat)
        print(f"Sending Heartbeat to {ouch_client_sock.sock.getpeername()}: {OUCHParser.parse_ouch_bytes(heartbeat)} ")

    def start(self):

        # Listen for connections from OUCH Client
        self.ouch_server_sock.listen(self.host, self.port)
        self.socket_list.append(self.ouch_server_sock.sock)

        try:
            # Check for incoming messages
            while True:

                read_sockets, _, exception_sockets = select.select(self.socket_list, [], self.socket_list, 0)

                for notified_socket in read_sockets:
                    # If notified socket is a server socket - new connection, accept it
                    if notified_socket == self.ouch_server_sock.sock:
                        # Accept new connection
                        client_socket, client_address = self.ouch_server_sock.sock.accept()
                        print(f"Accepting new connection from {client_address}")
                        self.socket_list.append(client_socket)
                    else:
                        # Receive messages from client
                        ouch_client_sock = OUCHSocketHandler(notified_socket)
                        received_messages = ouch_client_sock.receive()

                        for received_message in received_messages:

                            ouch_dict = OUCHParser.parse_ouch_bytes(received_message)

                            if ouch_dict["packet_type"] == "L":
                                # Found a login request, send a login response
                                print("Received Login Request")
                                requested_session = ouch_dict["requested_session"]

                                self.clients_dict = {
                                                    notified_socket:
                                                    {
                                                        "requested_session": requested_session,
                                                        "current_seq_num": 1
                                                    }
                                }

                                login_response = self.create_login_response(self.clients_dict[notified_socket])
                                ouch_client_sock.send(login_response)
                                print("Sent Login Response")
                                # Start sending Heartbeats
                                self.start_sending_heartbeats(ouch_client_sock, self.clients_dict[notified_socket])
                            elif ouch_dict["packet_type"] == "U":
                                if "message_type" in ouch_dict:
                                    if ouch_dict["message_type"] == "O":
                                        # Found a new order, send a new order ack
                                        print("Received New Order:" + str(ouch_dict))
                                        client_dict = self.clients_dict[notified_socket]
                                        new_order_ack = self.create_new_order_ack(client_dict, ouch_dict)
                                        ouch_client_sock.send(new_order_ack)
                                        print(f"Sending New Order Ack to {ouch_client_sock.sock.getpeername()}: {OUCHParser.parse_ouch_bytes(new_order_ack)} ")
                                    elif ouch_dict["message_type"] == "U":
                                        # Found a replace order, send a replace result
                                        print("Received Replace Order:" + str(ouch_dict))
                                        client_dict = self.clients_dict[notified_socket]
                                        replace_result = self.create_replace_ack(client_dict, ouch_dict)
                                        ouch_client_sock.send(replace_result)
                                        print(f"Sending Order Replaced to {ouch_client_sock.sock.getpeername()}: {OUCHParser.parse_ouch_bytes(replace_result)} ")
                                    elif ouch_dict["message_type"] == "X":
                                        # Found a cancel order, send a cancel result
                                        print("Received Cancel Order:" + str(ouch_dict))
                                        client_dict = self.clients_dict[notified_socket]
                                        cancel_result = self.create_cancel_ack(client_dict, ouch_dict)
                                        ouch_client_sock.send(cancel_result)
                                        print(f"Sending Order Canceled to {ouch_client_sock.sock.getpeername()}: {OUCHParser.parse_ouch_bytes(cancel_result)} ")

        except KeyboardInterrupt:
            print("caught keyboard interrupt, exiting")
        finally:
            for sock in self.socket_list:
                sock.close()

if __name__ == "__main__":

    if len(sys.argv) != 3:
        print("usage:", sys.argv[0], "<host> <port>")
        sys.exit(1)

    main_host, main_port = sys.argv[1], int(sys.argv[2])

    ouch_app_server = OUCHAppServer(main_host, main_port)

    # Start the OUCH Server
    ouch_app_server.start()


#!/usr/bin/python3
"""Module to Write a class Student that defines a student
"""

class Student:
    """Class Student

        Attributes:
            first_name (str): The first name
            last_name (str): The last name
            age (int): The age
    """

    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def to_json(self, attrs=None):
        student = {}
        if type(attrs) == list:
            for arg in attrs:
                if arg in list(self.__dict__.keys()):
                    student[arg] = self.__dict__[arg]
        else:
            student = {
                        'first_name': self.first_name,
                        'last_name': self.last_name,
                        'age': self.age
                    }
        return (student)


# Annotate Linux/ARM (OABI) system calls and arguments.
#@author b0bb
#@category Pwn
#@keybinding
#@menupath Analysis.Pwn.Syscalls.arm.oabi
#@toolbar

from lib.Syscalls import Syscalls
import ghidra.app.util.opinion.ElfLoader as ElfLoader

def run():

    if currentProgram.getExecutableFormat() != ElfLoader.ELF_NAME:
        popup('Not an ELF file, cannot continue')
        return

    arch = 'arm'
    abi  = 'oabi'

    obj = Syscalls(currentProgram, currentSelection, monitor, arch, abi)

run()


def count_zeros(number):
    count = 0
    while number > 0:
        digit = number % 10
        if digit == 0:
            count += 1
        number //= 10
    return count

user_number = input("Enter a number: ")
num = int(user_number)

zero_count = count_zeros(num)

print(f"The number of zeros in {num} is {zero_count}")


# coding: utf-8

import requests
from bs4 import BeautifulSoup
from requests import ReadTimeout
import csv
import pandas as pd

# filename=r"gaofen1995.csv"
# csv_file = open(filename, 'a+', newline='')
# csv_writer = csv.writer(csv_file)

def findCountry(id):
    country_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\country.csv"
    dfcounty = pd.read_csv(country_path, encoding='utf-8', chunksize=500, header=None)
    for chunk_country in dfcounty:
        lines_country=chunk_country.values
        if id in lines_country[:,0]:
            # print("country find")
            index_country=lines_country[:,0].tolist().index(id)
            country=lines_country[index_country][2]
            #获取country.csv中的国家信息
            return country
    print("未找 国家信息")
    return None

def findType(id):
    type_path = r"D:\个人\学习\大三下\数据可视化\实验\大作业\ml-25m\movies.csv"
    dftype = pd.read_csv(type_path, encoding='utf-8', chunksize=500)
    for chunk_type in dftype:
        lines_type=chunk_type.values
        if id in lines_type[:,0]:
            # print("type find")
            index_type=lines_type[:,0].tolist().index(id)
            type=lines_type[index_type][2]
            #获取country.csv中的国家信息
            return type
    print("未找 类型信息")
    return None

def findTags(id):
    tag_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\tags.csv"
    dftag = pd.read_csv(tag_path, encoding='utf-8',chunksize=500, header=None)
    for chunk_tag in dftag:
        lines_tag=chunk_tag.values
        if id in lines_tag[:,0]:
            # print("type find")
            index_tag=lines_tag[:,0].tolist().index(id)
            tag = lines_tag[index_tag][1:]
            #获取tags.csv中的标签
            return tag
    print("未找 标签信息")
    return None

def findYear(id):
    year_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\year.csv"
    dfyear = pd.read_csv(year_path, encoding='gbk', chunksize=500, header=None)
    for chunk_year in dfyear:
        lines_year=chunk_year.values
        if id in lines_year[:,0]:
            # print("type find")
            index_year=lines_year[:,0].tolist().index(id)
            year = lines_year[index_year][1:]
            #获取tags.csv中的标签
            return year
    print("未找 年份信息")
    return None

if __name__ == "__main__":
    begRow = 0  # 跳过表格的前begRow行
    # redRow=668#读取redRow条数据
    locat_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\1995-2019gaofen\gaofen2019.csv"

    new_path=r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\1995-2019gaofenALL\G2019A.csv"
    csv_file = open(new_path, 'a+', newline='')
    csv_writer = csv.writer(csv_file)

    error_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\1995-2019gaofenALL\G2019A_error.csv"
    csv_fileE = open(error_path, 'a+', newline='')
    csv_writerE = csv.writer(csv_fileE)

    notag_path = r"D:\个人\学习\大三下\数据可视化\大作业\数据处理\1995-2019gaofenALL\G2019A_notag.csv"
    csv_fileN = open(notag_path, 'a+', newline='')
    csv_writerN = csv.writer(csv_fileN)

    df = pd.read_csv(locat_path, encoding='utf-8', chunksize=500,header=None)

    count=0

    for chunk in df:
        lines=chunk.values.tolist()
        for i in range(len(lines)):
            count=count+1;
            mid = int(lines[i][0])
            print("现在开始读取第{}条电影id{}".format(count,mid))
            # id/名称/电影年份/国家/类型/评分/评分人数/标签/评分与生产时间间隔
            # mid = 1
            newline = []
            #电影id
            newline.append(mid)
            # print(newline)
            # 获取year.csv中电影的年份信息
            try:
                year_info = findYear(mid)
                newline.extend(year_info)
                # 获取counry,csv中的国家信息
                country_info = findCountry(mid)
                newline.append(country_info)
                # 获取movies.csv中的电影类型信息
                type_info = findType(mid)
                newline.append(type_info)
                # 获取高低分.csv中的分数信息 平均分，评分人数
                newline.append(lines[i][2])
                newline.append(lines[i][3])
            except:
                csv_writerE.writerow([mid])
                print("电影id：{}信息查找错误".format(mid))
                continue

            # 获取处理后的tags.csv中电影标签信息
            try:
                tag_info = findTags(mid)
                newline.extend(tag_info)
            except:
                print(newline)
                csv_writerN.writerow(newline)
                continue
            print(newline)
            # 写入新的表格中
            csv_writer.writerow(newline)

    print("over")
    csv_file.close()  # 关闭csv文件
    csv_fileE.close()
    csv_fileN.close()

# def get_movie_id(movie_csv_path):
#     movie_dct = {}
#
#     lines=df.values.tolist()
#     for i in range(len(lines)):
#         str_line=int(lines[i][1])
#         str_line=str(str_line)
#         index=int(lines[i][0])
#         index=str(index)
#         movie_dct[index]=str_line
#     return movie_dct
#
# if __name__ == "__main__":
#
#     url = 'https://www.imdb.com/title/tt'
#
#     cur_movie_id = None
#     cur_imdb_id = None
#
#
#     # 存放每一步电影的id和imdb的id
#     movie_dct = get_movie_id(r"D:\个人\学习\大三下\数据可视化\实验\大作业\ml-25m\links.csv")
#     count=0
#     for movie_id, imdb_id in movie_dct.items():
#         count=count+1
#         countall=count+begRow
#         print("现在开始读取第{}条数据".format(countall))
#         country = None
#         cur_movie_id = movie_id
#         cur_imdb_id = imdb_id
#         addZeroNum = 7-len(str(cur_imdb_id))
#         if addZeroNum == 0:
#             URL = url + cur_imdb_id
#         elif addZeroNum == 1:
#             URL = url + '0' + cur_imdb_id
#         elif addZeroNum == 2:
#             URL = url + '00' + cur_imdb_id
#         elif addZeroNum == 3:
#             URL = url + '000' + cur_imdb_id
#         elif addZeroNum == 4:
#             URL = url + '0000' + cur_imdb_id
#         elif addZeroNum == 5:
#             URL = url + '00000' + cur_imdb_id
#         elif addZeroNum == 6:
#             URL = url + '000000' + cur_imdb_id
#
#         print(URL)
#
#         try:
#             strhtml = requests.get(URL, timeout=30)  # Get方式获取网页数据
#             if strhtml.status_code == 200:
#                 print('correct')
#             else:
#                 print('error')
#                 csv_writer2.writerow([cur_movie_id, cur_imdb_id])  # 将数据存入csv文件
#                 continue
#             soup = BeautifulSoup(strhtml.text, 'lxml')
#             for i, tags in enumerate(soup.find_all(class_='txt-block')):
#                 for h4 in tags.find_all('h4'):
#                     title = h4.get_text()  # 例如Director
#                     # print(title)
#                     if title == 'Country:':
#                         for _, a in enumerate(h4.next_siblings):
#                             if a.name == 'a':
#                                 country = a.get_text()
#             print(country)
#
#         except (ConnectionError, ReadTimeout):
#             print('Crawling Failed', URL)
#             csv_writer2.writerow([cur_movie_id, cur_imdb_id])  # 将数据存入csv文件
#             continue
#         except:
#             print(cur_movie_id + 'is error')
#             csv_writer2.writerow([cur_movie_id, cur_imdb_id])  # 将数据存入csv文件
#             continue
#         if country==None:
#             csv_writer2.writerow([cur_movie_id, cur_imdb_id])
#             print("country is None")
#         else:
#             print(cur_movie_id, cur_imdb_id, country)
#             csv_writer.writerow([cur_movie_id, cur_imdb_id, country])  # 将数据存入csv文件
#     csv_file.close()  # 关闭csv文件


from typing import TypedDict
from datetime import timedelta
from django.utils import timezone
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import ModeratorRanking
from external_services.brainly_api import BrainlyGraphQLAPI, Market, transform_graphql_id

class RankingPlace(TypedDict):
    place: int
    points: int
    user_id: int

class GetDailyRankings(APIView):
    def get(self, request):
        market = Market.US

        graphql_api = BrainlyGraphQLAPI(market=market)
        gql_res = graphql_api.execute_query("""
        query GetModeratorRanking {
        userRankings(rankingType: MODERATOR_DAILY) {
            points
            user {id}
        }
        }
        """)

        bulk_data: list[ModeratorRanking] = []
        for place in gql_res.data["userRankings"]:
            user_id = transform_graphql_id(place["user"]["id"])
            user_global_id = f"{market.value}:{user_id}"

            bulk_data.append(
                ModeratorRanking(
                    moderator_id=user_global_id,
                    value=place["points"],
                    market=market.value,
                    time=timezone.now()
                )
            )

        #moderators =

        #rows = ModeratorRanking.objects.bulk_create(bulk_data)

        #print(rows)
        return Response("OK")
        #return Response(places)
        #range = (timezone.now() - timedelta(days=1), timezone.now())

        #rankings = ModeratorRanking.timescale.select_related("moderator").filter(time__range=range).time_bucket("time", "2 hours")
        #print(rankings)
        #return Response("hello")
        # items = Item.objects.all()
        # serializer = ItemSerializer(items, many=True)
        # return Response(serializer.data)


import subprocess as sp
import sys
from hashlib import sha256
from pathlib import Path

import pytest
import yaml

from qzemoji.base import AsyncEngineFactory
from qzemoji.orm import EmojiTable

pytestmark = pytest.mark.asyncio

async def test_build():
    r = sp.run(
        ["python", "script/build.py", "-o", "tmp/build.db"],
        executable=sys.executable,
        capture_output=True,
    )
    assert not r.stderr
    h1 = r.stdout.decode().rstrip()

    with open("data/emoji.yml", encoding="utf8") as f:
        d = yaml.safe_load(f)
    s = ";".join(f"{k}={d[k]}" for k in sorted(d))
    h2 = sha256(s.encode("utf8")).hexdigest().lower()
    assert h1 == h2

    out = Path(f"tmp/build.db")

    async with AsyncEngineFactory.sqlite3(out) as engine:
        built = EmojiTable(engine)
        assert not await built.is_corrupt()
        assert h2 == await built.sha256()

    out.unlink()


import queue
# init direction vectors
dr = [-1, 1, 0, 0]
dc = [0, 0, 1, -1]
MAX = 250
image = [[] for _ in range(MAX)]
while True:
  N, M = map(int, input().strip().split())
  if N == 0 and M == 0: break

  slick_fre = [0]*(M*N + 1)
  # MxN matrix of False values to track visited nodes at [i,j]
  visited = [[False for i in range (M)] for _ in range(N)]

  for i in range(N):
    image[i] = list(map(int, input().strip().split()))

  q = queue.Queue()
  total = 0
  for i in range(N):
    for j in range(M):
      if visited[i][j]: continue
      if not image[i][j]: continue
      visited[i][j] = True
      q.put((i,j))

      move_count = 1
      while not q.empty():
        (r,c) = q.get()
        for k in range(4):
          rr = r + dr[k]
          cc = c + dc[k]
          # Skip out of bounds location
          if rr < 0 or cc < 0: continue
          if rr >= N or cc >= M: continue
          #skip visited location and blocked cells
          if visited[rr][cc]: continue
          if image[rr][cc] == 0: continue
          move_count +=1
          visited[rr][cc] = True
          q.put((rr,cc))
      slick_fre[move_count] +=1
      total += 1
  print(total)
  for i in range(N*M+1):
    if(slick_fre[i] != 0):
      print(i, slick_fre[i])


#
# @lc app=leetcode id=104 lang=python3
#
# [104] Maximum Depth of Binary Tree
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
  def dfs(self, curr, depth):
    if not curr:
      return depth-1

    left = self.dfs(curr.left, depth+1)
    right = self.dfs(curr.right, depth+1)

    return max(left, right)

  def maxDepth(self, root: Optional[TreeNode]) -> int:
    if not root:
      return 0

    return self.dfs(root, 1)

# @lc code=end


import shutil
import sys
import os
import subprocess
import numpy as np
import cv2

def create_video(video_folder, format, out_file):
    file_and_format = f'{video_folder}/{format}'
    frames = 500
    for i in range(1, 500):
        print(file_and_format)
        frame_num = file_and_format.replace('*', '{:04}'.format(i))
        prev_frame_num = file_and_format.replace('*', '{:04}'.format(i-1))
        print(prev_frame_num)
        if not os.path.isfile(frame_num):
            shutil.copy(prev_frame_num, frame_num)

    frame_rate = 10
    subprocess.call(['ffmpeg',
                    '-framerate', str(frame_rate),
                    '-i',
                     '{}/{}'.format(video_folder, format.replace('*', '%04d')),
                     '-pix_fmt', 'yuv420p',
                     '{}.mp4'.format(out_file)])

def merge_frames(in_formats, nframes, out_format):
    for i in range(0, nframes):
        curr_imgs = []
        for in_format in in_formats:

            frame_num = in_format.replace('*', '{:04}'.format(i))
            prev_frame_num = in_format.replace('*', '{:04}'.format(i - 1))

            if not os.path.isfile(frame_num):
                shutil.copy(prev_frame_num, frame_num)
            curr_imgs.append(cv2.imread(frame_num))
        img_join = np.concatenate(curr_imgs, 1)
        cv2.imwrite(out_format.replace('*', '{:04}'.format(i)), img_join)

if __name__ == '__main__':
    # merge_frames(['/Users/xavierpuig/Desktop/test_videos/bob_with_info/action_*.png',
    #               '/Users/xavierpuig/Desktop/test_videos/alice_with_info/action_*.png'], 570,
    #              '/Users/xavierpuig/Desktop/test_videos/merged2_info/Action_*_normal.png')
    # create_video('/Users/xavierpuig/Desktop/test_videos/merged2_info/',
    #              'Action_*_normal.png',
    #              '/Users/xavierpuig/Desktop/test_videos/alice_and_bob_info.mp4')
    create_video('/Users/xavierpuig/Desktop/test_videos/alice_with_info_belief/',
                 'action_*.png',
                 '/Users/xavierpuig/Desktop/test_videos/alice_info_goals.mp4')
    #create_video(sys.argv[1], sys.argv[2], sys.argv[3])


import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

plt.style.use('seaborn-deep')
plt.rcParams['font.family'] = 'Arial'
plt.rcParams['font.size'] = 32
plt.rcParams['axes.labelsize'] = 32
plt.rcParams['axes.titlesize'] = 32
plt.rcParams['legend.fontsize'] = 32
plt.rcParams["figure.figsize"] = (9,5)
plt.rcParams['svg.fonttype'] = 'none'

SAVE_ROOT = "figs/"

def main():
    os.makedirs( SAVE_ROOT ,exist_ok=True )

    # ['sim_BBA: bitrate: 1.2% rebuf: 0.0585' ,'sim_RobustMPC: bitrate: 1.22% rebuf: 0.033' ,
    #  'sim_udr_1: bitrate: 1.2% rebuf: 0.0338' ,'sim_udr_2: bitrate: 1.04% rebuf: 0.0195' ,
    #  'sim_udr_3: bitrate: 1.1% rebuf: 0.0237' ,'sim_adr: bitrate: 1.11% rebuf: 0.0148']

    plt.rcParams['font.size'] = 32
    plt.rcParams['axes.labelsize'] = 32
    plt.rcParams['axes.titlesize'] = 32
    plt.rcParams['legend.fontsize'] = 32
    fig1, ax1 = plt.subplots(figsize=(9, 5))

    fcc_genet_bitrate, fcc_genet_rebuf = 1.17, 0.014

    fcc_udr1_bitrate ,fcc_udr1_rebuf = 1.2 ,0.034
    fcc_udr2_bitrate ,fcc_udr2_rebuf = 1.04 ,0.019
    fcc_udr3_bitrate ,fcc_udr3_rebuf = 1.1 ,0.023

    fcc_bba_bitrate ,fcc_bba_rebuf = 1.2 , 0.058
    fcc_mpc_bitrate ,fcc_mpc_rebuf = 1.22 ,0.033
    fcc_oboe_bitrate ,fcc_oboe_rebuf = 1.16 , 0.021

    msize = 200

    ax1.scatter( [fcc_bba_rebuf],[fcc_bba_bitrate] ,marker='d' ,color='C0' ,s=msize ,label='BBA' )
    ax1.scatter( [fcc_mpc_rebuf] ,[fcc_mpc_bitrate] ,marker='>' ,color='C1' ,s=msize ,label='MPC' )
    ax1.scatter( [fcc_oboe_rebuf] ,[fcc_oboe_bitrate] ,marker='v' ,color='darkorange' ,s=msize ,label='Oboe' )
    ax1.scatter( [fcc_udr1_rebuf] , [fcc_udr1_bitrate] ,marker='^' ,color='C3' ,s=msize ,label='RL1' )
    ax1.scatter( [fcc_udr2_rebuf] ,[fcc_udr2_bitrate] ,marker='<' ,color='C4' ,s=msize ,label='RL2' )
    ax1.scatter( [fcc_udr3_rebuf] , [fcc_udr3_bitrate] , marker='p' ,color='C5' ,s=msize ,label='RL3' )
    ax1.scatter( [fcc_genet_rebuf] ,[fcc_genet_bitrate] ,s=msize ,color='C2' ,label='Genet' )

    ax1.annotate('BBA', ( fcc_bba_rebuf, fcc_bba_bitrate-0.03))
    ax1.annotate('MPC', ( fcc_mpc_rebuf, fcc_mpc_bitrate-0.01))
    ax1.annotate('Oboe', (fcc_oboe_rebuf+0.004, fcc_oboe_bitrate-0.025))
    ax1.annotate('RL1', (fcc_udr1_rebuf+0.002, fcc_udr1_bitrate-0.03))
    ax1.annotate('RL2', (fcc_udr2_rebuf+0.003, fcc_udr2_bitrate+0.01))
    ax1.annotate('RL3', (fcc_udr3_rebuf+0.007, fcc_udr3_bitrate-0.01))
    ax1.annotate('Genet', ( fcc_genet_rebuf+0.005, fcc_genet_bitrate+0.02))

    ax1.set_ylabel('Bitrate (Mbps)')
    ax1.set_xlabel('90th percentile rebuffering ratio (%)')
    ax1.invert_xaxis()
    ax1.spines['top'].set_visible( False )
    ax1.spines['right'].set_visible( False )
    #ax.set_ylim(0.03, -0.01)

    fig1.legend(bbox_to_anchor=(0, 1.02, 1, 0.14), ncol=4, loc="upper center",
                borderaxespad=0, borderpad=0.2, columnspacing=0.01, handletextpad=0.001)

    # svg_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.svg')
    # pdf_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.pdf')
    # fig1.savefig(pdf_file,  bbox_inches='tight')
    # # os.system("inkscape {} --export-pdf={}".format(svg_file, pdf_file))
    # # os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

    svg_file = os.path.join( SAVE_ROOT ,'mahi_fcc_arrow.svg' )
    pdf_file = os.path.join( SAVE_ROOT ,'mahi_fcc_arrow.pdf' )
    fig1.savefig( pdf_file ,bbox_inches='tight' )
    # os.system( "inkscape {} --export-pdf={}".format( svg_file ,pdf_file ) )
    # os.system( "pdfcrop --margins 1 {} {}".format( pdf_file ,pdf_file ) )

    fig2, ax2 = plt.subplots(figsize=(9, 5))

    # ['sim_BBA: bitrate: 1.03% rebuf: 0.07658' ,'sim_RobustMPC: bitrate: 1.05% rebuf: 0.05053' ,
    #  'sim_udr_1: bitrate: 1.04% rebuf: 0.07323' ,'sim_udr_2: bitrate: 0.96% rebuf: 0.04276' ,
    #  'sim_udr_3: bitrate: 0.95% rebuf: 0.04796' ,'sim_adr: bitrate: 0.95% rebuf: 0.04498']

    norway_genet_bitrate ,norway_genet_rebuf = 1.04 ,0.043

    norway_udr1_bitrate ,norway_udr1_rebuf = 1.04 ,0.073
    norway_udr2_bitrate ,norway_udr2_rebuf = 0.96 ,0.043
    norway_udr3_bitrate ,norway_udr3_rebuf = 0.95 ,0.048

    norway_bba_bitrate ,norway_bba_rebuf = 1.03 ,0.077
    norway_mpc_bitrate ,norway_mpc_rebuf = 1.05 ,0.054
    norway_oboe_bitrate ,norway_oboe_rebuf = 1.04 ,0.051

    ax2.scatter( [norway_bba_rebuf], [norway_bba_bitrate] ,marker='d', color='C0', s=msize ,label='BBA' )
    ax2.scatter( [norway_mpc_rebuf] ,[norway_mpc_bitrate] ,marker='>' ,color='C1',s=msize ,label='MPC' )
    ax2.scatter( [norway_oboe_rebuf] ,[norway_oboe_bitrate] ,marker='v' ,color='darkorange',s=msize ,label='Oboe' )
    ax2.scatter( [norway_udr1_rebuf] ,[norway_udr1_bitrate] ,marker='^' ,color='C3',s=msize ,label='RL1' )
    ax2.scatter( [norway_udr2_rebuf] ,[norway_udr2_bitrate] ,marker='<' ,color='C4',s=msize ,label='RL2' )
    ax2.scatter( [norway_udr3_rebuf] ,[norway_udr3_bitrate] ,marker='p' ,color='C5',s=msize ,label='RL3' )
    ax2.scatter( [norway_genet_rebuf] ,[norway_genet_bitrate] , s=msize ,color='C2', label='Genet' )

    ax2.annotate('BBA', (norway_bba_rebuf+0.0001, norway_bba_bitrate-0.015))
    ax2.annotate('MPC', (norway_mpc_rebuf+0.005, norway_mpc_bitrate-0.02))
    ax2.annotate('Oboe', ( norway_oboe_rebuf+0.001, norway_oboe_bitrate-0.015))
    ax2.annotate('RL1', (norway_udr1_rebuf-0.001, norway_udr1_bitrate-0.01))
    ax2.annotate('RL2', (norway_udr2_rebuf+0.003, norway_udr2_bitrate+0.01))
    ax2.annotate('RL3', (norway_udr3_rebuf+0.006, norway_udr3_bitrate))
    ax2.annotate('Genet', (norway_genet_rebuf+0.005, norway_genet_bitrate+0.01))

    ax2.set_ylabel( 'Bitrate (Mbps)' )
    ax2.set_xlabel( '90th percentile rebuffering ratio (%)' )
    ax2.invert_xaxis()
    ax2.spines['top'].set_visible( False )
    ax2.spines['right'].set_visible( False )

    svg_file = os.path.join( SAVE_ROOT ,'mahi_norway_arrow.svg' )
    pdf_file = os.path.join( SAVE_ROOT , 'mahi_norway_arrow.pdf')
    fig2.savefig( pdf_file ,bbox_inches='tight' )
    # os.system( "inkscape {} --export-pdf={}".format( svg_file ,pdf_file ) )
    # os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

if __name__ == '__main__':
    main()


import copy
class ListNode:
    def __init__(self, data=0, next=None):
        self.data = data
        self.next = next
#Linked List object which is made up of Node objects
class LinkedList:
    def __init__(self):
        #Set head to the first Node object
        self.head = ListNode()
    #insert function to add new Nodes to list
    def insert(self, data):
        new_node = ListNode(data)
        curr = self.head
        while curr.next != None:
            curr = curr.next
        curr.next = new_node

    #performs an insert of data into the respected place in the list rather then at the end by default
    def sortedInsert(self,data):
        if not self.head.next:
            self.insert(data)
        new_node = ListNode(data)
        #handles if the new node is larger then the first data node *discount dummy head*
        if self.head.next.data > new_node.data:
            new_node.next = self.head.next
            self.head.next = new_node
        curr = self.head.next
        while curr.next != None:
            if new_node.data < curr.next.data:
                new_node.next = curr.next
                curr.next = new_node
                return
            curr = curr.next
        if curr.data < new_node.data:
            new_node.next = curr.next
            curr.next = new_node

    #delete a specific node from the list
    def delete(self, data):
        #if list is empty just return
        if not self.head.next:
            return
        curr = self.head.next
        prev = None
        while curr.next != None:
            if curr.next.data == data:
                prev = curr
                curr = curr.next
                prev.next = curr.next
                curr.next = None
                return
            curr = curr.next
        #if we didn't break out of function then we didn't find it
        print("The node with the data ", data, " doesn't exist in this list")

    #Return the length of the List
    def length(self):
        curr = self.head.next
        total = 1
        while curr.next != None:
            total += 1
            curr = curr.next
        return total

    #display List
    def printList(self):
        nodeList = []
        curr = self.head.next
        while curr != None:
            nodeList.append(curr.data)
            curr = curr.next
        print (nodeList)

    #HELPER FUNCTION: GET MIDDLE NODE
    def getMid(self,head) -> ListNode:
        if not head:
            return head
        slow, fast = head,head.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

    #HELPER FUNCTION: MERGE TWO LINKED LISTS:
    def merge(self,l1,l2):
        curr = dummy = ListNode()
        while l1 and l2:
            if l1.data <= l2.data:
                curr.next = l1
                l1 = l1.next if l1.next else None
            else:
                curr.next = l2
                l2 = l2.next if l2.next else None
            curr = curr.next
        if l1:
            curr.next = l1
        elif l2:
            curr.next = l2
        return dummy.next

    def sortList(self):
        if self.head:
            self._sortList(self.head)

    def _sortList(self, head):
        #base case
        if not head or not head.next:
            return head
        #splitting list in two halves
        #utilizing a helper function to get the middle node of the list
        middle = self.getMid(head)
        temp = middle.next
        middle.next = None
        middle = temp
        #now we wanna recurse to break each node to individual nodes from left to right
        left = self._sortList(head)
        right = self._sortList(middle)
        #now we need to merge the two left and right nodes using another helper function
        sortedList = self.merge(left,right)
        return sortedList

    def mergeTwoLists(self,l2):
        if self.head and self.head and l2 and l2.next:
            self._mergeTwoLists(self.head,l2)

    def _mergeTwoLists(self, h1,h2):
        dummy = ListNode()
        if not h1 and h2:
            return h2
        if not h2 and h1:
            return h1
        if not h1 and not h2:
            return
        l1 = self._sortList(h1)
        l2 = self._sortList(h2)
        dummy.next = self.merge(l1.next,l2.next)
        return dummy.next

    def reverseList(self):
        if self.head and self.head.next:
            self.head.next = self._reverseList(self.head.next)
            return self.head
    def _reverseList(self,h):
        if not h:
            return h
        curr = h
        prev = None
        next = curr
        while curr:
            next = curr.next
            curr.next = prev
            prev = curr
            curr = next
        return prev

    def isPalindrome(self):
        if self.head:
            return self._isPalindrome(self.head)
    #function to determine if linked list is a palindrome
    def _isPalindrome(self, node):
        #Deep copy welcome back my old friend
        cur_node = copy.deepcopy(node)
        if not cur_node.next:
            return True
        left = cur_node.next
        middle = self.getMid(cur_node.next)
        temp = middle.next
        right = self._reverseList(temp)
        while (right != None):
            if left.data != right.data:
                return False
            left = left.next
            right = right.next
        return True

    def rotate(self, k):
        if self.head:
            self._rotate(k)

    def _rotate(self,k):
        #Edge case is that if k == 0 then theres no rotations needed
        if k == 0:
            return self.head
        #We need to get the end node of the list aka the tail for later
        tail = self.head
        length = self.length()
        while tail.next:
            tail = tail.next
        #using our length function we can also get length for later use
        #since my length function starts at zero we wanna increment one more time cause we technically
        #start at the 1th node but we have to ignore the dummy header
        #We do this cause if k which is how many rotations is the same or longer then the list
        #we need to mod it because if k == length then no need to rotate
        #Or if k > length we need to mod to get the remainder to determine the rotations
        k %= length
        if k == 0:
            return self.head
        #start iterating again till you get to the kth node
        cur = self.head.next
        #This essentially gets us to the kth node at the end of the list
        for i in range(length-k-1):
            cur = cur.next
        new_head = cur.next
        tail.next = self.head.next
        self.head.next = new_head
        cur.next = None


'''FNN-based CBF
   Haiqiang Niu 08/08/2018
   ( modified 03/13/2019 E. Ozanich)
'''
from numpy.random import seed, randn
seed(1)
import tensorflow as tf
tf.set_random_seed(1)
import time
import numpy as np
import math
import os

from keras.layers import Dense, GaussianNoise, Dropout, BatchNormalization, Activation
from keras.optimizers import Adam,SGD,RMSprop
import keras.backend as K
from config import params
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.callbacks import EarlyStopping, LearningRateScheduler
from keras.regularizers import l1, l2
# instantiate regularizer
reg = l2(0.001)

# # set GPU # #
#os.environ["CUDA_DEVICE_ORDER"] = "00000000:03:00.0"
#os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=params.GPU_frac) # fix memory usage
tf.GPUOptions.allow_growth = True

# don't use early stopping
#callback = EarlyStopping(monitor='val_loss',
#                  min_delta=0.01,
#                  patience=20, # default: 2, number of increasing steps before stopping
#                  verbose=0, mode='min')

# learning rate scheduler
def step_decay(epoch):
    init=1e-4
    drop = 0.5
    epoch_drop = 30
    lrate = init*math.pow(drop, math.floor((1+epoch)/epoch_drop))
    return lrate

lrate = LearningRateScheduler(step_decay)

# # Set Training file ---------------------------------
fname = 'train'

nlayers = 5
params.nhidden = 512

if params.Coh:
    coh = 'coh'
else:
    coh = 'incoh'

modelname = 'FNN_models/%02ddeep_%04dhid_%s_%s_%03dbatch_%s'% (nlayers, params.nhidden, params.output,                                                          params.loss, params.batch_size, coh)
print(modelname)

#time.sleep(4600) # to delay, if multiple tasks

#-----------------------------------------------------------------
# # load data # #
#  training data
# two sources
x_train = np.load(params.fpath + 'train/x_' + fname + '.npy')
y_train = np.load(params.fpath + 'train/y_' + fname + '.npy')

print(x_train.shape)

#  validation data
params.fname = str(0) + 'dB/'
x_val = x_train
y_val = y_train

#-----------------------------------------------------------------
# # start TF session # #
sess=tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) # start a Tensorflow session using GPU
tf.set_random_seed(1)
input_size = x_train.shape[1]

# # create model # #
model = Sequential()
#model.add(GaussianNoise(0,input_shape=(input_size,)))
model.add(Dense(params.nhidden, activation='relu', input_shape=(input_size,)))
#model.add(GaussianNoise(0.1))
#model.add(Activation(activation='relu'))#, kernel_regularizer=l2(0.02)))

model.add(Dense(params.nhidden, activation='relu'))
model.add(Dense(params.nhidden, activation='relu'))
model.add(Dense(params.nhidden, activation='relu'))
layer7 = model.add(Dense(params.nhidden, activation='relu')) # visualize this layer

#model.add(Dropout(0.5))

model.add(Dense(params.nclasses, activation=params.output))#, activity_regularizer=l2(0.001)))
model.compile(loss=params.loss,
              optimizer=Adam(0.001))#0.1))
model.summary()

#-----------------------------------------------------------------
# # train model # #
t0 = time.time()
print(modelname)
history = model.fit(x_train, y_train,
                    batch_size=params.batch_size,
                    epochs=params.nepochs,
                   # validation_data=(x_val,y_val),
                    verbose=1, shuffle=False)#, callbacks = [callback])
print('%1d epochs trained in %.2f seconds.' % (params.nepochs, (time.time() - t0)) )

#-----------------------------------------------------------------
# # save model and training loss # #
history = history.history
model.save(modelname + '.h5')
np.savez(modelname + '_history', loss=history['loss'])#, val_loss = history['val_loss'])


from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import sys

from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout.addWidget(self.textEdit)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.editButton = QtWidgets.QPushButton(self.centralwidget)
        self.editButton.setObjectName("editButton")
        self.horizontalLayout.addWidget(self.editButton)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.verticalLayout.addWidget(self.tableWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Запуск"))
        self.editButton.setText(_translate("MainWindow", "Изменить"))

class MyWidget(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.con = sqlite3.connect("films_db.sqlite")
        self.pushButton.clicked.connect(self.update_result)
        self.editButton.clicked.connect(self.update_elems)

    def update_result(self):

        cur = self.con.cursor()
        que = "SELECT * FROM films WHERE " + self.textEdit.toPlainText()
        result = cur.execute(que).fetchall()

        self.tableWidget.setRowCount(len(result))
        self.tableWidget.setColumnCount(len(result[0]))

        for i, elem in enumerate(result):
            for j, val in enumerate(elem):
                self.tableWidget.setItem(i, j, QTableWidgetItem(str(val)))

    def update_elems(self):
        rows = list(set(map(lambda x: x.row(), self.tableWidget.selectedItems())))
        ids = [self.tableWidget.item(i, 0).text() for i in rows]
        valid = QMessageBox.question(self, '',
                                     f"Действительно заменить элементы с id {', '.join(ids)}",
                                     QMessageBox.Yes, QMessageBox.No)
        if valid == QMessageBox.Yes:
            cur = self.con.cursor()
            for i in ids:
                data = cur.execute(f"SELECT * FROM films WHERE id = {i}").fetchone()
                new_data = (data[0], data[1][::-1], data[2] + 1000, data[3], data[4] * 2)
                cur.execute(f"DELETE FROM films WHERE id = {i}")
                cur.execute("INSERT INTO films VALUES (?, ?, ?, ?, ?)", new_data)
            self.con.commit()

sys._excepthook = sys.excepthook

def exception_hook(exctype, value, traceback):
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

sys.excepthook = exception_hook

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyWidget()
    ex.show()
    sys.exit(app.exec())


import tinder.tinder_resource as tinder_resource
import json

facebook_token = ''
facebook_id = ''

# WARNING: THIS TAKES A WHILE!
def get_detailed_matches():
    data = tinder_resource.get_matches()['matches']
    matches = {}
    for match in data:
        user_id = match['person']['_id']
        user = tinder_resource.get_user(user_id)
        matches[user_id] = user['results']

    return matches

def save_detailed_matches():
    save('detailed_matches.json', jsonify(get_detailed_matches()))

def save_data():
    save('matches_data.json', jsonify(tinder_resource.get_matches()['matches']))
    save('profile.json', jsonify(tinder_resource.get_own_profile()))
    save('raw.json', jsonify(tinder_resource.get_matches()))

def save(file_name, data):
    f = open(file_name, 'w')
    f.write(data)
    f.close()

def jsonify(data):
    return json.dumps(data, indent=4)

tinder_resource.auth(facebook_token=facebook_token, facebook_id=facebook_id)

#save_data()
#save_detailed_matches()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 0496_Next_Greater_Element_I.py
# @Author: Xuewen Lei
# @Date  : 2021/9/7
# @Desc  :
from typing import List

class Solution:
    def nextGreaterElement(self, findNums: List[int], nums: List[int]) -> List[int]:
        st, d, ans = [], {}, []
        for v in nums:
            while st and st[-1] < v:
                d[st.pop()] = v
            st.append(v)
        for x in findNums:
            ans.append(d.get(x, -1))
        return ans


import io
import json

from avro.datafile import DataFileReader
from avro.io import DatumReader

def lambda_handler(event, context):
    try:
        return {
            "success": True,
            "num_records": event["num_records"],
            "results": [
                list(
                    json.dumps(
                        list(
                            DataFileReader(
                                io.BytesIO(bytes.fromhex(record[0])), DatumReader()
                            )
                        )
                    )
                )
                for record in event["arguments"]
            ],
        }
    except Exception as e:
        return {
            "success": False,
            "error_msg": f"Error processing Lambda event. Error: {e}",
        }


"""
rms - Our OPAL Application
"""
import copy
from opal.core import menus
from opal.core import application

class Application(application.OpalApplication):
    javascripts = [
        'js/rms/routes.js',
        'js/rms/controllers/directives.js',
        'js/rms/controllers/allocated_clinic_location_helper.js',
        'js/opal/controllers/discharge.js',
        # Uncomment this if you want to implement custom dynamic flows.
        # 'js/rms/flow.js',
    ]
    default_episode_category = 'Referral'

    menuitems = [
        # dict(href="/#/overview", display="Region Overview",
        #      icon="fa fa-dashboard", activepattern="overview"),
        menus.MenuItem(
            href="/#/myclinic",
            display="Morpeth Clinic",
            icon="fa fa-dashboard",
            activepattern="myclinic"),
    ]

    @classmethod
    def get_menu_items(cls, user=None):
        menu_items = []

        if user.is_superuser or user.has_perm('rms.can_refer'):
            menu_items.append(
                menus.MenuItem(
                    href="/pathway/#/referral_form/",
                    display="Make a referral",
                    icon="fa fa-plus",
                    activepattern="pathway/#/referral_form/"),
            )
            menu_items.append(
                menus.MenuItem(
                    href="/#/list/my_referrals",
                    display="My Referrals",
                    icon="fa fa-user",
                    activepattern="my_referrals",
                    template_name="menu_items/my_referrals.html"
                )
            )

        if user.is_superuser or user.has_perm('rms.can_assign_location'):
            menu_items.append(
                menus.MenuItem(
                    href="/#/list/new_referrals",
                    display="Triage Inbox",
                    icon="fa fa-inbox",
                    activepattern="new_referrals"
                )
            )

        if user.is_superuser or user.has_perm('rms.can_confirm_location'):
            menu_items.append(
                menus.MenuItem(
                    href="/#/list/approval_inbox",
                    display="Approval Inbox",
                    icon="fa fa-inbox",
                    activepattern="approval_inbox"
                ),
            )

        menu_items += copy.copy(cls.menuitems)
        return menu_items


import string
punctuations = ("\"", "'", "`", ".", ",", "!", "$", "%", "&", "*",
                "+", "-", "/", "\\", ":", ";", "<", ">", "=", "?",
                "~", "|", "^", "_")
quotes = ("\"", "'", "`", "”", "“")
TS_Word_List = open('TS_Corpus_Turkish_Word_List.txt', 'r').read().split("\n")
abbrs = open('abbr.txt', 'r').read().split("\n")

# Find the index of punctuation in given word
def find_punc_index(word):
    for punc in punctuations:
        punc_index = next((i for i, ch in enumerate(word) if ch in punc), None)
        if punc_index is not None:
            return punc_index

class split_tokens:
    # Completed #
    def in_quotes(word) -> str:
        if word[0] in quotes and word[-1] in quotes:
            return word[0], word[1:-1], word[-1]

    # Completed #
    def punc_initial(word) -> str:
        return word[0], word[1:]

    # Completed #
    def punc_final(word) -> str:
        return word[0:-1], word[-1]

    # Completed #
    def parenthesis(word) -> str:
        if word[-3:] != "(!)":
            part_1 = word[0]
            part_2 = word[1:-1]
            part_3 = word[-1]
            word = part_1, part_2, part_3
        else:
            part_1 = word[0]
            part_2 = word[1:-1]
            part_3 = word[-1]
            word = part_1, part_2, part_3
        return word

    # Completed #
    def multi_emoji(word) -> str:
        for emoji in word:
            print(emoji, "emoji")

    # Completed #
    def exclamation_tweak_word(word) -> str:
        part_1 = word[0:-3]
        part_2 = word[-3:]
        exclamation_tweak_tokenized = part_1, part_2
        return exclamation_tweak_tokenized
####################################################

    # Completed #
    def dot_in(word) -> str:
        word_split = word.split(".")
        the_dot = "."
        # If two pieces form a valid word eg. son.dan
        if str(word_split[0] + word_split[1]).lower() in TS_Word_List:
            part_1 = word_split[0]
            part_2 = word_split[1]
            word = part_1 + part_2
            return word

        # If (first piece and dot) in abbr list eg. Doç.Dr
        if word_split[0] + "." in abbrs:
            part_1 = word_split[0] + "."
            # "abbr"
            if word_split[1] in abbrs:
                part_2 = word_split[1]
                # "abbr"
            elif str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            elif str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                # "OOV"
            word = (part_1, part_2)
            return ",".join(word).split(",")

        # If first piece in abbr list
        # TBMM.Yeni
        if word_split[0] in abbrs:
            part_1 = word_split[0]
            # "abbr"
            if word_split[1] in abbrs:
                part_2 = word_split[1]
                # "abbr"
            elif str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            elif str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                # "OOV"
            word = (part_1, the_dot, part_2)
            return ",".join(word).split(",")

        # If first part is a valid token
        if str(word_split[0]).lower() in TS_Word_List:
            part_1 = word_split[0]
            # "token"
            if str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            elif str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                # "OOV"
            word = (part_1, the_dot, part_2)
            return ",".join(word).split(",")

        # If first part is not a valid token
        if str(word_split[0]).lower() not in TS_Word_List:
            part_1 = word_split[0]
            # "OOV"
            if str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            elif str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                #"OOV"
            word = (part_1, the_dot, part_2)
            return ",".join(word).split(",")
############################################

    # Completed #
    def comma_in(word) -> str:
        word_split = word.split(",")
        the_comma = ","

        # if first piece is token
        if word_split[0] in TS_Word_List:
            part_1 = word_split[0]
            # "token"
            if word_split[1] in abbrs:
                part_2 = word_split[1]
                # "abbr"
            if str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            if str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                # "OOV"
            word = (part_1, the_comma, part_2)
            return word

        # if first piece is OOV
        if word_split[0] not in TS_Word_List:
            part_1 = word_split[0]
            # "OVV"
            if word_split[1] in abbrs:
                part_2 = word_split[1]
                # "abbr"
            if str(word_split[1]).lower() in TS_Word_List:
                part_2 = word_split[1]
                # "token"
            if str(word_split[1]).lower() not in TS_Word_List:
                part_2 = word_split[1]
                # "OOV"
            word = (part_1, the_comma, part_2)
            return word
##################################

    # Completed #
    def multi_punctuation(word) -> str:
        if word[-3:] == "(!)":
            return split_tokens.exclamation_tweak(word)
        else:
            punc_location = find_punc_index(word)
            part1 = word[0:punc_location]
            part2 = word[punc_location:]
            multi_punc_tokenized = part1,part2
            return (multi_punc_tokenized)
    ##################################

    def num_of_initial_punc(word):
        matches = []
        while True:
            for char in word:
                if char in punctuations:
                    matches.append(char)
            return len(matches)
#            else:
#                sil = len(matches)
#               return "".join(matches) + "\n" + word[sil:]


'''
--------------------------------------------------------------------------
Python Runfile
--------------------------------------------------------------------------
License:
Copyright 2023 - Connor Kelly

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------
'''

from PIL import Image, ImageDraw, ImageFont

class Cursor():
    # class variables
    name = None
    cursor_type = None
    index = None
    target = None
    image = None
    limit = None
    shift = None
    target_image = None
    target_list = None
    selection_layer = None
    spacing = 2
    font = None
    position = (11, 40)

    def __init__(self, name="unnamed", cursor_type="None", index=0, target_list = [], target="None") -> None:
        self.name = name
        self.cursor_type = cursor_type
        self.index = index
        self.shift = index
        self.target_list = target_list
        self.target = None
        self.limit = 6
        self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 18)

        # initialize the selection_layer to be a new transparent image.
        # self.image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.target_image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.selection_layer = Image.new("RGBA", (320,240), (0,0,0,0))
        self.image = None
        self.target_image = None
        self.selection_layer = None
        self.draw_target_list()
        self.draw_selection_box()

    def cursor_up(self):
        print("cursor_up")
        self.index -= 1

        # check if we need to shift
        if (self.index < self.shift):
            self.shift_up()

        # redraw the selection box to index - shift
        self.draw_selection_box()

        return self.get_image()

    def cursor_dn(self):
        print("cursor_dn")
        self.index += 1

        # check if we need to shift
        if (self.index > self.shift + self.limit):
            self.shift_dn()

        # redraw the selection box to index - shift
        self.draw_selection_box()

        return self.get_image()

    def shift_up(self): # shifts the target_list upward
        self.shift -= 1
        # redraw the target_list
        self.draw_target_list()

    def shift_dn(self): # shifts the target_list downward
        self.shift += 1
        # redraw the target_list
        self.draw_target_list()

    def get_image(self):
        self.image = Image.new("RGBA", (320,240), (0,0,0,0))
        # self.image.paste(self.selection_layer, (0,0))
        # self.image.paste(self.target_image, (0,0))
        self.image = Image.alpha_composite(self.selection_layer, self.target_image)
        # self.image.show()
        return self.image

    def draw_target_list(self):
        if (self.target_list == []):
            self.target_list = ["Trackname"] * self.limit

        # reset the image
        self.target_image =  Image.new("RGBA", (320,240), (0,0,0,0))

        target_string = "\n".join(self.target_list)

        # draw the multiline target_list
        draw = ImageDraw.Draw(self.target_image)

        draw.text(self.position, target_string, font = self.font, spacing = self.spacing)

        return self.target_image
        # self.target_image.show()

    def draw_selection_box(self):
        self.selection_layer = Image.new("RGBA", (320,240), (0,0,0,0))

        # draw the multiline target_list
        draw = ImageDraw.Draw(self.selection_layer)

        slot = self.index - self.shift
        # x = (0, 320)

        pos = [0, (slot * 20) + 40, 320, ((slot + 1) * 20) + 40]
        print("pos: %s"%(str(pos)))
        #
        draw.rectangle(pos, fill = (100,100,100,255), outline=(0,0,0,0), width =1)
        # self.selection_layer.show()

        return self.selection_layer


import json
import requests
import os
import os.path
import sys

if len(sys.argv) != 2:
    print('Usage: '+sys.argv[0]+' INTXT')
    exit()
pdb_list_file = sys.argv[1]

API_URL='http://www.ebi.ac.uk/pdbe/api/mappings/uniprot/'

with open(pdb_list_file, 'r') as f:
    pdb_list = [line.strip().lower() for line in f.readlines()]

for pdb in pdb_list:
    r = json.loads(requests.get(API_URL + pdb).text)
    uniprot = r[pdb]['UniProt']
    if len(uniprot) == 0:
        print(f'WARNING: {pdb} maps to no UniProtIDs', file=sys.stderr)
    elif len(uniprot) > 1:
        print(f'WARNING: {pdb} maps to multiple UniProtIDs (' + ', '.join(uniprot.keys()) + ')', file=sys.stderr)
    for uni, annot in uniprot.items():
        print(pdb, uni, annot['identifier'], annot['name'], sep='\t')


from generic import AdventRunner

class AdventDaySixteen(AdventRunner):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.rules = {}
        self.your_ticket = []
        self.other_tickets = []

    def processData(self, file_data):
        input_type = 1

        for line in file_data:
            if line == '':
                continue

            if line == 'your ticket:':
                input_type = 2
                continue
            elif line == 'nearby tickets:':
                input_type = 3
                continue

            if input_type == 1:
                self.processRule(line)
            elif input_type == 2:
                self.your_ticket = self.processTicket(line)
            elif input_type == 3:
                self.other_tickets.append(self.processTicket(line))

        if self.additionalConfig['task'] == 1:
            return self.taskOne()
        elif self.additionalConfig['task'] == 2:
            return self.taskTwo()

    def processRule(self, rule):
        name, rules = rule.split(': ')
        options = rules.split(' or ')
        ranges = []
        for option in options:
            low, high = option.split('-')
            ranges.append([int(low),int(high)])
        self.rules[name] = ranges

    def processTicket(self, ticket):
        return list(map(int, ticket.split(',')))

    def testRules(self, field):
        for ranges in self.rules.values():
            if self.testRanges(ranges, field):
                return True
        return False

    def testRanges(self, ranges, field):
        for options in ranges:
            if field >= options[0] and field <= options[1]:
                return True
        return False

    def testRule(self, rule, field_index):
        ranges = self.rules[rule]
        for ticket in self.other_tickets:
            field = ticket[field_index]
            if not self.testRanges(ranges, field):
                return False
        return True

    def taskOne(self):
        total = 0
        for ticket in self.other_tickets:
            for field in ticket:
                if not self.testRules(field):
                    total += field
        return total

    def taskTwo(self):
        removeTickets = []
        for ticket in self.other_tickets:
            for field in ticket:
                if not self.testRules(field):
                    removeTickets.append(ticket)
                    break
        for ticket in removeTickets:
            self.other_tickets.remove(ticket)

        field_lookup = {}
        for rule in self.rules.keys():
            for index in range(0, len(self.your_ticket)):
                if self.testRule(rule, index):
                    if index in field_lookup.keys():
                        field_lookup[index].append(rule)
                    else:
                        field_lookup[index] = [rule]
        final = {}
        running = True
        while running:
            running = False
            for key, value in field_lookup.items():
                if len(value) == 1:
                  final[key] = value[0]
                  running = True
                  field_lookup = removeAssigned(field_lookup, value[0])
        total = 1
        for key, value in final.items():
            if 'departure' in value:
                total *= self.your_ticket[key]

        return total

def removeAssigned(field_lookup, field):
    for value in field_lookup.values():
        if field in value:
            value.remove(field)
    return field_lookup

advent = AdventDaySixteen()
advent.addData("./data/day-16/input.txt")
advent.additionalConfig = {'verbose': True, 'task': 2}
print(advent.runScript())


from csv import DictReader
import ast
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.rdCoordGen import AddCoords

def my_eval(str):
    testes = [
        (int),
        (float)
    ]
    for teste in testes:
        try:
            return teste(str)
        except ValueError:
            continue
    return str

def get_configs(file_path):
    with open(file_path, 'r', encoding='utf-8')as file:
        reader = DictReader(file)
        configs = list(reader)[0]
        for config in configs:
            configs[config] = my_eval(configs[config])
    return configs

def remove_isotopes(smile):
    mol = Chem.MolFromSmiles(smile)
    mol_block = Chem.MolToMolBlock(mol)
    new_mol_block = []
    for line in mol_block.split('\n'):
        if 'M  ISO' not in line:
            new_mol_block += [line]
    new_mol_block = '\n'.join(new_mol_block)
    new_mol = Chem.MolFromMolBlock(new_mol_block)
    new_smile = Chem.MolToSmiles(new_mol)
    return(new_smile)

if __name__ == "__main__":
    smile = r"Cn1c(=O)c2c(nc(/C=C/c3cccc(Cl)c3)n2[11CH3])n(C)c1=O"
    new_smile = remove_isotopes(smile)
    mol = Chem.MolFromSmiles(smile)
    new_mol = Chem.MolFromSmiles(new_smile)
    AddCoords(mol)
    AddCoords(new_mol)
    img = Draw.MolsToGridImage(mols=[mol, new_mol], legends=['original', 'no isotopes'])
    img.show()


import matplotlib.pyplot as plt
from die import Die

# Create a couple of dice
d1 = Die()
d2 = Die()

results = list()
for roll in range(5000):
    results.append(d1.roll_die() + d2.roll_die())

frequencies = list()
max_result = d1.num_sides + d2.num_sides
for value in range(2, max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)

x_values = [x for x in range(2, max_result + 1)]

fig, ax = plt.subplots()
ax.barh(x_values, frequencies)

ax.set_title("Frequency a number is rolled from a pair of D6 dice.")
ax.set_xlabel("Frequency", fontsize=14)
ax.set_ylabel("Dice number", fontsize=14)
ax.tick_params(axis="both", labelsize=14)
plt.show()


from django.test import TestCase
from .forms import MakePaymentForm
class TestPlaceOrderviews(TestCase):

    def test_placeorder_redirects_to_Login_if_not_Loggedin(self):
        response = self.client.get("/placeorder/")
        self.assertRedirects(response, '/accounts/login/?next=/placeorder/', status_code=302,
        target_status_code=200, fetch_redirect_response=True)

    def test_payment(self):
        form_params={
             'credit_card_number': '3333222211110000',
            'cvv': '100',
            'expiry_month': '5',
            'expiry_year': '2022',
            'stripe_id': '********HiddenInput***********'
        }
        response = self.client.post("/", form_params)
        form = MakePaymentForm(form_params)
        self.assertTrue(form.is_valid())


import json
import os

import tensorflow as tf
import tensorflow_datasets as tfds

BATCH_SIZE = 64
BUFFER_SIZE = 10000
LEARNING_RATE = 1e-4

def get_task_name():
    cluster_spec = os.environ.get("CLUSTER_SPEC", None)
    task_name = ""
    if cluster_spec:
        cluster_spec = json.loads(cluster_spec)
        job_index = os.environ["TASK_INDEX"]
        job_name = os.environ["JOB_NAME"]
        task_name = job_name + "_" + job_index

    return task_name

def input_fn(mode, input_context=None):
    datasets, info = tfds.load(
        name="mnist",
        data_dir="/tmp/" + get_task_name() + "/data",
        with_info=True,
        as_supervised=True,
    )

    mnist_dataset = datasets["train"] if mode == tf.estimator.ModeKeys.TRAIN else datasets["test"]

    def scale(image, label):
        image = tf.cast(image, tf.float32)
        image /= 255
        return image, label

    if input_context:
        mnist_dataset = mnist_dataset.shard(
            input_context.num_input_pipelines, input_context.input_pipeline_id
        )

    return mnist_dataset.map(scale).cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

def model_fn(features, labels, mode):
    model = tf.keras.Sequential(
        [
            tf.keras.layers.Conv2D(32, 3, activation="relu", input_shape=(28, 28, 1)),
            tf.keras.layers.MaxPooling2D(),
            tf.keras.layers.Flatten(),
            tf.keras.layers.Dense(64, activation="relu"),
            tf.keras.layers.Dense(10),
        ]
    )

    logits = model(features, training=False)

    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {"logits": logits}
        return tf.estimator.EstimatorSpec(labels=labels, predictions=predictions)

    optimizer = tf.compat.v1.train.GradientDescentOptimizer(learning_rate=LEARNING_RATE)
    loss = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=True, reduction=tf.keras.losses.Reduction.NONE
    )(labels, logits)
    loss = tf.reduce_sum(loss) * (1.0 / BATCH_SIZE)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode, loss=loss)

    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=loss,
        train_op=optimizer.minimize(loss, tf.compat.v1.train.get_or_create_global_step()),
    )

if __name__ == "__main__":
    strategy = tf.distribute.experimental.ParameterServerStrategy()
    config = tf.estimator.RunConfig(train_distribute=strategy)
    estimator = tf.estimator.Estimator(model_fn=model_fn, model_dir="/tmp/model", config=config)
    train_spec = tf.estimator.TrainSpec(input_fn)
    eval_spec = tf.estimator.EvalSpec(input_fn)
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)


from nltk.corpus import movie_reviews

from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import numpy as np
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.metrics import confusion_matrix

#print(movie_reviews.fileids())

def func(list0):
    sents = []
    for blist in list0:
        fsent = ""
        for slist in blist:
            fsent += slist + " "
        sents.append(fsent)
    return sents

sents0 = movie_reviews.sents("neg/cv000_29416.txt")
sents1 = movie_reviews.sents("pos/cv041_21113.txt")

texts0 = func(sents0)
texts1 = func(sents1)

file = open("table.txt", "w")
for i in texts0:
    file.write("neg" + "\t" + i + "\n")
file.close()

print(file)

file = open("table.txt", "a")
for i in texts1:
    file.write("pos" + "\t" + i + "\n")
file.close()

print(file)

df = pd.read_table("table.txt" , sep='\t', header=None, names=['label', 'message'])
df['label'] = df.label.map({'neg': 1, 'pos': 0})
# df['message'] = df['message'].apply(lambda x: ' '.join(x))

count_vect = CountVectorizer()

counts = count_vect.fit_transform(df['message'])

transformer = TfidfTransformer().fit(counts)

counts = transformer.transform(counts)

X_train, X_test, y_train, y_test = train_test_split(counts, df['label'], test_size=0.1, random_state=72)

model = MultinomialNB().fit(X_train, y_train)

#print(X_train, X_test, y_train, y_test)

predicted = model.predict(X_test)

print(np.mean(predicted == y_test))


import asyncio
import threading
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import ThreadingOSCUDPServer
from owo_suit import OWOSuit
import config

try:
    print("Starting OWO Suit...")
    c = config.get()
    owo_suit = OWOSuit(c.owo_ip, c.frequency, c.intensity)
    owo_suit.init()
    dispatcher = Dispatcher()
    owo_suit.map_parameters(dispatcher)
    osc_server = ThreadingOSCUDPServer(
        ("127.0.0.1", c.server_port), dispatcher, asyncio.new_event_loop())
    threading.Thread(target=lambda: osc_server.serve_forever(2),
                     daemon=True).start()
    threading.Thread(target=owo_suit.watch,
                     daemon=True).start()
    input("Press any key to exit\n")
except KeyboardInterrupt:
    print("Shutting Down...\n")
except OSError:
    pass


#!/usr/bin/python3

import random
import sys

n = int(sys.argv[1])
random.seed()
ar = []
i = 0
while i < n:
    new = random.randint(-n, n)
    if new not in ar:
        ar.append(new)
        i += 1

print(' '.join(map(str, ar)))


import unittest

from django import forms

from gn_django.form import utils

class SampleBlogForm(forms.Form):
    title = forms.CharField(max_length=50)
    author = forms.CharField(max_length=50)
    author_email = forms.EmailField(max_length=50, required=False)
    publication_date = forms.DateTimeField()

class TestFormUtils(unittest.TestCase):
    """
    Tests for form util functions
    """

    def test_get_form_error_dict_form_without_error(self):
        form = SampleBlogForm({'title': "My blog", "author": "Brendan", "publication_date": "2017-06-01 12:00:00"})
        self.assertTrue(form.is_valid())
        self.assertEquals(utils.get_form_error_dict(form), {})

    def test_get_form_error_dict_form_with_one_error(self):
        form = SampleBlogForm({"author": "Brendan", "publication_date": "2017-06-01 12:00:00"})
        self.assertFalse(form.is_valid())
        self.assertEquals(utils.get_form_error_dict(form), {'title': 'This field is required'})

    def test_get_form_error_dict_form_with_multiple_errors(self):
        form = SampleBlogForm({"author": "Brendan"*20, "publication_date": "2017-06-01 12:00:00"})
        self.assertFalse(form.is_valid())
        self.assertEquals(utils.get_form_error_dict(form), {'title': 'This field is required', 'author': 'Ensure this value has at most 50 characters (it has 140)'})

    def test_get_form_error_dict_form_with_multiple_errors_single_field(self):
        form = SampleBlogForm({'title': "My blog", "author": "Brendan", "publication_date": "2017-06-01 12:00:00", 'author_email': ("brendan"*50) + "smithatgmail.com"})
        self.assertFalse(form.is_valid())
        self.assertEquals(utils.get_form_error_dict(form), {'author_email': 'Enter a valid email address, Ensure this value has at most 50 characters (it has 366)'})


import ast
import pathlib
import unittest
import sympy
from masmod.ast_worker.if_else.condition_hoist import IfElseConditionHoistTransformer
from masmod.symbols import AnyContext

class TestConditionHoist(unittest.TestCase):

    def _do_test_case(self, case_name: str) -> None:
        with open(
            pathlib.Path(__file__).parent.joinpath(f"{case_name}/case.py"),
            mode="r",
            encoding="utf-8"
        ) as f:
            source_code = f.read()

        context = AnyContext()
        context["t"] = sympy.Symbol("t")
        node = ast.parse(source_code)
        transformer = IfElseConditionHoistTransformer(
            source_code=source_code,
            global_context=context,
            local_context=context
        )
        transformer.visit(node)

        transformed = ast.unparse(node)
        with open(
            pathlib.Path(__file__).parent.joinpath(f"{case_name}/trans.py"),
            mode="w",
            encoding="utf-8"
        ) as f:
            f.write(transformed)

        exec(transformed)

    def test_case_simple(self) -> None:
        self._do_test_case("simple")

    def test_case_multiple(self) -> None:
        self._do_test_case("multiple")

    def test_case_elif(self) -> None:
        self._do_test_case("elif")

    def test_case_named_condition(self) -> None:
        self._do_test_case("named_condition")

    def test_case_and_or(self) -> None:
        self._do_test_case("and_or")


from django.urls import path
from . import views

app_name = 'telemedWebapp'

urlpatterns = [
    path('', views.home_view, name='home'),
    path('register_rehabilitator/', views.register_rehabilitator, name='register_rehabilitator'),
    path('register_patient/', views.register_patient, name='register_patient'),
    path('rehabilitators/', views.view_all_rehabilitators, name='view_all_rehabilitators'),
    path('patients/', views.view_particular_patients, name='view_particular_patients'),
    path('login/', views.login_user, name='login_user'),
    path('logout/', views.logout_user, name='logout_user'),
    path('my_account/', views.my_account_view, name='my_account'),
    path('my_account/edit/', views.edit, name='edit'),
    path('add_exercise/', views.add_exercise, name='add_exercise'),
    path('edit_exercise/', views.edit_exercise, name='edit_exercise'),
    path('delete_exercise/', views.delete_exercise, name='delete_exercise'),
    path('view_exercises/', views.view_exercises, name='view_exercises'),
    path('exercise_plot/', views.exercise_plot, name='exercise_plot')
]


def solution(grid, k):
    from collections import deque
    answer = 0
    n, m = len(grid), len(grid[0])
    move_count=[[0]*len(grid[0]) for _ in range(len(grid))]

    dx=[0,0,1,-1]
    dy=[1,-1,0,0]

    q=deque([(0, 0, 0, 0)])
    while q:
        move, camp, x, y= q.popleft()
        print(move_count)
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]

            if nx<0 or nx>=n or ny<0 or ny>=m or grid[nx][ny]=='#':
                continue

            if move_count[nx][ny]==0: #처음 방문하는 경우
                move_count[nx][ny]=move+1

                if move+1 > k :
                    answer+=1
                    move=0

                q.append((move+1,answer,nx,ny))

    return answer

print(solution(["..FF", "###F", "###."],4))
print(solution(["..FF", "###F", "###."],5))

    # def dfs(move_count,x,y):
    #     for i in range(4):
    #         nx=x+dx[i]
    #         ny=y+dy[i]

    #         if nx<0 or nx>=n or ny<0 or ny>=m :
    #             continue

    #         if grid[nx][ny]=='.' or grid[nx][ny]=='F':
    #             move_count[nx][ny]=move_count[x][y]+1
    #             if move_count==k:
    #                 camp_count[nx][ny]+=1


import numpy as np

def newton(fp, fpp, x0, tol=1e-5, maxiter=100000):
	"""
	fp: function that takes an input x and returns the gradient of f at x
	fpp: function that takes an input x and returns the Hessian of f at x
	x0: initial point
	tol: toleracne parameter in the stopping criterion. Newton's method stops
		 when the 2-norm of the gradient is smaller than tol
	maxiter: maximum number of iterations

	This function should return a list of the sequence of approximate solutions
	x_k produced by each iteration
	"""
	x_traces = [np.array(x0)]
	x = np.array(x0)
	#   START OF YOUR CODE

	while (np.linalg.norm(fp(x))>=tol) and (len(x_traces)<maxiter):
		x = x - np.linalg.inv(fpp(x))@fp(x)
		x_traces += [x]

	#	END OF YOUR CODE

	return x_traces

def damped_newton(f, fp, fpp, x0, alpha=0.5, beta=0.5, tol=1e-5, maxiter=100000):
	"""
	f: function that takes an input x an returns the value of f at x
	fp: function that takes an input x and returns the gradient of f at x
	fpp: function that takes an input x and returns the Hessian of f at x
	x0: initial point in gradient descent
	alpha: parameter in Armijo's rule
				f(x + t * d) > f(x) + t * alpha * <f'(x), d>
	beta: constant factor used in stepsize reduction
	tol: toleracne parameter in the stopping crieterion. Here we stop
		 when the 2-norm of the gradient is smaller than tol
	maxiter: maximum number of iterations in gradient descent.

	This function should return a list of the sequence of approximate solutions
	x_k produced by each iteration and the total number of iterations in the inner loop
	"""
	x_traces = [np.array(x0)]
	stepsize_traces = []
	tot_num_iter = 0

	x = np.array(x0)

	for it in range(maxiter):
		#   START OF YOUR CODE

		direction = -np.linalg.inv(fpp(x))@fp(x)
		stepsize = 1.
		while ( f(x + stepsize*direction) > f(x) + stepsize*alpha*fp(x) @ direction ):
			stepsize *= beta
			tot_num_iter += 1
		x = x + stepsize*direction

		if np.linalg.norm(fp(x)) < tol:
			break

		#	END OF YOUR CODE

		x_traces.append(np.array(x))
		stepsize_traces.append(stepsize)

	return x_traces, stepsize_traces, tot_num_iter


import tushare as ts

pro = ts.pro_api('e826b583d264906bbf69b8d91c22a29c4c25839a7f13e02c7d869e85')

def forecastdata(ts_code, start_time, end_time):
    df = pro.daily(ts_code=ts_code, start_date=start_time, end_date=end_time)
    return df.iloc[:, 1].values, df.iloc[:, 5].values

def getstockprice(ts_code_list, start_time, end_time):
    datadict = {}
    for item in ts_code_list:
        df = pro.daily(ts_code=item, start_date=start_time, end_date=end_time)
        df = df.loc[:, ['trade_date', 'close']]
        # 在这里需要将array转化为list，十分重要，务必牢记
        df = df.values.tolist()
        datadict[item] = df
    return datadict


#!/usr/bin/env Python3

# this will create fake emails to use with my database project

import random
import string

email_list = []
num = 0                                                                 # initialized variable to track iterations.
# = int(input('How many emailss do you need ? '))

def random_char(y):
        return ''.join(random.choice(string.ascii_lowercase)
        for x in range(y))

uniq_email=(random_char(8)+"@gmail.com")
print(uniq_email)
email_list.append(uniq_email)
print(email_list)


#!/usr/bin/env python3

import argparse
import sys
import random

# import numpy as np

# class Decoder(object):
#     def __init__(self, initialProb, transProb, obsProb):
#         self.N = initialProb.shape[0]
#         self.initialProb = initialProb
#         self.transProb = transProb
#         self.obsProb = obsProb
#         assert self.initialProb.shape == (self.N, 1)
#         assert self.transProb.shape == (self.N, self.N)
#         assert self.obsProb.shape[0] == self.N

#     def Obs(self, obs):
#         return self.obsProb[:, obs, None]

#     def Decode(self, obs):
#         trellis = np.zeros((self.N, len(obs)))
#         backpt = np.ones((self.N, len(obs)), 'int32') * -1

#         # initialization
#         trellis[:, 0] = np.squeeze(self.initialProb * self.Obs(obs[0]))

#         for t in xrange(1, len(obs)):
#             trellis[:, t] = (trellis[:, t-1, None].dot(self.Obs(obs[t]).T) * self.transProb).max(0)
#             backpt[:, t] = (np.tile(trellis[:, t-1, None], [1, self.N]) * self.transProb).argmax(0)
#         # termination
#         tokens = [trellis[:, -1].argmax()]
#         for i in xrange(len(obs)-1, 0, -1):
#             tokens.append(backpt[tokens[-1], i])
#         return tokens[::-1]

parser=argparse.ArgumentParser(description='Remove recombination from snp matrix')
parser.add_argument('alnfile',type=argparse.FileType(),help="fastafiles")
parser.add_argument('-p','--posfile',type=argparse.FileType(),default=None)
args =parser.parse_args()

class segment:
    ntest=0
    alnlength=0
    def __init__(self,h,start,end):
        self.hash = h
        self.start = start
        self.end = end
        segment.ntest +=1
        segment.alnlength = l
    def test(self):
        statistic= 1.0*segment.ntest*(hashprob[self.hash]/l*1.0)**(len(self)-1)
        if statistic < 0.05:
            print("Length: {}, stat: {}, ntest: {}, l: {}, hash: {}".format(len(self),statistic,segment.ntest,segment.alnlength,self.hash),file=sys.stderr)
        return statistic >= 0.05
    def __len__(self):
        return self.end-self.start
    def discard(self):
        hashprob[self.hash]-=len(self)
        segment.alnlength-=len(self)
        segment.ntest -=1

def hash(pattern):
    """ Determines if patterns are the same. Note that some nonidentical patterns may still give the same hash. """
    prev=""
    n=1
    hashstring=list()
    for i in pattern:
        if i == prev:
            n+=1
        else:
            hashstring.append(n)
            n=1
        prev=i
    hashstring.append(n)
    return tuple(hashstring[1:])

if args.posfile:
    posList=list()
    for line in args.posfile:
        posList.append(line.strip())
    args.posfile.close()

line=next(args.alnfile)
(norgs,alnlength)=line.split()
(norgs,alnlength)=(int(norgs),int(alnlength))
aln=list()
for i in range(alnlength):
    aln.append(list("N" * norgs))

forspalte=list()
org=0
for line in args.alnfile:
    line=line.strip()
    forspalte.append(line[0:10])
    pos=0
    for char in line[10:]:
        try:
            aln[pos][org]=char
        except IndexError:
            print("norgs=%i\norg=%i\nalnlength=%i\ni=%i" %(norgs,org,alnlength,i))
        pos+=1
    org+=1

""" Calculate frequencies of hash values """
hashprob=dict()

for profile in aln:
    h=hash("".join(profile))
    try:
        hashprob[h]+=1
    except KeyError:
        hashprob[h]=1
l=len(aln)
last=hash("")
lastdeleted=hash("")
sincelast=0

segments=list()
begin = None
for i in range(l):
    sh=hash("".join(aln[i]))
    if sh == last:
        """ A new segment is started """
        pass
    else:
        """ segment ends and is recorded """
        if begin:
            segments.append(segment(last,begin,i))
        last = sh
        begin = i

discardedPositions = set()
lastDiscarded=None
segments.sort(key=len,reverse=True)
for i in range(len(segments)):
    s = segments[i]
    if s.test()== False:
        s.discard()
        # if lastDiscarded and (i-lastDiscarded<4) and (s.hash == segments[lastDiscarded].hash):
        #     discardedPositions.update(range(segments[lastDiscarded].start,s.end))
        #     print("Discarding {} SNPs".format(s.end-segments[lastDiscarded].start),file=sys.stderr)
        #else:
        discardedPositions.update(range(s.start,s.end))
        print("Discarding {} SNPs".format(s.end-s.start),file=sys.stderr)
        lastDiscarded = i
print("%i %i" % (norgs,l-len(discardedPositions)))

for i in range(norgs):
    print(forspalte[i],end='')
    for j in range(l):
        if not j in discardedPositions:
            print(aln[j][i],end='')
    print('')


import time
start = time.time()
number = 2000000
a=0
#s = [1 for i in range(2, number + 1)]
s=[1]*(number-1)
for n in range(2,len(s)):
    if s[n]==1:
        a+=n
        i=n
        max=len(s)/n
        while i<max:
            s[n*i]=0
            i+=1
print(a)

print('--------- time work---------')
end = time.time()

print(end - start)


import time
import gevent
from gevent import monkey; monkey.patch_all()
import pandas as pd
import requests as rs
from lxml import etree
from gevent.pool import Pool, Group

pageurl = 'https://www.hqms.org.cn/usp/roster/index.jsp'
dataurl = 'https://www.hqms.org.cn:443/usp/roster/rosterInfo.jsp'

def getProvinces(url):
    rqpu = rs.get(url)
    html = etree.HTML(rqpu.text)
    provs = html.xpath('//select')
    for p in provs:
        if 'organid' in p.values():
            pv = p
    li = []
    try:
        op = pv.xpath('option')
        for o in op:
            if o.attrib['value'] != '':
                li.append(o.attrib['value'])
    except:
        print('got an error')
    return li

def getData(pid):
    data = {'provinceId': pid, 'htype': '', 'hgrade': '', 'hclass': '', 'hname': ''}
    rq = rs.post(dataurl, data=data)
    df = pd.read_json(rq.text)
    df.to_csv('p{0}.csv'.format(pid), index=False, header=False)

if __name__ == '__main__':
    print(time.asctime(time.localtime(time.time())))
    pli = getProvinces(pageurl)
    group = Group()
    for l in pli:
        group.add(gevent.spawn(getData, l))
    group.join()
    print(time.asctime(time.localtime(time.time())))

    ''' below is sync methos
    for l in pli:
        getData(l)
    '''
    ''' below is async method
    pool = Pool(40)
    for l in pli:
        pool.imap(getData, l)
    pool.join()
    '''


from rest_framework import serializers
from django.db.models import Avg

from .models import Curso, Avaliacao

class AvaliacaoSerializer(serializers.ModelSerializer):

    class Meta:
        extra_kwargs = {
            'email': {'write_only': True} # write_only: True significa que o campo não será exibido na consulta, por segurança
        }
        model = Avaliacao
        fields = (
            'id',
            'curso',
            'nome',
            'email',
            'comentario',
            'avaliacao',
            'criacao',
            'ativo'
        )

    def validate_avaliacao(self, value):
        if value in range(1, 6):
            return value
        raise serializers.ValidationError('A avaliação deve estar entre 1 e 5')
class CursoSerializer(serializers.ModelSerializer):
    # 1 - Nested Relationship
    # avaliacoes = AvaliacaoSerializer(many=True, read_only=True) # novo atributo que tras todas as informações de cada item

    # 2 - Hyperlinked Relationship
    # avaliacoes = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='avaliacao-detail') # mostra um hyperlink para o item

    # 3 - Primary Key Related Field
    avaliacoes = serializers.PrimaryKeyRelatedField(many=True, read_only=True) # ideal pra grandes quantidades pq só mostra o ID do item

    media_avaliacoes = serializers.SerializerMethodField() # novo atributo que traz a média das avaliações

    class Meta:
        model = Curso
        fields = (
            'id',
            'titulo',
            'url',
            'criacao',
            'ativo',
            'avaliacoes',
            'media_avaliacoes'
        )

    def get_media_avaliacoes(self, obj):
        media = obj.avaliacoes.aggregate(Avg('avaliacao')).get('avaliacao__avg')

        if media is None:
            return 0
        return round(media, 1)