# PARKING MANAGEMENT #
#
import datetime
#
# For insurance purposes, the management of an office building is required to
# maintain, at all time, an accurate list of all the vehicles in the dedicated
# parking. In addition, for billing the different companies, the office
# building management wants to record occupation of the parking at different
# times and automatically emit bills to each specific companies.
#
# You are tasked with completing the series of functions below that fill the
# need of the office building parking management. You are allowed (and
# encouraged) to create additional, intermediate functions.
#
# The main data structure that your suite of function handles is a record of
# entrances and exits. A sample is given below. It consist of a pair of lists
# of tuples. The first list gives the timestamps and license plate number of
# vehicles entering the parking, the second exiting.
#
# DO NOT MODIFY CONSTANTS
PARKING_DATA_SAMPLE = ([
    (datetime.datetime(2017, 12, 12, 7, 13, 44, 0), 'LR10GHT'),
    (datetime.datetime(2017, 12, 12, 7, 13, 48, 0), 'LC11FBF'),
    (datetime.datetime(2017, 12, 12, 7, 13, 59, 0), 'LR10ZPP'),
    (datetime.datetime(2017, 12, 12, 7, 15, 2, 0), 'LJ65OSN'),
    (datetime.datetime(2017, 12, 12, 7, 15, 22, 0), 'LA63EWH'),
    (datetime.datetime(2017, 12, 12, 13, 1, 42, 0), 'LC11FBF')
], [(datetime.datetime(2017, 12, 12, 12, 13, 1, 0), 'LC11FBF'),
    (datetime.datetime(2017, 12, 12, 16, 42, 10, 0), 'LR10ZPP'),
    (datetime.datetime(2017, 12, 12, 17, 2, 41, 0), 'LR10GHT'),
    (datetime.datetime(2017, 12, 12, 17, 2, 58, 0), 'LA63EWH'),
    (datetime.datetime(2017, 12, 12, 17, 4, 3, 0), 'LJ65OSN'),
    (datetime.datetime(2017, 12, 12, 17, 10, 21, 0), 'LC11FBF')])
#
# A secondary data structure includes billing information. It is a dictionary
# that maps company names to a list of registered license plates.
#
# DO NOT MODIFY CONSTANTS
COMPANY_REGISTRATIONS_SAMPLE = {
    'Shire Tobacco Inc.': ['LR10GHT', 'LA63EWH'],
    'Rohan Equestrian Equipments': [],
    'Moria Construction Hardware': ['LC11FBF', 'LS66XKE', 'LR10ZPP', 'LJ65OSN']
}

def register_car(registration, company, plate):
    """
    Registers a new car.

    NOTE: this function should not modify the registration dictionary that is
    given, instead it should create a new dictionary.
    NOTE: this function should not introduce duplicates in the registration
    system. Specifically, if a car is already registered with the given
    company it should return an identical registration information. If the car
    is registered with a different company it should remove the first
    registration.
    NOTE: if the company is not listed in the dictionary, it should not
    introduce it. Instead it should just return an identical registration.

    E.g., register_car({'Stark Industries': ['IRNMN']}, 'Stark Industries',
                       'JARVIS')
    is {'Stark Industries': ['IRNMN', 'JARVIS']}
    E.g., register_car({'Stark Industries': ['IRNMN']}, 'Wayne Enterprises',
                       'IMBTMN')
    is {'Stark Industries': ['IRNMN']}

    :param registration: preexisting registration information
    :param company: company to register the car for
    :param plate: license plate of the car to register
    :return: new registration information dictionary with added registration
    :rtype: dict
    """
    l_registration = registration
    for key, value in l_registration.items():
        if company != key:
            if plate in [x for v in l_registration.values() for x in v]:
                try:
                    l_registration[key].remove(plate)
                except ValueError:
                    a = 0

    for key, value in l_registration.items():
        if company == key:
            if plate not in [x for v in l_registration.values() for x in v]:
                l_registration[key].append(plate)

    return l_registration

register_car({'Stark Industries': ['IRNMN']}, 'Stark Industries', 'JARVIS')
register_car({'Stark Industries': ['IRNMN']}, 'Wayne Enterprises', 'IMBTMN')
register_car({'X': []}, 'X', 'A')
register_car({'X': [], 'Y': ['A']}, 'X', 'A')
register_car({'Stark Industries': ['IRNMN']}, 'Stark Industries', 'JARVIS')

def occupancy(parking_data, cutoff_time=None):
    """
    Computes the occupancy of the parking at a given time. If no time is
    provided, check the current occupancy.

    E.g.,
    data = ([(datetime.datetime(2017, 12, 12,  7, 13, 44, 0), 'LR10GHT')], [])
    occupancy(data, time=datetime.datetime(2017, 12, 12,  7, 13, 45, 0))
    is ['LR10GHT']
    E.g.,
    data = ([(datetime.datetime(2017, 12, 12,  7, 13, 44, 0), 'LR10GHT')], [])
    occupancy(data, time=datetime.datetime(2017, 12, 12,  7, 13, 43, 0))
    is []

    :param parking_data: tuple of list of timestamped arrival and departure
    information including license plate. See sample above.
    :param time: time (as a datetime.datetime object) at which to check for
    occupancy. If no time is provided, use now.
    :return: list of cars present in the parking at the given time.
    :rtype: list
    """

    l_entry = parking_data[0]
    l_exit = parking_data[1]
    l_time = cutoff_time
    l_entered = {}
    l_plates = list([])

    if l_time is None:
        l_time = datetime.datetime.now()
    for entry in l_entry:
        print(entry)
        for i in entry:
            print(i)
            l_entrytime = entry[0]
            l_plate = entry[1]
            if l_plate not in l_entered:
                l_entered[l_plate] = [l_entrytime]
                print(l_entered)

    for exits in l_exit:
        print(exit)
        for i in exits:
            print("exit {}".format(i))
            l_exittime = exits[0]
            l_plate = exits[1]
        if l_plate not in l_entered:
            l_entered[l_plate] = [l_exittime]
        else:
            l_entered[l_plate].append(l_exittime)

    for key, value in l_entered.items():
        print(key)
        for index,val in enumerate(value):
            if index % 2 == 0:
                l_entry = value[index]
                try:
                    l_exit = value[index+1]
                except IndexError:
                    l_exit = datetime.datetime.now()
                print(l_entry, l_exit)
    if l_exit != [] and l_exit >= l_time:
        l_plates.append(key)

    return l_plates

data = ([(datetime.datetime(2017, 12, 12,  7, 13, 44, 0), 'LR10GHT')], [])
cutoff_time = datetime.datetime(2000, 1, 3, 12, 0)
occupancy(data, cutoff_time)
data = ([(datetime.datetime(2017, 12, 12,  7, 13, 44, 0), 'LR10GHT')], [])
cutoff_time = datetime.datetime(2000, 1, 3, 12, 0)
occupancy(data, cutoff_time)

def company_bill(parking_data, company_registration, company, t_start, t_end):
    """
    Computes the total, cumulated time in seconds, ignoring milliseconds, that
    cars registred with a given company stayed in the parking during the
    interval between t_start and t_end.

    E.g.,
    parking_data = (
        [(datetime.datetime(2017, 12, 12,  7, 13, 44, 0), 'LR10GHT')],
        [(datetime.datetime(2017, 12, 12,  7, 13, 45, 0), 'LR10GHT')]
    )
    company_registration = {'Shire Tobacco Inc.': ['LR10GHT']}
    company_bill(parking_data, company_registration,
                 'Shire Tobacco Inc.', …, …)
    is 1

    :param parking_data: see sample above
    :param company_registration: see sample above
    :param company: name of the company to compute billing for
    :param t_start: start of the billing interval
    :param t_end: end of the billing interval
    :return: cumulated number of seconds of car park occupancy
    :rtype: float | int
    """
    l_entry = parking_data[0]
    l_exit = parking_data[1]
    l_seconds = 0
    l_enttime = None
    l_exittime = None
    for l_company, l_plate in company_registration.items():
        if l_company != company:
            break  # do not continue
        l_plates = [x for x in l_plate]
        for plate in l_plates:
            for t in l_entry:
                l_ent = list(t)
                if plate in l_ent[1]:
                    l_enttime = l_ent[0]
                else:
                    l_enttime = None
            for e in l_exit:
                l_ex = list(e)
                if plate in l_ex:
                    l_exittime = l_ent[0]
                else:
                    l_exittime = None
        if (l_enttime is None or l_exittime is None):
            return l_seconds
        else:
            l_seconds = (l_exittime - l_enttime).seconds

    return l_seconds

parking_data = ([(datetime.datetime(2000, 1, 1, 12, 0), 'LR10GHT')],
[(datetime.datetime(2000, 1, 2, 12, 0), 'LR10GHT')])
start_time = datetime.datetime(2000, 1, 3, 12, 0)
end_time = datetime.datetime(2000, 1, 3, 12, 0)
company_registration = {'Shire Tobacco Inc.': ['LR10GHT']}
company_bill(parking_data, company_registration, 'Shire Tobacco Inc.', start_time, end_time)

parking_data = ([(datetime.datetime(2000, 1, 1, 12, 0), 'LR10GHT')],
    [(datetime.datetime(2000, 1, 2, 12, 0), 'LR10GHT')])
start_time = datetime.datetime(2000, 1, 3, 12, 0)
end_time = datetime.datetime(2000, 1, 3, 12, 0)
company_registration = {'Shire Tobacco Inc.': ['LR10GHT']}
company_bill(parking_data, company_registration, 'Shire Tobacco Inc.', start_time, end_time)

parking_data = ([(datetime.datetime(2000, 1, 1, 12, 0), 'LR10GHT')], [])
start_time = datetime.datetime(2000, 1, 3, 12, 0)
end_time = datetime.datetime(2000, 1, 3, 12, 0)
company_registration = {'Shire Tobacco Inc.': ['LR10GHT']}
company_bill(parking_data, company_registration, 'Shire Tobacco Inc.', start_time, end_time)

parking_data = ([(datetime.datetime(2000, 1, 1, 12, 0), 'LR10GHT'), (datetime.datetime(2000, 1, 2, 12, 0), 'LR10GHT'), (datetime.datetime(2000, 1, 3, 12, 0), 'LR10GHT')], [(datetime.datetime(2000, 1, 1, 12, 1), 'LR10GHT'), (datetime.datetime(2000, 1, 2, 12, 1), 'LR10GHT'), (datetime.datetime(2000, 1, 3, 12, 1), 'LR10GHT')])
start_time = datetime.datetime(2000, 1, 3, 12, 0)
end_time = datetime.datetime(2000, 1, 3, 12, 0)
company_registration = {'Shire Tobacco Inc.': ['LR10GHT']}
company_bill(parking_data, company_registration, 'Shire Tobacco Inc.', start_time, end_time)


'''
243. Shortest Word Distance II

Design a data structure that will be initialized with a string array, and then it should answer queries of the shortest distance between two different strings from the array.
Implement the WordDistance class:
    WordDistance(String[] wordsDict) initializes the object with the strings array wordsDict.
    int shortest(String word1, String word2) returns the shortest distance between word1 and word2 in the array wordsDict.
'''

class WordDistance:
    def __init__(self, wordsDict):
        self.dict = {}
        for i, word in enumerate(wordsDict):
            if word not in self.dict:
                self.dict[word] = []
            self.dict[word].append(i)

    def shortest(self, world1, world2):
        i1, i2 = self.dict[world1], self.dict[world2]
        res = len(self.dict)

        for i in i1:
            for j in i2:
                res = min(res, abs(i-j))
        return res


