"""
https://leetcode.com/problems/group-anagrams/
"""
# my solution after a while
import collections
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        if not strs or strs is None:
            return []

        sorted_list = []
        result = []

        for index, items in enumerate(strs):
            sorted_list.append(("".join(sorted(items)), strs[index]))

        word_table = collections.defaultdict(list)

        for item, index in sorted_list:
            word_table[item].append(index)

        for i in word_table.values():
            result.append(i)
        return result


# A simple setup script for creating a Windows service. See the comments in the
# Config.py and ServiceHandler.py files for more information on how to set this
# up.
#
# Installing the service is done with the option --install <Name> and
# uninstalling the service is done with the option --uninstall <Name>. The
# value for <Name> is intended to differentiate between different invocations
# of the same service code -- for example for accessing different databases or
# using different configuration files.

from cx_Freeze import setup, Executable

buildOptions = dict(includes=["service", "dlp"], include_msvcr=1)
executable = Executable("config.py", base = "Win32Service",
        targetName = "service.exe")

setup(
        name = "pulse_pull_client",
        version = "0.1",
        description = "Pulse service for Pull-mode deployment",
        executables = [executable],
        options = dict(build_exe = buildOptions))


def daily(start, end):
	return range(start, end+1)

def weekly(start, end):
	return map(lambda x: x * 5, range(start, end+1))

def tech_key(data_key, period, tech_ind):
	return str(data_key) + "-" + str(period) + "_" + str(tech_ind)

def calculate_quarter_count(start_date, end_date):
	assert(start_date <= end_date)
	quarter_num = 0
	if start_date.month < 4:
		quarter_num += 4
	elif start_date.month < 5:
		quarter_num += 3
	elif start_date.month < 8:
		quarter_num += 2
	elif start_date.month < 11:
		quarter_num += 1
	else:
		quarter_num += 0

	if end_date.month < 4:
		quarter_num -= 4
	elif end_date.month < 5:
		quarter_num -= 3
	elif end_date.month < 8:
		quarter_num -= 2
	elif end_date.month < 11:
		quarter_num -= 1
	else:
		quarter_num -= 0

	year_delta = end_date.year - start_date.year
	quarter_num += year_delta * 4
	return quarter_num

from datetime import datetime
def quarterify(year, quarter_num):
	""" quarter_num = 1, 2, 3, 4
	"""
	year = int(year)
	assert(quarter_num in [1, 2, 3, 4])
	if quarter_num == 1:
		return datetime(year, 5, 1)
	elif quarter_num == 2:
		return datetime(year, 8, 1)
	elif quarter_num == 3:
		return datetime(year, 11, 1)
	else:
		return datetime(year+1, 4, 1)

def get_closest_quarter(cur_date, past=True):
	if cur_date.month < 4:
		if past:
			return (cur_date.year-1), 3
			#return cur_date.replace(year=(cur_date.year-1), month=11, day=1)
		else:
			return (cur_date.year-1), 4
			#return cur_date.replace(year=(cur_date.year), month=4, day=1)
	elif cur_date.month < 5:
		if past:
			return (cur_date.year-1), 4
			#return cur_date.replace(year=(cur_date.year), month=4, day=1)
		else:
			return (cur_date.year), 1
			#return cur_date.replace(year=(cur_date.year), month=5, day=1)
	elif cur_date.month < 8:
		if past:
			return (cur_date.year), 1
			#return cur_date.replace(year=(cur_date.year), month=5, day=1)
		else:
			return (cur_date.year), 2
			#return cur_date.replace(year=(cur_date.year), month=8, day=1)
	elif cur_date.month < 11:
		if past:
			return (cur_date.year), 2
			#return cur_date.replace(year=(cur_date.year), month=8, day=1)
		else:
			return (cur_date.year), 3
			#return cur_date.replace(year=(cur_date.year), month=11, day=1)
	else:
		if past:
			return (cur_date.year), 3
			#return cur_date.replace(year=(cur_date.year), month=11, day=1)
		else:
			return (cur_date.year), 4
			#return cur_date.replace(year=(cur_date.year+1), month=4, day=1)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Date ：2020/10/31 18:47
# Author：Lydia
from typing import List

import pytest

# @pytest.fixture()
# def login():
#     # setup 操作
#     print("登录操作")
#     # yield 相当于return 操作
#     yield ['tom', '123456']
#     # teardown 操作
#     print("登出操作")

# login param 用法
@pytest.fixture(scope="function", params=['tom', 'lydia'])
def login(request):
    # setup 操作
    print("登录操作")
    username = request.param
    # yield 相当于return 操作
    yield username
    # teardown 操作
    print("登出操作")

@pytest.fixture(scope='session', autouse=True)
def conn_db():
    print("完成 数据库连接")
    yield "database"
    print("关闭 数据库连接")

# 中文转换
# pytest test_calc.py -m div -vs 执行打div标签的用例
def pytest_collection_modifyitems(
        session: "Session", config: "Config", items: List["Item"]
) -> None:
    print(type(items))
    # 调整执行顺序
    items.reverse
    # 调整编码格式
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
        # 加入标签
        if 'add' in item._nodeid:
            item.add_marker(pytest.mark.add)
        elif 'div' in item._nodeid:
            item.add_marker(pytest.mark.div)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

def driverConfig():
    PATH = "chromedriver.exe"
    invisibilidade = Options()
    invisibilidade.headless = True
    driver = webdriver.Chrome(PATH, options=invisibilidade)
    return driver

def abrindoGoogle():
    driver = driverConfig()
    driver.get("https://www.google.com/")
    return driver

def fazendoBusca(driver):
    loop = True

    pesquisa = driver.find_element_by_class_name("gLFyf.gsfi")
    pesquisa.clear()
    acao = input("Ação: ").strip()

    if acao == "":
        loop = False

    else:
        pesquisa.send_keys('cotação ' + acao)
        pesquisa.send_keys(Keys.RETURN)

        try:
            valorAcao = driver.find_element_by_class_name("IsqQVc.NprOob.XcVN5d")
            valorAcaoTexto = valorAcao.text

            print('O Ultimo valor de ', acao, 'encontado é de RS:' + valorAcaoTexto)

        except:
            print(f"Erro, Papel: {acao} não encontrado")

        return loop

loop = True

driver = abrindoGoogle()

while loop == True:
    loop = fazendoBusca(driver)

print('fim')


import pandas as pd
import numpy as np
import itertools
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

df = pd.read_csv('Classification/KNN/teleCust1000t.csv')
# print(df['custcat'].value_counts())
# df.hist(column='income', bins=50)
# plt.show()

X = df[[
    'region', 'tenure', 'age', 'marital', 'address', 'income', 'ed', 'employ',
    'retire', 'gender', 'reside'
]].values
Y = df['custcat'].values

X = preprocessing.StandardScaler().fit(X).transform(X.astype(float))

X_train, X_test, Y_train, Y_test = train_test_split(X,
                                                    Y,
                                                    test_size=0.1,
                                                    random_state=4)
# print('Train: ', X_train.shape, Y_train.shape)
# print('Test: ', X_test.shape, Y_test.shape)

# Startgin Classifier with k = 4
""" K = 4
knn = KNeighborsClassifier(n_neighbors=K)
knn.fit(X_train, Y_train)
predicted = knn.predict(X_test)
train_predicted = knn.predict(X_train)
 """

# Evaluating Accuracy
""" print('Train Accuracy:', metrics.accuracy_score(
    y_true=Y_train, y_pred=train_predicted))
print('Test Accuracy:', metrics.accuracy_score(
    y_true=Y_test, y_pred=predicted)) """

# Finding the best K
K_list = range(1, 601)
accuracies = []
for k in K_list:
    print(k, end=': ')
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, Y_train)
    predicted = knn.predict(X_test)
    train_predicted = knn.predict(X_train)
    accuracy = metrics.accuracy_score(y_true=Y_test, y_pred=predicted)
    print(accuracy)
    accuracies.append(accuracy)

print('Best K:', K_list[accuracies.index(max(accuracies))], 'with Accuracy:',
      max(accuracies))

# Plot the graph to visually find the value
plt.plot(K_list, accuracies, 'b-')
plt.show()


import sys
sys.stdin = open('4740.txt','r')

from collections import deque
dx=[1,0,-1,0]
dy=[0,1,0,-1]
def IS(y,x):
    return -1<y<N and -1<x<M
def Up(List):
    for c in List:
        if c!='#':return False
    return True
def BFS(i,j,color):
    Q=deque([(i,j)])
    Set=[(i,j)]
    while Q:
        y,x=Q.popleft()
        for d in range(4):
            Y,X=y+dy[d],x+dx[d]
            if IS(Y,X) and not Map[Y][X] and A[Y][X]==color:
                Map[Y][X]=1
                Q.append((Y,X))
                Set.append((Y,X))
    return Set,len(Set)
def Check():
    B=[]
    Max=0
    for i in range(N):
        for j in range(M):
            if A[i][j]!='#' and not Map[i][j]:
                Map[i][j]=1
                b,l=BFS(i,j,A[i][j])
                B.append(b)
                Max=max(Max,l)
    for d in B:
        if len(d)!=Max:continue
        for e in d:A[e[0]][e[1]]='#'
def Down(Y,X,qu):
    global A
    if qu=='L' or qu=='R':Y,X=X,Y
    for j in range(X):
        for i in range(Y-1,0,-1):
            if A[i][j]!='#':continue
            y=i
            while A[y][j]=='#' and y!=-1:y-=1
            if y==-1:break
            A[i][j],A[y][j]=A[y][j],A[i][j]
    if qu=='L' or qu=='R':
        t=1 if qu=='L' else 3
        for _ in range(t):A=list(map(list,zip(*A[::-1])))
for tc in range(int(input())):
    print('#%d'%(tc+1))
    N,M,Q=map(int,input().split())
    A=[list(map(str,input()))for _ in range(N)]
    for _ in range(Q):
        q=input()
        Map=[[0]*M for _ in range(N)]
        T=0
        if q=='D':Check()
        elif q=='L':T=3
        elif q=='R':T=1
        else:
            new=list(map(str,q[2:]))
            if Up(A[0]):A.append(new);A.pop(0)
        for t in range(T):A=list(map(list,zip(*A[::-1])))
        Down(N,M,q)
    for _ in range(N):print(''.join(map(str,A[_])))
    print()


# -*- coding: utf-8 -*-
"""
Created on Mon Dec  6 16:52:21 2021

@author: Katarina
"""

"""
Opening a .h5 and loading from it
"""

import os
import keras
import gzip
import numpy as np

"""
## Extract 'sz' targets from in/out data
"""
def get_targets(x_in, y_in, x_out, y_out, sz=500):

    x_temp = np.vstack((x_in, x_out))
    y_temp = np.vstack((y_in, y_out))

    inv = np.ones((x_in.shape[0],1))
    outv = np.zeros((x_out.shape[0],1))
    in_out_temp = np.vstack((inv, outv))

    assert x_temp.shape[0] == y_temp.shape[0]

    if sz > x_temp.shape[0]:
        sz = x_temp.shape[0]

    perm = np.random.permutation(x_temp.shape[0])
    perm = perm[0:sz]
    x_targets = x_temp[perm,:]
    y_targets = y_temp[perm,:]

    in_out_targets = in_out_temp[perm,:]

    return x_targets, y_targets, in_out_targets

"""
Get all the files
"""
files = [
      'first_1000_train_labels.gz', 'first_1000_train_images.gz',
      'second_1000_train_labels.gz', 'second_1000_train_images.gz',
      'third_1000_train_labels.gz', 'third_1000_train_images.gz',
      'fourth_1000_train_labels.gz', 'fourth_1000_train_images.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
      ]

