import configparser
import logging
import codecs
import sys

# Load ini content
def iniLoad(_file,_group,_item,_default):

    # Warning about loading data
    logging.warning('File Loading '+'../files_conf/'+_file)

    config = configparser.ConfigParser()
    config.sections()
    # UTF-16 for emoji support
    config.read('../files_conf/'+_file, encoding="utf-16")

    # If ini group not exist, create one
    if config.has_section(_group) == False:
        config.add_section(_group)
        return _default

    # If item not exist create one
    if config.has_option(_group, _item) == True:
        _return=config.get(_group, _item)
    else:
        config.set(_group,_item, _default)

    # Return result
    return _return

# Change ini content
def iniChange(_file,_group,_item,_value):

    # Warning about saving data
    logging.warning('File Saving '+'../files_conf/'+_file)

    config = configparser.ConfigParser()
    config.sections()
    # UTF-16 for emoji support
    config.readfp(codecs.open('../files_conf/'+_file, "r", "utf16"))

    # If ini group not exist, create one
    if config.has_section(_group) == False:
        config.add_section(_group)
    config.set(_group,_item, _value)

    # Merge the content to a file
    with open('../files_conf/'+_file, 'w',encoding="utf-16") as configfile:
        config.write(configfile)


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

from pybacklog import BacklogClient
from utils.Project import Project
from utils.Summary import Summary
from utils.Detail import Detail

class AppManager(object):
    ''' このアプリケーションのクラス
    '''
    def __init__(self, config, logger):
        self.client = BacklogClient(config['WIKI_SPACE'], config['API_KEY']['GLOBAL'])
        self.projects = []
        self.logger = logger
        for projectKey in config['PROCESSING_PROJECT_KEY']:
            self.projects.append(Project(projectKey, self.client, logger))

    def collectIssues(self, issueTypeName, beginDate, endDate, maxCount):
        for project in self.projects:
            project.collectIssues(issueTypeName, beginDate, endDate, maxCount)

    def reportSummary(self, wikiId, period, maxComments, isUpdateWiki = False):
        summary = Summary(wikiId, self.client, self.logger)
        record = {}
        for project in self.projects:
            rec = project.getSummaryRecord(maxComments)
            record[rec[1]] = (rec[2], rec[0])
        summary.addRecord(period, record).printRecords(isUpdateWiki)

    def reportDetail(self, wikiId, period, maxComments, isUpdateWiki = False):
        detail = Detail(wikiId, self.client, self.logger)
        projects = {}
        for project in self.projects:
            records = project.getDetailRecords(maxComments)
            label = f'{records[0]} ({records[1]})'
            projects[label] = records[2]
        detail.addSection(period, projects).printSections(isUpdateWiki)

if __name__ == '__main__':
    pass


from tensorflow.python.keras.utils.vis_utils import plot_model
from tensorflow.keras.applications import ResNet50V2
import tensorflow as tf
import numpy as np
import json
import matplotlib.pylab as plt

import tensorflow_datasets as tfds

# EuroSAT 위성사진 데이터셋 로드 (27,000장)
# https://www.kaggle.com/ryanholbrook/eurosat/metadata
DATA_DIR = "dataset/"
(train_ds, valid_ds), info = tfds.load('eurosat/rgb', split=['train[:80%]', 'train[80%:]'],
                                       shuffle_files=True,
                                       as_supervised=True,
                                       with_info=True,
                                       data_dir=DATA_DIR)

tfds.show_examples(train_ds, info)
tfds.as_dataframe(valid_ds.take(10), info)

# 목표 클래스의 개수
NUM_CLASSES = info.features["label"].num_classes
print(NUM_CLASSES)

# 숫자 레이블을 활용하여 문자열 메타데이터로 변환
print(info.features["label"].int2str(6))

# 데이터 전처리 파이프라인
BATCH_SIZE = 64
BUFFER_SIZE = 1000

def preprocess_data(image, label):
    image = tf.cast(image, tf.float32) / 255.   # 0~1 정규화, float32 변환
    return image, label

train_data = train_ds.map(preprocess_data, num_parallel_calls=tf.data.AUTOTUNE)
valid_data = valid_ds.map(preprocess_data, num_parallel_calls=tf.data.AUTOTUNE)

train_data = train_data.shuffle(BUFFER_SIZE).batch(
    BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
valid_data = valid_data.batch(BATCH_SIZE).cache().prefetch(tf.data.AUTOTUNE)

# Sequential API를 사용하여 샘플 모델 생성

def build_model():

    model = tf.keras.Sequential([

        # Convolution 층
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D((2, 2)),

        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D((2, 2)),

        # Classifier 출력층
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dropout(0.3),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dropout(0.3),
        tf.keras.layers.Dense(NUM_CLASSES, activation='softmax'),
    ])

    return model

model = build_model()

# 모델 컴파일
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# 모델 훈련
history = model.fit(train_data,
                    validation_data=valid_data,
                    epochs=5)

# 손실함수, 정확도 그래프 그리기

def plot_loss_acc(history, epoch):

    loss, val_loss = history.history['loss'], history.history['val_loss']
    acc, val_acc = history.history['accuracy'], history.history['val_accuracy']

    fig, axes = plt.subplots(1, 2, figsize=(12, 4))

    axes[0].plot(range(1, epoch + 1), loss, label='Training')
    axes[0].plot(range(1, epoch + 1), val_loss, label='Validation')
    axes[0].legend(loc='best')
    axes[0].set_title('Loss')

    axes[1].plot(range(1, epoch + 1), acc, label='Training')
    axes[1].plot(range(1, epoch + 1), val_acc, label='Validation')
    axes[1].legend(loc='best')
    axes[1].set_title('Accuracy')

    plt.show()

plot_loss_acc(history, 50)

# 샘플 이미지
image_batch, label_batch = next(iter(train_data.take(1)))

image = image_batch[0]
label = label_batch[0].numpy()

plt.imshow(image)
plt.title(info.features["label"].int2str(label))

# 데이터 증강 전후를 비교하는 시각화 함수를 정의

def plot_augmentation(original, augmented):

    fig, axes = plt.subplots(1, 2, figsize=(12, 4))

    axes[0].imshow(original)
    axes[0].set_title('Original')

    axes[1].imshow(augmented)
    axes[1].set_title('Augmented')

    plt.show()

# 좌우 뒤집기
lr_flip = tf.image.flip_left_right(image)
plot_augmentation(image, lr_flip)

# 상하 뒤집기
ud_flip = tf.image.flip_up_down(image)
plot_augmentation(image, ud_flip)

# 회전
rotate90 = tf.image.rot90(image)
plot_augmentation(image, rotate90)

# transpose
transpose = tf.image.transpose(image)
plot_augmentation(image, transpose)

# 이미지 자르기 1
crop1 = tf.image.central_crop(image, central_fraction=0.6)
plot_augmentation(image, crop1)

# 이미지 자르기 2
img = tf.image.resize_with_crop_or_pad(image, 64 + 20, 64 + 20)  # 사이즈 키우기
crop2 = tf.image.random_crop(img, size=[64, 64, 3])
plot_augmentation(image, crop2)

# 이미지 밝기
brightness = tf.image.adjust_brightness(image, delta=0.3)
plot_augmentation(image, brightness)

# 이미지 채도
saturation = tf.image.adjust_saturation(image, saturation_factor=0.5)
plot_augmentation(image, saturation)

# 이미지 대비
contrast = tf.image.adjust_contrast(image, contrast_factor=2)
plot_augmentation(image, contrast)

# 이미지 증강 전처리

def data_augmentation(image, label):

    image = tf.image.random_flip_left_right(image)  # 좌우 반전
    image = tf.image.random_flip_up_down(image)  # 상하 반전
    image = tf.image.random_brightness(image, max_delta=0.3)  # 밝기 변화
    image = tf.image.random_crop(image, size=[64, 64, 3])

    image = tf.cast(image, tf.float32) / 255.   # 0~1 정규화

    return image, label

train_aug = train_ds.map(
    data_augmentation, num_parallel_calls=tf.data.AUTOTUNE)
valid_aug = valid_ds.map(
    data_augmentation, num_parallel_calls=tf.data.AUTOTUNE)

train_aug = train_aug.shuffle(BUFFER_SIZE).batch(
    BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
valid_aug = valid_aug.batch(BATCH_SIZE).cache().prefetch(tf.data.AUTOTUNE)

print(train_aug)
print(valid_aug)

# 모델 생성
aug_model = build_model()

# 모델 컴파일
aug_model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# 모델 훈련
aug_history = aug_model.fit(train_aug,
                            validation_data=valid_aug,
                            epochs=50)

# 손실함수, 정확도 그래프 그리기
plot_loss_acc(aug_history, 50)

# Pre-trained 모델을 사전 학습된 가중치와 함께 가져오기

pre_trained_base = ResNet50V2(include_top=False,
                              weights='imagenet',
                              input_shape=[64, 64, 3])

# 사전 학습된 가중치를 업데이트 되지 않도록 설정
pre_trained_base.trainable = False

# 모델 구조 시각화
plot_model(pre_trained_base, show_shapes=True,
           show_layer_names=True, to_file='resnet50.png')

# Top 층에 Classifier 추가

def build_trainsfer_classifier():

    model = tf.keras.Sequential([

        # Pre-trained Base
        pre_trained_base,
        # Classifier 출력층
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dropout(0.3),
        tf.keras.layers.Dense(64, activation='relu'),
        tf.keras.layers.Dropout(0.3),
        tf.keras.layers.Dense(NUM_CLASSES, activation='softmax'),
    ])

    return model

# 모델 구조
tc_model = build_trainsfer_classifier()
tc_model.summary()

# 모델 컴파일
tc_model.compile(optimizer='adam',
                 loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# 모델 훈련
tc_history = tc_model.fit(train_aug,
                          validation_data=valid_aug,
                          epochs=5)

# 손실함수, 정확도 그래프 그리기
plot_loss_acc(tc_history, 50)


"""
Testing for row filtering

"""
import pytest

from koza.model.config.source_config import ColumnFilter, FilterCode, FilterInclusion
from koza.utils.row_filter import RowFilter

@pytest.mark.parametrize(
    "column, inclusion, code, value, result",
    [
        ('a', 'include', 'eq', 0.3, True),
        ('a', 'exclude', 'eq', 0.3, False),
        ('a', 'include', 'ge', 0.3, True),
        ('a', 'include', 'le', 0.3, True),
        ('a', 'include', 'ne', 0.3, False),
        ('a', 'include', 'ne', 0.4, True),
        ('a', 'include', 'lt', 0.4, True),
        ('a', 'include', 'gt', 0.4, False),
        ('a', 'include', 'gt', 0.2, True),
        ('a', 'exclude', 'gt', 0.4, True),
        ('a', 'exclude', 'gt', 0.2, False),
        ('b', 'include', 'eq', 10, True),
        ('b', 'include', 'ge', 10, True),
        ('b', 'include', 'le', 10, True),
        ('b', 'include', 'gt', 9, True),
        ('b', 'include', 'gt', 11, False),
        ('b', 'include', 'lt', 11, True),
        ('b', 'include', 'lt', 9, False),
        ('c', 'include', 'eq', 'llama', True),
        ('c', 'include', 'ne', 'alpaca', True),
        ('c', 'include', 'eq', 'alpaca', False),
        ('c', 'include', 'in', ['llama', 'alpaca'], True),
        ('c', 'include', 'in', ['condor', 'alpaca'], False),
    ],
)
def test_filter(column, inclusion, code, value, result):
    row = {'a': 0.3, 'b': 10, 'c': 'llama'}
    column_filter = ColumnFilter(
        column=column,
        inclusion=FilterInclusion(inclusion),
        filter_code=FilterCode(code),
        value=value,
    )

    rf = RowFilter([column_filter])

    assert rf.include_row(row) is result

@pytest.mark.parametrize(
    "column_filters, result",
    [
        (
            [
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('include'),
                    filter_code=FilterCode('lt'),
                    value=0.4,
                ),
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('include'),
                    filter_code=FilterCode('gt'),
                    value=0.1,
                ),
            ],
            True,
        ),
        (
            [
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('include'),
                    filter_code=FilterCode('lt'),
                    value=0.4,
                ),
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('exclude'),
                    filter_code=FilterCode('gt'),
                    value=0.4,
                ),
            ],
            True,
        ),
        (
            [
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('include'),
                    filter_code=FilterCode('in'),
                    value=[0.2, 0.3, 0.4],
                ),
                ColumnFilter(
                    column='b',
                    inclusion=FilterInclusion('exclude'),
                    filter_code=FilterCode('lt'),
                    value=9,
                ),
            ],
            True,
        ),
        (
            [
                ColumnFilter(
                    column='a',
                    inclusion=FilterInclusion('include'),
                    filter_code=FilterCode('in'),
                    value=[0.2, 0.3, 0.4],
                ),
                ColumnFilter(
                    column='b',
                    inclusion=FilterInclusion('exclude'),
                    filter_code=FilterCode('gt'),
                    value=9,
                ),
            ],
            False,
        ),
    ],
)
def test_multiple_filters(column_filters, result):
    row = {'a': 0.3, 'b': 10, 'c': 'llama'}

    rf = RowFilter(column_filters)

    assert rf.include_row(row) == result

