import argparse
import os
import shutil
import socket
import stat
import sys
from socket import *
from pathlib import Path

from progress.bar import IncrementalBar
import donkeycar as dk
from donkeycar.management.joystick_creator import CreateJoystick
from donkeycar.management.tub import TubManager
from donkeycar.utils import *

import time
from datetime import datetime
import requests
import tempfile
import tarfile
from requests_toolbelt.multipart.encoder import MultipartEncoder
import netifaces
from donkeycar import __version__

PACKAGE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
TEMPLATES_PATH = os.path.join(PACKAGE_PATH, 'templates')

HELP_CONFIG = 'location of config file to use. default: ./config.py'

def make_dir(path):
    real_path = os.path.expanduser(path)
    print('making dir ', real_path)
    if not os.path.exists(real_path):
        os.makedirs(real_path)
    return real_path

def load_config(config_path):

    '''
    load a config from the given path
    '''
    conf = os.path.expanduser(config_path)

    if not os.path.exists(conf):
        print("No config file at location: %s. Add --config to specify\
                location or run from dir containing config.py." % conf)
        return None

    try:
        cfg = dk.load_config(conf)
    except:
        print("Exception while loading config from", conf)
        return None

    return cfg

class BaseCommand(object):
    pass

class CreateCar(BaseCommand):

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='createcar', usage='%(prog)s [options]')
        parser.add_argument('--path', default=None, help='path where to create car folder')
        parser.add_argument('--template', default=None, help='name of car template to use')
        parser.add_argument('--overwrite', action='store_true', help='should replace existing files')
        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        self.create_car(path=args.path, template=args.template, overwrite=args.overwrite)

    def create_car(self, path, template='complete', overwrite=False):
        """
        This script sets up the folder structure for donkey to work.
        It must run without donkey installed so that people installing with
        docker can build the folder structure for docker to mount to.
        """

        # these are neeeded incase None is passed as path
        path = path or '~/mycar'
        template = template or 'complete'
        print("Creating car folder: {}".format(path))
        path = make_dir(path)

        print("Creating data & model folders.")
        folders = ['models', 'data', 'logs']
        folder_paths = [os.path.join(path, f) for f in folders]
        for fp in folder_paths:
            make_dir(fp)

        # add car application and config files if they don't exist
        app_template_path = os.path.join(TEMPLATES_PATH, template+'.py')
        config_template_path = os.path.join(TEMPLATES_PATH, 'cfg_' + template + '.py')
        myconfig_template_path = os.path.join(TEMPLATES_PATH, 'myconfig.py')
        train_template_path = os.path.join(TEMPLATES_PATH, 'train.py')
        calibrate_template_path = os.path.join(TEMPLATES_PATH, 'calibrate.py')
        car_app_path = os.path.join(path, 'manage.py')
        car_config_path = os.path.join(path, 'config.py')
        mycar_config_path = os.path.join(path, 'myconfig.py')
        train_app_path = os.path.join(path, 'train.py')
        calibrate_app_path = os.path.join(path, 'calibrate.py')

        if os.path.exists(car_app_path) and not overwrite:
            print('Car app already exists. Delete it and rerun createcar to replace.')
        else:
            print("Copying car application template: {}".format(template))
            shutil.copyfile(app_template_path, car_app_path)
            os.chmod(car_app_path, stat.S_IRWXU)

        if os.path.exists(car_config_path) and not overwrite:
            print('Car config already exists. Delete it and rerun createcar to replace.')
        else:
            print("Copying car config defaults. Adjust these before starting your car.")
            shutil.copyfile(config_template_path, car_config_path)

        if os.path.exists(train_app_path) and not overwrite:
            print('Train already exists. Delete it and rerun createcar to replace.')
        else:
            print("Copying train script. Adjust these before starting your car.")
            shutil.copyfile(train_template_path, train_app_path)
            os.chmod(train_app_path, stat.S_IRWXU)

        if os.path.exists(calibrate_app_path) and not overwrite:
            print('Calibrate already exists. Delete it and rerun createcar to replace.')
        else:
            print("Copying calibrate script. Adjust these before starting your car.")
            shutil.copyfile(calibrate_template_path, calibrate_app_path)
            os.chmod(calibrate_app_path, stat.S_IRWXU)

        if not os.path.exists(mycar_config_path):
            print("Copying my car config overrides")
            shutil.copyfile(myconfig_template_path, mycar_config_path)
            # now copy file contents from config to myconfig, with all lines
            # commented out.
            cfg = open(car_config_path, "rt")
            mcfg = open(mycar_config_path, "at")
            copy = False
            for line in cfg:
                if "import os" in line:
                    copy = True
                if copy:
                    mcfg.write("# " + line)
            cfg.close()
            mcfg.close()

        print("Donkey setup complete.")

class UpdateCar(BaseCommand):
    '''
    always run in the base ~/mycar dir to get latest
    '''

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='update', usage='%(prog)s [options]')
        parser.add_argument('--template', default=None, help='name of car template to use')
        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        cc = CreateCar()
        cc.create_car(path=".", overwrite=True, template=args.template)

class FindCar(BaseCommand):
    def parse_args(self, args):
        pass

    def run(self, args):
        print('Looking up your computer IP address...')
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8",80))
        ip = s.getsockname()[0]
        print('Your IP address: %s ' %s.getsockname()[0])
        s.close()

        print("Finding your car's IP address...")
        cmd = "sudo nmap -sP " + ip + "/24 | awk '/^Nmap/{ip=$NF}/B8:27:EB/{print ip}'"
        cmdRPi4 = "sudo nmap -sP " + ip + "/24 | awk '/^Nmap/{ip=$NF}/DC:A6:32/{print ip}'"
        print("Your car's ip address is:" )
        os.system(cmd)
        os.system(cmdRPi4)

class CalibrateCar(BaseCommand):

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='calibrate', usage='%(prog)s [options]')
        parser.add_argument('--channel', help="The channel you'd like to calibrate [0-15]")
        parser.add_argument('--address', default='0x40', help="The i2c address you'd like to calibrate [default 0x40]")
        parser.add_argument('--bus', default=None, help="The i2c bus you'd like to calibrate [default autodetect]")
        parser.add_argument('--pwmFreq', default=60, help="The frequency to use for the PWM")
        parser.add_argument('--arduino', dest='arduino', action='store_true', help='Use arduino pin for PWM (calibrate pin=<channel>)')
        parser.set_defaults(arduino=False)
        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        channel = int(args.channel)

        if args.arduino == True:
            from donkeycar.parts.actuator import ArduinoFirmata

            arduino_controller = ArduinoFirmata(servo_pin=channel)
            print('init Arduino PWM on pin %d' %(channel))
            input_prompt = "Enter a PWM setting to test ('q' for quit) (0-180): "
        else:
            from donkeycar.parts.actuator import PCA9685
            from donkeycar.parts.sombrero import Sombrero

            s = Sombrero()

            busnum = None
            if args.bus:
                busnum = int(args.bus)
            address = int(args.address, 16)
            print('init PCA9685 on channel %d address %s bus %s' %(channel, str(hex(address)), str(busnum)))
            freq = int(args.pwmFreq)
            print("Using PWM freq: {}".format(freq))
            c = PCA9685(channel, address=address, busnum=busnum, frequency=freq)
            input_prompt = "Enter a PWM setting to test ('q' for quit) (0-1500): "
            print()
        while True:
            try:
                val = input(input_prompt)
                if val == 'q' or val == 'Q':
                    break
                pmw = int(val)
                if args.arduino == True:
                    arduino_controller.set_pulse(channel,pmw)
                else:
                    c.run(pmw)
            except KeyboardInterrupt:
                print("\nKeyboardInterrupt received, exit.")
                break
            except Exception as ex:
                print("Oops, {}".format(ex))

class MakeMovieShell(BaseCommand):
    '''
    take the make movie args and then call make movie command
    with lazy imports
    '''
    def __init__(self):
        self.deg_to_rad = math.pi / 180.0

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='makemovie')
        parser.add_argument('--tub', help='The tub to make movie from')
        parser.add_argument('--out', default='tub_movie.mp4', help='The movie filename to create. default: tub_movie.mp4')
        parser.add_argument('--config', default='./config.py', help=HELP_CONFIG)
        parser.add_argument('--model', default=None, help='the model to use to show control outputs')
        parser.add_argument('--type', default=None, required=False, help='the model type to load')
        parser.add_argument('--salient', action="store_true", help='should we overlay salient map showing activations')
        parser.add_argument('--start', type=int, default=0, help='first frame to process')
        parser.add_argument('--end', type=int, default=-1, help='last frame to process')
        parser.add_argument('--scale', type=int, default=2, help='make image frame output larger by X mult')
        parser.add_argument('--draw-user-input', default=True, action='store_false', help='show user input on the video')
        parsed_args = parser.parse_args(args)
        return parsed_args, parser

    def run(self, args):
        '''
        Load the images from a tub and create a movie from them.
        Movie
        '''
        args, parser = self.parse_args(args)

        from donkeycar.management.makemovie import MakeMovie

        mm = MakeMovie()
        mm.run(args, parser)

class ShowCnnActivations(BaseCommand):

    def __init__(self):
        import matplotlib.pyplot as plt
        self.plt = plt

    def get_activations(self, image_path, model_path, cfg):
        '''
        Extracts features from an image

        returns activations/features
        '''
        from tensorflow.python.keras.models import load_model, Model

        model_path = os.path.expanduser(model_path)
        image_path = os.path.expanduser(image_path)

        model = load_model(model_path, compile=False)
        image = load_image(image_path, cfg)[None, ...]

        conv_layer_names = self.get_conv_layers(model)
        input_layer = model.get_layer(name='img_in').input
        activations = []
        for conv_layer_name in conv_layer_names:
            output_layer = model.get_layer(name=conv_layer_name).output

            layer_model = Model(inputs=[input_layer], outputs=[output_layer])
            activations.append(layer_model.predict(image)[0])
        return activations

    def create_figure(self, activations):
        import math
        cols = 6

        for i, layer in enumerate(activations):
            fig = self.plt.figure()
            fig.suptitle('Layer {}'.format(i+1))

            print('layer {} shape: {}'.format(i+1, layer.shape))
            feature_maps = layer.shape[2]
            rows = math.ceil(feature_maps / cols)

            for j in range(feature_maps):
                self.plt.subplot(rows, cols, j + 1)

                self.plt.imshow(layer[:, :, j])

        self.plt.show()

    def get_conv_layers(self, model):
        conv_layers = []
        for layer in model.layers:
            if layer.__class__.__name__ == 'Conv2D':
                conv_layers.append(layer.name)
        return conv_layers

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='cnnactivations', usage='%(prog)s [options]')
        parser.add_argument('--image', help='path to image')
        parser.add_argument('--model', default=None, help='path to model')
        parser.add_argument('--config', default='./config.py', help=HELP_CONFIG)

        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        cfg = load_config(args.config)
        activations = self.get_activations(args.image, args.model, cfg)
        self.create_figure(activations)

