#MultiWindowHandle on Yatra.Com
#code by sunil savale
#date- 20-09-2021

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as ec
import time

#Launch the Browser
class Windows():
    def MultiWindows(self):
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        driver.maximize_window()

        #Navigate the URL
        driver.get("https://www.yatra.com/")

        # get the parent window ids
        parent_window = driver.current_window_handle
        print(parent_window)
        time.sleep(5)

        # Click on Airline Web it will give us multiple airline
        window_handle = driver.find_element(By.XPATH, ".//img[contains(@src,'/2020/Jul/5b10fea20065899f659cec0ecee16bca.jpg')]").click()
        all_handles = driver.window_handles

        # get all the Window handles in the form of ids
        print(all_handles)

        #Switch to window and click on particular web element which is inspect
        for handle in all_handles:
            if handle != parent_window:
                driver.switch_to.window(handle)
                time.sleep(3)
                driver.find_element(By.XPATH, ".//a[@href='https://www.goair.in/plan-my-trip/web-check-in']").click()
                # wait = WebDriverWait(driver, 15)
                # input = wait.until(ec.visibility_of_element_located((By.XPATH, ".//input[@id='LastName']")))
                # input.click()
                # input.send_keys("Patil")
                driver.close()
                break
                #switch to parent window , back to the main window

        time.sleep(5)
        driver.switch_to.window(parent_window)
        driver.find_element(By.XPATH, ".//img[contains(@src,'/2020/Jul/5b10fea20065899f659cec0ecee16bca.jpg')]").click()

switch = Windows()
switch.MultiWindows()


from django.conf.urls import url
from . import controllers

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^order-add', controllers.orderAdd),
    url(r'^department-add', controllers.departmentAdd),
    url(r'^orderer-add', controllers.ordererAdd),
    url(r'^product-add', controllers.productAdd),
    url(r'^fill-db-from-json', controllers.fillDataBaseFromJSON),

    url(r'^order-update', controllers.orderUpdate),
    url(r'^department-update', controllers.departmentUpdate),
    url(r'^product-update', controllers.productUpdate),

    url(r'^order-del', controllers.orderDelete),
    url(r'^department-del', controllers.departmentDelete),
    url(r'^orderer-del', controllers.ordererDelete),
    url(r'^product-del', controllers.productDelete),

    url(r'^orders', controllers.orders),
    url(r'^orderers', controllers.orderers),
    url(r'^products', controllers.products),
    url(r'^departments', controllers.departments),
    url(r'^$', controllers.index)
]


import time
from datetime import timedelta

from flask import current_app
from sentry_sdk import capture_message

from twitter_bastion.celery import celery_app
from twitter_bastion.extensions import db
from twitter_bastion.models import Tweet
from twitter_bastion.services import (
    get_tweet_count_percent_difference_for_subsequent_time_windows,
)

@celery_app.task
def archive_old_tweets():
    max_unarchived_tweets = current_app.config['MAX_UNARCHIVED_TWEETS']

    for record in db.graph.run(
        'MATCH (t:Tweet) '
        'WHERE NOT t:Archived '
        'RETURN t '
        'ORDER BY t.created_at DESC '
        'SKIP $max_unarchived_tweets',
        max_unarchived_tweets=max_unarchived_tweets
    ):
        archivable_tweet = Tweet()
        archivable_tweet.id = record['t']['id']
        archivable_tweet.archived = True
        db.graph.push(archivable_tweet)

@celery_app.task
def monitor_tweet_count_variation():
    time_window_length_in_minutes = (
        current_app.config['TIME_WINDOW_LENGTH_IN_MINUTES']
    )
    time_window_interval_in_minutes = (
        current_app.config['TIME_WINDOW_INTERVAL_IN_MINUTES']
    )
    warning_threshold_difference = (
        current_app.config['WARNING_THRESHOLD_DIFFERENCE']
    )
    # while True:
    difference = (
        get_tweet_count_percent_difference_for_subsequent_time_windows(
            time_window_length_in_minutes, time_window_interval_in_minutes
        )
    )
    if difference >= warning_threshold_difference:
        capture_message(
            f'Increase by {difference}% tweets since last time window.'
        )
    elif difference <= -warning_threshold_difference:
        capture_message(
            f'Reduction by {difference}% tweets since last time window.'
        )


import re
import requests
import error_log as log
import review_scrape as rs

from bs4 import BeautifulSoup

headers = {
    'authority': 'www.glassdoor.com',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'dnt': '1',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
}

url = 'https://www.glassdoor.com/Reviews/Nuix-Reviews-E257627.htm'

response = requests.get(url, headers=headers)

c = response.content
soup = BeautifulSoup(c, 'html.parser')

samples = soup.find_all("div", "hreview")

