from libmodel import simple_autoPrelu

from torchvision import transforms,datasets

from torch.optim import Optimizer

import numpy as np

import torch

from pytorch_msssim import *

import os

from libcustomDataset import ImageFolderWithPaths

class CyclicLR(object):
    """
        optimizer (Optimizer): Wrapped optimizer.
        base_lr (float or list): Initial learning rate which is the
            lower boundary in the cycle for eachparam groups.
            Default: 0.001
        max_lr (float or list): Upper boundaries in the cycle for
            each parameter group. Functionally,
            it defines the cycle amplitude (max_lr - base_lr).
            The lr at any cycle is the sum of base_lr
            and some scaling of the amplitude; therefore
            max_lr may not actually be reached depending on
            scaling function. Default: 0.006
        step_size (int): Number of training iterations per
            half cycle. Authors suggest setting step_size
            2-8 x training iterations in epoch. Default: 2000
        mode (str): One of {triangular, triangular2, exp_range}.
            Values correspond to policies detailed above.
            If scale_fn is not None, this argument is ignored.
            Default: 'triangular'
        gamma (float): Constant in 'exp_range' scaling function:
            gamma**(cycle iterations)
            Default: 1.0
        scale_fn (function): Custom scaling policy defined by a single
            argument lambda function, where
            0 <= scale_fn(x) <= 1 for all x >= 0.
            mode paramater is ignored
            Default: None
        scale_mode (str): {'cycle', 'iterations'}.
            Defines whether scale_fn is evaluated on
            cycle number or cycle iterations (training
            iterations since start of cycle).
            Default: 'cycle'
        last_batch_iteration (int): The index of the last batch. Default: -1
    Example:
         optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
         scheduler = torch.optim.CyclicLR(optimizer)
         data_loader = torch.utils.data.DataLoader(...)
         for epoch in range(10):
             for batch in data_loader:
                 scheduler.batch_step()
                 train_batch(...)
    .. _Cyclical Learning Rates for Training Neural Networks: https://arxiv.org/abs/1506.01186
    .. _bckenstler/CLR: https://github.com/bckenstler/CLR
    """

    def __init__(self, optimizer, base_lr=1e-3, max_lr=6e-3,
                 step_size=2000, mode='triangular', gamma=1.,
                 scale_fn=None, scale_mode='cycle', last_batch_iteration=-1):

        if not isinstance(optimizer, Optimizer):
            raise TypeError('{} is not an Optimizer'.format(
                type(optimizer).__name__))
        self.optimizer = optimizer

        if isinstance(base_lr, list) or isinstance(base_lr, tuple):
            if len(base_lr) != len(optimizer.param_groups):
                raise ValueError("expected {} base_lr, got {}".format(
                    len(optimizer.param_groups), len(base_lr)))
            self.base_lrs = list(base_lr)
        else:
            self.base_lrs = [base_lr] * len(optimizer.param_groups)

        if isinstance(max_lr, list) or isinstance(max_lr, tuple):
            if len(max_lr) != len(optimizer.param_groups):
                raise ValueError("expected {} max_lr, got {}".format(
                    len(optimizer.param_groups), len(max_lr)))
            self.max_lrs = list(max_lr)
        else:
            self.max_lrs = [max_lr] * len(optimizer.param_groups)

        self.step_size = step_size

        if mode not in ['triangular', 'triangular2', 'exp_range'] \
                and scale_fn is None:
            raise ValueError('mode is invalid and scale_fn is None')

        self.mode = mode
        self.gamma = gamma

        if scale_fn is None:
            if self.mode == 'triangular':
                self.scale_fn = self._triangular_scale_fn
                self.scale_mode = 'cycle'
            elif self.mode == 'triangular2':
                self.scale_fn = self._triangular2_scale_fn
                self.scale_mode = 'cycle'
            elif self.mode == 'exp_range':
                self.scale_fn = self._exp_range_scale_fn
                self.scale_mode = 'iterations'
        else:
            self.scale_fn = scale_fn
            self.scale_mode = scale_mode

        self.batch_step(last_batch_iteration + 1)
        self.last_batch_iteration = last_batch_iteration

    def batch_step(self, batch_iteration=None):
        """
        questa funzione e richiamata all nell loop di training
        quando eseguo le diverse iterazioni

        :param batch_iteration: valore int del numero batch_size
        :return:
        """

        if batch_iteration is None:
            batch_iteration = self.last_batch_iteration + 1
        self.last_batch_iteration = batch_iteration
        for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
            param_group['lr'] = lr

    # metodi che possono essere scelti per cambiare
    #la scelta progeressiva del lr
    def _triangular_scale_fn(self, x):
        return 1.

    def _triangular2_scale_fn(self, x):
        return 1 / (2. ** (x - 1))

    def _exp_range_scale_fn(self, x):
        return self.gamma**(x)

    def get_lr(self):

        """
           funzione richiamata da batch_step che calcola per ogni batch_size
           il valore del lr che causa una variazione nella loss maggiore

           :return:
        """
        step_size = float(self.step_size)
        cycle = np.floor(1 + self.last_batch_iteration / (2 * step_size))
        x = np.abs(self.last_batch_iteration / step_size - 2 * cycle + 1)

        lrs = []
        param_lrs = zip(self.optimizer.param_groups, self.base_lrs, self.max_lrs)
        for param_group, base_lr, max_lr in param_lrs:
            base_height = (max_lr - base_lr) * np.maximum(0, (1 - x))
            if self.scale_mode == 'cycle':
                lr = base_lr + base_height * self.scale_fn(cycle)
            else:
                lr = base_lr + base_height * self.scale_fn(self.last_batch_iteration)
            lrs.append(lr)
        return lrs

def training(dataset_path, folder_save):

    array=[1,2,3,4]

    for i in array:

        #Data preparation
        MODE="train{}".format(i)
        data_transforms = {
            MODE: transforms.Compose([
                transforms.RandomCrop(size=(64,64)),
                transforms.Grayscale(num_output_channels=1),
                transforms.ToTensor()
            ]),
        }

        #data_dir="/media/velab/dati/Difetti/DatasetPalline/dataset1/"
        data_dir="/media/velab/dati/Difetti/PallineScritte/64SizeNoBatchLinear/"

        dset = {x: ImageFolderWithPaths(os.path.join(data_dir, x), data_transforms[x])
             for x in [MODE]}

        dset_loaders = {x: torch.utils.data.DataLoader(dset[x], batch_size=1,
                                                       shuffle=True, num_workers=4)
                        for x in [MODE]}

        #find_normalize(dset_loaders)
        #criterion= torch.nn.MSELoss()

        #criterion=torch.nn.L1Loss()
        #criterion=PercentileLoss()

        criterion = SSIM(data_range=1.0)

        # i risultati con la batch norm sono migliori rispetto al group norm
        #model = CasaeGroup()
        #model=Casae()

        #model=AutoencoderSSIM()

        model=simple_autoPrelu()

        #model= torch.load("/media/velab/dati/Difetti/PallineScritte/64SizeNoBatch/Model/{}/ConvAuto300.pth".format(MODE))

        optimizer=torch.optim.SGD(model.parameters(),lr=0.0001,momentum=0.9)

        scheduler = CyclicLR(optimizer)

        criterion.cuda()
        model.cuda()

        loss=None

        for epoch in range(0,250,1):
            train_loss = 0.0
            loss_final=0.0

            val_loss = 0.0
            val_loss_final = 0.0
            array_loss = []
            print("EPOCH {}".format(epoch))
            if training:
                model.train()
                for data in dset_loaders[MODE]:

                    scheduler.batch_step()

                    image,_,path=data

                    optimizer.zero_grad()

                    in_data= image.cuda()

                    out=model(in_data)

                    out=out.view(1,1,64,64)

                    loss=1- criterion(out,image.cuda())

                    array_loss.append(loss.item())

                    train_loss += loss.item()
                    loss.backward()

                    optimizer.step()

            train_loss = train_loss / len(dset_loaders[MODE])

            print("LOSS; {};".format(train_loss))

            if epoch%10==0:
                torch.save(model, "/media/velab/dati/Difetti/PallineScritte/64SizeNoBatchLinear/Model/train{}/ConvAuto{}.pth".format(i,epoch))
            else:
                #torch.save(model, "/media/velab/dati/Difetti/Texture/7/Random/Model/ConvAuto{}.pth".format(epoch))
                pass


import bpy
from bpy.props import BoolProperty

bl_info = {
    "name": "protected_delete_addon",
    "author": "1C0D",
    "version": (0, 0, 1),
    "blender": (2, 80, 0),
    "location": "View3D",
    "description": "protect from deleting with a pp in properties panel >object>visibility",
    "category": "Object",
}

bpy.types.Object.protected_delete = BoolProperty(
name = 'protected_delete',
default =False,
description='to apply to several obj from active-> right clic on checkbox > copy to selected',
)

class Delete_Override(bpy.types.Operator):
    """delete unprotected objects"""
    bl_idname = "object.delete_mod"
    bl_label = "Object Delete dd Operator"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        return context.mode=='OBJECT'

    def execute(self, context):

        override = bpy.context.copy()
        selected=bpy.context.selected_objects
        override['selected_objects'] = [obj for obj in selected if not obj.protected_delete]
        bpy.ops.object.delete(override)

        return {'FINISHED'}

class Delete_Override_Property_Select_Toggle(bpy.types.Operator):
        bl_idname = "object.del_override_toggle"
        bl_label = "Object Delete Operator"
        bl_options = {'REGISTER', 'UNDO'}
        bl_description= "to select all propected objects"

        def execute(self, context):

            bpy.ops.object.select_all(action='DESELECT')
            for obj in bpy.context.scene.objects:
                if obj.protected_delete:
                        obj.select_set(True)
                        context.view_layer.objects.active=obj

            return {'FINISHED'}

