# Processing to create the individual token dictionaries within a sentence dictionary
# Called by nlp_sentence_dictionary.py

from spacy.tokens import Token

from dna.utilities_and_language_specific import add_to_dictionary_values, check_name_gender, empty_string, \
    family_members, family_text, objects_string, processed_prepositions, space

family_roles = [key for key in family_members.keys()]

unwanted_tokens = ('INTJ',   # interjection
                   'X')      # other
# Want 'PUNCT' for semantic evaluation and 'SYM' to address statements such as "66.3% of the vote"

def _handle_noun(noun_token: Token) -> (str, str):
    """
    When processing a token (in add_token_details) that is a noun, first handle the special
    cases of a proper noun, pronoun, family member role, etc. Lastly, process a general noun.

    :param noun_token: Token of the noun
    :return: Two strings, the noun text and type
    """
    # Get text with adjectives and first level of preposition only
    noun_text = noun_token.text
    noun_texts = [noun_text]
    for child_token in noun_token.children:
        if child_token.pos_ == 'VERB':
            continue
        if child_token.dep_ == 'prep':
            noun_texts.append(child_token.text)
            for child2 in child_token.children:
                if child2.pos_ not in unwanted_tokens and child2.dep_ != 'prep' and child2.text != '.' and \
                        ((child2.dep_ == 'det' and child2.text == 'no') or child2.dep_ != 'det'):
                    if child2.dep_ == 'poss':
                        noun_texts.append(f'{child2.text}/poss/')
                    else:
                        for child3 in child2.children:
                            if child3.pos_ == 'SYM':
                                noun_texts.append(child3.text)
                        noun_texts.append(child2.text)
        else:
            if child_token.pos_ not in unwanted_tokens and child_token.text != '.' and \
                    ((child_token.dep_ == 'det' and child_token.text == 'no') or child_token.dep_ != 'det'):
                if child_token.dep_ == 'poss':
                    noun_texts.append(f'{child_token.text}/poss/')
                else:
                    noun_texts.append(child_token.text)
    noun_texts_in_order = []
    for token in noun_token.subtree:
        if token.text in noun_texts:
            noun_texts_in_order.append(token.text)
        elif f'{token.text}/poss/' in noun_texts:
            noun_texts_in_order.append(f'{token.text}/poss/')
    full_noun_text = space.join(noun_texts_in_order)
    # Handle proper nouns and pronouns
    if 'PROPN' in noun_token.pos_:      # Proper noun
        return _process_proper_noun(noun_token, full_noun_text)
    elif 'Prs' in noun_token.morph.get('PronType'):    # Personal pronoun
        return _process_personal_pronoun(noun_token)
    # Handle references to family members ('brother', 'sister', ...) or 'family'
    elif noun_text.lower() in family_roles:
        gender = family_members[noun_text.lower()]
        return full_noun_text, f'{gender}SINGPERSON' if gender else f'SINGPERSON'
    elif noun_text.lower()[0:-1] in family_roles:  # Check if plural family members were referenced
        gender = family_members[noun_text.lower()[0:-1]]   # Check if plural family members were referenced
        return full_noun_text, f'{gender}PLURALPERSON' if gender else f'PLURALPERSON'
    elif noun_text.lower() in family_text:
        return full_noun_text, 'PLURALPERSON'
    else:
        return _process_noun(noun_token, full_noun_text)

def _process_noun(token: Token, full_text: str) -> (str, str):
    """
    When processing a token that is a general noun, capture its number, gender, etc.

    :param token: Token of the noun
    :param full_text: Full text of the noun, with adjectives, prepositions, ...
    :return: Two strings, the entity text and type
    """
    symbols = [t.text for t in token.children if t.pos_ == 'SYM']
    ent_type = token.ent_type_ if token.ent_type_ else 'NOUN'
    # Include plurality in ent_type
    if 'Plur' in token.morph.get('Number'):
        ent_type = f'PLURAL{ent_type}'
    elif 'Sing' in token.morph.get('Number'):
        ent_type = f'SING{ent_type}'
    gender = empty_string
    if 'Fem' in token.morph.get('Gender'):
        gender = 'FEMALE'
    elif 'Masc' in token.morph.get('Gender'):
        gender = 'MALE'
    ent_type = f'{gender}{ent_type}' if gender else ent_type
    # Account for a determiner of 'no' - i.e., nothing (for ex, 'no information was found')
    if any([tc for tc in token.children if tc.dep_ == 'det' and tc.text == 'no']):
        ent_type = f'NEG{ent_type}'
    if 'PERCENT' in ent_type or 'MONEY' in ent_type:    # Clean up spaces introduced by the parse
        full_text = full_text.replace(' %', '%')
        for sym in symbols:
            if full_text[full_text.index(sym) + 2].isdigit():
                full_text = full_text.replace(f'{sym} ', sym)
            elif full_text[full_text.index(sym) - 2].isdigit():
                full_text = full_text.replace(f' {sym}', sym)
    return full_text, ent_type

def _process_prep_object(token: Token, dictionary: dict, prep_key: str):
    """
    When processing a token that is a preposition, capture its object details.

    :param token: Token of the preposition
    :param dictionary: Dictionary that the token details should be added to
    :param prep_key: Dictionary key where the details are added
    :return: None (Specified dictionary is updated)
    """
    # Retrieve the text for nouns (including adjectives and compound nouns)
    prep_ent, prep_ent_type = _handle_noun(token)
    prep_ent_detail = dict()
    prep_ent_detail['detail_text'] = prep_ent.split('$&')[0]
    prep_ent_detail['detail_type'] = prep_ent_type
    # Recursively call if there are more objects, related by conjunction
    for child in token.children:
        if 'cc' == child.dep_:
            dictionary[f'{prep_key}_cc'] = child.text
        elif 'conj' == child.dep_:
            _process_prep_object(child, dictionary, prep_key)
    add_to_dictionary_values(dictionary, 'prep_details', prep_ent_detail, dict)

def _process_personal_pronoun(token: Token) -> (str, str):
    """
    When processing a token that is a personal pronoun, return the text that identifies
    the person and their 'type' (gender + single/plural + PERSON).

    :param token: Token of the personal pronoun
    :return: A tuple holding the text that identifies the person and their 'type'
            (gender + single/plural + PERSON).
    """
    entity = token.text
    entity_type = token.ent_type_
    if '1' in token.morph.get('Person'):
        entity_type = 'PERSON'
    if '2' in token.morph.get('Person'):
        entity_type = 'AUDIENCE'
    elif '3' in token.morph.get('Person'):
        if token.text.lower() == 'it':
            entity_type = 'NOUN'
        else:
            entity_type = 'PERSON'
    # Include gender and plurality in ent_type
    if 'Plur' in token.morph.get('Number'):
        entity_type = f'PLURAL{entity_type}'
    elif 'Sing' in token.morph.get('Number'):
        entity_type = f'SING{entity_type}'
    if 'Fem' in token.morph.get('Gender'):
        entity_type = f'FEMALE{entity_type}'
    elif 'Masc' in token.morph.get('Gender'):
        entity_type = f'MALE{entity_type}'
    return entity, entity_type

def _process_proper_noun(token: Token, full_text: str) -> (str, str):
    """
    When processing a token that is a proper noun, return the text of the noun and its
    'type' (gender + single/plural + PERSON).

    :param token: Token of the proper noun
    :param full_text: Full text of the noun, with adjectives, prepositions, ...
    :return: A tuple holding the text of the noun and its 'type' (gender + single/plural + PERSON/
             GPE/LOC/EVENT/...).
    """
    entity_type = token.ent_type_
    if entity_type.endswith('DATE') or entity_type.endswith('TIME'):
        return full_text, entity_type
    if entity_type.endswith('GPE') or entity_type.endswith('LOC') or entity_type.endswith('FAC') \
            or entity_type.endswith('ORG') or entity_type.endswith('NORP') or entity_type.endswith('EVENT') \
            or entity_type.endswith('NOUN'):
        return _process_noun(token, full_text)
    return full_text, check_name_gender(full_text)

def _setup_entity_dictionary(ent_text: str, ent_type: str, ent_key: str, ent_lemma: str) -> dict:
    """
    Set up a dictionary to hold the entity's/token's details.

    :param ent_text: Full token string - For verbs, = lemma; For nouns, compound words
                     and modifiers added
    :param ent_type: The type of the entity - For example, SINGPERSON or NOUN
    :param ent_key: Dictionary key where the details are added
    :param ent_lemma: The lemma of the entity
    :return: A dictionary holding the entity's/token's initial dictionary definition
    """
    ent_dict = dict()
    # Lists of dictionaries have plural names while individual dictionary keys should not
    ent_base_key = 'detail'
    if 'verb' in ent_key:
        ent_base_key = 'verb'
    elif 'object' in ent_key:
        ent_base_key = 'object'
    elif 'subject' in ent_key:
        ent_base_key = 'subject'
    ent_dict[f'{ent_base_key}_text'] = ent_text
    ent_dict[f'{ent_base_key}_lemma'] = ent_lemma
    if ent_key != 'verb':
        ent_dict[f'{ent_base_key}_type'] = ent_type
    return ent_dict

def add_acomp_details(acomp_token: Token, verb_dict: dict):
    """
    Process the token details for an acomp dependency, which is not a noun/verb but an adjective. That
    adjective may be further expanded by a conjunction, an xcomp clause or preposition with a prepositional
    clause (pcomp). Although these are different dependency paths, their resultant semantics are similar.

    :param acomp_token: Token from spacy parse for an acomp sentence dependency
    :param verb_dict: Dictionary to which the acomp details are added
    :return: None (verb_dict is updated)
    """
    token_key = 'acomp'
    ent_dict = _setup_entity_dictionary(acomp_token.text, 'ADJ', token_key, acomp_token.lemma_)
    for child in acomp_token.children:
        if 'conj' == child.dep_:
            add_acomp_details(child, verb_dict)
        elif 'cc' == child.dep_:
            verb_dict[f'{token_key}_cc'] = child.text   # Future: What about and/or combinations?
        elif child.dep_ in ('xcomp', 'prep'):
            # acomp -> xcomp; e.g., 'he is able to swim' (acomp = able, xcomp = swim)
            # acomp -> prep + pcomp; e.g., 'he is tired of running' (acomp = tired, pcomp = running)
            token_words = [ww for ww in acomp_token.subtree]   # Get the full text of the acomp
            token_text = ' '.join([ww.text for ww in token_words]).strip()
            ent_dict[f'full_text'] = token_text
            # TODO: Expand to extract details of the xcomp verb or if there is a prepositional clause (pcomp)
    add_to_dictionary_values(verb_dict, token_key, ent_dict, dict)

