import threading
import time
from logging import WARNING
from types import MethodType
from typing import Sequence
from unittest import TestCase
from unittest.mock import Mock, patch

from google.protobuf.duration_pb2 import Duration
from google.rpc.error_details_pb2 import RetryInfo
from grpc import Compression

from opentelemetry.exporter.otlp.proto.grpc.exporter import (
    ExportServiceRequestT,
    InvalidCompressionValueException,
    OTLPExporterMixin,
    RpcError,
    SDKDataT,
    StatusCode,
    environ_to_compression,
)

class TestOTLPExporterMixin(TestCase):
    def test_environ_to_compression(self):
        with patch.dict(
            "os.environ",
            {
                "test_gzip": "gzip",
                "test_gzip_caseinsensitive_with_whitespace": " GzIp ",
                "test_invalid": "some invalid compression",
            },
        ):
            self.assertEqual(
                environ_to_compression("test_gzip"), Compression.Gzip
            )
            self.assertEqual(
                environ_to_compression(
                    "test_gzip_caseinsensitive_with_whitespace"
                ),
                Compression.Gzip,
            )
            self.assertIsNone(
                environ_to_compression("missing_key"),
            )
            with self.assertRaises(InvalidCompressionValueException):
                environ_to_compression("test_invalid")

    @patch(
        "opentelemetry.exporter.otlp.proto.grpc.exporter._create_exp_backoff_generator"
    )
    def test_export_warning(self, mock_expo):
        mock_expo.configure_mock(**{"return_value": [0]})

        rpc_error = RpcError()

        def code(self):
            return None

        rpc_error.code = MethodType(code, rpc_error)

        class OTLPMockExporter(OTLPExporterMixin):
            _result = Mock()
            _stub = Mock(
                **{"return_value": Mock(**{"Export.side_effect": rpc_error})}
            )

            def _translate_data(
                self, data: Sequence[SDKDataT]
            ) -> ExportServiceRequestT:
                pass

            @property
            def _exporting(self) -> str:
                return "mock"

        otlp_mock_exporter = OTLPMockExporter()

        with self.assertLogs(level=WARNING) as warning:
            # pylint: disable=protected-access
            otlp_mock_exporter._export(Mock())
            self.assertEqual(
                warning.records[0].message,
                "Failed to export mock to localhost:4317, error code: None",
            )

        def code(self):  # pylint: disable=function-redefined
            return StatusCode.CANCELLED

        def trailing_metadata(self):
            return {}

        rpc_error.code = MethodType(code, rpc_error)
        rpc_error.trailing_metadata = MethodType(trailing_metadata, rpc_error)

        with self.assertLogs(level=WARNING) as warning:
            # pylint: disable=protected-access
            otlp_mock_exporter._export([])
            self.assertEqual(
                warning.records[0].message,
                (
                    "Transient error StatusCode.CANCELLED encountered "
                    "while exporting mock to localhost:4317, retrying in 0s."
                ),
            )

    def test_shutdown(self):
        result_mock = Mock()

        class OTLPMockExporter(OTLPExporterMixin):
            _result = result_mock
            _stub = Mock(**{"return_value": Mock()})

            def _translate_data(
                self, data: Sequence[SDKDataT]
            ) -> ExportServiceRequestT:
                pass

            @property
            def _exporting(self) -> str:
                return "mock"

        otlp_mock_exporter = OTLPMockExporter()

        with self.assertLogs(level=WARNING) as warning:
            # pylint: disable=protected-access
            self.assertEqual(
                otlp_mock_exporter._export(data={}), result_mock.SUCCESS
            )
            otlp_mock_exporter.shutdown()
            # pylint: disable=protected-access
            self.assertEqual(
                otlp_mock_exporter._export(data={}), result_mock.FAILURE
            )
            self.assertEqual(
                warning.records[0].message,
                "Exporter already shutdown, ignoring batch",
            )

    def test_shutdown_wait_last_export(self):
        result_mock = Mock()
        rpc_error = RpcError()

        def code(self):
            return StatusCode.UNAVAILABLE

        def trailing_metadata(self):
            return {
                "google.rpc.retryinfo-bin": RetryInfo(
                    retry_delay=Duration(seconds=1)
                ).SerializeToString()
            }

        rpc_error.code = MethodType(code, rpc_error)
        rpc_error.trailing_metadata = MethodType(trailing_metadata, rpc_error)

        class OTLPMockExporter(OTLPExporterMixin):
            _result = result_mock
            _stub = Mock(
                **{"return_value": Mock(**{"Export.side_effect": rpc_error})}
            )

            def _translate_data(
                self, data: Sequence[SDKDataT]
            ) -> ExportServiceRequestT:
                pass

            @property
            def _exporting(self) -> str:
                return "mock"

        otlp_mock_exporter = OTLPMockExporter()

        # pylint: disable=protected-access
        export_thread = threading.Thread(
            target=otlp_mock_exporter._export, args=({},)
        )
        export_thread.start()
        try:
            # pylint: disable=protected-access
            self.assertTrue(otlp_mock_exporter._export_lock.locked())
            # delay is 1 second while the default shutdown timeout is 30_000 milliseconds
            start_time = time.time()
            otlp_mock_exporter.shutdown()
            now = time.time()
            self.assertGreaterEqual(now, (start_time + 30 / 1000))
            # pylint: disable=protected-access
            self.assertTrue(otlp_mock_exporter._shutdown)
            # pylint: disable=protected-access
            self.assertFalse(otlp_mock_exporter._export_lock.locked())
        finally:
            export_thread.join()


# This projects aim to create an appplication for tracking expenses
import tkinter as tk
import numpy as np
import datetime as date
import pandas as pd
import matplotlib.pyplot as plt
import random
import matplotlib.dates as mdates
import numpy as np
from time import strftime,localtime
import datetime as dt
from tabulate import tabulate

# I decide to not use objected oriented programming but this could be possible development
# Create empty list where equity is tracked
EQUITY=[]
DATES_EQ=[]
#Create empty lists EXPENSES
GOODS_OR_SERVICES = []
PRICES =[]
DATES =[]
EXPENSE_TYPES = []
#Create empty lists EXPENSES
TYPES_STIP = []
MONEY =[]
DATES_STIP=[]

# def reset EQUITY function
def resetEquity ():
	EQUITY.clear()
	DATES_EQ.clear()
# def set EQUITY function
def setEquity (equity,EQUITY,DATES_EQ):
	x=dt.datetime.now()
	DATES_EQ.append(x.strftime("%d/%m/%Y"))
	EQUITY.append(float(equity))
	df=CreateDataFrame_EQ()
	EQUITY=df['EQUITY'].values.tolist()
	DATES_EQ=df['DATES_EQ'].values.tolist()

# def delete LAST EXPENSE
def deleteLastExpense ():
	GOODS_OR_SERVICES.pop()
	PRICES.pop()
	DATES.pop()
	EXPENSE_TYPES.pop()
	EQUITY.pop()
	DATES_EQ.pop()

def deleteLastIncome ():
	TYPES_STIP.pop()
	MONEY.pop()
	DATES_STIP.pop()
	EQUITY.pop()
	DATES_EQ.pop()

def addExpenses (good_or_service,price,date,expense_type):
	GOODS_OR_SERVICES.append(good_or_service)
	PRICES.append(price)
	DATES.append(date)
	EXPENSE_TYPES.append(expense_type)
	EQUITY.append(float(-price))
	DATES_EQ.append(date)

def addIncomes (type_stip,money_stip,date_stip):
	TYPES_STIP.append(type_stip)
	MONEY.append(money_stip)
	DATES_STIP.append(date_stip)
	EQUITY.append(float(money_stip))
	DATES_EQ.append(date_stip)

def CreateDataFrame_ex ():
	expense_report = pd.DataFrame()
	expense_report['GOODS'] = GOODS_OR_SERVICES
	expense_report['PRICES'] = PRICES
	expense_report['DATES'] = DATES
	expense_report['EXPENSE_TYPES'] = EXPENSE_TYPES
	expense_report['DATES']=pd.to_datetime(expense_report['DATES'], dayfirst = True , format = '%d/%m/%Y')
	sorted_expense_report = expense_report.sort_values(by='DATES')
	print(tabulate(sorted_expense_report, headers='keys', tablefmt='psql'))
	return sorted_expense_report

def CreateDataFrame_in ():
	income_report = pd.DataFrame()
	income_report['TYPES'] = TYPES_STIP
	income_report['MONEY'] = MONEY
	income_report['DATES_STIP'] = DATES_STIP
	income_report['DATES_STIP']=pd.to_datetime(income_report['DATES_STIP'], dayfirst = True , format = '%d/%m/%Y')
	sorted_income_report = income_report.sort_values(by='DATES_STIP')
	print(tabulate(sorted_income_report, headers='keys', tablefmt='psql'))
	return sorted_income_report

def CreateDataFrame_EQ ():
	eq_report = pd.DataFrame()
	eq_report['EQUITY'] = EQUITY
	eq_report['DATES_EQ'] = DATES_EQ
	eq_report['DATES_EQ']=pd.to_datetime(eq_report['DATES_EQ'], dayfirst = True,format = '%d/%m/%Y')
	sorted_eq_report = eq_report.sort_values(by='DATES_EQ')
	print(tabulate(sorted_eq_report, headers='keys', tablefmt='psql'))
	return sorted_eq_report

def dataFrameTest_Year_ex():
	num_of_ex = 10
	Goods=["Mela","Pera","Banana","Carne","Acqua","cena","PC","telefono"]
	Price=[5,5,10,15,0.5,40,800,1000]
	Expenses_type=["Alimentari","Tecnologia"]
	for i in range(num_of_ex):
		date=str(random.randint(25,28))+"/"+ str(random.randint(9,12)) + "/"+"2022"
		addExpenses(Goods[random.randint(0,7)],Price[random.randint(0,7)],date,Expenses_type[random.randint(0,1)])
	df = CreateDataFrame_ex()
	CreateDataFrame_EQ ()
	return df

def dataFrameTest_Year_in():
	num_of_ex = 4
	type_stip=["Lavoro","azioni"]
	Money=[1600,400]
	for i in range(num_of_ex):
		date=str(random.randint(1,23))+"/"+ str(random.randint(1,9)) + "/"+"2022"
		addIncomes(type_stip[random.randint(0,1)],Money[random.randint(0,1)],date)
	df = CreateDataFrame_in()
	CreateDataFrame_EQ ()
	return df

def graphs(df):
	fig1=plt.figure(figsize=(3, 3), dpi=100)
	df1=pd.DataFrame()
	df1=df.groupby(["EXPENSE_TYPES"] , as_index=False)['PRICES'].sum()
	plt.pie(df1['PRICES'],labels=df1["EXPENSE_TYPES"], shadow=True, textprops = dict(color ="red"))
	plt.title("EXPENSES TYPES")

	fig2=plt.figure(figsize=(3, 3), dpi=100)
	plt.bar(df['GOODS'],df["PRICES"],color='red')
	plt.title("EXPENSES GOODS")

	fig3=plt.figure(figsize=(7, 5), dpi=100)
	df2=df.tail(20)
	now = dt.datetime.now()
	then = now - dt.timedelta(days=31)
	days = mdates.drange(then,now,dt.timedelta(days=1))
	#plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
	#plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=3))
	df5=CreateDataFrame_in()
	plt.plot(df2['DATES'],df2['PRICES'],'o',color='red')
	plt.plot(df5['DATES_STIP'],df5['MONEY'],'o',color='green')
	plt.gcf().autofmt_xdate()
	plt.title("INCOME-EXPENSES")

	fig4=plt.figure(figsize=(3, 3), dpi=100)
	df1=CreateDataFrame_EQ()
	eq=[]
	eq_sum=[]
	dat=[]
	eq=df1['EQUITY'].values.tolist()
	s=eq[0]
	for i in range(1,len(eq)+1):
		for j in range(0,i):
			s=s+eq[j]
		eq_sum.append(s)
		s=0
	print(eq_sum)
	dat=df1['DATES_EQ'].values.tolist()
	df2=df1.loc[df1['EQUITY'] > 0]
	df3=df1.loc[df1['EQUITY'] < 0]
	plt.bar(df2['DATES_EQ'],df2['EQUITY'],color='green')
	plt.bar(df3['DATES_EQ'],df3['EQUITY'],color='red')
	plt.plot(df1['DATES_EQ'],eq_sum,"blue")
	plt.gcf().autofmt_xdate()
	plt.title("EQUITY")

	return fig1,fig2,fig3,fig4

x=[]
table_ex=dataFrameTest_Year_ex()
table_in=dataFrameTest_Year_in()
for i in range(10,210,10):
		x.append(int(i))
print(x)


