# Launcher!
# Separated from main to individualize modules by having main just run game loop

from optparse import OptionParser
import sys

from generators import generator_dict

from file_io import read_startup_file

import pygame

from vars_constant import screen_width, screen_height
import vars_global  # for -p / --pos

from map import Map

from player import Player

from controller import Controller

from main import main

def launch():
    # Possible flags to throw when running module from command line. python launcher.py <flags>
    parser = OptionParser()

    parser.add_option("-r", "--run", type=str, dest="generator",
                      help="Enter a the name of the generator you would like to run. Use -l to see list of generators.")

    parser.add_option("-s", "--seed", type=str, dest="seed",
                      help="Enter seed to test generator with. Extra digits ignored, more digits added if needed.")

    parser.add_option("-p", "--pos", type=int, nargs=2, dest="pos",
                      help="Enter starting cell as two ints separated by spaces.")

    def print_generators(options=None, opt=None, value=None, parser=None):
        [print("{}: {}".format(name, generator_object.description)) for name, generator_object in generator_dict.items()]
        pygame.quit()
        sys.exit()

    parser.add_option("-l", "--list",
                      action="callback", callback=print_generators,
                      help="Print list of available generators.")

    (options, args) = parser.parse_args(sys.argv)

    # Seeing if value set in flag, if not set to hardcoded default
    try:
        generator = options.generator

        if not options.generator:
            raise AttributeError

        if str(options.generator) not in [key for key in generator_dict]:
            print("Invalid generator - {}!".format(options.generator))
            pygame.quit()
            sys.exit()

    except AttributeError:
        generator = str([key for key in generator_dict][0])

    try:
        if not options.seed:
            raise AttributeError

        seed = options.seed
    except AttributeError:
        seed = None

    try:
        if not options.pos:
            raise AttributeError

        vars_global.spectator_x = options.pos[0] * Map.Cell.size
        if __name__ == '__main__':
            vars_global.spectator_y = options.pos[1] * Map.Cell.size
    except AttributeError:
        pass

    print("Received values, Generator Choice: \"{}\", Seed: \"{}\"".format(generator, seed))
    print("Initializing engine.")

    startup_dict = read_startup_file()

    pygame.init()

    display = pygame.display.set_mode((screen_width, screen_height))

    pygame.display.set_caption("2d Cell Engine by Cole")

    cell_map = Map(display, generator, seed)

    player = Player(cell_map)

    print(main(display, pygame.time.Clock(), cell_map, player, Controller(startup_dict['Control Slot'], player)))

if __name__ == "__main__":
    launch()


# Author: Christian Brodbeck <christianbrodbeck@nyu.edu>

from nose.tools import eq_, ok_, assert_is_instance, assert_raises
from eelbrain._utils.testing import assert_dataobj_equal
from numpy.testing import assert_array_equal

from eelbrain import Factor, NDVar, Var, datasets, table, combine

def test_difference():
    "Test table.difference"
    ds = datasets.get_uv()
    print(table.difference('fltvar', 'A', 'a1', 'a2', 'rm', ds=ds))
    print(table.difference('fltvar', 'A', 'a1', 'a2', 'rm', by='B', ds=ds))
    print(table.difference('fltvar', 'A%B', ('a1', 'b1'), ('a2', 'b2'), 'rm',
                           ds=ds))

    # create bigger dataset
    ds['C', :] = 'c1'
    ds2 = datasets.get_uv()
    ds2['C', :] = 'c2'
    ds = combine((ds, ds2))
    print(table.difference('fltvar', 'A', 'a1', 'a2', 'rm', 'B%C', ds=ds))
    print(table.difference('fltvar', 'A%B', ('a1', 'b1'), ('a2', 'b2'), 'rm',
                           'C', ds=ds))

def test_frequencies():
    "Test table.frequencies"
    ds = datasets.get_uts()
    freq = table.frequencies('YCat', 'A', ds=ds)
    assert_array_equal(freq['A'], ['a0', 'a1'])
    ok_(all(c in freq for c in ds['YCat'].cells))
    print(freq)
    freq = table.frequencies('YCat', 'A % B', ds=ds)
    assert_array_equal(freq['A'], ['a0', 'a0', 'a1', 'a1'])
    assert_array_equal(freq['B'], ['b0', 'b1', 'b0', 'b1'])
    print(freq)
    freq = table.frequencies('YCat % A', 'B', ds=ds)
    print(freq)

def test_melt_ndvar():
    "Test table.melt_ndvar()"
    ds = datasets.get_uts(True)
    ds = ds.sub("A == 'a1'")

    lds = table.melt_ndvar('uts', ds=ds)
    ok_('time' in lds)
    assert_is_instance(lds['time'], Var)
    eq_(set(lds['time'].x), set(ds['uts'].time.x))

    # no ds
    lds2 = table.melt_ndvar(ds['uts'])
    assert_dataobj_equal(lds2['uts'], lds['uts'])

    # sensor
    lds = table.melt_ndvar("utsnd.summary(time=(0.1, 0.2))", ds=ds, varname='summary')
    eq_(set(lds['sensor'].cells), set(ds['utsnd'].sensor.names))

    # NDVar out
    lds = table.melt_ndvar("utsnd", 'sensor', ds=ds)
    ok_('utsnd' in lds)
    assert_is_instance(lds['utsnd'], NDVar)
    assert_dataobj_equal(lds[:ds.n_cases, 'utsnd'], ds.eval("utsnd.sub(sensor='0')"))

    # more than one dimensions
    assert_raises(ValueError, table.melt_ndvar, 'utsnd', ds=ds)

def test_repmeas():
    "Test table.repmeas (repeated measures table)"
    ds = datasets.get_uv()
    print(table.repmeas('fltvar', 'A', 'rm', ds=ds))
    print(table.repmeas('fltvar', 'A%B', 'rm', ds=ds))
    print(table.repmeas('fltvar', 'A', 'B%rm', ds=ds))

    # with int model
    ds['Bv'] = ds['B'].as_var({'b1': 1, 'b2': 2})
    print(table.repmeas('fltvar', 'A', 'Bv%rm', ds=ds))

    # test naturalization of cellnames
    ds['ANum'] = Factor(ds['A'], labels={'a1': '1', 'a2': '2'})
    print(table.repmeas('fltvar', 'ANum', 'rm', ds=ds))

    # with empty cell name
    ds['A'].update_labels({'a1': ''})
    print(table.repmeas('fltvar', 'A', 'rm', ds=ds))


class PaymentMethod(dict):
    pass

class CardInfo(PaymentMethod):
    allowed_methods = [
        "sale", "auth", "offline", "balance_inquire", "invoice.pay"
    ]

    def __init__(self, number, expiration, cvv=None):
        """
        A PaymentMethod representing a credit or debit card.
        """
        self.update({
            "cardnum": number,
            "cardexp": expiration,
            "cvv": cvv,
        })

class FiservCard(PaymentMethod):
    allowed_methods = ["sale"]

    def __init__(self, number, expiration, cvv=None):
        """
        A PaymentMethod representing a credit or debit card
        used through Fiserv.
        """
        self.update({
            "cardnum": number,
            "cardexp": expiration,
            "cvv": cvv,
        })

class MilitaryStarCard(PaymentMethod):
    allowed_methods = ["sale", "auth", "invoice.pay"]

    def __init__(self, number, expiration, cid):
        """
        A PaymentMethod representing a Military Star card.
        """
        self.update({
            "cardnum": number,
            "cardexp": expiration,
            "CID": cid,
        })

class ACHInfo(PaymentMethod):
    allowed_methods = ["sale", "credit", "invoice.pay"]

    def __init__(self, bank_name, routing, account, type, subtype,
                 first_name=None, last_name=None, company=None):
        """
        A PaymentMethod representing an ACH account.
        """
        self.update({
            "bankname": bank_name,
            "routing": routing,
            "account": account,
            "achaccounttype": type,
            "achaccountsubtype": subtype,
        })

        if first_name is not None:
            self["firstname"] = first_name
        if last_name is not None:
            self["lastname"] = last_name
        if company is not None:
            self["company"] = company

class ECheck(PaymentMethod):
    allowed_methods = ["sale", "credit", "invoice.pay"]

    def __init__(self, bank_name, routing, account, type, subtype,
                 first_name=None, last_name=None, company=None,
                 address1=None, city=None, state=None, zip=None, country=None):
        """
        A PaymentMethod representing an ECheck account.
        """
        self.update({
            "bankname": bank_name,
            "routing": routing,
            "account": account,
            "achaccounttype": type,
            "achaccountsubtype": subtype,
        })

        if first_name is not None:
            self["firstname"] = first_name
        if last_name is not None:
            self["lastname"] = last_name
        if company is not None:
            self["company"] = company
        if address1 is not None:
            self["address1"] = address1
        if city is not None:
            self["city"] = city
        if state is not None:
            self["state"] = state
        if zip is not None:
            self["zip"] = zip
        if country is not None:
            self["country"] = country

class EWallet(PaymentMethod):
    allowed_methods = ["credit"]

    def __init__(self, account, type="PayPal"):
        """
        A PaymentMethod representing an eWallet (i. e. PayPal) account.
        """
        self.update({
            "ewalletaccount": account,
            "ewallet_type": type,
        })

class Contact(dict):
    def __init__(self, first_name=None, last_name=None, company=None,
                 address1=None, address2=None, city=None, state=None,
                 zip=None, country=None, phone=None, fax=None, email=None):
        self.update({
            "firstname": first_name,
            "lastname": last_name,
            "company": company,
            "address1": address1,
            "address2": address2,
            "city": city,
            "state": state,
            "zip": zip,
            "country": country,
            "phone": phone,
            "fax": fax,
            "email": email,
        })

class Product(dict):
    def __init__(self, skunumber=None, description=None,
                 amount=None, quantity=None):
        self.update({
            "skunumber": skunumber,
            "description": description,
            "amount": amount,
            "quantity": quantity,
        })

class Option(dict):
    def __init__(self, type=None, value=None, percentage=None):
        self.update({
            "type": type,
            "value": value,
            "percentage": percentage,
        })

class SaleInfo(dict):
    def __init__(self, amount, currency=None,
                 ip_addr=None, order_id=None, order_desc=None,
                 tax=None, shipping_cost=None, po_number=None,
                 billing_contact=None, ship_to=None,
                 products=[], options=[], **kwargs):
        self.update({
            "amount": amount,
            "currency": currency,
            "cardipaddress": ip_addr,
            "orderid": order_id,
            "orderdesc": order_desc,
            "tax": tax,
            "shipamount": shipping_cost,
            "ponumber": po_number,
        })

        if billing_contact is not None:
            self.update(billing_contact)

        if ship_to is not None:
            self.update({
                "ship" + key: value
                for key, value in ship_to.items()
            })
            # XXX: the only parameter not present on shipping address is
            # "fax", let's ignore it for now
            self.pop("shipfax", None)

        for i, product in enumerate(products):
            self.update({
                "skunumber_%i" % i: product.get("skunumber"),
                "description_%i" % i: product.get("description"),
                "amount_%i" % i: product.get("amount"),
                "quantity_%i" % i: product.get("quantity"),
            })

        for i, option in enumerate(options):
            self.update({
                "opt_amount_type_%i" % i: option.get("type"),
                "opt_amount_value_%i" % i: option.get("value"),
                "opt_amount_percentage_%i" % i: option.get("percentage"),
            })

        self.update(kwargs)


