import decimal
from decimal import Decimal

from django.conf import settings
from shop.models import Product

class Cart(object):
    """Класс для работы с корзиной."""

    def __init__(self, request) -> None:
        """Инициализация объекта корзины."""
        self.session = request.session
        cart = self.session.get(settings.CART_SESSION_ID)
        if not cart:
            cart = self.session[settings.CART_SESSION_ID] = {}
        self.cart = cart

    def add(self, product, quantity=1, override_quantity=False) -> None:
        """Добавление товара в корзину или обновление его количества."""
        product_id = str(product.id)
        if product_id not in self.cart:
            self.cart[product_id] = {
                "quantity": 0,
                "price": str(product.price),
            }
        if override_quantity:
            self.cart[product_id]["quantity"] = quantity
        else:
            self.cart[product_id]["quantity"] += quantity
        self.save()

    def save(self) -> None:
        self.session.modified = True

    def remove(self, product) -> None:
        """Удаление товара из корзины."""
        product_id = str(product.id)
        if product_id in self.cart:
            del self.cart[product_id]
            self.save()

    def __iter__(self):
        """Перебор корзины."""
        product_ids = self.cart.keys()
        products = Product.objects.filter(id__in=product_ids)

        cart = self.cart.copy()
        for product in products:
            cart[str(product.id)]["product"] = product

        for item in cart.values():
            item["price"]: decimal = Decimal(item["price"])
            item["total_price"]: decimal = item["price"] * item["quantity"]
            yield item

    def __len__(self) -> int:
        """Возвращает общее количество товаров в корзине."""
        return len(self.cart)

    def get_total_price(self) -> Decimal:
        """Возвращает общую стоимость товаров в корзине."""
        return sum(
            Decimal(item["price"]) * item["quantity"]
            for item in self.cart.values()
        )

    def clear(self) -> None:
        """Очищение корзины."""
        del self.session[settings.CART_SESSION_ID]
        self.save()


import traceback
from typing import Any

from quart import ResponseReturnValue, request as view_request
from quart.views import View

from webapps.language.decorators.singleton import Singleton

from webapps.model.auth.access.access_errors import SerializableObjectNotAvialable
from webapps.model.identifier import ModelIdentifier
from webapps.model.auth.access.tic_tok_depot import SerializableObjectDepot
from webapps.modules.coroutinpromise.promisepool import PromisePool

from webapps.modules.lumber.lumber import Lumber

from webapps.modules.plugin.endpoints import PluginEndpoint
from webapps.modules.requests.httpcallbuilder import HttpCallBuilder
from webapps.modules.requests.httpheaders import HttpHeaders

from webapps.plugins.tplus.actuators.api_broker_call import TplusOpenapiBrokerHttpCall
from webapps.plugins.tplus.actuators.app_ticket_call import TplusTicketHttpCall
from webapps.plugins.tplus.actuators.app_token_call import TplusAppTokenHttpCall
from webapps.plugins.tplus.endpoints.auth.ticket import AppTicketEndpoints

from webapps.plugins.tplus.model.auth.app_ticket import AppTicket
from webapps.plugins.tplus.model.auth.app_token import AppToken
from webapps.plugins.tplus.model.dao.api_broker_request import TplusRestfulapiBrokerRequest
from webapps.plugins.tplus.model.dao.tplus_errors import AppTicketExpired, AppTokenExpired, AppTokenInvalid
from webapps.plugins.tplus.model.properties.tplus_endpoint_profile import TplusEndpointProfile
from webapps.plugins.tplus.model.properties.tplus_openapi_properties import TplusOpenApiProperties

@Singleton
class TplusOpenapiBroker(PluginEndpoint):

    _timber = Lumber.timber("endpoints")
    _err_timber = Lumber.timber("error")

    _ARGKEY_GRANT_TYPE_    = "grantType"
    _ARGVLU_REFRESH_TOKEN_ = "refresh_token"

    _ARGKEY_REFRESH_TOKEN_ = "refreshToken"

    class BrokerView(View):

        methods = ["GET", "POST"]

        def __init__(self, endpoint: PluginEndpoint) -> None:
            super().__init__()
            self._endpoint = endpoint

        async def dispatch_request(self, **kwargs: Any) -> ResponseReturnValue:

            if not TplusRestfulapiBrokerRequest._REQUEST_HEADER_CONTENT_TYPE_ in view_request.headers:
                return "A valid request must contain specified: 'Content-Type' as 'application/json'."

            content_type = view_request.headers.get(TplusRestfulapiBrokerRequest._REQUEST_HEADER_CONTENT_TYPE_)
            if (content_type != HttpHeaders._HDV_MIME_JSON_):
                return 'Content-Type not supported!'

            request = TplusRestfulapiBrokerRequest(await view_request.get_json())
            try:
                return await self._endpoint.request(request.api_path, request.api_method, request.body)
            except Exception as error:
                TplusOpenapiBroker._err_timber.error(f"{error}")
                TplusOpenapiBroker._err_timber.error(f"{error.args}")

                return "Request data is invalid/malformatted", 404

    def __init__(self, name: str, profile: TplusEndpointProfile, props: TplusOpenApiProperties) -> None:
        super().__init__(name, profile)

        assert profile and props

        self._profile = profile
        self._props = props

        self._conf_vault = dict(profile.valueset | props.valueset)

        self._seri_obj_depot = SerializableObjectDepot()
        self._http_call_builder = HttpCallBuilder(props.base_url)

        self._tok_identifier = ModelIdentifier(props.plugin_qualifier, profile.endpoint_qualifier, self.url)
        self._tic_identifier = AppTicketEndpoints.query_subscription()

        header_dic = {
            TplusOpenApiProperties._APP_KEY_: props.app_key,
            TplusOpenApiProperties._APP_SECRET_: props.app_secret
        }
        self._http_call_builder.headers = HttpHeaders(defaults= header_dic)

    def get_app_ticket(self) -> AppTicket:
        app_tic = AppTicket(self._seri_obj_depot.get(self._tic_identifier))

        if not app_tic.is_valid:
            raise AppTicketExpired(f"{app_tic.ticket} has expired. Lifetime: {app_tic.lifetime}.")

        return app_tic

    def get_token(self) -> AppToken:
        app_tok = AppToken(self._seri_obj_depot.get(self._tok_identifier))

        if not app_tok.is_valid:
            if not app_tok.is_expired:
                raise AppTokenInvalid(f"{app_tok.access_token_lifetime}")
            else:
                raise AppTicketExpired(f"{app_tok.refresh_token_lifetime}")

        return app_tok

    def put_token(self, app_tok) -> None:
        self._seri_obj_depot.put(app_tok, self._tok_identifier)

    async def try_fetch_ticket(self) -> AppTicket:
        ticket = None
        try:
            ticket = self.get_app_ticket()
        except (SerializableObjectNotAvialable, AppTicketExpired) as error:
            TplusOpenapiBroker._timber.info(f"App Ticket Error - not avialable or expired: '{error}', RENEWING.")
            ticket = await self.renew_app_ticket()
        finally:
            return ticket

    async def try_fetch_token(self) -> AppToken:
        token = None
        try:
            token = self.get_token()
        except (SerializableObjectNotAvialable, AppTokenExpired, AppTokenInvalid) as error:
            TplusOpenapiBroker._timber.info(f"App Token not avialable or expired: '{error}', RENEWING needed.")

            ticket = await self.try_fetch_ticket()
            token = await self.exchange_for_token(ticket)
            self.put_token(token)
        finally:
            return token

    async def renew_app_ticket(self):
        TplusOpenapiBroker._timber.debug(f"Calling renew_app_ticket()")

        http_call = self._http_call_builder.build(TplusTicketHttpCall, self._conf_vault)

        try:
            await http_call.refresh_app_ticket()
            app_tic = await PromisePool.wish(self._tic_identifier, None)
        except Exception as error:
            TplusOpenapiBroker._err_timber.error(f"Skipped waiting for app-ticket promise because: {error} - {error.args}")
            raise(error)

        return app_tic

    async def exchange_for_token(self, app_tic: AppTicket= None):
        TplusOpenapiBroker._timber.debug("Calling exchange_for_token()")

        http_call = self._http_call_builder.build(TplusAppTokenHttpCall, self._conf_vault)

        exchange_params = {
            TplusEndpointProfile._APP_TICKET_: app_tic.ticket,
            TplusOpenApiProperties._CERTIFICATE_: self._props.certificate
        }

        token_txt = await http_call.exchange_app_token(json = exchange_params)
        return AppToken(token_txt)

    async def refresh_token(self, app_tok: AppToken):
        TplusOpenapiBroker._timber.debug("Calling refresh_token()")

        refresh_params = {
            TplusOpenapiBroker._ARGKEY_GRANT_TYPE_: TplusOpenapiBroker._ARGVLU_REFRESH_TOKEN_,
            TplusOpenapiBroker._ARGKEY_REFRESH_TOKEN_: app_tok.refresh_token
        }

        http_call = self._http_call_builder.build(TplusAppTokenHttpCall, self._conf_vault)

        token_json = await http_call.refresh_app_token(url_params= refresh_params)
        return AppToken(token_json)

    async def request(self, api_path: str, api_method: str, request_body: dict):

        app_token = await self.try_fetch_token()
        headers = HttpHeaders(defaults={TplusOpenapiBrokerHttpCall._HDR_APP_TOKEN_: app_token.access_token})

        http_call = self._http_call_builder.build(TplusOpenapiBrokerHttpCall, self._conf_vault, api_path= api_path, api_method= api_method)
        http_call.update_http_headers(headers)

        return await http_call.broker_api_call(api_path, api_method, json= request_body)

    @property
    def view(self):
        return TplusOpenapiBroker.BrokerView.as_view(self._profile.name, self)


#!/usr/bin/env python3
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#

from gettext import gettext as _
from gi.repository import GObject, Gtk

from .config import Config
from .svg import JsonSvg

import datetime
import time
import gi
gi.require_version("Gtk", "3.0")

def relative_date(timestamp):
    t = datetime.date.fromtimestamp(timestamp)
    today = datetime.date.today()
    diff = t - today

    if diff.days == 0:
        return _('Today')
    if diff.days == -1:
        return _('Yesterday')
    if diff.days > -4:  # last 4 days we convert to weekdays
        return t.strftime('%A')

    return t.strftime('%x')

