from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import warnings

import numpy as np
from skimage.external import tifffile as tiff
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import Model

from deepcell.utils.data_utils import trim_padding
from deepcell.utils.io_utils import get_images_from_directory

def get_cropped_input_shape(images, num_crops=4, receptive_field=61, data_format=None):
    """Calculate the input_shape for models to process cropped sub-images.

    Args:
        images: numpy array of original data
        num_crops: number of slices for the x and y axis to create sub-images

    Returns:
        input_shape: new input_shape for model to process sub-images.
    """
    if data_format is None:
        data_format = K.image_data_format()
    if data_format == 'channels_first':
        channel_axis = 1
        row_axis = len(images.shape) - 2
        col_axis = len(images.shape) - 1
    else:
        channel_axis = len(images.shape) - 1
        row_axis = len(images.shape) - 3
        col_axis = len(images.shape) - 2

    channel_dim = images.shape[channel_axis]

    # Split the frames into quarters, as the full image size is too large
    crop_x = images.shape[row_axis] // num_crops + (receptive_field - 1)
    crop_y = images.shape[col_axis] // num_crops + (receptive_field - 1)

    if images.ndim == 5:
        input_shape = (images.shape[row_axis - 1], crop_x, crop_y, channel_dim)
    else:
        input_shape = (crop_x, crop_y, channel_dim)

    # switch to channels_first if necessary
    if channel_axis == 1:
        input_shape = (input_shape[-1], *input_shape[:-1])

    return input_shape

def get_padding_layers(model):
    """Get all names of padding layers in a model

    Args:
        model: Keras model

    Returns:
        padding_layers: list of names of padding layers inside model
    """
    padding_layers = []
    for layer in model.layers:
        if 'padding' in layer.name:
            padding_layers.append(layer.name)
        elif isinstance(layer, Model):
            padding_layers.extend(get_padding_layers(layer))
    return padding_layers

def process_whole_image(model, images, num_crops=4, receptive_field=61, padding=None):
    """Slice images into num_crops * num_crops pieces, and use the model to
    process each small image.

    Args:
        model: model that will process each small image
        images: numpy array that is too big for model.predict(images)
        num_crops: number of slices for the x and y axis to create sub-images
        receptive_field: receptive field used by model, required to pad images
        padding: type of padding for input images, one of {'reflect', 'zero'}

    Returns:
        model_output: numpy array containing model outputs for each sub-image
    """
    if K.image_data_format() == 'channels_first':
        channel_axis = 1
        row_axis = len(images.shape) - 2
        col_axis = len(images.shape) - 1
    else:
        channel_axis = len(images.shape) - 1
        row_axis = len(images.shape) - 3
        col_axis = len(images.shape) - 2

    if not padding:
        padding_layers = get_padding_layers(model)
        if padding_layers:
            padding = 'reflect' if 'reflect' in padding_layers[0] else 'zero'

    if str(padding).lower() not in {'reflect', 'zero'}:
        raise ValueError('Expected `padding_mode` to be either `zero` or '
                         '`reflect`.  Got ', padding)

    # Split the frames into quarters, as the full image size is too large
    crop_x = images.shape[row_axis] // num_crops
    crop_y = images.shape[col_axis] // num_crops

    # Set up receptive field window for padding
    win_x, win_y = (receptive_field - 1) // 2, (receptive_field - 1) // 2

    # instantiate matrix for model output
    model_output_shape = tuple(list(model.layers[-1].output_shape)[1:])
    if channel_axis == 1:
        output = np.zeros((images.shape[0], model_output_shape[1], *images.shape[2:]))
    else:
        output = np.zeros((*images.shape[0:-1], model_output_shape[-1]))

    expected_input_shape = get_cropped_input_shape(images, num_crops, receptive_field)
    if expected_input_shape != model.input_shape[1:]:
        raise ValueError('Expected model.input_shape to be {}. Got {}.  Use '
                         '`get_cropped_input_shape()` to recreate your model '
                         ' with the proper input_shape'.format(
                             expected_input_shape, model.input_shape[1:]))

    # pad the images only in the x and y axes
    pad_width = []
    for i in range(len(images.shape)):
        if i == row_axis:
            pad_width.append((win_x, win_x))
        elif i == col_axis:
            pad_width.append((win_y, win_y))
        else:
            pad_width.append((0, 0))

    if str(padding).lower() == 'reflect':
        padded_images = np.pad(images, pad_width, mode='reflect')
    else:
        padded_images = np.pad(images, pad_width, mode='constant', constant_values=0)

    for i in range(num_crops):
        for j in range(num_crops):
            e, f = i * crop_x, (i + 1) * crop_x + 2 * win_x
            g, h = j * crop_y, (j + 1) * crop_y + 2 * win_y

            if images.ndim == 5:
                if channel_axis == 1:
                    predicted = model.predict(padded_images[:, :, :, e:f, g:h])
                else:
                    predicted = model.predict(padded_images[:, :, e:f, g:h, :])
            else:
                if channel_axis == 1:
                    predicted = model.predict(padded_images[:, :, e:f, g:h])
                else:
                    predicted = model.predict(padded_images[:, e:f, g:h, :])

            # if using skip_connections, get the final model output
            if isinstance(predicted, list):
                predicted = predicted[-1]

            # if the model uses padding, trim the output images to proper shape
            # if model does not use padding, images should already be correct
            if padding:
                predicted = trim_padding(predicted, win_x, win_y)

            a, b = i * crop_x, (i + 1) * crop_x
            c, d = j * crop_y, (j + 1) * crop_y

            if images.ndim == 5:
                if channel_axis == 1:
                    output[:, :, :, a:b, c:d] = predicted
                else:
                    output[:, :, a:b, c:d, :] = predicted
            else:
                if channel_axis == 1:
                    output[:, :, a:b, c:d] = predicted
                else:
                    output[:, a:b, c:d, :] = predicted

    return output

def run_model(image, model, win_x=30, win_y=30, split=True):
    """Runs the chosen model.

    Args:
        model: model that will process each small image
        image: numpy array that is too big for model.predict(images)

    Returns:
        model_output: numpy array containing model outputs for each sub-image
    """
    # pad_width = ((0, 0), (0, 0), (win_x, win_x), (win_y, win_y))
    # image = np.pad(image, pad_width=pad_width , mode='constant', constant_values=0)
    is_channels_first = K.image_data_format() == 'channels_first'
    channel_axis = 1 if is_channels_first else -1
    x_axis = 2 if is_channels_first else 1
    y_axis = 3 if is_channels_first else 2

    n_features = model.layers[-1].output_shape[channel_axis]

    if split:
        warnings.warn('The split flag is deprecated and is designed to account '
                      'for a maximum tensor size.')

        image_size_x = image.shape[x_axis] // 2
        image_size_y = image.shape[y_axis] // 2

        if is_channels_first:
            shape = (n_features, 2 * image_size_x - win_x * 2, 2 * image_size_y - win_y * 2)
        else:
            shape = (2 * image_size_x - win_x * 2, 2 * image_size_y - win_y * 2, n_features)

        model_output = np.zeros(shape, dtype=K.floatx())

        if is_channels_first:
            img_0 = image[:, :, 0:image_size_x + win_x, 0:image_size_y + win_y]
            img_1 = image[:, :, 0:image_size_x + win_x, image_size_y - win_y:]
            img_2 = image[:, :, image_size_x - win_x:, 0:image_size_y + win_y]
            img_3 = image[:, :, image_size_x - win_x:, image_size_y - win_y:]

            model_output[:, 0:image_size_x - win_x, 0:image_size_y - win_y] = model.predict(img_0)
            model_output[:, 0:image_size_x - win_x, image_size_y - win_y:] = model.predict(img_1)
            model_output[:, image_size_x - win_x:, 0:image_size_y - win_y] = model.predict(img_2)
            model_output[:, image_size_x - win_x:, image_size_y - win_y:] = model.predict(img_3)
        else:
            img_0 = image[:, 0:image_size_x + win_x, 0:image_size_y + win_y, :]
            img_1 = image[:, 0:image_size_x + win_x, image_size_y - win_y:, :]
            img_2 = image[:, image_size_x - win_x:, 0:image_size_y + win_y, :]
            img_3 = image[:, image_size_x - win_x:, image_size_y - win_y:, :]

            model_output[0:image_size_x - win_x, 0:image_size_y - win_y, :] = model.predict(img_0)
            model_output[0:image_size_x - win_x, image_size_y - win_y:, :] = model.predict(img_1)
            model_output[image_size_x - win_x:, 0:image_size_y - win_y, :] = model.predict(img_2)
            model_output[image_size_x - win_x:, image_size_y - win_y:, :] = model.predict(img_3)

    else:
        model_output = model.predict(image)
        model_output = model_output[0, :, :, :]

    return model_output

def run_model_on_directory(data_location, channel_names, output_location, model,
                           win_x=30, win_y=30, split=True, save=True):

    is_channels_first = K.image_data_format() == 'channels_first'
    channel_axis = 1 if is_channels_first else -1
    n_features = model.layers[-1].output_shape[channel_axis]

    image_list = get_images_from_directory(data_location, channel_names)
    model_outputs = []

    for i, image in enumerate(image_list):
        print('Processing image {} of {}'.format(i + 1, len(image_list)))
        model_output = run_model(image, model, win_x=win_x, win_y=win_y, split=split)
        model_outputs.append(model_output)

        # Save images
        if save:
            for f in range(n_features):
                feature = model_output[f, :, :] if is_channels_first else model_output[:, :, f]
                cnnout_name = 'feature_{}_frame_{}.tif'.format(f, str(i).zfill(3))
                tiff.imsave(os.path.join(output_location, cnnout_name), feature)

    return model_outputs

def run_models_on_directory(data_location, channel_names, output_location, model_fn,
                            list_of_weights, n_features=3, win_x=30, win_y=30,
                            image_size_x=1080, image_size_y=1280, save=True, split=True):
    if split:
        input_shape = (len(channel_names), image_size_x // 2 + win_x, image_size_y // 2 + win_y)
    else:
        input_shape = (len(channel_names), image_size_x, image_size_y)

    is_channels_first = K.image_data_format() == 'channels_first'
    if not is_channels_first:
        input_shape = (input_shape[1], input_shape[2], input_shape[0])
        batch_shape = (1, input_shape[1], input_shape[2], input_shape[0])
    else:
        batch_shape = (1, input_shape[0], input_shape[1], input_shape[2])

    model = model_fn(input_shape=input_shape, n_features=n_features)

    for layer in model.layers:
        print(layer.name)

    channel_axis = 1 if is_channels_first else -1
    n_features = model.layers[-1].output_shape[channel_axis]

    model_outputs = []
    for weights_path in list_of_weights:
        model.load_weights(weights_path)
        processed_image_list = run_model_on_directory(
            data_location, channel_names, output_location, model,
            win_x=win_x, win_y=win_y, save=False, split=split)

        model_outputs.append(np.stack(processed_image_list, axis=0))

    # Average all images
    model_output = np.stack(model_outputs, axis=0)
    model_output = np.mean(model_output, axis=0)

    # Save images
    if save:
        for i in range(model_output.shape[0]):
            for f in range(n_features):
                if is_channels_first:
                    feature = model_output[i, f, :, :]
                else:
                    feature = model_output[i, :, :, f]
                cnnout_name = 'feature_{}_frame_{}.tif'.format(f, i)
                tiff.imsave(os.path.join(output_location, cnnout_name), feature)

    return model_output


from django.conf import settings
from django.db import models

from .recipes import Recipe

class FavoriteRecipe(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name='Пользователь',
        on_delete=models.CASCADE,
        related_name='favorite_recipes',
        help_text='Тот кто включает в избранное',
    )
    recipe = models.ForeignKey(
        Recipe,
        verbose_name='Рецепт',
        on_delete=models.CASCADE,
        related_name='favorite_authors',
        help_text='Добавленный в избранное рецепт'
    )

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=['user', 'recipe'],
                name='unique_favorite_recipe'
            ),
        ]
        verbose_name = 'Избранный рецепт'
        verbose_name_plural = 'Избранные рецепты'
        ordering = ['user']