# -*- coding: utf-8 -*-
# title: hangul.py
# course: 언어와 컴퓨터
# author: 박수지
# date created: 2018-10-11
# description: 한글 처리 모듈 --- 음절 분해 및 자모 결합

# 1. 음절 분해
# 준비물
LEADING = 'ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ'
VOWEL = 'ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ'
TRAILING = ('',) + tuple('ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ')

n_cnt = len(VOWEL) * len(TRAILING) # 588
t_cnt = len(TRAILING) # 28

syl = '햒'

# 음절의 지표
s_ind = ord(syl) - ord('가')

# 초성, 중성, 종성의 지표
l_ind = s_ind // n_cnt
v_ind = s_ind % n_cnt // t_cnt
t_ind = s_ind % n_cnt % t_cnt

jamos = ''
jamos += LEADING[l_ind]
jamos += VOWEL[v_ind]
jamos += TRAILING[t_ind]

jamos

# 2. 자모 결합
from unicodedata import name, lookup

# 알려진 사실
'''
>>> lookup('HANGUL CHOSEONG PANSIOS') + lookup('HANGUL JUNGSEONG ARAEA')
'ᅀᆞ'
'''

lvt = 'CHOSEONG', 'JUNGSEONG', 'JONGSEONG'

jamos = 'ㅈㅏㄹ'
l = lookup(name(jamos[0]).replace('LETTER', lvt[0]))
v = lookup(name(jamos[1]).replace('LETTER', lvt[1]))
t = lookup(name(jamos[2]).replace('LETTER', lvt[2]))
l + v + t

def jamo_name(jamo):
    return name(jamo).split()[-1]

jamos = 'ㅈㅏㄹ'
l = lookup(f'HANGUL {lvt[0]} {jamo_name(jamos[0])}')
v = lookup(f'HANGUL {lvt[1]} {jamo_name(jamos[1])}')
t = lookup(f'HANGUL {lvt[2]} {jamo_name(jamos[2])}')
l + v + t

#
jamo_names = 'CIEUC', 'A', 'RIEUL'
l = lookup(f'HANGUL {lvt[0]} {jamo_names[0]}')
v = lookup(f'HANGUL {lvt[1]} {jamo_names[1]}')
t = lookup(f'HANGUL {lvt[2]} {jamo_names[2]}')
l + v + t

#
jamo_names = 'PANSIOS', 'ARAEA', 'RIEUL'
l = lookup(f'HANGUL {lvt[0]} {jamo_names[0]}')
v = lookup(f'HANGUL {lvt[1]} {jamo_names[1]}')
t = lookup(f'HANGUL {lvt[2]} {jamo_names[2]}')
l + v + t

# 'ㅈ' -> 'CIEUC'
# 'ㅏ' -> 'A'
# 'ㄹ' -> 'RIEUL'
# ??? -> 'PANSIOS'
# ??? -> 'ARAEA'
def jamo_name(jamo):
    yet_dict = {'Z': 'PANSIOS', 'A': 'ARAEA', 'V': 'KAPYEOUNPIEUP', 'K': 'PIEUP-SIOS-KIYEOK', 'T': 'PIEUP-SIOS-TIKEUT'}
    if jamo in yet_dict.keys():
        return yet_dict[jamo]
    else:
        return name(jamo).split()[-1]

'''
>>> jamo_name("ㅈ")
'CIEUC'
>>> jamo_name("ㅏ")
'A'
>>> jamo_name("ㄹ")
'RIEUL'
>>> jamo_name("Z")
'PANSIOS'
>>> jamo_name("A")
'ARAEA'
'''

jamos = 'ZAㄹ'
l = lookup(f'HANGUL {lvt[0]} {jamo_name(jamos[0])}')
v = lookup(f'HANGUL {lvt[1]} {jamo_name(jamos[1])}')
t = lookup(f'HANGUL {lvt[2]} {jamo_name(jamos[2])}')
l + v + t
# 'ᅀᆞᆯ'

# 반복을 없애자
syl = ''
for p, j in zip(lvt, jamos):
    syl += lookup(f'HANGUL {p} {jamo_name(j)}')

# 더 간단하게
''.join(lookup(f'HANGUL {p} {jamo_name(j)}') for p, j in zip(lvt, jamos))

# 함수로
def conjoin(jamos):
    return ''.join(lookup(f'HANGUL {p} {jamo_name(j)}') for p, j in zip(lvt, jamos))

'''
>>> conjoin('ㅈㅏㄹ')
'잘'
>>> conjoin('ZAㄹ')
'ᅀᆞᆯ'
>>> conjoin('Kㅜㄹ')
'ᄢᅮᆯ'
>>> conjoin('TAㄹ')
'ᄣᆞᆯ'
'''


#!/usr/bin/python3
import argparse
import subprocess

if __name__ == '__main__':
    home = subprocess.os.environ.get('HOME')

    parser = argparse.ArgumentParser(description='A simple command-line tool to manage proxy settings')
    main_args = parser.add_argument_group()

    main_args.add_argument('-S','--setProxy',action='store_true',
                        help='Set best proxy automatically')
    main_args.add_argument('-T',
                        '--torPing',
                        help='Perform ping via tor'\
                        +', Helps to keep connection alive',
                        action='store_true')
    main_args.add_argument('-C',
                        '--customProxy',
                        nargs=1,
                        help='Set your own proxy. eg. -C 172.16.24.3:3128')
    main_args.add_argument('-N',
                        '--clearProxy',
                        action='store_true',
                        help='Reset the system proxy')
    parser.add_argument('-U',
                        '--update',
                        action='store_true',
                        help='Update ProxyHelper package')
    main_args.add_argument('-G',
                        '--getProxy',
                        action='store_true',
                        help='Print the best proxy, but don\'t set it.')
    parser.add_argument('--configure',
                        action='store_true',
                        help='Configure defaults in proxyhelper')
    arg = parser.parse_args()

    if arg.setProxy:
        subprocess.call(['bash',
                        '{}/.proxyhelper/zetproxy'.format(home)])
    elif arg.torPing:
        subprocess.call(['python3',
                        '{}/.proxyhelper/torpinger'.format(home)])
    elif arg.customProxy:
        subprocess.call(['bash',
                        '{}/.proxyhelper/zetproxy'.format(home)
                        ,'Proxy',arg.customProxy[0]])
    elif arg.clearProxy:
        subprocess.call(['bash',
                        '{}/.proxyhelper/zetproxy'.format(home),
                        'None'])
    elif arg.getProxy:
        subprocess.call(['python3',
                        '{}/.proxyhelper/surely_parallel.py'.format(home)])

    elif arg.update:
        print('Updating ProxyHelper.')
    else:
        parser.print_help()


import sys
import json
import urllib.request
from json_parser.py import JSONParser
from json_widgets import JSONTableWidget, JSONTreeWidget
from PyQt5 import QtWidgets, QtCore, QtWebKitWidgets

class MyWebView(QtWebKitWidgets.QWebView):
    def __init__(self, parent=None, *args, **kwargs):
        super(MyWebView, self).__init__(parent, *args, **kwargs)

    @QtCore.pyqtSlot(QtCore.QUrl)
    def url_slot(self, url):
        self.load(url)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = QtWidgets.QMainWindow()

    json_widget = JSONTreeWidget(parent=main_window)
    webkit_view = MyWebView()
    json_widget.url_signal.connect(webkit_view.url_slot)

    horizontal_layout = QtWidgets.QHBoxLayout()
    horizontal_layout.addWidget(json_widget)
    horizontal_layout.addWidget(webkit_view)
    layout_widget = QtWidgets.QWidget()
    layout_widget.setLayout(horizontal_layout)

    main_window.setCentralWidget(layout_widget)
    main_window.show()
    sys.exit(app.exec_())


import matplotlib.pyplot as plt

def read_data(details):
    result = []
    with open(details[1]) as file:
        for line in file:
            result.append(int(line))

    return result

def run():
    indexes = []
    data = read_data(("visualisation/subplots/temps/temps.txt", "temps.txt"))
    for i in range(len(data)):
        indexes.append(i + 1)
    fig, axes = plt.subplots(1, 2)
    axes[0].plot(indexes, data)
    axes[1].bar(indexes, data)
    plt.show()

run()


t = int(input())
datas = [2, 3, 5, 7, 11]
for tc in range(t):
    n = int(input())
    count = [0] * 5
    for i in range(5):
        while True:
            if n % datas[i] == 0:
                count[i] += 1
                n = n // datas[i]
            else:
                break
    print("#{}".format(tc+1), end=' ')
    for cnt in count:
        print(cnt, end=' ')
    print()


# https://leetcode.com/problems/find-mode-in-binary-search-tree/

class TreeNode:

    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def findMode(root: TreeNode):
    count = dict()

    def dfs(node: TreeNode):
        if not node:
            return

        count.setdefault(node.val, 0)
        count[node.val] += 1
        dfs(node.left)
        dfs(node.right)

    dfs(root)

    items = list(count.items())
    items.sort(key=lambda x: x[1], reverse=True)

    ans = []
    most = items[0][1]
    for val, num in items:
        if num < most:
            break
        ans.append(val)

    return ans


from django.views.generic import TemplateView
from django.urls import path
from EntregaIntermedia import views

from django.conf import settings
from django.conf.urls.static import static
from ProyectoDjango.settings import STATIC_URL, MEDIA_URL, MEDIA_ROOT

urlpatterns = [
    path('', views.Index.as_view(), name='index'),
    path('searchuser/', views.SearchUser.as_view(), name = 'searchuser'),
    path('searchitem/', views.SearchItem.as_view(), name='searchitem'),
    path('searchvehicle/', views.SearchVehicle.as_view(), name='searchvehicle'),
    path('searchadmin', views.SearchAdmin.as_view(), name = 'searchadmin'),

    path('adduser/', views.AddUser.as_view(), name='adduser'),
    path('additem/', views.AddItem.as_view(), name='additem'),
    path('addvehicle/', views.AddVehicle.as_view(), name='addvehicle'),
    path('addadmin/', views.AddAdmin.as_view(), name = 'addadmin'),

    path('players/', views.Users.as_view(), name = 'players'),
    path('items/', views.Items.as_view(), name = 'items'),
    path('vehicles/', views.Vehicles.as_view(), name = 'vehicles'),
    path('admins/', views.Administrators.as_view(), name = 'admins'),

    # Agregados
    path('edituser/<int:editthisid>/', views.EditUser.as_view(), name = 'edituser'),
    path('deleteuser/<int:deletethisid>/', views.DeleteUser.as_view(), name = 'deleteuser'),

    path('edititem/<str:editthisitem>/', views.EditItem.as_view(), name = 'edititem'),
    path('deleteitem/<str:deletethisitem>', views.DeleteItem.as_view(), name = 'deleteitem'),

    path('editvehicle/<str:editthisvehicle>/', views.EditVehicle.as_view(), name = 'editvehicle'),
    path('deletevehicle/<str:deletethisvehicle>/', views.DeleteVehicle.as_view(), name = 'deletevehicle'),

    path('editadmin/<int:editthisid>/', views.EditAdmin.as_view(), name = 'editadmin'),
    path('deleteadmin/<int:deletethisid>/', views.DeleteAdmin.as_view(), name = 'deleteadmin'),
]

urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)


#!/usr/bin/env python3

#genera gli eventi

import rospy
from simulator.msg import event
from simulator.msg import loginfo
import time
from datetime import datetime