#! python3
# This is to print fizzBuzz.
# if number divisiable by 3 and 5 print fizz buzz.
# if number divisable by 3 print fizz.
# if number is divisable by 5 print buzz.

nums = input('Enter a number:')  # Asks for a number.
nums = int(nums)  # Converts a string into a interger.

def fizzBuzz(nums):  # This is our main function
    # This loop will go through all the numbers from 1 up to the number the user inputs.
    for i in range(1, nums + 1):
        # Checks to see if the number is divisable between 3 and 5. Both have to to be true.
        if i % 3 == 0 and i % 5 == 0:
            print('FizzBuzz')
        elif i % 3 == 0:  # Checks to see if only divisable by 3.
            print('Fizz')
        elif i % 5 == 0:  # Checks to see if divisable by 5.
            print('Buzz')
        else:            # If no conditions are met above it will print out the number
            print(i)

# This will call the function fizzBuzz and add the input nums to it.
fizzBuzz(nums)


def no_vowels():
    print("".join([letter for letter in input() if letter not in ['a', 'o', 'u', 'e', 'i']]))

def trains():
    train = [0 for count in range(int(input()))]
    while True:
        command = input().split(" ")
        if command[0] == "End":
            print(train)
            break
        elif command[0] == "add":
            train[-1] += int(command[1])
        elif command[0] == "insert":
            train[int(command[1])] += int(command[2])
        elif command[0] == "leave":
            train[int(command[1])] -= int(command[2])

def to_do_list():
    notes = []

    while True:
        note = input().split("-")
        if note[0] == "End":
            break
        notes.append([int(note[0]), note[1]])

    print([(note[1]) for note in list(sorted(notes, key=lambda x: x[0]))])

def palindrome_strings():
    words = input().split()
    palindromes = [word for word in words if word == word[::-1]]
    print(palindromes)
    print(f"Found palindrome {words.count(input())} times")

def even_numbers():
    # The task is done the correctly, but Judge doesn't like it. I guess it's broken
    nums = list(map(int, input().split(", ")))
    print([nums.index(num) for num in nums if num % 2 == 0])

def the_office():
    employees_happiness = [int(employee) for employee in input().split(' ')]
    happiness_improvement_factor = int(input())
    employees_happiness = [em * happiness_improvement_factor for em in employees_happiness]
    filtered = list(filter(lambda x: x >= (sum(employees_happiness) / len(employees_happiness)), employees_happiness))
    if len(filtered) >= len(employees_happiness) / 2:
        print(f"Score: {len(filtered)}/{len(employees_happiness)}. Employees are happy!")
    else:
        print(f"Score: {len(filtered)}/{len(employees_happiness)}. Employees are not happy!")

if __name__ == '__main__':
    # no_vowels()
    # trains()
    # to_do_list()
    # palindrome_strings()
    # even_numbers()
    the_office()

SoftUni Lessons/Python Development/Python Fundamentals September 2019/Problems And Files/15 LISTS ADVANCED - Дата 16-ти октомври, 1830 - 2130/Redoing all the tasks.py


"""
Created on October 19, 2018

@author: mae-ma
@attention: evaluation of the architectures
@contact: albus.marcel@gmail.com (Marcel Albus)
@version: 1.3.1

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

History:
- v1.3.1: cleanup
- v1.3.0: plot for q-vals
- v1.2.1: change filenames
- v1.2.0: use smoothed score output for better visualization
- v1.1.1: use relative paths
- v1.1.0: add click commands
- v1.0.0: first init
"""

import numpy as np
from matplotlib import pyplot as plt
import pickle
import yaml
import shutil
import os
import click
import datetime

def smooth(x, window_len=11, window='hanning'):
    """smooth the data using a window with requested size.

    This method is based on the convolution of a scaled window with the signal.
    The signal is prepared by introducing reflected copies of the signal
    (with the window size) in both ends so that transient parts are minimized
    in the begining and end part of the output signal.

    input:
        x: the input signal
        window_len: the dimension of the smoothing window; should be an odd integer
        window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
            flat window will produce a moving average smoothing.

    output:
        the smoothed signal

    example:

    t=linspace(-2,2,0.1)
    x=sin(t)+randn(len(t))*0.1
    y=smooth(x)

    see also:

    numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
    scipy.signal.lfilter

    TODO: the window parameter could be the window itself if an array instead of a string
    NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
    """

    if x.ndim != 1:
        raise ValueError("smooth only accepts 1 dimension arrays.")

    if x.size < window_len:
        raise ValueError("Input vector needs to be bigger than window size.")

    if window_len < 3:
        return x

    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError(
            "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")

    s = np.r_[x[window_len-1:0:-1], x, x[-1:-window_len:-1]]
    # print(len(s))
    if window == 'flat':  # moving average
        w = np.ones(window_len, 'd')
    else:
        w = eval('np.'+window+'(window_len)')

    y = np.convolve(w/w.sum(), s, mode='valid')
    return y

class Evaluation:
    def __init__(self):
        # src_filepath = home/mae-ma/git/safety
        # self.src_filepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.src_filepath = os.getcwd()
        self.plot_filename = None

        with open(os.path.join(self.src_filepath, 'reward.yml'), 'r') as file:
            self.reward = yaml.load(file)
        with open(os.path.join(os.path.join(self.src_filepath, 'architectures'), 'config_dqn.yml'), 'r') as file:
            self.dqn_config = yaml.load(file)
        with open(os.path.join(self.src_filepath, 'model.yml'), 'r') as file:
            self.model = yaml.load(file)

        self.tgt_filepath = os.path.join(os.path.dirname(
            os.path.dirname(__file__)), 'results')
        if not os.path.exists(self.tgt_filepath):
            os.makedirs(self.tgt_filepath)

    def plot(self):
        print('–'*50)
        print('Plot "reward.yml"')
        csv_path = os.path.join(os.getcwd(), 'training_log_DQN.csv')
        self.csv = np.genfromtxt(csv_path, delimiter=',')
        smoothed = smooth(self.csv[:, 2], 31)

        fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))
        ax1.plot([_ for _ in range(len(smoothed))],
                 smoothed, 'b', label='loss')
        ax1.set_ylabel('Loss')

        score_value = [x[0] for x in self.reward]
        score_time = [x[1] for x in self.reward]
        ax2.plot(score_time, smooth(np.array(score_value), 11)[:-10], 'r', label='scores')
        ax2.set_xlabel('Steps')
        ax2.set_ylabel('Scores')
        ax1.set_xlim([- len(smoothed)*.05, len(smoothed)*1.05])
        ax2.set_xlim([- len(smoothed)*.05, len(smoothed)*1.05])
        ax2.legend()
        ax1.legend()
        ax2.grid()
        ax1.grid()
        fig.tight_layout()
        if self.model['config'][0]['class_name'] == 'Conv2D':
            model = 'Conv2D'
        else:
            model = '-u' + str(self.model['config'][0]['config']['units'])
        filename = 'lr' + \
            str(self.dqn_config['learning_rate']).replace('.', '_') + \
            '-g' + str(self.dqn_config['gamma']).replace('.', '_') + \
            model + '.pdf'
        self.plot_filename = filename
        plt.savefig(filename)
        plt.show()
        print('–'*50)

    def plot_q_vals(self):
        csv_path = os.path.join(os.getcwd(), 'q_val_DQN.csv')
        self.csv = np.genfromtxt(csv_path, delimiter=',')
        smoothed = smooth(self.csv[:], 51)
        fig, ax1 = plt.subplots(1, 1, figsize=(12, 12))
        ax1.plot([_ for _ in range(len(smoothed))],
                 smoothed, 'b', label='Q values')
        ax1.set_ylabel('Q values')
        ax1.set_xlabel('Steps')
        plt.legend()
        plt.grid()
        # plt.show()

    def save_all(self):
        print('–'*50)
        filelist = ['weights.h5', 'target_weights.h5',
                    'reward.yml', 'replay_buffer.pkl', 'training_log_DQN.csv',
                    self.plot_filename, 'architectures/config_dqn.yml', 'model.yml']
        folder = datetime.datetime.today().strftime('%Y_%m_%d-%H_%M') + '___' + self.plot_filename
        folderpath = os.path.join(self.tgt_filepath, folder)
        print('Save all files to: ' + folderpath)
        if not os.path.exists(folderpath):
            os.makedirs(folderpath)
        for file in filelist:
            shutil.copy2(os.path.join(self.src_filepath, file), folderpath)
        print('–'*50)

@click.command()
@click.option('--plot/-no-plot', '-p/-np', default=True, help='plot the results from the "results.yaml" file')
@click.option('--save/--no-save', '-s/-ns', default=False, help='backups the files')
def main(plot, save):
    ev = Evaluation()
    print('src: ', ev.src_filepath)
    if plot:
        ev.plot()
        # ev.plot_q_vals()
    if save:
        ev.save_all()

if __name__ == '__main__':
    main()


import apache_beam as beam

with beam.Pipeline() as pipeline:
  plants = (
      pipeline
      | 'Garden plants' >> beam.Create([
          ('🍓', 'Strawberry'),
          ('🥕', 'Carrot'),
          ('🍆', 'Eggplant'),
          ('🍅', 'Tomato'),
          ('🥔', 'Potato'),
      ])
      | 'Values' >> beam.Values()
      | beam.Map(print))


"""
Viết chương trình và in giá trị theo công thức cho trước: Q = √([(2 * C * D)/H]) (bằng chữ:
Q bằng căn bậc hai của [(2 nhân C nhân D) chia H]. Với giá trị cố định của C là 50, H là 30.
D là dãy giá trị tùy biến, được nhập vào từ giao diện người dùng, các giá trị của D được phân
cách bằng dấu phẩy.

Ví dụ: Giả sử chuỗi giá trị của D nhập vào là 100,150,180 thì đầu ra sẽ là 18,22,24.
"""

import math

def calculate():
    res = []
    c, h = 50, 30
    s = input("Nhap vao mot chuoi so: ")
    list_ele = s.split(",")
    for i in list_ele:
        res.append(str(int((math.sqrt((2*c*(int(i)))/h)))))
    return res

res = calculate()

print(",,".join(res))


# coding= utf-8
import json
import mitmproxy.http

# 存在中文乱码情况
class GetJson:

    def response(self, flow: mitmproxy.http.HTTPFlow):
        if "https://stock.xueqiu.com/v5/stock/batch/quote.json?_t" in flow.request.url and "x=" in flow.request.url:
            # 数据的模拟
            data = json.loads(flow.response.text)
            with open("./data/quote.json", "w", encoding="UTF-8") as f:
                json.dump(data, f,indent=2,ensure_ascii=False)
            print("加载入文件完成...")

addons = [
    GetJson()
]

if __name__ == '__main__':
    from mitmproxy.tools.main import mitmdump

    mitmdump(['-p', '8080', "-s", __file__])


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

from flask import render_template, redirect, url_for, flash, request
from ..models import User, News, Weather
from . import main
from .. import HWMonitor
from flask_login import login_required, current_user, login_user
from sqlalchemy import desc
from random import sample