def test_empty_filters():
    row = {'a': 0.3, 'b': 10, 'c': 'llama'}

    rf = RowFilter()

    assert rf.include_row(row)


import re

input_data = open("inputs/day12.in", "r").read().strip().split("\n")

def get_grav(c1, c2):
    return (c1 != c2) * ((c1 < c2) - (c1 > c2))

def unhash(moons, d):
    set_value = ()
    for moon in moons:
        set_value += (moons[moon]["pos"][d], moons[moon]["vel"][d])
    return set_value

moons = {}
for i, moon in enumerate(input_data):
    for pos in re.findall(r"<(.*)>", moon)[0].split(", "):
        exec(pos)

    moons[i] = {"pos": (x, y, z), "vel": (0, 0, 0)}
# moons[0]['pos'] = (-1,0,2)
# moons[1]['pos'] = (2,-10,-7)
# moons[2]['pos'] = (4,-8,8)
# moons[3]['pos'] = (3,5,-1)
moons[0]["pos"] = (-8, -10, 0)
moons[1]["pos"] = (5, 5, 10)
moons[2]["pos"] = (2, -7, 3)
moons[3]["pos"] = (9, -8, -3)

xstates = set()
ystates = set()
zstates = set()
xnew = True
ynew = True
znew = True
step = 0
while True:
    if unhash(moons, 0) not in xstates:
        xstates.add(unhash(moons, 0))
    elif xnew:
        xnew = False
        print("x:", step)

    ystates.add(unhash(moons, 1))
    zstates.add(unhash(moons, 2))
    for m1 in moons:
        for m2 in range(m1 + 1, len(moons)):
            gravity = tuple(
                get_grav(moons[m1]["pos"][i], moons[m2]["pos"][i]) for i in range(3)
            )
            moons[m1]["vel"] = tuple(v + dv for v, dv in zip(moons[m1]["vel"], gravity))
            moons[m2]["vel"] = tuple(v - dv for v, dv in zip(moons[m2]["vel"], gravity))
        moons[m1]["pos"] = tuple(
            p + v for p, v in zip(moons[m1]["pos"], moons[m1]["vel"])
        )

    step += 1

    if step == 100:
        energy = [
            (
                abs(moons[m]["pos"][0])
                + abs(moons[m]["pos"][1])
                + abs(moons[m]["pos"][2])
            )
            * (
                abs(moons[m]["vel"][0])
                + abs(moons[m]["vel"][1])
                + abs(moons[m]["vel"][2])
            )
            for m in moons
        ]
        print("Part 1:", sum(energy))

print("Part 2:", step)


# importing the module
import cv2
import numpy as np

file_depth = 'assets/test.txt'
# reading the image
img = cv2.imread('./test.png', 1)
f = open(file_depth, 'r')
lines = f.readlines()

depth = []
for line in lines:
    depth.append(line.split(' '))
# line1 = lines[0].split(' ')
print(depth[0][0])

h, w, _ = img.shape

h1 = h//2
w1 = w//2

img = cv2.resize(img, (w1, h1))

boxes = []

def click_event(event, x, y, flags, params):

    # checking for left mouse clicks
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x, ' ', y)

        print(f"depth: {depth[y*2][x*2]}")

        cv2.imshow('image', img)

# # driver function
if __name__=="__main__":

    # displaying the image
    cv2.imshow('image', img)

    # setting mouse handler for the image
    # and calling the click_event() function
    cv2.setMouseCallback('image', click_event)

    # wait for a key to be pressed to exit
    cv2.waitKey(0)

    # close the window
    cv2.destroyAllWindows()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer, BigInteger, TIMESTAMP
from sqlalchemy.dialects.mysql import DOUBLE
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class TeacherRate(Base):
    __tablename__ = 'teacher_rating_300'
    id = Column('id', BigInteger, primary_key=True)
    teacher_id = Column('teacher_id', Integer)
    rate_avg = Column('rate_avg', DOUBLE)
    number_rate = Column('number_rate', Integer)
    updated_time = Column('updated_time', TIMESTAMP)


"""
Module defining a webcam class for reading frames from an OAK.

Classes
-------
Webcam
    A class for reading frames from an OAK using the same interface as cv2.VideoCapture.
"""
from __future__ import annotations

import atexit
import contextlib
from threading import Condition, Thread
from typing import TYPE_CHECKING

import depthai as dai

from .calibration import ColorCalibrationData, get_camera_calibration_basic
from .nodes import create_color_camera, create_xout
from .tools.parsing import get_color_sensor_resolution_from_tuple

if TYPE_CHECKING:
    import numpy as np
    from typing_extensions import Self

class Webcam:
    """
    A class for reading frames from an OAK using the same interface as cv2.VideoCapture.

    Attributes
    ----------
    calibration : ColorCalibrationData
        The calibration info for the camera

    Methods
    -------
    stop()
        Stop the camera.
    read()
        Read a frame from the camera.
    """

    def __init__(
        self: Self, resolution: tuple[int, int] = (1920, 1080), fps: int = 30
    ) -> None:
        """
        Create a new Webcam object.

        Parameters
        ----------
        resolution : tuple[int, int], optional
            The resolution of the webcam, by default (1920, 1080)
        fps : int, optional
            The framerate of the webcam, by default 30
        """
        self._resolution = resolution
        self._fps = fps

        # get the calibration
        self._calibration = get_camera_calibration_basic(
            rgb_size=self._resolution,
        )

        # depthai stuff
        self._pipeline = dai.Pipeline()
        dai_resolution = get_color_sensor_resolution_from_tuple(self._resolution)
        self._cam = create_color_camera(
            self._pipeline, resolution=dai_resolution, fps=self._fps
        )
        self._xout_cam = create_xout(self._pipeline, self._cam.video, "cam")

        # frame storage
        self._frame: np.ndarray = None

        # thread for reading camera
        self._started = False
        self._stopped: bool = False
        self._thread: Thread = Thread(target=self._run, daemon=True)
        self._start_condition = Condition()

        # register stop function
        atexit.register(self.stop)

        # start the camera
        self._thread.start()
        with self._start_condition:
            self._start_condition.wait()

    @property
    def calibration(self: Self) -> ColorCalibrationData:
        """
        Returns the calibration info for the camera.

        Returns
        -------
        ColorCalibrationData
            The calibration info for the camera
        """
        return self._calibration.rgb

    def __del__(self: Self) -> None:
        """Stop the camera when the object is deleted."""
        self.stop()

    def stop(self: Self) -> None:
        """Stop the camera."""
        self._stopped = True
        with contextlib.suppress(RuntimeError):
            self._thread.join()

    def read(self: Self) -> tuple[bool, np.ndarray | None]:
        """
        Read a frame from the camera.

        Returns
        -------
        tuple[bool, np.ndarray]
            A tuple containing a boolean indicating if the frame was read successfully and the frame itself.
        """
        # get data
        return self._frame is not None, self._frame

    def _run(self: Self) -> None:
        """Run the camera."""
        with dai.Device(self._pipeline) as device:
            # get data queues
            q_camera = device.getOutputQueue(name="cam", maxSize=1, blocking=False)

            # loop until stopped
            while not self._stopped:
                # get data
                self._frame = q_camera.get().getCvFrame()

                # notify that we have started
                if not self._started:
                    with self._start_condition:
                        self._started = True
                        self._start_condition.notify()


import Player
import random

class Warrior(Player.Player):

    def __init__(self, name):
        self.list = [("Strengthstorm", 50), ("Divine Sword", 50),("Celestial Sword", 40),("Evil Sword", 40),("Fit of Rage", 60), ("Last whisper", 60)]
        self.class_name = "Warrior"
        self.attack_name = None
        self.damage = 50
        self.speed = 7
        self.defense = 2
        self.health = 100
        self.name = name
        self.bonus = [["Thor's Hammer", "damage", 10], ["Heal", "health", 10], ["Warrior effect", "damage", 20]]


import logging

from maasserver.enum import NODE_STATUS

def test_node_mark_failed_deployment_logs_failure(factory, caplog):
    node = factory.make_Node(
        status=NODE_STATUS.DEPLOYING, with_boot_disk=False
    )
    with caplog.at_level(logging.DEBUG):
        node.mark_failed()
    assert node.status == NODE_STATUS.FAILED_DEPLOYMENT
    record_tuple = (
        "maas.node",
        logging.DEBUG,
        f"Node '{node.hostname}' failed deployment",
    )

    assert record_tuple in caplog.record_tuples


from rest_framework import serializers
from .models import Post, Tag

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag
        fields = (
            'pk',
            'name',
        )

class PostSerializer(serializers.ModelSerializer):
    tag_name = TagSerializer(many=True)
    date_create = serializers.DateTimeField(format="%Y/%m/%d %H:%M", required=False, read_only=True)

    class Meta:
        model = Post
        #fields = "__all__"
        fields = (
            'pk',
            'title',
            'content',
            'meta_keywords',
            'meta_description',
            'date_create',
            'is_active',
            'count_view',
            'tag_name',
            'cover',

        )


def ess(string1 : str, string2 : str):
    """
    Encode the string1 and string2 as single string.
    :param string1: The first string
    :param string2: The second string
    :return: An encoded string.
    """
    dig = len(string1)
    return str(dig) + " " + string1 + string2

