# -*- coding: utf-8 -*-
"""
Created on Fri Nov 23 12:22:18 2018

@author: basak
"""
import pygame as pg
from psychopy import  core
import pandas as pd
import time
import os
import parallel

os.chdir("/home/fraimondo/Desktop/respi_eeg/Stim")

# open parallel port
parallel_port = parallel.Parallel()
parallel_port.setData(11)

list=pd.read_csv('nadine1_list_S1.csv')
stims=list['stims'].tolist()
timestim=list['time'].tolist()
triggers=list['trigger'].tolist()

pg.mixer.init()
pg.mixer.set_num_channels(7)
timer=core.Clock()
back = pg.mixer.Sound("nadine1.wav")
back.set_volume(0.5)

back.play()
timer.reset()
while True:
    parallel_port.setData(0)
    a=timer.getTime()
    a=("%.1f" % a)
    a=float(a)
    print(a)
    if a in timestim:
        ind=timestim.index(a)
        stim=stims[ind]
        s=pg.mixer.Sound(stim)
        s.play()
        parallel_port.setData(triggers[ind])
    if a==222.0:
        break
    time.sleep(.1)
parallel_port.setData(11)
parallel_port.setData(0)


#!/usr/bin/python3

import tornado.ioloop
import tornado.web
import tornado.template
import os.path
import time
import re
import csv
from datetime import datetime
import json
import datetime as dt
import fasteners

date_value = time.strftime("%d:%m:%G")
time_value = time.strftime("%H")
setting = dict(
    template_path=os.path.join(os.path.dirname(__file__), "templates"),
    static_path=os.path.join(os.path.dirname(__file__), "static"),
    debug=True
)
REs = {}
CAs = []
Mngip = {}
dict_cnflct = {}
list_days = []

def create_date_list():
    global list_days
    days = dt.datetime.today()
    for i in range(7):
        list_days.append(str(days).split()[0])
        days = days + dt.timedelta(1)

def csv_reader():
    """
    reads the csv files and creates a list of CAs
    a dictionary with CA name as keys and management ip as values
    and also another dictionary Ca name as keys and a list of REs as their values
    """
    global REs, CAs
    #   defines two regular expression and apply them on the CA name and RE name column in the file
    ptrn = re.compile(".*CA[0-9]+")
    ptrn_RE = re.compile(".*RE[0-9]+")
    try:
        with open("file.csv") as file_obj:
            reader = csv.reader(file_obj)
            #   make a list of CAs
            for row in reader:
                if ptrn.match(row[0]):
                    CAs.append(row[0])
                    Mngip.update({row[0]: row[16]})

        with open("interfaces.csv") as intf_obj:
            intf_reader = csv.reader(intf_obj)
            for ca in CAs:
                intf_obj.seek(0)
                int_node = []
                for row_r in intf_reader:
                    if ca == row_r[0] and ptrn_RE.match(row_r[1]):
                        int_node.append(row_r[1])
                REs.update({ca: int_node})
    except IOError:
        print("can not open CSV files, Please Contact system admin")

def mk_dict_schl(ca_ip):
    dic_overview = {}
    days = dt.datetime.today()
    file_path = "schedule"
    rw_lock = fasteners.ReaderWriterLock()
    with rw_lock.read_lock():
        with open(file_path, 'r') as f_output:
            for i in range(7):
                dic_overview.update({str(days).split()[0]: ""})
                for line in f_output:
                    list_line = line.split()
                    date_s_obj = datetime.strptime(list_line[3], '%d:%m:%Y')
                    temp_date_s = str(date_s_obj).split()[0]
                    str_date = str(days).split()[0]
                    time_s = int(list_line[4])
                    time_len = int(list_line[6]) - int(list_line[4])
                    if list_line[1] == ca_ip and str_date == temp_date_s:
                        if dic_overview[str_date] == "":
                            dic_overview[str_date] = {}
                            dic_overview[str_date].update({time_s: [list_line[2], time_len]})
                        else:
                            dic_overview[str_date].update({time_s: [list_line[2], time_len]})
                f_output.seek(0)
                days = days + dt.timedelta(1)
    # print(dic_overview)
    # print(dic_overview['2017-08-09'])
    return dic_overview

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        """
            loads the html template passes the list of CAs and date and time to the template
        """
        self.render("template.html", CAs=CAs, date_value=date_value, time_value=time_value, list_days=list_days)

class ViewHandler(tornado.web.RequestHandler):
    def post(self):
        """
            gets a CA name from drop box then find out its management ip address call a function that returns a dictionary of conflict in the txt file
        """
        value = self.get_argument('CA_drp_v')
        # print(Mngip[value])
        dic_over = mk_dict_schl(Mngip[value])
        self.write(dic_over)

class FormHandler(tornado.web.RequestHandler):
    def post(self):
        """
                handle the post request at the frontend which is a dictionary of new scheduling
                checks if the file exist and if there is any conflict returns a dictionary
        :return: a dictionary which is a list of conflict in the existing file
        """
        def write_to_file(lst, obj_file):
            """
            :param lst: list of dictionaries has been posted as new scheduling
            :param obj_file: file object is going to be written
            """
            cuntr = 1
            for item in lst:
                obj_file.write(str(cuntr) + ' ' + Mngip[item['CaNam']] + ' ' + item['ReNam'] + ' ' +
                               item['dt_s'] + ' ' + item['tm_s'] + ' ' + item['dt_e'] + ' ' +
                               item['tm_e'] + '\n')
                cuntr += 1

        def check_conflict(lst_schl):
            """
                checks if the file exists and if there is any conflict returns a dictionary
            :param lst_schl: list of dictionaries that has been posted as new scheduling
            :return: a dictionary which is a list of conflict in the existing file
            """
            global dict_cnflct
            dict_cnflct = {}
            file_path = "schedule"
            bl_cnflct = False
            cnflct_list = []
            if os.path.isfile(file_path):
                # file exist
                rw_lock = fasteners.ReaderWriterLock()
                with rw_lock.read_lock():
                    with open(file_path, 'r') as f_output:
                        y = 0
                        for line in f_output:
                            list_line = line.split()
                            date_s_obj = datetime.strptime(list_line[3], '%d:%m:%Y')
                            time_s = int(list_line[4])
                            date_e_obj = datetime.strptime(list_line[5], '%d:%m:%Y')
                            time_e = int(list_line[6])
                            for item in lst_schl:
                                # checking each row of new schedule with all rows in previous file
                                new_date_s = datetime.strptime(item['dt_s'], '%d:%m:%Y')
                                new_date_e = datetime.strptime(item['dt_e'], '%d:%m:%Y')
                                new_time_s = int(item['tm_s'])
                                new_time_e = int(item['tm_e'])
                                # checking if there is any conflict
                                if list_line[1] == Mngip[item['CaNam']] and\
                                        ((date_s_obj < new_date_s < date_e_obj) or (date_s_obj < new_date_e < date_e_obj)
                                         or (date_s_obj >= new_date_s and date_e_obj <= new_date_e)
                                         or (new_date_s < date_s_obj and new_date_e > date_e_obj))\
                                        and ((time_s < new_time_s < time_e) or (time_s < new_time_e < time_e)
                                             or (new_time_s <= time_s and new_time_e >= time_e)):
                                    bl_cnflct = True
                                    cnflct_list.extend([item['CaNam'], list_line[2], list_line[3],
                                                        list_line[4], list_line[5], list_line[6]])
                                    dict_cnflct.update({y: cnflct_list})
                                    y += 1
                                    cnflct_list = []

                    if not bl_cnflct:
                        # if there is no conflict append to the existing file
                        rw_lock = fasteners.ReaderWriterLock()
                        with rw_lock.read_lock():
                            with open(file_path, 'a') as f_output:
                                write_to_file(lst_schl, f_output)
                            return {0: "empty"}

                    elif bl_cnflct:
                        # return a dictionary of conflicts
                        return dict_cnflct

            else:
                # if the file doesn't exist
                rw_lock = fasteners.ReaderWriterLock()
                with rw_lock.read_lock():
                    with open(file_path, 'w') as f_output:
                        write_to_file(lst_schl, f_output)
                    return {0: "empty"}

        try:
            i = 1
            mylist = self.get_argument('table[%d][col1]' % i)
            dict_schl = {}
            lstA = []
            while mylist != "":
                CaNam = str(self.get_argument('table[%d][col1]' % i))
                ReNam = str(self.get_argument('table[%d][col2]' % i))
                dt_s = str(self.get_argument('table[%d][col3]' % i))
                dt_e = str(self.get_argument('table[%d][col4]' % i))
                dt_t = str(self.get_argument('table[%d][col5]' % i))
                tm_s = str(self.get_argument('table[%d][col6]' % i))
                tm_e = str(self.get_argument('table[%d][col7]' % i))
                dict_schl.update({'CaNam': CaNam, 'ReNam': ReNam, 'dt_s': dt_s, 'dt_e': dt_e, 'dt_t': dt_t,
                                  'tm_s': tm_s, 'tm_e': tm_e})
                lstA.append(dict_schl)
                dict_schl = {}
                i += 1
                mylist = self.get_argument('table[%d][col1]' % i)
        except:
            pass
        list_of_conflict = check_conflict(lstA)
        dict_cnflct = {}
        self.write(list_of_conflict)