class GeneratorNode:
    def __init__(self, node_name, next_element, gen_freq, num_messages, event_type, node_id,attribute1,value1,attribute2,value2,speed, pause_time, group_size):
        self.node_name = node_name
        self.next_element = "/" + next_element
        self.gen_freq = gen_freq
        self.num_messages = num_messages
        self.event_type = event_type
        self.event_id = 1
        self.attribute1 = attribute1
        self.value1 = value1
        self.attribute2 = attribute2
        self.value2 = value2
        self.speed = speed
        self.group_size = group_size
        self.pause_time = pause_time
        self.pause = False
        self.execution = False
        if pause_time != 0:
            self.pause = True

        self.pub = rospy.Publisher(self.next_element, event, queue_size=50)
        self.pub_info = rospy.Publisher("/log_info", loginfo, queue_size=50)
        self.log_info_sub = rospy.Subscriber("/log_info", loginfo, self.process_log_info)
        self.log_stop_sub = rospy.Subscriber("/log_stop", loginfo, self.process_log_info)

        time.sleep(2)
        info_msg =loginfo()
        info_msg.ID_node = node_id
        info_msg.type = "generator"
        info_msg.node_name = node_name
        info_msg.events_left = num_messages
        info_msg.statistic = False
        self.pub_info.publish(info_msg)
        print(self.node_name, "-- pronto")

    def generate_messages(self):

        print(self.node_name, " start sending messages...")

        #rospy.loginfo(f"{self.node_name}: Generating {self.num_messages} messages...")
        i = 0
        count = 0
        end = rospy.Time.now()
        start = rospy.Time.now()
        dt = (1 / self.gen_freq)/self.speed
        event_msg = event()
        event_msg.generator_id = self.node_name
        event_msg.type = self.event_type
        event_msg.route = [self.node_name]
        #event_msg.last_event = False
        event_msg.split_attribute1 = self.attribute1
        event_msg.split1 = self.value1
        event_msg.split_attribute2 = self.attribute2
        event_msg.split2 = self.value2
        error_count = 0
        while not rospy.is_shutdown():

            if i < self.num_messages and self.execution:
                inizio_timer = rospy.Time.now()
                event_msg.ID = self.event_id
                event_msg.generation_date = rospy.Time.now()
                #event_msg.gen_time = self.stamp_date()
                # if i == 0:
                #     event_msg.first_event = True
                self.event_id += 1
                i += 1
                self.pub.publish(event_msg)
                rospy.loginfo(f"{self.node_name}: Published event ID {event_msg.ID}")
                tempo_trascorso = rospy.Time.now() - inizio_timer
                tempo_di_attesa_rimanente = dt - tempo_trascorso.to_sec()
                if tempo_di_attesa_rimanente > 0:
                    time.sleep(tempo_di_attesa_rimanente)
                else:
                    error_count += 1

                if i == self.num_messages:
                    end = rospy.Time.now()
                    ct = (end-start).to_sec()/self.num_messages
                    print("REAL COMPUTING TIME ",ct, "totale exed ", error_count)

                if self.pause:
                    count += 1
                    if count == self.group_size:
                        count = 0
                        print("sliping time: ", self.pause_time/self.speed)
                        time.sleep(self.pause_time/self.speed)

    def stamp_date(self):
        completed_date = rospy.Time.now().to_sec()  # Ottieni il timestamp in secondi
        # Crea un oggetto datetime dalla data formattata
        formatted_date = datetime.fromtimestamp(completed_date)
        # Formatta la data secondo il tuo formato desiderato
        date_string = formatted_date.strftime('%d-%m-%y %H:%M:%S')
        return date_string

    def process_log_info(self,msg):

        print("da generatore RECIEVED ", msg.node_name)
        if msg.type == "end_node":
            if msg.start_esecution == True:
                self.execution = True
                self.generate_messages()

            if msg.stop_esecution == True:
                print(self.node_name, " stop and kill")
                self.execution = False
                info_msg =loginfo()
                info_msg.type = "generator"
                info_msg.node_name = self.node_name
                info_msg.events_left = (self.event_id -1)
                info_msg.statistic = True
                self.pub_info.publish(info_msg)
                rospy.signal_shutdown('Chiusura generatore')
        # if msg.type == "coda":
        #     if msg.statistic == True:
        #         self.execution = False
        #         print("stop per coda!!")

if __name__ == '__main__':
    rospy.init_node(rospy.get_param("~node_name", "generator"))
    node_id = rospy.get_param("~node_id", 1)
    node_name = rospy.get_param("~node_name", "generator")
    next_element = rospy.get_param("~next_element", "next_element")
    gen_freq = rospy.get_param("~gen_freq", 1.0)
    num_messages = rospy.get_param("~num_messages", 10)
    event_type = rospy.get_param("~event_type", "evento_semplice")
    speed = rospy.get_param("~speed", 1)
    pause_time = rospy.get_param("~pause_time", 0)
    group_size  = rospy.get_param("~group_size", 1)

    attribute1 = rospy.get_param("~split_attribute1", "-")
    value1 = rospy.get_param("~split1", 0.0)
    attribute2 = rospy.get_param("~split_attribute2", "-")
    value2 = rospy.get_param("~split2", 0.0)

    generator = GeneratorNode(node_name, next_element, gen_freq, num_messages, event_type, node_id,attribute1,value1,attribute2,value2,speed,pause_time, group_size)
    rospy.spin()


import time
from time import sleep
from plyer import notification
import random
import credentials
from win32com.client import Dispatch
from datetime import datetime

def speak(str):
    speak= Dispatch(("SAPI.SpVoice"))
    speak.Speak(str)

date= datetime.now()
hour=time.strftime("%I")
min=time.strftime("%M")

ans=date.weekday()

""" get time feature"""
""" Week day: 1 2 3 4 5 6 7"""
""" printing quotes acc to weekdays"""
""" motivating reminder each time you run the code"""

d2={0:"You are a rockstar", 1:"Believe you can and you will",2:"Try until you don't see test case accepted", 3: "If you feel like giving up, just remember why you started", 4: "Stars can't shine without nights", 5:"When you were born, this world recieved a gem", 6:"Be you because that's your power!!"}

if hour=="04" and min=="24" or hour=="01" and min=="00" or hour=="09"and min=="00":
    speak(d2[ans])

if hour=="03" and min=="22" or hour=="12" and min=="00" or hour=="08"and min=="00":
    speak("**Please Drink Water, Dear little superstar!!")

while True:
    notification.notify(
        title= "Check your to do once",
        message= "Start, grind, don't give up!",
        timeout=12
    )
    time.sleep(60*60)


# Databricks notebook source
ID_FIELDS = [
  "PSEUDO_HESID",
  "SUSRECID",
  "AEKEY",
  "EPIKEY",
  "ATTENDNO",
  "ATTENDKEY",
  "ATTENDID",
  "PREFERER",
]

OTHER_SENSITIVE_FIELDS = []

DERIVED_FIELDS = [
  *[f"OPERTN_3_{i+1:02}" for i in range(24)],  # Derived from OPERTN_4 (APC & OP)
  *[f"DIAG_3_{i+1:02}" for i in range(20)],    # Derived from DIAG_4 (APC & OP)
  *[f"DIAG2_{i+1:02}" for i in range(12)],     # Derived from DIAG3 (AE)
  *[f"TREAT2_{i+1:02}" for i in range(12)],    # Derived from TREAT3 (AE)
  "OPERTN_3_CONCAT",
  "OPERTN_4_CONCAT",
  "OPERTN_COUNT",
  "DIAG_3_CONCAT",
  "DIAG_4_CONCAT",
  "DIAG_COUNT",
  "STARTAGE_CALC",   # Derived from MYDOB and ADMIDATE (APC)
  "STARTAGE",        # Derived from MYDOB and ADMIDATE (APC)
  "APPTAGE_CALC",    # Can't be derived from MYDOB because this isn't in HES Non Sensitive! Derive from APPTAGE (OP)
  # "APPTAGE",       # Can't be derived from MYDOB because this isn't in HES Non Sensitive! (OP)
  "ARRIVALAGE_CALC", # Can't be derived from MYDOB because this isn't in HES Non Sensitive! Derive from ARRIVALAGE (AE)
  # "ARRIVALAGE",    # Can't be derived from MYDOB because this isn't in HES Non Sensitive! (AE)
]

EXCLUDED_FIELDS = [
  *ID_FIELDS,
  *OTHER_SENSITIVE_FIELDS,
  *DERIVED_FIELDS,
]

# COMMAND ----------


"""Implementation of the Hopper environment supporting
domain randomization optimization."""

from copy import deepcopy
from multiprocessing.sharedctypes import Value

import numpy as np
import gym
from gym import utils
from .mujoco_env import MujocoEnv
import torch
from torch.distributions.uniform import Uniform
from torch.nn.init import trunc_normal_ as tn

class CustomHopper(MujocoEnv, utils.EzPickle):
    def __init__(self, domain=None):
        MujocoEnv.__init__(self, 4)
        utils.EzPickle.__init__(self)

        self.original_masses = np.copy(self.sim.model.body_mass[1:])    # Default link masses

        if domain == 'source':  # Source environment has an imprecise torso mass (1kg shift) with respect to the
            self.sim.model.body_mass[1] -= 1.0

        self.random_masses = 3

    def set_parametrization(self, bounds, dist_type='uniform'):
        """
        This function sets the parameters useful for customizing the employed parametric distribution.
        """
        if dist_type.lower()=='uniform':
            self.low_uniform_1 = bounds[0]
            self.high_uniform_1 = bounds[1]
            self.low_uniform_2 = bounds[2]
            self.high_uniform_2 = bounds[3]
            self.low_uniform_3 = bounds[4]
            self.high_uniform_3 = bounds[5]
        elif dist_type.lower()=='normal':
            self.low_normal = bounds[0]
            self.high_normal = bounds[1]
            self.mean_normal_1 = bounds[2]
            self.std_normal_1 = bounds[3]
            self.mean_normal_2 = bounds[4]
            self.std_normal_2 = bounds[5]
            self.mean_normal_3 = bounds[6]
            self.std_normal_3 =bounds[7]
        else:
            raise ValueError("Invalid parametric distribution selected. Please try Uniform or Normal.")

    # def set_random_parameters(self):
    #     #Set random masses
    #     self.set_parameters(*np.hstack((np.array(self.sim.model.body_mass[1]), self.sample_parameters())))

    def sample_parameters(self, dist_type='uniform'):
        """
        Sample masses according to a domain randomization distribution
        -----
        This function samples masses for a Uniform Distribution. Sampling is done independently for each component of the
        vector.
        """
        if dist_type.lower()=='uniform':
            self.MassDistribution1 = Uniform(low = torch.tensor([self.low_uniform_1], dtype = float),
                                            high = torch.tensor([self.high_uniform_1], dtype = float),
                                            validate_args = False)
            self.MassDistribution2 = Uniform(low = torch.tensor([self.low_uniform_2], dtype = float),
                                            high = torch.tensor([self.high_uniform_2], dtype = float),
                                            validate_args = False)
            self.MassDistribution3 = Uniform(low = torch.tensor([self.low_uniform_3], dtype = float),
                                            high = torch.tensor([self.high_uniform_3], dtype = float),
                                            validate_args = False)

            randomized_masses = np.array([
            self.MassDistribution1.sample().detach().numpy(),
            self.MassDistribution2.sample().detach().numpy(),
            self.MassDistribution3.sample().detach().numpy()
            ])

        elif dist_type.lower()=='normal':
            empty_tensor = torch.empty(1,1)
            self.MassDistribution1 = tn(empty_tensor, a=self.low, b=self.high, mean=self.mean_normal_1, std=self.std_normal_1)
            self.MassDistribution2 = tn(empty_tensor, a=self.low, b=self.high, mean=self.mean_normal_2, std=self.std_normal_2)
            self.MassDistribution3 = tn(empty_tensor, a=self.low, b=self.high, mean=self.mean_normal_3, std=self.std_normal_3)

            randomized_masses = np.array([
            self.MassDistribution1.detach().numpy(),
            self.MassDistribution2.detach().numpy(),
            self.MassDistribution3.detach().numpy()
            ])

        else:
            raise ValueError("Parametric distribution not supported! Try normal or uniform.")

        return randomized_masses.reshape(-1,)

    def set_random_parameters(self, masses=['tigh', 'leg', 'foot'], dist_type='uniform'):
        """
        This function indicates which mass has to be randomized according to self.set_parametrization() and self.sample_parameters()
        """
        masses_map = {
            'tigh':1,
            'leg':2,
            'foot':3
        }
        if bool(masses):
            randomized_masses = self.sample_parameters(dist_type=dist_type)
            for mass in masses:
                self.sim.model.body_mass[masses_map[mass]+1] = randomized_masses[masses_map[mass]-1]

    def get_parameters(self):
        """Get value of mass for each link"""
        masses = np.array( self.sim.model.body_mass[1:] )
        return masses

    def set_parameters(self, *task):
        """Set each hopper link's mass to a new value"""
        self.sim.model.body_mass[1:] = task

    def step(self, a):
        """Step the simulation to the next timestep

        Parameters
        ----------
        a : ndarray,
            action to be taken at the current timestep
        """
        posbefore = self.sim.data.qpos[0]
        self.do_simulation(a, self.frame_skip)
        posafter, height, ang = self.sim.data.qpos[0:3]
        alive_bonus = 1.0
        reward = (posafter - posbefore) / self.dt
        reward += alive_bonus
        reward -= 1e-3 * np.square(a).sum()
        s = self.state_vector()
        done = not (np.isfinite(s).all() and (np.abs(s[2:]) < 100).all() and (height > .7) and (abs(ang) < .2))
        ob = self._get_obs()

        return ob, reward, done, {}

    def _get_obs(self):
        """Get current state"""
        return np.concatenate([
            self.sim.data.qpos.flat[1:],
            self.sim.data.qvel.flat
        ])

    def reset_model(self):
        """Reset the environment to a random initial state"""
        qpos = self.init_qpos + self.np_random.uniform(low=-.005, high=.005, size=self.model.nq)
        qvel = self.init_qvel + self.np_random.uniform(low=-.005, high=.005, size=self.model.nv)
        self.set_state(qpos, qvel)
        return self._get_obs()

    def viewer_setup(self):
        self.viewer.cam.trackbodyid = 2
        self.viewer.cam.distance = self.model.stat.extent * 0.75
        self.viewer.cam.lookat[2] = 1.15
        self.viewer.cam.elevation = -20

    def set_mujoco_state(self, state):
        """Set the simulator to a specific state

        Parameters:
        ----------
        state: ndarray,
               desired state
        """
        mjstate = deepcopy(self.get_mujoco_state())

        mjstate.qpos[0] = 0.
        mjstate.qpos[1:] = state[:5]
        mjstate.qvel[:] = state[5:]

        self.set_sim_state(mjstate)

    def set_sim_state(self, mjstate):
        """Set internal mujoco state"""
        return self.sim.set_state(mjstate)

    def get_mujoco_state(self):
        """Returns current mjstate"""
        return self.sim.get_state()

