from behave import *
from selenium import webdriver
from configuration.config import TestData
from pages.LoginPage import LoginPage
from pages.InventoryPage import InventoryPage
import traceback

@given(u'The Saucelabs portal is opened')
def launch_browser(context):
    context.driver = webdriver.Chrome(executable_path=TestData.CHROME_EXECUTABLE_PATH)

    try:
        context.driver.get(TestData.URL)
        context.loginPage = LoginPage(context.driver)
        context.inventoryPage = InventoryPage(context.driver)
    except:
        context.driver.close()
        assert False,"Test is failed in open login page section"

@given(u'Provide the credentials')
def enter_login_creds(context):
    try:
        context.loginPage.enter_login_credentials(TestData.USERNAME, TestData.PASSWORD)
    except Exception:
        traceback.print_exc()
        context.driver.close()
        assert False, "Test is failed in enter login credentials"

@when(u'Click on the Login button')
def enter_login(context):
    try:
        context.loginPage.enter_login()
    except:
        context.driver.close()
        assert False, "Test is failed in enter login"

@then(u'Login is successful and inventory is opened')
def validate_dashboard_page(context):
    try:
        context.inventoryPage.validateTitle()
    except:
        context.driver.close()
        assert False, "Test is failed in validate invetory page title"

@then(u'Add to cart two first elements')
def fill_cart(context):
    try:
        context.inventoryPage.addItemsToCart()
        assert context.inventoryPage.validateCartQuantity()
    except:
        context.driver.close()
        assert False, "Test is failed in adding items to the cart"

@then(u'Go to cart and checkout added items')
def checkout_cart(context):
    try:
        context.inventoryPage.goToCart()
        assert context.inventoryPage.validateCartUrl()
        context.inventoryPage.checkoutCart()
    except:
        context.driver.close()
        assert False, "Test is failed checking out items in the cart"

@then(u'Insert payment information')
def checkout_step_one(context):
    try:
        context.inventoryPage.fillStepOneInfo('Jorge', 'Guevara', '15036')
    except:
        context.driver.close()
        assert False, "Test is failed checking filling step one"

@when(u'Finish the checkout')
def finish_checkout(context):
    try:
        context.inventoryPage.finishCheckout()
    except:
        context.driver.close()
        assert False, "Test is failed almost at the end"

@then(u'You\'ll get a thanks')
def thanks(context):
    assert context.inventoryPage.validateMessage()

@then(u'Click hamburguer menu and click log out link')
def do_logout(context):
    context.loginPage.log_out()

@then(u'You\'ll be out of the system')
def step_impl(context):
    pass


from keras.layers import Conv2D, InputLayer, UpSampling2D, MaxPooling2D, AveragePooling2D,\
    BatchNormalization, Resizing, Reshape, Concatenate, Input
from keras.activations import relu, tanh
from keras.applications import InceptionResNetV2, MobileNetV2
from keras.models import Sequential, Model, load_model
from keras.optimizers import Adam
from keras.losses import mse
import tensorflow as tf

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage.color import rgb2lab, lab2rgb
from load_dataset import load_data, preprocess_image_old

def get_output_old(x):
    output = model.predict(x)[0] * 128
    ab = np.clip(output, -128, 127)
    size = 0  # !!!
    # print(ab.shape, ab)
    res = np.zeros(size, dtype=np.float32)
    res[:, :, 0] = np.squeeze(x)
    res[:, :, 1:] = ab
    res = lab2rgb(res)
    # print(res.shape, res)
    return res

def get_output_lab(img_l):
    output = model.predict(np.array([img_l]), verbose=0)[0] * 128
    ab = np.clip(output, -128, 127)
    res = np.zeros((*SIZE, 3), dtype=np.float32)
    res[:, :, 0] = img_l
    res[:, :, 1:] = ab
    return res

def imshow_lab(image, title='', show=False):
    image[:, :, 0] = np.clip(image[:, :, 0], 0, 100)
    image[:, :, 1:] = np.clip(image[:, :, 1:]*128, -128, 127)
    plt.imshow(lab2rgb(image))
    if title:
        plt.title(title)
    if show:
        plt.show()

def compare_images(image, res, save_path=None, gray=0):
    plt.figure(figsize=(9, 5))
    plt.subplot(1, 2+gray, 1)
    imshow_lab(image, title='Original')
    plt.axis('off')
    plt.subplot(1, 2+gray, 2)
    imshow_lab(res, title='Result')
    plt.axis('off')
    if gray == 1:
        plt.subplot(1, 3, 3)
        plt.imshow(np.mean(lab2rgb(image), axis=2), cmap='gray')
        plt.axis('off')
        plt.title('Gray')
    plt.show()

    if save_path:
        img2save = Image.fromarray(np.array(lab2rgb(res) * 255).astype(np.uint8))
        img2save.save(save_path)

def test_random(arr, count=5):
    for i in range(count):
        n = np.random.randint(0, len(arr))
        result = get_output_lab(arr[n, :, :, 0])
        compare_images(arr[n], result, gray=1)

SIZE = (224, 224)

inception_res_net_model = MobileNetV2(include_top=True)
inception_res_net_model.trainable = False
# inception_res_net_model.summary()

# train, test = load_data(train_range=(0, 4000), test_range=(20000, 20500))
# train_X, train_Y = train[:, :, :, 0], train[:, :, :, 1:]
# test_X, test_Y = test[:, :, :, 0], test[:, :, :, 1:]

# imshow_lab(train[10], show=True)
# imshow_lab(train[100], show=True)
# imshow_lab(train[1000], show=True)
# print(train[100], np.max(train[100, :, :, 1:]), np.min(train[100, :, :, 1:]), train[100].shape)
# print(lab2rgb(train[100]))

interpolation = 'bilinear'  # UpSampling2D(interpolation)
# 'nearest'-, 'bicubic'-, 'lanczos5'-, 'bilinear'++-, 'gaussian'++-, 'mitchellcubic'++-