# 首页
@main.route('/', methods=['GET', 'POST'])
def index():
    # 如果当前未登录，登入测试帐号
    if not current_user.is_authenticated:
        user = User.query.filter_by(id=100).first()
        login_user(user)

    # 幻灯片部分
    news_count = News.query.filter_by(news_agency='BBC').order_by(desc(News.id)).first().id  # 获取数据库中最后一条新闻的id
    slider = []
    if news_count >= 20:
        selected_news = sample(range(news_count - 10, news_count), 5)  # 随机获得5条新闻的id
        for news in selected_news:
            slider.append(News.query.filter_by(id=news).first())

    # 分页和新闻部分
    page = request.args.get('page', 1, type=int)
    pagination = News.query.order_by(desc(News.id)).paginate(page, per_page=12, error_out=False)
    articles = pagination.items

    # 天气部分
    weather = Weather.query.filter_by(city="Tianjin").order_by(desc(Weather.id)).first()

    # 系统状态部分
    hw_status = HWMonitor()

    return render_template("index.html",
                           articles=articles,
                           weather=weather,
                           pagination=pagination,
                           slider=slider,
                           hw=hw_status)

# 抓取新闻接口 默认50条
@main.route('/fetchnews', methods=['GET', 'POST'])
@login_required
def fetch_news():
    News.fetch_news(50)
    return redirect(url_for('.index'))


import datetime

# Types of fields.
CHAR = "C"
NUMERAL = "N"
DATE = "D"
LOGICAL = "L"

# System encoding which is used to convert field names between bytes and string.
SYSTEM_ENCODING = "ascii"

# Reference data

ENCODINGS = {
    # got from dbf description [dbfspec]
    # id      name      description
    0x00: ("ascii", "ASCII"),  # internal use
    0x01: ("cp437", "DOS USA"),
    0x02: ("cp850", "DOS Multilingual"),
    0x03: ("cp1252", "Windows ANSI"),
    0x04: ("mac_roman", "Standard Macintosh"),  # NOT SHURE
    0x64: ("cp852", "EE MS-DOS"),
    0x65: ("cp866", "Russian MS-DOS"),
    0x66: ("cp865", "Nordic MS-DOS"),
    0x67: ("cp861", "Icelandic MS-DOS"),
    0x6A: ("cp737", "Greek MS-DOS (437G)"),
    0x6B: ("cp857", "Turkish MS-DOS"),
    0x96: ("mac_cyrillic", "Russian Macintosh"),
    0x97: ("mac_latin2", "Eastern Europe Macintosh"),  # NOT SHURE
    0x98: ("mac_greek", "Greek Macinstosh"),
    0xC8: ("cp1250", "Windows EE"),
    0xC9: ("cp1251", "Russian Windows"),
    0xCA: ("cp1254", "Turkish Windows"),
    0xCB: ("cp1253", "Greek Windows"),
    ## These encodings are not supported by Python but a part of DBF spec.
    # 0x68:    ('cp895', 'Kamenicky (Czech) MS-DOS'),
    # 0x69:    ('cp790', 'Mazovia (Polish) MS-DOS'),
}

REVERSE_ENCODINGS = dict(
    [(value[0], (code, value[1])) for code, value in ENCODINGS.items()]
)

SIGNATURES = {
    0x02: "FoxBase",
    0x03: "dBASE III",
    0x04: "dBASE IV",
    0x05: "dBASE V",
    0x30: "Visual FoxPro",
    0x31: "Visual FoxPro with AutoIncrement field",
    0x43: "dBASE IV with SQL table and memo file",
    0x7B: "dBASE IV with memo file",
    0x83: "dBASE III with memo file",
    0x8B: "dBASE IV with memo file",
    0x8E: "dBASE IV with SQL table",
    0xB3: ".dbv and .dbt memo (Flagship)",
    0xCB: "dBASE IV with SQL table and memo file",
    0xE5: "Clipper SIX driver with SMT memo field",
    0xF5: "FoxPro with memo field",
    0xFB: "FoxPro",
}

SUPPORTED_SIGNATURES = (0x03, 0x04, 0x05)

# <   -- little endian
# B   -- version number (signature)
# 3B  -- last update (YY, MM, DD)
# L   -- number of records
# H   -- length of header
# H   -- length of each record
# 17x -- pad (2B -- reserved,
#              B -- incomplete transaction,
#              B -- encryption flag,
#             4B -- free record thread (reserved for LAN)
#             8B -- reserved for multiuser dBASE
#              B -- MDX flag)
# B   -- language driver
# 2x  -- pad (2B -- reserved)
HEADER_FORMAT = "<B3BLHH17xB2x"

# <   -- little endian
# 11s -- field name in ASCII (terminated by 0x00)
# c   -- field type (ASCII)
# 4x  -- field data address ( 2B -- address in memory (for dBASE)
#                          OR 4B -- offset of field from
#                                   beginning of record (for FoxPro)
# B   -- field length
# B   -- decimal count
# 14x -- pad (2B -- reserved for multi-user dBASE,
#              B -- work area id ()
#             2B -- reserved for multi-user dBASE,
#              B -- flag for SET FIELDS
#             7B -- reserved
#              B -- index field flag)
# B   -- language driver
# 2x  -- pad (2B -- reserved)
FIELD_DESCRIPTION_FORMAT = "<11sc4xBB14x"

# Common functions

def dbf2date(dbf_str):
    """
    Converts date from dbf-date to datetime.date

    Args:
        `dbf_str`:
            string in format YYYYMMDD
    """
    if dbf_str is None or not dbf_str.isdigit() or len(dbf_str) != 8:
        result = None
    else:
        result = datetime.date(int(dbf_str[:4]), int(dbf_str[4:6]), int(dbf_str[6:8]))
    return result

def date2dbf(dt):
    """
    Converts date from datetime.date to dbf-date (string in format YYYYMMDD)

    Args:
        `dt`:
            datetime.date instance
    """
    if not isinstance(dt, datetime.date):
        raise TypeError("Espects datetime.date instead of %s" % type(dt))
    return b"%04d%02d%02d" % (dt.year, dt.month, dt.day)

def dbf2str(dbf_str):
    """
    Converts date from dbf-date to string (DD.MM.YYYY)

    Args:
        `dbf_str`:
            dbf-date (bytes in format YYYYMMDD)
    """
    if dbf_str is None or not dbf_str.isdigit() or len(dbf_str) != 8:
        result = None
    else:
        string_date = dbf_str.decode(SYSTEM_ENCODING)
        result = ".".join(
            reversed((string_date[:4], string_date[4:6], string_date[6:8]))
        )
    return result

def str2dbf(dt_str):
    """
    Converts from string to dbf-date (string in format YYYYMMDD)

    Args:
        `dt_str`:
            string in format DD.MM.YYYY
    """
    if not isinstance(dt_str, str):
        raise TypeError("Espects string or unicode instead of %s" % type(dt_str))
    str_l = len(dt_str)
    if str_l != 10:
        raise ValueError(
            "Datestring must be 10 symbols (DD.MM.YYYY) " "length instead of %d" % str_l
        )
    d, m, y = dt_str.split(".")
    dbf_string = "".join((y, m, d))
    return dbf_string.encode(SYSTEM_ENCODING)

# References:
# [dbfspec]: http://www.clicketyclick.dk/databases/xbase/format/index.html


import csv

import pandas as pd

def parse_flat_output_to_csv(flat_output_path, csv_output_path):

    # Read the flat output file (assuming it's a text file with newline-separated records)
    with open(flat_output_path, 'r') as flat_file:
        flat_data = flat_file.read().strip().split('\n')

    # Split each line into fields (assuming fields are separated by a delimiter, e.g., tab or comma)
    parsed_data = [line.split('\t') for line in flat_data]  # Replace '\t' with your delimiter if needed

    # Convert the parsed data to a DataFrame
    df = pd.DataFrame(parsed_data)

    # Save the DataFrame to a CSV file
    df.to_csv(csv_output_path, index=False, header=False)  # Set index and header to False if not needed

    return csv_output_path

# Example usage:

flat_output_path = 'raj_file.txt'  # Replace with your flat output file path

csv_output_path = 'output.csv'  # Replace with your desired CSV output file path

result_path = parse_flat_output_to_csv(flat_output_path, csv_output_path)

print(f'CSV file saved at: {result_path}')


from flask import Flask, render_template
from flask import url_for

app = Flask(__name__)

@app.route('/')
@app.route('/list_prof/<list>')
def list_prof(list):
    jobs_list = ['Капитан', 'Штурман', 'Врач', 'Солдат', 'Гей']

    return render_template('list_prof.html', list=list, jobs=jobs_list)

if __name__ == '__main__':
    app.run(port=8080, host='127.0.0.1')

# http://127.0.0.1:8080/list_prof/ol


from utilities import excel_reader
from text_processing import text_normalizer

class ColumnFiller:

    def __init__(self, dict_filename, keyword_column = "Keyword", high_level_label_column = "High level label", status_logger = None):
        self.load_dictionary(dict_filename, keyword_column, high_level_label_column)
        self.status_logger = status_logger

    def log_percents(self, percent):
        if self.status_logger is not None:
            self.status_logger.update_step_percent(percent)

    def load_dictionary(self, dict_filename, keyword_column, high_level_label_column):
        dict_df = excel_reader.ExcelReader().read_file(dict_filename)
        assert keyword_column in dict_df.columns or high_level_label_column in dict_df.columns,\
         "Check column names in dictionary, %s and %s are not found"%(keyword_column, high_level_label_column)
        self.keyword_dictionary = {}
        for i in range(len(dict_df)):
            if dict_df[high_level_label_column].values[i] not in self.keyword_dictionary:
                self.keyword_dictionary[dict_df[high_level_label_column].values[i]] = []
            self.keyword_dictionary[dict_df[high_level_label_column].values[i]].append(dict_df[keyword_column].values[i])

    def label_articles_with_outcomes(self, label_column, articles_df,
            search_engine_inverted_index, _abbreviations_resolver, label_details_column = "",
            threshold=0.92, print_all = False, keep_hierarchy=False, resolve_abbreviations=False):
        found_high_level_values = {}
        articles_df[label_column] = ""
        if label_details_column != "":
            articles_df[label_details_column] =""
        for idx, key_1 in enumerate(self.keyword_dictionary):
            print("High level label: %s"%key_1)
            if idx % 3 == 0:
                self.log_percents(idx/len(self.keyword_dictionary)*90)
            for key in self.keyword_dictionary[key_1]:
                for article_index in search_engine_inverted_index.find_articles_with_keywords(
                        [key], threshold = threshold, extend_with_abbreviations = True):
                    if articles_df[label_column].values[article_index] == "":
                        articles_df[label_column].values[article_index] = set()
                    key_resolved = key_1
                    if resolve_abbreviations:
                        key_resolved = _abbreviations_resolver.replace_abbreviations(key_1)
                    articles_df[label_column].values[article_index].add(key_resolved)
                    if label_details_column != "":
                        if articles_df[label_details_column].values[article_index] == "":
                            articles_df[label_details_column].values[article_index] = set()
                        full_keyword_name = key
                        if resolve_abbreviations:
                            full_keyword_name = _abbreviations_resolver.replace_abbreviations(key)
                        full_keyword_path_name = "%s/%s"%(key_1, full_keyword_name)
                        if keep_hierarchy:
                            articles_df[label_details_column].values[article_index].add(full_keyword_path_name)
                            articles_df[label_details_column].values[article_index].add(key_1 + "/")
                        else:
                            articles_df[label_details_column].values[article_index].add(full_keyword_name)
                    if key not in found_high_level_values:
                        found_high_level_values[key] = set()
                    found_high_level_values[key].add(article_index)
        total_outcomes = 0
        for i in range(len(articles_df)):
            if articles_df[label_column].values[i] != "":
                total_outcomes += 1
        articles_df = text_normalizer.replace_string_default_values(articles_df, label_column)
        if label_details_column != "":
            articles_df = text_normalizer.replace_string_default_values(articles_df, label_details_column)
        if print_all:
            for key in found_high_level_values:
                print(key, len(found_high_level_values[key]))
        print("Labelled articles with outcomes: %d"%total_outcomes)
        return articles_df


