import os
import scipy
from Wall import Wall
from Antenna import Antenna
from Ray_Tracing import RayTracing
from utils import *
from tqdm import tqdm
import time
import warnings
warnings.filterwarnings('ignore')

class Main:
    def __init__(self, wall_list=None, tx=None, rx=None, street='s',
                 draw=1, simu='heatmap', resolution=1, impulse_response=0, show=False, TX = [[0,0],[0,0]]):
        if simu != 'TX':
            plt.figure()
        self.start = time.time()
        self.impulse_response = impulse_response
        self.resolution = resolution
        self.draw = draw
        self.simu = simu
        self.street = street
        if wall_list is not None:
            self.wall_list = wall_list
        else:
            self.initialize_wall()
        if tx is not None:
            self.tx = tx
        else:
            self.tx = Antenna(5, 95, 'tx')
        if rx is not None:
            self.rx = rx
        else:
            if simu == 'LOS':
                self.rx = Antenna(25, 50, 'rx')
            elif simu == 'diff':
                self.rx = Antenna(75, 5, 'rx')
                #self.rx = Antenna(40, 55, 'rx')
            elif self.simu == 'plot_1D' or self.simu == 'path_loss':
                temp = []
                d = []
                x_ = np.arange(5, 50, self.resolution)
                for x in x_:
                    y = 105 - (90/45)*x
                    d.append(np.linalg.norm([x-self.tx.pos_x, y-self.tx.pos_y]))
                    temp.append(Antenna(x, y, 'rx', plot=True))
                self.rx = temp
                self.d = d
                self.draw = 0
            elif simu == 'heatmap':
                temp = []
                x_ = 15
                y_ = 15
                self.bound = [0.5-x_, 50.5+x_, 100.5+y_, 0.5-y_]
                self.d = [50 + 2*x_, 100 + 2*y_]
                nbr_rx = (round(self.d[0]/self.resolution) * round(self.d[1]/self.resolution))
                start_1 = time.time()
                with tqdm(total=nbr_rx)as pbar:
                    count = 0
                    for i in np.arange(self.bound[0], self.bound[1], self.resolution):
                        for j in np.arange(self.bound[2], self.bound[3], -self.resolution):
                            temp.append(Antenna(i, j, 'rx', plot=False))
                            pbar.update()
                            pbar.set_description("Generate rx %i" % count)
                            count += 1
                    print("\n generating rx took is {} min {} seconds\n".format(round((time.time() - start_1)/60), round((time.time() - start_1)%(60))))
                    self.rx = temp
                    self.draw = 0
            elif simu == 'TX':
                temp = []
                x_ = 15
                y_ = 15
                self.bound = [0.5-x_, 50.5+x_, 100.5+y_, 0.5-y_]
                self.d = [50 + 2*x_, 100 + 2*y_]
                nbr_rx = (round(self.d[0]/self.resolution) * round(self.d[1]/self.resolution))
                start_1 = time.time()
                with tqdm(total=nbr_rx)as pbar:
                    count = 0
                    for i in np.arange(self.bound[0], self.bound[1], self.resolution):
                        for j in np.arange(self.bound[2], self.bound[3], -self.resolution):
                            temp.append(Antenna(i, j, 'rx', plot=False))
                            pbar.update()
                            pbar.set_description("Generate rx %i" % count)
                            count += 1
                    print("\n generating rx took is {} min {} seconds\n".format(round((time.time() - start_1)/60), round((time.time() - start_1)%(60))))
                    self.rx = temp
                    self.tx = [Antenna(TX[0][0], TX[0][1], 'tx'), Antenna(TX[1][0], TX[1][1], 'tx')]
                    self.draw = 0
        self.rays = None
        self.compute_ray()
        self.show()
        if show:
            plt.show()

    def initialize_wall(self):
        data = np.reshape(np.loadtxt('grand_place.txt', delimiter=None, dtype=int), (-1, 5))
        temp = []
        for i in range(data.shape[0]):
            if data[i, 4] == 1:
                street = 'c'
            else:
                street = 's'
            temp.append(Wall(data[i, 0], -data[i, 1]+100, data[i, 2], -data[i, 3]+100, street))
        self.wall_list = temp

    def check_in_map(self, rx):
        in_map = False
        rx = [rx.pos_x, rx.pos_y]
        if 0 < rx[0] < 50:
            if 40 < rx[0] < 50:
                if 0 < rx[1] < 128:
                    in_map = True
            elif 0 < rx[1] < 100:
                in_map = True

        elif -28 < rx[0] < 0:
            if 0 < rx[1] < 10 or 25 < rx[1] < 35 or 90 < rx[1] < 100:
                in_map = True
        elif 50 < rx[0] < 78:
            if 0 < rx[1] < 10 or 25 < rx[1] < 35 or 65 < rx[1] < 75:
                in_map = True
        else:
            in_map =False
        if self.simu =='TX':
            for i in range(len(self.tx)):
                if np.linalg.norm(np.subtract(self.tx[i].P, rx)) < 10:
                    in_map = False
        else :
            if np.linalg.norm(np.subtract(self.tx.P, rx)) < 10:
                in_map = False

        return in_map

    def compute_ray(self):
        if self.simu == 'LOS' or self.simu == 'diff':
            obj = RayTracing(self.wall_list, self.tx, self.rx, street=self.street, draw=self.draw)
            obj.ray_tracer()
            self.rays = obj.get_rays()
        elif self.simu == 'plot_1D' or self.simu == 'heatmap' or self.simu == 'path_loss':
            nbr_rx = len(self.rx)
            self.big_mat = np.zeros((nbr_rx, 6))  # P_RX_dBm, SNR_dB, K_dB, delay_spread, x, y
            start = time.time()
            with tqdm(total=nbr_rx) as pbar:
                for i in range(nbr_rx):
                    if self.check_in_map(self.rx[i]):
                        try:
                            obj = RayTracing(self.wall_list, self.tx, self.rx[i], street=self.street, draw=self.draw)
                            obj.ray_tracer()
                            self.rays = obj.get_rays()
                            [P_RX_dBm, SNR_dB, K_dB, delay_spread] = compute_properties(self.rays)
                            self.big_mat[i, :] = [P_RX_dBm, SNR_dB, K_dB, delay_spread, self.rx[i].pos_x, self.rx[i].pos_y]
                        except:
                            self.big_mat[i, :] = [None, None, None, None, self.rx[i].pos_x, self.rx[i].pos_y]
                    else:
                        self.big_mat[i, :] = [None, None, None, None, self.rx[i].pos_x, self.rx[i].pos_y]

                    pbar.set_description("Compute rays for wall %i" % i)
                    pbar.update()
            np.save('big_mat', self.big_mat)
            self.big_mat = np.load('big_mat.npy')
            print("\ncomputing rays take is {} min {} seconds\n".format(round((time.time() - start)/60), round((time.time() - start)%(60))))
        else:
            nbr_rx = len(self.rx)
            nbr_tx = len(self.tx)
            self.big_mat = np.zeros((nbr_rx, 4))  # P_RX_dBm, SNR_dB, K_dB, delay_spread, x, y
            start = time.time()

            with tqdm(total=nbr_rx*nbr_tx) as pbar:
                for i in range(nbr_rx):
                    ray_ = []
                    for j in range(nbr_tx):
                        if self.check_in_map(self.rx[i]) and self.big_mat[i, 0] != None:

                            obj = RayTracing(self.wall_list, self.tx[j], self.rx[i], street=self.street, draw=self.draw)
                            obj.ray_tracer()
                            self.rays = obj.get_rays()
                            for k in range(len(self.rays)):
                                ray_.append(self.rays[k])
                            if j == nbr_tx-1 :
                                [P_RX_dBm, SNR_dB, K_dB, delay_spread] = compute_properties(np.reshape(np.array(ray_), -1))
                                self.big_mat[i, :] = [P_RX_dBm, SNR_dB, K_dB, delay_spread]

                        else:
                            self.big_mat[i, :] = [None, None, None, None]

                        pbar.set_description("Compute rays for wall %i" % i)
                        pbar.update()
            np.save('big_mat', self.big_mat)
            idx = np.where(self.big_mat[:, 1] == self.big_mat[:, 1])
            pos = np.where(self.big_mat[idx, 1] >= 5)
            pos_ratio = len(self.big_mat[pos,1].T)
            ratio = pos_ratio /len(self.big_mat[idx,1].T)
            self.SNR_mean = np.mean(self.big_mat[idx, 1])
            self.ratio = ratio
            print("\ncomputing rays take is {} min {} seconds\n".format(round((time.time() - start) / 60),
                                                                        round((time.time() - start) % (60))))

    def show(self):
        if self.simu == 'diff' or self.simu == 'LOS':
            plt.title('Ray Tracing for tx ({},{}) and rx ({},{})'.format(self.tx.pos_x,
                                                                         self.tx.pos_y,
                                                                         self.rx.pos_x,
                                                                         self.rx.pos_y))
            [P_RX_dBm, SNR_dB, K_dB, delay_spread] = compute_properties(self.rays, impulse_response=self.impulse_response)
            plt.text(51, 80, 'P_RX_dBm = ' + str(round(P_RX_dBm, 2)) + '\n'+
                      'SNR_dB =' + str(round(SNR_dB, 2)) + '\n' +
                      'K_dB = ' + ['-' if K_dB is None else str(round(K_dB, 2))][0] + '\n' +
                      'delay_spread = ' + str(round(delay_spread, 10)) + '\n', color='red')
            print(" \ntotal time taken is is {} min {} seconds\n".format(round((time.time() - self.start)/60), round((time.time() - self.start)%(60))))

            plt.show(block=False)
        elif self.simu == 'plot_1D':
            idx = 0
            for feat in ['P_RX_dBm', 'SNR_dB', 'K_dB', 'delay_spread']:
                plt.figure()
                data = self.big_mat[:, idx]
                plt.plot(self.d, data)
                plt.title(feat + ' from tx = ({},{}) until ({},{})'.format(round(self.tx.pos_x, 1), round(self.tx.pos_y, 1), round(self.rx[-1].pos_x, 1), round(self.rx[-1].pos_y, 1)))
                plt.xlabel('distance [m]')
                plt.ylabel(feat)
                plt.savefig('output/' + feat + '_plot_1D.png')
                idx += 1
            plt.show(block=False)
        elif self.simu == 'path_loss':
            plt.figure()
            idx = np.where(self.big_mat[:, 0] == self.big_mat[:, 0])  # remove nan value
            P_RX_dBm = self.big_mat[idx, 0][0, :]
            self.d = np.array(self.d)[idx]
            [m, p] = np.polyfit(np.log10(self.d), P_RX_dBm, 1)
            fitted_P_RX_dBm = m*np.log10(self.d)+p
            plt.plot(np.log10(self.d), fitted_P_RX_dBm)
            plt.plot(np.log10(self.d), P_RX_dBm)
            plt.title('power receive  from tx = ({},{}) until ({},{})'.format(round(self.tx.pos_x, 1), round(self.tx.pos_y, 1),
                                                                       round(self.rx[-1].pos_x, 1),
                                                                       round(self.rx[-1].pos_y, 1)))
            plt.legend(['Fitted data', 'Original data'])
            plt.xlabel('log(d)')
            plt.ylabel('P_RX_dBm')
            plt.savefig('output/' + 'regression.png')
            plt.show(block=False)
            plt.figure()
            P_tx_dBm = 10 * np.log10(P_TX / 0.001)
            path_loss = P_tx_dBm - P_RX_dBm
            path_loss_linear = P_tx_dBm - fitted_P_RX_dBm
            plt.plot(np.log10(self.d), path_loss_linear)
            plt.plot(np.log10(self.d), path_loss)
            plt.legend(['Fitted data', 'Original data'])
            plt.title('path loss from tx = ({},{}) until ({},{})'.format(round(self.tx.pos_x, 1),
                                                                              round(self.tx.pos_y, 1),
                                                                              round(self.rx[-1].pos_x, 1),
                                                                              round(self.rx[-1].pos_y, 1)))
            plt.xlabel('log(d)')
            plt.ylabel('path_loss')
            plt.savefig('output/' + 'path_loss.png')
            plt.show(block=False)

            #PATH LOSS WITH GLOBAL FADING

            sigma_L = np.std(path_loss - path_loss_linear)
            plt.figure()
            plt.plot(np.log10(self.d), path_loss - path_loss_linear)
            plt.plot([np.log10(self.d).min(), np.log10(self.d).max()], [sigma_L, sigma_L], color='red')
            plt.plot([np.log10(self.d).min(), np.log10(self.d).max()], [-sigma_L, -sigma_L], color='red')
            plt.legend(['path_loss', 'Variability '])
            plt.title('Fadind variability')
            plt.xlabel('log(d)')
            plt.ylabel('Zero-mean power in dBm')
            plt.savefig('output/' + 'path_loss_variability.png')
            plt.show(block=False)
            plt.figure()
            L_sigma = np.log10(np.random.lognormal(0, sigma_L, len(path_loss)))
            path_loss_model = path_loss_linear + L_sigma
            plt.plot(np.log10(self.d), path_loss_model)
            plt.title("Path loss model with shadowing")
            plt.xlabel("log(d)")
            plt.ylabel("L [dB]")

            [m1, p1] = np.polyfit(np.log10(self.d), path_loss_model, 1)
            path_loss_linear_final = m1 * np.log10(self.d)+p1
            plt.plot(np.log10(self.d), path_loss_linear_final)

            #  Cell range as a function of connection probability
            n = m1/10
            Pr = np.arange(0.1, 1, 0.01)
            gamma = np.zeros(len(Pr))
            L_r = np.zeros(len(Pr))
            Lm = compute_Lm()
            for i in range(len(Pr)):
                gamma[i] = scipy.special.erfcinv((Pr[i])*2)*sigma_L*np.sqrt(2)
                L_r[i] = Lm + gamma[i]
            d_0 = self.d[0]
            L0_d0 = path_loss_linear[0]
            R = d_0 * 10**((L_r - L0_d0) / (10 * n))
            plt.show(block=False)
            plt.savefig('output/' + 'Path_loss_model_with_shadowing.png')
            plt.figure()
            plt.plot(Pr, R)
            plt.xlabel('Connection probability')
            plt.ylabel('Cell range (m)')
            plt.title('Cell range as a function or the connection probability')
            plt.savefig('output/' + 'Cell range as a function or the connection probability')
            plt.show(block=False)
            plt.figure()
            plt.plot(Pr, L_r)
            plt.xlabel('Connection probability')
            plt.ylabel('Path loss at cell edge L(R)')
            plt.title('Path loss at cell edge L(R) as a function or the connection probability')
            plt.savefig('output/' + 'Path loss at cell edge L(R) as a function or the connection probability')
            plt.show(block=False)
        else:
            start3 = time.time()
            with tqdm(total=4) as pbar:
                idx = 0
                for feat in ['P_RX_dBm', 'SNR_dB', 'K_dB', 'delay_spread']:
                    pbar.update()
                    pbar.set_description("Generate heatmap for %s" % feat)
                    try:
                        data = np.reshape(self.big_mat[:, idx], (round(self.d[0]/self.resolution),-1 ))#round(self.d[1]/self.resolution)
                    except ValueError:
                        data = np.reshape(self.big_mat[:, idx], (-1, -round(self.d[1]/self.resolution)))
                    fig, ax = plt.subplots()
                    im = ax.imshow(data.T)
                    plt.colorbar(im, ax=ax)
                    plt.title('heatmap for ' + feat)
                    path = 'output/' + 'res =' + str(self.resolution) + '/'
                    if not os.path.exists(path):
                        os.makedirs(path)
                    plt.savefig(path + self.simu+ '_'+ feat)

                    plt.show(block=False)
                    idx += 1
            print("\nplotting took is {} min {} seconds\n".format(round((time.time() - start3)/60), round((time.time() - start3)%(60*60))))
            print("\ntotal time taken is {} min {} seconds\n".format(round((time.time() - self.start) / 60),
                                                                     round((time.time() - self.start) % 60)))

