from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

def ngrams(input, n):
    input = re.sub('\n+', ' ', input)  # 去掉连续转义字符\n
    input = re.sub(' +', ' ', input)   # 去掉连续空格
    input = bytes(input,'UTF-8')
    input = input.decode('ascii', 'ignore')
    input = input.split(' ')
    output = []
    for i in range(len(input)-n+1):
        output.append(input[i:i+n])
    return output

html = urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)")
soup = BeautifulSoup(html,'html.parser')
content = soup.find('div',{'id':'mw-content-text'}).get_text()
test = ngrams(content,2)
print(test)
print('2-grams count is: '+str(len(test)))

# 消除转义字符的是下一行代码的ignore参数，转义字符因为不能被ascii decode，如果不加ignore就会报错，加了ignore就会被忽略。
# contents是一个str，由一系列不可改变的Unicode字符组成，本身不能被decode，用utf8 encode之后转为bytes才可以被decode。
# encode是转为计算机能理解的二进制数，bytes就是一系列不可改变的介于0-255之间的数字。decode就是转为我们可以理解的字符。
# ascii字符集远小于unicode字符集，用ascii decode的时候那些不在ascii字符集的字符就会导致报错，设了ignore参数后就会被忽略。


import numpy as np
import pandas as pd
import os

def get_data(data_path, seqs):

    data_frames = []
    for i in seqs:
        seq_path = f'000{i}' if i < 10 else f'00{i}'
        dir_path = f'{data_path}/{seq_path}'
        objects = os.listdir(dir_path)
        for object_id in objects:
            od_path = dir_path + f"/{object_id}"
            frames = os.listdir(od_path)
            label = int(object_id)
            num_frames = len(frames)
            if num_frames > 3:
                paths = np.array([od_path + f"/{f}" for f in frames if f[-4:]
                                  in ('.npy', '.npz')])
                ob_labels = [label for _ in paths]
                obj_data = pd.DataFrame()
                obj_data["path"] = paths
                obj_data["label"] = np.array(ob_labels)
                # print(obj_data.shape)
                data_frames.append(obj_data)
    data = pd.concat(data_frames)
    return data


import pandas
import numpy
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB, BernoulliNB, MultinomialNB
from sklearn.svm import SVC
from sklearn import metrics
from sklearn.linear_model import LogisticRegression

def read_file(file_name):
    return pandas.read_csv(file_name)

def label_encoder(file):
    encoder = LabelEncoder()
    for column in file.columns:
        file[column] = encoder.fit_transform(file[column])
    return file

def train_and_test_set(x, y):
    return train_test_split(x, y, random_state=30)

def separate_set(file):
    # csv files consist of 14 columns, last one being the type of disease,
    return file.iloc[:, 0:9], file.iloc[:, 13]