import numpy as np
import pandas as pd
import sys
import os
import logging
import math

NUMBER_CHAR = 1000

def get_logger(name):
    _log_format = f"%(asctime)s - [%(levelname)s] - %(name)s - (%(filename)s).%(funcName)s(%(lineno)d) - %(message)s"

    def get_file_handler():
        file_handler = logging.FileHandler("x.log")
        file_handler.setLevel(logging.WARNING)
        file_handler.setFormatter(logging.Formatter(_log_format))
        return file_handler

    def get_stream_handler():
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(logging.INFO)
        stream_handler.setFormatter(logging.Formatter(_log_format))
        return stream_handler

    def_logger = logging.getLogger(name)
    def_logger.setLevel(logging.INFO)
    #def_logger.addHandler(get_file_handler())
    def_logger.addHandler(get_stream_handler())
    return def_logger

def get_names():
    names = {
        'input_test_file': root_dir + "dissertation_test.csv",
        'input_file': root_dir + "dissertation.csv",
        'all_set_char_file': root_dir + "all_set_char.txt",
        'dict_oecds_file': root_dir + "dict_oecds.csv"
    }
    return names

def get_column(input_file, column):
    df = pd.read_csv(input_file)
    return df[column]

def show_plot(distribution):
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()
    fig.canvas.set_window_title('Диаграмма количества ключевых слов')
    ax.bar(distribution.keys(), distribution.values())

    ax.set_facecolor('seashell')
    fig.set_facecolor('floralwhite')
    fig.set_figwidth(12)  # ширина Figure
    fig.set_figheight(6)  # высота Figure
    ax.set_xlabel('Длинна текста')
    ax.set_ylabel('Частота использования ключевых слов')

    plt.show()

def len_split(x) -> int:
    try:
        return len(x.split(sep=', '))#chr(0x1f)))

    except AttributeError:
        return 0

def show_distribution(df_abstract, operatinon=len):
    distribution = {}
    for i, text in enumerate(df_abstract):
        try:
            l = operatinon(text)
        except TypeError:
            print(i, text)
        if l in distribution:
            distribution[l] += 1
        else:
            distribution[l] = 1

    print(max(distribution.keys()))
    print(distribution)
    show_plot(distribution)

def create_char_dict(set_char_filename, df_abstract):
    all_set_char = set()
    print(len(df_abstract))
    for text in df_abstract:
        try:
            all_set_char = all_set_char.union(set(text))
            # all_set_char = all_set_char.union(set(text.split()))
        except TypeError:
            continue
        except AttributeError:
            continue

    all_set_char = sorted(list(set(str(all_set_char).lower())))
    str_char = ''
    for char in all_set_char:
        str_char += f'{ord(char)} - ' + char + '\n' #chr(0x1f)
    #str_char = str_char[:-1]
    with open(set_char_filename, 'w') as f:
        f.write(str_char)
    # print(all_set_char)
    # print(f"len(all_set_char): {len(all_set_char)}")
    return all_set_char

def prepare_data(tuple_char, abstract):
    """
    Create list with index char in tuple_char.
    :param tuple_char:
    :param abstract:
    :return: indexes
    """
    indexes = []
    try:
        for char in abstract.lower():
            try:
                ind = tuple_char[0].index(char)
            except ValueError:
                if char in tuple_char[1]:
                    ind = len(tuple_char)
                elif char in tuple_char[2]:
                    ind = len(tuple_char) + 1
                else:
                    # logger.info(f'special symbol: {char}')
                    ind = None
            indexes.append(ind)
    except AttributeError :
        logger.warning(f'NaN in abstract: {abstract}')
        pass

    if len(indexes) > NUMBER_CHAR:
        indexes = indexes[:1000]
    # print(indexes)
    # print(len(indexes))
    return indexes

def quantization_abstract(indexes, tuple_char):
    quantum_abstract = np.zeros((NUMBER_CHAR, (len(tuple_char[0]) + len(tuple_char) - 1)), bool)
    for i, ind in enumerate(indexes):
        if ind:
            quantum_abstract[i][ind] = True
    return quantum_abstract

def quantization_oecds(oecds, dict_oecds):
    quantum_oecds = np.zeros(len(dict_oecds), bool)
    try:
        splited_oecds = oecds.split(sep=', ')
        for oecd in splited_oecds:
            quantum_oecds[dict_oecds[oecd.lower()]] = True
    except AttributeError:
        logger.warning(f'NaN in oecds')
        raise(f'NaN in oecds')
    return quantum_oecds

def get_tuple_char():
    """
    Tuple_char is tuple ((alphabetical), (upper), (lower), (latin))
    alphabetical - is real index. (tuple_char.index(char))
    (upper), (lower), (latin) - len(tuple_char), len(tuple_char) + 1,len(tuple_char) + 2.
    :return:
    """
    tuple_char = (
        (' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>',
        '?', '@', '[', '\\', ']', '^', '_', '`',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        '{', '|', '}', '~',
        'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
        'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э',
        'ю', 'я', 'ё'),
        ('⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹', '⁺', '⁻', '⁼', '⁽', '⁾', 'ⁿ'),
        ('₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉', '₊', '₋', '₌', '₍', '₎', 'ₒ', 'ₓ', 'ₙ'),
        ('α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'ο',
         'π', 'ρ', 'ς', 'σ', 'τ', 'φ', 'χ', 'ψ', 'ω')
    )

    return tuple_char

def create_dict_oecds(df_oecds, dict_oecds_file):
    list_oecd = []
    for oecds in df_oecds:
        #print(oecds)
        try:
            splited_oecds = oecds.split(sep=', ')
            for oecd in splited_oecds:
                list_oecd.append(oecd.lower())
        except AttributeError:
            logger.warning(f'NaN in oecds')

    list_oecd = sorted(list(set(list_oecd)))
    print(list_oecd)
    print(len(list_oecd))

    df_dict_oecds = pd.DataFrame()
    df_dict_oecds.index.name = 'index'
    df_dict_oecds['uniq_oecds'] = list_oecd
    df_dict_oecds.to_csv(dict_oecds_file)
    logger.info(f"Done!")
    dict_oecds = {}
    for i, oecd in enumerate(list_oecd):
        dict_oecds[oecd] = i
    return dict_oecds

def main():
    names = get_names()
    # df_abstract_oecds = get_column(names['input_test_file'], ['abstract', 'oecds.0'])
    df_abstract_oecds = get_column(names['input_file'], ['abstract', 'oecds.0'])
    # show_distribution(df_abstract)
    #all_set_char = create_char_dict(names['all_set_char_file'], df_abstract)
    tuple_char = get_tuple_char()
    quantum_abstracts = []
    quantum_oecds = []
    len_df_abstract = len(df_abstract_oecds['abstract'])
    dict_oecds = create_dict_oecds(df_abstract_oecds['oecds.0'], names['dict_oecds_file'])
    #get_oecds_tokinizer(df_abstract_oecds['oecds.0'])
    for i, (abstract, oecds) in enumerate(zip(df_abstract_oecds['abstract'], df_abstract_oecds['oecds.0'])):

        if i%1000 == 0:
            print(f'{i}/{len_df_abstract}')

        if abstract is np.NaN or oecds is np.NaN:
            # list_keyword.append(np.NaN)
            continue

        indexes = prepare_data(tuple_char, abstract)
        quantum_abstracts.append(quantization_abstract(indexes, tuple_char))
        quantum_oecds.append(quantization_oecds(oecds, dict_oecds))
        #sys.exit()

    # df_oecds = get_column(input_file, 'oecds.0')
    # show_distribution(df_oecds, len_split)

if __name__ == '__main__':
    root_dir = os.path.split(sys.argv[0])[0] + '/'
    logger = get_logger(__name__)
    main()


### MODULE 3 ###########
### ASSESSMENT #########

### Sod's Shock Tube ###

import numpy as np

nx = 81
dx = .25
dt = .0002
gamma = 1.4
T = 0.01
nt = 50

# Pressure given by equation of state
def p(u):
    return (gamma - 1)*(u[2] - 0.5*(u[1]**2/u[0]))

# Flux terms vector given by u
def computef(u):
    f = np.zeros((nx,3))
    for i in range(0,nx):
        f[i] = np.array([u[i][1],
                         u[i][1]**2/u[i][0] + p(u[i]),
                         (u[i][2] + p(u[i]))*(u[i][1]/u[i][0])])
    return f

# Initial Conditions
def vecu_initial(rho,u,p):
    return np.array([rho,
                     rho*u,
                     rho*(0.5*u**2 + p/((gamma-1)*rho))])

# Richtmyer method
def richtmyer(u, nt, dt, dx):
    un = np.zeros((nt,nx,3))
    un[:,:,:] = u.copy()
    umid = u.copy()

    for n in range(1,nt):
        for i in range(2,nx-1):
            f = computef(u)
            umid[i,:] =  0.5*(u[i+1,:] + u[i,:]) - dt/(2*dx) *(f[i+1,:] - f[i,:])
            fmid = computef(umid)
            un[n,i,:] =  u[i,:] - (dt/dx)*(fmid[i,:] - fmid[i-1,:])
        u = un[n].copy()

    return un