with gzip.open(files[0], 'rb') as lbpath:
    first_1000_train_labels = np.frombuffer(lbpath.read(), np.uint8)
    #print(first_1000_train_labels.shape)
    #print(first_1000_train_labels)

with gzip.open(files[1], 'rb') as imgpath:
    first_1000_train_images = np.frombuffer(
    imgpath.read(), np.uint8).reshape(len(first_1000_train_labels), 28, 28)
    #print(first_1000_train_images.shape)
    #print(first_1000_train_images)

with gzip.open(files[2], 'rb') as lbpath:
    second_1000_train_labels = np.frombuffer(lbpath.read(), np.uint8)

with gzip.open(files[3], 'rb') as imgpath:
    second_1000_train_images = np.frombuffer(
    imgpath.read(), np.uint8).reshape(len(second_1000_train_labels), 28, 28)

with gzip.open(files[4], 'rb') as lbpath:
    third_1000_train_labels = np.frombuffer(lbpath.read(), np.uint8)

with gzip.open(files[5], 'rb') as imgpath:
    third_1000_train_images = np.frombuffer(
    imgpath.read(), np.uint8).reshape(len(third_1000_train_labels), 28, 28)

with gzip.open(files[6], 'rb') as lbpath:
    fourth_1000_train_labels = np.frombuffer(lbpath.read(), np.uint8)

with gzip.open(files[7], 'rb') as imgpath:
    fourth_1000_train_images = np.frombuffer(
    imgpath.read(), np.uint8).reshape(len(fourth_1000_train_labels), 28, 28)

with gzip.open(files[8], 'rb') as lbpath:
    t10k_labels_idx1_ubyte = np.frombuffer(lbpath.read(), np.uint8, offset=8)
    #print(t10k_labels_idx1_ubyte.shape)

with gzip.open(files[9], 'rb') as imgpath:
    t10k_images_idx3_ubyte = np.frombuffer(
    imgpath.read(), np.uint8, offset=16).reshape(len(t10k_labels_idx1_ubyte), 28, 28)

x_train = first_1000_train_images #[:1000] # first 1000 images from .gz images file
y_train = first_1000_train_labels # first 1000 labels from .gz file

x_test = t10k_images_idx3_ubyte # all 10,000 images from test.gz?
y_test = t10k_labels_idx1_ubyte # all 10,000 labels from test.gz?

x_attack_train = third_1000_train_images # some images from mnist training data
y_attack_train = third_1000_train_labels

x_out = fourth_1000_train_images
y_out = fourth_1000_train_labels

target_train_size = x_train.shape[0]

# .gz has overall shape (2000, 28, 28) -- 2k images, each is 28x28 pixels
print('Loaded .gz data; shape: {} [y: {}], test shape: {} [y: {}]'.format(x_train.shape, y_train.shape, x_test.shape, y_test.shape))
# Let's flatten the images for easier processing (labels don't change)
flat_vector_size = 28 * 28
x_train = x_train.reshape(x_train.shape[0], flat_vector_size)
x_test = x_test.reshape(x_test.shape[0], flat_vector_size)
x_attack_train = x_attack_train.reshape(x_attack_train.shape[0], flat_vector_size)
x_out = x_out.reshape(x_out.shape[0], flat_vector_size)

# Put the labels in "one-hot" encoding using keras' to_categorical()
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_attack_train = keras.utils.to_categorical(y_attack_train, num_classes)
y_out = keras.utils.to_categorical(y_out, num_classes)

# extract targets (some in, some out)
x_targets, y_targets, in_or_out_targets = get_targets(x_train, y_train, x_out, y_out)

"""
## Load model from file
"""
def load_model(base_fp):
    # Model reconstruction from JSON file
    arch_json_fp = '{}-architecture.json'.format(base_fp)
    if not os.path.isfile(arch_json_fp):
        return None

    with open(arch_json_fp, 'r') as f:
        model = keras.models.model_from_json(f.read())

    # Load weights into the new model
    model.load_weights('{}-weights.h5'.format(base_fp))

    print('Loaded model from file ({}).'.format(base_fp))
    return model

model = load_model('30e_025rc_300h')
if model is None:
    print('Model files do not exist. Train the model first!')

# evaluate loaded model on test data
model.compile(loss='binary_crossentropy', optimizer='rmsprop',
metrics=['accuracy'])

score = model.evaluate(x_test, y_test, verbose=0)
print(score)
print(model.metrics_names)


from flask import Blueprint, request, jsonify, current_app
from app.serializer.address_locksmith import AddressLocksmithSchema as ma
from app.model.address_locksmith import AddressLocksmith as md
from app.jwt import locksmith_required, user_required
from flask_jwt_extended import get_jwt_identity
from sqlalchemy import and_

bp_address_locksmith = Blueprint('address_locksmith', __name__)

@bp_address_locksmith.route('/get-locksmith-address-list', methods=['GET'])
@locksmith_required
def locksmith_address_list():
    _filter = and_(md.active == True, md.locksmith_id == get_jwt_identity())
    return ma(many=True).jsonify(md.query.filter(_filter).all()), 200

@bp_address_locksmith.route('/add-locksmith-address', methods=['POST'])
@locksmith_required
def add_locksmith_address():
    request.json['locksmith_id'] = get_jwt_identity()
    value = ma().load(request.json)
    if value.main:
        _filter = and_(md.locksmith_id == get_jwt_identity())
        md.query.filter(_filter).update({"main": False})
        current_app.db.session.commit()
    current_app.db.session.add(value)
    current_app.db.session.commit()
    return jsonify({'msg': 'sucess'}), 201

@bp_address_locksmith.route('/update-locksmith-address', methods=['POST'])
@locksmith_required
def update_locksmith_address():
    request.json['locksmith_id'] = get_jwt_identity()
    _filter = and_(md.locksmith_id == get_jwt_identity(), md.id == request.json['id'])
    result = md.query.filter(_filter).first()
    address = ma().dump(result)
    if address:
        tmp_filter = md.locksmith_id == get_jwt_identity()
        md.query.filter(tmp_filter).update({"main": False})
        md.query.filter(_filter).update(request.json)
        current_app.db.session.commit()
        return jsonify({'msg': 'sucess'}), 201
    else:
        return jsonify({'msg': 'Failed'}), 409

@bp_address_locksmith.route('/delete-locksmith-address', methods=['POST'])
@locksmith_required
def delete_locksmith_address():
    _filter = and_(md.locksmith_id == get_jwt_identity(), md.id == request.json['id'])
    result = md.query.filter(_filter).first()
    address = ma().dump(result)
    if address:
        md.query.filter(_filter).update({"active": False})
        current_app.db.session.commit()
        return jsonify({'msg': 'sucess'}), 201
    else:
        return jsonify({'msg': 'Failed'}), 409


from datetime import datetime
import requests
import json

def format_time(timestamp_str):
    '''
    convert timestamp to pronounce.
    '''
    timestamp = datetime.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%S%z')
    time_to_pronounce = datetime.strftime(timestamp, '%#m月%#d日%p%#I時')

    time_in_jp = time_to_pronounce.replace('AM', '午前').replace('PM', '午後')

    return time_in_jp

def is_json(json_str):
    '''
    judge if json_str is valid.
    '''
    result = False
    try:
        json.loads(json_str)
        result = True
    except json.JSONDecodeError as jde:
        logger.info('got invalid response json, retrying.')

    return result

def get(rotation_code):
    uri = 'https://spla3.yuu26.com/api/coop-grouping/{}'.format(rotation_code)
    headers = {'Content-Type': 'application/json'}

    schedule = ''
    while True:
        schedule = requests.get(uri, headers=headers)
        valid_json = is_json(schedule.text)

        if valid_json:
            break

    schedule = schedule.json()
    map_detail = schedule['results'][0]

    result = {
        'stage': map_detail['stage']['name'],
        'start_time': map_detail['start_time'],
        'end_time': map_detail['end_time'],
        'weapons': [ weapon['name'] for weapon in map_detail['weapons']],
        'big_run': map_detail['is_big_run']
    }

    return result


#####################################################################
#                           PyRocket								#
# 2D Regenertive Cooling Simulation for Bipropellant Rocket Engines #
#                                                                   #
# Creator:  Joanthan Neeser                                         #
# Date:     15.12.2022                                              #
# Version:  2.3  													#
# License:	GNU GENERAL PUBLIC LICENSE V3							#
#####################################################################

# File with unit tests to validate program performance and inputs

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

# functional imports
import config
from Output import Settings2D
from PlottingFunctions import multi_plot

# import functions to be tested
from IsentropicRelations import Isentropic
from SectionThermalSim import HeatEquationSolver

def cooling_fluid_test():
    N = 100                                 # number of steps
    Cp = np.ndarray(N)                      # specific heat at constant pressure [J/kg/K]
    k = np.ndarray(N)                       # thermal conductivity [W/m/K]
    rho = np.ndarray(N)                     # density [kg/m^3]
    Pr = np.ndarray(N)                      # Prandtl number

    if config.coolant.phase == 'l':
        T_arr = np.linspace(288, 600, N)      # Temperature array [K]
    elif config.coolant.phase == 'g':
        T_arr = np.linspace(288, 1200, N)

    for i in range(len(T_arr)):
        # calculate coolant properties at every temperature
        coolant = thermo.Mixture(IDs=config.coolant.IDs, ws=config.coolant.ws, T=T_arr[i], P=config.coolant.P)
        Cp[i] = coolant.Cp
        rho[i] = coolant.rho
        Pr[i] = coolant.Pr
        k[i] = coolant.Cp * coolant.mu / coolant.Pr

    f, axes = plt.subplots(4, 1)

    axes[0].plot(T_arr, Cp)
    axes[0].set_ylabel('Cp [J/kg/K]')

    axes[1].plot(T_arr, k)
    axes[1].set_ylabel('k [W/m/K]')

    axes[2].plot(T_arr, rho)
    axes[2].set_ylabel('rho [kg/m^3]')

    axes[3].plot(T_arr, Pr)
    axes[3].set_ylabel('Pr')

    plt.xlabel('Temperature [K]')
    plt.show()

def material_property_test():
    try:
        config.material.plot(config.material.sig_u, 'ultimate strength [Pa]')
    except:
        print('Warning: no variable ultimate strength of material set')
    try:
        config.material.plot(config.material.E, 'youngs modulus [Pa]')
    except:
        print('Warning: no variable youngs modulus of material set')
    try:
        config.material.plot(config.material.k, 'thermal conductivity [W/m/K]')
    except:
        print('Warning: no variable thremal conductivity of material set')

def isentropic_relations_test():
	# Test isentropic relations class
	gas = Isentropic(config.Pc, config.cea.Tc, config.cea.gamma, config.cea.Pr, config.geometry[:,0], config.geometry[:,1])
	gas.calculate()

	multi_plot(gas.M, gas.T_s, gas.p_s/1e5, gas.T_aw, 'M', 'T_s [K]', 'p_s [bar]', 'T_aw [K]')

def section_thermal_sim_test():
    # Test 2D thermal sim

    def halpha_func(T, idx):
        return 1600, 2400

    def halpha_c_func(T, idx):
        return 1113

    q_rad = 0
    T_c = 288
    T_amb = 288
    idx = 1
    settings = Settings2D(cell_size=2e-4, time_step=4, tolerance=1e-3, max_iter=100, save_fig=False, print_result=False, run_time='steady_state')
    settings.output_msg()

    solver = HeatEquationSolver(
        idx,
        config.gas,
        config.material,
        config.cooling_geom,
        halpha_func,
        halpha_c_func,
        q_rad,
        T_c,
        T_amb,
        path='TestSectionThermalSim',
        settings=settings
    )

    solver.run_sim()

    if solver.diff < 1e-3:
        print('Solver converged sucessfully')

