import pytest
from pytest_mongodb.plugin import mongomock
from pytest_mongodb.plugin import pymongo
from wataguess.db import db

config = {
    "db_user": "test",
    "db_pass": "test",
    "db_host": "127.0.0.1",
    "db_port": 27017,
}

class Testdb:
    @pytest.fixture
    def db(self):
        watadb = db(config)
        return watadb

    def test_init(self, db):
        assert db.user == config["db_user"]
        assert db.password == config["db_pass"]
        assert db.host == config["db_host"]
        assert db.port == config["db_port"]
        assert isinstance(db.client, pymongo.mongo_client.MongoClient)
        assert isinstance(db.db, pymongo.database.Database)

    def test_connection(self, db):
        db.connection()
        assert isinstance(db.client, pymongo.mongo_client.MongoClient)

    def test_dbinit(self, db):
        db.dbinit()
        assert isinstance(db.db, pymongo.database.Database)

    def test_collection_presence(self, mongodb):
        assert "items" in mongodb.list_collection_names()
        assert "score" in mongodb.list_collection_names()

    def test_score_update(self, db, mongodb):
        db.db = mongodb
        user = 989767856876
        score = 500
        user1 = db.update_score(user, score)
        query = {"uuid": user}
        assert user1 == 500
        test_user = mongodb.score.find_one(query)
        assert test_user["score"] == score
        assert test_user["uuid"] == user

    def test_score_retrieval(self, db, mongodb):
        db.db = mongodb
        user1 = 7342754
        user2 = 93452345
        user3 = 832435632456
        user4 = 3246584567

        score1 = db.retrieve_score(user1)
        score2 = db.retrieve_score(user2)
        score3 = db.retrieve_score(user3)
        score4 = db.retrieve_score(user4)

        assert isinstance(score1, int)
        assert isinstance(score2, int)
        assert isinstance(score3, int)
        assert isinstance(score4, int)

        assert score1 == 500
        assert score2 == 420
        assert score3 == 999999
        assert score4 == 0

        query1 = {"uuid": user1}
        query2 = {"uuid": user2}
        query3 = {"uuid": user3}

        assert score1 == mongodb.score.find_one(query1)["score"]
        assert score2 == mongodb.score.find_one(query2)["score"]
        assert score3 == mongodb.score.find_one(query3)["score"]

    def test_item_update(self, db, mongodb):
        db.db = mongodb
        item1 = {"name": "this is a name", "price": 55.50, "uid": 542345}
        item2 = {"name": "pokemon box ruby sapphire", "price": 2000, "uid": 2354323}
        item3 = {"name": "mario", "price": 1000000, "uid": 9587643}

        assert db.update_item(item1["name"], item1["price"], item1["uid"])
        assert db.update_item(item2["name"], item2["price"], item2["uid"])
        assert db.update_item(item3["name"], item3["price"], item3["uid"])

        query1 = {"uid": item1["uid"]}
        query2 = {"uid": item2["uid"]}
        query3 = {"uid": item3["uid"]}

        test_item1 = mongodb.items.find_one(query1)
        test_item2 = mongodb.items.find_one(query2)
        test_item3 = mongodb.items.find_one(query3)

        assert item1.items() <= test_item1.items()
        assert item2.items() <= test_item2.items()
        assert item3.items() <= test_item3.items()

    def test_item_retrival(self, db, mongodb):
        db.db = mongodb
        item1 = 231341245
        item2 = 4598623457
        item3 = 354265423546

        test_item1 = db.retrieve_item(item1)
        test_item2 = db.retrieve_item(item2)
        test_item3 = db.retrieve_item(item3)

        assert isinstance(test_item1, dict)
        assert isinstance(test_item2, dict)
        assert test_item3 == None

        assert test_item1["uid"] == item1
        assert test_item1["price"] == 420.69
        assert test_item1["name"] == "pokemon box"
        assert test_item2["uid"] == item2
        assert test_item2["price"] == 65
        assert test_item2["name"] == "ska vinyl"

        query1 = {"uid": item1}
        query2 = {"uid": item2}

        assert test_item1.items() <= mongodb.items.find_one(query1).items()
        assert test_item2.items() <= mongodb.items.find_one(query2).items()

    def test_recent_item_retrival(self, db, mongodb):
        db.db = mongodb

        test_item1 = db.retrieve_recent_item()

        assert (
            test_item1.items()
            <= mongodb.items.find().sort("_id", -1).limit(1)[0].items()
        )

        item2 = {"name": "this is a name", "price": 55.50, "uid": 542345}

        assert db.update_item(item2["name"], item2["price"], item2["uid"])

        query2 = {"uid": item2["uid"]}

        assert mongodb.items.find_one(query2)

        test_item2 = db.retrieve_recent_item()

        assert item2.items() <= test_item2.items()

    def test_every_item_retrival(self, db, mongodb):
        db.db = mongodb

        items = mongodb.items.find()

        all_items = db.retrieve_all_items()

        for i in [0, 1]:
            assert items[i].items() <= all_items[i].items()

    def test_retrieve_top_scores(self, db, mongodb):
        db.db = mongodb

        users = mongodb.score.find().sort("score", -1).limit(5)

        top_users = db.retrieve_top_scores()

        for i in [0, 1, 2]:
            assert users[i].items() <= top_users[i].items()


import torch
import torch.nn as nn
import torch.nn.functional as F
import os