def number_of_pages():
	number_of_pages = soup.find_all("li", "page last")
	number_of_pages = (str(number_of_pages).split("
")[0])
	return (str(number_of_pages)[-1:])

num_of_pages = (int(number_of_pages()))

temp = []

for i in range(0, num_of_pages):
	temp.append(url[0:-4] + "_P" + (str(i+1)) + ".htm")

for t in temp:
	samples = soup.find_all("div", "hreview")

	for x in samples:
		scrape = rs.ReviewScrape(x)
		scrape.scrape_date_and_time()
		scrape.scrape_review_title()
		scrape.scrape_star_rating()
		scrape.scrape_employee_type()
		scrape.scrape_job_title()
		scrape.scrape_recomends()
		scrape.scrape_outlook()
		scrape.scrape_approves_of_ceo()
		scrape.scrape_time_period()
		scrape.scrape_pros_text()
		scrape.scrape_cons_text()
		scrape.scrape_advice_text()
		scrape.scrape_helpful_count()


from collections import defaultdict

n, m = map(int, input().split())

edges = []
parent = [i for i in range(n)]

for _ in range(m):
    a, b, c = map(int, input().split())
    edges.append((a-1, b-1, c))

def find(i):
    while i != parent[parent[i]]:
        parent[i] = parent[parent[i]]
        i = parent[i]
    return i

def union(x, y):
    p_x = find(x)
    p_y = find(y)
    parent[p_y] = p_x

def is_connected(x, y):
    p_x = find(x)
    p_y = find(y)
    return p_x == p_y

# build MST
tree = defaultdict(list)

edges.sort(key=lambda x: x[2])
for a, b, c in edges:
    if not is_connected(a, b):
        union(a, b)
        tree[a].append((b, c))
        tree[b].append((a, c))

ans = [0]*(2*m)

# Run DFS to count the number of times an edge is used
# as weights of all edges is different, hence each weight maps to a particular children

def dfs(src, p=-1):
    total = 1
    for v, c in tree[src]:
        if v != p:
            children = dfs(v, src)

            # children => nodes right to edge, n - children => nodes left to edge
            ans[c] += (n - children)*children

            total += children
    return total

dfs(0)

res = 0
for i in range(len(ans)):
    res += ans[i] * (1 << i)

print(str(bin(res))[2:])

Problem Solving/Algorithms/Graph Theory/Roads in HackerLand.py


import cuda_kernels
import math
import mrcfile
from numba import cuda
import numpy as np
import time
from utility_functions import blockPrint,enablePrint,print_progress

# From Release script
def AveragesOnShellsUsingLogicBCuda(inc,retofRR,retofRI,n1ofR,n2ofR, kXofR,kYofR,kZofR, \
                                    NumAtEachR,Thresh, RMax):
    print('This loop will go to '+str(RMax)+'\n' )
    NumAtEachRMax=NumAtEachR[-1];
    retofROutR = np.zeros([inc+1,NumAtEachRMax]); #retofRR.copy();# Real part of output
    retofROutI = np.zeros([inc+1,NumAtEachRMax]); #retofRI.copy();# Imag part of output
    n1ofROut   = np.zeros([inc+1,NumAtEachRMax]); #n1ofR.copy();
    n2ofROut   = np.zeros([inc+1,NumAtEachRMax]); #n2ofR.copy();
    NumAtROut  = np.zeros([inc+1,NumAtEachRMax]); #

    print("shape of retofROutR is ",np.shape(retofROutR))
    NumAtEachRMaxCuda= 15871;

    retofROutR[0,0] = retofRR[0,0];
    retofROutI[0,0] = retofRI[0,0];
    n1ofROut[0,0]    = n1ofR[0,0];
    n2ofROut[0,0]    = n2ofR[0,0];

    # Load all data into GPU memory
    # Need to convert data to contiguous arrays
    #kXofR_global_mem = cuda.to_device(np.ascontiguousarray(kXofR,dtype=np.float32))
    #kYofR_global_mem = cuda.to_device(np.ascontiguousarray(kYofR,dtype=np.float32))
    #kZofR_global_mem = cuda.to_device(np.ascontiguousarray(kZofR,dtype=np.float32))
    retofRR_global_mem = cuda.to_device(np.ascontiguousarray(retofRR,dtype=np.float32))
    retofRI_global_mem = cuda.to_device(np.ascontiguousarray(retofRI,dtype=np.float32))
    n1ofR_global_mem = cuda.to_device(np.ascontiguousarray(n1ofR,dtype=np.float32))
    n2ofR_global_mem = cuda.to_device(np.ascontiguousarray(n2ofR,dtype=np.float32))
    enablePrint()
    for r in range(1,RMax+1):#range(1,inc+1):
        #if r!=2: continue
        if ((r-1)%5)==0: print(r)
        NumOnSurf = int(NumAtEachR[r]);
        #LastInd = NumAtEachR[r]-1 ;
        kXNow    = kXofR[r][:NumOnSurf];
        kYNow    = kYofR[r][:NumOnSurf];
        kZNow    = kZofR[r][:NumOnSurf];#     Vectors
        retNowR = retofRR[r][:NumOnSurf];
        retNowI = retofRI[r][:NumOnSurf];
        n1Now    = n1ofR[r][:NumOnSurf];
        n2Now    = n2ofR[r][:NumOnSurf];#   for given

        ## Progress bar
        print_progress(r,RMax)
        ##

        NumLoops=1+int(NumOnSurf*NumOnSurf/NumAtEachRMaxCuda/NumAtEachRMaxCuda);# kicks in at r=50
        Stride=int(NumOnSurf/NumLoops);

        startTime = time.time()
        blockPrint()
        print("NumOnSurf:NumLoops:Stride -  ",NumOnSurf,":",NumLoops,":",Stride)
        stream = cuda.stream()
        #NumAtROutPre_global_mem = cuda.device_array((NumOnSurf,Stride))
        #Prod11_global_mem = cuda.device_array(NumOnSurf,stream=stream,dtype=np.float32)
        for jLoop in range(NumLoops):

            Start=jLoop*Stride;
            End= Start+Stride;
            if jLoop==(NumLoops-1):
                End = NumOnSurf;
            #print("jLoop,Start,End = %g,  %g  %g " %(jLoop,Start,End) )
            NumAtROutPre = np.zeros((NumOnSurf,End-Start), dtype=np.int)
            #print("NumAtROutPre.shape %g %g" %(NumAtROutPre.shape))
            InnerLogicCuda_start = time.time()
            NumAtROutPre_global_mem = AveragesOnShellsInnerLogicKernelCuda(kXNow,kYNow,kZNow,\
                                                                           #NumAtROutPre_global_mem,\
                                                                           #Prod11_global_mem,\
                                                                           #kXofR_global_mem,\
                                                                           #kYofR_global_mem,\
                                                                           #kZofR_global_mem,\
                                                                           retofRR_global_mem,\
                                                                           retofRI_global_mem,\
                                                                           n1ofR_global_mem,\
                                                                           n2ofR_global_mem,\
                                                                           NumOnSurf,\
                                                                           Thresh,\
                                                                           Start,\
                                                                           End,\
                                                                           r);
            print("Time to complete InnerLogicKernelCuda is ",time.time()-InnerLogicCuda_start)

            deltaTimeN =time.time()-startTime;
            startTime = time.time()

            LogicCCuda_start = startTime

            reduced = AveragesOnShellsInnerLogicCCuda(\
                                                      retofRR_global_mem,\
                                                      retofRI_global_mem,\
                                                      n1ofR_global_mem,\
                                                      n2ofR_global_mem,\
                                                      NumAtROutPre_global_mem,\
                                                      End,\
                                                      Start,\
                                                      NumOnSurf,\
                                                      r)
            stream = cuda.stream()
            cuda.synchronize()
            print("\nTime to complete AveragesOnShellsInnerLogicCCuda is ",time.time()-LogicCCuda_start)

            retofROutR[r][Start:End] = reduced[0]
            retofROutI[r][Start:End] = reduced[1]
            n1ofROut[r][Start:End]   = reduced[2]
            n2ofROut[r][Start:End]   = reduced[3]

            sum_start = time.time()
            print("Dimensions of NumAtROutPre_global_mem are ",NumAtROutPre_global_mem.shape)
            NumAtROut[r][Start:End] = cuda_kernels.sum_rows(NumAtROutPre_global_mem,Start,End)
            print("Time to compute sum_rows on GPU: ",time.time()-sum_start)
        deltaTime =time.time()-startTime;
        #if ((r-1)%5)==0:
        #    print("NumAtROutPre created in %f seconds, retofROutRPre  in %f seconds for size r=%g " \
        #        % (deltaTimeN,deltaTime,r))

    return [retofROutR, retofROutI, n1ofROut,n2ofROut,NumAtROut]

#%%     Section -1 Function Definitions %    For a given shell, this function returns whether a pair are close or not

def AveragesOnShellsInnerLogicKernelCuda(\
                                         kXNow,kYNow,kZNow,\
                                         #NumAtROutPre_global_mem,\
                                         #Prod11_global_mem,\
                                         #kXofR_global_mem,\
                                         #kYofR_global_mem,\
                                         #kZofR_global_mem,\
                                         retofRR_global_mem,\
                                         retofRI_global_mem,\
                                         n1ofR_global_mem,\
                                         n2ofR_global_mem,\
                                         NumOnSurf,\
                                         Thresh,\
                                         Start,\
                                         End,\
                                         r):

    stream = cuda.stream()
    time_to_device = time.time()

    NumAtROutPre_global_mem = cuda.device_array((NumOnSurf,End-Start))
    Prod11_global_mem = cuda.device_array(NumOnSurf,stream=stream,dtype=np.float32)

    # Set threads per block and blocks per grid
    threadsperblock = (1024,1,1)
    blockspergrid_x = int(math.ceil(kXNow[:NumOnSurf].shape[0] / threadsperblock[0]))
    blockspergrid = (blockspergrid_x,1,1)
    start_cuda = time.time()

    # Kernel 1, calculate Prod11
    cuda_kernels.cuda_calcProd11[blockspergrid, threadsperblock,stream](\
                                                                        kXNow,kYNow,kZNow,\
                                                                        #kXofR_global_mem,\
                                                                        #kYofR_global_mem,\
                                                                        #kZofR_global_mem,\
                                                                        Prod11_global_mem,\
                                                                        NumOnSurf,\
                                                                        r)

    stream.synchronize()
    # Kernel 2, calculate Inner2
    cuda_kernels.cuda_calcInner2[blockspergrid, threadsperblock,stream](\
                                                                        kXNow,kYNow,kZNow,\
                                                                        #kXofR_global_mem,\
                                                                        #kYofR_global_mem,\
                                                                        #kZofR_global_mem,\
                                                                        Prod11_global_mem,\
                                                                        NumAtROutPre_global_mem,\
                                                                        End,\
                                                                        Start,\
                                                                        Thresh,\
                                                                        NumOnSurf,\
                                                                        r)
    stream.synchronize()
    end_cuda = time.time()
    print("\nCUDA inner calculations completed in "+str(end_cuda - start_cuda)+".")
    return NumAtROutPre_global_mem

def AveragesOnShellsInnerLogicCCuda(\
                                    retNowR_global_mem,\
                                    retNowI_global_mem,\
                                    n1ofR_global_mem,\
                                    n2ofR_global_mem,\
                                    NumAtROutPre_global_mem,\
                                    End,\
                                    Start,\
                                    NumOnSurf,\
                                    r):

    threadsperblock = (1024,1,1)
    blockspergrid_x = int(math.ceil(retNowR_global_mem[r][:NumOnSurf].shape[0]/threadsperblock[0]))
    blockspergrid = (blockspergrid_x,1,1)
    # set up stream
    stream = cuda.stream()
    device_array_start = time.time()
    reduced_global_mem = cuda.device_array((4,(End-Start)))
    print("Shape of NumAtROutPre_global_mem is ",NumAtROutPre_global_mem.shape)
    print("Shape of reduced_global_mem is ",reduced_global_mem.shape)
    #print("Time to create cuda.device_array is ",time.time() - device_array_start)
    filter_time = time.time()
    cuda_kernels.filter_and_sum[threadsperblock,blockspergrid](\
        retNowR_global_mem,\
        retNowI_global_mem,\
        n1ofR_global_mem,\
        n2ofR_global_mem,\
        NumAtROutPre_global_mem,\
        reduced_global_mem,\
        End,\
        Start,\
        NumOnSurf,\
        r)
    stream.synchronize()
    print("Time to complete filter_and_sum is ",time.time() - filter_time)
    reduced_start = time.time()
    reduced = reduced_global_mem.copy_to_host()
    print("Time to transfer reduced to host is ",time.time() - reduced_start)

    return reduced

# From Analysis script
def threshold_binarize_array_cuda(dataarray, FSCCutoff, ThresholdForSphericity, highpassfilter, apix):

        # Thresholds
        cutoff_fsc = float(FSCCutoff)
        cutoff_binarize = float(ThresholdForSphericity)
        min_cutoff = min(cutoff_fsc,cutoff_binarize)

        # Coordinates
        center = (dataarray.shape[0]/2,dataarray.shape[1]/2,dataarray.shape[2]/2)
        #radius = int(inputmrc.shape[0]/2 + 0.5)

        # Fill up new np array
        boxsize = dataarray.shape[0]
        outarraythresholded = np.zeros((boxsize,)*3)
        outarraythresholdedbinarized = np.zeros((boxsize,)*3)

        points_array = cuda_kernels.calcDistance(boxsize,center)

        start_time = time.time()
        sorted_indexes = np.lexsort((points_array[:,1],points_array[:,0]))
        points_array = points_array[sorted_indexes]
        print("Sorting time is ",time.time() - start_time)

        counter = 0
        total_iterations = len(points_array)
        number_of_progress_bar_updates = 200
        iterations_per_progress_bar_update = int(total_iterations/number_of_progress_bar_updates)

        memory_inmrc_thresholded = np.copy(dataarray)
        memory_inmrc_thresholdedbinarized = np.copy(dataarray)
        outarraythresholded,outarraythresholdedbinarized = cuda_kernels.calcNeighbors(\
                    points_array,\
                    center,\
                    cutoff_fsc,\
                    cutoff_binarize,\
                    highpassfilter,\
                    min_cutoff,\
                    dataarray,\
                    memory_inmrc_thresholded,\
                    memory_inmrc_thresholdedbinarized,\
                    outarraythresholded,\
                    outarraythresholdedbinarized)

        return outarraythresholded,outarraythresholdedbinarized


#!/usr/bin/python3
import json
import time
import sys
import socket
import urllib.request

with open('/etc/energy-monitoring-with-graphite/clairvoyance/settings.json') as json_data:
    d = json.load(json_data)
    remote_server = d['remoteServer']
    remote_port = d['remotePort']
    addresses = d['addresses']
    metrics = d['metrics']
    paths = d['paths']
    interval = d['interval']
    server = d['server']
    port = d['port']

t_prev = ''
while True:
    t_start = time.time()
    t = ''
    timestamp_prev = -1
    for a in addresses:
        f = urllib.parse.urlencode({"formula": str(",".join(metrics))})
        a = urllib.parse.urlencode({"address": a})
        url = "http://" + remote_server + ":" + str(remote_port) + "/forecast?" + f + "&" + a
        print("url: " + url)
        try:
            j = json.loads(urllib.request.urlopen(url).read())
        except:
            print('clairvoyance: could not connect to server')
            continue

        #print("anser: " + str(json.dumps(j, indent=2)))
        n = 0
        timestamp = j["time"]
        for m in metrics:
            a_ = a.replace(".", "_").replace(" ", "_").replace("address=","").replace("+", "_").replace("%2C", "").replace("%5C", "")
            p = paths[n] + "." + a_

            value = ""
            t_value = 0
            try:
                data = j[m]
                for d in data:
                    if timestamp < d["time"]:
                        value = d["value"]
                        t_value = d["time"]
                        break
            except:
                print('clairvoyance: got an error')
                pass

            v = str(value)

            if p != '' and t_value != 0:
                t = t + p + ' ' + v + ' ' + str(int(t_value)) + '\n'

            n = n + 1

    if t != '' and t != t_prev:
        try:
            print("Sending " + str(t))
            s = socket.socket()
            s.connect((server, port))
            s.send(t.encode('ascii'))
            s.close()
            t_prev = t
        except:
            print(time.ctime(t_start) + ' clairvoyance: Error: Could not send message.')

    t_now = time.time()
    if (t_now - t_start) < interval:
        time.sleep(interval - (t_now - t_start))


from tensorflow.keras.models import save_model
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from helpers.ml import (extract_features, create_preprocess_pipeline, build_model,
                        split_dataframe_on_columns, transform_rnn_sequences,
                        create_target_scaler)
from helpers.database import InfluxdbQuery
from config import HOME
import pandas as pd
import joblib
import os

class MLTools():

    lookback = 48
    lookahead = 24
    binary_dir = os.path.join(HOME, 'btc_guru', 'bin')
    preprocess_pipeline_path = os.path.join(binary_dir, 'preprocess_pipeline.pkl')
    target_scaler_path = os.path.join(binary_dir, 'target_scaler.pkl')
    rnn_model_path = os.path.join(binary_dir, 'rnn_model.h5')

    @staticmethod
    def train_model() -> None:
        target_scaler = create_target_scaler()
        preprocess_pipeline = create_preprocess_pipeline()
        input_data = InfluxdbQuery(dict(fields='open,high,low,close,volume', result_limit=100000)).query()
        input_data = input_data.sort_values(by='time', ascending=True).set_index('time')
        input_data_features = extract_features(input_data, lookahead=MLTools.lookahead)
        input_data_features = input_data_features.pipe(lambda df: df[~pd.isna(df['target'])])
        dataframe_x, dataframe_y = split_dataframe_on_columns(input_data_features, ['target'])
        X_train, X_val, y_train, y_val = train_test_split(dataframe_x.values, dataframe_y.values,
                                                          test_size=0.03, random_state=42, shuffle=False)

        X_train = preprocess_pipeline.fit_transform(X_train)
        X_val = preprocess_pipeline.transform(X_val)

        y_train = target_scaler.fit_transform(y_train)
        y_val = target_scaler.transform(y_val)

        X_train, y_train = transform_rnn_sequences(X_train, y_train, lookback=MLTools.lookback,
            lookahead=MLTools.lookahead)
        X_val, y_val = transform_rnn_sequences(X_val, y_val, lookback=MLTools.lookback,
            lookahead=MLTools.lookahead)

        joblib.dump(preprocess_pipeline, MLTools.preprocess_pipeline_path)
        joblib.dump(target_scaler, MLTools.target_scaler_path)

        rnn_model = build_model(input_shape=X_train.shape[1:], output_neurons=y_train.shape[1])
        rnn_model.fit(X_train, y_train,
                      validation_data=(X_val, y_val),
                      epochs=30,
                      batch_size=32,
                      callbacks=[EarlyStopping(restore_best_weights=True, patience=2, monitor='val_loss')])

        save_model(rnn_model, MLTools.rnn_model_path)


"""Test frostfree class"""
import time
import unittest
from datetime import datetime
from threading import Thread
from typing import Optional

from src.localStorage.persistence.persistence import Persistence
from src.shared.enum.state import State
from src.zone.dto.info_frostfree import InfoFrostfree
from src.zone.frostfree import Frostfree
from src.zone.zone import Zone
from test.helpers.patchs.config_patch import ConfigPatch
from test.helpers.patchs.persistence_patch import PersistencePatch
from test.helpers.patchs.zone_patch import ZonePatch

ZONE1 = "zone1"

class TestFrostFree(unittest.TestCase):

    def setUp(self):
        ZonePatch.start_patch(self)
        PersistencePatch.start_patch(self)
        ConfigPatch.start_patch(self)

        self.thread = None
        self.zone: Optional[Zone] = None
        self.frostfree: Optional[Frostfree] = None

    def tearDown(self) -> None:
        ZonePatch.stop_patch(self)
        if self.frostfree:
            self.frostfree.stop_loop()
        if self.zone:
            self.zone.stop_loop()
        if self.thread:
            self.thread.join()

        PersistencePatch.stop_patch(self)
        ConfigPatch.stop_patch(self)

    def start_thread(self):
        self.thread = Thread(target=self.run_frostfree())
        self.thread.start()
        time.sleep(0.5)

    def run_frostfree(self):
        self.zone = Zone(1)
        time.sleep(0.5)
        self.frostfree = Frostfree([self.zone])

    def test_restore(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))

        self.start_thread()

        self.assertNotEqual(self.frostfree.end_date, None)
        self.assertEqual(self.zone.current_state, State.FROSTFREE)

    def test_restore_end_date_expired(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp()))

        self.start_thread()

        self.assertEqual(self.frostfree.end_date, None)
        self.assertNotEqual(self.zone.current_state, State.FROSTFREE)

    def test_can_stopped(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))

        self.start_thread()
        self.frostfree.stop()

        self.assertEqual(self.frostfree.end_date, None)
        self.assertNotEqual(self.zone.current_state, State.FROSTFREE)

    def test_get_data(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))
        self.start_thread()
        info_frostfree = InfoFrostfree(self.frostfree.end_date)
        result = self.frostfree.get_data()

        self.assertEqual(info_frostfree, result)

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