if __name__ == "__main__":
    cooling_fluid_test()
    material_property_test()
    isentropic_relations_test()
    section_thermal_sim_test()


"""
This module contains Fab's `main` method plus related subroutines.

`main` is executed as the command line ``fab`` program and takes care of
parsing options and commands, loading the user settings file, loading a
fabfile, and executing the commands given.

The other callables defined in this module are internal only. Anything useful
to individuals leveraging Fabric as a library, should be kept elsewhere.
"""

from collections import defaultdict
from operator import add, isMappingType
from optparse import OptionParser
import os
import sys
import types

from fabric import api, state  # For checking callables against the API, & easy mocking
from fabric.contrib import console, files, project  # Ditto
from fabric.network import denormalize, interpret_host_string, disconnect_all
from fabric.state import commands, connections, env_options
from fabric.tasks import Task
from fabric.utils import abort, indent

# One-time calculation of "all internal callables" to avoid doing this on every
# check of a given fabfile callable (in is_classic_task()).
_modules = [api, project, files, console]
_internals = reduce(lambda x, y: x + filter(callable, vars(y).values()),
    _modules,
    []
)

# Module recursion cache
class _ModuleCache(object):
    """
    Set-like object operating on modules and storing __name__s internally.
    """
    def __init__(self):
        self.cache = set()

    def __contains__(self, value):
        return value.__name__ in self.cache

    def add(self, value):
        return self.cache.add(value.__name__)

    def clear(self):
        return self.cache.clear()

_seen = _ModuleCache()

def load_settings(path):
    """
    Take given file path and return dictionary of any key=value pairs found.

    Usage docs are in docs/usage/fab.rst, in "Settings files."
    """
    if os.path.exists(path):
        comments = lambda s: s and not s.startswith("#")
        settings = filter(comments, open(path, 'r'))
        return dict((k.strip(), v.strip()) for k, _, v in
            [s.partition('=') for s in settings])
    # Handle nonexistent or empty settings file
    return {}

def _is_package(path):
    """
    Is the given path a Python package?
    """
    return (
        os.path.isdir(path)
        and os.path.exists(os.path.join(path, '__init__.py'))
    )

def find_fabfile():
    """
    Attempt to locate a fabfile, either explicitly or by searching parent dirs.

    Usage docs are in docs/usage/fabfiles.rst, in "Fabfile discovery."
    """
    # Obtain env value
    names = [state.env.fabfile]
    # Create .py version if necessary
    if not names[0].endswith('.py'):
        names += [names[0] + '.py']
    # Does the name contain path elements?
    if os.path.dirname(names[0]):
        # If so, expand home-directory markers and test for existence
        for name in names:
            expanded = os.path.expanduser(name)
            if os.path.exists(expanded):
                if name.endswith('.py') or _is_package(expanded):
                    return os.path.abspath(expanded)
    else:
        # Otherwise, start in cwd and work downwards towards filesystem root
        path = '.'
        # Stop before falling off root of filesystem (should be platform
        # agnostic)
        while os.path.split(os.path.abspath(path))[1]:
            for name in names:
                joined = os.path.join(path, name)
                if os.path.exists(joined):
                    if name.endswith('.py') or _is_package(joined):
                        return os.path.abspath(joined)
            path = os.path.join('..', path)
    # Implicit 'return None' if nothing was found

def is_classic_task(tup):
    """
    Takes (name, object) tuple, returns True if it's a non-Fab public callable.
    """
    name, func = tup
    return (
        callable(func)
        and (func not in _internals)
        and not name.startswith('_')
    )

def load_fabfile(path, importer=None):
    """
    Import given fabfile path and return (docstring, callables).

    Specifically, the fabfile's ``__doc__`` attribute (a string) and a
    dictionary of ``{'name': callable}`` containing all callables which pass
    the "is a Fabric task" test.
    """
    if importer is None:
        importer = __import__
    # Get directory and fabfile name
    directory, fabfile = os.path.split(path)
    # If the directory isn't in the PYTHONPATH, add it so our import will work
    added_to_path = False
    index = None
    if directory not in sys.path:
        sys.path.insert(0, directory)
        added_to_path = True
    # If the directory IS in the PYTHONPATH, move it to the front temporarily,
    # otherwise other fabfiles -- like Fabric's own -- may scoop the intended
    # one.
    else:
        i = sys.path.index(directory)
        if i != 0:
            # Store index for later restoration
            index = i
            # Add to front, then remove from original position
            sys.path.insert(0, directory)
            del sys.path[i + 1]
    # Perform the import (trimming off the .py)
    imported = importer(os.path.splitext(fabfile)[0])
    # Remove directory from path if we added it ourselves (just to be neat)
    if added_to_path:
        del sys.path[0]
    # Put back in original index if we moved it
    if index is not None:
        sys.path.insert(index + 1, directory)
        del sys.path[0]

    # Actually load tasks
    docstring, new_style, classic, default = load_tasks_from_module(imported)
    tasks = new_style if state.env.new_style_tasks else classic
    # Clean up after ourselves
    _seen.clear()
    return docstring, tasks, default

def load_tasks_from_module(imported):
    """
    Handles loading all of the tasks for a given `imported` module
    """
    # Obey the use of <module>.__all__ if it is present
    imported_vars = vars(imported)
    if "__all__" in imported_vars:
        imported_vars = [(name, imported_vars[name]) for name in \
                         imported_vars if name in imported_vars["__all__"]]
    else:
        imported_vars = imported_vars.items()
    # Return a two-tuple value.  First is the documentation, second is a
    # dictionary of callables only (and don't include Fab operations or
    # underscored callables)
    new_style, classic, default = extract_tasks(imported_vars)
    return imported.__doc__, new_style, classic, default

# For attribute tomfoolery
class _Dict(dict):
    pass

def extract_tasks(imported_vars):
    """
    Handle extracting tasks from a given list of variables
    """
    new_style_tasks = _Dict()
    classic_tasks = {}
    default_task = None
    if 'new_style_tasks' not in state.env:
        state.env.new_style_tasks = False
    for tup in imported_vars:
        name, obj = tup
        if is_task_object(obj):
            state.env.new_style_tasks = True
            # Honor instance.name
            new_style_tasks[obj.name] = obj
            # Handle aliasing
            if obj.aliases is not None:
                for alias in obj.aliases:
                    new_style_tasks[alias] = obj
            # Handle defaults
            if obj.is_default:
                default_task = obj
        elif is_classic_task(tup):
            classic_tasks[name] = obj
        elif is_task_module(obj):
            docs, newstyle, classic, default = load_tasks_from_module(obj)
            for task_name, task in newstyle.items():
                if name not in new_style_tasks:
                    new_style_tasks[name] = _Dict()
                new_style_tasks[name][task_name] = task
            if default is not None:
                new_style_tasks[name].default = default
    return new_style_tasks, classic_tasks, default_task

def is_task_module(a):
    """
    Determine if the provided value is a task module
    """
    #return (type(a) is types.ModuleType and
    #        any(map(is_task_object, vars(a).values())))
    if type(a) is types.ModuleType and a not in _seen:
        # Flag module as seen
        _seen.add(a)
        # Signal that we need to check it out
        return True

def is_task_object(a):
    """
    Determine if the provided value is a ``Task`` object.

    This returning True signals that all tasks within the fabfile
    module must be Task objects.
    """
    return isinstance(a, Task) and a.use_task_objects

def parse_options():
    """
    Handle command-line options with optparse.OptionParser.

    Return list of arguments, largely for use in `parse_arguments`.
    """
    #
    # Initialize
    #

    parser = OptionParser(usage="fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...")

    #
    # Define options that don't become `env` vars (typically ones which cause
    # Fabric to do something other than its normal execution, such as
    # --version)
    #

    # Version number (optparse gives you --version but we have to do it
    # ourselves to get -V too. sigh)
    parser.add_option('-V', '--version',
        action='store_true',
        dest='show_version',
        default=False,
        help="show program's version number and exit"
    )

    # List Fab commands found in loaded fabfiles/source files
    parser.add_option('-l', '--list',
        action='store_true',
        dest='list_commands',
        default=False,
        help="print list of possible commands and exit"
    )

    # Like --list, but text processing friendly
    parser.add_option('--shortlist',
        action='store_true',
        dest='shortlist',
        default=False,
        help="alias for -F short --list"
    )

    # Control behavior of --list
    LIST_FORMAT_OPTIONS = ('short', 'normal', 'nested')
    parser.add_option('-F', '--list-format',
        choices=LIST_FORMAT_OPTIONS,
        default='normal',
        help="formats --list, choices: %s" % ", ".join(LIST_FORMAT_OPTIONS)
    )

    # Display info about a specific command
    parser.add_option('-d', '--display',
        metavar='COMMAND',
        help="print detailed info about a given command and exit"
    )

    #
    # Add in options which are also destined to show up as `env` vars.
    #

    for option in env_options:
        parser.add_option(option)

    #
    # Finalize
    #

    # Return three-tuple of parser + the output from parse_args (opt obj, args)
    opts, args = parser.parse_args()
    return parser, opts, args

def _is_task(name, value):
    """
    Is the object a task as opposed to e.g. a dict or int?
    """
    return is_classic_task((name, value)) or is_task_object(value)

def _sift_tasks(mapping):
    tasks, collections = [], []
    for name, value in mapping.iteritems():
        if _is_task(name, value):
            tasks.append(name)
        elif isMappingType(value):
            collections.append(name)
    tasks = sorted(tasks)
    collections = sorted(collections)
    return tasks, collections

def _task_names(mapping):
    """
    Flatten & sort task names in a breadth-first fashion.

    Tasks are always listed before submodules at the same level, but within
    those two groups, sorting is alphabetical.
    """
    tasks, collections = _sift_tasks(mapping)
    for collection in collections:
        module = mapping[collection]
        if hasattr(module, 'default'):
            tasks.append(collection)
        join = lambda x: ".".join((collection, x))
        tasks.extend(map(join, _task_names(module)))
    return tasks

def _crawl(name, mapping):
    """
    ``name`` of ``'a.b.c'`` => ``mapping['a']['b']['c']``
    """
    key, _, rest = name.partition('.')
    value = mapping[key]
    if not rest:
        return value
    return _crawl(rest, value)

def crawl(name, mapping):
    try:
        result = _crawl(name, mapping)
        # Handle default tasks
        if isinstance(result, _Dict) and getattr(result, 'default', False):
            result = result.default
        return result
    except (KeyError, TypeError):
        return None

def _print_docstring(docstrings, name):
    if not docstrings:
        return False
    docstring = crawl(name, state.commands).__doc__
    if type(docstring) in types.StringTypes:
        return docstring

def _normal_list(docstrings=True):
    result = []
    task_names = _task_names(state.commands)
    # Want separator between name, description to be straight col
    max_len = reduce(lambda a, b: max(a, len(b)), task_names, 0)
    sep = '  '
    trail = '...'
    for name in task_names:
        output = None
        docstring = _print_docstring(docstrings, name)
        if docstring:
            lines = filter(None, docstring.splitlines())
            first_line = lines[0].strip()
            # Truncate it if it's longer than N chars
            size = 75 - (max_len + len(sep) + len(trail))
            if len(first_line) > size:
                first_line = first_line[:size] + trail
            output = name.ljust(max_len) + sep + first_line
        # Or nothing (so just the name)
        else:
            output = name
        result.append(indent(output))
    return result

