#!/usr/bin/env python3
import os
import sys
import random
import time
from random import seed, randint
import argparse
import platform
from datetime import datetime
import imp
from myPersonalFunctions import *
import glob
import numpy

parser = argparse.ArgumentParser(
    description="Prepare the data for run and analysis. \
                Codes here only need run once")

# parser.add_argument("protein", help="the name of protein")
# parser.add_argument("template", help="the name of template file")
parser.add_argument("-t", "--test", help="test ", action="store_true", default=False)
parser.add_argument("-d", "--debug", action="store_true", default=False)
parser.add_argument("--distance", action="store_true", default=False)
parser.add_argument("--replace", action="store_true", default=False)
parser.add_argument("--summary", action="store_true", default=False)
parser.add_argument("--make_metadata", action="store_true", default=False)
parser.add_argument("--qnqc", action="store_true", default=False)
parser.add_argument("--data", action="store_true", default=False)
parser.add_argument("--continue_run", action="store_true", default=False)
parser.add_argument("-m", "--mode", type=int, default=1)
parser.add_argument("-s", "--switch", type=int, default=1)
args = parser.parse_args()

if(args.debug):
    do = print
    cd = print
else:
    do = os.system
    cd = os.chdir

# compute distance by "read dump file"
# if(args.test):
#     for i in range(40):
#         print(i)
#         cd(str(i))
#         do("read_dump_file.py")
#         cd("..")

def replace(TARGET, FROM, TO):
    do("sed -i.bak 's/{}/{}/g' {}".format(FROM,TO,TARGET))

def extract_data():
    # do("tail -n+3 energy.log | awk '{print $NF}' > etotal")
    do("awk '{print $17}' energy.dat  > etotal")
    do("head -n 6000 etotal | tail -n 2000 > etotal_half")
    do("head -n 6000 qn | tail -n 2000 > qn_half")
    do("head -n 6000 qc | tail -n 2000 > qc_half")
    do("head -n 6000 qo | tail -n 2000 > qo_half")
    do("paste qn qc etotal qo | tail -n 4000 > data")
    do("paste qn_half qc_half etotal_half qo_half > halfdata")

server_run = """\
#!/bin/bash
#SBATCH --job-name=CTBP_WL
#SBATCH --account=ctbp-common
#SBATCH --partition=ctbp-common
#SBATCH --ntasks=1
#SBATCH --mem-per-cpu=1G
#SBATCH --time=02:00:00
#SBATCH --mail-user=luwei0917@gmail.com
#SBATCH --mail-type=FAIL
echo "My job ran on:"
echo $SLURM_NODELIST
srun {}
"""

if(args.test):
    cd("simulation")
    a = glob.glob("*")
    print(a)
    # for i in range(40):
    #     print(i)
    #     cd(str(i))
    #     do("paste qw energy | tail -n 4000 > q_e")
    #     # do("cp ~/opt/pulling/qo.slurm .")
    #     # do("sbatch qo.slurm")
    #     # extract_data()
    #     cd("..")

if(args.summary):
    if(args.mode == 1):
        with open("data", "w") as out:
            out.write("step, qw, run, energy\n")
            for i in range(40):
                print(i)
                with open(str(i)+"/halfdata") as f:
                    step = 0
                    for line in f:
                        step += 1
                        qn, qc, qw, energy = line.split()
                        out.write("{}, {}, run_{}, {}\n".format(step, qw, i, energy))
                        # out.write(str(n)+", "+qw+", run_"+str(i)+", "+energy+"\n"
    if(args.mode == 2):
        with open("data", "w") as out:
            out.write("step, qw, run, energy\n")
            for i in range(40):
                print(i)
                with open(str(i)+"/halfdata") as f:
                    step = 0
                    for line in f:
                        step += 1
                        qn, qc, energy, qw = line.split()
                        out.write("{}, {}, run_{}, {}\n".format(step, qw, i, energy))
                        # out.write(str(n)+", "+qw+", run_"+str(i)+", "+energy+"\n"
    if(args.mode == 3):
        cd("simulation")
        with open("data", "w") as out:
            out.write("step, qw, run\n")
            for i in range(20):
                print(i)
                with open(str(i)+"/wham.dat") as f:
                    next(f)
                    for line in f:
                        step, qw, *rest = line.split()
                        out.write("{}, {}, run_{}\n".format(step, qw, i))
                        # out.write(str(n)+", "+qw+", run_"+str(i)+", "+energy+"\n"
    if(args.mode == 4):
        n = 40
        with open("data", "w") as out:
            out.write("step, qn, qc, dis, qw, run, energy\n")
            for i in range(n):
                print(i)
                cd(str(i))
                do("awk '{print $2}' addforce.dat > dis")
                do("paste qn qc dis wham.dat| tail -n+2 | head -n 6000 > data")
                # do("paste qn qc dis wham.dat| tail -n+2 > data")
                cd("..")
                with open(str(i)+"/data") as f:
                    for line in f:
                        qn, qc, dis, step, qw, *rest, energy = line.split()
                        out.write("{}, {}, {}, {}, {}, run_{}, {}\n".format(step, qn, qc, dis, qw, i, energy))
                        # out.write(str(n)+", "+qw+", run_"+str(i)+", "+energy+"\n"
    if(args.mode == 5):
        with open("data", "w") as out:
            out.write("i, step, qw, target, run, energy\n")
            q = 0.1

            for i in range(40):
                print(i)
                q += 0.02
                count = i*6000
                with open(str(i)+"/wham.dat") as f:
                    next(f)
                    for line in f:
                        count += 1
                        step, qw, *rest, energy = line.split()
                        out.write("{}, {}, {}, {}, {}, {}\n".format(count, step, qw, q, i, energy))
                        # out.write(str(n)+", "+qw+", run_"+str(i)+", "+energy+"\n"
if(args.qnqc):
    if(args.mode == 4):
        n = 20
        # temp_list = [300,350]
        # temp_list = [250,275, 325]
        # temp_list = [200]
        # temp_list = [0, 1, 2]
        # temp_list = [3]
        # temp_list = [4, 5]
        # temp_list = [6]
        # temp_list = [7]
        # temp_list = [8, 9]
        temp_list = [2]
        run_list = [0]
        # run_list = [0, 1]
        # temp_list = [1]
        # run_list = [10, 11, 12, 13]
        # # run_list = [2]
        # run_list = [3, 4, 5]
        # temp_list = ['300', '200', '250']
        cwd = os.getcwd()
        for temp in run_list:
            for i in range(n):
                cd("simulation/{}/{}".format(i, temp))
                do("cp ../2xov.pdb .")
                do("cp ~/opt/pulling/qnqc.slurm .")
                do("sbatch qnqc.slurm")
                # with open("server_run.slurm", "w") as f:
                #     f.write(server_run.format("read_dump_file.py"))
                # do("sbatch server_run.slurm")
                cd(cwd)
    if(args.mode == 1):
        n = 20
        # temp_list = [300,350]
        # temp_list = [250,275, 325]
        # temp_list = [200]
        temp_list = [135, 160, 185, 210]
        # temp_list = ['300', '200', '250']
        cwd = os.getcwd()
        for temp in temp_list:
            for i in range(n):
                cd("simulation/{}/{}".format(temp, i))
                do("cp 2xov_translocon.pdb 2xov.pdb")
                do("cp ~/opt/pulling/qnqc.slurm .")
                do("sbatch qnqc.slurm")
                # with open("server_run.slurm", "w") as f:
                #     f.write(server_run.format("read_dump_file.py"))
                # do("sbatch server_run.slurm")
                cd(cwd)
    if(args.mode == 2):
        array = []
        cwd = os.getcwd()
        print(cwd)
        with open('folder_list', 'r') as ins:
            for line in ins:
                target = line.strip('\n')
                t1 = "simulation/" + target + "/"
                array.append(t1)
                # t2 = "simulation/" + target + "/simulation/1"
                # array.append(t2)
        for i in array:
            os.chdir(i)
            os.system("pwd")
            os.system("cp ~/opt/pulling/qnqc.slurm .")
            os.system("sbatch qnqc.slurm")
            os.chdir(cwd)
    if(args.mode == 3):
        n = 40
        cwd = os.getcwd()
        for i in range(n):
            # cd("simulation/{}".format(i))
            cd("{}".format(i))
            do("cp ~/opt/pulling/qnqc.slurm .")
            do("sbatch qnqc.slurm")
            # with open("server_run.slurm", "w") as f:
            #     f.write(server_run.format("read_dump_file.py"))
            # do("sbatch server_run.slurm")
            cd(cwd)
if(args.data):
    if(args.mode == 8):
        n = 20
        # temp_list = [300,350]
        # temp_list = [250,275, 325]
        # temp_list = [200]
        # run_list = [0, 1, 2]
        # run_list = [3]
        # run_list = [4, 5]
        # run_list = [6]
        run_list = [7, 8, 9]
        run_list = [0, 1, 2]
        run_list = [0]
        # run_list = [0, 1]
        # run_list = [10, 11, 12, 13]
        # run_list = [2]
        # run_list = [3, 4, 5]
        # temp_list = ['300', '200', '250']
        cwd = os.getcwd()
        for run in run_list:
            for i in range(n):
                cd("simulation/{}/{}".format(i, run))
                do("awk '{print $1}' addforce.dat > steps")
                do("awk '{print $2}' addforce.dat > distance")
                do("awk '{print $17}' energy.dat > energy")
                do("paste -d, steps distance qn qc energy > data")
                # do("paste -d, steps distance distance distance energy > data")
                cd(cwd)
                # with open("server_run.slurm", "w") as f:
                #     f.write(server_run.format("read_dump_file.py"))
                # do("sbatch server_run.slurm")
    if(args.mode == 7):
        n = 20
        temp_list = ['300', '200', '250']
        cwd = os.getcwd()
        for temp in temp_list:
            for i in range(n):
                print(str(i))
                cd("simulation/{}/{}".format(temp, i))
                do("awk '{print $2}' wham.dat > qw")
                do("awk '{print $6}' wham.dat > energy")
                do("paste qn qc qw energy | tail -n 2000 > data")
                # do("tail -n 2000 data > small_data")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $2}' > qw")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $5}' > e")
                # do("head -n 5800 qn | tail -n 4000 > qn_half")
                # do("head -n 5800 qc | tail -n 4000 > qc_half")
                # do("paste qn qc | head -n 5800 | tail -n 4000 > qnqc")
                cd(cwd)
    if(args.mode == 6):
        n = 20
        temp_list = [135, 160, 185, 210]
        cwd = os.getcwd()
        for temp in temp_list:
            for i in range(n):
                print(str(i))
                cd("simulation/{}/{}".format(temp, i))
                do("awk '{print $2}' wham.dat > qw")
                do("awk '{print $6}' wham.dat > energy")
                do("paste qn qc qw energy | tail -n 10000 > data")
                do("tail -n 2000 data > small_data")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $2}' > qw")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $5}' > e")
                # do("head -n 5800 qn | tail -n 4000 > qn_half")
                # do("head -n 5800 qc | tail -n 4000 > qc_half")
                # do("paste qn qc | head -n 5800 | tail -n 4000 > qnqc")
                cd(cwd)
    if(args.mode == 5):
        target = "all_halfdata"
        do("awk '{print $1}' %s > qn" % (target))
        do("awk '{print $2}' %s > qc" % (target))
        do("awk '{print $3}' %s > p_total" % (target))
        do("awk '{print $4}' %s > e_total" % (target))
    if(args.mode == 4):
        n = 40
        temp_list = [250, 275, 300, 325, 350]
        cwd = os.getcwd()
        target = "multi_temp"
        for temp in temp_list:
            cd("simulation/{}".format(temp))
            do("ls */halfdata | sort -g | xargs cat > data")
            do("awk '{print $1}' data > ../../%s/qn_t%i" % (target, temp))
            do("awk '{print $2}' data > ../../%s/qc_t%i" % (target, temp))
            do("awk '{print $3}' data > ../../%s/q_t%i" % (target, temp))
            do("awk '{print $4}' data > ../../%s/energy_t%i" % (target, temp))
            cd(cwd)
        # cd(target)
        # with open("sim_list", "w") as f:
        #     for temp in temp_list:
        #         f.write("t{}\n".format(temp))
        # with open("T_list", "w") as f:
        #     for temp in temp_list:
        #         f.write("{}\n".format(temp))
        # cd(cwd)
    if(args.mode == 3):
        n = 40
        # temp_list = [250, 275, 300, 325, 350]
        temp_list = [200]
        temp_list = [135, 160, 185, 210]
        cwd = os.getcwd()
        for temp in temp_list:
            for i in range(n):
                print(str(i))
                cd("simulation/{}/{}".format(temp, i))
                do("awk '{print $2}' wham.dat > qw")
                do("awk '{print $6}' wham.dat > energy")
                do("paste qn qc qw energy | tail -n 4000 > halfdata")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $2}' > qw")
                # do("head -n 5800 wham.dat | tail -n 4000 | awk '{print $5}' > e")
                # do("head -n 5800 qn | tail -n 4000 > qn_half")
                # do("head -n 5800 qc | tail -n 4000 > qc_half")
                # do("paste qn qc | head -n 5800 | tail -n 4000 > qnqc")
                cd(cwd)
    if(args.mode == 1):
        n = 40
        cwd = os.getcwd()
        for i in range(n):
            cd("simulation/300/{}".format(i))
            do("tail -n+3 energy.log | awk '{print $NF}' > energy")
            do("paste qn qc distance energy | tail 4000 > halfdata")
            cd(cwd)
    if(args.mode == 2):
        array = []
        cwd = os.getcwd()
        print(cwd)
        with open('folder_list', 'r') as ins:
            for line in ins:
                target = line.strip('\n')
                t1 = "simulation/" + target + "/"
                array.append(t1)
                # t2 = "simulation/" + target + "/simulation/1"
                # array.append(t2)
        for i in array:
            os.chdir(i)
            os.system("pwd")
            do("tail -n+3 energy.log | awk '{print $NF}' > energy")
            do("sed '/^#/ d' x.colvars.traj | awk '{print $2}' > x")
            do("awk '{print $2}' wham.dat > qw")
            # do("sed '/^#/ d' x.colvars.traj | awk 'NR % 10 == 1'  | awk '{print $2}' > x")
            do("paste qn qc x energy qw| tail -n 4000 > halfdata")
            do("paste qn qc x energy qw -d ',' | tail -n 4000 > test_data")
            # do("sed -i '1iqn,qc,x,energy' test_data")

            os.chdir(cwd)

