import re
import json
import time
import six

from six import PY2

from functools import wraps

from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
from twisted.internet.defer import Deferred
from twisted.python import log as twlog

def _to_json(output_object):
    return (json.dumps(
        output_object,
        indent=2,
        separators=(',', ': '),
        sort_keys=True,
    ) + '\n').encode()

class _JsonResource(Resource):
    _result = ''
    isLeaf = True

    def __init__(self, result, executed):
        Resource.__init__(self)
        self._result = result
        self._executed = executed

    def _setHeaders(self, request):
        """
        Those headers will allow you to call API methods from web browsers, they require CORS:
            https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
        """
        request.responseHeaders.addRawHeader(b'content-type', b'application/json')
        request.responseHeaders.addRawHeader(b'Access-Control-Allow-Origin', b'*')
        request.responseHeaders.addRawHeader(b'Access-Control-Allow-Methods', b'GET, POST, PUT, DELETE')
        request.responseHeaders.addRawHeader(b'Access-Control-Allow-Headers', b'x-prototype-version,x-requested-with')
        request.responseHeaders.addRawHeader(b'Access-Control-Max-Age', 2520)
        return request

    def render(self, request):
        self._setHeaders(request)
        # this will just add one extra field to the response to populate how fast that API call was processed
        self._result['execution'] = '%3.6f' % (time.time() - self._executed)
        return _to_json(self._result)

class _DelayedJsonResource(_JsonResource):
    """
    If your API method returned `Deferred` object instead of final result
    we can wait for the result and then return it in API response.
    """

    def _cb(self, result, request):
        self._setHeaders(request)
        result['execution'] = '%3.6f' % (time.time() - self._executed)
        raw = _to_json(result)
        if request.channel:
            request.write(raw)
            request.finish()
        else:
            twlog.err('REST API connection channel already closed')

    def _eb(self, err, request):
        self._setHeaders(request)
        execution = '%3.6f' % (time.time() - self._executed)
        raw = _to_json(dict(status='ERROR', execution=execution, errors=[str(err), ]))
        if request.channel:
            request.write(raw)
            request.finish()
        else:
            twlog.err('REST API connection channel already closed')

    def render(self, request):
        self._result.addCallback(self._cb, request)
        self._result.addErrback(self._eb, request)
        return NOT_DONE_YET

def maybeResource(f):
    @wraps(f)
    def inner(*args, **kwargs):
        _executed = time.time()
        try:
            result = f(*args, **kwargs)

        except Exception as exc:
            return _JsonResource(dict(status='ERROR', errors=[str(exc), ]), _executed)

        if isinstance(result, Deferred):
            return _DelayedJsonResource(result, _executed)

        if not isinstance(result, Resource):
            result = _JsonResource(result, _executed)

        return result
    return inner

class JsonAPIResource(Resource):

    _registry = None

    def __new__(cls, *args, **kwds):
        instance = super().__new__(cls, *args, **kwds)
        instance._registry = []
        for name in dir(instance):
            attribute = getattr(instance, name)
            annotations = getattr(attribute, "__txrestapi__", [])
            for annotation in annotations:
                method, regex = annotation
                instance.register(method, regex, attribute)
        return instance

    def __init__(self, *args, **kwargs):
        Resource.__init__(self, *args, **kwargs)
        if PY2:
            self._registry = []

    def _get_callback(self, request):
        request_method = request.method
        path_to_check = getattr(request, '_remaining_path', request.path)
        if not isinstance(path_to_check, six.text_type):
            path_to_check = path_to_check.decode()
        for m, r, cb in self._registry:
            if m == request_method or m == b'ALL':
                result = r.search(path_to_check)
                if result:
                    request._remaining_path = path_to_check[result.span()[1]:]
                    return cb, result.groupdict()
        return None, None

    def register(self, method, regex, callback):
        if not isinstance(regex, six.text_type):
            regex = regex.decode()
        self._registry.append((method, re.compile(regex), callback))

    def unregister(self, method=None, regex=None, callback=None):
        if regex is not None:
            if not isinstance(regex, six.text_type):
                regex = regex.decode()
            regex = re.compile(regex)
        for m, r, cb in self._registry[:]:
            if not method or (method and m == method):
                if not regex or (regex and r == regex):
                    if not callback or (callback and cb == callback):
                        self._registry.remove((m, r, cb))

    def getChild(self, name, request):
        r = self.children.get(name, None)
        if r is None:
            # Go into the thing
            callback, args = self._get_callback(request)
            if callback is None:
                return _JsonResource(dict(status='ERROR', errors=['path %r not found' % name, ]), time.time())
            else:
                return maybeResource(callback)(request, **args)
        else:
            return r


'''Single Number III

重点在于如何区分出不同的两个数，全部异或可以去到两个数所有不同的比特位
然后，xor & -xor 可以取到唯一一位不同bit的位置，这样就可以求解了。
'''
class Solution(object):
    def singleNumber(self, nums):
        xor = reduce((lambda x, y: x ^ y), nums)
        low_bit = xor & -xor
        a = b =0
        for num in nums:
            if num & low_bit:
                a ^= num
            else:
                b ^= num
        return a, b


# Installed
# import cv2
# import numpy as np
import time

# Project
# from grabscreen import grab_screen
import keys as k

# from getkeys import key_check

keys = k.Keys({})

for i in list(range(3))[::-1]:
    print(i + 1)
    time.sleep(1)

keys.directMouse(100, -100)


from PySide2.QtUiTools import loadUiType
from PySide2.QtWidgets import QMainWindow,QApplication
from os import path
from sys import argv
from cashier import cashierapp
from checkitem import checkitem
from storage import storagewin

FORM_CLASS,_=loadUiType(path.join(path.dirname(__file__),"GUI/admin.ui"))

class adminwin(QMainWindow,FORM_CLASS):
	def __init__(self, parent=None):
		super(adminwin,self).__init__(parent)
		self.setupUi(self)
		self.cashier = cashierapp()
		self.checkitem = checkitem()
		self.storage = storagewin()
		self.storage_bt.clicked.connect(self.storagewin)
		self.cashier_bt.clicked.connect(self.cashierwin)
		self.check_bt.clicked.connect(self.checkwin)

	def cashierwin(self):
		self.cashier.show()
	def checkwin(self):
		self.checkitem.show()
	def storagewin(self):
		self.storage.show()
if __name__ == "__main__":
	app = QApplication(argv)
	MainWindow = QMainWindow()
	window = adminwin()
	window.show()
	exit(app.exec_())


def num_of_diff_comb(*args):
    ''' Return the number of different combinations using one entry from each provided set.'''
    c = 1
    for s in args:
        c *= len(s)
    return c

def count_strings(s: set, n: int, f: int = 0):
    '''Given a set of characters, return the number of different combinations of length n, and f being the number of character already determined in these cominations'''
    return len(s) ** (n-f)

def num_of_perms(n:int, r:int = 0) -> int:
    '''Return the number of permutations of size r'''
    if not r: r = n
    return fact(n) // fact(n-r)

def num_of_perms_2(n: int, r:int = 0) -> int:

    if not r: r = n

    res = 1
    for _ in range(r):
        res *= n
        n -= 1
    return res

def seq_perms(s):
    return fact(len(s))

def num_of_combs(n: int, r: int = 0) -> int:
    '''Return the number of r-sized subsets'''
    if not r: r = n
    return fact(n) // (fact(r) * fact(n-r))

def fact(n: int):
    ''' Return the factorial of the given number'''
    return n * fact(n-1) if n else 1

def n_choose_r(n: int, r: int) -> int:
    '''Given n and r, return the number of ways to choose r from a set of size n.'''
    return fact(n) // (fact(r) * fact(n-r))

def num_of_perms_in_string(s: str) -> int:
    '''Return the number of ways to permute the characters in a string'''
    l = {}
    for c in s:
        if c in l:
            l[c] += 1
        else:
            l[c] = 1

    num = len(s)
    den = 1
    for _,v in l.items():
        den *= fact(v)
    return fact(num) // den

def count_multisets(l: list) -> int:
    s = set(l)
    n = len(l)
    m = len(s)
    return  n_choose_r(n + m - 1, m-1)

def count_multisets_2(n: int, m: int) -> int:
    return n_choose_r(n + m - 1, m-1)

def next_perm(p):
    n = list(p)
    x = 0

    i = len(n) - 1
    while 0 < i:
        if((n[i-1]) < n[i]):
            x = i-1
            break
        i -= 1

    swap = n.index(min((i for i in n[x+1:] if i > n[x])))

    n[swap],n[x] = n[x],n[swap]
    return n[:x+1] + sorted(n[x+1:])

def gen_perms(n):
    perms = []
    p,e = list(range(n)),list(reversed(range(n)))
    perms.append(p)

    while p != e:
        p = next_perm(p)
        perms.append(p)

    return perms

def gen_perms_rec(s):
    if len(s) == 2: return [s,[s[1],s[0]]]
    else:           return [[s[i]] + x for i in range(len(s)) for x in gen_perms_rec(s[:i]+s[i+1:])]

def next_subset(n,s):
    f = list(s)
    n = list(range(1,n+1))
    x = 0
    for i in range(1,len(f)+1):
        if f[-i] < n[-i]:
            x = len(f)-i
            break
    f[x] += 1
    x += 1

    while x < len(f):
        f[x] = f[x-1] + 1
        x += 1

    return f

def gen_lex_subsets(r,n):
    subsets = []
    s = list(range(1,r+1))
    f = list(range(n-r+1,n+1))
    subsets.append(s)

    while s != f:
        s = next_subset(n,s)
        subsets.append(s)
    return subsets

def gen_div_by(d,n):
    i = 1
    while i <= n:
        if i % d == 0:
            yield i
        else:
            yield 0
        i += 1

def find_common_235():
    u = 0
    for i,j,k in zip(   gen_div_by(2,30),   \
                        gen_div_by(3,30),   \
                        gen_div_by(5,30)    ):

        if i and j and k:   u += 1
        if i and j:         u -= 1
        if i and k:         u -= 1
        if j and k:         u -= 1
        if i:               u += 1
        if j:               u += 1
        if k:               u += 1

    return u

def divisible_by(n,a,b,c):
    u = 0
    for i,j,k in zip(   gen_div_by(a,n),   \
                        gen_div_by(b,n),   \
                        gen_div_by(c,n)    ):
        if i and j and k:   u += 1
        if i and j:         u -= 1
        if i and k:         u -= 1
        if j and k:         u -= 1
        if i:               u += 1
        if j:               u += 1
        if k:               u += 1
    return u