class DrbHandler(tornado.web.RequestHandler):
    def post(self):
        """
            gets a value from CAs drop box and returns a dictionary of REs
        """
        value = self.get_argument('CA_drpBx')
        list_re = REs[str(value)]
        dict_re = {}
        i = 0
        # convert the list into a dictionary
        for re in list_re:
            dict_re.update({i: re})
            i += 1
        self.write(dict_re)

def make_app():
    return tornado.web.Application([(r"/", MainHandler),
                                    (r"/form", FormHandler), (r"/api/Ca", DrbHandler),
                                    (r"/api/view", ViewHandler)], **setting)

if __name__ == "__main__":
    csv_reader()
    create_date_list()
    app = make_app()
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()


import math
def fnd_prime_fctrs(inp):
	ans=[]
	while(inp%2==0):
		ans.append(2)
		inp/=2

	for k in range(3,int(math.sqrt(inp))+1,2):
		while(inp%k==0):
			ans.append(k)
			inp/=k

	if inp>2:
		ans.append(inp)
	print(ans)

fnd_prime_fctrs(35)


from operator import itemgetter
def get_csv_data(file):
    try:
        with open(file, "r") as csvdata:
            table_data = [ line.split(";") for line in csvdata]
        return table_data
    except FileNotFoundError:
        print ("Cannot find file",file)
        return None

def country_sort(csvdata):
    country_dict = {}
    country_total_elo = {}
    for line in csvdata:
        country = line[2].strip()
        if country not in country_dict:
            country_dict[country] = 1
            country_total_elo[country] = int(line[3])
        elif country in country_dict:
            country_dict[country] += 1
            country_total_elo[country] += int(line[3])
    return sorted(country_dict.items()), sorted(country_total_elo.items())

def print_header():
    print("Players by country:")
    print("-------------------")

def print_result_country(csvdata):
    sorted_csvdata = sorted(csvdata,key=itemgetter(2))
    num = 0
    playernumber = 0
    country_list, country_elo_list = country_sort(csvdata)
    for country in country_list:
        print("{} ({}) ({}):".format(country[0], country[1], round(int(country_elo_list[num][1]) / int(country[1]),1)))
        for x in range(country[1]):
            lastname, firstname = sorted_csvdata[playernumber][1].split(",")
            firstname.strip()
            lastname.strip()
            fullname = firstname + lastname
            print("{:>40}{:>10d}".format(fullname, int(sorted_csvdata[playernumber][3])))
            playernumber += 1
        num += 1

filename = input("Enter filename: ")
csvdata = get_csv_data(filename)
print_header()
if csvdata:
    print_result_country(csvdata)


from .module import FlexModule
import torch
from torch.nn import functional as F
from torch.nn import init

class _BatchNorm(FlexModule):
    _version = 2

    def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True,
                 track_running_stats=True):
        super(_BatchNorm, self).__init__()
        self.num_features = num_features
        self.eps = eps
        self.momentum = momentum
        self.affine = affine
        self.track_running_stats = track_running_stats
        # register weight and bias
        self.shapes['weight'] = (num_features,) if affine else None
        self.shapes['bias'] = (num_features,) if affine else None
        self.shapes['running_mean'] = (num_features,) if track_running_stats else None
        self.shapes['running_var'] = (num_features,) if track_running_stats else None
        self.shapes['num_batches_tracked'] = (1,) if track_running_stats else None
        self.register_nonleaf_parameter('weight', None)
        self.register_nonleaf_parameter('bias', None)
        self.register_buffer('running_mean', None)
        self.register_buffer('running_var', None)
        self.register_buffer('num_batches_tracked', None)

    def _check_input_dim(self, input):
        raise NotImplementedError

    def forward(self, input):
        self._check_input_dim(input)

        exponential_average_factor = 0.0

        if self.training and self.track_running_stats:
            self.num_batches_tracked += 1
            if self.momentum is None:  # use cumulative moving average
                exponential_average_factor = 1.0 / self.num_batches_tracked.item()
            else:  # use exponential moving average
                exponential_average_factor = self.momentum

        return F.batch_norm(
            input, self.running_mean, self.running_var, self.weight, self.bias,
            self.training or not self.track_running_stats,
            exponential_average_factor, self.eps)

    def extra_repr(self):
        return '{num_features}, eps={eps}, momentum={momentum}, affine={affine}, ' \
               'track_running_stats={track_running_stats}'.format(**self.__dict__)

class BatchNorm2d(_BatchNorm):
    def _check_input_dim(self, input):
        if input.dim() != 4:
            raise ValueError('expected 4D input (got {}D input)'
                             .format(input.dim()))

class BatchNorm3d(_BatchNorm):
    def _check_input_dim(self, input):
        if input.dim() != 5:
            raise ValueError('expected 5D input (got {}D input)'
                             .format(input.dim()))


import tkinter
from tkinter import filedialog as f
import pyttsx3  # text to speech conversion library(it works offline)
from PIL.ImageTk import PhotoImage
from PIL import Image

engine = pyttsx3.init()
screen = tkinter.Tk()
screen.title('Text to Speech Conversion GUI')
screen.geometry('600x450')
screen.configure(background='black')
photo = PhotoImage(Image.open("ts.jpg").resize((590, 290)))
lab = tkinter.Label(screen, image = photo)
lab.pack()

def Btn_click():
    s = f.askopenfile(initialdir = '/Desktop',filetypes = (("Text files","*.txt"),("PDF File","*.pdf*")))
    file = s.name

    with open(file,'r') as g:
        info = g.read()
        voices = engine.getProperty('voices')
        a.setProperty("language",'hi')
        engine.setProperty("rate", 100) # speed of voice
        engine.setProperty('voice', voices[1].id)  # to set voice

        engine.say(info)
        engine.save_to_file(info,'tttttt.mp3')

        engine.runAndWait()

def scren_close():
    screen.destroy()

label = tkinter.Label(screen, text = 'Please Upload your Text and I will Read it for you',padx = 5,pady = 5,
    background = 'black', foreground = 'white',font = ('Times new Roman',14))
label.pack()

Button = tkinter.Button(screen, text = 'Search for the Text', command = Btn_click,padx = 1,pady = 1,background = 'blue', foreground = 'white',font = ('Times new Roman',14) )
Button.pack()

Button2 = tkinter.Button(screen, text = 'Stop Reading and Close Window', command = scren_close,padx = 1,pady = 1,background = 'blue', foreground = 'white',font = ('Times new Roman',14) )
Button2.pack()

screen.mainloop()


import time
import os
import sys
sys.path.append(os.getcwd())
import re
#import logger
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import yaml
import json
import numpy as np
import numpy.testing as npt
from loguru import logger
from util.redis_producer import redis_producer
#from multiprocessing import Lock
import threading
#logger = logger.getLogger()
#logger.setLevel(logger.INFO)

