import argparse
import logging

logging.basicConfig(level=logging.INFO)
logging.info('This will be logged')

base_string = argparse.ArgumentParser()
base_string.add_argument("string", help="string to test", type=str)
base_string = base_string.parse_args()
print("The string is : " + base_string.string)

list_ASCII = []
for character in base_string.string:
    list_ASCII.append(ord(character))

# Display list
print(list(base_string.string))

# Display list format Ascii
print(list_ASCII)

# List of characters converted to Binary
list_bin = []
for i in list_ASCII:
    list_bin.append(bin(int(i))[2:].zfill(8))

# Display list format Binary
print(list_bin)

# String of raw binary characters
character_bin = ''.join(list_bin)
print(character_bin)

# n = length of one block
n = 6

# list formatted with 6 digits in each block
list_bin_formatted = [character_bin[i:i + n] for i in range(0, len(character_bin), n)]
print(list_bin_formatted)

# list with 6 digits on the last element
lastElement = list_bin_formatted[-1].ljust(6, "0")
list_bin_formatted[-1] = lastElement
print(list_bin_formatted)


import csv
import json

from django.http import HttpResponse, Http404
from django.views.generic import ListView
from django.views.generic.list import BaseListView

class GetMoreView(ListView):
    """A base view for feeding data to 'get more...' type of links

    Will return a json result, with partial of rendered template:
    {
        "content": "....",
        "current": current_page number
        "total": total_pages
        "has_next": true if next page exists
    }
    We'll paginate the response. Since Get More link targets may already have
    initial data, we'll look for `initial` GET param, and take it into
    consideration, completing to page size.
    """

    def get_context_data(self, **kwargs):
        ctx = super(GetMoreView, self).get_context_data(**kwargs)
        try:
            initial = int(self.request.GET.get('initial', '0'))
        except ValueError:
            initial = 0

        # initial only affects on first page
        if ctx['page_obj'].number > 1 or initial >= self.paginate_by - 1:
            initial = 0

        ctx['object_list'] = ctx['object_list'][initial:]
        return ctx

    def render_to_response(self, context, **response_kwargs):
        """We'll take the rendered content, and shove it into json"""

        tmpl_response = super(GetMoreView, self).render_to_response(
            context, **response_kwargs).render()

        page = context['page_obj']

        result = {
            'content': tmpl_response.content,
            'total': context['paginator'].num_pages,
            'current': page.number,
            'has_next': page.has_next(),
        }

        return HttpResponse(json.dumps(result, ensure_ascii=False),
                            content_type='application/json')

class CsvView(BaseListView):
    """A view which generates CSV files with information for a model queryset.
    Important class members to set when inheriting:
      * model -- the model to display information from.
      * queryset -- the query performed on the model; defaults to all.
      * filename -- the name of the resulting CSV file (e.g., "info.csv").
      * list_display - a list (or tuple) of tuples, where the first item in
        each tuple is the attribute (or the method) to display and
        the second item is the title of that column.

        The attribute can be a attribute on the CsvView child or the model
        instance itself. If it's a callable it'll be called with (obj, attr)
        for the CsvView attribute or without params for the model attribute.
    """

    filename = None
    list_display = None

    def dispatch(self, request):
        if None in (self.filename, self.list_display, self.model):
            raise Http404()
        self.request = request
        response = HttpResponse(mimetype='text/csv')
        response['Content-Disposition'] = \
            'attachment; filename="{}"'.format(self.filename)

        object_list = self.get_queryset()
        self.prepare_csv_for_utf8(response)
        writer = csv.writer(response, dialect='excel')
        writer.writerow([title.encode('utf8')
                         for _, title in self.list_display])
        for obj in object_list:
            row = [self.get_display_attr(obj, attr)
                   for attr, _ in self.list_display]
            writer.writerow([unicode(item).encode('utf8') for item in row])
        return response

    def get_display_attr(self, obj, attr):
        """Return the display string for an attr, calling it if necessary."""
        display_attr = getattr(self, attr, None)
        if display_attr is not None:
            if callable(display_attr):
                display_attr = display_attr(obj, attr)
        else:
            display_attr = getattr(obj, attr)
            if callable(display_attr):
                display_attr = display_attr()
        if display_attr is None:
            return ""
        return display_attr

    @staticmethod
    def prepare_csv_for_utf8(fileobj):
        """Prepend a byte order mark (BOM) to a file.

        When Excel opens a CSV file, it assumes the encoding is ASCII. The BOM
        directs it to decode the file with utf-8.
        """
        fileobj.write('\xef\xbb\xbf')


from django.conf.urls import patterns, url

from workup.apps.forum.views import TopicList
from workup.apps.blog_extension.views import BlogPostList
from workup.apps.comments_extension.views.generic import CommentList

urlpatterns = patterns(
    "",
    url("^users/(?P<username>.*)/topics/$",
        TopicList.as_view(
            template_name='accounts/account_profile_topics.html'),
        {"by_score": False},
        name="topic_list_user"),
    url("^users/(?P<username>.*)/blogs/$",
        BlogPostList.as_view(
            template_name='accounts/account_profile_blogs.html'),
        name="blog_list_user"),
    url("^users/(?P<username>.*)/comments/$",
        CommentList.as_view(
            template_name='accounts/account_profile_comments.html'),
        {"by_score": False},
        name="comment_list_user"),
)


import json
import re

import aiogram
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram import Bot, Dispatcher, types, executor
from aiogram.dispatcher.filters import Text

from config_reader import config
from aiogram.contrib.fsm_storage.memory import MemoryStorage
# from pars_nbareplayhd import main
import pars_nbareplayhd_async
from pars_aanba import get_video_rus
from pars_aanba_vk import main

bot = Bot(token=config.bot_token.get_secret_value())

storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)

@dp.message_handler(commands="start")
async def start(message: types.Message):
    start_buttons = ["Получить матчи на англ.яз.", "Получить матчи на русском"]
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
    keyboard.add(*start_buttons)
    await message.answer("Запись матчей NBA", reply_markup=keyboard)

@dp.message_handler(Text(equals="Получить матчи на русском"))
async def matchs_today(message: types.Message):
    msg = await message.answer("Идет поиск матчей за сегодня..." + "\n\nЭто может занять до 60 сек")

    links = main()
    # links = get_video_rus("https://aanba.ru/zapisi-matchej")

    await msg.delete()

    with open("data/matchs_dict_aanba_vk.json", encoding="utf-8") as file:
        matchs_dict = json.load(file)

    matchs_buttons = []
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=1, one_time_keyboard=True)
    for k, v in matchs_dict.items():
        title = f"{v['match_title']}({k})"
        matchs_buttons.append(title)
    matchs_buttons.append('Назад')
    keyboard.add(*matchs_buttons)
    await message.answer("Выберите матч для просмотра", reply_markup=keyboard)

@dp.message_handler(Text(equals="Получить матчи на англ.яз."))
async def matchs_today(message: types.Message):
    msg = await message.answer("Идет поиск матчей за сегодня..." + "\n\nЭто может занять до 60 сек")

    # links = main()
    await pars_nbareplayhd_async.get_matchs()

    await msg.delete()

    with open("data/matchs_dict.json") as file:
        matchs_dict = json.load(file)

    matchs_buttons = []
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=1, one_time_keyboard=True)
    for k, v in matchs_dict.items():
        title = f"{v['match_title']}({k})"
        matchs_buttons.append(title)
    matchs_buttons.append('Назад')
    keyboard.add(*matchs_buttons)
    await message.answer("Выберите матч для просмотра", reply_markup=keyboard)

@dp.message_handler()
async def match_answer(message: types.Message):
    if '/' not in message.text:
        with open("data/matchs_dict.json") as file:
            matchs_dict = json.load(file)

        builder = InlineKeyboardMarkup(row_width=4)

        for i in range(1, 5):
            try:
                try:
                    builder.add(
                        InlineKeyboardButton(text=f'Четверть №{i}', url=matchs_dict[message.text[-6:-1]][f'Part{i}']))
                except KeyError:
                    # builder.add(InlineKeyboardButton(text=f'Четверть №{i} не вышла'))
                    print(0)
            except aiogram.utils.exceptions.BadRequest:
                print(0)

        await message.answer('Выберите четверть', reply_markup=builder)
    else:
        with open("data/matchs_dict_aanba_vk.json", encoding="utf-8") as file:
            matchs_dict = json.load(file)

        builder = InlineKeyboardMarkup(row_width=4)
        builder.add(InlineKeyboardButton(text=f'Полный матч', url=matchs_dict[message.text[-11:-1]]['full_game']))

        await message.answer('Получите, распишитесь', reply_markup=builder)

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)


class node:
    def __init__(self):
        self.data = None
        self.right = None
        self.left = None

class tree:
    def __init__(self):
        self.root = None
        self.queue = []

    def CreateNode(self, data):
        if self.root == None:
            self.root = node()
            self.root.data = data
            return
        i = self.root
        while 1:
            if self.root == None:
                self.root = node()
                self.root.data = data
                if temp.data > data:
                    temp.left = self.root
                else:
                    temp.right = self.root
                self.root = i
                return
            elif self.root.data > data:
                temp = self.root
                self.root = self.root.left
            elif self.root.data < data:
                temp = self.root
                self.root = self.root.right

    def BreathFirstTraverse(self, root):
        self.AppendChildren(root)
        while len(self.queue) != 0:
            temp = self.queue.pop(0)
            print(temp.data)
            if temp.left != None:
                self.AppendChildren(temp.left)
            if temp.right != None:
                self.AppendChildren(temp.right)

    def AppendChildren(self, address):
        self.queue.append(address)

    def FindElement(self, element):
        i = self.root
        while 1:
            if i == None:
                print(f"{element} not present in tree")
                break
            if i.data == element:
                print(f"{element} present in the tree")
                break
            if i.data > element:
                i = i.left
            else:
                i = i.right

t = tree()
t.CreateNode(6)
t.CreateNode(5)
t.CreateNode(9)
t.CreateNode(2)
t.CreateNode(7)
t.CreateNode(10)
t.CreateNode(1)
t.CreateNode(4)
t.CreateNode(8)
t.CreateNode(3)
t.BreathFirstTraverse(t.root)


import torch
import torch.nn as nn
import torch.nn.functional as F

from meloetta.frameworks.mewzero.model import config

