from django.db import connection
from django.test import Client
from myapp.models import PlantillaEquipo
from unittest import TestCase
from urllib.parse import urlencode
from .test_login import LoginTest
import json

class EquipoTest(TestCase):

    # Autenticación
    loginObj = LoginTest()
    token = loginObj.test()

    # Cabeceras generales
    headers = {
        'HTTP_AUTHORIZATION': 'Bearer ' + token
    }

    client = Client(**headers)

    # Listado
    def list(self, cantidad):

        response = self.client.get('/plantillas-equipo/list/')

        cantidadPlantillasEquipo = len(json.loads(response.content)['data'])

        self.assertEqual(cantidadPlantillasEquipo, cantidad)

        if cantidad > 0:
            self.update()

    # Eliminación de los proyectos en la base de datos
    def clean(self):

        with connection.cursor() as cursor:
            cursor.execute('DELETE FROM v1.plantillas_equipo;')

            self.store()

    # Almacenamiento
    def store(self):

            data = {
                'descripcion': 'Test',
            }

            response = self.client.post('/plantillas-equipo/store/',
                                        urlencode(data),
                                        content_type='application/x-www-form-urlencoded')

            self.assertEqual(response.status_code, 201)

            self.list(1)

    # Actualización
    def update(self):

        # Obtener la decisión almacenada anteriormente
        equipo = PlantillaEquipo.objects.get(descripcion='Test')

        data = {
            'descripcion': 'Test a',
        }

        response = self.client.put('/plantillas-equipo/' + str(equipo.planid),
                                    urlencode(data),
                                    content_type='application/x-www-form-urlencoded')

        self.assertEqual(response.status_code, 200)

        self.delete()

    # Eliminación
    def delete(self):
        # Obtener el proyecto modificado anteriormente
        equipo = PlantillaEquipo.objects.get(descripcion='Test a')

        response = self.client.delete('/plantillas-equipo/' + str(equipo.planid) + '/delete/')

        self.assertEqual(response.status_code, 200)

        self.list(0)

    # Prueba
    def test(self):

        self.clean()


#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import time

PI = 2 * np.arcsin(1)
N = 64
L = 2 * PI
dx = L / (N-1)
x = np.zeros(N)
f = np.zeros(N)
y = np.zeros(2*N)

def plot_c(x,y):
  N = len(x)
  yr = np.zeros(N)
  yi = np.zeros(N)
  for i in range(N):
    yr[i] = y[2*i]
    yi[i] = y[2*i+1]
  plt.figure()
  plt.plot(x,yr,x,yi)
  plt.legend(["real","imaginary"])
  plt.show(block=false)

def fft_slow(data, isign):
  N = len(data) * 0.5 >> 1
  tmp = np.zeros(2*N)
  if N < 2 or N & (N-1):
    print("data must be length of power of 2")
    return data
  else:
    for n in range(N):
      for k in range(N):
        theta = isign*PI*k*n*2./N
        ct = np.cos(theta)
        st = np.sin(theta)
        tmp[2*n] = tmp[2*n] + ct*data[2*k] - st*data[2*k+1]
        tmp[2*n+1] = tmp[2*n+1] + ct*data[2*k+1] - st*data[2*k]
    if isign < 0:
      for i in range(2*N):
        tmp[i] = tmp[i] / N
    return tmp

def fourier(N,x,y,f,b):
  for i in range(N):
    x[i] = i * dx
    f[i] = i
    y[2*i] = np.sin(x[i]) + np.sin(b*x[i])
    y[2*i+1] = 0
  plot_c(x,y)
  y = fft_slow(y,1.)
  plot_c(f[:0.5*N],y[:N])
  y = fft_slow(y,-1.)
  plt.show()

fourier(N,x,y,f,4)
fourier(N,x,y,f,13)
fourier(N,x,y,f,28)
fourier(N,x,y,f,39)
fourier(N,x,y,f,50)

# Despite having X11 forwarding enabled, got the error:
# "Network error: Connection refused: cannot connect to X server localhost:10.0"
# Because of this, I could not compare the different plots, but the above
# code should work.


#Python program to scrape website
#and save quotes from website
import requests
from bs4 import BeautifulSoup
import csv
import json

URL = "http://catalog.illinois.edu/courses-of-instruction/math/"
r = requests.get(URL)

soup = BeautifulSoup(r.content, 'html5lib')

courses=[] # a list to store quotes

table = soup.find('div', attrs = {'id':'courseinventorycontainer'})

for row in table.findAll('div', attrs = {'class':'courseblock'}):
    course = {}
	#quote['theme'] = row.h5.text
    course['tag'] = row.a.getText().strip()
    temp = row.find('p', attrs = {'class':'courseblockdesc'})
    course['patterns'] = temp.getText().strip().split('.')
    course['responses'] = [row.a['href']]
    course['context'] = [""]
    courses.append(course)
    #print(row)

intent = {}
intent['intents'] = courses
#'theme','url','img','lines','author'

filename = 'test_math.json'
json_obj = json.dumps(intent, indent = 4)

with open(filename, 'w',) as outfile:
	outfile.write(json_obj)


from .general_part import GeneralPart
import random as rand
import config
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import WebDriverException

class Create_edit_new_company(GeneralPart):

    def __init__(self, driver=None, testing_page=config.PROFIREADER_URL, company_list=config.COMPANY_LIST,
                 company_name='Fest', e_mail='we@1.com', re_mail='qweqw@qe.com'):
        super().__init__(driver=driver)
        self.driver = driver
        self.testing_page = testing_page
        self.company_list = company_list
        self.company_name = company_name
        self.e_mail = e_mail
        self.re_mail = re_mail

    def __call__(self, *args, **kwargs):
        self.test_create_new_company()
        self.scroll_all()
        self.get_own_company()

    @classmethod
    def __repr__(cls):
        return 'create_edit_new_company'

    def test_create_new_company(self):

        self.driver.get(self.company_list)
        self.driver.find_elements_by_css_selector("*[pr-test='CreateNewCompany']")[0].click()

        # name = self.driver.find_elements_by_css_selector("*[pr-test='InputCompanyName']")
        name = self.driver.find_element_by_name('company_name')
        name.send_keys(self.company_name)
        # country = self.driver.find_elements_by_css_selector("*[pr-test='InputCompanyCountry']")
        country = self.driver.find_element_by_name('company_country')
        country.send_keys(self.text)
        region = self.driver.find_element_by_name("company_region")
        region.send_keys(self.text)
        city = self.driver.find_element_by_name("company_city")
        city.send_keys(self.text)
        email = self.driver.find_element_by_name("company_email")
        email.send_keys(self.e_mail)
        time.sleep(5)

        # upload_image = self.driver.find_elements_by_css_selector("*[pr-test='UploadImageBtn']")
        upload_image = self.driver.find_element_by_name('file')
        print(upload_image)
        upload_image.send_keys("/Users/apple/Desktop/test.jpg")
        time.sleep(5)

        create_company = self.driver.find_elements_by_css_selector("*[pr-test='CreateCompanyBtn']")
        create_company[0].click()
        time.sleep(10)

    def scroll_all(self, old_pos=0):
        self.driver.get(self.company_list)

        while True:
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(10)
            new_Height = self.driver.execute_script("return document.body.scrollHeight")
            if old_pos != new_Height:
                old_pos = new_Height
            else:
                return False
            print(new_Height)

    def get_own_company(self):

        a_length = 1
        count = 0
        while count < a_length:
            own_company = self.driver.find_elements_by_css_selector("*[pr-test='CompanyTitle']")[count:]
            company = own_company[0].text

            a_length = len(own_company) if count == 0 else a_length
            count += 1
            print(company)

        self.click_own()

    def click_own(self):
        time.sleep(3)
        element_by = "//*[contains(text(),"+"'"+self.company_name+"'"+")]"
        print(element_by)
        own = self.driver.find_elements_by_xpath(element_by)
        print(own[0].text)

        time.sleep(3)
        own[0].click()

        self.edit_own()

    def edit_own(self):

        self.driver.find_elements_by_css_selector("*[pr-test='EditCompanyProfile']")[0].click()

        email = self.driver.find_element_by_name("company_email")
        email.clear()
        time.sleep(3)
        email.send_keys(self.re_mail)
        time.sleep(3)
        self.driver.find_elements_by_css_selector("*[pr-test='SaveCompanyProfile']")[0].click()

        company_email = self.driver.find_elements_by_css_selector("*[pr-test='CompanyEmail']")[0].text
        print(company_email)
        assert self.re_mail in company_email, "Can't edit company {comp_name} profile"\
            .format(comp_name=self.company_name)

        # self.driver.get(self.company_list)

        # company = self.driver.find_elements_by_css_selector("*[pr-test='CompanyThumbnail']")
        # print(company[1].text)
        # company[0].click()

        # company = self.driver.find_elements_by_css_selector("*[pr-test='JoinedCompany']")
        # own_or_joined = company[0].text
        # print(own_or_joined)

        # for i in company:
        #     # print(i)
        #     own_company = i.text
        #     print(own_company)
        #     own = 'Type: OWN COMPANY'
            # if own_company == own:
            #     return company[own].click()
        # company[0].click()

        # company = self.driver.find_elements_by_css_selector("*[pr-test='JoinedCompany']")
        # print(company)
        # company[0].click()

        # nxt = self.driver.find_elements_by_xpath("//p[text()='Type: OWN COMPANY']")
        # nxt = self.driver.find_elements_by_css_selector("*[text()='Type: OWN COMPANY']")
        #
        # print(nxt)
        # nxt[0].click()

        # list_company = []
        # for i in company:
        #     own = i.text
        #     list_company.append(own)
        #     print(list_company.append(own))
        #     # print(own)
        #     if own == 'Type: OWN COMPANY':
        #         list_company.append(own)[0].click()
        #         company(len(own)).click()

        # company[0].click()

        # re = set()
        # for item in company:
        #     item.get_attribute("text")
        # print(item.get_attribute("text"))


import threading
import pygame
from wypc import *
import queue
from multiprocessing  import Process