try:
    import uuid

    from datetime import datetime
    import os
    import logging
    import uuid
    import re
    from functools import wraps
    from enum import Enum
    from abc import ABC, abstractmethod
    from logging import StreamHandler

    from faker import Faker

    import pynamodb.attributes as at
    from pynamodb.models import Model
    from pynamodb.attributes import *
    from time import sleep
    from dotenv import load_dotenv
    load_dotenv(".env")
except Exception as e:
    raise Exception("Error : {}".format(e))

class DataGenerator(object):

    @staticmethod
    def get_data():
        faker = Faker()

        name = faker.name().split(" ")
        first_name = name[0]
        last_name = name[1]
        address = faker.address()
        text = faker.text()
        id = uuid.uuid4().__str__()
        city = faker.city()
        state = faker.state()

        _ = {
            "first_name": first_name,
            "last_name": last_name,
            "address": address,
            "text": text,
            "id": id,
            "city": city,
            "state": state
        }

        return _

class Users(Model):
    class Meta:
        table_name = os.getenv("DYNAMO_DB_TABLE_NAME")
        region = "us-east-1"
        aws_access_key_id =  os.getenv("DEV_ACCESS_KEY")
        aws_secret_access_key =  os.getenv("DEV_SECRET_KEY")

    pk = UnicodeAttribute(hash_key=True)
    sk= UnicodeAttribute(range_key=True)
    first_name = UnicodeAttribute()
    last_name = UnicodeAttribute()
    address = UnicodeAttribute()
    text = UnicodeAttribute()
    city = UnicodeAttribute()
    state = UnicodeAttribute()

def generate_data():
    for i in range(1, 500):
        json_data = DataGenerator.get_data()
        print(json_data)
        Users(
            pk=json_data.get("id").__str__(),
            sk=json_data.get("id").__str__(),
            first_name=json_data.get("first_name"),
            last_name=json_data.get("last_name"),
            address=json_data.get("address"),
            text=json_data.get("text"),
            city=json_data.get("address"),
            state=json_data.get("text")
        ).save()

generate_data()


import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

T =  [76.7, 80.9, 82.2, 86.2, 88.3, 91, 92.3, 93.5, 95.6, 97.7, 99.3] # boiling temperatures
P = [43.1, 56.3, 62.8, 64, 75.1, 79.9, 80.8, 90.3, 91.1, 101.4, 101.4] # pressure

T = np.array(T) + 273.5
slope, intercept, r, p, se = stats.linregress(1/T, np.log(P))
R = 8.314

x = np.linspace(0.010, 0.0130, 1000)
plt.scatter(1/T, np.log(P), color = "red", label = "Data")
plt.plot(x, slope*x + intercept, color = "blue", label = "Linear regression")
plt.xlabel("ln(P)")
plt.ylabel("1/T")
#plt.show()

Hv = -slope*R
Tru = Hv/(R*T)
print(slope)
print(Hv)
print(Tru)


import boto3
import time

sleep_time = 5

def StartDocumentTextDetection(s3BucketName, objectName):
    response = None
    client = boto3.client('textract')
    response = client.start_document_text_detection(
        DocumentLocation={
            'S3Object': {
                'Bucket': s3BucketName,
                'Name': objectName
            }
        }
    )
    return response["JobId"]

def isJobComplete(jobId):
    time.sleep(sleep_time)
    client = boto3.client('textract')
    response = client.get_document_text_detection(JobId=jobId)
    status = response["JobStatus"]
    print("Job status: {}".format(status))

    while (status == "IN_PROGRESS"):
        time.sleep(sleep_time)
        response = client.get_document_text_detection(JobId=jobId)
        status = response["JobStatus"]
        print("Job status: {}".format(status))

    return status

def getDocumentTextDetection(jobId):
    isJobComplete(jobId)

    client = boto3.client('textract')
    response = client.get_document_text_detection(JobId=jobId)
    status = response["JobStatus"]

    return response


import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate

fruits = np.load('fruits_300.npy')
fruits_2d = fruits.reshape(-1, 100*100)
print(fruits.shape)

apple = fruits[0:100].reshape(-1, 100*100)
pineapple = fruits[100:200].reshape(-1, 100*100)
banana = fruits[200:300].reshape(-1, 100*100)

print(apple.shape)

# 샘플 평균의 히스토그램 - 픽셀값 분석
plt.hist(np.mean(apple, axis=1), alpha=0.8)
plt.hist(np.mean(pineapple, axis=1), alpha=0.8)
plt.hist(np.mean(banana, axis=1), alpha=0.8)
plt.legend(['apple', 'pineapple', 'banana'])
plt.show()

# 픽셀 평균의 히스토그램
fig, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].bar(range(10000), np.mean(apple, axis=0))
axs[1].bar(range(10000), np.mean(pineapple, axis=0))
axs[2].bar(range(10000), np.mean(banana, axis=0))
plt.show()

# 평균 이미지 그리기
apple_mean = np.mean(apple, axis=0).reshape(100, 100)
pineapple_mean = np.mean(pineapple, axis=0).reshape(100, 100)
banana_mean = np.mean(banana, axis=0).reshape(100, 100)

fig, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].imshow(apple_mean, cmap='gray_r')
axs[1].imshow(pineapple_mean, cmap='gray_r')
axs[2].imshow(banana_mean, cmap='gray_r')
plt.show()

# 평균과 가까운 사진 고르기
abs_diff = np.abs(fruits - apple_mean)
abs_mean = np.mean(abs_diff, axis=(1, 2))
print(abs_mean.shape)

apple_index = np.argsort(abs_mean)[:100]
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
for i in range(10):
	for j in range(10):
		axs[i, j].imshow(fruits[apple_index[i*10 + j]], cmap='gray_r')
		axs[i, j].axis('off')
plt.show()

km = KMeans(n_clusters=3, random_state=42)
km.fit(fruits_2d)

print(km.labels_)

print(np.unique(km.labels_, return_counts=True))

def draw_fruits(arr, ratio=1):
	n = len(arr)	# n은 샘플 개수
	# 한 줄에 10개씩 이미지를 그립니다. 샘플 개수를 10으로 나누어 전체 행 개수를 계산
	rows = int(np.ceil(n / 10))
	# 행이 1개이면 열 개수는 샘플 개수. 그렇지 않으면 10개
	cols = n if rows < 2 else 10
	fig, axs = plt.subplots(rows, cols, figsize=(cols*ratio, rows*ratio), squeeze=False)

	for i in range(rows):
		for j in range(cols):
			if i*10 + j < n:
				axs[i, j].imshow(arr[i*10 + j], cmap='gray_r')
			axs[i, j].axis('off')
	plt.show()

draw_fruits(fruits[km.labels_==0])

draw_fruits(km.cluster_centers_.reshape(-1, 100, 100), ratio=3)

print(km.transform(fruits_2d[100:101]))

print(km.predict(fruits_2d[100:101]))

draw_fruits(fruits[100:101])

print(km.n_iter_)

# 최적의 K 찾기(클러스의 수 정하기)
inertia = []				# interia : 군집 내 샘플들과 군집 중심점의 거리 차이 --> 값이 적다는 것은 중심점 주변에 밀집해 있다는 뜻
for k in range(2, 7):
	km = KMeans(n_clusters=k, random_state=42)
	km.fit(fruits_2d)
	inertia.append(km.inertia_)

plt.plot(range(2, 7), inertia)
plt.xlabel('k')
plt.ylabel('inertia')
plt.show()

pca = PCA(n_components=50)
pca.fit(fruits_2d)

print(pca.components_.shape)

draw_fruits(pca.components_.reshape(-1, 100, 100))

print(fruits_2d.shape)

fruits_pca = pca.transform(fruits_2d)
print(fruits_pca.shape)

# 재구성
fruits_inverse = pca.inverse_transform(fruits_pca)
print(fruits_inverse.shape)

fruits_reconstruct = fruits_inverse.reshape(-1, 100, 100)

# 설명된 분산
print(np.sum(pca.explained_variance_ratio_))

plt.plot(pca.explained_variance_ratio_)

# 분류기와 함께 사용하기
lr = LogisticRegression()
target = np.array([0] * 100 + [1] * 100 + [2] * 100)

