import os

import tensorflow as tf

import Utils.files_preprocessing
from Data.FinancialPhrasebank import FinancialPhrasebank
from Data.IMDB import IMDB

class Dataset:

    def __init__(self, dataset_path, dataset_dir, train_dir, test_dir):
        self.dataset_path = dataset_path
        self.dataset_file = None
        self.dataset = None
        self.raw_train_ds = None
        self.raw_val_ds = None
        self.raw_test_ds = None
        self.dataset_dir = dataset_dir
        self.train_dir = train_dir
        self.train_pos_dir = os.path.join(train_dir, 'pos')
        self.train_neg_dir = os.path.join(train_dir, 'neg')
        self.test_dir = test_dir
        self.test_pos_dir = os.path.join(test_dir, 'pos')
        self.test_neg_dir = os.path.join(test_dir, 'neg')

        self.check_path()
        self.load_datasets()

        Utils.files_preprocessing.preprocess_all_folders()

        self.split_datasets()

    def check_path(self):
        if not os.path.exists(self.dataset_path):
            os.makedirs(self.dataset_path)

        if not os.path.exists(self.dataset_dir):
            os.makedirs(self.dataset_dir)

        if not os.path.exists(self.train_dir):
            os.makedirs(self.train_dir)
            os.makedirs(self.train_pos_dir)
            os.makedirs(self.train_neg_dir)

        if not os.path.exists(self.test_dir):
            os.makedirs(self.test_dir)
            os.makedirs(self.test_pos_dir)
            os.makedirs(self.test_neg_dir)

    def load_datasets(self):
        IMDB(self.dataset_path, self.dataset_dir, self.train_dir, self.test_dir)
        FinancialPhrasebank(self.dataset_path, self.dataset_dir, self.train_dir, self.test_dir,
                            self.train_pos_dir, self.train_neg_dir,
                            self.test_pos_dir, self.test_neg_dir)

    """
    ## The utility `tf.keras.preprocessing.text_dataset_from_directory` is used to
    ## generate a labeled `tf.data.Dataset` object from a set of text files on disk filed
    ## into class-specific folders.

    ## Let's use it to generate the training, validation, and test datasets. The validation
    ## and training datasets are generated from two subsets of the `train` directory, with 20%
    ## of samples going to the validation dataset and 80% going to the training dataset.
    """

    def split_datasets(self):
        batch_size = 32
        self.raw_train_ds = tf.keras.preprocessing.text_dataset_from_directory(
            self.train_dir,
            batch_size=batch_size,
            validation_split=0.2,
            subset="training",
            seed=1337,
        )
        self.raw_val_ds = tf.keras.preprocessing.text_dataset_from_directory(
            self.train_dir,
            batch_size=batch_size,
            validation_split=0.2,
            subset="validation",
            seed=1337,
        )
        self.raw_test_ds = tf.keras.preprocessing.text_dataset_from_directory(
            self.test_dir, batch_size=batch_size
        )

    def get_train_set(self) -> tf.data.Dataset:
        return self.raw_train_ds

    def get_test_set(self) -> tf.data.Dataset:
        return self.raw_test_ds

    def get_val_set(self) -> tf.data.Dataset:
        return self.raw_val_ds


from typing import overload, Literal, Optional

from pydantic import validate_arguments
from pydantic.dataclasses import dataclass

from prjstore.db import schemas
from prjstore.domain.products.shoes import Shoes
from prjstore.domain.products.simple_product import SimpleProduct
from util.money import Money

@dataclass
class ProductFactory:

    @overload
    @staticmethod
    def create(product_type: Optional[Literal['product']], *args, **kwargs) -> SimpleProduct:
        ...

    @overload
    @staticmethod
    def create(product_type: Literal['shoes'], *args, **kwargs) -> Shoes:
        ...

    @staticmethod
    @validate_arguments
    def create(product_type: Literal['product', 'shoes'] = 'product', *args, **kwargs):
        products = {
            'product': SimpleProduct,
            'shoes': Shoes,
        }
        return products[product_type](*args, **kwargs)

    @staticmethod
    def create_from_schema(product_pd: schemas.product.Product):
        if product_pd.type == "shoes":
            width = Shoes.widths[product_pd.shoes.width] if product_pd.shoes.width else None
            product = ProductFactory.create(
                product_type=product_pd.type, prod_id=product_pd.id, name=product_pd.name,
                price=Money(product_pd.price),
                color=product_pd.shoes.color, size=product_pd.shoes.size,
                length_of_insole=product_pd.shoes.length, width=width)
        else:
            product = ProductFactory.create(
                product_type="product", prod_id=product_pd.id,
                name=product_pd.name, price=(product_pd.price,))
        return product


class Solution(object):
    def uniquePaths(self, grid):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        return self.unique_paths_v1(grid)

    def unique_paths_v1(self, grid):
        """
            My original solution
        """

        # first create 2d array, 1st row and col equal to 1
        if grid[0][0] == 1:
            return 0

        dp = [[1 for j in range(len(grid[0]))] for i in range(len(grid))]

        # mark the obstacles in colums
        for j in range(len(dp[0])):
            if grid[0][j] == 1:
                while j < len(dp[0]):
                    dp[0][j] = 0
                    j += 1
                break
        # mark the obstacles in rows
        for i in range(len(dp)):
            if grid[i][0] == 1:
                while i < len(dp):
                    dp[i][0] = 0
                    i += 1
                break
        # print(dp)
        for i in range(1, len(grid)):
            for j in range(1, len(grid[0])):
                # if there's an obstacle mark that path as 0
                if grid[i][j] == 1:
                    dp[i][j] = 0
                else:
                    dp[i][j] = dp[i-1][j] + dp[i][j-1]

        print(dp)
        return dp[-1][-1]

    def unique_paths_v2(self, grid):
        # first create 2d array, 1st row and col equal to 1
        if grid[0][0] == 1:
            return 0

        # fill the array with zeros
        dp = [[0 for j in range(len(grid[0]))] for i in range(len(grid))]
        # mark the beginng of the path
        dp[0][0] = 1 - grid[0][0]

        # mark the obstacles in colums
        for i in range(1, len(dp[0])):
            # set the element at the current position
            # previous value * (1 - grid's value at the current position)
            # because once we see any obstacle on this path, there no way
            # to continue from now on so set the rest to 0
            dp[0][i] = dp[0][i-1] * (1-grid[0][i])

        # mark the obstacles in colums
        for i in range(1, len(dp)):
            dp[i][0] = dp[i-1][0] * (1-grid[i][0])

        print(dp)

        for i in range(1, len(grid)):
            for j in range(1, len(grid[0])):
                dp[i][j] = (dp[i-1][j] + dp[i][j-1]) * (1-grid[i][j])

        print(dp)
        return dp[-1][-1]

matrix = [
    [0, 0, 0],
    [0, 1, 0],
    [0, 0, 0]
]
# matrix = [[1]]
obj = Solution()
result = obj.uniquePaths(matrix)
print(result)


from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, Float

from core.dependencies.database import metadata

User = Table(
    "users",
    metadata,
    Column("id", String, primary_key=True),
    Column("name", String),
    Column("credit", Float),
    Column("payable", Float)
)


# V této ukázce se budeme zabývat jednoduchými objekty, které můžeme
# chápat jako rozšíření složených typů o «metody». Metoda je
# podprogram, který je svázán se svým složeným typem (objektem):
# metoda má vždy parametr, který reprezentuje instanci objektu se
# kterou bude pracovat. V Pythonu tento parametr explicitně uvádíme
# v hlavičce metody (tzn. v seznamu formálních parametrů), a to vždy
# jako první a vždy se jménem ‹self›.

# Při «volání» metod používáme tečkovou notaci, stejně jako
# u zabudovaných typů: máme-li hodnotu ‹items› typu ‹list›, můžeme
# napsat třeba ‹items.append(1)› a víme, že toto volání provede
# nějakou akci nad hodnotou ‹items›. Naše metody se budou chovat
# stejně (ve skutečnosti je totiž ‹append› metoda třídy ‹list›).

# Máme-li hodnotu ‹hospital› typu ‹Hospital› (u objektů také mluvíme
# o instanci ‹hospital› třídy ‹Hospital›), můžeme napsat třeba
# ‹hospital.add_doctor('dept', doc)›. Metodě definované jako
# ‹add_doctor(self, department, doctor)› bude hodnota ‹hospital›
# předána právě parametrem ‹self›, hodnoty uvedené při volání
# v závorkách pak v dalších parametrech.  Přesněji:
#
# • metoda ‹add_doctor› má 3 formální parametry,
# • uvažujeme volání ‹hospital.add_doctor('dept', doc)›.
#
# Parametry se předají takto:
#
# • hodnota ‹hospital› bude předána prvním parametrem (‹self›),
# • druhý parametr, ‹department›, bude mít hodnotu ‹'dept'›,
# • třetí parametr, ‹doctor›, bude mít hodnotu ‹doc›.

from typing import List, Dict

# Třída ‹Doctor› je obyčejný složený typ bez metod, jaké známe
# z předchozí ukázky. Bude mít atributy ‹name› (jméno lékaře) a
# ‹night_shift› (lze-li tomuto lékaři plánovat noční směny).

