import json
import pytest

from model_bakery import baker
from apps.customer.models import Customer

pytestmark = pytest.mark.django_db

class TestCustomerEndpoints:

    endpoint = '/api/v1/customers/'

    def test_list_all_customers(self, client):
        baker.make(Customer, _quantity=3)
        response = client.get(self.endpoint)
        assert response.status_code == 200
        assert len(json.loads(response.content)) == 3

    def test_create_new_customer(self, client, instance_customer_dict):
        response = client.post(
            self.endpoint,
            data=instance_customer_dict,
            format='json'
        )
        response_dict = json.loads(response.content.decode('utf-8'))
        assert response.status_code == 201
        assert all(item in response_dict.items() for item in instance_customer_dict.items()) == True

    def test_retrieve_one_customer(self, client, object_customer_dict):
        url = f'{self.endpoint}{object_customer_dict["customer_id"]}/'
        response = client.get(url)
        response_dict = json.loads(response.content.decode('utf-8'))
        assert response.status_code == 200
        assert all(item in response_dict.items() for item in object_customer_dict.items()) == True

    def test_update_customer(self, client, instance_customer_dict):
        old_customer = baker.make(Customer)
        url = f'{self.endpoint}{old_customer.customer_id}/'
        response = client.put(
            url,
            instance_customer_dict,
            format='json'
        )
        response_dict = json.loads(response.content.decode('utf-8'))
        assert response.status_code == 200
        assert all(item in response_dict.items() for item in instance_customer_dict.items()) == True

    @pytest.mark.parametrize(
        'field', [
            ('name'), ('code'), ('document'), ('phone'),
            ('email'), ('birth_date'), ('status')
        ]
    )
    def test_partial_update_customer(self, field, client, instance_customer_dict):
        customer = baker.make(Customer)
        valid_field = instance_customer_dict[field]
        url = f'{self.endpoint}{customer.customer_id}/'
        response = client.patch(
            url,
            {field: valid_field},
            format='json'
        )
        assert response.status_code == 200
        assert json.loads(response.content)[field] == valid_field

    def test_delete(self, client):
        customer = baker.make(Customer)
        url = f'{self.endpoint}{customer.customer_id}/'
        response = client.delete(url)
        assert response.status_code == 204
        assert Customer.objects.all().count() == 0


"""Module for ObjDetInstanceLabel"""
from typing import List, Optional, Union

from attr import define, field

from niceml.utilities.boundingboxes.bboxconversion import dict_to_bounding_box
from niceml.utilities.boundingboxes.boundingbox import BoundingBox, bounding_box_from_ullr
from niceml.utilities.imagesize import ImageSize
from niceml.utilities.instancelabeling import InstanceLabel

@define
class ObjDetInstanceLabel(InstanceLabel):  # pylint: disable=too-few-public-methods
    """Label information for one specific bounding box and its prediction score"""

    bounding_box: Union[BoundingBox, None] = field(
        converter=dict_to_bounding_box, default=None
    )
    score: Optional[float] = None
    rotation: int = None

    def calc_iou(self, other: "ObjDetInstanceLabel") -> float:
        """
        Calculates the IOU of two given `ObjDetInstanceLabel`s bounding boxes

        Args:
            other: ObjDetInstanceLabel to calculate the IOU for

        Returns:
            Calculated IOU
        """
        return self.bounding_box.calc_iou(  # pylint: disable=no-member
            other=other.bounding_box
        )

    def to_content_list(self) -> list:
        """
        Creates a list with the contents of the ObjDetInstanceLabel in the following order:
        1. all bounding boxes (separately),
        2. class name,
        3. class index,
        4. prediction score,
        5. rotation
        """
        content_list = list(  # pylint: disable=no-member
            self.bounding_box.get_absolute_ullr()
        )
        content_list += [self.class_name, self.class_index, self.score, self.rotation]
        return content_list

    def scale_label(self, scale_factor: float) -> "ObjDetInstanceLabel":
        """
        Scales an ObjDetInstanceLabel by a given `scale_factor`

        Args:
            scale_factor: Factor to scale the ObjDetInstanceLabel by

        Returns:
            Scaled instance of this ObjDetInstanceLabel"""
        return ObjDetInstanceLabel(
            class_name=self.class_name,
            class_index=self.class_index,
            color=self.color,
            active=self.active,
            bounding_box=self.bounding_box.scale(scale_factor),
            score=self.score,
            rotation=self.rotation,
        )

def obj_instance_factory_from_content_list(
    content_list: list,
) -> ObjDetInstanceLabel:
    """Creates an ObjDetInstanceLabel from a list of values.
    Correct order of 'content_list' required:
    idx 0-3: ullr coordinates,
    idx 4: class_name,
    idx 5: class_idx,
    idx 6: prediction score,
    idx 7: rotation"""
    bbox = bounding_box_from_ullr(*content_list[:4])
    class_name: str = content_list[4]
    class_index: Optional[int] = content_list[5]
    score: Optional[float] = content_list[6]
    rotation: int = content_list[7]
    return ObjDetInstanceLabel(
        class_name=class_name,
        bounding_box=bbox,
        class_index=class_index,
        score=score,
        rotation=rotation,
    )

def dict_to_objdet_instance_label(
    data: List[Union[dict, ObjDetInstanceLabel]]
) -> List[ObjDetInstanceLabel]:
    """Converts a list of dicts to a list of ObjDetInstanceLabels"""
    converted_data: List[ObjDetInstanceLabel] = []
    for entry in data:
        if isinstance(entry, dict):
            converted_data.append(ObjDetInstanceLabel(**entry))
        else:
            converted_data.append(entry)
    return converted_data

@define()
class ObjDetImageLabel:  # pylint: disable=too-few-public-methods
    """Labels of all ObjDetInstanceLabels on an image file"""

    filename: str
    img_size: ImageSize
    labels: List[ObjDetInstanceLabel] = field(converter=dict_to_objdet_instance_label)


"""
TNG Certificates
"""
# (c) 2015-2019 Microchip Technology Inc. and its subsidiaries.
#
# Subject to your compliance with these terms, you may use Microchip software
# and any derivatives exclusively with Microchip products. It is your
# responsibility to comply with third party license terms applicable to your
# use of third party software (including open source software) that may
# accompany Microchip software.
#
# THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
# EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
# WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
# PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT,
# SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE
# OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
# MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
# FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL
# LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED
# THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
# THIS SOFTWARE.

import time
import unicodedata
import re
import sys
from cryptography.hazmat.backends import default_backend
from cryptography import x509
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
from cryptography.utils import int_from_bytes
from cryptoauthlib import *
from common import *

ATCA_SUCCESS = 0x00

def init_device(iface='hid', **kwargs):
    """Initialize CryptoAuthLib for the current device"""
    # Loading cryptoauthlib(python specific)
    load_cryptoauthlib()

    # Get the target default config
    cfg = eval('cfg_ateccx08a_{}_default()'.format(atca_names_map.get(iface)))

    # Set interface parameters
    if kwargs is not None:
        for k, v in kwargs.items():
            icfg = getattr(cfg.cfg, 'atca{}'.format(iface))
            setattr(icfg, k, int(v, 16))

    # Basic Raspberry Pi I2C check
    if 'i2c' == iface and check_if_rpi():
        cfg.cfg.atcai2c.bus = 1

    # Initialize the stack
    assert atcab_init(cfg) == ATCA_SUCCESS

    # Check device type
    info = bytearray(4)
    assert atcab_info(info) == ATCA_SUCCESS
    dev_name = get_device_name(info)
    dev_type = get_device_type_id(dev_name)

    # Reinitialize if the device type doesn't match the default
    if dev_type != cfg.devtype:
        cfg.dev_type = dev_type
        assert atcab_release() == ATCA_SUCCESS
        time.sleep(1)
        assert atcab_init(cfg) == ATCA_SUCCESS

    return dev_name

def get_common_name(name):
    """
    Get the common name string from a distinguished name (RDNSequence)
    """
    for attr in name:
        if attr.oid == x509.oid.NameOID.COMMON_NAME:
            return attr.value
    return None

def tng_certs():
    """Read the TNG certificate chain from the device."""

    certs = []

    print('TNG Root Certificate:')
    root_cert_der_size = AtcaReference(0)
    assert tng_atcacert_root_cert_size(root_cert_der_size) == ATCA_SUCCESS

    root_cert_der = bytearray(root_cert_der_size.value)
    assert tng_atcacert_root_cert(root_cert_der, root_cert_der_size) == ATCA_SUCCESS

    root_cert = x509.load_der_x509_certificate(root_cert_der, default_backend())
    certs.insert(0, root_cert)

    print(get_common_name(root_cert.subject))
    print(root_cert.public_bytes(encoding=Encoding.PEM).decode('utf-8'))

    print('TNG Root Public Key:')
    # Note that we could, of course, pull this from the root certificate above.
    # However, this demonstrates the tng_atcacert_root_public_key() function.
    root_public_key_raw = bytearray(64)
    assert tng_atcacert_root_public_key(root_public_key_raw) == ATCA_SUCCESS

    root_public_key = ec.EllipticCurvePublicNumbers(
        curve=ec.SECP256R1(),
        x=int_from_bytes(root_public_key_raw[0:32], byteorder='big'),
        y=int_from_bytes(root_public_key_raw[32:64], byteorder='big'),
    ).public_key(default_backend())

    # Prove that cert public key and the public key from the func are the same
    cert_spk_der = root_cert.public_key().public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    func_spk_der = root_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    assert cert_spk_der == func_spk_der

    print(root_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.PEM
    ).decode('utf-8'))

    print('Validate Root Certificate:')
    root_public_key.verify(
        signature=root_cert.signature,
        data=root_cert.tbs_certificate_bytes,
        signature_algorithm=ec.ECDSA(root_cert.signature_hash_algorithm)
    )
    print('OK\n')

    print('TNG Signer Certificate:')
    signer_cert_der_size = AtcaReference(0)
    assert tng_atcacert_max_signer_cert_size(signer_cert_der_size) == ATCA_SUCCESS

    signer_cert_der = bytearray(signer_cert_der_size.value)
    assert tng_atcacert_read_signer_cert(signer_cert_der, signer_cert_der_size) == ATCA_SUCCESS

    signer_cert = x509.load_der_x509_certificate(signer_cert_der, default_backend())
    certs.insert(0, signer_cert)

    print(get_common_name(signer_cert.subject))
    print(signer_cert.public_bytes(encoding=Encoding.PEM).decode('utf-8'))

    print('TNG Signer Public Key:')
    # Note that we could, of course, pull this from the signer certificate above.
    # However, this demonstrates the tng_atcacert_signer_public_key() function.
    signer_public_key_raw = bytearray(64)
    assert tng_atcacert_signer_public_key(signer_public_key_raw) == ATCA_SUCCESS

    signer_public_key = ec.EllipticCurvePublicNumbers(
        curve=ec.SECP256R1(),
        x=int_from_bytes(signer_public_key_raw[0:32], byteorder='big'),
        y=int_from_bytes(signer_public_key_raw[32:64], byteorder='big'),
    ).public_key(default_backend())

    # Prove that cert public key and the public key from the func are the same
    cert_spk_der = signer_cert.public_key().public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    func_spk_der = signer_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    assert cert_spk_der == func_spk_der

    print(signer_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.PEM
    ).decode('utf-8'))

    # Note that this is a simple cryptographic validation and does not check
    # any of the actual certificate data (validity dates, extensions, names,
    # etc...)
    print('Validate Signer Certificate:')
    root_public_key.verify(
        signature=signer_cert.signature,
        data=signer_cert.tbs_certificate_bytes,
        signature_algorithm=ec.ECDSA(signer_cert.signature_hash_algorithm)
    )
    print('OK\n')

    print('TNG Device Certificate:')
    device_cert_der_size = AtcaReference(0)
    assert tng_atcacert_max_device_cert_size(device_cert_der_size) == ATCA_SUCCESS

    device_cert_der = bytearray(device_cert_der_size.value)
    assert tng_atcacert_read_device_cert(device_cert_der, device_cert_der_size) == ATCA_SUCCESS

    device_cert = x509.load_der_x509_certificate(device_cert_der, default_backend())
    certs.insert(0, device_cert)

    print(get_common_name(device_cert.subject))
    print(device_cert.public_bytes(encoding=Encoding.PEM).decode('utf-8'))

    print('TNG Device Public Key:')
    # Note that we could, of course, pull this from the device certificate above.
    # However, this demonstrates the tng_atcacert_device_public_key() function.
    device_public_key_raw = bytearray(64)
    assert tng_atcacert_device_public_key(device_public_key_raw, device_cert_der) == ATCA_SUCCESS

    device_public_key = ec.EllipticCurvePublicNumbers(
        curve=ec.SECP256R1(),
        x=int_from_bytes(device_public_key_raw[0:32], byteorder='big'),
        y=int_from_bytes(device_public_key_raw[32:64], byteorder='big'),
    ).public_key(default_backend())

    # Prove that cert public key and the public key from the func are the same
    cert_spk_der = device_cert.public_key().public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    func_spk_der = device_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.DER
    )
    assert cert_spk_der == func_spk_der

    print(device_public_key.public_bytes(
        format=PublicFormat.SubjectPublicKeyInfo,
        encoding=Encoding.PEM
    ).decode('utf-8'))

    # Note that this is a simple cryptographic validation and does not check
    # any of the actual certificate data (validity dates, extensions, names,
    # etc...)
    print('Validate Device Certificate:')
    signer_public_key.verify(
        signature=device_cert.signature,
        data=device_cert.tbs_certificate_bytes,
        signature_algorithm=ec.ECDSA(device_cert.signature_hash_algorithm)
    )
    print('OK\n')

    return certs