from django.db import models
import uuid

# Create your models here.

class CommonAttributes(models.Model):
    created_on = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class Product(CommonAttributes):

    BRAND_CHOICES = [
        ('ZOD', 'Zodiac'),
        ('PAN', 'Park Avenue'),
        ('NKE', 'Nike'),
        ('ADS', 'Adidas'),
        ('SPR', 'Spykar'),
        ('GSR', 'Gstar'),

    ]

    PRODUCT_CATEGORY_CHOICES = [
        ('SE', 'Shoes'),
        ('ST', 'Shirt'),
        ('PT', 'Pant'),

    ]

    image = models.ImageField(upload_to='dynamic_images')
    label = models.CharField(max_length=2, choices=PRODUCT_CATEGORY_CHOICES)
    price = models.FloatField()
    brand = models.CharField(max_length=3, choices=BRAND_CHOICES)
    description = models.TextField()
    remaining_stock = models.PositiveIntegerField(default=1)

    class Meta:
        verbose_name = 'Products'
        verbose_name_plural = 'Product'
        db_table = 'product'
        ordering = ['-created_on']

    def __str__(self):
        return self.label

class Sku(CommonAttributes):
    COLOR_CHOICES = [
        ('RD', 'Red'),
        ('WE', 'White'),
        ('GY', 'Gray'),
        ('YW', 'Yellow'),
        ('BK', 'Black'),

    ]

    SIZE_CHOICES = [
        ('S', 'Small'),
        ('M', 'Medium'),
        ('L', 'Large'),
        ('XL', 'Extra_Large'),

    ]

    GENDER_CHOICES = [
        ('M', 'Mens'),
        ('F', 'Womens'),
    ]

    id  = models.UUIDField(primary_key=True, default = uuid.uuid4, editable = False)
    sku_code = models.CharField(max_length=250,null=True,blank=True)
    color = models.CharField(max_length=2, choices=COLOR_CHOICES)
    size = models.CharField(max_length=2, choices=SIZE_CHOICES)
    gender_category = models.CharField(max_length=1, choices=GENDER_CHOICES)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='product')

    class Meta:
        verbose_name = 'Skus'
        verbose_name_plural = 'Sku'
        db_table = 'sku'
        ordering = ['-id']

    def save(self, *args, **kwargs):
            self.sku_code = self.gender_category + '-' + self.product.label + '-' + self.product.brand + '-' + self.color + '-' + self.size + '-' + str(self.id)
            super(Sku, self).save(*args, **kwargs)

    def __str__(self):
        return self.sku_code

class Batch(CommonAttributes):
    sku = models.ForeignKey(Sku, on_delete=models.CASCADE, related_name='sku')

    class Meta:
        verbose_name = 'Batchs'
        verbose_name_plural = 'Batch'
        db_table = 'batch'

    def __str__(self):
        return str(self.id)


from django.urls import path, re_path

from . import views

urlpatterns = [
	path('', views.HomeView.as_view(), name='home'),
	path('create/', views.CreateProject.as_view(), name='create'),
	path('delete/<int:project_id>/', views.delete_project, name='delete'),
	path('<int:project_id>', views.ProjectView.as_view(), name='view'),
	path('search/<project_name>', views.SearchProject.as_view(), name='search'),
	#path('update/<int:project_id>/'),
]


import platform
from typing import Optional

from docker.errors import ImageNotFound, NotFound
import os
import webbrowser
import time
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

import uuid
import click

from gigantumcli.dockerinterface import DockerInterface
from gigantumcli.commands.install import install
from gigantumcli.config import ConfigOverrideFile
from gigantumcli.utilities import ask_question, is_running_as_admin, get_nvidia_gpu_info, get_nvidia_smi_path

# We can disable this because requests is just being used to verify API connectivity
# and in a context where the client is running with HTTPS, the lookup still happens on
# localhost so ssl verification will fail anyway.
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def _check_for_api(port: int = 10000, launch_browser: bool = False, timeout: int = 5):
    """Check for the API to be live for up to `timeout` seconds, then optionally launch a browser window

    Args:
        port(int): port the server is running on
        launch_browser(bool): flag indicating if the browser should be launched on success == True
        timeout(int): Number of seconds to wait for the API before returning
    Returns:
        bool: flag indicating if the API is ready
    """
    protocol = "http"
    if port == 443:
        protocol = "https"

    success = False
    for _ in range(timeout):
        time.sleep(1)
        try:
            resp = requests.get("{}://localhost:{}/api/ping?v={}".format(protocol, port, uuid.uuid4().hex),
                                verify=False)

            if resp.status_code == 200:
                success = True
                break
        except requests.exceptions.ConnectionError:
            # allow connection errors, which mean the API isn't up yet.
            pass

    if success is True and launch_browser is True:
        time.sleep(1)
        # If here, things look OK. Start browser
        webbrowser.open_new("{}://localhost:{}".format(protocol, port))

    return success

@click.command()
@click.option('--edge', '-e', type=bool, is_flag=True, default=False,
              help="Flag indicating if the edge version should be used. Note, you must have access "
                   "to this image and may need to log into DockerHub.")
@click.option('--wait', '-w', 'timeout', type=int, default=90,
              help="Number of seconds to wait for the Client to be ready.")
@click.option('--yes', '-y', 'accept_confirmation', is_flag=True, default=False,
              help="Flag to automatically accept all confirmation prompts")
@click.option('--tag', '-t', type=str, default=None,
              help="Provide a tag to explicitly run instead of the latest available.")
@click.option('--port', type=int, default=10000,
              help="Set the port on which the Client web server is exposed (10000).")
@click.option('--external', is_flag=True, default=False,
              help="Set if you wish to run the Client for external access. Default runs on localhost.")
@click.option('--working-dir', '-d', 'working_dir', type=str, default="~/gigantum",
              help="Set the Client working directory (`~/gigantum`).")
@click.pass_context
def start(ctx, edge: bool, timeout: int, tag: Optional[str] = None, port: int = 10000, working_dir: str = "~/gigantum",
          external: bool = False, accept_confirmation: bool = False) -> None:
    """Start Gigantum Client.

    This command will clean-up any lingering Gigantum managed containers and then start the Client on the specified
    port (default 10000).

    Note, only running on localhost:10000 is supported with Gigantum.com. When using a self-hosted server, you can
    use any additional hostnames and ports that have been added to the auth redirect allowlist by the system
    administrator.
    \f

    Args:
        edge(bool): Flag indicating if the install is stable or edge
        timeout: Number of seconds to wait for API to come up
        tag: Tag to run, defaults to latest
        port: port the server runs on
        working_dir: Location to mount as the Gigantum working directory
        external: Optional flag if you want to run on the external network interface (0.0.0.0)
        accept_confirmation: Optional Flag indicating if you should skip the confirmation and auto-accept
    """
    # Make sure user is not root
    if is_running_as_admin():
        raise click.UsageError("Do not run `gigantum start` as root.")

    # Make sure you are in browser mode if running on 80/443
    if external:
        config_file = ConfigOverrideFile(working_dir=working_dir)
        if not config_file.is_browser_middleware_selected():
            click.secho("WARNING: When running the Client with external network access you MUST enable the "
                        "`multi-user` auth mode.", fg='yellow')
            click.secho("Run `gigantum config set-auth-mode multi-user` to configure this.", fg='yellow')
            click.secho("Failure to do so could allow anyone to access a valid user session.", fg='yellow', bold=True)
            raise click.Abort()

    click.echo("Verifying Docker is available...")
    # Check if Docker is running
    docker_obj = DockerInterface()
    image_name = docker_obj.image_name(edge)

    if not tag:
        # Trying to update to the latest version
        tag = 'latest'
    image_name_with_tag = "{}:{}".format(image_name, tag)

    # Check if working dir exists
    working_dir = os.path.expanduser(working_dir)
    if not os.path.exists(working_dir):
        os.makedirs(working_dir)

    # Check if application has been installed
    try:
        docker_obj.client.images.get(image_name_with_tag)
    except ImageNotFound:
        if ask_question("The Gigantum Client Docker image not found. Would you like to install it now?",
                        accept_confirmation):
            ctx.invoke(install, edge=edge)
        else:
            click.echo("Downloading the Gigantum Client Docker image is required to start the Client. "
                       "Please run `gigantum install`.")
            return

    # Check to see if already running
    try:
        if _check_for_api(port=port, launch_browser=False, timeout=1):
            click.echo("Client already running on http://localhost:{}".format(port))
            _check_for_api(port=port, launch_browser=True)
            click.echo("If page does not load, restart by running `gigantum stop` and then `gigantum start` again")
            return

        # remove any lingering gigantum managed containers
        docker_obj.cleanup_containers()

    except NotFound:
        # If here, the API isn't running and an older container isn't lingering, so just move along.
        pass

    # Start
    if external:
        interface = "0.0.0.0"
    else:
        interface = "127.0.0.1"

    port_mapping = {'10000/tcp': (interface, port)}
    if port == 443:
        # Running on https, set up http->https redirect
        port_mapping['10080/tcp'] = (interface, 80)

    # Make sure the container-container share volume exists
    if not docker_obj.share_volume_exists():
        docker_obj.create_share_volume()

    volume_mapping = {docker_obj.share_vol_name: {'bind': '/mnt/share', 'mode': 'rw'}}

    print('Host directory for Gigantum files: {}'.format(working_dir))
    if platform.system() == 'Windows':
        # windows docker has some eccentricities
        # no user ids, we specify a WINDOWS_HOST env var, and need to rewrite the paths for
        # bind-mounting inside the Client (see `dockerize_mount_path()` for details)
        rewritten_path = docker_obj.dockerize_mount_path(working_dir, image_name_with_tag)
        environment_mapping = {'HOST_WORK_DIR': rewritten_path,
                               'WINDOWS_HOST': 1}
        volume_mapping[working_dir] = {'bind': '/mnt/gigantum', 'mode': 'rw'}

    elif platform.system() == 'Darwin':
        # For macOS, use the cached mode for improved performance
        environment_mapping = {'HOST_WORK_DIR': working_dir,
                               'LOCAL_USER_ID':  os.getuid()}
        volume_mapping[working_dir] = {'bind': '/mnt/gigantum', 'mode': 'cached'}
    else:
        # For anything else, just use default mode.
        driver_version, num_gpus = get_nvidia_gpu_info()
        environment_mapping = {'HOST_WORK_DIR': working_dir,
                               'LOCAL_USER_ID':  os.getuid(),
                               'NVIDIA_DRIVER_VERSION': driver_version}

        if driver_version:
            print(f"Detected {num_gpus} GPU(s) available for use.")
            environment_mapping['NVIDIA_NUM_GPUS'] = num_gpus

            # Mount nvidia-smi as read-only if present
            smi_path = get_nvidia_smi_path()
            if smi_path:
                environment_mapping['NVIDIA_SMI_PATH'] = smi_path

        volume_mapping[working_dir] = {'bind': '/mnt/gigantum', 'mode': 'rw'}

    volume_mapping['/var/run/docker.sock'] = {'bind': '/var/run/docker.sock', 'mode': 'rw'}

    container = docker_obj.client.containers.run(image=image_name_with_tag,
                                                 detach=True,
                                                 name=image_name.replace("/", "."),
                                                 init=True,
                                                 ports=port_mapping,
                                                 volumes=volume_mapping,
                                                 environment=environment_mapping)
    print("Starting, please wait...")
    time.sleep(1)

    # Make sure volumes have mounted properly, by checking for the log file for up to timeout seconds
    success = False
    for _ in range(timeout):
        if os.path.exists(os.path.join(working_dir, '.labmanager', 'logs', 'labmanager.log')):
            success = True
            break

        # Sleep for 1 sec and increment counter
        time.sleep(1)

    if not success:
        msg = "\n\nWorking directory failed to mount! Have you granted Docker access to your user directory?"
        msg = msg + " \nIn both Docker for Mac and Docker for Windows this should be shared by default, but may require"
        msg = msg + " a confirmation from the user."
        msg = msg + "\n\nRun `gigantum stop`, verify your OS and Docker versions are supported, the allowed Docker"
        msg = msg + " volume share locations include `{}`, and try again.".format(working_dir)
        msg = msg + "\n\nIf this problem persists, contact support."

        # Stop and remove the container
        container.stop()
        container.remove()

        click.echo(msg, err=True)
        raise click.Abort()

    # Wait for API to be live before opening the user's browser
    if not _check_for_api(port=port, launch_browser=True, timeout=timeout):
        msg = "\n\nTimed out waiting for Gigantum Client web API! Try restarting Docker and then start again." + \
                "\nOr, increase timeout with --wait option (default is 90 seconds)."

        # Stop and remove the container
        container.stop()
        container.remove()

        click.echo(msg, err=True)
        raise click.Abort()

    if external:
        click.echo("Client running on http://0.0.0.0:{}".format(port))
    else:
        click.echo("Client running on http://localhost:{}".format(port))


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