class playMusic():
    def __init__(self, instructionInput_conn,emotionOutput_conn):
        self.t = Process(target=self.run,args=[instructionInput_conn,emotionOutput_conn])
        self.musicRadio = music()
        self.currentEmotionIdx = 4
        self.currentInstruction = 0

    def run(self, instructionInput_conn,emotionOutput_conn):
        while True:
            try:
                newInstruction = instructionInput_conn.revc()
                self.currentEmotionIdx = newInstruction
                print("--newInstruction :{}".format(newInstruction))
            except queue.Empty:
                pass
            try:
                newEmotionIdx = emotionOutput_conn.revc()
                self.currentEmotionIdx = newEmotionIdx
            except queue.Empty:
                pass
            print("currentInstruction:   {},currentEmotionIdx:   {}".format(self.currentInstruction,self.currentEmotionIdx))
            # 修改音乐播放状态
            self.__radioEvent(self.currentInstruction, self.currentEmotionIdx)
            # 清空指令状态
            self.currentInstruction = 0

    def start(self):
        self.t.start()

    def __radioEvent(self, instruction, emotionIdx):
        # 开始播放音乐
        if instruction == 0:
            self.musicRadio.load(self.__chooseSong(emotionIdx))
            self.musicRadio.play()
        # 停止播放音乐
        elif instruction == 1:
            self.musicRadio.stop()
        # 播放下一首音乐
        elif instruction == 2:
            self.musicRadio.nextMusic(self.__chooseSong(emotionIdx))
        # 播放上一首音乐
        elif instruction == 3:
            self.musicRadio.preMusic()
        # 暂停播放音乐
        elif instruction == 4:
            self.musicRadio.pause()
        # 重新开始播放音乐
        elif instruction == 5:
            self.musicRadio.restart()

    def __chooseSong(self, emotionIdx):
        allEmotion = {0: ["安静", "放松"],
                      1: ["治愈", "孤独", "伤感"],
                      2: ["安静", "放松"],
                      3: ["快乐", "感动", "浪漫", "兴奋"],
                      4: ["清新", "放松", "安静"],
                      5: ["治愈", "孤独", "伤感"],
                      6: ["快乐", "感动", "浪漫", "兴奋"]}
        targetEmotion = allEmotion[emotionIdx]
        tar = random.randint(0, len(targetEmotion) - 1)
        targetType = targetEmotion[tar]
        # song是个列表，第一个参数是song[0]是name,song[1]是歌曲url
        song = loadMusic(targetType)
        print(song)  # ['大梦初醒 Awaken ', '[图片]http://music.163.com/song/media/outer/url?id=493283507.mp3']
        return song

class music:
    def __init__(self):
        self.pm = pygame.mixer
        self.pm.init()
        self.musicPos = 0.0
        self.stack1 = []  # 歌曲栈, 存当前和播放过的

    def load(self, music, musicPath=""):
        if len(music) != 0:
            self.download(music)
            print("song/" + music[0] + ".mp3")
            self.pm.music.load("song/" + music[0] + ".mp3")
            self.stack1.append("song/" + music[0] + ".mp3")
        elif musicPath != "":
            self.pm.music.load(musicPath)

    def play(self):
        self.pm.music.play(loops=1, start=0.0)
        self.pm.music.set_volume(0.5)  # ——  参数0-1设置音量
        # time.sleep(40)

    def stop(self):
        self.pm.music.stop()

    def pause(self):
        self.pm.music.get_pos()
        self.pm.music.pause()

    def restart(self):
        self.pm.music.play(start=self.musicPos)

    def preMusic(self):
        self.stop()
        # self.stack1.pop()
        musicPath = self.stack1[-1]
        self.load([], musicPath)
        self.play()

    def nextMusic(self, music):
        """
        :param music:
        :return:
        """
        self.stop()
        # self.stack1.append(musicPath)
        self.load(music)
        self.play()

    def download(self, music):
        import urllib.request
        print(music)
        urllib.request.urlretrieve(music[1], "song/" + music[0] + ".mp3")
        print("downss")

if __name__ == "__main__":
    m = music()
    m.load(choseSong("anger"))
    m.play()
    m.stop()
    print("finish1")
    m.preMusic()
    m.stop()
    print("finish2")
    m.nextMusic(['What If', '[图片]http://music.163.com/song/media/outer/url?id=1217607.mp3'])
    m.stop()
    print("finish3")


from multiprocessing import Pool, cpu_count
from time import time

import requests
from matplotlib import pyplot as plt

def baidu(_):
    rep = requests.get('https://www.baidu.com')
    code = rep.status_code
    return time()

def highConcurrent(count):

    pool = Pool(cpu_count())
    tl = []
    start_time = time()

    for i in range(count):
        p = pool.apply_async(baidu, args=(i, ))
        tl.append(p)

    tl = [t.get() for t in tl]
    pool.close()
    pool.join()

    time_li = []
    for t in tl:
        time_li.append(t - start_time)

    plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 这两行需要手动设置
    fig, ax = plt.subplots()
    ax.plot([i for i in range(1, len(time_li) + 1)], time_li)
    ax.set(xlabel='请求', ylabel='时间（s）',
           title='线程（并发）')
    ax.grid()
    fig.savefig('并发（进程 apply_async）.png')

if __name__ == '__main__':
    highConcurrent(100)


# This file made available under CC0 1.0 Universal (https://creativecommons.org/publicdomain/zero/1.0/legalcode)
#
# Description: Check that security groups prefixed with "launch-wizard"
#              are not associated with network interfaces.'
#
# Trigger Type: Change Triggered
# Scope of Changes: EC2:NetworkInterface
# Accepted Parameters: None

import boto3
import botocore
import json
import os

APPLICABLE_RESOURCES = ["AWS::EC2::SecurityGroup"]

def evaluate_security_group(sg_groupId, included_items, excluded_items, debug_enabled):
	compliance_type = 'COMPLIANT'
	annotation = "Resource is compliant."
	# Call describe_security_groups because the IpPermissions that are returned
	# are in a format that can be used as the basis for input to
	# authorize_security_group_ingress and revoke_security_group_ingress.
	client = boto3.client("ec2");
	if (sg_groupId in excluded_items):
		print("Launch-wizard security group is in excluded group: ", sg_groupId)
		annotation = "Launch-wizard security group " + sg_groupId + " is in excluded groups"
		return {
			"compliance_type": compliance_type,
			"annotation": annotation
		}
	if (not("all" in included_items)) and (not(sg_groupId in included_items)):
		print("Launch-wizard security group is not in included items: ", sg_groupId)
		annotation = "Launch-wizard security group " + sg_groupId + " is not in included groups"
		return {
			"compliance_type": compliance_type,
			"annotation": annotation
		}

	try:
		response = client.describe_security_groups(GroupIds=[sg_groupId])
	except botocore.exceptions.ClientError as e:
		return {
			"compliance_type" : "NON_COMPLIANT",
			"annotation" : "describe_security_groups failure on group " + group_id
		}
	if debug_enabled:
		print("security group definition: ", json.dumps(response, indent=2))

	ip_permissions = response["SecurityGroups"][0]["IpPermissions"]
	ip_permissions_egress = response["SecurityGroups"][0]["IpPermissionsEgress"]

	try:
		if (len(ip_permissions) != 0):
			client.revoke_security_group_ingress(GroupId=sg_groupId, IpPermissions=ip_permissions)
		if (len(ip_permissions_egress) != 0):
			client.revoke_security_group_egress(GroupId=sg_groupId, IpPermissions=ip_permissions_egress)
	except botocore.exceptions.ClientError as e:
		return {
			"compliance_type" : "NON_COMPLIANT",
			"annotation" : "revoke_security_group_ingress failure on group " + sg_groupId
		}
	compliance_type = 'NON_COMPLIANT'
	annotation = 'Removed IP permissions from a launch-wizard security group ' + sg_groupId
	return {
		"compliance_type": compliance_type,
		"annotation": annotation
	}

def evaluate_compliance(configuration_item, included_items, excluded_items, debug_enabled):
	# Start as compliant
	compliance_type = 'COMPLIANT'
	annotation = "Resource is compliant."

	# Check resource for applicability

	if configuration_item["resourceType"] not in APPLICABLE_RESOURCES:
		compliance_type = 'NOT_APPLICABLE'
		annotation = "The rule doesn't apply to resources of type " + configuration_item["resourceType"] + "."
		return {
			"compliance_type": compliance_type,
			"annotation": annotation
		}
	if not('configuration' in configuration_item):
		return {
			"compliance_type": compliance_type,
			"annotation": "There are no resources to evaluate"
		}

	if not('groups' in configuration_item['configuration']):
		if 'groupId' in configuration_item['configuration']:
			if "launch-wizard" in configuration_item['configuration']['groupName']:
				single_sg_id = configuration_item['configuration']['groupId']
				return evaluate_security_group(single_sg_id, included_items, excluded_items, debug_enabled)
			else:
				return {
					"compliance_type": compliance_type,
					"annotation": "This is not a launch-wizard security group"
				}
		else:
			return {
				"compliance_type": compliance_type,
				"annotation": "There are no resources to evaluate"
			}

	# Iterate over security groups
	for sg in configuration_item['configuration']['groups']:
		if "launch-wizard" in sg['groupName']:
			sg_groupId = sg['groupId']
			return evaluate_security_group(sg_groupId, included_items, excluded_items, debug_enabled)

	return {
		"compliance_type": compliance_type,
		"annotation": annotation
	}

def lambda_handler(event, context):
	debug_enabled = True
	invoking_event = json.loads(event['invokingEvent'])
	configuration_item = invoking_event["configurationItem"]

	print("Invoking Event = ", invoking_event)
	print("Configuration Item = ", configuration_item)

	account_id = context.invoked_function_arn.split(":")[4]
	s3 = boto3.client('s3')

	bucket = os.environ["CENTRAL_POLICY_BUCKET"]
	# bucket = 'sp-central-policy-bucket'
	key = 'main_policy.txt'
	obj = s3.get_object(Bucket=bucket, Key=key)
	j = json.loads(obj['Body'].read())
	if debug_enabled:
		print("Received master policy: " + json.dumps(j, indent=2))

	included_items = ["all"]
	excluded_items = ["none"]
	account_policy_object = None
	key = "account_" + account_id + "_policy.txt"
	print("Account Key Used: " + key)
	account_policy_object = s3.get_object(Bucket=bucket, Key=key)
	if not(account_policy_object is None):
		ap = json.loads(account_policy_object['Body'].read())
		if debug_enabled:
			print("Received account specific policy: " + json.dumps(ap, indent=2))

	policies_enabled = False
	for item in j['policies']:
		if item['policy_name'] == 'DISALLOW_LAUNCH_WIZARD_IP_PERMISSIONS' and item['enabled'] == 'yes':
			policies_enabled = True
			if not(ap is None):
				for ap_item in ap['policies']:
					if (ap_item['policy_name'] == 'DISALLOW_LAUNCH_WIZARD_IP_PERMISSIONS') and (ap_item['enabled'] == 'yes'):
						included_items = ap_item["include"]
						excluded_items = ap_item["exclude"]
			evaluation = evaluate_compliance(configuration_item, included_items, excluded_items, debug_enabled)

	if policies_enabled == False:
		print('No policies were enabled')
		return

	config = boto3.client('config')

	print('Compliance evaluation for ', (configuration_item['resourceId'], evaluation["compliance_type"]))
	print('Annotation: ', (evaluation["annotation"]))

	response = config.put_evaluations(
		Evaluations=[
			{
				'ComplianceResourceType': invoking_event['configurationItem']['resourceType'],
				'ComplianceResourceId': invoking_event['configurationItem']['resourceId'],
				'ComplianceType': evaluation["compliance_type"],
				"Annotation": evaluation["annotation"],
				'OrderingTimestamp': invoking_event['configurationItem']['configurationItemCaptureTime']
			},
		],
		ResultToken=event['resultToken'])


