from errors import *

############################### CONST VARAIBLES ################################
symbols    = {":", "=", "<", ">", "{", "}", "[", "]", "(", ")", ";", "-", "+", \
"/", "*", ".", ","}
opperands  = {"+", "-", "*", "/"}
types      = {"node", "int", "bool", "char", "byte", "string"}
keywords   = {"for", "while", "in"}
states     = ["READING", "READ_KEYWORD", "READ_TYPE", "READ_WHITESPACE"]

class LexerState:
        #Set to default state on creation
        def __init__(self):
                self.reset()

        def reset(self):
                self.currentState = "NEUTRAL"
                self.parenStack   = []
                self.bracketStack = []
                self.braceStack   = []
                self.variables    = set()
                self.nodes        = set()
                self.existingVar  = False
                self.expectingEquals = False
                self.expectingBrace = False
                self.expectingType = False
                self.expectingVar = False
                self.expectingInt = False
                self.expectingBracket = False
                self.expectingComma = False
                self.expectingIn = False
                self.expectingInt = False
                self.expectingIterable = False
                self.expectingLiteral = False
                self.expectingOperand = False
                self.expectingOpenBrace = False
                self.expectingString = False
                self.possiblyLinking = False
                self.expectingLessThan = False
                self.expectingGreaterThan = False
                self.expectingNode = False
                self.numParens = 0

############################## PRIVATE FUNCTIONS ###############################
# Splits lines of file into words but leaves whitespace in string literals
# Params:
#       stringLines - list of lines in file as strings
#
# Returns:
#       wordsByLine - plaintext lines split into words (list of string lists)
#
def split_whitespace(stringLines):
        wordsByLine = []
        readingString = False
        readQuote = False
        readApost = False

        newWord = ""
        for line in stringLines:
                words = []
                for char in line:
                        if char == '"':
                                if readQuote and not readApost:
                                        readingString = False
                                else:
                                        readingString = True

                                readQuote = not readQuote

                        elif char == "'":
                                if readApost and not readQuote:
                                        readingString = False
                                else:
                                        readingString = True

                                readApost = not readApost

                        if not readingString and char.strip() == "":
                                if not newWord.strip() == "":
                                        words.append(newWord)
                                        newWord = ""
                        else:
                                newWord += char

                if not newWord.strip() == "":
                        words.append(newWord)
                        newWord = ""

                wordsByLine.append(words)

        return wordsByLine

# Separates symbols from non-symbol tokens
# Params:
#       wordList - plaintext lines split into words (list of string lists)
#
# Returns:
#       tokenized - lexed plaintext as tokenized lines (list of string lists)
#
# TODO : DON'T SPLIT BY PUNCTUATION IN STRINGS, DON'T SPLIT ON '.' IN DOUBLE
#        LITERALS
def tokenize_symbols(wordList):
        tokenized = [[] for line in wordList]

        for lineIndex in range(len(wordList)):
                line = wordList[lineIndex]
                for word in line:
                        start = 0
                        for charIndex in range(len(word)):
                                char = word[charIndex]
                                if char in symbols:
                                        if charIndex > start:
                                                tokenized[lineIndex].append(word[start:charIndex])
                                        tokenized[lineIndex].append(word[charIndex:charIndex + 1])
                                        start = charIndex + 1

                        if start != len(word):
                                tokenized[lineIndex].append(word[start:])

        return tokenized

# Determines if new token represents a newly defined variable
# Params:
#       token - next token to be processed
#       state - current state up to next token
#
# Returns:
#       True if token is new variable
#       False otherwise
#
def is_new_variable(token, state):
        variables = state.variables
        nodes = state.nodes

        if token not in variables.union(nodes).union(keywords).union(symbols).union(types):
                if state.currentState != "READING_LITERAL":
                        return True

        return False

# Determines if new token is part of rval for variable assignment
# Params:
#       token - next token to be processed
#       state - current state up to next token
#
# Returns:
#       True if token is part of valid rval
#       False otherwise
#
def is_rval(token, state):
        if state.currentState == "READING_LITERAL":
                return True

        try:
                intval = int(token)
        except: #not int literal
                return False

        return True

# Determines if new token is int literal or int variable
# Params:
#       token - next token to be processed
#       state - current state up to next token
#
# Returns:
#       True if token is int
#       False otherwise
#
def is_int(token, state):
        variables = state.variables
        if token in variables: #TODO CHECK IF INT VARIABLE
                return True
        try:
                x = int(token)
        except:
                return False

        return True

# Updates state appropriately when in neutral state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_neutral(state, token, grammarError):
        if token == 'node':
                state.currentState = "NODE_DECLARED"
        elif is_int(token, state):  #TODO: Replace with is_literal
                state.currentState = "LINKING_NODE"
                state.expectingEquals = True
        elif is_new_variable(token, state):
                state.variables.add(token)
                state.currentState = "DEFINING_VAR"
        elif token in state.variables:
                state.currentState = "DEFINING_VAR"
                state.existingVar = True
        elif token == '}':
                if len(state.braceStack) > 0:
                        state.braceStack.pop()
                else:
                        grammarError = ('error', "Unbalanced brackets '" + token + "'")
                        state.currentState = "NEUTRAL"
        elif token == "{":
                state.braceStack.append(1)
        elif token == "for":
                state.currentState = "FOR_LOOP_DECLARED"
                state.expectingVar = True

        return grammarError

# Updates state appropriately when in node declared state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_node_declared(state, token, grammarError):
        if is_new_variable(token, state):
                state.nodes.add(token)
                state.currentState = "DEFINING_NODE"
        elif token in nodes:
                grammarError = ('error', "Redefinition of node '" + token + "'")
                state.currentState = "NEUTRAL"
        else:
                grammarError = ('error', "Expecting Node name, got '" + token + "'")
                state.currentState = "NEUTRAL"

        return grammarError

# Updates state appropriately when in defining node state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_defining_node(state, token, grammarError):
        if token == '{':
                state.braceStack.append(1)
                state.currentState = "NEUTRAL"
        elif token == '<':
                state.expectingEquals = True
        elif token == "=":
                if state.expectingEquals:
                        state.currentState = "READING_PARAMS"
                        state.numParens = len(state.parenStack)
                        state.expectingVar = True
                else:
                        grammarError = ('error', "Expecting '=', got '" + token + "'")

        return grammarError

# Updates state appropriately when in defining reading params
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_reading_params(state, token, grammarError):
        if token == "{":
                if state.expectingBrace:
                        state.currentState = "NEUTRAL"
                        state.expectingBrace = False
                        state.braceStack.append(1)
                else:
                        grammarError = ('error', "Expecting parameters but got '{'")
        elif token == "(":
                state.parenStack.append(1)
        elif is_int(token, state):
                if not state.expectingInt:
                        grammarError = ('error', "Expecting int but got '" + token + "'")
                else:
                        state.expectingInt = False
                        state.expectingBracket = True
        elif token == ")":
                if len(state.parenStack) == 0:
                        grammarError = ('error', "You have unbalanced parentheses (more open than closed)")
                        state.currentState = "NEUTRAL"
                else:
                        state.parenStack.pop()
                if len(state.parenStack) == state.numParens:
                        state.expectingBrace = True
                        state.numParens = 0
        elif is_new_variable(token, state):
                if state.expectingVar:
                        state.variables.add(token)
                        state.expectingType = True
                        state.expectingVar = False
                else:
                        grammarError = ('error', "Not expecting variable, but got'" + token + "'")
        elif token in types:
                if state.expectingInt or state.expectingBracket:
                        grammarError = ('error', "Expecting integer or closing bracket but got '" + token + "'")
                elif state.expectingType:
                        state.expectingType = False
                else:
                        grammarError = ('error', "Not expecting type but got '" + token + "'")
        elif token == ",":
                if state.expectingType:
                        grammarError = ('error', "Expecting type but got ','")
                elif state.expectingVar:
                        grammarError = ('error', "Expecting var but got ','")
                else:
                        state.expectingVar = True
        elif token == "[":
                if not state.expectingType:
                        grammarError = ('error', "Expecting type but got '" + token + "'")
                elif state.expectingInt:
                        grammarError = ('error', "Expecting integer literal or variable but got '" + token + "'")
                elif state.expectingBracket: #closing bracket
                        grammarError = ('error', "Expecting ']' but got '" + token + "'")
                else:
                        state.expectingInt = True
                        state.expectingBracket = True
        elif token == "]":
                if not state.expectingType:
                        grammarError = ('error', "Expecting type but got '" + token + "'")
                elif not state.expectingBracket:
                        grammarError = ('error', "Unexpected ']'")
                else:
                        state.expectingInt = False
                        state.expectingBracket = False

        return grammarError

# Updates state appropriately when in defining var state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_defining_var(state, token, grammarError):
        if token == ",":
                state.currentState = "DEFINING_TUPLE" #TODO: Support Tuples
        elif token == ":":
                if not state.existingVar:
                        state.currentState = "EXPECTING_="
                else:
                        grammarError = ('error', "Redefinition of variable")
                        state.currentState = "NEUTRAL"
        elif token == "=":
                if not state.existingVar:
                        grammarError = ('error', "Variable undeclared before assignment")
                else:
                        state.currentState = "ASSINGING_TO_VAR"
                        state.expectingLiteral = True
                        state.existingVar = False
        else:
                grammarError = ('error', "During variable declaration got '" + token + "'")
                state.currentState = "NEUTRAL"

        return grammarError

# Updates state appropriately when in expecting equals state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_expecting_equals(state, token, grammarError):
        if token != "=":
                grammarError = ('error', "Expecting '=', got '" + token + "'")
                state.currentState = "NEUTRAL"
        else:
                state.currentState = "ASSINGING_TO_VAR"
                state.expectingLiteral = True
                state.possiblyLinking = True

        return grammarError

# Updates state appropriately when in assigning to var state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_assigning_to_var(state, token, grammarError):
        if state.possiblyLinking and token == ">":
                state.currentState = "LINKING_NODE"
                state.expectingLiteral = False
                state.possiblyLinking = False
        elif is_int(token, state) and state.expectingLiteral:
                state.expectingLiteral = False
                state.expectingOperand = True
        elif token in opperands:
                if not state.expectingOperand:
                        grammarError = ('error', "Unexpected " + token)
                state.expectingLiteral = True
                state.expectingOperand = False
        elif token == '"':
                pass #TODO: This
                #expectingString = True
        elif token == "'":
                pass #TODO: This
                #expectingString = True
        elif token == "[":
                state.expectingInt = True
                state.expectingBracket = True
                state.expectingLiteral = False
        elif token == "]":
                state.expectingInt = False
                state.expectingBracket = False
                state.expectingType = True
        elif is_int(token, state):
                if not state.expectingInt:
                        grammarError = ('error', "Unexpected '" + token + "'")
                else:
                        state.expectingComma = True
                        state.expectingBrace = False
                        state.expectingInt = False
                        state.expectingOperand = True
        elif token in types:
                if not state.expectingType:
                        grammarError = ('error', "Expecting type but got " + token + "'")
                else:
                        state.expectingOpenBrace = True
        elif token == "{":
                state.expectingOpenBrace = False
                state.expectingInt = True
                state.braceStack.append(1)
        elif token == "}":
                state.expectingComma = False
                state.expectingBrace = False
                if len(state.braceStack) > 0:
                        state.braceStack.pop(1)
                else:
                        pass
        elif token == ",":
                if not state.expectingComma:
                        grammarError = ('error', "Unexpected ','")
                else:
                        state.expectingInt = True
                        state.expectingComma = False
        elif token == ";":
                state.currentState = "NEUTRAL"
                state.expectingOpenBrace = False
        else:
                pass
                #grammarError = ('error', "While reading rval, got '" + token + "'")
                #state = "NEUTRAL"
        return grammarError