"""
This script tests serial ports.
"""

import serial
import sys
import time

class SerialPort:
    def __init__(self, device="/dev/ttyS0", baud=9600,
                 par=serial.PARITY_NONE, sbits=serial.STOPBITS_ONE,
                 bsize=serial.EIGHTBITS):
        self.serport = device
        self.baudrate = baud
        self.parity = par
        self.stopbits = sbits
        self.bytesize = bsize

    def port_open(self):
        self.port = serial.Serial(port=self.serport, baudrate=self.baudrate,
                                  parity=self.parity, stopbits=self.stopbits,
                                  bytesize=self.bytesize, timeout=1)

    def port_write(self, message):
        print('converting... ', message)
        print('\033[35m', 'sending...\n--> ', bytes(message), '\033[00m')
        self.port.write(bytes(message))
        return bytes(message)

    def port_listen(self):
        # [orc(c) for c in received]
        return self.port.readline()

    def port_read_bytes(self, bytes_to_read):
        bytes_read = b''
        while self.port.inWaiting() > 0:
            bytes_read += self.port.read(bytes_to_read)
        return bytes_read

    def port_close(self):
        try:
            self.port.close()
        except:
            pass

    def __str__(self):
        print(self.serport)
        print(self.baudrate)
        print(self.parity)
        print(self.stopbits)
        print(self.bytesize)

def configure_port(serial):
    serial.port_close()
    print('Set serial port params: ')
    params = ('device path [/dev/tty???]\n>',
              'baudrate [2400, 9600, 19200, 38400, 57600, 115200]\n>',
              'parity [PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE]\n>',
              'stopbits [STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO]\n>',
              'bytesize [FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS]\n>')
    check_params = ('2400', '9600', '19200', '38400', '57600', '115200',
                    'PARITY_NONE', 'PARITY_EVEN', 'PARITY_ODD', 'PARITY_MARK',
                    'PARITY_SPACE', 'STOPBITS_ONE', 'STOPBITS_ONE_POINT_FIVE',
                    'STOPBITS_TWO', 'FIVEBITS', 'SIXBITS', 'SEVENBITS',
                    'EIGHTBITS', '/dev/ttyS0', '/dev/ttyS1', '/dev/ttyS2',
                    '/dev/ttyS3', '/dev/ttyAMA0')
    new_params = []
    index = 0
    while True:
        temp_param = input(params[index])
        if temp_param in check_params:
            index += 1
            new_params.append(temp_param)
        elif temp_param == 'skip':
            return new_params
        else:
            print('Uups, a typo!')
            continue
    print('saved.')
    return new_params

def save_to_log(mode, ctime, data):
    with open('serial-test.log', 'a') as f:
        f.write('\t'.join([mode, ctime, str(data)]))
        f.write('\n')

def start_communication(serial):
    def listening_mode():
        start = 0
        while True:
            time.sleep(1)
            received = serial.port_listen()
            if not received:
                start += 1
                if start > 30:
                    print('No input received for > 30 sec, switching to writing mode...')
                    return 0
            else:
                start = 0
                save_to_log('RECEIVED', time.ctime(), received)
                print('\033[36m', '<-- ', received, '\033[00m')

    serial.port_open()
    yn = input('Listening mode (y/n)? ')
    if yn == 'y':
        print('listening... (if no input is received for 30 sec, stop)')
        listening_mode()

    while True:
        push = input('Push the floor button [0, 0.5, 1, 1.5, 2, 3] or send a HEARBEAT [-1]\n>')
        if push == 'exit':
            serial.port_close()
            sys.exit()
        elif push == 'conf' or push == 'reconf':
            serial.port_close()
            serial = SerialPort(configure_port(serial))
            serial.port_open()
            continue
        elif push == 'listen':
            print('Switching back to listening mode...')
            listening_mode()
        if push == '0':
            sent = serial.port_write(FLOOR0)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '0.5':
            sent = serial.port_write(FLOOR0_5)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '1':
            sent = serial.port_write(FLOOR_1)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '1.5':
            sent = serial.port_write(FLOOR1_5)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '2':
            sent = serial.port_write(FLOOR_2)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '3':
            sent = serial.port_write(FLOOR_3)
            save_to_log('SENT', time.ctime(), sent)
        elif push == '-1':
            sent = serial.port_write(HEART)
            save_to_log('SENT', time.ctime(), sent)
        else:
            print('Incorect input!')
            continue
        time.sleep(1)

def main():
    serial = SerialPort()
    yn = input('Configure serial port (y/n)? ')
    if yn == 'y':
        serial = SerialPort(configure_port(serial))
    start_communication(serial)

# floor consts
FLOOR0 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x40, 0x0, 0x19, 0x9,  0x7e )
FLOOR0_5 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x41, 0x1, 0x48, 0x1,  0x7e )
FLOOR_1 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x42, 0x0, 0xa9, 0x3a, 0x7e )
FLOOR1_5 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x43, 0x1, 0xf8, 0x32, 0x7e )
FLOOR_2 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x44, 0x0, 0x79, 0x6e, 0x7e )
FLOOR_3 = (0x7e, 0x0, 0x3, 0x42, 0x1, 0xff, 0xff, 0x0, 0x20, 0x45, 0x0, 0xa1, 0x77, 0x7e )
HEART = (0x7E, 0x00, 0x03, 0xe0, 0x01, 0xFF, 0xFF, 0x00, 0x20, 0xc3, 0x1e, 0x64, 0x02, 0x08, 0x3a, 0x7E )

if __name__ == "__main__":
    main()


import os

from data import common
from data import srdata

import numpy as np
import scipy.misc as misc

import torch
import torch.utils.data as data

class DIV2K(srdata.SRData):
    """继承自 srdata 的类

    """
    def __init__(self, args, train=True):
        super(DIV2K, self).__init__(args, train)
        # test_every: do test per every N batches
        # n_train: number of training set
        self.repeat = args.test_every // (args.n_train // args.batch_size) # 1000 / (800 // 32) = 1000 // 31iter = 32

    def _scan(self):
        """
        Return:
            list_hr: a list of hr image path
            list_lr: a list of lr image path [[scale1_pic1, scale1_pic2, scale1_pic3,...], [scale2_pic1, scale2_pic2, scale2_pic3,...], ...]
        """

        list_hr = []
        list_lr = [[] for _ in self.scale]
        if self.train:
            """训练集0001-0800
            DIV2K/DIV2K_train_HR/ -- 0001.png, 0002.png, ..., 0800.png train HR images
            (provided to the participants)
            """
            idx_begin = 0
            idx_end = self.args.n_train
        else:
            """在DIV2K上测试的话，测试集801-900
            DIV2K提供了测试集，测试集的数量801-》
            [DIV2K_website](https://data.vision.ee.ethz.ch/cvl/DIV2K/)
            DIV2K/DIV2K_valid_HR/ -- 0801.png, 0802.png, ..., 0900.png validation HR images
            (will be available to the participants at the beginning of the final evaluation phase)
            """
            idx_begin = self.args.n_train
            idx_end = self.args.offset_val + self.args.n_val   # offset_val  default 800

        for i in range(idx_begin + 1, idx_end + 1):
            filename = '{:0>4}'.format(i)   # i=2-->0002;   i=123-->0123
            list_hr.append(os.path.join(self.dir_hr, filename + self.ext))   # ext 扩展名
            for si, s in enumerate(self.scale):
                list_lr[si].append(os.path.join(
                    self.dir_lr,
                    'X{}.00/{}{}'.format(s, filename,  self.ext)  # img 的话会被自动转换成 .png 见下面
                ))

        return list_hr, list_lr

    def _set_filesystem(self, dir_data):
        self.apath = dir_data + '/DIV2K'   # 原始是DIV2K，但是咱也不知道为啥就800了，咱也不敢问
        self.dir_hr = os.path.join(self.apath, 'HR')
        self.dir_lr = os.path.join(self.apath, 'LR_bicubic')
        self.ext = '.png'

    def _name_hrbin(self):                    # 存储了所有的图片的 .npy 文件
        return os.path.join(
            self.apath,
            'bin',
            '{}_bin_HR.npy'.format(self.split)
        )

    def _name_lrbin(self, scale):             # 存储了所有的图片的 .npy 文件
        return os.path.join(
            self.apath,
            'bin',
            '{}_bin_LR_X{}.npy'.format(self.split, scale)
        )

    def __len__(self):
        """
        虚拟出一个epoch，之前是 iter 个 batch_size 之后结束一个 epoch ，然后valid在 test_every 个 iter 之后测试
        现在将一个 epoch 虚拟为 n_train * repeat =  n_train * (test_every // (n_train // batch_size))
        """
        if self.train:
            return len(self.images_hr) * self.repeat
        else:
            return len(self.images_hr)

    def _get_index(self, idx):
        if self.train:
            return idx % len(self.images_hr)
        else:
            return idx


#!/usr/bin/python
# -*- coding: UTF-8 -*-
# import win32api
# import win32con
# import win32gui
# import os
# import random       #随机数生成模块
# import re

from selenium import webdriver    #载入selenium模块
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from urllib import request       #载入urllib模块,用来获取代理IP
chrome_Options = webdriver.ChromeOptions()  # 浏览器参数

class OpenText (object):
    """
        获取txt内容为列表
        path 为文件地址路径
        listname 为列表名称
        """
    def __init__(self, path, listname):
        self.path = path
        self.listname = listname

    # @staticmethod
    def txtlist(self):
        self.listname = []  # 定义空列表
        with open(self.path, 'r') as f:        #循环读取文件
            adsUrlList = (f.readlines())
            for link in adsUrlList:
                link=link.strip()
                self.listname.append(link)  # 生成列表

        while '' in self.listname:
            self.listname.remove('')    #遍历列表 删除空元素
        return self.listname

    def total(self):
        return len(self.listname)    #获取总数