def draw_delete_override(self, context):
    layout = self.layout
    layout.use_property_split = True
    flow = layout.grid_flow(row_major=False, columns=0, even_columns=True, even_rows=False, align=False)

    obj = context.object
    col = flow.column()

    col.prop(obj, "protected_delete", text="protected_delete")
    row=layout.row()
    row.label(text='select all protected_delete')
    row.operator("object.del_override_toggle", text="",icon='ARROW_LEFTRIGHT')

addon_keymaps = []
def register():
    bpy.utils.register_class(Delete_Override_Property_Select_Toggle)
    bpy.types.OBJECT_PT_visibility.append(draw_delete_override)
    bpy.utils.register_class(Delete_Override)

    wm = bpy.context.window_manager

    km = wm.keyconfigs.addon.keymaps.new(name = 'Object Mode')
    kmi = km.keymap_items.new('object.delete_mod', 'DEL', 'PRESS',ctrl=True)

    addon_keymaps.append((km, kmi))

def unregister():

    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()
    bpy.utils.unregister_class(Delete_Override_Property_Select_Toggle)
    bpy.utils.unregister_class(Delete_Override)
    bpy.types.OBJECT_PT_visibility.remove(draw_delete_override)

if __name__ == "__main__":
    register()


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

import cchardet as chardet

class Encoding(object):

    def detectecEncoding(self, text):
        unicod = text
        detected = cchardet.detect(text)

    @staticmethod
    def clean(text):
        encoding = detectecEncoding(text)
        if (encoding != 'utf-8'):
            text = text.encode('utf-8')
        return text


from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

# Store form data
form_data = []

@app.route('/', methods=['GET', 'POST'])
def onboarding_form():
    global form_data
    if request.method == 'POST':
        form_data.append(request.form.to_dict())
        return redirect(url_for('results'))
    return render_template('form.html')

@app.route('/results')
def results():
    return render_template('results.html', form_data=form_data)

if __name__ == '__main__':
    import os
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)


from django.shortcuts import render

def homepage_view(request):
    return render(request, 'homepage.html', {
        'title': 'Django Ecommerce',
        'services': [{
            'name': 'Happy clients',
            'value': '1000+',
        }, {
            'name': 'Available products',
            'value': '2000+'
        }, {
            'name': 'Reviews',
            'value': '4.9/5'
        }]
    })


import os
import sqlite3
from random import choice
from string import ascii_letters, digits
from datetime import datetime
from dotenv import load_dotenv

load_dotenv()
database_file = os.getenv('database')

def current_time():
    return datetime.now().strftime('%d/%m/%Y %H:%M:%S')

def random_id():
    return ''.join(choice(ascii_letters + digits) for i in range(6))

def create_database():
    with sqlite3.connect(database_file) as connection:
        cursor = connection.cursor()
        cursor.execute('''CREATE TABLE IF NOT EXISTS warnings (
            id TEXT PRIMARY KEY,
            user_id INT,
            warning TEXT,
            warner TEXT,
            time TEXT
        );''')
        cursor.execute('''CREATE TABLE IF NOT EXISTS economy (
            user_id INT PRIMARY KEY,
            coins INT
        );''')
        connection.commit()

class Cosplayer:
    def __init__(self, user_id):
        self.user_id = user_id

    # economy
    async def create_wallet(self):
        with sqlite3.connect(database_file) as connection:
            try:
                cursor = connection.cursor()
                cursor.execute('''INSERT INTO economy (user_id, coins) VALUES (?, ?)''', (self.user_id, 0))
                connection.commit()
            except Exception as e:
                print(f'{current_time()} - Error: {e}')
                return 'error'
        return True

    async def get_coins(self):
        try:
            with sqlite3.connect(database_file) as connection:
                cursor = connection.cursor()
                results = False
                while not results:
                    cursor.execute('''SELECT * FROM economy WHERE user_id=?''', (self.user_id,))
                    results = cursor.fetchall()
                    if results:
                        return results[0][1]
                    else:
                        await self.create_wallet()
        except Exception as e:
            print(f'{current_time()} - Error: {e}')
            return 'error'

    async def update_coins(self, coins: int):
        try:
            with sqlite3.connect(database_file) as connection:
                old_coins = int(await self.get_coins())
                cursor = connection.cursor()
                cursor.execute('''UPDATE economy SET coins=? WHERE user_id=?''', (old_coins + coins, self.user_id))
                connection.commit()
            return True
        except Exception as e:
            print(f'{current_time()} - Error: {e}')
            return 'error'

    # warnings
    async def get_warnings(self):
        with sqlite3.connect(database_file) as connection:
            cursor = connection.cursor()
            cursor.execute('''SELECT * FROM warnings WHERE user_id=?''', (self.user_id,))
            results = cursor.fetchall()
        return results  # RETURN LIST HERE

    async def add_warning(self, warner: str, warning: str):
        try:
            with sqlite3.connect(database_file) as connection:
                cursor = connection.cursor()
                cursor.execute(
                    '''INSERT INTO warnings (id, user_id, warning, warner, time) VALUES (?, ?, ?, ?, ?)''', (
                        random_id(), self.user_id, warning, warner, current_time()
                    )
                )
                connection.commit()
            return True
        except Exception as e:
            print(f'{current_time()} - Error: {e}')
            return 'error'

    async def remove_warning(self, warning_number):
        try:
            with sqlite3.connect(database_file) as connection:
                cursor = connection.cursor()
                cursor.execute('''DELETE FROM warnings WHERE id=?''', (warning_number,))
                connection.commit()
            return True
        except Exception as e:
            print(f'{current_time()} - Error: {e}')
            return 'error'

    async def reset_warnings(self):
        try:
            with sqlite3.connect(database_file) as connection:
                cursor = connection.cursor()
                cursor.execute('''DELETE FROM warnings WHERE user_id=?''', (self.user_id,))
                connection.commit()
            return True
        except Exception as e:
            print(f'{current_time()} - Error: {e}')
            return 'error'

create_database()


# from pudb import set_trace; set_trace()
from typing import List, Set, Tuple
from itertools import product, chain
from collections import Counter, combinations

class Solution1:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        """LeetCode 90

        This is a cheating method. We basically use combinations to get all
        combinations from nums. Then use set to sieve out the duplicates, and
        then add the non-duplicate combinations to the result.

        40 ms, 51% ranking.
        """
        res = [[], nums]
        for size in range(1, len(nums)):
            res += [list(s) for s in set(tuple(sorted(c)) for c in combinations(nums, size))]
        return res

class Solution2:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        """The same idea as Solution1, but we are writing the combination part
        by hand.

        40 ms.
        """
        nums.sort()

        def comb(idx: int) -> Set[Tuple]:
            if idx == len(nums) - 1:
                return {(nums[idx],)}
            remains = comb(idx + 1)
            temp = {(nums[idx], *tup) for tup in remains}
            temp.add((nums[idx],))
            return remains.union(temp)

        return [list(tup) for tup in comb(0)] + [[]]

class Solution3:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        """This must be the smart way. I had this solution more than two years
        ago. I think this is also the standard method to obtain combinations of
        an array of ALL sizes.

        36 ms
        """
        counter = Counter(nums)
        res = [[]]
        for k, v in counter.items():
            for i in range(len(res)):
                for j in range(1, v + 1):
                    res.append(res[i] + [k] * j)
        return res

class Solution4:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        """One liner. Note the smart use of `chain`. If an empty list is in a
        chain, it will be combined with other lists.

        Reference: https://leetcode.com/problems/subsets-ii/discuss/1380314/Python-Oneliner-with-product-explained
        """
        return [list(chain(*p)) for p in product(*[[[k] * i for i in range(v + 1)] for k, v in Counter(nums).items()])]

class Solution5:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        """Use bit mask to identify which values in nums need to be included
        for each entry in the final power set.

        O(2^N * N)
        """
        n = len(nums)
        nums.sort()
        res = set()
        for mask in range(1 << n):
            temp = []
            for i in range(n):
                if (1 << i) & mask:  # check if the i-th bit in the mask is 1
                    temp.append(nums[i])
            res.add(tuple(temp))
        return [list(tup) for tup in res]

sol = Solution5()
tests = [
    ([1, 2, 2], [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]),
    ([0], [[], [0]]),
    ([1, 2, 3], [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]),
]

for i, (nums, ans) in enumerate(tests):
    res = sol.subsetsWithDup(nums)
    if sorted(res) == sorted(ans):
        print(f'Test {i}: PASS')
    else:
        print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')


from pages.comparisonPage.comparison_page import ComparisonPage
from utilities.status import Status
import unittest
import pytest
from ddt import ddt, data, unpack
from utilities.read_data import getCSVData

@pytest.mark.usefixtures("oneTimeSetUp", "setUp")
class ComparisonTests(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def classSetup(self, oneTimeSetUp):
        self.cp = ComparisonPage(self.driver)
        self.ts = Status(self.driver)

    # Need to verify two verification points
    # 1 fails, code will not go to the next verification point
    # If assert fails, it stops current test execution and
    # moves toaceEmail() the next test method

    # @pytest.mark.run(order=1)
    # def test_comparison_package(self):
    #     self.cp.closeCookie()
    #     self.cp.GoToPackageAndTab("Stor", "Viaplay")
    #     result = self.cp.verify_comparison_title()
    #     self.ts.mark(result, "verify package title")
    #     result2 = self.cp.verify_comparison_description()
    #     self.ts.mark(result2, "Verify package description")
    #
    # @pytest.mark.run(order=2)
    # def test_comparison_package2(self):
    #     self.cp.GoToPackageAndTab("Bas","Playtjänster")
    #     result = self.cp.verify_comparison_title()
    #     self.ts.mark(result, "verify package title")
    #     result2 = self.cp.verify_comparison_description()
    #     self.ts.markFinal("Test_comparison_nave", result2, "verify package description")
    #

    @pytest.mark.run(order=2)
    def test_comparison_page(self):
        # self.cp.closeCookie()
        self.cp.GoToPackageAndTab("Stor", "Tv-kanaler")
        result = self.cp.verify_comparison_title()
        self.ts.mark(result, "verify package title")
        result2 = self.cp.verify_comparison_description()
        self.ts.mark(result2, "verify package title")
        self.cp.openAllLogos()
        self.ts.markFinal("Test_comparison_nave", result2, "verify package description")

    # @pytest.mark.run(order=1)
    # def test_comparison_page_all(self):
    #     self.cp.closeCookie()
    #     self.cp.openAllPackagesTabsAndLogos()
    #


from .models import *
from .forms import *
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

@login_required
def account_redirect(request):
    return redirect('account-landing', pk=request.user.pk, name=request.user.username)

def post_list(request):
    posts = Post.objects.all().order_by('-date_created')
    return render(request, 'reddit/post_list.html',  {'posts': posts})

@login_required
def post_new(request):
    context = {}
    if request.method == "POST":
        form = PostForm(request.POST,request.FILES)
        if form.is_valid():

            post = form.save(commit=False)
            post.submitter = request.user
            post.save()

            for subreddit_id in request.POST.getlist('subreddits'):
                SubRedditPost.objects.create(subreddit_id=subreddit_id, post=post)
            return redirect('post_detail', pk=post.pk)

    else:
        form = PostForm()
        context['form'] = form
    return render(request, 'reddit/post_edit.html', {'form': form, 'is_create': True})

@login_required
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post, initial={'subreddits' : post.subreddits.all()})
    return render(request, 'reddit/post_edit.html', {'form': form, 'is_create': False})

