M = [[1,2,3],[2,0,1],[5,5,6],[-3,4,4]]
#Calculates whether a specified matrix M is a rectangular matrix

def is_rectangle(M):
    rect = True
    row_length = len(M[0])
    for i in range(0, len(M)):
        if (len(M[i]) != row_length):
            rect = False
    return rect

print(is_rectangle(M))


import csv
import codecs
import re
import xml.etree.cElementTree as ET

import cerberus

OSM_PATH = "map.osm"

NODES_PATH = "nodes.csv"
NODE_TAGS_PATH = "nodes_tags.csv"
WAYS_PATH = "ways.csv"
WAY_NODES_PATH = "ways_nodes.csv"
WAY_TAGS_PATH = "ways_tags.csv"

LOWER_COLON = re.compile(r'^([a-z]|_)+:([a-z]|_)+')
PROBLEMCHARS = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')
PHONENUM = re.compile(r'\+1\s\d{3}\s\d{3}\s\d{4}')
POSTCODE = re.compile(r'[A-z]\d[A-z]\s?\d[A-z]\d')

# Make sure the fields order in the csvs matches the column order in the sql table schema
NODE_FIELDS = ['id', 'lat', 'lon', 'user', 'uid', 'version', 'changeset', 'timestamp']
NODE_TAGS_FIELDS = ['id', 'key', 'value', 'type']
WAY_FIELDS = ['id', 'user', 'uid', 'version', 'changeset', 'timestamp']
WAY_TAGS_FIELDS = ['id', 'key', 'value', 'type']
WAY_NODES_FIELDS = ['id', 'node_id', 'position']

def update_phone_num(phone_num):
    """
    Clean phone number for insertion into SQL database
    """
    # Check for valid phone number format
    m = PHONENUM.match(phone_num)
    if m is None:
        # Convert all dashes to spaces
        if "-" in phone_num:
            phone_num = re.sub("-", " ", phone_num)
        # Remove all brackets
        if "(" in phone_num or ")" in phone_num:
            phone_num = re.sub("[()]", "", phone_num)
        # Space out 10 straight numbers
        if re.match(r'\d{10}', phone_num) is not None:
            phone_num = phone_num[:3] + " " + phone_num[3:6] + " " + phone_num[6:]
        # Space out 11 straight numbers
        elif re.match(r'\d{11}', phone_num) is not None:
            phone_num = phone_num[:1] + " " + phone_num[1:4] + " " + phone_num[4:7] + " " + phone_num[7:]
        # Add full country code
        if re.match(r'\d{3}\s\d{3}\s\d{4}', phone_num) is not None:
            phone_num = "+1 " + phone_num
        # Add + in country code
        elif re.match(r'1\s\d{3}\s\d{3}\s\d{4}', phone_num) is not None:
            phone_num = "+" + phone_num
        # Ignore tag if no area code and local number (<10 digits)
        elif sum(c.isdigit() for c in phone_num) < 10:
            return None
    return phone_num

def load_new_tag(element, secondary, default_tag_type):
    """
    Load a new tag dict to go into the list of dicts for way_tags, node_tags
    """
    new = {}
    new['id'] = element.attrib['id']
    if ":" not in secondary.attrib['k']:
        new['key'] = secondary.attrib['k']
        new['type'] = default_tag_type
    else:
        post_colon = secondary.attrib['k'].index(":") + 1
        new['key'] = secondary.attrib['k'][post_colon:]
        new['type'] = secondary.attrib['k'][:post_colon - 1]

    return new

# return the updated names
def update_name(name, mapping):
    name = name.split(' ')
    for i in range(len(name)):
        if name[i] in mapping:
            name[i] = mapping[name[i]]
            name[i] = string_case(name[i])
        else:
            name[i] = string_case(name[i])

    name = ' '.join(name)

    return name

def shape_element(element, node_attr_fields=NODE_FIELDS, way_attr_fields=WAY_FIELDS,
                  problem_chars=PROBLEMCHARS, default_tag_type='regular'):
    """Clean and shape node or way XML element to Python dict"""

    node_attribs = {}
    way_attribs = {}
    way_nodes = []
    tags = []

    if element.tag == 'node':
        for attrib in element.attrib:
            if attrib in NODE_FIELDS:
                node_attribs[attrib] = element.attrib[attrib]

        for child in element:
            node_tag = {}
            if LOWER_COLON.match(child.attrib['k']):
                node_tag['type'] = child.attrib['k'].split(':', 1)[0]
                node_tag['key'] = child.attrib['k'].split(':', 1)[1]
                node_tag['id'] = element.attrib['id']
                node_tag['value'] = update_name(child.attrib['v'],mapping)
                tags.append(node_tag)
            elif PROBLEMCHARS.match(child.attrib['k']):
                continue
            else:
                node_tag['key'] = child.attrib['k']
                node_tag['type'] = 'regular'
                node_tag['id'] = element.attrib['id']
                node_tag['value'] = update_name(child.attrib['v'],mapping)
                tags.append(node_tag)

        return {'node': node_attribs, 'node_tags': tags}

    elif element.tag == 'way':
        for attrib in element.attrib:
            if attrib in WAY_FIELDS:
                way_attribs[attrib] = element.attrib[attrib]

        position = 0
        for child in element:
            way_tag = {}
            way_node = {}

            if child.tag == 'tag':
                if LOWER_COLON.match(child.attrib['k']):
                    way_tag['type'] = child.attrib['k'].split(':', 1)[0]
                    way_tag['key'] = child.attrib['k'].split(':', 1)[1]
                    way_tag['id'] = element.attrib['id']
                    way_tag['value'] = update_name(child.attrib['v'],mapping)
                    tags.append(way_tag)
                elif PROBLEMCHARS.match(child.attrib['k']):
                    continue
                else:
                    way_tag['type'] = 'regular'
                    way_tag['key'] = child.attrib['k']
                    way_tag['id'] = element.attrib['id']
                    way_tag['value'] = update_name(child.attrib['v'],mapping)
                    tags.append(way_tag)

            elif child.tag == 'nd':
                way_node['id'] = element.attrib['id']
                way_node['node_id'] = child.attrib['ref']
                way_node['position'] = position
                position += 1
                way_nodes.append(way_node)

        return {'way': way_attribs, 'way_nodes': way_nodes, 'way_tags': tags}

# ================================================== #
#               Helper Functions                     #
# ================================================== #
def get_element(osm_file, tags=('node', 'way', 'relation')):
    """Yield element if it is the right type of tag"""

    context = ET.iterparse(osm_file, events=('start', 'end'))
    _, root = next(context)
    for event, elem in context:
        if event == 'end' and elem.tag in tags:
            yield elem
            root.clear()

class UnicodeDictWriter(csv.DictWriter, object):
    """Extend csv.DictWriter to handle Unicode input"""

    def writerow(self, row):
        super(UnicodeDictWriter, self).writerow({
            k: (v.encode('utf-8') if isinstance(v, unicode) else v) for k, v in row.iteritems()
        })

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

# ================================================== #
#               Main Function                        #
# ================================================== #
def process_map(file_in, validate):
    """Iteratively process each XML element and write to csv(s)"""

    with codecs.open(NODES_PATH, 'w') as nodes_file, \
         codecs.open(NODE_TAGS_PATH, 'w') as nodes_tags_file, \
         codecs.open(WAYS_PATH, 'w') as ways_file, \
         codecs.open(WAY_NODES_PATH, 'w') as way_nodes_file, \
         codecs.open(WAY_TAGS_PATH, 'w') as way_tags_file:

        nodes_writer = UnicodeDictWriter(nodes_file, NODE_FIELDS)
        node_tags_writer = UnicodeDictWriter(nodes_tags_file, NODE_TAGS_FIELDS)
        ways_writer = UnicodeDictWriter(ways_file, WAY_FIELDS)
        way_nodes_writer = UnicodeDictWriter(way_nodes_file, WAY_NODES_FIELDS)
        way_tags_writer = UnicodeDictWriter(way_tags_file, WAY_TAGS_FIELDS)

        nodes_writer.writeheader()
        node_tags_writer.writeheader()
        ways_writer.writeheader()
        way_nodes_writer.writeheader()
        way_tags_writer.writeheader()

        validator = cerberus.Validator()

        for element in get_element(file_in, tags=('node', 'way')):
            el = shape_element(element)
            if el:
                if element.tag == 'node':
                    nodes_writer.writerow(el['node'])
                    node_tags_writer.writerows(el['node_tags'])
                elif element.tag == 'way':
                    ways_writer.writerow(el['way'])
                    way_nodes_writer.writerows(el['way_nodes'])
                    way_tags_writer.writerows(el['way_tags'])

if __name__ == '__main__':
    # Note: Validation is ~ 10X slower. For the project consider using a small
    # sample of the map when validating.
    process_map(OSM_PATH, validate=True)

Data Analyst Nano Degree/Data_Wrangling_in_Python_and_SQLite/Import into CSV.py


# Bot config stuff
bot_name = "qbot"
prefix = "$"

# CONSTANTS
MAX_TITLE_LENGTH = 35
MAX_DESCRIPTION_LENGTH = 140

# ERR MSGS
CORRECT_TITLE_FORMAT = "{game}@{hour:minutes}{am/pm}"
TITLE_FORMAT_INCORRECT = "Incorrect title format. ```correct format: {}```".format(CORRECT_TITLE_FORMAT)
DESCR_TOO_LONG = "Max description length is " + str(MAX_DESCRIPTION_LENGTH)
LOBBY_ALREADY_EXISTS = "Lobby already exists."

# EMOJIS
WHITE_HEAVY_CHECK_MARK = '\N{WHITE HEAVY CHECK MARK}'

# STATUS COLORS
PENDING = 0xfecb01
ACTIVE = 0x4d8523
LIVE = 0x07f81a
EXPIRED = 0xff0000


def play_round(ai: chr, usr: chr) -> int:
    ai_play = -1
    usr_play = -1

    # Switch/Match statement?
    if ai == 'A':
        ai_play = 1
    elif ai == 'B':
        ai_play = 2
    elif ai == 'C':
        ai_play = 3

    if usr == 'X':
        usr_play = 1
    elif usr == 'Y':
        usr_play = 2
    elif usr == 'Z':
        usr_play = 3

    # 0 for loss
    # 3 for draw
    # 6 for win
    # + move.

    if usr_play == ai_play:
        # draw
        return usr_play + 3
    elif usr_play == 1 and ai_play == 2:
        return usr_play + 0
    elif usr_play == 2 and ai_play == 1:
        return usr_play + 6
    elif usr_play == 1 and ai_play == 3:
        return usr_play + 6
    elif usr_play == 3 and ai_play == 1:
        return usr_play + 0
    elif usr_play == 2 and ai_play == 3:
        return usr_play + 0
    elif usr_play == 3 and ai_play == 2:
        return usr_play + 6
    else:
        print("ERROR")
        return -1

def throw_game(a, u):

    if u == 'X':
        if a == 'A':
            return 3
        elif a == 'B':
            return 1
        else:
            return 2
    elif u == 'Y':
        if a == 'A':
            ai_play = 1
        elif a == 'B':
            ai_play = 2
        elif a == 'C':
            ai_play = 3
        return ai_play + 3
    elif u == 'Z':
        if a == 'A':
            return 2 + 6
        elif a == 'B':
            return 3 + 6
        else:
            return 1 + 6

if __name__ == '__main__':

    # with open('day2_test.txt') as in_file:
    with open('day2_given.txt') as in_file:
        data = [line.rstrip() for line in in_file]

    game = 0
    thrown = 0
    for moves in data:
        game += play_round(moves[0], moves[2])
        thrown += throw_game(moves[0], moves[2])

    print("TOTAL SCORE", game)
    print("ULTIMATE WIN:", thrown)


from setuptools import setup, find_packages
import os.path
import re