class Doctor:
    def __init__(self, name: str, night_shift: bool):
        self.name = name
        self.night_shift = night_shift

# Třída ‹Hospital› reprezentuje samotnou nemocnici. Nemocnice má
# lékaře a oddělení, na kterých jednotliví lékaři pracují. Data
# budeme ukládat do slovníku, ve kterém jako klíče použijeme názvy
# jednotlivých oddělení a hodnoty budou seznamy lékařů.

class Hospital:

    # Metoda ‹__init__› inicializuje novou nemocnici. Krom objektu,
    # který bude inicializovat (parametr ‹self›) jí předáme seznam
    # názvů oddělení (parametr ‹departments›). Metoda inicializuje
    # atribut ‹departments›.

    def __init__(self, departments: List[str]):
        self.departments: Dict[str, List[Doctor]] = {}
        for name in departments:
            self.departments[name] = []

    # Metoda ‹add_doctor› zařadí lékaře ‹doctor› na oddělení
    # ‹department›. Vstupní podmínkou je, že toto oddělení
    # v nemocnici existuje.

    def add_doctor(self, department: str, doctor: Doctor) -> None:
        self.departments[department].append(doctor)

    # Protože krom zvláštního zápisu volání je metoda podprogram
    # jako každý jiný, lze metody stejně tak klasifikovat na čisté
    # funkce, predikáty a podobně. Není ale obvyklé mluvit v tomto
    # kontextu o procedurách: metody velmi často mění předaný objekt
    # (parametr ‹self›) – na rozdíl od funkcí budeme tedy
    # předpokládat, není-li uvedeno jinak, že metoda mění objekt
    # ‹self›.

    # Budeme nicméně nadále explicitně uvádět, má-li mít metoda
    # nějaké «jiné» vedlejší efekty. Není-li tedy uvedeno jinak,
    # metoda může měnit «pouze» objekt předaný parametrem ‹self›.
    # Metoda, která je označená jako «čistá» (a tedy i metoda, která
    # je označená jako «predikát») «nemění» ani tento.

    # Metoda (predikát) ‹night_coverage› zkontroluje, že je na
    # každém oddělení aspoň jeden lékař, který může být zařazen na
    # noční směnu.

    def night_coverage(self) -> bool:
        for department, doctor_list in self.departments.items():
            found = False
            for doctor in doctor_list:
                if doctor.night_shift:
                    found = True

            if not found:
                return False
        return True

def main() -> None:
    house = Doctor('House', False)
    chase = Doctor('Chase', True)
    wilson = Doctor('Wilson', True)

    assert house.name == 'House'
    assert not house.night_shift
    assert chase.night_shift

    hospital = Hospital(["oncology", "diagnostics"])
    assert hospital.departments == {"oncology": [], "diagnostics": []}
    assert not hospital.night_coverage()

    hospital.add_doctor("oncology", wilson)
    assert hospital.departments == {"oncology": [wilson], "diagnostics": []}
    assert not hospital.night_coverage()

    hospital.add_doctor("diagnostics", house)
    assert hospital.departments == \
        {"oncology": [wilson], "diagnostics": [house]}
    assert not hospital.night_coverage()

    hospital.add_doctor("diagnostics", chase)
    assert hospital.departments == \
        {"oncology": [wilson], "diagnostics": [house, chase]}
    assert hospital.night_coverage()

if __name__ == "__main__":
    main()


import qrcode
from uuid import uuid4
from db import db

#creation of the QR Code begins here:

#Base URL
website_name = 'https://www.reportbin.link/'

#dynamoDB table creation starts here:

#create a ddb table
dbObject = db("DisposalBin-zetfb4bm65ekhk3iuha4doienm-dev")

#checks if table exists or not
dbObject.checkTable()

#create the QR codes with unique ID's for each bin
numberQR = int(input("How many QR codes would you like to generate?: "))

#vars
count = 0
success = 0

#creates the QR code and uploads it to the DynamoDB storage
for i in range(numberQR):
    binID = 'bin' + str(uuid4())[-6:] #gives the bin a randomized ID
    qr = qrcode.QRCode(version=1, box_size=10, border=1)
    qr.add_data(website_name + binID) #gives the QR code the value of the website and the bin ID
    qr.make(fit = True)
    img = qr.make_image(fill = 'black', back_color = 'white')#QR image creation
    img.save(binID + '.png')

    #appends to the table in DDB storage
    resp = dbObject.appendItem(str(uuid4()), binID, None, None, None, None, None)

    print("Uploading QR: ", binID)
    #check for status 200 and report back to user
    #report back if there a different status code
    if resp['ResponseMetadata']['HTTPStatusCode'] == 200:
        success += 1
    else:
        print(f'This id {count} did not upload: {binID}')
        continue

    #completion!
    print(f'Upload to dynamoDB complete!')


import cgitb
import json
import os
import io
import urllib
from urllib.parse import unquote
from .request import Request, RequestMaker
from .response import Response
from .routers import routes, itinerary
from ..core.errors import MuscularError
import cgi
from urllib.parse import unquote
from ..storage import StorageMapper
storageMapper = StorageMapper()

MAX_LINE = 64 * 1024
MAX_HEADERS = 100
TIMEOUT = 2
MAX_CONNECTIONS = 1000

class Transport:
    """
    Транспорт протокола стратегии
    """

    locale = storageMapper.get('locale')
    logger = storageMapper.get('logger')

    server = None

    def __init__(self):
        pass

    def init_server(self, server):
        self.server = server

    def make_response(self, response):
        pass

    def make_request(self):
        pass

class WsgiTransport(Transport):
    """
    Транспорт стратегии WSGI
    """

    locale = storageMapper.get('locale')
    logger = storageMapper.get('logger')

    def execute(self, *args, **kwargs):
        """
        Исполняем условия транспорта
        :param args:
        :param kwargs[environ]: Окружение запроса
        :param kwargs[start_response]: Метод для ответа
        :return:
        """
        self.environ = kwargs['environ']
        self.start_response = kwargs['start_response']
        return self.handler(self.environ, self.start_response)

    def handler(self, environ, start_response):
        """
        Обработчик транспорта

        :param environ: Переменные окружения запроса
        :param start_response: Метод ответа
        :return:
        """
        request = self.make_request(environ)
        if request is None:
            self.logger.debug(self.locale('HTTP Ответ 400 от сервера "Bad request"'))
            raise MuscularError(400, 'Bad request', 'Malformed request line')
        return self.server.handler(request)

    def make_response(self, response):
        """
        Отправляем ответ
        :param response: объект ответа
        :return:
        """
        self.send_header(str(response.http_status), response.headers)
        yield response.body

    def send_header(self, http_status, headers):
        """
        Передаем заголовки ответа
        :param http_status: HTTP статус ответа
        :param headers: Заголовки
        :return:
        """
        return self.start_response(str(http_status), headers)

    def make_request(self, environ):
        """
        Формируем обхект запроса на основании переменных запроса
        :param environ: Переменные запроса
        :return: Request
        """
        requestMaker = RequestMaker(environ)
        request = requestMaker.make()

        self.logger.debug(self.locale('Формируем тело Request'), extra={
            'request': request.__dict__
        })
        return request

#
# class TCPSocketTransport(HttpTransport):
#     __charset = 'iso-8859-1'
#
#     def execute(self, *args, **kwargs):
#         '''
#         :param args[0]: host
#         :param args[1]: port
#         :param kwargs:
#         :return:
#         '''
#         self.host = args[0]
#         self.port = args[1]
#         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM, proto=0)
#         # server.setblocking(0)
#         server.settimeout(TIMEOUT)
#         all_threads = []
#         try:
#             server.bind((self.host, self.port))
#             server.listen(MAX_CONNECTIONS)
#             while True:
#                 try:
#                     connect, _ = server.accept()
#                     t = threading.Thread(target=self.handler, args=(connect,))
#                     t.start()
#
#                     all_threads.append(t)
#                 except ConnectionResetError:
#                     connect = None
#                 except Exception as e:
#                     # self.send_error(connect, e)
#                     traceback.print_exc(file=sys.stdout)
#         finally:
#             if server:
#                 server.close()
#             for t in all_threads:
#                 t.join()
#         return
#
#     def handler(self, connect):
#         try:
#             self.reader = connect.makefile('rb')
#             self.writer = connect.makefile('wb')
#             request = self.make_request()
#             if request is None:
#                 raise MuscularError(400, 'Bad request', 'Malformed request line')
#             self.server.handler(request)
#         except ConnectionResetError:
#             connect = None
#         except Exception as e:
#             # self.send_error(connect, e)
#             traceback.print_exc(file=sys.stdout)
#         if connect:
#             connect.close()
#
#     def make_response(self, response):
#         try:
#             status_line = f'HTTP/1.1 {response.status} {response.reason}\r\n'
#             self.writer.write(status_line.encode('iso-8859-1'))
#
#             if response.headers:
#                 for (key, value) in response.headers:
#                     header_line = f'{key}: {value}\r\n'
#                     self.writer.write(header_line.encode('iso-8859-1'))
#
#             self.writer.write(b'\r\n')
#
#             if response.body:
#                 self.writer.write(response.body)
#
#             if hasattr(self.writer, 'flush'):
#                 self.writer.flush()
#             if self.writer:
#                 self.writer.close()
#         except Exception as err:
#             print('Internal Error:', err)
#             traceback.print_exc(file=sys.stdout)
#         return
#
#     def make_request(self):
#         headers = []
#         size = 0
#         while True:
#             line = self.reader.readline()
#             if len(line) > MAX_LINE:
#                 raise MuscularError(494, 'Request header too large')
#             if b'Content-Length' in line:
#                 s = re.sub("[\r\n]", "", line)
#                 h = s.split(':')
#                 size = h[1]
#             if line in (b'\r\n', b'\n', b''):
#                 break
#             headers.append(line)
#
#         if len(headers[1:]) > MAX_HEADERS:
#             raise MuscularError(494, 'Too many headers')
#         headers = Parser().parsestr(b''.join(headers[1:]).decode(self.__charset))
#
#         words = headers[0].decode(self.__charset).split()
#         if len(words) != 3:
#             raise HTTPErrorMuscularError(400, 'Bad request', 'Malformed request line')
#         method, target, protocol = words
#         if protocol != 'HTTP/1.1':
#             raise HTTPErrorMuscularError(505, 'HTTP Version Not Supported')
#
#         host = self.headers.get('Host')
#         if not host:
#             raise MuscularError(400, 'Bad request', 'Host header is missing')
#
#         if not size:
#             body = b''
#         else:
#             body = self.reader.read(size)
#
#         request = Request(
#             method=method,
#             protocol=protocol,
#             url=target,
#             server=(self.host, self.port),
#             remote_addr=None,
#             headers=headers,
#             body=body,
#         )
#         if self.reader and hasattr(self.reader, 'close'):
#             self.reader.close()
#         return request