def post_detail(request, pk):

    post = get_object_or_404(Post, pk=pk)
    return render(request, 'reddit/post_detail.html', {'post': post})

def sub_detail(request, pk):
    sub = get_object_or_404(SubReddit, pk=pk)
    return render(request, 'reddit/sub_detail.html', {'sub': sub})

@login_required
def add_comment(request, pk, parent_pk=None):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user
            comment.parent_id = parent_pk
            comment.save()
        return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'reddit/add_comment.html', {'form': form})

# Create your views here.
def home_view(request):
    context = {}
    context['form'] = PostForm()
    return render( request, "base.html", context)

def display_images(request):

    if request.method == 'GET':

        # getting all the objects of hotel.
       post = Post.objects.all()
       return render((request, 'post_detail.html',
                     {'images' : post}))


# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import logging
import datetime as dt
from src.utils.utils import Rsummary
from unidecode import unidecode
from pathlib import Path

# global variable
regions = ["national", "auvergne_rhone_alpes", "bourgogne_franche_comte", "bretagne", "centre_val_de_loire",
           "grand_est", "haut_de_france", "ile_de_france", "normandie", "nouvelle_aquitaine",
           "occitanie", "paca", "pays_de_loire"]
map_regions_encoding = {region: region_encoding for region, region_encoding in
                        zip(regions, ["", "Auvergne-Rhône-Alpes_", "Bourgogne-Franche-Comté_",
                                      "Bretagne_", "Centre-Val-de-Loire_",
                                      "Grand-Est_", "Hauts-de-France_", "Ile-de-France_",
                                      "Normandie_", "Nouvelle-Aquitaine_",
                                      "Occitanie_", "PACA_", "Pays-de-la-Loire_"])}

def read_load(eco2mix_load_path):
    eco2mix_load = pd.read_csv(eco2mix_load_path, encoding='windows-1252', sep="\t", engine="python",
                               usecols=['Date', 'Heures', 'Consommation'], index_col=False)
    eco2mix_load["minutes"] = eco2mix_load["Heures"].str[3:5]
    eco2mix_load = eco2mix_load[eco2mix_load["minutes"].isin(['00', '30'])].reset_index(drop=True)
    eco2mix_load["Date"] = pd.to_datetime(eco2mix_load["Date"], utc=True, dayfirst=True)
    return eco2mix_load

def create_nat_or_reg_data_load(region):
    print('\t' + region)
    logging.info(region)
    region_encoding = map_regions_encoding[region]
    eco2mix_load_real_time = read_load(
        eco2mix_load_path="Inputs/rte/eCO2mix_RTE_" + unidecode(region_encoding) + "En-cours-TR.xls")
    eco2mix_load_consolidated = read_load(
        eco2mix_load_path="Inputs/rte/eCO2mix_RTE_" + region_encoding + "En-cours-Consolide.xls")
    nat_or_reg_data_load = pd.concat([eco2mix_load_real_time, eco2mix_load_consolidated], ignore_index=True)

    year = 2012 if region == "national" else 2013
    eco2mix_load_path_root = "Inputs/rte/conso_nat/" if region == "national" else "Inputs/rte/conso_regions/" + region + "/"
    while Path(eco2mix_load_path_root + "eCO2mix_RTE_" + region_encoding + "Annuel-Definitif_" + str(
            year) + ".xls").is_file():
        eco2mix_load_final = read_load(
            eco2mix_load_path=eco2mix_load_path_root + "eCO2mix_RTE_" + region_encoding + "Annuel-Definitif_" + str(
                year) + ".xls")
        nat_or_reg_data_load = pd.concat([nat_or_reg_data_load, eco2mix_load_final],
                                         ignore_index=True)
        year = year + 1

    nat_or_reg_data_load = nat_or_reg_data_load.rename(
        columns={"Date": "date_jour", "Heures": "heures", "Consommation": "Load"})
    nat_or_reg_data_load["date"] = nat_or_reg_data_load["date_jour"].astype(str).str[:11] + nat_or_reg_data_load[
        "heures"]
    nat_or_reg_data_load = nat_or_reg_data_load.loc[:, ["date", "Load"]]
    nat_or_reg_data_load["date"] = pd.to_datetime(nat_or_reg_data_load["date"], utc=True)
    nat_or_reg_data_load = nat_or_reg_data_load.sort_values(by='date').reset_index(drop=True)
    nat_or_reg_data_load.loc[nat_or_reg_data_load['Load'] == "ND", 'Load'] = np.nan
    nat_or_reg_data_load['Load'] = pd.to_numeric(nat_or_reg_data_load['Load'])
    return nat_or_reg_data_load

def interpolation_data_load(data, var='Load'):
    #logging.info("avant interpolation")
    #Rsummary(data[[var]])

    data_interpolated_one_na = data.copy()
    data_interpolated_one_na[var + "_lag1"] = data_interpolated_one_na[var].shift(1)
    data_interpolated_one_na[var + "_lead1"] = data_interpolated_one_na[var].shift(-1)

    data_interpolated_one_na[var + "_interpolate"] = data_interpolated_one_na[var].interpolate(method='linear', limit=1,
                                                                                limit_direction="both")

    mask_only_one_consecutive_na = (data_interpolated_one_na[var + "_lag1"].notna()) & (
        data_interpolated_one_na[var].isna()) & (data_interpolated_one_na[var + "_lead1"].notna())
    data_interpolated_one_na.loc[mask_only_one_consecutive_na, var] = data_interpolated_one_na.loc[
        mask_only_one_consecutive_na, var + '_interpolate']

    del (data_interpolated_one_na[var + "_lag1"], data_interpolated_one_na[var + "_lead1"],
         data_interpolated_one_na[var + "_interpolate"])

    #logging.info("après interpolation")
    #Rsummary(data_interpolated_one_na[[var]])

    return data_interpolated_one_na

def process_rte_raw_datas(date_start=dt.datetime(2013, 1, 1, 0, 0, 0, tzinfo=dt.timezone.utc),
                          date_end=dt.datetime(2023, 2, 1, 0, 0, 0, tzinfo=dt.timezone.utc),
                          return_data_load=False):
    """
    A function used to process the eco2mix Power-related data (*) (real-time, consolidated and final): this data
    concerns France, its 12 administrative regions and its 21 metropolitan areas.
        real-time data : real-time smart devices data + estimation when data non available
        consolidated data : rte and enedis measured data (available after one month)
        final data : rte and local distribution entities (enedis, municipalities, etc.) measured data (available after
        one year)
    The "Load" or "Load_region" extracted is the national or regional power-related french electricity consumption,
    unit of measure = MW
    (*) https://www.rte-france.com/en/eco2mix/download-indicators

    ...

    Parameters
    ----------
    date_start : datetime64[ns, tz], optional
        The beginning date of the rte dataset (default is dt.datetime(2013, 1, 1, 0, 0, 0, tzinfo=dt.timezone.utc))
    date_end : datetime64[ns, tz], optional
        The end date of the rte dataset (default is dt.datetime(2023, 12, 31, 23, 30, 0, tzinfo=dt.timezone.utc))
    return_data_load : bool, optional
        (default is False)

    Returns
    -------
    Feather file
        rte.feather
    DataFrame, optional
        rte data_load

    """
    print("Processing the electricity load data:")
    logging.info("ELECTRICITY DATA LOADING")

    data_load = pd.DataFrame()
    for region in regions:
        nat_or_reg_data_load = create_nat_or_reg_data_load(region=region)

        nat_or_reg_data_load = interpolation_data_load(nat_or_reg_data_load)

        nat_or_reg_data_load.columns = ['date', 'Load'] if region == "national" else ['date', 'Load_' + region]

        nat_or_reg_data_load = nat_or_reg_data_load[
            ((nat_or_reg_data_load['date'] >= date_start) &
             (nat_or_reg_data_load['date'] <= date_end))]

        data_load = nat_or_reg_data_load if region == "national" else data_load.merge(
            nat_or_reg_data_load, on="date", how='left')

    Rsummary(data_load)

    # save rte data
    data_load.to_feather("Outputs/rte.feather")

    if return_data_load:
        return data_load


from typing import List, Tuple

def weighted_interval_scheduling(jobs: List[Tuple[int, int, int]]) -> int:
    # sort jobs by finish time
    jobs.sort(key=lambda x: x[1])

    n = len(jobs)
    dp = [0] * n
    dp[0] = jobs[0][2]

    for i in range(1, n):
        # find the latest job that doesn't conflict with the current job
        j = i - 1
        while j >= 0 and jobs[j][1] > jobs[i][0]:
            j -= 1

        # include the current job or not
        include = jobs[i][2]
        if j >= 0:
            include += dp[j]

        dp[i] = max(include, dp[i - 1])

    return dp[-1]