import os,cv2
import numpy as np

def transform(x, y, ang, s0, s1):
    x0 = x - s0[1] / 2
    y0 = y - s0[0] / 2
    xx = x0 * np.cos(ang) - y0 * np.sin(ang) + s1[1]/2
    yy = x0 * np.sin(ang) + y0 * np.cos(ang) + s1[0]/2
    return xx, yy

def guard(x, n):
    x[x < 1] = 1
    if n[1] > 0:
        if x[0] > n[1]:
            x[0] = n[1]
        if x[1] > n[1]:
            x[1] = n[1]
    if n[0] > 0:
        if x[2] > n[0]:
            x[2] = n[0]
        if x[3] > n[0]:
            x[3] = n[0]
    x[x < 1] = 1
    return x

def align(img, keypoints, crop_size=128, ec_mc_y=48, ec_y=40):
    keypoints = keypoints.astype(np.float64)
    if keypoints[0,0] == keypoints[1,0]:
        ang = 0
    else:
        ang_tan = (keypoints[0,1] - keypoints[1,1]) / \
            (keypoints[0,0] - keypoints[1,0])
        ang = np.arctan(ang_tan) / np.pi * 180
    M = cv2.getRotationMatrix2D((img.shape[1]//2, img.shape[0]//2),ang, 1.0)
    img_rotate = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
    h, w = img.shape[:2]
    x = (keypoints[0,0] + keypoints[1,0]) / 2
    y = (keypoints[0,1] + keypoints[1,1]) / 2

    ang = -ang / 180 * np.pi
    xx, yy = transform(x, y, ang, img.shape[:2], img_rotate.shape[:2])
    eyec = np.array([np.round(xx), np.round(yy)])

    x = (keypoints[3,0] + keypoints[4,0]) / 2
    y = (keypoints[3,1] + keypoints[4,1]) / 2
    xx, yy = transform(x, y, ang, img.shape[:2], img_rotate.shape[:2])
    mouthc = np.array([np.round(xx), np.round(yy)])

    resize_scale = ec_mc_y / (mouthc[1] - eyec[1])
    if resize_scale < 0:
        resize_scale = 1
    img_resize = cv2.resize(img_rotate, (int(resize_scale*img_rotate.shape[0]), int(resize_scale*img_rotate.shape[1])))
    eyec2 = (eyec - np.array([img_rotate.shape[1]/2, img_rotate.shape[0]/2])) * resize_scale + np.array([img_resize.shape[1]/2, img_resize.shape[0]/2])
    eyec2 = np.round(eyec2)
    trans_points = np.zeros((5,2))
    trans_points[:,0], trans_points[:,1] = transform(keypoints[:,0],keypoints[:,1], ang, img.shape[:2], img_rotate.shape[:2])
    trans_points = np.round(trans_points)
    trans_points[:,0] -= img_rotate.shape[1]/2
    trans_points[:,1] -= img_rotate.shape[0]/2
    trans_points *= resize_scale
    trans_points[:,0] += img_resize.shape[1] / 2
    trans_points[:,1] += img_resize.shape[0] / 2
    trans_points = np.round(trans_points)

    img_crop = np.zeros((crop_size, crop_size, 3))
    crop_y = int(eyec2[1] - ec_y)
    crop_y_end = int(crop_y + crop_size - 1)
    crop_x = int(eyec2[0] - np.floor(crop_size/2))
    crop_x_end = int(crop_x + crop_size - 1)
    box = guard(np.array([crop_x, crop_x_end, crop_y, crop_y_end]), img_resize.shape)
    box = box.astype(np.int32)
    img_crop[box[2]-crop_y+1:box[3]-crop_y+1, box[0]-crop_x+1:box[1]-crop_x+1,:] = img_resize[box[2]:box[3],box[0]:box[1],:]
    trans_points[:,0] -= crop_x - 1
    trans_points[:,1] -= crop_y - 1
    trans_points = trans_points.astype(np.int32)
    cropped = img_crop / 255
    return img_crop, trans_points

if __name__ == '__main__':
    path = './other_data/'
    files = os.listdir(path)
    for f in files:
        if 'png' not in f:
            continue
        img = cv2.imread(path+f)
        with open(path+f.replace('png','5pt')) as ff:
            lines = ff.readlines()
        keypoints = np.zeros((5,2))
        for index, line in enumerate(lines):
            line = line.strip()
            keypoints[index,:] = line.split(',')
        img_cropped, trans_pt = align(img, keypoints)
        cv2.imwrite('./solved_data/'+f, img_cropped)
        keypoints = keypoints.astype(np.int32)
        with open('./solved_data/'+f.replace('png','5pt'),'a') as ff:
            for point in trans_pt:
                ff.write(str(point[0])+','+str(point[1])+'\n')
                cv2.circle(img_cropped, (int(point[0]),int(point[1])),2,(255,255,0),3)
        #cv2.imwrite(f,img_cropped)


# Tag: BST
# 108. Convert Sorted Array to Binary Search Tree
# -----------------------------------------------------------------------------------
# Description:
# Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

# For this problem, a height-balanced binary tree is defined as a binary tree in which the depth
# of the two subtrees of every node never differ by more than 1.
# -----------------------------------------------------------------------------------
#Example:
# Given the sorted array: [-10,-3,0,5,9],

# One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

#       0
#      / \
#    -3   9
#    /   /
#  -10  5
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# 思路: Recursion
# base case:
# nothing to insert: return
# recursive step:
# insert mid val of sub arr to curr root,return root
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        def recursion(nums):
            if not nums:
                return
            if len(nums) == 1:
                return TreeNode(nums[0])
            else:
                mid = len(nums) //2
                root = TreeNode(nums[mid])
                root.left = recursion(nums[:mid])
                root.right = recursion(nums[mid+1:])
                return root

        root = recursion(nums)
        return root

117-Convert Sorted Array to Binary Search Tree.py


from confluent_kafka import Producer
import json
import time
import logging
import random
import csv
import os
import sys

from libs.helper import load_schema, check_schema, encode_failed

from dotenv import load_dotenv

class KafkaProducer:
    # producer
    producer: Producer = None

    def __init__(self, dotenv_path=".env"):
        # Load environment variables
        load_dotenv(override=False, dotenv_path=dotenv_path)

        self.ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

        logging.basicConfig(
            format="%(asctime)s %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S",
            filename=os.getenv("LOG_FILE_PATH"),
            filemode="w",
        )

        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)

    def create_producer(self):
        """
        create_producer Creates a Kafka Producer

        :return: The Kafka Producer
        """
        print("Trying to setup Kafka Producer...")
        self.producer = Producer(
            {"bootstrap.servers": str(os.getenv("KAFKA_BOOTSTRAP_SERVERS"))}
        )
        print("Kafka Producer has been initiated...")

    def receipt(self, err, msg):
        """
        receipt Callback function for message produce

        :param err: The error message
        :param msg: The message
        """
        if err is not None:
            self.logger.error("Error: {}".format(err))
            m = json.dumps(msg.value().decode("utf-8"))

            # extract wiki name from msg.topic
            wiki = msg.topic().split("-")[1]

            self.produce_message(
                "wikipedia-{}-failed-dlq".format(wiki), encode_failed(m)
            )
        else:
            # parse string to json object
            m = json.loads(msg.value().decode("utf-8"))

            message = "Produced message on topic {} with wikipedia change id {}".format(
                msg.topic(), m["id"]
            )
            self.logger.info(message + "\n")
            print(message)

    def produce_message(self, topic: str, message):
        """
        produce_message Produces a message to a topic

        :param topic: The topic to produce to
        :param message: The message to produce
        """

        try:
            # Produce the message
            self.producer.produce(topic, message.encode("utf-8"), callback=self.receipt)
            self.logger.info(
                "Message {} produced to topic {}".format(message.encode("utf-8"), topic)
            )

            # Flush the producer > wait for all messages to be delivered
            self.producer.flush()
            self.logger.info("Producer flushed")

        except Exception as e:
            self.logger.error("Failed to produce message: {}".format(e))

    def produce_csv_messages(self, path, encoding="utf-8", delimiter=","):
        """
        produce_csv_messages Reads the data from the csv file

        :param path: The path to the csv file
        :param encoding: The encoding of the csv file (default: utf-8)
        :param delimiter: The delimiter of the csv file (default: ,)
        """

        # load schema from src\static\wikipedia-schema.json
        schema = load_schema(
            os.path.join(
                self.ROOT_DIR,
                "static/wikipedia-schema.json",
            )
        )

        with open(path, encoding=encoding) as csvfile:
            # read csv file
            reader = csv.DictReader(csvfile, delimiter=delimiter)
            for row in reader:
                # check if row is valid json and valid schema
                if check_schema(row, schema):
                    # produce message to topic wikipedia-changes-success
                    self.produce_message("wikipedia-success", json.dumps(row))

                    # produce message to topic wikipedia-WIKI-success
                    self.produce_message(
                        "wikipedia-{}-success".format(row["wiki"]),
                        json.dumps(row),
                    )
                else:
                    self.produce_message(
                        "wikipedia-failed-schema",
                        encode_failed(row),
                    )

                # Generate a random sleep time between 0 and 1 seconds
                sleep = random.random()
                # Sleep for the random time
                time.sleep(sleep)

                print("Sleeping for {} seconds".format(sleep))

        print()
        print("Finished producing messages. Shutting down producer...")

    def run(self):
        """
        run Sets up the producer
        """
        # Create the producer
        self.create_producer()

        # Path to the csv file
        data_path = os.path.join(
            self.ROOT_DIR,
            "../data/raw/de_challenge_sample_data.csv",
        )
        # Parse the csv file and produce the messages
        self.produce_csv_messages(data_path)

if __name__ == "__main__":
    landing_zone = KafkaProducer()
    try:
        landing_zone.run()
    except KeyboardInterrupt:
        landing_zone.logger.info("%% Aborted by user")
    except Exception as e:
        landing_zone.logger.error(e)


import heapq
import itertools

# 1：通过生成式生成列表，集合和字典

prices = {
    'AAPL': 191.88,
    'GOOG': 1186.96,
    'IBM': 149.24,
    'ORCL': 48.44,
    'ACN': 166.89,
    'FB': 208.09,
    'SYMC': 21.29
}

prices2 = { key: value for key, value in prices.items() if value > 100 }

print('prices2', prices2)

# 2：嵌套列表的坑
# names = ['关羽', '张飞', '赵云', '马超', '黄忠']
# courses = ['语文', '数学', '英语']
# scores = [[None] * len(courses) for _ in range(len(names))]
# for row, name in enumerate(names):
#   for col, course in enumerate(courses):
#      scores[row][col] = float(input(f'请输入{name}的{course}成绩: '))
#      print(scores)

# 3：heapq 模块（推排序）
"""
从列表中找出最大的或最小的N个元素
堆结构(大根堆/小根堆)
"""
list1 = [34, 25, 12, 99, 87, 63, 58, 78, 88, 92]
list2 = [
    {'name': 'IBM', 'shares': 100, 'price': 91.1},
    {'name': 'AAPL', 'shares': 50, 'price': 543.22},
    {'name': 'FB', 'shares': 200, 'price': 21.09},
    {'name': 'HPQ', 'shares': 35, 'price': 31.75},
    {'name': 'YHOO', 'shares': 45, 'price': 16.35},
    {'name': 'ACME', 'shares': 75, 'price': 115.65}
]
print(heapq.nlargest(3, list1))
print(heapq.nsmallest(3, list1))
print(heapq.nlargest(2, list2, key=lambda x : x['price']))

# 4：迭代工具模块
# 产生ABCD的全排列
itertools.permutations('ABCD')
# 产生ABCDE的五选三组合
itertools.combinations('ABCDE', 3)
# 产生ABCD和123的笛卡尔积
itertools.product('ABCD', '123')
# 产生ABC的无限循环序列
itertools.cycle(('A', 'B', 'C'))

#5：collections模块
"""
namedtuple：命令元组，它是一个类工厂，接受类型的名称和属性列表来创建一个类。
deque：双端队列，是列表的替代实现。Python中的列表底层是基于数组来实现的，而deque底层是双向链表，因此当你需要在头尾添加和删除元素是，deque会表现出更好的性能，渐近时间复杂度为$O(1)$。
Counter：dict的子类，键是元素，值是元素的计数，它的most_common()方法可以帮助我们获取出现频率最高的元素。Counter和dict的继承关系我认为是值得商榷的，按照CARP原则，Counter跟dict的关系应该设计为关联关系更为合理。
OrderedDict：dict的子类，它记录了键值对插入的顺序，看起来既有字典的行为，也有链表的行为。
defaultdict：类似于字典类型，但是可以通过默认的工厂函数来获得键对应的默认值，相比字典中的setdefault()方法，这种做法更加高效。
"""
words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around',
    'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes',
    'look', 'into', 'my', 'eyes', "you're", 'under'
]