# Updates state appropriately when in for loop declared state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_for_loop_declared(state, token, grammarError):
        if token == "(":
                state.numParens = len(state.parenStack)
                state.parenStack.append(1)
        elif token == ")":
                if len(state.parenStack) > 0:
                        state.parenStack.pop()
                else:
                        grammarError = ('error', "You have unbalanced parentheses (more open than closed)")
                        state.currentState = "NEUTRAL"
        elif token == "in":
                if not state.expectingIn:
                        grammarError = ('error', "Unexpected 'in'")
                else:
                        state.expectingIterable = True
                        state.expectingComma = False
                        state.expectingVar = False
        elif state.expectingIterable:
                if token not in state.variables: #TODO: PROPER ITERABLE CHECK
                        grammarError = ('error', "Expecting iterable but got '" + token + "'")
                else:
                        state.expectingBrace = True
                        state.expectingIterable = False
        elif token == "{":
                if not state.expectingBrace:
                        grammarError = ('error', "Expecting for loop definition but got '{'")
                state.currentState = "NEUTRAL"
                state.braceStack.append(1)
        elif is_new_variable(token, state) or token in state.variables:
                if state.expectingVar:
                        state.expectingVar = False
                        state.expectingComma = True
                        state.expectingIn = True
                        state.variables.add(token)
                else:
                        grammarError = ('error', "Not expecting variable but got '" + token + "'")
        elif token == ',':
                if state.expectingComma:
                        state.expectingVar = True
                        state.expectingComma = False
                else:
                        grammarError = ('error', "Unexpected ,")

        return grammarError

# Updates state appropriately when in linking node state
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       grammarError - tuple of error type (warning or error) and error message
def process_linking_node(state, token, grammarError):
        if token == "=":
                if not state.expectingEquals:
                        grammarError = ('error', "Unexpected '='")
                else:
                        state.expectingGreaterThan = True
        elif token == ">":
                if not state.expectingGreaterThan:
                        grammarError = ('error', "Unexpected '>'")
                else:
                        state.expectingNode = True
                state.expectingEquals = False
                state.expectingGreaterThan = False
        elif token == "(":
                state.parenStack.append(1)
        elif token == ")":
                state.parenStack.pop()
                state.expectingEquals = True
        elif token == ";":
                state.currentState = "NEUTRAL"
                state.expectingEquals = False
                state.expectingNode = False
        else: #TODO: Check if valid node name and valid params instead of default acceptance
                pass #
                if state.expectingNode:
                        pass

        return grammarError

# Updates state according to next token, determins error if incorrect grammar
# Params:
#       state - current state up to next token
#       token - next token to be processed
#
# Returns:
#       state - updated state according to newly processed token
#       grammarError - tuple of error type (warning or error) and error message
#
def update_state(state, token):
        grammarError = None

        if state.currentState == "NEUTRAL":
                return state, process_neutral(state, token, grammarError)
        elif state.currentState == "NODE_DECLARED":
                return state, process_node_declared(state, token, grammarError)
        elif state.currentState == "DEFINING_NODE":
                return state, process_defining_node(state, token, grammarError)
        elif state.currentState == "READING_PARAMS":
                return state, process_reading_params(state, token, grammarError)
        elif state.currentState == "DEFINING_VAR":
                return state, process_defining_var(state, token, grammarError)
        elif state.currentState == "EXPECTING_=":
                return state, process_expecting_equals(state, token, grammarError)
        elif state.currentState == "ASSINGING_TO_VAR":
                return state, process_assigning_to_var(state, token, grammarError)
        elif state.currentState == "FOR_LOOP_DECLARED":
                return state, process_for_loop_declared(state, token, grammarError)
        elif state.currentState == "LINKING_NODE":
                return state, process_linking_node(state, token, grammarError)
        elif token == ';' and state.currentState != "NEUTRAL":
                grammarError = ('error', "Got ';' before expected end of line")
                state.currentState = "NEUTRAL"
        else: #default case. when finished, should not trigger
                state.currentState = "NEUTRAL"

        return state, grammarError

# Checks grammar of lexed file, reports warnings and errors
# Params:
#       lexed - lexed plaintext as tokenized lines (list of string lists)
#
def check_grammar(lexed):
        global parenStack, bracketStack, braceStack, variables, nodes
        global existingVar
        token = ""
        state = LexerState()

        for lineIndex in range(len(lexed)):
                line = lexed[lineIndex]
                for token in line:
                        state, err = update_state(state, token)
                        if err is not None:
                                errType, message = err
                                if errType == "warning":
                                        compile_warning(lineIndex + 1, message)
                                else:
                                        compile_error(lineIndex + 1, message)
        if len(state.parenStack) > 0:
                compile_error(-1, "You have unbalanced parentheses (more open than closed)")
        if len(state.bracketStack) > 0:
                compile_error(-1, "You have unbalanced brackets (more open than closed)")
        if len(state.braceStack) > 0:
                compile_error(-1, "You have unbalanced braces (more open than closed)")

################################## INTERFACE ###################################
# Lexes file, reports warnings and errors.
# Params:
#       fileText - plaintext of file as string
#
# Returns:
#       lexed - lexed plaintext tokenized lines (list of lists)
#
def lex(fileText):
        splitLines = fileText.split("\n")
        splitWords = split_whitespace(splitLines) #removes whitespace (not in strings)

        lexed = tokenize_symbols(splitWords)

        check_grammar(lexed) #Assure code is ready for parser, report errors

        return lexed


import random

print("가위바위보 게임입니다.")

game = ["가위", "바위", "보"]
rm = random.choice(game)
answer = input("가위,바위,보중 하나를 입력해주세요 : ")

if (answer != rm) :
    print("당신은 AI상대로 패배하였습니다.")
    print("정답은 \"{}\"였습니다.".format(rm))
else :
    print("당신은 AI상대로 승리하였습니다.")


def read_int(prompt, min, max):
    value = ''
    while value == '':
        try:
            value = input(prompt)
            if not min <= int(value) <= max:
                value = ''
                print("Error: the value is not within the permitted range (",min,"...",max,")")
        except:
            value = ''
            print("Error: wrong input")
    return value

v = read_int("Enter a number from -10 to 10: ", -10, 10)

print("The number is:", v)


from pymongo import MongoClient
import os

from dotenv import load_dotenv
load_dotenv()

MONGODB = os.getenv("MONGODB")
print(MONGODB)
db_connection = MongoClient(MONGODB)
db = db_connection.king_tide
collection = db["user"]
collection_files = db["files"]


import xlrd

def get_xls_data(xlsname):
    dataresultrow = [] # 保存从excel表中读取出来的值，每一行为一个list，dataresult中保存了所有行的内容
    dataresultcol = []
    result = []
    dataresulta = [] # 是由dict组成的list，是将dataresult中的内容全部转成字典组成的list：result

    worksheet = xlrd.open_workbook(xlsname)
    sheet_names = worksheet.sheet_names()
    print("sheet:", sheet_names)
    for sheet_namee in sheet_names:
        table = worksheet.sheet_by_name(sheet_namee)
        result.clear()
        dataresultcol.clear()
        dataresultrow.clear()
        for i in range(0, table.nrows):
            dataresultcol.append(table.cell_value(i, 0))
            for j in range(1, table.ncols):
                dataresultrow.append(table.cell_value(i, j))

        dataresult=[dataresultrow[k:k + j] for k in range(0, len(dataresultrow), j)]
        temp = dict(zip(dataresultcol, dataresult))
        dataresulta.append(temp)
        result.append(dataresulta)
    print("\nresult:\n", result)
    return

if __name__ == '__main__':
    xlsname = "tplm_lsc_ver0000.xlsx"  # excel表的名字
    get_xls_data(xlsname)
    print ("json to excel OK")


from flask import Flask, request, jsonify
import picamera
from io import BytesIO

app = Flask(__name__)

# Initialize camera object
camera = picamera.PiCamera()

@app.route('/camera/connect', methods=['GET'])
def connect_camera():
    # Wait for the camera to warm up
    camera.start_preview()
    return 'Camera connected successfully'

@app.route('/camera/capture', methods=['POST'])
def capture_image():
    # Capture an image and return it as a response
    img_io = BytesIO()
    camera.capture(img_io, 'jpeg')
    img_io.seek(0)
    return send_file(img_io, mimetype='image/jpeg')

@app.route('/camera/disconnect', methods=['GET'])
def disconnect_camera():
    # Close the camera connection
    camera.close()
    return 'Camera disconnected successfully'

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


#
# @lc app=leetcode.cn id=478 lang=python3
#
# [478] 在圆内随机生成点
#
from typing import List
from utils import *
from matplotlib import pyplot as plt
# @lc code=start
import random
import math

class Solution:

    def __init__(self, radius: float, x_center: float, y_center: float):
        self.x, self.y, self.r = x_center, y_center, radius

    def randPoint(self) -> List[float]:
        theta = random.random() * 2.0 * math.pi
        r = math.sqrt(random.random())
        return [
            self.x + self.r * r * math.cos(theta),
            self.y + self.r * r * math.sin(theta)
        ]

# Your Solution object will be instantiated and called as such:
# obj = Solution(radius, x_center, y_center)
# param_1 = obj.randPoint()
# @lc code=end
if __name__ == "__main__":
    s = Solution(1, 0, 0)
    x, y = [], []
    for _ in range(1000):
        p = s.randPoint()
        x.append(p[0])
        y.append(p[1])
    plt.scatter(x, y)
    plt.show()

478.在圆内随机生成点.py


from sys import stdin
input = stdin.readline
import copy as cp
from collections import deque, OrderedDict

LINF = 1001002003004005006
INF = 1001001001

def VI(N, init=0):
  return [init for _ in range(N)]
def VVI(N, M, init=0):
  return [[init for _ in range(M)] for _ in range(N)]

def VD(N, init=0.0):
  return [init for _ in range(N)]
def VVD(N, M, init=0.0):
  return [[init for _ in range(M)] for _ in range(N)]

def Decimal(x):
  print("{0:.10f}".format(x))

class Pair:
  def __init__(self, x=0, y=0):
    self.first = x
    self.second = y

  def __repr__(self):
    return '{0} {1}'.format(self.first, self.second)

  def __lt__(self, pi):
    return self.first < pi.first