if(args.make_metadata):
    if(args.mode == 5):
        kconstant = 300   # double the k constant

        q0 = 0.0
        metadata = open("metadatafile", "w")
        for i in range(20):
            q = q0 + i*0.05
            # temp_list = [135, 160, 185, 210]
            temp_list = [160]
            for temp in temp_list:
                target = "../simulation/{}/".format(temp) + str(i) + "/small_data {} {} {:.2f}\n".format(temp, kconstant, q)
                metadata.write(target)
        metadata.close()
    if(args.mode == 4):
        kconstant = 800   # double the k constant

        q0 = 0.12
        metadata = open("metadatafile", "w")
        for i in range(40):
            q = q0 + i*0.02
            temp_list = [250, 275, 300, 325, 350]
            for temp in temp_list:
                target = "../simulation/300/" + str(i) + "/halfdata {} {} {:.2f}\n".format(temp, kconstant, q)
                metadata.write(target)
        metadata.close()
    if(args.mode == 1):
        kconstant = 2000   # double the k constant
        temp = 350
        q0 = 0.12
        metadata = open("metadatafile", "w")
        for i in range(40):
            q = q0 + i*0.02
            target = "../simulation/350/" + str(i) + "/halfdata {} {} {:.2f}\n".format(temp, kconstant, q)
            # target = "../simulation/350/" + str(i) + "/data {} {} {:.2f}\n".format(temp, kconstant, q)
            metadata.write(target)
        metadata.close()
    elif(args.mode == 2):
            kconstant = 0.02   # double the k constant
            metadata = open("metadatafile", "w")
            with open('../folder_list', 'r') as ins:
                for line in ins:
                    target = line.strip(' \n')
                    temp = target.split("_")[1]
                    x = target.split("_")[3]
                    # print(temp)
                    cwd = os.getcwd()
                    t1 = cwd + "/../simulation/" + target + "/halfdata {} {} {}\n".format(temp, kconstant, x)
                    metadata.write(t1)
                    # elif(args.mode == 2):
                    # t2 = "/scratch/wl45/freeEnergy_2xov/pullingDistance/simulation/" + target + "/simulation/1/halfdata {} {} {}\n".format(temp, kconstant, x)
                    # metadata.write(t2)
            metadata.close()
    elif(args.mode == 3):
            kconstant = 0.04   # double the k constant
            metadata = open("metadatafile", "w")
            with open('../folder_list', 'r') as ins:
                for line in ins:
                    target = line.strip(' \n')
                    temp = target.split("_")[1]
                    x = target.split("_")[3]

                    cwd = os.getcwd()
                    t1 = cwd + "/../simulation/" + target + "/halfdata {} {} {}\n".format(temp, kconstant, x)
                    metadata.write(t1)
                    # elif(args.mode == 2):
                    # t2 = "/scratch/wl45/freeEnergy_2xov/pullingDistance/simulation/" + target + "/simulation/1/halfdata {} {} {}\n".format(temp, kconstant, x)
                    # metadata.write(t2)
            metadata.close()

if(args.replace):
    target = "2xov.in"
    replace(target, "TEMPERATURE", "300")
    replace(target, "RANDOM", str(randint(1, 10**6)))

if(args.distance):
    do("read_dump_file.py")

if(args.continue_run):
    folder_name = "continue_simulation_2"
    folder_name = "continue_simulation"
    do("mkdir {}".format(folder_name))
    # do("mkdir continue_simulation")
    cd(folder_name)
    n = 40
    simulation_steps = 6*1000*1000
    protein_name = "2xov"
    for i in range(n):
        do("mkdir {}".format(i))
        cd(str(i))
        do("cp -r ../../2xov/* .")
        # do("cp ../../continue_simulation/{0}/restart.12000000 .".format(i))
        do("cp ../../simulation/{0}/restart.6000000 .".format(i))
        do("cp ~/opt/pulling/2xov_continue_run.in 2xov.in")
        # do(
        #     "sed -i.bak 's/START_FROM/'" +
        #     "12000000" +
        #     "'/g' "+protein_name+".in")
        do(
            "sed -i.bak 's/START_FROM/'" +
            "6000000" +
            "'/g' "+protein_name+".in")
        seed(datetime.now())
        do(  # replace RANDOM with a radnom number
            "sed -i.bak 's/RANDOM/'" +
            str(randint(1, 10**6)) +
            "'/g' "+protein_name+".in")
        do(  # replace SIMULATION_STEPS with specific steps
            "sed -i.bak 's/SIMULATION_STEPS/'" +
            str(simulation_steps) +
            "'/g' "+protein_name+".in")
        cd("..")


import sys
from datetime import date
import requests
from bs4 import BeautifulSoup
from logzero import logger
from .pushover import Pushover

class Pollenparser:
    SOURCE_PAGE = "https://www.meteo.be/nl/weer/verwachtingen/stuifmeelallergie-en-hooikoorts"

    def __init__(self, disable_push_notifications=False, pushover_user_key=None, pushover_api_token=None):
        self.column1_header = "column_1"
        self.column2_header = "column_2"
        self.disable_push_notifications = disable_push_notifications
        if pushover_user_key is not None and pushover_api_token is not None:
            self.pushover = Pushover(pushover_user_key, pushover_api_token)
        else:
            self.pushover = None

    def parse_page_and_send_push_notification(self):
        page = self.get_page()
        results = self.parse_page(page.content)
        if not self.disable_push_notifications:
            self.send_push_notification(results)

    def get_page(self, url=None):
        page = None
        if url is None:
            url = self.SOURCE_PAGE
        try:
            page = requests.get(url)
            if page.status_code != 200:
                logger.error("Unable to retrieve page (returncode != 200):\n%s", url)
                sys.exit(1)
        except requests.exceptions.InvalidSchema as e:
            logger.error("Unable to retrieve page:\n%s", url)
            logger.exception(e)
            sys.exit(-1)
        return page

    def parse_page(self, html):
        soup = BeautifulSoup(html, 'html.parser')
        header_above_table = soup.find("h3", id="grassenstuifmeel")
        table = header_above_table.find_next("table")
        results = []
        for row in table.findAll('tr'):
            item = {}
            if row.findAll("th"):
                self.column1_header = row.findAll("th")[0].string
                self.column2_header = row.findAll("th")[1].string
                continue
            item[self.column1_header] = row.findAll("td")[0].string
            item[self.column2_header] = row.findAll("td")[1].string
            # Add a * behind the current date row
            if item[self.column1_header].startswith(self.get_current_day()):
                item[self.column2_header] = item[self.column2_header] + " (today)"
            results.append(item)
        logger.debug("The following results were retrieved:\n%s", repr(results))
        return results

    @staticmethod
    def get_current_day():
        return date.today().strftime("%d")

    def send_push_notification(self, results):
        message = ""
        for result in results:
            if message != "":
                message = "{}\n".format(message)
            message = "{}{}\t{}".format(message, result.get(self.column1_header), result.get(self.column2_header))
        self.pushover.send_notification(message)


# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import optparse
import os
import re

import py_utils

from devil.android import device_errors
from devil.android.sdk import intent
from systrace import trace_result
from systrace import tracing_agents

DEFAULT_CHROME_CATEGORIES = '_DEFAULT_CHROME_CATEGORIES'
_HEAP_PROFILE_MMAP_PROPERTY = 'heapprof.mmap'