def dess(string : str, index: int):
    """
    Decode the string which encodes by ess function.
    :param string: The encoded string
    :param index: Which string should be returned. 1 or 2.
    The other number will be return ERROR
    :return: The first or second string or ERROR
    """
    if index > 2:
        return "ERROR"
    str1Length = int(string[0])
    str1 = string[2:(2 + str1Length)]
    str2 = string[(2 + str1Length):]
    if index == 1:
        return str1
    else:
        return str2

def echo(string: str):
    """
    :param string: One of string.
    :return:A copy of that string with each character duplicated right after it.
    """
    newStr = ""
    for s in string:
        ss = s + s
        newStr += ss
    return newStr

def find_last_mismatch(string1 : str, string2 : str):
    """
     If the strings are identical, then the function will return -1.
     Otherwise, return the last index at which the two strings differ.
    :param string1: The first string
    :param string2: The second string
    :return: -1 or the index
    """
    if string1 == string2:
        return -1
    ### prevent out of index if those strings lengths are not equal.
    minLength = min([len(string1), len(string2)])
    for i in range(minLength - 1, -1, -1):
        if string1[i] != string2[i]:
            return i

def max_length(str1 : str, str2 : str, str3 : str):
    """
    Find the max length of those three strings.
    :param str1: The first string
    :param str2: The second string
    :param str3: The third string
    :return: The length of the largest of the three strings.
    """
    leng1 = len(str1)
    leng2 = len(str2)
    leng3 = len(str3)
    if leng1 >= leng2:
        maxL = leng1
    else:
        maxL = leng2

    if maxL <= leng3:
        return leng3
    else:
        return maxL

def addChar(i, string, newString):
    if i >= len(string):
        return newString + " "
    else:
        return newString + string[i]

def interleave(str1 : str, str2 : str, str3 : str):
    """
    Concat the chars in those strings in order.
    :param str1: The first string
    :param str2: The second string
    :param str3: The third string
    :return: A string that is the result of interleaving the
    given strings together, one character at a time.
    """
    maxL = max_length(str1, str2, str3)
    newString = ""
    for i in range(maxL):
        newString = addChar(i, str1, newString)
        newString = addChar(i, str2, newString)
        newString = addChar(i, str3, newString)
    return newString

def get_longest_stretch(string : str, char : str):
    """
    Find the longest stretch of the target character in the given string.
    An example of this function.
          >>> get_longest_stretch (" effghggefhhhg " , " h")
          >>> 3
    :param string: A string
    :param char: A target character
    :return: The length of the longest stretch of the target character in the given string.
    """
    if char not in string: return 0
    longest = 1
    current = 1
    skip = 0
    for i, s in enumerate(string):
        ### This step is for the situation that if the
        ### string which is long enough and the repeated char is also too long.
        ### This operation will make this program more efficient.
        if i < skip:
            continue
        ### record and update the longest chars
        if s == char:
            w = i + 1
            while w < len(string) and string[w] == char:
                current += 1
                w += 1
            if current != 1 and current > longest:
                longest = current
            current = 1
            skip = w

    return longest

def compute_product_at(list1 : list, list2 : list):
    """
    The product of the values in the first list that are at the indices given in the
    second list.
    :param list1: first values list
    :param list2: second indices list
    :return: The product of those values.
    """
    pro = 1
    for i in list2:
        pro *= list1[i]
    return pro

def insert_name_here(vals : list):
    """
    This function will returns a list containing each integer in vals
    that is greater than the integers immediately before and after it.
    :param vals: values list
    :return: A list which contain integers
    """
    if len(vals) <= 1:
        return list()
    newList = list()
    for i, val in enumerate(vals):
        if i == 0 and val > vals[i + 1]:
            newList.append(val)
        elif i == (len(vals) - 1) and val > vals[i - 1]:
            newList.append(val)
        else:
            if vals[i - 1] < val and val > vals[i + 1]:
                newList.append(val)
    return newList

def get_key_to_min(dic: dict):
    """
    Find the key of the smallest value in the dictionary
    that is greater than or equal to 10.
    :param dic: A dictionary
    :return: The key of the smallest value in the dictionary
    that is greater than or equal to 10.
    """
    minV = 1000
    minKey = None
    for key, value in dic.items():
        if 10 <= value < minV:
            minKey = key
            minV = value
    return minKey


# Create an empty set
s = set()

# Add elements to the set
s.add(1)
s.add(2)
s.add(3)
s.add(4)
s.add(3) #En un set no se repiten form_valores_bfd

s.remove(2)
print(s)
#Saber la cantidad de elementos en un set
print (f"The set has {len(s)} elements.")


import os

import numpy as np
import tensorflow as tf

from ops import multi_stack_lstm

context_features = {"length": tf.FixedLenFeature(shape=[], dtype=tf.int64),
                    "static_landmark": tf.FixedLenFeature(shape=[204], dtype=tf.float32),
                    "speaker_embedding": tf.FixedLenFeature(shape=[256], dtype=tf.float32)}

sequence_features = {"content_embedding": tf.FixedLenSequenceFeature([64], dtype=tf.float32),
                     "landmark_label": tf.FixedLenSequenceFeature([204], dtype=tf.float32)}

def _parse_single_sequence_example(data_record):
    context_sample, sequence_sample = tf.parse_single_sequence_example(serialized=data_record,
                                                                       context_features=context_features,
                                                                       sequence_features=sequence_features)
    length = context_sample["length"]
    static_landmark = context_sample["static_landmark"]
    speaker_embedding = context_sample["speaker_embedding"]
    content_embedding = sequence_sample["content_embedding"]
    landmark_label = sequence_sample["landmark_label"]

    content_embedding.set_shape([None, 64])
    landmark_label.set_shape([None, 204])

    return length, static_landmark, speaker_embedding, content_embedding, landmark_label

def enumerate_tfrecord(dir):
    tfrecord_list = []
    for dirs, _, files in os.walk(dir):
        for file in files:
            if file.endswith(".tfrecord"):
                tfrecord_list.append(os.path.join(dirs, file))

    return tfrecord_list

def get_batch(tfrecord_path, batch_size, num_epochs=100):
    if os.path.isdir(tfrecord_path):
        tfrecord_list = enumerate_tfrecord(tfrecord_path)
        dataset = tf.data.TFRecordDataset(tfrecord_list)
    else:
        dataset = tf.data.TFRecordDataset(tfrecord_path)
    dataset = dataset.map(_parse_single_sequence_example)
    dataset = dataset.shuffle(2000)
    epoch = tf.data.Dataset.range(num_epochs)
    dataset = epoch.flat_map(lambda i: tf.data.Dataset.zip((dataset, tf.data.Dataset.from_tensors(i).repeat())))
    dataset = dataset.repeat(num_epochs)
    dataset = dataset.padded_batch(batch_size=batch_size, padded_shapes=([], [204], [256], [None, 64], [None, 204]))

    iterator = dataset.make_one_shot_iterator()

    (length, static_landmark, speaker_embedding, content_embedding, landmark_label), epoch_now = iterator.get_next()

    return length, static_landmark, speaker_embedding, content_embedding, landmark_label, epoch_now

batch_size = 5
time_steps = 100
hidden_size = 256

dataset = tf.data.TFRecordDataset(["sequence_example.tfrecord"])
dataset = dataset.map(_parse_single_sequence_example)
dataset = dataset.padded_batch(batch_size=batch_size, padded_shapes=([], [136], [256], [None, 64], [None, 136]))
#dataset = dataset.batch(1)
dataset = dataset.shuffle(100)
dataset = dataset.repeat(1)

iterator = dataset.make_one_shot_iterator()
length, static_landmark, speaker_embedding, content_embedding, landmark_label = iterator.get_next()
print(length, static_landmark, speaker_embedding, content_embedding, landmark_label)
#content_embedding.set_shape([None, None, 64])
content_embedding = tf.pad(content_embedding, paddings=[(0, 0), (0, 18), (0, 0)])
stacked_lstm = multi_stack_lstm(hidden_size=hidden_size, num_lstms=3)
initial_state = state = stacked_lstm.zero_state(batch_size=batch_size, dtype=tf.float32)

print(initial_state)

outputs, states = tf.nn.dynamic_rnn(stacked_lstm, content_embedding, initial_state=initial_state, time_major=False, sequence_length=length)
outputs = outputs[:, 18:, :]

mask = tf.sequence_mask(lengths=length, maxlen=tf.shape(outputs)[1])

print(outputs, states)

initializer = tf.global_variables_initializer()

i = 0
with tf.Session() as sess:
    sess.run(initializer)
    try:
        while True:
            i += 1
            m, out, l, sl, se, ce, ll = sess.run([mask, outputs, length, static_landmark, speaker_embedding, content_embedding, landmark_label])
            print(i, l, sl.shape, se.shape, ce.shape, ll.shape, m.shape)
            print(out.shape)
    except:
        print("Training process finished")
        pass


from django.shortcuts import redirect, render
from django.http import HttpResponse, HttpResponseRedirect

from student1.forms import StudentRegistration
from . models import User

# Create your views here.

# this function for adding and displaying data
def home(request):

    if request.method == "POST":

        fm=StudentRegistration(request.POST)
        if fm.is_valid():
            nam=fm.cleaned_data['name']
            em=fm.cleaned_data['email']
            ps=fm.cleaned_data['password']

            print(nam)
            print(em)
            print(ps)
            fm.save()

            # user=User(name=nam,email=em,password=ps)
            # user.save

    else:

            fm=StudentRegistration()

    stud=User.objects.all()

    return render(request,'stud1/stud1.html',{'form':fm,'stu':stud})

def delete(request,id):
    if request.method =='POST':
        pi=User.objects.get(pk=id)
        pi.delete()
    return HttpResponseRedirect('/')

def update(request,id):
    if request.method =='POST':
        p=User.objects.get(pk=id)
        fm=StudentRegistration(request.POST,instance=p)
        if fm.is_valid():
            fm.save()

    else:
        p=User.objects.get(pk=id)
        fm=StudentRegistration(instance=p)

    return render(request,'stud1/update.html',{'form':fm})


"""
    creates the dictionary of frequencies based on the corpus
    corpus can be any iterable
"""

def frequency_dict(corpus):
    freq = {}
    for token in corpus:
        if token in freq.keys():
            freq[token] += 1
        else:
            freq[token] = 1
    return freq

def get_min(roots):
    m = min(roots,key=lambda root: root[1])
    roots.remove(m)
    return m

"""
    creates the huffman tree based on a frequency dictionary
"""
def huffman_tree(freq):
    # initial list of leaves
    roots  = [item for item in freq.items()]

    # combining the roots until they al all in one tree
    while len(roots)>=2:
        left = get_min(roots)
        right = get_min(roots)
        new_root = [left, left[1]+right[1], right]
        roots.append(new_root)

    # only the root of the final tree left
    return roots[0]

def code_builder(root, code = {}, prefix = ""):
    if len(root) == 2:
        code[root[0]] = prefix
    if len(root) == 3:
        code_builder(root[0], code, prefix+"0")
        code_builder(root[2], code, prefix+"1")
    return code


import mido
import json
import os
import sys
import pickle
import numpy as np

from collections import defaultdict

instruments = [73, 74, 75, 76, 77, 78, 79, 80]