from flask import Flask, jsonify, request

app = Flask(__name__)
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.learner.walk_through import WalkThrougher

def init():
    pass

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    return 'California: Data Understanding algorithms system.'

@app.route('/california_v1', methods=['POST'])
def intent_matching():
    # pass-through: return empty string.
    request_json = request.json
    first_letters = request_json['column_names']
    wt = WalkThrougher()
    res = []
    for fl in first_letters:
        res.append({
            'key': fl,
            'res': wt.combined_match(fl)
        })
    res_json = {
        'results': res
    }

    return jsonify(res_json)

if __name__ == '__main__':
    init()
    app.run(host='0.0.0.0', port=80)


import torch
import torchvision.transforms as Transforms

use_gpu = True if torch.cuda.is_available() else False

# trained on high-quality celebrity faces "celebA" dataset
# this model outputs 512 x 512 pixel images
model = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub',
                       'PGAN', model_name='celebAHQ-512',
                       pretrained=True, useGPU=use_gpu)
# this model outputs 256 x 256 pixel images
# model = torch.hub.load('facebookresearch/pytorch_GAN_zoo:hub',
#                        'PGAN', model_name='celebAHQ-256',
#                        pretrained=True, useGPU=use_gpu)
num_images = 1

def pgan():
    noise, _ = model.buildNoiseData(num_images)
    with torch.no_grad():
        generated_images = model.test(noise)

    transform = Transforms.Compose([Transforms.Normalize((-1., -1., -1.), (2, 2, 2)),
                                    Transforms.ToPILImage()])
    generated_images = generated_images[0]
    generated_images = transform(generated_images.clamp(min=-1, max=1))
    return generated_images


# Load External packages
import numpy as np                                      # for wicked fast arrays                                  MORE INFORMATION: https://docs.scipy.org/doc/
from numpy import *                                     # for ease of function use
from numpy.linalg import *                              # for ease of linalg function use
from numba import njit                                  # for compiling functions into C, so they run faster.     MORE INFORMATION: http://numba.pydata.org/
from numba import jit
import random as rd                                     # for random numbers

# internal dependencies
from .basic import *

## for iterating points (and saving the array of points)
def generate_points(n, transformations, weights=np.array([0.]), startingPoint=np.array([0.,0.,1.]), frame=np.array([0.])):
    start = time.time()
    print(f'Generating {n} points...', end='')
    if all(frame == np.array([0.])):
        G = _generate_points_full(n, transformations, weights, startingPoint)
    else:
        startingPoint = np.array([(frame[1]-frame[0])/2,(frame[3]-frame[2])/2,1.])
        G = _generate_points_zoom(n, transformations, weights, startingPoint, frame)
    print(f' Finished in {time.time()-start} seconds.')
    return G

@njit
def _generate_points_full(n, transformations, weights=np.array([0.]), startingPoint=np.array([0.,0.,1.])):
    if all(weights == np.array([0.])):
        return _generate_points_full_simple(n, transformations, startingPoint)
    output = np.array([[startingPoint[0],startingPoint[1],1.]]*n)
    for i in range(100):
        output[0] = transformations[choose_random_index(weights)] @ output[0]
    for i in range(1,n):
        output[i] = transformations[choose_random_index(weights)] @ output[i-1]
    return (output[:,0],output[:,1])

@njit
def _generate_points_full_simple(n, transformations, startingPoint=np.array([0.,0.,1.])):
    output = np.array([[startingPoint[0],startingPoint[1],1.]]*n)
    for _ in np.arange(100):
        output[0] = transformations[rd.randint(0,len(transformations)-1)] @ output[0]
    for i in np.arange(1,n):
        output[i] = transformations[rd.randint(0,len(transformations)-1)] @ output[i-1]
    return (output[:,0], output[:,1])

@njit
def _generate_points_zoom(n, transformations, weights=np.array([0.]), startingPoint=np.array([0.,0.,1.]),  frame=np.array([0.])):
    if all(weights == np.array([0.])):
        return generate_points_zoom_simple(n, transformations, startingPoint, frame)
    if all(frame == np.array([0.])): # this is to keep the order of function inputs consistant
        raise ValueError('ifsFractals: _generate_points_zoom was not given a zoom frame.')
    output = np.array([[startingPoint[0],startingPoint[1],1]]*n, dtype=np.float64)
    for _ in range(20):
        potentialPoint = transformations[choose_random_index(weights)] @ outputFigures[i-1]
        while (frame[0]>potentialPoint[0]) or (frame[1]<potentialPoint[0]) or (frame[2]>potentialPoint[1]) or (frame[3]<potentialPoint[1]):
            potentialPoint = transformations[choose_random_index(weights)] @ potentialPoint
        output[0] = potentialPoint
    for i in np.arange(1,n):
        potentialPoint = transformations[choose_random_index(weights)] @ outputFigures[i-1]
        while (frame[0]>potentialPoint[0]) or (frame[1]<potentialPoint[0]) or (frame[2]>potentialPoint[1]) or (frame[3]<potentialPoint[1]):
            potentialPoint = transformations[choose_random_index(weights)] @ potentialPoint
        output[i] = potentialPoint
    return (output[:,0], output[:,1])

@njit
def _generate_points_zoom_simple(n, transformations, startingPoint, frame):
    outputFigures = np.array([[startingPoint[0],startingPoint[1],1.]]*n)
    for _ in range(20):
        potentialPoint = transformations[rd.randint(0,len(transformations)-1)] @ potentialPoint
        while (frame[0]>potentialPoint[0]) or (frame[1]<potentialPoint[0]) or (frame[2]>potentialPoint[1]) or (frame[3]<potentialPoint[1]):
            potentialPoint = transformations[rd.randint(0,len(transformations)-1)] @ potentialPoint
        output[0] = potentialPoint
    for i in np.arange(1,n):
        potentialPoint = transformations[rd.randint(0,len(transformations)-1)] @ potentialPoint
        while (frame[0]>potentialPoint[0]) or (frame[1]<potentialPoint[0]) or (frame[2]>potentialPoint[1]) or (frame[3]<potentialPoint[1]):
            potentialPoint = transformations[rd.randint(0,len(transformations)-1)] @ potentialPoint
        output[i] = potentialPoint
    return (output[:,0], output[:,1])

@njit
def find_bounds(transformations, weights=np.array([0.])):
    G = _generate_points_full(100000, transformations=transformations, weights=weights)
    xmin, xmax, ymin, ymax = np.min(G[0]), np.max(G[0]), np.min(G[1]), np.max(G[1])
    errX, errY = (xmax-xmin)/10, (ymax-ymin)/10
    return np.array([xmin-errX, xmax+errX, ymin-errY, ymax+errY], dtype=np.float64)


# pylint:disable=redefined-outer-name
# pylint:disable=protected-access

import pytest
from aiohttp import ClientSession, ClientWebSocketResponse, WSMessage, WSMsgType

from tinvest import Streaming

@pytest.fixture()
async def queue(streaming):
    return streaming._queue

@pytest.fixture()
async def message(mocker):
    msg = mocker.AsyncMock(WSMessage)
    msg.type = WSMsgType.TEXT
    return msg

@pytest.fixture()
async def ws(mocker):
    return mocker.AsyncMock(ClientWebSocketResponse)

@pytest.fixture()
async def session(mocker, ws, message):
    ws.__aiter__.return_value = [message]

    s = mocker.AsyncMock(ClientSession)
    s.closed = False
    s.ws_connect.return_value.__aenter__.return_value = ws
    s.ws_connect.return_value.__aexit__.return_value = False
    return s

@pytest.fixture()
def closed_session(session):
    session.closed = True
    return session

@pytest.fixture()
async def streaming(token, session):
    return Streaming(token, session=session, reconnect_enabled=False)


from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException
from Classes.CleanText import CleanText
from Classes.RipPage import RipPage