class ShowHistogram(BaseCommand):

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='tubhist', usage='%(prog)s [options]')
        parser.add_argument('--tub', nargs='+', help='paths to tubs')
        parser.add_argument('--record', default=None, help='name of record to create histogram')
        parser.add_argument('--out', default=None, help='path where to save histogram end with .png')
        parsed_args = parser.parse_args(args)
        return parsed_args

    def show_histogram(self, tub_paths, record_name, out):
        '''
        Produce a histogram of record type frequency in the given tub
        '''
        from matplotlib import pyplot as plt
        from donkeycar.parts.datastore import TubGroup
        from donkeycar.parts.tub_v2 import Tub
        import pandas as pd
        output = out or os.path.basename(tub_paths)

        base_path = Path(os.path.expanduser(tub_paths)).absolute().as_posix()
        tub = Tub(base_path)
        records = list(tub)
        user_angles = []
        user_throttles = []

        for record in records:
            user_angle = float(record["user/angle"])
            user_throttle = float(record["user/throttle"])

            user_angles.append(user_angle)
            user_throttles.append(user_throttle)

        df = pd.DataFrame({'user_angle': user_angles, 'user_throttle': user_throttles})

        if record_name is not None:
            df[record_name].hist(bins=50)
        else:
            df.hist(bins=50)

        try:
            if out is not None:
                filename = output
            else:
                if record_name is not None:
                    filename = output + '_hist_%s.png' % record_name.replace('/', '_')
                else:
                    filename = output + '_hist.png'
            plt.savefig(filename)
            print('saving image to:', filename)
        except Exception as e:
            print(e)
        # plt.show()

    def run(self, args):
        args = self.parse_args(args)
        args.tub = ','.join(args.tub)
        self.show_histogram(args.tub, args.record, args.out)

class ShowPredictionPlots(BaseCommand):

    def plot_predictions(self, cfg, tub_paths, model_path, limit, model_type):
        '''
        Plot model predictions for angle and throttle against data from tubs.

        '''
        import matplotlib.pyplot as plt
        import pandas as pd

        model_path = os.path.expanduser(model_path)
        model = dk.utils.get_model_by_type(model_type, cfg)
        # This just gets us the text for the plot title:
        if model_type is None:
            model_type = cfg.DEFAULT_MODEL_TYPE
        model.load(model_path)

        user_angles = []
        user_throttles = []
        pilot_angles = []
        pilot_throttles = []

        from donkeycar.parts.tub_v2 import Tub
        from pathlib import Path

        base_path = Path(os.path.expanduser(tub_paths)).absolute().as_posix()
        tub = Tub(base_path)
        records = list(tub)
        records = records[:limit]
        bar = IncrementalBar('Inferencing', max=len(records))

        for record in records:
            img_filename = os.path.join(base_path, Tub.images(), record['cam/image_array'])
            img = load_image(img_filename, cfg)
            user_angle = float(record["user/angle"])
            user_throttle = float(record["user/throttle"])
            pilot_angle, pilot_throttle = model.run(img)

            user_angles.append(user_angle)
            user_throttles.append(user_throttle)
            pilot_angles.append(pilot_angle)
            pilot_throttles.append(pilot_throttle)
            bar.next()

        angles_df = pd.DataFrame({'user_angle': user_angles, 'pilot_angle': pilot_angles})
        throttles_df = pd.DataFrame({'user_throttle': user_throttles, 'pilot_throttle': pilot_throttles})

        fig = plt.figure()

        title = "Model Predictions\nTubs: " + tub_paths + "\nModel: " + model_path + "\nType: " + model_type
        fig.suptitle(title)

        ax1 = fig.add_subplot(211)
        ax2 = fig.add_subplot(212)

        angles_df.plot(ax=ax1)
        throttles_df.plot(ax=ax2)

        ax1.legend(loc=4)
        ax2.legend(loc=4)

        plt.savefig(model_path + '_pred.png')
        plt.show()

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='tubplot', usage='%(prog)s [options]')
        parser.add_argument('--tub', nargs='+', help='The tub to make plot from')
        parser.add_argument('--model', default=None, help='model for predictions')
        parser.add_argument('--limit', type=int, default=1000, help='how many records to process')
        parser.add_argument('--type', default=None, help='model type')
        parser.add_argument('--config', default='./config.py', help=HELP_CONFIG)
        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        args.tub = ','.join(args.tub)
        cfg = load_config(args.config)
        self.plot_predictions(cfg, args.tub, args.model, args.limit, args.type)

class Train(BaseCommand):

    def parse_args(self, args):
        HELP_FRAMEWORK = 'the AI framework to use (tensorflow|pytorch). ' \
                         'Defaults to config.DEFAULT_AI_FRAMEWORK'
        parser = argparse.ArgumentParser(prog='train', usage='%(prog)s [options]')
        parser.add_argument('--tub', nargs='+', help='tub data for training')
        parser.add_argument('--model', default=None, help='output model name')
        parser.add_argument('--type', default=None, help='model type')
        parser.add_argument('--config', default='./config.py', help=HELP_CONFIG)
        parser.add_argument('--framework',
                            choices=['tensorflow', 'pytorch', None],
                            required=False,
                            help=HELP_FRAMEWORK)
        parser.add_argument('--checkpoint', type=str,
                            help='location of checkpoint to resume training from')
        parser.add_argument('--transfer', type=str, help='transfer model')
        parser.add_argument('--comment', type=str,
                            help='comment added to model database - use '
                                 'double quotes for multiple words')
        parsed_args = parser.parse_args(args)
        return parsed_args

    def run(self, args):
        args = self.parse_args(args)
        args.tub = ','.join(args.tub)
        cfg = load_config(args.config)
        framework = args.framework if args.framework \
            else getattr(cfg, 'DEFAULT_AI_FRAMEWORK', 'tensorflow')

        if framework == 'tensorflow':
            from donkeycar.pipeline.training import train
            train(cfg, args.tub, args.model, args.type, args.transfer,
                  args.comment)
        elif framework == 'pytorch':
            from donkeycar.parts.pytorch.torch_train import train
            train(cfg, args.tub, args.model, args.type,
                  checkpoint_path=args.checkpoint)
        else:
            print(f"Unrecognized framework: {framework}. Please specify one of "
                  f"'tensorflow' or 'pytorch'")

class TrainRemote(BaseCommand):
    """
    train models on a remote server
    """
    # example command:
    #     donkey trainremote --tub ~/mycar/data/tub_4_21-06-11/ ~/mycar/data/tub_3_21-06-11/ --path ~/mycar
    #     --url https://hq.robocarstore.com/train/submit_job --get https://hq.robocarstore.com/train/refresh_job_statuses --model modelname

    WLAN="wlan0"
    HOTSPOT_IF_NAME="uap0"
    REFRESH_JOB_STATUS_URL = "https://hq.robocarstore.com/train/refresh_job_statuses"
    SUBMIT_JOB_URL = "https://hq.robocarstore.com/train/submit_job"
    TIMEOUT = 900 # limit the training to 15min

    def parse_args(self, args):
        parser = argparse.ArgumentParser(prog='train', usage='%(prog)s [options]')
        parser.add_argument('--tub', nargs='+', help='tub data for training')
        parser.add_argument('--path', default=None, help='path where to create car folder')
        parser.add_argument('--url', default=None, help='url of the remote server to submit job')
        parser.add_argument('--get', default=None, help='url of the remote server to get statuses')
        parser.add_argument('--model', default=None, help='output model name')

        parsed_args = parser.parse_args(args)
        return parsed_args

    def generate_tub_archive(self, tub_paths, carapp_path):
        print("generating tub archive")
        f = tempfile.NamedTemporaryFile(mode='w+b', suffix='.tar.gz', delete=False)

        with tarfile.open(fileobj=f, mode='w:gz') as tar:
            for tub_path in tub_paths:
                p = Path(tub_path)
                tar.add(p, arcname=p.name)
            tar.add(f"{carapp_path}/myconfig.py", arcname="myconfig.py")

        f.close()

        return f.name

    def get_wlan_mac_address(self, wlan):
        interfaces = netifaces.interfaces()
        if wlan not in interfaces:
            # logger.error(f"Network interface is not properly configured. {interface} does not exists.")
            return "None"
        addrs =  netifaces.ifaddresses(wlan)

        if addrs is None:
            return "None"

        if (netifaces.AF_LINK in addrs) and (len(addrs[netifaces.AF_LINK]) == 1):
            return addrs[netifaces.AF_LINK][0]['addr']
        else:
            return "None"

    def submit_job(self, tub_paths, carapp_path, submit_job_url):
        filename = self.generate_tub_archive(tub_paths, carapp_path)
        mp_encoder = MultipartEncoder(
            fields={
                'device_id': self.get_wlan_mac_address(self.WLAN),
                'hostname' : gethostname(),
                'tub_archive_file': ('file.tar.gz', open(filename, 'rb'), 'application/gzip'),
                'donkeycar_version': str(__version__)
            }
        )
        print(f"URL submitted to: {submit_job_url}")
        print(f"Data to submit: {mp_encoder}")
        r = requests.post(
            submit_job_url,
            data=mp_encoder,  # The MultipartEncoder is posted as data, don't use files=...!
            # The MultipartEncoder provides the content-type header with the boundary:
            headers={'Content-Type': mp_encoder.content_type}
        )
        print(f"Submission response: HTTP {r.status_code}")
        if (r.status_code == 200):
            # if HTTP 200 OK
            if ("job_uuid" in r.json()):
                try:
                    uuid = r.json()['job_uuid']
                    print(f"Submitted a Training Job to the remote server {submit_job_url}\n uuid: {uuid}")
                    return uuid
                except Exception as e:
                    print(e)
                    raise Exception("Failed to call submit job")
            else:
                raise Exception("Failed to call submit job")
        else:
            raise Exception("Failed to call submit job")

    def get_latest_job_status_from_hq(self, refresh_job_statuses, job_uuids):
        print(f"Getting lastest job status for uuid {job_uuids}")
        # job_uuids = [job_uuid for job_uuid in job_uuids if job_uuid]
        response = requests.post(refresh_job_statuses, data={"job_uuids": job_uuids})
        if response.status_code == 200:
            return response.json()
        else:
            print(response.status_code)
            print(response.content)
            raise Exception("Problem requesting latest job status from hq")

    def run(self, args):
        args = self.parse_args(args)
        tub_paths = list(args.tub)
        car_path = make_dir(args.path or '~/mycar')
        if args.url:
            url = args.url
        else:
            url = self.SUBMIT_JOB_URL
        if args.get:
            url_status = args.get
        else:
            url_status = self.REFRESH_JOB_STATUS_URL

        uuid = self.submit_job(tub_paths, car_path, url)
        if uuid:
            if args.model:
                model_name = args.model
            else:
                model_name = f"job_{uuid}"

            start_time = time.time()
            run_time = 0
            checkagain = True
            while checkagain and (run_time < self.TIMEOUT):
                result = self.get_latest_job_status_from_hq(url_status, uuid)[0]
                checkagain = result['status'] == "SCHEDULED" or result['status'] == "TRAINING"
                print(f"Training Status:{result['status']} at {datetime.now().strftime('%Y-%m-%d, %H:%M:%S')}")

                # print(result)
                time.sleep(5)
                if result['status'] == "COMPLETED":
                    print("Training completed!!")
                    print(f"Model URL: {result['model_url']}")
                    print(f"Model Accuracy URL: {result['model_accuracy_url']}")
                    print(f"Model Movie URL: {result['model_movie_url']}")

                    if not os.path.isdir(f"{car_path}/movies/"):
                        print(f"Creating movie folder '{car_path}/movies/' ")
                        os.mkdir(f"{car_path}/movies/")

                    command = ["curl", "--fail", result['model_url'], "--output", f"{car_path}/models/{model_name}.h5"]
                    proc = subprocess.Popen(command)
                    command = ["curl", "--fail", result['model_accuracy_url'], "--output", f"{car_path}/models/{model_name}.png"]
                    proc = subprocess.Popen(command)
                    command = ["curl", "--fail", result['model_movie_url'], "--output", f"{car_path}/movies/{model_name}.mp4"]
                    proc = subprocess.Popen(command)
                    print("Downloaded model, accuracy graph and movie from the URLs")
                    break
                elif result['status'] in ["NO_CAPACITY", "NO_QUOTA", "SPOT_REQUEST_FAILED", "TIMEOUT"]:
                    raise Exception(f"Failed to train the submitted job\nError : {result['status']}")

                run_time = time.time()-start_time
                print(f"Time spent: {run_time} s")