def process(filename):
    global instruments

    events = []

    f = mido.MidiFile(filename)
    for track in f.tracks:

        workChannel = -1

        currentNote = -1
        offset = -1
        duration = -1
        velocity = -1

        for msg in track:

            if msg.type == "program_change":
                if msg.program in instruments:
                    workChannel = msg.channel
                    print ("Flute channel set to %d\n" % workChannel)

            elif msg.type == "note_on":
                if workChannel >= 0:
                    if msg.velocity > 0:
                        currentNote = msg.note
                        offset = msg.time
                        velocity = msg.velocity
                    else:
                        if currentNote != -1 and offset != -1 and velocity != -1:
                            duration = msg.time
                            events.append((currentNote, offset, duration, velocity))

                        currentNote = -1
                        offset = -1
                        duration = -1
                        velocity = -1

            elif msg.type == "note_off":
                if currentNote != -1 and offset != -1 and velocity != -1:
                    duration = msg.time
                    events.append((currentNote, offset, duration, velocity))

                currentNote = -1
                offset = -1
                duration = -1
                velocity = -1

        if workChannel >= 0:
            global midiEvents
            midiEvents += events

midiEvents = []

fileCount = 0
for root, directories, filenames in os.walk("midi"):
    for filename in filenames:
        if filename.endswith(".mid") or filename.endswith(".midi"):
            print("Step 1. Processing %s..." % (filename))
            try:
                process(os.path.join(root, filename))
                fileCount += 1
                print("Step 1. Processed %s! (total = %d)" % (filename, fileCount))
            except KeyboardInterrupt:
                raise
            except:
                e = sys.exc_info()[0]
                print("Step 1. Fail to process %s! %s" % (filename, e))
                #raise

            # to prevent waiting for whole dataset
            #if fileCount > 5:
            #    break

print("Step 1. Finished! Processed %d files, %d MIDI events" % (fileCount, len(midiEvents)))

notes = defaultdict(int)
offsets = defaultdict(int)
durations = defaultdict(int)
velocities = defaultdict(int)

for event in midiEvents:
    notes[event[0]] += 1
    offsets[event[1]] += 1
    durations[event[2]] += 1
    velocities[event[3]] += 1

print ("Notes: %s\n" % notes.keys())
print ("Offsets: %s\n" % offsets.keys())
print ("Durations: %s\n" % durations.keys())
print ("Velocities: %s\n" % velocities.keys())

uniqNotes = len(notes)
uniqOffsets = len(offsets)
uniqDurations = len(durations)
uniqVelocities = len(velocities)
print("Step 2. Unique notes %d, offsets %d, durations %d, velocities %d, total = %d" % (uniqNotes, uniqOffsets, uniqDurations, uniqVelocities, uniqNotes + uniqOffsets + uniqDurations + uniqVelocities))

notesKeys = sorted(notes.keys())
notesEncoded = { n:i for i,n in enumerate(notesKeys)}

offsetsKeys = sorted(offsets.keys())
offsetsEncoded = { o:i for i,o in enumerate(offsetsKeys)}

durationsKeys = sorted(durations.keys())
durationsEncoded = { d:i for i,d in enumerate(durationsKeys)}

velocitiesKeys = sorted(velocities.keys())
velocitiesEncoded = { v:i for i,v in enumerate(velocitiesKeys)}

pickle.dump(notesKeys, open("g2.notes.p", "wb"))
pickle.dump(offsetsKeys, open("g2.offsets.p", "wb"))
pickle.dump(durationsKeys, open("g2.durations.p", "wb"))
pickle.dump(velocitiesKeys, open("g2.velocities.p", "wb"))

print("Step 3. One Hot encoding completed!")

X = np.zeros((len(midiEvents), uniqNotes + uniqOffsets + uniqDurations + uniqVelocities), dtype=np.float32)
print("Step 4. Training file shape:", X.shape)

for i, (note, offset, duration, velocity) in enumerate(midiEvents):
    noteOneHot = np.zeros(uniqNotes)
    noteOneHot[notesEncoded[note]] = 1.0
    X[i, 0:uniqNotes] = noteOneHot

    offsetOneHot = np.zeros(uniqOffsets)
    offsetOneHot[offsetsEncoded[offset]] = 1.0
    X[i, uniqNotes:uniqNotes + uniqOffsets] = offsetOneHot

    durationOneHot = np.zeros(uniqDurations)
    durationOneHot[durationsEncoded[duration]] = 1.0
    X[i, uniqNotes + uniqOffsets:uniqNotes + uniqOffsets + uniqDurations] = durationOneHot

    velocityOneHot = np.zeros(uniqVelocities)
    velocityOneHot[velocitiesEncoded[velocity]] = 1.0
    X[i, uniqNotes + uniqOffsets + uniqDurations:] = velocityOneHot

np.save("g2.X.npy", X)


##
## EPITECH PROJECT, 2020
## 303
## File description:
## calc
##

import sys
import math
from collections import OrderedDict

### Build graph ###
def getFileList(array):
    fileList = dict()
    for line in array:
        if (len(line) > 0 and line[0][-1] == ':'):
            fileList[line[0][:-1]] = ""
            for i in line[1:]:
                fileList[i] = ""
    return sorted(fileList)

def findNameList(name, fileList):
    i = 0

    for k in range(len(fileList)):
        if fileList[k] == name:
            i = k
    return i

def amountOfPresence(name, array):
    count = 0

    for line in array:
        for words in line:
            if (len(line) > 0 and line[0][-1] == ':'):
                if words == name:
                    count += 1

    return count

def lastOne(graph, name):
    count = 0
    length = len(graph)

    for each in (graph.keys()):
        if each == name:
            break
        count += 1
    print("Count", count)
    print("Length", length)
    if count == length:
        return True
    else:
        return False

def recurGraph(graph, name, links):

    if len(graph[name]) == 0:
        sys.stdout.write(links)
        return
    links += name + " -> "
    for each in graph[name]:
        links += each
        if (len(graph[each]) > 0):
            links += " -> "
        recurGraph(graph, each, links)

def printFileArrow(array, fileList, fileLinks):
    target = ""
    atEnd = False
    links = str()
    count = 0
    saveArray = array

    for i in range(len(fileList)):
        if len(fileLinks[i]) == 0:
            continue
        sys.stdout.write(fileList[i] + " -> ")
        target = fileList[i]
        while 1:
            if len(fileLinks[findNameList(target, fileList)]) == 0:
                break
            for line in array:
                if (len(line) > 0 and line[0][-1] == ':'):
                    if target in line:
                        sys.stdout.write(line[0][:-1])
                        if len(fileLinks[findNameList(line[0][:-1], fileList)]) != 0:
                            sys.stdout.write(" -> ")
                        target = line[0][:-1]
                        break
        print("")

    # graph = OrderedDict()
    # for i in range(len(fileList)):
    #     graph[fileList[i]] = list()
    #     for each in fileLinks[i]:
    #         graph[fileList[i]].append(each)

    # for file, lists in graph.items():
    #     links = file + " -> "
    #     if len(lists) == 0:
    #         continue
    #     for each in lists:
    #         if len(graph[each]) == 0:
    #             sys.stdout.write(links  + each)
    #             if file != graph.keys()[-1]:
    #                 print("")
    #             continue
    #         recurGraph(graph, each, links)
    #         if file != graph.keys()[-1]:
    #             print("")
    # print("")

### Engine for Option 1 -> Matrice display ###
def matrixEngine(array):
    fileList = getFileList(array)
    fileLinks = list()
    matrix = list()
    i = 0
    count = 0

    for each in fileList:
        fileLinks.append(list())
        for line in array:
            if len(line) > 0 and line[0][-1] == ':' and each in line:
                fileLinks[i].append(line[0][:-1])
            fileLinks[i] = sorted(fileLinks[i])
        i += 1
    for i in range(len(fileList)):
        matrix.append(list())
        for j in range(len(fileList)):
            matrix[i].append(0)

    for i in range(len(fileList)):
        for name in fileLinks[i]:
            matrix[i][findNameList(name, fileList)] = 1
    for line in matrix:
        sys.stdout.write("[")
        for each in line[:-1]:
            sys.stdout.write(str(each) + " ")
        print(str(line[-1]) + "]")
        count += 1
    if count == 0:
        print("Inconsistent file")
        exit(84)
    print("")

    printFileArrow(array, fileList, fileLinks)

def printingCompileLine(fileList, fileLinks, array, file):
    i = 84
    target = ""

    if file == "":
        return
    for k in range(len(fileList)):
        if fileList[k] == file:
            i = k
    if i == 84:
        exit(84)
    for links in fileLinks[i]:
        target = links + ":"
        for k in range(len(array)):
            if len(array[k]) > 0 and array[k][0][-1] == ':' and target in array[k]:
                for listPart in array[k + 1:]:
                    if (len(listPart) == 0):
                        break
                    else:
                        for part in listPart:
                            sys.stdout.write(part)
                            if part != listPart[-1]:
                                sys.stdout.write(" ")
                        print("")
    if len(fileLinks[i]) == 0:
            exit(0)
    for links in fileLinks[i]:
        printingCompileLine(fileList, fileLinks, array, links)

### Engine for Option 2 -> Display 'cc' lines ###
def clinesEngine(array, file):
    fileList = getFileList(array)
    fileLinks = list(list())
    i = 0

    for each in fileList:
        fileLinks.append(list())
        for line in array:
            if len(line) > 0 and line[0][-1] == ':' and each in line:
                fileLinks[i].append(line[0][:-1])
            fileLinks[i] = sorted(fileLinks[i])
        i += 1
    if len(fileLinks[findNameList(file, fileList)]) == 0:
        print("")
        return(0)
    printingCompileLine(fileList, fileLinks, array, file)


#!/usr/bin/env python3

import argparse
from pathlib import Path
import platform
import subprocess
import sys

from conans.client import conan_api

openrw_path = Path(__file__).resolve().parents[2]
cmake_generator_lookup = {
    2017: 'Visual Studio 15 2017',
}
architectures = ['x64', 'x86']

conan_arch_map = {
    'x86': 'x86',
    'x64': 'x86_64',
}

def to_cmake_generator(vs_version, arch):
    cmake_generator = cmake_generator_lookup[vs_version]
    if arch == 'x64':
        cmake_generator = '{} Win64'.format(cmake_generator)
    return cmake_generator

def create_solution(path, vs_version, arch):
    conan, _, _ = conan_api.ConanAPIV1.factory()
    conan.remote_add('bincrafters', 'https://api.bintray.com/conan/bincrafters/public-conan', force=True)
    conan_arch = conan_arch_map[arch]
    conan.install(path=openrw_path, generators=('cmake',), build=['missing', ],
                  settings=('build_type=Release', 'arch={}'.format(conan_arch), 'compiler.runtime=MD', ),
                  install_folder=path)
    cmake_generator = to_cmake_generator(vs_version=vs_version, arch=arch)
    subprocess.run([
        'cmake', '-DUSE_CONAN=ON', '-DBOOST_STATIC=ON', '-DMSVC_NO_DEBUG_RUNTIME=ON',
        '-DBUILD_TESTS=ON', '-DBUILD_VIEWER=ON', '-DBUILD_TOOLS=ON',
        '-G{}'.format(cmake_generator), str(openrw_path),
    ], cwd=path, check=True)