import collections

Item = collections.namedtuple('Item', ['weight', 'value'])

"""
Complexities analysis:
 * Runtime O(n * W), n - number of elements in knapsack, W - weight limit
 * Space O(n), n - number of elements in knapsack.
"""
def max_value(knapsack_max_weight, items):
    """
    Get the maximum value of the knapsack.
    """
    values = [0 for _ in range(knapsack_max_weight+1)]
    for item in items:
        for weight in range(knapsack_max_weight, item.weight-1, -1):
            values[weight] = max(values[weight], values[weight - item.weight] + item.value)
    return values[-1]

tests = [
    {
        'correct_output': 14,
        'input':
            {
                'knapsack_max_weight': 15,
                'items': [Item(10, 7), Item(9, 8), Item(5, 6)]}},
    {
        'correct_output': 13,
        'input':
            {
                'knapsack_max_weight': 25,
                'items': [Item(10, 2), Item(29, 10), Item(5, 7), Item(5, 3), Item(5, 1), Item(24, 12)]}}]
for test in tests:
    assert test['correct_output'] == max_value(**test['input'])


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

from click.testing import CliRunner
import os
import json
import pathlib
import tempfile
import pytest
import nibabel as nib
import numpy as np
from shutil import copyfile

from shimmingtoolbox.cli.prepare_fieldmap import prepare_fieldmap_cli
from shimmingtoolbox import __dir_testing__
from shimmingtoolbox.load_nifti import read_nii

PHASE_SCALING_SIEMENS = 4095
fname_phasediff = os.path.join(__dir_testing__, 'ds_b0', 'sub-realtime', 'fmap', 'sub-realtime_phasediff.nii.gz')
fname_mag_realtime = os.path.join(__dir_testing__, 'ds_b0', 'sub-realtime', 'fmap', 'sub-realtime_magnitude1.nii.gz')
fname_mag_fieldmap = os.path.join(__dir_testing__, 'ds_b0', 'sub-fieldmap', 'fmap', 'sub-fieldmap_magnitude1.nii.gz')
fname_phase1 = os.path.join(__dir_testing__, 'ds_b0', 'sub-fieldmap', 'fmap', 'sub-fieldmap_phase1.nii.gz')
fname_phase2 = os.path.join(__dir_testing__, 'ds_b0', 'sub-fieldmap', 'fmap', 'sub-fieldmap_phase2.nii.gz')

def test_cli_prepare_fieldmap_1_echo():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_phasediff,
                                                      '--mag', fname_mag_realtime,
                                                      '--unwrapper', 'skimage',
                                                      '--output', fname_output], catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

def test_cli_prepare_fieldmap_2_echos():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_phase1, fname_phase2,
                                                      '--mag', fname_mag_fieldmap,
                                                      '--unwrapper', 'skimage',
                                                      '--output', fname_output],
                               catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

def test_cli_prepare_fieldmap_3_echos():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')
        nii1 = read_nii(fname_phase1)[0]
        nii2 = read_nii(fname_phase2)[0]
        data3 = nii1.get_fdata() + nii2.get_fdata()
        data3 = np.angle(np.exp(1j * data3))
        nii3 = nib.Nifti1Image(data3, nii1.affine, header=nii1.header)
        fname_phase3 = os.path.join(tmp, "echo3.nii.gz")
        nib.save(nii3, fname_phase3)

        # Fix the json
        # Load the json file
        json_path1 = fname_phase1.split('.nii')[0] + '.json'
        json_path2 = fname_phase2.split('.nii')[0] + '.json'
        with open(json_path1) as json_file:
            json_1 = json.load(json_file)
        with open(json_path2) as json_file:
            json_2 = json.load(json_file)

        # Change EchoNumber to 3
        json_1["EchoNumber"] = 3
        # Change echo time
        te1 = json_1["EchoTime"]
        te2 = json_2["EchoTime"]
        te3 = te1 + te2
        json_1["EchoTime"] = te3
        # Save a new json file
        fname_json3 = fname_phase3.rsplit('.nii', 1)[0] + '.json'
        with open(fname_json3, 'w') as outfile:
            json.dump(json_1, outfile, indent=2)

        result = runner.invoke(prepare_fieldmap_cli, [fname_phase1, fname_phase2, fname_phase3,
                                                      '--mag', fname_mag_fieldmap,
                                                      '--unwrapper', 'skimage',
                                                      '--output', fname_output], catch_exceptions=False)
        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

def test_cli_prepare_fieldmap_default_output():
    runner = CliRunner()

    result = runner.invoke(prepare_fieldmap_cli, [fname_phase1, fname_phase2,
                                                  '--mag', fname_mag_fieldmap,
                                                  '--unwrapper', 'skimage'],
                           catch_exceptions=False)

    assert result.exit_code == 0
    assert os.path.isfile(os.path.join(os.curdir, 'fieldmap.nii.gz'))
    assert os.path.isfile(os.path.join(os.curdir, 'fieldmap.json'))
    os.remove(os.path.join(os.curdir, 'fieldmap.nii.gz'))
    os.remove(os.path.join(os.curdir, 'fieldmap.json'))

def test_cli_prepare_fieldmap_gaussian():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_phasediff,
                                                      '--mag', fname_mag_realtime,
                                                      '--unwrapper', 'skimage',
                                                      '--output', fname_output,
                                                      '--gaussian-filter', 'True',
                                                      '--sigma', 1],
                               catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

def test_cli_prepare_fieldmap_autoscale():
    """Tests the CLI with autoscale False"""
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        # Rescale input data to -pi to pi and save in tmp directory
        nii_phasediff = nib.load(fname_phasediff)
        phasediff = nii_phasediff.get_fdata()
        rescaled = phasediff * (2 * np.pi / PHASE_SCALING_SIEMENS) - np.pi
        nii_rescaled = nib.Nifti1Image(rescaled, nii_phasediff.affine, header=nii_phasediff.header)
        fname_rescaled = os.path.join(tmp, 'rescaled_phasediff.nii.gz')
        nib.save(nii_rescaled, fname_rescaled)

        # Copy the json file next to the new phase data (required by read_nii)
        fname_json = os.path.join(__dir_testing__, 'ds_b0', 'sub-realtime', 'fmap', 'sub-realtime_phasediff.json')
        fname_json_tmp = os.path.join(tmp, 'rescaled_phasediff.json')
        copyfile(fname_json, fname_json_tmp)

        # Set up other paths
        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_rescaled,
                                                      '--mag', fname_mag_realtime,
                                                      '--unwrapper', 'skimage',
                                                      '--output', fname_output,
                                                      '--autoscale-phase', 'False'],
                               catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

@pytest.mark.prelude
def test_cli_prepare_fieldmap_wrong_ext():
    runner = CliRunner()

    fname_output = 'fieldmap.txt'

    with pytest.raises(ValueError,
                       match=r"Output filename: .* must have one of the following extensions: '.nii', '.nii.gz'"):
        runner.invoke(prepare_fieldmap_cli, [fname_phasediff, '--mag', fname_mag_realtime, '--output', fname_output],
                      catch_exceptions=False)

def test_cli_prepare_fieldmap_default_fname_output():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        result = runner.invoke(prepare_fieldmap_cli, [fname_phasediff,
                                                      '--mag', fname_mag_realtime,
                                                      '--unwrapper', 'skimage',
                                                      '--output', tmp],
                               catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.nii.gz'))
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))

def test_cli_prepare_fieldmap_savemask():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')
        fname_output_mask = os.path.join(tmp, 'a', 'output_mask.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_phasediff,
                                                      '--mag', fname_mag_realtime,
                                                      '--unwrapper', 'skimage',
                                                      '--savemask', fname_output_mask,
                                                      '--output', fname_output], catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output_mask)

@pytest.mark.prelude
def test_cli_prepare_fieldmap_prelude():
    with tempfile.TemporaryDirectory(prefix='st_' + pathlib.Path(__file__).stem) as tmp:
        runner = CliRunner()

        fname_output = os.path.join(tmp, 'fieldmap.nii.gz')

        result = runner.invoke(prepare_fieldmap_cli, [fname_phasediff,
                                                      '--mag', fname_mag_realtime,
                                                      '--output', fname_output,
                                                      '--unwrapper', 'prelude'], catch_exceptions=False)

        assert result.exit_code == 0
        assert os.path.isfile(fname_output)
        assert os.path.isfile(os.path.join(tmp, 'fieldmap.json'))


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

import os
import time
import requests
from elasticsearch import Elasticsearch
import my_functions

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
config = my_functions.read_yaml(BASE_DIR + '/config.ini')

ES = Elasticsearch([
    {'host': config['data_store']['es_server']}
])

headers = {'User-Agent': config['flightradar24']['user_agent']}

if config['flightradar24']['service']=='by_code':
    flights_endpoint = str(config['flightradar24']['endpoint_live'] + '?airline=!' +
                           config['flightradar24']['airline_live_data']) + '&_' + \
                       str(int(time.mktime(time.localtime())*1000))
    flights_endpoint = flights_endpoint.rstrip()
    flights_request = requests.get(flights_endpoint, headers=headers)
else:

    balancer_request = requests.get(config['flightradar24']['balancer_config'], headers=headers)
    balancer_url = list(balancer_request.json().keys())[0]
    flights_endpoint = str('https://' + balancer_url + config['flightradar24']['endpoint'] + '&bounds=' +
                           config['flightradar24']['bounds']['world'])
    flights_endpoint = flights_endpoint.rstrip()
    flights_request = requests.get(flights_endpoint, headers=headers)
try:
    flights_data = flights_request.json()

    for key, flight in flights_data.items():
        if key == 'stats':
            continue
        if type(flight) is list:
            if config['flightradar24']['airline'] == 'ALL':
                my_functions.store_flight(config, ES, flight)
            else:
                if config['flightradar24']['airline'] in flight[13]:
                    my_functions.store_flight(config, ES, flight)
except:
    print("Oops, response is not json")


"""Main Entrypoint for Herald"""
import shlex
from collections import namedtuple

DELETED_STATUSES = set("D.|.D|MD|AD|RD|CD|DD|UD|DU".split("|"))
UNMERGED_STATUSES = set("DD|AA|UU|AU|UD|UA|DU".split("|"))
_StatusEntry = namedtuple("StatusEntry", ["status", "submodule", "orig_path", "path"])