class Actor(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(Actor, self).__init__()
        self.linear1 = nn.Linear(input_size, hidden_size)
        self.linear2 = nn.Linear(hidden_size, hidden_size)
        self.linear3 = nn.Linear(hidden_size, output_size)

    def forward(self, s):
        x = F.relu(self.linear1(s))
        x = F.relu(self.linear2(x))
        x = torch.tanh(self.linear3(x))

        return x

class Critic(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super().__init__()
        self.linear1 = nn.Linear(input_size, hidden_size)
        self.linear2 = nn.Linear(hidden_size, hidden_size)
        self.linear3 = nn.Linear(hidden_size, output_size)

    def forward(self, s, a):
        x = torch.cat([s, a], 1)
        x = F.relu(self.linear1(x))
        x = F.relu(self.linear2(x))
        x = self.linear3(x)

        return x

class Agent(object):
    def __init__(self, action_dim, state_dim):
        self.actor = Actor(state_dim, 256, action_dim)

    def act(self, s0):
        s0 = torch.tensor(s0, dtype=torch.float).unsqueeze(0)
        a0 = self.actor(s0).squeeze(0).detach().numpy()
        return a0

action_dim = 1
state_dim = 3
agent = Agent(action_dim, state_dim)
policy_net = os.path.dirname(os.path.abspath(__file__)) + '/actor_net.pth'
agent.actor.load_state_dict(torch.load(policy_net))

def my_controller(obs_list, action_space_list, obs_space_list):
    action = agent.act(obs_list[0])
    return [action]


""" Launcher functionality for the Azure Resource Manager (ARM)
"""
import logging
import os

import dcos_launch.platforms.arm
import dcos_launch.util

log = logging.getLogger(__name__)

class AzureResourceGroupLauncher(dcos_launch.util.AbstractLauncher):
    def __init__(self, config: dict, env=None):
        if env:
            os.environ.update(env)
        self.azure_wrapper = dcos_launch.platforms.arm.AzureWrapper(
            config['azure_location'],
            dcos_launch.util.set_from_env('AZURE_SUBSCRIPTION_ID'),
            dcos_launch.util.set_from_env('AZURE_CLIENT_ID'),
            dcos_launch.util.set_from_env('AZURE_CLIENT_SECRET'),
            dcos_launch.util.set_from_env('AZURE_TENANT_ID'))
        self.config = config
        log.debug('Using Azure Resource Group Launcher')

    def create(self):
        self.key_helper()
        self.azure_wrapper.deploy_template_to_new_resource_group(
            self.config['template_url'],
            self.config['deployment_name'],
            self.config['template_parameters'],
            self.config.get('tags'))
        return self.config

    def wait(self):
        self.resource_group.wait_for_deployment()

    def describe(self):
        return {
            'masters': dcos_launch.util.convert_host_list(self.resource_group.get_master_ips()),
            'private_agents': dcos_launch.util.convert_host_list(self.resource_group.get_private_agent_ips()),
            'public_agents': dcos_launch.util.convert_host_list(self.resource_group.get_public_agent_ips()),
            'master_fqdn': self.resource_group.public_master_lb_fqdn,
            'public_agent_fqdn': self.resource_group.public_agent_lb_fqdn}

    def delete(self):
        self.resource_group.delete()

    def key_helper(self):
        """ Adds private key to the config and injects the public key into
        the template parameters
        """
        if not self.config['key_helper']:
            return
        if 'sshRSAPublicKey' in self.config['template_parameters']:
            raise dcos_launch.util.LauncherError('KeyHelperError', 'key_helper will automatically'
                                                 'calculate and inject sshRSAPublicKey; do not set this parameter')
        private_key, public_key = dcos_launch.util.generate_rsa_keypair()
        self.config.update({'ssh_private_key': private_key.decode()})
        self.config['template_parameters'].update({'sshRSAPublicKey': public_key.decode()})

    @property
    def resource_group(self):
        try:
            return dcos_launch.platforms.arm.DcosAzureResourceGroup(self.config['deployment_name'], self.azure_wrapper)
        except Exception as ex:
            raise dcos_launch.util.LauncherError('GroupNotFound', None) from ex


import random
import os
import requests
import re
import time
import subprocess
import signal
import sys
from bs4 import BeautifulSoup

instrumental = "https://invidio.us/search?q=touhou%20instrumental"
bgm = "https://invidio.us/search?q=touhou+bgm"
classical = "https://invidio.us/search?q=touhou+classical"

def changePage(url, page):
    url = re.sub(r'&page=\d*', '', url)
    url = url + "&page=" + str(page)
    return url

def getUpperBound(session, url, lowerBound):
    url = changePage(url, lowerBound)
    nextUrl = changePage(url, lowerBound+1)
    nextSite = session.get(nextUrl)
    nextSoup = BeautifulSoup(nextSite.text, 'html.parser')
    match = nextSoup.findAll('div', class_='pure-u-1 pure-u-md-1-4')
    if match != []:
        return getUpperBound(session, url, lowerBound*4+1)
    else:
        return lowerBound+1

def getHighestPage(session, url, lowerBound, upperBound):
    print("Getting middle page")
    middlePage = int((lowerBound+upperBound)/2)
    middleUrl = changePage(url, middlePage)
    middleSite = session.get(middleUrl)
    middleSoup = BeautifulSoup(middleSite.text, 'html.parser')
    match = middleSoup.findAll('div', class_='pure-u-1 pure-u-md-1-4')
    if match != []:
        print("Not match")
        nextUrl = changePage(url, middlePage+1)
        nextSite = session.get(nextUrl)
        nextSoup = BeautifulSoup(nextSite.text, 'html.parser')
        match = nextSoup.findAll('div', class_='pure-u-1 pure-u-md-1-4')
        if match != []:
            return getHighestPage(session, url, middlePage+1, upperBound)
        else:
            return middlePage
    else:
        print("Match")
        previousUrl = changePage(url, middlePage-1)
        previousSite = session.get(previousUrl)
        previousSoup = BeautifulSoup(previousSite.text, 'html.parser')
        match = previousSoup.findAll('div', class_='pure-u-1 pure-u-md-1-4')
        if match != []:
            return middlePage-1
        else:
            return getHighestPage(session, url, lowerBound, middlePage-1)

def getListOfHrefs(session, url, page):
    url = changePage(url, page)
    site = session.get(url)
    soup = BeautifulSoup(site.text, 'html.parser')
    temp = soup.findAll('div', class_='pure-u-1 pure-u-md-1-4')
    a = []
    titles = []
    for element in temp:
        a.append((element.find('a')['href'], element.find('p', class_=False).text))
    firstBannedExpression = r"playlist"
    secondBannedExpression = r"vocal|subbed|subs|sub|cover|lyrics|Touhou Sky Arena Matsuri Climax Music Extended|Smooth McGroove"
    listOfHrefs = []
    for element in a:
       firstMatch = re.search(firstBannedExpression, element[0])
       secondMatch = re.search(secondBannedExpression, element[1], re.IGNORECASE)
       if not firstMatch and not secondMatch:
           listOfHrefs.append(element)
    return listOfHrefs

def getListOfVideos(listOfHrefs):
    listOfVideos = []
    for element in listOfHrefs:
        listOfVideos.append(("https://youtube.com" + element[0], element[1]))
    return listOfVideos

def interrupt(signum, frame):
    raise Exception("")

def getChoice(prompt):
    choice = ""
    timeout = 20
    #signal.alarm(timeout)
    signal.signal(signal.SIGALRM, interrupt)
    signal.alarm(timeout) # sets timeout
    while(True):
        try:
            choice = input(prompt)
        except:
            return choice
        if choice == "1" or choice == "2" or choice == "3" or choice == "Q":
            signal.alarm(0)
            return choice

links = [instrumental, bgm, classical]
mainTheme = random.randint(0,2)

def score(title):
    score = 0
    f = open(sys.argv[1], "r")
    for line in f:
        line_divided = line.split(',', maxsplit=1)
        line_score = int(line_divided[0])
        line_text = line_divided[1][:-1] #remove the trailing \n
        match = re.search(line_text, title, re.IGNORECASE)
        if match:
            score+=line_score
    f.close()
    return score

def getFilterInput(message, title):
    while True:
        user_input = input(message)
        match = re.search(r".*\,.*\d", user_input)
        if not match and user_input != "A":
            print("Wrongly formatted input, try again.\n")
            continue
        if user_input == "A":
            user_input = title
            user_score = input("Enter the score: ")
            try:
                int(user_score)
            except ValueError:
                print("Wrongly formatted score, try again.\n")
                continue
            user_input = re.sub('\|\||\&\&|\&|\|', '', user_input)
            return str(user_score)  + ',' + user_input + '\n'
        return user_input + '\n'

def addFilter(title):
    f = open(sys.argv[1], "a")
    message = "Enter the filter, separated by a , from the score. Negative score needs - prefix; eg. niggy, -50. Escape commas with '\\'. To add the entire title to filters, enter 'A'\n"
    f.write(getFilterInput(message, title))

session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'})

print("Getting upper bound page...")
upperBound = getUpperBound(session, instrumental, 1)
print("Getting highest page...")
highestPage = getHighestPage(session, links[mainTheme], upperBound/4-1, upperBound)
print("Getting random page...")
randomPage = random.randint(1,highestPage)
print("Getting list of videos...")
listOfVideos = getListOfVideos(getListOfHrefs(session, links[mainTheme], randomPage))
while True:
    print("Entering loop")
    for element in listOfVideos:
        print(element)
        if len(sys.argv) > 1:
            points = score(element[1])
            print("Points: " + str(points))
            if points <= 0:
                print("Points below or equal zero, skipping")
                continue
        print("Setting title")
        title = element[1]
        print("Setting address")
        address = element[0]
        print(title)
        print(address)
        if len(sys.argv) > 1:
            print("Touhou-ness score: " + str(points))
        subprocess.call(["mpv", "--no-video", "--af=scaletempo=speed=both", "--audio-pitch-correction=no", "--ytdl-format=bestaudio", element[0]])
        choice = getChoice("Press 1 to download the file, press 2 to go to the next, press 3 to add a new filter, press Q to quit.\n")
        if choice == "1":
            subprocess.call(["youtube-dl", "-x", "-o", "'%(title)s.%(ext)s'", "--audio-quality", "0", "--audio-format", "flac", element[0]])
        elif choice == "2":
            pass
        elif choice == "3":
            addFilter(title)
        elif choice == "Q":
            sys.exit()
        else:
            pass
    randomPage = random.randint(1, int(highestPage))
    listOfVideos = getListOfVideos(getListOfHrefs(session, links[mainTheme], randomPage))


# coding=utf-8

import torch.nn
import torch.nn.functional
import torch.autograd
import torch.tensor
import torch.optim
import torchvision
import torch.utils.data
import torchvision.transforms

from visdom import Visdom
# from graphviz import Digraph

import math
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image,PIL.ImageEnhance
import os
import time
import json
import collections
import random
import cv2

def get_img(imgx):

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    mean = np.array(mean,dtype=np.float32)
    mean = mean.reshape([3,1,1])

    std = np.array(std,dtype=np.float32)
    std = std.reshape([3,1,1])

    img = imgx.copy()
    img = img*std
    img = img+mean

    img=np.transpose(img,[1,2,0])
    # print(img.shape,type(img))

    img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
    # print(img.shape)
    return img

def show(img,gts):
    W=img.shape[1]
    H=img.shape[0]

    for i,gt in enumerate(gts):

        pt1_x = int(W*(gt[0]-0.5*gt[2]))
        pt1_y = int(H*(gt[1]-0.5*gt[3]))
        pt2_x = int(W*(gt[0]+0.5*gt[2]))
        pt2_y = int(H*(gt[1]+0.5*gt[3]))

    #     print(pt1_x,pt1_y,pt2_x,pt2_y)
    #     img = np.array(img)
    #
    #     print('img type: ',type(img),img.shape)
    #     cv2.imshow('img',img)
    #     cv2.waitKey(0)
        cv2.rectangle(img,pt1=(pt1_x,pt1_y),pt2=(pt2_x,pt2_y),color=(0,255,0),thickness=2)

    cv2.imshow('img',img)
    cv2.waitKey(0)

def IOU(Reframe,GTframe):
    """
    自定义函数，计算两矩形 IOU，传入为均为矩形对角线，（x,y）  坐标。·
    """
    x1 = Reframe[0]
    y1 = Reframe[1]
    width1 = Reframe[2]-Reframe[0]
    height1 = Reframe[3]-Reframe[1]

    x2 = GTframe[0]
    y2 = GTframe[1]
    width2 = GTframe[2]-GTframe[0]
    height2 = GTframe[3]-GTframe[1]

    endx = max(x1+width1,x2+width2)
    startx = min(x1,x2)
    width = width1+width2-(endx-startx)

    endy = max(y1+height1,y2+height2)
    starty = min(y1,y2)
    height = height1+height2-(endy-starty)

    if width <=0 or height <= 0:
        ratio = 0 # 重叠率为 0
    else:
        Area = width*height # 两矩形相交面积
        Area1 = width1*height1
        Area2 = width2*height2
        ratio = Area*1./(Area1+Area2-Area)
    # return IOU
    return ratio

# box  ndarray [n,5] [x1 y1 x2 y2 conf]
def nms(box,threshold=0.5):
    if box.shape[0] == 1:
        return box
    else:
        sort_arg = np.argsort(box[:,4]) #按第'4'列排序
        box = box[sort_arg]
        box = np.flip(box,axis=0)

        # 初步筛选
        k1=0.4
        sort_a = box[ box[:,4]>k1,... ]

        if sort_a.shape[0]> 0:
            #NMS
            k2=threshold
            pro_filter=[1]*sort_a.shape[0]

            for i in range(sort_a.shape[0]):
                if pro_filter[i] == 0:
                    continue
                else:
                    for j in range(i+1,sort_a.shape[0]):
                        intersect = IOU(sort_a[i,0:4],sort_a[j,0:4])

                        if intersect > k2:
                            pro_filter[j]=0

            filtered_box = []
            for i in range( sort_a.shape[0] ):
                if pro_filter[i] is not 0:
                    filtered_box.append(sort_a[i])

            return np.array(filtered_box)

        else:
            return None

# result ndarray [n,4]
# gt ndarray [g,4]
def eval_img(result,gt,threshold):
    tp=0
    fp=0
    fn=0

    if result is None:
        tp=0
        fp=0
        fn=gt.shape[0]
        recall=0
        percision=0
        # return tp,fp,fn
    else:
        result=result[0:4]
        gt=gt[0:4]
        for i in range(gt.shape[0]):
            for j in range(result.shape[0]):
                if IOU(gt[i],result[j])>=threshold:
                    tp+=1
                    break

        percision=tp/1./result.shape[0]
        recall=tp/1./gt.shape[0]

    return recall,percision

if __name__=="__main__":
    # a=[0.075,0.345,0.915,0.784]
    # b=[0.0847,0.335,0.922,0.776]
    #
    # c=IOU(b,a)
    #
    # print(c)
    a=np.array([[1,2,3,4,0],[0,1,2,3,2],[3,4,5,6,1],[4,5,6,7,4],[5,5,5,5,7]])
    x=nms(a)

    print(x)


#calss header
class _CAIRN():
	def __init__(self,):
		self.name = "CAIRN"
		self.definitions = [u'a small pile of stones made, especially on mountains, to mark a place or as a memorial (= an object to make people remember someone or something)']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


# Plotting processed DOS from pdos.py script
# Only works for files with spin
# Author: Razak Elmaslmane
# Email: arem502@york.ac.uk

import numpy as np
import matplotlib.pyplot as plt
import pickle, os

# Location of processed pickle files
plot_dir  = str("./outputs")

# Should the program show a plot in this run:
should_plot = True

# Should the program save figs:
should_save = False

def main():
	# Get project and file names:
	project_name, alpha_file_list, beta_file_list = get_filenames()

	"""
	The following bit loads in all alpha and beta files into two lists:

	Structure of these lists is as follows:
	1. The first index is the file, e.g. alpha_spins[0] is file 0 for spin alpha
	2. Inside this index is a numpy array, where the first column is energy, second is total DOS
		e.g. alpha_spins[0][:,0] and alpha_spins[0][:,1] are the energy and total DOS for file 0
	"""
	alpha_spins = spin_loader(alpha_file_list, project_name, "ALPHA")
	beta_spins  = spin_loader(beta_file_list, project_name, "BETA")

	# --------------------- The plotting bit  ---------------------

	if (should_plot):
		# Font size on figure:
		fontsize = 12

		# Transparency:
		trans = 0.9

		# Pre-amble to render figure text using latex:
		plt.rc('text', usetex=True)
		plt.rc('font', family='serif')

		# Set tick labels to equal font size:
		plt.rc('xtick', labelsize=fontsize)
		plt.rc('ytick', labelsize=fontsize)

		#Grain boundary gb:
		fig = plt.figure(figsize=(6,5))

		# x-axis limits:
		plt.xlim(-7.5,10)

		# y-axis limits:
		#plt.ylim(-ylim,ylim)

		# Don't show a grid on figure:
		plt.grid(False)

		# Kind 1:
		# Plot energy verses alpha and beta spins, set beta spin to negative y:
		plt.plot(alpha_spins[0][:,0], alpha_spins[0][:,1], color='k', linewidth=1.5, alpha=trans, label=r'Kind 1 total DOS')
		plt.plot(beta_spins[0][:,0], beta_spins[0][:,1] * -1, color='k', linewidth=1.5, alpha=trans)

		# Kind 2:
		# Plot energy verses alpha and beta spins, set beta spin to negative y:
		plt.plot(alpha_spins[1][:,0], alpha_spins[1][:,1], '-.', color='k', linewidth=1.5, alpha=trans, label=r'Kind 2 total DOS')
		plt.plot(beta_spins[1][:,0], beta_spins[1][:,1] * -1, '-.', color='k', linewidth=1.5, alpha=trans)

		# Axis labels:
		plt.xlabel('Energy (e.V.)', fontsize=fontsize)
		plt.ylabel('Density of States (arb. units)', fontsize=fontsize)

		# Show legend, no box:
		plt.legend(fontsize=fontsize-1, frameon=False)

		# Show minor tick marks, set direction in:
		plt.minorticks_on()
		plt.tick_params(axis='both',direction='in',which='both', right='on', top='on', labelsize=fontsize)

		if should_save:
			# Make a directory for output graphs and save figure there
			if not os.path.isdir("./graphs"): os.mkdir('./graphs')
			plt.savefig("graphs/graph.pdf", bbox_inches='tight')

		plt.show()

def get_filenames():
	# Get files in directory
	ls = sorted(os.listdir(plot_dir))
	# Seperate file name strings by _
	temp = [item.split("_") for item in ls]
	# If its got ALPHA in the name it's an alpha spin:
	alpha_file_list = [item[1] for item in temp if "ALPHA" in item[0]]
	# If its got BETA in the name it's an alpha spin:
	beta_file_list  = [item[1] for item in temp  if "BETA"  in item[0]]
	# Get project name:
	project_name = temp[0][0].split("-")[0]

	# Prints project name from files:
	print("-"*50)
	print ( f"Project name: {project_name}" )
	print("-"*50)

	return project_name, alpha_file_list, beta_file_list

def spin_loader(spin_file_list, project_name, spin_channel):
	name_preamble = f"{project_name}-SPIN_END"
	spin_dos = [None]*len(spin_file_list)

	for index, item in enumerate(spin_file_list):
		name = name_preamble.replace("SPIN", spin_channel)
		name = name.replace("END", item)
		loaded_dos = pickle.load(open( f"{plot_dir}/{name}", 'rb'))
		spin_dos[index] = pickle.load(open( f"{plot_dir}/{name}", 'rb'))
		print( f"{spin_channel.lower()} file {index} = {name}" )
	print("-"*50)
	return spin_dos

if __name__ == '__main__':
    main()


#!/user/bin/python
# -*- coding: utf-8 -*-
#
# @ Author: Yao Shuang-Long
# @ Date: 2020/11/26 17:52:08
# @ Summary: the summary.
# @ Contact: xxxxxxxx@email.com
# @ Paper Link:
#

import os
import pickle as pkl
import sys

import numpy as np

from ..data import load_from_csv, load_from_text
from ..data import build_graph
from ..data import get_all_triples

from ..data.data_utils import get_from_aigraph_url, download
from ..data.data_utils import extract_archive, clean_data
from ..data.data_utils import  load_and_check_original_data
from ..data.data_utils import str_to_idx, get_select_src_rate
from ..data.data_utils import pprint, set_download_dir

KG_BENCHMARK_DATASETS = ['WN18', 'WN18RR', 'FB15k', 'FB15k-237']

def _load_data(data_name, data_sha1, path=None, original=False):
    url = get_from_aigraph_url(data_name)
    path = set_download_dir(path)
    data_name = data_name.lower()
    data_name = 'wn18RR' if data_name == 'wn18rr' else data_name

    print(url)
    print(path)
    print(data_name)

    taz_path = download(url, path)
    fdir = os.path.join(os.environ['KG_DIR'], data_name)

    extract_archive(taz_path, fdir)

    data = load_and_check_original_data(fdir, data_name, data_sha1)

    data = clean_data(data)

    data, original_data, total_entities, total_relations = str_to_idx(data)
    if original:
        return pprint(original_data, total_entities, total_relations, data_name)

    return pprint(data, total_entities, total_relations, data_name)

def load_fb15k(path=None, original=False):
    """Load the FB15k dataset

    .. warning::
        The dataset includes a large number of inverse relations that spilled to the test set, and its use in
        experiments has been deprecated. Use FB15k-237 instead.

    FB15k is a split of Freebase, first proposed by :cite:`bordes2013translating`.

    The FB15k dataset is loaded from file if it exists at the ``AMPLIGRAPH_DATA_HOME`` location.
    If ``AMPLIGRAPH_DATA_HOME`` is not set the the default  ``~/ampligraph_datasets`` is checked.

    If the dataset is not found at either location it is downloaded and placed in ``AMPLIGRAPH_DATA_HOME``
    or ``~/ampligraph_datasets``.

    The dataset is divided in three splits:

    - ``train``
    - ``valid``
    - ``test``

    ========= ========= ======= ======= ============ ===========
     Dataset  Train     Valid   Test    Entities     Relations
    ========= ========= ======= ======= ============ ===========
    FB15K     483,142   50,000  59,071  14,951        1,345
    ========= ========= ======= ======= ============ ===========
    Cleaned   483,142   50,000  59,071  14,951        1,345
    ========= ========= ======= ======= ============ ===========

    """
    data_name = 'FB15k'

    data_sha1 = {
        'train': '578bdb6b4311d22d4baf7da30aaadf03d687c84d',
        'test': '00d340728878df4f0b318fd1f488855e9b770425',
        'valid': '2694fe891109dea3470bd975dd55eeb12ef30cbd'
    }

    return _load_data(data_name, data_sha1, path, original)

def load_fb15k237(path=None, original=False):
    """Load the FB15k-237 dataset

    FB15k-237 is a reduced version of FB15K. It was first proposed by :cite:`toutanova2015representing`.

    The FB15k-237 dataset is loaded from file if it exists at the ``AMPLIGRAPH_DATA_HOME`` location.
    If ``AMPLIGRAPH_DATA_HOME`` is not set the the default  ``~/ampligraph_datasets`` is checked.

    If the dataset is not found at either location it is downloaded and placed in ``AMPLIGRAPH_DATA_HOME``
    or ``~/ampligraph_datasets``.

    The dataset is divided in three splits:

    - ``train``
    - ``valid``
    - ``test``

    ========= ========= ======= ======= ============ ===========
     Dataset  Train     Valid   Test    Entities     Relations
    ========= ========= ======= ======= ============ ===========
    FB15K-237 272,115   17,535  20,466  14,541        237
    ========= ========= ======= ======= ============ ===========
    Cleaned   272,115   17,516  20,438  14,505        237
    ========= ========= ======= ======= ============ ===========

    return data, total_entities, total_relations
    """

    data_name = 'FB15k-237'

    data_sha1 = {
        'train': '1448a31a528da315217960edeca97f68209f8254',
        'test': '263a7bd582cf1d27961fc4143ca2bee474bf03fc',
        'valid': '732eb032787161e61b7bcc7ab3965b7569b405ce'
    }

    return _load_data(data_name, data_sha1, path, original)

def load_wn18(path=None, original=False):
    """Load the WN18 dataset

    .. warning::
        The dataset includes a large number of inverse relations that spilled to the test set, and its use in
        experiments has been deprecated. Use WN18RR instead.

    WN18 is a subset of Wordnet. It was first presented by :cite:`bordes2013translating`.

    The WN18 dataset is loaded from file if it exists at the ``AMPLIGRAPH_DATA_HOME`` location.
    If ``AMPLIGRAPH_DATA_HOME`` is not set the the default  ``~/ampligraph_datasets`` is checked.

    If the dataset is not found at either location it is downloaded and placed in ``AMPLIGRAPH_DATA_HOME``
    or ``~/ampligraph_datasets``.

    The dataset is divided in three splits:

    - ``train``: 141,442 triples
    - ``valid`` 5,000 triples
    - ``test`` 5,000 triples

    ========= ========= ======= ======= ============ ===========
     Dataset  Train     Valid   Test    Entities     Relations
    ========= ========= ======= ======= ============ ===========
    WN18      141,442   5,000   5,000   40,943        18
    ========= ========= ======= ======= ============ ===========
    Cleaned   141,442   5,000   5,000   40,943        18
    ========= ========= ======= ======= ============ ===========

    return data, total_entities, total_relations
    """

    data_name = 'WN18'

    data_sha1 = {
        'train': 'b78e956440e0b7631517aa1b230818f581281e6d',
        'test': 'e5308598809646ad01da6b4d9cede189f918aa31',
        'valid': '2433f787c8dcf3ac376d70ebd358ef3998313ea3'
    }

    return _load_data(data_name, data_sha1, path, original)

def load_wn18rr(path=None, original=False):
    """Load the WN18RR dataset

    The dataset is described in :cite:`DettmersMS018`.

    The WN18RR dataset is loaded from file if it exists at the ``AMPLIGRAPH_DATA_HOME`` location.
    If ``AMPLIGRAPH_DATA_HOME`` is not set the the default  ``~/ampligraph_datasets`` is checked.

    If the dataset is not found at either location it is downloaded and placed in ``AMPLIGRAPH_DATA_HOME``
    or ``~/ampligraph_datasets``.

    It is divided in three splits:

    - ``train``
    - ``valid``
    - ``test``

    ========= ========= ======= ======= ============ ===========
     Dataset  Train     Valid   Test    Entities     Relations
    ========= ========= ======= ======= ============ ===========
    WN18RR    86,835    3,034   3,134   40,943        11
    ========= ========= ======= ======= ============ ===========
    Cleaned   86,835    3,034   3,134   40,943        11
    ========= ========= ======= ======= ============ ===========

    .. warning:: WN18RR's validation set contains 198 unseen entities over 210 triples.
        The test set has 209 unseen entities, distributed over 210 triples.

    return data, total_entities, total_relations
    """

    data_name = 'WN18RR'

    data_sha1 = {
        'train': '078fd2890583f99d75342d0eeea6d0c4e6167c76',
        'test': 'da28a9c1759d66f87873d8e1ecc4884501211f83',
        'valid': '38bbe458b5f8e36310456cac5e0119f05d39cef5'
    }

    return _load_data(data_name, data_sha1, path, original)

def load_all_datasets(path=None):
    load_fb15k(path=path)
    load_fb15k237(path=path)
    load_wn18(path=path)
    load_wn18rr(path=path)

class DataBase():

    def __init__(self, data_name, path=None):

        kg_benchmark_dataset = set([x.lower() for x in KG_BENCHMARK_DATASETS])
        dir_name = data_name.lower()
        assert dir_name in kg_benchmark_dataset, f'{data_name} is not in benchmark datasets.'

        self.path = path
        self.dir_name = 'wn18RR' if dir_name == 'wn18rr' else dir_name

        self.filenames = [
            '1-1.txt',
            '1-n.txt',
            'n-1.txt',
            'n-n.txt',
            'constrain.txt',
            'entity2id.txt',
            'relation2id.txt',
            'train.csv',
            'valid.csv',
            'test.csv',
            'test2id_all.txt'
        ]

        self.data, self.entity_total, self.relation_total = self.load_dataset()

    @property
    def name(self):
        return self.dir_name

    def len(self):
        return len(self.data['train'])

    def load_dataset(self):
        if self.dir_name == 'fb15k':
            return load_fb15k(path=self.path)
        elif self.dir_name == 'fb15k-237':
            return load_fb15k237(path=self.path)
        elif self.dir_name == 'wn18':
            return load_wn18(path=self.path)
        else:
            return load_wn18rr(path=self.path)

    def load_1_1(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, '1-1.txt')
        return load_from_text(file_path=file_path, dtype=np.int64)

    def load_1_n(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, '1-n.txt')
        return load_from_text(file_path=file_path, dtype=np.int64)

    def load_n_1(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'n-1.txt')
        return load_from_text(file_path=file_path, dtype=np.int64)

    def load_n_n(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'n-n.txt')
        return load_from_text(file_path=file_path, dtype=np.int64)

    def load_train(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'train.csv')
        return load_from_csv(file_path=file_path, dtype=np.int64)

    def load_test(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'test.csv')
        return load_from_csv(file_path=file_path, dtype=np.int64)

    def load_valid(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'valid.csv')
        return load_from_csv(file_path=file_path, dtype=np.int64)

    def load_constraint(self):
        file_path = os.path.join(os.environ['KG_DIR'], self.dir_name, 'constraint.txt')
        constraint = {'rel_src': {}, 'rel_dst': {}}
        with open(file_path, 'r') as f:
            for i, line in enumerate(f.readlines()):
                line = [int(x) for x in line.strip().split('\t')]
                if i % 2 == 0:
                    constraint['rel_src'][line[0]] = set(line[2:])
                else:
                    constraint['rel_dst'][line[0]] = set(line[2:])
        return constraint

    @staticmethod
    def _build_graph(data_array):
        return build_graph(data_array)

    def get_all_triples(self):
        return get_all_triples(self.data)

    def get_select_src_rate(self):
        return get_select_src_rate(self.data)

class FB15k(DataBase):
    def __init__(self, path=None):
        super(FB15k, self).__init__(data_name='FB15k', path=path)

class FB15k237(DataBase):
    def __init__(self, path=None):
        super(FB15k237, self).__init__(data_name='FB15k-237', path=path)

class WN18(DataBase):
    def __init__(self, path=None):
        super(WN18, self).__init__(data_name='WN18', path=path)

class WN18RR(DataBase):
    def __init__(self, path=None):
        super(WN18RR, self).__init__(data_name='WN18RR', path=path)


# In[1]:
# packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# In[2]:
## functions

def dataImport(Patient, Pattern, agent, keys):

    mat = pd.DataFrame(np.load('Pt%s_%d_%d.npy'%(Patient, Pattern, agent), allow_pickle=True).item(), columns=keys)

    return mat

def dataSplit(mat):

    data_plus = 0.5*mat.values.T
    data_minus = -0.5*mat.values.T

    return data_plus, data_minus

def plot(Patient, data, allcycle):

    colors = ['thistle','#FBA07E','yellowgreen','#72AC4C','lightskyblue','darkseagreen','#FCCC2E','tomato','violet','#7C99D2','peru','khaki','#F197B7','orange','#F5DEB3','tan']
    font1 = {'family':'Arial', 'weight':'normal', 'size':'60'}
    time = np.arange(allcycle+1)

    n = len(data)

    fig, axs = plt.subplots(n, 1, figsize=(15,4*n), sharex=True)
    fig.subplots_adjust(hspace=0.1)

    for i in range(n-1):

        axs[i].spines['top'].set_visible(False)
        axs[i].spines['right'].set_visible(False)
        axs[i].spines['bottom'].set_visible(False)
        axs[i].spines['left'].set_visible(False)

        axs[i].stackplot(time, data[i][0], colors=colors, rasterized=True)
        axs[i].stackplot(time, data[i][1], colors=colors, rasterized=True)
        axs[i].get_xaxis().set_visible(False)
        axs[i].get_yaxis().set_visible(False)
        axs[i].set_ylim(-400000000000, 400000000000)

    axs[n-1].spines['top'].set_visible(False)
    axs[n-1].spines['right'].set_visible(False)
    axs[n-1].spines['left'].set_visible(False)

    axs[n-1].stackplot(time, data[n-1][0], colors=colors, rasterized=True)
    axs[n-1].stackplot(time, data[n-1][1], colors=colors, rasterized=True)
    axs[n-1].set_xticks(np.arange(0, allcycle+1, 1000))
    axs[n-1].get_yaxis().set_visible(False)
    axs[n-1].set_ylim(-400000000000, 400000000000)

    axs[n-1].set_xlabel('Time (cell cycle)', font1, labelpad=20)

    plt.rcParams['xtick.direction'] = 'in'
    plt.rcParams['ytick.direction'] = 'in'

    labels = axs[n-1].get_xticklabels() + axs[n-1].get_yticklabels()
    [label.set_fontname('Arial') for label in labels]
    axs[n-1].tick_params(length=10, labelsize=40, pad=20)

    plt.savefig('TNBC_%s_plus.tif' %Patient, bbox_inches='tight')
    plt.savefig('TNBC_%s_plus.pdf' %Patient, bbox_inches='tight')
    plt.show()

# In[3]:
## plot subclonal dynamics through total number of cells
keys = ['D:0.0,R:0.9,P:0.1',
        'D:0.0,R:1.0,P:0.0',
        'D:0.1,R:0.7,P:0.2',
        'D:0.1,R:0.8,P:0.1',
        'D:0.1,R:0.9,P:0.0',
        'D:0.2,R:0.5,P:0.3',
        'D:0.2,R:0.6,P:0.2',
        'D:0.2,R:0.7,P:0.1',
        'D:0.3,R:0.3,P:0.4',
        'D:0.3,R:0.4,P:0.3',
        'D:0.3,R:0.5,P:0.2',
        'D:0.4,R:0.1,P:0.5',
        'D:0.4,R:0.2,P:0.4',
        'D:0.4,R:0.3,P:0.3',
        'D:0.5,R:0.0,P:0.5',
        'D:0.5,R:0.1,P:0.4']

Para1 = [0.01, 0.008]
Para2 = [0.01, 8000000000000, 0.008]
Para3 = [0.01, 1, 0.0000000000005, 0.008]

dataE = []
for i in range(5):
    temp = dataImport('039', 1, i, keys)
    dataE.append(dataSplit(temp))

plot('039', dataE, 2000)

dataL = []
for i in range(5):
    temp = dataImport('039', 2, i, keys)
    dataL.append(dataSplit(temp))

plot('039', dataL, 2000)

dataH = []
for i in range(5):
    temp = dataImport('039', 3, i, keys)
    dataH.append(dataSplit(temp))

plot('039', dataH, 2000)


import copy
import pandas as pd
import numpy as np
import board as board

class Game:

    def __init__(self,player1,player2,board):
        self.player1 = player1
        self.player2 = player2
        self.board = board
        self.player1wins = 0
        self.player2wins = 0
        self.draws = 0

    #Play game will give an iteration of the game
    def PlayGame(self):
        PlayerToMove = self.player1 #as player1 always starts
        while (self.board.GetGameResult() == 2): #While game is not finished
            AvailableMoves = self.board.GetAvailableMoves()
            SelectedMove = PlayerToMove.move(self.board,AvailableMoves)
            self.board.update(SelectedMove,PlayerToMove.GetValue())
            if PlayerToMove.GetValue() == 1:
                PlayerToMove = self.player2
            else:
                PlayerToMove = self.player1

    def PlayManyGames(self,games,BurnIn=0):
        self.games = games - BurnIn
        for _ in range(BurnIn):
            self.board.ResetBoard()
            self.PlayGame()
            if self.board.GetGameResult() == 1:
                self.player1.reward(1)
                self.player2.reward(-1)
            elif self.board.GetGameResult() == -1:
                self.player1.reward(-1)
                self.player2.reward(1)
            else:
                self.player1.reward(0)
                self.player2.reward(0)
            self.player1.resetStatesHistory()
            self.player2.resetStatesHistory()

        for _ in range(games - BurnIn):
            self.board.ResetBoard()
            self.PlayGame()
            if self.board.GetGameResult() == 1:
                self.player1wins += 1
                self.player1.reward(1)
                self.player2.reward(-1)
            elif self.board.GetGameResult() == -1:
                self.player2wins += 1
                self.player1.reward(-1)
                self.player2.reward(1)
            else:
                self.draws += 1
                self.player1.reward(0)
                self.player2.reward(0)
            self.player1.resetStatesHistory()
            self.player2.resetStatesHistory()

    def PrintStats(self):
        total_games = self.games
        print(f"Player 1 wins: {self.player1wins}  ({self.player1wins/total_games*100:.2f}%)")
        print(f"Player 2 wins: {self.player2wins}  ({self.player2wins/total_games*100:.2f}%)")
        print(f"Draws: {self.draws}  ({self.draws/total_games*100:.2f}%)")


import cv2
import numpy as np

image = cv2.imread("C:/Users/14282/Desktop/Code/Python/cv/0.png",0) #将图片以灰度图导入
h = image.shape[0]
w = image.shape[1]

threshold, new_image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow("new",new_image)
print('阈值：'+str(threshold))

n = 0
new_image = np.zeros((h,w),np.uint8)
for i in range(h):
    for j in range(w):
        if(image[i,j]> threshold ):
            new_image[i,j] = 255
            n+=1
        else:
            new_image[i,j] = 0

print('黄色部分所占比例:'+str(n/(h*w)))

cv2.waitKey()


"""
Django settings for sprout project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
from __future__ import absolute_import
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
from datetime import timedelta
import os

from utils.conf import credentials

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = credentials["sprout"]["key"]

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DJANGO_DEBUG", "false") == "true"

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = [
    "localhost",
    "127.0.0.1"
]

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'appliances',
    'django_object_actions',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'

ROOT_URLCONF = 'sprout.urls'

WSGI_APPLICATION = 'sprout.wsgi.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.messages.context_processors.messages',
                'django.contrib.auth.context_processors.auth',
                "django.template.context_processors.request",
                "django.template.context_processors.debug",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                'appliances.context_processors.hubber_url',
                'appliances.context_processors.sprout_needs_update',
            ],
        },
    },
]

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

# STATIC_ROOT = os.path.join(BASE_DIR, '_static')

# CELERY SETTINGS
REDIS_PORT = os.environ.get("REDIS_PORT", 6379)
BROKER_URL = 'redis://127.0.0.1:{}/{}'.format(REDIS_PORT, os.environ.get("REDIS_DB_ID", 0))
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:{}/{}'.format(
    REDIS_PORT, os.environ.get("REDIS_RESULT_DB_ID", 1))
# CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_RESULT_SERIALIZER = 'pickle'
CELERY_TIMEZONE = "UTC"
CELERY_DISABLE_RATE_LIMITS = True
CELERY_TIMEZONE = 'UTC'
CELERYD_MAX_TASKS_PER_CHILD = int(os.environ.get("MAX_TASKS_PER_WORKER", 50))
# CELERY_ACKS_LATE = True
# CELERYD_PREFETCH_MULTIPLIER = 1

# TODO: To be able to use RHEV-M (and other EMSs), the mgmt_system for it must have
# TODO: .mark_as_template implemented. Then it can be used.
TEMPLATE_FORMAT = "s_tpl_{group}_{date}_{rnd}"
APPLIANCE_FORMAT = "s_appl_{group}_{date}_{rnd}"

# General redis settings
GENERAL_REDIS = dict(host='127.0.0.1', port=REDIS_PORT, db=int(os.environ.get("REDIS_GENERAL", 2)))

ATOMIC_REQUESTS = False  # Turn off after moving to postgre

HUBBER_URL = None

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:{}'.format(os.environ.get("MEMCACHED_PORT", 23156)),
    }
}

BROKEN_APPLIANCE_GRACE_TIME = dict(
    hours=1,
)

ORPHANED_APPLIANCE_GRACE_TIME = dict(
    minutes=45,
)

# Celery beat
CELERYBEAT_SCHEDULE = {
    'check-templates': {
        'task': 'appliances.tasks.check_templates',
        'schedule': timedelta(minutes=17),
    },

    'refresh-appliances': {
        'task': 'appliances.tasks.refresh_appliances',
        'schedule': timedelta(minutes=7),
    },

    'free-appliance-shepherd': {
        'task': 'appliances.tasks.free_appliance_shepherd',
        'schedule': timedelta(seconds=30),
    },

    'kill-unused-appliances': {
        'task': 'appliances.tasks.kill_unused_appliances',
        'schedule': timedelta(minutes=1),
    },

    'delete-nonexistent-appliances': {
        'task': 'appliances.tasks.delete_nonexistent_appliances',
        'schedule': timedelta(minutes=10),
    },

    'poke-trackerbot': {
        'task': 'appliances.tasks.poke_trackerbot',
        'schedule': timedelta(minutes=10),
    },

    'process-delayed-provision-tasks': {
        'task': 'appliances.tasks.process_delayed_provision_tasks',
        'schedule': timedelta(seconds=20),
    },

    'check-update': {
        'task': 'appliances.tasks.check_update',
        'schedule': timedelta(minutes=15),
    },

    'scavenge-managed-providers': {
        'task': 'appliances.tasks.scavenge_managed_providers',
        'schedule': timedelta(minutes=30),
    },

    'mailer-version-mismatch': {
        'task': 'appliances.tasks.mailer_version_mismatch',
        'schedule': timedelta(minutes=60),
    },

    'obsolete-template-deleter': {
        'task': 'appliances.tasks.obsolete_template_deleter',
        'schedule': timedelta(days=1),
    },
}

try:
    from sprout.local_settings import *  # NOQA
except ImportError:
    pass


class Messages:

    """This class contains all the levels storyline messages
    that will be passed my the game engine."""

    def __init__(self):
        self.house1 = "\nYou are at the house and you can begin by going"
        self.house1 += " to the bedroom or heading out the door to the forest."
        self.house2 = ""

        self.room1 = "\nYou have entered the Bedroom"
        self.room2 = "\nThere is nothing else in the bedroom"

        self.forest1 = "\nNow you are on a dirt path in the forest"
        self.forest2 = ""

        self.bush1 = "\nYou are now stuck in a small burrow in the woods"
        self.bush2 = "\nThere is nothing else in this burrow"

        self.entry1 = "\nThe caves entrance is cool with dim lighting"
        self.entry2 = ""

        self.exit1 = "\nFinally, You have arrived at the tunnels Exit!"
        self.exit2 = "\n\tCONGRATULATIONS! You have retrieved the Golden"
        self.exit2 += " Router and have found the exit to the dungeon."
        self.exit2 += " Game WON!"

        self.deadend1 = "\nOh no, you have found a dead end!"
        self.deadend2 = "\nThere is nothing else in this dark and cold hole"

        self.lake1 = "\nYou have just stumbled upon a huge underground"
        self.lake1 += " lake and the only way out of here is to cross it!"
        self.lake2 = ""

        self.lakeExit1 = "\nYou have successfully crossed the lake but"
        self.lakeExit1 += " doing so has weakened you and lowered your level"
        self.lakeExit2 = "\nThere is nothing else in the room"

        self.tunnel1 = "\nYou've found yourself in a dark muggy tunnel"
        self.tunnel2 = "\nWhere will you go now?"


import json
import os
import random
from time import sleep

from utils.utils import Utils
from utils.footage_creator import FootageCreator

import schedule
import telebot

def main():
    while True:
        with open("videos.json", "r") as videos_list:
            videos = json.load(videos_list)["videos"]
            video_data = random.choice(videos)

        with FootageCreator() as fc:
            url = fc.get_stream_url(video_data)
            video = fc.generate_random_mp4_video(url)

            if video:
                bot = telebot.TeleBot(os.getenv("BOT_TOKEN"))
                hash_tag = Utils.generate_hash_tag(
                    "криминальная россия", video_data["name"]
                )

                with open("output.mp4", "rb") as video_file:
                    bot.send_video(
                        os.getenv("CHANNEL_ID"),
                        video_file,
                        caption=f"{hash_tag}\n\n{video_data['description']}",
                        parse_mode="HTML",
                    )

                os.remove(video)

                break

if __name__ == "__main__":
    TIMEOUT = os.getenv("TIMEOUT")
    schedule.every(int(TIMEOUT) if TIMEOUT else 10).minutes.do(main)

    while 1:
        schedule.run_pending()
        sleep(1)


import os
import sys
import time
import subprocess
import atexit
import shutil
import src.internals.graph as graphviz
from multiprocessing import Process, Queue, Lock
from src.internals.disambiguator import Disambiguator
from src.internals.analyser import z3_analyse_hdead, z3_analyse_full, load_dot
from src.internals.process_manager import ProcessManager
from flask import session, Flask, request, render_template

from src.internals.translator import Translator
from src.internals.vmc_controller import VmcController, VmcException

UPLOAD_FOLDER = os.path.relpath("uploads")
TMP_FOLDER = os.path.join("src",'static','tmp')
VMC_LINUX = os.path.relpath('vmc65-linux')
VMC_MAC = os.path.relpath('vmc-macos')
VMC_WINDOWS = os.path.relpath('vmc-win7.exe')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['TMP_FOLDER'] = TMP_FOLDER
#Secret key used to cipher session cookies
app.secret_key = b'\xb1\xa8\xc0W\x0c\xb3M\xd6\xa0\xf4\xabSmz=\x83'
#Maximum uploaded file size 1MB
app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024

import src.sessions as sessions
import src.file_manager as fm

app.before_first_request(fm.start_deleter)
atexit.register(fm.final_delete)

@app.errorhandler(413)
def request_entity_too_large(error):
    return {'text': 'File Too Large'}, 413

def full_analysis_worker(fts_file, out_file, queue):
    dead = []
    false = []
    hidden = []
    fts_source = open(fts_file, 'r')
    sys.stdout = open(out_file, 'w')
    fts = load_dot(fts_source)
    z3_analyse_full(fts)
    for transition in fts._set_dead:
        dead.append({'src':transition._in._id, 'dst':transition._out._id,
            'label':str(transition._label), 'constraint':str(transition._constraint)})
    for transition in fts._set_false_optional:
        false.append({'src':transition._in._id, 'dst':transition._out._id,
            'label':str(transition._label), 'constraint':str(transition._constraint)})
    for state in fts._set_hidden_deadlock:
        hidden.append(state._id)
    queue.put({'ambiguities':{'dead': dead, 'false': false, 'hidden': hidden}})
    fts.report()
    sys.stdout.close()
    fts_source.close()

def hdead_analysis_worker(fts_file, out_file, queue):
    hidden = []
    fts_source = open(fts_file, 'r')
    sys.stdout = open(out_file, 'w')
    fts = load_dot(fts_source)
    z3_analyse_hdead(fts)
    for state in fts._set_hidden_deadlock:
        hidden.append(state._id)
    queue.put({'ambiguities':{'dead':[], 'false':[], 'hidden': hidden}})
    fts.report()
    sys.stdout.close()
    fts_source.close()

@app.route('/yield')
def get_output():
    pm = ProcessManager.get_instance()
    if not sessions.check_session():
        sessions.delete_output_file()
        return {"text":'\nSession timed-out'}, 404
    elif not 'id' in session or not pm.process_exists(session['id']):
        sessions.delete_output_file()
        return {"text":''}, 404
    else:
        with open(session['output']) as out:
            if pm.is_alive(session['id']):
                out.seek(session['position'])
                result = out.read(4096)
                session['position'] = out.tell()
                return {"text":result}, 206
            else:
                out.seek(session['position'])
                result = out.read()
                os.remove(session['output'])
                queue = ProcessManager.get_instance().get_queue(session['id'])
                payload = {}
                payload['text'] = result
                graph = graphviz.Graph.from_file(session['model'])
                payload['edges'], payload['nodes'] = graph.get_graph_number()
                payload['mts'] = graph.get_mts()
                if(queue):
                    tmp = queue.get()
                    session['ambiguities'] = tmp['ambiguities']
                    payload['ambiguities'] = tmp['ambiguities']
                    ProcessManager.get_instance().delete_queue(session['id'])
                    try:
                        dis = Disambiguator.from_file(session['model'])
                        dis.highlight_ambiguities(tmp['ambiguities']['dead'],
                                tmp['ambiguities']['false'],
                                tmp['ambiguities']['hidden'])
                        payload['graph'] = dis.get_graph()
                        graphviz.Graph(dis.get_graph()).draw_graph(session['graph'])
                        return payload, 200
                    except:
                        return payload, 200
                return payload, 200

@app.route('/', methods=['GET', 'POST'])
def index():
    return render_template("main.html")

@app.route('/full_analysis', methods=['POST'])
def full_analyser():
    pm = ProcessManager.get_instance()
    queue = Queue()
    lock = Lock()
    sessions.update_session_timeout()
    file_path = session['model']
    if os.path.isfile(file_path):
        thread = Process(target=full_analysis_worker,
                args=[file_path, session['output'], queue])
        session['id'] = str(thread.name)
        pm.add_process(session['id'], thread)
        pm.add_queue(session['id'], queue)
        pm.add_lock(session['id'], lock)
        pm.start_process(session['id'])
        session['position'] = 0
        return "Processing data..."
    return {"text": 'File not found'}, 400

@app.route('/hdead_analysis', methods=['POST'])
def hdead_analyser():
    pm = ProcessManager.get_instance()
    queue = Queue()
    lock = Lock()
    sessions.update_session_timeout()
    file_path = session['model']
    if os.path.isfile(file_path):
        thread = Process(target=hdead_analysis_worker,
                args=[file_path, session['output'], queue])
        session['id'] = str(thread.name)
        pm.add_process(key=session['id'], process=thread)
        pm.add_queue(session['id'], queue)
        pm.start_process(session['id'])
        pm.add_lock(session['id'], lock)
        session['position'] = 0
        return "Processing data..."
    return {"text": 'File not found'}, 400

@app.route('/delete_model', methods=['POST'])
def delete_model():
    if not 'model' in session:
        return {"text":"Model file deleted"}, 200
    file_path = session['model']
    sessions.close_session()
    if os.path.isfile(file_path):
        return {"text": "An error occured while deleting the file model"}, 400
    else:
        return {"text":"Model file deleted"}, 200

@app.route('/stop', methods=['POST'])
def stop_process():
    pm = ProcessManager.get_instance()
    session.pop('position', None)
    if 'id' in session and session['id']:
        pm.end_process(session['id'])
    sessions.delete_output_file()
    session.pop('id', None)
    session.pop('ambiguities', None)
    return {"text":'Stopped process'}, 200

@app.route('/remove_ambiguities', methods=['POST'])
def disambiguate():
    pm = ProcessManager.get_instance()
    if not sessions.check_session():
        return {"text": "No ambiguities data available execute a full analysis first"}, 400
    if not session['ambiguities']:
        queue = pm.get_queue(session['id'])
        if not queue:
            return {"text": "No ambiguities data available execute a full analysis first"}, 400
        else:
            session['ambiguities'] = queue.get()
    payload = {}
    file_path = session['model']
    if os.path.isfile(file_path):
        dis = Disambiguator.from_file(file_path)
        dis.remove_transitions(session['ambiguities']['dead'])
        dis.set_true_list(session['ambiguities']['false'])
        dis.solve_hidden_deadlocks(session['ambiguities']['hidden'])
        pm.delete_queue(session['id'])
        graph = graphviz.Graph(dis.get_graph())
        payload['mts'] = graph.get_mts()
        payload['text'] = "Removed ambiguities"
        payload['graph'] = graph.get_graph()
        payload['edges'], payload['nodes'] = graph.get_graph_number()
        graph.draw_graph(session['graph'])
        return payload, 200
    return {"text": 'File not found'}, 400

@app.route('/apply_all', methods=['POST'])
def apply_all():
    payload, status = disambiguate()
    if status == 200:
        if os.path.isfile(session['model']):
            with open(session['model'], 'w') as model:
                try:
                    model.write(payload['graph']);
                    session['ambiguities']['dead'] = []
                    session['ambiguities']['false'] = []
                    session['ambiguities']['hidden'] = []
                    session.modified = True
                    return {'text': 'Model file updated correctly'}, 200
                except:
                    return {'text':'Unable to update file model'}, 400
    return {'text':'Unable to update file model'}, 400

@app.route('/remove_false_opt', methods=['POST'])
def solve_fopt():
    pm = ProcessManager.get_instance()
    if not sessions.check_session():
        return {"text": "No ambiguities data available execute a full analysis first"}, 400
    if not session['ambiguities']:
        queue = pm.get_queue(session['id'])
        if not queue:
            return {"text": "No ambiguities data available execute a full analysis first"}, 400
        else:
            session['ambiguities'] = queue.get()
    payload = {}
    file_path = session['model']
    if os.path.isfile(file_path):
        dis = Disambiguator.from_file(file_path)
        dis.set_true_list(session['ambiguities']['false'])
        pm.delete_queue(session['id'])
        graph = graphviz.Graph(dis.get_graph())
        payload['mts'] = graph.get_mts()
        payload['text'] = "Removed false optional transitions"
        payload['graph'] = graph.get_graph()
        payload['edges'], payload['nodes'] = graph.get_graph_number()
        graph.draw_graph(session['graph'])
        return payload, 200
    return {"text": 'File not found'}, 400

@app.route('/apply_fopt', methods=['POST'])
def apply_fopt():
    payload, status = solve_fopt()
    if status == 200:
        if os.path.isfile(session['model']):
            with open(session['model'], 'w') as model:
                try:
                    model.write(payload['graph']);
                    session['ambiguities']['false'] = []
                    session.modified = True
                    return {'text': 'Model file updated correctly'}, 200
                except:
                    return {'text':'Unable to update file model'}, 400
    return {'text':'Unable to update file model'}, 400

@app.route('/remove_dead_hidden', methods=['POST'])
def solve_hdd():
    pm = ProcessManager.get_instance()
    if not sessions.check_session():
        return {"text": "No ambiguities data available execute a full analysis first"}, 400
    if not session['ambiguities']:
        queue = pm.get_queue(session['id'])
        if not queue:
            return {"text": "No ambiguities data available execute a full analysis first"}, 400
        else:
            session['ambiguities'] = queue.get()
    payload = {}
    file_path = session['model']
    if os.path.isfile(file_path):
        dis = Disambiguator.from_file(file_path)
        dis.remove_transitions(session['ambiguities']['dead'])
        dis.solve_hidden_deadlocks(session['ambiguities']['hidden'])
        pm.delete_queue(session['id'])
        graph = graphviz.Graph(dis.get_graph())
        payload['mts'] = graph.get_mts()
        payload['text'] = "Removed hidden deadlocks and dead transitions"
        payload['graph'] = graph.get_graph()
        payload['edges'], payload['nodes'] = graph.get_graph_number()
        graph.draw_graph(session['graph'])
        return payload, 200
    return {"text": 'File not found'}, 400

@app.route('/apply_hdd', methods=['POST'])
def apply_hdd():
    payload, status = solve_hdd()
    if status == 200:
        if os.path.isfile(session['model']):
            with open(session['model'], 'w') as model:
                try:
                    model.write(payload['graph']);
                    session['ambiguities']['dead'] = []
                    session['ambiguities']['hidden'] = []
                    session.modified = True
                    return {'text': 'Model file updated correctly'}, 200
                except:
                    return {'text':'Unable to update file model'}, 400
    return {'text':'Unable to update file model'}, 400

def get_vmc():
    pm = ProcessManager.get_instance()
    if not session['ambiguities']:
        queue = pm.get_queue(session['id'])
        if not queue:
            raise VmcException("No ambiguities data available execute a full analysis first")
        else:
            session['ambiguities'] = queue.get()
    if (len(session['ambiguities']['hidden']) != 0):
        raise VmcException("Hidden deadlocks detected. It is necessary to remove them before checking the property")

    fpath = session['model']
    actl_property = request.form['property']
    if (len(actl_property) == 0):
        raise VmcException('Missing property to be verified')

    if os.path.isfile(fpath):
        translator = Translator()
        translator.load_model(fpath)
        translator.translate()
        session_tmp_folder = session['output'].split('-output')[0]
        try:
            os.mkdir(session_tmp_folder)
        except FileExistsError:
            #if the directory is already present continue
            pass

        session_tmp_model = os.path.join(session_tmp_folder, 'model.txt')
        session_tmp_properties = os.path.join(session_tmp_folder, 'properties.txt')
        with open(session_tmp_model,"w") as vmc_file:
            vmc_file.write(translator.get_output())
        with open(session_tmp_properties,"w") as prop_file:
            prop_file.write(actl_property)

        try:
            if sys.platform.startswith('linux'):
                vmc = VmcController(VMC_LINUX)
            elif sys.platform.startswith('win'):
                vmc = VmcController(VMC_WINDOWS)
            elif sys.platform.startswith('cygwin'):
                vmc = VmcController(VMC_WINDOWS)
            elif sys.platform.startswith('darwin'):
                vmc = VmcController(VMC_MAC)
            else:
                raise VmcException("VMC is not compatible with your operating system")
            vmc.run_vmc(session_tmp_model,session_tmp_properties)
        except ValueError as ve:
            if str(ve) == 'Invalid vmc_path':
                shutil.rmtree(session_tmp_folder)
                raise VmcException("Unable to locate VMC executable")
            if str(ve) == 'Invalid model file':
                shutil.rmtree(session_tmp_folder)
                raise VmcException('Invalid model file')
            if str(ve) == 'Invalid properties file':
                shutil.rmtree(session_tmp_folder)
                raise VmcException('Invalid properties file')
        except:
            shutil.rmtree(session_tmp_folder)
            raise VmcException('An error occured')
        shutil.rmtree(session_tmp_folder)
        return vmc, translator
    raise VmcException('File not found')

@app.route('/verify_property', methods=['POST'])
def verify_property():
    try:
        vmc, t = get_vmc()
        return {"formula": vmc.get_formula(), "eval": vmc.get_eval(), "details": vmc.get_details()}, 200
    except VmcException as e:
        return {"text": str(e)}, 400
    except Exception as e:
        return {"text": "An error occured"}, 400

@app.route('/graph', methods=['POST'])
def get_graph():
    message = """No graph data available, the graph may be too big render.
        You can render it locally by downloading the graph source code and use
        the following command: dot -Tsvg model.dot -o output.svg"""
    if 'src' in request.form and request.form['src']:
        graphviz.Graph(request.form['src']).draw_graph(session['graph'])
    if sessions.check_session():
        if os.path.isfile(session['graph']):
            return {"source":os.path.join('static', 'tmp',
                os.path.basename(session['graph']))}, 200
    return {"text":message}, 400

@app.route('/counter_graph', methods=['POST'])
def show_counter_graph():
    if sessions.check_session():
        try:
            vmc, t = get_vmc()
            if not vmc._is_formula():
                return {"text": 'The formula is not valid, no counter example available'}, 200
            clean_counter = vmc.clean_counterexample()
            if not clean_counter:
                if vmc.get_eval() == 'FALSE':
                    return {"explanation": vmc.get_explanation()}
                else:
                    return {"text": 'The formula is TRUE, no counter example available'}, 200
            t.load_mts(clean_counter)
            t.mts_to_dot(session['counter_graph'])
            if(os.path.isfile(os.path.join(app.config['TMP_FOLDER'],
                os.path.basename(session['counter_graph'])))):
                return {
                        "text": "Here's an example where the provided property is false:",
                        "graph": os.path.join('static','tmp', os.path.basename(session['counter_graph']))
                        }, 200
            else:
                return {'text': 'No counter example graph available'}, 404
        except VmcException as e:
            return {"text": str(e)}, 400
        except Exception as e:
            print(str(e))
            return {"text": "An error occured"}, 400

@app.route('/translate_to_pml', methods=['POST'])
def translate_to_pml():
    if sessions.check_session():
        t = Translator()
        print('mdoasdoiajsodij')
        print(session['model'])
        t.load_model(session['model'])
        return {"text": t.translate_to_pml()}, 200


import openpyxl
import numpy as np

wb = openpyxl.load_workbook('MetroBank Balance Sheet.xlsx')

sheet = wb.worksheets[0] #Defines the active sheet
row_count = sheet.max_row + 1 #Gives the length of the csv

#--------------------------------------------------------------------------------------------------------------

Keys = {

    """
    Keys are dictionaries containing the keywords used to categorise debits/credits in the statement.
    """

    'Pay':['R M S LTD PAY', 'RMS PLC PAYMENTS'],

    'Dividends':['HSBC', 'MR S F GILONIS dividends', 'PAYPAL', 'NoteMachine', 'Bank Machine', 'LEVI STORE', 'Cineworld'],

    'Salary':['MR S F GILONIS Salary'],

    'Subsistence':['TESCO', 'MCDONALDS', 'BIRLEYS SANDWICHES', 'ELIOR@FEO', 'WAITROSE', 'CO-OP GROUP',
               'NOODLE EXPRESS', 'ALEXANDRA', 'INDEPENDENT HOUSE', 'BOOTS', 'KFC', 'PRET A MANGER',
               'PROTEIN HAUS', 'WCPC WIMBLEDON', 'FEO SOUTH QUAY', 'BUKOWSKI GRILL', 'SPORTS BAR AND GRILL',
               'LEMONGRASS', 'NEWS ON THE WHARF', 'CHIPOTLE', 'TAYLOR ST BARISTAS', 'THE SAVANNA',
               'KATZENJAMMERS', 'BEST ONE', 'THE GLASSHOUSE', 'THE ATTIC', 'BBQ KOREAN RESTRNT',
               'WRAP IT UP', 'CHICKEN SHOP', 'SANDWICH PLUS', 'IL VILLAGGIO', 'NANDOS', 'FRANCO MANCA',
               'SUPERDRUG','FIVE GUYS', 'CAFE SAMBA', 'OKI LONDON', 'SAINSBURYS', 'STARBUCKS',
               'WHEATSHEAF', 'WAGAMAMA', 'ELIOR UK', 'BURGER KING', 'SIMPLY DUCK', 'HOMEMADE PASSI',
               'IZ Mata Munchies', 'ALL BAR ONE', 'BARROW BOY', 'FAR ROCKAWAY', 'WM MORRISONS',
               'BRAZILIAN.NUTS', 'GOURMET', 'Gourmet', 'ANCHOR BANKSIDE', 'ABOKADO'],

    'Travel':['TICKETOFFICESALE', 'SELFSERVE TICKET', 'LUL TICKET MACHINE', 'TFL.GOV.UK',
                'PAYPAL UBER', 'THAMES CLIPPE'],

    'Rent':['MR S F GILONIS rent', 'Kings Lettings'],

    'Accountancy fees':['CLEVERACCOU'],

    'VAT':['HMRC VAT'],

    'Books':['WATERSTONES', 'WH SMITH', 'Amazon UK Marketplace', 'AMAZON UK', 'Amazon EU'],

    'Bank charges':['BLANK'],

    'Electronics':['CURRYS MEGASTORE']

    }
#--------------------------------------------------------------------------------------------------------------

Arrays = {

    """
    The arrays are used to count the sum of each category
    """

    'Pay':[],'Dividends':[],'Salary':[],'Subsistence':[],'Travel':[],'Rent':[],'Accountancy fees':[],'VAT':[],
    'Books':[],'Bank charges':[],'Electronics':[]
    }
#--------------------------------------------------------------------------------------------------------------

for i in range (1,row_count):  #From 1 to the number of rows
    entry=sheet.cell(row=i,column=2)  #Iterates through the second column (which contains the payment description)
    if entry.value is None:           #and stores the value in a variable 'entry'
        entry.value = "BLANK"   # If there is no description then label it 'BLANK'

    for key in Keys:
        cat = Keys[key] #Iterates through the Keys and sores the length of each in a variable 'lenCat'
        lenCat = len(cat)

        for k in range(lenCat):
            if(entry.value.find(cat[k]) != -1): # Looks through the keywords in each Key and compares them each payment description.
                sheet.cell(row=i,column=7).value = key #If there is a match then a label is added on the 7th column in Excel.
                value=sheet.cell(row=i,column=4).value - sheet.cell(row=i,column=5).value #the value (+ve/-ve) is stored in a variable 'value'
                Arrays[key].append(value) #This value is now appended to the relevant array.

#-----------------------------------------------------------------------------------------------------

#This code will add up to totals of each category and print them on the second sheet

#Total_pay = np.sum(Arrays['Pay'])
#print(Total_pay)

sheet2 = wb.worksheets[1] #defines second worksheet on which to print totals
count = 0 #creates a counter to locate correct column in excel
for array in Arrays: #iterates through each array
    cat = Arrays[array]
    total = np.sum(cat) #takes total of the array
    col=count+2
    count += 1
    sheet2.cell(row=2,column=col).value = array #prints name of array
    sheet2.cell(row=3,column=col).value = total #prints total beneath

    #-----------------------------------------------------------------------------------------------------

wb.save('MetroBank Balance Sheet.xlsx') #saves workbook


"""
  Library of EV3 robot functions that are useful in many different applications
  . For example things
  like arm_up, arm_down, driving around, or doing things with the Pixy camera.

  Add commands as needed to support the features you'd like to implement.  For
  organizational purposes try to only write methods into this library that
  are NOT specific to one tasks, but rather methods that would be useful
  regardless of the activity.  For example, don't make a connection to the
  remote control that sends the arm up if the ir remote control up button
  is pressed.  That's a specific input --> output task.  Maybe some other task
  would want to use the IR remote up button for something different.
  Instead just make a method called arm_up that could be called.  That way
  it's a generic action that could be used in any task.
"""
import mqtt_remote_method_calls as com
import ev3dev.ev3 as ev3
import time
import math

class Snatch3r(object):
    """Commands for the Snatch3r robot that might be useful in many
    different programs."""

    def __init__(self):
        self.running = True
        self.left_motor = ev3.LargeMotor(ev3.OUTPUT_B)
        self.right_motor = ev3.LargeMotor(ev3.OUTPUT_D)
        self.arm_motor = ev3.MediumMotor(ev3.OUTPUT_A)
        self.touch_sensor = ev3.TouchSensor()
        self.ir_sensor = ev3.InfraredSensor()
        self.beacon_seeker = ev3.BeaconSeeker(channel=4)
        self.color_sensor = ev3.ColorSensor()
        self.pixy = ev3.Sensor(driver_name='pixy-lego')
        self.mqtt_client = com.MqttClient(self)
        self.mqtt_client.connect_to_pc()

        assert self.ir_sensor.connected
        assert self.color_sensor.connected
        assert self.pixy.connected
        assert self.touch_sensor
        assert self.arm_motor.connected
        assert self.left_motor.connected
        assert self.right_motor.connected
        self.MAX_SPEED = 900

    def loop_forever(self):
        """Creates an infinite while loop"""
        while self.running:
            time.sleep(0.1)

    def drive_inches(self, distance, sp):
        """Drives the robot forward set amount of distance at a speed and
        backwards at a negative position"""
        pos = distance * 90

        self.left_motor.run_to_rel_pos(position_sp=pos, speed_sp=sp,
                                       stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.right_motor.run_to_rel_pos(position_sp=pos, speed_sp=sp,
                                        stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.left_motor.wait_while(ev3.Motor.STATE_RUNNING)
        self.right_motor.wait_while(ev3.Motor.STATE_RUNNING)

    def drive(self, left_speed_entry, right_speed_entry):
        """Drives the robot forward forever whenever the up button is
        pressed on the keypad or on the tkinter window"""
        self.left_motor.run_forever(speed_sp=left_speed_entry)
        self.right_motor.run_forever(speed_sp=right_speed_entry)

    def stop(self):
        """Stops the motors that are running on the robot when the spacebar
        button is pressed on the keypad or on the tkinter window"""
        self.right_motor.stop()
        self.left_motor.stop()

    def backward(self, left_speed_entry, right_speed_entry):
        """Drives the robot backwards when the down is pressed on the keypad
        or on the tkinter window"""
        self.left_motor.run_forever(speed_sp=-left_speed_entry)
        self.right_motor.run_forever(speed_sp=-right_speed_entry)

    def turn_degrees(self, degrees, sp):
        """Turns the robot a given amount of degrees at a speed and positive
        position and turns the  opposite way with negative position"""
        pos = int(degrees * 4.6)

        self.left_motor.run_to_rel_pos(position_sp=pos, speed_sp=sp,
                                       stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.right_motor.run_to_rel_pos(position_sp=-pos, speed_sp=sp,
                                        stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.left_motor.wait_while(ev3.Motor.STATE_RUNNING)
        self.right_motor.wait_while(ev3.Motor.STATE_RUNNING)

    def arm_calibration(self):
        """Calibrates the robot arm and sets the self.arm_position to 0 when
        the arm is all the way down"""
        self.arm_motor.run_forever(speed_sp=self.MAX_SPEED)

        while not self.touch_sensor.is_pressed:
            time.sleep(0.01)

        self.arm_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        ev3.Sound.beep().wait()

        arm_revolutions_for_full_range = 5112
        self.arm_motor.run_to_rel_pos(
            position_sp=-arm_revolutions_for_full_range)
        self.arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep().wait()
        self.arm_motor.position = 0

    def arm_up(self):
        """Lifts the robot arm up until it hits the touch sensor"""
        self.arm_motor.run_forever(speed_sp=900)
        while not self.touch_sensor.is_pressed:
            time.sleep(0.01)
        self.arm_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        ev3.Sound.beep().wait()

    def arm_down(self):
        """Lowers the robot arm to position 0"""
        self.arm_motor.run_to_abs_pos(position_sp=0, speed_sp=900)
        self.arm_motor.wait_while(ev3.Motor.STATE_RUNNING)
        ev3.Sound.beep().wait()

    def seek_beacon(self):
        """Finds the remote controller and drives to it and returns true
        when the beacon is reached."""
        forward_speed = 300
        turn_speed = 100
        print("running")
        while not self.touch_sensor.is_pressed:
            current_heading = self.beacon_seeker.heading
            current_distance = self.beacon_seeker.distance
            if current_distance == -128:
                print("IR Remote not found. Distance is -128")
                self.drive(-100, 100)
            else:
                if math.fabs(current_heading) < 2:
                    print("On the right heading. Distance: ", current_distance)
                    if current_distance <= 1:
                        print("You have found the beacon!")
                        self.stop()
                        self.drive_inches(4, 300)
                        # ev3.Sound.speak("You have found beacon")
                        return True
                    elif current_distance > 1:
                        self.drive(forward_speed, forward_speed)
                elif math.fabs(current_heading) > 2 and math.fabs(
                        current_heading) \
                        < 10:
                    print("Adjusting heading: ", current_heading)
                    if current_heading < 0:
                        self.drive(-turn_speed, turn_speed)
                    elif current_heading > 0:
                        self.drive(turn_speed, -turn_speed)
                elif math.fabs(current_heading) > 10:
                    self.drive(-200, 200)
                    print("Heading too far off to fix: ", current_heading)

            time.sleep(0.2)

        print("Bowser has stopped Mario!")
        ev3.Sound.speak('Bowser has stopped Mario')
        self.mqtt_client.send_message('bowser')
        self.stop()
        return False

    def shutdown(self):
        """Shuts down the robot and sets the LEDs to Green"""
        self.running = False

        self.left_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.right_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        self.arm_motor.stop(stop_action=ev3.Motor.STOP_ACTION_BRAKE)
        ev3.Leds.set_color(ev3.Leds.LEFT, ev3.Leds.GREEN)
        ev3.Leds.set_color(ev3.Leds.RIGHT, ev3.Leds.GREEN)
        print('Goodbye')
        ev3.Sound.speak('Goodbye').wait()

    def mario(self, left_speed_entry, right_speed_entry):
        """Function that is called in the guiPC that runs various different
        methods based on the robot's environment """
        self.drive(left_speed_entry, right_speed_entry)

        while True:
            self.pixy.mode = "SIG1"
            if self.pixy.value(3) > 45:
                self.seek_beacon()

                found_beacon = self.seek_beacon()

                if found_beacon is True:
                    ev3.Sound.speak(
                        "Mario found Princess Peach, You have won the "
                        "game").wait()
                    self.arm_up()
                    time.sleep(1)
                    self.arm_down()
                    self.mqtt_client.send_message('won_game')
                    break

            self.current_color = self.color_sensor.color
            if self.current_color == ev3.ColorSensor.COLOR_GREEN:
                self.stop()
                print('Mario has died')
                ev3.Sound.speak('Koopa Troopa has killed Mario').wait()
                self.mqtt_client.send_message('mario_is_dead')
                break

            if self.current_color == ev3.ColorSensor.COLOR_RED:
                self.stop()
                print('Mushroom obtained')
                ev3.Sound.speak("Mushroom obtained").wait()
                self.mqtt_client.send_message('mushroom_obtained')
                break


# namedtuple is an object like regular tuple but more readable. for readability we could use dictionaries but in that way we lose the immutability functionality of tuples and also there are lots of typing. so named tuples are a compromise
from collections import namedtuple

Color = namedtuple('Color', ['red', 'green', 'blue'])  # the first value is the name and the second value (list) is the values of the tuples

color = Color(55, 155, 255)  # readable: it is a color!
white = Color(255, 255, 255)
black = Color(red=0, green=0, blue=0)  # defines each argument
gray = Color(green=55, red=55, blue=55)  # defines each argument

print(color.blue)  # more readble: instead of color[0]


import numpy as np
import cv2
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

#def visusalize_detections(im, bboxes, plt_name='output', ext='.png', visualization_folder=None, thresh=0.5):
def visusalize_detections(img_path, xmin, ymin, xmax, ymax, score, plt_name='output', ext='.png', visualization_folder=None,  thresh=0.5):
    """
    A function to visualize the detections
    :param im: The image
    :param bboxes: The bounding box detections
    :param plt_name: The name of the plot
    :param ext: The save extension (if visualization_folder is not None)
    :param visualization_folder: The folder to save the results
    :param thresh: The detections with a score less than thresh are not visualized
    """

    im = cv2.imread(img_path)
    xmin = np.array(xmin)
    ymin = np.array(ymin)
    xmax = np.array(xmax)
    ymax = np.array(ymax)
    score = np.array(score)

    inds = np.where(score[:] >= thresh)[0]
    #print('inds:',inds)
    xmin = xmin[inds]
    xmax = xmax[inds]
    ymin = ymin[inds]
    ymax = ymax[inds]
    score = score[inds]

    #print(xmin)
    #print(ymin)
    #print(xmax)
    #print(ymax)
    fig, ax = plt.subplots(figsize=(12, 12))

    if im.shape[0] == 3:
        im_cp = im.copy()
        im_cp = im_cp.transpose((1, 2, 0))
        #if im.min() < 0:
        #    pixel_means = cfg.PIXEL_MEANS
        #    im_cp = im_cp + pixel_means

        im = im_cp.astype(dtype=np.uint8)

    im = im[:, :, (2, 1, 0)]

    ax.imshow(im, aspect='equal')

    if score.shape[0] == 0:
        return

    for i in range(score.shape[0]):
        ax.add_patch(
            plt.Rectangle((xmin[i], ymin[i]),
                          xmax[i]-xmin[i],
                          ymax[i]-ymin[i], fill=False,
                          edgecolor=(0, score[i], 0), linewidth=3)
        )

    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    if visualization_folder is not None:
        if not os.path.exists(visualization_folder):
            os.makedirs(visualization_folder)
        plt_name += ext
        plt.savefig(os.path.join(visualization_folder,plt_name),bbox_inches='tight')
        print('Saved {}'.format(os.path.join(visualization_folder, plt_name)))
    else:
        print('Visualizing {}!'.format(plt_name))
        plt.show()
    plt.clf()
    plt.cla()

def motion_blur(image, degree=10, angle=20):
    image = np.array(image)

    M = cv2.getRotationMatrix2D((degree/2, degree/2), angle, 1)
    motion_blur_kernel = np.diag(np.ones(degree))
    motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))

    motion_blur_kernel = motion_blur_kernel / degree
    blurred = cv2.filter2D(image, -1, motion_blur_kernel)
    # convert to uint8
    cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
    blurred = np.array(blurred, dtype=np.uint8)
    return blurred