def main():
    parser = argparse.ArgumentParser(description='Create a Visual Studio solution for OpenRW.')
    parser.add_argument('path', nargs='?', default=Path(), metavar='PATH', type=Path, help='Location to the solution')
    parser.add_argument('-v', default=max(cmake_generator_lookup.keys()), choices=list(cmake_generator_lookup.keys()),
                        type=int, metavar='VERSION', dest='vs_version',
                        help='Version of Visual Studio (choices={})'.format(list(cmake_generator_lookup.keys())))
    parser.add_argument('-a', default=architectures[0], choices=architectures, metavar='ARCH', dest='arch',
                        help='Architecture to build (choices={})'.format(architectures))
    ns = parser.parse_args()

    if platform.system() != 'Windows':
        print('This script can only generate Visual Studio solutions for Windows.', file=sys.stderr)
        sys.exit(1)

    path = ns.path.resolve()
    arch = ns.arch
    vs_version = ns.vs_version

    print('Solution directory: {}'.format(path))
    print('Architecture: {}'.format(arch))
    print('Visual Studio version: {}'.format(vs_version))

    create_solution(path=path, vs_version=vs_version, arch=arch)

if __name__ == '__main__':
    main()


from instruction_files.generator_class import GeneratorBasic

import string
import json
import random
from string import Template
import os
import re
from collections import Counter, defaultdict
import settings
from utils.common import get_options_string, get_alphabetwithoptions_string
import copy

instruction_dict = {
    "id": "slot_present",
    "Source": [
        "self"
    ],
    "Definitions": ["In this task you will be shown some dialogue text and you need to predict if a slot if present in the utterance.",
                    "Read the dialogue utterance and predict if a slot is present in the utterance.",
                    "Decided if a slot exists in the utterance given the dialogue text"],
    "Positive Examples": [
        {
            "text": "[RESPONSE] Yes. That sounds great. Can I scheduled a visit for the 9th of March? [ENDOFDIALOGUE]. The possible options are: [OPTIONS] yes||||no [QUESTION]. The slot visit date is present in the response?",
            "output": [
                "yes"
            ],
            "index": 2832,
            "candidates": [
                "yes",
                "no"
            ],
            "classes_in_options": [
                "yes",
                "no"
            ],
            "split": "train",
            "dataset": "slot-dstc8_sgd"
        },
        {
            "text": "[RESPONSE] time An hour before 6:30pm [ENDOFDIALOGUE]. The possible options are: [OPTIONS] yes||||no [QUESTION]. Is the slot time present in the response?",
            "output": [
                "yes"
            ],
            "index": 5418,
            "candidates": [
                "yes",
                "no"
            ],
            "classes_in_options": [
                "yes",
                "no"

            ],
            "split": "train",
            "dataset": "slot-restaurant8k"
        }
    ]
}

domain_term_dict = {}
domain_term_dict['uber_lyft'] = {'location.from':'from location',
                                'location.to':'to location',
                                'type.ride':'ride type',
                                'num.people':'number of people',
                                'price.estimate':'estimate price',
                                'duration.estimate':'estimate duration',
                                'time.pickup':'pickup time',
                                'time.dropoff':'dropoff time'}

domain_term_dict['movie_ticket'] = {'name.movie':'movie name',
                                   'name.theater':'theater name',
                                   'num.tickets':'number of tickets',
                                   'time.start':'start time',
                                   'location.theater':'theater location',
                                   'price.ticket':'ticket price',
                                   'type.screening':'screening type',
                                   'time.end':'end time',
                                   'time.duration':'duration time'}

domain_term_dict['restaurant_reservation'] = {'name.restaurant':'restaurant name',
                                             'name.reservation':'reservation name',
                                             'num.guests':'number of guests',
                                             'time.reservation':'reservation time',
                                             'type.seating':'seating type',
                                             'location.restaurant':'restaurant location'}

domain_term_dict['coffee_ordering'] = {'location.store':'store location',
                                      'name.drink':'drink name',
                                      'size.drink':'drink size',
                                      'num.drink':'number of drink',
                                      'type.milk':'milk type',
                                      'preference':'preference'}

domain_term_dict['pizza_ordering'] = {'name.store':'store name',
                                     'name.pizza':'pizza name',
                                     'size.pizza':'pizza size',
                                     'type.topping':'topping type',
                                     'type.crust':'crust type',
                                     'preference':'preference',
                                     'location.store':'store location'}

domain_term_dict['auto_repair'] = {'name.store':'store name',
                                  'name.customer':'customer name',
                                  'date.appt':'appt date',
                                  'time.appt':'appt time',
                                  'reason.appt':'appt reason',
                                  'name.vehicle':'vehicle name',
                                  'year.vehicle':'vehicle year',
                                  'location.store':'store location'}

# def clean_taskmaster_slots(slots_dict):
#     new_dict = {}
#     for k,val in slots_dict.items():
#         for i, v in enumerate(val):
#             if v[-1] == '.' or v[-1] == ',' or v[-1] == '?' and 'p.m' not in v.lower() and 'a.m' not in v.lower() :
#                 v = v[:-1]
#                 val[i] = v
#         kwords = k.split('.')
#         item_domain = kwords[0]
#         item_key = '.'.join(kwords[1:3])
#         if 'preference' in item_key: item_key = 'preference'
#         # print(k, item_domain, item_key)
#         if item_key not in domain_term_dict[item_domain]:
#             new_dict[item_key] = val
#             # print(new_dict[item_key], val, k)
#         else:
#             new_key = domain_term_dict[item_domain][item_key]
#             new_dict[new_key] = val
#     slots_dict = copy.deepcopy(new_dict)
#
#     return slots_dict

def clean_taskmaster_slots(slots_dict):
    for k,val in slots_dict.items():
        for i, v in enumerate(val):
            if v[-1] == '.' or v[-1] == ',' or v[-1] == '?' and 'p.m' not in v.lower() and 'a.m' not in v.lower() :
                v = v[:-1]
                val[i] = v
        slots_dict[k] = val

    return slots_dict

def clean_taskmaster_slotname(slotname):
    kwords = slotname.split('.')
    item_domain = kwords[0]
    item_key = '.'.join(kwords[1:3])
    if 'preference' in item_key: item_key = 'preference'
    new_slotname = domain_term_dict[item_domain].get(item_key, item_key)
    item_domain_name = ' '.join(item_domain.split('_'))
    new_slotname = new_slotname.replace('_', ' ')
    # slot_text = f"{new_slotname} in domain {item_domain_name}"
    slot_text = f"{new_slotname}"

    return slot_text

def get_slot_values(text, slot):
    text_words = text.split()
    slot_words = slot.split()
    slot_inter = []
    word_inter = []
    slots = defaultdict(list)
    for i, (t,s) in enumerate(zip(text_words,slot_words)):
        if "B-" in s:
            if len(slot_inter)>0:
                slot_val = (slot_inter)
                word_val = ' '.join(word_inter)
                # print('word_valb', word_val)
                slots[slot_val[0].replace('I-', '').replace('B-', '')].append(word_val)
                # print(slot_val, word_val)
            slot_inter, word_inter = [], []
            slot_inter.append(s)
            word_inter.append(t)
        elif 'I-' in s:
            slot_inter.append(s)
            word_inter.append(t)
        elif s=='O':
            if len(slot_inter)>0:
                slot_val = (slot_inter)
                word_val = ' '.join(word_inter)
                # print('word_val', word_val)
                slots[slot_val[0].replace('I-', '').replace('B-', '')].append(word_val)
                # print(slot_val, word_val)
            slot_inter, word_inter = [], []
    if len(slot_inter)>0:
        slot_val = (slot_inter)
        word_val = ' '.join(word_inter)
        slots[slot_val[0].replace('I-', '').replace('B-', '')].append(word_val)
        # print(slot_val, word_val)

    return slots

class Generator(GeneratorBasic):
    def __init__(self, args, taskconfig, data_readers):
        self.idx = 0
        self.args = args
        self.taskconfig = taskconfig
        if 'max_data' in self.taskconfig:
            self.max_data = self.taskconfig['max_data']
        else:
            self.max_data = args.max_data
        self.output_folder = args.tasks_output_folder
        self.out_file = self.taskconfig['task_name']
        self.data_readers = data_readers
        self.examples = []

    def generate_data(self):
        print('number of datareaders:', len(self.data_readers))
        sequences = []
        for d, dataset_reader in enumerate(self.data_readers):
            print(dataset_reader.name)
            dataset_reader.idx=0
            iterator_index = 0
            datapoints = []
            split = dataset_reader.split
            dp = dataset_reader.get_next()
            while dp is not None:
                # print(dp)
                iterator_index+=1
                dp = dataset_reader.get_next()
                if dp and 'index' not in dp:
                    dp['index'] = iterator_index
                if dp:
                    datapoints.append(dp)
                    dp['split'] = split
            datapoints = random.sample(datapoints, min(len(datapoints), self.max_data))
            definitions = instruction_dict['Definitions']
            yesnooptionstring = get_options_string(['yes', 'no'])
            candidates = ['yes', 'no']
            mapped_instruction = '. The possible options are: ' + yesnooptionstring

            print('len(datapoints)', len(datapoints))
            slot_classes = (dataset_reader.slot_classes)
            slot_classes = [x.replace('I-', '').replace('B-', '') for x in slot_classes]
            slot_classes = list(set(slot_classes) - set(['[PAD]', 'O']))

            if dataset_reader.name=='taskmaster':
                new_slot_classnames = []
                for k in slot_classes:
                    newslotname = clean_taskmaster_slotname(k)
                    new_slot_classnames.append(newslotname)
                slot_classes = new_slot_classnames[:]
                # print(slot_classes)

            for dp in datapoints:
                slot =  dp['slots']
                response = dp.get('text')
                if response is None:
                    response = dp['response']
                slots_dict = get_slot_values(response, slot)
                if dataset_reader.name == 'taskmaster':
                    new_slots_dict = {}
                    for k,v in slots_dict.items():
                        newk = clean_taskmaster_slotname(k)
                        new_slots_dict[newk] = v
                    slots_dict = copy.deepcopy(new_slots_dict)
                    # slots_dict = clean_taskmaster_slots(slots_dict)
                if 'O' in slots_dict:
                    del slots_dict['O']
                # print(slots_dict)
                # if len(slots_dict)==0:
                #     continue
                index = dp.get('index', -1)
                split = dp.get('split', 'unspecified')
                # mapping = {}
                # mapped_definition = Template(definition).substitute(**mapping)
                for k in slots_dict:
                    # v = random.choice(slots_dict[k])
                    kval = k.replace('_', ' ').replace('.', ' ')
                    slot = kval
                    # text = settings.RESPONSE_SEP + ' ' + response+ " " + settings.EOT_SEP+" Question: Is the slot "+ slot +" present in the utterance?"
                    context = dp.get('context', '')
                    if context !='' and type(context) is list:
                        context = (' '+settings.EOT_SEP+ ' ').join(dp['context'][-settings.MAX_CONTEXT_NUMUTTERANCE:])
                    if context!='':
                        text = settings.CONTEXT_SEP + ' ' + context + ' '+settings.RESPONSE_SEP + ' ' + response+ ' ' + settings.EOD_SEP + mapped_instruction
                    else:
                        text = settings.RESPONSE_SEP + ' ' + response+ ' ' + settings.EOD_SEP + mapped_instruction
                    if 'auto_repair' in kval:
                        print(response)
                        print(text)
                        import pdb; pdb.set_trace()
                    post_prompts = [settings.QUESTION_SEP+'. Is the slot: ' + slot + ' present in the response?', settings.QUESTION_SEP+'. The slot: ' + slot + ' is present in the response?', settings.QUESTION_SEP+'. Does the provided slot exist the response? The slot is: ' + slot, settings.QUESTION_SEP+'. Does the provided slot exist the response? The slot is: ' + slot, settings.QUESTION_SEP+'. Is the slot: ' + slot + ' in the response?']

                    text = text + ' ' + random.choice(post_prompts)
                    text = re.sub(' +', ' ', text)
                    output = 'yes'
                    sequences.append({'text':text, 'output': [output], 'index':index, 'candidates':candidates, 'classes_in_options':candidates, 'metadata':{'slot_label': slot, 'response':response}, 'split':split, 'dataset':dataset_reader.name})
                    random_slot = None
                    while True:
                        random_slot = random.choice(slot_classes)
                        if random_slot.replace(' ', '-') not in slots_dict:
                            break
                    slot = random_slot
                    slot = slot.replace('_', ' ').replace('.', ' ')
                    # text = settings.RESPONSE_SEP + ' ' + response+ " " + settings.EOT_SEP +"Question: Is the slot "+ slot +" present in the utterance?"
                    post_prompts = [settings.QUESTION_SEP+'. Is the slot: ' + slot + ' present in the response?', settings.QUESTION_SEP+'. The slot: ' + slot + ' is present in the response?', settings.QUESTION_SEP+'. Does the provided slot exist the response? The slot is: ' + slot, settings.QUESTION_SEP+'. Does the provided slot exist the response? The slot is: ' + slot, settings.QUESTION_SEP+'. Is the slot: ' + slot + ' in the response?']
                    if context!='':
                        text = settings.CONTEXT_SEP + ' ' + context + ' '+settings.RESPONSE_SEP + ' ' + response+ ' ' + settings.EOD_SEP + mapped_instruction
                    else:
                        text = settings.RESPONSE_SEP + ' ' + response+ ' ' + settings.EOD_SEP + mapped_instruction

                    text = text + ' ' + random.choice(post_prompts)
                    text = re.sub(' +', ' ', text)
                    output = 'no'
                    sequences.append({'input':text, 'outputs': [output], 'index':index, 'candidates':candidates, 'classes_in_options':candidates, 'metadata':{'slot_label': slot, 'response':response}, 'split':split, 'dataset':dataset_reader.name})
            print(f'after {dataset_reader.name} size', len(sequences))
        return (sequences, instruction_dict)

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

    def __getitem__(self, idx):
        return self.examples[idx]