def POW(x, n):
  ret = 1
  while n:
    if n&1:
      ret *= x
    x *= x
    n >>= 1
  return ret

class Unionfind:
    def __init__(self, N):
        self.N = N
        self.par = [i for i in range(N)] # 節点
        self.rank = [0 for _ in range(N)] # 木の高さ
        self.size = [1 for _ in range(N)] # 節点が属する木の節点数
        self.treeNum = N # 木の数
    def addNode(self, x): # 節点(木)追加
        self.par[x] = x
        self.rank[x] = 0
        self.size[x] = 1
        self.treeNum += 1
    def root(self, x): # 根を探すと同時に経路上にある節点の親が根になるように代入
        if self.par[x] == x:
            return x
        else:
            self.par[x] = Unionfind.root(self, self.par[x])
            return self.par[x]
    def same(self, x, y):
        return Unionfind.root(self, x) == Unionfind.root(self, y)
    def unite(self, x, y):
        x = Unionfind.root(self, x)
        y = Unionfind.root(self, y)
        if x == y:
            return 0
        self.treeNum -= 1
        ret = self.size[x] * self.size[y]
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return ret

    def group(self):
      ret = []
      fixed = [False for _ in range(self.N)]
      for i in range(self.N):
        if (not fixed[i]):
          tmp = []
          for j in range(i, self.N):
            if Unionfind.same(self, i, j):
              tmp.append(j)
              fixed[j] = True
          ret.append(tmp)

      return ret

N, M = map(int, input().rstrip().rsplit())

bridges = VVI(M,2)
for i in range(M):
  a, b = map(int, input().rstrip().rsplit())
  bridges[i] = a-1, b-1

bridges.reverse()

tmp = N * (N-1) // 2

ans = [tmp]

uf = Unionfind(N)
for i in range(M-1):
  a, b = bridges[i]
  cnt = uf.unite(a, b)
  tmp -= cnt
  ans.append(tmp)

ans.reverse()

for i in range(M):
  print(ans[i])


__author__ = 'Bruno'

__author__ = 'Bruno'

from ifes.cih.teclado import Teclado
from ifes.cci.control import Control

class ControleTelaRelatorio:

    def __init__(self):
        self.teclado = Teclado()
        self.posicao=[[0,0],[216,220],[144,220],[216,280],[216,340],[216,400]]
        self.ctrl = Control()

    def controle(self):
        self.teclado.capturaTeclas()

        TelaRelatorio, TelaRelatorioGerado = 4, 12
        opcao = TelaRelatorio
        papelparede, brapagar, seta, brareceber, brvendas, brestoque = 0,1,2,3,4,5

        largura, altura = 0,1

        if self.teclado.teclas[self.teclado.letra] != "":
            print(self.teclado.teclas[self.teclado.letra])

        if self.teclado.teclas[self.teclado.cima] and self.posicao[seta][altura] > self.posicao[brapagar][altura]:
            print("subi")
            self.posicao[seta][altura] -= 60

        if self.teclado.teclas[self.teclado.baixo] and self.posicao[seta][altura] < self.posicao[brestoque][altura]:
            print("desci")
            self.posicao[seta][altura] += 60

        brapagar = (self.posicao[seta][altura] == self.posicao[brapagar][altura])
        brareceber = (self.posicao[seta][altura] == self.posicao[brareceber][altura])
        brvendas = (self.posicao[seta][altura] == self.posicao[brvendas][altura])
        brestoque = (self.posicao[seta][altura] == self.posicao[brestoque][altura])

        if self.teclado.teclas[self.teclado.enter]:
            if brapagar:
                self.ctrl.gerar_relatorio_apagar()
                opcao = TelaRelatorioGerado
                print("brapagar")

            elif brareceber:
                self.ctrl.gerar_relatorio_areceber()
                opcao = TelaRelatorioGerado
                #self.ctrl.controle(1,0)
                print("brareceber")

            elif brvendas:
                self.ctrl.gerar_relatorio_vendas()
                opcao = TelaRelatorioGerado
                print("brvendas")

            elif brestoque:
                self.ctrl.gerar_relatorio_estoque()
                opcao = TelaRelatorioGerado
                print("brestoque")

        return opcao


#!/usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import datetime
import folium
import webbrowser

df = pd.read_csv("dublinbikes_20200101_20200401.csv")

eval_df = df.copy()
eval_df['TIME'] = pd.to_datetime(eval_df['TIME'])
eval_df['date'] = eval_df['TIME'].dt.floor('D')
eval_df = eval_df.groupby(['STATION ID', 'date', 'STATUS', 'ADDRESS']).agg({'AVAILABLE BIKES': 'sum', 'AVAILABLE BIKE STANDS': 'sum'})
eval_df = eval_df.reset_index(level=[0, 1, 2, 3])

top_address = eval_df.groupby('ADDRESS')['AVAILABLE BIKE STANDS'].sum().nlargest(20).index.tolist()
top_df = pd.DataFrame(columns=eval_df.columns)
for address in top_address:
    res = eval_df[eval_df['ADDRESS'] == address]
    top_df = top_df.append(res)
# normal plot show
top_df.set_index('date', inplace=True)
# top_df.groupby('ADDRESS')['AVAILABLE BIKES'].plot(Legend=True)
# fig = plt.figure(figsize=(10, 6))
# ax = fig.add_subplot(2,2)
fig, ax = plt.subplots(2, 2)

# minimize the gap between time interval
def plot_graph(ax, top_address, label):
    for address in top_address:
        ax.plot(top_df[top_df['ADDRESS'] == address].index, top_df[top_df['ADDRESS'] == address]['AVAILABLE BIKE STANDS'], label=address)
    ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
    ax.xaxis.set_major_locator(mdates.DayLocator(interval=3))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    ax.grid(True)
    ax.set_title(f'top {label} dublin station against cycle count')
    ax.set_xlabel("Day")
    ax.set_ylabel("Total Cycle Usage")
    ax.legend(loc="upper left")
    ax.set_xlim([datetime.date(2020, 2, 1), datetime.date(2020, 3, 1)])
    return ax

ax[0, 0] = plot_graph(ax[0, 0], top_address[0:5], '5')
ax[0, 1] = plot_graph(ax[0, 1], top_address[5:10], '5-10')
ax[1, 0] = plot_graph(ax[1, 0], top_address[10:15], '10-15')
ax[1, 1] = plot_graph(ax[1, 1], top_address[15:20], '15-20')
fig.tight_layout()

fig.subplots_adjust(hspace=0.2, wspace=0.2)

plt.show()

loc_df = df[['ADDRESS', 'LATITUDE', 'LONGITUDE']]
loc_df = loc_df.drop_duplicates()
city_latitude = float((loc_df.loc[loc_df['ADDRESS'].isin(['Townsend Street'])])['LATITUDE'])
city_longitude = float((loc_df.loc[loc_df['ADDRESS'].isin(['Townsend Street'])])['LONGITUDE'])
title = 'Dublin Bikes Stand Q1 - 2020'
title_html = '''

{}

             '''.format(title)
m = folium.Map([city_latitude, city_longitude], zoom_start=14)
for add in loc_df['ADDRESS']:
    stand_lat = float((loc_df.loc[loc_df['ADDRESS'].isin([add])])['LATITUDE'])
    stand_lon = float((loc_df.loc[loc_df['ADDRESS'].isin([add])])['LONGITUDE'])
    folium.Marker(location=[stand_lat, stand_lon], popup=f"{add}",
                  icon=folium.Icon(color='darkred', icon='bicycle', prefix='fa')).add_to(m)
m.get_root().html.add_child(folium.Element(title_html))
m.save("stand_location.html")
webbrowser.open("stand_location.html")

# Add additional features to dataset
df1 = df.copy()
df1['STATUS'] = df1['STATUS'].astype('category')
df1['LAST UPDATED'] = pd.to_datetime(df1['LAST UPDATED'])
df1['TIME'] = pd.to_datetime(df1['TIME'])
df1['formatted_time'] = df1['TIME'].dt.floor('h')
df1['day_of_week'] = df1['formatted_time'].dt.strftime('%A')
df1['day_of_month'] = df1['formatted_time'].dt.strftime('%d').astype(np.int64)
df1['hour'] = df1['formatted_time'].dt.strftime('%H').astype(np.int64)
df1['month'] = df1['formatted_time'].dt.strftime('%m').astype(np.int64)
df1['week'] = df1['formatted_time'].dt.strftime('%w').astype(np.int64)
numeric_columns = df1.select_dtypes(['int64']).columns
print(df1[numeric_columns].describe().T)
print(df1.select_dtypes(['category']).describe().T)
print(df1.dtypes)

# Descriptive statistics for all the DateTime features
print(df1.select_dtypes(['datetime64[ns]']).describe(datetime_is_numeric=True).T)
# Descriptive statistics for all the categorial features
print(df1['AVAILABLE BIKES'].describe().T)
print(df1['AVAILABLE BIKE STANDS'].describe().T)
# df1['TIME'] = df1['TIME'].dt.floor('Min')
df1['STATUS'] = df1['STATUS'].astype('category')
print(df1['STATUS'].describe().T)

# box plot for available bikes
color = {'boxes': 'DarkGreen', 'whiskers': 'DarkOrange', 'medians': 'DarkBlue', 'caps': 'Gray'}

df1['AVAILABLE BIKES'].plot(color=color, kind='box', figsize=(20, 20), title='Available bike usage', legend=True, grid=True)
plt.show()

# Histograms for all continuous features within the dataset
hist_df = df1.copy()
hist_df = hist_df[['TIME', 'AVAILABLE BIKES', 'AVAILABLE BIKE STANDS', 'hour', 'day_of_month']]
numeric_columns = hist_df.select_dtypes(['int64']).columns
# numeric_columns = numeric_columns.drop('STATION ID')
# Histograms for all continuous features within the dataset
hist_df[numeric_columns].hist(figsize=(20, 20))
hist_df['day_of_month'].value_counts().sort_index().plot()
# extract features from 2nd Feb to 1st Apr for station - Merrion Square/Grangegorman
ts = pd.to_datetime('2020/01/01')
te = pd.to_datetime('2020/04/01')
mask = (df1['TIME'] >= ts) & (df1['TIME'] <= te)
pd.options.mode.chained_assignment = None
# df['TIME'].max() - df['TIME'].min()
suburb_point = "Merrion Square South"
suburb_df = df1.loc[mask]
suburb_df = suburb_df[suburb_df['ADDRESS'] == suburb_point]
suburb_dataset = suburb_df[['TIME', 'AVAILABLE BIKES']]
numeric_columns = suburb_df.select_dtypes(['int64']).columns
print(suburb_df[numeric_columns].describe().T)

suburb_dataset['date'] = suburb_dataset['TIME'].dt.floor('T')
suburb_dataset = suburb_dataset.reset_index()
suburb_dataset.drop('TIME', axis=1, inplace=True)
time = suburb_dataset['date']
bikes = suburb_dataset['AVAILABLE BIKES']
# plot 2 week data for Merrion Square South region
fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_minor_locator(mdates.DayLocator(interval=1))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax.scatter(time, bikes, color="red", marker=".", label='bike count')
ax.set_xlabel("date")
ax.set_ylabel("bike available")
fig.autofmt_xdate()
ax.grid(True)
ax.set_title(f'Bike usage for {suburb_point}')
ax.set_xlim([datetime.date(2020, 1, 1), datetime.date(2020, 4, 2)])
plt.show()