def add_token_details(token: Token, dictionary: dict, token_key: str):
    """
    Expand/clarify the token text, get the token's entity type (or define one if blank), and
    add the token details to the specified dictionary key.

    :param token: Token from spacy parse
    :param dictionary: Dictionary that the token details should be added to
    :param token_key: Dictionary key where the details are added
    :return: None (Input dictionary is updated)
    """
    ent_text = token.text.split('$&')[0]
    if ent_text in ('.', ','):   # Erroneous parse sometimes returns punctuation
        return
    if 'verb' in token_key:
        ent_text = token.lemma_
        ent_type = 'VERB'
    else:
        ent_text, ent_type = _handle_noun(token)
    extraneous_words = []
    # Set up the entity's dictionary and add basic details
    ent_dict = _setup_entity_dictionary(ent_text, ent_type, token_key, token.lemma_)
    # Process specific children (conjunctions and prepositions)
    # Note that the conjunction and conjunctive words are included in the original text and are removed
    #    with the 'extraneous_words' processing
    for child in token.children:
        if ent_type == 'VERB' and ('obj' in child.dep_ or 'attr' in child.dep_):
            add_token_details(child, ent_dict, objects_string)
        elif 'conj' == child.dep_:
            extraneous_words.append(child.text)
            add_token_details(child, dictionary, token_key)
        elif 'cc' == child.dep_:
            extraneous_words.append(f' {child.text} ')
            dictionary[f'{token_key}_cc'] = child.text
        elif 'prep' in child.dep_:
            prep_dict = process_prep_token(child, token_key)
            if prep_dict:
                add_to_dictionary_values(ent_dict, 'preps', prep_dict, dict)
    if extraneous_words and 'object' in token_key:
        curr_text = ent_dict['object_text']
        for extraneous_word in extraneous_words:
            curr_text = curr_text.replace(extraneous_word, empty_string)
        ent_dict['object_text'] = curr_text.strip()
    add_to_dictionary_values(dictionary, token_key, ent_dict, dict)

def process_prep_token(prep_token: Token, token_key: str) -> dict:
    prep_dict = dict()
    if prep_token.text.lower() not in processed_prepositions:
        return prep_dict
    prep_dict['prep_text'] = prep_token.text
    for prep_child in prep_token.children:
        if 'obj' in prep_child.dep_ or 'attr' in prep_child.dep_:
            # Object/attr is part of the prepositional clause, so it adds to the current prep_dict
            _process_prep_object(prep_child, prep_dict, (token_key[0:-1] if token_key.endswith("s") else token_key))
            # TODO: Add processing for pcomp
    return prep_dict


from PySide2.QtWidgets import QWidget, QPushButton, QVBoxLayout, QTabWidget
from PySide2.QtCore import SIGNAL, QObject

from silx.gui.widgets import PeriodicTable

class ElementTable(QWidget):
    """
            Class to handle elements on a spectrum
            Display of rays for each known elements
    """
    def __init__(self, master, window):
        super().__init__(window)
        self.master = master
        self.window = window
        self.set_ui()
        self.window.show()
        self.selected_elements = []

    def set_ui(self):
        """

        set GUI for element table

        :return:
        """
        layout = QVBoxLayout()
        b_pt = QPushButton('Periodic Table')
        QObject.connect(b_pt, SIGNAL('clicked()'), self._start_periodic_table)
        layout.addWidget(b_pt)
        b_pt.show()
        btn_quit = QPushButton('Quit')
        QObject.connect(btn_quit, SIGNAL('clicked()'), self.quit)
        layout.addWidget(btn_quit)
        btn_quit.show()
        self.window.setLayout(layout)

    def _start_periodic_table(self):
        """
        Display periodic table. Cf. doc silx
        :return:
        """
        self.w = QTabWidget()
        self.pt = PeriodicTable.PeriodicTable(self.w)
        self.pt.sigElementClicked.connect(self._my_slot)
        self.pc = PeriodicTable.PeriodicCombo(self.w)
        self.pl = PeriodicTable.PeriodicList(self.w)
        self.pl.sigSelectionChanged.connect(self.change_list)
        self.pc.sigSelectionChanged.connect(self.change_combo)

        comboContainer = QWidget(self.w)
        comboContainer.setLayout(QVBoxLayout())
        comboContainer.layout().addWidget(self.pc)

        self.w.addTab(self.pt, "PeriodicTable")
        self.w.addTab(self.pl, "PeriodicList")
        self.w.addTab(comboContainer, "PeriodicCombo")

        self.pt.show()
        self.pl.show()
        self.pc.show()

        self.w.show()

    def _my_slot(self, item):
        """

            function to toggle clicked element

            :param item:
            :return:
        """
        self.pt.elementToggle(item)
        tmp = list()
        for e in self.pt.getSelection():
            tmp.append(e.symbol)

        if len(tmp) > len(self.selected_elements):
            self.selected_elements = tmp
            self._plot_element()
        else:
            to_erase = list(set(self.selected_elements) - set(tmp))
            self._supress_plot_element(to_erase)
            self.selected_elements = tmp

    def _plot_element(self):
        """

            Plot all line in lineident file for each ray of the selected element

            :return:
        """
        e = self.selected_elements[-1]
        ordre = "ordre_{}".format(int(self.master.num_ordre.text()))
        x_lower = \
            self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["import"].delim["delim_data"][
                ordre][
                'lim_basse']
        x_upper = \
            self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["import"].delim["delim_data"][
                ordre][
                'lim_haute']
        ele_in_lims = []
        key_list = []

        for key in self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["import"].lineident_json[e]:
            if key != 'number':
                if x_lower < self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())][
                        "import"].lineident_json[e][key]["lambda"] < x_upper:

                    ele_in_lims.append(self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())][
                                           "import"].lineident_json[e][key]["lambda"])
                    key_list.append(key)

        y = [self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["canvas"].y_lim[0],
             self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["canvas"].y_lim[1]]
        for i in range(len(ele_in_lims)):
            x = [ele_in_lims[i], ele_in_lims[i]]
            self.master.dict_tabs["Tab_{}".format(
                self.master.tabs.currentIndex())]["ax"].plot(x, y, linewidth=2.0, linestyle='-.', label="{}".format(
                key_list[i]), color='b')
            self.master.dict_tabs["Tab_{}".format(
                self.master.tabs.currentIndex())]["ax"].text(ele_in_lims[i], self.master.dict_tabs["Tab_{}".format(
                self.master.tabs.currentIndex())]["canvas"].y_lim[0], "{}".format(e), color='b', fontsize=12)
            self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["fig"].canvas.draw()

    def _supress_plot_element(self, to_erase):

        """

            Supress all ploted line when user unselect an element in the table

            :param to_erase:
            :return:
        """

        k = 0
        l = len(self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].lines)
        for j in range(l):
            line = self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].lines[j - k]
            if line.get_label()[0:2].replace(" ", "") == to_erase[0]:
                self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].lines.remove(line)
                k += 1
        self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["fig"].canvas.draw()

        k = 0
        l = len(self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].texts)
        for j in range(l):
            txt = self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].texts[j - k]
            if txt.get_text() == to_erase[0]:
                self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["ax"].texts.remove(txt)
                k += 1
        self.master.dict_tabs["Tab_{}".format(self.master.tabs.currentIndex())]["fig"].canvas.draw()

    def change_list(self, items):
        print("New list selection:", [item.symbol for item in items])

    def change_combo(self, item):
        print("New combo selection:", item.symbol)

    def click_table(self, item):
        print("New table click: %s (%s)" % (item.name, item.subcategory))

    def quit(self):
        self.window.close()


'''
한 개의 회의실이 있는데 이를 사용하고자 하는 N개의 회의에 대하여 회의실 사용표를 만들려고 한다.
각 회의 I 에 대해 시작시간과 끝나는 시간이 주어져 있고, 각 회의가 겹치지 않게 하면서 회의실을 사용할 수 있는 회의의 최대 개수를 찾아보자.
단, 회의는 한번 시작하면 중간에 중단될 수 없으며 한 회의가 끝나는 것과 동시에 다음 회의가 시작될 수 있다.
회의의 시작시간과 끝나는 시간이 같을 수도 있다. 이 경우에는 시작하자마자 끝나는 것으로 생각하면 된다.
'''

import sys

n = int(sys.stdin.readline())

arr = []
cnt = 1

for _ in range(n):
    a, b = map(int, sys.stdin.readline().split())
    arr.append([a, b])

arr.sort(key=lambda x:(x[1], x[0]))

close_time = arr[0][1]

for i in range(1, len(arr)):
    if (arr[i][0] >= close_time):
         cnt += 1
         close_time = arr[i][1]

sys.stdout.write(str(cnt))


import sys
import torch as th
import torch.nn.functional as F
from torch import nn
from torch.autograd import Variable
import numpy as np
import math
import data_utility
import pickle
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import seaborn as sns
import pandas as pd
sys.setrecursionlimit(50000)
class FeatureExtractionModel(nn.Module):
    def __init__(self, text_emb, rela_emb, bilstm, W, cnn1, cnn2, cnn3):
        super(FeatureExtractionModel, self).__init__()
        self.word_embedding = text_emb
        self.rela_embedding = rela_emb
        # LSTM layer
        self.bilstm = bilstm
        # Attention
        self.W = W
        # CNN layer
        self.cnn_1 = cnn1
        self.cnn_2 = cnn2
        self.cnn_3 = cnn3
        self.activation = nn.ReLU()

    def forward(self, question, word_relation, rela_relation):
        question = th.transpose(question, 0, 1)
        rela_relation = th.transpose(rela_relation, 0, 1)
        word_relation = th.transpose(word_relation, 0, 1)

        question = self.word_embedding(question)
        #question = self.dropout(question)
        rela_relation = self.rela_embedding(rela_relation)
        #rela_relation = self.dropout(rela_relation)
        word_relation = self.word_embedding(word_relation)
        #word_relation = self.dropout(word_relation)
#        self.bilstm.flatten_parameters()
        question_out, _ = self.bilstm(question)
        question_out = question_out.permute(1,2,0)
        #question_out = self.dropout(question_out)
        word_relation_out, word_relation_hidden = self.bilstm(word_relation)
        rela_relation_out, _ = self.bilstm(rela_relation, word_relation_hidden)
        #word_relation_out = self.dropout(word_relation_out)
        #rela_relation_out = self.dropout(rela_relation_out)
        relation = th.cat([rela_relation_out, word_relation_out], 0)
        relation = relation.permute(1,0,2)
        #print('relation', relation)
        # attention layer
        energy = th.matmul(relation, self.W)
        #print('energy', energy)
        energy = th.matmul(energy, question_out)
        #print('energy', energy.shape)
        energy_tmp = energy.view(energy.shape[0], energy.shape[1]*energy.shape[2])
        alpha = F.softmax(energy_tmp, dim=-1)
        alpha = alpha.view(energy.shape[0], energy.shape[1], energy.shape[2])
        return alpha

def extract_relas(tuples):
    pos_relas = []
    neg_relas = []
    for t in tuples:
        if t[2] == 1:
            pos_relas.append('.'.join(t[1] + [t[0]]))
        elif t[2] == 0:
            neg_relas.append('.'.join(t[1] + [t[0]]))
        else:
            print('error'); exit()
    return pos_relas, neg_relas

def to_variable(l):
    v = Variable(th.from_numpy(np.array(l))).cuda()
    return v

import sys
model_path = sys.argv[1]
rela_tokenizer_path = sys.argv[2]
model = th.load(model_path)

layer_list = list(model.children())
print(layer_list)

text_emb = layer_list[1]
rela_emb = layer_list[2]
bilstm = layer_list[3]
cnn1 = layer_list[4]
cnn2 = layer_list[5]
cnn3 = layer_list[6]
W = model.W

feature_extractor = FeatureExtractionModel(text_emb, rela_emb, bilstm, W, cnn1, cnn2, cnn3)
feature_extractor = feature_extractor.eval()
with open('tokenizer/ques_tokneizer_300.pkl', 'rb') as f:
    ques_tokenizer = pickle.load(f)
with open(rela_tokenizer_path, 'rb') as f:
    rela_tokenizer = pickle.load(f)