import logging
from typing import Dict, List

from soda.common.yaml_helper import to_yaml_str
from tests.helpers.common_test_tables import (
    customers_dist_check_test_table,
    customers_test_table,
)
from tests.helpers.data_source_fixture import DataSourceFixture

def assert_no_duplicate_check_identities(scan_result: dict):
    cloud_checks_by_identity = get_cloud_checks_by_identity(scan_result)

    duplicate_identity_messages = []
    for identity, duplicate_identity_cloud_checks in cloud_checks_by_identity.items():
        if len(duplicate_identity_cloud_checks) > 1:
            duplicate_identity_messages.append(
                f"Duplicate check identities: {len(duplicate_identity_cloud_checks)} checks have identity {identity}:"
            )
            for duplicate_identity_cloud_check in duplicate_identity_cloud_checks:
                location = duplicate_identity_cloud_check["location"]
                file_path = location["filePath"]
                line = location["line"]
                definition = duplicate_identity_cloud_check["definition"]
                duplicate_identity_messages.append(f"{file_path}:{line} {definition}")
            duplicate_identity_messages.append("")

    if duplicate_identity_messages:
        raise AssertionError("\n".join(duplicate_identity_messages))

def get_cloud_checks_by_identity(scan_result) -> Dict[str, List[dict]]:
    logging.debug(to_yaml_str(scan_result))
    cloud_checks_by_identity: Dict[str, List[dict]] = {}
    for cloud_check in scan_result["checks"]:
        identity = cloud_check["identity"]
        cloud_checks_by_identity.setdefault(identity, []).append(cloud_check)
    return cloud_checks_by_identity

def execute_scan_and_get_scan_result(data_source_fixture: DataSourceFixture, sodacl_yaml_str: str) -> dict:
    scan = data_source_fixture.create_test_scan()
    mock_soda_cloud = scan.enable_mock_soda_cloud()
    scan.add_sodacl_yaml_str(sodacl_yaml_str)
    scan.execute()
    return mock_soda_cloud.pop_scan_result()

def test_check_identity_ignore_name(data_source_fixture: DataSourceFixture):
    table_name = data_source_fixture.ensure_test_table(customers_test_table)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 0
        """,
    )

    row_count_identity = scan_result["checks"][0]["identity"]

    assert isinstance(row_count_identity, str)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 0:
                name: Naming this check should not change the identity!
        """,
    )

    # check that the identity remains the same
    assert scan_result["checks"][0]["identity"] == row_count_identity

    logs = scan_result["logs"]
    assert len(logs) > 0
    first_log = logs[0]
    assert first_log.get("level") == "info"
    assert first_log.get("message").startswith("Soda Core 3.")
    first_log_index = first_log.get("index")
    assert isinstance(first_log_index, int) and first_log_index >= 0
    first_log_timestamp = first_log.get("timestamp")
    assert isinstance(first_log_timestamp, str) and len(first_log_timestamp) > 0

def test_check_identity_line_number_change(data_source_fixture: DataSourceFixture):
    table_name = data_source_fixture.ensure_test_table(customers_test_table)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - missing_count(id) < 10
        """,
    )

    missing_identity = scan_result["checks"][0]["identity"]

    assert isinstance(missing_identity, str)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 0
            - missing_count(id) < 10
        """,
    )

    assert scan_result["checks"][1]["identity"] == missing_identity