class Gui(BaseCommand):
    def run(self, args):
        from donkeycar.management.kivy_ui import main
        main()

def execute_from_command_line():
    """
    This is the function linked to the "donkey" terminal command.
    """
    commands = {
        'createcar': CreateCar,
        'findcar': FindCar,
        'calibrate': CalibrateCar,
        'tubclean': TubManager,
        'tubhist': ShowHistogram,
        'tubplot': ShowPredictionPlots,
        'makemovie': MakeMovieShell,
        'createjs': CreateJoystick,
        'cnnactivations': ShowCnnActivations,
        'update': UpdateCar,
        'train': Train,
        'trainremote': TrainRemote,
        'ui': Gui,
    }

    args = sys.argv[:]

    if len(args) > 1 and args[1] in commands.keys():
        command = commands[args[1]]
        c = command()
        c.run(args[2:])
    else:
        dk.utils.eprint('Usage: The available commands are:')
        dk.utils.eprint(list(commands.keys()))

if __name__ == "__main__":
    execute_from_command_line()


"""
URL configuration for core project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView

from accounts.views import AboutView, ContactView, FeedView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('i18n/', include('django.conf.urls.i18n')),
    path('', FeedView.as_view(), name='feed'),
    path('blogs/', include('blogs.urls'), name='blogs'),
    path('about/', AboutView.as_view(), name='about'),
    path('contact/', ContactView.as_view(), name='contact'),
    path('authentications/', include('authentications.urls')),
    path('api/', include('api.urls')),
    path('accounts/', include('accounts.urls'), name='accounts'),
    path(
        'robots.txt',
        TemplateView.as_view(
            template_name='accounts/robots.txt',
            content_type='text/plain'
        ),
        name='robots'
    ),
]

handler404 = 'accounts.views.not_found'
handler500 = 'accounts.views.bad_request'


from discord import *
from google.cloud import firestore

class Note(Cog):
    def __init__(self, bot):
        self.bot = bot
        # self._last_member = None
    # @commands.slash_command(description='投稿するnoteを作成する')
    # async def setnote(self, ctx: ApplicationContext, notechannelid: str, value: str):
    #     db = firestore.Client()
    #     guilddbRef = db.collection(str(message.guild.id)).document('settings')
        # try:
        # toSendMsgs = guilddbRef.get().to_dict()['note_channels']
        #
        # except:
        #     return
        # sendmsgscol = guilddbRef.collection('note_channels')
        # note_channels = guilddbRef.collection('note_channels')
        # note_channels: firestore.CollectionReference
        # note_channels.add()
    @Cog.listener()
    async def on_ready(self):
        print("note ready.")
    @Cog.listener()
    async def on_message(self, message: Message):
        if message.author.bot:
            return
        db = firestore.Client()
        guilddbRef = db.collection(str(message.guild.id)).document('settings')
        try:
            toSendMsg = guilddbRef.get().to_dict()['note_channels'][str(message.channel.id)]
        except:
            return
        if toSendMsg is None:
           return
        var1 = await message.channel.history(limit=15).flatten()
        sentTxt1 = await message.channel.send(embed=Embed(description=toSendMsg))

        self.bot: Bot
        for x in var1:
            if x.author.id == self.bot.user.id:
                await x.delete()

                # return
            # break
                # return
                # break
                # break
    # @commands.slash_command(description=)
def setup(bot):
    bot.add_cog(Note(bot))


import os
import pathlib

import pytest

from pytest_ekstazi.plugin import DEFAULT_CONFIG_FILE

from .constants import TESTING_PROJECT_TEST_ROOT, CUSTOM_CONFIGURATION_FILE
from .utils import run_pytest, extract_test_case_results

CONFIGURATION_FILES = [DEFAULT_CONFIG_FILE, CUSTOM_CONFIGURATION_FILE]

@pytest.fixture(autouse=True)
def delete_configuration_file():
    for configuration_file in CONFIGURATION_FILES:
        file_path = TESTING_PROJECT_TEST_ROOT / pathlib.Path(configuration_file)
        if file_path.exists():
            os.remove(file_path)
    yield
    for configuration_file in CONFIGURATION_FILES:
        file_path = TESTING_PROJECT_TEST_ROOT / pathlib.Path(configuration_file)
        if file_path.exists():
            os.remove(file_path)

@pytest.fixture(scope='session')
def project_test_cases():
    output = run_pytest()[1]
    return set(extract_test_case_results(output).keys())


# Algoritmo de prim

class Vertex:
    def __init__(self, index):
        self.usado = False
        self.custo = float('inf')

class Grafo:
    def __init__(self, n):
        self.n = n
        self.matrix_arestas = [[float('inf') for _ in range(n)] for _ in range(n)]
        self.mst = []
        self.custos = [Vertex(i) for i in range(n)]

    def add(self, u, v, custo):
        self.matrix_arestas[u][v] = custo
        self.matrix_arestas[v][u] = custo

    def prim(self, s):
        self.custos[s].custo = 0
        while len(self.mst) < self.n:
            u = self.min()
            self.mst.append(u)
            self.custos[u].usado = True
            for v in range(self.n):
                if self.matrix_arestas[u][v] != float('inf'):
                    if self.custos[v].custo > self.matrix_arestas[u][v] and not self.custos[v].usado:
                        self.custos[v].custo = self.matrix_arestas[u][v]

    def min(self):
        min = float('inf')
        u = -1
        for v in range (self.n):
            if self.custos[v].custo < min and not self.custos[v].usado:
                min = self.custos[v].custo
                u = v
        return u

    def soma(self):
        soma = 0
        for v in self.custos:
            soma += v.custo
        return soma

# Exemplo de uso
g = Grafo(5)
g.add(0, 1, 1)
g.add(0, 2, 7)
g.add(1, 2, 5)
g.add(1, 3, 4)
g.add(1, 4, 3)
g.add(2, 4, 6)
g.add(3, 4, 2)

g.prim(0)
print(g.soma())

5 - Teoria dos Grafos/Aulas/Semana 08/prim.py


import sys
from time import sleep

import pygame

from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from ship import Ship
from bullet import Bullet
from alien import Alien

class AlienInvasion:
    """Класс для управления ресурсами и поведением игры"""

    def __init__(self):
        """Инициализирует игру и создаёт игровые ресурсы"""
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Alien Invasion")

        #sozdanie ekzemplyara dlya hraneniya igrovoy statistiki
        self.stats = GameStats(self)
        self.sb = Scoreboard(self)

        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        self._create_fleet()

        # sozdanie knopki Play
        self.play_button = Button(self, "Play")

    def run_game(self):
         """Запуск основного цикла игры"""
         while True:
            self._check_events()

            if self.stats.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_aliens()

            self._update_screen()

    def _check_events(self):
        # Обрабатывает нажатия клавиш клавиатуры и и мыши
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_play_button(self, mouse_pos):
        """zapuskaet novuu igru pri nazhatii knopki Play"""
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            # sbros igrovih nastroek
            self.settings.initialize_dynamic_settings()
            # sbros igrovoy statistiki
            self.stats.reset_stats()
            self.stats.game_active = True
            self.sb.prep_score()
            self.sb.prep_level()
            self.sb.prep_ships()

            # ochistka spiskov prishelcev i snaryadov
            self.aliens.empty()
            self.bullets.empty()

            # sozdanie novogo flota i razmeshenie korovlya v centre
            self._create_fleet()
            self.ship.center_ship()

            # ukazatel mishi skrivaetsya
            pygame.mouse.set_visible(False)

    def _check_keydown_events(self, event):
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = True
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = True
                elif event.key == pygame.K_q:
                    sys.exit()
                elif event.key == pygame.K_SPACE:
                    self._fire_bullet()

    def _check_keyup_events(self, event):
                if event.key == pygame.K_RIGHT:
                    self.ship.moving_right = False
                elif event.key == pygame.K_LEFT:
                    self.ship.moving_left = False

    def _fire_bullet(self):
        """sozdanie novogo snaryada i vkluchenie ego v group bullets"""
        if len(self.bullets) < self.settings.bullet_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        self.bullets.update()
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)

        self._check_bullet_alien_collisions()

    def _check_bullet_alien_collisions(self):
        #proverka popodaniy v prishelcev
        # pri obnaruzhenii kolliziy udalit' snaryad i prishelca
        collisions = pygame.sprite.groupcollide(
            self.bullets, self.aliens, True, True)

        if collisions:
            for aliens in collisions.values():
                self.stats.score += self.settings.alien_points * len(aliens)
            self.sb.prep_score()
            self.sb.check_high_score()

        if not self.aliens:
            #unichtozhenie sushestvushih snaryadov i sozdanie novogo flota
            self.bullets.empty()
            self._create_fleet()
            self.settings.increase_speed()

            # uvelichenie urovnya
            self.stats.level += 1
            self.sb.prep_level()

    def _update_aliens(self):
        """obnovlyaet pozicii prishelcev vo flote"""
        self._check_fleet_edges()
        self.aliens.update()

        #proverka koliziy prishelec-korabl
        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            self._ship_hit()

        #proverit' dobralis li prishelci do nizhnego kraya ekrana
        self._check_aliens_bottom()

    def _ship_hit(self):
        """obrabativaet stolknovenie koroblya s prishelcem"""
        # umenshaet ships_left
        if self.stats.ships_left > 0:
            self.stats.ships_left -= 1
            self.sb.prep_ships

            #ochistka spiskov prishelcev i snaryadov
            self.aliens.empty()
            self.bullets.empty()

            # sozdanie novogo flota i razmeshemie koroblya v centre
            self._create_fleet()
            self.ship.center_ship()

            #pauza
            sleep(0.5)
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)

    def _check_aliens_bottom(self):
        """proveryaet dobralis li prishelci do nizhnego kraya ekrana"""
        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >= screen_rect.bottom:
                #proishodit to zhe chto pri stolknovenii s korablem
                self._ship_hit()
                break

    def _update_screen(self):
        # Обновляет изображения на экране и отображает новый экран.
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)

        # vivodit informaciu o schete
        self.sb.show_score()

        # knopka Play otobrazhaetsya v tom sluchae esli igra neaktivna
        if not self.stats.game_active:
            self.play_button.draw_button()

        pygame.display.flip()

    def _create_fleet(self):
        """sozdanie flota vtorzheniya"""
        #sozdaniya prishelca
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        available_space_x = self.settings.screen_width - (2 * alien_width)
        number_aliens_x = available_space_x // (2 * alien_width)

        # opredelyaet kolichestvo ryadov pomeshaychihsya na ekrane
        ship_height = self.ship.rect.height
        available_space_y = (self.settings.screen_height -
            (3 * alien_height) - ship_height)
        number_rows = available_space_y // (2 * alien_height)

        #sozdanie flota vtorzheniya
        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number, row_number)

    def _create_alien(self, alien_number, row_number):
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
        self.aliens.add(alien)

    def _check_fleet_edges(self):
        """reagiruet na dostezhenie prishelcem kraya ekrana"""
        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        """opuskaet ves flot i menyaet napravlenie flota"""
        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed
        self.settings.fleet_direction *= -1

if __name__ == '__main__':
    # Создание экземпляра и запуск игры
    ai = AlienInvasion()
    ai.run_game()


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect
from django.urls import reverse

# def monday(request):
#     return HttpResponse('Good day monday')
#
# def tuesday(request):
#     return HttpResponse('Good day tuesday')

all_day = {
    'monday': 'Nice Monday',
    'tuesday': 'Nice Tuesday',
    'wednesday': 'Nice Wednesday',
    'thursday': 'Nice Thursday',
    'friday': 'Nice Friday',
    'saturday': 'Nice Saturday, putty',
    'sunday': 'Nice Sunday, do nay stop',
}

def index(request):
    days = list(all_day)
    context = {
        'days': days
    }
    return render(request, 'weekdays/index.html', context=context)
    # list_days = ''
    # for day_of in days:
    #     redirect_days = reverse('revers_url_day', args=[day_of])
    #     list_days += f"
{day_of}
"
    #
    # click_day = f'''
    #

    # {list_days}
    #

    # '''
    # return HttpResponse(click_day)

def create_day(request, new_day: str):
    describe_day = all_day.get(new_day)
    context = {
        'describe_day': describe_day,
        'new_day': new_day,
    }
    return render(request, 'weekdays/info_weekdays.html', context=context)
    # week = all_day.get(new_day, None)
    # if week:
    #     return HttpResponse(f'
{week}
')
    # # if new_day == 'monday':
    # #     return HttpResponse('Good day monday')
    # # elif new_day == 'tuesday':
    # #     return HttpResponse('Good day tuesday')
    # else:
    #     return HttpResponseNotFound(f'{new_day} is not of week')

def create_day_number(request, new_day: int):
    # if new_day <= 7:
    #     return HttpResponse(f'Today is {new_day} of the week')
    # else:
    #     return HttpResponseNotFound(f'Unknown number day {new_day}')
    days = list(all_day)
    if new_day > len(days):
        return HttpResponseNotFound(f'Unknown number day {new_day}')
    name_day = days[new_day - 1]
    urls_days = reverse('revers_url_day', args=[name_day])
    return HttpResponseRedirect(urls_days)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import traceback
from os import getenv

from dotenv import load_dotenv, find_dotenv
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from tests.test_handler import test
from harjutused.harjuta import harjuta
from soovitused.soovita import soovita
from stats.stats import register_user
from utils.logger import get_logger

# Load env data
load_dotenv(find_dotenv())
telegram_token = getenv('TELEGRAM_TOKEN')

logger = get_logger(__name__)

def start(update, context):
    """Send a message when the command /start is issued."""
    update.message.reply_text('Tere! Võib kirjutada käsku ```/abi```',
                              parse_mode='Markdown')
    try:
        register_user(update['message']['chat'])
    except:
        logger.error('Could not register user. Traceback: ' +
                     traceback.format_exc())
    logger.info('Started conversation\n\t\tUpdate:\n\t\t')
    logger.info(update)
    logger.info('\n\t\Context:\n\t\t')
    logger.info(context)

def abi(update, context):
    """Send a message when the command /help is issued."""
    kaust = os.path.dirname(os.path.realpath(__file__))
    with open(kaust + os.sep + 'abi.md', 'rb') as abi_fail:
        update.message.reply_text(
            abi_fail.read().decode('utf-8'),
            parse_mode='Markdown')

def kaja(update, context):
    """Kajasta kasutaja sõnumit."""
    update.message.reply_text(update.message.text)

def brauser(update, context):
    """Rockroll õpilasi"""
    update.message.reply_text('[midagi](https://www.youtube.com/watch?v=2QeGa3OhRsA)',
                              parse_mode='Markdown')

def downloader(update, context):
    update.message.reply_text('Ma loodan, et sa ei proovi mind nakata. Meil on juba piisavalt viirused päris maailmas. ')
    test(update, context)

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    # Loading telegram token from enviroment variables
    updater = Updater(
        telegram_token, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("abi", abi))
    dp.add_handler(CommandHandler("kaja", kaja))
    dp.add_handler(CommandHandler("brauser", brauser))
    dp.add_handler(CommandHandler("harjuta", harjuta))
    dp.add_handler(CommandHandler("soovita", soovita))
    dp.add_handler(CommandHandler("test", test, pass_user_data=True))

    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(MessageHandler(Filters.text, kaja))
    dp.add_handler(MessageHandler(Filters.document, downloader))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()


import re

from . import util

def compile_fn(api, checkout_root, out_dir):
  skia_dir      = checkout_root.join('skia')
  compiler      = api.vars.builder_cfg.get('compiler')
  configuration = api.vars.builder_cfg.get('configuration')
  extra_tokens  = api.vars.extra_tokens
  os            = api.vars.builder_cfg.get('os')
  target_arch   = api.vars.builder_cfg.get('target_arch')

  assert compiler == 'Clang'  # At this rate we might not ever support GCC.

  extra_cflags = []
  extra_ldflags = []
  if configuration == 'Debug':
    extra_cflags.append('-O1')

  ndk_asset = 'android_ndk_linux'
  ndk_path = ndk_asset
  if 'Mac' in os:
    ndk_asset = 'android_ndk_darwin'
    ndk_path = ndk_asset
  elif 'Win' in os:
    ndk_asset = 'android_ndk_windows'
    ndk_path = 'n'

  quote = lambda x: '"%s"' % x
  args = {
      'ndk': quote(api.vars.workdir.join(ndk_path)),
      'target_cpu': quote(target_arch),
      'werror': 'true',
  }
  env = {}
  extra_cflags.append('-DREBUILD_IF_CHANGED_ndk_version=%s' %
                      api.run.asset_version(ndk_asset, skia_dir))

  if configuration != 'Debug':
    args['is_debug'] = 'false'
  if 'Dawn' in extra_tokens:
    util.set_dawn_args_and_env(args, env, api, skia_dir)
    args['ndk_api'] = 26 #skia_use_gl=false, so use vulkan
  if 'Vulkan' in extra_tokens:
    args['ndk_api'] = 26
    args['skia_enable_vulkan_debug_layers'] = 'false'
    args['skia_use_gl'] = 'false'
  if 'ASAN' in extra_tokens:
    args['sanitize'] = '"ASAN"'
  if 'Graphite' in extra_tokens:
    args['skia_enable_graphite'] = 'true'
  if 'HWASAN' in extra_tokens:
    args['sanitize'] = '"HWASAN"'
  if 'Wuffs' in extra_tokens:
    args['skia_use_wuffs'] = 'true'
  if configuration == 'OptimizeForSize':
    # build IDs are required for Bloaty if we want to use strip to ignore debug symbols.
    # https://github.com/google/bloaty/blob/master/doc/using.md#debugging-stripped-binaries
    extra_ldflags.append('-Wl,--build-id=sha1')
    args.update({
      'skia_use_runtime_icu': 'true',
      'skia_enable_optimize_size': 'true',
      'skia_use_jpeg_gainmaps': 'false',
    })

  # The 'FrameworkWorkarounds' bot is used to test special behavior that's
  # normally enabled with SK_BUILD_FOR_ANDROID_FRAMEWORK.
  if 'FrameworkWorkarounds' in extra_tokens:
    extra_cflags.append('-DSK_SUPPORT_LEGACY_ALPHA_BITMAP_AS_COVERAGE')

  # If an Android API level is specified, use that.
  for t in extra_tokens:
    m = re.search(r'API(\d+)', t)
    if m and len(m.groups()) == 1:
      args['ndk_api'] = m.groups()[0]
      break

  if extra_cflags:
    args['extra_cflags'] = repr(extra_cflags).replace("'", '"')
  if extra_ldflags:
    args['extra_ldflags'] = repr(extra_ldflags).replace("'", '"')

  gn_args = ' '.join('%s=%s' % (k,v) for (k,v) in sorted(args.items()))
  gn      = skia_dir.join('bin', 'gn')

  with api.context(cwd=skia_dir):
    api.run(api.python, 'fetch-gn',
            script=skia_dir.join('bin', 'fetch-gn'),
            infra_step=True)

    with api.env(env):
      api.run(api.step, 'gn gen',
              cmd=[gn, 'gen', out_dir, '--args=' + gn_args])
      api.run(api.step, 'ninja', cmd=['ninja', '-C', out_dir])

ANDROID_BUILD_PRODUCTS_LIST = [
  'dm',
  'nanobench',
  'skpbench',
  # The following only exists when building for OptimizeForSize
  # This is the only target we currently measure: skbug.com/13657
  'skottie_tool_gpu',
]

def copy_build_products(api, src, dst):
  """Copy Android build products from src to dst."""
  util.copy_listed_files(api, src, dst, ANDROID_BUILD_PRODUCTS_LIST)


# Zadanie 1.
# Dwie liczby naturalne s¡ ró»nocyfrowe, je»eli nie posiadaj¡ »adnej wspólnej cyfry. Prosz¦ napisa¢ program,
# który wczytuje dwie liczby naturalne i poszukuje najmniejszej podstawy systemu (w zakresie 2-16), w którym
# liczby s¡ ró»nocyfrowe. Program powinien wypisa¢ znalezion¡ podstaw¦; je»eli podstawa taka nie istnieje,
# nale»y wypisa¢ komunikat o jej braku.
# Na przykaad: dla liczb 123 i 522 odpowiedzi¡ jest podstawa 11, bo 123 (10) = 102 (11) i 522 (10) = 435 (11)

def to_base(num, base):
    res = []

    while num > 0:
        res.append(num%base)
        num //= base

    return res  # nie ma znaczenia czy zwrócę liczbę czy jej rewers
    # return res[::-1]

def diff_digits(num1: list, num2: list):
    amount = [False]*16

    for digit in num1:
        amount[digit]  = True

    for digit in num2:
        if amount[digit]:
            return False

    return True

def check(num1, num2):
    for base in range(2, 16+1):
        n1, n2 = to_base(num1, base), to_base(num2, base)
        if diff_digits(n1, n2):
            print(base)
            return

    print("NIE MA TAKIEJ PODSTAWY")

check(123,522)


import os
import cv2
import re
from paddleocr import PPStructure, draw_structure_result, save_structure_res, PaddleOCR, draw_ocr
from LCS import lcs
from dictionaries import companies, listaCNPJ
import pytesseract

from PIL import Image
from fuzzywuzzy import fuzz
from fuzzywuzzy import process

def runTesseractOCR(img_path):

    jsonResult = {}

    cnpjFlag = False
    flagVencimento = False
    flagValorTotal = False
    valorTotalCount = 0

    tempVencimento = ''

    nameLCS = 0

    poChoices = ['PO', 'NUMERO PEDIDO', 'NRO PEDIDO', 'ORDEM COMPRA']
    conChoices = ['NRO NOTA', 'NRO DA NOTA', 'NUMERO NOTA', 'NUMERO DA NOTA', 'NÚMERO NOTA', 'NÚMERO DA NOTA']
    vencimentoChoices = ['DATA', 'VENCIMENTO', 'DATA VENCIMENTO']
    valorChoices = ['VALOR NOTA', 'VALOR DA NOTA', 'VALOR TOTAL', 'TOTAL', 'TOTAL NOTA']

    img = cv2.imread(img_path)
    config_tesseract = '--oem 3  --psm 11'
    texto = pytesseract.image_to_string(img, config=config_tesseract)
    texto = texto.lower()

    tempResult = texto.split('\n')
    result =[]

    for r in tempResult:
        if r != '':
            result.append(r)
    print(result)

    for s in result:
        if fuzz.token_set_ratio(s.upper(), 'Nro da nota') > 80 or fuzz.token_set_ratio(s.lower(), 'con') > 80:
            if jsonResult.get('con') != None:
                if fuzz.token_set_ratio(s, 'Nro da nota') > fuzz.token_set_ratio(jsonResult.get('con'), 'Nro da nota'):
                    con = re.search(r'[a-zA-Z]*[0-9]+', s)
                    if con != None:
                        jsonResult['con'] = con.group()#s[s.find(':')+1:].strip()
            else:
                con = re.search(r'[a-zA-Z]*[0-9]+', s)
                if con != None:
                    jsonResult['con'] = con.group()#s[s.find(':')+1:].strip()

        #Razao Social
#            if fuzz.token_set_ratio(s, 'Razao Social') > 60 and fuzz.token_set_ratio(s, 'MERCK') < 80:
#                if jsonResult.get('nome') != None:
#                    if fuzz.token_set_ratio(s, 'Razao Social') > fuzz.token_set_ratio(jsonResult.get('nome'), 'Razao Social'):
#                        jsonResult['nome'] = s[s.find(':')+1:].strip()
#                else:
#                    jsonResult['nome'] = s[s.find(':')+1:].strip()

        #Razao Social
        for company in companies:
            n = fuzz.partial_ratio(s.upper(), company)
            tempLCS = lcs(s.upper(), company)
            if n > 90 and tempLCS > nameLCS:
                jsonResult['nome'] = company
                nameLCS = tempLCS

        #PO
        if fuzz.token_set_ratio(s, 'PO') > 80 or fuzz.token_set_ratio(s, 'NUMERO PEDIDO') > 80:
            if jsonResult.get('PO') != None:
                if fuzz.token_set_ratio(s, 'PO') > fuzz.token_set_ratio(jsonResult.get('PO'), 'PO'):
                    jsonResult['PO'] = s
                    po = re.search(r'[0-9]+', s)
                    if po != None:
                        jsonResult['PO'] = po.group()

            else:
                jsonResult['PO'] = s
                po = re.search(r'[0-9]+', s)
                if po != None:
                    jsonResult['PO'] = po.group()

        #VALOR
        if flagValorTotal == False:
            if process.extractOne(s.upper(), valorChoices, scorer=fuzz.token_set_ratio)[1] > 80:
                flagValorTotal = True
                if jsonResult.get('valor') != None:
                    if fuzz.token_set_ratio(s, 'VALOR') > fuzz.token_set_ratio(jsonResult.get('valor'), 'VALOR'):
                        jsonResult['valor'] = s
                        if s.find('R$') != -1:
                            jsonResult['valor'] = s[s.find('R$'):].strip()

                else:
                    if s.find('R$') != -1:
                            jsonResult['valor'] = s[s.find('R$'):].strip()

        if flagValorTotal == True and valorTotalCount <= 5:
            valorTotalCount += 1
            if re.search(r'(?:R$ ?)?[0-9]+(?:\.[0-9]+)?,[0-9][0-9]', s) != None:
                jsonResult['valor'] = re.search(r'(?:R$ ?)?[0-9]+(?:\.[0-9]+)?,[0-9][0-9]', s).group()

        #Vencimento
        if process.extractOne(s.upper(), vencimentoChoices, scorer=fuzz.token_set_ratio)[1] > 85 > 80 or flagVencimento == True:
            flagVencimento = True
            if jsonResult.get('vencimento') != None:
                if fuzz.token_set_ratio(s.upper(), 'VENCIMENTO') > fuzz.token_set_ratio(tempVencimento, 'Vencimento') \
                    and re.search(r'[0-9]+/[0-9]+/[0-9]+', s) != None:
                    tempVencimento = s
                    vencimento = re.search(r'[0-9]+/[0-9]+/[0-9]+', s)
                    if vencimento != None:
                        jsonResult['vencimento'] = vencimento.group()
            else:
                vencimento = re.search(r'[0-9]+/[0-9]+/[0-9]+', s)
                if vencimento != None:
                    jsonResult['vencimento'] = vencimento.group()

        #nome
        for company in companies:
            if fuzz.token_set_ratio(s, company) > 80:
                jsonResult['nome'] = company

        #CNPJ
        if cnpjFlag == False:
            for num in listaCNPJ:
                if fuzz.token_set_ratio(s, num) > 90:
                    jsonResult['CNPJ'] = num
                    cnpjFlag = True
                    break
                else:
                    cnpj = re.findall(r'[0-9]+', num)
                    stringCNPJ = ''
                    for p in cnpj:
                        stringCNPJ += p
                    if fuzz.token_set_ratio(s, stringCNPJ) > 90:
                        jsonResult['CNPJ'] = num
                        cnpjFlag = True
                        break

    print('end of for loop \n')
    print(f'\nOutput tesseractReciboLocacao: {jsonResult} \n')

    return jsonResult

#res = runTesseractOCR('AGV_RL_0.jpg')
#print(res)


import numpy as np
import cv2

def image_t(im, scale=1.0, rot=45, trans=(50,-50)):
    # transform the rotation angle to radian
    rot_rad = np.pi * rot / 180

    # sample three points and compute their point coordinates in the target image
    m = np.float32([[scale * np.cos(rot_rad), scale * np.sin(rot_rad), trans[0]],
                    [-scale * np.sin(rot_rad), scale * np.cos(rot_rad), trans[1]],
                    [0, 0, 1]])
    src_pt1 = np.float32([0, 0, 1])
    src_pt2 = np.float32([0, 1, 1])
    src_pt3 = np.float32([1, 0, 1])
    dst_pt1 = np.dot(m, src_pt1)
    dst_pt2 = np.dot(m, src_pt2)
    dst_pt3 = np.dot(m, src_pt3)
    h, w, _ = im.shape
    center = np.float32([h/2, w/2])
    src_tri = np.float32([center + src_pt1[0:2], center + src_pt2[0:2], center + src_pt3[0:2]])
    dst_tri = np.float32([center + dst_pt1[0:2], center + dst_pt2[0:2], center + dst_pt3[0:2]])

    # calculate transformation matrix
    trans_mat = cv2.getAffineTransform(src_tri, dst_tri)

    # obtain the target image by using transform matrix
    result = cv2.warpAffine(im, trans_mat, (h, w))

    # return transformed result image
    return result

if __name__ == '__main__':
    im = cv2.imread('./misc/pearl.jpeg')

    scale  = 0.5
    rot    = 45
    trans  = (50, -50)
    result = image_t(im, scale, rot, trans)
    cv2.imwrite('./results/affine_result.png', result)

Assignment 1 - Image Processing Basic/code/runTransform.py


import requests as req
import bs4 as b

# Making a request.
res = req.get('https://www.learncodeonline.in')

# converting request object to soup object.
soup = b.BeautifulSoup(res.text,'lxml')

title = soup.select('title')

# getting all details from the data variable.

for i in title:
    print(i.getText())


import requests

class MockTest:
    def __init__(self, url, request_params1):
        self.url = url
        self.request_params1 = request_params1
        self.request_headers = {}

    def MockPort1(self):
        result = requests.get(url=self.url, params=self.request_params1, headers=self.request_headers, verify=False).text

        return result

if __name__ == '__main__':
    # obj = MockTest("https://mockapi.eolink.com/PE7cd7e93684a74e31d09980ae8c1b1fe10512d7b9ed829/api/personal/dashboard/sales/ranking?orderBy=qty&date=2023-03",'')
    obj = MockTest("http://192.168.110.13:8088/shopping_cart", '')
    print(obj.MockPort1())


from PyQt5 import uic, QtWidgets, QtCore
#import json
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
FORM_CLASS, _ = uic.loadUiType(os.path.join(dir_path, 'install_stream_dialog.ui'))

class InstallStreamDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent = None):
        super(InstallStreamDialog, self).__init__()
        # setup the ui from the Qt designer
        self.setupUi(self)
        self.val = None

        self.setWindowTitle("Change General parameters")

        self.parcourir_PB.clicked.connect(self.selectFolder)

    def selectFolder(self):
        file_dialog = QtWidgets.QFileDialog()
        selected_directory = file_dialog.getExistingDirectoryUrl(parent = self, caption="Sélectionner un dossier de téléchargement")

        if selected_directory:
            self.folder_LE.setText(selected_directory.toLocalFile())
            self.val = selected_directory.toLocalFile()


#!/usr/bin/env python3
import argparse
import requests
import time
import json
import sys

appid = '5fb5aa8f12acef8cc32c9e1d18451077'

def get_by_city(city):
    url = "https://api.openweathermap.org/data/2.5/weather?q={}&appid={}&units=imperial".format(city, appid)
    try:
        api_response = requests.get(url)
        api_response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print(err)
        sys.exit(1)
    data = api_response.json()
    return data

def get_by_zip(zip):
    url = "https://api.openweathermap.org/data/2.5/weather?zip={}&appid={}&units=imperial".format(zip, appid)
    try:
        api_response = requests.get(url)
        api_response.raise_for_status()
    except requests.exceptions.HTTPError as err:
        print(err)
        sys.exit(1)
    data = api_response.json()
    return data

def parse_and_format(api_response):
    city = api_response["name"]
    temp = api_response["main"]["temp"]
    humidity = api_response["main"]["humidity"]
    temp_min = api_response["main"]["temp_min"]
    temp_max = api_response["main"]["temp_max"]
    sunrise = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(api_response["sys"]["sunrise"]))
    sunset = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(api_response["sys"]["sunset"]))
    weather = """
    The average temperature in {}  is {}{}F with a high of {}{}F and a low of {}{}F

    Humidity is around {}{}

    Sunrise will be at {} and sunset at {}
    """.format(city, temp, '\u00b0', temp_max,'\u00b0', temp_min, '\u00b0', humidity, '\u0025', sunrise, sunset)
    return weather

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # require one of -city OR --zip but not both
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-c', '--city', action='store', nargs="+", help='the city name to fetch weather for')
    group.add_argument('-z', '--zip', action='store', help='the zip code to fetch weather for')
    args = parser.parse_args()
    if args.city:
        city = " ".join(args.city)
        response = get_by_city(city)
    else:
        response = get_by_zip(args.zip)
    weather = parse_and_format(response)
    print(weather)


import cv2
file_path = "./img/Ch02_img/girl.jpg"
img = cv2.imread(file_path)                                 # 이미지를 기본 값으로 받기
img_gray = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)      # 이미지를 흑백으로 받기

cv2.namedWindow("origin", cv2.WINDOW_AUTOSIZE)              # 원래 크기로 이미지 창 생성
cv2.namedWindow("gray", cv2.WINDOW_NORMAL)                  # 임의의 크기로 이미지 창 생성

cv2.imshow("origin", img)           # "origin" 창에 (기본값으로 받았던)이미지 표시
cv2.imshow("gray", img_gray)        # "gray" 창에 (흑백으로 받았던)이미지 표시

cv2.moveWindow("origin", 0,0)       # 창 위치 변경
cv2.moveWindow("gray", 100,100)     # 창 위치 변경

cv2.waitKey(0)                          # 아무키나 누르면
cv2.resizeWindow("origin", 200, 200)    # 창 크기 변경 (이미지 변경 불가, 그림이 짤려서 나옴)
cv2.resizeWindow("gray", 100, 100)      # 창 크기 변경 (변경 가능)

cv2.waitKey(0)
cv2.destroyWindow("gray")           # "gray" 창 닫기

cv2.waitKey(0)
cv2.destroyAllWindows()                 # 모든 창 닫기


def fib(fn):
    last=1
    pre_last=1
    if fn==1 or fn==2:
        return 1
    for i in range(0, fn-2):
        temp=last
        last+=pre_last
        pre_last=temp

    return last
def pierwsza(a):
    if a ==1:
        return False
    for z in range(2, a):
        if a % z == 0:
            return False
    return True

# print(fib(10))
# print(fib(20))
# print(fib(30))
# print(fib(40))
# print("\n")
# for i in range(1, 41):
#     if pierwsza(fib(i)):
#         print(fib(i))
# print("\n")
len_max=len(str(bin(fib(40)).replace("0b","")))-1
rep_binarna=""
fraktal_40=""
for i in range(1,40):
    line=str(bin(fib(i)).replace("0b",""))
    rep_binarna+=str(bin(fib(i)).replace("0b",""))+"\n"
    while len(line)!=len_max:
        line="0"+line
    # print(line)

# print(rep_binarna)

for i in range(1,40):
    liczba=0
    for z in bin(fib(i)).replace("0b",""):
        if z =="1":
            liczba+=1
    if liczba==6:
        print(bin(fib(i)).replace("0b",""))


def main():
    countries_count = int(input())

    all_cites = {}

    for _ in range(countries_count):
        country, *cities = input().split()
        for city in cities:
            all_cites[city] = country

    cities_count = int(input())
    for _ in range(cities_count):
        cit = input()
        print(all_cites[cit])

main()

homework for summer practice/Functional/Q.py


# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """

        if len(prices) == 0:
            return 0

        largest_profit = 0
        lowest = prices[0]

        for index in range(len(prices)):
            if index > 0:
                if prices[index] - lowest <= 0:
                    lowest = min(lowest, prices[index])
                else:
                    largest_profit = max(largest_profit, prices[index] - lowest)

        return largest_profit