scores = cross_validate(lr, fruits_2d, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

scores = cross_validate(lr, fruits_pca, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

pca = PCA(n_components=0.5)
pca.fit(fruits_2d)
print(pca.n_components_)

fruits_pca = pca.transform(fruits_2d)
print(fruits_pca.shape)

scores = cross_validate(lr, fruits_pca, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

# 군집과 함께 사용하기
km = KMeans(n_clusters=3, random_state=42)
km.fit(fruits_pca)

print(np.unique(km.labels_, return_counts=True))

# 시각화
for label in range(0, 3):
	data = fruits_pca[km.labels_ == label]
	plt.scatter(data[:, 0], data[:, 1])

plt.legend(['apple', 'banana', 'pineapple'])
plt.show()


# lista 6

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

def poisson_process(T, lamb):
    interarrival_times = np.random.exponential(scale=1/lamb, size=int(np.ceil(lamb*T)))
    jump_times = np.cumsum(interarrival_times)
    jump_times = jump_times[jump_times < T]
    return jump_times

jump_times = poisson_process(10, 1.5)
print("Czas skoków:", jump_times)

plt.plot(jump_times, np.arange(len(jump_times)),drawstyle='steps-post')
plt.xlabel("czas")
plt.show()

jump_times = poisson_process(10000, 1.5)
print("Czas skoków:", jump_times)
plt.plot(jump_times, np.arange(len(jump_times)),drawstyle='steps-post')
plt.xlabel("czas")
plt.show()

def compute_N(t, times):
    # znalezienie indeksu pierwszej wartości większej lub równej t
    Nt = np.searchsorted(times, t)
    return Nt

t = 5
Ns = [compute_N(t, poisson_process(10, 2)) for _ in  range(10000)]
plt.hist(Ns, bins=20)
plt.show()

sns.ecdfplot(Ns)

x = np.arange(0, 45, 0.001)
dist = lambda x: stats.poisson.cdf(x, 2*t)
y = dist(x)
plt.plot(x, y)
plt.show()

samples_poisson_theoretical=np.random.poisson(2*t,size=10**4)
print(stats.kstest(Ns,samples_poisson_theoretical))


try:
    import slugify as slugify_lib
except ImportError:
    slugify_lib = None

import werkzeug
from openerp.osv import osv, fields

def urlplus(url, params):
    return werkzeug.Href(url)(params or None)

class ir_attachment(osv.osv):
    _inherit = "ir.attachment"

    def _website_url_get(self, cr, uid, ids, name, arg, context=None):
        result = {}
        for attach in self.browse(cr, uid, ids, context=context):
            if attach.type == 'url':
                result[attach.id] = attach.url
            else:
                result[attach.id] = urlplus('/website/image', {
                    'model': 'ir.attachment',
                    'field': 'datas',
                    'id': attach.id,
                    'max_width': 1024,
                    'max_height': 768,
                })
        return result
    _columns = {
        'website_url': fields.function(
            _website_url_get, string="Attachment URL", type='char')
    }

    def try_remove(self, cr, uid, ids, context=None):
        """ Removes a web-based image attachment if it is used by no view
        (template)

        Returns a dict mapping attachments which would not be removed (if any)
        mapped to the views preventing their removal
        """
        Views = self.pool['ir.ui.view']
        attachments_to_remove = []
        # views blocking removal of the attachment
        removal_blocked_by = {}

        for attachment in self.browse(cr, uid, ids, context=context):
            # in-document URLs are html-escaped, a straight search will not
            # find them
            url = werkzeug.utils.escape(attachment.website_url)
            ids = Views.search(
                cr, uid, [('arch', 'like', url)], context=context)

            if ids:
                removal_blocked_by[attachment.id] = Views.read(
                    cr, uid, ids, ['name'], context=context)
            else:
                attachments_to_remove.append(attachment.id)
        if attachments_to_remove:
            self.unlink(cr, uid, attachments_to_remove, context=context)
        return removal_blocked_by


# Must change database parameter in mysql section of database.ini to test before test.

from config import config
from multiprocessing.dummy import Pool
from mysql.connector import MySQLConnection
import sys

urls = ([
         'google.com',
         'facebook.com',
         'instragram.com',
         'twitter.com',
         'hotmail.com',
         'gmail.com',
         'joojle.com',
         'line.com',
         'afaps.ac.th',
         'kmutnb.ac.th',
         'crma.ac.th',
         'rtna.ac.th',
         'rtaf.ac.th',
         'rpca.ac.th'
     ])

def insert(url):
    sql = "INSERT INTO test_pool (url) VALUES (%s);"
    conn = MySQLConnection(**params)
    cursor = conn.cursor()
    cursor.execute(sql, (url,))
    cursor.close()
    conn.commit()
    sql = "SELECT MAX(url_id) FROM test_pool;"
    cursor = conn.cursor()
    cursor.execute(sql)
    url_id = cursor.fetchone()[0]
    cursor.close()
    conn.close()
    return (url_id, url)

def create():
    sql = "CREATE TABLE IF NOT EXISTS test_pool (url_id INT NOT NULL AUTO_INCREMENT, url VARCHAR(255) NOT NULL, PRIMARY KEY (url_id)) ;"
    conn = MySQLConnection(**params)
    cursor = conn.cursor()
    cursor.execute(sql)
    cursor.close()
    conn.commit()
    conn.close()

def drop():
    sql = "DROP TABLE test_pool;"
    conn = MySQLConnection(**params)
    cursor = conn.cursor()
    cursor.execute(sql)
    cursor.close()
    conn.commit()
    conn.close()

def select():
    sql = "SELECt * FROM test_pool;"
    conn = MySQLConnection(**params)
    cursor = conn.cursor()
    cursor.execute(sql)
    rows = cursor.fetchall()
    cursor.close()
    conn.close()
    return set(rows)

p = Pool(2)
log = dict()
result_list = list()
params = config(section='mysql')
chunk_size = sys.argv[1]

if chunk_size <= sys.maxsize and chunk_size >=0:
    create()
    for res in p.imap_unordered(insert, urls, chunk_size):
        result_list.append((res[0], res[1]))
    r = select()
#    print(r)
    print(r == set(result_list))
    drop()
else:
    print("chunk_size must >=0 and <= {}".format(sys.maxsize))
# Notice if chunksize of imap_unordered greater than or equal to nmber of elements of url it will result as True otherwise false.


import base64
import zlib

import numpy as np
from pycocotools import _mask as coco_mask

def encode_binary_mask(mask: np.ndarray) -> str:
    """Converts a binary mask into OID challenge encoding ascii text.

    The original implemantation is below.
    https://gist.github.com/pculliton/209398a2a52867580c6103e25e55d93c
    """

    # check input mask --
    if mask.dtype != np.bool8:
        raise ValueError("encode_binary_mask expects a binary mask, received dtype == %s" % mask.dtype)

    mask = np.squeeze(mask)
    if len(mask.shape) != 2:
        raise ValueError("encode_binary_mask expects a 2d mask, received shape == %s" % mask.shape)

    # convert input mask to expected COCO API input --
    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
    mask_to_encode = mask_to_encode.astype(np.uint8)
    mask_to_encode = np.asfortranarray(mask_to_encode)

    # RLE encode mask --
    encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]

    # compress and base64 encoding --
    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
    base64_str = base64.b64encode(binary_str)
    return base64_str.decode()


from typing import Any, Mapping

from official.vision.serving import detection

class DetectionModule(detection.DetectionModule):
  """A serving detection module for exporting the model.

  This module overrides the tensorflow_models DetectionModule by only outputting
    the pre-nms detection_boxes and detection_scores.
  """

  def serve(self, images) -> Mapping[str, Any]:
    result = super().serve(images)
    final_outputs = {
        'detection_boxes': result['detection_boxes'],
        'detection_scores': result['detection_scores'],
    }
    return final_outputs


import numpy as np
import math
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import BoxSelectTool, PanTool, WheelZoomTool, ResetTool, HoverTool,SaveTool,LassoSelectTool,Circle,ColumnDataSource
from bokeh.layouts import gridplot

#output file
output_file("520464625.html")

#Gathering my data
rankings = pd.read_csv("integrated_dataset.csv")
#Important to make the lists/series/array the same size
rankings_restricted = rankings.dropna()
#I was unsure which data to takeout, and this system makes sure I can easily change the column extracted.
extra1 = "QS2023 Employer Reputation"
extra2 = "QS2023 Faculty Student Ratio"
extra3 = "QS2023 Citations per Faculty"
#First data variables
#linking the data together, for selection tools.
ColumnDataSource
data = {extra1: rankings_restricted[extra1],
        extra2: rankings_restricted[extra2],
        extra3: rankings_restricted[extra3],
        "QS2023 Academic Reputation": rankings_restricted["QS2023 Academic Reputation"],
        "THE2020 Overall Score":rankings_restricted["THE2020 Overall Score"]
        }
source = ColumnDataSource(data=data)
#My list of interactive tools
tools = [
        #I set the box select tool to width because there is always a direct correlation between y values if the y values are the same
        #As a result I restricted the the tool to only the width, so that the user can only look at the correlation between the x variables.
        BoxSelectTool(dimensions="width"),
        #The pan + wheel zoom tools, allow for the user to move around while zoomed in, if they wihs to only look at specific parts.
        PanTool(),
        WheelZoomTool(),
        #
        ResetTool(),
        #Allows the user to see the point, and their corresponding values
        # Unfortunately bokeh, unlike the boxselect tool, can not hover a point, and show it's repective other points
        HoverTool(tooltips = [("(x,y)", "($x, $y)")]),
        #Allows the user to save the modified version of the data.
        SaveTool()
         ]
#Initilizing the graph
p1 = figure(title="QS2023 Academic Reputation and THE2020 Overall Score",
           tools=tools,
           #all the scores are beteween 0 and 100
           x_range=(0, 100),
           y_range=(0, 100),
           x_axis_label="QS2023 Academic Reputation",
           y_axis_label="THE2020 Overall Score")
#This is a customization towards the select, box and lasso tools, There is a reason for the different colours, explained in the report.
r = p1.circle(x = "QS2023 Academic Reputation",
             y = "THE2020 Overall Score"
             , source =  source,fill_color="lightgreen", size=10, alpha=0.5,line_color=None)
r.selection_glyph = Circle(fill_color="lightgreen", line_color=None, fill_alpha=1)
r.nonselection_glyph = Circle(fill_color=None, line_color=None, fill_alpha=0.25)

#show(p1)#Making sure graph 1 works

#graph 2
p2 =  figure(title = extra1 + " and THE2020 Overall Score",
            tools=tools,
            #all the scores are beteween 0 and 100
            x_range=(0, 100),
            y_range=(0, 100),
            x_axis_label= extra1  ,
            y_axis_label="THE2020 Overall Score"
            )

r = p2.circle(x = extra1, y = "THE2020 Overall Score", source =  source,fill_color="orange", size=10, alpha=0.5,line_color=None)
r.selection_glyph = Circle(fill_color="orange", line_color=None, fill_alpha=1)
r.nonselection_glyph = Circle(fill_color=None, line_color=None, fill_alpha=0.25)

#graph 3
p3 =  figure(title = extra2 + " and THE2020 Overall Score",
            tools=tools,
            #all the scores are beteween 0 and 100
            x_range=(0, 100),
            y_range=(0, 100),
            x_axis_label= extra2  ,
            y_axis_label="THE2020 Overall Score"
            )

r = p3.circle(x = extra2, y = "THE2020 Overall Score", source =  source,fill_color="violet", size=10, alpha=0.5,line_color=None)
r.selection_glyph = Circle(fill_color="violet", line_color=None, fill_alpha=1)
r.nonselection_glyph = Circle(fill_color=None, line_color=None, fill_alpha=0.25)

#graph 4
p4 =  figure(title = extra3 + " and THE2020 Overall Score",
            tools=tools,
            #all the scores are beteween 0 and 100
            x_range=(0, 100),
            y_range=(0, 100),
            x_axis_label= extra3  ,
            y_axis_label="THE2020 Overall Score"
            )

r = p4.circle(x = extra3, y = "THE2020 Overall Score", source =  source,fill_color="turquoise", size=10, alpha=0.5,line_color=None)
r.selection_glyph = Circle(fill_color="turquoise", line_color=None, fill_alpha=0.65)
r.nonselection_glyph = Circle(fill_color=None, line_color=None, fill_alpha=0.25)
#I made sure to use light colours for consistency.

#The reason the arguments in this grid plot is like this, is because a 2*2 grid html is  better to look at than a 1*4 line of graphs
p = gridplot([[p1, p2],[p3,p4]])
#shows graph ( ° ͜ʖ °)
show(p)


import os
import numpy as np
import pandas as pd
import networkx as nx
import community
feat_file_name = "feature_map.txt"
feature_index = {}  # numeric index to name
inverted_feature_index = {} # name to numeric index
network = nx.Graph()

def parse_featname_line(line):
  """ used to parse each line of the files containing feature names """
  line = line[(line.find(' '))+1:]  # chop first field
  split = line.split(';')
  name = ';'.join(split[:-1]) # feature name
  index = int(split[-1].split(" ")[-1]) #feature index
  return index, name

def load_features():
  """
  parse each ego-network and creates two dictionaries:
      - feature_index: maps numeric indices to names
      - inverted_feature_index: maps names to numeric indices
  """
  import glob
  feat_file_name = 'tmp.txt'
  # may need to build the index first
  if not os.path.exists(feat_file_name):
      feat_index = {}
      # build the index from data/*.featnames files
      featname_files = glob.iglob("facebook/*.featnames")
      for featname_file_name in featname_files:
          featname_file = open(featname_file_name, 'r')
          for line in featname_file:
              # example line:
              # 0 birthday;anonymized feature 376
              index, name = parse_featname_line(line)
              feat_index[index] = name
          featname_file.close()
      keys = feat_index.keys()
      keys = sorted(keys)
      out = open(feat_file_name,'w')
      for key in keys:
          out.write("%d %s\n" % (key, feat_index[key]))
      out.close()

  index_file = open(feat_file_name,'r')
  for line in index_file:
      split = line.strip().split(' ')
      key = int(split[0])
      val = split[1]
      feature_index[key] = val
  index_file.close()

  for key in feature_index.keys():
      val = feature_index[key]
      inverted_feature_index[val] = key

def parse_nodes(network, ego_nodes):
  """
  for each nodes in the network assign the corresponding features
  previously loaded using the load_features function
  """
  # parse each node
  for node_id in ego_nodes:
      featname_file = open(f'facebook/{node_id}.featnames','r')
      feat_file     = open(f'facebook/{node_id}.feat','r')
      egofeat_file  = open(f'facebook/{node_id}.egofeat','r')
      edge_file     = open(f'facebook/{node_id}.edges','r')

      ego_features = [int(x) for x in egofeat_file.readline().split(' ')]

      # Add ego node features
      network.nodes[node_id]['features'] = np.zeros(len(feature_index))

      # parse ego node
      i = 0
      for line in featname_file:
          key, val = parse_featname_line(line)
          # Update feature value if necessary
          if ego_features[i] + 1 > network.nodes[node_id]['features'][key]:
              network.nodes[node_id]['features'][key] = ego_features[i] + 1
          i += 1

      # parse neighboring nodes
      for line in feat_file:
          featname_file.seek(0)
          split = [int(x) for x in line.split(' ')]
          node_id = split[0]
          features = split[1:]

          # Add node features
          network.nodes[node_id]['features'] = np.zeros(len(feature_index))

          i = 0
          for line in featname_file:
              key, val = parse_featname_line(line)
              # Update feature value if necessary
              if features[i] + 1 > network.nodes[node_id]['features'][key]:
                  network.nodes[node_id]['features'][key] = features[i] + 1
              i += 1

      featname_file.close()
      feat_file.close()
      egofeat_file.close()
      edge_file.close()

def get_shortest_path(G,u,v):
  """
  return the shortest path length between u,v in the graph
  without the edge (u,v)
  """
  removed = False
  if G.has_edge(u,v):
    removed = True
    G.remove_edge(u,v) # temporary remove edge
  try:
    sp = len(nx.shortest_path(G, u, v))
  except:
    sp = 0
  if removed:
    G.add_edge(u,v) # add back the edge if it was removed
  return sp

def get_hc_features(G, samples_edges, labels):
  # precompute metrics
  centralities = nx.degree_centrality(G)
  parts = community.best_partition(G)
  feats = []
  for (u,v),l in zip(samples_edges, labels):
    shortest_path = get_shortest_path(G, u, v)
    j_coefficient = next(nx.jaccard_coefficient(G, ebunch=[(u, v)]))[-1]
    u_centrality = centralities[u]
    v_centrality = centralities[v]
    u_community = parts.get(u)
    v_community = parts.get(v)
    # add the feature vector
    feats += [[shortest_path, j_coefficient, u_centrality, v_centrality]]
  return feats


import os
import argparse
import time
import torch
import torch.nn as nn
import numpy as np
from dataloader import get_mnist_loaders, get_cifar10_loaders, inf_generator
from utils import init_logger, RunningAverageMeter, accuracy
from model.mnist import mnist_model
from container import trainer

parser = argparse.ArgumentParser("MNIST")
parser.add_argument("--model", type=str, default="conv", choices=["conv", "wconv", "res"])
parser.add_argument("--epochs", type=int, default=100)
parser.add_argument("--lr", type=float, default=0.1)
parser.add_argument("--block", type=eval, default=1)
parser.add_argument("--hist", type=eval, default=False)
parser.add_argument("--norm", type=str, default="b")
parser.add_argument("--save", type=str, default="exp")
parser.add_argument("--alpha", type=float, default=0.05)
parser.add_argument("--iters", type=int, default=7)
parser.add_argument("--gpu", type=int, default=0)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--adv", type=str, default="none", choices=["none", "fgsm", "pgd", "ball"])
parser.add_argument("--opt", type=str, default="sgd", choices=["adam", "rms"])
parser.add_argument("--repeat", type=int, default=5)

args = parser.parse_args()

if __name__ == "__main__" :
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    np.random.seed(args.seed)
    args.save = os.path.join("experiments",args.save)
    if os.path.exists(args.save) :
        raise NameError("previous experiment '{}'already exists!".format(args.save))
    os.makedirs(args.save)

    logger = init_logger(logpath=args.save, experiment_name="logs-"+args.model)
    logger.info(args)

    if args.gpu >= 0 :
       args.device = torch.device("cuda:" + str(args.gpu) if torch.cuda.is_available() else "cpu")
    else :
        args.device = torch.device("cpu")
    model = mnist_model(args.model, layers=args.block, norm_type=args.norm)
    logger.info(model)
    model.to(args.device)

    train_loader, test_loader, train_eval_loader = get_mnist_loaders()
    loader = {"train_loader": train_loader, "train_eval_loader": train_eval_loader, "test_loader": test_loader}
    if args.opt == "sgd" :
        optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
        scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[30,60,90], gamma=0.1)
    elif args.opt == "adam" :
        optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, betas=(0., 0.9))
        scheduler = None
    elif args.opt == "rms" :
        optimizer = torch.optim.RMSprop(model.parameters(), lr=1e-3)
        scheduler = None

    adv_train = args.adv if args.adv != "none" else None
    model = trainer(model, logger, loader, args, "mnist", optimizer, scheduler, adv_train=adv_train)