# reading package's version (same way sqlalchemy does)
with open(
    os.path.join(os.path.dirname(__file__), 'btn_bot', '__init__.py')
) as v_file:
    package_version = \
        re.compile('.*__version__ = \'(.*?)\'', re.S)\
        .match(v_file.read())\
        .group(1)

setup(
    name='telegram template bot',
    version=package_version,
    author='Mohamad Khajezade',
    description='An MVC template to develope telegram bot',
    packages=find_packages(),
)


import ahapi
import plugins.configuration
import plugins.lists
import time

""" Generic add-block endpoint for Blocky/4"""

async def process(state: plugins.configuration.BlockyConfiguration, request, formdata: dict) -> dict:
    now = int(time.time())
    force = bool(formdata.get("force", False))
    ip = formdata.get("ip")
    reason = formdata.get("reason", "no reason specified")
    expires = int(formdata.get("expires", 0))
    if not expires:
        expires = now + state.default_expire_seconds
    host = formdata.get("host", plugins.configuration.DEFAULT_HOST_BLOCK)

    try:
        state.block_list.add(ip=ip, expires=expires, reason=reason, host=host, force=force)
    except plugins.lists.BlockListException as e:
        return {"success": False, "status": "failure", "message": str(e)}

    # All good!
    return {"success": True, "status": "blocked", "message": f"IP {ip} added to block list"}

def register(config: plugins.configuration.BlockyConfiguration):
    return ahapi.endpoint(process)


import torch
import torch.nn as nn
from torch.autograd import Function
from torchsummary import summary

def weight_init(model):

    classname = model.__class__.__name__
    if classname.find('Conv') != -1:
        nn.init.xavier_normal_(model.weight.data)
        model.bias.data.fill_(0)

class VectorQuantization(Function):

    @staticmethod
    def forward(ctx, inputs, codebook):
        """

        Parameters
        ----------
        ctx : context. It is used to store arbitrary data that can be retrieved
              during the backward pass.
        inputs : Tensor
            Encoded image vector(z_e_x).
        codebook : Tensor
            Embeddings.

        Returns
        -------
        idx : quantized indices / latents.

        """

        with torch.no_grad():

            input_size = inputs.size()
            emb_size = codebook.size(1)
            flat_input = inputs.view(-1, emb_size)

            codebook_sq = torch.sum(codebook ** 2, dim = 1)
            inputs_sq = torch.sum(inputs ** 2, dim = 1, keepdim = True)
            #torch.addmm: result = beta * input + alpha * (mat1[i] @ mat2[i])
            l2_dis = torch.addmm(input = codebook_sq + inputs_sq,
                                 mat1 = flat_input, mat2 = codebook.t(),
                                 alpha = 2.0, beta = 1.0)
            _, idx_flatten = torch.min(l2_dis, dim = 1)
            idx = idx_flatten(*input_size[:-1])
            ctx.mark_non_differentiable(idx)

            return idx

    @staticmethod
    def backward(ctx, grad_outputs):

        raise RuntimeError('Trying to call backward on graph containing `Vector Quantization`'
                           'which is non-differentiable. Use VQStraightThrough instead.')

class VQStraightThrough(Function):

    @staticmethod
    def forward(ctx, inputs, codebook):

        idx = VQ(inputs, codebook)
        flat_idx = idx.view(-1)
        ctx.save_for_backward(flat_idx, codebook)
        ctx.mark_non_differentiable(flat_idx)
        codes_flatten = torch.index_select(input = codebook, dim = 0, index = flat_idx)
        codes = codes_flatten.view_as(inputs)

        return (codes, flat_idx)

    @staticmethod
    def backward(ctx, grad_outputs, grad_indices):

        grad_inputs, grad_codebook = None, None

        if ctx.needs_input_grad[0]:

            grad_inputs = grad_outputs.clone()

        if ctx.needs_input_grad[1]:

            idx, codebook = ctx.saved_tensors
            emb_size = codebook.size(1)
            flat_grad_output = (grad_outputs.contiguous().view(-1, emb_size))
            grad_codebook = torch.zeros_like(codebook)
            grad_codebook.index_add_(0, idx, flat_grad_output)

        return (grad_inputs, grad_codebook)

VQ = VectorQuantization.apply
VQ_ST =  VQStraightThrough.apply

class VQEmbedding(nn.Module):

    def __init__(self, K: int, D: int):

        """

        Parameters
        ----------
        K : int
            Total number of embeddings in codebook.
        D : int
            Dimensions of every embedding in the codebook.

        """

        super().__init__()
        self.vq_embs = nn.Embedding(K, D)
        self.vq_embs.weight.data.uniform_(-1.0 / K, 1.0 / K)

    def forward(self, z_e_x):

        z_e_x = z_e_x.permute(0,2,3,1).contiguous()
        latents = VQ(z_e_x, self.vq_embs.weight)
        return latents

    def straight_through_forward(self, z_e_x):

        zex = z_e_x.permute(0,2,3,1).contiguous()
        z_q_x, idx = VQ_ST(zex, self.vq_embs.weight.detach())
        z_q_x = z_q_x.permute(0,3,1,2).contiguous()
        flat_zqx_tilde = torch.index_select(self.vq_embs.weight, dim = 0, index = idx)
        zqx_tilde = flat_zqx_tilde.view_as(zex)
        zqx_tilde = zqx_tilde.permute(0,3,1,2).contiguous()
        return z_q_x, zqx_tilde

class ResidualBlock(nn.Module):

    def __init__(self, channels):

        super().__init__()
        self.resblock = nn.Sequential(nn.ReLU(inplace = True),
                                      nn.Conv2d(channels, channels, kernel_size = 3, stride = 1, padding = 1),
                                      nn.BatchNorm2d(channels),
                                      nn.ReLU(inplace = True),
                                      nn.Conv2d(channels, channels, 1),
                                      nn.BatchNorm2d(channels)
                                      )

    def forward(self, x):

        return self.resblock(x) + x

class VQVAE(nn.Module):

    def __init__(self, in_c, out_c, K = 512):

        super().__init__()
        self.encoder = nn.Sequential(nn.Conv2d(in_c, out_c, kernel_size = 4, stride = 2, padding = 1),
                                     nn.BatchNorm2d(out_c),
                                     nn.ReLU(inplace = True),
                                     nn.Conv2d(out_c, out_c, kernel_size = 4, stride = 2, padding = 1),
                                     ResidualBlock(out_c),
                                     ResidualBlock(out_c)
                                     )
        self.codebook = VQEmbedding(K, D = out_c)

        self.decoder = nn.Sequential(ResidualBlock(out_c),
                                     ResidualBlock(out_c),
                                     nn.ReLU(inplace = True),
                                     nn.ConvTranspose2d(out_c, out_c, kernel_size = 4, stride = 2, padding = 1),
                                     nn.BatchNorm2d(out_c),
                                     nn.ReLU(inplace = True),
                                     nn.ConvTranspose2d(out_c, in_c, kernel_size = 4, stride = 2, padding = 1),
                                     nn.Tanh()
                                     )

        self.apply(weight_init)

    def encode(self, x):

        z_e_x = self.encoder(x)
        latents = self.codebook(z_e_x)
        return latents

    def decode(self, latents):

        z_q_x = self.codebook.vq_embs(latents).permute(0,3,1,2)
        x_tilde = self.decoder(z_q_x)
        return x_tilde

    def forward(self, x):

        z_e_x = self.encoder(x)
        z_q_x_st, z_q_x = self.codebook.straight_through_forward(z_e_x)
        x_tilde = self.decoder(z_q_x_st)
        return z_e_x, z_q_x, x_tilde

if __name__ == '__main__':

    vqvae = VQVAE(3, 256).to(torch.device('cuda'))
    print(summary(vqvae, (3,32,32)))


"""A collection of general and config helper functions"""

from .material import mat_to_dict
from honeybee.face import Face
from honeybee.shade import Shade
from honeybee.aperture import Aperture
from honeybee.door import Door
from honeybee.facetype import face_types
from honeybee.typing import clean_and_id_string, clean_string

def get_unit_system(file_3dm):
    """Get units from a 3dm file object.

    Args:
        file_3dm: A Rhino3dm file object.

    Returns:
        Rhino3dm file unit as a string.
    """
    UNITS = ['Meters', 'Millimeters', 'Feet', 'Inches', 'Centimeters']

    try:
        file_unit = file_3dm.Settings.ModelUnitSystem
    except AttributeError:
        raise TypeError(f'Expected a Rhino 3dm file object not {type(file_3dm)}')

    unit = str(file_unit).split('.')[-1]

    if unit not in UNITS:
        raise ValueError(
            f'{unit} is not currently supported. Supported units are {UNITS}.'
        )

    return unit

def child_layer_control(config, layer_name):
    """Checks if child layers are requested for a layer in the config file.

    Args:
        config: A dictionary of the config settings.
        layer_name: A text string of the layer name

    Returns:
        A bool.
    """

    if 'include_child_layers' in config['layers'][layer_name] and \
            config['layers'][layer_name]['include_child_layers']:
        return True
    else:
        return False

def grid_controls(config, layer_name):
    """Returns grid controls for a layer from the config.

    Args:
        grid_controls: A list of grid controls from the config file

    Returns:
        A tuple of grid controls

        -   grid_size,
        -   grid_offset.

        if valid grid settings are found in the config file or None
    """

    if 'grid_settings' in config['layers'][layer_name] and \
            config['layers'][layer_name]['exclude_from_rad']:
        grid_controls = config['layers'][layer_name]['grid_settings']
        return grid_controls['grid_size'], grid_controls['grid_offset']

def check_parent_in_config(file_3dm, config, layer_name, parent_layer_name):
    """Checks if the parent layer of a layer is already mentioned in the config file.

    This function will return True if the parent layer of a layer is already mentioned
    in the config file and child layers are requested from that parent layer in the
    config file.

    Args:
        file_3dm: A rhino3dm file objects.
        config: A dictionary of the config settings.
        layer_name: A text string of the layer name
        parent_layer_name: A text string of the parent layer name

    Returns:
        A bool.
    """

    if parent_layer_name in config['layers'] and\
            'include_child_layers' in config['layers'][parent_layer_name] and\
            config['layers'][parent_layer_name]['include_child_layers']:
        return True
    else:
        return False

def face3d_to_hb_face_with_face_type(config, face_obj, name, layer_name):
    """Create a Honeybee Face object with a specific face_type.

    This function returns a Honeybee Face object with a specific face_type requested
    in the config file and also assign a radiance material to the face if requested
    from the config file.

    Args:
        config: A dictionary of the config settings.
        face_obj: A Ladybug Face3d object.
        name: A text string of the name of the rhino object.
        layer_name: A text string of the rhino layer name.

    Returns:
        A Honeybee Face object.
    """

    obj_name = name or clean_and_id_string(layer_name)
    args = [clean_string(obj_name), face_obj]

    if config['layers'][layer_name]['honeybee_face_type'] == 'roof':
        face_type = face_types.roof_ceiling

    elif config['layers'][layer_name]['honeybee_face_type'] == 'wall':
        face_type = face_types.wall

    elif config['layers'][layer_name]['honeybee_face_type'] == 'floor':
        face_type = face_types.floor

    elif config['layers'][layer_name]['honeybee_face_type'] == 'airwall':
        face_type = face_types.air_boundary

    args.append(face_type)
    hb_face = Face(*args)
    hb_face.display_name = args[0]

    if 'radiance_material' in config['layers'][layer_name]:
        radiance_modifiers = mat_to_dict(config['sources']['radiance_material'])
        hb_face.properties.radiance.modifier = radiance_modifiers[config
            ['layers'][layer_name]['radiance_material']]
        return hb_face
    else:
        return hb_face