class StatusEntry(_StatusEntry):

    """Represents one line of status when running 'git status'"""

    def is_deleted(self):
        return self.status in DELETED_STATUSES

    def is_unmerged(self):
        return self.status in UNMERGED_STATUSES

    def is_submodule(self):
        """Return True if the status entry is a submdule"""
        check = False
        if self.submodule is None:
            check = False
        else:
            check = self.submodule.startswith("S")
        return check

def get_raw_git_status_lines(invoke):
    cmd = "git status --porcelain=v2"
    result = invoke.run(cmd, hide=True, warn=True)
    # TODO: handle when result is not ok
    if result.ok:
        return result.stdout.splitlines()

def get_checkable_lines(lines):
    clean_lines = strip_headers(strip_empty_lines(lines))
    status_entries = parse_lines(clean_lines)
    deleted_entries = [l for l in status_entries if l.is_deleted()]
    unmerged_lines = [l for l in status_entries if l.is_unmerged()]
    submodule_lines = [l for l in status_entries if l.is_submodule()]
    return list(
        set(status_entries)
        - set(deleted_entries)
        - set(unmerged_lines)
        - set(submodule_lines)
    )

def strip_empty_lines(lines):
    return [l for l in lines if len(l) > 0]

def strip_headers(lines):
    return [l for l in lines if not l.startswith("#")]

def parse_lines(lines):
    return [parse(l) for l in lines]

def parse(line):
    tokens = tokenize(line)
    if tokens[0] in ("!", "?"):
        return parse_untracked_ignored_entry(tokens)
    if tokens[0] == "1":
        return parse_ordinary_change_entry(tokens)
    if tokens[0] == "2":
        return parse_renamed_or_copied_entry(tokens)
    if tokens[0] == "u":
        return parse_unmerged_entry(tokens)
    raise ValueError(
        "Line given does not match one of the recognized Git "
        "Porcelain V2 formats. {}".format(line)
    )

def tokenize(line):
    lex = shlex.shlex(line)
    lex.whitespace_split = True
    lex.whitespace = " "
    return list(lex)

def parse_untracked_ignored_entry(tokens):
    # Handle untracked and ignored files, Porcelain V2 gives single letter
    # status for these. We double them to allow for consistency in string
    # parsing elsewhere.
    status, path = tokens
    return StatusEntry(status * 2, None, None, path)

def parse_ordinary_change_entry(tokens):
    _, status, submodule, _, _, _, _, _, path = tokens
    return StatusEntry(status, submodule, None, path)

def parse_renamed_or_copied_entry(tokens):
    _, status, submodule, _, _, _, _, _, _, paths = tokens
    path, orig_path = paths.split("\t")
    return StatusEntry(status, submodule, orig_path, path)

def parse_unmerged_entry(tokens):
    _, status, submodule, _, _, _, _, _, _, _, path = tokens
    return StatusEntry(status, submodule, None, path)


"""
Serializers for book APIs.
"""

from rest_framework import serializers

from core.models import Book, Tag, Review

class ReviewSerializer(serializers.ModelSerializer):
    """Serializer for reviews"""

    class Meta:
        model = Review
        fields = ['id', 'name']
        read_only_fields = ['id']

class TagSerializer(serializers.ModelSerializer):
    """Serializer for tags"""

    class Meta:
        model = Tag
        fields = ['id', 'name']
        read_only_fields = ['id']

class BookSerializer(serializers.ModelSerializer):
    """Serializer for books"""
    tags = TagSerializer(many=True, required=False)
    reviews = ReviewSerializer(many=True, required=False)

    class Meta:
        model = Book
        fields = [
            'id', 'title', 'author', 'number_of_pages', 'category',
            'language', 'link', 'tags', 'reviews',
        ]
        read_only_fields = ['id']

    def _get_or_create_tags(self, tags, book):
        """Handle getting or creating tags as needed."""
        auth_user = self.context['request'].user
        for tag in tags:
            tag_obj, created = Tag.objects.get_or_create(
                user=auth_user,
                **tag,
            )
            book.tags.add(tag_obj)

    def _get_or_create_reviews(self, reviews, book):
        """Handle getting or creating reviews as needed."""
        auth_user = self.context['request'].user
        for review in reviews:
            review_obj, create = Review.objects.get_or_create(
                user=auth_user,
                **review,
            )
            book.reviews.add(review_obj)

    def create(self, validated_data):
        """Create a book."""
        tags = validated_data.pop('tags', [])
        reviews = validated_data.pop('reviews', [])
        book = Book.objects.create(**validated_data)
        self._get_or_create_tags(tags, book)
        self._get_or_create_reviews(reviews, book)

        return book

    def update(self, instance, validated_data):
        """Update book."""
        tags = validated_data.pop('tags', None)
        reviews = validated_data.pop('reviews', None)
        if tags is not None:
            instance.tags.clear()
            self._get_or_create_tags(tags, instance)
        if reviews is not None:
            instance.reviews.clear()
            self._get_or_create_reviews(reviews, instance)

        for attr, value in validated_data.items():
            setattr(instance, attr, value)

        instance.save()
        return instance

class BookDetailSerializer(BookSerializer):
    """Serializer for book details view"""

    class Meta(BookSerializer.Meta):
        fields = BookSerializer.Meta.fields + ['description']

class BookImageSerializer(serializers.ModelSerializer):
    """Serializer for uploading images to books."""

    class Meta:
        model = Book
        fields = ['id', 'image']
        read_only_fields = ['id']
        extra_kwargs = {'image': {'required': 'True'}}


import pandas as pd
from random import randrange
import sys

SetsLimit = 10000
StopPoint = 100
v1 = v2 = v3 = 0
for i in range(0,SetsLimit):
    pointsSum = 0
    while pointsSum <= StopPoint :
        pointsSum += randrange(1,7)
    if pointsSum == 101:
        v1 += 1
    if pointsSum == 102:
        v2 += 1
    if pointsSum == 103:
        v3 += 1

print("101:", v1, "%.2f" %  (v1/SetsLimit*100), "%")
print("102:", v2, "%.2f" %  (v2/SetsLimit*100), "%")
print("103:", v3, "%.2f" %  (v3/SetsLimit*100), "%")

a = ['Apple', 'Bananas', 'Pears']
s = pd.Series(a)
print(s)


import os
import string

import maya.cmds
import maya.mel

import rigBuilder.face.utils.file as fileUtils
from rigBuilder.combination import combinationPublish

def rigCombinationBuilder(character, bodyRigPath, faceRigPath=None,
        publish=False, description=str()):

    # Open the body rig.
    assert os.path.exists, "Body Rig Path '%s' does not exist..."
    maya.cmds.file(bodyRigPath, o=True, f=True)

    # Rename the file.
    tempPath = fileUtils.getTempFilePath([character, 'combined', 'rig', 'anim'], 'ma')
    maya.cmds.file(rename=tempPath)

    # Fix the view.
    maya.cmds.viewSet(home=True)

    # Face rig hook-up.
    if faceRigPath and os.path.exists(faceRigPath):

        switchCtl = 'C_mainA_ctl_0'

        maya.cmds.setAttr('%s.v' % switchCtl, l=False, k=True)
        maya.cmds.setAttr('%s.v' % switchCtl, 1)
        maya.cmds.setAttr('%s.v' % switchCtl, l=True, k=False)

        # add face attrs to head fkik control
        maya.cmds.addAttr('%s' % switchCtl, ln='face', nn=' ', at='enum', en='Face')
        maya.cmds.setAttr('%s.face' % switchCtl, cb=True)
        maya.cmds.addAttr('%s' % switchCtl, ln='faceControls', at='double', min=0, max=1, dv=1)
        maya.cmds.setAttr('%s.faceControls' % switchCtl, cb=True, k=False)
        maya.cmds.addAttr('%s' % switchCtl, ln='advFaceControls', at='double', min=0, max=1, dv=1)
        maya.cmds.setAttr('%s.advFaceControls' % switchCtl, cb=True, k=False)
        maya.cmds.addAttr('%s' % switchCtl, ln='tongueControls', at='double', min=0, max=1, dv=1)
        maya.cmds.setAttr('%s.tongueControls' % switchCtl, cb=True, k=False)
        maya.cmds.addAttr('%s' % switchCtl, ln='followHead', at='double', min=0, max=1, dv=0)
        maya.cmds.setAttr('%s.followHead' % switchCtl, cb=True)

        # reference into scene
        maya.cmds.file(faceRigPath, r=True, typ='mayaAscii', ns='FACE', f=True)

        # organize hierarchy
        grp = maya.cmds.group(n='C_face_org_0', em=True)
        maya.cmds.parent(grp, 'modules_grp')
        maya.cmds.parent('FACE:rig_face_anim', grp)
        maya.cmds.parentConstraint('C_head_bnd_0', 'FACE:transform_grp', mo=True)

        # get all geometry in model
        maya.cmds.namespace(set='GEO')
        dagnodes = maya.cmds.namespaceInfo(lod=True)
        maya.cmds.namespace(set=':')

        for geo in dagnodes:

            if not maya.cmds.objExists(geo): continue # groupId node disappears
            if not maya.cmds.objectType(geo, isType='transform'): continue
#             if 'antenna' in geo: continue

            # add facial geometry as blendshapes to model geometry
            shp = maya.cmds.listRelatives(geo, s=True)
            if not shp: continue

            blendGeo = 'FACE:model:%s' % string.split(geo, ':')[-1]

            if geo == 'GEO:C_body_geo_0':
                blendGeo = 'FACE:model:C_face_geo_0'

            # doesn't exist, move on
            if not maya.cmds.objExists(blendGeo): continue
            # doesn't have any deformers driving it, move on
            if not maya.mel.eval('findRelatedDeformer "%s"' % blendGeo): continue

            maya.cmds.setAttr('%s.v' % blendGeo, 0)

            # ensure geo is visible
            geosrc = maya.cmds.listConnections('%s.v' % geo, s=True, d=False, p=True)
            if geosrc:
                maya.cmds.disconnectAttr(geosrc[0], '%s.v' % geo)
                maya.cmds.setAttr('%s.v' % geo, 1)

            shpdict = {}
            for shp in maya.cmds.listRelatives(geo, s=True):
                shpsrc = maya.cmds.listConnections('%s.v' % shp, s=True, d=False, p=True)
                if shpsrc:
                    shpdict[shp] = shpsrc[0]
                    maya.cmds.disconnectAttr(shpsrc[0], '%s.v' % shp)
                    maya.cmds.setAttr('%s.v' % shp, 1)
            # check to see if there's an existing blendshape, and use it instead
            existingBlend = None
            deformers = maya.mel.eval('findRelatedDeformer %s' % geo)
            for deformer in deformers:
                if maya.cmds.objectType(deformer, i='blendShape'):
                    existingBlend = deformer
                    break
            if existingBlend:
                maya.cmds.blendShape(existingBlend, edit=True, t=[geo, 1, blend, 1])
                blend = existingBlend
            else:
                blend = maya.cmds.blendShape(blendGeo, geo, foc=True, tc=False)[0]

            # reconnect
            if geosrc: maya.cmds.connectAttr(geosrc[0], '%s.v' % geo)
            for shp in shpdict:
                shpsrc = shpdict[shp]
                maya.cmds.connectAttr(shpsrc, '%s.v' % shp)

            maya.cmds.setAttr('%s.%s' % (blend, string.split(blendGeo, ':')[-1]), 1)

        # connect control vis toggles
        for tongue in maya.cmds.ls('FACE:*tongue*'):
            if maya.cmds.objectType(tongue, isType='nurbsCurve'):
                maya.cmds.connectAttr('%s.tongueControls' % switchCtl, '%s.v' % tongue)

        maya.cmds.connectAttr('%s.faceControls' % switchCtl, 'FACE:C_faceGUI_cth_0.v')

        # connect eyeballs
        for locus in ['left', 'right']:
            src = '%s_eye_bnd_0.rotate' % locus[0].upper()
            dst = 'FACE:interface_grp.%sEyeBodyRig' % locus
            if not maya.cmds.objExists(src) or maya.cmds.objExtsts(dst):
                continue

            maya.cmds.connectAttr(src, dst, f=True)

        # hide face geometry
        maya.cmds.setAttr('FACE:visibility_grp.modelDisplayMeshes', 0)