def _nested_list(mapping, level=1):
    result = []
    tasks, collections = _sift_tasks(mapping)
    # Tasks come first
    result.extend(map(lambda x: indent(x, spaces=level * 4), tasks))
    for collection in collections:
        module = mapping[collection]
        # Section/module "header"
        result.append(indent(collection + ":", spaces=level * 4))
        # Recurse
        result.extend(_nested_list(module, level + 1))
    return result

COMMANDS_HEADER = "Available commands"
NESTED_REMINDER = " (remember to call as module.[...].task)"

def list_commands(docstring, format_):
    """
    Print all found commands/tasks, then exit. Invoked with ``-l/--list.``

    If ``docstring`` is non-empty, it will be printed before the task list.

    ``format_`` should conform to the options specified in
    ``LIST_FORMAT_OPTIONS``, e.g. ``"short"``, ``"normal"``.
    """
    # Short-circuit with simple short output
    if format_ == "short":
        return _task_names(state.commands)
    # Otherwise, handle more verbose modes
    result = []
    # Docstring at top, if applicable
    if docstring:
        trailer = "\n" if not docstring.endswith("\n") else ""
        result.append(docstring + trailer)
    header = COMMANDS_HEADER
    if format_ == "nested":
        header += NESTED_REMINDER
    result.append(header + ":\n")
    c = _normal_list() if format_ == "normal" else _nested_list(state.commands)
    result.extend(c)
    return result

def display_command(name):
    """
    Print command function's docstring, then exit. Invoked with -d/--display.
    """
    # Sanity check
    command = crawl(name, state.commands)
    if command is None:
        msg = "Task '%s' does not appear to exist. Valid task names:\n%s"
        abort(msg % (name, "\n".join(_normal_list(False))))
    # Print out nicely presented docstring if found
    if command.__doc__:
        print("Displaying detailed information for task '%s':" % name)
        print('')
        print(indent(command.__doc__, strip=True))
        print('')
    # Or print notice if not
    else:
        print("No detailed information available for task '%s':" % name)
    sys.exit(0)

def _escape_split(sep, argstr):
    """
    Allows for escaping of the separator: e.g. task:arg='foo\, bar'

    It should be noted that the way bash et. al. do command line parsing, those
    single quotes are required.
    """
    escaped_sep = r'\%s' % sep

    if escaped_sep not in argstr:
        return argstr.split(sep)

    before, _, after = argstr.partition(escaped_sep)
    startlist = before.split(sep)  # a regular split is fine here
    unfinished = startlist[-1]
    startlist = startlist[:-1]

    # recurse because there may be more escaped separators
    endlist = _escape_split(sep, after)

    # finish building the escaped value. we use endlist[0] becaue the first
    # part of the string sent in recursion is the rest of the escaped value.
    unfinished += sep + endlist[0]

    return startlist + [unfinished] + endlist[1:]  # put together all the parts

def parse_arguments(arguments):
    """
    Parse string list into list of tuples: command, args, kwargs, hosts, roles.

    See docs/usage/fab.rst, section on "per-task arguments" for details.
    """
    cmds = []
    for cmd in arguments:
        args = []
        kwargs = {}
        hosts = []
        roles = []
        exclude_hosts = []
        if ':' in cmd:
            cmd, argstr = cmd.split(':', 1)
            for pair in _escape_split(',', argstr):
                k, _, v = pair.partition('=')
                if _:
                    # Catch, interpret host/hosts/role/roles/exclude_hosts
                    # kwargs
                    if k in ['host', 'hosts', 'role', 'roles', 'exclude_hosts']:
                        if k == 'host':
                            hosts = [v.strip()]
                        elif k == 'hosts':
                            hosts = [x.strip() for x in v.split(';')]
                        elif k == 'role':
                            roles = [v.strip()]
                        elif k == 'roles':
                            roles = [x.strip() for x in v.split(';')]
                        elif k == 'exclude_hosts':
                            exclude_hosts = [x.strip() for x in v.split(';')]
                    # Otherwise, record as usual
                    else:
                        kwargs[k] = v
                else:
                    args.append(k)
        cmds.append((cmd, args, kwargs, hosts, roles, exclude_hosts))
    return cmds

def parse_remainder(arguments):
    """
    Merge list of "remainder arguments" into a single command string.
    """
    return ' '.join(arguments)

def _merge(hosts, roles, exclude=[]):
    """
    Merge given host and role lists into one list of deduped hosts.
    """
    # Abort if any roles don't exist
    bad_roles = [x for x in roles if x not in state.env.roledefs]
    if bad_roles:
        abort("The following specified roles do not exist:\n%s" % (
            indent(bad_roles)
        ))

    # Look up roles, turn into flat list of hosts
    role_hosts = []
    for role in roles:
        value = state.env.roledefs[role]
        # Handle "lazy" roles (callables)
        if callable(value):
            value = value()
        role_hosts += value

    # Return deduped combo of hosts and role_hosts, preserving order within
    # them (vs using set(), which may lose ordering) and skipping hosts to be
    # excluded.
    cleaned_hosts = _clean_hosts(list(hosts) + list(role_hosts))
    all_hosts = []
    for host in cleaned_hosts:
        if host not in all_hosts and host not in exclude:
            all_hosts.append(host)
    return all_hosts

def _clean_hosts(host_list):
    """
    Clean host strings to ensure no trailing whitespace, etc.
    """
    return [host.strip() for host in host_list]

def get_hosts(command, cli_hosts, cli_roles, cli_exclude_hosts):
    """
    Return the host list the given command should be using.

    See :ref:`execution-model` for detailed documentation on how host lists are
    set.
    """
    # Command line per-command takes precedence over anything else.
    if cli_hosts or cli_roles:
        return _merge(cli_hosts, cli_roles, cli_exclude_hosts)
    # Decorator-specific hosts/roles go next
    func_hosts = getattr(command, 'hosts', [])
    func_roles = getattr(command, 'roles', [])
    if func_hosts or func_roles:
        return _merge(func_hosts, func_roles, cli_exclude_hosts)
    # Finally, the env is checked (which might contain globally set lists from
    # the CLI or from module-level code). This will be the empty list if these
    # have not been set -- which is fine, this method should return an empty
    # list if no hosts have been set anywhere.
    return _merge(state.env['hosts'], state.env['roles'], state.env['exclude_hosts'])

def update_output_levels(show, hide):
    """
    Update state.output values as per given comma-separated list of key names.

    For example, ``update_output_levels(show='debug,warnings')`` is
    functionally equivalent to ``state.output['debug'] = True ;
    state.output['warnings'] = True``. Conversely, anything given to ``hide``
    sets the values to ``False``.
    """
    if show:
        for key in show.split(','):
            state.output[key] = True
    if hide:
        for key in hide.split(','):
            state.output[key] = False

def _run_task(task, args, kwargs):
    # First, try class-based tasks
    if hasattr(task, 'run') and callable(task.run):
        return task.run(*args, **kwargs)
    # Fallback to callable behavior
    return task(*args, **kwargs)

def main():
    """
    Main command-line execution loop.
    """
    try:
        # Parse command line options
        parser, options, arguments = parse_options()

        # Handle regular args vs -- args
        arguments = parser.largs
        remainder_arguments = parser.rargs

        # Update env with any overridden option values
        # NOTE: This needs to remain the first thing that occurs
        # post-parsing, since so many things hinge on the values in env.
        for option in env_options:
            state.env[option.dest] = getattr(options, option.dest)

        # Handle --hosts, --roles, --exclude-hosts (comma separated string =>
        # list)
        for key in ['hosts', 'roles', 'exclude_hosts']:
            if key in state.env and isinstance(state.env[key], basestring):
                state.env[key] = state.env[key].split(',')

        # Handle output control level show/hide
        update_output_levels(show=options.show, hide=options.hide)

        # Handle version number option
        if options.show_version:
            print("Fabric %s" % state.env.version)
            sys.exit(0)

        # Load settings from user settings file, into shared env dict.
        state.env.update(load_settings(state.env.rcfile))

        # Find local fabfile path or abort
        fabfile = find_fabfile()
        if not fabfile and not remainder_arguments:
            abort("""Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.""")

        # Store absolute path to fabfile in case anyone needs it
        state.env.real_fabfile = fabfile

        # Load fabfile (which calls its module-level code, including
        # tweaks to env values) and put its commands in the shared commands
        # dict
        if fabfile:
            docstring, callables, default = load_fabfile(fabfile)
            state.commands.update(callables)

        # Handle case where we were called bare, i.e. just "fab", and print
        # a help message.
        actions = (options.list_commands, options.shortlist, options.display,
            arguments, remainder_arguments, default)
        if not any(actions):
            parser.print_help()
            sys.exit(1)

        # Abort if no commands found
        if not state.commands and not remainder_arguments:
            abort("Fabfile didn't contain any commands!")

        # Now that we're settled on a fabfile, inform user.
        if state.output.debug:
            if fabfile:
                print("Using fabfile '%s'" % fabfile)
            else:
                print("No fabfile loaded -- remainder command only")

        # Shortlist is now just an alias for the "short" list format;
        # it overrides use of --list-format if somebody were to specify both
        if options.shortlist:
            options.list_format = 'short'
            options.list_commands = True

        # List available commands
        if options.list_commands:
            print("\n".join(list_commands(docstring, options.list_format)))
            sys.exit(0)

        # Handle show (command-specific help) option
        if options.display:
            display_command(options.display)

        # If user didn't specify any commands to run, show help
        if not (arguments or remainder_arguments or default):
            parser.print_help()
            sys.exit(0)  # Or should it exit with error (1)?

        # Parse arguments into commands to run (plus args/kwargs/hosts)
        commands_to_run = parse_arguments(arguments)

        # Parse remainders into a faux "command" to execute
        remainder_command = parse_remainder(remainder_arguments)

        # Figure out if any specified task names are invalid
        unknown_commands = []
        for tup in commands_to_run:
            if crawl(tup[0], state.commands) is None:
                unknown_commands.append(tup[0])

        # Abort if any unknown commands were specified
        if unknown_commands:
            abort("Command(s) not found:\n%s" \
                % indent(unknown_commands))

        # Generate remainder command and insert into commands, commands_to_run
        if remainder_command:
            r = '<remainder>'
            state.commands[r] = lambda: api.run(remainder_command)
            commands_to_run.append((r, [], {}, [], [], []))

        # Ditto for a default, if found
        if not commands_to_run and default:
            commands_to_run.append((default.name, [], {}, [], [], []))

        if state.output.debug:
            names = ", ".join(x[0] for x in commands_to_run)
            print("Commands to run: %s" % names)

        # At this point all commands must exist, so execute them in order.
        for name, args, kwargs, cli_hosts, cli_roles, cli_exclude_hosts in commands_to_run:
            # Get callable by itself
            task = crawl(name, state.commands)
            # Set current task name (used for some error messages)
            state.env.command = name
            # Set host list (also copy to env)
            state.env.all_hosts = hosts = get_hosts(
                task, cli_hosts, cli_roles, cli_exclude_hosts)
            # If hosts found, execute the function on each host in turn
            for host in hosts:
                # Preserve user
                prev_user = state.env.user
                # Split host string and apply to env dict
                username, hostname, port = interpret_host_string(host)
                # Log to stdout
                if state.output.running:
                    print("[%s] Executing task '%s'" % (host, name))
                # Actually run command
                _run_task(task, args, kwargs)
                # Put old user back
                state.env.user = prev_user
            # If no hosts found, assume local-only and run once
            if not hosts:
                _run_task(task, args, kwargs)
        # If we got here, no errors occurred, so print a final note.
        if state.output.status:
            print("\nDone.")
    except SystemExit:
        # a number of internal functions might raise this one.
        raise
    except KeyboardInterrupt:
        if state.output.status:
            print >> sys.stderr, "\nStopped."
        sys.exit(1)
    except:
        sys.excepthook(*sys.exc_info())
        # we might leave stale threads if we don't explicitly exit()
        sys.exit(1)
    finally:
        disconnect_all()
    sys.exit(0)