class WsgiServer:
    """
    Объект сервера WSGI
    """

    __transport_class = WsgiTransport
    __transport = WsgiTransport
    __host = 'localhost'
    __port = 80

    locale = storageMapper.get('locale')
    logger = storageMapper.get('logger')

    def __init__(self, host, port):
        self.__host = host
        self.__port = port

        self.__transport = self.__transport_class()
        self.__transport.init_server(self)

    def init_transport(self, transport):
        """
        Инициализируем транспорт
        :param transport: Транспорт
        :return:
        """
        self.__transport_class = transport
        self.__transport = transport()
        self.__transport.init_server(self)

    def execute(self, *args, **kwargs):
        """
        Метод исполнения протокола сервера
        :param args:
        :param kwargs:
        :return:
        """
        try:
            return self.__transport.execute(*args, **kwargs)
        except Exception as e:
            return self.send_error(e)

    def handler(self, request):
        """
        Обработчик сервера
        :param request: Запрос к серверу
        :return:
        """
        self.logger.debug(self.locale('Получаем Request(%s)', request), extra={
            'request': request.__dict__
        })

        headers = []
        for header in request.headers:
            headers.append('%s: %s' % (header, request.headers[header]))
        self.logger.info(self.locale("{protocol} {method} {url}",
                                     method=request.method,
                                     url=request.url,
                                     protocol=request.protocol,
                                     ), extra={
            'request': request.__dict__,
            'method': request.method,
            'url': request.url,
            'protocol': request.protocol,
        })
        self.logger.debug(self.locale("{protocol} {method} {url}\t\n {headers}\t\n {body}",
                                      method=request.method,
                                      url=request.url,
                                      protocol=request.protocol,
                                      headers="\n".join(headers),
                                      body=request.body
                                      ), extra={
            'request': request.__dict__,
            'method': request.method,
            'url': request.url,
            'protocol': request.protocol,
            'headers': "\n".join(headers),
            'body': request.body
        })

        static = routes.get_current_static(request)
        if static:
            return self.handle_static(static, request)
        else:
            return self.handle_request(request)

    def handle_request(self, request):
        """
        Обработчик запроса к серверу
        :param request: Объект запроса
        :return:
        """
        for key, instance in itinerary.instance_list():
            call, dictionary = instance.get_current_route(request)
            if call:
                request.route = call
                request.itinerary = instance
                break

        if request.route:
            if request.route['redirect'] and request.route['redirect'] is not None:
                resp = Response.redirect(request.route['redirect'])
            else:
                try:
                    if hasattr(request.route['handler'], 'controller'):
                        resp = request.route['handler'](request.route['handler'].controller(),
                                                        request=request,
                                                        **dictionary)
                    else:
                        resp = request.route['handler'](request=request, **dictionary)
                except MuscularError as me:
                    raise MuscularError(me.status, me.reason, me.body)

                if not isinstance(resp, Response) and isinstance(resp, str):
                    resp = Response.body(200, body=resp)
                elif not isinstance(resp, Response) and isinstance(resp, bytes):
                    resp = Response.body(200, body=resp)
                elif not isinstance(resp, Response) and isinstance(resp, dict):
                    resp = Response.body(200, body=resp)
                elif not isinstance(resp, Response) and isinstance(resp, tuple):
                    kwargs = {}
                    status = 200
                    if len(resp) >= 0:
                        kwargs['body'] = resp[0]
                    if len(resp) >= 1:
                        status = resp[1]
                    if len(resp) >= 2:
                        kwargs['headers'] = resp[2]
                    resp = Response.body(status, **kwargs)
                elif not isinstance(resp, Response):
                    resp = Response.body(200, body=resp)

                if hasattr(request.itinerary, 'modify_response'):
                    resp = request.itinerary.modify_response(resp)

                headers = []
                for header in resp.headers:
                    headers.append('%s: %s' % (header[0], header[1]))
                self.logger.debug(self.locale("Ответ {http_status}\n {headers}\n {body}",
                                              resp=resp,
                                              headers="\n".join(headers),
                                              http_status=resp.http_status,
                                              body=resp.body,
                                              ))
            return self.__transport.make_response(resp)
        self.logger.debug(self.locale("Не удалось найти подходящий роутер в системе"))
        raise MuscularError(404, 'Not found')

    def handle_static(self, static, request):
        """
        Обработчик статических файлов
        :param static: Путь к диреткории с файлами
        :param request: Объект запроса
        :return:
        """
        path = request.path.replace(static['prefix'] + '/', '', 1)
        resp_file = os.path.join(static['directory'], unquote(path))

        if not os.path.isfile(resp_file):
            self.logger.debug(self.locale("Не удалось найти подходящий файл"))
        try:
            resp = Response.file(200, file=resp_file)

            if static['handler'] is not None:
                resp = static['handler'](resp)

            headers = []
            for header in resp.headers:
                headers.append('%s: %s' % (header[0], header[1]))
            self.logger.debug(self.locale("Ответ STATIC {http_status}\n {headers}\n {body}",
                                          resp=resp,
                                          headers="\n".join(headers),
                                          http_status=resp.http_status,
                                          body=resp.body,
                                          ))

            self.__transport.send_header(resp.status, resp.headers)
            with io.open(resp_file, "rb") as f:
                yield f.read()
        except Exception as e:
            self.logger.exception(self.locale("Обработка статического файла закончилась ошибкой"))
            raise MuscularError(404, 'Not found')

    def send_error(self, err):
        """
        Отправляет ответ ошибки
        :param err: Объект ошибки или текст ошибки
        :return:
        """
        try:
            status = err.status if hasattr(err, 'status') else 500
            reason = err.reason if hasattr(err, 'reason') else str(err)
            body = err.body if hasattr(err, 'body') else str(err)
        except Exception as e:
            status = 500
            reason = b'Internal Server Error'
            body = b'Internal Server Error'

        print('Internal Error:', err)
        self.logger.exception(str(err))
        # traceback.print_exc(file=sys.stdout)

        resp = Response.error(status, reason=reason, body=body)
        # call = routes.get_current_error_handler(resp)

        for key, instance in itinerary.instance_list():
            call = instance.get_current_error_handler(resp)
            if call:
                break

        if call is not None:
            resp = call['handler'](resp)
            if not isinstance(resp, Response) and isinstance(resp, str):
                resp = Response.error(status, reason=reason, body=resp)
            elif not isinstance(resp, Response) and isinstance(resp, bytes):
                resp = Response.error(status, reason=reason, body=resp)
            elif not isinstance(resp, Response) and isinstance(resp, dict):
                resp = Response.error(status, reason=reason, body=resp)
            elif not isinstance(resp, Response) and isinstance(resp, tuple):
                kwargs = {}
                if len(resp) >= 0:
                    kwargs['reason'] = resp[0]
                if len(resp) >= 1:
                    status = resp[1]
                if len(resp) >= 2:
                    kwargs['headers'] = resp[2]
                resp = Response.error(status, **kwargs)
            elif not isinstance(resp, Response):
                resp = Response.error(status, reason=reason, body=resp)

            headers = []
            for header in resp.headers:
                headers.append('%s: %s' % (header[0], header[1]))
            self.logger.debug(self.locale("Ответ ERROR {http_status}\n {headers}\n {body}",
                                          resp=resp,
                                          headers="\n".join(headers),
                                          http_status=resp.http_status,
                                          body=resp.body,
                                          ))
        return self.__transport.make_response(resp)