from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)

from hbdeal.services import deal_service
from hbdeal.core.models.deal import User
from hbdeal.core.hb_api import HBEmptyApiTokenError

bp = Blueprint('deals', __name__, url_prefix='/deals')

@bp.route('/<user_id>', methods=('GET',))
def user_deal_list(user_id):
    user = deal_service.get_user(user_id)
    deals = deal_service.get_user_deals(user)

    return render_template('deals_list.html', user=user, deals=deals)

@bp.route('/<user_id>/update', methods=('GET',))
def user_deals_update(user_id):
    user = deal_service.get_user(user_id)
    try:
        deals = deal_service.update_last_deals(user)
    except HBEmptyApiTokenError:
        # If User has not a TOKEN it will redirected to get a new token from Hubspot
        return redirect(deal_service.get_user_hb_api_auth_url(user))

    return redirect("/deals/{}".format(user.id))


from PIL import Image
import numpy as np
from nodes import Node, NodePath
import colorsys
import glob

class ImageGenerator:
    def __init__(self, start_node, end_node, nodes, paths):
        self.start_node: Node = start_node
        self.end_node: Node = end_node
        self.nodes: list[Node] = nodes
        self.paths: list[NodePath] = paths
        self.rows = max([node.row for node in self.nodes])
        self._max_value = max([node.value for node in self.nodes])  # 26 for a-z
        self._base_pixel_array = np.array([])
        self.generate_base_image()

    def generate_base_image(self):
        pixels = []
        for _ in range(0, self.rows+1):
            pixels.append([])
        for node in self.nodes:
            value = round(node.value * (100 / self._max_value) + 1, 1)  # range (0-100)
            pixel = colorsys.hls_to_rgb(0.7, value, 1)
            pixels[node.row].append(pixel)
        pixels[self.start_node.row][self.start_node.col] = (0, 150, 50)
        pixels[self.end_node.row][self.end_node.col] = (255, 0, 0)
        self._base_pixel_array = np.array(pixels, dtype=np.uint8)
        image = Image.fromarray(self._base_pixel_array)
        image.save("maps/map0.png")

    def draw_paths(self, paths: list[NodePath]):
        new_pixels = self._base_pixel_array.copy()
        path = None
        for path in paths:
            new_pixels[path.last_node().row][path.last_node().col] = (255, 150, 0)
            self._base_pixel_array[path.nodes[-2].row][path.nodes[-2].col] = (200, 200, 200)
        image = Image.fromarray(new_pixels)
        image.save(f"maps/map{path.length()}.png")

    @staticmethod
    def make_gif(file_name, folder_path: str):
        # Create the frames
        frames = []
        images = sorted(glob.glob(f"{folder_path}/*.png"))
        for image in images:
            new_frame = Image.open(image)
            frames.append(new_frame)

        # Save into a GIF file that loops forever
        frames[0].save(f"{file_name}.gif", format='GIF',
                       duration=100,
                       append_images=frames[1:],
                       save_all=True,
                       loop=0)

Day12: Hill Climbing Algorithm/image_generator.py


"""
Zhen Lu, 2017 <albert.lz07@gmail.com>
plot Sandia Flame results, this version is only for browse, specific
settings for different variables are required for journal artworks
"""
import glob
import numpy as np
import file_read as fr
# suppress the display of matplotlib.pyplot
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt

# data to plot
# two vars at least, because axes would be 1D vector for one var
var  = ['Z','T','CO']

# import data
xD_value=[]
data={}
expr={}
for filename in glob.glob('mean_xD*.csv'):
    pos = filename.find('.csv')
    xD = filename[7:pos]
    z = fr.z_str_to_num(xD)
    if z >= 7.5 and z <= 45.0:
        xD_value.append(z)
        data.update({z:np.genfromtxt(filename,
                                     delimiter=',',
                                     names=True)})
        exp_name='../../../pmCDEFarchives/pmD.stat/D{}.Yave'.format(xD)
        expr.update({z:fr.sf_expr_read(exp_name)})
xD_value.sort()
for z in xD_value:
    data[z]['r'] /= z
    expr[z]['r'] /= z

# plot
# use TEX for interpreter
plt.rc('text',usetex=True)
# use serif font
plt.rc('font',family='serif')
# figure and axes parameters
# total width is fixed
plot_width      =19.0
subplot_h       =4.0
margin_left     =2.0
margin_right    =0.3
margin_bottom   =1.5
margin_top      =1.0
space_width     =0.0
space_height    =1.0
ftsize          =12
# total height determined by the number of vars
plot_height     =((subplot_h+space_height)*float(len(var))
                  -space_height+margin_top+margin_bottom)