from __future__ import print_function, absolute_import, division
from ..utils import check_equilibrium
import numpy as np
from scipy.linalg import inv, pinv, norm
from scipy.optimize import basinhopping, fmin
from copy import deepcopy

class PCCAPlus(object):
    def __init__(self, n_states=2, pi=None, psi=None,
                 ncv=None, optimize=True,
                 objective='crisp_metastability'):
        '''
        Parameters:
        -----------
        n_states: int
            Number of states to coarse-grain the model.
        objective_function: str
            Objective functions to minimize the PCCA+ solution. Can be one of
            the following: crispness, crisp_metastability, fuzzy_metastability.
            Additionally, a function can be passed directly.
        '''
        if n_states < 2:
            raise ValueError('''Coarse-graining with PCCA+ requires at least
                                two states for valid decomposition.''')
        self.n_states = n_states
        self.objective = objective
        self.optimize = optimize
        self.ncv = ncv
        self.psi = psi
        self.pi = pi

    def fit(self, model):
        if self.n_states > model._T.shape[0] - 1:
            raise ValueError('''Number of states to coarse-grain must be one
                                less than states in initial model.''')

        # Solve or check left (pi) and right (psi) eigenvectors
        if self.psi is None:
            if self.pi is None:
                mu, psi = model.eigenvectors('both', k=self.n_states,
                                             ncv=self.ncv)
                self.pi = mu[:, 0]
                self.psi = psi
            else:
                self.pi = check_equilibrium(self.pi, model._T.shape[0])
                self.psi = model.eigenvectors('right', k=self.n_states,
                                              ncv=self.ncv)
        else:
            assert self.psi.shape[0] == model._T.shape[0], 'Invalid right eigenvectors'
            assert self.psi.shape[1] == self.n_states, 'Invalid number of vectors'
            if self.pi is None:
                self.pi = model.equilibrium
            else:
                self.pi = check_equilibrium(self.pi, model._T.shape[0])

        # Get transition matrix to coarsen
        self.T = model.transition_matrix

        # Guess A-matrix
        index = _index_search(psi)
        A = self.psi[index, :]
        try:
            A = inv(A)
        except:
            A = pinv(A)
        A = _fill_A(A, self.psi)

        # Optimize A Matrix
        if self.optimize:
            A = self._optimize_A(A)
        self.A = _fill_A(A, self.psi)
        return self

    def transform(self, model):
        # Build new coarse-grained model
        new_model = deepcopy(model)
        new_model.n_states = self.n_states
        new_model.fuzzy_membership = self.chi = np.dot(self.psi, self.A)
        new_model.crisp_membership = np.argmax(self.chi, axis=1)

        # Estimate coarse-grained transition matrix
        w = inv(np.dot(self.chi.T, self.chi))
        a = np.dot(np.dot(self.chi.T, model._T), self.chi)
        T_coarse = np.dot(w, a)

        pi_coarse = np.dot(self.chi.T, self.pi)
        X_coarse = np.dot(np.diag(pi_coarse), T_coarse)
        T_coarse = X_coarse / X_coarse.sum(axis=1)[:, None]
        new_model._T = T_coarse
        return new_model

    def fit_transform(self, model):
        self.fit(model)
        return self.transform(model)

    def _optimize_A(self, A):
        def f(alpha):
            if self.objective == 'crispness':
                return -1 * _crispness(alpha, self.T, self.pi, self.psi)
            elif self.objective == 'roeblitz':
                return -1 * _roeblitz(alpha, self.T, self.pi, self.psi)
            elif self.objective == 'crisp_metastability':
                return -1 * _crisp_metastability(alpha, self.T, self.pi, self.psi)
            elif self.objective == 'fuzzy_metastability':
                return -1 * _fuzzy_metastability(alpha, self.T, self.pi, self.psi)

        shape = A.shape
        alpha = A.ravel()
        alpha = basinhopping(f, alpha, niter_success=1000)['x']
        alpha = fmin(f, alpha,
                     xtol=1e-4, ftol=1e-4,
                     maxfun=1e4, maxiter=1e4,
                     disp=False, full_output=False)

        if np.isneginf(f(alpha)):
            raise ValueError('Minimization has failed to find good solution')
        return alpha.reshape(shape)

def _index_search(psi):
    n_micro, n_macro = psi.shape
    index = np.zeros(n_macro, dtype=int)

    # Find the outermost point in eigenvector space
    index[0] = np.argmax([norm(psi[mi]) for mi in range(n_micro)])

    # Change orthogonal reference frame with outermost point the origin
    ortho = psi - psi[index[0], np.newaxis]

    # Gram-Schmidt orthogonalization
    for ma in range(1, n_macro):
        temp = ortho[index[ma - 1]].copy()
        for mi in range(n_micro):
            ortho[mi] -= temp * np.dot(ortho[mi], temp)
        dist = np.array([norm(ortho[mi]) for mi in range(n_micro)])
        index[ma] = np.argmax(dist)
        ortho /= dist.max()
    return index

def _fill_A(A, psi):
    n_micro, n_macro = psi.shape
    A = A.copy()

    A[1:, 0] = -1 * A[1:, 1:].sum(1)
    A[0] = -1 * np.dot(psi[:, 1:].real, A[1:]).min(0)
    return A / A[0].sum()

def _crispness(alpha, T, pi, psi):
    '''
    Parameters:
    -----------
    A: numpy.ndarray
        The assignment (or transformation) matrix which is the output of the
        constrained optimization problem.
    psi: numpy.ndarray (n_micro, n_macro)
        Right eigenvectors of the transition matrix. This should contain the
        second through n_macro eigenvectors. The first eigenvector should be
        excluded as these are normalized to one.
    Returns:
    --------
    objective: float
        The objective function
    References:
    -----------
    ..  Kube S.,  "Statistical Error Estimation and Grid-free
    Hierarchical Refinement in Conformation Dynamics," Doctoral Thesis.
    2008
    '''
    n_micro, n_macro = psi.shape
    A = alpha.reshape(n_macro, n_macro)
    A = _fill_A(A, psi)

    lhs = 1 - A[0, 1:].sum()
    rhs = np.dot(psi[:, 1:], A[1:, 0])
    rhs = -rhs.min()

    if abs(lhs - rhs) > 1e-8:
        return -np.inf
    score = np.trace(np.dot(np.diag(1 / A[0]), np.dot(A.T, A)))
    return score

def _crisp_metastability(alpha, T, pi, psi):
    n_micro, n_macro = psi.shape
    A = alpha.reshape(n_macro, n_macro)
    A = _fill_A(A, psi)

    fuzzy_membership = np.dot(psi, A)
    crisp_membership = np.argmax(fuzzy_membership, axis=1)
    chi = 0.0 * fuzzy_membership
    chi[np.arange(n_micro), crisp_membership] = 1

    lhs = 1 - A[0, 1:].sum()
    rhs = np.dot(psi[:, 1:], A[1:, 0])
    rhs = -rhs.min()
    if abs(lhs - rhs) > 1e-8:
        return -np.inf

    score = 0.0
    for ma in range(n_macro):
        score += np.dot(T.dot(chi[:, ma]),
                        pi * chi[:, ma]) / np.dot(chi[:, ma], pi)
    return score

def _fuzzy_metastability(alpha, T, pi, psi):
    n_micro, n_macro = psi.shape
    A = alpha.reshape(n_macro, n_macro)
    A = _fill_A(A, psi)
    chi = np.dot(psi, A)

    lhs = 1 - A[0, 1:].sum()
    rhs = np.dot(psi[:, 1:], A[1:, 0])
    rhs = -rhs.min()
    if abs(lhs - rhs) > 1e-8:
        return -np.inf

    score = 0.0
    for ma in range(n_macro):
        score += np.dot(T.dot(chi[:, ma]),
                        pi * chi[:, ma]) / np.dot(chi[:, ma], pi)
    return score

def _roeblitz(alpha, T, pi, psi):
    n_micro, n_macro = psi.shape
    A = alpha.reshape(n_macro, n_macro)
    A = _fill_A(A, psi)

    score = 0
    for i in range(n_macro):
        for j in range(n_macro):
            score += A[j, i] ** 2 / A[0, i]
    return score


# -*- coding: utf-8 -*-
"""
URLs for entitlement requests app
"""

from django.conf.urls import url

from .views import IndexView, RejectEntitlementRequest, AcceptEntitlementRequest

urlpatterns = [
    url(r'^$',
        IndexView.as_view(),
        name='entitlementrequests-index'),
    url(r'^entitlement-requests/entitlement_request_id/(?P<entitlement_request_id>[\w\@\.\+-]+)$',
        RejectEntitlementRequest.as_view(),
        name='entitlement-request-delete'),
    url(r'^entitlement-requests/user_id/(?P<user_id>[\w\@\.\+-]+)$',
        AcceptEntitlementRequest.as_view(),
        name='entitlement-request-accept'),
]


import pandas as pd
import re

def clean_numeric_data(row):
    # Define a regular expression pattern to match numerical values
    num_pattern = r"[-+]?\d*\.?\d+"

    # Loop through all columns (except "case_id")
    for column in row.index:
        if (column != "case_id") and (column != 'chief'):
            cell_value = str(row[column])
            numerical_value = re.findall(num_pattern, cell_value)
            if numerical_value:
                row[column] = pd.to_numeric(numerical_value[0])
            else:
                row[column] = pd.NA

    return row

def replace_case_ids(gpt_output_file, matchup_file):
    # Read the CSV files
    gpt_output_data = pd.read_csv(gpt_output_file)
    matchup_data = pd.read_csv(matchup_file)

    # Remove duplicates from the matchup_data based on "CAP ID"
    matchup_data.drop_duplicates(subset="CAP ID", keep="first", inplace=True)

    # Create a dictionary mapping CAP ID to SCDB ID
    cap_id_to_scdb_id = dict(zip(matchup_data["CAP ID"], matchup_data["SCDB ID"]))

    # Update the "case_id" in GPT_output_data using the mapping
    gpt_output_data["caseId"] = gpt_output_data["caseId"].map(cap_id_to_scdb_id)

    # Clean numerical data row by row (keeping only numerical data)
    gpt_output_data = gpt_output_data.apply(clean_numeric_data, axis=1)

    # Save the updated data to a new CSV file (you can change the file name as desired)
    updated_output_file = "GPT_output_updated.csv"
    gpt_output_data.to_csv(updated_output_file, index=False)

    print(f"Updated data saved to '{updated_output_file}'.")

# Replace the file names with your actual file paths
gpt_output_file_path = "GPT_output.csv"
matchup_file_path = "matchup.csv"

replace_case_ids(gpt_output_file_path, matchup_file_path)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates
import csv
import collections

