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

from __future__ import print_function

# from scipy import spatial
import numpy as np
from scipy import spatial
mode = 'IVFPQ'

class Evaluation(object):
    def make_samples(self):
        raise NotImplementedError("Needs to implemented this method")

def distance(v1, v2, d_type='d1'):
    assert v1.shape == v2.shape, "shape of two vectors need to be same!"
    # L1范数 汉明距离
    if d_type == 'd1':
        return np.sum(np.absolute(v1 - v2))
    # L2范数 欧氏距离
    elif d_type == 'd2':
        return np.sum((v1 - v2) ** 2)
    # 正则化的欧氏距离
    elif d_type == 'd2-norm':
        return 2 - 2 * np.dot(v1, v2)
    elif d_type == 'd3':
        pass
    elif d_type == 'd4':
        pass
    elif d_type == 'd5':
        pass
    elif d_type == 'd6':
        pass
    elif d_type == 'd7':
        return 2 - 2 * np.dot(v1, v2)
    elif d_type == 'd8':
        return 2 - 2 * np.dot(v1, v2)
    elif d_type == 'cosine':
        return spatial.distance.cosine(v1, v2)
    elif d_type == 'square':
        return np.sum((v1 - v2) ** 2)

def AP(label, results, dicbase):
    ''' infer a query, return it's ap

    arguments
      label  : query's class
      results: a list of ID, e.g.:[1,2,11,4]
    '''
    precision = []
    hit = 0  # 命中的个数
    for i, result in enumerate(results):
        if dicbase.iloc[result,0] == label:
            hit += 1
            precision.append(hit / (i + 1.))
    if hit == 0:
        return 0.
    return np.mean(precision)

# 测试检索性能
def infer(query,
          mode,
          samples=None,
          lsh=None,
          sample_db_fn=None,
          depth=None,
          d_type='d1'):
    ''' infer a query, return it's ap

    arguments
      query       : a dict with three keys, see the template
                    {
                      'img': <path_to_img>,
                      'cls': <img class>,
                      'hist' <img histogram>
                    }
      samples     : a list of {
                                'img': <path_to_img>,
                                'cls': <img class>,
                                'hist' <img histogram>
                              }
      sample_db_fn: a function making samples, should be given if Database != None
      depth       : retrieved depth during inference, the default depth is equal to database size
      d_type      : distance type
  '''
    # TODO 选择检索模式
    q_img, q_cls, q_hist = query['img'], query['cls'], query['hist']
    if mode == 'LSH':
        results = lsh.query(query_point=q_hist, img_addr=q_img, num_results=depth, distance_func="cosine")
    else:
        results = []
        dis_l = []
        for idx, sample in enumerate(samples):
            s_img, s_cls, s_hist = sample['img'], sample['cls'], sample['hist']
            if q_img == s_img:  # 相同的图片不算
                continue
            if idx < depth:
                results.append({
                    'img': s_img,
                    'dis': distance(q_hist, s_hist, d_type=d_type),
                    'cls': s_cls
                })
                dis_l.append(results[-1]['dis'])
                max_dis = max(dis_l)
                max_index = dis_l.index(max_dis)
            else:
                dis = distance(q_hist, s_hist, d_type=d_type)
                if dis < max_dis:
                    results[max_index] = {
                        'img': s_img,
                        'dis': dis,
                        'cls': s_cls
                    }
                    dis_l[max_index] = dis
                    max_dis = max(dis_l)
                    max_index = dis_l.index(max_dis)

        results = sorted(results, key=lambda x: x['dis'])
        if depth and depth < len(results):
            results = results[:depth]  # 返回top-k检索结果
    #####
    ap = AP(q_cls, results, sort=False)

    return ap, results

def evaluate(db, sample_db_fn, depth=None, d_type='d1'):
    ''' infer the whole database

    arguments
      db          : an instance of class Database
      sample_db_fn: a function making samples, should be given if Database != None
      depth       : retrieved depth during inference, the default depth is equal to database size
      d_type      : distance type
  '''
    classes = db.get_class()
    print(classes)
    ret = {c: [] for c in classes}

    samples = sample_db_fn(db)  # 提前生成好samples

    for query in samples:
        ap, _ = infer(query, samples=samples, depth=depth, d_type=d_type)
        ret[query['cls']].append(ap)

    return ret

def evaluate_class(db, f_class=None, f_instance=None, depth=None):
    ''' infer the whole database

    arguments
      db     : an instance of class Database
      f_class: a class that generate features, needs to implement make_samples method
      depth  : retrieved depth during inference, the default depth is equal to database size
      d_type : distance type
  '''
    assert f_class or f_instance, "needs to give class_name or an instance of class"

    classes = db.get_class()
    ret = {c: [] for c in classes}

    if f_class:
        f = f_class()
    elif f_instance:
        f = f_instance
    index, dicbase, vecbase = f.make_samples(db, mode)  # 调用f的make_samples的方法
    query = vecbase
    query_label = dicbase.iloc[:,0]  # 标签
    result_D, result_I = index.search(query, depth + 1)     # actual search
    # 去除自身
    result_D = result_D[:,1:]
    result_I = result_I[:,1:]
    for results, label in zip(result_I, query_label):
        ap = AP(label,results,dicbase)
        ret[label].append(ap)  # 记录该类别的ap
    return ret


from easygui import *
from mc_dictionary_v1 import monster_cards

while True:
    fields = ['Cardname', 'Strength', 'Speed', 'Stealth', 'Cunning']
    card_details = multenterbox('Enter the card details here (do not exceed 25 characters):', fields=fields)

    cardname = card_details[0]
    strength = card_details[1]
    speed = card_details[2]
    stealth = card_details[3]
    cunning = card_details[4]

    print(strength, speed, stealth, cunning)

    if not strength.isdigit() or not speed.isdigit() or not stealth.isdigit() or not cunning.isdigit():
        msgbox('Please enter an integer in abilities.')

    if strength or speed or stealth or cunning >= 25:
        msgbox('Please enter a number below 25')
    else:
        break

    cardname, strength, speed, stealth, cunning = card_details

monster_cards[cardname] = {
    'strength': int(strength),
    'speed': int(speed),
    'stealth': int(stealth),
    'cunning': int(cunning)
}

print(monster_cards)


from django.contrib import admin
from django.urls import path

from books import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('<int:pk>/', views.author_detail, name='author-detail'),
    path('htmx/book/<int:pk>/detail/', views.detail_book, name='detail-book'),
    path('htmx/book/create/', views.create_book_form, name='create-book'),
    path('htmx/book/<int:pk>/delete/', views.delete_book, name='delete-book'),
    path('htmx/book/<int:pk>/update/', views.update_book_form, name='update-book'),
]


import nltk
import sklearn
import sklearn_crfsuite
from sklearn_crfsuite import metrics
import os
nltk.download('averaged_perceptron_tagger')
def read_iob2_sents(in_iob2_file):
    sents = []
    with open(in_iob2_file, 'r', encoding = 'utf8') as f:
        for sent_iob2 in f.read().split('\n\n'):
            sent = []
            for raw in sent_iob2.split('\n'):
                if raw == '':
                    continue
                columns = raw.split('\t')
                sent.append(tuple(columns))
            sents.append(sent)
    return sents
train_sents = read_iob2_sents('train.iob2')
train_sents[0]

def sent2labels(sent): return [label for token, pos,label in sent]
def word2features(sent, i):
    word = sent[i][0]
    postag = sent[i][1]
    features = {
        'bias' : 1.0,
        'word': word,
        'word_len':len(word),
        'word[0:3]':word[0:3],
        'word[-3:]': word[-3:],
        'word[-2:]': word[-2:],
        'word.islower()': word.islower(),
        'word.isalnum()': word.isalnum(),
        'word.isupper()': word.isupper(),
        'word.isdigit()': word.isdigit(),
        'word.isalpha()': word.isalpha(),
        'word.istitle()': word.istitle(),
        'word.containspace()':' ' in word==True,
        'word.containdash()':'-' in word==True,
        'postag': postag,
        'postag[:2]': postag[:2],}
    if i > 0:
        word1 = sent[i-1][0]
        postag1= sent[i-1][1]
        features.update({
            '-1:word': word1,
            '-1:word_len':len(word1),
            '-1:word[0:3]':word1[0:3],
            '-1:word[-3:]': word1[-3:],
            '-1:word[-2:]': word1[-2:],
            '-1:word.islower()': word1.islower(),
            '-1:word.isalnum()': word1.isalnum(),
            '-1:word.isupper()': word1.isupper(),
            '-1:word.isdigit()': word1.isdigit(),
            '-1:word.isalpha()': word1.isalpha(),
            '-1:word.istitle()': word1.istitle(),
            '-1:word.containspace()':' ' in word==True,
            '-1:word.containdash()':'-' in word==True,
            '-1:postag': postag1,
            '-1:postag[:2]': postag1[:2]})
    else:
        features['BOS'] = True
    if i < len(sent)-1:
        word1 = sent[i+1][0]
        postag1 = sent[i+1][1]
        features.update({
            '+1:word': word1,
            '+1:word_len':len(word1),
            '+1:word[0:3]':word1[0:3],
            '+1:word[-3:]': word1[-3:],
            '+1:word[-2:]': word1[-2:],
            '+1:word.islower()': word1.islower(),
            '+1:word.isalnum()': word1.isalnum(),
            '+1:word.isupper()': word1.isupper(),
            '+1:word.isdigit()': word1.isdigit(),
            '+1:word.isalpha()': word1.isalpha(),
            '+1:word.istitle()': word1.istitle(),
            '+1:word.containspace()':' ' in word==True,
            '+1:word.containdash()':'-' in word==True,
            '+1:postag': postag1,
            '+1:postag[:2]': postag1[:2]})
    else:
        features['EOS'] = True
    return features

def sent2features(sent):
    return [word2features(sent, i) for i in range(len(sent))]
def appendpostagged(sent):
    data=[]
    for i, s in enumerate(sent):
        # Obtain the list of tokens in the document
        tokens = [t for t, label in s]
        # Perform POS tagging
        for token in tokens :
            if(token==''):
                tokens.remove('')
        tagged = nltk.pos_tag(tokens)
        print(tagged)
        # Take the word, POS tag, and its label
        data.append([(w, pos, label) for (w, label), (word, pos) in zip(s, tagged)])
    return data

def write_iob2(data, pred, out_iob2_file):
    with open(out_iob2_file, 'wb') as iob2_writer:
        for _data, _pred in zip(data, pred):
            for _tuple, _pred in zip(_data, _pred):
                iob2_writer.write(bytes('\t'.join(_tuple) + '\t' + _pred + '\n', encoding = 'utf8'))
            iob2_writer.write(bytes('\n', encoding = 'utf8'))