def face3d_to_hb_face_with_rad(config, face_obj, name, layer_name):
    """Create a Honeybee Face object with a radiance material assigned to it.

    Args:
        config: A dictionary of the config settings.
        face_obj: A Ladybug Face3d object.
        name: A text string of the name of the rhino object.
        layer_name: A text string of the rhino layer name.

    Returns:
        A Honeybee Face object.
    """

    obj_name = name or clean_and_id_string(layer_name)
    args = [clean_string(obj_name), face_obj]
    hb_face = Face(*args)
    hb_face.display_name = args[0]

    if 'radiance_material' in config['layers'][layer_name]:
        radiance_modifiers = mat_to_dict(config['sources']['radiance_material'])
        hb_face.properties.radiance.modifier = radiance_modifiers[config
            ['layers'][layer_name]['radiance_material']]
        return hb_face
    else:
        return hb_face

def face3d_to_hb_object(config, face_obj, name, layer_name):
    """Create Honeybee Aperture, Shade, and Door objects.

    Args:
        config: A dictionary of the config settings.
        face_obj: A Ladybug Face3d object.
        name: A text string of the name of the rhino object.
        layer_name: A text string of the rhino layer name.

    Returns:
        A tuple of lists;

        -   Honeybee Aperture objects,
        -   Honeybee Shade objects,
        -   Honeybee Door objects.

        List will be empty if no objects are found for that Honeybee object.
    """

    hb_apertures, hb_doors, hb_shades = ([], [], [])

    obj_name = name or clean_and_id_string(layer_name)
    args = [clean_string(obj_name), face_obj]

    def hb_object(config, layer_name, hb_obj):
        if 'radiance_material' in config['layers'][layer_name]:
            radiance_modifiers = mat_to_dict(config['sources']['radiance_material'])
            hb_obj.properties.radiance.modifier = radiance_modifiers[config[
                'layers'][layer_name]['radiance_material']]
            return hb_obj
        else:
            return hb_obj

    if config['layers'][layer_name]['honeybee_face_object'] == 'aperture':
        hb_aperture = Aperture(*args)
        hb_aperture.display_name = args[0]
        hb_apertures.append(hb_object(config, layer_name, hb_aperture))

    elif config['layers'][layer_name]['honeybee_face_object'] == 'door':
        hb_door = Door(*args)
        hb_door.display_name = args[0]
        hb_doors.append(hb_object(config, layer_name, hb_door))

    elif config['layers'][layer_name]['honeybee_face_object'] == 'shade':
        hb_shade = Shade(*args)
        hb_shade.display_name = args[0]
        hb_shades.append(hb_object(config, layer_name, hb_shade))

    return hb_apertures, hb_doors, hb_shades


import pandas as pd
import plotly.express as px
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
import readline

import functions.microfim as mf
import functions.microdir as md
import functions.microinterestmeasures as mim
import functions.microimport as mi

import plotly.graph_objects as go
from plotly.subplots import make_subplots

from sklearn.metrics import pairwise_distances
from sklearn import manifold

"""
"""

def calculate_distance_matrix(df_pattern, metadata):
    """
    """
    # filter samples from metadata
    samples = metadata.iloc[:,0].to_list()
    print(samples)
    # filter pattern table
    pattern_table_filt = df_pattern[samples]

    data = pattern_table_filt.to_numpy()

    #jaccard distance matrix
    data_t = np.transpose(data)
    dis_matrix = pairwise_distances(data_t, metric = 'jaccard')

    return dis_matrix

def plot_heatmap(dis_matrix, metadata, outputdir, output_name, format):
    """
    """
    samples = metadata.iloc[:,0].to_list()
    dis_heatmap = px.imshow(dis_matrix,
                    x=samples,
                    y=samples)

    dis_heatmap.update_layout(coloraxis_colorbar=dict(
        title="Jaccard index",
        tickvals=[0,1],
        ticktext=["0","1"],
        lenmode="pixels", len=100,
    ))

    if format == 'SVG':
        save_name = output_name + '_heatmap.svg'
        dis_heatmap.write_image(os.path.join(outputdir, save_name))

    elif format == 'HTML':
        save_name = output_name + '_heatmap.html'
        dis_heatmap.write_html(os.path.join(outputdir, save_name))

    return

def heatmap_function():
    """
    """
    return

def hist_plot(df_pattern, col_to_group):
    """
    """
    title = col_to_group + ' distribution'
    hist_df = df_pattern.groupby([col_to_group]).size().reset_index(name='Counts')
    hist = px.bar(hist_df, x=col_to_group, y='Counts', height=400, width=800,
                  color=col_to_group, title='Pattern length distribution')

    return hist

def save_hist_plot(outputdir, plot, plot_name, format, set_col):
    """
    """

    if format == 'SVG':
        save_name = plot_name + '_' + set_col + '_hist.svg'
        plot.write_image(os.path.join(outputdir, save_name))

    elif format == 'HTML':
        save_name = plot_name + '_' + set_col + '_hist.html'
        plot.write_image(os.path.join(outputdir, save_name))

    return

def scatter_plot_2d(df_pattern, col_to_group1, col_to_group2):
    """
    """
    title = col_to_group1 + ' and ' + col_to_group2 + ' distribution'
    scatter_df = df_pattern.groupby([col_to_group1, col_to_group2]).size().reset_index(name='Counts')
    scatter_2d = px.scatter(scatter_df, x=col_to_group1, y="Counts", color=col_to_group2)

    return scatter_2d

def save_scatter_2d_plot(outputdir, plot, plot_name, format):
    """
    """

    if format == 'SVG':
        save_name = plot_name + '_hist.svg'
        plot.write_image(os.path.join(outputdir, save_name))

    elif format == 'HTML':
        save_name = plot_name + '_hist.html'
        plot.write_image(os.path.join(outputdir, save_name))

    return

def scatter_plot_3d(df_pattern, col_to_group1, col_to_group2, col_to_group3):
    """
    """
    title = col_to_group1 + ' and ' + col_to_group2 + ' and ' + col_to_group3 + ' distribution'
    scatter_df = df_pattern.groupby([col_to_group1, col_to_group2, col_to_group3]).size().reset_index(name='Counts')
    scatter3d = px.scatter_3d(scatter_df, x='Counts', y=col_to_group1, z=col_to_group3, color=col_to_group2)

    return scatter_3d

def save_scatter_3d_plot(outputdir, plot, plot_name, format):
    """
    """

    if format == 'SVG':
        save_name = plot_name + '_hist.svg'
        plot.write_image(os.path.join(outputdir, save_name))

    elif format == 'HTML':
        save_name = plot_name + '_hist.html'
        plot.write_image(os.path.join(outputdir, save_name))

    return

def set_col_name(plot_name):
    """
    """
    col_name = [each_string.lower() for each_string in plot_name]
    name = col_name[0][:3]
    return name


import plotly.express as px
from dash import Dash, html, dcc, dash_table

class SCVisualAnalytics:
    def __init__(self, dataframe):
        self.df = dataframe

        self.app = Dash(__name__)

        self.app.layout = html.Div(children=[
            html.H1(children='Welcome to SCVisualAnalytics'),

            html.Div(children=[
                dcc.Graph(
                    id="mean_price",
                    figure=self.__get_pie_mean_rental("price", "location", title="Mean price of rental"),
                    style={'width': '50%'}
                ),
                dcc.Graph(
                    id="mean_price_re",
                    figure=self.__get_pie_mean_rental("deposit_price", "location", title='Mean deposit of rental'),
                    style={'width': '50%'}
                ),
            ], style={
                'display': 'flex',
                'flex-direction': 'row'
            }),

            dash_table.DataTable(
                id='table',
                columns=self.__get_table_columns(),
                data=self.df.to_dict('records'),
                style_cell=dict(textAlign='left'),
                style_header=dict(backgroundColor="paleturquoise"),
                style_data=dict(backgroundColor="lavender"),
                page_size=10
            ),
        ])

    def run_server(self, debug: bool = False):
        print("NOTE: Preparing server...")
        self.app.run_server(debug=debug)

    def __get_table_columns(self):
        df_c = self.df
        columns = []
        for i in df_c.columns:
            if i in ['title', 'location', 'price', 'area', 'media_price', 'rooms_number', 'deposit_price']:
                columns.append({"name": i, "id": i})
        return columns

    def __get_mean_by_column(self, column):
        return self.df.groupby(column).mean().reset_index()

    def __get_pie_mean_rental(self, values, names, title):
        df_m = self.__get_mean_by_column('location')
        fig = px.pie(self.df, values=df_m[values], names=df_m[names], title=title, hole=.3)
        fig.update_traces(textinfo='value')
        return fig


import json
import os

import botocore
from boto3.dynamodb.conditions import Key
from helpers import check_request, get_dynamodb_table, jsonresponse

TABLE_NAME = os.environ.get('TABLE', 'PhoneNumbers')
def lambda_handler(message, context):

    try:
        check_request(message,'GET')

        table = get_dynamodb_table(TABLE_NAME)
        phonenumber = message['pathParameters']['id']

        response = table.query(
            KeyConditionExpression=Key('phonenumber').eq(phonenumber)
        )

        return jsonresponse(response['Items'], 200)

    except botocore.exceptions.ClientError as error:
        return jsonresponse(error, 500)


"""
bfs 이용한 풀이
"""
from sys import stdin
from collections import deque, defaultdict

input = lambda: stdin.readline().rstrip()
INF = 50002

def bfs() -> int:
    dq = deque([(0, 0, 0)])
    while dq:
        x, y, cnt = dq.popleft()

        if y == T:
            return cnt

        for i in range(x - 2, x + 3):
            for j in range(y - 2, y + 3):
                if x < 0 or y < 0:
                    continue
                if j in crds[i]:
                    dq.append((i, j, cnt + 1))
                    crds[i].remove(j)

    return -1

if __name__ == "__main__":
    n, T = map(int, input().split())
    crds = defaultdict(list)
    for _ in range(n):
        x, y = map(int, input().split())
        crds[x].append(y)

    print(bfs())


import argparse
import os