class GrantForwardLeads(object):
    def __init__(self, searchTerm, isTest=False):
        self.isTest = isTest
        self.searchTerm = searchTerm
        self.driver = webdriver.Firefox()
        self.base_url = 'https://www.grantforward.com/'

        self.arrayOfGrantForwardLeads = []
        self.arrayOfResultsPageArrays = []

        self.driver.get(self.base_url + '/index')
        self.driver.find_element_by_id('keyword').clear()
        self.driver.find_element_by_id('keyword').send_keys(self.searchTerm)
        self.driver.find_element_by_xpath('//div[2]/button').click()
        self.driver.implicitly_wait(2)

    def processSearchResultsAndMakeLeadArray(self):
        self.getTitlesAndLinksFromSearchResults()

        if self.arrayOfResultsPagesLinks != []:
            if self.isTest != True:
                isThereNextPage = self.checkIfNextPage()
                pageCount = 2
                while isThereNextPage == True and pageCount <= 10:
                    self.goToNextPage()
                    self.getTitlesAndLinksFromSearchResults()
                    isThereNextPage = self.checkIfNextPage()
                    pageCount += 1

            for singleResultArray in self.arrayOfResultsPageArrays:
                self.makeLeadArrayAndAddToGrantForwardLeads(singleResultArray)

        self.driver.quit()

        return self.arrayOfGrantForwardLeads

    def getTitlesAndLinksFromSearchResults(self):
        self.arrayOfTitles = self.driver.find_elements_by_xpath("//a[@class = 'grant-url']")
        self.arrayOfResultsPagesLinks = []
        for i in self.arrayOfTitles:
            self.arrayOfResultsPagesLinks.append(i.get_attribute('href'))

        for i in range(len(self.arrayOfTitles)):
            title = self.arrayOfTitles[i].text
            resultPageLink = self.arrayOfResultsPagesLinks[i]
            singleResultArray = [title, resultPageLink]
            self.arrayOfResultsPageArrays.append(singleResultArray)

    def makeLeadArrayAndAddToGrantForwardLeads(self, singleResultArray):
        name = CleanText.cleanALLtheText(singleResultArray[0])
        url = singleResultArray[1]
        resultPageInfo = self.goToResultPageAndPullInformation(url)

        keyword = CleanText.cleanALLtheText(self.searchTerm)
        description = CleanText.cleanALLtheText(resultPageInfo[0])
        sponsor = CleanText.cleanALLtheText(resultPageInfo[1])
        amount = CleanText.cleanALLtheText(resultPageInfo[2])
        eligibility = CleanText.cleanALLtheText(resultPageInfo[3])
        submissionInfo = CleanText.cleanALLtheText(resultPageInfo[4])
        categories = CleanText.cleanALLtheText(resultPageInfo[5])
        opportunitySourceLink = resultPageInfo[6]
        opportunitySourceText = CleanText.cleanALLtheText(RipPage.getPageSource(opportunitySourceLink))

        singleLeadArray = [keyword, url, name, description, sponsor, amount, eligibility, submissionInfo, categories,
                           opportunitySourceLink, opportunitySourceText]

        self.arrayOfGrantForwardLeads.append(singleLeadArray)

    def goToResultPageAndPullInformation(self, resultPageLink):
        self.driver.get(resultPageLink)
        self.driver.implicitly_wait(2)
        description = ''
        sponsor = ''
        amount = ''
        eligibility = ''
        submissionInfo = ''
        categories = ''
        opportunitySourceLink = ''

        if self.checkIfElementExists("//div[@id = 'field-description']/div[@class = 'content-collapsed']"):
            descriptionDiv = self.driver.find_element_by_xpath(
                "//div[@id = 'field-description']/div[@class = 'content-collapsed']")
            description = descriptionDiv.get_attribute('textContent')

        if self.checkIfElementExists("//div[@class = 'sponsor-content']/div/a"):
            sponsorDiv = self.driver.find_element_by_xpath("//div[@class = 'sponsor-content']/div/a")
            sponsor = sponsorDiv.get_attribute('textContent')

        if self.checkIfElementExists("//div[@id = 'field-amount_info']/div[@class = 'content-collapsed']"):
            amountDiv = self.driver.find_element_by_xpath(
                "//div[@id = 'field-amount_info']/div[@class = 'content-collapsed']")
            amount = amountDiv.get_attribute('textContent')

        if self.checkIfElementExists("//div[@id = 'field-eligibility']/div[@class = 'content-collapsed']"):
            eligibilityDiv = self.driver.find_element_by_xpath(
                "//div[@id = 'field-eligibility']/div[@class = 'content-collapsed']")
            eligibility = eligibilityDiv.get_attribute('textContent')

        if self.checkIfElementExists("//div[@id = 'field-submission_info']/div[@class = 'content-collapsed']"):
            submissionInfoDiv = self.driver.find_element_by_xpath(
                "//div[@id = 'field-submission_info']/div[@class = 'content-collapsed']")
            submissionInfo = submissionInfoDiv.get_attribute('textContent')

        if self.checkIfElementExists("//div[@id = 'field-subjects']/ul"):
            categoriesDiv = self.driver.find_element_by_xpath("//div[@id = 'field-subjects']/ul")
            categories = categoriesDiv.get_attribute('textContent')

        if self.checkIfElementExists("//a[@class = 'source-link btn btn-warning']"):
            opportunitySourceLinkDiv = self.driver.find_element_by_xpath("//a[@class = 'source-link btn btn-warning']")
            opportunitySourceLink = opportunitySourceLinkDiv.get_attribute('href')

        resultPageInfo = [description, sponsor, amount, eligibility, submissionInfo, categories, opportunitySourceLink]
        return resultPageInfo

    def checkIfNextPage(self):
        checkNextPage = self.driver.find_elements_by_xpath("(//a[contains(text(), 'Next')])[1]")
        if checkNextPage != []:
            return True
        else:
            return False

    def goToNextPage(self):
        try:
            self.driver.find_element_by_xpath("(//a[contains(text(), 'Next')])[1]").click()
            self.driver.implicitly_wait(2)
        except ElementNotVisibleException:
            self.driver.implicitly_wait(2)

    def checkIfElementExists(self, xpath):
        checkElementExists = self.driver.find_elements_by_xpath(xpath)
        if checkElementExists != []:
            return True
        else:
            return False


#import myfitnesspal
#import datetime

#mfp_day = datetime.datetime.today()
#user = 'spiccirillo1'

#client = myfitnesspal.Client('EBANutrition', '3BATrain!!')
#food_log1 = client.get_date(mfp_day.year, mfp_day.month, mfp_day.day, username=user)
#food_log1.meals
#food_log2 = client.get_date_bh(mfp_day.year, mfp_day.month, mfp_day.day, username=user)
#food_log2.meals
#food_log2.melas[0].entries

####
#import requests
#session = requests.Session()

import lxml.html
import re
#from .entry import Entry

#with open(r'html/sample_printable_diary.html', "r") as f:
with open(r'html/sample_printable_diary_w_exercise.html', "r") as f:
    page = f.read()
tree = lxml.html.document_fromstring(page)
#mfp_table = tree.xpath("//table[@id='food']")

meals   = []
entries = []

food_log_table = tree.xpath("//table[@id='food']")
for row in food_log_table[0].xpath("//tr") or []:
    tds = row.findall('td')
    print(tds[0].text)
    # Table header row
#    if tds[0].text.lower() == 'foods':
#        print("table header")
#        for td in tds:
#            print(td.text)
    # Rows with a 'class' attribute of 'title' are meal headers
#    elif 'class' in row.attrib:
        # If a previous meal has been extracted add it to meals list and clear entries list for next meal
#        if entries:
#            meals.append(meal_name)
#            entries = []
#        meal_name = tds[0].text.lower()
#        print(meal_name)
    # All other rows are food log entries
#    else:
#        entries.append(tds[0].text.lower())
#        for td in tds:
#            value = re.sub(r'[^\d.]+', '', td.text)
#            try:
#                num_value = int(value)
#            except:
#                num_value = 0
#            print(num_value)

# Add final meal to list
#if entries:
#    meals.append(meal_name)

#print("MEALS")
#print(meals)


import asyncio
import logging
import logging.config
import os
import time
import sys
import signal

import discord
import dotenv
import yaml
from discord.ext import commands

from discord_slash import SlashCommand
from discord_components import DiscordComponents

CORE_EXTENSIONS = [
    "bot.cogs.core.events",
    "bot.cogs.core.help",
    "bot.cogs.core.info",
    "bot.cogs.core.owner",
    "bot.cogs.core.settings",
    "bot.cogs.core.tasks",
]

EXTENSIONS = [
    # "bot.cogs.colors",
    # "bot.cogs.economy",
    "bot.cogs.embeds",
    "bot.cogs.fun",
    "bot.cogs.games",
    # "bot.cogs.greetings",
    # "bot.cogs.links",
    "bot.cogs.math",
    "bot.cogs.moderation",
    # "bot.cogs.modlogs",
    "bot.cogs.reaction_roles",
    "bot.cogs.search",
    "bot.cogs.starboard",
    "bot.cogs.suggestions",
]

with open("logging.yml", "r") as f:
    logging.config.dictConfig(yaml.load(f, Loader=yaml.SafeLoader))

def main():
    logger = logging.getLogger(__name__)
    dotenv.load_dotenv()
    try:
        with open("config.yml", "r") as f:
            config = yaml.load(f, Loader=yaml.SafeLoader)
    except FileNotFoundError:
        logger.critical(
            "No config.yml found. Please make sure it is in this directory.",
            extra={"botname": "N/A"},
        )
        sys.exit(1)

    logger.info("Parsing config.yml.", extra={"botname": "N/A"})
    bots = []
    for botconf in config["bots"]:
        adplogger = logging.LoggerAdapter(logger, extra={"botname": botconf["name"]})
        try:
            if not botconf["name"]:
                adplogger.error(
                    "Misconfigured config.yml file. You must set a name for each bot."
                )
            if not botconf["prefix"]:
                adplogger.error(
                    "Misconfigured config file. You must set a prefix for each bot."
                )
            if not botconf["token"]["value"] and not botconf["token"]["env"]:
                adplogger.error(
                    "Misconfigured config.yml file. You must set the bot's token either in config.yml or in your .env file."
                )
            if botconf["token"]["value"] and botconf["token"]["env"]:
                adplogger.error(
                    "Misconfigured config.yml file. You cannot have both value and env declared under token."
                )
        except KeyError:
            adplogger.error(
                "Invalid config.yml file. One or more keys are missing. Please use the template provided in the repository as well as the tutorial for configuring your bot.",
            )
            sys.exit(1)

        adplogger.info("Creating bot instance.")
        try:
            bot = commands.Bot(
                command_prefix=botconf["prefix"],
                help_command=commands.DefaultHelpCommand
                if botconf["use_default_help_command"]
                else None,
                intents=discord.Intents.all()
                if botconf["gateway_intents"]
                else discord.Intents.default(),
                description=botconf["description"],
            )
            bot.name = botconf["name"]
            bot.token = (
                os.environ[botconf["token"]["name"]]
                if botconf["token"]["env"]
                else botconf["token"]["value"]
            )
            bot.AZURE_KEY = os.environ["AZURE_KEY"]
        except:
            adplogger.error(
                "Failed to create bot instance. Check your config.yml file."
            )
            sys.exit(1)

        adplogger.info(
            "Activating Discord interactions (slash commands, buttons, and selects)."
        )

        slash = SlashCommand(bot, sync_commands=True)
        # DiscordComponents(bot)

        adplogger.info("Loading extensions.")

        for extension in CORE_EXTENSIONS:
            bot.load_extension(extension)

        for extension in EXTENSIONS:
            bot.load_extension(extension)

        # if botconf["extensions"]["enabled"]:
        #     for extension in botconf["extensions"]["enabled"]:
        #         try:
        #             bot.load_extension(extension)
        #         except:
        #             adplogger.error(
        #                 f"Unable to load extension {extension}. Check if the file exists."
        #             )
        # if botconf["extensions"]["disabled"]:
        #     for extension in EXTENSIONS:
        #         if extension not in botconf["extensions"]["disabled"]:
        #             bot.load_extension(extension)

        # if botconf["extensions"]["custom"]:
        #     for extension in botconf["extensions"]["custom"]:
        #         try:
        #             bot.load_extension(extension)
        #         except:
        #             adplogger.error(
        #                 f"Unable to load extension {extension}. Check if the file exists.",
        #                 exc_info=True,
        #             )

        bots.append(bot)

    loop = asyncio.get_event_loop()
    try:
        loop.add_signal_handler(signal.SIGINT, lambda: loop.stop())
        loop.add_signal_handler(signal.SIGTERM, lambda: loop.stop())
    except NotImplementedError:
        pass

    for bot in bots:
        logger.info("Starting bot.", extra={"botname": bot.name})
        loop.create_task(bot.start(bot.token))

    loop.run_forever()
    # except KeyboardInterrupt:
    #     logger.info("Received signal to shut down bots.", extra={"botname": "N/A"})
    #     loop.stop()
    # except Exception as e:
    #     print(e)

if __name__ == "__main__":
    main()


#Go Corona Go - a fun game developed using PyGame

import pygame
import random
import sys
import math
import time