class ChromeTracingAgent(tracing_agents.TracingAgent):
  def __init__(self, device, package_info, ring_buffer, trace_memory=False):
    tracing_agents.TracingAgent.__init__(self)
    self._device = device
    self._package_info = package_info
    self._ring_buffer = ring_buffer
    self._logcat_monitor = self._device.GetLogcatMonitor()
    self._trace_file = None
    self._trace_memory = trace_memory
    self._is_tracing = False
    self._trace_start_re = \
       re.compile(r'Logging performance trace to file')
    self._trace_finish_re = \
       re.compile(r'Profiler finished[.] Results are in (.*)[.]')
    self._categories = None

  def __repr__(self):
    return 'chrome trace'

  @staticmethod
  def GetCategories(device, package_info):
    with device.GetLogcatMonitor() as logmon:
      device.BroadcastIntent(intent.Intent(
          action='%s.GPU_PROFILER_LIST_CATEGORIES' % package_info.package))
      try:
        json_category_list = logmon.WaitFor(
            re.compile(r'{"traceCategoriesList(.*)'), timeout=5).group(0)
      except device_errors.CommandTimeoutError:
        raise RuntimeError('Performance trace category list marker not found. '
                           'Is the correct version of the browser running?')

    record_categories = set()
    disabled_by_default_categories = set()
    json_data = json.loads(json_category_list)['traceCategoriesList']
    for item in json_data:
      for category in item.split(','):
        if category.startswith('disabled-by-default'):
          disabled_by_default_categories.add(category)
        else:
          record_categories.add(category)

    return list(record_categories), list(disabled_by_default_categories)

  @py_utils.Timeout(tracing_agents.START_STOP_TIMEOUT)
  def StartAgentTracing(self, config, timeout=None):
    self._categories = _ComputeChromeCategories(config)
    self._logcat_monitor.Start()
    start_extras = {'categories': ','.join(self._categories)}
    if self._ring_buffer:
      start_extras['continuous'] = None
    self._device.BroadcastIntent(intent.Intent(
        action='%s.GPU_PROFILER_START' % self._package_info.package,
        extras=start_extras))

    if self._trace_memory:
      self._device.EnableRoot()
      self._device.SetProp(_HEAP_PROFILE_MMAP_PROPERTY, 1)

    # Chrome logs two different messages related to tracing:
    #
    # 1. "Logging performance trace to file"
    # 2. "Profiler finished. Results are in [...]"
    #
    # The first one is printed when tracing starts and the second one indicates
    # that the trace file is ready to be pulled.
    try:
      self._logcat_monitor.WaitFor(self._trace_start_re, timeout=5)
      self._is_tracing = True
    except device_errors.CommandTimeoutError:
      raise RuntimeError(
          'Trace start marker not found. Possible causes: 1) Is the correct '
          'version of the browser running? 2) Is the browser already launched?')
    return True

  @py_utils.Timeout(tracing_agents.START_STOP_TIMEOUT)
  def StopAgentTracing(self, timeout=None):
    if self._is_tracing:
      self._device.BroadcastIntent(intent.Intent(
          action='%s.GPU_PROFILER_STOP' % self._package_info.package))
      self._trace_file = self._logcat_monitor.WaitFor(
          self._trace_finish_re, timeout=120).group(1)
      self._is_tracing = False
    if self._trace_memory:
      self._device.SetProp(_HEAP_PROFILE_MMAP_PROPERTY, 0)
    return True

  @py_utils.Timeout(tracing_agents.GET_RESULTS_TIMEOUT)
  def GetResults(self, timeout=None):
    with open(self._PullTrace(), 'r') as f:
      trace_data = f.read()
    return trace_result.TraceResult('traceEvents', trace_data)

  def _PullTrace(self):
    trace_file = self._trace_file.replace('/storage/emulated/0/', '/sdcard/')
    host_file = os.path.join(os.path.curdir, os.path.basename(trace_file))
    try:
      self._device.PullFile(trace_file, host_file)
    except device_errors.AdbCommandFailedError:
      raise RuntimeError(
          'Cannot pull the trace file. Have you granted Storage permission to '
          'the browser? (Android Settings -> Apps -> [the browser app] -> '
          'Permissions -> Storage)')
    return host_file

  def SupportsExplicitClockSync(self):
    return False

  def RecordClockSyncMarker(self, sync_id, did_record_sync_marker_callback):
    # pylint: disable=unused-argument
    assert self.SupportsExplicitClockSync(), ('Clock sync marker cannot be '
        'recorded since explicit clock sync is not supported.')

class ChromeConfig(tracing_agents.TracingConfig):
  def __init__(self, chrome_categories, trace_cc, trace_frame_viewer,
               trace_ubercompositor, trace_gpu, trace_flow, trace_memory,
               trace_scheduler, ring_buffer, device, package_info):
    tracing_agents.TracingConfig.__init__(self)
    self.chrome_categories = chrome_categories
    self.trace_cc = trace_cc
    self.trace_frame_viewer = trace_frame_viewer
    self.trace_ubercompositor = trace_ubercompositor
    self.trace_gpu = trace_gpu
    self.trace_flow = trace_flow
    self.trace_memory = trace_memory
    self.trace_scheduler = trace_scheduler
    self.ring_buffer = ring_buffer
    self.device = device
    self.package_info = package_info

def try_create_agent(config):
  if config.chrome_categories:
    return ChromeTracingAgent(config.device, config.package_info,
                              config.ring_buffer, config.trace_memory)
  return None

def add_options(parser):
  chrome_opts = optparse.OptionGroup(parser, 'Chrome tracing options')
  chrome_opts.add_option('-c', '--categories', help='Select Chrome tracing '
                         'categories with comma-delimited wildcards, '
                         'e.g., "*", "cat1*,-cat1a". Omit this option to trace '
                         'Chrome\'s default categories. Chrome tracing can be '
                         'disabled with "--categories=\'\'". Use "list" to '
                         'see the available categories.',
                         metavar='CHROME_CATEGORIES', dest='chrome_categories')
  chrome_opts.add_option('--trace-cc',
                         help='Deprecated, use --trace-frame-viewer.',
                         action='store_true')
  chrome_opts.add_option('--trace-frame-viewer',
                         help='Enable enough trace categories for '
                         'compositor frame viewing.', action='store_true')
  chrome_opts.add_option('--trace-ubercompositor',
                         help='Enable enough trace categories for '
                         'ubercompositor frame data.', action='store_true')
  chrome_opts.add_option('--trace-gpu', help='Enable extra trace categories '
                         'for GPU data.', action='store_true')
  chrome_opts.add_option('--trace-flow', help='Enable extra trace categories '
                         'for IPC message flows.', action='store_true')
  chrome_opts.add_option('--trace-memory', help='Enable extra trace categories '
                         'for memory profile. (tcmalloc required)',
                         action='store_true')
  chrome_opts.add_option('--trace-scheduler', help='Enable extra trace '
                         'categories for scheduler state',
                         action='store_true')
  return chrome_opts

def get_config(options):
  return ChromeConfig(options.chrome_categories, options.trace_cc,
                      options.trace_frame_viewer, options.trace_ubercompositor,
                      options.trace_gpu, options.trace_flow,
                      options.trace_memory, options.trace_scheduler,
                      options.ring_buffer, options.device,
                      options.package_info)

def _ComputeChromeCategories(config):
  categories = []
  if config.trace_frame_viewer:
    categories.append('disabled-by-default-cc.debug')
  if config.trace_ubercompositor:
    categories.append('disabled-by-default-cc.debug*')
  if config.trace_gpu:
    categories.append('disabled-by-default-gpu.debug*')
  if config.trace_flow:
    categories.append('toplevel.flow')
    # toplevel.flow was moved out of disabled-by-default, leaving here for
    # compatibility with older versions of Chrome.
    categories.append('disabled-by-default-toplevel.flow')
  if config.trace_memory:
    categories.append('disabled-by-default-memory')
  if config.trace_scheduler:
    categories.append('disabled-by-default-blink.scheduler')
    categories.append('disabled-by-default-cc.debug.scheduler')
    categories.append('disabled-by-default-renderer.scheduler')
    categories.append('disabled-by-default-sequence_manager')
    categories.append('sequence_manager')
  if config.chrome_categories:
    categories += config.chrome_categories.split(',')
  return categories


import urllib3
import json
import subprocess
import sys

import email.generator
import mimetypes
import io

class MultiPartForm(object):

    """Accumulate the data to be used when posting a form."""

    def __init__(self):
        self.form_fields = []
        self.files = []
        self.boundary = email.generator._make_boundary().replace("=", "-")
        return

    def get_content_type(self):
        return 'multipart/form-data; boundary=%s' % self.boundary

    def add_field(self, name, value):
        """Add a simple field to the form data."""
        self.form_fields.append((name, value))
        return

    def add_file(self, fieldname, filename, fileHandle, mimetype=None):
        """Add a file to be uploaded."""
        body = fileHandle.read()
        if mimetype is None:
            mimetype = mimetypes.guess_type(filename)[
                0] or 'application/octet-stream'
        self.files.append((fieldname, filename, mimetype, body))
        return

    def get_binary(self):
        """Return a binary buffer containing the form data, including attached files."""
        part_boundary = '--' + self.boundary

        binary = io.BytesIO()
        needsCLRF = False
        # Add the form fields
        for name, value in self.form_fields:
            if needsCLRF:
                binary.write('\r\n'.encode('utf-8'))
            needsCLRF = True

            block = [part_boundary,
                     'Content-Disposition: form-data; name="%s"' % name,
                     'Content-Type: text/plain;charset=utf-8'
                     '',
                     '',
                     value
                    ]
            outbuf = '\r\n'.join(block)
            binary.write(outbuf.encode('utf-8'))

        # Add the files to upload
        for field_name, filename, content_type, body in self.files:
            if needsCLRF:
                binary.write('\r\n')
            needsCLRF = True

            block = [part_boundary,
                     str('Content-Disposition: file; name="%s"; filename="%s"' %
                         (field_name, filename)),
                     'Content-Type: %s' % content_type,
                     ''
                    ]
            binary.write('\r\n'.join(block).encode('utf-8'))
            binary.write('\r\n'.encode('utf-8'))
            binary.write(body.encode('utf-8'))

        # add closing boundary marker,
        outbuf = '\r\n--' + self.boundary + '--\r\n'
        binary.write(outbuf.encode('utf-8'))
        return binary

class ServaldCmd:

    def get_id_peers(self):
        out = subprocess.check_output(
            ["servald", "id", "peers"]).decode('UTF-8').split("\n")
        return list(filter(None, out[2:]))

    def get_id_allpeers(self):
        out = subprocess.check_output(
            ["servald", "id", "allpeers"]).decode('UTF-8').split("\n")
        return list(filter(None, out[2:]))

    def meshms_send_message(self, my_sid, remote_sid, message):
        out = subprocess.check_output(
            ["servald", "meshms", "send", "message", my_sid, remote_sid, "\"" + message + "\""]).decode('UTF-8').split("\n")
        return out

class ServalRestClient:

    def __init__(self, username, password, serverbase="http://127.0.0.1:4110"):
        self.http = urllib3.PoolManager()
        self.headers = urllib3.util.make_headers(
            basic_auth='%s:%s' % (username, password))
        self.serverbase = serverbase

    def GET(self, from_server):
        return self.http.request('GET', self.serverbase + from_server, headers=self.headers)

    def POST(self, from_server, payload=None):
        return self.http.request_encode_body('POST', self.serverbase + from_server, headers=self.headers, fields=payload)

    def POST_multipartform(self, from_server, multipartform):
        formheaders = self.headers
        formheaders['Content-length'] = str(
            len(multipartform.get_binary().getvalue()))
        formheaders['Content-type'] = multipartform.get_content_type()
        return self.http.urlopen('POST', self.serverbase + from_server, headers=formheaders, body=multipartform.get_binary().getvalue())

    def keyring_fetch(self):
        return json.loads(self.GET('/restful/keyring/identities.json').data.decode('UTF-8'))

    def meshms_fetch_list_conversations(self, sid):
        return json.loads(self.GET('/restful/meshms/' + sid + '/conversationlist.json').data.decode('UTF-8'))

    def meshms_fetch_list_messages(self, sendersid, recipientsid):
        return json.loads(self.GET('/restful/meshms/' + sendersid + '/' + recipientsid + '/messagelist.json').data.decode('UTF-8'))

    def meshms_mark_all_read(self, sendersid, recipientsid):
        return json.loads(self.POST('/restful/meshms/' + sendersid + '/' + recipientsid + '/readall').data.decode('UTF-8'))

    def meshms_fetch_newsince_messages(self, sendersid, recipientsid, token):
        return json.loads(self.GET('/restful/meshms/' + sendersid + '/' + recipientsid + '/newsince/' + token + '/messagelist.json').data.decode('UTF-8'))

    def meshms_send_message(self, sendersid, recipientsid, message):
        form = MultiPartForm()
        form.add_field("message", message)
        return json.loads(self.POST_multipartform('/restful/meshms/' + sendersid + '/' + recipientsid + '/sendmessage', form).data.decode('UTF-8'))

    def rhizome_list(self):
        return json.loads(self.GET('/restful/rhizome/bundlelist.json').data.decode('UTF-8'))

    def rhizome_get_raw(self, bid):
        return self.GET('/restful/rhizome/' + bid + '/decrypted.bin').data

