import eventlet
from eventlet import greenpool

from oslo_config import cfg
from oslo_log import log as logging
from oslo_service import loopingcall
from oslo_utils import excutils
from oslo_utils import strutils
from oslo_utils import timeutils
from taskflow.patterns import linear_flow
from taskflow import retry

import masakari.conf
from masakari.engine.drivers.taskflow import base
from masakari import exception
from masakari import objects
from masakari.objects import fields
from masakari import utils

CONF = masakari.conf.CONF
LOG = logging.getLogger(__name__)
ACTION = 'instance:evacuate'
# Instance power_state
SHUTDOWN = 4
TASKFLOW_CONF = cfg.CONF.taskflow_driver_recovery_flows

class DisableComputeServiceTask(base.MasakariTask):
    def __init__(self, context, novaclient, **kwargs):
        kwargs['requires'] = ["host_name"]
        super(DisableComputeServiceTask, self).__init__(context, novaclient,
                                                        **kwargs)

    def execute(self, host_name):
        msg = "Disabling compute service on host: '%s'" % host_name
        self.update_details(msg)
        self.novaclient.enable_disable_service(self.context, host_name,
            reason=CONF.host_failure.service_disable_reason)
        # Sleep until nova-compute service is marked as disabled.
        log_msg = ("Sleeping %(wait)s sec before starting recovery "
               "thread until nova recognizes the node down.")
        LOG.info(log_msg, {'wait': CONF.wait_period_after_service_update})
        eventlet.sleep(CONF.wait_period_after_service_update)
        msg = "Disabled compute service on host: '%s'" % host_name
        self.update_details(msg, 1.0)

class PrepareHAEnabledInstancesTask(base.MasakariTask):
    """Get all HA_Enabled instances."""

    def __init__(self, context, novaclient, **kwargs):
        kwargs['requires'] = ["host_name", "notification_uuid"]
        super(PrepareHAEnabledInstancesTask, self).__init__(context,
                                                            novaclient,
                                                            **kwargs)

    def execute(self, host_name, notification_uuid):
        def _filter_instances(instance_list):
            ha_enabled_instances = []
            non_ha_enabled_instances = []

            ha_enabled_key = CONF.host_failure.ha_enabled_instance_metadata_key

            for instance in instance_list:
                is_instance_ha_enabled = strutils.bool_from_string(
                    instance.metadata.get(ha_enabled_key, False))
                if CONF.host_failure.ignore_instances_in_error_state and (
                        getattr(instance, "OS-EXT-STS:vm_state") == "error"):
                    if is_instance_ha_enabled:
                        msg = ("Ignoring recovery of HA_Enabled instance "
                               "'%(instance_id)s' as it is in 'error' state."
                               ) % {'instance_id': instance.id}
                        LOG.info(msg)
                        self.update_details(msg, 0.4)
                    continue

                if is_instance_ha_enabled:
                    ha_enabled_instances.append(instance)
                else:
                    non_ha_enabled_instances.append(instance)

            msg = "Total HA Enabled instances count: '%d'" % len(
                ha_enabled_instances)
            self.update_details(msg, 0.6)

            if CONF.host_failure.evacuate_all_instances:
                msg = ("Total Non-HA Enabled instances count: '%d'" % len(
                    non_ha_enabled_instances))
                self.update_details(msg, 0.7)

                ha_enabled_instances.extend(non_ha_enabled_instances)

                msg = ("All instances (HA Enabled/Non-HA Enabled) should be "
                       "considered for evacuation. Total count is: '%d'") % (
                    len(ha_enabled_instances))
                self.update_details(msg, 0.8)

            return ha_enabled_instances

        msg = "Preparing instances for evacuation"
        self.update_details(msg)

        instance_list = self.novaclient.get_servers(self.context, host_name)

        msg = ("Total instances running on failed host '%(host_name)s' is "
               "%(instance_list)d") % {'host_name': host_name,
                                       'instance_list': len(instance_list)}
        self.update_details(msg, 0.3)

        instance_list = _filter_instances(instance_list)

        if not instance_list:
            msg = ("Skipped host '%s' recovery as no instances needs to be "
                   "evacuated" % host_name)
            self.update_details(msg, 1.0)
            LOG.info(msg)
            raise exception.SkipHostRecoveryException(message=msg)

        # persist vm moves
        for instance in instance_list:
            vmove = objects.VMove(context=self.context)
            vmove.instance_uuid = instance.id
            vmove.instance_name = instance.name
            vmove.notification_uuid = notification_uuid
            vmove.source_host = host_name
            vmove.status = fields.VMoveStatus.PENDING
            vmove.type = fields.VMoveType.EVACUATION
            vmove.create()

        # List of instance UUID
        instance_list = [instance.id for instance in instance_list]

        msg = "Instances to be evacuated are: '%s'" % ','.join(instance_list)
        self.update_details(msg, 1.0)

class EvacuateInstancesTask(base.MasakariTask):

    def __init__(self, context, novaclient, **kwargs):
        kwargs['requires'] = ["host_name", "notification_uuid"]
        self.update_host_method = kwargs['update_host_method']
        super(EvacuateInstancesTask, self).__init__(context, novaclient,
                                                    **kwargs)

    def _get_state_and_host_of_instance(self, context, instance):
        new_instance = self.novaclient.get_server(context, instance.id)
        instance_host = getattr(new_instance,
                                "OS-EXT-SRV-ATTR:hypervisor_hostname")
        old_vm_state = getattr(instance, "OS-EXT-STS:vm_state")
        new_vm_state = getattr(new_instance, "OS-EXT-STS:vm_state")

        return (old_vm_state, new_vm_state, instance_host)

    def _stop_after_evacuation(self, context, instance):
        def _wait_for_stop_confirmation():
            old_vm_state, new_vm_state, instance_host = (
                self._get_state_and_host_of_instance(context, instance))

            if new_vm_state == 'stopped':
                raise loopingcall.LoopingCallDone()

        try:
            # confirm instance is stopped after recovery
            self.novaclient.stop_server(context, instance.id)
            timer = loopingcall.FixedIntervalWithTimeoutLoopingCall(
                _wait_for_stop_confirmation)
            timer.start(interval=CONF.verify_interval,
                        timeout=CONF.wait_period_after_power_off).wait()
        except loopingcall.LoopingCallTimeOut:
            with excutils.save_and_reraise_exception():
                msg = ("Instance '%(uuid)s' is successfully evacuated but "
                       "timeout to stop.") % {'uuid': instance.id}
                LOG.warning(msg)
        finally:
            timer.stop()

    def _evacuate_and_confirm(self, context, vmove,
                              reserved_host=None):

        def _update_vmove(vmove, status=None, start_time=None,
                          end_time=None, dest_host=None,
                          message=None):
            if status:
                vmove.status = status
            if start_time:
                vmove.start_time = start_time
            if end_time:
                vmove.end_time = end_time
            if dest_host:
                vmove.dest_host = dest_host
            if message:
                vmove.message = message
            vmove.save()

        instance_uuid = vmove.instance_uuid
        instance = self.novaclient.get_server(context, instance_uuid)

        # Before locking the instance check whether it is already locked
        # by user, if yes don't lock the instance
        instance_already_locked = self.novaclient.get_server(
            context, instance.id).locked

        if not instance_already_locked:
            # lock the instance so that until evacuation and confirmation
            # is not complete, user won't be able to perform any actions
            # on the instance.
            self.novaclient.lock_server(context, instance.id)

        def _wait_for_evacuation_confirmation():
            old_vm_state, new_vm_state, instance_host = (
                self._get_state_and_host_of_instance(context, instance))

            if (new_vm_state == 'error' and
                    new_vm_state != old_vm_state):
                raise exception.InstanceEvacuateFailed(
                    instance_uuid=instance.id)

            if instance_host != vmove.source_host:
                if ((old_vm_state == 'error' and
                    new_vm_state == 'active') or
                        old_vm_state == new_vm_state):
                    raise loopingcall.LoopingCallDone()

        def _wait_for_evacuation():
            try:
                # add a timeout to the periodic call.
                timer = loopingcall.FixedIntervalWithTimeoutLoopingCall(
                    _wait_for_evacuation_confirmation)
                timer.start(interval=CONF.verify_interval,
                            timeout=CONF.wait_period_after_evacuation).wait()
            except loopingcall.LoopingCallTimeOut:
                with excutils.save_and_reraise_exception():
                    msg = ("Timeout for instance '%(uuid)s' evacuation."
                           % {'uuid': instance.id})
                    LOG.warning(msg)
            finally:
                # stop the periodic call, in case of exceptions or
                # Timeout.
                timer.stop()

        try:
            vm_state = getattr(instance, "OS-EXT-STS:vm_state")
            task_state = getattr(instance, "OS-EXT-STS:task_state")

            # Nova evacuates an instance only when vm_state is in active,
            # stopped or error state. If an instance is in other than active,
            # error and stopped vm_state, masakari resets the instance state
            # to *error* so that the instance can be evacuated.
            stop_instance = True
            if vm_state not in ['active', 'error', 'stopped']:
                self.novaclient.reset_instance_state(context, instance.id)
                instance = self.novaclient.get_server(context, instance.id)
                power_state = getattr(instance, "OS-EXT-STS:power_state")
                if vm_state == 'resized' and power_state != SHUTDOWN:
                    stop_instance = False

            elif vm_state == 'stopped' and task_state is None:
                # If vm_state is stopped and task_state is none, the instance
                # will be recovered with vm_state 'stopped'.
                # So it doesn't need to stop the instance after evacuation.
                stop_instance = False

            elif task_state is not None:
                # Nova fails evacuation when the instance's task_state is not
                # none. In this case, masakari resets the instance's vm_state
                # to 'error' and task_state to none.
                self.novaclient.reset_instance_state(context, instance.id)
                instance = self.novaclient.get_server(context, instance.id)
                if vm_state == 'active':
                    stop_instance = False

            # start to evacuate the instance
            _update_vmove(
                vmove,
                status=fields.VMoveStatus.ONGOING,
                start_time=timeutils.utcnow())
            self.novaclient.evacuate_instance(context, instance.id,
                                              target=reserved_host)

            _wait_for_evacuation()

            if vm_state != 'active':
                if stop_instance:
                    self._stop_after_evacuation(self.context, instance)
                    # If the instance was in 'error' state before failure
                    # it should be set to 'error' after recovery.
                    if vm_state == 'error':
                        self.novaclient.reset_instance_state(
                            context, instance.id)

            instance = self.novaclient.get_server(context, instance_uuid)
            dest_host = getattr(
                instance, "OS-EXT-SRV-ATTR:hypervisor_hostname")
            _update_vmove(
                vmove,
                status=fields.VMoveStatus.SUCCEEDED,
                dest_host=dest_host)
        except loopingcall.LoopingCallTimeOut:
            # Instance is not stop in the expected time_limit.
            msg = "Failed reason: timeout."
            _update_vmove(
                vmove,
                status=fields.VMoveStatus.FAILED,
                message=msg)
        except Exception as e:
            # Exception is raised while resetting instance state or
            # evacuating the instance itself.
            LOG.warning(str(e))
            _update_vmove(
                vmove,
                status=fields.VMoveStatus.FAILED,
                message=str(e))
        finally:
            _update_vmove(vmove, end_time=timeutils.utcnow())
            if not instance_already_locked:
                # Unlock the server after evacuation and confirmation
                self.novaclient.unlock_server(context, instance.id)

    def execute(self, host_name, notification_uuid, reserved_host=None):
        all_vmoves = objects.VMoveList.get_all_vmoves(
            self.context, notification_uuid, status=fields.VMoveStatus.PENDING)
        instance_list = [i.instance_uuid for i in all_vmoves]
        msg = ("Start evacuation of instances from failed host '%(host_name)s'"
               ", instance uuids are: '%(instance_list)s'") % {
            'host_name': host_name, 'instance_list': ','.join(instance_list)}
        self.update_details(msg)

        def _do_evacuate(context, host_name,
                         reserved_host=None):
            if reserved_host:
                msg = "Enabling reserved host: '%s'" % reserved_host
                self.update_details(msg, 0.1)
                if CONF.host_failure.add_reserved_host_to_aggregate:
                    # Assign reserved_host to an aggregate to which the failed
                    # compute host belongs to.
                    aggregates = self.novaclient.get_aggregate_list(context)
                    for aggregate in aggregates:
                        if host_name in aggregate.hosts:
                            try:
                                msg = ("Add host %(reserved_host)s to "
                                       "aggregate %(aggregate)s") % {
                                    'reserved_host': reserved_host,
                                    'aggregate': aggregate.name}
                                self.update_details(msg, 0.2)

                                self.novaclient.add_host_to_aggregate(
                                    context, reserved_host, aggregate)
                                msg = ("Added host %(reserved_host)s to "
                                       "aggregate %(aggregate)s") % {
                                    'reserved_host': reserved_host,
                                    'aggregate': aggregate.name}
                                self.update_details(msg, 0.3)
                            except exception.Conflict:
                                msg = ("Host '%(reserved_host)s' already has "
                                       "been added to aggregate "
                                       "'%(aggregate)s'.") % {
                                    'reserved_host': reserved_host,
                                    'aggregate': aggregate.name}
                                self.update_details(msg, 1.0)
                                LOG.info(msg)

                self.novaclient.enable_disable_service(
                    context, reserved_host, enable=True)

                # Set reserved property of reserved_host to False
                self.update_host_method(context, reserved_host)

            thread_pool = greenpool.GreenPool(
                CONF.host_failure_recovery_threads)

            nonlocal all_vmoves

            for vmove in all_vmoves:
                msg = ("Evacuation of instance started: '%s'"
                       % vmove.instance_uuid)
                self.update_details(msg, 0.5)
                thread_pool.spawn_n(self._evacuate_and_confirm, self.context,
                                    vmove, reserved_host)
            thread_pool.waitall()

            all_vmoves = objects.VMoveList.get_all_vmoves(
                self.context, notification_uuid)

            succeeded_vmoves = [i.instance_uuid for i in all_vmoves
                    if i.status == fields.VMoveStatus.SUCCEEDED]
            if succeeded_vmoves:
                succeeded_vmoves.sort()
                msg = ("Successfully evacuate instances '%(instance_list)s' "
                       "from host '%(host_name)s'") % {
                    'instance_list': ','.join(succeeded_vmoves),
                    'host_name': host_name}
                self.update_details(msg, 0.7)

            failed_vmoves = [i.instance_uuid for i in
                    all_vmoves if i.status == fields.VMoveStatus.FAILED]
            if failed_vmoves:
                msg = ("Failed to evacuate instances "
                       "'%(instance_list)s' from host "
                       "'%(host_name)s'") % {
                    'instance_list': ','.join(failed_vmoves),
                    'host_name': host_name}
                self.update_details(msg, 0.7)
                raise exception.HostRecoveryFailureException(
                    message=msg)

            msg = "Evacuation process completed!"
            self.update_details(msg, 1.0)

        lock_name = reserved_host if reserved_host else None

        @utils.synchronized(lock_name)
        def do_evacuate_with_reserved_host(context, host_name,
                notification_uuid, reserved_host):
            _do_evacuate(context, host_name,
                         reserved_host=reserved_host)

        if lock_name:
            do_evacuate_with_reserved_host(self.context, host_name,
                                           notification_uuid,
                                           reserved_host)
        else:
            # No need to acquire lock on reserved_host when recovery_method is
            # 'auto' as the selection of compute host will be decided by nova.
            _do_evacuate(self.context, host_name)