datas = data_utility.load_test_data()
for index, (i, ques_text, step_list) in enumerate(datas):
    if index != 337:
        continue
    for rela_len in range(0, len(step_list)-1):
        try:
            print('\r', index, end='')
            pos_relas, neg_relas = extract_relas(step_list[rela_len])
            t_ques_text = ques_tokenizer.process([ques_text])
            t_pos_rela_text, t_pos_rela = rela_tokenizer.process(pos_relas)
            #print(t_pos_rela)
            #print(t_pos_rela_text)
            v_ques_text = to_variable(t_ques_text)
            v_pos_rela = to_variable(t_pos_rela)
            v_pos_rela_text = to_variable(t_pos_rela_text)
            #print(v_ques_text)
            #print(v_pos_rela_text)
            #print(v_pos_rela)
            A = feature_extractor(v_ques_text, v_pos_rela_text, v_pos_rela)
            #print(ques_text, pos_relas)
            print(A)
            A = A.cpu().detach().numpy()
            rela_texts = rela_tokenizer.split(pos_relas[0])
            relas = rela_tokenizer.split_rela(pos_relas[0])
            ques_tokens = ques_text.split()
            print(ques_tokens)
            print(rela_texts)
            print(relas)
            if len(ques_tokens) < 5:
                ques_tokens = ['<PAD>'] * (5 - len(ques_tokens)) + ques_tokens
            df = pd.DataFrame( A[0])
            #columns = ques_tokens,
            #                           index = rela_texts + relas)
            ax = sns.heatmap( data= df, linewidth=0.1, cmap='Reds', annot=False,
                    square=True, cbar=False)
            #ax.set_title('TITLE HERE')
            #ax.set_xlabel('x_label')
            #ax.set_ylabel('y_label')
            #ax.xaxis.tick_top()
            ax.xaxis.set_ticks_position('top')

            # labels rotation
            #ax.set_xticklabels(ax.get_xticklabels(), rotation=0)
            #ax.set_yticklabels(ax.get_yticklabels(), rotation=0)

            plt.savefig(f'./{index}_{rela_len}.jpg', dpi=100)
        except ValueError:
            continue


#!/usr/bin/python3

import atexit
import re
import subprocess
import time
from typing import List

import log

_DEGREE_SIGN = "\N{DEGREE SIGN}"
DEGREE_SYMBOL = f"{_DEGREE_SIGN}C"

IPMI_INLET_SENSORNAME = "Inlet Temp"

class Sensor:
    def __init__(self, command: str, pattern: str) -> None:
        self.command = command
        self.pattern = pattern
        self.temps: List[float] = []
        self.last_sensor_reading = time.time()

    def set_last_sensor_reading(self) -> None:
        """Update time of last sensor reading."""
        self.last_sensor_reading = time.time()

    def clear_temps(self) -> None:
        """Clear stored tempartures."""
        self.temps = []

    def fetch_temps(self) -> List[float]:
        """Get sensor data and parse results.

        Returns:
            List[float]: Parsed tempatures
        """
        process = subprocess.run(
            self.command, capture_output=True, shell=True, text=True
        )
        matches = re.findall(self.pattern, process.stdout)
        self.temps = [float(match) for match in matches]
        self.set_last_sensor_reading()
        return self.temps

    def get_average_temp(self) -> float:
        """Calculates the average temparture.

        Returns:
            float: Average of input values
        """
        _divisor = 0
        _total = 0
        average = 0

        for temp in self.temps:
            if temp:
                _total += temp
                _divisor += 1

        if _divisor > 0:
            average = _total / _divisor

        return average

    def get_max_temp(self) -> float:
        """Get max temparture for a given sensor.

        Returns:
            float: Max temparture in degress celcius
        """
        if self.temps:
            return max(self.temps)
        return 0

class FanController:
    def __init__(self) -> None:
        self.static_speed_low: int = 0x02
        self.static_speed_high: int = 0x12
        self.default_threshold: int = 32
        self.base_temp: int = 30
        self.desired_temp_1: int = 40
        self.desired_temp_2: int = 45
        self.desired_temp_3: int = 55
        self.demand_1: int = 5
        self.demand_2: int = 40
        self.demand_3: int = 200
        self.hysteresis: int = 2
        self.current_mode: str = ""
        self.last_fan_setting: int = 0

    def set_fans_default(self):
        if self.current_mode != "default":
            log.info("::: Enable Dynamic Fan Control :::")
            for attempt in range(10):
                process = subprocess.run(
                    "ipmitool raw 0x30 0x30 0x01 0x01",
                    capture_output=True,
                    shell=True,
                    text=True,
                )
                if process.returncode == 0:
                    log.info("::: Enable Dynamic Fan Control Successful :::")
                    self.current_mode = "default"
                    self.last_fan_setting = 0
                    return True
                time.sleep(1)
                log.warning("::: Retrying Dynamic Fan Control :::")
                log.warning(f"Attempt: {attempt}")
            log.error("::: Retries of Dynamic Fan Control All Failed :::")
            return False
        return True

    def set_fans_servo(
        self,
        ambient_sensor: Sensor,
        cpu_sensor: Sensor,
        core_sensor: Sensor,
        hdd_sensor: Sensor,
    ):
        temps = [
            core_sensor.get_average_temp(),
            cpu_sensor.get_average_temp(),
            hdd_sensor.get_average_temp(),
        ]
        average_temp = sum(temps) / len(temps)
        weighted_average = max(average_temp, hdd_sensor.get_average_temp())

        log.info(
            f"Ambient temparture: {ambient_sensor.get_average_temp()}{DEGREE_SYMBOL}"
        )
        log.info(f"Weighted average temparture: {weighted_average}{DEGREE_SYMBOL}")

        if not weighted_average or weighted_average == 0:
            log.error(
                "::: ERROR READING ALL TEMPARTURES! FALLING BACK TO IDRAC CONTROL :::"
            )
            self.set_fans_default()

        if self.current_mode != "set":
            log.info("::: Disable Dynamic Fan Control :::")
            process = subprocess.run(
                "ipmitool raw 0x30 0x30 0x01 0x00",
                capture_output=True,
                shell=True,
                text=True,
            )
            if process.returncode == 0:
                log.info("::: Disable Dynamic Fan Control Successful :::")
                self.current_mode = "set"
            else:
                log.error("::: Disable Dynamic Fan Control Failed :::")

        demand = 0
        if weighted_average > self.base_temp and weighted_average < self.desired_temp_1:
            demand = 0 + (weighted_average - self.base_temp) * (self.demand_1 - 0) / (
                self.desired_temp_1 - self.base_temp
            )
        elif weighted_average >= self.desired_temp_2:
            demand = self.demand_2 + (weighted_average - self.desired_temp_2) * (
                self.demand_3 - self.demand_2
            ) / (self.desired_temp_3 - self.desired_temp_2)
        elif weighted_average >= self.desired_temp_1:
            demand = self.demand_1 + (weighted_average - self.desired_temp_1) * (
                self.demand_2 - self.demand_1
            ) / (self.desired_temp_2 - self.desired_temp_1)

        demand = int(
            self.static_speed_low
            + demand / 100 * (self.static_speed_high - self.static_speed_low)
        )
        if demand > 255:
            demand = 255

        log.info(f"Demand: {demand}")
        log.info(f"Fan setting: {self.last_fan_setting}")

        #   # ramp down the fans quickly upon lack of demand, don't ramp them up
        #   # to tiny spikes of 1 fan unit.  FIXME: But should implement long
        #   # term smoothing of +/- 1 fan unit
        if (
            not self.last_fan_setting
            or demand < self.last_fan_setting
            or demand > self.last_fan_setting + self.hysteresis
        ):
            log.info(f"::: ipmitool raw 0x30 0x30 0x02 0xff {demand:#x} :::")
            process = subprocess.run(
                f"ipmitool raw 0x30 0x30 0x02 0xff {demand:#x}",
                capture_output=True,
                shell=True,
                text=True,
            )
            if process.returncode == 0:
                log.info("::: Set Fan Speed Successful :::")
                self.current_mode = "set"
                self.last_fan_setting = demand
                return True
        log.info("No changes were made")
        return False

def main() -> None:
    """Main service loop."""
    while True:
        if not ambient.temps:
            ambient.fetch_temps()
        if not hdd.temps:
            hdd.fetch_temps()

        core.fetch_temps()
        cpu.fetch_temps()

        log.info(f"Ambient Temparture: {ambient.temps}")
        log.info(f"Cores Temparture: {core.temps}")
        log.info(f"CPUs Temparture: {cpu.temps}")
        log.info(f"HDDs Temparture: {hdd.temps}")

        if ambient.get_average_temp() > fc.default_threshold:
            log.info(
                f"::: Falling back because of high ambient temparture; {ambient.get_average_temp()} > {fc.default_threshold}"
            )
            if not fc.set_fans_default():
                continue
        else:
            if not fc.set_fans_servo(
                ambient_sensor=ambient, cpu_sensor=cpu, core_sensor=core, hdd_sensor=hdd
            ):
                continue

        if (time.time() - ambient.last_sensor_reading) > 60:
            ambient.temps = []
            ambient.set_last_sensor_reading()
            fc.current_mode = "reset"

        if (time.time() - hdd.last_sensor_reading) > 1200:
            hdd.temps = []
            hdd.set_last_sensor_reading()

        time.sleep(3)

@atexit.register
def end_application() -> None:
    log.info("::: Resetting Fans Back to Default :::")
    fc.set_fans_default()
    log.info("::: GOODBYE :::")

if __name__ == "__main__":
    log.init()
    ambient = Sensor(
        command="timeout -k 1 20 ipmitool sdr type temperature | awk '/Inlet Temp/'",
        pattern=r".*\| ([^ ]+) degrees C.*",
    )
    core = Sensor(
        command="timeout -k 1 20 sensors | awk '/Core/'",
        pattern=r".*:\s+\+([^ ]+).C.*",
    )
    cpu = Sensor(
        command="timeout -k 1 20 sensors | awk '/Package id/'",
        pattern=r".*:\s+\+([^ ]+).C.*",
    )
    hdd = Sensor(
        command="hddtemp /dev/sd? | awk '!/255/'",
        pattern=r"/dev/sd[a-z]:\s[a-zA-Z0-9-]*\s[a-zA-Z0-9-]*:\s([^ ]+).C",
    )
    fc = FanController()
    main()


from pptx import Presentation
import textwrap

def shorten_text(text, max_length):
    """
    Shorten the text to the max_length, adding an ellipsis if the text is shortened.
    """
    return textwrap.shorten(text, width=max_length, placeholder="...")

def process_slide(slide, max_length):
    """
    Process each slide, shortening the text in each text box if it's too long.
    """
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        text_frame = shape.text_frame
        for paragraph in text_frame.paragraphs:
            if len(paragraph.text) > max_length:
                paragraph.text = shorten_text(paragraph.text, max_length)

def process_presentation(input_file, output_file, max_length):
    """
    Process each slide in the presentation.
    """
    ppt = Presentation(input_file)
    for slide in ppt.slides:
        process_slide(slide, max_length)
    ppt.save(output_file)

# Example usage
input_file = 'path/to/input/presentation.pptx'
output_file = 'path/to/output/presentation.pptx'
max_length = 50  # Maximum length of text in characters
process_presentation(input_file, output_file, max_length)


import json
import os
import openai
from flask import Flask, request, jsonify, render_template

app = Flask(__name__)
promptdefault = "detailed image"

@app.route('/')
def home():
    return "Hello, Flask!"