def test_explicitely_specified_check_identity(data_source_fixture: DataSourceFixture):
    # 1. First a Soda Cloud user creates a new check
    # 2. Then the soda cloud user asks the Soda Cloud editor to fill in the identity in the check source so that...
    # 3. The Soda Cloud user can update the check keeping the same identity and hence without loosing the history

    table_name = data_source_fixture.ensure_test_table(customers_test_table)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 0
        """,
    )

    row_count_identity = scan_result["checks"][0]["identity"]

    assert isinstance(row_count_identity, str)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 0:
                identity: {row_count_identity}
        """,
    )

    # check that the identity remains the same
    assert scan_result["checks"][0]["identity"] == row_count_identity

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          checks for {table_name}:
            - row_count > 1:
                identity: {row_count_identity}
        """,
    )

    # check that the identity remains the same after changing the check (threshold in this case)
    assert scan_result["checks"][0]["identity"] == row_count_identity

def test_for_each_identity(data_source_fixture: DataSourceFixture):
    """Tests that same check generated by "for each" clause and manually will have unique identity and definition."""
    customers_table_name = data_source_fixture.ensure_test_table(customers_test_table)
    customers_dist_table_name = data_source_fixture.ensure_test_table(customers_dist_check_test_table)

    scan_result = execute_scan_and_get_scan_result(
        data_source_fixture,
        f"""
          for each dataset D:
            datasets:
                - {customers_table_name}
                - {customers_dist_table_name}
            checks:
            - row_count > 0
          checks for {customers_table_name}:
            - row_count > 0
        """,
    )
    assert scan_result["checks"][0]["identity"] != scan_result["checks"][1]["identity"]
    assert scan_result["checks"][0]["identity"] != scan_result["checks"][2]["identity"]
    assert scan_result["checks"][1]["identity"] != scan_result["checks"][2]["identity"]


with open('data.txt') as file:
	data = file.readlines()
	data = [line.rstrip() for line in data]

positions = [0] * len(data[0])

for line in data:
	for i, bit in enumerate(line):
		if bit == '1':
			positions[i] += 1
		else:
			positions[i] -= 1

for i, bit in enumerate(positions):
	if int(bit) < 0:
		positions[i] = '0'
	else:
		positions[i] = '1'

gamma = int(''.join(positions), 2)

for i, character in enumerate(positions):
	if character == '1':
		positions[i] = '0'
	else:
		positions[i] = '1'

epsilon = int(''.join(positions), 2)
print(gamma * epsilon)


#!/usr/bin/env python
# coding=utf-8
'''
@Description:
@Author: Xuannan
@Date: 2019-11-25 10:40:04
@LastEditTime: 2019-11-26 22:25:08
@LastEditors: Xuannan
'''

import qrcode,base64
from io import BytesIO

def set_qrcode(url):
    """
    根据传入的url 生成 二维码对象
    :param url:
    :return:
    """
    qr = qrcode.QRCode(version=1,  # 二维码大小 1～40
                       error_correction=qrcode.constants.ERROR_CORRECT_L,  # 二维码错误纠正功能
                       box_size=10,  # 二维码 每个格子的像素数
                       border=2)     # 二维码与图片边界的距离

    qr.add_data(url)
    qr.make(fit=True)
    img = qr.make_image()
    byte_io = BytesIO()
    img.save(byte_io, 'PNG')
    res = byte_io.getvalue()
    byte_io.close()
    return (base64.b64encode(res)).decode()


# coding=utf-8
import copy
from utils import utils
import numpy as np
import logging
import pandas as pd
from tqdm import tqdm
import torch
from collections import defaultdict, Counter
from utils.global_p import *
from sklearn.utils import shuffle

class DataProcessor(object):
    data_columns = [UID, IID, X]  # keys of required feature info in data dict, need to be converted into tensor
    info_columns = [SAMPLE_ID, TIME]  # key of extra info in data dict

    @staticmethod
    def parse_dp_args(parser):
        """
        data processing command parameters
        :param parser:
        :return:
        """
        parser.add_argument('--test_sample_n', type=int, default=100,
                            help='Negative sample num for each instance in test/validation set when ranking.')
        parser.add_argument('--train_sample_n', type=int, default=1,
                            help='Negative sample num for each instance in train set when ranking.')
        parser.add_argument('--sample_un_p', type=float, default=1.0,
                            help='Sample from neg/pos with 1-p or unknown+neg/pos with p.')
        parser.add_argument('--unlabel_test', type=int, default=0,
                            help='If the label of test is unknown, do not sample neg of test set.')
        return parser

    @staticmethod
    def batch_to_gpu(batch):
        if torch.cuda.device_count() > 0:
            new_batch = {}
            for c in batch:
                if type(batch[c]) is torch.Tensor:
                    new_batch[c] = batch[c].cuda()
                else:
                    new_batch[c] = batch[c]
            return new_batch
        return batch

    def __init__(self, data_loader, rank, train_sample_n, test_sample_n, sample_un_p, unlabel_test=0):
        """
        initialization
        :param data_loader: DataLoader
        :param model: Model
        :param rank: 1=topn recommendation; 0=rating/clicking prediction
        :param test_sample_n: negative sampling ratio for evaluation when topn recommendation task. positive:negative=1:test_sample_n
        """
        self.data_loader = data_loader
        self.rank = rank
        self.train_data, self.validation_data, self.test_data = None, None, None

        self.test_sample_n = test_sample_n
        self.train_sample_n = train_sample_n
        self.sample_un_p = sample_un_p
        self.unlabel_test = unlabel_test

        if self.rank == 1:
            # generate history dict
            self.train_history_pos = defaultdict(set)
            for uid in data_loader.train_user_pos.keys():
                self.train_history_pos[uid] = set(data_loader.train_user_pos[uid])
            self.validation_history_pos = defaultdict(set)
            for uid in data_loader.validation_user_pos.keys():
                self.validation_history_pos[uid] = set(data_loader.validation_user_pos[uid])
            self.test_history_pos = defaultdict(set)
            for uid in data_loader.test_user_pos.keys():
                self.test_history_pos[uid] = set(data_loader.test_user_pos[uid])

            self.train_history_neg = defaultdict(set)
            for uid in data_loader.train_user_neg.keys():
                self.train_history_neg[uid] = set(data_loader.train_user_neg[uid])
            self.validation_history_neg = defaultdict(set)
            for uid in data_loader.validation_user_neg.keys():
                self.validation_history_neg[uid] = set(data_loader.validation_user_neg[uid])
            self.test_history_neg = defaultdict(set)
            for uid in data_loader.test_user_neg.keys():
                self.test_history_neg[uid] = set(data_loader.test_user_neg[uid])
        self.vt_batches_buffer = {}

    def get_train_data(self, epoch, model):
        """
        convert training dataframe in dataloader into dict, shuffle every epoch
        the dict will be used for generating batches
        :param epoch: no shuffle if <0
        :param model: Model
        :return: dict
        """
        if self.train_data is None:
            logging.info('Prepare Train Data...')
            self.train_data = self.format_data_dict(self.data_loader.train_df, model)
            self.train_data[SAMPLE_ID] = np.arange(0, len(self.train_data[Y]))
        if epoch >= 0:
            utils.shuffle_in_unison_scary(self.train_data)
        return self.train_data

    def get_validation_data(self, model):
        """
        convert validation dataframe in dataloader into dict
        assign test_sample_n negative samples for each positive in topn recommendation task
        the dict will be used for generating batches
        :param epoch: no shuffle if <0
        :param model: Model
        :return: dict
        """
        if self.validation_data is None:
            logging.info('Prepare Validation Data...')
            df = self.data_loader.validation_df
            if self.rank == 1:
                tmp_df = df.rename(columns={self.data_loader.label: Y})
                tmp_df = tmp_df.drop(tmp_df[tmp_df[Y] <= 0].index)
                neg_df = self.generate_neg_df(
                    inter_df=tmp_df, feature_df=df, sample_n=self.test_sample_n, train=False)
                df = pd.concat([df, neg_df], ignore_index=True)
            self.validation_data = self.format_data_dict(df, model)
            self.validation_data[SAMPLE_ID] = np.arange(0, len(self.validation_data[Y]))
        return self.validation_data

    def get_test_data(self, model):
        """
        convert test dataframe in dataloader into dict
        assign test_sample_n negative samples for each positive in topn recommendation task
        the dict will be used for generating batches
        :param epoch: no shuffle if <0
        :param model: Model
        :return: dict
        """
        if self.test_data is None:
            logging.info('Prepare Test Data...')
            df = self.data_loader.test_df
            if self.rank == 1 and self.unlabel_test == 0:
                tmp_df = df.rename(columns={self.data_loader.label: Y})
                tmp_df = tmp_df.drop(tmp_df[tmp_df[Y] <= 0].index)
                neg_df = self.generate_neg_df(
                    inter_df=tmp_df, feature_df=df, sample_n=self.test_sample_n, train=False)
                neg_df = self.drop_duplicate(neg_df, self.test_sample_n) #only keep test_sample_n negative samples for each user

                df = pd.concat([df, neg_df], ignore_index=True)
            self.test_data = self.format_data_dict(df, model)
            self.test_data[SAMPLE_ID] = np.arange(0, len(self.test_data[Y]))
        return self.test_data

    def drop_duplicate(self, neg_df, test_sample_n):
        neg_df = shuffle(neg_df)
        return neg_df.groupby(UID).head(test_sample_n).reset_index(drop=True)

    def get_feed_dict(self, data, batch_start, batch_size, train, neg_data=None, special_cols=None):
        """
        topn model generate a batch, if train, assign a negative sample for each positive sample, make sure first half is positive and second half is negative
        :param data: data dict，generated by self.get_*_data() and self.format_data_dict()
        :param batch_start: batch start index
        :param batch_size: batch size
        :param train: train or evaluation
        :param neg_data: data dict for negative samples，directly use it if exists
        :param special_cols: columns which need special operations
        :return: feed dict of the batch
        """
        # if validation for test, negative samples are already sampled
        total_data_num = len(data[SAMPLE_ID])
        batch_end = min(len(data[self.data_columns[0]]), batch_start + batch_size)
        real_batch_size = batch_end - batch_start
        total_batch_size = real_batch_size * (self.train_sample_n + 1) if self.rank == 1 and train else real_batch_size
        feed_dict = {TRAIN: train, RANK: self.rank, REAL_BATCH_SIZE: real_batch_size,
                     TOTAL_BATCH_SIZE: total_batch_size}
        if Y in data:
            feed_dict[Y] = utils.numpy_to_torch(data[Y][batch_start:batch_start + real_batch_size], gpu=False)
        for c in self.info_columns + self.data_columns:
            if c not in data or data[c].size <= 0:
                continue
            d = data[c][batch_start: batch_start + real_batch_size]
            if self.rank == 1 and train:
                neg_d = np.concatenate(
                    [neg_data[c][total_data_num * i + batch_start: total_data_num * i + batch_start + real_batch_size]
                     for i in range(self.train_sample_n)])
                d = np.concatenate([d, neg_d])
            feed_dict[c] = d
        for c in self.data_columns:
            if c not in feed_dict:
                continue
            if special_cols is not None and c in special_cols:
                continue
            feed_dict[c] = utils.numpy_to_torch(feed_dict[c], gpu=False)
        return feed_dict

    def _check_vt_buffer(self, data, batch_size, train, model):
        buffer_key = ''
        if data is self.train_data and not train:
            buffer_key = '_'.join(['train', str(batch_size), str(model)])
        elif data is self.validation_data:
            buffer_key = '_'.join(['validation', str(batch_size), str(model)])
        elif data is self.test_data:
            buffer_key = '_'.join(['test', str(batch_size), str(model)])
        if buffer_key != '' and buffer_key in self.vt_batches_buffer:
            return self.vt_batches_buffer[buffer_key]
        return buffer_key

    def prepare_batches(self, data, batch_size, train, model):
        """
        convert data dict into batch
        :param data: dict, generate by self.get_*_data() and self.format_data_dict()
        :param batch_size: batch size
        :param train: train or not
        :param model: Model class
        :return: list of batches
        """

        buffer_key = self._check_vt_buffer(data=data, batch_size=batch_size, train=train, model=model)
        if type(buffer_key) != str:
            return buffer_key

        if data is None:
            return None
        num_example = len(data[Y])
        total_batch = int((num_example + batch_size - 1) / batch_size)
        assert num_example > 0
        # if train, one negative sample for each positive sample
        neg_data = None
        if train and self.rank == 1:
            neg_data = self.generate_neg_data(
                data, self.data_loader.train_df, sample_n=self.train_sample_n,
                train=True, model=model)
        batches = []
        for batch in tqdm(range(total_batch), leave=False, ncols=100, mininterval=1, desc='Prepare Batches'):
            batches.append(self.get_feed_dict(data=data, batch_start=batch * batch_size, batch_size=batch_size,
                                              train=train, neg_data=neg_data))

        if buffer_key != '':
            self.vt_batches_buffer[buffer_key] = batches
        return batches

    def format_data_dict(self, df, model):
        """
        deal with history interaction except uid,iid,label,user、item、context features
        :param df: train、validation、test df
        :param model: Model class
        :return:
        """

        data_loader = self.data_loader
        data = {}
        # record uid, iid
        out_columns = []
        if UID in df:
            out_columns.append(UID)
            data[UID] = df[UID].values
        if IID in df:
            out_columns.append(IID)
            data[IID] = df[IID].values
        if TIME in df:
            data[TIME] = df[TIME].values

        # record label into Y
        if data_loader.label in df.columns:
            data[Y] = np.array(df[data_loader.label], dtype=np.float32)
        else:
            logging.warning('No Labels In Data: ' + data_loader.label)
            data[Y] = np.zeros(len(df), dtype=np.float32)

        ui_id = df[out_columns]

        # concatenate user feature and item features based on uid, iid
        out_df = ui_id
        if data_loader.user_df is not None and model.include_user_features:
            out_df = pd.merge(out_df, data_loader.user_df, on=UID, how='left')
        if data_loader.item_df is not None and model.include_item_features:
            out_df = pd.merge(out_df, data_loader.item_df, on=IID, how='left')

        # whether contain context feature
        if model.include_context_features and len(data_loader.context_features) > 0:
            context = df[data_loader.context_features]
            out_df = pd.concat([out_df, context], axis=1, ignore_index=True)
        out_df = out_df.fillna(0)

        # if uid and iid are special, do not convert to multi-hot vectors as other features\
        if not model.include_id:
            out_df = out_df.drop(columns=out_columns)

        base = 0
        for feature in out_df.columns:
            out_df[feature] = out_df[feature].apply(lambda x: x + base)
            base += int(data_loader.column_max[feature] + 1)

        # if model requires，concatenate uid,iid before x
        data[X] = out_df.values.astype(int)
        assert len(data[X]) == len(data[Y])
        return data

    def generate_neg_data(self, data, feature_df, sample_n, train, model):
        """
        generate neg_data dict, use when prepare_batches_rk train=True
        :param data:
        :param feature_df:
        :param sample_n:
        :param train:
        :param model:
        :return:
        """
        inter_df = pd.DataFrame()
        for c in [UID, IID, Y, TIME]:
            if c in data:
                inter_df[c] = data[c]
            else:
                assert c == TIME
        neg_df = self.generate_neg_df(
            inter_df=inter_df, feature_df=feature_df,
            sample_n=sample_n, train=train)
        neg_data = self.format_data_dict(neg_df, model)
        neg_data[SAMPLE_ID] = np.arange(0, len(neg_data[Y])) + len(data[SAMPLE_ID])
        return neg_data

    def generate_neg_df(self, inter_df, feature_df, sample_n, train):
        """
        generate negative samples based on uid,iid and training or validation/test dataframe
        :param sample_n: negative samples number
        :param train: train or not
        :return:
        """
        other_columns = [c for c in inter_df.columns if c not in [UID, Y]]
        neg_df = self._sample_neg_from_uid_list(
            uids=inter_df[UID].tolist(), labels=inter_df[Y].tolist(), sample_n=sample_n, train=train,
            other_infos=inter_df[other_columns].to_dict('list'))
        neg_df = pd.merge(neg_df, feature_df, on=[UID] + other_columns, how='left')
        neg_df = neg_df.drop(columns=[IID])
        neg_df = neg_df.rename(columns={'iid_neg': IID})
        neg_df = neg_df[feature_df.columns]
        neg_df[self.data_loader.label] = 0
        return neg_df

    def _sample_neg_from_uid_list(self, uids, labels, sample_n, train, other_infos=None):
        """
        negative sampling based on list of user
        :param uids: uid list
        :param sample_n: number of samples for each uid
        :param train: train or not
        :param other_infos: other infomation that require copy except uid,iid,label，e.g. history
        :return: DataFrame，but require to be data dict generated by self.format_data_dict()
        """
        if other_infos is None:
            other_infos = {}
        iid_list = []

        other_info_list = {}
        for info in other_infos:
            other_info_list[info] = []

        # record iid
        item_num = self.data_loader.item_num
        for index, uid in enumerate(uids):
            if labels[index] > 0:
                # record positive samples
                train_history = self.train_history_pos
                validation_history, test_history = self.validation_history_pos, self.test_history_pos
                known_train = self.train_history_neg
            else:
                assert train
                # record negative samples
                train_history = self.train_history_neg
                validation_history, test_history = self.validation_history_neg, self.test_history_neg
                known_train = self.train_history_pos
            if train:
                # avoid getting known positive or negative for training set
                inter_iids = train_history[uid]
            else:
                # avoid getting known positive or negative for non-training set
                inter_iids = train_history[uid] | validation_history[uid] | test_history[uid]

            # check remaining number
            remain_iids_num = item_num - len(inter_iids)
            # report if no enough available items
            assert remain_iids_num >= sample_n

            # if remain_iids_num is small, list all available items and use np.choice
            remain_iids = None
            if 1.0 * remain_iids_num / item_num < 0.2:
                remain_iids = [i for i in range(1, item_num) if i not in inter_iids]

            sampled = set()
            if remain_iids is None:
                unknown_iid_list = []
                for i in range(sample_n):
                    iid = np.random.randint(1, self.data_loader.item_num)
                    while iid in inter_iids or iid in sampled:
                        iid = np.random.randint(1, self.data_loader.item_num)
                    unknown_iid_list.append(iid)
                    sampled.add(iid)
            else:
                unknown_iid_list = np.random.choice(remain_iids, sample_n, replace=False)

            # if train, sample from known negative samples or positive samples
            if train and self.sample_un_p < 1:
                known_iid_list = list(np.random.choice(
                    list(known_train[uid]), min(sample_n, len(known_train[uid])), replace=False)) \
                    if len(known_train[uid]) != 0 else []
                known_iid_list = known_iid_list + unknown_iid_list
                tmp_iid_list = []
                sampled = set()
                for i in range(sample_n):
                    p = np.random.rand()
                    if p < self.sample_un_p or len(known_iid_list) == 0:
                        iid = unknown_iid_list.pop(0)
                        while iid in sampled:
                            iid = unknown_iid_list.pop(0)
                    else:
                        iid = known_iid_list.pop(0)
                        while iid in sampled:
                            iid = known_iid_list.pop(0)
                    tmp_iid_list.append(iid)
                    sampled.add(iid)
                iid_list.append(tmp_iid_list)
            else:
                iid_list.append(unknown_iid_list)

        all_uid_list, all_iid_list = [], []
        for i in range(sample_n):
            for index, uid in enumerate(uids):
                all_uid_list.append(uid)
                all_iid_list.append(iid_list[index][i])
                # # copy other info
                for info in other_infos:
                    other_info_list[info].append(other_infos[info][index])

        neg_df = pd.DataFrame(data=list(zip(all_uid_list, all_iid_list)), columns=[UID, 'iid_neg'])
        for info in other_infos:
            neg_df[info] = other_info_list[info]
        return neg_df


from recognizers_text.matcher.string_matcher import StringMatcher
from recognizers_text.matcher.match_result import MatchResult

class TestStringMatcher:

    @staticmethod
    def test_simple_string_matcher():
        values = ["China", "Beijing", "City"]
        string_matcher = StringMatcher()
        string_matcher.init(values)
        for value in values:
            match = string_matcher.find(value)
            if match is not None:
                assert value == match[0].text

    @staticmethod
    def test_simple_with_ids_string_matcher():
        values = ["China", "Beijing", "City"]
        ids = ["1", "2", "3"]
        string_matcher = StringMatcher()
        string_matcher.init(values, ids)
        for i in range(0, len(values)):
            value = values[i]
            match = string_matcher.find(value)
            if match is not None:
                assert value == match[0].text
                assert ids[i] == match[0].canonical_values[0]

    @staticmethod
    def test_string_matcher():
        utc_8_value = 'UTC+08:00'
        utc_8_words = ['beijing time', 'chongqing time', 'hong kong time', 'urumqi time']
        utc_2_value = 'UTC+02:00'
        utc_2_words = ['cairo time', 'beirut time', 'gaza time', 'amman time']
        value_dictionary = {utc_2_value: utc_2_words, utc_8_value: utc_8_words}
        string_matcher = StringMatcher()
        string_matcher.init(value_dictionary)

        for value in utc_8_words:
            sentence = 'please change {}, thanks'.format(value)
            matches: [MatchResult] = string_matcher.find(sentence)
            assert value == matches[0].text
            assert utc_8_value == matches[0].canonical_values[0]
            assert 14 == matches[0].start

        for value in utc_2_words:
            sentence = 'please change {}, thanks'.format(value)
            matches: [MatchResult] = string_matcher.find(sentence)
            assert value == matches[0].text
            assert str(utc_2_value) == matches[0].canonical_values[0]
            assert 14 == matches[0].start


from lector.log import CONSOLE

def equal(obj1, obj2, extra=None):
    """Helper to print useful info if result is unexpected."""
    eq = obj1 == obj2

    if not eq:
        CONSOLE.print(obj1)
        CONSOLE.print(obj2)

        if extra is not None:
            CONSOLE.print(extra)

        return False

    return True


from rlbot.agents.base_agent import SimpleControllerState

from predict import next_ball_landing
from states.atba import GoToPointState
from util.rlmath import lerp
from util.vec import norm, Vec3

class GrabBallState(GoToPointState):
    def __init__(self):
        super().__init__(allow_slide=True, boost_min=20)

    def exec(self, bot) -> SimpleControllerState:

        car = bot.data.my_car
        ball = bot.data.ball
        teammate = None
        if len(bot.data.teammates) > 0:
            teammate = bot.data.teammates[0]

        if teammate is not None:
            my_dist = norm(car.pos - ball.pos)
            teammate_dist = norm(teammate.pos - ball.pos)

            if my_dist < teammate_dist:
                return self.go_for_ball(bot)
            else:
                return self.approach_defensively(bot)

        else:
            land_event = next_ball_landing(bot)

            if land_event.happens:
                ball_at_landing = land_event.data["obj"]

                dist = norm(car.pos - ball_at_landing.pos)

                self.target = ball_at_landing.pos
                self.target_vel = dist / land_event.time

            else:
                self.target = ball.pos
                self.target_vel = 1400

        return super().exec(bot)

    def approach_defensively(self, bot) -> SimpleControllerState:
        car = bot.data.my_car
        ball = bot.data.ball
        goal = Vec3(y=5400 * bot.data.team_sign)

        self.target = lerp(ball.pos, goal, 0.8)
        self.target_vel = norm(self.target - car.pos)

        return super().exec(bot)

    def go_for_ball(self, bot) -> SimpleControllerState:
        car = bot.data.my_car
        ball = bot.data.ball
        land_event = next_ball_landing(bot)

        if land_event.happens:
            ball_at_landing = land_event.data["obj"]

            dist = norm(car.pos - ball_at_landing.pos)

            self.target = ball_at_landing.pos
            self.target_vel = dist / land_event.time

        else:
            self.target = ball.pos
            self.target_vel = 1400

        return super().exec(bot)


from django.test import TestCase

from rest_framework.test import APIClient
from rest_framework.authtoken.models import Token

from .models import Reportes, Motivos

from ofertas.models import Ofertas

from users.models import User

from estabelecimentos.models import Estabelecimentos

from freelancers.models import FreeLancers

class TestReportes(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.writer = User.objects.create_user(
            'test_user',
            'test@example.com',
            'password1'
        )
        self.token = Token.objects.create(user=self.writer)

        self.writer2 = User.objects.create_user(
            'test_user2',
            'luizdendena@trampoja.com',
            'password1'
        )
        self.token2 = Token.objects.create(user=self.writer2)

        self.writer3 = User.objects.create_user(
            'test_user3',
            'test@trampoja.com',
            'password1'
        )
        self.token3 = Token.objects.create(user=self.writer3)

        self.oferta = Ofertas(
            nome='Oferta Teste',
            valor=80,
            owner=self.writer,
            closed = True
        )
        self.oferta.save()

        self.oferta2 = Ofertas(
            nome='Oferta Teste',
            valor=80,
            owner=self.writer,
            closed = False
        )
        self.oferta2.save()

        self.estabelecimento = Estabelecimentos(
            nome='Empresa Teste',
            cnpj='09992622970',
            razao_social='TESTE',
            tipo='bodega',
            telefone='049999950411',
            owner=self.writer
        )
        self.estabelecimento.save()

        self.freelancer = FreeLancers(
            nome='Test',
            sobrenome='Freela Teste',
            telefone='499999500411',
            nascimento='1997-05-25',
            bio='Piao trabaiado',
            owner=self.writer2
        )
        self.freelancer.save()

        self.freelancer2 = FreeLancers(
            nome='Test',
            sobrenome='Freela Teste',
            telefone='499999500411',
            nascimento='1997-05-25',
            bio='Piao trabaiado',
            owner=self.writer3
        )
        self.freelancer2.save()

        self.reporte = Reportes(
            freelancer=self.freelancer2,
            trampo=self.oferta2,
            descricao='Teste'
        )
        self.reporte.save()

        self.motivo = Motivos(
            motivo=1,
            nome='Motivo Teste'
        )
        self.motivo.save()

        self.motivo2 = Motivos(
            motivo=2,
            nome='Motivo2 Teste'
        )
        self.motivo2.save()

class TestCreateReportesView(TestReportes):
    def test_create_reporte_post_sucess(self):
        data = {
            'freelancer': self.freelancer.id,
            'oferta': self.oferta.id,
            'descricao': 'Descricao teste',
            'motivos': {
                1: True,
                2: True
            }
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
        response = self.client.post("/reportar", format='json', data=data)
        self.assertEqual(response.status_code, 201)

    def test_create_reporte_post_error_unique(self):
        data = {
            'freelancer': self.freelancer2.id,
            'oferta': self.oferta2.id,
            'descricao': 'Descricao teste',
            'motivos': {
                1: True,
                2: True
            }
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
        response = self.client.post("/reportar", format='json', data=data)
        self.assertEqual(response.status_code, 400)

    def test_create_reporte_post_error_oferta_not_closed(self):
        data = {
            'freelancer': self.freelancer.id,
            'oferta': self.oferta2.id,
            'descricao': 'Descricao teste',
            'motivos': {
                1: True,
                2: True
            }
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
        response = self.client.post("/reportar", format='json', data=data)
        self.assertEqual(response.status_code, 400)

class TestRepostesGetView(TestReportes):
    def test_get_reportes(self):
        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
        response = self.client.get("/reportes/1")
        self.assertEqual(response.status_code, 200)