import time
def chsum(str):
    x=0
    for t in str:
        x+=ord(t)-64
    return x

t=time.time()
f=open("C:\\names.txt")
x=f.read()
x=x.replace('"','').split(",")
x.sort()
sum=0
for n in range(0,len(x)):
    sum+=(n+1)*chsum(x[n])
print ('Sum:', sum, 'Time:', time.time() - t)


"""
Run simulations to calculate the expected revenue earned from auctions with
various constraints.
"""
import random
NUM_TRIALS = 1000

"""
2 bidder vickery auction uniform (0,1] bids, 1 item
What is the expected revenue from a vickery auction
with the above parameters?
"""
total = 0
for _ in range(NUM_TRIALS):
    b1 = random.random()
    b2 = random.random()
    total += min(b1,b2)
print ("Simulation 1")
print("\tExpected Vickery = ", total/NUM_TRIALS)

"""
2 bidder vickery auction uniform (0,1] bids, 1 item, reserve = 1/2
The reserve value is a floor on the price we are willing to sell the item at.
If both bids are below the reserve value, we do NOT sell the item.  If both
bids are above the reserve value, the highest bidder wins and pays the second
bidders bid.  If one bid is above the reserve value, the highest bidder wins
and pays the reserve value
"""
total = 0
sold = 0
reserve = 1/2.0
for _ in range(NUM_TRIALS):
    b1 = random.random()
    b2 = random.random()
    high_bid = max(b1,b2)
    low_bid = min(b1,b2)
    if high_bid < reserve:
        # we don't sell if neither bid meets the reserve
        continue
    else:
        total += max(reserve, low_bid)
        sold += 1