from collections import Counter

words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around',
    'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes',
    'look', 'into', 'my', 'eyes', "you're", 'under'
]
counter = Counter(words)
print(counter.most_common(3))


#!/usr/bin/env python
# coding=utf8

import logging
import setting
import simplejson
from lib.route import route
from model import *
from handler import BaseHandler
from tornado.web import RequestHandler

@route(r'/webapp', name='webapp_index')
class WebAppIndexHandler(BaseHandler):
    def get(self):
        brands = []
        f = self.get_argument('f', 'app')
        chars = '
#
'
        q = CarBrand.select(CarBrand.brand_pinyin_first).where(CarBrand.active == 1).group_by(CarBrand.brand_pinyin_first).\
            order_by(CarBrand.brand_pinyin_first.asc(), CarBrand.sort.asc()).tuples()
        for py, in q:
            chars += '
' + py + '
'
            item = {}
            item['char'] = py
            item['items'] = CarBrand.select().where((CarBrand.active == 1) & (CarBrand.brand_pinyin_first == py))
            brands.append(item)

        self.render("webapp/index.html", brands=brands, chars=chars, f=f)

@route(r'/webapp/car/(\d+)', name='webapp_car')
class WebAppCarHandler(BaseHandler):
    def get(self, id):
        brand = CarBrand.get(id=id)
        cars = []
        for factory in brand.factories:
            fac = {}
            fac['name'] = factory.factory_name
            fac['car'] = 0
            cars.append(fac)
            for car in factory.cars:
                c = {}
                c['name'] = car.car_name
                c['car'] = 1
                c['id'] = car.id
                c['logo'] = car.logo
                c['saleoff'] = car.stop_sale
                cars.append(c)
        else:
            for car in brand.cars:
                c = {}
                c['name'] = car.car_name
                c['car'] = 1
                c['id'] = car.id
                c['logo'] = car.logo
                c['saleoff'] = car.stop_sale
                cars.append(c)
        self.render("webapp/car.html", brand=brand, cars=cars)

@route(r'/webapp/car_item_list/(\d+)', name='webapp_car_item_list')  # 车型详情列表
class WebAppCarItemListHandler(RequestHandler):
    def get(self, id):
        result = {'flag': 0, 'msg': '', "data": {}}

        car = Car.get(id=id)
        if car:
            result['flag'] = 1
            result['data']['saleoff'] = car.stop_sale
            result['data']['name'] = car.car_name
            result['data']['logo'] = car.logo
            result['data']['detail_list'] = []
            for item in car.groups:
                group = {}
                group['name'] = item.group_name
                group['items'] = []
                result['data']['detail_list'].append(group)
                for carItem in item.items:
                    ci = {}
                    ci['id'] = carItem.id
                    ci['name'] = carItem.car_item_name
                    ci['gearbox'] = carItem.gearbox
                    ci['saleoff'] = carItem.stop_sale
                    group['items'].append(ci)

        else:
            result['msg'] = "系统异常"
        self.write(simplejson.dumps(result))

@route(r'/webapp/detail/(\d+)', name='webapp_detail')
class WebAppDetailHandler(BaseHandler):
    def get(self, id):
        carItem = CarItem.get(id=id)
        self.render("webapp/detail.html", car=carItem)


import MeCab
import math
import collections
tagger = MeCab.Tagger()

def worddividor(filepath):
    """do mornophical analysis, separate the words.
       if a noun (or name) connects to another noun (or name), combine into one word
    """
    dividors = []
    with open(filepath, "r", encoding="utf-8") as r:
        lines = r.readlines()
        for i in range(len(lines)):
            words_list = []
            node = tagger.parseToNode(lines[i])
            pos = "no"
            while node:
                if "名詞" in node.feature and "数" not in node.feature and "人名" not in node.feature:
                    if pos == "yes":
                        words_list[-1] = words_list[-1] + node.surface
                    else:
                        words_list.append(node.surface)
                        pos = "yes"
                elif "名詞" in node.feature and "数" not in node.feature and "人名" in node.feature:
                    if pos == "name":
                        words_list[-1] = words_list[-1] + node.surface
                    else:
                        words_list.append(node.surface)
                    pos = "name"
                else:
                    words_list.append(node.surface)
                    pos = "no"
                node = node.next
            if words_list:
                dividors.append(words_list)
    return dividors

def tfidf(lists, corpus):
    """calutate the tf-idf points,
       choose the top5 to output
    """
    usedwords = []
    documentlength = len(lists)
    wordslist = list(set(lists))
    wordslist.sort(key=lists.index)
    turns = collections.Counter(lists)
    turns = dict(turns)
    for i in wordslist:
        count = 0
        for j in corpus:
            if i in j:
                count += 1
        idf = math.log(len(corpus)/(count+1))
        tf = turns[i] / documentlength
        tf_idf = tf * idf
        turns[i] = tf_idf
    result = sorted(turns.items(), key=lambda item: item[1], reverse=True)
    for i in range(len(result)):
        usedwords.append(result[i][0])
    return usedwords[:5]  # 出力単語数

def main():
    filepath = "filepath"  # please input the path of txtfile
    allwords = []
    corpus = worddividor(filepath)
    for i in range(len(corpus)):
        goodkeywords = tfidf(corpus[i], corpus)
        allwords.append(goodkeywords)
    print(allwords)

if __name__ == "__main__":
    main()


""" /weather command """
from datetime import datetime, timedelta
from logging import getLogger
from forecastio import load_forecast
from geopy.geocoders import Nominatim
from telegram import Update
from telegram.ext import ContextTypes

log = getLogger(__name__)

def f_to_c(f_temp):
    """ convert temp unit """
    return int((float(f_temp) - 32.0) / 1.8)

def mph_to_kph(mph):
    """ convert speed unit """
    return mph * 1.60934

def weather_for_input(text, token=None):
    """ get weather for input like 'San Jose' """
    geolocator = Nominatim(user_agent='brambu/thedenbot/modules/weather')
    location = geolocator.geocode(text)
    if location is None:
        return None, None
    ret = load_forecast(token,
                        location.latitude,
                        location.longitude,
                        units='us')
    result = ret.json
    return location.address, result

def weather_print_result(address, result):
    """ print a forecastio forecast result """
    print_this = ''
    if address is None or result is None:
        print_this += 'aroo?'
        return print_this
    args = {}
    args.update(**result)
    args.update(address=address)
    template = [
        'Weather for {address}',
        '{current_time} {timezone}',
        '',
        'Currently: {currently[summary]} '
        '{currently[temperature]:0.0f}\u00b0F/{current_temperature_c}\u00b0C '
        'Feels like: {currently[apparentTemperature]:0.0f}\u00b0F'
        '/{feels_like_c}\u00b0C',
        'humidity: {humidity_pct}%, wind: {currently[windSpeed]}mph'
        '/{wind_kph:.2f}kph',
        'High: {daily[data][0][temperatureMax]:0.0f}\u00b0F'
        '/{daily_max_c}\u00b0C '
        'Low: {daily[data][0][temperatureMin]:0.0f}\u00b0F'
        '/{daily_min_c}\u00b0C ',
        '',
        # '{minutely[summary]} ',
        '{hourly[summary]} ',
        '{daily[summary]}'
    ]
    dt_current_time = datetime.utcfromtimestamp(args['currently']['time'])
    dt_current_time += timedelta(hours=args['offset'])
    current_time = dt_current_time.strftime('%Y/%m/%d %H:%M:%S')
    args.update(address=address)
    args.update(current_time=current_time)
    args.update(
        current_temperature_c=str(f_to_c(args['currently']['temperature']))
    )
    args.update(
        feels_like_c=str(f_to_c(args['currently']['apparentTemperature']))
    )
    args.update(
        daily_max_c=str(f_to_c(args['daily']['data'][0]['temperatureMax']))
    )
    args.update(
        daily_min_c=str(f_to_c(args['daily']['data'][0]['temperatureMin']))
    )
    args.update(
        humidity_pct=str(int(float(args['currently']['humidity']) * 100))
    )
    args.update(
        wind_kph=mph_to_kph(args['currently']['windSpeed'])
    )
    print_this += '\n'.join(template).format(**args)
    return print_this