@Gtk.Template(resource_path='/org/freedesktop/TuhiGui/ui/Drawing.ui')
class Drawing(Gtk.Box):
    __gtype_name__ = "Drawing"

    label_timestamp = Gtk.Template.Child()
    image_svg = Gtk.Template.Child()
    btn_rotate_left = Gtk.Template.Child()
    btn_rotate_right = Gtk.Template.Child()

    def __init__(self, json_data, *args, **kwargs):
        super().__init__()
        self.orientation = Config.instance().orientation

        self.json_data = json_data
        self.svg = svg = JsonSvg(json_data, orientation=self.orientation)
        day = relative_date(svg.timestamp)
        hour = time.strftime('%H:%M', time.localtime(svg.timestamp))

        self.label_timestamp.set_text(f'{day} {hour}')
        self.image_svg.set_from_file(svg.filename)
        self.timestamp = svg.timestamp

    def refresh(self):
        self.svg = svg = JsonSvg(self.json_data, self.orientation)
        self.image_svg.set_from_file(svg.filename)

    @GObject.Property
    def name(self):
        return "drawing"

    @Gtk.Template.Callback('_on_download_button_clicked')
    def _on_download_button_clicked(self, button):
        dialog = Gtk.FileChooserDialog(_('Please choose a file'),
                                       None,
                                       Gtk.FileChooserAction.SAVE,
                                       (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                        Gtk.STOCK_SAVE, Gtk.ResponseType.OK))

        dialog.set_do_overwrite_confirmation(True)
        # Translators: the default filename to save to
        dialog.set_current_name(_('untitled.svg'))

        filter_any = Gtk.FileFilter()
        # Translators: filter name to show all/any files
        filter_any.set_name(_('Any files'))
        filter_any.add_pattern('*')
        filter_svg = Gtk.FileFilter()
        # Translators: filter to show svg files only
        filter_svg.set_name(_('SVG files'))
        filter_svg.add_pattern('*.svg')
        dialog.add_filter(filter_svg)
        dialog.add_filter(filter_any)

        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            import shutil
            file = dialog.get_filename()
            shutil.copyfile(self.svg.filename, file)
            # FIXME: error handling

        dialog.destroy()

    @Gtk.Template.Callback('_on_delete_button_clicked')
    def _on_delete_button_clicked(self, button):
        Config.instance().delete_drawing(self.timestamp)

    @Gtk.Template.Callback('_on_rotate_button_clicked')
    def _on_rotate_button_clicked(self, button):
        if button == self.btn_rotate_left:
            advance = 1
        else:
            advance = 3

        orientations = ['portrait', 'landscape', 'reverse-portrait', 'reverse-landscape'] * 3
        o = orientations[orientations.index(self.orientation) + advance]
        self.orientation = o
        self.refresh()


import torch
from torchvision import datasets
from torchvision import transforms
import torch.utils.data as Data
from sklearn import metrics

from GoogLeNet import GoogLeNet
from resnet_18 import ResNet_18
from resnet_50 import ResNet_50
def test(model):

    test_transformer = transforms.Compose(
        [
            transforms.RandomHorizontalFlip(0.2),
            transforms.RandomRotation(68),
            transforms.RandomGrayscale(0.2),
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.5, 0.5, 0.5],
                                 std=[0.5, 0.5, 0.5])
        ])

    test_data = datasets.ImageFolder("./dataset2-master/images/TEST", transform=test_transformer)
    test_loader = Data.DataLoader(dataset=test_data, batch_size=32, shuffle=False, num_workers=2)

    device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model=model.to(device)

    test_corrects=0.0
    test_num=0
    with torch.no_grad():
        for data_x,data_y in test_loader:
            data_x=data_x.to(device)
            data_y=data_y.to(device)

            model.eval()
            output=model(data_x)

            pre_lab=torch.argmax(output,dim=1)
            test_corrects+=torch.sum(pre_lab==data_y)
            test_num+=data_x.size(0)

    test_acc=test_corrects.double().item()/test_num
    print("the accurate rate is {:.3f}".format(test_acc))
    f1=metrics.f1_score(data_y,pre_lab,average="samples")
    print("the f1 score is {:.3f}".format(f1))

if __name__=="__main__":
    model=ResNet_50()
    model.load_state_dict(torch.load("ResNet_50_best_model.pt"))

    test(model)


class ELFFlags:
    class SymBind:
        STB_LOCAL = 0
        STB_GLOBAL = 1
        STB_WEAK = 2
        STB_LOPROC = 13
        STB_HIPROC = 15

    class SymType:
        STT_NOTYPE = 0
        STT_OBJECT = 1
        STT_FUNC = 2
        STT_SECTION = 3
        STT_FILE = 4
        STT_LOPROC = 13
        STT_HIPROC = 15

    class Reloc:
        R_PPC_ADDR32 = 1
        R_PPC_ADDR16_LO = 4
        R_PPC_ADDR16_HI = 5
        R_PPC_ADDR16_HA = 6
        R_PPC_REL24 = 10


from flask import Blueprint, make_response, request

from slamd.common.error_handling import SlamdUnprocessableEntityException, ValueNotSupportedException
from slamd.common.session_backup.session_service import SessionService

session_blueprint = Blueprint('session', __name__, url_prefix='/session')

"""
    The form that serves these endpoints is found in the navbar. As such it is not created using WTForms.
    It relies on CSRF token elements in the body of the page for authentication.
    These are usually provided by other WTForms. However, some pages may not have form elements on them, in particular
    the landing page.
    These pages require the manual addition of CSRF token fields.
"""

@session_blueprint.route('/', methods=['GET'])
def save_session():
    json_string = SessionService.convert_session_to_json_string()
    response = make_response(json_string.encode())
    filename = SessionService.create_default_filename()
    response.headers['Content-Disposition'] = f'attachment; filename={filename}'
    response.mimetype = 'text/json'
    return response

@session_blueprint.route('/', methods=['POST'])
def load_session():
    if 'file' not in request.files:
        raise SlamdUnprocessableEntityException(message='Request did not contain a file')

    file_extension = request.files['file'].filename.lower().split('.')[-1]
    if file_extension != 'json':
        raise ValueNotSupportedException(message=f'Invalid file type: {file_extension}')

    # Flask checks that the 'Content-Length' header is present.
    # Otherwise, it rejects the request and returns a status code 400 - Bad Request.
    length_of_file = int(request.headers['CONTENT_LENGTH'])
    try:
        file_as_string = request.files['file'].read(length_of_file).decode()
    except UnicodeDecodeError:
        raise SlamdUnprocessableEntityException(message='Error while attempting to decode JSON file')

    SessionService.load_session_from_json_string(file_as_string)

    # In the frontend, Javascript will reload the page automatically if it receives an OK response
    # Actual content of response does not matter
    return ''

@session_blueprint.route('/', methods=['DELETE'])
def clear_session():
    SessionService.clear_session()

    # In the frontend, Javascript will reload the page automatically if it receives an OK response
    # Actual content of response does not matter
    return ''


from itertools import permutations
import math