def get_auto_flow(context, novaclient, process_what):
    """Constructs and returns the engine entrypoint flow.

    This flow will do the following:

    1. Disable compute service on source host
    2. Get all HA_Enabled instances.
    3. Evacuate all the HA_Enabled instances.
    4. Confirm evacuation of instances.
    """

    flow_name = ACTION.replace(":", "_") + "_engine"
    nested_flow = linear_flow.Flow(flow_name)

    task_dict = TASKFLOW_CONF.host_auto_failure_recovery_tasks

    auto_evacuate_flow_pre = linear_flow.Flow('pre_tasks')
    for plugin in base.get_recovery_flow(task_dict['pre'], context=context,
                                         novaclient=novaclient,
                                         update_host_method=None):
        auto_evacuate_flow_pre.add(plugin)

    auto_evacuate_flow_main = linear_flow.Flow('main_tasks')
    for plugin in base.get_recovery_flow(task_dict['main'], context=context,
                                         novaclient=novaclient,
                                         update_host_method=None):
        auto_evacuate_flow_main.add(plugin)

    auto_evacuate_flow_post = linear_flow.Flow('post_tasks')
    for plugin in base.get_recovery_flow(task_dict['post'], context=context,
                                         novaclient=novaclient,
                                         update_host_method=None):
        auto_evacuate_flow_post.add(plugin)

    nested_flow.add(auto_evacuate_flow_pre)
    nested_flow.add(auto_evacuate_flow_main)
    nested_flow.add(auto_evacuate_flow_post)

    return base.load_taskflow_into_engine(ACTION, nested_flow,
                                          process_what)

def get_rh_flow(context, novaclient, process_what, **kwargs):
    """Constructs and returns the engine entrypoint flow.

    This flow will do the following:

    1. Disable compute service on source host
    2. Get all HA_Enabled instances.
    3. Evacuate all the HA_Enabled instances using reserved_host.
    4. Confirm evacuation of instances.
    """
    flow_name = ACTION.replace(":", "_") + "_engine"
    nested_flow = linear_flow.Flow(flow_name)

    task_dict = TASKFLOW_CONF.host_rh_failure_recovery_tasks

    rh_evacuate_flow_pre = linear_flow.Flow('pre_tasks')
    for plugin in base.get_recovery_flow(
            task_dict['pre'],
            context=context, novaclient=novaclient, **kwargs):
        rh_evacuate_flow_pre.add(plugin)

    rh_evacuate_flow_main = linear_flow.Flow(
        "retry_%s" % flow_name, retry=retry.ParameterizedForEach(
            rebind=['reserved_host_list'], provides='reserved_host'))

    for plugin in base.get_recovery_flow(
            task_dict['main'],
            context=context, novaclient=novaclient, **kwargs):
        rh_evacuate_flow_main.add(plugin)

    rh_evacuate_flow_post = linear_flow.Flow('post_tasks')
    for plugin in base.get_recovery_flow(
            task_dict['post'],
            context=context, novaclient=novaclient, **kwargs):
        rh_evacuate_flow_post.add(plugin)

    nested_flow.add(rh_evacuate_flow_pre)
    nested_flow.add(rh_evacuate_flow_main)
    nested_flow.add(rh_evacuate_flow_post)

    return base.load_taskflow_into_engine(ACTION, nested_flow,
                                          process_what)


import requests
import pytest
import json
import os

# BASE_URL = "http://192.168.1.11"
# BASE_URL = "http://localhost"
ip = os.environ["IP"]
BASE_URL = "http://" + ip
print(BASE_URL)
port = os.environ["PORT"]
# BASE_PORT = "8082"
BASE_PORT = port
PATH = "/albums"

FULL_URL = BASE_URL + ":" + BASE_PORT + PATH

def test_get_albums():
    print(FULL_URL)
    resp = requests.get(FULL_URL)
    assert resp.status_code == 200
    print(resp.status_code)
    print(resp.json())

def test_get_album_with_id():
    resp = requests.get(FULL_URL + "/2")
    print(resp.json())
    assert resp.status_code == 200

def test_post_create_album():
    body = json.dumps({'title': "Ero", "artist": "Enigma", "country": "India"})
    resp = requests.post(FULL_URL, body)
    print(resp.json())
    print(resp.status_code)
    get_all = requests.get(FULL_URL)
    print(get_all.json())
    assert len(get_all.json()) > 3


import hashlib
import json

from asgiref.sync import async_to_sync
from django.core.files.base import ContentFile
from django.utils.timezone import now

from venueless.celery_app import app
from venueless.core.services.world import notify_schedule_change
from venueless.core.tasks import WorldTask
from venueless.importers.conftool import (
    create_posters_from_conftool,
    fetch_schedule_from_conftool,
)
from venueless.storage.models import StoredFile

@app.task(base=WorldTask)
def conftool_update_schedule(world):
    u = world.config.get("conftool_url")
    p = world.config.get("conftool_password")

    if not u or not p or not world.config.get("pretalx").get("conftool"):
        return "invalid"

    d = fetch_schedule_from_conftool(u, p)
    v = d.pop("version")
    checksum = hashlib.sha256(json.dumps(d, sort_keys=True).encode()).hexdigest()
    d["version"] = v
    document = json.dumps(d, sort_keys=True)

    if StoredFile.objects.filter(
        world=world, filename=f"conftool_schedule_{checksum}.json"
    ).exists():
        return "unchanged"

    sf = StoredFile.objects.create(
        world=world,
        date=now(),
        filename=f"conftool_schedule_{checksum}.json",
        type="application/json",
        file=ContentFile(document, "schedule.json"),
        public=True,
    )
    world.config["pretalx"]["url"] = sf.file.url
    world.config["pretalx"]["conftool"] = True
    world.save()
    async_to_sync(notify_schedule_change)(world.id)
    return sf.pk

@app.task(base=WorldTask)
def conftool_sync_posters(world):
    u = world.config.get("conftool_url")
    p = world.config.get("conftool_password")
    create_posters_from_conftool(world, u, p)


import pandas as pd
import numpy as np

class Dataset:
    def __init__(self, name):
        self.name = name
        self.X_train = np.array(pd.read_csv("feature_selection/data/" + name + "_train.data", header=None, sep=" ").iloc[:, :-1]) / 999
        self.y_train = (np.array(pd.read_csv("feature_selection/data/" + name + "_train.labels", header=None, sep=" ")) + 1 ) // 2
        self.y_train = self.y_train.reshape(-1)
        self.X_valid = np.array(pd.read_csv("feature_selection/data/" + name + "_valid.data", header=None, sep=" ").iloc[:, :-1]) / 999