from multiprocessing import context
from django.shortcuts import render
from users.models import Profile

def profiles(request):
    profile = Profile.objects.all()
    context = {
        'profiles' : profile
    }
    return render(request, 'users/profiles.html', context)

def user_profile(request, pk):
    user_profile = Profile.objects.get(id=pk)

    top_skills = user_profile.skill_set.exclude(description__exact="")
    other_skills = user_profile.skill_set.filter(description="")

    context = {
        'profile' : user_profile,
        'top_skills' : top_skills,
        'other_skills' : other_skills,
    }
    return render(request, 'users/user_profile.html', context)


import os
from pathlib import Path

import torch

def main():
    base_dir = Path('/home/kirill/data/stanford/')
    print(os.listdir(base_dir / 'models'))
    checkpoint = torch.load(base_dir / 'models/resnet50.pth.tar')
    print(checkpoint)

if __name__ == '__main__':
    main()


import sqlite3

class Connection:
    def __init__(self, db_file):
        self.conn = None
        self.db_file = db_file

    def __enter__(self):
        self.conn = sqlite3.Connection(self.db_file)
        return self.conn

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_tb or exc_val or exc_tb:
            self.conn.close()
            print(f"error {exc_type}, {exc_val}, {exc_tb}")
        else:
            self.conn.commit()
            self.conn.close()


import random
import omegaconf
import os

from tqdm import tqdm
from typing import List, Union

class DataPreparator:
    def __init__(self, config: omegaconf.dictconfig.DictConfig) -> None:
        self.config = config

    def _get_original_paths(self) -> List:
        """Gets each png file path.

        Returns:
            List: list of each images of dataset paths.
        """
        random.seed(7)
        original_paths = list()
        for id_ in tqdm(os.listdir(self.config.data_preparation.dataset_path)):
            if 'IDC' not in id_:
                id_dir = str(self.config.data_preparation.dataset_path) + '/' + id_
                for label in os.listdir(id_dir):
                    label_dir = id_dir + '/' + label
                    for image in os.listdir(label_dir):
                        if not os.path.isdir(image):
                            original_paths.append(label_dir + '/' + image)
        random.shuffle(original_paths)
        return original_paths

    def get_train_val_paths(self) -> Union[List, List, List]:
        """Splits the original paths to train, val.

        Returns:
            Union[List, List, List]: list of train and val paths.
        """

        original_paths = self._get_original_paths()
        index = int(len(original_paths) * self.config.data_pipeline.train_split)
        train_path = original_paths[: index]
        index = int(len(train_path) * self.config.data_pipeline.val_split)
        val_paths = train_path[: index]
        train_paths = train_path[index: ]
        return train_paths, val_paths

    def get_test_paths(self) -> Union[List, List, List]:
        """Splits the original paths to test.

        Returns:
            Union[List, List, List]: list of test paths.
        """
        original_paths = self._get_original_paths()
        index = int(len(original_paths) * self.config.data_pipeline.train_split)
        test_paths = original_paths[index: ]
        return test_paths


import dataclasses
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Set

from taipy.config.common.scope import Scope

from ..common.alias import JobId

@dataclass
class _DataNodeModel:

    id: str
    config_id: str
    scope: Scope
    storage_type: str
    name: str
    owner_id: Optional[str]
    parent_ids: List[str]
    last_edit_date: Optional[str]
    job_ids: List[JobId]
    version: str
    cacheable: bool
    validity_days: Optional[float]
    validity_seconds: Optional[float]
    edit_in_progress: bool
    data_node_properties: Dict[str, Any]

    def to_dict(self) -> Dict[str, Any]:
        return {**dataclasses.asdict(self), "scope": repr(self.scope)}

    @staticmethod
    def from_dict(data: Dict[str, Any]):
        return _DataNodeModel(
            id=data["id"],
            config_id=data["config_id"],
            scope=Scope._from_repr(data["scope"]),
            storage_type=data["storage_type"],
            name=data["name"],
            owner_id=data.get("owner_id", data.get("parent_id")),
            parent_ids=data["parent_ids"],
            last_edit_date=data.get("last_edit_date", data.get("last_edition_date")),
            job_ids=data["job_ids"],
            version=data["version"],
            cacheable=data["cacheable"],
            validity_days=data["validity_days"],
            validity_seconds=data["validity_seconds"],
            edit_in_progress=bool(data.get("edit_in_progress", data.get("edition_in_progress", False))),
            data_node_properties=data["data_node_properties"],
        )


import heapq
from collections import defaultdict
from typing import List

class Solution:
    def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:
        def dijkstra(start, end):
            dist[start] = 0
            q = [(0, start)]
            while q:
                nod = heapq.heappop(q)[1]
                for vecin, cost in graph[nod]:
                    if dist[nod] * cost < dist[vecin]:
                        dist[vecin] = dist[nod] * cost
                        heapq.heappush(q, (dist[vecin], vecin))
                    if vecin == end:
                        return

        graph = defaultdict(list)
        for i, edge in enumerate(edges):
            x = edge[0]
            y = edge[1]
            graph[x].append((y, -succProb[i]))
            graph[y].append((x, -succProb[i]))

        dist = [float("inf")] * (len(graph) + 1)
        dijkstra(start, end)
        return -dist[end]

if __name__ == '__main__':
    print(Solution().maxProbability(3, [[0, 1], [1, 2], [0, 2]], [0.5, 0.5, 0.2], 0, 2))


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
import numpy as np
import pickle
from utils import load_data,preprocess_features,preprocess_adj,tuple_to_torchSparseTensor
from gcn_model import GCN

adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data("cora")

print(adj.shape)  # (2708, 2708)
adj_hat = preprocess_adj(adj)
features = preprocess_features(features)
# features[0].shape == (49216, 2)
# features[1].sahpe == (49216,)
# features[2] === (2708, 1433)

# Convert to torch.Tensor
sparse_adj_hat = tuple_to_torchSparseTensor(adj_hat)
sparse_features = tuple_to_torchSparseTensor(features)

y_train = torch.FloatTensor(y_train)  # dtype = torch.float32
y_val = torch.FloatTensor(y_val)
y_test = torch.FloatTensor(y_test)

train_mask = torch.from_numpy(train_mask)  # dtype = torch.bool
val_mask = torch.from_numpy(val_mask)
test_mask = torch.from_numpy(test_mask)

# Build model and optimizer
model = GCN(input_dim=features[2][1],out_dim=y_train.shape[1],hidden_dims=[32])
optimizer = torch.optim.Adam(model.parameters(), lr = 0.01)  #NOTE Adam performs much better than SGD when training this GCN

# train loop
iter_all = 200
val_loss_dec_max = 10
val_loss_dec_count = 0
previous_val_loss = 999.9
from tqdm import tqdm
for ii in tqdm(range(iter_all)):
    optimizer.zero_grad() # zero the gradient buffers
    # output = model(sparse_adj_hat,sparse_features,is_sparse=True)
    output = model(sparse_adj_hat,sparse_features)

    train_loss = model.loss(output,y_train,train_mask)
    train_acc = model.accuracy(output,y_train,train_mask)
    val_loss = model.loss(output,y_val,val_mask)
    val_acc = model.accuracy(output,y_val,val_mask)

    if ii%20==0:
        print("iter_{}:train_loss=={},train_acc=={}; val_loss=={},val_acc=={},dec_count={}".format(ii,train_loss.item(),train_acc.item(),val_loss.item(),val_acc.item(),val_loss_dec_count))

    if val_loss.item() >= previous_val_loss:
        val_loss_dec_count += 1
    else:
        val_loss_dec_count = 0
    previous_val_loss = val_loss.item()

    if val_loss_dec_count >= val_loss_dec_max:
        print("Early stop occurred! The validation loss does not decrease for {} consecutive epochs".format(val_loss_dec_max))
        print("current iter={}, train_loss=={},train_acc=={}; val_loss=={},val_acc=={}".format(ii,train_loss.item(),train_acc.item(),val_loss.item(),val_acc.item()))
        break

    train_loss.backward()
    optimizer.step()

torch.save(model, 'training_dir/gcn_model.pkl')


# -*- coding: utf-8 -*-
import cv2,os
video_path = input("input video path:")
video_dir =os.path.splitext(video_path)[0] + "-crop" + os.path.splitext(video_path)[1]

def crop_video(video_path):
    video=cv2.VideoCapture(video_path)
    fps = int(round(video.get(cv2.CAP_PROP_FPS)))
    print("fps:"+str(fps))
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    new_hight = 1080
    new_width = int(width*new_hight/height)
    cv2.namedWindow(video_path, 0)
    cv2.resizeWindow(video_path, new_width, new_hight)  # 设置窗口大小
    grabbed, frame = video.read()
    cv2.imshow(video_path,frame)
    # 选择ROI
    roi = cv2.selectROI(windowName=video_path, img=frame, showCrosshair=True, fromCenter=False)
    x, y, w, h = roi
    print(roi)
    # 显示ROI并保存图片
    if roi != (0, 0, 0, 0):
        crop = frame[y:y+h, x:x+w]
        cv2.imshow(video_path, crop)
    else:
        crop = frame
        w = width
        h = height
        cv2.imshow(video_path, crop)
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    videoWriter = cv2.VideoWriter(video_dir,fourcc,fps,(w,h),True)
    videoWriter.write(crop)
    while True:
        grabbed, frame = video.read()
        crop = frame[y:y+h, x:x+w]
        cv2.imshow(video_path, crop)
        cv2.waitKey(5)
        videoWriter.write(crop)
        if not grabbed:
            print("End of video")
            break
    video.release()
    videoWriter.release()
    cv2.destroyAllWindows()
crop_video(video_path)


import json
import logging
from collections import defaultdict
from typing import Any, DefaultDict, Iterable

import requests
from django.conf import settings
from django.db import transaction
from requests_gssapi import HTTPSPNEGOAuth

from corgi.collectors.brew import Brew
from corgi.collectors.models import (
    CollectorErrataProduct,
    CollectorErrataProductVariant,
    CollectorErrataProductVersion,
    CollectorErrataRelease,
)
from corgi.core.models import SoftwareBuild

logger = logging.getLogger(__name__)

BREW_TAG_CANDIDATE_SUFFIX = "-candidate"