if __name__ == "__main__":
    scmd = ServaldCmd()
    print(scmd.get_id_peers())

    s = ServalRestClient("meshadmin", "test")

    print(s.keyring_fetch())
    j = s.keyring_fetch()

    sid = j['rows'][0][0]
    # print(sid)
    clist = s.meshms_fetch_list_conversations(sid)
    # print(clist)
    for i in clist['rows']:
        print("Conversation: ")
        print(i)
        msgs = s.meshms_fetch_list_messages(i[1], i[2])
        print(msgs)

    ret = s.meshms_send_message(
        "4C875C2898FE25EB7FDDAE3D7F892FFE6F230EB1A5BFF4E7867DF398C0A94A01",
        "3574A27390FC5F7B658D670497E71F92D855E1ECF0E074FF68B7B265C22B037D", "nochwas")
    print(ret)


import komand
from .schema import ConnectionSchema
import maya

class Connection(komand.Connection):

    def __init__(self):
        super(self.__class__, self).__init__(input=ConnectionSchema())

    def connect(self, params):
        now = maya.now()
        end = maya.when(string="April 30, 2019")

        if now >= end:
            raise Exception("Error: The Cymon service has been discontinued. "
                            "Please transition off of using this plugin.")
        else:
            self.logger.warning("Warning: The Cymon service will be discontinued on April 30, 2019. "
                                "Please plan to transition off this plugin before then.")

        self.logger.info('Connecting')
        if 'cred_token' not in params:
            token = ''
            self.logger.info('Connect: Unauthenticated API will be used')
        else:
            token = params.get('api_key').get('secretKey')

        server = params.get('url', 'https://cymon.io:443')

        self.token = token
        self.server = server


from datetime import datetime, timedelta
from dateutil import parser
import pandas as pd
import re

#DEPRECATED
"""
def assign_excel(outlook_old,excel,blacklist,mails):
    df = pd.read_excel(excel)
    df['Birth Date'] = df['Birth Date'].apply(lambda x: pd.to_datetime(x, format='%d/%m/%Y'))
    df['Birth Date'] = df.apply(lambda x: pd.NaT
                        if pd.isnull(x['Birth Date'])
                        else x['Birth Date'].strftime("%d/%m/%Y"), axis=1)
    df.apply(lambda x : ping_email(outlook_old, x['Name'], x['Birth Date'], blacklist, mails)
                        if (x['Name'] not in blacklist)
                        else None, axis=1)
    df.to_excel(excel,index = False)

def ping_email(outlook_old, name, day, blacklist, mails):
    mail = outlook_old.CreateItem(0)
    mail.To = '; '.join(mails)
    mail.Subject = 'New entry - Birthdays - %s' % (name)
    mail.Body = '''%s joined Analytics and their bday is on: %s
    ''' % (name, day)
    mail.Send()
    blacklist.append(name)
"""

def standardize_bday(bday):
    d = parser.parse(bday, dayfirst=True)
    return d.strftime("%d/%m/%Y")

def get_new_bdays(outlook, bdaysubjects, blacklist, bday_regex, mails, new_bdays):
    # Find Celfocus Analytics People Initiatives Mailbox
    for Mailbox in outlook.Folders:
        if "Celfocus Analytics People Initiatives" in Mailbox.Name:
            # Get Inbox folder from Celfocus Analytics People Initiatives
            inbox = Mailbox.Folders('Inbox')
            # Get Inbox' messages
            messages = inbox.Items

            # Loop every message
            for message in messages:
                # Check if subject is bday related and if sender isn't in blacklist
                if (any(bdaysubject in message.Subject.lower() for bdaysubject in bdaysubjects) and
                    message.SenderName not in blacklist):
                    # Match messages that contain a Bday
                    match = re.search(bday_regex, message.Body)
                    if match:
                        # Associate sender to its bday and owner
                        name = message.SenderName
                        day = standardize_bday(match.group())
                        if str(datetime.now().year) not in day:
                            new_bdays.append(('', name, day))
                            blacklist.append(name)

def excel_run2_week(excel, blacklist_1week, mails, bday_soon, no_pings):
    df = pd.read_excel(excel)
    df['Birth Date'] = df['Birth Date'].apply(lambda x: pd.to_datetime(x, format='%d/%m/%Y'))
    df['Birth Date'] = df.apply(lambda x: pd.NaT
                        if pd.isnull(x['Birth Date'])
                        else x['Birth Date'].strftime("%d/%m/%Y"), axis=1)
    df.apply(lambda x : check_next_week(x['Name'], x['Birth Date'], blacklist_1week, mails, bday_soon, no_pings)
                        if (x['Name'] not in blacklist_1week)
                        else None, axis=1)

def check_next_week(name, day, blacklist_1week, mails, bday_soon, no_pings):
    now = datetime.today().date()
    if name is not pd.NaT:
        day_check = datetime.strptime(day, '%d/%m/%Y').replace(year=now.year).date()
        if (day_check >= now + timedelta(3) and day_check < now + timedelta(10)):
            if name not in blacklist_1week and name not in no_pings:
                blacklist_1week.append(name)
                bday_soon.append((name, day))

def ping_1week(outlook_old, mails, new_bdays, bday_soon):
    mail = outlook_old.CreateItem(0)
    mail.To = '; '.join(mails)
    mail.Subject = 'Analytics Birthday Status'
    body = "The status for the Analytics Birthdays is as follows: \n"
    if new_bdays:
        body = body + '\n' + 'These fellas joined Analytics! \n'
        for new_bday in new_bdays:
            body = body + '\n' + '%s bday is on: %s' % (new_bday[1], new_bday[2][:-5])
    if bday_soon and new_bdays:
        body = body + '\n'
    if bday_soon:
        body = body + '\n' + 'These fellas have their birthday coming up next week! \n'
        for soon in bday_soon:
            body = body + '\n' + '%s bday is on: %s' % (soon[0], soon[1][:-5])
    elif not new_bdays and not bday_soon:
        body = body + '\n' + 'There were NO new additions to the birthday excel AND there are NO birthdays coming up next week'

    body = body + '\n\n' + "#DataScienceBaby"
    mail.Body = body
    mail.Send()


#!/usr/bin/env python
__author__ = 'j1mw3i'
import chart
import re

class LineChart(chart.SVGChart):

    def add_data(self, series, color, axle_x, axle_y):
        dwg = self.dwg
        grp_data = dwg.g(class_='series')
        grp_data.set_desc(desc=series.title)

        data = series.data
        unit_pixel_x = float(self.data_view.width) / axle_x
        unit_pixel_y = float(self.data_view.height) / axle_y

        points = [(self.data_view.x + round(idx * unit_pixel_x),
                   self.data_view.y + self.data_view.height - round(p * unit_pixel_y)) for idx, p in enumerate(data)]

        # grp_data.add(dwg.polyline(points, fill='none', stroke=color,
        #                           style='stroke-width:2; stroke-linecap:round; linejoin:round;'))
        path_d = 'M' + str(points[0][0]) + ',' + str(points[0][1]) + ' ' + \
                 'L'.join(map(lambda x: str(x[0]) + ',' + str(x[1]) + ' ', points[1:len(points)]))
        # path_d = 'M'
        # for i in range(len(points)):
        #     if i % 3 == 1:
        #         path_d += 'C' + str(points[i][0]) + ',' + str(points[i][1]) + ' '
        #     else:
        #         path_d += str(points[i][0]) + ',' + str(points[i][1]) + ' '
        id_name = re.sub(r'[^a-zA-Z0-9]', '', series.title)
        line_g = dwg.linearGradient(id=id_name, x1='0%', y1='0%', x2='0%', y2='100%')
        line_g.add_stop_color(offset='0%', color=color, opacity='0.4')
        line_g.add_stop_color(offset='100%', color='#F9FBFB', opacity='0.15')
        dwg.defs.add(line_g)

        grp_data.add(dwg.path(d=path_d, stroke=color, fill='none', style='stroke-width:1.1'))
        grp_data.add(dwg.path(d=path_d + 'Z', fill='url(#'+id_name+')'))

        for i in range(len(points)):
            circle = dwg.circle(class_='data_cycle', center=points[i], r=4, id=i,
                                fill='white', stroke=color,
                                style='display: none; stroke-width: 1.5;')
            circle.set_desc(title=series.title, desc=data[i])
            grp_data.add(circle)
        dwg.add(grp_data)

    def add_data_trigger(self, axle):
        dwg = self.dwg
        grp_trigger = dwg.g(class_='trigger_grp')
        if axle.style.position == chart.TOP_ or axle.style.position == chart.BOTTOM_:
            rect_width = float(self.data_view.width) / axle.gradation_max
            rect_height = self.data_view.height
            offset = (1, 0)
        else:
            rect_width = self.data_view.width
            rect_height = float(self.data_view.height) / axle.gradation_max
            offset = (0, 1)
        x = self.data_view.x - offset[0] * rect_width * 0.5
        y = self.data_view.y - offset[1] * rect_height * 0.5
        for i in range(axle.gradation_max + 1):
            rect = dwg.rect(class_='data_trigger', insert=(x, y), size=(rect_width, rect_height),
                            style='stroke: none; fill: transparent;')
            rect.set_desc(desc=i)
            grp_trigger.add(rect)
            x += offset[0] * rect_width
            y += offset[1] * rect_height
        dwg.add(grp_trigger)


from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil

class TestSwiftLateDylibClangDeps(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    @skipUnlessDarwin
    @swiftTest
    @skipIfDarwinEmbedded
    def test(self):
        """Test that a late loaded Swift dylib with Clang dependencies is debuggable"""
        self.build()
        target, process, _, _ = lldbutil.run_to_name_breakpoint(self, "main")
        # Initialize SwiftASTContext before loading the dylib.
        self.expect("expr -l Swift -- 0")
        bkpt = target.BreakpointCreateByLocation(lldb.SBFileSpec('dylib.swift'), 5)
        lldbutil.continue_to_breakpoint(process, bkpt)
        self.expect("v x", substrs=['42'])
        self.expect("expr x", substrs=['42'])


#  Copyright (c) 2020
#  @Author: xiaoweixiang

# Definition for a binary tree node.
from typing import List

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
        '''
        把中序遍历用bfs打印出来
        很简单
        :param nums:
        :return:
        '''

        if not nums:
            return None
        else:
            mid = len(nums) / 2
            node = TreeNode(nums[mid]);
            nums_left = nums[1:mid]
            nums_right = nums[mid + 1:]
            node.left = self.sortedArrayToBST(nums_left)
            node.right = self.sortedArrayToBST(nums_right)
        return node


import pandas as pd
import plotly.express as px
from dash import dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
from app import app
import base64
import io, os
import numpy as np

markdown_text = '''
### L1: 5-Year Data of 3 Areas in PJM

The data are collected from the [PJM’s Data Miner 2] (https://dataminer2.pjm.com).
The solar data are not available before 2019, synthetic data from [PVWatts] (https://pvwatts.nrel.gov) are used.
Given an address (city name or zip code),
PVWatts calculates the electricity production of a PV system using an hour-by-hour simulation over one year.
The temperature data are collected from [NOAA] (https://www.noaa.gov/).
The 3 selected areas are MIDATL, SOUTH, and WEST.
Data from 01/01/2015 to 12/31/2018 are used for training, and data from 01/01/2019 to 12/31/2019 are used for testing.
'''

DATA_PATH = os.path.join( "data/l1")
df = pd.DataFrame(columns=[ "Location", "Year", "Month", "Day", "Weekday", "Hour","Demand", "Temperature"])
for root, dirs, files in os.walk(DATA_PATH):
    date = pd.read_csv(os.path.join(root, "Date.csv"))
    month_series = date["Month"]
    curr_year = 2015
    year_series = [0 for i in month_series]

    for i in range(len(month_series)-1):
        if month_series[i+1] >= month_series[i]:
            year_series[i] += curr_year
        else:
            year_series[i] += curr_year
            curr_year+=1

    year_series[-1] += curr_year

    for file in files:
        if file != "Date.csv" and file != "fcst.csv" and file != "test.csv":
            t = pd.read_csv(os.path.join(root, file))
            if "Demand" in t.columns:
                temp_df = pd.DataFrame(data = t["Demand"])
            else:
                temp_df = pd.DataFrame(data = t["Net"])
                temp_df["Demand"] = temp_df["Net"]
                temp_df.drop(columns=["Net"], inplace = True)

            temp_df["Location"] = file.split(".")[0]
            temp_df["Year"] = year_series
            temp_df["Month"] = month_series
            temp_df["Day"] = date["Day"]
            temp_df["Hour"] = date["Hour"]
            temp_df["Weekday"] = date["Weekday"]
            temp_df["Temperature"] = t["Temperature"]

            df = pd.concat([df, temp_df])

df['DateTime'] = pd.to_datetime(df[['Year', 'Month', 'Day', 'Hour']])
df = df.set_index('DateTime')
df.rename(columns={'Demand': 'Load'}, inplace=True)
df_daily = df.groupby(['Location'])[['Load']].resample('D').mean().reset_index(level=['Location'])
locations = df.Location.unique()

layout = html.Div([
    dcc.Markdown(markdown_text),
    dcc.Markdown('**Time Series of Load**'),
    dcc.RadioItems(
        id='l1-load-ts-res',
        inputStyle={'margin-left': '1rem', 'margin-right': '0.5rem'},
        options=[{'label': i, 'value': i} for i in ['Hourly', 'Daily']],
        style={'margin-left': '-1rem'}
    ),
    dcc.Graph(id='l1-load-ts'),
    dcc.Markdown('**Summary Statistics of Load**'),
    dcc.RadioItems(
        id='l1-load-stat-by',
        inputStyle={'margin-left': '1rem', 'margin-right': '0.5rem'},
        options=[{'label': i, 'value': i} for i in ['By hour', 'By month']],
        style={'margin-left': '-1rem'}
    ),
    dcc.Graph(id='l1-load-stat'),
    dcc.Markdown('**Correlation Analysis**'),
    dcc.RadioItems(
        id='l1-load-temp-by',
        inputStyle={'margin-left': '1rem', 'margin-right': '0.5rem'},
        options=[{'label': i, 'value': i} for i in ['By hour', 'By month']],
        style={'margin-left': '-1rem'}
    ),
    dcc.Graph(id='l1-load-temp'),
    dcc.Markdown('**Evaluation of Forecasts**'),
    dcc.Markdown('Select the range of the test set, and download the test set or upload your forecasts.'),
    dcc.DatePickerRange(
        id='l1-eval-test',
        min_date_allowed='2015-01-01',
        max_date_allowed='2019-12-31',
        start_date='2019-01-01',
        end_date='2019-12-31',
    ),
    html.Div(
        dbc.Button('Download', id='l1-download'),
        style={'display': 'inline-block',"margin-left": "20px"}
        ),
    dcc.Download(id='l1-download-csv'),
    html.Div(
        dcc.Upload(
            id='l1-upload',
            children=html.Div([
                'Drag and Drop or ',
                html.A('Select Files')
            ]),
            style={
                'width': '200%',
                'height': '50px',
                'lineHeight': '50px',
                'borderWidth': '1px',
                'borderStyle': 'dashed',
                'borderRadius': '5px',
                'textAlign': 'center'
            }
        ),
        style={'display': 'inline-block',"margin-left": "20px"}
        ),
    dcc.Graph(id='l1-eval')
])

@app.callback(Output('l1-load-ts', 'figure'), Input('l1-load-ts-res', 'value'), prevent_initial_call=True)
def update_l1_load_ts(res):
    if res == 'Hourly':
        fig = px.line(df, x=df.index, y='Load', color='Location')
    else:
        fig = px.line(df_daily, x=df_daily.index, y='Load', color='Location')
    fig.update_layout(margin={'t': 50},xaxis_title="Datetime",yaxis_title = "Load (kW)")
    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
            buttons=list([
                dict(count=1, label='1 month', step='month', stepmode='backward'),
                dict(count=3, label='3 months', step='month', stepmode='backward'),
                dict(count=6, label='6 months', step='month', stepmode='backward'),
                dict(count=12, label='1 year', step='month', stepmode='backward'),
                dict(step='all')
            ])
        )
    )
    return fig

@app.callback(Output('l1-load-stat', 'figure'), Input('l1-load-stat-by', 'value'), prevent_initial_call=True)
def update_l1_load_stat(res):
    if res == 'By hour':
        fig = px.box(df, x='Hour', y='Load', color='Location', labels={'Load': 'Load (kW)'})
    else:
        fig = px.box(df, x='Month', y='Load', color='Location', labels={'Load': 'Load (kW)'})
    fig.update_layout(margin={'t': 30}, boxmode='group',xaxis_title="Datetime",yaxis_title = "Load (kW)")
    return fig

@app.callback(Output('l1-load-temp', 'figure'), Input('l1-load-temp-by', 'value'), prevent_initial_call=True)
def update_l1_load_temp(res):
    if res == 'By hour':
        fig = px.scatter(
            df, x='Temperature', y='Load', color='Location',
            facet_col='Hour', facet_col_wrap=6, facet_row_spacing=0.03, height=900,
            labels={'Temperature': 'Temperature (\u00b0C)', 'Load': 'Load (kW)'},
            category_orders={'Hour': list(range(24))}
        )
    else:
        fig = px.scatter(
            df, x='Temperature', y='Load', color='Location',  facet_col='Month', facet_col_wrap=6, height=500,
            labels={'Temperature': 'Temperature (\u00b0C)', 'Load': 'Load (kW)'}
        )
    fig.update_layout(margin={'t': 30})
    return fig

@app.callback(
    Output('l1-download-csv', 'data'),
    Input('l1-download', 'n_clicks'),
    State('l1-eval-test', 'start_date'),
    State('l1-eval-test', 'end_date'),
    prevent_initial_call=True
)
def update_l1_download(n_clicks, start_date, end_date):
    df_test = df[start_date:end_date]
    return dcc.send_data_frame(df_test.to_csv, 'test.csv')

@app.callback(
    Output('l1-eval', 'figure'),
    Input('l1-upload', 'contents'),
    State('l1-eval-test', 'start_date'),
    State('l1-eval-test', 'end_date'),
    prevent_initial_call=True
)
def update_l1_upload(contents, start_date, end_date):
    df_test = df[start_date:end_date].copy()
    if contents is None:
        fig = px.line(df_test, x=df_test.index, y='Load', color='Location', labels={'Load': 'Load (kW)'})
    else:
        df_test['set'] = 'Actual'
        content_type, content_string = contents.split(',')
        decoded = base64.b64decode(content_string)
        df_fcst = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
        df_fcst = df_fcst.set_index('DateTime')
        df_fcst['set'] = 'Forecasted'
        mape = np.mean(np.absolute((df_test['Load'].to_numpy() - df_fcst['Load'].to_numpy()) / df_test['Load'].max()))
        title = 'Mean absolute percentage error (MAPE): {:.2%}'.format(mape)
        df_eval = pd.concat([df_test, df_fcst])
        fig = px.line(df_eval, x=df_eval.index, y='Load', color='Location', line_dash = 'set', labels={'Load': 'Load (kW)'}, title = title)
        fig.update_layout(legend_title_text=None)
    return fig


# Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy

import torch

from nemo.collections.nlp.modules.common.transformer.transformer_encoders import TransformerEncoder

__all__ = ["PoolingEncoder"]

class PoolingEncoder(torch.nn.Module):

    _SUPPORTED_ARCH = ["max", "avg"]

    def __init__(
        self,
        num_layers: int,
        hidden_size: int,
        inner_size: int,
        mask_future: bool = False,
        num_attention_heads: int = 1,
        attn_score_dropout: float = 0.0,
        attn_layer_dropout: float = 0.0,
        ffn_dropout: float = 0.0,
        hidden_act: str = "relu",
        pre_ln: bool = False,
        pre_ln_final_layer_norm: bool = True,
        hidden_steps: int = 4,
        hidden_init_method: str = "default",
        hidden_blocks: int = 2,
        pooling_type: str = "max",
    ):
        super().__init__()

        # minimal steps to allow reduction
        self._hidden_steps = hidden_steps
        self._hidden_init_method = hidden_init_method
        self._hidden_blocks = hidden_blocks
        self._pooling_type = pooling_type

        if self._hidden_steps < 2:
            raise ValueError("Expected hidden_steps >= 2 but received hidden_steps = {self._hidden_steps}")

        if self.hidden_init_method not in self.supported_init_methods:
            raise ValueError(
                "Unknown hidden_init_method = {hidden_init_method}, supported methods are {supported_init_methods}".format(
                    hidden_init_method=self.hidden_init_method, supported_init_methods=self.supported_init_methods,
                )
            )

        if self._pooling_type not in self.supported_arch:
            raise ValueError(f"Unknown pooling_type = {pooling_type}. Available values = {self.supported_arch}")

        # self-attention encoder
        layer = TransformerEncoder(
            num_layers=num_layers,
            hidden_size=hidden_size,
            inner_size=inner_size,
            mask_future=mask_future,
            num_attention_heads=num_attention_heads,
            attn_score_dropout=attn_score_dropout,
            attn_layer_dropout=attn_layer_dropout,
            ffn_dropout=ffn_dropout,
            hidden_act=hidden_act,
            pre_ln=pre_ln,
            pre_ln_final_layer_norm=pre_ln_final_layer_norm,
        )
        self.self_att_layers = torch.nn.ModuleList([copy.deepcopy(layer) for _ in range(hidden_blocks)])

        self.pooling = self._build_pooling_module()

    def _build_pooling_module(self):
        """
        Returns pooling module.
        Allows to override for child classes.
        """
        if self._pooling_type == "max":
            pooling = torch.nn.MaxPool1d(kernel_size=2, stride=2)
        elif self._pooling_type == "avg":
            pooling = torch.nn.AvgPool1d(kernel_size=2, stride=2)

        return pooling

    @property
    def supported_arch(self):
        return self._SUPPORTED_ARCH

    @property
    def supported_init_methods(self):
        return ["default"]

    @property
    def hidden_steps(self):
        return self._hidden_steps

    @property
    def hidden_blocks(self):
        return self._hidden_blocks

    @property
    def hidden_init_method(self):
        return self._hidden_init_method

    def forward(self, encoder_states, encoder_mask):
        """
        Args:
            encoder_states: output of the encoder (B x L_enc x H)
            encoder_mask: encoder inputs mask (B x L_enc)
        """
        # initialize hidden state
        hidden_mask = encoder_mask
        hidden_states = encoder_states

        # apply block (self-attention, max-pool) multiple times
        for self_att in self.self_att_layers:
            residual = hidden_states

            # self-attention over hidden
            hidden_states = self_att(encoder_states=hidden_states, encoder_mask=hidden_mask)

            hidden_states += residual

            # max pool reduction if possible
            if hidden_states.shape[1] >= self.hidden_steps:
                # max pool hidden states
                hidden_states = hidden_states.permute(0, 2, 1)
                hidden_states = self.pooling(hidden_states)
                hidden_states = hidden_states.permute(0, 2, 1)

                # max pool mask
                hidden_mask = (
                    self.pooling(hidden_mask.unsqueeze(0).type_as(hidden_states)).squeeze(0).type_as(hidden_mask)
                )

        return hidden_states, hidden_mask


# Lab 8
import arcade
import random

SPRITE_SCALING_PLAYER = 0.5
SPRITE_SCALING_FOOD = 0.40
SPRITE_SCALING_VILLAIN = 0.40

FOOD_COUNT = 80
VILLAIN_COUNT = 50

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

SCREEN_TITLE = "Lab 8 - Sprites"