import os
import subprocess

from aws_cdk import(
    Stack,
    aws_lambda as _lambda
    )

from constructs import Construct

def subprocess_cmd(command):
    process=subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print(proc_stdout.decode("utf-8"))

class UrlLayersStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        layer_directory = "./bit_layer"
        if not os.path.isdir(layer_directory):
            subprocess_cmd("Echo 'Building python layers'; ./build_bit_layer_requirements.sh")

        bit_layer = _lambda.LayerVersion(
            self,
            "BitLayer",
            code=_lambda.Code.from_asset(f'{layer_directory}'),
            compatible_runtimes=[_lambda.Runtime.PYTHON_3_9],
            description="Bit Layer for dependencies"
        )


from django.conf.urls import url
from django.urls import path
from . import views

from archeutils import views as arche_views

app_name = 'shps'

urlpatterns = [
    url(
        r'^ids$',
        arche_views.get_ids,
        name='get_ids'
    ),
    url(
        r'^arche$',
        arche_views.project_as_arche_graph,
        name='project_as_arche'
    ),
    url(
        r'^arche-title-img$',
        arche_views.get_title_img,
        name='arche_title_img'
    ),
    url(r'^where-was/$', views.WhereWas.as_view(), name='where_was'),
    url(r'^shapes/$', views.TempSpatialListView.as_view(), name='browse_shapes'),
    url(
        r'^shape/arche/(?P<pk>[0-9]+)$',
        arche_views.res_as_arche_graph,
        name='arche_md'
    ),
    path('permalink/<unique>/', views.PermaLinkView.as_view(), name='permalink-view'),
    url(r'^shape/detail/(?P<pk>[0-9]+)$', views.TempSpatialDetailView.as_view(),
        name='shape_detail'),
    url(r'^shape/delete/(?P<pk>[0-9]+)$', views.TempSpatialDelete.as_view(),
        name='shape_delete'),
    url(r'^shape/edit/(?P<pk>[0-9]+)$', views.TempSpatialUpdate.as_view(),
        name='shape_edit'),
    url(r'^shape/create/$', views.TempSpatialCreate.as_view(),
        name='shape_create'),
    url(r'^sources/$', views.SourceListView.as_view(), name='browse_sources'),
    url(r'^source/detail/(?P<pk>[0-9]+)$', views.SourceDetailView.as_view(),
        name='source_detail'),
    url(r'^source/delete/(?P<pk>[0-9]+)$', views.SourceDelete.as_view(),
        name='source_delete'),
    url(r'^source/edit/(?P<pk>[0-9]+)$', views.SourceUpdate.as_view(),
        name='source_edit'),
    url(r'^source/create/$', views.SourceCreate.as_view(),
        name='source_create')
]


import asyncio
from django.shortcuts import render

from django.http import HttpResponse
from opentelemetry import trace

tracer = trace.get_tracer(__name__)

async def index(request):
    with tracer.start_as_current_span("get users"):
        await asyncio.sleep(2)

    with tracer.start_as_current_span("create index result"):
        await asyncio.sleep(0.2)

    return HttpResponse("Done")

async def list(request):
    with tracer.start_as_current_span("getting user list "):
        await asyncio.sleep(0.2)

    with tracer.start_as_current_span("create list result"):
        await asyncio.sleep(0.2)

    return HttpResponse("Done")


from email.mime import audio
import os
from pdb import Restart
from pydoc import text
from re import M
import time
from tkinter.messagebox import YES
from turtle import reset
import playsound
import speech_recognition as sr
from gtts import gTTS
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *

def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice1.mp3"
    tts.save(filename)
    playsound.playsound(filename)

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said
def write_slogan():
    text = get_audio()
    dir = "C:/Program Files/Mozilla Firefox/firefox.exe"
    photoshop = "C:/Program Files/Adobe/Adobe Photoshop 2021/photoshop.exe"
    chrome = "C:/Program Files/Google/Chrome/Application/chrome.exe"
    vscode = "C:/Users/Milad/AppData/Local/Programs/Microsoft VS Code/Code.exe"
    word = "C:/Program Files/Microsoft Office/root/Office16/winword.exe"
    exel = "C:/Program Files/Microsoft Office/root/Office16/EXCEL.EXE"
    access = "C:/Program Files/Microsoft Office/root/Office16/MSACCESS.EXE"
    powerpoint = "C:/Program Files/Microsoft Office/root/Office16/POWERPNT.EXE"

    # for hey in text:
    #     speak("Yes, I am in service")
    #     break
    if "hello" in text:
        speak("hello Iam tkinter, how are you ?")
        write_slogan()
    elif "how are you" in text:
        speak("Thanks, i am great")
        write_slogan()
    elif "thanks" in text :
        speak("it is Good")
    elif "iam find, Whats up ?" in text:
        speak("Thanks everything is great")
    elif "what's your name" in text:
        speak("My name is bam")
    elif "please open the Firefox" in text:
        speak("ok, please wait")
        os.startfile(dir)
    elif "please open the Photo Shop" in text:
        speak("ok, please wait")
        os.startfile(photoshop)
    elif "please open the Chrome" in text:
        speak("ok, please wait")
        os.startfile(chrome)
    elif "please open the vs code" in text:
        speak("ok, please wait")
        os.startfile(vscode)
    elif "please open the word" in text:
        speak("ok, please wait")
        os.startfile(word)
    elif "please open the Excel" in text:
        speak("ok, please wait")
        os.startfile(exel)
    elif "please open the access" in text:
        speak("ok, please wait")
        os.startfile(access)
    elif "please open the PowerPoint" in text:
        speak("ok, please wait")
        os.startfile(powerpoint)
    else:
        speak("Invalid input")
        print("Invalid input")

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
root.geometry("200x300")
style = Style()

style.configure('TButton', font =
			('calibri', 20, 'bold'),
					borderwidth = '4')
style.map('TButton', foreground = [('active', '!disabled', 'green')],
					background = [('active', 'black')])
btn1 = Button(frame,
                   text="Send Voice",
                   command=write_slogan)
btn1.grid(row = 0, column = 3, padx = 0)
root.mainloop()


def puzzle_three_one():
    sum = 0

    with open("input-3.txt") as file:
        for line in file:
            line = line.strip()

            split = int(len(line) / 2)
            firstpart = line[0:split]
            secondpart = line[split:len(line)]
            common = []
            for char in firstpart:
                if char in secondpart:
                    common.append(char)
            for letter in set(common):
                if letter.isupper():
                    priority = ord(letter) - ord('A') + 27
                else:
                    priority = ord(letter) - ord('a') + 1
                sum += priority

    print(sum)

def find_common(line):
    split = int(len(line) / 2)

    firstpart = line[0:split]
    secondpart = line[split:len(line)]
    common = []
    for char in firstpart:
        if char in secondpart:
            common.append(char)

    return set(common)

def priority(char):
    if char.isupper():
        priority = ord(char) - ord('A') + 27
    else:
        priority = ord(char) - ord('a') + 1
    return priority

def puzzle_three_two():
    sum = 0

    with open("input-3.txt") as file:
        alllines = file.readlines()

        for i in range(0, len(alllines) - 2, 3):
            firstchar = set(alllines[i].strip())
            secondchar = set(alllines[i + 1].strip())
            thirdchar = set(alllines[i + 2].strip())
            common = firstchar.intersection(secondchar).intersection(thirdchar)
            for char in common:
                sum += priority(char)
    print(sum)

if __name__ == '__main__':
    puzzle_three_one()
    puzzle_three_two()


import codecs
import json
import logging
import re
from typing import Optional

import requests
from bs4 import BeautifulSoup

from argostime.exceptions import CrawlerException
from argostime.exceptions import PageNotFoundException

from argostime.crawler.crawl_utils import CrawlResult, register_crawler

@register_crawler("HEMA", "hema.nl")
def crawl_hema(url: str) -> CrawlResult:
    """Crawler for hema.nl"""

    response: requests.Response = requests.get(url, timeout=10)

    if response.status_code != 200:
        logging.error("Got status code %d while getting url %s", response.status_code, url)
        raise PageNotFoundException(url)

    soup = BeautifulSoup(response.text, "html.parser")

    result: CrawlResult = CrawlResult(url=url)

    matches = soup.find_all("script")
    pattern = re.compile(r"var gtmDataObj = JSON\.parse\(.+\);")

    for match in matches:
        re_result: Optional[re.Match] = pattern.search(match.text)

        if re_result is not None:
            raw_json = re_result.group(0)
            logging.debug("Found raw product json %s", raw_json)
            break

    raw_json = raw_json.replace("var gtmDataObj = JSON.parse('", "")
    raw_json = raw_json.replace("');", "")

    raw_json = codecs.decode(raw_json, "unicode-escape")

    try:
        product_dict = json.loads(raw_json)
    except json.decoder.JSONDecodeError as exception:
        logging.error("Could not decode JSON %s, raising CrawlerException", raw_json)
        raise CrawlerException from exception

    logging.debug(product_dict)

    try:
        result.product_name = product_dict["ecommerce"]["detail"]["products"][0]["name"]
    except KeyError as exception:
        logging.error("Could not find product name in %s via %s", raw_json, url)
        raise CrawlerException from exception

    try:
        result.product_code = product_dict["ecommerce"]["detail"]["products"][0]["id"]
    except KeyError as exception:
        logging.error("Could not find product code in %s via %s", raw_json, url)
        raise CrawlerException from exception

    try:
        result.normal_price = float(product_dict["ecommerce"]["detail"]["products"][0]["price"])
    except KeyError as exception:
        logging.error("Could not find a valid price in %s via %s", raw_json, url)
        result.normal_price = -1

    return result