def get_parser():

    parser = argparse.ArgumentParser()

    parser.add_argument('--save_dir', type=str, default='path/to/save/models',
                        help='path where checkpoints will be saved')

    parser.add_argument('--model_name', type=str, default='model',
                        help='save_dir/project_name/model_name will be the path where logs and checkpoints are stored')

    parser.add_argument('--suff', type=str, default='',
                        help='the id of the dictionary to load for training')

    parser.add_argument('--image_model', type=str, default='resnet50', choices=['resnet18', 'resnet50', 'resnet101',
                                                                                'resnet152', 'inception_v3'])

    parser.add_argument('--recipe1m_dir', type=str, default='path/to/recipe1m',
                        help='directory where recipe1m dataset is extracted')

    parser.add_argument('--aux_data_dir', type=str, default='../data',
                        help='path to other necessary data files (eg. vocabularies)')

    parser.add_argument('--crop_size', type=int, default=224, help='size for randomly or center cropping images')

    parser.add_argument('--image_size', type=int, default=256, help='size to rescale images')

    parser.add_argument('--log_step', type=int , default=10, help='step size for printing log info')

    parser.add_argument('--learning_rate', type=float, default=0.001,
                        help='base learning rate')

    parser.add_argument('--scale_learning_rate_cnn', type=float, default=0.01,
                        help='lr multiplier for cnn weights')

    parser.add_argument('--lr_decay_rate', type=float, default=0.99,
                        help='learning rate decay factor')

    parser.add_argument('--lr_decay_every', type=int, default=2,
                        help='frequency of learning rate decay (default is every epoch)')

    parser.add_argument('--weight_decay', type=float, default=0.)

    parser.add_argument('--embed_size', type=int, default=512,
                        help='hidden size for all projections')

    parser.add_argument('--n_att_units', type=int, default=8,
                        help='number of attention heads in the unit decoder')

    parser.add_argument('--n_att_portions', type=int, default=8,
                        help='number of attention heads in the portion decoder')

    parser.add_argument('--transf_layers_units', type=int, default=2,
                        help='number of transformer layers in the unit decoder')

    parser.add_argument('--dim_feedforward', type=int, default=256,
                        help='dimension of inner linear layer of transformer decoder last linear projection')

    parser.add_argument('--transf_layers_portions', type=int, default=6,
                        help='number of transformer layers in the portion decoder')

    parser.add_argument('--num_epochs', type=int, default=30,
                        help='maximum number of epochs')

    parser.add_argument('--batch_size', type=int, default=128)

    parser.add_argument('--ckpt_epoch', type=int, default=1)

    parser.add_argument('--num_workers', type=int, default=8)

    parser.add_argument('--dropout_encoder', type=float, default=0.3,
                        help='dropout ratio for the image and ingredient encoders')

    parser.add_argument('--dropout_decoder_r', type=float, default=0.3,
                        help='dropout ratio in the instruction decoder')

    parser.add_argument('--dropout_decoder_i', type=float, default=0.3,
                        help='dropout ratio in the ingredient decoder')

    parser.add_argument('--finetune_after', type=int, default=-1,
                        help='epoch to start training cnn. -1 is never, 0 is from the beginning')

    parser.add_argument('--patience', type=int, default=50,
                        help='maximum number of epochs to allow before early stopping')

    parser.add_argument('--maxnumingrs', type=int, default=10,
                        help='maximum number of ingredients')

    parser.add_argument('--maxnumims', type=int, default=5,
                        help='maximum number of images per sample')

    parser.add_argument('--es_metric', type=str, default='loss', choices=['loss', 'accuracy'],
                        help='early stopping metric to track')

    parser.add_argument('--eval_split', type=str, default='val')

    parser.add_argument('--numgens', type=int, default=3)

    parser.add_argument('--resume', dest='resume', action='store_true',
                        help='resume training from the checkpoint in model_name')
    parser.set_defaults(resume=False)

    parser.add_argument('--nodecay_lr', dest='decay_lr', action='store_false',
                        help='disables learning rate decay')
    parser.set_defaults(decay_lr=True)

    parser.add_argument('--load_jpeg', dest='use_lmdb', action='store_false',
                        help='if used, images are loaded from jpg files instead of lmdb')
    parser.set_defaults(use_lmdb=True)

    parser.add_argument('--image_encoder_type', type=str, default='bottom-up',
                        help='could be resnet or bottom-up or resnet_features')  # resnet, bottom-up

    parser.add_argument('--ckpt_name', type=str, default='',
                        help='the name of the checkpoint used for sample.py to load')

    parser.add_argument('--splits', nargs='+', type=str, default=["train", "val"],
                        help='splits used either [train, val] or [test]')

    parser.add_argument('--saving_epochs', nargs='+', type=float, default=[2, 5, 10, 15, 25],
                        help='epochs @ where model is saved.')

    parser.add_argument('--model_type', type=str, default='unit',
                        help='specifies what type of model is trained? 1-stream (unit), or 2-stream(joint), or jointwithcalorie')

    parser.add_argument('--loss_lambda', nargs='+', type=float, default=[1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.01, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], help='loss tradeoff')

    parser.add_argument('--no_train_base_wcalorie', dest='train_base_wcalorie', action='store_false',
                        help='sets to train the base net with calorie loss')
    parser.set_defaults(train_base_wcalorie=True)

    parser.add_argument('--scaled_output', dest='scaled_output', action='store_true',
                        help='sets to train the base net with calorie loss')
    parser.set_defaults(scaled_output=False)

    parser.add_argument('--no_pre_load', dest='pre_load', action='store_false',
                        help='pre load bottom-up image features or not')
    parser.set_defaults(pre_load=True)

    parser.add_argument('--no_image_to_calorie_stream', dest='image_to_calorie_stream', action='store_false',
                        help='whether there is an image to calorie stream or not')
    parser.set_defaults(image_to_calorie_stream=True)

    parser.add_argument('--img_pad_size', type=int, default=50,
                        help='for bottom-up features up to 50 for images fixed 49')

    parser.add_argument('--no_warmup', dest='warmup', action='store_false',
                        help='optimizer starts with warm-up or not')
    parser.set_defaults(warmup=True)

    parser.add_argument('--warmup_epochs', type=int, default=4)

    parser.add_argument('--no_cnn_gradients', dest='keep_cnn_gradients', action='store_false',
                        help='train resnet or not when using cnn instead of bottom-up features')
    parser.set_defaults(keep_cnn_gradients=True)

    parser.add_argument('--just_store_cnn_features', dest='store_cnn_feat', action='store_true',
                        help='store cnn features')
    parser.set_defaults(store_cnn_feat=False)

    parser.add_argument('--features_dir', type=str, default='data',
                        help='path to stored cnn features')

    parser.add_argument('--calorie_range', nargs='+', type=int, default=[100, 1900],
                        help='Range of calorie for recipes.')

    parser.add_argument('--subsequent_calorie', dest='subsequent_calorie', action='store_true',
                        help='compute recipe calorie from predicted individual ingredient calories')
    parser.set_defaults(subsequent_calorie=False)

    parser.add_argument('--question_answer_path', type=str, default='/data/ajelodar/mcan-vqa-master/datasets/classes_dataset/',
                        help='path to question-answer json files')

    parser.add_argument('--calorie_dataset_path', type=str, default='/data/ajelodar/dish_classification/data/calorie_dataset',
                        help='path to calorie and dish dataset')

    parser.add_argument('--just_plot_error', dest='just_plot_error', action='store_true',
                        help='if set in sample.py just plots error without running the evaluation again')
    parser.set_defaults(just_plot_error=False)

    parser.add_argument('--use_distrib_loss', dest='use_distrib_loss', action='store_true',
                        help='use kl-divergence loss to make portion output of ingredients similar')
    parser.set_defaults(use_distrib_loss=False)

    parser.add_argument('--temp', default=4.0, type=float, help='temperature for kl loss scaling')

    parser.add_argument('--just_input_image', dest='just_input_image', action='store_true',
                        help='if using only image input')
    parser.set_defaults(just_input_image=False)

    parser.add_argument('--just_input_ingr', dest='just_input_ingr', action='store_true',
                        help='if using only ingredient input')
    parser.set_defaults(just_input_ingr=False)

    parser.add_argument('--remove_highest_errors', dest='remove_highest_errors', action='store_true',
                        help='if true removes N number of ingredients with highest error')
    parser.set_defaults(remove_highest_errors=False)

    parser.add_argument('--N_error', default=0, type=int, help='N number of ingredients')

    parser.add_argument('--pick_subset_ingrs', dest='pick_subset_ingrs', action='store_true',
                        help='if true removes only use the ingredients specified')
    parser.set_defaults(pick_subset_ingrs=False)

    parser.add_argument('--ingr_subset', nargs='+', type=str, default=["egg"],
                        help='the subset of ingredients specified to use for training.')

    parser.add_argument('--ingr_list_errors', type=str, default='data/ingr_list_errors.json',
                        help='path to a file which contains list of ingredients & their errors')

    parser.add_argument('--just_eval_subset_ingrs', action='store_true',
                        help='evaluate the model on a subset of the ingrdeints (not train time)')
    parser.set_defaults(just_eval_subset_ingrs=False)

    parser.add_argument('--pre_last_layer_size', default=128, type=int, help='pre last layer size')

    parser.add_argument('--portion_uses_ingr_only', action='store_true',
                        help='portion uses ingredient only or uses both ingr-image')
    parser.set_defaults(portion_uses_ingr_only=False)

    parser.add_argument('--do_calorie_scale', action='store_true',
                        help='calorie scaling is available or not')
    parser.set_defaults(do_calorie_scale=False)

    parser.add_argument('--do_total_calorie', action='store_true',
                        help='compute total calorie loss for simple decoder')
    parser.set_defaults(do_total_calorie=False)

    parser.add_argument('--do_prior_calorie', action='store_true',
                        help='compute prior mean calorie loss for indiv ingredient')
    parser.set_defaults(do_prior_calorie=False)

    parser.add_argument('--cnn_reduced', action='store_true',
                        help='CNN reduced')
    parser.set_defaults(cnn_reduced=False)

    parser.add_argument('--use_bert', action='store_true',
                        help='using bert embedding')
    parser.set_defaults(use_bert=False)

    parser.add_argument('--activation', type=str, default='relu', help='default activation function is relu')

    parser.add_argument('--dataset', type=str, default='recipe1m', help='dataset choice (recipe1m, menumatch)')

    parser.add_argument('--cross_valid_idx', default=0, type=int, help='cross validation iteration')

    parser.add_argument('--calorie_class_width', default=100, type=int, help='width for calorie classes')

    parser.add_argument('--model_output', type=str, default='all', choices=['all', 'just_calorie', 'just_ingr_calories', 'calories_and_calorie', 'portions_and_calorie'])

    parser.add_argument('--use_predicted', action='store_true',
                        help='using predicted ingredients or not')
    parser.set_defaults(use_predicted=False)

    parser.add_argument('--images_folder', type=str, default='/home/rpal/ahmad/personalized_unit_recognition/data/test_images',
                        help='path to a folder of images')

    parser.add_argument('--evaluate_given_images', action='store_true',
                        help='evaluate images in the test_images')
    parser.set_defaults(evaluate_given_images=False)

    args = parser.parse_args()

    return args


import datetime

from django.conf import settings
from django.http import HttpResponseForbidden
from django.shortcuts import render, redirect
from django.template import Template, Context
from django.utils.timezone import now

from spoilr.core.api.cache import memoized_cache
from spoilr.core.api.events import dispatch, HuntEvent
from spoilr.hints.models import HintSubmission
from spoilr.core.models import PuzzleAccess, HuntSetting

from hunt.app.core.assets.resolvers import create_round_url_resolver
from hunt.app.core.assets.rewriter import rewrite_relative_paths
from hunt.app.core.constants import ROUND_RD0_URL
from hunt.data_loader.round import get_round_data_text
from hunt.deploy.util import is_it_hunt, are_hunt_hints_released, is_hunt_complete

from .common import should_show_solutions, require_puzzle_access, get_puzzle_context, xframe_sameorigin_if_post

@require_puzzle_access(allow_rd0_access=False, require_access=True, skip_cache=True)
@xframe_sameorigin_if_post
def hints_view(request, embed):
    hints_unlocked = are_hints_available_for_puzzle(request.puzzle)
    time_left = None
    at_quota = False
    if hints_unlocked:
        time_left, at_quota = are_hints_available_for_team(request.team, request.puzzle, request.puzzle_access)

    hints_available = hints_unlocked and not time_left and not at_quota

    if request.method == 'POST':
        if not hints_available:
            return redirect(request.get_full_path())
        return hints_post_view(request, request.team, request.puzzle)

    round_url_resolver = create_round_url_resolver(request.puzzle.round.url, 'round')
    common_style = get_round_data_text(request.puzzle.round.url, 'round_common.css')

    hints = HintSubmission.objects.filter(team=request.team, puzzle=request.puzzle)
    context = get_puzzle_context(request.team, request.puzzle)
    context['hints'] = hints
    context['solved'] = request.puzzle_access.solved
    context['email'] = request.team.team_email
    context['hints_available'] = hints_available
    context['time_left'] = time_left
    context['at_quota'] = at_quota

    context_obj = Context(context)
    context['round_common_style'] = rewrite_relative_paths(
        Template(common_style).render(context_obj), round_url_resolver) if common_style else None

    return render(request, 'puzzle/hints_embed.tmpl' if embed else 'puzzle/hints.tmpl', context)

def hints_post_view(request, team, puzzle):
    assert not is_hunt_complete(), 'Can no longer submit hint requests'

    question = request.POST['question'].strip()
    email = request.POST['email']

    if len(question):
        hint, created = (HintSubmission.objects
            .update_or_create(team=team, puzzle=puzzle, question__iexact=question, defaults={
                'question': question,
                'email': email,
            }))
        if created:
            dispatch(
                HuntEvent.HINT_REQUESTED, team=team, puzzle=puzzle, hint=hint, object_id=puzzle.url,
                message=f'{team.name} asked “{question}” about {puzzle}')
    return redirect(request.get_full_path())

