import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import collections
import time

def preprocess():

    path = 'D:/'
    file_name = 'data.csv'

    start = time.time()

    # load data
    data = pd.read_csv(path + file_name)

    # drop rows with negative 'Order_Qty' value
    data = data[data.Order_Qty >= 0]

    # extract 'Order_Qty' column to be used as labels
    y = data['Order_Qty'].values
    # replace all non-zero values with one
    y[y > 0] = 1
    # drop 'Order_Qty' column
    data.drop('Order_Qty', axis=1, inplace=True)
    # total number of valid data sets
    data_size = len(y)
    dict_order_qty = dict(collections.Counter(y))
    keys_order_qty = list(dict_order_qty.keys())

    # extract 'Country' column
    countries = data['Country'].values
    # compute frequency of each country
    dict_country = dict(collections.Counter(countries))
    keys_country = list(dict_country.keys())
    top_3_countries = collections.Counter(countries).most_common(3)
    # print(top_3_countries)
    for i in range(0, len(countries)):
        country = countries[i]
        if country != 'US' and country != 'SA' and country != 'QA':
            countries[i] = 'other'
    # one-hot encode 'Country'
    le_country = LabelEncoder()
    labels_country = le_country.fit_transform(countries)
    country_b = np.zeros((len(countries), 4), dtype=int)
    country_b[np.arange(len(countries), dtype=int), labels_country] = 1
    # print(country_b)

    # extract 'Coverage' column
    coverage = data['Coverage'].values
    dict_coverage = dict(collections.Counter(coverage))
    keys_coverage = list(dict_coverage.keys())
    # one-hot encode 'Coverage'
    le_coverage = LabelEncoder()
    labels_coverage = le_coverage.fit_transform(coverage)
    coverage_b = np.zeros((len(coverage), 2), dtype=int)
    coverage_b[np.arange(len(coverage), dtype=int), labels_coverage] = 1
    # print(coverage_b)

    # extract 'SKU' column
    skus = [str(i) for i in data['SKU'].values]
    # compute frequency of each SKU
    dict_sku = dict(collections.Counter(skus))
    keys_sku = list(dict_sku.keys())
    top_5_skus = collections.Counter(skus).most_common(5)
    # print(top_5_skus)
    for i in range(0, len(skus)):
        sku = skus[i]
        if sku != '10070735' and sku != '10019577' and sku != '10108817' and sku != '10106342' and sku != '10064539':
            skus[i] = 'other'
    # one-hot encode 'SKU'
    le_sku = LabelEncoder()
    labels_sku = le_sku.fit_transform(skus)
    sku_b = np.zeros((len(skus), 6), dtype=int)
    sku_b[np.arange(len(skus), dtype=int), labels_sku] = 1
    # print(sku_b)

    # extract 'SKU_Category' column
    sku_categories = [str(i) for i in data['SKU_Category'].values]
    # compute frequency of each SKU category
    dict_sku_category = dict(collections.Counter(sku_categories))
    keys_sku_category = list(dict_sku_category.keys())
    top_5_sku_categories = collections.Counter(sku_categories).most_common(5)
    # print(top_5_sku_categories)
    for i in range(0, len(sku_categories)):
        sku_category = sku_categories[i]
        if sku_category != '33346' and sku_category != '10322' and sku_category != '14345' and sku_category != '10321' and sku_category != '14382':
            sku_categories[i] = 'other'
    # one-hot encode 'SKU_Category'
    le_sku_category = LabelEncoder()
    labels_sku_category = le_sku_category.fit_transform(sku_categories)
    sku_category_b = np.zeros((len(sku_categories), 6), dtype=int)
    sku_category_b[np.arange(len(sku_categories), dtype=int), labels_sku_category] = 1
    # print(sku_category_b)

    # extract 'EB_Flag' column
    eb_flag = data['EB_Flag'].values
    # compute frequency of each EB_Flag
    dict_eb_flag = dict(collections.Counter(eb_flag))
    keys_eb_flag = list(dict_eb_flag.keys())
    # one-hot encode 'EB_Flag'
    le_eb_flag = LabelEncoder()
    labels_eb_flag = le_eb_flag.fit_transform(eb_flag)
    eb_flag_b = np.zeros((len(eb_flag), 2), dtype=int)
    eb_flag_b[np.arange(len(eb_flag), dtype=int), labels_eb_flag] = 1
    # print(eb_flag_b)

    # extract 'RFQ_TYPE' column
    rfq_type = [str(i) for i in data['RFQ_TYPE'].values]
    # compute frequency of each RFQ_TYPE
    dict_rfq_type = dict(collections.Counter(rfq_type))
    keys_rfq_type = list(dict_rfq_type.keys())
    # One-hot encode RFQ_Type
    le_rfq_type = LabelEncoder()
    labels_rfq_type = le_rfq_type.fit_transform(rfq_type)
    rfq_type_b = np.zeros((len(rfq_type), 9), dtype=int)
    rfq_type_b[np.arange(len(rfq_type), dtype=int), labels_rfq_type] = 1

    # extract 'List_Price' column
    list_price = data['List_Price'].values
    # scale data to the [0, 1] range
    min_max_scaler = MinMaxScaler()
    list_price_n = np.array(min_max_scaler.fit_transform(np.array(list_price).reshape(-1, 1)))

    # extract 'RFQ_Price' column
    rfq_price = data['RFQ_Price'].values
    # scale data to the [0, 1] range
    min_max_scaler = MinMaxScaler()
    rfq_price_n = np.array(min_max_scaler.fit_transform(np.array(rfq_price).reshape(-1, 1)))
    # print(rfq_price_n)

    # extract 'List_Price*RFQ_Qty' column
    list_price_x_rfq_qty = data['List_Price*RFQ_Qty'].values
    # scale data to the [0, 1] range
    min_max_scaler = MinMaxScaler()
    list_price_x_rfq_qty_n = np.array(min_max_scaler.fit_transform(np.array(list_price_x_rfq_qty).reshape(-1, 1)))
    # print(list_price_x_rfq_qty_n)

    # extract 'RFQ_Price*Order_Qty' column
    rfq_price_x_order_qty = data['RFQ_Price*Order_Qty'].values
    # scale data to the [0, 1] range
    min_max_scaler = MinMaxScaler()
    rfq_price_x_order_qty_n = np.array(min_max_scaler.fit_transform(np.array(rfq_price_x_order_qty).reshape(-1, 1)))
    # print(rfq_price_x_order_qty_n)

    country_b = np.array(country_b)
    coverage_b = np.array(coverage_b)
    sku_b = np.array(sku_b)
    sku_category_b = np.array(sku_category_b)
    eb_flag_b = np.array(eb_flag_b)
    rfq_type_b = np.array(rfq_type_b)
    list_price_n = list_price_n
    rfq_price_n = rfq_price_n
    list_price_x_rfq_qty_n = list_price_x_rfq_qty_n
    rfq_price_x_order_qty_n = rfq_price_x_order_qty_n

    # concatenate all encoded and normalized arrays
    X = np.concatenate((country_b, coverage_b), axis=1)
    X = np.concatenate((X, sku_b), axis=1)
    X = np.concatenate((X, sku_category_b), axis=1)
    X = np.concatenate((X, eb_flag_b), axis=1)
    X = np.concatenate((X, rfq_type_b), axis=1)
    X = np.concatenate((X, list_price_n), axis=1)
    X = np.concatenate((X, rfq_price_n), axis=1)
    X = np.concatenate((X, list_price_x_rfq_qty_n), axis=1)
    X = np.concatenate((X, rfq_price_x_order_qty_n), axis=1)

    # split into train&cv and test sets
    test_size = 0.3
    X_train_and_cv, X_test, y_train_and_cv, y_test = train_test_split(X, y, test_size=test_size)

    # split into train and cv sets
    cv_size = 0.2
    X_train, X_cv, y_train, y_cv = train_test_split(X_train_and_cv,
                                                    y_train_and_cv,
                                                    test_size=cv_size)

    # # compute percentage of each key
    # for i in range(0, len(keys_order_qty)):
    #     key = keys_order_qty[i]
    #     dict_order_qty[key] = dict_order_qty.get(key) / data_size
    # for i in range(0, len(keys_country)):
    #     key = keys_country[i]
    #     dict_country[key] = dict_country.get(key) / data_size
    # for i in range(0, len(keys_sku)):
    #     key = keys_sku[i]
    #     dict_sku[key] = dict_sku.get(key) / data_size
    # for i in range(0, len(keys_sku_category)):
    #     key = keys_sku_category[i]
    #     dict_sku_category[key] = dict_sku_category.get(key) / data_size
    # for i in range(0, len(keys_eb_flag)):
    #     key = keys_eb_flag[i]
    #     dict_eb_flag[key] = dict_eb_flag.get(key) / data_size
    # for i in range(0, len(keys_rfq_type)):
    #     key = keys_rfq_type[i]
    #     dict_rfq_type[key] = dict_rfq_type.get(key) / data_size

    # # write result into a text file
    # file = open('result.txt', 'w')
    # file.write('Number of valid data sets: {}'.format(data_size))
    # file.write("\n\nCountry:")
    # file.write('\nKeys: {}'.format(keys_country))
    # file.write('\nNo. of keys: {}'.format(len(keys_country)))
    # file.write('\nFrequency of each category: {}'.format(dict_country))
    # file.write("\n\nCoverage:")
    # file.write('\nKeys: {}'.format(keys_coverage))
    # file.write('\nNo. of keys: {}'.format(len(keys_coverage)))
    # file.write('\nFrequency of each category: {}'.format(dict_coverage))
    # file.write("\n\nSKU:")
    # file.write('\nKeys: {}'.format(keys_sku))
    # file.write('\nNo. of keys: {}'.format(len(keys_sku)))
    # file.write('\nFrequency of each category: {}'.format(dict_sku))
    # file.write("\n\nSKU_Category:")
    # file.write('\nKeys: {}'.format(keys_sku_category))
    # file.write('\nNo. of keys: {}'.format(len(keys_sku_category)))
    # file.write('\nFrequency of each category: {}'.format(dict_sku_category))
    # file.write("\n\nEB_Flag:")
    # file.write('\nKeys: {}'.format(keys_eb_flag))
    # file.write('\nNo. of keys: {}'.format(len(keys_eb_flag)))
    # file.write('\nFrequency of each category: {}'.format(dict_eb_flag))
    # file.write("\n\nRFQ_Type:")
    # file.write('\nKeys: {}'.format(keys_rfq_type))
    # file.write('\nNo. of keys: {}'.format(len(keys_rfq_type)))
    # file.write('\nFrequency of each category: {}'.format(dict_rfq_type))
    # file.write("\n\nOrder_Qty:")
    # file.write('\nKeys: {}'.format(keys_order_qty))
    # file.write('\nNo. of keys: {}'.format(len(keys_order_qty)))
    # file.write('\nFrequency of each category: {}'.format(dict_order_qty))
    # file.close()

    end = time.time()

    print('\nPREPROCESSING COMPLETE\nTime elapsed: {:.2f} {}'.format((end - start), 'seconds'))

    return X_train, X_cv, X_test, y_train, y_cv, y_test


#!/usr/bin/env python3
"""API module"""
import requests
from datetime import datetime
import sys

if __name__ == '__main__':
    url = sys.argv[1]

    response = requests.get(url)

    if response.status_code == 404:
        print('Not found')
    elif response.status_code == 403:
        limit = response.headers['X-Ratelimit-Reset']
        now = datetime.now().timestamp()
        difference = (int(limit) - int(now)) / 60
        print('Reset in {} min'.format(int(difference)))
    elif response.status_code == 200:
        print(response.json()['location'])


def isvalid(text):
    pairsopen = {'(':')', '[':']','{':'}'}
    pairsclose = {')':'(', ']':'[','}':'{'}
    match = {'(':0, '[':0,'{':0}
    for i in text:
        if i in pairsopen.keys():
            match[i] += 1
        elif i in pairsclose.keys():
            if match[pairsclose[i]] > 0:
                match[pairsclose[i]] -= 1
            else:
                return False #parantez açılmadan kapanmış
        else:
            return False #geçersiz karakter
    return True if sum(match.values()) == 0 else False