async def weather_get(
        update: Update,
        context: ContextTypes.DEFAULT_TYPE,
) -> None:
    """ example /weather San Jose """
    token = context.application.bot_data.get('config', {}).get('darksky_token')
    text = update.message.text
    text = text.removeprefix('/weather ')
    address, result = weather_for_input(text, token=token)
    try:
        print_this = weather_print_result(address, result)
    except BaseException as ex:
        print_this = 'not sure'
        log.warning('weather error %s', ex)
    await update.message.reply_text(
        print_this,
        reply_to_message_id=update.message.message_id,
    )


try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

config = {
    'description': 'Snippet highlighter - Yelp Coding Challenge',
    'author': 'Nicolas Richard',
    'url': '',
    'download_url': '',
    'author_email': 'NicolasRichard3@gmail.com',
    'version': '0.1',
    'install_requires': ['nose', 'stemming', 'wsgiref'],
    'packages': ['snippet_highlighter'],
    'scripts': [],
    'name': 'snippet_highlighter'
}

setup(**config)


import click
from typing import Optional, List
from datetime import date, timedelta
from flask.cli import AppGroup
from webapp.services.base_service import get_base_service_dependencies
from webapp.services.action.action_exporter_service import ActionExporterService
from webapp.dependencies import dependencies

action_cli = AppGroup('action')

@action_cli.command("export", help='Exports all actions to an xlsx and sends it as an email attachment.')
@click.option('-b', '--begin', 'begin', type=click.DateTime(formats=['%Y-%m-%d']), help='If unset, the last month will be exported.')
@click.option('-e', '--end', 'end', type=click.DateTime(formats=['%Y-%m-%d']), help='If unset, the last month will be exported.')
@click.option('-r', '--recipient', 'recipients', multiple=True, help='If unset, admin emails will be used.')
def cli_queue_status_checks(begin: Optional[date] = None, end: Optional[date] = None, recipients: Optional[List[str]] = None):
    if begin is None or end is None:
        begin = (date.today().replace(day=1) - timedelta(days=1)).replace(day=1)
        end = date.today().replace(day=1) - timedelta(days=1)
    action_exporter_service = ActionExporterService(
        **get_base_service_dependencies(),
        mail_helper=dependencies.get_mail_helper(),
        action_repository=dependencies.get_action_repository(),
    )
    file_name = action_exporter_service.export(
        begin=begin,
        end=end,
    )
    action_exporter_service.send_export(
        begin=begin,
        end=end,
        file_name=file_name,
        recipients=recipients or dependencies.get_config_helper().get('SUMMARY_RECIPIENTS'),
    )


"""
chain()方法,接收多个输入
"""

from itertools import chain

a = [1, 2, 3, 4]
b = ['x', 'y', 'z']
c = [4, 5]

for x in chain(a, b, c):
    print(x, end=' ') # 1 2 3 4 x y z 4 5

"""
使用常规的加号可以将序列链接，但是局限于类型相同的序列，并且效率上也要比chain()差
"""

迭代器和生成器/迭代_在不同的容器_P133.py


from django.urls import path, re_path
from . import views

urlpatterns = [
    # Path Converters
    # int: numbers
    # str: strings
    # path: whole urls /
    # slug: hyphen-and_underscores_stuff
    # UUID: universally unique indentifier

    path('', views.home, name="home"),
    path('<int:year>/<str:month>/', views.home, name="home"),
    path('events', views.all_events, name="list-events"),
    path('add_event', views.add_event, name="add-event"),
    path('update_event/<event_id>', views.update_event, name="update-event"),
    path('delete_event/<event_id>', views.delete_event, name="delete-event"),
    path('show_event/<event_id>', views.show_event, name="show-event"),
    path('regist_event/<event_id>', views.regist_user_event, name="regist-event"),
    path('my_events/', views.my_events, name="my-events"),

    path('add_venue', views.add_venue, name="add-venue"),
    path('list_venues', views.list_venues, name="list-venues"),
    path('show_venue/<venue_id>', views.show_venue, name="show-venue"),
    path('update_venue/<venue_id>', views.update_venue, name="update-venue"),
    path('venue_updated', views.venue_updated, name="venue-updated"),
    path('search_venues', views.search_venues, name="search-venues"),
    path('delete_venue/<venue_id>', views.delete_venue, name="delete-venue"),
    path('venue_text', views.venue_text, name='venue-text'),
    path('venue_csv', views.venue_csv, name='venue-csv'),
    path('venue_pdf', views.venue_pdf, name='venue-pdf'),

    path('converter_png', views.converter_png, name='converter-png'),
    path('download_images/', views.download_images, name='download_images'),
    # path('image_png', views.download, name='image-png'),
    ]


import requests
from bs4 import BeautifulSoup as bs
from multiprocessing.pool import ThreadPool
import json
import csv
import sys
from tqdm import tqdm
from datetime import date
from time import sleep
from pathlib import Path
import re
from random import random

CD = Path().cwd()
TODAY = date.today().strftime('%d-%m-%Y')

class Artsy:
    def __init__(self):
        self.main_page = 'https://www.artsy.net/art-fairs'
        self.timeout = 120
        self.headers = {
                'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
            }
        self.export_path = Path(r'C:\Users\u2048873\Oxford_Data')
        self.identify_fairs()

    def identify_fairs(self):
        current_fair_tags = ['div', 'class', 'HKmvV']
        part_fair_tags = ['div', 'class', 'bhRLJp']

        contents = self.make_request(self.main_page)
        soup = bs(contents, 'html.parser')
        current_fairs = soup.find_all(current_fair_tags[0], {current_fair_tags[1]: current_fair_tags[2]})
        self.current_fairs = []
        for fair in current_fairs:
            try:
                link = fair.find('a')
                link = 'https://www.artsy.net' + link['href']
                if 'fair' in link:
                    self.current_fairs.append(link)
            except:
                pass

        past_fairs = soup.find_all(part_fair_tags[0], {part_fair_tags[1]: part_fair_tags[2]})
        self.past_fairs = []
        for fair in past_fairs:
            link = 'https://www.artsy.net' + fair['href']
            if 'fair' in link:
                self.past_fairs.append(link)

    def make_request(self, url):
        sleep(5)
        try:
            r = requests.get(url, timeout=120, headers=self.headers)
            if r.status_code == 200:
                return r.content
            else:
                print(r.status_code)
                return None
        except Exception as e:
            return None

    def extract_current_fairs(self):
        for current_fair in self.current_fairs:
            fair = Fair(current_fair, self.export_path)
            print(f'{current_fair} - Extracting')
            fair.fetch_data()

    def extract_past_fairs(self):
        for past_fair in self.past_fairs:
            fair = Fair(past_fair, self.export_path)
            print(f'{past_fair} - Extracting')
            fair.fetch_data()

    def extract_txt_fairs(self):
        self.import_fairs_from_txt()
        for txt_fair in self.txt_fairs:
            fair = Fair(txt_fair, self.export_path)
            print(f'{txt_fair} - Extracting')
            fair.fetch_data()

    def import_fairs_from_txt(self):
        self.txt_fairs = []
        file_loc = (CD / 'fairs.txt').resolve()
        with open(file_loc, 'r', encoding='utf-8') as fs:
            for line in fs:
                line = line.strip()
                fair_url = f'https://www.artsy.net{line}'
                self.txt_fairs.append(fair_url)

class Fair:
    def __init__(self, url, export_path):
        self.url = url
        self.export_file = url.split('/')[-1] + '-' + TODAY + '.csv'
        self.search_url = url + '/artworks?page='

        self.timeout = 120
        self.headers = {
                'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
            }

        self.export_path = Path(export_path, self.export_file)
        self.setup_csv(self.export_path)

        self.long_fair = self.check_long_fair()
        if self.long_fair:
            self.long_fair_tags = self.generate_long_fair_tags()

    def fetch_data(self):
        artworks = []
        print(f'{self.url} - Collecting links')
        links = self.fetch_links()
        print(f'{self.url} - Extracting Data')
        for link in links:
            content = self.make_request(link)
            if content:
                artwork = Artwork(content)
                artworks.append(artwork)
                self.export_data(self.export_path, artwork=artwork.export_to_csv())

    def fetch_links(self):
        search_tags = ['div', 'data-test', 'artworkGridItem']#
        links = []
        if self.long_fair:
            for tag in self.long_fair_tags:
                for x in range(100):
                    x = x+1
                    link = self.search_url + str(x) + tag
                    content = self.make_request(link)
                    if content:
                        soup = bs(content, 'html.parser')
                        page_links = soup.find_all(search_tags[0], {search_tags[1]: search_tags[2]})
                        if page_links:
                            for page_link in page_links:
                                link = 'https://www.artsy.net' + page_link.find('a')['href']
                                links.append(link)
                        else:
                            break
        else:
            # CHANGE BACK TO 100
            for x in range(100):
                x = x+1
                link = self.search_url + str(x)
                content = self.make_request(link)
                if content:
                    soup = bs(content, 'html.parser')
                    page_links = soup.find_all(search_tags[0], {search_tags[1]: search_tags[2]})
                    if page_links:
                        for page_link in page_links:
                            link = 'https://www.artsy.net' + page_link.find('a')['href']
                            links.append(link)
                    else:
                        break
        return set(links)

    def make_request(self, url):
        # Make sure the server does not block requests
        sleeptime = random() * 3
        sleep(sleeptime)
        r = requests.get(url, timeout=120, headers=self.headers)
        if r.status_code == 200:
            return r.content
        else:
            print(r.status_code)
            sleep(200)
            return None

    def export_data(self, exloc, artwork=None):
        if artwork:
            with open(exloc, 'a', encoding='UTF-8', newline='') as f:
                w = csv.writer(f, delimiter=';')
                w.writerow(artwork)
        else:
            with open(exloc, 'a', encoding='UTF-8', newline='') as f:
                w = csv.writer(f, delimiter=';')
                for artwork in self.artworks:
                    try:
                        if artwork.data:
                            w.writerow(artwork.export_to_csv())
                    except:
                        pass

    def setup_csv(self, exloc):
        with open(exloc, 'w', encoding='UTF-8', newline='') as f:
            w = csv.writer(f, delimiter=';')
            w.writerow(['artist', 'artist_slug', 'title', 'year', 'gallery', 'price', 'currency', 'category', 'materials', 'dimensions', 'sold', 'url', 'image_url'])

    def check_long_fair(self):
        r = requests.get(self.search_url, timeout=self.timeout, headers=self.headers)
        if r.status_code == 200:
            soup = bs(r.text, 'html.parser')
        else:
            print('Error loading fair')
            sys.exit()

        numberOfArtworks = soup.find('div', {'class': 'iAatLR'}).text
        numberOfArtworks = re.search('(\d+)', numberOfArtworks)

        try:
            numberOfArtworks = int(numberOfArtworks.group())
        except Exception as e:
            print(e)
            return False

        if numberOfArtworks > 3000:
            return True
        if numberOfArtworks <= 3000:
            return False

    def generate_long_fair_tags(self):
        arttype = [
            'painting',
            'photography',
            'sculpture',
            'prints',
            'work-on-paper',
            'design',
            'drawing',
            'installation',
            'film-slash-video',
            'jewelry',
            'performance-art'
        ]
        price = [
            'price_range=1000-5000',
            'price_range=5000-10000',
            'price_range=10000-25000',
            'price_range=25000-100000',
            'price_range=2A-1000'
        ]

        tags = []
        for art in arttype:
            for p in price:
                tags.append(f'&additional_gene_ids%5B0%5D=' + art + '&' + p)

        return tags