def make_valid_filename(s):
    """
    Convert an arbitrary string into one that can be used in an ascii filename.
    """
    if sys.version_info[0] <= 2:
        if not isinstance(s, unicode):
            s = str(s).decode('utf-8')
    else:
         s = str(s)
    # Normalize unicode characters
    s = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore').decode('ascii')
    # Remove non-word and non-whitespace characters
    s = re.sub(r'[^\w\s-]', '', s).strip()
    # Replace repeated whitespace with an underscore
    s = re.sub(r'\s+', '_', s)
    # Replace repeated dashes with a single dash
    s = re.sub(r'-+', '-', s)
    return s

if __name__ == '__main__':
    parser = setup_example_runner(__file__)
    parser.add_argument(
        '-s',
        '--save',
        help='Save TNG certificates to file.',
        action='store_true'
    )
    args = parser.parse_args()

    dev_name = init_device(args.iface, **parse_interface_params(args.params))

    certs = tng_certs()

    if args.save:
        for cert in certs:
            filename = make_valid_filename(get_common_name(cert.subject)) + '.crt'
            with open(filename, 'wb') as f:
                f.write(cert.public_bytes(encoding=Encoding.PEM))


from functools import lru_cache
from typing import Generator
from enum import IntEnum

class AlgSimple():
    '''простые алгоритмы'''

    @lru_cache(maxsize=None)
    def fib_rec(self, n):
        '''Фибоначчи на рекурсии (с мемоизацией)'''
        if n < 2:
            return n
        return self.fib_rec(n-1) + self.fib_rec(n-2)

    def fib_max(self, n: int) -> Generator[int, None, None]:
        '''Самый быстрый Фибоначчи на циклах и генераторах'''
        yield 0
        if n < 2:
            yield 1
        # начальные значения
        last = 0
        next = 1
        for _ in range(1, n):
            old_next = next
            next = last + next
            last = old_next
            yield next

class dnk():
    ''' Нуклеотид(ACGT) - кодон(последов-ть 3 нуклеодитов) - ген'''
    def __init__(self):
        self.Nucleotide = IntEnum('Nucleotide', ('A', 'C', 'G', 'T'))

    def get_gene(self, str_nucl:str) -> list:
        '''Из строки генов -->> последовательность кодонов'''
        self.gene = []
        for i in range(0, len(str_nucl), 3):
            if (i + 2) <= len(str_nucl):
                codon = (
                    self.Nucleotide[str_nucl[i]],
                    self.Nucleotide[str_nucl[i+1]],
                    self.Nucleotide[str_nucl[i+2]]
                )
                self.gene.append(codon)
        return self.gene

    def get_exists_codon(self, codon):
        '''
        Проверить наличие кодона в гене (бинарный поиск)
        в работе можно воспользоваться библиотекой bisect(!)
        NOTE: с постоянной сортировкой вся выгода от бин.поиска теряется (!)
        '''
        low = 0
        high = len(self.gene) - 1
        gene_sort = sorted(self.gene)

        while low <= high:
            mean = int(high / 2)
            if gene_sort[mean] == codon:
                return True
            elif gene_sort[mean] > codon:
                high = mean - 1
            else:
                low = mean + 1
        return False

if __name__ == '__main__':
    str_test = "ACGTGGCTCTCTAACGTACGTACGTACGGGGTTTATATATACCCTAGGACTCCCTTT"
    # alg = AlgSimple()
    d = dnk()
    # для проверки бинарного поиска
    test_codon = (d.Nucleotide.A, d.Nucleotide.C, d.Nucleotide.G)
    d.get_gene(str_test)
    a = 1


import sys
import traceback
import logging

from PySide2 import QtCore, QtWidgets, QtGui

from hyo2.abc.lib.helper import Helper
from hyo2.abc.lib.logging import set_logging
from hyo2.abc.app.app_style import AppStyle
from hyo2.soundspeedmanager import app_info

set_logging(ns_list=["hyo2.abc", "hyo2.soundspeed", "hyo2.soundspeedmanager", "hyo2.soundspeedsettings",
                     "hyo2.surveydatamonitor"])
logger = logging.getLogger(__name__)

def qt_custom_handler(error_type: QtCore.QtMsgType, error_context: QtCore.QMessageLogContext, message: str):

    logger.info("Qt error: %s [%s, %s:%s:%d, v.%s] -> %s"
                % (error_type, error_context.category, error_context.file, error_context.function,
                   error_context.line, error_context.version, message))

    for line in traceback.format_stack():
        logger.debug("- %s" % line.strip())

QtCore.qInstallMessageHandler(qt_custom_handler)

app = None

def gui():
    """Create the application and show the Sound Speed Manager gui"""
    from hyo2.soundspeedmanager.mainwin import MainWin
    global app
    app = QtWidgets.QApplication([])
    app.setStyleSheet(AppStyle.load_stylesheet())

    if Helper.is_script_already_running():
        txt = "The app is already running!"
        logger.warning(txt)
        msg_box = QtWidgets.QMessageBox()
        msg_box.setWindowTitle("Multiple Instances of Sound Speed Manager")
        msg_box.setIconPixmap(QtGui.QPixmap(app_info.app_icon_path).scaled(QtCore.QSize(36, 36)))
        msg_box.setText('%s\n\nDo you want to continue? This might create issues.' % txt)
        msg_box.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
        msg_box.setDefaultButton(QtWidgets.QMessageBox.No)
        reply = msg_box.exec_()
        if reply == QtWidgets.QMessageBox.No:
            sys.exit(app.exit())

    main_win = MainWin()
    sys.excepthook = main_win.exception_hook  # install the exception hook
    main_win.show()
    main_win.do()

    sys.exit(app.exec_())


"""
run.py (lolvelty)
-----------------

Calculates the "lolvelty" novelty score to documents
in Elasticsearch, on a document-by-document basis.
Note that this is a slow procedure, and the bounds of
document "lolvelty" can't be known a priori.
"""

from nesta.core.luigihacks.elasticsearchplus import ElasticsearchPlus
from nesta.packages.novelty.lolvelty import lolvelty
from ast import literal_eval
import os
import boto3
import json
import logging

def run():
    s3_bucket = os.environ["BATCHPAR_bucket"]
    batch_file = os.environ["BATCHPAR_batch_file"]
    count = int(os.environ['BATCHPAR_count'])
    es_index = os.environ['BATCHPAR_index']
    es_host = os.environ['BATCHPAR_outinfo']
    es_port = int(os.environ['BATCHPAR_out_port'])
    es_index = os.environ['BATCHPAR_index']
    es_type = os.environ['BATCHPAR_out_type']
    entity_type = os.environ["BATCHPAR_entity_type"]
    aws_auth_region = os.environ["BATCHPAR_aws_auth_region"]
    fields = literal_eval(os.environ["BATCHPAR_fields"])
    score_field = os.environ["BATCHPAR_score_field"]
    test = literal_eval(os.environ["BATCHPAR_test"])

    # Extract all document ids in this chunk
    s3 = boto3.resource('s3')
    ids_obj = s3.Object(s3_bucket, batch_file)
    logging.info(f'Getting document ids...')
    all_doc_ids = json.loads(ids_obj.get()['Body']._raw_stream.read())
    logging.info(f'Got {len(all_doc_ids)} document ids')

    # Set up Elasticsearch
    es = ElasticsearchPlus(hosts=es_host,
                           port=es_port,
                           aws_auth_region=aws_auth_region,
                           no_commit=("AWSBATCHTEST" in os.environ),
                           entity_type=entity_type,
                           do_sort=False)

    min_match = 0.3 if not test else 0.05
    for doc_id in all_doc_ids:
        # Check whether the doc exists with the correct fields
        existing = es.get(es_index, doc_type=es_type,
                          id=doc_id)['_source']
        # Get the score
        score = None
        if any(f in existing for f in fields):
            score = lolvelty(es, es_index, doc_id,
                             fields, total=count,
                             minimum_should_match=min_match)
        # Merge existing info into new doc
        doc = {**existing}
        doc[score_field] = score
        es.index(index=es_index, doc_type=es_type,
                 id=doc_id, body=doc)

if __name__ == "__main__":

    log_level = logging.INFO
    if 'BATCHPAR_outinfo' not in os.environ:
        logging.getLogger('boto3').setLevel(logging.CRITICAL)
        logging.getLogger('botocore').setLevel(logging.CRITICAL)
        logging.getLogger('s3transfer').setLevel(logging.CRITICAL\
)
        logging.getLogger('urllib3').setLevel(logging.CRITICAL)
        log_level = logging.INFO
        pars = {'batch_file': ('ArxivLolveltyTask-2019-08-19'
                               '-True-15662373053397586.json'),
                'config': 'mysqldb.config',
                'bucket': 'nesta-production-intermediate',
                'done': 'False',
                'outinfo': ('https://search-arxlive'
                            '-t2brq66muzxag44zwmrcfrlmq4.'
                            'eu-west-2.es.amazonaws.com'),
                'count': '6000',
                'out_port': '443',
                'fields': "('textBody_abstract_article',)",
                'score_field': "metric_novelty_article",
                'index': 'arxiv_dev',
                'out_type': '_doc',
                'aws_auth_region': 'eu-west-2',
                'entity_type': 'article',
                'test': 'True',
                'routine_id': 'DUMMYTEST'}
        for k, v in pars.items():
            os.environ[f'BATCHPAR_{k}'] = v

    log_stream_handler = logging.StreamHandler()
    logging.basicConfig(handlers=[log_stream_handler, ],
                        level=logging.INFO,
                        format="%(asctime)s:%(levelname)s:%(message)s")
    run()


import numpy as np
import re

def natural_sort(l):
    """ Sort list of strings in natural order"""
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
    return sorted(l, key = alphanum_key)

def batch_generator(items, batch_size=128, equal_size=False):
    """Generate batches over the input data.
    If equal_size is True, generator will stop when left data size will be less than batch_size."""
    i = 0
    stop = batch_size if equal_size else 1

    while True:
        fs = items[i:i+batch_size]
        i += batch_size
        if len(fs) < stop:
            raise StopIteration
        yield fs

def norm_values(X, x_min=None, x_max=None):
    """Normalize values from zero to one"""
    x_min = x_min if x_min is not None else np.min(X)
    x_max = x_max if x_max is not None else np.max(X)
    return (X - x_min) / (x_max - x_min + 1e-18)


# python3
import urllib.request
import json
import codecs
import xml.etree.cElementTree as ET
import datetime
import time
import email.utils
import sys

def datetime_to_rfc822(dt):
    return email.utils.formatdate(time.mktime(dt.timetuple()))
def iso8601_to_rfc822(chain):
    dt = datetime.datetime.strptime(chain.replace(":", ""), "%Y-%m-%dT%H%M%S%z")
    return datetime_to_rfc822(dt)

