import os, sys, requests
import time, json
from bs4 import BeautifulSoup

URL_CONST = 7

def clear():
    if os.name == 'nt':
        _ = os.system('cls')

    else:
        _ = os.system('clear')

def yes_no_query():
  yes = {'y', 'yes', 'ye', ''}
  no = {'no','n'}

  while(True):
    choice = input().lower()
    if choice in yes:
      return True
    elif choice in no:
      return False
    else:
      sys.stdout.write("Please respond with 'yes' or 'no'\t")

def read_monitor_query():
  read = {'read', 'r', 'rea'}
  monitor = {'monitor','monit','m'}

  while(True):
    choice = input().lower()
    if choice in read:
      return True
    elif choice in monitor:
      return False
    else:
      sys.stdout.write("Please respond with 'read' or 'monitor'\t")

def enter_query():
  enter = {''}

  while(True):
    choice = input().lower()
    if choice in enter:
      return True
    else:
      return False

#main

#clear the console window
clear()

#load the saved url, ask if it's okay, do changes and start the monitor
print("Default file: stored_data.txt\nPress Enter to continue or input anything to select another file:\n")
if enter_query() == False:
    print("\nPlease specify the name of file you want to use:\n")
    filename = input()
    if not filename.endswith(".txt"):
        filename = filename + ".txt"
else:
    filename = "stored_data.txt"

#load the json file
data = None

while(True):
  try:
      with open(filename, 'r') as textfile:
          data = json.load(textfile)
          if "url" not in data or not data["url"]:
              raise FileNotFoundError
          else:
              break

  except (FileNotFoundError, json.decoder.JSONDecodeError):   #if there is no file, input new url
      print(f"File {filename} does not exist or is corrupted, do you want to create a new one? Y/n")
      if yes_no_query() == False:
          exit("Program has ended.")

      print("Please input the url of the Minecraft server you want to monitor:\n")
      url = input()
      data = {
        "url": str(url),
        "name": "",
        "address": "",
        "max players": 0,
        "hours": {}
        }
      with open(filename, 'w') as outfile:
          json.dump(data, outfile, indent=4)

#request the webpage data
clear()
print(f"Currently used url: {data['url']}\n")

print("Do you want to launch the monitor or get a single data read?\tmonitor/read")
if read_monitor_query() == True:    #single read
    res = requests.get(data['url'])
    soup = BeautifulSoup(res.text, 'html.parser')

    server_name = (soup.find("h1").string)
    server_ip = (soup.find(string = "Address").parent.parent.parent.contents[3].string)

    player_string = (soup.find(string = "Players").parent.parent.parent.contents[3].string)
    players = int(player_string.split("/")[0])
    max_players = int(player_string.split("/")[1])

    clear()
    print(f"Server name: {server_name}\nAddress: {server_ip}\n")
    print(f"Players: {players}\nMax players: {max_players}\n")

else:                               #monitor
    while(True):
        t = time.localtime()
        current_time = time.strftime("%H:%M", t)
        print(f"Running: {current_time}", end="\r")

        if t.tm_min % 15 == 0:   #get data

            res = requests.get(data['url'])
            soup = BeautifulSoup(res.text, 'html.parser')

            server_name = (soup.find("h1").string)
            server_address = (soup.find(string = "Address").parent.parent.parent.contents[3].string)

            player_string = (soup.find(string = "Players").parent.parent.parent.contents[3].string)

            players = int(player_string.split("/")[0])
            max_players = int(player_string.split("/")[1])

            data["name"] = server_name
            data["address"] = server_address
            data["max players"] = max_players

            time_formatted = str( float(t.tm_hour) + round(float(t.tm_min/60), 2) )

            if time_formatted not in data["hours"]:
                data["hours"][time_formatted] = []
            data["hours"][time_formatted].append(players)

            with open(filename, 'w') as outfile:
                json.dump(data, outfile, indent=4)
            print("\nCheck successful!")

        time.sleep(60)


import cv2
import numpy as np
import os
from random import shuffle
import tensorflow as tf
import matplotlib.pyplot as plt
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from tqdm import tqdm
from pathlib import Path
import models

TRAINDIR='train/train'
TESTDIR='test1/test1'
PRACTDIR='pract'
IMG_SIZE= 50
LR=1e-3

def predict_clf(target_path,model):
    target_img=cv2.imread(target_path)
    target= process_img(target_path)
    np_test=np.array(target).reshape(IMG_SIZE, IMG_SIZE, 1)
    ans=model.predict([np_test])
    sc=ans
    ans=np.round(ans)
    print(sc[0])
    if(np.all(ans==[1,0])):
        return 'cat',str(sc[0]*100)[1:6]
    else:
        return 'dog',str(sc[0]*100)[1:6]

def process_img(path):
    img_data=cv2.imread(path,cv2.IMREAD_GRAYSCALE)
    img_data=cv2.resize(img_data,(IMG_SIZE,IMG_SIZE))
    return np.array(img_data)

def create_label(img_name):
    #creating one-hot encoded vector from imgname
    word_label=img_name.split('.')[0]
    if word_label == 'cat':
        return np.array([1,0])
    elif word_label == 'dog':
        return np.array([0,1])

def model_evaluate(model,test_it_x,test_it_y):
    test_it_y=np.array(test_it_y)
    score = model.evaluate(test_it_x, test_it_y)
    return score

def run(targname=''):

    MODEL_NAME='default.tfl'
    MODEL_DIR='models'

    #tf-model
    tf.reset_default_graph()
    model=models.catDefaultModel(IMG_SIZE,LR)

    if(os.path.exists( os.path.join( MODEL_DIR,'{}.meta'.format(MODEL_NAME)))):
        model.load(os.path.join( MODEL_DIR,MODEL_NAME))

    if not targname=='':
        target_path=targname
        return predict_clf(target_path,model)
        #print(model_evaluate(model,X_test,y_test))

run()


from pip._vendor import colorama
from sklearn import preprocessing,svm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd

import numpy as np
import csv

import pickle

x_dates = []
y_prices = []

def load_model(model_name):
    pickle_in = open(model_name,'rb')
    clf = pickle.load(pickle_in )
    return clf

def load_file_graph(file_name):
    with open(file_name, 'r') as csv_file:
        csv_file_reader = csv.reader(csv_file)
        next(csv_file_reader)
        for row in csv_file_reader:
            x_dates.append(int(row[0].split('-')[0]))
            y_prices.append(float(row[1]))

def estimate_coefficients(x,y):

    x_size = np.size(x)
    mean_x, mean_y = np.mean(x), np.mean(y)

    SS_xy = np.sum((y*x) - (x_size*mean_y*mean_x))
    SS_xx = np.sum((x*x) - (x_size*mean_x*mean_x))

    b_1 = SS_xy / SS_xx
    b_0 = mean_y - (b_1*mean_x)
    return (b_0,b_1)

def plot_regression_line(x,y,coefficient_b):

    #plot the pois ate the graph
    plt.scatter(x,y,marker="o",s=30,color="m")

    #predict de responce vector
    y_pred = coefficient_b[0]+ (coefficient_b[1]*x)

    #plotting the regretion line
    plt.plot(x,y_pred,color="g")

    #labels
    plt.xlabel("year")
    plt.ylabel("price")

    plt.show()

load_file_graph('gold_price.csv')

clf = load_model('linearRegre.pickle')

prediction = clf.predict([[2021],[2022],[2023],[2044]] )
print("predict values_________________________")
print(prediction)

x_dates = np.array(x_dates)
y_prices = np.array(y_prices)

b =  estimate_coefficients(x_dates,y_prices)
plot_regression_line(x_dates,y_prices,b)


class ProductOfLastKNumbers:

        def __init__(self):
            self.prefixprod = []
            self.prefixprod.append(1)

        def add(self, num: int) -> None:
            if num==0:
                self.prefixprod=[1]
            else:
                self.prefixprod.append(self.prefixprod[-1]*num)

        def getProduct(self, k: int) -> int:
            if k>=len(self.prefixprod):
                return 0
            beforek=self.prefixprod[-k-1]
            currprod=self.prefixprod[-1]
            return currprod/beforek

if __name__ == '__main__':
    init = ProductOfLastKNumbers()
    init.add(3)  # [3]
    init.add(0)  # [3,0]
    init.add(2)  # [3,0,2]
    init.add(5)  # [3,0,2,5]
    init.add(4)  # [3,0,2,5,4]
    print(init.getProduct(2))  # return 20. The product of the last 2 numbers is 5 * 4 = 20
    print(init.getProduct(3))  # return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40
    print(init.getProduct(4))  # return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0
    init.add(8)  # [3,0,2,5,4,8]
    print(init.getProduct(2))  # return 32. The product of the last 2 numbers is 4 * 8 = 32


import hashlib
import matplotlib.pyplot as plt

#from my_laba import MyHashFunction,MyHashNumberFunction,HashMD5
#Хеш функции
#Хэш-функция берем сумму первой и последней букв слова
def MyHashFunction(word):
    if word != '':
        return (ord(word[0])+ord(word[-1]))//10
    return None
#Хэш-функция берем все букву и умножаем на их порядковый номер
def MyHashNumberFunction(word):
    if word != '':
        sum=0
        for i in range(len(word)):
            sum+=i*ord(word[i])
        return sum
    return None
#Хэш-функция MD5
def HashMD5(word):
    if word != '':
        return hashlib.md5(word.encode()).hexdigest()
    return None

class guards_date:   #является ячейкой помяти
    def __init__(self,text=None):
        if text:
            self.id=text[1]     #код хранимой информации
            self.text=text[0]  #хранит информацию
        else:
            self.id=text    #код хранимой информации
            self.text=text  #хранит информацию
    def delet(self):    #функция удаления
        self.id=None
        self.text=None  #функция удаления

class key_date:     #хранит информацию о ключе
    def __init__(self, name,pair=None):
        self.key_name=str(name)      #ключ
        self.pair=guards_date(pair)        #ссылка на элемент памяти :guards_date

    def replacement_pair(self,pair):    #замена ссылки на ячейку памяти
        self.pair=guards_date(pair)            #замена ссылки на ячейку памяти
    def del_key(self):          #удаление ключа
        self.key_name=None
        self.pair.delet()
        self.pair=None                      #удаление ключа

class hash:
    def __init__(self, name,key=None,pair=None):
        self.hash_name=str(name)      #хеш
        self.keys=[]        #ссылка на ключи :key_date
        if key!=None:
            self.keys.append(key_date(key))
            if pair!=None:
                self.keys[len(self.keys)-1].replacement_pair(pair)    #хранит ключи
    def add_key(self,key,pair):    #добавление ссылки на ячейку памяти
        for i in self.keys:
            if i.key_name==key:
                i.replacement_pair(pair)
                return True
        self.keys.append(key_date(key, pair))              #добавляет или заменяет ключ и данные его
        return False
    def del_hash(self):          #удаление хеша
        self.hash_name=None
        for i in self.keys:
            i.del_key()
        self.keys=None                      #удаление хеша
    def del_key_number(self,number): #удаление ключа по номерному порядку
        if number>len(self.keys)-1:
            return False
        self.keys[number].del_key()
        self.keys.pop(number)
        return True         #удаление ключа по номерному порядку
    def del_key_text(self,text): #удаление ключа по содержанию
        x=self.search_key(text)
        if x>=0:
            self.keys[x].del_key()
            self.keys.pop(x)
            return True
        else:
            return False             #удаление ключа по содержанию
    def search_key(self,text):                  #поиск ключа по содержанию
        for i in range(len(self.keys)):
            if self.keys[i].key_name==str(text):
                return i
        return -1               #поиск ключа по содержанию
    def return_value_key(self,text):
        x=self.search_key(text)
        if x>=0:
            return self.keys[x].pair
        return None         #выводит значение данного ключа