#         maya.cmds.file(faceRigPath, ur='FACERN')
#         maya.cmds.parent('FACERNfosterParent1', 'C_face_org_0')

    maya.cmds.viewFit(all=True)
    maya.cmds.file(save=True, type='mayaAscii')
    if publish:
        combinationPublish.publishCombinedRig(character, tempPath)
    return True


import random

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
from sklearn.decomposition import PCA
from sklearn.metrics import confusion_matrix
from torch import nn
from torchtext.legacy import data
from torchtext.vocab import Vectors
from tqdm import tqdm

from util import init_device, load_params
from util.mmd import run_test
from util.model import MMD, MyEmbedding, MyMLP
from util.nlp_preprocessing import dataframe2dataset, tokenizer_ja

def train():
    # ランダムシード初期化
    seed = 0
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True
    device = init_device()

    # パラメータ読み込み
    print("Loading parameters...")
    params = load_params("/workspace/amazon_review/config/params_mmd.json")

    # データセット読み込み
    train_df = pd.read_json(params["ja_train_path"], orient="record", lines=True)
    if params["is_developing"]:
        train_df = train_df.sample(n=200000, random_state=1)
    dev_df = pd.read_json(params["ja_dev_path"], orient="record", lines=True)
    test_df = pd.read_json(params["ja_test_path"], orient="record", lines=True)

    # sourceカテゴリーとtargetカテゴリーを分ける
    train_source_df = train_df[train_df["product_category"] == params["source_category"]]
    dev_source_df = dev_df[dev_df["product_category"] == params["source_category"]]
    test_source_df = test_df[test_df["product_category"] == params["source_category"]]
    train_target_df = train_df[train_df["product_category"] == params["target_category"]]
    dev_target_df = dev_df[dev_df["product_category"] == params["target_category"]]
    test_target_df = test_df[test_df["product_category"] == params["target_category"]]

    # target_ratioで指定した比率までtargetのデータ数を減らす
    source_num = train_source_df.shape[0]
    target_num = int(source_num * params["target_ratio"])
    if target_num > train_target_df.shape[0]:
        print("Target ratio is too large...")
        print(f"Source num: {train_source_df.shape[0]}, Target num: {train_target_df.shape[0]}")
        exit()
    train_target_df = train_target_df.sample(target_num, replace=False)
    print(f"Source num: {train_source_df.shape[0]}, Target num: {train_target_df.shape[0]}")

    # クラスラベル設定
    for df in [train_source_df, dev_source_df, test_source_df, train_target_df, dev_target_df, test_target_df]:
        # 3以上かを予測する場合
        df["class"] = 0
        df["class"][df["stars"] > 3] = 1

        # 5クラス分類する場合
        # df["class"] = df["stars"] - 1

    # フィールド作成
    print("Building data iterator...")
    text_field = data.Field(
        sequential=True,
        tokenize=tokenizer_ja,
        use_vocab=True,
        lower=True,
        include_lengths=True,
        batch_first=True,
        fix_length=params["token_max_length"],
        init_token="<cls>",
        eos_token="<eos>",
    )
    label_field = data.Field(sequential=False, use_vocab=False)
    fields = [("text", text_field), ("label", label_field)]

    # データセット作成
    columns = ["review_body", "class"]
    train_source_dataset = dataframe2dataset(train_source_df, fields, columns)
    dev_source_dataset = dataframe2dataset(dev_source_df, fields, columns)
    test_source_dataset = dataframe2dataset(test_source_df, fields, columns)
    train_target_dataset = dataframe2dataset(train_target_df, fields, columns)
    dev_target_dataset = dataframe2dataset(dev_target_df, fields, columns)
    test_target_dataset = dataframe2dataset(test_target_df, fields, columns)
    all_train_dataset = dataframe2dataset(pd.concat([train_source_df, train_target_df]), fields, columns)

    # embedding作成
    if params["use_pretrained_vector"]:
        japanese_fasttext_vectors = Vectors(name=params["ja_vector_path"])
        text_field.build_vocab(all_train_dataset, vectors=japanese_fasttext_vectors, min_freq=1)
    else:
        text_field.build_vocab(all_train_dataset, min_freq=1)

    # データセット作成
    # train_source_iterのみエポックごとに生成し直す
    dev_source_iter = data.BucketIterator(
        dataset=dev_source_dataset, batch_size=params["batch_size"], train=False, sort=False
    )
    test_source_iter = data.BucketIterator(
        dataset=test_source_dataset, batch_size=params["batch_size"], train=False, sort=False
    )
    train_target_iter = data.BucketIterator(dataset=train_target_dataset, batch_size=params["batch_size"], train=True)
    dev_target_iter = data.BucketIterator(
        dataset=dev_target_dataset, batch_size=params["batch_size"], train=False, sort=False
    )
    test_target_iter = data.BucketIterator(
        dataset=test_target_dataset, batch_size=params["batch_size"], train=False, sort=False
    )

    # モデル構築
    v_size = len(text_field.vocab.stoi)
    if params["use_pretrained_vector"]:
        source_embedding = MyEmbedding(
            params["emb_dim"], v_size, params["token_max_length"], params["class_num"], text_field
        ).to(device)
        target_embedding = MyEmbedding(
            params["emb_dim"], v_size, params["token_max_length"], params["class_num"], text_field
        ).to(device)
    else:
        source_embedding = MyEmbedding(params["emb_dim"], v_size, params["token_max_length"]).to(device)
        target_embedding = MyEmbedding(params["emb_dim"], v_size, params["token_max_length"]).to(device)
    mlp = MyMLP(class_num=params["class_num"]).to(device)
    criterion = getattr(nn, params["criterion"])()
    mmd = MMD("rbf")

    optimizer_s_emb = getattr(torch.optim, params["optimizer"])(source_embedding.parameters(), lr=params["lr"])
    optimizer_t_emb = getattr(torch.optim, params["optimizer"])(target_embedding.parameters(), lr=params["lr"])
    optimizer_mlp = getattr(torch.optim, params["optimizer"])(mlp.parameters(), lr=params["lr"])

    split_ratio = len(train_target_dataset) / len(train_source_dataset)

    # 訓練
    for epoch in range(params["epochs"]):
        print(f"\nepoch {epoch+1} / {params['epochs']}")
        # 各lossの初期化
        total_source_loss = 0
        total_target_loss = 0
        total_mmd_loss = 0
        total_all_loss = 0

        # MMDのためにターゲットと同数のソースが必要なためエポックごとにソースのデータローダーを作成し直す
        # エポックごとに異なるソースのデータが使用されるようになる
        random.seed(epoch)
        train_source_subset, _ = train_source_dataset.split(split_ratio=split_ratio)
        train_source_iter = data.BucketIterator(
            dataset=train_source_subset, batch_size=params["batch_size"], train=True
        )

        for i, (source_batch, target_batch) in tqdm(
            enumerate(zip(train_source_iter, train_target_iter)), total=len(train_source_iter)
        ):
            source_embedding.train()
            target_embedding.train()
            mlp.train()

            source_x, source_y = source_batch.text[0].to(device), (source_batch.label).to(device)
            target_x, target_y = target_batch.text[0].to(device), (target_batch.label).to(device)

            # MMDの処理はBatch数が同数でなけらばならないためcontinueする
            # sourceとtargetで毎回同じBatch数だけデータがロードされる処理ができれば下の処理は不要
            if source_x.shape[0] != params["batch_size"] or target_x.shape[0] != params["batch_size"]:
                continue

            source_embed = source_embedding(source_x)
            source_pred = mlp(source_embed)
            source_loss = criterion(source_pred, source_y)
            total_source_loss += source_loss.cpu()

            target_embed = target_embedding(target_x)
            target_pred = mlp(target_embed)
            target_loss = criterion(target_pred, target_y)
            total_target_loss += target_loss.cpu()

            if params["lambda"] == 0:
                all_loss = source_loss + target_loss
            else:
                mmd_loss = mmd(source_embed, target_embed)
                total_mmd_loss += mmd_loss.cpu()
                all_loss = source_loss + target_loss + params["lambda"] * mmd_loss

            optimizer_s_emb.zero_grad()
            optimizer_t_emb.zero_grad()
            optimizer_mlp.zero_grad()

            all_loss.backward()

            optimizer_s_emb.step()
            optimizer_t_emb.step()
            optimizer_mlp.step()
            total_all_loss += all_loss.cpu()

        mean_source_loss = total_source_loss / len(train_source_iter)
        mean_target_loss = total_target_loss / len(train_target_iter)
        mean_all_loss = total_all_loss / len(train_source_iter)
        if params["lambda"] == 0:
            print(f"Loss -> Source: {mean_source_loss:.3f}\tTarget: {mean_target_loss:.3f}\tAll: {mean_all_loss:.3f}")
        else:
            mean_mmd_loss = total_mmd_loss / len(train_source_iter)
            print(
                f"Loss -> Source: {mean_source_loss:.3f}\tTarget: {mean_target_loss:.3f}\tMMD: {mean_mmd_loss:.3f}\tAll: {mean_all_loss:.3f}"  # noqa #E501
            )

        dev_source_accuracy, dev_source_f1 = run_test(source_embedding, mlp, dev_source_iter, device)
        print(f"\nDev source Accuracy: {dev_source_accuracy:.2f}")
        print(f"Dev source F1 Score: {dev_source_f1:.2f}")
        dev_target_accuracy, dev_target_f1 = run_test(target_embedding, mlp, dev_target_iter, device)
        print(f"\nDev target Accuracy: {dev_target_accuracy:.2f}")
        print(f"Dev target F1 Score: {dev_target_f1:.2f}")

    test_source_accuracy, test_source_f1 = run_test(source_embedding, mlp, test_source_iter, device)
    print(f"\nTest source Accuracy: {test_source_accuracy:.2f}")
    print(f"Test source F1 Score: {test_source_f1:.2f}")
    test_target_accuracy, test_target_f1 = run_test(target_embedding, mlp, test_target_iter, device)
    print(f"\nTest target Accuracy: {test_target_accuracy:.2f}")
    print(f"Test target F1 Score: {test_target_f1:.2f}")

    # 特徴量可視化
    source_embedding.eval()
    target_embedding.eval()

    train_source_iter = data.BucketIterator(dataset=train_source_dataset, batch_size=params["batch_size"], train=True)
    source_embedding_list = []
    for batch in train_source_iter:
        x, _ = batch.text[0].to(device), (batch.label).to(device)
        with torch.no_grad():
            embedding = source_embedding(x)
        source_embedding_list.extend(embedding.cpu().numpy())
    source_df = pd.DataFrame(np.array(source_embedding_list))

    target_embedding_list = []
    for batch in train_target_iter:
        x, _ = batch.text[0].to(device), (batch.label).to(device)
        with torch.no_grad():
            embedding = target_embedding(x)
        target_embedding_list.extend(embedding.cpu().numpy())
    target_df = pd.DataFrame(np.array(target_embedding_list))

    pca = PCA(n_components=2)
    pca.fit(source_df)
    source_pca_df = pca.transform(source_df)
    target_pca_df = pca.transform(target_df)
    source_pca_df.shape

    plt.scatter(source_pca_df[:, 0], source_pca_df[:, 1], label="Source")
    plt.scatter(target_pca_df[:, 0], target_pca_df[:, 1], label="Target")
    plt.legend()
    plt.show()

    label_list = []
    pred_list = []
    for batch in test_target_iter:
        x, y = batch.text[0].to(device), (batch.label).to(device)
        with torch.no_grad():
            embedding = target_embedding(x)
            pred = mlp(embedding)
        label_list.extend(list(y.cpu().numpy()))
        pred_list.extend(list(pred.cpu().numpy().argmax(1)))
    print(confusion_matrix(label_list, pred_list))