#main
print("go")
train_sents = read_iob2_sents('train.iob2')
train_sents = appendpostagged(train_sents)
#print(train_sents)
test_sents = read_iob2_sents('test.iob2')
test_sents = appendpostagged(test_sents)

X_train = [sent2features(s) for s in train_sents]
y_train = [sent2labels(s) for s in train_sents]

X_test = [sent2features(s) for s in test_sents]
y_test = [sent2labels(s) for s in test_sents]
print("openfile success")

crf = sklearn_crfsuite.CRF(
    algorithm='lbfgs',
    all_possible_states=True,
    all_possible_transitions=True,
    c1 =0.1,
    c2=0.1,
    linesearch='StrongBacktracking',
    max_iterations=8000,
    min_freq=0
)
print("start train")
crf.fit(X_train, y_train)
print("end and start pred")
y_pred = crf.predict(X_test)
print(y_pred)

labels = list(crf.classes_)
labels.remove('O')
sorted_labels = sorted(
    labels,
    key=lambda name: (name[1:], name[0])
)
print(metrics.flat_classification_report(y_test, y_pred, labels=sorted_labels, digits=3))
write_iob2(test_sents, y_pred, 'answer.iob2')


from itertools import product

def solution(word):
    words=[]
    for i in range(1,6):
        for c in product(['A','E','I','O','U'],repeat=i):
            words.append(''.join(list(c)))
    words.sort()
    return words.index(word)+1

# 중복 순열 함수 product : 서로다른 n개, 중복을 허락하고 r개를 일렬로 나열


"""This is the main module to run the app"""

# Importing the necessary Python modules.
import streamlit as st

# Import necessary functions from web_functions
from web_functions import load_data

# Import pages
from Tabs import home, data, predict, visualise

# Configure the app
st.set_page_config(
    page_title = 'Stress Level Detector',
    page_icon = 'heavy_exclamation_mark',
    layout = 'wide',
    initial_sidebar_state = 'auto'
)

# Dictionary for pages
Tabs = {
    "Home": home,
    "Data Info": data,
    "Prediction": predict,
    "Visualisation": visualise

}

# Create a sidebar
# Add title to sidear
st.sidebar.title("Navigation")

# Create radio option to select the page
page = st.sidebar.radio("Pages", list(Tabs.keys()))

# Loading the dataset.
df, X, y = load_data()

# Call the app funciton of selected page to run
if page in ["Prediction", "Visualisation"]:
    Tabs[page].app(df, X, y)
elif (page == "Data Info"):
    Tabs[page].app(df)
else:
    Tabs[page].app()


__author__ = "Andre Gomes"
__copyright__ = "Copyright (c) 2013-2015, Mobile Cloud Networking (MCN) project"
__credits__ = ["Andre Gomes"]
__license__ = "Apache"
__version__ = "2.0"
__maintainer__ = "Andre Gomes"
__email__ = "gomes@inf.unibe.ch"
__status__ = "Production"

"""
Service Orchestrator for ICNaaS.
Version 2.0
"""

import os
import random
import requests
import threading
import time

import icnaas.monitor
import icnaas.template_generator

from sdk.mcn import util
from sm.so import service_orchestrator
from sm.so.service_orchestrator import LOG
from sm.so.service_orchestrator import BUNDLE_DIR

DEFAULT_REGION = 'UBern'
FMC_ENABLED = False
NUM_LAYERS = 2
NUM_ROUTERS_LAYER = 1

# DO NOT CHANGE
SCALE_NO_ACTION = 0
SCALE_IN_CPU = 1
SCALE_OUT_CPU = 2
SCALE_IN_INTERESTS = 3
SCALE_OUT_INTERESTS = 4

# Safeguards to avoid scaling because of a peak, scale only if load remains for X minutes
CPU_SCALE_IN_SAFEGUARD = 10
CPU_SCALE_OUT_SAFEGUARD = 10
INTERESTS_SCALE_IN_SAFEGUARD = 5
INTERESTS_SCALE_OUT_SAFEGUARD = 5

DEFAULT_METRICS = [icnaas.monitor.CCN_ROUTER_CPU, icnaas.monitor.CCN_NUMBER_OF_INTERESTS]
DEFAULT_THRESHOLDS = { icnaas.monitor.CCN_ROUTER_CPU: { 'scale_out': 75, 'scale_in': 0 }, \
            icnaas.monitor.CCN_NUMBER_OF_INTERESTS: { 'scale_out': 1500, 'scale_in': 30 } }

class SOE(service_orchestrator.Execution):
    """
    SO execution part.
    """
    def __init__(self, token, tenant, ready_event):
        super(SOE, self).__init__(token, tenant)
        self.token = token
        self.tenant = tenant
        self.event = ready_event
        self.updated = False
        self.endpoint = None
        self.maas_endpoint = None
        self.mobaas_endpoint = None
        # Default topology
        self.layers = {}
        self.routers = {}
        self.stack_id = None
        self.deployer = util.get_deployer(self.token,
                                          url_type='public',
                                          tenant_name=self.tenant,
                                          region=DEFAULT_REGION)

    def design(self):
        """
        Do initial design steps here.
        """
        LOG.debug('Executing design logic')
        self.resolver.design()
        # Create topology
        routers_count = 0
        init_cell_id = 200
        for layer in range(NUM_LAYERS):
            self.layers[layer] = { 'cpu_scale_in_count': 0, 'cpu_scale_out_count': 0, \
                'int_scale_in_count': 0, 'int_scale_out_count': 0 }
            for i in range(1,NUM_ROUTERS_LAYER + 1):
                routers_count += 1
                cell_id = 0
                if layer == 0:
                    cell_id = init_cell_id
                    init_cell_id += 1
                self.routers[routers_count] = { 'public_ip': 'unassigned', 'layer': layer, \
                            'cell_id': cell_id, 'provisioned': False }

    def deploy(self, attributes):
        """
        deploy SICs.
        """
        LOG.debug('Deploy service dependencies')
        self.resolver.deploy()
        LOG.debug('Executing deployment logic')
        # Get template
        generator = icnaas.template_generator.ICNaaSTemplateGenerator(self.routers)
        template = generator.generate(fmc = FMC_ENABLED)
        # Deploy template
        if self.stack_id is None:
            self.stack_id = self.deployer.deploy(template, self.token, \
                name='icnaas_' + str(random.randint(1000, 9999)))

    def provision(self, attributes=None):
        """
        (Optional) if not done during deployment - provision.
        """
        self.resolver.provision()
        LOG.debug('ICN SO provision - Getting EPs')
        for ep_entity in self.resolver.service_inst_endpoints:
            for item in ep_entity:
                if 'mcn.endpoint.mobaas' in item['attributes']:
                    self.mobaas_endpoint = item['attributes']['mcn.endpoint.mobaas']

        # EP is only the IP
        if self.mobaas_endpoint is not None and self.mobaas_endpoint.startswith('http'):
            self.mobaas_endpoint = self.mobaas_endpoint.split('/')[2].split(':')[0]

        LOG.info('Now I can provision my resources once my resources are created. Service info:')
        LOG.info(self.resolver.service_inst_endpoints)

        # Wait for create/update to be completed
        while (True):
            if self.stack_id is not None:
                tmp = self.deployer.details(self.stack_id, self.token)
                if tmp['state'] == 'CREATE_COMPLETE' or tmp['state'] == 'UPDATE_COMPLETE':
                    break
                else:
                    time.sleep(10)

        LOG.debug('Executing resource provisioning logic')
        # XXX note that provisioning of external services must happen before resource provisioning
        # Get endpoint of MaaS
        if attributes:
            #print attributes
            if 'mcn.endpoint.maas' in attributes:
                self.maas_endpoint = str(attributes['mcn.endpoint.maas'])
            if 'mcn.endpoint.mobaas' in attributes:
                self.mobaas_endpoint = str(attributes['mcn.endpoint.mobaas'])

        # Update stack
        self.update(True)

        # Mark all routers as provisioned
        for r in self.routers:
            self.routers[r]['provisioned'] = True

        # once logic executes, deploy phase is done
        self.event.set()

    def dispose(self):
        """
        Dispose SICs.
        """
        LOG.info('Disposing of 3rd party service instances...')
        self.resolver.dispose()

        if self.stack_id is not None:
            LOG.info('Disposing of resource instances...')
            self.deployer.dispose(self.stack_id, self.token)
            self.endpoint = None
            self.maas_endpoint = None
            self.routers = { 1: { 'public_ip': 'unassigned', 'layer': 0, 'cell_id': 200, \
            'provisioned': False, 'scale_in_count': 0, 'scale_out_count': 0 }, \
            2: { 'public_ip': 'unassigned', 'layer': 1, 'cell_id': 0, \
            'provisioned': False, 'scale_in_count': 0, 'scale_out_count': 0 } }
            self.stack_id = None

    def state(self):
        """
        Report on state.
        """

        # TODO ideally here you compose what attributes should be returned to the SM
        # In this case only the state attributes are returned.
        resolver_state = self.resolver.state()
        LOG.info('Resolver state:')
        LOG.info(resolver_state.__repr__())

        if self.stack_id is not None:
            tmp = self.deployer.details(self.stack_id, self.token)
            # Update routers dictionary and service endpoint
            if tmp.get('output', None) is not None:
                for i in tmp['output']:
                    # CCNx Router IP
                    if i['output_key'].startswith('mcn.ccnx'):
                        router_id = i['output_key'].split('.')[2][6:]
                        self.routers[int(router_id)]['public_ip'] = str(i['output_value'])
                    # ICNaaS Service Endpoint
                    elif i['output_key'] == 'mcn.endpoint.icnaas':
                        self.endpoint = 'http://' + str(i['output_value']) + ':5000'
                        i['output_value'] = self.endpoint
                return tmp['state'], self.stack_id, tmp['output']
            else:
                return tmp['state'], self.stack_id, None
        else:
            return 'Unknown', 'N/A'

    def update(self, provisioning = False, attributes = None):
        """
        deploy updated SICs.
        """
        LOG.debug('Executing update deployment logic')
        # Check if attributes are being updated
        if attributes:
            if 'mcn.endpoint.maas' in attributes:
                self.maas_endpoint = str(attributes['mcn.endpoint.maas'])
            if 'mcn.endpoint.mobaas' in attributes:
                self.mobaas_endpoint = str(attributes['mcn.endpoint.mobaas'])
        # Get new template
        generator = icnaas.template_generator.ICNaaSTemplateGenerator(self.routers, self.maas_endpoint, \
            self.mobaas_endpoint)
        template = generator.generate(provisioning, FMC_ENABLED)
        # Wait for any pending operation to complete
        while (True):
            if self.stack_id is not None:
                tmp = self.deployer.details(self.stack_id, self.token)
                if tmp['state'] == 'CREATE_COMPLETE' or tmp['state'] == 'UPDATE_COMPLETE':
                    break
                else:
                    time.sleep(10)
        # Deploy new template
        if self.stack_id is not None:
            self.deployer.update(self.stack_id, template, self.token)
        # Mark as updated for SOD
        self.updated = True

    def notify(self, entity, attributes, extras):
        super(SOE, self).notify(entity, attributes, extras)
        # TODO here you can add logic to handle a notification event sent by the CC
        # XXX this is optional