resolution = 0.3
Main(simu='LOS', impulse_response=1)
Main(simu='plot_1D', resolution=resolution)
Main(simu='heatmap', resolution=resolution)
Main(simu='path_loss', resolution=resolution)
Main(simu='diff')
Main(simu='plot_1D', resolution=resolution)
Main(simu='TX', resolution=resolution, TX=[[25, 80], [25, 20]])
plt.show()

# Brute force approach
'''best_snr = [-1000]
for x1 in range(1, 49, resolution):
    for y1 in range(1, 99, resolution):
        for x2 in range(1, 49, resolution):
            for y2 in range(1, 99, resolution):
                if np.linalg.norm([abs(x2-x1), abs(y2-y1)]) >= 10:
                    simu = Main(simu='TX', resolution=resolution, TX=[[x1, y1], [x2, y2]])
                    snr = simu.ratio
                    if snr > best_snr[0]:
                        best_snr = [snr, x1, y1, x2, y2]
                        print(best_snr)
np.save('best_snr', best_snr)'''


from gevent import monkey

# monkey.patch_all()
from volttron.server.serviceloader import init_services

monkey.patch_socket()
monkey.patch_ssl()

import argparse
import logging
from logging import handlers
import logging.config
from urllib.parse import urlparse

import os
from pathlib import Path
import resource
import stat
import sys
import threading

import gevent

# import gevent.monkey
# import gevent.threading as threading
#
from volttron.utils import ClientContext as cc
from volttron.utils.keystore import get_random_key

# gevent.monkey.patch_socket()
# gevent.monkey.patch_ssl()
import zmq
from zmq import green

# Link to the volttron-client library
from volttron.utils import decode_key, encode_key, get_version

# Create a context common to the green and non-green zmq modules.
green.Context._instance = green.Context.shadow(zmq.Context.instance().underlying)

# from .vip.router import *
# from .vip.socket import decode_key, encode_key, Address
# from .vip.tracking import Tracker

from volttron.client.known_identities import (
    PLATFORM_WEB,
    CONTROL,
    CONTROL_CONNECTION,
)
from volttron.utils import store_message_bus_config
from volttron.utils.keystore import KeyStore, KnownHostsStore
from volttron.utils.persistance import load_create_store

from volttron.server.tracking import Tracker
from volttron.types.server_config import ServiceConfigs, ServerConfig
# TODO rmq
# from .vip.rmq_router import RMQRouter

# from volttron.utils.rmq_setup import start_rabbit
# from volttron.utils.rmq_config_params import RMQConfig

from volttron.server.router import Router
from volttron.types.peer import ServicePeerNotifier
from volttron.services.auth.auth_service import AuthFile, AuthEntry, AuthFileUserIdAlreadyExists

# TODO Key Discovery RPC
# from ..services.external import ExternalRPCService, KeyDiscoveryAgent
# from ..services.routing import ZMQProxyRouter

try:
    from services.web import PlatformWebService

    HAS_WEB = True
except ImportError:
    HAS_WEB = False

from volttron.server.log_actions import log_to_file, configure_logging, LogLevelAction
from volttron.server import server_argparser as config, aip

_log = logging.getLogger(os.path.basename(sys.argv[0]) if __name__ == "__main__" else __name__)

# Only show debug on the platform when really necessary!
log_level_info = (
    'volttron.platform.main',
    'volttron.platform.vip.zmq_connection',
    'urllib3.connectionpool',
    'watchdog.observers.inotify_buffer',
    'volttron.platform.auth',
    'volttron.platform.store',
    'volttron.platform.control',
    'volttron.platform.vip.agent.core',
    'volttron.utils',
    'volttron.platform.vip.router',
    'vip.router',
    'volttron.server.router.router'
)

for log_name in log_level_info:
    logging.getLogger(log_name).setLevel(logging.INFO)

# No need for str after python 3.8
VOLTTRON_INSTANCES = Path("~/.volttron_instances").expanduser().resolve()

