#-- Python imports --#
from __future__ import division
import threading, pygame, math, sys, datetime

import ogre.renderer.OGRE as ogre
import ogre.io.OIS as OIS

#-- Local file imports --#
import SampleFramework as sf
from ShrewFrameListener import ShrewFrameListener

from MeshMaker import MeshMaker
from PlaneMaker import PlaneMaker

#-- Path imports --#
sys.path.append("../dataObjects")
from ExperimentData import States, ExperimentData

class Shrewenstein3D(sf.Application):
    def init(self):
        #__init__ is defined in sf.Application, so we do our actual init'ing here.
        self.planeMaker = PlaneMaker(self.sceneManager)
        self.meshMaker = MeshMaker(self.sceneManager)

        #get instance of ExperimentData, we need its info to set up the scene
        self.exData = ExperimentData()

        #start events engine
        pygame.init()

    def _createScene(self):
        self.init()

        exData = self.exData

        #lights!
        self.sceneManager.ambientLight = (0.5,0.5,0.5) #using *mostly* ambient light, with minor lighting for contours.
        light = self.sceneManager.createLight('goal1Light')
        light.type = ogre.Light.LT_POINT
        lightPos = list(self.exData.goal1Pos)
        lightPos[0] = lightPos[0] - 100
        light.position = lightPos
        light.diffuseColour = 1, 1, 1
        light.specularColour = 1, 1, 1

        light = self.sceneManager.createLight('goal2Light')
        light.type = ogre.Light.LT_POINT
        lightPos = list(self.exData.goal2Pos)
        lightPos[0] = lightPos[0] + 100
        light.position = lightPos
        light.diffuseColour = 1, 1, 1
        light.specularColour = 1, 1, 1

        light = self.sceneManager.createLight('sampleLight')
        light.type = ogre.Light.LT_POINT
        lightPos = list(self.exData.samplePos)
        lightPos[2] = lightPos[2] - 100
        light.position = lightPos
        light.diffuseColour = 1, 1, 1
        light.specularColour = 1, 1, 1

        #room
        cubeTextures=[
            'Tupaia3D/Gray',#floor
            'Tupaia3D/Gray',#ceiling
            'Tupaia3D/WallPaint',#left
            'Tupaia3D/WallPaint',#right
            'Tupaia3D/WallPaint',#front wall
            'Tupaia3D/WallPaint',#back wall
            ]

        cube1Size = (exData.roomSizeX,exData.roomSizeY,exData.roomSizeZ)
        cube1Center = (0,exData.roomSizeY/2,exData.roomSizeZ/2)
        self.planeMaker.makeCube(objName='room', textureNames=cubeTextures, centerPoint=cube1Center, size=cube1Size, normalsPointOut=False)

        #cylinders
        self.meshMaker.makeCylinderMesh(name='goalMarkerMesh', radius=10, height=100, sides=12)

        ent = self.sceneManager.createEntity('goalCylinder1', 'goalMarkerMesh')
        ent.setMaterialName('Tupaia3D/Blue')
        node = self.sceneManager.getRootSceneNode().createChildSceneNode('goalCylinder1', self.exData.goal1Pos)
        node.attachObject(ent);

        ent = self.sceneManager.createEntity('goalCylinder2', 'goalMarkerMesh')
        ent.setMaterialName('Tupaia3D/Blue')
        node = self.sceneManager.getRootSceneNode().createChildSceneNode('goalCylinder2', self.exData.goal2Pos)
        node.attachObject(ent)

        #goal textures on wall. Horizontal or vertical will be visible based on trial.
        goalTextureSizeX = 200
        goalTextureSizeY = 200

        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture0', objName='GoalTexture1H', size=(goalTextureSizeX,goalTextureSizeY),
            meshName = 'GoalMesh1H', centerPoint=exData.goal1TexPos, normalVector=(0,0,-1), upVector=(0,1,0))
        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture90', objName='GoalTexture1V', size=(goalTextureSizeX,goalTextureSizeY),
            meshName = 'GoalMesh1V', centerPoint=exData.goal1TexPos, normalVector=(0,0,-1), upVector=(0,1,0))

        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture0', objName='GoalTexture2H', size=(goalTextureSizeX,goalTextureSizeY),
            meshName = 'GoalMesh2H', centerPoint=exData.goal2TexPos, normalVector=(0,0,-1), upVector=(0,1,0))
        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture90', objName='GoalTexture2V', size=(goalTextureSizeX,goalTextureSizeY),
            meshName = 'GoalMesh2V', centerPoint=exData.goal2TexPos, normalVector=(0,0,-1), upVector=(0,1,0))

        #fixation texture
        fixationTextureSizeX = 1
        fixationTextureSizeY = 1
        fixationPos = exData.fixationPos

        self.planeMaker.makePlane(textureName='Tupaia3D/Blue', objName='FixationTexture', meshName='fixationMesh',
            size=(fixationTextureSizeX,fixationTextureSizeY), centerPoint=fixationPos, normalVector=(0,0,-1), upVector=(0,1,0))

        #sample texture. Horizontal or vertical will be displayed based on trial.
        sampleTextureSizeX = 25
        sampleTextureSizeY = 25

        samplePos = exData.samplePos

        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture0', objName='SampleTextureH', meshName='sampleMeshH',
            size=(sampleTextureSizeX,sampleTextureSizeY), centerPoint=samplePos, normalVector=(0,0,-1), upVector=(0,1,0))
        self.planeMaker.makePlane(textureName='Tupaia3D/GoalTexture90', objName='SampleTextureV', meshName='sampleMeshV',
            size=(sampleTextureSizeX,sampleTextureSizeY), centerPoint=samplePos, normalVector=(0,0,-1), upVector=(0,1,0))

        #Make things invisible that should be invisible at trial start
        self.sceneManager.getEntity('SampleTextureH').setVisible(False)
        self.sceneManager.getEntity('SampleTextureV').setVisible(False)

        self.sceneManager.getEntity('GoalTexture2H').setVisible(False)
        self.sceneManager.getEntity('GoalTexture2V').setVisible(False)
        self.sceneManager.getEntity('GoalTexture1H').setVisible(False)
        self.sceneManager.getEntity('GoalTexture1V').setVisible(False)

        self.sceneManager.getEntity('goalCylinder1').setVisible(False)
        self.sceneManager.getEntity('goalCylinder2').setVisible(False)

    def _createCamera(self):
        self.camera =  self.sceneManager.createCamera ('PlayerCam')
        self.camera.nearClipDistance = 5
        self.camera.farClipDistance = 10000

    def _createViewports(self):
        viewport = self.renderWindow.addViewport(self.camera)
        viewport.backGroundColor = (0, 0, 0)
        self.camera.aspectRatio = float(viewport.actualWidth) / float (viewport.actualHeight)

    def _createFrameListener(self):
        self.frameListener = ShrewFrameListener(self.renderWindow, self.camera, self.sceneManager)
        self.root.addFrameListener(self.frameListener)
        self.frameListener.showDebugOverlay(False)

        #Start the experiment
        self.frameListener.startFixation()

if __name__ == '__main__':
    ta = Shrewenstein3D()
    ta.go()


#!/usr/bin/env python
"""Helper script to test out reports

Example:
python run_report.py -m cases deaths -d date -c "[('date', '>', '2020-03-20')]"
"""
import ast
import logging

from tlbx import Script, Arg, st

from zillion_covid_19.warehouse import wh

@Script(
    Arg("-m", "--metrics", nargs="+", help="Metrics to include in report"),
    Arg("-d", "--dimensions", nargs="+", help="Dimensions to include in report"),
    Arg("-c", "--criteria", help="String to be eval'd as criteria param"),
    Arg("-ll", "--log_level", type=int, default=None, help="Set log level"),
)
def main(metrics=None, dimensions=None, criteria=None, log_level=None):
    if criteria:
        criteria = ast.literal_eval(criteria)

    if log_level:
        logger = logging.getLogger()
        logger.setLevel(log_level)

    print("Metrics:", metrics)
    print("Dimensions:", dimensions)
    print("Criteria:", criteria)

    result = wh.execute(metrics=metrics, dimensions=dimensions, criteria=criteria)
    print(result.df_display)

if __name__ == "__main__":
    main()


# @team 35
# @author Jiacheng Ye   904973      Shanghai, China
# @author Shiyi Xu      780801      Melbourne, Australia
# @author Yuyao Ma      1111182     Yinchuan, China
# @author Yujing Guan   1011792     Fuzhou, China
# @author Zexin Yu      10328021    Dalian, China
# Search Twitters in 6 States:
# TAS, WA

# Use tweept library, link: https://github.com/tweepy/tweepy
# APIs reference: https://docs.tweepy.org/en/latest/api.html#api-reference
# https://developer.twitter.com/en/docs/twitter-api/v1/tweets/search/api-reference/get-search-tweets

import tweepy, couchdb, json, sys, itertools

from os.path import dirname, abspath
parent_dir = dirname(dirname(abspath(__file__)))
sys.path.append(parent_dir)
grandparent_dir = dirname(dirname(dirname(abspath(__file__))))
sys.path.append(grandparent_dir)
grandgrandparent_dir = dirname(dirname(dirname(dirname(abspath(__file__)))))
sys.path.append(grandgrandparent_dir)

from twitter_api_config import twitter_api
from couchdb_config import Config

# Define the geo restriction for states
state_geo = {'TAS': '-41.454521,145.970673,150km', 'WA': '-27.672817,121.628311,900km'}

# Search all twitters with keyword in a certain State
def count_keyword_in_different_states(keyword, state, api, db):

    count = 0
    # Define max_id in order to achieve getting non-repetitive data
    max_id = 0
    prev_id = 0

    # Because of twitter API limits the maximum number of returns to 100 at a time,
    # so use max_id field restricts the content returned to be different each time.
    for i in itertools.count():
        # For the first time, there is no max_id field, so call api.search() without max_id
        if (i == 0):
            search_results = api.search(q = keyword, geocode = state_geo[state], count = 100)
        # After the first time, through max_id to return non duplicate query results
        else:
            search_results = api.search(q = keyword, geocode = state_geo[state], max_id = max_id, count = 100)

        # If the query result is empty, the loop ends
        if (len(search_results) == 0):
            return count

        # Update max_id --- max_id is '_id' filed of the last query result
        max_id = search_results[-1]._json['id'] - 1

        # process Tweet content
        for tweet in search_results:
            # If the id of this result is the same as that of the previous one,
            # it can be proved that all tweets have been found, then just return
            if (tweet._json['id'] == prev_id):
                print(state, count)
                return count

            # tweet is an object, and the relevant information of Twitter is in tweet._json
            # To detect whether a message contains a 'text' key.
            # Not all objects returned by twitter are messages. The difference is whether a message object contains a 'text' key
            if 'text' in tweet._json:
                count += 1
                # Conduct the dictionary for result
                dict_search = {'id': tweet._json['id_str'], 'created': tweet._json['created_at'], 'text': tweet._json['text'], 'state': state}
                # Save the result dictionary to CouchDB
                db.save(dict_search)
                # Update prev_id
                prev_id = tweet._json['id']

    return count