# Make sure that caffe is on the python path:
caffe_root = '../'  # this file is expected to be in {sfd_root}/sfd_test_code
import os
os.chdir(caffe_root)
import sys
sys.path.insert(0, 'python')
import caffe

caffe.set_device(2)
caffe.set_mode_gpu()
model_def = 'models/VGGNet/WIDER_FACE/SFD_trained/deploy.prototxt'
model_weights = 'models/VGGNet/WIDER_FACE/SFD_trained/SFD.caffemodel'
net = caffe.Net(model_def, model_weights, caffe.TEST)

count = 0
Path = './tools/'
f = open('./output/demo_dets.txt', 'wt')
for Name in open('/data/home/larainelu/DataSet/2/image_list.txt'):
    Image_Path = Path + Name[:-1]
    image = caffe.io.load_image(Image_Path)
    heigh = image.shape[0]
    width = image.shape[1]

    net.blobs['data'].reshape(1, 3, image.shape[0], image.shape[1])
    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2, 0, 1))
    transformer.set_mean('data', np.array([104, 117, 123]))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2, 1, 0))
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image

    detections = net.forward()['detection_out']
    det_conf = detections[0, 0, :, 2]
    det_xmin = detections[0, 0, :, 3]
    det_ymin = detections[0, 0, :, 4]
    det_xmax = detections[0, 0, :, 5]
    det_ymax = detections[0, 0, :, 6]

    keep_index = np.where(det_conf >= 0)[0]
    det_conf = det_conf[keep_index]
    det_xmin = det_xmin[keep_index]
    det_ymin = det_ymin[keep_index]
    det_xmax = det_xmax[keep_index]
    det_ymax = det_ymax[keep_index]

    f.write('{:s}\n'.format(Name[:-1]))
    f.write('{:.1f}\n'.format(det_conf.shape[0]))
    for i in xrange(det_conf.shape[0]):
        xmin = det_xmin[i] * width
        ymin = det_ymin[i] * heigh
        xmax = det_xmax[i] * width
        ymax = det_ymax[i] * heigh
        score = det_conf[i]
        if score >= 0.5:
            f.write('{:.3f} {:.3f} {:.3f} {:.3f} {:.10f}\n'.
                    format(xmin, ymin, (xmax-xmin+1), (ymax-ymin+1), score))

    #############visualize#################
    output_path = './output/'
    visusalize_detections(Image_Path, det_xmin * width,det_ymin * heigh,det_xmax * width,det_ymax * heigh,det_conf, plt_name = Name[:-1]+'_detection_SFD', ext='.png', visualization_folder = output_path)
	#################################
    count += 1
    print('%d' % count)