from pygame import display, event
from pygame import mixer

# initialize pygame
pygame.init()

screen = pygame.display.set_mode((800, 600))

running = True

# background
background = pygame.image.load('background1.jpeg')
intro_background = pygame.image.load('intro_bg3.jpg')

# background music
mixer.music.load('back_music.wav')
mixer.music.play(-1)

# caption and icon
pygame.display.set_caption("GO CORONA!")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

# colours
bright_red = (250, 0, 0)
red = (200, 0, 0)
bright_green = (0,250,0)
green = (0,200, 0)

# sanitiser
player_img = pygame.image.load('sanitiser.png')
player_X = 368
player_Y = 500
playerX_change = 0

# corona
corona_img = []
corona_X = []
corona_Y = []
coronaX_change = []
coronaY_change = []
num_of_virus = 6

for i in range(num_of_virus):
    corona_img.append(pygame.image.load('corona.png'))
    corona_X.append(random.randint(0, 736))
    corona_Y.append(random.randint(50, 150))
    coronaX_change.append(2)
    coronaY_change.append(40)

# water
water_img = []
water_X = []
water_Y = []
waterX_change = []
waterY_change = []
water_state = []
num_of_drops = 5

for j in range(num_of_drops):
    water_img.append(pygame.image.load('water.png'))
    water_X.append(0)
    water_Y.append(480)
    waterX_change.append(0)
    waterY_change.append(3)
    water_state.append('ready')

# score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
max_scores=[0]

textX = 10
textY = 10

# game over text
over_font = pygame.font.Font('freesansbold.ttf', 64)

# button
def button(msg, x, y, w, h, ic, ac, action = None):
    global running, score_value
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    small_text = pygame.font.Font('freesansbold.ttf', 20)

    if x < mouse[0] < x + w and y < mouse[1] < y + h:
        pygame.draw.rect(screen, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            if action == 'play':
                running = True
                for i in range(num_of_virus):
                    corona_X[i] = (random.randint(0, 736))
                    corona_Y[i] = (random.randint(50, 150))
                player_X = 368
                player_Y = 500
                score_value = 0
                game_loop()
            elif action == 'quit':
                pygame.quit()
                quit()

    else:
        pygame.draw.rect(screen, ic, (x, y, w, h))
    text = small_text.render(msg, True, (0, 0, 0))
    screen.blit(text, (x + (w/2) - 22, y + (h/2) - 10))

def game_intro():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
        screen.fill((255, 255, 255))
        screen.blit(intro_background, (0, -100))
        name = over_font.render("GO CORONA!", True, (0, 0, 0))
        tiny_text = pygame.font.Font('freesansbold.ttf', 20)
        by_name = tiny_text.render("By Abitha Rao", True, (0, 0, 0))
        screen.blit(name, (200, 50))
        screen.blit(by_name, (340, 500))

        small_text = pygame.font.Font('freesansbold.ttf', 20)

        button("PLAY", 150, 450, 100, 50, bright_green, green, action = 'play')
        button("QUIT", 550, 450, 100, 50, bright_red, red, action= 'quit')

        pygame.display.update()

def show_score(x, y):
    score = font.render("Score : " + str(score_value), True, (0, 0, 0))
    max_score = font.render("Max Score : " + str(max_scores[0]), True, (0, 0, 0))
    screen.blit(score, (x, y))
    screen.blit(max_score, (x, y+40))

def game_over_text():
    over_text = over_font.render("GAME OVER!", True, (0, 0, 0))
    screen.blit(over_text, (200, 250))

def player(x, y):
    screen.blit(player_img, (x, y))

def corona(x, y, i):
    screen.blit(corona_img[i], (x, y))

def spray(x, y, j):
    global water_state
    water_state[j] = 'spray'
    screen.blit(water_img[j], (x - 10, y + 10))

def isCollision(corona_X, corona_Y, water_X, water_Y):
    dist = math.sqrt((math.pow(corona_X - water_X, 2)) + (math.pow(corona_Y - water_Y, 2)))
    if dist <= 27:
        return True
    else:
        return False

def game_loop():
    global player_X, playerX_change, player_Y, score_value, running, corona_X, corona_Y
    while running:

        screen.fill((0, 0, 0))
        # background image
        screen.blit(background, (0, 0))

        for event in pygame.event.get():
             if event.type == pygame.QUIT:
                running = False
                if score_value > max_scores[0]:
                    max_scores.clear()
                    max_scores.append(score_value)
                game_intro()

            # keystrokes
             if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    playerX_change = -2
                if event.key == pygame.K_RIGHT:
                    playerX_change = 2
                if event.key == pygame.K_SPACE:
                    for j in range(num_of_drops):
                        if water_state[j] == 'ready':
                            water_sound = mixer.Sound('shoot.wav')
                            water_sound.play()
                            water_X[j] = player_X
                            spray(water_X[j], water_Y[j], j)

             if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerX_change = 0

        player_X += playerX_change

        if player_X <= 0:
             player_X = 0
        elif player_X >= 736:
             player_X = 736

        #corona movement
        for i in range(num_of_virus):

            # game over
            if corona_Y[i]  > 440:
                for j in range(num_of_virus):
                    corona_Y[j] = 1000
                game_over_text()
                player_X = 368
                player_Y = 500
                if score_value > max_scores[0]:
                    max_scores.clear()
                    max_scores.append(score_value)
                break

            corona_X[i] += coronaX_change[i]
            if corona_X[i] <= 0:
                coronaX_change[i] = 3
                corona_Y[i] += coronaY_change[i]
            elif corona_X[i] >= 736:
                coronaX_change[i] = -3
                corona_Y[i] += coronaY_change[i]

            # collision:
            for j in range(num_of_drops):
                collision = isCollision(corona_X[i], corona_Y[i], water_X[j], water_Y[j])
                if collision:
                    explosion_sound = mixer.Sound('explosion.wav')
                    explosion_sound.play()
                    water_Y[j] = 480
                    water_state[j] = 'ready'
                    score_value += 1
                    corona_X[i] = random.randint(0, 736)
                    corona_Y[i] = random.randint(50, 150)

            corona(corona_X[i], corona_Y[i], i)

        # spray movement
        for j in range(num_of_drops):
            if water_Y[j] <= 0:
                water_Y[j] = 480
                water_state[j] = 'ready'
            if water_state[j] is 'spray':
                spray(water_X[j], water_Y[j], j)
                water_Y[j] -= waterY_change[j]

        player(player_X, player_Y)

        show_score(textX, textY)
        pygame.display.update()

game_intro()


from django.shortcuts import render, HttpResponse, redirect
# from django.core.urlresolvers import reverse
import random, time

def index(request):
    if "total" not in request.session:
        request.session['total'] = 0
    return render(request, 'ninjagold/index.html')

time = {"time": time.strftime('%B %d, %Y %I:%M %p')}
def process(request):
    randNum = {
        "farm": random.randrange(10, 21),
        "cave": random.randrange(5, 11),
        "house": random.randrange(2, 6),
        "casino": random.randrange(0, 51),
        "pick": random.randrange(0, 2)
    }
    building = request.POST['building']

    if "messages" not in request.session:
        request.session['messages'] = []
    if building == "farm":
        request.session['total'] += randNum['farm']
        result = {
            "message":"Earned " + str(randNum['farm']) + " golds from the farm! " + str(time['time']),
            "color": "green"
        }
        request.session['messages'].append(result)
    elif building == "cave":
        request.session['total'] += randNum['cave']
        result = {
            "message": "Earned " +  str(randNum['cave']) + " golds from a cave! " + str(time['time']),
            "color": "green"
        }
        request.session['messages'].append(result)
    elif building == "house":
        request.session['total'] += randNum['house']
        result = {
            "message" : "Earned " + str(randNum['house']) + " golds from the house! " + str(time['time']),
            "color": "green"
        }
        request.session['messages'].append(result)
    elif building == "casino":
        if randNum['pick'] == 0:
            request.session['total'] += randNum['casino']
            result = {
                "message":"Earned " + str(randNum['casino']) + " golds from a casino! " +     str(time['time']),
                "color": "green"
            }
            request.session['messages'].append(result)
        else:
            request.session['total'] -= randNum['casino']
            result = {
                "message":"Entered a casino and lost " + str(randNum['casino']) + " golds...Ouch..." + str(time['time']),
                "color": "red"
            }
            request.session['messages'].append(result)
    return redirect('ninjagold:index')

def clear(request):
    request.session['messages'] = []
    return redirect('ninjagold:index')

def restart(request):
    request.session['total'] = 0
    request.session['messages'] = []
    return redirect('ninjagold:index')


from flask import Flask, render_template

from doctor import Doctor

app = Flask(__name__)
doctor = Doctor('medical.pl')

# Set up PyHamlJinja
app.jinja_env.add_extension('pyhaml_jinja.HamlExtension')

@app.route('/')
def home():
  return render_template('home.haml')

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


import json
from json import encoder

namen = 'n'
tip = 't'
barva = 'b'
rože = 'r'
izbira = 'i'
shrani_izbiro = 's'

class Sopek:
    """Izberemo kakšen aranžma želimo in nato poiščemo sliko, ki mu je vektorsko najbližje  """

    def __init__(self, uid, fp):
        self.uid = uid

        self.fp = fp

    def izbira(self,izbira):
        self.izbira = izbira[:]

    def vektor(self,vektor):
        self.vektor = vektor[:]

    def id(fp):
        uid = 0
        for x in fp:
            if x == fp:
                pass
            else:
                uid += 1

    def primerjava_fingerprint_izbira(fp, izbira):
        primerjava_namen = izbira & fp[1]
        primerjava_tip = izbira & fp[2]
        primerjava_barva = izbira & fp[3]
        primerjava_roze = izbira & fp[4]
        return [primerjava_namen, primerjava_tip, primerjava_barva, primerjava_roze]

    def najboljsi_kompromisi(izbira):
        stevilo_izbire = {}
        for x in izbira:
            for namen,tip,barva,roze in izbira:
                stevilo_izbire[namen] = stevilo_izbire.get(namen, 0) + 1
                stevilo_izbire[tip] = stevilo_izbire.get(tip, 0) + 1
                stevilo_izbire[barva] = stevilo_izbire.get(barva, 0) + 1
                stevilo_izbire[roze] = stevilo_izbire.get(roze, 0) + 1
        max_ujemanje = 0
        naj_priblizek = set()

        for izbira in stevilo_izbire:
            if stevilo_izbire[izbira] > max_ujemanje:
                naj_priblizek = {izbira}
                max_ujemanje = stevilo_izbire[izbira]
            elif stevilo_izbire[izbira] == max_ujemanje:
                naj_priblizek.add(izbira)

        return naj_priblizek

    def dobi_json_slovar(self):
        return {
            "fp": self.fp,
        }

class Orozarna:

    def __init__(self, zacetni_sopki = None , prost_id = 0):
        self.sopki = zacetni_sopki = {}
        self.prost_id = prost_id

    def json_slovar(self):
        slovar = {}
        for uid, sopek in self.sopki.items():
            slovar[uid] = sopek.dobi_json_slovar()
        return slovar

    def shrani_v_json_datoteko(self, ime_datoteke):
        with open(ime_datoteke, 'w') as out_file:
            json.dump(self.json_slovar(), out_file, indent=None)

    def nov_id(self):
        self.prost_id += 1
        return self.prost_id

    def dodaj_sopek(self, fp):
        uid = self.nov_id()
        sopek = Sopek(uid,fp)
        self.sopki[uid] = sopek

    @staticmethod
    def prevedi_json(json_slovar):
        slovar_sopek = {}
        for uid, sopek_slovar in json_slovar.items():
            slovar_sopek[int(uid)] = [
                Sopek.prevedi_json(sopek_slovar)
            ]

        return Orozarna(
            slovar_sopek, max(slovar_sopek.keys()) + 1
        )


import numpy as np
import cv2 as cv

shrub_img = cv.imread('Screenshot.jpg', cv.IMREAD_UNCHANGED)
needle_img = cv.imread('scrub.jpg', cv.IMREAD_UNCHANGED)

needle_w = needle_img.shape[1]
needle_h = needle_img.shape[0]
result = cv.matchTemplate(shrub_img, needle_img, cv.TM_CCOEFF_NORMED)

# get the best match position
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)