def start_volttron_process(opts):
    """Start the main volttron process.

    Typically, this function is used from main.py and just uses the argparser's
    Options arguments as inputs.   It also can be called with a dictionary.  In
    that case the dictionaries keys are mapped into a value that acts like the
    args options.
    """
    if isinstance(opts, dict):
        opts = type("Options", (), opts)()

    # Change working dir
    os.chdir(opts.volttron_home)

    # vip_address is meant to be a list so make it so.
    if not isinstance(opts.vip_address, list):
        opts.vip_address = [opts.vip_address]
    if opts.log:
        opts.log = config.expandall(opts.log)
    if opts.log_config:
        opts.log_config = config.expandall(opts.log_config)

    # Configure logging
    level = max(1, opts.verboseness)
    if opts.monitor and level > logging.INFO:
        level = logging.INFO

    if opts.log is None:
        log_to_file(sys.stderr, level)
    elif opts.log == "-":
        log_to_file(sys.stdout, level)
    elif opts.log:
        log_to_file(opts.log, level, handler_class=handlers.WatchedFileHandler)
    else:
        log_to_file(None, 100, handler_class=lambda x: logging.NullHandler())

    if opts.log_config:
        with open(opts.log_config, "r") as f:
            for line in f.readlines():
                _log.info(line.rstrip())

        error = configure_logging(opts.log_config)

        if error:
            _log.error("{}: {}".format(*error))
            sys.exit(1)

    if opts.secure_agent_users == "True":
        _log.info("VOLTTRON starting in secure mode")
        os.umask(0o007)
    else:
        opts.secure_agent_users = "False"

    opts.publish_address = config.expandall(opts.publish_address)
    opts.subscribe_address = config.expandall(opts.subscribe_address)
    opts.vip_address = [config.expandall(addr) for addr in opts.vip_address]
    opts.vip_local_address = config.expandall(opts.vip_local_address)
    opts.message_bus = config.expandall(opts.message_bus)
    if opts.web_ssl_key:
        opts.web_ssl_key = config.expandall(opts.web_ssl_key)
    if opts.web_ssl_cert:
        opts.web_ssl_cert = config.expandall(opts.web_ssl_cert)

    if opts.web_ssl_key and not opts.web_ssl_cert:
        raise Exception("If web-ssl-key is specified web-ssl-cert MUST be specified.")
    if opts.web_ssl_cert and not opts.web_ssl_key:
        raise Exception("If web-ssl-cert is specified web-ssl-key MUST be specified.")

    if opts.web_ca_cert:
        assert os.path.isfile(opts.web_ca_cert), "web_ca_cert does not exist!"
        os.environ["REQUESTS_CA_BUNDLE"] = opts.web_ca_cert

    # Removed the check for opts.web_ca_cert to be the same cert that was used to create web_ssl_key
    # and opts.web_ssl_cert

    os.environ["MESSAGEBUS"] = opts.message_bus
    os.environ["SECURE_AGENT_USERS"] = opts.secure_agent_users
    if opts.instance_name is None:
        if len(opts.vip_address) > 0:
            opts.instance_name = opts.vip_address[0]

    _log.debug("opts.instancename {}".format(opts.instance_name))
    if opts.instance_name:
        store_message_bus_config(opts.message_bus, opts.instance_name)
    else:
        # if there is no instance_name given get_platform_instance_name will
        # try to retrieve from config or default a value and store it in the config
        cc.get_instance_name()

    # Log configuration options
    if getattr(opts, "show_config", False):
        _log.info("volttron version: {}".format(get_version()))
        for name, value in sorted(vars(opts).items()):
            _log.info("%s: %s" % (name, str(repr(value))))

    # Increase open files resource limit to max or 8192 if unlimited
    try:
        soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)

    except OSError:
        _log.exception("error getting open file limits")
    else:
        if soft != hard and soft != resource.RLIM_INFINITY:
            try:
                limit = 8192 if hard == resource.RLIM_INFINITY else hard
                resource.setrlimit(resource.RLIMIT_NOFILE, (limit, hard))
            except OSError:
                _log.exception("error setting open file limits")
            else:
                _log.debug(
                    "open file resource limit increased from %d to %d",
                    soft,
                    limit,
                )
        _log.debug("open file resource limit %d to %d", soft, hard)

    opts.aip = aip.AIPplatform(opts)
    opts.aip.setup()

    # Check for secure mode/permissions on VOLTTRON_HOME directory
    mode = os.stat(opts.volttron_home).st_mode
    if mode & (stat.S_IWGRP | stat.S_IWOTH):
        _log.warning("insecure mode on directory: %s", opts.volttron_home)
    # Get or generate encryption key
    keystore = KeyStore()
    _log.debug("using key-store file %s", keystore.filename)
    if not keystore.isvalid():
        _log.warning("key store is invalid; connections may fail")
    st = os.stat(keystore.filename)
    if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO):
        _log.warning("insecure mode on key file")
    publickey = decode_key(keystore.public)
    opts.volttron_publickey = keystore.public
    if publickey:
        # Authorize the platform key:
        entry = AuthEntry(
            credentials=encode_key(publickey),
            user_id="platform",
            capabilities=[{
                "edit_config_store": {
                    "identity": "/.*/"
                }
            }],
            comments="Automatically added by platform on start",
        )
        AuthFile().add(entry, overwrite=True)
        # Add platform key to known-hosts file:
        known_hosts = KnownHostsStore()
        known_hosts.add(opts.vip_local_address, encode_key(publickey))
        for addr in opts.vip_address:
            known_hosts.add(addr, encode_key(publickey))
    secretkey = decode_key(keystore.secret)

    # Add the control.connection so that volttron-ctl can access the bus
    control_conn_path = KeyStore.get_agent_keystore_path(CONTROL_CONNECTION)
    os.makedirs(os.path.dirname(control_conn_path), exist_ok=True)
    ks_control_conn = KeyStore(KeyStore.get_agent_keystore_path(CONTROL_CONNECTION))
    entry = AuthEntry(
        credentials=encode_key(decode_key(ks_control_conn.public)),
        user_id=CONTROL_CONNECTION,
        capabilities=[
            {
                "edit_config_store": {
                    "identity": "/.*/"
                }
            },
            "allow_auth_modifications",
        ],
        comments="Automatically added by platform on start",
    )
    try:
        AuthFile().add(entry)
    except AuthFileUserIdAlreadyExists:
        pass

    # The following line doesn't appear to do anything, but it creates
    # a context common to the green and non-green zmq modules.
    zmq.Context.instance()    # DO NOT REMOVE LINE!!
    # zmq.Context.instance().set(zmq.MAX_SOCKETS, 2046)

    tracker = Tracker()
    protected_topics_file = os.path.join(opts.volttron_home, "protected_topics.json")
    _log.debug("protected topics file %s", protected_topics_file)
    external_address_file = os.path.join(opts.volttron_home, "external_address.json")
    _log.debug("external_address_file file %s", external_address_file)
    protected_topics = {}
    if opts.agent_monitor_frequency:
        try:
            int(opts.agent_monitor_frequency)
        except ValueError as e:
            raise ValueError("agent-monitor-frequency should be integer "
                             "value. Units - seconds. This determines how "
                             "often the platform checks for any crashed agent "
                             "and attempts to restart. {}".format(e))

    # Allows registration agents to callbacks for peers
    notifier = ServicePeerNotifier()

    # Main loops
    def zmq_router(stop):
        try:
            _log.debug("Running zmq router")
            Router(
                opts.vip_local_address,
                opts.vip_address,
                secretkey=secretkey,
                publickey=publickey,
                default_user_id="vip.service",
                monitor=opts.monitor,
                tracker=tracker,
                volttron_central_address=opts.volttron_central_address,
                volttron_central_serverkey=opts.volttron_central_serverkey,
                instance_name=opts.instance_name,
                bind_web_address=opts.bind_web_address,
                protected_topics=protected_topics,
                external_address_file=external_address_file,
                msgdebug=opts.msgdebug,
                service_notifier=notifier,
            ).run()
        except Exception:
            _log.exception("Unhandled exception in router loop")
            raise
        except KeyboardInterrupt:
            pass
        finally:
            _log.debug("In finally")
            stop(platform_shutdown=True)

    address = "inproc://vip"
    pid_file = os.path.join(opts.volttron_home, "VOLTTRON_PID")
    try:
        protected_topics = {}
        proxy_router = None
        proxy_router_task = None

        _log.debug("********************************************************************")
        _log.debug("VOLTTRON PLATFORM RUNNING ON {} MESSAGEBUS".format(opts.message_bus))
        _log.debug("********************************************************************")

        server_config = ServerConfig()
        server_config.opts = opts
        server_config.internal_address = address
        server_config.aip = opts.aip

        server_config.auth_file = Path(opts.volttron_home).joinpath("auth.json")
        server_config.protected_topics_file = Path(
            opts.volttron_home).joinpath("protected_topics.json")

        # The return value will be added to the service_config.yml file in order to pass in
        # the expected defaults. From there the user may choose to modify the defaults.
        service_config_file = Path(opts.volttron_home).joinpath("service_config.yml")
        service_configs = ServiceConfigs(service_config_file=service_config_file,
                                         server_config=server_config)
        service_configs.init_services(server_config=server_config)

        # This variable will hold the executing services to determine when one of the services
        # dies or the platform has been shutdown.
        spawned_greenlets = []

        # TODO Replace with module level zmq that holds all of the zmq bits in order to start and
        #  run the message bus regardless of whether it's zmq or rmq.
        if opts.message_bus == "zmq":
            # first service loaded must be the config store
            config_store = service_configs.get_service_instance("volttron.services.config_store")

            # start it up before anything else
            event = gevent.event.Event()
            task = gevent.spawn(config_store.core.run, event)
            event.wait()
            del event
            spawned_greenlets.append(task)

            # if we have an auth service it should be started before the
            # zmq router.

            auth_service = service_configs.get_service_instance("volttron.services.auth")
            if auth_service is None:
                _log.warning("Auth service disabled.")

            if auth_service:
                event = gevent.event.Event()
                task = gevent.spawn(auth_service.core.run, event)
                event.wait()
                del event
                spawned_greenlets.append(task)

            # Start ZMQ router in separate thread to remain responsive
            thread = threading.Thread(target=zmq_router, args=(config_store.core.stop, ))
            thread.daemon = True
            thread.start()

            gevent.sleep(0.1)
            if not thread.is_alive():
                sys.exit()

        # TODO Better make this so that it removes instances from this file or it will just be an
        #  ever increasing file depending on the number of instances it could get quite large.
        # The instance file is where we are going to record the instance and
        # its details according to
        instance_file = str(VOLTTRON_INSTANCES)
        try:
            instances = load_create_store(instance_file)
        except ValueError:
            os.remove(instance_file)
            instances = load_create_store(instance_file)
        this_instance = instances.get(opts.volttron_home, {})
        this_instance["pid"] = os.getpid()
        this_instance["version"] = get_version()
        # note vip_address is a list
        this_instance["vip-address"] = opts.vip_address
        this_instance["volttron-home"] = opts.volttron_home
        this_instance["volttron-root"] = os.path.abspath("../../..")
        this_instance["start-args"] = sys.argv[1:]
        instances[opts.volttron_home] = this_instance
        instances.async_sync()

        protected_topics_file = os.path.join(opts.volttron_home, "protected_topics.json")
        _log.debug("protected topics file %s", protected_topics_file)
        external_address_file = os.path.join(opts.volttron_home, "external_address.json")
        _log.debug("external_address_file file %s", external_address_file)

        # Auth and config store services have already been run, so we can run the others now.
        for svc_name in service_configs.get_service_names():
            if svc_name not in ('volttron.services.auth', 'volttron.services.config_store'):
                _log.debug(f"Starting service: {svc_name}")
                obj = service_configs.get_service_instance(svc_name)
                event = gevent.event.Event()
                task = gevent.spawn(obj.core.run, event)
                event.wait()
                spawned_greenlets.append(task)

        control_service = service_configs.get_service_instance("volttron.services.control")

        entry = AuthEntry(
            credentials=control_service.core.publickey,
            user_id=CONTROL,
            capabilities=[
                {
                    "edit_config_store": {
                        "identity": "/.*/"
                    }
                },
                "allow_auth_modifications",
            ],
            comments="Automatically added by platform on start",
        )
        try:
            AuthFile().add(entry)
        except AuthFileUserIdAlreadyExists:
            pass

        # # TODO Key discovery agent add in.
        # # KeyDiscoveryAgent(
        # #     address=address,
        # #     serverkey=publickey,
        # #     identity=KEY_DISCOVERY,
        # #     external_address_config=external_address_file,
        # #     setup_mode=opts.setup_mode,
        # #     bind_web_address=opts.bind_web_address,
        # #     enable_store=False,
        # #     message_bus="zmq",
        # # ),
        # ]

        health_service = service_configs.get_service_instance("volttron.services.health")
        if health_service is not None:
            notifier.register_peer_callback(health_service.peer_added, health_service.peer_dropped)

        # Auto-start agents now that all services are up
        if opts.autostart:
            for name, error in opts.aip.autostart():
                _log.error("error starting {!r}: {}\n".format(name, error))

        # Done with all start up process write a PID file

        with open(pid_file, "w+") as f:
            f.write(str(os.getpid()))

        # Wait for any service to stop, signaling exit
        try:
            gevent.wait(spawned_greenlets, count=1)
        except KeyboardInterrupt:
            _log.info("SIGINT received; shutting down")
        finally:
            sys.stderr.write("Shutting down.\n")
            if proxy_router_task:
                proxy_router.core.stop()
            _log.debug("Kill all service agent tasks")
            for task in spawned_greenlets:
                task.kill(block=False)
            gevent.wait(spawned_greenlets)
    except Exception as e:
        _log.error(e)
        import traceback

        _log.error(traceback.print_exc())
    finally:
        _log.debug("AIP finally")
        opts.aip.finish()
        instance_file = str(VOLTTRON_INSTANCES)
        try:
            instances = load_create_store(instance_file)
            instances.pop(opts.volttron_home, None)
            instances.sync()
            if os.path.exists(pid_file):
                os.remove(pid_file)
        except Exception:
            _log.warning(f"Unable to load {VOLTTRON_INSTANCES}")
        _log.debug("********************************************************************")
        _log.debug("VOLTTRON PLATFORM HAS SHUTDOWN")
        _log.debug("********************************************************************")