class SOD(service_orchestrator.Decision, threading.Thread):
    """
    Decision part of SO.
    """

    def __init__(self, so_e, token, ready_event):
        super(service_orchestrator.Decision, self).__init__()
        self.so_e = so_e
        self.token = token
        self.event = ready_event
        self.monitor = None
        self.rules_engine = RulesEngine()

    def run(self):
        """
        Decision part implementation goes here.
        """
        while True:
            # It is unlikely that logic executed will be of any use until the provisioning phase has completed
            LOG.debug('Waiting for deploy and provisioning to finish')
            self.event.wait()
            LOG.debug('Starting runtime logic...')
            # Decision logic
            # Until service instance is destroyed
            while self.so_e.stack_id is not None:
                # Check if update is complete
                while True:
                    tmp = self.so_e.deployer.details(self.so_e.stack_id, self.so_e.token)
                    if tmp['state'] == 'UPDATE_COMPLETE':
                        break
                    else:
                        time.sleep(10)
                # Set updated back to False
                self.so_e.updated = False
                # Update the information about CCNx routers
                self.so_e.state()
                # Then, attempt to connect to MaaS
                self.monitor = icnaas.monitor.ICNaaSMonitorCCNRouter(self.so_e.maas_endpoint)
                # Afterwards, keep checking the metrics until service is updated
                while not self.so_e.updated:
                    self.check_metrics()
                    for i in range(0, 6):
                        if self.so_e.updated:
                            break
                        time.sleep(10)
            self.event = ready_event

    def check_metrics(self):
        # If monitoring is not connected, ignore
        if self.monitor.connFailed:
            return
        # Check metrics for all active routers in each layer
        for layer in self.so_e.layers:
            layer_values = { 'routers_count': 0, 'sum_cpu': 0.0, 'sum_interests': 0 }
            for r in self.so_e.routers:
                if self.so_e.routers[r]['layer'] != layer:
                    continue
                if self.so_e.routers[r]['public_ip'] != 'unassigned':
                    values = self.monitor.get(self.so_e.routers[r]['public_ip'])
                if values is not None:
                    layer_values['routers_count'] += 1
                    layer_values['sum_cpu'] += float(values[icnaas.monitor.CCN_ROUTER_CPU])
                    layer_values['sum_interests'] += int(values[icnaas.monitor.CCN_NUMBER_OF_INTERESTS])
            if layer_values['routers_count'] > 0:
                layer_avg = { icnaas.monitor.CCN_ROUTER_CPU: (layer_values['sum_cpu'] / layer_values['routers_count']), \
                    icnaas.monitor.CCN_NUMBER_OF_INTERESTS: (layer_values['sum_interests'] / float(layer_values['routers_count'])) }
                actions = self.rules_engine.process(layer_avg)
                if not actions:
                    self.scale_actions(SCALE_NO_ACTION, layer)
                else:
                    for a in actions:
                        self.scale_actions(a, layer)

    def scale_actions(self, action, layer):
        if action == SCALE_NO_ACTION:
            self.so_e.layers[layer]['cpu_scale_in_count'] = 0
            self.so_e.layers[layer]['cpu_scale_out_count'] = 0
            self.so_e.layers[layer]['int_scale_in_count'] = 0
            self.so_e.layers[layer]['int_scale_out_count'] = 0
        elif action == SCALE_IN_CPU or action == SCALE_IN_INTERESTS:
            safeguard = CPU_SCALE_IN_SAFEGUARD
            count = self.so_e.layers[layer]['cpu_scale_in_count']
            if action == SCALE_IN_INTERESTS:
                safeguard = INTERESTS_SCALE_IN_SAFEGUARD
                count = self.so_e.layers[layer]['int_scale_in_count']
                self.so_e.layers[layer]['int_scale_out_count'] = 0
            else:
                self.so_e.layers[layer]['cpu_scale_out_count'] = 0
            if count >= (safeguard - 1):
                if action == SCALE_IN_INTERESTS:
                    self.so_e.layers[layer]['int_scale_in_count'] = 0
                else:
                    self.so_e.layers[layer]['cpu_scale_in_count'] = 0
                # Do not scale in if minimum amount of components are running
                minimum = 1
                #if layer == 1:
                #    minimum = 2
                if sum(1 for x in self.so_e.routers.values() if x['layer'] == layer) > minimum:
                    # SCALE IN
                    for k in reversed(sorted(self.so_e.routers.keys())):
                        if self.so_e.routers[k]['layer'] == layer:
                            # Remove from ICN Manager
                            out = requests.delete(self.so_e.endpoint + '/icnaas/api/v1.0/routers/' \
                                + self.so_e.routers[k]['public_ip'])
                            # Update service instance
                            del self.so_e.routers[k]
                            self.so_e.update()
                            self.so_e.provision()
                            self.so_e.state()
                            return
            else:
                if action == SCALE_IN_INTERESTS:
                    self.so_e.layers[layer]['int_scale_in_count'] += 1
                else:
                    self.so_e.layers[layer]['cpu_scale_in_count'] += 1
        elif action == SCALE_OUT_CPU or action == SCALE_OUT_INTERESTS:
            safeguard = CPU_SCALE_OUT_SAFEGUARD
            count = self.so_e.layers[layer]['cpu_scale_out_count']
            if action == SCALE_OUT_INTERESTS:
                safeguard = INTERESTS_SCALE_OUT_SAFEGUARD
                count = self.so_e.layers[layer]['int_scale_out_count']
                self.so_e.layers[layer]['int_scale_in_count'] = 0
            else:
                self.so_e.layers[layer]['cpu_scale_in_count'] = 0
            # SCALE OUT
            if count >= (safeguard - 1):
                if action == SCALE_OUT_INTERESTS:
                    self.so_e.layers[layer]['int_scale_out_count'] = 0
                else:
                    self.so_e.layers[layer]['cpu_scale_out_count'] = 0
                cell_id = 0
                if layer == 0:
                    for x in self.so_e.routers.values():
                        if x['cell_id'] > cell_id:
                            cell_id = x['cell_id']
                    cell_id += 1
                key = max(self.so_e.routers.keys()) + 1
                self.so_e.routers[key] = { 'public_ip': 'unassigned', 'layer': layer, \
                    'cell_id': cell_id, 'provisioned': False }
                self.so_e.update()
                self.so_e.provision()
                self.so_e.state()
                return
            else:
                if action == SCALE_OUT_INTERESTS:
                    self.so_e.layers[layer]['int_scale_out_count'] += 1
                else:
                    self.so_e.layers[layer]['cpu_scale_out_count'] += 1

class RulesEngine(object):
    """
    Rules Engine for Scaling Decisions
    """

    def __init__(self, metrics = None, thresholds = None):
        if metrics is None:
            self.metrics = DEFAULT_METRICS
        else:
            self.metrics = metrics
        if thresholds is None:
            self.thresholds = DEFAULT_THRESHOLDS
        else:
            self.thresholds = thresholds

    def process(self, values):
        actions = []
        for metric in self.metrics:
            if metric == icnaas.monitor.CCN_ROUTER_CPU:
                if (100 - float(values[icnaas.monitor.CCN_ROUTER_CPU])) >= \
                    self.thresholds[icnaas.monitor.CCN_ROUTER_CPU]['scale_out']:
                    actions.append(SCALE_OUT_CPU)
                elif (100 - float(values[icnaas.monitor.CCN_ROUTER_CPU])) <= \
                    self.thresholds[icnaas.monitor.CCN_ROUTER_CPU]['scale_in']:
                    actions.append(SCALE_IN_CPU)
            elif metric == icnaas.monitor.CCN_NUMBER_OF_INTERESTS:
                if int(values[icnaas.monitor.CCN_NUMBER_OF_INTERESTS]) >= \
                    self.thresholds[icnaas.monitor.CCN_NUMBER_OF_INTERESTS]['scale_out']:
                    actions.append(SCALE_OUT_INTERESTS)
                elif int(values[icnaas.monitor.CCN_NUMBER_OF_INTERESTS]) <= \
                    self.thresholds[icnaas.monitor.CCN_NUMBER_OF_INTERESTS]['scale_in']:
                    actions.append(SCALE_IN_INTERESTS)
            else:
                pass
        return actions

class ServiceOrchestrator(object):
    """
    ICNaaS SO.
    """

    def __init__(self, token, tenant):
        # this python thread event is used to notify the SOD that the runtime phase can execute its logic
        self.event = threading.Event()
        self.so_e = SOE(token=token, tenant=tenant, ready_event=self.event)
        self.so_d = SOD(so_e=self.so_e, token=token, ready_event=self.event)
        LOG.debug('Starting SOD thread...')
        self.so_d.start()


import os
import sample_db
import fato_db
from datetime import date
import smartTag
'''
    For the given path, get the List of all files in the directory tree
'''

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles

def findDisk(dirPath):
    return dirPath.split('/')[2]

def read_sample(dirName):
    #dirName = '/Users/luisclaudio/Downloads/Groove Metal';

    # Get the list of all files in directory tree at given path
    listOfFiles = getListOfFiles(dirName)

    # Print the files
    # for elem in listOfFiles:
    # print(elem)

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

    # Get the list of all files in directory tree at given path
    listOfFiles = list()
    files = []#[['path', 'name', 'disk', 'extension', 'date']]
    for (dirpath, dirnames, filenames) in os.walk(dirName):
        listOfFiles += [os.path.join(dirpath, file) for file in filenames]
        # files += [dirpath,[os.path.join('', file) for file in filenames]]
        for i in filenames:
            if i[-3:] in ['wav', 'mp3', 'aif']:
                files.append([i, dirpath, i[-3:], findDisk(dirpath), date.today().strftime("%d/%m/%Y") ])

    # Print the files
    # for elem in listOfFiles:
    #    print(elem)
    for elem in files:
        print(elem)
    return files

def add_sample(fileSample):
    sample_db.add(fileSample[0], fileSample[1], fileSample[2], fileSample[3], fileSample[4], )
    syncOnFato()

def add_command(lstSamples):
    for i in lstSamples[1:]:
        sample_db.add(i[0],i[1],i[2],i[3],i[4],)
    syncOnFato()