names = ['artificial', "digits"]
datasets = []
for name in names:
    datasets.append(Dataset(name))


import json
import PIL.Image as Image
import torch.utils.data as data
from torchvision import transforms as transforms
from typing import List, Dict

from .. import LabeledDataset
from . import config

class SslDataset(LabeledDataset):
    r"""Dataset class with region-level annotations"""

    def __init__(self, pre_transforms, transform, meta_data: List[Dict]):
        r"""Constructor for annotated region dataset

        Args:
            meta_data: dataset metadata
            transform: transform to apply on images
            crop_size: size of center crop
            image_size: the size to resize the images to
        """
        super(SslDataset, self).__init__(transform, meta_data)
        self.pre_transforms = pre_transforms

    def __getitem__(self, idx):
        filepath = self.filepaths[idx]
        image = Image.open(filepath)
        image = self.pre_transforms[self.labels[idx]](image)
        image = self.transform(image)
        return image, self.labels[idx]

    def __len__(self):
        return len(self.filepaths)

def get_ssl_data_loaders(meta_data_path, transform, transform_test,
                         batch_size=8, num_workers=4):
    with open(meta_data_path, "r") as f:
        meta_data = json.load(f)
    pre_transforms = [
        transforms.Compose([
            transforms.Resize((image_size, image_size))
        ]) for image_size in config.IMAGE_SIZES]
    train_set = SslDataset(pre_transforms, transform, meta_data["train"])
    train_loader = data.DataLoader(train_set, batch_size=batch_size,
                                   num_workers=num_workers, shuffle=True)
    val_set = SslDataset(pre_transforms, transform_test, meta_data["val"])
    val_loader = data.DataLoader(val_set, batch_size=batch_size,
                                 num_workers=num_workers, shuffle=False)
    return train_loader, val_loader


from bs4 import BeautifulSoup
import requests
import csv
from datetime import datetime
import time

global TEST_MODE
TEST_MODE = False

global dataListDict
dataListDict = []

global keysList
keysList = ["Enroll date", "URL", "Register no.", "Given Name", "Profile Name", "Labs", "Quests"]

def addError(err, data):
    global dataListDict
    err = "E: " + err
    dataListDict.append({"Enroll date": data[0], "URL": data[3], "Register no.": data[2], "Given Name": data[1], "Profile Name": err})

def getDetailsForProfile(data):
    try:
        r = requests.get(data[3])
    except:
        addError("Invalid URL", data)
        return

    soup = BeautifulSoup(r.content, 'html.parser')

    name_h1 = soup.find("h1", {"class": "l-mbm"})
    labs_quests_p = soup.find("p", {"class": "public-profile__hero__details"})
    badges_divs = soup.find_all("div", {"class": "public-profile__badge"})

    if name_h1 is None:
        addError("Not found", data)
        return

    global dataListDict
    global keysList

    temp_dict = {}
    temp_dict["Enroll date"] = data[0]
    temp_dict["Given Name"] = data[1]
    temp_dict["Register no."] = data[2]
    temp_dict["URL"] = data[3]

    name = name_h1.text.strip()
    labs_quests = labs_quests_p.text.strip().rsplit("\n")

    temp_dict["Profile Name"] = name[:-1]
    temp_dict["Labs"] = labs_quests[0]
    temp_dict["Quests"] = labs_quests[3]

    for badge in badges_divs:
        b_arr = badge.text.strip().rsplit("\n")
        temp_dict[b_arr[0]] = b_arr[4]
        if b_arr[0] not in keysList:
            keysList.append(b_arr[0])

    dataListDict.append(temp_dict)

if __name__ == "__main__":
    start_time = time.time()
    if TEST_MODE:
        print("TEST_MODE: ON")
        file_name = 'test_urls.csv'
    else:
        file_name = 'profile_urls.csv'

    total_count = 0
    with open(file_name, mode ='r') as file:
        csvFile = csv.reader(file)
        next(csvFile)
        total_count = sum(1 for row in csvFile)
        print("Total entries:", total_count)

    with open(file_name, mode ='r') as file:
        csvFile = csv.reader(file)
        next(csvFile)

        i = 0
        for lines in csvFile:
            getDetailsForProfile(lines)
            i += 1
            if i % 5 == 0:
                print(i, "/", total_count, "profiles analysed")

    print("Saving data")

    todayFileName = datetime.today().strftime('%Y-%m-%d')
    if TEST_MODE:
        todayFileName = todayFileName + "-test.csv"
    else:
        todayFileName = ".\\CSV\\" + todayFileName + ".csv"

    with open(todayFileName, 'w', newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames = keysList)
        writer.writeheader()
        writer.writerows(dataListDict)

    diff = time.time() - start_time
    print("Finished in", int(diff/60), "min", "{:7.4f}".format(float(diff%60)), "sec")


# -*- coding: utf-8 -*-
import pytest

@pytest.mark.parametrize(
    'params, expected',
    [
        ([[10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]],
            [1, 1, 2, 3, 3, 3]),
    ],
)
def test_book(params, expected):
    from leetcode.my_calendar_iii import MyCalendarThree

    s = MyCalendarThree()
    actual = []
    for param in params:
        actual.append(s.book(*param))
    assert expected == actual


from heapq import heappush, heappop, heapify
from collections import namedtuple

class Node(namedtuple('Node', 'prob,value,children')):
    def __lt__(self, other):
        return self.prob < other.prob

def pnode(probs):
    nodes = [Node(p, x, []) for x, p in probs.items()]
    if not nodes:
        return
    heapify(nodes)
    while len(nodes) != 1:
        a, b = heappop(nodes), heappop(nodes)
        heappush(nodes, Node(a.prob + b.prob, None, [a, b]))
    return nodes[0]

def codes(node, prefix=()):
    if node.value is not None:
        yield node.value, prefix
        return
    left, right = node.children
    yield from codes(left, prefix + (0,))
    yield from codes(right, prefix + (1,))

if __name__ == '__main__':
    r = dict(codes(pnode({
        'a': 0.5,
        'b': 0.25,
        'c': 0.125,
        'd': 0.125,
    })))
    assert len(r['a']) == 1
    assert len(r['b']) == 2
    assert len(r['c']) == 3
    assert len(r['d']) == 3