if __name__ == '__main__':
    num_tests = int(input())
    for _ in range(num_tests):
        n = int(input())
        jobs = []
        for _ in range(n):
            i, j, k = map(int, input().split())
            jobs.append((i, j, k))
        print(weighted_interval_scheduling(jobs))


import os
from flask import Flask, Blueprint, jsonify, request, render_template, redirect, url_for, flash, jsonify
from flask.ext.sqlalchemy import SQLAlchemy

root = os.path.abspath(os.path.dirname(__file__))
abt = Blueprint('abt', __name__,
    template_folder=os.path.join(root, 'templates'),
    static_folder=os.path.join(root, 'static'),
    url_prefix='/abt'
)

@abt.route('/')
def index():
    return 'insert beautiful UI here'

def getStats(Test, Experiment):
    result = {'tests' : []}
    for test in Test.query.all():
        test_res = {'test_name' : test.name, 'experiments': []}
        experiments = Experiment.query.filter_by(test_id = test.test_id).all()
        for exp in experiments:
            if exp.convert > 0.0:
                convert_rate =((exp.convert*1.0)/exp.participation)*100
            else:
                convert_rate = "inf"
            test_res['experiments'].append({'experiment': exp.experiment, 'participation': exp.participation, 'converts': exp.convert, 'convert_rate': convert_rate})

        result['tests'].append(test_res)
    return jsonify(result)

def mostConverts(Test, Experiment):
    result = {'tests' : []}
    for test in Test.query.all():
        test_res = {'test name' : test.name, 'winner': None, 'max convert rate': 0}
        experiments = Experiment.query.filter_by(test_id = test.test_id).all()
        max_convert_rate = 0.0
        best_exp_res = {}
        for exp in experiments:
            if exp.convert > 0.0:
                convert_rate =((exp.convert*1.0)/exp.participation)*100
            else:
                continue
            if convert_rate > max_convert_rate:
                max_convert_rate = convert_rate
                best_exp_res = {'experiment': exp.experiment, 'participation': exp.participation, 'converts': exp.convert, 'convert_rate': convert_rate}
        test_res['max convert rate'] = max_convert_rate
        test_res['winner'] = best_exp_res
        result['tests'].append(test_res)

    return jsonify(result)


from datetime import datetime
from datetime import timedelta
from celery.schedules import crontab
from IGitt.Interfaces.Actions import MergeRequestActions
from IGitt.Interfaces.MergeRequest import MergeRequest, MergeRequestStates
from IGitt.Interfaces.Repository import Repository

from gitmate.utils import lock_igitt_object
from gitmate_hooks.utils import ResponderRegistrar

@ResponderRegistrar.scheduled_responder(
    'pr_stale_reminder', crontab(minute='0', hour='6,18'), is_active=True)
def add_stale_label_to_merge_requests(
        repo: Repository,
        pr_expire_limit: int = 'Expiry limit in no. of day for pull requests',
        stale_label: str = 'Label to be used for marking stale'
):
    """
    Assigns the chosen label to pull requests which haven't been updated in a
    certain period of time.
    """
    minimum_pr_update_time = (datetime.now() -
                              timedelta(days=pr_expire_limit)).date()
    for pr in repo.search_mrs(
            updated_before=minimum_pr_update_time,
            state=MergeRequestStates.OPEN,
    ):
        with lock_igitt_object('label mr', pr):
            if stale_label not in pr.labels:
                pr.labels = pr.labels | {stale_label}

@ResponderRegistrar.responder(
    'pr_stale_reminder',
    MergeRequestActions.LABELED,
    MergeRequestActions.UNLABELED,
    MergeRequestActions.REOPENED,
    MergeRequestActions.SYNCHRONIZED,
    MergeRequestActions.COMMENTED,
    MergeRequestActions.MERGED
)
def remove_stale_label_from_merge_requests(
        pr: MergeRequest,
        *args,
        stale_label: str = 'Label to be used for marking stale'
):
    """
    Unassigns the chosen label from pull requests when they are updated again.
    """
    if len(args) > 0 and args[0] == stale_label:
        # LABELED and UNLABELED events return the label used, skip action if
        # the label was ``stale_label``
        return

    with lock_igitt_object('label mr', pr):
        pr.labels = pr.labels - {stale_label}


# from pudb import set_trace; set_trace()
from typing import List
import math

class Solution:
    def findMaxFish(self, grid: List[List[int]]) -> int:
        """This is equivalent to find all the cliques and compute the
        total sum of the nodes. Then find the largest sum among the cliques.

        O(MN), 262 ms, faster than 95.26%
        """
        M, N = len(grid), len(grid[0])

        def dfs(i: int, j: int) -> int:
            res = grid[i][j]
            grid[i][j] = 0
            for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
                ni, nj = i + di, j + dj
                if 0 <= ni < M and 0 <= nj < N and grid[ni][nj]:
                    res += dfs(ni, nj)
            return res

        res = 0
        for i in range(M):
            for j in range(N):
                if grid[i][j]:
                    res = max(res, dfs(i, j))
        return res

# sol = Solution2()
# tests = [
#     ("hello", "holle"),
#     ("leetcode", "leotcede"),
# ]

# for i, (s, ans) in enumerate(tests):
#     res = sol.reverseVowels(s)
#     if res == ans:
#         print(f'Test {i}: PASS')
#     else:
#         print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')


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

from bs4 import BeautifulSoup
from termcolor import colored

def make_the_soup(filename):
    """Read an xml document and return its content as a bs4 object

    :param filename: filename
    :return: content of the document or False
    :rtype: bs4.BeautifulSoup or boolean
    """
    # Parsing the XML file with lxml-xml used to work but no longer does
    # We are therefore parsing the input file with HTML (lxml)
    # which implies the following modifications:
    # - empty elements are open/close
    # - attributes are all lowercase
    # - the tree begins with extra html/body/... elements
    try:
        with open(filename, "r") as f:
            soup = BeautifulSoup(f, "lxml")
    except Exception as e:
        print(colored("Error", "red", attrs=["bold"]), e)
        soup = False
    return soup

def make_out_filenames(name_input, name_output=False):
    """Create output file names

    :param name_input: filename
    :type name_input: string
    :param name_output: filename
    :type name_output: string or Boolean
    :return: filenames
    :rtype: tuple
    """
    if not name_output:
        nin = name_input.split(".")
        out_xml = str(nin[0]) + "_out.xml"
        out_guard = str(nin[0]) + "_guard.xml"
        out_txt = str(nin[0]) + ".txt"
    else:
        nout = name_output[0].split(".")
        out_xml = str(nout[0]) + ".xml"
        out_guard = str(nout[0]) + "_guard.xml"
        out_txt = str(nout[0]) + ".txt"
    return out_xml, out_guard, out_txt

def write_output(filename, content):
    """Write strings into documents

    :param filename: filename
    :type filename: string
    :param content: file content
    :type content: string
    """
    with open(filename, "w") as f:
        f.write(content)

def make_string(soup):
    """ Transform a parsed XML tree into a string

    :param soup: parsed XML tree
    :type soup: bs4.BeautifulSoup
    :rtype: string
    """
    s = str(soup.prettify())
    return s


import logging
import os

from datetime import timedelta
from hashlib import md5
from mimetypes import guess_extension
from urllib.parse import quote, unquote_plus

from django.conf import settings
from django.utils import timezone

from .browser import RequestBrowser
from .html_asset import HTMLAsset
from .url import sanitize_url
from .utils import http_date_format

logger = logging.getLogger('html_snapshot')

# https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#heuristic_caching
HEURISTIC_CACHE_THRESHOLD_PERCENT = 10
HTML_SNAPSHOT_HASH_LEN = 10

def max_filename_size():
    return os.statvfs(settings.SOSSE_HTML_SNAPSHOT_DIR).f_namemax

class CacheHit(Exception):
    def __init__(self, asset):
        self.asset = asset

class CacheMiss(Exception):
    pass

class CacheRefresh(Exception):
    def __init__(self, page):
        self.page = page