if __name__ == "__main__":
    train()


# mongo db 사용을 위한 패키지 import
from pymongo import MongoClient

# 연결
conn = MongoClient('localhost', port=27017)
print(conn)

# 데이터 베이스 사용 설정
db = conn.mgdb  # 없으면 생성됨

# collection 사용 설정
collect = db.data

# 데이터 수정
collect.update_many(
    {'empno': "10001"},
    {'$set': {'name': "최"}}
)

result = collect.find()
for temp in result:
    print(temp["name"], temp["age"])  # 없는 key 못 씀
    print(temp.get("name1", "이름 없음"))  # 없는 key 쓸 수 있음


from unittest.mock import AsyncMock
from unittest.mock import Mock

import pytest

from gloop.features.onboard import Onboard

@pytest.fixture
def onboard():
    def player_factory(player_id):
        mock = Mock()
        mock.id = player_id
        return mock

    return Onboard(
        AsyncMock(),
        player_factory,
        AsyncMock(),
        AsyncMock,
        AsyncMock(),
        AsyncMock
    )

@pytest.mark.asyncio
async def test_identify_client_with_available_id(onboard):
    expected_id = 42

    client = AsyncMock()
    client.request_client_id.return_value = expected_id

    onboard.player_repo.is_client_id_available.return_value = True

    player = await onboard.login()
    assert player.id == expected_id

@pytest.mark.asyncio
async def test_identify_client_with_unavailable_id(onboard):
    available_id = 42
    unavailable_id = 41

    client = AsyncMock()
    client.request_client_id.side_effect = [
        unavailable_id,
        available_id
    ]

    onboard.player_repo.is_client_id_available.side_effect = [
        False,
        True
    ]

    player = await onboard.login()
    assert player.id == available_id

@pytest.mark.asyncio
async def test_repeat_identify_client_until_available_id_is_provided(onboard):
    repeat = 10
    available_id = 42
    unavailable_id = 41

    client = AsyncMock()
    client.request_client_id.side_effect = [unavailable_id] * repeat + [available_id]

    onboard.player_repo.is_client_id_available.side_effect = [False] * repeat + [True]

    player = await onboard.login()
    assert player.id == available_id
    assert client.request_client_id.await_count == repeat + 1


# Imports
from bs4 import BeautifulSoup as bs
import requests
import psycopg2

# Web Scraper
response = requests.get('https://myanimelist.net/anime/season').text
soup = bs(response, 'lxml')

# Initializing empty lists to hold separate anime info
# anime_html = []
titles = []
scores = []
epnums = []
eplens = []
memnums = []
startdates = []
synopses = []
gnr_placeholder = []
genres = []
studios = []
sources = []
urls = []
imagelinks = []

info = soup.find('div', class_='js-seasonal-anime')
print(info.find('a', class_='link-title')['href']) # How to get an anime's url
print(info.find('img')['src']) # How to get an anime's image link

# Miscellaneous Test Code Snippets

# Parsing through html info
# for match in soup.find_all('div', class_='js-seasonal-anime'):
#     anime_html.append(match)

# print(len(anime_html))

# info = soup.find('div', class_='js-seasonal-anime')
# print(info)

# gnr1 = [genre.text.strip() for genre in info.find_all('span', class_='genre')]
# gnr1.extend([info.find('div', class_='properties').find_all('div', class_='property')[2].find('span', class_='item').text])
# gnr1.extend([info.find('div', class_='properties').find_all('div', class_='property')[3].find('span', class_='item').text])
# print(gnr1)
# print(info.find('div', class_='properties').find_all('div', class_='property')[2].find('span', class_='item').text, info.find('div', class_='properties').find_all('div', class_='property')[3].find('span', class_='item').text)

# for match in soup.find_all('div', class_='js-seasonal-anime'):
#     try:
#         print(match.find('div', class_='properties').find_all('div', class_='property')[2])
#     except:
#         print("N/A")
    # demographics.append(match.find('div', class_='properties').find_all('div', class_='property')[3])#.find('span', class_='item').text) # How to get an anime's demographic

# print(demographics)

# Test code for finding the mediums for all anime on the page
"""
for match in soup.find_all('div', class_='js-seasonal-anime-list'):
    print(match.find('div', class_='anime-header').text)
"""


import json
from datetime import datetime

import pytest
from jsonpath import jsonpath

from Services.apis.tagApi import Tag

class TestTag:
    def setup_class(self):
        self.tag = Tag()

    def teardown_class(self):
        # 一次操作下来遗留的数据只有：Hogwarts组下的tag_new标签和tag1更新后的数据
        lists = self.tag.get_corp_tag_list()
        group_id = jsonpath(lists.json(), '$.tag_group[?(@.group_name=="Hogwarts")]')[0]['group_id']
        self.tag.del_corp_group(group_id)

    def test_token(self):
        self.tag.get_token()

    @pytest.mark.run(order=1)
    def test_get_corp_list(self):
        result = self.tag.get_corp_tag_list()
        print(result.json())

    @pytest.mark.run(order=2)
    @pytest.mark.parametrize("group_name,tag_name",
                             [["Hogwarts", "tag1"], ["New_Group", "tag_new"]])
    def test_add_group_and_tag(self, group_name, tag_name):
        """
        新增标签组和组下的tag
        """
        # 获取添加前的标签组名
        pre_groups = self.tag.get_groups_names()
        print(pre_groups)
        # 针对添加的数据进行校验,若group不存在，则直接新增
        if group_name not in pre_groups:
            self.tag.add_corp_tag(group_name, tag_name)
            assert group_name in self.tag.get_groups_names()
            assert tag_name in self.tag.get_tags_name(group_name)
        else:
            tags = self.tag.get_tags_name(group_name)
            # 若group_name在添加前就存在，则判断新增的tag名称是否存在
            if tag_name in tags:
                # 若存在，则进行添加操作后，返回数据中tag_name是不会重复的。
                self.tag.add_corp_tag(group_name, tag_name)
                new_tags = self.tag.get_tags_name(group_name)
                assert len(tags) == len(new_tags)

    @pytest.mark.run(order=3)
    @pytest.mark.parametrize("group_name,tag_name", [["Hogwarts", "tag2"]])
    def test_add_tag_name(self, group_name, tag_name):
        """在已存在的标签组下新增tag"""
        pre_groups = self.tag.get_groups_names()
        print(pre_groups)
        # 针对添加的数据进行校验
        if group_name in pre_groups:
            tags = self.tag.get_tags_name(group_name)
            # 若group_name在添加前就存在，则判断新增的tag名称是否存在
            if tag_name in tags:
                # 若存在，则进行添加操作后，返回数据中tag_name是不会重复的。
                self.tag.add_corp_tag(group_name, tag_name)
                new_tags = self.tag.get_tags_name(group_name)
                assert len(tags) == len(new_tags)

            else:
                # 若需新增的tag在已有组中不存在，则添加成功，返回数据中应有对应的tag
                self.tag.add_corp_tag(group_name, tag_name)
                new_tags = self.tag.get_tags_name(group_name)
                assert tag_name in new_tags

    @pytest.mark.run(order=4)
    @pytest.mark.parametrize("group_name,tag_name", [["Hogwarts", "tag2"]])
    def test_add_tag_fail(self, group_name, tag_name):
        """重复添加1次标签和标签组"""
        pre_tags = self.tag.get_tags_name(group_name)
        self.tag.add_corp_tag(group_name, tag_name)
        new_tags = self.tag.get_tags_name(group_name)
        assert len(pre_tags) == len(new_tags)

    @pytest.mark.run(order=5)
    @pytest.mark.parametrize("tag_name", ["tag1_new", "tag1_中文", "tag1[中文]"])
    def test_edit_corp_tag(self, tag_name, group_name="Hogwarts"):
        tag_id = self.tag.get_tag_id_by_group_name(group_name)[0]
        print(f"tag_id是{tag_id}")
        res = self.tag.edit_corp_tag(tag_id, tag_name)
        print(res)
        # 返回的数据校验
        tags = self.tag.get_tags_name(group_name)
        print(tags)
        assert tag_name in tags

    @pytest.mark.run(order=6)
    @pytest.mark.parametrize("group_name", ["New_Group"])
    def test_del_corp_tag(self, group_name):
        tag_id = self.tag.get_tag_id(group_name)
        print(tag_id)
        self.tag.del_corp_tag(tag_id)
        groups = self.tag.get_groups_names()
        assert group_name not in groups


class TreeNode:
    def __init__(self, key, val, left = None, right = None, parent = None):
        self.key = key
        self.payload = val
        self.leftChild = left
        self.rightChild = right
        self.parent = parent

    def hasLeftChild(self):
        return self.leftChild

    def hasRightChild(self):
        return self.rightChild

    def isLeftChild(self):
        return self.parent and self.parent.leftChild == self

    def isRightChild(self):
        return self.parent and self.parent.rightChild == self

    def isRoot(self):
        return not self.parent

    def isLeaf(self):
        return not (self.rightChild or self.leftChild)

    def hasAnyChildren(self):
        return self.rightChild or self.leftChild

    def hasBothChildren(self):
        return self.leftChild and self.rightChild

    def replaceNodeData(self, key, value, lc, rc):
        self.key = key
        self.payload = value
        self.leftChild = lc
        self.rightChild = rc

        if self.hasLeftChild():
            self.leftChild.parent = self
        if self.hasRightChild():
            self.rightChild.parent = self

