import torch
import numpy as np
import torchvision.transforms as transforms

from .auto_augment_tf import (
    auto_augment_policy,
    AutoAugment,
)

IMAGENET_PIXEL_MEAN = [123.675, 116.280, 103.530]
IMAGENET_PIXEL_STD = [58.395, 57.12, 57.375]

def get_data_transform(is_training, augment='default', train_crop_size=224, test_scale=256, test_crop_size=224, interpolation='bicubic', auto_augment_policy='v0'):
    da_args = {
        'train_crop_size': train_crop_size,
        'test_scale': test_scale,
        'test_crop_size': test_crop_size,
        'interpolation': transforms.InterpolationMode.BICUBIC if interpolation == 'bicubic' else transforms.InterpolationMode.BILINEAR
    }

    if augment == 'default':
        return build_default_transform(is_training, **da_args)
    elif augment == 'auto_augment_tf':
        return build_imagenet_auto_augment_tf_transform(is_training, policy=auto_augment_policy, **da_args)
    else:
        raise ValueError(augment)

def get_normalize():
    normalize = transforms.Normalize(
        mean=torch.Tensor(IMAGENET_PIXEL_MEAN) / 255.0,
        std=torch.Tensor(IMAGENET_PIXEL_STD) / 255.0,
    )
    return normalize

def build_default_transform(
    is_training, train_crop_size=224, test_scale=256, test_crop_size=224, interpolation=transforms.InterpolationMode.BICUBIC
):
    normalize = get_normalize()
    if is_training:
        ret = transforms.Compose(
            [
                transforms.RandomResizedCrop(train_crop_size, interpolation=interpolation),
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                normalize,
            ]
        )
    else:
        ret = transforms.Compose(
            [
                transforms.Resize(test_scale, interpolation=interpolation),
                transforms.CenterCrop(test_crop_size),
                transforms.ToTensor(),
                normalize,
            ]
        )
    return ret

def build_imagenet_auto_augment_tf_transform(
    is_training, policy='v0', train_crop_size=224, test_scale=256, test_crop_size=224, interpolation=transforms.InterpolationMode.BICUBIC
):

    normalize = get_normalize()
    img_size = train_crop_size
    aa_params = {
        "translate_const": int(img_size * 0.45),
        "img_mean": tuple(round(x) for x in IMAGENET_PIXEL_MEAN),
    }

    aa_policy = AutoAugment(auto_augment_policy(policy, aa_params))

    if is_training:
        ret = transforms.Compose(
            [
                transforms.RandomResizedCrop(train_crop_size, interpolation=interpolation),
                transforms.RandomHorizontalFlip(),
                aa_policy,
                transforms.ToTensor(),
                normalize,
            ]
        )
    else:
        ret = transforms.Compose(
            [
                transforms.Resize(test_scale, interpolation=interpolation),
                transforms.CenterCrop(test_crop_size),
                transforms.ToTensor(),
                normalize,
            ]
        )
    return ret


from tkinter import *

root = Tk()

photo = PhotoImage(file="bg.gif")
theLabel = Label(root,
                 text="tkinter",
                 justify=LEFT,      # 左对齐
                 image=photo,       # 背景图
                 compound=CENTER,   # 字体在图片正上方
                 font=("simsun.ttc", 20),
                 fg="white"         # 前景色，也就是字体色
                 )
theLabel.pack()

mainloop()


def solution(S, P, Q):
    dic = {'A': 1, 'C': 2, 'G': 3, 'T': 4}
    count = [0] * 4
    cur = []
    for i in S:
        count[dic[i] - 1] += 1
        tmp_count = count[:]
        cur.append(tmp_count)
    res = []
    for i in range(len(P)):
        p, q = P[i], Q[i]
        if p != 0:
            tmp = [b - a for a, b in zip(cur[p - 1], cur[q])]
            for idx, j in enumerate(tmp, 1):
                if j > 0:
                    res.append(idx)
                    break
        else:
            for idx, j in enumerate(cur[q], 1):
                if j > 0:
                    res.append(idx)
                    break
    return res

S, P, Q = ('CAGCCTA', [2, 5, 0], [4, 5, 6])
print(solution(S, P, Q))


import numpy as np

def GetInput(filename):
    with open(filename) as f:
        line = f.readlines()
    input = [int(str) for str in line[0].split(',')]
    return input

input=GetInput('input6.txt')
counts=np.zeros(9).astype(np.int64)
for i in input:
    counts[i]+=1
print(counts, counts.sum())
for iter in range(256):
    zeros=counts[0]
    counts[0]=counts[1]
    counts[1]=counts[2]
    counts[2]=counts[3]
    counts[3]=counts[4]
    counts[4]=counts[5]
    counts[5]=counts[6]
    counts[6]=zeros #reset after spawn
    counts[6]+=counts[7]
    counts[7]=counts[8]
    counts[8]=zeros
print(counts, counts.sum())


import math

import rospy
import tf2_ros

from simulation.utils.geometry import Pose, Transform, Vector
from simulation.utils.road.sections import CustomSection
from simulation.utils.ros_base.node_base import NodeBase

class ScanNode(NodeBase):
    """ROS node providing services to access the road's groundtruth."""

    def __init__(self, name="scan_node", log_level=rospy.INFO):
        super().__init__(name=name, log_level=log_level)

        self.section = CustomSection()
        self.run(function=self.update_middle_line, rate=self.param.rate)

    def start(self):
        self.listener = tf2_ros.Buffer()
        tf2_ros.TransformListener(self.listener)

    def stop(self):
        self.save_section()

    def save_section(self):
        """Save the current custom section to a file."""
        if len(self.section.middle_line_points) > 1:
            # Adjust initial position to zero.
            initial_pose = Pose(self.section.middle_line.interpolate_pose(0))
            tf = Transform(initial_pose.position, initial_pose.orientation).inverse
            self.section.middle_line_points = [
                tf * p for p in self.section.middle_line_points
            ]
        self.section.save_as_yaml(self.param.file_path)

    def get_current_pose(self):
        """Try to get the current pose from /tf."""
        try:
            tf_transform = self.listener.lookup_transform(
                "odom", "vehicle", rospy.Time.now(), timeout=rospy.Duration(0.01)
            )
            return Pose(tf_transform.transform)
        except Exception as e:
            rospy.logerr(f"Could not lookup transform {e}")
            return

    def update_middle_line(self):
        """Update the middle line using the current position."""

        current_pose = self.get_current_pose()
        if current_pose is None:
            return

        middle_line_position = current_pose.position + Vector(0, 0.2).rotated(
            current_pose.orientation
        ).rotated(math.pi / 2)

        if (
            len(self.section.middle_line_points) == 0
            or middle_line_position.distance(self.section.middle_line_points[-1])
            > self.param.min_point_distance
        ):
            self.section.middle_line_points.append(middle_line_position)
            rospy.logdebug(f"Add new point: {middle_line_position}")


first = "XueShao"
last = "Chen"

info = f"My name is {first.upper()} {last.lower()}"
print(info)

person = {"name": "Chen", "age": 30}
print(f"My name is {person['name']} and I am {person['age']}")

for n in range(15):
    print(f"Value is {n:03}")

from datetime import datetime

bday = datetime(2018, 3, 15)
print(f"Birthday is {bday:%b %d, %Y}")


#!/usr/bin/env python3
"""
This test script shows that you can kreate komponents from a python script.
The komponents will still be configured by the konfig.strukture.
You can also configure them further in python.
In general it is preferred to kreate all komponents from the strukture.
"""

import kreate.kube
import kreate.kube.resource
from kreate.kube import KubeCli, KustApp

class DemoScriptApp(KustApp):
    def kreate_komponents(self) -> None:
        kreate.kube.resource.Ingress(self, "root")
        kreate.kube.resource.Ingress(self, "api")

        kreate.kube.resource.Egress(self, "db")
        kreate.kube.resource.Egress(self, "redis")
        kreate.kube.resource.Egress(self, "xyz")

        depl = kreate.kube.resource.Deployment(self)
        self.kreate_komponent("Service", "main")
        self.kreate_komponent("Service", "https")
        self.kreate_komponent("PodDisruptionBudget", "main")
        kreate.kube.Kustomization(self)
        self.kreate_komponent("Secret", "main")
        self.kreate_komponent("Secret", "secret-files")
        self.kreate_komponent("ServiceAccount")
        self.kreate_komponent("ServiceMonitor")
        self.kreate_komponent("HorizontalPodAutoscaler")
        self.kreate_komponent("MyUdpService", "main")
        self.kreate_komponent("CronJob", "main")
        self.kreate_komponent("StatefulSet", "main")
        self.kreate_patch(self.deployment.main, "HttpProbes")
        self.kreate_patch(depl, "AntiAffinity")
        # Add the next two in alphabetical order, to be predictable
        self.kreate_patch(depl, "VolumeMounts", "demo-extra-files")
        self.kreate_patch(depl, "VolumeMounts", "demo-files")
        self.kreate_patch(depl, "VolumeMounts", "demo-secret-files")
        self.kreate_patch(depl, "KubernetesAnnotations")
        self.kreate_patch(depl, "ElasticLogging")
        self.kreate_patch(depl, "EgressLabels")
        self.kreate_patch(self.statefulset.main, "KubernetesAnnotations")
        self.kreate_patch(self.statefulset.main, "ElasticLogging")
        self.aktivate_komponents()

    def tune_komponents(self) -> None:
        self.ingress.root.sticky()
        self.ingress.root.whitelist("10.20.30.40")
        self.ingress.root.basic_auth()
        self.ingress.root.label("dummy", "jan")
        pdb = self.poddisruptionbudget.main
        pdb.yaml.spec.minAvailable = 2
        pdb.label("testje", "test")

KubeCli(app_class=DemoScriptApp).run()


import sys
all = []
def solution(n,r,avg,lista, listb):
    nn = n*avg
    rest = nn - sum(lista)
    if rest <=0:
        return 0
    num =0
    j = sorted(range(n),key = lambda k:listb[k])
    for u in j :
        if(lista[u]<r and rest<=r-lista[u]):
            num = num + listb[u]*rest
            return num
        else:
            num = num + listb[u]*(r - listb[u])
            rest = rest - (r - lista[u])

for line in sys.stdin:
    all.append(line.strip())

i = 0
while i < len(all):
    n,r,avg = [int(x) for x in all[i].split(' ')]
    i +=1
    j=0
    A = []
    B = []
    while j<n:
        a,b =[int(x) for x in all[i].split(' ')]
        A.append(a)
        B.append(b)
        i +=1
        j +=1
    print(solution(n,r,avg,A,B))


import random
answer = random.randint(1 , 100)
guess = int (input ('choose between 1 to 100  :') )
while answer != guess :
    if answer > guess :
        print ('answer is more ! ')
    else :
        print (' answer is less !')
    guess = int (input (''))
print ('congrats !!')

python simple exercises and basic projects/guessnumber.py


import random
sinir = input("Oyunu kaç kez oynamak istediğinizi giriniz: ") # Kullanıcı oyunu kaç kez oynamak istediğini belirler
while sinir.isdigit() == False: # Kullanıcı sınırı sayıyla ifade etmek zorundadır.
    sinir = input("Lütfen bir sayıyla ifade ediniz: ")