def dev_maju(liste):
    list = liste.split()
    liste_maju = []
    for char in list:
        char_maj = char.upper()
        liste_maju.append(char_maj)
    return liste_maju

liste_mot = input("Entrez une liste de mots : ")
print("La liste des mots en majuscules est : ", dev_maju(liste_mot))

Exercices Python/Exercices Très Facile YTB/transfo_en_maj.py


S_ALONE = 0
S_TALKING = 1

# ==============================================================================
# Group class:
# member fields:
#   - An array of items, each a Member class
#   - A dictionary that keeps who is a chat group
# member functions:
#    - join: first time in
#    - leave: leave the system, and the group
#    - list_my_peers: who is in chatting with me?
#    - list_all: who is in the system, and the chat groups
#    - connect: connect to a peer in a chat group, and become part of the group
#    - disconnect: leave the chat group but stay in the system
# ==============================================================================

class Group:

    def __init__(self):
        self.members = {}
        self.chat_grps = {}
        self.grp_ever = 0

    def join(self, name):
        self.members[name] = S_ALONE
        return

    def is_member(self, name):

        # IMPLEMENTATION
        # ---- start your code ---- #
        return name in self.members.keys()
        # ---- end of your code --- #

    # implement
    def leave(self, name):
        """
        leave the system, and the group
        """
        # IMPLEMENTATION
        # ---- start your code ---- #
        self.disconnect(name)
        try:
            del self.members[name]
        except KeyError:
            print(name, "not found")
        # ---- end of your code --- #
        return

    def find_group(self, name):
        """
        Auxiliary function internal to the class; return two
        variables: whether "name" is in a group, and if true
        the key to its group
        """

        found = False
        group_key = 0
        # IMPLEMENTATION
        # ---- start your code ---- #
        for key, group in self.chat_grps.items():
            if name in group:
                found = True
                group_key = key
                break

        # ---- end of your code --- #
        return found, group_key

    def connect(self, me, peer):
        """
        me is alone, connecting peer.
        if peer is in a group, join it
        otherwise, create a new group with you and your peer
        """
        peer_in_group, group_key = self.find_group(peer)

        # IMPLEMENTATION
        # ---- start your code ---- #
        if me in self.members and peer in self.members: #In case someone accidentally adds someone not in system
            if peer_in_group:
                print(peer, "is talking already, connect!")
                if me not in self.chat_grps[group_key]:
                    self.chat_grps[group_key].append(me)
                    self.members[me] = S_TALKING
            else:
                print(peer, "is idle as well")
                self.grp_ever += 1
                self.chat_grps[self.grp_ever]=[me, peer]
                self.members[me] = self.members[peer] = S_TALKING
        else:
            print("{} or {} not found".format(me, peer))
        # ---- end of your code --- #
        return

    # implement
    def disconnect(self, me):
        """
        find myself in the group, quit, but stay in the system
        """
        # IMPLEMENTATION
        # ---- start your code ---- #
        if me in self.members: #Check if in system
            in_group, group_key = self.find_group(me)
            if in_group:
                self.chat_grps[group_key].remove(me)
                self.members[me] = S_ALONE
                if len(self.chat_grps[group_key]) == 1:# peer may be alone
                    peer = self.chat_grps[group_key].pop()
                    self.members[peer] = S_ALONE
                    del self.chat_grps[group_key]
        else:
            print("{} or {} not found".format(me, peer))
        # ---- end of your code --- #
        return

    def list_all(self):
        # a simple minded implementation
        full_list = "Users: ------------" + "\n"
        full_list += str(self.members) + "\n"
        full_list += "Groups: -----------" + "\n"
        full_list += str(self.chat_grps) + "\n"
        return full_list

    # implement
    def list_me(self, me):
        """
        return a list, "me" followed by other peers in my group
        """
        my_list = []
        # IMPLEMENTATION
        # ---- start your code ---- #
        if me in self.members.keys(): #check if in system
            my_list.append(me)
            in_group, group_key = self.find_group(me)
            if in_group:
                for member in self.chat_grps[group_key]:
                    if member != me:
                        my_list.append(member)
                # [my_list.append(member) for member in self.chat_grps[group_key]
                # if member != me]
        else:
            print(me, "not found")
        # ---- end of your code --- #
        return my_list

    def get_num_loners(self):
        """
        returns the number of loners in the system
        """
        num_loners = 0
        for m in self.members.values():
            if m == 0:
                num_loners += 1
        return num_loners

    def get_biggest_group(self):
        """
        returns the biggest group
        """
        return max(self.chat_grps.values(), key=len)

    def groups_with_n_member(self, n):
        """
        returns the groups with n members
        """
        return [group for group in self.chat_grps.values() if len(group) == n]