"""
    Registered environments
"""
gym.envs.register(
        id="CustomHopper-v0",
        entry_point="%s:CustomHopper" % __name__,
        max_episode_steps=500,
)

gym.envs.register(
        id="CustomHopper-source-v0",
        entry_point="%s:CustomHopper" % __name__,
        max_episode_steps=500,
        kwargs={"domain": "source"}
)

gym.envs.register(
        id="CustomHopper-target-v0",
        entry_point="%s:CustomHopper" % __name__,
        max_episode_steps=500,
        kwargs={"domain": "target"}
)


import argparse
import sys
import time
from functools import partial

import numpy as np
import tritonclient.grpc as grpcclient
from tritonclient.utils import InferenceServerException
label=['F', 'N', 'Q','V']
parser = argparse.ArgumentParser()
parser.add_argument(
    "-v",
    "--verbose",
    action="store_true",
    required=False,
    default=False,
    help="Enable verbose output",
)
parser.add_argument(
    "-u",
    "--url",
    type=str,
    required=False,
    default="140.116.233.113:8001",
    help="Inference server URL. Default is localhost:8001.",
)
parser.add_argument(
    "-t",
    "--client-timeout",
    type=float,
    required=False,
    default=None,
    help="Client timeout in seconds. Default is None.",
)

FLAGS = parser.parse_args()
try:
    triton_client = grpcclient.InferenceServerClient(
        url=FLAGS.url, verbose=FLAGS.verbose
    )
except Exception as e:
    print("context creation failed: " + str(e))
    sys.exit()

FLAGS = parser.parse_args()
try:
    triton_client = grpcclient.InferenceServerClient(
        url=FLAGS.url, verbose=FLAGS.verbose
    )
except Exception as e:
    print("context creation failed: " + str(e))
    sys.exit()

model_name = "CWT_CNN"
# Infer
inputs = []
outputs = []
inputs.append(grpcclient.InferInput("input.1", [1,1, 368], "FP32"))
outputs.append(grpcclient.InferRequestedOutput("280"))

def callback(user_data, result, error):
    if error:
        user_data.append(error)
    else:
        user_data.append(result)

def CNN_processing(seg_list):
    pred_list=[]
    user_data = []
    for i, seg in enumerate(seg_list):
        # 創建 PILLOW
        # image = Image.fromarray((image * 255).astype(np.uint8))
        # input_data = transform(image).unsqueeze(0).to(device)
        seg=np.float32(seg)
        seg = seg[None,None,:]

        num_examples  = seg.shape[0]
        signal_length = seg.shape[-1]
        # 進行推理
        inputs[0].set_data_from_numpy(seg)

        triton_client.async_infer(
        model_name=model_name,
        inputs=inputs,
        callback=partial(callback, user_data),
        outputs=outputs,
        client_timeout=FLAGS.client_timeout)
    time_out = 1000
    while (len(user_data) == 0) and time_out > 0:
        time_out = time_out - 1
        time.sleep(0.001)
    # Display and validate the available results
    if len(user_data) >=1:
        # Check for the errors
        if type(user_data[0]) == InferenceServerException:
            print(user_data[0])
            sys.exit(1)
        # Validate the values by matching with already computed expected
        # values.
        for ind in range(len(user_data)):
            output=user_data[ind].as_numpy("280")
            #印出結果
            pred = np.argmax(output)
            label_index=pred.item()
            pred_list.append(label[label_index])

    return pred_list


# 2) реализовать метакласс. позволяющий создавать всегда один и тот же объект класса (см. урок)

class MyMetaClass(type):

    def __init__(cls, *args, **kwargs):
        cls.a = None
        super().__init__(*args, **kwargs)

    def __call__(cls, *args, **kwargs):
        if cls.a == None:
            cls.a = super().__call__(*args, **kwargs)
            cls.a = cls.a = super().__call__(*args, **kwargs)
            return cls.a
        else:
            return cls.a

class MyClass(metaclass=MyMetaClass):

    def method_1(self):
        pass

obj1 = MyClass()
obj2 = MyClass()

print(type(obj1))
print(type(obj2))
print(obj1 == obj2)
print(obj1 is obj2)


import asyncio
import threading

import websockets
import random
import RPi.GPIO as GPIO
import time

# ######################### RASPBERRY_PART ######################### #

makerobo_RelayPin = 13  # 定义继电器管脚为Pin13
makerobo_TRIG = 11  # 超声波模块Tring控制管脚
makerobo_ECHO = 12  # 超声波模块Echo控制管脚
makerobo_BeepPin = 16

max_distance = 25
current_distance = 0
relay_state = False
relay_lock_state = "relay_lock_normal"  # relay_lock_on relay_lock_normal relay_lock_off
relay_thread = None

# 初始化工作
def makerobo_setup():
    global BEEP
    GPIO.setmode(GPIO.BOARD)  # 采用实际的物理管脚给GPIO口
    # GPIO.setwarning(False)  # 去除GPIO警告

    # 继电器初始化
    GPIO.setup(makerobo_RelayPin, GPIO.OUT)  # 设置Pin模式为输出模式
    GPIO.output(makerobo_RelayPin, GPIO.LOW)  # 关闭继电器

    # 超声波初始化
    GPIO.setup(makerobo_TRIG, GPIO.OUT)  # Tring设置为输出模式
    GPIO.setup(makerobo_ECHO, GPIO.IN)  # Echo设置为输入模式

    # 蜂鸣器初始化
    GPIO.setup(makerobo_BeepPin, GPIO.OUT, initial= GPIO.HIGH)

# 释放资源
def makerobo_destroy():
    GPIO.output(makerobo_RelayPin, GPIO.LOW)  # 关闭继电器
    GPIO.cleanup()  # 释放资源

# # 超声波计算距离函数
def ur_disMeasure():
    GPIO.output(makerobo_TRIG, 0)  # 开始起始
    time.sleep(0.000002)  # 延时2us

    GPIO.output(makerobo_TRIG, 1)  # 超声波启动信号，延时10us
    time.sleep(0.00001)  # 发出超声波脉冲
    GPIO.output(makerobo_TRIG, 0)  # 设置为低电平

    while GPIO.input(makerobo_ECHO) == 0:  # 等待回传信号
        us_a = 0
    us_time1 = time.time()  # 获取当前时间
    while GPIO.input(makerobo_ECHO) == 1:  # 回传信号截止信息
        us_a = 1
    us_time2 = time.time()  # 获取当前时间

    us_during = us_time2 - us_time1  # 转换微秒级的时间

    # 声速在空气中的传播速度为340m/s, 超声波要经历一个发送信号和一个回波信息，
    # 计算公式如下所示：
    return us_during * 340 / 2 * 100  # 求出距离

# 继电器自动控制器
def relay_controller(distance):
    # 超出距离,关闭继电器
    # while True:
    global relay_state, relay_lock_state
    if relay_lock_state == "relay_lock_on":
        print("常开控制")
        # GPIO.output(makerobo_BeepPin, GPIO.LOW)
        relay_state = True
    if relay_lock_state == "relay_lock_off":
        print("常闭控制")
         #GPIO.output(makerobo_BeepPin, GPIO.LOW)
        relay_state = False
    if relay_lock_state == "relay_lock_normal":
        print("正常控制")
        if distance >= max_distance:
            GPIO.output(makerobo_RelayPin, GPIO.LOW)
            relay_state = False
        if distance <= max_distance:
            GPIO.output(makerobo_RelayPin, GPIO.HIGH)
            relay_state = True

async def create_disThread_loop():
    global current_distance
    while True:
        # us_dis = random.randint(20, 30)
        us_dis = ur_disMeasure()  # 获取超声波计算距离
        print("距离", us_dis)
        current_distance = us_dis
        relay_controller(current_distance)
        GPIO.output(makerobo_BeepPin, GPIO.HIGH)
        await asyncio.sleep(1)

# ######################### WEBSOCKETS ######################### #

wsServer = "ws://192.168.43.72:8188/"
global_ws = None

def createDataFrame(dataType, payload):
    frame = "rasp," + str(dataType) + "," + str(payload)
    return frame

async def on_message(message):
    print("Received message:", message)

async def on_error(error):
    print("WebSocket error:", error)

async def on_close():
    print("WebSocket connection closed")

def msg_handler(message):
    global relay_state, relay_lock_state
    decoded_message = message.decode()
    # 解析数据帧
    if "," in decoded_message:
        parts = decoded_message.split(",")
        clientName = parts[0]
        clientFunc = parts[1]
        clientPayload = parts[2]
    else:
        clientName = ""
        clientFunc = ""
        clientPayload = ""
    if clientName == "H5":
        if clientFunc == "command":

            if clientPayload == "relay_lock_on":
                print("常开继电器")
                GPIO.output(makerobo_BeepPin, GPIO.LOW)
                GPIO.output(makerobo_RelayPin, GPIO.HIGH)
                relay_lock_state = clientPayload

            if clientPayload == "relay_lock_off":
                print("常闭继电器")
                GPIO.output(makerobo_BeepPin, GPIO.LOW)
                GPIO.output(makerobo_RelayPin, GPIO.LOW)
                relay_lock_state = clientPayload

            if clientPayload == "relay_lock_normal":
                print("自控继电器")
                GPIO.output(makerobo_BeepPin, GPIO.HIGH)
                if relay_lock_state == "relay_lock_on" or relay_lock_state == "relay_lock_off":
                    GPIO.output(makerobo_BeepPin,GPIO.LOW)
                relay_lock_state = clientPayload

            if clientPayload == "relay_on":
                print("打开继电器")
                GPIO.output(makerobo_RelayPin, GPIO.HIGH)
                relay_state = True

            if clientPayload == "relay_off":
                print("关闭继电器")
                GPIO.output(makerobo_RelayPin, GPIO.LOW)
                relay_state = False

    # else:
    #     print("非H5不处理")

async def send_message(websocket):
    global relay_state, current_distance
    while True:
        # 模拟需要发送的数据，这里使用字符串 "data" 作为示例
        dis_data = createDataFrame("data_dis", current_distance)
        print(dis_data)
        # 发送数据到服务器
        await websocket.send(dis_data)
        if relay_state:
            relay_data = createDataFrame("state_dis", "relay_on")
        else:
            relay_data = createDataFrame("state_dis", "relay_off")
        await websocket.send(relay_data)
        # 等待一秒
        await asyncio.sleep(1)

async def receive_message(websocket):
    while True:
        # 接收服务器发送的数据
        message = await websocket.recv()
        msg_handler(message)

async def connect_websocket():
    try:
        async with websockets.connect(wsServer) as ws:
            global global_ws
            global_ws = ws
            disThread_task = asyncio.ensure_future(create_disThread_loop())
            tasks = [send_message(ws), receive_message(ws), disThread_task]
            await asyncio.gather(*tasks)
    except KeyboardInterrupt:
        await on_close()
    except Exception as e:
        await on_error(e)
       #  makerobo_destroy()

if __name__ == '__main__':
    makerobo_setup()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(connect_websocket())
    loop.close()


import logging

from ocgull.constants import ProtectionStatus

logger = logging.getLogger(__name__)