def logistic_regression(x_train, x_test, y_train, y_test):
    print("Logistic regression:")

    #liblinear
    lr_model = LogisticRegression().fit(x_train, y_train)
    y_predicted = lr_model.predict(x_test)
    print("\t Logistic regression (liblinear solver) score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    #newton-cg
    lr_model = LogisticRegression(multi_class='multinomial', solver='newton-cg').fit(x_train, y_train)
    y_predicted = lr_model.predict(x_test)
    print("\t Logistic regression (multinomial, newton-cg solver) score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    print("*******************************************")

def support_vector_machine(x_train, x_test, y_train, y_test):

    print("SVM:")

    #Linear
    svm_model = SVC(kernel='linear').fit(x_train, y_train)
    y_predicted = svm_model.predict(x_test)
    print("\t Linear score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    #RBF
    svm_model = SVC(kernel='rbf').fit(x_train, y_train)
    y_predicted = svm_model.predict(x_test)
    print("\t RBF score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    print("*******************************************")

def naive_bayes(x_train, x_test, y_train, y_test):
    print("NAIVE BAYES")

    # Gaussian
    nb_model = GaussianNB().fit(x_train, y_train)
    y_prob = nb_model.predict(x_test)
    print("\tGaussian naive bayes score: " + str(metrics.f1_score(y_test, y_prob, average='micro')))

    # Bernoulli
    nb_model = BernoulliNB().fit(x_train, y_train)
    y_prob = nb_model.predict(x_test)
    print("\tBernoulli naive bayes score: " + str(metrics.f1_score(y_test, y_prob, average='micro')))

    # Multinomial
    nb_model = MultinomialNB().fit(x_train, y_train)
    y_prob = nb_model.predict(x_test)
    print("\tMultinomial naive bayes score: " + str(metrics.f1_score(y_test, y_prob, average='micro')))

    print("*******************************************")

def k_neighbors_classifier(x_train, x_test, y_train, y_test):
    print("K NEIGHBORS CLASSIFIER:")

    # Euclidean
    knn = KNeighborsClassifier(n_neighbors=3, metric='euclidean').fit(x_train, y_train)
    y_predicted = knn.predict(x_test)
    print("\t Euclidean metrics score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    # Manhattan
    knn = KNeighborsClassifier(n_neighbors=4, metric='manhattan').fit(x_train, y_train)
    y_predicted = knn.predict(x_test)
    print("\t Manhattan metrics score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    # Chebyshev
    knn = KNeighborsClassifier(n_neighbors=3, metric='chebyshev').fit(x_train, y_train)
    y_predicted = knn.predict(x_test)
    print("\t Chebyshev neighbors score: " + str(metrics.f1_score(y_test, y_predicted, average='micro')))

    print("*******************************************")

def analyze_database(file_path, db_name):
    data = label_encoder(read_file(file_path))
    x, y = separate_set(data)
    print(db_name);
    print("\nType of disease recognition scores\n")
    x_train, x_test, y_train, y_test = train_and_test_set(x, y)
    support_vector_machine(x_train, x_test, y_train, y_test) # SVM
    naive_bayes(x_train, x_test, y_train, y_test) # Naive Bayes
    k_neighbors_classifier(x_train, x_test, y_train, y_test) # K Neighbors
    logistic_regression(x_train, x_test, y_train, y_test) # Logistic regression

    print("\nIs disease present recognition scores\n")
    y = numpy.where(y >= 1, 1, 0)
    x_train, x_test, y_train, y_test = train_and_test_set(x, y)
    support_vector_machine(x_train, x_test, y_train, y_test) # SVM
    naive_bayes(x_train, x_test, y_train, y_test) # Naive Bayes
    k_neighbors_classifier(x_train, x_test, y_train, y_test) # K Neighbors
    logistic_regression(x_train, x_test, y_train, y_test) # Logistic regression

# Main
if __name__ == '__main__':
    #cleveland
    analyze_database('data/cleveland.csv', "CLEVELAND DATABASE")
    #switzerland
    analyze_database('data/switzerland.csv', "SWITZERLAND DATABASE")
    #va
    analyze_database('data/va.csv', "VA DATABASE")
    #hungarian
    analyze_database('data/hungarian.csv', "HUNGARIAN DATABASE")


import logging
import sys
import json

from sonrai_api import token

# globals
logger = logging.getLogger("sonrai_api")
api_token = token.verify_token()

# Create the Logger for use with all modules
# https://docs.python.org/3/library/logging.html#logrecord-objects
logging.basicConfig(
    format='[%(asctime)s] [%(levelname)s] -- %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    stream=sys.stdout
)

# Set ERROR for initial loading of information
level = logging.getLevelName('ERROR')
logger.setLevel(level)

# Constants from config
try:
    with open('sonrai_api/config.json') as json_file:
        config = json.load(json_file)
except json.decoder.JSONDecodeError:
    logging.error("Unable to parse the config.json file")
    exit(255)
except FileNotFoundError:
    logging.error("Unable to find the config.json file")
    exit(254)

# define Python user-defined exceptions
class SonraiAPIException(Exception):
    """Base class for other exceptions"""
    pass

# Set level according to the config file.
level = logging.getLevelName(config['sonrai-token-log-level'])
logger.setLevel(level)


from flask import Flask, request, abort

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)
from time import time

app = Flask(__name__)

line_bot_api = LineBotApi('') #チャンネルアクセストークン
handler = WebhookHandler('') #チャンネルシークレット

@app.route("/")
def test():
    return "OK"

@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body)

    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        print("Invalid signature. Please check your channel access token/channel secret.")
        abort(400)

    return 'OK'

users = {}
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    userId = event.source.user_id
    if event.message.text == "ありがとう":
        reply_message = "どういたしまして。"
    elif event.message.text == "": #質問
         reply_message = "" #返答
    elif event.message.text == "計測開始":
        reply_message = "計測を開始しました"
        if not userId in users:
            users[userId] = {}
            users[userId]["total"] = 0
        users[userId]["start"] = time()
    elif event.message.text == "計測終了":
        end = time()
        difference = int(end - users[userId]["start"])
        users[userId]["total"] += difference
        hour = difference // 3600
        minute = (difference % 3600) // 60
        second = difference % 60
        t_hour = users[userId]["total"] // 3600
        t_minute = (users[userId]["total"] % 3600) // 60
        t_second = users[userId]["total"] % 60
        reply_message = f"{hour}時間{minute}分{second}秒\n合計{t_hour}時間{t_minute}分{t_second}秒\n計測を終了しました"
    else:
        reply_message = "" #返事
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text = reply_message))

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


import os
import re
import csv
import json
import time
import spacy
import string
import requests
from tqdm import tqdm

nlp = spacy.load("en_core_web_sm")

# read all the outputs
def read_skill_outputs(skill_name):
    with open('results/' + skill_name + '/content_safety/outputs.csv') as f:
        reader = csv.reader(f)
        outputs = []
        for row in reader:
            outputs.append(row)
    return outputs

def read_media_outputs(skill_name):
    outputs = []
    with open('results/' + skill_name + '/content_safety/outputs_image.csv') as f:
        reader = csv.reader(f)
        for row in reader:
            outputs.append(row)
    with open('results/' + skill_name + '/content_safety/outputs_mp3.csv') as f:
        reader = csv.reader(f)
        for row in reader:
            outputs.append(row)
    #with open('results/outputs_html.csv') as f:
    #    reader = csv.reader(f)
    #    for row in reader:
    #        outputs.append(row[0])
    return outputs

def write_results(filename, outputs):
    with open('results/' + filename, 'w', newline = '') as f:
        writer = csv.writer(f)
        for output in outputs:
            x = writer.writerow(output)

def get_data_collection_outputs(outputs):
    noun = ['address', 'name', 'email', 'email address', 'birthday', 'age', 'gender', 'location', 'contact', 'phonebook', 'profession', 'income', 'ssn', 'zipcode', 'ethnicity', 'affiliation', 'orientation', 'affiliation', 'postal code', 'zip code', 'first name', 'last name', 'full name', 'phone number', 'social security number', 'passport number', 'driver license', 'bank account number', 'debit card number']
    noun2 = [word.split()[-1] for word in noun]
    add_sentences = {"how old are you": 'age', "when were you born": 'age', "where do you live":'location' ,"where are you from": 'location', "what can i call you": 'name', 'male or female': 'gender', 'what city do you live in?': 'location'}
    words = ['companion app', 'alexa app', 'amazon.com', 'permission', 'enable', 'grant']
    skills = []
    for output in outputs:
        filename, output = output
        output = output.lower()
        if 'you' not in output:
            continue
        if ' ' not in output:
            continue
        sentences = re.split(r' *[\n\,\.!][\'"\)\]]* *', output)
        for sentence in sentences:
            if any (word in output for word in words):
                continue
            if any ('your ' + word + ' is' in sentence for word in noun):
                continue
            if any (word in sentence for word in noun) and 'your' in sentence:
                doc = nlp(sentence)
            for word in noun:
                if word not in sentence or 'your' not in sentence:
                    continue
                if word == 'name' and 'your name' not in sentence:
                    continue
                if word == 'address' and 'email address' in sentence:
                    continue
                if word == 'phone number' and 'dial your local emergency' in sentence:
                    continue
                for l in doc:
                    if l.text == 'your' and l.head.text in noun2 and l.head.text in word:
                        if 'name' in word:
                            skills.append((filename, output, 'collect data name'))
                        else:
                            skills.append((filename, output, 'collect data ' + word))
            for sent in add_sentences:
                    if sent in sentence.translate(str.maketrans('', '', string.punctuation)):
                        skills.append((filename, output, 'collect data ' + add_sentences[sent]))
    return set(skills)

def get_skill_data_collection_in_outputs(skill_name, outputs):
    data_collection_outputs = get_data_collection_outputs(outputs)
    write_results(skill_name + '/content_safety/outputs_data_collection.csv', data_collection_outputs)

# send outputs to perspective and check their toxicity
def get_toxic_request(skill_name, outputs):
    api_key = open('tokens.txt').read().split('\n')[1][13:]
    url = ('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze' + '?key=' + api_key)
    if len(outputs) == 0:
        outputs = [['', ' ']]
    for output in tqdm(outputs[:1000]):
        filename, output = output
        data_dict = {'comment': {'text': output},'languages': ['en'],'requestedAttributes': {'TOXICITY': {},'SEVERE_TOXICITY':{},'IDENTITY_ATTACK':{},'INSULT':{},'PROFANITY':{},'THREAT':{}}}
        response = requests.post(url = url, data = json.dumps(data_dict))
        response_dict = json.loads(response.content.decode())
        time.sleep(1)
        with open('results/' + skill_name + '/content_safety/toxic_result.txt', 'a') as f:
            x = f.write(filename + '\t' + str(output) + '\t')
            json.dump(response_dict, f)
            x = f.write('\n')

def get_toxic_result(skill_name, outputs):
    results = open('results/' + skill_name + '/content_safety/toxic_result.txt').read().split('\n')[:-1]
    toxic = {}
    for result in results:
        try:
            filename, sentence, response = result.split('\t')
            response_dict = json.loads(response)
            if 'attributeScores' not in response_dict.keys():
                continue
            if response_dict['attributeScores']['PROFANITY']['summaryScore']['value'] < 0.9:
                continue
            if response_dict['attributeScores']['TOXICITY']['summaryScore']['value'] < 0.9:
                continue
            toxic[sentence] = response_dict
        except:
            continue
    results = []
    for output in outputs:
        filename, output = output
        if output in toxic:
            results.append((filename, output))
    return results

def get_toxic_output(skill_name, outputs):
    get_toxic_request(skill_name, outputs)
    toxic_content = get_toxic_result(skill_name, outputs)
    write_results(skill_name + '/content_safety/outputs_toxic.csv', toxic_content)

# a faster version for positive rating (don't check verb)
def get_positive_rating(skill_name, outputs):
    keywords = ['5 star review', 'five star review', '5-star review', '5 star rating', 'five star rating', '5-star rating']
    outputs_with_5_star = []
    for output in outputs:
        filename, output = output
        if any (word in output[1] for word in keywords):
            outputs_with_5_star.append((filename, output))
    write_results(skill_name + '/content_safety/outputs_positive_rating.csv', outputs_with_5_star)

# a complete version that uses nlp to check result
def get_positive_rating(skill_name, outputs):
    keywords = ['5 star review', 'five star review', '5-star review', '5 star rating', 'five star rating', '5-star rating']
    outputs_with_5_star = []
    for output in outputs:
        filename, output = output
        if any (word in output for word in keywords):
            doc = nlp(output)
            for sentence in doc.sents:
                if any (word in sentence.text for word in keywords):
                    for word in sentence:
                        if word.text == 'star' and (word.head.head.lemma_ == 'leave' or word.head.head.lemma_ == 'give'):
                            outputs_with_5_star.append((filename, output))
    write_results(skill_name + '/content_safety/outputs_positive_rating.csv', outputs_with_5_star)

def get_content_safety(skill_name):
    outputs = read_skill_outputs(skill_name)
    outputs = outputs + read_media_outputs(skill_name)
    # check data collection (prepare for Privacy Violation, pre-6.4.1)
    get_skill_data_collection_in_outputs(skill_name, outputs)
    # check toxic content, asking for positive rating (Content Safety 6.5.1)
    print('---------- Sending outputs to Perspective to scan toxicity ----------')
    get_toxic_output(skill_name, outputs)
    get_positive_rating(skill_name, outputs)


import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
import cv2
import pytesseract
from PIL import Image

def filter(img):
    blur = cv2.bilateralFilter(img,9,75,75)
    blur = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((1, 1), np.uint8)
    blur = cv2.dilate(blur, kernel, iterations=1)
    blur = cv2.erode(blur, kernel, iterations=1)
    ##blur = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
    return blur
def ocr(path):
    img = cv2.imread(path)
    text=filter(img)
    pytesseract.pytesseract.tesseract_cmd= 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
    out = pytesseract.image_to_string(text)
    out=out.replace('\"','^')
    out=out.replace('—','-')
    out=out.replace(" ","")
    return out


import os
import sys

import grpc
from chirpstack_api.as_pb.external import api

# Configuration.

# This must point to the API interface.
server = "localhost:8080"

# The DevEUI for which you want to enqueue the downlink.
dev_eui = bytes([0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01])

# The API token (retrieved using the web-interface).
api_token = "..."

if __name__ == "__main__":
  # Connect without using TLS.
  channel = grpc.insecure_channel(server)

  # Device-queue API client.
  client = api.DeviceQueueServiceStub(channel)

  # Define the API key meta-data.
  auth_token = [("authorization", "Bearer %s" % api_token)]

  # Construct request.
  req = api.EnqueueDeviceQueueItemRequest()
  req.device_queue_item.confirmed = False
  req.device_queue_item.data = bytes([0x01, 0x02, 0x03])
  req.device_queue_item.dev_eui = dev_eui.hex()
  req.device_queue_item.f_port = 10

  resp = client.Enqueue(req, metadata=auth_token)

  # Print the downlink frame-counter value.
  print(resp.f_cnt)


# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/13 19:33
# @Author  : AsiHacker
# @File    : demo
# @Software: PyCharm
# @notice  : True masters always have the heart of an apprentice.
from itertools import product

list1 = range(1, 3)
list2 = range(4, 6)
list3 = range(7, 9)
for item1, item2, item3 in product(list1, list2, list3):
    print(item1 + item2 + item3)


#! /usr/bin/env python
# Ari Lacenski 20151218

import sys, os
import shutil
from densities import for_all_densities

def error_no_destination():
	print("Destination directory does not exist.")

def error_usage():
	print("Usage: " + __file__ + " [source_path] [destination_path]")
	print("Example: " + __file__ + " /Users/ari/Downloads/ic_launcher /Users/ari/workspace/android/android-mango/app/res/drawable")

# source_dir should contain drawable- subdirectories with the image at different densities
# drawable-mdpi, drawable-hdpi, drawable-xhdpi, and so on
try:
	source_dir = sys.argv[1]
except:
	error_usage()
	exit()

try:
	dest_dir = sys.argv[2]
except:
	error_usage()
	exit()

if not dest_dir:
	error_usage()
	exit()

if not os.path.exists(dest_dir):
	error_no_destination()
	exit()

def copy_file(density_prefix, source_dir, dest_dir, asset_filename):

	# confirm that the file exists at the expected density
	source_asset_path = os.path.join(source_dir, "drawable-" + density_prefix, asset_filename)

	file = open(source_asset_path, "r")

	if not file:
		print(source_asset_path + " does not exist. Skipping.")

	else:
		# prune "drawable" off the destination, if provided
		if "drawable" in dest_dir:
			dest_dir = os.path.dirname(dest_dir)

		drawable_dir = "drawable-" + density_prefix
		dest_drawable_dir = os.path.join(dest_dir, drawable_dir)
		if not os.path.exists(dest_drawable_dir):
			os.makedirs(dest_drawable_dir)

		dest_asset_path = os.path.join(dest_dir, drawable_dir, asset_filename)
		shutil.copyfile(source_asset_path, dest_asset_path)

		if not open(dest_asset_path, "r"):
			print("Copying " + source_asset_path + " failed.")
			exit()
		else:
			print(dest_asset_path + " created.")

# prune trailing slashes to prepare dirname
source_dir = source_dir.rstrip(os.sep)

# the unique filename of the asset:
# ic_launcher.png
asset_filename = os.path.basename(source_dir) + ".png"

for_all_densities(copy_file, source_dir=source_dir, dest_dir=dest_dir, asset_filename=asset_filename)


#!/usr/bin/env python3
import sys
import os
import timeit

### NOTE have to keep applying the list of frequency changes ad infinitum

### from reddit Alfred456654
def foo():
    with open('input.txt', 'r') as of:
        nbs = list(map(int, list(map(str.strip, of.readlines()))))
        freqs_reached = set([0])
        freq = 0
        while True:
            for nb in nbs:
                freq += nb
                if freq in freqs_reached:
                    return freq
                freqs_reached.add(freq)

def bar():
    with open('input.txt', 'r') as f:
        dflist = list(map(int, list(map(str.strip, f.readlines()))))

    # starting frequency 0
    f0 = 0
    freqlist = [0]
    while True:
        for df in dflist:
            f0 += df
            if f0 in freqlist:
                return f0
            else:
                freqlist.append(f0)

print("first dupe w/ foo = {}".format(foo()))

print("first dupe w/ bar = {}".format(bar()))


n = int(input())   # n은 지역의 수
local = list(map(int, input().split()))   # 지역별 필요한 예산
m = int(input())   # m은 예산 총액

start = 0
end = max(local)

def binary_search(array, start, end) :
    while start < end :
        mid = (start + end+1) // 2
        total = 0

        for x in array :
            if x > mid :  # 중간값보다 배열의 값이 크면
                total += mid   # 중간값을 더함
            else :        # 중간값보다 배열의 값이 작거나 같으면
                total += x     # 배열의 값을 더함
        if total <= m :   # 모두 더한 합이 예산 총액보다 작거나 같은 경우
            start = mid  # 상한값 조정을 위해 start를 중간값보다 크게 잡음
        else :            # 모두 더한 합이 예산 총액보다 큰 경우
            end = mid - 1    # 상한값을 작게 만들어야 하므로 end를 중간값보다 작게 잡음

        # start값이 end값보다 작은 경우에만 while문이 돌아가므로 start와 end 범위를 줄이다 보면 mid가 결정됨
    return end

result = binary_search(local, start, end)

print(result)


from django.contrib import admin
from django.shortcuts import redirect
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

from services.views import PatientSignup, LoginRedirect, Profile, CancelAppointment, DepartmentsList, \
    DepartmentAppointments, Home, BookAppointment, EditProfile, PatientsList, NewPeriodicMedication, NewResult, \
    VerifyPatient, EditPatient, NewAppointment, StaffList, EditStaff, Testing

urlpatterns = [
                  path('admin/', admin.site.urls),
                  path('testing/', Testing.as_view()),

                  path('accounts/', include('django.contrib.auth.urls')),
                  path('accounts/signup/', PatientSignup.as_view(), name='signup'),
                  path('home', Home.as_view(), name='home'),
                  path('', lambda req: redirect('home')),
                  path('login/redirect/', LoginRedirect.as_view(), name='login-redirect'),

                  path('profile/edit/', EditProfile.as_view(), name='edit-profile'),
                  path('profile/<int:pk>/', Profile.as_view(), name='profile'),

                  path('cancel-appointment/', CancelAppointment.as_view(), name='cancel-appointment'),
                  path('book-appointment/', BookAppointment.as_view(), name='book-appointment'),

                  path('departments/', DepartmentsList.as_view(), name='departments'),
                  path('departments/<int:pk>', DepartmentAppointments.as_view(), name='department'),

                  path('staff/patients/', PatientsList.as_view(), name='staff-patients'),
                  path('staff/list/', StaffList.as_view(), name='staff-list'),
                  path('staff/new/periodic-medication/', NewPeriodicMedication.as_view(), name='staff-new-pm'),
                  path('staff/new/appointment/', NewAppointment.as_view(), name='staff-new-appointment'),
                  path('staff/new/result/', NewResult.as_view(), name='staff-new-result'),
                  path('staff/api/verification/', VerifyPatient.as_view(), name='staff-api-verification'),
                  path('staff/api/edit-patient/', EditPatient.as_view(), name='staff-api-edit-patient'),
                  path('staff/api/edit-staff/', EditStaff.as_view(), name='staff-api-edit-staff'),

              ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL,
                                                                                document_root=settings.MEDIA_ROOT)


import glob
import os
import operator
import yaml
import collections
import sys

yaml.add_multi_constructor('!', lambda loader, suffix, node: None)

clusters = {}
folders = {}
samcount = 0

samrootfolder = "extracted-sams"
if len(sys.argv) == 2:
	samrootfolder = sys.argv[1]

print("Scanning SAMs...")
samfolders = glob.glob(os.path.join(samrootfolder, "*"))
for samfolder in samfolders:
	sams = os.listdir(samfolder)
	for sam in sams:
		samcount += 1
		rc = []
		y = yaml.load(open(os.path.join(samfolder, sam)))
		if "Resources" in y:
			for r in y["Resources"]:
				if "Type" in y["Resources"][r]:
					t = y["Resources"][r]["Type"]
					rc.append(t)
		rc = sorted(list(set(rc)))
		#print(rc)

		rcs = ",".join(rc)
		clusters[rcs] = clusters.get(rcs, 0) + 1
		if not rcs in folders:
			folders[rcs] = []
		folders[rcs].append(os.path.basename(samfolder))

print("Cluster analysis...")
print("{} clusters over {} SAM files".format(len(clusters), samcount))
print("significant > 5 occurrences")
for rcs in clusters:
	if clusters[rcs] > 5:
		pct = round(100 * clusters[rcs] / samcount, 1)
		print("[{:3d}] [{:4.1f}%] {}".format(clusters[rcs], pct, rcs))
print("complex > 5 resources")
for rcs in clusters:
	if rcs.count(",") > 5:
		print("[{:3d}] ({:2d}x) {}".format(clusters[rcs], rcs.count(","), rcs))
print("significant folders")
for rcs in folders:
	if len(folders[rcs]) > 5:
		print(rcs, folders[rcs])


from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard
import time

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Python Snake Game")
screen.tracer(0)

scoreboard = Scoreboard()
snake = Snake()
food = Food()

game_is_on = True
def stop_game():
    global game_is_on
    game_is_on = False

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down,"Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
screen.onkey(stop_game, "Escape")

while game_is_on:
    screen.update()
    time.sleep(0.1)
    snake.move()

    # Detect food collision.
    if snake.head.distance(food) < 15:
        #print("nom nom nom..")
        food.refresh()
        scoreboard.increment_score()
        snake.eat()

    # Detect wall collision.
    if snake.head.xcor() > 280 or snake.head.ycor() < -280 or snake.head.ycor() > 260 or snake.head.ycor() < -280:
        game_is_on = False
        scoreboard.game_over()

    # Detect tail collision.
    for segment in snake.body[1:]:
        if snake.head.distance(segment) < 10:
            game_is_on = False
            scoreboard.game_over()

screen.exitonclick()


from Movie_World.project.customer import Customer
from Movie_World.project.dvd import DVD

class MovieWorld:
    d_capacity = 15
    c_capacity = 10

    def __init__(self, name):
        self.name = name
        self.customers = []
        self.dvds = []

    @staticmethod
    def dvd_capacity():
        return MovieWorld.d_capacity

    @staticmethod
    def customer_capacity():
        return MovieWorld.c_capacity

    def add_customer(self, customer):
        if MovieWorld.c_capacity > len(self.customers) and customer not in self.customers:
            self.customers.append(customer)
            #MovieWorld.c_capacity -= 1

    def add_dvd(self, dvd):
        if MovieWorld.d_capacity > len(self.dvds):
            self.dvds.append(dvd)
            #MovieWorld.d_capacity -= 1

    def rent_dvd(self, customer_id: int, dvd_id: int):
        all_movie_ids = [movie.id for movie in self.dvds]
        all_cust_ids = [cust.id for cust in self.customers]
        if dvd_id in all_movie_ids and customer_id in all_cust_ids:
            dvd = [dvd for dvd in self.dvds if dvd.id == dvd_id][0]
            customer = [cust for cust in self.customers if cust.id == customer_id][0]
            if dvd in customer.rented_dvds:
                return f"{customer.name} has already rented {dvd.name}"
            if dvd.is_rented:
                return "DVD is already rented"
            if customer.age < dvd.age_restriction:
                return f"{customer.name} should be at least {dvd.age_restriction} to rent this movie"
            dvd.is_rented = True
            customer.rented_dvds.append(dvd)
            return f"{customer.name} has successfully rented {dvd.name}"

    def return_dvd(self, customer_id: int, dvd_id: int):
        all_dvd_ids = [movie.id for movie in self.dvds]
        if dvd_id in all_dvd_ids:
            dvd = [dvd for dvd in self.dvds if dvd.id == dvd_id][0]
            customer = [cust for cust in self.customers if cust.id == customer_id][0]
            if dvd not in customer.rented_dvds:
                return f"{customer.name} does not have that DVD"
            dvd.is_rented = False
            customer.rented_dvds.remove(dvd)
            return f"{customer.name} has successfully returned {dvd.name}"

    def __repr__(self):
        result = '\n'.join([repr(cust) for cust in self.customers]) + '\n'
        result += '\n'.join([repr(dvd) for dvd in self.dvds])
        return result
#------------------------------------------------------------------TEST-SECTION----------------------------
c1 = Customer("John", 16, 1)
c2 = Customer("Anna", 55, 2)
c3 = Customer("George", 21, 3)
c4 = Customer("Jim", 26, 4)
c5 = Customer("Rachel", 27, 5)
c6 = Customer("Kimberly", 43, 6)
c7 = Customer("Ivan", 51, 7)
c8 = Customer("Peter", 9, 8)
c9 = Customer("Alex", 80, 9)
c10 = Customer("Stephan", 19, 10)
c11 = Customer("Austin", 30, 11)

d1 = DVD("Black Widow", 1, 2020, "April", 18)
d2 = DVD.from_date(2, "The Croods 2", "23.12.2020", 8)
d3 = DVD.from_date(3, "The Big Short", "23.11.2020", 5)
d4 = DVD.from_date(4, "Jobs", "23.10.2020", 10)
d5 = DVD.from_date(5, "Billions", "09.12.2020", 23)
d6 = DVD.from_date(6, "The Mechanic", "23.9.2020", 34)
d7 = DVD.from_date(7, "John Wick", "23.07.2020", 56)
d8 = DVD.from_date(8, "Titanic", "23.08.2020", 71)
d9 = DVD.from_date(9, "Rush Hour", "23.06.2020", 21)
d10 = DVD.from_date(10, "Trader", "23.05.2020", 28)
d11 = DVD.from_date(11, "Too Big to Fail", "23.04.2020", 17)
d12 = DVD.from_date(12, "2012", "23.03.2020", 13)
d13 = DVD.from_date(13, "Air Force I", "23.02.2020", 11)
d14 = DVD.from_date(14, "Scary Movie", "23.1.2020", 39)
d15 = DVD.from_date(15, "House of Wax", "23.01.2020", 43)
d16 = DVD.from_date(16, "SAW", "23.12.2020",0)

movie_world = MovieWorld("Movie Arena")

movie_world.add_customer(c1)
movie_world.add_customer(c2)
movie_world.add_customer(c3)
movie_world.add_customer(c4)
movie_world.add_customer(c5)
movie_world.add_customer(c6)
movie_world.add_customer(c7)
movie_world.add_customer(c8)
movie_world.add_customer(c9)

movie_world.add_dvd(d1)
movie_world.add_dvd(d2)
movie_world.add_dvd(d3)
movie_world.add_dvd(d4)
movie_world.add_dvd(d5)
movie_world.add_dvd(d6)
movie_world.add_dvd(d7)
movie_world.add_dvd(d8)
movie_world.add_dvd(d9)
movie_world.add_dvd(d10)
movie_world.add_dvd(d11)
movie_world.add_dvd(d12)
movie_world.add_dvd(d13)
movie_world.add_dvd(d14)
movie_world.add_dvd(d14)

print(repr(c2))
print(repr(d1))

print(movie_world.dvd_capacity())
print(movie_world.customer_capacity())

print(movie_world.rent_dvd(5, 5))
print(movie_world.rent_dvd(5, 5))
movie_world.rent_dvd(6, 6)
print(movie_world.rent_dvd(5, 6))
print(movie_world.rent_dvd(5, 5))
print(movie_world.rent_dvd(5, 14))
print(movie_world.return_dvd(5, 5))
print(movie_world.return_dvd(5, 5))

print(repr(movie_world))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools
import sys
from collections import defaultdict

def read_input(filename):
    table = defaultdict(dict)
    for line in open(filename):
        tokens = line.strip().rstrip('.').split(' ')
        table[tokens[0]][tokens[-1]] = int(tokens[3]) * (-1 if tokens[2] == "lose" else 1)
    return table

def score(table, p):
    s = 0
    for i in range(len(p)):
        pers  = p[i]
        left  = p[(i - 1) % len(p)]
        right = p[(i + 1) % len(p)]
        s += table[pers][left] + table[pers][right]
    return s

def part1(table):
    nodes = list(table.keys())
    best = 0
    for p in itertools.permutations(nodes):
        best = max(best, score(table, p))
    return best

def part2(table):
    for k in list(table.keys()):
        table[k]["me"] = 0
        table["me"][k] = 0
    return part1(table)

def main(filename):
    table = read_input(filename)
    print(part1(table))
    print(part2(table))

if __name__ == '__main__':
    main('input' if len(sys.argv) == 1 else sys.argv[1])


import datetime
import logging
import re
import sys
import time
from collections import defaultdict
from functools import wraps
from typing import List, Optional, Union

import pandas as pd
import progressbar
import pytz

from cityair_api.settings import CHECKINFO_PARSE_PATTERN
from .exceptions import EmptyDataException

logger = logging.getLogger(__name__)

FATHER_PREFIXES = ["CA", "ROOFTOP", "LAB"]

def is_main_device(serial):
    for prefix in FATHER_PREFIXES:
        if serial.startswith(prefix):
            return True
    return False

RIGHT_PARAMS_NAMES = {
        'FlagPs220': '220',
        'RecvDate': 'receive_date', 'Ps220': '220',
        'GsmRssi': 'rssi',
        'SendDate': 'date', 'Temperature': 'T',
        'BatLow': 'is_bat_low',
        'Humidity': 'RH', 'Pressure': 'P',
        'ChildDevices': 'children',
        'DataDeliveryPeriodSec': 'delivery_period',
        'Description': 'description',
        'DeviceCheckInfos': 'check_infos',
        'DeviceFirstDate': 'first_packet_date',
        'DeviceIMEI': 'IMEI',
        'DeviceIMSI': 'IMSI',
        'DeviceLastDate': 'last_packet_date',
        'DeviceLastGeo': 'coordinates',
        'DeviceName': 'name',
        'FlagBatLow': 'is_bat_low', 'DeviceId': 'id',
        'IsOffline': 'is_offline',
        'SerialNumber': 'serial_number',
        'SoftwareVersion': 'software',
        'SourceType': 'type', 'TagIds': 'tags',
        'MoId': 'id',
        'Name': 'name',
        'PublishName': 'internal_name',
        'PublishNameRu': 'name_ru',
        'ManualDeviceLinks': 'devices_manual',
        'DeviceLink': 'devices_auto',
        'GmtOffset': 'gmt_offset',
        'DotItem': 'coordinates',
        'Latitude': 'latitude',
        'Longitude': 'longitude',
        'LocationId': 'location',
        'GeoInfo': 'coordinates',
        'Geo': 'coordinates', 'DataAqi': 'AQI',
        'GmtHour': 'gmt_hour_diff',
        'PublishOnMap': 'is_public',
        'NameRu': 'name_ru',
        'PacketId': 'packet_id',
        "Humidity_": "RH"
        }
USELESS_COLS = ['220', 'BatLow', 'receive_date', 'GeoInfo', 'Geo', 'Date',
                'SendDate', 'ResetMoData', 'description', 'coordinates',
                'rssi', 'FlagBatLowHasFailed', 'FlagPs220HasFailed',
                'IsNotSaveData', 'ParentDeviceId', 'SourceType', 'tags',
                'DataProviderId', 'IsDeleted', 'IsManualParamLinks',
                'IsStartInterval1H', 'ManualPacketParamLinks', 'Timestamp',
                'is_bat_low', 'BounceNorth', 'BounceSouth', 'BounceEast',
                'BounceWest', 'CountryId', 'BounceNorth', 'BounceSouth',
                'BounceEast', 'BounceWest', 'GmtHour', 'LocationUrl',
                'DistributionSummary', 'SortRank', 'PacketId', 'packet_id',
                "DoS", "DDW", 'Pcf', 'Scf']

MAIN_DEVICE_PARAMS = ['serial_number', 'name', 'software', 'stations',
                      'children', 'check_infos']
MAIN_STATION_PARAMS = ['id', 'name', 'name_ru', 'location', 'gmt_offset',
                       'devices', 'latitude', 'longitude']

def add_progress_bar(method):
    """
    Decorator to display progress bar

    """
    progress_scaler = 10 ** 6
    default_take_count = 500

    @wraps(method)
    def progressed(*args, **kwargs):
        start_date = to_date(kwargs.get('start_date'))
        if not start_date:
            return method(*args, **kwargs)
        finish_date = to_date(
                kwargs.get('finish_date', datetime.datetime.utcnow()))
        kwargs.update(take_count=kwargs.get('take_count', default_take_count))
        bar = progressbar.ProgressBar(max_value=(finish_date - start_date)
                                      .total_seconds() / progress_scaler,
                                      widgets=[
                                              f"{args[1]}",
                                              ': ',
                                              progressbar.Percentage(),
                                              '    ',
                                              progressbar.Timer(),
                                              progressbar.Bar(),
                                              progressbar.ETA()
                                              ],
                                      max_error=False
                                      )
        if kwargs.get('format', 'df') == 'df':
            res = pd.DataFrame()
        else:
            res = defaultdict(pd.DataFrame)
        while finish_date - start_date > datetime.timedelta(hours=1):
            try:
                data = method(*args, **kwargs)
                if isinstance(res, pd.DataFrame):
                    res = pd.concat([res, data], sort=False)
                    start_date = res.index[-1]
                else:
                    for serial, df in data.items():
                        res[serial] = pd.concat([res[serial], df], sort=False)
                    start_date = max([res[key].index[-1] for key in res])
            except EmptyDataException:
                start_date += datetime.timedelta(days=2)
            kwargs.update(
                    start_date=start_date + datetime.timedelta(seconds=30))
            fetched_seconds = (finish_date.replace(tzinfo=pytz.utc)
                               - start_date.replace(
                            tzinfo=pytz.utc)).total_seconds()
            bar.update(bar.max_value - (fetched_seconds / progress_scaler))
        size = len(res) if isinstance(res, pd.DataFrame) else max(
                map(len, res.values()))
        logger.info(f'finished acquiring {args[1]} data of size {size}')
        if size == 0:
            raise EmptyDataException(request_args=kwargs)
        return res

    return progressed

def unpack_cols(df, cols_to_unpack, right_params_names=RIGHT_PARAMS_NAMES):
    for col in cols_to_unpack:
        df = df.assign(**df[col].apply(pd.Series)).drop(col, 1)
    df.rename(right_params_names, axis=1, inplace=True)
    return df

def prep_dicts(dicts, newkeys, keys_to_drop, dropna=True):
    res = []
    for d in dicts:
        new_dict = {}
        for key, value in zip(d.keys(), d.values()):
            if key in keys_to_drop:
                continue
            if dropna and value is None:
                continue
            if key == 'is_offline':
                new_dict['is_online'] = not value
                continue
            new_key = newkeys.get(key, key)
            if 'date' in new_key:
                value = to_date(value)
            new_dict[new_key] = value
        res.append(new_dict)
    return res

def parse_checkinfo(msg: str) -> List[dict]:
    parsed_checkinfo = []
    parse_re = re.compile(CHECKINFO_PARSE_PATTERN)
    infos = parse_re.findall(msg.replace(", ", ","))
    if not infos:
        raise ValueError("checkinfo data should be in the format"
                         f" of \"{CHECKINFO_PARSE_PATTERN}\"")
    for info in infos:
        module, status, count, details = info.split(',')
        parsed_checkinfo.append(dict(
                module=module,
                has_error=False if status == 'ok' else True,
                errors_count=int(count),
                details=str(details).replace("\"", "")))
    return parsed_checkinfo

def to_date(date: Union[datetime.datetime, str, None],
            format: str = 'date') -> Optional[Union[str, datetime.datetime]]:
    """

    :param date: date (datetime or string) to be converted
    :param format: str, 'str' or 'date', default 'date'
        return type
    :return:
    """
    if not date:
        return None
    if isinstance(date, datetime.datetime):
        pass
    elif isinstance(date, str):
        date = pd.to_datetime(date, dayfirst=True)
    else:
        raise ValueError(f"date should be 'str' or 'datetime', "
                         f"got {type(date)} instead")

    if format == 'str':
        return date.isoformat()
    elif format == 'date':
        return date.replace(tzinfo=None)
    else:
        raise ValueError(f"format arg should be one of the 'str', 'date', "
                         f"got {format} instead")

def prep_df(df: pd.DataFrame, right_param_names: dict = RIGHT_PARAMS_NAMES,
            cols_to_drop: List[str] = USELESS_COLS,
            dicts_cols: List[str] = [], dropna: bool = True,
            index_col: str = None, cols_to_unpack: List[str] = []):
    res = df.copy()
    res.dropna(how='all', axis=0)
    res.rename(right_param_names, axis=1, inplace=True)

    for col in dicts_cols:
        res[col] = res[col].apply(prep_dicts,
                                  args=[right_param_names, cols_to_drop,
                                        dropna])
    try:
        res = unpack_cols(res, cols_to_unpack)
    except (KeyError, TypeError):
        pass
    if dropna:
        res.dropna(how='all', axis=1, inplace=True)
    for col in res:
        if 'date' in col.lower():
            res[col] = res[col].apply(to_date)
    try:
        res['is_online'] = ~ res['is_offline']
        res.drop('is_offline', axis=1, inplace=True)
    except KeyError:
        pass
    if index_col and index_col in res.columns:
        res.set_index(index_col, inplace=True)
    res.rename(right_param_names, axis=1, inplace=True)
    res = res.drop(cols_to_drop, axis=1, errors='ignore')
    res = res.sort_index()
    return res

def timeit(method):
    """
    Decorator to print time elapsed

    """

    @wraps(method)
    def timed(*args, **kwargs):
        ts = time.time()
        result = method(*args, **kwargs)
        te = time.time()

        logger.debug(
                f"{te - ts:.2f} seconds took to {method.__name__} of size"
                f"{sys.getsizeof(result) / 1000: .2f} KB\nargs were"
                f" {', '.join(map(str, args[1:]))}\nkwargs were: {kwargs}")
        return result

    return timed


import sys
import csv
from py2neo import Graph, Node, Relationship, NodeMatcher

def loadEmployees(g, file):
   with open(file, 'r') as f:
       rows = list(csv.reader(f))
   for row in rows:
       r = ",".join(row).split(":")
       n = Node("Employee", first=r[0], mid=r[1], last=r[2], ssn=r[3], dob=r[4], address=r[5], sex=r[6],
         salary=int(r[7]), superV=r[8], deptId=int(r[9]))
       g.create(n)

def loadDepatments(g, file):
   with open(file,'r') as f:
       rows = list(csv.reader(f))
   for row in rows:
       r = row[0].split(":")
       print(r)
       n = Node('Department',dept=r[0], deptId=r[1], ssn=r[2], date=r[3])
       g.create(n)

def loadDepatments2(g, file):
   with open(file,'r') as f:
       rows = list(csv.reader(f))
   matcher = NodeMatcher(g)
   for row in rows:
       r = row[0].split(":")
       d = matcher.match("Department", ssn=r[2]).first()
       e = matcher.match("Employee", ssn=r[2]).first()
       #print(d)
       #n = Node('Departments',dept=r[0], deptId=r[1], date=r[3])
       rel1 = Relationship(d,"managed_by",e)
       rel2 = Relationship(e,"manages",d)
       g.create(rel1)
       g.create(rel2)

def loadEmployees2(g, file):
    with open(file, 'r') as f:
        rows = list(csv.reader(f))
    matcher = NodeMatcher(g)
    for row in rows:
        r = ",".join(row).split(":")
        if r[8] == "null":
            d = matcher.match("Department", deptId=r[9]).first()
            e = matcher.match("Employee", ssn=r[3]).first()
            rel1 = Relationship(d,"employs",e)
            rel2 = Relationship(e,"works_for",d)
            g.create(rel1)
            g.create(rel2)
        elif r[8] != "null":
            d = matcher.match("Department", deptId=r[9]).first()
            boss = matcher.match("Employee", ssn=r[8]).first()
            e = matcher.match("Employee", ssn=r[3]).first()
            rel1 = Relationship(d,"employs",e)
            rel2 = Relationship(e,"works_for",d)
            rel3 = Relationship(e,"supervisee",boss)
            rel4 = Relationship(boss,"supervisor",e)
            g.create(rel1)
            g.create(rel2)
            g.create(rel3)
            g.create(rel4)

def loadDependents(g, file):
    with open(file,'r') as f:
       rows = list(csv.reader(f))
    matcher = NodeMatcher(g)
    for row in rows:
       r = row[0].split(":")
       print(r)
       d = Node('Dependent',ssn=r[0], name=r[1], sex=r[2], birthdate=r[3], relationship=r[4])
       e = matcher.match("Employee", ssn=r[0]).first()
       rel1 = Relationship(d,"dependents_of",e)
       rel2 = Relationship(e,"dependent",d)
       g.create(rel1)
       g.create(rel2)

def loadProject(g, file):
   with open(file, 'r') as f:
       rows = list(csv.reader(f))
   matcher = NodeMatcher(g)
   for row in rows:
       r = row[0].split(":")
       print(r)
       p = Node("Project", name=r[0], pnumber=r[1], location=r[2], controllingDept=r[3])
       d = matcher.match("Department", deptId=r[3]).first()
       e = matcher.match("Employee", deptId=r[3]).first()
       rel1 = Relationship(d,"controls",p)
       rel2 = Relationship(p,"controlled_by",d)
       g.create(rel1)
       g.create(rel2)

def loadDeptLocations(g, file):
   with open(file,'r') as f:
       rows = list(csv.reader(f))
   matcher = NodeMatcher(g)
   for row in rows:
       r = row[0].split(":")
       rlen = len(r)
       d = matcher.match('Department',deptId=r[0]).first()
       n = Node('Department_location', deptId=r[0], address=','.join(r[1:rlen]).split(','))
       rel = Relationship(d,"is_located",n)
       rel1 = Relationship(n,"located_at",d)
       g.create(rel)
       g.create(rel1)

def loadWorksOn(g, file):
    with open(file, 'r') as f:
        rows = list(csv.reader(f))
    matcher = NodeMatcher(g)
    for row in rows:
        r = row[0].split(":")
        w = Node("WorksOn", ssn=r[0], pnumber=r[1], hours=r[2])
        p = matcher.match("Project", pnumber=r[1]).first()
        e = matcher.match("Employee",ssn=r[0]).first()
        rel1 = Relationship(e,"empWorkHours",w)
        rel2 = Relationship(p,"projectWorkHours",w)
        g.create(rel1)
        g.create(rel2)

def loadWorksOn2(g, file):
    with open(file, 'r') as f:
        rows = list(csv.reader(f))
    matcher = NodeMatcher(g)
    for row in rows:
        r = row[0].split(":")
        e = matcher.match('Employee',ssn=r[0]).first()
        p = matcher.match("Project", pnumber=r[1]).first()
        rel1 = Relationship(p,"worker",e)
        rel2 = Relationship(e,"works_on",p)
        g.create(rel1)
        g.create(rel2)

def main():
    g = Graph(auth=('neo4j','Adventure@25'))
    g.delete_all()
    loadEmployees(g,"./"+sys.argv[1]+"/EMPLOYEES.dat")
    loadDepatments(g,"./"+sys.argv[1]+"/DEPARTMENTS.dat")
    loadDepatments2(g,"./"+sys.argv[1]+"/DEPARTMENTS.dat")
    loadEmployees2(g,"./"+sys.argv[1]+"/EMPLOYEES.dat")
    loadDependents(g,"./"+sys.argv[1]+"/DEPENDENTS.dat")
    loadDeptLocations(g,"./"+sys.argv[1]+"/DEPT_LOCATIONS.dat")
    loadProject(g,"./"+sys.argv[1]+"/PROJECTS.dat")
    loadWorksOn(g,"./"+sys.argv[1]+"/WORKS_ON.dat")
    loadWorksOn2(g,"./"+sys.argv[1]+"/WORKS_ON.dat")

main()


from torch import nn
from .metrics import PixelNorm, WeightedSum, initialize_layer

class ToRGB(nn.Module):
    def __init__(self, in_channels, flag_tanh):
        super(ToRGB, self).__init__()
        self.main = nn.Conv2d(in_channels, 3, kernel_size=1, stride=1)
        initialize_layer(self.main)
        self.tanh = nn.Tanh() if flag_tanh else None

    def forward(self, x):
        x = self.main(x)
        return x if self.tanh is None else self.tanh(x)

class G_block(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(G_block, self).__init__()
        layers = [
            nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
            PixelNorm(),
            nn.LeakyReLU(0.2),
            nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
            PixelNorm(),
            nn.LeakyReLU(0.2),
        ]
        initialize_layer(layers[0])
        initialize_layer(layers[3])
        self.main = nn.Sequential(*layers)

    def forward(self, x):
        return self.main(x)

class Generator(nn.Module):
    def __init__(self,
        latent_size=512,
        channel_dict={
            4:512, 8:512, 16:512, 32:512, 64:256,
            128:128, 256:64, 512:32, 1024:16
        },
        device='cuda',
        flag_tanh=True
    ):
        super(Generator, self).__init__()
        self.device = device
        self.flag_tanh = flag_tanh
        self.latent_size = latent_size
        self.channel_dict = channel_dict
        self.output_resolution = 4
        self.has_fadein = False
        self.weighted_alpha = 0

        layers = [
            nn.ConvTranspose2d(latent_size, channel_dict[4], kernel_size=4),
            nn.LeakyReLU(0.2),
            nn.Conv2d(channel_dict[4], channel_dict[4], kernel_size=3, stride=1, padding=1),
            PixelNorm(),
            nn.LeakyReLU(0.2),
        ]
        initialize_layer(layers[0])
        initialize_layer(layers[2])

        self.main = nn.Sequential(*layers).to(self.device)
        self.to_rgb = ToRGB(in_channels=channel_dict[4], flag_tanh=self.flag_tanh).to(self.device)
        self.to_rgb_fadein = None
        self.weighted_sum = WeightedSum().to(self.device)
        self.upsample = None
        self.last_block = None

    def forward(self, z):
        if len(z.shape) != 4:
            z = z.view(-1, self.latent_size, 1, 1)
        x = self.main(z)
        if self.has_fadein:
            x = self.upsample(x)
            x1 = self.to_rgb_fadein(x)
            x2 = self.last_block(x)
            x2 = self.to_rgb(x2)
            x = self.weighted_sum(x1, x2, self.weighted_alpha)
        else:
            x = self.to_rgb(x)
        return x

    def add_to_main(self, layer):
        # self.main.add_module(str(len(self.main)), layer)
        self.main = nn.Sequential(*self.main, layer)

    def remove_fadein(self):
        if not self.has_fadein:
            return
        self.has_fadein = False
        self.add_to_main(self.upsample)
        self.add_to_main(self.last_block)
        self.upsample = None
        self.last_block = None
        self.to_rgb_fadein = None

    def add_block(self, fadein=False):
        if self.has_fadein:
            self.remove_fadein()
        in_channels = self.channel_dict[self.output_resolution]
        out_channels = self.channel_dict[self.output_resolution * 2]
        if fadein:
            self.has_fadein = True
            self.upsample = nn.Upsample(scale_factor=2).to(self.device)
            self.last_block = G_block(in_channels, out_channels).to(self.device)
            self.to_rgb_fadein = self.to_rgb
            self.weighted_alpha = 0
        else:
            self.add_to_main(nn.Upsample(scale_factor=2).to(self.device))
            self.add_to_main(G_block(in_channels, out_channels).to(self.device))

        self.to_rgb = ToRGB(out_channels, flag_tanh=self.flag_tanh).to(self.device)
        self.output_resolution *= 2

    def set_weighted_alpha(self, alpha):
        self.weighted_alpha = alpha


import os
from datetime import datetime
from werkzeug.utils import secure_filename
from config import *

# 이미지 기본 설정
def allowed_file(file):
	if (
		file.filename.rsplit(".", 1)[1].lower() not in ALLOWED_EXTENSIONS
		or "." not in file.filename
	):
		return False

	return True

def manufacture_img(input_img, folderurl):
	# 사진 이름 테이블에 삽입 및 저장
	if input_img and allowed_file(input_img):  #  이미지 확장자 확인
		suffix = datetime.now().strftime("%y%m%d_%H%M%S")
		filename = "_".join(
			[input_img.filename.rsplit(".", 1)[0], suffix]
		)  # 중복된 이름의 사진을 받기위해서 파일명에 시간을 붙임
		extension = input_img.filename.rsplit(".", 1)[1]
		filename = secure_filename(f"{filename}.{extension}")
		input_img.save(os.path.join(folderurl, filename))
		return filename

def delete_img(target):
	if os.path.isfile(target):
		os.remove(target)


h, w = map(int, input().split()) # h: 세로길이/ w: 가로길이

blocks = list(map(int, input().split()))

max_h = max(blocks) # 제일 높은 기둥
max_h_index = blocks.index(max_h) # 제일 높은 기둥의 인덱스

answer = 0
start = blocks[0]

temp = 0
total = 0
for i in range(max_h_index + 1):
    if blocks[i] > temp:
        temp = blocks[i]

    total += temp

temp = 0
for i in range(w - 1, max_h_index, -1):
    if blocks[i] > temp:
        temp = blocks[i]

    total += temp

answer = total - sum(blocks)

print(answer)


from collections import deque
import copy

def solution(tickets):
    answer = []
    dic = {}
    for ticket in tickets:
        if ticket[0] not in dic.keys():
            dic[ticket[0]] = [ticket[1]]
        else:
            dic[ticket[0]].append(ticket[1])
    for key in dic.keys():
        dic[key].sort()
    root = ['ICN']
    que = deque([['ICN', root, dic]])
    while que:
        start, new_root, new_dic = que.popleft()
        if len(new_root) == len(tickets) + 1:
            answer = new_root
            break
        # 출발 경로가 없는 도착지로만 존재 할 경우 dic에서 key를 찾을 수 없기 때문에 key가 dic에 있다는 가정을 넣어주어야 한다
        if start in new_dic and new_dic[start]:
            for end in new_dic[start]:
                now_dic = copy.deepcopy(new_dic)
                now_dic[start].remove(end)
                now_root = copy.deepcopy(new_root)
                now_root.append(end)
                que.append([end, now_root, now_dic])

    return answer

print(solution([["ICN", "JFK"], ["HND", "IAD"], ["JFK", "HND"]]))
print(solution([["ICN", "SFO"], ["ICN", "ATL"], ["SFO", "ATL"], ["ATL", "ICN"], ["ATL", "SFO"]]))
print(solution([["ICN", "AOO"], ["AOO", "BOO"], ["BOO", "COO"], ["COO", "DOO"], ["DOO", "EOO"], ["EOO", "DOO"], ["DOO", "COO"], ["COO", "BOO"], ["BOO", "COO"]]))


import pytest
import os
import glob

from camoco.Config import cf
from camoco import Tools as tools

import camoco as co
import pandas as pd

''' -------------------------------------------------------------------------
            'Test' Fixtures
'''

@pytest.fixture(scope='module')
def testRefGen(Zm5bFGS):
    # This was a mistake
    return Zm5bFGS

@pytest.fixture(scope='module')
def testGWAS(ZmWallace):
    return ZmWallace

@pytest.fixture(scope='module')
def testCOB(ZmRNASeqTissueAtlas):
    return ZmRNASeqTissueAtlas

''' -------------------------------------------------------------------------
            RefGen Fixtures
'''

@pytest.fixture(scope="module")
def Zm5bFGS():
    if cf.test.force.RefGen:
        tools.del_dataset('RefGen', 'Zm5bFGS', force=True)
    if not tools.available_datasets('RefGen', 'Zm5bFGS'):
        # We have to build it
        gff = os.path.expanduser(
            os.path.join(
                cf.options.testdir,
                'raw', 'RefGen', 'ZmB73_5b_FGS.gff.gz'
            )
        )
        # This is stupid and necessary because pytables wont let me open
        # more than one table
        co.RefGen.from_gff(
            gff, 'Zm5bFGS', 'Maize 5b Filtered Gene Set'
        )
    return co.RefGen('Zm5bFGS')

@pytest.fixture(scope="module")
def Zm5bFGS_duplicate(Zm5bFGS):
    # Build a refgen over an already built refgen
    # We have to build it
    gff = os.path.expanduser(
        os.path.join(
            cf.options.testdir,
            'raw', 'RefGen', 'ZmB73_5b_FGS.gff.gz'
        )
    )
    # This is stupid and necessary because pytables wont let me open
    # more than one table
    co.RefGen.from_gff(
        gff, 'Zm5bFGS', 'Maize 5b Filtered Gene Set'
    )
    return co.RefGen('Zm5bFGS')

''' -------------------------------------------------------------------------
            COB Fixtures
'''

@pytest.fixture(scope="module")
def ZmRNASeqTissueAtlas(Zm5bFGS):
    if cf.test.force.COB:
        print('Rebuilding ZmRNASeqTissueAtlas')
        tools.del_dataset('COB', 'ZmRNASeqTissueAtlas', force=True)
        tools.del_dataset('Expr', 'ZmRNASeqTissueAtlas', force=True)
    if not tools.available_datasets('Expr', 'ZmRNASeqTissueAtlas'):
        # Build it
        return co.COB.from_table(
            os.path.join(cf.options.testdir,
                'raw', 'Expr', 'RNASEQ',
                'MaizeRNASeqTissue.tsv.bz2',
            ),
            'ZmRNASeqTissueAtlas',
            'Maize RNASeq Tissue Atlas Network, Sekhon 2013, PLoS ONE',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.3,
            max_accession_missing_data=0.08,
            min_single_sample_expr=1,
            min_expr=0.001,
            quantile=False,
            max_val=300,
            dry_run=False
        )
    else:
        return co.COB('ZmRNASeqTissueAtlas')

@pytest.fixture(scope="module")
def ZmRoot(Zm5bFGS):
    if cf.test.force.COB:
        tools.del_dataset('Expr','ZmRoot',force=True)
    if not tools.available_datasets('Expr','ZmRoot'):
        return co.COB.from_table(
            os.path.join(
                cf.options.testdir,
                'raw','Expr',
                'RNASEQ','ROOTFPKM.tsv.gz'
            ),
            'ZmRoot',
            'Maize Root Network',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.3,
            max_accession_missing_data=0.08,
            min_single_sample_expr=1,
            min_expr=0.001,
            quantile=False,
            max_val=300
        )
    else:
        return co.COB('ZmRoot')

@pytest.fixture(scope="module")
def ZmSAM(Zm5bFGS):
    if cf.test.force.COB:
        tools.del_dataset('Expr','ZmSAM',force=True)
    if not tools.available_datasets('Expr','ZmSAM'):
        return co.COB.from_table(
            os.path.join(
                cf.options.testdir,
                'raw','Expr','RNASEQ',
                'TranscriptomeProfiling_B73_Atlas_SAM_FGS_LiLin_20140316.txt.gz'
            ),
            'ZmSAM',
            'Maize Root Network',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.4,
            min_expr=0.1,
            quantile=False,
            dry_run=False,
            max_val=250
        )
    else:
        return co.COB('ZmSAM')

@pytest.fixture(scope="module")
def ZmSAM2(Zm5bFGS):
    if cf.test.force.COB:
        tools.del_dataset('Expr','ZmSAM2',force=True)
    if not tools.available_datasets('Expr','ZmSAM2'):
        return co.COB.from_table(
            os.path.join(
                cf.options.testdir,
                'raw','Expr','RNASEQ',
                'TranscriptomeProfiling_B73_Atlas_SAM_FGS_LiLin_20140316.txt.gz'
            ),
            'ZmSAM2',
            'Maize Root Network, but loose',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.4,
            min_single_sample_expr=1,
            min_expr=0.01,
            quantile=False,
            dry_run=False,
            max_val=250
        )
    else:
        return co.COB('ZmSAM2')

@pytest.fixture(scope="module")
def ZmPAN2(Zm5bFGS):
    if cf.test.force.COB:
        tools.del_dataset('Expr','ZmPAN2',force=True)
    if not tools.available_datasets('Expr','ZmPAN2'):
        return co.COB.from_table(
            os.path.join(
                cf.options.testdir,
                'raw','Expr','RNASEQ',
                'PANGenomeFPKM.txt.gz'
            ),
            'ZmPAN2',
            'Maize Root Network but extra loose',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.4,
            min_single_sample_expr=1,
            min_expr=0.01,
            quantile=False,
            dry_run=False,
            sep=',',
            max_val=300
        )
    else:
        return co.COB('ZmPAN2')

@pytest.fixture(scope="module")
def ZmPAN(Zm5bFGS):
    if cf.test.force.COB:
        tools.del_dataset('Expr','ZmPAN',force=True)
    if not tools.available_datasets('Expr','ZmPAN'):
        return co.COB.from_table(
            os.path.join(
                cf.options.testdir,
                'raw','Expr','RNASEQ',
                'PANGenomeFPKM.txt.gz'
            ),
            'ZmPAN',
            'Maize Root Network',
            Zm5bFGS,
            rawtype='RNASEQ',
            max_gene_missing_data=0.4,
            min_expr=1,
            quantile=False,
            dry_run=False,
            sep=',',
            max_val=300
        )
    else:
        return co.COB('ZmPAN')

''' -------------------------------------------------------------------------
            GWAS Fixtures
'''
@pytest.fixture(scope='module')
def testGWAS(testRefGen):
    if cf.test.force.Ontology:
        tools.del_dataset('GWAS','testGWAS',force=True)
    df = pd.DataFrame({
        'Trait' : ['a','a','b','b'],
        'CHR' : ['chr1','chr2','chr3','chr4'],
        'POS' : [100,200,300,400],
        'Start' : [100,200,300,400],
        'End' : [1000,20000,3000,4000],
        'id' : ['snp1','snp2','snp3','snp4'],
        'pval' : [0.05,0.05,0.01,0.01]
    })
    gwas = co.GWAS.from_DataFrame(
        df,
        'testGWAS',
        'Test GWAS Dataset',
        testRefGen,
        chr_col='CHR',
        pos_col='POS',
        id_col='id',
        term_col='Trait'
    )
    return gwas

@pytest.fixture(scope='module')
def ZmWallace(Zm5bFGS):
    if cf.test.force.Ontology:
        tools.del_dataset('GWAS','ZmWallace',force=True)
    if not tools.available_datasets('GWAS','ZmWallace'):
        # Grab path the csv
        csv = os.path.join(
            cf.options.testdir,
            'raw','GWAS','WallacePLoSGenet',
            'Wallace_etal_2014_PLoSGenet_GWAS_hits-150112.txt.gz'
        )
        # Define our reference geneome
        df = pd.DataFrame.from_csv(csv,index_col=None,sep='\t')
        # Import class from dataframe
        gwas  = co.GWAS.from_DataFrame(
            df, 'ZmWallace', 'Wallace PLoS ONE Dataset.',
            Zm5bFGS,
            term_col='trait', chr_col='chr', pos_col='pos'
        )
        return gwas
    else:
        return co.GWAS('ZmWallace')

@pytest.fixture(scope="module")
def ZmIonome(Zm5bFGS):
        # Delete the old dataset
    if cf.test.force.Ontology:
        tools.del_dataset('GWAS','ZmIonome',force=True)
    if not tools.available_datasets('GWAS','ZmIonome'):
        # Grab path the csv
        csv = os.path.join(
            cf.options.testdir,
            'raw','GWAS','Ionome',
            'sigGWASsnpsCombinedIterations.longhorn.allLoc.csv.gz'
        )
        # Define our reference geneome
        df = pd.DataFrame.from_csv(csv,index_col=None)
        # Import class from dataframe
        IONS  = co.GWAS.from_DataFrame(
            df,'ZmIonome','Maize Ionome',
            Zm5bFGS,
            term_col='el',chr_col='chr',pos_col='pos'
        )
        # Get rid of pesky Cobalt
        IONS.del_term('Co59')
        # I guess we need a test in here too
        return IONS
    else:
        return co.GWAS('ZmIonome')

'''----------------------------------------------------------------------------
    GOnt Fixtures
----------------------------------------------------------------------------'''

@pytest.fixture(scope='module')
def testGO(Zm5bFGS):
    if cf.test.force.Ontology:
        tools.del_dataset('GOnt','TestGO',force=True)
    if not tools.available_datasets('GOnt','TestGO'):
        obo = os.path.join(
            cf.options.testdir,
            'raw','GOnt','go.test.obo'
        )
        gene_map_file = os.path.join(
            cf.options.testdir,
            'raw','GOnt','go.test.tsv'
        )
        return co.GOnt.from_obo(
           obo, gene_map_file, 'TestGO',
           'Test GO', Zm5bFGS
        )
    else:
        return co.GOnt('testGO')

@pytest.fixture(scope="module")
def testZmGO(Zm5bFGS):
    if cf.test.force.Ontology:
        tools.del_dataset('GOnt','ZmGO',force=True)
    if not tools.available_datasets('GOnt','ZmGO'):
        obo = os.path.join(
            cf.options.testdir,
            'raw','GOnt','go.obo.gz'
        )
        gene_map_file = os.path.join(
            cf.options.testdir,
            'raw','GOnt','zm_go.tsv.gz'
        )
        return co.GOnt.from_obo(
           obo, gene_map_file, 'ZmGO',
           'Maize Gene Ontology', Zm5bFGS
        )
    else:
        return co.GOnt('ZmGO')


import json
import requests
import geopandas as gpd
import numpy as np
import shapely.speedups
import pandas as pa
import matplotlib.pyplot as plt

# my_gdf.to_sql(name="test", con=engine, if_exists='append', index=False,
#               dtype={'geom': Geometry(geometry_type='POLYGON', srid=4326)})
def create_heirarchy(geo_data_frame, layers):
    filter = geo_data_frame["IndexOrder"] == layers[0]
    child_filter = geo_data_frame["IndexOrder"] == layers[1]
    parent_order = geo_data_frame[filter]
    child_order = geo_data_frame[child_filter]
    count = 0
    print("total parent ", parent_order.size)
    for row in parent_order.iterrows():
        count = count + 1
        print(count)
        ids = get_all_intersected_polygons(row[1].geometry, child_order)
        geo_data_frame.loc[geo_data_frame.unique_id.isin(ids), "parent_id"] = int(row[1].unique_id)
    geo_data_frame.to_file('/home/gulve/data-analysis/feature-comparator/data/complte_irl.gpkg', layer='index',
                           driver='GPKG')
    return geo_data_frame

def call_hierarchy(geo_data_frame, lowest_order):
    filter = geo_data_frame["IndexOrder"] == lowest_order
    geo_data_frame.unique_id.astype(str)
    # result_data_frame = gpd.GeoDataFrame(columns=["order10", "order9", "order8"])
    apts = gpd.read_file(
        "C:\\Amol_Parande\\8_MIS\Abhijeet\\contains_Python\\2_Geopakage\\Geo_Data.gpkg", layer="Points")
    for row in geo_data_frame[filter].iterrows():
        list = [row[1].unique_id]
        construct_hierarchy(geo_data_frame, row[1].parent_id, list)
        query = get_name_hierarchy_from_ids(geo_data_frame, list)
        # res = compare_pelias_hierarchy(geo_data_frame, query, list, {})
        print(query, ",", compare_with_apt(geo_data_frame, list, apts), percentage_match(None))

def percentage_match(ll):
    if ll == None:
        return 0
    count = 0
    for name in ll:
        if ll[name]:
            count = count + 1
    return count / len(ll)

api = "https://api.geocode.earth/v1/autocomplete?focus.point.lat=18.545351289248973&focus.point.lon=73.88522386550905&api_key=ge-8f137223ed5b405d&text="

def compare_pelias_hierarchy(geo_data_frame, query, hierarchy_ids, response_model_attribute):
    #     name  neighbourhood  locality
    resp = requests.get(api + query.strip())
    if resp.status_code != 200:
        raise Exception('GET /tasks/ {}'.format(resp.status_code))
    res_json = resp.json()
    if len(res_json["features"]) <= 0:
        return
    name_matching = res_json["features"][0]["properties"]
    # if "name" in name_matching and "neighbourhood" in name_matching and "locality" in name_matching:
    return json_value_math(name_matching, hierarchy_ids, geo_data_frame)

def json_value_math(json_obj, hierarchy_ids, geo_data_frame):
    json_obj = json.loads(json.dumps(json_obj))
    arr = {}
    for id in hierarchy_ids:
        name = get_area_name(geo_data_frame, id)
        if "County" not in name:
            temp = False
            for value in json_obj.values():
                temp |= name == value
            arr[name] = temp
        else:
            arr[name] = True
    return arr

def create_convex_hull_and_compare(apts_within_area, area):
    name_poly = check_for_same_name_poly_area(apts_within_area, area)
    if name_poly > 50:
        return name_poly
    group = apts_within_area.groupby("LOCALITY_NAME")
    for single_group in group:
        poly = single_group[1]["geometry"].unary_union.convex_hull
        intersection = poly.intersection(area[1].geometry)
        area[1].within(poly[1].geometry)

def check_for_same_name_poly_area(apts_within_area, area):
    fltr = apts_within_area[apts_within_area["LOCALITY_NAME"] == area[1].name]
    poly_with_area_name = fltr[1]["geometry"].unary_union.convex_hull
    intersection = poly_with_area_name.intersection(area[1].geometry)
    return (intersection.area / area.area) * 100

def compare_with_apt(geo_data_frame, hierarchical_ids, apts):
    global apt_group_by
    data_frame = geo_data_frame[geo_data_frame.unique_id.isin(hierarchical_ids[:2])]
    # data_frame = data_frame[not data_frame["name"].str.contains("County"
    for area in data_frame.iterrows():
        area_arr = []
        for touple in area.iteritems():
            area_arr.append(touple)
        gpd.GeoDataFrame(area_arr)
        apts_within_area = apts[apts.within(area[1].geometry)]
        group_by = apts_within_area.groupby("LOCALITY_NAME").aggregate("LOCALITY_NAME").count()
        arr = []
        for touple in group_by.iteritems():
            arr.append(touple)
        apt_group_by = pa.DataFrame(arr, columns=["LOCALITY_NAME", "count"])
        apt_group_by = remove_parent(apt_group_by, area, geo_data_frame)
        apt_group_by["perc"] = apt_group_by['count'] / (apt_group_by.sum()[1] / 100)
        apt_group_by = apt_group_by.sort_values(by="perc", ascending=False)
        aa = apt_group_by[apt_group_by["LOCALITY_NAME"] == area[1]["name"].upper()]
        if len(aa.perc.array) <= 0:
            return False
    return True

def filter_county_column_name(data_frame):
    for key in data_frame.keys():
        if "county" in key.lower():
            return key

def remove_parent(apt_group_by, area, geo_data_frame):
    list = []
    construct_hierarchy(geo_data_frame, area[1].parent_id, list)
    for id in list:
        row = geo_data_frame[geo_data_frame["unique_id"] == id]["name"].values[0].upper()
        apt_group_by = apt_group_by.drop(apt_group_by[apt_group_by["LOCALITY_NAME"] == row].index)
    return apt_group_by

def get_all_name_matching_poly(data_frame, poly):
    poly_list = poly[poly.LOCALITY_NAME.isin([x.upper() for x in data_frame.name.values])]
    return poly_list

def get_intersecting_polygons(poly_list, lowest_geo_data_frame):
    lowest_poly = poly_list[poly_list["LOCALITY_NAME"] == lowest_geo_data_frame.name.values[0].upper()]
    pip_mask = lowest_poly[lowest_poly.within(poly_list['geometry'])]
    for poly in poly_list.iterrows():
        poly[1].geometry

def get_area_name(geo_data_frame, id):
    return geo_data_frame.loc[geo_data_frame.unique_id == id].name.values[0]

def get_name_hierarchy_from_ids(geo_data_frame, list):
    string = ""
    for id in list:
        string += (geo_data_frame.loc[geo_data_frame.unique_id == id].name.values[0] + " ")
    return string

def construct_hierarchy(geo_data_frame, parent_id, list):
    if parent_id == '':
        return list
    list.append(np.int64(parent_id))
    construct_hierarchy(geo_data_frame,
                        geo_data_frame[geo_data_frame["unique_id"] == np.int64(parent_id)].parent_id.values[0],
                        list)

def get_all_intersected_polygons(parent_poly, child_data_frame):
    return child_data_frame[child_data_frame.within(parent_poly)].unique_id.array

def create_df_from_tuple(group_by, columns):
    arr = []
    index = []
    for touple in group_by.iteritems():
        arr.append(touple)
        index.append(touple)
    return pa.DataFrame(arr, columns=columns, index=index)

# df = gpd.read_file("/home/gulve/data-analysis/Geogesion/dublin.gpkg", driver="GPKG",
#                    layer="TTOM-Core::IndexArea")
df = gpd.read_file("/home/gulve/data-analysis/feature-comparator/data/complte_irl.gpkg",
                   driver="GPKG",
                   layer="index")
# df = df[df["IndexOrder"].notnull()]
# df['unique_id'] = df.name.map(hash)
# df['parent_id'] = ''
# create_heirarchy(df, ["Order9", "Order10"])

# df.loc[df["IndexOrder"] == "Order8", "parent_id"] = ''
call_hierarchy(df, "Order10")

def absolute_value(val):
    a = np.round(val, 0)
    return a
# csv = pa.read_csv("/home/gulve/data-analysis/hierarchy_res.csv")
# csv = csv.astype({"apt": str})
# both_matches_hierarchy = csv[csv["apt"] == csv["pelias"]]
#
# need_to_check = csv[csv["pelias"] == "None"]
# need_to_check = need_to_check[need_to_check["apt"] == "False"]
# print()
# not_matches = csv[csv["pelias"] != "None"]
# not_matches = not_matches[not_matches["apt"] != not_matches["pelias"]]
# print(need_to_check)
#
# pelias_distinct_values = create_df_from_tuple(csv.pelias.value_counts(), ["is_matches", "count"])
# pelias_distinct_values.plot.pie(y='count', autopct=absolute_value)
# apt = create_df_from_tuple(csv.apt.value_counts(), ["is_matches", "count"])
# apt = apt.astype({'count': np.int64})
# apt.plot.pie(y="count", x="apt", autopct=absolute_value)
# plt.show()
# df = gpd.read_file("/home/gulve/data-analysis/Geogesion/tt.gpkg", driver="GPKG",
#                    layer="TTOM-Core::IndexArea")
# create_convex_hull(df)


from typing import final
from abc import ABC, abstractmethod
from logging import getLogger

from app.web.application import Application

class CleanupCTX(ABC):
    def __init__(self, app: Application):
        self.app = app
        self.logger = getLogger(self.__class__.__name__)
        app.cleanup_ctx.append(self._cleanup)

    @abstractmethod
    async def on_startup(self):
        ...

    @abstractmethod
    async def on_shutdown(self):
        ...

    @final
    async def _cleanup(self, _: Application):
        await self._on_startup()
        yield
        await self._on_shutdown()

    @final
    async def _on_startup(self):
        self.logger.info(f"starting up {self.__class__.__name__}...")
        await self.on_startup()

    @final
    async def _on_shutdown(self):
        self.logger.info(f"shutting down {self.__class__.__name__}...")
        await self.on_shutdown()


import time
import click

import requests
from sqlalchemy.exc import IntegrityError

from db import sql_engine, Session
from db.models import EtfCategory, IsinCategory, Etf
from db.table_manager import create_table
from scraping.items import EtfItem, string_to_date

class Extraetf():
    """
    This class extracts ETF data from extraetf.com via an undocumented API. As such an API is always subject to change
    we must expect this implementation to work only for a limited amount of time.
    """

    def __init__(self):
        # the category names on extraetf.com
        self.category_types = {'sector_name': 'Sektor', 'land_name': 'Land', 'region_name': 'Region',
                               'asset_class_name': "Asset Klasse", 'strategy_name': 'Strategie',
                               'fund_currency_id': 'Währung',
                               'bond_type_name': 'Anlageart', 'commodity_class_name': 'Rohstoffklasse',
                               'bond_maturity_name': 'Laufzeit',
                               'bond_rating_name': 'Rating'}
        create_table(sql_engine)
        self.session = Session()
        self.cgry_cache = dict()

    def collect_data(self):
        offset = 0
        limit = 200

        while True:
            params = {'offset': offset, 'limit': limit, 'ordering': '-assets_under_management', 'leverage_from': 1,
                      'leverage_to': 1}
            response = requests.get('https://de.extraetf.com/api-v2/search/full/', params=params)
            time.sleep(2)

            data = response.json()
            page = int(offset / limit + 1)
            results = data['results']
            click.echo(f"Extracted etfs from page {page}!")
            self.__parse_page(results)

            if data['next'] is None:
                break

            offset += limit

        self.session.close()

    def __parse_page(self, results):
        """
        Parses and stores a page from extraetf.com and extracts the data from each detail page displayed on that page.
        """

        for result in results:
            detail_params = {'isin': result['isin']}
            detail_response = requests.get('https://de.extraetf.com/api-v2/detail/', detail_params)
            detail_result = detail_response.json()['results'][0]

            item = self.__parse_item(result, detail_result)
            item = self.__process_item(item)
            self.__save_item(item)

            self.save_item_categories(result, detail_result)

            time.sleep(0.25)  # slow down to avoid getting banned :)

    def __parse_item(self, result, detail_result):
        """
        Parses the data of one detail page, i.e. of one ETF
        """
        item = EtfItem()
        for field in item.fields:
            item.setdefault(field, None)

        item['name'] = result['fondname']
        item['isin'] = result['isin']
        item['wkn'] = result['wkn']
        item['ter'] = result['ter']
        click.echo(f"Parsing ETF '{result['isin']}'")

        replication = detail_result['replication_methodology_first_level']
        repl_detail = detail_result['replication_methodology_second_level']
        if repl_detail is not None:
            replication = repl_detail if replication is None else '(' + repl_detail + ')'

        item['replication'] = replication
        item['distribution_policy'] = result['distribution_policy']
        item['fund_size'] = result['assets_under_management']
        item['inception'] = result['launch_date']
        item['fund_currency'] = result['fund_currency_id']
        item['fund_domicile'] = result['fund_domicile']

        item['fund_provider'] = detail_result['provider_name']
        item['legal_structure'] = result['legal_structure']
        item['fund_structure'] = detail_result['fund_structure']
        item['ucits_compliance'] = detail_result['ucits_konform']
        item['administrator'] = detail_result['administrator']
        item['revision_company'] = detail_result['auditor_company']
        item['custodian_bank'] = detail_result['depotbank']
        item['tax_germany'] = detail_result['tax_status_de']
        item['tax_switzerland'] = detail_result['tax_status_ch']
        item['tax_austria'] = detail_result['tax_status_au']
        item['tax_uk'] = detail_result['tax_status_uk']
        item['distribution_frequency'] = detail_result['distribution_interval']

        item['volatility_one_year'] = detail_result['volatility_1_year']
        item['fiscal_year_end_month'] = detail_result['fiscal_year_end_month']
        item['indextype'] = detail_result['index_type']
        item['swap_counterparty'] = detail_result['swap_counterparty']
        item['collateral_manager'] = detail_result['collateral_manager']
        item['securities_lending'] = detail_result['has_securities_lending']
        item['securities_lending_counterparty'] = detail_result['securities_lending_party']

        item['net_assets_currency'] = detail_result['net_assets_currency']
        item['is_accumulating'] = detail_result['is_accumulating']
        item['is_derivative_based'] = detail_result['is_derivative_based']
        item['is_distributing'] = detail_result['is_distributing']
        item['is_etc'] = detail_result['is_etc']
        item['is_etf'] = detail_result['is_etf']
        item['is_hedged'] = detail_result['is_hedged']
        item['hedged_currency'] = detail_result['is_hedged_currency']
        item['is_index_fund'] = detail_result['is_index_fund']
        item['is_leveraged'] = detail_result['is_leveraged']
        item['is_physical_full'] = detail_result['is_physical_full']
        item['is_short'] = detail_result['is_short']
        item['is_socially_responsible_fund'] = detail_result['is_socially_responsible_fund']
        item['is_structured'] = detail_result['is_structured']
        item['is_swap_based_etf'] = detail_result['is_swap_based_etf']
        item['is_synthetic_replication'] = detail_result['is_synthetic_replication']

        return item

    def __process_item(self, item):
        """
        Converts the data of an item into appropriate formats if neccesary
        """
        item['inception'] = string_to_date(item['inception'])

        return item

    def __save_item(self, item: EtfItem):
        etf = item.to_etfitemdb()
        try:
            current_data: Etf = self.session.query(Etf).filter_by(isin=etf.isin).first()
            if current_data is not None:
                click.echo(f"Updating existing ETF {current_data.isin}")
                self.__update_item(current_data, etf)

            else:
                self.session.add(etf)
                self.session.commit()
        except:
            click.echo(f"Could not save data for {etf.isin}!")
            self.session.rollback()
            raise

    def __update_item(self, current_data, etf):
        """
        Overwrites certain ETF data from justetf if a particular ETF is included in both databases
        """

        # we only update attributes that do not exist at justetf.com or are of higher precision at extraetf.com
        # higher precision
        current_data.fund_size = etf.fund_size
        current_data.ter = etf.ter
        # new
        current_data.tax_germany = etf.tax_germany
        current_data.net_assets_currency = etf.net_assets_currency
        current_data.is_accumulating = etf.is_accumulating
        current_data.is_derivative_based = etf.is_derivative_based
        current_data.is_distributing = etf.is_distributing
        current_data.is_etc = etf.is_etc
        current_data.is_etf = etf.is_etf
        current_data.is_hedged = etf.is_hedged
        current_data.hedged_currency = etf.hedged_currency
        current_data.is_index_fund = etf.is_index_fund
        current_data.is_leveraged = etf.is_leveraged
        current_data.is_physical_full = etf.is_physical_full
        current_data.is_short = etf.is_short
        current_data.is_socially_responsible_fund = etf.is_socially_responsible_fund
        current_data.is_structured = etf.is_structured
        current_data.is_swap_based_etf = etf.is_swap_based_etf
        current_data.is_synthetic_replication = etf.is_synthetic_replication
        self.session.commit()

    def save_item_categories(self, result, detail_result):
        """
        Saves the item category for each item
        """
        isin = result['isin']
        categories = [(detail_result[cat_key], cat_type) for cat_key, cat_type in self.category_types.items()]

        try:
            for cat_name, cat_type in categories:
                self.__save_item_category(cat_name, cat_type, isin)

        except:
            click.echo(f"Could not save data for {result['isin']}!")
            self.session.rollback()
            raise

    def __save_item_category(self, cat_name, cat_type, isin):
        """
        Saves the category if it is a previously unknown category and associates a particular ISIN with that category in the database.
        """

        if cat_name is not None and cat_name:
            if cat_name in self.cgry_cache:
                # if category has already been added to db we can use the cache to retrieve its id
                cat_id = self.cgry_cache[cat_name]
            else:
                cat_id = self.__add_category(cat_name, cat_type)
            if cat_id:
                ic = IsinCategory()
                ic.etf_isin = isin
                ic.category_id = cat_id

                try:
                    self.session.add(ic)
                    self.session.commit()
                except IntegrityError:
                    click.echo(f"Could not insert category data for ETF with ISIN {isin} and category {cat_name} "
                                    f"as it already exists!")
                    self.session.rollback()

            else:
                click.echo(f"No ID was found for item with category {cat_name}")

    def __add_category(self, cat_name, cat_type):
        """
        Stores a new category in database
        """
        etf_category = EtfCategory()
        etf_category.name = cat_name
        etf_category.type = cat_type
        self.session.add(etf_category)
        self.session.commit()
        self.session.refresh(etf_category)  # refresh to get auto-incremented cat_id
        cat_id = etf_category.id
        self.cgry_cache[cat_name] = cat_id
        return cat_id


from collections import deque

N, M = list(map(int, input().split()))

restrict = deque([])
test = deque([])

for i in range(N):
    length, speed = list(map(int, input().split()))
    restrict.append((length, speed))

for i in range(M):
    length, speed = list(map(int, input().split()))
    test.append((length, speed))

max_speed = 0
test_length = 0
restrict_length = 0

while (test or restrict):
    if test_length > restrict_length:
        r_length, r_speed = restrict.popleft()
        restrict_length += r_length
        max_speed = max(max_speed, t_speed - r_speed)

    elif test_length < restrict_length:
        t_length, t_speed = test.popleft()
        test_length += t_length
        max_speed = max(max_speed, t_speed - r_speed)

    else:
        r_length, r_speed = restrict.popleft()
        t_length, t_speed = test.popleft()
        test_length += t_length
        restrict_length += r_length
        max_speed = max(max_speed, t_speed - r_speed)

print(max_speed)


import json
import logging
import os.path
import re
import shutil
import ssl
import subprocess
import time
import urllib2

import settings

_BOARD = open('/etc/board').read().strip()
_REPO = ('ccrisan', 'motionPie')
_DOWNLOAD_URL = 'https://github.com/{owner}/{repo}/releases/download/%(version)s/motionpie-%(board)s-%(version)s.img.gz'.format(
        owner=_REPO[0], repo=_REPO[1])
_LIST_VERSIONS_URL = 'https://api.github.com/repos/{owner}/{repo}/releases'.format(
        owner=_REPO[0], repo=_REPO[1])
_DOWNLOAD_DIR = '/data/.firmware_update'
_DOWNLOAD_FILE_NAME = os.path.join(_DOWNLOAD_DIR, 'firmware.gz')

# versions

def get_version():
    import motioneye

    return motioneye.VERSION

def get_all_versions():
    url = _LIST_VERSIONS_URL
    url += '?_=' + str(int(time.time())) # prevents caching

    try:
        logging.debug('board is %s' % _BOARD)
        logging.debug('fetching %s...' % url)

        context = ssl._create_unverified_context()

        response = urllib2.urlopen(url, timeout=settings.REMOTE_REQUEST_TIMEOUT, context=context)
        releases = json.load(response)

        versions = []
        for release in releases:
            if release.get('prerelease') and not os.path.exists('/data/etc/prereleases'):
                continue

            for asset in release.get('assets', []):
                if not re.match('^motionpie-%s-\d{8}\.img.gz$' % _BOARD, asset['name']):
                    continue

            versions.append(release['name'])

        logging.debug('available versions: %(versions)s' % {'versions': ', '.join(versions)})

        return sorted(versions)

    except Exception as e:
        logging.error('could not get versions: %s' % e, exc_info=True)

    return []

def compare_versions(version1, version2):
    version1 = [int(n) for n in version1.split('.')]
    version2 = [int(n) for n in version2.split('.')]

    len1 = len(version1)
    len2 = len(version2)
    length = min(len1, len2)
    for i in xrange(length):
        p1 = version1[i]
        p2 = version2[i]

        if p1 < p2:
            return -1

        elif p1 > p2:
            return 1

    if len1 < len2:
        return -1

    elif len1 > len2:
        return 1

    else:
        return 0

# updating

def download(version):
    url = _DOWNLOAD_URL % {'version': version, 'board': _BOARD}

    try:
        logging.info('downloading %s...' % url)

        shutil.rmtree(_DOWNLOAD_DIR, ignore_errors=True)
        os.makedirs(_DOWNLOAD_DIR)
        subprocess.check_call(['/usr/bin/wget', url, '--no-check-certificate', '-O', _DOWNLOAD_FILE_NAME])

    except Exception as e:
        logging.error('could not download update: %s' % e)

        raise

    try:
        logging.info('decompressing %s...' % _DOWNLOAD_FILE_NAME)

        subprocess.check_call(['/bin/gunzip', _DOWNLOAD_FILE_NAME])

    except Exception as e:
        logging.error('could not decompress archive: %s' % e)

        raise

    extracted_file_name = _DOWNLOAD_FILE_NAME.replace('.gz', '')

    try:
        logging.info('reading partiton table...')

        output = subprocess.check_output(['/sbin/fdisk', '-l', extracted_file_name])
        lines = [l.strip().replace('*', ' ') for l in output.split('\n') if l.startswith(extracted_file_name)]
        boot_info = lines[0].split()
        root_info = lines[1].split()

        boot_start, boot_end = int(boot_info[1]), int(boot_info[2])
        root_start, root_end = int(root_info[1]), int(root_info[2])

    except Exception as e:
        logging.error('could not read partition table: %s' % e)

        raise

    try:
        logging.info('extracting boot.img...')

        subprocess.check_call(['/bin/dd', 'if=' + extracted_file_name, 'of=' + os.path.join(_DOWNLOAD_DIR, 'boot.img'),
                'bs=2048', 'skip=' + str(boot_start / 4), 'count=' + str((boot_end - boot_start + 1) / 4)])

    except Exception as e:
        logging.error('could not extract boot.img: %s' % e)

        raise

    try:
        logging.info('extracting root.img...')

        subprocess.check_call(['/bin/dd', 'if=' + extracted_file_name, 'of=' + os.path.join(_DOWNLOAD_DIR, 'root.img'),
                'bs=2048', 'skip=' + str(root_start / 4), 'count=' + str((root_end - root_start + 1) / 4)])

    except Exception as e:
        logging.error('could not extract root.img: %s' % e)

        raise

def perform_update(version):
    logging.info('updating to version %(version)s...' % {'version': version})

    logging.info('killing motioneye init script...')
    os.system('kill $(pidof S95motioneye)')

    logging.info('stopping netwatch init script...')
    os.system('/etc/init.d/S41netwatch stop')

    download(version)

    logging.info('backing up /boot/config.txt')
    if os.system('/bin/cp /boot/config.txt /tmp/config.txt'):
        logging.error('failed to backup /boot/config.txt')

        raise Exception('failed to backup /boot/config.txt')

    logging.info('unmounting boot partition...')
    if os.system('/bin/umount /boot'):
        logging.error('failed to unmount boot partition')

        raise Exception('failed to unmount boot partition')

    try:
        logging.info('installing boot image...')
        boot_img = os.path.join(_DOWNLOAD_DIR, 'boot.img')

        subprocess.check_call(['/bin/dd', 'if=' + boot_img, 'of=/dev/mmcblk0p1', 'bs=1M'])

    except Exception as e:
        logging.error('could not install boot image: %s' % e)

        raise

    logging.info('mounting boot partition read-write...')
    if os.system('/bin/mount -o rw /dev/mmcblk0p1 /boot'):
        logging.error('failed to mount boot partition')

        raise Exception('failed to mount boot partition')

    logging.info('restoring up /boot/config.txt')
    if os.system('/bin/cp /tmp/config.txt /boot/config.txt'):
        logging.error('failed to restore /boot/config.txt')

        raise Exception('failed to restore /boot/config.txt')

    logging.info('preparing to boot in fwupdate mode...')
    try:
        config_lines = [c.strip() for c in open('/boot/config.txt', 'r').readlines() if c.strip()]

    except Exception as e:
        logging.error('failed to read /boot/config.txt: %s' % e, exc_info=True)

        raise

    config_lines.append('initramfs fwupdater.gz')

    try:
        with open('/boot/config.txt', 'w') as f:
            for line in config_lines:
                f.write(line + '\n')

    except Exception as e:
        logging.error('failed to write /boot/config.txt: %s' % e, exc_info=True)

        raise

    logging.info('rebooting...')

    if os.system('/sbin/reboot'):
        logging.error('failed to reboot')
        logging.info('hard rebooting...')
        open('/proc/sysrq-trigger', 'w').write('b') # reboot


from HMO_server.connect_to_database import cursor

def fetch_patient_data():
    cursor.execute('SELECT p.patient_id, p.first_name, p.last_name, a.city, a.street, a.number, p.date_of_birth, '
                   'p.telephone, p.mobile, v1.vaccine_date, v1.manufacturer '
                   'FROM Patient p '
                   'JOIN Address a ON p.address_id = a.address_id '
                   'LEFT JOIN Vaccine v1 ON p.patient_id = v1.patient_id AND v1.vaccine_id = '
                   '    (SELECT MAX(v2.vaccine_id) FROM Vaccine v2 WHERE v2.patient_id = p.patient_id) '
                   'ORDER BY p.patient_id')
    rows = cursor.fetchall()
    return rows

def parse_patient_data(rows):
    patients = []
    current_patient_id = None
    current_patient = None

    for row in rows:
        patient_id, first_name, last_name, city, street, number, date_of_birth, telephone, mobile, vaccine_date,\
        manufacturer = row

        if patient_id != current_patient_id:
            current_patient_id = patient_id
            current_patient = {
                "patient_id": str(patient_id),
                "first_name": first_name,
                "last_name": last_name,
                "city": city,
                "street": street,
                "number": number,
                "date_of_birth": str(date_of_birth),
                "telephone": telephone,
                "mobile": mobile,
                "vaccine_dates": [],
                "manufacturers": [],
                "positive_date": None,
                "recovery_date": None
            }
            patients.append(current_patient)

        if vaccine_date is not None:
            current_patient["vaccine_dates"].append(str(vaccine_date))
            current_patient["manufacturers"].append(manufacturer)

    return patients


from io import StringIO
import sys

def sandbox(code: str, block_globals: bool=False,
            block_locals: bool=False) -> tuple:
    """Runs the code-string and captures any errors

    Args:
     code: executable string
     block_globals: if True don't use global namespace
     block_ locals: if True don't use local namespace
    Returns:
     output, stderr, and any exception code
    """
    old_stdout = sys.stdout
    old_stderr = sys.stderr
    redirected_output = sys.stdout = StringIO()
    redirected_error = sys.stderr = StringIO()

    namespace_globals = {} if block_globals else globals()
    namespace_locals = {} if block_locals else locals()
    output, error, exception = None, None, None

    try:
        exec(code, namespace_globals, namespace_locals)
    except:
        import traceback
        exception = traceback.format_exc()

    output = redirected_output.getvalue()
    error = redirected_error.getvalue()

    # reset outputs to the original values
    sys.stdout = old_stdout
    sys.stderr = old_stderr
    return output, error, exception


import sys
import os
import re
from Bio import SeqIO

in_file = "./Brassica.bac.final.0.03.rep.fasta"
out_file ="./Brassica.bac.final.0.03.rep.rename.fasta"

output_handle = open(out_file, "w")

OldseqID = list()
seqInfo = list()
NewseqID = list()

for seq_record in SeqIO.parse(in_file, "fasta"):
    OldseqID.append(seq_record.id)
    seqInfo.append(seq_record.description)
    seqID_New = seq_record.description
    seqID_New = re.findall('Otu.{6}',seq_record.description)
    NewseqID.append(seqID_New)
    seq_record.id = seqID_New[0]

    SeqIO.write(seq_record, output_handle, "fasta")

output_handle.close()
renamed = len(NewseqID)

print("done")
print("renamed %i sequence records" %(renamed))