# Setting initial state
def u_initial():
    u = np.ones((nx,3))
    u[0:nx//2] = vecu_initial(1,0,100000)
    u[nx//2:nx] = vecu_initial(0.125,0,10000)
    return u

u = u_initial()

un = richtmyer(u, nt, dt, dx)
print(un[len(un)-1])


#!/usr/bin/env python
"""@package topo

Wireless Network topology creation.

@author Ramon Fontes (ramonrf@dca.fee.unicamp.br)

This package includes code to represent network topologies.
"""

from mininet.util import irange
from mininet.topo import Topo
from mn_iot.mac802154.link import mac802154Link

class Topo_sixlowpan(Topo):
    "Data center network representation for structured multi-trees."

    def add6lowpan(self, name, **opts):
        """Convenience method: Add host to graph.
           name: host name
           opts: host options
           returns: host name"""
        if not opts and self.hopts:
            opts = self.hopts
        return self.addNode(name, **opts)

    # This legacy port management mechanism is clunky and will probably
    # be removed at some point.

    def addPort(self, src, dst, sport=None, dport=None):
        """Generate port mapping for new edge.
            src: source switch name
            dst: destination switch name"""
        # Initialize if necessary
        ports = self.ports
        ports.setdefault(src, {})
        ports.setdefault(dst, {})
        # New port: number of outlinks + base
        if sport is None:
            src_base = 1 if self.isAP(src) else 0
            if 'ap' in src:
                sport = None
            else:
                sport = len(ports[ src ]) + src_base
        if dport is None:
            dst_base = 1 if self.isAP(dst) else 0
            if 'ap' in dst:
                dport = None
            else:
                dport = len(ports[ dst ]) + dst_base
        ports[ src ][ sport ] = (dst, dport)
        ports[ dst ][ dport ] = (src, sport)
        return sport, dport

    def nodes( self, sort=True ):
        "Return nodes in graph"
        if sort:
            return self.sorted( self.g.nodes() )
        else:
            return self.g.nodes()

    def aps( self, sort=True ):
        """Return aps.
           sort: sort aps alphabetically
           returns: dpids list of dpids"""
        return [ n for n in self.nodes( sort ) if self.isAP( n ) ]

    def stations( self, sort=True ):
        """Return stations.
           sort: sort stations alphabetically
           returns: list of stations"""
        return [ n for n in self.nodes( sort ) if not self.isAP( n ) ]

    def isAP( self, n ):
        "Returns true if node is a switch."
        return self.g.node[ n ].get( 'isAP', False )

# Our idiom defines additional parameters in build(param...)
# pylint: disable=arguments-differ

class Single6lowpanTopo(Topo_sixlowpan):
    "Single k stations."
    def build(self, k=2, **_opts):
        "k: number of stations"
        self.k = k
        for h in irange(1, k):
            sta = self.add6lowpan('sta%s' % h)
            self.addLink(sta, sta, cls=mac802154Link, panid='0xbeef')

class Minimal6lowpanTopo(Single6lowpanTopo):
    "Minimal wireless topology with two stations and one ap"
    def build(self):
        return Single6lowpanTopo.build(self, k=2)

# pylint: enable=arguments-differ


import copy
from abc import ABC, abstractmethod
from typing import Any, Callable, List, Optional, Set

import torch

from renate.models.layers import ContinualNorm
from renate.types import NestedTensors
from renate.utils.deepspeed import convert_to_tensor, recover_object_from_tensor

class RenateModule(torch.nn.Module, ABC):
    """A class for torch models with some additional functionality for continual learning.

    ``RenateModule`` derives from ``torch.nn.Module`` and provides some additional functionality
    relevant to continual learning. In particular, this concerns saving and reloading the model
    when model hyperparameters (which might affect the architecture) change during hyperparameter
    optimization. There is also functionality to retrieve internal-layer representations for use
    in replay-based CL methods.

    When implementing a subclass of ``RenateModule``, make sure to call the base class' constructor
    and provide your model's constructor arguments. Besides that, you can define a
    ``RenateModule`` just like ``torch.nn.Module``.

    Example::

        class MyMNISTMLP(RenateModule):

        def __init__(self, num_hidden: int):
            super().__init__(
                constructor_arguments={"num_hidden": num_hidden}
                loss_fn=torch.nn.CrossEntropyLoss()
            )
            self._fc1 = torch.nn.Linear(28*28, num_hidden)
            self._fc2 = torch.nn.Linear(num_hidden, 10)

        def forward(self, x: torch.Tensor) -> torch.Tensor:
            x = self._fc1(x)
            x = torch.nn.functional.relu(x)
            return self._fc2(x)

    The state of a ``RenateModule`` can be retrieved via the ``RenateModule.state_dict()`` method,
    just as in ``torch.nn.Module``. When reloading a ``RenateModule`` from a stored state dict, use
    ``RenateModule.from_state_dict``. It wil automatically recover the hyperparameters and
    reinstantiate your model accordingly.

    Note: Some methods of ``RenateModule`` accept an optional ``task_id`` argument. This is in
    anticipation of future methods for continual learning scenarios where task identifiers are
    provided. It is currently not used.

    Args:
        constructor_arguments: Arguments needed to instantiate the model.
    """

    def __init__(self, constructor_arguments: dict):
        super(RenateModule, self).__init__()
        self._constructor_arguments = copy.deepcopy(constructor_arguments)
        self._tasks_params_ids: Set[str] = set()
        self._intermediate_representation_cache: List[torch.Tensor] = []
        self._hooks: List[Callable] = []

    @classmethod
    def from_state_dict(cls, state_dict):
        """Load the model from a state dict.

        Args:
            state_dict: The state dict of the model. This method works under the assumption that
                this has been created by `RenateModule.state_dict()`.
        """
        extra_state = recover_object_from_tensor(state_dict["_extra_state"])
        constructor_arguments = extra_state["constructor_arguments"]
        model = cls(**constructor_arguments)
        for task in extra_state["tasks_params_ids"]:
            model.add_task_params(task)
        # TODO: See https://github.com/awslabs/Renate/issues/236.
        # There are changes to the `class_means` or `componenets` of a model
        # that are not loaded, and should probably not be stored.
        model.load_state_dict(state_dict, strict=False)
        return model

    def get_extra_state(self, encode: bool = True) -> Any:
        """Get the constructor_arguments, and task ids necessary to reconstruct the model."""
        extra_state = {
            "constructor_arguments": self._constructor_arguments,
            "tasks_params_ids": self._tasks_params_ids,
        }
        return convert_to_tensor(extra_state) if encode else extra_state

    def set_extra_state(self, state: Any, decode: bool = True):
        """Extract the content of the ``_extra_state`` and set the related values in the module."""
        extra_state = recover_object_from_tensor(state) if decode else state
        self._constructor_arguments = extra_state["constructor_arguments"]
        self._tasks_params_ids = extra_state["tasks_params_ids"]

    @abstractmethod
    def forward(self, x: NestedTensors, task_id: Optional[str] = None) -> torch.Tensor:
        """Performs a forward pass on the inputs and returns the predictions.

        This method accepts a task ID, which may be provided by some continual learning scenarios.
        As an example, the task id may be used to switch between multiple output heads.

        Args:
            x: Input(s) to the model. Can be a single tensor, a tuple of tensor, or a dictionary
                mapping strings to tensors.
            task_id: The identifier of the task for which predictions are made.
        Returns:
            The model's predictions.
        """
        pass

    def get_params(self, task_id: Optional[str] = None) -> List[torch.nn.Parameter]:
        """User-facing function which returns the list of parameters.

        If a ``task_id`` is given, this should return only parameters used for the specific task.

        Args:
            task_id: The task id for which we want to retrieve parameters.
        """
        return list(self.parameters())

    def _add_task_params(self, task_id: str) -> None:
        """Adds new parameters, associated to a specific task, to the model.

        The method should not modify modules created in previous calls, beyond the ones defined
        in ``self._add_task_params()``. The order of the calls is not guaranteed when the model
        is loaded after being saved.

        Args:
            task_id: The task id for which the new parameters are added.
        """
        pass

    def add_task_params(self, task_id: Optional[str] = None) -> None:
        """Adds new parameters, associated to a specific task, to the model.

        This function should not be overwritten; use ``_add_task_params`` instead.

        Args:
            task_id: The task id for which the new parameters are added.
        """
        if task_id in self._tasks_params_ids:
            return
        self._add_task_params(task_id)
        self._tasks_params_ids.add(task_id)

    def get_logits(self, x: NestedTensors, task_id: Optional[str] = None) -> torch.Tensor:
        """Returns the logits for a given pair of input and task id.

        By default, this method returns the output of the forward pass. This may be overwritten
        with custom behavior, if necessary.

        Args:
            x: Input(s) to the model. Can be a single tensor, a tuple of tensor, or a dictionary
                mapping strings to tensors.
            task_id: The task id.
        """
        return self.forward(x, task_id)

    def get_intermediate_representation(self) -> List[torch.Tensor]:
        """Returns the cached intermediate representation."""
        return self._intermediate_representation_cache

    def replace_batch_norm_with_continual_norm(self, num_groups: int = 32) -> None:
        """Replaces every occurence of batch normalization with continual normalization.

        Pham, Q., Liu, C., & Hoi, S. (2022). Continual normalization: Rethinking batch
        normalization for online continual learning. arXiv preprint arXiv:2203.16102.

        Args:
            num_groups: Number of groups when considering the group normalization in continual
                normalization.
        """

        def _replace(module):
            for name, child in module.named_children():
                if not list(module.children()):
                    _replace(child)
                if isinstance(
                    child, (torch.nn.BatchNorm1d, torch.nn.BatchNorm2d, torch.nn.BatchNorm3d)
                ):
                    setattr(
                        module,
                        name,
                        ContinualNorm(
                            num_features=child.num_features,
                            eps=child.eps,
                            momentum=child.momentum,
                            affine=child.affine,
                            track_running_stats=child.track_running_stats,
                            num_groups=num_groups,
                        ),
                    )

        _replace(self)

    def _intermediate_representation_caching_hook(self) -> Callable:
        """Hook to cache intermediate representations during training."""

        def hook(m: torch.nn.Module, _, output: torch.Tensor) -> None:
            if m.training:
                self._intermediate_representation_cache.append(output)

        return hook

    def register_intermediate_representation_caching_hook(self, module: torch.nn.Module) -> None:
        """Add a hook to cache intermediate representations during training.

        Store the reference to the hook to enable its removal.

        Args:
            module: The module to be hooked.
        """
        hook = module.register_forward_hook(self._intermediate_representation_caching_hook())
        self._hooks.append(hook)

    def deregister_hooks(self) -> None:
        """Remove all the hooks that were registered."""
        for hook in self._hooks:
            hook.remove()
        self._hooks = []
        self.reset_intermediate_representation_cache()

    def reset_intermediate_representation_cache(self) -> None:
        """Resets the intermediate representation cache."""
        self._intermediate_representation_cache = []

class RenateWrapper(RenateModule):
    """A simple wrapper around a torch model.

    If you are using a torch model with fixed hyperparameters, you can use this wrapper to expose
    it as a ``RenateModule``. In this case, do _not_ use the ``from_state_dict`` method but
    reinstantiate the model, wrap it, and call ``load_state_dict``. If a tuple or a dictionary of
    tensors is passed to the `RenateWrapper`'s forward function, it is unpacked before passing it
    to the torch model's forward function.

    Example::

        my_torch_model = torch.nn.Linear(28*28, 10)  # Instantiate your torch model.
        model = RenateWrapper(my_torch_model)
        state_dict = torch.load("my_state_dict.pt")
        model.load_state_dict(state_dict)

    Args:
        model: The torch model to be wrapped.
    """

    def __init__(self, model: torch.nn.Module) -> None:
        super().__init__(constructor_arguments={})
        self._model = model

    def forward(self, x: NestedTensors, task_id: Optional[str] = None) -> torch.Tensor:
        if isinstance(x, torch.Tensor):
            outputs = self._model(x)
        elif isinstance(x, tuple):
            outputs = self._model(*x)
        elif isinstance(x, dict):
            outputs = self._model(**x)
        else:
            raise TypeError(f"Expected tensor or tuple/dict of tensors; found {type(x)}.")

        if isinstance(outputs, tuple) and len(outputs) == 1:
            return outputs[0]
        else:
            return outputs

    @classmethod
    def from_state_dict(cls, state_dict):
        raise NotImplementedError(
            "RenateWrapper does not support `from_state_dict`. Instantiate the object using the "
            "standard constructor, then call `load_state_dict`."
        )


def solution(n, m):
    prime = [i for i in range(1, min(n,m)+1) if min(n,m) % i == 0]
    answer = []

    for i in reversed(prime):
        if max(n, m) % i == 0:
            answer.append(i)
            break

    answer.append(answer[0] * (n/answer[0]) * (m/answer[0]))

    return answer

lv1/최대공약수와 최소공배수.py

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


import os
import pypdfium2 as pdfium

files = []
for file in os.listdir('.'):
    #split the file name and get the extension
    extension = os.path.splitext(file)[1]
    if extension == '.pdf':
        files.append(os.path.abspath(file))

    # Load a pdf document
for filepath in files:
    filename = os.path.basename(filepath)
    filename = os.path.splitext(filename)[0]
    pdf = pdfium.PdfDocument(filepath)
    page_indices = [i for i in range(len(pdf))]
    renderer = pdf.render_to(pdfium.BitmapConv.pil_image, page_indices = page_indices)
    if not os.path.exists(filename):
        #make a directory to store the images with the same name as the pdf file
        os.mkdir(filename)
        #check if the current directory is the temporary directory
    if os.getcwd() != filename:
        #change directory to the newly created directory
        os.chdir(filename)

    for image, index in zip(renderer, page_indices):
        #save the image
        image.save("output_%02d.jpg" % index)

    #change directory to the parent directory
    os.chdir('..')


from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import Thread, Post

class MySignUpForm(UserCreationForm):
    # imie_i_nazwisko = forms.CharField(max_length=100, required=False, help_text='Nie wymagane')
    first_name = forms.CharField(label='Imię', max_length=100, required=False, help_text='Nie wymagane')
    last_name = forms.CharField(label='Nazwisko', max_length=100, required=False, help_text='Nie wymagane')
    email = forms.EmailField(max_length=150)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )

    def __init__(self, *args, **kwargs):
        super(MySignUpForm, self).__init__(*args, **kwargs)

        self.fields['username'].help_text = 'Maksymalnie 150 znaków. Jedynie litery, cyfry i @/./+/-/_.'
        self.fields['password1'].help_text = 'Twoje hasło musi zawierać co najmniej 8 znaków. '

class ThreadForm(forms.ModelForm):

    class Meta:
        model = Thread
        fields = {'category_name', 'title', 'description'}

    def __init__(self, *args, **kwargs):
        super(ThreadForm, self).__init__(*args, **kwargs)
        self.fields['description'].widget.attrs['style'] = 'width:800px; height:150px'
        self.fields['title'].widget.attrs['style'] = 'width:600px; height:25px'
        self.fields['category_name'].widget.attrs['style'] = 'width:300px; height:25px'
        self.fields.keyOrder = ['title', 'category_name', 'description']
        self.fields['category_name'].label = 'Kategoria'
        self.fields['title'].label = 'Tytuł'
        self.fields['description'].label = 'Opis'

class PostForm(forms.ModelForm):

    class Meta:
        model = Post
        fields = {'text'}

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.fields['text'].label = 'Komentarz'
        self.fields['text'].widget.attrs['cols'] = 100
        self.fields['text'].widget.attrs['rows'] = 15

class ProfileUpdateForm(forms.ModelForm):
    email = forms.EmailField(required=False, max_length=150)
    # imie_i_nazwisko = forms.CharField(required=False)
    first_name = forms.CharField(label='Imię', max_length=100, required=False, help_text='Nie wymagane')
    last_name = forms.CharField(label='Nazwisko', max_length=100, required=False, help_text='Nie wymagane')

    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name')


import webapp2
import jinja2
import os
import datetime
from google.appengine.api import users
from google.appengine.ext import ndb
from model import RoomModel, BookingModel

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True
)