class Ocgull():
    """
    Gull that's responsible for patrolling the dock for available Oc1s.
    """

    def __init__(self, spreadsheet_repo, prev_spreadsheet_repo, notifier):
        self.spreadsheet_repo = spreadsheet_repo
        self.prev_spreadsheet_repo = prev_spreadsheet_repo
        self.notifier = notifier

    def patrol(self):
        """Patrol the dock and caw caw if potential OCs are found."""

        spreadsheet = self.spreadsheet_repo.fetch()
        prev_spreadsheet = self.prev_spreadsheet_repo.fetch()

        unlocked_sheets = self._get_recently_unlocked_sheets(spreadsheet, prev_spreadsheet)
        if unlocked_sheets:
            self.notifier.send_notification(unlocked_sheets)
            self.prev_spreadsheet_repo.save_snapshot(spreadsheet)

        return unlocked_sheets

    def _get_recently_unlocked_sheets(self, spreadsheet, prev_spreadsheet):
        prev_unlocked_sheets = set(sheet for sheet in prev_spreadsheet.sheets
                if sheet.protection == ProtectionStatus.UNLOCKED)
        curr_unlocked_sheets = set(sheet for sheet in spreadsheet.sheets
                if sheet.protection == ProtectionStatus.UNLOCKED)

        result = curr_unlocked_sheets - prev_unlocked_sheets
        logger.info("Recently unlocked sheets: {}.".format([s.title for s in result]))
        return list(result)


import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import pickle
import cv2
import glob
import time
import collections
from random import randint
from scipy.ndimage.measurements import label
from skimage.feature import hog
from class_functions import *
from utils import *
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC, SVC
from sklearn.preprocessing import StandardScaler
from scipy.ndimage.measurements import label
from moviepy.editor import VideoFileClip

record_video = True

if record_video == True:
    show_plot = False
else:
    show_plot = True

# Read in cars and notcars
cars = glob.glob('training_data/vehicles/**/*.png', recursive=True)
notcars = glob.glob('training_data/non-vehicles/**/*.png', recursive=True)
#cars = glob.glob('training_data_subset/vehicles_smallset/**/*.jpeg', recursive=True)
#notcars = glob.glob('training_data_subset/non-vehicles_smallset/**/*.jpeg', recursive=True)

# load car images
car_images = []
for image_path in cars:
    if image_path.endswith('.png'):
        image = cv2.imread(image_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        #augmented_image = cv2.bitwise_not(image)
    else:
        image = mpimg.imread(image_path)
    car_images.append(image)
    #car_images.append(augmented_image)

# load non-car images
notcar_images = []

for image_path in notcars:
    if image_path.endswith('.png'):
        image = cv2.imread(image_path)
        augmented_image = cv2.flip(image,1)
        augmented_image2 = cv2.flip(image,0)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        augmented_image = cv2.cvtColor(augmented_image, cv2.COLOR_BGR2RGB)
        augmented_image2 = cv2.cvtColor(augmented_image2, cv2.COLOR_BGR2RGB)
    else:
        image = mpimg.imread(image_path)
        augmented_image = cv2.flip(image,1)
        augmented_image2 = cv2.flip(image,0)
    notcar_images.append(image)
    notcar_images.append(augmented_image)
    notcar_images.append(augmented_image2)

car_images_count = len(car_images)
notcar_images_count = len(notcar_images)

print ('dataset has cars:', car_images_count)
print ('and not cars:', notcar_images_count)

# create a small subset of images for visualization
example_images = []
titles = []
for i in range(0,4):
    example_images.append(car_images[randint(0, car_images_count-1)])
    titles.append("car")
for i in range(0,4):
    example_images.append(notcar_images[randint(0, notcar_images_count-1)])
    titles.append("not car")

if show_plot == True:
    plot_images(example_images, (4, 2), fig_size=(10, 5),titles=titles)

# parameters of feature extraction
color_space = 'YCrCb' # Can be GRAY, RGB, HSV, LUV, HLS, YUV, YCrCb
orient = 32  # HOG orientations
pix_per_cell = 8 # HOG pixels per cell
cell_per_block = 2 # HOG cells per block
hog_channel = 0 # Can be 0, 1, 2, or "ALL"
spatial_size = (16, 16) # Spatial binning dimensions
hist_bins = 32    # Number of histogram bins
spatial_feat = True # Spatial features on or off
hist_feat = True # Histogram features on or off
hog_feat = True # HOG features on or off

# subsample examples for extracting hog features
images_for_features = list (example_images [1:-1])
for i in range(len(images_for_features)):
    if hog_channel == 'ALL':
        channel = 0
        print("Warning! 'ALL' option for displaying is not supported")
    else:
        channel = hog_channel
    images_for_features[i] = convert_colorspace(images_for_features[i],cspace=color_space,channel=channel)

hog_features_examples = []
hog_features_examples.extend (images_for_features)

for img in images_for_features:
    features, hog_image = get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=True)
    hog_features_examples.append (hog_image)

if show_plot == True:
    plot_images(hog_features_examples, (6, 2), fig_size=(20, 6))

car_features = extract_features(car_images, color_space=color_space,
                                spatial_size=spatial_size, hist_bins=hist_bins,
                                orient=orient, pix_per_cell=pix_per_cell,
                                cell_per_block=cell_per_block,
                                hog_channel=hog_channel, spatial_feat=spatial_feat,
                                hist_feat=hist_feat, hog_feat=hog_feat)
notcar_features = extract_features(notcar_images, color_space=color_space,
                                   spatial_size=spatial_size, hist_bins=hist_bins,
                                   orient=orient, pix_per_cell=pix_per_cell,
                                   cell_per_block=cell_per_block,
                                   hog_channel=hog_channel, spatial_feat=spatial_feat,
                                   hist_feat=hist_feat, hog_feat=hog_feat)

# Create an array stack, NOTE: StandardScaler() expects np.float64
X = np.vstack((car_features, notcar_features)).astype(np.float64)
# Fit a per-column scaler
X_scaler = StandardScaler().fit(X)
# Apply the scaler to X
scaled_X = X_scaler.transform(X)

# Define the labels vector
y = np.hstack((np.ones(len(car_features)), np.zeros(len(notcar_features))))

# Split up data into randomized training and test sets
random_state = randint(0, 100)
X_train, X_test, y_train, y_test = train_test_split(scaled_X, y, test_size=0.2, random_state=random_state)

print('Using:',orient,'orientations',pix_per_cell,
      'pixels per cell and', cell_per_block,'cell(s) per block')
print('Feature vector length:', len(X_train[0]))

# Use a linear SVC
svc = LinearSVC()
# Check the training time for the SVC
t=time.time()
svc.fit(X_train, y_train)
t2 = time.time()
print(round(t2-t, 2), 'Seconds to train SVC...')
# Check the score of the SVC
print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))
# Check the prediction time for a single sample
t=time.time()

#circular buffer for filtering
heat_buffer = collections.deque(maxlen=25)#20

#define sliding window search parameters
ystarts = [ 360, 380, 380]
ystops =  [ 500, 550, 670]
scales =  [ 1.0, 1.5, 2.5]

def process_image(image):

    car_boxes = find_cars(image, color_space, ystarts, ystops, scales, svc, X_scaler, hog_channel, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)

    #draw boxes
    #out_img = np.copy(image)
    #for c1, c2 in car_boxes:
        #cv2.rectangle(out_img, c1, c2, (0,0,255), 6)

    #plt.imshow(out_img)
    #plt.show ()

    heat = np.zeros_like(image[:,:,0]).astype(np.float)

    # Add heat to each box in box list
    heat = add_heat(heat,car_boxes)
    heat = running_average(heat_buffer, heat)

    # Apply threshold to help remove false positives
    heat = apply_threshold(heat,4)#5 is the best so far with 20 filter length combination

    # Visualize the heatmap when displaying
    heatmap = np.clip(heat, 0, 255)

    # Find final boxes from heatmap using label function
    labels = label(heatmap)

    draw_img = draw_labeled_bboxes(np.copy(image), labels)

    if record_video == True:
        return draw_img
    else:
        return draw_img, heatmap

#Video processing
if record_video == True:
    clip = VideoFileClip("project_video.mp4")#.subclip(24,30)#.subclip(20,25)
    new_clip = clip.fl_image( process_image )
    new_clip.write_videofile("project_video_processed.mp4", audio=False)
else:
    #load test image
    images = glob.glob('test_images/*.jpg')
    #image = mpimg.imread('test_images/test3.jpg')
    for image_path in images:
        image = mpimg.imread(image_path)
        draw_img, heatmap = process_image(image)
        fig = plt.figure()
        plt.subplot(121)
        plt.imshow(draw_img)
        plt.title('Car Positions')
        plt.subplot(122)
        plt.imshow(heatmap, cmap='hot')
        plt.title('Heat Map')
        fig.tight_layout()
        plt.show ()


from collections import defaultdict, deque
import heapq
import sys
# input = sys.stdin.readline

sys.stdin = open('input.txt', 'r')
N, E = map(int, input().split())
graph = defaultdict(list)
INF = 1e10

for _ in range(E):
  a, b, c = map(int, input().split())
  graph[a].append([c, b])
  graph[b].append([c, a])

v1, v2 = sorted(map(int, input().split()))

def dijkstra(start, end):
  distance = [INF] * (N + 1)
  distance[start] = 0
  heap = []
  heapq.heappush(heap, [0, start])

  while heap:
    currentDistance, currentNode = heapq.heappop(heap)

    if currentNode == end:
      continue

    for connectedDistance, connectedNode in graph[currentNode]:
      newDistance = connectedDistance + currentDistance
      if distance[connectedNode] > newDistance:
        distance[connectedNode] = newDistance
        heapq.heappush(heap, [newDistance, connectedNode])

  return distance[end]

specificDistance = dijkstra(v1, v2)
v1toEnd = dijkstra(1, v1) + specificDistance + dijkstra(v2, N)
v2toEnd = dijkstra(1, v2) + specificDistance + dijkstra(v1, N)

if v1toEnd == INF and v2toEnd == INF:
  print(-1)
else:
  print(min(v1toEnd, v2toEnd))

백준알고리즘/Class4/특정한 최단 경로.py

특정한 최단 경로.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PROJECT EULER - PROBLEM 3
LARGEST PRIME FACTOR

https://projecteuler.net/problem=3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?
"""

def largestPrimeFactor(n):
    if n%2==0:
        while n%2==0:
            n = n//2

    k = 3
    while k*k <= n:
        if n%k == 0:
            while n%k == 0:
                n=n//k
        k += 2

    return n

print(largestPrimeFactor(600851475143))


"""
Program Name= Queues: A Tale of Two Stacks;
Purpose: To build a queue with the help of two stacks.
"""

class MyQueue(object):
    def __init__(self):
        self.stackNewestOnTop = []
        self.stackOldestOnTop = []

    def peek(self):
        self.shiftStacks()
        return self.stackOldestOnTop[-1]

    def dequeue(self):
        self.shiftStacks()
        return self.stackOldestOnTop.pop()

    def enqueue(self, value):
        self.stackNewestOnTop.append(value)

    def shiftStacks(self):
        if len(self.stackOldestOnTop) == 0:
            while len(self.stackNewestOnTop) != 0:
                self.stackOldestOnTop.append(self.stackNewestOnTop.pop())

queue = MyQueue()
t = int(input())
for line in range(t):
    values = map(int, input().split())
    values = list(values)
    if values[0] == 1:
        queue.enqueue(values[1])
    elif values[0] == 2:
        queue.dequeue()
    else:
        print(queue.peek())


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 24 14:55:01 2017

@author: braatenj
"""

import gdal
import subprocess
from glob import glob
import os

def get_dims(fileName):
  src = gdal.Open(fileName)
  ulx, xres, xskew, uly, yskew, yres  = src.GetGeoTransform()
  sizeX = src.RasterXSize
  sizeY = src.RasterYSize
  lrx = ulx + (sizeX * xres)
  lry = uly + (sizeY * yres)
  return [ulx,uly,lrx,lry,xres,-yres,sizeX,sizeY]

vrtFile = '/vol/v1/proj/nccn/gee_idl_lt_compare/raster/idl_2012_vrt/idl_2012_greatest_fast_dist_mmu11.vrt'
boundsFile = '/vol/v1/proj/nccn/gee_idl_lt_compare/raster/mora/nccn_gee_vs_idl_mora_ftv_nbr.bsq'
clipFile = '/vol/v1/proj/nccn/gee_idl_lt_compare/vector/LPa01_LEWI_MORA_NOCA_OLYM.geojson'
outFile = '/vol/v1/proj/nccn/gee_idl_lt_compare/raster/idl_2012_vrt/idl_2012_greatest_fast_dist_mmu11_mora.bsq'

dims = get_dims(boundsFile)
projwin = "{0} {1} {2} {3}".format(dims[0],dims[1],dims[2],dims[3]);
ext = "{0} {3} {2} {1}".format(dims[0],dims[1],dims[2],dims[3]);