def createRSS(numElts, out=sys.stdout.buffer):
    base_path = "https://www.arretsurimages.net"
    base_api = "https://api.arretsurimages.net/api/public"
    url = base_api + "/search?q="

    req = urllib.request.Request(url, headers={"range": "objects 0-%d" % (numElts-1)})
    with urllib.request.urlopen(req) as f:
        obj = json.load(codecs.getreader("utf-8")(f))
        hits = obj["hits"]["hits"]

    root = ET.Element("rss", version="2.0")
    channel = ET.SubElement(root, "channel")
    ET.SubElement(channel, "title").text = "Arrêt sur Images"
    ET.SubElement(channel, "link").text = base_path
    ET.SubElement(channel, "lastBuildDate").text = datetime_to_rfc822(datetime.datetime.now())

    for hit in hits:
        item = ET.SubElement(channel, "item")
        ET.SubElement(item, "title").text = hit["title"]
        ET.SubElement(item, "pubDate").text = iso8601_to_rfc822(hit["updated_at"]) # "published_at"?
        ET.SubElement(item, "link").text = "%s/%s" % (base_path, hit["path"])
        img = '
' % (base_api, hit["thumbnail"]["slug"])
        ET.SubElement(item, "description").text = '%s
%s%s' % (hit["lead"], hit["tease"], img)

    ET.ElementTree(root).write(out)

if __name__ == "__main__":
    if len(sys.argv) == 1:
        createRSS(30)
    else:
        createRSS(30, out=sys.argv[1])


from airflow import DAG

from datetime import timedelta

import pendulum

from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator,BranchPythonOperator
import random

def task_1():
    list_1 = []
    for i in range(5):
        list_1.append(random.randrange(1,100))  #random no between 1 and 99 (100 is excluded)
    return list_1

def check(ti):
    list_2 = ti.xcom_pull(task_ids='task_1')
    print('length of list',len(list_2)) #check in logs of a task
    if(len(list_2)==5):
        return 'success' #define this task
    else:
        return 'fail'    #define this task

with DAG(dag_id='dag_2',description='second dag',
        start_date=pendulum.datetime(2022,6,11,17,9,tz='Asia/Calcutta'),
        schedule_interval=timedelta(minutes=2),
        catchup=False,
        default_args={ #can override in task
            'depends_on_past': False,
            'retries':1,
            'retry_delay': timedelta(seconds=3),

        }
        ) as dag:

        t1 = PythonOperator(
            task_id='task_1',
            python_callable=task_1,

        )

        t2 = BranchPythonOperator(
            task_id= "task_2",
            python_callable=check
        )

        t3 = BashOperator(
            task_id ='success',
            bash_command='echo success'
        )

        t4 = BashOperator(
            task_id = 'fail',
            bash_command='echo fail'
        )

        t1 >> t2 >> [t3,t4]


import numpy as np
import cv2, threading, re, sys, logging
from enum import Enum
from abc import ABC, abstractmethod
from PIL import ImageGrab
from imutils.object_detection import non_max_suppression
from timeit import default_timer as timer

from typing import Dict, List

logger = logging.getLogger()

PATTERN_SCALE = 1 # reduce image size for faster processing
BORDER_THRESH = 0.8 # if nor scaling, 0.8 is enough. must try to get this right
PIECE_THRESH = 0.8
LASTMOVE_THRESH = 0.7

# PATTERN_PATH = './patterns'
try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    PATTERN_PATH = f'{sys._MEIPASS}/patterns'
except Exception:
    PATTERN_PATH = './patterns'

GRID_WIDTH = 9
GRID_HEIGHT = 10
PIECE_NAME = 'King Advisor Elephant Rook Cannon Horse Pawn'.split()

PIECE_SYMBOL = {
    'King': 'K',
    'Advisor': 'A',
    'Elephant': 'B',
    'Rook': 'R',
    'Cannon': 'C',
    'Horse': 'N',
    'Pawn': 'P'
}

MOVE_PTN = re.compile(r'([a-z])(\d+)([a-z])(\d+)')

FILE2NUM = dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9)

def load_gray(fileName):
    img = cv2.imread(f'{PATTERN_PATH}/{fileName}.png', 0)
    return cv2.resize(img, (0,0), fx=PATTERN_SCALE, fy=PATTERN_SCALE)

def get_imgsize(fileName):
    # ptn = cv2.imread(f'{PATTERN_PATH}/{fileName}.png', 0)
    img = load_gray(fileName)
    return np.array(img.shape[::-1])

def showimg(img):
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def load_img_mask(fileName):
    # gray = cv2.imread(f'{PATTERN_PATH}/{fileName}.png', 0)
    gray = load_gray(fileName)
    _, mask = cv2.threshold(gray,10,255,cv2.THRESH_BINARY)
    return gray, mask

def find_pattern_verbose(fileName, thresh=0.8):
    largeImg = cv2.imread(f'{PATTERN_PATH}/ex2.png')
    temp_gray = cv2.imread(f'{PATTERN_PATH}/{fileName}.png',0)

    # save the image dimensions
    W, H = temp_gray.shape[::-1]

    # Converting them to grayscale
    img_gray = cv2.cvtColor(largeImg,
                            cv2.COLOR_BGR2GRAY)

    # Passing the image to matchTemplate method
    match = cv2.matchTemplate(img_gray, temp_gray,  cv2.TM_CCOEFF_NORMED)

    # return np.where(match >= thresh)

    # Select rectangles with
    # confidence greater than threshold
    (y_points, x_points) = np.where(match >= thresh)
    boxes = list()
    for (x, y) in zip(x_points, y_points):
        boxes.append((x, y, x + W, y + H))

    # apply non-maxima suppression to the rectangles
    # this will create a single bounding box
    boxes = non_max_suppression(np.array(boxes))

    # loop over the final bounding boxes
    for (x1, y1, x2, y2) in boxes:

        # draw the bounding box on the image
        cv2.rectangle(largeImg, (x1, y1), (x2, y2),
                    (255, 0, 0), 3)

    # Show the template and the final output
    cv2.imshow("After NMS", largeImg)
    cv2.waitKey(0)

    # destroy all the windows
    # manually to be on the safe side
    cv2.destroyAllWindows()

def find_pattern(board_gray, piece_gray, thresh=0.5, center=1, display=0, mask=None):
    W, H = piece_gray.shape[::-1]
    # Passing the image to matchTemplate method
    match = cv2.matchTemplate(board_gray, piece_gray, cv2.TM_CCOEFF_NORMED, mask=mask)

    # reduce the matching overlap
    (y_points, x_points) = np.where(match >= thresh)
    boxes = list()
    for (x, y) in zip(x_points, y_points):
        boxes.append((x, y, x + W, y + H))
    boxes = non_max_suppression(np.array(boxes))
    if display==1:
        for (x1, y1, x2, y2) in boxes:
            # draw the bounding box on the image
            cv2.rectangle(board_gray, (x1, y1), (x2, y2),
                        (0, 0, 0), 2)
        showimg(board_gray)

    coords = np.array( [ (x[0], x[1]) for x in boxes ] )

    if center==1 and len(coords) > 0:
        coords = np.add(coords, np.divide((W,H), 2))

    return coords

# def screen_cap_pyautogui():
#     img = pyautogui.screenshot()
#     return cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

def screen_cap_forscale(region=None):
    # screencap actually not take much time compare ti matching, so make it simple...
    img = ImageGrab.grab(bbox=region)
    img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
    img = cv2.resize(img, (0,0), fx=PATTERN_SCALE, fy=PATTERN_SCALE)
    if region is not None:
        img = img[ region[1]:region[3] , region[0]:region[2] ]
    return img

# This only work for SCALE=1
def screen_cap(region=None):
    # if region is None:
    #     logger.info(f'Capture the whole screen')
    # img = ImageGrab.grab(bbox=region)
    # screencap actually not take much time compare ti matching, so make it simple...
    img = ImageGrab.grab(bbox=region)
    img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
    # img = cv2.resize(img, (0,0), fx=PATTERN_SCALE, fy=PATTERN_SCALE)
    # if region is not None:
    #     img = img[ region[1]:region[3] , region[0]:region[2] ]
    return img

def create_mask(img_rgb):
    return np.array(np.array([ [0 if y.sum() < 10 else 255 for y in x] for x in img_rgb ])).astype(np.uint8)

class Side(Enum):
    Unknow = 0
    White = 1
    Black = 2

    @property
    def opponent(self):
        if self == Side.Unknow:
            return Side.Unknow
        if self == Side.Black:
            return Side.White
        return Side.Black

    @property
    def fen(self):
        if self == Side.White:
            return 'w'
        if self == Side.Black:
            return 'b'
        raise Exception('should not be called')

class MonitorFatal(Enum):
    NoBoardFound = 1

class BoardMonitorListener(ABC):
    @abstractmethod
    def on_monitor_fatal(self, type: MonitorFatal):
        pass
    @abstractmethod
    def on_monitor_error(self, msg: str):
        pass
    @abstractmethod
    def on_board_updated(self, fen: str, moveSide: Side, lastmovePosition):
        pass

class PiecePattern:
    def __init__(self, side: Side, name: str) -> None:
        self.side = side
        self.name = name
        self.fullName = f'{side.name}{name}'
        self.symbol = PIECE_SYMBOL[name]
        if side==Side.Black:
            self.symbol = self.symbol.lower()
        self.gray = load_gray(self.fullName)