def syncOnFato():
    for row in sample_db.viewall():
        fato_db.addSampleIfNotExist(row[0])

def autoTag():
    lstAutoTag = smartTag.autoTag()
    for i in lstAutoTag:
        fato_db.addIfNotExist(i[1], i[2])

def runCode(dirName):
    minhaLista = read_sample(dirName)
    add_command(minhaLista)
    smartTag
    #autoTag()


import pytrends
from pytrends.request import TrendReq
pytrends = TrendReq(hl="en-US", tz=360)
kw_list = ["NFT's"]
kw_list = ["oil"]
kw_list = ["deep value driller"]
pytrends.build_payload(kw_list=kw_list)
#pytrend.build_payload(kw_list, timeframe='today 1-m')

historicaldf = pytrend.get_historical_interest(
  kw_list,
  year_start=2020, month_start=10, day_start=1, hour_start=0,
  year_end=2022, month_end=2, day_end=21, hour_end=15, cat=0, geo='', gprop='', sleep=0)

historicaldf.plot(figsize=(20, 12))
historicaldf.plot(subplots=True, figsize=(20, 12))

df = pytrends.interest_over_time()

adf = df[df.index > '2021-06-01']

df.plot(figsize=(20,7), color='purple', linewidth=7, label=kw_list)
adf["NFT's"].plot(figsize=(20,7), color='purple', linewidth=7)
adf["oil"].plot(figsize=(20,7), color='purple', linewidth=7)
adf["ukraine"].plot(figsize=(20,7), color='purple', linewidth=7)
adf["deep value driller"].plot(figsize=(20,7), color='purple', linewidth=7)


import logging

from schedule import every

from dispatch.database.core import SessionLocal
from dispatch.decorators import scheduled_project_task, timer
from dispatch.plugin import service as plugin_service
from dispatch.project.models import Project
from dispatch.scheduler import scheduler
from dispatch.term import service as term_service
from dispatch.term.models import TermCreate

log = logging.getLogger(__name__)

@scheduler.add(every(1).hour, name="sync-terms")
@timer
@scheduled_project_task
def sync_terms(db_session: SessionLocal, project: Project):
    """Syncs terms from external sources."""
    term_plugin = plugin_service.get_active_instance(
        db_session=db_session, project_id=project.id, plugin_type="term"
    )

    if not term_plugin:
        log.warning(
            f"Skipping syncing terms. No term plugin enabled. Project: {project.name}. Organization: {project.organization.name}"
        )
        return

    for t in term_plugin.instance.get():
        log.debug(f"Adding term {t} to {project.name} project.")
        term_in = TermCreate(**t)
        term = term_service.get_by_text(db_session=db_session, text=term_in.text)

        if term:
            term_service.update(db_session=db_session, term=term, term_in=term_in)
        else:
            term_service.create(db_session=db_session, term_in=term_in)


# -*- coding: utf-8 -*-
# @Time    : 2019/10/15 0015 16:03
# @Author  : Erichym
# @Email   : 951523291@qq.com
# @File    : 537.py
# @Software: PyCharm

class Solution:
    def complexNumberMultiply(self, a: str, b: str) -> str:
        plus_ind1=a.find('+')
        plus_ind2 = b.find('+')
        a1=int(a[0:plus_ind1])
        b1=int(a[plus_ind1+1:-1])
        a2=int(b[0:plus_ind2])
        b2=int(b[plus_ind2+1:-1])
        return str(int(a1*a2-b1*b2))+"+"+str(int(a1*b2+b1*a2))+"i"

if __name__=="__main__":
    a="78+-76i"
    b="-86+72i"
    so=Solution()
    a=so.complexNumberMultiply(a,b)
    print(a)


from collections import OrderedDict
from typing import Literal

import numpy as np
from ta.trend import sma_indicator, ema_indicator

from model.strategies._mixin import StrategyMixin

class MovingAverage(StrategyMixin):
    """
    Strategy that uses a simple or exponential moving average as a trigger for
    going long or short, with no neutral position. When the price crosses over
    above the moving average it goes long, and vice versa when the price crosses below.

    Parameters
    ----------
    ma : int
        Moving average window.
    moving_av : Literal["sma", "ema"], optional
        Type of moving average (simple or exponential), by default 'sma'.
    data : pd.DataFrame, optional
        Dataframe of OHLCV data, by default None.
    **kwargs : dict, optional
        Additional keyword arguments to be passed to parent class, by default None.

    Attributes
    ----------
    params : OrderedDict
        Parameters for the strategy, by default {"ma": lambda x: int(x)}

    Methods
    -------
    update_data()
        Retrieves and prepares the data.
    _calculate_positions(data)
        Calculates positions based on strategy rules.
    get_signal(row)
        Returns signal based on current data.

    """
    def __init__(self, ma: int, moving_av: Literal["sma", "ema"] = 'sma', data=None, **kwargs):
        """
        Parameters
        ----------
        ma : int
            Moving average window.
        moving_av : Literal["sma", "ema"], optional
            Type of moving average (simple or exponential), by default 'sma'.
        data : pd.DataFrame, optional
            Dataframe of OHLCV data, by default None.
        **kwargs : dict, optional
            Additional keyword arguments to be passed to parent class, by default None.
        """
        self._ma = ma
        self._moving_av = moving_av

        self.params = OrderedDict(ma=lambda x: int(x))

        StrategyMixin.__init__(self, data, **kwargs)

    def __repr__(self):
        return "{}(symbol = {}, SMA = {})".format(self.__class__.__name__, self.symbol, self._ma)

    def update_data(self, data):
        """
        Updates the input data with additional columns required for the strategy.

        Parameters
        ----------
        data : pd.DataFrame
            OHLCV data to be updated.

        Returns
        -------
        pd.DataFrame
            Updated OHLCV data containing additional columns.
        """
        data = super().update_data(data)

        if self._moving_av == 'sma':
            data["SMA"] = sma_indicator(close=data[self.close_col], window=self._ma)
        elif self._moving_av == 'ema':
            data["SMA"] = ema_indicator(close=data[self.close_col], window=self._ma)
        else:
            raise('Method not supported')

        return data

    def _calculate_positions(self, data):
        """
        Calculates positions based on strategy rules.

        Parameters
        ----------
        data : pd.DataFrame
            OHLCV data.

        Returns
        -------
        pd.DataFrame
            OHLCV data with additional 'position' column containing -1 for short, 1 for long.
        """
        data["position"] = np.where(data["SMA"] > data[self.close_col], 1, 0)
        data["position"] = np.where(data["SMA"] < data[self.close_col], -1, data["position"])
        return data

    def get_signal(self, row=None):
        """
        Returns signal based on current data.

        Parameters
        ----------
        row : pd.Series, optional
            Row of OHLCV data to generate signal for, by default None.

        Returns
        -------
        int
            Signal (-1 for short, 1 for long, 0 for neutral).
        """
        if row is None:
            row = self.data.iloc[-1]

        if row["SMA"] > row[self.close_col]:
            return 1
        elif row["SMA"] < row[self.close_col]:
            return -1

        elif row["SMA"] == row[self.close_col]:
            return 0


# -*- coding: utf-8 -*-
import scrapy
import re
import urllib.parse
import codecs
from scrapy.spidermiddlewares.httperror import HttpError
from twisted.internet.error import DNSLookupError
from twisted.internet.error import TimeoutError,TCPTimedOutError