class ErrataTool:
    """Interface to the Errata Tool APIs."""

    GSSAPI_AUTH = HTTPSPNEGOAuth()

    def __init__(self):
        self.session = requests.Session()
        self.session.auth = self.GSSAPI_AUTH

    def get(
        self,
        path: str,
        **request_kwargs: Any,
    ) -> dict[str, Any]:
        """Get the response to a REST API call or raise an exception."""
        url = f"{settings.ERRATA_TOOL_URL}/{path}"
        response = self.session.get(url, **request_kwargs)
        response.raise_for_status()
        return response.json()

    def get_paged(
        self, path: str, page_data_attr: str = "data", pager: str = "page[number]"
    ) -> list[dict[str, Any]]:
        """Generator to iterate over data from paged Errata Tool API endpoint."""
        params = {pager: 1}
        data: list[dict[str, Any]] = []
        while True:
            page = self.get(path, params=params)
            page_data: Iterable[dict[str, Any]] = page[page_data_attr]
            if page_data:
                data.extend(page_data)
                params[pager] += 1
            else:
                break
        return data

    def load_et_products(self) -> None:
        # preload the data from ET before starting a DB transaction
        products_and_versions = self.get_products_and_versions()
        releases = self.get_paged("api/v1/releases")
        variants = self.get_paged("api/v1/variants")
        with transaction.atomic():
            self._delete_collector_objects()
            self.load_products_and_versions(products_and_versions)
            self.load_releases(releases)
            self.load_variants(variants)

    def _delete_collector_objects(self) -> None:
        CollectorErrataProduct.objects.all().delete()
        CollectorErrataProductVersion.objects.all().delete()
        CollectorErrataRelease.objects.all().delete()
        CollectorErrataProductVariant.objects.all().delete()

    @staticmethod
    def load_products_and_versions(products_and_versions: list[dict[str, Any]]) -> None:
        for product in products_and_versions:
            product_versions = product.pop("product_versions", [])
            et_product = CollectorErrataProduct.objects.create(
                et_id=product.pop("id"),
                name=product["attributes"].pop("name"),
                short_name=product["attributes"].pop("short_name"),
                meta_attr=product,
            )
            logger.debug(f"Created ET Product: {et_product.short_name}")
            for product_version in product_versions:
                name = product_version["attributes"].pop("name")
                brew_tags = ErrataTool.strip_brew_tag_candidate_suffixes(
                    product_version.pop("brew_tags")
                )
                CollectorErrataProductVersion.objects.create(
                    et_id=product_version.pop("id"),
                    name=name,
                    product=et_product,
                    brew_tags=brew_tags,
                    meta_attr=product_version,
                )
                logger.debug(f"Created ET ProductVersion: {name}")

    @staticmethod
    def strip_brew_tag_candidate_suffixes(brew_tags: list[str]) -> list[str]:
        return [t.removesuffix(BREW_TAG_CANDIDATE_SUFFIX) for t in brew_tags]

    def load_releases(self, releases: list[dict[str, Any]]) -> None:
        for release in releases:
            attributes = release["attributes"]
            name = attributes.pop("name")
            relationships = release["relationships"]
            brew_tags_dict = relationships.pop("brew_tags")
            brew_tags = [tag["name"] for tag in brew_tags_dict]
            release_obj = CollectorErrataRelease.objects.create(
                et_id=release.pop("id"),
                name=name,
                is_active=attributes.pop("is_active"),
                enabled=attributes.pop("enabled"),
                brew_tags=self.strip_brew_tag_candidate_suffixes(brew_tags),
                meta_attr=attributes,
            )
            logger.debug(f"Created ET Release: {name}")

            # Sometimes releases can refer to Product Versions which no longer exist
            product_version_names = [pv["name"] for pv in relationships.pop("product_versions")]
            product_versions = CollectorErrataProductVersion.objects.filter(
                name__in=product_version_names
            )
            release_obj.product_versions.set(product_versions)

    def load_variants(self, variants: list[dict[str, Any]]) -> None:
        for variant in variants:
            attributes = variant["attributes"]
            product_version = attributes["relationships"].pop("product_version")
            product_version_name = product_version["name"]

            # Sometimes variants can refer to Product Versions which no longer exist
            et_product_version = CollectorErrataProductVersion.objects.filter(
                name=product_version_name
            ).first()
            name = attributes.pop("name")
            cpe = attributes.pop("cpe", "")
            # CORGI-648 the value can be null
            if not cpe:
                cpe = ""
            CollectorErrataProductVariant.objects.create(
                name=name,
                cpe=cpe,
                product_version=et_product_version,
                et_id=variant.pop("id"),
                meta_attr=attributes,
            )
            logger.debug(f"Created ET Product Variant: {name}")

    def get_erratum_components(self, erratum_id: str):
        """Fetch components attached to erratum and index by their Variant.

        Example:

        {
          "RHEL-7.6-AUS": {
            "name": "RHEL-7.6-AUS",
            "description": "Red Hat Enterprise Linux 7.6 Advanced Update Support",
            "builds": [
              {
                "polkit-0.112-18.el7_6.3": {
                  "nvr": "polkit-0.112-18.el7_6.3",
                  "nevr": "polkit-0:0.112-18.el7_6.3",
                  "id": 1848203,
                  "is_module": false,
                  "variant_arch": {
                    "7Server-7.6.AUS": {
                      "SRPMS": [
                        "polkit-0.112-18.el7_6.3.src.rpm"
                      ],
                      "x86_64": [
                        "polkit-0.112-18.el7_6.3.x86_64.rpm",
                        "polkit-0.112-18.el7_6.3.i686.rpm",
                        "polkit-devel-0.112-18.el7_6.3.x86_64.rpm",
                        "polkit-devel-0.112-18.el7_6.3.i686.rpm",
                        "polkit-debuginfo-0.112-18.el7_6.3.x86_64.rpm",
                        "polkit-debuginfo-0.112-18.el7_6.3.i686.rpm"
                      ],
                      "noarch": [
                        "polkit-docs-0.112-18.el7_6.3.noarch.rpm"
                      ]
                    }
                  },
                  "added_by": "jrybar"
                }
              }
            ]
          }
        }
        """
        variant_to_component_map: DefaultDict[str, list] = defaultdict(list)
        builds = self.get(f"api/v1/erratum/{erratum_id}/builds_list.json")
        brew = Brew(SoftwareBuild.Type.BREW)
        for product_version in builds.values():
            for build in product_version["builds"]:
                for build_nvr, build_data in build.items():
                    if build_data["is_module"]:
                        self._parse_module(
                            build_nvr, build_data["variant_arch"], brew, variant_to_component_map
                        )
                        continue
                    build_id = build_data["id"]
                    for variant, components_by_arch in build_data["variant_arch"].items():
                        variant_components = []
                        for _, components in components_by_arch.items():
                            # Collect all components as a single list, their arch information is
                            # included in the NVRA string anyway.
                            variant_components.extend(components)
                        variant_to_component_map[variant].append({build_id: variant_components})

        return dict(variant_to_component_map)

    def save_variant_cdn_repo_mapping(self):
        """Fetches all existing Errata Tool Variants and the CDN repos they are configured with.

        Each erratum's builds are mapped against individual Variants that determine which repos
        the content gets pushed to.
        """

        for repo in self.get_paged("api/v1/cdn_repos"):
            repo_attrs = repo["attributes"]
            repo_rels = repo["relationships"]

            for variant in repo_rels["variants"]:
                variant_name = variant["name"]
                try:
                    variant_obj = CollectorErrataProductVariant.objects.get(name=variant_name)
                except CollectorErrataProductVariant.DoesNotExist:
                    logger.debug(
                        "Repo %s links to a non-existent variant: %s",
                        repo_attrs["name"],
                        variant_name,
                    )
                    continue
                uniq_repos = set(variant_obj.repos)
                uniq_repos.add(repo_attrs["name"])
                variant_obj.repos = list(uniq_repos)
                logger.info("Linking repo %s to variant %s", repo_attrs["name"], variant_name)
                variant_obj.save()

    @staticmethod
    def get_variant_cdn_repo_mapping() -> dict[str, list[str]]:
        variants_with_repos = {
            variant.name: variant.repos
            for variant in CollectorErrataProductVariant.objects.exclude(repos__len=0)
        }
        return variants_with_repos

    def get_errata_key_details(self, name: str) -> tuple[int, bool]:
        erratum_details = self.get(f"api/v1/erratum/{name}")
        errata_type_details = list(erratum_details["errata"].values())
        if len(errata_type_details) != 1:
            raise ValueError(
                f"Erratum with name {name} has more than one type key: {errata_type_details}"
            )
        erratum_id = errata_type_details[0]["id"]
        shipped_live = errata_type_details[0]["status"] == "SHIPPED_LIVE"
        return erratum_id, shipped_live

    def get_builds_for_errata(self, errata_id: int) -> list[int]:
        build_ids = set()
        for variant in self.get_erratum_components(str(errata_id)).values():
            for component in variant:
                build_ids.update(component.keys())
        return list(build_ids)

    def _parse_module(
        self,
        module_name: str,
        module_data: dict[str, dict[str, list[str]]],
        brew: Brew,
        variant_to_component_map: DefaultDict[str, list],
    ) -> None:
        """Persist collector models for modular builds from errata build_list.json. This allows
        later processing of modular builds in the Brew task slow_fetch_modular_build"""
        for variant, components_by_arch in module_data.items():
            modular_rpms = []
            for arch, components in components_by_arch.items():
                # SRPMS are looked up in persist_modules from Brew via RPMs
                if arch == "SRPMS":
                    continue
                # persist_modules expects the .rpm suffix stripped
                rpm_suffix_len = ".rpm"
                stripped_components = [
                    rpm[: -len(rpm_suffix_len)] for rpm in components if rpm.endswith(".rpm")
                ]
                modular_rpms.extend(stripped_components)
            build_ids = brew.persist_modules({module_name: modular_rpms})
            for build_id in build_ids:
                # persist_modules returns a flat list of build_ids, so its no longer possible to
                # map modular_rpms to build_ids. In slow_save_errata these are only saved as
                # meta_attr so, let's just save the entire list for each build_id
                variant_to_component_map[variant].append({build_id: modular_rpms})

    def get_erratum_notes(self, erratum_id: int) -> dict:
        # Get the contents of the "Notes" (formerly "How to test") field from an erratum.
        # Quarkus uses this field to associate an SBOM with an erratum.
        erratum_json = self.get(f"api/v1/erratum/{erratum_id}?format=json")
        try:
            # All products that use SBOMer should have notes set
            # NB: Though the field in the ET UI is called "Notes", the field name in the API
            # is "how_to_test", which was the original purpose of the Notes field.
            notes = json.loads(erratum_json["content"]["content"]["how_to_test"])
        except json.JSONDecodeError as error:
            logger.warning(f"Couldn't load Notes for erratum {erratum_id}")
            raise error
        return notes

    def get_products_and_versions(self) -> list[dict[str, Any]]:
        products = self.get_paged("api/v1/products")
        for product in products:
            product_versions: list[dict[str, Any]] = []
            pv_relation = product["relationships"].pop("product_versions")
            for pv in pv_relation:
                response = self.get(f"api/v1/products/{product['id']}/product_versions/{pv['id']}")
                data = response["data"]
                product_versions.append(data)
            product["product_versions"] = product_versions
        return products


import streamlit as st
import urllib.parse as parse

# fixed values
S_default_value = r'''\begin{align*}
R(g) &= \frac{1}{n} \sum_{i=1}^{n} \ell(y_i,g(x_i))\\
&=\frac{1}{2n} (\mathbf{X}\boldsymbol{w}-\mathbf{y})^T (\mathbf{X}\boldsymbol{w}-\mathbf{y})
\end{align*}
'''

text_area_height = 220

# input
st.title('tex image link generator')
S = st.text_area(label='Input tex equations',
                 value=S_default_value,
                 height=text_area_height)

# display styleを個々に追加したい
style_dict = {'default': '\displaystyle ',
              'inline': '\\textstyle '}
style_prefix = style_dict[
    st.selectbox('Select style', list(style_dict.keys()))]

size_dict = {'M': '',
             'L': '\large ',
             '2L': '\Large ',
             '3L': '\LARGE ',
             '4L': '\huge ',
             '5L': '\Huge '}