def are_hints_available_for_puzzle(puzzle):
    if not is_it_hunt():
        return False
    if puzzle.round.url == ROUND_RD0_URL:
        return False
    if are_hunt_hints_released() or puzzle.puzzledata.hints_force_released:
        return True
    return puzzle.puzzledata.hints_release_delay != None

def are_hints_available_for_team(team, puzzle, puzzle_access):
    at_quota = False
    time_left = None
    solved_puzzle_ids = PuzzleAccess.objects.filter(team=team, solved=True).values_list('puzzle_id', flat=True)
    open_hints_count = HintSubmission.objects.filter(team=team, resolved_time__isnull=True).exclude(puzzle__in=solved_puzzle_ids).count()
    quota = settings.HUNT_OPEN_HINTS_EXPANDED_QUOTA if are_hunt_hints_released() else settings.HUNT_OPEN_HINTS_QUOTA
    if open_hints_count >= quota:
        at_quota = True

    if not are_hunt_hints_released():
        available_time = now() - puzzle_access.timestamp
        if puzzle.puzzledata.hints_release_delay and puzzle.puzzledata.hints_release_delay > available_time:
            time_left = puzzle.puzzledata.hints_release_delay - available_time
    return time_left, at_quota


import asyncio
from pysnaffler.ruleset import SnafflerRuleSet
from pysnaffler.utils import sizeof_fmt
from typing import List
import toml

class pySnaffler:
	def __init__(self, ruleset:SnafflerRuleSet = None, max_file_size:int = 10485760, max_connections:int = 200,
					max_downloads:int = 4, max_downloads_total:int = 20, keep_files:bool = False,
					download_base_dir:str = './snaffler_downloads', dry_run:bool = False, gen_filelist:bool = False):
		self.ruleset = ruleset
		self.download_base_dir = download_base_dir
		self.max_file_size = max_file_size
		self.max_connections = max_connections
		self.max_downloads = max_downloads
		self.max_downloads_total = max_downloads_total
		self.keep_files = keep_files
		self.dry_run = dry_run
		self.gen_filelist = gen_filelist
		self.stat_fcnt = 0
		self.stat_fsize = 0
		self.stat_flarge = 0
		self.total_dl_semaphore = asyncio.Semaphore(self.max_downloads_total)

	def print_stats(self):
		if self.dry_run:
			print('Total files would\'ve been downloaded: %s Totaling %s Skipped %s files because of size constraints' % (self.stat_fcnt, sizeof_fmt(self.stat_fsize), self.stat_flarge))
			return

		print('Total files downloaded: %s Totaling %s Skipped %s files because of size constraints' % (self.stat_fcnt, sizeof_fmt(self.stat_fsize), self.stat_flarge))

	def to_dict(self):
		return {
			#'ruleset' : self.ruleset.to_dict(),
			'download_base_dir' : self.download_base_dir,
			'max_file_size' : self.max_file_size,
			'max_connections' : self.max_connections,
			'max_downloads' : self.max_downloads,
			'max_downloads_total' : self.max_downloads_total,
			'keep_files' : self.keep_files,
			'dry_run' : self.dry_run,
			'gen_filelist' : self.gen_filelist
		}

	def to_toml(self):
		# serialize all the settings to a toml file
		return toml.dumps(self.to_dict())

	@staticmethod
	def from_dict(d:dict):
		# deserialize from a dict
		if 'ruleset' not in d:
			d['ruleset'] = SnafflerRuleSet.load_default_ruleset()
		else:
			d['ruleset'] = SnafflerRuleSet.from_dict(d['ruleset'])
		return pySnaffler(
			d['ruleset'],
			d['download_base_dir'],
			d['max_file_size'],
			d['max_connections'],
			d['max_downloads'],
			d['max_downloads_total'],
			d['keep_files'],
			d['dry_run'],
			d['gen_filelist']
		)

	@staticmethod
	def from_toml(toml_str:str):
		# deserialize from a toml string
		d = toml.loads(toml_str)
		return pySnaffler.from_dict(d)

	@staticmethod
	def from_config_file(config_file:str):
		# deserialize from a toml file
		with open(config_file, 'r') as f:
			return pySnaffler.from_toml(f.read())


#!/usr/bin/env python3
# coding:utf-8
import argparse
import re
import os
from qiime2 import Artifact
from pandas import *
# import pdb
# argument:
p = argparse.ArgumentParser(description="This script is used to plot phylogentic tree of top abundant otus")
p.add_argument('-i', '--input', dest='otu', metavar='<path>', default=False,
               help='table.qza')
p.add_argument('-m', '--metadata', dest='map', metavar='<path>', default=False,
               help='Specify metadata with sample id at first column')
p.add_argument('-c', '--category', dest='group', metavar='<str>', default=False,
               help='Column name of group in mapping file, according to this group, the missing samples and zero features will be removed')
p.add_argument('-r', '--repseqs', dest='rep', metavar='<path>', default=False,
               help='representative seqs of features (.qza)')
p.add_argument('-t', '--taxonomy', dest='taxon', metavar='<path>', default=False,
               help='table.qza')
p.add_argument('-f', '--filter', dest='filter', metavar='<flag:taxon1,taxon2>', default=False,
               help='Taxon to be filtered. use "keep:taxon1,taxon2" to keep noly taxon1, taxon2. or use "exclude:taxon1,taxon2" to filter taxon1, taxon2.')
p.add_argument('--min-abundance', dest='mina', metavar='int', default=1,
               help='pass this to filter out the otu table and representative sequences less than the specified value')
p.add_argument('-o', '--out', dest='out', metavar='<directory>',
               default='./', help='Specify the output directory')
p.add_argument('--raw', metavar='<directory>',
               default=None, help='output csv tables before filter')
p.add_argument('--tsv', metavar='<path>',
               default=None, help='output otu table with taxonomy at last column')
p.add_argument('--consensus', metavar='<path>',
               default=None, help='output otu table with Consensus Lineage at last column')
p.add_argument('--even', metavar='<path>',
               default=None, help='output even otu table with taxonomy at last column')
p.add_argument('--evenconsensus', metavar='<path>',
               default=None, help='output even otu table with Consensus Lineage at last column')
p.add_argument('--fmap', metavar='<path>',
               default=None, help='output mapping file after filter')
p.add_argument('--odepth', metavar='<path>',
               default=None, help='Output depth used in even tabale calculation')
opt = p.parse_args()
if not os.path.exists(opt.out):
    os.makedirs(opt.out)
opt.out = opt.out + '/'

def iterin(a, b):
    return list(map(lambda x: x in list(b), a))

def iterfind(a, b):
    out = []
    for i in a:
        found = False
        for j in b:
            if re.search(j, i, flags=re.IGNORECASE):
                found = True
                break
        out.append(found)
    return out

def clean_tax(tax):
    for ambi in ['uncultured', 'Ambiguous', 'unidentified', 'unassigned', 'unclassified']:
        tax = re.sub(';[^;]*' + ambi + '.*', '', tax, flags=re.I)
    tax = re.sub('; ', ';', tax)
    tax = re.sub(r'[\-| ]+', '_', tax)
    tax = re.sub(';$', '', tax)
    return(tax)

def ch_col(df, index, val):
    columns = list(df.columns)
    columns[index] = val
    df.columns = columns
    return df

# load otu table from qiime2 Artifact
if opt.otu.endswith('.qza'):
    otu = Artifact.load(opt.otu)
    otu = otu.view(DataFrame)
    taxon = Artifact.load(opt.taxon)
    taxon = taxon.view(DataFrame)
# otherwise load otu table from tsv table
else:
    skip_rows = []
    with open(opt.otu) as f:
        for num, line in enumerate(f):
            if len(line.split('\t')) < 2:
                skip_rows.append(num)
    otu = read_csv(opt.otu, sep='\t', skiprows=skip_rows, index_col=0)
    otu_t = otu.iloc[:, -1]
    taxon = DataFrame({
        'Taxon': otu_t.copy(),
        'Confidence': [0.9] * otu_t.shape[0]
    }, index=otu_t.index.copy())
    taxon.index.set_names("Feature ID", inplace=True)
    otu.drop(otu.columns[-1], axis=1, inplace=True)
    otu = otu.T
    otu.columns.set_names(None, inplace=True)

if opt.raw:
    otu.T.to_csv(os.path.join(opt.raw, "otu_tale_before_filter.csv"))
    taxon.to_csv(os.path.join(opt.raw, "taxonomy_before_filter.csv"))

# load rep seqs from qiime2 Artifact
if opt.rep.endswith('.qza'):
    rep = Artifact.load(opt.rep)
# otherwise import rep seqs from fasta
else:
    rep = Artifact.import_data('FeatureData[Sequence]', opt.rep)
rep = rep.view(Series)

if opt.map and opt.group:
    # load mapping file
    mapd = read_csv(opt.map, sep='\t')
    # filter mapping file
    mapd = mapd.loc[mapd[opt.group].notna(), :]
    mapd = mapd.loc[:, mapd.notna().sum() > 1]
    mapd.to_csv(
        opt.fmap if opt.fmap else os.path.join(opt.out, 'mapping_file.txt'),
        sep='\t', index=False
    )
    # filter otu table
    otu = otu.loc[iterin(otu.index, mapd.iloc[:, 0]), :]

otu = otu.loc[:, otu.sum() >= int(opt.mina)]
otu_set1 = otu.columns

# pdb.set_trace()

# filter taxa
if opt.filter:
    flag, ftaxa = opt.filter.split(':')
    ftaxa = ftaxa.split(',')
    if flag == "keep":
        taxon = taxon.loc[iterfind(taxon.iloc[:, 0], ftaxa), :]
    else:
        taxon = taxon.loc[[not b for b in iterfind(taxon.iloc[:, 0], ftaxa)], :]

# clean taxa
taxon.iloc[:, 0] = [clean_tax(t) for t in taxon.iloc[:, 0]]

otu_set2 = taxon.index

# calculate inner set
inner_otuset = [o for o in otu_set1 if o in otu_set2]

# loc is much more faster
# otu = otu.iloc[:, iterin(otu.columns, inner_otuset)]
otu = otu.loc[:, inner_otuset]
# taxon = taxon.iloc[iterin(taxon.index, inner_otuset), :]
taxon = taxon.loc[inner_otuset, :]
# rep = rep.iloc[iterin(rep.index, inner_otuset)]
rep = rep.loc[inner_otuset]
# save data
if opt.tsv or opt.consensus:
    df = otu.T
    df = df.join(taxon)
    df.drop(df.columns[-1], axis=1, inplace=True)
    df.index.set_names("#OTU ID", inplace=True)
    df = ch_col(df, -1, "taxonomy")
    # print(df)
    df.to_csv(opt.tsv, sep='\t')
    if opt.consensus:
        df = ch_col(df, -1, "Consensus Lineage")
        df.to_csv(opt.consensus, sep='\t')

otu_qza = Artifact.import_data("FeatureTable[Frequency]", otu)
otu_qza.save(opt.out + 'table.qza')

if opt.even or opt.evenconsensus or opt.odepth:
    from qiime2.plugins import feature_table
    depth = int(otu.T.sum().min())
    # if depth < 5000:
    #     print("Min depth among samples is too small({}); reset sample depth to: 5000".format(depth))
    #     depth = 5000
    if opt.odepth:
        with open(opt.odepth, 'w') as f:
            f.write(str(depth))
    print("sample depth is: {}".format(depth))
    if opt.even or opt.evenconsensus:
        rarefy_result = feature_table.methods.rarefy(table=otu_qza, sampling_depth=depth)
        df = rarefy_result.rarefied_table.view(DataFrame).T
        df = df.join(taxon)
        df.drop(df.columns[-1], axis=1, inplace=True)
        df.index.set_names("#OTU ID", inplace=True)
        if opt.even:
            df = ch_col(df, -1, "taxonomy")
            df.to_csv(opt.even, sep='\t')
        if opt.evenconsensus:
            df = ch_col(df, -1, "Consensus Lineage")
            df.to_csv(opt.evenconsensus, sep='\t')