def main(argv=sys.argv):
    # Refuse to run as root
    if not getattr(os, "getuid", lambda: -1)():
        sys.stderr.write("%s: error: refusing to run as root to prevent "
                         "potential damage.\n" % os.path.basename(argv[0]))
        sys.exit(77)

    volttron_home = os.path.normpath(
        config.expandall(os.environ.get("VOLTTRON_HOME", "~/.volttron")))
    os.environ["VOLTTRON_HOME"] = volttron_home
    # Setup option parser
    parser = config.ArgumentParser(
        prog=os.path.basename(argv[0]),
        add_help=False,
        description="VOLTTRON platform service",
        usage="%(prog)s [OPTION]...",
        argument_default=argparse.SUPPRESS,
        epilog="Boolean options, which take no argument, may be inversed by "
        "prefixing the option with no- (e.g. --autostart may be "
        "inversed using --no-autostart).",
    )
    parser.add_argument(
        "-c",
        "--config",
        metavar="FILE",
        action="parse_config",
        ignore_unknown=False,
        sections=[None, "volttron"],
        help="read configuration from FILE",
    )
    parser.add_argument(
        "-l",
        "--log",
        metavar="FILE",
        default=None,
        help="send log output to FILE instead of stderr",
    )
    parser.add_argument(
        "-L",
        "--log-config",
        metavar="FILE",
        help="read logging configuration from FILE",
    )
    parser.add_argument(
        "--log-level",
        metavar="LOGGER:LEVEL",
        action=LogLevelAction,
        help="override default logger logging level",
    )
    parser.add_argument(
        "--monitor",
        action="store_true",
        help="monitor and log connections (implies -v)",
    )
    parser.add_argument(
        "-q",
        "--quiet",
        action="add_const",
        const=10,
        dest="verboseness",
        help="decrease logger verboseness; may be used multiple times",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        action="add_const",
        const=-10,
        dest="verboseness",
        help="increase logger verboseness; may be used multiple times",
    )
    parser.add_argument(
        "--verboseness",
        type=int,
        metavar="LEVEL",
        default=logging.WARNING,
        help="set logger verboseness",
    )
    # parser.add_argument(
    #    '--volttron-home', env_var='VOLTTRON_HOME', metavar='PATH',
    #    help='VOLTTRON configuration directory')
    parser.add_argument("--show-config", action="store_true", help=argparse.SUPPRESS)
    parser.add_help_argument()
    parser.add_version_argument(version="%(prog)s " + str(get_version()))

    agents = parser.add_argument_group("agent options")
    agents.add_argument(
        "--autostart",
        action="store_true",
        inverse="--no-autostart",
        help="automatically start enabled agents and services",
    )
    agents.add_argument(
        "--no-autostart",
        action="store_false",
        dest="autostart",
        help=argparse.SUPPRESS,
    )
    agents.add_argument(
        "--publish-address",
        metavar="ZMQADDR",
        help="ZeroMQ URL used for pre-3.x agent publishing (deprecated)",
    )
    agents.add_argument(
        "--subscribe-address",
        metavar="ZMQADDR",
        help="ZeroMQ URL used for pre-3.x agent subscriptions (deprecated)",
    )
    agents.add_argument(
        "--vip-address",
        metavar="ZMQADDR",
        action="append",
        default=[],
        help="ZeroMQ URL to bind for VIP connections",
    )
    agents.add_argument(
        "--vip-local-address",
        metavar="ZMQADDR",
        help="ZeroMQ URL to bind for local agent VIP connections",
    )
    agents.add_argument(
        "--bind-web-address",
        metavar="BINDWEBADDR",
        default=None,
        help="Bind a web server to the specified ip:port passed",
    )
    agents.add_argument(
        "--web-ca-cert",
        metavar="CAFILE",
        default=None,
        help=
        "If using self-signed certificates, this variable will be set globally to allow requests"
        "to be able to correctly reach the webserver without having to specify verify in all calls.",
    )
    agents.add_argument(
        "--web-secret-key",
        default=None,
        help="Secret key to be used instead of https based authentication.",
    )
    agents.add_argument(
        "--web-ssl-key",
        metavar="KEYFILE",
        default=None,
        help="ssl key file for using https with the volttron server",
    )
    agents.add_argument(
        "--web-ssl-cert",
        metavar="CERTFILE",
        default=None,
        help="ssl certficate file for using https with the volttron server",
    )
    agents.add_argument(
        "--volttron-central-address",
        default=None,
        help="The web address of a volttron central install instance.",
    )
    agents.add_argument(
        "--volttron-central-serverkey",
        default=None,
        help="The serverkey of volttron central.",
    )
    agents.add_argument(
        "--instance-name",
        default=None,
        help="The name of the instance that will be reported to "
        "VOLTTRON central.",
    )
    agents.add_argument(
        "--msgdebug",
        action="store_true",
        help="Route all messages to an agent while debugging.",
    )
    agents.add_argument(
        "--setup-mode",
        action="store_true",
        help="Setup mode flag for setting up authorization of external platforms.",
    )
    parser.add_argument(
        "--message-bus",
        action="store",
        default="zmq",
        dest="message_bus",
        help="set message to be used. valid values are zmq and rmq",
    )
    agents.add_argument(
        "--volttron-central-rmq-address",
        default=None,
        help="The AMQP address of a volttron central install instance",
    )
    agents.add_argument(
        "--agent-monitor-frequency",
        default=600,
        help="How often should the platform check for crashed agents and "
        "attempt to restart. Units=seconds. Default=600",
    )
    agents.add_argument(
        "--secure-agent-users",
        default=False,
        help="Require that agents run with their own users (this requires "
        "running scripts/secure_user_permissions.sh as sudo)",
    )

    # XXX: re-implement control options
    # on
    # control.add_argument(
    #    '--allow-root', action='store_true', inverse='--no-allow-root',
    #    help='allow root to connect to control socket')
    # control.add_argument(
    #    '--no-allow-root', action='store_false', dest='allow_root',
    #    help=argparse.SUPPRESS)
    # control.add_argument(
    #    '--allow-users', action='store_list', metavar='LIST',
    #    help='users allowed to connect to control socket')
    # control.add_argument(
    #    '--allow-groups', action='store_list', metavar='LIST',
    #    help='user groups allowed to connect to control socket')

    ipc = "ipc://%s$VOLTTRON_HOME/run/" % ("@" if sys.platform.startswith("linux") else "")

    parser.set_defaults(
        log=None,
        log_config=None,
        monitor=False,
        verboseness=logging.WARNING,
        volttron_home=volttron_home,
        autostart=True,
        publish_address=ipc + "publish",
        subscribe_address=ipc + "subscribe",
        vip_address=[],
        vip_local_address=ipc + "vip.socket",
    # This is used to start the web server from the web module.
        bind_web_address=None,
    # Used to contact volttron central when registering volttron central
    # platform agent.
        volttron_central_address=None,
        volttron_central_serverkey=None,
        instance_name=None,
    # allow_root=False,
    # allow_users=None,
    # allow_groups=None,
        verify_agents=True,
        resource_monitor=True,
    # mobility=True,
        msgdebug=None,
        setup_mode=False,
    # Type of underlying message bus to use - ZeroMQ or RabbitMQ
        message_bus="zmq",
    # Volttron Central in AMQP address format is needed if running on RabbitMQ message bus
        volttron_central_rmq_address=None,
        web_ssl_key=None,
        web_ssl_cert=None,
        web_ca_cert=None,
    # If we aren't using ssl then we need a secret key available for us to use.
        web_secret_key=None,
    )

    # Parse and expand options
    args = argv[1:]
    conf = os.path.join(volttron_home, "config")
    if os.path.exists(conf) and "SKIP_VOLTTRON_CONFIG" not in os.environ:
        # command line args get preference over same args in config file
        args = args + ["--config", conf]
    logging.getLogger().setLevel(logging.NOTSET)
    opts = parser.parse_args(args)

    start_volttron_process(opts)

def _main():
    """Entry point for scripts."""
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        pass

if __name__ == "__main__":
    _main()


import scrapy
import html2text
import re

class SenateSpider(scrapy.Spider):
    name = "senate_members"
    start_urls = [ 'http://www.oksenate.gov/Senators/Default.aspx?selectedtab=0' ]

    def parse(self, response):
        hrefs = response.xpath('//table[@summary="This table lists the Senators alphabetically.  Each senator\'s name is linked to his or her respective contact page."]/tr/td/span/span/a/@href').extract()[:48]

        for href in hrefs:
            yield scrapy.Request(response.urljoin(href),
                                 callback=self.parse_senator)

    def parse_senator(self, response):
        yield scrapy.Request(
            response.urljoin(response.xpath('//iframe/@src').extract_first()),
            callback=self.parse_senator_iframe)

    def parse_senator_iframe(self, response):
        htmlstrip = html2text.HTML2Text()
        htmlstrip.ignore_links = True
        #sen_re = re.compile(r'Senator (.*) - District (\d+)')
        sen_re = re.compile(r'(?:Senator )?\b(.*)\s*-\s*District\s*(\d+)')
        phone_re = re.compile(r'\(?(405|918)\)?.?(\d{3}).?(\d{4})')
        room_re = re.compile(r'(Rm\.|Room)\s*([0-9a-zA-Z]+)')

        markdown_page = htmlstrip.handle(response.body_as_unicode()).replace('\\', '').replace('*', '')
        name, dist = sen_re.findall(markdown_page)[0]

        district = int(dist)
        fname = name.split()[0]
        lname = name.split()[-1]

        phone = '-'.join(phone_re.findall(markdown_page)[0])

        cmtes = response.xpath('//html/body/table/tr/td/table/tr[4]/td/ul[1]/li').extract()
        cmtes = map(htmlstrip.handle, cmtes)
        cmtes = map(lambda a: a.split(' ', 1)[1].replace('*', '').strip(), cmtes)

        # Remove Chair, etc:
        cmtes = map(lambda a: a.split('\\-')[0].strip(), cmtes)

        room = room_re.findall(markdown_page)[0][1]

        party_letter = response.xpath('//html/body/table/tr/td/table/tr/td').re(r'Republican|Democrat|Libertarian')[0][0]

        return dict(
            fname=fname,
            lname=lname,
            name=name,
            district=district,
            party=party_letter,
            committees=cmtes,
            office=room,
            phone=phone
        )


"""---------Day39&40----------"""
#Question 1
def rec( A , B ) :
    if (A > 0) :
       Ans = A ** B
       print(Ans)
    else :
        Ans = 0
    return Ans
print("\n\nWeek Challenge ^_^")
rec( 5 , 3)

#Question 2
list1 = [-4, -6, -5, -1, 2, 3, 7, 9, 88 ]
print(list1)
PosList = []
FN = lambda x : x > 0
for x in list1 :
    if FN( x ) :
        PosList.append( x )
print(PosList)


from keras.models import Sequential
from keras import (
    layers,
    models,
)
from keras.callbacks import EarlyStopping
from keras.metrics import Precision, Recall, Accuracy