class Elements(object):
    """
    定位元素，获取所有匹配值
    elements 为一组元素,如：ads = driver.find_elements_by_tag_name("iframe")
    listname 为列表名称
    """
    def __init__(self, elements, listname):
        self.elements = elements
        self.listname = listname

    def elementslist(self): #获取到一个列表
        self.listname = []  # 定义空列表
        for i in self.elements:
            element = (i.get_attribute("name")) # get_attribute获取name的值
            self.listname.append(element) # 生成列表
        return self.listname

class WaitOpen(object):
    """
        等待页面加载成功
        1、首次5s内异常，执行刷新一次
        2、刷新后异常，执行关闭当前标签
        3、重新打开页面异常，执行关闭当前标签
        4、再次重新打开页面异常，执行关闭当前标签
        """
    def __init__(self, url, e, dr):
        self.url = url
        self.e = e
        self.dr = dr
    def open(self):
        try:
            self.dr = webdriver.Chrome(chrome_options=chrome_Options)
            self.dr.get(self.url)
            WebDriverWait(self.dr, 5).until(EC.presence_of_element_located((By.XPATH, self.e)))
        except Exception:
            now_handle = self.dr.current_window_handle  # 获取当前窗口句柄
            print('当前窗口句柄是：', now_handle)
            print("刷新第一次！5s钟内未加载完成,即将刷新当前窗口！")
            self.dr.refresh()
            try:
                WebDriverWait(self.dr, 5).until(EC.presence_of_element_located((By.XPATH, self.e)))
            except Exception:
                now_handle = self.dr.current_window_handle  # 获取当前窗口句柄
                print('当前窗口句柄是：', now_handle)
                print("关闭第1次！5s钟内未加载完成,即将关闭当前窗口！")
                self.dr.close()
                try:
                    self.dr = webdriver.Chrome(chrome_options=chrome_Options)
                    self.dr.get(self.url)
                    WebDriverWait(self.dr, 5).until(EC.presence_of_element_located((By.XPATH, self.e)))
                except Exception:
                    now_handle = self.dr.current_window_handle  # 获取当前窗口句柄
                    print('当前窗口句柄是：', now_handle)
                    print("关闭第2次！5s钟内未加载完成,即将关闭当前窗口！")
                    self.dr.close()
"""
                    try:
                        driver = webdriver.Chrome(chrome_options=chrome_Options)
                        driver.get(url)
                        WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, e)))
                    except Exception:
                        now_handle = driver.current_window_handle  # 获取当前窗口句柄
                        print('当前窗口句柄是：', now_handle)
                        print("关闭第3次！5s钟内未加载完成,即将关闭当前窗口！")
                        driver.close()
                        try:
                            driver = webdriver.Chrome(chrome_options=chrome_Options)
                            driver.get(url)
                            WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, e)))
                        except Exception:
                            now_handle = driver.current_window_handle  # 获取当前窗口句柄
                            print('当前窗口句柄是：', now_handle)
                            print("关闭第4次！5s钟内未加载完成,即将关闭当前窗口！")
                            driver.close()
                            try:
                                driver = webdriver.Chrome(chrome_options=chrome_Options)
                                driver.get(url)
                                WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, e)))
                            except Exception:
                                now_handle = driver.current_window_handle  # 获取当前窗口句柄
                                print('当前窗口句柄是：', now_handle)
                                print("关闭第5次！5s钟内未加载完成,即将关闭当前窗口！")
                                print('该页面未正常打开', url)
                                driver.close()
"""
"""
if __name__ == '__main__':
    adsUrl = "text/links.txt"
    clickWords = "text/words.txt"
    OpenText = OpenText(adsUrl, 'links')
    # print(OpenText.txtlist())
    print(OpenText.total())
    chrome_Options = webdriver.ChromeOptions()  # 浏览器参数
    driver = webdriver.Chrome(chrome_options=chrome_Options)
    driver.get('http://www.iqly.cn')
    ads = driver.find_elements_by_tag_name("iframe")
    a =Elements(ads, 'ls')
    print(a.elementslist())

"""


import matplotlib.pyplot as plt
import os
import pprint
from math import inf

import numpy as np

from src.util.io import write_data_frame, write_text, write_npz, \
    write_matplotlib_figure

def save_train_test(df_train, df_test, save_to):
    write_data_frame(os.path.join(save_to, "df_train.csv"), df_train)
    write_data_frame(os.path.join(save_to, "df_test.csv"), df_test)

def save_X_y(X_train, y_train, X_test, y_test, feature_names, save_to):
    write_text(
        os.path.join(save_to, "X_y_stats.txt"),

        "X_train shape: " + str(X_train.shape) + "\n"
        + "y_train length: " + str(len(y_train)) + "\n"
        + "X_test shape: " + str(X_test.shape) + "\n"
        + "y_test length: " + str(len(y_test)) + "\n"
        + "Feature names:\n" + pprint.pformat(feature_names) + "\n"
    )
    write_npz(os.path.join(save_to, "X_train.npz"), X_train)
    write_npz(os.path.join(save_to, "X_test.npz"), X_test)

def save_filtered_features(feature_names, feature_names_new, save_to):
    old_set = set(feature_names)
    new_set = set(feature_names_new)

    filtered_features = sorted(old_set.difference(new_set))

    write_text(
        os.path.join(save_to, "filtered_features.txt"),
        pprint.pformat(filtered_features)
    )

def save_classifier_stats(stats, save_to):
    np.set_printoptions(linewidth=inf, formatter={
        "float": lambda x: "{0:0.4f}".format(x)
    })

    write_text(
        os.path.join(save_to, stats["name"] + "_summary.txt"),

        "Classifier: " + stats["name"] + "\n"
        + "Params: " + str(stats["params"]) + "\n"
        + "\n"
        + "Labels: " + str(stats["labels"]) + "\n"
        + "\n"
        + "Training accuracy: " + str(stats["training_accuracy"]) + "\n"
        + "Training confusion matrix:\n"
        + np.array2string(stats["training_confusion_matrix"]) + "\n"
        + "\n"
        + "Accuracy: " + str(stats["accuracy"]) + "\n"
        + "\n"
        + "Precision: " + np.array2string(stats["precision"]) + "\n"
        + "Mean: " + str(stats["precision_mean"]) + "\n"
        + "Std: " + str(stats["precision_std"]) + "\n"
        + "\n"
        + "Recall: " + np.array2string(stats["recall"]) + "\n"
        + "Mean: " + str(stats["recall_mean"]) + "\n"
        + "Std: " + str(stats["recall_std"]) + "\n"
        + "\n"
        + "F1 score: " + np.array2string(stats["F1"]) + "\n"
        + "Mean: " + str(stats["F1_mean"]) + "\n"
        + "Std: " + str(stats["F1_std"]) + "\n"
        + "\n"
        + "Cohen Kappa score: " + str(stats["cohen_kappa"]) + "\n"
        + "Confusion matrix:\n"
        + np.array2string(stats["confusion_matrix"]) + "\n"
        + "\n"
    )

    write_text(
        os.path.join(save_to, stats["name"] + "_top_features.txt"),
        top_features_to_string(stats["top_features"])
    )

    write_data_frame(
        os.path.join(save_to, stats["name"] + "_accurate_rows.csv"),
        stats["accurate_rows"]
    )

    write_data_frame(
        os.path.join(save_to, stats["name"] + "_error_rows.csv"),
        stats["error_rows"]
    )

def top_features_to_string(features):
    if isinstance(features, tuple):
        return "Negative features:\n" + pprint.pformat(features[0]) + "\n" \
               + "Positive features:\n" + pprint.pformat(features[1]) + "\n"
    elif isinstance(features, list):
        return "Top features:\n" + pprint.pformat(features) + "\n"
    elif features is None:
        return "Top features: " + str(None) + "\n"
    else:
        raise ValueError

def save_classifiers_comparison(all_stats, abbreviations, save_to):
    accuracies = [stats["accuracy"] for stats in all_stats]
    n = len(accuracies)

    plt.bar(np.arange(n), accuracies)

    plt.xticks(range(n), abbreviations)
    plt.yticks()

    plt.title("Classifier Comparison")
    plt.xlabel("Classifier")
    plt.ylabel("Accuracy")

    write_matplotlib_figure(os.path.join(save_to, "comparison.png"), plt)


import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
import time
from bayes_opt import BayesianOptimization
import exp
import dataset

class ConvLSTM(nn.Module):

    def __init__(self, input_dim, hidden_dim, output_dim, num_layers, num_filters, filter_size, dropout, use_bn, window_len):

        super(ConvLSTM, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim

        self.num_layers = num_layers
        self.num_filters = num_filters
        self.filter_size = filter_size

        self.dropout = dropout
        self.use_bn = use_bn
        self.window_len = window_len

        # Layers 2-5
        self.conv1 = nn.Conv1d(input_dim, num_filters, filter_size)
        self.conv2 = nn.Conv1d(num_filters, num_filters, filter_size)
        self.conv3 = nn.Conv1d(num_filters, num_filters, filter_size)
        self.conv4 = nn.Conv1d(num_filters, num_filters, filter_size)

        # Layers 6-7 - each dense layer has LSTM cells
        self.lstm1 = nn.LSTM(num_filters, hidden_dim, num_layers, batch_first=True)
        self.lstm2 = nn.LSTM(hidden_dim, hidden_dim, num_layers, batch_first=True)
        self.lstm3 = nn.LSTM(hidden_dim, hidden_dim, num_layers, batch_first=True)
        self.lstm4 = nn.LSTM(hidden_dim, hidden_dim, num_layers, batch_first=True)
        self.lstm5 = nn.LSTM(hidden_dim, hidden_dim, num_layers, batch_first=True)

        # Layer 9 - prepare for softmax
        self.fc = nn.Linear(hidden_dim, output_dim)
        self.dropout = nn.Dropout(dropout)

    def init_hidden(self, input_shape):
        '''
        Initializes hidden state

        Create two new tensors with sizes n_layers x batch_size x n_hidden,
        initialized to zero, for hidden state and cell state of LSTM
        '''
        weight = next(self.parameters()).data

        # changed this from batch_size to 3*batch_size
        hidden = (weight.new(self.num_layers, input_shape[0], self.hidden_dim).zero_(),
        weight.new(self.num_layers, input_shape[0], self.hidden_dim).zero_())

        return hidden

    def forward(self, x):

        # Layer 1 - flatten (see -1)
        self.hidden = self.init_hidden(x.shape)
        x = x.view(-1, self.input_dim, self.window_len)

        # Layers 2-5 - RELU
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))

        # Layers 5 and 6 - flatten
        x = x.view(1, -1, self.num_filters)

        # Layers 6-8 - hidden layers
        x, self.hidden = self.lstm1(x, self.hidden)
        x, self.hidden = self.lstm2(x, self.hidden)
        x, self.hidden = self.lstm3(x, self.hidden)
        x, self.hidden = self.lstm4(x, self.hidden)
        x, self.hidden = self.lstm5(x, self.hidden)

        # Layers 8 - flatten, fully connected for softmax. Not sure what dropout does here
        x = x.contiguous().view(-1, self.hidden_dim)
        x = self.dropout(x)
        out = self.fc(x[:, -1])

        # out = out.view(self.batch_size, -1, self.output_dim)[:, -1, :]

        return out

