"""
  Triggered on the Cloud event and performs the data validation.
"""
import boto3
import os
import logging
import codecs
import pandas as pd
import json

#####################################################
#  VARIABLES
#####################################################

file_name = os.path.splitext(os.path.basename(__file__))[0]

default_log_args = {
    "level": logging.DEBUG if os.environ.get("DEBUG", 0) else logging.INFO,
    "format": "%(asctime)s [%(levelname)s] %(name)s - %(message)s",
    "datefmt": "%d-%b-%y %H:%M"
}

root = logging.getLogger()
if root.handlers:
    for handler in root.handlers:
        root.removeHandler(handler)

sample_records = 100
data_records = []
data_column = []
with_header = True
data_validation_flag = True
global row_count, column_count, comment
row_count =0
column_count = 0
comment = []

file_formats = ['.txt', '.csv', '.tsv']
file_names = ['prof']  # , 'inst', 'rx'
template = {
    'prof': { 79: 'prof_v3_template.csv', 74: 'prof_v2_template.csv'}
}

#####################################################
#  FUNCTIONS
#####################################################

def get_logger(name):
    logging.basicConfig(**default_log_args)
    return logging.getLogger(name)

def get_split_char(key):
    if key.lower().endswith('.tsv'):
        return '\t'
    elif key.lower().endswith('.csv'):
        return ','
    elif key.lower().endswith('.txt'):
        return '|'
    else:
        return ' '

def get_row_count_of_s3_csv(bucket_name, path):
    req = boto3.client('s3').select_object_content(
        Bucket=bucket_name,
        Key=path,
        ExpressionType="SQL",
        Expression="""SELECT count(*) FROM s3object """,
        InputSerialization={"CSV": {"FileHeaderInfo": "Use",
                                    "AllowQuotedRecordDelimiter": True}},
        OutputSerialization={"CSV": {}},
    )
    row_count = next(int(x["Records"]["Payload"]) for x in req["Payload"])
    return row_count

#####################################################
#  MAIN LAMBDA EVENT
#####################################################

def lambda_main(event: dict = None, context: dict = None):
    try:
        global row_count, column_count
        log = get_logger(f"{file_name}.{lambda_main.__name__}")
        log.info('=' * 30 + " Init " + '=' * 30)
        log.info(f"[*] event - {event}")
        log.info(f"[*] context - {context}")
        records = event.get("Records", None)

        if not records:
            log.info(f"[-] No Records found in the events - {event}")
            return None

        new_file = [{'bucket': record["s3"]["bucket"]["name"],
                      'key': record['s3']['object']['key'],
                      'size': record['s3']['object']['size']}
                     for record in records
                     if record.get("s3")]

        new_file = new_file[0]
        s3 = boto3.resource("s3")
        key = new_file['key']
        bucket = new_file['bucket']

        if any(key.lower().endswith(fmt) for fmt in file_formats):
            msg = '>>>>>>> [+] Expected File Format matched'
            comment.append(msg)
            log.info(msg)
        else:
             msg = '>>>>>>> [-] Expected File Format unmatched'
             comment.append(msg)
             log.info(msg)
             return -1

        if any(fmt in key.lower() for fmt in file_names):
            msg  = '>>>>>>> [+] Expected File Name matched'
            comment.append(msg)
            log.info(msg)
        else:
             msg = '>>>>>>> [-] Expected File Name unmatched'
             comment.append(msg)
             log.info(msg)
             return -1

        row_count = get_row_count_of_s3_csv(bucket, key)
        log.info(f'>>>>>>> [+] Row_Count = {row_count}')
        split_char = get_split_char(key)
        idx = 0
        s3_object = s3.Object(new_file['bucket'], key)
        line_stream = codecs.getreader("utf-8")
        for line in line_stream(s3_object.get()['Body']):
            # log.info(f"line is {line}")
            if idx == 0:
                data_column = [value.strip() for value in line.split(split_char)]
            else:
                data_records.append([value.strip() for value in line.split(split_char)])
            idx += 1
            if idx > sample_records:
                break

        log.info(f'>>>>>>> [+] Collected Sample records = {sample_records}')
        log.info(f'>>>>>>> Column Length - {len(data_column)}')

        # Check Sample Records has headers
        template_file = ''
        if 'prof' in key.lower():
            template_file = template['prof'].get(len(data_column), '')

        if not template_file:
            log.info('>>>>>>> [-] Template File not found.')
            return -1

        log.info(f'>>>>>>> [+] Template File found - {template_file}')
        csv_df = pd.read_csv(open(template_file))
        # print(csv_df.columns)
        # print(csv_df.head())
        # print(csv_df[['Seq', 'ColumnName']].values.tolist())
        # print('|'.join(csv_df['ColumnName'].values.tolist()))
        expected_columns = csv_df['ColumnName'].values.tolist()
        actual_columns = data_column
        log.info(f'=====>> {len(expected_columns)} == {len(actual_columns)}')
        log.info(f'=====>> {set(expected_columns) - set(actual_columns)}/{set(actual_columns) - set(expected_columns)}')
        log.info(f'=====>> type(data_records) = {type(data_records)}{len(data_records)} type(data_column)  = {type(data_column)}{len(data_column)}')

        if expected_columns == actual_columns:
            log.info('>>>>>>> [+] Header found.')
            df = pd.DataFrame(data=data_records, columns=data_column)
        else:
            log.info('>>>>>>> [-] Header is not found.')
            df = pd.DataFrame(data=data_records.append(data_column), columns=expected_columns)

        print(df.head())
        # print(df.describe())
        # print(df.columns)
        actual_columns = list(df.columns)

        # dataframe.size
        size = df.size
        # dataframe.shape
        shape = df.shape
        # printing size and shape
        log.info(">>>>> Size = {} | Shape ={} | Shape[0] x Shape[1] = {}".format(size, shape, shape[0]*shape[1]))
        column_count = shape[1]
        log.info(f'>>>>>>> Column_Count = {column_count}')

        if len(expected_columns) == column_count:
            msg = '[+] Column Count Matched'
            comment.append(msg)
            log.info(msg)
        else:
            msg = '[-] Column Count Unmatched'
            comment.append(msg)
            log.info(msg)
            return -1

        if expected_columns == actual_columns:
            msg  = '[+] Column Order & Name Matched'
            comment.append(msg)
            log.info(msg)
        else:
            msg = '[-] Column Order & Name Unmatched'
            comment.append(msg)
            log.info(msg)
            return -1
        if template_file == 'prof_v3_template.csv':
            log.info(f" >>> df['Member Sex'].isin(['M', 'F', 'U'] = {all(df['Member Sex'].isin(['M', 'F', 'U']).tolist())}")
            for col in ['Claim Paid Date', 'Payment Adjudication Date', 'Claim Submission Date', 'Member Date of Birth (DOB)', 'Member Date of Death (DOD)', 'Beginning Date of Service', 'Ending Date of Service']:
                m1 = df[col].eq('') | df[col].isna()
                m2 = pd.to_datetime(df[col], format='%m/%d/%Y', errors='coerce').isna()
                log.info(f">>>> Date Validation - {col} = {m1.eq(m2).all()}")
            return 1
    except Exception as err:
        comment.append(sr(err))
        return -1

def lambda_handler(event: dict = None, context: dict = None):

    log = get_logger(f"{file_name}.{lambda_handler.__name__}")
    if lambda_main(event, context):
        kwargs = {"row_count": row_count,
            "column_count": column_count,
            "data_validation_flag": True,
            "comment":  '|'.join(comment)}
    else:
        log.info('Data validation failed')
        kwargs = {"row_count": row_count,
            "column_count": column_count,
            "data_validation_flag": False,
            "comment":  '|'.join(comment)}
    log.info(f'{kwargs}')

    _records  = event['Records'][0]
    _records['row_count'] = kwargs['row_count']
    _records['column_count'] = kwargs['column_count']
    _records['data_validation_flag'] = kwargs['data_validation_flag']
    _records['comment'] =  kwargs['comment']

    lambda_client = boto3.client('lambda')

    lambda_client.invoke(FunctionName='File-Meta-Data-Catcher',
                         InvocationType='Event',
                         Payload=json.dumps(event))
    log.info('=' * 30 + " Exit " + '=' * 30)


import mock

from nova.network import model as network_model
from nova import test
from nova import utils
from nova.virt.vmwareapi import fake as vmwareapi_fake
from nova.virt.vmwareapi import vim_util
from nova.virt.vmwareapi import vmops

class fake_session(object):
    def __init__(self, ret=None):
        self.ret = ret

    def _get_vim(self):
        return vmwareapi_fake.FakeVim()

    def _call_method(self, module, method, *args, **kwargs):
        return self.ret

    def _wait_for_task(self, task_ref):
        return