#ref:https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
#nz=100 epoch=100 fid=34 face_reg=80.5%
#nz=100 epoch=100 fid=29 face_reg=86.9% #save Dx higher
#test fid=52.34 face_reg=84.7% original unnormalize
#test fid=51.57 face_reg=88.7% easy unnormalize
#test fid=61.54 face_reg=82.9% transform unnormalize
#test fid=34 face_reg=77.7% easy unnormalize
#test fid=26.87 face_reg=89.8% nz=300 epoch=300 1000/1000
import argparse
import os
import random
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.parallel
from tqdm import tqdm
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from model import Generator,Discriminator
#from IPython.display import HTML
# custom weights initialization called on netG and netD
class UnNormalize(object):
    def __init__(self, mean, std):
        self.mean = mean
        self.std = std
    def __call__(self, tensor):
        for t, m, s in zip(tensor, self.mean, self.std):
            t.mul_(s).add_(m)
        return tensor
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        nn.init.normal_(m.weight.data, 0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        nn.init.normal_(m.weight.data, 1.0, 0.02)
        nn.init.constant_(m.bias.data, 0)
#def main(args):
if __name__ == "__main__":
    # Seed

    # Set random seed for reproducibility
    PATH = "./model/best_model_P1B.pt"
    # ckpt = torch.load(PATH)
    # print(ckpt["epoch"])
    manualSeed = 999
    #manualSeed = random.randint(1, 10000) # use if you want new results
    print("Random Seed: ", manualSeed)
    random.seed(manualSeed)
    torch.manual_seed(manualSeed)

    save_path = "./tmp/"
    # Root directory for dataset
    dataroot = "hw2_data/face"
    # Number of workers for dataloader
    workers = 2
    # Batch size during training
    batch_size = 128
    # Spatial size of training images. All images will be resized to this
    #   size using a transformer.
    image_size = 64
    # Number of channels in the training images. For color images this is 3
    nc = 3
    # Size of z latent vector (i.e. size of generator input)
    nz = 300
    # Size of feature maps in generator
    ngf = 64
    # Size of feature maps in discriminator
    ndf = 64
    # Number of training epochs
    num_epochs = 300
    # Learning rate for optimizers
    lr = 0.0002
    # Beta1 hyperparam for Adam optimizers
    beta1 = 0.5
    # Number of GPUs available. Use 0 for CPU mode.
    ngpu = 1
    # Create the dataset
    mean = (0.5, 0.5, 0.5)
    std = (0.5, 0.5, 0.5)
    dataset = dset.ImageFolder(root=dataroot,transform=transforms.Compose([
                                transforms.Resize(image_size),
                                transforms.CenterCrop(image_size),
                                transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                            ]))
    # Create the dataloader
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                            shuffle=True, num_workers=workers)

    # Decide which device we want to run on
    device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")

    # Plot some training images
    real_batch = next(iter(dataloader))
    plt.figure(figsize=(8,8))
    plt.axis("off")
    plt.title("Training Images")
    plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(),(1,2,0)))
    plt.savefig("./test.png")

    # Create the generator
    netG = Generator(ngf, nz, nc).to(device)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # Apply the weights_init function to randomly initialize all weights
    #  to mean=0, stdev=0.02.
    netG.apply(weights_init)

    # Print the model
    print(netG)

    # Create the Discriminator
    netD = Discriminator(ndf, nc).to(device)
    # Apply the weights_init function to randomly initialize all weights
    #  to mean=0, stdev=0.2.
    netD.apply(weights_init)
    # Print the model
    print(netD)

    # Initialize BCELoss function
    criterion = nn.BCELoss()

    # Establish convention for real and fake labels during training
    real_label = 1.
    fake_label = 0.

    # Setup Adam optimizers for both G and D
    optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999))
    #optimizerD = optim.SGD(netD.parameters(), lr=lr)
    optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999))
    transform = transforms.Compose([
            #transforms.Normalize((-1, -1, -1), (2, 2, 2)),
            transforms.ToPILImage(),
        ])
    unorm = UnNormalize(mean, std)
    best_Dx = 0.0
    best_DGz = 0.0
    # Training Loop
    iters = 0
    print("Starting Training Loop...")
    # For each epoch
    for epoch in range(num_epochs):
        # Lists to keep track of progress
        img_list = []
        G_losses = []
        D_losses = []
        # For each batch in the dataloader
        train_loop = tqdm(dataloader,position=0,leave=False,ncols=60,colour='green',desc='Epoch '+str(epoch+1))
        for i, data in enumerate(train_loop, 0):

            ############################
            # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
            ###########################
            ## Train with all-real batch
            netD.zero_grad()
            # Format batch
            real_cpu = data[0].to(device)
            b_size = real_cpu.size(0)
            label = torch.full((b_size,), real_label, dtype=torch.float, device=device)
            # Forward pass real batch through D
            output = netD(real_cpu).view(-1)
            # Calculate loss on all-real batch
            errD_real = criterion(output, label)
            # Calculate gradients for D in backward pass
            errD_real.backward()
            D_x = output.mean().item()

            ## Train with all-fake batch
            # Generate batch of latent vectors
            noise = torch.randn(b_size, nz, 1, 1, device=device)
            # Generate fake image batch with G
            fake = netG(noise)
            label.fill_(fake_label)
            # Classify all fake batch with D
            output = netD(fake.detach()).view(-1)
            # Calculate D's loss on the all-fake batch
            errD_fake = criterion(output, label)
            # Calculate the gradients for this batch, accumulated (summed) with previous gradients
            errD_fake.backward()
            D_G_z1 = output.mean().item()
            # Compute error of D as sum over the fake and the real batches
            errD = errD_real + errD_fake
            # Update D
            optimizerD.step()

            ############################
            # (2) Update G network: maximize log(D(G(z)))
            ###########################
            netG.zero_grad()
            label.fill_(real_label)  # fake labels are real for generator cost
            # Since we just updated D, perform another forward pass of all-fake batch through D
            output = netD(fake).view(-1)
            # Calculate G's loss based on this output
            errG = criterion(output, label)
            # Calculate gradients for G
            errG.backward()
            D_G_z2 = output.mean().item()
            # Update G
            optimizerG.step()

            # Save Losses for plotting later
            G_losses.append(errG.item())
            D_losses.append(errD.item())

            iters += 1
        # Output training stats
        print('[%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f'% (epoch, num_epochs,errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
        if(D_x > best_Dx):
            torch.save({
                'epoch':(epoch+1),
                'netG_state_dict':netG.state_dict(),
                'netD_state_dict':netD.state_dict(),
                'D_x':D_x,
                'D_G_z1':D_G_z1,
                }, PATH)
            best_Dx = D_x
            best_DGz = D_G_z1

    num_generate_ = 5000
    num_result = 20000
    ckpt = torch.load(PATH)
    print(ckpt["epoch"])
    netG = Generator(ngf, nz, nc)
    netD = Discriminator(ndf, nc)
    netG.load_state_dict(ckpt["netG_state_dict"])
    netD.load_state_dict(ckpt["netD_state_dict"])
    netG.to(device)
    netD.to(device)
    with torch.no_grad():
        # Create batch of latent vectors that we will use to visualize
        #  the progression of the generator
        img_prob = []
        for _ in range(num_result//num_generate_):
            fixed_noise = torch.randn(num_generate_, nz, 1, 1, device=device)
            fake = netG(fixed_noise)
            prob = netD(fake)
            fake = fake.detach().cpu()
            prob = prob.detach().cpu()
            #print(fake[0]) (1000,3,64,64)
            #print(prob.size()) (1000,1,1,1)
            #print(prob[0].item()) 0.05
            for j in range(num_generate_):
                img = fake[j] / 2 + 0.5
                im = transform(img)
                img_prob.append((im,prob[j].item()))

        img_prob.sort(key=lambda tup: tup[1],reverse=True)  # sorts in place

        for j in range(1000):
            path = os.path.join(save_path,f"{j}.png")
            im =  img_prob[j][0]
            im.save(path)


from django.contrib.sites.models import Site
from django.db.models import F
from django.template import TemplateDoesNotExist
from django.template.loader import get_template
from django.views.generic import TemplateView

from .models import AboutViews

class PageMixin(object):
    """Mixin para pages.

    Variables de clase:
    * template_name: Template .html (requerido)
    * template_md: Template .md (no requerido)
    * context_md: Contexto para templates *.md

    Requiere una URLConf para cada vista.

    Si no tiene valor template_md, simplemente renderiza
    la pagina .html.

    get_context_md similar a get_context_data pero para template_md.
    """
    template_md = None
    context_md = {}

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        if self.template_md:
            try:
                context['template_md'] = get_template(self.template_md).render(
                    self.get_context_md()
                )
            except TemplateDoesNotExist:
                raise TemplateDoesNotExist('Template {} no existe'.format(self.template_md))
        return context

    def get_context_md(self):
        """Similar a get_context_data pero para el *.md."""
        return self.context_md

class CookieConsentView(PageMixin, TemplateView):
    """Muestra la política de cookies."""
    template_name = 'pages/cookie_consent.html'
    template_md = 'pages/cookie_consent.md'

    def get_context_md(self):
        context = super().get_context_md()
        context['site'] = Site.objects.get_current()
        return context

class AboutView(PageMixin, TemplateView):
    """Pagina de About.

    Cuanta el numero de visitas que tiene.
    """
    template_name = 'pages/about.html'
    model = AboutViews

    def dispatch(self, request, *args, **kwargs):
        if AboutViews.objects.count() == 0:
            AboutViews.objects.create()
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        if not request.user.is_superuser:
            AboutViews.objects.update(views=F('views') + 1)
        return super().get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['views_about'] = AboutViews.objects.get(pk=1)
        return context


import scipy.optimize

from evaluation import *
from utils import get_rotation_matrix_safe

@dataclass
class RotationSolution:

    objective_fnc: float
    rotation_vector_normal: float
    rotation_vector: np.ndarray
    permutation: list

def get_distance(one, two, angular_distance=True, squared=True):

    if angular_distance:
        scalar_products = one[:, 0] * two[:, 0] + one[:, 1] * two[:, 1] + one[:, 2] * two[:, 2]
        scalar_products = np.clip(scalar_products, a_min=-1.0, a_max=1.0)
        angular_distances = np.arccos(scalar_products)
        if squared:
            dist = np.sum(angular_distances ** 2) / angular_distances.shape[0]
        else:
            dist = np.sum(np.abs(angular_distances)) / angular_distances.shape[0]
    else:
        norms = np.linalg.norm(one - two, axis=1)
        if squared:
            dist = np.sum(norms ** 2) / norms.shape[0]
        else:
            dist = np.sum(np.abs(norms)) / norms.shape[0]

    return dist

def rotation_sort_value(solution: RotationSolution):
    return math.sqrt(solution.objective_fnc) * 10 + solution.rotation_vector_normal

def find_sorted_rotations(normals1, normals2, zero_around_z):

    assert len(normals1.shape) == 2
    assert len(normals2.shape) == 2

    assert np.all(np.abs(np.linalg.norm(normals1, axis=1) - 1.0) < 10 ** -5)
    assert np.all(np.abs(np.linalg.norm(normals2, axis=1) - 1.0) < 10 ** -5)

    solutions = find_rotations_info(normals1, normals2, zero_around_z)
    solutions.sort(key=lambda solution: rotation_sort_value(solution))
    return solutions

class ObjFunction:

    def __init__(self, src_normals, dst_normals, zero_around_z):
        self.src_normals = src_normals
        self.dst_normals = dst_normals
        self.zero_around_z = zero_around_z

    def function_value(self, r):

        if self.zero_around_z:
            r = np.array([r[0], r[1], 0.0])

        R = get_rotation_matrix_safe(r)
        rotated = (R @ self.src_normals.T).T

        return get_distance(rotated, self.dst_normals)

def find_rotations_info(normals1, normals2, zero_around_z):

    swapped = False
    if len(normals1) > len(normals2):
        swapped = True
        normals1, normals2 = normals2, normals1

    len1 = len(normals1)
    len2 = len(normals2)

    solutions = []

    for cur_permutation in itertools.permutations(range(len2), len1):

        def min_function(r):
            target = normals2[list(cur_permutation)]
            return ObjFunction(normals1, target, zero_around_z).function_value(r)

        use_shgo = False
        if use_shgo:
            sf = 1.0
            if zero_around_z:
                bounds = [(-math.pi * sf, math.pi * sf), (-math.pi * sf, math.pi * sf)]
            else:
                bounds = [(-math.pi * sf, math.pi * sf), (-math.pi * sf, math.pi * sf), (-math.pi * sf, math.pi * sf)]
            solution = scipy.optimize.shgo(min_function, bounds)
            if solution.success:

                for i in range(solution.xl.shape[0]):
                    r_star = solution.xl[i]
                    value = min_function(r_star)

                if swapped:
                    r_star = -r_star
                if zero_around_z:
                    r_star = np.array([r_star[0], r_star[1], 0])
                solutions.append(RotationSolution(rotation_vector=r_star, objective_fnc=value, rotation_vector_normal=np.linalg.norm(r_star), permutation=list(cur_permutation)))
        else:
            if zero_around_z:
                initial = np.zeros(2)
            else:
                initial = np.zeros(3)
            r_star = scipy.optimize.fmin(min_function, initial, disp=False)
            value = min_function(r_star)
            if swapped:
                r_star = -r_star
            if zero_around_z:
                r_star = np.array([r_star[0], r_star[1], 0])
            solutions.append(RotationSolution(rotation_vector=r_star, objective_fnc=value, rotation_vector_normal=np.linalg.norm(r_star), permutation=list(cur_permutation)))

    return solutions


# -*- coding: UTF-8 -*-

import os.path
import re
from datetime import datetime,timedelta
from settings import siteinfo,lockfile,usertemppath,isuploadfileoss

def getfiletypename(filetype):
    if (filetype is 1): return '邮箱校验'
    if (filetype is 2): return '短信校验'
    if (filetype is 3): return '密码加密'
    if (filetype is 4): return '金融支付校验'
    if (filetype is 5): return '金融支付强校验'

def file_name(path):
    return os.path.split(path)[1]

def file_extension(filename):
    finfo = os.path.splitext(filename)
    if (len(finfo) < 2):
        return ''
    else:
        return finfo[1]

def lock_site_notify():
    if (os.path.exists(lockfile) is True):
        return True,siteinfo['locknotify']
    return False,siteinfo['sitenotify']

def deep_auth_create(userid, code):
    filename = '%s_%s' % (userid, code)
    nowdir = datetime.now().strftime('%Y%m%d')
    nowpath = os.path.join(usertemppath, nowdir, filename)
    if (os.path.exists(nowpath) is False):
        if (os.path.exists(os.path.join(usertemppath, nowdir)) is False):
            os.makedirs(os.path.join(usertemppath, nowdir))
        with open(nowpath, 'w+') as up:
            up.write(filename)

def deep_auth_check(userid, code):
    filename = '%s_%s' % (userid, code)
    now = datetime.now()
    nowdir = now.strftime('%Y%m%d')
    if (os.path.exists(os.path.join(usertemppath, nowdir, filename)) is True):
        return True
    beforetime = now - timedelta(days=1)
    beforetimedir = beforetime.strftime('%Y%m%d')
    if (os.path.exists(os.path.join(usertemppath, beforetimedir, filename)) is True):
        return True
    return False

def checkfilename(filename):
    filter_rule = '[^\u2E80-\u9FFFa-zA-Z0-9_.]+'
    if (re.search(filter_rule, filename)):
        return False
    else:
        return True

def file_exists(filepath,oss=None):
    if isuploadfileoss is True:
        return oss.exists(filepath)
    else:
        return os.path.exists(filepath)

def file_read(filepath,oss=None):
    if isuploadfileoss is True:
        filedata = oss.Bucket.get_object(filepath).read()
    else:
        with open(filepath, 'r') as f:
            filedata = f.readlines()
    return filedata

def file_write(filepath,filedata,oss=None):
    if isuploadfileoss is True:
        oss.Bucket.put_object(filepath, filedata)
    else:
        with  open(filepath, 'w', encoding='utf-8') as f:
            f.write(filedata)


class Node:

    def __init__(self, value):
        self.val = value
        self.lch = None
        self.rch = None
        self.height = 1

    def insert_children(self, child):
        if self.lch is None:
            self.lch = Node(child)
            self.lch.height = self.height + 1
        else:
            self.rch = Node(child)
            self.rch.height = self.height + 1

    def insert_node(self, value, child):
        if self is not None:
            if self.val != value:
                if self.lch is not None:
                    self.lch.insert_node(value, child)
                if self.rch is not None:
                    self.rch.insert_node(value, child)
            else:
                self.insert_children(child)

    def get_height_of_tree(self):
        tmp = self.height
        if self.lch is not None:
            tmp = max(self.lch.get_height_of_tree(), tmp)
        if self.rch is not None:
            tmp = max(self.rch.get_height_of_tree(), tmp)
        return tmp

if __name__ == '__main__':
    root = Node(int(0))
    for i in range(int(input())-1):
        inf = input().split()
        root.insert_node(int(inf[0]), int(inf[1]))
    print(root.get_height_of_tree())


from sarpaminfohub.contactlist.tests.contactlist_test_case import ContactListTestCase
class ContactListPageTestCase(ContactListTestCase):
    FLASH_MESSAGE = "<div id=\"flashmessagebox\">"

    def check_flash_message_appears_on_page(self, url):
        response = self.client.get(url)
        self.assertEquals(200, response.status_code)

        self.assertContains(response, self.FLASH_MESSAGE)

    def check_flash_message_does_not_appear_on_page(self, url):
        response = self.client.get(url)
        self.assertEquals(200, response.status_code)

        self.assertNotContains(response, self.FLASH_MESSAGE)


def solution( prerequisites,numCourses):
    # 课程的长度
    clen = len(prerequisites)
    if clen == 0:
        return []
    # 入度数组
    in_degrees = [0 for _ in range(numCourses)]
    # 邻接表
    adj = [set() for _ in range(numCourses)]
    # 想要学习课程 0 ，你需要先完成课程 1 ，我们用一个匹配来表示他们: [0,1]
    # 1 -> 0，这里要注意：不要弄反了
    for second, first in prerequisites:
        in_degrees[second] += 1
        adj[first].add(second)

    # print("in_degrees", in_degrees)
    # 首先遍历一遍，把所有入度为 0 的结点加入队列
    res = []
    queue = []
    for i in range(numCourses):
        if in_degrees[i] == 0:
            queue.append(i)

    while queue:
        top = queue.pop(0)
        res.append(top)

        for successor in adj[top]:
            in_degrees[successor] -= 1
            if in_degrees[successor] == 0:
                queue.append(successor)
    if len(res) != numCourses:
        return []
    return res

if __name__=="__main__":
    n=int(input())
    lsn=eval(input())
    for item in lsn:
        item=list(reversed(item))
    lsn=sorted(lsn)
    ans=solution(lsn,n)
    print(ans)


from transformers import pipeline
import nltk.data
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

sentence_tokenizer = nltk.data.load("tokenizers/punkt/english.pickle")

# Load fine-tuned T5 model
summary_pipe = pipeline("summarization", model="final_model")
gen_kwargs = {"length_penalty": 0.8, "num_beams": 8, "max_length": 128}
sentence_transformer = SentenceTransformer('all-MiniLM-L6-v2')

def get_embedding(text):
    return sentence_transformer.encode(text)

def calculate_paragraph_importance(doc, window):
    doc_embedding = get_embedding(doc)
    window_embedding = get_embedding(window)
    return cosine_similarity([doc_embedding], [window_embedding])[0][0]

def capitalize(page):
    sentences = sentence_tokenizer.tokenize(page)
    return " ".join([sent.capitalize() for sent in sentences])

def sliding_window_summarize(text, window_size=300):
    sentences = text.split('.')
    grouped_sentences = []
    current_group = []
    current_count = 0

    for sentence in sentences:
        word_count = len(sentence.split())
        if current_count + word_count <= window_size:
            current_group.append(sentence)
            current_count += word_count
        else:
            grouped_sentences.append('. '.join(current_group) + '.')
            current_group = [sentence]
            current_count = word_count

    if current_group:
        grouped_sentences.append('. '.join(current_group) + '.')

    summaries = []
    for group in grouped_sentences:
        if group.strip():
            summary = summary_pipe(capitalize(group), **gen_kwargs)[0]['summary_text']
            summaries.append(capitalize(summary))

    return summaries

def summarize_article(pdf):
    bullet_points = []
    full_text = ""
    similarity_threshold = 0.2
    for page in pdf.pages:
        extracted = page.extract_text(x_tolerance=1) or ""
        full_text += " " + extracted

    window_summaries = sliding_window_summarize(full_text)
    bullet_points.extend(window_summaries)

    return "• " + "\n• ".join([b for b in bullet_points if calculate_paragraph_importance(full_text, b) >= similarity_threshold])


"""
O programa lê vários números e coloca em uma lista. Depois disso ele separa os números em uma lista par e uma lista ímpar e ao final mostra as três listas.
"""
lista = []
par = []
impar = []
while True:
    lista.append(int(input('Digite um número: ')))
    continuar = str(input('Quer continuar? [S/N] ')).upper().strip()[0]
    if continuar == 'N':
        break
for num in range(0, len(lista)):
    if lista[num] % 2 == 0:
        par.append(lista[num])
    else:
        impar.append(lista[num])
print(f'{"-=-" * 20}'
      f'\nA lista completa é {lista}'
      f'\nA lista de pares é {par}'
      f'\nA lista de ímpares é {impar}')


import torch
import torch.nn as nn
from .Attention import Attention
from models.BiLstm import Bilstm
import torch.nn.functional as F

class Vanilla(nn.Module):

    def __init__(self, args, embedding, pad):
        super(Vanilla, self).__init__()
        self.lstm = Bilstm(args, embedding, pad)
        self.attention = Attention(args)
        self.w = nn.Linear(args.embed_dim, args.class_num, bias=True)
        torch.nn.init.xavier_uniform_(self.w.weight)

    def forward(self, x, local, add_num, max_add, length):
        left_matrix, tar_matrix, right_matrix = self.lstm(x, local, add_num, max_add, length)
        x = self.attention(left_matrix, tar_matrix, right_matrix)
        logit = F.softmax(self.w(x), 0)
        return logit


from collections import deque

# A class to represent a graph object
class Graph:
    # Constructor
    def __init__(self, edges, N):
        # A list of lists to represent an adjacency list
        self.adjList = [[] for _ in range(N)]

        # add edges to the undirected graph
        for (src, dest) in edges:
            self.adjList[src].append(dest)
            self.adjList[dest].append(src)

def DFS(graph, v, discovered):
    # create a stack used to do iterative DFS
    stack = deque()

    # push the source node into the stack
    stack.append(v)

    # loop till stack is empty
    while stack:

        # Pop a vertex from the stack
        v = stack.pop()

        # if the vertex is already discovered yet, ignore it
        if discovered[v]:
            continue

        # we will reach here if the popped vertex `v`
        # is not discovered yet; print it and process
        # its undiscovered adjacent nodes into the stack
        discovered[v] = True
        print(v, end=' ')

        # do for every edge `v —> u`
        adj = graph.adjList[v]
        for i in reversed(range(len(adj))):
            u = adj[i]
            if not discovered[u]:
                stack.append(u)

# Function to perform DFS traversal on the graph on a graph
def recursive_DFS(graph, v, discovered):
    discovered[v] = True            # mark the current node as discovered
    print(v, end=' ')               # print the current node

    # do for every edge `v —> u`
    for u in graph.adjList[v]:
        if not discovered[u]:       # if `u` is not yet discovered
            recursive_DFS(graph, u, discovered)

def discover(graph, numNodes):
    # to keep track of whether a vertex is discovered or not
    discovered = [False] * numNodes

    # Do iterative DFS traversal from all undiscovered nodes to
    # cover all unconnected components of a graph
    for i in range(numNodes):
        if not discovered[i]:
            DFS(graph, i, discovered)
    print()

def discover_recursive(graph, numNodes):
    # to keep track of whether a vertex is discovered or not
    discovered = [False] * numNodes

    # Perform DFS traversal from all undiscovered nodes to
    # cover all unconnected components of a graph
    for i in range(numNodes):
        if not discovered[i]:
            DFS(graph, i, discovered)
    print()

if __name__ == '__main__':

    # List of graph edges as per the above diagram
    edges = [
        # Notice that node 0 is unconnected
        (1, 2), (1, 7), (1, 8), (2, 3), (2, 6), (3, 4),
        (3, 5), (8, 9), (8, 12), (9, 10), (9, 11)
    ]

    # total number of nodes in the graph (0–12)
    N = 13

    # build a graph from the given edges
    graph = Graph(edges, N)

    discover(graph, N)
    discover_recursive(graph, N)


from itertools import tee, chain, islice, zip_longest
import hashlib
from math import log
import re
from contextlib import contextmanager
from .active_timeout import TimeoutExceeded

replacement = re.compile(r'\.0+$')

def intToBytes(num, padding_length=0):
    s = format(num, 'x')
    if len(s) % 2:
        s = '0' + s
    result = bytes.fromhex(s)
    if len(result) < padding_length:
        return (bytes(padding_length-len(result)))+result
    return result

def bytesToInt(num):
    result = 0
    exp = int(256 ** (len(num)-1))
    for i in range(len(num)):
        result += int(num[i]*exp)
        exp //= 256
    return result

def clump(seq, length):
    getter = iter(seq)
    while True:
        try:
            tmp = next(getter)
            yield chain([tmp], islice(getter, length-1))
        except StopIteration:
            return

split_iterable = clump  # for compatability

def _getelem(iterator, elem):
    for row in iterator:
        yield row[elem]

def splice(seq):
    """
    Takes an iterable (which must iterate over a rectangular dataset)
    For an iterator which yields m rows of n elements each
    return n elements of length m, each yielding a different
    column of the original sequence
    """
    getter = iter(seq)
    first = next(getter)
    return [
        _getelem(iterator, i)
        for iterator, i in zip(
            tee(chain([first], getter), len(first)),
            range(len(first))
        )
    ]

def byte_xor(b1, b2):
    return intToBytes(bytesToInt(b1) ^ bytesToInt(b2), max(len(b1), len(b2)))

def hashfile(filepath, algorithm='sha1', length=None):
    reader = open(filepath, mode='rb')
    hasher = hashlib.new(algorithm)
    chunk = reader.read(4096)
    while chunk:
        hasher.update(chunk)
        chunk = reader.read(4096)
    reader.close()
    if isinstance(length, int):
        try:
            return hasher.digest(length)
        except TypeError:
            print(
                "Variable-length digests not supported for",
                algorithm,
                "algorithm on this platform"
            )
    return hasher.digest()

def byteSize(n):
    if n == 0:
        return '0B'
    index = min(8, int(log(abs(n), 1024)))
    suffix = [
        'B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'
    ][index]
    output = '%.1f' % (n / 1024**index)
    return replacement.sub('', output)+suffix

def first(iterable, predicate=lambda x: True):
    if not callable(predicate):
        def func(item):
            return item == predicate
    else:
        func = predicate
    for item in iterable:
        if func(item):
            return item

class LockTimeoutExceeded(TimeoutExceeded):
    pass

@contextmanager
def context_lock(lock, timeout=-1):
    attempt = lock.acquire(timeout=timeout)
    if not attempt:
        raise LockTimeoutExceeded(
            "Failed to acquire %s after %d seconds" % (lock, timeout)
        )
    try:
        yield lock
    finally:
        lock.release()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/01/06
# @Author  : yuetao
# @Site    :
# @File    : 字符串的排列.py
# @Desc    :
class Solution(object):
    def permutation(self, s):
        """
        :type s: str
        :rtype: List[str]
        """

        def dfs(s, index, cur_s, used):
            if len(cur_s) == len(s):
                if cur_s not in result:
                    result.append(cur_s)
                return
            for i in range(0, len(s)):
                if not used[i]:
                    cur_s += s[i]
                    used[i] = True
                    dfs(s, index + 1, cur_s, used)
                    cur_s = cur_s[:-1]
                    used[i] = False

        result = []
        length = len(s)
        used = [False] * length
        dfs(s, 0, "", used)
        return result

if __name__ == '__main__':
    solve = Solution()
    s = "abc"
    result = solve.permutation(s)
    print(result)


import json
import os

for datasets in ["train", "test", "val"]:

    s = ""

    for file_name in os.listdir(r"datasets/jsons"):
        json_path = os.path.join(r"datasets/jsons", file_name)

        if datasets in file_name:

            data = json.load(open(json_path, 'r', encoding='utf-8'), strict=False)
            s = s + "{} {} {} {} {} {} {}\n".format(file_name.replace(".json", ".jpg"), data["start"][0], data["start"][1],
                                                   data["medium"][0], data["medium"][1], data["end"][0], data["end"][1])

    f = open(r"datasets/annotations/{}_dataset.txt".format(datasets), "w", encoding="utf-8")
    f.write(s)
    f.close()


#encoding: utf-8
import numpy as np
import numpy.ma as ma
import itertools
from scipy import ndimage
from skimage.measure import label
import re

def regex_match(necessity, string_to_match):

    """"Gathering all regex matches used throughout the code to make it applicable to other datasets by modifying.
    Therefore, if we have to train on a new dataset where sequence description is in a new format, only this
    function has to be editted and all regex matches used to make the dataset mapping will be up to date. Generally
    regex are extensivelly used to navigate through map files and identify if something is a directory, etc. In short,
    it is used to find what is what.

    ..note:: Downside is that this script will have to be imported to other scripts under module directory.
             This makes it impossible to import any script from modules directory to *global_functions* script,
             otherwise mutual import will happen and an exception will be raised.

    :param string necessity: can be *patient image*, *patient image*, *axial T2*, *axial DWI*, *windows or unix directory*
    It indicates what we are looking for, which means that we are questioning if a string is a directory or an image etc.
    :param string_to_match:

    :returns: Bool, True if the string is what we are looking for, false otherwise.
    """
    expressions = {'patient image':re.compile(r'^PC-[0-9]{10}.*?.nii$|^aa[a,b]0*[0-9]{2}.*?.nii$|^01\.00[0-9]{2}$'),
                   'patient directory': re.compile(r'^PC-[0-9]{10}$|^01.[0-9]{4}$|^aa[a,b]0*[0-9]{2}$'),
                   'axial T2':re.compile(r'(ax.*?t2|t2.*?ax)(?!.*pelvis.*$)', re.IGNORECASE),
                   'axial DWI': re.compile(r'diff.*?adc|adc.*?diff|^dADC|apparent diff', re.IGNORECASE),
                   'windows or unix directory': re.compile(r'^[A-Z]:.*?|^/.*?')
                   }
    if re.match(expressions[necessity], string_to_match) is not None:
        return True
    else:
        return False

def pretty(data, string, fun_range, *indeces):

    value_dic = {}
    for i in range(fun_range-1,-1,-1):
        the_value = data[i]
        for idx in indeces:
            the_value = the_value[idx]
        value_dic[string + str(i+1)] = the_value
    return value_dic

def merge_dictionairies(*dics):

    x = {}
    for dic in dics:
        x.update(dic)
    return x

def custom_pixelwise_features(img, ws, function, *extra_args):
    """Returns features for the windows within the image calculated by function.

    It is a common issue that many features have to be calculated for each pixel within the image,
    with respect to a neighborhood of pixels, referred to as window. Window has a size *ws x ws*.
    *pixelwise_features* calls iteratively a function that calculates all these features.
    Function *function* can have in this version arbitrarilly many arguments given as
    the input specified by *extra_args*. *pixelwise_features* calls then this function for every
    window within the image. This means, that , for example, two directly subsequent windows only differ in one value.

    The important thing about *pixelwise_features* is that it takes in account boundary conditions.
    For a pixel that lies close to the boundary and a window of size *ws x ws* would yield an index error,
    is truncated to a window just fitting in the image. For example, the window of a pixel at *img[1,0]*
    for *ws = 9*, would be *img[0:5, 0:4]*.

    It is used for sliding window estimation of C2 and C5 features. Otherwise, *pixelwise_features* which calls an implementation
    from *ndimage* library, which is way more efficient is used.

    :param np.array img: image for which the features are calculated
    :param int ws: window size
    :param function.object function: function that calculates the features. It only takes one input, namely window (e.g. img[0:5, 0:4]
    :param any *extra_args: Any further set of parameters that function *function* could take as input

    :returns: dictionary where keys are features names and entries are 2d arrays with feature values over the image.
    """

    dummy_img = np.ones(img.shape)
    feat_characteristics = function(dummy_img)
    features_num = len(feat_characteristics)
    feat_names = feat_characteristics.keys()
    if (ws%2 == 0):
        # raise ValueError("radiomics.C1 : window size has to be odd numbers")
        pass
    shape = list(img.shape)
    shape.append(features_num)
    x_start_shifted = np.array([a - int(ws/2) for a in range(shape[0])])
    x_start = [a[a >= 0] for a in x_start_shifted]
    x_stop_shifted = np.array([a + int(ws / 2) for a in range(shape[0])])
    x_stop = [a[a < len(img)] for a in x_stop_shifted]
    y_start_shifted = np.array([a - int(ws/2) for a in range(shape[1])]) # supposing square support region
    y_start = [a[a >= 0] for a in y_start_shifted]
    y_stop_shifted = np.array([a + int(ws / 2) for a in range(shape[1])])
    y_stop = [a[a < len(img[0])] for a in y_stop_shifted]

    for i in range(len(x_start)):
        if x_start[i].size == 0:
            x_start[i] = 0
        else:
            x_start[i] = int(x_start[i])
    for i in range(len(x_stop)):
        if x_stop[i].size == 0:
            x_stop[i] = int(shape[0])
        else:
            x_stop[i] = int(x_stop[i])
    for i in range(len(y_start)):
        if y_start[i].size == 0:
            y_start[i] = 0
        else:
            y_start[i] = int(y_start[i])
    for i in range(len(y_stop)):
        if y_stop[i].size == 0:
            y_stop[i] = int(shape[1])
        else:
            y_stop[i] = int(y_stop[i])
    features = np.zeros(shape)
    for i in range(shape[0]):
        for j in range(shape[1]):
            features[i][j] = function(img[x_start[i]:x_stop[i], y_start[j]:y_stop[j]], *extra_args)
    features = np.swapaxes(features, 1, 2)
    features = np.swapaxes(features, 0, 1)
    dict_to_return = {a_key:an_array for a_key, an_array in itertools.izip(feat_names, features)}
    return dict_to_return

def pixelwise_features(img, ws, function, *extra_args):

    """Sliding window feature estimation by using *ndimage.filters.generic_filter*, which is much
    faster than the custom implementation.

    :param np.array img: image for which the features are calculated
    :param int ws: window size
    :param function.object function: function that calculates the features. It only takes one input, namely window (e.g. img[0:5, 0:4]
    :param any *extra_args: Any further set of parameters that function *function* could take as input

    :returns: 2d numpy array with feature values over the image.
    """

    feat_shape = img.shape
    features = np.zeros(feat_shape)
    ndimage.filters.generic_filter(img, function, size=ws, output=features, extra_arguments=extra_args)
    return features

def single_values_to_img(dictionary, img_shape):
    """"For ROI based features, replicate img.shape times the single value over the entire ROI"""
    extended_dic = {}
    for item in dictionary:
        if type(dictionary[item]) is list or type(dictionary[item]) is np.ndarray:
            i=0
            for value in dictionary[item]:
                level = np.tile(value, img_shape)
                hist_bin_name = item + '_%d'%i
                extended_dic[hist_bin_name] = level
                i += 1
        else:
            level = np.tile(dictionary[item], img_shape)
            extended_dic[item] = level
    return extended_dic

def set_in_range(img, indices, values):

    """Crop an array to the boundaries specified by indices and fill it with specified values. New image shape
    and values must have the same shape.

    :param 2d numpy array img:
    :param numpy array indices:
    :param 2d numpy array values:

    :returns: 2d numpy array with new shape and values
    """
    rows_0 = np.min(indices[1])
    rows_1 = np.max(indices[1])
    columns_0 = np.min(indices[0])
    columns_1 = np.max(indices[0])
    img[columns_0:columns_1, rows_0:rows_1] = values
    return img

def trim_array_to_ROI(img, return_support_region = False):

    """Crop an image to the boundaries specified by the circumscribed rectangle of the region defined by a mask.

    :param 2d numpy masked array img: mask is the region of interest.
    :param bool return_support_region: If set to true, also the boudaries of the circumscribed rectangle are returned.
    Default is False.

    :returns: 2d numpy array, cropped image

    :returns: If *return_support_region* is set, also a list with coordinates of the ROI circumscribed rectangle is returned.
    """
    #img.mask = np.logical_not(img.mask)
    edges_y = []
    for row in img:
        edges = ma.notmasked_edges(row)
        if edges is not None:
            edges_y.append(edges)
    edges_x = []
    for col in np.swapaxes(img, 0, 1):
        edges = ma.notmasked_edges(col)
        if edges is not None:
            edges_x.append(edges)
    if edges_x == [] and edges_y == []:
        return None
    edges_x = np.swapaxes(edges_x, 0, 1)
    edges_y = np.swapaxes(edges_y, 0, 1)
    min_x = np.min(edges_x[0])
    max_x = np.max(edges_x[1])
    min_y = np.min(edges_y[0])
    max_y = np.max(edges_y[1])
    # do not return stripes
    if max_x - min_x <= 1:
        if min_x == 0:
            max_x += 1
        elif max_x == edges_x[1, -1]:
            min_x -= 1
        else:
            max_x += 1
    if max_y - min_y <= 1:
        if min_y == 0:
            max_x += 1
        elif max_y == edges_y[1, -1]:
            min_y -= 1
        else:
            max_y += 1
    new_data = img.data[min_x : max_x+1, min_y : max_y+1]
    support_region = [(min_x, max_x+1), (min_y, max_y+1)]
    if return_support_region:
        return new_data, support_region
    return new_data

def healthy_rois(img, roi_inds, ROIs, a_function):
    """For ROI based calculation of C2 and C5 features. Apply the function defined as input to regions
    defined by the dual of circumscribed rectangles around regions of interest defined at ROIs.
    Healthy ROIs are defined by the intersections of lines defining the boundaries of circumscribed rectangles
    around regions defined by ROIs, thus they are compact and recangularly shaped.

    :param 2d numpy array img:
    :param list roi_inds: indices of lines defining the boundaries of circumscribed rectangles
    around regions defined by ROIs
    :param 2d numpy array ROIs:
    :param function object a_function: function to apply on healthy regions

    :returns: features dictionary, where keys are features' names and entries are 2d numpy arrays with the feature values over each pixel.

    :returns: A list with coordinates of the healthy ROI circumscribed rectangle is also returned.
    """
    features = []
    indices = []
    roi_inds.append([(0, img.shape[0]), (0, img.shape[1])])
    row_inds = np.sort(np.unique(np.ravel(np.array([x[0] for x in roi_inds]))))
    col_inds = np.sort(np.unique(np.ravel(np.array([x[1] for x in roi_inds]))))
    for i in range(len(row_inds)-1):
        for j in range(len(col_inds)-1):
            if not np.any(ROIs[row_inds[i]:row_inds[i+1], col_inds[j]:col_inds[j+1]]): # is healthy
                min_x = row_inds[i]
                max_x = row_inds[i+1]
                min_y = col_inds[j]
                max_y = col_inds[j+1]
                if max_x - min_x == 1:
                    if min_x <= 0:
                        max_x += 2
                        min_x = 0
                    elif max_x >= img.shape[0]:
                        min_x -= 2
                        max_x = img.shape[0]
                    else:
                        max_x += 1
                if max_y - min_y == 1:
                    if min_y <= 0:
                        max_y += 2
                        min_y = 0
                    elif max_y >= img.shape[-1]:
                        min_y -= 2
                        max_y = img.shape[-1]
                    else:
                        max_y += 1
                indices.append([(min_x, max_x), (min_y, max_y)])
                sub_image = img[min_x:max_x, min_y:max_y]
                features.append(a_function(sub_image))
    roi_inds.pop(-1) # the instance gets affected globaly!!
    return features, indices

def ROI_based_calclulations(img, ROIs, the_function):

    """For ROI based calculation of C2 and C5 features. Apply the function defined as input to regions
    defined by the circumscribed rectangles around regions of interest defined at ROIs. ROIs is a numpy array
    representing the mask where 0 refers to healthy tissue and 1 to lesions. There can multiple lesions of
    arbitrary shape. Also a 4-connected 1-pixel binary errosion
    and dilation is applied on ROIs to compensate for registration errors of the ROIs masks, that usually are
    the outputs of a registation and salt and pepper noise is frequently observed.
    Lesions with a total area less than 10 pixels are disregarded as they are probably a result of registration errors that
    are not compensated during erosion-dilation.

    :param 2d numpy array img:
    :param 2d numpy array ROIs:
    :param function object the_function:

    :returns: features dictionary, where keys are features' names and entries are 2d numpy arrays with the feature values over each pixel.
    """
    roi_features = []
    roi_indices = []
    corrected_ROIs = np.zeros(ROIs.shape)
    # 0 is the healthy, handled later
    #CHANGE
    non_compact_ROIs, components = label(ROIs, neighbors=4, return_num=True)
    if components > 1:  # 0 is healthy, 1 is one compact region. then usually it's noise if it's more
        remove_registration_errors = np.logical_not(ROIs)
        remove_registration_errors = ndimage.binary_erosion(remove_registration_errors, iterations=1)
        remove_registration_errors = ndimage.binary_dilation(remove_registration_errors, iterations=1)
        ROIs = np.logical_not(remove_registration_errors)
        non_compact_ROIs, components = label(ROIs, neighbors=4, return_num=True)
    ROIs = np.logical_or(corrected_ROIs, np.logical_not(ROIs))  # this roi masks out True values.
    for subroi in range(1, components + 1):  # 0 is the rest, the non-roi
        #CHANGE
        this_subroi = np.where(non_compact_ROIs == subroi, False, True)
        if np.sum(np.logical_not(this_subroi)) > 10:
            roi_img = ma.masked_array(img, this_subroi)
            roi_img, their_indices = trim_array_to_ROI(roi_img, return_support_region=True)
            roi_features.append(the_function(roi_img))
            roi_indices.append(their_indices)
        else:
            ROIs = np.where(non_compact_ROIs == subroi, True, ROIs) # change noise to healthy to give features
    hr, hi = healthy_rois(img, roi_indices, np.logical_not(ROIs), the_function) #needs True to consider as sick
    roi_features.extend(hr)
    roi_indices.extend(hi)
    # reconstruct
    features = {}
    for feature_name in list(roi_features[0]):
        features[feature_name] = np.zeros(img.shape)
    for this_roi_feature_dic, this_roi_support in itertools.izip(roi_features, roi_indices):
        # assuming compact rois
        for feature_name in this_roi_feature_dic:
            features[feature_name] = set_in_range(features[feature_name], this_roi_support,
                                                  this_roi_feature_dic[feature_name])

    return features

# from scipy.misc import toimage
# toimage(this_roi).show()


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

import sys
import os
import re
from utilities import log, languageTranslate
from cineast import CineastWebService

_ = sys.modules[ "__main__" ].__language__

def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input
    msg = ""
    subtitles_list = []
    websvc = CineastWebService()
    title = title.strip()
    if tvshow:  # TvShow
        log(__name__, "search TVshow with "+tvshow)
        for result in websvc.searchTvSubtitlesByTitle(tvshow):
            subtitle = {"format": "smi", "sync": True}
            subtitle['link'] = result['link']
            subtitle['filename'] = result['title']
            rating = 0
            if season == result['season']:
                rating += 3
            if episode == result['episode']:
                rating += 3
            subtitle['rating'] = str(rating)
            subtitle['language_name'] = result['language']
            subtitle['language_flag'] = "flags/%s.gif" %languageTranslate(result['language'],0,2)
            subtitles_list.append( subtitle )
    elif title:   # Movie
        srch_title = title
        query = re.match("^(.*)\(([^\)]*)\)$", title)
        if query:
            srch_title = query.group(2).strip() # English title
        log(__name__, "search Movie with "+srch_title)
        for result in websvc.searchMovieSubtitlesByTitle(srch_title):
            subtitle = {"format": "smi", "sync": True}
            subtitle['link'] = result['link']
            subtitle['filename'] = result['title']
            subtitle['rating'] = "6" if file_original_path.find(result['relgrp']) > 0 else "0"
            subtitle['language_name'] = result['language']
            subtitle['language_flag'] = "flags/%s.gif" %result['language'][:2].lower()
            subtitles_list.append( subtitle )
    else:
        msg = _(800)
    return subtitles_list, "", msg #standard output

def download_subtitles (subtitles_list, pos, zip_subs, tmp_sub_dir, sub_folder, session_id): #standard input
    language = subtitles_list[pos][ "language_name" ]
    websvc = CineastWebService()
    subtitle = websvc.parseSubtitlesPage( subtitles_list[pos]["link"] )[0]
    log(__name__,  u"download subtitle from %s" %subtitle["link"])
    try:
        tmp_fname = os.path.join(tmp_sub_dir, subtitle["filename"])
        resp = urllib2.urlopen(url)
        f = open(tmp_fname, "w")
        f.write(resp.read())
        f.close()
    except:
        return False, language, ""
    return True, language, tmp_fname #standard output


import sys
import random
from load import *
from dna import *

def oneFrame(DNA):
    orf_List = []
    i = 0
    stopCodon = ["TAG","TGA","TAA"]
    while i < len(DNA):
        if DNA[i:i+3] == "ATG":
            j = i
            while DNA[j:j+3] not in stopCodon and j < len(DNA):
                j += 3
            if DNA[i:j] != "ATG":
                orf_List.append(DNA[i:j])
        i += 3
    return orf_List

def oneFrameV2(DNA):
    orf_List = oneFrame(DNA)
    new_orf_list = []
    for i in range(len(orf_List)-1):
        new_orf_list.append(orf_List[i])
        if orf_List[i+1] in orf_List[i]:
            i += 2
        else:
            i += 1
    return new_orf_list

def longestORF(DNA):
    maxORF = ""
    for i in range(3):
        list = oneFrameV2(DNA[i:])
        if len(list) != 0 and len(maxORF)<len(max(list,key=len)):
            maxORF = max(list,key=len)
    return maxORF

def longestORFBothStrands(DNA):
    list = []
    list.append(longestORF(DNA))
    list.append(longestORF(reverseComplement(DNA)))
    if len(list) != 0:
        return max(list,key=len)
    else:
        return ""

def collapse(list):
    return "".join(list)

def longestORFNoncoding(DNA, numReps):
    dnaList = list(DNA)
    maxLen = 0
    for i in range(numReps):
        random.shuffle(dnaList)
        garbSeq = longestORFBothStrands(collapse(dnaList))
        if maxLen < len(garbSeq):
            maxLen = len(garbSeq)
    return maxLen

def findORFs(DNA):
    maxORF = []
    for i in range(3):
        list = oneFrameV2(DNA[i:])
        if len(list) != 0:
            maxORF.append(max(list,key=len))
    return maxORF

def findORFsBothStrands(DNA):
    return findORFs(DNA)+findORFs(reverseComplement(DNA))

def getCoordinates(orf,DNA):
    list = []
    start = DNA.find(orf)
    if start != -1:
        list.append(start)
        list.append(start+len(orf))
        return list
    else:
        start = reverseComplement(DNA).find(orf)
        list.append(start)
        list.append(start+len(orf))
        return list

def geneFinder(DNA, minLen):
    finalOutputList = []
    ORFList = findORFsBothStrands(DNA)
    for item in ORFList:
        if len(item) >= minLen:
            tempList = getCoordinates(item,DNA)
            tempList.append(codingStrandToAA(item))
            finalOutputList.append(tempList)
    finalOutputList.sort()
    return finalOutputList

def printGenes(geneList):
    for item in geneList:
        print(item)

def main():
    DNA = loadSeq("X73525.fa")
    minLen = longestORFNoncoding(DNA,1500)
    printGenes(geneFinder(DNA,minLen))
main()


# Python imports
from dataclasses import dataclass
from pathlib import Path
import pandas as pd
import numpy as np
import spatialmath
from kincalib.utils.ExperimentUtils import separate_markerandfiducial
from kincalib.utils.Frame import Frame

np.set_printoptions(precision=4, suppress=True, sign=" ")

def get_wrist_fiducials_cp(robot_cp):
    fiducials_cp = robot_cp.loc[robot_cp["m_t"] == "f"]
    steps = fiducials_cp["step"].unique().reshape((-1, 1))
    _, wrist_fiducials = separate_markerandfiducial(
        None, Path("./share/custom_marker_id_112.json"), df=fiducials_cp
    )
    wrist_fiducial_cp = pd.DataFrame(
        np.hstack((steps, wrist_fiducials.T)), columns=["step", "x", "y", "z"]
    )
    return wrist_fiducial_cp

@dataclass
class FRE:
    """Calculate the Fiducial registration error (FRE) between two point clouds. The input data can be specified
    in joints space or cartesian space. If joints data is given, the class requires also a forward kinematic function.
    Each set of points should be passed as a dictionary with the following keys:

    ```dict(mode='',data=df, fk=fk)```

    Notes
    - mode has to be either 'joint' or 'cartesian'
    - data is a dataframe with the columns `step q1 q2 q3 q4 q5 q6` or `step x y z`
    - fk is a forward kinematic function to calculate cartesian points from joints. Only required if `mode=='joints'`
    - The `step` column is used to define correspondances between the set of points.

    Parameters
    ----------
    input_dict1: dict
        Input dict
    input_dict2: dict
        Input dict

    TODO:
    - Cartesian calculation from joints is not very intuitive for people to use.
    - Maybe extract cartesian should be embedded in the fk_function
    """

    input_dict1: dict
    input_dict2: dict

    def __post_init__(self):
        assert self.input_dict1["mode"] in ["joint", "cartesian"]
        assert self.input_dict2["mode"] in ["joint", "cartesian"]

        if self.input_dict1["mode"] == "joint":
            assert "fk" in self.input_dict1, "No fk function for input_data1"
            self.set1_cp_df = self.calculate_cartesian(
                self.input_dict1["data"], self.input_dict1["fk"]
            )
        else:
            self.set1_cp_df = self.input_dict1["data"]

        if self.input_dict2["mode"] == "joint":
            assert "fk" in self.input_dict2, "No fk function for input_data2"
            self.set2_cp_df = self.calculate_cartesian(
                self.input_dict2["data"], self.input_dict2["fk"]
            )
        else:
            self.set2_cp_df = self.input_dict2["data"]

    def calculate_fre(self):
        data = pd.merge(
            self.set1_cp_df, self.set2_cp_df, how="inner", on="step", suffixes=("_1", "_2")
        )

        A = data[["x_1", "y_1", "z_1"]].to_numpy().T
        B = data[["x_2", "y_2", "z_2"]].to_numpy().T

        rig_T = Frame.find_transformation_direct(A, B)
        B_est = rig_T @ A
        errors = B_est - B

        error_list = np.linalg.norm(errors, axis=0)

        return error_list

    def calculate_cartesian(self, data_df, fk_func):
        steps = data_df["step"].to_numpy().reshape((-1, 1))
        data_cp: spatialmath.pose3d.SE3
        data_cp = fk_func(data_df[["q1", "q2", "q3", "q4", "q5", "q6"]].to_numpy())
        data_cp = self.extract_cartesian_xyz(data_cp.data)
        data_cp = np.hstack((steps, data_cp))

        return pd.DataFrame(data_cp, columns=["step", "x", "y", "z"])

    def extract_cartesian_xyz(self, cartesian_t: np.ndarray):
        position_list = []
        for i in range(len(cartesian_t)):
            position_list.append(cartesian_t[i][:3, 3])

        return np.array(position_list)


#!/usr/bin/python3
import pygal
import sys, json;
from datetime import datetime
import platform

step = 2
data = json.load(sys.stdin)
classes = set([n["device_class"] for n in data["nodes"]])

hist = pygal.Histogram(title="%s on %s" % (str(datetime.now()), platform.node()), x_title="Utilization %", y_title="# of OSDs")
for c in sorted(classes):
	bins = [[x, 0] for x in range(0,100,step)]
	for node in data["nodes"]:
		if node["device_class"] == c:
			u = (int)(node["utilization"]/step);
			bins[u][1] += 1;
	hist.add(c, [(b[1], b[0], b[0]+step) for b in bins])

hist.render_to_file('/dev/stdout')


import scapy.all as scapy
import optparse

def get_arguments():
    parser=optparse.OptionParser()
    parser.add_option("-i","--interface",dest="interface",help="-i eth0")

    (options,argument) = parser.parse_args()

    if not options.interface:
        parser.error("[-] Please enter an interface")
    return options

def get_mac(ip):
    arp_request = scapy.ARP(pdst=ip)
    broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
    arp_request_broadcast = broadcast/arp_request
    answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
    return answered_list[0][1].hwsrc

def sniff(interface):
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)

def process_sniffed_packet(packet):
    if packet.haslayer(scapy.ARP) and packet[scapy.ARP].op == 2:
        try:
            real_mac = get_mac(packet[scapy.ARP].psrc)
            response_mac = packet[scapy.ARP].hwsrc

            if real_mac != response_mac:
                print("[+] Arp spoof detected !")
        except IndexError:
            pass

try:
    options=get_arguments()
    interface=options.interface
    sniff(interface)

except KeyboardInterrupt:
    print("\n [-] Quitting...\n")


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 29 18:31:14 2019

@author: 64360
"""

import os
import cv2
import numbers
import numpy as np
import matplotlib.pyplot as plt

def random_translation(seg_img, lab_img, probability=1.0, border_usage=0.5, default_border=0.25, label_of_interest=None, default_pixel=None, default_label=None):
    """
    Translates a random selection of the input by a random amount. The translation varies between datapoints, but is the same for
    all inputs of a single datapoint.

    Parameters
    ----------
    source : iterable
        An iterable over a number of datapoints where each datapoint is a tuple of a list of inputs and a parameter dictionary.
    probability: float
        The probability of translating the input. If it is below 1, some inputs will be passed through unchanged.
    border_usage: float
        If a label_of_interest is set, the border is defined as the area between the rectangular bounding box around the region of
        interest and the edge of the image. This parameter defines how much of that border may be used for translation or, in other
        words, how much of that border may end up outside of the new image.
    default_border: float
        The amount of translation that is possible with respect to the input size, if label_of_interest is either not specified or
        not found in the input. border_usage does not apply to the default_border.
    label_of_interest: int
        The label of interest in the ground truth.
    default_pixel: number or None
        The fill value for the image input. If None, the minimum value will be used.
    default_label: number or None
        The fill value for the ground truth input. If None, the minimum value will be used.

    Returns
    -------
    gen : generator
        A generator that yields each transformed datapoint as a tuple of a list of inputs and a parameter dictionary.
    """
    if not isinstance(probability, numbers.Number):
        raise TypeError("Probability must be a number! Received: {}".format(type(probability)))

    if not isinstance(border_usage, numbers.Number):
        raise TypeError("Border usage must be a number! Received: {}".format(type(border_usage)))

    if not isinstance(default_border, numbers.Number):
        raise TypeError("Default border must be a number! Received: {}".format(type(default_border)))

    if label_of_interest is not None and not isinstance(label_of_interest, numbers.Number):
        raise TypeError("Label of interest must be a number! Received: {}".format(type(label_of_interest)))

    if default_pixel is not None and not isinstance(default_pixel, numbers.Number):
        raise TypeError("Default pixel must be a number! Received: {}".format(type(default_pixel)))

    if default_label is not None and not isinstance(default_label, numbers.Number):
        raise TypeError("Default label must be a number! Received: {}".format(type(default_label)))

    if probability > 1.0 or probability < 0.0:
        raise ValueError("Probability must be between 0.0 and 1.0! Received: {}".format(probability))

    if border_usage > 1.0 or border_usage < 0.0:
        raise ValueError("Border usage must be between 0.0 and 1.0! Received: {}".format(border_usage))

    if default_border > 1.0 or default_border < 0.0:
        raise ValueError("Default border must be between 0.0 and 1.0! Received: {}".format(default_border))

    def non(s):
        return s if s < 0 else None

    def mom(s):
        return max(0, s)

    if(np.random.rand() < probability):

        if label_of_interest is None or label_of_interest not in lab_img:
            xdist = default_border * lab_img.shape[0]
            ydist = default_border * lab_img.shape[1]

        else:
            itemindex = np.where(lab_img == 255)

            xdist = min(np.min(itemindex[0]), lab_img.shape[0] - np.max(itemindex[0])) * border_usage
            ydist = min(np.min(itemindex[1]), lab_img.shape[1] - np.max(itemindex[1])) * border_usage

        ox = np.random.randint(-xdist, xdist) if xdist >= 1 else 0
        oy = np.random.randint(-ydist, ydist) if ydist >= 1 else 0

        fill_value = default_pixel if default_pixel is not None else np.min(seg_img)
        shift_seg = np.full_like(seg_img, fill_value)
        shift_seg[mom(ox):non(ox), mom(oy):non(oy), ...] = seg_img[mom(-ox):non(-ox), mom(-oy):non(-oy), ...]

        fill_value = default_label if default_label is not None else np.min(lab_img)
        shift_lab = np.full_like(lab_img, fill_value)
        shift_lab[mom(ox):non(ox), mom(oy):non(oy), ...] = lab_img[mom(-ox):non(-ox), mom(-oy):non(-oy), ...]

    return shift_seg, shift_lab

seg_img_dir = "datasets/CT/DM/test/"
lab_img_dir = "datasets/CT/GT/test/"

label_list = []
image_list = []

img_name_list = os.listdir(seg_img_dir)
lab_name_list = os.listdir(lab_img_dir)

for i in range(len(img_name_list)):
    img_path = seg_img_dir + img_name_list[i]
    lab_path = lab_img_dir + lab_name_list[i]

    img = cv2.imread(img_path, 0)
    lab = cv2.imread(lab_path, 0)

    image, label = random_translation(img, lab, probability=1.0, border_usage=0.8, default_border=0.25, label_of_interest=255, default_pixel=None, default_label=None)

    plt.figure(figsize = (20, 20))

    plt.subplot(141)
    plt.imshow(img, cmap = 'gray')
    plt.title("OrgImg: %d" % i)

    plt.subplot(142)
    plt.imshow(lab, cmap = 'gray')
    plt.title("OrgLab: %d" % i)

    plt.subplot(143)
    plt.imshow(image, cmap = 'gray')
    plt.title("RdmRotImg: %d" % i)

    plt.subplot(144)
    plt.imshow(label, cmap = 'gray')
    plt.title("RdmRotLab: %d" % i)

    plt.show()