def spiralOrder(matrix):
    if matrix==[]:
        return []
    #どこをすでに通ったかの記録
    R,C=len(matrix),len(matrix[0])
    seen=[[0]*C for _ in matrix]
    dr=[0,1,0,-1] #iの回る方向
    dc=[1,0,-1,0] #jの回る方向

    ans=[]
    r=c=di=0
    for i in range(R*C):
        ans.append(matrix[r][c])
        seen[r][c]=1
        next_r=r+dr[di]
        next_c=c+dc[di]
        if 0<=next_r<R and 0<=next_c<C and seen[next_r][next_c]==0:
            r=next_r; c=next_c
        else:
            di=(di+1)%4
            r=r+dr[di]
            c=c+dc[di]
    return ans

ans=spiralOrder([[1,2,3],
            [4,5,6],
            [7,8,9]])
print(ans)


from datetime import timedelta

from celery.task.control import inspect, revoke
from django.contrib.auth import get_user_model
from django.db import models

from reservations.tasks import check_basket, execute_reservation_job

User = get_user_model()

class TimestampedModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class ReservationJob(TimestampedModel):
    PENDING = "PENDING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"
    CANCELLED = "CANCELLED"
    APPROVED = "APPROVED"

    STATUS_CHOICES = (
        (PENDING, "PENDING"),
        (COMPLETED, "COMPLETED"),
        (FAILED, "FAILED"),
        (CANCELLED, "CANCELLED"),
        (APPROVED, "APPROVED"),
    )

    ETA = "ETA"
    IMMEDIATE = "IMMEDIATE"

    EXECUTION_TYPE_CHOICES = ((ETA, "ETA"), (IMMEDIATE, "IMMEDIATE"))

    execution_time = models.DateTimeField()
    execution_type = models.CharField(
        max_length=100,
        null=True,
        blank=True,
        choices=EXECUTION_TYPE_CHOICES,
        default=ETA,
    )
    status = models.CharField(
        max_length=255, null=True, blank=True, choices=STATUS_CHOICES, default=PENDING
    )
    selection = models.ForeignKey("selections.Selection", on_delete=models.CASCADE)
    user = models.ForeignKey(
        User,
        related_name="reservation_jobs",
        on_delete=models.CASCADE,
        null=True,
        blank=True,
    )

    def __str__(self):
        return f"<ReservationJob> {self.execution_time.strftime('%Y-%m-%d %H:%M')} {self.status}"

    class Meta:
        ordering = ("selection__slot__date_time",)

    def revoke(self):
        i = inspect()
        scheduled_tasks = i.scheduled()
        for _, tasks in scheduled_tasks.items():
            rj_task = [task for task in tasks if task["request"]["args"][0] == self.id]
            if rj_task:
                revoke(rj_task[0]["request"]["id"])

    def execute(self):
        if self.status == self.CANCELLED:
            return

        if self.execution_type == self.IMMEDIATE:
            execute_reservation_job.delay(self.id)
        else:
            execute_reservation_job.apply_async((self.id,), eta=self.execution_time)

    def save(
        self, force_insert=False, force_update=False, using=None, update_fields=None
    ):
        if self.status == self.CANCELLED:
            self.revoke()

        super().save(force_insert, force_update, using, update_fields)
        self.execute()

class Reservation(TimestampedModel):
    PENDING = "PENDING"
    IN_CART = "IN_CART"
    FAILED = "FAILED"
    REMOVED_FROM_BASKET = "REMOVED_FROM_BASKET"
    PAID = "PAID"
    EXPIRED = "EXPIRED"

    STATUS_CHOICES = (
        (IN_CART, "IN_CART"),
        (FAILED, "FAILED"),
        (PENDING, "PENDING"),
        (REMOVED_FROM_BASKET, "REMOVED_FROM_BASKET"),
        (PAID, "PAID"),
        (EXPIRED, "EXPIRED"),
    )

    user = models.ForeignKey(
        User, related_name="reservations", on_delete=models.CASCADE
    )
    selection = models.ForeignKey("selections.Selection", on_delete=models.CASCADE)
    status = models.CharField(
        max_length=255, null=True, blank=True, choices=STATUS_CHOICES, default=PENDING
    )

    def __str__(self):
        return f"<Reservation> {self.user} {self.status} {self.selection}"

    class Meta:
        ordering = ("selection__slot__date_time",)

    @property
    def is_success(self):
        return self.status == Reservation.IN_CART

    def save(
        self, force_insert=False, force_update=False, using=None, update_fields=None
    ):
        from reservations.helpers import send_reservation_email

        super().save(force_insert, force_update, using, update_fields)

        if self.status in (Reservation.IN_CART, Reservation.FAILED):
            send_reservation_email(self)

            status = (
                ReservationJob.COMPLETED if self.is_success else ReservationJob.FAILED
            )
            ReservationJob.objects.filter(
                user=self.user, selection=self.selection
            ).update(status=status)

        if self.status == Reservation.IN_CART:
            check_basket.apply_async(
                (self.id,), eta=self.created_at + timedelta(hours=12)
            )


from rasa.nlu.config import RasaNLUModelConfig
from rasa.shared.nlu.constants import TEXT
from rasa.shared.nlu.training_data.message import Message

def test_spacy_ner_extractor(component_builder, spacy_nlp):
    _config = RasaNLUModelConfig({"pipeline": [{"name": "SpacyEntityExtractor"}]})
    ext = component_builder.create_component(_config.for_component(0), _config)
    example = Message(
        data={
            TEXT: "anywhere in the U.K.",
            "intent": "restaurant_search",
            "entities": [],
            "text_spacy_doc": spacy_nlp("anywhere in the west"),
        }
    )

    ext.process(example, spacy_nlp=spacy_nlp)

    assert len(example.get("entities", [])) == 1
    assert example.get("entities")[0] == {
        "start": 16,
        "extractor": "SpacyEntityExtractor",
        "end": 20,
        "value": "U.K.",
        "entity": "GPE",
        "confidence": None,
    }

    # Test dimension filtering includes only specified dimensions

    example = Message(
        data={
            TEXT: "anywhere in the West with Sebastian Thrun",
            "intent": "example_intent",
            "entities": [],
            "text_spacy_doc": spacy_nlp("anywhere in the West with Sebastian Thrun"),
        }
    )
    _config = RasaNLUModelConfig({"pipeline": [{"name": "SpacyEntityExtractor"}]})

    _config.set_component_attr(0, dimensions=["PERSON"])
    ext = component_builder.create_component(_config.for_component(0), _config)
    ext.process(example, spacy_nlp=spacy_nlp)

    assert len(example.get("entities", [])) == 1
    assert example.get("entities")[0] == {
        "start": 26,
        "extractor": "SpacyEntityExtractor",
        "end": 41,
        "value": "Sebastian Thrun",
        "entity": "PERSON",
        "confidence": None,
    }


import numpy as np
import pandas as pd
# from dev.dataloading.dataset import Dataset
# from dev.dataloading.domain import Domain

from utils import Dataset, Domain

CATEGORICAL = "categorical"
CONTINUOUS = "continuous"
ORDINAL = "ordinal"