class HTMLCache():
    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#expires_or_max-age
    @staticmethod
    def _max_age_check(asset, max_file_size):
        if (asset.max_age and asset.last_modified) or asset.etag:
            if asset.max_age and asset.last_modified and asset.last_modified + timedelta(seconds=asset.max_age) >= timezone.now():
                logger.debug('cache hit, max_age')
                raise CacheHit(asset)
            else:
                logger.debug('cache miss, max_age, %s + %s > %s', asset.last_modified, asset.max_age, timezone.now())

                # https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#validation
                if not asset.has_cache_control:
                    logger.debug('cache miss, max_age, no cache control')
                    raise CacheMiss()

                headers = {
                    'Accept': '*/*',
                    'If-Modified-Since': http_date_format(asset.download_date)
                }

                if asset.etag:
                    headers['If-None-Match'] = asset.etag

                page = RequestBrowser.get(asset.url,
                                          check_status=True,
                                          max_file_size=max_file_size,
                                          headers=headers)

                if page.status_code == 304:
                    # http not modified
                    logger.debug('cache hit, max_age, cache control, not modified')
                    raise CacheHit(asset)

                logger.debug('cache refresh, max_age, cache control, refresh')
                raise CacheRefresh(page)

    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#heuristic_caching
    @staticmethod
    def _heuristic_check(asset):
        if asset.download_date and asset.last_modified:
            dt = asset.download_date - asset.last_modified
            dt = (dt.total_seconds() * HEURISTIC_CACHE_THRESHOLD_PERCENT) / 100
            dt = timedelta(seconds=dt)
            if asset.download_date + dt > timezone.now():
                logger.debug('cache hit, heuristic_caching')
                raise CacheHit(asset)
            else:
                logger.debug('cache miss, heuristic_caching, %s + %s < %s', asset.download_date, dt, timezone.now())
                raise CacheMiss()

    @staticmethod
    def _cache_check(url, max_file_size):
        asset = HTMLAsset.objects.filter(url=url).order_by('download_date').last()

        if not asset:
            logger.debug('cache miss, asset does not exist')
            raise CacheMiss()

        if asset.download_date is None:
            logger.debug('cache miss, force refresh')
            raise CacheMiss()

        HTMLCache._max_age_check(asset, max_file_size)
        HTMLCache._heuristic_check(asset)
        logger.debug('cache miss, cache outdated')
        raise CacheMiss()

    @staticmethod
    def download(url, max_file_size):
        try:
            HTMLCache._cache_check(url, max_file_size)
        except CacheHit as e:
            e.asset.increment_ref()
            raise
        except CacheRefresh as e:
            return e.page
        except CacheMiss:
            pass

        page = RequestBrowser.get(url,
                                  check_status=True,
                                  max_file_size=max_file_size,
                                  headers={'Accept': '*/*'})
        return page

    @staticmethod
    def create_cache_entry(url, filename, page=None):
        asset, created = HTMLAsset.objects.get_or_create(url=url, filename=filename)

        if created:
            asset.init_ref_count()

        asset.increment_ref()

        if page:
            asset.update_from_page(page)

        return asset

    @staticmethod
    def write_asset(url, content, page, extension=None, mimetype=None):
        assert isinstance(content, bytes)

        logger.debug('html_write_asset for %s', url)
        _hash = md5(content).hexdigest()[:HTML_SNAPSHOT_HASH_LEN]

        # Build the extension using mimetypes, because the appropriate extension
        # is required by Nginx when the file is served statically
        if extension is None:
            assert mimetype is not None
            extension = guess_extension(mimetype)
            if extension is None:
                _, ext = url.rsplit('.', 1)
                if '?' in ext:
                    ext, _ = ext.split('?', 1)

                if '/' in ext:
                    extension = '.bin'
                else:
                    extension = f'.{ext}'

        url = sanitize_url(url)
        filename_url = HTMLCache.html_filename(url, _hash, extension)
        dest = os.path.join(settings.SOSSE_HTML_SNAPSHOT_DIR, filename_url)
        dest_dir, _ = dest.rsplit('/', 1)
        os.makedirs(dest_dir, 0o755, exist_ok=True)

        with open(dest, 'wb') as fd:
            fd.write(content)

        return HTMLCache.create_cache_entry(url, filename_url, page)

    @staticmethod
    def html_filename(url, _hash, extension):
        assert extension.startswith('.')

        # replace http:// by http:/
        url = url.replace('//', '/')

        # Unquote before requoting
        url = unquote_plus(url)
        # Replace % by , to prevent interpration of the escape by nginx
        url = quote(url).replace('%', ',')

        # Make sure the filename is not longer than supported by the filesystem
        parts = url.split('/')
        _parts = []
        for no, part in enumerate(parts):
            if no == len(parts) - 1:
                max_len = max_filename_size() - HTML_SNAPSHOT_HASH_LEN - len(extension) - 1
                if len(part) > max_len:
                    part = part[:max_len]
                part = f'{part}_{_hash}{extension}'
            else:
                if len(part) > max_filename_size():
                    part = part[:max_filename_size() - HTML_SNAPSHOT_HASH_LEN - 1]
                    part = f'{part}_{_hash}'

            _parts.append(part)
        return '/'.join(_parts)


import psycopg2
import time
import sys
import stomp
import threading
import datetime
from commons import RandomUtils
from db_tools.postgresql import PG_Client

def readNewMessageFile():
    tag_info = PG_Client.query('select i_id from tag_info ORDER BY RANDOM() LIMIT 1 ', None);
    print(' rows : ', tag_info);
    msg = newMessageData(tag_info);
    print(' msg : ' , msg)

    with open('E:\projects\五系统一中心\mq_test\Win32\Win32\\001', 'w+') as f:
        f.write(str(msg));
        print(f.read())

def createNewMessage(count):
    allMsg = '';
    for i in range(count) :
        # 数据库查询
        tag_info = PG_Client.query('select i_id from tag_info ORDER BY RANDOM() LIMIT 1 ', None);
        print(' rows : ', tag_info);

        # 获取当前时间戳
        dtime = datetime.datetime.now()
        ans_time = int(time.mktime(dtime.timetuple())) * 1000;

        id = tag_info['i_id'];
        # id = 1111;
        # 图层
        emapArr = ['egms_369857' , 'egms_130738'];
        layer_name = RandomUtils.getRamdonByArr(emapArr);

        key = "coordinate"

        # 随机坐标
        x = RandomUtils.getRandomBetweenNumbers(0, 61);
        y = RandomUtils.getRandomBetweenNumbers(-46, 0);
        val = str(x) + "," + str(y);

        msg = '{"cls":"tag","id":%(id)d,"layer_name":"%(layer_name)s","time":%(time)d,"key":"%(key)s","val":"%(val)s"}\n';
        msg = msg % {   'id' : id ,
                        'layer_name' : layer_name ,
                        'time' : ans_time ,
                        'key'  : key ,
                        'val' : val
                     }
        allMsg += msg;

    print(' msg : ' , allMsg)
    with open('E:\projects\五系统一中心\mq_test\Win32\Win32\\001', 'w+') as f:
        f.write(allMsg)
        # f.write(writeMsg);
        # print(f.read())