def del_someword(date_str):
    return date_str[:-2]

FILE_NAME = "20200506_233732.csv"
CSV_FILE = open("./" + FILE_NAME, "r", encoding="ms932", errors="", newline="" )

f = csv.reader(CSV_FILE, delimiter=",", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)

datas = map(del_someword, next(f))

each_time_counts = collections.Counter(datas)

print(each_time_counts)
print("> tyr to sort the list...")

time_list = []
count_list = []

for time in sorted(each_time_counts.keys()):
    time_list.append(datetime.datetime.strptime(time, "%Y%m%d_%H:%M:%S,%f"))
    count_list.append(each_time_counts[time])

for x in range(1, len(count_list)):
    count_list[x] += count_list[x -1]

plt.plot(time_list, count_list)
plt.show()


import numpy as np
import pandas as pd
import os
import time
import math

'''
the set MovieLens 100K
set URL: https://grouplens.org/datasets/movielens/100k/
'''
'''
    R:User-item corresponding co-occurrence matrix m*n
    P:user-factor matrix m*k
    Q:Item factor matrix k*n
    K:dimension of the hidden vector
    steps:maximum number of iterations
    alpha:learning rate
    Lambda:weighting factor for L2 regularization
'''

# Decompose the matrix R into P,Q
def matrix_factorization(R, P, Q, K, steps, alpha=0.05, Lambda=0.002):
    # total time
    sum_st = 0
    # Size of previous loss
    e_old = 0
    # End-of-program marker
    flag = 1
    # Gradient descent end condition 1: maximum number of iterations satisfied
    for step in range(steps):
        # Time of the start of each fall generation
        st = time.time()
        cnt = 0
        e_new = 0
        for u in range(1, len(R)):
            for i in range(1, len(R[u])):
                if R[u][i] > 0:
                    eui = R[u][i] - np.dot(P[u, :], Q[:, i])
                    for k in range(K):
                        temp = P[u][k]
                        P[u][k] = P[u][k] + alpha * eui * Q[k][i] - Lambda * P[u][k]
                        Q[k][i] = Q[k][i] + alpha * eui * temp - Lambda * Q[k][i]
        for u in range(1, len(R)):
            for i in range(1, len(R[u])):
                if R[u][i] > 0:
                    cnt += 1
                    e_new = e_new + pow(R[u][i] - np.dot(P[u, :], Q[:, i]), 2)
        e_new = e_new / cnt
        et = time.time()
        sum_st = sum_st + (et - st)
        # Difference in loss before and after the first iteration is not executed
        if step == 0:
            e_old = e_new
            continue
        # Gradient descent end condition 2: loss too small, jump out
        if e_new < 1e-3:
            flag = 2
            break
        # Gradient descent end condition 3:
        # The difference between the front and rear loses is too small, jump out
        if (e_old - e_new) < 1e-10:
            flag = 3
            break
        else:
            e_old = e_new
    print(f'--------Summary---------\nThe type of jump out:{flag}\nTotal steps:{step+1}\nTotal time:{sum_st}\n'
          f'Average time:{sum_st / (step+1)}\nThe e is :{e_new}')
    return P, Q

# view
def view_data():
    rtnames = ['user', 'item', 'score', 'time']
    data = pd.read_csv("D:\\YSA\\MovieLens\\ml-100k\\ML100K.txt", sep='\t', header=None, names=rtnames)
    # print(data.head())
    # Number of users
    u_cnt = len(np.unique(data[rtnames[0]]))
    # item number
    i_cnt = len(np.unique(data['item']))
    # record number
    r_cnt = len(data)
    # Minimum number of items to be accessed by a user
    u_cnt_min = min(data['user'].value_counts().values)
    # Average number of items visited by users
    u_cnt_avg = r_cnt / u_cnt
    # Minimum number of visits to the project
    i_cnt_min = min(data['item'].value_counts().values)
    # Average number of visits to the project
    i_cnt_avg = r_cnt / i_cnt

# Split data integration training set, test set
def split_data(dformat):
    # Read raw data
    rating = pd.read_csv("D:\\YSA\\MovieLens\\ml-100k\\ML100K.txt", sep='\t', header=None, names=dformat)
    # Sort by chronological order
    rating.sort_values(by=['time'], axis=0, inplace=True)
    # Determine the boundary line according to the chronological value 8:2
    boundary = rating['time'].quantile(0.8)
    # Slice and dice data by time cut-off point to generate training set
    train = rating[rating['time'] < boundary]
    # Training set sorted by user, chronological order
    train.sort_values(by=['user', 'time'], axis=0, inplace=True)
    # Slice and dice data by time cut-off point to generate test sets
    test = rating[rating['time'] >= boundary]
    # Test sets sorted by user, chronological order
    test.sort_values(by=['usr', 'time'], axis=0, inplace=True)
    data = pd.concat([train, test])
    # Check if the catalogue exists
    if os.path.exists("D:\\YSA\\MovieLens\\ml-100k"):
        pass
    else:
        os.mkdir("D:\\YSA\\MovieLens\\ml-100k")
    # Write the training set and test set to a file
    train.to_csv("D:\\YSA\\MovieLens\\ml-100k\\ML100K_Train.txt", sep=',', index=False, header=None)
    test.to_csv("D:\\YSA\\MovieLens\\ml-100k\\ML100K_test.txt", sep=',', index=False, header=None)
    print(f'split data complete!')

# Get local data
def getData(path, dformat):
    # Read user-coincidence matrix data
    rating = np.loadtxt(path+"\\Basic_MF\\rating.txt", delimiter=',', dtype=float)
    # Retrieve training set data
    trainData = pd.read_csv(path+"\\ML100K_Train.txt", sep=',', header=None, names=dformat)
    # Read test set data
    testData = pd.read_csv(path+"\\ML100K_test.txt", sep=',', header=None, names=dformat)
    data = pd.concat([trainData, testData])
    # Total number of users
    all_user = np.unique(data['user'])
    # Total number of items
    all_item = np.unique(data['item'])
    return rating, trainData, testData, all_user, all_item

# Generate a user-item matrix and save it to a local file
def getUserItem(path, train, all_user, all_item):
    train.sort_values(by=['user, item'], axis=0, inplace=True)
    # Number of rows of the user-project co-occurrence matrix
    num_user = len(all_user)+1
    # Number of columns of the user-item co-occurrence matrix
    num_item = len(all_item)+1
    # Initialisation of the user-project co-occurrence matrix
    rating_mat = np.zeros([num_user, num_item], dtype=int)
    # User-project co-occurrence matrix assignment
    for i in range(len(train)):
        user = train.iloc[i]['user']
        item = train.iloc[i]['item']
        score = train.iloc[i]['score']
        rating_mat[user][item] = score
    # Determine if a folder exists
    if os.path.exists(path+"\\BasicMF"):
        pass
    else:
        os.mkdir(path+"\\BasicMF")
    # Save user-project co-occurrence matrix to file
    np.savetxt(path+"\\BasicMF\\rating.txt", rating_mat, fmt='%d', delimiter=',', newline='\n')
    print(f'generate rating matrix complete!')

# train
def train(path, rating, K, steps, matrix_factorization_BiasSVD=None):
    R = rating
    M = len(R)
    N = len(R[0])
    # User matrix initialisation
    P = np.random.normal(loc=0, scale=0.01, size=(M, K))
    # Project matrix initialisation
    Q = np.random.normal(loc=0, scale=0.01, size=(K, N))
    P, Q = matrix_factorization_BiasSVD(R, P, Q, K, steps)
    # Determine if a folder exists
    if os.path.exists(path + "\\Basic_MF"):
        pass
    else:
        os.mkdir(path + "\\Basic_MF")
    # Save P, Q to file
    np.savetxt(path+"\\Basic_MF\\userMatrix.txt", P, fmt="%.6f", delimiter=',', newline='\n')
    np.savetxt(path+"\\Basic_MF\\itemMatrix.txt", Q, fmt="%.6f", delimiter=',', newline='\n')
    print("train complete!")

# Generate topk recommendation list
def topK(dic, k):
    keys = []
    values = []
    for i in range(k):
        key, value = max(dic.items(), key=lambda x: x[1])
        keys.append(key)
        values.append(value)
        dic.pop(key)
    return keys, values

# test
def test(path, trainData, testData, all_item, k):
    # Read the user matrix
    P = np.loadtxt(path+"\\Basic_MF\\userMatrix.txt", delimiter=',', dtype=float)
    # Read the project matrix
    Q = np.loadtxt(path+"\\Basic_MF\\itemMatrix.txt", delimiter=',', dtype=float)
    # Collection of users in the test set
    testUser = np.unique(testData['user'])
    # Length of test set
    test_lenght = len(testData)

    Hits = 0
    MRR = 0
    NDCG= 0
    # Start time
    st = time.time()
    for user_i in testUser:
        # Items that have been accessed by the ith user in the test set in the training set
        visited_list = list(trainData[trainData['user'] == user_i]['item'])
        # No training data, skipped
        if len(visited_list) == 0:
            continue
        # Test set of items accessed by the ith user and de-duplicated
        test_list = list(testData[testData['user'] == user_i]['item'].drop_duplicates())
        # The items accessed by the ith user in the test set are removed from the items
        # already accessed by that user in the training set
        test_list = list(set(test_list) - set(test_list).intersection(set(visited_list)))
        # Test set ith user's access item is empty, skipped
        if len(test_list) == 0:
            continue
        # Generate a test set of items not visited by the ith user:rating pair
        poss = {}
        for item in all_item:
            if item in visited_list:
                continue
            else:
                poss[item] = np.dot(P[user_i, :], Q[:, item])
        # Recommended list of the num i user of the test set
        ranked_list, test_score = topK(poss, k)
        # List of items accessed by the i-th user of the hit test set
        h = list(set(test_list).intersection(set(ranked_list)))
        Hits += len(h)
        for item in test_list:
            for i in range(len(ranked_list)):
                if item == ranked_list[i]:
                    MRR += 1 / (i+1)
                    NDCG += 1 / (math.log2(i+1+1))
                else:
                    continue
    HR = Hits / test_lenght
    MRR /= test_lenght
    NDCG /= test_lenght
    # End Time
    et = time.time()
    print("HR@10:%.4f\nMRR@10:%.4f\nNDCG@10:%.4f\nTotal time:%.4f" % (HR, MRR, NDCG, et-st))

if __name__ == '__main__':
    rtnames = ['user', 'item', 'score', 'time']
    path = "C:\\Desktop\\test01 of project\\ml-100k"
    rating, trainData, testData, all_user, all_item = getData(path, rtnames)
    # train(path, rating, 30, 10)
    test(path, trainData, testData, all_item, 10)


import os
import json
import requests

DUCKLING_URL = os.getenv('DUCKLING_PARSE_URL', 'http://localhost:8000/parse')