model1 = Sequential([
    InputLayer(input_shape=(None, None, 1)),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same', strides=2),
    # AveragePooling2D(pool_size=(2, 2)),  # or strides=2
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same', strides=2),
    # AveragePooling2D(pool_size=(2, 2)),  # or strides=2
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same', strides=2),
    # AveragePooling2D(pool_size=(2, 2)),  # or strides=2
    Conv2D(512, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same'),
    UpSampling2D(size=(2, 2), interpolation=interpolation),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same'),
    UpSampling2D(size=(2, 2), interpolation=interpolation),
    Conv2D(32, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(2, kernel_size=(3, 3), activation=tanh, padding='same'),
    UpSampling2D(size=(2, 2), interpolation=interpolation),
])

model2 = Sequential([
    InputLayer(input_shape=(*SIZE, 1)),
    BatchNormalization(),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same'),
    AveragePooling2D(pool_size=(2, 2)),  # or , strides=2
    BatchNormalization(),
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    AveragePooling2D(pool_size=(2, 2)),  # or strides=2
    BatchNormalization(),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(512, kernel_size=(3, 3), activation=relu, padding='same'),
    BatchNormalization(),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same'),
    UpSampling2D(size=(2, 2), interpolation=interpolation),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same'),
    UpSampling2D(size=(2, 2), interpolation=interpolation),
    Conv2D(32, kernel_size=(3, 3), activation=relu, padding='same'),
    Conv2D(2, kernel_size=(3, 3), activation=tanh, padding='same')])

# model3
input_layer = InputLayer(input_shape=(*SIZE, 1))
x = Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same')(input_layer)
x = Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = AveragePooling2D()(x)
x = Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = AveragePooling2D()(x)
x = Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(512, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(512, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same')(x)

res_net_output = inception_res_net_model.predict(tf.image.grayscale_to_rgb(tf.multiply(input_layer, 2.55)))
res_net_output = Reshape(-1, 1, 1, 1000)(res_net_output)
res_net_output = Resizing(224/4, 224/4)(res_net_output)
x = tf.concat((x, res_net_output), axis=-1)

x = Conv2D(256, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(128, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = UpSampling2D(size=(2, 2), interpolation=interpolation)(x)
x = Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = Conv2D(64, kernel_size=(3, 3), activation=relu, padding='same')(x)
x = UpSampling2D(size=(2, 2), interpolation=interpolation)(x)
x = Conv2D(32, kernel_size=(3, 3), activation=relu, padding='same')(x)
output_layer = Conv2D(2, kernel_size=(3, 3), activation=tanh, padding='same')(x)

model3 = Model(inputs=input_layer, outputs=output_layer)

model = model3
model.compile(optimizer=Adam(), loss=mse, metrics=['accuracy'])
model.summary()

# self_model.fit(x=train_X, y=train_Y, validation_data=(test_X, test_Y), BATCH_SIZE=8, epochs=3, verbose=1)
# self_model.save('models/color_model2_2.h5', save_format='h5')
# self_model = load_model('models/color_model2_2.h5')

# test_random(train, 2)
# test_random(test, 3)

# result = get_output_old(X)
# compare_images(image=img_name, res=result, save_path='results/res1.jpg', gray=1)


"""

@Time: 2021/1/20 17:59
@Author:
@File: main.py
"""
from pyfuseki import AsyncFuseki
import pyfuseki
from pyfuseki.utils import RdfUtils
from rdflib import Graph, Literal, XSD
from demo.RdfPrefix import RdfPrefix as rp
from demo.properties import ObjectProperty as op, DataProperty as dp
import asyncio

async def insert_test():
    pyfuseki.register.register_common_prefix("http://www.yubin.com/kg/")
    fuseki = AsyncFuseki('http://localhost:3030', 'pyfuseki_db')
    g = Graph()

    # RdfUtils.bind_prefixes_to_graph(self.g, [rp.BrandProject, rp.Firm])  # 绑定前缀
    # 假设获取的数据为rev_data
    rev_data = {
        'band_project': '腾讯',
        '所属企业': '深圳市腾讯计算机系统有限公司',
        '成立日期': '1998-11-11',
        '英文名称': 'QQ'
    }
    # 将rev_data转化成RDF三元组并加入graph中
    tencent = rp.BrandProject.val('腾讯')
    RdfUtils.add_dict_to_graph(g, tencent, {
        op.subordinateTo.value: rp.Firm.val('深圳市腾讯计算机系统有限公司'),
        dp.createTime.value: Literal(rev_data['成立日期'], datatype=XSD.date),
        dp.enName.value: Literal(rev_data['英文名称'], datatype=XSD.string)
    })
    print(g)
    # 将graph插入
    await fuseki.insert_graph(g)

if __name__ == '__main__':
    asyncio.run(insert_test())


from __future__ import (absolute_import, division, print_function, unicode_literals)
from builtins import (ascii, bytes, chr, dict, filter, hex, input, int, map, next, oct, open, pow, range, round, str, super, zip)

import os
import unittest

import json

import foostache

class FoostacheTestCase(unittest.TestCase):

    def setUp(self):
        here = os.path.abspath(os.path.dirname(__file__))
        with open(os.path.join(here, "language", "examples", "sample1.fs"), encoding="utf_8") as f:
            self.sample1fs = f.read()
        with open(os.path.join(here, "language", "examples", "sample1.json"), "rb") as f:
            self.sample1json = json.load(f)
        with open(os.path.join(here, "language", "examples", "sample1.output"), encoding="utf_8") as f:
            self.sample1output = f.read()
        self.maxDiff = None

    def test_sample1(self):
        template = foostache.Template(self.sample1fs)
        output = template.render(self.sample1json)
        self.assertEqual(self.sample1output, output)

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


import scipy.io
import pyproj
import numpy as np
import pickle
import matplotlib.pyplot as plt
from tqdm import tqdm

def AMPS_wind(polygons,fname,icemask):
    mat = scipy.io.loadmat(fname)
    projection = pyproj.Proj("epsg:3031")
    lons,lats = np.meshgrid(mat["ERA_lon"],mat["ERA_lat"])
    x,y = projection.transform(lons,lats)
    zonal_winds = mat["zonal_winds_AMPS"]
    zonal_winds = np.nanmean(zonal_winds[:,:,:,:2],axis=3)
    #zonal_winds = zonal_winds[:,:,[10,11,0,1],1]#np.nanmean(zonal_winds,axis=3)
    #zonal_winds = zonal_winds[:,:,[10,11,0,1],1]#np.nanmean(zonal_winds,axis=3)
    zonal_winds = np.nanmean(zonal_winds,axis=2)
    merid_winds = mat["merid_winds_AMPS"]
    merid_winds = merid_winds[:,:,[6,7,8,9],2]#np.nanmean(merid_winds,axis=3)
    merid_winds = np.nanmean(merid_winds,axis=2)
    winds_by_shelf = {}
    for k in tqdm(polygons.keys()):
        centroid = list(polygons[k][0].centroid.coords)[0]
        dist = np.sqrt((x- centroid[0])**2 + (y- centroid[1])**2)
        radius=250*10**3
        z = np.nanmean(zonal_winds[dist<radius])
        m = np.nanmean(merid_winds[dist<radius])
        a = np.arctan2(m,z)
        winds_by_shelf[k] = z#np.degrees(a)#np.nanmean(np.degrees(a))
    return winds_by_shelf


from flask import Flask
import datetime

app = Flask(__name__)

@app.route('/get_time/now')
def get_time():
    time = datetime.datetime.now()
    return f"Точное время: {time}"

if __name__ == "__main__":
    app.run(port=5555, debug=True)


from pathlib import Path

import dash_core_components as dcc
import dash_html_components as html

from ...api_doc import ApiDoc
from ...helpers import (
    ExampleContainer,
    HighlightedSource,
    load_source_with_environment,
)
from ...metadata import get_component_metadata
from .radio_check_inline import inline_inputs
from .size import inputs as input_size
from .text_label import text_input as input_text_label
from .textarea import textareas as input_textarea
from .validation import inputs as input_validation

HERE = Path(__file__).parent

input_simple_source = (HERE / "simple.py").read_text()
input_text_label_source = (HERE / "text_label.py").read_text()
input_size_source = (HERE / "size.py").read_text()
input_validation_source = (HERE / "validation.py").read_text()
input_radio_check_source = (HERE / "radio_check.py").read_text()
input_textarea_source = (HERE / "textarea.py").read_text()
input_radio_check_inline_source = (HERE / "radio_check_inline.py").read_text()
input_radio_check_standalone_source = (
    HERE / "radio_check_standalone.py"
).read_text()

def get_content(app):
    return [
        html.H2("Input components"),
        html.P(
            dcc.Markdown(
                "*dash-bootstrap-components* has its own versions of some of "
                "the input components available in *dash-core-components*. "
                "They have been designed to share the same interface as the "
                "corresponding components in *dash-core-components* for "
                "familiarity, but have a few additional Bootstrap specific "
                "features."
            )
        ),
        html.H4("Input"),
        html.P(
            dcc.Markdown(
                "The input component allows for text or numeric input, its "
                "basic usage is the same as `dcc.Input`, but Bootstrap styles "
                "will be applied for you."
            )
        ),
        ExampleContainer(
            load_source_with_environment(
                input_simple_source, "text_input", {"app": app}
            )
        ),
        HighlightedSource(input_simple_source),
        html.H4("Labels and Text"),
        html.P(
            [
                dcc.Markdown(
                    "Use the `FormGroup` component along with `Label` and "
                    "`FormText` to control the layout of your `Input` "
                    "components."
                ),
                "See the ",
                dcc.Link("documentation for forms", href="/l/components/form"),
                " for more details.",
            ]
        ),
        ExampleContainer(input_text_label),
        HighlightedSource(input_text_label_source),
        html.H4("Input size"),
        html.P(
            dcc.Markdown(
                "You can control the size of the `Input` using the `bs_size` "
                "keyword argument. It accepts the values `sm`, `md` or `lg` "
                "for small, medium or large sizes respectively."
            )
        ),
        ExampleContainer(input_size),
        HighlightedSource(input_size_source),
        html.H4("Input validation"),
        html.P(
            dcc.Markdown(
                "Add valid / invalid styles to your `Input` components using "
                "the `valid` and `invalid` keyword arguments."
            )
        ),
        ExampleContainer(input_validation),
        HighlightedSource(input_validation_source),
        html.H4("Textarea"),
        html.P(
            dcc.Markdown(
                "The `Textarea` component works like the "
                "*dash-core-components* analogue, but accepts the additional "
                "arguments `valid`, `invalid`, and `bs_size` much like "
                "`Input`."
            )
        ),
        ExampleContainer(input_textarea),
        HighlightedSource(input_textarea_source),
        html.H4("RadioItems and Checklist"),
        html.P(
            dcc.Markdown(
                "`RadioItems` and `Checklist` components also work like "
                "*dash-core-components* but again with Bootstrap styles "
                "added. In addition the `inline` keyword can be used to "
                "easily make inline checklists or radioitems. Use these "
                "components with `FormGroup` for automatic spacing and "
                "padding."
            )
        ),
        ExampleContainer(
            load_source_with_environment(
                input_radio_check_source, "inputs", {"app": app}
            )
        ),
        HighlightedSource(input_radio_check_source),
        html.P(
            dcc.Markdown(
                "Use the `inline` keyword to make the radioitems or "
                "checklists fit next to each other on a line."
            )
        ),
        ExampleContainer(inline_inputs),
        HighlightedSource(input_radio_check_inline_source),
        html.P(
            dcc.Markdown(
                "If you need more granular control over checkboxes "
                "and radio buttons, you can also create standalone "
                "components. Bind callbacks to the `checked` keyword "
                "to react to changes in the input state. To attach "
                "a label, create a FormGroup with `check=True` and "
                "use the label's `html_for` keyword to bind it to "
                "the checkbox."
            )
        ),
        ExampleContainer(
            load_source_with_environment(
                input_radio_check_standalone_source,
                "standalone_radio_check",
                {"app": app},
            )
        ),
        HighlightedSource(input_radio_check_standalone_source),
        ApiDoc(
            get_component_metadata("src/components/input/Input.js"),
            component_name="Input",
        ),
        ApiDoc(
            get_component_metadata("src/components/input/Textarea.js"),
            component_name="Textarea",
        ),
        ApiDoc(
            get_component_metadata("src/components/input/RadioItems.js"),
            component_name="RadioItems",
        ),
        ApiDoc(
            get_component_metadata("src/components/input/Checklist.js"),
            component_name="Checklist",
        ),
        ApiDoc(
            get_component_metadata("src/components/input/Checkbox.js"),
            component_name="Checkbox",
        ),
        ApiDoc(
            get_component_metadata("src/components/input/RadioButton.js"),
            component_name="RadioButton",
        ),
    ]


from typing import List, Tuple

from django.db.models import QuerySet
from thefuzz import fuzz
from trama.models import Obra

class Scorer:
    def score(self, query_result: QuerySet, text: str) ->\
            List[Tuple[Obra, float]]:
        scoring_fun = fuzz.token_set_ratio
        obras: List[Obra] = query_result.all()

        scored_obras = {}
        for obra in obras:
            trabajos = [
                x.nombre_trabajo for x in
                obra.trabajo_representado.all()]
            score = max(
                scoring_fun(obra.nombre_obra, text),
                scoring_fun(obra.descripcion, text),
                scoring_fun(obra.estilo.nombre_estilo, text),
            )

            score_trabajos = max([scoring_fun(x, text)
                                  for x in trabajos])

            scored_obras[obra] = max(score, score_trabajos)
        return [(x, y) for (x, y) in
                sorted(scored_obras.items(), key=lambda x: x[1],
                       reverse=True)]


import re
import textblob
from textblob import TextBlob
import csv

def get_tweet_sentiment(tweet):
    analysis = TextBlob(tweet)
    if analysis.sentiment.polarity > 0:
        return 'P'
    elif analysis.sentiment.polarity == 0:
        return 'N'
    else:
        return 'Z'

def get_tweets():
    tweets = []
    fetched_tweets = []
    file = open('MY_results_full_data','w+')
    with open('population_preprocessed_FULL.csv', encoding = 'utf8') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        for row in csv_reader:
            if line_count != 0:
                fetched_tweets.append(row[0])
            line_count += 1
        for tweet in fetched_tweets:
            sentiment = get_tweet_sentiment(tweet)
            file.write(sentiment)
    file.close()

get_tweets()


import argparse
import logging
import sys

from lib.event_manager import Atman
from lib.globals import LOGGER_TAG

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Alien')
    parser.add_argument("--verbose", "-v", action="store_true")
    parser.add_argument("--show_logs", "-s", action="store_false")
    parser.add_argument("--log_file", "-lf", default=None, help="Set path to save log output")
    args = parser.parse_args()

    logger = logging.getLogger(LOGGER_TAG)

    if args.log_file:
        log_file_handler = logging.FileHandler(args.log_file)
        formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s')
        log_file_handler.setFormatter(formatter)
        logger.addHandler(log_file_handler)

    if args.show_logs:
        stdout_handler = logging.StreamHandler(sys.stdout)
        formatter = logging.Formatter('[%(levelname)s] [%(threadName)s] %(message)s')
        stdout_handler.setFormatter(formatter)
        logger.addHandler(stdout_handler)

    if args.verbose:
        logger.setLevel(level=logging.DEBUG)
    else:
        logger.setLevel(level=logging.INFO)

    trigger_events = Atman()
    trigger_events.start()
    trigger_events.join()


import boto3
import json
from django.core.serializers.json import DjangoJSONEncoder

import xml.etree.cElementTree as et

from django.conf import settings

def is_svg(f):
    tag = None
    try:
        for event, el in et.iterparse(f.open(mode='r'), ('start',)):
            tag = el.tag
            break
    except et.ParseError:
        pass
    return tag == '{http://www.w3.org/2000/svg}svg'

class DynamoDbModuleUtility:
    """
        Get Module permission from Dynamo db and access.
    """

    @staticmethod
    def get_module_permissions(airport_code):
        if settings.AIRPORT_PLAN_ENABLE:
            if airport_code not in settings.AIRPORT_PLANS:
                DynamoDbModuleUtility.fetch_module_permissions(airport_code)

    @staticmethod
    def fetch_module_permissions(airport_code):
        """
            Used to return all module permissions to save in session
        """
        if settings.AIRPORT_PLAN_ENABLE:
            dynamodb = boto3.resource('dynamodb', region_name=settings.AWS_DEFAULT_REGION)
            airport_table = dynamodb.Table(settings.APP_PREFIX + 'airport')
            airport_key = '{0}_{1}'.format(airport_code, settings.BACKEND_ID)
            airport_object = airport_table.get_item(Key={"id": airport_key})
            if 'Item' in airport_object.keys() and 'plan' in airport_object['Item']:
                settings.AIRPORT_PLANS[airport_code] = json.loads(json.dumps(
                    airport_object['Item']['plan'], cls=DjangoJSONEncoder))
            else:
                settings.AIRPORT_PLANS[airport_code] = {}

    @staticmethod
    def return_module_attribute(airport_code, module_key, attribute):
        """
            Check if the module key exists in dynamodb data
        """
        DynamoDbModuleUtility.get_module_permissions(airport_code)
        if settings.AIRPORT_PLAN_ENABLE and airport_code in settings.AIRPORT_PLANS:
            plan = settings.AIRPORT_PLANS[airport_code]
            for module in plan['modules']:
                if module['key'] == module_key:
                    if 'attributes' in module.keys() and attribute in module['attributes'].keys():
                        return module['attributes'][attribute]
        return None

    @staticmethod
    def get_plan_data(airport_code):
        """
            Return module based on module permissions
        """
        DynamoDbModuleUtility.get_module_permissions(airport_code)
        if settings.AIRPORT_PLAN_ENABLE:
            if airport_code in settings.AIRPORT_PLANS:
                return settings.AIRPORT_PLANS[airport_code]
            return {}
        else:
            return {}

    @staticmethod
    def has_module_permissions(airport_code, module_key):
        """
            Return module based on module permissions
        """
        DynamoDbModuleUtility.get_module_permissions(airport_code)
        if settings.AIRPORT_PLAN_ENABLE:
            if airport_code in settings.AIRPORT_PLANS:
                plan = settings.AIRPORT_PLANS[airport_code]
                for module in plan['modules']:
                    if module['key'] == module_key and module.get('enabled', 'false') == 'true':
                        return True
                return False
            return False
        else:
            return True

    @staticmethod
    def has_sources_permissions(airport_code, source_key):
        """
            Return source based on sources permissions
        """
        DynamoDbModuleUtility.get_module_permissions(airport_code)
        if settings.AIRPORT_PLAN_ENABLE:
            if airport_code in settings.AIRPORT_PLANS:
                plan = settings.AIRPORT_PLANS[airport_code]
                for source in plan['sources']:
                    if source['key'] == source_key and source.get('enabled', 'false') == 'true':
                        return True
                return False
            return False
        else:
            return True


import argparse
import logging
import socket

from dns.resolver import Resolver

from certipy.target import Target

class DnsResolver:
    def __init__(self, options: argparse.Namespace, target: Target):
        self.resolver = Resolver()

        # We can't put all possible nameservers in the list of nameservers, since
        # the resolver will fail if one of them fails
        nameserver = options.nameserver
        if nameserver is None:
            nameserver = target.dc_ip
        if nameserver is None:
            nameserver = target.target_ip

        self.resolver.nameservers = [nameserver]
        self.use_tcp = options.dns_tcp

    def resolve(self, hostname: str) -> str:
        # Try to resolve the hostname with DNS first, then try a local resolve
        try:
            # Check if hostname is an IP
            socket.inet_aton(hostname)
            return hostname
        except Exception:
            pass

        ip_addr = None
        logging.debug(
            "Trying to resolve %s at %s"
            % (repr(hostname), repr(self.resolver.nameservers[0]))
        )
        try:
            answers = self.resolver.resolve(hostname, tcp=self.use_tcp)
            if len(answers) == 0:
                raise Exception()

            ip_addr = answers[0].to_text()
        except Exception:
            pass

        if ip_addr is None:
            try:
                ip_addr = socket.gethostbyname(hostname)
            except Exception:
                ip_addr = None

        if ip_addr is None:
            logging.warning("Failed to resolve: %s" % hostname)
            return hostname

        return ip_addr


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
main.py
@author Luc Kusters
@date 10-11-2023
"""

import os.path
import typing

import collections

Task = collections.namedtuple("Task", ("low", "high"))
TaskPair = collections.namedtuple("TaskPair", ("t1", "t2"))

def load_data(path: os.PathLike) -> list[str]:
    with open(path, "r") as fhandle:
        lines: list[str] = fhandle.readlines()
    return lines

def parse_data(lines: list[str]) -> list[TaskPair]:
    parsed_data = []
    for line in lines:
        task1, task2 = line.split(",")
        t1 = Task(*list(int(lim) for lim in task1.split("-")))
        t2 = Task(*list(int(lim) for lim in task2.split("-")))
        parsed_data.append(TaskPair(t1, t2))
    return parsed_data

def fully_contained(task_pair: TaskPair) -> bool:
    t1, t2 = task_pair
    t1_contains_t2 = (t1.low >= t2.low) and (t1.high <= t2.high)
    t2_contains_t1 = (t1.low <= t2.low) and (t1.high >= t2.high)
    if t1_contains_t2 or t2_contains_t1:
        return True
    return False

def argmax(iter_: typing.Iterable):
    return max(zip(iter_, range(len(iter_))))[1]

def overlap(task_pair: TaskPair) -> bool:
    """
    t1 = 6-8
    t2 = 3-4

    t_high = argmax(t1.high, t2.high)
    """
    # sort tasks by max high range value
    idx_high = argmax(tuple(t.high for t in task_pair))
    idx_low = not idx_high
    t_low = task_pair[idx_low]
    t_high = task_pair[idx_high]
    print(t_low, t_high, t_low.high >= t_high.low)
    return t_low.high >= t_high.low

def part_1(parsed_data: list[TaskPair]) -> int:
    num_fully_contained = sum(fully_contained(task_pair)
                              for task_pair in parsed_data
                              )
    return num_fully_contained

def part_2(parsed_data: list[TaskPair]):
    return sum(overlap(task) for task in parsed_data)

def main():
    data = load_data("data.txt")
    parsed_data = parse_data(data)
    print("part 1:", part_1(parsed_data))
    print("part_2:", part_2(parsed_data))

if __name__ == "__main__":
    main()


'''This module creates a diagram dataset yaml that can be used to visualise a GA'''
import math
from collections import OrderedDict

from matplotlib import cm
import yaml
import numpy as np

from cga_src.ga import GA
from cga_src.cnn import CNN
from cga_src.evaluator import Evaluator
from cga_src.pooling_layer import PoolingLayer
from cga_src.population import Population

class DiagramExporter:
    '''Exports a pickle from a GA to a yaml file that can be used to visualise the GA history'''

    def __init__(self):
        self._point_id = 0
        self._colors = None
        self._species = None
        self._generations = None
        self._algorithm = None

    def pickle_to_yaml(self, data, file_in, file_out):
        '''Parses a pickle to a diagram yaml file'''
        algorithm = GA.from_pickle(
            file_in,
            data=data,
            restore_individual=lambda top: CNN.from_string(
                top,
                input_shape=data.example.shape,
                num_classes=data.num_classes
            ),
        )

        # unset pickle file so that it won't start pickling again
        algorithm.pickle_file = None

        return self.ga_to_yaml(algorithm, file_out)

    def ga_to_yaml(self, algorithm, file_out):
        '''Parses a GA history to a diagram yaml file'''
        self._point_id = 0
        self._algorithm = algorithm

        self._generations = [gen for gen in algorithm.generations]

        # This updates old pickles where pooling layers had a filter depth
        evaluator = algorithm.evaluator

        def update_individual(individual: CNN):
            # pylint: disable=protected-access
            topology_before = individual.topology
            for layer in individual._layers:
                if isinstance(layer, PoolingLayer):
                    layer._kernel._depth = None

            if topology_before in evaluator.cache:
                evaluator.cache[individual.topology] = evaluator.cache[topology_before]

            if individual.history.parent1:
                update_individual(individual.history.parent1)
            if individual.history.parent2:
                update_individual(individual.history.parent2)

        for gen in self._generations:
            for individual in gen:
                update_individual(individual)
        self._assign_species()

        points = {}
        for gen_index, gen in enumerate(self._generations):
            for i in gen.individuals:
                self._parse_individual(i, gen_index+1, None, points)

        # get the best individual
        best_individual = algorithm.best_individual

        # and the one with the highest fitness
        highest_acc_index = np.argmax([
            ev.accuracy for gen in algorithm.generations for ev in gen.evaluations
        ])

        highest_acc_individual = [
            ind for gen in algorithm.generations for ind in gen.individuals
        ][highest_acc_index]

        # parse the two
        best_individual_gen_index = highest_acc_gen_index = None
        for i, gen in enumerate(algorithm.generations):
            if best_individual_gen_index is None and best_individual in gen:
                best_individual_gen_index = i + 1
            if highest_acc_individual is not None and highest_acc_gen_index is None and highest_acc_individual in gen:
                highest_acc_gen_index = i + 1
            if best_individual_gen_index is not None and highest_acc_gen_index is not None:
                break

        best_individual = self._parse_individual(
            best_individual,
            best_individual_gen_index,
            None,
            points
        )
        highest_acc_individual = self._parse_individual(
            highest_acc_individual,
            highest_acc_gen_index,
            None,
            points
        )

        # unpack the structure into a flat array
        points = [p for point in points.values() for data in point.values()
                  for p in data]
        # sort by z index so frontend can just iterate over the points
        points = sorted(points, key=lambda i: i['z'], reverse=True)

        min_fitness = min([gen.worst_fitness
                           for gen in algorithm.generations]
                          )
        obj = {
            'points': points,
            'num_generations': len(self._generations),
            'max_fitness': algorithm.best_fitness,
            'min_fitness': min_fitness,
            'best_individual': best_individual,
            'highest_acc_individual': highest_acc_individual,
            'evaluation_time': algorithm.evaluation_time
        }

        with open(file_out, 'w') as file:
            yaml.safe_dump(obj, file)

    def _assign_species(self):
        rgb = cm.get_cmap('tab10')
        greys = cm.get_cmap('binary')

        all_topologies = [i.topology for g in self._generations for i in g]
        self._species = {topology: i for
                         (i, topology) in enumerate(all_topologies)}

        all_topologies = set(all_topologies)
        fitnesses = {}
        accuracies = {}

        # if individuals have been evaluated with multiple epochs: take the highest fitness
        for topology in {i.topology for g in self._generations for i in g}:
            fitnesses[topology] = max([
                fitnesses[topology] if topology in fitnesses else -1,
                self._algorithm.evaluator.get_evaluation(
                    topology
                ).result().fitness
            ])
            accuracies[topology] = max([
                accuracies[topology] if topology in accuracies else -1,
                self._algorithm.evaluator.get_evaluation(
                    topology
                ).result().accuracy
            ])

        # get the top N topologies according to accuracy
        top_n = rgb.N
        accuracies = sorted(OrderedDict(accuracies),
                            key=lambda i: accuracies[i],
                            reverse=True
                            )
        top_individuals = accuracies[:top_n]

        def hex_color(float_color):
            float_color = float_color[:3]
            rgb = tuple([int(256 * v) for v in float_color])
            return '#%02x%02x%02x' % rgb

        # create colors for top topologies
        # using ordered dict so the assignment of colors will be the same for every pickle
        self._colors = OrderedDict({
            t: hex_color(rgb.colors[i]) for i, t in enumerate(top_individuals)
        })

        # for all others use greyscale
        number_of_greys = len(accuracies) - \
            len(top_individuals)

        self._colors.update(OrderedDict({
            t: hex_color(greys(i / number_of_greys)[:3])
            for i, t in enumerate(all_topologies.difference(top_individuals))
        }))

    def _parse_individual(self, individual: CNN, gen_index, child: CNN, points):
        '''
        Recursively iterates through the history and extracts
        X,Y,Z coordinates and colors from it
        '''

        fitness = individual.history.evaluation.fitness if individual.history.evaluation is not None \
            else child.history.evaluation.fitness if child and child.history.evaluation else -1

        if gen_index not in points:
            points[gen_index] = {}
        if fitness not in points[gen_index]:
            points[gen_index][fitness] = []
        else:
            for point in points[gen_index][fitness]:
                # prevent multi evaluation of the same data point
                if individual.topology == point['topology']:
                    return point

        points_at_this_location = len(
            points[gen_index][fitness]
        )

        def get_parent_generation(own_generation, parent: CNN):
            if parent.history.evaluation is None:
                # this parent was never in a generation (crossover-mutation by product)
                # make him a ghost
                return own_generation - .5
            # else: this is a proper parent generation, so return a whole number that is smaller
            if own_generation % 1 == 0:
                return own_generation - 1
            return math.floor(own_generation)

        parent1 = parent2 = None
        if individual.history.parent1 is not None:
            parent_generation = get_parent_generation(
                gen_index,
                individual.history.parent1
            )
            parent1 = self._parse_individual(
                individual.history.parent1,
                parent_generation,
                individual, points
            )

        if individual.history.parent2 is not None:
            parent_generation = get_parent_generation(
                gen_index,
                individual.history.parent2
            )
            parent2 = self._parse_individual(
                individual.history.parent2,
                parent_generation,
                individual, points
            )

        point = dict(individual.history.__dict__)
        color = self._colors[individual.topology] if individual.topology in self._colors else None

        if individual.topology not in self._species:
            self._species[individual.topology] = 'ghost_%d' % len(
                self._species)

        point.update({
            'id': 'p_%d' % self._point_id,
            'species': 's_%s' % str(self._species[individual.topology]),

            'x': gen_index,
            'y': fitness,
            'z': points_at_this_location * 4,
            'evaluation': individual.history.evaluation.__dict__ if individual.history.evaluation else None,
            'topology': individual.topology,
            # epoch could be a np int which confuses the yaml parser
            'num_epoch': int(individual.num_epochs),
            'color': color,
            'parent1': parent1,
            'parent2': parent2,
            'num_params': sum(p.numel() for p in individual.parameters()),
        })
        self._point_id += 1
        points[gen_index][fitness] += [point]

        return point


from __future__ import annotations

import asyncio
import os
import tempfile
from collections.abc import AsyncIterator, Iterator
from pathlib import Path
from typing import Any

import aiohttp
import aiohttp.web
import pytest
from yarl import URL

from platform_container_runtime.config import KubeClientAuthType, KubeConfig
from platform_container_runtime.kube_client import KubeClient

from .conftest import create_local_app_server

class TestKubeClientTokenUpdater:
    @pytest.fixture
    async def kube_app(self) -> aiohttp.web.Application:
        async def _get_nodes(request: aiohttp.web.Request) -> aiohttp.web.Response:
            auth = request.headers["Authorization"]
            token = auth.split()[-1]
            app["token"]["value"] = token
            return aiohttp.web.json_response({"kind": "NodeList", "items": []})

        app = aiohttp.web.Application()
        app["token"] = {"value": ""}
        app.router.add_routes([aiohttp.web.get("/api/v1/nodes", _get_nodes)])
        return app

    @pytest.fixture
    async def kube_server(
        self, kube_app: aiohttp.web.Application, unused_tcp_port_factory: Any
    ) -> AsyncIterator[str]:
        async with create_local_app_server(
            kube_app, port=unused_tcp_port_factory()
        ) as address:
            yield f"http://{address.host}:{address.port}"

    @pytest.fixture
    def kube_token_path(self) -> Iterator[str]:
        _, path = tempfile.mkstemp()
        Path(path).write_text("token-1")
        yield path
        os.remove(path)

    @pytest.fixture
    async def kube_client(
        self, kube_server: str, kube_token_path: str
    ) -> AsyncIterator[KubeClient]:
        async with KubeClient(
            config=KubeConfig(
                url=URL(kube_server),
                auth_type=KubeClientAuthType.TOKEN,
                token_path=kube_token_path,
                token_update_interval_s=1,
            )
        ) as client:
            yield client

    async def test_token_periodically_updated(
        self,
        kube_app: aiohttp.web.Application,
        kube_client: KubeClient,
        kube_token_path: str,
    ) -> None:
        await kube_client.get_nodes()
        assert kube_app["token"]["value"] == "token-1"

        Path(kube_token_path).write_text("token-2")
        await asyncio.sleep(2)

        await kube_client.get_nodes()
        assert kube_app["token"]["value"] == "token-2"

class TestKubeClient:
    async def test_get_node(self, kube_client: KubeClient) -> None:
        nodes = await kube_client.get_nodes()

        assert nodes

        node = await kube_client.get_node(nodes[0].metadata.name)

        assert node.metadata.labels
        assert node.container_runtime_version
        assert node.os
        assert node.architecture


from c7n.utils import type_schema
from c7n_gcp.actions import MethodAction, SetIamPolicy
from c7n_gcp.provider import resources
from c7n_gcp.query import QueryResourceManager, TypeInfo, ChildTypeInfo, ChildResourceManager

@resources.register('spanner-instance')
class SpannerInstance(QueryResourceManager):
    """
    https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances
    """
    class resource_type(TypeInfo):
        service = 'spanner'
        version = 'v1'
        component = 'projects.instances'
        enum_spec = ('list', 'instances[]', None)
        scope_key = 'parent'
        scope_template = 'projects/{}'
        name = id = 'name'
        default_report_fields = [
            "name", "displayName", "nodeCount", "state", "config"]
        labels = True
        labels_op = 'patch'
        asset_type = "spanner.googleapis.com/Instance"

        @staticmethod
        def get(client, resource_info):
            return client.execute_command(
                'get', {'name': resource_info['resourceName']}
            )

        @staticmethod
        def get_label_params(resource, all_labels):
            return {'name': resource['name'],
                    'body': {
                        'instance': {
                            'labels': all_labels
                        },
                        'field_mask': ', '.join(['labels'])}}

@SpannerInstance.action_registry.register('delete')
class SpannerInstanceDelete(MethodAction):
    """The action is used for spanner instances delete.

    GCP action is https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances/delete

    :Example:

    .. code-block:: yaml

        policies:
          - name: gcp-spanner-instances-delete
            resource: gcp.spanner-instance
            filters:
              - type: value
                key: nodeCount
                op: gte
                value: 2
            actions:
              - type: delete
    """
    schema = type_schema('delete')
    method_spec = {'op': 'delete'}

    def get_resource_params(self, model, resource):
        return {'name': resource['name']}

@SpannerInstance.action_registry.register('set')
class SpannerInstancePatch(MethodAction):
    """The action is used for spanner instances nodeCount patch.

    GCP action is https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances/patch

    :Example:

    .. code-block:: yaml

        policies:
          - name: gcp-spanner-instances-change-node-count
            resource: gcp.spanner-instance
            filters:
              - type: value
                key: nodeCount
                op: gte
                value: 2
            actions:
              - type: set
                nodeCount: 1
    """
    schema = type_schema('set', required=['nodeCount'],
                         **{'nodeCount': {'type': 'number'}})
    method_spec = {'op': 'patch'}
    method_perm = 'update'

    def get_resource_params(self, model, resource):
        result = {'name': resource['name'],
                  'body': {
                      'instance': {
                          'nodeCount': self.data['nodeCount']
                      },
                      'field_mask': ', '.join(['nodeCount'])}
                  }
        return result

SpannerInstance.action_registry.register('set-iam-policy', SetIamPolicy)

@resources.register('spanner-database-instance')
class SpannerDatabaseInstance(ChildResourceManager):
    """GCP resource:
    https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases
    """
    def _get_parent_resource_info(self, child_instance):
        resource_name = None
        if child_instance['name'] is not None:
            resource_names = child_instance['name'].split('/databases')
            if len(resource_names) > 0:
                resource_name = resource_names[0]
        return {
            'resourceName': resource_name
        }

    class resource_type(ChildTypeInfo):
        service = 'spanner'
        version = 'v1'
        component = 'projects.instances.databases'
        enum_spec = ('list', 'databases[]', None)
        name = id = 'name'
        scope = None
        parent_spec = {
            'resource': 'spanner-instance',
            'child_enum_params': [
                ('name', 'parent')
            ]
        }
        default_report_fields = ["name", "state", "createTime"]
        asset_type = "spanner.googleapis.com/Database"

        @staticmethod
        def get(client, resource_info):
            return client.execute_command(
                'get', {
                    'name': resource_info['resourceName']}
            )

SpannerDatabaseInstance.action_registry.register('set-iam-policy', SetIamPolicy)

@SpannerDatabaseInstance.action_registry.register('delete')
class SpannerDatabaseInstanceDropDatabase(MethodAction):
    """The action is used for databases deleting.

    GCP action is https://cloud.google.com/spanner/docs
        /reference/rest/v1/projects.instances.databases/dropDatabase.

    :Example:

    .. code-block:: yaml

        policies:
          - name: gcp-spanner-instance-databases-delete
            resource: gcp.spanner-database-instance
            filters:
              - type: value
                key: name
                op: contains
                value: dev
            actions:
              - type: delete
    """
    schema = type_schema('dropDatabase', **{'type': {'enum': ['delete']}})
    method_spec = {'op': 'dropDatabase'}
    method_perm = 'drop'

    def get_resource_params(self, model, resource):
        return {'database': resource['name']}


import streamlit as st
import streamlit.components.v1 as components

_radio_button = components.declare_component(
    "radio_button", url="http://localhost:3001",
)

def custom_radio_button(label, options, default, key=None):
    return _radio_button(label=label, options=options, default=default, key=key)

result = custom_radio_button(
    "How many bats?",
    options=["one bat", "TWO bats", "THREE bats", "FOUR BATS! ah ah ah!"],
    default="one bat",
)
st.write("This many: %s" % result)


import requests
import json
import time

#check flask api
api_url = "api/"
port = "5000"

#check fastapi api
# api_url = "api/"
# port = "8000"

#check torchServe api
# api_url = "predictions/bert_trimsweep"
# port = "8080"

url = 'http://127.0.0.1:' + port + '/' + api_url

test_texts = ["Wir hatten hier ein sehr gutes Essen.",
              "Zusammenfassung: guter Service, leckeres (höheres) Essen und schöne Lage.",
              "All dies zu einem sehr vernünftigen Preis!",
              "Das Essen war einfach schlecht.",
              "Wir hatten hier ein paar super Cocktails."]

start_time = time.time()
for text in test_texts:
    j_data = json.dumps({"text": text})
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
    r = requests.post(url, data=j_data, headers=headers)
    print(r, r.text)

print("Requests took %0.3f s" % (time.time() - start_time))


import numpy as np
import re
import sys
import time

from multiprocessing import Process, Queue

from plotter.utils.gcode import *
from plotter.utils.calibration import *

def processPlotterQueue(plotter):
    """Callback for plotter process."""
    plotter.processQueueAsync()

class BasePlotter:
    """Base class of all plotter implementations. Always call '__init__' in the derived class after
    setting up all (custom) member variables. Otherwise the (custom) config and calibration is
    not available in the worker process."""

    def __init__(self, config, initial_lengh, PhysicsEngineClass):
        """Sets up the worker process and initializes the system."""
        base = config["base_width"]
        self.calib = Calibration(base,
                                 PhysicsEngineClass.calcOrigin(
                                     initial_lengh, base),
                                 stepsPerMM=config["steps_per_mm"],
                                 resolution=config["movement_resolution"])

        self.config = config
        self.physicsEngine = PhysicsEngineClass(self.config, self.calib)
        self.currPos = np.zeros((2,))
        self.currCordLength = self.physicsEngine.point2CordLength(self.currPos)
        self.speed = 10000
        self.penIsDown = False

        self.workerProcess = Process(target=processPlotterQueue, args=(self,))
        self.workerQueue = Queue(1000)
        self.workerProcess.start()

    def shutdown(self):
        """Stops the worker queue and the worker process."""
        print("Shutting down..")
        self.workerQueue.put(None)
        self.workerQueue.close()
        self.workerQueue.join_thread()
        self.workerProcess.join()

    def executeGCodeFile(self, file_name):
        """Opens the specified gcode file and pushes each command into the worker queue.
        This will block if the queue is full."""
        with open(file_name, 'r') as f:
            lines = f.readlines()

        for c in lines:
            self.workerQueue.put(c.strip())

    def processQueueAsync(self):
        """Plotter worker function which runs in the worker process.
        Processes new commands by reading from the worker queue."""
        print("Plotter process started")

        item = self.workerQueue.get()
        while(item is not None):

            self.executeCmd(item)

            item = self.workerQueue.get()

        print("Plotter process stopped")
        exit(0)

    def executeCmd(self, cmd):
        """Executes a command. Should only be called from the worker process."""

        if cmd.startswith("G0"):
            d = decodeGCode(cmd)
            if not d:
                return

            if "S" in d:
                self.setSpeed(d["S"])

            self.moveToPos([d.get("X", self.currPos[0]),
                            d.get("Y", self.currPos[1])])

        elif cmd.startswith("G28"):
            self.moveToPos([0, 0])

        elif cmd.startswith("G2"):
            d = decodeGCode(cmd)
            if not d:
                return

            if "S" in d:
                self.setSpeed(d["S"])

            if "R" not in d and "X" not in d and "Y" not in d:
                print(d)
                print("Unexpected cmd type. Failed to process command.")
                return

            self.moveArc([d["X"], d["Y"]], d["R"],
                         d.get("A", 0), d.get("B", 360))

        elif cmd.startswith("M3"):
            self.penDown()
        elif cmd.startswith("M4"):
            self.penUp()
        else:
            print("Unexpected cmd type. Failed to process command.")

    def moveToPos(self, targetPos):
        """Move to specified position.
        Raises an error if not implemented by derived class."""
        raise NotImplementedError("Function moveToPos not implemented")

    def moveArc(self, center, radius, startAngle, endAngle):
        """Move on an arc.
        Raises an error if not implemented by derived class."""
        raise NotImplementedError("Function moveArc not implemented")

    def penUp(self):
        """Lift the pen.
        Raises an error if not implemented by derived class."""
        raise NotImplementedError("Function penUp not implemented")

    def penDown(self):
        """Lower the pen.
        Raises an error if not implemented by derived class."""
        raise NotImplementedError("Function penDown not implemented")

    def setSpeed(self, s):
        """Sets the current speed. (used by subsequent commands)."""
        self.speed = s

    def __str__(self):
        return """
{}

------------ State ------------
Current Position: {}
Current Length:   {}
PenState:         {}
Current Speed:    {}
Queue Size:       {}
-------------------------------""".format(self.calib, self.currPos, self.currCordLength, "DOWN" if self.penIsDown else "UP", self.speed, self.workerQueue.qsize())


#!/usr/bin/env python
from __future__ import print_function, absolute_import, division

import logging

from collections import defaultdict
from errno import ENOENT
from stat import S_IFDIR, S_IFLNK, S_IFREG
from sys import argv, exit
from time import time

from fuse import FUSE, FuseOSError, Operations, LoggingMixIn

import metaserver, dataserver, xmlrpclib, pickle
from xmlrpclib import Binary
import sys
from zlib import crc32
import random
import math

import pprint
from time import sleep

BLKSIZE = 8

if not hasattr(__builtins__, 'bytes'):
    bytes = str

"""
FOR FINAL PROJECT
PUT ALL CHANGES IN DATAWRAPPER - few exceptions
"""

"""
KNOWN BUGS
    - can still cd into directory without x permission
"""

def split_to_list(split_this):
    arr = []
    temp = split_this
    while len(temp) > BLKSIZE:
        arr.append(temp[0:BLKSIZE])
        temp = temp[BLKSIZE:]
    if len(temp) > 0:
        arr.append(temp)
    return arr

def join_from_list(join_this):
    temp = bytes()  # put these methods after the "if not hasattr" so that it gets the right type
    for piece in join_this:
        temp = temp + piece
    return temp

# DO NOT TOUCH NO CHANGES TO META SHOULD BE NECESSARY
class MetaWrapper:
    def __init__(self,url):
        print("Starting META proxy at " + str(url))
        self.proxy = xmlrpclib.ServerProxy(url)
        self.retryNum = 5

    def makeInode(self): # ONLY WORKS FOR DATASERVER!!!!
        for i in range(0,self.retryNum):
            try:
                return self.proxy.makeInode()
            except:
                continue
        return 0

    def test(self):
        for i in range(0,self.retryNum):
            try:
                return self.proxy.test()
            except:
                continue
        return False

    def getkeys(self):
        for i in range(0,self.retryNum):
            try:
                return self.proxy.getkeys()
            except:
                continue
        return []

    # key is string, data is object
    def put(self, key, data):
        for i in range(0,self.retryNum):
            try:
                return self.proxy.put(Binary(key),Binary(pickle.dumps(data)))
            except:
                continue
        return None

    # key is string, returns object
    def get(self, key):
        for i in range(0,self.retryNum):
            try:
                return pickle.loads(self.proxy.get((Binary(key))).data)
            except:
                #print ("ERROR: Accessing file that doesn't exist")
                continue
        return None

    def rem(self, key):
        #print("__REM__")
        for i in range(0,self.retryNum):
            try:
                return self.proxy.rem((Binary(key)))
            except:
                continue
        return False

    def print_content(self):
        #print("__PRINT_CONTENT__")
        for i in range(0,self.retryNum):
            try:
                return self.proxy.print_content()
            except:
                continue
        return False

def split_path(path):
    parent_path, file_name = path.rsplit('/',1)
    if not parent_path: # if parent is root, parent_path will just be '' after split
        parent_path = '/'
    return parent_path, file_name

# ALL CHANGES SHOULD HAPPEN HERE
class DataWrapper:
    def __init__(self,portList):
        print("Starting DATA proxies for " + str(portList))
        self.proxy = [] # create list for all proxies

        for idx, portNum in enumerate(portList):
            self.proxy.append( xmlrpclib.ServerProxy("http://localhost:" + str(portNum)) )

    def test(self):
        test = []
        for idx, p in enumerate(self.proxy):
            print("DataWrapper testing Proxy", idx)
            try:
                test.append(self.proxy[idx].test())
            except:
                test.append(False)

        return test

    # key is string, data is object # FIXME or is data string?
    def put(self, key, data):
        print("DataWrapper PUT")
        key = str(key) # Binary() takes string input
        print(str(len(self.proxy)) + " dataservers")

        inode = str(key)
        N = len(self.proxy)
        starting_server = int(inode)%N
        print("Starting server: " + str(starting_server))
        ################################
        # happens when initializing files
        # FIXME: necessary? Also always fails
        if data == "":
            print ("initializing block0")
            for i in range(0,3): # 3 replicas
                for r in range(0,3): # try to store data 3 times
                    try:
                        self.proxy[(starting_server+i)%N].put_block(
                            Binary(str(i)), #replica_num
                            Binary(inode),  #inode
                            Binary(str(block_num)), #block num
                            Binary( "" ) ) # data
                        print("Initialized inode on dataproxy: "+ str((starting_server+i)%N))
                        break
                    except:
                        print("Failed to init inode on dataproxy: "+ str((starting_server+i)%N))
                        continue
            return
        ################################
        # writing data to files
        for block_num, d in enumerate(split_to_list(data)):
            for replica_num in range(0,3): # for each replica 0, 1, 2
                block_stored = False
                while block_stored is False:
                    try:
                        self.proxy[(starting_server+replica_num+block_num)%N].put_block(
                            Binary(str(replica_num)), #replica_num
                            Binary(inode),  #inode
                            Binary(str(block_num)), #block num
                            Binary(str(d) ) ) # data
                        print("PUT dataserver " + str((starting_server+replica_num+block_num)%N) +
                            ", replica " + str(replica_num) + " | inode: " + str(inode) +
                            ", block: " + str(block_num) + ", data:  " + str(d) )
                            #last element will print newline to screen as well
                        block_stored = True
                    except:
                        pass
        print("BLOCK PUT FINISHED")
        return

    # key is string(inode), returns Binary object
    def get(self, key, size, offset=0):
        print("DataWrapper GET | inode: " + str(key) +
            ", offset: " + str(offset) + ", size: " + str(size) )
        key = str(key)

        #####################################
        #####################################
        inode = str(key)
        N = len(self.proxy)

        starting_block = int(offset/BLKSIZE)
        first_byte_of_starting_block = starting_block*BLKSIZE
        offset_from_start_of_starting_block = offset-first_byte_of_starting_block
        num_blocks_to_retrieve = int(math.ceil(float(size+offset_from_start_of_starting_block)/BLKSIZE))
        blocks_to_retrieve = range(starting_block, num_blocks_to_retrieve)
        print("blocks to retrieve: {}".format(blocks_to_retrieve))

        starting_server = (int(inode)+starting_block)%N

        temp = []
        print("Starting server: " + str(starting_server))
        for block in blocks_to_retrieve:
            block_retrieved = False
            while block_retrieved is False: # retry forever until at least one copy of block was retrieved
                print(block)

                replicas_needing_rewrite = []
                replicas_to_try = range(0,3)
                random.shuffle(replicas_to_try)
                for trynum, replica in enumerate(replicas_to_try):
                    print("TRYING GET_" + str(trynum) + ": dataserver: " + str((starting_server+block+replica)%N) +
                                ",replica " + str(replica) + ",inode: " + str(inode) +
                                ",block_num: " + str(block))
                    try:
                        result = pickle.loads(self.proxy[(starting_server + block + replica) % N].get_block(
                            Binary(str(replica)),  # replica_num
                            Binary(inode),  # inode
                            Binary(str(block))  # block num
                        ).data)

                        print(str(result))
                        result_data = result["value"]
                        result_checksum = result["checksum"]
                        result_checksum_calc = crc32(result_data)

                        if result_checksum_calc != result_checksum:
                            replicas_needing_rewrite.append(replica)
                            continue

                        for replica_to_rewrite in replicas_needing_rewrite:
                            print("rewriting replica: {} for inode: {} block: {}".format(replica_to_rewrite, inode, block))
                            self.proxy[(starting_server + replica_to_rewrite + block) % N].put_block(
                                Binary(str(replica_to_rewrite)),  # replica_num
                                Binary(inode),  # inode
                                Binary(str(block)),  # block num
                                Binary(str(result_data)))  # data

                        temp.append(result_data)
                        block_retrieved = True
                        print("GOT: " + str(temp[-1]))
                        break
                    except:
                        continue
                                      # infinite loop described below shouldn't occur if you don't try to read past the end of the file
                # if r == maxtries-1: # CAREFUL COULD CAUSE ENDLESS LOOP BECAUSE SIZE FUCKED UP IN OUTERMOST FOR LOOP
                #     break

        print(temp)
        print(join_from_list(temp))
        print("BLOCK GET FINISHED")
        return join_from_list(temp)

    def rem(self, key):
        key = str(key)
        #print("__REM__")
        for p in self.proxy:
            p.rem((Binary(key)))
        return

    def print_content(self):
        #print("__PRINT_CONTENT__")
        for p in self.proxy:
            p.print_replicas
        return

class Memory(LoggingMixIn, Operations):

    def __init__(self, metaport, dataports):
        # connect metaservers and dataservers
        self.metaproxy = MetaWrapper("http://localhost:" + str(metaport)) # FIXME: make like dataproxy
        try:
            print("MetaServer connected: " + str(self.metaproxy.test()))
        except:
            print("METASERVER NOT CONNECTED!!!!")
        self.dataproxy = DataWrapper(dataports) # create object with all data servers
        dataproxytest = self.dataproxy.test()
        print("DataServers connected: " + str(dataproxytest))
        if False in dataproxytest:
            print("DATASERVER NOT CONNECTED!!!!")

        # initialize filedescriptor
        self.fd = 0
        now = time()

        #initialize metaserver if not initialized
        if not self.metaproxy.get('/'):
            print("INITIALIZING METASERVER!!!")
            self.metaproxy.put('/',dict(st_mode=(S_IFDIR | 0o755), st_ctime=now,
                            st_mtime=now, st_atime=now, st_nlink=2, files=[]))
        else:
            print("METASERVER ALREADY INITIALIZED!!!")

        return

    def chmod(self, path, mode):
        #print("----CHMOD: " + path + " MODE: " + str(mode) )
        file = self.metaproxy.get(path)
        file['st_mode'] &= 0o770000
        file['st_mode'] |= mode
        self.metaproxy.put(path,file)
        return 0

    def chown(self, path, uid, gid):
        #print("----CHOWN: " + path + "")
        file = self.metaproxy.get(path)
        file['st_uid'] = uid
        file['st_gid'] = gid
        self.metaproxy.put(path,file)

    def create(self, path, mode):
        print("CREATE:", path)

        inode_t = self.metaproxy.makeInode()
        self.dataproxy.put(inode_t,'')
        #self.dataproxy.print_content()
        #self.metaproxy.print_content()

        parent_path, new_file_name = split_path(path)

        parent = self.metaproxy.get(parent_path) # get parent dir
        parent['files'].append(new_file_name)
        self.metaproxy.put(parent_path,parent) # update parent in database
        self.metaproxy.put(path, dict(st_mode=(S_IFREG | mode), st_nlink=1,
                                st_size=0, st_ctime=time(), st_mtime=time(),
                                st_atime=time() , inode=inode_t))
        self.fd += 1
        return self.fd

    def getattr(self, path, fh=None):
        #print("----GETATTR: " + path + "")
        file = self.metaproxy.get(path)
        if not file: # if file doesn't exist
            raise FuseOSError(ENOENT)
        return file

    def getxattr(self, path, name, position=0):
        #print("----GETXATTR: " + path + " NAME: " + str(name) + " POS: " + str(position))
        attrs = self.metaproxy.get(path)
        if attrs is not None:
            attrs = attrs.get('attrs',{})
        else:
            attrs = {}

        try:
            return attrs[name]
        except KeyError:
            return ''

    # FIXME: untested
    def listxattr(self, path):
        #print("_-_-LISTXATTR: " + path)
        attrs = self.metaproxy.get(path).get('attrs',{})
        return attrs.keys()

    def mkdir(self, path, mode):
        #print("**MKDIR: " + path + "")

        parent_path, new_dir_name = split_path(path)

        parent = self.metaproxy.get(parent_path) # get parent dir
        parent['st_nlink'] += 1 # update parent dir
        parent['files'].append(new_dir_name) # add name to files

        self.metaproxy.put(parent_path,parent) # store updated parent dir

        #pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(parent)

        # add new dir
        self.metaproxy.put(path,dict(st_mode=(S_IFDIR | mode), st_nlink=2,
                                st_size=0, st_ctime=time(), st_mtime=time(),
                                st_atime=time(), files=[]) )

    def open(self, path, flags):
        #print("_OPEN: " + path + "")
        self.fd += 1
        return self.fd

    def read(self, path, size, offset, fh):
        print("READ: " + path + "")
        inode = self.metaproxy.get(path)['inode']
        file_size = self.metaproxy.get(path).get('st_size', 0)
        if size > file_size:
            size = file_size
        return self.dataproxy.get(inode,size,offset)

    def readdir(self, path, fh):
        print("****READDIR: " + path)
        cur_dir = self.metaproxy.get(path)
        print (cur_dir)
        return ['.','..'] + [ x for x in sorted(cur_dir.get('files',[]))]

    def readlink(self, path):
        print("****READLINK: " + path + "")
        pp = pprint.PrettyPrinter(indent=4)
        metadata = self.metaproxy.get(path)
        while metadata==None: # FIXME: eliminate endless loop possibility
            metadata = self.metaproxy.get(path)

        print("____metadata:")
        pp.pprint(metadata)
        inode = metadata['inode']
        print("____inode:")
        pp.pprint(inode)
        data = self.dataproxy.get(inode,metadata['st_size']) # no offset necessary
        print("____data:")
        pp.pprint(data)
        return data
        #return self.dataproxy.get(self.metaproxy.get(path)['inode'])

    def removexattr(self, path, name):
        #print("-_-_REMOVEXATTR: " + path + "")
        file = self.metaproxy.get(path)
        attrs = file.get('attrs',{})
        try:
            del attrs[name]
        except KeyError:
            pass        # Should return ENOATTR
        self.metaproxy.put(path,file)

    # since use inodes for data, do not have to move anything in data server
    def rename(self, old, new):
        #print("RENAME: " + old + " to " + new )
        file = self.metaproxy.get(old)

        old_parent_path, old_file_name = split_path(old)
        new_parent_path, new_file_name = split_path(new)
        #print(old_parent_path,old_file_name)
        #print(new_parent_path,new_file_name)

        #   update old parent
        old_parent = self.metaproxy.get(old_parent_path)
        old_parent['files'].remove(old_file_name)

        #   update new parent
        if old_parent_path!=new_parent_path:
            new_parent = self.metaproxy.get(new_parent_path)
        else:
            new_parent = old_parent

        new_parent['files'].append(new_file_name)

        # update actual path
        # check if directory or file
        if file['st_mode'] & 0o770000 == S_IFREG: # just a file
            #print('Moving file')
            self.metaproxy.rem(old)
            self.metaproxy.put(new,file)
        elif file['st_mode'] & 0o770000 == S_IFDIR:
            old_parent['st_nlink'] -= 1
            new_parent['st_nlink'] += 1
            keys = self.metaproxy.getkeys()
            #print(keys)
            for key in keys:
                if old in key:
                    temp = self.metaproxy.get(key)
                    self.metaproxy.rem(key)

                    new_path = key.replace(old, new, 1)
                    #print("OLD PATH: " + key + ", NEW PATH: " + new_path)

                    self.metaproxy.put(new_path,temp)
                    #self.metaproxy.print_content()
        self.metaproxy.put(old_parent_path,old_parent)
        self.metaproxy.put(new_parent_path,new_parent)
        return

    def rmdir(self, path):
        self.metaproxy.rem(path)
        parent_path, dir_name = split_path(path)
        parent = self.metaproxy.get(parent_path)
        parent['files'].remove(dir_name)
        parent['st_nlink'] -= 1
        self.metaproxy.put(parent_path,parent)

    def setxattr(self, path, name, value, options, position=0):
        #print("-_-_SETXATTR: " + path + "")
        file = self.metaproxy.get(path)
        attrs = file.get('attrs',{})
        attrs[name] = value
        self.metaproxy.put(path,file)

    def statfs(self, path):
        #print("_STATFS: " + path + "")
        return dict(f_bsize=512, f_blocks=4096, f_bavail=2048)

    def symlink(self, target, source):
        print("**SYMLINK target: " + target + " source: " + source)
        inode_t = self.metaproxy.makeInode()
        self.metaproxy.put(target,dict(st_mode=(S_IFLNK | 0o777), st_nlink=1,
                                    st_size=len(source), inode = inode_t))
        parent_path, link_name = split_path(target)
        parent = self.metaproxy.get(parent_path)
        parent['files'].append(link_name)
        self.metaproxy.put(parent_path, parent)
        self.dataproxy.put(inode_t, source)
        self.metaproxy.print_content()
        self.dataproxy.print_content()
        return

    def truncate(self, path, length, fh=None):
        print("TRUNCATE: " + path + "")
        file = self.metaproxy.get(path)
        contents = self.dataproxy.get(file['inode'],file['st_size'])
        contents = contents[:length]
        self.dataproxy.put(file['inode'],contents)
        file['st_size'] = length
        self.metaproxy.put(path,file)

    def unlink(self, path):
        #print("**UNLINK: " + path + "")
        parent_path, file_name = split_path(path)
        inode_t = self.metaproxy.get(path)['inode']
        self.dataproxy.rem(inode_t)
        self.metaproxy.rem(path)

        parent = self.metaproxy.get(parent_path)
        parent['files'].remove(file_name)
        self.metaproxy.put(parent_path,parent)

    def utimens(self, path, times=None):
        #print("----UTIMENS: " + path )
        now = time()
        atime, mtime = times if times else (now, now)
        file = self.metaproxy.get(path)
        file['st_atime'] = atime
        file['st_mtime'] = mtime
        self.metaproxy.put(path,file)

    def write(self, path, data, offset, fh):
        print("WRITE: " + path )
        file = self.metaproxy.get(path)
        contents = self.dataproxy.get(file['inode'],file['st_size'])
        contents = contents[:offset] + data
        self.dataproxy.put(file['inode'],contents)
        #self.dataproxy.print_content()
        file['st_size'] = len(contents)

        self.metaproxy.put(path,file)
        return len(data)

if __name__ == '__main__':
    if len(argv) < 4:
        print('usage: %s <mountpoint> <metaserver port#> <dataserver port#> ... <dataserver port#N>' % argv[0])
        exit(1)
    mountpoint = argv[1]
    meta_port = argv[2]
    data_ports = argv[3:]
    print("mountpoint: " + mountpoint)
    print("metaserver port: " + meta_port)
    print("dataserver ports: " + str(data_ports))
    #logging.basicConfig(level=logging.DEBUG)
    fuse = FUSE(Memory(meta_port,data_ports), mountpoint, foreground=True, debug=False) #FIXME: debug = true


'''
邻域对比图协同滤波(NCGCF)的TensorFlow实现
    作者：山东大学 数学学院 陈彦丞 (201900090074@mail.sdu.edu.cn)
    指导教师：山东大学数据科学研究院 曲存全
    校外指导教师：中国科学院大学数学科学学院 姜志鹏
    时间：2023.3.28
'''

import addition as add
from NCGCF_methods.parser import parse_args
from NCGCF_methods.load_data import *
import multiprocessing # 多线程管理包
import heapq

cores = multiprocessing.cpu_count() // 2 # cpu线程数

args = parse_args() # 引入参数
Ks = eval(args.Ks) # eval()函数用来执行一个字符串表达式，并返回表达式的值

data_generator = Data(path=args.data_path + args.dataset, batch_size=args.batch_size) # 载入数据
USR_NUM, ITEM_NUM = data_generator.n_users, data_generator.n_items # 用户项目数量
N_TRAIN, N_TEST = data_generator.n_train, data_generator.n_test # 训练集与测试集数量
BATCH_SIZE = args.batch_size # 批量大小

def ranklist_by_heapq(user_pos_test, test_items, rating, Ks): # 测试项目评分排序(不含auc评分)
    item_score = {}
    for i in test_items:
        item_score[i] = rating[i] # 测试集项目对应评估分数构建字典

    K_max = max(Ks) # 100
    K_max_item_score = heapq.nlargest(K_max, item_score, key=item_score.get) # get返回指定键的值
    # 从item_score中寻找最大的K_max个元素，测试集评分最高的项目

    r = []
    for i in K_max_item_score:
        if i in user_pos_test: # 是否在用户的正测试集中
            r.append(1)
        else:
            r.append(0)
    auc = 0.
    return r, auc

def get_auc(item_score, user_pos_test): # 获得auc评分
    item_score = sorted(item_score.items(), key=lambda kv: kv[1]) # 项目评分排序
    item_score.reverse() # 降序
    item_sort = [x[0] for x in item_score] # 项目
    posterior = [x[1] for x in item_score] # 评估分数

    r = [] # 真实类别
    for i in item_sort:
        if i in user_pos_test: # 是否在用户的正测试集中
            r.append(1)
        else:
            r.append(0)
    auc = add.auc(ground_truth=r, prediction=posterior)
    return auc

def ranklist_by_sorted(user_pos_test, test_items, rating, Ks): # # 测试项目评分排序(含auc评分)
    item_score = {}
    for i in test_items:
        item_score[i] = rating[i]

    K_max = max(Ks)
    K_max_item_score = heapq.nlargest(K_max, item_score, key=item_score.get)

    r = []
    for i in K_max_item_score:
        if i in user_pos_test:
            r.append(1)
        else:
            r.append(0)
    auc = get_auc(item_score, user_pos_test)
    return r, auc

def get_performance(user_pos_test, r, auc, Ks): # 获得预测的表现
    precision, recall, ndcg, hit_ratio = [], [], [], []

    for K in Ks:
        precision.append(add.mean_k(r, K)) # 准确率
        recall.append(add.recall_at_k(r, K, len(user_pos_test))) # 召回率
        ndcg.append(add.ndcg_at_k(r, K)) # ndcg
        hit_ratio.append(add.hit_at_k(r, K)) # 是否完全正确(1 or 0)

    return {'recall': np.array(recall), 'precision': np.array(precision),
            'ndcg': np.array(ndcg), 'hit_ratio': np.array(hit_ratio), 'auc': auc}

def test_one_user(x): # 输入是二元的
    rating = x[0] # 用户评分(列表)
    u = x[1] # 用户ID(数字)

    try:
        training_items = data_generator.train_items[u] # 用户的项目训练集
    except Exception:
        training_items = []

    user_pos_test = data_generator.test_set[u] # 用户的项目测试集

    all_items = set(range(ITEM_NUM)) # 所有项目

    test_items = list(all_items - set(training_items)) # 训练集之外作为测试集

    if args.test_flag == 'part':
        r, auc = ranklist_by_heapq(user_pos_test, test_items, rating, Ks) # 不含auc评分
    else:
        r, auc = ranklist_by_sorted(user_pos_test, test_items, rating, Ks) # 含auc评分

    return get_performance(user_pos_test, r, auc, Ks)

def test(sess, model, users_to_test, drop_flag=False, batch_test_flag=False): # 测试
    result = {'precision': np.zeros(len(Ks)), 'recall': np.zeros(len(Ks)), 'ndcg': np.zeros(len(Ks)),
              'hit_ratio': np.zeros(len(Ks)), 'auc': 0.}

    pool = multiprocessing.Pool(cores)
    # Pool类代表了一个进程池的Worker来执行并行的任务，我们直接调用这个类的方法就可以将任务下发到Worker进程中去

    u_batch_size = BATCH_SIZE * 2
    i_batch_size = BATCH_SIZE

    test_users = users_to_test # 进行测试的用户
    n_test_users = len(test_users)
    n_user_batchs = n_test_users // u_batch_size + 1 # 测试用户批量大小

    count = 0

    for u_batch_id in range(n_user_batchs): # 相当于对测试用户进行批次划分
        start = u_batch_id * u_batch_size
        end = (u_batch_id + 1) * u_batch_size

        user_batch = test_users[start: end]

        if batch_test_flag: # 测试项目集分批次

            n_item_batchs = ITEM_NUM // i_batch_size + 1 # 批次数量
            rate_batch = np.zeros(shape=(len(user_batch), ITEM_NUM)) # 单个批次用户与测试项目对应的矩阵

            i_count = 0
            for i_batch_id in range(n_item_batchs):
                i_start = i_batch_id * i_batch_size
                i_end = min((i_batch_id + 1) * i_batch_size, ITEM_NUM)

                item_batch = range(i_start, i_end)

                if drop_flag == False:
                    i_rate_batch = sess.run(model.batch_ratings, {model.users: user_batch,
                                                                model.pos_items: item_batch})
                else:
                    i_rate_batch = sess.run(model.batch_ratings, {model.users: user_batch,
                                                                model.pos_items: item_batch,
                                                                model.node_dropout: [0.]*len(eval(args.layer_size)),
                                                                model.mess_dropout: [0.]*len(eval(args.layer_size))})
                rate_batch[:, i_start: i_end] = i_rate_batch
                i_count += i_rate_batch.shape[1]

            assert i_count == ITEM_NUM

        else:
            item_batch = range(ITEM_NUM)

            if drop_flag == False:
                rate_batch = sess.run(model.batch_ratings, {model.users: user_batch,
                                                              model.pos_items: item_batch})
            else:
                rate_batch = sess.run(model.batch_ratings, {model.users: user_batch,
                                                              model.pos_items: item_batch,
                                                              model.node_dropout: [0.] * len(eval(args.layer_size)),
                                                              model.mess_dropout: [0.] * len(eval(args.layer_size))})

        user_batch_rating_uid = zip(rate_batch, user_batch)
        # zip()函数用于将可迭代的对象作为参数，将两个对象中对应的元素打包成一个个元组，然后返回由这些元组组成的列表。
        batch_result = pool.map(test_one_user, user_batch_rating_uid) # Pool.map()仅接受一个迭代器参数，输出为字典组成的列表
        count += len(batch_result) # 测试用户数量

        for re in batch_result:
            result['precision'] += re['precision']/n_test_users # 将每个批次的测试结果进行累积，re是字典
            result['recall'] += re['recall']/n_test_users # 对应值为Ks长度的数组
            result['ndcg'] += re['ndcg']/n_test_users
            result['hit_ratio'] += re['hit_ratio']/n_test_users
            result['auc'] += re['auc']/n_test_users

    assert count == n_test_users # 检验测试用户是否全部测试完成
    pool.close() # 关闭进程池
    return result


# coding:utf-8
from PyQt6.QtCore import Qt, pyqtSignal, QUrl, QEvent
from PyQt6.QtGui import QDesktopServices
from PyQt6.QtWidgets import QWidget, QLabel, QVBoxLayout, QHBoxLayout, QFrame

from qfluentwidgets import (ScrollArea, PushButton, ToolButton, FluentIcon,
                            isDarkTheme, IconWidget, Theme, ToolTipFilter)
from ..common.icon import Icon
from ..common.config import cfg, FEEDBACK_URL, HELP_URL, EXAMPLE_URL

class ToolBar(QWidget):
    """ Tool bar """

    def __init__(self, title, subtitle, parent=None):
        super().__init__(parent=parent)
        self.titleLabel = QLabel(title, self)
        self.subtitleLabel = QLabel(subtitle, self)

        self.documentButton = PushButton(
            self.tr('Documentation'), self, Icon.DOCUMENT)
        self.sourceButton = PushButton(self.tr('Source'), self, FluentIcon.GITHUB)
        self.themeButton = ToolButton(Icon.CONSTRACT, self)
        self.feedbackButton = ToolButton(FluentIcon.FEEDBACK, self)

        self.vBoxLayout = QVBoxLayout(self)
        self.buttonLayout = QHBoxLayout()

        self.__initWidget()

    def __initWidget(self):
        self.setFixedHeight(138)
        self.vBoxLayout.setSpacing(0)
        self.vBoxLayout.setContentsMargins(36, 22, 36, 12)
        self.vBoxLayout.addWidget(self.titleLabel)
        self.vBoxLayout.addSpacing(4)
        self.vBoxLayout.addWidget(self.subtitleLabel)
        self.vBoxLayout.addSpacing(4)
        self.vBoxLayout.addLayout(self.buttonLayout, 1)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop)

        self.buttonLayout.setSpacing(4)
        self.buttonLayout.setContentsMargins(0, 0, 0, 0)
        self.buttonLayout.addWidget(self.documentButton, 0, Qt.AlignmentFlag.AlignLeft)
        self.buttonLayout.addWidget(self.sourceButton, 0, Qt.AlignmentFlag.AlignLeft)
        self.buttonLayout.addStretch(1)
        self.buttonLayout.addWidget(self.themeButton, 0, Qt.AlignmentFlag.AlignRight)
        self.buttonLayout.addWidget(self.feedbackButton, 0, Qt.AlignmentFlag.AlignRight)
        self.buttonLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft)

        self.themeButton.installEventFilter(ToolTipFilter(self.themeButton))
        self.feedbackButton.installEventFilter(
            ToolTipFilter(self.feedbackButton))
        self.themeButton.setToolTip(self.tr('Toggle theme'))
        self.feedbackButton.setToolTip(self.tr('Send feedback'))

        self.titleLabel.setObjectName('titleLabel')
        self.subtitleLabel.setObjectName('subtitleLabel')

        self.themeButton.clicked.connect(self.toggleTheme)
        self.documentButton.clicked.connect(
            lambda: QDesktopServices.openUrl(QUrl(HELP_URL)))
        self.sourceButton.clicked.connect(
            lambda: QDesktopServices.openUrl(QUrl(EXAMPLE_URL)))
        self.feedbackButton.clicked.connect(
            lambda: QDesktopServices.openUrl(QUrl(FEEDBACK_URL)))

    def toggleTheme(self):
        theme = Theme.LIGHT if isDarkTheme() else Theme.DARK
        cfg.set(cfg.themeMode, theme)

class ExampleCard(QWidget):
    """ Example card """

    def __init__(self, title, widget: QWidget, sourcePath, stretch=0, parent=None):
        super().__init__(parent=parent)
        self.widget = widget
        self.stretch = stretch

        self.titleLabel = QLabel(title, self)
        self.card = QFrame(self)

        self.sourceWidget = QFrame(self.card)
        self.sourcePath = sourcePath
        self.sourcePathLabel = QLabel(
            self.tr('Source code'), self.sourceWidget)
        self.linkIcon = IconWidget(FluentIcon.LINK, self.sourceWidget)

        self.vBoxLayout = QVBoxLayout(self)
        self.cardLayout = QVBoxLayout(self.card)
        self.topLayout = QHBoxLayout()
        self.bottomLayout = QHBoxLayout(self.sourceWidget)

        self.__initWidget()

    def __initWidget(self):
        self.linkIcon.setFixedSize(16, 16)
        self.__initLayout()

        self.sourceWidget.setCursor(Qt.CursorShape.PointingHandCursor)
        self.sourceWidget.installEventFilter(self)

        self.titleLabel.setObjectName('titleLabel')
        self.card.setObjectName('card')
        self.sourcePathLabel.setObjectName('sourcePathLabel')
        self.sourceWidget.setObjectName('sourceWidget')

    def __initLayout(self):
        self.vBoxLayout.setSizeConstraint(QVBoxLayout.SizeConstraint.SetMinimumSize)
        self.cardLayout.setSizeConstraint(QVBoxLayout.SizeConstraint.SetMinimumSize)
        self.topLayout.setSizeConstraint(QHBoxLayout.SizeConstraint.SetMinimumSize)

        self.vBoxLayout.setSpacing(12)
        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.topLayout.setContentsMargins(12, 12, 12, 12)
        self.bottomLayout.setContentsMargins(18, 18, 18, 18)
        self.cardLayout.setContentsMargins(0, 0, 0, 0)

        self.vBoxLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignTop)
        self.vBoxLayout.addWidget(self.card, 0, Qt.AlignmentFlag.AlignTop)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop)

        self.cardLayout.setSpacing(0)
        self.cardLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.cardLayout.addLayout(self.topLayout, 0)
        self.cardLayout.addWidget(self.sourceWidget, 0, Qt.AlignmentFlag.AlignBottom)

        self.widget.setParent(self.card)
        self.topLayout.addWidget(self.widget)
        if self.stretch == 0:
            self.topLayout.addStretch(1)

        self.widget.show()

        self.bottomLayout.addWidget(self.sourcePathLabel, 0, Qt.AlignmentFlag.AlignLeft)
        self.bottomLayout.addStretch(1)
        self.bottomLayout.addWidget(self.linkIcon, 0, Qt.AlignmentFlag.AlignRight)
        self.bottomLayout.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

    def eventFilter(self, obj, e):
        if obj is self.sourceWidget:
            if e.type() == QEvent.Type.MouseButtonRelease:
                QDesktopServices.openUrl(QUrl(self.sourcePath))

        return super().eventFilter(obj, e)

class GalleryInterface(ScrollArea):
    """ Gallery interface """

    def __init__(self, title: str, subtitle: str, parent=None):
        """
        Parameters
        ----------
        title: str
            The title of gallery

        subtitle: str
            The subtitle of gallery

        parent: QWidget
            parent widget
        """
        super().__init__(parent=parent)
        self.view = QWidget(self)
        self.toolBar = ToolBar(title, subtitle, self)
        self.vBoxLayout = QVBoxLayout(self.view)

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        self.setViewportMargins(0, self.toolBar.height(), 0, 0)
        self.setWidget(self.view)
        self.setWidgetResizable(True)

        self.vBoxLayout.setSpacing(30)
        self.vBoxLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.vBoxLayout.setContentsMargins(36, 20, 36, 36)

        self.__setQss()
        cfg.themeChanged.connect(self.__setQss)

    def addExampleCard(self, title, widget, sourcePath: str, stretch=0):
        card = ExampleCard(title, widget, sourcePath, stretch, self.view)
        self.vBoxLayout.addWidget(card, 0, Qt.AlignmentFlag.AlignTop)
        return card

    def scrollToCard(self, index: int):
        """ scroll to example card """
        w = self.vBoxLayout.itemAt(index).widget()
        self.verticalScrollBar().setValue(w.y())

    def resizeEvent(self, e):
        super().resizeEvent(e)
        self.toolBar.resize(self.width(), self.toolBar.height())

    def __setQss(self):
        self.view.setObjectName('view')
        theme = 'dark' if isDarkTheme() else 'light'
        with open(f'app/resource/qss/{theme}/gallery_interface.qss', encoding='utf-8') as f:
            self.setStyleSheet(f.read())


import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
plt.rcParams["font.size"] = 16

counts = []

with open("../build/braid_counts.txt", "r") as input_file:
    for _ in range(100):
        counts.append(int(input_file.readline().split()[1]))

fig, ax = plt.subplots(figsize = (7, 4))
plt.hist(counts, bins = range(1,101))
plt.xlim(1,101)
fig.subplots_adjust(left=0.08, right=0.95, bottom=0.16, top=0.95)
plt.xlabel("Number of homotopy classes")
plt.savefig("./histogram.pdf")
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 16 08:58:59 2021

@author: -
"""

import requests
import lxml.etree as etree

if __name__ == '__main__':
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'}
    url='https://www.taobao.com'
    url1='https://bj.58.com/ershoufang/'
    res=requests.get(url=url1, headers=headers)
    res.encoding=res.apparent_encoding
    # with open('../video-word/url-tb.txt','w',encoding='utf-8') as fp:
    #     fp.write(res.text)

    tree=etree.HTML(res.text)

    print(res.text)


import datetime
import os
from typing import List

from analyze_result import AnalyzeResult
from analyzer import Analyzer
# from cache import Cache
from api.fmg_api import FmgAPI
from sender import Sender
from stock_history_reader import StockHistoryReader
from telegram_bot import init_telegram_bot

'''
tasks:
-) post when `choosen` companies lost more than 3% of price for day
-) print by query /get
-) parse macrotrends (or any other) & compare P/E (take trailing p/e only)
-) simulation of algorithm from random data in the past & show profit from strategy
'''

init_telegram_bot()

def read_good_companies():
    with open("data/good_companies.txt") as fp:
        symbols: [str] = fp.readlines()
        symbols = [x.rstrip() for x in symbols]
        return symbols

def start():
    years = 15
    history = StockHistoryReader.read_history()
    tickers = history.get_tickers()

    result: List[AnalyzeResult] = []
    for ticker in tickers:
        now = datetime.datetime.now()  # be able to change this
        analyzer = Analyzer(ticker, history.get_history(ticker), now, years)
        is_good_company = analyzer.analyze()
        if is_good_company is True:
            pe: float = FmgAPI.get_quote_metrics(ticker)['pe']
            if pe is None or pe > 40:
                continue
            percent_pe_less_than_history = analyzer.get_percent_pe_less_than_history(pe)

            current_ev_over_ebitda = FmgAPI.get_key_metrics(ticker)['enterpriseValueOverEBITDATTM']
            if current_ev_over_ebitda is None:
                current_ev_over_ebitda = 100
            percent_ev_to_ebitda_less_than_history = analyzer.get_percent_ev_to_ebitda_less_than_history(
                current_ev_over_ebitda)

            analyze_result = AnalyzeResult(ticker, pe, percent_pe_less_than_history, current_ev_over_ebitda,
                                           percent_ev_to_ebitda_less_than_history)  # be able to get the old one
            result.append(analyze_result)

    print("Analysis is processed")

    result = sorted(result,
                    key=lambda cur: (cur.percent_pe_less_than_history is None, cur.percent_pe_less_than_history))
    # result = sorted(result, key=lambda cur: (
    #     cur.percent_ev_to_ebitda_less_than_history is None, cur.percent_ev_to_ebitda_less_than_history))

    for res in result:
        print(res.ticker, res.pe, res.percent_pe_less_than_history, res.current_ev_over_ebitda,
              res.percent_ev_to_ebitda_less_than_history)

    additional_info = []
    for ticker in result:
        additional_info.append("some additional info will be here later")

    Sender.send_result(result, additional_info)

if __name__ == '__main__':
    print("start main")
    print("FINANCIAL_MODELING_GREP_API:", os.environ['FINANCIAL_MODELING_GREP_API'])
    try:
        start()
    except:
        var = traceback.format_exc()
        print("Whew!", var, "occurred.")
    finally:
        print("Finished")
        os._exit(0)