# min and max of axis
xmin = 0.0
xmax = 0.3
xtick= tuple(np.arange(xmin,xmax,0.1))

# generate the figure
fig, axes = plt.subplots(len(var),len(xD_value),
                         sharex='col',sharey='row',
                         figsize=fr.cm2inch(plot_width, plot_height))
# generate the axis
for i,v in enumerate(var):
    for j,z in enumerate(xD_value):
        axes[i,j].plot(data[z]['r'],data[z][v],'-b',
                       label='Sim.',linewidth=1.5)
        axes[i,j].plot(expr[z]['r'],expr[z][v],'ok',
                       label='Exp.',linewidth=1.5)
    # ylabel, temperature has a unit
    if v == 'T':
        str_label=r"$\langle\tilde{{T}}\rangle\;(\mathrm{{K}})$"
    elif v == 'Z':
        str_label=r"$\langle\tilde{{Z}}\rangle$"
    else:
        str_label=r'$\langle\tilde Y_{\mathrm{'+v+r'}}\rangle$'
    axes[i,0].set_ylabel(str_label,fontsize=ftsize)
# title and xlabel
for j,z in enumerate(xD_value):
    #title
    axes[0,j].set_title('$x/D={0:.2g}$'.format(z),
                        fontsize=ftsize)
    #r/x
    axes[-1,j].set_xlabel('$r/x$',
                          fontsize=ftsize)
    axes[-1,j].set_xlim(xmin,xmax)
    axes[-1,j].set_xticks(xtick)

axes[-1,-1].set_xticks(xtick+(xmax,))

# legend
axes[0,-1].legend(fontsize=ftsize,
                  numpoints=1,
                  frameon=False)

# set margins
plt.subplots_adjust(left    =margin_left/plot_width,
                    bottom  =margin_bottom/plot_height,
                    right   =1.0-margin_right/plot_width,
                    top     =1.0-margin_top/plot_height,
                    wspace  =space_width/plot_width,
                    hspace  =space_height/plot_height)

# save plot
plt.savefig('radial_ave.png',dpi=400)
plt.savefig('radial_ave.pdf')
plt.savefig('radial_ave.eps')

# plot the rms
# generate the figure
fig, axes = plt.subplots(len(var),len(xD_value),
                         sharex='col',sharey='row',
                         figsize=fr.cm2inch(plot_width, plot_height))
# generate the axis
for i,v in enumerate(var):
    for j,z in enumerate(xD_value):
        axes[i,j].plot(data[z]['r'],data[z][v+'rms'],'-b',
                       label='Sim.',linewidth=1.5)
        axes[i,j].plot(expr[z]['r'],expr[z][v+'rms'],'ok',
                       label='Exp.',linewidth=1.5)
    # ylabel, temperature has a unit
    if v == 'T':
        str_label=(r'$\langle\tilde{T}^{\prime\prime 2}'
                   r'\rangle^{1/2}\;(\mathrm{{K}})$')
    elif v == 'Z':
        str_label=r'$\langle\tilde{Z}^{\prime\prime 2}\rangle^{1/2}$'
    else:
        str_label=(r'$\langle\tilde Y^{\prime\prime 2}_{\mathrm{'
                   +v+r'}}\rangle^{1/2}$')
    axes[i,0].set_ylabel(str_label,
                         fontsize=ftsize)
# title and xlabel
for j,z in enumerate(xD_value):
    #title
    axes[0,j].set_title('$x/D={0:.2g}$'.format(z),
                        fontsize=ftsize)
    #r/x
    axes[-1,j].set_xlabel('$r/x$',
                          fontsize=ftsize)
    axes[-1,j].set_xlim(xmin,xmax)
    axes[-1,j].set_xticks(xtick)

axes[-1,-1].set_xticks(xtick+(xmax,))

# legend
axes[0,-1].legend(fontsize=ftsize,
                  numpoints=1,
                  frameon=False)

# set margins
plt.subplots_adjust(left    =margin_left/plot_width,
                    bottom  =margin_bottom/plot_height,
                    right   =1.0-margin_right/plot_width,
                    top     =1.0-margin_top/plot_height,
                    wspace  =space_width/plot_width,
                    hspace  =space_height/plot_height)

plt.savefig('radial_rms.png',dpi=400)
plt.savefig('radial_rms.pdf')
plt.savefig('radial_rms.eps')


import cv2
import pytesseract

from image_gw_api.cascade_classifiers import DEFAULT_CLASSIFIER
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r"/usr/local/Cellar/tesseract/4.1.1/bin/tesseract"

class ImageProcessor:

    def __init__(self, image):
        self.image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        self.car_plate_cascade_classifier = DEFAULT_CLASSIFIER
        self.car_plate = None

    def _extract_car_plate(self):
        car_plate_rects = self.car_plate_cascade_classifier.detectMultiScale(
            self.image, scaleFactor=1.1, minNeighbors=5
        )
        car_plate_img = None

        for x, y, w, h in car_plate_rects:
            car_plate_img = self.image[y + 15:y + h - 10, x + 15:x + w - 20]

        return car_plate_img

    @staticmethod
    def _enlarge_img(image, scale_percent):
        width = int(image.shape[1] * scale_percent / 100)
        height = int(image.shape[0] * scale_percent / 100)
        dim = (width, height)
        resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
        return resized_image

    def process_image(self):
        car_plate_img = self._enlarge_img(self._extract_car_plate(), 150)
        car_plate_gray = cv2.cvtColor(car_plate_img, cv2.COLOR_RGB2GRAY)
        blurred_car_plate = cv2.medianBlur(car_plate_gray, 3)

        binary_image = cv2.threshold(blurred_car_plate, 130, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        inverted_bin = cv2.bitwise_not(binary_image)

        kernel = np.ones((2, 2), np.uint8)
        processed_img = cv2.erode(inverted_bin, kernel, iterations=1)
        processed_img = cv2.dilate(processed_img, kernel, iterations=1)

        self.car_plate = pytesseract.image_to_string(
            processed_img, config=f'--psm 8 --oem 3 -c tessedit_char_whitelist=0123456789ЙЦУКЕНГШЩЗХЇФІВАПРОЛДЖЯЧСМИТЬ'
                                  f'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
        ).rstrip("\n\f")

        return self.car_plate


from linebot.models import (
    TemplateSendMessage, ConfirmTemplate, PostbackTemplateAction,
)

def confirm(cat, i, DB):
    questions = DB.get_category(cat)
    #这里i 不用 -= 1 是因为data[userid][cat]是从 0开始计算
    return   TemplateSendMessage(
                alt_text='Confirm template',
                template=ConfirmTemplate(
                    text = str(questions[i][2]) + ' Q' + str(questions[i][3]) + ' : ' + questions[i][1],
                    actions=[
                        PostbackTemplateAction(
                            label='没问题',
                            text=str(questions[i][2]) + ' 第' + str(questions[i][3]) + '题已回复没问题',  #给使用者看相对题号
                            data='cat=%s&no=%d&answer=OK' % (questions[i][2], questions[i][3]) #questions是整份问卷第几题 绝对题号
                        ),
                        PostbackTemplateAction(
                            label='待改进',
                            text=str(questions[i][2]) + ' 第' + str(questions[i][3]) + '题已回复待改进', #给使用者看相对题号
                            data='cat=%s&no=%d&answer=NO' % (questions[i][2], questions[i][3])
                        )
                    ]
                ))

    # 功能：从DB抓问题，编织成confirm tempate
    # 输入：1.cat ： 类别
    #      2. i  ： 相对题号
    # 输出：ConfirmTemplate with question


from Final import climate
from Final import precip_amount
from Final import train_data, train_labels, test_data, test_labels
import pandas as pd
import numpy as np

import time
import datetime as dt

import sklearn as sk
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.experimental import enable_halving_search_cv
from sklearn.model_selection import HalvingGridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

'''
#We need to convert our labels to someting RandomForestClassifier can understand.
# It does not understand continuious, so while I could convert it to an int, I dont
# think that is statistically the best way to do that. Sklearn as a preprocessing
# model for label encoding, which I used below to relabel the labels into something
# it understands and can read in on.
'''

# rf_labels = le.fit_transform(rf_labels)

RFC = RandomForestClassifier(verbose = 0, warm_start = False, max_features = None,
                             max_depth = None, criterion = 'gini',
                             min_samples_split = 35, n_estimators = 80)

# Beautiful, so I got my best parameters from the grid search, you can see the
# best parameters in the RFC call. Lets fit it now.

# parameters = {'n_estimators': [50, 100, 150, 200],
#               'max_depth': [None, 25, 50, 100],
#               'max_features': [None, 4, 6, 8, 10],
#               'min_samples_split': [2, 4, 10, 25, 75, 125, 150, 200],
#               'criterion': ['gini']
#               }

# I JUST found out about halving grid search which gives as good or  better
# results quicker, so we are DEFINITELY going to use this from now on.

# grid = HalvingGridSearchCV(RFC, parameters)
# results = grid.fit(train_data, train_labels)
# print(results.best_params_)
# print(results.best_estimator_)
# print(results.best_score_)

# print(climate.head())
RFC.fit(train_data, train_labels)
score = RFC.score(test_data, test_labels)
# print(score)

# Lets give it a shot to see it it will predict a past date correctly.
avg_max_temp_july = climate.loc[climate.month == 7]\
    .loc[climate.year == 1996].max_temp.mean()
avg_min_temp_july = climate.loc[climate.month == 7]\
    .loc[climate.year == 1996].min_temp.mean()
pred_x = np.array([1996, 7, 29, avg_max_temp_july, avg_min_temp_july]).reshape(1, -1)

# print(climate.loc[climate.month == 7].loc[climate.year == 1996]\
    # .loc[climate.day == 29])
# print(RFC.predict(pred_x))
# print(RFC.predict_proba(pred_x))

# Now lets try a future date, but lets build a function for it
# (and also a fun printout).
def rf_predict(df, year, month, day):
    avg_max_temp = df.loc[df.month == month].loc[df.day == day].max_temp.mean()
    avg_min_temp = df.loc[df.month == month].loc[df.day == day].min_temp.mean()
    pred_x = np.array([year, month, day, avg_max_temp, avg_min_temp]).reshape(1, -1)
    return pred_x
pred_x = rf_predict(climate, 2022, 5, 28)

date = [int(pred_x[0][1]), int(pred_x[0][2]), int(pred_x[0][0])]

prediction = RFC.predict(pred_x)
probability = RFC.predict_proba(pred_x)

# print(prediction)
# print(probability)
proba_of_rain = round(probability[0][1], 2) * 100
if prediction == 1:
    print(f'On {date[0]}-{date[1]}-{date[2]}:\nI predict it will rain!')
    print(f'There is a {proba_of_rain} percent chance of rain!')
else:
    print(f'On {date[0]}-{date[1]}-{date[2]}:\nI do not think it will rain!')
    print(f'There is a {proba_of_rain} percent chance of rain!')

'''
This model predict there was a 58 %\ chance it did rain and a 41%\ chance it did not
rain on July 29th, 1996 (as requested by my friend for the day he was born).
Historical records show that it DID in fact rain on that day, therefore we can
say the the model predicted correctly that it rained.

For the future prediction, the model is predicting that it has a 76 %\ chance
of not raining and a 24 %\ chance of raining tomorrow (at the time of writing),
and weather data for tomorrow shows that there is a 24%\ chance of rain. For the
day after tomorrow, it is showing there is a 80.2%\ chance of not raining and a
19.8%\ chance of raining. The Weather Channel shows there is an 18%\ chance of
raining. That is about 2%\ off of what the Weather Channel states it will be.

So it looks like the RFC has a 75%\ likelihood that it is correct, based on the raw
score of the model. I want to try and also fit a Random Forest Regressor,
so I am going to do that below with the
same steps as above. I will have to change the labels because the regressor
runs on a quantative basis, so it needs all numbers instead of a bool.
'''

climate['raw_precip'] = precip_amount
# print(climate.head())
# print(climate.raw_precip.unique())
climate.raw_precip.fillna(climate.raw_precip.mean())

features = climate[['year', 'month', 'day', 'max_temp', 'min_temp']].to_numpy()
labels = climate['raw_precip'].to_numpy().reshape(-1, 1)

minmax = MinMaxScaler()
labels = minmax.fit_transform(labels)

train_data, test_data,\
train_labels, test_labels = train_test_split(features, labels, test_size = 0.2)

RFR = RandomForestRegressor(verbose = 1, warm_start = True,
                            criterion = 'absolute_error', random_state = 11,
                            bootstrap = True)

# RFR.fit(train_data, train_labels)
# score = RFR.score(test_data, test_labels)
# print(score)

# parameters = {'max_depth': [None],
#               'min_samples_split': [None],
#               'max_features': [None, 1],
#               'n_estimators': [100]}

# grid = HalvingGridSearchCV(RFR, parameters)
# results = grid.fit(train_data, train_labels)
# score = grid.score(test_data, test_labels)
# print(score)
# print(results.best_params_)
# print(results.best_score_)


import speech_recognition as sr
import pyaudio
p = pyaudio.PyAudio()

# Initialize the recognizer
r = sr.Recognizer()

for i in range(p.get_device_count()):
    dev = p.get_device_info_by_index(i)
    if dev['maxInputChannels'] > 0:
        print('Found microphone: {} {}'.format(i, dev['name']))

# Function to convert text to
# speech
def SpeakText(command):
    pass
    # Initialize the engine
    #import pyttsx3
    #engine = pyttsx3.init()
    #engine.say(command)
    #engine.runAndWait()

while True:
    try:

        # use the microphone as source for input.
        with sr.Microphone(device_index=1) as source2:

            # wait for a second to let the recognizer
            # adjust the energy threshold based on
            # the surrounding noise level
            r.adjust_for_ambient_noise(source2, duration=0.2)

            #listens for the user's input
            audio2 = r.listen(source2)

            # Using google to recognize audio
            MyText = r.recognize_google(audio2, language="ZH")
            MyText = MyText.lower()

            print("Did you say ",MyText)
            #SpeakText(MyText)

    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))

    except sr.UnknownValueError:
        print("unknown error occurred")