tempFile = outFile.replace('.bsq', '_temp.bsq')
cmdTrans = 'gdal_translate -q --config GDAL_DATA "/usr/lib/anaconda/share/gdal" -of ENVI -projwin '+projwin+' '+ vrtFile + ' ' + tempFile
cmdWarp = 'gdalwarp -of ENVI -dstnodata None -q -tr 30 30 -cutline '+clipFile+' -te '+ ext+' '+tempFile+' '+outFile

subprocess.call(cmdTrans, shell=True)
subprocess.call(cmdWarp, shell=True)

delete = glob(os.path.dirname(outFile)+'/*temp*')
if len(delete) != 0:
  for this in delete:
    os.remove(this)


from __future__ import absolute_import, division, unicode_literals

import re
import os
import unittest

try:
    unittest.TestCase.assertEqual
except AttributeError:
    unittest.TestCase.assertEqual = unittest.TestCase.assertEquals

from .support import get_data_files, TestData, test_dir, errorMessage
from html5lib import HTMLParser, inputstream

class Html5EncodingTestCase(unittest.TestCase):
    def test_codec_name_a(self):
        self.assertEqual(inputstream.codecName("utf-8"), "utf-8")

    def test_codec_name_b(self):
        self.assertEqual(inputstream.codecName("utf8"), "utf-8")

    def test_codec_name_c(self):
        self.assertEqual(inputstream.codecName("  utf8  "), "utf-8")

    def test_codec_name_d(self):
        self.assertEqual(inputstream.codecName("ISO_8859--1"), "windows-1252")

def runParserEncodingTest(data, encoding):
    p = HTMLParser()
    t = p.parse(data, useChardet=False)
    encoding = encoding.lower().decode("ascii")

    assert encoding == p.tokenizer.stream.charEncoding[0], errorMessage(data, encoding, p.tokenizer.stream.charEncoding[0])

def runPreScanEncodingTest(data, encoding):
    stream = inputstream.HTMLBinaryInputStream(data, chardet=False)
    encoding = encoding.lower().decode("ascii")

    # Very crude way to ignore irrelevant tests
    if len(data) > stream.numBytesMeta:
        return

    assert encoding == stream.charEncoding[0], errorMessage(data, encoding, stream.charEncoding[0])

def test_encoding():
    for filename in get_data_files("encoding"):
        test_name = os.path.basename(filename).replace('.dat',''). \
            replace('-','')
        tests = TestData(filename, b"data", encoding=None)
        for idx, test in enumerate(tests):
            yield (runParserEncodingTest, test[b'data'], test[b'encoding'])
            yield (runPreScanEncodingTest, test[b'data'], test[b'encoding'])

try:
    import chardet
    def test_chardet(self):
        data = open(os.path.join(test_dir, "encoding" , "chardet", "test_big5.txt")).read()
        encoding = inputstream.HTMLInputStream(data).charEncoding
        assert encoding[0].lower() == "big5"
    setattr(Html5EncodingTestCase, 'test_chardet', test_chardet)
except ImportError:
    print("chardet not found, skipping chardet tests")


from distutils.dir_util import copy_tree, remove_tree
from pathlib import Path

import pytest

from picsorter.core import (
    _get_creation_date,
    _rename_file,
    rename_multiple_files,
)

def copy_directory(new_directory_name: str = "tests/pictures_copy"):
    """Copy directory with pictures to perform tests."""
    copy_tree("tests/pictures", new_directory_name)

def delete_directory(directory_name: str = "tests/pictures_copy"):
    """Delete a directory and its content."""
    remove_tree(directory_name)

@pytest.mark.parametrize(
    "picture_path, expected",
    [
        (Path("tests/pictures/bird.jpg"), "2023-05-29_08-07-04"),
        (Path("tests/pictures/ladybug.jpg"), "2023-05-29_07-52-22"),
    ],
)
def test_get_creation_date(picture_path, expected):
    assert expected == _get_creation_date(picture_path)

@pytest.mark.parametrize(
    "picture_path, expected, expected_append_old_name",
    [
        (
            Path("tests/pictures_copy/bird.jpg"),
            "2023-05-29_08-07-04.jpg",
            "2023-05-29_08-07-04_bird.jpg",
        ),
        (
            Path("tests/pictures_copy/ladybug.jpg"),
            "2023-05-29_07-52-22.jpg",
            "2023-05-29_07-52-22_ladybug.jpg",
        ),
    ],
)
def test_rename_file(picture_path, expected, expected_append_old_name):
    """Test renaming files to creation date and to creation date with old
    file name appended."""
    new_path = "tests/pictures_copy"
    # first copy directory 'pictures' with its content
    copy_directory(new_directory_name=new_path)

    # rename file
    _rename_file(picture_path)
    # check if file with new name exists
    assert (Path(new_path) / expected).exists()

    # delete directory
    delete_directory(directory_name=new_path)

    # again, copy directory
    copy_directory(new_directory_name=new_path)
    # rename file and append original file name
    _rename_file(picture_path, append_old_name=True)
    assert (Path(new_path) / expected_append_old_name).exists()
    delete_directory(directory_name=new_path)

def test_rename_multiple_files():
    new_path = "tests/pictures_copy"
    copy_directory(new_directory_name=new_path)

    rename_multiple_files(Path(new_path), file_extension="jpg")

    for renamed_file in ["2023-05-29_08-07-04.jpg", "2023-05-29_07-52-22.jpg"]:
        assert (Path(new_path) / renamed_file).exists()

    # ladybug.jpg in pictures_copy/more_pictures should have kept its name
    assert (Path(new_path) / "more_pictures/ladybug.jpg").exists()
    delete_directory(directory_name=new_path)


import mimetypes
import os

from pulumi import FileAsset, ResourceOptions
import pulumi_aws as aws

from constants import certificate_arn, env, prefix, static_site_path

# TODO Apply to production - create stack, etc.

def get_web_contents_root_path():
    # TODO Update for circleci -- if cwd.startswith("/home/circleci"):
    cwd = os.getcwd()
    # root_dir = cwd.replace("infrastructure", "frontend")
    # return f"{root_dir}/build"
    return os.path.join(os.getcwd(), static_site_path)

def crawl_static_dir(static_dir, f):
    for file in os.listdir(static_dir):
        filepath = os.path.join(static_dir, file)

        if os.path.isdir(filepath):
            crawl_static_dir(filepath, f)
        elif os.path.isfile(filepath):
            f(filepath)

def create_static_site() -> dict:
    static_bucket = aws.s3.Bucket(
        f"{prefix}-bucket",
        bucket=f"{prefix}-bucket",
        hosted_zone_id="Z2O1EMRO9K5GLX",
        lifecycle_rules=[aws.s3.BucketLifecycleRuleArgs(
            enabled=True,
            id="ZmJiMjViZTItY2IzMS00N2QyLThjZDYtNDJjNTg0MWU0ODJj",
            noncurrent_version_expiration=aws.s3.BucketLifecycleRuleNoncurrentVersionExpirationArgs(
                days=7,
            ),
        )],
        request_payer="BucketOwner",
        server_side_encryption_configuration=aws.s3.BucketServerSideEncryptionConfigurationArgs(
            rule=aws.s3.BucketServerSideEncryptionConfigurationRuleArgs(
                apply_server_side_encryption_by_default=aws.s3.BucketServerSideEncryptionConfigurationRuleApplyServerSideEncryptionByDefaultArgs(
                    sse_algorithm="AES256",
                ),
            ),
        ),
        versioning=aws.s3.BucketVersioningArgs(
            enabled=True,
        )
    )

    def bucket_object_converter(filepath):
        """
        Takes a file path and returns a bucket object managed by Pulumi
        """
        web_contents_root_path = get_web_contents_root_path()
        relative_path = filepath.replace(web_contents_root_path + "/", "")
        # Determine the mimetype using the `mimetypes` module.
        mime_type, _ = mimetypes.guess_type(filepath)
        aws.s3.BucketObject(
            relative_path,
            key=relative_path,
            acl="public-read",
            bucket=static_bucket.id,
            content_type=mime_type,
            source=FileAsset(filepath),
            opts=ResourceOptions(parent=static_bucket),
        )
    static_web_contents_root_path = get_web_contents_root_path()
    crawl_static_dir(static_web_contents_root_path, bucket_object_converter)

    alias = "gingerbeans.tech" if env == "production" else f"{env}.gingerbeans.tech"
    s3_origin_id = f"{prefix}-s3-origin-id"
    origin_access_control = aws.cloudfront.OriginAccessControl(
        f"{prefix}-origin-access-control",
        description="S3 Bucket Policy",
        origin_access_control_origin_type="s3",
        signing_behavior="always",
        signing_protocol="sigv4"
    )
    hosted_zone = aws.route53.get_zone(name="gingerbeans.tech")
    hosted_zone_id = hosted_zone.zone_id

    distribution = aws.cloudfront.Distribution(
        f"{prefix}-distribution",
        aliases=[alias],
        custom_error_responses=[
            aws.cloudfront.DistributionCustomErrorResponseArgs(
                error_code=404,
                response_code=200,
                error_caching_min_ttl=0,
                response_page_path="/index.html",
            ),
            aws.cloudfront.DistributionCustomErrorResponseArgs(
                error_code=403,
                response_code=200,
                error_caching_min_ttl=0,
                response_page_path="/index.html",
            ),
        ],
        default_cache_behavior=aws.cloudfront.DistributionDefaultCacheBehaviorArgs(
            allowed_methods=[
                "DELETE",
                "GET",
                "HEAD",
                "OPTIONS",
                "PATCH",
                "POST",
                "PUT",
            ],
            cached_methods=[
                "GET",
                "HEAD",
            ],
            target_origin_id=s3_origin_id,
            forwarded_values=aws.cloudfront.DistributionDefaultCacheBehaviorForwardedValuesArgs(
                query_string=False,
                cookies=aws.cloudfront.DistributionDefaultCacheBehaviorForwardedValuesCookiesArgs(
                    forward="none",
                ),
            ),
            viewer_protocol_policy="allow-all",
            min_ttl=0,
            default_ttl=3600,
            max_ttl=86400,
        ),
        default_root_object="index.html",
        enabled=True,
        is_ipv6_enabled=True,
        origins=[aws.cloudfront.DistributionOriginArgs(
            domain_name=f"{prefix}-bucket.s3.us-east-2.amazonaws.com",
            origin_id=s3_origin_id,
            origin_access_control_id=origin_access_control.id,
            # s3_origin_config=aws.cloudfront.DistributionOriginS3OriginConfigArgs(
                # origin_access_identity=origin_access.cloudfront_access_identity_path,
            # ),
        )],
        restrictions=aws.cloudfront.DistributionRestrictionsArgs(
            geo_restriction=aws.cloudfront.DistributionRestrictionsGeoRestrictionArgs(
               restriction_type="none",
            ),
        ),
        viewer_certificate=aws.cloudfront.DistributionViewerCertificateArgs(
            acm_certificate_arn=certificate_arn,
            minimum_protocol_version="TLSv1.2_2021",
            ssl_support_method="sni-only",
        )
    )

    alias_a_record = aws.route53.Record(
        f"{env}-a-record",
        name=alias,
        zone_id=hosted_zone_id,
        type="A",
        aliases=[
            aws.route53.RecordAliasArgs(
                name=distribution.domain_name,
                zone_id=distribution.hosted_zone_id,
                evaluate_target_health=True,
            )
        ],
    )

    alias_aaaa_record = aws.route53.Record(
        f"{env}-aaaa-record",
        name=alias,
        zone_id=hosted_zone_id,
        type="AAAA",
        aliases=[
            aws.route53.RecordAliasArgs(
                name=distribution.domain_name,
                zone_id=distribution.hosted_zone_id,
                evaluate_target_health=True,
            )
        ],
    )

    resource_dict = {
        "s3": static_bucket,
        "cf": distribution,
        "r53_a": alias_a_record,
        "r53_aaaa": alias_aaaa_record,
    }

    return resource_dict


# importing packages
import logging
import math
from django.utils import timezone
from django.utils import dateparse
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from time import sleep, time
from scrapper import celery_app
from apps.jobs.models import Job
import os
from decouple import config

# DASAUGE_URL = "https://dasauge.de/jobs/stellenangebote/"

@celery_app.task(name="save scrapped dasauge job", serializer='json')
def save_dasauge_job(job):
    title = job['title']
    print(f'saving {title} to db')

    try:
        Job.objects.get_or_create(
            title=job['title'],
            link=job['link'],
            company_avatar=job['company_avatar'],
            contract_type=job['contract_type'],
            company=job['company'],
            summary=job['summary'],
            description=job['description'],
            posted_at=job['posted'],
            location=job['location'],
            platform="Dasauge",
            tags=job['tags']
        )
    except Exception as e:
        print(f'failed to save {title}')
        print(e)

    return print(f'{title} saved to db')