@app.route('/prompt', methods =["POST", "GET"])
def prompting():
    if request.method == "POST":
       story_prompt = request.form.get("sprompt")
       # getting input with name = name in HTML form
       character_name = request.form.get("cname")
       global promptdefault
       promptdefault = story_prompt
       img_url = generate_image()
       return render_template("testing.html", image_url=img_url)
    return render_template("prompting.html")

@app.route('/testing', methods=['POST', 'GET'])
def loading():
    return render_template('testing.html')

@app.route('/generate-image', methods=['POST', 'GET'])
def generate_image():
    openai.api_key = os.getenv("DALLE_API_KEY")
    global promptdefault

    if not openai.api_key:
        return jsonify({'error': 'API key not found'}), 403

    try:
        # Call the OpenAI API to generate an image
        response = openai.Image.create(
            prompt=promptdefault,
            n=1,
            size="1024x1024"
        )

        # Extract the image URL(s) from the response
        # Adjust the following line according to the actual response structure
        image_urls = [data['url'] for data in response['data']]
        # Send back the image URLs in the response
        #jsonify({'images': image_urls})
        return image_urls[0]
        #return render_template("testing.html", image_url=website_url)

    except openai.error.OpenAIError as e:
        # Handle OpenAI specific errors
        return jsonify({'error': str(e)}), 500
    except Exception as e:
        # Handle other possible errors
        #return jsonify({'error': 'An error occurred while generating the image'}), 500
        return render_template('prompting.html', image_url=website_url)

if __name__ == '__main__':
    app.run(debug=True)


from dependency_injector.wiring import Provide, inject
from fastapi import APIRouter, Depends
from pydantic import BaseModel

from app.container import AppContainer
from app.core.jwt import get_current_user

from .usecases import (
    CreateThreadUsecase,
    GetThreadMembers,
    GetThreadUsecase,
    GetUserThreads,
    RemoveMemberFromThreadUsecase,
)

Container = AppContainer.thread
router = APIRouter(prefix="/threads", tags=["threads"])

@router.get("")
@inject
def get_all_threads(
    user_id: int = Depends(get_current_user),
    usecase: GetUserThreads = Depends(Provide[Container.get_user_threads]),
):
    threads = usecase.get_user_threads(user_id)
    return {"data": threads}

class CreateThreadRequest(BaseModel):
    name: str

@router.post("")
@inject
def create_thread(
    req: CreateThreadRequest,
    user_id: int = Depends(get_current_user),
    usecase: CreateThreadUsecase = Depends(Provide[Container.create_thread]),
):
    thread = usecase.create_thread(req.name, user_id)
    return {"data": thread}

@router.get("/{thread_id}")
@inject
def get_thread(
    thread_id: int,
    user_id: int = Depends(get_current_user),
    usecase: GetThreadUsecase = Depends(Provide[Container.get_thread]),
):
    thread = usecase.get_thread_for_user(thread_id, user_id)
    return {"data": thread}

@router.get("/{thread_id}/members")
@inject
def get_thread_members(
    thread_id: int,
    user_id: int = Depends(get_current_user),
    usecase: GetThreadMembers = Depends(Provide[Container.get_thread_members]),
):
    thread_members = usecase.get_thread_members_for_user(thread_id, user_id)
    return {"data": thread_members}

class RemoveMemberRequest(BaseModel):
    user_id: int

@router.delete("/{thread_id}/members")
@inject
def remove_member_from_thread(
    thread_id: int,
    req: RemoveMemberRequest,
    user_id: int = Depends(get_current_user),
    usecase: RemoveMemberFromThreadUsecase = Depends(
        Provide[Container.remove_member_from_thread]
    ),
):
    usecase.remove_member(thread_id, user_id, req.user_id)
    return {"data": {"success": True}}


from typing import List

def checkPossibility(nums: List[int]) -> bool:
	# if len(nums) < 1:
	# 	return True
	# count = 0
	# for i in range(1, len(nums)):
	# 	if count >= 2:
	# 		return False
	# 	if nums[i - 1] < nums[i]:
	# 		continue;
	# 	else:
	# 		count += 1
	# if count < 2:
	# 	return True
	# else:
	# 	return False
	check = False
	for i in range(0, len(nums) - 1):
		if (nums[i] > nums[i + 1]) and check == True:
			return False
		elif nums[i] > nums[i + 1]:
			if i!=0 and nums[i - 1] > nums[i + 1]:
				nums[i + 1] = nums[i]
			check = True
	return True

li = [4,2,3]
li2 = [3,4,2,3]
print(checkPossibility(li))
print(checkPossibility(li2))


import hashlib
import os
import json

def check_sum():
    git_version = input('Git Version: ')
    svn_version = input('SVN Version: ')
    path = input('Folder: ')
    file_json = input('Output JSON Name: ')

    while True:
        check_del = input(f'Deleted Start Path "{path}/"? [y/N]: ')

        if check_del.lower() == 'y' or check_del.lower() == 'n' or check_del.lower() == 'yes' or check_del.lower() == 'no':
            break

    md5_list = {
        "files":[],
        "svn_version": svn_version,
        "git_version": git_version
    }

    print(f'Start Make JSON: "{file_json}.json"')

    for rootdir, dirs, files in os.walk(path):
        for file in files:
            # print (os.path.join(rootdir, file))

            with open(os.path.join(rootdir, file), 'rb') as get_file:
                byte_size = get_file.read()

                get_file.close()

                if check_del.lower() == 'y' or check_del.lower() == 'yes':
                    md5_list['files'].append({
                        "size":len(byte_size),
                        "name":str(os.path.join(rootdir, file)).replace('\\', '/').lstrip(f'{path}/'),
                        "md5":str(hashlib.md5(byte_size).hexdigest())
                    })

                else:
                    md5_list['files'].append({
                        "size":len(byte_size),
                        "name":str(os.path.join(rootdir, file)).replace('\\', '/'),
                        "md5":str(hashlib.md5(byte_size).hexdigest())
                    })

        with open(file_json+'.json', 'w') as json_file:
            json_file.write(json.dumps(md5_list))
            json_file.close()

if __name__ == '__main__':
    check_sum()
    print('Done!')


import base64

def pic2py(picture_name):
    open_pic = open("%s" % picture_name, 'rb')
    b64str = base64.b64encode(open_pic.read())
    open_pic.close()

    # 注意这边b64str一定要加上.decode()
    write_data = 'img = "%s"' % b64str.decode()
    f = open('%s.py' % picture_name.replace('.', '_'), 'w+')
    f.write(write_data)
    f.close()

if __name__ == '__main__':
    name = input(r"C:\Users\nimendafeige\PycharmProjects\untitled\1.gif")
    pic2py(name)


lang = open('lang.txt', 'r').read().strip()
import re
code = ''
if lang == 'python3' or lang == 'pypy':
    pattern = re.compile(r"(while|for|if|switch\s*\()")
    if pattern.search(code):
        print("Error: The code contains a while/for/if/switch statement.")
        exit(171)

    #check user used abs and min and max functions
    pattern = re.compile(r"(abs|min|max|eval|exec)\s*\(")

    # check user used ** operator
    if '**' in code:
        print('Error: ** operator is not allowed')
        exit(171)

    # check user used lambda
    pattern = re.compile(r"\\blambda\\b")
    if pattern.search(code):
        print('Error: Lambda functions are not allowed')
        exit(171)

    # check user used def and class
    pattern = re.compile(r"\\b(def|class)\\b")
    if pattern.search(code):
        print('Error: Defining functions and classes are not allowed')
        exit(171)

    # check user used yield and return
    pattern = re.compile(r"\\b(yield|return)\\b")
    if pattern.search(code):
        print('Error: Returning and yielding are not allowed')
        exit(171)
    pattern = re.compile("(\\[|\\]|\\{|\\})")
    if pattern.search(code):
        print('Error: Arrays and dictionaries are not allowed')
        exit(171)

if lang == 'cpp':
    with open('code.txt', 'r') as f:
        code = f.read()

    pattern = re.compile(r"(while|if|for|switch)\s*\(")
    if pattern.search(code):
        print('Error: Loops and conditions are not allowed')
        exit(171)

    pattern = re.compile("#include\\s+<(?!iostream)[^>]+>")
    if pattern.search(code):
        print('Error: The code includes a library other than iostream.')
        exit(171)

    #check user used abs, min, max
    pattern = re.compile(r"(abs|min|max|fabs)\s*\(")
    if pattern.search(code):
        print('Error: Restricted built-in function used')
        exit(171)

    # check user used goto
    pattern = re.compile(r"\\bgoto\\b")
    if pattern.search(code):
        print('Error: goto is not allowed')
        exit(171)

    # check user used compare operators
    pattern = re.compile(r"\\b(==|!=|<=|>=|<|>)\\b")
    if pattern.search(code):
        print('Error: Comparing operators are not allowed')
        exit(171)

# check user used array and dictionary
pattern = re.compile("(\\[|\\]|\\{|\\})")
if pattern.search(code):
    print('Error: Arrays and dictionaries are not allowed')
    exit(171)

# check user used bitwise operators
pattern = re.compile("(&|\\||\\^|~)")
if pattern.search(code):
    print('Error: Bitwise operators are not allowed')
    exit(171)

# check user used logical operators
pattern = re.compile("(\\band\\b|\\bor\\b|\\bnot\\b)")
if pattern.search(code):
    print('Error: Logical operators are not allowed')
    exit(171)

# check user used && and ||
pattern = re.compile("(\\|\\||&&)")
if pattern.search(code):
    print('Error: Logical operators are not allowed')
    exit(171)


"""
Van Krevelen图是在石油化工领域应用广泛的图表，用来描述碳和氢之间的相对含量关系。
"""

import numpy as np
import matplotlib.pyplot as plt

# 生成数据
carbon = np.arange(0, 1.01, 0.01)  # 碳含量
hydrogen = np.arange(0, 1.01, 0.01)  # 氢含量
oxygen = np.arange(0, 1.01, 0.01)  # 氧含量
plt.xlabel('C/H')
plt.ylabel('O/H')

# # 绘制Van Krevelen图
# for x in range(len(oxygen)):
#     plt.plot(carbon/hydrogen, oxygen[x]/hydrogen, color='black', linewidth=0.5)
#
# for y in range(len(carbon)):
#     plt.plot(carbon[y]/hydrogen, oxygen/hydrogen, color='black', linewidth=0.5)

# 绘制Van Krevelen图
X, Y = np.meshgrid(carbon/hydrogen, oxygen/hydrogen)
plt.plot(X.flatten(), Y.flatten(), color='black', linewidth=0.5)

# 在图表上绘制示例数据
example_data = [(0.2, 0.1, 0.7), (0.3, 0.2, 0.5), (0.1, 0.25, 0.65), (0.15, 0.35, 0.5), (0.35, 0.3, 0.35)]
colors = ['r', 'g', 'b', 'c', 'm']
for i in range(len(example_data)):
    plt.scatter(example_data[i][0]/example_data[i][1], example_data[i][2]/example_data[i][1], color=colors[i], s=50)

# 显示图表
plt.xlabel('C/H')
plt.ylabel('O/H')
plt.show()


from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_cache import Cache
from flask_migrate import Migrate
from  flask_moment import Moment
from flask_uploads import UploadSet,configure_uploads,patch_request_class,IMAGES

bootstrap = Bootstrap()
moment = Moment()
db = SQLAlchemy()
migrate = Migrate(db=db)
login_manager = LoginManager()
cache = Cache(config={'CACHE_TYPE':'redis'})

file = UploadSet('photos',IMAGES)