class Transformer:
    """Continuous and ordinal columns are normalized to [0, 1].
    Discrete columns are converted to a one-hot vector.
    """

    def __init__(
        self, categorical_columns, continuous_columns, bin_size=None, normalize=False
    ):
        # self.target_feat_idx = target_feat_idx
        self.meta = None
        self.output_dim = None
        self.bin_size = bin_size
        self.categorical_columns = categorical_columns
        self.continuous_columns = continuous_columns
        self.normalize = normalize

    @staticmethod
    def get_metadata(
        data,
        categorical_columns=tuple(),
        continuous_columns=tuple(),
        ordinal_columns=tuple(),
        normalize=False,
    ):
        meta = []

        df = pd.DataFrame(data)
        for index in df:
            column = df[index]

            if index in categorical_columns:
                mapper = column.value_counts().index.tolist()
                meta.append(
                    {
                        "name": index,
                        "type": CATEGORICAL,
                        "size": len(mapper),
                        "i2s": mapper,
                    }
                )
            elif index in ordinal_columns:
                value_count = list(dict(column.value_counts()).items())
                value_count = sorted(value_count, key=lambda x: -x[1])
                mapper = list(map(lambda x: x[0], value_count))
                meta.append(
                    {"name": index, "type": ORDINAL, "size": len(mapper), "i2s": mapper}
                )
            elif index in continuous_columns:
                meta.append(
                    {
                        "name": index,
                        "type": CONTINUOUS,
                        "min": column.min() if normalize else 0,
                        "max": column.max() if normalize else 1,
                    }
                )

        return meta

    def fit(self, data):
        self.meta = self.get_metadata(
            data,
            self.categorical_columns,
            self.continuous_columns,
            normalize=self.normalize,
        )
        self.output_dim = 0
        for info in self.meta:
            # if info['name'] != self.target_feat_idx:
            if info["type"] in [CONTINUOUS, ORDINAL]:
                self.output_dim += 1
            else:
                self.output_dim += info["size"]

    # def transform(self, data: pd.DataFrame, target: list) -> Dataset:
    def transform(self, data: pd.DataFrame) -> Dataset:

        data_t = []
        attrs = []
        shape = []
        original_shape = []
        for id_, info in enumerate(self.meta):
            col_name = info["name"]
            col = data[col_name].to_numpy()
            attrs.append(col_name)
            if info["type"] == CONTINUOUS:
                if self.bin_size is not None:
                    col_binned = np.array(
                        pd.cut(
                            col,
                            bins=self.bin_size,
                            labels=np.arange(self.bin_size),
                            retbins=False,
                        )
                    )
                    col_binned = col_binned.reshape([-1, 1])
                    data_t.append(col_binned)
                    shape.append(self.bin_size)
                else:
                    # print(f"continuous column {id_}: {info['max']}, {info['min']}. col.max={col.max()}, col.min={col.min()}")
                    col_normed = (col - info["min"]) / (
                        info["max"] - info["min"] + 1e-9
                    )
                    data_t.append(col_normed.reshape([-1, 1]))
                    shape.append(1)
                original_shape.append(1)
            else:
                mapper = dict([(item, id) for id, item in enumerate(info["i2s"])])
                mapped = data[col_name].apply(lambda x: mapper[x]).values.astype(int)
                data_t.append(mapped.reshape([-1, 1]).astype(int))
                shape.append(info["size"])
                original_shape.append(info["size"])

        # domain = Domain(attrs, shape, target)
        domain = Domain(attrs, shape)
        df = pd.DataFrame(np.concatenate(data_t, axis=1), columns=domain.attrs)
        for id_, info in enumerate(self.meta):
            col_name = info["name"]
            if info["type"] == CATEGORICAL:
                df[col_name] = df[col_name].astype(int)

        # self.original_domain = Domain(attrs, original_shape, target)
        self.original_domain = Domain(attrs, original_shape)
        return Dataset(df, domain)

    def inverse_transform(self, data: Dataset) -> pd.DataFrame:
        """Takes as input a Dataset and return a DataFrame"""
        # data_t = np.zeros([len(data), len(self.meta)])
        data_t = pd.DataFrame()

        data = data.df.copy()
        res = []
        column_names = []
        for id_, info in enumerate(self.meta):
            col_name = info["name"]
            column_names.append(col_name)
            if info["type"] == CONTINUOUS:
                current = data.iloc[:, 0]
                data = data.iloc[:, 1:]
                min_val = info["min"]
                max_val = info["max"]
                if self.bin_size is not None:
                    # current = 0.5 * (2*current+1) / self.bin_size
                    lower = current.values / self.bin_size
                    upper = (current.values + 1) / self.bin_size
                    current = np.random.uniform(lower, upper)
                    current = current * (max_val - min_val) + min_val
                    res.append(current)
                else:
                    current = (current * (max_val - min_val) + min_val).values
                    res.append(current)
                # data_t[col_name] = current
                # print(data_t[col_name].isna().sum())
            else:
                current = data.iloc[:, 0].astype(int)
                data = data.iloc[:, 1:]
                col_data = np.array(list(map(info["i2s"].__getitem__, current)))
                res.append(col_data)
                # data_t[col_name] = list(map(info['i2s'].__getitem__, current))

        data_t = pd.DataFrame(np.column_stack(res), columns=column_names)
        for id_, info in enumerate(self.meta):
            col_name = info["name"]
            if info["type"] == CATEGORICAL:
                data_t[col_name] = data_t[col_name].astype(int)

        return data_t

if __name__ == "__main__":
    cat = ("animal",)
    con = ("age",)
    trans = Transformer(cat, con, bin_size=None)

    data = pd.DataFrame(
        [
            ["cat", 19],
            ["cat", 11],
            ["dog", 15.1],
            ["dog", 14.5],
            ["dog", 13.1],
            ["dog", 11.2],
            ["dog", 15.6],
            ["whale", 17],
            ["whale", 23],
        ],
        columns=cat + con,
    )
    trans.fit(
        data,
    )


import requests
import time
import sys

print('''
    #############################
    #   *         *         *   #
    #   *   ¡Paginas web    *   #
    #   *         *         *   #
    #############################
''')

url = input("pagina a refrescar: ")
# ejemplo 60 = 1min, 120 = 2min etc...
refrescar = int(input("cada cuanto quiere que se actualize (segundos): "))

while True:
    responde = requests.get(url)
    if responde.status_code == 200:
        print(f"Contenido actualizado:\n{responde.text}")
        print(f"pagina actualizada se refrescara en {refrescar} segundos")
    else:
        print(f"No se pudo obtener el contenido (Código de estado: {responde.status_code})")
        print("no se actulizo la pagina")
        res = input("salir S o N").lower()
        if res == "s":
            sys.exit()
        elif res == "n":
            print(f"la pagina se refrescara en {refrescar}")

    time.sleep(refrescar)


from flask import Flask, render_template, request, redirect
import os
from flask_sqlalchemy import SQLAlchemy

pathdir = os.path.dirname(os.path.abspath(__file__))
db_uri = 'sqlite:///' + os.path.join(pathdir, 'DB.sqlite')

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri

db = SQLAlchemy(app)

class Teilnehmer(db.Model):

    member = db.Column(db.String(80), primary_key=True, unique=True)

@app.route('/', methods=['POST', 'GET'])
def home():
    if request.form:

        new_Teilnehmer = Teilnehmer(member=request.form.get('member'))
        db.session.add(new_Teilnehmer)
        db.session.commit()

    x = Teilnehmer.query.all()

    return(render_template('home.html', member_list=x))

@app.route('/update', methods=['POST'])
def update():
    newtitle = request.form.get('newtitle')
    oldtitle = request.form.get('oldtitle')
    kandidat = Teilnehmer.query.filter_by(member=oldtitle).first()
    kandidat.member = newtitle
    db.session.commit()
    return(redirect('/'))

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


import pandas as pd
import numpy as np
import math
import os
from Sentiment import get_sentiment_value, get_polarity_value, get_subjectivity_value
from Keyword import extract_v2, filter_text_by_pos, lemetize_text
from Utils import cleantext
import warnings
from nltk import word_tokenize
from gensim import corpora, models, similarities

warnings.simplefilter(action='ignore')