result = cv.matchTemplate(shrub_img, needle_img, cv.TM_SQDIFF_NORMED)

print(result)

threshold = 0.09
locations = np.where(result <= threshold)
locations = list(zip(*locations[::-1]))
rectangles = []
for loc in locations:
    rect = [int(loc[0]), int(loc[1]), needle_w, needle_h]
    rectangles.append(rect)
print(rectangles)
if len(rectangles):
    print("Found needle")

    line_color = (0, 255, 0)
    line_type = cv.LINE_4
    thickness = 4

    # need to loop over all the locations and draw the rectangles around them
    for loc in rectangles:
        # determine the box position
        top_left = loc
        bottom_right = (top_left[0]+needle_w, top_left[1]+needle_h)

        # draw the box
        cv.rectangle(shrub_img, tuple(top_left), tuple(bottom_right),
                     line_color, thickness)
cv.imshow('Matches', shrub_img)
cv.waitKey()

'''
threshold = 0.8

if max_val >= threshold:
    print('Found needle')
# get dimensions of the needle image
    needle_w = needle_img.shape[1]
    needle_h = needle_img.shape[0]

    top_left = max_loc
    bottom_right = (top_left[0]+needle_w, top_left[1]+needle_h)

    cv.rectangle(shrub_img, top_left, bottom_right, color=(
        0, 255, 0), thickness=2, lineType=cv.LINE_4)

    cv.imshow('Result', shrub_img)
    cv.waitKey()
else:
    print("Needle not found")
'''


"""
Handlers for working with version control services (i.e. GitHub) for builds.
"""

import hashlib
import json
from queue import Queue, Empty
import threading

import docker
from kubernetes import client, config
from tornado import web, gen
from tornado.iostream import StreamClosedError
from tornado.log import app_log

from .base import BaseHandler
from .build import Build

class BuildHandler(BaseHandler):
    """A handler for working with GitHub."""

    @gen.coroutine
    def emit(self, data):
        if type(data) is not str:
            serialized_data = json.dumps(data)
        else:
            serialized_data = data
        self.write('data: {}\n\n'.format(serialized_data))
        yield self.flush()

    def initialize(self):
        if self.settings['use_registry']:
            self.registry = self.settings['registry']

    def _generate_build_name(self, build_slug, ref, limit=63, hash_length=6, ref_length=6):
        """
        Generate a unique build name with a limited character length..

        Guaranteed (to acceptable level) to be unique for a given user, repo,
        and ref.

        We really, *really* care that we always end up with the same
        'build_name' for a particular repo + ref, but the default max
        character limit for build names is 63. To meet this constraint, we
        include a prefixed hash of the user / repo in all build names and do
        some length limiting :)

        Note that ``build`` names only need to be unique over a shorter period
        of time, while ``image`` names need to be unique for longer. Hence,
        different strategies are used.

        TODO: Make sure that the returned value matches the k8s name
        validation regex, which is:
        [a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*

        """
        build_slug_hash = hashlib.sha256(build_slug.encode('utf-8')).hexdigest()

        return '{name}-{hash}-{ref}'.format(
            name=build_slug[:limit - hash_length - ref_length - 2],
            hash=build_slug_hash[:hash_length],
            ref=ref[:ref_length]
        ).lower()

    @gen.coroutine
    def fail(self, message):
        yield self.emit({
            'phase': 'failed',
            'message': message + '\n',
        })

    @gen.coroutine
    def get(self, provider_prefix, spec):
        """Get a built image for a given GitHub user, repo, and ref."""
        # We gonna send out event streams!
        self.set_header('content-type', 'text/event-stream')
        self.set_header('cache-control', 'no-cache')

        # EventSource cannot handle HTTP errors,
        # so we have to send error messages on the eventsource
        if provider_prefix not in self.settings['repo_providers']:
            yield self.fail("No provider found for prefix %s" % provider_prefix)
            return

        key = '%s:%s' % (provider_prefix, spec)

        try:
            provider = self.get_provider(provider_prefix, spec=spec)
        except Exception as e:
            app_log.exception("Failed to get provider for %s", key)
            yield self.fail(str(e))
            return

        ref = yield provider.get_resolved_ref()
        if ref is None:
            yield self.fail("Could not resolve ref for %s. Double check your URL." % key)
            return
        build_name = self._generate_build_name(provider.get_build_slug(), ref).replace('_', '-')

        # FIXME: EnforceMax of 255 before image and 128 for tag
        image_name = '{prefix}{build_slug}:{ref}'.format(
            prefix=self.settings['docker_image_prefix'],
            build_slug=provider.get_build_slug(), ref=ref
        ).replace('_', '-').lower()

        if self.settings['use_registry']:
            image_manifest = yield self.registry.get_image_manifest(*image_name.split('/', 1)[1].split(':', 1))
            if image_manifest:
                yield self.emit({
                    'phase': 'built',
                    'imageName': image_name,
                    'message': 'Found built image, launching...\n'
                })
                return
        else:
            # Check if the image exists locally!
            # Assume we're running in single-node mode!
            docker_client = docker.from_env(version='auto')
            try:
                image = docker_client.images.get(image_name)
                yield self.emit({
                    'phase': 'built',
                    'imageName': image_name,
                    'message': 'Found built image, launching...\n'
                })
                return
            except docker.errors.ImageNotFound:
                # image doesn't exist, so do a build!
                pass

        try:
            config.load_incluster_config()
        except config.ConfigException:
            config.load_kube_config()

        api = client.CoreV1Api()

        q = Queue()

        if self.settings['use_registry']:
            push_secret = self.settings['docker_push_secret']
        else:
            push_secret = None

        build = Build(
            q=q,
            api=api,
            name=build_name,
            namespace=self.settings["build_namespace"],
            git_url=provider.get_repo_url(),
            ref=ref,
            image_name=image_name,
            push_secret=push_secret,
            builder_image=self.settings['builder_image_spec']
        )

        # FIXME: use a thread pool
        build_thread = threading.Thread(target=build.submit)
        log_thread = threading.Thread(target=build.stream_logs)

        build_thread.start()

        done = False

        while True:
            # FIXME: use Futures
            try:
                progress = q.get_nowait()
            except Empty:
                yield gen.sleep(0.5)
                continue

            # FIXME: If pod goes into an unrecoverable stage, such as ImagePullBackoff or
            # whatever, we should fail properly.
            if progress['kind'] == 'pod.phasechange':
                if progress['payload'] == 'Pending':
                    event = {'message': 'Waiting for build to start...\n', 'phase': 'waiting'}
                elif progress['payload'] == 'Deleted':
                    event = {
                        'phase': 'built',
                        'message': 'Built image, launching...\n',
                        'imageName': image_name
                    }
                    done = True
                elif progress['payload'] == 'Running':
                    if not log_thread.is_alive():
                        log_thread.start()
                    continue
                elif progress['payload'] == 'Succeeded':
                    # Do nothing, is ok!
                    continue
                else:
                    event = {'phase': progress['payload']}
            elif progress['kind'] == 'log':
                # We expect logs to be already JSON structured anyway
                event = progress['payload']

            try:
                yield self.emit(event)
                q.task_done()
                if done:
                    break
            except StreamClosedError:
                # Client has gone away!
                break