size_prefix = size_dict[st.selectbox('Select size', list(size_dict.keys()))]

color_dict = {'black': '\color{black}',
             'white': '\color{white}'}

color_prefix = color_dict[st.selectbox('Select color', list(color_dict.keys()))]

# encode
S_quote = parse.quote_plus(color_prefix + size_prefix + style_prefix + S)

url_prefix = 'https://render.githubusercontent.com/render/math?math='
URL = url_prefix + S_quote

tag_prefix = '<img src=\n"'
tag_suffix = '" \nalt="' + S + '">'
TAG = tag_prefix + URL + tag_suffix

md_prefix = '![' + S + ']('
md_suffix = ')'

MD = md_prefix + URL + md_suffix

'## Image Preview'
st.markdown(TAG, unsafe_allow_html=True)

'## HTML'
st.code(TAG, language='html')

'## Markdown'
st.code(MD, language='')
# st.code(URL)

# 'メモ書き'
# 'latex result'
# st.latex(S)
# 'markdown tex result'
# st.markdown('$$' + S + '$$')

# reference
''
''
''
'Project repository https://github.com/masakiaota/tex_image_link_generator'


from airflow.models.dag import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.sqlite_operator import SqliteOperator
from airflow.operators.bash_operator import BashOperator
from airflow.operators.sqlite_operator import SqliteOperator
from airflow.operators.sqlite_operator import SqliteOperator
from airflow.operators.bash_operator import BashOperator
from airflow.operators.bash_operator import BashOperator
from airflow.operators.email_operator import EmailOperator
from airflow.operators.bash_operator import BashOperator

store_dag = DAG(**{"dag_id": "store_DAG", "start_date": "'2020-6-30'"})

clean_raw_csv = PythonOperator(
    **{"python_callable": "data_cleaner", "task_id": "clean_raw_csv"},
    dag=store_dag
)
insert_into_table = SqliteOperator(
    **{
        "sql": "insert_into_table.sql",
        "sqlite_conn_id": "mysql_conn",
        "task_id": "insert_into_table",
    },
    dag=store_dag
)
check_file_exists = BashOperator(
    **{
        "bash_command": "shasum ~/store_files_airflow/raw_store_transactions_2020-06-30.csv",
        "task_id": "check_file_exists",
    },
    dag=store_dag
)
create_mysql_table = SqliteOperator(
    **{
        "sql": "create_table.sql",
        "sqlite_conn_id": "mysql_conn",
        "task_id": "create_mysql_table",
    },
    dag=store_dag
)
select_from_table = SqliteOperator(
    **{
        "sql": "select_from_table.sql",
        "sqlite_conn_id": "mysql_conn",
        "task_id": "select_from_table",
    },
    dag=store_dag
)
move_file1 = BashOperator(
    **{
        "bash_command": "cat ~/store_files_airflow/location_wise_profit.csv && mv ~/store_files_airflow/location_wise_profit.csv ~/store_files_airflow/location_wise_profit_%s.csv' % yesterday_date",
        "task_id": "move_file1",
    },
    dag=store_dag
)
move_file2 = BashOperator(
    **{
        "bash_command": "cat ~/store_files_airflow/store_wise_profit.csv && mv ~/store_files_airflow/store_wise_profit.csv ~/store_files_airflow/store_wise_profit_%s.csv' % yesterday_date",
        "task_id": "move_file2",
    },
    dag=store_dag
)
send_email = EmailOperator(
    **{
        "to": "shivanshsinghchauhan@gmail.com",
        "subject": "Daily report generated",
        "html_content": "
Congratulations! Your store reports are ready.
",
        "task_id": "send_email",
    },
    dag=store_dag
)
rename_raw = BashOperator(
    **{
        "bash_command": "mv ~/store_files_airflow/raw_store_transactions_2020-06-30.csv ~/store_files_airflow/raw_store_transactions_2020-06-31.csv",
        "task_id": "rename_raw",
    },
    dag=store_dag
)

check_file_exists >> clean_raw_csv >> create_mysql_table >> insert_into_table >> select_from_table >> move_file1 >> send_email >> rename_raw
select_from_table >> move_file2 >> send_email


from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'roomManager.views.index', name = 'index'),
    url(r'^checkIn/$', 'roomManager.views.checkIn', name = 'checkIn'),

    #url(r'^checkIn/(?P<id>\d+)/$', 'roomManager.views.checkIn', name = 'checkIn'),
    url(r'^payBalance/$', 'roomManager.views.payBalance', name = 'payBalance'),
    url(r'^addalance/$', 'roomManager.views.addBalance', name = 'addBalance'),

    url(r'^queryCenter/$', 'roomManager.views.queryCenter', name = 'queryCenter'),
    url(r'^queryByRoomId/$', 'roomManager.views.queryByRoomId', name = 'queryByRoomId'),
    url(r'^queryByRoomType/$', 'roomManager.views.queryByRoomType', name = 'queryByRoomType'),
    url(r'^queryByBedType/$', 'roomManager.views.queryByBedType', name = 'queryByBedType'),
    url(r'^queryByFloorId/$', 'roomManager.views.queryByFloorId', name = 'queryByFloorId'),

    url(r'^myRoom/$', 'roomManager.views.myRoom', name = 'myRoom'),
    url(r'^checkOut/$', 'roomManager.views.checkOut', name = 'checkOut'),
    url(r'^deleteRoom/(?P<id>\d+)/$', 'roomManager.views.deleteRoom', name = 'deleteRoom'),
)


# coding: utf-8
import os
import numpy as np
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
from sklearn.externals import joblib
from sklearn import svm
import matplotlib.pyplot as plt
from sayuri.machine import MachineLoader

MODEL_FILE = os.path.join(os.path.dirname(__file__), MachineLoader.MACHINE_FILE)

def load_data(skip_header=1):
    import os
    data_file = os.path.join(os.path.dirname(__file__), "training_data_with_label.csv")
    header = []
    with open(data_file, newline=os.linesep) as f:
        header = next(f).replace(os.linesep, "").split(",")
    dataset = np.genfromtxt(data_file, delimiter=",", skip_header=skip_header)
    y = dataset[:, 0]  # left side data is label
    X = dataset[:, range(1, dataset.shape[1])]

    return {"dataset": dataset, "header": header, "y": y, "X": X}

def make_model(y, X, columns, save_model=False):
    # select feature
    _X = X[:, columns]

    # reguralization
    scaler = preprocessing.StandardScaler()
    _X = scaler.fit_transform(_X)

    x_train, x_test, y_train, y_test = train_test_split(_X, y, test_size=0.25, random_state=42)

    candidates = [{'kernel': ["rbf"], 'gamma': [1e-3, 1e-4], 'C': [1, 10, 100]},
                  {'kernel': ['linear'], 'C': [1, 10, 100]}]

    clf = GridSearchCV(svm.SVC(C=1), candidates, cv=5, scoring="f1")
    clf.fit(x_train, y_train)

    for params, mean_score, scores in sorted(clf.grid_scores_, key=lambda s: s[1], reverse=True):
        print("%0.3f (+/-%0.03f) for %r" % (mean_score, scores.std() / 2, params))

    model = clf.best_estimator_
    y_predict = model.predict(x_test)

    print(classification_report(y_test, y_predict, target_names=["good", "bad"]))

    # plot boundary
    x_min, x_max = x_test[:, 0].min() - 1, x_test[:, 0].max() + 1
    y_min, y_max = x_test[:, 1].min() - 1, x_test[:, 1].max() + 1
    h = .02  # step size
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    z = model.predict(np.c_[xx.ravel(), yy.ravel()])
    mean = {}
    mean["0"] = [np.average(xx.ravel()[z == 0]), np.average(yy.ravel()[z == 0])]
    mean["1"] = [np.average(xx.ravel()[z == 1]), np.average(yy.ravel()[z == 1])]
    z = z.reshape(xx.shape)
    plt.contourf(xx, yy, z, cmap=plt.cm.Paired)
    plt.axis('off')

    # plot test result (2d)
    plt.scatter(x_test[y_test == y_predict, 0], x_test[y_test == y_predict, 1], color="blue", cmap=plt.cm.Paired)
    plt.scatter(x_test[y_test != y_predict, 0], x_test[y_test != y_predict, 1], color="red", cmap=plt.cm.Paired)
    plt.scatter(mean["0"][0], mean["0"][1], color="green", cmap=plt.cm.Paired)
    plt.scatter(mean["1"][0], mean["1"][1], color="green", cmap=plt.cm.Paired)

    plt.show()

    if save_model:
        pipe = Pipeline([("scaling", scaler), ("estimator", model)])
        if sum(pipe.predict(scaler.inverse_transform(x_test)) != y_predict) > 0:
            raise Exception("Pipeline is not correct")
        joblib.dump(pipe, MODEL_FILE)
        return pipe

    return model


import bpy

S1  ="builtin.select"
#背面選択アドオン「Box Select X-Ray」必須。
#https://blenderartists.org/t/box-select-x-ray/1212316
#なければデフォルトの物を選択ってやりたいけど未対応
S2  ="mesh_tool.select_box_xray"
S3  ="builtin.select_box"
S4  ="builtin.move"

def set_active_tool(tool_name):
    for area in bpy.context.screen.areas:
        if area.type == "VIEW_3D":
            override = bpy.context.copy()
            override["space_data"] = area.spaces[0]
            override["area"] = area
            bpy.ops.wm.tool_set_by_id(override, name=tool_name)

#選択オブジェクトの属性やモードによって選択を切り替える
def si_MoveComponent(a,b,c,select_tool):
    oSel = bpy.context.selected_objects
    for o in oSel:
        if o.type == "MESH":
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.context.tool_settings.mesh_select_mode = (a,b,c)
            set_active_tool(select_tool)
        elif o.type == "CURVE":
            bpy.ops.object.mode_set(mode='EDIT')
            set_active_tool(S4)
        elif o.type == "SURFACE":
            bpy.ops.object.mode_set(mode='EDIT')
            set_active_tool(S4)

        #アーマチュアやカーブなど、オブジェクトモードなら編集に入るが
        #POSEモードなら何もしない。
        elif o.type == "ARMATURE":
            if bpy.context.mode == "POSE":
                print("Pose now")
            else:
                bpy.ops.object.mode_set(mode='EDIT')

class si_MoveComponent_OT_object(bpy.types.Operator):
    bl_idname = "object.si_movecomponent"
    bl_label = "Move Component Tool"
    bl_description = "Move Component Tool like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context,):
        si_MoveComponent(True,True,True,S1)
        return {'FINISHED'}

class si_MoveComponent1_OT_object(bpy.types.Operator):
    bl_idname = "object.si_movecomponent1"
    bl_label = "Point Select Tool"
    bl_description = "Point Select Tool like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context,):
        si_MoveComponent(True,False,False,S2)
        return {'FINISHED'}

class si_MoveComponent2_OT_object(bpy.types.Operator):
    bl_idname = "object.si_movecomponent2"
    bl_label = "Edge Select Tool"
    bl_description = "Edge Select Tool like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context,):
        si_MoveComponent(False,True,False,S2)
        return {'FINISHED'}

class si_MoveComponent3_OT_object(bpy.types.Operator):
    bl_idname = "object.si_movecomponent3"
    bl_label = "Polygon Select Tool"
    bl_description = "Polygon Select Tool like Softimage"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context,):
        si_MoveComponent(False,False,True,S2)
        return {'FINISHED'}


"""
General utility functions
"""
import subprocess
import os
import shutil
import shlex
import glob
import logging