def checkform(text):
    pairs = {')':'(', ']':'[','}':'{'}
    que = []
    for i in text:
        if i in pairs.values():
            que.append(i)
        elif i in pairs.keys():
            if que[-1] == pairs[i]:
                que.pop(-1)
            else:
                return False #parantez açılmadan kapanmış
        else:
            return False #geçersiz karakter
    return False if que else True #que de eleman varsa False

def is_wellformed(data):
    lst = [i for i in ["()","{}","[]"] if i in data]
    while lst:
        for i in lst:
            data = data.replace(i,"")
        lst = [i for i in ["()","{}","[]"] if i in data]
    return not bool(data)

def checkform1(text):
    pairs = {')':'(', ']':'[','}':'{'}
    que = []
    for i in text:
        if i in pairs.values():
            que.append(i)
        elif (i in pairs.keys()) and que and (que[-1] == pairs[i]):
            que.pop(-1)
        else:
            return False
    return False if que else True

def checkform3(text):
    lst = [i for i in ["()","{}","[]"] if i in text]
    while lst:
        for i in lst:
            text = text.replace(i,"")
        lst = [i for i in ["()","{}","[]"] if i in text]
    return not bool(text)

from collections import deque
def checkform4(s):
    pairs = {')':'(', ']':'[','}':'{'}
    que = deque()
    for i in s:
        if (i in pairs.keys()) and que and (que[-1] == pairs[i]):
            que.pop()
        else:
            que.append(i)
    return len(que) == 0

s = ["([)]", "([])", "((()[]{()})())"]

for i in s:
    print(i, is_wellformed(i))
    print(i, checkform4(i))


"""
    The MIT License (MIT)

    Copyright (c) 2015 Erik Soma

    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.
"""

# python standard libary
import ctypes

c_bool = ctypes.c_bool
c_char = ctypes.c_char
c_wchar = ctypes.c_wchar
c_byte = ctypes.c_byte
c_ubyte = ctypes.c_ubyte
c_short = ctypes.c_short
c_ushort = ctypes.c_ushort
c_int = ctypes.c_int
c_uint = ctypes.c_uint
c_long = ctypes.c_long
c_ulong = ctypes.c_ulong
c_longlong = ctypes.c_longlong
c_ulonglong = ctypes.c_ulonglong
c_float = ctypes.c_float
c_double = ctypes.c_double
c_longdouble = ctypes.c_longdouble
c_char_p = ctypes.c_char_p
c_wchar_p = ctypes.c_wchar_p
c_void_p = ctypes.c_void_p
c_size_t = ctypes.c_size_t
py_object = ctypes.py_object

addressof = ctypes.addressof
alignment = ctypes.alignment
byref = ctypes.byref
cast = ctypes.cast
CFUNCTYPE = ctypes.CFUNCTYPE
create_string_buffer = ctypes.create_string_buffer
create_unicode_buffer = ctypes.create_unicode_buffer
memmove = ctypes.memmove
memset = ctypes.memset
POINTER = ctypes.POINTER
pointer = ctypes.pointer
resize = ctypes.resize
sizeof = ctypes.sizeof
string_at = ctypes.string_at
wstring_at = ctypes.wstring_at

del ctypes


#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
SeisHub database client for ObsPy.