class Photo3Spider(scrapy.Spider):
    name = 'photo3'
    allowed_domains = ['68aiav.com']
    start_urls = ['http://www.68aiav.com/vodtag/无码破解版']

    def parse(self, response):
        title_li = response.xpath('//div[@class="wrap mt20"]//li')
        for title in title_li:
            item = {}
            item['title'] = title.xpath('.//h3/text()').extract_first()
            item['date'] = title.xpath('.//dt/text()').extract_first()
            item['href'] = title.xpath('.//a/@href').extract_first()
            item['href'] = 'http://www.68aiav.com' + item['href']
            item['img'] = "http:"+ title.xpath('.//img/@data-original').extract_first()

            yield scrapy.Request(
                item["href"],
                callback=self.play_url, #跳转到播放器页面
                meta = {"item":item}
            )

    def play_url(self,response):
        item = response.meta['item']
        # 全集
        item['play'] = response.xpath('//div[@class="wrap mt20"]//li/a/@href').extract_first()
        item['play'] = 'http://www.68aiav.com' +item['play']
        yield scrapy.Request(
            item['play'],
            callback=self.m3u8_url, #获取m3u8的地址
            meta={"item":item}
        )
    def m3u8_url(self,response):
        item = response.meta['item']
        # m3u8 = response.xpath('//')
        # 得到的url_1是%2f%50%75%62%6c%69%63%2f%7形式,
        # ?????有的时候会报这里有问题,有的时候不会??????
        url_if = re.findall('


from langkit.parsers import List
from langkit.dsl import Field, T

from language.parser import A, GprNode

class TypedStringDecl(GprNode):
    type_id = Field(type=T.Identifier)
    string_literals = Field(type=T.StringLiteral.list)

A.add_rules(
    typed_string_decl=TypedStringDecl(
        "type",
        A.identifier,
        "is",
        "(",
        List(A.string_literal, sep=","),
        ")",
        ";",
    ),
)


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def estimate_wave_duration(df, region):
    region_data = df[df['Регион'] == region].copy()

    dates = region_data['Дата'].values
    cases = region_data['Заражений за день'].values

    # print("cases = ")
    # print(cases)

    rises = []  # список для длительности подъемов
    falls = []  # список для длительности спадов
    rise_values = []  # список значений в каждом подъеме
    fall_values = []  # список значений в каждом спаде

    current_wave = []  # текущая волна (подъем или спад)
    current_trend = None  # текущий тренд: 'rise' (подъем) или 'fall' (спад)

    for i in range(1, len(cases)):
        if cases[i] <= cases[i-1]:
            if current_trend != 'rise':  # Если тренд изменился, сохраняем предыдущую волну и начинаем новую
                if current_wave:
                    rises.append(len(current_wave))
                    rise_values.append(current_wave)
                current_wave = [cases[i-1], cases[i]]
                current_trend = 'rise'
            else:  # Продолжаем текущую волну
                current_wave.append(cases[i])
        else:
            if current_trend != 'fall':   # Если тренд изменился, сохраняем предыдущую волну и начинаем новую
                if current_wave:
                    falls.append(len(current_wave))
                    fall_values.append(current_wave)
                current_wave = [cases[i-1], cases[i]]
                current_trend = 'fall'
            else:  # Продолжаем текущую волну
                current_wave.append(cases[i])

    # Добавляем последнюю волну в соответствующий список
    if current_wave:
        if current_trend == 'rise':
            rises.append(len(current_wave))
            rise_values.append(current_wave)
        else:
            falls.append(len(current_wave))
            fall_values.append(current_wave)

    # Выводим длительности подъемов и спадов
    print("Длительность подъемов (в днях):", rises)
    print("Длительность спадов (в днях):", falls)

    print("Значения в подъемах:")
    for i, values in enumerate(rise_values):
        print("Подъем", i+1, ":", values)

    print("Значения в спадах:")
    for i, values in enumerate(fall_values):
        print("Спад", i+1, ":", values)

    # Строим график
    plt.plot(region_data['Дата'],cases)

    # Отображаем волны
    # for i, wave in enumerate(rise_values):
    #     x_wave = np.arange(len(wave))
    #     x_offset = np.sum(rises[:i])  # Смещение для горизонтальной оси
    #     plt.plot(x_wave + x_offset, wave, color='blue')

    # for i, wave in enumerate(fall_values):
    #     x_wave = np.arange(len(wave))
    #     x_offset = np.sum(rises[:len(rise_values)]) + np.sum(falls[:i])  # Смещение для горизонтальной оси
    #     plt.plot(x_wave + x_offset, wave, color='blue')

    plt.title(f'Длительность волн для региона: {region}')
    plt.xlabel('Дни')
    plt.ylabel('Значение')
    plt.xticks(rotation = 45)

    table_text = f'Rises: {rises}\nFalls: {falls}'
    plt.text(0.75, 0.95, table_text, transform=plt.gca().transAxes, fontsize=10, va='top')

    plt.show()


from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
import sqlite3

class Item(BaseModel):
    project_name: str = None
    development_version: Optional[str] = None
    staging_version: Optional[str] = None
    production_version: Optional[str] = None

app = FastAPI()

@app.get("/api/health")
def get_health():
    return { "Status" : "Healthy" }

@app.post("/api/add_project/")
def add_project(item: Item):
    project_name, development_version = item.project_name, item.development_version
    staging_version, production_version =  item.staging_version, item.production_version

    data = (project_name, development_version, staging_version, production_version)
    try:
        connection = sqlite3.connect('../tagger.db')
        cursor = connection.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS tagger (
            project_name TEXT NOT NULL UNIQUE,
            development TEXT,
            staging TEXT,
            production TEXT,
            PRIMARY KEY(project_name)
            )
        ''')
        cursor.execute("INSERT INTO tagger values (?,?,?,?)", data)
        return {"Success": f"Project {project_name} Added"}
    except:
        print(f"Project {project_name} already exists")
        if development_version != None:
            cursor.execute('insert into tagger (project_name,development) values(?, ?) \
                           on conflict(project_name) do update set development=excluded.development',
                           (project_name, development_version))
            return {"Success": f"Project {project_name} development version updated to: {development_version}"}
        if staging_version != None:
            cursor.execute('insert into tagger (project_name,staging) values(?, ?) \
                           on conflict(project_name) do update set staging=excluded.staging', (project_name, staging_version))
            return {"Success": f"Project {project_name} staging version updated to: {staging_version}"}
        if production_version != None:
            cursor.execute('insert into tagger (project_name,production) values(?, ?) \
                           on conflict(project_name) do update set production=excluded.production', (project_name, production_version))
            return {"Success": f"Project {project_name} production version updated to: {production_version}"}
    finally:
        connection.commit()
        connection.close()

@app.get("/api/get_all_projects")
def get_all():
    try:
        connection = sqlite3.connect('../tagger.db')
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM tagger")
        results = cursor.fetchall()
        for i in results:
            entry = [ {"Project_name": i[0],
                       "development" : i[1],
                       "staging": i[2],
                       "production": i[3]}
                       for i in results ]
        return entry
    except:
        return {"Error" : "Could not fetch projects"}
    finally:
        connection.commit()
        connection.close()

@app.get("/api/get_projects")
def get_all(name: str):
    proj_name = name
    try:
        connection = sqlite3.connect('tagger.db')
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM tagger WHERE project_name LIKE ?", ('%'+proj_name+'%',))
        results = cursor.fetchall()
        for i in results:
            entry = [ {"Project_name": i[0], "development" : i[1], "staging": i[2], "production": i[3]} for i in results ]
        return entry
    except:
        return {"Error" : "Could not fetch projects"}
    finally:
        connection.commit()
        connection.close()


#we will use this file to save the constants

WIDTH = 600
HEIGHT = 600

ROWS = 3
COLS = 3

SQSIZE = WIDTH//ROWS

LINE_WIDTH = 20
CIRC_WIDTH = 15
CROSS_WIDTH = 20

RADIUS = SQSIZE // 4

OFFSET = 50

#color constants for the screen
BG_COLOR = (207, 185, 151)
LINE_COLOR = (30, 30, 30)
CIRCLE_COLOR = (0, 0, 0)


# -*- coding: utf-8 -*-
'''
Задание 5.1a

Сделать копию скрипта задания 5.1.

Дополнить скрипт:
- Добавить проверку введенного IP-адреса.
- Адрес считается корректно заданным, если он:
   - состоит из 4 чисел разделенных точкой,
   - каждое число в диапазоне от 0 до 255.

Если адрес задан неправильно, выводить сообщение:
'Incorrect IPv4 address'

Ограничение: Все задания надо выполнять используя только пройденные темы.

'''

LB = 0
UA = 0
IP = True
ipAddr = input('Enter IPv4 address like this X.X.X.X : ').split('.')
if len(ipAddr) == 4:
    try:
        for i in ipAddr:
            if 0 <= int(i) <= 255:
                if int(i) == 255:
                    LB += 1
                elif int(i) == 0:
                    UA += 1
                else:
                    continue
            else:
                IP = False
                break
        if IP == True:
            if int(ipAddr[0]) in range(1, 224):
                print('Address is unicast')
            elif int(ipAddr[0]) in range(224, 240):
                print('Address is multicast')
            elif LB == 4:
                print('Address is Local Broadcast')
            elif UA == 4:
                print('Address is UnAssigned')
            else:
                print('Address is unused')
        else:
            print('Incorrect IPv4 address  Limit')
    except ValueError:
        print('Incorrect IPv4 address ValueError')
else:
    print('Incorrect IPv4 address')


from selenium import webdriver
from selenium.webdriver.common.by import By
import os
currentDirectory = os.getcwd()
if not os.path.exists('scrape_results'):
    os.makedirs('scrape_results')

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options, executable_path=currentDirectory + "/chromedriver.exe")

# https://www.silverlakemobility.com/plugins/WebPlugIn?type=1-1&action={}&email=fongyoong8@gmail.com&data=hSiJpBIcBFFMPGRgXWckfA==
# https://www.silverlakemobility.com/plugins/WebPlugIn?type={}&email=fongyoong8@gmail.com&data=v6HVSSWR+DGJWVuqTGCNyQ==

for i in range(0, 1000, 1):
    driver.get('https://www.silverlakemobility.com/plugins/WebPlugIn?type={}&email=fongyoong8@gmail.com&data=v6HVSSWR+DGJWVuqTGCNyQ=='.format(i))
    body = driver.find_elements(By.XPATH, '//body')[0]
    children = body.find_elements_by_css_selector("*")
    if len(children) > 0:
        driver.save_screenshot("scrape_results\\{}.png".format(i))

driver.close()


import pygame

pygame.init()

(WindowWidth, WindowHeight) = (800, 600)
vel = 5
vel_ballx = 3
vel_bally = 3
width = WindowWidth * 0.313
height = WindowHeight * 0.042
x1 = (WindowWidth / 2) - (width / 2)
y1 = 550
x2 = (WindowWidth / 2) - (width / 2)
y2 = 50 - height
x_ball = (WindowWidth / 2)
y_ball = (WindowHeight / 2)

screen = pygame.display.set_mode((WindowWidth, WindowHeight))
pygame.display.set_caption('Ping Pong 1.0')

running = True
while running:
    pygame.time.delay(16)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    y_ball += vel_bally
    x_ball += vel_ballx

    if x_ball > WindowWidth or x_ball < 0:
        vel_ballx = -vel_ballx

    if y_ball >= y1 and x_ball >= x1 and x_ball <= x1 + width:
        vel_bally = vel_bally * -1

    if y_ball <= y2 + height and x_ball >= x2 and x_ball <= x2 + width:
        vel_bally = vel_bally * -1

    if y_ball < 0 or y_ball > WindowHeight:
        vel_bally = vel_bally * -1

    if keys[pygame.K_a]:
        x2 -= vel
        if x2 < 0:
            x2 = 0

    elif keys[pygame.K_d]:
        x2 += vel
        if x2 > 800 - width:
            x2 = 800 - width

    if keys[pygame.K_LEFT]:
        x1 -= vel
        if x1 < 0:
            x1 = 0

    elif keys[pygame.K_RIGHT]:
        x1 += vel
        if x1 > 800 - width:
            x1 = 800 - width

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 0, 0), (x1, y1, width, height))
    pygame.draw.rect(screen, (0, 0, 0), (x2, y2, width, height))
    pygame.draw.circle(screen, (205, 13, 13), (x_ball, y_ball), 12)
    pygame.display.flip()

pygame.quit()


#1001번 - 두 정수 A와 B를 입력받은 다음, A-B를 출력하는 프로그램을 작성하시오.

a, b = input("정수를 두개 입력해주세요:").split()

a = int(a)
b = int(b)

print(a-b)

#input을 사용하여 받은 값은 문자열에 여겨진다.
#split() 함수는 특정란 문자로 자를 때 사용하며 공백을 기준으로 입력한 문자열을 공백을 기준으로 잘라 a,b에 각각 대입한다.
#a와 b를 각각 문자열을 int() 함수를 사용하여 정수형으로 변환한다.
#"정수 - 정수" 연산을 수행한다.

#한번에 두개의 값을 입력 받는 경우 split() 함수를 사용하기도 한다.


import boto3
import sys
from pprint import pprint

fileName = sys.argv[1]
name = sys.argv[2]

bucket='itpface'
collectionId='itpFaces'
s3 = boto3.resource('s3')
client=boto3.client('rekognition')

# Get list of objects for indexing
images=[('Aiden.jpeg','Aiden'),
      ('Caleb.jpeg','Caleb'),
      ('Effy.jpeg','Effy'),
      ('Elizabeth.jpeg','Elizabeth'),
      ('Ellen.jpeg','Ellen'),
      ('Hweiyi.jpeg','Huiyi'),
      ('Jenna.jpeg','Jenna'),
      ('Jiyao.jpeg','Jiyao'),
      ('Kimberly.jpeg','Kimberly'),
      ('Yen.jpeg','Yen')
      ]

def uploadSingleImg():
    file = open("./itpStudents/"+fileName,'rb')
    object = s3.Object('itpface', fileName)
    ret = object.put(Body=file,
                    Metadata={'FullName':name}
                    )
    response = client.index_faces(CollectionId=collectionId,
                                    Image={'S3Object':{'Bucket':bucket,'Name':fileName}},
                                    ExternalImageId=name,
                                    MaxFaces=2,
                                    QualityFilter="AUTO",
                                    DetectionAttributes=['DEFAULT'])
    pprint (response)

# Iterate through list to upload objects to S3
def uploadImages():
    for image in images:
        file = open("./itpStudents/"+image[0],'rb')
        object = s3.Object('itpface', image[0])
        ret = object.put(Body=file,
                        Metadata={'FullName':image[1]}
                        )
        client.index_faces(CollectionId=collectionId,
                                        Image={'S3Object':{'Bucket':bucket,'Name':image[0]}},
                                        ExternalImageId=image[1],
                                        MaxFaces=2,
                                        QualityFilter="AUTO",
                                        DetectionAttributes=['DEFAULT'])

uploadSingleImg()


"""
检查两个字符串是否为乱序字符串：检查

逐个字符依次对照检查

算法复杂度O（n^2）
"""
'''
@Time    : 2018/2/4 下午2:29
@Author  : scrappy_zhang
@File    : str01.py
'''

def anagramSolution1(s1, s2):
    # 先判断长度
    if (len(s1) != len(s2)):
        return False
    # 字符串为不可变类型，需要转换为列表，以便后续替换
    alist = list(s2)

    pos1 = 0
    stillOK = True

    while pos1 < len(s1) and stillOK:
        pos2 = 0
        found = False
        # 将S2中每个元素与S1[pos1]相比较，若相同，则将found设为True
        while pos2 < len(alist) and not found:
            if s1[pos1] == alist[pos2]:
                found = True
            else:
                pos2 = pos2 + 1
        # 将S2中对应相同的第一个元素设为None
        if found:
            alist[pos2] = None
        else:
            stillOK = False

        pos1 = pos1 + 1

    return stillOK

print(anagramSolution1('abcd', 'dcba'))

2算法分析/乱序字符串检查/str01.py


'''
At first, I thought the problem was solved by DP. However, O(n**2) was still needed no matter what, leading to time limit exceeded.
After a while, it is noticed that this can be solved by the greedy algorithm as what matters is the maximum position.

At each point, record the farest point it can get. Update the farest point while iterating the nums list.
Take [2,3,1,1,4] for example,
At index 0, max_ = 2
At index 1, 1+3 > max_ ==> max_4 (Note that it is feasible as index 0 can choose to reach index 1 and jump to 4.)
At the time max_>= total length -1, we got the answer!

Take [3,2,1,0,4] for example,
At index 0, max_ = 3
At index 1, 1+2 not bigger than max_, so it doesn't matter if we change or not.
At index 2, 2+1 not bigger than max_, so it doesn't matter if we change or not.
At index 3, the max_ is still 3 so it means the sequence terminates at this point.

Time complexity: O(n)

'''

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        if len(nums) <=1:
            return True

        max_ = nums[0]
        i = 0
        while i <= max_:
            if i + nums[i]>=len(nums)-1:
                return True
            if i + nums[i]>max_:
                max_ = i+nums[i]

            i+=1

        return False


#The underlying data structure for Blossom is going to be a key-value store that uses the common names for flowers as the key and saves the floral meaning of the flower as the value.

from linked_list import Node, LinkedList
from blossom_lib import flower_definitions

class HashMap:
  def __init__(self, size):
    self.array_size = size
    self.array = [LinkedList() for number in range(size)]

  #Generate a unique hash code for any given key
  def hash(self, key):
    return sum(key.encode())

  #Convert the hash code into an array index to use with self.array
  def compress(self, hash_code):
    return hash_code % self.array_size

  #Place a key value pair at the index given by compress
  def assign(self, key, value):
    array_index = self.compress(self.hash(key))
    #self.array[array_index] = [key,value]
    payload = Node([key,value])
    list_at_array = self.array[array_index]
    for item in list_at_array:
      if key == item[0]:
        item[1] = value
        return
    list_at_array.insert(payload)

  #Retrun the value of a given key
  def retrieve(self, key):
    array_index = self.compress(self.hash(key))
    list_at_index = self.array[array_index]
    #if payload =! None and payload[0] == key:
      #return payload[1]
    #else:
      #return None
    for item in list_at_index:
      if item[0] == key:
        return item[1]
      return None

blossom = HashMap(len(flower_definitions))

for flower in flower_definitions:
  #print(flower)
  blossom.assign(flower[0], flower[1])

print(blossom.retrieve('snapdragon'))


import pandas as pd
import numpy as np
import joblib
import ast
from google.cloud import storage
from joblib import Parallel, delayed
import pandas as pd
from pandas import DataFrame
import numpy as np
import joblib
import ast
from joblib import Parallel, delayed
import json
import os
from tqdm import tqdm
import os, pdb
from datetime import datetime
import pickle
from collections import defaultdict, deque
import re
import faiss
from google.cloud import storage

def string2array(x):
    x = x.replace('\n', '').strip('[]')
    x_list = [float(i) for i in x.split(' ') if len(i) != 0]
    return np.array(x_list)

def drop_columns(df):
    to_drop = ['Red Fruit','Tropical','Tree Fruit','Oaky',
               'Ageing','Black Fruit','Citrus','Dried Fruit','Earthy',
               'Floral','Microbio','Spices', 'Vegetal',
               'Unnamed: 58', 'None_child', 'None_count', "None",
                'Unnamed: 60', 'Unnamed: 61', 'Unnamed: 62', 'Unnamed: 63', 'Unnamed: 64']

    for c in to_drop:
        try:
            df.drop(c, axis = 1, inplace= True)
        except Exception as e:
            print(e)

    return df

def fill_na(df):
    with open('/opt/ml/wine/code/data/meta_data/string_columns.json','r',encoding='utf-8') as f:
        cols = json.load(f)
        for col in cols:
            if col in df.columns: df[col] = df[col].fillna('Empty')

    with open('/opt/ml/wine/code/data/meta_data/dict_columns.json','r',encoding='utf-8') as f:
        cols = json.load(f)
        for col in cols:
            if col in df.columns: df[col] = df[col].fillna('{}')

    with open('/opt/ml/wine/code/data/meta_data/seq_columns.json','r',encoding='utf-8') as f:
        cols = json.load(f)
        for col in cols:
            if col in df.columns: df[col] = df[col].fillna("[]")

    with open('/opt/ml/wine/code/data/meta_data/float_columns.json','r',encoding='utf-8') as f:
        cols = json.load(f)
        #col = [c for c in col if '_count' in c]
        #for col in cols:
            #if col in df.columns: df[col] = df[col].fillna(0)

    return df

def str2list(x):
    if len(x) > 0:
        if x[0] != '[':
            list = [x]
        else:
            list = ast.literal_eval(x)
            if len(list) == 0: list = ['Empty']
    else: list = ['Empty']

    return [str(s).replace(' ','') for s in list]

def feature_mapper(df, column):

    def space_remover(x):
        try:
            return str(x).replace(' ','_')
        except:
            print(column, x)
            return x
    df.loc[:,column] = df.loc[:,column].apply(lambda x: space_remover(x))

    unique_val = list(df[column].unique())
    unique_val.sort()
    feature2idx = {f:i for i, f in enumerate(unique_val)}
    idx2feature = {i:f for i, f in enumerate(unique_val)}

    if not os.path.exists('/opt/ml/wine/code/data/feature_map/'):
        os.makedirs('/opt/ml/wine/code/data/feature_map/')

    with open(f'/opt/ml/wine/code/data/feature_map/{column}2idx.json','w',encoding='utf-8') as f:
        json.dump(feature2idx, f, ensure_ascii=False)
    with open(f'/opt/ml/wine/code/data/feature_map/idx2{column}.json','w',encoding='utf-8') as f:
        json.dump(idx2feature, f, ensure_ascii=False)

    return feature2idx, idx2feature

def list_feature_mapper(args, df, column):

    df[column] = df[column].apply(lambda x: str2list(x))
    exploded = df[column].explode(column)

    if args.prepare_recbole:
        df[column] = df[column].apply(lambda x: " ".join(x))

    unique_val = set(list(exploded))

    feature2idx = {f:i for i, f in enumerate(unique_val)}
    idx2feature = {i:f for i, f in enumerate(unique_val)}

    if not os.path.exists('/opt/ml/wine/code/data/feature_map/'):
        os.makedirs('/opt/ml/wine/code/data/feature_map/')

    with open(f'/opt/ml/wine/code/data/feature_map/{column}2idx.json','w',encoding='utf-8') as f:
        json.dump(feature2idx, f, ensure_ascii=False)
    with open(f'/opt/ml/wine/code/data/feature_map/idx2{column}.json','w',encoding='utf-8') as f:
        json.dump(idx2feature, f, ensure_ascii=False)

    return df, feature2idx, idx2feature

def map_all_single_features(df):
    with open('/opt/ml/wine/code/data/meta_data/string_columns.json','r',encoding='utf-8') as f:
        single_category_columns = json.load(f)

    for c in single_category_columns:
        if c in df.columns: feature_mapper(df, c)
    return

def map_all_list_features(df,args):
    list_columns = ['grape','pairing']
    for c in list_columns:
        df ,_ ,_ = list_feature_mapper(args, df, c)
    return df

def note_mapper(df, note_col):

    note = note_col

    note_col = note_col + '_child'
    note_col = note_col.replace(' ','_')

    try:
        df[note_col] = df[note_col].apply(lambda x: ast.literal_eval(x))
    except Exception as e:
        print(e)

    unique_val = []
    for note_dic in df[note_col]:
        unique_val.extend(list(note_dic.keys()))
    unique_val = list(set(unique_val))

    feature2idx = {f:i for i, f in enumerate(unique_val)}
    idx2feature = {i:f for i, f in enumerate(unique_val)}

    if not os.path.exists('/opt/ml/wine/code/data/feature_map/'):
        os.makedirs('/opt/ml/wine/code/data/feature_map/')

    with open(f'/opt/ml/wine/code/data/feature_map/{note}2idx.json','w',encoding='utf-8') as f:
        json.dump(feature2idx, f, ensure_ascii=False)
    with open(f'/opt/ml/wine/code/data/feature_map/idx2{note}.json','w',encoding='utf-8') as f:
        json.dump(idx2feature, f, ensure_ascii=False)

    return feature2idx, idx2feature

def expand_notes(df, args):
    notes = ['Red Fruit','Tropical','Tree Fruit','Oaky',
        'Ageing','Black Fruit','Citrus','Dried Fruit','Earthy',
        'Floral','Microbio','Spices', 'Vegetal']

    i = 0
    if args.expand_notes:
        for note_col in tqdm(notes):

            note_df = []

            feature2idx, idx2feature = note_mapper(df, note_col)

            for total_count, note_dic in tqdm(zip(df[note_col+'_count'], df[note_col.replace(' ','_') + '_child'])):
                row_data = [0 for i in range(len(feature2idx))]

                if total_count != 0:
                    for note in note_dic:
                        row_data[feature2idx[note]] = note_dic[note] / total_count

                note_df.append(row_data)

            columns = [idx2feature[i] for i in range(len(idx2feature))]
            note_df = pd.DataFrame(note_df, columns=columns, index = df.index)

            if i == 0:
                result = note_df
                i += 1
            else:
                result = pd.concat([result, note_df], axis=1)

            df.drop(note_col.replace(' ','_') + '_child', axis = True, inplace = True)

        df = pd.concat([df, result], axis=1)

    else:
        for note_col in tqdm(notes):

            note_array = []

            feature2idx, idx2feature = note_mapper(df, note_col)

            for total_count, note_dic in tqdm(zip(df[note_col+'_count'], df[note_col.replace(' ','_') + '_child'])):
                row_data = [str(0) for i in range(len(feature2idx))]

                if total_count != 0:
                    for note in note_dic:
                        row_data[feature2idx[note]] = str(note_dic[note] / total_count)
                note_array.append(row_data)

            note_seq = note_col.replace(' ','_') + '_seq'
            df[note_seq] = note_array
            df[note_seq] = df[note_seq].apply(lambda x: " ".join(x))
            df.drop(note_col.replace(' ','_') + '_child', axis = 1, inplace = True)
    return df

def item_preprocess(df, args):
    df = fill_na(df)
    df = drop_columns(df)
    map_all_single_features(df)
    df = map_all_list_features(df, args)
    #df = expand_notes(df, args)
    return df

def inter_preprocess(df, args):

    df = df[df['email'].notna()]
    tqdm.pandas()

    return df

def parallel(func, df, args, num_cpu):

    df_chunks = np.array_split(df, num_cpu)

    print('Parallelizing with ' +str(num_cpu)+'cores')
    with Parallel(n_jobs = num_cpu, backend="multiprocessing") as parallel:
        results = parallel(delayed(func)(df_chunks[i], args) for i in range(num_cpu))

    for i,data in enumerate(results):
        data.dropna(inplace = True)
        if i == 0:
            result = data
        else:
            result = pd.concat([result, data], axis = 0)

    for c in result.columns:
        if 'Unnamed:' in c:
            result.drop(c, axis = 1, inplace= True)

    return result

def to_recbole_columns(columns):

    meta_data_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'meta_data')

    float_path = os.path.join(meta_data_folder, 'float_columns.json' )
    float_seq_path = os.path.join(meta_data_folder, 'float_seq_columns.json' )
    token_path = os.path.join(meta_data_folder, 'token_columns.json' )
    seq_path = os.path.join(meta_data_folder, 'seq_columns.json' )

    with open(float_path,'r',encoding='utf-8') as f:  float_columns = set(json.load(f))
    with open(float_seq_path,'r',encoding='utf-8') as f:  float_seq_columns = set(json.load(f))
    with open(token_path,'r',encoding='utf-8') as f:  token_columns = set(json.load(f))
    with open(seq_path,'r',encoding='utf-8') as f:  seq_columns = set(json.load(f))

    recbole_columns = []
    df = []
    for c in columns:
        if c in float_columns: recbole_columns.append(f'{c}:float')
        elif c in token_columns: recbole_columns.append(f'{c}:token')
        elif c in seq_columns: recbole_columns.append(f'{c}:token_seq')
        elif c in float_seq_columns: recbole_columns.append(f'{c}:float_seq')
        else:
            df.append(c)
            recbole_columns.append(f'{c}:float')

    print(f"{df[:5]}... are defaulely assigned to float type")

    return recbole_columns

def afterprocessing(sub,train):
    # 날짜를 datetime 형식으로 변환
    new_train = train.copy()
    new_train['time'] = new_train['time'].apply(lambda x: datetime.fromtimestamp(x))

    # 유저별 영화시청 마지막년도 추출
    user_mv_idx= new_train.groupby('user')['time'].max().reset_index()
    user_mv_idx['lastyear'] = user_mv_idx['time'].apply(lambda x : x.year)
    user_mv_idx.drop('time',inplace = True ,axis=1)

    # 영화 개봉년도와 유저시청년도 합친 데이터프레임 구축
    years = pd.read_csv("/opt/ml/input/data/train/years.tsv",sep = '\t')
    sub = pd.merge(sub,years, on = ['item'] , how = 'left')
    sub = pd.merge(sub,user_mv_idx,on =['user'],how ='left')

    # 늦게 개봉한 영화 제외하고 상위 10개 추출
    sub = sub[sub['lastyear'] >= sub['year']]
    sub = sub.groupby('user').head(10)[['user','item']]
    return sub

def keep_only_english(text):
    try:
        english_text = re.sub(r'[^a-zA-Z\s]', '', text)
        return english_text.lower().strip()
    except: return None

def find_vectors(columns_name : deque, grouped_vectors: DataFrame):
    column, name = columns_name.popleft()
    if name is not None:
        grouped_vectors = grouped_vectors.query(f"{column} == '{name}'")
        if columns_name:
            vector = find_vectors(columns_name, grouped_vectors.drop(column,axis = 1))
        else:
            vector = grouped_vectors.vectors.mean()

        return vector
    else:
        return grouped_vectors.vectors.mean()

def get_item_vector(df, vector_path):
    df = df.sort_values(by='wine_id', ascending=True)
    with open(vector_path,'r') as f:
        vectors = json.load(f)
    vector_list = []

    for id in tqdm(df.wine_id):
        id = str(id)
        if id in vectors.keys():
            vector_list.append(np.array(vectors[id]))
        else:
            vector_list.append(None)
    df['vectors'] = vector_list
    return df

def fill_vectors(df : DataFrame, vector_path: str):
    df.drop_duplicates(subset='wine_id', keep='first', inplace=True)
    columns_to_check = df.columns.drop('wine_id')
    df.dropna(subset=columns_to_check, how='all', inplace=True)
    df = get_item_vector(df, vector_path)

    for col in ['country','region1', 'winetype', 'wine_style','region']:
        if col in df.columns:
            df[col] = df[col].apply(keep_only_english)

    grouped_vectors = df[df.vectors.isna()==False].groupby([
        'country',
        'region1',
        'winetype',
        'wine_style'
    ]).agg({'vectors': 'mean'}).reset_index()

    non_vectors = df[df['vectors'].isna()==True]
    non_vectors_cols = list(grouped_vectors.columns)
    non_vectors_cols.append('wine_id')
    non_vectors = non_vectors.loc[:, non_vectors_cols]
    vectors = []

    for index, row in tqdm(non_vectors.iterrows()):
        columns_name = deque()

        for column, name in zip(row.keys(), row.values):
            if column != 'vectors':
                columns_name.append((column, name))
            else: break
        vector = find_vectors(columns_name, grouped_vectors)
        vectors.append(vector)

    non_vectors['vectors'] = vectors

    not_filled = non_vectors[non_vectors['vectors'].isna()]
    filled = non_vectors[non_vectors['vectors'].notna()]

    mean_vector = filled['vectors'].mean()

    not_filled['vectors'] = [mean_vector for _ in range(len(not_filled))]

    filled_total = pd.concat([filled, not_filled], axis=0)

    no_vectors = df[df['vectors'].isna()]
    no_vectors.drop('vectors', axis=1, inplace=True)
    no_vectors.reset_index(drop=True, inplace=True)  # Use drop=True to reset the index without keeping the old index
    no_vectors = no_vectors.sort_values(by='wine_id', ascending=True)

    yes_vectors = df[df['vectors'].notna()]
    yes_vectors.reset_index(drop=True, inplace=True)

    if len(set(no_vectors['wine_id']).intersection(set(yes_vectors['wine_id']))) != 0:
        shared_wine_ids = set(no_vectors['wine_id']).intersection(set(yes_vectors['wine_id']))
        yes_vectors = yes_vectors[~yes_vectors['wine_id'].isin(shared_wine_ids)]

    filled_total.reset_index(drop = True, inplace = True)
    filled_total = filled_total.sort_values(by='wine_id', ascending=True)

    no_vectors.set_index('wine_id', inplace = True)
    no_vectors['wine_id'] = no_vectors.index

    yes_vectors.set_index('wine_id', inplace = True)
    yes_vectors['wine_id'] = yes_vectors.index

    filled_total.set_index('wine_id', inplace = True)
    filled_total['wine_id'] = filled_total.index

    no_vectors['vectors'] = filled_total['vectors']
    no_vectors.reset_index(drop = True, inplace = True)
    yes_vectors.reset_index(drop = True, inplace = True)

    df = pd.concat([no_vectors,yes_vectors], axis=0)
    print('df')
    print(len(df.drop_duplicates(subset='wine_id')), len(df))

    df = df.sort_values(by='wine_id', ascending=True).reset_index(drop = True)
    return df

def count_grape(data : pd.DataFrame):
    dict = defaultdict(float)
    for grapes, dist in zip(data.grape, data.distance):
        try: grapes = ast.literal_eval(grapes)
        except: pass

        for grape in grapes:
            try: dict[grape.lower()] += dist
            except TypeError as t: continue
    return [max(dict , key=lambda k: dict[k])]

def count_pairing(data : pd.DataFrame):
    dict = defaultdict(float)
    data = data[data['pairing'] != '']
    for pairings, dist in zip(data.pairing, data.distance):
        for menu in pairings.split(' '):
            try: dict[menu] += dist
            except TypeError as t: continue
    return max(dict , key=lambda k: dict[k])

def count_most_str(data : pd.DataFrame, column):
    dict = defaultdict(float)
    for feat, dist in zip(data[column], data.distance):
        try:
            dict[feat] += dist
        except TypeError as t:
            continue
    return max(dict , key=lambda k: dict[k])

def count_most_cont(data : pd.DataFrame, column):
    dict = defaultdict(float)
    for feat, dist in zip(data[column], data.distance):
        try:
            dict[feat] += dist
        except TypeError as t:
            continue
    return max(dict , key=lambda k: dict[k])

def most_close(sim_items : DataFrame):
    result = {}
    sim_items.dropna(inplace=True)
    for col in sim_items.columns:
        if col == 'pairing':
            result[col] = count_pairing(sim_items)
        elif col == 'grape':
            result[col] = count_grape(sim_items)
        elif col in ['price', 'wine_rating', 'num_votes',
                     'Red Fruit', 'Tropical', 'Tree Fruit', 'Oaky', 'Ageing', 'Black Fruit',
                     'Citrus', 'Dried Fruit', 'Earthy', 'Floral', 'Microbio', 'Spices',
                     'Vegetal', 'Light', 'Bold', 'Smooth', 'Tannic', 'Dry', 'Sweet', 'Soft',
                     'Acidic', 'Fizzy', 'Gentle']:
            result[col] = count_most_cont(sim_items, col)
        else:
            result[col] = count_most_str(sim_items, col)
    return result

def find_most_sim_item(df : DataFrame, to_fill_wine_id: int, index : faiss.IndexIDMap2):

    ###index should be wine_id/wine_id
    item_to_fill = df.loc[to_fill_wine_id,:]

    item_vector = item_to_fill.vectors
    try:
        None_col = list(item_to_fill.index[item_to_fill.isna()])
    except: pdb.set_trace()
    None_col.append('distance')

    if len(None_col) > 1:

        # Faiss expects the query vectors to be normalized
        to_search = np.expand_dims(item_vector, axis=0)
        to_search = np.ascontiguousarray(to_search.astype(np.float32))

        k = index.ntotal
        distances, searched_wine_ids = index.search(to_search, k=20)

        result = []
        for ids, dists in zip(searched_wine_ids[0], distances[0]):
            result.append((ids, dists))

        sim_items = df.loc[[x[0] for x in result], :]
        sim_items['distance'] = 0
        sim_items = sim_items.loc[:, None_col]

        for id, dist in result: sim_items.loc[id, 'distance'] = 1/dist
        pdb.set_trace()
        to_fill = most_close(sim_items)

        for col, val in to_fill.items():
            df.loc[to_fill_wine_id, col] = val

    return df

##data_to_normal(data,'email','timestamp','rating','wine_id')
def data_to_normal(data,user_id,timestamp,rating,wine_id):
    grouped_data = data.groupby(user_id)[rating].agg(['mean', 'std','count'])

    # 여러개 구매한 유저
    other_user = grouped_data[(grouped_data['count']>=5) & (grouped_data['std'] != 0)]

    other_user

    other_userlist = list(other_user.index)

    other_user_data = data[data[user_id].isin(other_userlist)].sort_values(by=user_id)
    other_user_data

    other_data = pd.merge(other_user_data,other_user, on =user_id,how='left')

    other_data = other_data.set_index(other_user_data.index)

    other_data['scaled_rating'] = (other_data[rating]-other_data['mean'])/other_data['std']
    print(other_data['scaled_rating'].quantile(0.75))
    result = other_data[[user_id,timestamp,'scaled_rating',wine_id]]
    result.rename(columns = {'scaled_rating':'rating'})
    return result

def get_data_from_bucket():
    bucket_name = 'inter_info_db2model'
    item_data_source_blob_name = 'item_data_bucket.csv'
    inter_data_source_blob_name = 'inter_bucket.csv'
    destination_folder = '/opt/ml/wine/data'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    item_blob = bucket.blob(item_data_source_blob_name)
    inter_blob = bucket.blob(inter_data_source_blob_name)

    item_blob.download_to_filename(os.path.join(destination_folder, item_data_source_blob_name))
    inter_blob.download_to_filename(os.path.join(destination_folder, inter_data_source_blob_name))


import pandas as pd
import csv
import numpy as np
df= pd.read_csv('sep_2019 MELT.csv')

#FLIGHT FILTER
#Def: filters flights by origin, destination, day, and hour
    #inputs:
        #df: melted data frame
        #origin: list of strings
        #destination: list of strings
        #day: list of integers
        #hour: list of integers
    #Description: it checks whether there is an empty input(s)
    #Return:new dataframe with filtered flights by origin,destination,day, and hour

def flight_filter(df, org,dest,day_,hour_):
    day=[]
    hour=[]
    for d in day_:
        day.append(np.int64(d))
    for h in hour_:
        hour.append(np.int64(h))

#     return(len(org),len(dest),len(day),len(hour))
    if len(org)==0 and len(dest)>0 and len(day)>0 and len(hour)>0:  #no org

        fl1=df[df.Dst.isin(dest)]
        fl2=fl1[fl1.Day.isin(day)]
        fl3=fl2[fl2.Rls_HR.isin(hour)]
#         return(type(df['Org'][0]),type(df['Dst'][0]),type(df['Day'][1])==numpy.int64,type(df['RlsHR'][1])==numpy.int64)
        return fl3

    if len(org)>0 and len(dest)==0 and len(day)>0 and len(hour)>0:  #no dest
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Day.isin(day)]
        fl3=fl2[fl2.Rls_HR.isin(hour)]
        return fl3

    if len(org)>0 and len(dest)>0 and len(day)==0 and len(hour)>0:  #no day
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Dst.isin(dest)]
        fl3=fl2[fl2.Rls_HR.isin(hour)]
        return fl3
    if len(org)>0 and len(dest)>0 and len(day)>0 and len(hour)==0:  #no hour
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Dst.isin(dest)]
        fl3=fl2[fl2.Day.isin(day)]
        return fl3

    if len(org)==0 and len(dest)==0 and len(day)>0 and len(hour)>0:  #no org,dest
        fl1=df[df.Day.isin(day)]
        fl2=fl1[fl1.Rls_HR.isin(hour)]
        return fl2
    if len(org)==0 and len(dest)>0 and len(day)==0 and len(hour)>0:  #no org,day
        fl1=df[df.Dst.isin(dest)]
        fl2=fl1[fl1.Rls_HR.isin(hour)]
        return fl2
    if len(org)==0 and len(dest)>0 and len(day)>0 and len(hour)==0:  #no org,hour
        fl1=df[df.Dst.isin(dest)]
        fl2=fl1[fl1.Day.isin(day)]
        return fl2

    if len(org)>0 and len(dest)==0 and len(day)>0 and len(hour)==0:  #no dest,hour
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Day.isin(day)]
        return fl2
    if len(org)>0 and len(dest)==0 and len(day)==0 and len(hour)>0:  #no dest,day
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Rls_HR.isin(hour)]
        return fl2
    if len(org)>0 and len(dest)>0 and len(day)==0 and len(hour)==0:  #no day,hour
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Dst.isin(dest)]
        return fl2

    if len(org)>0 and len(dest)==0 and len(day)==0 and len(hour)==0:  #only org
        fl1=df[df.Org.isin(org)]
        return fl1
    if len(org)==0 and len(dest)>0 and len(day)==0 and len(hour)==0:  #only dest
        fl1=df[df.Dst.isin(dest)]
        return fl1
    if len(org)==0 and len(dest)==0 and len(day)>0 and len(hour)==0:  #only day
        fl1=df[df.Day.isin(day)]
        return fl1
    if len(org)==0 and len(dest)==0 and len(day)==0 and len(hour)>0:  #only hour
        fl1=df[df.Rls_HR.isin(hour)]
        return fl1

    if len(org)==0 and len(dest)==0 and len(day)==0 and len(hour)==0:  #none entered
        return df
    if len(org)>0 and len(dest)>0 and len(day)>0 and len(hour)>0:
        fl1=df[df.Org.isin(org)]
        fl2=fl1[fl1.Dst.isin(dest)]
        fl3=fl2[fl2.Day.isin(day)]
        fl4=fl3[fl3.Rls_HR.isin(hour)]
        return fl4

org=['ATL']
dest=[]
day_=[2,3]
hour_=[2]
print(flight_filter(df, org,dest,day_,hour_))
df2 = flight_filter(df, org,dest,day_,hour_)
from tkinter import *   # from x import * is bad practice
import pandas as pd
import tkinter as tk
# http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame
import tkinter as tk
from tkinter import filedialog
import pandas as pd
#import numpy as np
from tabulate import tabulate

#importing filters
import csv
import numpy
#print(df2)

class VerticalScrolledFrame(Frame):
    """A pure Tkinter scrollable frame that actually works!
    * Use the 'interior' attribute to place widgets inside the scrollable frame
    * Construct and pack/place/grid normally
    * This frame only allows vertical scrolling

    """
    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)

        # create a vertical scrollbar for scrolling it
        vscrollbar = Scrollbar(self, orient=VERTICAL)
        vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)

        # create a horizontal scrollbar and the canvas
        hscrollbar = Scrollbar(self, orient=HORIZONTAL)
        hscrollbar.pack(fill=X, side=BOTTOM, expand=FALSE)
        canvas = Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE, anchor=W)
        vscrollbar.config(command=canvas.yview)
        hscrollbar.config(command=canvas.xview)

        # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=NW)

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

if __name__ == "__main__":

    class SampleApp(Tk):

        def __init__(self, *args, **kwargs):
            root = Tk.__init__(self, *args, **kwargs)

            self.frame = VerticalScrolledFrame(root)
            self.frame.grid(row=0, column=0)
            #self.label = Label(text="Shrink the window to activate the scrollbar.")
            #self.label.grid(row=1, column=0)

            def graphdesk(desk):
                print('you are printing imaginary graphs!')
                print(desk)

            deskNum=tk.StringVar()
            entry_deskNum = tk.Entry(self.frame.interior, textvariable=deskNum, width=6).grid(row=0, column=0)
            button_graphdesk = tk.Button(self.frame.interior, text = 'Graph the desk!', command=lambda:graphdesk(deskNum.get())).grid(row=0, column=1, sticky='w'+'e')
            headers = []
            for col in df2.columns:
                #print(col)
                headers.append(col)
            headers[0] = 'Flight#'
            headerindex = 0
            for i in headers:
                tk.Label(self.frame.interior,text=i,font=("Helvetica", 8),bg = 'cyan',anchor= 'e',relief = 'solid').grid(row = 1, column = headerindex, sticky = 'w'+'e')
                headerindex +=1
            df2.to_csv('headless.csv', header=False, index=False)
            listedDF = df2.values.tolist()
            i_index = 2
            j_index = 0
            for i in listedDF:
                for j in i:
                    if i[0] < 101:
                        tk.Label(self.frame.interior, text=j,font=("Helvetica", 8),bg = 'lightgreen',anchor= 'e',relief = 'solid').grid(row = i_index, column = j_index, sticky = 'w'+'e')
                    else:
                        tk.Label(self.frame.interior,text=j,font=("Helvetica", 8),bg = 'orange', anchor = 'e', relief = 'solid').grid(row = i_index, column = j_index, sticky = 'w'+'e')
                    j_index +=1
                i_index +=1
                j_index = 0

    app = SampleApp()
    app.mainloop()