class Artwork:
    def __init__(self, content):
        soup = bs(content, 'html.parser')
        self.extract_data(soup)

    def extract_data(self, soup):
        json_block = self.find_json_block(soup)
        self.extract_artwork_data(json_block, soup)

    def find_json_block(self, soup):
        block = soup.find('script', {'type': 'application/ld+json'})
        try:
            json_data = json.loads(block.text)
            return json_data
        except:
            return None

    def extract_artwork_data(self, json_data, soup):
        try:
            element = json_data['brand']['name']
            self.artist = element.strip().replace('\"', '')
        except:
            self.artist = ''

        try:
            block = soup.find('div', {'data-test': 'artworkSidebar'})
            element = block.find('a', {'class': 'cCmCIs'})
            self.artist_slug = element['href']
        except:
            self.artist_slug = ''

        try:
            element = soup.find('h1', {'class': 'fiPLKL'}).text
            if ', ' in element:
                self.title = ', '.join(element.split(', ')[:-1])
            else:
                self.title = element
        except:
            self.title = ''

        try:
            self.year = json_data['productionDate']
        except:
            self.year = ''

        try:
            block = soup.find('div', {'class': 'ArtworkSidebarPartnerInfo__PartnerContainer-sc-16oykvq-0'})
            element = block.find('a')
            self.gallery = element['href']
        except:
            self.gallery = ''

        try:
            price_block = json_data['offers']
        except:
            price_block = None

        try:
            if price_block:
                self.price = price_block['price']
            else:
                self.price = ''
        except:
            try:
                if price_block:
                    lowprice = price_block['lowPrice']
                    highprice = price_block['highPrice']
                    self.price = f'{str(lowprice)}-{str(highprice)}'
                else:
                    self.price = ''
            except:
                self.price = ''

        try:
            if price_block:
                self.currency = price_block['priceCurrency']
            else:
                self.currency = ''
        except:
            self.currency = ''

        try:
            details_block = soup.find('div', {'data-test', 'artworkDetails'})
        except:
            details_block = None

        try:
            element = json_data['category']
            self.category = element

        except:
            self.category = ''

        try:
            artwork_block = soup.find('div', {'data-test': 'artworkSidebar'})
        except:
            artwork_block = None

        try:
            if artwork_block:
                element = artwork_block.find('div', {'class': 'ldlHGe'})
                self.materials = element.text
            else:
                self.materials = ''

        except:
            self.materials = ''

        try:
            width = json_data['width'].replace(' in', '')
            height = json_data['height'].replace(' in', '')
            try:
                depth = json_data['depth'].replace(' in', '')
                self.dimensions = f'{width} x {height} x {depth} in'
            except:
                self.dimensions = f'{width} x {height} in'
        except:
            self.dimensions = ''

        try:
            if self.price:
                self.sold = 'False'
            else:
                self.sold = 'True'
        except:
            self.sold = ''

        try:
            self.url = json_data['url']
        except:
            self.url = ''

        try:
            self.image_url = json_data['image']
        except:
            self.image_url = ''

    def export_to_csv(self):
        return [self.artist, self.artist_slug, self.title, self.year, self.gallery, self.price, self.currency, self.category, self.materials, self.dimensions, self.sold, self.url, self.image_url]

if __name__ == '__main__':
    main = Artsy()
    main.extract_current_fairs()
    #main.extract_past_fairs()
    #main.extract_txt_fairs()


import sys
import os
import os.path as osp
import numpy as np
import json
from sklearn.cluster import DBSCAN
import tqdm
import matplotlib.pyplot as plt
from scipy.spatial import distance
from copy import deepcopy
from cylinder_cluster import cylinder_cluster
"""
Do some work on object level
"""

def cal_precision_recall_single(labels_true, labels_pred, instance_id_true, instance_id_pred, class_label=0, targets_for_debug=None):

    """
    Parameters
    -------------
    This function deals with single frame precision and recall calculation
    labels_true: 1-d array
        The true label of the targets of one single frame
    labels_pred: 1-d array
        The predicted label of the targets of one single frame
    instance_id_true: 1-d array
        The instance ID of the targets of one single frame from SSD bounding box
    instance_id_pred: 1-d array
        The instance ID of the post-clustering output
    class_label: an int
        The label of the class to calculation. 0 for background, 1 for pedestrian, 2 for cyclist, 3 for car

    Returns
    ------------
    num_TP: an int
        The number of true positives of the class_label
    """

    num_TP_in_pred = 0
    num_TP_in_true = 0
    num_instances_pred = 0
    num_instances_true = 0
    mask_class_label_pred = labels_pred == class_label
    mask_class_label_true = labels_true == class_label
    num_instances_pred = np.unique(instance_id_pred[labels_pred==class_label]).shape[0]
    num_instances_true = np.unique(instance_id_true[labels_true==class_label]).shape[0]
    intersection_sum = 0
    union_sum = 0

    if instance_id_pred.shape[0] >0:
        instance_id_pred_max = instance_id_pred.max()
    else:
        instance_id_pred_max = 0
    if instance_id_true.shape[0] >0:
        instance_id_true_max = instance_id_true.max()
    else:
        instance_id_true_max = 0

    intersection_sum = np.sum(np.logical_and(mask_class_label_pred, mask_class_label_true))
    union_sum = np.sum(np.logical_or(mask_class_label_pred, mask_class_label_true))
    num_TP_single_target = 0
    # calculate num_TP_in_pred (for precision)
    for i in np.arange(instance_id_pred_max + 1):
        indx_pred = np.logical_and(instance_id_pred == i, mask_class_label_pred)
        for j in np.arange(instance_id_true_max + 1):
            indx_true = np.logical_and(instance_id_true == j, mask_class_label_true)
            intersection = np.sum(np.logical_and(indx_pred, indx_true))
            union = np.sum(np.logical_or(indx_pred, indx_true))
            if intersection/union >= 0.5:
                num_TP_in_pred += 1
                break

    # calculate num_TP_in_true (for recall)
    for i in np.arange(instance_id_true_max + 1):
        indx_true = np.logical_and(instance_id_true == i, mask_class_label_true)
        for j in np.arange(instance_id_pred_max + 1):
            indx_pred = np.logical_and(instance_id_pred == j, mask_class_label_pred)
            intersection = np.sum(np.logical_and(indx_pred, indx_true))
            union = np.sum(np.logical_or(indx_pred, indx_true))
            num_true = np.sum(indx_true)
            num_pred = np.sum(indx_pred)

            if intersection/union >= 0.5:
                num_TP_in_true += 1
                if np.sum(indx_true) == 1:
                    num_TP_single_target +=1
                break

    return num_TP_in_pred, num_TP_in_true, num_instances_pred, num_instances_true, intersection_sum, union_sum, num_TP_single_target

def cal_precision_recall_all(labels_true, labels_pred, instance_id_true, instance_id_pred, frame_id, num_class = 4, targets_for_debug=None):
    """
    Parameters
    -------------
    labels_true: 1-d array
        The true labels of the targets of all the frames
    labels_pared: 1-d array
        The predicted labels of the targets of all the frames
    instance_id_true:
        The instance ID of the targets of all frames from SSD bounding box
    instance_id_pred:
        The instance ID of the post-clustering output
    frame_id: 1-d array
        The frame_id of each target. The size of frame_id should be equal to labels_true and labels_pred

    Returns
    -------------
    precision: 1-d array
        the precision of each class
    recall: 1-d array
        the recall of each class

    """

    num_TP_in_pred = np.zeros(num_class)
    num_TP_in_true = np.zeros(num_class)
    intersection_sum = np.zeros(num_class)
    union_sum = np.zeros(num_class)
    num_instance_pred = np.zeros(num_class)
    num_instance_true = np.zeros(num_class)
    precision = np.zeros(num_class)
    recall = np.zeros(num_class)
    num_TP_single_target_total = 0
    pbar = tqdm.tqdm(total = frame_id.max()+1, desc='calculate precision and recall')
    for i in np.arange(0, frame_id.max()+1):
        pbar.update()
        # if i<100:
        #     continue
        labels_true_single = labels_true[frame_id==i]
        labels_pred_single = labels_pred[frame_id==i]
        instance_id_pred_single = instance_id_pred[frame_id==i]
        instance_id_true_single = instance_id_true[frame_id==i]
        for j in np.arange(1, num_class):
            num_TP_in_pred_single, num_TP_in_true_single, num_instance_pred_single, num_instance_true_single, intersection_single, union_single, num_TP_single_target = cal_precision_recall_single(labels_true_single,
                                                                                labels_pred_single,
                                                                                instance_id_true_single,
                                                                                instance_id_pred_single,
                                                                                class_label=j)
                                                                                # targets_for_debug=targets_for_debug[frame_id==i,:])
            # print("num_TP_in_pred_single",num_TP_in_pred_single)
            # print("num_TP_in_true_single", num_TP_in_true_single)

            num_TP_in_pred[j] += num_TP_in_pred_single
            num_TP_in_true[j] += num_TP_in_true_single
            num_instance_pred[j] += num_instance_pred_single
            num_instance_true[j] += num_instance_true_single
            intersection_sum[j] += intersection_single
            union_sum[j] += union_single
            num_TP_single_target_total += num_TP_single_target
    precision = num_TP_in_pred / num_instance_pred
    recall    = num_TP_in_true / num_instance_true

    return precision, recall, intersection_sum/union_sum, num_TP_single_target_total