def main():
    # Get Twitter API handle
    api = twitter_api()

    # The keyword we would like to search
    search_keyword = ''

    # Define the name of CouchDB Database
    COUCHDB_DATABASE = "twitter_raw_data"
    # Get the server handle
    server = couchdb.Server(Config.COUCHDB_SERVER)
    # If the Database is not existent, create it
    if (not server.__contains__(COUCHDB_DATABASE)):
        db = server.create(COUCHDB_DATABASE)
    # Get the Database handle
    db = server[COUCHDB_DATABASE]

    count = 0

    for state in state_geo:
        count += count_keyword_in_different_states(search_keyword, state, api, db)

    print ('Total Count:', count)

if __name__ == '__main__':
    main()


'''
Created on 06.12.2019

@author: Zoli
'''
import argparse
import datetime
from solaredge import datareadout, solaredge
from battery import batteryestimator, battery
from report import excelreporter
from energycost import energycost, energycostcalculator

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--site_token', type=str, help='Site token of the solaredge inverter')
    parser.add_argument('-i', '--site_id', type=int, help='Site ID of the solaredge inverter')
    parser.add_argument('-s', '--start_date', type=str, help='Start date of energy readout in YYYY-MM-DD format')
    parser.add_argument('-e', '--end_date', type=str, help='End date of energy readout in YYYY-MM-DD format')
    parser.add_argument('-o', '--report_file', type=str, default='energy.xlsx', help='Output Excel report file')
    parser.add_argument('-a', '--acc_report_file', type=str, default='energy_accumulated.xlsx', help='Output Excel report file with accumulated energy')
    args = parser.parse_args()

    inverter = solaredge.Solaredge(args.site_token)
    readout = datareadout.DataReadout(inverter, args.site_id)
    energy = readout.getDetailedEnergy(args.start_date, args.end_date)

    # 2019
    ENERGY_COST_HIGH = 0.2526
    ENERGY_COST_LOW = 0.1901
    highWeekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
    highTarifHours = (datetime.time(hour=7, minute=0), datetime.time(hour=18, minute=59, second=59))
    validFrom = datetime.datetime(year=2019, month=1, day=1)
    validTo = datetime.datetime(year=2019, month=12, day=31, hour=23, minute=59, second=59)
    cost = energycost.EnergyCost()
    cost.setDualTariffEnergyCost(ENERGY_COST_HIGH, highWeekdays, highTarifHours[0], highTarifHours[1], ENERGY_COST_LOW, validFrom, validTo)
    REFUND = 0.093
    cost.setConstantEnergyRefundPerKWh(refund=REFUND, validFrom=validFrom, validTo=validTo)

    # 2020
    ENERGY_COST_HIGH = 0.2570
    ENERGY_COST_LOW = 0.1946
    validFrom = datetime.datetime(year=2020, month=1, day=1)
    validTo = datetime.datetime(year=2020, month=12, day=31, hour=23, minute=59, second=59)
    cost.setDualTariffEnergyCost(ENERGY_COST_HIGH, highWeekdays, highTarifHours[0], highTarifHours[1], ENERGY_COST_LOW, validFrom, validTo)
    REFUND = 0.1060
    cost.setConstantEnergyRefundPerKWh(refund=REFUND, validFrom=validFrom, validTo=validTo)

    # 2021
    ENERGY_COST_HIGH = 0.2247
    ENERGY_COST_LOW = 0.1921
    validFrom = datetime.datetime(year=2021, month=1, day=1)
    validTo = datetime.datetime(year=2021, month=12, day=31, hour=23, minute=59, second=59)
    cost.setDualTariffEnergyCost(ENERGY_COST_HIGH, highWeekdays, highTarifHours[0], highTarifHours[1], ENERGY_COST_LOW, validFrom, validTo)
    REFUND = 0.0896
    cost.setConstantEnergyRefundPerKWh(refund=REFUND, validFrom=validFrom, validTo=validTo)

    costCalculator = energycostcalculator.EnergyCostCalculator(cost)

    reporter = excelreporter.ExcelReporter(args.report_file)
    reporter.writeEnergyData(energy, readout.energyTypes(), costCalculator)

    realBattery = battery.Battery(capacity=9300, chargingLossPercent=1, dischargingLossPercent=1, maxChargingPower=5000, maxDischargingPower=7000)
    batteryEstimator = batteryestimator.BatteryEstimator(realBattery)
    accumulatedEnergy = batteryEstimator.accumulateFeedInEnergy(energy, datareadout.DataReadout.energyTypes())

    reporter = excelreporter.ExcelReporter(args.acc_report_file)
    reporter.writeEnergyData(accumulatedEnergy, readout.energyTypes(), costCalculator)


import celery
import feather
import numpy
import pandas as pd
import requests

from datetime import datetime, timedelta
from statsmodels.tsa.arima_model import ARIMA

# create a differenced series
def difference(dataset, interval=1):
    diff = list()
    for i in range(interval, len(dataset)):
        value = dataset[i] - dataset[i - interval]
        diff.append(value)
    return numpy.array(diff)

# invert differenced value
def inverse_difference(history, yhat, interval=1):
    return yhat + history[-interval]

def process_prediction(overall_data):
    result = ""
    try:
        split_point = len(overall_data) - 1
        dataset, validation = overall_data[0:split_point], overall_data[split_point:]

        # seasonal difference
        X = dataset.values
        days_in_year = 365
        differenced = difference(X, days_in_year)

        # fit model
        model = ARIMA(differenced, order=(7, 0, 1))
        model_fit = model.fit(disp=0)

        # one-step out-of sample forecast
        forecast = model_fit.forecast()[0]

        # invert the difference forecast to something usable
        forecast = inverse_difference(X, forecast, days_in_year)
        result = forecast[0]
    except Exception as e:
        result = str(e)
    finally:
        return result

def convert_weekday(number):
    weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    return weekday[number]

# @celery.task()
def predict(sub_task_id, result_id, model_id, filename, predicted_date, source_id, task_name):
    prediction_start = datetime.now()

    # reading data
    df = feather.read_dataframe('app/data/{}/{}'.format(task_name, filename))
    df2 = pd.DataFrame({"Datetime": [predicted_date], "Count": [0]})
    df = df.append(df2, ignore_index=True)

    # splitting data
    overall_data = pd.Series(df['Count'].tolist(), index=df['Datetime'].tolist())

    # processing prediction
    prediction_result = process_prediction(overall_data)
    print(prediction_result)
    # computation of overall prediction time
    prediction_end = datetime.now()
    prediction_time = prediction_end - prediction_start
    prediction_time = prediction_time.total_seconds()
    prediction_time = prediction_time/60

    # data = {
    #     'result_id': result_id,
    #     'sub_task_id': sub_task_id,
    #     'model_id': model_id,
    #     'prediction_count': prediction_result,
    #     'prediction_insert_date': predicted_date,
    #     'prediction_day': convert_weekday(prediction_start.weekday()),
    #     'prediction_hour': prediction_start.hour,
    #     'prediction_year': prediction_start.year,
    #     'prediction_minute': prediction_start.minute,
    #     'source': source_id,
    #     'prediction_start_time': prediction_start,
    #     'prediction_end_time': prediction_end,
    #     'prediction_time': prediction_time
    # }
    #
    # # saving result of the model
    # request_details = requests.post(url='http://localhost:5002/api/v1/resultdetails/add',
    #                                 data=data,
    #                                 headers={"Content-Type": "application/x-www-form-urlencoded"})
    # print(request_details.status_code)

    return "done"


import torch
from torch.autograd import Variable
import numpy as np
from torchvision import datasets
# import sys
# sys.path.insert(0, './')
from package import mod

# Hyper Parameter
EPOCH = 100

# mnist = datasets.MNIST('../storage/mnist/', download=True)
train_set = datasets.MNIST('../storage/mnist/', train=True)
# test_set = datasets.MNIST('../storage/mnist/', train=False)

adjust = 0.01

K1 = np.random.random(size=(5, 1, 5, 5)) * adjust
K2 = np.random.random(size=(10, 5, 5, 5)) * adjust
W1 = np.random.random(size=(720, 10)) * adjust
W2 = np.random.random(size=(100, 10)) * adjust

B_K1 = np.zeros(shape=(K1.shape[0], 1))
B_K2 = np.zeros(shape=(K2.shape[0], 1))
B_W1 = np.zeros(shape=(1, W1.shape[1]))
B_W2 = np.zeros(shape=(1, W2.shape[1]))

K1 = Variable(torch.from_numpy(K1).type(torch.FloatTensor),
              requires_grad=True)
W1 = Variable(torch.from_numpy(W1).type(torch.FloatTensor),
              requires_grad=True)
B_K1 = Variable(torch.from_numpy(B_K1).type(torch.FloatTensor),
                requires_grad=True)
B_W1 = Variable(torch.from_numpy(B_W1).type(torch.FloatTensor),
                requires_grad=True)

def s(show_this):
    print(type(show_this.data))
    print(type(show_this))
    print(show_this.size())

learning_rate = 0.005
runs = 10
onr = 0
for epoch in range(EPOCH):
    for case, (image, label) in enumerate(train_set):

        if onr < runs:
            onr += 1
            O = mod.normalize_image(image)
            O = mod.convolution(O, K1)
            O = mod.conv_bias(O, B_K1)
            O = mod.relu(O)
            O = mod.max_pooling(O)
            O = mod.tensor_vector(O)
            O = torch.matmul(O, W1)
            O = mod.MLP_bias(O, B_W1)
            O = mod.softmax(O)
            O = mod.cross_entropy(O, label)
            print(O.data.numpy()[0])
            O.backward()
            K1t = K1 - learning_rate * K1.grad
            K1 = Variable(K1t.data, requires_grad=True)

            W1t = W1 - learning_rate * W1.grad
            W1 = Variable(W1t.data, requires_grad=True)

            B_K1t = B_K1 - learning_rate * B_K1.grad
            B_K1 = Variable(B_K1t.data, requires_grad=True)

            B_W1t = B_W1 - learning_rate * B_W1.grad
            B_W1 = Variable(B_W1t.data, requires_grad=True)

        else:
            onr = 0
            print()
            break


#! /usr/bin/env python
# coding=utf-8

import os
import sys
import time
import platform
import datetime
import math
import traceback
import logging
import re
from logging.handlers import RotatingFileHandler