class hash_table:
    def __init__(self,name='Name_table'):
        self.hash_table_name=str(name)      #название хеш-таблицы
        self.hash_table=[]        #хранит список кеша  :hash
    def search_hash(self,text):                  #поиск hash
        for i in range(len(self.hash_table)):
            if self.hash_table[i].hash_name==str(text):
                return i
        return -1                   #поиск hash
    def add_hash(self,name_hash,name_key,pair):    #добавляет пару в хеш-таблицу
        x=self.search_hash(name_hash)
        if x>=0:
            self.hash_table[x].add_key(name_key,pair)
        else:
            self.hash_table.append(hash(name_hash,name_key,pair))   #добавляет пару в хеш-таблицу
    def del_hash_table(self):          #удаление хеш-таблицы
        self.hash_table_name=None
        for i in self.hash_table:
            i.del_hash()
        self.hash_table=None                     #удаление хеш-таблицы
    def return_key(self,name_hash,name_key):
        x=search_hash(name_hash)
        if x>=0:
            y=self.hash_table[x].keys.search_key(name_key)
            if y>=0:
                return self.hash_table[x].keys[y].pair
        return -1      #получение доступа к элементу памяти
    def hist(self):
        index = [i for i in range(len(self.hash_table))]
        values = []
        for key in self.hash_table:
            if len(key.keys):
                values.append(len(key.keys))
            else:
                values.append(0)
        plt.bar(index, values)
        plt.show()
    def view(self):
        print(self.hash_table_name)
        t1='-'
        t2='--'
        t3='---'
        for i in self.hash_table:
            print(t1+str(i.hash_name))#хеш - значение
            for j in i.keys:
                print(t2+str(j.key_name))#ключ - значение
                print(t3,j.pair.id,j.pair.text)

if __name__ == '__main__':
    pair=[None]
    if pair[0]:
        print(pair)
    hash_tab=hash_table()
    #id_text=0
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
     #       id_text+=1
            hash_tab.add_hash(MyHashFunction(word),word,[word,index])
    hash_tab.view()
    hash_tab.hist()
    print('')
    print('------------------------------')
    print('')
    key2=hash_table()
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
            key2.add_hash(MyHashNumberFunction(word),word,[word,index])
    key2.view()
    key2.hist()
    print('')
    print('------------------------------')
    print('')
    key3=hash_table()
    with open('text6.txt', 'r') as file:
        for index, word in enumerate(file.read().split(' ')):
            word = ''.join(letter for letter in word if letter.isalpha())
            #print(index, word)
            key3.add_hash(HashMD5(word),word,[word,index])
    key3.view()
    key3.hist()


from django import forms
from .models import *
from django.utils.timezone import now

category_3 = {1: 'Удовлетворительное', 2: 'Хорошее', 0: 'Неудовлетворительное'}
category_3 = tuple(map(tuple, category_3.items()))

mark_rep = {0: 'Репер', 1: 'Марка', 2: 'Инклинометр'}
mark_rep = tuple(map(tuple, mark_rep.items()))

cond = {1: 'Действующая', 2: 'Резервная', 3: 'Недействующая', 4: 'Законсервированная', 5: 'Неисправная',
        6: 'Заброшенная', 7: 'Ликвидированная', 8: 'Проектная', 9: 'Пробурена'}
cond = tuple(map(tuple, cond.items()))

class FileFieldForm(forms.Form):
    file_field = forms.FileField(label='Фотодокументация', widget=forms.ClearableFileInput(attrs={'multiple': True}))

class EgpObservesInspectionForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(EgpObservesInspectionForm, self).__init__(*args, **kwargs)
        self.fields['date'].initial = now
        self.fields['damage_description'].label = False
        self.fields['survey2'].label = False
        self.fields['logs_results'].label = False

    class Meta:
        model = EgpObservesInspection
        fields = (
            'egp_obs', 'date', 'survey1', 'survey2', 'painting_condition', 'label_condition', 'damage_description',
            'logs_status', 'logs_results', 'comments', 'recommendations')

        widgets = {
            'egp_obs': forms.TextInput(attrs={'class': 'form-control', 'type': 'integer'}),
            'date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
            'painting_condition': forms.Select(choices=category_3, attrs={'class ': 'form-control'}),
            'label_condition': forms.Select(choices=category_3, attrs={'class': 'form-control'}),
            'damage_description': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2, 'placeholder': 'Описание повреждений'}),
            'logs_status': forms.CheckboxInput(),
            'logs_results': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2, 'placeholder': 'Результаты видеокаротажа'}),
            'comments': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2}),
            'recommendations': forms.Textarea(
                attrs={'class': 'form-control', 'type': 'text', 'rows': 2}),
            'survey1': forms.Select(attrs={'class': 'form-control'}),
            'survey2': forms.Select(attrs={'class': 'form-control'})
        }
        labels = {
            'egp_obs': 'Номер пункта наблюдений',
            'date': 'Дата инспекции',
            'painting_condition': 'Состояние покраски',
            'label_condition': 'Состояние маркировки',
            'logs_status': 'Проведение видеокаротажа',
            'comments': 'Примечания',
            'recommendations': 'Рекомендации',
            'survey1': 'Обследование провели',
        }

class EgpObservesForm(forms.ModelForm):
    class Meta:
        model = EgpObserves
        fields = ('egp_obs_type',)

        widgets = {
            'egp_obs_type': forms.Select(choices=mark_rep, attrs={'class': 'form-control'}),
        }
        labels = {
            'egp_obs_type': 'Тип пункта наблюдений',
        }

class EgpObservesConditionForm(forms.ModelForm):
    class Meta:
        model = EgpObservesCondition
        fields = ('condition',)

        widgets = {
            'condition': forms.Select(choices=cond, attrs={'class': 'form-control'}),
        }
        labels = {
            'condition': 'Техническое состояние пункта наблюдений',
        }

class DocumentsForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(DocumentsForm, self).__init__(*args, **kwargs)
        self.fields['creation_date'].initial = now
    class Meta:
        model = Documents
        fields = ('creation_date',)

        widgets = {
            'creation_date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
        }
        labels = {
            'creation_date': 'Дата создания акта',
        }


class LinkedList:
    def __init__(self):
        self.head = None

    def add(self, data):
        new_node = Node(data)

        if self.head is None:
            self.head = new_node
        else:
            new_node.next = self.head
            self.head = new_node

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_list(l):
    if isinstance(l,LinkedList):
        cur_node = l.head
    elif isinstance(l, Node):
        cur_node = l

    while cur_node is not None:
        print(cur_node.data , end= ' ')
        cur_node = cur_node.next

def find_last_occuerance_and_remove(item, l):
    node = l.head
    occurance_node = None
    while node is not None:
        if node.data == item:
            occurance_node = node
        node = node.next

    if occurance_node:
        occurance_node.data = occurance_node.next.data
        occurance_node.next = occurance_node.next.next

if __name__ == "__main__":
    l = LinkedList()

    for i in [7,10,2,3,4,5,6,2,5,6]:
        l.add(i)

    print_list(l)

    key_item = 2 # need to find the last occurrence of that item and need to remove that.

    find_last_occuerance_and_remove(key_item, l)
    print ('\n')
    print_list(l)


import os
from model import Model

def save_config(file_name, model):
    """ Creates a save file that can be used to access the model again """
    if ".cfg" not in file_name.lower():
        file_name = file_name + ".cfg"

    if not os.path.exists(file_name):
        file = open(file_name, "w")
        file.write("NAME=" + str(model.NAME) +
                   "\nSIZE=" + str(model.SIZE) +
                   "\nEPOCHS=" + str(model.EPOCHS) +
                   "\nBATCH=" + str(model.BATCH_SIZE) +
                   "\nPATH=" + str(model.data_path))
        file.close()
        return True
    return False

def read_config(file_name):
    """ Reads a save file which references a model """
    name = ""
    size = ""
    epochs = ""
    batch_size = ""
    data_path = ""
    if ".cfg" not in file_name.lower():
        file_name = file_name + ".cfg"

    if os.path.exists(file_name):
        file = open(file_name, "r")
        f = file.read()
        list = f.split("\n")
        for t in list:
            if "name" in t.lower():
                name = t.split("=")[1]
            elif "size" in t.lower():
                size = int(t.split("=")[1])
            elif "epochs" in t.lower():
                epochs = int(t.split("=")[1])
            elif "batch" in t.lower():
                batch_size = int(t.split("=")[1])
            elif "path" in t.lower():
                data_path = t.split("=")[1]

        file.close()
        return Model(name, size, epochs, batch_size, data_path)
    return False


import collections
import operator
import typing as T

from .base import Vocabulary

class CountVocabulary(Vocabulary[int]):
    """Vocabulary from token counts"""

    def __init__(
        self,
        sentences: T.Optional[T.Iterable[T.Iterable[str]]] = None,
        min_count: int = 1,
        max_vocab_size: T.Optional[int] = None,
        padding: bool = True,
        padding_token: T.Optional[str] = None,
        oov_token: T.Optional[str] = None,
    ) -> None:
        super().__init__(padding=padding, padding_token=padding_token, oov_token=oov_token)
        self._min_count = min_count
        self._max_vocab_size = max_vocab_size
        self._counter: T.Optional[collections.Counter] = None
        self._index_to_token: T.List[str] = []

        if sentences is not None:
            self.update(sentences)

    @property
    def index_to_token_dict(self) -> T.List[str]:
        return self._index_to_token

    @property
    def counter_dict(self):
        return self._counter

    @property
    def min_count(self) -> int:
        return self._min_count

    @min_count.setter
    def min_count(self, v: int):
        if v != self._min_count:
            self._min_count = v
            self.build()

    @property
    def max_vocab_size(self):
        return self._max_vocab_size

    @max_vocab_size.setter
    def max_vocab_size(self, v: T.Optional[int]):
        if v != self._max_vocab_size:
            self._max_vocab_size = v
            self.build()

    def to_tokens(self, id_sentences: T.Iterable[T.Iterable[int]], remove_paddings: bool = True) -> T.List[T.List[str]]:
        if not self._index_to_token:
            self._build_index_to_token()
        retlist = []
        padding_idx = self._token_to_index.get(self._padding_token)
        for ids in id_sentences:
            tokens = []
            for idx in ids:
                if remove_paddings and padding_idx is not None and idx == padding_idx:
                    continue
                tokens.append(self._index_to_token[idx])
            retlist.append(tokens)
        return retlist

    def merge_by_count(self, vocab: "CountVocabulary") -> None:
        if vocab.counter_dict:
            if self._counter is None:
                self._counter = collections.Counter()
            self._counter.update(vocab.counter_dict)
            self.build()

    def update(self, sentences: T.Iterable[T.Iterable[str]]) -> None:
        if self._counter is None:
            self._counter = collections.Counter()
        for tokens in sentences:
            self._counter.update(tokens)
        # Need to recreate token_to_index dictionary from counts
        self.build()

    def build(self):
        self.init()
        counts = sorted(self._counter.items(), key=operator.itemgetter(1), reverse=True)
        actual_max_vocab_size = self._max_vocab_size
        if actual_max_vocab_size is not None:
            actual_max_vocab_size += len(self._token_to_index)
        for key, count in counts:
            if actual_max_vocab_size is not None and len(self._token_to_index) >= actual_max_vocab_size:
                break
            if self._min_count and count < self._min_count:
                continue
            self.add_token(key)

    def __getstate__(self) -> T.Dict[str, T.Any]:
        """Robust pickling"""
        state = super().__getstate__()
        state["min_count"] = self._min_count
        state["max_vocab_size"] = self._max_vocab_size
        state["counter"] = self._counter
        return state

    def __setstate__(self, state) -> None:
        """Robust pickling"""
        super().__setstate__(state)
        self._min_count = state["min_count"]
        self._max_vocab_size = state["max_vocab_size"]
        self._counter = state["counter"]
        self._build_index_to_token()

    # overrides

    def add_token(self, token: str):
        self._token_to_index[token] = len(self._token_to_index)

    def init(self):
        super().init()
        self._index_to_token = []

    def init_padding(self):
        self.add_token(self._padding_token)

    def init_oov(self):
        self.add_token(self._oov_token)

    def serialize_entry(self, token: str, _: T.Any) -> str:
        return token

    def deserialize_entry(self, entry: str) -> None:
        self.add_token(entry)

    # private

    def _build_index_to_token(self):
        self._index_to_token = [None] * len(self._token_to_index)
        for token, token_id in self._token_to_index.items():
            self._index_to_token[token_id] = token


from odoo import _, api, fields, models