eps_xy_list = [None, 0.5, 2 , 4]
eps_v_list = [None, 2, 1.6, 1]
min_targets_list = [None, 1, 2, 3]
def post_clustering(targets_xyv, labels_pred, frame_id, DBSCAN_eps = 1.1, DBSCAN_min_samples=1, algorithm=1, target_scores = None, filter_objects = False):
    """
    Parameters:
    ------------------
    targets_xyv: 2-d array
        The x, y coordinates and velocity of targets
    labels_pred: 1-d array
        The predicted labels of the targets of all the frames
    frame_id: 1-d array
        The frame_id of each target. The size of frame_id should be equal to labels_pred and targets_rav
    """
    color_LUT = np.array(['c','g','r','b'])
    min_scores_list = []
    post_clst_id = -1 * np.ones(targets_xyv.shape[0])
    pbar = tqdm.tqdm(total = frame_id.max()+1, desc='post_clustering')
    t = 0
    debug_mode = False
    filter_objects = True
    for i in np.arange(0, frame_id.max() + 1):
        pbar.update()
        if debug_mode and i < 3803:
            continue
        targets_xyv_single = targets_xyv[frame_id==i, :]
        labels_pred_single = labels_pred[frame_id==i]
        targets_xyv_ped = targets_xyv_single[labels_pred_single==1, :]
        targets_xyv_biker = targets_xyv_single[labels_pred_single==2, :]
        targets_xyv_car = targets_xyv_single[labels_pred_single==3, :]
        targets_scores_single = target_scores[frame_id == i, :] / np.reshape(np.linalg.norm(target_scores[frame_id == i, :] , ord=2, axis=1), (-1, 1))
        # first time
        if targets_xyv_ped.shape[0] > 0:
            post_clst_id_ped = cylinder_cluster(targets_xyv_ped[:, :3], eps_xy = eps_xy_list[1], eps_v = eps_v_list[1], min_targets=min_targets_list[1])
            max_clst_id_ped = post_clst_id_ped.max()
        else:
            post_clst_id_ped = np.array([])
            max_clst_id_ped = -1
        if targets_xyv_biker.shape[0] > 1:
            post_clst_id_biker = cylinder_cluster(targets_xyv_biker[:, :3], eps_xy = eps_xy_list[2], eps_v = eps_v_list[2], min_targets=min_targets_list[2])

            max_clst_id_biker = max_clst_id_ped + 1 + post_clst_id_biker.max()
        else:
            post_clst_id_biker = -1 * np.ones([targets_xyv_biker.shape[0]])
            max_clst_id_biker = max_clst_id_ped
        if targets_xyv_car.shape[0] > 2:
            post_clst_id_car = cylinder_cluster(targets_xyv_car[:, :3], eps_xy = eps_xy_list[3], eps_v = eps_v_list[3], min_targets=min_targets_list[3])

        else:
            post_clst_id_car = -1 * np.ones([targets_xyv_car.shape[0]])

        post_clst_id_biker[post_clst_id_biker>-1] = post_clst_id_biker[post_clst_id_biker>-1] + max_clst_id_ped+1
        post_clst_id_car[post_clst_id_car>-1] = post_clst_id_car[post_clst_id_car>-1] + max_clst_id_biker + 1

        post_clst_id_single = -1 * np.ones(np.sum(frame_id == i))
        post_clst_id_single[labels_pred_single==1] = post_clst_id_ped
        post_clst_id_single[labels_pred_single==2] = post_clst_id_biker
        post_clst_id_single[labels_pred_single==3] = post_clst_id_car
        labels_pred_single[post_clst_id_single==-1] = 0
        if debug_mode:
            plt.figure(figsize = (16, 16))
            plt.title("Frame:{}".format(i))
            ax1 = plt.subplot(221)
            ax1.set_title("cluster before refinement")
            sc1 = ax1.scatter(targets_xyv_single[:, 1], targets_xyv_single[:, 0], c = post_clst_id_single, s = 10*post_clst_id_single+7)
            plt.colorbar(sc1, ax=ax1)
            ax1.set_xlim([-25, 25])
            ax1.set_ylim([0, 40])
            ax2 = plt.subplot(222)
            ax2.set_title("labels before refinement")
            ax2.scatter(targets_xyv_single[:, 1], targets_xyv_single[:, 0], c = color_LUT[labels_pred_single])
            ax2.set_xlim([-25, 25])
            ax2.set_ylim([0, 40])

        space_threshold = 1
        speed_threshold = [0, 3, 2, 1.2]
        score_threshold = 0.6

        if post_clst_id_single.shape[0] > 0 and filter_objects:
            min_dist_mat = 10 * np.ones([int(post_clst_id_single.max() + 1), int(post_clst_id_single.max() + 1)])
            min_v_diff_mat = 10 * np.ones([int(post_clst_id_single.max() + 1), int(post_clst_id_single.max() + 1)])
            object_label_list = 5 * np.ones(int(post_clst_id_single.max() + 1))
            for k in np.arange(int(post_clst_id_single.max() + 1)):
                for l in np.arange(int(post_clst_id_single.max() + 1)):
                    if k!=l and np.sum(post_clst_id_single == k) == 0 or np.sum(post_clst_id_single == l) == 0:
                        continue
                    object_label_list[k] = labels_pred_single[post_clst_id_single==k][0]
                    min_dist_pair = distance.cdist(targets_xyv_single[post_clst_id_single == k, :2], targets_xyv_single[post_clst_id_single == l, :2]).min()
                    min_dist_mat[k, l] = min_dist_pair
                    min_v_diff_pair = distance.cdist(np.reshape(targets_xyv_single[post_clst_id_single == k, 2], (-1, 1)), np.reshape(targets_xyv_single[post_clst_id_single == l, 2], (-1, 1))).min()
                    min_v_diff_mat[k, l] = min_v_diff_pair
                    min_score_diff_pair = distance.cdist(targets_scores_single[post_clst_id_single == k, :], targets_scores_single[post_clst_id_single == l, :]).min()
                    if min_dist_pair < space_threshold:
                        label1 = labels_pred_single[post_clst_id_single == k][0]
                        label2 = labels_pred_single[post_clst_id_single == l][0]
                        if (label1 == 2 and label2 == 3) or (label1 == 3 and label2 == 2):
                            min_scores_list.append(min_score_diff_pair)
                            # print(min_scores_list)
                            if min_v_diff_pair < speed_threshold[3]:
                                num_targets1 = np.sum(post_clst_id_single == k)
                                num_targets2 = np.sum(post_clst_id_single == l)
                                if num_targets1 > num_targets2:
                                    label_refine = label1
                                else:
                                    label_refine = label2
                                if label_refine == 3 and min_score_diff_pair < score_threshold:
                                    post_clst_id_single[post_clst_id_single == k] = l
                                    labels_pred_single[post_clst_id_single == k] = label_refine
                                    labels_pred_single[post_clst_id_single == l] = label_refine
                        elif (label1 == 1 and label2 == 3) or (label1 == 3 and label2 == 1):
                            if min_v_diff_pair < speed_threshold[3]:
                                num_targets1 = np.sum(post_clst_id_single == k)
                                num_targets2 = np.sum(post_clst_id_single == l)
                                if num_targets1 > num_targets2:
                                    label_refine = label1
                                else:
                                    label_refine = label2
                                if label_refine == 3  and min_score_diff_pair < score_threshold:
                                    post_clst_id_single[post_clst_id_single == k] = l
                                    labels_pred_single[post_clst_id_single == k] = label_refine
                                    labels_pred_single[post_clst_id_single == l] = label_refine
                        elif (label1 == 1 and label2 == 2) or (label1 == 2 and label2 == 1):
                            if min_v_diff_pair < speed_threshold[2]:
                                num_targets1 = np.sum(post_clst_id_single == k)
                                num_targets2 = np.sum(post_clst_id_single == l)
                                if num_targets1 > num_targets2:
                                    label_refine = label1
                                else:
                                    label_refine = label2
                                if label_refine == 2 and min_score_diff_pair < score_threshold:
                                    post_clst_id_single[post_clst_id_single == k] = l
                                    labels_pred_single[post_clst_id_single == k] = label_refine
                                    labels_pred_single[post_clst_id_single == l] = label_refine
            num_obj_around = np.sum(np.logical_and(min_dist_mat < space_threshold, min_v_diff_mat < speed_threshold[3]), axis = 1)
            id_list_of_cars_surrounded_by_a_lot_of_bikers = np.nonzero(np.logical_and(num_obj_around>2, object_label_list == 3))
            for id_of_cars_surrounded_by_a_lot_of_bikers in id_list_of_cars_surrounded_by_a_lot_of_bikers:
                labels_pred_single[post_clst_id_single == id_of_cars_surrounded_by_a_lot_of_bikers] = 2

        # second time
        targets_xyv_ped = targets_xyv_single[labels_pred_single==1, :]
        targets_xyv_biker = targets_xyv_single[labels_pred_single==2, :]
        targets_xyv_car = targets_xyv_single[labels_pred_single==3, :]
        if targets_xyv_ped.shape[0] > 0:
            post_clst_id_ped = cylinder_cluster(targets_xyv_ped[:, :3], eps_xy = eps_xy_list[1], eps_v = eps_v_list[1], min_targets=min_targets_list[1])
            max_clst_id_ped = post_clst_id_ped.max()
        else:
            post_clst_id_ped = -1 * np.ones([targets_xyv_ped.shape[0]])
            max_clst_id_ped = -1
        if targets_xyv_biker.shape[0] > 1:
            post_clst_id_biker = cylinder_cluster(targets_xyv_biker[:, :3], eps_xy = eps_xy_list[2], eps_v = eps_v_list[2], min_targets=min_targets_list[2] )
            max_clst_id_biker = max_clst_id_ped + 1 + post_clst_id_biker.max()
        else:
            post_clst_id_biker = -1 * np.ones([targets_xyv_biker.shape[0]])
            max_clst_id_biker = max_clst_id_ped
        if targets_xyv_car.shape[0] > 2:
            post_clst_id_car = cylinder_cluster(targets_xyv_car[:, :3], eps_xy = eps_xy_list[3], eps_v = eps_v_list[3], min_targets=min_targets_list[3])

        else:
            post_clst_id_car = -1 * np.ones([targets_xyv_car.shape[0]])

        post_clst_id_biker[post_clst_id_biker>-1] = post_clst_id_biker[post_clst_id_biker>-1] + max_clst_id_ped+1
        post_clst_id_car[post_clst_id_car>-1] = post_clst_id_car[post_clst_id_car>-1] + max_clst_id_biker + 1

        post_clst_id_single = -1 * np.ones(np.sum(frame_id == i))
        post_clst_id_single[labels_pred_single==1] = post_clst_id_ped
        post_clst_id_single[labels_pred_single==2] = post_clst_id_biker
        post_clst_id_single[labels_pred_single==3] = post_clst_id_car
        labels_pred_single[post_clst_id_single==-1] = 0

        post_clst_id[frame_id==i] = post_clst_id_single
        labels_pred[frame_id==i] = labels_pred_single

    min_scores_list = np.array(min_scores_list)
    return post_clst_id, labels_pred