def parse(event, context):
    inbound_text = event['queryStringParameters']['date_string']
    print('Text to parse: %s' % inbound_text)

    r = requests.post(DUCKLING_URL, data = { 'locale': 'en_ZA', 'text': inbound_text })
    print('Raw response from Duckling: %s' % r.content)
    parse_response = json.loads(r.text)
    print("Executed response, result: %s" % parse_response)

    if check_parse_succeeded(parse_response):
        primary_value = parse_response[0]['value']['value']
        print("And extracted: %s" % primary_value)
        desired_length = len("2018-11-11T11:11")
        return_string = primary_value[0:desired_length]
    else:
        print("Could not parse text")
        return_string = "ERROR_PARSING"

    response = {
        "statusCode": 200,
        "headers": {
            "content-type": "text/plain"
        },
        "body": return_string
    }

    return response

def check_parse_succeeded(parse_response):
    if not parse_response:
        return False
    elif not isinstance(parse_response, list):
        return False
    elif len(parse_response) == 0:
        return False
    elif 'value' not in parse_response[0]:
        return False
    elif 'value' not in parse_response[0]['value']:
        return False
    else:
        return True


import os

command = input()

while not command == "End":

    data = command.split("-")

    action = data[0]
    file_name = data[1]

    if action == "Create":

        with open(file_name, "w") as file:
            file.write("")

    elif action == "Add":

        content = data[2]

        with open(file_name, "a") as file:
            file.write(f"{content}\n")

    elif action == "Replace":

        old_string = data[2]
        new_string = data[3]

        if os.path.exists(file_name):

            with open(file_name, "r+") as file:

                file_content = file.read().replace(old_string, new_string)
                file.seek(0)
                file.truncate()
                file.write(file_content)

        else:
            print("An error occurred")

    elif action == "Delete":

        if os.path.exists(file_name):
            os.remove(file_name)

        else:
            print("An error occurred")

    command = input()

Python Advanced 05.2022/File Handling/file_manipulator.py


from datetime import datetime
import sys
import os
# from urllib.parse import urlparse, unquote
import re
import mimetypes
import random
import string

def genRandStr(count: int=10, chars=string.ascii_uppercase + string.digits):
    """Generate a random string."""
    return ''.join(random.choice(chars) for _ in range(count))

def time2Str(timestamp):
    """Transform a Zeitgeist timestamp into a human-readable string."""
    (timestamp, remainder) = divmod(timestamp, 1000)
    string = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
    string += (".%d" % remainder)
    return string

# def urlToUnixPath(url):
#     """Convert a file:// URL to a Unix path string."""
#     p = urlparse(url)
#     return unquote(os.path.abspath(os.path.join(p.netloc, p.path)))

# def uq(s):
#     """Unquote a string."""
#     return unquote(s)

def hasIntersection(s1, s2):
    """Return True if two sets share at least one item, False otherwise."""
    for i in s1:
        for j in s2:
            if i == j:
                return True

    return False

def intersection(l1, l2):
    """Return the intersection of two lists. Naive implementation."""
    ret = []
    for i in l1:
        if i in l2:
            ret.append(i)

    return ret

# Get the last line of a file
BLOCKSIZE = 4096
def tail(f):
    # Save old position.
    oldPos = f.tell()

    # Seek to end of file to get file length.
    f.seek(0, 2)
    bytesInFile = f.tell()

    # Parse file to get tail.
    linesFound = 0
    totalBytesScanned = 0

    # Find any position (efficiently-ish) such that it's before 2+ lines.
    while (linesFound < 2 and bytesInFile > totalBytesScanned):
        byteBlock = min(BLOCKSIZE, bytesInFile - totalBytesScanned)
        f.seek( -(byteBlock + totalBytesScanned), 2)
        totalBytesScanned += byteBlock

        buff = f.read(BLOCKSIZE)
        try:
            countableBuff = buff.decode('utf-8')
        except(UnicodeDecodeError) as e:
            return None
        else:
            linesFound += countableBuff.count('\n')

    # Seek to that position where there are 2+ lines, and then read lines.
    f.seek(-totalBytesScanned, 2)
    lineList = list(f.readlines())

    # Reset position;
    f.seek(oldPos, 0)

    # Return the last line.
    try:
        readableLine = lineList[-1].decode('utf-8')
    except(UnicodeDecodeError) as e:
        return None
    else:
        return readableLine

# Copied from StackExchange, timed prints.
import atexit
from time import time
from datetime import timedelta

__timed_print_start = time()

def __secondsToStr(t):
    global __timed_print_start
    return str(timedelta(seconds=t - __timed_print_start))

def tprnt(msg: str, file=sys.stdout):
    stripped = msg.lstrip("\n")
    leadingCount = len(msg) - len(stripped)
    print("%s%s: %s" % ("\n" * leadingCount, __secondsToStr(time()), stripped),
          file=file)

def __finalTimedPrint():
    tprnt("Exiting.")

def registerTimePrint():
    atexit.register(__finalTimedPrint)


import numpy as np
import pandas as pd
import logging
from typing import List, Dict
from chinese_calendar.constants import holidays, workdays
from chinese_calendar.utils import is_workday, is_holiday
from src.feature_base import FeaturizerBase, WinOffsetFeaturizerBase
# from ..util.decorators import log_status

__all__ = ['DateTimeFeaturizer', 'DifferenceFeaturizer', 'RollingStatsFeaturizer']

class DateTimeFeaturizer(FeaturizerBase):
    DEFAULT_FEATURES = [
        'year',
        'index_15min',
        'day_of_year',
        'is_leap_year',
        'day_of_year_sin',
        'day_of_year_cos',
        'quarter',
        'month',
        'week',
        'day',
        'day_of_month_sin',
        'day_of_month_cos',
        'day_of_week',
        'is_weekend',
        'day_of_week_sin',
        'day_of_week_cos',
        'hour',
        'minute',
        'second',
        'second_of_day',
        'second_of_day_sin',
        'second_of_day_cos',
        'is_workday',
        'is_holiday',
        'is_special_workday',
        'is_special_holiday'
    ]

    dt_format = '%Y-%m-%d %H:%M:%S'

    def __init__(self, *, feature_col: List[str] = None, dt_col: str = None):
        """
        featurizer for making Datetime features, see class attribute DEFAULT_FEATURES for more information.

        Args:
            feature_col: the list of datetime features interested, must in DEFAULT_FEATURES,
                         default using all DEFAULT_FEATURES
            dt_col: datetime column, default using the index of the input dataframe
        """
        super().__init__()

        if feature_col is None:
            self.feature_cols = self.DEFAULT_FEATURES
        else:
            self.feature_cols = list(feature_col)

        self.dt_col = dt_col

#     @log_status()
    def transform(self, df: pd.DataFrame):
        """
        Transform input df to df with specified datetime features.

        Caution:
        1. The input df should have index that is a pandas datetime index, if not, should specify dt_col.
        2. The time gap between two samples should be 15mins.
        3. The output df will have a datetime index using original df.index or dt_col

        Args:
            df: input df

        Returns: df with Datetime features

        """
        if self.dt_col is None:
            dt_col = df.index
        else:
            dt_col = df[self.dt_col]

        dt_col = pd.to_datetime(dt_col, format=self.dt_format)
        dt_col = dt_col.to_series()
        out_df = pd.DataFrame(index=dt_col)

        year = dt_col.dt.year
        doy = dt_col.dt.dayofyear
        leap = np.asarray(dt_col.dt.is_leap_year, dtype=int)
        diy = 365 + leap
        day = dt_col.dt.day
        dim = dt_col.dt.days_in_month
        dow = dt_col.dt.dayofweek
        doy_angular = 2 * np.pi * doy / diy
        day_angular = 2 * np.pi * day / dim
        dow_angular = 2 * np.pi * dow / 7

        out_df['year'] = year - year.min()
        out_df['day_of_year'] = doy
        out_df['is_leap_year'] = leap
        out_df['day_of_year_sin'] = np.sin(doy_angular)
        out_df['day_of_year_cos'] = np.cos(doy_angular)

        out_df['quarter'] = dt_col.dt.quarter
        out_df['month'] = dt_col.dt.month
        out_df['week'] = dt_col.dt.isocalendar().week

        out_df['day'] = day
        out_df['day_of_month_sin'] = np.sin(day_angular)
        out_df['day_of_month_cos'] = np.cos(day_angular)

        out_df['day_of_week'] = dow
        out_df['is_weekend'] = np.asarray(dow >= 5, dtype=int)
        out_df['day_of_week_sin'] = np.sin(dow_angular)
        out_df['day_of_week_cos'] = np.cos(dow_angular)

        out_df['hour'] = dt_col.dt.hour
        out_df['minute'] = dt_col.dt.minute
        out_df['index_15min'] = out_df['hour'] * 4 + out_df['minute'] // 15
        out_df['second'] = dt_col.dt.second
        out_df['second_of_day'] = out_df['hour'] * 3600 + out_df['minute'] * 60 + out_df['second']
        sod_angular = 2 * np.pi * out_df['second_of_day'] / 86400
        out_df['second_of_day_sin'] = np.sin(sod_angular)
        out_df['second_of_day_cos'] = np.cos(sod_angular)

        out_df['is_workday'] = dt_col.dt.date.map(is_workday)
        out_df['is_holiday'] = dt_col.dt.date.map(is_holiday)
        out_df['is_special_workday'] = dt_col.dt.date.map(lambda d: d in workdays)
        out_df['is_special_holiday'] = dt_col.dt.date.map(lambda d: d in holidays)
        out_df['is_special_workday'] = out_df['is_special_workday'].map(lambda x: 1 if x else 0)
        out_df['is_special_holiday'] = out_df['is_special_holiday'].map(lambda x: 1 if x else 0)

        return pd.concat([df, out_df[self.feature_cols]], axis=1)

class DifferenceFeaturizer(WinOffsetFeaturizerBase):

    def __init__(self, *, offsets, feature_col, freq=None):
        """
        Building difference features.

        Args:
            offsets: list of lags, each element in the list corresponds to one lag.
            feature_col: list of input raw features. These features will be used to calculate stats and lag features.
            freq: The shift frequency.
        """
        super().__init__(offsets=offsets, feature_col=feature_col)

        self.freq = freq

#     @log_status()
    def transform(self, df: pd.DataFrame) -> pd.DataFrame:
        """
        Transform df to have difference features

        Args:
            df: input df

        Returns: output df with difference features.

        """
        feature_df = df[self.feature_col]
        df_lst = [df]

        for offset in self.offsets:
            diff_df = feature_df - feature_df.shift(offset, self.freq)
            diff_df.columns = [
                '{}_diff_offset_{}'.format(col, offset)
                for col in feature_df.columns.tolist()
            ]
            df_lst.append(diff_df)

        return pd.concat(df_lst, axis=1)