class VMwareVMOpsTestCase(test.NoDBTestCase):
    def setUp(self):
        super(VMwareVMOpsTestCase, self).setUp()
        subnet_4 = network_model.Subnet(cidr='192.168.0.1/24',
                                        dns=[network_model.IP('192.168.0.1')],
                                        gateway=
                                            network_model.IP('192.168.0.1'),
                                        ips=[
                                            network_model.IP('192.168.0.100')],
                                        routes=None)
        subnet_6 = network_model.Subnet(cidr='dead:beef::1/64',
                                        dns=None,
                                        gateway=
                                            network_model.IP('dead:beef::1'),
                                        ips=[network_model.IP(
                                            'dead:beef::dcad:beff:feef:0')],
                                        routes=None)
        network = network_model.Network(id=0,
                                        bridge='fa0',
                                        label='fake',
                                        subnets=[subnet_4, subnet_6],
                                        vlan=None,
                                        bridge_interface=None,
                                        injected=True)
        self.network_info = network_model.NetworkInfo([
                network_model.VIF(id=None,
                                  address='DE:AD:BE:EF:00:00',
                                  network=network,
                                  type=None,
                                  devname=None,
                                  ovs_interfaceid=None,
                                  rxtx_cap=3)
                ])
        utils.reset_is_neutron()
        self._session = fake_session()

    def test_get_machine_id_str(self):
        result = vmops.VMwareVMOps._get_machine_id_str(self.network_info)
        self.assertEqual(result,
                         'DE:AD:BE:EF:00:00;192.168.0.100;255.255.255.0;'
                         '192.168.0.1;192.168.0.255;192.168.0.1#')

    def test_is_neutron_nova(self):
        self.flags(network_api_class='nova.network.api.API')
        ops = vmops.VMwareVMOps(None, None, None)
        self.assertFalse(ops._is_neutron)

    def test_is_neutron_neutron(self):
        self.flags(network_api_class='nova.network.neutronv2.api.API')
        ops = vmops.VMwareVMOps(None, None, None)
        self.assertTrue(ops._is_neutron)

    def test_is_neutron_quantum(self):
        self.flags(network_api_class='nova.network.quantumv2.api.API')
        ops = vmops.VMwareVMOps(None, None, None)
        self.assertTrue(ops._is_neutron)

    def test_use_linked_clone_override_nf(self):
        value = vmops.VMwareVMOps.decide_linked_clone(None, False)
        self.assertFalse(value, "No overrides present but still overridden!")

    def test_use_linked_clone_override_nt(self):
        value = vmops.VMwareVMOps.decide_linked_clone(None, True)
        self.assertTrue(value, "No overrides present but still overridden!")

    def test_use_linked_clone_override_ny(self):
        value = vmops.VMwareVMOps.decide_linked_clone(None, "yes")
        self.assertTrue(value, "No overrides present but still overridden!")

    def test_use_linked_clone_override_ft(self):
        value = vmops.VMwareVMOps.decide_linked_clone(False, True)
        self.assertFalse(value,
                        "image level metadata failed to override global")

    def test_use_linked_clone_override_nt(self):
        value = vmops.VMwareVMOps.decide_linked_clone("no", True)
        self.assertFalse(value,
                        "image level metadata failed to override global")

    def test_use_linked_clone_override_yf(self):
        value = vmops.VMwareVMOps.decide_linked_clone("yes", False)
        self.assertTrue(value,
                        "image level metadata failed to override global")

    def _test_get_datacenter_ref_and_name(self, ds_ref_exists=False):
        instance_ds_ref = mock.Mock()
        instance_ds_ref.value = "ds-1"
        _vcvmops = vmops.VMwareVCVMOps(self._session, None, None)
        if ds_ref_exists:
            ds_ref = mock.Mock()
            ds_ref.value = "ds-1"
        else:
            ds_ref = None

        def fake_call_method(module, method, *args, **kwargs):
            fake_object1 = vmwareapi_fake.FakeRetrieveResult()
            fake_object1.add_object(vmwareapi_fake.Datacenter(
                ds_ref=ds_ref))
            if not ds_ref:
                # Token is set for the fake_object1, so it will continue to
                # fetch the next object.
                setattr(fake_object1, 'token', 'token-0')
                if method == "continue_to_get_objects":
                    fake_object2 = vmwareapi_fake.FakeRetrieveResult()
                    fake_object2.add_object(vmwareapi_fake.Datacenter())
                    return fake_object2

            return fake_object1

        with mock.patch.object(self._session, '_call_method',
                               side_effect=fake_call_method) as fake_call:
            dc_info = _vcvmops.get_datacenter_ref_and_name(instance_ds_ref)

            if ds_ref:
                self.assertEqual(1, len(_vcvmops._datastore_dc_mapping))
                fake_call.assert_called_once_with(vim_util, "get_objects",
                    "Datacenter", ["name", "datastore", "vmFolder"])
                self.assertEqual("ha-datacenter", dc_info.name)
            else:
                calls = [mock.call(vim_util, "get_objects", "Datacenter",
                                   ["name", "datastore", "vmFolder"]),
                         mock.call(vim_util, "continue_to_get_objects",
                                   "token-0")]
                fake_call.assert_has_calls(calls)
                self.assertIsNone(dc_info)

    def test_get_datacenter_ref_and_name(self):
        self._test_get_datacenter_ref_and_name(ds_ref_exists=True)

    def test_get_datacenter_ref_and_name_with_no_datastore(self):
        self._test_get_datacenter_ref_and_name()


import random
import crypto_lib

class EllipticCurve:
    def __init__(self, a: int, b: int, p: int):
        self.a = a
        self.b = b
        self.p = p

    def __iter__(self):
        yield self.a
        yield self.b
        yield self.p

    def __eq__(self, other):
        if not isinstance(other, EllipticCurve):
            raise TypeError(other.__class__)
        return self.a == other.a and self.b == other.b and self.p == other.p

    def __hash__(self):
        return hash((self.a, self.b, self.p))

    def __repr__(self):
        return f"E_{self.p}({self.a}, {self.b})"

    @property
    def zero(self):
        return EllipticPoint(None, None, self)

class EllipticPoint:
    def __init__(self, x: int, y: int, curve: EllipticCurve):
        self.x = x
        self.y = y
        self.curve = curve
        self.is_null = x is None

    def __neg__(self) -> "EllipticPoint":
        if self.is_null:
            return self
        return EllipticPoint(self.x, (-self.y) % self.curve.p, self.curve)

    def __eq__(self, q) -> bool:
        return self.x == q.x and self.y == q.y

    def __hash__(self):
        return hash((self.x, self.y, self.curve))

    def __iter__(self):
        yield self.x
        yield self.y

    def __add__(self, q) -> "EllipticPoint":
        if not isinstance(q, EllipticPoint):
            raise TypeError(q.__class__)
        p = self
        if p.is_null:
            return q
        if q.is_null:
            return p
        c = self.curve
        if p == -q:
            return c.zero
        c_p = c.p
        if p == q:
            numer = (3 * p.x * p.x + c.a) % c_p
            denom = (2 * p.y) % c_p
            l = (numer * crypto_lib.inverse_in_group(denom, c_p)) % c_p
        else:
            numer = (q.y - p.y) % c_p
            denom = (q.x - p.x) % c_p
            l = (numer * crypto_lib.inverse_in_group(denom, c_p)) % c_p
        x = (l * l - p.x - q.x) % c_p
        y = (l * (p.x - x) - p.y) % c_p
        return EllipticPoint(x, y, c)

    def __mul__(self, k) -> "EllipticPoint":
        if not isinstance(k, int):
            raise TypeError(k.__class__)
        if k < 0:
            return -(self * (-k))
        p = self
        r = self.curve.zero
        while k > 0:
            if k % 2:
                r += p
            k >>= 1
            p += p
        return r

    def __sub__(self, other):
        return self + (-other)

    def __rsub__(self, other):
        return other + (-self)

    def __rmul__(self, other):
        return self * other

    def __repr__(self):
        if self.is_null:
            return "O"
        return repr(tuple(self))