def cal_f1(precision, recall):

    return 2*precision*recall/(precision+recall)

if __name__ == "__main__":
    """ Test the post_clustering->precision & recall pipeline """
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    result_DIR = osp.join(BASE_DIR, 'results', 'RTCtrain_info')
    config_list = os.listdir(result_DIR)
    config_list.sort()
    RTCnet_result_info_path = osp.join(result_DIR, config_list[-1])
    speed_threshold = 0.3
    speed_threshold_to_change_label = 0
    show_RTC_result = True
    with open(RTCnet_result_info_path, 'r') as f:
        RTCnet_result_info = json.load(f)

    if show_RTC_result:
        result_path = RTCnet_result_info["result_folder"]

        data_path = RTCnet_result_info["data_path"]
        labels_pred = np.load(osp.join(result_path, "pred_labels_test.npy"))
        labels_true = np.load(osp.join(result_path, "true_labels_test.npy"))
        target_scores = np.load(osp.join(result_path, "final_score.npy"))
        frame_id = np.load(osp.join(data_path, "test", "frame_id.npy"))
        instance_id_true = np.load(osp.join(data_path, "instance_id_test.npy"))
        targets_rav = np.load(osp.join(data_path, "test", "features.npy"))[:, :3]

        targets_xyv = np.zeros(targets_rav.shape)
        targets_xyv[:,0] = targets_rav[:,0]*np.cos(targets_rav[:,1])
        targets_xyv[:,1] = targets_rav[:,0]*np.sin(targets_rav[:,1])
        targets_xyv[:,2] = targets_rav[:,2]
        targets_v = np.abs(targets_rav[:, 2])
        labels_pred[targets_v < speed_threshold_to_change_label] = 0
        labels_pred = labels_pred[targets_v > speed_threshold]
        labels_true = labels_true[targets_v > speed_threshold]
        target_scores = target_scores[targets_v > speed_threshold]
        frame_id = frame_id[targets_v > speed_threshold]
        targets_rav = targets_rav[targets_v>speed_threshold, :]
        targets_xyv = targets_xyv[targets_v > speed_threshold, :]

        instance_id_true = instance_id_true[targets_v > speed_threshold]
        instance_id_pred, labels_pred = post_clustering(targets_xyv, labels_pred, frame_id, target_scores = target_scores)
        precision_RTC, recall_RTC, IoU_RTC, num_TP_single_target_total = cal_precision_recall_all(labels_true, labels_pred, instance_id_true, instance_id_pred, frame_id, targets_for_debug=targets_xyv)

        print("precision_RTC", precision_RTC, "recall_RTC", recall_RTC)
        np.savetxt(osp.join(result_path, "precision_RTC.csv"), precision_RTC, delimiter=",")
        np.savetxt(osp.join(result_path, "recall_RTC.csv"), recall_RTC, delimiter=",")
        np.savetxt(osp.join(result_path, "F1score.csv"), cal_f1(precision_RTC, recall_RTC), delimiter=",")
        np.savetxt(osp.join(result_path, "IoU_RTC.csv"), IoU_RTC, delimiter=",")
        np.save(osp.join(result_path, "pred_labels_refine.npy"), labels_pred)
        print("number of TP object with 1 target:", num_TP_single_target_total)
        print("f1 RTC: ", cal_f1(precision_RTC, recall_RTC))
        print("IoU RTC:", IoU_RTC)


#!/usr/bin/env python
# encoding: utf-8

from api_v1 import _ApiVersion1

def api(version, **kwargs):
    if version == 1:
        return _ApiVersion1(**kwargs)
    else:
        raise ValueError("Unsupported API version: '{}'".format(version))


# !usr/bin/python3.10
# https://www.coursera.org/learn/algorithms-graphs-data-structures/

import utils
from collections import defaultdict
from draw import draw_graph
from graph import Graph
import matplotlib.pyplot as plt

class GraphSearch:

    def __init__(self, graph: dict, matrix: bool = False):
        self.graph = graph
        if matrix:
            self.adjacency_matrix = matrix
        self.BFSExplored = []
        self.DFSExplored = []
        self.k_stack = []
        self.reversed_graph = Graph()
        self.SCC = defaultdict(list)
        self.kosaraju_explored = []
        self.strong_comp = 0

    def BFS(self, start_node: int, end_node: int=0, is_shortest: bool=False) -> tuple[list[int], int] or list[int]:
        """
        Breadth-first searching algorithm
        """
        assert type(start_node) == int, f"Node type({start_node}) must be an integer"
        assert type(end_node) == int, f"Node type({end_node}) must be an integer"

        Q = [start_node]
        self.BFSExplored.append(start_node)

        while Q:
            if end_node and end_node in self.BFSExplored:
                break

            v = Q.pop(0)

            for w in self.graph[v]:
                if w not in self.BFSExplored:
                    self.BFSExplored.append(w)
                    Q.append(w)

        if is_shortest:
            return (self.BFSExplored, self.distance(end_node))
        return self.BFSExplored

    def DFS(self, start_node: int = 0, sccs=False) -> list[int]:
        """
        Depth-first search algorithm (recursive)
        """
        assert type(start_node) == int, f"Node type({start_node}) must be an integer"

        if start_node not in self.DFSExplored:
            self.DFSExplored.append(start_node)

        if not sccs:
            for i in self.graph[start_node]:
                if i not in self.DFSExplored:
                    self.DFSExplored.append(i)
                    self.DFS(i)
                self.reverse_graph(i, start_node)

                # fills the stack for Kosaraju's Algorithm
                if self.DFSExplored[-1] not in self.k_stack:
                    self.k_stack.append(self.DFSExplored[-1])

        else:

            for i in self.reversed_graph.graph[start_node]:
                if len(self.reversed_graph.graph[start_node]) == 0:

                    self.SCC[self.strong_comp].append([])
                if i not in self.kosaraju_explored:
                    self.kosaraju_explored.append(i)
                    self.SCC[self.strong_comp].append(i)
                    self.DFS(i, True)
                    self.strong_comp += 1

    def kosaraju(self):
        self.DFS(0)
        self.complete_stack()

    def complete_stack(self):
        for i in reversed(self.DFSExplored):
            if i not in self.k_stack:
                self.k_stack.append(i)

    def reverse_graph(self, a, b):
        self.reversed_graph.add_edges([(a, b)], True)

    def reverse_traverse(self):
        for i in reversed(self.k_stack):
            if i not in self.kosaraju_explored:
                self.DFS(i, True)

g = Graph()
G = g.add_edges([(0, 1), (1, 2), (2, 3), (3, 0), (2, 4), (4, 5), (5, 6), (6, 4), (6, 7)], True)
gs = GraphSearch(G)
gs.kosaraju()
gs.reverse_traverse()

points = utils.random_layout(G)
print(g.graph)
print(gs.k_stack)
print(gs.DFSExplored)
print(gs.reversed_graph.graph)
print(gs.SCC)

# draw_graph(G, points)

# print("DepthFirstSearch", gs.DFS(0))


import numpy as np
import cv2
import matplotlib.pyplot as plt

chess=cv2.imread("images\\input\\chess.jpg")# foto okundu
chess_board=cv2.imread("images\\input\\chessboard.jpg")
chess_rgb=cv2.cvtColor(chess,cv2.COLOR_BGR2RGB)#renk uzayı rgb formatına çevrildi
chess_board_rgb=cv2.cvtColor(chess_board,cv2.COLOR_BGR2RGB)#renk uzayı rgb formatına çevrildi
plt.imshow(chess)# ekrana bastırılır
plt.waitforbuttonpress()

gray_c=cv2.cvtColor(chess_rgb,cv2.COLOR_RGB2GRAY)#foto gray formata çevrildi
gray_b=cv2.cvtColor(chess_board_rgb,cv2.COLOR_RGB2GRAY)#foto gray formata çevrildi
plt.imshow(gray_c)
plt.waitforbuttonpress()

plt.imshow(chess_board)
plt.waitforbuttonpress()

gray_float=np.float32(gray_c)# fotonun pikselleri float tipine getirildi
# floata çevirme nedeni cornerharris fonksiyonunun float tipinde foto istemesidir
print(gray_float.shape)

#köşe tespiti algoritması
dst=cv2.cornerHarris(gray_float,2,17,0.04)#en sondaki sabit değeri değiştirilebilir ikini

dst=cv2.dilate(dst,None)# erozyon işlemi uygulandı

chess[dst>0.01*dst.max()]=[255,0,0]# harris fonksiyonundan çıkan görüntüden max değerlerinden yüzde bir büyük olan piksellere gerçek resim üzerinde kırmızı ile  boyanır
# ve tespit görüntülenir
plt.imshow(chess)
plt.waitforbuttonpress()

board_float=np.float32(gray_b)#fotonun piksel değerleri floata tipine geldi

dst=cv2.cornerHarris(board_float,2,29,0.04)# köşe tespiti fonksiyonu uyglandı

dst=cv2.dilate(dst,None)# erozyon işlemi uygulandı

chess_board[dst>0.01*dst.max()]=[255,0,0]# harris fonksiyonundan çıkan görüntüden max değerlerinden yüzde bir büyük olan piksellere gerçek resim üzerinde kırmızı ile  boyanır
# ve tespit görüntülenir

plt.imshow(chess_board)
plt.waitforbuttonpress()

# diğer bir algoritma

corners=cv2.goodFeaturesToTrack(gray_b,100,0.01,10)# maxcorner=5 # 3. paramtre en az uzaklık

corners=np.int0(corners)# integer formatına çevrildi

for i in corners:
    x,y=i.ravel()
    cv2.circle(chess_board_rgb,(x,y),3,(255,0,0),-1)# köşelere çember çizildi

plt.imshow(chess_board_rgb)
plt.waitforbuttonpress()


import asyncio
from random import random
from itertools import chain, islice

def get_chunks_it(l, n):
    """ Chunks an iterator `l` in size `n`
    Args:
        l (Iterator[Any]): an iterator
        n (int): size of
    Returns:
        Generator[Any]
    """
    iterator = iter(l)
    for first in iterator:
        yield chain([first], islice(iterator, n - 1))