sinir = int(sinir) # While döngüsünde kullanabilmemiz için int'e çevirmemiz gerekir.
sayac = 0
dogruyer = 0
yanlisyer = 0
while sayac < sinir:
    if sayac == 0: # Her seferinde şartları hatırlatmasına gerek yok diye düşündüğümden farklılık olsun diye diğer girdi inputları farklı bir cümleyle veriyi istiyor
        girdi = input("Lütfen iki basamaklı ve her iki basamağı da farklı rakamlardan oluşan bir sayı seçiniz: ")
    else:
        girdi = input("\nŞimdi tekrar bir sayı giriniz: ")
    if girdi.isdigit() == True and len(girdi) == 2 and girdi[0] != 0 and girdi[0] != girdi[1]: # Girdinin istenen tüm özellikleri bunlar
        sayi = str(random.randint(10,98))
        while sayi[0] == sayi[1]: # Basamakların aynı olması durumu
            sayi = str(random.randint(10,98))
        print("Programın seçtiği sayı: ",sayi) # Programın doğru çalışıp çalışmadığını kontrol etmek için var
        if girdi[0] == sayi[0]:
            dogruyer += 1
        elif girdi[-1] == sayi[-1]:
            dogruyer += 1
        elif girdi[0] == sayi[-1]:
            yanlisyer += -1
        elif girdi[-1] == sayi[0]:
            yanlisyer += -1
        sayac += 1 # Kullanıcı sayıyı yanlış girdiğinde sayaç devreye girmez yani sınırın sayacı ancak doğru veriyle çalışır.
    else:
        girdi = input("Lütfen belirtilen şartlara uygun bir sayı giriniz: ")
print("\nDoğruyer sayacının değeri: ",dogruyer)
print("Yanlışyer sayacının değeri: ",yanlisyer)


from datetime import date, datetime
from pytz import utc, timezone

from odoo.addons.resource.models.utils import Intervals, sum_intervals
from odoo.fields import Date

from .common import TestContractCommon