def generate_curve_points(curve: EllipticCurve):
    result = []
    a, b, p = curve
    m = p // 4
    for x in range(p):
        y2 = (x * x * x + a * x + b) % p
        l = crypto_lib.fastpow(y2, (p - 1) // 2, p)
        if l not in (0, 1):
            continue
        y = crypto_lib.fastpow(y2, m + 1, p)
        result.append(EllipticPoint(x, y, curve))
        if y != 0:
            result.append(EllipticPoint(x, (-y) % p, curve))
    return result

def generate_primes(n):
    # решето Эратосфена, можно немного оптимизировать, сразу убрав из решетки четные
    sieve = [True] * (n+1)
    sieve[0] = sieve[1] = False
    for i in range(2, (n+1) // 2):
        for j in range(2*i, n+1, i):
            sieve[j] = False
    return [i for i, f in enumerate(sieve) if f]

def _test_generate_curve_points():
    curve = EllipticCurve(1, 2, 19)
    expected = {
        EllipticPoint(1, 2, curve),
        EllipticPoint(1, 17, curve),
        EllipticPoint(8, 3, curve),
        EllipticPoint(8, 16, curve),
        EllipticPoint(10, 9, curve),
        EllipticPoint(10, 10, curve),
        EllipticPoint(14, 9, curve),
        EllipticPoint(14, 10, curve),
        EllipticPoint(17, 7, curve),
        EllipticPoint(17, 12, curve),
        EllipticPoint(18, 0, curve),
    }
    actual = set(generate_curve_points(curve))
    assert expected == actual, (expected - actual, actual - expected)

def _test_ops():
    curve = EllipticCurve(1, 2, 19)
    p = EllipticPoint(1, 2, curve)
    q = EllipticPoint(10, 10, curve)
    assert p + q == EllipticPoint(17, 7, curve), p + q
    assert p + p == EllipticPoint(18, 0, curve), p + p
    assert 2 * p == EllipticPoint(18, 0, curve), 2 * p
    assert (q - q).is_null, q - q
    assert q + p - q == p, (p, q, q + p, q + p - q)
    r = p
    for _ in range(curve.p * curve.p):
        r += p
        if r.is_null:
            break
    assert r.is_null, r

def main():
    _test_generate_curve_points()
    _test_ops()
    assert generate_primes(17) == [2, 3, 5, 7, 11, 13, 17]

    curve = EllipticCurve(-5, -1, 23)
    assert (4 * curve.a ** 3 + 27 * curve.b ** 2) % curve.p != 0

    print("Generating keypair")
    points = generate_curve_points(curve)
    print(f"Generated points for {curve}: {points}")
    curve_order = len(points) + 1  # +1 is for the point at infinity
    print(f"n = {curve_order}")

    print("Generating P...")
    # Ищем такую точку, которая образует подгруппу простого порядка
    # причем желательно, чтобы порядок был по-больше.
    # Для всех точек кривой находим порядок циклической подгруппы, которую точка
    # порождает, после чего смотрим, является ли порядок простым.
    # И среди всех точек, порождающих подгруппу простого порядка, выбираем ту,
    # где порядок наибольший.
    # На практике так делать слишком медленно, на практике сначала вычисляют
    # порядок кривой (есть какой-то полиномиальный алгоритм для этого),
    # затем находят достаточно большой делитель и затем вычисляют базовую точку
    # по формуле (N/n)*P, N - порядок кривой, n - найденный делитель, P - любая точка.
    # Нетрудно доказать, что такая точка всегда генерирует подгруппу порядка n...
    p = None
    n = 0
    primes_up_to_n = set(generate_primes(curve_order))
    for pp in points:
        p_acc = curve.zero
        for i in range(1, curve_order+1):
            p_acc += pp
            if p_acc.is_null:
                if i > n:
                    if i in primes_up_to_n:
                        n = i
                        p = pp
                break
    print(f"Found P={p} of order = {n}")

    # Генерируем пару ключей:
    d = random.randrange(1, n)
    q = d * p
    public_key = curve.a, curve.b, p, n, q
    private_key = d
    print(f"public key (a, b, P, n, Q) = {public_key}")
    print(f"private key (d) = {private_key}")

    # Подпись:
    # Сообщение представлено в виде просто неотрицательного числа, по модулю
    # не превышающего n. На практике вычисляется какой-нибудь криптостойкий хеш и
    # обрезается до битовой длины не длинее n.
    t = random.randrange(n)
    print(f"Singing message t = {t}")
    signature = None
    while True:
        k = random.randrange(1, n)
        print(f"trying k = {k}")
        x1, y1 = k * p
        r = x1 % n
        if r == 0:
            continue
        l = crypto_lib.inverse_in_group(k, n)
        s = (l * (t + d * r)) % n
        if s == 0:
            continue
        signature = r, s
        break
    print(f"signature (r, s) = {signature}")

    # Проверка подписи:
    print(f"Validating signature")
    r, s = signature
    assert 1 <= s <= n - 1
    assert 1 <= r <= n - 1
    w = crypto_lib.inverse_in_group(s, n)
    u1 = (t * w) % n
    u2 = (r * w) % n
    x0, y0 = u1 * p + u2 * q
    v = x0 % n
    print(f"w = {w}; u1 = {u1}; u2 = {u2}; x0 = {x0}; v = {v}")
    valid = v == r
    print("OK" if valid else "Invalid!")

if __name__ == "__main__":
    main()


from turtle import Turtle

class StateOnMap(Turtle):

    def __init__(self, name, coord):
        super().__init__()
        self.penup()
        self.hideturtle()
        self.goto(coord)
        self.write(name, move=False, align='center', font=('Arial', 10, 'normal'))


from datetime import datetime
from chalicelib import s3
from chalicelib.constants import EVENT_ARRIVAL, EVENT_DEPARTURE

import itertools

DATE_FORMAT_MASSDOT = "%Y-%m-%d %H:%M:%S"
DATE_FORMAT_OUT = "%Y-%m-%dT%H:%M:%S"

def pairwise(iterable):
    # an itertools recipe from the docs
    # "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)

def unique_everseen(iterable, key=None):
    # an itertools recipe from the docs
    # "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in itertools.filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

def dwells(stop_ids, sdate, edate):
    rows_by_time = s3.download_events(sdate, edate, stop_ids)

    dwells = []
    for maybe_an_arrival, maybe_a_departure in pairwise(rows_by_time):
        # Look for all ARR/DEP pairs for same trip id
        if (
            maybe_an_arrival["event_type"] in EVENT_ARRIVAL
            and maybe_a_departure["event_type"] in EVENT_DEPARTURE
            and maybe_an_arrival["trip_id"] == maybe_a_departure["trip_id"]
        ):
            dep_dt = datetime.strptime(maybe_a_departure["event_time"], DATE_FORMAT_MASSDOT)
            arr_dt = datetime.strptime(maybe_an_arrival["event_time"], DATE_FORMAT_MASSDOT)
            delta = dep_dt - arr_dt
            dwells.append(
                {
                    "route_id": maybe_a_departure["route_id"],
                    "direction": int(maybe_a_departure["direction_id"]),
                    "arr_dt": arr_dt.strftime(DATE_FORMAT_OUT),
                    "dep_dt": dep_dt.strftime(DATE_FORMAT_OUT),
                    "dwell_time_sec": delta.total_seconds(),
                }
            )

    return dwells

def headways(stop_ids, sdate, edate):
    rows_by_time = s3.download_events(sdate, edate, stop_ids)

    only_departures = filter(lambda row: row["event_type"] in EVENT_DEPARTURE, rows_by_time)

    headways = []
    for prev, this in pairwise(only_departures):
        if this["trip_id"] == prev["trip_id"] != "":
            # in rare cases, same train can arrive and depart twice
            # Here, we should skip the headway
            # (though if trip_id is empty, we don't know).
            continue
        this_dt = datetime.strptime(this["event_time"], DATE_FORMAT_MASSDOT)
        prev_dt = datetime.strptime(prev["event_time"], DATE_FORMAT_MASSDOT)
        delta = this_dt - prev_dt
        headway_time_sec = delta.total_seconds()

        # Throw out any headways > 120 min
        if headway_time_sec > 120 * 60:
            continue

        benchmark_headway = this.get("scheduled_headway")
        if benchmark_headway == "":
            benchmark_headway = None

        headways.append(
            {
                "route_id": this["route_id"],
                "direction": this["direction_id"],
                "current_dep_dt": this["event_time"],
                "headway_time_sec": headway_time_sec,
                "benchmark_headway_time_sec": benchmark_headway,
            }
        )

    return headways

def travel_times(stops_a, stops_b, sdate, edate):
    rows_by_time_a = s3.download_events(sdate, edate, stops_a)
    rows_by_time_b = s3.download_events(sdate, edate, stops_b)

    departures = filter(lambda event: event["event_type"] in EVENT_DEPARTURE, rows_by_time_a)
    # we reverse arrivals so that if the same train arrives twice (this can happen),
    # we get the earlier time.
    arrivals = {
        (event["service_date"], event["trip_id"]): event
        for event in reversed(list(rows_by_time_b))
        if event["event_type"] in EVENT_ARRIVAL and event["trip_id"] != ""
    }

    travel_times = []
    for departure in unique_everseen(departures, key=lambda x: (x["service_date"], x["trip_id"])):
        arrival = arrivals.get((departure["service_date"], departure["trip_id"]))
        if arrival is None:
            continue
        dep = departure["event_time"]
        arr = arrival["event_time"]

        dep_dt = datetime.strptime(dep, DATE_FORMAT_MASSDOT)
        arr_dt = datetime.strptime(arr, DATE_FORMAT_MASSDOT)
        delta = arr_dt - dep_dt
        travel_time_sec = delta.total_seconds()

        if travel_time_sec < 0:
            continue

        # benchmark calculation:
        # not every file will have the scheduled_tt field, so we use get.
        sched_arr = arrival.get("scheduled_tt")
        sched_dep = departure.get("scheduled_tt")
        try:
            # sched values may be None or ''
            benchmark = float(sched_arr) - float(sched_dep)
        except (TypeError, ValueError):
            benchmark = None

        travel_times.append(
            {
                "route_id": departure["route_id"],
                "direction": int(departure["direction_id"]),
                "dep_dt": dep_dt.strftime(DATE_FORMAT_OUT),
                "arr_dt": arr_dt.strftime(DATE_FORMAT_OUT),
                "travel_time_sec": travel_time_sec,
                "benchmark_travel_time_sec": benchmark,
            }
        )

    return travel_times


import pandas as pd
import requests
from pandas.io.json import json_normalize
import datetime
from dateutil import parser
import co2_converter_common as ccc
from dataiku.customrecipe import get_recipe_config

# Get input parameters:
input_df, output_dataset, columns_names = ccc.get_input_output()
coordinates = ccc.get_coordinates(input_df)
date_column_name = ccc.get_date_column_name(input_df)
extracted_geopoint, extracted_longitude, extracted_latitude = ccc.get_geopoint_column_names(columns_names)
user_selected_columns = get_recipe_config().get('user_selected_columns')

API_ENDPOINT = 'https://api.electricitymap.org/v3/power-breakdown/past-range'
API_TOKEN = ccc.get_api_token()

# Date is not in the future:
input_df[date_column_name] = pd.to_datetime(input_df[date_column_name], format="%Y-%m-%dT%H:%M:%S.%fZ", utc=True)
now = datetime.datetime.utcnow()
if max(input_df[date_column_name]).timestamp() > now.timestamp():
    raise Exception("Date is in the future. Please check your input dataset or use the CO2 forecast component.")

# Parse Geopoint to longitude and latitude:
input_df[extracted_geopoint] = input_df[coordinates].apply(lambda point: ccc.parse_wkt_point(point))
input_df[extracted_longitude] = input_df[extracted_geopoint].apply(lambda point: point[0])
input_df[extracted_latitude] = input_df[extracted_geopoint].apply(lambda point: point[1])

# setup request
session = requests.session()

# GroupBy latitude, longitude to retrieve only one API call per coordinates:
dates_per_unique_latitude_longitudes = input_df.groupby([extracted_longitude, extracted_latitude])[date_column_name].unique()
data = pd.DataFrame()

# for each unique location location and with 10 days chunks (to avoid API limit):

for index_latitude_longitude, dates in enumerate(dates_per_unique_latitude_longitudes):

    # Parse dates:
    now = datetime.datetime.utcnow()
    max_date = parser.parse(str(dates.max()))
    min_date = parser.parse(str(dates.min()))

    # Get only the day from the dates:
    min_date_day = min_date.strftime("%Y-%m-%d")
    max_date_day = max_date.strftime("%Y-%m-%d")

    # As the API is limited to 10 days, I create chunks of dates:
    chunked_dates = ccc.date_chunk(min_date_day, max_date_day, 10)

    for index_chunked_dates in range(len(chunked_dates)):
        params = {
            'lat': dates_per_unique_latitude_longitudes.index[index_latitude_longitude][1],
            'lon': dates_per_unique_latitude_longitudes.index[index_latitude_longitude][0],
            'start': chunked_dates[index_chunked_dates][0],
            'end': chunked_dates[index_chunked_dates][-1]
        }

        # make request and create df dataframe with response from API:
        response = session.get(API_ENDPOINT, params=params, auth=('auth-token', API_TOKEN))
        response_json = response.json()
        chunked_data = json_normalize(response_json['data'])
        chunked_data['latitude'] = dates_per_unique_latitude_longitudes.index[index_latitude_longitude][1]
        chunked_data['longitude'] = dates_per_unique_latitude_longitudes.index[index_latitude_longitude][0]
        data = data.append(chunked_data, ignore_index=True)

    # Filtering on the columns selected by the user:
    data_to_return = data[['datetime', 'latitude', 'longitude']]
    for user_selected_column in user_selected_columns:
        column_list = [column for column in data.columns if column.startswith(user_selected_column)]
        user_selected_data = data[column_list]
        data_to_return = pd.concat((data_to_return, user_selected_data), axis=1)

    # rename datetime in e-mix_date_time:
    data_to_return = data_to_return.rename(columns={
        "datetime": "e-mix_date_time", "latitude": extracted_latitude, "longitude": extracted_longitude}
    )

    # join on date with input_df:
    # convert emix_date_time to datetime format:
    data_to_return['e-mix_date_time'] = pd.to_datetime(data_to_return['e-mix_date_time'])

    # convert DateColName to datetime format:
    input_df[date_column_name] = pd.to_datetime(input_df[date_column_name])

    output_df = ccc.merge_w_nearest_keys(input_df, data_to_return, date_column_name, 'e-mix_date_time', by=[extracted_latitude, extracted_longitude])

    # drop extracted columns (not needed):
    output_df.drop([extracted_geopoint, extracted_latitude, extracted_longitude], axis=1, inplace=True)

# ##################################### output ######################################

# Write output
output_dataset.write_with_schema(output_df)


MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

def is_available_resources(drink):
    drink_resources = MENU[drink]["ingredients"]
    for item in drink_resources:
        if drink_resources[item] > resources[item]:
            print(f"Sorry there is not enough {item}.")
            return False
    return True

def insert_coins():
    print("Please insert coins? ")
    quarters = int(input("how many quarters?: "))
    total_q = quarters * 0.25
    dimes = int(input("how many dimes?: "))
    total_d = dimes * 0.10
    nickles = int(input("how many nickles?: "))
    total_n = nickles * 0.05
    pennies = int(input("how many pennies?: "))
    total_p = pennies * 0.01
    total = total_d + total_q + total_n + total_p
    return total

def if_enough_coins(user_coins, cost):
    if user_coins >= cost:
        print(f"Here is ${(user_coins - cost):.2f} in change.")
        return True
    else:
        print("Sorry that's not enough money. Money refunded.")
        print(f"You need {cost - user_coins} to order the product")
        return False

def make_coffee(drink):
    global profit
    profit += drink_cost
    withdrew_resources(drink)

    print(f"Here is your {drink} \N{hot beverage} Enjoy!")

def withdrew_resources(drink):
    list_ingredients = MENU[drink]["ingredients"]

    for ingredient in list_ingredients:
        withdraw_resources = list_ingredients[ingredient]
        resources[ingredient] -= withdraw_resources

def shows_the_resources(resources_in_machine, current_profit):
    result = f"Water: {resources_in_machine['water']}\n" \
             f"Milk: {resources_in_machine['milk']}\n" \
             f"Coffee: {resources_in_machine['coffee']}\n" \
             f"Money: ${current_profit}"
    return result

profit = 0
order = ''

while order != "off":
    print("You can order from the Coffee Machine named Fernando: Espresso: $1.50, Latte: $2.50, Cappuccino: $3.0")
    order = input("What would you like? (espresso/latte/cappuccino): ").lower()

    if order == "espresso" or order == "latte" or order == "cappuccino":
        drink_cost = MENU[order]["cost"]
        if is_available_resources(order):
            if if_enough_coins(insert_coins(), drink_cost):
                make_coffee(order)
    elif order == "report":
        print(shows_the_resources(resources, profit))
    elif order == "off":
        print("The machine is currently not available.")
        break
    else:
        print("Please choice between espresso/latte/cappuccino!!!")

15-day - Coffee Machine/Coffee Machine.py


from flask import Flask, render_template, request, redirect, flash
from flask_mysqldb import MySQL
from scripts.scraper import getReddit
import yaml

app = Flask(__name__)

# Configure db
db = yaml.safe_load(open("db.yaml"))
app.config['MYSQL_HOST'] = db['mysql_host']
app.config['MYSQL_USER'] = db['mysql_user']
app.config['MYSQL_PASSWORD'] = db['mysql_password']
app.config['MYSQL_DB'] = db['mysql_db']

# app.config['MYSQL_HOST'] = 'aws-lab.cu3ottuyfwbk.us-east-1.rds.amazonaws.com'
# app.config['MYSQL_USER'] = 'admin'
# app.config['MYSQL_PASSWORD'] = '29!nt$XELNE1'
# app.config['MYSQL_DB'] = 'flaskapp'

mysql = MySQL(app)

@app.route('/')
def index():
        # data = getReddit() #getting back the pandas dataframe of json data
        # if data != None:
        #     return render_template('index.html', data=data)
        # else:
        #     return render_template("error.html")

        data = getReddit() #getting back the pandas dataframe of json data
        return render_template('index.html', data=data)
        # if request.method == 'POST':
        #     # Fetch form data
        #     userDetails = request.form
        #     if userDetails['name'] != '' and userDetails['email'] != '': #if both fields are not empty
        #         name = userDetails['name']
        #         email = userDetails['email']
        #         cur = mysql.connection.cursor()
        #         cur.execute("INSERT INTO users(name, email) VALUES(%s, %s)",(name, email))
        #         mysql.connection.commit()
        #         cur.close()
        #         return "
Success
"
        # return render_template('index.html')

@app.route('/newsletter', methods=['GET', 'POST'])
def newsletter():
        if request.method == 'POST':
            # Fetch form data
            userDetails = request.form
            if userDetails['name'] != '' and userDetails['email'] != '': #if both fields are not empty
                name = userDetails['name']
                email = userDetails['email']
                cur = mysql.connection.cursor()
                cur.execute("INSERT INTO users(name, email) VALUES(%s, %s)",(name, email))
                mysql.connection.commit()
                cur.close()
                # flash("You have signed up for the newsletter! Thank you!")
                return redirect(request.url)

        return render_template('newsletter.html')

if __name__ == "__main__":
        app.run()


import tkinter as tk

def onclick():
    make = entry1.get()
    dictionary['car'] = make
    model = entry2.get()
    dictionary['model'] = model
    color = entry3.get()
    dictionary['color'] = color
    wheelcolor = entry4.get()
    dictionary['wheelcolor'] = wheelcolor
    print(dictionary)
    print(printYourCar())

def printYourCar():
    return "You want " + dictionary["car"] + " " + dictionary["model"] + " in " + dictionary['color'] + " with " + dictionary['wheelcolor'] + " wheels!"

dictionary = {'car' : "", 'model': "", 'color': "", 'wheelcolor': ""}
#This creates new window
window = tk.Tk()
window.title("Car Cofigurator")
#First Line on the window
greeting = tk.Label(text="This is my car configurator!",)
greeting.pack()
#Second Line on the window
label1 = tk.Label(text="Write down what car you want. (Example: Audi, BMW, Mercedes)")
#Entry field
entry1 = tk.Entry()
#
label2 = tk.Label(text="Write down what model do you want. (Example: RS4, S350d, M5)")
entry2 = tk.Entry()
#
label3 = tk.Label(text="Write down what color of the car you want. (Example: red, blue, orange)")
entry3 = tk.Entry()
#
label4 = tk.Label(text="Write down what color of the wheels you want. (Example: red, black, grey, white)")
entry4 = tk.Entry()
#
btn1 = tk.Button(window, text="Next", command=onclick)

label1.pack()
entry1.pack()

label2.pack()
entry2.pack()

label3.pack()
entry3.pack()

label4.pack()
entry4.pack()

btn1.pack()

window.mainloop()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('book', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='sequencebook',
            name='number',
            field=models.CharField(max_length=10, null=True, verbose_name='\u041d\u043e\u043c\u0435\u0440 \u0432 \u0441\u0435\u0440\u0438\u0438', blank=True),
            preserve_default=True,
        ),
    ]


from world import World
import json
import cProfile

unit_testing = False

def main():

    if unit_testing:
        world = World(30, 1, 1)
        world.unit_test()
    else:
        with open("species.json", "w") as speciesFile:
            speciesFile.write("{}")
        world = World(50, 1, 1000)
        while True:
            if world.make_moves():
                continue
            else:
                generations = {}
                with open("species.json", "r") as speciesFile:
                    generations = json.load(speciesFile)
                    generations[f"generation{world.generation}"] = world.current_generation_best_move_history[f"generation{world.generation}"]

                with open("species.json", "w") as speciesFile:
                    json.dump(generations, speciesFile, indent=4)

#main()

cProfile.run("main()", sort="cumtime")


'''
Write a function called list_manipulator which receives a list of numbers as first parameter and different amount of other parameters. The second parameter might be "add" or "remove". The third parameter might be "beginning" or "end". There might or might not be any other parameters (numbers):
 - In case of "add" and "beginning",
    add the given numbers to the beginning of the given list of numbers
    and return the new list

 - In case of "add" and "end",
    add the given numbers to the end of the given list of numbers
    and return the new list

 - In case of "remove" and "beginning"
    - If there is another parameter (number), remove that amount of numbers from
        the beginning of the list of numbers.
    - If there are no other parameters, remove only the first element of the list.
    - Finaly, return the new list

 - In case of "remove" and "end"
    - If there is another parameter (number), remove that amount of numbers from
    the end of the list of numbers.
    - Otherwise if there are no other parameters, remove only the last element of the list.
    - Finaly, return the new list
'''

from collections import deque

def list_manipulator(nums, *args):
    numbers = deque(nums)
    parameters = len(args)
    action = args[0]
    direction = args[1]

    if action == 'add' and direction == 'beginning':
        if parameters > 2:
            items = []
            for a in args[2:]:       # get all extra numbers
                items.append(a)

            for i in items[::-1]:    # add them to front starting from the last one
                numbers.appendleft(i)
        return list(numbers)

    elif action == 'add' and direction == 'end':
        if parameters > 2:
            items = []
            for a in args[2:]:      # get all extra numbers
                items.append(a)

            for i in items:
                numbers.append(i)   # add them to original list one by one
        return list(numbers)

    elif action == 'remove' and direction == 'beginning':
        if parameters > 2:
            items = args[2]
            for _ in range(items):
                numbers.popleft()
        else:
            numbers.popleft()
        return list(numbers)

    elif action == 'remove' and direction == 'end':
        if parameters > 2:
            items = args[2]
            for _ in range(items):
                numbers.pop()
        else:
            numbers.pop()
        return list(numbers)

print(list_manipulator([1,2,3], "remove", "end"))
print(list_manipulator([1,2,3], "remove", "beginning"))

print(list_manipulator([1,2,3], "add", "beginning", 20))

print(list_manipulator([1,2,3], "add", "end", 30))

print(list_manipulator([1,2,3], "remove", "end", 2))

print(list_manipulator([1,2,3], "remove", "beginning", 2))

print(list_manipulator([1,2,3], "add", "beginning", 20, 30, 40))
print(list_manipulator([1,2,3], "add", "end", 30, 40, 50))


#!/bin/env python

"""
    Python 3
    Go through each tab of Nimal's excel sheet, create database input files
"""

import os
import pandas as pd
import numpy as np
from datetime import date

def main():
    input_dir = '../Jenny_ucl/ucl_covid19'
    fn = os.path.join(input_dir, 'Covid-19_version4_Hayley.xls')
    # creating pandas.io.excel.ExcelFile object
    xl = pd.ExcelFile(fn)
    #  generate a dictionary of DataFrames
    dfs = {sh:xl.parse(sh) for sh in xl.sheet_names}

    for sheet_name in dfs.keys():
        print(sheet_name)

    """
    1. Codes
    """
    df_codes = dfs['Codes']
    df_codes['source'] = 'Codes'
    # print(df_codes['cat'].unique())
    df_codes['cat_new'] = df_codes['cat'].astype(str).str.replace('(', '').astype(int)
    df_codes['label'] = df_codes['label'].str.rstrip()
    # add order
    df_codes['codes_order'] = df_codes.groupby('label').cumcount() + 1

    df_codes = df_codes.sort_values(['label', 'codes_order'])

    df_codes.drop(['cat'], axis=1, inplace=True)
    df_codes.rename(columns={'cat_new': 'Value', 'val': 'Category', 'label': 'Label'}, inplace=True)

    """
    2. Sequences
    """
    df_sequences = dfs['Sequence_start_end']
    df_sequences['source'] = 'Sequences'
    # add sequence id
    df_sequences['section_id'] = df_sequences.index + 1
    df_sequences.rename(columns={'Block name': 'label', 'End_position': 'end_position'}, inplace=True)

    """
    3. Statements
    """
    df_statements = dfs['Statements']
    df_statements['source'] = 'Statements'

    """
    4. Questions
    """
    df_questions = dfs['Questions']
    df_questions['source'] = 'Questions'
    # sort by column "Number"
    df_questions = df_questions.sort_values('Order')
    # replace 'qc' with 'qi' in Label column
    df_questions['label'] = df_questions['label'].str.replace('qc_', 'qi_')

    # deal with same label name
    df_questions['label'] = df_questions['label'] + '_' + df_questions.groupby('label').cumcount().astype(str)
    df_questions['label'] = df_questions['label'].str.replace('_0', '')

    df_questions['Response_domain'] = df_questions['Response_domain'].str.rstrip()

    """
    5. Response Domain
    """
    df_response = dfs['Response_'].loc[:, ['Label', 'Type', 'Numeric_Type/Datetime_type', 'Min', 'Max']]
    df_response['source'] = 'Response'
    # convert to integer
    df_response['Min'] = df_response['Min'].apply(lambda x: None if pd.isnull(x) else '{0:.0f}'.format(pd.to_numeric(x)))
    df_response['Max'] = df_response['Max'].apply(lambda x: None if pd.isnull(x) else '{0:.0f}'.format(pd.to_numeric(x)))

    """
    Find parent and position
    """
    df_questions_p = df_questions.loc[:, ['Order', 'label', 'source']]
    df_questions_p.rename(columns={'Order': 'start_position'}, inplace=True)
    df_statements_p = df_statements.loc[:, ['Order', 'Label', 'source']]
    df_statements_p.rename(columns={'Order': 'start_position', 'Label': 'label'}, inplace=True)

    df_all = pd.concat([df_sequences.loc[:, ['start_position', 'end_position', 'label', 'source']], df_questions_p, df_statements_p])
    df_all = df_all.sort_values('start_position').reset_index()

    df_all['above_label'] = df_all.apply(lambda row: row['label'] if row['source'] == 'Sequences' else None, axis=1)
    df_all.loc[:, 'above_label'] = df_all.loc[:, 'above_label'].ffill()
    df_all['parent_type'] = 'CcSequence'
    df_all['Position'] = df_all.groupby('above_label').cumcount()

    df_all.to_csv(os.path.join(input_dir, 'ALL_ORDER.csv'), encoding='utf-8', index=False)

    # csv
    df_questions_new = pd.merge(df_questions, df_all, how='left', left_on = ['Order', 'label'], right_on=['start_position', 'label'])
    questions_keep = ['label', 'Literal', 'Instructions', 'Response_domain', 'above_label', 'parent_type', 'Position']
    df_questions_new[questions_keep].to_csv(os.path.join(input_dir, 'questions.csv'), encoding='utf-8', index=False, sep=';')

    df_statements_new = pd.merge(df_statements, df_all, how='left', left_on = ['Order', 'Label'], right_on=['start_position', 'label'])
    statements_keep = ['Label', 'Literal', 'above_label', 'parent_type', 'Position']
    df_statements_new[statements_keep].to_csv(os.path.join(input_dir, 'statements.csv'), encoding='utf-8', index=False, sep=';')

    df_sequences.drop(['source', 'start_position', 'end_position'], 1).to_csv(os.path.join(input_dir, 'sequences.csv'), encoding='utf-8', index=False, sep=';')
    df_response.drop('source', 1).to_csv(os.path.join(input_dir, 'response.csv'), encoding='utf-8', index=False, sep=';')
    df_codes.drop('source', 1).to_csv(os.path.join(input_dir, 'codes.csv'), encoding='utf-8', index=False, sep=';')

if __name__ == '__main__':
    main()


import sentence_classification

json1 = sentence_classification.load_data('story.json')
json2 = sentence_classification.load_data('story1.json')

data = sentence_classification.load_data('story.json')

def lower_strings(data):
    for value in data['data']:
        for i in range(len(value['story'])):
            for j in range(len(value['story'][i]['patterns'])):
                value['story'][i]['patterns'][j] = value['story'][i]['patterns'][j].lower()
            for j in range(len(value['story'][i]['responses'])):
                value['story'][i]['responses'][j] = value['story'][i]['responses'][j].lower()

lower_strings(data)

def get_response(input_text, data):
    for value in data['data']:
        for i in range(len(value['story'])):
            for j in range(len(value['story'][i]['patterns'])):
                if input_text == value['story'][i]['patterns'][j]:
                    if value['story'][i]['responses']:
                        return value['story'][i]['responses'][0]

    return "NULL"

def add_response(input_text, result, tag, data):

    # check result
    for value in data['data']:
        for i in range(len(value['story'])):
            for j in range(len(value['story'][i]['responses'])):
                if result == value['story'][i]['responses'][j]:
                    print("This response is exist")
                    value['story'][i]['patterns'].append(input_text)
                    sentence_classification.write_data('story.json', data)
                    return
    # check tag
    for value in data['data']:
        if tag == value['tag']:
            value['story'].append(
                {"patterns": [input_text], "responses": [result]})
            sentence_classification.write_data('story.json', data)
            return
    # add new tag
    data['data'].append(
        {"tag": tag, "story": [{"patterns": [input_text], "responses": [result]}]})
    sentence_classification.write_data('story.json', data)

def get_text(input_text):

    if input_text == "exit":
        return "exit"

    result = get_response(input_text, data)

    if result == "NULL":
        print("Don't have this story")

    else:
        print("Bot: ", result)

    return result

for value in json2['data']:
    tag = value['tag']
    patterns = ""
    responses = ""
    for i in range(len(value['story'])):
        for j in range(len(value['story'][i]['patterns'])):
            patterns = value['story'][i]['patterns'][j]
            responses = value['story'][i]['responses'][0]
            print("User: ", patterns)
            result = get_text(patterns)
            if result == "NULL":
                add_response(patterns, responses, tag, data)
                print("Add new story")
            else:
                print(result)

print("Done")


def my_sum(a,b):
    result = a + b
    return result

a = input('number1?')
b = input('number2?')

while not (a.isdigit() & b.isdigit()):
    a = input('number1?')
    b = input('number2?')

a = int(a)
b = int(b)

print(my_sum(a, b))


N: int = int(input())
valores = input().split(" ")
for i in range(len(valores)):
    k = int(valores[i])
menor = min(valores)
posicao: int = 0
for i in range(len(valores)):
    if valores[i] == menor:
        posicao += i
print(f"Menor valor: {menor}")
print(f"Posicao: {posicao}")


from pytube import Playlist, YouTube
import re
from datetime import timedelta
from tqdm import tqdm

# Ask user for YouTube playlist URL
playlist_url = input("Enter YouTube playlist URL: ")

# Extract playlist ID from URL
playlist_id = re.findall(r"(?<=list=)[^&$]+", playlist_url)[0]

# Initialize playlist object
playlist = Playlist(f"https://www.youtube.com/playlist?list={playlist_id}")

print("Processing playlist...")

# Initialize variables
total_duration = 0
num_processed = 0

# Loop through each video in playlist
for url in tqdm(playlist.video_urls):
    yt = YouTube(url)
    if yt:
        duration = yt.length
        total_duration += duration
        num_processed += 1
    else:
        print(f"Could not process video: {url}")

# Format total duration
total_duration = str(timedelta(seconds=total_duration))

# Print total duration
print(f"Total duration: {total_duration}")


import asyncio
import aiofiles
import json
from os.path import exists

from ..exceptions import WiserExtraConfigError

class _WiserExtraConfig:
    def __init__(self, config_file: str, hub_name: str):
        self._config_file = config_file + "_" + hub_name
        self._config = {}

    async def async_load_config(self):
        if exists(self._config_file):
            async with aiofiles.open(self._config_file, mode="r") as config_file:
                try:
                    contents = await config_file.read()
                    if contents:
                        self._config = json.loads(contents)
                    config_file.close()

                except (
                    OSError,
                    EOFError,
                    TypeError,
                    AttributeError,
                    json.JSONDecodeError,
                ) as ex:
                    raise WiserExtraConfigError("Error loading extra config file")
        else:
            await self.async_update_config("Info", "Version", "1.0.0")
        return

    async def async_update_config(self, section: str, key: str, value):
        # If section doesn't exist
        if section not in self._config:
            self._config[section] = {}

        # If key not in section
        if key and value:
            if self._config[section].get(key):
                self._config[section][key].update(value)
            else:
                self._config[section][key] = value

        await self.async_write_config()

    async def async_remove_config(self, section: str, key: str):
        if key and self._config[section].get(key):
            del self._config[section][key]

        await self.async_write_config()

    async def async_write_config(self):
        # Write to config file
        async with aiofiles.open(self._config_file, mode="w") as config_file:
            try:
                await config_file.write(json.dumps(self._config, indent=2))
                config_file.close()
            except (
                OSError,
                EOFError,
                TypeError,
                AttributeError,
                json.JSONDecodeError,
            ) as ex:
                raise WiserExtraConfigError("Error writing to extra config file")

    # @property
    def config(self, section: str = None, key: str = None):
        try:
            if section:
                if key:
                    return self._config[section][key]
                else:
                    return self._config[section]
            else:
                return self._config
        except:
            return None


def solve():
    d = "0"
    i = 1
    while len(d) <= 1000000:
        d += str(i)
        i += 1
    i = 1

    res = 1
    while i <= 1000000:
        res *= int(d[i])
        i *= 10
    return res

print(solve())


def genMap():
	htmlID = "\"mapdiv\""

	json = '{ \n\
		"type": "map",\n\
		"theme": "light",\n\
		\n\
		colorSteps: 10,\n\
	  /**\n\
	   * create data provider object\n\
	   * map property is usually the same as the name of the map file.\n\
	   * getAreasFromMap indicates that amMap should read all the areas available\n\
	   * in the map data and treat them as they are included in your data provider.\n\
	   * in case you dont set it to true, all the areas except listed in data\n\
	   * provider will be treated as unlisted.\n\
	   */\n\
	\n\
		"dataProvider": {\n\
			"map": "brazilLow",\n\
			areas: [{\n\
				id: "BR-AC",\n\
				value: 167499,\n\
				balloonText: "Acre: 167.499"\n\
			}, {	\n\
				id: "BR-AL",\n\
				value: 356632,\n\
				balloonText: "Alagoas: 356.632"\n\
			}, {	\n\
	\n\
				id: "BR-AM",\n\
				value: 361234,\n\
				balloonText: "Amazonas: 361.234"\n\
			}, {	\n\
				id: "BR-AP",\n\
				value: 77702,\n\
				balloonText: "Amapa: 77.702"\n\
			}, {\n\
				id: "BR-BA",\n\
				value: 1284185,\n\
				balloonText: "Bahia: 1.284.185"\n\
			}, {\n\
				id: "BR-CE",\n\
				value: 638115,\n\
				balloonText: "Ceara: 638.115"\n\
			}, {\n\
				id: "BR-DF",\n\
				value: 563830,\n\
				balloonText: "Distrito Federal: 563.830"\n\
			}, {\n\
				id: "BR-ES",\n\
				value: 567421,\n\
				balloonText: "Espirito Santo: 567.421"\n\
			}, {\n\
				id: "BR-GO",\n\
				value: 771157,\n\
				balloonText: "Goias: 771.157"\n\
			}, {\n\
				id: "BR-MA",\n\
				value: 534824,\n\
				balloonText: "Maranhao: 534.824"\n\
			}, {\n\
				id: "BR-MG",\n\
				value: 1554511,\n\
				balloonText: "Minas Gerais: 1.554.511"\n\
			}, {\n\
				id: "BR-MS",\n\
				value: 257805,\n\
				balloonText: "Mato Grosso do Sul: 257.805"\n\
			}, {\n\
				id: "BR-MT",\n\
				value: 219908,\n\
				balloonText: "Mato Grosso: 219.908"\n\
			}, {\n\
				id: "BR-PA",\n\
				value: 627012,\n\
				balloonText: "Para: 627.012"\n\
			}, {\n\
				id: "BR-PB",\n\
				value: 393390,\n\
				balloonText: "Paraiba: 393.390"\n\
			}, {\n\
				id: "BR-PE",\n\
				value: 2310700,\n\
				balloonText: "Pernambuco: 2.310.700"\n\
			}, {\n\
				id: "BR-PI",\n\
				value: 246121,\n\
				balloonText: "Piaui: 246.121"\n\
			}, {\n\
				id: "BR-PR",\n\
				value: 860685,\n\
				balloonText: "Parana: 860.685"\n\
			}, {\n\
				id: "BR-RJ",\n\
				value: 2590871,\n\
				balloonText: "Rio de Janeiro: 2.590.871"\n\
			}, {\n\
				id: "BR-RN",\n\
				value: 286061,\n\
				balloonText: "Rio Grande do Norte: 286.061"\n\
			}, {\n\
				id: "BR-RO",\n\
				value: 86148,\n\
				balloonText: "Rondonia: 86.148"\n\
			}, {\n\
				id: "BR-RR",\n\
				value: 46936,\n\
				balloonText: "Roraima: 46.936"\n\
			}, {\n\
				id: "BR-RS",\n\
				value: 732148,\n\
				balloonText: "Rio Grande do Sul: 732.148"\n\
			}, {\n\
				id: "BR-SC",\n\
				value: 475599,\n\
				balloonText: "Santa Catarina: 475.599"\n\
			}, {	\n\
				id: "BR-SE",\n\
				value: 203188,\n\
				balloonText: "Sergipe: 203.188"\n\
			}, {	\n\
				id: "BR-SP",\n\
				value: 5761174,\n\
				balloonText: "Sao Paulo: 5.761.174"\n\
			}, {\n\
				id: "BR-TO",\n\
				value: 150568,\n\
				balloonText: "Tocantins: 150.568"\n\
			}]\n\
		},\n\
	\n\
	  /** \n\
	   * create areas settings\n\
	   * autoZoom set to true means that the map will zoom-in when clicked on the area\n\
	   * selectedColor indicates color of the clicked area.\n\
	   */\n\
	   \n\
		"areasSettings": {\n\
			"autoZoom": true,\n\
		},\n\
	\n\
		valueLegend: {\n\
	   		right: 10,\n\
	    		minValue: "poucos votos",\n\
	    		maxValue: "muitos votos!"\n\
		},\n\
	\n\
	  /**\n\
	   * lets say we want a small map to be displayed, so lets create it\n\
	   */\n\
	   \n\
		"smallMap": {}\n\
	}'

	lines = ['var icon = "M21.25,8.375V28h6.5V8.375H21.25zM12.25,28h6.5V4.125h-6.5V28zM3.25,28h6.5V12.625h-6.5V28z";\n\n','AmCharts.makeChart('+htmlID+','+json+');']

	worldmap = open("./static/assets/js/worldmap.js",'w')

	worldmap.writelines(lines)

	worldmap.close()


import FocusTimer

def continueOr(study, short_break, long_break, countSB):
    choice = (input('Would you like to continue focusing? (y/n)\n'))
    if countSB > 3:
        countSB = 0
    else:
        countSB += 1
    while True:
        print(choice)
        if choice == 'y':
            print('“First say to yourself what you would be; and then do what you have to do.”')
            return FocusTimer.timer(study, short_break, long_break, countSB)
        elif choice == 'n':
            print('"You have power over your mind — not outside events. Realize this, and you will find strength."')
            break
        else:
            choice = input('Please enter y/n')


# -*- coding:utf-8 -*-
"""进制转换"""

'''
2进制满2进1，7进制满7进1...

188的10进制数为188
188 = 10**0 * 8 + 10**1 * 8 + 10**2 * 1
2828的七进制数为11150
2828 = 7**0 * 0 + 7**1 * 5 + 7**2 * 1 + 7**3 * 1 + 7**4 * 1
'''

'''十进制转二进制'''
def transfer_binary(d):
    sli = []
    while d > 0:
        remainder = d % 2
        sli.append(str(remainder))
        d = d // 2
    return ''.join(sli[::-1])

'''十进制转任意进制'''
def transfer_common(d, base):
    digits = '0123456789ABCDEF'  # digits随着需要增加
    sli = []
    while d > 0:
        remainder = d % base
        sli.append(digits[remainder])
        d = d // base
    return ''.join(sli[::-1])

'''用递归实现整数转任意进制'''
def to_str(n, base):
    digits = '0123456789ABCDEF'
    if n < base:
        return digits[n]
    else:
        return to_str(n // base, base) + digits[n % base]

if __name__ == '__main__':
    print(transfer_common(100000, 16))
    print(to_str(100000, 16))


import torch.nn as nn
import torch

class BCE(nn.Module):
	def __init__(self, user_size, item_size, dim):
		super().__init__()
		self.user_embd = nn.Embedding(user_size, dim)
		self.item_embd = nn.Embedding(item_size, dim)
		# self.user_bias = nn.Embedding(user_size, 1)
		# self.item_bias = nn.Embedding(item_size, 1)

		nn.init.xavier_normal_(self.user_embd.weight)
		nn.init.xavier_normal_(self.item_embd.weight)
		# self.user_bias.weight.data.fill_(0.)
		# self.item_bias.weight.data.fill_(0.)

	def forward(self, u, i):
		user = self.user_embd(u)
		item = self.item_embd(i)
		# u_bias = self.user_bias(u)
		# i_bias = self.item_bias(i)
		# prediction = u_bias + i_bias + (user * item).sum(dim = -1)
		prediction = (user * item).sum(dim = -1)
		return prediction.squeeze()

class BPR(nn.Module):
	def __init__(self, user_size, item_size, dim):
		super().__init__()
		self.user_embd = nn.Embedding(user_size, dim)
		self.item_embd = nn.Embedding(item_size, dim)
		nn.init.xavier_normal_(self.user_embd.weight)
		nn.init.xavier_normal_(self.item_embd.weight)

	def forward(self, u, i, j):
		user = self.user_embd(u)
		item_i = self.item_embd(i)
		item_j = self.item_embd(j)

		prediction_i = (user * item_i).sum(dim = -1)
		prediction_j = (user * item_j).sum(dim = -1)

		return prediction_i, prediction_j


from microbit import running_time

time = running_time()
next_cmd_time = time + 5000
next_var_checked_time = time + 15000
next_var_requested_time = time + 20000

def send_name(name):
    print("Node to Hub -> I am {}".format(name))

def send_interval(interval):
    print("Node -> Set interval to {} ms.".format(interval))

def send_variable(name, value):
    print("Node to Hub -> Send var {} value: {}".format(name, value))

def read_variable(name):
    valueReturned = "value_from_{}".format(name)
    print("Node to Hub -> Read variable '{}': {}".format(name, valueReturned))
    return valueReturned;

def command_received(cmd):
    global next_cmd_time
    received = False
    now = running_time()
    if now > next_cmd_time:
        received = True
        next_cmd_time = now + 5000
        print("Hub to Node -> Command: {}".format(cmd))
    return received

def variable_changed(var):
    global next_var_checked_time
    changed = False
    now = running_time()
    if now > next_var_checked_time:
        changed = True
        next_var_checked_time = now + 15000
        print("Hub to Node -> Command: {}".format(var))
    return changed

def variable_requested(var):
    global next_var_requested_time
    requested = False
    now = running_time()
    if now > next_var_requested_time:
        requested = True
        next_var_requested_time = now + 20000
        print("Hub to Node -> Command: {}".format(var))
    return requested


# Импорт необходимых библиотек и модулей
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import email_config  # Импорт конфигурационного файла с электронной почтой

@pytest.fixture
def driver():
    # Инициализация веб-драйвера
    driver = webdriver.Chrome()
    yield driver
    driver.quit()  # Закрыть браузер после завершения теста

def test_password_recovery(driver):
    # Открыть сайт Ростелеком
    driver.get("https://lk.rt.ru")

    # Ожидание появления кнопки "Войти с паролем" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#standard_auth_btn"))
    ).click()

    # Ожидание появления кнопки "Забыл пароль" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#forgot_password"))
    ).click()

    # Клик по кнопке для восстановления пароля по почте
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#t-btn-tab-mail"))
    ).click()

    # Ожидание появления поля ввода почты, ввод почты
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "#username"))
    ).send_keys(email_config.email)  # Использование электронной почты из конфигурационного файла

    # Ожидание появления кнопки "Продолжить" и клик по ней
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#reset"))
    ).click()

    # Ожидание в 20 секунд для ручного ввода кода из почты
    print("Ожидание ввода кода из почты...")
    driver.implicitly_wait(20)  # Пауза в 20 секунд

    # Попытка найти сообщение об ошибке
    error_message_element = EC.presence_of_element_located((By.CSS_SELECTOR, "#form-error-message"))
    success_window_element = EC.presence_of_element_located((By.CSS_SELECTOR, "#page-right > div"))

    if WebDriverWait(driver, 10).until(error_message_element):
        error_message = driver.find_element(By.CSS_SELECTOR, "#form-error-message").text
        assert error_message == "Этот пароль уже использовался, укажите другой пароль", f"Ошибка: {error_message}"
    elif WebDriverWait(driver, 10).until(success_window_element):
        print("Процесс восстановления пароля прошел успешно")
    elif driver.current_url == "https://lk.rt.ru":
        print("Процесс восстановления пароля прошел успешно, пользователь перенаправлен на страницу авторизации")
    else:
        print("Неизвестное состояние, тест не удался")


# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
#

# %% [markdown]
# # Initial Bringup of the Sovol-SO1
#
# Much of these are taken from: https://github.com/Springwald/GCodePlotter/blob/main/code/Plotter/SovolS01Hardware.cs
#
# It was learnt that the Sovol-SO1 does not require a \r, only \n is used
#
# Shows up as a USB to serial device, with a 115200 baud rate.

# %%
# Requires pyserial library to be installed
import serial
serial_port = "/dev/ttyUSB0" # Set this to whatever tty serial dev
baudrate = 115200
serial = serial.Serial(serial_port, baudrate)

# %%
serial.close()

# %%
# Clear the buffer
serial.write(b"\n\r")
time.sleep(1.0)
res = serial.read_all()
print(res)

# %%
# Get some basic settings set
serial.write(b"G90\n\r") # Set to absolute mode
serial.write(b"G21\n\r") # Set to mm

# %%
# Pen Down
serial.write(b"M280P0S0\n\r")

# %%
# Pen Up (HIGH)

serial.write(b"M280P0S90\n\r")

# %%
# Pen Up (Normal)

serial.write(b"M280P0S30\n\r")

# %%
# Pause
serial.write(b"M280P0S0\n\r") # pen down
serial.write(b"G4 P2000\n\r") # P50 is the pause length in ms
serial.write(b"M280P0S30\n\r") # pen up