class RCNN_model:
    """
    Container class for the Keras LSTM model. Standardizes
    the usecases, so that it can be used similarily to other
    Keras models.
    """

    def __init__(self, input_length, EMBEDDING_DIM, MAX_NB_WORDS, MAX_SEQUENCE_LENGTH, EPOCH_SIZE, BATCH_SIZE):
        """
        Creates the RCNN structure. and attaches it to the class.
        """
        self.BATCH_SIZE = BATCH_SIZE
        self.EPOCH_SIZE = EPOCH_SIZE

        model = Sequential()
        model.add(layers.Input(input_length))
        model.add(layers.Embedding(input_dim=MAX_NB_WORDS, output_dim=EMBEDDING_DIM, input_length=input_length))
        model.add(layers.SpatialDropout1D(0.25))
        model.add(layers.Bidirectional(layers.GRU(50, return_sequences=True)))
        model.add(layers.Convolution1D(100, 3, activation="relu"))
        model.add(layers.Dense(50, activation="relu"))
        model.add(layers.Dropout(0.2))
        model.add(layers.Dense(1, activation="sigmoid"))
        model.compile(optimizer='adam', loss='binary_crossentropy', metrics=["accuracy", Precision(), Recall()])

        model.summary()

        self.model = model

    def fit(self, train_x, train_y):
        """
        Uses the input traning data to fit the Keras model.
        """
        history = self.model.fit(train_x, train_y, epochs=self.EPOCH_SIZE, batch_size=self.BATCH_SIZE, validation_split=0.2, callbacks=[EarlyStopping(monitor='val_loss', patience=1, min_delta=0.0001)])
        self.history = history
        return history

    def evaluate(self, valid_x, valid_y):
        """
        Uses the validation data to evaluate the model,
        and returns the models metrics.
        """
        results = self.model.evaluate(valid_x, valid_y, batch_size=self.BATCH_SIZE)
        print(results)
        return results[1:] + ["NaN"]

    def predict(self, valid_x):
        return self.model.predict(valid_x)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  : search_file05.py
# Author: roohom
# Date  : 2018/10/26 0026

"""
计算一个文件下所有文件的大小
    - 打开文件夹
    - 遍历所有文件
    - 获取文件大小并打印
"""

import os

target_folder = input("请输入你想进入的文件夹：")
os.chdir(target_folder)
all_file = os.listdir(target_folder)
file_info = {}

for each_file in all_file:
    if os.path.isfile(each_file):
        file_size = os.path.getsize(each_file)
        file_info[each_file] = file_size

for file in file_info.items():
    if file[1] > 1024:
        print("{}文件的大小为：{}KB".format(file[0], int(file[1])/1024))
    else:
        print("{}文件的大小为：{}字节".format(file[0], int(file[1])))


"""
Checks for manifests, related to KubeDNS service (CoreDNS, actually),
deploys LoadBalancer-type service (for usage with MetalLB), then
runs Docker-container with 'drill', to test reachability of KubeDNS from
external network. Intended to use with Kind-based K8s clusters.
"""
import logging
from time import sleep
from typing import Dict, List, Optional
import os
import yaml
from kubernetes.client import models
from kubetest.manifest import get_type, new_object
import kubetest.objects
import pytest
import testinfra.host

LOG = logging.getLogger(__name__)
TEST_QUERY = "kubernetes.default.svc.cluster.local"
"""
these functions are copy-pasted shamelessly from kubetest.manifest.
The only difference - load_file **ignores** unknown manifest types
'Raises' is simply replaced with 'Print' call. That's all.
"""

def load_file(path: str) -> List[object]:
    """ Loads manifest from YAML file and returns kubetest ApiObjects"""
    with open(path, "r") as input_file:
        manifests = yaml.load_all(input_file, Loader=yaml.SafeLoader)
        objs: List[object] = list()
        for manifest in manifests:
            obj_type: Optional[object] = get_type(manifest)
            if obj_type is None:
                LOG.warning("Unable to determine object type for manifest:",
                            manifest)
            else:
                objs.append(new_object(obj_type, manifest))
    return objs

def load_path(path: str) -> List[object]:
    """ Calls load_file for all YAML files in the specified directory """
    if not os.path.isdir(path):
        raise ValueError("{} is not a directory".format(path))

    objs: List[object] = list()
    for file_name in os.listdir(path):
        if os.path.splitext(file_name)[1].lower() in [".yaml", ".yml"]:
            objs = objs + load_file(os.path.join(path, file_name))
    return objs

# Kubetest copy-paste END

def preload_manifests(path: str,
                      selector: Dict[str, str]) -> List[models.V1Service]:
    """
    Loads all manifest files from dir, and returns a list of all
    Service objects with matching selector in spec
    """

    objects: List[object] = list()

    try:
        objects = load_path(path)
    except ValueError:
        pass

    coredns_svc = [
        obj for obj in objects
        if isinstance(obj, models.V1Service) and obj.spec.selector == selector
    ]
    return coredns_svc

MANIFESTS = preload_manifests(path="./kubernetes-networks/coredns",
                              selector={"k8s-app": "kube-dns"})

if not MANIFESTS:
    pytest.skip("Skipping CoreDNS tests", allow_module_level=True)

def get_services_by_proto(proto_string: str) -> List[models.V1Service]:
    return [
        obj for obj in MANIFESTS if obj.spec.ports[0].protocol == proto_string
    ]

@pytest.fixture(scope="module")
def coredns_tcp(kube_module: kubetest.client.TestClient
                ) -> kubetest.objects.Service:
    """ Return instance of CoreDNS LoadBalancer service for TCP endpoint"""
    svc_list = get_services_by_proto("TCP")
    svc = kubetest.objects.Service(svc_list[0])
    svc.create()
    kube_module.wait_until_created(svc, timeout=10)
    svc.wait_until_ready(timeout=10)
    yield svc
    svc.delete(options=None)
    svc.wait_until_deleted()

@pytest.fixture(scope="module")
def coredns_udp(kube_module: kubetest.client.TestClient
                ) -> kubetest.objects.Service:
    """ Return instance of CoreDNS LoadBalancer service for UDP endpoint"""
    svc_list = get_services_by_proto("UDP")
    svc = kubetest.objects.Service(svc_list[0])
    svc.create()
    kube_module.wait_until_created(svc, timeout=10)
    svc.wait_until_ready(timeout=10)
    yield svc
    svc.delete(options=None)
    svc.wait_until_deleted()

def get_lb_ip(service: kubetest.objects.Service) -> Optional[str]:
    """ Returns IP address of LoadBalancer-assigned Ingress or None """
    ip: Optional[str] = None
    count: int = 0
    while ip is None and count < 3:
        try:
            service.refresh()
            ip = service.obj.status.load_balancer.ingress[0].ip
        except TypeError:
            LOG.info("Still have no IP for %s. Retrying...", service.name)
            sleep(3)
            count = count + 1
            continue
    return ip

@pytest.mark.xfail(
    len(MANIFESTS) < 2,
    run=False,
    reason="TCP or UDP service for CoreDNS is not defined",
)
@pytest.mark.it("CoreDNS services MUST have the same LB ingress IP")
def test_coredns_shared_ip(coredns_tcp: kubetest.objects.Service,
                           coredns_udp: kubetest.objects.Service) -> None:

    tcp_ip: Optional[str] = get_lb_ip(coredns_tcp)
    udp_ip: Optional[str] = get_lb_ip(coredns_udp)

    assert udp_ip == tcp_ip, "UDP ({}) and TCP({}) LB IPs don't match".format(
        udp_ip, tcp_ip)

@pytest.mark.xfail(
    get_services_by_proto("TCP") == list(),
    run=False,
    reason="TCP service for CoreDNS is not defined",
)
@pytest.mark.it("Verify CoreDNS via MetalLB. Using TCP")
def test_coredns_tcp(coredns_tcp: kubetest.objects.Service,
                     test_container: testinfra.host.Host) -> None:
    assert isinstance(coredns_tcp, kubetest.objects.Service
                      ), "CoreDNS Service for TCP Endpoint is not available"

    assert (coredns_tcp.obj.status.load_balancer != {}
            ), "LoadBalancer ingress status is not set. Is MetalLB available?"

    ip: Optional[str] = get_lb_ip(coredns_tcp)
    assert ip is not None, "No IP address assigned for TCP endpoint"

    test_container.run("apk --no-cache -q add drill")
    assert (test_container.package("drill").is_installed is
            True), "Can't bootstrap test container - curl is not installed"

    tcp: str = test_container.check_output("drill -t {rec} @{ip}".format(
        ip=ip, rec=TEST_QUERY))
    assert (tcp.find("NOERROR") !=
            -1), "CoreDNS is not accessible via TCP, output is {}".format(tcp)
    LOG.info(tcp)

@pytest.mark.xfail(
    get_services_by_proto("UDP") == list(),
    run=False,
    reason="UDP service for CoreDNS is not defined",
)
@pytest.mark.it("Verify CoreDNS via MetalLB. Using UDP")
def test_coredns_udp(coredns_udp: kubetest.objects.Service,
                     test_container: testinfra.host.Host) -> None:

    assert coredns_udp is not None and isinstance(
        coredns_udp, kubetest.objects.Service
    ), "CoreDNS Service for UDP Endpoint is not available"

    assert (coredns_udp.obj.status.load_balancer.ingress != {}
            ), "LoadBalancer ingress status is not set. Is MetalLB available?"

    ip: Optional[str] = get_lb_ip(coredns_udp)
    assert ip is not None, "No IP address assigned for UDP endpoint"

    test_container.run("apk --no-cache -q add drill")
    assert (test_container.package("drill").is_installed is
            True), "Can't bootstrap test container - curl is not installed"

    udp: str = test_container.check_output("drill -u {rec} @{ip}".format(
        ip=ip, rec=TEST_QUERY))

    assert (udp.find("NOERROR") !=
            -1), "CoreDNS is not accessible via UDP, output is {}".format(udp)
    LOG.info(udp)


"""config file for experiments"""
import torch

data_file_name = 'test_data.pt'
model_name = 'ETHGT.pt'
scaler_file_name = 'test_data_scaler.pt'
project_id = 'bda-6893'
bucket_name = 'bda_eth_19g'

############
# init train
############
init_config_name = 'init_config.pt'

############
# preprocess config
############
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cpu')

numeric_features = ['value', 'gas', 'gas_price', 'receipt_cumulative_gas_used',
                    'receipt_gas_used', 'transaction_type']
preprocess_config = {
  'num_cpus': 4,
  'num_gpus': 1,

}

edge_attr_cols = ['value', 'gas', 'gas_price', 'receipt_cumulative_gas_used', 'receipt_gas_used', 'transaction_type']
target = 'gas_price'
node2vec_params = {
      'embedding_dim': 16,
      'walk_length': 20,
      'context_size': 10,
      'walks_per_node': 20,
      'num_negative_samples': 1,
      'p': 200,
      'q': 1,
      'sparse': True
  }
loader_params = dict(batch_size=128, shuffle=True, num_workers=2)
optim_params = {'lr': 0.01}
loop_params = {'num_episodes': 5}
train_params = {'loader_params': loader_params, 'optim_params': optim_params, 'loop_params': loop_params}

data_params = {
    'x': {'node2vec_params': node2vec_params, 'train_params': train_params, 'device': device},
    'y': {'target': target},
    'edge': {'edge_attr_cols': edge_attr_cols}
    }

############
# tune config
############
tune_config = {
  'num_samples': 1,
  'num_epochs': 100,
  'gpus_per_trial': 1
}

############
# trainer config
############
trainer_config = {
  'max_epochs': 500,
  'accelerator': 'gpu',
  'devices': 1
}


from PIL import Image
import os
import shutil

def rotate(data):
    save_dir = './division/rotate'
    from_dir = './division/random2000'
    shutil.rmtree(f'{save_dir}/{data}')
    os.mkdir(f'{save_dir}/{data}')

    imgs = os.listdir(f"{from_dir}/{data}")

    for i in imgs:
        img_path = f"{from_dir}/{data}/{i}"
        save_path = f"{save_dir}/{data}/{i}"
        img = Image.open(img_path)
        img2 = img.rotate(180)
        img2.save(save_path)