class TestResource(TestContractCommon):

    @classmethod
    def setUpClass(cls):
        super(TestResource, cls).setUpClass()
        cls.calendar_richard = cls.env['resource.calendar'].create({'name': 'Calendar of Richard'})
        cls.employee.resource_calendar_id = cls.calendar_richard

        cls.calendar_35h = cls.env['resource.calendar'].create({
            'name': '35h calendar',
            'attendance_ids': [
                (0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
                (0, 0, {'name': 'Monday Lunch', 'dayofweek': '0', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
                (0, 0, {'name': 'Monday Evening', 'dayofweek': '0', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
                (0, 0, {'name': 'Tuesday Morning', 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
                (0, 0, {'name': 'Tuesday Lunch', 'dayofweek': '1', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
                (0, 0, {'name': 'Tuesday Evening', 'dayofweek': '1', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
                (0, 0, {'name': 'Wednesday Morning', 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
                (0, 0, {'name': 'Wednesday Lunch', 'dayofweek': '2', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
                (0, 0, {'name': 'Wednesday Evening', 'dayofweek': '2', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
                (0, 0, {'name': 'Thursday Morning', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
                (0, 0, {'name': 'Thursday Lunch', 'dayofweek': '3', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
                (0, 0, {'name': 'Thursday Evening', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'}),
                (0, 0, {'name': 'Friday Morning', 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
                (0, 0, {'name': 'Friday Lunch', 'dayofweek': '4', 'hour_from': 12, 'hour_to': 13, 'day_period': 'lunch'}),
                (0, 0, {'name': 'Friday Evening', 'dayofweek': '4', 'hour_from': 13, 'hour_to': 16, 'day_period': 'afternoon'})
            ],
        })

        cls.contract_cdd = cls.env['hr.contract'].create({
            'date_start': Date.to_date('2021-09-01'),
            'date_end': Date.to_date('2021-10-31'),
            'name': 'First CDD Contract for Richard',
            'resource_calendar_id': cls.calendar_35h.id,
            'wage': 5000.0,
            'employee_id': cls.employee.id,
            'state': 'open',
        })
        cls.contract_cdi = cls.env['hr.contract'].create({
            'date_start': Date.to_date('2021-11-01'),
            'name': 'CDI Contract for Richard',
            'resource_calendar_id': cls.calendar_richard.id,
            'wage': 5000.0,
            'employee_id': cls.employee.id,
            'state': 'draft',
            'kanban_state': 'done',
        })

    def test_calendars_validity_within_period(self):
        tz = timezone(self.employee.tz)
        calendars = self.employee.resource_id._get_calendars_validity_within_period(
            tz.localize(datetime(2021, 10, 1, 0, 0, 0)),
            tz.localize(datetime(2021, 12, 1, 0, 0, 0)),
        )
        interval_35h = Intervals([(
            tz.localize(datetime(2021, 10, 1, 0, 0, 0)),
            tz.localize(datetime.combine(date(2021, 10, 31), datetime.max.time())),
            self.env['resource.calendar.attendance']
        )])
        interval_40h = Intervals([(
            tz.localize(datetime(2021, 11, 1, 0, 0, 0)),
            tz.localize(datetime(2021, 12, 1, 0, 0, 0)),
            self.env['resource.calendar.attendance']
        )])

        self.assertEqual(1, len(calendars), "The dict returned by calendars validity should only have 1 entry")
        self.assertEqual(2, len(calendars[self.employee.resource_id.id]), "Jean should only have one calendar")
        richard_entries = calendars[self.employee.resource_id.id]
        for calendar in richard_entries:
            self.assertTrue(calendar in (self.calendar_35h | self.calendar_richard), "Each calendar should be listed")
            if calendar == self.calendar_35h:
                self.assertFalse(richard_entries[calendar] - interval_35h, "Interval 35h should cover all calendar 35h validity")
                self.assertFalse(interval_35h - richard_entries[calendar], "Calendar 35h validity should cover all interval 35h")
            elif calendar == self.calendar_richard:
                self.assertFalse(richard_entries[calendar] - interval_40h, "Interval 40h should cover all calendar 40h validity")
                self.assertFalse(interval_40h - richard_entries[calendar], "Calendar 40h validity should cover all interval 40h")

    def test_queries(self):
        employees_test = self.env['hr.employee'].create([{
            'name': 'Employee ' + str(i),
        } for i in range(0, 50)])
        for emp in employees_test:
            new_contract = self.contract_cdd.copy()
            new_contract.employee_id = emp
            new_contract.state = 'open'
            new_contract = self.contract_cdi.copy()
            new_contract.employee_id = emp
            new_contract.state = 'draft'
            new_contract.kanban_state = 'done'

        start = utc.localize(datetime(2021, 9, 1, 0, 0, 0))
        end = utc.localize(datetime(2021, 11, 30, 23, 59, 59))
        with self.assertQueryCount(15):
            work_intervals, _ = (employees_test | self.employee).resource_id._get_valid_work_intervals(start, end)

        self.assertEqual(len(work_intervals), 51)

    def test_get_valid_work_intervals(self):
        start = timezone(self.employee.tz).localize(datetime(2021, 10, 24, 2, 0, 0))
        end = timezone(self.employee.tz).localize(datetime(2021, 11, 6, 23, 59, 59))
        work_intervals, _ = self.employee.resource_id._get_valid_work_intervals(start, end)
        sum_work_intervals = sum_intervals(work_intervals[self.employee.resource_id.id])
        self.assertEqual(75, sum_work_intervals, "Sum of the work intervals for the employee should be 35h+40h = 75h")


import numpy as np
import cv2
import os
import features

# OS Paths to the different template image folders
root = os.path.abspath(os.getcwd())

path_to_data = os.path.join(root, "Template-image/pause")
imgsPause = os.listdir(path_to_data)

path_to_data = os.path.join(root, "Template-image/volumeup")
imgsVolumeup = os.listdir(path_to_data)

path_to_data = os.path.join(root, "Template-image/volumedown")
imgsVolumedown = os.listdir(path_to_data)

path_to_data = os.path.join(root, "Template-image/songnext")
imgsSongnext = os.listdir(path_to_data)

path_to_data = os.path.join(root, "Template-image/songprevious")
imgsSongprevious = os.listdir(path_to_data)

path_to_data = os.path.join(root, "Template-image/default")
imgsDefault = os.listdir(path_to_data)

# Print amount of data loaded
print("## DATA ASSETS ## \n Up: "+str(len(imgsVolumeup))+" Down: "+str(len(imgsVolumedown))+" Next: "+str(len(imgsSongnext))+" Previous: "+str(len(imgsSongprevious))+" Pause: "+str(len(imgsPause))+" Default: "+str(len(imgsDefault)))

# "5" is a placeholder number, but don't change it to 0-4 by default, as they are template values.
currentGesture = 5
templateHolder = [[],[],[],[],[],[]]
dirHoldForloop = [imgsPause,imgsVolumeup,imgsVolumedown,imgsSongnext,imgsSongprevious,imgsDefault]
strHoldForloop = ["pause","volumeup","volumedown","songnext","songprevious","default"]

for i in range (len(strHoldForloop)):
    for x in range(len(dirHoldForloop[i])):
        template = cv2.imread("Template-image/"+strHoldForloop[i]+"/"+str(x)+".png",0)
        templateHolder[i].append(template)

def calculateData(dataset, iteration):
    ratioLR, amountFarPoints, ratioBounding, circularity = 0,0,0,0
    hull = []

    cv2.imshow('image', templateHolder[dataset][iteration])
    threshold = templateHolder[dataset][iteration]

    print("Dataset:",dataset,"iteration",iteration)
    (im2, contours, hierachy) = cv2.findContours(templateHolder[dataset][iteration], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    drawing = np.zeros((templateHolder[dataset][iteration].shape[0], templateHolder[dataset][iteration].shape[1], 3), np.uint8)

    for i in range(len(contours)):
        color_contours = (0, 0, 255)
        color = (255, 0, 0)
        cv2.drawContours(drawing, contours, i, color_contours, 1, 8)
        if len(contours) > 0:
            maxContour = max(contours, key=cv2.contourArea)

    for i in range(2):
        hull.append(cv2.convexHull(maxContour))
        cv2.drawContours(drawing, hull, i, color, 1, 8)
        normArray = np.array(hull, dtype=object)

        centerHandY, centerHandX, smallX, bigX, smallY, bigY, smallXY, bigXY, smallYX, bigYX = features.calculateCenter(normArray[i])
        imageLcount, imageRcount, ratioLR = features.calcPixelsLeftRight(threshold, centerHandY)
        perimeter, circularity = features.calculatePeriCirc(maxContour)

        yDist = bigY - smallY
        xDist = bigX - smallX
        thresholdResize = threshold[smallX:bigX, smallY:bigY]
        array = np.array(thresholdResize)
        whitePixel, blackpixels, ratioBounding = features.boundingbox(array, yDist, xDist)

        cv2.line(drawing, pt1=(smallXY, smallX), pt2=(centerHandY, centerHandX), color=(0, 255, 0), thickness=1)
        cv2.line(drawing, pt1=(bigXY, bigX), pt2=(centerHandY, centerHandX), color=(0, 255, 0), thickness=1)
        cv2.line(drawing, pt1=(smallY, smallYX), pt2=(centerHandY, centerHandX), color=(0, 255, 0), thickness=1)
        cv2.line(drawing, pt1=(bigY, bigYX), pt2=(centerHandY, centerHandX), color=(0, 255, 0), thickness=1)

        amountFarPoints = features.convexity(maxContour)

    cv2.imshow("Convex", drawing)
    return ratioLR, amountFarPoints,ratioBounding,circularity

def main():
    # Function reading and writing to text documents made with help from: https://www.geeksforgeeks.org/reading-writing-text-files-python/
    # For-loop with a length of all hand gestures
    for m in range (len(templateHolder)):
        if m == 0: fileData = open("Template-image\dataset_pause.txt","w")
        if m == 1: fileData = open("Template-image\dataset_volumeup.txt","w")
        if m == 2: fileData = open("Template-image\dataset_volumedown.txt","w")
        if m == 3: fileData = open("Template-image\dataset_songnext.txt","w")
        if m == 4: fileData = open("Template-image\dataset_songprevious.txt","w")
        if m == 5: fileData = open("Template-image\dataset_default.txt","w")

        # For-loop with a length of all the templates within each hand gesture
        for p in range (len(templateHolder[m])):
            # Loads all the feature methods and receives the data for each feature
            dataOutput = calculateData(m, p)
            for a in range (len(dataOutput)):
                try:
                    dataWrite = (str(round(dataOutput[a], 2))+" ")
                except TypeError:
                    dataWrite = dataOutput[a]

                fileData.write(dataWrite)
                if a == len(dataOutput)-1:
                    fileData.write("\n")
            # cv2.waitKey(0) # Used for manual analyzing each individual hand gesture from the template folder
        fileData.close()

main()

Image processing/Hand gesture detection/Lukas - CreateTemplateData.py


import sys

input = sys.stdin.readline
n = int(input())
trees = {}
for i in range(n):
    root, left, right = input().strip().split()
    trees[root] = [left, right]

def preorder(start):
    if start != ".":
        print(start, end="")
        preorder(trees[start][0])
        preorder(trees[start][1])

def medianOrder(start):
    if start != ".":
        medianOrder(trees[start][0])
        print(start, end="")
        medianOrder(trees[start][1])

def lastOrder(start):
    if start != ".":
        lastOrder(trees[start][0])
        lastOrder(trees[start][1])
        print(start, end="")

preorder("A")
print()
medianOrder("A")
print()
lastOrder("A")


from neuralnet import NeuralNetMLP
from sklearn.model_selection import train_test_split
from load import load_mnist

import matplotlib.pyplot as plt
import numpy as np

def main():
    images, labels, rows, cols = load_mnist('../')
    x_train, x_test, y_train, y_test = train_test_split(
        images, labels, test_size=0.2)
    nn = NeuralNetMLP(n_output=10,
                      n_features=x_train.shape[1],
                      n_hidden=50,
                      epochs=10,
                      minibatches=10)
    nn.fit(x_train, y_train)
    y_train_pred = nn.predict(x_train)
    fig, ax = plt.subplots(4, 4)
    fig.canvas.manager.window.attributes('-topmost', 1)
    ax = ax.flatten()
    for i in range(16):
        ax[i].imshow(x_train[i].reshape(rows, cols),
                     cmap='gray', interpolation='bilinear')
        ax[i].set_title(str(y_train[i]) + ' => ' + str(y_train_pred[i]))
        ax[i].set_xticks([])
        ax[i].set_yticks([])
    plt.tight_layout()
    plt.show()

    return 0

if __name__ == '__main__':
    main()


class MyClass:
	pass

# class MyClass():
# 	pass

# class MyClass(MyParentClass1):
# 	pass

# class MyClass(MyParentClass1, MyParentClass2):
# 	pass

# obj = MyClass
obj = MyClass()

class Point:

	system = "2D"

	def __init__(self, x=0, y=0):
		self.x = x
		self.y = y
		self.quadrant = 1
		# print("init is called for Point(%d, %d)" % (self.x, self.y))

	def __del__(self):
		# print("del is called for Point(%d, %d)" % (self.x, self.y))
		pass

	def __add__(self, p2):
		print("add called")
		# print("Calculating add of:")
		# self.show()
		# p2.show()
		if isinstance(p2, Point):
			return Point(self.x + p2.x, self.y + p2.y)
		elif isinstance(p2, int):
			return Point(self.x + p2, self.y + p2)

	def __radd__(self, p2):
		print("radd called")
		return Point(self.x + p2, self.y + p2)

	def __len__(self):
		return self.x + self.y

	def show(self):
		print("Point(%d, %d)" % (self.x, self.y))

	def help():
		print("Type for simulating Point in a %s system/coordinates" % Point.system)

	@staticmethod
	def stat_func():
		print("Called a stat_func() of Point %s" % Point.system)

	@classmethod
	def class_func(cls):
		print("Called a class_func() of Point %s" % cls.system)

# p1 = Point()
# p2 = Point(1,1)
# p3 = Point(1,2)

# del p2

# for i in range(11, 21):
# 	x = Point(i, i)

point1 = Point(1,2)
point2 = Point(3,5)

i = 10
j = 20

i + j
p3 = point1 + point2
p4 = point1 + i
p5 = i + point1
# point3 = point1 + point2
# point3.show()

p3.show()
p4.show()
p5.show()

print(len(p3))
print(len(p4))
print(len(p5))

print("-"*40)

Point.help()
print(Point.system)
Point.stat_func()
Point.class_func()

print("Terminating...")


import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
import csv
import json
import os
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from keras.models import Sequential
from keras.models import Model
from keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense, Lambda
from keras.layers import Concatenate
from keras.backend import int_shape, flatten
from keras import backend as K
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import concatenate
from tensorflow.keras.layers import Input, Reshape, Activation
from tensorflow.keras.utils import plot_model
from sklearn.preprocessing import LabelEncoder
from scipy.ndimage import gaussian_filter
from tensorflow.keras.regularizers import l2
from tensorflow.keras.layers import BatchNormalization
from sklearn.metrics import accuracy_score, f1_score
import dlib

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("/Users/nicole/Documents/2023-1/PFC2/propuesta/shape_predictor_68_face_landmarks.dat")

def get_facial_landmarks(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faces = detector(gray)
    result=image
    for face in faces:
        landmarks = predictor(gray, face)
        landmarks_dict = {}

        landmarks_dict['eye_left'] = (landmarks.part(36).x, landmarks.part(36).y)
        landmarks_dict['eye_right'] = (landmarks.part(45).x, landmarks.part(45).y)
        landmarks_dict['eyebrow_left'] = (landmarks.part(17).x, landmarks.part(19).y)
        landmarks_dict['eyebrow_right'] = (landmarks.part(26).x, landmarks.part(24).y)
        landmarks_dict['nose_base'] = (landmarks.part(33).x, landmarks.part(33).y)
        landmarks_dict['nose_tip'] = (landmarks.part(30).x, landmarks.part(30).y)
        landmarks_dict['nose_left'] = (landmarks.part(31).x, landmarks.part(31).y)
        landmarks_dict['nose_right'] = (landmarks.part(35).x, landmarks.part(35).y)
        landmarks_dict['lip_upper_center'] = (landmarks.part(51).x, landmarks.part(51).y)
        landmarks_dict['lip_lower_center'] = (landmarks.part(57).x, landmarks.part(57).y)
        landmarks_dict['lip_left_corner'] = (landmarks.part(48).x, landmarks.part(48).y)
        landmarks_dict['lip_right_corner'] = (landmarks.part(54).x, landmarks.part(54).y)
        landmarks_dict['cheek_left'] = (landmarks.part(1).x, landmarks.part(1).y)
        landmarks_dict['cheek_right'] = (landmarks.part(15).x, landmarks.part(15).y)

        # Dibujar los landmarks y sus alrededores en la imagen original
        radius = 20  # Radio para resaltar los landmarks
        mask = np.zeros_like(image)

        for landmark in landmarks_dict.values():
            cv2.circle(mask, landmark, radius, (255, 255, 255), -1)

        # Aplicar la máscara a la imagen original
        result = cv2.bitwise_and(image, mask)

    return result

def detect_skin_tone(image):
    # Convertir la imagen a espacio de color HSV
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # Definir los valores de umbral para detección de tono de piel en HSV
    lower_threshold = np.array([0, 20, 70], dtype=np.uint8)
    upper_threshold = np.array([25, 255, 255], dtype=np.uint8)

    # Aplicar la detección de tono de piel utilizando los valores de umbral
    skin_tone_mask = cv2.inRange(hsv_image, lower_threshold, upper_threshold)

    # Aplicar operaciones de morfología para mejorar la detección
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    skin_tone_mask = cv2.morphologyEx(skin_tone_mask, cv2.MORPH_OPEN, kernel)
    skin_tone_mask = cv2.morphologyEx(skin_tone_mask, cv2.MORPH_CLOSE, kernel)

    # Obtener la imagen con el tono de piel detectado
    skin_tone_image = cv2.bitwise_and(image, image, mask=skin_tone_mask)

    return skin_tone_image

def detect_circles(image):
    # Convertir la imagen a escala de grises
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Aplicar suavizado a la imagen para reducir ruido
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)

    # Detección de círculos utilizando el algoritmo de Hough
    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=100,
                               param1=50, param2=30, minRadius=5, maxRadius=50)

    # Si se encontraron círculos, eliminar el fondo
    if circles is not None:
        circles = np.round(circles[0, :]).astype("int")
        mask = np.zeros_like(gray)
        for (x, y, r) in circles:
            cv2.circle(mask, (x, y), r, (255), -1)

        masked_image = cv2.bitwise_and(image, image, mask=mask)
        return masked_image

    return image

def remove_background(image):
    # Obtener el número de canales de la imagen
    num_channels = len(image.shape)

    if num_channels == 2:
        print( "Blanco y negro")
        image=detect_skin_tone(image)
        image=detect_circles(image)
    elif num_channels == 3:
        print( "A color")
    else:
        print( "Formato de imagen no válido")

    return image

# Ruta del archivo CSV
coco_csv_path = "/Users/nicole/Documents/2023-1/PFC2/Data Set/emotions/train_emotions_data_70.csv"
max_images_per_label = 1000

# Obtener las imágenes y las etiquetas del archivo CSV
images = []
emotions = []
ages = []

# Leer el archivo CSV
with open(coco_csv_path, 'r') as file:
    reader = csv.reader(file)
    header = next(reader)  # Leer la primera fila del encabezado

    for row in reader:
        image_id = row[0]
        file_name = row[1]
        path = row[2]
        age = row[3]
        emotion = row[4]

        images.append({
            "id": image_id,
            "file_name": file_name,
            "path": path
        })
        emotions.append(emotion)
        ages.append(age)

# Contador de imágenes por etiqueta
label_counts = {}
age_counts = {}

# Crear las listas para almacenar los datos de entrenamiento y prueba
X = []
y = []

# Ruta de la carpeta que contiene las imágenes
images_folder = "/Users/nicole/Documents/2023-1/PFC2/Data Set/emotions"

# Leer las imágenes y las etiquetas
for image_info, label, age in zip(images, emotions, ages):
    if label not in label_counts:
        label_counts[label] = 0
        age_counts[label] = {"kid": 0, "no kid": 0}

    # Verificar el límite de imágenes por etiqueta
    if label_counts[label] >= max_images_per_label:
        continue

    # Clasificar las imágenes según la etiqueta de edad
    if age == "kid":
        if age_counts[label]["kid"] < 1 * max_images_per_label:
            age_counts[label]["kid"] += 1
        else:
            continue
    else:
        if age_counts[label]["no kid"] < 0 * max_images_per_label:
            age_counts[label]["no kid"] += 1
        else:
            continue

    # Ruta completa de la imagen
    image_file = os.path.join(images_folder, str(image_info["path"]))

    # Leer la imagen
    image = cv2.imread(image_file)
    # Redimensionar a 48x48 píxeles y convertir a escala de grises
    image = cv2.resize(image, (144, 144))
    #image = remove_background(image)
    image = get_facial_landmarks(image)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Incrementar el contador de imágenes por etiqueta
    label_counts[label] += 1

    X.append(image)
    y.append(label)

# Realizar el conteo de elementos para cada etiqueta
for emotion, count in label_counts.items():
    print(f"{emotion}: {count} elementos (niños: {age_counts[emotion]['kid']}, no niños: {age_counts[emotion]['no kid']})")

# Convertir las listas a matrices numpy
X = np.array(X)
y = np.array(y)

# Expandir las dimensiones de X para agregar el canal de color
X = np.expand_dims(X, axis=-1)

# Dividir los datos en conjuntos de entrenamiento y prueba
X_train, y_train = X, y
X_train = X_train.astype(np.uint8)
X_train = X_train * 255

# Ruta del archivo CSV de prueba
coco_csv_path_test = "/Users/nicole/Documents/2023-1/PFC2/Data Set/emotions/test_emotions_data_30.csv"

# Obtener las imágenes y las etiquetas del archivo CSV de prueba
images_ = []
emotions_ = []
ages_ = []

# Leer el archivo CSV de prueba
with open(coco_csv_path_test, 'r') as file:
    reader = csv.reader(file)
    header = next(reader)  # Leer la primera fila del encabezado

    for row in reader:
        image_id = row[0]
        file_name = row[1]
        path = row[2]
        age = row[3]
        emotion = row[4]

        images_.append({
            "id": image_id,
            "file_name": file_name,
            "path": path
        })
        emotions_.append(emotion)
        ages_.append(age)

# Contador de imágenes por etiqueta
label_counts_ = {}
age_counts_ = {}

# Crear las listas para almacenar los datos de prueba
X_ = []
y_ = []
max_images_per_label=450
# Leer las imágenes y las etiquetas del archivo CSV de prueba
for image_info, label, age in zip(images_, emotions_, ages_):
    if label not in label_counts_:
        label_counts_[label] = 0
        age_counts_[label] = {"kid": 0, "no kid": 0}

    # Verificar el límite de imágenes por etiqueta
    if label_counts_[label] >= max_images_per_label:
        continue

    # Clasificar las imágenes según la etiqueta de edad
    if age == "kid":
        if age_counts_[label]["kid"] < 1 * max_images_per_label:
            age_counts_[label]["kid"] += 1
        else:
            continue
    else:
        if age_counts_[label]["no kid"] < 0.0 * max_images_per_label:
            age_counts_[label]["no kid"] += 1
        else:
            continue

    # Ruta completa de la imagen
    image_file = os.path.join(images_folder, str(image_info["path"]))

    # Leer la imagen
    image = cv2.imread(image_file)
    # Redimensionar a 48x48 píxeles y convertir a escala de grises
    image = cv2.resize(image, (144, 144))
    #image = remove_background(image)
    image = get_facial_landmarks(image)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Incrementar el contador de imágenes por etiqueta
    label_counts_[label] += 1

    X_.append(image)
    y_.append(label)

# Realizar el conteo de elementos para cada etiqueta
for emotion, count in label_counts_.items():
    print(f"{emotion}: {count} elementos (niños: {age_counts_[emotion]['kid']}, no niños: {age_counts_[emotion]['no kid']})")

X_ = np.array(X_)
y_ = np.array(y_)

# Expandir las dimensiones de X para agregar el canal de color
X_ = np.expand_dims(X_, axis=-1)

X_test, y_test = X_, y_
X_test = X_test.astype(np.uint8)
X_test = X_test * 255

num_classes = len(np.unique(y))

# Inicializar el codificador de etiquetas
label_encoder = LabelEncoder()

# Codificar las etiquetas de entrenamiento
y_train_encoded = label_encoder.fit_transform(y_train)

# Codificar las etiquetas de prueba (si es necesario)
y_test_encoded = label_encoder.fit_transform(y_test)

# Convertir las etiquetas codificadas a codificación one-hot
y_train = to_categorical(y_train_encoded, num_classes)
y_test = to_categorical(y_test_encoded, num_classes)

# Verificar las dimensiones de los conjuntos
print('Dimensiones de X_train:', X_train.shape)
print('Dimensiones de X_test:', X_test.shape)
print('Dimensiones de y_train:', y_train.shape)
print('Dimensiones de y_test:', y_test.shape)

# Matrices de filtro para la detección de bordes
top_edge_filter = np.array([[-1, -1, -1], [1, 1, 1], [0, 0, 0]]).reshape((3, 3, 1, 1))
bottom_edge_filter = np.array([[1, 1, 1], [0, 0, 0],[-1, -1, -1]]).reshape((3, 3, 1, 1))
left_edge_filter = np.array([[-1, 1, 0], [-1, 1, 0], [-1, 1, 0]]).reshape((3, 3, 1, 1))
right_edge_filter = np.array([[0, 1, -1], [0, 1, -1], [0, 1, -1]]).reshape((3, 3, 1, 1))

input_shape = (144, 144, 1)
inputs = Input(shape=input_shape)

conv_1=Conv2D(filters=1, kernel_size=(3, 3), padding="same", strides=(1, 1), kernel_regularizer=l2(0.001), kernel_initializer=tf.constant_initializer(top_edge_filter))(inputs)
conv_2=Conv2D(filters=1, kernel_size=(3, 3), padding="same", strides=(1, 1), kernel_regularizer=l2(0.001), kernel_initializer=tf.constant_initializer(bottom_edge_filter))(conv_1)
conv_3=Conv2D(filters=1, kernel_size=(3, 3), padding="same", strides=(1, 1), kernel_regularizer=l2(0.001), kernel_initializer=tf.constant_initializer(left_edge_filter))(conv_2)
conv_4=Conv2D(filters=1, kernel_size=(3, 3), padding="same", strides=(1, 1), kernel_regularizer=l2(0.001), kernel_initializer=tf.constant_initializer(right_edge_filter))(conv_3)

conv_5=Conv2D(filters=64, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(conv_4)
actv_1=Activation('relu')(conv_5)
batchnorm_1 = BatchNormalization()(actv_1)
conv_6=Conv2D(filters=64, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_1)
actv_2=Activation('relu')(conv_6)
batchnorm_2 = BatchNormalization()(actv_2)
maxp_1=MaxPool2D(pool_size=(2,2))(batchnorm_2)
drop_1=Dropout(0.1)(maxp_1)

conv_7=Conv2D(filters=128, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(drop_1)
actv_3=Activation('relu')(conv_7)
batchnorm_3 = BatchNormalization()(actv_3)
conv_8=Conv2D(filters=128, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_3)
actv_4=Activation('relu')(conv_8)
batchnorm_4 = BatchNormalization()(actv_4)
conv_9=Conv2D(filters=128, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_4)
actv_5=Activation('relu')(conv_9)
batchnorm_5 = BatchNormalization()(actv_5)
maxp_2=MaxPool2D(pool_size=(2,2))(batchnorm_5)
drop_2=Dropout(0.1)(maxp_2)

conv_10=Conv2D(filters=256, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(drop_2)
actv_6=Activation('relu')(conv_10)
batchnorm_6 = BatchNormalization()(actv_6)
conv_11=Conv2D(filters=256, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_6)
actv_7=Activation('relu')(conv_11)
batchnorm_7 = BatchNormalization()(actv_7)
conv_12=Conv2D(filters=256, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_7)
actv_8=Activation('relu')(conv_12)
batchnorm_8 = BatchNormalization()(actv_8)
conv_13=Conv2D(filters=256, kernel_size=(3,3),padding="same",strides=(1,1),kernel_regularizer=l2(0.001))(batchnorm_8)
actv_9=Activation('relu')(conv_13)
batchnorm_9 = BatchNormalization()(actv_9)
maxp_3=MaxPool2D(pool_size=(2,2))(batchnorm_9)
drop_3=Dropout(0.1)(maxp_3)
flat_1= Flatten()(drop_3)
outputs = Dense(7, activation='softmax')(flat_1)
model = Model(inputs=inputs, outputs=outputs)

tf.keras.utils.plot_model(model, show_shapes=True)
print(model.summary())
# Compilar el modelo
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Entrenar el modelo
history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=25, batch_size=64)

# Evaluar el modelo en el conjunto de prueba
y_pred = model.predict(X_test)
y_pred = np.argmax(y_pred, axis=1)
y_test = np.argmax(y_test, axis=1)

# Calcular el accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Calcular el F1 score
f1 = f1_score(y_test, y_pred, average='weighted')
print("F1 Score:", f1)

# Imprimir el reporte de clasificación y la matriz de confusión
print(classification_report(y_test, y_pred))
confusion_mtx = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_mtx, annot=True, fmt='d')
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

# Guardar el modelo entrenado
model.save('fercChilds100.h5')


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
from xmind.core.markerref import MarkerId
xmind_name="netty"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("Netty:ByteBuf缓冲区")
r2=s2.getRootTopic()
r2.setTitle("Netty:ByteBuf缓冲区")

content={

'定义':[
    '一个字节容器，内部是一个字节数组'
],
'AbstractByteBuf抽象类':[
    {'属性':[
        {'readerIndex（读指针）':[
            '指示读取的起始位置,readerIndex与writerIndex相等，表示ByteBuf不可读了'
        ]},
        {'writerIndex（写指针）':[
            '写入的起始位置,writerIndex与capacity()容量相等，表示ByteBuf已经不可写了'
        ]},
        'maxCapacity（最大容量）'
    ]},
    {'方法':[
        {'容量系列':[
            {'capacity()':[
                'ByteBuf的容量'
            ]},
            {'maxCapacity()':[
                'ByteBuf能够容纳的最大字节数'
            ]}
        ]},
        {'写入系列':[
            {'isWritable()':[
                'ByteBuf是否可写',
                'false:不代表不能再往ByteBuf中写数据了,ByteBuf是可扩容的'
            ]},
            {'writableBytes()':[
                '取得可写入的字节'
            ]},
            {'writeBytes(byte[] src)':[
                '把src字节数组中的数据全部写到ByteBuf'
            ]},
            {'markWriterIndex()与resetWriterIndex()':[
                '实现覆盖写'
            ]}
        ]},
        {'读取系列':[
            {'isReadable()':[
                'ByteBuf是否可读'
            ]},
            {'readableBytes()':[
                'ByteBuf当前可读取的字节数'
            ]},
            {'readBytes(byte[] dst)':[
                '读取ByteBuf中的数据,将数据从ByteBuf读取到dst字节数组中'
            ]},
            {'markReaderIndex()与resetReaderIndex()':[
                '实现重复读'
            ]}
        ]}
    ]}
],
'ByteBufAllocato分配器':[
    {'定义':[
        '创建缓冲区和分配内存空间',
        '默认分配器为ByteBufAllocator.DEFAULT,可通过Java系统参数（SystemProperty）的选项io.netty.allocator.type进行配置'
    ]},
    {'两种实现':[
        {'PoolByteBufAllocator':[
            '将ByteBuf实例放入池中,采用了jemalloc高效内存分配的策略'
        ]},
        {'UnpooledByteBufAllocator':[
            '通过Java的垃圾回收机制回收'
        ]}
    ]},
    {'分配器分配ByteBuf的方法':[
        {'ByteBufAllocator.DEFAULT.buffer(9, 100)':[
            '初始容量为9，最大容量100的缓冲区'
        ]},
        {'ByteBufAllocator.DEFAULT.buffer()':[
            '初始容量为256，最大容量Integer.MAX_VALUE的缓冲区'
        ]}
    ]}
],
'缓冲区类型':[
    '堆缓存区',
    '直接缓存区:使用堆外内存',
    '组合缓存区'
],
'ByteBuf的自动释放':[
    {'ByteBufe的内存回收':[
        '通过引用计数的方式管理的(对Pooled ByteBuf的支持)'
    ]},
    {'入站的ByteBuf':[
        {'TailHandler自动释放':[
            'Netty默认会在ChannelPipline通道流水线的最后添加一个TailHandler末尾处理器',
            '它实现了默认的处理方法，在这些方法中会完成ByteBuf内存释放的工作'
        ]},
        {'SimpleChannelInboundHandler自动释放':[
            '继承SimpleChannelInboundHandler,它在调用完channelRead()方法后，帮忙释放ByteBuf实例'
        ]},
        {'手动释放ByteBuf':[
            '调用byteBuf.release()'
        ]}
    ]},
    {'出站的ByteBuf':[
        'HeadHandler自动释放'
    ]}
],
'ByteBuf的浅层复制':[
    'slice切片浅层复制',
    'duplicate整体浅层复制'
]

}

#构建xmind
xmind.build(content,r2)
#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")


from flask import request, Blueprint, render_template, url_for, session

from datetime import datetime

from core import JsonResponse
from core import Database
from core import Config
from core import Authorization
from core import Scheduler

from model.ModuleSessionType import ModuleSessionType as ModuleSessionTypeModel
from model.ModuleSession import ModuleSession as ModuleSessionModel
from model.Module import Module as ModuleModel
from model.Teacher import Teacher as TeacherModel
from model.RoomBooking import RoomBooking as RoomBookingModel
from model.Campus import Campus as CampusModel
from model.CampusBuilding import CampusBuilding as CampusBuildingModel
from model.CampusBuildingRoom import CampusBuildingRoom as CampusBuildingRoomModel
from model.StudentModule import StudentModule as StudentModuleModel
from model.Term import Term as TermModel

ModuleSession = Blueprint('ModuleSession', __name__)

@ModuleSession.route('/module/<module_id>/session', methods=['GET'])
def List(module_id):
    """ Lists all sessions for given module. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return render_template('error/no_access.html')

    # Get module object
    module = ModuleModel.findById(module_id)

    if not module:
        return render_template('error/resource_not_found.html')

    # Get list of module sessions for given module
    sessions = ModuleSession.findBy('module', module_id)

    # Get a list of all teachers
    teachers = TeacherModel.all()

    # Get a list of all module session types
    session_types = ModuleSessionTypeModel.all()

    return render_template('session_list.html', data = {
        'module': module,
        'sessions': sessions,
        'teachers': teachers,
        'session_types': session_types
    })

@ModuleSession.route('/module/<module_id>/session', methods=['POST'])
def Create(module_id):
    """ Creates a new module session. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this page. Contact system administrator.'})

    # Get module object
    module = ModuleModel.findById(module_id)

    if not module:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Module not found.'})

    # Parse and validate request body
    teacher      = request.form.get('teacher')
    sessionType  = request.form.get('type')

    if not TeacherModel.findById(teacher):
        return JsonResponse.badRequest({'message': 'not_found', 'nice_message': 'Teacher not found.'})

    if not ModuleSessionTypeModel.findById(sessionType):
        return JsonResponse.badRequest({'message': 'not_found', 'nice_message': 'Module session not found.'})

    # Save new data to database
    moduleSession = ModuleSessionModel()

    moduleSession.setModule(module.getId()) \
                 .setStaff(teacher) \
                 .setType(sessionType) \
                 .save()

    return JsonResponse.ok()

@ModuleSession.route('/module/<module_id>/session/<session_id>', methods=['GET'])
def View(module_id, session_id):
    """ Returns module session information. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return render_template('error/no_access.html')

    # Get module object
    module = ModuleModel.findById(module_id)

    if not module:
        return render_template('error/resource_not_found.html')

    # Get session object
    theSession = ModuleSessionModel.findById(session_id)

    if not theSession:
        return render_template('error/resource_not_found.html')

    # Get the campus objects
    campus_list = CampusModel.all()
    campuses = []

    for campus in campus_list:
        buildings = CampusBuildingModel.findBy('campus', campus.getId())
        if(len(buildings) > 0):
            campuses.append({
                'campus': campus,
                'buildings': buildings
            })

    theSession = {
        'session': theSession,
        'staff': TeacherModel.findById(theSession.getStaff()),
        'type': ModuleSessionTypeModel.findById(theSession.getType()),
        'campuses': CampusModel.all()
    }

    # Get list of teachers
    teachers = TeacherModel.all()

    # Get list of session types
    session_types = ModuleSessionTypeModel.all()

    # Get terms
    terms = TermModel.all()
    term_list = []

    for term in terms:
        term_list.append({
            'term': term,
            'startDate': datetime.utcfromtimestamp(term.getStartDate()).strftime("%B %Y"),
            'endDate': datetime.utcfromtimestamp(term.getEndDate()).strftime("%B %Y")
        })

    # Get list of room bookings
    room_bookings = RoomBookingModel.findBy('module_session', session_id)
    room_bookings2 = []

    for booking in room_bookings:
        room = CampusBuildingRoomModel.findById(booking.getRoom())
        building = CampusBuildingModel.findById(room.getBuilding())
        campus = CampusModel.findById(building.getCampus())
        timeFrom = datetime.utcfromtimestamp(booking.getTimeFrom()).strftime("%Y-%m-%d %H:%M")
        timeTo   = datetime.utcfromtimestamp(booking.getTimeTo()).strftime("%Y-%m-%d %H:%M")

        room_bookings2.append({
            'booking': booking,
            'room': room,
            'building': building,
            'campus': campus,
            'timeFrom': timeFrom,
            'timeTo': timeTo
        })

    return render_template('session_view.html', data = {
        'module': module,
        'session': theSession,
        'teachers': teachers,
        'sessionTypes': session_types,
        'roomBookings': room_bookings2,
        'campuses': campuses,
        'terms': term_list
    })

@ModuleSession.route('/module/<module_id>/session/<session_id>', methods=['POST'])
def Update(module_id, session_id):
    """ Updates a module session. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator.'})

    # Get session object
    session = ModuleSessionModel.findById(session_id)

    if not session:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Session not found.'})

    # Save new values to database
    teacher     = request.form.get('teacher')
    sessionType = request.form.get('type')

    if not teacher and not sessionType:
        return JsonResponse.badRequest({'message': 'missing_parameters', 'nice_message': 'Please enter a teacher and session type.'})

    if teacher:
        if not TeacherModel.findById(teacher):
            return JsonResponse.badRequest({'message': 'not_found', 'nice_message': 'Teacher not found.'})

        session.setStaff(teacher)

    if sessionType:
        if not ModuleSessionModel.findById(sessionType):
            return JsonResponse.badRequest({'message': 'not_found', 'nice_message': 'Session type not found.'})

        session.setType(sessionType)

    session.save()

    return JsonResponse.ok()

@ModuleSession.route('/module/<module_id>/session/<session_id>', methods=['DELETE'])
def Delete(module_id, session_id):
    """ Deletes a module session. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator.'})

    # Get session object
    theSession = ModuleSessionModel.findById(session_id)

    if not theSession:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Session not found.'})

    # Delete from database
    theSession.delete()

    return JsonResponse.ok()

@ModuleSession.route('/module/<module_id>/session/<session_id>/booking', methods=['POST'])
def CreateBooking(module_id, session_id):
    """ Creates a new room booking. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact sytem administrator.'})

    # Get session object
    theSession = ModuleSessionModel.findById(session_id)

    if not theSession:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Session not found.'})

    module = ModuleModel.findById(theSession.getModule())

    if not module:
        return JsonResponse.internalServerError({'message': 'not_found', 'nice_message': 'Module not found.'})

    enrolments = StudentModuleModel.findBy('module', module.getId())

    # Get post values
    building_id = request.form.get('building')
    duration    = request.form.get('duration')
    day         = request.form.get('day')
    hour        = request.form.get('hour')

    duration = int(duration)

    if duration <= 0 or duration > 3:
        return JsonResponse.badRequest({'message': 'bad_request', 'nice_message': 'Please enter a duration between 1 and 3.'})

    if not building_id or not duration or not day or not hour:
        return JsonResponse.badRequest({'message': 'missing_parameters', 'nice_message': 'Please ensure you enter a buildng, duration and time.'})

    day = day + " 03:00" # Daylight savings time fix

    ts = int(datetime.strptime(day + " UTC", "%Y-%m-%d %H:%M %Z").strftime("%s"))

    building = CampusBuildingModel.findById(building_id)

    if not building:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Building not found.'})

    #def scheduleOneOff(building_id, duration, capacity, day, hour, sessionType = None, session = None):
    res = Scheduler.scheduleOneOff(building_id, duration, len(enrolments), ts, hour, theSession.getType(), theSession.getId())

    if not res:
        return JsonResponse.badRequest({'message': 'booking_failed', 'nice_message': 'Booking not made.'})

    return JsonResponse.ok()

@ModuleSession.route('/module/<module_id>/session/<session_id>/booking/<booking_id>', methods=['DELETE'])
def DeleteBooking(module_id, session_id, booking_id):
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator'})

    theBooking = RoomBookingModel.findById(booking_id)

    if not theBooking:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Booking not found.'})

    theBooking.delete()

    return JsonResponse.ok()

@ModuleSession.route('/module/<module_id>/session/<session_id>/recurring_booking', methods=['POST'])
def CreateRecurringBooking(module_id, session_id):
    """ Updates a module session. """
    # Verify user access
    if not Authorization.canAccess(session.get('user'), ('scheduling_admin')):
        return JsonResponse.unauthorized({'message': 'no_access', 'nice_message': 'You do not have access to this function. Contact system administrator'})

    # Get session object
    theSession = ModuleSessionModel.findById(session_id)

    if not theSession:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Session not found.'})

    module = ModuleModel.findById(theSession.getModule())

    if not module:
        return JsonResponse.internalServerError({'message': 'not_found', 'nice_message': 'Module not found.'})

    enrolments = StudentModuleModel.findBy('module', module.getId())

    # Get post values
    building_id = request.form.get('building')
    duration    = request.form.get('duration')
    day         = request.form.get('day')
    frequency   = request.form.get('frequency')
    term_id     = request.form.get('term')

    if not building_id or not duration or not day or not frequency or not term_id:
        return JsonResponse.badRequest({'message': 'missing_parameters', 'nice_message': 'Please enter a building, duration, day, frequency and term'})

    if day not in ('1', '2', '3', '4', '5'):
        return JsonResponse.badRequest({'message': 'bad_request', 'nice_message': 'Please choose a day Monday to Friday.'})

    day = int(day)

    if frequency not in ('1', '2', '3', '4', '5', '6', '7', '8'):
        return JsonResponse.badRequest({'message': 'bad_request', 'nice_message': 'Please select a frequency.'})

    frequency = int(frequency)
    duration = int(duration)

    if duration <= 0 or duration > 3:
        return JsonResponse.badRequest({'message': 'bad_requst', 'nice_message': 'Please select a duration betweene 1 and 3 hours.'})

    building = CampusBuildingModel.findById(building_id)

    if not building:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Building not found.'})

    term = TermModel.findById(term_id)

    if not term:
        return JsonResponse.notFound({'message': 'not_found', 'nice_message': 'Term not found.'})

    #def scheduleRecurring(term_id, session_id, building_id, day_of_week, frequency, duration, sessionType):
    res = Scheduler.scheduleRecurring(term.getId(), theSession.getId(), building.getId(), day, frequency, duration, theSession.getType())

    #if not res:
    #    return JsonResponse.badRequest()

    return JsonResponse.ok()


from crawlers.service import CrawlerServices
from crawlers.sok_market import sok_crawler
from mongo.services import MongoService
import datetime

class SokMarketTasks(object):

    def sok_crawler_tasks(self, activity_name, activity_category):

        try:

            print("Task sok_crawler_tasks started.....", datetime.datetime.utcnow())

            get_crawler_config = CrawlerServices().get_crawler_config(company="sok_market", activity=activity_name)

            get_crawlers = CrawlerServices().fetch_urls_to_crawl({"status": 1, 'company__name': 'sok_market', "activity__name": activity_name, "activity_category__name": activity_category})

            if get_crawlers:

                for crawler in get_crawlers:

                    print("sok_crawler_tasks {} started...".format(crawler.page_url))

                    data = {
                        'info': {
                            'company_name': str(crawler.company),
                            'demand': str(crawler.demand),
                            'activity': str(crawler.activity),
                            'activity_category': str(crawler.activity_category),
                            'page_name': str(crawler.page_name),
                            'page_category': str(crawler.page_category),
                            'page_url': str(crawler.page_url),
                            'crawled_time': str(datetime.datetime.now()),
                        },
                    }

                    data['products_and_price'] = []

                    if crawler.page_numbers >= 1:

                        for page in range(1, crawler.page_numbers + 1):

                            innerHTML = sok_crawler.SokCrawler().get_innerHTML(crawler.page_url, page)
                            products_and_price = sok_crawler.SokCrawler().html_parser(innerHTML, get_crawler_config, crawler.page_category)

                            if products_and_price:

                                data['products_and_price'] = data['products_and_price'] + products_and_price

                            else:

                                print("sok_crawler_tasks products_and_price is False...")

                    else:

                        innerHTML = sok_crawler.SokCrawler().get_innerHTML(crawler.page_url)
                        products_and_price = sok_crawler.SokCrawler().html_parser(innerHTML, get_crawler_config, crawler.page_category)

                        if products_and_price:

                            data['products_and_price'] = products_and_price

                        else:

                            print("sok_crawler_tasks products_and_price is False...")

                    if data['products_and_price']:

                        document_save = MongoService().insert_one(collection=activity_name, document=data)

                        if document_save:
                            print("Document saved mongodb Sok Market...", datetime.datetime.utcnow())

            else:
                print("SokCrawlers Activity Category: {} bulunmuyor....".format(activity_category))

        except Exception as e:
            print("SokCrawlers sok_crawler_tasks EXCEPTION: {}".format(e))


# Author: Ankush Gupta
# Date: 2015

"""
Entry-point for generating synthetic text images, as described in:

@InProceedings{Gupta16,
      author       = "Gupta, A. and Vedaldi, A. and Zisserman, A.",
      title        = "Synthetic Data for Text Localisation in Natural Images",
      booktitle    = "IEEE Conference on Computer Vision and Pattern Recognition",
      year         = "2016",
    }
"""
import os
from synthgen import *
from common import *
from functools import reduce
import re
from time import time
from data_provider import DateProvider

# Define some configuration variables:
NUM_IMG = 1  # number of images to use for generation (-1 to use all available):
INSTANCE_PER_IMAGE = 1  # number of times to use the same image
SECS_PER_IMG = 5  # max time per image in seconds

# path to the data-file, containing image, depth and segmentation:
DATA_PATH = 'data'
OUT_FILE = 'results/SynthText.h5'

MASKS_DIR = "./masks"

def add_res_to_db(imgname, res, db):
    """
    Add the synthetically generated text image instance
    and other metadata to the dataset.
    """
    ninstance = len(res)
    for i in range(ninstance):
        dname = "%s_%d" % (imgname, i)
        db['data'].create_dataset(dname, data=res[i]['img'])
        db['data'][dname].attrs['charBB'] = res[i]['charBB']
        db['data'][dname].attrs['wordBB'] = res[i]['wordBB']
        L = res[i]['txt']
        L = [n.encode("ascii", "ignore") for n in L]
        db['data'][dname].attrs['txt'] = L

def main(viz=False, debug=False, output_masks=False, data_path=None):
    """
    Entry point.

    Args:
        viz: display generated images. If this flag is true, needs user input to continue with every loop iteration.
        output_masks: output masks of text, which was used during generation
    """
    if output_masks:
        # create a directory if not exists for masks
        if not os.path.exists(MASKS_DIR):
            os.makedirs(MASKS_DIR)

    # open databases:
    print(colorize(Color.BLUE, 'getting data..', bold=True))

    provider = DateProvider(data_path)

    # db = DateProvider.get_data()
    print(colorize(Color.BLUE, '\t-> done', bold=True))

    # open the output h5 file:
    out_db = h5py.File(OUT_FILE, 'w')
    out_db.create_group('/data')
    print(colorize(Color.GREEN, 'Storing the output in: ' + OUT_FILE, bold=True))

    # get the names of the image files in the dataset:
    imnames = provider.get_imnames()
    N = len(imnames)
    global NUM_IMG
    if NUM_IMG < 0:
        NUM_IMG = N
    start_idx, end_idx = 0, min(NUM_IMG, N)

    renderer = RendererV3(DATA_PATH, max_time=SECS_PER_IMG)
    for i in range(start_idx, end_idx):
        imname = imnames[i]

        try:
            # get the image:
            img = provider.get_image(imname)
            # get the pre-computed depth:
            #  there are 2 estimates of depth (represented as 2 "channels")
            #  here we are using the second one (in some cases it might be
            #  useful to use the other one):
            depth = provider.get_depth(imname)
            # get segmentation:
            seg = provider.get_segmap(imname)[:].astype('float32')
            area = provider.get_segmap(imname).attrs['area']  # number of pixels in each region
            label = provider.get_segmap(imname).attrs['label']

            # re-size uniformly:
            sz = depth.shape[:2][::-1]
            img = np.array(img.resize(sz, Image.ANTIALIAS))
            seg = np.array(Image.fromarray(seg).resize(sz, Image.NEAREST))
            print(colorize(Color.RED, '%d of %d' % (i, end_idx - 1), bold=True))

            if debug:
                print("\n    Processing " + str(imname) + "...")

            res = renderer.render_text(img, depth, seg, area, label,
                                  ninstance=INSTANCE_PER_IMAGE)
            if len(res) > 0:
                # non-empty : successful in placing text:
                add_res_to_db(imname, res, out_db)
                if debug:
                    print("    Success. " + str(len(res[0]['txt'])) + " texts placed:")
                    print("    Texts:" + ";".join(res[0]['txt']) + "")
                    ws = re.sub(' +', ' ', (" ".join(res[0]['txt']).replace("\n", " "))).strip().split(" ")
                    print("    Words: #" +str(len(ws)) + " " + ";".join(ws) + "")
                    print("    Words bounding boxes: " + str(res[0]['wordBB'].shape) + "")
            else:
                print("    Failure: No text placed.")

            if len(res) > 0 and output_masks:
                ts = str(int(time() * 1000))

                # executed only if --output-masks flag is set
                prefix = MASKS_DIR + "/" + imname + ts

                imageio.imwrite(prefix + "_original.png", img)
                imageio.imwrite(prefix + "_with_text.png", res[0]['img'])

                # merge masks together:
                merged = reduce(lambda a, b: np.add(a, b), res[0]['masks'])
                # since we just added values of pixels, need to bring it back to 0..255 range.
                merged = np.divide(merged, len(res[0]['masks']))
                imageio.imwrite(prefix + "_mask.png", merged)

                # print bounding boxes
                f = open(prefix + "_bb.txt", "w+")
                bbs = res[0]['wordBB']
                boxes = np.swapaxes(bbs, 2, 0)
                words = re.sub(' +', ' ', ' '.join(res[0]['txt']).replace("\n", " ")).strip().split(" ")
                assert len(boxes) == len(words)
                for j in range(len(boxes)):
                    as_strings = np.char.mod('%f', boxes[j].flatten())
                    f.write(",".join(as_strings) + "," + words[j] + "\n")
                f.close()

            # visualize the output:
            if viz:
                # executed only if --viz flag is set
                for idict in res:
                    img_with_text = idict['img']
                    viz_textbb(1, img_with_text, [idict['wordBB']], alpha=1.0)
                    viz_masks(2, img_with_text, seg, depth, idict['labeled_region'])
                    # viz_regions(rgb.copy(),xyz,seg,regions['coeff'],regions['label'])
                    if i < INSTANCE_PER_IMAGE - 1:
                        raw_input(colorize(Color.BLUE, 'continue?', True))
                if 'q' in input(colorize(Color.RED, 'continue? (enter to continue, q to exit): ', True)):
                    break
        except:
            traceback.print_exc()
            print(colorize(Color.GREEN, '>>>> CONTINUING....', bold=True))
            continue
    provider.close()
    out_db.close()

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Genereate Synthetic Scene-Text Images')
    parser.add_argument('--viz', action='store_true', dest='viz', default=False,
                        help='flag for turning on visualizations')
    parser.add_argument('--output-masks', action='store_true', dest='output_masks', default=False,
                        help='flag for turning on output of masks')
    parser.add_argument('--debug', action='store_true', dest='debug', default=False,
                        help='flag for turning on debug output')
    parser.add_argument("--data", type=str, dest='data_path', default=None,
                        help="absolute path to data directory containing images, segmaps and depths")
    args = parser.parse_args()
    main(viz=args.viz, debug=args.debug, output_masks=args.output_masks, data_path=args.data_path)


import sys

from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMenu, QAction,
                             QWidget, QLabel, QScrollArea,
                             QVBoxLayout, QHBoxLayout,
                             QFrame, QSizePolicy, QLayout,
                             QGraphicsOpacityEffect,
                             )
from PyQt5.QtCore import (Qt, QAbstractAnimation, QPropertyAnimation,
                          QSequentialAnimationGroup, QEasingCurve,
                          )

default_style = """\
MyChat{
    border-radius: 10px;
    font: bold 14px;
    padding: 6px;
    background: lawngreen;

}
OtherChat{
/*    border: 2px solid black;*/
    border-radius: 10px;
    font: bold 14px;
    padding: 6px;
    background: white;
}
"""

class MyChat(QLabel):
    pass
class OtherChat(QLabel):
    pass

class Base(QWidget):
    def set_fadeout(self, wait_for_sec=7):
        opc = QGraphicsOpacityEffect(self)
        self.opc = opc
        self.setGraphicsEffect(opc)

        cue = QSequentialAnimationGroup()
        self.cue = cue
        cue.addPause(wait_for_sec*1000)

        # Fade-out
        anim = QPropertyAnimation(opc, b"opacity")
        self.anim = anim
        anim.setDuration(3000)
        anim.setStartValue(1)
        anim.setEndValue(0)
        anim.setEasingCurve(QEasingCurve.InExpo)

        cue.addAnimation(anim)
        cue.finished.connect(self.deleteLater)
        cue.start(QAbstractAnimation.DeleteWhenStopped)

    @classmethod
    def auto_delete(cls, text, wait_for_sec=30, parent=None, *args, **kwargs):
        me = cls(text, parent, *args, **kwargs)
        me.set_fadeout(wait_for_sec=wait_for_sec)
        return me

class Mine(Base):
    def __init__(self, text, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.text = MyChat(text, self)
        self.initUI()

    def initUI(self):
        self.setSizePolicy(
            QSizePolicy(
                QSizePolicy.Preferred,
                QSizePolicy.MinimumExpanding
                )
            )
        align = QHBoxLayout(self)
        self.text.setWordWrap(True)
##        self.text.setSizePolicy(
##            QSizePolicy(
##                QSizePolicy.Minimum,
##                QSizePolicy.MinimumExpanding
##                )
##            )
        align.addSpacing(20)
        align.addStretch(0)
        align.addWidget(self.text)

class Other(Base):
    def __init__(self, text, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.text = OtherChat(text, self)
        self.initUI()

    def initUI(self):
        self.setSizePolicy(
            QSizePolicy(
                QSizePolicy.Preferred,
                QSizePolicy.MinimumExpanding
                )
            )
        align = QHBoxLayout(self)
        align.setSizeConstraint(QLayout.SetMaximumSize)
        self.text.setWordWrap(True)
##        self.text.setSizePolicy(
##            QSizePolicy(
##                QSizePolicy.ShrinkFlag | QSizePolicy.ExpandFlag,
####                QSizePolicy.Preferred,
##                QSizePolicy.MinimumExpanding
##                )
##            )
        align.addWidget(self.text)
        align.addStretch(0)
        align.addSpacing(20)

class ShowHistory(QScrollArea):

    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
##        # prep
##        self.items = []
        self.t_mode = False
        self._auto_scroll = True
        # make parts
        self.initUI()

    def initUI(self):
        if not self.parentWidget(): self.resize(400,300)
##        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setFrameShape(QFrame.NoFrame)
        # construct and set
        base = QWidget(self, objectName="base")
        self.base = base
        self.setWidgetResizable(True)
##        base.setMinimumWidth(280)
        base.setSizePolicy(
            QSizePolicy(
                QSizePolicy.Maximum,
                QSizePolicy.MinimumExpanding
                )
            )
        margin = QVBoxLayout(base, objectName="margin")
        margin.setSizeConstraint(QLayout.SetMaximumSize)
        scroll_layout = QVBoxLayout(objectName="scroll")
        self.scroll_layout = scroll_layout
        base.setLayout(margin)
        margin.addLayout(scroll_layout)
        scroll_layout.setSizeConstraint(QLayout.SetMaximumSize)
        margin.addStretch(20)
        # set
        self.setWidget(base)
        # set trigger
        vSB = self.verticalScrollBar()
        vSB.setObjectName("vSB")
        self.vSB = vSB
        vSB.rangeChanged.connect(self.scroll_to_bottom)

    def i_said(self, text, fade=-1):
        if fade > 0:
            chat = Mine.auto_delete(text, wait_for_sec=fade, parent=self.base)
        else:
            chat = Mine(text, self.base)
        self.scroll_layout.addWidget(chat)

    def they_said(self, text, fade=-1):
        if fade > 0:
            chat = Other.auto_delete(text, wait_for_sec=fade, parent=self.base)
        else:
            chat = Other(text, self.base)
        self.scroll_layout.addWidget(chat)

    def clear_posts(self):
        for c in self.base.children():
            if isinstance(c, Base): c.deleteLater()

    def transparent(self, t_mode):
##        fshape = QFrame.NoFrame if t_mode else QFrame.Box
##        self.setFrameShape(fshape)
        base = self.base
        base.setAutoFillBackground(t_mode)
        base.setAttribute(Qt.WA_NoSystemBackground, t_mode)
        base.setAttribute(Qt.WA_TranslucentBackground, t_mode)
        base.update()

    def scroll_to_bottom(self):
        if not self._auto_scroll: return
        vSB = self.verticalScrollBar()
        vSB.setValue(vSB.maximum())
        ### top = minimum()
        ### bottom = maximum()
        ### doclength = maximum() - minimum() + pageStep()

class MainWindow(QMainWindow):

    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.t_mode = False
        self._ontop = False
        self._auto_scroll = True
        self.initUI()

    def initUI(self):
        self.setWindowTitle("Main Window")
        self.resize(400,300)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        # construct
        self.history = ShowHistory(self)
        self.setCentralWidget(self.history)
        self.history.setSizePolicy(QSizePolicy(QSizePolicy.Fixed,
                                               QSizePolicy.MinimumExpanding
                                               )
                                   )
##        self.sw_transparent()
##        self.setWindowFlags(self.windowFlags()|Qt.CustomizeWindowHint)
        self.init_cmenu()

    def init_cmenu(self):
        # construct
        cmenu = QMenu(self)
        self.cmenu = cmenu
##        name = "withframe" if self.t_mode else "transparent"
        name = "transparent"
        sw_t = cmenu.addAction(name)
        sw_t.setCheckable(True)
        sw_t.setChecked(self.t_mode)
        sw_t.toggled.connect(self.sw_transparent)

        sw_s = cmenu.addAction("on top")
        sw_s.setCheckable(True)
        sw_s.setChecked(self._ontop)

        sw_s.toggled.connect(self.sw_auto_scroll)
        sw_s = cmenu.addAction("auto")
        sw_s.setCheckable(True)
        sw_s.setChecked(self._auto_scroll)
        sw_s.toggled.connect(self.sw_auto_scroll)

        self.extra_cmenu = cmenu.addSection("extra")

        close = cmenu.addAction("close")
        self.choice_close = close

    def sw_transparent(self):
        # keep current position
        pos = self.geometry()
        # switch opaque-transparent
        t_mode = not self.t_mode
        self.t_mode = t_mode
        self.history.transparent(t_mode)
##        self.setAttribute(Qt.WA_TranslucentBackground, t_mode)
        # frame includes border or background
        self.setWindowFlags(self.windowFlags() ^ Qt.FramelessWindowHint)
        # restore the position
        self.setGeometry(pos)  # frame operation moves the mainwindow
        self.show()

    def sw_ontop(self):
        self._ontop = not self._ontop
##        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setWindowFlags(self.windowFlags() ^ Qt.WindowStaysOnTopHint)
##        # for not working
##        if self._ontop:
##            self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
##        else:
##            self.setWindowFlags(self.windowFlags() & ~Qt.WindowStaysOnTopHint)
        self.show()

    def sw_auto_scroll(self):
        self._auto_scroll = not self._auto_scroll
        self.history._auto_scroll = self._auto_scroll

    def contextMenuEvent(self, event):
        cmenu = self.cmenu
        close = self.choice_close
        action = cmenu.exec_(self.mapToGlobal(event.pos()))
##        if action == sw_t:
##            self.sw_transparent()
        if action == close:
            self.close()

    def mousePressEvent(self, event):
        if Qt.LeftButton != event.button():
            return
        pos = event.pos()
        clicked = self.childAt(pos)
        if isinstance(clicked, QLabel):
            text = clicked.text()
            QGuiApplication.clipboard().setText(text)
            self.setWindowTitle(text)
        print("ouch")

    def addChat1(self, text, fade=-1):
        self.history.i_said(text, fade)
    def addChat2(self, text, fade=-1):
        self.history.they_said(text, fade)
    def clear_history(self):
        self.history.clear_posts()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyleSheet(default_style)
    ex = MainWindow()
    ex.show()
    choice = (ex.addChat2, ex.addChat1)
    for k in range(100):
        choice[k&1]("test{:0>2}".format(k),fade=k+3)
##    sys.exit(app.exec_())


import time
import yaml
from argparse import ArgumentParser
from tqdm import tqdm
import random

import torch
import multiprocessing as mp
from torch.utils.data import DataLoader

from torch.profiler import profile, record_function, ProfilerActivity

from train_utils.helper import construct_agent_cls
from train_utils.dataset import UCI, AutoUCI, sample_data

try:
    import wandb
except ImportError:
    wandb = None

def run(config, args):
    seed = random.randint(1, 10000)
    # seed = 2025
    print(f'Random seed: {seed}')
    torch.manual_seed(seed)
    if args.log and wandb:
        group = config['group'] if 'group' in config else None
        run = wandb.init(
            entity='hzzheng',
            project=config['project'],
            group=group,
            config=config)
        config = wandb.config
    print('Start running...')
    # Parse configuration
    if args.cpu:
        device = torch.device('cpu')
    else:
        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    T = config['T']
    dim_context = config['dim_context']
    num_arm = config['num_arm']
    # ---------------- construct strategy -------------------------
    agent = construct_agent_cls(config, device)

    # --------------- construct bandit ---------------------------
    # dataset = UCI(config['datapath'], dim_context, num_arm)
    num_data = config['num_data'] if 'num_data' in config else None

    dataset = AutoUCI(config['data_name'], dim_context, num_arm,
                      num_data, config['version'])
    bandit = DataLoader(dataset, shuffle=True)
    # --------------------- training -----------------------------
    pbar = tqdm(range(T), dynamic_ncols=True, smoothing=0.1)
    loader = sample_data(bandit)
    reward_history = []
    accum_regret = 0

    # time
    choose_time = []
    update_time = []

    for e in pbar:
        context, label = next(loader)
        context = context.squeeze(0).to(device)
        with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], record_shapes=True) as prof:
        # with torch.autograd.profiler.record_function('Arm selection'):
        # torch.cuda.synchronize(device)
        # start = time.time()
            with record_function("Arm selection"):
                arm_to_pull = agent.choose_arm(context)
        # torch.cuda.synchronize(device)
        # end = time.time()
        # select_cost = end - start
        # choose_time.append(select_cost)
        # compute reward
            if label != arm_to_pull:
                reward = 0
            else:
                reward = 1
            # agent update
            agent.receive_reward(arm_to_pull, context[arm_to_pull], reward)

        # torch.cuda.synchronize(device)
        # start = time.time()
            with record_function('Update model'):
                agent.update_model(num_iter=min(50, config['num_iter']))
        # torch.cuda.synchronize(device)
        # end = time.time()
        # update_cost = end - start
        # update_time.append(update_cost)

        reward_history.append(reward)
        accum_regret += 1 - reward
        print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=20))
        # save and upload results
        pbar.set_description(
            (
                f'Accumulated regret: {accum_regret}'
            )
        )
        if wandb and args.log:
            wandb.log(
                {
                    'Regret': accum_regret,
                    # 'Selecting time': select_cost,
                    # 'Update time': update_cost
                }
            )
    # print(f'averaged selecting cost: {sum(choose_time) / len(choose_time)}\n'
    #       f'averaged updating cost: {sum(update_time) / len(update_time)}\n')
    if wandb and args.log:
        run.finish()
    print('Done!')

if __name__ == '__main__':
    torch.backends.cudnn.benchmark = True
    parser = ArgumentParser(description="basic paser for bandit problem")
    parser.add_argument('--config_path', type=str,
                        default='configs/uci/shuttle-lmcts.yaml')
    parser.add_argument('--cpu', action='store_true', default=False)
    parser.add_argument('--log', action='store_true', default=False)
    parser.add_argument('--repeat', type=int, default=1)
    args = parser.parse_args()
    with open(args.config_path, 'r') as stream:
        config = yaml.load(stream, yaml.FullLoader)
    if args.repeat == 1:
        run(config, args)
    else:
        for i in range(args.repeat):
            p = mp.Process(target=run, args=(config, args))
            p.start()


from datetime import datetime
from textwrap import dedent

import requests
from enum import Enum, auto
from environs import Env
from time import sleep
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler

import logging

logger = logging.getLogger(__name__)

class States(Enum):
    MAIN = auto()
    REQUEST = auto()
    WRITE_TO_US = auto()
    MESSAGE_TO_ADMIN = auto()

def phone_request(update, context):
    keyboard = [
        [KeyboardButton('Отправить номер телефона', request_contact=True)],
        ['Гостевая ссылка (без входа)', 'Отмена'],
    ]
    reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
    update.message.reply_text(
        'Чтобы войти или зарегистрироваться на сайте, нужно предоставить номер',
        reply_markup=reply_markup,
    )

    return States.REQUEST

def main_menu(update, context):
    keyboard = [
        ['➡ Войти на сайт'],
        ['✉ Написать нам']
    ]
    reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
    context.bot.send_message(
        text='Главное меню. Выберете действие!',
        chat_id=update.effective_chat.id,
        reply_markup=reply_markup,
    )

    return States.MAIN

def start(update, context):
    if not 'phone_number' in context.user_data:
        return phone_request(update, context)
    return main_menu(update, context)

def delete_last_message(update, context):
    chat_id = update.callback_query.message.chat_id
    message_id = update.callback_query.message.message_id
    context.bot.delete_message(chat_id, message_id)
    return start(update, context)

def get_api_respone(update, context):
    phone_number = context.user_data['phone_number']
    chat_id = update.message.chat_id
    username = update.message.chat.username

    api_url = context.bot_data['api_url']
    payload = {
         'phone': phone_number,
         'chat_id': chat_id,
         'username': username
    }
    response = requests.post(api_url, data=payload)
    response.raise_for_status()
    user_status = response.json()
    if 'register' in user_status:
        user_link = user_status['register']
        text = dedent('''
            📱 По вашему номеру не найдено регистрации.\n
            Ниже указаны ссылки на *регистрацию* и на
            *прикрепление/смену номера* 👇\n
            _(ссылки действуют 5 мин)_'''
        )
        reply_markup=InlineKeyboardMarkup(
            inline_keyboard=[[InlineKeyboardButton('Новая регистрация', url=user_link)],
                             [InlineKeyboardButton('Завершить', callback_data='Завершить')]]
        )
        update.message.reply_text(
            text,
            parse_mode='markdown',
            reply_markup=reply_markup,
        )
        return States.REQUEST
    elif 'login' in user_status:
        user_link = user_status['login']
        text = f'Вот ссылка для входа на сайт\n_(действует 5 мин_\n\n[{user_link}]({user_link})'
        update.message.reply_text(text, parse_mode='markdown')

    return main_menu(update, context)

def get_guest_link(update, context):
    api_guest_url = context.bot_data['api_guest_url']
    response = requests.get(api_guest_url)
    guest_url = response.json()['url']
    text = f'Вот гостевая ссылка для входа на сайт\n_(действует 5 мин_\n\n[{guest_url}]({guest_url})'
    update.message.reply_text(text, parse_mode='markdown')

    return start(update, context)

def handle_new_phonenumber(update, context):
    context.user_data['phone_number'] = update.message.contact.phone_number
    return get_api_respone(update, context)

def write_to_us(update, context):
    text ='Внимательно выберите тему обращения:'
    reply_markup=InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton('Предложение партнерства', callback_data='Предложение партнерства'),
                InlineKeyboardButton('Активировать кабинет', callback_data='Активировать кабинет')
            ],
            [
                InlineKeyboardButton('Забыл(а) пароль', callback_data='Забыл(а) пароль'),
                InlineKeyboardButton('Другое', callback_data='Другое')
            ],
        ],
    )
    update.message.reply_text(
        text,
        parse_mode='markdown',
        reply_markup=reply_markup,
    )
    return States.WRITE_TO_US

def choose_topic(update, context):
    topic = update.callback_query.data
    context.user_data['topic'] = topic
    keyboard = [
        ['➡ Войти на сайт'],
        ['✉ Написать нам']
    ]
    reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
    context.bot.send_message(
        text=f'Тема: {topic}\nВведите текст вашего обращения:',
        chat_id=update.effective_chat.id,
        parse_mode='markdown',
        reply_markup=reply_markup,
    )

    return States.MESSAGE_TO_ADMIN

def send_message_to_admin(update, context):
    if not 'phone_number' in context.user_data:
        phone = 'Не указан'
    else:
        phone = context.user_data['phone_number']
    topic = context.user_data['topic']

    message = dedent(f'''
        Дата и время: {str(datetime.now())}
        chat id: {update.message.chat_id}
        username: {update.message.chat.username}
        phone: {phone}
        Тема: {topic}
        Текст сообщения: {update.message.text}'''
    )

    if context.bot_data['admin_chat_id']:
        context.bot.send_message(
            text=message,
            chat_id=context.bot_data['admin_chat_id'],
        )

    update.message.reply_text('Сообщение отослано.')
    return start(update, context)

def cancel(update, context):
    username = update.effective_user.first_name
    logger.info(f'User {username} canceled the conversation.')
    update.message.reply_text('До встречи!',
                              reply_markup=ReplyKeyboardRemove())

    return ConversationHandler.END

def error(update, error):
    """Log Errors caused by Updates."""
    logger.warning(f'Update "{update}" caused error "{error}"')

def main():
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.DEBUG)
    env = Env()
    env.read_env()

    bot_token = env.str('TG_BOT_TOKEN')
    key_path = env.str('KEY_PATH')
    cert_path = env.str('CERT_PATH')
    public_ip = env.str('PUBLIC_IP')

    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            States.MAIN: [
                MessageHandler(Filters.regex(r'^➡ Войти на сайт$'), get_api_respone),
                MessageHandler(Filters.regex(r'^✉ Написать нам$'), write_to_us),
            ],
            States.REQUEST: [
                MessageHandler(Filters.regex(r'^➡ Войти на сайт$'), get_api_respone),
                MessageHandler(Filters.regex(r'^✉ Написать нам$'), write_to_us),
                CallbackQueryHandler(delete_last_message, pattern=r'^Завершить$'),
                MessageHandler(Filters.contact, handle_new_phonenumber),
                MessageHandler(Filters.regex(r'^Гостевая ссылка'), get_guest_link),
                MessageHandler(Filters.regex(r'^Отмена$'), start),
            ],
            States.WRITE_TO_US: [
                CallbackQueryHandler(choose_topic),
                MessageHandler(Filters.regex(r'^➡ Войти на сайт$'), get_api_respone),
                MessageHandler(Filters.regex(r'^✉ Написать нам$'), write_to_us),
            ],
            States.MESSAGE_TO_ADMIN: [
                MessageHandler(Filters.regex(r'^➡ Войти на сайт$'), get_api_respone),
                MessageHandler(Filters.regex(r'^✉ Написать нам$'), write_to_us),
                MessageHandler(Filters.text, send_message_to_admin),
            ],
        },
        fallbacks=[CommandHandler('cancel', cancel)],
        allow_reentry=True,
    )

    while True:
        try:
            updater = Updater(token=bot_token)
            dp = updater.dispatcher
            dp.add_handler(conv_handler)
            dp.add_error_handler(error)
            dp.bot_data = {
                'api_url': env.str('API_URL'),
                'api_guest_url': env.str('API_GUEST_URL'),
                'admin_chat_id': env.str('ADMIN_CHAT_ID', default=None)
            }
            # updater.start_polling()
            updater.start_webhook(listen='0.0.0.0',
                      port=8443,
                      url_path=bot_token,
                      key=key_path,
                      cert=cert_path,
                      webhook_url=f'{public_ip}:8443/{bot_token}')
            updater.idle()
        except Exception:
            logger.exception('Ошибка в simple-reg-bot. Перезапуск через 5 секунд.')
            sleep(5)

if __name__ == '__main__':
    main()


'''
A simple REST API exposing a swagger schema.
'''
import logging
import os.path
import random

from flask import request
from flask_restplus import Resource, fields, Model

from common import main, api

log = logging.getLogger(__name__)

example_ns = api.namespace('example', description="Example operations")
scene_ns = api.namespace('scenes', description="Scene operations")

class Colour:
    def __init__(self, value):
        if isinstance(value, str):
            self._int_value = int(value.lstrip('#'), base=16)
        elif isinstance(value, int):
            self._int_value = value
        else:
            raise AssertionError("Invalid Type")

    @property
    def int(self):
        return self._int_value

    @property
    def hex(self):
        return "#{0:06x}".format(self._int_value)

class HexColourField(fields.String):
    __schema_format__ = "hexcolour"

class IntColourField(fields.Integer):
    __schema_format__ = "intcolour"

# Specifications of the objects accepted/returned by the API.
IdModel = api.model('IdModel', {
    'id': fields.Integer(required=True,
                         description='Id number',
                         example=123456),
})
HexColourModel = api.model('HexColourModel', {
    'hexcolour': HexColourField(required=True,
                                description='Hex colour',
                                example="#123456",
                                min_length=7,
                                max_length=7),
})

IntColourModel = api.model('IntColourModel', {
    'intcolour': IntColourField(required=True,
                                description='Int colour',
                                example=16777215,
                                min=0,
                                max=16777215),
})

class SceneModelClass(Model):
    @property
    def __schema__(self):
        schema = super().__schema__
        schema.update({"format": "scene"})
        return schema

SceneModel = SceneModelClass('Scene', {
    'foreground colour': IntColourField(required=True,
                                        description='Int colour',
                                        example=16777215,
                                        min=0,
                                        max=16777215),
    'background colour': IntColourField(required=True,
                                        description='Int colour',
                                        example=16777215,
                                        min=0,
                                        max=16777215)
})
api.add_model(SceneModel.name, SceneModel)

COLOUR_DATA_DICT = {}
SCENE_DATA_DICT = {}

@example_ns.route('')
class ExampleCollection(Resource):
    """Resource allowing access to different types of methods and models."""

    @api.marshal_with(IdModel)
    def post(self):
        """Return a new ID."""
        new_id = random.randint(0, 0xffffffff)
        COLOUR_DATA_DICT[new_id] = None
        return {'id': new_id}

@example_ns.route('/<int:int_id>/hexcolour')
class HexColourResource(Resource):

    @api.marshal_with(HexColourModel)
    def get(self, int_id):
        """Return the colour for the ID"""
        log.debug("Got parameter: %r", int_id)
        return {'hexcolour': COLOUR_DATA_DICT[int_id].hex}

    @api.expect(HexColourModel)
    @api.response(204, 'Colour successfully updated.')
    def put(self, int_id):
        """Set the colour for the ID"""
        log.debug("Got parameter: %r", int_id)
        log.debug("Got body: %r", request.data)
        COLOUR_DATA_DICT[int_id] = Colour(request.json['hexcolour'])
        return None, 204

@example_ns.route('/<int:int_id>/intcolour')
class IntColourResource(Resource):

    @api.marshal_with(IntColourModel)
    def get(self, int_id):
        """Return the colour for the ID"""
        log.debug("Got parameter: %r", int_id)
        return {'intcolour': COLOUR_DATA_DICT[int_id].int}

    @api.expect(IntColourModel)
    @api.response(204, 'Colour successfully updated.')
    def put(self, int_id):
        """Set the colour for the ID"""
        log.debug("Got parameter: %r", int_id)
        log.debug("Got body: %r", request.data)
        COLOUR_DATA_DICT[int_id] = Colour(request.json['intcolour'])
        return None, 204

@scene_ns.route('/<int:int_id>')
class SceneResource(Resource):

    @api.marshal_with(SceneModel)
    @api.response(404, "Scene doesn't exist.")
    def get(self, int_id):
        """Return the scene for the ID"""
        log.debug("Got parameter: %r", int_id)
        if int_id not in SCENE_DATA_DICT:
            api.abort(404, "Scene '{}' doesn't exist".format(int_id))
        return {'foreground colour': SCENE_DATA_DICT[int_id][0],
                'background colour': SCENE_DATA_DICT[int_id][1]}

    @api.expect(SceneModel)
    @api.response(204, 'Scene successfully updated.')
    def put(self, int_id):
        """Set the scene for the ID"""
        log.debug("Got parameter: %r", int_id)
        log.debug("Got body: %r", request.data)
        SCENE_DATA_DICT[int_id] = (request.json['foreground colour'],
                                   request.json['background colour'])
        return None, 204

if __name__ == '__main__':
    main(os.path.splitext(os.path.basename(__file__))[0] + '.json')