class BinarySearchTree:

    def __init__(self):
        self.root = None
        self.size = 0

    def lenght(self):
        return self.size

    def __len__(self):
        return self.size

    def __iter__(self):
        return self.root.__iter__()

    """
    Метод будет выполнять проверку на наличие корня дерева, а в случае отсутствия последнего
    создавать объект TreeNode и устанавливать его, как корневой узел.
    В противном случае put вызовет приватную рекурсивную вспомогательную функцию _put
    для поиска места в дереве по следующему алгоритму:
        Начиная от корня, проходим по двоичному дереву, сравнивая новый ключ с ключом текущего узла.
        Если первый меньше второго, то идём в левое поддерево. Наоборот - в правое.

        Когда не осталось левых или правых потомков для поиска - мы нашли позицию для установки нового узла.

        Чтобы добавить узел в дерево, создаём новый объект TreeNode и помещаем его на
        найденное за предыдущие шаги место.
    """

    def put(self, key, val):
        if self.root:
            self._put(key, val, self.root)

        else:
            self.root = TreeNode(key, val)

        self.size += 1

    def _put(self, key, val, currentNode):
        if key < currentNode.key:
            if currentNode.hasLeftChild():
                self._put(key, val, currentNode.leftChild)
            else:
                currentNode.leftChild = TreeNode(key, val, parent = currentNode)

        else:
            if currentNode.hasRightChild():
                self._put(key, val, currentNode.rightChild)
            else:
                currentNode.rightChild = TreeNode(key, val, parent = currentNode)

    def get(self, key):
        if self.root:
            res = self._get(key, self.root)

            if res:
                return res.payload

            else:
                return None
        else:
            return None

    def _get(self, key, currentNode):
        if not currentNode:
            return None
        elif currentNode.key == key:
            return currentNode
        elif key < currentNode.key:
            return self._get(key, currentNode.leftChild)
        else:
            return self._get(key, currentNode.rightChild)

    def __getitem__(self, item):
        return self.get(item)

    def __contains__(self, key):
        if self._get(key, self.root):
            return True

        else:
            return False

    def delete(self, key):
        if self.size > 1:
            nodeToRemove = self._get(key, self.root)
            if nodeToRemove:
                self.remove(nodeToRemove)
                self.size -= 1
            else:
                raise KeyError('Error, key not in tree')

        elif self.size == 1 and self.root.key == key:
            self.root = None
            self.size -= 1

        else:
            raise KeyError('Error, key not in tree')

    def __delitem__(self, key):
        self.delete(key)

    def __setitem__(self, key, value):
        self.put(key, value)

    """
    Если у узла есть правый потомок, то преемник - наименьший ключ в правом поддереве.

    Если у узла нет правого потомка и сам он - левый потомок родителя, то преемником будет родитель.

    Если узел - правый потомок своего родителя и сам правого потомка не имеет,
    то его преемником будет преемник родителя (исключая сам этот узел).
    """

    def findSuccessor(self):
        succ = None
        if self.hasRightChild():
            succ = self.rightChild.findMin()

        else:
            if self.parent:
                if self.isLeftChild():
                    succ = self.parent

                else:
                    self.parent.rightChild = None
                    succ = self.parent.findSuccessor()
                    self.parent.rightChild = self

        return succ

    def findMin(self):
        current = self
        while current.hasLeftChild():
            current = current.leftChild

        return current

    def spliceOut(self):
        if self.isLeaf():
            if self.isLeftChild():
                self.parent.leftChild = None

            else:
                self.parent.rightChild = None

        elif self.hasAnyChildren():
            if self.hasLeftChild():
                if self.isLeftChild():
                    self.parent.leftChild = self.leftChild

                else:
                    self.parent.rightChild = self.leftChild
                self.leftChild.parent = self.parent

            else:
                if self.isLeftChild():
                    self.parent.leftChild = self.rightChild

                else:
                    self.parent.rightChild = self.rightChild

                self.rightChild.parent = self.parent

    def remove(self, currentNode):
        if currentNode.isLeaf():
            if currentNode == currentNode.parent.leftChild:
                currentNode.parent.leftChild = None

            else:
                currentNode.parent.rightChild = None

        elif currentNode.hasBothChildren(): #interior

            succ = currentNode.findSuccessor()
            succ.spliceOut()
            currentNode.key = succ.key
            currentNode.payload = succ.payload

            """
            Если текущий узел - левый потомок, то нужно всего лишь обновить родительскую ссылку на него у предка,
            а затем обновить ссылку его потомка, чтобы она указывала на нового родителя.

            Если текущий узел - правый потомок, то мы обновляем родительскую ссылку его потомка,
            чтобы она указывала на предка текущего узла, а затем - ссылку на правого потомка у родителя текущего узла.

            Если текущий узел предка не имеет, то он должен быть корнем.
            В этом случае мы просто заменяем данные key, payload, leftChild и rightChild,
            вызвав для корня метод replaceNodeData."""

        else:           #This node has one child
            if currentNode.hasLeftChild():
                if currentNode.isLeftChild():
                    currentNode.leftChild.parent = currentNode.parent
                    currentNode.parent.leftChild = currentNode.leftChild

                elif currentNode.isRightChild():
                    currentNode.leftChild.parent = currentNode.parent
                    currentNode.parent.rightChild = currentNode.leftChild

                else:
                    currentNode.replaceNodeData(currentNode.leftChild.key,
                                                currentNode.leftChild.payload,
                                                currentNode.leftChild.leftChild,
                                                currentNode.leftChild.rightChild)

            else:
                if currentNode.isLeftChild():
                    currentNode.rightChild.parent = currentNode.parent
                    currentNode.parent.leftChild = currentNode.rightChild

                elif currentNode.isRightChild():
                    currentNode.rightChild.parent = currentNode.parent
                    currentNode.parent.rightChild = currentNode.rightChild

                else:
                    currentNode.replaceNodeData(currentNode.rightChild.key,
                                                currentNode.rightChild.payload,
                                                currentNode.rightChild.leftChild,
                                                currentNode.rightChild.rightChild)

if __name__ == '__main__':
    myTree = BinarySearchTree()
    myTree[3] = "red"
    myTree[4] = "blue"
    myTree[6] = "yellow"
    myTree[2] = "at"

    print(myTree[6])
    print(myTree[2])


"""
E->TE'
E'->+TE'|~
T->FT'
T'->*FT'|~
F->(E)|id
"""
from .first_follow_class import FF
from .products import Product
import re

sources = {}
products = []
ter = {'id':0,'+':1,'*':2,'(':3,')':4,'$':5}
non_ter = {'E':0,"E'":1,"T":2,"T'":3,"F":4}
def getFirst():
    for i in range(len(products)):
        source = products[i].source
        if products[i].source not in sources.keys():
            sources[source] = FF(source)
        # 右边第一个是终结符直接加入
        if re.match('id|[+*a-z~()]', products[i].right[0]):
            sources[source].first.append(products[i].right[0])
        # 右边第一个是非终结符，加入add集
        elif re.match('[A-Z]', products[i].right[0]):
            sources[source].add.append(products[i].right[0])

        # 寻找产生式的或， 重复上述步骤
        try:
            if re.match('id|[a-z+*~()]', products[i].right[products[i].right.index('|') + 1]):
                sources[source].first.append(products[i].right[products[i].right.index('|') + 1])
            elif re.match('[A-Z]', products[i].right[products[i].right.index('|') + 1]):
                sources[source].add.append(products[i].right[products[i].right.index('|') + 1])
        except:
            continue

    for i in range(len(sources)):
        for s in sources.keys():
            if len(sources[s].add) > 0:
                for s_add in sources[s].add:
                    sources[s].first += sources[s_add].first
    for s in sources.keys():
        sources[s].first = set(sources[s].first)
        sources[s].reset_add()
"""
E->TE'
E'->+TE'|~
T->FT'
T'->*FT'|~
F->(E)|id
"""
def getFollow():
    # todo
    sources["E"].follow = ['$',')']
    sources["E'"].follow = ['$',')']
    sources["T"].follow = ['+','$',')']
    sources["T'"].follow = ['+','$',')']
    sources["F"].follow = ['*','+','$',')']

analysis_table = []
def get_analysis_table():
    ret = [[0 for _ in range(6)] for i in range(5)]
    ret[0][0] = ['T',"E'"]
    ret[0][3] = ['T',"E'"]
    ret[1][1] = ['+','T',"E'"]
    ret[1][4] = ['~']
    ret[1][5] = ['~']
    ret[2][0] = ['F',"T'"]
    ret[2][-3] = ['F',"T'"]
    ret[3][1] = ['~']
    ret[3][2] = ['*','F',"T'"]
    ret[3][4] = ['~']
    ret[3][-1] = ['~']
    ret[4][0] = ['id']
    ret[4][3] =['(','E',')']
    return ret

def parse_string_to_pds(s) -> list:
    for i in s:
        p = Product(i)
        products.append(p)
def parse_inp(inp)->list:
    return re.findall("id|[()a-z*+]",inp)

if __name__ == '__main__':
    # s = ["E->TE'",
    #      "E'->+TE'|~",
    #      "T->FT'",
    #      "T'->*FT'|~",
    #      "F->(E)|id"]
    # parse_string_to_pds(s)
    # getFirst()
    # getFollow()
    analysis_table = get_analysis_table()
    st = ['$','E']
    inp = parse_inp(input())
    inp.append("$")
    ip = 0
    X = st[-1]
    while X!='$':
        if X==inp[ip]:
            st.pop(-1)
            ip+=1
        elif re.match("id|[a-z+*()~]",X):
            print("弹栈，弹出非终结符{}".format(X))
            if X == ')':
                print("括号不匹配")
            exit(0)
        elif analysis_table[non_ter[X]][ter[inp[ip]]]==0:
            print("输入串跳过记号{}，用户多输入了一个{}".format(inp[ip],inp[ip]))
            exit(0)
        elif analysis_table[non_ter[X]][ter[inp[ip]]]!=0:
            print(X,'->',analysis_table[non_ter[X]][ter[inp[ip]]])
            st.pop(-1)
            for f in range(len(analysis_table[non_ter[X]][ter[inp[ip]]])-1,-1,-1):
                if analysis_table[non_ter[X]][ter[inp[ip]]][f]=='~':
                    continue
                st.append(analysis_table[non_ter[X]][ter[inp[ip]]][f])
        X = st[-1]
    print("0 error 0 warning")


import torch
import torch.nn as nn
import torchvision.models.resnet as resnet
from torch import Tensor

from .network import Network