if __name__ == "__main__":
    g = Group()
    g.join('a')
    g.join('b')
    g.join('c')
    g.join('d')
    print(g.list_all())

    g.connect('a', 'b')
    print(g.list_all())
    print("Groups with 2 members:", g.groups_with_n_member(2))
    print("Biggest group:", g.get_biggest_group())
    print()
    g.connect('c', 'a')
    print(g.list_all())

    print("Members in my group:", g.list_me('b'))
    print("Number of loners:", g.get_num_loners())
    print("Biggest group:", g.get_biggest_group())
    print("Groups with three members:", g.groups_with_n_member(3))
    print()
    g.leave('c')
    print(g.list_all())
    g.disconnect('b')
    print(g.list_all())


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 24 08:35:05 2021

@author: Aaron Alvarez

Purpose: The purpose of this program is to obtain the product that was most ordered from a list
"""
from pyspark import SparkConf, SparkContext
import collections

# Configure app and provide name
conf = SparkConf().setMaster("local").setAppName("Product Most Ordered")
sc= SparkContext(conf = conf)

# Helper function to parse the lines of the csv file and get the neccesary fields for the exercise
def parseLine(line):
    fields = line.split(',')
    productID = int(fields[2])
    quantity = int(fields[3])
    return (productID, quantity)

# Read csv file
lines = sc.textFile("file:///SparkCourse/order_items.csv")

# Parse lines
parsedLines = lines.map(parseLine)

# Get totals by product
totalsByProduct = parsedLines.mapValues(lambda x:(x,1)).reduceByKey(lambda x,y:(x[0]+y[0], x[1]+y[1]))

# Sort results by total quantity ordered by product
sortedByquantity = totalsByProduct.sortBy(lambda x: x[1])

# Collect the result
results = sortedByquantity.collect()

# Print out the results
for result in results:
    print(result)


from flask import Blueprint, jsonify, request
from database_manager import DatabaseManager
from person_service import PersonService
from random_user_api import RandomUserApi
from pony.orm import *

person_api_blueprint = Blueprint('person_api', __name__)
db = DatabaseManager().initialize_database()
person_service = PersonService(db)

@person_api_blueprint.route('/api/persons', methods=['POST'])
def add_new_person():
    json = request.get_json()
    new_person = person_service.add_person(dict(first_name=json['first_name'], last_name=json['last_name']))
    response = jsonify(new_person.to_dict())
    response.status_code = 201
    return response

@person_api_blueprint.route('/api/persons/<int:person_id>', methods=['DELETE'])
def remove_person(person_id):
    try:
        person_service.remove_person(person_id)
        return ('', 204)
    except ObjectNotFound:
        return ('', 404)

@person_api_blueprint.route('/api/persons/<int:person_id>', methods=['GET'])
def get_person(person_id):
    person = person_service.get_person_by_id(person_id)
    if person:
        response = jsonify(person.to_dict())
        response.status_code = 200
        return response
    else:
        return ('', 404)

@person_api_blueprint.route('/api/persons/list', methods=['GET'])
def list_all_persons():
    all_persons = person_service.list_all_persons()
    all_persons = map(lambda x:x.to_dict(), all_persons)
    response = jsonify(all_persons)
    response.status_code = 200
    return response

@person_api_blueprint.route('/api/persons/import/random', methods=['POST'])
def import_random_person():
    random_person = RandomUserApi().get_random_person()
    new_person = person_service.add_person(random_person)
    response = jsonify(new_person.to_dict())
    response.status_code = 201
    return response


"""alter column webhook.is_successed -> is_existed

Revision ID: 9ad21de124d0
Revises: 275d36156c6d
Create Date: 2021-04-29 08:28:05.630568

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '9ad21de124d0'
down_revision = '275d36156c6d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('webhook', sa.Column('is_existed', sa.Boolean(), nullable=True))
    op.drop_column('webhook', 'is_successed')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('webhook', sa.Column('is_successed', sa.BOOLEAN(), autoincrement=False, nullable=True))
    op.drop_column('webhook', 'is_existed')
    # ### end Alembic commands ###