if "__main__" == __name__:
    rotate('train')
    rotate('test')
    rotate('validation')


import numpy as np
import plotly.express as px
import streamlit as st
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd

def created_page(df):

    st.title('Created Variables')

    cols = st.columns([1,2])

    with cols[0].container():

        df['Adjusted Categories'] =  df['Page Category'].apply(lambda x : group_categories(x))

        page_cat = df['Adjusted Categories'].value_counts()
        f3 = px.pie(values=page_cat.values, names=page_cat.index,title='Adjusted Page Category')
        f3.update_traces(textposition='inside', textinfo='percent+label',showlegend=False)
        f3.update_layout(
                margin=dict(l=0, r=0, t=40, b=0),
                width=300,
                height=300
            )
        st.plotly_chart(f3)

        st.write("""Merging very similar categories (like 'sports league' and 'sports team') makes 'Page Category'
                    more readable.""")
        st.write("""Also, we represented the evolution of the essential values between the last
                    48 to 24 hours and the last 24 hours to have a better understanding on the tendency.""")

    with cols[1].container():

        pattern = ['Min','Max','Avg','Median','Std','total']
        display_cols = [col for col in df.columns if 'evo' in col]
        #st.write(display_cols)
        fig = make_subplots(rows=2, cols=3, subplot_titles=pattern)

        for i,col in enumerate(display_cols):
            fig.add_trace(
                go.Histogram(x=df[col],xbins=dict(
                        size=10
                    )),
                row=i%2+1, col=i//2+1
            )

        fig.update_layout(
                    margin=dict(l=0, r=0, t=40, b=0),
                    showlegend=False,
                    title='How did the page number of comments evolve?  '
                )
        st.plotly_chart(fig)

def group_categories(cat):
    if cat in ["Politician","Political party",'Non-governmental organization (ngo)',
    'Government official']:
        return 'Politics'
    if cat in ['Aerospace/defense','Cars','Website','Automobiles and parts',
    'Telecommunication','App page','Software','Electronics','Computers/technology',
    'Camera/photo','Computers','Phone/tablet','Tools/equipment','Internet/software']:
        return 'Technology'
    if cat in ['Food/beverages','Clothing','Restaurant/cafe',
    'Outdoor gear/sporting goods','Vitamins/supplements','Health/medical/pharmacy',
    'Home decor','Health/beauty','Health/medical/pharmaceuticals','Coach','Spas/beauty/personal care']:
        return 'Personal Care'
    if cat in ['Personal blog','Jewelry/watches''Reference website','Landmark','Other']:
        return 'Other'
    if cat in ['Athlete','Professional sports team','Sports event','Sports league',
    'Sports venue','Sports/recreation/activities','Recreation/sports website']:
        return 'Sports'
    if cat in ['Arts/humanities website','Record label','Movie','Song','Artist',
    'Musician/band','Author','Comedian','Studio','Writer','Music video','Music award','Album','Book',
    'Producer','Tv/movie award','Musical instrument','Photographer']:
        return 'Art'
    if cat in ['Media/news/publishing','News/media website','Magazine',
    'Publisher','Business/economy website','Society/culture website','News personality']:
        return 'News'
    if cat in ['Travel/leisure','Hotel','Transportation','Local/travel website']:
        return 'Personal Care'
    if cat in ['Product/service','Retail and consumer merchandise','Company',
    'Local business','Shopping/retail','Professional services','Appliances','Insurance company'
    'Bank/financial institution','Small business']:
        return 'Companies'
    if cat in ['Public figure','Arts/entertainment/nightlife','Actor/director'
    'Tv show','Entertainer','Non-profit organization','Tv channel','Entertainment website',
    'Movie theater','Just for fun','Club','Tv network','Bar','Games/toys','Radio station',
    'Teens/kids website','Video game']:
        return 'Entertainment'
    if cat in ['Education website','Community','Education','School sports team','University',
    'Church/religious organization','School','Cause','Organization']:
        return 'Communities'
    return 'Other'


import gymnasium as gym
import matplotlib.pyplot as plt
import numpy as np
from ia import *
from numpy.random import gamma
from numpy.typing import NDArray

def plot_values(v: NDArray) -> None:
    v = np.reshape(v, (4, 4))
    # plot the state-value function
    fig = plt.figure(figsize=(8, 8))
    ax = fig.add_subplot(111)
    _ = ax.imshow(v, cmap="cool")
    for (j, i), label in np.ndenumerate(v):
        ax.text(i, j, np.round(label, 3), ha="center", va="center", fontsize=14)
    plt.tick_params(bottom="off", left="off", labelbottom="off", labelleft="off")
    plt.title("State-Value Function")
    plt.show()

if __name__ == "__main__":
    env = gym.make("FrozenLake-v1", render_mode="rgb_array", is_slippery=False)
    d = env.observation_space
    qa = QLearningAgent(env, Tabular, d=d)
    qa.train(0.01, max_steps=True, num_episodes=10000, gamma=0.9)
    v = np.empty(d.n)
    for i in range(d.start, d.start + d.n):
        print(i)
        _, v[i] = qa.val_act(np.int64(i))
    plot_values(v)
    qa.live_play(path="frozenlake.mp4", max_steps=True)


"""
script finding timeout with rate increase
"""

import pyNN.spiNNaker as p

p.setup(1.0)
inp = p.Population(40, p.SpikeSourcePoisson(rate=2),
                   additional_parameters={"seed": 417},
                   label = "input")
inp.record("spikes")
p.run(100)
p.reset()
inp.set(rate=30)
p.run(100)


# Use custom implementation:
from MFP import *

# Use MFP_TF wrapper:
# from MFP_TF import *

import urllib.request
from io import BytesIO
import os
import gzip
import struct

import numpy as np
np.set_printoptions(suppress=True)

class MNIST:
    def __init__(self):
        source = 'http://yann.lecun.com/exdb/mnist'
        target = os.path.abspath('./data/MNIST/')

        datasets = {
            'train_images': 'train-images.idx3-ubyte',
            'train_labels': 'train-labels.idx1-ubyte',
            'test_images': 't10k-images.idx3-ubyte',
            'test_labels': 't10k-labels.idx1-ubyte'
        }

        if not os.path.exists(target):
            os.makedirs(target)

            datasets_compressed = {
                'train_images': 'train-images-idx3-ubyte.gz',
                'train_labels': 'train-labels-idx1-ubyte.gz',
                'test_images': 't10k-images-idx3-ubyte.gz',
                'test_labels': 't10k-labels-idx1-ubyte.gz'
            }

            for ident, filename in datasets_compressed.items():
                compressed_file = BytesIO()
                compressed_file.write(urllib.request.urlopen(source + '/' + filename).read())
                compressed_file.seek(0)

                decompressed_file = gzip.GzipFile(fileobj=compressed_file, mode='rb')

                with open(target + '\\' + datasets[ident], 'wb') as outfile:
                    outfile.write(decompressed_file.read())

        self.train_images = MNIST._load_images(target + '/' + datasets['train_images'])
        self.train_labels = MNIST._load_labels(target + '/' + datasets['train_labels'])

        self.test_images = MNIST._load_images(target + '/' + datasets['test_images'])
        self.test_labels = MNIST._load_labels(target + '/' + datasets['test_labels'])

    @staticmethod
    def _load_images(file_name):
        with open(file_name, 'rb') as image_file:
            ftype, length, rows, cols = struct.unpack(">IIII", image_file.read(16))
            return np.fromfile(image_file, dtype=np.uint8).reshape(length, rows * cols).astype(float) / 128 - 1

    @staticmethod
    def _load_labels(file_name):
        with open(file_name, 'rb') as label_file:
            label_file.read(8)  # Ignore header info for file type and number of entries
            integer_matrix = np.fromfile(label_file, dtype=np.int8)

            # Convert to one-hot format
            bool_matrix = np.zeros((np.size(integer_matrix), 10))
            bool_matrix[np.arange(np.size(integer_matrix)), integer_matrix] = True
            return bool_matrix

    def sample(self, quantity=1):
        x = np.random.randint(np.size(self.train_images[0]), size=quantity)
        return [self.train_images[x].T, self.train_labels[x].T]

    def survey(self, quantity=200):
        if quantity is None:
            return self.test_images.T, self.test_labels.T
        else:
            x = np.linspace(0, np.size(self.test_images[0]), quantity).astype(int)  # Size changes error granularity
            return [self.test_images[x].T, self.test_labels[x].T]

    def size_input(self):
        return np.size(self.train_images[0])

    def size_output(self):
        return np.size(self.train_labels[0])

    def plot(self, plt, predict):
        # Do not attempt to plot an image
        pass

    @staticmethod
    def error(expect, predict):
        predict_id = np.argmax(predict, axis=0)
        expect_id = np.argmax(expect, axis=0)
        return int((1.0 - np.mean((predict_id == expect_id).astype(float))) * 100)

environment = MNIST()

# ~~~ Create the network ~~~
network_params = {
    # Shape of network
    "units": [environment.size_input(), 100, 100, 50, environment.size_output()],

    # Basis function(s) from network's Function.py
    "basis": basis_bent,
    "basis_final": basis_softmax,

    # Distribution to use for weight initialization
    "distribute": dist_normal
    }

network = MFP(**network_params)

# ~~~ Train the network ~~~
optimizer_params = {
    # Source of stimuli
    "batch_size": 5,

    "cost": cost_cross_entropy,

    # Learning rate
    "learn_step": 0.5,
    "learn_anneal": anneal_fixed,

    # Weight decay regularization function
    "regularize_step": 0.0,
    "regularizer": reg_L2,

    "epsilon": 0.0,           # error allowance
    "iteration_limit": None,  # limit on number of iterations to run

    "debug_frequency": 1000,
    "debug": True,

    # The error measurement used in the mnist graph is highly susceptible to sampling error
    "graph": False
    }

Adagrad(network, environment, **optimizer_params).minimize()

# ~~~ Test the network ~~~
[stimuli, expectation] = environment.survey()
print(network.predict(stimuli))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/11/14 8:49

'''
序列化：
'''

import pickle  # 引入序列化模块

#将对象序列化到文件中
with open(r"b.txt","wb") as f:
    a1 = "元古"
    a2 = 234
    a3 = [10,20,30]

    pickle.dump(a1,f)
    pickle.dump(a2,f)
    pickle.dump(a3,f)

#将获得的数据反序列化成对象
with open(r"b.txt","rb") as f:
    b1 = pickle.load(f)
    b2 = pickle.load(f)
    b3 = pickle.load(f)

    print("反序列化结果为：",b1)
    print("反序列化结果为：",b2)
    print("反序列化结果为：",b3)


"""
File: hailstone.py
Name: Angus
-----------------------
This program should implement a console program that simulates
the execution of the Hailstone sequence, defined by Douglas
Hofstadter. Output format should match what is shown in the sample
run in the Assignment 2 Handout.
"""
EXIT = 1

def my_number(x):
    if x % 2 == 1:
        n = 3 * x + 1
        print(str(x) + '  is odd , so I make 3n+1 : ' + str(n))
        return n
    elif x % 2 == 0:
        n = x / 2
        print(str(x) + '  is even , so I take half : ' + str(n))
        return n

def step_plus_one(s):
    s += 1
    return s