class ExpendableResource(models.Model):
    _name = 'resource.expendable'
    _description = 'Expendable resource of an employee.'

    name = fields.Char(
        string='Name',
        required=True,
    )
    employee_id = fields.Many2one(
        comodel_name='hr.employee',
        string='Employee',
    )
    delivery_date = fields.Date(
        string='Delivery Date',
        default=lambda self: fields.Date.context_today(self),
    )
    description = fields.Text(
        string="Description",
    )

    @api.model
    def create(self, vals):
        res = super().create(vals)
        msg = _('
 Expendable Resource'
                ' {name} has been created:
'
                '
Name: {name}
'
                ).format(name=res.name)
        if res.description:
            msg += _('
Description: {description}

'
                     ).format(description=res.description)
        if res.delivery_date:
            msg += _('
Delivery Date: {delivery_date}
'
                     '
').format(delivery_date=res.delivery_date)
        res.employee_id.message_post(body=msg)
        return res

    @api.multi
    def write(self, vals):
        for resource in self:
            msg = _('
 Expendable'
                    ' Resource {resource} has been modified:'
                    '
'
                    ).format(resource=resource.name,)
            post = False
            if 'name' in vals and vals.get(
               'name') != resource.name:
                msg += _('
Name: {old_val} '
                         ' {new_val}
').format(
                    old_val=resource.name,
                    new_val=vals['name'],
                )
                post = True
            if 'description' in vals and vals.get(
               'description') != resource.description:
                msg += _('
Description: {old_val} '
                         ' {new_val}
').format(
                    old_val=resource.description,
                    new_val=vals['description'],
                )
                post = True
            msg += '
'
            if post:
                resource.employee_id.message_post(body=msg)
        return super().write(vals)

    @api.multi
    def unlink(self):
        for resource in self:
            msg = _('
 Expendable'
                    ' Resource {resource} has been deleted:'
                    '

Name: {resource}
'
                    '
Description: {description}
'
                    '
Delivery Date: {delivery_date}
'
                    ).format(
                resource=resource.name, delivery_date=resource.delivery_date,
                description=resource.description
            )
            resource.employee_id.message_post(body=msg)
        return super().unlink()


'''Configuration script for Target environment.'''
from collections import OrderedDict
from typing import Dict
import numpy as np

from psgi.envs.base_predicate_config import BasePredicateConfig
from psgi.envs.predicate_graph import PredicateLogicGraph

# Define parameters (no intersection allowed)
FOOD_FRUIT = ['apple', 'pear']    # pick X
FOOD_VEGI = ['cabbage', 'carrot'] # pick X / slice X
FOOD_MEAT = ['beef', 'pork']      # pick X / slice X / cook X
FOOD_PARAMS = FOOD_FRUIT + FOOD_VEGI + FOOD_MEAT

OBJECT= ['dish']
PLACE = ['table']
###
PARAMS = FOOD_PARAMS + OBJECT + PLACE
assert len(set(PARAMS)) == len(PARAMS), "Error! duplicated params!"

TRAIN_ONLY = ['apple', 'beef']
EVAL_ONLY = ['pear', 'pork']
assert all([par in PARAMS for par in TRAIN_ONLY+EVAL_ONLY]), "Unknown parameter in TRAIN_ONLY and/or EVAL_ONLY"
TRAIN_PARAMS = [par for par in PARAMS if par not in EVAL_ONLY]
EVAL_PARAMS = [par for par in PARAMS if par not in TRAIN_ONLY]

# Manually set the feature
FEATURE_POSITIVE_NAME_PARAMS = {
  'f_pickupable': FOOD_PARAMS+OBJECT,
  'f_sliceable': FOOD_PARAMS,
  'f_needslice': FOOD_VEGI + FOOD_MEAT,
  'f_cookable': FOOD_MEAT,
}
assert all([set(param_set).issubset(set(PARAMS)) for param_set in FEATURE_POSITIVE_NAME_PARAMS.values()] ), "Error! 'FEATURE_POSITIVE_NAME_PARAMS' contains unknown parameter"

class CookingClassic(BasePredicateConfig):
  environment_id = 'cooking_classic'

  def __init__(self, seed: int, graph_param: str, keep_pristine: bool = False, feature_mode: str = 'cluster-s', visualize: bool = False):
    self.num_graphs = 1
    self.max_step = 8
    super().__init__(
      seed=seed,
      graph_param=graph_param,
      keep_pristine=keep_pristine,
      feature_mode=feature_mode,
      visualize=visualize,
    )

  @property
  def parameters(self):
    return set(PARAMS)

  # Manually set
  def _construct_predicate_graph(self):
    """Implement predicate precondition&effect
    """
    g = PredicateLogicGraph('CookingClassic')
    # Add all features
    PICKUPABLE_X = '(f_pickupable, A)' # pick
    SLICEABLE_X = '(f_sliceable, A)' # slice
    NEEDSLICE_X = '(f_needslice, A)'
    COOKABLE_X = '(f_cookable, A)' # slice

    g.add_features([PICKUPABLE_X, SLICEABLE_X, NEEDSLICE_X, COOKABLE_X])

    # Add all subtask nodes (just names)
    PICKUP_X = '(pickup, A)' # pick
    SLICE_X = '(slice, A)' # slice
    COOK_X = '(cook, A)'
    g.add_subtasks([PICKUP_X, SLICE_X, COOK_X])

    # Add each option one-by-one
    OP_PICKUP_X = '(op_pickup, A)'
    g.add_option(
      name=OP_PICKUP_X,
      precondition=g[PICKUPABLE_X],
      effect=g[PICKUP_X]
    )
    # Slice option
    OP_SLICE_X = '(op_slice, A)'
    g.add_option(
      name=OP_SLICE_X,
      precondition=g[SLICEABLE_X] & g[PICKUP_X],
      effect=g[SLICE_X]
    )
    # Cook option
    OP_COOK_X = '(op_cook, A)'
    AND1 = g[COOKABLE_X] & g[PICKUP_X] & (~g[NEEDSLICE_X])
    AND2 = g[COOKABLE_X] & g[PICKUP_X] & g[SLICE_X]
    g.add_option(
      name=OP_COOK_X,
      precondition=AND1|AND2,
      effect=g[COOK_X]
    )
    return g

  def _sample_param_set(self, seed, graph_param):
    if graph_param == 'train':
      params = TRAIN_PARAMS
    elif graph_param == 'eval':
      params = EVAL_PARAMS
    else:
      raise ValueError(f"Error! Got unknown graph_param: {graph_param}")
    return params

  # Manually set
  def _set_reward_and_termination(self):
    if self.graph_param == 'train':
      self._add_reward(name='(cook, beef)', reward=1.0, terminal=True, success=True)
    elif self.graph_param == 'eval':
      self._add_reward(name='(cook, pork)', reward=1.0, terminal=True, success=True)

  # Manually set
  def _construct_initial_completion(self, unrolled_graph):
    # Initialize
    completion = OrderedDict()
    for node in unrolled_graph.subtask_nodes:
      completion[node.name] = False

    # Nothing is completed in the beginning
    return completion

  def _construct_feature_positives(self):
    return FEATURE_POSITIVE_NAME_PARAMS

  def _perturb_subtasks(self, rng):
    return


#!/usr/bin/python

import os ; import numpy as np ; import datetime as dt ; from dateutil import tz, rrule ;
#import matplotlib.pyplot as plt; #import seaborn as sns ;

date_list=[x.strftime('%Y%m%d') for x in rrule.rrule(rrule.DAILY,dtstart=dt.datetime.strptime('20170601','%Y%m%d'),until=dt.datetime.strptime('20170630','%Y%m%d'))]
l_idx_s=6; l_idx_e=30 ;

for dte in date_list[:]:

    main='/home/vkvalappil/Data/' ; date=dte+'06' ;
    output=main+'modelWRF/ARW/output/gsi_plots/rad_sens/amsua_iasi_J/' +str(l_idx_s)+'_'+str(l_idx_e)+'hour/'+date+'/'
    if not os.path.exists(output)   :
        os.makedirs(output)

    gsiFile_1=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/amsua_J/hourly'+date+'_d02_amsua_J.csv'
    gsiFile_2=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/iasi_J/hourly'+date+'_d02_iasi_J.csv'
    gsiFile_3=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/amsua+iasi_J/hourly'+date+'_d02_amsua+iasi_J.csv'

    #gsiFile_3=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_iasi.csv'
    #gsiFile_5=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_seviri.csv'
    #gsiFile_4=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+iasi.csv'
    #gsiFile_7=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+hirs.csv'
#   gsiFile_8=main+'modelWRF/ARW/output/GFS_ENKF/gsi_opp/newBe/gsi_radiance_sensitivity/'+date+'/hourly'+date+'_d02_amsua+mhs.csv'

    #gsiFile=main+'modelWRF/oppModel/ARW/output/hourly'+date+'_d02.csv'

    nogFile=main+'modelWRF_2/ARW/output/no_gsi_0.25/hourly'+date+'_d02.csv'

    lstFile=main+'modelWRF/scripts/master_uae.csv'

    lst_f=np.genfromtxt(lstFile,delimiter=',',dtype='S')
    g_data_1=np.genfromtxt(gsiFile_1,delimiter=',',dtype='S') ;
    g_data_2=np.genfromtxt(gsiFile_2,delimiter=',',dtype='S') ;
    g_data_3=np.genfromtxt(gsiFile_3,delimiter=',',dtype='S') ;

    #g_data_4=np.genfromtxt(gsiFile_4,delimiter=',',dtype='S') ;
    #g_data_5=np.genfromtxt(gsiFile_5,delimiter=',',dtype='S') ;
    #g_data_6=np.genfromtxt(gsiFile_6,delimiter=',',dtype='S') ;
    #g_data_7=np.genfromtxt(gsiFile_7,delimiter=',',dtype='S') ;
    #g_data_8=np.genfromtxt(gsiFile_8,delimiter=',',dtype='S') ;

    ng_data=np.genfromtxt(nogFile,delimiter=',',dtype='S')

    temp_cons_bias=np.empty((0,5)) ; rh_cons_bias=np.empty((0,5)) ;
    dt_cons_bias=np.empty((0,5)) ; mr_cons_bias=np.empty((0,5)) ;
    temp_cons_mae=np.empty((0,5)) ; temp_cons_rmse=np.empty((0,5)) ;
    rh_cons_mae=np.empty((0,5)) ; rh_cons_rmse=np.empty((0,5)) ;
    dt_cons_mae=np.empty((0,5)) ; dt_cons_rmse=np.empty((0,5)) ;
    mr_cons_mae=np.empty((0,5)) ; mr_cons_rmse=np.empty((0,5)) ;

    for ii in range(0,lst_f.shape[0]) :
        tid=lst_f[ii,0] ; locNme=lst_f[ii,-1]  ;
        g_req_data_1=g_data_1[np.where(g_data_1[:,0]==tid)[0],:]
        g_req_data_2=g_data_2[np.where(g_data_2[:,0]==tid)[0],:]
        g_req_data_3=g_data_3[np.where(g_data_3[:,0]==tid)[0],:]

    #    g_req_data_4=g_data_4[np.where(g_data_4[:,0]==tid)[0],:]
    #    g_req_data_5=g_data_5[np.where(g_data_5[:,0]==tid)[0],:]
    #    g_req_data_6=g_data_6[np.where(g_data_6[:,0]==tid)[0],:]
    #    g_req_data_7=g_data_7[np.where(g_data_7[:,0]==tid)[0],:]
    #    g_req_data_8=g_data_8[np.where(g_data_8[:,0]==tid)[0],:]

        ng_req_data=ng_data[np.where(ng_data[:,0]==tid)[0],:]

        f_date=g_req_data_1[:,3][::24] ; date_1=dt.datetime.strptime(f_date[0],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d')
        date_2=dt.datetime.strptime(f_date[1],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d') ;
        date_3=dt.datetime.strptime(f_date[2],'%Y-%m-%d %H:%M:%S').strftime('%Y%m%d');

        rs_file_1='/home/vkvalappil/Data/metarData/'+date_1[0:6]+'/'+locNme+'_'+date_1+'.csv'
        rs_file_2='/home/vkvalappil/Data/metarData/'+date_2[0:6]+'/'+locNme+'_'+date_2+'.csv'
        rs_file_3='/home/vkvalappil/Data/metarData/'+date_3[0:6]+'/'+locNme+'_'+date_3+'.csv'

        rs_data_1=np.genfromtxt(rs_file_1,delimiter=',',dtype='S')
        rs_data_2=np.genfromtxt(rs_file_2,delimiter=',',dtype='S')
        rs_data_3=np.genfromtxt(rs_file_3,delimiter=',',dtype='S')

        rs_data=np.concatenate([rs_data_1[1:,:],rs_data_2[1:,:],rs_data_3[1:,:]],axis=0)

        from_zone = tz.gettz('Asia/Dubai') ; to_zone = tz.gettz('UTC') ;
        date_list=np.array([(dt.datetime.strptime(x,'%Y-%m-%d %H:%M:%S')).replace(tzinfo=from_zone).astimezone(to_zone).strftime('%Y-%m-%d %H:%M') for x in g_req_data_1[:,3]]);
        rs_req_data=rs_data[np.where(np.in1d(rs_data[:,1],date_list))[0],:]

##################################################Temperature ##########################################################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,5].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,5].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,5].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,5].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,5].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,5].astype(float) ; obs=rs_req_data[l_idx_s:l_idx_e,4].astype(float)

        temp_bias_1=(data_1-obs).mean() ;
        temp_bias_2=(data_2-obs).mean() ;
        temp_bias_3=(data_3-obs).mean() ;
        temp_bias_ng=(data_ng-obs).mean() ;

        temp_mae_1=np.abs(data_1-obs).mean() ;
        temp_mae_2=np.abs(data_2-obs).mean() ;
        temp_mae_3=np.abs(data_3-obs).mean() ;

    #    temp_mae_4=np.abs(data_4-obs).mean() ;
        temp_mae_ng=np.abs(data_ng-obs).mean() ;

        temp_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        temp_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        temp_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    temp_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        temp_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        temp_bias=np.vstack([locNme,temp_bias_1,temp_bias_2,temp_bias_3,temp_bias_ng]).T
        temp_mae=np.vstack([locNme,temp_mae_1,temp_mae_2,temp_mae_3,temp_mae_ng]).T
        temp_rmse=np.vstack([locNme,temp_rmse_1,temp_rmse_2,temp_rmse_3,temp_rmse_ng]).T

        temp_cons_bias=np.concatenate([temp_cons_bias,temp_bias],axis=0)
        temp_cons_mae=np.concatenate([temp_cons_mae,temp_mae],axis=0)
        temp_cons_rmse=np.concatenate([temp_cons_rmse,temp_rmse],axis=0)

##########################################Relative Hum #############################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,7].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,7].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,7].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,7].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,7].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,7].astype(float) ; obs=np.round(rs_req_data[l_idx_s:l_idx_e,6].astype(float))

        rh_bias_1=(data_1-obs).mean() ;
        rh_bias_2=(data_2-obs).mean() ;
        rh_bias_3=(data_3-obs).mean() ;
        rh_bias_ng=(data_ng-obs).mean()

        rh_mae_1=np.abs(data_1-obs).mean() ;
        rh_mae_2=np.abs(data_2-obs).mean() ;
        rh_mae_3=np.abs(data_3-obs).mean() ;

    #    rh_mae_4=np.abs(data_4-obs).mean() ;
        rh_mae_ng=np.abs(data_ng-obs).mean() ;

        rh_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        rh_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        rh_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    rh_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        rh_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        rh_bias=np.vstack([locNme,rh_bias_1,rh_bias_2,rh_bias_3,rh_bias_ng]).T
        rh_mae=np.vstack([locNme,rh_mae_1,rh_mae_2,rh_mae_3,rh_mae_ng]).T
        rh_rmse=np.vstack([locNme,rh_rmse_1,rh_rmse_2,rh_rmse_3,rh_rmse_ng]).T

        rh_cons_mae=np.concatenate([rh_cons_mae,rh_mae],axis=0)
        rh_cons_rmse=np.concatenate([rh_cons_rmse,rh_rmse],axis=0)
        rh_cons_bias=np.concatenate([rh_cons_bias,rh_bias],axis=0)

###################################################Dew temp ######################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,6].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,6].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,6].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,6].astype(float) ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,6].astype(float) ;

        data_ng=ng_req_data[l_idx_s:l_idx_e,6].astype(float) ; obs=np.round(rs_req_data[l_idx_s:l_idx_e,5].astype(float))

        dt_bias_1=(data_1-obs).mean() ;
        dt_bias_2=(data_2-obs).mean() ;
        dt_bias_3=(data_3-obs).mean() ;
        dt_bias_ng=(data_ng-obs).mean() ;

        dt_mae_1=np.abs(data_1-obs).mean() ;
        dt_mae_2=np.abs(data_2-obs).mean() ;
        dt_mae_3=np.abs(data_3-obs).mean() ;

#       dt_mae_4=np.abs(data_4-obs).mean() ;
        dt_mae_ng=np.abs(data_ng-obs).mean() ;

        dt_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        dt_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        dt_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

#    dt_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        dt_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        dt_bias=np.vstack([locNme,dt_bias_1,dt_bias_2,dt_bias_3,dt_bias_ng]).T
        dt_mae=np.vstack([locNme,dt_mae_1,dt_mae_2,dt_mae_3,dt_mae_ng]).T
        dt_rmse=np.vstack([locNme,dt_rmse_1,dt_rmse_2,dt_rmse_3,dt_rmse_ng]).T

        dt_cons_mae=np.concatenate([dt_cons_mae,dt_mae],axis=0)
        dt_cons_rmse=np.concatenate([dt_cons_rmse,dt_rmse],axis=0)
        dt_cons_bias=np.concatenate([dt_cons_bias,dt_bias],axis=0)

############################################### Mix ratio #######################################################

        data_1=g_req_data_1[l_idx_s:l_idx_e,10].astype(float) ;
        data_2=g_req_data_2[l_idx_s:l_idx_e,10].astype(float) ;
        data_3=g_req_data_3[l_idx_s:l_idx_e,10].astype(float) ;

    #    data_4=g_req_data_4[l_idx_s:l_idx_e,10].astype(float) ;
    #    data_5=g_req_data_5[l_idx_s:l_idx_e,10].astype(float) ;
    #    data_6=g_req_data_6[l_idx_s:l_idx_e,10].astype(float)  ;
    #    data_7=g_req_data_7[l_idx_s:l_idx_e,10].astype(float)  ;
    #    data_8=g_req_data_8[l_idx_s:l_idx_e,10].astype(float)  ;
    #

        data_ng=ng_req_data[l_idx_s:l_idx_e,10].astype(float) ; obs=rs_req_data[l_idx_s:l_idx_e,11].astype(float)

        mr_bias_1=np.abs(data_1-obs).mean() ;
        mr_bias_2=np.abs(data_2-obs).mean() ;
        mr_bias_3=np.abs(data_3-obs).mean() ;
        mr_bias_ng=np.abs(data_ng-obs).mean() ;

        mr_mae_1=np.abs(data_1-obs).mean() ;
        mr_mae_2=np.abs(data_2-obs).mean() ;
        mr_mae_3=np.abs(data_3-obs).mean() ;

    #    mr_mae_4=np.abs(data_4-obs).mean() ;
        mr_mae_ng=np.abs(data_ng-obs).mean() ;

        mr_rmse_1=np.sqrt(np.square(data_1-obs).mean()) ;
        mr_rmse_2=np.sqrt(np.square(data_2-obs).mean()) ;
        mr_rmse_3=np.sqrt(np.square(data_3-obs).mean()) ;

    #    mr_rmse_4=np.sqrt(np.square(data_4-obs).mean()) ;
        mr_rmse_ng=np.sqrt(np.square(data_ng-obs).mean()) ;

        mr_bias=np.vstack([locNme,mr_bias_1,mr_bias_2,mr_bias_3,mr_bias_ng]).T
        mr_mae=np.vstack([locNme,mr_mae_1,mr_mae_2,mr_mae_3,mr_mae_ng]).T
        mr_rmse=np.vstack([locNme,mr_rmse_1,mr_rmse_2,mr_rmse_3,mr_rmse_ng]).T

        mr_cons_mae=np.concatenate([mr_cons_mae,mr_mae],axis=0)
        mr_cons_rmse=np.concatenate([mr_cons_rmse,mr_rmse],axis=0)
        mr_cons_bias=np.concatenate([mr_cons_bias,mr_bias],axis=0)

    header='locname,amsua,iasi,iasi+amsua,noDa' ;
    header=np.vstack(np.array(header.split(","))).T

    temp_cons_bias=np.concatenate([header,temp_cons_bias],axis=0)
    rh_cons_bias=np.concatenate([header,rh_cons_bias],axis=0)
    dt_cons_bias=np.concatenate([header,dt_cons_bias],axis=0)
    mr_cons_bias=np.concatenate([header,mr_cons_bias],axis=0)

    temp_cons_mae=np.concatenate([header,temp_cons_mae],axis=0)
    rh_cons_mae=np.concatenate([header,rh_cons_mae],axis=0)
    dt_cons_mae=np.concatenate([header,dt_cons_mae],axis=0)
    mr_cons_mae=np.concatenate([header,mr_cons_mae],axis=0)

    temp_cons_rmse=np.concatenate([header,temp_cons_rmse],axis=0)
    rh_cons_rmse=np.concatenate([header,rh_cons_rmse],axis=0)
    dt_cons_rmse=np.concatenate([header,dt_cons_rmse],axis=0)
    mr_cons_rmse=np.concatenate([header,mr_cons_rmse],axis=0)

    outFile_mr_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_mixr_2m_'+date+'.csv'
    outFile_dt_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_dewtemp_2m_'+date+'.csv'
    outFile_rh_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_rh_2m_'+date+'.csv'
    outFile_temp_bias=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'bias_temperature_2m_'+date+'.csv'

    outFile_mr_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_mixr_2m_'+date+'.csv'
    outFile_dt_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_dewtemp_2m_'+date+'.csv'
    outFile_rh_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_rh_2m_'+date+'.csv'
    outFile_temp_mae=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'mae_temperature_2m_'+date+'.csv'

    outFile_mr_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_mixr_2m_'+date+'.csv'
    outFile_dt_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_dewtemp_2m_'+date+'.csv'
    outFile_rh_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_rh_2m_'+date+'.csv'
    outFile_temp_rmse=output+'/'+str(l_idx_s)+'_'+str(l_idx_e)+'rmse_temperature_2m_'+date+'.csv'

    np.savetxt(outFile_temp_bias,temp_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_bias,rh_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_bias,dt_cons_bias,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_bias,mr_cons_bias,delimiter=',',fmt='%s')

    np.savetxt(outFile_temp_mae,temp_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_mae,rh_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_mae,dt_cons_mae,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_mae,mr_cons_mae,delimiter=',',fmt='%s')

    np.savetxt(outFile_temp_rmse,temp_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_rh_rmse,rh_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_dt_rmse,dt_cons_rmse,delimiter=',',fmt='%s')
    np.savetxt(outFile_mr_rmse,mr_cons_rmse,delimiter=',',fmt='%s')

#################################################################################################################


import collections
import sys

inputs = sys.stdin.readline
N = int(inputs())
counseling_info = [list(map(int, inputs().split())) for _ in range(N)]

def solution(n, info):
    adj_list = collections.defaultdict(tuple)
    for d, (t, p) in enumerate(info):
        adj_list[d + 1] = (t, p)

    ret = 0

    def dfs(curr_day, curr_sum):
        nonlocal adj_list, n, ret

        if curr_day == n:
            ret = max(curr_sum, ret)
            return

        dfs(curr_day + 1, curr_sum)

        if curr_day + adj_list[curr_day + 1][0] <= n:
            dfs(curr_day + adj_list[curr_day + 1][0], curr_sum + adj_list[curr_day + 1][1])

    dfs(curr_day=0, curr_sum=0)

    return ret

ret = solution(N, counseling_info)
print(ret)


# -*- coding: utf-8 -*-
"""
Created on Sun Apr  7 20:07:53 2019

@author: Jerry Xing
"""
import numpy as np
def backprop(W, aas,zzs, yTr,  trans_func_der):
#% function [gradient] = backprop(W, aas, zzs, yTr,  der_trans_func)
#%
#% INPUT:
#% W weights (list of ndarray)
#% aas output of forward pass (list of ndarray)
#% zzs output of forward pass (list of ndarray)
#% yTr 1xn ndarray (each entry is a label)
#% der_trans_func derivative of transition function to apply for inner layers
#%
#% OUTPUTS:
#%
#% gradient = the gradient at w as a list of ndarries
#%

    n = np.shape(yTr)[1]
    delta = zzs[0] - yTr
    # compute gradient with back-prop

    gradient = [None] * len(W)
    # err = delta/np.float(n)

    for i in range(len(W)):
        # INSERT CODE HERE:
        # gradient[i] = np.dot(zzs[i].T, err)
        # err = np.multiply(np.dot(W[i].T, err), trans_func_der(aas[i]))

        gradient[i] = delta @ ((zzs[i + 1]).T) / n
        delta = np.multiply((W[i][:, :-1]).T @ delta, trans_func_der(aas[i + 1]))

    return gradient


from flask import Flask
from flask_jsonpify import jsonify
import json
from flask_cors import CORS

i = ''
i += 'Franziska '
i += 'Romani '
i += 'Furtado'

app = Flask(__name__)
#app.run(port = 5000)
CORS(app)
#obj = json.loads(i)
with open('teste.json', 'w') as myfile:
    json.dump(i , myfile)

@app.route("/")
def hello():
    return jsonify(i)

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


from scrapy.crawler import CrawlerProcess

import datetime
import os
import sys

from Demo.spiders.example import ExampleSpider
from common import mongodb_pool
from init import log_dir, base_dir, scrapy_settings, local_host_ip

def start_scrapy(environ='dev', work_id=None, work_name=None):
    LOG_LEVEL_DICT = {
        "dev": "INFO",
        "test": "INFO",
        "rel": "INFO",
    }

    log_level = LOG_LEVEL_DICT[environ]
    if work_id is None:
        work_id = local_host_ip
    print("worker_id: ", work_id)
    if work_name:
        log_file = work_name + "demo" + datetime.datetime.now().strftime(".%Y%m%d.log")
    else:
        log_file = "demo" + datetime.datetime.now().strftime(".%Y%m%d.log")
    log_path = os.path.join(log_dir, log_file)
    scrapy_settings.set('LOG_LEVEL', log_level)
    scrapy_settings.set('LOG_FILE', log_path)
    print("base_dir: ", base_dir)
    print("log_file: ", log_path)
    scrapy_mongodb_db = scrapy_settings.getdict('SCRAPY_MONGODB_DATABASE')[environ]
    scrapy_settings.set('SCRAPY_DATABASE', scrapy_mongodb_db)
    mongodb_cfg = scrapy_settings.getdict('SCRAPY_MONGODB_CONFIG')[environ]
    print("mongodb_cfg:", mongodb_cfg)
    scrapy_settings.set('SCRAPY_WORKER_ID', work_id)
    mongodb_pool.init_mongodb(mongodb_cfg)

    process = CrawlerProcess(scrapy_settings)
    process.crawl(ExampleSpider)

    process.start()

if __name__ == '__main__':
    print("Begin......")
    if len(sys.argv) == 1:
        start_scrapy()
    elif len(sys.argv) >= 3:
        environ = sys.argv[1]
        work_id = sys.argv[2]
        work_name = sys.argv[3]
        start_scrapy(environ, work_id, work_name)


import random
myRandom = random.randrange(1,11)
k=1
correct = False
print("Welcome to Guess the number.")
print("This game will ramdom number 1 to 10.")
print("You only have 3 chances to guess the number.")
while True:
    number = int(input("Enter your answer =====>  "))
    correct = (number == myRandom)
    if not correct:
        if number>myRandom:
            print("It's greater.")
        if number<myRandom:
            print("It's lower.")
    if correct :
        print("You Win!!")
        break
    if number < 0 or k == 3 or number > myRandom:
        break
    k+=1
if not correct:
    print("Try again.")
    print("The answer is ",myRandom)


import theano.tensor as tt
import lasagne as lnn

from . import dnn

def compute_loss(prediction, target):
    # need to clip predictions for numerical stability
    eps = 1e-7
    pred_clip = tt.clip(prediction, eps, 1.-eps)
    return lnn.objectives.binary_crossentropy(pred_clip, target).mean()

def build_net(in_shape, out_size_chroma, out_size, model):
    # first, stack the dnn chroma extractor
    chroma_network, input_var, crm_target_var = dnn.build_net(
        in_shape, out_size_chroma, model
    )

    # then, add the logistic regression chord classifier
    crd_target_var = tt.matrix('target_output', dtype='float32')

    chord_network = lnn.layers.DenseLayer(
        chroma_network, name='chords', num_units=out_size,
        nonlinearity=lnn.nonlinearities.softmax)

    # tag chord classification parameters so we can distinguish them later
    for p in chord_network.get_params():
        chord_network.params[p].add('chord')

    return (chroma_network, chord_network,
            input_var, crm_target_var, crd_target_var)

def build_model(in_shape, out_size_chroma, out_size, model):
    (crm, crd, inv, crmv, crdv) = build_net(in_shape, out_size_chroma,
                                            out_size, model)
    return dict(chroma_network=crm, chord_network=crd,
                input_var=inv, chroma_target_var=crmv, chord_target_var=crdv,
                chroma_loss_fn=compute_loss,
                chord_loss_fn=dnn.categorical_crossentropy)

create_iterators = dnn.create_iterators

def add_sacred_config(ex):

    # =============================================================== dense net

    ex.add_named_config(
        name='dense_net',
        datasource=dict(
            context_size=7,
        ),
        chroma_network=dict(
            model=dict(
                type='chroma_dnn',
                dense=dict(
                    num_layers=3,
                    num_units=512,
                    dropout=0.5,
                    nonlinearity='rectify',
                    batch_norm=False,
                ),
                out_nonlinearity='sigmoid'
            ),
            optimiser=dict(
                name='adam',
                params=dict(
                    learning_rate=0.0001
                ),
                schedule=None
            ),
            training=dict(
                iterator='BatchIterator',
                batch_size=512,
                num_epochs=500,
                early_stop=20,
                early_stop_acc=False,
            ),
            regularisation=dict(
                l2=1e-4,
                l1=0.0,
            ),
        ),
        optimiser=dict(
            name='adam',
            params=dict(
                learning_rate=0.001
            ),
            schedule=None
        ),
        training=dict(
            iterator='BatchIterator',
            batch_size=512,
            num_epochs=500,
            early_stop=20,
            early_stop_acc=True
        ),
        regularisation=dict(
            l2=1e-4,
            l1=0.0,
        ),
        testing=dict(
            test_on_val=False,
            batch_size=None
        )
    )


import sys

def count(arr):
    '''
    get length of longest contigious sub array which has all unique elements
    avoid duplicates
    '''
    max_len = 0
    for i in range(len(arr)):
        s = set()
        _min = arr[i]
        _max = arr[i]
        s.add(arr[i])
        for j in range(i+1, len(arr)):
            if arr[j] in s:
                break
            s.add(arr[j])
            _min = min(_min, arr[j])
            _max = max(_max, arr[j])
            if (_max - _min == j-i):
                leng = j-i+1
                if leng > max_len:
                    max_len = leng
    print(max_len)

arr = [10, 12, 11]
count(arr)


import matplotlib.pyplot as plt

from sdaudio.callables import Circular
from sdaudio.callables import Constant
from sdaudio import draw
from sdaudio import wavio
from sdaudio.wt_oscillators import Choruses

def main():

    #-------------------------------------------------------------------------
    # sawtooth demo

    print("Generating 60 Hz sawtooth, no chorus")

    sr = 8000
    dur = 7.0

    freqs = draw.line(sr, dur, 60, 60)

    x = draw.sawtooth(sr, dur, Circular(freqs), n = 5)

    plt.figure()
    plt.plot(x)
    plt.xlim([0, 3000])
    plt.grid(True)
    plt.title('Sawtooth, n = 5, no chorus')

    fout = 'saw-no-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    #-------------------------------------------------------------------------
    # sawtooth oscillator with chorus

    print("Generating 60 Hz sawtooth, with chorus")

    table = draw.sawtooth(sr, 1.0, Constant(1.0))

    chorus = [0.99, 1.0, 1.01]

    chorus = [0.97, 1.0, 1.03]

    chorus = [0.991234134, 1.012983475290375]

    gen = Choruses(sr, table, chorus)

    x = gen.generate(dur, Circular(freqs))

    plt.figure()
    plt.plot(x)
    plt.xlim([0, 3000])
    plt.grid(True)
    plt.title('Sawtooth, n = 5, with chorus')

    fout = 'saw-with-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    #-------------------------------------------------------------------------
    # freq ramp

    print("Generating sawtooth ramp, no chorus")

    freqs = draw.line(sr, dur, 40, 200)

    x = draw.sawtooth(sr, dur, Circular(freqs))

    fout = 'saw-ramp-no-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    print("Generating sawtooth ramp, with chorus")

    x = gen.generate(dur, Circular(freqs))

    fout = 'saw-ramp-with-chorus.wav'

    print("Writing: %s" % fout)

    wavio.write(fout, 0.666 * x, sr)

    plt.show()

if __name__ == "__main__":
    main()


import os
import time
import pytest
from models import Event
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app import create_app

SECRET = 'Isecret'
DB_NAME = 'test.db'

@pytest.fixture
def client():
    db_url = "sqlite:///" + DB_NAME
    app = create_app(SECRET, db_url)
    app.testing = True
    yield app.test_client()
    os.unlink(DB_NAME)

@pytest.fixture
def sess():
    db_url = "sqlite:///" + DB_NAME
    engine = create_engine(db_url)
    sess = sessionmaker(bind=engine)
    yield sess()

def _test_status(code, resp):
    assert resp.status_code == code

ROUTES = [
    ('/query_name', 'post'),
    ('/add_event', 'put')
]

def test_no_auth(client):
    for route, method in ROUTES:
        _test_status(401, getattr(client, method)(route))

def test_bad_auth(client):
    for route, method in ROUTES:
        _test_status(401, getattr(client, method)(route, data={'SECRET_KEY':'sup'}))

def test_good_auth(client):
    for route, method in ROUTES:
        _test_status(200, getattr(client, method)(route,
                                                  data={'SECRET_KEY':SECRET,
                                                        'name':"haha"}))
def test_count(client):
    data = {'SECRET_KEY':SECRET,'name':"haha"}
    for i in range(3):
        client.put('/add_event', data=data)
    assert client.post('/query_name', data=data).json == 1
    time.sleep(1)
    data['window'] = 1e-7
    assert client.post('/query_name', data=data).json == 0

def test_multiple_keys(client):
    data = {'SECRET_KEY':SECRET}
    names = ['haha', 'sup', 'todo']
    for name in names:
        data['name'] = name
        client.put('/add_event', data=data)
    for name in names:
        data['name'] = name
        assert client.post('/query_name', data=data).json == 1

def test_unique(client, sess):
    """test failure"""
    data = {'SECRET_KEY':SECRET,'name':"haha"}
    events = [Event(name=data['name'], time=Event.gen_dt(i % 3))
              for i in range(6)]
    sess.add_all(events)
    sess.commit()
    assert client.post('/query_name', data=data).json == 3


import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import mean_absolute_error
import time
import warnings
warnings.filterwarnings('ignore')

# 导入数据
tr_data = pd.read_csv('xfdata/房屋租赁价格预测挑战赛公开数据/train.csv')
te_data = pd.read_csv('xfdata/房屋租赁价格预测挑战赛公开数据/test.csv')

data = pd.concat([tr_data, te_data], ignore_index=True)

print("#"*10 + "进行特征工程..." + "#"*10)
# 时间处理
data["建成年份"] = data["建成年份"].apply(lambda x: int(x) if 1811 <= x <= 2020 else np.nan)
data["最后翻新年份"] = data["最后翻新年份"].apply(lambda x : int(x) if 1990<= x <= 2020 else np.nan)
data["上传年份"] = data["上传日期"].replace({"Feb20": 2020, "May19": 2019,"Oct19": 2019,"Sep18": 2018})   # 2019年有两个月份
data["上传月份"] = data["上传日期"].replace({"Feb20": 3, "May19": 6, "Oct19": 10,"Sep18": 9})

data["上传-建成年差"] = data["上传年份"] - data["建成年份"]
data["最后翻新-建成年差"] = data["最后翻新年份"] - data["建成年份"]
data["上传-最后翻新年差"] = data["上传年份"] - data["最后翻新年份"]

# 处理负值的上传年差：
data["上传-建成年差"] = data["上传-建成年差"].apply(lambda x: x if x >=0 else np.nan)
data["最后翻新-建成年差"] = data["最后翻新-建成年差"].apply(lambda x: x if x >=0 else np.nan)
data["上传-最后翻新年差"] = data["上传-最后翻新年差"].apply(lambda x: x if x >=0 else np.nan)

# 费用、房间面积
data["房间数量"] = data["房间数量"].apply(lambda x: int(x) if 1 <= x <= 8 else 8)

data["供暖费用"] = data["供暖费用"].apply(lambda x : x if x <= 198 else np.nan)
data["服务费"] = data["服务费"].apply(lambda x : x if x <= 1000 else np.nan)

data["log_供暖费用"] = np.log(data["供暖费用"] + 1)         # # 供暖费相关性要比log_供暖费强
data["log_服务费"] = np.log(data["服务费"]+1)         # 服务费相关性要比log_服务费强

# 居住面积有0，这是问题的，但是test中也有，总不能删掉test吧？
data["居住面积"] = data["居住面积"].apply(lambda x: x if 10<=x<=500 else np.nan)  # 再议
data["log_居住面积"] = np.log(data["居住面积"])         # 居住面积的异常值感觉要处理，7/15 去除+1，已经没有0了

# 处理房屋租金
data.drop(data[(data["房屋租金"]>=10000)|(data["房屋租金"]<=100)].index, inplace=True)
data["log_房屋租金"] = np.log(data["房屋租金"]+1)   # 还有必要+1吗？

data["单位log面积服务费"] = data["服务费"] / data["log_居住面积"]
data["单位面积服务费"] = data["服务费"] / data["居住面积"]
data["单位房间服务费"] = data["服务费"] / data["房间数量"]

data["单位log面积上传图片数"] = data["上传图片数"] / data["log_居住面积"]
data["单位面积上传图片数"] = data["上传图片数"] / data["居住面积"]
data["单位房间上传图片数"] = data["上传图片数"] / data["房间数量"]

# 楼层处理
def judge_use_GB50096(x):
    # 使用住宅设计规范判定楼层：低层、多层、中高层、高层，外加地下室
    if x == 10086:   # 缺失值
        return np.nan
    elif x <= 0:
        return 0
    elif 1<=x and x<=3:
        return 1
    elif 4<=x and x<=6:
        return 2
    elif 7<=x and x<=9:      # & 是位运算符，and才是对于的逻辑运算符，python中建议常用逻辑运算符
        return 3
    else:
        return 4

data["所处楼层"] = data["所处楼层"].apply(lambda x: int(x) if x <= 31 else np.nan) # 31  # 由异常值函数得到下限10
data["建筑楼层"] = data["建筑楼层"].apply(lambda x : int(x) if x <= 54 else np.nan) # 54 # 由函数得到下限11
data["楼层占比"] = data["所处楼层"] / data["建筑楼层"]   # -1 地下一层
data["楼层占比"] = data["楼层占比"].apply(lambda x: x if np.abs(x) <= 1 else np.nan)
# 划分等级
data["所处楼层划分"] = data["所处楼层"].fillna(10086).apply(lambda x: judge_use_GB50096(x))
data["建筑楼层划分"] = data["建筑楼层"].fillna(10086).apply(lambda x: judge_use_GB50096(x))

# 电梯效应，所在楼层大于7楼没电梯，影响比1楼没电梯大
# <=11, 12<=18 19<=33(31)
# data["电梯效应"] = data.apply(lambda x: x["所处楼层"] if np.exp(x["有电梯"]+1) else -np.exp(x["所处楼层"]+1), axis=1)   # axis=1将行传入apply

# 其他
data["可带宠物_2"] = data["可带宠物"].replace({"negotiable": 2, "no": 1, "yes": 3})

# 类型特征编码
# 有缺失值的特征不适于one-hot，会重叠其中一类和缺失值
data = pd.get_dummies(data, columns=['上传月份'], drop_first=True)

# 特征统计量 ["区域1",'区域2', '区域3', '街道']
# 服务费         6385
# 所处楼层       47743
# 建筑楼层       90806
# 价格趋势        1699
# 房间数量           0
# 居住面积     本无缺失
temp = data.groupby("区域1")['服务费'].agg([('区域1_服务费_mean','mean'), ('区域1_服务费_std','std')])   # 这么高级的吗？
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域1', how='left')

temp = data.groupby("区域2")['服务费'].agg([('区域2_服务费_mean','mean'), ('区域2_服务费_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域2', how='left')

temp = data.groupby("区域3")['服务费'].agg([('区域3_服务费_mean','mean'), ('区域3_服务费_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域3', how='left')

temp = data.groupby("街道")['服务费'].agg([('街道_服务费_mean','mean'), ('街道_服务费_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='街道', how='left')

# 增加居住面积
temp = data.groupby("区域1")['居住面积'].agg([('区域1_居住面积_mean','mean'), ('区域1_居住面积_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域1', how='left')

temp = data.groupby("区域2")['居住面积'].agg([('区域2_居住面积_mean','mean'), ('区域2_居住面积_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域2', how='left')

temp = data.groupby("区域3")['居住面积'].agg([('区域3_居住面积_mean','mean'), ('区域3_居住面积_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域3', how='left')

temp = data.groupby("街道")['居住面积'].agg([('街道_居住面积_mean','mean'), ('街道_居住面积_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='街道', how='left')

# 增加更多特征统计量
temp = data.groupby("区域1")['所处楼层'].agg([('区域1_所处楼层_mean','mean'), ('区域1_所处楼层_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域1', how='left')

temp = data.groupby("区域1")['价格趋势'].agg([('区域1_价格趋势_mean','mean'), ('区域1_价格趋势_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域1', how='left')

temp = data.groupby("区域1")['房间数量'].agg([('区域1_房间数量_mean','mean'), ('区域1_房间数量_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域1', how='left')
# 1
temp = data.groupby("区域2")['所处楼层'].agg([('区域2_所处楼层_mean','mean'), ('区域2_所处楼层_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域2', how='left')

temp = data.groupby("区域2")['价格趋势'].agg([('区域2_价格趋势_mean','mean'), ('区域2_价格趋势_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域2', how='left')

temp = data.groupby("区域2")['房间数量'].agg([('区域2_房间数量_mean','mean'), ('区域2_房间数量_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域2', how='left')
# 2
temp = data.groupby("区域3")['所处楼层'].agg([('区域3_所处楼层_mean','mean'), ('区域3_所处楼层_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域3', how='left')

temp = data.groupby("区域3")['价格趋势'].agg([('区域3_价格趋势_mean','mean'), ('区域3_价格趋势_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域3', how='left')

temp = data.groupby("区域3")['房间数量'].agg([('区域3_房间数量_mean','mean'), ('区域3_房间数量_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='区域3', how='left')
# 3
temp = data.groupby("街道")['所处楼层'].agg([('街道_所处楼层_mean','mean'), ('街道_所处楼层_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='街道', how='left')

temp = data.groupby("街道")['价格趋势'].agg([('街道_价格趋势_mean','mean'), ('街道_价格趋势_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='街道', how='left')

temp = data.groupby("街道")['房间数量'].agg([('街道_房间数量_mean','mean'), ('街道_房间数量_std','std')])
temp.fillna(0, inplace=True)
data = data.merge(temp, on='街道', how='left')

l = data.columns.to_list()
ft = ["ID","房屋租金","log_供暖费用",
      "没有停车位","log_房屋租金",
      "上传日期","可带宠物","log_服务费",'log_居住面积','单位面积服务费','单位面积上传图片数']

features = [i for i in l if i not in ft]
category_fea = []
for i in ['房屋状况','内饰质量','加热类型','房屋类型','所处楼层划分', '建筑楼层划分','可带宠物_2']:
    category_fea.append(features.index(i))

train_data = data[data["房屋租金"].notnull()].reset_index(drop=True)
test_data = data[data["房屋租金"].isnull()].reset_index(drop=True)
x_train_temp = train_data[features]
y_train = train_data["log_房屋租金"]
x_test_temp = test_data[features]

# 处理种类基数较多的分类变量
from category_encoders import TargetEncoder
import pandas as pd
enc = TargetEncoder(cols=["区域1",'区域2', '区域3', '街道',"邮政编码"])
# transform the datasets
x_train = enc.fit_transform(x_train_temp, y_train)
x_test = enc.transform(x_test_temp)

# 保存x_test，用于预测
x_test.to_csv("user_data/tmp_data/x_test.csv", index=False)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print(l)
print("\n")
print(features)
print("all_len: {}, fea_len: {}".format(len(l),len(features)))
print("train_data_num: {}, test_data_num: {}".format(len(train_data),len(test_data)))
print("category_fea: ", category_fea)
print("#"*10 + "特征工程已完成！" + "#"*10)
print("\n\n")
print("#"*10 + "进行训练模型..." + "#"*10)
############################################################
# 训练模型
def cv_model(clf, train_x, train_y, test_x, clf_name):
    folds = 10
    seed = 2022
    kf = KFold(n_splits=folds, shuffle=True, random_state=seed)

    train = np.zeros(train_x.shape[0])
#     test = np.zeros(test_x.shape[0])

    cv_scores = []

    for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)):
        print('************************************ {} ************************************'.format(str(i+1)))
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        trn_x, trn_y, val_x, val_y = train_x.iloc[train_index], train_y[train_index], train_x.iloc[valid_index], train_y[valid_index]

        if clf_name == "lgb":
            train_matrix = clf.Dataset(trn_x, label=trn_y)
            valid_matrix = clf.Dataset(val_x, label=val_y)

            params = {
                'boosting_type': 'gbdt',
                'objective': 'regression',
                'metric': 'l1',          # mae mean_absolute_error

                'seed': 2022,
                'n_jobs':-1,

                'bagging_fraction': 0.9426917523072508,
                'bagging_freq': 3,
                'feature_fraction': 0.6495602688979487,
                'lambda_l1': 0.02052223766043643,
                'lambda_l2': 3.7224143113186443,
                'learning_rate': 0.03319994978090327,
                'max_bin': 158,
                'min_data_in_leaf': 26,
                'min_sum_hessian_in_leaf': 8.865519169470296,
                'num_leaves': 89,
            }

            # category_fea
            model = clf.train(params, train_matrix, 90000, valid_sets=[train_matrix, valid_matrix],
                              categorical_feature=category_fea, verbose_eval=3000, early_stopping_rounds=1000)
            val_pred = model.predict(val_x, num_iteration=model.best_iteration)
#             test_pred = model.predict(test_x, num_iteration=model.best_iteration)
            print(list(sorted(zip(features, model.feature_importance("gain")), key=lambda x: x[1], reverse=True)))
        # 保存模型
        model.save_model("user_data/model_data/lgb_model_{}.txt".format(str(i+1)))
        train[valid_index] = val_pred
#         test += test_pred / kf.n_splits     # 这里必有加号
        cv_scores.append(mean_absolute_error(val_y, val_pred))
        print(cv_scores)

    print("%s_scotrainre_list:" % clf_name, cv_scores)
    print("%s_score_mean:" % clf_name, np.mean(cv_scores))
    print("%s_score_std:" % clf_name, np.std(cv_scores))
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    return train

lgb_train = cv_model(lgb, x_train, y_train, x_test, "lgb")
# lgb_train.to_csv("user_data/tmp_data/lgb_train.csv", index=False)   # 可用于stacking，但本方案并未采取模型融合
print("#"*10 + "模型训练已完成！" + "#"*10)


"""위클리 챌린지 4주차"""

def solution(table, languages, preference):
    scores = []
    for i in range(len(table)):
        scores.append(list(table[i].split()))

    table = []
    for i in range(len(scores)):
        total = 0
        for idx in range(len(languages)):
            if languages[idx] in scores[i]:
                total += (6 - scores[i].index(languages[idx])) * preference[idx]
        table.append([scores[i][0], total])
    table.sort(key=lambda x: (-x[1], x[0]))

    return table[0][0]

"""풀이는 같으나 훨씬 간단"""
# def solution(table, languages, preference):
#    score = {}
#    for t in table:
#        for lang, pref in zip(languages, preference):
#            if lang in t.split():
#                score[t.split()[0]] = (
#                    score.get(t.split()[0], 0) + (6 - t.split().index(lang)) * pref
#                )
#    return sorted(score.items(), key=lambda item: [-item[1], item[0]])[0][0]

table = [
    "SI JAVA JAVASCRIPT SQL PYTHON C#",
    "CONTENTS JAVASCRIPT JAVA PYTHON SQL C++",
    "HARDWARE C C++ PYTHON JAVA JAVASCRIPT",
    "PORTAL JAVA JAVASCRIPT PYTHON KOTLIN PHP",
    "GAME C++ C# JAVASCRIPT C JAVA",
]
languages = ["PYTHON", "C++", "SQL"]
preference = [7, 5, 5]
print(solution(table, languages, preference))


# Comparsion models for competing risks
# In this script we train the different models for competing risks
import sys
from nfg import datasets
from experiment import *

random_seed = 0

# Open dataset
dataset = sys.argv[1] # FRAMINGHAM, SYNTHETIC_COMPETING, PBC, SEER

# Specific fold selection
fold = None
if len(sys.argv) == 3:
    fold = int(sys.argv[2])

print("Script running experiments on ", dataset)
x, t, e, covariates = datasets.load_dataset(dataset, competing = True)

# Hyperparameters
max_epochs = 1000
grid_search = 100
layers = [[i] * (j + 1) for i in [25, 50] for j in range(4)]
layers_large = [[i] * (j + 1) for i in [25, 50] for j in range(8)]

batch = [100, 250] if dataset != 'SEER' else [1000, 5000]

# DSM
param_grid = {
    'epochs': [max_epochs],
    'learning_rate' : [1e-3, 1e-4],
    'batch': batch,

    'k' : [2, 3, 4, 5],
    'distribution' : ['LogNormal', 'Weibull'],
    'layers' : layers_large,
}
DSMExperiment.create(param_grid, n_iter = grid_search, path = 'Results/{}_dsm'.format(dataset), random_seed = random_seed, fold = fold).train(x, t, e)

# NFG Competing risk
param_grid = {
    'epochs': [max_epochs],
    'learning_rate' : [1e-3, 1e-4],
    'batch': batch,

    'dropout': [0., 0.25, 0.5, 0.75],

    'layers_surv': layers,
    'layers' : layers,
    'act': ['Tanh'],
}
NFGExperiment.create(param_grid, n_iter = grid_search, path = 'Results/{}_nfg'.format(dataset), random_seed = random_seed, fold = fold).train(x, t, e)
NFGExperiment.create(param_grid, n_iter = grid_search, path = 'Results/{}_nfgcs'.format(dataset), random_seed = random_seed, fold = fold).train(x, t, e, cause_specific = True)

# Desurv
param_grid = {
    'epochs': [max_epochs],
    'learning_rate' : [1e-3, 1e-4],
    'batch': batch,

    'layers_surv': layers_large,
    'layers': layers_large,
    'act': ['Tanh'],
}
DeSurvExperiment.create(param_grid, n_iter = grid_search, path = 'Results/{}_ds'.format(dataset), random_seed = random_seed).train(x, t, e)

# DeepHit Competing risk
param_grid = {
    'epochs': [max_epochs],
    'learning_rate' : [1e-3, 1e-4],
    'batch': batch,

    'nodes' : layers,
    'shared' : layers
}
DeepHitExperiment.create(param_grid, n_iter = grid_search, path = 'Results/{}_dh'.format(dataset), random_seed = random_seed, fold = fold).train(x, t, e)


#Titulo              Ejercicio 4
#Descripcion         Diagrama para calcular el mayor de dos numeros.
#
#Importaciones
import os
import time
#
#
#Funciones - Borrar consola cls()
def cls():
    os.system('cls' if os.name=='nt' else 'clear')
#
#
#Debug - Operacion
someError = 1
#
#Menu Loop
while someError > 0:
    #
    #Enseñar Menu
    cls()
    print ("CUAL ES EL MAYOR DE DOS NUMEROS")
    print ("________________________________")
    print()
    #Variables de entrada
    primerNumero = input("Introduce el primer numero :")
    segundoNumero = input("Introduce el segundo numero :")
    #
    #Intentar realizar operacion
    try:
        #Si el primer numero es mas grande
        if float(primerNumero) > float(segundoNumero):
            #
            #Mostrar resultado
            cls()
            print (str(primerNumero) + " es mayor que " + str(segundoNumero))
            someError = 0

        #Si el segundo numero es mas grande
        elif float(primerNumero) < float(segundoNumero):
            cls()
            print (str(primerNumero) + " es menor que " + str(segundoNumero))
            someError = 0
        else:
            cls()
            print ("Los numeros son iguales")
            someError = 0
    #
    #Si la operacion no se puede llevar a cabo
    except ValueError:
        cls()
        print ("Algunos valores no son correctos")
        someError += 1
        time.sleep(5)


import cv2
import pyzbar.pyzbar as pyzbar

def decodeDisplay(image):
    barcodes = pyzbar.decode(image)
    for barcode in barcodes:
        # get location

        (x, y, w, h) = barcode.rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (225, 225, 225), 2)

        # to string
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type

        # get image type
        text = "{} ({})".format(barcodeData, barcodeType)
        cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    .5, (225, 225, 225), 2)

        # show code image type
        print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
    return image

def detect():
    camera = cv2.VideoCapture(0)

    while True:
        # get frame
        ret, frame = camera.read()
        # change color
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        im = decodeDisplay(gray)

        cv2.waitKey(5)
        cv2.imshow("camera", im)
        # quit
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    camera.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    detect()


import unittest
from client import createThread

class MyTest(unittest.TestCase):
    #tesing query partterns that are frequent
    def test_frequent(self):
        frequent = '2018-09-15'
        res = createThread('machine.log', frequent)
        length = len(res)
        for i in range(length):
            if 'is shut down' in res[i]:
                res[i] = ''
        res = ''.join(res)
        res = res.strip().split('\n')
        self.assertEqual(len(res), 90)
        self.assertTrue(res[0].startswith(frequent))

    # tesing query partterns that are infrequent
    def test_infrequent(self):
        infrequent = 'xxxx-xx-xx'
        res = createThread('machine.log', infrequent)
        length = len(res)
        for i in range(length):
            if 'is shut down' in res[i]:
                res[i] = ''
        res = ''.join(res)
        res = res.strip().split('\n')
        self.assertEqual(len(res), 10)
        self.assertTrue(res[0].startswith(infrequent))

    # tesing query partterns that are rare
    def test_rare(self):
        rare ='Thisisthelogofmachine'
        res = createThread('machine.log', rare)
        length = len(res)
        for i in range(length):
            if 'is shut down' in res[i]:
                res[i] = ''
        res = ''.join(res)
        res = res.strip().split('\n')
        self.assertEqual(len(res), 1)
        self.assertTrue(res[0].startswith(rare))

if __name__ == '__main__':
    unittest.main()


import pandas as pd
import numpy as np

data_hami = pd.read_excel(r'Book2.xlsx', sheet_name='sheet1')
columns = data_hami.columns
columns = np.ravel(columns)
columns0List = data_hami[columns[0]].tolist()
columns1List = data_hami[columns[1]].tolist()
columns2List = data_hami[columns[2]].tolist()
columns3List = data_hami[columns[3]].tolist()
columns4List = data_hami[columns[4]].tolist()

df = pd.DataFrame({columns[1]:columns1List,
                    columns[2]:columns2List,
                    columns[3]:columns3List,
                    columns[4]:columns4List},
                    index=columns0List)

df["HAMI"] = df[columns[1]] + df[columns[2]] + df[columns[3]] - df[columns[4]]

df = df.drop(columns = [columns[1], columns[2], columns[3], columns[4]])

df.to_excel("HAMI-Georgia.xlsx")


import requests
import bs4

response = requests.get("https://yandex.ru/pogoda/")
print(response.status_code)

tree = bs4.BeautifulSoup(response.text, 'html.parser')

for item in tree.select(".card"):
    obj = item.select_one('.title-icon__text')
    if (obj is not None):
        print(obj.text)

count = 0
for item in tree.select(".forecast-briefly__day"):
    res = item.find('a').attrs['aria-label']
    if (count > 2) & (count < 9):
        print(res)
    count+=1


def twoSum(nums, target):
# your code here
    for x in nums:
        nowIndex = nums.index(x)
        for y in range(nowIndex+1, len(nums) ,+1):
            if nums[nowIndex] + nums[y] == target:
                return [nowIndex, y]

result=twoSum([2, 11, 7, 15], 9)
print(result) # show [0, 2] because nums[0]+nums[2] is 9


from BinaryTreeInorderTraversal import TreeNode

class BinaryTreeMaximumPathSum:
    def maxPathSum(self, root: TreeNode) -> int:
        self.maxSum = float('-inf')
        self.maxPathSumHelper(root)
        return self.maxSum

    def maxPathSumHelper(self, node):
        if not node:
            return 0

        print(node.val)
        leftSum = max(self.maxPathSumHelper(node.left), 0)
        rightSum = max(self.maxPathSumHelper(node.right), 0)
        currMax = node.val + leftSum + rightSum
        self.maxSum = max(self.maxSum, currMax)

        return node.val + max(leftSum, rightSum)

if __name__ == '__main__':
    init = BinaryTreeMaximumPathSum()
    root = TreeNode(-10)
    root.left = TreeNode(9)
    root.right = TreeNode(20)
    root.right.left = TreeNode(15)
    root.right.right = TreeNode(7)
    print(init.maxPathSum(root))  # 42


import gi.repository

gi.require_version('Budgie', '1.0')
from gi.repository import Budgie, GObject, Gtk, Gio
import os

"""
    Budgie Pi Audio Switcher Plugin - switch audio output modes on Raspberry Pi
    Copyright (C) 2020  Samuel Lane

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

    Icons made by Freepik <https://www.flaticon.com/authors/freepik>
    from Flaticon.com <https://www.flaticon.com/>
"""

app_settings = Gio.Settings.new("com.budgie-pi.audio-switcher")

def load_settings():
    # Load the settings
    read_last = app_settings.get_string("current-mode")
    read_forced = app_settings.get_string("startup-mode")
    return read_last, read_forced

def save_settings(save_mode, save_forced):
    # saves the settings
    app_settings.set_string("current-mode",save_mode)
    app_settings.set_string("startup-mode",save_forced)

class BudgiePiAudio(GObject.GObject, Budgie.Plugin):
    """ This is simply an entry point into your Budgie Applet implementation.
        Note you must always override Object, and implement Plugin.
    """
    # Good manners, make sure we have unique name in GObject type system
    __gtype_name__ = "BudgiePiAudioSwitcher"

    def __init__(self):
        """ Initialisation is important.
        """
        GObject.Object.__init__(self)

    def do_get_panel_widget(self, uuid):
        """ This is where the real fun happens. Return a new Budgie.Applet
            instance with the given UUID. The UUID is determined by the
            BudgiePanelManager, and is used for lifetime tracking.
        """
        return BudgiePiAudioApplet(uuid)

class BudgiePiAudioSettings(Gtk.Grid):

    def __init__(self, setting):
        super().__init__()

        button_last = Gtk.RadioButton(label="Remember Last Setting")
        button_last.connect("toggled", self.toggled_cb)
        button_hdmi = Gtk.RadioButton.new_from_widget(button_last)
        button_hdmi.set_label("Always Start With HDMI Output")
        button_hdmi.connect("toggled", self.toggled_cb)
        button_hdmi.set_active(False)
        button_jack = Gtk.RadioButton.new_with_label_from_widget(
            button_last, "Always Start With 3.5mm Output")
        button_jack.connect("toggled", self.toggled_cb)
        button_jack.set_active(False)

        self.audiomode, self.forcemode = load_settings()
        if self.forcemode == 'LAST':
            button_last.set_active(True)
        elif self.forcemode == 'HDMI':
            button_hdmi.set_active(True)
        else:
            button_jack.set_active(True)

        blank = Gtk.Label(" ")
        self.set_row_spacing (10)
        self.attach(blank, 0, 0, 1, 1)
        self.attach(button_last, 0, 1, 1, 1)
        self.attach(button_hdmi, 0, 2, 1, 1)
        self.attach(button_jack, 0, 3, 1, 1)
        self.show_all()

    def toggled_cb(self, button):
        if button.get_active():
            switchtomode = button.get_label()
            if switchtomode == 'Remember Last Setting':
                forcesetting = 'LAST'
            elif switchtomode == 'Always Start With HDMI Output':
                forcesetting = 'HDMI'
            else:
                forcesetting = 'JACK'
            save_settings(self.audiomode, forcesetting)
        # else do nothing

class BudgiePiAudioApplet(Budgie.Applet):
    """ Budgie.Applet is in fact a Gtk.Bin """
    manager = None

    def __init__(self, uuid):

        Budgie.Applet.__init__(self)
        self.uuid = uuid

        self.audiomode, self.forcemode = load_settings()
        self.box = Gtk.EventBox()
        self.hdmiicon = Gtk.Image.new_from_icon_name(
            "audio-hdmi-symbolic",
            Gtk.IconSize.MENU,
        )
        self.jackicon = Gtk.Image.new_from_icon_name(
            "audio-jack-symbolic",
            Gtk.IconSize.MENU,
        )
        if not self.forcemode == 'LAST':
            self.audiomode = self.forcemode
        if self.audiomode == 'JACK':
            self.displayicon = self.jackicon
            self.box.set_tooltip_text('Audio output set to 3.5mm jack')
            os.system("amixer cset numid=3 1")
        else:
            self.displayicon = self.hdmiicon
            self.box.set_tooltip_text('Audio output set to HDMI')
            os.system("amixer cset numid=3 2")
        self.box.add(self.displayicon)
        self.add(self.box)
        self.box.show_all()
        self.show_all()
        self.box.connect("button-press-event", self.on_press)

    def on_press(self, box, arg):

        self.box.remove(self.displayicon)
        self.forcemode = app_settings.get_string("startup-mode")
        if self.audiomode == 'HDMI':
            self.displayicon = self.jackicon
            self.audiomode = 'JACK'
            self.box.set_tooltip_text('Audio output currently set to 3.5mm jack')
            os.system("amixer cset numid=3 1")
        else:
            self.displayicon = self.hdmiicon
            self.audiomode = 'HDMI'
            self.box.set_tooltip_text('Audio output currently set to HDMI')
            os.system("amixer cset numid=3 2")
        save_settings(self.audiomode, self.forcemode)
        self.box.add(self.displayicon)
        self.box.show_all()

    def do_supports_settings(self):
        """Return True if support setting through Budgie Setting,
        False otherwise.
        """
        return True

    def do_get_settings_ui(self):
        """Return the applet settings with given uuid"""
        return BudgiePiAudioSettings(self.get_applet_settings(self.uuid))


#!/bin/bash
from plan2scene.config_manager import ConfigManager
import argparse
import os
import os.path as osp
import glob
import subprocess
import logging

if __name__ == "__main__":
    """
    Preview GNN preditions for every nth epoch.
    All surfaces (including observed surfaces) are assigned a propagated texture computed using texture embeddings of neighboring surfaces.
    """
    conf = ConfigManager()
    parser = argparse.ArgumentParser(
        description="Preview GNN preditions for every nth epoch. All surfaces are assigned the GNN prediction (including observed surfaces).")
    conf.add_args(parser)
    parser.add_argument("output_path", type=str, help="Output directory which will contain the preview.html file.")
    parser.add_argument("interval", type=int, help="Interval between previewed epochs.")
    parser.add_argument("gnn_train_path", type=str,
                        help="Path to GNN train directory. This directory has the conf directory and checkpoints directory.")
    args = parser.parse_args()
    conf.process_args(args, output_is_dir=True)

    output_path = args.output_path
    gnn_train_path = args.gnn_train_path
    interval = args.interval
    conf_path = osp.join(gnn_train_path, "conf", "texture_prop.json")
    checkpoints_path = osp.join(gnn_train_path, "checkpoints")

    assert osp.exists(conf_path)
    assert osp.exists(checkpoints_path)

    current_checkpoint_id = interval
    preview_images = None

    with open(osp.join(output_path, "preview.html"), "w") as f:
        f.write("
\n")
        while True:
            candidates = glob.glob(osp.join(checkpoints_path, f"loss-*-epoch-{current_checkpoint_id}.ckpt"))
            if len(candidates) == 0:
                break
            candidate = candidates[0]
            logging.info(f"Processing {candidate}")

            command = "/bin/bash ./code/scripts/plan2scene/texture_prop/preview_all_prop.sh {output_path} {conf_path} {checkpoint_path}".format(
                output_path=osp.join(output_path, f"epoch_{current_checkpoint_id}"),
                conf_path=conf_path,
                checkpoint_path=candidate
            )
            my_env = os.environ.copy()
            assert subprocess.call(command, shell=True, env=my_env) == 0

            # Preview
            if preview_images is None:
                preview_images = os.listdir(osp.join(output_path, f"epoch_{current_checkpoint_id}", "archs"))
                preview_images = [a for a in preview_images if a.endswith(".png")]
                f.write("
\n")
                f.write("Epoch	\n")
                for preview_image in preview_images:
                    f.write(f"{preview_image}	\n")

                f.write("
\n")

            f.write("
\n")
            f.write(f"{current_checkpoint_id}	\n")

            for preview_image in preview_images:
                f.write("\n")
                f.write("".format(img_path=osp.join(f"epoch_{current_checkpoint_id}", "archs", preview_image)))
                f.write("	\n")

            f.write("
\n")
            f.flush()

            current_checkpoint_id += interval
        f.write("
\n")


class MyRestaurant:
    restaurant_name='' # 레스토랑 명칭
    cuisine_type=''    # 레스토랑 종류
    total_price=0      # 오늘 총 매출
    total_sales=0      # 총 매출(매출.txt)
    # 생성자
    def __init__(self,name,type):
        self.restaurant_name = name
        self.cuisine_type = type
        self.f=open("./%s 매출.txt"%self.cuisine_type,'r')
        self.total_sales = int(self.f.read())
        self.f.close()
        self.f=open("./%s 매출.txt"%self.cuisine_type,'w')
    # 소개
    def describe_restaurant(self):
        print("저희 %s 레스토랑 명칭은 '%s'입니다."%(self.cuisine_type,self.restaurant_name))
    # 오픈
    def open_restaurant(self):
        print("\n저희 %s 레스토랑이 오픈했습니다.\n"%self.restaurant_name)
    # 메뉴 주문
    def order_menu_list(self,menu_list):
        menu_name = list(menu_list.keys())
        menu_price = list(menu_list.values())
        for i in range(len(menu_list)):
            print(" %d. %s %s원 "%(i+1,menu_name[i],menu_price[i]),end='')
        print('')
        choice_menu = int(input("메뉴를 고르세요: "))-1
        if choice_menu>len(menu_list)-1:
            print("없는 메뉴입니다.")
        else:
            print("%s의 가격은 %s원 입니다. 감사합니다.\n"%(menu_name[choice_menu],menu_price[choice_menu]))
            self.total_price += menu_price[choice_menu]
    # 소멸자
    def __del__(self):
        self.total_sales = self.total_sales + self.total_price
        self.f.write(str(self.total_sales))
        print("%s 레스토랑 문닫겠습니다" % self.restaurant_name)

# 일식 레스토랑
class Japanese_Restaurant(MyRestaurant):
    menu_list = {'초밥':12000,'사케동':5000,'가츠동':4000,'다코야끼':3500,'우동':2500}
    def describe_restaurant(self):
        print("저희 일식 레스토랑 명칭은 '%s'입니다."%(self.restaurant_name))
# 중식 레스토랑
class Chinese_Restaurant(MyRestaurant):
    menu_list = {'짜장면':3500,'짬뽕':5500,'탕수육':8000,'깐풍기':9500}
    def describe_restaurant(self):
        print("저희 중식 레스토랑 명칭은 '%s'입니다."%(self.restaurant_name))

open_yn= ''
menu = ''

name, type = input("레스토랑 명칭과 종류를 선택하세요.(공백으로 구분): ").split()

if type=='일식':
    my_restaurant = Japanese_Restaurant(name,type)
elif type=='중식':
    my_restaurant = Chinese_Restaurant(name,type)
else:
    print("없는 레스토랑입니다.")
    exit()

my_restaurant.describe_restaurant()
open_yn= input("레스토랑을 오픈하시겠습니까?(y/n)")
if open_yn=='n':
    exit()
my_restaurant.open_restaurant()

while True:
    menu = input("어서오세요. 무엇을 도와드릴까요?\n(종료:-1/ 주문:order/ 주문가격확인:price/ 총매출확인:total): ")
    if menu=='-1':      # 종료
        exit()
    elif menu=='order': # 주문
        my_restaurant.order_menu_list(my_restaurant.menu_list)
    elif menu=='price': # 주문 가격 확인
         print("지금까지 총 가격은 %s원 입니다.\n"%my_restaurant.total_price)
    elif menu=='total': # 총 매출 확인
        print("어제까지 총 매출은 %s원 입니다.\n"%my_restaurant.total_sales)
    else:
          print("없는 메뉴입니다. 다시 선택하세요.")

01. jump to python/chap05/restaurant_class_v5.py


from globals import MAP_SIZE, MAP_WINDOW_SIZE, WORLD_INFO, SETTINGS, LIFE

import graphics as gfx

import bad_numbers
import maps

import threading
import logging
import time

class ChunkHandler(threading.Thread):
	def __init__(self, check_every=5):
		threading.Thread.__init__(self)

		self.last_checked = -check_every
		self.check_every = check_every
		self.load_clusters = []

	def check_chunks(self, force=False):
		if not force and WORLD_INFO['ticks']-self.last_checked<self.check_every:
			return False

		self.last_checked = WORLD_INFO['ticks']

		for life in [l for l in LIFE.values() if l['online']]:
			_x_min = bad_numbers.clip(life['pos'][0]-MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1-MAP_WINDOW_SIZE[0])
			_y_min = bad_numbers.clip(life['pos'][1]-MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1-MAP_WINDOW_SIZE[1])
			_x_max = bad_numbers.clip(life['pos'][0]+MAP_WINDOW_SIZE[0], 0, MAP_SIZE[0]-1)
			_y_max = bad_numbers.clip(life['pos'][1]+MAP_WINDOW_SIZE[1], 0, MAP_SIZE[1]-1)
			_refresh = False

			for y in range(_y_min, _y_max, WORLD_INFO['chunk_size']):
				for x in range(_x_min, _x_max, WORLD_INFO['chunk_size']):
					maps.load_cluster_at_position_if_needed((x, y))

					SETTINGS['loading'] = True

					if 'player' in life:
						_refresh = True

			if _refresh:
				gfx.refresh_view('map')

		SETTINGS['loading'] = False

	def load_cluster(self, cluster_key):
		self.load_clusters.append(cluster_key)

	def run(self):
		while SETTINGS['running'] and not SETTINGS['kill threads']:
			self.check_chunks()
			time.sleep(1)

		logging.debug('ChunkHandler thread shutting down.')

def init():
	SETTINGS['chunk_handler'] = ChunkHandler()
	SETTINGS['chunk_handler'].start()


import os
import time
import slack_auth
from slackclient import SlackClient

BOT_ID = slack_auth.BOT_ID

AT_BOT = "<@" + BOT_ID + ">"
EXAMPLE_COMAND = "do"

slack_client = SlackClient(slack_auth.SLACK_BOT_TOKEN)

def parse_slack_output(slack_rtm_output):
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                return output['text'].split(AT_BOT)[1].strip().lower(), \
                       output['channel']
    return None, None

def handle_command(command, channel):
    response = "Not sure what you mean. Use the *" + EXAMPLE_COMAND + \
                "* command ith numbers, delimited by spaces."
    if command.startswith(EXAMPLE_COMAND):
        response = "Sure...write some more code then I can do that!"
    slack_client.api_call("chat.postMessage", channel=channel, text = response, as_user=True)

if __name__ == "__main__":
    READ_WEBSOCKET_DELAY = 1
    if slack_client.rtm_connect():
        print("StarterBot connected and running!")
        while True:
            command, channel = parse_slack_output(slack_client.rtm_read())
            if command and channel:
                handle_command(command, channel)
            time.sleep(READ_WEBSOCKET_DELAY)
    else:
        print("Connection failed.")


# ----------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------
import os
import shutil
import pandas as pd

# ----------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------

def GetFilesInDirectory(path):
    fileNamesList = []
    counter = 0
    for foldername, subfolders, filenames in os.walk(path):
        total = len(filenames)
        if foldername == path + "\.tmp.driveupload":
            continue
        for filename in filenames:
            fileNamesList.append(filename)
            counter = counter + 1
            #print(str(foldername) + " / " + str(counter) + " / " + str(total))
    return fileNamesList

# ----------------------------------------------------------------------
# Variables
# ----------------------------------------------------------------------

path = 'F:\Google_Drive'
missingFilesPath = 'F:\Google_Drive_Upload'

# ----------------------------------------------------------------------
# Main Control
# ----------------------------------------------------------------------

# Get list of files in the current folder
fileNamesList = GetFilesInDirectory(path)

# Get list of files from Google Photos
data = pd.read_csv(r'out.csv')
df = pd.DataFrame(data, columns=['filename'])

# Get list of files in the current folder that aren't already in Google Photos
missedPhotosCounter = 0
for fileName in fileNamesList:
    if fileName not in df['filename'].values:
        missedPhotosCounter = missedPhotosCounter + 1
        print(str(missedPhotosCounter) + ": " + fileName)
        shutil.copy(os.path.join(path, fileName), missingFilesPath)