class LSTM(nn.Module):

    def __init__(self, input_dim, hidden_dim, output_dim, num_layers, dropout, use_bn):

        super(LSTM, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim
        self.num_layers = num_layers

        self.dropout = dropout
        self.use_bn = use_bn

        self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers, batch_first=True)
        self.fc = nn.Linear(self.hidden_dim, output_dim)

    def init_hidden(self, input_shape):
        return (torch.zeros(self.num_layers, input_shape[0], self.hidden_dim),
        torch.zeros(self.num_layers, input_shape[0], self.hidden_dim))

    def forward(self, x):
        self.hidden = self.init_hidden(x.shape)
        lstm_out, self.hidden = self.lstm(x, self.hidden)
        out = self.fc(lstm_out[:, -1])
        return out

class CNN(nn.Module):
    def __init__(self, input_dim, output_dim, num_filters, filter_size, dropout, window_len):
        super(CNN, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.num_filters = num_filters
        self.filter_size = filter_size
        self.dropout = dropout
        self.window_len = window_len

        self.conv1 = nn.Conv1d(input_dim, num_filters, filter_size)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv1d(num_filters, num_filters, filter_size)
        self.conv3 = nn.Conv1d(num_filters, num_filters, filter_size)

        self.fc = nn.Linear(num_filters, output_dim)

    def forward(self, x, batch_size):
        x = x.view(-1, self.input_dim, self.window_len)
        print('forward 1:', x.shape)
        '''
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        위의 식으로 코드 돌리면 filter 개수가 절반이 되어서 에러가 남
        '''
        x = F.relu(self.conv1(x))
        print('forward 2:',x.shape)
        x = F.relu(self.conv2(x))
        print('forward 3:',x.shape)
        x = F.relu(self.conv3(x))
        print('forward 4:',x.shape)

        x = x.view(1, -1, self.num_filters)
        print('forward 5:',x.shape)
        #x = self.dropout(x)
        out = self.fc(x)
        print('forward 6:',out.shape)
        #out = out.view(batch_size, -1, self.output_dim)[:, -1, :]
        out = np.squeeze(out)
        print('forward 7:',out.shape)

        return out

class Manager():
    def __init__(self, args):
        self.trainset = dataset.NumDataset('data/FakeData.csv', args.x_frames, args.y_frames, args.str_len)
        self.valset = dataset.NumDataset('data/FakeData.csv', args.x_frames, args.y_frames, args.str_len)
        self.testset = dataset.NumDataset('data/FakeData.csv', args.x_frames, args.y_frames, args.str_len)
        self.device = args.device

        # Select the model type
        if args.model == 'ConvLSTM':
            self.model = ConvLSTM(args.input_dim, args.hid_dim, args.y_frames, args.n_layers, args.n_filters,
                                  args.filter_size, args.dropout, args.use_bn, args.str_len)
        elif args.model == 'LSTM':
            self.model = LSTM(args.input_dim, args.hid_dim, args.y_frames, args.n_layers, args.dropout, args.use_bn)
        elif args.model == 'CNN':
            self.model = CNN(args.input_dim, args.y_frames, args.n_filters, args.filter_size, args.dropout, args.str_len)
        else:
            raise ValueError('In-valid model choice')

        self.model.to(self.device)

        self.pbounds = {
        'learning_rate': args.lr,
        'batch_size': args.batch_size
        }

        self.bayes_optimizer = BayesianOptimization(
        f=self.train,
        pbounds=self.pbounds
        )

    def train(self, learning_rate, batch_size):

        model = self.model
        batch_size = round(batch_size)
        print('batch size: ', batch_size)
        loss_fn = torch.nn.CrossEntropyLoss()

        trainloader = DataLoader(self.trainset, batch_size=batch_size,
        shuffle=True, drop_last=True)

        optimizer = optim.Adam(model.parameters(), lr=learning_rate)
        model.train()
        model.zero_grad()
        optimizer.zero_grad()

        train_acc = 0.0
        train_loss = 0.0

        for i, (X, y) in enumerate(trainloader):

            print('X1: ', X.shape)
            print('y2: ', y.shape)

            X = X.transpose(0, 1).float().to(self.device)
            print('X2: ', X.shape)

            y_true = y.long().to(self.device)
            print('y_true shape: ', y_true.shape)
            y_1 = y_true.view(-1)
            print('y_1 shape: ', y_1.shape)

            model.zero_grad()
            optimizer.zero_grad()
            if model == 'ConvLSTM' or model == 'LSTM':
                model.hidden = [hidden.to(args.device) for hidden in model.init_hidden()]
            print('batch size final: ', batch_size)
            y_pred = model(X, batch_size)
            print('y_pred shape', y_pred.shape)
            loss = loss_fn(y_pred, y_true.view(-1))
            loss.backward()
            optimizer.step()

            _, y_pred = torch.max(y_pred.data, 1)
            train_loss += loss.item()
            train_acc += (y_pred == y_true).sum()

        train_loss = train_loss / len(trainloader)
        train_acc = train_acc / len(trainloader)
        train_acc = float(train_acc)

        return train_loss, train_acc

    def validate(self, loss_fn, args, batch_size):
        model = self.model
        batch_size = round(batch_size)
        valloader = DataLoader(self.valset, batch_size=args.batch_size,
        shuffle=False, drop_last=True)
        model.eval()

        val_acc = 0.0
        val_loss = 0.0
        with torch.no_grad():
            for i, (X, y) in enumerate(valloader):

                X = X.transpose(0, 1).float().to(args.device)
                y_true = y.long().to(args.device)
                if args.model == 'ConvLSTM' or args.model == 'LSTM':
                    model.hidden = [hidden.to(args.device) for hidden in model.init_hidden()]

                y_pred = model(X, batch_size)
                loss = loss_fn(y_pred, y_true.view(-1))

                _, y_pred = torch.max(y_pred.data, 1)
                val_loss += loss.item()
                val_acc += (y_pred == y_true).sum()

        val_loss = val_loss / len(valloader)
        val_acc = val_acc / len(valloader)
        val_acc = float(val_acc)

        return val_loss, val_acc

    def test(self, args):
        model = self.model
        testloader = DataLoader(self.testset, batch_size=args.inference_batch_size,
                                shuffle=False, drop_last=True)
        model.eval()

        test_acc = 0.0
        with torch.no_grad():
            for i, (X, y) in enumerate(testloader):

                X = X.transpose(0, 1).float().to(args.device)
                y_true = y[:, 0].long().to(args.device)
                if args.model == 'ConvLSTM' or args.model == 'LSTM':
                    model.hidden = [hidden.to(args.device) for hidden in model.init_hidden()]

                y_pred = model(X)
                _, y_pred = torch.max(y_pred.data, 1)
                test_acc += (y_pred == y_true).sum()

        test_acc = test_acc / len(testloader)
        test_acc = float(test_acc)
        return test_acc

def experiment(mode, args):

    # ===== List for epoch-wise data ====== #
    train_losses = []
    val_losses = []
    train_accs = []
    val_accs = []
    # ===================================== #

    loss_fn = torch.nn.CrossEntropyLoss()
    manager = Manager(args)

    if args.mode == 'train':
        for epoch in range(args.epoch):  # loop over the dataset multiple times
            ts = time.time()
            print('Start training ... ')
            manager.bayes_optimizer.maximize(args.init_points, args.n_iter, acq='ei', xi=0.01)
            exp.save_exp_result(manager)

            print('Start validation ... ')
            val_loss, val_acc = manager.validate(loss_fn, args)
            te = time.time()

            # ====== Add Epoch Data ====== #
            train_losses.append(train_loss)
            val_losses.append(val_loss)
            train_accs.append(train_acc)
            val_accs.append(val_acc)
            # ============================ #

            print(
                'Epoch {}, Acc(train/val): {:2.2f}/{:2.2f}, Loss(train/val) {:2.5f}/{:2.5f}. Took {:2.2f} sec'.format(epoch, train_acc, val_acc, train_loss, val_loss, te - ts))
    elif args.mode == 'test':
        test_acc = manager.test(args)

    # ======= Add Result to Dictionary ======= #
    result = {}
    if args.mode == 'train':
        result['train_losses'] = train_losses
        result['val_losses'] = val_losses
        result['train_accs'] = train_accs
        result['val_accs'] = val_accs
        result['train_acc'] = train_acc
        result['val_acc'] = val_acc
    elif args.mode == 'test':
        result['test_acc'] = test_acc

    return vars(args), result


#!/usr/bin/env python3
from arp.arp import ARProcess
from baselines.common.cmd_util import make_mujoco_env
from common.utils import mujoco_arg_parser
from baselines.common import tf_util as U
import time
import matplotlib.pyplot as plt
import numpy as np
from rl_experiments.normalized_env import NormalizedEnv

def train(env_id, num_timesteps, seed, p, alpha):
    from common import ar_mlp_policy
    from ar_ppo import ar_pposgd_simple
    U.make_session(num_cpu=1).__enter__()
    env = make_mujoco_env(env_id, seed)
    env = NormalizedEnv(env)
    ar = ARProcess(p, alpha, size=env.action_space.shape[-1])
    def policy_fn(name, ob_space, ac_space):
        return ar_mlp_policy.ARMlpPolicy(name=name, ob_space=ob_space, ac_space=ac_space,
           hid_size=64, num_hid_layers=2, phi=ar.phi, sigma_z=ar.sigma_z)

    # Plot the learning curve
    plt.ion()
    fig = plt.figure(figsize=(10, 6))
    ax1 = fig.add_subplot(111)
    hl1, = ax1.plot([], [], markersize=10, color='r')
    ax1.set_xlabel("# time steps")
    ax1.set_ylabel("Average return")
    ax1.set_title("Learning progress")
    def mujoco_callback(locals, globals):
        if not 'seg' in locals:
            return
        episodic_returns = locals['episodic_returns']
        episodic_lengths = locals['episodic_lengths']
        ep_rets = locals['seg']['ep_rets']
        ep_lens = locals['seg']['ep_lens']
        if len(ep_rets):
            episodic_returns += ep_rets
            episodic_lengths += ep_lens
            window_size_steps = 5000
            x_tick = 1000
            if episodic_lengths:
                ep_lens = np.array(episodic_lengths)
                returns = np.array(episodic_returns)
            cum_episode_lengths = np.cumsum(ep_lens)
            if cum_episode_lengths[-1] >= x_tick:
                steps_show = np.arange(x_tick, cum_episode_lengths[-1] + 1, x_tick)
                rets = []

                for i in range(len(steps_show)):
                    rets_in_window = returns[(cum_episode_lengths > max(0, x_tick * (i + 1) - window_size_steps)) *
                                             (cum_episode_lengths < x_tick * (i + 1))]
                    if rets_in_window.any():
                        rets.append(np.mean(rets_in_window))
                if len(rets) > 0:
                    hl1.set_xdata(np.arange(1, len(rets) + 1) * x_tick)
                    ax1.set_xlim([x_tick, len(rets) * x_tick])
                    hl1.set_ydata(rets)
                    ax1.set_ylim([np.min(rets), np.max(rets) + 50])
            time.sleep(0.01)
            fig.canvas.draw()
            fig.canvas.flush_events()

    ar_pposgd_simple.learn(env, policy_fn,
                           callback=mujoco_callback,
                           max_timesteps=num_timesteps,
                           timesteps_per_actorbatch=2048,
                           clip_param=0.2, entcoeff=0.0,
                           optim_epochs=10, optim_stepsize=3e-4, optim_batchsize=64,
                           gamma=0.995, lam=0.995, schedule='linear', ar=ar
                           )
    env.close()

def main():
    args = mujoco_arg_parser().parse_args()
    train(args.env, num_timesteps=args.num_timesteps, seed=args.seed, p=args.p, alpha=args.alpha)

if __name__ == '__main__':
    main()


from .                   import views
from django.conf.urls    import url
from django.contrib.auth import views as auth_views

app_name = 'accounts'

urlpatterns = [
	url(r'^$',
		views.dashboard,
		name = 'dashboard'),
	url(r'^login/$',
		auth_views.login,
		{'template_name': 'accounts/login.html', 'extra_context':{'title':'Login', 'current': 4}},
        name = 'login'),
	url(r'^logout/$',
		auth_views.logout,
		{'next_page': 'core:index'},
		name = 'logout'),
	url(r'^signup/$',
		views.register,
		name = 'register'),
]


#!/usr/bin/python
"""
  (C) Copyright 2020 Intel Corporation.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
  The Government's rights to use, modify, reproduce, release, perform, display,
  or disclose this software are subject to the terms of the Apache License as
  provided in Contract No. B609815.
  Any reproduction of computer software, computer software documentation, or
  portions thereof marked with this legend must also reproduce the markings.
"""
from ior_test_base import IorTestBase
from mdtest_test_base import MdtestBase

import write_host_file

class LargeFileCount(MdtestBase, IorTestBase):
    """Test class Description: Runs IOR and MDTEST to create large number
                               of files.
    :avocado: recursive
    """

    def single_client(self, api):
        """Use one client only for POSIX api until DAOS-3320
           is resolved.
        Arguments:
            api (str): IOR or MDTEST api to be used
        """
        if api == "POSIX":
            self.hostlist_clients = [self.hostlist_clients[0]]
            self.hostfile_clients = write_host_file.write_host_file(
                self.hostlist_clients, self.workdir,
                self.hostfile_clients_slots)

    def test_largefilecount(self):
        """Jira ID: DAOS-3845.
        Test Description:
            Run IOR and MDTEST with 48 clients.
        Use Cases:
            Run IOR for 5 mints with DFS and POSIX
            Run MDTEST to create 25K files with DFS and POSIX
        :avocado: tags=all,daosio,hw,large,full_regression,largefilecount
        """
        apis = self.params.get("api", "/run/largefilecount/*")

        for api in apis:
            self.ior_cmd.api.update(api)
            self.mdtest_cmd.api.update(api)

            # Until DAOS-3320 is resolved run IOR for POSIX
            # with single client node
            self.single_client(api)

            # run mdtest and ior
            self.execute_mdtest()
            self.run_ior_with_pool()

    def test_largefilecount_rc(self):
        """Jira ID: DAOS-3845.
        Test Description:
            Run IOR and MDTEST with 48 clients with very large file counts.
            This test is not supposed to run as part of pr or weekly runs.
            This should only be run before release as desired.
        Use Cases:
            Run IOR for 5 mints with DFS and POSIX
            Run MDTEST to create 25K files with DFS and POSIX
        :avocado: tags=all,daosio,hw,large,rc,largefilecount_rc
        """
        apis = self.params.get("api", "/run/largefilecount/*")
        num_of_files_dirs_rc = self.params.get("num_of_files_dirs_rc",
                                               "/run/mdtest/*")

        # update mdtest file count
        self.mdtest_cmd.num_of_files_dirs.update(num_of_files_dirs_rc)

        for api in apis:
            self.ior_cmd.api.update(api)
            self.mdtest_cmd.api.update(api)

            # Until DAOS-3320 is resolved run IOR for POSIX
            # with single client node
            self.single_client(api)

            # run mdtest and ior
            self.execute_mdtest()
            self.run_ior_with_pool()


#!/usr/bin/python

import re
from sys import argv

def increase_version_number(tags):
    if tags[2]<10:
        tags[2]+=1
    else:
        tags[2]=0
        if tags[1]<10:
            tags[1]+=1
        else:
            tags[1]=0
            tags[0]+=1
    return tags

def check_semver(version):
    m =  re.findall('\d+',version)
    if m:
        return [int(m[0]),int(m[1]),int(m[2])]
    raise Exception("Last release Tag not in proper format of vX.X.X")

current_version = check_semver(argv[1])
new_version = increase_version_number(current_version)

res = str(".".join(map(str, new_version)))

print("v" + res)


import assets.NeuralNetwork
import torchvision
from torchvision import datasets, transforms, models
import assets.loadJson
import argparse

arguments_parser = argparse.ArgumentParser(description='predict-file')
arguments_parser.add_argument('--input_img', default='./flowers/test/1/image_06743.jpg', action="store", type = str)
arguments_parser.add_argument('--checkpoint', default='checkpoint.pth', action="store",type = str)
arguments_parser.add_argument('--top_k', default=5, dest="top_k", action="store", type=int)
arguments_parser.add_argument('--category_names', dest="category_names", action="store", default='cat_to_name.json')
arguments_parser.add_argument('--gpu', default="gpu", action="store", dest="gpu")

arguments = arguments_parser.parse_args()
path_image = arguments.input_img
top_k = arguments.top_k
gpu = arguments.gpu
input_img = arguments.input_img
checkpoint_path = arguments.checkpoint
idx_to_naems_file = arguments.category_names

cat = assets.loadJson.load_json(idx_to_naems_file)

model = assets.NeuralNetwork.loading_model(checkpoint_path)

assets.NeuralNetwork.predict(input_img, model, cat, top_k, gpu)


"""Runs Treadmill application presence daemon.
"""

import logging

import click

from treadmill import appenv
from treadmill import context
from treadmill import runtime as app_runtime
from treadmill import zkutils

_LOGGER = logging.getLogger(__name__)

#: 3 hours
_TICKETS_REFRESH_INTERVAL = 60 * 60 * 3

def init():
    """App main."""

    @click.group(name='presence')
    def presence_grp():
        """Register container/app presence."""
        context.GLOBAL.zk.conn.add_listener(zkutils.exit_on_lost)

    @presence_grp.command()
    @click.option('--approot', type=click.Path(exists=True),
                  envvar='TREADMILL_APPROOT', required=True)
    @click.option('--runtime', default=app_runtime.DEFAULT_RUNTIME)
    @click.option('--refresh-interval', type=int,
                  default=_TICKETS_REFRESH_INTERVAL)
    @click.argument('manifest', type=click.Path(exists=True))
    @click.argument('container-dir', type=click.Path(exists=True))
    def register(approot, runtime, refresh_interval, manifest,
                 container_dir):
        """Register container presence."""
        tm_env = appenv.AppEnvironment(approot)

        app_runtime.get_runtime(runtime, tm_env, container_dir).register(
            manifest, refresh_interval
        )

    @presence_grp.command()
    @click.option('--approot', type=click.Path(exists=True),
                  envvar='TREADMILL_APPROOT', required=True)
    @click.option('--runtime', default=app_runtime.DEFAULT_RUNTIME)
    @click.argument('manifest', type=click.Path(exists=True))
    @click.argument('container-dir', type=click.Path(exists=True))
    def monitor(approot, runtime, manifest, container_dir):
        """Monitor container services."""
        tm_env = appenv.AppEnvironment(approot)

        app_runtime.get_runtime(runtime, tm_env, container_dir).monitor(
            manifest
        )

    del register
    del monitor
    return presence_grp


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

class MNIST:
    image_size = 28
    image_size_flat = image_size * image_size

    def __init__(self, val_ratio=0.1, data_dir='MNIST_data'):
        self.val_ratio = val_ratio
        self.data_dir = data_dir

        img_train = self.load_flat_images('train-images-idx3-ubyte.gz')
        lab_train = self.load_labels('train-labels-idx1-ubyte.gz')
        img_test = self.load_flat_images('t10k-images-idx3-ubyte.gz')
        lab_test = self.load_labels('t10k-labels-idx1-ubyte.gz')

        self.val_train_num = round(len(img_train) * self.val_ratio)
        self.main_train_num = len(img_train) - self.val_train_num

        self.img_train = img_train[0:self.main_train_num] / 255.0
        self.lab_train = lab_train[0:self.main_train_num].astype(np.int)
        self.img_train_val = img_train[self.main_train_num:] / 255.0
        self.lab_train_val = lab_train[self.main_train_num:].astype(np.int)

        self.img_test = img_test / 255.0
        self.lab_test = lab_test.astype(np.int)

    def load_binary_to_num(self, dataset_name, offset):
        path = os.path.join(self.data_dir, dataset_name)
        with gzip.open(path, 'rb') as binary_file:
            data = np.frombuffer(binary_file.read(), np.uint8, offset=offset)
        return data

    def load_flat_images(self, dataset_name):
        data = self.load_binary_to_num(dataset_name, offset=16)
        images_flat_all = data.reshape(-1, self.image_size_flat)
        return images_flat_all

    def load_labels(self, dataset_name):
        labels_all = self.load_binary_to_num(dataset_name, offset=8)
        return labels_all

    def one_hot(self, labels):
        class_num = np.max(self.lab_test) + 1
        convert = np.eye(class_num, dtype=float)[labels]
        return convert

# The input argument must be a numpy array
def format_images(images):
    return images.reshape([-1, 28, 28])


import os
from flask import Flask, g
from flask_sijax import sijax
import flask_sijax
path = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app = Flask(__name__)

app.config['SIJAX_STATIC_PATH'] = path
app.config['SIJAX_JSON_URI'] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)