class RollingStatsFeaturizer(WinOffsetFeaturizerBase):
    DEFAULT_STATS = ['min', 'max', 'median', 'mean', 'std', 'skew']
    DEFAULT_ROLLING_KWARGS = {'min_periods': 1, 'center': False}

    def __init__(self,
                 *,
                 wins,
                 offsets,
                 feature_col,
                 is_interval: bool = False,
                 interval_key: str = 'index_15min',
                 freq: int = None,
                 stats: List[str] = None,
                 quantiles: List[float] = None,
                 rolling_kwargs: Dict = None):
        """
        Generate Rolling stats features, this is a class designed for three purpose:
        1. Generate Rolling window stats.
            1. subseries rolling window stats.
            2. ordinal rolling window stats.
        2. Generate Lag features.

        To generate pure Lag features, include window size 1 in wins, then this will generate pure lag according
        to offsets.

        To generate ordinal rolling window stats, set window size > 1, skew features will only be built when
        window size >= 3. The resulting ordinal rolling window stats will shift according to offsets. For example,
        for window size = 3 and the sample with timestamp 2020-01-02 08:30:00, the window will contain samples:

        ["2020-01-02 08:00:00", "2020-01-02 08:15:00", "2020-01-02 08:30:00"]

        To generate subseries rolling window stats, set is_interval=True, set window size > 1, skew features will
        only be built when window size >= 3. The data will first be grouped by using interval_key. The resulting ordinal
        rolling window stats will shift according to offsets. For example, for window size = 3 and the sample with
        timestamp 2020-01-05 08:30:00, the window will contain samples:

        ["2020-01-03 08:30:00", "2020-01-04 08:30:00", "2020-01-05 08:30:00"]

        Caution:
        if is_interval=True:
        1. Ensure that index is pd datetime index.
        2. Ensure that index gap is 15 mins.
        3. Ensure that index has no missing values or duplicate values.
        4. Ensure that the columns interested have no missing values.

        Args:
            wins: list of window sizes, each element in the list corresponds to one window size.
            offsets: list of lags, each element in the list corresponds to one lag.
            feature_col: list of input raw features. These features will be used to calculate stats and lag features.
            is_interval: If True, subseries rolling window stats else ordinal rolling stats features.
            interval_key: The interval key used to groupby df. used only if is_interval=True.
            freq: The shift frequency.
            stats: The stats features, if None, DEFAULT_STATS are used. skew is used only when window size >= 3, if
                   window size = 1, only mean will be used.
            quantiles: list of quantiles to build quantile features.
            rolling_kwargs: extra kwargs pass to df.rolling function.
        """
        super().__init__(wins=wins, offsets=offsets, feature_col=feature_col)

        self.freq = freq
        self.stats = stats
        self.offsets = offsets
        self.is_interval = is_interval
        self.interval_key = interval_key
        self.quantiles = quantiles
        self.rolling_kwargs = rolling_kwargs
        self.wins = wins

#     @log_status()
    def transform(self, df: pd.DataFrame) -> pd.DataFrame:
        """
        Transform df to have rolling stats features

        Args:
            df: input df

        Returns: output df with rolling stats features.

        """
        drop_interval_key = False

        if self.is_interval:
            if self.interval_key not in list(df.columns):
                logging.info('interval key {} not in df columns, '
                             'using default index_15min as interval key...'.format(self.interval_key))

                self.interval_key = 'index_15min'
                index_15min = df.index.hour * 4 + df.index.minute // 15
                df = df.assign(index_15min=index_15min)
                drop_interval_key = True  # drop index_15min after transformation

            if self.interval_key not in self.feature_col:
                self.feature_col.append(self.interval_key)
            else:
                logging.warning('interval_key {} in feature_col, '
                                'this feature will not be used to'
                                ' calculate the interval features ...'.format(self.interval_key))

        feature_df = df[self.feature_col]
        df_lst = [df]

        for win in self.wins:
            if win <= 1:
                logging.warning('window size <= 1, this will build lags only ...')
                stats = ['mean']
                win = 1
            elif win < 3 and 'skew' in self.stats:
                logging.warning('window size {} < 3, '
                                'skew requires at least 3 samples, '
                                'it will not be calculated !'.format(win))
                stats = self.stats[:]
                stats.remove('skew')
            else:
                stats = self.stats

            for offset in self.offsets:
                if self.is_interval:
                    curr_df = self._build_interval_stats(feature_df, win, offset, stats)
                else:
                    curr_df = self._build_rolling_stats(feature_df, win, offset, stats)

                df_lst.append(curr_df)

        out_df = pd.concat(df_lst, axis=1)
        if drop_interval_key:
            out_df.drop(columns=self.interval_key, inplace=True)

        return out_df

    def _build_interval_stats(self, df, win, offset, stats):
        df_lst = []
        df_groupby = df.groupby(self.interval_key)
        for col in df.columns.tolist():
            if col != self.interval_key:
                agg_dict = {
                    f'{self.interval_key}_{col}_win_{win}_offset_{offset}_{stat}': stat
                    for stat in stats
                }
                stats_df = df_groupby[col].apply(lambda x: x.rolling(win, **self.rolling_kwargs).agg(agg_dict))
                df_lst.append(stats_df)

                if self.quantiles is not None:
                    for qtl in self.quantiles:
                        pct_df = df_groupby[col].apply(lambda x: x.rolling(win, **self.rolling_kwargs).quantile(qtl))
                        pct_df.name = f'{self.interval_key}_{col}_win_{win}_offset_{offset}_q{qtl}'
                        df_lst.append(pct_df)

        return pd.concat(df_lst, axis=1).shift(offset, self.freq)

    def _build_rolling_stats(self, df, win, offset, stats):
        df_lst = []
        for col in df.columns.tolist():
            if col != self.interval_key:
                agg_dict = {
                    f'{col}_win_{win}_offset_{offset}_{stat}': stat
                    for stat in stats
                }
                stats_df = df[col].rolling(win, **self.rolling_kwargs).agg(agg_dict)
                df_lst.append(stats_df)

                if self.quantiles is not None:
                    for qtl in self.quantiles:
                        pct_df = df[col].rolling(win, **self.rolling_kwargs).quantile(qtl)
                        pct_df.name = f'{col}_win_{win}_offset_{offset}_q{qtl}'
                        df_lst.append(pct_df)

        return pd.concat(df_lst, axis=1).shift(offset, self.freq)

    @property
    def stats(self):
        return self._stats

    @stats.setter
    def stats(self, value):
        if value is None:
            logging.warning('using all default stats {}'.format(self.DEFAULT_STATS))
            self._stats = self.DEFAULT_STATS
        else:
            self._stats = list(value)

    @property
    def rolling_kwargs(self):
        return self._rolling_kwargs

    @rolling_kwargs.setter
    def rolling_kwargs(self, value):
        if value is None:
            self._rolling_kwargs = self.DEFAULT_ROLLING_KWARGS
        elif not isinstance(value, dict):
            raise ValueError('rolling_kwargs should be a dict!')
        else:
            self._rolling_kwargs = value

    @property
    def quantiles(self):
        return self._quantiles

    @quantiles.setter
    def quantiles(self, value):
        if value is not None:
            if isinstance(value, (float, int)):
                self._quantiles = [value]
            else:
                self._quantiles = list(value)

            for qtl in self._quantiles:
                if qtl > 1 or qtl < 0:
                    raise ValueError('Percentiles have to be float between 0 and 1!')

        else:
            self._quantiles = value


from random import randint as randi

words = ['Capital', 'Country', 'City', 'District', 'Region']
a = 0
b = len(words) - 1
rand = randi(a, b)
shadow = ''
word_for_game = words[rand] # слово по рандомному индексу

for _ in word_for_game: # форматируем слово в ****
    shadow += '*'

while True:
    print('Guess a letter:', end='')
    prospective_letter = str(input()) # вводим предполагаемую букву
    up_prosp_letter = prospective_letter.upper()

    where_is_letter = [] # список с индексами мест, на которых находится данная буква
    i = 0
    number_of_mistake = 0

    if prospective_letter in word_for_game:
        print('Hit!')
        for _ in word_for_game:
            if _ == prospective_letter:
                where_is_letter.append(i)
            i += 1
        for _ in where_is_letter:
            shadow = shadow[:_] + prospective_letter + shadow[_+1:]
        print('The word:', shadow)

    elif up_prosp_letter in word_for_game:
        print('Hit!')
        for _ in word_for_game:
            if _ == up_prosp_letter:
                where_is_letter.append(i)
            i += 1
        for _ in where_is_letter:
            shadow = shadow[:_] + up_prosp_letter + shadow[_+1:]
        print('The word:', shadow)

    else:
        number_of_mistake += 1
        print('Missed, mistake ', number_of_mistake, 'out of 5')
        if number_of_mistake == 5:
            print('You lost!')
            break

    if '*' not in shadow:
        print('You won!')
        break


from typing import List, NamedTuple

from mediapipe.python.solutions.drawing_utils import draw_landmarks, DrawingSpec
from mediapipe.python.solutions.hands import Hands
from mediapipe.python.solutions.hands_connections import HAND_CONNECTIONS
from numpy import ndarray

from src.model.color import Color
from src.tracker.tracker import Tracker

class HandTracker(Tracker):
    """A tracker class used for hand tracking."""

    _results: NamedTuple

    def __init__(self):
        """Initializes the tracker class."""

        self._solution = Hands(max_num_hands=1, model_complexity=0, min_detection_confidence=0.8,
                               min_tracking_confidence=0.6)

    def track(self, image: ndarray, draw: bool = True) -> ndarray:
        self._results = self._solution.process(image)

        # Draw marks on the hand image
        if self._results.multi_hand_landmarks:
            for hand_landmarks in self._results.multi_hand_landmarks:
                if draw:
                    draw_landmarks(
                        image,
                        hand_landmarks,
                        HAND_CONNECTIONS,
                        landmark_drawing_spec=DrawingSpec(color=Color.RED.color)
                    )
        return image

    def get_landmarks(self, image: ndarray) -> list:
        landmarks: List[List[int, int, int]] = []
        if self._results.multi_hand_landmarks:
            for hand_landmarks in self._results.multi_hand_landmarks:
                for (i, landmark) in enumerate(hand_landmarks.landmark):
                    height, width, _ = image.shape
                    x = int(landmark.x * width)
                    y = int(landmark.y * height)
                    landmarks.append([i, x, y])
        return landmarks


import logging
import os
import time
import warnings
from datetime import datetime, timedelta
import onnxruntime
import json
warnings.filterwarnings("ignore")
import cv2
import numpy as np
import torch

from clogging import LOGGING_PATH, setup_logger

time_run = datetime.now()
dt_string = time_run.strftime("%d-%m-%Y")
log_fname = '%s' % dt_string
logger = setup_logger(log_fname,LOGGING_PATH +
                       '/log_%s.log' % dt_string, level=logging.DEBUG)