class Indicator:
    def __init__(self, data_folder):
        self.data_folder = data_folder

    @staticmethod
    def average(values_tab):
        return sum(values_tab) / len(values_tab)

    @staticmethod
    def get_mean_time_gap(tweets):
        len_t = len(tweets)
        if len_t <= 1:
            return 0

        before = tweets[0]
        cumul = 0
        for i in range(1, len_t):
            current = tweets[i]
            cumul += current - before
            before = current

        return cumul / len_t

    @staticmethod
    def nb_different_topics(texts):
        textValues = texts.values
        texts = texts.apply(cleantext)
        keywords_column = texts.apply(lambda x: extract_v2(x, withNumbers=False))
        values = keywords_column.values
        list_sets = []

        topics = []
        i = 0
        for keywords in values:
            to_remove = []
            new_kws = set(" ".join(keywords).split())

            j = 0
            for one_set in list_sets:
                inter = one_set.intersection(new_kws)
                if len(inter) > 0:
                    to_remove.append(j)
                j += 1

            if len(to_remove) > 0:
                new_set = set().union(new_kws)
                new_topic = []
                for j in to_remove:
                    one_set = list_sets[j]
                    new_set = new_set.union(one_set)
                    for topic in topics[j]:
                        new_topic.append(topic)

                new_topics = []
                new_list_sets = []
                for j in range(0, len(list_sets)):
                    if j not in to_remove:
                        new_topics.append(topics[j])
                        new_list_sets.append(list_sets[j])

                topics = new_topics
                list_sets = new_list_sets

                list_sets.append(new_set)
                topics.append(new_topic)

            elif len(new_kws) > 0:
                list_sets.append(new_kws)
                topics.append([textValues[i]])

            i += 1

        #file = open(name, 'w')
        #for topic in topics:
        #    for text in topic:
        #        file.write(text+"\n")
        #    file.write('--------------------\n')

        #file.close()
        return len(list_sets)

    @staticmethod
    def nb_different_topics_v2(name, texts):
        textValues = texts.values
        texts = texts.apply(cleantext)
        lemmetized_column = texts.apply(lambda x: lemetize_text(x))
        values = lemmetized_column.values
        list_sets = []

        topics = []

        tab_texts = [word_tokenize(text) for text in values]
        dictionary = corpora.Dictionary(tab_texts)
        feature_cnt = len(dictionary.token2id)
        corpus = [dictionary.doc2bow(text) for text in tab_texts]
        tfidf = models.TfidfModel(corpus)

        for keyword in values:
            kw_vector = dictionary.doc2bow(word_tokenize(keyword))
            index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=feature_cnt)
            sim = index[tfidf[kw_vector]]
            for i in range(len(sim)):
                print('keyword is similar to text%d: %.2f' % (i + 1, sim[i]))

        return 0

    @staticmethod
    def get_indicators_user(user_file):
        df_csv = pd.read_csv(user_file)

        tweet_list = df_csv[["text"]]
        tweet_list['text'] = tweet_list['text'].apply(str)

        tweet_list['sentiment'] = tweet_list['text'].apply(get_sentiment_value)
        tweet_list['polarity'] = tweet_list['text'].apply(get_polarity_value)
        tweet_list['subjectivity'] = tweet_list['text'].apply(get_subjectivity_value)

        average_sentiment = tweet_list['sentiment'].mean()
        average_polarity = tweet_list['polarity'].mean()
        average_subjectivity = tweet_list['subjectivity'].mean()
        average_post_time_gap = Indicator.get_mean_time_gap(df_csv['created_at'].values) / 1000 / 60
        if math.isnan(average_post_time_gap) is True:
            average_post_time_gap = 0

        nb_topics = Indicator.nb_different_topics(tweet_list['text'])
        print(user_file)

        return average_sentiment, average_polarity, average_subjectivity, average_post_time_gap, nb_topics, tweet_list['text'].size

    def get_all_indicator_users(self):
        average_sentiment = []
        average_polarity = []
        average_subjectivity = []
        average_time_gap = []
        average_nb_topics = []

        out = pd.DataFrame(columns=['sentiment', 'polarity', 'subjectivity', 'timegap', 'nb_topics', 'nb_tweets'])

        cpt = 1
        for element in os.listdir(self.data_folder):
            if not os.path.isdir(element):
                sentiment, polarity, subjectivity, time_gap, nb_topics, nb_tweets = self.get_indicators_user(os.path.join(self.data_folder, element))
                row = pd.DataFrame(np.array([[sentiment, polarity, subjectivity, time_gap, nb_topics, nb_tweets]]),
                                   columns=['sentiment', 'polarity', 'subjectivity', 'timegap', 'nb_topics', 'nb_tweets'])
                out = pd.concat([out, row], ignore_index=True)

                average_sentiment.append(sentiment)
                average_polarity.append(polarity)
                average_subjectivity.append(subjectivity)
                average_time_gap.append(time_gap)
                average_nb_topics.append(nb_topics)

                cpt += 1

        out.to_csv(self.data_folder+".csv", index=False)

        average_sentiment = self.average(average_sentiment)
        average_polarity = self.average(average_polarity)
        average_subjectivity = self.average(average_subjectivity)

        average_time_gap = np.array(average_time_gap)
        average_time_gap_no_zero = average_time_gap[average_time_gap != 0.0]
        average_time_gap = self.average(average_time_gap_no_zero)

        average_nb_topics = self.average(average_nb_topics)

        return average_sentiment, average_polarity, average_subjectivity, average_time_gap, average_nb_topics

if __name__ == '__main__':
    indi = Indicator('./example/troll-user-tweets')
    print(indi.get_all_indicator_users())
    indi = Indicator('./example/nontroll-user-tweets')
    print(indi.get_all_indicator_users())


import numpy as np

class KKT():
    def __init__(self, c, A, b, G, h, p, m, n, fengge_bar):
        self.c = c.copy()
        self.A = A.copy()
        self.b = b.copy()
        self.G = G.copy()
        self.h = h.copy()

        self.cbh = np.vstack([-1 * self.c, self.b, self.h])
        self.fengge_bar = fengge_bar.copy()

        self.p = p
        self.m = m
        self.n = n

        self.c_shape = c.shape
        self.A_shape = A.shape
        self.b_shape = b.shape
        self.G_shape = G.shape
        self.h_shape = h.shape

        self.zero_nn = np.zeros([self.n, self.n])
        self.zero_pp = np.zeros([self.p, self.p])
        self.zero_mm = np.zeros([self.m, self.m])
        self.zero_11 = np.zeros([1, 1])
        self.zero_mp = np.zeros([self.m, self.p])
        self.zero_n1 = np.zeros([self.n, 1])
        self.zero_p1 = np.zeros([self.p, 1])
        self.zero_m1 = np.zeros([self.m, 1])
        self.zero_1m = self.zero_m1.transpose()
        self.zero_npluspmplus1 = np.zeros([self.n + self.p, self.m + 1])

        self.one_mm = np.eye(self.m)
        self.one_11 = np.eye(1)

        self.Residual_Matrix = self.residual_matrix(c, A, b, G, h)
        self.Direction_Matrix = self.direction_matrix(c, A, b, G, h)
        self.delta_x1, self.delta_y1, self.delta_z1 = None, None, None

        self.equ_fenge = [n, p, m, 1, m, 1]
        self.fenge = np.add.accumulate(self.equ_fenge)

    def residual_matrix(self, c, A, b, G, h):
        hang_0 = np.concatenate((self.zero_nn, self.A.transpose(), self.G.transpose(), self.c), axis=1)
        hang_1 = np.concatenate((-1 * self.A, self.zero_pp, self.zero_mp.transpose(), b), axis=1)
        hang_2 = np.concatenate((-1 * self.G, self.zero_mp, self.zero_mm, self.h), axis=1)
        hang_3 = np.concatenate(
            (-1 * self.c.transpose(), -1 * self.b.transpose(), -1 * self.h.transpose(), self.zero_11), axis=1)
        return np.concatenate((hang_0, hang_1, hang_2, hang_3), axis=0)

    def direction_matrix(self, c, A, b, G, h):
        hang_0 = np.concatenate((self.zero_nn, self.A.transpose(), self.G.transpose()), axis=1)
        hang_1 = np.concatenate((self.A, self.zero_pp, self.zero_mp.transpose()), axis=1)
        hang_2 = np.concatenate((self.G, self.zero_mp, self.zero_mm), axis=1)
        return np.concatenate((hang_0, hang_1, hang_2), axis=0)

    def solve(self, direction_type, W, W_2, bx, by, bz, lambda_bs, b_tau, b_kappa, kappa, tau):
        minus_w2 = -1 * W_2
        b_xyz = np.vstack([bx, by, bz])
        # print(b_xyz)
        if direction_type == 0:
            self.Direction_Matrix[self.fenge[1]:self.fenge[2], self.fenge[1]:self.fenge[2]] = minus_w2
            self.delta_x1, self.delta_y1, self.delta_z1 = np.vsplit(np.linalg.solve(self.Direction_Matrix, self.cbh),
                                                                    self.fenge[0:2])
            self.delta_z1 = self.sub_two(self.delta_z1)

        delta_x2, delta_y2, delta_z2 = np.vsplit(np.linalg.solve(self.Direction_Matrix, b_xyz),
                                                 self.fenge[0:2])
        delta_z2 = self.sub_two(delta_z2)
        h = self.sub_two(self.h)

        delta_tau = (b_tau - b_kappa / tau + np.dot(self.c.transpose(), delta_x2)[0] + np.dot(self.b.transpose(),
                                                                                delta_y2)[0] + np.dot(
            h.transpose(), delta_z2)) / (kappa / tau - np.dot(self.c.transpose(), self.delta_x1)[0] -
                                         np.dot(self.b.transpose(), self.delta_y1)[0] -
                                         np.dot(h.transpose(), self.delta_z1)[0])
        delta_x = delta_x2 + delta_tau * self.delta_x1
        delta_y = delta_y2 + delta_tau * self.delta_y1
        delta_z = delta_z2 + delta_tau * self.delta_z1
        delta_s = -1 * np.dot(W, lambda_bs + np.dot(W, delta_z))
        delta_kappa = -(b_kappa + kappa * delta_tau) / tau
        return delta_x, delta_y, delta_z, delta_tau, delta_s, delta_kappa

    def residual_update(self, x, y, s, z, k, t):
        sk = np.concatenate([self.zero_n1, self.zero_p1, s, [[k]]])
        xyzt = np.concatenate([x, y, z, [[t]]])

        residual_vec = sk - np.dot(self.Residual_Matrix, xyzt)
        return np.vsplit(residual_vec, self.fenge[0:3])

    def sub_two(self, s):
        return np.vstack(i[:3 * (i.shape[0] <= 1) - 2] for i in np.vsplit(s, self.fengge_bar))


from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),
    (r'^auth/', include('safebook.tlssrp.urls')),
    (r'', include('safebook.profiles.urls')),
)


import pytest
import logging
from azure.iot.device.iothub.models import MethodRequest, MethodResponse

logging.basicConfig(level=logging.DEBUG)

dummy_rid = 1
dummy_name = "name"
dummy_payload = {"MethodPayload": "somepayload"}
dummy_status = 200