def estimate_pi(n):
    """
        Estimates pi by throwing a point (x,y) randomly *n* times in
        the unit square and counting the number of hits where
        x^2 + Y^2 <= 1.
        Pi is then approximated as 4 * no. hits / n.
        input:
            *n* (int): The number of times to throw the point
        output:
            *estimate* (float): The estimate for pi found here
    """
    hits = sum(int(random()**2 + random()**2 <= 1) for _ in range(n))
    estimate = 4 * hits / n
    return estimate

async def estimate_pi_async(n):
    """
        Estimates pi by throwing a point (x,y) randomly *n* times in
        the unit square and counting the number of hits where
        x^2 + Y^2 <= 1.
        Pi is then approximated as 4 * no. hits / n.
        input:
            *n* (int): The number of times to throw the point
        output:
            *estimate* (float): The estimate for pi found here

        **Note:**
            This is an asynchronous implementation that throws
            100 points before awaiting to relinquish control.
    """
    hits = 0
    for chunk in get_chunks_it(range(n), 100):
        await asyncio.sleep(0)  # Relinquish control so something else can run
        hits += sum(int(random()**2 + random()**2 <= 1) for _ in chunk)
    estimate = 4 * hits / n
    return estimate

31380 Intelligent Systems/python projects/Week2D1/comm_exercise/rpc/util.py


from odoo import models, api, fields

class SomeModel(models.Model):
    _name = 'some.model'

    @api.model
    def add_contact(self, partner, contacts):
        partner.ensure_one()
        if contacts:
            partner.date = fields.Date.context_today(self)
            partner.child_ids |= contacts

    @api.model
    def add_contacts_option2(self, partner, contacts):
        partner.ensure_one()
        if contacts:
            today = fields.Date.context_today(self)
            partner.update(
                {'date': today,
                 'child_ids': partner.child_ids | contacts}
            )


from pygame import*
mixer.init()

class Game_sprite(sprite.Sprite):
    def __init__(self, imagename, width, height, x, y):
        self.image = transform.scale(image.load(imagename),(width, height))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class Player(Game_sprite):
    def __init__(self):
        super().__init__("hero.png", 50, 50, 50, 420)

    def update(self,screen):
        pressed_keys= key.get_pressed()
        if pressed_keys[K_a] and self.rect.x > 0:
            self.rect.x -= 10
        if pressed_keys[K_w] and self.rect.y > 0:
            self.rect.y -= 10
        if pressed_keys[K_d] and self.rect.x < 650:
            self.rect.x += 10
        if pressed_keys[K_s] and self.rect.y < 450:
            self.rect.y += 10

        screen.blit(self.image,self.rect)

    def is_collide(self,enemy):
        if self.rect.colliderect(enemy.rect):
            return True
        return False

class Wall(sprite.Sprite):
    def __init__(self,x,y,w,h):
        super().__init__()
        self.image = Surface((w,h))
        self.rect = Rect(x,y,w,h)
        self.image.fill((255,255,255))

    def update(self, screen):
        screen.blit(self.image,self.rect)

class Enemy(Game_sprite):
    def __init__(self):
        super().__init__("cyborg.png",50,50,460,260)
        self.speed = 3
    def update(self, screen):
        if self.rect.x >= 500:
            self.speed *= -1
        if self.rect.x <= 100:
            self.speed *= -1
        self.rect.x += self.speed

        screen.blit(self.image,self.rect)

window = display.set_mode((700,500))
display.set_caption('Лабиринт')

background = transform.scale(image.load("background.jpg"),(700,500))
win = transform.scale(image.load("aga.jpg"),(700,500))
player = Player()
gold = Game_sprite('treasure.png',250,250,450,200)
lox = transform.scale(image.load("aga1.jpg"),(700,500))
enemy = Enemy()
wall1 = Wall (20,80,20,250)
wall2 = Wall (100,180,20,270)
wall3 = Wall (20,80,350,20)
wall4 = Wall (100,180,350,20)
wall5 = Wall (370,80,300,20)
wall6 = Wall (670,80,20,200)
wall7 = Wall (450,180,20,100)
wall8 = Wall (20,80,20,350)

walls = sprite.Group()
walls.add(wall1)
walls.add(wall2)
walls.add(wall3)
walls.add(wall4)
walls.add(wall5)
walls.add(wall6)
walls.add(wall7)
walls.add(wall8)

mixer.music.load("jungles.ogg")
kick = mixer.Sound('kick.ogg')
mixer.music.play()

clock = time.Clock()
game = True

while game:
    clock.tick(60)
    for e in event.get():
        if e.type == QUIT:
            kick.play()
            game = False
    if player.is_collide(enemy):
        window.blit(lox, (0,0))
    if sprite.spritecollideany(player,walls):
        player.rect.x = 60
        player.rect.y = 420

    window.blit(background,(0,0))
    player.update(window)
    enemy.update(window)
    wall1.update(window)
    wall2.update(window)
    wall3.update(window)
    wall3.update(window)
    wall4.update(window)
    wall5.update(window)
    wall6.update(window)
    wall7.update(window)
    wall8.update(window)

    if player.is_collide(enemy):
        window.blit(lox, (0,0))
    if player.is_collide(gold):
        window.blit(win, (0,0))

    window.blit(gold.image, gold.rect)

    display.update()


# 그리디_ATM
# 줄을 서 있는 사람의 수 N과 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어졌을 때, 각 사람이 돈을 인출하는데 필요한 시간의 합의 최솟값을 구하는 프로그램
n = int(input())
p = list(map(int, input().split()))

# 1. 걸리는 시간 순으로 정렬 (오름차순)
p.sort()

# 2. 돈 인출하는 데 걸리는 시간 계산
answer = 0
previous = 0
for i in range(n):
    previous += p[i]
    answer += previous

print(answer)


from ursina import *

def update():
    if held_keys["a"]:
        pika.x -= 4.1 * time.dt
    elif held_keys["d"]:
        pika.x += 4.1 * time.dt
    if held_keys["w"]:
        pika.y += 4.1 * time.dt
    elif held_keys["s"]:
        pika.y -= 4.1 * time.dt

app = Ursina()

# test_square = Entity(model = 'quad', color = color.red, scale = (1, 1), position = (1, 1))
pika_texture = load_texture("pika.png")
pika = Entity(model="quad", texture=pika_texture, scale=(2, 2))

app.run()


# Created on Heesung의 iPad.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(BCM)

servo = 16
GPIO.setup(servo, GPIO.OUT)

pwm = GPIO.PWM(servo, 50)
pwm.start(3.0)

try:
    while True:
        userInput = input("q: 0DEG, w: 90DEG, e: 180DEG\n")
        print(userInput)

        if userInput == 'q':
            pwm.ChangeDutyCycle(3.0)
        elif userInput == 'w':
            pwm.ChangeDutyCycle(7.75)
        elif userInput == 'e':
            pwm.ChangeDutyCycle(12.5)

        time.sleep(1)
        pwm.ChangeDutyCycle(0.0)
except KeyboardInterrupt:
    pass

pwm.stop()
GPIO.cleanup()


import os
import tensorflow as tf
from src import WaldoNet

_MOMENTUM = 0.9
_LEARNING_RATE = 0.1
_BATCH_SIZE = 4
_ITERATIONS = 10000
_MODEL_DIR = r"E:\COS 429\FinalProj\Model7"
_DATA_DIR = r"E:\COS 429\FinalProj\TheNewIms"
_DATA_FILE = r"E:\COS 429\FinalProj\imagesForTraining.txt"

def getFileNames(dir, locations):
    fnames = []
    for file in os.listdir(dir):
        fnames.append((_DATA_DIR+'/'+file, int(locations[file][0]), int(locations[file][1])))
    return fnames

def decodeImage(filename, label, len):
    image = tf.image.decode_png(tf.read_file(filename), channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    return image, tf.one_hot(label, len)

def train_input_fn(locations):
    f = getFileNames(_DATA_DIR, locations)
    fnames = [x[0] for x in f]
    labels = [((x[1]//500)//50)*20+(x[1]%500)//50 for x in f]
    lens = [x[2]//2500 for x in f]
    dataset = tf.data.Dataset.from_tensor_slices((fnames, labels, lens))
    dataset.shuffle(buffer_size=1024)
    dataset = dataset.map(decodeImage)
    dataset = dataset.prefetch(4*_BATCH_SIZE)
    dataset = dataset.batch(_BATCH_SIZE)
    iterator = dataset.make_one_shot_iterator()
    images, labels = iterator.get_next()
    return images, labels

def model_fn(features, labels, mode):
    logits = WaldoNet.inference(x=features, num_classes=100, batch_size=_BATCH_SIZE)
    predictions = {
        'classes': tf.argmax(logits, axis=1),
        'probabilities': tf.nn.softmax(logits, name='prediction_probability')
    }
    _loss = WaldoNet.loss(logits, labels)
    if mode == tf.estimator.ModeKeys.TRAIN:
        global_step = tf.train.get_or_create_global_step()
        learning_rate = tf.train.exponential_decay(_LEARNING_RATE, global_step=global_step, decay_steps=5000, decay_rate=0.9, name='learning_rate')
        tf.summary.scalar('learning_rate', learning_rate)
        optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=_MOMENTUM)
        train_op = optimizer.minimize(_loss, global_step)
    else:
        train_op = None
    accuracy = tf.metrics.accuracy(
        tf.argmax(labels, axis=1), predictions['classes'])
    metrics = {'accuracy': accuracy}
    tf.identity(accuracy[1], name='train_accuracy')
    tf.summary.scalar('train_accuracy', accuracy[1])
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=predictions,
        loss=_loss,
        train_op=train_op,
        eval_metric_ops=metrics)

def main(unused_argv):

    filedict = {}
    with open(_DATA_FILE) as f:
        for line in f:
            s = line.split(",")
            filedict[s[0]] = (int(s[1]), int(s[2]))
    run_config = tf.estimator.RunConfig().replace(save_checkpoints_steps=500, save_summary_steps=50, model_dir=_MODEL_DIR)
    waldo_finder = tf.estimator.Estimator(model_fn=model_fn, model_dir=_MODEL_DIR, config=run_config)
    tensors_to_log = {
        'learning_rate': 'learning_rate',
        'cross_entropy': 'cross_entropy',
        'train_accuracy': 'train_accuracy'
    }

    logging_hook = tf.train.LoggingTensorHook(
        tensors=tensors_to_log, every_n_iter=100)
    waldo_finder.train(input_fn=lambda: train_input_fn(filedict), steps=_ITERATIONS, hooks=[logging_hook])

if __name__ == "__main__":
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run()