import os
from pathlib import Path
import httplib2

from datetime import datetime
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework.response import Response
from django.shortcuts import get_list_or_404, get_object_or_404
import logging

from googleapi.models import *
from googleapi.api.serializers import *
from datetime import datetime

from google.oauth2 import id_token, service_account
from google.auth.transport import requests
from apiclient import discovery

logger = logging.getLogger(__name__)

class UserAccessRecordListAPIView(APIView):
    def get(self, request, email):
        userAccessRecord = get_list_or_404(UserAccessRecord, email=email)
        serializer = UserAccessRecordSerializer(userAccessRecord, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

class UserAccessRecordAPIView(APIView):
    scopes = ["https://www.googleapis.com/auth/drive",
              "https://www.googleapis.com/auth/drive.file",
              "https://www.googleapis.com/auth/spreadsheets"]
    secretFile = os.path.join(os.getcwd(), "env", os.getenv(
        "DJANGO_GOOGLE_SERVICES_ACCOUNT_FILENAME"))
    credentials = service_account.Credentials.from_service_account_file(
        secretFile, scopes=scopes)
    service = discovery.build("sheets", "v4", credentials=credentials)
    spreadsheetID = os.getenv("DJANGO_GOOGLE_SPREADSHEET_ID")
    spreatsheetName = "UserInfoForPortal"
    spreatSheetColumnNumber = {
        "EmailAddress": {"colnumNumber": 2, "updateable": False},
        "ChineseName": {"colnumNumber": 8, "updateable": False},
        "FirstName": {"colnumNumber": 9, "updateable": False},
        "LastName": {"colnumNumber": 10, "updateable": False},
        "NickName": {"colnumNumber": 11, "updateable": False},
        "WechatID": {"colnumNumber": 13, "updateable": False},
        "TShirtSize": {"colnumNumber": 14, "updateable": False},
        "DietaryRestriction": {"colnumNumber": 15, "updateable": False},
        "ReimbursementMethod": {"colnumNumber": 16, "updateable": True},
        "ReimbursementAccountType": {"colnumNumber": 17, "updateable": True},
        "ReimbursementAccountEmail": {"colnumNumber": 18, "updateable": True},
        "ReimbursementAccountPhoneNumber": {"colnumNumber": 19, "updateable": True},
        "PhoneNumber": {"colnumNumber": 23, "updateable": True},
        "Zgid": {"colnumNumber": 32, "updateable": False},
        "ZgidEmail": {"colnumNumber": 33, "updateable": False},
        "PromoCode2020": {"colnumNumber": 34, "updateable": False}
    }

    def _checkVaildToken(self, request):
        req = requests.Request()
        id_info = id_token.verify_oauth2_token(
            request.data["id_token"], req, os.getenv('DJANGO_GOOGLE_CLIEND_ID'))
        if id_info["iss"] != "accounts.google.com" or id_info["email"] != request.data["email"]:
            return False
        return True

    def _getGoogleSheetRowNumberFromEmail(self, request):
        ranges = [
            f"{self.spreatsheetName}!B:B",
            f"{self.spreatsheetName}!AG:AG"
        ]
        serviceRequest = self.service.spreadsheets().values().batchGet(
            spreadsheetId=self.spreadsheetID, ranges=ranges, majorDimension='COLUMNS')
        try:
            response = serviceRequest.execute()
        except Exception as e:
            logger.error(e)
            return -1
        userEmail: str = request.data["email"]
        rowList: list
        if userEmail.endswith("@zgzg.io"):
            rowList = response["valueRanges"][1]["values"][0]
        else:
            rowList = response["valueRanges"][0]["values"][0]
            map(str.lower, rowList)
        return rowList.index(userEmail)+1 if userEmail in rowList else -1

    def _updateGoogleSheet(self, request):
        ret = {"returnCode": -1}
        rowNumber = self._getGoogleSheetRowNumberFromEmail(request)
        if rowNumber < 1:
            return ret
        range_ = f"{self.spreatsheetName}!A{rowNumber}:ZZ{rowNumber}"
        valueInputOption = "RAW"
        values = []
        serviceRequest = self.service.spreadsheets().values().get(
            spreadsheetId=self.spreadsheetID, range=range_)
        try:
            response = serviceRequest.execute()
        except Exception as e:
            logger.error(e)
            return ret
        responseValues = response["values"][0]
        try:
            for key in self.spreatSheetColumnNumber:
                if self.spreatSheetColumnNumber[key]["updateable"]:
                    responseValues[self.spreatSheetColumnNumber[key]
                                   ["colnumNumber"]-1] = str(request.data["data"][key])
        except Exception as e:
            print(e, self.spreatSheetColumnNumber[key]
                  ["colnumNumber"]-1, len(responseValues))
        serviceRequest = self.service.spreadsheets().values().update(
            spreadsheetId=self.spreadsheetID,
            range=f"{self.spreatsheetName}!A{rowNumber}:ZZ{rowNumber}",
            valueInputOption=valueInputOption,
            body={
                "values": [responseValues]
            })

        try:
            response = serviceRequest.execute()
        except Exception as e:
            logger.error(e)
            return False
        return True

    def _getGoogleSheetData(self, request):
        ret = {"returnCode": -1}
        rowNumber = self._getGoogleSheetRowNumberFromEmail(request)
        if rowNumber < 1:
            return ret
        range_ = f"{self.spreatsheetName}!A{rowNumber}:ZZ{rowNumber}"
        serviceRequest = self.service.spreadsheets().values().get(
            spreadsheetId=self.spreadsheetID, range=range_)
        try:
            response = serviceRequest.execute()
        except Exception as e:
            logger.error(e)
            return ret
        responseValues = response["values"][0]
        PersonEmail = responseValues[self.spreatSheetColumnNumber["EmailAddress"]["colnumNumber"]-1]
        ZgidEmail = responseValues[self.spreatSheetColumnNumber["ZgidEmail"]["colnumNumber"]-1]
        if PersonEmail == request.data["email"] or ZgidEmail == request.data["email"]:
            ret["returnCode"] = 1
            for key in self.spreatSheetColumnNumber:
                ret[key] = responseValues[self.spreatSheetColumnNumber[key]
                                          ["colnumNumber"]-1]
        return ret

    def get(self, request):
        userAccessRecord = UserAccessRecord.objects.all()
        serializer = UserAccessRecordSerializer(userAccessRecord, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def post(self, request):
        request.data["record_time"] = datetime.now()
        request.data["action"] = "View"

        serializers = UserAccessRecordSerializer(data=request.data)
        if serializers.is_valid():
            serializers.save()
            if self._checkVaildToken(request):
                ret = self._getGoogleSheetData(request)
                status_ = status.HTTP_201_CREATED if ret["returnCode"] == 1 else status.HTTP_404_NOT_FOUND
                return Response(ret, status=status_)
            return Response(status=status.HTTP_401_UNAUTHORIZED)
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)

    def put(self, request):
        request.data["record_time"] = datetime.now()
        tokenVaild = self._checkVaildToken(request)
        request.data["action"] = "Unauthorized to update profile"

        if tokenVaild:
            request.data["action"] = "Update profile: "
            ret = self._getGoogleSheetData(request)
            for key in ret:
                if ret[key] != request.data["data"][key]:
                    request.data["action"] += f"{key} from {ret[key]} to {request.data['data'][key]},"
            self._updateGoogleSheet(request)

        serializers = UserAccessRecordSerializer(data=request.data)
        if serializers.is_valid():
            serializers.save()
            if tokenVaild:
                ret = self._getGoogleSheetData(request)
                status_ = status.HTTP_201_CREATED if ret["returnCode"] == 1 else status.HTTP_404_NOT_FOUND
                return Response(ret, status=status_)
            return Response(status=status.HTTP_401_UNAUTHORIZED)
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)


valor = 0
cont = 0
for s in range(1,501,2):
    if s % 3 == 0:
        valor = valor + s
        cont = cont + 1
print(f'O valor total de todos os {cont} números Multiplos de 3 é {valor}')


import cv2
import numpy as np
import torch
import onnxruntime
import pycuda.driver as cuda
import pycuda.autoinit
import tensorrt as trt
from options.options import parser
from options.config import cfg

def inference_with_onnx(model_file, model_in):
    ort_session = onnxruntime.InferenceSession(model_file)

    # compute ONNX Runtime output prediction
    ort_inputs = {ort_session.get_inputs()[0].name: model_in}
    ort_outs = ort_session.run(None, ort_inputs)
    return ort_outs