def middle_permutation(string):
    string = sorted(string)
    if len(string) < 10:
        for i, x in enumerate(permutations(string), 1):
            if i == math.factorial(len(string)) // 2:
                return ''.join(list(x))
    else:
        first = string[(len(string) // 2)]
        if len(string) % 2 == 0:
            first = string[(len(string) // 2)-1]
        second = ''
        if len(string) % 2 == 1: # odds
            second = string[string.index(first) - 1]

        contains = [first, second]
        rev = reversed(string)
        string = ''.join(contains) + ''.join(list(filter(lambda x: x not in contains, rev)))
        return string

# memorable this was solved during a CS DEPT session in preparation for algolympics

# For s = "abc", the result should be "bac".

# The permutations in order are: "abc", "acb", "bac", "bca", "cab", "cba" So, The middle term is "bac".


import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

from Formulas import Formulas
from Graficos import Graficos
from KmeansCalculo import KmeansCalculo
from Processamento import Processamento

class Main:

    def __init__(self):
        print("Main")
        self.K = 5
        self.formulas = Formulas()
        self.graficos = Graficos()
        self.labels: list = ['date', 'county', 'state', 'fips', 'cases', 'deaths']

    def processar_dataset(self):
        print("Processar Dataset")
        processamento = Processamento('us-counties-covid-19-dataset.csv')
        conjunto = processamento.read_dataset()
        conjunto = processamento.remove_colunas(['date', 'state', 'fips'])

        processamento.salvar_dataset('dataset_processado.csv')
        # print(conjunto.head(10))

        return conjunto

    def imprimir_graficos(self):
        print("Imprimir Graficos")
        conjunto = self.get_dataset_filtrado()

        self.graficos.plot_seaborn(conjunto)
        self.graficos.plot_scatter(conjunto)
        self.graficos.mpld3(conjunto)

    def get_dataset_processado(self):
        processamento = Processamento('dataset_processado.csv')
        conjunto = processamento.read_dataset()

        return conjunto

    def get_dataset_filtrado(self):
        processamento = Processamento('dataset_processado.csv')
        conjunto = processamento.read_dataset()

        filtro = "cases > 2 and deaths > 2"
        conjunto = processamento.filtrar_conjunto(max_item_conjunto=500, filtro=filtro)

        return conjunto

    def fit_k_means(self):
        print("Teste Kmeans")
        conjunto = self.get_dataset_filtrado()
        # conjunto = self.get_dataset_processado()

        kmeansCalculo = KmeansCalculo(conjunto, parada=20, max_iter=7)
        centroids, matriz = kmeansCalculo.fit_k_means(qt_centroids=self.K)

        # kmeansCalculo.reposicionar_centroids(centroids, matriz)

        # self.graficos.grafico_com_centroiods(kmeansCalculo.conjunto, centroids, visualizar_label_centroid=False)
        # self.graficos.grafico_com_centroiods_agrupados(matriz, centroids, visualizar_legenda=True, visualizar_label_centroid=False)

        # Exemplo da função
        # kmeansCalculo.reposicionar_centroids(centroids, matriz)

        kmeansCalculo.recalcular_centroids(centroids, matriz, self.graficos)

    def compare_kmeans(self):
        conjunto = self.get_dataset_filtrado()
        X = conjunto['cases'].values.reshape(-1, 1)
        y = conjunto['deaths'].values.reshape(-1, 1)

        conjunto_tratado = conjunto[['cases', 'deaths']]

        kmeans = KMeans(n_clusters=5).fit(conjunto_tratado)
        centroids = kmeans.cluster_centers_

        # print(centroids)
        # print(kmeans.labels_)

        # Plotting the cluster centers and the data points on a 2D plane
        plt.scatter(conjunto_tratado['cases'], conjunto_tratado['deaths'], c=kmeans.labels_.astype(float), s=50,
                    alpha=0.5)
        plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
        plt.show()

main = Main()
main.processar_dataset()
# main.imprimir_graficos()
main.fit_k_means()
main.compare_kmeans()


# python3
import sys

def compute_min_refills(distance, tank, stops):
    # write your code here
    i = 0
    n = len(stops)
    stops = [0]+stops
    output = 0
    stops.append(distance)
    last_fuel = 0

    while i<=n:
        last_fuel = i
        while i<=n and stops[i+1]-stops[last_fuel]<=tank:
            i += 1

        if i==last_fuel:
            return -1
        if i<=n:
            output += 1

    return output

if __name__ == '__main__':
    d, m, _, *stops = map(int, sys.stdin.read().split())
    print(compute_min_refills(d, m, stops))
    #print(compute_min_refills(500, 200, [100,200,300,400]))


import shutil
import zipfile
import os
import glob
import datetime

from pollen import utils
from pollen.scrlogger import ScrLogger

class Zipper(object):
    ''' Pollen Cloud Compiler Zip Util'''

    def __init__(self, workzip, bundle_paths, workname, cbundle):
        self.log          = ScrLogger()
        self.workzip      = workzip
        self.bundle_paths = bundle_paths
        self.cbundle      = cbundle
        self.bundle_names  = []
        self.tmpdir       = '/tmp/' + workname

    def zip(self):
        ''' create the file'''
        self.log.info("\nPreparing files...", 0)
        self.log.debug("Work directory: " + self.tmpdir)
        self.log.debug("Bundle directories (%s): %s" %
                       (str(len(self.bundle_paths) - 1),
                        str(self.bundle_paths[1:])))

        utils.rmfile(self.workzip)
        zip_ = zipfile.ZipFile(self.workzip, 'w')
        self._make_bundle_zip(zip_)
        self._make_c_zip(zip_)
        zip_.close()

    def _zip_bundles(self, zip_, paths):
        ''' add local bundles to the zip and maintain list of bundle names'''

        for src in paths:
            if src.find('*') != -1 or src.find('?') != -1:
                wildcards = glob.glob(src)
                if len(wildcards) > 0:
                    self._zip_bundles(zip_, wildcards)  # recurse for wildcard
                    continue
            if not os.path.exists(src):
                self.bundle_names.append(src)
                continue  # system bundle
            utils.rmdir(self.tmpdir)
            bundle_name = utils.get_bundle_name(src)
            if bundle_name not in self.bundle_names:
                self.bundle_names.append(bundle_name)
            shutil.copytree(src, self.tmpdir + '/' + bundle_name)
            self._zip_dir(self.tmpdir, zip_)
            utils.rmdir(self.tmpdir)

    def _make_bundle_zip(self, zip_):
        ''' add local bundles to the zip and maintain list of bundle names'''
        if self.bundle_paths is not None:
            for src in self.bundle_paths:
                if src[0] == '@':  # not on the server
                    continue
                _, _, _ = os.walk(src).next()  # todo: ejs??

            self._zip_bundles(zip_, self.bundle_paths)

    def _make_c_zip(self, zip_):
        ''' add c dirs to zip'''
        ptmp = []
        if self.cbundle is None:
            return
        else:
            for des in self.cbundle:
                if des[0] != '@':  # not on the server
                    ptmp.append(des)

        if len(ptmp) > 0:
            self.log.debug("C directory included: %s" % str(ptmp))

        for src in ptmp:
            utils.rmdir(self.tmpdir)
            os.mkdir(self.tmpdir)
            shutil.copytree(src, self.tmpdir + '/cbundle/')
            self._zip_dir(self.tmpdir, zip_)
            utils.rmdir(self.tmpdir)

    def _filename_ok(self, file_):
        ''' some files should not be sent to the server'''
        if file_.endswith('.zip'):
            return False
        if file_.endswith('stderr'):
            return False
        if file_.endswith('stdout'):
            return False
        if file_.endswith('.out'):
            return False
        if file_.endswith('.hex'):
            return False
        return True

    def _zip_dir(self, path, zip_):
        ''' zip!'''
        origpath = os.getcwd()
        os.chdir(path)
        for root, _, files in os.walk('.'):
            for file_ in files:
                if self._filename_ok(file_):
                    zip_.write(os.path.join(root, file_))
        os.chdir(origpath)


from bs4 import BeautifulSoup
from urllib.parse import urlencode, urlunparse
import json
import os
import requests
import sys

def price(listing):
    price_node = listing.find('a', class_='text-price')
    try:
        price = list(price_node.children)[2]
    except:
        price = price_node.get_text()

    price = (
        price
            .strip()
            .replace(',', '')
            .replace(u'\xa3', '')
    )

    try:
        return int(price)
    except:
        return None

def beds(listing):
    try:
        return int(
            listing
                .find('span', class_='num-beds')
                .get_text()
                .strip()
        )
    except:
        return None

def parse_listing(listing):
    return {
        'listing_id': int(listing.attrs['data-listing-id']),
        'price': price(listing),
        'latitude': listing
            .find('meta', itemprop='latitude')
            .attrs['content'],
        'longitude': listing
            .find('meta', itemprop='longitude')
            .attrs['content'],
        'beds': beds(listing)
    }

def parse_listings(listings):
    return map(parse_listing, listings)

params = {
    'q': 'london',
    'results_sort': 'newest_listings',
    'search_source': 'home',
    'price_frequency': 'per_month',
    'radius': 0,
    'identifier': 'london',
    'page_size': 100,
    'pn': 1
}

url = urlunparse([
    'http',
    'www.zoopla.co.uk',
    'to-rent/property/london/',
    '',
    urlencode(params),
    ''
])

soup = BeautifulSoup(requests.get(url).content, 'html5lib')

print(len(soup.find_all('li', attrs={'data-listing-id': True})))

#listings = parse_listings()

# list(map(json.dumps, listings))


#
# GUI of second window
#

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIntValidator
from PyQt5.QtCore import *

from classes import Function, Worker
from convert_and_dfgBuild import create_dfg, start_DFG
from function_manag import get_ret_values, merge_in_one

class Ui_ConstructorWindow(object):
    def setupUi(self, MainWindow, fs=[]):
        self.fs = fs
        self.function = None
        self.dfg = None

        # Created in: PyQt5 designer
        # Created by: PyQt5 UI code generator 5.15.4
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1130, 870)
        MainWindow.setMaximumSize(QtCore.QSize(1500, 870))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")

        self.listFunctions = QtWidgets.QListWidget(self.centralwidget)
        self.listFunctions.setGeometry(QtCore.QRect(40, 40, 161, 221))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listFunctions.setFont(font)
        self.listFunctions.setObjectName("listFunctions")
        self.gridLayout.addWidget(self.listFunctions, 1, 0, 2, 2)

        self.listFunctAdd = QtWidgets.QListWidget(self.centralwidget)
        self.listFunctAdd.setGeometry(QtCore.QRect(250, 40, 161, 191))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listFunctAdd.setFont(font)
        self.listFunctAdd.setObjectName("listFunctAdd")
        self.gridLayout.addWidget(self.listFunctAdd, 1, 2, 1, 1)

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 10, 131, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 2)

        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(260, 10, 161, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 0, 2, 1, 1)

        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(470, 10, 161, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 0, 3, 1, 1)

        self.listFunctDel = QtWidgets.QListWidget(self.centralwidget)
        self.listFunctDel.setGeometry(QtCore.QRect(460, 40, 161, 191))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listFunctDel.setFont(font)
        self.listFunctDel.setObjectName("listFunctDel")
        self.gridLayout.addWidget(self.listFunctDel, 1, 3, 1, 1)

        self.btnGenerate = QtWidgets.QPushButton(self.centralwidget)
        self.btnGenerate.setGeometry(QtCore.QRect(820, 230, 131, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnGenerate.setFont(font)
        self.btnGenerate.setObjectName("btnGenerate")
        self.gridLayout.addWidget(self.btnGenerate, 2, 4, 1, 1)

        self.textPrint = QtWidgets.QTextEdit(self.centralwidget)
        self.textPrint.setGeometry(QtCore.QRect(690, 40, 391, 171))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.textPrint.setFont(font)
        self.textPrint.setReadOnly(True)
        self.textPrint.setObjectName("textPrint")
        self.gridLayout.addWidget(self.textPrint, 1, 4, 1, 1)

        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(50, 290, 61, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)

        self.lineFuncName = QtWidgets.QLineEdit(self.centralwidget)
        self.lineFuncName.setGeometry(QtCore.QRect(180, 290, 151, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineFuncName.setFont(font)
        self.lineFuncName.setReadOnly(True)
        self.lineFuncName.setObjectName("lineFuncName")
        self.gridLayout.addWidget(self.lineFuncName, 3, 1, 1, 2)

        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(50, 320, 71, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)

        self.lineInstrsNum = QtWidgets.QLineEdit(self.centralwidget)
        self.lineInstrsNum.setGeometry(QtCore.QRect(180, 320, 151, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineInstrsNum.setFont(font)
        self.lineInstrsNum.setReadOnly(True)
        self.lineInstrsNum.setObjectName("lineInstrsNum")
        self.gridLayout.addWidget(self.lineInstrsNum, 4, 1, 1, 2)

        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(50, 350, 71, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_6.setFont(font)
        self.label_6.setObjectName("label_6")
        self.gridLayout.addWidget(self.label_6, 5, 0, 1, 1)

        self.listArgs = QtWidgets.QListWidget(self.centralwidget)
        self.listArgs.setGeometry(QtCore.QRect(180, 350, 151, 81))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listArgs.setFont(font)
        self.listArgs.setObjectName("listArgs")
        self.gridLayout.addWidget(self.listArgs, 5, 1, 2, 2)

        self.label_12 = QtWidgets.QLabel(self.centralwidget)
        self.label_12.setText("")
        self.label_12.setObjectName("label_12")
        self.gridLayout.addWidget(self.label_12, 6, 0, 1, 1)

        self.label_7 = QtWidgets.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(50, 440, 81, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_7.setFont(font)
        self.label_7.setObjectName("label_7")
        self.gridLayout.addWidget(self.label_7, 7, 0, 1, 1)

        self.label_21 = QtWidgets.QLabel(self.centralwidget)
        self.label_21.setObjectName("label_21")
        self.gridLayout.addWidget(self.label_21, 8, 0, 1, 1)

        self.listReturnValues = QtWidgets.QListWidget(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listReturnValues.setFont(font)
        self.listReturnValues.setObjectName("listReturnValues")
        self.gridLayout.addWidget(self.listReturnValues, 7, 1, 2, 2)

        self.lineDistance = QtWidgets.QLineEdit(self.centralwidget)
        self.lineDistance.setGeometry(QtCore.QRect(180, 470, 151, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineDistance.setFont(font)
        self.lineDistance.setObjectName("lineDistance")
        self.onlyInt = QIntValidator()
        self.lineDistance.setValidator(self.onlyInt)
        self.gridLayout.addWidget(self.lineDistance, 9, 1, 1, 2)

        self.label_8 = QtWidgets.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(50, 470, 81, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_8.setFont(font)
        self.label_8.setObjectName("label_8")
        self.gridLayout.addWidget(self.label_8, 9, 0, 1, 1)

        self.btnFindNodes = QtWidgets.QPushButton(self.centralwidget)
        self.btnFindNodes.setGeometry(QtCore.QRect(130, 510, 131, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnFindNodes.setFont(font)
        self.btnFindNodes.setObjectName("btnFindNodes")
        self.gridLayout.addWidget(self.btnFindNodes, 10, 1, 1, 2)

        self.labelNodes = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.labelNodes.setFont(font)
        self.labelNodes.setObjectName("labelNodes")
        self.gridLayout.addWidget(self.labelNodes, 11, 0, 1, 1)
        self.lineNodes = QtWidgets.QLineEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineNodes.setFont(font)
        self.lineNodes.setReadOnly(True)
        self.lineNodes.setObjectName("lineNodes")
        self.gridLayout.addWidget(self.lineNodes, 11, 1, 1, 2)
        self.labelEdges = QtWidgets.QLabel(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.labelEdges.setFont(font)
        self.labelEdges.setObjectName("labelEdges")
        self.gridLayout.addWidget(self.labelEdges, 12, 0, 1, 1)
        self.lineEdges = QtWidgets.QLineEdit(self.centralwidget)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineEdges.setFont(font)
        self.lineEdges.setReadOnly(True)
        self.lineEdges.setObjectName("lineEdges")
        self.gridLayout.addWidget(self.lineEdges, 12, 1, 1, 2)

        self.lineNodesNum = QtWidgets.QLineEdit(self.centralwidget)
        self.lineNodesNum.setGeometry(QtCore.QRect(220, 570, 111, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineNodesNum.setFont(font)
        self.lineNodesNum.setReadOnly(True)
        self.lineNodesNum.setObjectName("lineNodesNum")
        self.gridLayout.addWidget(self.lineNodesNum, 13, 2, 1, 1)

        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(50, 560, 161, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_9.setFont(font)
        self.label_9.setObjectName("label_9")
        self.gridLayout.addWidget(self.label_9, 13, 0, 1, 2)

        self.listFoundNodes = QtWidgets.QListWidget(self.centralwidget)
        self.listFoundNodes.setGeometry(QtCore.QRect(50, 610, 281, 191))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.listFoundNodes.setFont(font)
        self.listFoundNodes.setObjectName("listFoundNodes")
        self.gridLayout.addWidget(self.listFoundNodes, 14, 0, 1, 3)

        self.btnDisplayDFG = QtWidgets.QPushButton(self.centralwidget)
        self.btnDisplayDFG.setGeometry(QtCore.QRect(120, 810, 131, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnDisplayDFG.setFont(font)
        self.btnDisplayDFG.setObjectName("btnDisplayDFG")
        self.btnDisplayDFG.setEnabled(False)
        self.gridLayout.addWidget(self.btnDisplayDFG, 15, 0, 1, 3)

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1128, 21))
        self.menubar.setObjectName("menubar")

        MainWindow.setMenuBar(self.menubar)
        self.actionBack = QtWidgets.QAction(MainWindow)
        self.actionBack.setObjectName("actionBack")

        self.menubar.addAction(self.actionBack)

        self.btnClearlAdd = QtWidgets.QPushButton(self.centralwidget)
        self.btnClearlAdd.setGeometry(QtCore.QRect(250, 240, 161, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnClearlAdd.setFont(font)
        self.btnClearlAdd.setObjectName("btnClearlAdd")
        self.gridLayout.addWidget(self.btnClearlAdd, 2, 2, 1, 1)
        self.btnClearlAdd.clicked.connect(self.unselectAdd)

        self.btnClearlDel = QtWidgets.QPushButton(self.centralwidget)
        self.btnClearlDel.setGeometry(QtCore.QRect(460, 240, 161, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnClearlDel.setFont(font)
        self.btnClearlDel.setObjectName("btnClearlDel")
        self.gridLayout.addWidget(self.btnClearlDel, 2, 3, 1, 1)

        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(370, 290, 711, 551))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        header = self.tableWidget.horizontalHeader()
        header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
        header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
        self.tableWidget.verticalHeader().setVisible(False)
        self.tableWidget.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
        self.gridLayout.addWidget(self.tableWidget, 3, 3, 13, 2)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        # Set all functions from uploaded LLVM module
        for f in self.fs:
            self.listFunctions.addItem(f.name)
            self.listFunctAdd.addItem(f.name)
            self.listFunctDel.addItem(f.name)
        self.listFunctAdd.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection
        )
        self.listFunctDel.setSelectionMode(
            QtWidgets.QAbstractItemView.ExtendedSelection
        )

        self.btnClearlDel.clicked.connect(self.unselectDel)
        self.btnGenerate.clicked.connect(self.generate)
        self.btnFindNodes.clicked.connect(self.find_nodes)
        self.btnDisplayDFG.clicked.connect(self.show_dfg_path)

        self.threadpool = QThreadPool()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "DFGConstructor"))
        self.label.setText(_translate("MainWindow", "Main function for DFG:"))
        self.label_2.setText(_translate("MainWindow", "Functions to add:"))
        self.label_3.setText(_translate("MainWindow", "Functions to exclude:"))
        self.btnGenerate.setText(_translate("MainWindow", "Generate model"))
        self.label_4.setText(_translate("MainWindow", "Function"))
        self.label_5.setText(_translate("MainWindow", "Instructions"))
        self.label_6.setText(_translate("MainWindow", "Arguments"))
        self.label_7.setText(_translate("MainWindow", "Return value"))
        self.label_8.setText(_translate("MainWindow", "Distance"))
        self.btnFindNodes.setText(_translate("MainWindow", "Find nodes"))
        self.label_9.setText(_translate("MainWindow", "Nodes on entered distance\n"
"from return value"))
        self.label_21.setText(_translate("MainWindow", "            \n\n"))
        self.btnDisplayDFG.setText(_translate("MainWindow", "Display path"))
        self.btnClearlDel.setText(_translate("MainWindow", "Clear"))
        self.btnClearlAdd.setText(_translate("MainWindow", "Clear"))
        self.labelNodes.setText(_translate("MainWindow", "Nodes"))
        # self.labelEdges_2.setText(_translate("MainWindow", "   "))
        self.labelEdges.setText(_translate("MainWindow", "Edges"))
        # self.menuBack.setTitle(_translate("MainWindow", "Home"))
        self.actionBack.setText(_translate("MainWindow", "Back"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "Type"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Value"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "Distance"))

    def unselectAdd(self):
        self.listFunctAdd.clearSelection()
        self.listFunctAdd.clearFocus()

    def unselectDel(self):
        self.listFunctDel.clearSelection()
        self.listFunctDel.clearFocus()

    # Merging other functions into main one and deleting the marked ones
    def generate(self):
        self.dfg = None
        self.lineFuncName.setText('')
        self.lineInstrsNum.setText('')
        self.lineDistance.setText('')
        self.listArgs.clear()
        self.listReturnValues.clear()
        # self.lineRetValue.setText('')
        self.listFoundNodes.clear()
        self.lineNodesNum.setText('')
        self.lineNodes.setText('')
        self.lineEdges.setText('')

        if len(self.listFunctions.selectedItems()) == 1:
            addf = self.listFunctAdd.selectedItems()
            delf = self.listFunctDel.selectedItems()
            worker = Worker(merge_in_one, self.fs, self.listFunctions.selectedItems()[0].text(),
                            [a.text() for a in addf], [d.text() for d in delf])
            worker.signals.result.connect(self.set_all)
            worker.signals.progress.connect(self.reportProgress)
            self.threadpool.start(worker)

    def reportProgress(self, progress):
        self.textPrint.append(progress)

    # Set information about final function
    def set_all(self, result: Function):
        if len(result.labels) > 1:
            self.textPrint.append(f"Error: chosen function {result.name} has more than 1 label")
            self.textPrint.append(f"Choose another function")
            return
        self.function = result
        self.textPrint.append("")
        self.lineFuncName.setText(result.name)
        self.lineInstrsNum.setText(str(len(result.labels[0].operations)))
        self.listArgs.addItems(result.params)
        if result.labels[0].operations[-1].args[0] == '':
            self.btnDisplayDFG.setEnabled(False)
            self.btnFindNodes.setEnabled(False)
            self.btnGenerate.setEnabled(False)
            worker = Worker(get_ret_values, result)
            worker.signals.result.connect(self.set_return_val)
            worker.signals.progress.connect(self.reportProgress)
            self.threadpool.start(worker)
        else:
            self.listReturnValues.addItem(result.labels[0].operations[-1].args[0])

    # Set returning values of final function
    def set_return_val(self, ret_values):
        self.listReturnValues.addItems(ret_values)
        self.btnGenerate.setEnabled(True)
        self.btnDisplayDFG.setEnabled(True)
        self.btnFindNodes.setEnabled(True)

    # Search for nodes at entered distance, if dfg is not created yet -> construct a dfg
    def find_nodes(self):
        distance = self.lineDistance.text()
        self.listFoundNodes.clear()
        self.lineNodesNum.setText('')
        if distance and self.function is not None:
            self.btnDisplayDFG.setEnabled(False)
            self.btnFindNodes.setEnabled(False)
            self.btnGenerate.setEnabled(False)
            worker = Worker(create_dfg, self.function)
            worker.signals.result.connect(self.save_dfg)
            worker.signals.progress.connect(self.reportProgress)
            self.threadpool.start(worker)

    # Save constructed dfg and set information about it
    def save_dfg(self, dfg):
        self.dfg = dfg
        self.lineNodes.setText(str(len(dfg.nodes)))
        self.lineEdges.setText(str(len(dfg.edges)))
        distance = self.lineDistance.text()
        if self.listReturnValues.count() == 0:
            self.btnDisplayDFG.setEnabled(True)
            self.btnFindNodes.setEnabled(True)
            self.btnGenerate.setEnabled(True)
            return
        ret_val = self.listReturnValues.selectedItems()[0].text() if len(self.listReturnValues.selectedItems()) == 1 \
            else self.listReturnValues.item(0).text()
        worker = Worker(start_DFG, self.dfg, self.function, distance, ret_val)
        worker.signals.result.connect(self.display_nodes)
        worker.signals.progress.connect(self.reportProgress)
        self.threadpool.start(worker)

    # Display the found nodes at entered distance
    def display_nodes(self, dfg):
        if dfg != []:
            self.dfg = dfg
            self.listFoundNodes.clear()
            self.lineNodesNum.setText(str(len(self.dfg.map_path.keys())))
            for node, value in self.dfg.map_path.items():
                for val in value:
                    self.listFoundNodes.addItem(str(node))
        else:
            self.textPrint.append(f"Error: path was not found")
        self.btnDisplayDFG.setEnabled(True)
        self.btnFindNodes.setEnabled(True)
        self.btnGenerate.setEnabled(True)

    # Display the path from chosen node to a returning variable in the table
    def show_dfg_path(self):
        if len(self.listFoundNodes.selectedItems()) == 1 and self.dfg is not None:
            distance = self.lineDistance.text()
            value = self.listFoundNodes.selectedItems()[0].text()

            index = self.listFoundNodes.selectedIndexes()[0].row()
            number = 0
            for i in range(0, index):
                if self.listFoundNodes.item(i).text() == value:
                    number += 1

            nodes = self.dfg.map_path[value][number]
            self.display_path(nodes)

    # Displaying path in the table
    def display_path(self, nodes):
        x = 0
        for node in nodes[::-1]:
            key = node[0]
            self.tableWidget.setRowCount(x+2)
            item = QtWidgets.QTableWidgetItem()
            self.tableWidget.setVerticalHeaderItem(x, item)
            item = QtWidgets.QTableWidgetItem()
            self.tableWidget.setVerticalHeaderItem(x+1, item)

            item3 = QtWidgets.QTableWidgetItem()
            item3.setTextAlignment(Qt.AlignCenter)
            item3.setText("n")
            self.tableWidget.setItem(x, 0, item3)

            item4 = QtWidgets.QTableWidgetItem()
            item4.setText(node[1].name)
            self.tableWidget.setItem(x, 1, item4)
            item5 = QtWidgets.QTableWidgetItem()
            item5.setText(str(key))
            item5.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(x, 2, item5)

            item6 = QtWidgets.QTableWidgetItem()
            item6.setText("e")
            item6.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(x+1, 0, item6)
            item7 = QtWidgets.QTableWidgetItem()
            item7.setText(node[2].name)
            item7.setTextAlignment(Qt.AlignCenter)
            self.tableWidget.setItem(x+1, 1, item7)

            x += 2

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_ConstructorWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


from log import logger
from collections import OrderedDict
from flask import Blueprint, request, make_response
from model import Statistics, DailyFreq
from service import RecordService
from repository import RecordRepository
from database import db
from common import response_success, response_failed
from util import validate_token, convert_to_date

record_repository = RecordRepository(conn=db)
record_service = RecordService(record_repo=record_repository)
statistics_controller = Blueprint("statistics_controller", __name__, url_prefix="/api/v1")

OVERHEAT_STATUS = 2
NORMAL_STATUS = 1
UNDERHEAT_STATUS = 0
PROCESSING_STATUS = -1

@statistics_controller.get("/statistics")
def get_statistics():
    uid = int(request.args.get("uid", 0))
    token = request.headers.get("Authorization")

    if uid == 0:
        return make_response(response_failed(400, "need uid"), 400)

    payload, _ = validate_token(token)
    if payload["id"] != uid:
        logger.error("user not match")
        return make_response(response_failed(400, "user not match"), 400)

    records = record_service.get_records_by_uid(uid)

    if len(records) == 0:
        logger.info("user=%d has no record" % uid)
        return make_response(response_success(None, "user=%d has no record" % uid))

    overheat_num, normal_num, underheat_num, processing_num = 0, 0, 0, 0
    daily_dict = {}

    for record in records:
        if record.result >= OVERHEAT_STATUS:
            overheat_num += 1
        elif record.result >= NORMAL_STATUS:
            normal_num += 1
        elif record.result >= UNDERHEAT_STATUS:
            underheat_num += 1
        elif record.result >= PROCESSING_STATUS:
            processing_num += 1

        date = convert_to_date(record.request_time)
        if date in daily_dict:
            daily_dict[date] += 1
        else:
            daily_dict.setdefault(date, 1)

    ordered_daily_dict = OrderedDict(sorted(daily_dict.items(), key=lambda x: x[0]))
    ordered_freqs : list[DailyFreq] = []
    for key, value in ordered_daily_dict.items():
        daily_freq = DailyFreq(key, value)
        ordered_freqs.append(daily_freq)

    daily_freqs : list[DailyFreq] = [ordered_freqs[0]]
    for i in range(1, len(ordered_freqs)):
        for date in range(daily_freqs[-1].date+86400, ordered_freqs[i].date, 86400):
            daily_freqs.append(DailyFreq(date, 0))
        daily_freqs.append(ordered_freqs[i])

    statistics = Statistics(uid, overheat_num, normal_num, underheat_num, processing_num,
                            daily_freqs)

    return make_response(response_success(statistics.to_json(), "get statistics info successfully"))


#!/usr/bin/python3

# ML_hw3_Q5.5.1.py

# modules
import pandas as pd
import numpy as np
import math
import statistics

# Loading CSV Files

## training data ##
train_f = open("training_data_student/train.csv")
train_a = np.loadtxt(train_f, delimiter=",")

train_X = np.delete(train_a, -1, 1)
train_y = train_a[:,784]

## test data ##
test_f = open("training_data_student/test.csv")
test_a = np.loadtxt(test_f, delimiter=",")

test_X = np.delete(test_a, -1, 1)
test_y = test_a[:,784]

## init weights ##
# 1st layer
alpha1_f = open("params/alpha1.txt")
alpha1 = np.loadtxt(alpha1_f, delimiter=",")

beta1_f = open("params/beta1.txt")
beta1 = np.loadtxt(beta1_f, delimiter=",")
beta1 = beta1[:,None]

alpha_star = alpha1
alpha = np.hstack((beta1, alpha_star))

# 2nd layer
alpha2_f = open("params/alpha2.txt")
alpha2 = np.loadtxt(alpha2_f, delimiter=",")

beta2_f = open("params/beta2.txt")
beta2 = np.loadtxt(beta2_f, delimiter=",")
beta2 = beta2[:,None]

beta_star = alpha2
beta = np.hstack((beta2, beta_star))

### hyper-parameters
# learning rate
eta = 0.01
# number of epochs
epochs = 100

batch_size = 10

## training loop ##
print("epoch\ttrain_l\ttest_l\ttest_ac")
for epoch in range(epochs):
	## training ##
	l_train_loss = []
	total_t = int(len(train_X)/batch_size)
	for t in range(total_t):
		x = train_X[t:t+batch_size].T
		x = np.vstack(([1]*batch_size,x))

		# y: to one hot
		y_true = train_y[t:t+batch_size].T
		y_onehot = np.zeros((10,batch_size))
		for n in range(batch_size):
			y_onehot[int(y_true[n]), n] = 1
		y = y_onehot

		####### forward #######
		a = np.matmul(alpha,x) # 1st linear layer (pre-activation)

		z = 1/(1+np.exp(-a)) # hidden layer (Sigma activation)
		z = np.vstack(([1]*batch_size,z)) # add bias z0=1

		b = np.matmul(beta,z) # 2nd linear layer (pre-activation)

		y_hat = np.exp(b)/sum(np.exp(b)) # output layer (softmax activation)

		loss_v = -sum(y*np.log(y_hat))
		loss = statistics.mean(loss_v)
		l_train_loss.append(float(loss))

		####### backward #######
		### update weights: beta ###
		# deri of l by b (10, batch_size)
		d_l_b = -y + y_hat

		# deri of l by beta (batch_size, 257)
		d_l_beta = np.matmul(d_l_b, np.transpose(z))

		### update weights: alpha ###
		# deri of z by a (256, batch_size)
		d_z_a = np.exp(-a)/((1+np.exp(-a))**2)

		# deri of l by z (256, batch_size)
		beta_star = np.delete(beta, 0, 1)
		d_l_z = np.matmul(np.transpose(beta_star), d_l_b)

		# deri of l by a (256, batch_size)
		d_l_a = d_l_z * d_z_a

		# deri of l by alpha (256, 785)
		d_l_alpha = np.matmul(d_l_a, np.transpose(x))

		# update alpha (256, 785)
		alpha = alpha - ( eta * d_l_alpha )

		# update beta (10, 257)
		beta = beta - (eta * d_l_beta)

		#print('step', t, float(loss))

	# compute the average training loss for the epoch
	average_training_loss = statistics.mean(l_train_loss)

	## testing ##
	l_test_loss = []
	n_correct = 0
	for n in range(len(test_X)):
		# x: add bias x0=1
		x = test_X[n]
		x = np.vstack(([1],x[:, np.newaxis]))

		# y: to one hot
		y_true = int(test_y[n])
		y_onehot = [0]*10
		y_onehot[y_true] = 1
		y = np.array([y_onehot]).T

		####### forward #######
		a = np.matmul(alpha,x) # 1st linear layer (pre-activation)

		z = 1/(1+np.exp(-a)) # hidden layer (Sigma activation)
		z = np.vstack(([1],z)) # add bias z0=1

		b = np.matmul(beta,z) # 2nd linear layer (pre-activation)

		y_hat = np.exp(b)/sum(np.exp(b)) # output layer (softmax activation)

		loss = -sum(y*np.log(y_hat))
		l_test_loss.append(float(loss))

		# evaluate
		y_hat = y_hat.tolist()
		y_predict = y_hat.index(max(y_hat))
		if y_predict == y_true:
			n_correct += 1

	# compute the average test loss for the epoch
	average_test_loss = statistics.mean(l_test_loss)

	# compute test accuracy
	l_test_accuracy = n_correct / len(test_X)

	print('%i\t%.4f\t%.4f\t%.4f' %(epoch, average_training_loss, average_test_loss, l_test_accuracy))

# close the files
train_f.close()
test_f.close()

alpha1_f.close()
alpha2_f.close()
beta1_f.close()
beta2_f.close()


from django import forms
from posts.models import Comment, Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('text', 'group', 'image')
        labels = {
            'text': 'Тут будет пост',
            'group': 'А вот тут - группа',
        }
        help_texts = {
            'text': 'Вот прям сюда пиши!',
            'group': 'Группа, к которой будет относиться пост',
        }

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('text',)
        help_texts = {
            'text': 'Сюда комментарий пиши'
        }


import contextlib
import hashlib
import logging
import os
import re
from datetime import datetime, timedelta
from fnmatch import fnmatch

import click
import click_odoo
from click_odoo import odoo

from ._dbutils import advisory_lock, db_exists, pg_connect
from .manifest import expand_dependencies
from .update import _save_installed_checksums

_logger = logging.getLogger(__name__)

EXCLUDE_PATTERNS = ("*.pyc", "*.pyo")

def check_dbname(dbname):
    if not re.match("^[A-Za-z][A-Za-z0-9-_.]*$", dbname):
        raise click.ClickException("Invalid database name '{}'".format(dbname))

def check_cache_prefix(cache_prefix):
    if not re.match("^[A-Za-z][A-Za-z0-9-]{0,7}$", cache_prefix):
        raise click.ClickException(
            "Invalid cache prefix name '{}'".format(cache_prefix)
        )

@contextlib.contextmanager
def _patch_ir_attachment_store(force_db_storage):
    @odoo.api.model
    def _db_storage(self):
        return "db"

    if not force_db_storage:
        yield
    else:
        # make sure attachments created during db initialization
        # are stored in database, so we get something consistent
        # when recreating the db by copying the cached template
        if odoo.release.version_info >= (12, 0):
            from odoo.addons.base.models.ir_attachment import IrAttachment
        else:
            from odoo.addons.base.ir.ir_attachment import IrAttachment
        orig = IrAttachment._storage
        IrAttachment._storage = _db_storage
        try:
            yield
        finally:
            IrAttachment._storage = orig

def odoo_createdb(dbname, demo, module_names, force_db_storage):
    with _patch_ir_attachment_store(force_db_storage):
        odoo.service.db._create_empty_database(dbname)
        odoo.tools.config["init"] = dict.fromkeys(module_names, 1)
        odoo.tools.config["without_demo"] = not demo
        odoo.modules.registry.Registry.new(dbname, force_demo=demo, update_module=True)
        _logger.info(click.style(f"Created new Odoo database {dbname}.", fg="green"))
        with odoo.sql_db.db_connect(dbname).cursor() as cr:
            _save_installed_checksums(cr)
        odoo.sql_db.close_db(dbname)

def _fnmatch(filename, patterns):
    for pattern in patterns:
        if fnmatch(filename, pattern):
            return True
    return False

def _walk(top, exclude_patterns=EXCLUDE_PATTERNS):
    for dirpath, dirnames, filenames in os.walk(top):
        dirnames.sort()
        reldir = os.path.relpath(dirpath, top)
        if reldir == ".":
            reldir = ""
        for filename in sorted(filenames):
            filepath = os.path.join(reldir, filename)
            if _fnmatch(filepath, exclude_patterns):
                continue
            yield filepath

def addons_hash(module_names, with_demo):
    h = hashlib.sha1()
    h.update("!demo={}!".format(int(bool(with_demo))).encode("utf8"))
    for module_name in sorted(expand_dependencies(module_names, True, True)):
        module_path = odoo.modules.get_module_path(module_name)
        h.update(module_name.encode("utf8"))
        for filepath in _walk(module_path):
            h.update(filepath.encode("utf8"))
            with open(os.path.join(module_path, filepath), "rb") as f:
                h.update(f.read())
    return h.hexdigest()

def refresh_module_list(dbname):
    with click_odoo.OdooEnvironment(database=dbname) as env:
        env["ir.module.module"].update_list()

class DbCache:
    """Manage a cache of db templates.

    Templates are named prefix-YYYYmmddHHMM-hashsum, where
    YYYYmmddHHMM is the date and time when the given hashsum has last been
    used for that prefix.
    """

    HASH_SIZE = hashlib.sha1().digest_size * 2
    MAX_HASHSUM = "f" * HASH_SIZE

    def __init__(self, prefix, pgcr):
        check_cache_prefix(prefix)
        self.prefix = prefix
        self.pgcr = pgcr

    def _lock(self):
        return advisory_lock(self.pgcr, self.prefix)

    def _make_pattern(self, dt=None, hs=None):
        if dt:
            dtpart = dt.strftime("%Y%m%d%H%M")
        else:
            dtpart = "_" * 12
        if hs:
            hspart = hs
        else:
            hspart = "_" * self.HASH_SIZE
        pattern = self.prefix + "-" + dtpart + "-" + hspart
        # 63 is max postgres db name, so we may truncate the hash part
        return pattern[:63]

    def _make_new_template_name(self, hashsum):
        return self._make_pattern(dt=datetime.utcnow(), hs=hashsum)

    def _create_db_from_template(self, dbname, template):
        _logger.info(
            click.style(
                f"Creating database {dbname} from template {template}",
                fg="green",
            )
        )
        self.pgcr.execute(
            f"""
            CREATE DATABASE "{dbname}"
            ENCODING 'unicode'
            TEMPLATE "{template}"
            """
        )

    def _rename_db(self, dbname_from, dbname_to):
        self.pgcr.execute(
            f"""
            ALTER DATABASE "{dbname_from}"
            RENAME TO "{dbname_to}"
            """
        )

    def _drop_db(self, dbname):
        _logger.info(f"Dropping database {dbname}")
        self.pgcr.execute(
            f"""
            DROP DATABASE "{dbname}"
            """
        )

    def _find_template(self, hashsum):
        """search same prefix and hashsum, any date"""
        pattern = self.prefix + "-____________-" + hashsum
        self.pgcr.execute(
            """
            SELECT datname FROM pg_database
            WHERE datname like %s
            ORDER BY datname DESC  -- MRU first
            """,
            (pattern,),
        )
        r = self.pgcr.fetchone()
        if r:
            return r[0]
        else:
            return None

    def _touch(self, template_name, hashsum):
        # change the template date (MRU mechanism)
        assert template_name.endswith(hashsum)
        new_template_name = self._make_new_template_name(hashsum)
        if template_name != new_template_name:
            self._rename_db(template_name, new_template_name)

    def create(self, new_database, hashsum):
        """Create a new database from a cached template matching hashsum"""
        with self._lock():
            template_name = self._find_template(hashsum)
            if not template_name:
                return False
            else:
                self._create_db_from_template(new_database, template_name)
                self._touch(template_name, hashsum)
                return True

    def add(self, new_database, hashsum):
        """Create a new cached template"""
        with self._lock():
            template_name = self._find_template(hashsum)
            if template_name:
                self._touch(template_name, hashsum)
            else:
                new_template_name = self._make_new_template_name(hashsum)
                self._create_db_from_template(new_template_name, new_database)

    @property
    def size(self):
        with self._lock():
            pattern = self._make_pattern()
            self.pgcr.execute(
                """
                SELECT count(*) FROM pg_database
                WHERE datname like %s
            """,
                (pattern,),
            )
            return self.pgcr.fetchone()[0]

    def purge(self):
        with self._lock():
            pattern = self._make_pattern()
            self.pgcr.execute(
                """
                SELECT datname FROM pg_database
                WHERE datname like %s
            """,
                (pattern,),
            )
            for (datname,) in self.pgcr.fetchall():
                self._drop_db(datname)

    def trim_size(self, max_size):
        with self._lock():
            pattern = self._make_pattern()
            self.pgcr.execute(
                """
                SELECT datname FROM pg_database
                WHERE datname like %s
                ORDER BY datname DESC
                OFFSET %s
            """,
                (pattern, max_size),
            )
            for (datname,) in self.pgcr.fetchall():
                self._drop_db(datname)

    def trim_age(self, max_age):
        with self._lock():
            pattern = self._make_pattern()
            max_name = self._make_pattern(
                dt=datetime.utcnow() - max_age, hs=self.MAX_HASHSUM
            )
            self.pgcr.execute(
                """
                SELECT datname FROM pg_database
                WHERE datname like %s
                  AND datname <= %s
                ORDER BY datname DESC
            """,
                (pattern, max_name),
            )
            for (datname,) in self.pgcr.fetchall():
                self._drop_db(datname)

@click.command()
@click_odoo.env_options(
    default_log_level="warn",
    with_database=False,
    with_rollback=False,
    with_addons_path=True,
)
@click.option(
    "--new-database",
    "-n",
    required=False,
    help="Name of new database to create, possibly from cache. "
    "If absent, only the cache trimming operation is executed.",
)
@click.option(
    "--modules",
    "-m",
    default="base",
    show_default=True,
    help="Comma separated list of addons to install.",
)
@click.option(
    "--demo/--no-demo", default=True, show_default=True, help="Load Odoo demo data."
)
@click.option(
    "--cache/--no-cache",
    default=True,
    show_default=True,
    help="Use a cache of database templates with the exact "
    "same addons installed. Disabling this option "
    "also disables all other cache-related operations "
    "such as max-age or size. Note: when the cache is "
    "enabled, all attachments created during database "
    "initialization are stored in database instead "
    "of the default Odoo file store.",
)
@click.option(
    "--cache-prefix",
    default="cache",
    show_default=True,
    help="Prefix to use when naming cache template databases "
    "(max 8 characters). CAUTION: all databases named like "
    "{prefix}-____________-% will eventually be dropped "
    "by the cache control mechanism, so choose the "
    "prefix wisely.",
)
@click.option(
    "--cache-max-age",
    default=30,
    show_default=True,
    type=int,
    help="Drop cache templates that have not been used for "
    "more than N days. Use -1 to disable.",
)
@click.option(
    "--cache-max-size",
    default=5,
    show_default=True,
    type=int,
    help="Keep N most recently used cache templates. Use "
    "-1 to disable. Use 0 to empty cache.",
)
@click.option(
    "--unless-exists",
    is_flag=True,
    help="Don't report error if database already exists.",
)
def main(
    env,
    new_database,
    modules,
    demo,
    cache,
    cache_prefix,
    cache_max_age,
    cache_max_size,
    unless_exists,
):
    """Create an Odoo database with pre-installed modules.

    Almost like standard Odoo does with the -i option,
    except this script manages a cache of database templates with
    the exact same addons installed. This is particularly useful to
    save time when initializing test databases.

    Cached templates are identified by computing a sha1
    checksum of modules provided with the -m option, including their
    dependencies and corresponding auto_install modules.
    """
    if new_database:
        check_dbname(new_database)
    if unless_exists and db_exists(new_database):
        msg = "Database already exists: {}".format(new_database)
        click.echo(click.style(msg, fg="yellow"))
        return
    module_names = [m.strip() for m in modules.split(",")]
    if not cache:
        if new_database:
            odoo_createdb(new_database, demo, module_names, False)
        else:
            _logger.info(
                "Cache disabled and no new database name provided. " "Nothing to do."
            )
    else:
        with pg_connect() as pgcr:
            dbcache = DbCache(cache_prefix, pgcr)
            if new_database:
                hashsum = addons_hash(module_names, demo)
                if dbcache.create(new_database, hashsum):
                    _logger.info(
                        click.style(
                            "Found matching database template! âœ¨ ðŸ�° âœ¨",
                            fg="green",
                            bold=True,
                        )
                    )
                    refresh_module_list(new_database)
                else:
                    odoo_createdb(new_database, demo, module_names, True)
                    dbcache.add(new_database, hashsum)
            if cache_max_size >= 0:
                dbcache.trim_size(cache_max_size)
            if cache_max_age >= 0:
                dbcache.trim_age(timedelta(days=cache_max_age))

if __name__ == "__main__":  # pragma: no cover
    main()


# -*- coding: utf-8 -*-
"""
Created on 2018-11-29 14:14:33
Last Modified on 2018-11-29 14:14:33

Add refseq id list to homogenes list base on all_proteins.data file.

@Author: Ying Huang
"""
import os
import pandas as pd

def get_protein_refseq_lst_of_species(taxonomy_id, all_proteins_data):

    species_lst = all_proteins_data[
        all_proteins_data['taxonomy_id'] == taxonomy_id
    ]

    species_lst.columns = [
        '{}_{}'.format(n, taxonomy_id) for n in species_lst.columns
    ]

    return species_lst.iloc[:, [1,2]].copy()

def add_gene_ids(homo_db, homogenes_df, species_ids):
    """Add refseq id list to homogenes list base on all_proteins.data file.

    homo_db: <Path> database directory contained files 'homologene.data,
        taxid_taxname, all_proteins.data'. All of the file can be download
        from 'ftp://ftp.ncbi.nih.gov/pub/HomoloGene/current'.
    homogenes_df: <DataFrame> result of 'find_homogenes.find_homogenes(homo_db,
        species_ids)', which contains 'group_id', 'gene_name', 'protein_id' of
        homogenes.
    species_ids: <iter> a list of species ids."""

    homogenes_df = homogenes_df.copy()

    all_proteins_data_path = os.path.join(homo_db, 'all_proteins.data')
    all_proteins_data = pd.read_csv(all_proteins_data_path, header=None, sep='\t')[[0, 4, 5]]
    all_proteins_data.columns = ['taxonomy_id', 'protein_id', 'refseq_id']

    # merge refseq id to homogenes_df
    for sp_id in species_ids:
        tmp_protein_refseq_lst = get_protein_refseq_lst_of_species(
            sp_id, all_proteins_data
        )

        homogenes_df = pd.merge(
            homogenes_df, tmp_protein_refseq_lst,
            on=tmp_protein_refseq_lst.columns[0],
            how='outer',
        )

    # remove umatched rows
    na_mask = homogenes_df.apply(lambda x: x.isna().any(), axis=1)
    homogenes_df = homogenes_df[~(na_mask)]

    # sort columns by 'taxonomy_id'
    sorted_col = homogenes_df.columns.tolist()
    sorted_col.sort(
        key=lambda x: int(x.split('_')[-1])
    )

    return homogenes_df[sorted_col].copy()


import numpy as np
import requests
from bs4 import BeautifulSoup

def get_data(countrylabel):
    """take html source code from link and read information from it as string. Read dates and total cases
	return dictionary. Read corona data from http://www.worldometers.info/coronavirus/country/countrylabel.
	:param: countrylabel str"""
    country = dict({'countrylabel':countrylabel})       # return dictionary with data to this country

    link = "http://www.worldometers.info/coronavirus/country/%s/" % countrylabel  # read this website
    r = requests.get(link)                              # response from website
    soup = BeautifulSoup(r.content, 'html.parser')      # data structure representing a parsed HTML
    charts = soup.find_all("script",
                           attrs={'type': 'text/javascript',
                                  'src':'', 'class':'', 'data-cfasync':''})  # find all <script type='text/javascript' in html and store them to a list
    for i in range(len(charts)):
        if 'coronavirus-cases-linear' in str(charts[i]):                     # find the chart with data wanted
            rawdata = str(charts[i]).replace(" ", "")                        # html to string and omit whitspaces
            time = rawdata.split('xAxis:{\ncategories:[')[1].split("]}")[0]  # split string to time data
            time = np.array(time.replace("\"", "").split(','))               # make array of strings
            total = rawdata.split('TotalCoronavirusCases')[1].split('data:[')[1].split("]}")[0]  # total cases to time data
            total = np.array(total.split(','), dtype=int)
            country['time'] = time
            country['total'] = total

        elif 'graph-active-cases-total' in str(charts[i]):
            rawdata = str(charts[i]).replace(" ", "")                        # html to string and omit whitspaces
            active = rawdata.split('TotalCoronavirusCurrentlyInfected')[1].split('data:[')[1].split("]}")[0] # currently infected
            active = np.array(active.split(','), dtype=int)
            country['active'] = active
    return country

def get_data_berlin():
    """ Load from https://datawrapper.dwcdn.net/fIw01/56/ to get Covid19 data of Berlin. Source is Senatsverwaltung
    fuer Gesundheit und Pflege.
    :return dictionary
    time (str-array), total (int-array), inhosp (int-array), sever (int-array), death (int-array)"""
    berlin = dict({'countrylabel':'Berlin'})       # return dictionary with data to Berlin

    link = 'https://datawrapper.dwcdn.net/fIw01/56/'

    r = requests.get(link)                              # response from website
    soup = BeautifulSoup(r.content, 'html.parser')      # data structure representing a parsed HTML
    sourcecode = str(soup.contents)                        # get source code as string
    rawdata = sourcecode.split('gestorben\\\\n')[1].split('\\",\\"')[0]   # crop string to data
    string = rawdata.replace('\\\\t', ',').replace('\\\\n', ',')          # change utf8 delimiter to comma
    arraystr = np.array(string.split(','))                                # make string
    arraystr = np.reshape(arraystr, (len(arraystr)//5, 5))                # change to table (2darray) with 5 columns

    # extract columns in dictionary time=dates (str) rest is integer
    berlin['time'] = np.array([translate_time_format(date) for date in arraystr[:, 0]])  # dd.02.yyy -> Febdd etc
    berlin['total'] = np.array(arraystr[:, 1], dtype=int)
    berlin['inhosp'] = np.array(arraystr[:, 2], dtype=int)
    berlin['severe'] = np.array(arraystr[:, 3], dtype=int)
    berlin['death'] = np.array(arraystr[:, 4], dtype=int)
    berlin['active'] = np.zeros(len(berlin['time']))

    return berlin

def translate_time_format(ddmmyyy):
    """
    :param: ddmmyyyy (str) as 'dd.mm.yyyy'  with mm = 01,02,...,12
    :return date (str) as 'MMdd'        with MM=Jan,Feb,...,Dec
    """
    months = dict({'01': 'Jan', '02': 'Feb', '03': 'Mar', '04': 'Apr', '05': 'May', '06': 'Jun',
                   '07': 'Jul', '08': 'Aug', '09': 'Sep', '10': 'Oct', '11': 'Nov', '12': 'Dec'})
    dmy = ddmmyyy.split('.')
    return months[dmy[1]] + dmy[0]

if __name__ == '__main__':
    print(get_data('germany'))


#  File: GuessingGame.py

#  Description: A guessing game that is based on the binary search algorithm

#  Student Name: Keerat Baweja

#  Student UT EID: kkb792

#  Course Name: CS 303E

#  Unique Number: 50860

#  Date Created: 4/13/2016

#  Date Last Modified: 4/13/2016

def main():
  # Ask the user to start the game
  print("Think of a number between 1 and 100 inclusive.\nAnd I will guess what it is in 7 tries or less.")
  print("")
  answer = input("Are you ready? (y/n): ")
  print("")
  while answer != 'n' and answer != 'y':
    answer = input("Are you ready? (y/n): ")
    print("")
  if answer == 'n':
    print("Bye")
    return

  # Initialize variables
  lo = 0
  hi = 100
  mid = (lo + hi)//2
  response = '5'
  tries = 1

  # While loop to go through the guessing game
  while response != '0' and tries <= 7:
    print("Guess", tries, ": The number you thought was", mid)
    response = input("Enter 1 if my guess was high, -1 if low, and 0 if correct: ")
    print("")

    # While loop to check for correct response and adjust low based on response from user
    while response != '1' and response != '-1' and response != '0':
      print("Guess", tries, ": The number you thought was", mid)
      response = input("Enter 1 if my guess was high, -1 if low, and 0 if correct: ")
      print("")

    if response == '1':
      hi = mid
    elif response == '-1':
      lo = mid
    elif response == '0':
      print("Thank you for playing the Guessing Game.")
      return
    mid = (lo + hi)//2
    tries += 1

  # Print error message
  print("Either you guessed a number out of range or you had an incorrect entry.")
  return

main()


from __future__ import print_function

import pygion
from pygion import task, Region, Reduce
import numpy

@task(privileges=[Reduce('+')])
def inc(R, step):
    numpy.add(R.x, step, out=R.x)
    numpy.add(R.y, step, out=R.y)

@task
def main():
    R = Region([4, 4], {'x': pygion.float64, 'y': pygion.int32})
    pygion.fill(R, 'x', 1.25)
    pygion.fill(R, 'y', 2)
    inc(R, 20)
    print(R.x)
    print(R.y)
    assert R.x[0, 0] == 21.25
    assert R.y[0, 0] == 22

if __name__ == '__main__':
    main()


from abc import ABC, abstractmethod
import os

from FileHandler import CSVHandler, parse_types
import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score
from collections import defaultdict

class QueryHandlerInterface(ABC):
    """Интерфейс обработчика запросов"""

    @abstractmethod
    def get_sources(self, d_type, folder):
        """Функция должна возвращать список файлов, которые можно прочитать."""
        pass

    @abstractmethod
    def get_metrics(self, d_type, source, target=''):
        """Функция должна возвращать список метрик, котороые возможно вернуть."""
        pass

    @abstractmethod
    def get_data(self, request):
        """Функция должна возвращать данные в необходиомом формате."""
        pass

def dataframe_to_response(target, df):
    """Возвращает список временных рядов. Временной ряд:
    {
    "target":"pps in",
    "datapoints":[
      [622,1450754160000],
      [365,1450754220000]
    ]
  }"""
    response = []
    if df.empty:
        return response

    # if freq is not None:
    #    orig_tz = df.index.tz
    #    df = df.tz_convert('UTC').resample(rule=freq, label='right', closed='right', how='mean').tz_convert(orig_tz)

    if isinstance(df, pd.Series):
        response.append(_series_to_response(df))
    elif isinstance(df, pd.DataFrame):
        for col in df.columns:
            response.append(_series_to_response(df[col]))
    else:
        abort(404, Exception('Received object is not a dataframe or series.'))
    return response

def _series_to_response(df):
    sorted_df = df.dropna().sort_index()
    timestamps = sorted_df.index
    values = sorted_df.values.tolist()
    return {'target': '%s' % df.name,
            'datapoints': list(zip(values, timestamps))}

def dataframe_to_json_table(df):
    """Возвращает датафрейм в виде таблицы. Таблица:
    {
    "columns":[
      {"text":"Time","type":"time"},
      {"text":"Country","type":"string"},
      {"text":"Number","type":"number"}
    ],
    "rows":[
      [1234567,"SE",123],
      [1234567,"DE",231],
      [1234567,"US",321]
    ],
    "type":"table"
  }"""
    response = []
    if df.empty:
        return response
    if isinstance(df, pd.DataFrame):
        response.append({'type': 'table',
                         'columns': df.columns.map(lambda col: {"text": col}).tolist(),
                         'rows': df.where(pd.notnull(df), None).values.tolist()})
    else:
        abort(404, Exception('Received object is not a dataframe.'))
    return response

def parse_optional(target):
    res = defaultdict(lambda: None)
    if target.get('data', None) is not None:
        res['index_col'] = target['data'].get('index_col', None)
        res['log_scale'] = target['data'].get('log_scale', None)
    return res

class QueryHandler(QueryHandlerInterface):
    """Обработчик запросов для курсовой :3"""
    special_metrics = ['agreement_count', 'default_rate', #'client_count',
                            'avg_PD', 'gini']

    def __init__(self, path, filetypes: list):
        """filetypes - список обрабатываемых типов файлов"""
        self.path = path
        self.filetypes = filetypes
        self.type_class_dict = parse_types(filetypes)  # словарь {тип: класс_обработчика}
        self.file_class_dict = {}  # словарь {файл: класс_обработчика}

    def get_sources(self, d_type: str, folder: str) -> list:
        """Возвращает список ресурсов, а заодно заполняет словарь file_class_dict"""
        results = []
        for filename in os.listdir(os.path.join(self.path, folder)):
            for filetype in self.filetypes:
                if filetype in filename:
                    results.append(filename)
                    self.file_class_dict[filename] = self.type_class_dict[filetype](filename, os.path.join(self.path, folder))
                    break
        return results

    def get_metrics(self, d_type: str, source: str, target: str = '') -> list:
        columns = self.file_class_dict[source].get_columns(target)
        all_metrics = list(map(lambda x: 'special:'+x, self.special_metrics)) + columns
        return all_metrics

    def special_metric_handle(self, metric, source, options: dict):
        if metric == 'agreement_count':
            data = self.file_class_dict[source].get_data_by_column('id', options)
            data.sort_index()
            res = data.groupby(data.index).count()
            res.name = metric
            return res
        # elif metric == 'client_count':
        elif metric == 'default_rate':
            data_def = self.file_class_dict[source].get_data_by_column('default_12m', options)
            data_non_def = self.file_class_dict[source].get_data_by_column('cur_default', options)
            res = data_def.groupby(data_def.index).sum() / data_non_def.groupby(data_non_def.index).sum()
            res.name = metric
            return res
        elif metric == 'avg_PD':
            data = self.file_class_dict[source].get_data_by_column('pd', options)
            res = data.groupby(data.index).mean()
            res.name = metric
            return res
        elif metric == 'gini':
            data = self.file_class_dict[source].get_data_by_column(['pd', 'default_12m'], options)
            res = 2 * data.groupby(data.index).apply(lambda group: roc_auc_score(group.default_12m, group.pd)) - 1
            res.name = metric
            return res

    def get_data(self, request) -> list:
        results = []
        for target in request['targets']:
            source = target['source']
            if source == "":
                continue
            metric = target['target']
            if ':' in metric:
                metric = metric.split(':')[1]
            options = parse_optional(target)
            if metric in self.special_metrics:
                data = self.special_metric_handle(metric, source, options)
            else:
                data = self.file_class_dict[source].get_data_by_column(metric, options)
            if target.get('type', 'ts') == 'table':
                results.extend(dataframe_to_json_table(data))
            else:
                results.extend(dataframe_to_response(target, data))
        return results


from django.conf.urls import patterns, include, url

from piston.resource import Resource
from piston.authentication import HttpBasicAuthentication

from handler.todoapi import TodosHandler

# Uncomment the next two lines to enable the admin:
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'todolist.handler.views.index', name='index'),
    url(r'^register$', 'todolist.handler.views.register', name='register'),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
    (r'^accounts/logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
)

#these are the calls which need authentication of the user
auth = HttpBasicAuthentication(realm="API")
todoresource = Resource(TodosHandler, authentication=auth)
urlpatterns += patterns('',
   url(r'^todo/list$', todoresource),
   url(r'^todo/task/(?P<todoitem_id>[^/]+)?$', todoresource),
)


#!/usr/bin/env python3
import subprocess
import re
import sys
import os

class bcolors:
    pink='\033[95m'
    cyan='\033[36m'
    ENDC = '\033[0m'
    red='\033[31m'
    green='\033[32m'
    orange='\033[33m'

def main():
    print(f'''{bcolors.cyan}
    .__       .__  __
    |__| ____ |__|/  |_            ______ ____ _____    ____
    |  |/    \|  \   __\  ______  /  ___// ___\\__  \  /    \
    |  |   |  \  ||  |   /_____/  \___ \\\  \___ / __ \|   |  \\
    |__|___|  /__||__|           /____  >\___  >____  /___|  /
            \/                        \/     \/     \/     \/

                                                {bcolors.green}by init__6{bcolors.ENDC}

            ''')
    host = sys.argv[1]
    nmap = subprocess.check_output(('nmap %s -p- -T4 --min-rate=9999 | grep open ' % host ), shell=True)
    nmap= str(nmap)
    # nmap = '''
    # 53/tcp  open     domain
    # 80/tcp  open     http
    # 443/tcp open     https
    # '''
    nmap = nmap.replace('b\'','\\n')
    nmap = nmap.replace('tcp','')
    nmap = nmap.replace('open','\n')

    pattern = re.compile(r'(?<=\\n)(.*)(?=\/)', re.IGNORECASE)
    result = re.findall(pattern, nmap)
    final = ",".join(result)
    print('')
    print(f"{bcolors.cyan}Open Ports: {bcolors.pink}",final,f"{bcolors.ENDC}")
    print('\n\n')
    print(f"{bcolors.red}Running service enumeration scans on {bcolors.pink}%s {bcolors.ENDC}" % host)
    print('')
    try:
        file = sys.argv[2]
        os.system('nmap -sV -sC -p' + final + ' ' + host + ' | tee ' + file )
    except:
        os.system('nmap -sV -sC -p' + final + ' ' + host)

try:
    if sys.argv[1] == '-h':

        print('''
Usage:
    %s $ip $file_to_output
              ''' % sys.argv[0])
    else:
        main()
except KeyboardInterrupt:
    exit()


import firebase_admin
from firebase_admin import credentials, firestore
import time
import threading
import urllib.request
from os import system

print('Initializing Firestore connection...')
# Credentials and Firebase App initialization. Always required
firCredentials = credentials.Certificate('')
firApp = firebase_admin.initialize_app (firCredentials)

# Get access to Firestore
db = firestore.client()
print('Connection initialized')

# Create an Event for notifying main thread.
callback_done = threading.Event()

# Create a callback on_snapshot function to capture changes
def on_snapshot1(col_snapshot, changes, read_time):

    print(u'Callback received query snapshot.')

    if not col_snapshot:
        return

    for change in changes:
        if change.type.name == 'ADDED':
            # print(f'uid : {change.document.id}')
            uid = f'{change.document.id}'
            #print(uid)

            user_ref = db.collection(u'result').document(uid)
            doc = user_ref.get()

            if doc.exists:
                # print(f'Document data: {doc.to_dict()}')
                url_front = doc.get('imageurl_front')
            else:
                print(u'No such document!')

            urllib.request.urlretrieve(url_front, "front.png")

            system("python DataWriteFront.py")

    callback_done.set()

def on_snapshot2(col_snapshot, changes, read_time):

    print(u'Callback received query snapshot.')

    if not col_snapshot:
        return

    for change in changes:
        if change.type.name == 'ADDED':
            # print(f'uid : {change.document.id}')
            uid = f'{change.document.id}'
            #print(uid)

            user_ref = db.collection(u'result').document(uid)
            doc = user_ref.get()

            if doc.exists:
                # print(f'Document data: {doc.to_dict()}')
                url_side = doc.get('imageurl_side')
            else:
                print(u'No such document!')

            urllib.request.urlretrieve(url_side, "side.png")

            system("python DataWriteSide.py")

    callback_done.set()

while True:
    print('processing...')
    col_query1 = db.collection(u'result').where(u'flag_front', u'==', 1)
    col_query2 = db.collection(u'result').where(u'flag_side', u'==', 1)

    query_watch = col_query1.on_snapshot(on_snapshot1)
    query_watch = col_query2.on_snapshot(on_snapshot2)

    time.sleep(4)


"""Day 7: The Treachery of Whales"""

from typing import List, Dict
from util import file_input, text_input, ints

example_input = "16,1,2,0,4,2,7,1,2,14"

def min_consumption(positions: List[int], fixed_burn_rate: bool = True) -> int:
    num_positions = max(positions) + 1
    fuel_required = [0] * num_positions
    for from_pos in positions:
        for to_pos in range(num_positions):
            fuel_spent = abs(to_pos - from_pos)
            if not fixed_burn_rate:
                fuel_spent = int((fuel_spent * fuel_spent + fuel_spent) / 2)
            fuel_required[to_pos] += fuel_spent
    return min(fuel_required)

def day7_1(nums: List[int]) -> int:
    return min_consumption(nums)

assert day7_1(text_input(example_input, ints)[0]) == 37
assert day7_1(file_input(7, ints)[0]) == 345035

def day7_2(nums: List[int]) -> int:
    return min_consumption(nums, fixed_burn_rate=False)

assert day7_2(text_input(example_input, ints)[0]) == 168
assert day7_2(file_input(7, ints)[0]) == 97038163


from banksim.agent.loan import Loan
from banksim.intervention.base import Intervention, InterventionInstance
import numpy as np

class F2LoanIntervention(Intervention):
    def __init__(self, default_loans_range=range(0, 10), bank_id_filter=[1], step=239):
        self.bank_id_filter = bank_id_filter
        self.default_loans_range = default_loans_range
        self.step = step

    def get_instances(self):
        return [F2LoanInterventionInstance(i, self.bank_id_filter, self.step) for i in self.default_loans_range]

class F2LoanInterventionInstance(InterventionInstance):
    def __init__(self, default_loans, bank_id_filter=[1], step=239):
        self.bank_id_filter = bank_id_filter
        self.default_loans = default_loans
        self.step = step

    def intervention(self, schedule, **kwargs):
        bank = kwargs['bank']

        if self.step != schedule.steps:
            return

        # id_filter worked and bankid not in it
        if self.bank_id_filter is not None \
                and len(self.bank_id_filter) > 0 \
                and bank.unique_id not in self.bank_id_filter:
            return

        loans_with_bank = [x for x in schedule.agents if isinstance(x, Loan) and x.pos == bank.pos and
                           x.loan_approved and x.loan_solvent]
        for loan in np.random.choice(loans_with_bank, min(len(loans_with_bank), self.default_loans)):
            loan.loan_solvent = False
            loan.rwamount = loan.rweight * loan.amount

    def id(self):
        return f'f2l{self.default_loans}'


from django.shortcuts import render
from django.views.generic import TemplateView

from apps.accounts.mixins import LoggedInRequiredMixin
from .models import Painter
from django.views.generic.edit import CreateView
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseServerError
from .forms import UploadFileForm
from django.views.generic.edit import FormView
from django.shortcuts import render, redirect
from django.contrib.messages import get_messages
from django.contrib import messages
import json

# Create your views here.
class PainterCreate(LoggedInRequiredMixin, TemplateView):
    # template_name = 'painter.html'
    template_name = 'editor_painter.html'

    def get_context_data(self, **kwargs):
        context = super(PainterCreate, self).get_context_data(**kwargs)
        projects = Painter.get_all(self.request)
        context['projects'] = projects

        storage = get_messages(self.request)
        kv = None
        for message in storage:
            kv = message
            break
        # parse
        if kv:
            kv = kv.message
            kv_dict = json.loads(kv)
            context.update(kv_dict)
        return context

class CreateProject(LoggedInRequiredMixin, CreateView):

    def post(self, request, *args, **kwargs):
        name = request.POST.get('name')
        if not name:
            return HttpResponseBadRequest()
        try:
            user = request.user
            p = Painter(name=name, user=user)
            p.save()
        except:
            import traceback
            return HttpResponseServerError(traceback.format_exc())

        return HttpResponse(content='Created project `{0}`.'.format(name))

class UploadFile(LoggedInRequiredMixin, FormView):
    template_name = 'editor_painter.html'

    def post(self, request, *args, **kwargs):
        content = request.FILES['file']
        filename = request.FILES['file'].name
        with open(filename, 'wb') as f:
            for chunk in content.chunks():
                f.write(chunk)
        context = {
            'upload_success': True,
            'hide_upload_dialog': True,
        }
        messages.add_message(request, messages.INFO, json.dumps(context))
        return redirect('/painters/painter/')


from django.urls import path

from openapi_documentor.openapi.views import (
    api_detail_view,
    api_list_view,
    api_tagged_view,
)

app_name = "openapi"
urlpatterns = [
    path("", view=api_list_view, name="list"),
    path("tagged/<str:tag>", view=api_tagged_view, name="tag"),
    path("<slug:pk>/", view=api_detail_view, name="detail"),
]