from tkinter import Tk, filedialog
from typing import List, Union, Dict
# from utils.loader import load_json
# import pycountry
import re
import docx

class ValueExtractor:
    def __init__(self):
        self.week_patterns = [
            # Week of the year, e.g., Week 35
            r'Week\s*\d{1,2}',
            # Weekday, e.g., Monday, Tuesday, etc.
            r'(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)',

            r'第\d{1,2}\s*?周',  # matches 第1 周, 第12周, etc.
            r'(?:星期一|星期二|星期三|星期四|星期五|星期六|星期日)',  # matches 星期一, 星期二, etc.
        ]

        self.date_patterns = [
            # English dates
            r'\d{1,2}\s*?(?:January|February|March|April|May|June|July|August|September|October|November|December)\s*?\d{4}',
            r'\d{1,2}\s*?(?:January|February|March|April|May|June|July|August|September|October|November|December)',
            r'(?:January|February|March|April|May|June|July|August|September|October|November|December)\s*?\d{4}',
            # Chinese dates
            # 2022年7月22日 or 2022 年 7 月 22 日
            r'\d{4}\s*?年\s*?\d{1,2}\s*?月\s*?\d{1,2}\s*?日',
            # 2022年7月 or 2022 年 7 月
            r'\d{4}\s*?年\s*?\d{1,2}\s*?月',
            r'\d{1,2}\s*?月\s*?\d{1,2}\s*?日',                  # 3月7日 or 3 月 7 日
            r'\d{1,2}\s*?月'                                  # 3月 or 3 月
        ]

        self.percentage_patterns = [
            # Match number followed by % (e.g., 50%, 5.5%, 0.123%)
            r'\d+(?:\.\d+)?\s*%',
            # Chinese percentage pattern
            r'\d+(?:\.\d+)?\s*百分之'
        ]

        self.numerical_patterns = [
            # English Numerical Units Pattern
            r'\d+\s*(?:hundred|thousand|million|billion)s?',
            # Chinese Numerical Units Pattern
            # r'(?:一|十|百|千)?\d+\s*(?:億|萬|百萬|千萬)?',
            # r'\b(?:一|十|百|千)?\d+\s*(?:億|萬|百萬|千萬)?\b'

        ]
        self.time_patterns = [
            # 24-hour time format (e.g., 9:30, 15:45, 00:00)
            r'\d{1,2}(?::\d{2})',

            # matches 9.00 am, 9.00am, 9:00PM, 6p.m., 6:00p.m., etc.
            r'\d{1,2}(?:[.:]\d{2})?\s?[apAP]\.?m\.?',

            # Noon and midnight (e.g., 12 noon, 12 midnight)
            r'12\s?(?:noon|midnight)',

            # 24-hour time format in Chinese (e.g., 9 時, 15 時 30 分, 00 時)
            r'\d{1,2}\s?時(?:\s?\d{1,2}\s?分)?',

            # 12-hour time format in Chinese (e.g., 9 時 30 分 AM, 3 時 15 分 PM)
            r'\d{1,2}\s?時\s?\d{1,2}\s?分\s?[APap]\.?m\.?',

        ]

        self.quantity_patterns = [
            # Percentage
            r'\d+(?:\.\d{1,2})?\s*%',               # matches 1 %, 1%, etc.

            # US Dollar
            r'US\$\d+(?:\.\d{1,2})?',   # matches US$1.00, US$1, US$0.50, etc.
            r'\$\d+(?:\.\d{1,2})?',    # matches $1.00, $1, etc.
            r'\s+\d+\s?USD',               # matches 1 USD, 1USD, etc.

            # Euro
            r'€\d+(?:\.\d{1,2})?',     # matches €1.00, €1, etc.
            r'\s+\d+\s?EUR',               # matches 1 EUR, 1EUR, etc.

            # Chinese Yuan/Renminbi
            r'¥\d+(?:\.\d{1,2})?',     # matches ¥1.00, ¥1, etc.
            r'\s+\d+\s?CNY',               # matches 1 CNY, 1CNY, etc.
            r'\s+\d+\s?RMB',               # matches 1 RMB, 1RMB, etc.

            # Singapore Dollar
            r'S\$\d+(?:\.\d{1,2})?',   # matches S$1.00, S$1, etc.
            r'\s+\d+\s?SGD',               # matches 1 SGD, 1SGD, etc.

            # Chinese expressions
            r'\d+\s?美元',              # matches 1 美元 for US dollar
            r'\d+\s?元',                # matches 1 元 for RMB
            r'\d+\s?新加坡元',          # matches 1 新加坡元 for Singapore dollar
            r'\d+\s?欧元',              # matches 1 欧元 for Euro
        ]

        self.word_to_number = {
            'one': '1',
            'two': '2',
            'three': '3',
            'four': '4',
            'five': '5',
            'six': '6',
            'seven': '7',
            'eight': '8',
            'nine': '9',
            'ten': '10',
            'eleven': '11',
            'twelve': '12',
            'thirteen': '13',
            'fourteen': '14',
            'fifteen': '15',
            'sixteen': '16',
            'seventeen': '17',
            'eighteen': '18',
            'nineteen': '19',
            'twenty': '20',
            'third': '3',
            'thirds': '3',
            'quarter': '4',
            'quarters': '4',
            'half': '2',
        }

        def get_country_list():
            # Get a list of all countries in English, their corresponding names in Chinese (Simplified), and their short forms.
            countries = {}
            chinese_country_map = load_json("resources/country_map.json")
            countries.update(chinese_country_map)
            for country in pycountry.countries:
                countries[country.name.lower()] = country.name
                countries[country.alpha_2] = country.name

                try:
                    cn_name = country.name_translations['zh-Hans']
                    countries[cn_name] = country.name
                except (KeyError, AttributeError):
                    pass
            return countries

        # self.countries = get_country_list()

    def _chinese_to_number(self, chinese_num: str) -> int:
        chinese_numerals = {
            '零': 0, '一': 1, '二': 2, '三': 3, '四': 4,
            '五': 5, '六': 6, '七': 7, '八': 8, '九': 9
        }
        unit_positions = {
            '十': 10, '百': 100, '千': 1000, '万': 10000
        }
        val = 0
        unit_val = 1
        for char in reversed(chinese_num):
            if char in unit_positions:
                unit_val = unit_positions[char]
            else:
                val += chinese_numerals[char] * unit_val
                if unit_val > 1:
                    unit_val = 1
        return val

    def extract_date_values_from_text(self, s: str) -> List[str]:
        """
        Extracts date values from the given string.

        Args:
        - s (str): The input string.

        Returns:
        - List[str]: List of extracted date values.
        """

        date_values = []

        # Iterating in the order of the patterns
        for pattern in self.date_patterns:
            matches = re.findall(pattern, s)
            if matches:
                # Extend date_values with the new matches
                date_values.extend(matches)

                # Remove these matches from the string to avoid extracting sub-patterns again
                for match in matches:
                    # replace only the first occurrence
                    s = s.replace(match, '', 1)

        return date_values

    def extract_numerical_values_from_text(self, s: str) -> List[str]:
        """
        Extracts numerical values from the given string, excluding those that are part of a date.

        Args:
        - s (str): The input string.

        Returns:
        - List[str]: List of extracted numerical values.
        """

        # Extract date values
        date_values = self.extract_date_values_from_text(s)

        # Remove extracted date values from the string
        for date in date_values:
            s = s.replace(date, '')

        # Extract fraction values
        fraction_values = self.extract_fractions_from_text(s)

        # Remove extracted fraction values from the string
        for fraction in fraction_values:
            s = s.replace(fraction, '')

        # Extract time values
        time_values = self.extract_time_values_from_text(s)

        # Remove extracted time values from the string
        for time in time_values:
            s = s.replace(time, '')

        # Extract quantity values
        quantity_values = self.extract_quantity_values_from_text(s)

        # Remove extracted quantity values from the string
        for quantity in quantity_values:
            s = s.replace(quantity, '')

        # Find chinese order words and replace into Arabic number
        replace_keys_ch = {
            '零級': '0',
            '一級': '1',
            '二級': '2',
            '三級': '3',
            '四級': '4',
            '五級': '5',
            '六級': '6',
            '七級': '7',
            '八級': '8',
            '九級': '9',
            '第零': '0',
            '第一': '1',
            '第二': '2',
            '第三': '3',
            '第四': '4',
            '第五': '5',
            '第六': '6',
            '第七': '7',
            '第八': '8',
            '第九': '9',
        }
        # Remove extracted order values from the string
        for key in replace_keys_ch:
            s = s.replace(key, replace_keys_ch[key])

        # Extract numbers, excluding those within date patterns
        matches = re.findall(r'(\d+(?:,\d{3})*(?:\.\d+)?)', s)
        result = [match.replace(",", "") for match in matches]
        return result

    def extract_time_values_from_text(self, s: str) -> List[str]:
        """
        Extracts time values from the given string, excluding those that are part of a date.

        Args:
        - s (str): The input string.

        Returns:
        - List[str]: List of extracted times.
        """

        time_values = []

        # Iterating in the order of the patterns
        for pattern in self.time_patterns:
            matches = re.findall(pattern, s)
            if matches:
                # Extend time_values with the new matches
                time_values.extend(matches)

                # Remove these matches from the string to avoid extracting sub-patterns again
                for match in matches:
                    # replace only the first occurrence
                    s = s.replace(match, '', 1)

        return time_values

    def extract_quantity_values_from_text(self, s: str) -> List[str]:
        """
        Extracts quantity values from the given string, excluding those that are part of a date.

        Args:
        - s (str): The input string.

        Returns:
        - List[str]: List of extracted quantitys.
        """

        quantity_values = []

        # Iterating in the order of the patterns
        for pattern in self.quantity_patterns:
            matches = re.findall(pattern, s)
            if matches:
                # Extend quantity_values with the new matches
                quantity_values.extend(matches)

                # Remove these matches from the string to avoid extracting sub-patterns again
                for match in matches:
                    # replace only the first occurrence
                    s = s.replace(match, '', 1)

        return quantity_values

    def extract_fractions_from_text(self, text: str) -> List[str]:
        """
        Extracts fractions from both English and Chinese text.

        Args:
        - text (str): The input string.

        Returns:
        - List[str]: List of extracted fractions.
        """
        extracted_fractions = []

        try:
            # For English fractions
            extracted_fractions.extend(re.findall(r'\b\d+/\d+\b', text))

            # For worded English fractions
            worded_english_fractions_raw = re.findall(
                r'\b(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)-(\w+)\b', text)
            extracted_fractions.extend(
                f"{self.word_to_number.get(numerator, 'unknown')}/{self.word_to_number.get(denominator, 'unknown')}"
                for numerator, denominator in worded_english_fractions_raw if numerator in self.word_to_number and denominator in self.word_to_number
            )

            # For Chinese fractions
            chinese_fractions_raw = re.findall(
                r'([零一二三四五六七八九十百千万]+)分之([零一二三四五六七八九十百千万]+)', text)
            extracted_fractions.extend(
                f"{self._chinese_to_number(numerator)}/{self._chinese_to_number(denominator)}"
                for denominator, numerator in chinese_fractions_raw
            )
        except Exception as e:
            # You can log the error if needed or take any other corrective actions.
            print(f"Error encountered: {e}")

        return extracted_fractions

    def extract_country_names_from_text(self, text: str, lang: str = "en") -> List[str]:
        found_countries = set()

        # Define the patterns to remove as a regular expression
        patterns_to_remove = [r"\.", r"LAP", r"ESG"]
        combined_pattern = "|".join(patterns_to_remove)

        cleaned_text = re.sub(combined_pattern, "", text)

        for country in self.countries:
            if len(country) == 2:  # If it's an alpha-2 code, it should be uppercase
                if lang == "en":
                    pattern = rf"\b{re.escape(country)}\b"
                else:
                    pattern = rf"{re.escape(country)}"
                if re.search(pattern, cleaned_text):
                    found_countries.add(self.countries[country])
            else:
                if lang == "en":
                    pattern = rf"\b{re.escape(country)}\b"
                else:
                    pattern = rf"{re.escape(country)}"
                if re.search(pattern, cleaned_text, re.IGNORECASE):
                    found_countries.add(self.countries[country])

        return list(found_countries)