def main():
    print('This program computes Hailstone sequence')
    x = int(input('Please key in  the first number!'))
    s = 0
    if x == EXIT:
        pass
    if x <= 0:
        print('Invalid number please key in positive number')
    else:
        while x > 0 and x != 1:
            x = my_number(x)
            s = step_plus_one(s)
        print('It took  '+str(s)+'  steps to reach 1')

###### DO NOT EDIT CODE BELOW THIS LINE ######

if __name__ == "__main__":
    main()


# 딕셔너리 선언
dict = {
    "name" : "DB-HDW",
    "type" : "computer",
    "element" : ["cpu", "gpu", "ram", "mainboard"]
}
print("딕셔너리 선언(고정) : dict =", dict, "\n")

"""for <키 변수> in <딕셔너리>:
        <코드>
"""

for key in dict:
    print(key, ":", dict[key])


import yaml
import logging
from os import walk
import json
import noise

# default values
conf = {
    "long_press": 1.2, # long press in seconds
    "delay": 1.0, # timer length before counting buttons
    "sleep_track_limit": 1, #minutes
    "selected_noise": "nature.wav",
    "available_noises":[],
    "volume_offset":0
}

def update_config(settings):
    global conf
    result = {"action":"success","message":f"Settings have been updated"}
    new_settings = conf

    for key, value in settings.items():
        try:
            new_settings[key] = value
        except Exception as e:
            error = f"update_config Setting Key: {key} not found {e}"
            logging.warn(error)
            result = {"action":"error","message": error}
    if result['action'] == "success":
        with open(r'./config.yaml', 'w') as file:
            yaml.dump(new_settings, file)
        read_config()
    noise.intialize_noise(conf['selected_noise'], conf['volume_offset'])
    return json.dumps(result)

def read_config():
    global white_noise
    global conf
    try:
        with open(r'./config.yaml') as file:
            values = yaml.load(file, Loader=yaml.FullLoader)
            for key, value in  values.items():
                conf[f"{key}"] = value
        _, _, conf['available_noises'] = next(walk("./noise/"))
    except Exception as e:
        logging.debug("""./config.yaml not found. creating from default values""")
        with open(r'./config.yaml', 'w') as file:
            yaml.dump(conf, file)
    print(conf)
    return conf


import configparser

class OverlayConfig():
    def __init__(self, colored_noise=True, opacity=0.7):
        self.colored_noise = colored_noise
        self.opacity = opacity

class CameraConfig():
    def __init__(self, camera_id=0, capture_interval=1):
        self.id = camera_id
        self.capture_interval = capture_interval

class Config:
    def __init__(self, colored_noise=True, opacity=0.7, camera_id=0, capture_interval=1):
        self.overlay = OverlayConfig(
            colored_noise=colored_noise,
            opacity=opacity
        )
        self.camera = CameraConfig(
            capture_interval=capture_interval,
            camera_id=camera_id
        )

def parse_config(filename=None):

    config = Config(
        colored_noise=True,
        opacity=0.7,
        camera_id=0,
        capture_interval=1
    )

    if not (filename is None):
        config_parser = configparser.ConfigParser()
        config_parser.read(filename)

        if 'OVERLAY' in config_parser.sections():
            if 'ColoredNoise' in config_parser['OVERLAY']:
                config.overlay.colored_noise = config_parser['OVERLAY']['ColoredNoise'].lower() in ['yes', 'true']
            if 'Opacity' in config_parser['OVERLAY']:
                config.overlay.opacity = float(config_parser['OVERLAY']['Opacity'])
        if 'CAMERA' in config_parser.sections():
            if 'Camera' in config_parser['CAMERA']:
                config.camera.id = int(config_parser['CAMERA']['Camera'])
            if 'CaptureInterval' in config_parser['CAMERA']:
                config.camera.capture_interval = float(config_parser['CAMERA']['CaptureInterval'])

    return config


#!/usr/bin/env python3

import json
import yaml
import asyncio
from aiohttp.web import Application, Response, MsgType, WebSocketResponse, HTTPFound

import spotbox

@asyncio.coroutine
def wshandler(request):
    resp = WebSocketResponse()
    ok, protocol = resp.can_prepare(request)
    if not ok:
        return HTTPFound('/index.html')

    yield from resp.prepare(request)
    print('Client connected.')
    request.app['sockets'].append(resp)
    box.command({"command": "state"})

    while True:
        msg = yield from resp.receive()
        if msg.tp == MsgType.text:
            box.command(json.loads(msg.data))
        else:
            break

    request.app['sockets'].remove(resp)
    print('Someone disconnected.')
    return resp

@asyncio.coroutine
def redirect(request):
    return HTTPFound('/index.html')

def brodcast(app, msg):
    for ws in app['sockets']:
        ws.send_str(json.dumps(msg))

def consumer_thread(app, loop):
    def inner():
        while True:
            msg = box.status_queue.get()
            loop.call_soon_threadsafe(brodcast, app, msg)
    return inner

@asyncio.coroutine
def init(loop, host, port):
    app = Application(loop=loop)
    app['sockets'] = []
    app.router.add_route('GET', '/ws', wshandler)
    app.router.add_route('GET', '/', redirect)
    app.router.add_static('/', '../ui/static')
    handler = app.make_handler()
    srv = yield from loop.create_server(handler, host, port)
    print("Server started at {}:{}".format(host, port))
    return app, srv, handler

@asyncio.coroutine
def finish(app, srv, handler):
    for ws in app['sockets']:
        ws.close()
    app['sockets'].clear()
    yield from asyncio.sleep(0.1)
    srv.close()
    yield from handler.finish_connections()
    yield from srv.wait_closed()

with open("config.yaml", 'r') as config:
    config = yaml.load(config)
    credentials = config["spotify"]
    host = config["server"]["host"]
    port = config["server"]["port"]
    buffer_size = config["audio"]["buffer_size"]

box = spotbox.Spotbox(credentials["username"], credentials["password"], buffer_size)
alsa = spotbox.Alsa(box.sound_queue)

loop = asyncio.get_event_loop()
app, srv, handler = loop.run_until_complete(init(loop, host, port))

loop.run_in_executor(None, consumer_thread(app, loop))
loop.run_in_executor(None, box.run)
loop.run_in_executor(None, alsa.run)
try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.run_until_complete(finish(app, srv, handler))


vat = 0
numVat = 0
totalVat = 0

def main():

    salesfigures = []
    salefigvat = []
    total =0
    sf=0

    sf = int(input("enter sales figure"))

    while(sf != -1):
        salesfigures.append(sf)

        salefigvat.append(addVAT(sf))
        total += salefigvat[-1]
        sf = int(input("enter sales figure"))

    print("the sales you entered were : %s\nthe sales inclduing VAT were: %s"%(salesfigures,salefigvat))
    print("total vat charged=",totalVat)
    print("total sales for this period is:",total)

def addVAT(sf):
    global vat
    global numVat
    global totalVat

    vat = sf * 0.23
    numVat = sf + vat
    totalVat += vat

    return numVat

main()

Python assignmnet 1 Proffsssional Skills.py


from setuptools import find_packages, setup

def get_description():
    return "Publishing microservice for Red Hat's Content Delivery Network"

def get_long_description():
    with open("README.md") as readme:
        text = readme.read()

    # Long description is everything after README's initial heading
    idx = text.find("\n\n")
    return text[idx:]

def get_requirements():
    with open("requirements.in") as reqs:
        return [
            line.split()[0]
            for line in reqs.read().splitlines()
            if not line.startswith("#")
        ]

setup(
    name="exodus-gw",
    version="0.0.1",
    packages=find_packages(exclude=["tests"]),
    include_package_data=True,
    url="https://github.com/release-engineering/exodus-gw",
    license="GNU General Public License",
    description=get_description(),
    long_description=get_long_description(),
    long_description_content_type="text/markdown",
    classifiers=[
        "Development Status :: 4 - Beta",
        "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
        "Programming Language :: Python :: 3",
    ],
    install_requires=get_requirements(),
    python_requires=">=3.11",
    project_urls={
        "Documentation": "https://release-engineering.github.io/exodus-gw",
    },
    entry_points={
        "console_scripts": [
            "exodus-gw-alembic-upgrade = exodus_gw.alembic_upgrade:entry_point"
        ]
    },
)


# Licensed under a 3-clause BSD style license - see LICENSE.rst
from ... import splatalogue
from ...utils.testing_tools import MockResponse
from astropy import units as u
from astropy.tests.helper import pytest
import requests
import os

SPLAT_DATA = 'CO_colons.csv'

def data_path(filename):
    data_dir = os.path.join(os.path.dirname(__file__), 'data')
    return os.path.join(data_dir, filename)

@pytest.fixture
def patch_post(request):
    mp = request.getfuncargvalue("monkeypatch")
    mp.setattr(requests, 'post', post_mockreturn)
    return mp

def post_mockreturn(url, data=None, timeout=10, **kwargs):
    filename = data_path(SPLAT_DATA)
    content = open(filename, "rb").read()
    return MockResponse(content, **kwargs)

def test_simple(patch_post):
    x = splatalogue.Splatalogue.query_lines(114 * u.GHz, 116 * u.GHz, chemical_name=' CO ')

def test_init(patch_post):
    x = splatalogue.Splatalogue.query_lines(114 * u.GHz, 116 * u.GHz, chemical_name=' CO ')
    S = splatalogue.Splatalogue(chemical_name=' CO ')
    y = S.query_lines(114 * u.GHz, 116 * u.GHz)
    # it is not currently possible to test equality between tables:
    # masked arrays fail
    # assert y == x
    assert len(x) == len(y)
    assert all(y['Species'] == x['Species'])
    assert all(x['Chemical Name'] == y['Chemical Name'])

def test_load_species_table():
    tbl = splatalogue.load_species_table.species_lookuptable()
    CO = tbl.find(' CO ')
    assert len(CO) == 4

# regression test: ConfigItems were in wrong order at one point
def test_url():
    assert 'http://' in splatalogue.core.Splatalogue.QUERY_URL
    assert 'cv.nrao.edu' in splatalogue.core.Splatalogue.QUERY_URL

# regression test: get_query_payload should work (#308)
def test_get_payload():
    q = splatalogue.core.Splatalogue.query_lines_async(1 * u.GHz, 10 * u.GHz,
                                                       get_query_payload=True)
    assert '__utma' in q

# regression test: line lists should ask for only one line list, not all
def test_line_lists():
    q = splatalogue.core.Splatalogue.query_lines_async(1 * u.GHz, 10 * u.GHz,
                                                       line_lists=['JPL'],
                                                       get_query_payload=True)
    assert q['displayJPL'] == 'displayJPL'
    assert q['displaySLAIM'] == ''

# regression test: raise an exception if a string is passed to line_lists
# uses get_query_payload to avoid having to monkeypatch
def test_linelist_type():
    with pytest.raises(TypeError) as exc:
        splatalogue.core.Splatalogue.query_lines_async(1 * u.GHz, 10 * u.GHz,
                                                       line_lists='JPL',
                                                       get_query_payload=True)
    assert exc.value.args[0] == "Line lists should be a list of linelist names.  See Splatalogue.ALL_LINE_LISTS"

def test_top20_crashorno():
    splatalogue.core.Splatalogue.query_lines_async(114 * u.GHz, 116 * u.GHz, top20='top20',
                                                   get_query_payload=True)
    with pytest.raises(ValueError) as exc:
        splatalogue.core.Splatalogue.query_lines_async(114 * u.GHz, 116 * u.GHz,
                                                       top20='invalid',
                                                       get_query_payload=True)
    assert exc.value.args[0] == "Top20 is not one of the allowed values"