@app.route('/')
def index():
   return 'Index'

@flask_sijax.route(app, '/hello')
def hello():
   def say_hi(obj_response):
      obj_response.alert('Hi there!')
   if g.sijax.is_sijax_request:
      # Sijax request detected - let Sijax handle it
      g.sijax.register_callback('say_hi', say_hi)
      return g.sijax.process_request()
      return _render_template('sijaxexample.html')

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


from django.conf import settings

def update_dns_record(dns_record, ip):
    if dns_record.provider:
        config = settings.DYNAMICDNS_PROVIDERS[dns_record.provider]
        mod_path, mod_name = config['plugin'].rsplit('.', 1)
        exec('from {} import {} as DnsPlugin'.format(mod_path, mod_name))
        dns_plugin = DnsPlugin(dns_record.domain, config)
        dns_plugin.update(ip)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
def capitaliser_pays(nom):
    nom = nom.split(' ')
    nom = [nom[i].lower() for i in range(len(nom))]
    for i in range(len(nom)):
        if nom[i] != "and":
            nom[i] = nom[i].capitalize()

    nom = ' '.join(nom)
    place = nom.find('-')

    if place != -1:
        print(place)
        nom = list(nom)
        nom[place + 1] = nom[place + 1].upper()
        nom = ''.join(nom)

    return nom

if __name__ == '__main__':
    pays = [
        'Guinea-bisAu'
        'AfghanIstan',
        'albania',
        'algeria',
        'AndorRa',
        'angolA',
        'antigua ANd barbuda',
        'argEntina',
        'Armenia',
        'austrAlia',
        'ausTria',
        'azerBaijaN'
    ]
    for i in range(len(pays)):
        pays[i] = capitaliser_pays(pays[i])

    print(pays)