class Villain(arcade.Sprite):

    def __init__(self, filename, sprite_scaling):
        super(). __init__(filename, sprite_scaling)

        self.change_x = 0
        self.change_y = 0

    def update(self):

        self.center_x += self.change_x
        self.center_y += self.change_y

        if self.left < 0:
            self.change_x = -2

        if self.right > SCREEN_WIDTH:
            self.change_x = -2

        if self.bottom < 0:
            self.change_y = -2

        if self.top > SCREEN_HEIGHT:
            self.change_y = -2

# 2nd option
    # def reset_pos(self):
        # self.center_y = random.randrange(SCREEN_HEIGHT + 20, SCREEN_HEIGHT + 100)
        # self.center_x = random.randrange(SCREEN_WIDTH)

    # def update(self):
        # self.center_y += 3
        # if self.top < -0:
            # self.reset_pos()

class Food(arcade.Sprite):

    def reset_pos(self):
        self.center_y = random.randrange(SCREEN_HEIGHT + 20, SCREEN_HEIGHT + 100)
        self.center_x = random.randrange(SCREEN_WIDTH)

    def update(self):
        self.center_y -= 1
        if self.top < 0:
            self.reset_pos()

class SpritesGame(arcade.Window):

    def __init__(self):

        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)

        # VARIABLES
        self.villain_sprite_list = None
        self.player_sprite_list = None
        self.food_sprite_list = None

        # Player
        self.player_sprite = None
        self.score = 0

        # Cursor
        self.set_mouse_visible(False)

        arcade.set_background_color(arcade.color.BLUE)

    def setup(self):

        self.player_sprite_list = arcade.SpriteList()
        self.food_sprite_list = arcade.SpriteList()
        self.villain_sprite_list = arcade.SpriteList()

        # Score
        self.score = 0

        # Character (Kenny.nL)
        self.player_sprite = arcade.Sprite("Xavier.png", SPRITE_SCALING_PLAYER)

        self.player_sprite.center_x = 50
        self.player_sprite.center_y = 50
        self.player_sprite_list.append(self.player_sprite)

        # Food
        for i in range(FOOD_COUNT):
            # Create Food
            food = Food("Food.png", SPRITE_SCALING_FOOD)

            food.center_x = random.randrange(SCREEN_WIDTH)
            food.center_y = random.randrange(SCREEN_HEIGHT)

            self.food_sprite_list.append(food)

        for i in range(VILLAIN_COUNT):
            # Create Villain (kenny.nL)
            villain = Villain("Villain.png", SPRITE_SCALING_VILLAIN)

            villain.center_x = random.randrange(SCREEN_WIDTH)
            villain.center_y = random.randrange(SCREEN_HEIGHT)
            villain.change_x = random.randrange(-3, 4)
            villain.change_y = random.randrange(-3, 4)

            self.villain_sprite_list.append(villain)

    def on_draw(self):

        arcade.start_render()
        self.food_sprite_list.draw()
        self.player_sprite_list.draw()
        self.villain_sprite_list.draw()

        # Text
        output = f"Score: {self.score}"
        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)

    def on_mouse_motion(self, x, y, dx, dy):

        # Character movement
        self.player_sprite.center_x = x
        self.player_sprite.center_y = y

    def on_update(self, delta_time):

        self.food_sprite_list.update()
        self.villain_sprite_list.update()

        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.food_sprite_list)
        for food in hit_list:
            food.remove_from_sprite_lists()
            self.score += 1

        hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.villain_sprite_list)
        for villain in hit_list:
            villain.remove_from_sprite_lists()
            self.score -= 1

def main():
    window = SpritesGame()
    window.setup()
    arcade.run()

if __name__ == "__main__":
    main()


import sys, json
from sympy import *

def f(statement, data):
    x = symbols('x')
    fn = str(eval(statement))
    y = []
    for x in data:
        x = float(x)
        y.append(eval(fn))
    return y

if __name__ == '__main__':
    statement = sys.argv[1]
    data = json.loads(sys.argv[2])
    print(json.dumps(f(statement, data)))


#1
x = [ [5,2,3], [10,8,9] ]
students = [
     {'first_name':  'Michael', 'last_name' : 'Jordan'},
     {'first_name' : 'John', 'last_name' : 'Rosales'}
]
sports_directory = {
    'basketball' : ['Kobe', 'Jordan', 'James', 'Curry'],
    'soccer' : ['Messi', 'Ronaldo', 'Rooney']
}
z = {'x': 10, 'y': 20}

x[1][0]=15
print(x)
students[0]['last_name']="Bryant"
print(students)
sports_directory['soccer'][0]="Andres"
print(sports_directory)
z['y']=30
print(z)

#2

students = [
         {'first_name':  'Michael', 'last_name' : 'Jordan'},
         {'first_name' : 'John', 'last_name' : 'Rosales'},
         {'first_name' : 'Mark', 'last_name' : 'Guillen'},
         {'first_name' : 'KB', 'last_name' : 'Tonel'}
    ]
for dic in students:
    for val,cal in dic.items():
        print(f'{val} -- {cal}')
        print(f'{val}')

#3
a_key = "first_name"
values_of_a_key = [a_dict[a_key] for a_dict in students]
print(values_of_a_key)
b_key = "last_name"
values_of_b_key = [b_dict[b_key] for b_dict in students]
print(values_of_b_key)

#4

dojo = {
   'locations': ['San Jose', 'Seattle', 'Dallas', 'Chicago', 'Tulsa', 'DC', 'Burbank'],
   'instructors': ['Michael', 'Amy', 'Eduardo', 'Josh', 'Graham', 'Patrick', 'Minh', 'Devon']
}
print(dojo['locations'],dojo['instructors'])

_python/python_fundamentals/10th Assignment Functions Intermediate II/function_intermediate_II.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" \example X4M300_playback_recording.py

#Target module: X4M300 (only data recorded by X4M300 needed)

#Introduction: This is an example of how to playback recoreded data.