# %%
# Auto home
serial.write(b"G28\n\r")

# %%
# Go to position
x = 100
y = 120.0

max_y = 300 #mm
max_x = 300 #mm
sendstr = f"G1 X{x:0.3f} Y{y:0.3f}\n\r".encode('utf-8')
print(sendstr)
serial.write(sendstr)

# %%
# Travel speed
serial.write(b"G1 F10000\n\r")

# %%
#Paint Speed
# Travel speed
serial.write(b"G1 F3000\n\r")

# %%
import time
with SovolSO1("/dev/ttyUSB0") as plotter:
    plotter.autoHome()
    plotter.setTravelSpeed()
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((75.0,75.0))
    plotter.setDrawingSpeed()
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,100.0))
    plotter.moveTo((50.0,100.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((50.0,100.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((50.0,50.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((50.0,50.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,50.0))
    plotter.moveTo((75.0,75.0))
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.moveTo((100.0,50.0))
    plotter.setPen(PenState.DOWN)
    plotter.pause(50)
    plotter.moveTo((100.0,100.0))
    plotter.arcTo((50.0,100.0), (-25.0,-25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((50.0,50.0), (25.0,-25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((100.0,50.0), (25.0,25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.arcTo((100.0,100.0), (-25.0,25.0), Rotation.COUNTER_CLOCKWISE)
    plotter.setPen(PenState.HIGH_UP)
    plotter.pause(50)
    plotter.setTravelSpeed()
    plotter.moveTo((0.0,0.0))


#Cookie Recipe Calculator by Taylor Griffin
print('Cookie Recipe Calculator by Taylor Griffin')
#the values
cookie_amount = int(input('\nHow Many Cookies Do You Wish to Make? '))
cookies = 48
sugar = 1.5
butter = 1
flour = 2.75
#calculating the ingredients needed
total_sugar = (sugar * cookie_amount) /cookies
total_butter = (butter * cookie_amount) /cookies
total_flour = (flour * cookie_amount) /cookies
#Displaying the cups of ingredients needed formatted to the thousandths
print('\nNumber of Cookies =', cookie_amount, end='\n\n')
print('Cups of Sugar Needed =', format(total_sugar, ',.3f'))
print('Cups of Butter Needed =', format(total_butter, ',.3f'))
print('Cups of Flour Needed =', format(total_flour, ',.3f'), end='\n\n')


from django.conf.urls import patterns, url, include
from rest_framework.urlpatterns import format_suffix_patterns
import views

urlpatterns = patterns('',
                        url(r'^api-login/', include('rest_framework.urls', namespace='rest_framework')), # User accessible
                        url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
                        url(r'^api/kits/$', views.KitList.as_view()), # User accessible
                        url(r'^api/kits/(?P<pk>[0-9]+)/$', views.KitDetail.as_view()),
                        url(r'^api/samples/$', views.SampleDemoList.as_view()), # User accessible
                        url(r'^api/samples/(?P<pk>[0-9]+)/$', views.SampleDemoDetail.as_view()), # User accessible
                        url(r'^api/samples/endproducts/$', views.SampleList.as_view()),
                        url(r'^api/samples/endproducts/(?P<pk>[0-9]+)/$', views.SampleDetail.as_view()),
                        url(r'^api/ckits/$', views.CustomKitList.as_view()),
                        url(r'^api/ckits/(?P<pk>[0-9]+)/$', views.CustomKitDetail.as_view()), # Only User accessible
                        url(r'^api/ckits/create/$', views.CustomKitCreate.as_view()), # User accessible
                        url(r'^api/users/$', views.UserList.as_view()),
                        url(r'^api/users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()), # User accessible
                       )

urlpatterns = format_suffix_patterns(urlpatterns)


"""This module contains retry logic for bucardo replication.

The adding and dropping of triggers in bucardo causes the potential for a
customer-facing outage due to lock contention.  The retry module allows bucardo
to attempt to get a lock, gracefully move on if it can't get one quickly
enough, and retry later.

Classes exported:
Retry: Add or remove bucardo triggers with graceful timeout and retry options.
"""
import psycopg2

from plugins import Plugin
from plugins.bucardo import Bucardo

class Retry(Plugin):
    """Add or remove bucardo triggers with graceful timeout and retry options.

    The user defines a timeout setting in the config.  If bucardo is unable to get a
    lock in that time, it will stop trying and move on.

    Methods exported:
    add_triggers: add as many triggers as possible within user-defined limits
    drop_triggers: drop as many triggers as possible within user-defined limits
    install: install dependencies for bucardo retry logic
    """

    def __init__(self, cfg):
        """Create aliases for configuration settings and instantiate Bucardo.

        In the interests of keeping lines shorter and more readable, some variables are
        assigned to config settings.

        A Bucardo class is instantiated so that its methods can be invoked as part of
        the methods of the Retry class.

        Keyword arguments:
        cfg: contents of the config file as a dictionary

        Variables exported:
        bucardo_instance: an instantiation of the Bucardo class
        cfg: a dictionary containing the config file
        primary_user: the owner of the primary database
        repl_name: an arbitrary name given by the user to the replication task
        timeout: number of milliseconds to wait for a lock before giving up
        """
        super(Retry, self).__init__(cfg)
        self.bucardo_instance = Bucardo(cfg)
        self.cfg = cfg
        self.primary_user = cfg['databases']['primary']['database_owner']
        self.repl_name = cfg['bucardo']['replication_name']
        self.timeout = cfg['retry']['timeout']

    def _check_for_syncs(self):
        """Check the bucardo database to see if a sync exists.  Return True or False."""
        query = 'SELECT * FROM bucardo.sync LIMIT 1'
        conn = psycopg2.connect(self.bucardo_conn_pg_format)
        try:
            with conn.cursor() as cur:
                cur.execute(query)
                syncs = cur.fetchone()
        finally:
            conn.close()
        if syncs:
            return True
        else:
            return False

    def _set_timeout(self, user, timeout_value):
        """Change the lock_timeout setting on the primary database.

        The lock_timeout setting will cause the query to fail if the lock cannot be
        obtained in the specified number of milliseconds.  By setting it for the user,
        we can ensure that when the bucardo command-line tool will have this setting
        when it connects.  This is only a problem for user traffic if the application
        connects as the database owner, which is already a serious security flaw, so
        this script counts on that not being the case.

        Keyword arguments:
        user -- the postgres user whose timeout value should be changed
        timeout_value -- the new timeout value (either DEFAULT or some number of milliseconds)
        """
        query = f'ALTER ROLE {user} SET lock_timeout = {timeout_value}'
        conn = psycopg2.connect(self.primary_conn_pg_format)
        try:
            with conn.cursor() as cur:
                cur.execute(query)
                conn.commit()
        finally:
            conn.close()

    def add_triggers(self):
        """Add replication triggers to tables on the primary database.

        The script updates the timeout value to whatever is in the config.

        Then it adds however many triggers it can without hitting the timeout limit,
        which applies on a per-trigger basis.

        Then it sets the timeout value back to the default.  If the user already had a
        non-default lock_timeout value, then this may cause the wrong value to be set.
        You are welcome to submit a PR in that case.
        """
        print('Adding triggers.')
        self._set_timeout(self.primary_user, self.timeout)

        # Which bucardo function to call depends on whether we just need to add
        # some triggers that timed out, or whether this is the first attempt at
        # adding them.
        sync_already_added = self._check_for_syncs()

        if sync_already_added:
            query = f"SELECT bucardo.validate_sync('{self.repl_name}', 0)"
            conn = psycopg2.connect(self.bucardo_conn_pg_format)
            try:
                with conn.cursor() as cur:
                    cur.execute(query)
                    for notice in conn.notices:
                        print(notice, end='')
            finally:
                conn.close()

            # Enable and disable triggers as needed.
            self.bucardo_instance._manage_triggers()

        else:
            self.bucardo_instance.add_triggers()

        self._set_timeout(self.primary_user, 'DEFAULT')

        print(
            'Attempted to add triggers. Check the output above for warnings about missing triggers. '
            'If you see any, just run add_triggers again. If there are no warnings, you should be good to go.'
        )

    def drop_triggers(self):
        """Drop triggers on replicated tables on the primary database.
        The script updates the timeout value to whatever is in the config.

        Then it drops however many triggers it can without hitting the timeout limit,
        which applies on a per-trigger basis.

        Then it sets the timeout value back to the default.  If the user already had a
        non-default lock_timeout value, then this may cause the wrong value to be set.
        You are welcome to submit a PR in that case.
        """
        print('Dropping triggers.')
        self._set_timeout(self.primary_user, self.timeout)
        self.bucardo_instance.drop_triggers()
        self._set_timeout(self.primary_user, 'DEFAULT')
        print(
            'Attempted to drop triggers. Check the output above for warnings about missing triggers. '
            'If you see any, just run try_drop again. If there are no warnings, you should be good to go.'
        )

    def install(self):
        """Install the dependencies for retry logic on the bucardo database.

        Strictly speaking, this function only installs one of two dependencies.  It
        loads a slightly modified copy of a bucardo function provided by the Bucardo
        project to the bucardo database.

        It does not install the Perl Try::Tiny module, which is used by the forked
        version of the function and is not installed with bucardo.  You must install
        Try::Tiny yourself.
        """
        print('Installing dependencies.')
        # Read in the definition of a SQL stored procedure.
        custom_logic_file = 'plugins/retry/custom_validate_sync.sql'
        with open(custom_logic_file, 'r') as file:
            query = file.read()

        # Load the stored procedure into bucardo, replacing the one provided by the Bucardo project.
        conn = psycopg2.connect(self.bucardo_conn_pg_format)
        try:
            with conn.cursor() as cur:
                cur.execute(query)
                conn.commit()
        finally:
            conn.close()
        print(
            'Dependencies installed, except for the Perl Try::Tiny module. '
            'Make sure that module is installed before proceeding.'
        )

    def uninstall(self):
        """Uninstall the dependencies for retry logic on the bucardo database.

        Strictly speaking, this function only uninstalls one of two dependencies.  It
        loads the original copy of a bucardo function provided by the Bucardo project
        to the bucardo database.

        It does not remove the Perl Try::Tiny module, which is used by the forked
        version of the function and is not installed with bucardo.  You must uninstall
        Try::Tiny yourself.
        """
        print('Uninstalling dependencies.')
        # Read in the definition of a SQL stored procedure.
        custom_logic_file = 'plugins/retry/original_validate_sync.sql'
        with open(custom_logic_file, 'r') as file:
            query = file.read()

        # Load the stored procedure into bucardo, replacing the modified one.
        conn = psycopg2.connect(self.bucardo_conn_pg_format)
        try:
            with conn.cursor() as cur:
                cur.execute(query)
                conn.commit()
        finally:
            conn.close()
        print(
            'Dependencies removed, except for the Perl Try::Tiny module. '
        )


# -*- coding: utf-8 -*-
from PyQt4 import QtCore
from PyQt4.QtGui import QIcon
from PyQt4.QtGui import QDialog, QTableWidgetItem
from PyQt4.uic import loadUi
from edit_window import EditWindowPresenter
from message_window import MessageWindowPresenter

class ContactsWindowPresenter(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        loadUi('ContactsWindow.ui', self)
        self.parent = parent
        self.setWindowTitle(U'Контакты')
        self.setWindowIcon(QIcon('icon.png'))
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setHorizontalHeaderLabels([u'Имя', u'Телефон', u'Дата рождения'])
        self.update_table()
        self.editButton.clicked.connect(self.on_editButton_click)
        self.deleteButton.clicked.connect(self.on_deleteButton_click)

    def on_editButton_click(self):
        selected_rows = self.tableWidget.selectionModel().selectedRows()
        if not selected_rows:
            MessageWindowPresenter(self, u"Вы не выделили ни одной строки!").show()
        elif len(selected_rows) > 1:
            MessageWindowPresenter(self, u"Вы должны выделить только онду строку!").show()
        else:
            self.edit = EditWindowPresenter(self)
            self.edit.show()

    def on_deleteButton_click(self):
        selected_rows = self.tableWidget.selectionModel().selectedRows()
        if not selected_rows:
            MessageWindowPresenter(self, u"Вы не выделили ни одной строки!").show()
        else:
            for row in selected_rows:
                row_number = row.row()
                self.parent.parent.service.delete_contact(self.parent.user_info[0][0],
                                                          self.tableWidget.item(row_number, 0).text(),
                                                          self.tableWidget.item(row_number, 1).text(),
                                                          self.tableWidget.item(row_number, 2).text())
            self.update_table()
            MessageWindowPresenter(self, u"{0} строк удалены!".format(len(selected_rows))).show()

    def update_table(self):
        data = self.parent.parent.service.get_contacts(self.parent.user_info[0][0])
        row = 0
        self.tableWidget.setRowCount(0)
        for tup in data:
            if tup[0][
                0] in self.parent.tableWidget.currentItem().text() and self.parent.tableWidget.currentItem().text() != u'Прочее' \
                    or not (tup[0][0] in u'ЙЦУКЕНГШЩЗЪХФЫВАПРОЛДЖЭЯЧСМИТЬБЮйцукенгшщзхъэждлорпавыфячсмитьбю') and \
                    self.parent.tableWidget.currentItem().text() == u'Прочее':
                self.tableWidget.setRowCount(row + 1)
                cell_0 = QTableWidgetItem(unicode(tup[0]))
                cell_1 = QTableWidgetItem(tup[1])
                cell_2 = QTableWidgetItem(str(tup[2]))
                self.tableWidget.setItem(row, 0, cell_0)
                self.tableWidget.setItem(row, 1, cell_1)
                self.tableWidget.setItem(row, 2, cell_2)
                row += 1


a = 37
b = 5

addition = a + b
subtraction = a - b
division = a / b
multiplication = a * b
exponent = a ** b
modulus = a % b
floor_division = a // b

print(addition , subtraction, division, multiplication, exponent, modulus, floor_division, sep='\n')


# -*- coding: utf-8 -*-
"""
Created on Wed Aug 15 00:00:16 2018

@author: NickYue
"""

class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp = [1, 1, 2]
        if n <= 2:
            return dp[n]
        else:
            dp += [0 for i in range(n-2)]  # 后面创建多个
            for i in range(3, n + 1):
                for j in range(1, i+1):
                    # 若n为4：dp[0]*dp[3]+dp[1]*dp[2]+dp[2]*dp[1]+dp[3]*dp[0]
                    dp[i] += dp[j-1] * dp[i-j]
            return dp[n]


# Built-in Library
from re import I
from typing import Sequence, Dict
import time
# External Imports.
import dm_env
from dm_env import specs
from torch.utils.tensorboard import SummaryWriter
from torch.distributions import distribution
from torch.distributions.categorical import Categorical
from torch.nn.modules import dropout
import tree

import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F

# Internal Imports.
from algorithms import base
import algorithms.utils.sequence as sequence
from algorithms.vit import ViT

use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')
print('we will use ', device)

writer = SummaryWriter('logs')

class A2C(base.Agent):
    """A simple TensorFlow-based feedforward actor-critic implementation."""

    def __init__(
            self,
            obs_spec: specs.Array,
            action_spec: specs.Array,
            max_sequence_length: int,
            network: 'PolicyValueNet',
            learning_rate: float,
            discount: float,
    ):
        """A simple actor-critic agent."""

        self._discount = discount

        # Internalise network and optimizer.
        self._network = network
        self._optimizer = torch.optim.Adam(
            network.parameters(), lr=learning_rate)

        # Create windowed buffer for learning from trajectories.
        self._buffer = sequence.Buffer(
            obs_spec, action_spec, max_sequence_length)

    def __compute_returns(self, final_value, rewards, discounts):
        value = final_value
        returns = []
        for step in reversed(range(len(rewards))):
            value = rewards[step] + self._discount * value * discounts[step]
            returns.insert(0, value)
        return torch.tensor(returns)

    def _step(self, trajectory: sequence.Trajectory, step: int):
        """Do a batch of SGD on the actor + critic loss."""
        stime = time.time()
        observations, actions, costs, discounts = trajectory

        # calculate values by the value network.
        observations = observations[:-1]
        _, values = self._network(observations)
        values = torch.squeeze(values)

        # compute loss.
        advantage = costs - values
        print("costs shape:", costs.shape, "costs", costs, "values", values)
        actor_loss = costs.mean()
        critic_loss = advantage.pow(2).mean()
        loss = (actor_loss + 0.05 * critic_loss).pow(0.1)
        # print("actor_loss ", actor_loss, "critic_loss", critic_loss, "loss", loss)

        # log to tensor board event.
        writer.add_scalar('Multi-Step_Loss', loss.item())
        print('Multi-Step loss is:', loss.item())

        # update parameter.
        self._network.train()
        #TODO(thekips): test.
        for p in self._network._policy_head.parameters():
            p.requires_grad = False
        self._optimizer.zero_grad()
        loss.backward()
        nn.utils.clip_grad_norm_(self._network.parameters(), 0.5)

        self._optimizer.step()
        print("Time of step use %ds" % (time.time() - stime))

    def select_action(self, timestep: dm_env.TimeStep) -> base.Action:
        """Selects actions according to the latest softmax policy."""
        # combine the agent's location with obj's location respectively.
        observation = torch.tensor(timestep.observation)
        # print('observation shape is', observation.shape)

        policies, _ = self._network(observation)
        return policies

    def update(
        self,
        timestep: dm_env.TimeStep,
        action: base.Action,
        new_timestep: dm_env.TimeStep,
        step: int,
    ):
        """Receives a transition and performs a learning update."""

        self._buffer.append(timestep, action, new_timestep)

        # When the batch is full, do a step of SGD.
        if self._buffer.full() or new_timestep.last():
            trajectory = self._buffer.drain()
            trajectory = tree.map_structure(torch.tensor, trajectory)
            self._step(trajectory, step)

class PolicyValueNet(nn.Module):
    def __init__(
        self,
        image_size,
        patch_size,
        hidden_size: int,
    ):
        super(PolicyValueNet, self).__init__()
        print('Vit init.')
        self._vit = ViT(image_size=image_size, patch_size=patch_size, num_classes=hidden_size,
                       dim=1024, depth=6, heads=16, mlp_dim=2048, dropout=0.1, emb_dropout=0.1)
        self._policy_head = nn.Linear(hidden_size, 2)
        self._value_head = nn.Linear(hidden_size, 1)

        print('Vit end init.')
        # self._fc_layer = nn.Sequential()
        # for i in range(len(hidden_sizes) - 1):
        #     self._fc_layer.add_module(
        #         'fc'+str(i), nn.Linear(hidden_sizes[i], hidden_sizes[i+1]))
        #     self._fc_layer.add_module('relu'+str(i), nn.ReLU())

    def forward(self, x: torch.Tensor):
        x = x / 255 # take value in x into [0, 1] as float.
        if len(x.shape) == 3:
            x = torch.unsqueeze(x, 0)
        # (thekips):When use pytorch<=1.3.0, you should use x.float() to avoid scalar type error.
        x = self._vit(x.float())   # x.shape turn from image_size to hidden_size.
        # x = einops.rearrange(x, 'i j k l -> i (j k l)')

        policies = self._policy_head(x)
        value = self._value_head(x)

        return policies.squeeze().tolist(), value


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 28 18:27:01 2019

@author: Paulo
"""

import pandas as pd
import numpy as np
from PIL import Image
from nightmares.images_defines import GDOT_SAVE_FOLDER
from os.path import join, isdir
from os import listdir as ls

def create_info_cols(df):
    """ Create width and height columns in a dataframe.
        Arguments:
            df: Dataframe to put the columns.
        Returns:
            df: Modified dataframe.
    """
    df["width"] = np.nan
    df["height"] = np.nan
    return df

def put_image_info(df, name, width=np.nan, height=np.nan):
    """ Put width and height information into a dataframe.
        Arguments:
            df: Dataframe to put width and height information.
            name: Name of the image to put the information.
            width: Width of the image.
            height: Height of the image.
        Returns:
            df: Modified dataframe.
    """
    df.loc[df.name == name, "width"] = width
    df.loc[df.name == name, "height"] = height
    return df

def join_per_year(root_folder=GDOT_SAVE_FOLDER):
    """ Uses all years images informations to make a info file with all csv.
        The function will read the images.csv files, join then and put the new
        file in the root_folder.s
        Arguments:
            root_folder: Folder with the year/month folders.
    """
    infos = []
    for year_folder in ls(root_folder):
        print(year_folder)
        year_folder = join(root_folder, year_folder)
        if not isdir(year_folder): continue
        image_info_file = join(year_folder, "images.csv")
        try:
            info = pd.read_csv(image_info_file)
            info = create_info_cols(info)
        except:
            print("image.csv from {} does not exists!".format(year_folder))
            continue
        for month_folder in ls(year_folder):
            month_folder = join(year_folder, month_folder)
            if not isdir(month_folder): continue
            images = ls(month_folder)
            for image in images:
                image_filepath = join(month_folder, image)
                with Image.open(image_filepath) as img:
                    width, height = img.size
                    info = put_image_info(info, image, width=width, height=height)
        info.to_csv(image_info_file, index=False)
        infos.append(info)
    infos = pd.concat(infos, axis=0)
    infos.to_csv(join(root_folder, "images.csv"), index=False)


# 11- Utilizando os códigos criados na resolução dos exercícios anteriores,
# crie uma função que retorne uma lista de itens inteiros e que receba a quantidade de itens a serem
# gerados por parâmetro.

from random import randint

def lista_inteiros(qtd):
    lista = []
    for n in range(qtd):
        lista.append(randint(0, 1999))
    return lista

if __name__ == '__main__':
    while True:
        try:
            nome = input('Olá usuário!!! Qual o seu nome? ')
            itens = int(input(f'Muito bem {nome}, digite a quantidade de itens que queres gerar na lista: '))
            break
        except ValueError:
            print('\033[1;31mVALOR DIGITADO ERRADO, REPITA A OPERAÇÃO!!!\033[0m')

    print(f'{nome} , a sua lista ficou assim: \033[1;32m\n{lista_inteiros(itens)} \033[0m.')