def ext_init(app):
    bootstrap.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    migrate.init_app(app)
    cache.init_app(app)
    configure_uploads(app, file)
    patch_request_class(app, None)

    login_manager.init_app(app)
    login_manager.login_view = 'user.login'
    login_manager.login_message = '先登录再访问'
    login_manager.session_protection = 'strong'


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

## Code used to obtain search results from the Bing web search API ##
## Last edited by Bronwen Hunter on 05/01/2021 ##

import requests
import pandas as pd
import json
import math
import time
from datetime import datetime
import argparse

class bing_wrapper():

    def __init__(self,
                 queryfile, # File where list of queries is stored
                 api_key,   # API key used to access Bing services
                 directory, # Directory where output files will be stored
                 numberofresults=300,
                 pagesize=100,
                 language='en'):

        df = pd.read_csv(queryfile)
        self.querylist = list(df['queries'])
        self.api_key = api_key
        self.directory = directory
        self.numberofresults = numberofresults
        self.pagesize = pagesize
        self.language = language

    def bing_search(self):

        ''' Function to retrieve all results'''

        search_url = "https://api.bing.microsoft.com/v7.0/search"
        resultsdict = {}
        for query in self.querylist:
            numberofpages = math.ceil(self.numberofresults/self.pagesize)
            urllist = []
            namelist = []
            for i in range(0,numberofpages):
                headers = {"Ocp-Apim-Subscription-Key":self.api_key}
                params = {"q":query,
                          "textDecorations":False,
                          "textFormat":"HTML",
                          "count":self.pagesize, # Returns a maximum of 100 results per page
                          "offset":i*self.pagesize, # Allows for pagination
                          "safeSearch":"strict",
                          "setLang":self.language }
                response = requests.get(search_url, headers=headers, params=params)
                search_results = response.json()
                print("Retrieving page {}/{} for query:{}".format(i+1, numberofpages, query))
                results = search_results.get('webPages', {}).get('value', {})
                urllist = urllist + [results[j]['url'] for j in enumerate(results)]
                namelist = namelist + [results[j]['name'] for j in enumerate(results)]
                time.sleep(5)

            resultsdf = pd.DataFrame() # Creating a dataframe to stored the results
            resultsdf['external_url']=urllist
            resultsdf['title'] = namelist
            resultsdf['query'] = [query for item in urllist]
            resultsdf['source'] = ['Bing' for item in urllist]
            resultsdict[query] = resultsdf
        allresultssdf = bing_wrapper.deduplicated_save(self, resultsdict)
        return allresultssdf

    def deduplicated_save(self, resultsdict):

        '''Function to deduplicate and save bing search results as a CSV file'''

        fullresults = pd.concat(resultsdict.values(), ignore_index=True)
        deduplicated = fullresults.drop_duplicates(subset = 'external_url')   # Deduplicating by result URL
        deduplicated['id'] = [('B'+str(i)) for i in range(len(deduplicated))] # Adding an ID to each result
        now = datetime.now()
        dt_string = now.strftime("%d-%m-%Y")                                  # Adding the date the search was conducted to the filename
        deduplicated.to_csv(self.directory+"bingsearch_allqueries_{}.csv".format(dt_string))
        print("Documents are written to the {folder}".format(folder=self.directory))
        return deduplicated

def main():

    """The main function defines the interface with the users."""

    parser = argparse.ArgumentParser()
    parser.add_argument('--queryfile', default=None, required=True, type=str, help='Filepath where queries are stored')
    parser.add_argument('--api_key', default=None, required=True, type=str, help='API key for bing endpoint')
    parser.add_argument('--directory', default=None, required=True, type=str, help='Directory where output will be saved')
    parser.add_argument('--numberofresults', default=300, required=False, type=int, help='Number of results to return')
    parser.add_argument('--pagesize', default=100, required=False, type=int, help='Number of results returned per page')
    parser.add_argument('--lang', default = 'en', required=False, type=str, help='Language of the search results' )

    args = parser.parse_args()
    print(args)

    inst = bing_wrapper(args.queryfile,
                       args.api_key,
                       args.directory,
                       args.numberofresults,
                       args.pagesize,
                       args.lang)

    inst.bing_search()

if __name__ == "__main__":
    main()