class AddRoom(webapp2.RequestHandler):  # Add room class handles for addition of room
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        user = users.get_current_user()

        if user:
            main_header = 'Please Add Room Information Below'
            login_logout = 'Logout'
            login_logout_url = users.create_logout_url(self.request.uri)

        else:
            main_header = 'Please Login to Access This Page..!!'
            login_logout = 'Login'
            login_logout_url = users.create_login_url(self.request.uri)

        template_values = {
            'main_header': main_header,
            'login_logout': login_logout,
            'login_logout_url': login_logout_url,
            'user': user
        }
        template = JINJA_ENVIRONMENT.get_template('addRoom.html')
        self.response.write(template.render(template_values))

    def post(self):  # Handles addition of data into ndb
        print("Post method of booking")
        self.response.headers['Content-Type'] = 'text/html'
        user = users.get_current_user()
        error_message = ''

        if user:
            main_header = 'Please Add Room Information Below'
            login_logout = 'Logout'
            login_logout_url = users.create_logout_url(self.request.uri)
            user_given_room_name = self.request.get("room_name")
            my_model_key = ndb.Key('RoomModel', user_given_room_name)
            my_room = my_model_key.get()
            if my_room:  # Validating if the room is already present
                error_message = 'Room Already exists ! Please use add booking option for booking'
            else:
                my_room = RoomModel(id=user_given_room_name)
                my_room.roomName = user_given_room_name
                my_room.createdBy = user.email()
                my_room.put()
                self.redirect('/')

        else:
            main_header = 'Please Login to Access This Page..!!'
            login_logout = 'Login'
            login_logout_url = users.create_login_url(self.request.uri)
        template_values = {
            'main_header': main_header,
            'login_logout': login_logout,
            'login_logout_url': login_logout_url,
            'user': user,
            'error_message': error_message
        }
        template = JINJA_ENVIRONMENT.get_template('addRoom.html')
        self.response.write(template.render(template_values))

class AddBooking(webapp2.RequestHandler):  # Handles add booking

    def post(self):
        self.response.headers['Content-Type'] = 'text/html'
        user = users.get_current_user()
        error_message = ''
        user_given_room_name = self.request.get("room_name")

        if user:
            main_header = 'Please Add Room Information Below'
            login_logout = 'Logout'
            login_logout_url = users.create_logout_url(self.request.uri)

            page_id = self.request.get("page_id")
            if page_id == "addBooking":
                user_given_room_name = self.request.get("room_name")
                user_room_key = ndb.Key('RoomModel', user_given_room_name)
                room_ndb_object = user_room_key.get()
                existing_bookings = room_ndb_object.booking
                date_time_format = "%Y-%m-%dT%H:%M"
                # Read user inputs
                new_start_date = self.request.get("startDate")
                new_start_time = self.request.get("startTime")
                new_end_date = self.request.get("endDate")
                new_end_time = self.request.get("endTime")
                new_booking_id = self.request.get("bookingId")
                # Convert user input to date format
                new_start_date_time = datetime.datetime.strptime(new_start_date + "T" + new_start_time,
                                                                 date_time_format)
                new_end_date_time = datetime.datetime.strptime(new_end_date+"T"+new_end_time, date_time_format)
                add_booking_flag = True
                print(new_start_date_time)
                print(new_end_date_time)
                if new_start_date_time >= new_end_date_time:  # Check for start date and time
                    add_booking_flag = False
                    error_message = "Start Time should always be greater than End Time"
                if add_booking_flag and (new_start_date_time < datetime.datetime.now()):  # Check for past Bookings
                    add_booking_flag = False
                    error_message = "No past bookings allowed. Current time is : {0} GMT(No daylight savings)".format(
                        datetime.datetime.now().strftime("%Y-%m-%d  %H:%M"))
                # Check for add_booking_flag which will be false if any of above conditions are true.!
                if add_booking_flag is True:
                    for each_booking in existing_bookings:
                        existing_start_date_time = each_booking.startTime
                        existing_end_date_time = each_booking.endTime
                        existing_booking_id = each_booking.bookingId
                        if existing_booking_id == new_booking_id:
                            add_booking_flag = False
                            error_message = "Please select unique Booking id. Booking already exists"
                            break
                        if new_start_date_time <= existing_end_date_time and \
                                new_end_date_time >= existing_start_date_time:
                            add_booking_flag = False
                            error_message = "Booking Overlaps, " \
                                            "Please view bookings and make new non-overlapping booking"
                            break
                if add_booking_flag:  # Checks if the booking has passed all the checks,
                    # if yes, will be persisted to ndb
                    new_booking = BookingModel(startTime=new_start_date_time,
                                               endTime=new_end_date_time,
                                               bookingId=new_booking_id,
                                               userCreated=user.email()
                                               )  # type: BookingModel
                    room_ndb_object.booking.append(new_booking)
                    room_ndb_object.put()
                    self.redirect('/')

        else:
            main_header = 'Please Login to Access This Page..!!'
            login_logout = 'Login'
            login_logout_url = users.create_login_url(self.request.uri)
        template_values = {
            'main_header': main_header,
            'login_logout': login_logout,
            'login_logout_url': login_logout_url,
            'user': user,
            'error_message': error_message,
            'room_name': user_given_room_name
        }
        template = JINJA_ENVIRONMENT.get_template('addBooking.html')
        self.response.write(template.render(template_values))


from unittest import TestCase

class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        """
        :type gas: List[int]
        :type cost: List[int]
        :rtype: int
        """
        # First go through and see where the cost to get to the next station is
        possible_starting_points = set()
        num_of_stations = len(gas)
        for i, gas_available in enumerate(gas):
            # If the gas availabe is greater than the cost needed to get to the next station
            if gas_available >= cost[i]:
                possible_starting_points.add(i)

        for starting_point in list(possible_starting_points):
            current_gas = 0
            for i in self.get_wrap_index(starting_point, num_of_stations):
                if current_gas < 0:
                    break
                current_gas = current_gas + gas[i] - cost[i]
            if current_gas < 0:
                possible_starting_points.remove(starting_point)
        if len(possible_starting_points) == 1:
            return possible_starting_points.pop()
        else:
            return -1

    @staticmethod
    def get_wrap_index(s, length):
        starting = s
        while True:
            yield s
            s = (s + 1) % length
            if s == starting:
                return

class SolutionTest(TestCase):
    def setUp(self):
        self.sol = Solution()

    def test_can_complete_circuit(self):
        gas = [1, 2, 3, 4, 5]
        cost = [3, 4, 5, 1, 2]
        o = self.sol.canCompleteCircuit(gas, cost)
        self.assertEqual(o, 3)

    def test2(self):
        gas = [3, 3, 4]
        cost = [3, 4, 4]
        o = self.sol.canCompleteCircuit(gas, cost)
        self.assertEqual(o, -1)

    def test3(self):
        gas = [5, 1, 2, 3, 4]
        cost = [4, 4, 1, 5, 1]
        o = self.sol.canCompleteCircuit(gas, cost)
        self.assertEqual(o, 4)


import numpy as np
import matplotlib.pyplot as plt

def main():

    N_bandits = 10
    bandits_probs = abs(np.random.normal(size=N_bandits))
    N_experiments = 100
    N_episodes = 100

    class Bandit:
        def __init__(self,bandits_probs):
            self.N = len(bandits_probs)
            self.probs = bandits_probs

        def get_reward(self,action):
            rand = abs(np.random.normal())
            reward = 1 if (rand > self.probs[action]) else 0
            return reward

    class Agent:
        def __init__(self,bandit):
            self.k = np.zeros(bandit.N,dtype=np.int)
            self.Q = np.zeros(bandit.N,dtype=np.float)

        def update_Q(self,action,reward):
            self.k[action] +=1
            self.Q[action] += (reward - self.Q[action])/self.k[action]

    def experiment(agent,bandit,N_episodes):

        action_history = []
        reward_history = np.zeros(bandit.N)
        for i in range(N_episodes):
            for j in range(N_bandits):
                reward = bandit.get_reward(j)

                agent.update_Q(j,reward)

                #action_history.append(action)
                reward_history[j] = reward

        print(reward_history.shape)
        return np.array(reward_history)

    reward_history_avg = np.zeros(N_bandits)

    for i in range(N_experiments):
        bandit = Bandit(bandits_probs)
        agent = Agent(bandit)

        reward_history = experiment(agent,bandit,N_episodes)

        reward_history_avg += reward_history

    reward_history_avg /=np.float(N_experiments)
    best_arm = np.argmax(reward_history_avg)
    print("Reward history avg ={}".format(reward_history_avg))
    print("The Best arm to choose after {} experiments is {} with a reward of {}".format(N_experiments,best_arm,reward_history_avg.max()))

    plt.plot(reward_history_avg)
    plt.show()