# uncompyle6 version 3.7.4
# Python bytecode 3.4 (3310)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/org/py4grid/scripts/py4gridserver_entrypoint.py
# Compiled at: 2014-09-03 22:05:30
# Size of source mod 2**32: 2581 bytes
"""
PY4GRID : a little framework to simule multiprocessing over a lot of computers
Copyright (C) 2014  João Jorge Pereira Farias Junior
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse, socketserver, sys, os, signal
MODEL_BASE = None
if hasattr(os, 'fork'):
    MODEL_BASE = socketserver.ForkingTCPServer
else:
    MODEL_BASE = socketserver.ThreadingTCPServer
    MODEL_BASE.daemon_threads = True

def main(init_args=None):
    import org.py4grid.GPR as gpr, org.py4grid.multicast.daemons.DiscoverServers as discover
    discover_server = None
    server = None
    server_thread = None
    parse = argparse.ArgumentParser(prog='PY4GRIDSERVER')
    parse.add_argument('-port', help='a port to server usage to listen, default is 4680', default=4680, type=int)
    init_args = parse.parse_args(init_args)
    try:
        try:
            dic = {'port': init_args.port,  'ip': ''}
            host = (
             dic['ip'], init_args.port)
            print('HOST', host)
            server = MODEL_BASE(host, gpr.ProcessRemoteClient)

            def handle_controlz(signum, frame):
                print(signum, frame, 'CONTROl + Z')
                raise Exception('Press CTRL + Z')

            signal.signal(signal.SIGTSTP, handle_controlz)
            discover_server = discover.ServerDaemon(port=init_args.port)
            discover_server.start()
            print('SERVER_ADDRESS', server.server_address)
            server.serve_forever()
        except KeyboardInterrupt as kex:
            print('KEYBOARD interrupt...')
        except Exception as ex:
            print('Exception :', ex, '...')

    finally:
        print('Stoping DiscoverDaemon')
        discover_server.StopDaemon()
        print('DiscoverDaemon Stoped')
        print('Stoping PY4GRIDSERVER')
        server.server_close()
        print('PY4GRIDSERVER Stoped')
        sys.exit(0)

if __name__ == '__main__':
    print(sys.argv)
    main(init_args=sys.argv[1:])
    sys.exit(0)


# ref from official zeroQ
# https://github.com/amirgholami/ZeroQ/blob/ba37f793dbcb9f966b58f6b8d1e9de3c34a11b8c/classification/classification/distill_data.py
# @file Different utility functions
# Copyright (c) Yaohui Cai, Zhewei Yao, Zhen Dong, Amir Gholami
# All rights reserved.
# This file is part of ZeroQ repository.
#
# ZeroQ is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ZeroQ is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZeroQ repository.  If not, see <http://www.gnu.org/licenses/>.
# *

import os
import torch
import torch.nn as nn
import copy
import tqdm
import torch.optim as optim
from train_script.utils import *
from utils.val import validation
from train_script.get_zeroQ_data import *
from utils.quantize_model import quantize_model, freeze_act, freeze_bn, un_freeze_act

def getDistilData(teacher_model,
                  dataset,
                  batch_size,
                  num_batch=1,
                  for_inception=False):
    """
	Generate distilled data according to the BatchNorm statistics in the pretrained single-precision model.
	Currently only support a single GPU.
	teacher_model: pretrained single-precision model
	dataset: the name of the dataset
	batch_size: the batch size of generated distilled data
	num_batch: the number of batch of generated distilled data
	for_inception: whether the data is for Inception because inception has input size 299 rather than 224
	"""

    # initialize distilled data with random noise according to the dataset
    dataloader = getRandomData(dataset=dataset,
                               batch_size=batch_size,
                               for_inception=for_inception)

    print('****** Data loaded ******')

    eps = 1e-6
    # initialize hooks and single-precision model
    hooks, hook_handles, bn_stats, refined_gaussian = [], [], [], []
    teacher_model = teacher_model.cuda()
    teacher_model = teacher_model.eval()

    # get number of BatchNorm layers in the model
    layers = sum([
        1 if isinstance(layer, nn.BatchNorm2d) else 0
        for layer in teacher_model.modules()
    ])

    for n, m in teacher_model.named_modules():
        if isinstance(m, nn.Conv2d) and len(hook_handles) < layers:
            # register hooks on the convolutional layers to get the intermediate output after convolution and before BatchNorm.
            hook = output_hook()
            hooks.append(hook)
            hook_handles.append(m.register_forward_hook(hook.hook))
        if isinstance(m, nn.BatchNorm2d):
            # get the statistics in the BatchNorm layers
            bn_stats.append(
                (m.running_mean.detach().clone().flatten().cuda(),
                 torch.sqrt(m.running_var +
                            eps).detach().clone().flatten().cuda()))
    assert len(hooks) == len(bn_stats)

    print("Training ZeroQ Dataset with {} batch...".format(num_batch))
    for i, gaussian_data in enumerate(dataloader):

        if i == num_batch:
            break

        # initialize the criterion, optimizer, and scheduler
        gaussian_data = gaussian_data.cuda()
        gaussian_data.requires_grad = True
        crit = nn.CrossEntropyLoss().cuda()
        optimizer = optim.Adam([gaussian_data], lr=0.5)
        scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                         min_lr=1e-4,
                                                         verbose=False,
                                                         patience=100)

        input_mean = torch.zeros(1, 3).cuda()
        input_std = torch.ones(1, 3).cuda()

        for it in tqdm.tqdm(range(500)):
            teacher_model.zero_grad()
            optimizer.zero_grad()
            for hook in hooks:
                hook.clear()
            output = teacher_model(gaussian_data)
            mean_loss = 0
            std_loss = 0

            # compute the loss according to the BatchNorm statistics and the statistics of intermediate output
            for cnt, (bn_stat, hook) in enumerate(zip(bn_stats, hooks)):
                tmp_output = hook.outputs
                bn_mean, bn_std = bn_stat[0], bn_stat[1]
                tmp_mean = torch.mean(tmp_output.view(tmp_output.size(0),
                                                      tmp_output.size(1), -1),
                                      dim=2)
                tmp_std = torch.sqrt(
                    torch.var(tmp_output.view(tmp_output.size(0),
                                              tmp_output.size(1), -1),
                              dim=2) + eps)
                mean_loss += own_loss(bn_mean, tmp_mean)
                std_loss += own_loss(bn_std, tmp_std)
            tmp_mean = torch.mean(gaussian_data.view(gaussian_data.size(0), 3,
                                                     -1),
                                  dim=2)
            tmp_std = torch.sqrt(
                torch.var(gaussian_data.view(gaussian_data.size(0), 3, -1),
                          dim=2) + eps)
            mean_loss += own_loss(input_mean, tmp_mean)
            std_loss += own_loss(input_std, tmp_std)
            total_loss = mean_loss + std_loss

            # update the distilled data
            total_loss.backward()
            optimizer.step()
            scheduler.step(total_loss.item())

        refined_gaussian.append(gaussian_data.detach().clone())

    for handle in hook_handles:
        handle.remove()

    print("Training ZeroQ Dataset Done ...........")
    return refined_gaussian

# training

def train_zeroQ(fp_model, q_model, val_dataloder, batch_size=32, for_incep=False):

    dataloader = getDistilData(
        fp_model.cuda(),
        "imagenet",
        batch_size=batch_size,
        for_inception=for_incep)

    # Freeze BatchNorm statistics
    q_model.eval()
    q_model = q_model.cuda()
    q_model = un_freeze_act(q_model)

    # update activation
    with torch.no_grad():
        for _, inputs in enumerate(dataloader):
            if isinstance(inputs, list):
                inputs = inputs[0]
                inputs = inputs.cuda()
                _ = quantize_model(inputs)

    print("Zero Shot Quantization Finished .....")

    q_model = freeze_act(q_model)
    print("Eval after zeroQ update")
    q_top_1, q_top_5 = validation(val_dataloder, q_model)

    return q_model


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

from . import views

app_name = 'myTestApplication'
urlpatterns = [
    url(r'^$', views.category_list, name='category_list'),
    url(r'^(?P<pk>[0-9]+)/export_categories/$', views.export_categories, name='export_categories'),
    url(r'^(?P<pk>[0-9]+)/export_products/$', views.export_products, name='export_products'),
    url(r'^(?P<pk>[0-9]+)/export_total/$', views.export_total, name='export_total'),
    url(r'^category/new/$', views.category_new, name='category_new'),
    url(r'^category/(?P<pk>[0-9]+)/new/$', views.category_new, name='category_new'),
    url(r'^category/(?P<pk>[0-9]+)/edit/$', views.category_edit, name='category_edit'),
    url(r'^category/(?P<pk>[0-9]+)/delete/$', views.category_delete, name='category_delete'),
    url(r'^(?P<pk>[0-9]+)/subcategory/$', views.category_list, name='category_list'),
    url(r'^(?P<pk>[0-9]+)/products/$', views.product_list, name='product_list'),
    url(r'^(?P<categoryId>[0-9]+)/products/new/$', views.product_edit, name='product_edit'),
    url(r'^(?P<categoryId>[0-9]+)/products/(?P<pk>[0-9]+)/edit/$', views.product_edit, name='product_edit'),
    url(r'^(?P<categoryId>[0-9]+)/products/(?P<pk>[0-9]+)/delete/$', views.product_delete, name='product_delete'),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


"""
Course: CSE 251
Lesson Week: 14
File: assignment.py
Author: Jesse Gibbons
Purpose: Assignment 13 - Family Search

Instructions:

Depth First Search
https://www.youtube.com/watch?v=9RHO6jU--GU

Breadth First Search
https://www.youtube.com/watch?v=86g8jAQug04

Describe how to sped up part 1

Organize families with list comprehensions and fetch them all concurrently.
Prioritize fetching parents before children so we can
recurse to fetch the parent families concurrently with
requests to fetch the children.

Before change:
total_time                   : 102.31911579999996
People and families / second : 3.8702445472070837

After changes:
total_time                   : 4.1270161000011285
People and families / second : 88.44162250782114

Describe how to sped up part 2

Since part 2 does nothing, it cannot be faster.
It also doesn't work as expected, so I can only make it slower.
Breadth-first search is done one level at a time. It can be
accomplished as follows:
If the given list is not empty:
    get every element in the list, and put all the results in a new list.
    recurse with the new list.

10% Bonus to speed up part 2

Use a 5-semaphore to control the number of simultaneous requests.
Within semaphore block, call code to request.
Outside semaphore block, do all other processing.