rep = Artifact.import_data("FeatureData[Sequence]", rep)
rep.save(opt.out + 'rep-seqs.qza')
taxon = Artifact.import_data("FeatureData[Taxonomy]", taxon)
taxon.save(opt.out + 'taxonomy.qza')


import sys
input = sys.stdin.readline

# N = int(input())
# M = int(input())
# S = input().strip()
# answer = 0

# # 기본 시작은 IOI,
# std = "IOI"
# for i in range(N - 1):  # N이 1씩 늘어날때마다 + OI를 붙여주면 됨
#     std += "OI"

# length = len(std)
# for j in range(M - length):
#     if std == S[j: j + length]:
#         answer += 1

# print(answer)

N = int(input())
M = int(input())
S = input().strip()

count = 0
pattern = 0 # 패턴의 길이
i = 0

while i < M - 1:
    if S[i: i+3] == "IOI":
        i += 2
        pattern += 1
        if pattern == N:
            count += 1
            pattern -= 1
    else:
        i += 1
        pattern = 0

print(count)

# 참고 사이트: https://black-hair.tistory.com/135


import math
import pyvista as pv
import numpy as np
from . import Singleton

class CameraStore(metaclass=Singleton):
    def __init__(self, window_size):
        self.window_size = window_size
        self.reset()

    def reset(self):
        self.camera = pv.Camera()
        self.fx = 10000
        self.fy = 10000
        self.cx = self.window_size[0] // 2
        self.cy = self.window_size[1] // 2
        self.cam_viewup = (0, 0, 0)

    def set_camera_extrinsics(self):
        self.camera.SetPosition((0, 0, -1e-8)) # Set the camera position at the origin of the world coordinate system
        self.camera.SetFocalPoint((*self.camera.GetWindowCenter(),0)) # Get the camera window center
        self.camera.SetViewUp(self.cam_viewup)

    def set_camera_intrinsics(self):
        # Set camera intrinsic attribute
        self.camera_intrinsics = np.array([
            [self.fx, 0, self.cx],
            [0, self.fy, self.cy],
            [0, 0, 1]
        ])

        # convert the principal point to window center (normalized coordinate system) and set it
        wcx = -2*(self.cx - float(self.window_size[0])/2) / self.window_size[0]
        wcy =  2*(self.cy - float(self.window_size[1])/2) / self.window_size[1]
        self.camera.SetWindowCenter(wcx, wcy) # (0,0)

        # Setting the view angle in degrees
        view_angle = (180 / math.pi) * (2.0 * math.atan2(self.window_size[1]/2.0, self.fx)) # or view_angle = np.degrees(2.0 * math.atan2(height/2.0, f)) # focal_length = (1080 / 2.0) / math.tan(math.radians(self.plotter.camera.view_angle / 2))
        self.camera.SetViewAngle(view_angle) # view angle should be in degrees


from flask import Flask, request, jsonify, render_template
import openai
import json
import yfinance as yf
import os
import datetime

openai.api_key = os.environ.get('OPENAI_API_KEY')

app = Flask(__name__)

def get_stockinfo(stock_code_all, start_date="2023-01-01", end_date=str(datetime.date.today())):
    if start_date > end_date:
        return None
    end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    end_date = end_date + datetime.timedelta(days=1) #结束日期延后一天
    end_date = end_date.strftime('%Y-%m-%d')

    stock_code_all = stock_code_all.split(",")
    stocklist = {}
    for stock_code in stock_code_all:
        stockinfo = {}
        if not stock_code.endswith('.HK'):
            stock_code = stock_code.strip().split('.')[0]
        if stock_code.startswith(('60', '688')):
            stock_code += '.SS'
        elif stock_code.startswith(('00', '002', '30')):
            stock_code += '.SZ'
        # 从Yahoo Finance获取股票数据
        ticker = yf.Ticker(stock_code)
        df = ticker.history(start=start_date, end=end_date)
        if len(df) == 0:
            return None
        stock_info = ticker.info
        #for key, value in stock_info.items():
        #    print(f"{key}: {value}")
        longname = stock_info["longName"]
        current_price = stock_info["currentPrice"]
        stockinfo[stock_code] = df['Close'].round(2).tolist()
        stocklist.update(stockinfo)
    print(stocklist)
    return stocklist

# Step 1, send model the user query and what functions it has access to
def run_conversation(question):
    current_date = datetime.date.today()
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=[{"role": "user", "content": question}],
        temperature=0,
        functions=[
            {
                "name": "get_stockinfo",
                "description": f"今天的日期是{current_date}，根据用户输入的stock code和起止时间，获取股票价格信息",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "stock_code": {
                            "type": "string",
                            "description": "all stock codes mentioned",
                        },
                        "start_date": {
                            "type": "string",
                            "description": "start date",
                        },
                        "end_date": {
                            "type": "string",
                            "description": "end date",
                        },
                    },
                    "required": ["stock_code"],
                },
            }
        ],
        function_call="auto",
    )

    print(f"{response['usage']}\n{response['choices'][0]['message']}")
    message = response['choices'][0]['message']
    arguments = json.loads(message["function_call"]["arguments"])

    # Step 2, check if the model wants to call a function
    if message.get("function_call"):
        function_name = message["function_call"]["name"]
        function = globals()[function_name]
        # Step 3, call the function
        # Note: the JSON response from the model may not be valid JSON
        function_response = function(*arguments.values())

        # Step 4, send model the info on the function call and function response
        second_response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=[
                {"role": "user", "content": question}, # 第一 user
                message, # 第二 assistant call什么函数，参数是什么
                {
                    "role": "function", # 第三 function
                    "name": function_name,
                    "content": "起止时间内的每日股票价格：" + str(function_response) + "\n不要评价起止时间之外的数据，不要列出所有股价，除非用户要求", # 函数返回
                },
            ],
        )
        print(f"{second_response['usage']}\n")
        return second_response["choices"][0]["message"]['content']

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/chat', methods=['POST'])
def chatbot():
    data = request.get_json()
    question = data['question']
    response = run_conversation(question)
    return jsonify({'response': response})

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


import re
import unittest
from datetime import datetime

from ...constants import *  # NOQA
from . import (
    ArchiverTestCaseBase,
    RemoteArchiverTestCaseBase,
    ArchiverTestCaseBinaryBase,
    RK_ENCRYPTION,
    src_dir,
    BORG_EXES,
)