main()


from flask import Flask, request, jsonify
from flask_cors import CORS
from pymongo import MongoClient
import subprocess
import json
import os

app = Flask(__name__)
CORS(app)

# Load the config from config.json
with open('config/config.json') as config_file:
    config_data = json.load(config_file)

# Connect to the MongoDB database
client = MongoClient(config_data['database'])
db = client['spotify']
collection = db['search_input']

@app.route('/search-artist', methods=['POST'])
def search_artist():
    artist_name = request.json['artistName']
    print(artist_name)

    # Update the search_input document with the artist name
    collection.update_one({}, {"$set": {"searchInput": artist_name}}, upsert=True)

    # Execute the data.js script
    subprocess.run(['node', 'data.js'])

    response = {
        'artistName': artist_name
    }
    return jsonify(response)

if __name__ == '__main__':
    # Use the PORT environment variable provided by Heroku
    port = int(os.environ.get(5000))
    app.run(host='0.0.0.0', port=port)


from tokenObj import *
from character import *
from error import IllegalCharError, InvalidSyntaxError
import string

# CONSTANTES
CHAR_IDENTIFICADOR = string.ascii_lowercase + \
    string.ascii_uppercase + "_" + "1234567890" + "()"

CHAR_OPERADOR = "-+"

#######################################
# TOKENS
#######################################
# Constantes tokens tipos
TT_UNION = 'UNION'
TT_DIF = 'DIF'
TT_ID = 'ID'
TT_CHAR = 'CHAR'

class CharacterPreprocess:
    def __init__(self, string):
        self.posActual = -1
        self.string = string
        self.operaciones = []
        self.dobleComillaAbierta = False
        self.comillaAbierta = False

    def avanzarChar(self):
        if self.posActual < len(self.string) - 1:
            self.posActual += 1
        else:
            self.posActual = None

    def avanzarOperaciones(self):
        if self.posActual < len(self.operaciones) - 1:
            self.posActual += 1
        else:
            self.posActual = None

    def retroceder(self):
        if self.posActual != None:
            self.posActual -= 1

    def splitString(self):
        '''
        Creamos los tokens
        '''
        while self.posActual != None:
            self.avanzarChar()
            if self.posActual == None:
                break

            # identificar cadena de char
            if self.string[self.posActual] == '"':
                self.plainString()

            # identificar identificador
            elif self.string[self.posActual] in CHAR_IDENTIFICADOR:
                self.retroceder()
                self.identificador()

            # identificar operador
            elif self.string[self.posActual] in CHAR_OPERADOR:
                if self.string[self.posActual] == '+':
                    self.operaciones.append(Token(TT_UNION))
                elif self.string[self.posActual] == '-':
                    self.operaciones.append(Token(TT_DIF))

    def identificador(self):
        tempIdentificador = ""
        self.avanzarChar()

        '''
        Caso identificador character anteriormente
        definido en el .atg
        '''
        while self.string[self.posActual] in CHAR_IDENTIFICADOR:
            tempIdentificador += self.string[self.posActual]
            self.avanzarChar()
            if self.posActual == None:
                break

        self.retroceder()

        '''
        Caso CHR(23)
        '''
        if (tempIdentificador.find('CHR(') != -1):
            tempString = chr(
                int(tempIdentificador[tempIdentificador.find('(')+1:tempIdentificador.find(')')]))
            self.operaciones.append(Token(TT_CHAR, Character(tempString)))
        else:
            self.operaciones.append(Token(TT_ID, tempIdentificador))

    def plainString(self):
        '''
        Caso "abcd"
        '''
        tempString = ""
        self.avanzarChar()
        while self.string[self.posActual] != '"':
            tempString += self.string[self.posActual]
            self.avanzarChar()

        # print(tempString)
        self.operaciones.append(Token(TT_CHAR, Character(tempString)))

    def operar(self, expresionesTratadas):
        '''
        Operamos dada una lista de tokens
        '''
        self.posActual = -1
        operacionCola = []
        errores = []
        hayError = False
        while self.posActual != None:
            self.avanzarOperaciones()
            if self.posActual == None:
                break

            # solo es char
            if self.operaciones[self.posActual].tipo == TT_CHAR:
                operacionCola.append(self.operaciones[self.posActual])

            # operacion de union
            elif self.operaciones[self.posActual].tipo == TT_UNION:
                self.avanzarOperaciones()
                char1 = operacionCola.pop().valor
                if (self.posActual == None):
                    errores.append(InvalidSyntaxError(
                        "Se esperaba set o identificador"))
                    hayError = True
                    break

                try:
                    char2 = self.operaciones[self.posActual].valor.elementos
                except:
                    errores.append(InvalidSyntaxError(
                        "Se esperaba set o identificador"))
                    hayError = True
                    break
                char1.union(char2)

                operacionCola.append(Token(TT_CHAR, char1))

            # operacion de diferencia
            elif self.operaciones[self.posActual].tipo == TT_DIF:
                self.avanzarOperaciones()
                char1 = operacionCola.pop().valor
                if (self.posActual == None):
                    errores.append(InvalidSyntaxError(
                        "Se esperaba set o identificador"))
                    hayError = True
                    break
                try:
                    char2 = self.operaciones[self.posActual].valor.elementos
                except:
                    errores.append(InvalidSyntaxError(
                        "Se esperaba set o identificador"))
                    hayError = True
                    break
                char1.diferencia(char2)
                #print(f'{char1} \n {char2}')

                operacionCola.append(Token(TT_CHAR, char1))

            # solo es un identificador
            elif self.operaciones[self.posActual].tipo == TT_ID:
                if self.operaciones[self.posActual].valor in expresionesTratadas:
                    valorId = expresionesTratadas[self.operaciones[self.posActual].valor]

                    operacionCola.append(Token(TT_CHAR, valorId))
                else:
                    print('error')
                    hayError = True
                    errores.append(IllegalCharError(
                        f'Este identificador es usado antes de ser declarado: {self.operaciones[self.posActual].valor}'))
                    break

        if hayError:
            resultado = []
        else:
            resultado = operacionCola.pop().valor

        return resultado, errores


from __future__ import unicode_literals

"""Benchmark for SQLAlchemy.

An adaptation of Robert Brewers' ZooMark speed tests. """

import datetime
from sqlalchemy import Table, Column, Integer, Unicode, Date, \
    DateTime, Time, Float, Sequence, ForeignKey,  \
    select, join, and_, outerjoin, func
from sqlalchemy.testing import replay_fixture

ITERATIONS = 1