class BoardMonitor:
    playSize = get_imgsize('play_area')             # The captured image of playing field only. This is used to calculate the grid size
    pieceSize = get_imgsize('BlackKing')            # Any image that have the size of piece to search
    playStart = get_imgsize('corrner2playfield')    # To get the offset from where Border is detected, to the actual playing field
    gridSize = np.divide(playSize, (GRID_WIDTH-1, GRID_HEIGHT-1))

    borderPatternGray, borderPatternMask = load_img_mask('border')  # The border pattern. Make sure that the tool can recognize this given any active possition
                                                                    # NOTE: when the lastmove is in the conner, the lastmove indicator may "peek" into border pattern
                                                                    #       make sure to mask it properly
    borderSize = get_imgsize('border')
    lastMovePatternGray, lastMovePatternMsk = load_img_mask('lastMove')

    positions = None
    piecePatterns: Dict[str, PiecePattern] = {}

    idleCnt = {}
    lastBoardStr = ''
    lastScanRegion = None

    mySide = Side.White
    lastMoveSide = Side.Unknow
    lastMovePosition = None
    forceMyMoveNext = False

    eventListeners: List[BoardMonitorListener] = []
    stopPolling = False
    pollingStopped = threading.Event()
    engine_thread = None

    fatalCnt: Dict[MonitorFatal, int] = {}

    def __init__(self) -> None:
        self.clear_board()
        for side in [Side.Black, Side.White]:
            for name in PIECE_NAME:
                p = PiecePattern(side,name)
                self.piecePatterns[p.fullName] = p

        # maybe this help speedup pattern matching
        self.patternOffset = [[np.multiply(self.gridSize, (i,j)) + self.playStart - np.divide(self.gridSize,2)
                               for i in range(GRID_WIDTH)] for j in range(GRID_HEIGHT)] # [Y,X]

    def is_myside(self, side:Side):
        return False if side==Side.Unknow else side==self.mySide

    def add_event_listener(self, listener: BoardMonitorListener):
        self.eventListeners.append(listener)

    def clear_board(self):
        self.positions = [['.' for i in range(GRID_WIDTH)] for j in range(GRID_HEIGHT)] # It's [Y,X] in this array

    def crop_image(self, board_gray):
        # board_gray = cv2.cvtColor(board_color, cv2.COLOR_BGR2GRAY)
        # upleft_ptn = cv2.imread(f'{PATTERN_PATH}/upleft.png', 0)
        borderPos = find_pattern(board_gray, self.borderPatternGray, mask=self.borderPatternMask, thresh=BORDER_THRESH, center=0)
        if len(borderPos) != 1:
            self.lastScanRegion = None
            return None

        upleftPos = borderPos[0]
        boardStart = upleftPos # + self.upleftSize - self.pieceSize
        boardEnd =  upleftPos + self.borderSize #+ self.upleftSize + self.playSize + self.pieceSize

        board_crop = board_gray[ boardStart[1]:boardEnd[1] , boardStart[0]:boardEnd[0] ]
        if self.lastScanRegion is None:
            self.lastScanRegion = (upleftPos[0], upleftPos[1], boardEnd[0], boardEnd[1])
        elif upleftPos[0] != 0 or upleftPos[1] != 0: # Not all zero
            self.lastScanRegion = None

        return board_crop

    # def scan_piece_name(self, board_gray, pieceName, display=0):
    #     piece_gray = cv2.imread(f'{PATTERN_PATH}/{pieceName}.png',0)
    #     coord = find_pattern(board_gray, piece_gray, center=1, display=display)
    #     if len(coord)==0:
    #         return coord
    #     return np.round((coord - self.playStart) / self.gridSize).astype(int)

    def scan_piece(self, board_gray, piece_gray, display=0):
        coord = find_pattern(board_gray, piece_gray, thresh=PIECE_THRESH, center=1, display=display)
        if len(coord)==0:
            return coord
        return np.round((coord - self.playStart) / self.gridSize).astype(int)

    def scan_lastmove(self, board_gray, display=0):
        coord = find_pattern(board_gray, self.lastMovePatternGray, thresh=LASTMOVE_THRESH,
                            mask=self.lastMovePatternMsk, center=1, display=display)
        if len(coord)==0:
            # self.send_msg('** Error: Last move position not found')
            return None
        coord = coord[0]
        return np.round((coord - self.playStart) / self.gridSize).astype(int)

    def send_msg(self, msg):
        try:
            self.idleCnt[msg] += 1
        except KeyError:
            self.idleCnt[msg] = 1
        if (self.idleCnt[msg] == 1):
            logger.info(msg)
            for l in self.eventListeners:
                l.on_monitor_error(msg)
        if (self.idleCnt[msg] > 10):
            self.idleCnt[msg] = 0

    def send_board_update_event(self, fen: str, moveSide: Side):
        if moveSide == Side.Unknow and self.forceMyMoveNext:
            moveSide = self.mySide.opponent
            logger.warning(f'Force moveside {moveSide}')
        if self.forceMyMoveNext or fen != self.lastBoardStr or moveSide != self.lastMoveSide:
            for l in self.eventListeners:
                l.on_board_updated(fen, moveSide, self.lastMovePosition)
        self.forceMyMoveNext = False
        self.lastBoardStr = fen
        self.lastMoveSide = moveSide

    def send_board_fatal(self, type: MonitorFatal):
        try:
            self.fatalCnt[type] += 1
        except KeyError:
            self.fatalCnt[type] = 1
        if self.fatalCnt[type] > 4:
            self.fatalCnt[type] = 0
            for l in self.eventListeners:
                l.on_monitor_fatal(type)

    # a little bit faster, but still slow thouigh...
    def scan_image(self, board_gray):
        board = self.crop_image(board_gray)
        self.clear_board()

        if board is None:
            # self.send_msg("** Error : No Board found.")
            self.send_board_fatal(MonitorFatal.NoBoardFound)
            self.lastBoardStr = ''
            return
        self.fatalCnt[MonitorFatal.NoBoardFound] = 0

        self.lastMovePosition = self.scan_lastmove(board)
        moveSide = Side.Unknow
        kingPos = {Side.Black: None, Side.White: None}

        for row in range(GRID_HEIGHT):
            for col in range(GRID_WIDTH):
                ptnOffs = self.patternOffset[row][col]
                s = ptnOffs.astype(int)
                e = (ptnOffs + self.gridSize).astype(int)
                boardLoc = board[ s[1]:e[1], s[0]:e[0] ]
                for _, piece in self.piecePatterns.items():
                    res = find_pattern(boardLoc, piece.gray, thresh=PIECE_THRESH)
                    if len(res) > 0:
                        # found here
                        if np.array_equal(self.lastMovePosition, (col,row)):
                            moveSide = piece.side
                        curp = self.positions[row][col]
                        if (curp != '.'):
                            logger.warning(f'Duplicated piece found at {row}:{col} {curp} -> {piece.symbol}')
                        self.positions[row][col] = piece.symbol
                        if piece.name == 'King':
                            kingPos[piece.side] = (col,row)
                        break

        if kingPos[Side.Black] is None or kingPos[Side.White] is None:
            logger.warning(f'KING is not found in both sides')
            return

        self.mySide = Side.White if kingPos[Side.White][1] > kingPos[Side.Black][1] else Side.Black
        # if self.lastMoveSide == 'Unknown':
        #     self.send_msg(f'** Error: move side unkown, assum my side move')
        #     self.lastMoveSide = self.mySide

        self.send_board_update_event(self.get_fen(), moveSide)

    # def scan_image_full(self, board_gray):
    #     board = self.crop_image(board_gray)
    #     self.clear_board()

    #     if board is None:
    #         self.send_msg("** Error : No Board found.")
    #         self.lastBoardStr = ''
    #         return

    #     self.lastMovePosition = self.scan_lastmove(board)
    #     self.lastMoveSide = 'Unknown'
    #     kingPos = {'Black': None, 'White': None}

    #     for _, piece in self.piecePatterns.items():
    #         for pos in self.scan_piece(board, piece.gray):
    #             if np.array_equal(pos, self.lastMovePosition):
    #                 self.lastMoveSide = piece.side
    #             curp = self.positions[pos[1]][pos[0]]
    #             if (curp != '.'):
    #                 logger.warning(f'** Error: Duplicated piece found at {pos} {curp} -> {piece.symbol}')
    #             self.positions[pos[1]][pos[0]] = piece.symbol
    #             if piece.name == 'King':
    #                 kingPos[piece.side] = pos

    #     if kingPos['Black'] is None or kingPos['White'] is None:
    #         logger.warning(f'** Error: KING is not found in both sides')
    #         return

    #     self.mySide = 'White' if kingPos['White'][1] > kingPos['Black'][1] else 'Black'

    #     self.send_board_update_event()

    def board_str(self):
        r = f'lastMove={self.lastMoveSide} {self.lastMovePosition} me={self.mySide}\n'
        for row in self.positions:
            r += '|' + ' '.join(row) + '|\n'
        return r

    def get_fen(self):
        # Start for highest rank
        fens = []
        rotate = self.mySide==Side.Black
        def addfen(fen, sym):
            spaceSym = f'{"" if spacecnt==0 else spacecnt}'
            if rotate:
                fen = sym + spaceSym + fen
                if sym=='':
                    fens.insert(0,fen)
            else:
                fen = fen + spaceSym + sym
                if sym=='':
                    fens.append(fen)
            return fen
        for row in range(GRID_HEIGHT): # we save highest rank at 0
            fen = ''
            spacecnt = 0
            for col in range(GRID_WIDTH):
                p = self.positions[row][col]
                if p == '.':
                    spacecnt += 1
                else:
                    fen = addfen(fen, p)
                    spacecnt = 0
            fen = addfen(fen, '')
        return f'{"/".join(fens)}'

    def move_parse(self, mv):
        m = MOVE_PTN.match(mv)
        sx, sy = FILE2NUM[m.group(1)], int(m.group(2))
        ex, ey = FILE2NUM[m.group(3)], int(m.group(4))
        if self.mySide==Side.Black:
            return [ [GRID_WIDTH-sx, sy-1], [GRID_WIDTH-ex, ey-1] ]
        else:
            return [ [sx-1, GRID_HEIGHT-sy], [ex-1, GRID_HEIGHT-ey] ]

    def screen_check(self):
        # start = timer()
        img = screen_cap(self.lastScanRegion)
        self.scan_image(img)
        # end = timer()
        # logger.info(end-start)

    def start(self, delaySecond=0.1):
        self.stop()

        def polling():
            import time
            logger.info('ScreenMonitor started')
            while not self.stopPolling:
                try:
                    self.screen_check()
                except Exception as e:
                    logger.warning(f'screen check failed {e.args}')
                    break
                time.sleep(delaySecond)
            self.pollingStopped.set()
            self.engine_thread = None
            logger.warning('ScreenMonitor stopped')
        self.engine_thread = threading.Thread(target=polling, daemon=True)
        self.engine_thread.start()

    def stop(self):
        if self.engine_thread and self.engine_thread.is_alive():
            logger.info('Stopping monitor thread...')
            self.stopPolling = True
            self.pollingStopped.wait()
            self.pollingStopped.clear()
            self.stopPolling = False
        self.clear_board()

    # def timeit():
    #     board_gray = cv2.imread(f'{PATTERN_PATH}/ex2.png',0)
    #     start = timer()
    #     pos = find_pieces(board_gray)
    #     end = timer()
    #     print_board(pos)
    #     logger.info(end-start)


from CutsceneClass import *
from sounds import *

intro = []

scene = CutsceneClass()
scene.filename = "INTRO:WESTWOOD.WSA"
scene.palette = "INTRO:WESTWOOD.PAL"
scene.fadeOut = True
scene.song = 0
scene.fps = 5.7
scene.hold = 20
intro.append(scene)

scene = CutsceneClass()
scene.fadeOut = True
scene.hold = 15
scene.fps = 7.3
scene.text = [ (0, "and") ]
scene.textPosition = (0,0)
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:VIRGIN.CPS"
scene.fadeOut = True
scene.fps = 7.7
scene.hold = 15
intro.append(scene)

scene = CutsceneClass()
scene.fadeOut = True
scene.hold = 10
scene.fps = 7.2
scene.song = 1
scene.text = [(0, 1)]
scene.textPosition = (0,0)
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO1.WSA"
scene.fps = 9.3
scene.fadeOut = True
scene.hold = 35
scene.loop = (1,1,0,20)
scene.sound = {
        70 : (Intro_Dune()),
        85 : (Intro_TheBuilding(), Intro_OfADynasty())
        }
scene.text = [(70, "a.k.a. Doon Lunacy")]
scene.textPosition = (0,30)
scene.animPosition = (0, -100)
scene.textFadeIn = False
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO2.WSA"
scene.fps = 5.4
scene.fadeIn = True
scene.fadeOut = True
scene.hold = 20
scene.sound = {
        10 : (Intro_ThePlanetArrakis(), Intro_KnownAsDune())
        }