@celery_app.task(name="scrap dasauge job descriptions", serializer='json')
def scrap_dasauge_job_descriptions(job, driver):
    try:
        print(f"Started scrapping {job['title']}")
        driver.get(job['link'])
        # let the driver wait 3 seconds to locate the element before exiting out
        driver.implicitly_wait(10)

        try:
            location = driver.find_element_by_xpath('//span[contains(@itemprop,"address")]').text
        except:
            location = None
        try:
            description = driver.find_element_by_xpath('//table[@class="infotab"]').text
        except:
            description = None
        try:
            tags = []
            tags_el = driver.find_elements_by_xpath('//a[@rel="tag"]')
            for tag in tags_el:
                tags.append(tag.text)
            tags = ';'.join(tags)

        except:
            tags = None

    except:
        location = None
        description = None
        tags = None
    job['location'] = location
    job['description'] = description
    job['tags'] = tags
    print('Finished scrapping descriptions')
    save_dasauge_job(job)

@celery_app.task(name="scrap dasauge jobs", serializer='json')
def scrap_dasauge_jobs(title, location):
    DASAUGE_URL = f'https://dasauge.de/jobs/stellenangebote/?begriff={title}&land={location}'
    try:

        # Add additional Options to the webdriver
        chrome_options = Options()
        # add the argument and make the browser Headless.
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--window-size=1920x1080")
        chrome_options.add_argument("--disable-notifications")
        chrome_options.add_argument("--disable-popup-blocking")
        chrome_options.add_argument("--disable-extensions")
        chrome_options.add_argument("--disable-dev-shm-usage")
        if config('MODE') == 'dev':
            driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
            driver1 = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
        else:
            chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
            driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"),
                                      chrome_options=chrome_options)
            driver1 = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"),
                                       chrome_options=chrome_options)

        driver.get(DASAUGE_URL)
        sleep(3)

        action = ActionChains(driver)
        action = ActionChains(driver1)
        # let the driver wait 3 seconds to locate the element before exiting out
        driver.implicitly_wait(10)
        try:
            no_jobs = driver.find_element_by_xpath('//div[contains(@id,"infobox")]/h2').text
        except:
            no_jobs = False
        if (no_jobs == 'Leider wurden keine Einträge nach deinen Kriterien gefunden.'
                or no_jobs == 'Unfortunately no entries were found according to your criteria.'):
            raise Exception("No jobs found")
        try:
            pages = math.ceil(int(driver
                                  .find_element_by_xpath('//div[contains(@class,"in")]')
                                  .text.split(' ')[0].replace('.', '')) / 15)
        except:
            pages = 100
        for i in range(0, pages):

            try:
                job_card = driver.find_elements_by_xpath('//div[contains(@id,"eliste")]/article')
            except:
                break
            for idx, job in enumerate(job_card):
                try:
                    title = job.find_elements_by_xpath('//h2[contains(@class,"hassub")]')[idx].text
                except:
                    title = 'None'
                try:
                    link = (job
                            .find_elements_by_xpath('//h2[contains(@class,"hassub")]')[idx]
                            .find_element_by_tag_name('a')
                            .get_attribute(name="href"))
                except:
                    link = None

                try:
                    company = (job
                               .find_elements_by_xpath('//div[@class="boxsubline toplinie"]')[idx]
                               .find_element_by_tag_name('em').text)
                except:
                    company = None

                try:
                    company_avatar = (job
                                      .find_elements_by_xpath(
                        '//div[@class="mlogomini mlogof uid"]')[idx]
                                      .find_element_by_tag_name('img').get_attribute(name="src"))
                except:
                    company_avatar = None
                try:
                    c_list = []
                    c_types = (job
                               .find_elements_by_xpath(
                        '//ul[@class="jobprops"]')[idx]
                               .find_elements_by_tag_name("li"))
                    for c in c_types:
                        c_list.append(c.text)
                    contract_type = ';'.join(c_list)
                except:
                    contract_type = None
                try:
                    summary = (job
                               .find_elements_by_xpath(
                        '//div[@class="neutral klickbatzen"]')[idx]
                               .find_element_by_tag_name('p').text[:130] + '...')
                except:
                    summary = None
                try:
                    posted = dateparse.parse_datetime(
                        job.find_elements_by_xpath('//time[@itemprop="datePosted"]')[idx]
                            .get_attribute(name='datetime'))
                except:
                    posted = timezone.now()

                job = {
                    'title': title,
                    'company': company,
                    'link': link,
                    'posted': posted,
                    'company_avatar': company_avatar,
                    'summary': summary,
                    'contract_type': contract_type,
                }
                scrap_dasauge_job_descriptions(job, driver1)

            print("Finished scraping the jobs for page: {}".format(str(i + 1)))

            try:
                next_page = driver.find_element_by_xpath('//a[@rel="next"]')
                next_page.click()
            except:
                break

        print('Finished scraping dasauge jobs')

    except Exception as e:
        logging.warning(e)
        print('Failed')


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/10 15:00
# @Author  : Zoe
# @Site    :
# @File    : correction_contours.py
# @Software: PyCharm Community Edition
import cv2
import numpy as np
from matplotlib import pyplot as plt
import copy

def correct_contours(img):
    src_img = copy.copy(img)

    # 灰度化
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    plt.imshow(gray)
    plt.title('gray')
    plt.show()
    # 二值化
    _, bin_img = cv2.threshold(gray,  100, 200, cv2.THRESH_BINARY)
    plt.imshow(bin_img)
    plt.title('thresh')
    plt.show()

    # 找轮廓边框
    _, contours, _ = cv2.findContours(bin_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    for i in range(len(contours)):
        rect = cv2.minAreaRect(contours[i])  # 得到最小外接矩形的（中心(x,y), (宽,高), 旋转角度）
        rectpoint = cv2.boxPoints(rect)  # cv2.boxPoints(rect) for OpenCV 3[0] 获取最小外接矩形的4个顶点
        angle = rect[2]
        print(angle)
        line1 = np.sqrt((rectpoint[1][1] - rectpoint[0][1]) * (rectpoint[1][1] - rectpoint[0][1]) + (
                         rectpoint[1][0] - rectpoint[0][0]) * (rectpoint[1][0] - rectpoint[0][0]))
        line2 = np.sqrt((rectpoint[3][1] - rectpoint[0][1]) * (rectpoint[3][1] - rectpoint[0][1]) + (
                         rectpoint[3][0] - rectpoint[0][0]) * (rectpoint[3][0] - rectpoint[0][0]))

        # 如果面积太小直接pass
        if line1 * line2 < 600:
            continue

        # 为了让正方形横着放，所以旋转角度是不一样的。竖放的，给他加90度，翻过来
        if line1 > line2:
            angle = 90 + angle

        # RoiSrcImg = np.zeros(img.shape, np.uint8)
        # cv2.drawContours(bin_img, contours, -1, 255)

        # RoiSrcImg = copy.copy(bin_img)
        # plt.imshow(bin_img)
        # plt.title('bin_img after draw contours')
        # plt.show()

        # 对RoiSrcImg进行旋转
        center = rect[0]
        # 计算旋转加缩放的变换矩阵
        M2 = cv2.getRotationMatrix2D(center, angle, 1)

        # 仿射变换
        RatationedImg = cv2.warpAffine(img, M2, (img.shape[1], img.shape[0]), 1, cv2.BORDER_CONSTANT, 1)

        plt.imshow(RatationedImg)
        plt.title('after rotate image')
        plt.show()

    # 对ROI区域进行抠图

    # 对旋转后的图片进行轮廓提取
    for j in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[j])
        print(x, y, w, h)
        if w*h < 600:
            continue

        dst_img = RatationedImg[x:x+h, y:y+w, :]
    plt.imshow(dst_img)
    plt.title('RIO')
    plt.show()

if __name__ == '__main__':

    # image = cv2.imread('./data/1.jpg', 0)
    image = cv2.imread('./data/4.jpg')
    if image is None:
        raise Exception("No image load!")

    correct_contours(image)


'''Send an HTTPS request to hostname (port 443) and print the reply to stdout.

OpenSSL is used directly, not via PyOpenSSL.

The program flow follows `tls-connect.c`.
'''

import argparse
import sys

from pyopenssl_examples.cffi.openssl.bio_02_tls_openssl import ffi, lib

def BIO_get_ssl(cbio, sslp):
    return lib.BIO_ctrl(cbio, lib.BIO_C_GET_SSL, 0, sslp)

def bio_connect(hostname):
    '''Send an HTTP request to hostname (port 80) and print the reply to stdout.

    It follows the example of `man BIO_s_connect` (version: OpenSSL-1.1.0).
    '''
    # Set up the OpenSSL library
    lib.SSL_load_error_strings()
    lib.SSL_library_init()

    # Create SSL context structure and load the trust store
    # (accepted root ca-certificates)

    ctx = lib.SSL_CTX_new(lib.SSLv23_client_method())
    if not ctx:
        sys.stderr.write('Error creating SSL context\n')
        sys.exit(1)

    if not lib.SSL_CTX_load_verify_locations(
            ctx,
            b'/etc/ssl/certs/ca-certificates.crt',
            ffi.NULL):
        sys.stderr.write('Error loading trust store into SSL context\n')
        lib.ERR_print_errors_fp(sys.stderr)

    # Set up the SSL connection

    cbio = lib.BIO_new_ssl_connect(ctx)

    # FIXME does this work? make sense? verify always passes
    ssl = lib.SSL_new(ctx)
    BIO_get_ssl(cbio, ssl)
    # Set flag SSL_MODE_AUTO_RETRY
    lib.SSL_set_mode(ssl, lib.SSL_MODE_AUTO_RETRY)

    # Connect to server <hostname>
    name = '{hostname}:{port}'.format(hostname=hostname, port='https')
    name_bytes = name.encode('ascii')

    lib.BIO_set_conn_hostname(cbio, name_bytes)

    if lib.BIO_do_connect(cbio) <= 0:
        sys.stderr.write('Error connecting to server\n')
        sys.exit(1)

    # Check the certificate

    if lib.SSL_get_verify_result(ssl) != lib.X509_V_OK:
        sys.stderr.write(
            'Certificate verification error: {err}\n'.format(
                err=lib.SSL_get_verify_result(ssl)))
        # sys.exit(1)

    # send HTTP request to the server <hostname>

    req = 'GET / HTTP/1.1\x0D\x0AHost: %s\x0D\x0AConnection: ' \
          'Close\x0D\x0A\x0D\x0A' % hostname
    lib.BIO_puts(cbio, req.encode('ascii'))

    # read HTTP response from server and print to stdout

    # output bio
    out = lib.BIO_new_fp(sys.stdout, 0)

    tmpbuf_len = 1024
    tmpbuf = tmpbuf_len * b'\0'  # c-style way to allocate memory

    while True:
        length = lib.BIO_read(cbio, tmpbuf, tmpbuf_len)
        if length <= 0:
            break
        lib.BIO_write(out, tmpbuf, length)
        break  # to print complete answer comment out this line

    # close TCP/IP connection and free used BIOs

    lib.BIO_free(cbio)
    lib.SSL_CTX_free(ctx)
    lib.BIO_free(out)

    return 0

def create_parser():
    parser = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0])
    parser.add_argument('hostname',
                        help='host name of the server '
                             "(example: 'editorconfig.org')")
    return parser

def main():
    parser = create_parser()
    args = parser.parse_args()
    bio_connect(args.hostname)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Sat Oct 13 16:37:20 2018