class ResidualLayer(nn.Module):
    def __init__(self, channels: int) -> None:
        super().__init__()
        self.res = nn.Sequential(
            nn.Linear(channels, channels),
            nn.ReLU(),
            nn.Linear(channels, channels),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return F.relu(x + self.res(x))

class PrivateSpatialEncoder(nn.Module):
    def __init__(self, gen: int, n_active: int, config: config.PrivateEncoderConfig):
        super().__init__()

        self.config = config
        self.gen = gen
        self.n_active = n_active

        self.project = nn.Linear(
            config.entity_embedding_dim * 2, config.entity_embedding_dim
        )

        self.resblock_stacks = nn.Sequential(
            *[ResidualLayer(config.entity_embedding_dim) for _ in range(2)]
        )

        self.spatial_out = nn.Linear(
            config.entity_embedding_dim, config.entity_embedding_dim
        )

    def forward(
        self, entity_embeddings: torch.Tensor, mask: torch.Tensor
    ) -> torch.Tensor:
        active_mask = mask[..., : self.n_active, :]
        active = entity_embeddings[..., : self.n_active, :].sum(1, keepdim=True)
        active = active / active_mask.sum(1, keepdim=True).clamp(min=1)

        reserve_mask = mask[..., self.n_active :, :]
        reserve = entity_embeddings[..., self.n_active :, :].sum(1, keepdim=True)
        reserve = reserve / reserve_mask.sum(1, keepdim=True).clamp(min=1)

        spatial_emb = torch.cat((active, reserve), dim=-1)
        spatial_emb = self.project(spatial_emb)
        spatial_emb = self.resblock_stacks(spatial_emb)
        spatial_emb = self.spatial_out(spatial_emb)

        return spatial_emb


import pickle
from optparse import OptionParser
import os
import sys
from file_util import *
from patch_util import *

# cannot be loaded as a module
if __name__ != "__main__":
    sys.stderr.write('This file cannot be loaded as a module!')
    sys.exit()

# parse command-line options
disc = """
This utility applies patch files.
"""

parser = OptionParser(description=disc)
parser.add_option('--patch-config', dest='patchconfig', metavar='DIR',
                  help='patch configuration file')
(options, args) = parser.parse_args()

# the patchconfig option is required
if options.patchconfig is None:
    parser.print_help(sys.stdout)
    sys.exit()

# normalize the patch directory value
patchdir = os.path.dirname(os.path.abspath(options.patchconfig)).replace('\\', '/')
if patchdir[-1] != '/':
    patchdir += '/'

# check if the patching should be skipped
if os.path.isfile(patchdir + 'NOPATCH'):
    nopatch = True
    sys.stdout.write('NOPATCH exists -- files have not been patched.\n')
else:
    nopatch = False
    # locate the patch configuration file
    if not os.path.isfile(options.patchconfig):
        sys.stderr.write('File '+options.patchconfig+' does not exist.\n')
        sys.exit()

    scope = {}
    execfile(options.patchconfig, scope)
    patches = scope["patches"]

    for patch in patches:
        file = patchdir+'patches/'+patch['name']+'.patch'
        dopatch = True

        if 'condition' in patch:
          # Check that the environment variable is set.
          if patch['condition'] not in os.environ:
            sys.stderr.write('Skipping patch file '+file+'\n')
            dopatch = False

        if dopatch:
          if not os.path.isfile(file):
              sys.stderr.write('Patch file '+file+' does not exist.\n')
          else:
              sys.stderr.write('Reading patch file '+file+'\n')
              dir = patch['path']
              patchObj = from_file(file)
              patchObj.apply(dir)
              if 'note' in patch:
                  separator = '-' * 79 + '\n'
                  sys.stderr.write(separator)
                  sys.stderr.write('NOTE: '+patch['note']+'\n')
                  sys.stderr.write(separator)

# read the current include file, if any
incfile = patchdir + 'patch_state.h'
if nopatch:
    incnew = """// This file is generated by the patch tool and should not be edited manually.
#ifndef _PATCH_STATE_H
#define _PATCH_STATE_H
// No patches have been applied to the Chromium/WebKit source base.
#define CEF_PATCHES_APPLIED    0
#endif // _PATCH_STATE_H
"""
else:
    incnew = """// This file is generated by the patch tool and should not be edited manually.
#ifndef _PATCH_STATE_H
#define _PATCH_STATE_H
// Patches have been applied to the Chromium/WebKit source base.
#define CEF_PATCHES_APPLIED    1
#endif // _PATCH_STATE_H
"""

inccur = ''
if os.path.isfile(incfile):
    inccur = read_file(incfile)

if inccur != incnew:
    sys.stdout.write('Writing file '+incfile+'.\n')
    write_file(incfile, incnew)


from django.contrib import admin
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from accounts.models import CustomUser
from drf_store import settings
from items.models import Item
from services.managers import InvoiceManager, RentManager, ServiceManager

class Invoice(models.Model):

    class InvoiceStatuses(models.IntegerChoices):
        PAID = 0, _('Оплачен')
        UNPAID = 1, _('Ожидает оплаты')
        CANCELED = 2, _('Отменен')

    user_id = models.ForeignKey(
        to=CustomUser,
        on_delete=models.CASCADE,
        verbose_name=_('user')
    )
    status = models.PositiveSmallIntegerField(
        choices=InvoiceStatuses.choices,
        default=InvoiceStatuses.UNPAID,
        blank=False,
        verbose_name=_('status')
    )
    date_created = models.DateTimeField(
        _('date created'),
        auto_now_add=True
    )
    status_updated = models.DateTimeField(
        _('status updated'),
        default=timezone.now
    )

    objects = InvoiceManager()

    class Meta:
        verbose_name = _('invoice')
        verbose_name_plural = _('invoices')
        permissions = [
            ('can_change_status', _('Can change status'))
        ]

    def __str__(self):
        return f'[{self.id}] {self.user_id}'

    @property
    @admin.display(description=_('Total price'))
    def price_total(self):
        total = 0
        for obj in self.purchase_set.all():
            total += obj.price
        for obj in self.rent_set.all():
            total += obj.price
        return total

    def get_final_price_and_currency(self):
        """
        Evaluate final_price(= price - currency) and new_currency
        :return: (final_price, new_currency)
        """
        max_discount = self.price_total * settings.MAX_DISCOUNT
        bound_price = self.price_total - max_discount
        diff = max_discount - self.user_id.profile.currency
        if diff >= 0:
            return bound_price + diff, 0
        return bound_price, diff * (-1)

    @property
    def user_profile(self):
        return self.user_id.profile

class Service(models.Model):
    item = models.ForeignKey(to=Item, on_delete=models.CASCADE, verbose_name=_('item'))
    invoice = models.ForeignKey(to=Invoice, on_delete=models.CASCADE, verbose_name=_('invoice'))
    quantity = models.PositiveSmallIntegerField(_('quantity'), default=1, blank=False)

    path_template = 'services/item_service.html'

    objects = ServiceManager()

    class Meta:
        abstract = True

    def __str__(self):
        return f'[{self.invoice.status}]-{self.item.title}'

    def delete(self, using=None, keep_parents=False):
        self.item.count_available += self.quantity
        self.item.save()
        result_delete = super().delete()
        if not self.invoice.purchase_set.exists() and not self.invoice.rent_set.exists():
            self.invoice.delete()
        return result_delete

class Purchase(Service):

    class Meta:
        verbose_name = _('purchase')
        verbose_name_plural = _('purchases')

class Rent(Service):
    date_from = models.DateField(_('Date from'), null=False, default=timezone.now)
    date_to = models.DateField(_('Date to'), null=False, default=timezone.now)
    daily_payment = models.PositiveSmallIntegerField(
        _('daily payment'),
        default=50,
        help_text=_('Daily payment'),
        blank=False,
        null=False
    )

    objects = RentManager()

    path_template = 'services/item_rent.html'

    class Meta:
        verbose_name = _('rent')
        verbose_name_plural = _('rents')


'''
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.

You may return any answer array that satisfies this condition.

Example 1:

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Note:

2 <= A.length <= 20000
A.length % 2 == 0
0 <= A[i] <= 1000
'''
def sortArrayByParityII(arr):

    for i, elem in enumerate(arr):
        print("i and elem ",i, elem)
        if elem%2 == 0 and i%2==0:
            # all even
            continue
        elif elem%2!=0 and i%2 != 0:
            #all odd
            continue
        elif elem%2==0 and i%2 != 0:
            # even element odd index
            # pass
            j = len(arr)-1
            # while j!=len(arr)-1 and arr[j]%2!=0:
            while arr[j]%2==0 and i!=j:
                j-=1
            print(i, arr[i], j, arr[j])
            arr[i], arr[j] = arr[j], arr[i]
        elif elem%2!=0 and i%2 == 0:
            # odd element even inxed
            j = len(arr)-1
            while arr[j]%2!=0 and i!=j:
                j-=1
            print(i, arr[i], j, arr[j])
            arr[i], arr[j] = arr[j], arr[i]
    print(arr)

sortArrayByParityII([3,5,7,9,4,6,2,8])

def asortArrayByParityII(A):
    j = 1
    for i in range(0, len(A), 2):
        if A[i] % 2:
            while A[j] % 2:
                j += 2
            A[i], A[j] = A[j], A[i]
    print(A)
    return A

def bsortArrayByParityII(A):
    res = [0] * len(A)
    odd, even = 1, 0
    for num in A:
        if num % 2:
            res[odd] = num
            odd += 2
        else:
            res[even] = num
            even += 2
    print(res)
    return res

asortArrayByParityII([3,5,7,9,4,6,2,8])
bsortArrayByParityII([3,5,7,9,4,6,2,8])


# draw_quadratic.py

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import statsmodels.api as sm

# Global parameters
_filename = "example_data/ols.csv"
_title = "Quadratic"

# Configuration
plt.rcParams['font.sans-serif'] = ['Times New Roman']

title_x_axis = "Sonic Temperature"
title_y_axis = "Shortwave Radiation"

unit_x_axis = r'$^oC$'
unit_y_axis = r'$W/m^2$'

def read_and_format_csv(filename):
    try:
        # Read csv file and use 'tab' as delimiter
        df = pd.read_csv(filename, delimiter=',')

        # If the file is not seperated by common raise exception and use common as delimiter
        if(df.shape[1] != 2):
            raise Exception("Input file is not seperated by ','")

    except Exception:
        print("Use 'tab' as the delimiter")

        # Read csv file and ust 'tab' as delimiter
        df = pd.read_csv(filename, delimiter='\t')

    # Drop the time columns
    # df.drop(df.columns[0], axis=1, inplace=True)

    return df

def format(df):
    # Transform the type of df into float64,
    df = df.astype('float64')

    return df

def log_out(filename, message):
    with open(filename, 'w') as f:
        print(message, file=f)

def draw_scatter_with_quadratic(df_x, df_y):
    # Generate coefficients of quadratic
    coef = np.polyfit(df_x, df_y, 2)
    df_y_fit = np.polyval(coef, df_x)

    # Generate figure
    fig, ax = plt.subplots()

    # Scatter plot
    ax.scatter(df_x, df_y, s=20, c="black", label="Original Data")

    # Draw OLS on axes
    ax.plot(df_x, df_y_fit, 'r-',
            label="Quadratic curve")

    # Put the title
    ax.set_title(_title)

    # Put the label on each axis
    ax.set_xlabel(f"{title_x_axis} ({unit_x_axis})")
    ax.set_ylabel(f"{title_y_axis} ({unit_y_axis})")

    # Put the legend on the chart
    ax.legend()

if __name__ == "__main__":
    # Read the csv file into a dataframe and drop the time columns
    df_raw = read_and_format_csv(_filename)

    # Format the df_raw & take out the units
    df_formatted = format(df_raw)

    # Store the names of the columns for calling
    array_columns = df_formatted.columns

    # Sorting the dataframe according to the x value
    df_formatted.sort_values(by=array_columns[0], inplace=True)

    # Drawing the scatter plot and the quadratic curve
    draw_scatter_with_quadratic(df_x=df_formatted[array_columns[0]],
                                df_y=df_formatted[array_columns[1]])

    plt.show()


#workermodel
import random

class Worker:

    def __init__(self, wid, classification, properties):
    # do not change input parameters - it is used for initialize a worker
        self.wid = wid
        self.classification = classification
        self.properties = properties
        self.task = None # it will be assigned by task assignment strategy

    def execute(self, worker_manager, task_manager, data):
    # must have this function
    # this function returns the actual execution time
        for du in self.task.data_units:
            for i in range(len(du.data_row) - 1):
                n1 = du.data_row[i]
                n2 = du.data_row[i+1]
                steps = int(n1.get_distance(n2) / 5)
                for j in range(steps):
                    lat = n1.lat + (n2.lat-n1.lat)/steps*j
                    lng = n1.lng + (n2.lng-n1.lng)/steps*j
                    trees = data.tf.find_trees(lat, lng)
                    for tree in trees:
                        if random.random() < self.properties["accuracy"]: # it can be set by worker properties on web
                            data.res.append(tree)
        return self.properties["execution_time"]

    def submit(self):
    # must have this function
        if self.task is None:
            return
        for du in self.task.data_units:
            du.add_judgement(self, self.task, du)

#taskmodel
class Task:

    def __init__(self, tid, data_units):
    # do not change input parameters - it is used for initialize a task
        self.tid = tid
        self.data_units = data_units
        self.properties = {} # configurable

#workerslt
def worker_seletion(worker):
# do not change input parameters - it returns if a worker is qualified (true)
    return True

#taskgen
def task_generation(data, n_data_rows):
# do not change input parameters - it returns data_units for creating a new task
    data_units = data.random_selection(n_data_rows)
    return data_units
#taskassign

def assign(worker_manager, task_manager, data):
    worker = wm.return_upcoming_worker()
    task = tm.return_upcoming_task()
    return (worker, task)

#others

import xml.dom.minidom
import matplotlib.pyplot as plt
import math

class Tree:
    def __init__(self, id, category, lat, lng):
        self.category = category
        self.lat = lat
        self.lng = lng
        self.id = id

class TreeFinder:

    def __init__(self, filename, initial_trees = [], grid_size = 5):
        self.tree_finder = []
        self.trees = initial_trees
        self.count = 0
        self.max_lat = -1e99
        self.min_lat = 1e99
        self.max_lng = -1e99
        self.min_lng = 1e99
        self.grid_size = grid_size
        tid = len(self.trees)
        if filename is not None and filename != "":
            for line in open(filename,'r'):
                temp = line.split(';')
                tree = Tree(tid, temp[0],float(temp[1]),float(temp[2]))
                tid += 1
                self.trees.append(tree)
        for tree in self.trees:
            self.max_lat = max(self.max_lat, tree.lat)
            self.min_lat = min(self.min_lat, tree.lat)
            self.max_lng = max(self.max_lng, tree.lng)
            self.min_lng = min(self.min_lng, tree.lng)
        w = LatLng(self.min_lat, self.min_lng).get_xy(LatLng(self.min_lat, self.max_lng)).x
        h = LatLng(self.min_lat, self.min_lng).get_xy(LatLng(self.max_lat, self.min_lng)).y
        self.n_lat = int(h / grid_size) + 1
        self.n_lng = int(w / grid_size) + 1
        self.k_lat = int((self.n_lat-1)/(self.max_lat-self.min_lat))
        self.k_lng = int((self.n_lng-1)/(self.max_lng-self.min_lng))
        for i in range(self.n_lat):
            self.tree_finder.append([])
            for j in range(self.n_lng):
                self.tree_finder[i].append([])
        self.tree_vis = [False for tree in self.trees]
        for tree in self.trees:
            self.add_tree(tree)

    def add_tree(self, tree):
        i = int((tree.lat - self.min_lat) * self.k_lat)
        j = int((tree.lng - self.min_lng) * self.k_lng)
        self.tree_finder[i][j].append(tree)
        self.count += 1

    def find_trees(self, lat, lng, radius = 5):
        trees = []
        grid_range = int((radius-0.1)/self.grid_size) + 1
        i = int((lat - self.min_lat) * self.k_lat)
        j = int((lng - self.min_lng) * self.k_lng)
        for p in range(i-grid_range,i+grid_range+1):
            for q in range(j-grid_range,j+grid_range+1):
                if 0 <= p and p < self.n_lat and 0<=q and q < self.n_lng:
                    for tree in self.tree_finder[p][q]:
                        if LatLng(lat,lng).get_distance(LatLng(tree.lat,tree.lng)) < radius:
                            trees.append(tree)
        return trees

class Cartesian:

    def __init__(self, x, y):
        self.x = x
        self.y = y

class LatLng:

    def __init__(self, lat, lng):
        self.lat = lat
        self.lng = lng

    def get_latlng(self, x, y):
        return LatLng(self.lat + y/111300.0, self.lng + x/111300.0/math.cos(self.lat/180.0*math.pi))

    def get_xy(self, latlng):
        return Cartesian((latlng.lng - self.lng) * 111300.0 * math.cos(self.lat/180.0*math.pi), (latlng.lat - self.lat) * 111300.0)

    def get_distance(self,latlng):
        p = self.get_xy(latlng)
        return math.sqrt(p.x*p.x + p.y*p.y)

class RoadNetwork:

    def __init__(self, xml_filename):
        DOMTree = xml.dom.minidom.parse(xml_filename)
        self.roads = []
        root = DOMTree.documentElement
        xml_roads = root.getElementsByTagName("road")
        i = 0
        for xml_road in xml_roads:
            coords = []
            nodes = xml_road.getElementsByTagName("node")
            for node in nodes:
                coords.append(LatLng(float(node.getAttribute('lat')),float(node.getAttribute('lng'))) )
            self.roads.append(coords)
            i += 1

    def plot_map(self):
        plt.switch_backend('Agg')   # run matplotlib at the backend
        for road in self.roads:
            ndx = []
            ndy = []
            for node in road:
                ndy.append(node.lat)
                ndx.append(node.lng)
            plt.plot(ndx, ndy, lw = 0.5, c = [0.3,0.3,0.3])

def init(instance, worker_manager, task_manager, data):
    rn = RoadNetwork("project/example/road_network.xml")
    tf = TreeFinder("project/example/trees.csv")
    for r in rn.roads:
        data.add_data(r)
    data.rn = rn
    data.tf = tf
    data.res = []

def output(instance, worker_manager, task_manager, data):
    dict = {}
    n = 0
    for du in data.data_units:
        n += len(du.judgements)
    dict["cost"] = n * data.price
    dict["num_of_workers"] = len(worker_manager.workers)
    return dict

def final(instance, worker_manager, task_manager, data):
    plt.figure()
    data.rn.plot_map()
    px = []
    py = []
    for tree in data.res:
        px.append(tree.lng)
        py.append(tree.lat)
    plt.scatter(px,py,c=[0.3,0.8,0.3],marker='o',s=1)
    plt.savefig(instance.fig_location("tree_map"))


import numpy as np
import matplotlib.pyplot as plt

r0=1000.0
e=[i for i in range(1,4000)]
cw=0.08
plist=[]
for i in e:
	plist.append(cw*i/(2*r0*((1+i/r0)**cw-1)))
p=np.array(plist)
plt.plot(e,p,color="red",linestyle="--",marker="*",label='')
plt.legend()
plt.title("pay-slippage")
plt.xlabel("pay")
plt.ylabel("slippage")
plt.show()


from args import arg
import numpy as np
import matplotlib.pyplot as plt

# PLOT = arg.Plot

def plot_list(Rewards , title = None, Plot_mean_reward = arg.Plot_mean_reward, cut_len = 10):
    # print mean_ys
    if (Plot_mean_reward):

        # y = np.zeros(len(Rewards))
        y = []
        mean_y = Rewards[0]
        for i in range(len(Rewards)):
            # if(i%100 == 0 and i <= len(Rewards)-100):
            if (i % cut_len == 0):
                end_i = i + cut_len
                if (end_i > len(Rewards) - 1):
                    end_i = len(Rewards) - 1
                    mean_y = np.mean(Rewards[i:end_i])
                y.append(np.mean(Rewards[i:end_i]))
            # y[i] = mean_y
        y
        plt.plot(y)
        plt.show()
        return 1

    # len(Rewards)
    color = 'blue'
    ys = Rewards  # 放大到达终点前的曲线变化
    xs = np.arange(0, len(ys))
    plt.plot(xs, ys, color=color)

    # plot the (mean ± 2*std)
    if (1):
        ys_avg = ys  # 中心线
        ys_std = np.ones(len(ys)) * np.std(ys)  # 方差

        # 最近邻方差
        if (1):
            cut_len = 10

            for i in range(len(ys)):
                if (i > (len(ys) - cut_len // 2)):
                    ys_std[i] = np.std(ys[-cut_len:])
                    continue
                if (i < cut_len // 2):
                    ys_std[i] = np.std(ys[:cut_len])
                else:
                    # i = 10
                    ys_std[i] = np.std(Rewards[i - cut_len // 2:i + cut_len // 2])

        r1 = list(map(lambda x: x[0] - x[1], zip(ys_avg, ys_std)))
        r2 = list(map(lambda x: x[0] + x[1], zip(ys_avg, ys_std)))

        plt.fill_between(xs, r1, r2, color=color, alpha=0.1)

    if(title == None):
        plt.xlabel('episode')
        plt.ylabel('Reward')
    else:
        plt.xlabel(title[0])
        plt.ylabel(title[1])
    plt.show()

    # env.close()

    1

def plot_mean_loss(ys):
        cut_len = 10
        # y = np.zeros(len(ys))
        y = []
        mean_y = ys[0]
        for i in range(len(ys)):
            # if(i%100 == 0 and i <= len(ys)-100):
            if (i % cut_len == 0):
                end_i = i + cut_len
                if (end_i > len(ys) - 1):
                    end_i = len(ys) - 1
                    mean_y = np.mean(ys[i:end_i])
                y.append(np.mean(ys[i:end_i]))
        plt.plot(y)
        plt.xlabel('episode')
        plt.ylabel('Loss')
        plt.show()

def plot_loss(loss_s):
    # if (plot_mean_loss):
    #     plot_mean_loss(loss_s)
    # else:
    color = 'blue'
    ys = loss_s  # 放大到达终点前的曲线变化
    xs = np.arange(0, len(ys))
    plt.plot(xs, ys, color=color)
    plt.xlabel('episode')
    plt.ylabel('Loss')
    plt.show()

def plot_net(net, plot_type = 0 ):
    # net = self.eval_net
    plot_type = ['name', 'data'][plot_type]
    if(plot_type == 'name'):
        for name, parms in net.named_parameters():
            print('---->  name:{:10} ---->  grad_requirs: {} ---->  grad_value: {}'.format(name.rjust(15),
                                                                                parms.requires_grad, str(
                    list(parms.grad.shape)).rjust(15)))
    if(plot_type == 'data'):
        for name, parms in net.named_parameters():
            print('---->  name:{} ---->  grad_requirs: {} ---->  grad_value: {}'.format(name.rjust(15),
                                                                                parms.requires_grad, parms.grad ))

if __name__ == '__main__':
    PLOT_MEAN_LOSS = PLOT_LOSS = 1
    rewards = [1,2,3,4,5,6]*100
    plot_list(rewards, False)
    plot_list(rewards, Plot_mean_reward = True, cut_len= 100)

    loss_s = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4]
    plot_loss(loss_s)
    plot_mean_loss(loss_s)


from rero_ils.modules.operation_logs.api import OperationLog, \
    OperationLogsSearch
from rero_ils.modules.operation_logs.logs.api import SpecificOperationLog

from ...items.api import Item
from ...patrons.api import Patron, current_librarian

class LoanOperationLogsSearch(OperationLogsSearch):
    """RecordsSearch for LoanOperationLogs."""

    def get_logs_by_trigger(self, triggers, date_range=None):
        """Get the operation logs base es search.

        :param triggers: list[str] - loan triggers value to filter
        :return: an elasticsearch dsl search query
        """
        query = self.filter('term', record__type='loan')\
            .filter('terms', loan__trigger=triggers)
        if date_range:
            query = query.filter('range', date=date_range)
        return query

class LoanOperationLog(OperationLog, SpecificOperationLog):
    """Operation log for loans."""

    @classmethod
    def create(cls, data, id_=None, index_refresh='false', **kwargs):
        """Create a new record instance and store it in elasticsearch.

        :param loan_data: Dict with the loan metadata.
        :param id_: Specify a UUID to use for the new record, instead of
                    automatically generated.
        :param refresh: If `true` then refresh the affected shards to make
            this operation visible to search, if `wait_for` then wait for a
            refresh to make this operation visible to search, if `false`
            (the default) then do nothing with refreshes.
            Valid choices: true, false, wait_for
        :returns: A new :class:`Record` instance.
        """
        log = {
            'record': {
                'value': data.get('pid'),
                'type': 'loan'
            },
            'operation': 'create',
            'date': data['transaction_date'],
            'loan': {
                'pid': data['pid'],
                'trigger': data['trigger'],
                'override_flag': False,
                'transaction_channel': 'system' if not data.get(
                    'selfcheck_terminal_id') else 'sip2',
                'transaction_location': {
                    'pid': data['transaction_location_pid'],
                    'name': cls._get_location_name(
                        data['transaction_location_pid'])
                },
                'pickup_location': {
                    'pid': data['pickup_location_pid'],
                    'name': cls._get_location_name(data['pickup_location_pid'])
                },
                'patron': cls._get_patron_data(
                    Patron.get_record_by_pid(data['patron_pid'])),
                'item': cls._get_item_data(
                    Item.get_record_by_pid(data['item_pid']['value']))
            }
        }
        if current_librarian:
            log['user'] = {
                'type': 'ptrn',
                'value': current_librarian.pid
            }
            log['user_name'] = current_librarian.formatted_name
            log['organisation'] = {
                'value': current_librarian.organisation_pid,
                'type': 'org'
            }
            log['library'] = {
                'value': current_librarian.library_pid,
                'type': 'lib'
            }
        else:
            log['user_name'] = 'system'
        # Store transaction user name if not done by SIP2
        if log['loan']['transaction_channel'] != 'sip2':
            transaction_user = Patron.get_record_by_pid(
                data['transaction_user_pid'])
            log['loan']['transaction_user'] = {
                'pid': data['transaction_user_pid'],
                'name': transaction_user.formatted_name
            }
        return super().create(log, index_refresh=index_refresh)

    @classmethod
    def anonymize_logs(cls, loan_pid):
        """Anonymize all logs corresponding to the given loan.

        :param loan_pid: Loan PID.
        """
        for log in OperationLogsSearch().get_logs_by_record_pid(loan_pid):
            record = log.to_dict()
            record['loan']['patron']['name'] = 'anonymized'
            record['loan']['patron']['pid'] = 'anonymized'
            cls.update(log.meta.id, log['date'], record)


import argparse
import logging

import chardet
import pypinyin
import os
import random
from datetime import datetime

import numpy as np
import torch
import transformers
from torch.nn import DataParallel
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

from tokenizations.bpe_tokenizer import get_encoder

# 不带声调的(style=pypinyin.NORMAL)
def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s

# 带声调的(默认)
def yinjie(word):
    s = ''
    # heteronym=True开启多音字
    for i in pypinyin.pinyin(word, heteronym=True):
        s = s + ''.join(i) + " "
    return s

class logger:
    def __init__(self, path, clevel=logging.DEBUG, Flevel=logging.DEBUG):
        self.logger = logging.getLogger(path)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
        # 设置CMD日志
        sh = logging.StreamHandler()
        sh.setFormatter(fmt)
        sh.setLevel(clevel)
        # 设置文件日志
        fh = logging.FileHandler(path, encoding='utf-8')
        fh.setFormatter(fmt)
        fh.setLevel(Flevel)
        self.logger.addHandler(sh)
        self.logger.addHandler(fh)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def war(self, message):
        self.logger.warn(message)

    def error(self, message):
        self.logger.error(message)

    def cri(self, message):
        self.logger.critical(message)

logg = logger('/templat.log', logging.INFO, logging.INFO)

def log(x):
    logg.info(x)

def checkchar(lines):
    # 原文里面的字符串
    chars1 = []
    # small里面的字符串
    chars2 = []
    smalllines = []
    for ch in lines[0]:
        chars1.append(ch)
    chars1 = set(chars1)

    with open('cache/vocab_small.txt', 'r', encoding='utf8') as f:
        small = f.readlines()
        small = [line.replace('\n', ' [SEP] ') for line in small]  # 用[SEP]表示换行, 段落之间使用SEP表示段落结束
        small = ''.join(small)
        smalllines.append(small)

    all_len = len(smalllines)
    for ch in smalllines[0]:
        chars2.append(ch)
    chars2 = set(chars2)

    aa = chars1 - chars2
    bb = chars2 - chars1
    print(len(aa))
    print(len(bb))
    writevocab(chars1)

# 生成词典
def writevocab(chars1):
    with open('cache/vocab_cidian.txt', 'w', encoding='utf8') as f:
        for ch in chars1:
            if len(ch) == 0:
                continue
            if ch.startswith('\\ue'):
                continue
            f.write(ch)
            f.write('\n')
    print('writevocab finish')

def build_files(filename, data_path, tokenized_data_path, num_pieces, full_tokenizer, min_length):
    lines = []
    with open(filename, 'rb') as f:
        text = f.read()
    char = chardet.detect(text)
    print(char['encoding'])
    encoding = char['encoding']
    if 'GB2312' == char['encoding']:
        encoding = 'gb18030'
    try:
        with open(filename, 'r', encoding=encoding) as f:
            nodel = f.readlines()
            nodel = [line.replace('\n', ' [SEP] ') for line in nodel]  # 用[SEP]表示换行, 段落之间使用SEP表示段落结束
            nodel = ''.join(nodel)
            lines.append(nodel)
            print('reading ok')
    except UnicodeDecodeError as e:
        # os.remove(os.path.join(data_path, filename))
        print('except:', e)
    all_len = len(lines)
    # checkchar(lines)
    if not os.path.exists(tokenized_data_path):
        os.mkdir(tokenized_data_path)

    for i in tqdm(range(num_pieces)):
        print('开始处理:{}'.format(i))
        parse(i, all_len, lines, tokenized_data_path, num_pieces, full_tokenizer)

    print('finish')

def parse(i, all_len, lines, tokenized_data_path, num_pieces, full_tokenizer):
    sublinesNew = lines[all_len // num_pieces * i: all_len // num_pieces * (i + 1)]
    if i == num_pieces - 1:
        sublinesNew.extend(lines[all_len // num_pieces * (i + 1):])  # 把尾部例子添加到最后一个piece
    sublines = []
    for line in sublinesNew:
        new = full_tokenizer.tokenize(line)
        new = full_tokenizer.convert_tokens_to_ids(new)
        sublines.append(new)
    full_line = []
    for subline in sublines:
        full_line.append(full_tokenizer.convert_tokens_to_ids('[MASK]'))  # 文章开头添加MASK表示文章开始
        full_line.extend(subline)
        full_line.append(full_tokenizer.convert_tokens_to_ids('[CLS]'))  # 文章之间添加CLS表示文章结束
    with open(tokenized_data_path + 'tokenized_train_{}.txt'.format(i), 'w', encoding='utf8') as f:
        for id in full_line:
            f.write(str(id) + ' ')
    print('write-finish：{}'.format(i))

def createfilepath(filepath):
    if not os.path.exists(filepath):
        os.mkdir(filepath)

def createpyfromtemplate(templatename, nodelname):
    newfilename = templatename.replace("template", nodelname)
    alllines = []
    try:
        with open('template/' + templatename, 'r', encoding='utf-8') as f:
            nodel = f.readlines()
            nodel = [line.replace('template', nodelname) for line in nodel]
            nodel = ''.join(nodel)
            alllines.append(nodel)
            print('reading ok')
        with open(nodelname + '/' + newfilename, 'w', encoding='utf8') as f:
            for line in alllines:
                f.write(line)
    except UnicodeDecodeError as e:
        print('except:', e)

def main():
    log('start')
    filename = '全唐诗.txt'
    nodel = filename.split('.')[0]
    nodelname = pinyin(nodel)
    # 新建文件夹
    createfilepath(nodelname)
    createfilepath(nodelname + '/data_' + nodelname)
    createfilepath(nodelname + '/data_' + nodelname+'/tokenized')
    createfilepath(nodelname + '/generated_' + nodelname)
    createfilepath(nodelname + '/model_' + nodelname)
    createfilepath(nodelname + '/model_' + nodelname + '/final_model')
    log('files build')

    # 根据模板创建需要的py文件和bat脚本
    createpyfromtemplate('start-parse-template.bat', nodelname)
    createpyfromtemplate('start-train-template.bat', nodelname)
    createpyfromtemplate('generate_template.py', nodelname)
    createpyfromtemplate('train-template.py', nodelname)

    log('py and bat build')

    # return
    # 根据指定的小说生成训练数据集
    tokenizer_path = 'cache/vocab_small.txt'
    raw_data_path = nodelname + '/data_' + nodelname
    tokenized_data_path = raw_data_path + '/tokenized/'

    from tokenizations import tokenization_bert
    full_tokenizer = tokenization_bert.BertTokenizer(vocab_file=tokenizer_path)
    full_tokenizer.max_len = 999999

    log('building datas')

    build_files(filename=filename, data_path=raw_data_path, tokenized_data_path=tokenized_data_path, num_pieces=1,
                full_tokenizer=full_tokenizer, min_length=128)
    log('files built')

if __name__ == '__main__':
    main()


import sys
import os
import argparse
import logging
import warnings
import io
import json
import subprocess

import cv2
import mxnet as mx
from mxnet import nd, autograd, gluon
from mxnet.gluon.data.vision import transforms
import math
import warnings
import numpy as np

warnings.filterwarnings("ignore",category=FutureWarning)

# sys.path.append(os.path.join(os.path.dirname(__file__), '/opt/ml/code/package'))

import pickle
# from io import StringIO
from timeit import default_timer as timer
from collections import Counter

with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    # from autogluon import ImageClassification as task

import flask

# import sagemaker
# from sagemaker import get_execution_role, local, Model, utils, fw_utils, s3
# import boto3
import tarfile

# import pandas as pd

prefix = '/opt/ml/'
# model_path = os.path.join(prefix, 'model')
model_path = os.environ.get('SM_MODEL_DIR', '/opt/ml/model')
ctx = mx.gpu() if mx.context.num_gpus() else mx.cpu()

# A singleton for holding the model. This simply loads the model and holds it.
# It has a predict function that does a prediction based on the model and the input data.

class ScoringService(object):
    model = None                # Where we keep the model when it's loaded

    @classmethod
    def get_model(cls):
        """Get the model object for this instance, loading it if it's not already loaded."""
        if cls.model == None:
            print(os.listdir("/opt/ml/model/"))
            cls.model = gluon.nn.SymbolBlock.imports(
                "/opt/ml/model/custom_model-symbol.json", ['data'],
                "/opt/ml/model/custom_model-0000.params", ctx=ctx)
        return cls.model

    @classmethod
    def predict(cls, input_np):
        """For the input, do the predictions and return them.
        Args:
            input (a pandas dataframe): The data on which to do the predictions. There will be
                one prediction per row in the dataframe"""
        net = cls.get_model()
        res = net(mx.nd.array(input_np, ctx))
        prob = mx.nd.softmax(res)
        prob = prob.asnumpy()

        prob_sqr = (prob * prob) / np.sum(prob * prob)
        prob = prob_sqr
        prob_sqr = (prob * prob) / np.sum(prob * prob)
        prob = prob_sqr

        print("prob={}".format(str(prob)))
        clsidx = np.argmax(prob)
        classes = []
        with open("/opt/ml/model/classes.txt", "r") as fp:
            classes = fp.readlines()
            classes = [l.strip() for l in classes]
        return int(clsidx), classes, prob.flatten().tolist()

# The flask app for serving predictions
app = flask.Flask(__name__)

@app.route('/ping', methods=['GET'])
def ping():
    """Determine if the container is working and healthy. In this sample container, we declare
    it healthy if we can load the model successfully."""
    # health = ScoringService.get_model() is not None  # You can insert a health check here
    health = 1

    status = 200 if health else 404
    print("===================== PING ===================")
    return flask.Response(response="{'status': 'Healthy'}\n", status=status, mimetype='application/json')

@app.route('/invocations', methods=['POST'])
def invocations():
    """Do an inference on a single batch of data. In this sample server, we take data as CSV, convert
    it to a pandas data frame for internal use and then convert the predictions back to CSV (which really
    just means one prediction per line, since there's a single column.
    """
    data = None
    print("================ INVOCATIONS =================")

    # Convert from CSV to pandas
    if flask.request.content_type == 'application/json':
        data = flask.request.data.decode('utf-8')
        data = json.loads(data)
        data_np = np.asarray(data['data'])
    elif flask.request.content_type == 'image/jpeg':
        data = flask.request.data
        print("len(data)={}".format(len(data)))
        data_np = np.fromstring(data, dtype=np.uint8)
        print("data_np.shape={}".format(str(data_np.shape)))
        print(' '.join(['{:x}'.format(d) for d in data_np[:20].tolist()]), flush=True)
        data_np = cv2.imdecode(data_np, cv2.IMREAD_UNCHANGED)
        data_np = data_np.astype(np.float32) - [123.675, 116.28, 103.53]
        data_np /= [58.395, 57.12, 57.375]
        data_np = data_np.transpose(2, 0, 1)
        data_np = np.expand_dims(data_np, axis=0)
    else:
        return flask.Response(response='This predictor only supports JSON data and JPEG image data',
                              status=415, mimetype='text/plain')
    print(data_np.shape)

    # Do the prediction
    clsidx, classes, prob = ScoringService.predict(data_np)
    print("classes={}".format(str(classes)))
    print("prob={}".format(str(prob)))
    results = [{"Class": cls, "Probability": p} for cls, p in zip(classes, prob)]
    print("results={}".format(str(results)))
    output_dict = {"ClassIndex": clsidx, "Results": results}
    output_dict_str = json.dumps(output_dict)
    print(output_dict_str)

    # Convert from numpy back to CSV
    out = io.StringIO()
    # pd.DataFrame({'results':predictions}).to_csv(out, header=False, index=False)
    out.write(output_dict_str)
    result = out.getvalue()

    return flask.Response(response=result, status=200, mimetype='application/json')


import sys
sys.stdin = open("수열합.txt")

T = int(input())
for tc in range(1, T+1):
    N, M = map(int, input().split())
    result = []
    if M > 500:
        M = 500
    for A in range(M):
        num = list(map(int,input().split()))
        buf = []
        flag = 1
        if len(result) == 0:
            result.extend(num)
        else:
            for i, j in enumerate(result):
                if num[0] < j:
                    result = result[:i] + num + result[i:]
                    flag = 0
                    break
            if len(buf) == 0 and flag:
                result.extend(num)
    print('#{}'.format(tc),end='')
    for i in range(1, 11):
        print(' {}'.format(result[-i]),end='')
    print()


import datetime

import openpaperwork_core

class Plugin(openpaperwork_core.PluginBase):
    def get_interfaces(self):
        return [
            'gtk_calendar_popover',
        ]

    def get_deps(self):
        return [
            {
                'interface': 'gtk_resources',
                'defaults': ['openpaperwork_gtk.resources'],
            },
            {
                'interface': 'i18n',
                'defaults': ['openpaperwork_core.i18n.python'],
            },
        ]

    def gtk_calendar_add_popover(self, gtk_entry):
        widget_tree = self.core.call_success(
            "gtk_load_widget_tree",
            "openpaperwork_gtk.widgets.calendar", "calendar.glade"
        )
        widget_tree.get_object("calendar_popover").set_relative_to(
            gtk_entry
        )
        gtk_entry.connect(
            "icon-release", self._open_calendar, widget_tree
        )
        widget_tree.get_object("calendar_calendar").connect(
            "day-selected-double-click",
            self._update_date, gtk_entry, widget_tree
        )

    def _open_calendar(
            self, gtk_entry, icon_pos, event, widget_tree):
        date = self.core.call_success(
            "i18n_parse_date_short", gtk_entry.get_text()
        )
        if date is None:
            return
        widget_tree.get_object("calendar_calendar").select_month(
            date.month - 1, date.year
        )
        widget_tree.get_object("calendar_calendar").select_day(
            date.day
        )
        widget_tree.get_object("calendar_popover").set_visible(True)

    def _update_date(self, gtk_calendar, gtk_entry, widget_tree):
        date = widget_tree.get_object("calendar_calendar").get_date()
        date = datetime.datetime(year=date[0], month=date[1] + 1, day=date[2])
        date = self.core.call_success("i18n_date_short", date)
        gtk_entry.set_text(date)
        widget_tree.get_object("calendar_popover").set_visible(False)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init

class Doc2VecC(nn.Module):

    def __init__(self, vocab_size, emb_dim, merge="average"):
        super(Doc2VecC, self).__init__()

        assert merge in ["average", "concat"]
        self.merge = merge
        self.vocab_size = vocab_size
        self.emb_dim = emb_dim

        if self.merge == "concat":
            self.center_embeddings = nn.Embedding(vocab_size, 2 * emb_dim)
        elif self.merge == "average":
            self.center_embeddings = nn.Embedding(vocab_size, emb_dim)

        self.context_embeddings = nn.Embedding(vocab_size, emb_dim)

        init_range = 1.0 / self.emb_dim
        init.uniform_(self.center_embeddings.weight.data, -init_range, init_range)
        init.uniform_(self.context_embeddings.weight.data, -init_range, init_range)

    def forward(self, center_w, local_context_w, global_context_w, negative_ws, lengths):
        emb_c = self.center_embeddings(center_w)
        emb_local = local_context_w.matmul(self.context_embeddings.weight)
        emb_global = global_context_w.matmul(self.context_embeddings.weight)
        emb_global /= lengths

        if self.merge == "average":
            emb_v = emb_local + emb_global
        else:
            emb_v = torch.cat((emb_local, emb_global), dim=1)

        neg_s = self.center_embeddings(negative_ws)
        pos_score = torch.sum(torch.mul(emb_c, emb_v), dim=1)
        pos_score = -F.logsigmoid(pos_score)

        neg_score = torch.bmm(neg_s, emb_v.unsqueeze(2)).squeeze()
        neg_score = -torch.sum(F.logsigmoid(-neg_score), dim=1)

        return torch.mean(pos_score + neg_score)

    def save_embedding(self, file_name):
        u_emb = self.context_embeddings.weight.cpu().data.numpy()
        word_embeddings = u_emb
        word_embeddings.dump(file_name)


import argparse
import asyncio
import configparser

from services import SERVICE_NAMES
from services.service_handler import ServiceHandler
from util.config_type import Subtype

def _parse_requested_values(section_config, name, requested_values) -> dict:
    result = dict()
    for key, config_type in requested_values.items():
        # Handle missing values
        if key not in section_config:
            if config_type.required:
                print("Section '%s' is missing config key '%s', skipping." % (name, key))
                return None
            if config_type.default:
                result[key] = config_type.default
            continue

        # Handle service type
        if key == "type":
            if section_config[key] in SERVICE_NAMES:
                result[key] = SERVICE_NAMES.get(section_config[key])
                continue
            else:
                print("Section '%s' has an unknown type '%s', skipping." % (name, section_config[key]))
                return None

        # Handle multi-value values
        if config_type.multi_value:
            result[key] = section_config[key].strip().split("\n")
        else:
            result[key] = section_config[key]

        # Handle subtypes
        if config_type.subtype is Subtype.YES_NO:
            if result[key] not in ["yes", "no"]:
                print("Key '%s' in section '%s' must be either 'yes' or 'no', skipping." % (key, name))
                return None

    return result

def parse_config(section_config, name) -> dict:
    result = dict()

    if name in ["yes", "no"]:
        print("Section has an invalid name '%s', skipping." % section_name)
        return None

    result["name"] = name

    # Parse general config
    requested_values = ServiceHandler.requested_config_values()
    basic_parse = _parse_requested_values(section_config, name, requested_values)

    if not basic_parse:
        return None
    result.update(basic_parse)

    # Parse service-specific config
    requested_values = result["type"].requested_config_values()
    service_parse = _parse_requested_values(section_config, name, requested_values)

    if not service_parse:
        return None
    result.update(service_parse)

    for key in section_config.keys():
        if key not in result:
            print("Unknown config key '%s' for service '%s'." % (key, name))

    return result

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        prog="Chat Relay",
        description="A script for relaying messages between different chat services."
    )
    parser.add_argument("-c", help="Config file location", default="config.ini")
    args = parser.parse_args()

    config = configparser.ConfigParser()
    config.read(args.c)

    for section_name in config.sections():
        section = config[section_name]

        service_config = parse_config(section, section_name)

        if not service_config:
            continue

        # Initialize and start service
        service = service_config["type"](service_config)
        asyncio.ensure_future(service.start())

    asyncio.get_event_loop().run_forever()


import numpy as np
import tensorflow as tf
from .frame_encoder import FrameEncoder, FrameEncoderConfig
from .video_encoder import VideoEncoder, VideoEncoderConfig
from .causal_conv_net import CausalConvNet, CausalConvNetConfig
from . import wavenet_ops
from . import constants

class SynthModelConfig:

    def __init__(
        self,
        frame_config,
        video_config,
        conv_net_config,
        num_notes,
        batch_size,
        window_length,
        audio_frames_per_segment,
        l2_reg_strength,
        learning_rate,
        learning_rate_decay_rate,
        learning_rate_decay_steps,
        max_grad_norm,
        repred_strength,
        dropout_prob
    ):
        self.frame_config = frame_config
        self.video_config = video_config
        self.conv_net_config = conv_net_config
        self.num_notes = num_notes
        self.batch_size = batch_size
        self.window_length = window_length
        self.audio_frames_per_segment = audio_frames_per_segment
        self.l2_reg_strength = l2_reg_strength
        self.learning_rate = learning_rate
        self.learning_rate_decay_rate = learning_rate_decay_rate
        self.learning_rate_decay_steps = learning_rate_decay_steps
        self.max_grad_norm = max_grad_norm
        self.repred_strength = repred_strength
        self.dropout_prob = dropout_prob

DefaultSynthConfig = SynthModelConfig(
    frame_config = FrameEncoderConfig(
        num_features = 128),
    video_config = VideoEncoderConfig(
        frame_width = 780,
        frame_height = 57),
    conv_net_config = CausalConvNetConfig(
        filter_width = 2,
        quantization_channels = 256,
        residual_channels = 64,
        dilation_channels = 64,
        condition_channels = 2 * constants.NUM_NOTES + 1,#128,
        skip_channels = 256,
        dilations = [2**i for i in range(10)] * 6,
        output_width = constants.AUDIO_SEGMENT_LEN * constants.AUDIO_SAMPLE_RATE,
        generator_batch_size = 1),
    num_notes = 2 * constants.NUM_NOTES, # 2 because press and unpress
    batch_size = 3,
    window_length = constants.AUDIO_WINDOW_LEN * constants.AUDIO_SAMPLE_RATE,
    audio_frames_per_segment = (constants.AUDIO_SEGMENT_LEN + constants.AUDIO_WINDOW_LEN) * constants.AUDIO_SAMPLE_RATE,
    l2_reg_strength = .0001,
    learning_rate = .002,
    learning_rate_decay_rate = .3,
    learning_rate_decay_steps = 8000,
    max_grad_norm = 10,
    repred_strength = 0,#.25
    dropout_prob = 0#.2
)

class SynthModel:

    def __init__(self, config):
        self.config = config
        self.quant_channels = self.config.conv_net_config.quantization_channels

        np_notes_one_pad = np.zeros(
            (self.config.batch_size, self.config.audio_frames_per_segment, self.config.num_notes + 1),
            np.float32
        )
        np_notes_one_pad[:, :, 0] = 1
        self.notes_one_pad = tf.constant(np_notes_one_pad)

        self.frame_encoder = FrameEncoder(config.frame_config)
        self.video_encoder = VideoEncoder(self.frame_encoder, config.video_config)
        self.conv_net = CausalConvNet(config.conv_net_config)

        self.notes = tf.placeholder(tf.float32, shape=[None, config.num_notes])
        self.note_sparse_idxs = tf.placeholder(tf.int64, shape=[None, 3])

        self.video_frames = tf.placeholder(tf.float32, shape=[None] + config.video_config.frame_dims)
        self.audio_frames = tf.placeholder(tf.int32, shape=[None, config.audio_frames_per_segment])
        self.sparse_idxs = tf.placeholder(tf.int64, shape=[None, 3])

        self.video_frames_gen = tf.placeholder(tf.float32, shape=[None] + config.video_config.frame_dims)
        self.notes_gen = tf.placeholder(tf.float32, shape=[None, config.num_notes + 1])
        self.audio_frames_gen = tf.placeholder(tf.int32, shape=[None])
        self.audio_frames_gen_one_hot = tf.one_hot(self.audio_frames_gen, self.quant_channels)

        self.pred_op = self.get_predict_op()
        self.repred_op = self.get_repredict_op(self.pred_op)
        self.loss_op = self.get_loss_op(self.pred_op, self.repred_op)
        self.train_op = self.get_training_op(self.loss_op)
        self.generator_op, self.init_ops, self.push_ops = self.get_generator_op()

    def get_predict_op(self):
        with tf.variable_scope("pred"):
            condition_tensor = self._get_condition_tensor()
            audio_frames_one_hot = wavenet_ops.one_hot(self.audio_frames, self.quant_channels)
            if self.config.dropout_prob > 0:
                audio_frames_one_hot = tf.nn.dropout(audio_frames_one_hot, keep_prob = 1 - self.config.dropout_prob)
            audio_preds = self.conv_net.encode(audio_frames_one_hot, condition_tensor)
            return audio_preds

    def get_repredict_op(self, pred_op):
        with tf.variable_scope("pred", reuse=True):
            condition_tensor = self._get_condition_tensor()
            audio_frames_one_hot = wavenet_ops.one_hot(self.audio_frames[:, 0:self.config.window_length], self.quant_channels)
            pred_op_with_window = tf.concat([audio_frames_one_hot, pred_op], axis = 1)
            audio_preds = self.conv_net.encode(pred_op_with_window, condition_tensor)
            return audio_preds

    def get_generator_op(self):
        with tf.variable_scope("pred", reuse=True):
            if constants.USE_VIDEO:
                condition_features = self.video_encoder.encode(self.video_frames_gen)
            else:
                condition_features = self.notes_gen

            audio_preds, init_ops, push_ops = self.conv_net.generate(self.audio_frames_gen_one_hot, condition_features)

            return tf.nn.softmax(audio_preds), init_ops, push_ops

    def get_training_op(self, loss):
        with tf.variable_scope("train"):
            params = tf.trainable_variables()

            global_step = tf.Variable(0, trainable=False)
            learning_rate = tf.train.exponential_decay(self.config.learning_rate,
                                                       global_step = global_step,
                                                       decay_steps = self.config.learning_rate_decay_steps,
                                                       decay_rate = self.config.learning_rate_decay_rate,
                                                       staircase=True)

            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, epsilon=1e-4)

            gradients, self.grad_norm = tf.clip_by_global_norm(
                tf.gradients(loss, params), self.config.max_grad_norm)

            return optimizer.apply_gradients(zip(gradients, params),
                                             global_step = global_step)

    def get_loss_op(self, pred_op, repred_op):
        with tf.variable_scope("loss"):
            audio_frames_one_hot = wavenet_ops.one_hot(self.audio_frames, self.quant_channels)
            target_audio = tf.slice(audio_frames_one_hot,
                                   [0, self.config.window_length, 0],
                                   [-1, -1, -1])

            logits_pred = tf.reshape(pred_op, [-1, self.quant_channels])
            logits_repred = tf.reshape(repred_op, [-1, self.quant_channels])
            labels = tf.reshape(target_audio, [-1, self.quant_channels])

            loss_pred = tf.nn.softmax_cross_entropy_with_logits(logits = logits_pred, labels = labels)
            reduced_loss_pred = tf.reduce_mean(loss_pred)

            loss_repred = tf.nn.softmax_cross_entropy_with_logits(logits = logits_repred, labels = labels)
            reduced_loss_repred = tf.reduce_mean(loss_repred)

            if self.config.l2_reg_strength > 0:
                self.l2_loss = self.config.l2_reg_strength * tf.add_n(
                    [tf.nn.l2_loss(v) for v in tf.trainable_variables() if not('bias' in v.name)])
            else:
                self.l2_loss = tf.constant(0.0)

            if self.config.repred_strength > 0:
                self.repred_loss = self.config.repred_strength * reduced_loss_repred
            else:
                self.repred_loss = tf.constant(0.0)

            total_loss = (reduced_loss_pred + self.repred_loss + self.l2_loss)

            return total_loss

    def generate(self, session, condition_frames, prev_audio_frames):
        input_feed = {
            self.audio_frames_gen: prev_audio_frames
        }
        if constants.USE_VIDEO:
            input_feed[self.video_frames_gen] = condition_frames
        else:
            condition_frames = np.pad(condition_frames, [[0, 0], [1, 0]], 'constant', constant_values = 1)
            input_feed[self.notes_gen] = condition_frames

        output_feed = [
            self.generator_op
        ] + self.push_ops

        return session.run(output_feed, input_feed)[0]

    def optimize(self, session, data):
        VIDEO_SEGS = 0
        VIDEO_TIMES = 1
        NOTE_SEGS = 2
        NOTE_TIMES = 3
        AUDIO_SEGS = 4

        idxs = self._get_sparse_idxs(data[VIDEO_SEGS], data[VIDEO_TIMES], self.config.frame_config.num_features)
        idxs_notes = self._get_sparse_idxs(data[NOTE_SEGS], data[NOTE_TIMES], self.config.num_notes)

        input_feed = {
            self.video_frames: np.concatenate(data[VIDEO_SEGS]),
            self.audio_frames: data[AUDIO_SEGS],
            self.sparse_idxs: idxs,
            self.notes: np.concatenate(data[NOTE_SEGS]),
            self.note_sparse_idxs: idxs_notes
        }

        output_feed = [
            self.train_op,
            self.loss_op,
            self.grad_norm,
            self.repred_loss,
            self.l2_loss
        ]

        return session.run(output_feed, input_feed)

    def _get_sparse_idxs(self, segments, segment_times, num_features):
        frames_per_segment = np.array([len(frames) for frames in segments])
        frame_times = np.concatenate(segment_times)

        splits = np.cumsum(frames_per_segment)[0:-1]
        segments = np.split(frame_times, splits)
        segment_idxs = []
        for i, segment in enumerate(segments):
            idxs = np.stack([
                np.repeat(i, num_features * len(segment)),
                np.repeat(segment, num_features),
                np.tile(range(num_features), len(segment))
            ]).T
            segment_idxs.append(idxs)

        return np.concatenate(segment_idxs, axis=0)

    def _get_condition_tensor(self):
        if constants.USE_VIDEO:
            dense_video_shape = [
                self.config.batch_size,
                self.config.audio_frames_per_segment,
                self.config.frame_config.num_features
            ]
            condition_tensor = self.video_encoder.encode_sparse(self.video_frames,
                                                                self.sparse_idxs,
                                                                dense_shape = dense_video_shape)

            return tf.sparse_tensor_to_dense(condition_tensor)
        else:
            dense_notes_shape = [
                self.config.batch_size,
                self.config.audio_frames_per_segment,
                self.config.num_notes
            ]

            condition_tensor = tf.SparseTensor(
                self.note_sparse_idxs,
                tf.reshape(self.notes, [-1]),
                dense_shape = dense_notes_shape)
            dense = tf.sparse_tensor_to_dense(condition_tensor)
            dense = tf.pad(dense, [[0, 0], [0, 0], [1, 0]])

            return dense + self.notes_one_pad

    def _get_condition_width(self):
        if constants.USE_VIDEO:
            return self.config.frame_config.num_features
        else:
            return self.config.num_notes + 1


from keras.datasets import imdb
from keras import preprocessing

# 사용할 단어수
max_features = 1000
# 텍스트 길이
max_len = 20

#이롬설명 참고
# https://subinium.github.io/Keras-6-1/

# 데이터 셋 생성
# 데이터 로드
# 영화평 중 자주 사용하는 단어 1000개만
(X_train, y_train),(X_test, y_test) = imdb.load_data(num_words=max_features)

# 모든 영화평을 20개 단어로 길이로 동일하게 만듦
X_train = preprocessing.sequence.pad_sequences(X_train, maxlen=max_len)
X_test = preprocessing.sequence.pad_sequences(X_test, maxlen=max_len)
# 모델 구축
from keras.models import Sequential
from keras.layers import Flatten, Dense, Embedding
model = Sequential()
model.add(Embedding(10000, 8, input_length=max_len))
#출력갯수: samples, max_length * 8
model.add(Flatten())
model.add(Dense(1, activation='sigmoid')) # 긍정/ 부정 평가
# 모델 설정
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test,y_test))
model.summary()


import csv
import pandas
import json
import folium
import requests
import re
import branca
import os
from pyecharts.charts import Line
from pyecharts import options as opts
# from .source.code import utils

def decode_address(encode):
    decode_file = './source/data/daily_info/address_decode.json'
    with open(decode_file, "r", encoding='utf-8') as f:
        decode_data = json.load(f)
    if encode in decode_data.keys():
        return decode_data[encode]
    else:
        return None

def parse_to_dict(csvname, saved_dict):
    with open(csvname, mode='r') as f:
        reader = csv.reader(f)
        for i, row in enumerate(reader):
            saved_dict[row[0]] = row[1:]
def days_to_string(start_day = "4-1", duration = 40):
    all_days=[]
    current_day = start_day
    for i in range(duration):
        all_days.append(current_day)
        day = int(current_day.split('-')[1])
        month = int(current_day.split('-')[0])
        day += 1
        if(day==31):
            day=1
            month += 1
        current_day = "%s-%s"%(month,day)
    print(all_days)
    return all_days

def getGPS(address: str, myAK='27gAPLcAkOCzK6NGQVyRyMQYPGjRBa9F'):
    # myAK = '27gAPLcAkOCzK6NGQVyRyMQYPGjRBa9F'
    url = 'https://api.map.baidu.com/geocoding/v3/?address=' + \
        address + '&output=json&coordtype=bd09ll&ak=' + myAK
    res = requests.get(url)
    res.data = res.content.decode()
    # print(res.data)
    pattern = '\"lng\"\:(\d+\.\d+),\"lat\"\:(\d+\.\d+)'
    try:
        return re.findall(pattern, res.data)[0]
    except:
        print(res.data)

def parse_zhch(s):
    return str(str(s).encode('ascii', 'xmlcharrefreplace'))[2:-1]

def mapping():
    qz = {}
    wzz = {}
    parse_to_dict('./qz.csv',qz)
    parse_to_dict('./wzz.csv',wzz)
    pre_qz = {}
    pre_wzz = {}
    parse_to_dict('./pred_qz.csv',pre_qz)
    parse_to_dict('./pred_wzz.csv',pre_wzz)

    all_days = days_to_string("4-1",duration=len(qz["Shanghai"]))

    pudongnew_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    pudongnew_charts.add_xaxis(xaxis_data=all_days)
    pudongnew_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["PudongNew"],label_opts = opts.LabelOpts(is_show=False))
    pudongnew_charts.set_global_opts(title_opts = opts.TitleOpts(title="浦东新区每日新增确诊人数", pos_top = "20px"))
    pudongnew_charts.render("pudongnew_charts.html")
    pudongnew_html = open("pudongnew_charts.html","r",encoding="utf-8").read()
    pudongnew_iframe = branca.element.IFrame(html=pudongnew_html, width=500, height=300)
    pudongnew_popup = folium.Popup(pudongnew_iframe, max_width=500)

    huangpu_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    huangpu_charts.add_xaxis(xaxis_data=all_days)
    huangpu_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Huangpu"],label_opts = opts.LabelOpts(is_show=False))
    huangpu_charts.set_global_opts(title_opts = opts.TitleOpts(title="黄浦区每日新增确诊人数", pos_top = "20px"))
    huangpu_charts.render("huangpu_charts.html")
    huangpu_html = open("huangpu_charts.html","r",encoding="utf-8").read()
    huangpu_iframe = branca.element.IFrame(html=huangpu_html, width=500, height=300)
    huangpu_popup = folium.Popup(huangpu_iframe, max_width=500)

    xuhui_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    xuhui_charts.add_xaxis(xaxis_data=all_days)
    xuhui_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Xuhui"],label_opts = opts.LabelOpts(is_show=False))
    xuhui_charts.set_global_opts(title_opts = opts.TitleOpts(title="徐汇区每日新增确诊人数", pos_top = "20px"))
    xuhui_charts.render("xuhui_charts.html")
    xuhui_html = open("xuhui_charts.html","r",encoding="utf-8").read()
    xuhui_iframe = branca.element.IFrame(html=xuhui_html, width=500, height=300)
    xuhui_popup = folium.Popup(xuhui_iframe, max_width=500)

    changning_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    changning_charts.add_xaxis(xaxis_data=all_days)
    changning_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Changning"],label_opts = opts.LabelOpts(is_show=False))
    changning_charts.set_global_opts(title_opts = opts.TitleOpts(title="长宁区每日新增确诊人数", pos_top = "20px"))
    changning_charts.render("changning_charts.html")
    changning_html = open("changning_charts.html","r",encoding="utf-8").read()
    changning_iframe = branca.element.IFrame(html=changning_html, width=500, height=300)
    changning_popup = folium.Popup(changning_iframe, max_width=500)

    jingan_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    jingan_charts.add_xaxis(xaxis_data=all_days)
    jingan_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Jingan"],label_opts = opts.LabelOpts(is_show=False))
    jingan_charts.set_global_opts(title_opts = opts.TitleOpts(title="静安区每日新增确诊人数", pos_top = "20px"))
    jingan_charts.render("jingan_charts.html")
    jingan_html = open("jingan_charts.html","r",encoding="utf-8").read()
    jingan_iframe = branca.element.IFrame(html=jingan_html, width=500, height=300)
    jingan_popup = folium.Popup(jingan_iframe, max_width=500)

    putuo_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    putuo_charts.add_xaxis(xaxis_data=all_days)
    putuo_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Putuo"],label_opts = opts.LabelOpts(is_show=False))
    putuo_charts.set_global_opts(title_opts = opts.TitleOpts(title="普陀区每日新增确诊人数", pos_top = "20px"))
    putuo_charts.render("putuo_charts.html")
    putuo_html = open("putuo_charts.html","r",encoding="utf-8").read()
    putuo_iframe = branca.element.IFrame(html=putuo_html, width=500, height=300)
    putuo_popup = folium.Popup(putuo_iframe, max_width=500)

    hongkou_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    hongkou_charts.add_xaxis(xaxis_data=all_days)
    hongkou_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Hongkou"],label_opts = opts.LabelOpts(is_show=False))
    hongkou_charts.set_global_opts(title_opts = opts.TitleOpts(title="虹口区每日新增确诊人数", pos_top = "20px"))
    hongkou_charts.render("hongkou_charts.html")
    hongkou_html = open("hongkou_charts.html","r",encoding="utf-8").read()
    hongkou_iframe = branca.element.IFrame(html=hongkou_html, width=500, height=300)
    hongkou_popup = folium.Popup(hongkou_iframe, max_width=500)

    yangpu_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    yangpu_charts.add_xaxis(xaxis_data=all_days)
    yangpu_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Yangpu"],label_opts = opts.LabelOpts(is_show=False))
    yangpu_charts.set_global_opts(title_opts = opts.TitleOpts(title="杨浦区每日新增确诊人数", pos_top = "20px"))
    yangpu_charts.render("yangpu_charts.html")
    yangpu_html = open("yangpu_charts.html","r",encoding="utf-8").read()
    yangpu_iframe = branca.element.IFrame(html=yangpu_html, width=500, height=300)
    yangpu_popup = folium.Popup(yangpu_iframe, max_width=500)

    minhang_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    minhang_charts.add_xaxis(xaxis_data=all_days)
    minhang_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Minhang"],label_opts = opts.LabelOpts(is_show=False))
    minhang_charts.set_global_opts(title_opts = opts.TitleOpts(title="闵行区每日新增确诊人数", pos_top = "20px"))
    minhang_charts.render("minhang_charts.html")
    minhang_html = open("minhang_charts.html","r",encoding="utf-8").read()
    minhang_iframe = branca.element.IFrame(html=minhang_html, width=500, height=300)
    minhang_popup = folium.Popup(minhang_iframe, max_width=500)

    baoshan_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    baoshan_charts.add_xaxis(xaxis_data=all_days)
    baoshan_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Baoshan"],label_opts = opts.LabelOpts(is_show=False))
    baoshan_charts.set_global_opts(title_opts = opts.TitleOpts(title="宝山区每日新增确诊人数", pos_top = "20px"))
    baoshan_charts.render("baoshan_charts.html")
    baoshan_html = open("baoshan_charts.html","r",encoding="utf-8").read()
    baoshan_iframe = branca.element.IFrame(html=baoshan_html, width=500, height=300)
    baoshan_popup = folium.Popup(baoshan_iframe, max_width=500)

    jiading_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    jiading_charts.add_xaxis(xaxis_data=all_days)
    jiading_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Jiading"],label_opts = opts.LabelOpts(is_show=False))
    jiading_charts.set_global_opts(title_opts = opts.TitleOpts(title="嘉定区每日新增确诊人数", pos_top = "20px"))
    jiading_charts.render("jiading_charts.html")
    jiading_html = open("jiading_charts.html","r",encoding="utf-8").read()
    jiading_iframe = branca.element.IFrame(html=jiading_html, width=500, height=300)
    jiading_popup = folium.Popup(jiading_iframe, max_width=500)

    jinshan_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    jinshan_charts.add_xaxis(xaxis_data=all_days)
    jinshan_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Jinshan"],label_opts = opts.LabelOpts(is_show=False))
    jinshan_charts.set_global_opts(title_opts = opts.TitleOpts(title="金山区每日新增确诊人数", pos_top = "20px"))
    jinshan_charts.render("jinshan_charts.html")
    jinshan_html = open("jinshan_charts.html","r",encoding="utf-8").read()
    jinshan_iframe = branca.element.IFrame(html=jinshan_html, width=500, height=300)
    jinshan_popup = folium.Popup(jinshan_iframe, max_width=500)

    songjiang_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    songjiang_charts.add_xaxis(xaxis_data=all_days)
    songjiang_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Songjiang"],label_opts = opts.LabelOpts(is_show=False))
    songjiang_charts.set_global_opts(title_opts = opts.TitleOpts(title="松江区每日新增确诊人数", pos_top = "20px"))
    songjiang_charts.render("songjiang_charts.html")
    songjiang_html = open("songjiang_charts.html","r",encoding="utf-8").read()
    songjiang_iframe = branca.element.IFrame(html=songjiang_html, width=500, height=300)
    songjiang_popup = folium.Popup(songjiang_iframe, max_width=500)

    qingpu_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    qingpu_charts.add_xaxis(xaxis_data=all_days)
    qingpu_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Qingpu"],label_opts = opts.LabelOpts(is_show=False))
    qingpu_charts.set_global_opts(title_opts = opts.TitleOpts(title="青浦区每日新增确诊人数", pos_top = "20px"))
    qingpu_charts.render("qingpu_charts.html")
    qingpu_html = open("qingpu_charts.html","r",encoding="utf-8").read()
    qingpu_iframe = branca.element.IFrame(html=qingpu_html, width=500, height=300)
    qingpu_popup = folium.Popup(qingpu_iframe, max_width=500)

    fengxian_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    fengxian_charts.add_xaxis(xaxis_data=all_days)
    fengxian_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Fengxian"],label_opts = opts.LabelOpts(is_show=False))
    fengxian_charts.set_global_opts(title_opts = opts.TitleOpts(title="奉贤区每日新增确诊人数", pos_top = "20px"))
    fengxian_charts.render("fengxian_charts.html")
    fengxian_html = open("fengxian_charts.html","r",encoding="utf-8").read()
    fengxian_iframe = branca.element.IFrame(html=fengxian_html, width=500, height=300)
    fengxian_popup = folium.Popup(fengxian_iframe, max_width=500)

    chongming_charts = Line(init_opts = opts.InitOpts(width = '500px',height = '300px'))
    chongming_charts.add_xaxis(xaxis_data=all_days)
    chongming_charts.add_yaxis(series_name = '当日新增确诊人数',y_axis = qz["Chongming"],label_opts = opts.LabelOpts(is_show=False))
    chongming_charts.set_global_opts(title_opts = opts.TitleOpts(title="崇明区每日新增确诊人数", pos_top = "20px"))
    chongming_charts.render("chongming_charts.html")
    chongming_html = open("chongming_charts.html","r",encoding="utf-8").read()
    chongming_iframe = branca.element.IFrame(html=chongming_html, width=500, height=300)
    chongming_popup = folium.Popup(chongming_iframe, max_width=500)

    csv_file = './source/data/CaseInfo_April/daily_report_by_address.csv'
    colormap = branca.colormap.linear.YlOrRd_09.scale(0, 1).to_step(4)
    df = pandas.read_csv(csv_file, encoding='utf-8', dtype={'addressID': str})
    shanghai = folium.Map(
        location=[31.2304, 121.4737], zoom_start=12, min_zoom=10)
    colormap.caption = "距离最近一次通报疫情天数"
    colormap.add_to(shanghai)
    # print(df[0]['encode'])
    PudongNew = folium.FeatureGroup(name=parse_zhch("浦东新区"))
    Huangpu = folium.FeatureGroup(name=parse_zhch("黄浦区"))
    Xuhui = folium.FeatureGroup(name=parse_zhch("徐汇区"))
    Changning = folium.FeatureGroup(name=parse_zhch("长宁区"))
    Jingan = folium.FeatureGroup(name=parse_zhch("静安区"))
    Putuo = folium.FeatureGroup(name=parse_zhch("普陀区"))
    Hongkou = folium.FeatureGroup(name=parse_zhch("虹口区"))
    Yangpu = folium.FeatureGroup(name=parse_zhch("杨浦区"))
    Minhang = folium.FeatureGroup(name=parse_zhch("闵行区"))
    Baoshan = folium.FeatureGroup(name=parse_zhch("宝山区"))
    Jiading = folium.FeatureGroup(name=parse_zhch("嘉定区"))
    Jinshan = folium.FeatureGroup(name=parse_zhch("金山区"))
    Songjiang = folium.FeatureGroup(name=parse_zhch("松江区"))
    Qingpu = folium.FeatureGroup(name=parse_zhch("青浦区"))
    Fengxian = folium.FeatureGroup(name=parse_zhch("奉贤区"))
    Chongming = folium.FeatureGroup(name=parse_zhch("崇明区"))
    gps_xuhui = [31.194804,121.443108]
    gps_hongkou = [31.270886,121.510373]
    gps_minhang = [31.11866,121.387916]
    gps_pudongnew = [31.226114,121.558216]
    gps_putuo = [31.254588,121.403438]
    gps_changning = [31.22643,121.430459]
    gps_jingan = [31.234088,121.453169]
    gps_yangpu = [31.265577,121.532651]
    gps_baoshan = [31.410168,121.496]
    gps_jiading = [31.413445,121.268854]
    gps_songjiang = [31.057646,121.237961]
    gps_qingpu = [31.154218,121.131413]
    gps_fengxian = [30.923968,121.480217]
    gps_chongming = [31.629307,121.405567]
    gps_jinshan = [30.746611,121.348767]
    gps_huangpu = [31.236351,121.480342]
    pudongnew = folium.Marker(location=gps_pudongnew,popup=pudongnew_popup).add_to(shanghai)
    xuhui = folium.Marker(location=gps_xuhui,popup=xuhui_popup).add_to(shanghai)
    hongkou = folium.Marker(location=gps_hongkou,popup=hongkou_popup).add_to(shanghai)
    minhang = folium.Marker(location=gps_minhang,popup=minhang_popup).add_to(shanghai)
    putuo = folium.Marker(location=gps_putuo,popup=putuo_popup).add_to(shanghai)
    changning = folium.Marker(location=gps_changning,popup=changning_popup).add_to(shanghai)
    jingan = folium.Marker(location=gps_jingan,popup=jingan_popup).add_to(shanghai)
    yangpu = folium.Marker(gps_yangpu,popup=yangpu_popup).add_to(shanghai)
    baoshan = folium.Marker(gps_baoshan,popup=baoshan_popup).add_to(shanghai)
    jiading = folium.Marker(gps_jiading,popup=jiading_popup).add_to(shanghai)
    songjiang = folium.Marker(gps_songjiang,popup=songjiang_popup).add_to(shanghai)
    qingpu = folium.Marker(gps_qingpu,popup=qingpu_popup).add_to(shanghai)
    fengxian = folium.Marker(gps_fengxian,popup=fengxian_popup).add_to(shanghai)
    chongming = folium.Marker(gps_chongming,popup=chongming_popup).add_to(shanghai)
    jinshan = folium.Marker(gps_jinshan,popup=jinshan_popup).add_to(shanghai)
    huangpu = folium.Marker(gps_huangpu,popup=huangpu_popup).add_to(shanghai)
    for i, item in df.iterrows():
        encode = item['addressID']
        # print(encode)
        decode_gps = decode_address(encode)
        # print(decode_gps)
        if(decode_gps == None):
            continue
        gps = getGPS(decode_gps)
        if (gps == None):
            continue
        gps = list(gps)
        tmp = float(gps[1])
        gps[1] = float(gps[0])
        gps[0] = tmp
        total_day = len(item)-1
        last_report = 15
        report_times = 0
        base_radius = 10
        add_radius = 3
        for j, Item in enumerate(item):
            if j == 0:
                continue
            if Item == 1:
                report_times += 1
                last_report = total_day - j
        text = folium.Popup((parse_zhch(decode_gps)+"。累计通报次数为%s次，最近一次通报为%s天前。"%(report_times,last_report)),max_width=100)
        if last_report > 14:
            tmp_circle = folium.Circle(location=gps, color=colormap.rgb_hex_str(
                1/4.0), fill=colormap.rgb_hex_str(1/4.0), radius=base_radius + add_radius * report_times, popup=text)
            # tmp_circle.add_to(data_15)
            if encode[3:5]=='01':
                tmp_circle.add_to(PudongNew)
            elif encode[3:5]=='02':
                tmp_circle.add_to(Hongkou)
            elif encode[3:5]=='03':
                tmp_circle.add_to(Minhang)
            elif encode[3:5]=='04':
                tmp_circle.add_to(Xuhui)
            elif encode[3:5]=='05':
                tmp_circle.add_to(Huangpu)
            elif encode[3:5]=='06':
                tmp_circle.add_to(Jiading)
            elif encode[3:5]=='07':
                tmp_circle.add_to(Jingan)
            elif encode[3:5]=='08':
                tmp_circle.add_to(Yangpu)
            elif encode[3:5]=='09':
                tmp_circle.add_to(Baoshan)
            elif encode[3:5]=='10':
                tmp_circle.add_to(Fengxian)
            elif encode[3:5]=='11':
                tmp_circle.add_to(Songjiang)
            elif encode[3:5]=='12':
                tmp_circle.add_to(Chongming)
            elif encode[3:5]=='13':
                tmp_circle.add_to(Qingpu)
            elif encode[3:5]=='14':
                tmp_circle.add_to(Jinshan)
            elif encode[3:5]=='15':
                tmp_circle.add_to(Changning)
            elif encode[3:5]=='16':
                tmp_circle.add_to(Putuo)
            # print(1)
        elif last_report <= 14 and last_report > 7:
            tmp_circle = folium.Circle(location=gps, color=colormap.rgb_hex_str(
                2/4.0), fill=colormap.rgb_hex_str(2/4.0), radius=base_radius + add_radius * report_times, popup=text)
            # tmp_circle.add_to(data_7_14)
            if encode[3:5]=='01':
                tmp_circle.add_to(PudongNew)
            elif encode[3:5]=='02':
                tmp_circle.add_to(Hongkou)
            elif encode[3:5]=='03':
                tmp_circle.add_to(Minhang)
            elif encode[3:5]=='04':
                tmp_circle.add_to(Xuhui)
            elif encode[3:5]=='05':
                tmp_circle.add_to(Huangpu)
            elif encode[3:5]=='06':
                tmp_circle.add_to(Jiading)
            elif encode[3:5]=='07':
                tmp_circle.add_to(Jingan)
            elif encode[3:5]=='08':
                tmp_circle.add_to(Yangpu)
            elif encode[3:5]=='09':
                tmp_circle.add_to(Baoshan)
            elif encode[3:5]=='10':
                tmp_circle.add_to(Fengxian)
            elif encode[3:5]=='11':
                tmp_circle.add_to(Songjiang)
            elif encode[3:5]=='12':
                tmp_circle.add_to(Chongming)
            elif encode[3:5]=='13':
                tmp_circle.add_to(Qingpu)
            elif encode[3:5]=='14':
                tmp_circle.add_to(Jinshan)
            elif encode[3:5]=='15':
                tmp_circle.add_to(Changning)
            elif encode[3:5]=='16':
                tmp_circle.add_to(Putuo)
        elif last_report <= 7 and last_report > 1:
            tmp_circle = folium.Circle(location=gps, color=colormap.rgb_hex_str(
                3/4.0), fill=colormap.rgb_hex_str(3/4.0), radius=base_radius + add_radius * report_times, popup=text)
            # tmp_circle.add_to(data_1_7)
            if encode[3:5]=='01':
                tmp_circle.add_to(PudongNew)
            elif encode[3:5]=='02':
                tmp_circle.add_to(Hongkou)
            elif encode[3:5]=='03':
                tmp_circle.add_to(Minhang)
            elif encode[3:5]=='04':
                tmp_circle.add_to(Xuhui)
            elif encode[3:5]=='05':
                tmp_circle.add_to(Huangpu)
            elif encode[3:5]=='06':
                tmp_circle.add_to(Jiading)
            elif encode[3:5]=='07':
                tmp_circle.add_to(Jingan)
            elif encode[3:5]=='08':
                tmp_circle.add_to(Yangpu)
            elif encode[3:5]=='09':
                tmp_circle.add_to(Baoshan)
            elif encode[3:5]=='10':
                tmp_circle.add_to(Fengxian)
            elif encode[3:5]=='11':
                tmp_circle.add_to(Songjiang)
            elif encode[3:5]=='12':
                tmp_circle.add_to(Chongming)
            elif encode[3:5]=='13':
                tmp_circle.add_to(Qingpu)
            elif encode[3:5]=='14':
                tmp_circle.add_to(Jinshan)
            elif encode[3:5]=='15':
                tmp_circle.add_to(Changning)
            elif encode[3:5]=='16':
                tmp_circle.add_to(Putuo)
        else:
            tmp_circle = folium.Circle(location=gps, color=colormap.rgb_hex_str(
                4/4.0), fill=colormap.rgb_hex_str(4/4.0), radius=base_radius + add_radius * report_times, popup=text)
            # tmp_circle.add_to(data_1)
            if encode[3:5]=='01':
                tmp_circle.add_to(PudongNew)
            elif encode[3:5]=='02':
                tmp_circle.add_to(Hongkou)
            elif encode[3:5]=='03':
                tmp_circle.add_to(Minhang)
            elif encode[3:5]=='04':
                tmp_circle.add_to(Xuhui)
            elif encode[3:5]=='05':
                tmp_circle.add_to(Huangpu)
            elif encode[3:5]=='06':
                tmp_circle.add_to(Jiading)
            elif encode[3:5]=='07':
                tmp_circle.add_to(Jingan)
            elif encode[3:5]=='08':
                tmp_circle.add_to(Yangpu)
            elif encode[3:5]=='09':
                tmp_circle.add_to(Baoshan)
            elif encode[3:5]=='10':
                tmp_circle.add_to(Fengxian)
            elif encode[3:5]=='11':
                tmp_circle.add_to(Songjiang)
            elif encode[3:5]=='12':
                tmp_circle.add_to(Chongming)
            elif encode[3:5]=='13':
                tmp_circle.add_to(Qingpu)
            elif encode[3:5]=='14':
                tmp_circle.add_to(Jinshan)
            elif encode[3:5]=='15':
                tmp_circle.add_to(Changning)
            elif encode[3:5]=='16':
                tmp_circle.add_to(Putuo)
        if(i%400==0):
            # break
            print(i)
        # print(last_report)
        # print(gps)
    Minhang.add_to(shanghai)
    PudongNew.add_to(shanghai)
    Xuhui.add_to(shanghai)
    Changning.add_to(shanghai)
    Jingan.add_to(shanghai)
    Putuo.add_to(shanghai)
    Hongkou.add_to(shanghai)
    Yangpu.add_to(shanghai)
    Baoshan.add_to(shanghai)
    Jiading.add_to(shanghai)
    Jinshan.add_to(shanghai)
    Songjiang.add_to(shanghai)
    Qingpu.add_to(shanghai)
    Fengxian.add_to(shanghai)
    Chongming.add_to(shanghai)
    folium.LayerControl().add_to(shanghai)
    os.remove("pudongnew_charts.html")
    os.remove("huangpu_charts.html")
    os.remove("xuhui_charts.html")
    os.remove("changning_charts.html")
    os.remove("jingan_charts.html")
    os.remove("putuo_charts.html")
    os.remove("hongkou_charts.html")
    os.remove("yangpu_charts.html")
    os.remove("minhang_charts.html")
    os.remove("baoshan_charts.html")
    os.remove("jiading_charts.html")
    os.remove("jinshan_charts.html")
    os.remove("songjiang_charts.html")
    os.remove("qingpu_charts.html")
    os.remove("fengxian_charts.html")
    os.remove("chongming_charts.html")
    shanghai.save("test.html")
mapping()