class ResNetwork(Network):
    def __init__(self, n_channels: int, image_height: int, image_width: int, action_space_dim: int):
        super(ResNetwork, self).__init__(n_channels, image_height, image_width, action_space_dim)
        self._norm_layer = nn.BatchNorm2d

        self.inplanes = 64
        self.dilation = 1

        self.groups = 1
        self.base_width = 64
        self.conv1 = nn.Conv2d(n_channels, self.inplanes,
                               kernel_size=(3, 3), stride=(2, 2), padding=(1, 1),
                               bias=False)  # 40
        self.bn1 = self._norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)  # 20
        self.layer1 = self._make_layer(64, 2)  # 20
        self.layer2 = self._make_layer(128, 2, stride=2)  # 10
        self.layer3 = self._make_layer(256, 2, stride=2)  # 5
        self.layer4 = self._make_layer(512, 2, stride=2)  # 3
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))  # 1
        self.fc = nn.Linear(512 * resnet.BasicBlock.expansion, action_space_dim)
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        for m in self.modules():
            if isinstance(m, resnet.BasicBlock):
                nn.init.constant_(m.bn2.weight, 0)  # type: ignore[arg-type]

    def _make_layer(self, planes: int, blocks: int,
                    stride: int = 1, dilate: bool = False) -> nn.Sequential:
        block = resnet.BasicBlock
        norm_layer = self._norm_layer
        downsample = None
        previous_dilation = self.dilation
        if dilate:
            self.dilation *= stride
            stride = 1
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                resnet.conv1x1(self.inplanes, planes * block.expansion, stride),
                norm_layer(planes * block.expansion),
            )

        layers = [block(self.inplanes, planes, stride, downsample, self.groups,
                        self.base_width, previous_dilation, norm_layer)]
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes, groups=self.groups,
                                base_width=self.base_width, dilation=self.dilation,
                                norm_layer=norm_layer))

        return nn.Sequential(*layers)

    def forward(self, x: Tensor) -> Tensor:
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)
        return x


import os

from bottle import abort, request, route, run, view
import requests

API_URL = os.environ['AWS_ENDPOINT']
TITLE = "PyBites PEP8 Checker"
ERROR_CODE = 400

@route('/', method=['GET', 'POST'])
@view('index')
def index():
    code = request.forms.get('code') or ''
    pep_errors = ''

    if code:
        payload = {"code": code}
        resp = requests.post(API_URL, json=payload).json()

        error = resp.get("errorMessage")
        exception = resp.get("errorType")
        if error and exception:
            msg = f"Lambda function raised a {exception} exception"
            abort(ERROR_CODE, msg)

        pep_errors = resp["body"]

    return {'title': TITLE,
            'code': code,
            'pep_errors': pep_errors}

run(host='localhost', port=8080, debug=True, reloader=True)


from django.urls import path
from .views import (
    ProductDetailView,
    CheckoutView,
    HomeView,
    OrderSummaryView,
    add_to_cart,
    remove_from_cart,
    remove_single_item_from_cart,
    ShopView,
    PaymentView,
    AddCouponView,
    RequestRefundView,
)

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('shop', ShopView.as_view(), name='shop'),
    path('checkout/', CheckoutView.as_view(), name='checkout'),
    path('shop/', ShopView.as_view(), name='shop'),
    path('order-summary/', OrderSummaryView.as_view(), name='order-summary'),
    path('product/<slug>/', ProductDetailView.as_view(), name='product'),
    path('add-to-cart/<slug>/', add_to_cart, name='add-to-cart'),
    path('add-coupon/', AddCouponView.as_view(), name='add-coupon'),
    path('remove-from-cart/<slug>/', remove_from_cart, name='remove-from-cart'),
    path('remove-item-from-cart/<slug>/', remove_single_item_from_cart,
         name='remove-single-item-from-cart'),
    path('payment/<payment_option>/', PaymentView.as_view(), name='payment'),
    path('request-refund/', RequestRefundView.as_view(), name='request-refund'),

   # path('', home, name='home'),
   # path('shop/', shop, name='shop'),
   # path('checkout/', checkout, name='checkout'),
   # path('detail/', detail, name='detail'),
   # path('cart/', cart, name='cart'),
   # path('about/', about, name='about'),
   # path('', contact, name='contact'),

]


# import packages -- os and pandas
import os
import pandas as pd

# %% Loop through folder and combine
# input path
# escape sequences: \newline,\\,\',\",\a,\b,\f,\n,\N,\r,\t,\u,\U,\v,\[0-9],\x
inpath = r"C:\Users\######\Py-R\data\session4\exercise2"
# list of files
inpathfiles = os.listdir(inpath)

# empty list
dfs = []

# %% loop
for filename in inpathfiles:
    # condition/filter -- match with "WaferFlow"
    if "WaferFlow" in filename:
        # append path to filename
        filenamefull = inpath + "\\" + filename

        # get metadata
        df_head = pd.read_csv(filenamefull,header=None,nrows=4)
        metadata = {}
        for x in range(df_head.shape[0]):
            metadata[df_head.iloc[x,0]] = df_head.iloc[x,1]

        # read body of file into dataframe
        df = pd.read_csv(filenamefull,skiprows=5)

        # delete units row
        df = df.drop([0])

        # EXTRA EXPLANATION: create stacked dataframe -- NEW
        stacked = pd.melt(df, id_vars=["sec"], var_name="Variable",value_name="Value")
        # remove nulls -- NEW
        stacked_clean = stacked[pd.to_numeric(stacked["Value"], errors='coerce').notnull()]

        # loop
        for key in metadata:
            stacked_clean.insert(0,key,metadata[key])

        # add filename
        stacked_clean.insert(0, 'FileName', filename)

        # add data to list
        dfs.append(stacked_clean)
#%% combine data
result = pd.concat(dfs)
# %% output file
# set output directory -- create if non-existent
outpath = inpath + "\output"
if not os.path.exists(outpath):
    os.makedirs(outpath)
# create full output path
outfile = outpath + "\\session4output2dict.csv"
# create ouput file
result.to_csv(outfile,index=False)


def text_editor(text, pattern):
    undo = []
    redo = []

    for i in text:
        undo.append(i)

    for i in pattern:
        if i == "u":
            data = undo.pop()
            redo.append(data)
        else:
            data = redo.pop()
            undo.append(data)

    res = ""
    while len(undo):
        res = undo[-1] + res

        undo.pop()

    return res

if __name__ == "__main__":
    ans = text_editor("Hello", "uuurr")
    print("Solution: ", ans)


import cv2
import numpy as np
from counter import *

input_name = input("Type the name of the file in input folder : ")
cap = cv2.VideoCapture("input/"+input_name)
x1 = int(input("Type the x-coordinate of first point of line : "))
y1 = int(input("Type the y-coordinate of first point of line : "))
x2 = int(input("Type the x-coordinate of second point of line : "))
y2 = int(input("Type the y-coordinate of second point of line : "))

if(cap.isOpened()==False):
	print("Error reading video !")