print ("Simulation 2")
print("\tWith the reserve set to {}, out of {} attempts, we sold the item {} \
times".format(reserve, NUM_TRIALS, sold))
print("\tExpected Vickery with 1/2 reserve= ", total/NUM_TRIALS)


class Solution:
  def fractionToDecimal(self, numerator, denominator):
    """
    :type numerator: int
    :type denominator: int
    :rtype: str
    """
    if numerator == 0:
      return "0"
    res = []
    if numerator / denominator < 0:
      res.append("-")
    num, den = abs(numerator), abs(denominator)
    res.append(str(num // den))
    num %= den
    if num == 0:
      return "".join(res)

    res.append(".")
    dic = dict()
    dic[num] = len(res)
    while num != 0:
      num *= 10
      res.append(str(num // den))
      num %= den
      if num in dic:
        index = dic.get(num)
        res.insert(index, "(")
        res.append(")")
        break
      else:
        dic[num] = len(res)
    return "".join(res)

s = Solution()
print(s.fractionToDecimal(6, 4))

Leetcode/166. Fraction to Recurring Decimal.py


from __future__ import absolute_import, print_function

from bottle import default_app, install, route, request, redirect, run, template

# Import eStore model http://editor.ponyorm.com/user/pony/eStore
from pony.orm.examples.estore import *
from pony.orm.integration.bottle_plugin import PonyPlugin

# After the plugin is installed each request will be processed
# in a separate database session. Once the HTTP request processing
# is finished the plugin does the following:
#  * commit the changes to the database (or rollback if an exception happened)
#  * clear the transaction cache
#  * return the database connection to the connection pool
install(PonyPlugin())

@route('/')
@route('/products/')
def all_products():
    # Get the list of all products from the database
    products = select(p for p in Product)
    return template('''

List of products

    %for p in products:

{{ p.name }}
    %end

    ''', products=products)

@route('/products/:id/')
def show_product(id):
    # Get the instance of the Product entity by the primary key
    p = Product[id]
    # You can traverse entity relationship attributes inside the template
    # In this examples it is many-to-many relationship p.categories
    # Since the data were not loaded into the cache yet,
    # it will result in a separate SQL query.
    return template('''

{{ p.name }}

Price: {{ p.price }}

Product categories:

    %for c in p.categories:

{{ c.name }}
    %end

    Edit product info
    Return to all products
    ''', p=p)

@route('/products/:id/edit/')
def edit_product(id):
    # Get the instance of the Product entity and display its attributes
    p = Product[id]
    return template('''
    <form action='/products/{{ p.id }}/edit/' method='post'>

          Product name:
          <input type="text" name="name" value="{{ p.name }}">

          Product price:
          <input type="text" name="price" value="{{ p.price }}">

      <input type="submit" value="Save!">
    </form>

Discard changes

Return to all products
    ''', p=p)

@route('/products/:id/edit/', method='POST')
def save_product(id):
    # Get the instance of the Product entity
    p = Product[id]
    # Update the attributes with the new values
    p.name = request.forms.get('name')
    p.price = request.forms.get('price')
    # We might put the commit() command here, but it is not necessary
    # because PonyPlugin will take care of this.
    redirect("/products/%d/" % p.id)
    # The Bottle's redirect function raises the HTTPResponse exception.
    # Normally PonyPlugin closes the session with rollback
    # if a callback function raises an exception. But in this case
    # PonyPlugin understands that this exception is not the error
    # and closes the session with commit.

run(debug=True, host='localhost', port=8080, reloader=True)


#-*- coding: utf8
from __future__ import print_function, division

import dataio
import numpy as np
import os

from prme import sgd

def learn(trace_fpath, nk, rate, regularization, alpha, tau,
        from_=0, to=np.inf, validation=0.1):

    dts, Trace, seen, hyper2id, obj2id = \
            dataio.initialize_trace(trace_fpath, from_, to)
    no = len(obj2id)
    nh = len(hyper2id)

    validation_from = int(len(dts) - len(dts) * validation)
    print('Using first %d of %d as train, rest is validation' \
            % (validation_from, len(dts)))

    dts_train = dts[:validation_from]
    Trace_train = Trace[:validation_from]

    rnd_idx = np.arange(len(dts_train))
    np.random.shuffle(rnd_idx)

    dts_train = np.asanyarray(dts_train[rnd_idx], dtype='f8', order='C')
    Trace_train = np.asanyarray(Trace_train[rnd_idx], dtype='i4', order='C')

    dts_val = np.asanyarray(dts[validation_from:], dtype='f8', order='C')
    Trace_val = np.asanyarray(Trace[validation_from:], dtype='i4', order='C')

    XG_ok = np.random.normal(0, 0.01, (no, nk))
    XP_ok = np.random.normal(0, 0.01, (no, nk))
    XP_hk = np.random.normal(0, 0.01, (nh, nk))

    cost_train, cost_val = sgd(dts, Trace, XG_ok, XP_ok, XP_hk, seen, rate, \
            regularization, alpha, tau, dts_val, Trace_val)

    rv = {}
    rv['num_topics'] = np.asarray([nk])
    rv['trace_fpath'] = np.asarray([os.path.abspath(trace_fpath)])
    rv['rate'] = np.asarray([rate])
    rv['regularization'] = np.asarray([regularization])
    rv['alpha'] = np.asarray([alpha])
    rv['tau'] = np.asarray([tau])
    rv['from_'] = np.asarray([from_])
    rv['cost_train'] = np.asarray([cost_train])
    rv['cost_val'] = np.asarray([cost_val])
    rv['to'] = np.asarray([to])
    rv['hyper2id'] = hyper2id
    rv['obj2id'] = obj2id
    rv['XG_ok'] = XG_ok
    rv['XP_ok'] = XP_ok
    rv['XP_hk'] = XP_hk
    return rv


from __future__ import print_function
import time
import sys
from pyEMG.robolimb import RoboLimb

r = RoboLimb()
r.start()
time.sleep(2)

movement_dict = {1:"cylindrical", 2:"lateral", 3:"tridigit_ext", 4:"bidigit_ext", 5:"pointer", 6:"thumbs_up"}

def main():
	try:
		while(True):
			grasp_input = get_input('Grasp (1:cylindrical, 2:lateral, 3:tridigit_ext, 4:bidigit_ext, 5:pointer, 6:thumbs_up, 0:exit)\n') # Wait for input after object placement
			if int(grasp_input) not in (list(movement_dict.keys()) + [0]):
				print("Movement not understood, pleas try again.")
				pass
			elif grasp_input == str(0):
				break
			else:
				r.grasp(movement_dict[int(grasp_input)])
				time.sleep(1.5)
				_ = get_input('Press to open')
				r.open_fingers()
				time.sleep(1.5)
	except KeyboardInterrupt:
		r.stop_all()
		print('\n')
	finally:
		print("Exiting...")
		r.open_all()
		time.sleep(1)
		r.close_finger(1)
		time.sleep(1)
		r.stop()
		sys.exit()

if __name__ == "__main__":
	# Support Python 2 and 3 input
	get_input = input
	if sys.version_info[:2] <= (2, 7):
	    get_input = raw_input

	main()


# coding: utf-8

# In[ ]:

import torch
from torchtext import data

SEED = 1234

torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

TEXT = data.Field(tokenize='spacy')
LABEL = data.LabelField(dtype=torch.float)

# In[2]:

#https://towardsdatascience.com/use-torchtext-to-load-nlp-datasets-part-i-5da6f1c89d84
from torchtext import datasets

import logging

import pandas as pd
import numpy as np

VAL_RATIO = 0.2
def prepare_csv(trainCsvPath, testCsvPath, seed=999):
    df_train = pd.read_csv(trainCsvPath)
    df_train["comment_text"] =         df_train.comment_text.str.replace("\n", " ")
    idx = np.arange(df_train.shape[0])
    np.random.seed(seed)
    np.random.shuffle(idx)
    val_size = int(len(idx) * VAL_RATIO)
    df_train.iloc[idx[val_size:], :].to_csv(
        "cache/dataset_train.csv", index=False)
    df_train.iloc[idx[:val_size], :].to_csv(
        "cache/dataset_val.csv", index=False)

    df_test = pd.read_csv(testCsvPath)
    df_test["comment_text"] =         df_test.comment_text.str.replace("\n", " ")
    df_test.to_csv("cache/dataset_test.csv", index=False)

import re
import spacy
NLP = spacy.load('en')
MAX_CHARS = 20000
def tokenizer(comment):
    comment = re.sub(
        r"[\*\"“”\n\\…\+\-\/\=\(\)‘•:\[\]\|’\!;]", " ",
        str(comment))
    comment = re.sub(r"[ ]+", " ", comment)
    comment = re.sub(r"\!+", "!", comment)
    comment = re.sub(r"\,+", ",", comment)
    comment = re.sub(r"\?+", "?", comment)
    if (len(comment) > MAX_CHARS):
        comment = comment[:MAX_CHARS]
    return [
        x.text for x in NLP.tokenizer(comment) if x.text != " "]

import torch
from torchtext import data
LOGGER = logging.getLogger("toxic_dataset")
def get_dataset(trainCsvPath, testCsvPath, fix_length=100, lower=False, vectors=None):
    if vectors is not None:
        # pretrain vectors only supports all lower cases
        lower = True
    LOGGER.debug("Preparing CSV files...")
    prepare_csv(trainCsvPath, testCsvPath)
    comment = data.Field(
        sequential=True,
        fix_length=fix_length,
        tokenize=tokenizer,
        pad_first=True,
        #tensor_type=torch.cuda.LongTensor,
        lower=lower
    )
    LOGGER.debug("Reading train csv file...")
    train, val = data.TabularDataset.splits(
        path='cache/', format='csv', skip_header=True,
        train='dataset_train.csv', validation='dataset_val.csv',
        fields=[
            ('id', None),
            ('comment_text', comment),
            ('toxic', data.Field(
                use_vocab=False, sequential=False)), #tensor_type=torch.cuda.ByteTensor
            ('severe_toxic', data.Field(
                use_vocab=False, sequential=False)), #tensor_type=torch.cuda.ByteTensor
            ('obscene', data.Field(
                use_vocab=False, sequential=False)), #tensor_type=torch.cuda.ByteTensor
            ('threat', data.Field(
                use_vocab=False, sequential=False)), #tensor_type=torch.cuda.ByteTensor
            ('insult', data.Field(
                use_vocab=False, sequential=False)), #tensor_type=torch.cuda.ByteTensor
            ('identity_hate', data.Field(
                use_vocab=False, sequential=False
                )),   #tensor_type=torch.cuda.ByteTensor
        ])
    LOGGER.debug("Reading test csv file...")
    test = data.TabularDataset(
        path='cache/dataset_test.csv', format='csv',
        skip_header=True,
        fields=[
            ('id', None),
            ('comment_text', comment)
        ])
    LOGGER.debug("Building vocabulary...")
    comment.build_vocab(
        train, val, test,
        max_size=20000,
        min_freq=50,
        vectors=vectors
    )
    LOGGER.debug("Done preparing the datasets")
    return train, val, test

def get_iterator(dataset, batch_size, train=True,
    shuffle=True, repeat=False):
    dataset_iter = data.Iterator(
        dataset, batch_size=batch_size, device=0,
        train=train, shuffle=shuffle, repeat=repeat,
        sort=False
    )
    return dataset_iter

dataSet = get_dataset("../../../data/nlp/jigsawtoxic/train.csv", "../../../data/nlp/jigsawtoxic/test.csv")

# In[3]:

#print(dataSet.shape)

# In[ ]:

import torchtext
from torchtext import data

# tokenizer function using spacy
nlp = spacy.load('en',disable=['parser', 'tagger', 'ner'])
def tokenizer(s):
    return [w.text.lower() for w in nlp(tweet_clean(s))]

def tweet_clean(text):
    text = re.sub(r'[^A-Za-z0-9]+', ' ', text) # remove non alphanumeric character
    text = re.sub(r'https?:/\/\S+', ' ', text) # remove links
    return text.strip()

# define the columns that we want to process and how to process
txt_field = data.Field(sequential=True,
                       tokenize=tokenizer,
                       include_lengths=True,
                       use_vocab=True)
label_field = data.Field(sequential=False,
                         use_vocab=False,
                         pad_token=None,
                         unk_token=None)

train_val_fields = [
    ('ItemID', None), # we dont need this, so no processing
    ('Sentiment', label_field), # process it as label
    ('SentimentSource', None), # we dont need this, so no processing
    ('SentimentText', txt_field) # process it as text
]

trainData = data.TabularDataset.splits(path='../../../data/nlp/',
                                            format='csv',
                                            train='Sentiment.csv',
                                            fields=train_val_fields,
                                            skip_header=True)

print(len(trainData))
#print(len(validationData))

from torchtext import vocab
# specify the path to the localy saved vectors
vec = vocab.Vectors('glove.6B.50d.txt', '../../../data/nlp/')
# build the vocabulary using train and validation dataset and assign the vectors
txt_field.build_vocab(trainData, max_size=100000, vectors=vec)
# build vocab for labels
label_field.build_vocab(trainData)

print(txt_field.vocab.vectors.shape)
# torch.Size([100002, 100])

txt_field.vocab.vectors[txt_field.vocab.stoi['the']]


"""
Project: Learn how to use PyTorch Lighnting on a simple toy problem
Author: Dr. Arrykrishna Mootoovaloo
Reference: PyTorch Lighnting website
Date: December 2022
"""
from ml_collections import ConfigDict
from pl_bolts.transforms.dataset_normalizations import cifar10_normalization
from pl_bolts.datamodules import CIFAR10DataModule
import torchvision

def data_module(cfg: ConfigDict):
    """The data module for the CIFAR 10 data

    Args:
        config (ConfigDict): the main configuration file.
    """
    train_transforms = torchvision.transforms.Compose(
        [
            torchvision.transforms.RandomCrop(
                cfg.trans.crop_size, cfg.trans.padding),
            torchvision.transforms.RandomHorizontalFlip(),
            torchvision.transforms.ToTensor(),
            cifar10_normalization()
        ]
    )

    test_transforms = torchvision.transforms.Compose(
        [
            torchvision.transforms.ToTensor(),
            cifar10_normalization()
        ]
    )

    module = CIFAR10DataModule(
        data_dir=cfg.paths.data,
        batch_size=cfg.training.batch_size,
        num_workers=cfg.training.num_workers,
        train_transforms=train_transforms,
        test_transforms=test_transforms,
        val_transforms=test_transforms
    )

    return module


import csv

# Reading CSV Files with csv
# Opens a csv file
with open('csv_file.csv') as f:

    # Reads a csv file with the Python's built-in reader() function
    # row by row.
    csv_reader = csv.reader(f, delimiter=',')

    # row variable returns a string list elements containing the
    # data found by removing the delimeters.
    for row in csv_reader:
        print(row)

# Reading CSV Files into Dictionary with csv
with open('csv_file.csv') as f:

    # Reads a csv file with as a dictionary where the keys are the
    # first line of the CSV file.
    csv_reader = csv.DictReader(f)

    # row variable returns a dict wher the keys are the first line or row
    for row in csv_reader:
        print(row)


from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse
from django.utils.html import escape

from products.models import Product, Orders, OrderDetails

# Create your views here.

# Admin's product functions
def product_change(request, pid):
    if request.user.is_superuser:
        url = "/admin/products/accept_change/" + pid
        product = Product.objects.get(product_id=pid)
        return render(request, 'product_change.html', {'product': product, 'url': url})
    return HttpResponse("You don't have permission to view this page")

def product_add(request):
    if request.user.is_superuser:
        url = "/admin/products/accept_add/"
        return render(request, 'product_add.html', {'url': url})
    return HttpResponse("You don't have permission to view this page")

def product_accept_change(request, pid):
    if request.user.is_superuser:
        product = Product.objects.get(product_id=pid)
        if request.method == 'POST':
            product_id = request.POST['product_id']
            product_name = request.POST['product_name']
            product_image = request.POST['product_image']
            company = request.POST['company']
            product_description = request.POST['product_description']
            quantity = request.POST['quantity']
            price = request.POST['price']

            # Update any fields that's different
            if product_id != product.product_id:
                product.product_id = product_id
                product.save(update_fields=["product_id"])

            if product_name != product.product_name:
                product.product_name = product_name
                product.save(update_fields=["product_name"])

            if product_image != product.product_image:
                product.product_image = product_image
                product.save(update_fields=["product_image"])

            if company != product.company:
                product.company = company
                product.save(update_fields=["company"])

            if product_description != product.product_description:
                product.product_description = product_description
                product.save(update_fields=["product_description"])

            if quantity != product.quantity_in_stock:
                product.quantity_in_stock = quantity
                product.save(update_fields=["quantity_in_stock"])

            if price != product.sell_price:
                product.sell_price = price
                product.save(update_fields=["sell_price"])

        return redirect('/products')
    return HttpResponse("You don't have permission to view this page")

def product_accept_add(request):
    if request.user.is_superuser:
        if request.method == 'POST':
            product_id = request.POST['product_id']
            product_name = request.POST['product_name']
            product_image = request.POST['product_image']
            company = request.POST['company']
            product_description = request.POST['product_description']
            quantity = request.POST['quantity']
            price = request.POST['price']

            product = Product(product_id=product_id, product_name=product_name, product_image=product_image, company=company, product_description=product_description, quantity_in_stock=quantity, sell_price=price)
            product.save()

            return redirect('/products')
        messages.add_message(request, messages.INFO, 'Error! Please refill the form to add a product')
        return redirect('/admin/products/add')
    return HttpResponse("You don't have permission to view this page")

def product_delete(request, pid):
    if request.user.is_superuser:
        Product.objects.get(product_id=pid).delete()
        return redirect('/products')
    return HttpResponse("You don't have permission to view this page")

# Admin's order functions
def order_status_change(request, oid):
    if request.user.is_superuser:
        order = Orders.objects.get(order_id=oid)

        if order.status == 'Pending':
            order.status = 'Confirmed'

        order.save(update_fields=["status"])
        return redirect('/products/orders')
    return HttpResponse("You don't have permission to view this page")

def order_filter(request):
    if request.user.is_superuser:
        order = Orders.objects.all()
    elif request.user.is_authenticated:
        order = Orders.objects.filter(user=request.user.username)

    if 'status' in request.GET:
        status = escape(request.GET["status"])
        if status == 'confirmed':
            order = order.filter(status="Confirmed")
        elif status == 'pending':
            order = order.filter(status="Pending")
        elif status == 'cancelled':
            order = order.filter(status="Canceled")

    return render(request, 'order.html', {'orders': order})

def order_details(request, oid):
    order = Orders.objects.get(order_id=oid)
    details = OrderDetails.objects.filter(order_id=oid).select_related('product_id')

    return render(request, 'order_details.html', {'id': oid, 'status': order.status, 'details': details, 'total': order.total_price})


from ruxit.api.base_plugin import RemoteBasePlugin
import logging
from kafka_offset_reader import KafkaOffsetReader

logger = logging.getLogger(__name__)

class KafkaOffsetPlugin(RemoteBasePlugin):

    def initialize(self, **kwargs):
        self.kafka = KafkaOffsetReader(self.config)

    def query(self, **kwargs):
      highwaters = self.kafka.get_highwater()
      offsets = self.kafka.get_offsets()
      id = "Kafka Cluster" + str(self.config["bootstrap_servers"])
      group = self.topology_builder.create_group(identifier=id, group_name=id)
      # topic > group > partition

      for (topic, partition_offsets) in highwaters.items():
        device = group.create_device(identifier=str(topic))

        highwaters = dict()
        for (partition, offset) in partition_offsets.items():
          highwaters[partition.partition] = offset
          device.absolute(key="topic.highwater", dimensions={"partition": str(partition)}, value=offset)

        if topic in offsets:
          for (consumer_group, group_offsets) in offsets[topic].items():
            for (partition, partition_offset) in group_offsets.items():
              lag = highwaters.get(partition, partition_offset - 1) - partition_offset
              logger.debug("Lag calculated for " + topic + " - " + str(partition) + ": " + str(lag))
              device.absolute(key="consumer.lag", dimensions={"partition": str(partition), "consumer_group": str(consumer_group)}, value=lag)

              device.absolute(key="consumer.offset", dimensions={"partition": str(partition), "consumer_group": str(consumer_group)}, value=partition_offset)


import sys
import threading
import time
from pynput import keyboard
from tkinter import *
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

class Display:
    def __init__(self, settings, enigma, pairs, reset, console):
        self.rT1, self.rN1 = settings[0], settings[3]
        self.rT2, self.rN2 = settings[1], settings[4]
        self.rT3, self.rN3 = settings[2], settings[5]
        self.refT = settings[6]
        self.enigma = enigma
        self.pairs = pairs
        self.reset = reset
        self.console = console
        self.line = ""
        self.exit_code = 0

    def editEnigma(self):
        pass

    def interactiveConsole(self, key):
        try:
            if key.char in ALPHABET:
                self.line += self.enigma.process(key.char)
                print(f" |{self.enigma.rotors[0].rotations+1:02}-{self.enigma.rotors[0].type[3]}|{self.enigma.rotors[1].rotations+1:02}-{self.enigma.rotors[1].type[3]}|{self.enigma.rotors[2].rotations+1:02}-{self.enigma.rotors[2].type[3]}|{self.enigma.reflector.typeStr}| enigma £> {key.char} | {self.line}      ", end='\r')
        except:
            #print(f"Not a valid key.")
            if (key == keyboard.Key.alt_l) or (key == keyboard.Key.alt_r):
                # editing mode
                self.exit_code = 2
                sys.exit(1)
            elif key == keyboard.Key.space:
                self.line += " "
                print(f" |{self.enigma.rotors[0].rotations+1:02}-{self.enigma.rotors[0].type[3]}|{self.enigma.rotors[1].rotations+1:02}-{self.enigma.rotors[1].type[3]}|{self.enigma.rotors[2].rotations+1:02}-{self.enigma.rotors[2].type[3]}|{self.enigma.reflector.typeStr}| enigma £> _ | {self.line}      ", end='\r')

            elif key == keyboard.Key.delete:
                self.exit_code = 3
                sys.exit(1)
            elif key == keyboard.Key.enter:
                self.enigma.reset()
                cipher = self.enigma.process(self.line)
                print(f"\n{cipher}      ")

                if self.reset: self.enigma.reset()
                self.line = ""
                print(f" |{self.enigma.rotors[0].rotations+1:02}-{self.enigma.rotors[0].type[3]}|{self.enigma.rotors[1].rotations+1:02}-{self.enigma.rotors[1].type[3]}|{self.enigma.rotors[2].rotations+1:02}-{self.enigma.rotors[2].type[3]}|{self.enigma.reflector.typeStr}| enigma £>                       ", end='\r')
            elif key == keyboard.Key.backspace:
                if (self.line != ""):
                    if (self.line[-1] != "_") or (self.line[-1] != " "):
                        turn_first = self.enigma.rotors[0].rotate(force=True, offset=-1)
                        if turn_first: turn_next = True
                        else: turn_next = False
                        for rotor in self.enigma.rotors[1:]:
                            turn_next = rotor.rotate(force=(turn_next and turn_first), offset=-1)
                    self.line = self.line[:-1]

                print(f" |{self.enigma.rotors[0].rotations+1:02}-{self.enigma.rotors[0].type[3]}|{self.enigma.rotors[1].rotations+1:02}-{self.enigma.rotors[1].type[3]}|{self.enigma.rotors[2].rotations+1:02}-{self.enigma.rotors[2].type[3]}|{self.enigma.reflector.typeStr}| enigma £> - | {self.line}      ", end='\r')
            elif key == keyboard.Key.esc:
                self.exit_code = 0
                sys.exit(1)
            else:
                pass

    def userInterface(self, key):
        if key == keyboard.Key.esc:
            sys.exit(1)

    def start_thread(self):
        self.root = Tk()
        self.root.geometry("800x1000")

        editing = Tk.frame(self.root)

        #Create a canvas object
        c= Canvas(self.root,width=400, height=400)
        c.pack()
        c.create_oval(60,60,210,210)
        self.root.mainloop()

    def on_press(self, key):
        if self.console:
            self.interactiveConsole(key)
        else:
            self.userInterface(key)

    def headless(self):
        # aka interactive console
        print("Starting interactive console.    ", end='\r')
        time.sleep(.5)
        print("Starting interactive console..   ", end='\r')
        time.sleep(.5)
        print("Starting interactive console...  ", end='\r')
        time.sleep(.5)
        print("                                                                                ", end='\r')
        print(f" |{self.enigma.rotors[0].rotations+1:02}-{self.enigma.rotors[0].type[3]}|{self.enigma.rotors[1].rotations+1:02}-{self.enigma.rotors[1].type[3]}|{self.enigma.rotors[2].rotations+1:02}-{self.enigma.rotors[2].type[3]}|{self.enigma.reflector.typeStr}| enigma £>                 ", end='\r')
        listener = keyboard.Listener(on_press=self.on_press, suppress=True)
        listener.start()
        listener.join()
        if self.exit_code == 0 or self.exit_code == 1:
            string = 'Returning to non-interactive console'
        elif self.exit_code == 2:
            string = 'Entering Edit Machine Mode'
        elif self.exit_code == 3:
            string = 'Exiting Application'

        print(f"{string}                        ", end='\r')
        time.sleep(.5)
        print(f"{string}.                       ", end='\r')
        time.sleep(.5)
        print(f"{string}..                      ", end='\r')
        time.sleep(.5)
        print(f"{string}...                     ", end='\r')
        time.sleep(.5)
        if (self.exit_code == 0) or (self.exit_code == 1): print("                                                                  ", end='\r')
        if (self.exit_code == 0) or (self.exit_code == 1): print("General Console Controls: Type input and press enter to see output, type quit() to quit, reset() to reset machine, edit() to edit machine, and inter() to start the interactive mode.")

        return self.exit_code

    def run(self):
        thread = threading.Thread(target=self.start_thread)
        thread.start()
        listener = keyboard.Listener(on_press=self.on_press, suppress=True)
        listener.start()
        listener.join()
        thread.join()


from django.db import models

from django.db import models
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    phone = models.CharField(max_length=13)

    def __str__(self):
        return self.username

DIFF_CHOICES =(
    ('pc', 'pc'),
    ('gpc','gpc'),
    ('vr','vr'),
    ('xbox','xbox'),
    ('ps','ps')
    )
class Book(models.Model):
    game=models.CharField(max_length=5, choices=DIFF_CHOICES, default='ps')
    date=models.DateField()
    time=models.TimeField()
    number=models.IntegerField(default=0)

    def __str__(self) -> str:
        return self.game


# Helper Functions
import pytest
from tests.common.helpers.assertions import pytest_assert

pytestmark = [
    pytest.mark.topology('any')
]

def get_status_redisout(status_out):
    """Extract status value for feature in redis
    """
    status_list = status_out[1:]
    status = ""
    for s in status_list:
        status = s.encode('UTF-8')
        return status

# Test Functions
def test_show_features(duthost):
    """Verify show features command output against CONFIG_DB
    """
    features_dict, succeeded = duthost.get_feature_status()
    pytest_assert(succeeded, "failed to obtain feature status")
    for cmd_key, cmd_value in features_dict.items():
        feature = str(cmd_key)
        status_out = duthost.shell('/usr/bin/redis-cli -n 4 hgetall "FEATURE|{}"'.format(feature), module_ignore_errors=False)['stdout_lines']
        redis_value = get_status_redisout(status_out)
        status_value_expected = str(cmd_value)
        assert str(redis_value) == status_value_expected, "'{}' is '{}' which does not match with config_db".format(cmd_key, cmd_value)


import sys
from PyQt5 import QtWidgets
import design
import random

class MatrixApp(QtWidgets.QMainWindow, design.Ui_Matrix):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.randomiseMatrix.clicked.connect(self.newMatrix)
        self.pushButton_2.clicked.connect(self.showMatrix)
        self.showMinimum.clicked.connect(self.printMin)
        self.sumRow.clicked.connect(self.printSumC)
        self.M = 0
        self.N = 0
        self.matr = [[1 for y in range(self.M)] for x in range(self.N)]
    def newMatrix(self):
        self.listWidget.clear()
        self.matr = [[random.randrange(0, 10) for y in range(self.M)] for x in range(self.N)]
        for i in self.matr:
            self.listWidget.addItem(str(i))
    def showMatrix(self):
        self.listWidget.clear()
        self.M = self.rows.value()
        self.N = self.columns.value()
        self.matr = [[1 for y in range(self.M)] for x in range(self.N)]
        for i in self.matr:
            self.listWidget.addItem(str(i))
    def printMin(self):
        i = self.columnNumber.value()
        min = 10
        for j in self.matr:
            if j[i] < min:
                min = j[i]
        self.listWidget.addItem("minimum of {} colomn - ".format(i) + str(min))
    def printSumC(self):
        i = self.rowNumber.value()
        s = sum(self.matr[i])
        self.listWidget.addItem("sum of {} row - ".format(i) + str(s))
    def printSumAll(self):
        s = 0
        for i in self.matr:
            s += sum(i)
        self.listWidget.addItem("sum of all elements - ".format(i) + str(s))
def main():
    app = QtWidgets.QApplication(sys.argv)
    window = MatrixApp()
    window.show()
    app.exec()

if __name__ == '__main__':
    main()