class ObjectDetectionClassification:
    """
        Performs the Object Localization or extraction into important entities of poses from image and gives coorniates for detected bbox
        and also gives the classification if images into 2 classes Helathy,Disease

    Args:
        self: constructor,
        combined_json: configuration file json

    Returns:
        None

    """

    def __init__(self,img):
        self.file_dir = os.getcwd()
        self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
        logger.info(['FILE DIR',self.file_dir])
        self.img = img
        #Config file
        with open("config.json") as config_file:
            self.config_data = json.load(config_file)
        config_file.close()
        self.model_path = os.path.join(self.file_dir,self.config_data['models_location_path'])
        self.object_detection_model = os.path.join(self.model_path,self.config_data['obj_detection_model'])

    def load_model(self):
        """
            Loads Object Detection Model in ONNX runtime.

        Args:
            self: constructor

        Returns:
            ONNX Runtime inference session
        """
        try:
            cuda = False
            model_name = self.object_detection_model
            providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
            session = onnxruntime.InferenceSession(model_name, providers=providers)
            return session
        except Exception as e:
            print(['Exception occured in loading model',e])

    def scale_coords(self,img1_shape, coords, img0_shape, ratio_pad=None):
        '''
            Rescale coords (xyxy) from img1_shape to img0_shape
            Args:
            img1_shape: resized image shape
            img0_shape: original input image shape
            coords: predictions returned from object detection model

            Returns:
            ROI coordinates in original image
        '''
        try:
            if ratio_pad is None:  # calculate from img0_shape
                gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
                pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2  # wh padding
            else:
                gain = ratio_pad[0][0]
                pad = ratio_pad[1]

            coords[ [0, 2]] -= pad[0]  # x padding
            coords[ [1, 3]] -= pad[1]  # y padding
            coords[ 0:4] /= gain
            return coords
        except Exception as e:
            print(['Exception occured in scale_coords function ',e])

    def letterbox(self,im: np.array, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleup=True, stride=32):
        """
            Creates Letter Box or gives depth,width,height of boxes.

        Args:
            self: constructor,
            im (array): image of input card,
            new_shape (filter): image resizing filter,
            color:  color in RGB channel,
            auto,scaleup,stride: other tuning parameters

        Returns:
            Image with ratio,width and height of boxes
        """
        try:
            # Resize and pad image while meeting stride-multiple constraints
            shape = im.shape[:2]  # current shape [height, width]

            if isinstance(new_shape, int):
                new_shape = (new_shape, new_shape)

            # Scale ratio (new / old)
            r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
            if not scaleup:  # only scale down, do not scale up (for better val mAP)
                r = min(r, 1.0)

            # Compute padding
            new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
            dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding

            if auto:  # minimum rectangle
                dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding

            dw /= 2  # divide padding into 2 sides
            dh /= 2

            if shape[::-1] != new_unpad:  # resize
                im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_CUBIC)
            top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
            left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
            # The image returned below gets degraded
            im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
            return im, r, (dw, dh)
        except Exception as e:
            print(['Exception occured in creating bounding box',e])

    def preprocess_image(self):
        """
            Preprocessing of input image.

        Args:
            self: constructor,
            img (array): image of input

        Returns:
            Image with ratio,width and height of boxes
        """
        try:
            # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            logger.info(['Processing Images'])
            image = self.img.copy()
            image, ratio, dwdh = self.letterbox(image, auto=False)
            original_image = image
            image = image.transpose((2, 0, 1))
            image = np.expand_dims(image, 0)
            image = np.ascontiguousarray(image)
            im = image.astype(np.float32)
            im /= 255
            return im,original_image,ratio,dwdh
        except Exception as e:
                print(['Exception occured in preprocessing image',e])

    def object_model_predictions(self,img,ort_session):
        """
            Object Detection Model predictions using ONNX runtime.

        Args:
            self: constructor,
            img (array): image of input ,
            ort_session: onnx modelsession,

        Returns:
            Output predictions of different rois or entities with craft coordinates
        """

        try:
            outname = [i.name for i in ort_session.get_outputs()]
            inname = [i.name for i in ort_session.get_inputs()]
            inp = {inname[0]:img}
            # ONNX inference
            outputs = ort_session.run(outname, inp)[0]
            return outputs
        except Exception as e:
            print(['Exception occured in ONNX Inference session and predictions',e])

    def roi_extraction(self,img,outputs,im0):
        """
            Extracts ROIs from the Model outputs

        Args:
            self: constructor,
            img (array): preprocessed image,
            original_image: original input image ,
            ratio (float):  ratio, in which boxes are proportioned,
            dw,dh (float): width and height of bounding box,
            outputs (array): bounding boxes predictions for different entities
            im0: original input image

        Returns:
            Bounding box extracted for different entities clubbed in a Dictionary
        """

        try:
            # roi ext
            roi_dict = dict()
            pred = outputs
            names = ['disease','healthy']

            # Process detections
            for det in enumerate(pred):  # detections per image
                if len(det):
                    # Rescale boxes from img_size to im0 size
                    det1 = self.scale_coords(img.shape[2:], det[1][1:5], im0.shape).round()
                    logger.info(['Scaled Coordinates',det1])
                    cls = det[1][5]
                    score = det[1][6]
                    cls_id = int(cls)
                    name = names[cls_id]
                    logger.info(['Class Name',name])
                    score = round(float(score),3)
                    box_coordinates = [int(coord) for coord in det1]
                    logger.info(['Confidence Score',score])
                    im = cv2.rectangle(im0, (box_coordinates[0], box_coordinates[1]), (box_coordinates[2], box_coordinates[3]), (0, 255, 0), 2)
                    # Add text to the image
                    font = cv2.FONT_HERSHEY_SIMPLEX
                    cv2.putText(im, name, (box_coordinates[0], box_coordinates[1] - 10), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
            return im
        except Exception as e:
            print(['Exception occured in roi_extraction from images',e])

    def main(self):
        """
            Integration of all above methods,gives coordinates of bbox and image classification results

        Args:
            self: constructor,
            image (array): original input image

        Returns:
            Dictionary with bbox img
        """
        try:
            logger.info(['Starting Main Function In Object Detection/Classifiation'])
            start_t = time.perf_counter()
            object_model = self.load_model()
            img,_,_,_ = self.preprocess_image()
            preds = self.object_model_predictions(img,object_model)
            im = self.roi_extraction(img,preds,self.img)
            end_t = time.perf_counter()
            print("Time Taken for execution:",end_t-start_t)
            logger.info(["Returning Output Image"])
            return im
        except Exception as e:
            logger.debug(['Exception occured in main function',e])


"""
66. 二叉树的前序遍历
给出一棵二叉树，返回其节点值的前序遍历。

样例
给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3
 返回 [1,2,3].

挑战
你能使用非递归实现么？
"""

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

# 递归
class Solution:

    result = []

    """
    @param root: A Tree
    @return: Preorder in ArrayList which contains node values.
    """
    def preorderTraversal(self, root):
        # write your code here
        self.result = []
        self.helper(root)
        return self.result

    def helper(self, root):
        if not root:
            return
        self.result.append(root.val)
        self.helper(root.left)
        self.helper(root.right)

# 非递归
# 1. 有点类似BFS的写法，只不过是用stack而不是queue：
# 2. 首先把root入栈
# 3. 出栈的元素同时放进结果队列
# 4. 先把右儿子节点入栈，再把左儿子节点入栈，这样出栈的顺序是先左后右（根节点已出）
# 按照这个次序继续，直到stack为空
class Solution1:
    """
    @param root: A Tree
    @return: Preorder in ArrayList which contains node values.
    """
    def preorderTraversal(self, root):
        # write your code here
        if not root:
            return []
        stack = [root]
        result = []
        while stack:
            curr = stack.pop()
            result.append(curr.val)
            if curr.right:
                stack.append(curr.right)
            if curr.left:
                stack.append(curr.left)
        return result


import jajapy as ja

def example_2():
	# MODEL CREATION
	#----------------
	# State 0 is labelled with B, state 1 with T, etc...
	labelling = list("BTSXSPTXPVVE")
	initial_state = 0
	name = "MC_REBER"
	# From state 0 we move to state 1 with probability 0.5
	# and to state 5 with probability 0.5, and so on...
	transitions = [(0,1,0.5),(0,5,0.5),(1,2,0.6),(1,3,0.4),(2,2,0.6),(2,3,0.4),
				   (3,7,0.5),(3,4,0.5),(4,11,1.0),(5,6,0.7),(5,9,0.3),
				   (6,6,0.7),(6,9,0.3),(7,6,0.7),(7,9,0.3),(8,7,0.5),(8,4,0.5),
				   (9,8,0.5),(9,10,0.5),(10,11,1.0),(11,11,1.0)]
	original_model = ja.createMC(transitions,labelling,initial_state,name)

	# SETS GENERATION
	#------------------------
	# We generate 1000 sequences of 10 observations for each set
	training_set = original_model.generateSet(1000,10)
	test_set = original_model.generateSet(1000,10)

	# LEARNING
	#---------
	nb_trials = 10 # we will learn this model 10 times
	best_model = None
	quality_best = -1024
	for n in range(1,nb_trials+1):
		current_model = ja.BW().fit(training_set,nb_states=12,pp=n, stormpy_output=False)
		current_quality = current_model.logLikelihood(test_set)
		if quality_best < current_quality: #we keep the best model only
				quality_best = current_quality
				best_model = current_model

	print("Best model:")
	print(best_model)
	print('loglikelihood of the test set under the best model:')
	print(quality_best)

if __name__ == '__main__':
	example_2()


'''
simple command line script to strip script tags from archived html files in order to reduce page load time and remove trackers. Self documenting :)
'''
from bs4 import BeautifulSoup
import argparse
import os

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("inputfile", help="path of the file for which you want to strip tags",type=str)
    parser.add_argument("outputfile", help="path of file that will be outputted",type=str)
    parser.add_argument("--tags",nargs='*', help="tags you want to strip. Defaults to 'script'. Input like so (without quotes): '--tags script img style'",type=str,default=['script'])
    parser.add_argument("--encoding",help="html file encoding. Defaults to utf-8",type=str,default='utf-8')
    args = parser.parse_args()

    input_file = args.inputfile
    output_file = args.outputfile
    tags = args.tags
    encoding = args.encoding

    with open(input_file,encoding=encoding) as html_input_file:
        soup = BeautifulSoup(html_input_file,'html.parser')
        for tag in tags:
            #thanks to https://stackoverflow.com/a/5598705/4188138 for this
            [element.extract() for element in soup.findAll(tag)]

    #make parent directory of output file if it doesn't exist
    os.makedirs(os.path.dirname(output_file),exist_ok=True)

    with open(output_file,'w+',encoding='utf-8') as html_output_file:
        html_output_file.write(str(soup.html))


import unittest
import calc_funcs

# class TddTest(unittest.TestCase):
#     def test_add(self):
#         self.assertEqual(calc_funcs.add(5, 3), 7)
#
#     def test_sub(self):
#         res = calc_funcs.sub(5, 3)
#         if res > 2:
#             bool_val = True
#         else:
#             bool_val = False
#
#         self.assertTrue(bool_val)
#
#     def test_div(self):
#         self.assertRaises(ZeroDivisionError, calc_funcs.div(4, 0))
#
#     def test_mul(self):
#         none_check = True
#         res = calc_funcs.mul(10, 9)
#
#         if res > 100:
#             none_check = None
#
#         self.assertIsNone(none_check)

def test_add():
    assert calc_funcs.add(10,20), 30

if __name__ == "__main__":
    testcase = unittest.FunctionTestCase(test_add(),
                                         setUp=setUp,
                                         tearDown=tearDown)