class ZooMarkTest(replay_fixture.ReplayFixtureTest):

    """Runs the ZooMark and squawks if method counts vary from the norm."""

    __requires__ = 'cpython',
    __only_on__ = 'postgresql+psycopg2'

    def _run_steps(self, ctx):
        with ctx():
            self._baseline_1a_populate()
        with ctx():
            self._baseline_2_insert()
        with ctx():
            self._baseline_3_properties()
        with ctx():
            self._baseline_4_expressions()
        with ctx():
            self._baseline_5_aggregates()
        with ctx():
            self._baseline_6_editing()
        with ctx():
            self._baseline_7_multiview()

    def setup_engine(self):
        self._baseline_1_create_tables()

    def teardown_engine(self):
        self._baseline_8_drop()

    def _baseline_1_create_tables(self):
        Table(
            'Zoo',
            self.metadata,
            Column('ID', Integer, Sequence('zoo_id_seq'),
                   primary_key=True, index=True),
            Column('Name', Unicode(255)),
            Column('Founded', Date),
            Column('Opens', Time),
            Column('LastEscape', DateTime),
            Column('Admission', Float),
        )
        Table(
            'Animal',
            self.metadata,
            Column('ID', Integer, Sequence('animal_id_seq'),
                   primary_key=True),
            Column('ZooID', Integer, ForeignKey('Zoo.ID'), index=True),
            Column('Name', Unicode(100)),
            Column('Species', Unicode(100)),
            Column('Legs', Integer, default=4),
            Column('LastEscape', DateTime),
            Column('Lifespan', Float(4)),
            Column('MotherID', Integer, ForeignKey('Animal.ID')),
            Column('PreferredFoodID', Integer),
            Column('AlternateFoodID', Integer),
        )
        self.metadata.create_all()

    def _baseline_1a_populate(self):
        Zoo = self.metadata.tables['Zoo']
        Animal = self.metadata.tables['Animal']
        engine = self.metadata.bind
        wap = engine.execute(Zoo.insert(), Name='Wild Animal Park',
                             Founded=datetime.date(2000, 1, 1),
                             Opens=datetime.time(8, 15, 59),
                             LastEscape=datetime.datetime(
                                 2004, 7, 29, 5, 6, 7),
                             Admission=4.95).inserted_primary_key[0]
        sdz = engine.execute(Zoo.insert(), Name='San Diego Zoo',
                             Founded=datetime.date(1935, 9, 13),
                             Opens=datetime.time(9, 0, 0),
                             Admission=0).inserted_primary_key[0]
        engine.execute(Zoo.insert(inline=True), Name='Montr\xe9al Biod\xf4me',
                       Founded=datetime.date(1992, 6, 19),
                       Opens=datetime.time(9, 0, 0), Admission=11.75)
        seaworld = engine.execute(Zoo.insert(), Name='Sea_World',
                                  Admission=60).inserted_primary_key[0]

        # Let's add a crazy futuristic Zoo to test large date values.

        engine.execute(
            Zoo.insert(), Name='Luna Park',
            Founded=datetime.date(2072, 7, 17),
            Opens=datetime.time(0, 0, 0),
            Admission=134.95).inserted_primary_key[0]

        # Animals

        leopardid = engine.execute(Animal.insert(), Species='Leopard',
                                   Lifespan=73.5).inserted_primary_key[0]
        engine.execute(Animal.update(Animal.c.ID == leopardid), ZooID=wap,
                       LastEscape=datetime.datetime(
                           2004, 12, 21, 8, 15, 0, 999907,)
                       )
        engine.execute(
            Animal.insert(),
            Species='Lion', ZooID=wap).inserted_primary_key[0]

        engine.execute(Animal.insert(), Species='Slug', Legs=1, Lifespan=.75)
        engine.execute(Animal.insert(), Species='Tiger',
                       ZooID=sdz).inserted_primary_key[0]

        # Override Legs.default with itself just to make sure it works.

        engine.execute(Animal.insert(inline=True), Species='Bear', Legs=4)
        engine.execute(Animal.insert(inline=True), Species='Ostrich', Legs=2,
                       Lifespan=103.2)
        engine.execute(Animal.insert(inline=True), Species='Centipede',
                       Legs=100)
        engine.execute(Animal.insert(), Species='Emperor Penguin',
                       Legs=2, ZooID=seaworld).inserted_primary_key[0]
        engine.execute(Animal.insert(), Species='Adelie Penguin',
                       Legs=2, ZooID=seaworld).inserted_primary_key[0]
        engine.execute(Animal.insert(inline=True), Species='Millipede',
                       Legs=1000000, ZooID=sdz)

        # Add a mother and child to test relationships

        bai_yun = engine.execute(
            Animal.insert(),
            Species='Ape',
            Name='Bai Yun',
            Legs=2).inserted_primary_key[0]
        engine.execute(Animal.insert(inline=True), Species='Ape',
                       Name='Hua Mei', Legs=2, MotherID=bai_yun)

    def _baseline_2_insert(self):
        Animal = self.metadata.tables['Animal']
        i = Animal.insert(inline=True)
        for x in range(ITERATIONS):
            i.execute(Species='Tick', Name='Tick %d' % x, Legs=8)

    def _baseline_3_properties(self):
        Zoo = self.metadata.tables['Zoo']
        Animal = self.metadata.tables['Animal']
        engine = self.metadata.bind

        def fullobject(select):
            """Iterate over the full result row."""

            return list(engine.execute(select).first())

        for x in range(ITERATIONS):

            # Zoos

            fullobject(Zoo.select(Zoo.c.Name == 'Wild Animal Park'))
            fullobject(Zoo.select(Zoo.c.Founded ==
                       datetime.date(1935, 9, 13)))
            fullobject(Zoo.select(Zoo.c.Name ==
                       'Montr\xe9al Biod\xf4me'))
            fullobject(Zoo.select(Zoo.c.Admission == float(60)))

            # Animals

            fullobject(Animal.select(Animal.c.Species == 'Leopard'))
            fullobject(Animal.select(Animal.c.Species == 'Ostrich'))
            fullobject(Animal.select(Animal.c.Legs == 1000000))
            fullobject(Animal.select(Animal.c.Species == 'Tick'))

    def _baseline_4_expressions(self):
        Zoo = self.metadata.tables['Zoo']
        Animal = self.metadata.tables['Animal']
        engine = self.metadata.bind

        def fulltable(select):
            """Iterate over the full result table."""

            return [list(row) for row in engine.execute(select).fetchall()]

        for x in range(ITERATIONS):
            assert len(fulltable(Zoo.select())) == 5
            assert len(fulltable(Animal.select())) == ITERATIONS + 12
            assert len(fulltable(Animal.select(Animal.c.Legs == 4))) \
                == 4
            assert len(fulltable(Animal.select(Animal.c.Legs == 2))) \
                == 5
            assert len(
                fulltable(
                    Animal.select(
                        and_(
                            Animal.c.Legs >= 2,
                            Animal.c.Legs < 20)))) == ITERATIONS + 9
            assert len(fulltable(Animal.select(Animal.c.Legs > 10))) \
                == 2
            assert len(fulltable(Animal.select(Animal.c.Lifespan
                                               > 70))) == 2
            assert len(fulltable(Animal.select(Animal.c.Species.
                                               startswith('L')))) == 2
            assert len(fulltable(Animal.select(Animal.c.Species.
                                               endswith('pede')))) == 2
            assert len(fulltable(Animal.select(Animal.c.LastEscape
                                               != None))) == 1
            assert len(
                fulltable(
                    Animal.select(
                        None == Animal.c.LastEscape))) == ITERATIONS + 11

            # In operator (containedby)

            assert len(fulltable(Animal.select(Animal.c.Species.like('%pede%'
                                                                     )))) == 2
            assert len(
                fulltable(
                    Animal.select(
                        Animal.c.Species.in_(
                            ['Lion', 'Tiger', 'Bear'])))) == 3

            # Try In with cell references
            class thing(object):
                pass

            pet, pet2 = thing(), thing()
            pet.Name, pet2.Name = 'Slug', 'Ostrich'
            assert len(
                fulltable(
                    Animal.select(
                        Animal.c.Species.in_([pet.Name, pet2.Name])))) == 2

            # logic and other functions

            assert len(fulltable(Animal.select(Animal.c.Species.like('Slug'
                                                                     )))) == 1
            assert len(fulltable(Animal.select(Animal.c.Species.like('%pede%'
                                                                     )))) == 2
            name = 'Lion'
            assert len(
                fulltable(
                    Animal.select(
                        func.length(
                            Animal.c.Species) == len(name)))) == ITERATIONS + 3
            assert len(
                fulltable(
                    Animal.select(
                        Animal.c.Species.like('%i%')))) == ITERATIONS + 7

            # Test now(), today(), year(), month(), day()

            assert len(
                fulltable(
                    Zoo.select(
                        and_(
                            Zoo.c.Founded != None,
                            Zoo.c.Founded < func.current_timestamp(
                                _type=Date))))) == 3
            assert len(
                fulltable(
                    Animal.select(
                        Animal.c.LastEscape == func.current_timestamp(
                            _type=Date)))) == 0
            assert len(
                fulltable(
                    Animal.select(
                        func.date_part(
                            'year',
                            Animal.c.LastEscape) == 2004))) == 1
            assert len(
                fulltable(
                    Animal.select(
                        func.date_part(
                            'month',
                            Animal.c.LastEscape) == 12))) == 1
            assert len(
                fulltable(
                    Animal.select(
                        func.date_part(
                            'day',
                            Animal.c.LastEscape) == 21))) == 1

    def _baseline_5_aggregates(self):
        Animal = self.metadata.tables['Animal']
        Zoo = self.metadata.tables['Zoo']
        engine = self.metadata.bind

        for x in range(ITERATIONS):

            # views

            view = engine.execute(select([Animal.c.Legs])).fetchall()
            legs = sorted([x[0] for x in view])
            expected = {
                'Leopard': 73.5,
                'Slug': .75,
                'Tiger': None,
                'Lion': None,
                'Bear': None,
                'Ostrich': 103.2,
                'Centipede': None,
                'Emperor Penguin': None,
                'Adelie Penguin': None,
                'Millipede': None,
                'Ape': None,
                'Tick': None,
            }
            for species, lifespan in engine.execute(
                    select([Animal.c.Species, Animal.c.Lifespan])).fetchall():
                assert lifespan == expected[species]
            expected = ['Montr\xe9al Biod\xf4me', 'Wild Animal Park']
            e = select([Zoo.c.Name],
                       and_(Zoo.c.Founded != None,
                            Zoo.c.Founded <= func.current_timestamp(),
                            Zoo.c.Founded >= datetime.date(1990,
                                                           1,
                                                           1)))
            values = [val[0] for val in engine.execute(e).fetchall()]
            assert set(values) == set(expected)

            # distinct

            legs = [
                x[0]
                for x in engine.execute(
                    select([Animal.c.Legs],
                           distinct=True)).fetchall()]
            legs.sort()

    def _baseline_6_editing(self):
        Zoo = self.metadata.tables['Zoo']
        engine = self.metadata.bind
        for x in range(ITERATIONS):

            # Edit

            SDZ = engine.execute(Zoo.select(Zoo.c.Name == 'San Diego Zoo'
                                            )).first()
            engine.execute(
                Zoo.update(
                    Zoo.c.ID == SDZ['ID']),
                Name='The San Diego Zoo',
                Founded=datetime.date(1900, 1, 1),
                Opens=datetime.time(7, 30, 0), Admission='35.00')

            # Test edits

            SDZ = engine.execute(Zoo.select(Zoo.c.Name == 'The San Diego Zoo'
                                            )).first()
            assert SDZ['Founded'] == datetime.date(1900, 1, 1), \
                SDZ['Founded']

            # Change it back

            engine.execute(Zoo.update(Zoo.c.ID == SDZ['ID'
                                                      ]), Name='San Diego Zoo',
                           Founded=datetime.date(1935, 9, 13),
                           Opens=datetime.time(9, 0, 0),
                           Admission='0')

            # Test re-edits

            SDZ = engine.execute(Zoo.select(Zoo.c.Name == 'San Diego Zoo'
                                            )).first()
            assert SDZ['Founded'] == datetime.date(1935, 9, 13)

    def _baseline_7_multiview(self):
        Zoo = self.metadata.tables['Zoo']
        Animal = self.metadata.tables['Animal']
        engine = self.metadata.bind

        def fulltable(select):
            """Iterate over the full result table."""

            return [list(row) for row in engine.execute(select).fetchall()]

        for x in range(ITERATIONS):
            fulltable(
                select(
                    [Zoo.c.ID] + list(Animal.c),
                    Zoo.c.Name == 'San Diego Zoo',
                    from_obj=[join(Zoo, Animal)]))
            Zoo.select(Zoo.c.Name == 'San Diego Zoo')
            fulltable(
                select(
                    [Zoo.c.ID, Animal.c.ID],
                    and_(
                        Zoo.c.Name == 'San Diego Zoo',
                        Animal.c.Species == 'Leopard'
                    ),
                    from_obj=[join(Zoo, Animal)])
            )

            # Now try the same query with INNER, LEFT, and RIGHT JOINs.

            fulltable(select([
                Zoo.c.Name, Animal.c.Species],
                from_obj=[join(Zoo, Animal)]))
            fulltable(select([
                Zoo.c.Name, Animal.c.Species],
                from_obj=[outerjoin(Zoo, Animal)]))
            fulltable(select([
                Zoo.c.Name, Animal.c.Species],
                from_obj=[outerjoin(Animal, Zoo)]))

    def _baseline_8_drop(self):
        self.metadata.drop_all()


import pymongo
from pymongo import MongoClient
import time
import pprint

client = MongoClient('localhost', 27017)
db = client['sahamyab']
series_collection = db['tweets']

### Adding index to field
series_collection.create_index([("mediaContentType", pymongo.DESCENDING), ("parentId", pymongo.DESCENDING)])

start_time = time.time()
res = series_collection.find({
    '$and':[
              {'mediaContentType':'image/jpeg'}, {'parentId':{ '$exists': True } }
           ]}, {"senderName": 1, '_id':0})

end_time = time.time()
delta_time = end_time - start_time
print('run time:', delta_time)

lis = []
for i in res:
    lis.append(i['senderName'])
    pprint.pprint(i)

NoSQL Databases (Cassandra, MongoDB, Neo4j, Elasticsearch)/MongoDB/1000 twiits/game5_1.py


number_of_lines = int(input())

d = {}
for i in range(0,number_of_lines,1):
    data = input().split(' ')
    key = data[0]
    value = data[1]
    d[value] = key #reversed to get over non duplicate keys.

last = input()

print(list(d.values()).count(d[last]))


import os
import cv2
import numpy as np
from flask import Flask, request, jsonify
from src.preprocessing import segment_image, match_template

app = Flask(__name__)

@app.route("/", methods=["GET"])
def test():
    return "API Working"

@app.route("/match", methods=["GET"])
def similar_template():
    test_image_path = request.args["image_path"]
    TEMPLATE_DIR = request.args["template_folder_path"]
    template_image_file = [
        os.path.join(TEMPLATE_DIR, item) for item in os.listdir(TEMPLATE_DIR)
    ]

    image_segment = segment_image(test_image_path).compute()
    template_score = []
    template_name = []
    for template_image in template_image_file:
        template = cv2.imread(template_image)
        gray_image = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
        cumul_point = 0
        for segment in image_segment:
            cumul_point = cumul_point + match_template(
                segment,
                gray_image).compute()
        template_score.append(cumul_point)
        template_name.append(template_image)

    output = {
        "input_filename": test_image_path,
        "template_file_name": template_name[np.argmax(template_score)],
    }
    return jsonify(output)

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5000)