ret, frame = cap.read()
frame = cv2.resize(frame, (0,0), fx = 0.5, fy = 0.5)
f_height, f_width, _ = frame.shape
line = [(x1//2,y1//2),(x2//2, y2//2)]
#line = [(0,int(f_height/1.5)),(f_width-1, int(f_height/1.5))]

object_counter = Counter(frame, line)

result = cv2.VideoWriter('output1.avi',  cv2.VideoWriter_fourcc('M','J','P','G'), 10, (f_width,f_height))

while ret:
	object_counter.count(frame)
	frame_ = object_counter.show_frame()
	# cv2.imshow('Frame', frame_)
	# cv2.waitKey(25)
	result.write(frame_)
	print(object_counter.vehicle_count)
	ret, frame = cap.read()
	if ret:
  		frame = cv2.resize(frame, (0,0), fx = 0.5, fy = 0.5)

cap.release()
result.release()
cv2.destroyAllWindows()


#преобразование массива в словарь
def preobr(mass):
    #словарь смежности вершин
    tempDict = {}
    for i in range(len(mass)):
        #массив вершин
        tempEdges = []
        for j in range(len(mass[i])):
            if mass[i][j] == 1:
                tempEdges.append(j+1)
        tempDict[i+1] = tempEdges
    return tempDict  #вывод {1: [2, 3], 2: [1, 3], 3: [1, 2, 4], 4: [3, 5], 5: [4]}

def shirina(mass, start):
    #берём данные в виде словаря
    inc = preobr(mass)

    visited = [] #массив посещённых вершин
    Q = []  #очередь
    BFS = [] #путь обхода
    matrix = {} #результирующая матрица

    def bfs(v):
        if v in visited: #если вершина уже посещена, выходим
            return
        visited.append(v) #посетили вершину v
        BFS.append(v) #запомнили порядок обхода
        for i in inc[v]: #все смежные с v вершины
            if not i in visited:
                Q.append(i) #записываем в очередь
        tempConn = [] #массив соединений

        parent = 0
        for k in matrix.keys():
            if matrix[k][v-1] == 1:
                parent = k+1

        for j in range(len(mass[v-1])):
            fl = 1
            for l in visited:
                if l == v:
                    if mass[v-1][j] != 1:
                        fl=0
                elif l == parent:
                    if mass[l-1][j] == 1 or j == v-1 or mass[v-1][j] != 1:
                        fl=0
                else:
                    if mass[l-1][j] == 1 and mass[parent-1][l-1] == 1 and mass[v-1][j] == 1 and j != parent-1:
                        fl=0
                        break

            if fl == 1:
                tempConn.append(1)
            else:
                tempConn.append(0)
        matrix[v-1] = tempConn
        while Q:
            bfs(Q.pop(0))

    bfs(start) #начальная вершина обхода - start
    dict= [BFS,matrix] #путь обхода и матрица
    return dict


# 백준 - 숫자 할리갈리 게임(20923)
# https://www.acmicpc.net/problem/20923

import sys
from collections import deque

if __name__ == "__main__":
    N, M = list(map(int, sys.stdin.readline().split()))
    do_deck = deque()
    su_deck = deque()
    for _ in range(N):
        d, s = list(map(int, sys.stdin.readline().split()))
        do_deck.appendleft(d)
        su_deck.appendleft(s)
    do_ground = deque()
    su_ground = deque()
    for i in range(M):
        if i % 2 == 0:
            do_ground.append(do_deck.popleft())
        else:
            su_ground.append(su_deck.popleft())
        if not do_deck or not su_deck:
            break
        if (do_ground and do_ground[-1] == 5) or (su_ground and su_ground[-1] == 5):
            do_deck.extend(su_ground)
            do_deck.extend(do_ground)
            do_ground = deque()
            su_ground = deque()
        elif do_ground and su_ground and (do_ground[-1] + su_ground[-1] == 5):
            su_deck.extend(do_ground)
            su_deck.extend(su_ground)
            do_ground = deque()
            su_ground = deque()
    if len(do_deck) > len(su_deck):
        print("do")
    elif len(do_deck) < len(su_deck):
        print("su")
    else:
        print("dosu")

baekjoon/20923-숫자-할리갈리-게임/20923_숫자_할리갈리_게임.py


from django.contrib import admin

from what.utils import Utils
from what.models import Teacher, Student, Quiz, Annal, Question, Answer, Setting, Locale

class AnnalAdmin(admin.ModelAdmin):
	list_display = ["annal_name", "teacher", "enabled", "number_of_questions",
	"created_on"]
	search_fields = ["annal_name"]
	actions = ["delete_selected"]

	def get_form(self, request, obj=None, **kwargs):
		self.exclude = ["created_on", "number_of_questions"]
		return super().get_form(request, obj, **kwargs)

	def save_model(self, request, obj, form, change):
		if not obj.id:
			obj.teacher.number_of_annals += 1
			obj.teacher.save()
		obj.save()

	def delete_model(self, request, obj):
		obj.teacher.number_of_annals -= 1
		obj.teacher.save()
		obj.delete()

	def delete_selected(self, request, queryset):
		for obj in queryset:
			obj.teacher.number_of_annals -= 1
			obj.teacher.save()
			obj.delete()

class QuestionAdmin(admin.ModelAdmin):
	list_display = ["get_question", "points_rewarded", "annal"]
	actions = ["delete_selected"]

	def get_question(self, obj):
		return obj.question_text

	def save_model(self, request, obj, form, change):
		if not obj.id:
			obj.annal.number_of_questions += 1
			obj.annal.save()
		obj.save()

	def delete_model(self, request, obj):
		obj.annal.number_of_questions -= 1
		obj.annal.save()
		obj.delete()

	def delete_selected(self, request, queryset):
		for obj in queryset:
			obj.annal.number_of_questions -= 1
			obj.annal.save()
			obj.delete()

	get_question.short_description = "Question"
	get_question.admin_order_field = "question_text"

class AnswerAdmin(admin.ModelAdmin):
	list_display = ["get_answer", "question", "get_annal", "answer_is_correct"]

	def get_answer(self, obj):
		return obj.answer_text

	def get_annal(self, obj):
		return obj.question.annal

	get_answer.short_description = "Answer"
	get_answer.admin_order_field = "answer_text"
	get_annal.short_description = "Annal"
	get_annal.admin_order_field = "question__annal"

class TeacherAdmin(admin.ModelAdmin):
	list_display = ["get_user", "is_admin", "number_of_annals"]

	def get_user(self, obj):
		return obj.user.username

	def get_form(self, request, obj=None, **kwargs):
		self.exclude = ["number_of_annals"]
		return super().get_form(request, obj, **kwargs)

	get_user.short_description = "Teacher"
	get_user.admin_order_field = "user__username"

class StudentAdmin(admin.ModelAdmin):
	list_display = ["student_name", "number_of_quizzes"]

	def get_form(self, request, obj=None, **kwargs):
		self.exclude = ["number_of_quizzes"]
		return super().get_form(request, obj, **kwargs)

class QuizAdmin(admin.ModelAdmin):
	list_display = ["get_student", "get_url", "annal", "score",
	"max_score", "number_of_questions", "submitted", "start_time",
	"get_finish_time"]
	actions = ["delete_selected"]

	def get_student(self, obj):
		return obj.student.student_name

	def get_url(self, obj):
		return Utils.get_quiz_url(obj.quiz_code)

	def get_finish_time(self, obj):
		return Utils.format_duration(obj.annal.annal_duration - obj.finish_time)

	def get_form(self, request, obj=None, **kwargs):
		self.exclude = ["quiz_code", "score", "max_score", "finish_time"]
		return super().get_form(request, obj, **kwargs)

	def save_model(self, request, obj, form, change):
		if not obj.id:
			obj.quiz_code = Utils.generate_quiz_code()
			obj.student.number_of_quizzes += 1
			obj.student.save()
		obj.save()

	def delete_model(self, request, obj):
		obj.student.number_of_quizzes -= 1
		obj.student.save()
		obj.delete()

	def delete_selected(self, request, queryset):
		for obj in queryset:
			obj.student.number_of_quizzes -= 1
			obj.student.save()
			obj.delete()

	get_student.short_description = "Quiz for"
	get_student.admin_order_field = "student__student_name"
	get_url.short_description = "Quiz link"
	get_url.admin_order_field = "quiz_code"
	get_finish_time.short_description = "Finished in"
	get_finish_time.admin_order_field = "-finish_time"

admin.site.register(Annal, AnnalAdmin)
admin.site.register(Teacher, TeacherAdmin)
admin.site.register(Student, StudentAdmin)
admin.site.register(Quiz, QuizAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(Answer, AnswerAdmin)
admin.site.register(Locale)
admin.site.register(Setting)


import numpy as np
from sklearn.metrics import mean_absolute_error

import datamunge as dm
import plots as plts

ALL_ROOMS = ["babbage", "babyfoot", "jacquard"]

def print_losses(history):
    print("{:<25} {:<10}".format("Loss Name", "Losses"))
    print("")
    for key, item in history.history.items():
        print("{:<25}".format(key), end="")
        for val in item:
            print("{:.5f} ".format(val), end="")
        print("")

def rescaled_mae(scaler, preds, true_vals, orig_columns=5):
    """
    Calculates the MAE after rescaling the data.
    """
    preds_rs = dm.rescale(scaler, preds, orig_columns)
    true_vals_rs = dm.rescale(scaler, true_vals, orig_columns)
    return mean_absolute_error(true_vals_rs, preds_rs)

def print_mae(preds, true_vals):
    """ Prints the MAE for the given prediction and truth values """
    print(f"MAE {mean_absolute_error(true_vals, preds)}")

def print_rescaled_mae(scaler, preds, true_vals, orig_columns=5):
    print(f"MAE rescaled {rescaled_mae(scaler, preds, true_vals, orig_columns)}")

def make_predictions(model, test_x, test_y):
    preds = model.predict(test_x).flatten()
    true_vals = test_y.flatten()
    return preds, true_vals

def run_predictions(model, in_test_x, in_test_y, scaler=None):
    preds, true_vals = make_predictions(model, in_test_x, in_test_y)

    print_mae(preds, true_vals)
    if not scaler is None:
        print_rescaled_mae(scaler, preds, true_vals, in_test_x.shape[2])

    return (preds, true_vals)

def make_bywindow_predictions(model, test_x, test_y):
    if isinstance(test_x, list):
        #lookback = test_x[0].shape[1]
        predictions = test_y.shape[1]
        test_set = [test_x[0][0::predictions], test_x[1][0::predictions]]
    else:
        #lookback = test_x.shape[1]
        predictions = test_y.shape[1]
        test_set = test_x[0::predictions]

    preds = model.predict(test_set).flatten()
    true_vals = test_y[0::predictions].flatten()

    return preds, true_vals

def make_bywindow_multi_predictions(model, test_x, test_sets):
    """
    Using the model and the given test sets, makes predictions based on the window
    used in the train set and joins the predictions together to make one continuous
    prediction. This multi prediction version is used for models that have multiple
    output predictions.
    """
    if isinstance(test_x, list):
        lookback = test_x[0].shape[1]
        test_set = [test_x[0][0::lookback], test_x[1][0::lookback]]
    else:
        lookback = test_x.shape[1]
        test_set = test_x[0::lookback]

    preds = model.predict(test_set)

    preds = [x.flatten() for x in preds]
    truth_sets = [x[0::lookback].flatten() for x in test_sets]

    return preds, truth_sets

def plot_preds_v_truth(preds, true_vals, truth_name="temperature", chart_index=None):
    plts.pandas_2line_plot_from_data(
        true_vals, preds, truth_name, "predictions", chart_index
    )

def chart_index_for_room(df, split_date, room, num_predictions=12):
    test_set = df.loc[df.index >= split_date]
    return test_set[test_set.room == room].iloc[:-num_predictions].index

def prediction_chart_for_room(
    model,
    room,
    room_testsets,
    truth_name="temperature",
    chart_index=None,
    scaler=None,
    orig_columns=5,
):
    in_test_x = room_testsets[room]["test_x"]
    in_test_y = room_testsets[room]["test_y"]

    preds, true_vals = make_bywindow_predictions(model, in_test_x, in_test_y)

    if not scaler is None:
        preds = dm.rescale(scaler, preds, orig_columns)
        true_vals = dm.rescale(scaler, true_vals, orig_columns)

    plot_preds_v_truth(
        preds, true_vals, truth_name=f"{truth_name} {room}", chart_index=chart_index
    )

def prediction_chart_for_partition(
    model,
    partition,
    testsets,
    truth_name="temperature",
    chart_index=None,
    scaler=None,
    orig_columns=5,
):
    in_test_x = testsets[partition]["test_x"]
    in_test_y = testsets[partition]["test_y"]

    preds, true_vals = make_bywindow_predictions(model, in_test_x, in_test_y)

    if not scaler is None:
        preds = dm.rescale(scaler, preds, orig_columns)
        true_vals = dm.rescale(scaler, true_vals, orig_columns)

    plot_preds_v_truth(
        preds,
        true_vals,
        truth_name=f"{truth_name} {partition}",
        chart_index=chart_index,
    )

def prediction_charts_by_room(
    model,
    room_testsets,
    truth_name="temperature",
    chart_index=None,
    rooms=None,
    scalers=None,
    orig_columns=5,
):
    if rooms is None:
        rooms = ALL_ROOMS

    for room in rooms:
        if scalers is None:
            scaler = None
        else:
            scaler = scalers[room]

        prediction_chart_for_room(
            model,
            room,
            room_testsets,
            truth_name=truth_name,
            chart_index=chart_index,
            scaler=scaler,
            orig_columns=orig_columns,
        )

def run_predictions_for_rooms(model, room_testsets, scalers, rooms=None):
    if rooms is None:
        rooms = ALL_ROOMS

    for room in rooms:
        in_test_x = room_testsets[room]["test_x"]
        in_test_y = room_testsets[room]["test_y"]

        scaler = scalers[room]

        print(f"Predictions for {room}")
        run_predictions(model, in_test_x, in_test_y, scaler)
        print("")

def replace_column_in_input(pred_vals, in_vals, col):
    return np.dstack((np.delete(in_vals, col, axis=2), pred_vals))

def get_slice_of_testset(test_x, start_index=0, end_index=1):
    if isinstance(test_x, list):
        return list(map(lambda x: x[start_index:end_index], test_x))

    return test_x[start_index:end_index]

def make_stepthrough_predictions(model, test_x, test_y, output_col=0, window_size=12):
    test_set = get_slice_of_testset(test_x)

    preds = model.predict(test_set).flatten()

    if isinstance(test_x, list):
        test_length = len(test_x[0])
    else:
        test_length = len(test_x)

    for i in range(window_size, test_length, window_size):
        last_prediction = preds[-window_size:]
        input_slice = get_slice_of_testset(test_x, i, i + 1)
        if isinstance(test_x, list):
            new_input = input_slice
            new_input[0] = replace_column_in_input(
                last_prediction, input_slice[0], output_col
            )
        else:
            new_input = replace_column_in_input(
                last_prediction, input_slice, output_col
            )
        preds = np.append(preds, model.predict(new_input).flatten())

    true_vals = test_y.flatten()[0::window_size]
    return preds, true_vals

def stepthrough_predictions_for_daynum(
    model, test_x, test_y, daynum=0, dayindex=0, dayspan=1, output_col=0, window_size=12
):
    slice_start = 288 * daynum + dayindex
    slice_end = slice_start + dayspan * 288
    index_slice = slice(slice_start, slice_end)

    if isinstance(test_x, list):
        test_slice_x = list(map(lambda x: x[index_slice], test_x))
    else:
        test_slice_x = test_x[index_slice]

    test_slice_y = test_y[index_slice]

    return make_stepthrough_predictions(
        model, test_slice_x, test_slice_y, output_col, window_size
    )