from os import read, register_at_fork
from datetime import datetime
from retro_video import RetroVideo

URL = "http://127.0.0.1:5000"
BACKUP_URL = "https://retro-video-store-api.herokuapp.com"

def main():
    print("WELCOME TO RETRO VIDEO STORE")
    # run_cli()
    pass

    def print_stars():
        print("\n**************************\n")

    def list_options():

        options = {
            "1": "Add a Video",
            "2": "Edit a Video",
            "3": "Delete a Video",
            "4": "Get information about all videos",
            "5": "Get information about one video",
            "6": "Add a customer",
            "7": "Edit a customer",
            "8": "Delete a customer",
            "9": "Get information about one customer",
            "10": "Get information about all customers",
            "11": "Check out a video to a customer",
            "12": "Check in a video to a customer",
            "13": "Quit"
            }

        print_stars()
        print("Welcome to the Retro Video Store CLI")
        print("These are the actions you can perform")
        print_stars()

        for choice_num in options:
            print(f"Option {choice_num}. {options[choice_num]}")

        print_stars()

        return options

    def make_choice(options, retro_video):
        valid_choices = options.keys()
        choice = None

        while choice not in valid_choices:
                print("Please choose an option from the list above")
                choice = input("Please enter you selection here: ")
        if choice in ['2','3'] and retro_video.selected_video==None:
            print("To edit or delete you must select a video")
            choice = "5"

        elif choice in ['7','8'] and retro_video.selected_customer==None:
            print("To edit or delete you must select a customer")
            choice = "9"

        elif choice in valid_choices:
            return choice

    def run_cli(play=True):
        retro_video = RetroVideo(url="https://retro-video-store-api.herokuapp.com")

        options = list_options()

        while play==True:

            choice = make_choice(options, retro_video)

            retro_video.print_selected()

            # Create a video
            if choice=='1':
                print("Great! Let's add a new video.")
                title=input("What is the title of your video? ")
                release_date=input("What is the release date of your video? ")
                total_inventory=input("What is the total inventory of your video? ")
                available_inventory = input("what is the available inventory of your video?")
                response = retro_video.create_video(title=title, total_inventory=total_inventory, release_date = release_date, available_inventory = available_inventory)

                print_stars()
                print("New video:", response)
            # Edit a video
            elif choice=='2':
                print(f"Okay! We will be editing {retro_video.selected_video}")
                title=input("What is the new title of your video? ")
                release_date=input("What is the new release date of your video? ")
                total_inventory=input("What is the new total inventory of your video? ")
                available_inventory = input("What is the new available inventory of your video? ")
                response = retro_video.update_video(title=title, release_date = release_date, total_inventory=total_inventory, available_inventory = available_inventory)

                print_stars()
                print("Updated Video:", response)
            # Delete selected video
            elif choice=='3':
                retro_video.delete_video()

                print_stars()
                print("Video has been deleted.")

                print_stars()
                print(retro_video.list_videos())
            # Get information about all videos
            elif choice=='4':
                print_stars()
                for video in retro_video.list_videos():
                    print(video)
            # Get information about one video
            elif choice=='5':
                title = input("Please enter the video title you would like to select: ")
                retro_video.get_video(title=title)
                if title == None:
                    print("Could not select. Please enter a video title.")

                if retro_video.selected_video:
                    print_stars()
                    print("Selected video: ", retro_video.selected_video)
    # ================================== Customer ========================================================================

            # Create a customer
            if choice=='6':
                print("Great! Let's add a new customer.")
                name=input("What is the name of your customer? ")
                phone=input("What is the phone number of your customer? ")
                postal_code=input("What is the postal_code of your customer? ")
                registered_at = input("what is the time your customer was registered at?")
                videos_checked_out = input("How many videos are check out for your customer?")
                response = retro_video.create_customer(name=name, phone=phone, postal_code = postal_code, registered_at=registered_at, videos_checked_out = videos_checked_out)

                print_stars()
                print("New customer:", response)
            # Edit a customer
            elif choice=='7':
                print(f"Okay! We will be editing {retro_video.selected_customer}")
                name=input("What is the new name of your customer? ")
                phone=input("What is the new phone number of your customer? ")
                postal_code=input("What is the new postal_code of your customer? ")
                registered_at= input("When was your customer registered at? ")
                videos_checked_out = input("How many videos are check out for your customer?")
                response = retro_video.update_customer(name=name, phone=phone, postal_code = postal_code, registered_at=registered_at, videos_checked_out= videos_checked_out)

                print_stars()
                print("Updated Customer:", response)
            # Delete selected customer
            elif choice=='8':
                retro_video.delete_customer()

                print_stars()
                print("Customer has been deleted.")

                print_stars()
                print(retro_video.list_customers())
            # Get information about all customers
            elif choice=='9':
                name = input("Please enter the customer name you would like to select: ")
                retro_video.get_customer(name=name)
                if name == None:
                    print("Could not select. Please enter a customer name.")

                if retro_video.selected_customer:
                    print_stars()
                    print("Selected customer: ", retro_video.selected_customer)
            # Get information about one customer
            elif choice=='10':
                print_stars()
                for customer in retro_video.list_customers():
                    print(customer)
# ============================================ Rentals =====================================
            # Check out a video to a customer
            elif choice == "11":
                print("Please enter the customer id and video id for the movie you would like to check out.")
                customer_id = input("Customer id: ")
                video_id = input("Video id: ")
                print(customer_id, video_id)
                response = retro_video.video_check_out(customer_id, video_id)
                print(response)
                print("Check out was successful")

            # Check in a video to a customer
            elif choice == "12":
                print("Please enter the customer id and video id for the movie you would like to check in.")
                customer_id = input("Customer id: ")
                video_id = input("Video id: ")
                response = retro_video.video_check_in(customer_id=customer_id, video_id=video_id)
                if response == 200:
                    print("Check in was successful")

    # ============================================ Quit =====================================
            # Quit
            elif choice == "13":
                play=False
                print("\nThanks for using the Retro Video Store CLI!")

            print_stars()

    run_cli()

if __name__ == "__main__":
    main()


def maopao(arr):
    count = len(arr)
    for i in range(0,count):
        for j in range(i+1,count):
            if arr[i]>arr[j]:
                arr[i],arr[j] = arr[j],arr[i]

    return arr

if __name__=="__main__":
    arr = [23,4,235]
    print(maopao(arr))


from datetime import datetime, date
from math import floor

def calculate_late_fee(rate, contract_end, date_return):
            ''' Calculates late fee
            For every given contract, adds 20% of the rate on top of the original rate for every
            day that has passed the contract_end date. If vehicle has not been returned
            the function returns late fee as it would be as of date of calculation '''

            try:
                d1 = datetime.fromisoformat(contract_end)
                d2 = datetime.fromisoformat(date_return)
                days_late = (d2-d1).days

                if days_late < 1 : return 0

                return ((days_late * int(rate)) * 0.2)

            except ValueError:
                #if date return has not been listed late fee wont be calculated
                d1 = datetime.fromisoformat(contract_end)
                d2 = datetime.today()
                days_late = (d2-d1).days

                if days_late < 1 : return 0

                return floor(((days_late * int(rate)) * 0.2))

def calculate_base_price(start, end, rate):
    """Calculates price of vehicle (this does not include late return fee)
       tries to calculate base price from date handout and date return,
       if date handover has been given but not date return it calculates the total cost until this day
       if they are not found then returnes rate """
    try:
        d1 = datetime.fromisoformat(start)
        d2 = datetime.fromisoformat(end)
        return floor((d2-d1).days * int(rate))
    except:
        d1 = datetime.fromisoformat(start)
        d2 = datetime.today()
        return floor((d2-d1).days * int(rate))

def calculate_total_price(contract):
    """Calculations of total price"""
    contract_start = contract['contract_start']
    contract_end = contract['contract_end']
    date_return = contract['date_return']
    rate = contract['rate']

    #late fee calculated as the day the contract ended versus the return date of the vehicle
    late_fee = calculate_late_fee(rate, contract_end, date_return)
    #base fee calculated from day wehicle contract start and until contract end.
    base_price = calculate_base_price(contract_start, contract_end, rate)

    return floor(late_fee + base_price)

def select_time_period(contracts, date_from, date_to):
            '''Recieves contracts and a start and end date.
            Filters out any contract that does not fit within the
            given time frame and returns the filtered list
            '''
            date_from = datetime.fromisoformat(date_from)
            date_to = datetime.fromisoformat(date_to)
            new_contracts = []
            for contract in contracts:
                contract_start = datetime.fromisoformat(contract['contract_start'])
                contract_end = datetime.fromisoformat(contract['contract_end'])

                # Check if contract fits in the given window
                if contract_start <= date_from: continue
                if contract_end >= date_to: continue

                new_contracts.append(contract)

            return new_contracts


from picar import front_wheels, back_wheels
import picar
import time

picar.setup()

class CarController:
    def __init__(self):
        self.fw = front_wheels.Front_Wheels()
        self.bw = back_wheels.Back_Wheels()
        self.speed = 30

    def turnStraight(self, angle=0):
        """
        turn front wheel straight
        """
        self.fw.turn_straight()
        time.sleep(0.5)

    def moveForward(self, t=0):
        """
        move the car forward, then stop
        """
        self.turnStraight()
        time.sleep(0.5)
        self.bw.forward()
        self.bw.speed = self.speed
        # make the car run for t second
        if t>0:
            time.sleep(t)
        self.bw.stop()

    def moveBackward(self, t=0):
        """
        move the car backward, then stop
        """
        self.turnStraight()
        time.sleep(0.5)
        self.bw.backward()
        self.bw.speed = self.speed
        # make the car run for t seconds
        if t>0:
            time.sleep(t)
        self.bw.stop()

    def turnLeft(self, angle=0):
        """
        turn front wheel to the left
        """
        self.fw.turn_left(angle)
        time.sleep(0.5)

    def turnRight(self, angle=0):
        """
        turn front wheel to the right
        """
        self.fw.turn_right(angle)
        time.sleep(0.5)

    def destroy():
        self.bw.stop()

if __name__ == '__main__':
    cc = CarController()
    try:
        main()
    except KeyboardInterrupt:
        destroy()


import random
def select(array, count):
    if len(array) <= count: return array

    for i in range(1, len(array)):
        array[i] += array[i-1]

    result = []
    has_select = [0 for i in range(len(array))]
    while count > 0:
        count -= 1
        val = random.uniform(0, array[-1])

        start = 0
        end = len(array) - 1
        while start <= end:
            mid = (start + end) // 2
            if array[mid] == val:
                break
            elif array[mid] > val:
                end = mid - 1
            else:
                start = mid + 1
        while has_select[mid] == 1:
            mid = (mid + 1) % len(array)
        result.append(mid)
        has_select[mid] = 1
    return result

print(select([random.sample([1,3,5], 1)[0] for i in range(999)], 200))

练习/带权重的随机抽奖程序.py

带权重的随机抽奖程序.py