def inference_with_engine(model_file, model_in):
    TRT_LOGGER = trt.Logger(trt.Logger.INFO)
    ort_outs = []
    with open(model_file, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime:
        engine = runtime.deserialize_cuda_engine(f.read())
        model_in = model_in.ravel()
        h_input = np.empty(trt.volume(engine.get_binding_shape(0)), np.float32)
        d_input = cuda.mem_alloc(1 * h_input.nbytes)

        if cfg.NUM_CLASSES and cfg.NUM_EGO:
            output_cls_size = engine.get_binding_shape(1)
            output_ego_size = engine.get_binding_shape(2)
            output_exist_size = engine.get_binding_shape(3)
            h_output_cls = np.empty(trt.volume(engine.get_binding_shape(1)), np.float32)
            h_output_ego = np.empty(trt.volume(engine.get_binding_shape(2)), np.float32)
            h_output_exist = np.empty(trt.volume(engine.get_binding_shape(3)), np.float32)
            d_output_cls = cuda.mem_alloc(1 * h_output_cls.nbytes)
            d_output_ego = cuda.mem_alloc(1 * h_output_ego.nbytes)
            d_output_exist = cuda.mem_alloc(1 * h_output_exist.nbytes)
            bindings = [int(d_input), int(d_output_cls), int(d_output_ego), int(d_output_exist)]
        elif cfg.NUM_CLASSES and cfg.NUM_EGO == 0:
            output_cls_size = engine.get_binding_shape(1)
            h_output_cls = np.empty(trt.volume(engine.get_binding_shape(1)), np.float32)
            d_output_cls = cuda.mem_alloc(1 * h_output_cls.nbytes)
            bindings = [int(d_input), int(d_output_cls)]
        elif cfg.NUM_CLASSES == 0 and cfg.NUM_EGO:
            output_ego_size = engine.get_binding_shape(1)
            output_exist_size = engine.get_binding_shape(2)
            h_output_ego = np.empty(trt.volume(engine.get_binding_shape(1)), np.float32)
            h_output_exist = np.empty(trt.volume(engine.get_binding_shape(2)), np.float32)
            d_output_ego = cuda.mem_alloc(1 * h_output_ego.nbytes)
            d_output_exist = cuda.mem_alloc(1 * h_output_exist.nbytes)
            bindings = [int(d_input), int(d_output_ego), int(d_output_exist)]

        stream = cuda.Stream()
        with engine.create_execution_context() as context:
            cuda.memcpy_htod_async(d_input, model_in, stream)
            context.execute_async(bindings=bindings, stream_handle=stream.handle)
            if cfg.NUM_CLASSES:
                cuda.memcpy_dtoh_async(h_output_cls, d_output_cls, stream)
            if cfg.NUM_EGO:
                cuda.memcpy_dtoh_async(h_output_ego, d_output_ego, stream)
                cuda.memcpy_dtoh_async(h_output_exist, d_output_exist, stream)
            stream.synchronize()
            if cfg.NUM_CLASSES:
                h_output_cls = h_output_cls.reshape(output_cls_size)
                ort_outs.append(h_output_cls)
            if cfg.NUM_EGO:
                h_output_ego = h_output_ego.reshape(output_ego_size)
                h_output_exist = h_output_exist.reshape(output_exist_size)
                ort_outs.append(h_output_ego)
                ort_outs.append(h_output_exist)
            return ort_outs

def inference(model_file, image_file):
    image = cv2.imread(image_file)
    h, w, c = image.shape
    image_bgr = cv2.resize(image, (cfg.LOAD_IMAGE_WIDTH, cfg.LOAD_IMAGE_HEIGHT), interpolation=cv2.INTER_NEAREST)
    image_bgr = image_bgr[cfg.VERTICAL_CROP_SIZE:, :, :]
    image_bgr = cv2.resize(image_bgr, (cfg.MODEL_INPUT_WIDTH, cfg.MODEL_INPUT_HEIGHT), interpolation=cv2.INTER_NEAREST)
    image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
  #  image_rgb = (image_rgb - cfg.INPUT_MEAN) * cfg.INPUT_STD
    image_rgb = np.transpose(image_rgb, (2, 0, 1))
    image_rgb = np.ascontiguousarray(image_rgb, dtype=np.float32)
    image_rgb = np.expand_dims(image_rgb, 0)
    ort_outs = []
    if model_file.find('onnx') != -1:
        ort_outs = inference_with_onnx(model_file, image_rgb)
        ort_out = torch.from_numpy(ort_outs[0])
        ort_out = ort_out.data.cpu().numpy()
        ort_out = np.squeeze(ort_out, 0)
    elif model_file.find('engine') != -1:
        ort_outs = inference_with_engine(model_file, image_rgb)
        ort_out = ort_outs[0]
        ort_out = np.transpose(ort_out, (2, 0, 1))
    if len(ort_outs):
        threshold_cls = int(cfg.THRESHOLD_CLS * 255)
        result_cls = np.zeros((h, w)).astype(np.uint8)
        # ort_out = F.softmax(ort_out, dim=1)

        for num in range(cfg.NUM_CLASSES-1):
            prob_map = (ort_out[num + 1] * 255).astype(np.uint8)
            map_bak = np.zeros((cfg.LOAD_IMAGE_HEIGHT, cfg.LOAD_IMAGE_WIDTH))
            map_bak[cfg.VERTICAL_CROP_SIZE:, :] = cv2.resize(prob_map, (cfg.LOAD_IMAGE_WIDTH, cfg.IN_IMAGE_H_AFTER_CROP),
                                                         interpolation=cv2.INTER_NEAREST)
            map_bak = cv2.resize(map_bak, (w, h), interpolation=cv2.INTER_NEAREST)
            result_cls[map_bak >= threshold_cls] = num + 1

        result_cls_color = np.copy(image)
        # cv2.imshow("alllane", result_cls_color)
        # cv2.waitKey()
        for i in range(h):
            for j in range(w):
                if result_cls[i, j] != 0:
                    result_cls_color[i, j, :] = cfg.EG0_POINT_COLORS[result_cls[i, j] - 1]
        # cv2.imshow("alllane", result_cls_color)
        # cv2.waitKey()
        print("src/samples/alllane.png")
        cv2.imwrite("src/samples/alllane.png", result_cls_color)

if __name__ == '__main__':
    args = parser.parse_args()
    cfg.THRESHOLD_CLS = 0.85
    cfg.MODEL_INPUT_WIDTH, cfg.MODEL_INPUT_HEIGHT = 480,224
    image_file = 'src/samples/lane_detection_l4e_input.png'
    image = cv2.imread(image_file)
    h,w,c = image.shape
    image_re = cv2.resize(image, (int(1.2*w), int(1.2*h)), interpolation=cv2.INTER_NEAREST)
    image_re1 = cv2.resize(image, (int(1.2*w), int(1.2*h)), interpolation=cv2.INTER_LINEAR)
    merged = np.concatenate([image_re, image_re1], axis=1)
    cv2.imshow("resize", merged)
    cv2.waitKey()
    inference(args.model_file, image_file)
    print("All Done")


#!/usr/bin/env python
from class1 import Person, Manager
import shelve

bob = Person("Bob Smith")  # Создание объектов для сохранения
sue = Person("Sue Jones", job="dev", pay=100000)
tom = Manager("Tom Jones", 50000)

db = shelve.open("persondb")  # Имя файла хранилища
for object in (bob, sue, tom):  # В качестве ключа использовать атрибут name
    db[object.name] = object  # Сохранить объект в хранилище
db.close()


from tkinter import *
from tkinter import filedialog
from PIL import Image
from time import time

def ouvrir(nominage):
    """
        Ouvre l'image que l'on veut traiter.

        :param nominage: Chemin de l'image.
        :type nominage: str.
        :returns: img.
    """
    img = Image.open(nominage)
    print(img.format,img.size,img.mode)
    return img

def hexa2(n):
    L=[]
    lettres = ['A','B','C','D','E','F']
    hexarep = ""
    if n==0:
        hexarep += "0"
    while n!=0:
        if n%16 < 10:
            hexarep += str(n%16)
        else:
            hexarep += lettres[n%16-16]
        n=n//16
    return(hexarep[::-1])

def dessinerGrille(ims):
    ''' dessine la grille en traçant chaque cercle un à un '''
    cv.delete(ALL)
    pixs = ims.load()
    for j in range(height):
        for i in range(width):
            n = pixs[i,j]
            hexaval = hexa2(n)
            if len(hexaval) == 1:
                hexaval = '0' + hexaval
            couleur = '#' + str(3*(hexaval))
            dessinerCase(1,j,i,couleur)
        cv.update()

def dessinerGrilleCouleur(ims):
    cv.delete(ALL)
    pixs = ims.load()
    for j in range(height):
        for i in range(width):
            hexaval = ""
            for k in range(3):
                hexa = hexa2(pixs[i,j][k])
                if len(hexa) == 1:
                    hexaval += '0' + hexa
                else:
                    hexaval += hexa
            couleur = '#' + hexaval
            dessinerCase(1,j,i,couleur)
        cv.update()

def dessinerCase(cote,j,i,couleur):
    x0,y0 = (cote)*i,(cote)*j
    x1,y1 = x0 + cote, y0 + cote
    cv.create_rectangle(x0,y0,x1,y1,fill=couleur,outline="")

imopen = filedialog.askopenfilename(title="Ouvrir une image")

ims = ouvrir(imopen)

fen = Tk()

(width,height) = ims.size

cv = Canvas(fen,height=height,width=width,bg="white")
cv.pack()

t1 = time()
if ims.mode == 'L':
    imd = Image.new('F', ims.size)
    pixs = ims.load()
    dessinerGrille(ims)
elif ims.mode == 'RGB' or ims.mode == 'RGBA':
    imd = Image.new('RGB', ims.size)
    pixs = ims.load()
    dessinerGrilleCouleur(ims)
t2 = time()

print(round(t2-t1,3))

fen.mainloop()


import copy
import networkx as nx
import matplotlib.pyplot as plt
import random
import scipy
import math

########################
### INPUT FORMATTING ###
########################

# Turns list of swaps into dictionary of rules
def genswaps(swaps):
    rulesdict = {"n":[],"z":[]}
    for swap in swaps:
        for sp in swap:
            if sp not in rulesdict.keys():
                rulesdict[sp] = []
                for rule in swaps:
                    if sp in rule:
                        rulesdict[sp] += [x for x in rule if x != sp]
    return rulesdict

# Turns string representation of state into list of lists
def genstate(strstate):
    rows = strstate.splitlines()
    statelist = []
    for row in rows:
        statelist.append(row.split())
    return statelist

##########################
### COMPUTE STATESPACE ###
##########################

# Generate list of valid neighbor indices for given state and indices
def neighbors(state, x, y):
    locs = []
    if x != 0:
        locs.append([x-1, y])
    if x != (len(state)-1):
        locs.append([x+1, y])
    if y != 0:
        locs.append([x, y-1])
    if y != (len(state[x])-1):
        locs.append([x, y+1])
    return locs

#  Generate next possible states
def next_states(state, rules, reachedstates):
    # input state as list of lists
    # input rules as dictionary where each species is a key
    # input list of reached states
    newstates = []
    for i in range(len(state)):
        for j in range(len(state[i])):
            locs = neighbors(state, i, j)
            for loc in locs:
                if state[loc[0]][loc[1]] in rules[state[i][j]]:
                    tempstate = copy.deepcopy(state)
                    tempstate[loc[0]][loc[1]], tempstate[i][j] = tempstate[i][j], tempstate[loc[0]][loc[1]]
                    if tempstate not in reachedstates:
                        reachedstates.append(tempstate)
                        newstates.append(tempstate)
    return newstates

# Returns all reachable states and count information
def statespace(state, rules):
    reachedstates = [state]
    unchecked = [state]
    edgechart = []
    while len(unchecked) != 0:
        newstates = next_states(unchecked[0], rules, reachedstates)
        unchecked += newstates
        edgechart.append(len(newstates))
        del unchecked[0]
    return reachedstates, edgechart

# Draws statespace as a graph
def sstree(state, rules, i1, i2, iv, nodesize=300):
    G = nx.Graph()
    colorsd = {}
    ustates = statespace(state, rules)[0]
    for ustate in ustates:
        G.add_node(str(ustate))
        if ustate[i1][i2] == iv:
            colorsd[str(ustate)] = 'r'
        else:
            colorsd[str(ustate)] = 'g'
    for ustate in ustates:
        nexts = next_states(ustate, rules, [])
        for n in nexts:
            G.add_edge(str(n), str(ustate))
    colorsd[str(state)] = 'b'
    colors = [colorsd[str(node)] for node in G.nodes()]
    pos = nx.spring_layout(G)
    nx.draw_networkx(G, pos, node_color=colors, with_labels=False, node_size = nodesize)
    plt.axis('off')
    plt.show()

# Determines number of states to reach given probability of correct output
def convergence(state, rules, i1, i2, iv, n):
    disti = {str(state): 1}
    i = 0
    r = 0
    while r < n and i < 10**4:
        r = 0
        distn = {}
        for ori in disti.keys():
            newstates = next_states(eval(ori), rules, [])
            for newstate in newstates:
                if str(newstate) in distn.keys():
                    distn[str(newstate)] += disti[ori]/len(newstates)
                else:
                    distn[str(newstate)] = disti[ori]/len(newstates)
        for ori in distn.keys():
            if eval(ori)[i1][i2] == iv:
                r += distn[ori]
        i += 1
        #print(sum(distn.values()))
        disti = copy.deepcopy(distn)
    return i

# Estimates hitting time for initial state and desired outputs, n is number of trials
def hitting(state, rules, i1, i2, iv, n):
    i = 0
    for j in range(n):
        curr = copy.deepcopy(state)
        while curr[i1][i2] != iv:
            curr = random.choice(next_states(curr, rules, []))
            i += 1
    return (i/n)

#######################
### PRINTING STATES ###
#######################

# Returns string representation of a state
def printstate(state):
    statestr = ""
    for i in range(len(state)):
        for sp in state[i]:
            statestr += (sp + " ")
        statestr += "\n"
    return statestr

# Returns string representation and degree information for state space
def printstatespace(state, rules):
    states = statespace(state, rules)
    statesstr = ""
    for s in states[0]:
        statesstr += (printstate(s)+"\n")
    statesstr += ("Degree of each state: " + str(states[1])+'\n')
    return statesstr

############################
### CHECKING CORRECTNESS ###
############################

# Determines values of output indices
def checkoutput(statespace, i1, i2):
    outputs = {}
    for state in statespace:
        if state[i1][i2] not in outputs:
            outputs[state[i1][i2]] = 1
        else:
            outputs[state[i1][i2]] += 1
    return outputs

# Determines all possible values and frequencies of output indices
def checkcorrect(initstates, rules, i1, i2, i3="", i4='', i5 = '', i6=''):
    outputstr = ""
    for initstate in initstates:
        outputstr += printstate(initstate)
        sspace = statespace(initstate, rules)
        outputstr += "Possible outputs and frequencies: "
        outputstr += str(checkoutput(sspace[0], i1, i2))
        if i3 != '' and i4 != '':
            print("2nd output loc checked")
            outputstr += str(checkoutput(sspace[0], i3, i4))
        if i5 != '' and i6 != '':
            print("3rd output loc checked")
            outputstr += str(checkoutput(sspace[0], i5, i6))
        outputstr += "\n Number of reachable states: "
        outputstr += (str(sum(sspace[1])+1) +'\n')
    return outputstr

###############################
### NETWORKX REPRESENTATION ###
###############################

# Determines distinct colors for each species
def colorsplit(rules):
    c_inc = (0.75/len(rules.keys()))
    c = 0.25
    colors = {}
    for sp in rules.keys():
        if sp == 'n' or sp == 'z':
            colors[sp] = (0,0,0)
        else:
            colors[sp] = (c, 1-random.random()*0.75, 1-random.random()*0.75)
            c += c_inc
    return colors

# Takes state and rules and draws configuration using networkx
def drawstate(state, rules, colorset):
    N = len(state[0])
    M = len(state)
    G = nx.grid_2d_graph(N, M)
    pos = dict((n, n) for n in G.nodes())
    labels = dict(((i, j), state[M-1-j][i]) for i, j in G.nodes())
    species = set(labels.values())
    colors = [colorset[labels[node]] for node in G.nodes()]
    edgelist = []
    for node in G.nodes():
        neighbors = G.neighbors(node)
        for neighbor in neighbors:
            if labels[neighbor] in rules[labels[node]]:
                edgelist.append((node, neighbor))
    nx.draw_networkx_nodes(G, pos = pos, node_color = colors)
    nx.draw_networkx_labels(G, pos = pos, labels = labels)
    nx.draw_networkx_edges(G, pos = pos, edgelist = edgelist)

# Simulates states for n swaps, displaying each state for k seconds,
# and gives statespace information and correctness for i1 and i2
def simulate(state, rules, n, k, i1, i2):
    colorset = colorsplit(rules)
    stateinfo = checkcorrect([state], rules, i1, i2)
    infostr = stateinfo.splitlines()[-1] + '\n' + stateinfo.splitlines()[-2]
    for i in range(n):
        plt.axis('off')
        drawstate(state, rules, colorset)
        plt.pause(k)
        state = random.choice(next_states(state, rules, []))
        plt.clf()
    plt.axis('off')
    plt.text(0.0,0.5,infostr)
    plt.show()

########################
### GATE INFORMATION ###
########################

AND_init_state_AB = [["A","B","n"], ["D","F","C"]]
AND_init_state_GH = [["G","H","n"], ["D","F","C"]]
AND_init_state_AH = [["A","H","n"], ["D","F","C"]]
AND_init_state_BH = [["B","H","n"], ["D","F","C"]]

AND_swap_list = [["A","D"],["A","B"],["G","F"],["H","C"],["G","D"],["A","C"], \
    ["G","B"],["B","F"],["G","C"],["H","F"],["G","H"]]

NAND_init_state_AB = [['n','n','n','B','n','n','n','n','n','n'],
                   ['A','B','n','p1','w','n','ap','n','A','n'],
                   ['D','F','C1','w','w','w','b3','x','x','C2'],
                   ['n','n','n','p2','w','n','gp','n','G','n'],
                   ['n','n','n','H','n','n','n','n','n','n']]
NAND_init_state_GB = [['n','n','n','B','n','n','n','n','n','n'],
                   ['G','B','n','p1','w','n','ap','n','A','n'],
                   ['D','F','C1','w','w','w','b3','x','x','C2'],
                   ['n','n','n','p2','w','n','gp','n','G','n'],
                   ['n','n','n','H','n','n','n','n','n','n']]
NAND_init_state_AH = [['n','n','n','B','n','n','n','n','n','n'],
                   ['A','H','n','p1','w','n','ap','n','A','n'],
                   ['D','F','C1','w','w','w','b3','x','x','C2'],
                   ['n','n','n','p2','w','n','gp','n','G','n'],
                   ['n','n','n','H','n','n','n','n','n','n']]
NAND_init_state_GH = [['n','n','n','B','n','n','n','n','n','n'],
                   ['G','H','n','p1','w','n','ap','n','A','n'],
                   ['D','F','C1','w','w','w','b3','x','x','C2'],
                   ['n','n','n','p2','w','n','gp','n','G','n'],
                   ['n','n','n','H','n','n','n','n','n','n']]

NAND_swap_list = [['A','D'],['G','D'],['B','F'],['H','F'],['A','B'],
                  ['A','C1'],['G','F'],['G','B'],['G','C1'],['H','C1'],
                  ['A','w'],['B','w'],['G','w'],['H','w'],
                  ['A','p1'], ['G','p2'],['B', 'b3'],['H','b3'],['ap','H'],
                  ['gp','B'],['gp','x'],['ap','x'],['A','ap'],['G','gp'],
                  ['G','C2'],['A','C2']]

Toffoli_swap_list = [['01','D'],['11','D'],['03','F'],['13','F'],['11','13'],
                     ['11','C'],['01','03'],['01','13'],['01','C'],['03','C'],
                     ['01','w0'],['03','w0'],['01','B0'],['01','B1'],['03','B0'],['03','B1'],
                     ['B0','w0p'],['B1','w0p'],['B0','02'],['B1','12'],['11','w1'],
                     ['11','A0'],['11','A1'],['A0','w1p'],['A1','w1p'],['A1','12'],['A0','02'],
                     ['A0','x'],['x','A1'],['A0','ap'],['ap','y'],['ap','1'],['A1','bp'],
                     ['bp','y'],['bp','0'],['B0','x'],['B1','x'],['B1','ap'],['B0','bp']]

Fredkin_swap_list = [['A', '03'], ['B', '13'], ['A', 'x'], ['B', 'x'], ['A', 'xA'], ['B', 'xB'], ['A', '01'], \
    ['A', '11'], ['B', '02'], ['B', '12'], ['01', 'w'], ['11', 'w'], ['02', 'w'], ['12', 'w']]

FredkinR_swap_list = [ ['13', 'B'], ['03', 'A'], ['03', 'w3'], ['13', 'w3'],
                        ['A', 'wAB'], ['B', 'wAB'], ['A', 'wA'], ['B', 'wB'] ,
                        ['01', 'w1'] , ['11', ' w1'], ['02', 'w2'], ['12', 'w2'],
                        ['A', 'w'], ['B', 'w'], ['01', 'w'], ['11', 'w'],
                        ['02', 'w'], ['12', 'w'], ['s1', 'wB1'], ['s2', 'wB2'],
                        ['s1', '01'], ['s1', '11'], ['s2', '02'], ['s2', '12'],
                        ['wB2', 'B'], ['wB2', '01'], ['wB2', '11'], ['wB1', 'B'],
                        ['wB1', '02'], ['wB1', '12'], ['02', 'A'], ['12', 'A'],
                        ['01', 'A'], ['11', 'A'], ['02', 'B'], ['12', 'B'],
                        ['01', 'B'], ['11', 'B'], ['02', '01'], ['02', '11'],
                        ['12', '01'], ['12', '11']]

Fan_swap_list = [["A", "01"], ["A", "02"], ["B", "11"], ["B", "12"],
                 ["A", "x"], ["B", "x"], ["A", "wa"], ["B", "wb"],
                 ["02", "w"], ["12", "w"], ["02", "z"], ["12", "z"],
                 ["A", "z"], ["B", "z"]]

T_init_001 = genstate("01 03 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 12 x x y y \n z z B1 z z z bp z 1")
T_init_110 = genstate("11 13 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 02 x x y y \n z z B1 z z z bp z 1")
T_init_111 = genstate("11 13 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 12 x x y y \n z z B1 z z z bp z 1")
T_init_100 = genstate("11 03 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 02 x x y y \n z z B1 z z z bp z 1")
T_init_101 = genstate("11 03 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 12 x x y y \n z z B1 z z z bp z 1")
T_init_001 = genstate("01 03 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 12 x x y y \n z z B1 z z z bp z 1")
T_init_000 = genstate("01 03 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 02 x x y y \n z z B1 z z z bp z 1")
T_init_010 = genstate("01 13 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 02 x x y y \n z z B1 z z z bp z 1")
T_init_011 = genstate("01 13 z z A0 z z z z \n D F C w1 w1 A1 z z z \n z z w0 z w1p z ap z 0 \n z B0 w0 w0p 12 x x y y \n z z B1 z z z bp z 1")

F_init_101 = genstate("z z z 11 z \n A z xA xA w \n 13 x x z w \n B z xB xB w \n z z z 02 z")
F_init_011 = genstate("z z z 01 z \n A z xA xA w\n 13 x x z w \n B z xB xB w\n z z z 12 z")
F_init_111 = genstate("z z z 11 z \n A z xA xA w \n 13 x x z w \n B z xB xB w \n z z z 12 z")
F_init_110 = genstate("z z z 11 z \n A z xA xA w \n 03 x x z w \n B z xB xB w \n z z z 12 z")
F_init_000 = genstate("z z z 01 z \n A z xA xA w \n 03 x x z w \n B z xB xB w \n z z z 02 z")
F_init_100 = genstate("z z z 11 z \n A z xA xA w \n 03 x x z w\n B z xB xB w \n z z z 02 z")
F_init_010 = genstate("z z z 01 z \n A z xA xA w \n 03 x x z w \n B z xB xB w \n z z z 12 z")
F_init_001 = genstate("z z z 01 z \n A z xA xA w \n 13 x x z w \n B z xB xB w \n z z z 02 z")
Fredkin_comp = genstate("z z z 11 z z z z z z z\n A z xA xA w z z z z z z\n 03 x x z w z z z z z z\n B z xB xB w w w w w w z\n z z z 02 z z z z z w z\n z z z z z z z z z w z\n z z z z z z A z xA xA w\n z z z z z z 13 x x z w\n z z z z z z B z xB xB w\n z z z z z z z z z 12 z")

FR_init_011 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 13 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 12 z w w w w w w w z 01 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_101 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 13 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 02 z w w w w w w w z 11 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_111 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 13 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 12 z w w w w w w w z 11 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_001 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 13 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 02 z w w w w w w w z 01 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_010 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 03 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 12 z w w w w w w w z 01 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_100 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 03 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 02 z w w w w w w w z 11 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_110 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 03 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 12 z w w w w w w w z 11 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")
FR_init_000 = genstate("z z z z z z w3 w3 w3 w3 z z z z z\n z z z z z z w3 z z w3 z z z z z\n z z z z z z A 03 B B z z z z z\n z z z z z z z wAB z z z z z z z\n z z z z z z wA wAB wB z z z z z z\n w w wA wA wA z wA z wB z wA wA wA w w\n w z 02 z w w w w w w w z 01 z w\n w1 w1 wB2 w w z z z z z w w wB1 w2 w2\n z z z s2 z z z z z z z s1 z z z")

Fan_init_1 = genstate("w z wb wb wb wb wb z w\n w n 12 n wb n 12 n w\n w n n n wb n n n w\n w n A n wb n n n w\n w n 11 x x n n n w\n w n B n wa n n n w\n w n n n wa n n n w\n w n 02 n wa n 02 n w\n w z wa wa wa wa wa z w")
Fan_init_0 = genstate("w z wb wb wb wb wb z w\n w n 12 n wb n 12 n w\n w n n n wb n n n w\n w n A n wb n n n w\n w n 01 x x n n n w\n w n B n wa n n n w\n w n n n wa n n n w\n w n 02 n wa n 02 n w\n w z wa wa wa wa wa z w")

F_inputs = [F_init_000, F_init_100, F_init_010, F_init_001, F_init_110, F_init_101, F_init_011, F_init_111]
T_inputs = [T_init_000, T_init_100, T_init_110, T_init_111, T_init_010, T_init_001, T_init_011, T_init_101]
NAND_inputs = [NAND_init_state_AB, NAND_init_state_AH, NAND_init_state_GB, NAND_init_state_GH]
AND_inputs = [AND_init_state_AB,AND_init_state_GH,AND_init_state_AH,AND_init_state_GH]
FR_inputs = [FR_init_000, FR_init_100, FR_init_010, FR_init_001, FR_init_110, FR_init_011, FR_init_101, FR_init_111]

Fredkin_swaps = genswaps(Fredkin_swap_list)
AND_swaps = genswaps(AND_swap_list)
NAND_swaps = genswaps(NAND_swap_list)
Toffoli_swaps = genswaps(Toffoli_swap_list)
FredkinR_swaps = genswaps(FredkinR_swap_list)
Fan_swaps = genswaps(Fan_swap_list)

###################
### COMPUTATION ###
###################
# print(hitting(Fredkin_comp, Fredkin_swaps, 8, 10, '12', 100))
# sstree(Fredkin_comp, Fredkin_swaps, 8, 10, '12', nodesize=75)
# print(checkcorrect([Fredkin_comp], Fredkin_swaps, 8, 10))
# print(checkcorrect(F_inputs, Fredkin_swaps, 2, 4))
# print(checkcorrect(FR_inputs, FredkinR_swaps, 6, 0, 0, 8, 6, 14))
# print(hitting(F_init_001, Fredkin_swaps, 2, 4, '01', 10000))
# print(convergence(F_init_000, Fredkin_swaps, 2, 4, '01', 0.2))
# print(convergence(T_init_001, Toffoli_swaps, 3, 8, '1', 0.3))
# sstree(T_init_000, Toffoli_swaps, 3, 8, '0', nodesize=50)
# sstree(F_init_000, Fredkin_swaps, 2, 4, '01')
# print(checkcorrect(F_inputs, Fredkin_swaps, 2, 4))
# simulate(F_init_000, Fredkin_swaps, 2000, 0.2, 2, 4)
# drawstate(T_init_000, Toffoli_swaps, colorsplit(Toffoli_swaps))
# print(checkcorrect(AND_inputs, AND_swaps, 1, 2))
# print(checkcorrect([NAND_init_state_AH], NAND_swaps, 2, 9))
# print(checkcorrect(T_inputs,Toffoli_swaps, 3, 8))


#!/usr/bin/env python3
import vapoursynth as vs
import sys
import os
from timeit import default_timer

class Timer(object):
    def __init__(self, verbose=False):
        self.verbose = verbose
        self.timer = default_timer

    def __enter__(self):
        self.start = self.timer()
        return self

    def __exit__(self, *args):
        end = self.timer()
        self.elapsed_secs = end - self.start
        self.elapsed = self.elapsed_secs * 1000  # millisecs
        if self.verbose:
            print('elapsed time: %f ms' % self.elapsed)

num_threads = 1
core = vs.get_core(threads=num_threads)
core.set_max_cache_size(256)
num_frames = 1000
gpu = False
num_extra_iterations = 1

if gpu:
    clip = core.std.BlankClip(format=vs.YUV420P8, width=1920, height=1080, color=[69, 242, 115], length=num_frames, gpu=1)
else:
    clip = core.std.BlankClip(format=vs.YUV420P8, width=1920, height=1080, color=[69, 242, 115], length=num_frames)

with Timer() as target:

    if gpu:
        clip = core.std.TransferFrame(clip, 0)

    with open(os.devnull, 'w') as f:
        clip.output(f)

print("With extra_iterations = " + str(num_extra_iterations) + " gpu = " + str(gpu) + " threads = " + str(num_threads) + " elapsed time %f ms. Est. FPS = %f" % (target.elapsed, (num_frames / target.elapsed_secs)))

# ret.output(sys.stdout, y4m=True)


# coding: utf-8

# In[1]:

import imp
import matplotlib.pyplot as plt
import numpy as np
ctrl = imp.load_package('control', r'C:\Users\gonmo\PycharmProjects\python-control-GDM\control')

# # Simple example that is not OK

# In[2]:

sys1 = ctrl.tf([1, 1], [1, 10, 1])
print(sys1)

# In[3]:

r, k = ctrl.rlocus(sys1)
plt.show()
r, k = ctrl.rlocus(sys1, grid=True)

# However, when I plot the grid the figure looks not so good. The problem are the axis limits. If I redefine the y-axis limits the problem is solved.

# In[4]:

r, k = ctrl.rlocus(sys1, grid=True, ylim=[-10, 10])

# # Excercises from Franklyn

# I gonna plot several root locus in order to test the quality of the plots.

# In[5]:

sys1 = ctrl.tf([1,2],np.poly([0,-1,-5,-10]))
r1,k1=ctrl.rlocus(sys1)
plt.show()
r1,k1=ctrl.rlocus(sys1, grid = True)

# In[6]:

sys2 = ctrl.tf([1],np.poly([0,-1,-5,-10]))
r2,k2=ctrl.rlocus(sys2)
plt.show()
r2,k2=ctrl.rlocus(sys2, grid = True)

# In[7]:

sys3 = ctrl.tf(np.poly([-2,-6]),np.poly([0,-1,-5,-10]))
r3,k3=ctrl.rlocus(sys3)
plt.show()
r3,k3=ctrl.rlocus(sys3, grid = True)

# In[8]:

sys4 = ctrl.tf(np.poly([-2,-4]),np.poly([0,-1,-5,-10]))
r4,k4=ctrl.rlocus(sys4)
plt.show()
r4,k4=ctrl.rlocus(sys4, grid = True)

# In[9]:

sys5 = ctrl.tf([1],np.poly(np.roots([1,3,10])))
r5,k5=ctrl.rlocus(sys5)
plt.show()
r5,k5=ctrl.rlocus(sys5, grid = True)

# Here the plot show problems. This is beacuase the axis limits are not similar.
# I can resolve his problem redefinig the x limits axis.

# In[10]:

r5,k5=ctrl.rlocus(sys5, grid = True, xlim=[-5,0.1])

# In[11]:

sys6 = ctrl.tf([1],np.poly(np.hstack((np.roots([1,3,10]),[0]))))
r6,k6=ctrl.rlocus(sys6)
plt.show()
r6,k6=ctrl.rlocus(sys6, grid = True)

# In[12]:

sys7 = ctrl.tf(np.poly(np.roots([1,2,8])),np.poly(np.hstack((np.roots([1,2,10]),[0]))))
r7,k7=ctrl.rlocus(sys7)
plt.show()
r7,k7=ctrl.rlocus(sys7, grid = True)

# In[13]:

sys8 = ctrl.tf([1,0,1],[1,0,4,0])
r8,k8=ctrl.rlocus(sys8)
plt.show()
r8,k8=ctrl.rlocus(sys8, grid = True)

# In[14]:

sys9 = ctrl.tf(np.poly(np.roots([1,2,12])),np.poly(np.hstack((np.roots([1,2,10]),[0]))))
r9,k9=ctrl.rlocus(sys9)
plt.show()
r9,k9=ctrl.rlocus(sys9, grid = True)

# In[15]:

sys10 = ctrl.tf([1,0,4],[1,0,1,0])
r10,k10=ctrl.rlocus(sys10)
plt.show()
r10,k10=ctrl.rlocus(sys10, grid = True)

# # Simple examples that can cause problems

# In[16]:

sys11 = ctrl.tf([1, 0, -4], [1, 0, -1])
r11,k11=ctrl.rlocus(sys11)
plt.show()
r11,k11=ctrl.rlocus(sys11, grid = True)

# In[17]:

sys12 = ctrl.tf([1, 1], [1, 0])
r12,k12=ctrl.rlocus(sys12)
plt.show()
r12,k12=ctrl.rlocus(sys12, grid = True)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import copy
import itertools

def get_maximums(numbers):
	liste_finale = []
	for listes in numbers:
		liste_ordre = sorted(listes, reverse=True)
		liste_finale += liste_ordre[0:1]

	return [liste_finale]

def join_integers(numbers):
	string_nbr = ""
	for elem in numbers:
		string_nbr += str(elem)
	return int(string_nbr)

def generate_prime_numbers(limit):
	premiers = []
	nombres = list(range(2, limit + 1))
	while len(nombres) != 0:
		premiers.append(nombres[0])
		nombres = [elem for elem in nombres if elem % nombres[0] != 0]

	return premiers

def combine_strings_and_numbers(strings, num_combinations, excluded_multiples):
	return [""]

if __name__ == "__main__":
	print(get_maximums([[1,2,3], [6,5,4], [10,11,12], [8,9,7]]))
	print("")
	print(join_integers([111, 222, 333]))
	print(join_integers([69, 420]))
	print("")
	print(generate_prime_numbers(77))
	print("")
	print(combine_strings_and_numbers(["A", "B"], 2, None))
	print(combine_strings_and_numbers(["A", "B"], 5, 2))


import requests
from bs4 import BeautifulSoup
import datetime
import json, time

borrowerID = list(set([8962, 6403, 3587, 7825, 5267, 7847, 7858, 7738, 7745, 6728, 7760, 7761, 8145, 7635, 8530, 4310, 7256, 7654, 6630, 4075, 7150, 8053, 6775, 8130, 7390, 2047, 3812, 7837, 6276, 8078, 7837, 8327, 8310, 8155, 5360, 8602, 8698, 8763, 8810, 5021, 8310, 8896, 8895, 8435, 7905, 8896, 8602, 7637]))
# borrowerID = [5383]
entireDataList = []
list(set(borrowerID))
#{"username":username,"Loans": [{"lender":x,"borrower":x,"loanAmount":x,"dateLent":x,"datePaid":x, "status":x}] }
userNum = 0
print("There are {} total loans".format(len(borrowerID)))
for userID in borrowerID:
	userNum += 1
	print("Starting Loan {} out of {}".format(userNum,len(borrowerID)))
	extractedUserData = {}
	extractedUserData['username'] = ""
	extractedUserData['loans'] = []
	offset = 0
	while True: # loop to keep increasing pages
		url = "https://redditloans.com/mobile_query.php?checkid={}&limit=25&offset={}".format(userID, offset)
		r = requests.get(url)
		soup = BeautifulSoup(r.text, 'html.parser')
		loans = soup.find_all('tr')
		if len(loans) > 1: # If there are loans on this page continue
			for loan in loans: # iterate through all loans
				dataColumns = loan.find_all('td')
				if len(dataColumns) > 0: # if 'loan' is actually a loan
					loanTuple = {}
					loanTuple['lender'] = dataColumns[2].text
					loanTuple['borrower'] = dataColumns[4].text
					loanTuple['loanAmount'] = dataColumns[5].text
					if dataColumns[7].text == "Yes":
						loanTuple['status'] = 0
					else:
						loanTuple['status'] = 2
					loanTuple['dateLent'] = dataColumns[8].text
					try:
						loanTuple['datePaid'] = dataColumns[9].text
					except: # no date of repayment
						loanTuple['status'] = 1
						loanTuple['datePaid'] = 0

					extractedUserData['loans'].append(loanTuple)

			offset += 25
		else:
			break
	extractedUserData['username'] = loanTuple['borrower']
	entireDataList.append(extractedUserData)
	time.sleep(2)

with open("scrapedData1.txt", "a") as theFile:
	theFile.write(str(entireDataList))


# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 paho.mqtt.publish as publish

from modules.event.instance.status.events import *
from modules.util.log import *
from modules.util import cartridgeagentutils
import healthstats
import constants
from config import Config

log = LogFactory().get_log(__name__)

started = False
activated = False
ready_to_shutdown = False
maintenance = False

publishers = {}
""" :type : dict[str, EventPublisher] """

def publish_instance_started_event():
    global started, log
    if not started:
        log.info("Publishing instance started event")

        application_id = Config.application_id
        service_name = Config.service_name
        cluster_id = Config.cluster_id
        member_id = Config.member_id
        instance_id = Config.instance_id
        cluster_instance_id = Config.cluster_instance_id
        network_partition_id = Config.network_partition_id
        partition_id = Config.partition_id

        instance_started_event = InstanceStartedEvent(
            application_id,
            service_name,
            cluster_id,
            cluster_instance_id,
            member_id,
            instance_id,
            network_partition_id,
            partition_id)

        publisher = get_publisher(constants.INSTANCE_STATUS_TOPIC + constants.INSTANCE_STARTED_EVENT)
        publisher.publish(instance_started_event)
        started = True
        log.info("Instance started event published")
    else:
        log.warn("Instance already started")

def publish_instance_activated_event(health_stat_plugin):
    global activated, log
    if not activated:
        # Wait for all ports to be active
        listen_address = Config.listen_address
        configuration_ports = Config.ports
        ports_active = cartridgeagentutils.wait_until_ports_active(
            listen_address,
            configuration_ports,
            int(Config.read_property("port.check.timeout", critical=False)))

        if ports_active:
            log.info("Publishing instance activated event")
            service_name = Config.service_name
            cluster_id = Config.cluster_id
            member_id = Config.member_id
            instance_id = Config.instance_id
            cluster_instance_id = Config.cluster_instance_id
            network_partition_id = Config.network_partition_id
            partition_id = Config.partition_id

            instance_activated_event = InstanceActivatedEvent(
                service_name,
                cluster_id,
                cluster_instance_id,
                member_id,
                instance_id,
                network_partition_id,
                partition_id)

            publisher = get_publisher(constants.INSTANCE_STATUS_TOPIC + constants.INSTANCE_ACTIVATED_EVENT)
            publisher.publish(instance_activated_event)

            log.info("Instance activated event published")
            log.info("Starting health statistics notifier")

            health_stat_publishing_enabled = Config.read_property(constants.CEP_PUBLISHER_ENABLED, True)

            if health_stat_publishing_enabled:
                interval_default = 15  # seconds
                interval = Config.read_property("stats.notifier.interval", False)
                if interval is not None and len(interval) > 0:
                    try:
                        interval = int(interval)
                    except ValueError:
                        interval = interval_default
                else:
                    interval = interval_default
                health_stats_publisher = healthstats.HealthStatisticsPublisherManager(interval, health_stat_plugin)
                log.info("Starting Health statistics publisher with interval %r" % interval)
                health_stats_publisher.start()
            else:
                log.warn("Statistics publisher is disabled")

            activated = True
            log.info("Health statistics notifier started")
        else:
            log.error("Ports activation timed out. Aborting InstanceActivatedEvent publishing [IPAddress] %s [Ports] %s"
                      % (listen_address, configuration_ports))
    else:
        log.warn("Instance already activated")

def publish_maintenance_mode_event():
    global maintenance, log
    if not maintenance:
        log.info("Publishing instance maintenance mode event")

        service_name = Config.service_name
        cluster_id = Config.cluster_id
        member_id = Config.member_id
        instance_id = Config.instance_id
        cluster_instance_id = Config.cluster_instance_id
        network_partition_id = Config.network_partition_id
        partition_id = Config.partition_id

        instance_maintenance_mode_event = InstanceMaintenanceModeEvent(
            service_name,
            cluster_id,
            cluster_instance_id,
            member_id,
            instance_id,
            network_partition_id,
            partition_id)

        publisher = get_publisher(constants.INSTANCE_STATUS_TOPIC + constants.INSTANCE_MAINTENANCE_MODE_EVENT)
        publisher.publish(instance_maintenance_mode_event)

        maintenance = True
        log.info("Instance Maintenance mode event published")
    else:
        log.warn("Instance already in a Maintenance mode...")

def publish_instance_ready_to_shutdown_event():
    global ready_to_shutdown, log
    if not ready_to_shutdown:
        log.info("Publishing instance activated event")

        service_name = Config.service_name
        cluster_id = Config.cluster_id
        member_id = Config.member_id
        instance_id = Config.instance_id
        cluster_instance_id = Config.cluster_instance_id
        network_partition_id = Config.network_partition_id
        partition_id = Config.partition_id

        instance_shutdown_event = InstanceReadyToShutdownEvent(
            service_name,
            cluster_id,
            cluster_instance_id,
            member_id,
            instance_id,
            network_partition_id,
            partition_id)

        publisher = get_publisher(constants.INSTANCE_STATUS_TOPIC + constants.INSTANCE_READY_TO_SHUTDOWN_EVENT)
        publisher.publish(instance_shutdown_event)

        ready_to_shutdown = True
        log.info("Instance ReadyToShutDown event published")
    else:
        log.warn("Instance already in a ReadyToShutDown event...")

def get_publisher(topic):
    if topic not in publishers:
        publishers[topic] = EventPublisher(topic)

    return publishers[topic]

class EventPublisher:
    """
    Handles publishing events to topics to the provided message broker
    """
    def __init__(self, topic):
        self.__topic = topic

    def publish(self, event):
        mb_ip = Config.read_property(constants.MB_IP)
        mb_port = Config.read_property(constants.MB_PORT)
        payload = event.to_json()
        publish.single(self.__topic, payload, hostname=mb_ip, port=mb_port)


import csv, sys, os

csvFileOpen = open('/Users/siup/Downloads/gcal.csv')
csvFileRead = csv.reader(csvFileOpen)
csvFileList = list(csvFileRead)

gam = "/Users/siup/bin/gam/gam"
i = 1

while i < len(csvFileList):
    os.system(gam + " create resource " + csvFileList[i][0] + csvFileList[i][1] + " description " + csvFileList[i][2] + " type " +  csvFileList[i][3])
    i +=1


#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i-1]-h[i]))
        if i < N-1:
            dp[i+2] = min(dp[i+2],dp[i]+abs(h[i-1]-h[i+1]))
    print(dp[N])

if __name__ == "__main__":
    main()


import numpy as np
import matplotlib
from matplotlib import pyplot as plt

def get_memory_len(kernel_array, ratio):
    """
    Return the number of time bins until kernel_array has decreased by the factor 'ratio'
    'kernel_array' may initially rise, but must be monotonically decreasing from
    the maximum.
    """
    arr = (kernel_array - ratio*kernel_array.max())[::-1]
        # The point where this array reaches zero is the desired memory time
        # We flip the order with [::-1] because np.searchsorted expects increasing order
    memory_len = len(kernel_array) - np.searchsorted(arr, 0)
    return memory_len

def find_first_spike(V, threshold):
    # Based on the equivalent code to py_find_1st (https://github.com/roebel/py_find_1st)
    ind = np.flatnonzero(V >= threshold)
    if len(ind):
        return ind[0]
    else:
        return -1

def calculate_Vt(unweighted_input, theta, omega):
    spike_time = []
    datalen = unweighted_input.shape[1]
    V = (omega[:,np.newaxis] * unweighted_input).sum(axis=0)
    ref_kernel = np.exp(- np.arange(1000) / 20) #tau_mem = 20
    ref_memory_len = get_memory_len(ref_kernel, ratio=0.001)
    new_ref_kernel = ref_kernel[:ref_memory_len]
    done = False
    while not done:
        spike_idx = find_first_spike(V, theta)
        if spike_idx == -1:
            done = True
        else:
            spike_time.append(spike_idx)
            mem_len = min(ref_memory_len, datalen - spike_idx)
            V[spike_idx:spike_idx+mem_len] -= theta * new_ref_kernel[:mem_len]
    return V, spike_time

def plt_V(in_data, omega, cycle):
    input_data = np.load(in_data)
    data, presyn_input, markers, n_fea_occur, fea_time, fea_order = input_data['arr_0'], input_data['arr_1'], input_data['arr_2'], input_data['arr_3'], input_data['arr_4'], input_data['arr_5']

    if cycle != None:
        updated_omega = np.load(omega)[cycle]
    else:
        updated_omega = np.load(omega)
    time = np.arange(0, len(presyn_input[1]), 1)
    V_t, spike_time = calculate_Vt(presyn_input, 1, updated_omega)

    matplotlib.rcParams['figure.figsize'] = (15.0, 3.5)
    plt.xlabel("Time (ms)", fontsize=10)
    plt.ylabel("Voltage", fontsize=10)
    plt.plot(time, V_t)
    for marker in markers:
        plt.gca().add_patch(marker)
    plt.ylim((0, 1.2))

def multi_Vplot(row, col, data, omega, cycle, title, fig_width, fig_height):
    fig, axs = plt.subplots(row, col, figsize=(fig_width, fig_height))

    for n in range(len(cycle)):
        axs = plt.subplot(row, col, n+1)
        plt_V(data, omega[n], cycle[n])
        axs.set_title(title[n], fontsize = 15)

    fig.tight_layout()


# This should be the basic replica of the 'cp' command
# If ran from the command line without arguments
# It should print out the usage:
# copy [source] [destination]
# When just one argument is provided print out
# No destination provided
# When both arguments provided and the source is a file
# Read all contents from it and write it to the destination

import sys

def inputs():
    try:
        if len(sys.argv) == 1:
            print('copy [source] [destination]')
        elif len(sys.argv) == 3:
            print('No destination provided')
        else:
            copy_paste()

    except FileNotFoundError:
        print('File not found.')

def copy_paste():
    with open(sys.argv[2]) as file_from:
        with open(sys.argv[3]) as file_to:
            for line in file_from:
                file_to.write(line)

inputs()


def get_vat(payment, percent=18):
    try:
        payment = float(payment)
        vat = payment / 100 * percent
        vat = round(vat,2)
        return "VAT summ is {}".format(vat )
    except (TypeError, ValueError):
        return "Incorrect input type!"

result = get_vat(76)
print(result)

result = get_vat(76,12)
print(result)


"""Extensions around mlflow to track experiments.
"""
import bz2
import contextlib
import json
import logging
import os.path
import tempfile
import time

from mlflow import (
    active_run,
    log_metric,
    log_artifacts,
    log_artifact,
    log_param,
    set_tag,
)

__all__ = [
    # alias standard mlflow names
    "active_run",
    "log_artifact",
    "log_artifacts",
    "log_param",
    "log_metric",
    "set_tag",
    # mlflow extensions
    "delete_experiment",
    "get_all_infos",
    "get_all_infos_df",
    "get_all_metrics",
    "get_all_params",
    "get_artifact_repository",
    "get_infos",
    "get_metric",
    "get_metric_history",
    "get_param",
    "get_store",
    "get_structured",
    "get_tempdir",
    "get_trash_folder",
    "get_run",
    "has_active_run",
    "is_local_artifact_respository",
    "list_artifacts",
    "list_all_runs",
    "log_all_metrics",
    "log_all_params",
    "log_structured",
    "log_tempdir",
    "new_run",
    "open_artifact",
    "restore_experiment",
    "set_all_tags",
]
_logger = logging.getLogger()

def get_store(uri=None):
    from mlflow import get_tracking_uri
    from mlflow.store.file_store import FileStore

    if uri is None:
        uri = get_tracking_uri()

    return FileStore(uri)

def get_artifact_repository(run_uuid, store=None):
    from mlflow.store.artifact_repo import ArtifactRepository

    store = _ensure_store(store)
    run = store.get_run(run_uuid)
    return ArtifactRepository.from_artifact_uri(run.info.artifact_uri, store)

def is_local_artifact_respository(artifact_repo):
    from mlflow.store.local_artifact_repo import LocalArtifactRepository

    return isinstance(artifact_repo, LocalArtifactRepository)

def list_all_runs(store=None):
    """Get the ids of all runs"""
    store = _ensure_store(store)

    return [
        run_info.run_uuid
        for experiment in store.list_experiments()
        for run_info in store.list_run_infos(experiment.experiment_id)
    ]

@contextlib.contextmanager
def new_run(**kwargs):
    import mlflow

    with mlflow.start_run(**kwargs) as run, tempfile.TemporaryDirectory() as tmpdir:
        _logger.info("start run %s", run.info.run_uuid)
        run.tmpdir = tmpdir

        try:
            yield run

        finally:
            log_artifacts(tmpdir)

def log_tempdir():
    run = active_run()
    assert run is not None
    log_artifacts(run.tmpdir)

def has_active_run():
    return active_run() is not None

@contextlib.contextmanager
def open_artifact(run_uuid, artifact_path, mode="rt", *, open=open, store=None):
    artifact_repo = get_artifact_repository(run_uuid, store)
    assert is_local_artifact_respository(
        artifact_repo
    ), "only works with local artifacts"

    local_path = artifact_repo.download_artifacts(artifact_path)
    with open(local_path, mode) as fobj:
        yield fobj

    # TODO: delete local path for non local repos

def list_artifacts(run_uuid, path=".", store=None):
    artifact_repo = get_artifact_repository(run_uuid, store=store)
    return artifact_repo.list_artifacts(path)

def log_fobj(fobj, fname):
    """Log the contents as fobj as an artifact."""
    if hasattr(fobj, "encoding"):
        mode = "wt"

    else:
        mode = "wb"

    with tempfile.TemporaryDirectory() as tempdir:
        target_fname = os.path.join(tempdir, fname)
        with open(target_fname, mode) as target:
            while True:
                chunk = fobj.read(1024 * 1024)
                if len(chunk) == 0:
                    break

                target.write(chunk)

        log_artifact(target_fname)

def log_structured(**values):
    """Log a stream of structured values to an artifact inside mlflow.
    """
    tempdir = get_tempdir()

    # work around possible overflows by using strings during storage
    values = dict(values, timestamp=str(int(1000 * time.time())))

    with bz2.open(os.path.join(tempdir, "structured_log.bz2"), "at") as fobj:
        fobj.write(json.dumps(values))
        fobj.write("\n")

def get_structured(run_uuid, store=None):
    with open_artifact(
        run_uuid, "structured_log.bz2", open=bz2.open, store=store
    ) as fobj:
        # convert back the string representation of timestamps
        return [
            dict(d, timestamp=int(d["timestamp"]))
            for d in (json.loads(line) for line in fobj)
        ]

def get_tempdir():
    """Get a tempdir, after the run it will be logged as an artifact."""
    run = active_run()

    if run is None:
        raise RuntimeError("no active run")

    if not hasattr(run, "tmpdir"):
        raise RuntimeError("Run has no configured tmpdir")

    return run.tmpdir

def active_run_uuid():
    run = active_run()
    return None if run is None else run.info.run_uuid

def get_all_infos_df():
    import pandas as pd

    infos = pd.DataFrame(get_all_infos())

    infos.columns = pd.MultiIndex.from_tuples(infos.columns)
    infos["run", "start_time"] = pd.to_datetime(infos["run", "start_time"], unit="ms")
    infos["run", "end_time"] = pd.to_datetime(infos["run", "end_time"], unit="ms")
    infos["run", "duration"] = pd.to_timedelta(infos["run", "duration"], unit="ms")

    return infos

def get_all_infos(store=None):
    return [get_infos(run_uuid, store=store) for run_uuid in list_all_runs(store=store)]

def get_infos(run_uuid, store=None):
    from mlflow.entities import RunStatus

    run = get_run(run_uuid, store=store)

    if run.info.end_time is None:
        duration = None

    else:
        duration = run.info.end_time - run.info.start_time

    return {
        ("run", "uuid"): run.info.run_uuid,
        ("run", "experiment_id"): run.info.experiment_id,
        ("run", "status"): RunStatus.to_string(run.info.status),
        ("run", "start_time"): run.info.start_time,
        ("run", "end_time"): run.info.end_time,
        ("run", "duration"): duration,
        **{("metric", m.key): m.value for m in get_all_metrics(run_uuid, store=store)},
        **{("param", p.key): p.value for p in get_all_params(run_uuid, store=store)},
    }

def log_all_metrics(**metrics):
    _kv_apply(log_metric, metrics)

def log_all_params(**params):
    _kv_apply(log_param, params)

def set_all_tags(**tags):
    _kv_apply(set_tag, tags)

def _kv_apply(func, mapping):
    for k, v in mapping.items():
        func(k, v)

def get_run(run_uuid, store=None):
    return _ensure_store(store).get_run(run_uuid)

def get_all_metrics(run_uuid, store=None):
    return _ensure_store(store).get_all_metrics(run_uuid)

def get_all_params(run_uuid, store=None):
    return _ensure_store(store).get_all_params(run_uuid)

def get_metric(run_uuid, metric_key, store=None):
    return _ensure_store(store).get_metric(run_uuid, metric_key)

def get_metric_history(run_uuid, metric_key, store=None):
    return _ensure_store(store).get_metric_history(run_uuid, metric_key)

def get_param(run_uuid, param_key, store=None):
    return _ensure_store(store).get_param(run_uuid, param_key)

def delete_experiment(run_uuid, store=None):
    return _ensure_store(store).delete_experiment(run_uuid)

def restore_experiment(run_uuid, store=None):
    return _ensure_store(store).restore_experiment(store)

def get_trash_folder(store=None):
    return _ensure_store(store).trash_folder

def _ensure_store(store):
    return store if store is not None else get_store()


from meshingv2 import *
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import pylab

import matplotlib
import matplotlib.pyplot as pyplot
import matplotlib.tri as tri
#matplotlib.rcParams['text.usetex'] = True
#from matplotlib import rc
#rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
#rc('text', usetex=True)

hstep = 1/16
threshold = 0.05 * hstep

print('-----------------------------------------------------')

def cos_times_sin(p):               # the level-set function
    (x,y) = p
    (a,b) = (1.5,0.5)
    if isinstance(x,list) or isinstance(x,np.ndarray):
        f = []
        for i,xval in enumerate(x):
            f.append( np.cos(a*np.pi*xval)*np.sin(b*np.pi*y[i]) )
        return f
    else:
        return np.cos(a*np.pi*x)*np.sin(b*np.pi*y)

def linear(p):
    (x,y)   = p
    (a,b,c) = (3,2,1)
    if isinstance(x,list) or isinstance(x,np.ndarray):
        f = []
        for i,xval in enumerate(x):
            f.append( a*xval+b*y[i]+c )
        return f
    else:
        return a*x+b*y+c

def circle(p):
    (x,y) = p
    (a,b) = (1.1,1.3)
    if isinstance(x,list) or isinstance(x,np.ndarray):
        f = []
        for i,xval in enumerate(x):
            f.append( (xval/a)**2+(y[i]/b)**2-1 )
        return f
    else:
        return (x/a)**2+(y/b)**2-1

def star(p):
    (x,y) = p
    if isinstance(x,list) or isinstance(x,np.ndarray):
        f = []
        for i in range(len(x)):
            if x[i] == 0:
                if y[i] > 0:
                    angle = np.pi/2
                else:
                    angle = -np.pi/2
            else:
                angle = np.arctan(y[i]/x[i])
            f.append( (3./5) + (1./4)*np.cos(6*angle)**2 - (x[i]**2+y[i]**2) )
        return f
    else:
        if x == 0:
            if y > 0:
                angle = np.pi/2
            else:
                angle = -np.pi/2
        else:
            angle = np.arctan(y/x)
        return ( (3./5) + (1./4)*np.cos(6*angle)**2 - (x**2+y**2) )

def dumbbell(p):
    (x,y)      = p
    (ax,ay)    = (-0.5,0)
    (bx,by)    = (0.5,0)
    (c1,c2,c3) = (0.3,0.25,0.1)

    if isinstance(x,list) or isinstance(x,np.ndarray):
        f = []
        for i in range(len(x)):
            dxa = np.sqrt( (ax-x[i])**2+(ay-y[i])**2 )
            dxb = np.sqrt( (bx-x[i])**2+(by-y[i])**2 )
            if ax<x[i] and x[i]<bx:
                f.append(max( c1-dxa, c2-dxb,c3-abs(y[i]) ))
            else:
                f.append(max( c1-dxa, c2-dxb ))
        return f
    else:
        dxa = np.sqrt( (ax-x)**2+(ay-y)**2 )
        dxb = np.sqrt( (bx-x)**2+(by-y)**2 )
        if ax<x and x<bx:
            return max( c1-dxa, c2-dxb, c3-abs(y) )
        else:
            return max( c1-dxa, c2-dxb )

def qualityplot(data,names):
    ind = np.arange(len(data))
    width = len(data)*(0.35/5)

    for j in range(len(names)):
        fig = pyplot.figure(figsize=(7,7))
        for i in range(len(data)):
            p1 = pyplot.bar(i-width/2, data[i][1][j], width, color='xkcd:salmon')
            p2 = pyplot.bar(i+width/2, data[i][2][j], width, color='xkcd:light blue')
        pyplot.title(names[j])
        xticks = []
        for i in range(len(data)):
            xticks.append(data[i][0].replace("_"," "))
        pyplot.xticks(ind,xticks)
        pyplot.ylabel(names[j])
        pyplot.legend((p1[0],p2[0]),('Before','After'))
        string = 'Saved_figures/' + str(names[j]).replace(" ", "_") + '.png'
        fig.savefig(string, dpi=fig.dpi, bbox_inches = "tight")
    return

def simplesol(z,thresh, newtriang = False):
    m = mesh(hstep,thresh)
    m.initzeroedgesinfo(z)
    m.updatezeroelementinfo(z)
    m.findzero(z)
    m.plotzeroz('Saved_figures/' + z.__name__ + '_mesh.png')
    m.plotz(z)
    pyplot.close()

    [avgskewness, maxskewness, stdskewness] = m.skewness()
    [minsize, maxsize, stdsize] = m.sizes()
    before = [avgskewness, maxskewness, stdskewness, minsize, maxsize, stdsize]

    m.simplegridtozeropoint(z,thresh)

    if newtriang:
        m.newtriang()
        m.updatezeroelementinfo(z)

    m.findzero(z)

    [avgskewness, maxskewness, stdskewness] = m.skewness()
    [minsize, maxsize, stdsize] = m.sizes()
    after = [avgskewness, maxskewness, stdskewness, minsize, maxsize, stdsize]

    m.plotzeroz('Saved_figures/' + z.__name__ + '_adjmesh.png')
    return [before, after]

def simplemeshfit(farr,thresh, newtriang = True):
    print('hstep = '+str(hstep))
    if isinstance(farr,list):
        soldata = []
        for i in range(len(farr)):
            soldata.append([farr[i].__name__])
        for i in range(len(farr)):
            print('Calculating mesh for function '+farr[i].__name__)
            soldata[i].extend(simplesol(farr[i],thresh,newtriang = True))
        return soldata
    else:
        print('Calculating mesh for function '+farr.__name__)
        return simplesol(farr,thresh)

thresh = 0.3 * hstep
data = simplemeshfit([cos_times_sin,linear,circle,star,dumbbell],thresh, newtriang = True)

#pyplot.show()
pyplot.close('all')

names = ['Average skewness', 'Maximum skewness', 'Standard deviation of skewness',
         'Minimum triangle size', 'Maximum triangle size', 'Standard deviation of triangle size']
qualityplot(data,names)

#pyplot.show()
pyplot.close('all')

oldversions/Versie met werkende cut methode/main_programv2.py


import os
import copy
import pandas

from .. import layers
from .. import backends as be
from ..models.initialize import init_model as init
from . import gradient_util as gu

class State(object):
    """
    A State is a list of tensors that contains the states of the units
    described by a model.

    For a model with L hidden layers, the tensors have shapes

    shapes = [
    (num_samples, num_visible),
    (num_samples, num_hidden_1),
                .
                .
                .
    (num_samples, num_hidden_L)
    ]

    """
    def __init__(self, tensors):
        """
        Create a State object.

        Args:
            tensors: a list of tensors

        Returns:
            state object

        """
        self.units = tensors
        self.shapes = [be.shape(t) for t in self.units]

    @classmethod
    def from_model(cls, batch_size, model):
        """
        Create a State object.

        Args:
            batch_size (int): the number of samples per layer
            model (Model): a model object

        Returns:
            state object

        """
        shapes = [(batch_size, l.len) for l in model.layers]
        units = [model.layers[i].random(shapes[i]) for i in range(model.num_layers)]
        return cls(units)

    @classmethod
    def from_visible(cls, vis, model):
        """
        Create a state object with given visible unit values.

        Args:
            vis (tensor (num_samples, num_visible)): visible unit values.
            model (Model): a model object

        Returns:
            state object

        """
        batch_size = be.shape(vis)[0]
        state = cls.from_model(batch_size, model)
        state.units[0] = vis
        return state

    @classmethod
    def from_state(cls, state):
        """
        Create a State object from an existing State.

        Args:
            state (State): a State instance

        Returns:
            state object

        """
        return copy.deepcopy(state)

class Model(object):
    """
    General model class.
    Currently only supports models with 2 layers,
    (i.e., Restricted Boltzmann Machines).

    Example usage:
    '''
    vis = BernoulliLayer(nvis)
    hid = BernoulliLayer(nhid)
    rbm = Model([vis, hid])
    '''

    """
    def __init__(self, layer_list):
        """
        Create a model.

        Notes:
            Only 2-layer models currently supported.

        Args:
            layer_list: A list of layers objects.

        Returns:
            model: A model.

        """
        # the layers are stored in a list with the visible units
        # as the zeroth element
        self.layers = layer_list
        self.num_layers = len(self.layers)
        self.layer_connections = self._layer_connections()
        self.weight_connections = self._weight_connections()

        assert self.num_layers == 2,\
        "Only models with 2 layers are currently supported"

        # adjacent layers are connected by weights
        # therefore, if there are len(layers) = n then len(weights) = n - 1
        self.weights = [
            layers.Weights((self.layers[i].len, self.layers[i+1].len))
        for i in range(self.num_layers - 1)
        ]

    def get_config(self) -> dict:
        """
        Get a configuration for the model.

        Notes:
            Includes metadata on the layers.

        Args:
            None

        Returns:
            A dictionary configuration for the model.

        """
        config = {
            "model type": "RBM",
            "layers": [ly.get_config() for ly in self.layers],
        }
        return config

    @classmethod
    def from_config(cls, config: dict):
        """
        Build a model from the configuration.

        Args:
            A dictionary configuration of the model metadata.

        Returns:
            An instance of the model.

        """
        layer_list = []
        for ly in config["layers"]:
            layer_list.append(layers.Layer.from_config(ly))
        return cls(layer_list)

    def initialize(self, data, method: str='hinton'):
        """
        Initialize the parameters of the model.

        Args:
            data: A Batch object.
            method (optional): The initialization method.

        Returns:
            None

        """
        try:
            func = getattr(init, method)
        except AttributeError:
            print(method + ' is not a valid initialization method for latent models')
        func(data, self)
        for l in self.layers:
            l.enforce_constraints()
        for w in self.weights:
            w.enforce_constraints()

    def random(self, vis):
        """
        Generate a random sample with the same shape,
        and of the same type, as the visible units.

        Args:
            vis: The visible units.

        Returns:
            tensor: Random sample with same shape as vis.

        """
        return self.layers[0].random(vis)

    def _layer_connections(self):
        """
        Helper function to enumerate the connections between layers.
        List of list of indices of each layer connected to the layer.
        e.g. for a 4-layer model the connections are [[1], [0, 2], [1, 3], [2]].

        Args:
            None

        Returns:
            list: Indices of connecting layers.

        """
        return [[j for j in [i-1,i+1] if 0<=j<self.num_layers]
                   for i in range(self.num_layers)]

    def _weight_connections(self):
        """
        Helper function to enumerate the connections between weights and layers.
        List of list of indices of each weight layer connected to the layer.
        e.g. for a 4-layer model the connections are [[0], [0, 1], [1, 2], [2]].

        Args:
            None

        Returns:
            list: Indices of connecting weight layers.

        """
        return [[j for j in [i-1,i] if 0<=j<self.num_layers-1]
                   for i in range(self.num_layers)]

    def _connected_rescaled_units(self, i, state):
        """
        Helper function to retrieve the rescaled units connected to layer i.

        Args:
            i (int): the index of the layer of interest
            state (State): the current state of the units

        Returns:
            list[tensor]: the rescaled values of the connected units

        """
        return [self.layers[j].rescale(state.units[j])
                            for j in self.layer_connections[i]]

    def _connected_weights(self, i):
        """
        Helper function to retrieve the values of the weights connecting
        layer i to its neighbors.

        Args:
            i (int): the index of the layer of interest

        Returns:
            list[tensor]: the weights connecting layer i to its neighbros

        """
        return [self.weights[j].W() if j < i else self.weights[j].W_T()
                            for j in self.weight_connections[i]]

    def _alternating_update(self, func_name, state, beta=None, clamped=[]):
        """
        Performs a single Gibbs sampling update in alternating layers.
        state -> new state

        Args:
            func_name (str, function name): layer function name to apply to the units to sample
            state (State object): the current state of each layer
            beta (optional, tensor (batch_size, 1)): Inverse temperatures
            clamped (list): list of layer indices to clamp (no update)

        Returns:
            new state

        """
        updated_state = State.from_state(state)

        # update the odd then the even layers
        for ll in [range(1, self.num_layers, 2), range(0, self.num_layers, 2)]:
            for i in ll:
                if i in clamped:
                    continue
                else:
                    func = getattr(self.layers[i], func_name)

                    updated_state.units[i] = func(
                        self._connected_rescaled_units(i, updated_state),
                        self._connected_weights(i),
                        beta)

        return updated_state

    def markov_chain(self, n, state, beta=None, clamped=[]):
        """
        Perform multiple Gibbs sampling steps in alternating layers.
        state -> new state

        Notes:
            Samples layers according to the conditional probability
            on adjacent layers,
            x_i ~ P(x_i | x_(i-1), x_(i+1) )

        Args:
            n (int): number of steps.
            state (State object): the current state of each layer
            beta (optional, tensor (batch_size, 1)): Inverse temperatures
            clamped (list): list of layer indices to clamp

        Returns:
            new state

        """
        new_state = State.from_state(state)
        for _ in range(n):
            new_state = self._alternating_update('conditional_sample',
                                                 new_state,
                                                 beta,
                                                 clamped)
        return new_state

    def mean_field_iteration(self, n, state, beta=None, clamped=[]):
        """
        Perform multiple mean-field updates in alternating layers
        states -> new state

        Notes:
            Returns the expectation of layer units
            conditioned on adjacent layers,
            x_i = E[x_i | x_(i-1), x_(i+1) ]

        Args:
            n (int): number of steps.
            state (State object): the current state of each layer
            beta (optional, tensor (batch_size, 1)): Inverse temperatures
            clamped (list): list of layer indices to clamp

        Returns:
            new state

        """
        new_state = State.from_state(state)
        for _ in range(n):
            new_state = self._alternating_update('conditional_mean',
                                                 new_state,
                                                 beta,
                                                 clamped)
        return new_state

    def deterministic_iteration(self, n, state, beta=None, clamped=[]):
        """
        Perform multiple deterministic (maximum probability) updates
        in alternating layers.
        state -> new state

        Notes:
            Returns the layer units that maximize the probability
            conditioned on adjacent layers,
            x_i = argmax P(x_i | x_(i-1), x_(i+1))

        Args:
            n (int): number of steps.
            state (State object): the current state of each layer
            beta (optional, tensor (batch_size, 1)): Inverse temperatures
            clamped (list): list of layer indices to clamp

        Returns:
            new state

        """
        new_state = State.from_state(state)
        for _ in range(n):
            new_state = self._alternating_update('conditional_mode',
                                                 new_state,
                                                 beta,
                                                 clamped)
        return new_state

    def gradient(self, data_state, model_state):
        """
        Compute the gradient of the model parameters.
        Updates the states for the positive and negative phases,
        and computes the gradient from the unit values.

        Args:
            data_state (State object): The observed visible units and sampled hidden units.
            model_state (State objects): The visible and hidden units sampled from the model.

        Returns:
            dict: Gradients of the model parameters.

        """
        grad = gu.Gradient(
            [None for l in self.layers],
            [None for w in self.weights]
        )

        # POSITIVE PHASE (using observed)

        # compute the conditional mean of the hidden layers
        new_data_state = self.mean_field_iteration(1, data_state, clamped=[0])

        # compute the postive phase of the gradients of the layer parameters
        for i in range(self.num_layers):
            grad.layers[i] = self.layers[i].derivatives(
                new_data_state.units[i],
                self._connected_rescaled_units(i, new_data_state),
                self._connected_weights(i)
            )

        # compute the positive phase of the gradients of the weights
        for i in range(self.num_layers - 1):
            grad.weights[i] = self.weights[i].derivatives(
                self.layers[i].rescale(new_data_state.units[i]),
                self.layers[i+1].rescale(new_data_state.units[i+1]),
            )

        # NEGATIVE PHASE (using sampled)

        # compute the conditional mean of the hidden layers
        new_model_state = self.mean_field_iteration(1, model_state, clamped=[0])

        # update the gradients of the layer parameters with the negative phase
        for i in range(self.num_layers):
            grad.layers[i] = be.mapzip(be.subtract,
                self.layers[i].derivatives(
                    new_model_state.units[i],
                    self._connected_rescaled_units(i, new_model_state),
                    self._connected_weights(i)
                ),
            grad.layers[i])

        # update the gradients of the weight parameters with the negative phase
        for i in range(self.num_layers - 1):
            grad.weights[i] = be.mapzip(be.subtract,
                self.weights[i].derivatives(
                    self.layers[i].rescale(new_model_state.units[i]),
                    self.layers[i+1].rescale(new_model_state.units[i+1]),
                ),
            grad.weights[i])

        return grad

    def parameter_update(self, deltas):
        """
        Update the model parameters.

        Notes:
            Modifies the model parameters in place.

        Args:
            deltas (Gradient)

        Returns:
            None

        """
        for i in range(self.num_layers):
            self.layers[i].parameter_step(deltas.layers[i])
        for i in range(self.num_layers - 1):
            self.weights[i].parameter_step(deltas.weights[i])

    def joint_energy(self, data):
        """
        Compute the joint energy of the model based on a state.

        Args:
            data (State object): the current state of each layer

        Returns:
            tensor (num_samples,): Joint energies.

        """
        energy = 0
        for i in range(self.num_layers - 1):
            energy += self.layers[i].energy(data.units[i])
            energy += self.layers[i+1].energy(data.units[i+1])
            energy += self.weights[i].energy(data.units[i], data.units[i+1])
        return energy

    def marginal_free_energy(self, data):
        """
        Compute the marginal free energy of the model.

        If the energy is:
        E(v, h) = -\sum_i a_i(v_i) - \sum_j b_j(h_j) - \sum_{ij} W_{ij} v_i h_j
        Then the marginal free energy is:
        F(v) =  -\sum_i a_i(v_i) - \sum_j \log \int dh_j \exp(b_j(h_j) - \sum_i W_{ij} v_i)
        This can be extended to a deep model by a sum over all hidden states

        Args:
            data (State object): The current state of each layer.

        Returns:
            tensor (batch_size, ): Marginal free energies.

        """
        assert self.num_layers == 2 # supported for 2-layer models only
        i = 0
        phi = be.dot(data.units[i], self.weights[i].W())
        log_Z_hidden = self.layers[i+1].log_partition_function(phi)
        energy = 0
        energy += self.layers[i].energy(data.units[i])
        energy -= be.tsum(log_Z_hidden, axis=1)
        return energy

    def save(self, store):
        """
        Save a model to an open HDFStore.

        Notes:
            Performs an IO operation.

        Args:
            store (pandas.HDFStore)

        Returns:
            None

        """
        # save the config as an attribute
        config = self.get_config()
        store.put('model', pandas.DataFrame())
        store.get_storer('model').attrs.config = config
        # save the weights
        for i in range(self.num_layers - 1):
            key = os.path.join('weights', 'weights'+str(i))
            self.weights[i].save_params(store, key)
        for i in range(len(self.layers)):
            key = os.path.join('layers', 'layers'+str(i))
            self.layers[i].save_params(store, key)

    @classmethod
    def from_saved(cls, store):
        """
        Build a model by reading from an open HDFStore.

        Notes:
            Performs an IO operation.

        Args:
            store (pandas.HDFStore)

        Returns:
            None

        """
        # create the model from the config
        config = store.get_storer('model').attrs.config
        model = cls.from_config(config)
        # load the weights
        for i in range(len(model.weights)):
            key = os.path.join('weights', 'weights'+str(i))
            model.weights[i].load_params(store, key)
        # load the layer parameters
        for i in range(len(model.layers)):
            key = os.path.join('layers', 'layers'+str(i))
            model.layers[i].load_params(store, key)
        return model


from ROOT import *
from array import array
import sys

output = '~/www/12-05-2017/ss/'
output = '~/www/12-05-2017/ss1/'
output = '~/www/13-05-2017/best_fit/'
output = '~/www/15-05-2017/best_fit/'
output = '~/www/22-05-2017/best_fit/'
output = '~/www/25-05-2017/best_fit/'
output = '~/www/14-06-2017/best_fit/'
output = '~/www/15-06-2017/best_fit/'
output = '~/www/01-07-2017/best_fit/'
output = '~/www/07-08-2017/best_fit/'
output = '~/www/07-08-2017/best_fit_pn/'
output = '~/www/08-08-2017/best_fit_pn/'

gROOT.SetBatch(True)

gROOT.ProcessLine(".L plotStyle.C")

SetPlotStyle()

gStyle.SetPalette(1)

from bestFits import *

#xminshift = [3, 1, 3, 2, 3, 0]
#xmaxshift = [1, 2, 2, 2, 1, 2]

#xminshift = [1, 1, 1, 1, 1, 1]
#xmaxshift = [1, 1, 1, 1, 1, 1]

#xminshift = [-6, 1, 1, 1, 1, 1]
#xmaxshift = [ 1, 1, 1, 1, 1, 1]

#xminshift = [ 2, 3, 1, 4, 1, 1]
#xmaxshift = [ 2, 6, 4, 9, 4, 3]

#xminshift = [ 0, 3, 1, 4, 1, 1]
#xmaxshift = [ 4, 6, 4, 9, 4, 3]

#xminshift = [ 1, 5, 2, 5, 3, 3 ]
#xmaxshift = [ 1, 4, 3, 9, 1, 1 ]

#xminshift = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
#xmaxshift = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]

xminshift = []
xmaxshift = []
for t in titles:
    xminshift.append(1)
    xmaxshift.append(1)

xminshift = [ 1, 1, 1, 1, 1, 1 ]
xmaxshift = [ 1, 1, 1, 1, 4, 1 ]

#xminshift = [ 1, 5, 2, 5, 3, 3 ]
#xmaxshift = [ 1, 4, 3, 9, 1, 1 ]

cs = []
gs = []
N = []
X = []
Y = []
Exl = []
Exh = []
Eyl = []
Eyh = []
cls = []
bxs = []

for ti, t in enumerate(T):
    N.append(len(t)-1)

    X.append(array('d', [t[j][0] for j in xrange(0, N[-1])]))
    Y.append(array('d', [i-1 for i in xrange(N[-1], 0, -1)]))
    Exl.append(array('d', [abs(i) for i in [t[j][1] for j in xrange(0, N[-1])]]))
    Exh.append(array('d', [abs(i) for i in [t[j][2] for j in xrange(0, N[-1])]]))
    Eyl.append(array('d', [0.0 for j in xrange(0, N[-1])]))
    Eyh.append(array('d', [0.0 for j in xrange(0, N[-1])]))
    gs.append(TGraphAsymmErrors(N[-1], X[-1], Y[-1], Exl[-1], Exh[-1], Eyl[-1], Eyh[-1]))
    gs[-1].SetName(titles[ti])
    gs[-1].SetTitle(titles[ti])
    gs[-1].SetMarkerStyle(21)
    gs[-1].SetLineColor(kRed)
    gs[-1].SetLineWidth(3)
    gs[-1].GetYaxis().SetLabelOffset(1)
    cs.append(TCanvas(titles[ti]))
    cs[-1].SetLeftMargin(0.31)
    gs[-1].Draw("APZ")
    gs[-1].SetMaximum(N[-1]-0.5)
    gs[-1].SetMinimum(-0.5)
    gs[-1].GetXaxis().SetTitle("#mu")
    gxrange = gs[-1].GetXaxis().GetXmax()-gs[-1].GetXaxis().GetXmin()
    gxradd = 0.1*gxrange
    gs[-1].GetXaxis().SetLimits(gs[-1].GetXaxis().GetXmin()-xminshift[ti]*gxradd, gs[-1].GetXaxis().GetXmax()+xmaxshift[ti]*gxradd)
    if t[-1][0]+t[-1][1] > gs[-1].GetXaxis().GetXmin() and t[-1][0]+t[-1][2] < gs[-1].GetXaxis().GetXmax():
        bxs.append(TBox(t[-1][0]+t[-1][1], Y[-1][0]+0.5, t[-1][0]+t[-1][2], Y[-1][-1]-0.5))
    elif t[-1][0]+t[-1][1] <= gs[-1].GetXaxis().GetXmin():
        bxs.append(TBox(gs[-1].GetXaxis().GetXmin(), Y[-1][0]+0.5, t[-1][0]+t[-1][2], Y[-1][-1]-0.5))
    elif t[-1][0]+t[-1][2] >= gs[-1].GetXaxis().GetXmax():
        bxs.append(TBox(t[-1][0]+t[-1][1], Y[-1][0]+0.5, gs[-1].GetXaxis().GetXmax(), Y[-1][-1]-0.5))
    bxs[-1].SetFillColor(kAzure-9)
    #bxs[-1].SetFillStyle(3004)
    bxs[-1].Draw()
    cls.append(TLine(t[-1][0], Y[-1][0]+0.5, t[-1][0], Y[-1][-1]-0.5))
    cls[-1].SetLineColor(kBlue)
    cls[-1].SetLineWidth(3)
    cls[-1].Draw("SAME")
    gs[-1].Draw("PZ SAME")
    tx = TText()
    tx.SetTextAlign(32)
    tx.SetTextSize(gs[-1].GetXaxis().GetLabelSize())
    tx.SetTextFont(gs[-1].GetXaxis().GetLabelFont())
    tmx = TMathText()
    tmx.SetTextAlign(32)
    tmx.SetTextSize(gs[-1].GetXaxis().GetLabelSize())
    tmx.SetTextFont(gs[-1].GetXaxis().GetLabelFont())
    #ylabels = ["b2j3","b3j3","b2j4","b3j4","b4j4","comb"]
    ylabels = ["b2j3","b2j4","b3j3","b3j4","b4j4","comb"]
    #xposition = gs[-1].GetXaxis().GetBinLowEdge(1)-(gs[-1].GetXaxis().GetBinLowEdge(1)+gs[-1].GetXaxis().GetBinUpEdge(gs[-1].GetXaxis().GetNbins()+1))/20
    xposition = gs[-1].GetXaxis().GetXmin()-(gs[-1].GetXaxis().GetXmax()+gs[-1].GetXaxis().GetXmax())/100
    #xposition = gs[-1].GetXaxis().GetBinLowEdge(1)-gs[-1].GetXaxis().GetBinCenter(1)
    for i in xrange(0, len(t)-1):
        #ytexl = TLatex(xposition, Y[-1][i], ylabels[i]+" #mu_{comb.} = %.3f_{%+.3f}^{%+.3f}" % (t[i][0], t[i][1], t[i][2]))
        #ytexl.Draw()
#        tx.DrawText(xposition, Y[-1][i], ylabels[i]+"                          ")
#        tmx.DrawMathText(xposition+0.03, Y[-1][i], "\mu = %.2f_{%+.2f}^{%+.2f}" % (t[i][0], t[i][1], t[i][2]))
#        tx.DrawText(xposition, Y[-1][i], ylabels[i])
#       tmx.DrawMathText(xposition, Y[-1][i], ylabels[i]+"\ \mu = %.2f_{%+.2f}^{%+.2f}" % (t[i][0], t[i][1], t[i][2]))
       tmx.DrawMathText(xposition, Y[-1][i], "\mu_{"+ylabels[i]+"} = %.2f_{%+.2f}^{%+.2f}" % (t[i][0], t[i][1], t[i][2]))
    #leg = TLegend(0.75,0.90,0.90,0.70)
    #leg = TLegend(0.70,0.70,0.95,0.40)
    leg = TLegend(0.70,0.45,0.95,0.15)
    leg.SetFillStyle(0)
    leg.SetBorderSize(0)
    #leg.AddEntry(line[0],"Expected","l")
    leg.AddEntry(gs[-1], "#mu_{category}", "pl")
    gtmp = TGraph()
    gtmp.SetLineColor(kBlue)
    gtmp.SetLineWidth(3)
    gtmp.SetFillColor(kAzure-9)
    #leg.AddEntry(cls[-1], "#mu_{comb.} = %.3f_{%+.3f}^{%+.3f}" % (t[-1][0], t[-1][1], t[-1][2]), "fl")
    leg.AddEntry(gtmp, "#mu_{comb.} = %.2f_{%+.2f}^{%+.2f}" % (t[-1][0], t[-1][1], t[-1][2]), "fl")
    #c1.RedrawAxis("g");
    leg.Draw()
    #tex = TLatex(0.1969, 0.906825, "CMS")
    tex = TLatex(0.34, 0.906825, "CMS")
    #tex = TLatex(0.7969, 0.906825, "CMS")
    tex.SetNDC()
    tex.SetTextAlign(13)
    tex.SetTextFont(61)
    tex.SetTextSize(0.07475)
    tex.SetLineWidth(2)
    tex.Draw()
    #text1 = TLatex(0.98, 0.95125, "36 fb^{-1}, #sqrt{s} = 13 TeV")
    text1 = TLatex(0.98, 0.95125, "36 fb^{-1} (13 TeV)")
    text1.SetNDC()
    text1.SetTextAlign(31)
    text1.SetTextFont(42)
    text1.SetTextSize(0.04875)
    text1.SetLineWidth(2)
    text1.Draw()
    #ctex = TLatex(0.50, 0.87, titles[ti])
    ctex = TLatex(0.87, 0.906825, "Hut" if "hut" in titles[ti] else "Hct")
    ctex.SetNDC()
    ctex.SetTextAlign(13)
    ctex.SetTextFont(61)
    ctex.SetTextSize(0.07475)
    ctex.SetLineWidth(2)
    ctex.Draw()
    cs[-1].RedrawAxis()
    cs[-1].Print(output+cs[-1].GetName()+'.png')
    cs[-1].Print(output+cs[-1].GetName()+'.pdf')
    cs[-1].Print(output+cs[-1].GetName()+'.eps')


from data import DICTIONARY, LETTER_SCORES
import pytest

def load_words():
    """Loads Dictionary into List"""
    return open(DICTIONARY,'r').read().split('\n')

def calc_word_value(word):
    """Returns the Scrabble value of any string
    str => int"""
    value = 0
    for letter in list(word):
        try:
            value += LETTER_SCORES[letter.upper()]
        except(KeyError):
            pass
    return value

def max_word_value(word_list=[]):
    max_word = ["",0]
    if not word_list:
        word_list = load_words()

    for word in word_list:
        value = calc_word_value(word)
        if value > max_word[1]:
            max_word = [word, value]
    return max_word[0]

class test(object):

    def test_load_words(self):
        assert type(load_words()) == list

    def test_calc_word_value(self):
        assert calc_word_value("Green") == 6
        assert calc_word_value("zebra") == 16

    def test_max_word_value(self):
        assert max_word_value() == 'benzalphenylhydrazone'

print(max_word_value(load_words()))


import pandas as pd

from matplotlib import pyplot as p

y=[2,34,5,67,4,3]
x=[2001,2002,2003,2004,2005,2006]

p.plot(x,y)
#p.plot(y,x)
p.title('data')
p.xlabel('food corps')
p.ylabel('years')
p.show()


# -*- coding: utf-8 -*-

def _init():
    """initial a dict to store global veriables"""
    global _global_dict  #initialize
    _global_dict = {}

def import_variables_from_file(locations):
    """read given variable files from variable files under  etc path"""
    variable_files = locations # define a list contains the variable files
    for variable_file in variable_files:
        with open(variable_file) as file_object:
            lines = file_object.read().splitlines()
            for line in lines:
                if '=' in line:
                    _global_dict[line.split('=')[0].strip()] =  line.split('=')[1].strip()

def get_dict():
    """  read all variables in _global_dict currently  """
    print('==> The variables defined in etc files are:')
    for key ,value in _global_dict.items():
        print ('    '+key,':',value)
    print()
    print('==> Please attention,these variable names have alreaby been occupied!pleasae don\'t overwrite them!!')
    for key in _global_dict.keys():
        print('    '+key,end = ',')
    print('\n')

def set_value(key,value):
    """ define a global variable"""
    _global_dict[key] = value

def get_value(key,defValue=None):
    """ get a veriable ,retuen NONE if not exist """
    try:
        return _global_dict[key]
    except KeyError:
        return defValue

def get_values(*keys,defValue=None):
    """ get a variables ,retuen NONE if not exist """

    import basic_class
    tmp_vars = []
    basic_class.mylogger_record.debug('items in _global_dict are:')
    for key in keys:
        try:
            tmp_vars.append(_global_dict[key])
            basic_class.mylogger_recordnf.debug(key+' = '+_global_dict[key])
        except KeyError:
            tmp_vars.append(defValue)
    return tmp_vars


import base64
import json
import os
import random
import time
from functools import wraps
from queue import Queue
from threading import Thread

import cats

fernet = None

COMMON_WORDS_SET = set(cats.lines_from_file("data/common_words.txt"))
CAPTCHA_QUEUE_LEN = 200
CAPTCHA_LENGTH = 10
CAPTCHA_WORD_LEN = 10

captcha_queue = Queue()

def require_fernet(f):
    @wraps(f)
    def wrapped(*args, **kwargs):
        global fernet
        if not fernet:
            from cryptography.fernet import Fernet

            fernet = Fernet(os.environ.get("FERNET_KEY", Fernet.generate_key()))
        return f(*args, **kwargs)

    return wrapped

def token_writer(f):
    @wraps(f)
    @require_fernet
    def wrapped(*args, **kwargs):
        data = f(*args, **kwargs)
        decoded = json.dumps(data).encode("utf-8")
        return fernet.encrypt(decoded).decode("utf-8")

    return wrapped

def token_reader(fail):
    def decorator(f):
        @wraps(f)
        @require_fernet
        def wrapped(*, token, **kwargs):
            from cryptography.fernet import InvalidToken

            if not token:
                return fail
            try:
                return f(
                    token=json.loads(fernet.decrypt(token.encode("utf-8"))), **kwargs
                )
            except (TypeError, InvalidToken):
                return fail

        return wrapped

    return decorator

@token_writer
def create_wpm_authorization(user, wpm):
    return {
        "user": user,
        "wpm": wpm,
    }

@token_reader(fail=0)
def get_authorized_limit(user, token):
    if token["user"] != user:
        return 0
    return token["wpm"]

@token_writer
def encode_challenge(user, words):
    return {
        "user": user,
        "words": words,
        "startTime": time.time(),
    }

@token_reader(fail=(False, False))
def decode_challenge(token):
    return token["user"], token["words"], token["startTime"]

def populate_captcha_queue():
    while captcha_queue.qsize() < CAPTCHA_QUEUE_LEN:
        captcha_queue.put(generate_captcha())

def generate_captcha():
    from claptcha import Claptcha

    word = random.choice([x for x in COMMON_WORDS_SET if len(x) < CAPTCHA_WORD_LEN])
    c = Claptcha(word, "gui_files/FreeMono.ttf", margin=(20, 10))
    image_b64 = base64.b64encode(c.bytes[1].getvalue()).decode("utf-8")
    return "data:image/png;base64," + image_b64, word

def get_captcha_urls(num_words=CAPTCHA_LENGTH):
    Thread(target=populate_captcha_queue).start()

    images, words = [], []
    for _ in range(num_words):
        image, word = captcha_queue.get()
        images.append(image)
        words.append(word)

    return images, words


from flask import Flask
app = Flask(__name__)

@app.route('/')
def main():
    head = open("index0.html").read()
    body = open("laser_macaroni.html").read()
    foot = open("index1.html").read()
    return head+body+foot

if __name__ == "__main__":
    app.run()


import os
import sqlite3
import re
from subprocess import Popen, check_output

def init_db(db_file):
    conn = sqlite3.connect(db_file)
    c = conn.cursor()
    c.execute("CREATE TABLE models (name string primary key, script text, description string)")
    c.execute("CREATE TABLE trainings (label string primary key, status string, train_dir string, tensorboard integer, submit_at text, start_at text, stop_at text, num_gpu integer)")
    conn.commit()
    conn.close()

def deploy_web():
    db_file = os.environ.get('FLASK_DB')
    if not os.path.isfile(db_file):
        print("Database is not setup, setting up now")
        print(db_file)
        init_db(db_file)

    cmd = "ps -u ai u"
    res = check_output(cmd.split()).decode('ascii').split('\n')
    for line in res:
        if re.search('gunicorn', line):
            pid = line.split()[1]
            os.system('kill {}'.format(pid))
            print('Kill flask process with PID {}'.format(pid))

    cwd = os.getcwd()
    flask_dir = os.path.dirname(os.path.realpath(__file__)) + '/../../../webapp'
    os.chdir(flask_dir)
    cmd = 'gunicorn main:app -b 0.0.0.0:{} -w 4'.format(os.environ.get('FLASK_PORT'))
    Popen(cmd.split())
    os.chdir(cwd)