@pytest.mark.describe("MethodRequest - Instantiation")
class TestMethodRequest(object):
    @pytest.mark.it("Instantiates with a read-only 'request_id' attribute")
    def test_request_id_property_is_read_only(self):
        m_req = MethodRequest(request_id=dummy_rid, name=dummy_name, payload=dummy_payload)
        new_rid = 2

        with pytest.raises(AttributeError):
            m_req.request_id = new_rid
        assert m_req.request_id != new_rid
        assert m_req.request_id == dummy_rid

    @pytest.mark.it("Instantiates with a read-only 'name' attribute")
    def test_name_property_is_read_only(self):
        m_req = MethodRequest(request_id=dummy_rid, name=dummy_name, payload=dummy_payload)
        new_name = "new_name"

        with pytest.raises(AttributeError):
            m_req.name = new_name
        assert m_req.name != new_name
        assert m_req.name == dummy_name

    @pytest.mark.it("Instantiates with a read-only 'payload' attribute")
    def test_payload_property_is_read_only(self):
        m_req = MethodRequest(request_id=dummy_rid, name=dummy_name, payload=dummy_payload)
        new_payload = {"NewPayload": "somenewpayload"}

        with pytest.raises(AttributeError):
            m_req.payload = new_payload
        assert m_req.payload != new_payload
        assert m_req.payload == dummy_payload

@pytest.mark.describe("MethodResponse - Instantiation")
class TestMethodResponseInstantiation(object):
    @pytest.mark.it("Instantiates with an editable 'request_id' attribute")
    def test_instantiates_with_request_id(self):
        response = MethodResponse(request_id=dummy_rid, status=dummy_status, payload=dummy_payload)
        assert response.request_id == dummy_rid

        new_rid = "2"
        assert response.request_id != new_rid
        response.request_id = new_rid
        assert response.request_id == new_rid

    @pytest.mark.it("Instantiates with an editable 'status' attribute")
    def test_instantiates_with_status(self):
        response = MethodResponse(request_id=dummy_rid, status=dummy_status, payload=dummy_payload)
        assert response.status == dummy_status

        new_status = 400
        assert response.status != new_status
        response.status = new_status
        assert response.status == new_status

    @pytest.mark.it("Instantiates with an editable 'payload' attribute")
    def test_instantiates_with_payload(self):
        response = MethodResponse(request_id=dummy_rid, status=dummy_status, payload=dummy_payload)
        assert response.payload == dummy_payload

        new_payload = {"NewPayload": "yes_this_is_new"}
        assert response.payload != new_payload
        response.payload = new_payload
        assert response.payload == new_payload

    @pytest.mark.it("Instantiates with a default 'payload' of 'None' if not provided")
    def test_instantiates_without_payload(self):
        response = MethodResponse(request_id=dummy_rid, status=dummy_status)
        assert response.request_id == dummy_rid
        assert response.status == dummy_status
        assert response.payload is None

@pytest.mark.describe("MethodResponse - .create_from_method_request()")
class TestMethodResponseCreateFromMethodRequest(object):
    @pytest.mark.it("Instantiates using a MethodRequest to provide the 'request_id'")
    def test_instantiates_from_method_request(self):
        request = MethodRequest(request_id=dummy_rid, name=dummy_name, payload=dummy_payload)
        status = 200
        payload = {"ResponsePayload": "SomeResponse"}
        response = MethodResponse.create_from_method_request(
            method_request=request, status=status, payload=payload
        )

        assert isinstance(response, MethodResponse)
        assert response.request_id == request.request_id
        assert response.status == status
        assert response.payload == payload

    @pytest.mark.it("Instantiates with a default 'payload' of 'None' if not provided")
    def test_instantiates_without_payload(self):
        request = MethodRequest(request_id=dummy_rid, name=dummy_name, payload=dummy_payload)
        status = 200
        response = MethodResponse.create_from_method_request(request, status)

        assert isinstance(response, MethodResponse)
        assert response.request_id == request.request_id
        assert response.status == status
        assert response.payload is None


#!/usr/bin/env python3

# info
# -name   : zhangruochi
# -email  : zrc720@gmail.com

"""
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
"""

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """

        TABLE = {")": "(","]": "[","}":"{"}

        stack = []
        for _ in s:
            if _ in TABLE.values():
                stack.append(_)
            else:
                if not stack:
                    return False
                elif stack.pop() != TABLE[_]:
                    return False

        if not stack:
            return True

        return False

if __name__ == '__main__':
    solution = Solution()
    print(solution.isValid("()"))


from PIL import Image
import numpy as np

import matplotlib.pyplot as plt
from thresholding import threshold

i = Image.open('images/numbers/0.1.png')
iar = np.array(i)
i2 = Image.open('images/numbers/y0.4.png')
iar2 = np.array(i2)
i3 = Image.open('images/numbers/y0.5.png')
iar3 = np.array(i3)
i4 = Image.open('images/facebook.png')
iar4 = np.array(i4)

iar = threshold(iar)
iar2 = threshold(iar2)
iar3 = threshold(iar3)
iar4 = threshold(iar4)

fig = plt.figure()
ax1 = plt.subplot2grid((8,6),(0,0), rowspan=4, colspan=3)
ax2 = plt.subplot2grid((8,6),(4,0), rowspan=4, colspan=3)
ax3 = plt.subplot2grid((8,6),(0,3), rowspan=4, colspan=3)
ax4 = plt.subplot2grid((8,6),(4,3), rowspan=4, colspan=3)

ax1.imshow(iar)
ax2.imshow(iar2)
ax3.imshow(iar3)
ax4.imshow(iar4)

plt.show()

Labmaka Internal Training/P2: Image Recognition/testreshold.py


"""
Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
"""

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
from tools.linked_list import ListNode

class Solution:
    """
    Runtime: 36 ms, faster than 80.68% of Python3.
    Memory Usage: 13.1 MB, less than 5.04% of Python3.
    """

    def swapPairs(self, head: ListNode) -> ListNode:
        if head and head.next:
            temp = head.next
            head.next, temp.next = self.swapPairs(temp.next), head
            return temp
        else:
            return head

class Solution2:
    """
    LC recursion solution.
    Runtime: 24 ms, faster than 95.14% of Python3.
    Memory Usage: 14.1 MB, less than 99.98% of Python3.

    Time Complexity: O(N) where N is the size of the linked list.
    Space Complexity: O(N) stack space utilized for recursion.
    """
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        # nodes to be swapped
        first = head
        second = head.next

        # swapping
        first.next = self.swapPairs(second.next)
        second.next = first

        # now the head is the swapping node
        return second

class Solution3:
    """
    LC iterative solution.
    Runtime: 28 ms, faster than 83.37% of Python3.
    Memory Usage: 14 MB, less than 99.98% of Python3.

    Time Complexity: O(N) where N is the size of the linked list.
    Space Complexity: O(1).
    """
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        # dummy node acts as a prevNode for the head node
        # of the list and hence and hence stores pointer to the head node.
        dummy = ListNode(-1)
        dummy.next = head

        prev_node = dummy

        while head and head.next:
            # nodes to be swapped
            first = head
            second = head.next

            # swapping
            prev_node.next = second
            first.next = second.next
            second.next = first

            # reinit the head and prev_node for the next swap
            prev_node = first
            head = first.next

        # return new head node
        return dummy.next


from tkinter import W
import numpy as np
from scipy.spatial import distance

class KNN:
	def __init__(self, n):
		self.k = n

	def prepare(self, X, Y):
		# find all the distances of all the data nxn matrix of distances
		self.X = X
		self.Y = Y

		self.distances = np.zeros((Y.shape[0], Y.shape[0]))

		# calculate the distances for later
		for i in range(self.distances.shape[0]):
			for j in range(self.distances.shape[1]):
				if(i == j):
					self.distances[i][j] = 0
				else:
					self.distances[i][j] = self.distances[j][i] = distance.euclidean(X[i], X[j])

		return

	def find_label(self, i):
		# after fitting this can predict the label for a dp
		row = self.distances[i].tolist()
		labels = self.Y.tolist()

		knn_labels = []
		visited = []

		for i in range(self.k):
			min = float("inf")
			best_idx = -1
			for idx, distance in enumerate(row):
				if(idx not in visited and  distance < min and distance != 0):
					min = distance
					best_idx = idx

			visited.append(best_idx)
			knn_labels.append(labels[best_idx])

		counts = {}
		for l in knn_labels:
			if(str(l) in counts):
				counts[str(l)] += 1
			else:
				counts[str(l)] = 1

		label = ""
		count = 0
		for l in counts:
			if(counts[l] > count):
				label = l

		return label

	def predict(self, dp):
		return

		return counts


'''
Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

concept:

test cases:

recursive vs iterative:

'''
import math
import itertools
class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        return list(itertools.permutations(nums))

''' without itertools, recursive, loops'''
def dfs(nums, array, answer):
    if len(array) == len(nums):
        #print(array)
        answer.append(array)
        if len(answer) == 2:
            print(answer)
            return answer
    else:
        for i in range(0,len(nums)):
            if nums[i] in array:
                continue
            array.append(nums[i])
            dfs(nums, array, answer)
            array.pop()

a = []
b = dfs([1,2],[],a)
print(a)
print(b)