The obspy.seishub package contains a client for the seismological database
SeisHub (http://www.seishub.org).

ObsPy is an open-source project dedicated to provide a Python framework for
processing seismological data. It provides parsers for common file formats and
seismological signal processing routines which allow the manipulation of
seismological time series (see Beyreuther et al. 2010, Megies et al. 2011).
The goal of the ObsPy project is to facilitate rapid application development
for seismology.

For more information visit http://www.obspy.org.

:copyright:
    The ObsPy Development Team (devs@obspy.org)
:license:
    GNU Lesser General Public License, Version 3
    (http://www.gnu.org/copyleft/lesser.html)
"""

from setuptools import find_packages, setup
import os
import shutil
import sys

LOCAL_PATH = os.path.abspath(os.path.dirname(__file__))
DOCSTRING = __doc__.split("\n")

# package specific settings
NAME = 'obspy.seishub'
AUTHOR = 'The ObsPy Development Team'
AUTHOR_EMAIL = 'devs@obspy.org'
LICENSE = 'GNU Lesser General Public License, Version 3 (LGPLv3)'
KEYWORDS = ['ObsPy', 'seismology', 'SeisHub']
INSTALL_REQUIRES = ['obspy.core', 'obspy.mseed', 'lxml', 'obspy.xseed']
ENTRY_POINTS = {}

def convert2to3():
    """
    Convert source to Python 3.x syntax using lib2to3.
    """
    # create a new 2to3 directory for converted source files
    dst_path = os.path.join(LOCAL_PATH, '2to3')
    shutil.rmtree(dst_path, ignore_errors=True)

    # copy original tree into 2to3 folder ignoring some unneeded files
    def ignored_files(_adir, filenames):
        return ['.svn', '2to3', 'debian', 'build', 'dist'] + \
               [fn for fn in filenames if fn.startswith('distribute')] + \
               [fn for fn in filenames if fn.endswith('.egg-info')]
    shutil.copytree(LOCAL_PATH, dst_path, ignore=ignored_files)
    os.chdir(dst_path)
    sys.path.insert(0, dst_path)
    # run lib2to3 script on duplicated source
    from lib2to3.main import main
    print("Converting to Python3 via lib2to3...")
    main("lib2to3.fixes", ["-w", "-n", "--no-diffs", "obspy"])

def getVersion():
    # fetch version
    file = os.path.join(LOCAL_PATH, 'obspy', NAME.split('.')[1], 'VERSION.txt')
    return open(file).read()

def setupPackage():
    # use lib2to3 for Python 3.x
    if sys.version_info[0] == 3:
        convert2to3()
    # setup package
    setup(
        name=NAME,
        version=getVersion(),
        description=DOCSTRING[1],
        long_description="\n".join(DOCSTRING[3:]),
        url="http://www.obspy.org",
        author=AUTHOR,
        author_email=AUTHOR_EMAIL,
        license=LICENSE,
        platforms='OS Independent',
        classifiers=[
            'Development Status :: 4 - Beta',
            'Environment :: Console',
            'Intended Audience :: Science/Research',
            'Intended Audience :: Developers',
            'License :: OSI Approved :: GNU Library or ' + \
                'Lesser General Public License (LGPL)',
            'Operating System :: OS Independent',
            'Programming Language :: Python',
            'Topic :: Scientific/Engineering',
            'Topic :: Scientific/Engineering :: Physics'],
        keywords=KEYWORDS,
        packages=find_packages(exclude=['distribute_setup']),
        namespace_packages=['obspy'],
        zip_safe=False,
        install_requires=INSTALL_REQUIRES,
        download_url="https://svn.obspy.org/trunk/%s#egg=%s-dev" % (NAME,
                                                                    NAME),
        include_package_data=True,
        test_suite="%s.tests.suite" % (NAME),
        entry_points=ENTRY_POINTS,
    )
    # cleanup after using lib2to3 for Python 3.x
    if sys.version_info[0] == 3:
        os.chdir(LOCAL_PATH)

if __name__ == '__main__':
    setupPackage()


import os, sys, time
import numpy as np
import magent
from magent.model import BaseModel
from magent.builtin.tf_model import DeepQNetwork
from keras import backend as K
from keras.optimizers import Adam
import tensorflow as tf
import random
from ReplayBuffer_v2 import ReplayBuffer
from keras.layers import Dense, Dropout, Conv2D, Input, Lambda, Flatten, TimeDistributed, merge
from keras.layers import Add, Reshape, MaxPooling2D, Concatenate, Embedding, RepeatVector
from keras.models import Model
from keras.layers.core import Activation
from keras.utils import np_utils,to_categorical
from keras.engine.topology import Layer
from keras.callbacks import TensorBoard
from magent.builtin.tf_model import DeepQNetwork

np.random.seed(16)

def Adjacency(state):
    adj = []
    dis = []
    for j in range(20):
        dis.append([state[j][-2],state[j][-1],j])
    for j in range(20):
        f = []
        for r in range(len(dis)):
            f.append([(dis[r][0]-dis[j][0])**2+(dis[r][1]-dis[j][1])**2,r])
        f.sort(key=lambda x:x[0])
        y = []
        for r in range(4):
            y.append(f[r][1])
        y = to_categorical(y,num_classes=20)
        adj.append(y)
    return adj

def observation(state1,state2):
    state = []
    for j in range(20):
        state.append(np.hstack(((state1[j][0:11,0:11,1]-state1[j][0:11,0:11,5]).flatten(),state2[j][-1:-3:-1])))
    return state

def MLP():
    In_0 = Input(shape=[123])

    h = Dense(512, activation='relu',kernel_initializer='random_normal')(In_0)
    h = Dense(128, activation='relu',kernel_initializer='random_normal')(h)

    h = Reshape((1,128))(h)

    model = Model(input=In_0,output=h)
    return model

def MultiHeadsAttModel(l=2, d=128, dv=16, dout=128, nv = 8 ):

    v1 = Input(shape = (l, d))
    q1 = Input(shape = (l, d))
    k1 = Input(shape = (l, d))
    ve = Input(shape = (1, l))

    v2 = Dense(dv*nv, activation = "relu",kernel_initializer='random_normal')(v1)
    q2 = Dense(dv*nv, activation = "relu",kernel_initializer='random_normal')(q1)
    k2 = Dense(dv*nv, activation = "relu",kernel_initializer='random_normal')(k1)

    v = Reshape((l, nv, dv))(v2)
    q = Reshape((l, nv, dv))(q2)
    k = Reshape((l, nv, dv))(k2)
    v = Lambda(lambda x: K.permute_dimensions(x, (0,2,1,3)))(v)
    k = Lambda(lambda x: K.permute_dimensions(x, (0,2,1,3)))(k)
    q = Lambda(lambda x: K.permute_dimensions(x, (0,2,1,3)))(q)

    att = Lambda(lambda x: K.batch_dot(x[0],x[1] ,axes=[3,3]) / np.sqrt(dv))([q,k])# l, nv, nv
    att = Lambda(lambda x: K.softmax(x))(att)
    out = Lambda(lambda x: K.batch_dot(x[0], x[1],axes=[3,2]))([att, v])
    out = Lambda(lambda x: K.permute_dimensions(x, (0,2,1,3)))(out)

    out = Reshape((l, dv*nv))(out)

    T = Lambda(lambda x: K.batch_dot(x[0],x[1]))([ve,out])

    out = Dense(dout, activation = "relu",kernel_initializer='random_normal')(T)
    model = Model(inputs=[q1,k1,v1,ve], outputs=out)
    return model

def Q_Net(action_dim):
    I1 = Input(shape = (1, 128))
    I2 = Input(shape = (1, 128))
    I3 = Input(shape = (1, 128))

    h1 = Flatten()(I1)
    h2 = Flatten()(I2)
    h3 = Flatten()(I3)

    h = merge([h1,h2,h3],mode='concat')
    V = Dense(action_dim,kernel_initializer='random_normal')(h)

    model = Model(input=[I1,I2,I3],output=V)
    return model

path="data/battle_model"
map_size=100
capacity = 200000
batch_size = 256
totalTime = 0
TAU = 0.01
LRA = 0.0001
param = None
alpha = 0.6
GAMMA = 0.96
n_episode = 100000
max_steps = 300
episode_before_train = 200
n_agent=20
magent.utility.init_logger("battle")
env = magent.GridWorld("battle", map_size=30)
env.set_render_dir("build/render")
handles = env.get_handles()
sess = tf.Session()
K.set_session(sess)
n = len(handles)
n_actions=env.get_action_space(handles[0])[0]
i_episode=0
buff=ReplayBuffer(capacity)
l=40

print(env.get_action_space(handles[0])[0])
print(env.get_action_space(handles[1])[0])
#f = open('log.txt','w')

######build the model#########
cnn = MLP()
m1 = MultiHeadsAttModel(l=4)
m2 = MultiHeadsAttModel(l=4)
q_net = Q_Net(action_dim = 9)
vec = np.zeros((1,4))
vec[0][0] = 1

In= []
for j in range(n_agent):
    In.append(Input(shape=[123]))
    In.append(Input(shape=(4,20)))
In.append(Input(shape=(1,4)))
feature = []
for j in range(n_agent):
    feature.append(cnn(In[j*2]))

feature_ = merge(feature,mode='concat',concat_axis=1)

relation1 = []
for j in range(n_agent):
    T = Lambda(lambda x: K.batch_dot(x[0],x[1]))([In[j*2+1],feature_])
    relation1.append(m1([T,T,T,In[40]]))

relation1_ = merge(relation1,mode='concat',concat_axis=1)

relation2 = []
for j in range(n_agent):
    T = Lambda(lambda x: K.batch_dot(x[0],x[1]))([In[j*2+1],relation1_])
    relation2.append(m2([T,T,T,In[40]]))

V = []
for j in range(n_agent):
    V.append(q_net([feature[j],relation1[j],relation2[j]]))

model = Model(input=In,output=V)
model.compile(optimizer=Adam(lr = 0.00003), loss='mse')
model.summary()

######build the target model#########
cnn_t = MLP()
m1_t = MultiHeadsAttModel(l=4)
m2_t = MultiHeadsAttModel(l=4)
q_net_t = Q_Net(action_dim = 9)
In_t= []
for j in range(n_agent):
    In_t.append(Input(shape=[123]))
    In_t.append(Input(shape=(4,20)))
In_t.append(Input(shape=(1,4)))

feature_t = []
for j in range(n_agent):
    feature_t.append(cnn_t(In_t[j*2]))

feature_t_ = merge(feature_t,mode='concat',concat_axis=1)

relation1_t = []
for j in range(n_agent):
    T = Lambda(lambda x: K.batch_dot(x[0],x[1]))([In_t[j*2+1],feature_t_])
    relation1_t.append(m1_t([T,T,T,In_t[40]]))

relation1_t_ = merge(relation1_t,mode='concat',concat_axis=1)

relation2_t = []
for j in range(n_agent):
    T = Lambda(lambda x: K.batch_dot(x[0],x[1]))([In_t[j*2+1],relation1_t_])
    relation2_t.append(m2_t([T,T,T,In_t[40]]))

V_t = []
for j in range(n_agent):
    V_t.append(q_net_t([feature_t[j],relation1_t[j],relation2_t[j]]))

model_t = Model(input=In_t,output=V_t)

tf_model = DeepQNetwork(env, handles[1], 'trusty-battle-game-l', use_conv=True)
tf_model.load("data/battle_model", 0, 'trusty-battle-game-l')
###########playing#############
while i_episode<n_episode:
    alpha*=0.996
    if alpha<0.01:
        alpha=0.01
    print(i_episode)
    i_episode=i_episode+1
    env.reset()
    #env.add_walls(method="random", n=map_size * map_size * 0.03)
    env.add_agents(handles[0], method="random", n=20)
    env.add_agents(handles[1], method="random", n=12)
    step_ct = 0
    done = False
    n = len(handles)
    obs  = [[] for _ in range(n)]
    ids  = [[] for _ in range(n)]
    action = [[] for _ in range(n)]
    nums = [env.get_num(handle) for handle in handles]
    steps = 0
    score = 0
    loss = 0
    dead = [0,0]

    while steps<max_steps:
        steps+=1
        i=0
        obs[i] = env.get_observation(handles[i])
        adj = Adjacency(obs[i][1])
        flat_ob = observation(obs[i][0],obs[i][1])
        ob=[]
        for j in range(n_agent):
            ob.append(np.asarray([flat_ob[j]]))
            ob.append(np.asarray([adj[j]]))
        ob.append(np.asarray([vec]))
        acts = model.predict(ob)
        action[i]=np.zeros(n_agent,dtype = np.int32)
        for j in range(n_agent):
            if np.random.rand()<alpha:
                action[i][j]=random.randrange(n_actions)
            else:
                action[i][j]=np.argmax(acts[j])
        env.set_action(handles[i], action[i])

        obs[1] = env.get_observation(handles[1])
        ids[1] = env.get_agent_id(handles[1])
        acts = tf_model.infer_action(obs[1], ids[1], 'e_greedy')
        env.set_action(handles[1], acts)
        done = env.step()

        next_obs = env.get_observation(handles[0])
        flat_next_obs = observation(next_obs[0],next_obs[1])
        rewards = env.get_reward(handles[0])
        score += sum(rewards)
        if steps%3 ==0:
            buff.add(flat_ob, action[0], flat_next_obs, rewards, done, adj)

        if (i_episode-1) % 10 ==0:
            env.render()
        if max_steps == steps:
            print(dead[0],end='\t')
            print(dead[1],end='\t')
            print(score/300,end='\t')
            #f.write(str(dead[i])+'\t'+str(score[i]/300)+'\t')
            #f.write(str(loss/100)+'\n')
            print(loss/100,end='\n')
        env.clear_dead()

        ############add to n_agent##############
        idd = n_agent - len(env.get_agent_id(handles[0]))
        if idd>0:
            env.add_agents(handles[0], method="random", n=idd)
            dead[0]+=idd
        idd = 12 - len(env.get_agent_id(handles[1]))
        if idd>0:
            env.add_agents(handles[1], method="random", n=idd)
            dead[1]+=idd

        if i_episode < episode_before_train:
            continue
        if steps%3 != 0:
            continue
        #############training###########
        batch = buff.getBatch(128)
        states,actions,rewards,new_states,dones,adj=[],[],[],[],[],[]
        for i_ in  range(n_agent*2+1):
            states.append([])
            new_states.append([])
        for e in batch:
            for j in range(n_agent):
                states[j*2].append(e[0][j])
                states[j*2+1].append(e[5][j])
                new_states[j*2].append(e[2][j])
                new_states[j*2+1].append(e[5][j])
            states[40].append(vec)
            new_states[40].append(vec)
            actions.append(e[1])
            rewards.append(e[3])
            dones.append(e[4])

        actions = np.asarray(actions)
        rewards = np.asarray(rewards)
        dones = np.asarray(dones)

        for i_ in  range(n_agent*2+1):
            states[i_]=np.asarray(states[i_])
            new_states[i_]=np.asarray(new_states[i_])

        q_values = model.predict(states)
        target_q_values = model_t.predict(new_states)

        for k in range(len(batch)):
            if dones[k]:
                for j in range(n_agent):
                    q_values[j][k][actions[k][j]] = rewards[k][j]
            else:
                for j in range(n_agent):
                    q_values[j][k][actions[k][j]] =rewards[k][j] + GAMMA*np.max(target_q_values[j][k])

        history=model.fit(states, q_values,epochs=1,batch_size=128,verbose=0)
        his=0
        for (k,v) in history.history.items():
            his+=v[0]
        loss+=(his/20)
        #########train target model#########
        weights = cnn.get_weights()
        target_weights = cnn_t.get_weights()
        for w in range(len(weights)):
            target_weights[w] = TAU * weights[w] + (1 - TAU)* target_weights[w]
        cnn_t.set_weights(target_weights)

        weights = q_net.get_weights()
        target_weights = q_net_t.get_weights()
        for w in range(len(weights)):
            target_weights[w] = TAU * weights[w] + (1 - TAU)* target_weights[w]
        q_net_t.set_weights(target_weights)

        weights = m1.get_weights()
        target_weights = m1_t.get_weights()
        for w in range(len(weights)):
            target_weights[w] = TAU * weights[w] + (1 - TAU)* target_weights[w]
        m1_t.set_weights(target_weights)

        weights = m2.get_weights()
        target_weights = m2_t.get_weights()
        for w in range(len(weights)):
            target_weights[w] = TAU * weights[w] + (1 - TAU)* target_weights[w]
        m2_t.set_weights(target_weights)

        #######save model###############
    model.save('gdn.h5')


# -*- coding: utf-8 -*-
import json

import scrapy

from ..items import DouyuItem

class DouyuSpider(scrapy.Spider):
    name = 'douyu'
    allowed_domains = ['douyucdn.cn']
    baseurl = 'http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset='
    offset = 0

    start_urls = [baseurl + str(offset)]

    def parse(self, response):
        # r_list存放所有主播信息,每个主播为一个字典
        r_list = json.loads(response.text)['data']
        if len(r_list) == 0:
            return

        for r in r_list:
            item = DouyuItem()
            item['link'] = r['vertical_src']
            item['name'] = r['nickname']
            item['house'] = r['room_id']
            item['city'] = r['anchor_city']

            yield item

        self.offset += 20
        yield scrapy.Request(self.baseurl + str(self.offset), callback=self.parse, dont_filter=True)


# Copyright 2021 Hang-Chi Shen. 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 numpy as np
import tensorflow as tf
from scipy import fft
from .block2channel import Block2Channel2d
from .block2channel import Block2Channel3d
from .block2channel import block2channel_2d
from .block2channel import block2channel_3d

def channel2fft(np_array, block_shape, check_shape=True):
    """
    Convert 2d or 3d numpy array to 3d, then do fft op at last dimension.
    :param np_array: 2d or 3d array
    :param block_shape: (block_h, block_w) example (2, 2),
    block shape should <= input tensor shape
    :param check_shape: check shape while run block2channel_2d(...) or block2channel_3d(...)
    :return: 3d numpy array, the same like output of block2channel_2d(...) or block2channel_3d(...)
    """
    if len(np_array.shape) == 2:
        out = block2channel_2d(np_array, block_shape, False, check_shape)

    elif len(np_array.shape) == 3:
        out = block2channel_3d(np_array, block_shape, False, check_shape)

    else:
        print("shape {} not support, recommend [h, w] or [h, w, channel]".format(np_array.shape))
        raise NotImplementedError
    # todo: fix bugs here
    return fft.rfft(out.astype(np.float32))

class RFFTLayer2d(tf.keras.layers.Layer):
    """
    Convert tf tensor with batch(like [batch, h, w]) to [h//block_H, w//block_w, block_h*block_w]
    then do DCT op at last dimension.
    :param block_shape: (block_h, block_w) example (2, 2),
    block shape should <= input tensor shape
    :param check_shape: check shape while run block2channel_2d(...)
    :return: [batch, h//block_H, w//block_w, block_h*block_w]
    """

    def __init__(self, block_shape, groups=None, dct_type=2, check_shape=True, **kwargs):
        super(RFFTLayer2d, self).__init__(**kwargs)
        self.block_shape = block_shape
        self.groups = groups
        self.dct_type = dct_type
        self.check_shape = check_shape
        self.block2Channel2d = None

    def build(self, input_shape):
        self.block2Channel2d = Block2Channel2d(self.block_shape, False, self.check_shape)

    def call(self, inputs, **kwargs):
        # [batch, h, w] ==>> [batch, h//block_H, w//block_w, block_h*block_w]
        out = self.block2Channel2d(inputs)
        if self.groups:
            print("NotImplemented!")
            raise NotImplemented
        else:
            return tf.cast(tf.signal.rfft(tf.cast(out, dtype=tf.float32)), dtype=tf.float32)

class RFFTLayer3d(tf.keras.layers.Layer):
    """
    Convert tf tensor with batch [batch, h, w, channel] to [batch, h//block_H, w//block_w, channel*block_h*block_w],
    then do rfft op at last dimension.
    :param block_shape: (block_h, block_w) example (2, 2),
    block shape should <= input tensor shape
    :param dct_type: default 2, example tf.signal.dct(tensor, type=dct_type)
    :param check_shape: check shape while run block2channel_3d(...)
    :return: [batch, h//block_H, w//block_w, channel*block_h*block_w]
    """

    def __init__(self, block_shape, groups=None, data_type=tf.float32, check_shape=True, **kwargs):
        super(RFFTLayer3d, self).__init__(**kwargs)
        self.block_shape = block_shape
        self.groups = groups
        self.data_type = data_type
        self.check_shape = check_shape
        self.block2Channel3d = None

    def build(self, input_shape):
        self.block2Channel3d = Block2Channel3d(self.block_shape, False, self.check_shape)

    def call(self, inputs, **kwargs):
        # [batch, h, w, channel] ==>> [batch, h//block_H, w//block_w, channel*block_h*block_w]
        out = self.block2Channel3d(inputs)
        if self.groups:
            print("NotImplemented!")
            raise NotImplemented

        else:
            return tf.cast(tf.signal.rfft(tf.cast(out, dtype=tf.float32)), dtype=self.data_type)


#  Hint:  You may not need all of these.  Remove the unused functions.
from hashtables import (HashTable)

def get_indices_of_item_weights(weights, length, limit):
    ht = HashTable(16)
    l = 0
    r = 1
    if length != 1:
        while l != length:
            while r != length:
                if weights[l] + weights[r] == limit:
                    if l > r:
                        return [l, r]
                    else:
                        return [r, l]
                else:
                    r += 1
            l += 1
            r = l + 1
    return None

def print_answer(answer):
    if answer is not None:
        print(str(answer[0] + " " + answer[1]))
    else:
        print("None")


import card
import player
import supply

# runs the game
class Game:
    def __init__(self, playersUI, actionCards, victoryCards, treasureCards, testing = False):
        players = []
        for playerUI in playersUI:
            players.append(player.Player(playerUI, testing))
        sup = supply.Supply(actionCards, victoryCards, treasureCards)
        self.Round = Round(players, sup)

    def playGame(self):
        self.setup()
        currentPlayer = 0
        while(not self.Round.isEnd()):
            print(currentPlayer)
            self.Round.players[currentPlayer].playTurn(self.Round)
            currentPlayer = (currentPlayer + 1) % len(self.Round.players)

    def setup(self):
        for player in self.Round.players:
            player.setUp(supply)    # shuffle deck and draw initial cards

# has supply and list of players (info needed for the game)
class Round:
    def __init__(self, players, supply):
        self.players = players
        self.supply = supply

    def isEnd(self):
        if self.supply.supplyPile["Province"].count == 0:
            return True
        if self.supply.emptyPiles >= 3:
            return True
        return False


# This file analyzes the result of a voltage scan
# You can configure what it does using `volt_config.py`
# The input should be a csv file with the following columns:
#

import traceback

from volt_config import *
from consts import *

from numpy import *
import matplotlib as mpl
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import sys, os
from typing import *

cmap_diverging = plt.get_cmap('RdBu')
cmap_normal    = plt.get_cmap('viridis')

coils_for_dim = {
    'x': (3, 4),
    'y': (5, 6),
    'z': (1, 2),
}
up_dn_sd_for_dim = {
    'x': (4, 7, 10),
    'y': (5, 9, 11),
    'z': (6, 8, 12),
}
sup_sdn_ssd_for_dim = {
    'x': (13, 16, 19),
    'y': (14, 18, 20),
    'z': (15, 17, 21),
}

def heatmap_for_property(data, v_min, v_max, coila, coilb, prop_name, diverging=False, is_abs=False, mg=False, is_fit=False):
    fig, ax = plt.subplots(figsize=(8, 6), dpi=192)

    if diverging:
        amax = max(abs(data.max()), abs(data.min()))
        norm = mpl.colors.Normalize(vmin=-amax, vmax=amax)
        mapp = mpl.cm.ScalarMappable(norm, cmap_diverging)
        cmap = cmap_diverging
    elif mg:  # milligauss, for error plot
        norm = mpl.colors.Normalize(vmin=0, vmax=data.max())
        mapp = mpl.cm.ScalarMappable(norm, cmap_normal)
        cmap = cmap_normal
    else:
        if is_abs:
            norm = mpl.colors.LogNorm(vmin=data.min(), vmax=data.max())
        else:
            norm = mpl.colors.Normalize(vmin=data.min(), vmax=data.max())
        mapp = mpl.cm.ScalarMappable(norm, cmap_normal)
        cmap = cmap_normal

    mapp.set_array([])

    extent = (0, upper_lim + 0.5, 0, upper_lim + 0.5 ) if is_fit else (v_min, v_max, v_min, v_max)

    ax.imshow(data.T, cmap=cmap, norm=norm, extent=extent, origin='lower')
    ax.set_xlabel(f"Voltage for Coil {coila} [V]")
    ax.set_ylabel(f"Voltage for Coil {coilb} [V]")
    ax.set_title(prop_name)
    fig.colorbar(mapp)
    return fig, ax

def bilinear(X, m1, m2, b):
    v0, v1 = X
    return (m1*v0) + (m2*v1) + b

def bilinear_fit(data: ndarray, dimension: str, coila: int, coilb: int):
    up, dn, sd = up_dn_sd_for_dim[dimension]
    # sup, sdn, ssd = sup_sdn_ssd_for_dim[dimension]

    v0 = data[:, :, 0].flatten()
    v1 = data[:, :, 1].flatten()

    b_up = data[:, :, up].flatten()
    # s_up = data[:, :, sup].flatten()

    b_dn = data[:, :, dn].flatten()
    # s_dn = data[:, :, sdn].flatten()

    b_sd = data[:, :, sd].flatten()
    # s_sd = data[:, :, ssd].flatten()

    p_up, c_up = curve_fit(bilinear, (v0, v1), b_up)  # , sigma=s_up)
    p_dn, c_dn = curve_fit(bilinear, (v0, v1), b_dn)  # , sigma=s_dn)
    p_sd, c_sd = curve_fit(bilinear, (v0, v1), b_sd)  # , sigma=s_sd)

    e_up = sqrt(diag(c_up))
    e_dn = sqrt(diag(c_dn))
    e_sd = sqrt(diag(c_sd))

    sys.stdout.write(f" {GREEN}Done.{RESET} Parameters:\n")
    for (d, p, e) in (('Up', p_up, e_up), ('Down', p_dn, e_dn), ('Side', p_sd, e_sd)):
        print("    %s% 4s%s:  (% 5.3f ± %5.3f) ⋅ V_%i  +  (% 5.3f ± %5.3f) ⋅ V_%i  +  (% 5.3f ± %5.3f)  =  B%s" % (
            BOLD, d, RESET, p[0], e[0], coila, p[1], e[1], coilb, p[2], e[2], dimension
        ))

    sys.stdout.write(f"  {BOLD}Down{RESET} full compensation:")
    sys.stdout.write(" V_%i  =  %5.3f  +  %5.3f ⋅ V_%i\n" % (coilb, (-p_dn[2]/p_dn[1]), (-p_dn[0]/p_dn[1]), coila))

    v0s = tile(linspace(0, upper_lim + 0.5, 1024), 1024)
    v1s = repeat(linspace(0, upper_lim + 0.5, 1024), 1024)

    bup = bilinear((v0s, v1s), *p_up)
    bdn = bilinear((v0s, v1s), *p_dn)
    bsd = bilinear((v0s, v1s), *p_sd)

    err = abs(bdn)

    imin = argmin(err)

    print(f"  Minimum of fit error at {GREEN}({v0s[imin]}V, {v1s[imin]}V): {MAGENTA}{err[imin]} mG{RESET}.")

    bup.resize(1024, 1024)
    bdn.resize(1024, 1024)
    bsd.resize(1024, 1024)

    return bup, bdn, bsd, (-p_dn[0]/p_dn[1]), (-p_dn[2]/p_dn[1])

if __name__ == "__main__":
    coila, coilb  = coils_for_dim[dimension]
    up, dn, sd    = up_dn_sd_for_dim[dimension]
    sup, sdn, ssd = sup_sdn_ssd_for_dim[dimension]

    print(f"Running analysis for {BOLD}{dimension.upper()}{RESET} (coils {coila} and {coilb}).")

    sys.stdout.write("Loading data...")

    try:
        rdata = loadtxt(scan_out, delimiter=',', skiprows=2, comments='#')
    except Exception:
        sys.stdout.write(f" {BR_RED}Couldn't read CSV.{RESET} Traceback:\n")
        traceback.print_exc()
        exit(1)

    dim = int(sqrt(rdata.shape[0]))
    data = zeros((dim, dim, 22))

    for i in range(dim):
        for j in range(dim):
            data[i, j, :] = rdata[dim * i + j, 2:]

    del rdata

    sys.stdout.write(f" {GREEN}Loaded data{RESET} ({dim} × {dim}).\n")

    # run fits

    sys.stdout.write(f"Running fits...")

    fit_up, fit_dn, fit_sd, fitm, fitb = bilinear_fit(data, dimension, coila, coilb)

    _heatmap = lambda dat, name, diverging=False, is_abs=False, mg=False, is_fit=False: \
        heatmap_for_property(
            dat, lower_lim - resolution / 2, upper_lim + resolution / 2, coila, coilb, name, diverging, is_abs, mg, is_fit
        )

    bstr = 'B_' + dimension

    PLOTS: List[Tuple[str, Callable]] = [
        ('top_b',    lambda data: _heatmap(data[:,:,up],  f'Top ${bstr}$ [G]', True)),
        ('bottom_b', lambda data: _heatmap(data[:,:,dn],  f'Bottom ${bstr}$ [G]', True)),
        ('side_b',   lambda data: _heatmap(data[:,:,sd],  f'Side ${bstr}$ [G]', True)),
        ('s_top_b',  lambda data: _heatmap(data[:,:,sup], f'σ Top ${bstr}$ [G]', True)),
        ('err_sum',  lambda data:
          _heatmap(abs(data[:,:,dn])+abs(data[:,:,sd])+1e-9, f'|Bottom ${bstr}$| + 0.2|Side ${bstr}$|', False, True)
        ),
        ('top_fit',    lambda data: _heatmap(fit_up.T, f'Fit Top ${bstr}$ [G]', True, is_fit=True)),
        ('bottom_fit', lambda data: _heatmap(fit_dn.T, f'Fit Bottom ${bstr}$ [G]', True, is_fit=True)),
        ('side_fit',   lambda data: _heatmap(fit_sd.T, f'Fit Side ${bstr}$ [G]', True, is_fit=True)),
    ]

    err = (abs(data[:,:,dn])).flatten()
    i_min_err = argmin(err)
    # print(data[:,:,0].shape, "    ", i_min_err)
    min_v0    = data[:,:,0].flatten()[i_min_err]
    min_v1    = data[:,:,1].flatten()[i_min_err]
    print(f"Minimum of measured error at {GREEN} ({min_v0}V, {min_v1}V): {MAGENTA}{1000*err[i_min_err]} mG{RESET}.")

    n_plots = len(PLOTS)

    if do_plots:
        for i, (name, closure) in enumerate(PLOTS):
            sys.stdout.write(f"\r{CLEAR_LINE}Running plot {i+1}/{n_plots}: '{name}'")
            fig, ax = closure(data)
            if name == 'bottom_fit':
                ax.plot(
                    [0, upper_lim + 0.5],
                    [fitb, fitm * (upper_lim + 0.5) + fitb],
                    c='black',
                    ls='--',
                    label='Fit'
                )
                ax.set_xlim(0, upper_lim + 0.5)
                ax.set_ylim(0, upper_lim + 0.5)
            # fig.savefig(os.path.join(plot_dir, f'{name}_{dimension}_{run_addl}.pgf'))
            fig.savefig(os.path.join(plot_dir, f'{name}_{dimension}_{run_addl}.png'))

        sys.stdout.write(f"\r{CLEAR_LINE}Plots {GREEN}done{RESET}.")


# import required libraries
from vidgear.gears import VideoGear
import cv2
import socket

options = {"hflip": True, "exposure_mode": "auto", "iso": 800, "exposure_compensation": 15, "awb_mode": "horizon", "sensor_mode": 0}

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("192.168.1.5", 55556))
stream = VideoGear(resolution=(128, 128), framerate=60, logging = True, **options).start()

while True:

    frame = stream.read()
    if frame is None:
        break

    byte_image = bytearray(frame)
    print(len(byte_image))
    with open('opencv.jpg','rb') as image:
        f = image.read()
        byte_image = bytearray(f)
    sock.send(byte_image)
    # {do something with the frame here}

    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        sock.close()
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()


import sys
from keras.models import load_model
import numpy as np
import jieba
from gensim.models.word2vec import Word2Vec
from keras.preprocessing.sequence import pad_sequences
from keras.models import model_from_json
from keras import backend as K

#p test_model.py ./data/test_x.csv ./dict.txt.big final_result.csv
X_test_address = sys.argv[1]
dictionary_address = sys.argv[2]
output_adress = sys.argv[3]
w2v_model = Word2Vec.load("word2vec_250.model")

def predict_model(X_test, model_name):
    json_file = open(model_name+'.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    model = model_from_json(loaded_model_json)
    model.load_weights(model_name+'.weight')
    Y_preds = model.predict(X_test)
    print(Y_preds)
    K.clear_session()
    del model
    return Y_preds

def predict_model_hdf5(X_test, model_name):
    model = load_model(model_name+'.h5')
    Y_preds = model.predict(X_test)
    print(Y_preds)
    K.clear_session()
    del model
    return Y_preds

def process_text(text):
    cutWords = []
    for w in text :
        if( not_Bnumber(w) ):
            cutWords.append(w)
    return cutWords

def not_Bnumber(text):
    if len(text)>1 and (text[0]=='B' or 'b') and text[1].isdigit():
        return False
    else:
        return True
# Load dict from TA
jieba.load_userdict(dictionary_address)

test_x = []
with open(X_test_address,'r',encoding = 'utf-8') as f :
    lines = f.readlines()
    for i,line in enumerate(lines) :
        if i != 0 :        # ignore first line "id,comment"
            test_x.append(line.split(',',1)[1])

cutWords = []
# cut test x
for x in test_x :
    # Using accurate mode
    setList = jieba.cut(x,cut_all=False)
    cutWords.append(process_text(setList))

embedding_matrix = np.zeros(  (len(w2v_model.wv.vocab.items())+1, w2v_model.vector_size) )
word2idx = {}

vocab_list = [(word, w2v_model.wv[word]) for word, _ in w2v_model.wv.vocab.items()]
for i, vocab in enumerate(vocab_list):
    word, vec = vocab
    embedding_matrix[i + 1] = vec
    word2idx[word] = i + 1

def text_to_index(corpus):
    new_corpus = []
    for doc in corpus:
        new_doc = []
        for word in doc:
            try:
                new_doc.append(word2idx[word])
            except:
                new_doc.append(0)
        new_corpus.append(new_doc)
    return np.array(new_corpus)

PADDING_LENGTH = 100
X_test = text_to_index(cutWords)
X_test = pad_sequences(X_test, maxlen=PADDING_LENGTH, padding='post', truncating='post')

Y_preds0 = predict_model(X_test, 'rnn250_00')
Y_preds1 = predict_model(X_test, 'rnn250_01')
Y_preds2 = predict_model(X_test, 'rnn250_02')
Y_preds7 = predict_model(X_test, 'rnn250_07')
Y_preds_label = (Y_preds0 + Y_preds1 + Y_preds2 + Y_preds7)/4

for i in range(Y_preds_label.shape[0]):
    if(Y_preds_label[i]>=0.5):
        Y_preds_label[i] = 1
    else:
        Y_preds_label[i] = 0

save = open(output_adress,'w')
save.write("id,label\n")
for i in range(len(Y_preds_label)):
    save.write(str(i) + "," + str(int(Y_preds_label[i])) + "\n")
save.close()

'''
START_TIME = time.time()
Y_preds0 = predict_model(X_test, 'rnn250_00')
runTime = time.time() - START_TIME
print(runTime)
np.save('rnn250_00.npy', Y_preds0)
'''


from advent.intcode import compute, run, run_io
import asyncio
import pytest

pytestmark = pytest.mark.asyncio

async def test_input():
    assert await run_io([42], [3, 0, 4, 0, 99]) == 42

async def test_large_number():
    output = await run_io([], [1102, 34915192, 34915192, 7, 4, 7, 99, 0])
    assert len(str(output)) == 16

async def test_relative_mode():
    program = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
    iq = asyncio.Queue()
    oq = asyncio.Queue()
    task = asyncio.create_task(compute(program, iq, oq))
    await task

    output = []
    while True:
        try:
            output.append(oq.get_nowait())
        except asyncio.QueueEmpty:
            break

    assert output == program

async def test_relative_mode_simple():
    program = [109, 36, 204, -34, 99]
    output = await run_io([], program)
    assert output == 204

async def test_relative_mode_input():
    program = [109, 36, 203, 0, 204, 0, 99]
    output = await run_io([42], program)
    assert output == 42

async def test_relative_mode_output():
    program = [109, -1, 204, 1, 99]
    output = await run_io([], program)
    assert output == 109

async def test_long_instruction():
    program = [109, 36, 21102, 3, 4, 0, 204, 0, 99]
    output = await run_io([], program)
    assert output == 12

async def test_relative_base_adjust():
    program = [109, 36, 203, -2, 209, -2, 204, -41, 99]
    output = await run_io([5], program)
    assert output == 109

async def test_output():
    output = await run_io([], [104, 1125899906842624, 99])
    assert output == 1125899906842624

async def test_equals():
    assert await run_io([8], [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]) == 1
    assert await run_io([9], [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]) == 0

async def test_less_than():
    assert await run_io([7], [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]) == 1
    assert await run_io([8], [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]) == 0
    assert await run_io([9], [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]) == 0

async def test_less_than_immediate():
    program = [3, 3, 1107, -1, 8, 3, 4, 3, 99]
    assert await run_io([7], program) == 1
    assert await run_io([8], program) == 0
    assert await run_io([9], program) == 0

async def test_equals_immediate():
    program = [3, 3, 1108, -1, 8, 3, 4, 3, 99]
    assert await run_io([7], program) == 0
    assert await run_io([8], program) == 1
    assert await run_io([9], program) == 0

async def test_jump():
    program = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]
    assert await run_io([0], program) == 0
    assert await run_io([8], program) == 1
    assert await run_io([9], program) == 1

async def test_jump_immediate():
    program = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]
    assert await run_io([0], program) == 0
    assert await run_io([8], program) == 1
    assert await run_io([9], program) == 1

async def test_big_program_jump():
    program = [
        3,
        21,
        1008,
        21,
        8,
        20,
        1005,
        20,
        22,
        107,
        8,
        21,
        20,
        1006,
        20,
        31,
        1106,
        0,
        36,
        98,
        0,
        0,
        1002,
        21,
        125,
        20,
        4,
        20,
        1105,
        1,
        46,
        104,
        999,
        1105,
        1,
        46,
        1101,
        1000,
        1,
        20,
        4,
        20,
        1105,
        1,
        46,
        98,
        99,
    ]

    assert await run_io([0], program) == 999
    assert await run_io([8], program) == 1000
    assert await run_io([9], program) == 1001

async def test_whole_program():
    with open("input/02.txt") as file_input:
        symbols = file_input.read().strip("\n").split(",")
        code = [int(symbol) for symbol in symbols]
        output = await run(code, 12, 2)
        assert output == 5534943

async def test_program_doesnt_mutate_input():
    with open("input/02.txt") as file_input:
        symbols = file_input.read().strip("\n").split(",")
        code = [int(symbol) for symbol in symbols]
        orig = code.copy()
        await run(code, 12, 2)
        assert code == orig


import click
import os
import json

@click.group()
def main():
    pass

@main.command()
@click.option('clang_path', '--clang',
              help = 'The clang bin path',
              type = click.STRING,
              default= '/nix/store/p5w6pzixrxzikq4vjvkzg7xccwwbvanv-clang-wrapper-11.1.0/bin/clang')
def cpp_json(clang_path: str):
    include_path_env = os.getenv('CMAKE_INCLUDE_PATH')
    if include_path_env == None:
      print("CMAKE_INCLUDE_PATH has no content.")
      return
    include_paths = list(set(include_path_env.split(':')))
    print(include_paths)

    js = {
      "configurations": [
          {
            "name": "Linux",
            "includePath": [
              "${workspaceFolder}/**"
            ],
            "defines": [
              "SPDLOG_FMT_EXTERNAL"
            ],
            "compilerPath": clang_path,
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "linux-clang-x64"
          }
      ],
      "version": 4
    }
    print(js)
    for e in include_paths:
      js['configurations'][0]['includePath'].append(e)

    if not os.path.exists('.vscode'):
      os.makedirs('.vscode')

    with open('.vscode/c_cpp_properties.json', 'w') as fp:
      json.dump(js, fp, indent=4)

if __name__ == '__main__':
    main()


import re

#Opens the input File and extracts the lines of C code
inputFile = open('sample.txt','r')
cCode = inputFile.read().split('\n')
print(cCode)
cLines = []
for line in cCode:
    if line !='':
        line = line.lstrip().rstrip()
        cLines.append(line)
print(cLines)

# Output File
assemblyFile = open('assemblyCode.txt','w')

#Defining all the lists and counters
symbolList = []
globalList = []
arrayList = []
extList = []
loopCounter = 0
ifCounter = 0
varCounter = 0

#RegEx
add = re.compile(r'(.+)=(.+)\+(.+)')
var = re.compile(r'var (.*)=(.*)')
sub = re.compile(r'(.+)=(.+)\-(.+)')
glo = re.compile(r'glob var (.*)=(.*)')
arr = re.compile(r'var (.+)\[(.+)\]')       #seems to be working
ext = re.compile(r'extern(.*)')
slop = re.compile(r'loop(.+)')
elop = re.compile(r'endloop(.*)')
ifgt = re.compile(r'if (.*) > (.*)')
eif = re.compile(r'endif(.*)')
ifgte = re.compile(r'if (.*) >= (.*)')
ifeq = re.compile(r'if (.*) == (.*)')
assign = re.compile(r'(.+) = (.+)')
arrAssign = re.compile(r'(.+)\[(.+)\] := (.+)')
#Boolean for Integer Checking
def tryInt(number):
    try:
        int(number)
        return True
    except ValueError:
        return False

#Shit starts
for line in cLines:
    code = ''
    # add = re.compile(r'(.+)=(.+)\+(.+)')
    if add.match(line):
        c = add.match(line).group(1).lstrip().rstrip()
        a = add.match(line).group(2).lstrip().rstrip()
        b = add.match(line).group(3).lstrip().rstrip()
        if c in extList:
            c = c + '_g'
        else:
            c = c + '_'

        if not tryInt(a):
            if a in extList:
                a = a + '_g'
            else:
                a = a + '_'
        if not tryInt(b):
            if b in extList:
                b = b + '_g'
            else:
                b = b + '_'
        #Both integers so do direct addition
        if tryInt(b) and tryInt(a):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'ADI ' + str(b) + '\n'
            code = code + 'STA ' + c + '\n'
        #Single part is integer

        elif tryInt(a):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'ADD M'+ '\n'
            code = code + 'STA ' + c + '\n'
        elif tryInt(b):
            code = code + 'LDA ' + a + '\n'
            code = code + 'ADI ' + str(b) + '\n'
            code = code + 'STA ' + c + '\n'
        #None integers
        else:
            code = code + 'LDA ' + a + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'ADD M' + '\n'
            code = code + 'STA ' + c + '\n'
    #Similar to addition
    elif sub.match(line):
        c = add.match(line).group(1).lstrip().rstrip()
        a = add.match(line).group(2).lstrip().rstrip()
        b = add.match(line).group(3).lstrip().rstrip()
        if c in extList:
            c = c + '_g'
        else:
            c = c + '_'

        if not tryInt(a):
            if a in extList:
                a = a + '_g'
            else:
                a = a + '_'
        if not tryInt(b):
            if b in extList:
                b = b + '_g'
            else:
                b = b + '_'
        if tryInt(b) and tryInt(a):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'SUI ' + str(b) + '\n'
            code = code + 'STA ' + c + '\n'
        elif tryInt(a):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'SUB M'+ '\n'
            code = code + 'STA ' + c + '\n'
        elif tryInt(b):
            code = code + 'LDA ' + a + '\n'
            code = code + 'SUI ' + str(b) + '\n'
            code = code + 'STA ' + c + '\n'
        else:
            code = code + 'LDA ' + a + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'SUB M' + '\n'
            code = code + 'STA ' + c + '\n'

    # Various variables definitions
    elif var.match(line):
        variable = var.match(line).group(1).lstrip().rstrip()
        content = var.match(line).group(2).lstrip().rstrip()
        symbolList.append(variable)
        varCounter +=1
        code = code + 'JMP DEF' + str(varCounter) + '\n'
        code = code + variable + '_:' + ' DB ' + str(content) + '\n'
        code = code + 'DEF' + str(varCounter) + ': '
    elif glo.match(line):
        variable = var.match(line).group(1).lstrip().rstrip()
        content = var.match(line).group(2).lstrip().rstrip()
        varCounter +=1
        code = code + 'JMP DEF' + str(varCounter) + '\n'
        code = code + variable + '_g:' + ' DB ' + str(content) + '\n'
        code = code + 'DEF' + str(varCounter) + ': '
    elif arr.match(line):
        arrName = arr.match(line).group(1).lstrip().rstrip()
        arrSize = arr.match(line).group(2).lstrip().rstrip()
        arrayList.append(arrName)
        varCounter += 1
        code = code + 'JMP DEF' + str(varCounter) + '\n'
        code = code + arrName + '_:' + ' DA ' + str(arrSize) + '\n'
        code = code + 'DEF' + str(varCounter) + ': '
    elif ext.match(line):
        variable = ext.match(line).group(1).lstrip().rstrip()
        extList.append(variable)

    ### Variable Assignment
    elif assign.match(line):
        variable = assign.match(line).group(1).lstrip().rstrip()
        number = assign.match(line).group(2).lstrip().rstrip()
        if variable in extList:
            variable = variable + '_g'
        else:
            variable = variable + '_'
        code = code + 'MVI A,' + str(number) + '\n'
        code = code + 'STA ' + variable + '\n'
    elif arrAssign.match(line):
        arrVar = arrAssign.match(line).group(1).lstrip().rstrip()
        arrIndex = arrAssign.match(line).group(2).lstrip().rstrip()
        arrValue = arrAssign.match(line).group(3).lstrip().rstrip()
        arrVar = arrVar + '_'
        print(arrVar)
        code = code + 'MVI A,' + str(arrValue) + '\n'
        code = code + 'STA ' + arrVar + '+' + str(arrIndex) + '\n'

    #What if we kept the burden of putting proper loops and endloops indexes to
    #the user itself? We extract the counter presented by the user and
    #use it to set the labels accordingly. Would be a little easier, but not much.
    elif slop.match(line):
        num = slop.match(line).group(1).lstrip().rstrip()
        if not tryInt(num):
            if num in extList:
                num = num + '_g'
            else:
                num = num + '_'
        loopCounter += 1
        code = code + 'PUSH E' + '\n'
        if tryInt(num):
            code = code + 'MVI E,' + str(num) + '\n'
        else:
            code = code + 'LXI H,' + num + '\n'
            code = code + 'MOV E,M' + '\n'
        code = code + 'LOOP' + str(loopCounter) + ': '
    elif elop.match(line):
        code = code + 'DCR E' + '\n'
        code = code + 'JNZ LOOP' + str(loopCounter) + '\n'
        code = code + 'POP E' + '\n'
        loopCounter -=1

    ##Conditionals start from here
    ##
    elif ifgte.match(line):
        a = ifgte.match(line).group(1).lstrip().rstrip()
        b = ifgte.match(line).group(2).lstrip().rstrip()
        if not tryInt(a):
            if a in extList:
                a = a + '_g'
            else:
                a = a + '_'
        if not tryInt(b):
            if b in extList:
                b = b + '_g'
            else:
                b = b + '_'
        ifCounter += 1
        if tryInt(a) and tryInt(b):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'SUI ' + str(b) + '\n'
        elif tryInt(a):
            code = code + 'MVI A,' + str(a) + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'SUB M' + '\n'
        elif tryInt(b):
            code = code + 'LDA ' + a + '\n'
            code = code + 'SUI ' + str(b) + '\n'
        else:
            code = code + 'LDA ' + a + '\n'
            code = code + 'LXI H,' + b + '\n'
            code = code + 'SUB M' + '\n'
        code = code + 'JM ' + 'ENDIF' + str(ifCounter) + '\n'
    elif ifgt.match(line):
        a = ifgt.match(line).group(1).lstrip().rstrip()
        b = ifgt.match(line).group(2).lstrip().rstrip()
        if not tryInt(a):
            if a in extList:
                a = a + '_g'
            else:
                a = a + '_'
        if not tryInt(b):
            if b in extList:
                b = b + '_g'
            else:
                b = b + '_'
        ifCounter += 1
        if tryInt(a) and tryInt(b):
            code = code + 'MVI A,' + str(b) + '\n'
            code = code + 'SUI ' + str(a) + '\n'
        elif tryInt(b):
            code = code + 'MVI A,' + str(b) + '\n'
            code = code + 'LXI H,' + a + '\n'
            code = code + 'SUB M' + '\n'
        elif tryInt(b):
            code = code + 'LDA ' + b + '\n'
            code = code + 'SUI ' + str(a) + '\n'
        else:
            code = code + 'LDA ' + b + '\n'
            code = code + 'LXI H,' + a + '\n'
            code = code + 'SUB M' + '\n'
        code = code + 'JP ' + 'ENDIF' + str(ifCounter) + '\n'
    elif ifeq.match(line):
        a = ifeq.match(line).group(1).lstrip().rstrip()
        b = ifeq.match(line).group(2).lstrip().rstrip()
        if not tryInt(a):
            if a in extList:
                a = a + '_g'
            else:
                a = a + '_'
        if not tryInt(b):
            if b in extList:
                b = b + '_g'
            else:
                b = b + '_'
        ifCounter += 1
        if tryInt(a) and tryInt(b):
            code = code + 'MVI A,' + str(b) + '\n'
            code = code + 'SUI ' + str(a) + '\n'
        elif tryInt(b):
            code = code + 'MVI A,' + str(b) + '\n'
            code = code + 'LXI H,' + a + '\n'
            code = code + 'SUB M' + '\n'
        elif tryInt(b):
            code = code + 'LDA ' + b + '\n'
            code = code + 'SUI ' + str(a) + '\n'
        else:
            code = code + 'LDA ' + b + '\n'
            code = code + 'LXI H,' + a + '\n'
            code = code + 'SUB M' + '\n'
        code = code + 'JNZ ' + 'ENDIF' + ifCounter + '\n'
    elif eif.match(line):
        code = code + 'ENDIF' + str(ifCounter) + ': '
        ifCounter -=1

    assemblyFile.write(code)

code = 'HLT\n'
assemblyFile.write(code)


import cv2
import numpy as np
def ContrastEnhancement(image):
    #### it work very well

    image=np.array(image,np.uint8)
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    # cv2.imshow("lab", lab)
    # cv2.waitKey(0)

    # -----Splitting the LAB image to different channels-------------------------
    l, a, b = cv2.split(lab)

    # -----Applying CLAHE to L-channel-------------------------------------------
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(6, 6))
    cl = clahe.apply(l)

    # -----Merge the CLAHE enhanced L-channel with the a and b channel-----------
    limg = cv2.merge((cl, a, b))

    # -----Converting image from LAB Color model to RGB model--------------------
    final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
#    final=np.array(final/255.0,np.float32)
#     cv2.imshow("lab", final)
#     cv2.waitKey(0)
    return final


#!/usr/bin/env python3

import vapoursynth as vs
import audiocutter
import lvsfunc as lvf
from subprocess import call

core = vs.core
ts_in = r'02/Val x Love - 02 (AT-X HD MPEG2).d2v'
src = lvf.src(ts_in)

if ts_in.endswith('d2v'):
    src = core.vivtc.VDecimate(src)

ac = audiocutter.AudioCutter()

vid = ac.split(src, [(739,3687),(3928,35027)])

ac.ready_qp_and_chapters(vid)

vid.set_output(0)

if __name__ == "__main__":
    ac.cut_audio(r'ValxLove_02_cut.m4a', audio_source=r'02/Val x Love - 02 (AT-X HD MPEG2) T104f stereo 255 kbps DELAY -322 ms.aac')

Miscellaneous groups/[ViM]/Currently Airing/Val x Love/ac_ValxLove_02.py


from tabulate import tabulate
from collections import OrderedDict

def isterminal(char):
    if(char.isupper() or char == "#"):
        return False
    else:
        return True
def insert(grammar, lhs, rhs):
    if(lhs in grammar and rhs not in grammar[lhs] and grammar[lhs] != "null"):
        grammar[lhs].append(rhs)
    elif(lhs not in grammar or grammar[lhs] == "null"):
        grammar[lhs] = [rhs]
    return grammar
def first(lhs, grammar, grammar_first):
    rhs = grammar[lhs]
    for i in rhs:
        k = 0
        flag = 0
        current = []
        confirm = 0
        flog = 0
        if(lhs in grammar and "#" in grammar_first[lhs]):
            flog = 1
        while(1):
            check = []
            if(k>=len(i)):
                if(len(current)==0 or flag == 1 or confirm == k or flog == 1):
                    grammar_first = insert(grammar_first, lhs, "#")
                break
            if(i[k].isupper()):
                if(grammar_first[i[k]] == "null"):
                    grammar_first = first(i[k], grammar, grammar_first)
                for j in grammar_first[i[k]]:
                    grammar_first = insert(grammar_first, lhs, j)
                    check.append(j)
            else:
                grammar_first = insert(grammar_first, lhs, i[k])
                check.append(i[k])
            if(i[k]=="#"):
                flag = 1
            current.extend(check)
            if("#" not in check):
                if(flog == 1):
                    grammar_first = insert(grammar_first, lhs, "#")
                break
            else:
                confirm += 1
                k+=1
                grammar_first[lhs].remove("#")
    return(grammar_first)
def rec_follow(k, next_i, grammar_follow, i, grammar, start, grammar_first, lhs):
    if(len(k)==next_i):
        if(grammar_follow[i] == "null"):
            grammar_follow = follow(i, grammar, grammar_follow, start)
        for q in grammar_follow[i]:
            grammar_follow = insert(grammar_follow, lhs, q)
    else:
        if(k[next_i].isupper()):
            for q in grammar_first[k[next_i]]:
                if(q=="#"):
                    grammar_follow = rec_follow(k, next_i+1, grammar_follow, i, grammar, start, grammar_first, lhs)
                else:
                    grammar_follow = insert(grammar_follow, lhs, q)
        else:
            grammar_follow = insert(grammar_follow, lhs, k[next_i])

    return(grammar_follow)

def follow(lhs, grammar, grammar_follow, start):
    for i in grammar:
        j = grammar[i]
        for k in j:
            if(lhs in k):
                next_i = k.index(lhs)+1
                grammar_follow = rec_follow(k, next_i, grammar_follow, i, grammar, start, grammar_first, lhs)
    if(lhs==start):
        grammar_follow = insert(grammar_follow, lhs, "$")
    return(grammar_follow)
def show_dict(dictionary):
    for key in dictionary.keys():
        print(key+"  :  ", end = "")
        for item in dictionary[key]:
            if(item == "#"):
                print("Epsilon, ", end = "")
            else:
                print(item+", ", end = "")
        print("")
def get_rule(non_terminal, terminal, grammar, grammar_first):
    for rhs in grammar[non_terminal]:
        for rule in rhs:
            if(rule == terminal):
                string = non_terminal+"="+rhs
                return string

            elif(rule.isupper() and terminal in grammar_first[rule]):
                string = non_terminal+"="+rhs
                return string

grammar = OrderedDict()
grammar_first = OrderedDict()
grammar_follow = OrderedDict()

f = open('first_follow_grammar.txt')
for i in f:
    i = i.replace("\n", "")
    lhs = ""
    rhs = ""
    flag = 1
    for j in i:
        if(j=="="):
            flag = (flag+1)%2
            continue
        if(flag==1):
            lhs += j
        else:
            rhs += j
    grammar = insert(grammar, lhs, rhs)
    grammar_first[lhs] = "null"
    grammar_follow[lhs] = "null"

print("Grammar")
show_dict(grammar)

for lhs in grammar:
    if(grammar_first[lhs] == "null"):
        grammar_first = first(lhs, grammar, grammar_first)

start = list(grammar.keys())[0]
for lhs in grammar:
    if(grammar_follow[lhs] == "null"):
        grammar_follow = follow(lhs, grammar, grammar_follow, start)

print("\nFollow")
show_dict(grammar_follow)

f.close()


import time

from nbformat import read
from runipy.notebook_runner import NotebookRunner
from steemdata.helpers import timeit

def run_notebook(filename='Charts'):
    """ Run Specific Notebook. """
    # convert to v3 of notebook format, as v4 is not supported yet
    notebook = read(open('%s.ipynb' % filename), 3)
    r = NotebookRunner(notebook)
    r.run_notebook()

def run():
    """ Run a chart updating notebook twice a day. """
    while True:
        print('Running Charts notebook...')
        with timeit():
            run_notebook()
        print('Done.')
        time.sleep(3600 * 12)

if __name__ == '__main__':
    run()


#!/usr/bin/env python3
#
# Copyright (c) 2017-2020 The Bitcoin ABC developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

import mock
import unittest

from test.abcbot_fixture import ABCBotFixture
import test.mocks.phabricator

class EndpointBackportcheckTestCase(ABCBotFixture):
    def test_backportCheck_happyPath(self):
        self.phab.differential.revision.search.return_value = test.mocks.phabricator.Result([{
            'id': '1234',
            'fields': {
                'summary': 'This is a test summary'
            },
        }])

        response = self.post_json_with_hmac(
            '/backportCheck', self.headers, {'object': {'phid': '1234'}})
        assert response.status_code == 200
        self.phab.differential.revision.search.assert_called_with(
            constraints={"phids": ['1234']})
        self.phab.differential.revision.edit.assert_not_called()

    def test_backportCheck_invalid_json(self):
        response = self.post_data_with_hmac(
            '/backportCheck', self.headers, "not: a valid json")
        self.assertEqual(response.status_code, 415)

    def test_backportCheck_hasNoPRs(self):
        # Despite potential matches for linking PRs, the phab API should not be
        # called to update the summary, even if the result would be the same.
        self.phab.differential.revision.search.return_value = test.mocks.phabricator.Result([{
            'id': '1234',
            'fields': {
                'summary': "This is a test summary `Ignore this backport PR2345` some text.\n"
                "Some text ```Ignore this PR3456``` Some more text.\n"
                "```\nPR4567 in a multi-line code block\nPR5678 in the same code block\n```\n"
                "  Ignore this indented PR4567"
                # Note that short numbered PRs are much more common when referencing non-bitcoin PRs,
                # so we'll ignore them for now.
                "Ignore short numbered PRs: PR123"
                # But we do support secp256k1 PRs with 2-3 digits, so make
                # sure they're also ignored properly
                "This is a test summary `Ignore this secp256k1 backport PR234` some text.\n"
                "Some text ```Ignore this secp256k1 PR345``` Some more text.\n"
                "```\nsecp256k1 PR456 in a multi-line code block\nsecp256k1 PR567 in the same code block\n```\n"
                "  Ignore this indented secp256k1 PR456"
                "Ignore long numbered PRs for secp256k1: PR1234"
                "Ignore short numbered PRs for secp256k1: PR1",
            },
        }])

        response = self.post_json_with_hmac(
            '/backportCheck', self.headers, {'object': {'phid': '1234'}})
        assert response.status_code == 200
        self.phab.differential.revision.search.assert_called_with(
            constraints={'phids': ['1234']})
        self.phab.differential.revision.edit.assert_not_called()

    def test_backportCheck_hasPRs(self):
        self.phab.differential.revision.search.return_value = test.mocks.phabricator.Result([{
            'id': '1234',
            'fields': {
                'summary': "This is a test summary\n"
                # Bitcoin Core links
                "Backport of Core PR2345 and PR34567\n"
                "PR6789 outside of a code block `PR4567 inside a code block`\n"
                "```PR4567 in a single-line code block```\n"
                "```\nPR4567 in a multi-line code block\n```\n"
                "  PR4567 in a code block using indentation\n"
                "Another backport PR567890\n"
                # secp256k1 links
                "Backport of Secp256k1 PR23 and PR345\n"
                "SECP256K1 PR678 outside of a code block `secp256k1 PR456 inside a code block`\n"
                "```secp256k1 PR456 in a single-line code block```\n"
                "```\nsecp256k1 PR456 in a multi-line code block\n```\n"
                "  secp256k1 PR456 in a code block using indentation\n"
                "Another secp backport PR567",
            },
        }])

        response = self.post_json_with_hmac(
            '/backportCheck', self.headers, {'object': {'phid': '1234'}})
        assert response.status_code == 200
        self.phab.differential.revision.search.assert_called_with(
            constraints={'phids': ['1234']})
        calls = [mock.call(transactions=[{
            "type": "summary",
            "value": "This is a test summary\n"
            # Bitcoin Core links
            "Backport of Core [[https://github.com/bitcoin/bitcoin/pull/2345 | PR2345]] and "
            "[[https://github.com/bitcoin/bitcoin/pull/34567 | PR34567]]\n"
            "[[https://github.com/bitcoin/bitcoin/pull/6789 | PR6789]] outside of a code block `PR4567 inside a code block`\n"
            "```PR4567 in a single-line code block```\n"
            "```\nPR4567 in a multi-line code block\n```\n"
            "  PR4567 in a code block using indentation\n"
            "Another backport [[https://github.com/bitcoin/bitcoin/pull/567890 | PR567890]]\n"
            # secp256k1 links
            "Backport of Secp256k1 [[https://github.com/bitcoin-core/secp256k1/pull/23 | PR23]] and "
            "[[https://github.com/bitcoin-core/secp256k1/pull/345 | PR345]]\n"
            "SECP256K1 [[https://github.com/bitcoin-core/secp256k1/pull/678 | PR678]] outside of a code block `secp256k1 PR456 inside a code block`\n"
            "```secp256k1 PR456 in a single-line code block```\n"
            "```\nsecp256k1 PR456 in a multi-line code block\n```\n"
            "  secp256k1 PR456 in a code block using indentation\n"
            "Another secp backport [[https://github.com/bitcoin-core/secp256k1/pull/567 | PR567]]",
        }], objectIdentifier='1234'), mock.call(transactions=[{
            "type": "comment",
            "value": "[Bot Message]\n"
            "One or more PR numbers were detected in the summary.\n"
            "Links to those PRs have been inserted into the summary for reference.",
        }], objectIdentifier='1234')]
        self.phab.differential.revision.edit.assert_has_calls(
            calls, any_order=True)

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


'''
Desenvolva um programa que leia seis números inteiros e mostre a soma apenas daqueles que forem pares.
Se o valor digitado for ímpar, desconsidere-o.
'''
soma = 0
cont = 0
for c in range(1,7):
    num = int(input('Digite um valor: '))
    if num % 2 == 0:
        soma += num
print('A soma dos números é {}'.format(soma))


import numpy as np
import measurements as m
from core import Experiment, Rat, TrialEpoch

cached_data = True

expt = Experiment(
    name="201704",
    cache_key="epoch",
    plot_key="",
    trial_epochs=[
        TrialEpoch("mags", start_idx=1, stop_idx=2),
        TrialEpoch("light1", start_idx=4, stop_idx=5),
        TrialEpoch("light2", start_idx=6, stop_idx=7),
        TrialEpoch("sound1", start_idx=8, stop_idx=9),
        TrialEpoch("sound2", start_idx=10, stop_idx=11),
        TrialEpoch("trial1", start_idx=12, stop_idx=13),
        TrialEpoch("trial2", start_idx=14, stop_idx=15),
        TrialEpoch("trial3", start_idx=16, stop_idx=17),
        TrialEpoch("trial4", start_idx=18, stop_idx=19),
        TrialEpoch("baseline", start_idx=4, duration=-10),
        TrialEpoch("baseline", start_idx=6, duration=-10),
    ],
    measurements=[m.Duration(), m.Count(), m.Latency(), m.AtLeastOne()],
    rats=[
        Rat('R141', group="1", gender="male"),
        Rat('R142', group="2", gender="female"),
        Rat('R143', group="2", gender="male"),
        Rat('R144', group="1", gender="female"),
        Rat('R145', group="2", gender="male"),
        Rat('R146', group="1", gender="female"),
        Rat('R147', group="1", gender="male"),
        Rat('R148', group="2", gender="female"),
    ],
    ignore_sessions=['!2017-04-14'],
)

short_sessions = [val for val in np.arange(20) + 1]

colours = {'baseline, ': '#252525',
           'light, rewarded': '#1f77b4',
           'light, unrewarded': '#aec7e8',
           'light1, rewarded': '#1f77b4',
           'light1, unrewarded': '#aec7e8',
           'light2, rewarded': '#1f77b4',
           'light2, unrewarded': '#aec7e8',
           'sound, rewarded': '#2ca02c',
           'sound, unrewarded': '#98df8a',
           'sound1, rewarded': '#2ca02c',
           'sound1, unrewarded': '#98df8a',
           'sound2, rewarded': '#e377c2',
           'sound2, unrewarded': '#f7b6d2'
           }

def add_datapoints(session, data, rat):
    print(session)

    def add_data(cue, trial=None, n_missing=0):
        if trial is not None:
            meta = {
                "cue_type": cue[:-1],
                "trial_type": trial[-1],
                "rewarded": "rewarded" if trial[-1] in ("2", "4") else "unrewarded",
                "cue": cue,
            }
            trial = data[trial]
            cue = data[cue]
            session.add_epoch_data(rat.rat_id, trial.intersect(cue), meta, n_missing)
        else:
            meta = {
                "cue_type": cue,
                "trial_type": "",
                "rewarded": "",
                "cue": cue,
            }
            session.add_epoch_data(rat.rat_id, data[cue], meta, n_missing)

    if rat.group == "1":
        if session.number in short_sessions:
            add_data("light1", "trial1", n_missing=8)
            add_data("sound2", "trial1", n_missing=8)
            add_data("light1", "trial2", n_missing=8)
            add_data("sound1", "trial2", n_missing=8)
            add_data("light2", "trial3", n_missing=8)
            add_data("sound1", "trial3", n_missing=8)
            add_data("light2", "trial4", n_missing=8)
            add_data("sound2", "trial4", n_missing=8)
            add_data("baseline", n_missing=32)
        else:
            add_data("light1", "trial1")
            add_data("sound2", "trial1")
            add_data("light1", "trial2")
            add_data("sound1", "trial2")
            add_data("light2", "trial3")
            add_data("sound1", "trial3")
            add_data("light2", "trial4")
            add_data("sound2", "trial4")
            add_data("baseline")

    elif rat.group == "2":
        if session.number in short_sessions:
            add_data("light2", "trial1", n_missing=8)
            add_data("sound2", "trial1", n_missing=8)
        elif rat.rat_id == "R148" and session.number == 25:  # "!2017-05-09"
            add_data("light2", "trial1", n_missing=1)
            add_data("sound2", "trial1", n_missing=1)
        else:
            add_data("light2", "trial1")
            add_data("sound2", "trial1")
        if session.number in short_sessions:
            add_data("light2", "trial2", n_missing=8)
            add_data("sound1", "trial2", n_missing=8)
        elif rat.rat_id == "R148" and session.number in [24, 26]:  # "!2017-05-08", "!2017-05-10"
            add_data("light2", "trial2", n_missing=2)
            add_data("sound1", "trial2", n_missing=2)
        elif rat.rat_id == "R148" and session.number == 28:  # "!2017-05-12"
            add_data("light2", "trial2", n_missing=1)
            add_data("sound1", "trial2", n_missing=1)
        else:
            add_data("light2", "trial2")
            add_data("sound1", "trial2")
        if session.number in short_sessions:
            add_data("light1", "trial3", n_missing=8)
            add_data("sound1", "trial3", n_missing=8)
        elif rat.rat_id == "R148" and session.number == 24:  # "!2017-05-08"
            add_data("light1", "trial3", n_missing=1)
            add_data("sound1", "trial3", n_missing=1)
        elif rat.rat_id == "R148" and session.number == 26:  # "!2017-05-10"
            add_data("light1", "trial3", n_missing=2)
            add_data("sound1", "trial3", n_missing=2)
        else:
            add_data("light1", "trial3")
            add_data("sound1", "trial3")
        if session.number in short_sessions:
            add_data("light1", "trial4", n_missing=8)
            add_data("sound2", "trial4", n_missing=8)
        else:
            add_data("light1", "trial4")
            add_data("sound2", "trial4")
        if session.number in short_sessions:
            add_data("baseline", n_missing=32)
        elif rat.rat_id == "R148" and session.number == 24:  # "!2017-05-08"
            add_data("baseline", n_missing=2)
        elif rat.rat_id == "R148" and session.number == 26:  # "!2017-05-10"
            add_data("baseline", n_missing=4)
        else:
            add_data("baseline")

expt.add_datapoints = add_datapoints
expt.analyze(cached_data=cached_data)

change = [10.5, 20.5, 45.5]

expt.plot_all(cached_data=cached_data, measure="Duration", colours=colours, change=change, filetype="svg")
expt.plot_all(cached_data=cached_data, colours=colours, change=change)


""" File name:   disease_scenario.py
    Author:      Longfei Zhao
    Date:        26.2.2017
    Description: This file represents a scenario simulating the spread of an
                 infectious disease around Australia. It should be
                 implemented for Part 1 of Exercise 4 of Assignment 0.

                 See the lab notes for a description of its contents.
"""

class DiseaseScenario():
    """docstring for DiseaseScenario."""

    def __init__(self):
        self.locations = []
        self.disease = {}
        self.conn = {}

    def read_scenario_file(self, scenario_file_name):
        try:
            file = open(scenario_file_name)
            for line in file:
                info = line.split()
                keyword = info[0]
                if keyword == 'threshold':
                    self.threshold = float(info[1])
                elif keyword == 'growth':
                    self.growth = float(info[1])
                elif keyword == 'spread':
                    self.spread = float(info[1])
                elif keyword == 'location':
                    self.locations.append(info[1])
                    self.disease.setdefault(info[1], 0.0)
                    self.conn.setdefault(info[1], set())
                elif keyword == 'start':
                    self.location = info[1]
                elif keyword == 'disease':
                    self.disease[info[1]] = float(info[2])
                elif keyword == 'conn':
                    self.conn[info[1]].add(info[2])
                    self.conn[info[2]].add(info[1])
            return True
        except IOError:
            return False
    def valid_moves(self):
        ''' return a list of valid moves.
        '''
        res = []
        res.append(self.location)
        res.extend(self.conn[self.location])
        return res
    def move(self, loc):
        ''' move the agent to the specified location and clear the disease there.
        '''
        try:
            if loc in self.conn[self.location] or loc == self.location:
                self.location = loc
                self.disease[loc] = 0
            else:
                raise ValueError()
        except ValueError:
            raise
    def spread_disease(self):
        ''' spread the disease according the threshold, growth, spread, and connections between locations.
        '''
        nextDisease = self.disease.copy()
        for loc, value in nextDisease.items():
            if loc == self.location:
                value = 0.0
            else:
                value += self.disease[loc] * self.growth
                for neighbor in self.conn[loc]:
                    if self.disease[neighbor] >= self.threshold:
                        value += self.disease[neighbor] * self.spread
                nextDisease[loc] = value
        self.disease = nextDisease.copy()


import json
import argparse
import os

from . import utils as mllogger
import mlperf_logging.mllog as mllog

# map keys traditionally used in hugectr configs
# to more descriptive ones more suitable for MLPerf
hugectr_to_mlperf_layer_name = {
    'sparse_embedding1': 'embeddings',
    'fc1': 'bottom_mlp_dense1',
    'fc2': 'bottom_mlp_dense2',
    'fc3': 'bottom_mlp_dense3',
    'fc4': 'top_mlp_dense1',
    'fc5': 'top_mlp_dense2',
    'fc6': 'top_mlp_dense3',
    'fc7': 'top_mlp_dense4',
    'fc8': 'top_mlp_dense5'
}

def log_hparams(config):
    mllogger.log_event(key='eval_samples',
                       value=config['layers'][0]['eval_num_samples'])
    mllogger.log_event(key='global_batch_size',
                       value=config['solver']['batchsize'])
    mllogger.log_event(key='opt_base_learning_rate',
                       value=config['optimizer']['sgd_hparam']['learning_rate'])
    mllogger.log_event(key='sgd_opt_base_learning_rate',
                       value=config['optimizer']['sgd_hparam']['learning_rate'])
    mllogger.log_event(key='sgd_opt_learning_rate_decay_poly_power',
                       value=config['optimizer']['sgd_hparam'].get('decay_power'))
    mllogger.log_event(key='opt_learning_rate_warmup_steps',
                       value=config['optimizer']['sgd_hparam']['warmup_steps'])
    mllogger.log_event(key='opt_learning_rate_warmup_factor',
                       value=0.0)  # not configurable
    mllogger.log_event(key='lr_decay_start_steps',
                       value=config['optimizer']['sgd_hparam'].get('decay_start'))
    mllogger.log_event(key='sgd_opt_learning_rate_decay_steps',
                       value=config['optimizer']['sgd_hparam'].get('decay_steps'))
    mllogger.log_event(key='gradient_accumulation_steps',
                       value=1)  # not configurable

def log_config(config):
    # print hparams and submission info on the first node only
    if 'SLURM_NODEID' not in os.environ or os.environ['SLURM_NODEID'] == '0':
        mllogger.mlperf_submission_log('dlrm')
        log_hparams(config)

    for layer in config['layers']:
        hugectr_name = layer['name']
        if hugectr_name not in hugectr_to_mlperf_layer_name:
            # layer has no parameters, nothing to be done
            continue

        mlperf_name = hugectr_to_mlperf_layer_name[hugectr_name]
        mllogger.log_event(mllog.constants.WEIGHTS_INITIALIZATION,
                           metadata={'tensor': mlperf_name})

class LogConverter:
    def __init__(self, steps_per_epoch, start_timestamp):
        self.start_time = start_timestamp
        self.steps_per_epoch = steps_per_epoch

    def _get_log_foo(self, key):
        if '_start' in key:
            return mllogger.log_start
        if '_end' in key or '_stop' in key:
            return mllogger.log_end
        else:
            return mllogger.log_event

    def _get_value(self, data):
        if data[0] == 'eval_accuracy':
            return float(data[1])
        if data[0] == 'train_samples':
            return int(data[1])

    def _get_metadata(self, data):
        if data[0] == 'eval_accuracy':
            self._last_eval_accuracy = float(data[1])
            return { 'epoch_num': float(data[2]) + 1 }
        if 'eval' in data[0]:
            return { 'epoch_num': float(data[1]) + 1 }
        if 'epoch' in data[0]:
            return { 'epoch_num': int(data[1]) + 1 }
        if data[0] == 'run_stop':
            return { 'status': 'success' if self._last_eval_accuracy > 0.8025 else 'aborted' }

    def _get_kvm(self, data):
        key = data[0]
        if data[0] == 'init_end':
            key = 'init_stop'
        if data[0] == 'train_epoch_start':
            key = 'epoch_start'
        if data[0] == 'train_epoch_end':
            key = 'epoch_stop'

        value = self._get_value(data)
        metadata = self._get_metadata(data)

        return key, value, metadata

    def _get_time_ms(self, ms):
        return self.start_time + int(float(ms))

    def validate_event(self, event):
        try:
            float(event[0])

            if not event[1].isidentifier():
                return False

            for x in event[2:]:
                float(x)
            return True
        except:
            return False

    def log_throughput(self, line):
        """Read throughput from log file of recommendation

        Example:
            From Below line
            "Hit target accuracy AUC 0.8025 at epoch 0.95 in 22718s. Average speed 175427.7 records/s."
            Get the value -> 175427.7
        """
        lastmatch = None
        if "Hit target accuracy" in line:
            lastmatch = line
        elif "Average speed" in line:
            lastmatch = line
        throughput = 0

        if lastmatch is not None:
            throughput = float(lastmatch.split(' ')[-2])
            epoch = float(lastmatch.split(' ')[7])

        if throughput > 0:
            mllogger.log_event(key="tracked_stats",
                               value={'throughput': throughput},
                               metadata={'step': epoch})

    def log_event(self, event_log):
        if self.validate_event(event_log):
            log_foo = self._get_log_foo(event_log[1])
            key, value, metadata = self._get_kvm(event_log[1:])
            time_ms = self._get_time_ms(event_log[0])

            log_foo(key=key, value=value, metadata=metadata, time_ms=time_ms)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--log_path', type=str,
                        help='Path to the logs to be translated')

    parser.add_argument('--config_file', type=str,
                        help='HugeCTR input config file in JSON format')

    parser.add_argument('--start_timestamp', type=int,
                        help='Seconds since 1970-01-01 00:00:00 UTC at the time of training start')
    args = parser.parse_args()

    with open(args.config_file, 'r') as f:
        config = json.load(f)
    log_config(config)

    # Convert to ms to be consistent with the MLPerf logging API
    start_timestamp_ms = args.start_timestamp * 1000
    converter = LogConverter(
        steps_per_epoch=(config['layers'][0]['num_samples'] / config['solver']['batchsize']),
        start_timestamp=start_timestamp_ms,
    )

    with open(args.log_path) as f:
        log_lines = f.readlines()

    for line in log_lines:
        event_log = [x.strip() for x in line.strip().strip('][\x08 ,').split(',')]
        converter.log_event(event_log)
        converter.log_throughput(line)

if __name__ == '__main__':
    main()


# encoding: utf-8
from __future__ import unicode_literals

import sys

from tornado import ioloop
from tornado.httpclient import AsyncHTTPClient

from autopkg.poll_sms import get_sms
from daemonizer import Daemon
from .settings import KYLIN_HOST

def check_celery():
    client = AsyncHTTPClient()
    client.fetch("http://{}/celery/qsize/".format(KYLIN_HOST))

class MyDaemon(Daemon):
    def run(self):
        # p1 = ioloop.PeriodicCallback(get_sms, 1000)
        # p1.start()
        ioloop.PeriodicCallback(check_celery, 3600*1000).start()
        ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    d = MyDaemon("/tmp/kylin-daemon.pid", stdout="/tmp/my-daemon.log", stderr="/tmp/my-damon.error.log")
    if len(sys.argv) == 2:
        if sys.argv[1] == "start":
            d.start()
        elif sys.argv[1] == "stop":
            d.stop()
        elif sys.argv[1] == "restart":
            d.restart()
        else:
            print("Unknown command")
            sys.exit(2)
        sys.exit(0)
    else:
        print("Usage: %s start|stop|restart" % sys.argv[0])
        sys.exit(2)


import unittest2 as unittest
import lxml
from lxml.etree import ParserError

from zope.interface import alsoProvides
from zope.viewlet.interfaces import IViewletManager
from zope.component import queryMultiAdapter

from Products.Five.browser import BrowserView as View
from Products.CMFCore.utils import getToolByName

from emas.theme.interfaces import IEmasThemeLayer
from emas.theme import MessageFactory as _

from emas.theme.tests.base import FUNCTIONAL_TESTING

def find_viewlet(context, request, manager_name, viewlet_name, layer=None):
    if layer:
        alsoProvides(request, layer)

    view = View(context, request)
    manager = queryMultiAdapter(
        (context, request, view),
        IViewletManager,
        manager_name,
        default=None
    )
    manager.update()
    viewlets = manager.viewlets
    viewlets = [v for v in viewlets if v.__name__ == viewlet_name]
    return viewlets and viewlets[0] or None

class TestPracticeServiceMessagesViewlet(unittest.TestCase):
    """ Test the intelligent practice messages service viewlets  """

    layer = FUNCTIONAL_TESTING

    def setUp(self):
        super(TestPracticeServiceMessagesViewlet, self).setUp()
        self.setRoles(['Reader',])
        self.context = self.portal.maths
        self.request = self.portal.REQUEST
        self.manager_name = 'plone.abovecontent'
        self.themelayer = IEmasThemeLayer
        self.viewlet_name = 'emas.practice_service_messages'

    def test_viewlet_exists(self):
        viewlet = self.get_viewlet()
        self.failUnless(viewlet)
        self.failUnless(viewlet.__name__ == self.viewlet_name)

    def test_no_messages(self):
        viewlet = self.get_viewlet()
        result = viewlet.index()
        self.assertRaises(ParserError, lxml.html.fromstring, result)

    def test_noaccess_message(self):
        plone_utils = getToolByName(self.portal, 'plone_utils')
        message = _(u'You do not currently have access to this service.')
        plone_utils.addPortalMessage(message, 'services-warning')

        viewlet = self.get_viewlet()
        result = viewlet.index()

        expected_result = \
u"""\n\n
\n
You do not currently have access to this service.
\n
\n\n\n"""
        self.assertEqual(result, expected_result, 'Incorrect message returned.')

        doc = lxml.html.fromstring(result)
        self.assertEqual(len(doc.xpath('//dl')), 1, 'No content found.')

    def get_viewlet(self):
        viewlet = find_viewlet(self.context,
                               self.request,
                               self.manager_name,
                               self.viewlet_name,
                               self.themelayer)
        return viewlet


from django.conf.urls import url, include
from django.urls import path, include
from . import views

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('admin/add-playlist/', views.PlaylistCreateView.as_view(), name='playlist-create'),
    path('admin/add-cartoon/', views.CartoonCreateView.as_view(), name='cartoon-create'),
    path('admin/playlists/', views.PlaylistListView.as_view(), name='playlistlisting'),
    path('', views.CartoonListView.as_view(), name='cartoonlisting'),
    path('cartoons/<int:pk>/', views.CartoonDetailsView.as_view(), name='cartoondetails'),
    path('cartoons/<int:pk>/edit/', views.CartoonEditView.as_view(), name='cartoon-edit'),
    path('playlists/<int:pk>/', views.PlaylistDetailsView.as_view(), name='playlistdetails'),
    path('playlists/<int:pk>/edit/', views.PlaylistEditView.as_view(), name='playlist-edit'),

]