def parse_cmd(cmd):
    """Parse shell command for globbing and environment variables."""
    if not isinstance(cmd, list):
        cmd = shlex.split(cmd)
    parsed_cmd = [
        cmd[0],
    ]
    for word in cmd[1:]:
        if word[0] == "$":
            var = word[1:].strip("{").strip("}")
            varval = os.environ.get(var, word)
            parsed_cmd.append(varval)
        else:
            if "*" in word:
                items = glob.glob(word)
                for item in items:
                    parsed_cmd.append(item)
            else:
                parsed_cmd.append(word)
    return parsed_cmd

def execute(cmd, workdir=".", outfile="run.log", purge_workdir=False, **kwargs):
    """Execute external command in workdir, streaming output/error to outfile.

    Args:
        cmd (str): command; executed in `workir`; if it contains `$` or
                   `*`-globbing, these are shell-expanded
        workdir (path-like): execution directory relative to workroot
        outfile (str): output file for the stdout/stderr stream; continuously
                       updated during execution
        purge_workdir (bool): if true, any existing working directory is purged
        kwargs (dict): passed directly to the underlying `subprocess.call()`

    Returns:
        None

    Raises:
        OSError: if `cmd` cannot be executed
        RuntimeError: if `cmd` returncode is nonzero
        SubprocessError: other possible circumstances
    """
    # prepare workdir
    origdir = os.getcwd()
    try:
        os.makedirs(workdir)
    except OSError:
        # directory exists
        if purge_workdir:
            # that's a bit brutal, but saves to worry of links and subdirs
            shutil.rmtree(workdir)
            os.makedirs(workdir)
    os.chdir(workdir)
    # prepare out/err handling
    filename = kwargs.pop("stdout", outfile)
    if filename:
        kwargs["stdout"] = open(filename, "w")
    filename = kwargs.pop("stderr", None)
    if filename:
        kwargs["stderr"] = open(filename, "w")
    else:
        kwargs["stderr"] = subprocess.STDOUT
    # execute the command, make sure output is not streamed
    _cmd = parse_cmd(cmd)
    try:
        returncode = subprocess.call(_cmd, **kwargs)
        if returncode:
            LOGGER.critical(
                "Execution of {:s} FAILED with exit status {:d}".format(
                    _cmd, returncode
                )
            )
            raise RuntimeError
    #
    except subprocess.SubprocessError:
        LOGGER.critical("Subprocess call of {:s} FAILED".format(_cmd))
        raise
    #
    except (OSError, FileNotFoundError) as exc:
        LOGGER.critical(
            "Abnormal termination: OS could not execute %s in %s", _cmd, workdir
        )
        LOGGER.critical(
            "If the command is a script ,"
            "check permissions and that is has a shebang!"
        )
        raise
    #
    finally:
        # make sure we return to where we started from in any case!
        os.chdir(origdir)

def configure_logger(name, filename=None, verbosity=logging.INFO):
    """Get parent logger: logging INFO on the console and DEBUG to file."""
    if filename is None:
        filename = name + ".debug.log"
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    # may need this if running within ipython notebook, to avoid duplicates
    logger.propagate = False
    # console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(verbosity)
    # file handler with full debug info
    fh = logging.FileHandler(filename, mode="w")
    fh.setLevel(logging.DEBUG)
    # message formatting
    fileformat = logging.Formatter("%(name)s - %(levelname)s: %(message)s")
    consformat = logging.Formatter("%(levelname)7s: %(message)s")
    fh.setFormatter(fileformat)
    ch.setFormatter(consformat)
    # add the configured handlers
    logger.addHandler(fh)
    logger.addHandler(ch)
    return logger

def get_logger(name, filename=None, verbosity=logging.INFO):
    """Return a named logger with file and console handlers.

    Get a `name`-logger. Check if it is(has) a parent logger.
    If parent logger is not configured, configure it, and if a child logger
    is needed, return the child.
    The check for parent logger is based on `name`: a child if it contains '.',
    i.e. looking for 'parent.child' form of `name`.
    A parent logger is configured by defining a console handler at `verbosity`
    level, and a file handler at DEBUG level, writing to `filename`.
    """
    parent = name.split(".")[0]
    if filename is None:
        filename = parent + ".debug.log"
    parent_logger = logging.getLogger(parent)
    if not parent_logger.handlers:
        configure_logger(parent, filename, verbosity)
    return logging.getLogger(name)

LOGGER = get_logger(__name__)


"""
This module provides generic component methods for your Flask app.

A component is a class which provides the "glue" between your models and your
data marshalling/validation. The advantages to this pattern are that it allows
for extensive code reuse, a single place for business logic, and consistent
handling for validation that leverages the `marshmallow` library.
"""
from abc import ABC

from flask import current_app, url_for

from .model import GenericModel
from .schema import GenericSchema
from .shortcuts import (
    DEFAULT_DICT, MODEL_INST, MODEL_TYPE, SCHEMA_INST, SCHEMA_TYPE, SELECT_INST
)

class GenericComponent(ABC):
    """
    A generic component with all the basics needed to perform CRUD operations
    on a database table via your Python app. This component is designed to be
    extended for your model; simply inherit from it, and specify the
    appropriate model and corresponding schema.
    """
    schema: SCHEMA_TYPE = GenericSchema
    model: MODEL_INST = GenericModel

    def __init__(self, context=DEFAULT_DICT, *args, **kwargs):
        """
        Set context for the class. Context is a great way to pass data around
        the component and methods.
        """
        self.context: dict = context

    @classmethod
    def add_metadata(cls, query: SELECT_INST, context: dict) -> (
            SELECT_INST, dict):
        """
        Add in metadata for API methods. This enables for basic pagination
        support via the schema.
        """
        offset: int = context.get('offset', 1)
        limit: int = context.get('limit', 0)

        total: int = query.count()

        remaining: int = 0
        if query and offset:
            query: SELECT_INST = query.paginate(offset, limit)
            remaining: int = max(
                total - (limit * (offset - 1)) - query.count(), 0
            )

        context['metadata'] = {
            'count': total,
            'remaining': remaining,
            'offset': offset,
            'limit': limit,
            'next': context.get('next') if remaining > 0 else None,
            'previous': context.get('previous') if offset > 1 else None,
            'route_args': context.get('route_args', {}),
        }

        return query, context

    @staticmethod
    def context_urls(route_name: str, **kwargs: dict) -> (str, str):
        """
        Generate next and previous urls for pagination purposes.
        """
        context_next: str = url_for(
            route_name,
            _external=True,
            _scheme='https' if current_app.config.get('USE_HTTPS') else 'http',
            **kwargs
        )

        context_previous: str = url_for(
            route_name,
            _external=True,
            _scheme='https' if current_app.config.get('USE_HTTPS') else 'http',
            **kwargs
        )

        return context_next, context_previous

    def base_query(self) -> SELECT_INST:
        """
        Surface a base query for this model. This method is meant to be
        overridden for the specific model. For instance, if a JOIN condition
        should be part of the default querying, or if query optimization is
        needed, this is an excellent place to put it to avoid bloating the
        models.
        """
        return self.model.base_query()

    def search(self, query: SELECT_INST, request_args: dict=DEFAULT_DICT) -> (
            SELECT_INST):  # pylint: disable=unused-argument
        """
        This method is designed to be overridden to provide an interface for
        searching via query parameters passed in from the endpoint.
        """
        return query

    def get_by_id(self, item_id: int) -> MODEL_TYPE:
        """
        Convenience function for getting an item by ID. This may be overridden,
        for example to provide for permission schemes via business logic in the
        component.
        """
        return self.model.get(self.model.id == item_id)

    def get_items(self, request_args: dict=DEFAULT_DICT,
                  context: dict=DEFAULT_DICT) -> dict:
        """
        Generic method to retrieve all items from the database for the model
        associated with this component, and serialize it to a dictionary via
        the schema. This method is designed to be flexible in filtering results
        via overriding the `search` method.
        """
        items: SELECT_INST = self.search(self.base_query(), request_args)

        if self.context.get('wrapper'):
            items, context = self.add_metadata(items, self.context)

        return self.schema(context=context).dump(items, many=True).data

    def get_item(self, item_id) -> dict:
        """
        Retrieve a specific item from the database for the model associated
        with this component by ID, and serialize it to a dictionary via the
        schema.
        """
        return self.schema().dump(self.get_by_id(item_id), many=False).data

    def create_item(self, data: dict) -> dict:
        """
        Create an item for the associated model. This method will serialize
        and validate the data before model creation, then will serialize the
        return data into a dictionary.
        """
        schema: SCHEMA_INST = self.schema()
        item: MODEL_INST = schema.make_instance(data)
        item.save()
        return schema.dump(item, many=False).data

    def update_item(self, item_id: int, data: dict) -> dict:
        """
        Update an existing item from the database with new data. This method
        will serialize and validate the data before updating the model item,
        then will serialize the return data into a dictionary.
        """
        schema: SCHEMA_INST = self.schema()
        serialized_data: dict = schema.load(data).data
        item: MODEL_INST = self.get_by_id(item_id)
        item.update_instance(serialized_data)

        return schema.dump(item, many=False).data

    def delete_item(self, item_id: int) -> dict:
        """
        Delete an item via ID. This will return the data from the deleted item.
        """
        schema: SCHEMA_INST = self.schema(context=self.context)
        item: MODEL_INST = self.get_by_id(item_id)
        item.delete_instance()
        item.save()

        return schema.dump(item, many=False).data


'''Modules for terminal menu and screen clear.'''
import os
from simple_term_menu import TerminalMenu
from dog import Dog
import dogs_sql

def menu():
    '''Loops through menu until quit.'''
    options = ["Add Dog", "Delete Dog", "Get Info", "Feeding/Meds", "Grooming/Belongings", "Quit"]
    main_menu = TerminalMenu(options)
    info_options = ["All", "Large", "Small", "Search Name"]
    info_menu = TerminalMenu(info_options)

    quit = False

    while quit is False:
        options_list = main_menu.show()
        options_choice = options[options_list]

        if options_choice == "Quit":
            quit = True

        if options_choice == "Add Dog":
            name = input("Dog's name: ")
            owner = input("Owner's name: ")
            breed = input("Breed: ")
            while True:
                size = input("Large or Small: ")
                if size not in ("Large", "Small"):
                    print("Please enter Large or Small")
                else:
                    break
            while True:
                try:
                    age = int(input("Age: "))
                except ValueError:
                    print("Please enter a number")
                else:
                    break
            gender = input("Gender: ")
            feed_meds = input("Medication or special food required: ")
            grooming = input("Do they need grooming?: ")
            belongings = input("Do they have any belongings?: ")
            friendly = input("Are they friendly?: ")
            dog = Dog(name, owner, breed, size, age, gender,
                    feed_meds, grooming, belongings, friendly)
            dogs_sql.add_dog(dog)

        if options_choice == "Delete Dog":
            name = input("Name: ")
            dogs_sql.delete_dog(name)

        if options_choice == "Get Info":
            options_list = info_menu.show()
            options_choice = info_options[options_list]
            if options_choice == "All":
                print(dogs_sql.get_dogs())
            if options_choice == "Large":
                print(dogs_sql.get_large_dogs())
            if options_choice == "Small":
                print(dogs_sql.get_small_dogs())
            if options_choice == "Search Name":
                name = input("Name: ")
                print(dogs_sql.get_info(name))

        if options_choice == "Feeding/Meds":
            print(dogs_sql.feed_meds())

        if options_choice == "Grooming/Belongings":
            print(dogs_sql.out_groom())
            print(dogs_sql.out_belongings())

        else:
            print(options_choice)

    os.system("clear")

menu()