class TaskRunnerAPI:
    conf = None
    token = None
    def __init__(self):
        self.conf = {}
        logger.info("runner init")
        self.conf["site"] = os.getenv("consolesite")
        self.conf["user"] = os.getenv("consoleuser")
        self.conf["passwd"] = os.getenv("consolepasswd")
        #fr = open('./taskrunner/conf.yml')
        #self.conf = yaml.load(fr)
        #logger.info(self.conf)
        #fr.close()
        self.MQ = redis_producer()

    def produces(self,api,time=None,isOK=True):
        return
        tmp = {}
        tmp["api"] = api
        if isOK == False:
            tmp["isOK"] = False
        else:
            tmp["isOK"] = True
            tmp["time"] = time
        self.MQ.producer(json.dumps(tmp))

    def pub_msg(self,msg):
        return
        self.MQ.pub_performace(msg=msg)

    def run(self,code,name=None):
        logger.info(threading.current_thread().name)
        #return True
        if not self.login():
            logger.error("login failed")
            return False
        aliveds = self.find_allalive_ds()
        if aliveds == [] or aliveds == False:
            logger.error("no alive ds")
            return False
        datasource = self.find_metadata(aliveds)
        if not datasource:
            logger.error("no data")
            return False

        logger.info("running job")
        #[{""resultVarName"":""result"",""resultDest"":""-1""}]
        var_result = self.code_reveal(code=code)
        result = []
        for var in var_result:
            tmp = {
                "resultVarName":var,
                "resultDest" : datasource.get("dataServerId")
            }
            result.append(tmp)
        datasource_l = []
        datasource["varName"] = "data"
        datasource_l.append(datasource)
        return  self.job_pipline(datasource=[] ,result=result,code=self.sub_debug_reveal(code=code),name=name)

    def code_reveal(self,code):
        print(code.split('\n'))
        re_reveal = re.compile(r'''^[a-zA-Z_\-0-9\ \=]+\.(?:debug_reveal|reveal)\([a-zA-Z_0-9\ \[\]\-\.\(\),=]+['"]([a-zA-Z_\-0-9]+)['"]''')
        reveal = []
        for line in code.split('\n'):
            tmp = re.search(re_reveal,line)
            if tmp:
                print(line)
                reveal.append(tmp.group(1))
        return  reveal

    def sub_debug_reveal(self,code):
        re_reveal = re.compile(r'\.debug_reveal\(')
        return re.sub(re_reveal,".reveal(",code)

    def job_pipline(self,datasource,result,code,name=None):
        logger.info("start job")
        begin = time.time()
        jobid = self.job_create(datasource=datasource,result=result,code=code,name=name)
        if jobid == False:
            return False
        if not self.job_start(jobid=jobid):
            return False
        if not  self.job_status(jobid=jobid):
            return  False
        result =  self.job_result(jobid=jobid)
        end = time.time()
        if len(result.keys()) != 0:
            msg = {}
            msg["jobname"] = name
            msg["time"]    = end-begin
            self.pub_msg(msg=msg)
            #self.job_delete(jobid=jobid)
        return result

    def job_create(self,datasource,result,code,name=None):
        if name == None:
            jobname = 'privpy-libarary'
        else:
            jobname = 'privpy-libarary-'+name
        body = {
            "name":jobname,
            "code":code,
            "taskDataSourceVOList":datasource,
            "taskResultVOList":result
        }
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        logger.info(body)
        try:
            if "http://" in self.conf.get("site"):
                url = "%s/api/api-tm/task" % self.conf.get("site")
            else:
                url = "https://%s/api/api-tm/task" %(self.conf.get("site"))
            while 1:
                req = requests.post(url=url,headers=head,data=json.dumps(body),verify=False)
                self.produces(api="POST/api/api-tm/task", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    logger.info(req.text)
                    time.sleep(1)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                if req.json().get("code") != 0:
                    logger.error("create job failed :%s"%(req.text))
                    return False
                return req.json().get("data").get("id")
        except Exception as err:
            self.produces(api="POST/api/api-tm/task", isOK=False)
            logger.error(err)
            logger.error(req.text)
            return False
    def job_start(self,jobid):
        logger.info("start job:%s"%(str(jobid)))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }

        try:
            if "http://" in self.conf.get("site"):
                url = "%s/api/api-tm/task/startTask" % self.conf.get("site")
            else:
                url = "https://%s/api/api-tm/task/startTask" %(self.conf.get("site"))
            url = "%s/%s"%(url,str(jobid))
            while 1:
                req = requests.put(url=url,headers=head,verify=False)
                self.produces(api="PUT/api/api-tm/task/startTask", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                if req.json().get("code") != 0:
                    logger.error("start job failed:%s"%(req.text))
                    return False
                else:
                    return True
        except Exception as err:
            self.produces(api="PUT/api/api-tm/task/startTask", isOK=False)
            logger.error(err)
        return True

    def job_delete(self,jobid):
        logger.info("delete job :%s" % (str(jobid)))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-tm/task" % self.conf.get("site")
        else:
            url = "https://%s/api/api-tm/task" % (self.conf.get("site"))
        url = '%s/%s' % (url, str(jobid))
        try:
            while 1:
                req = requests.delete(url=url, headers=head, verify=False)
                self.produces(api="DELETE/api/api-tm/task", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                else:
                    return True

        except Exception as err:
            self.produces(api="DELETE/api/api-tm/task",  isOK=False)
            logger.error(err)
            logger.error(req.text)
            return False

    def job_status(self,jobid):
        logger.info("check job status:%s"%(str(jobid)))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-tm/task" % self.conf.get("site")
        else:
            url = "https://%s/api/api-tm/task" % (self.conf.get("site"))
        url = '%s/%s' % (url, str(jobid))
        tryCount = 0
        try:
            while tryCount < 300:
                req = requests.get(url=url,headers=head,verify=False)
                self.produces(api="GET/api/api-tm/task", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                if req.json().get("data").get("queueStatus") == 6 or req.json().get("data").get("queueStatus") == 7  or req.json().get("data").get("queueStatus") == 8:
                    logger.info("job finished")
                    return True
                logger.info("job:%s is running ...."%(str(jobid)))
                time.sleep(6)
                tryCount =  tryCount + 1

        except Exception as err:
            self.produces(api="GET/api/api-tm/task", isOK=False)
            logger.error(err)
            logger.error(req.text)
            return False

    def job_result(self,jobid):
        logger.info("get job result")
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-tm/task/getTaskResult" % self.conf.get("site")
        else:
            url = "https://%s/api/api-tm/task/getTaskResult" % (self.conf.get("site"))
        url = '%s/%s' % (url, str(jobid))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        result_ditc = {}
        try:
            get_result = True
            while 1:
                get_result = True
                req = requests.get(url=url,headers=head,verify=False)
                self.produces(api="GET/api/api-tm/task/getTaskResult", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                for res in req.json().get("data"):
                    tmp = {}
                    #logger.info(type(eval(res.get("result"))))
                    if res.get("result") == None:
                        get_result = False
                    try:
                        if isinstance((eval(res.get("result"))),list):
                            logger.info("list result")
                            tmp['val'] = np.array(eval(res.get("result")),dtype=float)
                            logger.info(tmp['val'].dtype)
                        else:
                            tmp['val'] = eval(res.get("result"))
                        result_ditc[res.get("resultVarName")] = tmp
                    except Exception as err:
                        logger.info(err)
                        tmp['val'] = res.get("result")
                        result_ditc[res.get("resultVarName")] = tmp
                if get_result == False:
                    time.sleep(3)
                    continue
                return result_ditc
        except Exception as err:
            self.produces(api="GET/api/api-tm/task/getTaskResult",  isOK=False)
            logger.error(err)
            logger.error(req.text)
            return result_ditc

    def login(self):
        threadname = threading.current_thread().name
        logger.info("login :%s"%(self.conf.get("site")))
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-sso/token/simpleLogin" % self.conf.get("site")
        else:
            url = "https://%s/api/api-sso/token/simpleLogin" % (self.conf.get("site"))
        if 'multiRun' in threadname:
            username = self.conf.get("user")+str((int(threadname.split('_')[-1])+1)%(int(self.conf.get("thread"))))
        else:
            username = self.conf.get("user")
        data = "username=%s&password=%s" % (username, self.conf.get("passwd"))
        logger.info(data)
        try:
            req = requests.post(url=url, params=data, verify=False)
            # print(req.text)
            self.token = req.json().get("data").get("access_token")
            self.produces(api="POST/api/api-sso/token/simpleLogin", time=req.elapsed.total_seconds() * 1000, isOK=True)
        except Exception as err:
            logger.error(err)
            self.produces(api="POST/api/api-sso/token/simpleLogin",  isOK=False)
            return False
        return  True

    def find_allalive_ds(self):
        logger.info("find ds")
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-tm/dataServer" % self.conf.get("site")
        else:
            url = "https://%s/api/api-tm/dataServer" % (self.conf.get("site"))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        alive_ds = []
        page = 1
        try:
            while 1:
                req = requests.get(url='%s?page=%s'%(url,str(page)),headers=head,verify=False)
                self.produces(api="GET/api/api-tm/dataServer", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                if len(req.json().get("data").get("data")) == 0:
                    logger.error("ds is empty")
                    return False
                for ds in req.json().get("data").get("data"):
                    if ds.get("status") == '1' and ds.get("isCache") == 0:
                        alive_ds.append(ds.get("id"))
                if page == req.json().get("data").get("nextPageNo"):
                    return  alive_ds
                page = page + 1
        except Exception as err:
            self.produces(api="GET/api/api-tm/dataServer", isOK=False)
            logger.error(req.text)
            logger.error(err)
            return False

    def find_metadata(self,aliveds = []):
        datasource = {}
        logger.info("find metadata")
        if "http://" in self.conf.get("site"):
            url = "%s/api/api-tm/dataSourceMetadata" % self.conf.get("site")
        else:
            url = "https://%s/api/api-tm/dataSourceMetadata" % (self.conf.get("site"))
        head = {
            "Authorization": "bearer %s" % (self.token),
            "Content-Type": "application/json"
        }
        page = 1
        try:
            while 1:
                req = requests.get(url='%s?page=%s'%(url,str(page)),headers=head,verify=False)
                self.produces(api="GET/api/api-tm/dataSourceMetadata", time=req.elapsed.total_seconds() * 1000, isOK=True)
                logger.info(req.text)
                if req.json().get("subCode") == 'GLOBAL0004':
                    time.sleep(1)
                    logger.info(req.text)
                    self.login()
                    head["Authorization"] = "bearer %s" % (self.token)
                    continue
                if len(req.json().get("data").get("data")) == 0:
                    logger.info("data is empty")
                    return False
                for data in req.json().get("data").get("data"):
                    if data.get("dsId")  in aliveds:
                        datasource["dataSourceId"] = req.json().get("data").get("data")[0].get("dataSetId")
                        datasource["dataSourceMetadataId"] = req.json().get("data").get("data")[0].get("id")
                        datasource["dataServerId"] = req.json().get("data").get("data")[0].get("dsId")
                        return  datasource
                if page == req.json().get("data").get("nextPageNo"):
                    return False
                page = page + 1
        except Exception as err:
            self.produces(api="GET/api/api-tm/dataSourceMetadata", isOK=True)
            logger.error(err)
            logger.error(req.text)
            return False

if __name__ == '__main__':
    runner = TaskRunnerAPI()
    code= '''
import privpy as pp
import os
import sys
sys.path.append(os.getcwd() + '/privpy_lib')
import pnumpy as pnp
import numpy as np

a = pp.farr([1, 2])
b = pp.farr([1, 2, 3])
c = pp.farr([3, 4])
d = pp.farr([1, 3])

s1 = pnp.array_equal(a, a)
s2 = pnp.array_equal(a, b)
s3 = pnp.array_equal(a, c)
s4 = pnp.array_equal(a, d)

pp.debug_reveal(s1, 's1')
pp.debug_reveal(s2, 's2')
pp.debug_reveal(s3, 's3')
pp.debug_reveal(s4, 's4')
    '''
    #print(runner.sub_debug_reveal(code))
    #print(runner.code_reveal(code=code))
    res = (runner.run(code=code))
    print(res)
    a = np.array([1, 2])
    b = np.array([1, 2, 3])
    c = np.array([3, 4])
    d = np.array([1, 3])

    #res = test_util.run_task(code)
    s1 = res['s1']['val']
    s2 = res['s2']['val']
    s3 = res['s3']['val']
    s4 = res['s4']['val']

    npt.assert_array_almost_equal(s1, np.array_equal(a, a))
    npt.assert_array_almost_equal(s2, np.array_equal(a, b))
    npt.assert_array_almost_equal(s3, np.array_equal(a, c))
    npt.assert_array_almost_equal(s4, np.array_equal(a, d))
    print('assert ok')


# -*- coding: utf-8 -*-
"""Cisco DNA Center GetAuthenticationAndPolicyServers data model.

Copyright (c) 2019-2021 Cisco Systems.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import json
from builtins import *

import fastjsonschema

from dnacentersdk.exceptions import MalformedRequest

class JSONSchemaValidatorF7Cc2592721F5B9B9F99795A26130147(object):
    """GetAuthenticationAndPolicyServers request schema definition."""
    def __init__(self):
        super(JSONSchemaValidatorF7Cc2592721F5B9B9F99795A26130147, self).__init__()
        self._validator = fastjsonschema.compile(json.loads(
            '''{
                "$schema": "http://json-schema.org/draft-04/schema#",
                "properties": {
                "response": {
                "items": {
                "properties": {
                "accountingPort": {
                "type": "string"
                },
                "authenticationPort": {
                "type": "string"
                },
                "ciscoIseDtos": {
                "items": {
                "properties": {
                "description":
                 {
                "type": "string"
                },
                "externalCiscoIseIpAddrDtos": {
                "properties": {
                "externalCiscoIseIpAddresses": {
                "items": {
                "properties": {
                "externalIpAddress": {
                "type": "string"
                }
                },
                "type": "object"
                },
                "type": "array"
                },
                "type": {
                "type": "string"
                }
                },
                "type": "object"
                },
                "failureReason": {
                "type": "string"
                },
                "fqdn": {
                "type": "string"
                },
                "instanceUuid": {
                "type": "string"
                },
                "ipAddress": {
                "type": "string"
                },
                "password": {
                "type": "string"
                },
                "role": {
                "enum": [
                "PRIMARY",
                "SECONDARY",
                "PXGRID"
                ],
                "type": "string"
                },
                "sshkey": {
                "type": "string"
                },
                "subscriberName": {
                "type": "string"
                },
                "trustState": {
                "enum": [
                "TRUSTED",
                "UNTRUSTED",
                "INIT"
                ],
                "type": "string"
                },
                "type": {
                "type": "string"
                },
                "userName": {
                "type": "string"
                }
                },
                "type": "object"
                },
                "type": "array"
                },
                "encryptionKey": {
                "type": "string"
                },
                "encryptionScheme": {
                "enum": [
                "KEYWRAP",
                "RADSEC"
                ],
                "type": "string"
                },
                "instanceUuid": {
                "type": "string"
                },
                "ipAddress": {
                "type": "string"
                },
                "isIseEnabled": {
                "type": "boolean"
                },
                "iseEnabled": {
                "type": "boolean"
                },
                "messageKey": {
                "type": "string"
                },
                "port": {
                "type": "integer"
                },
                "protocol": {
                "enum": [
                "TACACS",
                "RADIUS",
                "RADIUS_TACACS"
                ],
                "type": "string"
                },
                "pxgridEnabled": {
                "type": "boolean"
                },
                "retries": {
                "type": "integer"
                },
                "role": {
                "type": "string"
                },
                "sharedSecret": {
                "type": "string"
                },
                "state": {
                "enum": [
                "ACTIVE",
                "INACTIVE",
                "RBAC_SUCCESS",
                "RBAC_FAILURE",
                "DELETED",
                "FAILED",
                "INPROGRESS"
                ],
                "type": "string"
                },
                "timeoutSeconds": {
                "type": "integer"
                },
                "useDnacCertForPxgrid": {
                "type": "boolean"
                }
                },
                "type": "object"
                },
                "type": "array"
                }
                },
                "type": "object"
                }'''.replace("\n" + ' ' * 16, '')
        ))

    def validate(self, request):
        try:
            self._validator(request)
        except fastjsonschema.exceptions.JsonSchemaException as e:
            raise MalformedRequest(
                '{} is invalid. Reason: {}'.format(request, e.message)
            )


# My solution: [sort + lambda]
class Solution(object):
    def sortedSquares(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        return list(map(lambda x: x * x, sorted(A, key = abs)))

#Solution: [Sort]
class Solution(object):
    def sortedSquares(self, A):
        return sorted(x*x for x in A) #sorted function costs O(NlogN) Time complexity

#[Two pointers]: Since the array is sorted with negative values ahead of positive values
class Solution(object):
    def sortedSquares(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        j = 0
        N = len(A)

        while j < N and A[j] < 0:             #i for index of negative value, j for index of positive value
            j += 1

        i = j - 1

        new = []
        while 0 <=i and j < N:                #when both i and j within the range, compare their value
            if A[i] **2 < A[j] **2:
                new.append(A[i] **2)
                i -= 1
            else:
                new.append(A[j] **2)
                j +=1

        while i >= 0:                        #without j
            new.append(A[i] ** 2)
            i -= 1
        while j < N:                         #without i
            new.append(A[j] **2)
            j += 1

        return new


#!/usr/bin/python3
#This code is a parallelized version of the py_graphthest_0_1.py code.
#Also, this code has (from lines 21-41) a commented graph (in the appropriate edge labels) for use with the project.

import numpy as np
from pyglga_lib import createPop, createPop2, gracefulCross, getGraph, objFcn, glga, graphSort
import time
import multiprocessing as mp
from functools import partial
import sys
from datetime import date, datetime

def cp1(input,iterable):
	#return np.random.choice(Range+1,Range,replace=False)

	g_type=input["Graph_type"]
	g_size=input["Graph_size"]
	#Get graph edge list
	graph=getGraph(g_type,g_size)
	#g1=[[20,19],
 	# 		[ 1,2],
 	# 		[ 1,3],
	# 		[ 3,4],
	# 		[ 4,5],
	# 		[ 2,6],
	# 		[ 5,7],
 	# 		[ 5,8],
 	# 		[ 4,9],
 	# 		[ 6,10],
 	# 		[ 8,11],
 	# 		[ 8,12],
 	# 		[ 5,13],
 	# 		[10,14],
 	# 		[11,15],
	# 		[11,16],
	# 		[11,17],
	# 		[ 3,18],
	# 		[ 7,19],
	# 		[ 9,20]]
	# graph=np.array(g1)

	input["graph"]=graph

	t1=time.time()
	BestSoln,gens,score=glga(input)
	t2=time.time()-t1
	print(iterable,'\t',f"{gens:10}",'\t',f"{score:.6}",'\t',f"{t2:.6}")

	return BestSoln,graph,iterable,gens,score,t2

def cp_parallel(input):

	print('Graph\t\tGens\t Score\t Time(s)')

	p=mp.Pool()

	iterable=range(100)
	results=p.map(partial(cp1,input),iterable)
	#print(results)

	p.close()
	p.join()

	#print(results)

	return np.array(results)

if __name__=="__main__":

	graph_type=["complete","cycles","path","tree","wheel","random_normal_caterpillar","complete_bipartite","crown","helm","gen_Petersen"]
	graph_size={
		"complete":[5,8,10,15,16,20,24,30],
		"cycles":[8,10,15,20,25,30,40,50],
		"path":[5,10,20,25, 27,28,30,40,50],
		"tree":[5,10,18,20,25,28,30,35,40,50,60,70,80,90,100],
		"wheel":[7,8,10,15,20,30],
		"random_normal_caterpillar":[5,8,10,15,30,45],
		"complete_bipartite":[[5,5],[5,10],[5,20],[10,20]],
		"crown":[5,8,10,15,20,30],
		"helm":[5,8,10,20,30],
		"gen_Petersen":[[5,2],[6,3],[7,3],[8,4],[9,4],[10,5]]
	}

	#Determine Graph Type
	g_type=graph_type[3]

	#Determine Graph Size
	g_size=graph_size[g_type][0]

	#Get graph edge list
	#graph=getGraph(g_type,g_size)
	#graph=

	#Inputs Dictionary
	input={
		"Graph_type":g_type,
		"Graph_size":g_size,
		"PopSize":500,#30*g_size, # these should always be even.
		"Range":g_size,
		"maxGenerations": 1e11,
		"output":'silent',#verbose',
		"swaps":.25,
		"restart_Iter":2e3,
		"restart": True
	}

	#pop=createPop(input["PopSize"],input["Range"])
	#gracefulCross(pop,input["swaps"],input["Range"])

	###############Initialize GA
	#pool=multiprocessing.Pool()
	#for i in range(10):
	#BestSoln,gens,score=glga(input)
	#pool.map(glga(input),range(1))
	#start=time.time()
	res=cp_parallel(input)
	#print(res)

	today=datetime.now()
	d1=today.strftime("%d%m%Y_%H%M%S")
	filename=g_type+str(g_size)+'_'+d1+'.csv'

	gens_Avg=0
	score_Avg=0
	time_Avg=0

	with open(filename, 'w') as f:

		for i in range(100):
			iterable=res[i,2]
			#iter_Avg+=iterable/100
			gens=res[i,3]
			gens_Avg+=gens/100
			score=res[i,4]
			score_Avg+=score/100
			time=res[i,5]
			time_Avg+=time/100
			print(iterable,',',gens,',',score,',',time,file=f)

	print(g_type+ ' graphs of size '+str(g_size))
	print('Mean runtime: ' + f"{time_Avg:.6}"+' secs')
	print('Mean no. generations: ' + f"{gens_Avg:.6}")
	print('Mean score: ' + f"{score_Avg:.6}")


import unittest
from PokerCalcMain import calculate_deuces

class TestPokerMain(unittest.TestCase):
    """
    Unit tests for PokerCalcMain
    """

    def test_calculate_deuces(self):
        """
        Method for getting the deuces score
        :return:
        """

        board = ['4h', '5h', '8h', '7h', '9c']
        hand = ['2s', '3s']

        deuces = calculate_deuces(board, hand)

        self.assertEqual(deuces[0], 7414)
        self.assertEqual(deuces[1], 'High Card')

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


__author__ = 'andy'
import os
import random

def getPathM():
    fileName = os.path.realpath(__file__)
    filePath = os.path.dirname(fileName)
    return filePath

# http://www.careercup.com/question?id=5398298810646528
def randRepData():
    a = []
    for i in range(500):
        a += [i] * random.randint(0, 499)
    random.shuffle(a)
    return a

#randRepData()
#print(randRepData())

def lambdaTest():
    cng = lambda j: j+1
    i = 1
    print(i)
    b, c = cng(i)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('shop', '0029_auto_20150314_1224'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='bills',
            options={'ordering': ['-modified_date'], 'verbose_name': 'Счет', 'verbose_name_plural': 'Счета'},
        ),
    ]


from flask import jsonify
#from markdown import markdown

def checkPrime(n):
    if(n == 0 or n == 1):
        #print(markdown('%d is neither prime nor composite.' % n))
        outstr = str(n) + ' is neither prime nor composite.'
        #return
    elif(n < 0):
        #print(markdown('%d cannot be prime.' % n))
        outstr = str(n) + ' cannot be prime.'
        #return
    else:
        for i in range(2, n):
            if(n % i == 0):
                #print(markdown('%d is not prime because %d / %d = %d' % (n, n, i, n/i)))
                outstr = str(n) + ' is not prime because ' + str(n) + ' / ' + str(i) + ' = ' + str(n/i);
                #return
                break
    #print(markdown('%d is prime.' % n))
    return(jsonify(outstr))


#! /usr/bin/env python
# encoding: utf-8

from waflib.Build import BuildContext
from waflib import Logs
import waflib

import os

top = "."

VERSION = "5.0.0"

class UploadContext(BuildContext):
    cmd = "upload"
    fun = "upload"

def options(opt):
    opt.add_option(
        "--run_tests", default=False, action="store_true", help="Run all unit tests"
    )

    opt.add_option(
        "--pytest_basetemp",
        default="pytest_temp",
        help="Set the basetemp folder where pytest executes the tests",
    )

def build(bld):
    # Create a virtualenv in the source folder and build universal wheel
    with bld.create_virtualenv() as venv:
        venv.run(cmd="python -m pip install wheel")
        venv.run(cmd="python setup.py bdist_wheel --universal", cwd=bld.path)

        # Run the unit-tests
        if bld.options.run_tests:
            _pytest(bld=bld, venv=venv)

    # Delete the egg-info directory, do not understand why this is created
    # when we build a wheel. But, it is - perhaps in the future there will
    # be some way to disable its creation.
    egg_info = os.path.join(bld.path.abspath(), "pytest_testdirectory.egg-info")

    if os.path.isdir(egg_info):
        waflib.extras.wurf.directory.remove_directory(path=egg_info)

def _find_wheel(ctx):
    """Find the .whl file in the dist folder."""

    wheel = ctx.path.ant_glob("dist/*-" + VERSION + "-*.whl")

    if not len(wheel) == 1:
        ctx.fatal("No wheel found (or version mismatch)")
    else:
        wheel = wheel[0]
        Logs.info("Wheel %s", wheel)
        return wheel

def upload(bld):
    """Upload the built wheel to PyPI (the Python Package Index)"""

    with bld.create_virtualenv() as venv:
        venv.run("python -m pip install twine")

        wheel = _find_wheel(ctx=bld)

        venv.run(f"python -m twine upload {wheel}")

def docs(ctx):
    """Build the documentation"""

    ctx.pip_compile(
        requirements_in="docs/requirements.in", requirements_txt="docs/requirements.txt"
    )

    with ctx.create_virtualenv() as venv:
        venv.run("python -m pip install -r docs/requirements.txt")

        build_path = os.path.join(ctx.path.abspath(), "build", "docs")

        venv.run("giit clean . --build_path {}".format(build_path))
        venv.run("giit sphinx . --build_path {}".format(build_path))

def prepare_release(ctx):
    """Prepare a release."""

    with ctx.rewrite_file(filename="setup.py") as f:
        pattern = r'VERSION = "\d+\.\d+\.\d+"'
        replacement = f'VERSION = "{VERSION}"'

        f.regex_replace(pattern=pattern, replacement=replacement)

def _pytest(bld, venv):
    # To update the requirements.txt just delete it - a fresh one
    # will be generated from test/requirements.in
    if not os.path.isfile("test/requirements.txt"):
        venv.run("python -m pip install pip-tools")
        venv.run(
            "pip-compile setup.py test/requirements.in "
            "--output-file test/requirements.txt"
        )

    venv.run("python -m pip install -r test/requirements.txt")

    # Install the pytest-testdirectory plugin in the virtualenv
    wheel = _find_wheel(ctx=bld)

    venv.run("python -m pip install {}".format(wheel))

    # We override the pytest temp folder with the basetemp option,
    # so the test folders will be available at the specified location
    # on all platforms. The default location is the "pytest" local folder.
    basetemp = os.path.abspath(os.path.expanduser(bld.options.pytest_basetemp))

    # We need to manually remove the previously created basetemp folder,
    # because pytest uses os.listdir in the removal process, and that fails
    # if there are any broken symlinks in that folder.
    if os.path.exists(basetemp):
        waflib.extras.wurf.directory.remove_directory(path=basetemp)

    testdir = bld.path.find_node("test")

    # Make python not write any .pyc files. These may linger around
    # in the file system and make some tests pass although their .py
    # counter-part has been e.g. deleted
    venv.run("python -B -m pytest {} --basetemp {}".format(testdir.abspath(), basetemp))


from __future__ import print_function
import argparse
import os
import pdb
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from tensorboardX import SummaryWriter
from MyDataset import MyDataset
from Generator import Generator
from Discriminator import Discriminator

parser = argparse.ArgumentParser()
parser.add_argument('--ngpu',           type=int, default=1, help='number of GPUs to use')
parser.add_argument('--dim_z',          type=int, default=100, help='size of the latent z vector')
parser.add_argument('--dim_g',          type=int, default=64)
parser.add_argument('--dim_d',          type=int, default=64)
parser.add_argument('--batch_size',     type=int, default=64, help='input batch size')
parser.add_argument('--nepoch',         type=int, default=25, help='number of epochs to train for')
parser.add_argument('--nworkers',       type=int, help='number of data loading workers', default=2)
parser.add_argument('--manual_seed',    type=int, help='manual seed')
parser.add_argument('--image_size',     type=int, default=64, help='the height / width of the input image to network')
parser.add_argument('--cuda',           action='store_true', help='enables cuda')
parser.add_argument('--lr',             type=float, default=0.0002, help='learning rate, default=0.0002')
parser.add_argument('--beta1',          type=float, default=0.5, help='beta1 for adam. default=0.5')
# parser.add_argument('--dataset',        required=True, help='cifar10 | lsun | mnist |imagenet | folder | lfw | fake')
parser.add_argument('--dataroot',       required=True, help='path to dataset')
parser.add_argument('--output_folder',  default='.', help='folder to output images and model checkpoints')
parser.add_argument('--log_directory',  type=str, help='directory to save log summaries', default="./log/1225/")
parser.add_argument('--netG',           default='', help="path to netG (to continue training)")
parser.add_argument('--netD',           default='', help="path to netD (to continue training)")

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find("BatchNorm") != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)

if __name__ == '__main__':
    opt = parser.parse_args()
    print(opt)

    try:
        os.makedirs(opt.output_folder)
    except OSError:
        pass

    if opt.manual_seed is None:
        opt.manual_seed = random.randint(1, 100000)
    print("Random Seed: ", opt.manual_seed)
    random.seed(opt.manual_seed)
    torch.manual_seed(opt.manual_seed)

    cudnn.benchmark = True

    if torch.cuda.is_available() and not opt.cuda:
        print('WARNING: You have a CUDA device, so you should probably run with --cuda')

    train_augmentation = transforms.Compose([transforms.Resize(opt.image_size),
                                             transforms.CenterCrop(opt.image_size),
                                             transforms.ToTensor(),
                                             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    dataset = MyDataset(opt.dataroot, train_augmentation)
    num_channel = 3

    # if opt.dataset in ['imagenet', 'folder', 'lfw']:
    #     dataset = dset.ImageFolder(root=opt.dataroot,
    #                             transform=transforms.Compose([
    #                                 transforms.Resize(opt.image_size),
    #                                 transforms.CenterCrop(opt.image_size),
    #                                 transforms.ToTensor(),
    #                                 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ##
    #                             ]))
    #     num_channel = 3
    # elif opt.dataset == 'lsun':
    #     dataset = dset.LSUN(root=opt.dataroot, classes=['bedroom_train'],
    #                         transform=transforms.Compose([
    #                             transforms.Resize(opt.image_size),
    #                             transforms.CenterCrop(opt.image_size),
    #                             transforms.ToTensor(),
    #                             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    #                         ]))
    #     num_channel = 3
    # elif opt.dataset == 'cifar10':
    #     dataset = dset.CIFAR10(root=opt.dataroot, download=True,
    #                         transform=transforms.Compose([
    #                             transforms.Resize(opt.image_size),
    #                             transforms.ToTensor(),
    #                             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    #                         ]))
    #     num_channel = 3
    # elif opt.dataset == 'mnist':
    #     dataset = dset.MNIST(root=opt.dataroot, download=True,
    #                         transform=transforms.Compose([
    #                             transforms.Resize(opt.image_size),
    #                             transforms.ToTensor(),
    #                             transforms.Normalize((0.5,), (0.5,)),
    #                         ]))
    #     num_channel = 1
    # elif opt.dataset == 'fake':
    #     dataset = dset.FakeData(image_size=(3, opt.imageSize, opt.imageSize),
    #                             transform=transforms.ToTensor())
    #     num_channel = 3
    assert dataset
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size,
                                            shuffle=True, num_workers=int(opt.nworkers))

    device = torch.device("cuda:0" if opt.cuda else "cpu")
    ngpu  = int(opt.ngpu)
    dim_z = int(opt.dim_z)
    dim_g = int(opt.dim_g)
    dim_d = int(opt.dim_d)

    netG = Generator(ngpu, dim_z, dim_g, num_channel).to(device)
    netG.apply(weights_init)
    if opt.netG != '':
        netG.load_state_dict(torch.load(opt.netG))
    print(netG)

    netD = Discriminator(ngpu, dim_d, num_channel).to(device)
    netD.apply(weights_init)
    if opt.netD != '':
        netD.load_state_dict(torch.load(opt.netD))
    print(netD)

    criterion = nn.BCELoss()

    fixed_noise = torch.randn(opt.batch_size, dim_z, 1, 1, device=device)
    real_label = 1
    fake_label = 0

    optimD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
    optimG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))

    writer = SummaryWriter(log_dir=opt.log_directory)

    for epoch in range(opt.batch_size):
        for i, data in enumerate(dataloader):
            # (1) Update D network: minimize -( log(D(x)) + log(1 - D(G(z))) )
            netD.zero_grad()
            real = data.to(device)  ## [0]?
            batch_size = real.size(0)
            label = torch.full((batch_size,), real_label, device=device)

            output = netD(real)
            errD_real = criterion(output, label)
            errD_real.backward()
            D_x = output.mean().item()

            noise = torch.randn(batch_size, dim_z, 1, 1, device=device)
            fake = netG(noise)
            label.fill_(fake_label)
            output = netD(fake.detach())
            errD_fake = criterion(output, label)
            errD_fake.backward()
            D_G_z1 = output.mean().item()
            errD = errD_real + errD_fake
            optimD.step()

            # (2) Update G network: minimize -log(D(G(z)))
            netG.zero_grad()
            label.fill_(real_label)
            output = netD(fake)
            errG = criterion(output, label)
            errG.backward()
            D_G_z2 = output.mean().item()
            optimG.step()

            print('[%d/%d][%d/%d] LossD: %.4f, LossG: %.4f, D(x): %.4f, D(G(z)): %.4f & %.4f' \
            % (epoch, opt.nepoch, i, len(dataloader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))

            writer.add_scalar('Generator/Loss',     errG.item(), len(dataloader) * (epoch - 1) + i)
            writer.add_scalar('Discriminator/Loss', errD.item(), len(dataloader) * (epoch - 1) + i)

            if i % 100 == 0:
                vutils.save_image(real, '%s/real_samples.png' % opt.output_folder, normalize=True)
                fake = netG(fixed_noise)
                vutils.save_image(fake.detach(), '%s/fake_samples_epoch_%03d.png' % (opt.output_folder, epoch), normalize=True)

        # do checkpointing
        torch.save(netG.state_dict(), '%s/netG_epoch_%d.pkl' % (opt.output_folder, epoch))
        torch.save(netD.state_dict(), '%s/netD_epoch_%d.pkl' % (opt.output_folder, epoch))


# coding=utf-8

import dingbot
import sys

help=u'''钉钉机器人的命令行支持。

dingbot -init
dingbot -setup name webhook [secret]
dingbot -robot login/logout name1 [name2...]
dingbot -list [all/using]
  钉钉机器人的管理模块
  -init   -i    初始化命令行的参数，清除载入的机器人
  -setup  -s    安装一个钉钉机器人，用于后续操作
  -robot  -r    载入或者移除机器人，可以同时指定多个。
                当使用发送模块的时候，所有被载入的机器人都会同步发送消息。
  -list   -l    列出所有安装或者载入的机器人，默认为all

dingbot -text content
dingbot -image file-path
dingbot -link url
  钉钉机器人的发送模块
  -text   -t    发送字符串
  -image        发送图片
  -link         发送链接

更多帮助,请访问 https://github.com/WuJunkai2004/Dingbot'''

class config(dingbot._configure_manage):
    __path__ = r'.\dingbot_cli.ini'
    __inst__ = {u'robot':[]}

    def for_each(self):
        for robot in self.data[u'robot']:
            yield dingbot.DingAPI( dingbot.DingManage(robot) )

bot_config = dingbot.config()
cli_config = config()

class cmd:
    def __init__(self, opt):
        '初始化'
        self.argv = {}      # 内部码
        self.attr = opt     # 外部码

        self.option()

        self.action()

    def option(self):
        '遍历命令，并转化'
        for item in self.attr:
            if(item[0] in r'\/-'):
                sets = item[1:]
                self.argv[ sets.lower() ] = []
            else:
                self.argv[ sets.lower() ].append( item )
        self._synonym_option()

    def _synonym_option(self):
        keys = list(self.argv.keys())
        maps = {
        #   短名：长名
            '?' : 'help',
            'h' : 'help',
            't' : 'text',
            'i' : 'init',
            's' : 'setup',
            'r' : 'robot',
            'l' : 'list'
        }
        for name in keys:
            if(name in maps.keys()):
                self.argv[ maps[name] ] = self.argv[name]

    def action(self):
        handle(self.argv)

class handle:
    __all__ = ['_help','_init','_setup','_robot','_list' ]
    def __init__(self, argv):
        for act in argv.keys():
            if('_{}'.format(act) in self.__all__):
                method = getattr(self,'_{}'.format(act))
                method(argv[act])
                break

    def _help(self,para):
        print(help)

    def _init(self,para):
        cli_config.data = {u'robot':None}
        cli_config.save()

    def _setup(self,para):
        para += [None]
        robot = dingbot.Manage(para[0])
        robot.login(para[1],para[2])
        robot.remember()

    def _robot(self,para):
        if  (para[0].lower() == 'login' ):
            cli_config.data[u'robot'] += para[1:]
        elif(para[0].lower() == 'logout' ):
            for item in para[1:]:
                cli_config.data[u'robot'].remove(item)
        else:
            raise RuntimeError

    def _list(self,para):
        para += ['all']
        if( para[0] == 'all' ):
            print('\n'.join(bot_config.data.keys()))
        if( para[0] == 'using' ):
            print('\n'.join(cli_config.data[u'robot']))

c = cmd(sys.argv[1:])


import argparse
from collections import defaultdict

def get_gold_dict(filename):
  gold = defaultdict(list)
  with open(filename) as f:
    for line in f:
      line = line.strip()
      w1, w2 = line.split(" ")
      #w2 = w2.lower()
      gold[w1].append(w2)
  return gold

def get_gold_bins(filename):
  gold = [defaultdict(list) for i in range(5)]
  count = 0
  with open(filename) as f:
    for line in f:
      line = line.strip()
      w1, w2 = line.split(" ")
      #w2 = w2.lower()
      _gold = gold[count // 300]
      if w1 not in _gold: count += 1
      _gold[w1].append(w2)
  return gold

def get_D(filename):
  D = {}
  with open(filename) as f:
    for line in f:
      line = line.strip()
      w1, w2 = line.split("\t")
      if w2 == "EMPTY": continue
      D[w1] = w2.lower()
  return D

def get_DD(filename):
  D = {}
  with open(filename) as f:
    for line in f:
      line = line.strip()
      w1, w2 = line.split("\t")
      if w2 == "EMPTY": continue
      w1, w2 = w1.lower(), w2.lower()
      if w1 not in D: D[w1] = w2
  return D

def get_prec(D, DD, gold):
  count = 0
  right = 0
  unk = []
  for word in gold:
    if word not in D:
      if word not in DD:
        unk.append(gold[word][0])
        if word in gold[word]: right += 1
        #count -= 1
      else:
        if DD[word] in gold[word]: right += 1
    else:
      if D[word] in gold[word]: right += 1
    count += 1
  #print(unk)
  #print(len(unk))
  #print(count)
  return 1.0 * right / count

def main(args):
  gold = get_gold_dict(args.gold)
  D = get_D(args.D)
  DD = get_DD(args.D)
  print(get_prec(D, DD, gold))
  gb = get_gold_bins(args.gold)
  for g in gb:
    print(get_prec(D, DD, g))

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  res_dir = "/data00/home/chizewen/res/en2it"
  parser.add_argument("--D", default="./out/80Kgreedy.it")
  parser.add_argument("--gold",
    default="/Users/chizewen/repo/transmat/data/OPUS_en_it_europarl_test.txt")
  args = parser.parse_args()
  main(args)


import json
from tqdm import tqdm

dict = {}

lines = open('content.json').readlines()
for line in lines:
    dict[len(dict)] = json.loads(line)

lines = open('ad-content.json').readlines()
for line in lines:
    dict[len(dict)] = json.loads(line)

lines = open('kt-content.json').readlines()
for line in lines:
    dict[len(dict)] = json.loads(line)

list = []
for k, v in dict.items():
    if v['topic'][4:9]=='World':
        list.append(k)
    if v['topic'][6:11]=='World':
        list.append(k)
    if v['topic'][4:12]=='Regional':
        list.append(k)
    if v['topic'][6:14]=='Regional':
        list.append(k)
    if v['topic'][15:28]=='International':
        list.append(k)
    if v['topic'][0:3]=="Top":
        v['topic'] = v['topic'][4:]
    #if v['topic'].find('Titles') != -1:
    #    list.append(k)
    del v['url']

for k in list:
    dict.pop(k)

f = open('content2.json', mode='w')
for obj in dict.values():
    f.write(json.dumps(obj)+'\n')


import asyncio
import utils
from .utils import UtilsTask
from reqs.live_daily_job import (
    HeartBeatReq,
    RecvHeartGiftReq,
    OpenSilverBoxReq,
    RecvDailyBagReq,
    SignReq,
    WatchTvReq,
    SignFansGroupsReq,
    SendGiftReq,
    ExchangeSilverCoinReq,
)

class HeartBeatTask:
    @staticmethod
    def target(step):
        if step == 0:
            return HeartBeatTask.heart_beat
        return None

    @staticmethod
    async def heart_beat(user):
        json_rsp = await user.req_s(HeartBeatReq.pc_heartbeat, user)
        # print(json_rsp)
        user.infos(['心跳包(5分钟左右间隔)'])
        json_rsp = await user.req_s(HeartBeatReq.app_heartbeat, user)
        print(json_rsp)
        return (0, (280, 300), user.id),

class RecvHeartGiftTask:
    @staticmethod
    def target(step):
        if step == 0:
            return RecvHeartGiftTask.recv_heartgift
        return None

    @staticmethod
    async def recv_heartgift(user):
        json_rsp = await user.req_s(RecvHeartGiftReq.recv_heartgift, user)
        if json_rsp['code'] == 400:
            user.fall_in_jail()
            return (0, (0, 0), user.id),
        return (0, (280, 300), user.id),

class OpenSilverBoxTask:
    @staticmethod
    def target(step):
        if step == 0:
            return OpenSilverBoxTask.open_silver_box
        return None

    @staticmethod
    async def open_silver_box(user):
        while True:
            user.infos(["检查宝箱状态"])
            temp = await user.req_s(OpenSilverBoxReq.check_time, user)
            # print (temp['code'])    #宝箱领完返回的code为-10017
            if temp['code'] == -10017:
                user.infos(["今日宝箱领取完毕"])
                json_rsp = None
            else:
                time_start = temp['data']['time_start']
                time_end = temp['data']['time_end']
                json_rsp = await user.req_s(OpenSilverBoxReq.open_silver_box, user, time_start, time_end)
            if json_rsp is None or json_rsp['code'] == -10017 or json_rsp['code'] == -800:
                sleeptime = utils.seconds_until_tomorrow() + 300
                return (0, (sleeptime, sleeptime+30), user.id),
            elif not json_rsp['code']:
                user.infos(["打开了宝箱"])
            elif json_rsp['code'] == 400:
                user.infos(["宝箱开启中返回了小黑屋提示"])
                user.fall_in_jail()
                # 马上继续调用，由下一次的user去supend这个task
                return (0, (0, 0), user.id),
            else:
                user.infos(["继续等待宝箱冷却..."])
                sleeptime = (json_rsp['data'].get('surplus', 3)) * 60 + 5
                return (0, (sleeptime, sleeptime+30), user.id),

class RecvDailyBagTask:
    @staticmethod
    def target(step):
        if step == 0:
            return RecvDailyBagTask.recv_dailybag
        return None

    @staticmethod
    async def recv_dailybag(user):
        json_rsp = await user.req_s(RecvDailyBagReq.recv_dailybag, user)
        try:
            json_rsp['data']['bag_list']
        except:
            user.warn(f'recv_dailybag {json_rsp}')
        for i in json_rsp['data']['bag_list']:
            user.infos([f'获得-{i["bag_name"]}-成功'])
        sleeptime = 21600
        return (0, (sleeptime, sleeptime+30), user.id),

class SignTask:
    @staticmethod
    def target(step):
        if step == 0:
            return SignTask.sign
        return None

    @staticmethod
    async def sign(user):
        json_rsp = await user.req_s(SignReq.sign, user)
        user.infos([f'签到状态: {json_rsp["msg"]}'])
        if json_rsp['code'] == -500 and '已' in json_rsp['msg']:
            sleeptime = utils.seconds_until_tomorrow() + 300
        else:
            sleeptime = 350
        return (0, (sleeptime, sleeptime+30), user.id),

class WatchTvTask:
    @staticmethod
    def target(step):
        if step == 0:
            return WatchTvTask.watch_tv
        return None

    @staticmethod
    async def watch_tv(user):
        # -400 done/not yet
        json_rsp = await user.req_s(WatchTvReq.watch_tv, user)
        user.infos([f'双端观看直播:  {json_rsp["msg"]}'])
        if json_rsp['code'] == -400 and '已' in json_rsp['msg']:
            sleeptime = utils.seconds_until_tomorrow() + 300
        else:
            sleeptime = 350
        return (0, (sleeptime, sleeptime+30), user.id),

class SignFansGroupsTask:
    @staticmethod
    def target(step):
        if step == 0:
            return SignFansGroupsTask.sign_groups
        return None

    @staticmethod
    async def sign_groups(user):
        json_rsp = await user.req_s(SignFansGroupsReq.fetch_groups, user)
        groups = [(i['group_id'], i['owner_uid']) for i in json_rsp['data']['list']]
        if groups:
            tasks = []
            for group_id, owner_uid in groups:
                task = asyncio.ensure_future(SignFansGroupsTask.sign_group(user, group_id, owner_uid))
                tasks.append(task)
            await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
        sleeptime = 21600
        return (0, (sleeptime, sleeptime+30), user.id),

    @staticmethod
    async def sign_group(user, group_id, owner_uid):
        json_rsp = await user.req_s(SignFansGroupsReq.sign_group, user, group_id, owner_uid)
        if not json_rsp['code']:
            data = json_rsp['data']
            if data['status']:
                user.infos([f'应援团 {group_id} 已应援过'])
            else:
                user.infos([f'应援团 {group_id} 应援成功,获得 {data["add_num"]} 点亲密度'])
        else:
            user.infos([f'应援团 {group_id} 应援失败'])

class SendGiftTask:
    @staticmethod
    def target(step):
        if step == 0:
            return SendGiftTask.send_gift
        return None

    @staticmethod
    async def fetch_giftbags(user):
        gift_bags = await UtilsTask.fetch_giftbags(user)
        results = []
        for bag_id, gift_id, gift_num, _, _, left_time in gift_bags:
            results.append([gift_id, gift_num, bag_id, left_time])
        return results

    @staticmethod
    async def fetch_wearing_medal(user):
        json_rsp = await user.req_s(SendGiftReq.fetch_wearing_medal, user)
        if not json_rsp['code']:
            data = json_rsp['data']
            if data:
                room_id = data['roominfo']['room_id']
                remain_intimacy = int(data['day_limit']) - int(data['today_feed'])
                medal_name = data['medal_name']
                return [(room_id, remain_intimacy, medal_name), ]
            else:
                # print('暂无佩戴任何勋章')
                return []

    @staticmethod
    async def send_expiring_gift(user):
        if not user.task_ctrl['clean-expiring-gift']:
            return
        gift_bags = await SendGiftTask.fetch_giftbags(user)
        room_id = user.task_ctrl['clean-expiring-gift2room']
        time_set = user.task_ctrl['set-expiring-time']
        # filter 用过期来过滤
        expiring_giftbags = []
        for gift in gift_bags:
            left_time = gift[3]
            if left_time is not None and 0 < int(left_time) < time_set:
                expiring_giftbags.append(gift[:3])
        if expiring_giftbags:
            print('发现即将过期的礼物')
            if user.task_ctrl['clean_expiring_gift2all_medal']:
                print('正在清理过期礼物到用户勋章')
                medals = await UtilsTask.fetch_medals(user)
                expiring_giftbags = await SendGiftTask.fill_intimacy(user, expiring_giftbags, medals)

            print('正在清理过期礼物到指定房间')
            for gift_id, gift_num, bag_id in expiring_giftbags:
                await UtilsTask.send_gift(user, room_id, gift_num, bag_id, gift_id)
        else:
            print('未发现即将过期的礼物')

    @staticmethod
    async def send_medal_gift(user):
        medals = []
        if user.task_ctrl['send2wearing-medal']:
            medals = await SendGiftTask.fetch_wearing_medal(user)
            if not medals:
                print('暂未佩戴任何勋章')
        if user.task_ctrl['send2medal']:
            medals += await UtilsTask.fetch_medals(user, user.task_ctrl['send2medal'])
        # print('目前的勋章', medals)

        print('正在投递勋章')
        gift_bags = await SendGiftTask.fetch_giftbags(user)
        # print(temp)
        send_giftbags = []
        for gift in gift_bags:
            gift_id = int(gift[0])
            left_time = gift[3]
            # 过滤某些特定礼物以及永久礼物
            if (gift_id not in [4, 3, 9, 10]) and left_time is not None:
                send_giftbags.append(gift[:3])
        await SendGiftTask.fill_intimacy(user, send_giftbags, medals)

    @staticmethod
    async def fill_intimacy(user, gift_bags, medals):
        json_rsp = await user.req_s(SendGiftReq.fetch_gift_config, user)
        gift_price = {gift['id']: (gift['price'] / 100) for gift in json_rsp['data']}
        num_sent = 0
        for room_id, remain_intimacy, medal_name in medals:
            filled_intimacy = 0
            for gift in gift_bags:
                gift_id, gift_num, bag_id = gift
                if (gift_num * gift_price[gift_id] <= remain_intimacy):
                    num_sent = gift_num
                elif remain_intimacy >= gift_price[gift_id]:
                    num_sent = int(remain_intimacy / gift_price[gift_id])
                else:
                    continue
                gift[1] -= num_sent
                score = gift_price[gift_id] * num_sent
                await UtilsTask.send_gift(user, room_id, num_sent, bag_id, gift_id)
                filled_intimacy += score
                remain_intimacy -= score
            user.infos([f'对 {medal_name} 共送出亲密度为{filled_intimacy}的礼物'])
        # 过滤掉送光了的礼物包
        return [gift for gift in gift_bags if gift[1]]

    @staticmethod
    async def send_gift(user):
        await SendGiftTask.send_medal_gift(user)
        await SendGiftTask.send_expiring_gift(user)
        sleeptime = 21600
        return (0, (sleeptime, sleeptime+30), user.id),

class ExchangeSilverCoinTask:
    @staticmethod
    def target(step):
        if step == 0:
            return ExchangeSilverCoinTask.silver2coin
        return None

    @staticmethod
    async def silver2coin(user):
        if not user.task_ctrl['silver2coin']:
            sleeptime = 21600
            return (0, (sleeptime, sleeptime+30), user.id),
        json_rsp = await user.req_s(ExchangeSilverCoinReq.silver2coin_web, user)
        user.infos([f'{json_rsp["msg"]}'])
        if json_rsp['code'] == 403 and '最多' in json_rsp['msg']:
            finish_web = True
        else:
            finish_web = False
        if finish_web:
            sleeptime = utils.seconds_until_tomorrow() + 300
        else:
            sleeptime = 350

        return (0, (sleeptime, sleeptime+30), user.id),


#!/usr/bin/env python3
#! python3

import re
import time
import asyncio
from constants import *
from youtube_dl import YoutubeDL
from apricot.utils import parse_qs, urlparse

class YoutubeExtractor(object):

	def __init__(self, session):
		self.session = session
		self.ytdl	= YoutubeDL(YTDL_OPTS)

	def getVideoID(self, url):
		""" Extract the video id from a url, return video id as str. """
		idregx = re.compile(r'[\w-]{11}$')
		url = str(url)

		if idregx.match(url):
			return url # ID of video

		# extract info
		if '://' not in url:
			url = '//' + url
		parsedurl = urlparse(url)
		if parsedurl.netloc in ('youtube.com', 'www.youtube.com'):
			query = parse_qs(parsedurl.query)
			if 'v' in query and idregx.match(query['v'][0]):
				return query['v'][0]
		elif parsedurl.netloc in ('youtu.be', 'www.youtu.be'):
			vidid = parsedurl.path.split('/')[-1] if parsedurl.path else ''
			if idregx.match(vidid):
				return vidid

		# raise error if not found
		err = "Need 11 character video id or the URL of the video. Got %s"
		raise ValueError(err % url)

	def timeToSeconds(self, t):
		""" Convert time_dict to seconds """
		return t['seconds'] + (t['minutes'] * 60) + (t['hours'] * 60 * 60)

	def timeToText(self, _time):
		""" Convert time_dict to formatted text """
		secs   = self.timeToSeconds(_time)
		FORMAT = '%M:%S'
		if _time['hours'] > 0: FORMAT = '%H:' + FORMAT
		return time.strftime(FORMAT, time.gmtime(secs))

	async def getShorten(self, url):
		""" Use Google URL Shortener to shorten url """
		query   = GOOGLE_SHORT.format(key=GOOGLE_API_KEY)
		headers = {'content-type': 'application/json'}
		payload = {"longUrl": url}

		resp    = await self.session.post(query, headers=headers, json=payload)
		data    = await resp.json()
		return data['id']

	async def getThumbnail(self, url):
		""" Get the thumbnail url of a video """
		url   = self.getVideoID(url)
		thumb = MAX_THUMBNAIL.format(id=url)

		# see if maxres thumbnail is valid
		resp  = await self.session.head(thumb)

		# if not, fall back to default hq thumbnail
		if str(resp.status).startswith('4'):
			thumb = MIN_THUMBNAIL.format(id=url)

		return thumb

	async def getTitle(self, url):
		""" Get the title of a youtube url """
		link_copy   = str(YOUTUBE_LINK)
		oembed_copy = str(YOUTUBE_OEMBED)
		url  = self.getVideoID(url)
		url  = link_copy.format(id=url)
		url  = oembed_copy.format(url=url)

		resp = await self.session.get(url)
		data = await resp.json()

		return data['title']

	async def getAudioStream(self, url, future=None):
		""" Get the M4A audio stream url of a video """
		url  = self.getVideoID(url)
		info = self.ytdl.extract_info(url, download=False)

		# get audio url
		audio_info = [
			f for f in info['formats']
			if 'audio' in f['format'].lower()
		]
		audio_info = audio_info[0] # get first link

		# set future if any and return result
		if future is not None:
			try: future.set_result(audio_info['url'])
			except: pass
		return audio_info['url']

	async def getVideoTime(self, url):
		""" Get video duration in {"hours:int", "minutes":int, "seconds":int, "stream":bool} """
		gdata_copy = str(YOUTUBE_GDATA)
		url = self.getVideoID(url)
		url = gdata_copy.format(id=url, key=GOOGLE_API_KEY)

		# extract duration and create time_dict
		resp = await self.session.get(url)
		info = await resp.json()
		info = info['items'][0]['contentDetails']
		duration = str(info['duration'])
		duration = duration[2:].lower() # remove "PT"
		TIME = {'hours':0,'minutes':0,'seconds':0}

		# populate time_dict with values
		for unit in TIME:
			if unit[0] in duration:
				valid = duration.split(unit[0])[0]
				valid = re.sub('\D', '', valid)
				TIME[unit] = int(valid[-2:])
		if max([TIME[t] for t in TIME]) < 1:
			TIME['stream'] = True
		else:
			TIME['stream'] = False

		# return time_dict
		return TIME

	async def getSearchResults(self, search, results=1):
		""" return array of {'id':str, 'title':str} from youtube search """
		params = {
			"key": GOOGLE_API_KEY,
			"q": search,
			"maxResults": str(results),
			"part": "snippet"
		}

		# fetch json info
		resp = await self.session.get(YOUTUBE_SEARCH, params=params)
		data = await resp.json()

		# get results
		_results_ = []
		_found_   = 0
		for item in data['items']:
			video_id    = item['id']['videoId']
			video_title = item['snippet']['title']
			info = {'id': video_id, 'title': video_title}
			_found_ += 1
			if _found_ <= results:
				_results_.append(info)
			if _found_ == results:
				break

		return _results_

	async def getInfo(self, url, search=False):
		""" Get all info of video from id """

		# get first url from search if enabled
		if search:
			info = await self.getSearchResults(url, results=1)
			url = info[0]['id']
		url   = self.getVideoID(url)

		# spawn audio fetcher in a non blocking task
		# so that other operations can finish first
		def on_audio_complete(task):
			task.event.set()
		future     = asyncio.Future()
		audio_coro = lambda: self.getAudioStream(url, future)
		task       = self.session.loop.create_task(audio_coro())
		task.event = asyncio.Event()
		task.add_done_callback(on_audio_complete)

		# get info
		_thumb = await self.getThumbnail(url)
		_title = await self.getTitle(url)
		_time  = await self.getVideoTime(url)

		# wait for audio fetching to complete
		# and get result
		await task.event.wait()
		_audio = future.result()

		# create dict
		info = {}
		info['thumbnail'] = _thumb
		info['title']     = _title
		info['audio']     = _audio
		info['time']      = _time

		# return final built info
		return info