def test_band_crashorno():
    splatalogue.core.Splatalogue.query_lines_async(band='alma3',
                                                   get_query_payload=True)
    with pytest.raises(ValueError) as exc:
        splatalogue.core.Splatalogue.query_lines_async(band='invalid',
                                                       get_query_payload=True)
    assert exc.value.args[0] == "Invalid frequency band."


import sys
from collections import defaultdict
si = sys.stdin.readline

n = int(si())
# 딕셔너리의 기본값으로 빈 리스트 지정
x_dict = defaultdict(list)
y_dict = defaultdict(list)
result = 0

for _ in range(n):
    x, y = map(int, si().split())
    x_dict[x].append(y) # x좌표가 같은 y값끼리 하나의 리스트에 담기게 됨.
    y_dict[y].append(x) # y좌표가 같은 x값끼리 하나의 리스트에 담기게 됨.

# key값 마다 해당 value 리스트의 길이가 2를 넘어가면 전부 같은 직선으로 간주
for key in x_dict:
    if len(x_dict[key]) >= 2:
        result += 1

for key in y_dict:
    if len(y_dict[key]) >= 2:
        result += 1

print(result)

11_자료 구조/평행선.py


#parse_xml.py

import xml.etree.ElementTree as et
import spacy
import string
import os
import json

nlp = spacy.load('en')

def parse(f):
	tree = et.parse(f)
	root = tree.getroot()
	text = root[1]
	# print(root[1])
	# print(text.text)
	anno = root[3]
	# print(anno)
	# for r in et.tostring(root[1]):
	# 	print(r.tag, r.text)

	# print(et.tostring(root[1]))
	texttostring = et.tostring(text, encoding="utf8", method="xml").decode()
	# print(texttostring)
	openbracket = False
	leftbracket = False
	finaltext = ""
	x = 0

	# replace html left tag with <

	while x < len(texttostring)-1:
		#print(json.dumps(texttostring[x]))
		if texttostring[x] == "<":
			openbracket = True
		elif texttostring[x] == "&" and texttostring[x+1]=="l" and texttostring[x+2]=="t" and texttostring[x+3]==";":
			openbracket = False
			finaltext += "<"
			x += 3
		elif texttostring[x] == ">":
			openbracket = False
		elif texttostring[x] == "&" and texttostring[x+1]=="g" and texttostring[x+2]=="t" and texttostring[x+3]==";":
			finaltext += ">"
			openbracket = False
			x += 3
		elif not openbracket:
			#finaltext += texttostring[x]
			if texttostring[x] == "\n":
				#if texttostring[x+1] == "\n":
				#	x += 1
				finaltext += " "
			elif texttostring[x] != "\n":
				finaltext += texttostring[x].strip('\n')
		x += 1
	finaltext = finaltext[1:]
	#print(finaltext)
	attribs = []
	for child in anno:
		attribs.append((int(child.attrib['StartNode']), int(child.attrib['EndNode']), child.attrib['Type']))
	start = []
	startlabel = {}
	for t in attribs:
#		if t[0] > 11 and t[0] < 1161:
#			start.append(t[0]-18)
#			startlabel[t[0]-18] = t[2]
#		elif t[0] > 1160:
#			start.append(t[0]-36)
#			startlabel[t[0]-36] = t[2]
#		else:
#			start.append(t[0])
#			startlabel[t[0]] = t[2]
		start.append(t[0])
		startlabel[t[0]] = t[2]

	sents = [] # list of sentences, each sentence is a list of words
	labels = [] # label of corresponding word
	word = ""
	index = 0
	label = ""

	doc = nlp(finaltext)

	spacy_sents = list(doc.sents) # doc.sents is a list of spans
	startnode = 0
	#print(sorted(startlabel))

	for sent in spacy_sents:
		s = []
		l = []
		for word in sent:
			s.append(word)
			#print(word, startnode)
			if startnode in start:
				l.append(startlabel[startnode])
			else:
				l.append("Not Applicable")
			#print(l[-1])
			startnode += len(word.string)
		sents.append(s)
		labels.append(l)
	#print(sents, labels)
	return sents, labels

if __name__ == "__main__":
	for filename in os.listdir("./"):
		if filename.endswith(".xml"):
			print(filename)
			try:
				parse(filename)
				print("done!")
			except Exception as e:
				print(e)

	# tokenize with spacy first --> each word is a spacy object
	# keep a count variable to keep track if the start/end nodes match up
	# for l in range(len(finaltext)):

	# 	if finaltext[l]=="\n":
	# 		continue
	# 	elif finaltext[l] != " ":
	# 		word += finaltext[l]
	# 	else:
	# 		if index in start:
	# 			label = startlabel[index]
	# 		else:
	# 			label = "Not Applicable"
	# 		word = word.strip(string.punctuation).strip()
	# 		pairs.append((word, label))
	# 		index = l + 1
	# 		word = ""

	# return pairs

#for p in pairs:
#	print(p)

# write own code to parse through body of text
# id numbers correspond to character indices

# Child nodes for annotations
# Id --> int (start and end characters of text without xml tags)
# Type --> annotation type
# for child in anno:
# 	print(child.attrib['StartNode'], child.attrib['EndNode'], child.attrib['Type'])
# 	print(child.tag, child.text, child.attrib)

# for e in text.iter():
# 	print(e.text)


import datetime

from flask import Blueprint, url_for, session, render_template, request, \
    redirect, current_app

from DemoPortalFlask.logic.message import msg

admin_api = Blueprint('admin_api', __name__)

@admin_api.route('/toggle-admin')
def toggle_admin():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'])

    user_record = current_app.cluster.get_user(session['email'])
    if not user_record or not user_record[0]['admin']:
        msg(access_logger, 'Admin privileges not enabled for this account.',
            'error')
        return redirect(request.referrer)

    if 'admin' not in session:
        session['admin'] = True
        return redirect(request.referrer)
    else:
        del session['admin']

    return redirect(url_for('dashboard_api.index'))

@admin_api.route('/add-admin', methods=['GET', 'POST'])
def add_admin():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    if request.method == 'POST':
        if 'email' in request.form and request.form['email']:
            current_app.cluster.set_admin(request.form['email'], True)
        else:
            msg(access_logger, 'Please specify an email address.', 'warn')

    headings = ['user', 'admin']
    users = current_app.cluster.get_users()
    users = [user for user in users if user['admin']]
    return render_template('users.jinja2',
                           title='Add Admin User',
                           headings=headings,
                           users=users)

@admin_api.route('/remove-admin')
def remove_admin():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    if 'email' in request.args and request.args['email']:
        current_app.cluster.set_admin(request.args['email'], False)
    else:
        msg(access_logger, 'Please specify an email address.', 'warn')

    return redirect(url_for('admin_api.add_admin'))

@admin_api.route('/admin-history')
@admin_api.route('/admin-history/<int:page>')
def admin_history(page=0):
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    msg(access_logger,
        'This query is expensive. Please do not refresh more than needed.',
        'warn', log=False)

    date = datetime.datetime.combine(datetime.date.today(),
                                     datetime.datetime.min.time())
    date = date + datetime.timedelta(days=-1 * page)

    page_range = 9
    start_range = page - page_range / 2 if (page - page_range / 2) > 0 else 0
    paging = {
        'date': datetime.date.today() + datetime.timedelta(days=-1 * page),
        'page': page,
        'start_range': start_range,
        'end_range': start_range + page_range,
        'back': page - 1 if (page - 1) > 0 else 0,
        'forward': page + 1
    }

    history = access_logger.get_access_log(date)
    headings = ['user',
                'request', 'request_update',
                'method', 'endpoint',
                'level', 'message',
                'form_variables', 'get_variables',
    ]

    return render_template('history.jinja2',
                           history_log=True,
                           title='Admin History',
                           paging=paging,
                           headings=headings,
                           history=history)

@admin_api.route('/last-seen')
def last_seen():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    history = access_logger.get_last_seen_log()
    headings = ['user', 'date']

    return render_template('history.jinja2',
                           title='Last Seen',
                           headings=headings,
                           history=history)

@admin_api.route('/launches')
def launches():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    msg(access_logger,
        'This query is expensive. Please do not refresh more than needed.',
        'warn', log=False)

    history = access_logger.get_launches()
    headings = ['date', 'demo', 'user']

    if 'advanced' in request.args:
        headings += ['form_variables', 'request', 'time']

    description = '''Views:
        Simple |
        Advanced
        '''.format(request.url_rule)

    return render_template('history.jinja2',
                           title='Launch History (by Date)',
                           description=description,
                           headings=headings,
                           history=history)

@admin_api.route('/demo-launches')
def demo_launches():
    if 'email' not in session:
        return redirect(url_for('authentication_api.login'))
    access_logger = current_app.cluster.get_access_logger(request,
                                                          session['email'],
                                                          init_log=False)

    if 'admin' not in session:
        msg(access_logger, 'Enable admin privileges first.', 'error')
        return redirect(request.referrer)

    msg(access_logger,
        'This query is expensive. Please do not refresh more than needed.',
        'warn', log=False)

    history = access_logger.get_demo_launches()
    headings = ['demo', 'user', 'time']

    if 'advanced' in request.args:
        headings += ['form_variables', 'request']

    description = '''Views:
        Simple |
        Advanced
        '''.format(request.url_rule)

    return render_template('history.jinja2',
                           title='Launch History (by Demo)',
                           description=description,
                           headings=headings,
                           history=history)


#
# @lc app=leetcode.cn id=7 lang=python3
#
# [7] 整数反转
#
# https://leetcode-cn.com/problems/reverse-integer/description/
#
# algorithms
# Easy (34.75%)
# Total Accepted:    559.8K
# Total Submissions: 1.6M
# Testcase Example:  '123'
#
# 给出一个 32 位的有符号整数，你需要将这个整数中每位上的数字进行反转。
#
#
#
# 注意：
#
#
# 假设我们的环境只能存储得下 32 位的有符号整数，则其数值范围为 [−2^31,  2^31 − 1]。请根据这个假设，如果反转后整数溢出那么就返回
# 0。
#
#
#
#
# 示例 1：
#
#
# 输入：x = 123
# 输出：321
#
#
# 示例 2：
#
#
# 输入：x = -123
# 输出：-321
#
#
# 示例 3：
#
#
# 输入：x = 120
# 输出：21
#
#
# 示例 4：
#
#
# 输入：x = 0
# 输出：0
#
#
#
#
# 提示：
#
#
# -2^31
#
#
#
class Solution:
    def reverse(self, x: int) -> int:
        """
        :type x: int
        :rtype: int
        """
        if x < 0:
            x = int(str(x*(-1))[::-1])*(-1)
        else:
            x = int(str(x)[::-1])
        if -2**31 <= x <= 2**31-1:
            return x
        else:
            return 0


# 자연수 N과 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.
# 1부터 N까지 자연수 중에서 M개를 고른 수열
# 같은 수를 여러 번 골라도 된다.
# 첫째 줄에 자연수 N과 M이 주어진다. (1 ≤ M ≤ N ≤ 7)
# 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다.
# 수열은 사전 순으로 증가하는 순서로 출력해야 한다.
# 이 문제 시리즈는 각 코드가 어떤 역할을 수행하는지 잘 파악해야하는 것 같다.

# 내 풀이
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
arr = []

def func():
  if(len(arr) == m):
      print(' '.join(map(str,arr)))
      return

  for i in range(1,n+1):
      arr.append(i)  # 기존에 있던 방문여부를 묻는 조건문 삭제
      func()
      arr.pop()

func()