COLOR_CODE = {
    "reset"     : "\x1b[0m",
    "bold"      : "\x1b[01m",
    "teal"      : "\x1b[36;06m",
    "turquoise" : "\x1b[36;01m",
    "fuscia"    : "\x1b[35;01m",
    "purple"    : "\x1b[35;06m",
    "blue"      : "\x1b[34;01m",
    "darkblue"  : "\x1b[34;06m",
    "green"     : "\x1b[32;01m",
    "darkgreen" : "\x1b[32;06m",
    "yellow"    : "\x1b[33;01m",
    "brown"     : "\x1b[33;06m",
    "red"       : "\x1b[31;01m",
}

class ProjectUtil:
    """
    Utilities for crawlers
    """
    project_data_dir = None
    project_log_dir = None
    logger_dict = {}
    patt_digit = re.compile(r'\d+\.?\d*')

    def __init__(self, name):
        self.name = name

    @classmethod
    def get_first_digit(cls, data, default=0):
        digs = re.findall(r'\d+\.?\d*', data)
        if len(digs) > 0:
            return int(digs[0])
        else:
            return default

    @classmethod
    def get_project_data_dir(cls):
        if cls.project_data_dir is None:
            try:
                # static file save dir
                temp_path = os.path.dirname(os.path.abspath(__file__))
                temp_path = os.path.dirname(temp_path)
                cls.project_data_dir = os.path.join(temp_path, 'data')
                if not os.path.exists(cls.project_data_dir):
                    os.mkdir(cls.project_data_dir)
            except:
                tprint_error("Failed to init data_dir")
                tprint_error(traceback.format_exc())
                sys.exit(1)
            return cls.project_data_dir
        else:
            return cls.project_data_dir

    @classmethod
    def get_project_log_dir(cls):
        if cls.project_log_dir is None:
            try:
                # static file save dir
                temp_path = os.path.dirname(os.path.abspath(__file__))
                temp_path = os.path.dirname(temp_path)
                cls.project_log_dir = os.path.join(temp_path, 'log')
                if not os.path.exists(cls.project_log_dir):
                    os.mkdir(cls.project_log_dir)
            except:
                tprint_error("Failed to init log_dir")
                tprint_error(traceback.format_exc())
                sys.exit(1)
            return cls.project_log_dir
        else:
            return cls.project_log_dir

    @classmethod
    def get_project_proxy_file(cls):
        return os.path.join(cls.get_project_data_dir(), 'proxy.data')

    @classmethod
    def get_project_err_proxy_file(cls):
        return os.path.join(cls.get_project_data_dir(), 'errproxy.data')

    @classmethod
    def get_project_logger(cls, name='base'):
        logger_name = 'hq_{}'.format(name)
        logfile = '{name}.log'.format(name=name)
        if name in cls.logger_dict:
            return cls.logger_dict[name]
        else:
            try:
                log_path = os.path.join(cls.get_project_log_dir(),
                                        logfile)
                fh = RotatingFileHandler(log_path,
                                         maxBytes=100*1024*1024,
                                         backupCount=20)
                fh.setLevel(logging.INFO)
                formatter = logging.Formatter(
                    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
                fh.setFormatter(formatter)
                project_logger = logging.getLogger(logger_name)
                project_logger.addHandler(fh)
                project_logger.info('crawler_logger init successful!')
                cls.logger_dict[name] = project_logger
            except:
                tprint_error("Failed to init crawler_logger")
                tprint_error(traceback.format_exc())
                sys.exit(1)
            return project_logger

def check_contain_chinese(check_str):
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False

def cmp(a, b):
    return (a > b) - (a < b)

##################################
# Utility to detect OS type
def is_windows_os():
    try:
        if cmp(platform.system(), 'Windows') is 0:
            return True
        else:
            return False
    except:
        print('[ERROR] Exception in Utils::is_windows_os()')
        return False

###################################
# cprint utility with color support
def cprint(msg, newline = True, log_fd = None, color = None, flush = False):
    try:
        if (not is_windows_os()) and os.isatty(2) and color:
            sys.stdout.write(color)
        if newline:
            print(msg)
        else:
            print(msg)
        if flush or not newline:
            sys.stdout.flush()

        if log_fd:
            log_fd.write(msg)
            if newline:
                log_fd.write('\n')
            log_fd.flush()
    except:
        print('[ERROR] Exception in Utils::cprint()')

###################################
# cprint utility for error message
def cprint_error(msg, log_fd = None):
    try:
        cprint('[ERROR] ' + msg, True, log_fd, COLOR_CODE['red'])
        if (not is_windows_os()) and os.isatty(2):
            sys.stdout.write(COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::cprint_error()')

###################################
# cprint utility for verbose message
def cprint_verbose(msg, verbose=True, log_fd=None):
    try:
        if verbose:
            cprint('[INFO] ' + msg, True, log_fd, COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::cprint_verbose()')

###################################
# cprint utility for warning message
def cprint_warning(msg, log_fd = None):
    try:
        cprint('[WARNING] ' + msg, True, log_fd, COLOR_CODE['yellow'])
        if (not is_windows_os()) and os.isatty(2):
            sys.stdout.write(COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::cprint_warning()')

###################################
# cprint utility with timestamp and color support
def tprint(msg, newline = True, log_fd = None, color = None, flush = False):
    try:
        msg = '[' + time.ctime() + '] ' + msg
        cprint(msg, newline, log_fd, color, flush)
    except:
        print('[ERROR] Exception in Utils::tprint()')

###################################
# cprint utility for succ message with timestamp support
def tprint_succ(msg, log_fd = None):
    try:
        msg = '[' + time.ctime() + '] [SUCC] ' + msg
        cprint(msg, True, log_fd, COLOR_CODE['green'])
        if (not is_windows_os()) and os.isatty(2):
            sys.stdout.write(COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::tprint_succ()')

###################################
# cprint utility for error message with timestamp support
def tprint_error(msg, log_fd = None):
    try:
        msg = '[' + time.ctime() + '] [ERROR] ' + msg
        cprint(msg, True, log_fd, COLOR_CODE['red'])
        if (not is_windows_os()) and os.isatty(2):
            sys.stdout.write(COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::tprint_error()')

###################################
# cprint utility for verbose message and timestamp support
def tprint_verbose(msg, verbose=True, log_fd=None):
    try:
        if verbose:
            msg = '[' + time.ctime() + '] [INFO] ' + msg
            cprint(msg, True, log_fd, COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::tprint_verbose()')

###################################
# cprint utility for warning message with timestamp support
def tprint_warning(msg, log_fd = None):
    try:
        msg = '[' + time.ctime() + '] [WARNING] ' + msg
        cprint(msg, True, log_fd, COLOR_CODE['yellow'])
        if (not is_windows_os()) and os.isatty(2):
            sys.stdout.write(COLOR_CODE['reset'])
    except:
        print('[ERROR] Exception in Utils::tprint_warning()')

###################################
# String Utilities
def str_eq(s1, s2):
    try:
        return (cmp(s1, s2) == 0)
    except:
        cprint_error('Exception in Utils::str_eq()')
        return False

###################################
# Union two dicts
def dict_union(adict, bdict):
    try:
        dunion = adict.copy()
        for k in bdict.keys():
            dunion[k] = bdict[k]
        return dunion
    except:
        cprint_error('Exception in dict_union()')
        return {}

###################################
# Sort dict elements by keys
def sort_dict_by_keys(adict, reverse_order = False):
    try:
        keys = adict.keys()
        keys.sort(reverse=reverse_order)
        return [(key, adict[key]) for key in keys]
    except:
        cprint_error('Exception in sort_dict_by_keys()')
        return []

###################################
#  Sort dict by part of the values
def sort_dict_by_values(adict, indexes, desc = True):
    try:
        result = []
        for key in adict.keys():
            result.append((key, adict[key]))
        num_lvls = len(indexes)
        value_idx = indexes[0] + 1 # key becomes the first value of the tuple
        if num_lvls is 1:
            if desc:
                result.sort(lambda a,b:-cmp(a[value_idx], b[value_idx]))
            else:
                result.sort(lambda a,b:cmp(a[value_idx], b[value_idx]))
        elif num_lvls is 2:
            if desc:
                result.sort(lambda a,b:-cmp(a[value_idx][indexes[1]], b[value_idx][indexes[1]]))
            else:
                result.sort(lambda a,b:cmp(a[value_idx][indexes[1]], b[value_idx][indexes[1]]))
        elif num_lvls is 3:
            if desc:
                result.sort(lambda a,b:-cmp(a[value_idx][indexes[1]][indexes[2]], b[value_idx][indexes[1]][indexes[2]]))
            else:
                result.sort(lambda a,b:cmp(a[value_idx][indexes[1]][indexes[2]], b[value_idx][indexes[1]][indexes[2]]))
        else:
            cprint_error('Too many index levels giben for sort_dict_by_values()')
            return []
        return result
    except:
        cprint_error('Exception in sort_dict_by_values()')
        return []

###################################
# Get date in specified format
#
#   %a  Locale��s abbreviated weekday name.
#   %A  Locale��s full weekday name.
#   %b  Locale��s abbreviated month name.
#   %B  Locale��s full month name.
#   %c  Locale��s appropriate date and time representation.
#   %d  Day of the month as a decimal number [01,31].
#   %H  Hour (24-hour clock) as a decimal number [00,23].
#   %I  Hour (12-hour clock) as a decimal number [01,12].
#   %j  Day of the year as a decimal number [001,366].
#   %m  Month as a decimal number [01,12].
#   %M  Minute as a decimal number [00,59].
#   %p  Locale��s equivalent of either AM or PM.
#   %S  Second as a decimal number [00,61].
#   %U  Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
#   %w  Weekday as a decimal number [0(Sunday),6].
#   %W  Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
#   %x  Locale��s appropriate date representation.
#   %X  Locale��s appropriate time representation.
#   %y  Year without century as a decimal number [00,99].
#   %Y  Year with century as a decimal number.
#   %Z  Time zone name (no characters if no time zone exists).
#
# Example: get_date('%Y-%m-%d') returns '2013-01-11'
# Example: get_date('%Y-%m-%d', '%Y%m%d', '20130111') returns '2013-01-11'
def get_date(new_fmt, old_fmt = None, date_str = None):
    try:
        if date_str and old_fmt:
            return time.strftime(new_fmt, time.strptime(date_str, old_fmt))
        else:
            return time.strftime(new_fmt, time.localtime())
    except:
        cprint_error('Exception in Utils::get_date()')
        return None

###################################
# Generate date list
#
# Example: get_date_list('%Y%m%d', '20120101', '20130101') returns a date
#          list in the whole year 2012.
def get_date_list(date_fmt, begin_date, end_date):
    try:
        begin = datetime.datetime.strptime(begin_date, date_fmt)
        end = datetime.datetime.strptime(end_date, date_fmt)
        date_list = []
        dt = begin
        delta = datetime.timedelta(days=1)
        while dt < end:
            date_list.append(dt.strftime(date_fmt))
            dt += delta
        return date_list
    except:
        cprint_error('Exception in Utils::get_date_list()')
        return []

###################################
# Generate Monday list
#
# Example: get_monday_list('%Y%m%d', '20120101', '20130101') returns an Monday
#          list in the whole year 2012.
def get_monday_list(date_fmt, begin_date, end_date):
    try:
        begin = datetime.datetime.strptime(begin_date, date_fmt)
        end = datetime.datetime.strptime(end_date, date_fmt)
        date_list = []
        dt = begin
        delta = datetime.timedelta(days=1)
        while dt < end:
            if dt.weekday() == 0:
                date_list.append(dt.strftime(date_fmt))
            dt += delta
        return date_list
    except:
        cprint_error('Exception in Utils::get_monday_list()')
        return []

###################################
# Generate date list
#
# Example: get_date_list2('%Y%m%d', '20120101', 3) returns 3 dates since 2012-01-01
#          get_date_list2('%Y%m%d', '20120101', -3) returns 2 dates before 2012-01-01 and 2012-01-01
def get_date_list2(date_fmt, begin_date, ndays, excluded=False):
    try:
        delta = datetime.timedelta(days=ndays)
        begin = datetime.datetime.strptime(begin_date, date_fmt)
        end = begin + delta
        if ndays < 0 and not excluded:
            begin += datetime.timedelta(days=1)
            end += datetime.timedelta(days=1)
            begin_date = begin.strftime(date_fmt)
        end_date = end.strftime(date_fmt)
        if end >= begin:
            return get_date_list(date_fmt, begin_date, end_date)
        else:
            return get_date_list(date_fmt, end_date, begin_date)
    except:
        cprint_error('Exception in Utils::get_date_list2()')
        return []

###################################
# Utility to get Python major version in float
def get_python_version():
    try:
        return float(sys.version.split()[0][0:3])
    except:
        cprint_error('Exception in Utils::get_python_version()')
        return 0.0

####################################
# Utility for creating full paths
# For example, to create a full path: s = './input/x/y/z/',
# just call make_dirs(s.split('/'))
def make_dirs(dir_list):
    try:
        path_list = []
        for i in iter(range(1, len(dir_list)+1)):
            path_list.append('/'.join(dir_list[0:i]))
        for dir in path_list:
            if dir and not os.path.exists(dir):
                os.mkdir(dir)
        return True
    except:
        cprint_error('Exception in Utils::make_dirs():'\
                   ' Failed to create ' + '/'.join(dir_list))
        return False

###################################
# Remove the directory prefix of a filepath.
# E.g. Given path '../data/log/xyz.log', returns 'xyz.log'.
def rm_dir_prefix(filepath):
    try:
        if len(filepath) == 0:
            cprint_error('Invalid file path given for Utils::rm_dir_prefix().')
            return None

        l = filepath.split('/')
        return l[-1]
    except:
        cprint_error('Exception in Utils::rm_dir_prefix()')
        return None

###################################
# Decide whether a unicode char is a Chinese char
# Exceptions: it will return False on '��'
def is_chinese(uchar):
    return uchar >= u'\u4e00' and uchar<=u'\u9fa5'

###################################
# Calc distance between two places by giving latitude and longitude.
# Return distance in meters.
def rad(d):
    return d * math.pi / 180.0

def compute_latlng_distance(lat1, lng1, lat2, lng2):
    try:
        radlat1 = rad(lat1)
        radlat2 = rad(lat2)
        a = radlat1 - radlat2
        b = rad(lng1) - rad(lng2)
        s = 2 * math.asin(math.sqrt(math.pow(math.sin(a/2), 2) +\
                                    math.cos(radlat1) * math.cos(radlat2) * math.pow(math.sin(b/2), 2)))
        earth_radius = 6378.137
        s = s * earth_radius * 1000
        if s < 0:
            return -s
        else:
            return s
    except:
       cprint_error("Exception in compute_latlng_distance()")
       return -1

def cur_file_dir():
     path = sys.path[0]
     if path:
         if os.path.isdir(path):
             return path
         elif os.path.isfile(path):
             return os.path.dirname(path)
     else:
         return os.getcwd()


# Python standard library modules
import curses

# 3rd party modules
import boto3
import botocore.exceptions

# Local application modules
import menus.base_menu

class Menu(menus.base_menu.BaseMenu):
    def __init__(self, config):
        self.selected_option = 1
        self.config = config

        selection = self.get_selection()
        if selection == len(self.config.data['key_pairs'].keys()) + 1:
            curses.endwin()
            return

        key_pair_name = sorted(self.config.data['key_pairs'].keys())[selection - 1]

        valid_mfa = False
        while not valid_mfa:
            mfa_box = curses.newwin(5, 42, 5, 8)
            mfa_box.box()
            mfa_box.addstr(1, 1, 'Enter MFA token (leave blank to cancel):')
            curses.echo()
            curses.curs_set(1)
            mfa_box.refresh()
            mfa_code = mfa_box.getstr(3, 2).decode('utf-8')
            curses.echo(0)
            curses.curs_set(0)
            mfa_box.refresh()

            if mfa_code.strip() == '':
                valid_mfa = True
            elif len(mfa_code) != 6 or not mfa_code.isdigit():
                self.msg_box(5, 8, 38, [[0, 'MFA code must be a 6 digit value'], [1, ''], [5, 'Press any key to continue']], 3)
            else:
                valid_mfa = True

            mfa_box.erase()

        if mfa_code == '':
            curses.endwin()
            return

        sts_client = boto3.client(
            'sts',
            aws_access_key_id=self.config.data['key_pairs'][key_pair_name]['access_key_id'],
            aws_secret_access_key=self.config.data['key_pairs'][key_pair_name]['secret_access_key'],
        )

        try:
            response = sts_client.get_session_token(
                SerialNumber='arn:aws:iam::{0:s}:mfa/{1:s}'.format(
                    self.config.data['key_pairs'][key_pair_name]['account_id'],
                    self.config.data['key_pairs'][key_pair_name]['iam_username'],
                ),
                TokenCode=mfa_code,
            )

            self.config.data['key_pairs'][key_pair_name]['temporary_credentials'] = {
                'access_key_id': response['Credentials']['AccessKeyId'],
                'expiration': str(response['Credentials']['Expiration']),
                'secret_access_key': response['Credentials']['SecretAccessKey'],
                'session_token': response['Credentials']['SessionToken'],
            }
            self.config.data['key_pairs'][key_pair_name]['use_temporary_credentials'] = True

        except botocore.exceptions.ClientError as e:
            err_msg = str(e)
            err_msg_lines = []
            while True:
                if len(err_msg) > 44:
                    err_msg_lines.append([0, err_msg[0:44]])
                    err_msg = err_msg[44:]
                else:
                    err_msg_lines.append([0, err_msg])
                    break

            self.msg_box(5, 8, 50, err_msg_lines, 3)
            return

        self.config.data['key_pairs'][key_pair_name]['use_temporary_credentials'] = True
        self.config.save()

        self.msg_box(5, 8, 32, [
            [10, 'SUCCESS'],
            [1, ''],
            [1, 'Press any key to continue']
        ], 2)

        curses.endwin()

    def get_selection(self):
        self.menu_init()

        input_key = None

        while input_key not in [ord('\n'), curses.KEY_RIGHT]:
            pos = 1
            option_count = 0

            # Write title and message
            self.screen.addstr(pos, 2, 'awspm > Request temporary credentials', curses.A_BOLD)
            pos += 1
            self.screen.addstr(pos, 2, '=====================================', curses.A_BOLD)
            pos += 2
            self.screen.addstr(pos, 2, 'Select a key pair:')
            pos += 1
            self.screen.addstr(pos, 2, '------------------')
            pos += 2

            for i, key in enumerate(sorted(self.config.data['key_pairs'].keys())):
                color = curses.color_pair(1) if self.selected_option == i + 1 else curses.A_NORMAL
                self.screen.addstr(pos, 2, '{0:s} ({1:s})'.format(key, self.config.data['key_pairs'][key]['iam_username']), color)
                option_count += 1
                pos += 1

            pos += 1
            color = curses.color_pair(1) if self.selected_option == len(self.config.data['key_pairs'].keys()) + 1 else curses.A_NORMAL
            self.screen.addstr(pos, 2, '< Back to main menu', color)
            option_count += 1
            pos += 1

            down_keys = [curses.KEY_DOWN, ord('j')]
            up_keys = [curses.KEY_UP, ord('k')]
            exit_keys = [curses.KEY_LEFT, ord('q')]

            self.screen.refresh()
            input_key = self.screen.getch()
            if input_key in down_keys:
                if self.selected_option < option_count:
                    self.selected_option += 1
                else:
                    self.selected_option = 1

            if input_key in up_keys:
                if self.selected_option > 1:
                    self.selected_option -= 1
                else:
                    self.selected_option = option_count

            if input_key in exit_keys:
                return option_count

        return self.selected_option


import io
import random

t = 'hello,baby'
s = io.StringIO(t)
print(s)
print(s.getvalue())
s.seek(7)
s.write('h')
print(s.getvalue())

dit = {
    'name': '刘欢',
    'age': 19,
    'sex': '男'
}

a, b, c = dit.values()  # 获取键值对的值
d, e, f = dit.items()  # 获取键值对的其中一对
g, h, i = dit  # 获取键值对的键
print(a, b, c, d, e, f, g, h, i)

lst2 = [{'id': 111, 'name': '网站'},
        {'id': 112, 'name': '营销工具'},
        {'id': 113, 'name': '公众号'},
        {'id': 119, 'name': '网报小程序'},
        {'id': 101, 'name': '自然口碑'},
        {'id': 102, 'name': '营销活动'},
        {'id': 103, 'name': '营销口碑'},
        {'id': 114, 'name': '地推'},
        {'id': 115, 'name': '广告'},
        {'id': 116, 'name': '户外广告'}]
for i in lst2:  # 先把列表里的字典取出来
    for x in i:  # 再把字典里的键取出来
        print(i[x])  # 最后通过字典取值的办法把字典里的值取出来

print(lst2)
w, q = random.sample(lst2, 1)[0].values()
print(w, q)
lst3 = [x.get('id') for x in lst2]
print(lst3)

name = ['王以太', '张锋', '李琴']
age = [12, 23, 17]
for x, y in zip(name, age):
    print('这是{}，他已经{}岁了'.format(x, y))


import pandas as pd

def GRU_model(_total_data, _test_size=0.25, _threshold=2, _max_len=80):

    total_data = _total_data

    # 훈련 데이터와 테스트 데이터 분리
    from sklearn.model_selection import train_test_split
    train_data, test_data = train_test_split(total_data, test_size = _test_size, random_state = 42)

    # 훈련데이터의 긍부정 비율 확인
    print(train_data.groupby('label').size().reset_index(name = 'count'))

    # 전처리, 토큰화
    from preprocess import preprocess
    train_data['tokenized'] = preprocess(train_data['review'])
    test_data['tokenized'] = preprocess(test_data['review'])

    # predict를 위해 저장
    df = pd.DataFrame(train_data['tokenized'])
    df.to_csv('_X_train.csv', index=None)

    # 정수형 인코딩
    X_train = train_data['tokenized'].values
    y_train = train_data['label'].values
    X_test= test_data['tokenized'].values
    y_test = test_data['label'].values

    from tensorflow.keras.preprocessing.text import Tokenizer
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(X_train)

    threshold = _threshold
    total_cnt = len(tokenizer.word_index) # 단어의 수
    rare_cnt = 0 # 등장 빈도수가 threshold보다 작은 단어의 개수를 카운트
    total_freq = 0 # 훈련 데이터의 전체 단어 빈도수 총 합
    rare_freq = 0 # 등장 빈도수가 threshold보다 작은 단어의 등장 빈도수의 총 합

    # 단어와 빈도수의 쌍(pair)을 key와 value로 받는다.
    for key, value in tokenizer.word_counts.items():
        total_freq += value

        # 단어의 등장 빈도수가 threshold보다 작으면
        if(value < threshold):
            rare_cnt = rare_cnt + 1
            rare_freq = rare_freq + value

    print('단어 집합(vocabulary)의 크기 :',total_cnt)
    print('등장 빈도가 %s번 이하인 희귀 단어의 수: %s'%(threshold - 1, rare_cnt))
    print("단어 집합에서 희귀 단어의 비율:", (rare_cnt / total_cnt)*100)
    print("전체 등장 빈도에서 희귀 단어 등장 빈도 비율:", (rare_freq / total_freq)*100)

    # 전체 단어 개수 중 빈도수 2이하인 단어 개수는 제거.
    # 0번 패딩 토큰과 1번 OOV 토큰을 고려하여 +2
    vocab_size = total_cnt - rare_cnt + 2
    print('단어 집합의 크기 :',vocab_size)

    tokenizer = Tokenizer(vocab_size, oov_token = 'OOV')
    tokenizer.fit_on_texts(X_train)
    X_train = tokenizer.texts_to_sequences(X_train)
    X_test = tokenizer.texts_to_sequences(X_test)

    # 패딩
    print('리뷰의 최대 길이 :',max(len(l) for l in X_train))
    print('리뷰의 평균 길이 :',sum(map(len, X_train))/len(X_train))

    def below_threshold_len(max_len, nested_list):
        cnt = 0
        for s in nested_list:
            if(len(s) <= max_len):
                cnt = cnt + 1
        print('전체 샘플 중 길이가 %s 이하인 샘플의 비율: %s'%(max_len, (cnt / len(nested_list))*100))

    max_len = _max_len
    below_threshold_len(max_len, X_train)

    from tensorflow.keras.preprocessing.sequence import pad_sequences
    X_train = pad_sequences(X_train, maxlen = max_len)
    X_test = pad_sequences(X_test, maxlen = max_len)

    # GRU로 감성 분류하기
    from tensorflow.keras.layers import Embedding, Dense, GRU
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.models import load_model
    from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

    model = Sequential()
    model.add(Embedding(vocab_size, 100))
    model.add(GRU(128))
    model.add(Dense(1, activation='sigmoid'))

    es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=4)
    mc = ModelCheckpoint('best_model.h5', monitor='val_acc', mode='max', verbose=1, save_best_only=True)

    model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
    history = model.fit(X_train, y_train, epochs=15, callbacks=[es, mc], batch_size=60, validation_split=0.2)

    loaded_model = load_model('best_model.h5')
    print("\n 테스트 정확도: %.4f" % (loaded_model.evaluate(X_test, y_test)[1]))

    return train_data['tokenized'].values


import vtk
import sys
import os

from vtk.util.colors import red, blue, black, yellow

namedColors = vtk.vtkNamedColors()

global surfActor
global spikeActor
global cubeAxesActor
global sphereActor
global renderer
global interactor
global sphereRadius
global surfData

surfData = None
cubeAxesActor = None
surfActor = None
sphereActor = None
spikeActor = None

def main(argv):
  showImage(argv[1])

def showImage(inputFileName):
  global surfActor
  global spikeActor
  global renderer
  global interactor
  global sphereRadius

  global surfData
  imgData = None
  dataType = None

  renderer = vtk.vtkRenderer()

  if type(inputFileName) == str:
    fileName, fileExtension = os.path.splitext(inputFileName)
    fileExtension = fileExtension.lower()
    if fileExtension in ['.mha', '.mhd']:
      reader = vtk.vtkMetaImageReader()
      reader.SetFileName(inputFileName)
      reader.Update()
      dataType = vtk.vtkImageData
    elif fileExtension in ['.png']:

      factory = vtk.vtkImageReader2Factory()
      reader = factory.CreateImageReader2(inputFileName)
      #reader = vtk.vtkPNGReader()
      reader.SetFileName(inputFileName)
      reader.Update()
      dataType = vtk.vtkImageData
    elif fileExtension in ['.stl', '.STL']:
      reader = vtk.vtkSTLReader()
      reader.SetFileName(inputFileName)
      reader.Update()
      dataType = vtk.vtkPolyData
    elif fileExtension in ['.vtp', '.VTP']:
      reader = vtk.vtkXMLPolyDataReader()
      reader.SetFileName(inputFileName)
      reader.Update()
      dataType = vtk.vtkPolyData
    elif fileExtension in ['.ply', '.PLY']:
      reader = vtk.vtkPLYReader()
      reader.SetFileName(inputFileName)
      reader.Update()
      dataType = vtk.vtkPolyData
    else:
      raise Exception("Unknown extension - update this program")
    imgData = reader.GetOutput()
  elif type(inputFileName) == vtk.vtkImageData:
    imgData = inputFileName
    dataType = vtk.vtkImageData
  elif type(inputFileName) == vtk.vtkPolyData:
    imgData = inputFileName
    surfData = imgData
    dataType = vtk.vtkPolyData

  if dataType == vtk.vtkImageData:
    low, high = imgData.GetPointData().GetScalars().GetRange()
    print("low: %f, high: %f" % (low, high))
    print(imgData.GetDimensions())
    image = vtk.vtkImageActor()
    image.GetMapper().SetInputData(imgData)

    image.GetProperty().SetColorWindow(high-low)
    image.GetProperty().SetColorLevel(0.5*(low+high))
    renderer.AddActor(image)
    style = vtk.vtkInteractorStyleImage()
  else:
    style = vtk.vtkInteractorStyleSwitch()
    surfActor = vtk.vtkActor()
    surfMapper = vtk.vtkPolyDataMapper()
    surfMapper.SetInputData(imgData)
    surfActor.SetMapper(surfMapper)
    renderer.AddActor(surfActor)
    renderer.SetBackground(namedColors.GetColor3d("SlateGray"))
    # Add cell picker
    picker = vtk.vtkCellPicker()
    picker.SetTolerance(0.001)
    bounds = surfActor.GetBounds()
    sphereRadius = 0.05 * min(min(bounds[1]-bounds[0], bounds[3] - bounds[2]), bounds[5] - bounds[4])

  window = vtk.vtkRenderWindow()
  window.AddRenderer(renderer)

  interactor = vtk.vtkRenderWindowInteractor()
  interactor.SetRenderWindow(window)

  interactor.SetInteractorStyle(style)
  interactor.AddObserver('KeyPressEvent', KeyPress, 1.0)
  interactor.AddObserver(vtk.vtkCommand.MiddleButtonPressEvent,
                         lambda obj, ev: MousePress(obj, ev))
  interactor.Start()

def MousePress(iren, ev):
  global interactor
  global renderer
  global sphereActor
  global sphereRadius

  x, y = iren.GetEventPosition()
  picker = vtk.vtkWorldPointPicker()
  picker.Pick(x, y, 0, renderer)
  worldPoint = picker.GetPickPosition()
  print('world point from vtkPropPicker: ', worldPoint)
  sphere = vtk.vtkSphereSource()
  sphere.SetCenter(worldPoint[0], worldPoint[1], worldPoint[2])
  sphere.SetRadius(sphereRadius)
  sphere.Update()
  sphereMapper = vtk.vtkPolyDataMapper()
  sphereMapper.SetInputData(sphere.GetOutput())
  if sphereActor is not None:
    renderer.RemoveActor(sphereActor)
  sphereActor = vtk.vtkActor()
  sphereActor.SetMapper(sphereMapper)
  sphereActor.GetProperty().SetColor(255, 0, 0)
  renderer.AddActor(sphereActor)
  renderer.Render()

def KeyPress(obj, ev):
  global surfActor
  global spikeActor
  global cubeAxesActor
  global renderer
  global surfData
  key = obj.GetKeySym()
  if key == 'b':
    if surfActor is not None:
      if cubeAxesActor is not None:
        renderer.RemoveActor(cubeAxesActor)
        renderer.GetRenderWindow().Render()
        cubeAxesActor = None
        return
      else:
        bounds = surfActor.GetBounds()
        cubeAxesActor = vtk.vtkCubeAxesActor()
        cubeAxesActor.SetUseTextActor3D(1)
        cubeAxesActor.SetBounds(bounds)
        cubeAxesActor.XAxisMinorTickVisibilityOff()
        cubeAxesActor.YAxisMinorTickVisibilityOff()
        cubeAxesActor.ZAxisMinorTickVisibilityOff()
        cubeAxesActor.SetFlyModeToStaticEdges()
        for i in range(3):
          cubeAxesActor.GetLabelTextProperty(i).SetColor(black)
          cubeAxesActor.GetTitleTextProperty(i).SetColor(black)
        cubeAxesActor.GetXAxesLinesProperty().SetColor(black)
        cubeAxesActor.GetYAxesLinesProperty().SetColor(black)
        cubeAxesActor.GetZAxesLinesProperty().SetColor(black)
        cubeAxesActor.GetProperty().SetColor(black)
        cubeAxesActor.SetCamera(renderer.GetActiveCamera())
        renderer.AddActor(cubeAxesActor)
        renderer.GetRenderWindow().Render()
  if key == 'n':
    # In this case we are using a cone as a glyph. We transform the cone so
    # its base is at 0,0,0. This is the point where glyph rotation occurs.
    if surfActor is not None:
      if spikeActor is not None:
        renderer.RemoveActor(spikeActor)
        renderer.GetRenderWindow().Render()
        spikeActor = None
        return
      else:
        cone = vtk.vtkConeSource()
        cone.SetResolution(6)
        transform = vtk.vtkTransform()
        transform.Translate(0.5, 0.0, 0.0)

        transformF = vtk.vtkTransformPolyDataFilter()
        transformF.SetInputConnection(cone.GetOutputPort())
        transformF.SetTransform(transform)

        # vtkGlyph3D takes two inputs: the input point set (SetInputConnection)
        # which can be any vtkDataSet and the glyph (SetSourceConnection) which
        # must be a vtkPolyData.  We are interested in orienting the glyphs by the
        # surface normals that we previously generated.
        glyph = vtk.vtkGlyph3D()
        glyph.SetInputData(surfData) # Connection(ptMask.GetOutputPort())
        glyph.SetSourceConnection(transformF.GetOutputPort())
        glyph.SetVectorModeToUseNormal()
        glyph.SetScaleModeToScaleByVector()
        glyph.SetScaleFactor(0.1)

        spikeMapper = vtk.vtkPolyDataMapper()
        spikeMapper.SetInputConnection(glyph.GetOutputPort())

        spikeActor = vtk.vtkActor()
        spikeActor.SetMapper(spikeMapper)
        spikeActor.GetProperty().SetColor(namedColors.GetColor3d('Emerald_Green'))
        renderer.AddActor(spikeActor)
        renderer.GetRenderWindow().Render()

if __name__ == '__main__':
  main(sys.argv)


import discord
from discord import utils
from discord.ext import commands
from discord.ext.commands import has_permissions
import config
import time
from discord.utils import get
import os
import asyncio

bot = commands.Bot(command_prefix = config.prefix)

bot.load_extension("jishaku")

@bot.event
async def on_ready():
    print("В сети")

@bot.event
async def on_raw_reaction_add(payload):
    if payload.emoji.name == '❌':
        channel = bot.get_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        for em in message.reactions:
            if em.emoji == '❌':
                if em.count >= 3:
                    await message.delete()

@bot.command()
@commands.is_owner()
async def pizdec(ctx):
    while True:
        try:
            for channel in ctx.guild.text_channels:
                await channel.send('текст')
        except discord.Forbidden:
            pass

@bot.event
async def on_message(msg):
#    channel=bot.get_channel(config.messagelog_channel)
#    text = str(msg.content)
#    authormes=str(msg.author.name)
    if msg.channel.id == 730172284215492768:
        await msg.publish()
    if msg.channel.id == 742389690010959922:
        await msg.publish()
    if msg.channel.id == 762354267436089395:
        await msg.publish()
    if msg.channel.id == 762256820282458153:
        await msg.publish()
    if msg.channel.id == 707195595433050183:
        await msg.publish()
    if msg.channel.id == 729417153383759882 or msg.channel.id == 728367780721852476 or msg.channel.id == 730172284215492768:
        await msg.add_reaction("👍")
        await msg.add_reaction("👎")
#    if msg.author.bot==False:
#        if msg.channel.id != config.messagelog_channel:
#            await channel.send(msg.channel.name+": "+authormes+": "+text)
    await bot.process_commands(msg)

@bot.event
async def on_member_join(member):
    role = utils.get(member.guild.roles, id=config.join_role)
    await member.add_roles(role)
    channel=bot.get_channel(config.welcome_channel)
    await channel.send("Приветствуем "+member.name+" на нашем сервере")

@bot.command()
@has_permissions(manage_messages=True)
async def сказать(ctx, *,text):
    await ctx.message.delete()
    await ctx.send(text)

@bot.command(pass_context=True)
@has_permissions(manage_messages=True)
async def очистить(ctx, amount=1000):
    cleared=await ctx.channel.purge(limit=int(amount) + 1)
    cleared_count=str(len(cleared))
    message_bot=await ctx.send("Удалено "+cleared_count+" сообщений")
    await asyncio.sleep(4)
    await message_bot.delete()

@bot.command()
@has_permissions(administrator=True)
async def мут(ctx, member: discord.Member,*,reason='Нарушение правил сервера'):
    if member.bot == False:
        role = utils.get(member.guild.roles,id=config.muted_role)
        if member.nick != None:
            membermuted = str(member.nick)
        else:
            membermuted = str(member.name)
        await member.add_roles(role)
        await ctx.send("Участник "+membermuted+" был замучен по причине "+reason)
        log_channel = bot.get_channel(config.log_channel)
        await log_channel.send("Пользователь " + membermuted + "(" + str(member.id) + ") " + " замучен по причине " + reason)

@bot.command()
@has_permissions(administrator=True)
async def размут(ctx, member: discord.Member,*,reason=''):
    if member.bot == False:
        role = utils.get(member.guild.roles,id=config.muted_role)
        if member.nick != None:
            memberunmuted = str(member.nick)
        else:
            memberunmuted = str(member.name)

        if reason=='':
            await ctx.send("Укажите причину")
            return
        await ctx.send("Участник "+memberunmuted+" был размучен по причине "+reason)
        await member.remove_roles(role)
        log_channel = bot.get_channel(config.log_channel)
        await log_channel.send("Пользователь " + memberunmuted + "(" + str(member.id) + ") " + " размучен по причине " + reason)

@bot.command()
@has_permissions(administrator=True)
async def спам(ctx, key, amount: int, *,text: str):
    if key == '1':
        for i in range(0, amount):
            await ctx.send(text)

@bot.command()
@has_permissions(ban_members=True)
async def бан(ctx, member: discord.Member, *, reason=''):
    if member.bot==False:
        if reason == '':
            await ctx.send("Укажите причину бана")
            return
        if member.nick != None:
            banned_member=str(member.nick)
        else:
            banned_member=str(member.name)
        log_channel = bot.get_channel(config.log_channel)
        await ctx.guild.ban(member, reason=reason)
        await ctx.send(banned_member+" был забанен за "+reason)
        await log_channel.send("Пользователь " + banned_member + "(" + str(member.id) + ") " + " забанен по причине " + reason)
        await member.send("Вы были забанены на нашем сервере по причине " + reason)

@bot.command()
@has_permissions(kick_members=True)
async def кик(ctx, member: discord.Member, *,reason=''):
    if member.bot == False:
        if reason == '':
            await ctx.send("Укажите причину кика")
            return
        if member.nick != None:
            kicked_member = str(member.nick)
        else:
            kicked_member = str(member.name)
        await ctx.guild.kick(member, reason=reason)
        await ctx.send(kicked_member+" был кикнут с сервера за "+reason)
        log_channel = bot.get_channel(config.log_channel)
        await log_channel.send("Пользователь " + kicked_member + "(" + str(member.id) + ") " + " кикнут по причине " + reason)
        await member.send("Вы были кикнуты с нашего сервера по причине " + reason)

@bot.command()
async def писатьвлс(ctx, member: discord.Member, *, text):
    await member.send("С вами хочет связаться "+str(ctx.message.author)+". Текст: "+str(text))

@bot.command()
async def связаться(ctx, *, text):
    channel=bot.get_channel(config.message_from_users_channel)
    await channel.send("Обращение от "+str(ctx.message.author)+": "+text)

@bot.command()
@has_permissions(manage_roles=True)
async def выдатьроль(ctx, member: discord.Member, role: discord.Role):
    await member.add_roles(role)
    await ctx.send(str(member.name)+" выдана роль "+str(role.name))

@bot.command()
@has_permissions(manage_roles=True)
async def снятьроль(ctx, member: discord.Member, role: discord.Role):
    await member.remove_roles(role)
    await ctx.send("С участника "+str(member.name)+" снята роль "+str(role.name))

OPERATIONS = {
    '+': lambda x, y: x + y,
    '-': lambda x, y: x - y,
    '*': lambda x, y: x * y,
    '/': lambda x, y: x / y
}

@bot.command()
async def калк(ctx, a, operator, b):
    await ctx.send("Ответ: " + str(OPERATIONS[operator](int(a), int(b))))

#командо помощь
#@bot.command()
#async def помощь(ctx):#команда
#    await ctx.send(config.o1) #текст который выведеться

#команда /прав2
#@bot.command()
#async def прав2(ctx):#команда
#    await ctx.send(config.p2) #текст который выведеться

#команда/прав1
#@bot.command()
#async def прав1(ctx):#команда
#    await ctx.send(config.p1) #текст который выведеться

#команда /прав
#@bot.command()
#async def правила(ctx):#команда
#    await ctx.send(config.PRAV1) #текст который выведеться

#bot.run("")
token = os.environ.get("BOT_TOKEN")
bot.run(str(token))


from pyramid.config import Configurator
from pyramid.renderers import json_renderer_factory
from gaf_api.resources import Root
import logging

LOGGER = logging.getLogger("gaf_api")

def get_root(request):
    return Root()

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application. """
    config = Configurator(settings=settings)
    config.set_root_factory(get_root)
    config.add_renderer(None, json_renderer_factory)

    # API v1
    config.add_route('v1:calendar/events', '/api/v1/calendar/events')
    config.add_route('v1:calendar/event', '/api/v1/calendar/event/{event}')
    config.add_route("v1:live", "api/v1/live")
    config.scan(".api_v1")

    return config.make_wsgi_app()


import asyncio
import time

import pytest
import dask
from dask_gateway.auth import get_auth, BasicAuth, KerberosAuth, JupyterHubAuth
from dask_gateway.client import Gateway, GatewayCluster, cleanup_lingering_clusters
from dask_gateway_server.compat import get_running_loop
from dask_gateway_server.managers.inprocess import InProcessClusterManager
from dask_gateway_server.utils import random_port
from tornado import web
from tornado.httpclient import HTTPRequest

from .utils import temp_gateway

def test_get_auth():
    # Pass through existing auth objects
    auth = BasicAuth()
    assert get_auth(auth) is auth

    # Auth by keyword name
    auth = get_auth("basic")
    assert isinstance(auth, BasicAuth)

    auth = get_auth("kerberos")
    assert isinstance(auth, KerberosAuth)

    # Auth from config
    config = {"gateway": {"auth": {"type": "basic", "kwargs": {}}}}
    with dask.config.set(config):
        auth = get_auth()
        assert isinstance(auth, BasicAuth)

    # Auth from config with import path
    config = {
        "gateway": {"auth": {"type": "dask_gateway.auth.BasicAuth", "kwargs": {}}}
    }
    with dask.config.set(config):
        auth = get_auth()
        assert isinstance(auth, BasicAuth)

    # Auth from config with kwargs
    config = {"gateway": {"auth": {"type": "basic", "kwargs": {"username": "bruce"}}}}
    with dask.config.set(config):
        auth = get_auth()
        assert isinstance(auth, BasicAuth)
        assert auth.username == "bruce"

    # Errors
    with pytest.raises(TypeError):
        get_auth(1)

    with pytest.raises(TypeError):
        get_auth(lambda: 1)

    with pytest.raises(ImportError):
        get_auth("dask_gateway.auth.Foo")

    with pytest.raises(ImportError):
        get_auth("not_a_real_module_name_foo_barrr")

def test_config_auth_kwargs_template_environment_vars(monkeypatch):
    monkeypatch.setenv("TEST_USER", "bruce")
    config = {
        "gateway": {"auth": {"type": "basic", "kwargs": {"username": "{TEST_USER}"}}}
    }
    with dask.config.set(config):
        auth = get_auth()
        assert isinstance(auth, BasicAuth)
        assert auth.username == "bruce"

def test_jupyterhub_auth(monkeypatch):
    with pytest.raises(ValueError) as exc:
        get_auth("jupyterhub")
    assert "JUPYTERHUB_API_TOKEN" in str(exc.value)

    monkeypatch.setenv("JUPYTERHUB_API_TOKEN", "abcde")
    auth = get_auth("jupyterhub")
    assert auth.api_token == "abcde"
    assert isinstance(auth, JupyterHubAuth)

    # Parameters override environment variable
    assert JupyterHubAuth(api_token="other").api_token == "other"

def test_client_init():
    config = {
        "gateway": {
            "address": "http://127.0.0.1:8888",
            "public-address": None,
            "proxy-address": 8786,
            "auth": {"type": "basic", "kwargs": {"username": "bruce"}},
        }
    }

    with dask.config.set(config):
        # Defaults
        gateway = Gateway()
        assert gateway.address == "http://127.0.0.1:8888"
        assert gateway._public_address == "http://127.0.0.1:8888"
        assert gateway.proxy_address == "gateway://127.0.0.1:8786"
        assert gateway.auth.username == "bruce"

        # Address override
        gateway = Gateway(address="http://127.0.0.1:9999")
        assert gateway.address == "http://127.0.0.1:9999"

        # Proxy address override
        gateway = Gateway(proxy_address="gateway://123.4.5.6:9999")
        assert gateway.proxy_address == "gateway://123.4.5.6:9999"

        # Auth override
        gateway = Gateway(auth="kerberos")
        assert isinstance(gateway.auth, KerberosAuth)

    config = {
        "gateway": {
            "address": None,
            "public-address": None,
            "proxy-address": 8786,
            "auth": {"type": "basic", "kwargs": {}},
        }
    }

    with dask.config.set(config):
        # No address provided
        with pytest.raises(ValueError):
            Gateway()

    config["gateway"]["address"] = "http://127.0.0.1:8888"
    config["gateway"]["proxy-address"] = None

    with dask.config.set(config):
        # No proxy-address provided
        with pytest.raises(ValueError):
            Gateway()

def test_gateway_addresses_template_environment_vars(monkeypatch):
    monkeypatch.setenv("TEST", "foobar")

    with dask.config.set(
        gateway__address="http://{TEST}:80",
        gateway__proxy_address=8785,
        gateway__public_address="/{TEST}/foo/",
    ):
        g = Gateway()
    assert g.address == "http://foobar:80"
    assert g.proxy_address == "gateway://foobar:8785"
    assert g._public_address == "/foobar/foo"

    with dask.config.set(gateway__proxy_address="gateway://{TEST}:8787"):
        g = Gateway("http://test.com")
    assert g.address == "http://test.com"
    assert g.proxy_address == "gateway://foobar:8787"

class SlowHandler(web.RequestHandler):
    async def get(self):
        self.waiter = asyncio.ensure_future(asyncio.sleep(30))
        self.settings["task_set"].add(self.waiter)
        try:
            await self.waiter
        except asyncio.CancelledError:
            return
        self.write("Hello world")

    def on_connection_close(self):
        if hasattr(self, "waiter"):
            self.waiter.cancel()

class slow_server(object):
    def __init__(self):
        self.port = random_port()
        self.address = "http://127.0.0.1:%d" % self.port
        self.tasks = set()
        self.app = web.Application([(r"/", SlowHandler)], task_set=self.tasks)

    async def __aenter__(self):
        self.server = self.app.listen(self.port)
        return self

    async def __aexit__(self, *args):
        self.server.stop()
        await asyncio.gather(*self.tasks, return_exceptions=True)

@pytest.mark.asyncio
async def test_client_fetch_timeout():
    async with slow_server() as server:
        gateway = Gateway(server.address, auth=BasicAuth("alice"))
        with pytest.raises(TimeoutError):
            await gateway._fetch(HTTPRequest(url=server.address, request_timeout=1))

@pytest.mark.asyncio
async def test_client_reprs(tmpdir):
    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:
        async with Gateway(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
            asynchronous=True,
        ) as gateway:
            cluster = await gateway.new_cluster()

            # Plain repr
            assert cluster.name in repr(cluster)

            # HTML repr with dashboard
            cluster.dashboard_link = "%s/gateway/clusters/%s/status" % (
                gateway_proc.public_url,
                cluster.name,
            )
            assert cluster.name in cluster._repr_html_()
            assert cluster.dashboard_link in cluster._repr_html_()

            # HTML repr with no dashboard
            cluster.dashboard_link = None
            assert "Not Available" in cluster._repr_html_()

@pytest.mark.asyncio
async def test_cluster_widget(tmpdir):
    pytest.importorskip("ipywidgets")

    def test():
        with GatewayCluster(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
        ) as cluster:
            # Smoke test widget
            cluster._widget()

            template = "
Workers	 %d
"
            assert (template % 0) in cluster._widget_status()

            cluster.scale(1)
            timeout = time.time() + 30
            while time.time() < timeout:
                if cluster.scheduler_info.get("workers"):
                    break
                time.sleep(0.25)
            else:
                assert False, "didn't scale up in time"

            assert (template % 1) in cluster._widget_status()

    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:
        loop = get_running_loop()
        await loop.run_in_executor(None, test)

@pytest.mark.asyncio
async def test_dashboard_link_from_public_address(tmpdir):
    pytest.importorskip("bokeh")

    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:
        with dask.config.set(
            gateway__address=gateway_proc.public_urls.connect_url,
            gateway__public_address="/services/dask-gateway/",
            gateway__proxy_address=gateway_proc.gateway_urls.connect_url,
        ):
            async with Gateway(asynchronous=True) as gateway:
                assert gateway._public_address == "/services/dask-gateway"

                cluster = await gateway.new_cluster()

                sol = "/services/dask-gateway/gateway/clusters/%s/status" % cluster.name
                assert cluster.dashboard_link == sol

                clusters = await gateway.list_clusters()
                for c in clusters:
                    assert c.dashboard_link.startswith("/services/dask-gateway")

@pytest.mark.asyncio
async def test_create_cluster_with_GatewayCluster_constructor(tmpdir):
    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:
        async with GatewayCluster(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
            asynchronous=True,
        ) as cluster:

            # Cluster is now present in list
            clusters = await cluster.gateway.list_clusters()
            assert len(clusters)
            assert clusters[0].name == cluster.name

            await cluster.scale(1)

            with cluster.get_client(set_as_default=False) as client:
                res = await client.submit(lambda x: x + 1, 1)
                assert res == 2

        assert cluster.status == "closed"

        async with Gateway(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
            asynchronous=True,
        ) as gateway:
            # No cluster running
            clusters = await gateway.list_clusters()
            assert not clusters

@pytest.mark.asyncio
async def test_GatewayCluster_shutdown_on_close(tmpdir):
    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:

        def test():
            cluster = GatewayCluster(
                address=gateway_proc.public_urls.connect_url,
                proxy_address=gateway_proc.gateway_urls.connect_url,
            )
            assert cluster.shutdown_on_close
            assert cluster in GatewayCluster._instances

        loop = get_running_loop()
        await loop.run_in_executor(None, test)

        assert len(GatewayCluster._instances) == 0

        async with Gateway(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
            asynchronous=True,
        ) as gateway:
            # No cluster running
            clusters = await gateway.list_clusters()
            assert not clusters

@pytest.mark.asyncio
async def test_GatewayCluster_cleanup_atexit(tmpdir):
    async with temp_gateway(
        cluster_manager_class=InProcessClusterManager, temp_dir=str(tmpdir)
    ) as gateway_proc:

        def test():
            return GatewayCluster(
                address=gateway_proc.public_urls.connect_url,
                proxy_address=gateway_proc.gateway_urls.connect_url,
            )

        loop = get_running_loop()
        cluster = await loop.run_in_executor(None, test)

        assert len(GatewayCluster._instances) == 1

        def test_cleanup():
            # No warnings raised by cleanup function
            with pytest.warns(None) as rec:
                cleanup_lingering_clusters()
            for r in rec:
                assert not issubclass(r.category, UserWarning)

            # Cluster is now closed
            assert cluster.status == "closed"

            # No harm in double running
            with pytest.warns(None) as rec:
                cleanup_lingering_clusters()
            for r in rec:
                assert not issubclass(r.category, UserWarning)

        await loop.run_in_executor(None, test_cleanup)

        async with Gateway(
            address=gateway_proc.public_urls.connect_url,
            proxy_address=gateway_proc.gateway_urls.connect_url,
            asynchronous=True,
        ) as gateway:
            # No cluster running
            clusters = await gateway.list_clusters()
            assert not clusters


import tensorflow as tf
from tensorflow import keras

epochs=200
batch_size=128
verbose=1
nb_classes=10 # number of outputs
n_hidden=128
validation_split=0.2 # how much train is reserved for validation
mnist=keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()

reshaped=784
x_train=x_train.reshape(60000,reshaped)
x_test=x_test.reshape(10000,reshaped)
x_train=x_train.astype('float32')
x_test=x_test.astype('float32')
x_train/=255
x_test/=255
print(x_train.shape[0],'train samples')
print(x_test.shape[0],'test samples')

y_train=tf.keras.utils.to_categorical(y_train,nb_classes)
y_test=tf.keras.utils.to_categorical(y_test,nb_classes)
from keras.layers import Activation
model=tf.keras.models.Sequential()
model.add(keras.layers.Dense(nb_classes, input_shape=(reshaped,)))
#model.add(name='dense_layer')
model.add(Activation('softmax'))
model.compile(optimizer='SGD',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=128,epochs=50,verbose=1,validation_split=0.2)
test_loss,test_acc=model.evaluate(x_test,y_test)
print('Test accuracy:',test_acc)


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('pomodoro', views.pomodoro, name='pomodoro'),
    path('calendar', views.calendar, name='calendar'),
    path('flashcard', views.flashcard, name='flashcard'),
]


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

# create the 5x5 mean filter
meanFilter = np.ones((5,5))/25

# create a 5x5 guassian filter
x = cv2.getGaussianKernel(5,1)
gaussian = x*x.T

# create a Laplacian filter
laplacian=np.array([[0, 1, 0],
                    [1,-4, 1],
                    [0, 1, 0]])

filters = [meanFilter, gaussian, laplacian]

# zero pad the filter (requirement of the convolution theorem)
# compute the Fourier transforms of the filters and plot the magnitude spectrum
padSize = 100
filters = [np.pad(m, ((padSize, ), (padSize, )), 'constant') for m in filters]
fft_filters = [np.fft.fft2(x) for x in filters]
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
mag_spectrum = [np.log(np.abs(z)+1) for z in fft_shift]

# plotting
filter_name = ['Mean', 'Gaussian','Laplacian']
for i in range(3):
    plt.subplot(1,3,i+1),plt.imshow(mag_spectrum[i],cmap = 'gray')
    plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])

plt.show()


from django.db import models
from django.utils.translation import ugettext_lazy as _

from blocks.models import Block

class Carousel(Block):
    has_controls = models.BooleanField(
        _("With controls"),
        default=True,
        help_text=_("If true, adds the previous and next controls")
    )
    has_indicators = models.BooleanField(
        _("With indicators"),
        default=True,
        help_text=_("If true, adds indicators to the carousel")
    )
    has_captions = models.BooleanField(
        _("With captions"),
        default=True,
        help_text=_("If true, adds captions to the carousel")
    )

    interval = models.IntegerField(
        default=5000,
        help_text=_("The amount of time to delay between automatically cycling an item. "
                    "If false, carousel will not automatically cycle.")
    )
    keyboard = models.BooleanField(
        default=True,
        help_text=_("Whether the carousel should react to keyboard events.")
    )
    pause = models.BooleanField(
        default=True,
        help_text=_(
            """If set to "True", pauses the cycling of the carousel on mouseenter
            and resumes the cycling of the carousel on mouseleave.
            If set to "False", hovering over the carousel won't pause it.""")
    )
    ride = models.BooleanField(
        default=False,
        help_text=_("Autoplays the carousel after the user manually cycles the first item. "
                    'If "True", autoplays the carousel on load.')
    )
    wrap = models.BooleanField(
        default=True,
        help_text=_("Whether the carousel should cycle continuously or have hard stops.")
    )

    crop_size = models.CharField(_('Crop size'), max_length=255, blank=True, null=True)
    image_css_class = models.CharField(_('Image css class'), max_length=255, db_index=True,
                                       blank=True, null=True)
    caption_css_class = models.CharField(_('Caption css class'), max_length=255, db_index=True,
                                         blank=True, null=True)

    class Meta:
        app_label = 'blocks'
        verbose_name = 'Carousel Block'
        verbose_name_plural = 'Carousel Blocks'

class Slide(models.Model):
    carousel = models.ForeignKey(Carousel, on_delete=models.CASCADE, related_name='slides')

    title = models.CharField(_('Title'), max_length=255, blank=True, null=True)
    caption = models.TextField(_('Caption'), blank=True, null=True)
    image = models.ImageField(_("Image"), upload_to="blocks/carousels")

    class Meta:
        app_label = 'blocks'
        verbose_name = 'Carousel Block Slide'
        verbose_name_plural = 'Carousel Block Slides'


import random
random.seed(99)
from collections import Counter, OrderedDict, defaultdict

# Documentation: https://docs.python.org/3.7/library/stdtypes.html#typesmapping

def make_dict(low, high, size):
    """ Returns a dictionary with ints as keys """
    c = Counter()
    ints = [random.randrange(low, high, step=1) for _ in range(size)]
    c.update(ints)
    return dict(c)

# Inspect dict methods
print(dir(dict))
# 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'

# Create a dict
d1 = dict()
d = {}

# Generate some data
int_map = make_dict(1, 10, 10000)

# update an existing dictionary
# this OVERWRITES existing keys
d.update(int_map)

# add item to dictionary and set its value
d[10] = 1000
d['a'] = 1
d.update({'b': 2, 'c': 3})

# get item from dictionary
get_10_key = d.get(10)
get_11_key = d.get(11, 0)

# See all keys
keys = d.keys()

# See all values
values = d.values()

# See all keys and values as one object
kvs = d.items()

# copy existing dictionary
d1 = d.copy()

# Clear a dictionary
d1.clear()

# Delete a dict
del d1

# Remove a key and return its value
a = d.pop('a')

# Delete a key
del d[10]

# Remove an item - the last items in are the first to be removed (LIFO)
c = d.popitem()
b = d.popitem()

# See if an item is in the dictionary
isin = 8 in d

# See if an item is NOT in the dictionary
notin = 8 not in d

# loop through a dictionary
for k,v in d.items():
    if k == 8:
        print(F"8 is in d and its value is {v}")

# # Indexing on what's called "Dictionary View" objects (d.keys(), d.items(), d.values()) won't work
try:
    i = d.keys()[0]
except TypeError:
    i = list(d.keys())[0] or list(d)[0]

# add new keys to an existing dictionary and set their initial value
new_keys = [1, 2, 3, 'a', 'b', 'c']
for key in new_keys:
    d.setdefault(key, 10000)

# Initialize a dictionary with keys from an iterable (usually a list)
import string
letters = string.ascii_letters
initial_d = dict.fromkeys(letters, 0)
some_string = """fromkeys() is a class method that returns a new dictionary. value defaults to None. All of the values
refer to just a single instance, so it generally doesn’t make sense for value to be a mutable object such as an empty
list. To get distinct values, use a dict comprehension instead."""
for letter in some_string:
    if letter in initial_d:
        initial_d[letter] += 1
    else:
        initial_d[letter] = 1

# https://docs.python.org/3.7/library/collections.html#collections.OrderedDict
regular_d = {}
regular_d['key1'] = 'a'
regular_d['key2'] = 'b'
for n,item in enumerate('cdefghijklmnop', start=3):
    regular_d[F'key{n}'] = item
# print(od) - are the keys ordered based on the order they were INSERTED?

# OrderedDict() is less important now that the built-in dict() now remembers insertion order as-of Python 3.7
ordered_d = OrderedDict()
for n, item in enumerate('abcdefghijklmnop', start=1):
    ordered_d[F'key{n}'] = item

# Move item in OrderedDict() to end
ordered_d.move_to_end('key1')

# Set a default value on a dict
word_mapper = {'roof': 'roof', 'roofing': 'roof', 'roofs': 'roof', 'roofer': 'roofer', 'roofers': 'roofer',
         'windows': 'window', 'window': 'window', 'siding': 'side'}
default_d = defaultdict(list)
for k,v in word_mapper.items():
    default_d[v].append(k)


################## SUBSET GENEPOP FILE WITH LIST OF LOCI ######################
# 20180711 Natalie Lowell
# PURPOSE: to subset a Genepop file with a list of loci to keep
# INPUTS: managed by argparse below, include:
# - genepop file to filter
# - list of loci to keep
# - output filepath
# - format of header
# OUTPUT: genepop file with only desired loci
###############################################################################

# organize parameter inputs with argparse
import argparse
parser = argparse.ArgumentParser(description="Filter Genepop file to include only provided locus names.")
parser.add_argument("-i", "--infile", help="input Genepop file for filtering", type=str, required=True)
parser.add_argument("-f", "--format", help="input Genepop header format, answer 1 if locus names are each on a line and 2 if locus names are in one line separated by commas", type=str, required=True)
parser.add_argument("-l", "--loci", help="text file of loci to keep, with each locus name on its own line", type=str, required=True)
parser.add_argument("-o", "--outfile", help="filtered Genepop file", type=str, required=True)
args = parser.parse_args()

# get locus names to keep from input file
locus_names_file = open(args.loci, "r")
locus_names_to_keep = []
for line in locus_names_file:
    if line.strip() != "":
        locus_names_to_keep.append(line.strip())
locus_names_file.close()

# read in genepop file that needs filtering
genepop = open(args.infile, "r")
genepop_str = genepop.read()
genepop_lines = genepop_str.split("\n")

# get locus names out of genepop file
all_locus_names = []
gp_chunks = []
gp_chunk_lines = []
for line in genepop_lines:
    if line.strip() != "pop" and line != "Pop":
        gp_chunk_lines.append(line)
    elif line.strip() == "pop" or line.strip() == "Pop":
        gp_chunks.append(gp_chunk_lines)
        gp_chunk_lines = []
gp_chunks.append(gp_chunk_lines)

if args.format == "1":
    for line in gp_chunks[0]:
        if line != "" and line[0] != "#":
            all_locus_names.append(line.strip())
if args.format == "2":
    for line in gp_chunks[0]:
        if line != "" and line[0] != "#":
            locus_names = line.split(",")
            for locus in locus_names:
                all_locus_names.append(locus.strip())

# get indeces of loci to keep in genepop file
indeces_loci_to_keep = []
for locus in locus_names_to_keep:
    indeces_loci_to_keep.append(all_locus_names.index(locus))

### write filtered genepop file

# make new header with old header
if gp_chunks[0][0][0] == "#":
    old_header = gp_chunks[0][0]
    text_to_keep = gp_chunks[0][0][1:]
    new_header = "# Genepop made with subset_GP_forloci.py, old header: " + text_to_keep

# make new genepop file and add header
filtered_genepop = open(args.outfile, "w")
filtered_genepop.write(new_header+ "\n")

# add locus names, either one on each line or in header
if args.format == "1":
    for locus in locus_names_to_keep:
        filtered_genepop.write(locus + "\n")
elif args.format == "2":
    header = ""
    for locus in locus_names_to_keep:
        header += locus + ","
    header = header [:-1]
    filtered_genepop.write(header + "\n")

# add pop and genotype lines
for gp_chunk in gp_chunks[1:]:
    filtered_genepop.write("pop\n")
    for line in gp_chunk:
        if line != "":
            linelist = line.split(",")
            ind_name = linelist[0]
            genotypes = linelist[1].strip().split()
            filtered_genepop.write(ind_name + ", ")
            for index in indeces_loci_to_keep:
                filtered_genepop.write(" " + genotypes[index])
            filtered_genepop.write("\n")
filtered_genepop.close()


#!/usr/bin/python3
""" state view """
from api.v1.views import app_views
from flask import jsonify, Blueprint, make_response, abort, request
from models import storage
from models.state import State
from models.base_model import BaseModel

@app_views.route('/states', methods=["GET", "POST"], strict_slashes=False)
def get_all_states():
    """ retrieves all state objects """
    if request.method == 'GET':
        output = []
        states = storage.all(State).values()
        for state in states:
            output.append(state.to_dict())
        return (jsonify(output))
    if request.method == 'POST':
        data = request.get_json()
        if not request.is_json:
            abort(400, description="Not a JSON")
        if 'name' not in request.json:
            abort(400, description="Missing name")
        state = State(**data)
        state.save()
        return (jsonify(state.to_dict()), 201)

@app_views.route('/states/<state_id>', methods=["GET", "PUT"],
                 strict_slashes=False)
def get_a_state(state_id):
    """ retrieves one unique state object """
    state = storage.get(State, state_id)
    if state is None:
        abort(404)
    if request.method == "GET":
        output = state.to_dict()
        return (jsonify(output))
    if request.method == "PUT":
        print("test\n")
        data = request.get_json()
        if not request.is_json:
            abort(400, description="Not a JSON")
        for key, value in data.items():
            setattr(state, key, value)
        state.save()
        return (jsonify(state.to_dict()), 200)

@app_views.route('/states/<state_id>', methods=["GET", "DELETE"],
                 strict_slashes=False)
def del_a_state(state_id):
    """ delete one unique state object """
    state = storage.get(State, state_id)
    if state is None:
        abort(404)
    storage.delete(state)
    storage.save()
    result = make_response(jsonify({}), 200)
    return result