"""
Problem: https://www.hackerrank.com/challenges/list-comprehensions/problem
Max Score: 10
Difficulty: Easy
Author: Ric
Date: Nov 13, 2019
"""

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    # arr = []
    # for i in range (x + 1):
    #     for j in range (y + 1):
    #         for k in range (z + 1):
    #             if i+j+k != n:
    #                 arr.append([i,j,k])
    # print(arr)
    print([ [i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n ])

Python/02. Basic Data Types/01. List_Comprehensions.py


import socket
import socks
from htun.args import args

def create_socket():
    if args.proxy:
        server_socket = socks.socksocket()
        proto, rest = args.proxy.split('://')
        host, port = rest.split(':')
        port = int(port)
        if proto == "socks4":
            server_socket.set_proxy(socks.SOCKS4, host, port)
        elif proto == "socks5":
            server_socket.set_proxy(socks.SOCKS5, host, port)
        else:
            print("Invalid protocol: %s (must be one of socks4 or socks5)" %
                  proto)
    else:
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    server_socket.connect((args.uri["peer_ip"], args.uri["port"]))

    return server_socket

server_socket = create_socket()


from fastapi import FastAPI, Request, Response
import uvicorn
from db.session import engine
from db.base import Base
from core.config import Settings
from apis.base import api_router

def create_tables():  # new
    Base.metadata.create_all(bind=engine)

def create_router(app):
    app.include_router(api_router)

def start_application():
    app = FastAPI(title=Settings.PROJECT_TITLE, version=Settings.PROJECT_VERSION,
                  description=Settings.PROJECT_DESCRIPTION)
    create_tables()
    create_router(app)
    return app

app = start_application()

async def catch_exceptions_middleware(request: Request, call_next):
    try:
        return await call_next(request)
    except Exception:
        return Response("Internal server error", status_code=500)

app.middleware('http')(catch_exceptions_middleware)

@app.get("/")
def read_root():
    return {"Hello": "World"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)


from preprocess import *

#create dataset
x_train, x_validate, x_test, y_train, y_validate, y_test, z_train, z_validate, z_test = create_dataset(300, 0.2,0.25)

# get mean euclidean distance
def mean_euclidean_distance(gt,prediction):
    store=[]
    error=gt-prediction
    for i in range(z_test.shape[0]):
      distance=np.sqrt(np.sum(np.square(error[i])))
      store.append(distance)
    s=np.array(store)
    return np.mean(s)

def heatmap_evaluation(result,z_test):
    result=np.squeeze(result, axis=(3,))
    predict=[]
    for i in range(result.shape[0]):
        landmarks=[int(np.where(result[i,:,:]==np.max(result[i,:,:]))[1]),int(np.where(result[i,:,:]==np.max(result[i,:,:]))[0])]
        predict.append(landmarks)
    prediction=np.array(predict)
    r=mean_euclidean_distance(z_test,prediction)
    print("overall mean distance error for FCN is",str(r))

def cnn_evaluation(result,z_test):
    error=result-z_test
    distance=[]
    for i in range(error.shape[0]):
        ed=np.sqrt(error[i,0]**2+error[i,1]**2)
        distance.append(ed)
    distance=np.array(distance)
    print("overall mean distance error for CNN is",np.mean(distance))


import os

################################################################################
# Setup
################################################################################

AddOption('--for_debug', dest='debug', action='store_true', default=False, help="build with debug symbols")
#AddOption('--with-assertions', dest='assertions', action='store_true', default=False, help="build with debug symbols")
#AddOption('--non-optimized', dest='optimize', action='store_false', default=True, help="create non optimized build")
AddOption('--prefix', dest='prefix', action='store', default='#/install', help="install location")
AddOption('--run', dest='run', action='store', default='', help="run programs")

env = Environment(tools = ['default', 'clang', 'clangxx'])
if not 'HOMEBREW_PREFIX' in os.environ:
    print("Error: HOMEBREW_PREFIX needs to point to your Homebrew prefix. Typically '/usr/local' or '/opt/homebrew'")
    Exit(1)
homebrew_prefix = os.environ['HOMEBREW_PREFIX']
llvm = os.path.join(homebrew_prefix, 'opt/llvm')
libev = os.path.join(homebrew_prefix, 'opt/libev')
libressl = os.path.join(homebrew_prefix, 'opt/libressl')

env.PrependENVPath('PATH', os.path.join(llvm, 'bin'))
if not 'WASI_SYSROOT' in os.environ:
    print("Error: WASI_SYSROOT needs to point to a precompiled wasi sysroot")
    Exit(1)
env['WASI_SYSROOT'] = os.environ['WASI_SYSROOT']

env.Append(CXXFLAGS = ['-std=c++2a', '-fcoroutines-ts',
                       '-Wall', '-Wextra', '-Wc++2a-compat', '-Werror'],
           LIBPATH = [os.path.join(libev, 'lib'), os.path.join(libressl, 'lib')],
           CPPPATH = [os.path.join(libev, 'include'), os.path.join(libressl, 'include')])

if GetOption('debug'):
    env.Append(CXXFLAGS = ['-g'])
else:
    env.Append(CXXFLAGS = ['-O3'])
    env.Append(CXXFLAGS = ['-DNDEBUG'])

env['PREFIX'] = GetOption('prefix')
env.Alias('install', env['PREFIX'])

################################################################################
# Methods
################################################################################

def RunProgram(self, name, dependencies, program, args):
    if GetOption('run') == name:
        run = self.Command(target = 'output.log',
                           source = [program[0].abspath],
                           action = '$SOURCE %s | tee $TARGET' % (' '.join(args)))
        Depends(run, dependencies)
        self.AlwaysBuild(run)
        self.Alias('run', run)
        Default('run')
AddMethod(Environment, RunProgram)

def PrepareWasm(self):
    self['OBJSUFFIX'] = '.wo'
    self.Append(CXXFLAGS = ['--target=wasm32-wasi',
                            '--sysroot=%s' % (env['WASI_SYSROOT']),
                            '-fvisibility=hidden',
                            '-fno-exceptions'],
                LINKFLAGS = ['--target=wasm32-wasi',
                             '--sysroot=%s' % (env['WASI_SYSROOT']),
                             '-nostartfiles',
                             '-Wl,--export-dynamic',
                             '-Wl,--import-memory',
                             '-Wl,--no-entry'])
AddMethod(Environment, PrepareWasm)
def WasmObjects(self, sources):
    wasm_env = self.Clone()
    wasm_env.PrepareWasm()
    return wasm_env.Object(sources)
AddMethod(Environment, WasmObjects)
def Wasm(self, target, sources):
    wasm_env = self.Clone()
    wasm_env.PrepareWasm()
    return wasm_env.Program(target, sources)
AddMethod(Environment, Wasm)

################################################################################
# SConscripts
################################################################################

Export('env')

SConscript('dep/SConscript', variant_dir='build_dep', duplicate=0)
SConscript('src/SConscript', variant_dir='build', duplicate=0)


import pandas as pd
import pprint as pp
import numpy as np

def numeric_to_categorical(df, variables):
    '''
    @summary: converts numeric variables in a dataframe to categorical type (as str).
    '''
    for var in variables:
        df[var] = df[var].astype('str')

    return df

def dummify(df, cate_variables, inplace = False):
    '''
    @Summary: convert the categorical variables to numeric variables by using dummies (binary).
    Old categorical variables will be dropped.
    @param df: target dataframe
    @param cate_variables: target variables to be converted to binary dummies
    @param inplace: indicate if operation should be performed on original dataframe or a copy of dataframe.
    Original dataframe is used when inplace = True, otherwise, a copy of the dataframe.
    @return: A dataframe with new converted numeric variables.
    '''
    # make a copy before creating dummies
    if(inplace):
        df_new = df.copy()
    else:
        df_new = df

    # convert text-based columns to dummies (except v22)
    for var_name in cate_variables:
        dummies = pd.get_dummies(df[var_name], prefix=var_name)

        # Drop the current variable, concat/append the dummy dataframe to the dataframe.
        df_new = pd.concat([df_new.drop(var_name, 1), dummies.iloc[:,1:]], axis = 1)

    return df_new

def get_corr(df, corr_threshold):
    '''
    @summary: Get variable pairs with their correlation based on the passed threshold.
    @param df: target dataframe
    @param corr_threshold: cut-off correlation (abs(corrleation) > corr_threshold will be returned)
    @
    '''

    corr_var = df.corr()
    high_correlation = []

    for index, row in corr_var.iterrows():
        row_index_num = corr_var.index.get_loc(index)

        var1_na_count = sum(df.iloc[:,row_index_num].isnull())

        for colname in corr_var.columns.values:
            col_index = corr_var.columns.get_loc(colname)
            if(col_index < row_index_num and abs(row[colname]) > corr_threshold and row.name != colname):
            #if(col_index < row_index_num and row[colname] > correlation_threshold and row.name != colname):
                var2_na_count = sum(df[colname].isnull())
                high_correlation.append({'_var1': row.name, '_var2': colname, \
                                         '_var_corr': round(row[colname],3), \
                                         'var1_na': var1_na_count, 'var2_na': var2_na_count})

    return high_correlation

def get_feature_importance(df, model):
    feature_imprtance = zip(df.columns[1:], model.feature_importances_)
    dtype = [('feature', 'S10'), ('importance', 'float')]
    feature_imprtance = np.array(feature_imprtance, dtype = dtype)
    feature_sort = np.sort(feature_imprtance, order='importance')[::-1]
    return feature_sort


def solution(numbers):
    n = len(numbers)
    stack = []
    answer = [-1] * n

    for i in range(n) :
        while stack and numbers[stack[-1]] < numbers[i] :
            answer[stack.pop()] = numbers[i]
        stack.append(i)

    return answer

# 100만개 => 이중 포문으로 돌리면 반드시 시간초과
# 그렇다면 우선 순위 큐 ? => 인덱스를 저장할 방법이 없다....
# 그냥 스택 + 인덱스를 활용해서 해야함!
# 스택에 아무 것도 없거나, 현재 인덱스보다 작은 인덱스 중 작은 수가 없다면 그냥 스택에 추가
# 나보다 작은 수를 가진 인덱스가 있다면 답에 내 수를 적어주고, 스택에서 팝

프로그래머스/Lv.2/[다시]뒤에 있는 큰 수 찾기.Py

[다시]뒤에 있는 큰 수 찾기.Py


import json
import shlex
import sys
from pprint import pprint
from typing import Any, Callable

from slack import WebClient as SlackClient

from exoskelton.plugin_loader import PluginLoader
from exoskelton.slack_argparser import SlackArgumentParserException
from exoskelton.slack_event_context import SlackEventContextFactory

class SlackDummyClient:
    mock_method = ["chat_postMessage", "chat_postEphemeral"]

    def __init__(self, *args: Any, **kwarg: Any):
        self.slack_client = SlackClient(*args, **kwarg)

    def __getattr__(self, key: str) -> Callable[..., str]:
        if key in self.mock_method:

            def dummy_function(*args: Any, **kwarg: Any) -> str:
                print(key)
                pprint(args)
                pprint(kwarg)
                return json.loads(
                    """
                    {
                        "ok": true,
                        "channel": "C1H9RESGL",
                        "ts": "1503435956.000247",
                        "message": {
                            "text": "Here's a message for you",
                            "username": "ecto1",
                            "bot_id": "B19LU7CSY",
                            "attachments": [
                                {
                                    "text": "This is an attachment",
                                    "id": 1,
                                    "fallback": "This is an attachment's fallback"
                                }
                            ],
                            "type": "message",
                            "subtype": "bot_message",
                            "ts": "1503435956.000247"
                        }
                    }"""
                )

            return dummy_function
        else:
            return getattr(self.slack_client, key)

with open("slack_credential.json") as f:
    slack_tokens = json.load(f)

slack_bot_token = slack_tokens["slack_bot_token"]
slack_dummy_client = SlackDummyClient(slack_bot_token)

pl = PluginLoader(slack_dummy_client, "exoskelton")
parser = pl.create_parser()
context_factory = SlackEventContextFactory(slack_dummy_client)

def handle_message(text: str) -> None:
    event_data = {
        "token": "one-long-verification-token",
        "team_id": "TXXXXXXXX",
        "api_app_id": "AXXXXXXXXX",
        "event": {
            "type": "message",
            "text": text,
            "channel": "dummy_channel",
            "user": "UXXXXXXX1",
            "ts": "1549692377.007700",
        },
        "type": "event_callback",
        "authed_users": ["UXXXXXXX1"],
        "event_id": "Ev08MFMKH6",
        "event_time": 1234567890,
    }

    context = context_factory.create(event_data=event_data)
    if context is None:
        # TODO: logging
        print("Failed to parse event_data")
        return

    if context.text.startswith(parser.prog):
        command_str = shlex.split(context.text)[1:]
        try:
            args = parser.parse_args(command_str)
            args.context = context
            args.handler(**vars(args))
        except SlackArgumentParserException as e:
            # TODO: logging
            context.reply_ephemeral(str(e))
        except Exception:
            # TODO: logging
            err = sys.exc_info()
            context.reply_ephemeral(f"Failure on executing command: {err}")

if __name__ == "__main__":
    while True:
        text = input("> ")
        handle_message(text)


def filter_field_instances(field_instances, filter_subcode, filter_value,
                           filter_mode='e'):
    """Filter the given field.

    Filters given field and returns only that field instances that contain
    filter_subcode with given filter_value. As an input for search function
    accepts output from record_get_field_instances function. Function can be
    run in three modes:

    - 'e' - looking for exact match in subfield value
    - 's' - looking for substring in subfield value
    - 'r' - looking for regular expression in subfield value

    Example:

    record_filter_field(record_get_field_instances(rec, '999', '%', '%'),
                        'y', '2001')

    In this case filter_subcode is 'y' and filter_value is '2001'.

    :param field_instances: output from record_get_field_instances
    :param filter_subcode: name of the subfield
    :type filter_subcode: string
    :param filter_value: value of the subfield
    :type filter_value: string
    :param filter_mode: 'e','s' or 'r'
    """
    matched = []
    if filter_mode == 'e':
        to_match = (filter_subcode, filter_value)
        for instance in field_instances:
            if to_match in instance[0]:
                matched.append(instance)
    elif filter_mode == 's':
        for instance in field_instances:
            for subfield in instance[0]:
                if subfield[0] == filter_subcode and \
                   subfield[1].find(filter_value) > -1:
                    matched.append(instance)
                    break
    elif filter_mode == 'r':
        reg_exp = re.compile(filter_value)
        for instance in field_instances:
            for subfield in instance[0]:
                if subfield[0] == filter_subcode and \
                   reg_exp.match(subfield[1]) is not None:
                    matched.append(instance)
                    break
    return matched


import os
import sys
import copy

class ConvertAnnotation(object):
    def __init__(self, char_dir, origin_annotation_dir, split_rate=None):
        self.char_2_id = {}
        self.id_2_char = {}
        with open(char_dir, 'r') as fp:
            offset = 0
            for i, line in enumerate(fp):
                if line[0] in self.char_2_id.keys():
                    # print(line[0])
                    offset += 1
                else:
                    self.char_2_id[line[0]] = i - offset
                    self.id_2_char[i - offset] = line[0]
        num_class_id = len(self.char_2_id) - 1
        self.image_dir_list = []
        self.image_annotation = []
        with open(origin_annotation_dir, 'r') as fp:
            for i, line in enumerate(fp):
                line = line.strip().split(' ')
                assert len(line) == 2
                self.image_dir_list.append(line[0])
                self.image_annotation.append(line[1])
        self.image_id_annotation = []
        for i, annotation in enumerate(self.image_annotation):
            id_tmp = []
            for _, wd in enumerate(annotation):
                if wd in self.char_2_id.keys():
                    id_tmp.append(self.char_2_id[wd])
                else:
                    num_class_id += 1
                    self.char_2_id[wd] = num_class_id
                    self.id_2_char[num_class_id] = wd
                    id_tmp.append(num_class_id)
            self.image_id_annotation.append(id_tmp)
        if split_rate:
            split_point = int(len(self.image_id_annotation) * (1-split_rate))
            self.image_id_annotation_train = self.image_id_annotation[:split_point]
            self.image_id_annotation_test = self.image_id_annotation[split_point:]
            self.image_dir_list_train = self.image_dir_list[:split_point]
            self.image_dir_list_test = self.image_dir_list[split_point:]
            del self.image_id_annotation
            del self.image_dir_list

    def generate_new_chart(self, save_dir):
        with open(save_dir, 'w') as fp:
            for i, wd in enumerate(self.char_2_id.keys()):
                fp.write(wd + '\n')

class MergeDict(object):
    def __init__(self, first_dict, second_dict):
        self.first_char_2_id = {}
        self.second_char_2_id = {}
        with open(first_dict, 'r') as fp:
            first_offset = 0
            for i, line in enumerate(fp.readlines()):
                if line in self.first_char_2_id.keys():
                    first_offset += 1
                else:
                    self.first_char_2_id[line[0]] = i - first_offset
        # print(len(self.first_char_2_id))
        with open(second_dict, 'r') as fp:
            second_offset = 0
            for i, line in enumerate(fp.readlines()):
                if line in self.second_char_2_id.keys():
                    second_offset += 1
                else:
                    self.second_char_2_id[line[0]] = i - second_offset
        # print(len(self.second_char_2_id))
        first_num_class_id = len(self.second_char_2_id) - 1
        # num_unkown = 0
        for i, wd in enumerate(self.second_char_2_id):
            if wd in self.first_char_2_id.keys():
                pass
            else:
                # num_unkown += 1
                first_num_class_id += 1
                self.first_char_2_id[wd] = first_num_class_id
        print(len(self.first_char_2_id))

    def generate_new_dict(self, save_dir):
        with open(save_dir, 'w') as fp:
            for i, wd in enumerate(self.first_char_2_id.keys()):
                if i == len(self.first_char_2_id):
                    fp.write(wd)
                else:
                    fp.write(wd + '\n')

if __name__ == '__main__':
    chart_dir = '/home/oushu/tongli/ocr/ocr/TextGen/TextRecognitionDataGenerator/newcorpus1/Train/c1train_labels.txt'
    # annotation_dir = '/home/oushu/lixingyu/DataSet/image_chinese_dataset/labels.txt'
    # CA = ConvertAnnotation(chart_dir, annotation_dir, 0.05)
    # CA.generate_new_chart('/home/oushu/lixingyu/DataSet/new_char.txt')
    second_chart_dir = '/home/oushu/lixingyu/DataSet/char_6112.txt'
    MD = MergeDict(chart_dir, second_chart_dir)
    MD.generate_new_dict('/home/oushu/lixingyu/DataSet/new_char.txt')


from typing import List

def searchInsert(nums: List[int], target: int) -> int:
    low = 0
    high = len(nums) - 1
    mid = 0
    l = len(nums) - 1

    if nums[0] > target:
        return 0
    elif nums[-1] < target:
        return l + 1

    while low <= high:
        mid = (high + low) // 2

        if nums[mid] < target:
            low = mid + 1
            if mid + 1 <= l and nums[mid + 1] > target:
                return mid + 1
        elif nums[mid] > target:
            high = mid - 1
            if nums[mid - 1] < target:
                return mid
        else:
            return mid


"""empty message

Revision ID: 4f851adcf334
Revises: dd672c449325
Create Date: 2016-04-06 12:07:09.791911

"""

# revision identifiers, used by Alembic.
revision = '4f851adcf334'
down_revision = 'dd672c449325'

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('feed',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('feedType', sa.String(length=3), nullable=True),
    sa.Column('title', sa.String(length=80), nullable=True),
    sa.Column('detail', sa.Text(), nullable=True),
    sa.Column('badge', sa.String(length=256), nullable=True),
    sa.Column('publish_date', sa.DateTime(), nullable=True),
    sa.Column('update_date', sa.DateTime(), nullable=True),
    sa.Column('public', sa.Boolean(), nullable=True),
    sa.Column('active', sa.Boolean(), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.drop_table('diary')
    op.alter_column('following', 'active',
               existing_type=mysql.TINYINT(display_width=1),
               type_=sa.Boolean(),
               existing_nullable=True)
    op.alter_column('planting', 'active',
               existing_type=mysql.TINYINT(display_width=1),
               type_=sa.Boolean(),
               existing_nullable=True)
    op.alter_column('reminder', 'active',
               existing_type=mysql.TINYINT(display_width=1),
               type_=sa.Boolean(),
               existing_nullable=True)
    op.alter_column('user', 'active',
               existing_type=mysql.TINYINT(display_width=1),
               type_=sa.Boolean(),
               existing_nullable=True)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('user', 'active',
               existing_type=sa.Boolean(),
               type_=mysql.TINYINT(display_width=1),
               existing_nullable=True)
    op.alter_column('reminder', 'active',
               existing_type=sa.Boolean(),
               type_=mysql.TINYINT(display_width=1),
               existing_nullable=True)
    op.alter_column('planting', 'active',
               existing_type=sa.Boolean(),
               type_=mysql.TINYINT(display_width=1),
               existing_nullable=True)
    op.alter_column('following', 'active',
               existing_type=sa.Boolean(),
               type_=mysql.TINYINT(display_width=1),
               existing_nullable=True)
    op.create_table('diary',
    sa.Column('id', mysql.INTEGER(display_width=11), nullable=False),
    sa.Column('user_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
    sa.Column('title', mysql.VARCHAR(length=80), nullable=True),
    sa.Column('publish_date', mysql.DATETIME(), nullable=True),
    sa.Column('update_date', mysql.DATETIME(), nullable=True),
    sa.Column('active', mysql.TINYINT(display_width=1), autoincrement=False, nullable=True),
    sa.Column('badge', mysql.VARCHAR(length=256), nullable=True),
    sa.Column('detail', mysql.TEXT(), nullable=True),
    sa.Column('public', mysql.TINYINT(display_width=1), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['user_id'], [u'user.id'], name=u'diary_ibfk_1'),
    sa.PrimaryKeyConstraint('id'),
    mysql_default_charset=u'latin1',
    mysql_engine=u'InnoDB'
    )
    op.drop_table('feed')
    ### end Alembic commands ###


import numpy as np
import os
import torch
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import TensorDataset, DataLoader
from Network import VAE_Network

class Beta_VAE_Agent():
    def __init__(self, input_dim, latent_dim, output_dim, beta, learning_rate, seed, log_dir):
        self.input_dim = input_dim
        torch.manual_seed(seed)
        np.random.seed(seed)
        self.network = VAE_Network(input_dim, latent_dim, output_dim)
        self.network.train()
        self.optimizer = torch.optim.Adam(self.network.parameters(), lr = learning_rate)
        self.beta = beta
        self.log_dir = log_dir
        if not os.path.exists(self.log_dir):
            os.mkdir(self.log_dir)
        self.writer = SummaryWriter(log_dir = self.log_dir)
        self.best_score = 1e10
        self.early_stopping_count = 0

    def update_params(self, optim, loss, networks, retain_graph = False, grad_cliping = None):
        optim.zero_grad()
        loss.backward(retain_graph=retain_graph)
        if grad_cliping:
            for net in networks:
                torch.nn.utils.clip_grad_norm_(net.parameters(), grad_cliping)
        optim.step()

    def loss_function(self, x, x_hat, mean, log_var):
        reproduction_loss = torch.nn.MSELoss(reduction = 'sum')(x_hat, x)
        KLD = - 0.5 * torch.sum(1 + log_var - mean.pow(2) - log_var.exp())
        return reproduction_loss + self.beta * KLD

    def load_data(self, feature, label, valid_size, test_size, num_cpu, batch_size):
        print('Begining loading...')
        self.batch_size = batch_size
        T = feature.shape[0]
        index_1 = round(T * (1 - valid_size - test_size))
        index_2 = round(T * (1 - test_size))
        feature_train = feature[:index_1, :]
        label_train = label[:index_1, :]
        feature_valid = feature[index_1: index_2, :]
        label_valid = label[index_1: index_2, :]
        dataset_train = TensorDataset(feature_train, label_train)
        self.train_loader = DataLoader(dataset_train, batch_size = batch_size, shuffle = True, num_workers = num_cpu)
        dataset_valid = TensorDataset(feature_valid, label_valid)
        self.valid_loader = DataLoader(dataset_valid, batch_size = batch_size, shuffle = True, num_workers = num_cpu)
        self.sample_num_train = feature_train.shape[0]
        self.sample_num_valid = feature_valid.shape[0]
        print(f'The data contains {self.sample_num_train} training samples and {self.sample_num_valid} valid samples')
        print("Complete!")

    def train(self, epoch_num, lam):
        # print("Start training VAE...")
        self.network.train()
        for i in range(epoch_num):
            train_losses = list()
            for j, (x, _) in enumerate(self.train_loader):
                x = x.to(self.network.device)
                x_hat, mean, log_var = self.network.forward(x)
                L1_loss = 0
                for param in self.network.parameters():
                    L1_loss += torch.sum(torch.abs(param))
                loss = self.loss_function(x, x_hat, mean, log_var) + L1_loss * self.batch_size * lam
                train_losses.append(loss.item())
                self.update_params(self.optimizer, loss, networks=[self.network])
            train_loss = torch.tensor(train_losses).mean()
            # print(f'Epoch {i}/{epoch_num-1} Training Loss {train_loss:.2f} Time Consume {dtime:.3f}')
            self.writer.add_scalar('loss/train', train_loss, i)
            if i % 10 == 0:
                self.evaluate(i, epoch_num)
                torch.save(self.network.state_dict(), os.path.join(self.log_dir, 'Beta_VAE_final.pth'))
            if self.early_stopping_count >= 10:
                break
        print(f'best score:{self.best_score}')

    def evaluate(self, i, epoch_num):
        self.network.eval()
        with torch.no_grad():
            valid_losses = list()
            for j, (x, y) in enumerate(self.valid_loader):
                x = x.to(self.network.device)
                x_hat, mean, log_var = self.network.forward(x)
                loss = torch.nn.MSELoss(reduction = 'sum')(x_hat, x)
                valid_losses.append(loss.item())
            valid_loss = torch.tensor(valid_losses).mean()
        self.writer.add_scalar('loss/valid', valid_loss.item(), i)
        # print("-"*60)
        # print(f'Evaluation {i}/{epoch_num-1} Loss {test_loss.item():.2f}')
        # print("-"*60)
        if valid_loss.item() < self.best_score:
            # print('Update Model')
            self.early_stopping_count = 0
            self.best_score = valid_loss.item()
            torch.save(self.network.state_dict(), os.path.join(self.log_dir, 'Beta_VAE_best.pth'))
        else:
            self.early_stopping_count += 1

        self.network.train()


import pandas as pd
from meteostat import Point, Daily
from datetime import datetime
import json
import csv

with open('/Users/akhilesh/WildfireDB_Resources/Weather/us_data_full.json') as f:
    jsondata = json.load(f)
stations = list()
for item in jsondata:
    stations.append(item.get('id'))
#stations = ['70133','70000']

# now we will open a file for writing
# us_csv_file = open('/Users/akhilesh/WildfireDB_Resources/Weather/us_csv_file.csv', 'w')

# # create the csv writer object
# csv_writer = csv.writer(us_csv_file)

# # Counter variable used for writing
# # headers to the CSV file
# count = 0

# for j in jsondata:
#     if count == 0:

#         # Writing headers of CSV file
#         header = j.keys()
#         csv_writer.writerow(header)
#         count += 1

#     # Writing data of CSV file
#     csv_writer.writerow(j.values())

# us_csv_file.close()
stationsdf = pd.read_csv(r'/Users/akhilesh/WildfireDB_Resources/Weather/us_csv_file.csv')
#print(stationsdf.dtypes)
# List of station IDs in the US
#print(stations)
start = datetime(2012, 1, 1)
end = datetime(2012, 12, 31)
# Create an empty DataFrame to store the data
df = pd.DataFrame()

# Loop over the stations and download the data
for station in stations:
    # Create a Point object for the station

    # Download the daily temperature data for the station
    data = Daily(station,start,end)
    data = data.fetch()
    data['id']=station
    dates = pd.date_range(start, periods=len(data), freq='D')
    data['date'] = dates
    #data = data.assign(id = [station])
    #print(data.head())

    # Add the data to the DataFrame
    df = pd.concat([df, data])
    #print(df.dtypes)

# Save the data to a CSV file

joined=df.merge(stationsdf,on='id',how='left')
#to remove snow,wpgt,tsun
#to remove elevation, daily,timezone,region,country,name
joined.drop(['snow', 'wpgt','tsun','elevation','daily','timezone','region','country','name'], axis=1, inplace=True)
joined = joined.rename(columns={'id': 'station_id'})
joined = joined.rename(columns={'latitude': 'station_latitude'})
joined = joined.rename(columns={'longitude': 'station_longitude'})
joined = joined.drop(df.columns[0], axis=1)
joined=joined[['station_latitude','station_longitude','tavg', 'tmin', 'tmax', 'prcp', 'wdir', 'wspd', 'pres', 'station_id','date']]

#print(joined)
joined.to_csv('/Users/akhilesh/WildfireDB_Resources/Weather/bulk_weather_2012.csv',index=False)


#! /usr/bin/env python3

import io

with io.open("parrots.txt", "a") as my_file:
    # Alternative way to open and append.
    # Notice the "\n" to create a newline.
    my_file.write(u"Polly\n")  # the u prefix ensures unicode
    my_file.write(u"Piney\n")
    my_file.write(u"Fjordy\n")

names = []
my_file = io.open("parrots.txt", "r")  # open parrots.txt for reading
names.append(my_file.readline())  # read the first line
names.append(my_file.readline())  # read the second line
my_file.close()

for i, item in enumerate(names):
    print("Parrot #", i, "is", item)  # print read-in lines from the list


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from .models import Topic, Entry
from .forms import EntryForm, TopicForm
from django.http import Http404

'''A view function takes in information from a request, prepares the data needed to generate a page and then sends
the data back to the browser, often by using a template that defines what the page will look like. A context is a
dictionary in which the keys are names we'll use in the template to access the data and the values are the data we
need to send to the template '''

def check_topic_owner(topic, request):
    return topic.owner == request.user

def index(request):
    """The home page for Learning Log"""
    return render(request, 'learning_logs/index.html')

@login_required
def topics(request):
    """Show all topics"""
    topics = Topic.objects.filter(owner=request.user).order_by('date_added')
    context = {'topics': topics}
    return render(request, 'learning_logs/topics.html', context)

@login_required()
def topic(request, topic_id):
    """Show a single topic and all its entries"""
    topic = get_object_or_404(Topic, id=topic_id)
    # make sure the topic belongs to the current user
    if topic.owner != request.user:
        raise Http404
    entries = topic.entry_set.order_by('-date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'learning_logs/topic.html', context)

@login_required()
def new_topic(request):
    """Add a new topic"""
    if request.method != 'POST':
        # No data submitted; create a blank form
        form = TopicForm()
    else:
        # POST data submitted; process the data
        form = TopicForm(data=request.POST)
        if form.is_valid():
            new_topic = form.save(commit=False)
            new_topic.owner = request.user
            new_topic.save()
            return redirect('learning_logs:topics')

    # Display a blank or invalid form
    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)

@login_required()
def new_entry(request, topic_id):
    """Add a new entry for a particular topic"""
    topic = Topic.objects.get(id=topic_id)
    if request.method != 'POST':
        # no data submitted, create a blank form
        form = EntryForm()
    else:
        form = EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.topic = topic
            new_entry.save()
            return redirect('learning_logs:topic', topic_id=topic_id)

    # Display a blank or invalid form
    context = {'topic': topic, 'form': form}
    return render(request, 'learning_logs/new_entry.html', context)

@login_required()
def edit_entry(request, entry_id):
    """Editing an existing entry"""
    entry = Entry.objects.get(id=entry_id)
    topic = entry.topic
    if topic.owner != request.user:
        raise Http404

    if request.method != 'POST':
        # initial request, pre-fill form with current entry
        form = EntryForm(instance=entry)
    else:
        # POST data submitted; process data
        form = EntryForm(instance=entry, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect('learning_logs:topic', topic_id=topic.id)

    context = {'entry': entry, 'topic': topic, 'form': form}
    return render(request, 'learning_logs/edit_entry.html', context)


from library import Book
import json

def load_books():
    try:
        file=open("book.dat",'r')
        book_dict=json.loads(file.read())
        books=[]
        #id,name,description,isbn,page_count,issued,author,year
        for book in book_dict:
            book_obj=Book(book['id'],book['name'],book['description'],book['isbn'],book['page_count'],book['issued'],book['author'],book['year'])
            books.append(book_obj)
        return books
    except:
        return []

def save_books(books):
    json_books=[]
    for book in books:
        json_books.append(book.to_dict())
    with open("book.dat",'w')as file:
        file.write(json.dumps(json_books,indent=4))

def add_book(book):
    books=load_books()
    new_book=Book(book['id'],book['name'],book['description'],book['isbn'],book['page_count'],book['issued'],
                  book['author'],book['year'])
    save_books([*books,assign_idvalue(books,new_book)])

def assign_idvalue(books,new_book):
    book_ids=[]
    for book in books:
        book_ids.append(int(book.id))
    if list(filter(lambda id: id == int(new_book.id),book_ids))==[]:
        return new_book
    else:
        new_book.id=int(max(book_ids)+1)
        return new_book
def get_issuedbooks():
    books=load_books()
    for book in books:
        return list(filter(lambda book:book.issued==True,books))
def get_unissuedbooks():
    books=load_books()
    for book in books:
        return list(filter(lambda book:book.issued==False,books))
def find_books(book_id):
    books=load_books()
    for book in books:
        if book.id==book_id:
            return book
    return None
def update_book(book):
    book=Book(book['id'],book['name'],book['description'],book['isbn'],book['page_count'],book['issued'],
                  book['author'],book['year'])
    books=load_books()
    if book != None:
        books = list(filter(lambda bk: int(bk.id) != int(book.id), books))
        books.append(book)
        save_books(books)

def deletebook(id):
    books=load_books()
    books=list(filter(lambda bk: int(bk.id)!=int(id),books))
    save_books(books)


'''
첫째 줄에는 두 개의 자연수가 주어진다. 이 둘은 10,000이하의 자연수이며 사이에 한 칸의 공백이 주어진다.
첫째 줄에는 입력으로 주어진 두 수의 최대공약수를, 둘째 줄에는 입력으로 주어진 두 수의 최소 공배수를 출력한다.
'''
# 유클리드 호제법

import sys

a, b = map(int, sys.stdin.readline().split())

def get_gcd(a, b):
    if b==0:
        return a
    else:
        return get_gcd(b, a%b)

gcd = get_gcd(a, b)
lcm = int((a * b / gcd))

sys.stdout.write(str(gcd)+'\n')
sys.stdout.write(str(lcm))

2609_최대공약수와최소공배수.py


class linearsearch:
    def __init__(self,arr):
        self.arr=arr
    def search(self,target):
        for i in range(len(self.arr)):
            if self.arr[i]==target:
                return i
        return -1
my_list=[1,3,4,6,5,6,7,8]
searcher=linearsearch(my_list)
print(searcher.search(3))


def enthalpy_Cpg_Hvap(self):
        r'''Method to calculate the enthalpy of an ideal mixture (no pressure
        effects). This routine is based on "route A", where only the gas heat
        capacity and enthalpy of vaporization are used.

        The reference temperature is a property of the class; it defaults to
        298.15 K.

        For a pure gas mixture:

        .. math::
             H = \sum_i z_i \cdot \int_{T_{ref}}^T C_{p}^{ig}(T) dT

        For a pure liquid mixture:

        .. math::
             H = \sum_i z_i \left( \int_{T_{ref}}^T C_{p}^{ig}(T) dT + H_{vap, i}(T) \right)

        For a vapor-liquid mixture:

        .. math::
             H = \sum_i z_i \cdot \int_{T_{ref}}^T C_{p}^{ig}(T) dT
                 + \sum_i x_i\left(1 - \frac{V}{F}\right)H_{vap, i}(T)

        Returns
        -------
        H : float
            Enthalpy of the mixture with respect to the reference temperature,
            [J/mol]

        Notes
        -----
        The object must be flashed before this routine can be used. It
        depends on the properties T, zs, xs, V_over_F, HeatCapacityGases,
        EnthalpyVaporizations, and.
        '''
        H = 0
        T = self.T
        if self.phase == 'g':
            for i in self.cmps:
                H += self.zs[i]*self.HeatCapacityGases[i].T_dependent_property_integral(self.T_REF_IG, T)
        elif self.phase == 'l':
            for i in self.cmps:
                # No further contribution needed
                Hg298_to_T = self.HeatCapacityGases[i].T_dependent_property_integral(self.T_REF_IG, T)
                Hvap = self.EnthalpyVaporizations[i](T) # Do the transition at the temperature of the liquid
                if Hvap is None:
                    Hvap = 0 # Handle the case of a package predicting a transition past the Tc
                H += self.zs[i]*(Hg298_to_T - Hvap)
        elif self.phase == 'l/g':
            for i in self.cmps:
                Hg298_to_T_zi = self.zs[i]*self.HeatCapacityGases[i].T_dependent_property_integral(self.T_REF_IG, T)
                Hvap = self.EnthalpyVaporizations[i](T)
                if Hvap is None:
                    Hvap = 0 # Handle the case of a package predicting a transition past the Tc
                Hvap_contrib = -self.xs[i]*(1-self.V_over_F)*Hvap
                H += (Hg298_to_T_zi + Hvap_contrib)
        return H


"""Whatsapp chat data loader"""

import logging
from pathlib import Path
from typing import List

from llama_index.readers.base import BaseReader
from llama_index.readers.schema.base import Document

class WhatsappChatLoader(BaseReader):
    """
    Whatsapp chat data loader.

    Args:
        path (str): Path to Whatsapp chat file.
    """

    def __init__(self, path: str):
        """Initialize with path."""

        self.file_path = path

    def load_data(self) -> List[Document]:
        """
        Parse Whatsapp file into Documents
        """

        from chatminer.chatparsers import WhatsAppParser

        path = Path(self.file_path)

        parser = WhatsAppParser(path)
        parser.parse_file()
        df = parser.parsed_messages.get_df()

        logging.debug(f"> Number of messages: {len(df)}.")

        docs = []
        n = 0
        for row in df.itertuples():
            extra_info = {
                "source": str(path).split("/")[-1].replace(".txt", ""),
                "author": row.author,
                "timestamp": str(row.timestamp),
            }

            docs.append(
                Document(
                    text=str(row.timestamp)
                    + " "
                    + row.author
                    + ":"
                    + " "
                    + row.message,
                    extra_info=extra_info,
                )
            )

            n += 1
            logging.debug(f"Added {n} of {len(df)} messages.")

        logging.debug(f"> Document creation for {path} is complete.")
        return docs


DEBUG = True

SECRET_KEY = 'q^es5sedujo$g@%-d4tl9ws@z+#m1mab&sdr_5)r&a80_+kd@+'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
    }
}

ROOT_URLCONF = 'esteid.urls'

MIDDLEWARE_CLASSES = []

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.admin',
    'esteid',
]

LOGGING = {
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(name)s: %(message)s'
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        'django': {'handlers': [], 'propagate': True},
        'django.request': {'handlers': [], 'propagate': True},
        'django.security': {'handlers': [], 'propagate': True},

        # Uncomment to enable zeep debug logging
        # 'zeep.transports': {
        #     'level': 'DEBUG',
        #     'propagate': True,
        #     'handlers': ['console'],
        # },
    }
}


"""
Р-33.  (С.С. Поляков) Укажите наибольшее целое значение А, при котором выражение
**(y – x != 5) ∨ (A < 2x^3 + y) ∨ (A < y^2 + 16)**
истинно для любых целых положительных значений x и y.
"""
result = []

def F(x, y, A):
    return (y - x != 5) or (A < 2*(x**3) + y) or (A < y**2 + 16)

for A in range(1, 100):
    OK = True
    for x in range(1, 1000):
        for y in range(1, 1000):
            if not F(x, y, A):
                OK = False
                break
    if OK:
        result.append(A)
print(max(result))

# Перебор А, но в обратную сторону
# for A in range(100, 0, -1):
#     OK = True
#     for k in range(1,1000):
#         for n in range(1,1000):
#             if not f(k, n, A):
#                 OK = False
#                 break
#     if OK:
#         print(A)
#         break


from django.shortcuts import render
from .models import *

# Create your views here.

def home(request):
    if request.method=='POST':
        name=request.POST['name']
        skill=request.POST['skill']
        description=request.POST['description']
        obj=Details(name=name,skill=skill,description=description)
        obj.save()
    return render(request,'home.html')

def detail(request):
    obj=Details.objects.all()
    return render(request,'details.html',{'obj':obj})


from django import forms
from .models import Cliente

class FormularioCliente(forms.ModelForm):

    class Meta:

        model = Cliente

        fields = (

            'empresa',
            'nit',
            'nom_cliente',
            'nom_comercial_cliente',
            'direccion',
            'telefono',
            'correo',
            'web',
            'pais',
            'image'

        )

        labels = {

            'nom_empresa' : 'nombre cliente empresa',
            'nom_comercial' : 'nombre comercial cliente',

        }