scene.text = [(10, 3)]
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO3.WSA"
scene.fps = 5.4
fade_in = True
scene.fadeOut = True
scene.hold = 35
scene.text = [(0, 4), (33, 5)]
scene.sound = {
        0 : (Intro_LandOfSand()),
        33 : (Intro_Home(), Intro_OfTheSpice(), Intro_Melange())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO9.WSA"
scene.fps = 5.4
fade_in = True
scene.fadeOut = True
scene.hold = 2
scene.text = [(0, 6), (32, 7)]
scene.sound = {
        0 : (Intro_TheSpice(), Intro_Controls(), Intro_TheEmpire()),
        18 : (Intro_Clank()),
        32 : (Intro_WhoEver(), Intro_ControlsDune(), Intro_ControlsTheSpice()),
        53 : (Intro_Brakes_2p()),
        61 : (Intro_Clank())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO10.WSA"
scene.fps = 3.0
scene.hold = 25
scene.text = [(0, 8)]
scene.sound = {
        0 : (Intro_TheEmperor(), Intro_HasProposedAChallenge(), Intro_ToEachOfTheHouses())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO11.WSA"
scene.fadeOut = True
scene.fps = 8.6
scene.text = [(0, 9), (61, 10), (83, 11)]
scene.textColor = 208
scene.loop = (44, 0, 0, 16)
scene.sound = {
        0 : (Intro_TheHouse(), Intro_ThatProduces(), Intro_TheMostSpice(), Intro_WillControlDune()),
        61 : (Intro_ThereAreNoSet(), Intro_Territories()),
        83 : (Intro_AndNo(), Intro_RulesOfEngagement())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO4.WSA"
scene.fps = 4.1
scene.fadeOut = True
scene.text = [(11, 12)]
scene.sound = {
        11 : (Intro_VastArmies(), Intro_HasArrived())
        }
intro.append(scene)

scene = CutsceneClass()
fade_in = True
scene.fps = 5.8
scene.hold = 38
scene.text = [(0, 13)]
scene.textPosition = (0,0)
scene.textFadeIn = False
scene.sound = {
        0 : (Intro_AndNow(), Intro_3Houses(), Intro_ForControl(), Intro_OfDune())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO6.WSA"
scene.fps = 10.8
scene.fadeOut = True
scene.text = [(0, 14)]
scene.sound = {
        0  : (Intro_TheNobleAtreides()),
        31 : (Intro_Glass()),
        32 : (Intro_Glass()),
        33 : (Intro_Glass()),
        63 : (Intro_Glass()),
        64 : (Intro_Glass()),
        65 : (Intro_Glass())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO7A.WSA"
scene.fps = 4.4
scene.text = [(0, 15)]
scene.sound = {
        0  : (Intro_TheInsideous(),  Intro_Ordos()),
        2  : (Intro_Missile_8()),
        7  : (Intro_Missile_8()),
        26 : (Intro_Missile_8())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO7B.WSA"
scene.fps = 7.9
scene.fadeOut = True
scene.continuation = True
scene.hold = 5
scene.sound = {
        10 : (Intro_Missile_8())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO8A.WSA"
scene.fps = 4.0
scene.text = [(0, 16)]
scene.sound = {
        0 : (Intro_AndThe(), Intro_EvilHarkonnen()),
        5 : (Sound_Gun()),
        8 : (Sound_Gun())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO8B.WSA"
scene.fps = 15.0
scene.continuation = True
scene.sound = {
        1 : (Sound_Gun())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO8C.WSA"
scene.fps = 6.4
scene.fadeOut = True
continuation = True
scene.sound = {
        16 : (Sound_ExplosionSmall()),
        25 : (Sound_ExplosionMedium())
        }
intro.append(scene)

scene = CutsceneClass()
scene.filename = "INTRO:INTRO5.WSA"
scene.fps = 4.6
scene.fadeOut = True
scene.loop = (3, 0, 15, 0)
scene.text = [(0, 17)]
scene.sound = {
        0 : (Intro_OnlyOneHouse(), Intro_WillPrevail())
        }
intro.append(scene)

scene = CutsceneClass()
scene.fadeOut = True
scene.fps = 4.9
scene.hold = 22
scene.text = [(0, 18)]
scene.textPosition = (0,0)
scene.textFadeIn = False
scene.sound = {
        0 : (Intro_Your(), Intro_BattleForDune(), Intro_Begins())
        }
intro.append(scene)

scene = CutsceneClass()
scene.fadeOut = True
scene.hold = 50
scene.text = [(0, 19)]
scene.textPosition = (0,0)
scene.textFadeIn = False
scene.sound = {
        0 : (Intro_Now())
        }
intro.append(scene)

# vim:ts=4:sw=4:et


from urllib.parse import parse_qs
from uuid import uuid1
import requirements
import lib.dynamo as dynamo
import lib.backend as backend
from jinja2 import Environment, FileSystemLoader
from decimal import Decimal
import boto3
import json
import os

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ["SITE_TABLE_NAME"])
cors_url = "http://"+os.environ['SITE_URL'];

def register(event, context):
    """
    /registerにpostされたnameとurlをDBに登録し
    /にリダイレクトする
    """
    print(event)
    body = json.loads(event["body"])
    # siteの定義
    site = {
        "id": str(uuid1()),
        "name": body['name'],
        "url": body['url'],
        "code": backend.probe(body['url'])
    }

    dynamo.put_site(site)

    response = {
        "statusCode": 302,
        "headers": {
            'Location': './',
            "Access-Control-Allow-Origin": cors_url,
            "Access-Control-Allow-Credentials": True,
            'Content-Type': 'application/json',
            "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with",
            "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS"
         },
        "body":""
    }
    print(response)
    return response

def remove(event, context):
    """
    /removeにpostされたidをremove_site()に渡し
    /にリダイレクトする
    """
    body = json.loads(event["body"])

    dynamo.remove_site(body["id"])

    response = {
        "statusCode": 302,
        "headers": {
            'Location': './',
            "Access-Control-Allow-Origin": cors_url,
            "Access-Control-Allow-Credentials": True,
            'Content-Type': 'application/json'
        },
        "body":""
    }

    return response

def get_site(event, context):
    data = table.scan()
    json_str = json.dumps(data["Items"],default=decimal_default_proc)
    print(cors_url)
    response = {
       "statusCode":200,
        "headers": {
            "Access-Control-Allow-Origin": cors_url,
			"Access-Control-Allow-Credentials": True,
			'Content-Type': 'application/json'
        },
       "body" : json_str
    }

    return response

def decimal_default_proc(obj):
    if isinstance(obj, Decimal):
        return float(obj)
    raise TypeError


import numpy as np
import matplotlib.pyplot as plt

import openmc
import openmc.mgxs as mgxs

#############################
# Multigroup cross sections #
#############################

groups = mgxs.EnergyGroups(group_edges=[0., 1.0e2, 2.0e7])

xs_source = openmc.XSdata('source', groups)
xs_source.order = 1
xs_source.set_total([1.0, 2.0])
xs_source.set_scatter_matrix([[[0.9, 0.1], [0.05, 0.0]],
                              [[0.05, 0.0], [0.8, 0.0]]])
xs_source.set_absorption([0.05, 1.15])
xs_source.set_nu_fission([0.0, 1.0])
xs_source.set_chi([1.0, 0.0])
# xs_source.set_scatter_matrix([[[0.0, 0.0], [0.0, 0.0]],
#                               [[0.0, 0.0], [0.0, 0.0]]])
# xs_source.set_absorption([1.0, 2.0])
# xs_source.set_nu_fission([0.0, 0.0])
# xs_source.set_chi([1.0, 0.0])

xs_thin = openmc.XSdata('thin', groups)
xs_thin.order = 1
xs_thin.set_total([0.5, 1.0])
xs_thin.set_scatter_matrix([[[0.05, 0.0], [0.45, 0.1]],
                            [[0.0, 0.0], [1.0, 0.01]]])
xs_thin.set_absorption([0.0, 0.0])
xs_thin.set_nu_fission([0.0, 0.0])
xs_thin.set_chi([0.0, 0.0])
# xs_thin.set_scatter_matrix([[[0.0, 0.0], [0.0, 0.0]],
#                               [[0.0, 0.0], [0.0, 0.0]]])
# xs_thin.set_absorption([0.5, 1.0])
# xs_thin.set_nu_fission([0.0, 0.0])
# xs_thin.set_chi([1.0, 0.0])

xs_thick = openmc.XSdata('thick', groups)
xs_thick.order = 1
xs_thick.set_total([0.5, 10.0])
xs_thick.set_scatter_matrix([[[0.0, 0.0], [0.0, 0.0]],
                             [[0.0, 0.0], [0.0, 0.0]]])
xs_thick.set_absorption([0.5, 10.0])
xs_thick.set_nu_fission([0.0, 0.0])
xs_thick.set_chi([0.0, 0.0])

mg_cross_sections_file = openmc.MGXSLibrary(groups)
mg_cross_sections_file.add_xsdatas([xs_source, xs_thin, xs_thick])
mg_cross_sections_file.export_to_hdf5()

#############
# Materials #
#############

mac_source = openmc.Macroscopic('source')
m_source = openmc.Material(material_id=1, name='source')
m_source.set_density('macro', 1.0)
m_source.add_macroscopic(mac_source)

mac_thin = openmc.Macroscopic('thin')
m_thin = openmc.Material(material_id=2, name='thin')
m_thin.set_density('macro', 1.0)
m_thin.add_macroscopic(mac_thin)

mac_thick = openmc.Macroscopic('thick')
m_thick = openmc.Material(material_id=3, name='thick')
m_thick.set_density('macro', 1.0)
m_thick.add_macroscopic(mac_thick)

materials_file = openmc.Materials([m_source, m_thin, m_thick])
materials_file.cross_sections = "./mgxs.h5"
materials_file.export_to_xml()

############
# Geometry #
############

x_pos = [0.0, 0.5, 3.0, 4.0]
inf_dist = 10000.0

s_x1 = openmc.XPlane(surface_id=1, x0=x_pos[0], name='x1')
s_x2 = openmc.XPlane(surface_id=2, x0=x_pos[1], name='x2')
s_x3 = openmc.XPlane(surface_id=3, x0=x_pos[2], name='x3')
s_x4 = openmc.XPlane(surface_id=4, x0=x_pos[3], name='x4')
s_ymin = openmc.YPlane(surface_id=5, y0=-inf_dist, name='ymin')
s_ymax = openmc.YPlane(surface_id=6, y0=inf_dist, name='ymax')
s_zmin = openmc.ZPlane(surface_id=7, z0=-inf_dist, name='zmin')
s_zmax = openmc.ZPlane(surface_id=8, z0=inf_dist, name='zmax')

s_x1.boundary_type = 'reflective'
s_x4.boundary_type = 'vacuum'
s_ymin.boundary_type = 'reflective'
s_ymax.boundary_type = 'reflective'
s_zmin.boundary_type = 'reflective'
s_zmax.boundary_type = 'reflective'

c_source = openmc.Cell(cell_id=1, name='source')
c_thin = openmc.Cell(cell_id=2, name='thin')
c_thick = openmc.Cell(cell_id=3, name='thick')

c_source.region = +s_x1 & -s_x2 & +s_ymin & -s_ymax & +s_zmin & -s_zmax
c_thin.region = +s_x2 & -s_x3 & +s_ymin & -s_ymax & +s_zmin & -s_zmax
c_thick.region = +s_x3 & -s_x4 & +s_ymin & -s_ymax & +s_zmin & -s_zmax

c_source.fill = m_source
c_thin.fill = m_thin
c_thick.fill = m_thick

root = openmc.Universe(universe_id=1, name='root')
root.add_cells([c_source, c_thin, c_thick])

geometry = openmc.Geometry(root)
geometry.export_to_xml()

###########
# Tallies #
###########

mesh = openmc.Mesh()
mesh.dimension = [1000, 1, 1]
mesh.lower_left = [x_pos[0], -inf_dist, -inf_dist]
mesh.upper_right = [x_pos[-1], inf_dist, inf_dist]

mesh_filter = openmc.MeshFilter(mesh)
energy_filter = openmc.EnergyFilter(groups.group_edges)

tally = openmc.Tally(name='flux')
tally.filters = [mesh_filter, energy_filter]
tally.scores = ['flux']

tallies_file = openmc.Tallies()
tallies_file.append(tally)
tallies_file.export_to_xml()

############
# Settings #
############

dist_source = 2.0
uniform_dist = openmc.stats.Box([x_pos[0], -dist_source, -dist_source],
                                [x_pos[1], dist_source, dist_source])
isotropic_dist = openmc.stats.Isotropic()
energy_dist = openmc.stats.Discrete([0.], [1.0])
source = openmc.Source(uniform_dist,
                       isotropic_dist,
                       energy_dist,
                       strength=1)
settings_file = openmc.Settings()
settings_file.energy_mode = "multi-group"
settings_file.run_mode = "fixed source"
settings_file.batches = 100
settings_file.particles = int(1e7)
settings_file.survival_biasing = True
settings_file.verbosity = 6
settings_file.source = source
settings_file.export_to_xml()

#######
# Run #
#######

openmc.run(threads=4)

################
# Load results #
################

# sp = openmc.StatePoint("statepoint.{}.h5".format(settings_file.batches))


import types
import pandas as pd
import dateutil.parser
import numpy as np
import ast
import matplotlib.pyplot as plt
import panel as pn
import panel.widgets as pnw
import matplotlib.gridspec as gridspec
import param
from labellines import labelLine, labelLines
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import HoverTool
from bokeh.palettes import Category20

class Dataset():
    def __init__(self, filename):
        self.df = pd.read_csv(filename)

class Dashboard(param.Parameterized):

    df = Dataset("./Data/vgsales.csv").df
    #data_path = "../Data/"

    #df = pd.read_csv(data_path+"vgsales.csv")

    piattaforma = param.ObjectSelector(default="Wii", objects=df["Platform"].unique().tolist())

    valore = param.ObjectSelector(default="Year", objects=["Year","Genre","Publisher"])

    def __init__(self, **params):

        super().__init__(**params)

        self.panel()

    @param.depends('piattaforma')
    def data(self):
        return self.df[self.df["Platform"] == self.piattaforma]

    @param.depends('data',"valore")
    def plot_1(self):
        group = self.valore
        try:
            #output_file("bar_sorted.html")
            a = self.data().groupby(group).sum()["Global_Sales"].div(self.data().groupby(group).size())

            fruits = a.index.astype("str").tolist()
            counts = a.tolist()

            # sorting the bars means sorting the range factors
            #sorted_fruits = sorted(fruits, key=lambda x: counts[fruits.index(x)])

            #ciao =
            p = figure(x_range=fruits, plot_height=350, title="Global sales per game grouped by"+group,
                       toolbar_location=None,tools="")

            p.vbar(x=fruits, top=counts, width=0.9)

            p.add_tools(HoverTool(tooltips=[(group, "@x"),("Value","@top")]))

            p.xgrid.grid_line_color = None
            p.y_range.start = 0

            if group == "Publisher":
                p.xaxis.major_label_orientation = 45
                p.xaxis.axis_label_text_font_size = "2pt"

            return pn.pane.Bokeh(p)
        except Exception as e:
            #print(e)
            return pn.pane.Str(str(e),sizing_mode="stretch_width")

    @param.depends('data',"valore")
    def plot_2(self):
        group = self.valore

        temp = self.data().groupby(group).sum()[["NA_Sales","EU_Sales","JP_Sales","Other_Sales"]]
        temp = temp.div(temp.sum(axis=1),axis = 0)
        temp = temp.reset_index()
        temp[group] = temp[group].astype("str")

        fruits = temp[group].tolist()
        years = ["NA_Sales","EU_Sales","JP_Sales","Other_Sales"]

        p = figure(x_range=fruits, plot_height=350,title="Sales percentage for each area of the world grouped by " + str(group),
                   toolbar_location=None, tools="")

        p.vbar_stack(years, x=group, width=0.9, source=temp,
                     legend_label=years,color = Category20[11][2:6])

        hover = HoverTool()
        hover.tooltips = [("Zone", "$name"),("Value","@$name")]
        hover.formatters = {'Value': 'printf'}
        p.add_tools(hover)

        p.y_range.start = 0
        p.x_range.range_padding = 0.1
        p.xgrid.grid_line_color = None
        p.axis.minor_tick_line_color = None
        p.outline_line_color = None
        p.legend.location = "top_left"
        p.legend.orientation = "vertical"
        p.add_layout(p.legend[0], 'right')

        if group == "Publisher":
            p.xaxis.major_label_orientation = 45
            p.xaxis.axis_label_text_font_size = "2pt"
        return pn.pane.Bokeh(p)

    @param.depends('piattaforma')
    def Html_ret(self):
        plat = self.piattaforma
        return pn.pane.HTML(''+plat+'', style = {
          "position": "absolute",
          "top": "50%",
          "left": "50%",
          "transform": "translate(-50%, -50%)",
          "font-size":"120px",
            "letter-spacing":"0.1em",
          "-webkit-text-fill-color": "transparent",
            "-webkit-text-stroke-width": "3px",
          "-webkit-text-stroke-color": "white",
            "text-shadow": "8px 8px #d52e3f,20px 20px #000000"
})

    @param.depends('data')
    def Html_ret_2(self):
        dat = self.data()
        return pn.pane.HTML(' Number of games produced: '+str(dat.shape[0])+'', style = {
          "position": "relative",
          "top": "50%",
          "left": "70%",
          "transform": "translate(-50%, -50%)",
          "font-size":"40px",
            "letter-spacing":"0.1em",
          "-webkit-text-fill-color": "transparent",
            "-webkit-text-stroke-width": "2px",
          "-webkit-text-stroke-color": "white",
            "text-shadow": "6px 6px #d52e3f,10px 10px #000000"
})

    def panel(self):
        appbar = pn.Row(
            pn.pane.HTML('''
Videogames sales dashboard
''',style = {
              "font-size": "4rem",
            "text-align": "left",
              "height":"10vh",
      "line-height": "10vh",
	    "color": "#fcedd8",
	    "background": "#d52e3f",
	   	"font-family": "Niconne, cursive",
	    "font-weight": "200",
      "text-shadow": "2px 2px 0px #eb452b, 4px 4px 0px #efa032, 6px 6px 0px #46b59b, 8px 8px 0px #017e7f, 10px 10px 0px #052939, 12px 12px 0px #c11a2b, 14px 14px 0px #c11a2b, 16px 16px 0px #c11a2b, 18px 18px 0px #c11a2b"
},width = 1000
            ),
            pn.layout.HSpacer(height=0),
            pn.pane.PNG(
                "./scripts/cont.png",
                width=200,
                align="center",
                sizing_mode="fixed",
                margin=(10, 50, 10, 5),
            ),
            sizing_mode="stretch_width",
            css_classes=["app-bar"],
        )
        gspec = pn.GridSpec(sizing_mode='stretch_both',background = "#f2f2f2")
        gspec[0, :3] = pn.Column(appbar,pn.layout.HSpacer(height=10))
        gspec[1, 0] = pn.Column(pn.Param(self.param,
                               parameters=["piattaforma", "valore"],
                widgets={
                    "piattaforma": {
                        "type": pn.widgets.Select,
                        "inline": True,
                        "align": "end",
                        "color": "#fcba03"
                    }
                }),css_classes=['app-container'])

        gspec[1,1] = pn.Row(pn.layout.VSpacer(width=10),pn.Column(self.plot_1,css_classes=["app-container"]))
        gspec[1,2] = pn.Row(pn.layout.VSpacer(width=10),pn.Column(self.plot_2,css_classes=["app-container"],sizing_mode= "stretch_width"))
        gspec[2,0] = pn.Row(pn.layout.VSpacer(width=10),self.Html_ret)
        gspec[2,1] = pn.Row(self.Html_ret_2,width =100)

        return gspec


import requests
import json
import pandas as pd
import sqlite3
from datetime import date
from datetime import datetime
from dateutil import parser
import time

SERVER = '10.24.25.130:8000'
conn = sqlite3.connect('usersrollup.db')
c = conn.cursor()

def makedb():
	c.execute('''Create Table users (username text,compname text,stat text,time text)''')
	conn.commit()

now = str(datetime.now())
r = requests.get('http://'+SERVER+'/get_dup')

newtxt = json.loads(r.text)
if (newtxt=={}):
	print("Returned nothing.");
else:
	#print(newtxt,now)
	for x in newtxt:
		time.sleep(5)
		r = requests.get('http://'+SERVER+'/get_log?username='+x+'&compname=all')
		thisreturn = json.loads(r.text)
		#print(x,thisreturn)
		for key,value in thisreturn.items():
			data2 = (value['username'],value['compname'],value['stat'],now)
			try:
				c.execute("INSERT INTO users VALUES "+str(data2))
			except sqlite3.OperationalError:
				makedb()
				c.execute("INSERT INTO users VALUES "+str(data2))
			conn.commit()
	r = requests.get('http://'+SERVER+'/db?action=clearlog')


from . import Command
from .. import utils
from urllib.parse import urlencode

class Lmdtfy(Command):
    names = ["lmdtfy", "search"]
    description = "Helps someone look something up on the superior search engine"
    usage = "!lmdtfy [query]"
    examples = f"!lmdtfy Does water boil in space?"

    async def execute_command(self, client, msg, content):
        if not content:
            content = "How do I think of what to search?"

        url = "https://lmgtfy.com/?s=d&" + urlencode({"q": content})
        await utils.delay_send(msg.channel, url, 1)

class Lmgtfy(Command):
    names = ["lmgtfy"]
    description = "Helps someone look something up on the inferior search engine"
    usage = "!lmgtfy [query]"
    examples = f"!lmgtfy Why am I not using DuckDuckGo?"

    async def execute_command(self, client, msg, content):
        if not content:
            content = "How do I think of what to search?"

        url = "https://lmgtfy.com/?" + urlencode({"q": content})
        await utils.delay_send(msg.channel, url, 1)


import os
import imageio
import argparse
import numpy as np
import torch
from torch.autograd import Variable
from models import Generator, Discriminator

SEED = 11
CUDA = torch.cuda.is_available()
np.random.seed(SEED)
torch.cuda.manual_seed(SEED) if CUDA else torch.manual_seed(SEED)

CYLW = "\033[93m"
CYLW2 = "\033[33m"
CEND = "\033[0m"
BOLD = "\033[1m"
RULE_STRING = 80 * "-"
LINEBREAK = f"{CYLW}{BOLD}{RULE_STRING}{CEND}"

def to_var(x):
    """Convert numpy to variable."""
    if torch.cuda.is_available():
        x = x.cuda()
    return Variable(x)

def to_data(x):
    """Convert variable to numpy."""
    if torch.cuda.is_available():
        x = x.cpu()
    return x.data.numpy()

def create_dir(directory):
    """Create a directory if it does not already exist."""
    if not os.path.exists(directory):
        os.makedirs(directory)

def print_model(model, model_name):
    """Print model information"""
    print(LINEBREAK)
    print(f"\t\t\t\t{CYLW2}{BOLD}{model_name}{CEND}")
    print(LINEBREAK)
    print(model)
    print(LINEBREAK)

def sample_noise(dim, batch_size):
    """
    Generate a PyTorch Variable of uniform random noise.

    Input:
    - batch_size: Integer giving the batch size of noise to generate.
    - dim: Integer giving the dimension of noise to generate.

    Output:
    - A PyTorch Variable of shape (batch_size, dim, 1, 1) containing uniform
      random noise in the range (-1, 1).
    """
    return to_var(torch.rand(batch_size, dim) * 2 - 1).unsqueeze(2).unsqueeze(3)

def build_discriminator(opts):
    D = Discriminator(conv_dim=opts.conv_dim)

    print_model(D, "DISCRIMINATOR")

    if torch.cuda.is_available():
        D.cuda()

    return D

def build_generator(opts):
    G = Generator(noise_size=opts.noise_size, conv_dim=opts.conv_dim)

    print_model(G, "GENERATOR")

    if torch.cuda.is_available():
        G.cuda()

    return G

def checkpoint(iteration, G, D, opts):
    """Save the parameters of the generator G and discriminator D"""
    G_path = os.path.join(opts.checkpoint_dir, "G.pkl")
    D_path = os.path.join(opts.checkpoint_dir, "D.pkl")
    torch.save(G.state_dict(), G_path)
    torch.save(D.state_dict(), D_path)

def create_image_grid(array, ncols=None):
    num_images, channels, cell_h, cell_w = array.shape

    if not ncols:
        ncols = int(np.sqrt(num_images))
    nrows = int(np.math.floor(num_images / float(ncols)))
    result = np.zeros((cell_h * nrows, cell_w * ncols, channels), dtype=array.dtype)
    for i in range(0, nrows):
        for j in range(0, ncols):
            result[
                i * cell_h : (i + 1) * cell_h, j * cell_w : (j + 1) * cell_w, :
            ] = array[i * ncols + j].transpose(1, 2, 0)

    return result.squeeze() if channels == 1 else result

def save_samples(G, fixed_noise, iteration, opts):
    generated_images = G(fixed_noise)
    generated_images = to_data(generated_images)

    grid = create_image_grid(generated_images)

    path = os.path.join(opts.sample_dir, "sample-{:06d}.png".format(iteration))
    imageio.imwrite(path, grid)
    print("Saved {}".format(path))

def create_parser():
    """Creates a parser for command-line arguments."""
    parser = argparse.ArgumentParser()

    # Model hyper-parameters
    parser.add_argument(
        "--image_size",
        type=int,
        default=32,
        help="The side length N to convert images to NxN.",
    )
    parser.add_argument("--conv_dim", type=int, default=32)
    parser.add_argument("--noise_size", type=int, default=100)

    # Training hyper-parameters
    parser.add_argument("--num_epochs", type=int, default=40)
    parser.add_argument(
        "--batch_size",
        type=int,
        default=16,
        help="The number of images in a batch.",
    )
    parser.add_argument(
        "--num_workers",
        type=int,
        default=0,
        help="The number of threads to use for the DataLoader.",
    )
    parser.add_argument(
        "--lr",
        type=float,
        default=0.0003,
        help="The learning rate (default 0.0003)",
    )
    parser.add_argument("--beta1", type=float, default=0.9)
    parser.add_argument("--beta2", type=float, default=0.999)

    # Data sources
    parser.add_argument(
        "--emoji",
        type=str,
        default="Apple",
        choices=["Apple", "Facebook", "Windows"],
        help="Choose the type of emojis to generate.",
    )

    # Directories and checkpoint/sample iterations
    parser.add_argument("--checkpoint_dir", type=str, default="./checkpoints_vanilla")
    parser.add_argument("--sample_dir", type=str, default="./samples_vanilla")
    parser.add_argument("--log_step", type=int, default=10)
    parser.add_argument("--sample_every", type=int, default=200)
    parser.add_argument("--checkpoint_every", type=int, default=400)

    return parser


from typing import Any, Dict, List, Type, TypeVar, Union

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

T = TypeVar("T", bound="AuthClientOpenApiVO")

@_attrs_define
class AuthClientOpenApiVO:
    """
    Attributes:
        id (Union[Unset, str]): AuthRecord ID
        name (Union[Unset, str]): Client name
        mac (Union[Unset, str]): Client mac
        wireless (Union[Unset, bool]): Is it wireless type
        ssid (Union[Unset, str]): SSID name
        network_name (Union[Unset, str]): Network name
        auth_type (Union[Unset, int]): Auth type

            0: No Auth 1: Simple Password 2: Exrternal Radius 3: Voucher

            4: External Portal Server 5: Local User 6: SMS 7: Facebook

            8: Hotspot Radius 9: Mac Auth (with fail over) 10: Admin auth 12：form auth
        fb_ver (Union[Unset, int]): Version of Facebook Wi-Fi (authType = Facebook or Facebook V2)
        download (Union[Unset, int]): Total Download (Byte)
        upload (Union[Unset, int]): Total Upload (Byte)
        duration (Union[Unset, int]): Total Duration (s)
        start (Union[Unset, int]): Start timestamp
        end (Union[Unset, int]): End timestamp
        valid (Union[Unset, bool]): Is the client valid
        local_user_name (Union[Unset, str]): LocalUser Name (authType = Local User)
        voucher_code (Union[Unset, str]): Voucher code (authType = Voucher)
        form_name (Union[Unset, str]): form name (authType = form auth)
        radius_username (Union[Unset, str]): Radius userName (authType = External RADIUS or Hotspot RADIUS)
        admin_name (Union[Unset, str]): Admin name (authType = ADMIN)
        ip (Union[Unset, str]): IP address at the time of authentication
    """

    id: Union[Unset, str] = UNSET
    name: Union[Unset, str] = UNSET
    mac: Union[Unset, str] = UNSET
    wireless: Union[Unset, bool] = UNSET
    ssid: Union[Unset, str] = UNSET
    network_name: Union[Unset, str] = UNSET
    auth_type: Union[Unset, int] = UNSET
    fb_ver: Union[Unset, int] = UNSET
    download: Union[Unset, int] = UNSET
    upload: Union[Unset, int] = UNSET
    duration: Union[Unset, int] = UNSET
    start: Union[Unset, int] = UNSET
    end: Union[Unset, int] = UNSET
    valid: Union[Unset, bool] = UNSET
    local_user_name: Union[Unset, str] = UNSET
    voucher_code: Union[Unset, str] = UNSET
    form_name: Union[Unset, str] = UNSET
    radius_username: Union[Unset, str] = UNSET
    admin_name: Union[Unset, str] = UNSET
    ip: Union[Unset, str] = UNSET
    additional_properties: Dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        id = self.id
        name = self.name
        mac = self.mac
        wireless = self.wireless
        ssid = self.ssid
        network_name = self.network_name
        auth_type = self.auth_type
        fb_ver = self.fb_ver
        download = self.download
        upload = self.upload
        duration = self.duration
        start = self.start
        end = self.end
        valid = self.valid
        local_user_name = self.local_user_name
        voucher_code = self.voucher_code
        form_name = self.form_name
        radius_username = self.radius_username
        admin_name = self.admin_name
        ip = self.ip

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if id is not UNSET:
            field_dict["id"] = id
        if name is not UNSET:
            field_dict["name"] = name
        if mac is not UNSET:
            field_dict["mac"] = mac
        if wireless is not UNSET:
            field_dict["wireless"] = wireless
        if ssid is not UNSET:
            field_dict["ssid"] = ssid
        if network_name is not UNSET:
            field_dict["networkName"] = network_name
        if auth_type is not UNSET:
            field_dict["authType"] = auth_type
        if fb_ver is not UNSET:
            field_dict["fbVer"] = fb_ver
        if download is not UNSET:
            field_dict["download"] = download
        if upload is not UNSET:
            field_dict["upload"] = upload
        if duration is not UNSET:
            field_dict["duration"] = duration
        if start is not UNSET:
            field_dict["start"] = start
        if end is not UNSET:
            field_dict["end"] = end
        if valid is not UNSET:
            field_dict["valid"] = valid
        if local_user_name is not UNSET:
            field_dict["localUserName"] = local_user_name
        if voucher_code is not UNSET:
            field_dict["voucherCode"] = voucher_code
        if form_name is not UNSET:
            field_dict["formName"] = form_name
        if radius_username is not UNSET:
            field_dict["radiusUsername"] = radius_username
        if admin_name is not UNSET:
            field_dict["adminName"] = admin_name
        if ip is not UNSET:
            field_dict["ip"] = ip

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        id = d.pop("id", UNSET)

        name = d.pop("name", UNSET)

        mac = d.pop("mac", UNSET)

        wireless = d.pop("wireless", UNSET)

        ssid = d.pop("ssid", UNSET)

        network_name = d.pop("networkName", UNSET)

        auth_type = d.pop("authType", UNSET)

        fb_ver = d.pop("fbVer", UNSET)

        download = d.pop("download", UNSET)

        upload = d.pop("upload", UNSET)

        duration = d.pop("duration", UNSET)

        start = d.pop("start", UNSET)

        end = d.pop("end", UNSET)

        valid = d.pop("valid", UNSET)

        local_user_name = d.pop("localUserName", UNSET)

        voucher_code = d.pop("voucherCode", UNSET)

        form_name = d.pop("formName", UNSET)

        radius_username = d.pop("radiusUsername", UNSET)

        admin_name = d.pop("adminName", UNSET)

        ip = d.pop("ip", UNSET)

        auth_client_open_api_vo = cls(
            id=id,
            name=name,
            mac=mac,
            wireless=wireless,
            ssid=ssid,
            network_name=network_name,
            auth_type=auth_type,
            fb_ver=fb_ver,
            download=download,
            upload=upload,
            duration=duration,
            start=start,
            end=end,
            valid=valid,
            local_user_name=local_user_name,
            voucher_code=voucher_code,
            form_name=form_name,
            radius_username=radius_username,
            admin_name=admin_name,
            ip=ip,
        )

        auth_client_open_api_vo.additional_properties = d
        return auth_client_open_api_vo

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


'''
Read an integer . For all non-negative integers , print . See the sample for details.

Input Format

The first and only line contains the integer, .

Constraints

Output Format

Print  lines, one corresponding to each .
'''

if __name__ == '__main__':
    n = int(input())
    if n > 0:
        for x in range(0, n):
            print(x**2)
    else:
        print("Operation Not Possible as per condition")


from engine.hive import Hive
import engine.hive_representation as represent
from unittest import TestCase
from engine.hive_utils import Player, Direction, HiveException
import numpy as np
import os
import json, sys

BUG_C = 22  # number of bugs without extension
import logging

FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
logger = logging.getLogger()

class TestRepresentation(TestCase):
    """
    Verify logic related to various outputs and inputs of the
    Hive engine.
    """

    @property
    def _list_repr(self):
        #    / \ / \ / \ / \ / \
        #   |wB1|wS2|   |bB1|   |
        #  / \ / \ / \ / \ / \ /
        # |wG1|   |wS1|bS1|bG1|
        #  \ / \ / \ / \ / \ / \
        #   |   |wQ1|   |bQ1|   |
        #  / \ / \ / \ / \ / \ /
        # |   |   |   |bA1|   |
        #  \ / \ / \ / \ / \ /
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        with open(os.path.join(__location__, 'repr_data.json')) as f:
            d = json.load(f)
            return np.array(d["repr_list"])

    def setUp(self) -> None:
        self.sh = logging.StreamHandler(sys.stdout)
        logger.addHandler(self.sh)
        self.hive = Hive()

    def tearDown(self) -> None:
        logger.removeHandler(self.sh)

    def test_empty_adjacency_state(self):
        adj = represent.get_adjacency_state(self.hive)
        self.assertEqual(len(adj), BUG_C)
        self.assertEqual(len(adj.get(list(adj.keys())[0])), BUG_C - 1)

        list_repr = represent.list_representation(adj)
        self.assertEqual(len(list_repr), BUG_C*(BUG_C - 1))
        self.assertTrue(all(v == 0 for v in list_repr))

    def test_load_state_from_list(self):
        """Verify canonical adjacency state loading"""
        input_list = self._list_repr

        self.hive = Hive.load_state_with_player(input_list, Player.WHITE)
        # ['wS2', 'wB1', 'wS1', 'wG1', 'bS1', 'bB1', 'bQ1', 'wQ1', 'bG1', 'bA1']
        self.assertEqual(len(self.hive.level.get_played_pieces()), 10)
        self.assertEqual(len(self.hive.level.get_unplayed_pieces(Player.WHITE)), 6)
        self.assertEqual(len(self.hive.level.get_unplayed_pieces(Player.BLACK)), 6)

        names = [str(p) for p in self.hive.level.get_played_pieces()]
        self.assertTrue("wS2" in names)
        self.assertTrue("wB1" in names)
        self.assertTrue("wS1" in names)
        self.assertTrue("wG1" in names)
        self.assertTrue("bS1" in names)
        self.assertTrue("bB1" in names)
        self.assertTrue("bQ1" in names)
        self.assertTrue("wQ1" in names)
        self.assertTrue("bG1" in names)
        self.assertTrue("bA1" in names)

        wB1_pos = self.hive.level.goto_direction(self.hive.locate("wG1"), Direction.HX_NE)
        self.assertEqual(self.hive.locate("wB1"), wB1_pos)

        # Verify everything around bS1
        bS1_pos = self.hive.locate("bS1")
        self.assertEqual(self.hive.locate("wS1"), self.hive.level.goto_direction(bS1_pos, Direction.HX_W))
        self.assertEqual(self.hive.locate("bB1"), self.hive.level.goto_direction(bS1_pos, Direction.HX_NE))
        self.assertEqual(self.hive.locate("bG1"), self.hive.level.goto_direction(bS1_pos, Direction.HX_E))
        self.assertEqual(self.hive.locate("bQ1"), self.hive.level.goto_direction(bS1_pos, Direction.HX_SE))

        self.assertFalse(self.hive.level.get_tile_content(self.hive.level.goto_direction(bS1_pos, Direction.HX_NW)))
        self.assertFalse(self.hive.level.get_tile_content(self.hive.level.goto_direction(bS1_pos, Direction.HX_SW)))

        # Test transforming it back to the list representation
        result = represent.two_dim_representation(represent.get_adjacency_state(self.hive))
        np.testing.assert_equal(result, input_list)

    def test_action_vector(self):
        self.hive = Hive.load_state_with_player(self._list_repr, Player.WHITE)
        __location__ = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__)))
        with open(os.path.join(__location__, 'repr_data.json')) as f:
            print(self.hive)
            d = json.load(f)
            res = represent.get_all_action_vector(self.hive)
            print(res)
            assert all([a == b for a,b in zip(res, d["test_action_vector"])])

    def test_actions_from_vector(self):
        self.hive = Hive.load_state_with_player(self._list_repr, Player.WHITE)
        all_actions = represent.get_all_action_vector(self.hive)
        indices = [i for i, v in enumerate(all_actions) if v > 0]
        for action_number in indices:
            self.hive.action_from_vector(action_number)

        # test other player's turn
        self.hive._toggle_player(self.hive.current_player)
        all_actions = represent.get_all_action_vector(self.hive)
        indices = [i for i, v in enumerate(all_actions) if v > 0]
        for action_number in indices:
            self.hive.action_from_vector(action_number)

    def test_action_numbers(self):
        self.hive = Hive()
        self.hive.place_piece_without_action("wB2")
        self.hive.level.current_player = Player.BLACK
        self.hive.place_piece_without_action("bB1", "wB2", Direction.HX_W)
        self.hive.level.current_player = Player.WHITE
        self.hive.place_piece_without_action("wA2", "wB2", Direction.HX_SE)
        self.hive.level.current_player = Player.BLACK
        self.hive.place_piece_without_action("bS2", "bB1", Direction.HX_W)
        self.hive.level.current_player = Player.WHITE
        self.assertEqual(represent.get_all_action_vector(self.hive)[90], 0)
        self.assertRaises(HiveException, self.hive.action_from_vector, 90)

if __name__ == '__main__':
    import unittest
    unittest.main()


# ====== Global array variables - Exercise 3 ======

# Exercise 3: using global array variables in functions
#
#   We propose 5 different functions to fill a global array "pop" with a given value.
#   Some are correct and some are not.
#   The use of different functions are activated through "run_function" flags.
#
#   Set to True the "run_function" flags associated with correct ways of
#   modifying global arrays inside a function.
#   If you don't choose well, the program will crash...

from larray import *

AGE = Axis("age=0..5")

fill_value = 5
expected_pop = full(AGE, fill_value)

# SOLUTION:
run_function_1 = False
run_function_2 = True
run_function_3 = True
run_function_4 = False
run_function_5 = True

# ================= VERSION 1 =================
if run_function_1:
    pop = zeros(AGE)

    # EXPLANATION: the use of operator '=' creates a new local array 'pop'.
    # Inside the function, the global array 'pop' is shadowed by the local array 'pop'.
    def fill_pop_1(fill_value):
        pop = full(AGE, fill_value)

    fill_pop_1(fill_value)
    assert larray_equal(pop, expected_pop), "version 1 of fill_pop function is incorrect"

# ================= VERSION 2 =================
if run_function_2:
    pop = zeros(AGE)

    # EXPLANATION: the use of [:] next to the array 'pop' avoid to create a local array.
    def fill_pop_2(fill_value):
        pop[:] = full(AGE, fill_value)

    fill_pop_2(fill_value)
    assert larray_equal(pop, expected_pop), "version 2 of fill_pop function is incorrect"

# ================= VERSION 3 =================
if run_function_3:
    pop = zeros(AGE)

    # EXPLANATION: the use of operator '=' creates a new local array 'pop' but
    # this local array is returned at the end of the function and its content
    # set to the global array 'pop' by typing "pop = fill_pop_3(fill_value)"
    def fill_pop_3(fill_value):
        pop = full(AGE, fill_value)
        return pop

    pop = fill_pop_3(fill_value)
    assert larray_equal(pop, expected_pop), "version 3 of fill_pop function is incorrect"

# ================= VERSION 4 =================
if run_function_4:
    pop = zeros(AGE)

    # EXPLANATION: input arguments are local variables.
    def fill_pop_4(pop, fill_value):
        pop = full(AGE, fill_value)

    fill_pop_4(pop, fill_value)
    assert larray_equal(pop, expected_pop), "version 4 of fill_pop function is incorrect"

# ================= VERSION 5 =================
if run_function_5:
    pop = zeros(AGE)

    # EXPLANATION: the use of [:] next to the array 'pop' avoid to create a local array.
    def fill_pop_5(pop, fill_value):
        pop[:] = full(AGE, fill_value)

    fill_pop_5(pop, fill_value)
    assert larray_equal(pop, expected_pop), "version 5 of fill_pop function is incorrect"


import socket
import os
import subprocess
import struct
import json

phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

phone.bind(('127.0.0.1',8081))

phone.listen(5)

while True:
    conn,client_addr=phone.accept()

    while True:
        try:
            data=conn.recv(1024)
            if not data:break
            print('客户端的数据为',data)
            # 1、conn.send(data.upper())
            obj=subprocess.Popen(data.decode('utf-8'),shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            stdout=obj.stdout.read()
            stderr=obj.stderr.read()
            #1、数据长度转固定长度报头
            header_dic={
                'folename':'a.txt',
                'md5':'graga',
                'total_size':len(stdout) + len(stderr)
            }
            header_json=json.dumps(header_dic)
            header_bytes=header_json.encode('utf-8')

            header_len=struct.pack('i',len(header_bytes))
            #2、把报头发送给客户端
            conn.send(header_len)
            conn.send(header_bytes)
            #3、发送数据
            conn.send(stdout)
            conn.send(stderr)
        except ConnectionResetError:
            break

    conn.close()

phone.close()


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

import pandas as pd
import numpy as np
import requests as request

from package.index import get_company_info_url
from package.index import get_finance_income_report_url
from package.index import get_finance_balance_report_url
from package.template import create_post_company_basic_data
from package.template import create_post_company_data
from package.template import create_info_df
from package.template import create_composite_income_df

def read_company_info(stock_id, dfs):
    for df in dfs:
        if df.empty:
            continue

        for row in df.itertuples():
            if str(row[1]) == "股票代號":
                stock_id = row[2]
                company_type = row[4]
                print("{}: {}, {}: {}".format(row[1], stock_id, row[3], company_type))
                info = create_info_df(stock_id, company_type)
                return info

def create_year_dataframe(lables, year, values):
    df = create_composite_income_df(year + 1911)

    is_pre = False
    pre_other_profit = 0
    pre_other_loss = 0
    for i in range(len(lables)):
        if lables[i].find("營業收入") == 0 or lables[i].find("淨收益") == 0:
            df.營業收入 = int(values[i])
            print("營業收入: {}".format(int(values[i])))
        elif lables[i].find("營業成本") == 0:
            df.營業成本 = int(values[i])
            print("營業成本: {}".format(int(values[i])))
        elif lables[i].find("營業毛利") == 0:
            df.營業毛利 = int(values[i])
            print("營業毛利: {}".format(int(values[i])))
        elif lables[i].find("營業費用") == 0:
            df.營業費用 = int(values[i])
            print("營業費用: {}".format(int(values[i])))
        elif lables[i].find("營業利益") == 0 or lables[i].find("營業淨利") == 0:
            df.營業利益 = int(values[i])
            print("營業利益: {}".format(int(values[i])))
        elif lables[i].find("營業外收入及支出") == 0:
            df.業外收支 = int(values[i])
            print("業外收支: {}".format(int(values[i])))
        elif lables[i].find("稅前淨利") == 0 or lables[i].find("繼續營業單位稅前淨利") == 0 or lables[i].find("繼續營業單位稅前合併淨利") == 0 or lables[i].find("繼續營業單位稅前損益") == 0:
            df.稅前淨利 = int(values[i])
            print("稅前淨利: {}".format(int(values[i])))
        elif lables[i].find("所得稅") == 0:
            df.所得稅 = abs(int(values[i]))
            print("所得稅: {}".format(abs(int(values[i]))))
        elif lables[i].find("本期淨利") == 0 or lables[i].find("繼續營業單位淨利") == 0 or lables[i].find("繼續營業單位稅後合併淨利") == 0 or lables[i].find("本期稅後淨利") == 0:
            df.本期淨利 = int(values[i])
            print("本期淨利: {}".format(int(values[i])))
        elif lables[i].find("本期綜合損益總額") == 0 or ((lables[i].find("合併總損益") == 0) and (lables[i].find("合併總損益歸屬予") == -1)):
            df.本期綜合損益 = int(values[i])
            print("本期綜合損益: {}".format(int(values[i])))
        elif lables[i].find("淨利（淨損）歸屬於母公司業主") == 0:
            df.業主淨利 = int(values[i])
            print("業主淨利: {}".format(int(values[i])))
        elif lables[i].find("母公司股東") == 0 and lables[i-1].find("合併總損益歸屬予") == 0:
            df.業主淨利 = int(values[i])
            print("業主淨利: {}".format(int(values[i])))
        elif lables[i].find("基本每股盈餘") == 0:
            df.基本每股盈餘 = values[i]
            print("基本每股盈餘: {}".format(values[i]))
        ### pre IFRSs
        elif lables[i].find("營業外收入及利益") == 0:
            is_pre = True
            pre_other_profit = int(values[i])
        elif lables[i].find("營業外費用及損失") == 0:
            pre_other_loss = int(values[i])
        else:
            continue

    if is_pre:
        other = pre_other_profit - pre_other_loss
        df.業外收支 = other
        print("業外收支: {}".format(other))

    if pd.isnull(df.loc[year+1911, "營業利益"]):
        profit = df.loc[year+1911, "營業收入"] - df.loc[year+1911, "營業費用"]
        df.營業利益 = profit
        print("營業利益: {}".format(profit))
    return df

def read_finance_data(stock_id, dfs, year):
    for df in dfs:
        if df.empty:
            continue

        columns = list(df.columns)
        if "{}年".format(year) in columns or "{}年度".format(year) in columns:
            data = create_year_dataframe(df[columns[0]].values, year, df[columns[3]].values)
            return data

def is_step2_needed(dfs):
    step2_needed = False

    for df in dfs:
        if step2_needed:
            break

        if df.empty:
            continue

        columns = list(df.columns)
        for column in columns:
            if "公司代號" in columns and "公司名稱" in columns:
                step2_needed = True
                break

    return step2_needed

def query_company_info(stock_id):
    url = get_company_info_url()
    data = create_post_company_basic_data(stock_id)
    print("post {} company info url: {}".format(stock_id, url))
    response = request.post(url, data)
    status = response.status_code
    print("status code: {}".format(status))
    if status != 200:
        return
    info = read_company_info(stock_id, pd.read_html(response.content))
    return info

def query_company_income_state(stock_id, year):
    url = get_finance_income_report_url(year)
    # step 1
    data = create_post_company_data(stock_id, year, 1)
    print("post {} finance report url: {}".format(stock_id, url))
    response = request.post(url, data)
    status = response.status_code
    print("status code: {}".format(status))
    if status != 200:
        return

    dfs = pd.read_html(response.content)
    if is_step2_needed(dfs):
        # step 2
        data = create_post_company_data(stock_id, year, 2)
        print("post {} finance report url: {}".format(stock_id, url))
        response = request.post(url, data)
        status = response.status_code
        print("status code: {}".format(status))
        if status != 200:
            return

    dfs = pd.read_html(response.content)
    results = read_finance_data(stock_id, dfs, year)
    return results

if __name__ == "__main__":
    query_company_info(2382)


quiz = {
    1 : {
        "question" : "Who is the creator of bitcoin?",
        "answer" : "Satoshi Nakamoto"
    },
    2 : {
        "question" : "Who is the creator of Ethereum?",
        "answer" : "Vitalik Buterin"
    },
    3 : {
        "question" : "What hashing function is used in orde to validate and confirm bitcoin transactions?",
        "answer" : "Proof of work"
    },
    4 : {
        "question" : "What algortihmic function is used to validate transactions using ethereum?",
        "answer" : "Proof of stake"
    },
    5 : {
        "question" : "What year was the bitcoin whitepaper first published?",
        "answer" : "2008"
    },
    6 : {
        "question" : "What does defi stand for?",
        "answer" : "Decentralized finance"
    }
}


import time

def createGenerator():

    mylist = [i for i in range(1, 30)]

    for i in mylist:

        time.sleep(1)
        if i % 2:
            yield i

for i in createGenerator():
    print(i)


import hashlib
import logging
import sys

from turbine.runtime import RecordList, Runtime

logging.basicConfig(level=logging.INFO)

def print_records(records: RecordList) -> RecordList:
    print(records)
    return

class App:
    @staticmethod
    async def run(turbine: Runtime):
        try:
            source = await turbine.resources("kafka-test")

            records = await source.records("user_activity", {'conduit':'true', 'readFromBeginning': 'true'})

            printed = await turbine.process(records, print_records)

            destination_db = await turbine.resources("pg")

            await destination_db.write(printed, "messages")
        except Exception as e:
            print(e, file=sys.stderr)


# -*- coding: utf-8 -*-

class ShortInputException(Exception):
    '''Uma classe exceção definida pelo usuário.'''

    def __init__(self, length, atleast):
        Exception.__init__(self)
        self.length = length
        self.atleast = atleast

try:
    text = input('Entre com alguma coisa >>> ')

    if len(text) < 3:
        raise ShortInputException(len(text), 3)
except EOFError:
    print('Por que você jogou um EOF em mim?')
except ShortInputException as e:
    print('ShortInputException: A entrada teve o comprimento {0}, o \
esperado era pelo menos {1}'.format(e.length, e.atleast))
else:
    print('Nenhuma exceção foi levantada.')