def newMessageData(tag_info):
    # 图层
    emapArr = ['egms_map' , 'egms_461253' , 'egms_090836' ];

    mqMessageData = {};
    mqMessageData['cls'] = "tag"
    mqMessageData['id'] = tag_info['i_id'];
    # 随机获取图层
    layer_name = RandomUtils.getRamdonByArr(emapArr);
    mqMessageData['layer_name'] = layer_name
    mqMessageData['time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    mqMessageData['key'] = "coordinate"
    # 随机坐标
    x = RandomUtils.getRandomBetweenNumbers( 0 , 200  );
    y = RandomUtils.getRandomBetweenNumbers( -120 , 0 );
    mqMessageData['val'] = str(x) + "," + str(y);

    # key = RandomUtils.getRandomBetweenNumbers(0 , 10);
    # if key == 1 :
    #     mqMessageData['key'] = "online"
    #     mqMessageData['val'] = "0"

    # 自定义人数
    # personArr = [ 131 ,601 , 378 , 420  , 592]
    # mqMessageData['id'] = RandomUtils.getRamdonByArr(personArr);
    # mqMessageData['id'] = 300; # 321 130 128 300 601
    # mqMessageData['layer_name'] = 'egms_110'
    # 直接定义图层
    # mqMessageData['layer_name'] = 'egms_090836'
    # mqMessageData['key'] = "online"
    # mqMessageData['val'] = "0"
    # mqMessageData['key'] = "coordinate"
    # mqMessageData['val'] = str(328) + "," + str(10);
    return mqMessageData;

def writeNewMessageFile():
    tag_info = PG_Client.query('select i_id from tag_info ORDER BY RANDOM() LIMIT 1 ', None);
    print(' rows : ', tag_info);
    msg = newMessageData(tag_info);
    print(' msg : ' , msg)

    with open('E:\projects\五系统一中心\mq_test\Win32\Win32\\001', 'a+') as f:
        writeMsg =str(msg) + '\r'
        f.writelines(writeMsg)
        # f.write(writeMsg);
        print(f.read())

# for i in range(100):
#     writeNewMessageFile();

createNewMessage(15);

# 获取当前时间戳
dtime = datetime.datetime.now()
ans_time = int(time.mktime(dtime.timetuple())) ;

print('ans_time : '  , ans_time)


from django.test import TestCase, RequestFactory
from django.urls import reverse

from continuing_education.api.serializers.address import AddressSerializer, AddressPostSerializer
from continuing_education.tests.factories.address import AddressFactory
from reference.tests.factories.country import CountryFactory

class AddressSerializerTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.country = CountryFactory()

        cls.address = AddressFactory(
            country=cls.country
        )
        url = reverse('continuing_education_api_v1:address-list-create')
        cls.serializer = AddressSerializer(cls.address, context={'request': RequestFactory().get(url)})

    def test_contains_expected_fields(self):
        expected_fields = [
            'uuid',
            'location',
            'postal_code',
            'city',
            'country'
        ]
        self.assertListEqual(list(self.serializer.data.keys()), expected_fields)

class AddressPostSerializerTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.country = CountryFactory()

        cls.address = AddressFactory(
            country=cls.country
        )
        url = reverse('continuing_education_api_v1:address-list-create')
        cls.serializer = AddressPostSerializer(cls.address, context={'request': RequestFactory().get(url)})

    def test_contains_expected_fields(self):
        expected_fields = [
            'uuid',
            'location',
            'postal_code',
            'city',
            'country'
        ]
        self.assertListEqual(list(self.serializer.data.keys()), expected_fields)

    def test_ensure_country_field_is_slugified(self):
        self.assertEqual(
            self.serializer.data['country'],
            self.country.iso_code
        )


import argparse
import os
import csv
from glob import glob
import codecs

if __name__ == "__main__":
	parser = argparse.ArgumentParser(
		description="Create normlized text files for each audio file in mailabs datasets.",
		formatter_class=argparse.ArgumentDefaultsHelpFormatter
	)
	parser.add_argument("--datasets_root", type=str, help="Path to the mailabs root directory (e.g. '/training_data/de_DE').")
	# overwrite
	# male, female, mix
	# audio format
	args = parser.parse_args()
	print(args.datasets_root)

	wav_folders = []

	print("Searching speakers...")

	# list female speakers
	speaker_search_dir_female = os.path.join(args.datasets_root, "by_book/female/*")  # modified by AVI
	female_speaker_dirs = glob(speaker_search_dir_female)
	print(female_speaker_dirs)

	# list male speakers
	speaker_search_dir_male = os.path.join(args.datasets_root, "by_book/male/*")  # modified by AVI
	male_speaker_dirs = glob(speaker_search_dir_male)

	# list mixed speakers
	speaker_search_dir_mix = os.path.join(args.datasets_root, "by_book/mix/*")  # modified by AVI
	mix_speaker_dirs = glob(speaker_search_dir_mix)

	all_speakers = female_speaker_dirs + male_speaker_dirs + mix_speaker_dirs
	print(all_speakers)

	# Check if speaker dirs have subfoders
	print("Checking subfolders...")
	for speaker in all_speakers:
		# get subfolders
		speaker_subfolders_search_dir = os.path.join(speaker, "*/")  # modified by AVI
		print(speaker_subfolders_search_dir)
		speaker_subfolders = glob(speaker_subfolders_search_dir)
		print(speaker_subfolders)

		# is subfolder a wavs dir?
		for speaker_subfolder in speaker_subfolders:
			last_folder = os.path.basename(os.path.normpath(speaker_subfolder))
			if last_folder == 'wavs':
				#print(speaker_subfolder + " is a wav folder")
				wav_folders.append(speaker_subfolder)
			else:
				# traverse further dirs
				speaker_subfolders_books_search_dir = os.path.join(speaker_subfolder, "*/")  # modified by AVI
				speaker_subfolders_books_dirs = glob(speaker_subfolders_books_search_dir)
				#print(speaker_subfolders_books_dirs)
				for speaker_subfolders_book in speaker_subfolders_books_dirs:
					last_folder = os.path.basename(os.path.normpath(speaker_subfolders_book))
					if last_folder == 'wavs':
						#print(speaker_subfolders_book + " is a wav folder")
						wav_folders.append(speaker_subfolders_book)

	print("Found " + str(len(wav_folders)) + " wav folders")
	print(wav_folders)

	# read metadata.csv
	file_count = 0
	for wav_folder in wav_folders:
		wav_folder_parent = os.path.dirname(os.path.dirname(wav_folder))
		metadata_path = os.path.join(wav_folder_parent, "metadata.csv")
		if os.path.exists(metadata_path):
			with open(metadata_path, newline='', encoding='utf-8') as csvfile:
				csv_reader = csv.reader(csvfile, delimiter='|')
				for row in csv_reader:
					txt_file_to_create = os.path.join(wav_folder, row[0] + ".txt")
					if os.path.exists(txt_file_to_create):
						print(txt_file_to_create + " already exists.")
					else:
						expected_wav_file = os.path.join(wav_folder, row[0] + ".wav")
						if os.path.exists(expected_wav_file):
							if len(row) > 1:
								print("All good. Creating " + txt_file_to_create)
								f = codecs.open(txt_file_to_create, "w", "utf-8")
								f.write(row[1])
								f.close()
								file_count +=1
							else:
								print("Metadat is corrupt" + str(row))
						else:
							print("Corresponding wav file " + expected_wav_file + " was not found.")
		else:
			print("Expected file to exist: " + metadata_path)

	print("Wrote " + str(file_count) + " files.")


from random import choice, random, randrange
from functools import reduce

import svgwrite as svg
import time
import numpy as np
from itertools import product
from copy import copy
from random import random
from math import acos, sqrt, pi, cos, sin
import shapely.geometry as geom
import shapely.affinity as aff
import bezier as bez
from copy import deepcopy

class Petal(dict):
    DEFAULT_PARMS = {
        'length': 0, # distance from base to tip
        'width': 0,  # width at base
        'lft_bot' : 0, # controls the fatness at the bottom left
        'lft_top' : 0, # controls the pointiness at the top left
        'rgt_top' : 0, # controls the pointiness at the top right
        'rgt_bot' : 0, # controls the fatness at the bottom right
        }
    LIMIT = 6.

    def __init__(self, length=0, width=0):
        #super().__init__(self.DEFAULT_PARMS)
        super().__init__()
        self['length'] = length
        self['width'] = width
        self.cache = None
        self.points = []

    def __repr__(self):
        res = ''
        for k in self:
            res += '%s\t\t%s\n'%(k, self[k])
        return res

    def check(self):
        return self['lft_bot'] < self['lft_top'] < self['rgt_top'] < self['rgt_bot']

    def _limit_(self):
        '''Do not allow taper control points to cross.
        The right control point must always be on the right or the line will self intersect
        '''
        tmp = [self['lft_bot'], self['lft_top'], self['rgt_top'], self['rgt_bot']]
        tmp.sort()
        self['lft_bot'], self['lft_top'], self['rgt_top'], self['rgt_bot'] = tmp

    def randomize(self):
        self['lft_bot'] = - self['width'] + random() * self['width'] * 2
        self['rgt_bot'] = self['lft_bot'] + random() * (self['width'] * 2 - abs(self['lft_bot']))
        self['lft_top'] = - self['width'] + random() * self['width'] * 2
        self['rgt_top'] = self['lft_top'] + random() * (self['width'] * 2 - abs(self['lft_top'])) + 0.2
        return self

    def random_split(self):
        p1 = copy(self)
        p2 = copy(self)
        for k in self:
            if k not in ('width', 'length'):
                split = random()
                p1[k] *= split
                p2[k] *= 1-split
        #print(p1, '\n', p2)
        return p1,p2

    def mag(self):
        return sqrt(self.dot(self))

    def dot(self, p):
        return sum(self[k]*p[k] for k in p if k not in ('width', 'length'))

    def angle_between(self, p):
        return acos(self.dot())

    def __add__(self, p):
        pr = copy(self)
        for k in self:
            if k not in ('width', 'length'):
                pr[k] += p[k]
        return pr
    def __sub__(self, p):
        pr = copy(self)
        for k in self:
            if k not in ('width', 'length'):
                pr[k] -= p[k]
        return pr
    def __mul__(self, f):
        pr = copy(self)
        for k in self:
            if k not in ('width', 'length'):
                pr[k] *= f
        return pr
    def __truediv__(self, f):
        pr = copy(self)
        for k in self:
            if k not in ('width', 'length'):
                pr[k] /= f
        return pr

    def _points(self):
        return [
                (-self['width']/2., 0), # Curve start point
                # curve control point - curve starts-off pointing towards this
                (self['lft_bot'], self['length']/2.),
                # curve control point - curve ends pointing away from this
                (self['lft_top'], self['length']/2.),
                # end point
                (0, self['length']),
                (self['rgt_top'], self['length']/2.),
                (self['rgt_bot'], self['length']/2.),
                (self['width']/2., 0),
                ]

    def to_svg(self):
        points = self._points()
        return ['M', points[0],
                'C', points[1], points[2], points[3],
                'C', points[4], points[5], points[6],
               ]

    def to_LineString(self, use_cache=True):
        flat = 0.1
        points = self._points()

        # if we're not using the cache or it hasn't been created yet or it's
        # out of date: regenerate it
        if not use_cache or self.cache is None or points != self.points:
            self.points = points
            side1 = points[:4]
            side2 = points[3:]
            self.cache = geom.LineString(side1[0:1] + [bz[3] for bz in bez.subdiv([side1], flat)] +
                                   #side2[0:1] +
                                   [bz[3] for bz in bez.subdiv([side2], 0.1)])

            p1 = self.cache.coords[0]
#            for p2 in self.cache.coords[1:]:
#                print('d: ' + str(sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)))
#                p1 = p2

        return self.cache

class PetalExtra(Petal):
    def randomize(self):
        '''Generates a curve with very few constraints and but only allows it to pass if it doesn't
        self intersect'''
        ok = False
        while not ok:
            a = random() * 2 * pi
            d = self['width'] * random()
            self['lft_bot_x'] = d * cos(a) - self['width'] / 2
            self['lft_bot_y'] = d * sin(a)

            a = random() * 2 * pi
            d = self['width'] * random()
            self['lft_top_x'] = d * cos(a)
            self['lft_top_y'] = d * sin(a) + self['length']

            a = random() * 2 * pi
            d = self['width'] * random()
            self['rgt_bot_x'] = d * cos(a) + self['width'] / 2
            self['rgt_bot_y'] = d * sin(a)

            a = random() * 2 * pi
            d = self['width'] * random()
            self['rgt_top_x'] = d * cos(a)
            self['rgt_top_y'] = d * sin(a) + self['length']

            ok = geom.Polygon(self.to_LineString()).is_valid
            print('.', end='')
        print('')
        return self

    def _points(self):
        return [
                (-self['width']/2., 0), # Curve start point
                # curve control point - curve starts-off pointing towards this
                (self['lft_bot_x'], self['lft_bot_y']),
                # curve control point - curve ends pointing away from this
                (self['lft_top_x'], self['lft_top_y']),
                # end point
                (0, self['length']),
                (self['rgt_top_x'], self['rgt_top_y']),
                (self['rgt_bot_x'], self['rgt_bot_y']),
                (self['width']/2., 0),
                ]

    def dist(self, p):
        return (p - self).mag()

    def too_big(self):
        return \
        self['lft_bot_x'] + self['width'] / 2 > self['width'] or \
        self['lft_bot_y'] > self['width'] or \
        self['lft_top_x'] > self['width'] or \
        self['lft_top_y'] - self['length'] > self['width'] or \
        self['rgt_bot_x'] - self['width'] / 2 > self['width'] or \
        self['rgt_bot_y'] > self['width'] or \
        self['rgt_top_x'] > self['width'] or \
        self['rgt_top_y'] - self['length'] > self['width']

    def find_nearby(self, min_dist=5, max_dist=10):
        new = deepcopy(self)

        ok = False
        while not ok or not min_dist < self.dist(new) < max_dist or new.too_big():
            a = random() * 2 * pi
            d = max_dist * random()
            new['lft_bot_x'] = self['lft_bot_x'] + d * cos(a)
            new['lft_bot_y'] = self['lft_bot_y'] + d * sin(a)

            a = random() * 2 * pi
            d = max_dist * random()
            new['lft_top_x'] = self['lft_top_x'] + d * cos(a)
            new['lft_top_y'] = self['lft_top_y'] + d * sin(a)

            a = random() * 2 * pi
            d = max_dist * random()
            new['rgt_bot_x'] = self['rgt_bot_x'] + d * cos(a)
            new['rgt_bot_y'] = self['rgt_bot_y'] + d * sin(a)

            a = random() * 2 * pi
            d = max_dist * random()
            new['rgt_top_x'] = self['rgt_top_x'] + d * cos(a)
            new['rgt_top_y'] = self['rgt_top_y'] + d * sin(a)

            ok = geom.Polygon(new.to_LineString()).is_valid
            print('.', end='')
        print('')
        return new

def occlude_petals(petals):
    polys = [geom.Polygon(p) for p in petals]

    occluded_petals = []
    for i, p in enumerate(petals[:-1]):
        for poly in polys[i+1:]:
            try:
                p = p.difference(poly)
            except:
                print('Error')
        occluded_petals.append(p)
    occluded_petals.append(petals[-1])

    polylines = []
    for p in occluded_petals:
        if type(p) not in (geom.linestring.LineString, geom.multilinestring.MultiLineString):
            print(type(p))
            continue
        if type(p) is geom.multilinestring.MultiLineString:
            for p2 in p:
                polylines.append(p2.coords)
        else:
            polylines.append(p.coords)

    polylines = [svg.shapes.Polyline(pts) for pts in polylines]

    return polylines

def variflower(x,y, npetals, petal):
    petal_scale = 0.2**(1./npetals)
    petal_angle = 137.5
    petals = [petal]
    for i in range(int(npetals)):
        petals.append(petals[-1].find_nearby(min_dist=5, max_dist=10))
    petals = [aff.translate(
                            aff.scale(
                                      aff.rotate(p.to_LineString(),
                                                 petal_angle*i, origin=(0,0)),
                                      petal_scale**i, petal_scale**i, origin=(0,0)),
                            x,y)
             for i, p in enumerate(petals)]
    return occlude_petals(petals)

def geom_flower_phi(x,y, npetals, petal):
    petal_scale = 0.2**(1./npetals)
    petal_angle = 137.5
    petals = [aff.translate(
                            aff.scale(
                                      aff.rotate(petal.to_LineString(),
                                                 petal_angle*i, origin=(0,0)),
                                      petal_scale**i, petal_scale**i, origin=(0,0)),
                            x,y)
             for i in range(int(npetals))]
    return occlude_petals(petals)

def flower(x,y, npetals, petal):
    petal_angle = 360 / npetals
    angle = 0
    ps = []
    for p in range(int(npetals)):
        angle += petal_angle
        path = svg.path.Path()
        path.push(petal.to_svg())
        path.rotate(angle, (x,y))
        path.translate(x,y)
        ps.append(path)
    return ps

def flower_sheet(npetals, x,y, radius, spacing=10):
    width = radius / 2

    petal = PetalExtra(length = radius / 2., width = width).randomize()
    #flower_fn = geom_flower_phi
    flower_fn = variflower

    ps = []
    for x,y in product(range(x), range(y)):
        petal = petal.find_nearby(min_dist=4, max_dist=7)
        ps += flower_fn((radius+spacing)*(x+1),
                         (radius+spacing)*(y+1),
                         npetals,
                         #PetalExtra(length = (radius - spacing) / 2., width = width).randomize())
                         #Petal(length = (radius - spacing) / 2., width = width).randomize())
                         #start + xv*x + yv*y)
                         petal)
    return ps

def draw(flower, drawing, color='black', line_width=1.):
    for petal in flower:
        petal.fill('none')
        drawing.add(petal)
        petal.stroke(color, width=line_width)

def draw_cnc(flowers, scale=1.):
    header = ['%',
            'G21 (All units in mm)',
            'G90 (All absolute moves)',
            ]
    footer = ['G00 X0.0 Y 0.0 Z0.0',
            'M2',
            '%',
            ]

    body = []
    for path in flowers:
        body.append('G01 Z15 F10000')
        first = True
        for pt in path.points:
            xy = ' X ' + format((pt[0]) * scale, '.4f') + \
                 ' Y ' + format((pt[1]) * scale, '.4f')
            if first:
                body.append('G01' + xy + ' F10000') # should be G00 but firware ignores F
                body.append('G01 Z34 F10000')
                first = False
            else:
                body.append('G01' + xy + ' F10000')

    return '\n'.join(header + body + footer)

def main():
    npetals = 10
    x,y = 10, 10
    radius = 60
    spacing = 10

    flowers = flower_sheet(npetals, x,y, radius, spacing)
    #flowers = variflower(0,0, npetals, PetalExtra(length = 200, width = 50).randomize())
    #flowers = geom_flower_phi(0,0, npetals)
    dwg = svg.Drawing('test.svg')
    draw(flowers, dwg, color='black', line_width=1.)
    #dwg.viewbox(minx=0, miny=0,
    dwg.viewbox(minx=-300, miny=-300,
                width=300+(radius+spacing)*(x+1), height=300+(radius+spacing)*(y+1))
    dwg.save()

    with open('test.cnc', 'w') as cnc:
        cnc.write(draw_cnc(flowers))

if __name__ == '__main__':
    t0 = time.time()
    main()
    t1 = time.time()

    print("Elapsed:", t1-t0)


import array
import random
import math

import pyglet

from .resources import get_image, TILE_WIDTH, HALF_FONT_INFO
from .util import pushed_matrix, UP, DOWN, LEFT, RIGHT
from .caterpillar import Caterpillar
from .coccoon import Cocoon
from .level import load_level_to_grid
from . import tiles

SPEED = 2

class Grid:
    def __init__(self, state, egg=None, level=0, ui=None):
        self.state = state
        self.ui = ui
        self.egg = egg
        self.width = 31
        self.height = 17
        self.tiles = {}
        self.caterpillar = None
        self.caterpillar_opacity = 255
        self.sprites = {}
        self.eol_tiles = []
        self.batch = pyglet.graphics.Batch()
        self.score_batch = pyglet.graphics.Batch()
        self.displayed_score = 0
        self.t = 0
        self.gameover_t = None
        self.total_score = 0
        self.score_labels = []
        self.cocoon = None
        self.done = False
        self.background = pyglet.image.TileableTexture. create_for_image(
            get_image('tile', 0, 0, 2, 2)
        )
        self.level = int(level)
        self.autogrow_flowers = True
        self.collected_sprites = {}
        if level == 0:
            self.add_caterpillar()
            self.init_level0()
        else:
            load_level_to_grid(level, self)

        self.main_score_label = pyglet.text.Label(
            f'',
            **HALF_FONT_INFO.label_args(),
            anchor_x='right',
            anchor_y='baseline',
            align='center',
            batch=self.score_batch,
            x=(self.width - .5) * TILE_WIDTH,
            y=(self.height - .5) * TILE_WIDTH + HALF_FONT_INFO.baseline,
        )
        self.gameover_label = pyglet.text.Label(
            f'',
            **HALF_FONT_INFO.label_args(),
            anchor_x='left',
            anchor_y='baseline',
            align='center',
            batch=self.score_batch,
            color=(255, 255, 255, 255),
            x=-.5 * TILE_WIDTH,
            y=(self.height - .5) * TILE_WIDTH + HALF_FONT_INFO.baseline,
        )
        if not self.level:
            self.gameover_label.text = 'Crash to form a cocoon.'.upper()

        self.t = 1
        if self.caterpillar is None:
            self.add_caterpillar()
        #self.add_cocoon(self.caterpillar) ## debug

    def add_caterpillar(self, x=None, y=None, direction=(1, 0)):
        self.caterpillar = Caterpillar(
            self, self.egg or self.state.choose_egg(),
            x=x, y=y, direction=direction,
        )

    def init_level0(self):
        for x in range(self.width):
            for y in range(self.height):
                if random.randrange(7) < 2:
                    self[x, y] = 'grass'

        for x, y in (0, 2), (11, 5), (17, 5):
            self[x, y] = 'flower'

        head_x, head_y = self.caterpillar.segments[-1].xy
        self[head_x - 1, head_y] = None
        self[head_x + 0, head_y] = None
        self[head_x + 1, head_y] = 'grass'
        self[head_x + 2, head_y] = 'grass'
        self[head_x + 2, head_y].grow_flower()
        self[head_x + 3, head_y] = None
        self[head_x + 0, head_y - 1] = 'grass'
        self[head_x + 0, head_y + 1] = 'grass'

        for i, d in enumerate((
            #RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT,
            #RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT, RIGHT,
            #DOWN, LEFT, UP,
            #DOWN, DOWN, LEFT, LEFT, UP, UP, LEFT, LEFT, LEFT, LEFT, DOWN, DOWN,
            #RIGHT, RIGHT, DOWN, DOWN, LEFT, LEFT, DOWN, DOWN, DOWN, DOWN,
            #RIGHT, RIGHT, UP, UP, RIGHT, RIGHT, #DOWN, DOWN, RIGHT, RIGHT, RIGHT, RIGHT,
            #UP, UP, LEFT, LEFT, UP, UP, RIGHT, RIGHT, UP, UP, UP, #LEFT
            #UP, UP, UP, *[LEFT]*10, *[DOWN]*1, *[RIGHT]*7, DOWN
        )):
            self.caterpillar.turn(d)
            self.caterpillar.step(force_eat=i>2)

    def add_a_flower(self, grass_only=False):
        if not self.autogrow_flowers:
            return False
        if grass_only == False:
            if self.add_a_flower(grass_only=True):
                return True
        xs = list(range(self.width))
        ys = list(range(self.width))
        random.shuffle(xs)
        random.shuffle(ys)
        caterpillar_xys = set(s.xy for s in self.caterpillar.segments)
        for x in xs:
            for y in ys:
                if (x, y) in caterpillar_xys:
                    continue
                tile = self.tiles.get((x, y))
                if tile is None:
                    if not grass_only:
                        self[x, y] = 'flower'
                        return True
                elif tile.grow_flower():
                    return True

    def draw(self):
        with pushed_matrix():
            pyglet.gl.glTranslatef(TILE_WIDTH/2, TILE_WIDTH/2, 1)
            pyglet.gl.glScalef(1/2, 1/2, 1)
            self.background.blit_tiled(
                0, 0, 0,
                self.width * TILE_WIDTH * 2, self.height * TILE_WIDTH * 2,
            )
            pyglet.gl.glScalef(2, 2, 1)
            pyglet.gl.glTranslatef(TILE_WIDTH/2, TILE_WIDTH/2, 0)
            self.batch.draw()
            self.caterpillar.draw()
            if self.cocoon:
                self.cocoon.draw()
            self.score_batch.draw()

    def tick(self, dt):
        self.t += dt
        self.caterpillar.tick(dt * SPEED)
        if self.cocoon:
            self.cocoon.tick(dt)
        self.eol_tiles = [tile for tile in self.eol_tiles if tile.tick(dt)]
        for tile in self.tiles.values():
            tile.tick(dt)
        if self.score_labels:
            new_score_labels = []
            for label in self.score_labels:
                t = self.t - label._caterpillar_start_t
                label.x = (label._caterpillar_x) * TILE_WIDTH
                label.y = (label._caterpillar_y + t + t**2*2) * TILE_WIDTH
                label.color = (*label._caterpillar_color, int(abs(1 - t)**.5 * 255))
                if t < 1:
                    new_score_labels.append(label)
                else:
                    label.delete()
            self.score_labels = new_score_labels
        if self.displayed_score != self.total_score:
            diff = (self.total_score - self.displayed_score)
            if diff < 1:
                self.displayed_score = self.total_score
            else:
                self.displayed_score += (self.total_score - self.displayed_score) * 0.1
                if diff > 1000:
                    self.displayed_score += 111
                if diff > 100:
                    self.displayed_score += 11
                elif diff > 10:
                    self.displayed_score += 1
                elif diff > 0:
                    self.displayed_score += 0.5
                else:
                    self.displayed_score -= 0.5
            if self.displayed_score:
                self.main_score_label.text = str(int(self.displayed_score))
            else:
                self.main_score_label.text = ''
        if self.gameover_t is not None:
            gt = (self.t - self.gameover_t)
            n = 30
            b = int(min(255-n, (255-n) * gt))
            o = int(math.sin(gt * math.tau / 2) * n)
            self.gameover_label.color = (
                b-o, b-o, b-o, 255
            )

    def handle_command(self, command):
        if command == 'up':
            self.caterpillar.turn(UP)
        elif command == 'down':
            self.caterpillar.turn(DOWN)
        elif command == 'left':
            self.caterpillar.turn(LEFT)
        elif command == 'right':
            self.caterpillar.turn(RIGHT)
        elif command == 'end' and self.ui:
            self.ui.activate()
            return True

    def __getitem__(self, x_y):
        x, y = x_y
        if x < 0 or y < 0 or x >= self.width or y >= self.height:
            return tiles.edge
        return self.tiles.get(x_y, tiles.empty)

    def __setitem__(self, x_y, item):
        eol_tile = self.tiles.pop(x_y, None)
        if eol_tile:
            self.eol_tiles.append(eol_tile)
            eol_tile.delete()
        x, y = x_y
        if x < 0 or y < 0 or x >= self.width or y >= self.height:
            return
        if item is not None:
            if isinstance(item, str):
                item = tiles.new(item, self, x, y)
            self.tiles[x_y] = item

    def add_cocoon(self, caterpillar):
        self.cocoon = Cocoon(self, caterpillar)

    def score(self, amount, x, y):
        self.total_score += amount
        if self.total_score <= 0:
            self.total_score = 0
        self.main_score_label.text = str(self.total_score)
        if not (0 < amount < 5):
            label = self.add_label(f'{amount:+1}', x, y)
            if amount > 0:
                label._caterpillar_color = 250, 255, 200
            else:
                label._caterpillar_color = 255, 230, 200

    def add_label(self, label, x, y):
        label = pyglet.text.Label(
            label,
            **HALF_FONT_INFO.label_args(),
            anchor_x='center',
            anchor_y='baseline',
            align='center',
            batch=self.score_batch,
            x=x * TILE_WIDTH,
            y=y * TILE_WIDTH,
        )
        self.score_labels.append(label)
        label._caterpillar_start_t = self.t
        label._caterpillar_x = x
        label._caterpillar_y = y
        label._caterpillar_color = 255, 255, 255
        return label

    def signal_done(self):
        if self.done:
            return True
        self.shot = pyglet.image.get_buffer_manager().get_color_buffer().get_texture()
        self.state.level_completed(
            self.level, self.total_score, self.caterpillar.collected_items,
            self.cocoon.butterfly,
        )
        if self.ui:
            self.ui.activate(self.shot)

    def signal_game_over(self, message):
        self.gameover_label.text = f'{message}    Press esc to exit.'.upper()
        self.gameover_t = self.t

    def update_collected(self, caterpillar):
        for item in caterpillar.collected_items.difference(self.collected_sprites):
            for i in range(100):
                for sprite in self.collected_sprites.values():
                    if sprite._caterpillar_i == i:
                        break
                else:
                    break
            sprite = pyglet.sprite.Sprite(
                get_image(item),
                x=(self.width-1/4) * TILE_WIDTH,
                y=(self.height - 3/4 - i/2) * TILE_WIDTH,
                batch=self.score_batch,
            )
            sprite._caterpillar_i = i
            sprite.scale = 1/4
            self.collected_sprites[item] = sprite
        for name, sprite in list(self.collected_sprites.items()):
            if name not in caterpillar.collected_items:
                sprite.delete()
                del self.collected_sprites[name]


from rest_framework import serializers

from ...leeway.models import LeewaySimulation

class LeewaySimulationSerializer(serializers.ModelSerializer):
    """
    Serializer for the Leeway Simulations. Inherits from
    `serializers.ModelSerializer`.
    """

    #: Show username instead of id
    username = serializers.ReadOnlyField(source="user.username")

    class Meta:
        """
        Define model and the corresponding fields
        """

        #: The model class for this serializer
        model = LeewaySimulation

        #: Exclude user field because the username is shown instead
        exclude = ["user"]

        #: Define fields which are shown when retrieving simulations,
        #: but cannot be set when creating new ones
        read_only_fields = [
            "uuid",
            "img",
            "netcdf",
            "traceback",
            "simulation_started",
            "simulation_finished",
        ]


from discord.ext.commands import Bot, Cog, Context, command

from games.roles import simple

class GameRoleCog(Cog):
    @command(aliases=["sr"])
    async def show_role(self, ctx: Context, player_count: int) -> None:
        """役職一覧を表示(引数：プレイ人数)"""
        send_message: str = f"{player_count}人プレイ役職：\n"
        for role in simple[player_count]:
            send_message += str(role.name) + "\n"
        await ctx.send(send_message)

def setup(bot: Bot) -> None:
    bot.add_cog(GameRoleCog(bot))


from elasticsearch import Elasticsearch
import requests
import tarfile

# On Windows/Ubuntu try to download the elasticsearch application and run it as a service
# elastic search instance has to be running on the machine. Default port is 9200.
# Call the Elastic Search instance
# Download the client library using pip for python and use it
# Use https://www.oreilly.com/library/view/elasticsearch-the-definitive/9781449358532/
# to explore more on Elasticsearch

elastic_search = Elasticsearch([{'host': 'localhost', 'port': 9200}])

# delete any pre-existing index
# if es.indices.exists(index="myindex"):
#     es.indices.delete(index='myindex', ignore=[400, 404])

def get_data():

    """
    This function downloads the required data to test elasticsearch and searching mechanism
    """
    url = 'http://www.cs.cmu.edu/~dbamman/data/booksummaries.tar.gz'
    target_path = 'books.tar.gz'

    response = requests.get(url, stream=True)
    if response.status_code == 200:
        open(target_path, 'wb').write(response.content)

    # open tar zip file file
    file = tarfile.open(target_path)
    # extracting the tar zip file
    file.extractall('.')
    file.close()
    return True

class Indexing:

    def __init__(self, path, elastic_instance):
        self.path = path
        self.elastic_instance = elastic_instance
        self.res = None

    def build_index(self,index_count):
        path = self.path
        count = 1
        for line in open(path, encoding='utf-8'):
            try:
                fields = line.split("\t")
                doc = {'id': fields[0],
                       'title': fields[2],
                       'author': fields[3],
                       'summary': fields[6]
                       }

                self.res = self.elastic_instance.index(index="myindex", id=fields[0], body=doc)
                count = count + 1
                if count == index_count:
                    break
            except:
                pass

    def search_query(self, query):
        result = self.elastic_instance.search(index="myindex", body={"query": {"match": {"summary": query}}})
        print("Your search returned %d results." % result['hits']['total']['value'])
        return result

if __name__ == '__main__':
    get_data()
    instance = Indexing("booksummaries/booksummaries.txt", elastic_search)
    instance.build_index(index_count= 1000)

    while True:
        query = input("Enter your search query: ")
        if query.lower() == "stop":
            break
        res = instance.search_query(query)
        print("Your search returned %d results:" % res['hits']['total']['value'])
        for hit in res["hits"]["hits"]:
            print(hit["_source"]["title"])
            loc = hit["_source"]["summary"].lower().index(query)
            print(hit["_source"]["summary"][:100])
            print(hit["_source"]["summary"][loc - 100:loc + 100])


import glob
import torch

from PIL import Image

class ImageDataset(torch.utils.data.Dataset):
    def __init__(self, root_dir, transform=None, mode='trainA'):
        self.transform = transform
        self.image_paths = glob.glob(root_dir + f'{mode}/*')

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

    def __getitem__(self, index):
        image = Image.open(self.image_paths[index])

        if image.mode != 'RGB':
            image = image.convert('RGB')

        if self.transform is not None:
            image = self.transform(image)
        return image

if __name__ == '__main__':
    root_dir = 'C:/DB/CycleGAN/horse2zebra/horse2zebra/'

    A_dataset = ImageDataset(root_dir, mode='trainA')
    B_dataset = ImageDataset(root_dir, mode='trainB')

    for i in range(len(A_dataset)):
        cv2.imshow('A', A_dataset[i])
        cv2.imshow('B', B_dataset[i])
        cv2.waitKey(0)

8. Image Generation/8.4. CycleGAN/core/dataset.py