@author: mansi
"""

import pandas as pd
import numpy as np
import pandas as pd
import datetime as dt
def format_time(m):
   new_time = '0'
   time = m
   if(len(time)==1):
       new_time = "0" +time
   elif(len(time)==2 and int(time)<24):
       new_time = time + "00"
   elif(len(time)==2 and int(time)>=24):
       new_time = "00" + time
   elif(len(time)==3):
       new_time = "0" + time
   elif(len(time) == 4):
       new_time = time
   final_time = new_time[0:2]
   return final_time
   print(final_time)

def date_time_converter(value):
    if(isinstance(value, str)):
        date = pd.to_datetime(value, format='%m/%d/%Y')
        return date
df=pd.read_csv('D:/Sem1/INF552/Project/Data/Final1/train.csv',
               converters={'Date Occurred': date_time_converter,'Time Occured':format_time})
##df=pd.read_csv('D:/Sem1/INF552/Project/Data/Final1/train.csv',converters={'Date Occurred': date_time_converter})
columns = df.columns
b=df[['Date Occurred','Time Occurred','Crime Code']]

##print(columns)
a=type(columns)
##print(a)

##print(b)
column_1 = df.iloc[:,3]
f=type(column_1)
##print(f)
#o=pd.DataFrame({"dayofweek": column_1.dt.dayofweek})

b['weekday'] = column_1.dt.dayofweek
#print(b)
X = b['weekday'].values
#print(X)
Y=b['Time Occurred'].values
#print(Y)
from sklearn.model_selection import train_test_split
X = X.reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
##from sklearn.preprocessing import StandardScaler, MinMaxScaler
##sc_X = StandardScaler()
##X_train = sc_X.transform(X_train)
##X_test = sc_X.transform(X_test)
##print(X_test)
from sklearn.linear_model import LogisticRegression
regressor = LogisticRegression()
regressor.fit(X_train, y_train)
b1 = regressor.coef_
b0 = regressor.intercept_
y_pred = regressor.predict(X_test)
m=y_pred.astype(int)
#print(X_test)
##c=int(y_pred)
#print(m)
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test,y_pred)
rmse = mse**0.5
print(rmse)


import unittest
import os
from server1 import *
from server2 import *
from client import *

class TestUDP(unittest.TestCase):

    # Место для ваших страданий :)
    # Ведь нужно написать еще 18 тестов

    def test_F(self):
        self.assertEqual(F('килобайт'), 'илобайт')

    def test_F1(self):
        self.assertEqual(F1('каракатица'), 'коаракоатица')

    def test_m_recvfrom(self):

        host_1 = ''
        port_1 = 2002
        addr_1 = (host_1, port_1)
        sock_1 = socket(AF_INET, SOCK_DGRAM)
        sock_1.bind(addr_1)

        os.startfile('client.py')
        msg_1, addr_1 = m_recvfrom(sock_1)
        sock_1.close()
        self.assertNotEqual(msg_1, 'err')

if __name__ == '__main__':
    unittest.main()


from torch.utils.data.sampler import Sampler

## one by one
class CustomBatchSampler_Multi(Sampler):

    def __init__(self, sampler):
        for samp in sampler:
            if not isinstance(samp, Sampler):
                raise ValueError("sampler should be an instance of "
                                 "torch.utils.data.Sampler, but got sampler={}"
                                 .format(samp))
        self.samplers = sampler
        self.n_samples = [len(samp) for samp in self.samplers]
        self.sample_cnt = [0 for samp in self.samplers]
        self.iters = [iter(samp) for samp in self.samplers]

    def __iter__(self):

        # for each iteration step
        for ii in range(len(self)):

            # if index is the even number
            if ii % 2 == 0:
                sampler_id = 0
            else:
                sampler_id = 1

            self.sample_cnt[sampler_id] += 1    # the nubmer of used sample. One sample per one iteration.
            if self.sample_cnt[sampler_id] > self.n_samples[sampler_id]: ## if exceeding the number of samples, reinitialize the sampler
                self.iters[sampler_id] = iter(self.samplers[sampler_id])
                self.sample_cnt[sampler_id] = 1

            batch = []

            ## starting index of the iterator
            if sampler_id is 0:
                prev_idx = 0
            else:
                prev_idx = self.n_samples[sampler_id-1]

            ## include a sample in the batch
            batch.append(next(self.iters[sampler_id]) + prev_idx)

            yield batch

    def __len__(self):

        return len(self.samplers[0]) * 2


import numpy as np
from src.compute_corr_coef import compute_corr_coef
from utils.plotting import plot_similarities

def compute_trust_values(dsk, do_plot=False):
    """
    Compute trust values following formula 6

    k:= number of blendshapes
    n:= num_features (num_markers*3)

    :param dsk: delta_sk vector (k, n)
    :param do_plot: decide if we want to plot the between-correlation matrix
    :return: trust values vector (k,)
    """

    if len(np.shape(dsk)) != 2:
        raise ValueError("[COMPUTE TRUST VALUE] dsk dimensions not supported ({}) instead of 2".format(len(np.shape(dsk))))

    # compute between-blendshape correlation
    ckl = compute_corr_coef(dsk, dsk)
    ckl = np.maximum(ckl, np.zeros(np.shape(ckl)))
    if do_plot:
        plot_similarities(ckl, "Between blendshapes correlation", vmin=0, vmax=1)

    # compute lower triangle
    num_k = np.shape(ckl)[0]
    low_trig = np.zeros(num_k)
    for k in range(num_k):
        val = 0
        for l in range(k):
            val += ckl[k, l]
        low_trig[k] = val
    max_low_trig = np.max(low_trig)

    # compute trust values  (formula 6)
    tk = np.zeros(num_k)
    for k in range(len(tk)):
        tk[k] = 1 - low_trig[k]/max_low_trig

    return tk

if __name__ == '__main__':
    """
    test compute_trust_values function

    run: python -m src.compute_trust_values
    """
    np.random.seed(0)
    from utils.re_order_delta import re_order_delta

    # test compute trust values
    sk = np.random.rand(6, 3)  # (k, n)
    sorted_sk = re_order_delta(sk)

    tk = compute_trust_values(sorted_sk, do_plot=False)

    print("tk")
    print(tk)


"""Tests for submission_runner.py.

This is an end-to-end test for MNIST in PyTorch and Jax that requires the
dataset to be available. For testing the workload and reference submission code
for all workloads, see reference_algorithm_tests.py.
"""
import copy
import os
import sys

from absl import flags
from absl import logging
from absl.testing import absltest
from absl.testing import parameterized

from algorithmic_efficiency.profiler import PassThroughProfiler
import submission_runner

FLAGS = flags.FLAGS
# Needed to avoid UnparsedFlagAccessError
# (see https://github.com/google/model_search/pull/8).
FLAGS(sys.argv)

_MNIST_DEV_ALGO_DIR = 'reference_algorithms/development_algorithms/mnist'

class SubmissionRunnerTest(parameterized.TestCase):
  """Tests for reference submissions."""

  @parameterized.named_parameters(
      dict(
          testcase_name='mnist_jax',
          workload='mnist',
          framework='jax',
          submission_path=(f'{_MNIST_DEV_ALGO_DIR}/mnist_jax/submission.py'),
          tuning_search_space=(
              f'{_MNIST_DEV_ALGO_DIR}/tuning_search_space.json')),
      dict(
          testcase_name='mnist_pytorch',
          workload='mnist',
          framework='pytorch',
          submission_path=(
              f'{_MNIST_DEV_ALGO_DIR}/mnist_pytorch/submission.py'),
          tuning_search_space=(
              f'{_MNIST_DEV_ALGO_DIR}/tuning_search_space.json')),
  )
  def test_submission(self,
                      workload,
                      framework,
                      submission_path,
                      tuning_search_space):
    FLAGS.framework = framework
    workload_metadata = copy.deepcopy(submission_runner.WORKLOADS[workload])
    workload_metadata['workload_path'] = os.path.join(
        submission_runner.BASE_WORKLOADS_DIR,
        workload_metadata['workload_path'] + '_' + framework,
        'workload.py')
    workload_obj = submission_runner.import_workload(
        workload_path=workload_metadata['workload_path'],
        workload_class_name=workload_metadata['workload_class_name'],
        workload_init_kwargs={})
    score = submission_runner.score_submission_on_workload(
        workload_obj,
        workload,
        submission_path,
        data_dir='~/tensorflow_datasets',  # The default in TFDS.
        tuning_ruleset='external',
        tuning_search_space=tuning_search_space,
        num_tuning_trials=1,
        profiler=PassThroughProfiler(),
        max_global_steps=500,
    )
    logging.info(score)

  def test_convert_filepath_to_module(self):
    """Sample test for the `convert_filepath_to_module` function."""
    test_path = os.path.abspath(__file__)
    module_path = submission_runner.convert_filepath_to_module(test_path)
    self.assertNotIn('.py', module_path)
    self.assertNotIn('/', module_path)
    self.assertIsInstance(module_path, str)

if __name__ == '__main__':
  absltest.main()


#imports
import numpy as np
import cv2

class ColorDescriptor:
    def __init__(self, bins):
        #store number of bins for the 3d histogram
        self.bins = bins

    def describe(self, image):
        #convert the image to HSV
        #initialise the features used to quanity the image
        image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        features = []

        #get dimensions & compute centre of image
        (h, w) = image.shape[:2]
        (cX, cY) = (int(w * 0.5), int(h * 0.5))

        #divide the image into four rectangles(topleft, top right, bottomright, bottom left)
        segments = [(0, cX, 0, cY), (cX, w, 0, cY), (cX, w, cY, h), (0, cX, cY, h)]

        #construct eliptical mask representing the centre of the image
        (axesX, axesY) = (int((w * 0.75) / 2), int((h * 0.75) / 2))

        ellipMask = np.zeros(image.shape[:2], dtype = "uint8")
        cv2.ellipse(ellipMask, (cX, cY), (axesX, axesY), 0, 0, 360, 255, -1)

        #loop over the segments
        for (startX, endX, startY, endY) in segments:
            #construct a mask for each corner of the image, subtracting the elliptical centre from it
            cornerMask = np.zeros(image.shape[:2], dtype = "uint8")
            cv2.rectangle(cornerMask, (startX, startY), (endX, endY), 255, -1)
            cornerMask = cv2.subtract(cornerMask, ellipMask)

            #extract a colour histogram from the image, then update the feature vector
            hist = self.histogram(image, cornerMask)
            features.extend(hist)

        #extract a color histogram from the elliptical region and update the feature vector
        hist = self.histogram(image, ellipMask)
        features.extend(hist)

        #return the feature vector
        return features

    def histogram(self, image, mask):
        #extract 3d colour hist from the masked region, using the supplied number of bins per channel; then
        #normalise the histogram
        hist = cv2.calcHist([image], [0, 1, 2], mask, self.bins,
            [0, 180, 0, 256, 0, 256])
        dst = np.zeros(image.shape[:2], dtype = "uint8")
        hist = cv2.normalize(hist, dst).flatten()

        #return the hist
        return hist


import pickle
import jieba
import torch
import torch.utils.data

VOCAB_SIZE = 84316

label2id = {}
def split_words(dataset):
    raw_docs = []
    docs = []
    labels = []
    for topic, datas in dataset.items():
        if not topic in label2id.keys():
            label2id[topic] = len(list(label2id))
        for data in datas:
            seg = jieba.cut(data["title"])
            raw_docs.append([topic, data["title"]])
            docs.append(" ".join(seg))
            labels.append(label2id[topic])
    return raw_docs, docs, labels

def Read_and_save():
    with open("dataset/train.pkl", "rb") as f:
        train_data = pickle.load(f)
    with open("dataset/valid.pkl", "rb") as f:
        valid_data = pickle.load(f)

    train_raw, train_docs, train_labels = split_words(train_data)
    valid_raw, valid_docs, valid_labels = split_words(valid_data)
    vocab_set = set()
    for s in train_docs:
        s = s.split(' ')
        for word in s:
            vocab_set.add(word)
    for s in valid_docs:
        s = s.split(' ')
        for word in s:
            vocab_set.add(word)
    print(vocab_set)
    print(len(vocab_set))
    vocab_set.remove('')
    pickle.dump(vocab_set, open('vocab.pkl', 'wb'))

# 加载pkl格式的数据
def load_data(path):
    return pickle.load(open(path, 'rb'))

# 保存成pkl格式的数据
def save_data(obj, path):
    pickle.dump(obj, open(path, 'wb'))

class Dataset(torch.utils.data.Dataset):
    def __init__(self, data, label):
        self.dataLen = len(data)
        self.data = data
        self.label = label

    def __getitem__(self, index):
        # data[index]: 句子的index表示
        # label[index]: 该句子的label
        return self.data[index], self.label[index], len(self.data[index])

    def __len__(self):
        return self.dataLen

# 对数据进行padding
def collate_fun(data):
    data_label_len = sorted(data, key=lambda x: len(x[0]), reverse=True)
    max_sentence_length = len(data_label_len[0][0])
    data_tensor = []
    for sentence, label, l in data_label_len:
        inp = [sentence[i] if i < len(sentence) else VOCAB_SIZE-1 for i in range(max_sentence_length)]
        data_tensor.append(inp)
    data_tensor = torch.LongTensor(data_tensor)
    target_tensor = torch.LongTensor([i for _, i, __ in data_label_len])
    real_length_tensor = torch.LongTensor([i for _, __, i in data_label_len])
    return data_tensor, target_tensor, real_length_tensor

# 获取dataloader对象
def Get_dataloader(batch_size, filename, shuffle):
    data_and_label = load_data(filename)
    data_train = [i for i, j in data_and_label]
    label_train = [j for i, j in data_and_label]
    # 把 dataset 放入 DataLoader
    trainloader = torch.utils.data.DataLoader(
        dataset=Dataset(data_train, label_train),
        batch_size=batch_size,
        shuffle=shuffle,
        collate_fn=collate_fun,
        drop_last=True
    )
    return trainloader