# Create an instance of the ValueExtractor class
extractor = ValueExtractor()
# Function to extract and print values from text
def extract_and_print_values(text):
    # Extract date patterns
    date_matches = []
    for pattern in extractor.date_patterns:
        date_matches.extend(re.findall(pattern, text))

    # Extract time patterns
    time_matches = []
    for pattern in extractor.time_patterns:
        time_matches.extend(re.findall(pattern, text))

    # Extract quantity patterns
    quantity_matches = []
    for pattern in extractor.quantity_patterns:
        quantity_matches.extend(re.findall(pattern, text))

    week_matches = []
    for pattern in extractor.week_patterns:
        week_matches.extend(re.findall(pattern, text))

    percentage_matches = []
    for pattern in extractor.percentage_patterns:
        percentage_matches.extend(re.findall(pattern, text))

    numerical_matches = []
    for pattern in extractor.numerical_patterns:
        numerical_matches.extend(re.findall(pattern, text))

    # Extracted values
    extracted_values = {
        # "Dates": date_matches,
        # "Times": time_matches,
        # "Quantities": quantity_matches,
        # "Weeks": week_matches,
        # "Percentages": percentage_matches,
        "Numericals": numerical_matches,
    }

    # Print the extracted values
    for category, values in extracted_values.items():
        if values:
            print(f"{category}:")
            for value in values:
                print(f"- {value}")

# Function to extract and process text from paragraphs
def extract_text_from_paragraphs(doc):
    doc_text = ""
    for paragraph in doc.paragraphs:
        doc_text += paragraph.text + "\n"
    extract_and_print_values(doc_text)

# Function to extract and process text from tables
def extract_text_from_tables(doc):
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                cell_text = cell.text
                extract_and_print_values(cell_text)

# Function to extract and process hyperlinks
def extract_hyperlinks(doc):
    for hyperlink in doc.element.xpath('//w:hyperlink'):
        link_text = hyperlink.xpath('./w:r/w:t')
        if link_text:
            link_text = link_text[0].text
            print(f"Hyperlink: {link_text}")

# Open a file dialog to select a docx file
root = Tk()
root.withdraw()  # Hide the main window
file_path = filedialog.askopenfilename(filetypes=[("Word Files", "*.docx")])

if file_path:
    # Load the docx file
    doc = docx.Document(file_path)

    # Extract and process text from paragraphs, tables, and hyperlinks
    extract_text_from_paragraphs(doc)
    extract_text_from_tables(doc)
    # extract_hyperlinks(doc)
else:
    print("No file selected.")


from flask import Flask
from flask_restful import Api
from resources.tracker import Tracker, TrackerCount, TrackerInsert

app = Flask(__name__)
api = Api(app)

api.add_resource(Tracker, '/tracker')
api.add_resource(TrackerCount, '/tracker/count')
api.add_resource(TrackerInsert, '/tracker/insert')

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


# -*- coding: utf-8 -*-
"""
Created on Thu May 14 18:55:59 2020

@author: 许继元
"""

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget

class UserCenter(QWidget):
    def __init__(self, user, num, label):
        super(UserCenter, self).__init__()  # 使用super函数可以实现子类使用父类的方法
        self.setWindowTitle("个人主页")
        self.setWindowIcon(QIcon('../Data/douban.jpg'))  # 设置窗口图标
        self.resize(500, 150)

        self.user_label = QLabel(self)
        self.num_label = QLabel(self)
        self.label_label = QLabel(self)

        self.user_label.setText("
您好!" + user + "
")
        self.num_label.setText("
您在豆瓣评论过" + num + "部电影
")
        try:
            self.label_label.setText("
您喜欢的电影类型是:" + label + "
")
        except:
            self.label_label.setText("")

        self.v_layout = QVBoxLayout()

        self.v_layout.addWidget(self.user_label)
        self.v_layout.addWidget(self.num_label)
        self.v_layout.addWidget(self.label_label)

        self.setLayout(self.v_layout)


#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import datetime
from collections import Counter

def arguments():
    # Handle command line arguments
    parser = argparse.ArgumentParser(description='Adventofcode.')
    parser.add_argument('-f', '--file', required=True)

    args = parser.parse_args()

    return args

class Guard: # pylint: disable=too-few-public-methods, too-many-instance-attributes
    guards_dict = {}

    def __init__(self, name):
        self.name = name
        self.sleep_schedule = []
        self.time_asleep = 0
        Guard.guards_dict[name] = self
        self.minutes_asleep = []
        self.most_minutes_asleep = None
        self.most_common_minute_sleeping = None
        self.part1_result = None
        self.part2_result = None

    def log_sleep_schedule(self, schedule):
        self.sleep_schedule.append(schedule)

    def calculate_sleep(self):
        datetime_format = '%H:%M'
        last_falling_asleep = None
        for minutes in self.sleep_schedule:
            if "falls asleep" in minutes:
                last_falling_asleep = minutes[12:17]
            elif "wakes up" in minutes:
                time_sleeping = datetime.datetime.strptime(minutes[12:17], datetime_format) - datetime.datetime.strptime(last_falling_asleep, datetime_format)
                self.time_asleep = self.time_asleep + time_sleeping.seconds

                sleep_dt = datetime.datetime.strptime(last_falling_asleep, datetime_format)
                wake_dt = datetime.datetime.strptime(minutes[12:17], datetime_format)
                for specific_minute in range(sleep_dt.minute, wake_dt.minute):
                    self.minutes_asleep.append(specific_minute)
        if self.minutes_asleep == []:
            self.minutes_asleep = [0, 0]

        self.minutes_asleep = Counter(self.minutes_asleep)
        self.most_minutes_asleep = self.minutes_asleep.most_common()[0][0]
        self.most_common_minute_sleeping = self.minutes_asleep.most_common()[0]

    def calculate_part1_result(self):
        self.part1_result = int(self.name[1:]) * self.most_minutes_asleep

    def calculate_part2_result(self):
        self.part2_result = int(self.name[1:]) * self.most_common_minute_sleeping[0]

def main():
    args = arguments()

    with open(args.file) as file:
        input_file = file.read().strip().splitlines()
    sorted_schedule = (sorted(input_file))

    #part1
    guards = []
    guard_id = -1
    for row in sorted_schedule:
        if row.split()[3] in Guard.guards_dict:
            guard = Guard.guards_dict[row.split()[3]]
            guard.log_sleep_schedule(row)
        else:
            if guard_id != row.split()[3]:
                if "Guard" in row:
                    guard_id = row.split()[3]
                    guard = Guard(guard_id)
                    guards.append(guard)
                else:
                    guard.log_sleep_schedule(row)

    time_asleep = -1
    times_asleep_per_minute = -1
    for row in guards:
        row.calculate_sleep()
        #part1
        if row.time_asleep > time_asleep:
            time_asleep = row.time_asleep
            most_sleepy_guard = row
        #part2
        if row.most_common_minute_sleeping[1] > times_asleep_per_minute:
            times_asleep_per_minute = row.most_common_minute_sleeping[1]
            most_sleepy_guard_part2 = row

    most_sleepy_guard.calculate_part1_result()
    most_sleepy_guard_part2.calculate_part2_result()

    print("part1 result:", most_sleepy_guard.part1_result)
    print("part2 result:", most_sleepy_guard_part2.part2_result)

if __name__ == '__main__':
    main()


from flask import Flask, render_template
from data import title, departures, tours, subtitle, description

app = Flask(__name__)

@app.route('/')
def render_index_html():
    tour = []
    for i in range(1, 7):
        tour.append(tours[i])
    return render_template('index.html', title_page=title, nav=departures, sub_title=subtitle, des=description, tour=tour)

@app.route('/departure/<departure>')
def render_departure(departure):
    list_departure = []
    list_tour = []
    for i in tours:
        if tours[i]['departure'] == departure:
            list_departure.append(i)
    for j in list_departure:
        list_tour.append(tours[j])
    temp_price = []
    temp_night = []
    for pn in list_tour:
        temp_price.append(pn['price'])
        temp_night.append(pn['nights'])
    description_content = []
    description_content.append(min(temp_price))
    description_content.append(max(temp_price))
    description_content.append((min(temp_night)))
    description_content.append((max(temp_night)))

    return render_template('departure.html', title_page=title, nav=departures, tour=list_tour, tour_id=list_departure, departure_id=departure, content=description_content)

@app.route('/tour/<id>/')
def render_tour(id):
    return render_template('tour.html', title_page=title, nav=departures, tour=tours[int(id)])

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