#!/usr/bin/env python3
n = int(input())
ps = [tuple(map(int,input().split())) for i in range(n)]
qs = [(x+y, x-y) for (x,y) in ps] # rotate 45
xs = [x for (x,y) in qs]
ys = [y for (x,y) in qs]
dx = max(xs) - min(xs)
dy = max(ys) - min(ys)
l = max(dx, dy)
for q in \
        [ (min(xs) + l//2, min(ys) + l//2)
        , (min(xs) + l//2, max(ys) - l//2)
        , (max(xs) - l//2, min(ys) + l//2)
        , (max(xs) - l//2, max(ys) - l//2)
        ]:
    p = [((x+y)//2, (x-y)//2) for (x,y) in [q]][0]
    if len(set([abs(p[0] - x) + abs(p[1] - y) for (x,y) in ps])) == 1:
        break
print(*p)


import numpy as np
from sklearn import preprocessing
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt

data = np.loadtxt('spiral3.csv', delimiter=',')
data_train, data_test = train_test_split(data, test_size=0.25, random_state=0)
y_train = data_train[:, 2].astype(int)
y_test = data_test[:, 2].astype(int)
sc = preprocessing.StandardScaler().fit(data_train[:, :2])
X_train = sc.transform(data_train[:, :2])
X_test = sc.transform(data_test[:, :2])

clf = MLPClassifier(
    verbose=True,
    hidden_layer_sizes=(10),
    alpha=1.0e-8,
    max_iter=1000)

clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)

print(clf.score(X_train, y_train))
print(clf.score(X_test, y_test))

fig = plt.figure()
ax = fig.add_subplot()
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_hat, s=1, alpha=0.5)
plt.show()


import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
import numpy as np
import keras

#fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

#load dataset
dataframe = pandas.read_csv("iris.csv", header=None)
dataset = dataframe.values
x = dataset[:, 0:4].astype(float)
y = dataset[:, 4]

#encode class values as integers
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)
#convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_y)

#define baseline model
def baseline_model():
    model = Sequential()
    model.add(Dense(8, input_dim=4, activation="relu"))
    model.add(Dense(3, activation="softmax"))
    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])
    return model

estimator = KerasClassifier(build_fn=baseline_model(),
                            epochs=200,
                            batch_size=5,
                            verbose=1)

kfold = KFold(n_splits=10, shuffle=True, random_state=seed)

#results = cross_val_score(estimator, x, dummy_y, cv=kfold)
#print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
#https://machinelearningmastery.com/multi-class-classification-tutorial-keras-deep-learning-library/

num_classes = 3
epochs = 60

data = np.genfromtxt("iris.csv", delimiter=",", dtype=str)
np.random.shuffle(data)
x = data[:, :4].astype("float32")
y = data[:, -1]
encoder = LabelEncoder()
encoder.fit(y)
encoded_y = encoder.transform(y)
y = keras.utils.to_categorical(encoded_y, num_classes)

x_train = x[:120, :]
y_train = y[:120, :]
x_test  = x[120:, :]
y_test  = y[120:, :]

print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")

model = Sequential()
model.add(Dense(10, activation="relu", input_shape=(4,)))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation="softmax"))

model.compile(loss="categorical_crossentropy",
              optimizer=RMSprop(),
              metrics=["accuracy"])

history = model.fit(x_train, y_train,
                    batch_size=16,
                    epochs=epochs,
                    verbose=0,
                    validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=1)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
print(history.history["val_acc"][-5:])


import cv2 as cv
from PIL import Image
import glob, os, time
from natsort import natsorted, ns
import PPTXTest
import PdfTest
from functools import partial
from random import randint
from multiprocessing.dummy import Pool as ThreadPool
import preprocessingImageTest as ppc
import comprassionImageTest as cmprs
def stope():
    global globvar
    globvar = 1
def playvideoSIFTSURF(fnamevideo,binarmethod,fnamepdf,smethod,pers,test, showvideo, showbinar,showcrop,showwords, framenub,typevideo):
    global globvar
    globvar = 0
    filename, file_extension = os.path.splitext(fnamepdf)
    if file_extension == '.pdf':
        pass
    elif file_extension == '.pptx':
        PPTtoPDF(fnamepdf,'D:/123456.pdf')
        fnamepdf = 'D:/123456.pdf'
    else:
        print("BAD FILETYPE PRESENTATION")
        return
    directoryImage = PdfTest.pdftojpg(fnamepdf)
    cap = cv.VideoCapture(fnamevideo)
    print(cap.grab())

    frames = []
    files = glob.glob(directoryImage + "/*.jpg")
    for fram in natsorted(files,alg=ns.IGNORECASE):
        img = cv.imread(fram)
        img = cv.resize(img,(800,600))
        frames.append(img)

    k= 0

    img2 = cv.imread('1.png')

    img2 = cv.resize(img2,(800,700))

    SlideDetected = False
    FirstSlideDetected = False
    FindNewSlide = True
    start = time.time()
    slideChanges = []

    if test is True:
        countframes = cap.get(cv.CAP_PROP_FRAME_COUNT)
        for i in range(4):
            position = randint(int((countframes/4)*i), int((countframes/4))*(i+1))
            print(position)
            cap.set(cv.CAP_PROP_POS_FRAMES ,position)
            #cap.set(cv.CAP_PROP_POS_MSEC, position/cap.get(cv.CAP_PROP_FPS))
            ref, frame = cap.read()
            time.sleep(2)
            info_slide = []
            info_slide.append(frame)
            gaus = ppc.toGausian(frame)
            gray = ppc.togray(gaus)
            binar = ppc.select_binar(binarmethod,gray)
            crop_img = frame
            if typevideo == 2:
                crop_img = ppc.select_cropimg(pers,binar,frame)
            if crop_img is not None:
                crop_img = ppc.toGausian(crop_img)
                pools = ThreadPool(8)
                goods = pools.map(partial(cmprs.comprassionWithImagesParallel, crop_img),frames)
                pools.close()
            else:
                frame = ppc.toGausian(frame)
                pools = ThreadPool(8)
                goods = pools.map(partial(cmprs.comprassionWithImagesParallel, frame),frames)
                pools.close()
            maxlst = []
            for i in range(len(goods)):
                maxlst.append(len(goods[i][1]))
            tet = maxlst.index(max(maxlst)) +1
            info_slide.append(tet)
            print(maxlst, tet)
            slideChanges.append(info_slide)
        return(slideChanges)
    while(1):
        if globvar == 1:
            globvar = 0
            break
        ref, frame = cap.read()

        if frame is None:
            break
        if showvideo ==1:
            cv.imshow("frame",frame)

        k +=1

        if k%framenub == 0:

            gaus = ppc.toGausian(frame)
            gray = ppc.togray(gaus)
            binar = ppc.select_binar(binarmethod,gray)
            if showbinar == 1:
                cv.imshow("binar",binar)
            crop_img = frame
            if typevideo == 2:
                crop_img = ppc.select_cropimg(pers,binar,frame)

            if crop_img is not None:
                if showcrop == 1:
                    cv.imshow("Crop",crop_img)

                if SlideDetected is True:

                    if FirstSlideDetected is False:
                        FirstSlideDetected = True
                    else:
                        if smethod ==2:
                            img3, goods = cmprs.comprasion(crop_img,img2)
                        else:
                            img3, goods = cmprs.comprasionSurf(crop_img,img2)
                        if img3 is None:
                            continue
                        img2 = crop_img
                        print("LENGOODS IS ",len(goods))
                        if len(goods) < 200:
                            SlideDetected = False
                            FindNewSlide = True
                            print("SLIDES CHANGING")

                if FindNewSlide is True:
                    pools = ThreadPool(8)
                    goods = pools.map(partial(cmprs.comprassionWithImagesParallel, crop_img),frames)
                    pools.close()
                    maxlst = []
                    for i in range(len(goods)):
                        maxlst.append(len(goods[i][1]))
                    tet = maxlst.index(max(maxlst))
                    print("THAT IS SLIDE",tet,"AND MAX VALUES IS",max(maxlst))
                    if max(maxlst) > 100:
                        SlideDetected = True
                        FindNewSlide = False
                        img2 = crop_img

                #cv.imshow("crop",crop_img)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break

    print(slideChanges)
    end = time.time()
    print("total time is: ", start, end)
    print("total time is: ", end - start)
    cap.release()
    cv.destroyAllWindows()
    if showwords==1:
        return(dict, slideChanges)
    else:
        return(None,slideChanges)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
给定一个字符串，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。

示例：
    输入："Let's take LeetCode contest"
    输出："s'teL ekat edoCteeL tsetnoc"

链接：https://leetcode-cn.com/problems/reverse-words-in-a-string-iii
"""

class Solution:
    def reverseWords(self, s: str) -> str:

        char_list = [self.reverse(char) for char in s.split(" ")]

        return " ".join(char_list)

    def reverse(self, char):

        if not char:
            return ""

        rst = []
        for s in char:
            rst.insert(0, s)
        return "".join(rst)

if __name__ == '__main__':
    s = "Let's take LeetCode contest"
    rst = Solution().reverseWords(s)
    print("result is", rst)


import praw
import sub
import json
import dropbox
import time

class MassMessage:

	def __init__(self, sub, reddit, dropbox_api):
		self.sub = sub
		self.reddit = reddit
		self.dropbox_api = dropbox_api

	def doBackup(self):
		client = dropbox.Dropbox(self.dropbox_api)
		f = open(self.sub.existing_users, 'rb')

		ts = str(time.time())

		#DROPBOX SPECIAL PATH
		path = "/" + self.sub.subName + "/" + ts + "/" + self.sub.subName + "_" + "existing_users.txt"

		response = client.files_upload(f.read(), path, mode=dropbox.files.WriteMode.overwrite)

	def run(self):

		self.doBackup()

		count = 1

		newUsers = self.loadUsers()

		if(len(newUsers) < 1):
			self.sub.logMe("Short list! No users to PM...")
			return

		self.sub.logMe("Messaging the following users (next line):")
		print(newUsers)

		with open(self.sub.existing_users) as json_file:
			data = json.load(json_file)

			existingUsers = data["existing"]

			for USER_SEND in newUsers:

				if count <= self.sub.messager_limit:
					##check if user has already been messaged
					alreadyExists = USER_SEND in existingUsers
					if(not alreadyExists):
						bool = self.sendMessage(USER_SEND, count)
						if(bool):
							data["existing"].append(USER_SEND)
							self.writeFile(data)
							count = count + 1
					else:
						self.sub.logMe("Found duplicate: " + USER_SEND + "!")

				else:
					self.sub.logMe("Reached messager count!")
					break

			#Empty scraper_users.txt file after messaging everyone
			self.sub.logMe("Emptying new users file!")
			with open(self.sub.scraper_users, 'w'): pass

	def writeFile(self, data):

		sub = self.sub

		with open(sub.existing_users, 'w') as json_file:
			self.sub.logMe("Updating existing users file!")
			json.dump(data, json_file)

	def sendMessage(self, USER_SEND, count):
		reddit = self.reddit

		self.sub.logMe("Messaging: " + USER_SEND + "!")

		reddit.redditor(USER_SEND).message(self.sub.subject_line, self.sub.message_template)

		bool = True

		if(bool):
			self.sub.logMe("Successfully messaged " + USER_SEND + "! #" + str(count))
		else:
			self.sub.logMe("Error! Could not send message to " + USER_SEND, logging.WARNING)

		return bool

	#Load scraper_users.txt file file
	def loadUsers(self):

		sub = self.sub

		with open(sub.scraper_users) as f:
			lines = f.readlines()
			# you may also want to remove whitespace characters like `\n` at the end of each line
			lines = [x.strip() for x in lines]
			#print(lines)

		return lines;