"""
import time
import threading
import multiprocessing as mp
from multiprocessing.pool import ThreadPool
import json
import random
import requests

# Include cse 251 common Python files - Dont change
import os, sys
sys.path.append('../../code')
from cse251 import *

TOP_API_URL = 'http://127.0.0.1:8123'

# ----------------------------------------------------------------------------
class Person:
    def __init__(self, data):
        super().__init__()
        self.id = data['id']
        self.name = data['name']
        self.parents = data['parent_id']
        self.family = data['family_id']
        self.birth = data['birth']

    def __str__(self):
        output  = f'id        : {self.id}\n'
        output += f'name      : {self.name}\n'
        output += f'birth     : {self.birth}\n'
        output += f'parent id : {self.parents}\n'
        output += f'family id : {self.family}\n'
        return output

# ----------------------------------------------------------------------------
class Family:

    def __init__(self, id, data):
        super().__init__()
        self.id = data['id']
        self.husband = data['husband_id']
        self.wife = data['wife_id']
        self.children = data['children']

    def children_count(self):
        return len(self.children)

    def __str__(self):
        output  = f'id         : {self.id}\n'
        output += f'husband    : {self.husband}\n'
        output += f'wife       : {self.wife}\n'
        for id in self.children:
            output += f'  Child    : {id}\n'
        return output

# -----------------------------------------------------------------------------
class Tree:

    def __init__(self, start_family_id):
        super().__init__()
        self.people = {}
        self.families = {}
        self.start_family_id = start_family_id

    def add_person(self, person):
        if self.does_person_exist(person.id):
            print(f'ERROR: Person with ID = {person.id} Already exists in the tree')
        else:
            self.people[person.id] = person

    def add_family(self, family):
        if self.does_family_exist(family.id):
            print(f'ERROR: Family with ID = {family.id} Already exists in the tree')
        else:
            self.families[family.id] = family

    def get_person(self, id):
        if id in self.people:
            return self.people[id]
        else:
            return None

    def get_family(self, id):
        if id in self.families:
            return self.families[id]
        else:
            return None

    def get_person_count(self):
        return len(self.people)

    def get_family_count(self):
        return len(self.families)

    def does_person_exist(self, id):
        return id in self.people

    def does_family_exist(self, id):
        return id in self.families

    def display(self, log):
        log.write('Tree Display')
        for family_id in self.families:
            fam = self.families[family_id]

            log.write(f'Family id: {family_id}')

            # Husband
            husband = self.get_person(fam.husband)
            if husband == None:
                log.write(f'  Husband: None')
            else:
                log.write(f'  Husband: {husband.name}, {husband.birth}')

            # wife
            wife = self.get_person(fam.wife)
            if wife == None:
                log.write(f'  Wife: None')
            else:
                log.write(f'  Wife: {wife.name}, {wife.birth}')

            # Parents of Husband
            if husband == None:
                log.write(f'  Husband Parents: None')
            else:
                parent_fam_id = husband.parents
                if parent_fam_id in self.families:
                    parent_fam = self.get_family(parent_fam_id)
                    father = self.get_person(parent_fam.husband)
                    mother = self.get_person(parent_fam.wife)
                    log.write(f'  Husband Parents: {father.name} and {mother.name}')
                else:
                    log.write(f'  Husband Parents: None')

            # Parents of Wife
            if wife == None:
                log.write(f'  Wife Parents: None')
            else:
                parent_fam_id = wife.parents
                if parent_fam_id in self.families:
                    parent_fam = self.get_family(parent_fam_id)
                    father = self.get_person(parent_fam.husband)
                    mother = self.get_person(parent_fam.wife)
                    log.write(f'  Wife Parents: {father.name} and {mother.name}')
                else:
                    log.write(f'  Wife Parents: None')

            # children
            output = []
            for index, child_id in enumerate(fam.children):
                person = self.people[child_id]
                output.append(f'{person.name}')
            out_str = str(output).replace("'", '', 100)
            log.write(f'  Children: {out_str[1:-1]}')

    def _test_number_connected_to_start(self):
        # start with first family, how many connected to that family
        inds_seen = set()

        def _recurive(family_id):
            nonlocal inds_seen
            if family_id in self.families:
                # count people in this family
                fam = self.families[family_id]

                husband = self.get_person(fam.husband)
                if husband != None:
                    if husband.id not in inds_seen:
                        inds_seen.add(husband.id)
                    _recurive(husband.parents)

                wife = self.get_person(fam.wife)
                if wife != None:
                    if wife.id not in inds_seen:
                        inds_seen.add(wife.id)
                    _recurive(wife.parents)

                for child_id in fam.children:
                    if child_id not in inds_seen:
                        inds_seen.add(child_id)

        _recurive(self.start_family_id)
        return len(inds_seen)

    def _count_generations(self, family_id):
        max_gen = -1

        def _recurive_gen(id, gen):
            nonlocal max_gen
            if id in self.families:
                if max_gen < gen:
                    max_gen = gen

                fam = self.families[id]

                husband = self.get_person(fam.husband)
                if husband != None:
                    _recurive_gen(husband.parents, gen + 1)

                wife = self.get_person(fam.wife)
                if wife != None:
                    _recurive_gen(wife.parents, gen + 1)

        _recurive_gen(family_id, 0)
        return max_gen + 1

    def __str__(self):
        out = '\nTree Stats:\n'
        out += f'Number of people                    : {len(self.people)}\n'
        out += f'Number of families                  : {len(self.families)}\n'
        out += f'Max generations                     : {self._count_generations(self.start_family_id)}\n'
        out += f'People connected to starting family : {self._test_number_connected_to_start()}\n'
        return out

# ----------------------------------------------------------------------------
# Do not change
class Request_thread(threading.Thread):

    def __init__(self, url):
        # Call the Thread class's init function
        threading.Thread.__init__(self)
        self.url = url
        self.response = {}

    def run(self):
        response = requests.get(self.url)
        # Check the status code to see if the request succeeded.
        if response.status_code == 200:
            self.response = response.json()
        else:
            print('RESPONSE = ', response.status_code)

# -----------------------------------------------------------------------------
# Change this function to speed it up
def depth_fs_pedigree(family_id, tree):
    if family_id == None:
        return

    print(f'Retrieving Family: {family_id}')

    req_family = Request_thread(f'{TOP_API_URL}/family/{family_id}')
    req_family.start()
    req_family.join()

    new_family = Family(family_id, req_family.response)
    tree.add_family(new_family)

    # Get husband details
    husband_id, wife_id, children_ids = new_family.husband, new_family.wife, [c for c in new_family.children if not tree.does_person_exist(c)]
    print(f'   Retrieving Husband : {husband_id}')
    print(f'   Retrieving Wife    : {wife_id}')
    print(f'   Retrieving children: {str(children_ids)[1:-1]}')
    req_parents = [Request_thread(f'{TOP_API_URL}/person/{id}') for id in [husband_id, wife_id]]
    [t.start() for t in req_parents]
    [t.join() for t in req_parents]
    parents = [Person(r.response) for r in req_parents]

    family_threads = [threading.Thread(target=depth_fs_pedigree, args=(p.parents, tree)) for p in parents if p is not None]
    req_children = [Request_thread(f'{TOP_API_URL}/person/{id}') for id in children_ids]

    [t.start() for t in req_children]
    [tree.add_person(person) for person in parents]
    [thread.start() for thread in family_threads]
    [t.join() for t in req_children]
    for person in req_children:
        if person is not None:
            tree.add_person(Person(person.response))
    [thread.join() for thread in family_threads]

# -----------------------------------------------------------------------------
# You should not change this function
def part1(log, start_id, generations):
    tree = Tree(start_id)

    req = Request_thread(f'{TOP_API_URL}/start/{generations}')
    req.start()
    req.join()

    log.start_timer('Depth-First')
    depth_fs_pedigree(start_id, tree)
    total_time = log.stop_timer()

    req = Request_thread(f'{TOP_API_URL}/end')
    req.start()
    req.join()

    tree.display(log)
    log.write(tree)
    log.write(f'total_time                   : {total_time}')
    log.write(f'People and families / second : {(tree.get_person_count()  + tree.get_family_count()) / total_time}')
    log.write('')

# -----------------------------------------------------------------------------
def breadth_fs_pedigree(start_id, tree):
    #      - implement breadth first retrieval
    #      - Limit number of concurrent connections to the FS server to 5
    # This video might help understand BFS
    # https://www.youtube.com/watch?v=86g8jAQug04

    req_sem = threading.Semaphore(5)
    #tree_lock = threading.Lock()
    #Used internally.
    def get_family(family_id):
        """
        Fetches the family from id.
        Add family to tree.
        Put parents in current_parent_id_list
        put children in current_child_id_list
        """
        req_family = Request_thread(f'{TOP_API_URL}/family/{family_id}')
        with req_sem:
            #We are in a helper thread, so fetch without making a new thread.
            req_family.run()
        new_family = Family(family_id, req_family.response)
        #with tree_lock:
        tree.add_family(new_family)
        parents_ids = [new_family.husband, new_family.wife]
        current_parent_id_list.extend(parents_ids)
        children_ids = [c for c in new_family.children if not tree.does_person_exist(c)]
        current_child_id_list.extend(children_ids)

    def get_parent(id):
        """
        Fetch the person of the given id.
        Append the result's parents' family id to next_family_id_list
        Return the result person
        """
        req_person = Request_thread(f'{TOP_API_URL}/person/{id}')
        with req_sem:
            req_person.run()
        new_person = Person(req_person.response)
        if new_person != None:
            #with tree_lock:
            tree.add_person(new_person)
            return new_person.parents

    def get_child(id):
        """
        Fetch the person of the given id.
        Return the result person.
        """
        get_parent(id)

    current_family_id_list = [start_id]
    next_family_id_list = []
    while len(current_family_id_list) !=  0:
        current_parent_id_list = []
        current_child_id_list = []
        with ThreadPool(10) as pool:
            # get family and collect parents, children
            pool.map(get_family, current_family_id_list)
            print("got all the family pool")
            print(f"parents: {current_parent_id_list}")
            print(f"children: {current_child_id_list}")
            # get parents and collect people, next generation family ids
            next_family_id_list = pool.map(get_parent, current_parent_id_list)
            print(f"next family id list: {next_family_id_list}")
            # get children and collect people
            pool.map(get_child, current_child_id_list)
        current_family_id_list = [id for id in next_family_id_list if id is not None]
        next_family_id_list = []

# -----------------------------------------------------------------------------
# You should not change this function
def part2(log, start_id, generations):
    tree = Tree(start_id)

    req = Request_thread(f'{TOP_API_URL}/start/{generations}')
    req.start()
    req.join()

    log.start_timer('Breadth-First')
    breadth_fs_pedigree(start_id, tree)
    total_time = log.stop_timer()

    req = Request_thread(f'{TOP_API_URL}/end')
    req.start()
    req.join()

    tree.display(log)
    log.write(tree)
    log.write(f'total_time      : {total_time}')
    log.write(f'People / second : {tree.get_person_count() / total_time}')
    log.write('')

# -----------------------------------------------------------------------------
def main():
    log = Log(show_terminal=True, filename_log='assignment.log')

    # starting family
    req = Request_thread(TOP_API_URL)
    req.start()
    req.join()

    print(f'Starting Family id: {req.response["start_family_id"]}')
    start_id = req.response['start_family_id']

    part1(log, start_id, 6)

    part2(log, start_id, 6)

if __name__ == '__main__':
    main()


import cv2
import sys
import matplotlib.pyplot as plt

argvs = sys.argv
if (len(argvs) != 2):
	print('Usage: $ python %s filename' % argvs[0])
	exit()

imagefilename = argvs[1]
try:
	img = cv2.imread(imagefilename, 1)
except:
	print('faild to load %s' % imagefilename)
	exit()

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

#HAAR分類器の顔検出用の特徴量
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"
cascade_path = "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml"
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml"
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt_tree.xml"

color = (255, 255, 255) #白
#color = (0, 0, 0) #黒

#グレースケール変換
image_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#カスケード分類器の特徴量を取得する
cascade = cv2.CascadeClassifier(cascade_path)

#物体認識（顔認識）の実行
#image – CV_8U 型の行列．ここに格納されている画像中から物体が検出されます
#objects – 矩形を要素とするベクトル．それぞれの矩形は，検出した物体を含みます
#scaleFactor – 各画像スケールにおける縮小量を表します
#minNeighbors – 物体候補となる矩形は，最低でもこの数だけの近傍矩形を含む必要があります
#flags – このパラメータは，新しいカスケードでは利用されません．古いカスケードに対しては，cvHaarDetectObjects 関数の場合と同じ意味を持ちます
#minSize – 物体が取り得る最小サイズ．これよりも小さい物体は無視されます
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
#facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)

print("face rectangle")
print(facerect)

if len(facerect) > 0:
    #検出した顔を囲む矩形の作成
        for rect in facerect:
                cv2.rectangle(img, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=2)

    #認識結果の表示
        plt.imshow(img)
        plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
        plt.show()