class ArchiverTestCase(ArchiverTestCaseBase):
    def test_prune_repository(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        self.cmd(f"--repo={self.repository_location}", "create", "test1", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "test2", src_dir)
        # these are not really a checkpoints, but they look like some:
        self.cmd(f"--repo={self.repository_location}", "create", "test3.checkpoint", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "test3.checkpoint.1", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "test4.checkpoint", src_dir)
        output = self.cmd(f"--repo={self.repository_location}", "prune", "--list", "--dry-run", "--keep-daily=1")
        assert re.search(r"Would prune:\s+test1", output)
        # must keep the latest non-checkpoint archive:
        assert re.search(r"Keeping archive \(rule: daily #1\):\s+test2", output)
        # must keep the latest checkpoint archive:
        assert re.search(r"Keeping checkpoint archive:\s+test4.checkpoint", output)
        output = self.cmd(f"--repo={self.repository_location}", "rlist", "--consider-checkpoints")
        self.assert_in("test1", output)
        self.assert_in("test2", output)
        self.assert_in("test3.checkpoint", output)
        self.assert_in("test3.checkpoint.1", output)
        self.assert_in("test4.checkpoint", output)
        self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=1")
        output = self.cmd(f"--repo={self.repository_location}", "rlist", "--consider-checkpoints")
        self.assert_not_in("test1", output)
        # the latest non-checkpoint archive must be still there:
        self.assert_in("test2", output)
        # only the latest checkpoint archive must still be there:
        self.assert_not_in("test3.checkpoint", output)
        self.assert_not_in("test3.checkpoint.1", output)
        self.assert_in("test4.checkpoint", output)
        # now we supersede the latest checkpoint by a successful backup:
        self.cmd(f"--repo={self.repository_location}", "create", "test5", src_dir)
        self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=2")
        output = self.cmd(f"--repo={self.repository_location}", "rlist", "--consider-checkpoints")
        # all checkpoints should be gone now:
        self.assert_not_in("checkpoint", output)
        # the latest archive must be still there
        self.assert_in("test5", output)

    def _create_archive_ts(self, name, y, m, d, H=0, M=0, S=0):
        self.cmd(
            f"--repo={self.repository_location}",
            "create",
            "--timestamp",
            datetime(y, m, d, H, M, S, 0).strftime(ISO_FORMAT_NO_USECS),  # naive == local time / local tz
            name,
            src_dir,
        )

    # This test must match docs/misc/prune-example.txt
    def test_prune_repository_example(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        # Archives that will be kept, per the example
        # Oldest archive
        self._create_archive_ts("test01", 2015, 1, 1)
        # 6 monthly archives
        self._create_archive_ts("test02", 2015, 6, 30)
        self._create_archive_ts("test03", 2015, 7, 31)
        self._create_archive_ts("test04", 2015, 8, 31)
        self._create_archive_ts("test05", 2015, 9, 30)
        self._create_archive_ts("test06", 2015, 10, 31)
        self._create_archive_ts("test07", 2015, 11, 30)
        # 14 daily archives
        self._create_archive_ts("test08", 2015, 12, 17)
        self._create_archive_ts("test09", 2015, 12, 18)
        self._create_archive_ts("test10", 2015, 12, 20)
        self._create_archive_ts("test11", 2015, 12, 21)
        self._create_archive_ts("test12", 2015, 12, 22)
        self._create_archive_ts("test13", 2015, 12, 23)
        self._create_archive_ts("test14", 2015, 12, 24)
        self._create_archive_ts("test15", 2015, 12, 25)
        self._create_archive_ts("test16", 2015, 12, 26)
        self._create_archive_ts("test17", 2015, 12, 27)
        self._create_archive_ts("test18", 2015, 12, 28)
        self._create_archive_ts("test19", 2015, 12, 29)
        self._create_archive_ts("test20", 2015, 12, 30)
        self._create_archive_ts("test21", 2015, 12, 31)
        # Additional archives that would be pruned
        # The second backup of the year
        self._create_archive_ts("test22", 2015, 1, 2)
        # The next older monthly backup
        self._create_archive_ts("test23", 2015, 5, 31)
        # The next older daily backup
        self._create_archive_ts("test24", 2015, 12, 16)
        output = self.cmd(
            f"--repo={self.repository_location}",
            "prune",
            "--list",
            "--dry-run",
            "--keep-daily=14",
            "--keep-monthly=6",
            "--keep-yearly=1",
        )
        # Prune second backup of the year
        assert re.search(r"Would prune:\s+test22", output)
        # Prune next older monthly and daily backups
        assert re.search(r"Would prune:\s+test23", output)
        assert re.search(r"Would prune:\s+test24", output)
        # Must keep the other 21 backups
        # Yearly is kept as oldest archive
        assert re.search(r"Keeping archive \(rule: yearly\[oldest\] #1\):\s+test01", output)
        for i in range(1, 7):
            assert re.search(r"Keeping archive \(rule: monthly #" + str(i) + r"\):\s+test" + ("%02d" % (8 - i)), output)
        for i in range(1, 15):
            assert re.search(r"Keeping archive \(rule: daily #" + str(i) + r"\):\s+test" + ("%02d" % (22 - i)), output)
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        # Nothing pruned after dry run
        for i in range(1, 25):
            self.assert_in("test%02d" % i, output)
        self.cmd(
            f"--repo={self.repository_location}", "prune", "--keep-daily=14", "--keep-monthly=6", "--keep-yearly=1"
        )
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        # All matching backups plus oldest kept
        for i in range(1, 22):
            self.assert_in("test%02d" % i, output)
        # Other backups have been pruned
        for i in range(22, 25):
            self.assert_not_in("test%02d" % i, output)

    # With an initial and daily backup, prune daily until oldest is replaced by a monthly backup
    def test_prune_retain_and_expire_oldest(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        # Initial backup
        self._create_archive_ts("original_archive", 2020, 9, 1, 11, 15)
        # Archive and prune daily for 30 days
        for i in range(1, 31):
            self._create_archive_ts("september%02d" % i, 2020, 9, i, 12)
            self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=7", "--keep-monthly=1")
        # Archive and prune 6 days into the next month
        for i in range(1, 7):
            self._create_archive_ts("october%02d" % i, 2020, 10, i, 12)
            self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=7", "--keep-monthly=1")
        # Oldest backup is still retained
        output = self.cmd(
            f"--repo={self.repository_location}", "prune", "--list", "--dry-run", "--keep-daily=7", "--keep-monthly=1"
        )
        assert re.search(r"Keeping archive \(rule: monthly\[oldest\] #1" + r"\):\s+original_archive", output)
        # Archive one more day and prune.
        self._create_archive_ts("october07", 2020, 10, 7, 12)
        self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=7", "--keep-monthly=1")
        # Last day of previous month is retained as monthly, and oldest is expired.
        output = self.cmd(
            f"--repo={self.repository_location}", "prune", "--list", "--dry-run", "--keep-daily=7", "--keep-monthly=1"
        )
        assert re.search(r"Keeping archive \(rule: monthly #1\):\s+september30", output)
        self.assert_not_in("original_archive", output)

    def test_prune_repository_save_space(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        self.cmd(f"--repo={self.repository_location}", "create", "test1", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "test2", src_dir)
        output = self.cmd(f"--repo={self.repository_location}", "prune", "--list", "--dry-run", "--keep-daily=1")
        assert re.search(r"Keeping archive \(rule: daily #1\):\s+test2", output)
        assert re.search(r"Would prune:\s+test1", output)
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_in("test1", output)
        self.assert_in("test2", output)
        self.cmd(f"--repo={self.repository_location}", "prune", "--save-space", "--keep-daily=1")
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_not_in("test1", output)
        self.assert_in("test2", output)

    def test_prune_repository_prefix(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        self.cmd(f"--repo={self.repository_location}", "create", "foo-2015-08-12-10:00", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "foo-2015-08-12-20:00", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "bar-2015-08-12-10:00", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "bar-2015-08-12-20:00", src_dir)
        output = self.cmd(
            f"--repo={self.repository_location}",
            "prune",
            "--list",
            "--dry-run",
            "--keep-daily=1",
            "--match-archives=sh:foo-*",
        )
        assert re.search(r"Keeping archive \(rule: daily #1\):\s+foo-2015-08-12-20:00", output)
        assert re.search(r"Would prune:\s+foo-2015-08-12-10:00", output)
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_in("foo-2015-08-12-10:00", output)
        self.assert_in("foo-2015-08-12-20:00", output)
        self.assert_in("bar-2015-08-12-10:00", output)
        self.assert_in("bar-2015-08-12-20:00", output)
        self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=1", "--match-archives=sh:foo-*")
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_not_in("foo-2015-08-12-10:00", output)
        self.assert_in("foo-2015-08-12-20:00", output)
        self.assert_in("bar-2015-08-12-10:00", output)
        self.assert_in("bar-2015-08-12-20:00", output)

    def test_prune_repository_glob(self):
        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
        self.cmd(f"--repo={self.repository_location}", "create", "2015-08-12-10:00-foo", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "2015-08-12-20:00-foo", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "2015-08-12-10:00-bar", src_dir)
        self.cmd(f"--repo={self.repository_location}", "create", "2015-08-12-20:00-bar", src_dir)
        output = self.cmd(
            f"--repo={self.repository_location}",
            "prune",
            "--list",
            "--dry-run",
            "--keep-daily=1",
            "--match-archives=sh:2015-*-foo",
        )
        assert re.search(r"Keeping archive \(rule: daily #1\):\s+2015-08-12-20:00-foo", output)
        assert re.search(r"Would prune:\s+2015-08-12-10:00-foo", output)
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_in("2015-08-12-10:00-foo", output)
        self.assert_in("2015-08-12-20:00-foo", output)
        self.assert_in("2015-08-12-10:00-bar", output)
        self.assert_in("2015-08-12-20:00-bar", output)
        self.cmd(f"--repo={self.repository_location}", "prune", "--keep-daily=1", "--match-archives=sh:2015-*-foo")
        output = self.cmd(f"--repo={self.repository_location}", "rlist")
        self.assert_not_in("2015-08-12-10:00-foo", output)
        self.assert_in("2015-08-12-20:00-foo", output)
        self.assert_in("2015-08-12-10:00-bar", output)
        self.assert_in("2015-08-12-20:00-bar", output)

class RemoteArchiverTestCase(RemoteArchiverTestCaseBase, ArchiverTestCase):
    """run the same tests, but with a remote repository"""

@unittest.skipUnless("binary" in BORG_EXES, "no borg.exe available")
class ArchiverTestCaseBinary(ArchiverTestCaseBinaryBase, ArchiverTestCase):
    """runs the same tests, but via the borg binary"""


import clipboard

with open('original/OR.bin', 'rb') as f:
    data = f.read()

comps = []
addresses = []

offset = 6*0x4000+0x2483
for i in range(86):
    word = data[offset+i*2] + (data[offset+1+i*2]<<8)
    addresses.append(word-0x8000)
    comps.append(f"\t.dw scriptsData_{i:02x}")

addresses.append(0x2647)
comps.append("")

for i, address in enumerate(addresses[:-1]):
    nextAddress = addresses[i+1]
    scriptBytes = data[6*0x4000+address:6*0x4000+nextAddress]
    comps.append(f"scriptsData_{i:02x}:")
    if not scriptBytes:
        continue

    countByte = scriptBytes[0]
    comps.append(f"\t.db ${countByte:02x}")
    for i in range(countByte):
        comps.append(f"\t.db ${scriptBytes[i*2+1]:02x} ${scriptBytes[i*2+2]:02x}")
    comps.append(f"\t.db ${scriptBytes[-1]:02x}")
    comps.append(f"")

final_str = "\n".join(comps)
print(final_str)
clipboard.copy(final_str)


# import the necessary packages
from imutils import face_utils
import numpy as np
import argparse
import imutils
import dlib
import cv2
import math
from numpy.linalg import norm
import sys
import copy
import os

def start(swap, image, video):
    # construct the argument parser and parse the arguments
    # ap = argparse.ArgumentParser()
    # ap.add_argument("-p", "--shape-predictor", required=True,
    # 	help="path to facial landmark predictor")
    # ap.add_argument("-i", "--image", required=True,
    # 	help="path to input image")
    # args = vars(ap.parse_args())

    # initialize dlib's face detector (HOG-based) and then create
    # the facial landmark predictor
    cam = cv2.VideoCapture(video)
    width = int(cam.get(3))  # float
    height = int(cam.get(4))
    print(width, height)
    vidWriter = cv2.VideoWriter("./video_output_data1.mp4",cv2.VideoWriter_fourcc(*'mp4v'), int(cam.get(5)), (width, height))
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
    # swap = True
    face_swap_file = image
    # load the input image, resize it, and convert it to grayscale
    # images = ['Scarlett.jpg', 'Rambo.jpg']
    # imageA = cv2.imread('Scarlett.jpg')

            # cv2.circle(imageB, (x, y), 1, (0, 0, 255), -1)
    currentframe = 0
    while (True):
        # reading from frame
        ret, frame = cam.read()
        if ret:
            imageA = frame
            grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
            rectsA = detector(grayA, 1)
            if (swap and len(rectsA) < 2) or (not swap and len(rectsA) == 0):
                vidWriter.write(frame)
                print("Can't find enough faces in frame")
                continue
            PA = []
            xPointsA = []
            yPointsA = []
            shapeA = None
            # for (i, rect) in enumerate(rectsA):
                # determine the facial landmarks for the face region, then
                # convert the facial landmark (x, y)-coordinates to a NumPy
                # array
            if swap:
                shapeA = predictor(grayA, rectsA[0])
            else:
                shapeA = predictor(grayA, rectsA[0])
            shapeA = face_utils.shape_to_np(shapeA)
            print(len(shapeA))
            for (x, y) in shapeA:
                PA.append([x, y, 1])
                xPointsA.append(x)
                yPointsA.append(y)
                # convert dlib's rectangle to a OpenCV-style bounding box
                # [i.e., (x, y, w, h)], then draw the face bounding box
                # (x, y, w, h) = face_utils.rect_to_bb(rect)
                # cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 255, 0), 2)
                # # show the face number
                # cv2.putText(imageA, "Face #{}".format(i + 1), (x - 10, y - 10),
                # 	cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                # loop over the (x, y)-coordinates for the facial landmarks
                # and draw them on the image
                # for (x, y) in shapeA:
                # cv2.circle(imageA, (x, y), 1, (0, 0, 255), -1)
            if swap:
                imageB = frame
            else:
                imageB = cv2.imread(face_swap_file)

            # detect faces in the grayscale image

            grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
            # detect faces in the grayscale image
            # rectsB = detector(grayB, 1)

            PB = []
            shapeB = None
            xPointsB = []
            yPointsB = []

            # loop over the face detections
            # for (i, rect) in enumerate(rectsB):
                # determine the facial landmarks for the face region, then
                # convert the facial landmark (x, y)-coordinates to a NumPy
                # array
            if swap:
                shapeB = predictor(grayB, rectsA[1])
            else:
                rectsB = detector(grayB, 1)
                shapeB = predictor(grayB, rectsB[0])
            shapeB = face_utils.shape_to_np(shapeB)
            # convert dlib's rectangle to a OpenCV-style bounding box
            # [i.e., (x, y, w, h)], then draw the face bounding box
            # (x, y, w, h) = face_utils.rect_to_bb(rect)
            # cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 255, 0), 2)
            # # show the face number
            # cv2.putText(imageB, "Face #{}".format(i + 1), (x - 10, y - 10),
            # 	cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            # loop over the (x, y)-coordinates for the facial landmarks
            # and draw them on the image
            for (x, y) in shapeB:
                PB.append([x, y, 1])
                xPointsB.append(x)
                yPointsB.append(y)
            weights_x, weights_y = find_weights(PA, shapeA, xPointsB, yPointsB)
            # print(weights)
            mask_warped_img, warped_img = warp_face(imageA, imageB, shapeA, shapeB, weights_x, weights_y)
            r = cv2.boundingRect(mask_warped_img)
            center = ((r[0] + int(r[2] / 2), r[1] + int(r[3] / 2)))
            output = cv2.seamlessClone(warped_img.copy(), imageA, mask_warped_img, center, cv2.NORMAL_CLONE)
            if swap:
                weights_x, weights_y = find_weights(PB, shapeB, xPointsA, yPointsA)
                # print(weights)
                mask_warped_img, warped_img = warp_face(imageB, imageA, shapeB, shapeA, weights_x, weights_y)
                r = cv2.boundingRect(mask_warped_img)
                center = ((r[0] + int(r[2] / 2), r[1] + int(r[3] / 2)))
                output = cv2.seamlessClone(warped_img.copy(), output, mask_warped_img, center, cv2.NORMAL_CLONE)
                # imageA[rects[0].top() - 40:rects[0].bottom() + 40, rects[0].left() - 40:rects[0].right() + 40, :] = output
                # cv2.imshow("warped_img", warped_img)
                # cv2.imshow("mask_warped", mask_warped_img)
                # cv2.imshow("mask", mask)
            vidWriter.write(output)
            cv2.imwrite('output_data1_' + str(currentframe) + '.jpg', output)
            # cv2.waitKey(0)

            # increasing counter so that it will
            # show how many frames are created
            currentframe += 1
        else:
            break
    vidWriter.release()
    # Release all space and windows once done
    cam.release()
    cv2.destroyAllWindows()
    # loop over the face detections

    # a1 = weights[-1]
    # ay = weights[-2]
    # ax = weights[-3]
    # weights = weights[:len(weights)-3]
    # warpedA = []
    # for i in range(grayB.shape[0]):
    # 	for j in range(grayB.shape[1]):
    # 		sum = 0
    # 		for ind, weight in enumerate(weights):
    # 			# print(shapeB[ind])
    # 			r = norm(shapeB[ind] - [i, j], 1) + np.exp(10 ** -7)
    # 			u = (r ** 2) * (math.log(r ** 2))
    # 			sum = sum + weight*u
    # 		warpedA.append(a1 + ax*i + ay*j + sum)
    #
    # # print(warpedA)
    #
    # new = np.zeros(grayB.shape)
    #
    # warpedA = np.reshape(warpedA, (333,500,2))
    # print(warpedA.shape)
    # print(new.shape)
    #
    # for i in range(new.shape[0]):
    # 	for j in range(new.shape[1]):
    # 		if 0 <= int(warpedA[i][j][1]) < grayA.shape[1] and 0 <= int(warpedA[i][j][0]) < grayA.shape[0]:
    # 			print(warpedA[i][j])
    # 			new[i][j] = grayA[int(warpedA[i][j][0])][int(warpedA[i][j][1])]

    # show the output image with the face detections + facial landmarks
    # cv2.imshow("OutputA", imageA)

def warp_face(imageA, imageB, shapeA, shapeB, weights_x, weights_y):
    w, h = imageB.shape[:2]
    mask = mask_from_points((w, h), shapeB)
    # def warp_tps(img_source,img_target,points1,points2,weights_x,weights_y,mask):
    xy1_min = np.float32([min(shapeA[:, 0]), min(shapeA[:, 1])])
    xy1_max = np.float32([max(shapeA[:, 0]), max(shapeA[:, 1])])
    xy2_min = np.float32([min(shapeB[:, 0]), min(shapeB[:, 1])])
    xy2_max = np.float32([max(shapeB[:, 0]), max(shapeB[:, 1])])
    x = np.arange(xy1_min[0], xy1_max[0]).astype(int)
    y = np.arange(xy1_min[1], xy1_max[1]).astype(int)
    X, Y = np.mgrid[x[0]:x[-1] + 1, y[0]:y[-1] + 1]
    # X,Y = np.mgrid[0:src_shape[2],0:src_shape[3]]
    pts_src = np.vstack((X.ravel(), Y.ravel()))
    xy = pts_src.T
    u = np.zeros_like(xy[:, 0])
    v = np.zeros_like(xy[:, 0])
    # print(u.shape)
    # print(v.shape)
    for i in range(xy.shape[0]):
        u[i] = fxy(xy[i, :], shapeA, weights_x)
    u[u < xy2_min[0]] = xy2_min[0]
    u[u > xy2_max[0]] = xy2_max[0]
    for j in range(xy.shape[0]):
        v[j] = fxy(xy[j, :], shapeA, weights_y)
    v[v < xy2_min[1]] = xy2_min[1]
    v[v > xy2_max[1]] = xy2_max[1]
    #     print(u.shape)
    #     print(img_source.shape)
    warped_img = imageA.copy()
    mask_warped_img = np.zeros_like(warped_img[:, :, 0])
    for a in range(u.shape[0]):
        #     for b in range(v.shape[0]):
        #     warped_img[xy[a,1],xy[a,0],:] = warped_src_face[v[a],u[a],:]
        if mask[v[a], u[a]] > 0:
            warped_img[xy[a, 1], xy[a, 0], :] = imageB[v[a], u[a], :]
            mask_warped_img[xy[a, 1], xy[a, 0]] = 255
        # plt.imshow(warped_img)
        # plt.show()
        # return warped_img, mask_warped_img
    return mask_warped_img, warped_img

def find_weights(PA, shapeA, xPointsB, yPointsB):
    K = np.zeros([len(shapeA), len(shapeA)])
    for i in range(len(shapeA)):
        for j in range(len(shapeA)):
            r = norm(shapeA[i] - shapeA[j], 2) + np.exp(10 ** -7)
            K[i][j] = (r ** 2) * (math.log(r ** 2))
    mat = np.vstack([np.hstack((K, PA)), np.hstack([np.transpose(PA), np.zeros([3, 3])])])
    lam = np.exp(10 ** -7)
    V = []
    for v in xPointsB:
        V.append(v)
    V.append(0)
    V.append(0)
    V.append(0)
    # print('K', K)
    weights_x = np.matmul(np.linalg.inv(mat + lam * np.identity(len(shapeA) + 3)), V)
    V = []
    for v in yPointsB:
        V.append(v)
    V.append(0)
    V.append(0)
    V.append(0)
    weights_y = np.matmul(np.linalg.inv(mat + lam * np.identity(len(shapeA) + 3)), V)
    return weights_x, weights_y

def U(r):
    return (r**2)*(math.log(r**2))

def fxy(pt1,pts2,weights):
    K = np.zeros([pts2.shape[0],1])
    for i in range(pts2.shape[0]):
        K[i] = U(np.linalg.norm((pts2[i]-pt1),2) + np.exp(10**-7))
    f = weights[-1] + weights[-3]*pt1[0] +weights[-2]*pt1[1]+np.matmul(K.T,weights[0:-3])
    return f

def mask_from_points(size, points,erode_flag=1):
    radius = 10  # kernel size
    kernel = np.ones((radius, radius), np.uint8)

    mask = np.zeros(size, np.uint8)
    cv2.fillConvexPoly(mask, cv2.convexHull(points), 255)
    if erode_flag:
        mask = cv2.erode(mask, kernel,iterations=1)

    return mask

if __name__ == '__main__':
    start()


# https://adventofcode.com/2020/day/19
import re

# rules, messages = open("day_19_input_sample1.txt", "r").read().split("\n\n")
rules, messages = open("day_19_input.txt", "r").read().split("\n\n")

rules = {int(r.split(': ')[0]): r.split(': ')[1].split(" ") for r in rules.split("\n")}

patterns = {}

def find_pattern(index):
    pattern = ""
    for item in rules[index]:
        match = re.match(r"\"(\w+)\"", item)
        if match:
            pattern += match[1]
            continue
        if item.isdigit():
            item = int(item)
            if item in patterns:
                # Already computed
                pattern += patterns[item]
            else:
                pattern += find_pattern(item)
            continue
        if item == '|':
            pattern += '|'
            continue

    patterns[index] = f"(?:{pattern})"
    return patterns[index]

pattern = "^" + find_pattern(0) + "$"
total = sum(1 for m in messages.split("\n") if re.match(pattern, m))
print(f"Part One: {total}")


import random
import sys

flag=True
n=True
ro=1
Player_points=0
System_points=0
while(n):
    print("Let's Start Round: ","%d"%+ro)
    Player=input("Enter your choice(rock,paper,scissors,enough): ")   #player selection option
    Options_possible=["rock","paper","scissors"]   #list of possible options for system
    System=random.choice(Options_possible)
    print(f"\nYou chose {Player}, system chose {System}.\n")
    if(Player == System):
        print("Round Tie No points!!")
        ro+=1
    elif(Player == "rock"):
        if(System == "scissors"):
            print("Hurry!! You won...:) in round - ")
            Player_points+=1
            ro+=1
        else:
            print("Oops!! You lose...:( in round - ")
            System_points+=1
            ro+=1
    elif(Player == "paper"):
        if(System == "rock"):
            print("Hurry!! You won...:) in round - ")
            Player_points+=1
            ro+=1
        else:
            print("Oops!! You lose...:( in round - ")
            System_points+=1
    elif(Player == "scissors"):
        if(System == "paper"):
            print("Hurry!! You won...:) in round - ")
            Player_points+=1
            ro+=1
        else:
            print("Oops!! You lose...:( in round - ")
            System_points+=1
            ro+=1
    elif(Player == "enough"):
        flag=False
        break
    else:
        print("Enter proper choice: ")

print("Total points after the tournament is")
print(f"Your points are {Player_points}")
print(f"system points are {System_points}")
if(Player_points>System_points):
    print("Congratulations!! you won the tournament...:)")
elif(Player_points<System_points):
    print("Better Luck Next Time...:(")
else:
    print("Tournament Draw...!!")

 #actually not required
if(flag!=True):
    sys.exit()


#-*- coding=utf8 -*-
'''
Created on 2015.12.11

@author: albertcheng
'''
import os
import sys
import re
import logging
# config logging
fmt = '%(asctime)s - %(lineno)s - %(levelname)s - %(message)s'
logging.basicConfig(stream = sys.stdout ,level=logging.DEBUG,
                    format= fmt,
                    filename='res_tool.log',
                    filemode='w')
# define a handler
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# 设置日志打印格式
formatter = logging.Formatter(fmt)
console.setFormatter(formatter)
# 将定义好的console日志handler添加到root logger
logger = logging.getLogger('')
logger.addHandler(console)

def ifFileExists(func):
    """
    # 修饰函数，用于统一检查待读取文件是否存在
    """
    def __deco(*argu):
        bfile  = argu[0] + ".bin" # 构造bin文件路径
        csfile = argu[0] + ".cs" # 构造cs文件路径
        if not os.path.exists(bfile):
            logger.error(u"文件  [%s] 不存在" % bfile)
            assert os.path.exists(bfile)
        if not os.path.exists(csfile):
            logger.error(u"文件  [%s] 不存在" % csfile)
            assert os.path.exists(csfile)
        return func(*argu)
    return __deco

def loadBin(func):
    """
    # 修饰函数，用于统一文件读取的默认操作
    """

    def __deco(*argu):
        bfile = argu[0] + ".bin" # 构造bin文件路径
        csfile = argu[0] + ".cs" # 构造cs文件路径
        with open(bfile, "rb") as Bin_File:
            argu = list(argu)
            argu.append([[y for y in x.split("\t") if y != "\r\n"] for x in Bin_File.readlines()])

        with open(csfile, "rb") as csharp_File:
            p = re.compile("[\";]")
            argu.append([p.sub("", line.strip().split(" ")[-1]) for line in csharp_File.readlines() if line.strip().startswith("public") and line.strip().endswith(";")  and "=" not in line])

        return func(*argu)
    return __deco

@ifFileExists
@loadBin
def LoadSingleKey(*argu):
    key, bindata, tableData = argu[1:]
    ret = {}
    key_idx = tableData.index(key)
    for data in bindata:
        # 断言检查角色ID的配置唯一性
        assert data[key_idx] not in ret
        ret[data[key_idx]] = data
    ret["__table"] = tableData
    return ret

@ifFileExists
@loadBin
def LoadDoubleKey(*argu):
    key1, key2, bindata, tableData= argu[1:]
    key1_idx = tableData.index(key1)
    key2_idx = tableData.index(key2)
    ret = {}
    for data in bindata:
        key1ID = data[key1_idx]
        key2ID = data[key2_idx]
        if key1ID not in ret:
            ret[key1ID] = {}
        assert key2ID not in ret[key1ID]
        ret[key1ID][key2ID] = data
    ret["__table"] = tableData
    return ret


#%% Comprehension with array
data = range(100)##Array from 0 to 99
pares = [numero for numero in data if numero % 2 == 0 ]
pares

#%% Comprehension con diccionarios
stundent_uid = [1,2,3]
students = ['Luis','Jose','Rogelio']
students_with_uid = {uid: student for uid,
                     student in zip(stundent_uid, students)}
students_with_uid

#%% Set comprehension
import random
random_numbers = []
for i in range(10):
  random_numbers.append(random.randint(1,3))
print(random_numbers)

not_repeated = {number for number in random_numbers} #esto es un set ya que un set se puede inicializar tambien con las llaves

print(not_repeated)