#Command to run: "python X4M300_plot_movementlist.py -f xethru_recording_meta.dat" or "python3 X4M300_plot_movementlist.py -f xethru_recording_meta.dat".
"""

from __future__ import print_function
from pymoduleconnector import ModuleConnector
from pymoduleconnector import DataPlayer
from pymoduleconnector import DataType

from optparse import OptionParser
from datetime import datetime

import sys
import time

def start_playback(meta_filename, depth):
    print("start playback from: {}, depth: {}".format(meta_filename, depth))
##! [Typical usage]
    player = DataPlayer(meta_filename, depth)

    mc = ModuleConnector(player, log_level=0)

    # Get read-only interface and receive telegrams / binary packets from recording
    x4m300 = mc.get_x4m300()

    # Control output
    player.set_filter(DataType.BasebandIqDataType | DataType.PresenceSingleDataType);
    player.play()
    # ...
    player.pause();
    # ...
    player.set_playback_rate(2.0);

##! [Typical usage]
    player.set_playback_rate(1.0);
    player.set_loop_mode_enabled(True);
    player.play();

    try:
        while True:
            if x4m300.peek_message_baseband_iq():
                data = x4m300.read_message_baseband_iq()
                print("received baseband iq data, frame counter: {}".format(data.frame_counter))
            if x4m300.peek_message_presence_single():
                data = x4m300.read_message_presence_single()
                print("received presence single data, frame counter: {}".format(data.frame_counter))
            time.sleep(0.02) # Sleep 20 ms
    except (KeyboardInterrupt, SystemExit):
        del mc
        raise

def main():
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="meta_filename", metavar="FILE",\
                      help="meta file from recording")
    parser.add_option("--depth", dest="depth", metavar="NUMBER", type="int", default=-1,\
                      help="number of meta files to read in chained mode")

    (options, args) = parser.parse_args()
    if not options.meta_filename:
        print("Please specify a 'xethru_recording_meta.dat' file (use -f <file> or --file <file>)")
        sys.exit(1)

    start_playback(options.meta_filename, options.depth)

if __name__ == "__main__":
    main()


import requests
import json
api_url = 'https://jsonplaceholder.typicode.com/todos'
todo={"usedId":1,'title':"Buy milk","completed":False}
headers = {"Content-Type":"application/json"}

reponse = requests.post(api_url, data=json.dump(todo),headers=headers)
reponse.json()

print(reponse.status_code)


import cv2
import numpy as np
from pathlib import Path
import os.path
try:
	import functions
except ImportError:
	from HRV import functions

sourcePath=str(Path(__file__).resolve().parents[1])

def hrvElaboration(videofilepath,showImages=False, showVideo=False, showArousal=False):
	'''
		This script calculate the hr every second and pass them to the caller.
		It :
		1) opens the video
		2) reads the frames
		3) does the face detection & relative skin detection
		4) Average of the skin pixel to obtain a triplet of RGB values
		5) Concatenate the triplets to obtain the RGB temporal trace.
		6) Does the process (in the functions method) with CHROM
		7) calculate the hr

		Inputs:
		- videofilepath is the path of the video
		- showImages will show the images of rppg signal, hrv signal and periodogramm if True (default: False)
		- show Video will show the frames of the video and the skin used for detection if True (default: False)

	'''

	## Variables
	face_red_means = list()			# the lists of the forehead means
	face_green_means = list()
	face_blue_means = list()
	bpms= list()					# a series with the second in question and the bpm calculated for that second
	firstTime= True
	secondsBeforeNewLine=20			# cosmetic variable to set after how many second-points it should have a new line
	windowForArousal=10				# how many seconds to wait before take an arousal value
	fps=0							# in a video, the fps is known
	videoLen=0						# the number of frames in the video
	emotionalFeatures=list()

	cap = cv2.VideoCapture(videofilepath)  # open the video
	if (cap.isOpened() == False):
		print("[info hrvElaboration] Unable to read the video")

	print("[info hrvElaboration] Processing the video with the hrv mode...")
	 # Find OpenCV version
	(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

	#Taking the fps of the video and the number of frames of it
	if int(major_ver)  < 3 :
		fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
		videoLen=int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
	else :
		fps = cap.get(cv2.CAP_PROP_FPS)
		videoLen=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

	# for now we assume that the script calling is in a subfolder too
	face_cascade = cv2.CascadeClassifier(os.path.join(sourcePath,'haarcascade_frontalface_default.xml'))

	print("[info hrvElaboration] The fps of the video is: "+str(fps)+" , "+ "and the number of frames is: "+str(videoLen) )
	print("[info hrvElaboration] Please wait while it is calculating:")
	# first loop: it takes the frames, saves the means and works on them. no display of the video, just a . every second
	for currentFrame in range(0, videoLen):
		ret, frame = cap.read()
		if(frame is not None):
			try:

				#Face detection
				gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
				faces = face_cascade.detectMultiScale(gray, 1.3, 5)

				if len(faces) == 1: 		#if the detection is of just 1 face
					for (x,y,w,h) in faces:
						 # static Skin detection
						reducedWidth = int(w * 0.63)
						modifiedX = int(w * 0.15)
						reduceHeigh = int(h * 0.25)
						inizioY = int(h * 0.1)
						frontX = int(w - reducedWidth)

						if firstTime:
						# locking the coordinates of the forehead
							foreheadY = y+inizioY
							foreheadX = x+frontX
							foreheadX2 = x+reducedWidth
							foreheadY2 = y+reduceHeigh
							#firstTime = False

						rawForehead = frame[foreheadY:foreheadY2, foreheadX:foreheadX2]
						face_red_means.append(np.mean(rawForehead[:,:,2]))
						face_green_means.append(np.mean(rawForehead[:,:,1]))
						face_blue_means.append(np.mean(rawForehead[:,:,0]))
				else:
					if len(faces) == 0:
						print("[info hrvElaboration] no face detected in a frame")
					else:
						print("[info hrvElaboration] too many faces detected in a frame")

					if not firstTime : 	#maybe an error of the cascade, but the video should be done with the subject not moving
						rawForehead = frame[foreheadY:foreheadY2, foreheadX:foreheadX2]
						face_red_means.append(np.mean(rawForehead[:,:,2]))
						face_green_means.append(np.mean(rawForehead[:,:,1]))
						face_blue_means.append(np.mean(rawForehead[:,:,0]))
				# show the skin in video
				if(showVideo):
					cv2.rectangle(frame, (foreheadX, foreheadY), (foreheadX2, foreheadY2), (0,255,0), 2) # the rectangle that will show on the forehead
				#calculates not for each frame but for each second more or less
				if (currentFrame>0 and currentFrame%fps==0) :
					#try:
					bpm, sf = functions.hrElaboration(face_red_means, face_green_means, face_blue_means, None, fps, False ,False, False, currentFrame,showArousal=showArousal)
					bpms.append([currentFrame/fps,bpm])

					#except Exception as e :
					#	print()
					#	print("[exception hrvVElaboration] Unknown error: " + ( str(e)))
					#	cap.release()
					#	return -2
					if(int(currentFrame/fps)%int(secondsBeforeNewLine/2)==0):
						emotionalFeatures.append([currentFrame/fps,sf])
					print ('.' , end='', flush=True) 					# 1 point displayed for second
					if(int(currentFrame/fps)%secondsBeforeNewLine==0):
						print()

				if(showVideo):	#show the video
					cv2.imshow('Video', frame)

					if cv2.waitKey(1) & 0xFF == ord('q'):
						break

			except Exception as e:
				print (" [exception hrvElaboration] Exception: "+str(e))
				print("[exception hrvElaboration] Exception at frame: "+ str(currentFrame)+" of "+str(videoLen))
		else:
			print("[exception hrvElaboration] Null frame at frame: "+ str(currentFrame)+" of "+str(videoLen) +".")
			break # just for now

	print(functions.hrElaboration(face_red_means, face_green_means, face_blue_means, None, fps, showImages ,False, False, currentFrame, showArousal=showArousal))
	print()

	if( showArousal):	#shows the arousal feature normalized on the first element
		hr=list()
		arousal=list()
		timesH=list()
		timesA=list()
		for i in range(0,len(bpms)):
			hr.append(bpms[i][1])
			timesH.append(bpms[i][0])
		for i in range(0,len(emotionalFeatures)):
			timesA.append(emotionalFeatures[i][0])
			arousal.append(emotionalFeatures[i][1])
		arousal/=arousal[0]

		functions.plotHRAndArousal(timesH,timesA,hr,arousal)

	cap.release()
	return bpms, emotionalFeatures


import random
import string
from model.project import Project

def random_string(maxlen):
    symbols = string.ascii_letters + string.digits #+ string.punctuation + " "*2
    return "".join([random.choice(symbols) for x in range(random.randrange(maxlen))])

project_1 = Project(random_string(15), random_string(15))
pr_2 = Project(random_string(15), random_string(15))

def test_add_new_project(app):
    #app.session.login("administrator", "root")
    old_list = app.pr.get_project_list()
    app.pr.add_new_project(project_1)
    new_list = app.pr.get_project_list()
    assert len(old_list) + 1 == len(new_list)
    old_list.append(project_1.name)
    print('\n' 'old = ', sorted(old_list))
    print('new = ', sorted(new_list))
    assert sorted(new_list) == sorted(old_list)

def test_sp_add(app):
    #app.session.login("administrator", "root")
    old_list = app.soap.get_projects()
    app.pr.add_new_project(pr_2)
    new_list = app.soap.get_projects()
    pr_id = sorted(new_list, key=Project.id_or_max)[-1].id
    assert len(old_list) + 1 == len(new_list)
    old_list.append(Project(name=pr_2.name, id=pr_id))
    print('\n' 'old = ', sorted(old_list, key=Project.id_or_max))
    print('new = ', sorted(new_list, key=Project.id_or_max))
    assert sorted(new_list, key=Project.id_or_max) == sorted(old_list, key=Project.id_or_max)


#Write a program to print a list of strings in the predefined order, with the following modifications:
#If the string’s length is equal to its printed position, convert string to UPPERCASE
	#- Else convert the string to lowercase
	#- Except if the string’s position is unchanged from its original position
	#- Input will be a number N, then a list of N non-repeating numbers defining the required position of the string, and finally the list of N non-repeating strings. Output will be a list of N strings at the required location, with the required changes.

#- Input:
#5
#5
#4
#3
#1
#2
#Dog
#Goat
#Cat
#Horse
#Elephant

#- Output:
#horse
#elephant
#Cat
#GOAT
#dog

x=int(input("Enter the number N"))
list1=[]
print("Enter the loaction index")
for i in range(0,x):                  #taking input in the string for the indexes.
    n=int(input())
    list1.append(n)
print("Now enter the items in list2")
list2=[]
for i in range(0,x):                   #taking input in the next string for the names
    n=input()
    list2.append(n)
list3 = list2
zipped_list = zip(list1,list2)         #making a tuple from the two given lists
sorted_pairs = sorted(zipped_list)     #sorting those tuples
tuples = zip(*sorted_pairs)
list1, list2 = [ list(tuple) for tuple in  tuples]    #separating the two lists after sorting
#print(list1)
#print(list2)

for i in range (len(list2)):
    if(list2[i] == list3[i]):                    #checking if the position is same from the original list(cond-3)
        continue
    elif (len(list2[i]) == i+1):                #if the length of the string is equal to the position
        list2[i] = list2[i].upper()
    else :
        list2[i] = list2[i].lower()             #any other case from these two

print(list2)


import doctest

def letter_nums(input_str):
    '''(str) -> str

    Returns a string representing the letters of input_str along with the
    index of each letter (starting from 1)

    >>> letter_nums('Hello')
    '1=H2=e3=l4=l5=o'
    >>> letter_nums('Brian')
    '1=B2=r3=i4=a5=n'
    '''

    # complete the function using a while loop (uncomment to use)
    '''
    res = ""
    count = 0
    # keep looping until we've added one item per letter of the input
    while(count < len(input_str)):
        # add the index (which will be 1 greater than the number of times
        # we've looped
        res += str(count +1)
        res += "="
        # add the count-th letter of the input string
        res += input_str[count]
        # we have to increment the count ourselves
        count +=1
    return res
    '''

    # complete the function using an elemental for loop (uncomment to use)
    '''
    res = ""
    index = 0
    # loop once for each letter in the input string
    for next_letter in input_str:
        # add the index (which we will need to keep track of ourselves, since
        # the elemental for loop doesn't let us know where we are in the list)
        res += str(index + 1)
        res += "="
        # add the letter. Note that we don't need to retrieve it, because
        # the elemental for loop takes care of that for us
        res += next_letter
        # we have to keep track of the index ourselves
        index += 1
    return res
    '''

    # complete the function using an indexed for loop
    res = ""
    # loop through the indices of the letters in the input string
    for index in range(0, len(input_str)):
        # add the index, which will be updated for us
        res += str(index + 1)
        res += "="
        # add the letter, since we have the index, it's fairly easy to retrieve
        res += input_str[index]
    return res

if(__name__ == "__main__"):
    # print the output of the doctest, so we know we've passed
    # print(doctest.testmod())
    # or just run doctest, and then we only see output if there's a problem
    doctest.testmod()

    print(letter_nums("Test Sentence"))

    # SOME LOOP PRACTICE
    my_list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

    # ELEMENTAL FOR LOOP: Print the elements of a list
    for element in my_list:
        print(element, end=' ')
    print("All done!")

    # INDEXED FOR LOOP: Print the elements of a list
    for index in range(0, len(my_list)):
        print(my_list[index], end=' ')
    print ("That's all of 'em")

    # WHILE LOOP: prompt a user until they say "Stop"
    response = ""
    while(response != "Stop!"):
        response = input("Type something (say 'Stop!' to exit):")
    print("Have a nice day")

    # A FEW OTHER FUN LOOPS

    # PRINT A LIST BACKWARDS
    # first reverse the list
    my_list.reverse()
    # now just loop through it normally
    for element in my_list:
        print(element, end=' ')
    print("Yup, that's backwards")
    # should probably turn the list back around in case anyone else
    # needs to use it
    my_list.reverse()

    # Here's a much more efficient way (can you see why?)
    # print each element by its index, starting at the end and working towards
    # the front
    for index in range(len(my_list) - 1, -1, -1):
        print(my_list[index], end=' ')
    print("That's backwards too...")

    # print out every other element in the list
    # to print out the alternate elements, we just have to set our skip to be
    # 2 when we're creating our range
    # Try to do this with one of the other types of loops... it's not easy
    for index in range(0, len(my_list), 2):
        print(my_list[index], end=' ')
    print("skipping elements is easy if you use the right type of loop")


#calss header
class _EASE():
	def __init__(self,):
		self.name = "EASE"
		self.definitions = [u'the state of experiencing no difficulty, effort, pain, etc.: ', u'relaxed: ', u'If someone, especially a soldier, is at ease, they are standing with their feet apart and their hands behind their back.']

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

		self.specie = 'nouns'

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


from datetime import datetime
from app import db
from app.models import Stock, StockReport
from ..schema import ErrorSchema

def get_top_delivery(delivery_limit):
    delivery_query = db.session.query(StockReport.stock_id, StockReport.prev_price, StockReport.last_price,\
        ((StockReport.delivery_qty*100.0)/StockReport.traded_qty).label('delivery_per'), db.func.max(StockReport.date)\
            .label('last_trade_date')).group_by(StockReport.stock_id).subquery()
    delivery_detail = db.session.query(Stock, delivery_query).join(delivery_query, Stock.id == delivery_query.c.stock_id)\
        .filter(Stock.series == 'EQ').order_by(delivery_query.c.delivery_per.desc()).limit(delivery_limit).all()

    return delivery_detail

def get_top_volumn(volumn_limit):
    volumn_query = db.session.query(StockReport.stock_id, StockReport.prev_price, StockReport.last_price, StockReport.traded_qty,\
        db.func.max(StockReport.date).label('last_trade_date')).group_by(StockReport.stock_id).subquery()
    volumn_detail = db.session.query(Stock, volumn_query).join(volumn_query, Stock.id == volumn_query.c.stock_id)\
        .filter(Stock.series == 'EQ').order_by(volumn_query.c.traded_qty.desc()).limit(volumn_limit).all()

    return volumn_detail

def get_top_gainer(gainer_limit):
    gainer_query = db.session.query(StockReport.stock_id, StockReport.prev_price, StockReport.last_price,\
        (((StockReport.prev_price-StockReport.last_price)*100)/StockReport.prev_price).label('change_per'),\
            db.func.max(StockReport.date).label('last_trade_date')).group_by(StockReport.stock_id).subquery()
    gainer_detail = db.session.query(Stock, gainer_query).join(gainer_query, Stock.id == gainer_query.c.stock_id)\
        .filter(Stock.series == 'EQ').order_by(gainer_query.c.change_per.asc()).limit(gainer_limit).all()

    return gainer_detail

def get_top_loser(loser_limit):
    loser_query = db.session.query(StockReport.stock_id, StockReport.prev_price, StockReport.last_price,\
        (((StockReport.prev_price-StockReport.last_price)*100)/StockReport.prev_price).label('change_per'),\
            db.func.max(StockReport.date).label('last_trade_date')).group_by(StockReport.stock_id).subquery()
    loser_detail = db.session.query(Stock, loser_query).join(loser_query, Stock.id == loser_query.c.stock_id)\
        .filter(Stock.series == 'EQ').order_by(loser_query.c.change_per.desc()).limit(loser_limit).all()

    return loser_detail


from sqlalchemy import Integer
from sqlalchemy.sql import bindparam, column, text

from hsredshift.analytics.filters import Region
from hsredshift.analytics.library.base import BaseRedshiftQuery, QueryDisplayVisual

class SingleAccountLoBestRankBySeason(BaseRedshiftQuery):
	name = "single_account_lo_best_rank_by_season"
	enabled = True
	is_personalized = True
	cache_warming_enabled = False
	required_parameters = ["account_lo", "Region"]
	available_parameters = ["account_lo", "Region"]
	display_visual = QueryDisplayVisual.TABLE
	stmt = text("""
		SELECT
			g.ladder_season,
			sum(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_STANDARD') THEN 1 ELSE 0 END) AS num_standard_games,
			min(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_STANDARD') THEN p.rank ELSE NULL END) AS best_standard_rank,
			min(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_STANDARD') THEN p.legend_rank ELSE NULL END) AS best_standard_legend_rank,
			sum(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_WILD') THEN 1 ELSE 0 END) AS num_wild_games,
			min(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_WILD') THEN p.rank ELSE NULL END) AS best_wild_rank,
			min(CASE WHEN p.game_type = f_enum_val('BnetGameType.BGT_RANKED_WILD') THEN p.legend_rank ELSE NULL END) AS best_wild_legend_rank
		FROM player p
		JOIN game g ON g.id = p.game_id
		WHERE p.rank <> -1
		AND p.options_visible
		AND p.region BETWEEN :min_region AND :max_region
		AND p.account_lo = :account_lo
		GROUP BY g.ladder_season
		ORDER BY g.ladder_season DESC
	""").bindparams(
		bindparam("min_region", type_=Integer),
		bindparam("max_region", type_=Integer),
		bindparam("account_lo", type_=Integer)
	).columns(
		column("ladder_season", Integer),
		column("num_standard_games", Integer),
		column("best_standard_rank", Integer),
		column("best_standard_legend_rank", Integer),
		column("num_wild_games", Integer),
		column("best_wild_rank", Integer),
		column("best_wild_legend_rank", Integer),
	)

	def to_chart_series(self, params, result_set):
		result = {
			"metadata": {
			},
			"data": {
				"ALL": [],
			}
		}

		for row in result_set:
			result["data"]["ALL"].append(row)

		return result

	def get_supported_filter_members(self, filter):
		if filter == Region:
			return [Region.REGION_EU, Region.REGION_CN, Region.REGION_KR, Region.REGION_US]
		else:
			return []

	def get_default_value_for_filter(self, filter):
		if filter == Region:
			raise ValueError("There is no default value for the Region param. It must be supplied.")

	def example_parameters(self):
		return dict(
			Region="REGION_US",
			account_lo=23226729
		)

if __name__ == "__main__":
	from hsredshift.utils.runners import LocalQueryRunner
	runner = LocalQueryRunner(locals())
	runner.run()


import datetime
import random

from mythic_scorer_core import mythic_scorer_sql
from mythic_scorer_core import market_date_entity
from mythic_scorer_core import market_entity
from mythic_scorer_core import name_entity
from mythic_scorer_core import price_entity

import test_helper

def test_0():
    sqlWrapper = mythic_scorer_sql.SqlWrapper('mythic-scorer', 'changeme', 'mythic_scorer')
    market = market_entity.Market(sqlWrapper)
    market.selectMarketBySymbol('INDEX')

    symbol = test_helper.randomString()
    taskId = random.randint(0, 1000)

    marketDate = market_date_entity.MarketDate(sqlWrapper)
    dates = marketDate.selectMarketDates(market.id)
    if len(dates) < 1:
        marketDate.insertMarketDate(taskId, market.id, datetime.date(2015, 1, 23))
    else:
        marketDate.selectMarketDateById(dates[0])

    openPrice = test_helper.randomMoney()
    highPrice = test_helper.randomMoney()
    lowPrice = test_helper.randomMoney()
    closePrice = test_helper.randomMoney()

    volume = random.randint(0, 1000)
    openInterest = random.randint(0, 1000)

    name = name_entity.Name(sqlWrapper)
    name.insertEquityName(taskId, market.id, symbol, test_helper.randomString())

    price = price_entity.Price(sqlWrapper)
    rowId = price.insertPrice(taskId, name.id, marketDate.id, openPrice, highPrice, lowPrice, closePrice, volume, openInterest)

    price.selectPriceById(price.id)
    assert price.id == rowId
    assert price.creationTaskId == taskId
    assert price.updateTaskId == 0
    assert price.nameId == name.id
    assert price.marketDateId == marketDate.id
    assert price.volume == volume
    assert price.openInterest == openInterest
    assert price.openPrice.intVal == openPrice.intVal
    assert price.highPrice.intVal == highPrice.intVal
    assert price.lowPrice.intVal == lowPrice.intVal
    assert price.closePrice.intVal == closePrice.intVal

    price.selectPrice(marketDate.id, name.id)
    assert price.id == rowId


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Given a graph G=(V,E) and s1,t1,s2,t2 in V it finds a minimum cost subset
of the edges such that s1 is connected with t1 and s2 is connected with t2.
V={0,1,...,n}
 s1 is point 0
 t1 is point 1
 s2 is point n-2
 t2 is point n-1

"""

import cvxpy as py
import numpy as np
import matplotlib.pyplot as plt
#from time import time

def plot_graph(points,A):
    """
    Input:
        List of points
        Adjacency matrix A for the points
    """
    n = len(points)
    x = []; y = []
    for i in range(n):
        x.append(points[i][0])
        y.append(points[i][1])
        plt.plot(x, y, 'co',color='r',markersize=2)

    for i in range(n):
        for j in range(n):
            if A[i,j]==1:
                if not(i==j):
                    plt.arrow(x[i], y[i], x[j]-x[i], y[j]-y[i],head_width = 0,linewidth=0.5,
            color = 'g', length_includes_head = False)

    plt.text(points[0,0],points[0,1], "s1", size=15)
    plt.text(points[1,0],points[1,1], "t1", size=15)
    plt.text(points[n-2,0],points[n-2,1], "s2", size=15)
    plt.text(points[n-1,0],points[n-1,1], "t2", size=15)

    #Set box slitghtly larger than the range of x and y
    plt.xlim(min(x)-0.05*(max(x)-min(x)), max(x)+0.05*(max(x)-min(x)))
    plt.ylim(min(y)-0.05*(max(y)-min(y)), max(y)+0.05*(max(y)-min(y)))

    plt.show()

def random_points(n):
    """Makes  a list of n random points (x,y) with 0<=x,y<=1
    """
    np.random.seed(26)
    points = np.random.random(size=(n,2))    # create random points
    return(points)

def compute_distances(points):
    """
    Output: D[i,j]=D[j,i] is the Euclidean distance between points i and j
    """
    n=len(points)
    p=points
    D = np.zeros((n,n))
    for i in range(n):
        for j in range(i,n):
            D[i,j]=np.sqrt((p[i,0]-p[j,0])**2+(p[i,1]-p[j,1])**2)
            D[j,i]=D[i,j]
    return D

def define_edges(D,n):
    """
    Some functipon for defining a random set of the edges.
    There is an edge between points i and j if their distance is below
    some threshold value mu
    Output: Adjacency matrix A
    """
    A=np.zeros((n,n))
    mu=0.3
    for i in range(n):
        for j in range(n):
            if D[i,j]<= mu:
                A[i,j]=1
    return A

def main():

    n = 25  #number of points

    points = random_points(n)   # create n random points in a 1x1 square

    D=compute_distances(points) #D[i,j]=D[j,i]=the distance between points i and j

    A=define_edges(D,n) #adjacency matrix: A[i,j]=1 if there is an edge (i,j)

    plot_graph(points,A) #plot the points and edges defined by A

    x = py.Variable((n,n),boolean=True)        #x[i,j]=1 if edge (i,j) is bought
    y1 = py.Variable((n,n))   #y1[i,j] is the value of flow 1 on arc (i,j)
    y2 = py.Variable((n,n))   #y2[i,j] is the value of flow 2 on arc (i,j)

    # For old version of CVXPY:
    # x = py.Bool(n,n)  # x[i,j]=1 if edge (i,j) is bought
    # y1 = py.Variable(n, n)  # y1[i,j] is the value of flow 1 on arc (i,j)
    # y2 = py.Variable(n, n)  # y2[i,j] is the value of flow 2 on arc (i,j)

    constraints = [x>=0]
    ####################
    for i in range(n):
        constraints+= [x[i,i]==0]
    for i in range(n):
        for j in range(n):
            constraints += [x[i,j] <= A[i,j]]

    for i in range(n):
        for j in range(n):
            constraints += [x[i, j] == x[j,i]]

    constraints += [sum(y1[0, j] for j in range (n)) == 1]
    constraints += [sum(y1[i, 1] for i in range (n)) == 1]
    constraints += [sum(y2[n-2, j] for j in range(n)) == 1]
    constraints += [sum(y2[i, n-1] for i in range(n)) == 1]

    for i in range(n):
        for j in range(n):
            constraints += [y1[i, j] <= x[i, j]]
    for i in range(n):
        for j in range(n):
            constraints += [y1[i, j] == -y1[j, i]]
    for i in range(n):
        for j in range(n):
            constraints += [y2[i, j] <= x[i, j]]
    for i in range(n):
        for j in range(n):
            constraints += [y2[i, j] == -y2[j, i]]

    for i in range(2,n):
        constraints += [sum(y1[i, j] for j in range(n))-sum(y1[j, i] for j in range(n))==0]
    for i in range(0,n-2):
        constraints += [sum(y2[i, j] for j in range(n))-sum(y2[j, i] for j in range(n))==0]

    objective = py.Minimize(sum(sum(x[i,j]*D[i,j] for i in range(n))for j in range(n)))
    prob = py.Problem(objective, constraints)
    print("Solving")
    prob.solve(solver=py.GLPK_MI)
    print("Solved")
    print(prob.status)
    #####################
    try:
        if prob.status == 'optimal':
            xval=np.array(x.value)
            plot_graph(points,xval)  # the edges bought
            print('Value is',prob.value)
    except:
        print('No feasible solution found')

main()


import tkinter as TK
from table_nf import MyTable
import sampleData

class Main:
    def __init__(self, parent):
        self.parent=parent
        #parent.geometry("500x200")
        mainFrame = TK.Frame(parent, borderwidth=5, bg='yellow')
        mainFrame.pack()
        self.table = MyTable(mainFrame, createColumns(),
                                rows=10, scroll=True,  # Vertical scroll forced
                                drawCell=self.drawCell,         # These are three callback functions
                                cellClick=self.clicked,
                                dataChanged=None)
        self.table.setData(sampleData.data1)

    def drawCell(self,widget, data):
        #print ('Table row', widget.tableRow, 'Column',widget.tableColumn)  # Visible cells
        #print ('Data  row', widget.dataCoords[0], 'Column',widget.dataCoords[1]) # actual data cells
        widget.setText(data)
        widget.configure(bg='light grey')
        if widget.tableColumn == 2: # Age
            if data > 20:
                widget.configure(bg='light green')

    def clicked(self, widget): # Row, column the same as drawCell
        print ('Cell Text   :', widget.getText())
        if widget.tableRow < 0: # Header - maybe sort data and reset into table
            print ('Header clicked')
            sortedData = sorted(sampleData.data1, key=lambda x: x[widget.tableColumn])
            self.table.setData(sortedData)

def createColumns():
    columns = [
        {'text':'ID', 'width': 6, 'bg':'blue', 'fg':'white'},
        {'text':'Name', 'width': 18, 'bg':'blue', 'fg':'white'},
        {'text':'Age', 'width': 10, 'bg':'blue', 'fg':'white', 'align':'center'},
        {'text':'Sex', 'width': 6, 'bg':'blue', 'fg':'white', 'align':'center',}
    ]
    return columns

root = TK.Tk()
main = Main(root)

TK.mainloop()